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
= bl
->props
.brightness
;
112 struct pwm_state state
;
114 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
115 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
116 bl
->props
.state
& BL_CORE_FBBLANK
)
120 brightness
= pb
->notify(pb
->dev
, brightness
);
122 if (brightness
> 0) {
123 pwm_get_state(pb
->pwm
, &state
);
124 state
.duty_cycle
= compute_duty_cycle(pb
, brightness
);
125 pwm_apply_state(pb
->pwm
, &state
);
126 pwm_backlight_power_on(pb
);
128 pwm_backlight_power_off(pb
);
131 if (pb
->notify_after
)
132 pb
->notify_after(pb
->dev
, brightness
);
137 static int pwm_backlight_check_fb(struct backlight_device
*bl
,
138 struct fb_info
*info
)
140 struct pwm_bl_data
*pb
= bl_get_data(bl
);
142 return !pb
->check_fb
|| pb
->check_fb(pb
->dev
, info
);
145 static const struct backlight_ops pwm_backlight_ops
= {
146 .update_status
= pwm_backlight_update_status
,
147 .check_fb
= pwm_backlight_check_fb
,
151 #define PWM_LUMINANCE_SHIFT 16
152 #define PWM_LUMINANCE_SCALE (1 << PWM_LUMINANCE_SHIFT) /* 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
)
174 * @lightness is given as a number between 0 and 1, expressed
175 * as a fixed-point number in scale
176 * PWM_LUMINANCE_SCALE. Convert to a percentage, still
177 * expressed as a fixed-point number, so the above formulas
181 if (lightness
<= (8 * PWM_LUMINANCE_SCALE
)) {
182 retval
= DIV_ROUND_CLOSEST(lightness
* 10, 9033);
184 retval
= (lightness
+ (16 * PWM_LUMINANCE_SCALE
)) / 116;
185 retval
*= retval
* retval
;
186 retval
+= 1ULL << (2*PWM_LUMINANCE_SHIFT
- 1);
187 retval
>>= 2*PWM_LUMINANCE_SHIFT
;
194 * Create a default correction table for PWM values to create linear brightness
195 * for LED based backlights using the CIE1931 algorithm.
198 int pwm_backlight_brightness_default(struct device
*dev
,
199 struct platform_pwm_backlight_data
*data
,
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.
210 data
->max_brightness
=
211 min((int)DIV_ROUND_UP(period
, fls(period
)), 4096);
213 data
->levels
= devm_kcalloc(dev
, data
->max_brightness
,
214 sizeof(*data
->levels
), GFP_KERNEL
);
218 /* Fill the table using the cie1931 algorithm */
219 for (i
= 0; i
< data
->max_brightness
; i
++) {
220 retval
= cie1931((i
* PWM_LUMINANCE_SCALE
) /
221 data
->max_brightness
) * period
;
222 retval
= DIV_ROUND_CLOSEST_ULL(retval
, PWM_LUMINANCE_SCALE
);
223 if (retval
> UINT_MAX
)
225 data
->levels
[i
] = (unsigned int)retval
;
228 data
->dft_brightness
= data
->max_brightness
/ 2;
229 data
->max_brightness
--;
234 static int pwm_backlight_parse_dt(struct device
*dev
,
235 struct platform_pwm_backlight_data
*data
)
237 struct device_node
*node
= dev
->of_node
;
238 unsigned int num_levels
= 0;
239 unsigned int levels_count
;
240 unsigned int num_steps
= 0;
241 struct property
*prop
;
250 memset(data
, 0, sizeof(*data
));
253 * These values are optional and set as 0 by default, the out values
254 * are modified only if a valid u32 value can be decoded.
256 of_property_read_u32(node
, "post-pwm-on-delay-ms",
257 &data
->post_pwm_on_delay
);
258 of_property_read_u32(node
, "pwm-off-delay-ms", &data
->pwm_off_delay
);
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 * If the GPIO is not known to be already configured as output, that
504 * is, if gpiod_get_direction returns either 1 or -EINVAL, change the
505 * direction to output and set the GPIO as active.
506 * Do not force the GPIO to active when it was already output as it
507 * could cause backlight flickering or we would enable the backlight too
508 * early. Leave the decision of the initial backlight state for later.
510 if (pb
->enable_gpio
&&
511 gpiod_get_direction(pb
->enable_gpio
) != 0)
512 gpiod_direction_output(pb
->enable_gpio
, 1);
514 pb
->power_supply
= devm_regulator_get(&pdev
->dev
, "power");
515 if (IS_ERR(pb
->power_supply
)) {
516 ret
= PTR_ERR(pb
->power_supply
);
520 pb
->pwm
= devm_pwm_get(&pdev
->dev
, NULL
);
521 if (IS_ERR(pb
->pwm
) && PTR_ERR(pb
->pwm
) != -EPROBE_DEFER
&& !node
) {
522 dev_err(&pdev
->dev
, "unable to request PWM, trying legacy API\n");
524 pb
->pwm
= pwm_request(data
->pwm_id
, "pwm-backlight");
527 if (IS_ERR(pb
->pwm
)) {
528 ret
= PTR_ERR(pb
->pwm
);
529 if (ret
!= -EPROBE_DEFER
)
530 dev_err(&pdev
->dev
, "unable to request PWM\n");
534 dev_dbg(&pdev
->dev
, "got pwm for backlight\n");
536 /* Sync up PWM state. */
537 pwm_init_state(pb
->pwm
, &state
);
540 * The DT case will set the pwm_period_ns field to 0 and store the
541 * period, parsed from the DT, in the PWM device. For the non-DT case,
542 * set the period from platform data if it has not already been set
543 * via the PWM lookup table.
545 if (!state
.period
&& (data
->pwm_period_ns
> 0))
546 state
.period
= data
->pwm_period_ns
;
548 ret
= pwm_apply_state(pb
->pwm
, &state
);
550 dev_err(&pdev
->dev
, "failed to apply initial PWM state: %d\n",
555 memset(&props
, 0, sizeof(struct backlight_properties
));
558 pb
->levels
= data
->levels
;
561 * For the DT case, only when brightness levels is defined
562 * data->levels is filled. For the non-DT case, data->levels
563 * can come from platform data, however is not usual.
565 for (i
= 0; i
<= data
->max_brightness
; i
++)
566 if (data
->levels
[i
] > pb
->scale
)
567 pb
->scale
= data
->levels
[i
];
569 if (pwm_backlight_is_linear(data
))
570 props
.scale
= BACKLIGHT_SCALE_LINEAR
;
572 props
.scale
= BACKLIGHT_SCALE_NON_LINEAR
;
573 } else if (!data
->max_brightness
) {
575 * If no brightness levels are provided and max_brightness is
576 * not set, use the default brightness table. For the DT case,
577 * max_brightness is set to 0 when brightness levels is not
578 * specified. For the non-DT case, max_brightness is usually
582 /* Get the PWM period (in nanoseconds) */
583 pwm_get_state(pb
->pwm
, &state
);
585 ret
= pwm_backlight_brightness_default(&pdev
->dev
, data
,
589 "failed to setup default brightness table\n");
593 for (i
= 0; i
<= data
->max_brightness
; i
++) {
594 if (data
->levels
[i
] > pb
->scale
)
595 pb
->scale
= data
->levels
[i
];
597 pb
->levels
= data
->levels
;
600 props
.scale
= BACKLIGHT_SCALE_NON_LINEAR
;
603 * That only happens for the non-DT case, where platform data
604 * sets the max_brightness value.
606 pb
->scale
= data
->max_brightness
;
609 pb
->lth_brightness
= data
->lth_brightness
* (state
.period
/ pb
->scale
);
611 props
.type
= BACKLIGHT_RAW
;
612 props
.max_brightness
= data
->max_brightness
;
613 bl
= backlight_device_register(dev_name(&pdev
->dev
), &pdev
->dev
, pb
,
614 &pwm_backlight_ops
, &props
);
616 dev_err(&pdev
->dev
, "failed to register backlight\n");
623 if (data
->dft_brightness
> data
->max_brightness
) {
625 "invalid default brightness level: %u, using %u\n",
626 data
->dft_brightness
, data
->max_brightness
);
627 data
->dft_brightness
= data
->max_brightness
;
630 bl
->props
.brightness
= data
->dft_brightness
;
631 bl
->props
.power
= pwm_backlight_initial_power_state(pb
);
632 backlight_update_status(bl
);
634 platform_set_drvdata(pdev
, bl
);
639 data
->exit(&pdev
->dev
);
643 static int pwm_backlight_remove(struct platform_device
*pdev
)
645 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
646 struct pwm_bl_data
*pb
= bl_get_data(bl
);
648 backlight_device_unregister(bl
);
649 pwm_backlight_power_off(pb
);
652 pb
->exit(&pdev
->dev
);
659 static void pwm_backlight_shutdown(struct platform_device
*pdev
)
661 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
662 struct pwm_bl_data
*pb
= bl_get_data(bl
);
664 pwm_backlight_power_off(pb
);
667 #ifdef CONFIG_PM_SLEEP
668 static int pwm_backlight_suspend(struct device
*dev
)
670 struct backlight_device
*bl
= dev_get_drvdata(dev
);
671 struct pwm_bl_data
*pb
= bl_get_data(bl
);
674 pb
->notify(pb
->dev
, 0);
676 pwm_backlight_power_off(pb
);
678 if (pb
->notify_after
)
679 pb
->notify_after(pb
->dev
, 0);
684 static int pwm_backlight_resume(struct device
*dev
)
686 struct backlight_device
*bl
= dev_get_drvdata(dev
);
688 backlight_update_status(bl
);
694 static const struct dev_pm_ops pwm_backlight_pm_ops
= {
695 #ifdef CONFIG_PM_SLEEP
696 .suspend
= pwm_backlight_suspend
,
697 .resume
= pwm_backlight_resume
,
698 .poweroff
= pwm_backlight_suspend
,
699 .restore
= pwm_backlight_resume
,
703 static struct platform_driver pwm_backlight_driver
= {
705 .name
= "pwm-backlight",
706 .pm
= &pwm_backlight_pm_ops
,
707 .of_match_table
= of_match_ptr(pwm_backlight_of_match
),
709 .probe
= pwm_backlight_probe
,
710 .remove
= pwm_backlight_remove
,
711 .shutdown
= pwm_backlight_shutdown
,
714 module_platform_driver(pwm_backlight_driver
);
716 MODULE_DESCRIPTION("PWM based Backlight Driver");
717 MODULE_LICENSE("GPL v2");
718 MODULE_ALIAS("platform:pwm-backlight");