]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/video/backlight/pwm_bl.c
backlight: pwm_bl: add configurable delay between re-enabling PWM and switching backl...
[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/gpio/consumer.h>
14 #include <linux/gpio.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/fb.h>
20 #include <linux/backlight.h>
21 #include <linux/err.h>
22 #include <linux/pwm.h>
23 #include <linux/pwm_backlight.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27
28 struct pwm_bl_data {
29         struct pwm_device       *pwm;
30         struct device           *dev;
31         unsigned int            period;
32         unsigned int            lth_brightness;
33         unsigned int            *levels;
34         bool                    enabled;
35         struct regulator        *power_supply;
36         struct gpio_desc        *enable_gpio;
37         unsigned int            scale;
38         bool                    legacy;
39         int                     turn_on_delay_ms;
40         int                     (*notify)(struct device *,
41                                           int brightness);
42         void                    (*notify_after)(struct device *,
43                                         int brightness);
44         int                     (*check_fb)(struct device *, struct fb_info *);
45         void                    (*exit)(struct device *);
46 };
47
48 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
49 {
50         int err;
51
52         if (pb->enabled)
53                 return;
54
55         pwm_enable(pb->pwm);
56
57         if (pb->turn_on_delay_ms > 0) {
58                 dev_dbg(pb->dev, "Sleeping %u..%uµs\n",
59                         pb->turn_on_delay_ms * 1000,
60                         pb->turn_on_delay_ms * 1100);
61                 if (pb->turn_on_delay_ms > 20)
62                         msleep(pb->turn_on_delay_ms);
63                 else
64                         usleep_range(pb->turn_on_delay_ms * 1000,
65                              pb->turn_on_delay_ms * 1100);
66         }
67
68         err = regulator_enable(pb->power_supply);
69         if (err < 0)
70                 dev_err(pb->dev, "failed to enable power supply\n");
71
72         if (pb->enable_gpio)
73                 gpiod_set_value_cansleep(pb->enable_gpio, 1);
74
75         pb->enabled = true;
76 }
77
78 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
79 {
80         if (!pb->enabled)
81                 return;
82
83         pwm_config(pb->pwm, 0, pb->period);
84         pwm_disable(pb->pwm);
85
86         if (pb->enable_gpio)
87                 gpiod_set_value_cansleep(pb->enable_gpio, 0);
88
89         regulator_disable(pb->power_supply);
90         pb->enabled = false;
91 }
92
93 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
94 {
95         unsigned int lth = pb->lth_brightness;
96         int duty_cycle;
97
98         if (pb->levels)
99                 duty_cycle = pb->levels[brightness];
100         else
101                 duty_cycle = brightness;
102
103         return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
104 }
105
106 static int pwm_backlight_update_status(struct backlight_device *bl)
107 {
108         struct pwm_bl_data *pb = bl_get_data(bl);
109         int brightness = bl->props.brightness;
110         int duty_cycle;
111
112         if (bl->props.power != FB_BLANK_UNBLANK ||
113             bl->props.fb_blank != FB_BLANK_UNBLANK ||
114             bl->props.state & BL_CORE_FBBLANK)
115                 brightness = 0;
116
117         if (pb->notify)
118                 brightness = pb->notify(pb->dev, brightness);
119
120         if (brightness > 0) {
121                 duty_cycle = compute_duty_cycle(pb, brightness);
122                 pwm_config(pb->pwm, duty_cycle, pb->period);
123                 pwm_backlight_power_on(pb, brightness);
124         } else {
125                 pwm_backlight_power_off(pb);
126         }
127
128         if (pb->notify_after)
129                 pb->notify_after(pb->dev, brightness);
130
131         return 0;
132 }
133
134 static int pwm_backlight_check_fb(struct backlight_device *bl,
135                                   struct fb_info *info)
136 {
137         struct pwm_bl_data *pb = bl_get_data(bl);
138
139         return !pb->check_fb || pb->check_fb(pb->dev, info);
140 }
141
142 static const struct backlight_ops pwm_backlight_ops = {
143         .update_status  = pwm_backlight_update_status,
144         .check_fb       = pwm_backlight_check_fb,
145 };
146
147 #ifdef CONFIG_OF
148 static int pwm_backlight_parse_dt(struct device *dev,
149                                   struct platform_pwm_backlight_data *data)
150 {
151         struct device_node *node = dev->of_node;
152         struct property *prop;
153         int length;
154         u32 value;
155         int ret;
156
157         if (!node)
158                 return -ENODEV;
159
160         memset(data, 0, sizeof(*data));
161
162         /* determine the number of brightness levels */
163         prop = of_find_property(node, "brightness-levels", &length);
164         if (!prop)
165                 return -EINVAL;
166
167         data->max_brightness = length / sizeof(u32);
168
169         /* read brightness levels from DT property */
170         if (data->max_brightness > 0) {
171                 size_t size = sizeof(*data->levels) * data->max_brightness;
172
173                 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
174                 if (!data->levels)
175                         return -ENOMEM;
176
177                 ret = of_property_read_u32_array(node, "brightness-levels",
178                                                  data->levels,
179                                                  data->max_brightness);
180                 if (ret < 0)
181                         return ret;
182
183                 ret = of_property_read_u32(node, "default-brightness-level",
184                                            &value);
185                 if (ret < 0)
186                         return ret;
187
188                 data->dft_brightness = value;
189                 data->max_brightness--;
190         }
191
192         data->enable_gpio = -EINVAL;
193         return 0;
194 }
195
196 static struct of_device_id pwm_backlight_of_match[] = {
197         { .compatible = "pwm-backlight" },
198         { }
199 };
200
201 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
202 #else
203 static int pwm_backlight_parse_dt(struct device *dev,
204                                   struct platform_pwm_backlight_data *data)
205 {
206         return -ENODEV;
207 }
208 #endif
209
210 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
211 {
212         struct device_node *node = pb->dev->of_node;
213
214         /* Not booted with device tree or no phandle link to the node */
215         if (!node || !node->phandle)
216                 return FB_BLANK_UNBLANK;
217
218         /*
219          * If the driver is probed from the device tree and there is a
220          * phandle link pointing to the backlight node, it is safe to
221          * assume that another driver will enable the backlight at the
222          * appropriate time. Therefore, if it is disabled, keep it so.
223          */
224
225         /* if the enable GPIO is disabled, do not enable the backlight */
226         if (pb->enable_gpio && gpiod_get_value(pb->enable_gpio) == 0)
227                 return FB_BLANK_POWERDOWN;
228
229         /* The regulator is disabled, do not enable the backlight */
230         if (!regulator_is_enabled(pb->power_supply))
231                 return FB_BLANK_POWERDOWN;
232
233         /* The PWM is disabled, keep it like this */
234         if (!pwm_is_enabled(pb->pwm))
235                 return FB_BLANK_POWERDOWN;
236
237         return FB_BLANK_UNBLANK;
238 }
239
240 static int pwm_backlight_probe(struct platform_device *pdev)
241 {
242         struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
243         struct platform_pwm_backlight_data defdata;
244         struct backlight_properties props;
245         struct backlight_device *bl;
246         struct device_node *node = pdev->dev.of_node;
247         struct pwm_bl_data *pb;
248         struct pwm_args pargs;
249         int ret;
250
251         if (!data) {
252                 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
253                 if (ret < 0) {
254                         dev_err(&pdev->dev, "failed to find platform data\n");
255                         return ret;
256                 }
257
258                 data = &defdata;
259         }
260
261         if (data->init) {
262                 ret = data->init(&pdev->dev);
263                 if (ret < 0)
264                         return ret;
265         }
266
267         pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
268         if (!pb) {
269                 ret = -ENOMEM;
270                 goto err_alloc;
271         }
272
273         if (data->levels) {
274                 unsigned int i;
275
276                 for (i = 0; i <= data->max_brightness; i++)
277                         if (data->levels[i] > pb->scale)
278                                 pb->scale = data->levels[i];
279
280                 pb->levels = data->levels;
281         } else {
282                 pb->scale = data->max_brightness;
283         }
284         pb->notify = data->notify;
285         pb->notify_after = data->notify_after;
286         pb->check_fb = data->check_fb;
287         pb->exit = data->exit;
288         pb->dev = &pdev->dev;
289         pb->enabled = false;
290
291         pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
292                                                   GPIOD_ASIS);
293         if (IS_ERR(pb->enable_gpio)) {
294                 ret = PTR_ERR(pb->enable_gpio);
295                 goto err_alloc;
296         }
297
298         /*
299          * Compatibility fallback for drivers still using the integer GPIO
300          * platform data. Must go away soon.
301          */
302         if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
303                 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
304                                             GPIOF_OUT_INIT_HIGH, "enable");
305                 if (ret < 0) {
306                         dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
307                                 data->enable_gpio, ret);
308                         goto err_alloc;
309                 }
310
311                 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
312         }
313
314         /*
315          * If the GPIO is not known to be already configured as output, that
316          * is, if gpiod_get_direction returns either GPIOF_DIR_IN or -EINVAL,
317          * change the direction to output and set the GPIO as active.
318          * Do not force the GPIO to active when it was already output as it
319          * could cause backlight flickering or we would enable the backlight too
320          * early. Leave the decision of the initial backlight state for later.
321          */
322         if (pb->enable_gpio &&
323             gpiod_get_direction(pb->enable_gpio) != GPIOF_DIR_OUT)
324                 gpiod_direction_output(pb->enable_gpio, 1);
325
326         pb->power_supply = devm_regulator_get(&pdev->dev, "power");
327         if (IS_ERR(pb->power_supply)) {
328                 ret = PTR_ERR(pb->power_supply);
329                 goto err_alloc;
330         }
331
332         pb->pwm = devm_pwm_get(&pdev->dev, NULL);
333         if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
334                 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
335                 pb->legacy = true;
336                 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
337         }
338
339         if (IS_ERR(pb->pwm)) {
340                 ret = PTR_ERR(pb->pwm);
341                 if (ret != -EPROBE_DEFER)
342                         dev_err(&pdev->dev, "unable to request PWM\n");
343                 goto err_alloc;
344         }
345
346         of_property_read_u32(node, "turn-on-delay-ms", &pb->turn_on_delay_ms);
347
348         dev_dbg(&pdev->dev, "got pwm for backlight\n");
349
350         /*
351          * FIXME: pwm_apply_args() should be removed when switching to
352          * the atomic PWM API.
353          */
354         pwm_apply_args(pb->pwm);
355
356         /*
357          * The DT case will set the pwm_period_ns field to 0 and store the
358          * period, parsed from the DT, in the PWM device. For the non-DT case,
359          * set the period from platform data if it has not already been set
360          * via the PWM lookup table.
361          */
362         pwm_get_args(pb->pwm, &pargs);
363         pb->period = pargs.period;
364         if (!pb->period && (data->pwm_period_ns > 0))
365                 pb->period = data->pwm_period_ns;
366
367         pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
368
369         memset(&props, 0, sizeof(struct backlight_properties));
370         props.type = BACKLIGHT_RAW;
371         props.max_brightness = data->max_brightness;
372         bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
373                                        &pwm_backlight_ops, &props);
374         if (IS_ERR(bl)) {
375                 dev_err(&pdev->dev, "failed to register backlight\n");
376                 ret = PTR_ERR(bl);
377                 if (pb->legacy)
378                         pwm_free(pb->pwm);
379                 goto err_alloc;
380         }
381
382         if (data->dft_brightness > data->max_brightness) {
383                 dev_warn(&pdev->dev,
384                          "invalid default brightness level: %u, using %u\n",
385                          data->dft_brightness, data->max_brightness);
386                 data->dft_brightness = data->max_brightness;
387         }
388
389         bl->props.brightness = data->dft_brightness;
390         bl->props.power = pwm_backlight_initial_power_state(pb);
391         backlight_update_status(bl);
392
393         platform_set_drvdata(pdev, bl);
394         return 0;
395
396 err_alloc:
397         if (data->exit)
398                 data->exit(&pdev->dev);
399         return ret;
400 }
401
402 static int pwm_backlight_remove(struct platform_device *pdev)
403 {
404         struct backlight_device *bl = platform_get_drvdata(pdev);
405         struct pwm_bl_data *pb = bl_get_data(bl);
406
407         backlight_device_unregister(bl);
408         pwm_backlight_power_off(pb);
409
410         if (pb->exit)
411                 pb->exit(&pdev->dev);
412         if (pb->legacy)
413                 pwm_free(pb->pwm);
414
415         return 0;
416 }
417
418 static void pwm_backlight_shutdown(struct platform_device *pdev)
419 {
420         struct backlight_device *bl = platform_get_drvdata(pdev);
421         struct pwm_bl_data *pb = bl_get_data(bl);
422
423         pwm_backlight_power_off(pb);
424 }
425
426 #ifdef CONFIG_PM_SLEEP
427 static int pwm_backlight_suspend(struct device *dev)
428 {
429         struct backlight_device *bl = dev_get_drvdata(dev);
430         struct pwm_bl_data *pb = bl_get_data(bl);
431
432         if (pb->notify)
433                 pb->notify(pb->dev, 0);
434
435         pwm_backlight_power_off(pb);
436
437         if (pb->notify_after)
438                 pb->notify_after(pb->dev, 0);
439
440         return 0;
441 }
442
443 static int pwm_backlight_resume(struct device *dev)
444 {
445         struct backlight_device *bl = dev_get_drvdata(dev);
446
447         backlight_update_status(bl);
448
449         return 0;
450 }
451 #endif
452
453 static const struct dev_pm_ops pwm_backlight_pm_ops = {
454 #ifdef CONFIG_PM_SLEEP
455         .suspend = pwm_backlight_suspend,
456         .resume = pwm_backlight_resume,
457         .poweroff = pwm_backlight_suspend,
458         .restore = pwm_backlight_resume,
459 #endif
460 };
461
462 static struct platform_driver pwm_backlight_driver = {
463         .driver         = {
464                 .name           = "pwm-backlight",
465                 .pm             = &pwm_backlight_pm_ops,
466                 .of_match_table = of_match_ptr(pwm_backlight_of_match),
467         },
468         .probe          = pwm_backlight_probe,
469         .remove         = pwm_backlight_remove,
470         .shutdown       = pwm_backlight_shutdown,
471 };
472
473 module_platform_driver(pwm_backlight_driver);
474
475 MODULE_DESCRIPTION("PWM based Backlight Driver");
476 MODULE_LICENSE("GPL");
477 MODULE_ALIAS("platform:pwm-backlight");