]> Git Repo - linux.git/blob - drivers/video/backlight/pwm_bl.c
ARM: dts: imx7s: Enable SNVS power key according to board design
[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/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/gpio.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/platform_device.h>
20 #include <linux/fb.h>
21 #include <linux/backlight.h>
22 #include <linux/err.h>
23 #include <linux/pwm.h>
24 #include <linux/pwm_backlight.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27
28 struct pwm_bl_data {
29         struct pwm_device       *pwm;
30         struct device           *dev;
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         unsigned int            post_pwm_on_delay;
39         unsigned int            pwm_off_delay;
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)
49 {
50         struct pwm_state state;
51         int err;
52
53         pwm_get_state(pb->pwm, &state);
54         if (pb->enabled)
55                 return;
56
57         err = regulator_enable(pb->power_supply);
58         if (err < 0)
59                 dev_err(pb->dev, "failed to enable power supply\n");
60
61         state.enabled = true;
62         pwm_apply_state(pb->pwm, &state);
63
64         if (pb->post_pwm_on_delay)
65                 msleep(pb->post_pwm_on_delay);
66
67         if (pb->enable_gpio)
68                 gpiod_set_value_cansleep(pb->enable_gpio, 1);
69
70         pb->enabled = true;
71 }
72
73 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
74 {
75         struct pwm_state state;
76
77         pwm_get_state(pb->pwm, &state);
78         if (!pb->enabled)
79                 return;
80
81         if (pb->enable_gpio)
82                 gpiod_set_value_cansleep(pb->enable_gpio, 0);
83
84         if (pb->pwm_off_delay)
85                 msleep(pb->pwm_off_delay);
86
87         state.enabled = false;
88         state.duty_cycle = 0;
89         pwm_apply_state(pb->pwm, &state);
90
91         regulator_disable(pb->power_supply);
92         pb->enabled = false;
93 }
94
95 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
96 {
97         unsigned int lth = pb->lth_brightness;
98         struct pwm_state state;
99         u64 duty_cycle;
100
101         pwm_get_state(pb->pwm, &state);
102
103         if (pb->levels)
104                 duty_cycle = pb->levels[brightness];
105         else
106                 duty_cycle = brightness;
107
108         duty_cycle *= state.period - lth;
109         do_div(duty_cycle, pb->scale);
110
111         return duty_cycle + lth;
112 }
113
114 static int pwm_backlight_update_status(struct backlight_device *bl)
115 {
116         struct pwm_bl_data *pb = bl_get_data(bl);
117         int brightness = bl->props.brightness;
118         struct pwm_state state;
119
120         if (bl->props.power != FB_BLANK_UNBLANK ||
121             bl->props.fb_blank != FB_BLANK_UNBLANK ||
122             bl->props.state & BL_CORE_FBBLANK)
123                 brightness = 0;
124
125         if (pb->notify)
126                 brightness = pb->notify(pb->dev, brightness);
127
128         if (brightness > 0) {
129                 pwm_get_state(pb->pwm, &state);
130                 state.duty_cycle = compute_duty_cycle(pb, brightness);
131                 pwm_apply_state(pb->pwm, &state);
132                 pwm_backlight_power_on(pb);
133         } else
134                 pwm_backlight_power_off(pb);
135
136         if (pb->notify_after)
137                 pb->notify_after(pb->dev, brightness);
138
139         return 0;
140 }
141
142 static int pwm_backlight_check_fb(struct backlight_device *bl,
143                                   struct fb_info *info)
144 {
145         struct pwm_bl_data *pb = bl_get_data(bl);
146
147         return !pb->check_fb || pb->check_fb(pb->dev, info);
148 }
149
150 static const struct backlight_ops pwm_backlight_ops = {
151         .update_status  = pwm_backlight_update_status,
152         .check_fb       = pwm_backlight_check_fb,
153 };
154
155 #ifdef CONFIG_OF
156 #define PWM_LUMINANCE_SCALE     10000 /* luminance scale */
157
158 /*
159  * CIE lightness to PWM conversion.
160  *
161  * The CIE 1931 lightness formula is what actually describes how we perceive
162  * light:
163  *          Y = (L* / 902.3)           if L* ≤ 0.08856
164  *          Y = ((L* + 16) / 116)^3    if L* > 0.08856
165  *
166  * Where Y is the luminance, the amount of light coming out of the screen, and
167  * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human
168  * perceives the screen to be, and is a number between 0 and 100.
169  *
170  * The following function does the fixed point maths needed to implement the
171  * above formula.
172  */
173 static u64 cie1931(unsigned int lightness, unsigned int scale)
174 {
175         u64 retval;
176
177         lightness *= 100;
178         if (lightness <= (8 * scale)) {
179                 retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9023);
180         } else {
181                 retval = int_pow((lightness + (16 * scale)) / 116, 3);
182                 retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale));
183         }
184
185         return retval;
186 }
187
188 /*
189  * Create a default correction table for PWM values to create linear brightness
190  * for LED based backlights using the CIE1931 algorithm.
191  */
192 static
193 int pwm_backlight_brightness_default(struct device *dev,
194                                      struct platform_pwm_backlight_data *data,
195                                      unsigned int period)
196 {
197         unsigned int counter = 0;
198         unsigned int i, n;
199         u64 retval;
200
201         /*
202          * Count the number of bits needed to represent the period number. The
203          * number of bits is used to calculate the number of levels used for the
204          * brightness-levels table, the purpose of this calculation is have a
205          * pre-computed table with enough levels to get linear brightness
206          * perception. The period is divided by the number of bits so for a
207          * 8-bit PWM we have 255 / 8 = 32 brightness levels or for a 16-bit PWM
208          * we have 65535 / 16 = 4096 brightness levels.
209          *
210          * Note that this method is based on empirical testing on different
211          * devices with PWM of 8 and 16 bits of resolution.
212          */
213         n = period;
214         while (n) {
215                 counter += n % 2;
216                 n >>= 1;
217         }
218
219         data->max_brightness = DIV_ROUND_UP(period, counter);
220         data->levels = devm_kcalloc(dev, data->max_brightness,
221                                     sizeof(*data->levels), GFP_KERNEL);
222         if (!data->levels)
223                 return -ENOMEM;
224
225         /* Fill the table using the cie1931 algorithm */
226         for (i = 0; i < data->max_brightness; i++) {
227                 retval = cie1931((i * PWM_LUMINANCE_SCALE) /
228                                  data->max_brightness, PWM_LUMINANCE_SCALE) *
229                                  period;
230                 retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE);
231                 if (retval > UINT_MAX)
232                         return -EINVAL;
233                 data->levels[i] = (unsigned int)retval;
234         }
235
236         data->dft_brightness = data->max_brightness / 2;
237         data->max_brightness--;
238
239         return 0;
240 }
241
242 static int pwm_backlight_parse_dt(struct device *dev,
243                                   struct platform_pwm_backlight_data *data)
244 {
245         struct device_node *node = dev->of_node;
246         unsigned int num_levels = 0;
247         unsigned int levels_count;
248         unsigned int num_steps = 0;
249         struct property *prop;
250         unsigned int *table;
251         int length;
252         u32 value;
253         int ret;
254
255         if (!node)
256                 return -ENODEV;
257
258         memset(data, 0, sizeof(*data));
259
260         /*
261          * These values are optional and set as 0 by default, the out values
262          * are modified only if a valid u32 value can be decoded.
263          */
264         of_property_read_u32(node, "post-pwm-on-delay-ms",
265                              &data->post_pwm_on_delay);
266         of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay);
267
268         data->enable_gpio = -EINVAL;
269
270         /*
271          * Determine the number of brightness levels, if this property is not
272          * set a default table of brightness levels will be used.
273          */
274         prop = of_find_property(node, "brightness-levels", &length);
275         if (!prop)
276                 return 0;
277
278         data->max_brightness = length / sizeof(u32);
279
280         /* read brightness levels from DT property */
281         if (data->max_brightness > 0) {
282                 size_t size = sizeof(*data->levels) * data->max_brightness;
283                 unsigned int i, j, n = 0;
284
285                 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
286                 if (!data->levels)
287                         return -ENOMEM;
288
289                 ret = of_property_read_u32_array(node, "brightness-levels",
290                                                  data->levels,
291                                                  data->max_brightness);
292                 if (ret < 0)
293                         return ret;
294
295                 ret = of_property_read_u32(node, "default-brightness-level",
296                                            &value);
297                 if (ret < 0)
298                         return ret;
299
300                 data->dft_brightness = value;
301
302                 /*
303                  * This property is optional, if is set enables linear
304                  * interpolation between each of the values of brightness levels
305                  * and creates a new pre-computed table.
306                  */
307                 of_property_read_u32(node, "num-interpolated-steps",
308                                      &num_steps);
309
310                 /*
311                  * Make sure that there is at least two entries in the
312                  * brightness-levels table, otherwise we can't interpolate
313                  * between two points.
314                  */
315                 if (num_steps) {
316                         if (data->max_brightness < 2) {
317                                 dev_err(dev, "can't interpolate\n");
318                                 return -EINVAL;
319                         }
320
321                         /*
322                          * Recalculate the number of brightness levels, now
323                          * taking in consideration the number of interpolated
324                          * steps between two levels.
325                          */
326                         for (i = 0; i < data->max_brightness - 1; i++) {
327                                 if ((data->levels[i + 1] - data->levels[i]) /
328                                    num_steps)
329                                         num_levels += num_steps;
330                                 else
331                                         num_levels++;
332                         }
333                         num_levels++;
334                         dev_dbg(dev, "new number of brightness levels: %d\n",
335                                 num_levels);
336
337                         /*
338                          * Create a new table of brightness levels with all the
339                          * interpolated steps.
340                          */
341                         size = sizeof(*table) * num_levels;
342                         table = devm_kzalloc(dev, size, GFP_KERNEL);
343                         if (!table)
344                                 return -ENOMEM;
345
346                         /* Fill the interpolated table. */
347                         levels_count = 0;
348                         for (i = 0; i < data->max_brightness - 1; i++) {
349                                 value = data->levels[i];
350                                 n = (data->levels[i + 1] - value) / num_steps;
351                                 if (n > 0) {
352                                         for (j = 0; j < num_steps; j++) {
353                                                 table[levels_count] = value;
354                                                 value += n;
355                                                 levels_count++;
356                                         }
357                                 } else {
358                                         table[levels_count] = data->levels[i];
359                                         levels_count++;
360                                 }
361                         }
362                         table[levels_count] = data->levels[i];
363
364                         /*
365                          * As we use interpolation lets remove current
366                          * brightness levels table and replace for the
367                          * new interpolated table.
368                          */
369                         devm_kfree(dev, data->levels);
370                         data->levels = table;
371
372                         /*
373                          * Reassign max_brightness value to the new total number
374                          * of brightness levels.
375                          */
376                         data->max_brightness = num_levels;
377                 }
378
379                 data->max_brightness--;
380         }
381
382         return 0;
383 }
384
385 static const struct of_device_id pwm_backlight_of_match[] = {
386         { .compatible = "pwm-backlight" },
387         { }
388 };
389
390 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
391 #else
392 static int pwm_backlight_parse_dt(struct device *dev,
393                                   struct platform_pwm_backlight_data *data)
394 {
395         return -ENODEV;
396 }
397
398 static
399 int pwm_backlight_brightness_default(struct device *dev,
400                                      struct platform_pwm_backlight_data *data,
401                                      unsigned int period)
402 {
403         return -ENODEV;
404 }
405 #endif
406
407 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
408 {
409         struct device_node *node = pb->dev->of_node;
410
411         /* Not booted with device tree or no phandle link to the node */
412         if (!node || !node->phandle)
413                 return FB_BLANK_UNBLANK;
414
415         /*
416          * If the driver is probed from the device tree and there is a
417          * phandle link pointing to the backlight node, it is safe to
418          * assume that another driver will enable the backlight at the
419          * appropriate time. Therefore, if it is disabled, keep it so.
420          */
421
422         /* if the enable GPIO is disabled, do not enable the backlight */
423         if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
424                 return FB_BLANK_POWERDOWN;
425
426         /* The regulator is disabled, do not enable the backlight */
427         if (!regulator_is_enabled(pb->power_supply))
428                 return FB_BLANK_POWERDOWN;
429
430         /* The PWM is disabled, keep it like this */
431         if (!pwm_is_enabled(pb->pwm))
432                 return FB_BLANK_POWERDOWN;
433
434         return FB_BLANK_UNBLANK;
435 }
436
437 static int pwm_backlight_probe(struct platform_device *pdev)
438 {
439         struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
440         struct platform_pwm_backlight_data defdata;
441         struct backlight_properties props;
442         struct backlight_device *bl;
443         struct device_node *node = pdev->dev.of_node;
444         struct pwm_bl_data *pb;
445         struct pwm_state state;
446         unsigned int i;
447         int ret;
448
449         if (!data) {
450                 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
451                 if (ret < 0) {
452                         dev_err(&pdev->dev, "failed to find platform data\n");
453                         return ret;
454                 }
455
456                 data = &defdata;
457         }
458
459         if (data->init) {
460                 ret = data->init(&pdev->dev);
461                 if (ret < 0)
462                         return ret;
463         }
464
465         pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
466         if (!pb) {
467                 ret = -ENOMEM;
468                 goto err_alloc;
469         }
470
471         pb->notify = data->notify;
472         pb->notify_after = data->notify_after;
473         pb->check_fb = data->check_fb;
474         pb->exit = data->exit;
475         pb->dev = &pdev->dev;
476         pb->enabled = false;
477         pb->post_pwm_on_delay = data->post_pwm_on_delay;
478         pb->pwm_off_delay = data->pwm_off_delay;
479
480         pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
481                                                   GPIOD_ASIS);
482         if (IS_ERR(pb->enable_gpio)) {
483                 ret = PTR_ERR(pb->enable_gpio);
484                 goto err_alloc;
485         }
486
487         /*
488          * Compatibility fallback for drivers still using the integer GPIO
489          * platform data. Must go away soon.
490          */
491         if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
492                 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
493                                             GPIOF_OUT_INIT_HIGH, "enable");
494                 if (ret < 0) {
495                         dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
496                                 data->enable_gpio, ret);
497                         goto err_alloc;
498                 }
499
500                 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
501         }
502
503         /*
504          * If the GPIO is not known to be already configured as output, that
505          * is, if gpiod_get_direction returns either 1 or -EINVAL, change the
506          * direction to output and set the GPIO as active.
507          * Do not force the GPIO to active when it was already output as it
508          * could cause backlight flickering or we would enable the backlight too
509          * early. Leave the decision of the initial backlight state for later.
510          */
511         if (pb->enable_gpio &&
512             gpiod_get_direction(pb->enable_gpio) != 0)
513                 gpiod_direction_output(pb->enable_gpio, 1);
514
515         pb->power_supply = devm_regulator_get(&pdev->dev, "power");
516         if (IS_ERR(pb->power_supply)) {
517                 ret = PTR_ERR(pb->power_supply);
518                 goto err_alloc;
519         }
520
521         pb->pwm = devm_pwm_get(&pdev->dev, NULL);
522         if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
523                 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
524                 pb->legacy = true;
525                 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
526         }
527
528         if (IS_ERR(pb->pwm)) {
529                 ret = PTR_ERR(pb->pwm);
530                 if (ret != -EPROBE_DEFER)
531                         dev_err(&pdev->dev, "unable to request PWM\n");
532                 goto err_alloc;
533         }
534
535         dev_dbg(&pdev->dev, "got pwm for backlight\n");
536
537         /* Sync up PWM state. */
538         pwm_init_state(pb->pwm, &state);
539
540         /*
541          * The DT case will set the pwm_period_ns field to 0 and store the
542          * period, parsed from the DT, in the PWM device. For the non-DT case,
543          * set the period from platform data if it has not already been set
544          * via the PWM lookup table.
545          */
546         if (!state.period && (data->pwm_period_ns > 0))
547                 state.period = data->pwm_period_ns;
548
549         ret = pwm_apply_state(pb->pwm, &state);
550         if (ret) {
551                 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
552                         ret);
553                 goto err_alloc;
554         }
555
556         if (data->levels) {
557                 /*
558                  * For the DT case, only when brightness levels is defined
559                  * data->levels is filled. For the non-DT case, data->levels
560                  * can come from platform data, however is not usual.
561                  */
562                 for (i = 0; i <= data->max_brightness; i++) {
563                         if (data->levels[i] > pb->scale)
564                                 pb->scale = data->levels[i];
565
566                         pb->levels = data->levels;
567                 }
568         } else if (!data->max_brightness) {
569                 /*
570                  * If no brightness levels are provided and max_brightness is
571                  * not set, use the default brightness table. For the DT case,
572                  * max_brightness is set to 0 when brightness levels is not
573                  * specified. For the non-DT case, max_brightness is usually
574                  * set to some value.
575                  */
576
577                 /* Get the PWM period (in nanoseconds) */
578                 pwm_get_state(pb->pwm, &state);
579
580                 ret = pwm_backlight_brightness_default(&pdev->dev, data,
581                                                        state.period);
582                 if (ret < 0) {
583                         dev_err(&pdev->dev,
584                                 "failed to setup default brightness table\n");
585                         goto err_alloc;
586                 }
587
588                 for (i = 0; i <= data->max_brightness; i++) {
589                         if (data->levels[i] > pb->scale)
590                                 pb->scale = data->levels[i];
591
592                         pb->levels = data->levels;
593                 }
594         } else {
595                 /*
596                  * That only happens for the non-DT case, where platform data
597                  * sets the max_brightness value.
598                  */
599                 pb->scale = data->max_brightness;
600         }
601
602         pb->lth_brightness = data->lth_brightness * (state.period / pb->scale);
603
604         memset(&props, 0, sizeof(struct backlight_properties));
605         props.type = BACKLIGHT_RAW;
606         props.max_brightness = data->max_brightness;
607         bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
608                                        &pwm_backlight_ops, &props);
609         if (IS_ERR(bl)) {
610                 dev_err(&pdev->dev, "failed to register backlight\n");
611                 ret = PTR_ERR(bl);
612                 if (pb->legacy)
613                         pwm_free(pb->pwm);
614                 goto err_alloc;
615         }
616
617         if (data->dft_brightness > data->max_brightness) {
618                 dev_warn(&pdev->dev,
619                          "invalid default brightness level: %u, using %u\n",
620                          data->dft_brightness, data->max_brightness);
621                 data->dft_brightness = data->max_brightness;
622         }
623
624         bl->props.brightness = data->dft_brightness;
625         bl->props.power = pwm_backlight_initial_power_state(pb);
626         backlight_update_status(bl);
627
628         platform_set_drvdata(pdev, bl);
629         return 0;
630
631 err_alloc:
632         if (data->exit)
633                 data->exit(&pdev->dev);
634         return ret;
635 }
636
637 static int pwm_backlight_remove(struct platform_device *pdev)
638 {
639         struct backlight_device *bl = platform_get_drvdata(pdev);
640         struct pwm_bl_data *pb = bl_get_data(bl);
641
642         backlight_device_unregister(bl);
643         pwm_backlight_power_off(pb);
644
645         if (pb->exit)
646                 pb->exit(&pdev->dev);
647         if (pb->legacy)
648                 pwm_free(pb->pwm);
649
650         return 0;
651 }
652
653 static void pwm_backlight_shutdown(struct platform_device *pdev)
654 {
655         struct backlight_device *bl = platform_get_drvdata(pdev);
656         struct pwm_bl_data *pb = bl_get_data(bl);
657
658         pwm_backlight_power_off(pb);
659 }
660
661 #ifdef CONFIG_PM_SLEEP
662 static int pwm_backlight_suspend(struct device *dev)
663 {
664         struct backlight_device *bl = dev_get_drvdata(dev);
665         struct pwm_bl_data *pb = bl_get_data(bl);
666
667         if (pb->notify)
668                 pb->notify(pb->dev, 0);
669
670         pwm_backlight_power_off(pb);
671
672         if (pb->notify_after)
673                 pb->notify_after(pb->dev, 0);
674
675         return 0;
676 }
677
678 static int pwm_backlight_resume(struct device *dev)
679 {
680         struct backlight_device *bl = dev_get_drvdata(dev);
681
682         backlight_update_status(bl);
683
684         return 0;
685 }
686 #endif
687
688 static const struct dev_pm_ops pwm_backlight_pm_ops = {
689 #ifdef CONFIG_PM_SLEEP
690         .suspend = pwm_backlight_suspend,
691         .resume = pwm_backlight_resume,
692         .poweroff = pwm_backlight_suspend,
693         .restore = pwm_backlight_resume,
694 #endif
695 };
696
697 static struct platform_driver pwm_backlight_driver = {
698         .driver         = {
699                 .name           = "pwm-backlight",
700                 .pm             = &pwm_backlight_pm_ops,
701                 .of_match_table = of_match_ptr(pwm_backlight_of_match),
702         },
703         .probe          = pwm_backlight_probe,
704         .remove         = pwm_backlight_remove,
705         .shutdown       = pwm_backlight_shutdown,
706 };
707
708 module_platform_driver(pwm_backlight_driver);
709
710 MODULE_DESCRIPTION("PWM based Backlight Driver");
711 MODULE_LICENSE("GPL");
712 MODULE_ALIAS("platform:pwm-backlight");
This page took 0.076325 seconds and 4 git commands to generate.