Commit | Line | Data |
---|---|---|
c8df7428 CG |
1 | /* |
2 | * Copyright (C) 2012 Bachmann electronic GmbH | |
3 | * Christian Gmeiner <christian.gmeiner@gmail.com> | |
4 | * | |
5 | * Backlight driver for ot200 visualisation device from | |
6 | * Bachmann electronic GmbH. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License version 2 as published by | |
10 | * the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/fb.h> | |
15 | #include <linux/backlight.h> | |
16 | #include <linux/gpio.h> | |
16559ae4 | 17 | #include <linux/platform_device.h> |
c8df7428 CG |
18 | #include <linux/cs5535.h> |
19 | ||
20 | static struct cs5535_mfgpt_timer *pwm_timer; | |
21 | ||
22 | /* this array defines the mapping of brightness in % to pwm frequency */ | |
23 | static const u8 dim_table[101] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, | |
24 | 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, | |
25 | 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, | |
26 | 10, 10, 11, 11, 12, 12, 13, 14, 15, 15, 16, | |
27 | 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, | |
28 | 30, 31, 33, 35, 37, 39, 41, 43, 45, 47, 50, | |
29 | 53, 55, 58, 61, 65, 68, 72, 75, 79, 84, 88, | |
30 | 93, 97, 103, 108, 114, 120, 126, 133, 140, | |
31 | 147, 155, 163}; | |
32 | ||
33 | struct ot200_backlight_data { | |
34 | int current_brightness; | |
35 | }; | |
36 | ||
37 | #define GPIO_DIMM 27 | |
38 | #define SCALE 1 | |
39 | #define CMP1MODE 0x2 /* compare on GE; output high on compare | |
40 | * greater than or equal */ | |
41 | #define PWM_SETUP (SCALE | CMP1MODE << 6 | MFGPT_SETUP_CNTEN) | |
42 | #define MAX_COMP2 163 | |
43 | ||
44 | static int ot200_backlight_update_status(struct backlight_device *bl) | |
45 | { | |
46 | struct ot200_backlight_data *data = bl_get_data(bl); | |
47 | int brightness = bl->props.brightness; | |
48 | ||
49 | if (bl->props.state & BL_CORE_FBBLANK) | |
50 | brightness = 0; | |
51 | ||
52 | /* enable or disable PWM timer */ | |
53 | if (brightness == 0) | |
54 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, 0); | |
55 | else if (data->current_brightness == 0) { | |
56 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0); | |
57 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, | |
58 | MFGPT_SETUP_CNTEN); | |
59 | } | |
60 | ||
61 | /* apply new brightness value */ | |
62 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, | |
63 | MAX_COMP2 - dim_table[brightness]); | |
64 | data->current_brightness = brightness; | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
69 | static int ot200_backlight_get_brightness(struct backlight_device *bl) | |
70 | { | |
71 | struct ot200_backlight_data *data = bl_get_data(bl); | |
72 | return data->current_brightness; | |
73 | } | |
74 | ||
75 | static const struct backlight_ops ot200_backlight_ops = { | |
76 | .update_status = ot200_backlight_update_status, | |
77 | .get_brightness = ot200_backlight_get_brightness, | |
78 | }; | |
79 | ||
80 | static int ot200_backlight_probe(struct platform_device *pdev) | |
81 | { | |
82 | struct backlight_device *bl; | |
83 | struct ot200_backlight_data *data; | |
84 | struct backlight_properties props; | |
85 | int retval = 0; | |
86 | ||
87 | /* request gpio */ | |
f673934c JH |
88 | if (devm_gpio_request(&pdev->dev, GPIO_DIMM, |
89 | "ot200 backlight dimmer") < 0) { | |
c8df7428 CG |
90 | dev_err(&pdev->dev, "failed to request GPIO %d\n", GPIO_DIMM); |
91 | return -ENODEV; | |
92 | } | |
93 | ||
94 | /* request timer */ | |
95 | pwm_timer = cs5535_mfgpt_alloc_timer(7, MFGPT_DOMAIN_ANY); | |
96 | if (!pwm_timer) { | |
97 | dev_err(&pdev->dev, "MFGPT 7 not available\n"); | |
f673934c | 98 | return -ENODEV; |
c8df7428 CG |
99 | } |
100 | ||
f1b60d46 | 101 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
c8df7428 CG |
102 | if (!data) { |
103 | retval = -ENOMEM; | |
f1b60d46 | 104 | goto error_devm_kzalloc; |
c8df7428 CG |
105 | } |
106 | ||
107 | /* setup gpio */ | |
108 | cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_ENABLE); | |
109 | cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_AUX1); | |
110 | ||
111 | /* setup timer */ | |
112 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, 0); | |
113 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP2, MAX_COMP2); | |
114 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, PWM_SETUP); | |
115 | ||
116 | data->current_brightness = 100; | |
117 | props.max_brightness = 100; | |
118 | props.brightness = 100; | |
119 | props.type = BACKLIGHT_RAW; | |
120 | ||
121 | bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, data, | |
122 | &ot200_backlight_ops, &props); | |
123 | if (IS_ERR(bl)) { | |
124 | dev_err(&pdev->dev, "failed to register backlight\n"); | |
125 | retval = PTR_ERR(bl); | |
f1b60d46 | 126 | goto error_devm_kzalloc; |
c8df7428 CG |
127 | } |
128 | ||
129 | platform_set_drvdata(pdev, bl); | |
130 | ||
131 | return 0; | |
132 | ||
f1b60d46 | 133 | error_devm_kzalloc: |
c8df7428 | 134 | cs5535_mfgpt_free_timer(pwm_timer); |
c8df7428 CG |
135 | return retval; |
136 | } | |
137 | ||
138 | static int ot200_backlight_remove(struct platform_device *pdev) | |
139 | { | |
140 | struct backlight_device *bl = platform_get_drvdata(pdev); | |
c8df7428 CG |
141 | |
142 | backlight_device_unregister(bl); | |
143 | ||
144 | /* on module unload set brightness to 100% */ | |
145 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0); | |
146 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); | |
147 | cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, | |
148 | MAX_COMP2 - dim_table[100]); | |
149 | ||
150 | cs5535_mfgpt_free_timer(pwm_timer); | |
c8df7428 | 151 | |
c8df7428 CG |
152 | return 0; |
153 | } | |
154 | ||
155 | static struct platform_driver ot200_backlight_driver = { | |
156 | .driver = { | |
157 | .name = "ot200-backlight", | |
158 | .owner = THIS_MODULE, | |
159 | }, | |
160 | .probe = ot200_backlight_probe, | |
161 | .remove = ot200_backlight_remove, | |
162 | }; | |
163 | ||
164 | module_platform_driver(ot200_backlight_driver); | |
165 | ||
166 | MODULE_DESCRIPTION("backlight driver for ot200 visualisation device"); | |
167 | MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>"); | |
168 | MODULE_LICENSE("GPL"); | |
169 | MODULE_ALIAS("platform:ot200-backlight"); |