]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/video/backlight/pwm_bl.c
pwm_backlight: add device tree support for Low Threshold Brightness
[karo-tx-linux.git] / drivers / video / backlight / pwm_bl.c
1 /*
2  * linux/drivers/video/backlight/pwm_bl.c
3  *
4  * simple PWM based backlight control, board code has to setup
5  * 1) pin configuration so PWM waveforms can output
6  * 2) platform_data being correctly configured
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/fb.h>
18 #include <linux/backlight.h>
19 #include <linux/err.h>
20 #include <linux/pwm.h>
21 #include <linux/pwm_backlight.h>
22 #include <linux/slab.h>
23
24 struct pwm_bl_data {
25         struct pwm_device       *pwm;
26         struct device           *dev;
27         unsigned int            period;
28         unsigned int            lth_brightness;
29         unsigned int            *levels;
30         int                     (*notify)(struct device *,
31                                           int brightness);
32         void                    (*notify_after)(struct device *,
33                                         int brightness);
34         int                     (*check_fb)(struct device *, struct fb_info *);
35         void                    (*exit)(struct device *);
36 };
37
38 static int pwm_backlight_update_status(struct backlight_device *bl)
39 {
40         struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
41         int brightness = bl->props.brightness;
42         int max = bl->props.max_brightness;
43
44         if (bl->props.power != FB_BLANK_UNBLANK)
45                 brightness = 0;
46
47         if (bl->props.fb_blank != FB_BLANK_UNBLANK)
48                 brightness = 0;
49
50         if (pb->notify)
51                 brightness = pb->notify(pb->dev, brightness);
52
53         if (brightness == 0) {
54                 pwm_config(pb->pwm, 0, pb->period);
55                 pwm_disable(pb->pwm);
56         } else {
57                 int duty_cycle;
58
59                 if (pb->levels) {
60                         duty_cycle = pb->levels[brightness];
61                         max = pb->levels[max];
62                 } else {
63                         duty_cycle = brightness;
64                 }
65
66                 duty_cycle = pb->lth_brightness +
67                      (duty_cycle * (pb->period - pb->lth_brightness) / max);
68                 pwm_config(pb->pwm, duty_cycle, pb->period);
69                 pwm_enable(pb->pwm);
70         }
71
72         if (pb->notify_after)
73                 pb->notify_after(pb->dev, brightness);
74
75         return 0;
76 }
77
78 static int pwm_backlight_get_brightness(struct backlight_device *bl)
79 {
80         return bl->props.brightness;
81 }
82
83 static int pwm_backlight_check_fb(struct backlight_device *bl,
84                                   struct fb_info *info)
85 {
86         struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
87
88         return !pb->check_fb || pb->check_fb(pb->dev, info);
89 }
90
91 static const struct backlight_ops pwm_backlight_ops = {
92         .update_status  = pwm_backlight_update_status,
93         .get_brightness = pwm_backlight_get_brightness,
94         .check_fb       = pwm_backlight_check_fb,
95 };
96
97 #ifdef CONFIG_OF
98 static int pwm_backlight_parse_dt(struct device *dev,
99                                   struct platform_pwm_backlight_data *data)
100 {
101         struct device_node *node = dev->of_node;
102         struct property *prop;
103         int length;
104         u32 value;
105         int ret;
106
107         if (!node)
108                 return -ENODEV;
109
110         memset(data, 0, sizeof(*data));
111
112         /* determine the number of brightness levels */
113         prop = of_find_property(node, "brightness-levels", &length);
114         if (!prop)
115                 return -EINVAL;
116
117         data->max_brightness = length / sizeof(u32);
118
119         /* read brightness levels from DT property */
120         if (data->max_brightness > 0) {
121                 size_t size = sizeof(*data->levels) * data->max_brightness;
122
123                 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
124                 if (!data->levels)
125                         return -ENOMEM;
126
127                 ret = of_property_read_u32_array(node, "brightness-levels",
128                                                  data->levels,
129                                                  data->max_brightness);
130                 if (ret < 0)
131                         return ret;
132
133                 ret = of_property_read_u32(node, "default-brightness-level",
134                                            &value);
135                 if (ret < 0)
136                         return ret;
137
138                 if (value >= data->max_brightness) {
139                         dev_warn(dev, "invalid default brightness level: %u, using %u\n",
140                                  value, data->max_brightness - 1);
141                         value = data->max_brightness - 1;
142                 }
143
144                 data->dft_brightness = value;
145                 data->max_brightness--;
146
147                 ret = of_property_read_u32(node, "low-threshold-brightness",
148                                            &value);
149                 if (!ret)
150                         data->lth_brightness = value;
151         }
152
153         /*
154          * TODO: Most users of this driver use a number of GPIOs to control
155          *       backlight power. Support for specifying these needs to be
156          *       added.
157          */
158
159         return 0;
160 }
161
162 static struct of_device_id pwm_backlight_of_match[] = {
163         { .compatible = "pwm-backlight" },
164         { }
165 };
166
167 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
168 #else
169 static int pwm_backlight_parse_dt(struct device *dev,
170                                   struct platform_pwm_backlight_data *data)
171 {
172         return -ENODEV;
173 }
174 #endif
175
176 static int pwm_backlight_probe(struct platform_device *pdev)
177 {
178         struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
179         struct platform_pwm_backlight_data defdata;
180         struct backlight_properties props;
181         struct backlight_device *bl;
182         struct pwm_bl_data *pb;
183         unsigned int max;
184         int ret;
185
186         if (!data) {
187                 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
188                 if (ret < 0) {
189                         dev_err(&pdev->dev, "failed to find platform data\n");
190                         return ret;
191                 }
192
193                 data = &defdata;
194         }
195
196         if (data->init) {
197                 ret = data->init(&pdev->dev);
198                 if (ret < 0)
199                         return ret;
200         }
201
202         pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
203         if (!pb) {
204                 dev_err(&pdev->dev, "no memory for state\n");
205                 ret = -ENOMEM;
206                 goto err_alloc;
207         }
208
209         if (data->levels) {
210                 max = data->levels[data->max_brightness];
211                 pb->levels = data->levels;
212         } else
213                 max = data->max_brightness;
214
215         pb->notify = data->notify;
216         pb->notify_after = data->notify_after;
217         pb->check_fb = data->check_fb;
218         pb->exit = data->exit;
219         pb->dev = &pdev->dev;
220
221         pb->pwm = devm_pwm_get(&pdev->dev, NULL);
222         if (IS_ERR(pb->pwm)) {
223                 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
224
225                 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
226                 if (IS_ERR(pb->pwm)) {
227                         dev_err(&pdev->dev, "unable to request legacy PWM\n");
228                         ret = PTR_ERR(pb->pwm);
229                         goto err_alloc;
230                 }
231         }
232
233         dev_dbg(&pdev->dev, "got pwm for backlight\n");
234
235         /*
236          * The DT case will set the pwm_period_ns field to 0 and store the
237          * period, parsed from the DT, in the PWM device. For the non-DT case,
238          * set the period from platform data.
239          */
240         if (data->pwm_period_ns > 0)
241                 pwm_set_period(pb->pwm, data->pwm_period_ns);
242
243         pb->period = pwm_get_period(pb->pwm);
244         pb->lth_brightness = data->lth_brightness * (pb->period / max);
245
246         memset(&props, 0, sizeof(struct backlight_properties));
247         props.type = BACKLIGHT_RAW;
248         props.max_brightness = data->max_brightness;
249         bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
250                                        &pwm_backlight_ops, &props);
251         if (IS_ERR(bl)) {
252                 dev_err(&pdev->dev, "failed to register backlight\n");
253                 ret = PTR_ERR(bl);
254                 goto err_alloc;
255         }
256
257         bl->props.brightness = data->dft_brightness;
258         backlight_update_status(bl);
259
260         platform_set_drvdata(pdev, bl);
261         return 0;
262
263 err_alloc:
264         if (data->exit)
265                 data->exit(&pdev->dev);
266         return ret;
267 }
268
269 static int pwm_backlight_remove(struct platform_device *pdev)
270 {
271         struct backlight_device *bl = platform_get_drvdata(pdev);
272         struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
273
274         backlight_device_unregister(bl);
275         pwm_config(pb->pwm, 0, pb->period);
276         pwm_disable(pb->pwm);
277         if (pb->exit)
278                 pb->exit(&pdev->dev);
279         return 0;
280 }
281
282 #ifdef CONFIG_PM
283 static int pwm_backlight_suspend(struct device *dev)
284 {
285         struct backlight_device *bl = dev_get_drvdata(dev);
286         struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
287
288         if (pb->notify)
289                 pb->notify(pb->dev, 0);
290         pwm_config(pb->pwm, 0, pb->period);
291         pwm_disable(pb->pwm);
292         if (pb->notify_after)
293                 pb->notify_after(pb->dev, 0);
294         return 0;
295 }
296
297 static int pwm_backlight_resume(struct device *dev)
298 {
299         struct backlight_device *bl = dev_get_drvdata(dev);
300
301         backlight_update_status(bl);
302         return 0;
303 }
304
305 static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
306                          pwm_backlight_resume);
307
308 #endif
309
310 static struct platform_driver pwm_backlight_driver = {
311         .driver         = {
312                 .name           = "pwm-backlight",
313                 .owner          = THIS_MODULE,
314 #ifdef CONFIG_PM
315                 .pm             = &pwm_backlight_pm_ops,
316 #endif
317                 .of_match_table = of_match_ptr(pwm_backlight_of_match),
318         },
319         .probe          = pwm_backlight_probe,
320         .remove         = pwm_backlight_remove,
321 };
322
323 module_platform_driver(pwm_backlight_driver);
324
325 MODULE_DESCRIPTION("PWM based Backlight Driver");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS("platform:pwm-backlight");
328