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