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