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