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;
31 unsigned int post_pwm_on_delay;
32 unsigned int pwm_off_delay;
33 int (*notify)(struct device *,
35 void (*notify_after)(struct device *,
37 int (*check_fb)(struct device *, struct fb_info *);
38 void (*exit)(struct device *);
41 static void pwm_backlight_power_on(struct pwm_bl_data *pb)
48 if (pb->power_supply) {
49 err = regulator_enable(pb->power_supply);
51 dev_err(pb->dev, "failed to enable power supply\n");
54 if (pb->post_pwm_on_delay)
55 msleep(pb->post_pwm_on_delay);
57 gpiod_set_value_cansleep(pb->enable_gpio, 1);
62 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
67 gpiod_set_value_cansleep(pb->enable_gpio, 0);
69 if (pb->pwm_off_delay)
70 msleep(pb->pwm_off_delay);
73 regulator_disable(pb->power_supply);
77 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness, struct pwm_state *state)
79 unsigned int lth = pb->lth_brightness;
83 duty_cycle = pb->levels[brightness];
85 duty_cycle = brightness;
87 duty_cycle *= state->period - lth;
88 do_div(duty_cycle, pb->scale);
90 return duty_cycle + lth;
93 static int pwm_backlight_update_status(struct backlight_device *bl)
95 struct pwm_bl_data *pb = bl_get_data(bl);
96 int brightness = backlight_get_brightness(bl);
97 struct pwm_state state;
100 brightness = pb->notify(pb->dev, brightness);
102 if (brightness > 0) {
103 pwm_get_state(pb->pwm, &state);
104 state.duty_cycle = compute_duty_cycle(pb, brightness, &state);
105 state.enabled = true;
106 pwm_apply_might_sleep(pb->pwm, &state);
108 pwm_backlight_power_on(pb);
110 pwm_backlight_power_off(pb);
112 pwm_get_state(pb->pwm, &state);
113 state.duty_cycle = 0;
115 * We cannot assume a disabled PWM to drive its output to the
116 * inactive state. If we have an enable GPIO and/or a regulator
117 * we assume that this isn't relevant and we can disable the PWM
118 * to save power. If however there is neither an enable GPIO nor
119 * a regulator keep the PWM on be sure to get a constant
122 state.enabled = !pb->power_supply && !pb->enable_gpio;
123 pwm_apply_might_sleep(pb->pwm, &state);
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 data->levels = devm_kcalloc(dev, num_levels,
267 sizeof(*data->levels), GFP_KERNEL);
271 ret = of_property_read_u32_array(node, "brightness-levels",
277 ret = of_property_read_u32(node, "default-brightness-level",
282 data->dft_brightness = value;
285 * This property is optional, if is set enables linear
286 * interpolation between each of the values of brightness levels
287 * and creates a new pre-computed table.
289 of_property_read_u32(node, "num-interpolated-steps",
293 * Make sure that there is at least two entries in the
294 * brightness-levels table, otherwise we can't interpolate
295 * between two points.
298 unsigned int num_input_levels = num_levels;
304 if (num_input_levels < 2) {
305 dev_err(dev, "can't interpolate\n");
310 * Recalculate the number of brightness levels, now
311 * taking in consideration the number of interpolated
312 * steps between two levels.
314 num_levels = (num_input_levels - 1) * num_steps + 1;
315 dev_dbg(dev, "new number of brightness levels: %d\n",
319 * Create a new table of brightness levels with all the
320 * interpolated steps.
322 table = devm_kcalloc(dev, num_levels, sizeof(*table),
327 * Fill the interpolated table[x] = y
328 * by draw lines between each (x1, y1) to (x2, y2).
331 for (i = 0; i < num_input_levels - 1; i++) {
334 y1 = data->levels[i];
335 y2 = data->levels[i + 1];
338 for (x = x1; x < x2; x++) {
340 div_s64(dy * (x - x1), dx);
343 /* Fill in the last point, since no line starts here. */
347 * As we use interpolation lets remove current
348 * brightness levels table and replace for the
349 * new interpolated table.
351 devm_kfree(dev, data->levels);
352 data->levels = table;
355 data->max_brightness = num_levels - 1;
361 static const struct of_device_id pwm_backlight_of_match[] = {
362 { .compatible = "pwm-backlight" },
366 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
368 static int pwm_backlight_parse_dt(struct device *dev,
369 struct platform_pwm_backlight_data *data)
375 int pwm_backlight_brightness_default(struct device *dev,
376 struct platform_pwm_backlight_data *data,
383 static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
385 unsigned int nlevels = data->max_brightness + 1;
386 unsigned int min_val = data->levels[0];
387 unsigned int max_val = data->levels[nlevels - 1];
389 * Multiplying by 128 means that even in pathological cases such
390 * as (max_val - min_val) == nlevels the error at max_val is less
393 unsigned int slope = (128 * (max_val - min_val)) / nlevels;
394 unsigned int margin = (max_val - min_val) / 20; /* 5% */
397 for (i = 1; i < nlevels; i++) {
398 unsigned int linear_value = min_val + ((i * slope) / 128);
399 unsigned int delta = abs(linear_value - data->levels[i]);
408 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
410 struct device_node *node = pb->dev->of_node;
414 * If the enable GPIO is present, observable (either as input
415 * or output) and off then the backlight is not currently active.
417 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
420 if (pb->power_supply && !regulator_is_enabled(pb->power_supply))
423 if (!pwm_is_enabled(pb->pwm))
427 * Synchronize the enable_gpio with the observed state of the
430 gpiod_direction_output(pb->enable_gpio, active);
433 * Do not change pb->enabled here! pb->enabled essentially
434 * tells us if we own one of the regulator's use counts and
435 * right now we do not.
438 /* Not booted with device tree or no phandle link to the node */
439 if (!node || !node->phandle)
440 return FB_BLANK_UNBLANK;
443 * If the driver is probed from the device tree and there is a
444 * phandle link pointing to the backlight node, it is safe to
445 * assume that another driver will enable the backlight at the
446 * appropriate time. Therefore, if it is disabled, keep it so.
448 return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN;
451 static int pwm_backlight_probe(struct platform_device *pdev)
453 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
454 struct platform_pwm_backlight_data defdata;
455 struct backlight_properties props;
456 struct backlight_device *bl;
457 struct pwm_bl_data *pb;
458 struct pwm_state state;
463 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
465 return dev_err_probe(&pdev->dev, ret,
466 "failed to find platform data\n");
472 ret = data->init(&pdev->dev);
477 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
483 pb->notify = data->notify;
484 pb->notify_after = data->notify_after;
485 pb->check_fb = data->check_fb;
486 pb->exit = data->exit;
487 pb->dev = &pdev->dev;
489 pb->post_pwm_on_delay = data->post_pwm_on_delay;
490 pb->pwm_off_delay = data->pwm_off_delay;
492 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
494 if (IS_ERR(pb->enable_gpio)) {
495 ret = dev_err_probe(&pdev->dev, PTR_ERR(pb->enable_gpio),
496 "failed to acquire enable GPIO\n");
500 pb->power_supply = devm_regulator_get_optional(&pdev->dev, "power");
501 if (IS_ERR(pb->power_supply)) {
502 ret = PTR_ERR(pb->power_supply);
503 if (ret == -ENODEV) {
504 pb->power_supply = NULL;
506 dev_err_probe(&pdev->dev, ret,
507 "failed to acquire power regulator\n");
512 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
513 if (IS_ERR(pb->pwm)) {
514 ret = dev_err_probe(&pdev->dev, PTR_ERR(pb->pwm),
515 "unable to request PWM\n");
519 dev_dbg(&pdev->dev, "got pwm for backlight\n");
521 /* Sync up PWM state. */
522 pwm_init_state(pb->pwm, &state);
525 * The DT case will set the pwm_period_ns field to 0 and store the
526 * period, parsed from the DT, in the PWM device. For the non-DT case,
527 * set the period from platform data if it has not already been set
528 * via the PWM lookup table.
530 if (!state.period && (data->pwm_period_ns > 0))
531 state.period = data->pwm_period_ns;
533 ret = pwm_apply_might_sleep(pb->pwm, &state);
535 dev_err_probe(&pdev->dev, ret,
536 "failed to apply initial PWM state");
540 memset(&props, 0, sizeof(struct backlight_properties));
543 pb->levels = data->levels;
546 * For the DT case, only when brightness levels is defined
547 * data->levels is filled. For the non-DT case, data->levels
548 * can come from platform data, however is not usual.
550 for (i = 0; i <= data->max_brightness; i++)
551 if (data->levels[i] > pb->scale)
552 pb->scale = data->levels[i];
554 if (pwm_backlight_is_linear(data))
555 props.scale = BACKLIGHT_SCALE_LINEAR;
557 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
558 } else if (!data->max_brightness) {
560 * If no brightness levels are provided and max_brightness is
561 * not set, use the default brightness table. For the DT case,
562 * max_brightness is set to 0 when brightness levels is not
563 * specified. For the non-DT case, max_brightness is usually
567 /* Get the PWM period (in nanoseconds) */
568 pwm_get_state(pb->pwm, &state);
570 ret = pwm_backlight_brightness_default(&pdev->dev, data,
573 dev_err_probe(&pdev->dev, ret,
574 "failed to setup default brightness table\n");
578 for (i = 0; i <= data->max_brightness; i++) {
579 if (data->levels[i] > pb->scale)
580 pb->scale = data->levels[i];
582 pb->levels = data->levels;
585 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
588 * That only happens for the non-DT case, where platform data
589 * sets the max_brightness value.
591 pb->scale = data->max_brightness;
594 pb->lth_brightness = data->lth_brightness * (div_u64(state.period,
597 props.type = BACKLIGHT_RAW;
598 props.max_brightness = data->max_brightness;
599 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
600 &pwm_backlight_ops, &props);
602 ret = dev_err_probe(&pdev->dev, PTR_ERR(bl),
603 "failed to register backlight\n");
607 if (data->dft_brightness > data->max_brightness) {
609 "invalid default brightness level: %u, using %u\n",
610 data->dft_brightness, data->max_brightness);
611 data->dft_brightness = data->max_brightness;
614 bl->props.brightness = data->dft_brightness;
615 bl->props.power = pwm_backlight_initial_power_state(pb);
616 backlight_update_status(bl);
618 platform_set_drvdata(pdev, bl);
623 data->exit(&pdev->dev);
627 static void pwm_backlight_remove(struct platform_device *pdev)
629 struct backlight_device *bl = platform_get_drvdata(pdev);
630 struct pwm_bl_data *pb = bl_get_data(bl);
631 struct pwm_state state;
633 backlight_device_unregister(bl);
634 pwm_backlight_power_off(pb);
635 pwm_get_state(pb->pwm, &state);
636 state.duty_cycle = 0;
637 state.enabled = false;
638 pwm_apply_might_sleep(pb->pwm, &state);
641 pb->exit(&pdev->dev);
644 static void pwm_backlight_shutdown(struct platform_device *pdev)
646 struct backlight_device *bl = platform_get_drvdata(pdev);
647 struct pwm_bl_data *pb = bl_get_data(bl);
648 struct pwm_state state;
650 pwm_backlight_power_off(pb);
651 pwm_get_state(pb->pwm, &state);
652 state.duty_cycle = 0;
653 state.enabled = false;
654 pwm_apply_might_sleep(pb->pwm, &state);
657 #ifdef CONFIG_PM_SLEEP
658 static int pwm_backlight_suspend(struct device *dev)
660 struct backlight_device *bl = dev_get_drvdata(dev);
661 struct pwm_bl_data *pb = bl_get_data(bl);
662 struct pwm_state state;
665 pb->notify(pb->dev, 0);
667 pwm_backlight_power_off(pb);
670 * Note that disabling the PWM doesn't guarantee that the output stays
671 * at its inactive state. However without the PWM disabled, the PWM
672 * driver refuses to suspend. So disable here even though this might
673 * enable the backlight on poorly designed boards.
675 pwm_get_state(pb->pwm, &state);
676 state.duty_cycle = 0;
677 state.enabled = false;
678 pwm_apply_might_sleep(pb->pwm, &state);
680 if (pb->notify_after)
681 pb->notify_after(pb->dev, 0);
686 static int pwm_backlight_resume(struct device *dev)
688 struct backlight_device *bl = dev_get_drvdata(dev);
690 backlight_update_status(bl);
696 static const struct dev_pm_ops pwm_backlight_pm_ops = {
697 #ifdef CONFIG_PM_SLEEP
698 .suspend = pwm_backlight_suspend,
699 .resume = pwm_backlight_resume,
700 .poweroff = pwm_backlight_suspend,
701 .restore = pwm_backlight_resume,
705 static struct platform_driver pwm_backlight_driver = {
707 .name = "pwm-backlight",
708 .pm = &pwm_backlight_pm_ops,
709 .of_match_table = of_match_ptr(pwm_backlight_of_match),
711 .probe = pwm_backlight_probe,
712 .remove_new = pwm_backlight_remove,
713 .shutdown = pwm_backlight_shutdown,
716 module_platform_driver(pwm_backlight_driver);
718 MODULE_DESCRIPTION("PWM based Backlight Driver");
719 MODULE_LICENSE("GPL v2");
720 MODULE_ALIAS("platform:pwm-backlight");