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
22struct 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	unsigned int		post_pwm_on_delay;
32	unsigned int		pwm_off_delay;
33	int			(*notify)(struct device *,
34					  int brightness);
35	void			(*notify_after)(struct device *,
36					int brightness);
37	int			(*check_fb)(struct device *, struct fb_info *);
38	void			(*exit)(struct device *);
39};
40
41static void pwm_backlight_power_on(struct pwm_bl_data *pb)
42{
43	int err;
44
45	if (pb->enabled)
46		return;
47
48	if (pb->power_supply) {
49		err = regulator_enable(pb->power_supply);
50		if (err < 0)
51			dev_err(pb->dev, "failed to enable power supply\n");
52	}
53
54	if (pb->post_pwm_on_delay)
55		msleep(pb->post_pwm_on_delay);
56
57	gpiod_set_value_cansleep(pb->enable_gpio, 1);
58
59	pb->enabled = true;
60}
61
62static void pwm_backlight_power_off(struct pwm_bl_data *pb)
63{
64	if (!pb->enabled)
65		return;
66
67	gpiod_set_value_cansleep(pb->enable_gpio, 0);
68
69	if (pb->pwm_off_delay)
70		msleep(pb->pwm_off_delay);
71
72	if (pb->power_supply)
73		regulator_disable(pb->power_supply);
74	pb->enabled = false;
75}
76
77static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness, struct pwm_state *state)
78{
79	unsigned int lth = pb->lth_brightness;
80	u64 duty_cycle;
81
82	if (pb->levels)
83		duty_cycle = pb->levels[brightness];
84	else
85		duty_cycle = brightness;
86
87	duty_cycle *= state->period - lth;
88	do_div(duty_cycle, pb->scale);
89
90	return duty_cycle + lth;
91}
92
93static int pwm_backlight_update_status(struct backlight_device *bl)
94{
95	struct pwm_bl_data *pb = bl_get_data(bl);
96	int brightness = backlight_get_brightness(bl);
97	struct pwm_state state;
98
99	if (pb->notify)
100		brightness = pb->notify(pb->dev, brightness);
101
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);
107
108		pwm_backlight_power_on(pb);
109	} else {
110		pwm_backlight_power_off(pb);
111
112		pwm_get_state(pb->pwm, &state);
113		state.duty_cycle = 0;
114		/*
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
120		 * inactive output.
121		 */
122		state.enabled = !pb->power_supply && !pb->enable_gpio;
123		pwm_apply_might_sleep(pb->pwm, &state);
124	}
125
126	if (pb->notify_after)
127		pb->notify_after(pb->dev, brightness);
128
129	return 0;
130}
131
132static 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
140static 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 */
164static 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 */
192static
193int 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
229static 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;
234	unsigned int num_steps = 0;
235	struct property *prop;
236	unsigned int *table;
237	int length;
238	u32 value;
239	int ret;
240
241	if (!node)
242		return -ENODEV;
243
244	memset(data, 0, sizeof(*data));
245
246	/*
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.
249	 */
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);
253
254	/*
255	 * Determine the number of brightness levels, if this property is not
256	 * set a default table of brightness levels will be used.
257	 */
258	prop = of_find_property(node, "brightness-levels", &length);
259	if (!prop)
260		return 0;
261
262	num_levels = length / sizeof(u32);
263
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);
268		if (!data->levels)
269			return -ENOMEM;
270
271		ret = of_property_read_u32_array(node, "brightness-levels",
272						 data->levels,
273						 num_levels);
274		if (ret < 0)
275			return ret;
276
277		ret = of_property_read_u32(node, "default-brightness-level",
278					   &value);
279		if (ret < 0)
280			return ret;
281
282		data->dft_brightness = value;
283
284		/*
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.
288		 */
289		of_property_read_u32(node, "num-interpolated-steps",
290				     &num_steps);
291
292		/*
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.
296		 */
297		if (num_steps) {
298			unsigned int num_input_levels = num_levels;
299			unsigned int i;
300			u32 x1, x2, x, dx;
301			u32 y1, y2;
302			s64 dy;
303
304			if (num_input_levels < 2) {
305				dev_err(dev, "can't interpolate\n");
306				return -EINVAL;
307			}
308
309			/*
310			 * Recalculate the number of brightness levels, now
311			 * taking in consideration the number of interpolated
312			 * steps between two levels.
313			 */
314			num_levels = (num_input_levels - 1) * num_steps + 1;
315			dev_dbg(dev, "new number of brightness levels: %d\n",
316				num_levels);
317
318			/*
319			 * Create a new table of brightness levels with all the
320			 * interpolated steps.
321			 */
322			table = devm_kcalloc(dev, num_levels, sizeof(*table),
323					     GFP_KERNEL);
324			if (!table)
325				return -ENOMEM;
326			/*
327			 * Fill the interpolated table[x] = y
328			 * by draw lines between each (x1, y1) to (x2, y2).
329			 */
330			dx = num_steps;
331			for (i = 0; i < num_input_levels - 1; i++) {
332				x1 = i * dx;
333				x2 = x1 + dx;
334				y1 = data->levels[i];
335				y2 = data->levels[i + 1];
336				dy = (s64)y2 - y1;
337
338				for (x = x1; x < x2; x++) {
339					table[x] = y1 +
340						div_s64(dy * (x - x1), dx);
341				}
342			}
343			/* Fill in the last point, since no line starts here. */
344			table[x2] = y2;
345
346			/*
347			 * As we use interpolation lets remove current
348			 * brightness levels table and replace for the
349			 * new interpolated table.
350			 */
351			devm_kfree(dev, data->levels);
352			data->levels = table;
353		}
354
355		data->max_brightness = num_levels - 1;
356	}
357
358	return 0;
359}
360
361static const struct of_device_id pwm_backlight_of_match[] = {
362	{ .compatible = "pwm-backlight" },
363	{ }
364};
365
366MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
367#else
368static int pwm_backlight_parse_dt(struct device *dev,
369				  struct platform_pwm_backlight_data *data)
370{
371	return -ENODEV;
372}
373
374static
375int pwm_backlight_brightness_default(struct device *dev,
376				     struct platform_pwm_backlight_data *data,
377				     unsigned int period)
378{
379	return -ENODEV;
380}
381#endif
382
383static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
384{
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];
388	/*
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
391	 * than 1%.
392	 */
393	unsigned int slope = (128 * (max_val - min_val)) / nlevels;
394	unsigned int margin = (max_val - min_val) / 20; /* 5% */
395	int i;
396
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]);
400
401		if (delta > margin)
402			return false;
403	}
404
405	return true;
406}
407
408static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
409{
410	struct device_node *node = pb->dev->of_node;
411	bool active = true;
412
413	/*
414	 * If the enable GPIO is present, observable (either as input
415	 * or output) and off then the backlight is not currently active.
416	 * */
417	if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
418		active = false;
419
420	if (pb->power_supply && !regulator_is_enabled(pb->power_supply))
421		active = false;
422
423	if (!pwm_is_enabled(pb->pwm))
424		active = false;
425
426	/*
427	 * Synchronize the enable_gpio with the observed state of the
428	 * hardware.
429	 */
430	gpiod_direction_output(pb->enable_gpio, active);
431
432	/*
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.
436	 */
437
438	/* Not booted with device tree or no phandle link to the node */
439	if (!node || !node->phandle)
440		return FB_BLANK_UNBLANK;
441
442	/*
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.
447	 */
448	return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN;
449}
450
451static int pwm_backlight_probe(struct platform_device *pdev)
452{
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;
459	unsigned int i;
460	int ret;
461
462	if (!data) {
463		ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
464		if (ret < 0)
465			return dev_err_probe(&pdev->dev, ret,
466					     "failed to find platform data\n");
467
468		data = &defdata;
469	}
470
471	if (data->init) {
472		ret = data->init(&pdev->dev);
473		if (ret < 0)
474			return ret;
475	}
476
477	pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
478	if (!pb) {
479		ret = -ENOMEM;
480		goto err_alloc;
481	}
482
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;
488	pb->enabled = false;
489	pb->post_pwm_on_delay = data->post_pwm_on_delay;
490	pb->pwm_off_delay = data->pwm_off_delay;
491
492	pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
493						  GPIOD_ASIS);
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");
497		goto err_alloc;
498	}
499
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;
505		} else {
506			dev_err_probe(&pdev->dev, ret,
507				      "failed to acquire power regulator\n");
508			goto err_alloc;
509		}
510	}
511
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");
516		goto err_alloc;
517	}
518
519	dev_dbg(&pdev->dev, "got pwm for backlight\n");
520
521	/* Sync up PWM state. */
522	pwm_init_state(pb->pwm, &state);
523
524	/*
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.
529	 */
530	if (!state.period && (data->pwm_period_ns > 0))
531		state.period = data->pwm_period_ns;
532
533	ret = pwm_apply_might_sleep(pb->pwm, &state);
534	if (ret) {
535		dev_err_probe(&pdev->dev, ret,
536			      "failed to apply initial PWM state");
537		goto err_alloc;
538	}
539
540	memset(&props, 0, sizeof(struct backlight_properties));
541
542	if (data->levels) {
543		pb->levels = data->levels;
544
545		/*
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.
549		 */
550		for (i = 0; i <= data->max_brightness; i++)
551			if (data->levels[i] > pb->scale)
552				pb->scale = data->levels[i];
553
554		if (pwm_backlight_is_linear(data))
555			props.scale = BACKLIGHT_SCALE_LINEAR;
556		else
557			props.scale = BACKLIGHT_SCALE_NON_LINEAR;
558	} else if (!data->max_brightness) {
559		/*
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
564		 * set to some value.
565		 */
566
567		/* Get the PWM period (in nanoseconds) */
568		pwm_get_state(pb->pwm, &state);
569
570		ret = pwm_backlight_brightness_default(&pdev->dev, data,
571						       state.period);
572		if (ret < 0) {
573			dev_err_probe(&pdev->dev, ret,
574				      "failed to setup default brightness table\n");
575			goto err_alloc;
576		}
577
578		for (i = 0; i <= data->max_brightness; i++) {
579			if (data->levels[i] > pb->scale)
580				pb->scale = data->levels[i];
581
582			pb->levels = data->levels;
583		}
584
585		props.scale = BACKLIGHT_SCALE_NON_LINEAR;
586	} else {
587		/*
588		 * That only happens for the non-DT case, where platform data
589		 * sets the max_brightness value.
590		 */
591		pb->scale = data->max_brightness;
592	}
593
594	pb->lth_brightness = data->lth_brightness * (div_u64(state.period,
595				pb->scale));
596
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);
601	if (IS_ERR(bl)) {
602		ret = dev_err_probe(&pdev->dev, PTR_ERR(bl),
603				    "failed to register backlight\n");
604		goto err_alloc;
605	}
606
607	if (data->dft_brightness > data->max_brightness) {
608		dev_warn(&pdev->dev,
609			 "invalid default brightness level: %u, using %u\n",
610			 data->dft_brightness, data->max_brightness);
611		data->dft_brightness = data->max_brightness;
612	}
613
614	bl->props.brightness = data->dft_brightness;
615	bl->props.power = pwm_backlight_initial_power_state(pb);
616	backlight_update_status(bl);
617
618	platform_set_drvdata(pdev, bl);
619	return 0;
620
621err_alloc:
622	if (data->exit)
623		data->exit(&pdev->dev);
624	return ret;
625}
626
627static void pwm_backlight_remove(struct platform_device *pdev)
628{
629	struct backlight_device *bl = platform_get_drvdata(pdev);
630	struct pwm_bl_data *pb = bl_get_data(bl);
631	struct pwm_state state;
632
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);
639
640	if (pb->exit)
641		pb->exit(&pdev->dev);
642}
643
644static void pwm_backlight_shutdown(struct platform_device *pdev)
645{
646	struct backlight_device *bl = platform_get_drvdata(pdev);
647	struct pwm_bl_data *pb = bl_get_data(bl);
648	struct pwm_state state;
649
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);
655}
656
657#ifdef CONFIG_PM_SLEEP
658static int pwm_backlight_suspend(struct device *dev)
659{
660	struct backlight_device *bl = dev_get_drvdata(dev);
661	struct pwm_bl_data *pb = bl_get_data(bl);
662	struct pwm_state state;
663
664	if (pb->notify)
665		pb->notify(pb->dev, 0);
666
667	pwm_backlight_power_off(pb);
668
669	/*
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.
674	 */
675	pwm_get_state(pb->pwm, &state);
676	state.duty_cycle = 0;
677	state.enabled = false;
678	pwm_apply_might_sleep(pb->pwm, &state);
679
680	if (pb->notify_after)
681		pb->notify_after(pb->dev, 0);
682
683	return 0;
684}
685
686static int pwm_backlight_resume(struct device *dev)
687{
688	struct backlight_device *bl = dev_get_drvdata(dev);
689
690	backlight_update_status(bl);
691
692	return 0;
693}
694#endif
695
696static 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,
702#endif
703};
704
705static struct platform_driver pwm_backlight_driver = {
706	.driver		= {
707		.name		= "pwm-backlight",
708		.pm		= &pwm_backlight_pm_ops,
709		.of_match_table	= of_match_ptr(pwm_backlight_of_match),
710	},
711	.probe		= pwm_backlight_probe,
712	.remove_new	= pwm_backlight_remove,
713	.shutdown	= pwm_backlight_shutdown,
714};
715
716module_platform_driver(pwm_backlight_driver);
717
718MODULE_DESCRIPTION("PWM based Backlight Driver");
719MODULE_LICENSE("GPL v2");
720MODULE_ALIAS("platform:pwm-backlight");
721