1 // SPDX-License-Identifier: GPL-2.0-only
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
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/gpio.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
16 #include <linux/backlight.h>
17 #include <linux/err.h>
18 #include <linux/pwm.h>
19 #include <linux/pwm_backlight.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
24 struct pwm_device *pwm;
26 unsigned int lth_brightness;
29 struct regulator *power_supply;
30 struct gpio_desc *enable_gpio;
33 unsigned int post_pwm_on_delay;
34 unsigned int pwm_off_delay;
35 int (*notify)(struct device *,
37 void (*notify_after)(struct device *,
39 int (*check_fb)(struct device *, struct fb_info *);
40 void (*exit)(struct device *);
43 static void pwm_backlight_power_on(struct pwm_bl_data *pb)
45 struct pwm_state state;
48 pwm_get_state(pb->pwm, &state);
52 err = regulator_enable(pb->power_supply);
54 dev_err(pb->dev, "failed to enable power supply\n");
57 pwm_apply_state(pb->pwm, &state);
59 if (pb->post_pwm_on_delay)
60 msleep(pb->post_pwm_on_delay);
63 gpiod_set_value_cansleep(pb->enable_gpio, 1);
68 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
70 struct pwm_state state;
72 pwm_get_state(pb->pwm, &state);
77 gpiod_set_value_cansleep(pb->enable_gpio, 0);
79 if (pb->pwm_off_delay)
80 msleep(pb->pwm_off_delay);
82 state.enabled = false;
84 pwm_apply_state(pb->pwm, &state);
86 regulator_disable(pb->power_supply);
90 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
92 unsigned int lth = pb->lth_brightness;
93 struct pwm_state state;
96 pwm_get_state(pb->pwm, &state);
99 duty_cycle = pb->levels[brightness];
101 duty_cycle = brightness;
103 duty_cycle *= state.period - lth;
104 do_div(duty_cycle, pb->scale);
106 return duty_cycle + lth;
109 static int pwm_backlight_update_status(struct backlight_device *bl)
111 struct pwm_bl_data *pb = bl_get_data(bl);
112 int brightness = bl->props.brightness;
113 struct pwm_state state;
115 if (bl->props.power != FB_BLANK_UNBLANK ||
116 bl->props.fb_blank != FB_BLANK_UNBLANK ||
117 bl->props.state & BL_CORE_FBBLANK)
121 brightness = pb->notify(pb->dev, brightness);
123 if (brightness > 0) {
124 pwm_get_state(pb->pwm, &state);
125 state.duty_cycle = compute_duty_cycle(pb, brightness);
126 pwm_apply_state(pb->pwm, &state);
127 pwm_backlight_power_on(pb);
129 pwm_backlight_power_off(pb);
132 if (pb->notify_after)
133 pb->notify_after(pb->dev, brightness);
138 static int pwm_backlight_check_fb(struct backlight_device *bl,
139 struct fb_info *info)
141 struct pwm_bl_data *pb = bl_get_data(bl);
143 return !pb->check_fb || pb->check_fb(pb->dev, info);
146 static const struct backlight_ops pwm_backlight_ops = {
147 .update_status = pwm_backlight_update_status,
148 .check_fb = pwm_backlight_check_fb,
152 #define PWM_LUMINANCE_SCALE 10000 /* luminance scale */
155 * CIE lightness to PWM conversion.
157 * The CIE 1931 lightness formula is what actually describes how we perceive
159 * Y = (L* / 903.3) if L* ≤ 8
160 * Y = ((L* + 16) / 116)^3 if L* > 8
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.
166 * The following function does the fixed point maths needed to implement the
169 static u64 cie1931(unsigned int lightness, unsigned int scale)
174 * @lightness is given as a number between 0 and 1, expressed
175 * as a fixed-point number in scale @scale. Convert to a
176 * percentage, still expressed as a fixed-point number, so the
177 * above formulas can be applied.
180 if (lightness <= (8 * scale)) {
181 retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9033);
183 retval = int_pow((lightness + (16 * scale)) / 116, 3);
184 retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale));
191 * Create a default correction table for PWM values to create linear brightness
192 * for LED based backlights using the CIE1931 algorithm.
195 int pwm_backlight_brightness_default(struct device *dev,
196 struct platform_pwm_backlight_data *data,
203 * Once we have 4096 levels there's little point going much higher...
204 * neither interactive sliders nor animation benefits from having
205 * more values in the table.
207 data->max_brightness =
208 min((int)DIV_ROUND_UP(period, fls(period)), 4096);
210 data->levels = devm_kcalloc(dev, data->max_brightness,
211 sizeof(*data->levels), GFP_KERNEL);
215 /* Fill the table using the cie1931 algorithm */
216 for (i = 0; i < data->max_brightness; i++) {
217 retval = cie1931((i * PWM_LUMINANCE_SCALE) /
218 data->max_brightness, PWM_LUMINANCE_SCALE) *
220 retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE);
221 if (retval > UINT_MAX)
223 data->levels[i] = (unsigned int)retval;
226 data->dft_brightness = data->max_brightness / 2;
227 data->max_brightness--;
232 static int pwm_backlight_parse_dt(struct device *dev,
233 struct platform_pwm_backlight_data *data)
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;
248 memset(data, 0, sizeof(*data));
251 * These values are optional and set as 0 by default, the out values
252 * are modified only if a valid u32 value can be decoded.
254 of_property_read_u32(node, "post-pwm-on-delay-ms",
255 &data->post_pwm_on_delay);
256 of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay);
258 data->enable_gpio = -EINVAL;
261 * Determine the number of brightness levels, if this property is not
262 * set a default table of brightness levels will be used.
264 prop = of_find_property(node, "brightness-levels", &length);
268 data->max_brightness = length / sizeof(u32);
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;
275 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
279 ret = of_property_read_u32_array(node, "brightness-levels",
281 data->max_brightness);
285 ret = of_property_read_u32(node, "default-brightness-level",
290 data->dft_brightness = value;
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.
297 of_property_read_u32(node, "num-interpolated-steps",
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.
306 if (data->max_brightness < 2) {
307 dev_err(dev, "can't interpolate\n");
312 * Recalculate the number of brightness levels, now
313 * taking in consideration the number of interpolated
314 * steps between two levels.
316 for (i = 0; i < data->max_brightness - 1; i++) {
317 if ((data->levels[i + 1] - data->levels[i]) /
319 num_levels += num_steps;
324 dev_dbg(dev, "new number of brightness levels: %d\n",
328 * Create a new table of brightness levels with all the
329 * interpolated steps.
331 size = sizeof(*table) * num_levels;
332 table = devm_kzalloc(dev, size, GFP_KERNEL);
336 /* Fill the interpolated table. */
338 for (i = 0; i < data->max_brightness - 1; i++) {
339 value = data->levels[i];
340 n = (data->levels[i + 1] - value) / num_steps;
342 for (j = 0; j < num_steps; j++) {
343 table[levels_count] = value;
348 table[levels_count] = data->levels[i];
352 table[levels_count] = data->levels[i];
355 * As we use interpolation lets remove current
356 * brightness levels table and replace for the
357 * new interpolated table.
359 devm_kfree(dev, data->levels);
360 data->levels = table;
363 * Reassign max_brightness value to the new total number
364 * of brightness levels.
366 data->max_brightness = num_levels;
369 data->max_brightness--;
375 static const struct of_device_id pwm_backlight_of_match[] = {
376 { .compatible = "pwm-backlight" },
380 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
382 static int pwm_backlight_parse_dt(struct device *dev,
383 struct platform_pwm_backlight_data *data)
389 int pwm_backlight_brightness_default(struct device *dev,
390 struct platform_pwm_backlight_data *data,
397 static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
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];
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
407 unsigned int slope = (128 * (max_val - min_val)) / nlevels;
408 unsigned int margin = (max_val - min_val) / 20; /* 5% */
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]);
422 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
424 struct device_node *node = pb->dev->of_node;
426 /* Not booted with device tree or no phandle link to the node */
427 if (!node || !node->phandle)
428 return FB_BLANK_UNBLANK;
431 * If the driver is probed from the device tree and there is a
432 * phandle link pointing to the backlight node, it is safe to
433 * assume that another driver will enable the backlight at the
434 * appropriate time. Therefore, if it is disabled, keep it so.
437 /* if the enable GPIO is disabled, do not enable the backlight */
438 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
439 return FB_BLANK_POWERDOWN;
441 /* The regulator is disabled, do not enable the backlight */
442 if (!regulator_is_enabled(pb->power_supply))
443 return FB_BLANK_POWERDOWN;
445 /* The PWM is disabled, keep it like this */
446 if (!pwm_is_enabled(pb->pwm))
447 return FB_BLANK_POWERDOWN;
449 return FB_BLANK_UNBLANK;
452 static int pwm_backlight_probe(struct platform_device *pdev)
454 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
455 struct platform_pwm_backlight_data defdata;
456 struct backlight_properties props;
457 struct backlight_device *bl;
458 struct device_node *node = pdev->dev.of_node;
459 struct pwm_bl_data *pb;
460 struct pwm_state state;
465 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
467 dev_err(&pdev->dev, "failed to find platform data\n");
475 ret = data->init(&pdev->dev);
480 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
486 pb->notify = data->notify;
487 pb->notify_after = data->notify_after;
488 pb->check_fb = data->check_fb;
489 pb->exit = data->exit;
490 pb->dev = &pdev->dev;
492 pb->post_pwm_on_delay = data->post_pwm_on_delay;
493 pb->pwm_off_delay = data->pwm_off_delay;
495 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
497 if (IS_ERR(pb->enable_gpio)) {
498 ret = PTR_ERR(pb->enable_gpio);
503 * Compatibility fallback for drivers still using the integer GPIO
504 * platform data. Must go away soon.
506 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
507 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
508 GPIOF_OUT_INIT_HIGH, "enable");
510 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
511 data->enable_gpio, ret);
515 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
519 * If the GPIO is not known to be already configured as output, that
520 * is, if gpiod_get_direction returns either 1 or -EINVAL, change the
521 * direction to output and set the GPIO as active.
522 * Do not force the GPIO to active when it was already output as it
523 * could cause backlight flickering or we would enable the backlight too
524 * early. Leave the decision of the initial backlight state for later.
526 if (pb->enable_gpio &&
527 gpiod_get_direction(pb->enable_gpio) != 0)
528 gpiod_direction_output(pb->enable_gpio, 1);
530 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
531 if (IS_ERR(pb->power_supply)) {
532 ret = PTR_ERR(pb->power_supply);
536 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
537 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
538 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
540 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
543 if (IS_ERR(pb->pwm)) {
544 ret = PTR_ERR(pb->pwm);
545 if (ret != -EPROBE_DEFER)
546 dev_err(&pdev->dev, "unable to request PWM\n");
550 dev_dbg(&pdev->dev, "got pwm for backlight\n");
552 /* Sync up PWM state. */
553 pwm_init_state(pb->pwm, &state);
556 * The DT case will set the pwm_period_ns field to 0 and store the
557 * period, parsed from the DT, in the PWM device. For the non-DT case,
558 * set the period from platform data if it has not already been set
559 * via the PWM lookup table.
561 if (!state.period && (data->pwm_period_ns > 0))
562 state.period = data->pwm_period_ns;
564 ret = pwm_apply_state(pb->pwm, &state);
566 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
571 memset(&props, 0, sizeof(struct backlight_properties));
574 pb->levels = data->levels;
577 * For the DT case, only when brightness levels is defined
578 * data->levels is filled. For the non-DT case, data->levels
579 * can come from platform data, however is not usual.
581 for (i = 0; i <= data->max_brightness; i++)
582 if (data->levels[i] > pb->scale)
583 pb->scale = data->levels[i];
585 if (pwm_backlight_is_linear(data))
586 props.scale = BACKLIGHT_SCALE_LINEAR;
588 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
589 } else if (!data->max_brightness) {
591 * If no brightness levels are provided and max_brightness is
592 * not set, use the default brightness table. For the DT case,
593 * max_brightness is set to 0 when brightness levels is not
594 * specified. For the non-DT case, max_brightness is usually
598 /* Get the PWM period (in nanoseconds) */
599 pwm_get_state(pb->pwm, &state);
601 ret = pwm_backlight_brightness_default(&pdev->dev, data,
605 "failed to setup default brightness table\n");
609 for (i = 0; i <= data->max_brightness; i++) {
610 if (data->levels[i] > pb->scale)
611 pb->scale = data->levels[i];
613 pb->levels = data->levels;
616 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
619 * That only happens for the non-DT case, where platform data
620 * sets the max_brightness value.
622 pb->scale = data->max_brightness;
625 pb->lth_brightness = data->lth_brightness * (state.period / pb->scale);
627 props.type = BACKLIGHT_RAW;
628 props.max_brightness = data->max_brightness;
629 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
630 &pwm_backlight_ops, &props);
632 dev_err(&pdev->dev, "failed to register backlight\n");
639 if (data->dft_brightness > data->max_brightness) {
641 "invalid default brightness level: %u, using %u\n",
642 data->dft_brightness, data->max_brightness);
643 data->dft_brightness = data->max_brightness;
646 bl->props.brightness = data->dft_brightness;
647 bl->props.power = pwm_backlight_initial_power_state(pb);
648 backlight_update_status(bl);
650 platform_set_drvdata(pdev, bl);
655 data->exit(&pdev->dev);
659 static int pwm_backlight_remove(struct platform_device *pdev)
661 struct backlight_device *bl = platform_get_drvdata(pdev);
662 struct pwm_bl_data *pb = bl_get_data(bl);
664 backlight_device_unregister(bl);
665 pwm_backlight_power_off(pb);
668 pb->exit(&pdev->dev);
675 static void pwm_backlight_shutdown(struct platform_device *pdev)
677 struct backlight_device *bl = platform_get_drvdata(pdev);
678 struct pwm_bl_data *pb = bl_get_data(bl);
680 pwm_backlight_power_off(pb);
683 #ifdef CONFIG_PM_SLEEP
684 static int pwm_backlight_suspend(struct device *dev)
686 struct backlight_device *bl = dev_get_drvdata(dev);
687 struct pwm_bl_data *pb = bl_get_data(bl);
690 pb->notify(pb->dev, 0);
692 pwm_backlight_power_off(pb);
694 if (pb->notify_after)
695 pb->notify_after(pb->dev, 0);
700 static int pwm_backlight_resume(struct device *dev)
702 struct backlight_device *bl = dev_get_drvdata(dev);
704 backlight_update_status(bl);
710 static const struct dev_pm_ops pwm_backlight_pm_ops = {
711 #ifdef CONFIG_PM_SLEEP
712 .suspend = pwm_backlight_suspend,
713 .resume = pwm_backlight_resume,
714 .poweroff = pwm_backlight_suspend,
715 .restore = pwm_backlight_resume,
719 static struct platform_driver pwm_backlight_driver = {
721 .name = "pwm-backlight",
722 .pm = &pwm_backlight_pm_ops,
723 .of_match_table = of_match_ptr(pwm_backlight_of_match),
725 .probe = pwm_backlight_probe,
726 .remove = pwm_backlight_remove,
727 .shutdown = pwm_backlight_shutdown,
730 module_platform_driver(pwm_backlight_driver);
732 MODULE_DESCRIPTION("PWM based Backlight Driver");
733 MODULE_LICENSE("GPL v2");
734 MODULE_ALIAS("platform:pwm-backlight");