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/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.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>
23 struct pwm_device *pwm;
25 unsigned int lth_brightness;
28 struct regulator *power_supply;
29 struct gpio_desc *enable_gpio;
32 unsigned int post_pwm_on_delay;
33 unsigned int pwm_off_delay;
34 int (*notify)(struct device *,
36 void (*notify_after)(struct device *,
38 int (*check_fb)(struct device *, struct fb_info *);
39 void (*exit)(struct device *);
42 static void pwm_backlight_power_on(struct pwm_bl_data *pb)
44 struct pwm_state state;
47 pwm_get_state(pb->pwm, &state);
51 err = regulator_enable(pb->power_supply);
53 dev_err(pb->dev, "failed to enable power supply\n");
56 pwm_apply_state(pb->pwm, &state);
58 if (pb->post_pwm_on_delay)
59 msleep(pb->post_pwm_on_delay);
62 gpiod_set_value_cansleep(pb->enable_gpio, 1);
67 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
69 struct pwm_state state;
71 pwm_get_state(pb->pwm, &state);
76 gpiod_set_value_cansleep(pb->enable_gpio, 0);
78 if (pb->pwm_off_delay)
79 msleep(pb->pwm_off_delay);
81 state.enabled = false;
83 pwm_apply_state(pb->pwm, &state);
85 regulator_disable(pb->power_supply);
89 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
91 unsigned int lth = pb->lth_brightness;
92 struct pwm_state state;
95 pwm_get_state(pb->pwm, &state);
98 duty_cycle = pb->levels[brightness];
100 duty_cycle = brightness;
102 duty_cycle *= state.period - lth;
103 do_div(duty_cycle, pb->scale);
105 return duty_cycle + lth;
108 static int pwm_backlight_update_status(struct backlight_device *bl)
110 struct pwm_bl_data *pb = bl_get_data(bl);
111 int brightness = backlight_get_brightness(bl);
112 struct pwm_state state;
115 brightness = pb->notify(pb->dev, brightness);
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);
123 pwm_backlight_power_off(pb);
126 if (pb->notify_after)
127 pb->notify_after(pb->dev, brightness);
132 static int pwm_backlight_check_fb(struct backlight_device *bl,
133 struct fb_info *info)
135 struct pwm_bl_data *pb = bl_get_data(bl);
137 return !pb->check_fb || pb->check_fb(pb->dev, info);
140 static const struct backlight_ops pwm_backlight_ops = {
141 .update_status = pwm_backlight_update_status,
142 .check_fb = pwm_backlight_check_fb,
146 #define PWM_LUMINANCE_SHIFT 16
147 #define PWM_LUMINANCE_SCALE (1 << PWM_LUMINANCE_SHIFT) /* luminance scale */
150 * CIE lightness to PWM conversion.
152 * The CIE 1931 lightness formula is what actually describes how we perceive
154 * Y = (L* / 903.3) if L* ≤ 8
155 * Y = ((L* + 16) / 116)^3 if L* > 8
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.
161 * The following function does the fixed point maths needed to implement the
164 static u64 cie1931(unsigned int lightness)
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
176 if (lightness <= (8 * PWM_LUMINANCE_SCALE)) {
177 retval = DIV_ROUND_CLOSEST(lightness * 10, 9033);
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;
189 * Create a default correction table for PWM values to create linear brightness
190 * for LED based backlights using the CIE1931 algorithm.
193 int pwm_backlight_brightness_default(struct device *dev,
194 struct platform_pwm_backlight_data *data,
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.
205 data->max_brightness =
206 min((int)DIV_ROUND_UP(period, fls(period)), 4096);
208 data->levels = devm_kcalloc(dev, data->max_brightness,
209 sizeof(*data->levels), GFP_KERNEL);
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)
220 data->levels[i] = (unsigned int)retval;
223 data->dft_brightness = data->max_brightness / 2;
224 data->max_brightness--;
229 static int pwm_backlight_parse_dt(struct device *dev,
230 struct platform_pwm_backlight_data *data)
232 struct device_node *node = dev->of_node;
233 unsigned int num_levels;
234 unsigned int num_steps = 0;
235 struct property *prop;
244 memset(data, 0, sizeof(*data));
247 * These values are optional and set as 0 by default, the out values
248 * are modified only if a valid u32 value can be decoded.
250 of_property_read_u32(node, "post-pwm-on-delay-ms",
251 &data->post_pwm_on_delay);
252 of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay);
255 * Determine the number of brightness levels, if this property is not
256 * set a default table of brightness levels will be used.
258 prop = of_find_property(node, "brightness-levels", &length);
262 num_levels = length / sizeof(u32);
264 /* read brightness levels from DT property */
265 if (num_levels > 0) {
266 size_t size = sizeof(*data->levels) * num_levels;
268 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
272 ret = of_property_read_u32_array(node, "brightness-levels",
278 ret = of_property_read_u32(node, "default-brightness-level",
283 data->dft_brightness = value;
286 * This property is optional, if is set enables linear
287 * interpolation between each of the values of brightness levels
288 * and creates a new pre-computed table.
290 of_property_read_u32(node, "num-interpolated-steps",
294 * Make sure that there is at least two entries in the
295 * brightness-levels table, otherwise we can't interpolate
296 * between two points.
299 unsigned int num_input_levels = num_levels;
305 if (num_input_levels < 2) {
306 dev_err(dev, "can't interpolate\n");
311 * Recalculate the number of brightness levels, now
312 * taking in consideration the number of interpolated
313 * steps between two levels.
315 num_levels = (num_input_levels - 1) * num_steps + 1;
316 dev_dbg(dev, "new number of brightness levels: %d\n",
320 * Create a new table of brightness levels with all the
321 * interpolated steps.
323 size = sizeof(*table) * num_levels;
324 table = devm_kzalloc(dev, size, GFP_KERNEL);
328 * Fill the interpolated table[x] = y
329 * by draw lines between each (x1, y1) to (x2, y2).
332 for (i = 0; i < num_input_levels - 1; i++) {
335 y1 = data->levels[i];
336 y2 = data->levels[i + 1];
339 for (x = x1; x < x2; x++) {
341 div_s64(dy * (x - x1), dx);
344 /* Fill in the last point, since no line starts here. */
348 * As we use interpolation lets remove current
349 * brightness levels table and replace for the
350 * new interpolated table.
352 devm_kfree(dev, data->levels);
353 data->levels = table;
356 data->max_brightness = num_levels - 1;
362 static const struct of_device_id pwm_backlight_of_match[] = {
363 { .compatible = "pwm-backlight" },
367 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
369 static int pwm_backlight_parse_dt(struct device *dev,
370 struct platform_pwm_backlight_data *data)
376 int pwm_backlight_brightness_default(struct device *dev,
377 struct platform_pwm_backlight_data *data,
384 static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
386 unsigned int nlevels = data->max_brightness + 1;
387 unsigned int min_val = data->levels[0];
388 unsigned int max_val = data->levels[nlevels - 1];
390 * Multiplying by 128 means that even in pathological cases such
391 * as (max_val - min_val) == nlevels the error at max_val is less
394 unsigned int slope = (128 * (max_val - min_val)) / nlevels;
395 unsigned int margin = (max_val - min_val) / 20; /* 5% */
398 for (i = 1; i < nlevels; i++) {
399 unsigned int linear_value = min_val + ((i * slope) / 128);
400 unsigned int delta = abs(linear_value - data->levels[i]);
409 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
411 struct device_node *node = pb->dev->of_node;
413 /* Not booted with device tree or no phandle link to the node */
414 if (!node || !node->phandle)
415 return FB_BLANK_UNBLANK;
418 * If the driver is probed from the device tree and there is a
419 * phandle link pointing to the backlight node, it is safe to
420 * assume that another driver will enable the backlight at the
421 * appropriate time. Therefore, if it is disabled, keep it so.
424 /* if the enable GPIO is disabled, do not enable the backlight */
425 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
426 return FB_BLANK_POWERDOWN;
428 /* The regulator is disabled, do not enable the backlight */
429 if (!regulator_is_enabled(pb->power_supply))
430 return FB_BLANK_POWERDOWN;
432 /* The PWM is disabled, keep it like this */
433 if (!pwm_is_enabled(pb->pwm))
434 return FB_BLANK_POWERDOWN;
436 return FB_BLANK_UNBLANK;
439 static int pwm_backlight_probe(struct platform_device *pdev)
441 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
442 struct platform_pwm_backlight_data defdata;
443 struct backlight_properties props;
444 struct backlight_device *bl;
445 struct device_node *node = pdev->dev.of_node;
446 struct pwm_bl_data *pb;
447 struct pwm_state state;
452 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
454 dev_err(&pdev->dev, "failed to find platform data\n");
462 ret = data->init(&pdev->dev);
467 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
473 pb->notify = data->notify;
474 pb->notify_after = data->notify_after;
475 pb->check_fb = data->check_fb;
476 pb->exit = data->exit;
477 pb->dev = &pdev->dev;
479 pb->post_pwm_on_delay = data->post_pwm_on_delay;
480 pb->pwm_off_delay = data->pwm_off_delay;
482 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
484 if (IS_ERR(pb->enable_gpio)) {
485 ret = PTR_ERR(pb->enable_gpio);
490 * If the GPIO is not known to be already configured as output, that
491 * is, if gpiod_get_direction returns either 1 or -EINVAL, change the
492 * direction to output and set the GPIO as active.
493 * Do not force the GPIO to active when it was already output as it
494 * could cause backlight flickering or we would enable the backlight too
495 * early. Leave the decision of the initial backlight state for later.
497 if (pb->enable_gpio &&
498 gpiod_get_direction(pb->enable_gpio) != 0)
499 gpiod_direction_output(pb->enable_gpio, 1);
501 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
502 if (IS_ERR(pb->power_supply)) {
503 ret = PTR_ERR(pb->power_supply);
507 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
508 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
509 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
511 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
514 if (IS_ERR(pb->pwm)) {
515 ret = PTR_ERR(pb->pwm);
516 if (ret != -EPROBE_DEFER)
517 dev_err(&pdev->dev, "unable to request PWM\n");
521 dev_dbg(&pdev->dev, "got pwm for backlight\n");
523 /* Sync up PWM state. */
524 pwm_init_state(pb->pwm, &state);
527 * The DT case will set the pwm_period_ns field to 0 and store the
528 * period, parsed from the DT, in the PWM device. For the non-DT case,
529 * set the period from platform data if it has not already been set
530 * via the PWM lookup table.
532 if (!state.period && (data->pwm_period_ns > 0))
533 state.period = data->pwm_period_ns;
535 ret = pwm_apply_state(pb->pwm, &state);
537 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
542 memset(&props, 0, sizeof(struct backlight_properties));
545 pb->levels = data->levels;
548 * For the DT case, only when brightness levels is defined
549 * data->levels is filled. For the non-DT case, data->levels
550 * can come from platform data, however is not usual.
552 for (i = 0; i <= data->max_brightness; i++)
553 if (data->levels[i] > pb->scale)
554 pb->scale = data->levels[i];
556 if (pwm_backlight_is_linear(data))
557 props.scale = BACKLIGHT_SCALE_LINEAR;
559 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
560 } else if (!data->max_brightness) {
562 * If no brightness levels are provided and max_brightness is
563 * not set, use the default brightness table. For the DT case,
564 * max_brightness is set to 0 when brightness levels is not
565 * specified. For the non-DT case, max_brightness is usually
569 /* Get the PWM period (in nanoseconds) */
570 pwm_get_state(pb->pwm, &state);
572 ret = pwm_backlight_brightness_default(&pdev->dev, data,
576 "failed to setup default brightness table\n");
580 for (i = 0; i <= data->max_brightness; i++) {
581 if (data->levels[i] > pb->scale)
582 pb->scale = data->levels[i];
584 pb->levels = data->levels;
587 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
590 * That only happens for the non-DT case, where platform data
591 * sets the max_brightness value.
593 pb->scale = data->max_brightness;
596 pb->lth_brightness = data->lth_brightness * (div_u64(state.period,
599 props.type = BACKLIGHT_RAW;
600 props.max_brightness = data->max_brightness;
601 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
602 &pwm_backlight_ops, &props);
604 dev_err(&pdev->dev, "failed to register backlight\n");
611 if (data->dft_brightness > data->max_brightness) {
613 "invalid default brightness level: %u, using %u\n",
614 data->dft_brightness, data->max_brightness);
615 data->dft_brightness = data->max_brightness;
618 bl->props.brightness = data->dft_brightness;
619 bl->props.power = pwm_backlight_initial_power_state(pb);
620 backlight_update_status(bl);
622 platform_set_drvdata(pdev, bl);
627 data->exit(&pdev->dev);
631 static int pwm_backlight_remove(struct platform_device *pdev)
633 struct backlight_device *bl = platform_get_drvdata(pdev);
634 struct pwm_bl_data *pb = bl_get_data(bl);
636 backlight_device_unregister(bl);
637 pwm_backlight_power_off(pb);
640 pb->exit(&pdev->dev);
647 static void pwm_backlight_shutdown(struct platform_device *pdev)
649 struct backlight_device *bl = platform_get_drvdata(pdev);
650 struct pwm_bl_data *pb = bl_get_data(bl);
652 pwm_backlight_power_off(pb);
655 #ifdef CONFIG_PM_SLEEP
656 static int pwm_backlight_suspend(struct device *dev)
658 struct backlight_device *bl = dev_get_drvdata(dev);
659 struct pwm_bl_data *pb = bl_get_data(bl);
662 pb->notify(pb->dev, 0);
664 pwm_backlight_power_off(pb);
666 if (pb->notify_after)
667 pb->notify_after(pb->dev, 0);
672 static int pwm_backlight_resume(struct device *dev)
674 struct backlight_device *bl = dev_get_drvdata(dev);
676 backlight_update_status(bl);
682 static const struct dev_pm_ops pwm_backlight_pm_ops = {
683 #ifdef CONFIG_PM_SLEEP
684 .suspend = pwm_backlight_suspend,
685 .resume = pwm_backlight_resume,
686 .poweroff = pwm_backlight_suspend,
687 .restore = pwm_backlight_resume,
691 static struct platform_driver pwm_backlight_driver = {
693 .name = "pwm-backlight",
694 .pm = &pwm_backlight_pm_ops,
695 .of_match_table = of_match_ptr(pwm_backlight_of_match),
697 .probe = pwm_backlight_probe,
698 .remove = pwm_backlight_remove,
699 .shutdown = pwm_backlight_shutdown,
702 module_platform_driver(pwm_backlight_driver);
704 MODULE_DESCRIPTION("PWM based Backlight Driver");
705 MODULE_LICENSE("GPL v2");
706 MODULE_ALIAS("platform:pwm-backlight");