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