1
2
3
4
5
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 bool legacy;
32 unsigned int post_pwm_on_delay;
33 unsigned int pwm_off_delay;
34 int (*notify)(struct device *,
35 int brightness);
36 void (*notify_after)(struct device *,
37 int brightness);
38 int (*check_fb)(struct device *, struct fb_info *);
39 void (*exit)(struct device *);
40};
41
42static void pwm_backlight_power_on(struct pwm_bl_data *pb)
43{
44 struct pwm_state state;
45 int err;
46
47 pwm_get_state(pb->pwm, &state);
48 if (pb->enabled)
49 return;
50
51 err = regulator_enable(pb->power_supply);
52 if (err < 0)
53 dev_err(pb->dev, "failed to enable power supply\n");
54
55 state.enabled = true;
56 pwm_apply_state(pb->pwm, &state);
57
58 if (pb->post_pwm_on_delay)
59 msleep(pb->post_pwm_on_delay);
60
61 if (pb->enable_gpio)
62 gpiod_set_value_cansleep(pb->enable_gpio, 1);
63
64 pb->enabled = true;
65}
66
67static void pwm_backlight_power_off(struct pwm_bl_data *pb)
68{
69 struct pwm_state state;
70
71 pwm_get_state(pb->pwm, &state);
72 if (!pb->enabled)
73 return;
74
75 if (pb->enable_gpio)
76 gpiod_set_value_cansleep(pb->enable_gpio, 0);
77
78 if (pb->pwm_off_delay)
79 msleep(pb->pwm_off_delay);
80
81 state.enabled = false;
82 state.duty_cycle = 0;
83 pwm_apply_state(pb->pwm, &state);
84
85 regulator_disable(pb->power_supply);
86 pb->enabled = false;
87}
88
89static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
90{
91 unsigned int lth = pb->lth_brightness;
92 struct pwm_state state;
93 u64 duty_cycle;
94
95 pwm_get_state(pb->pwm, &state);
96
97 if (pb->levels)
98 duty_cycle = pb->levels[brightness];
99 else
100 duty_cycle = brightness;
101
102 duty_cycle *= state.period - lth;
103 do_div(duty_cycle, pb->scale);
104
105 return duty_cycle + lth;
106}
107
108static int pwm_backlight_update_status(struct backlight_device *bl)
109{
110 struct pwm_bl_data *pb = bl_get_data(bl);
111 int brightness = backlight_get_brightness(bl);
112 struct pwm_state state;
113
114 if (pb->notify)
115 brightness = pb->notify(pb->dev, brightness);
116
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);
122 } else {
123 pwm_backlight_power_off(pb);
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)
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164static u64 cie1931(unsigned int lightness)
165{
166 u64 retval;
167
168
169
170
171
172
173
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
190
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
202
203
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
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
248
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
256
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
265 if (num_levels > 0) {
266 size_t size = sizeof(*data->levels) * num_levels;
267
268 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
269 if (!data->levels)
270 return -ENOMEM;
271
272 ret = of_property_read_u32_array(node, "brightness-levels",
273 data->levels,
274 num_levels);
275 if (ret < 0)
276 return ret;
277
278 ret = of_property_read_u32(node, "default-brightness-level",
279 &value);
280 if (ret < 0)
281 return ret;
282
283 data->dft_brightness = value;
284
285
286
287
288
289
290 of_property_read_u32(node, "num-interpolated-steps",
291 &num_steps);
292
293
294
295
296
297
298 if (num_steps) {
299 unsigned int num_input_levels = num_levels;
300 unsigned int i;
301 u32 x1, x2, x, dx;
302 u32 y1, y2;
303 s64 dy;
304
305 if (num_input_levels < 2) {
306 dev_err(dev, "can't interpolate\n");
307 return -EINVAL;
308 }
309
310
311
312
313
314
315 num_levels = (num_input_levels - 1) * num_steps + 1;
316 dev_dbg(dev, "new number of brightness levels: %d\n",
317 num_levels);
318
319
320
321
322
323 size = sizeof(*table) * num_levels;
324 table = devm_kzalloc(dev, size, GFP_KERNEL);
325 if (!table)
326 return -ENOMEM;
327
328
329
330
331 dx = num_steps;
332 for (i = 0; i < num_input_levels - 1; i++) {
333 x1 = i * dx;
334 x2 = x1 + dx;
335 y1 = data->levels[i];
336 y2 = data->levels[i + 1];
337 dy = (s64)y2 - y1;
338
339 for (x = x1; x < x2; x++) {
340 table[x] = y1 +
341 div_s64(dy * (x - x1), dx);
342 }
343 }
344
345 table[x2] = y2;
346
347
348
349
350
351
352 devm_kfree(dev, data->levels);
353 data->levels = table;
354 }
355
356 data->max_brightness = num_levels - 1;
357 }
358
359 return 0;
360}
361
362static const struct of_device_id pwm_backlight_of_match[] = {
363 { .compatible = "pwm-backlight" },
364 { }
365};
366
367MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
368#else
369static int pwm_backlight_parse_dt(struct device *dev,
370 struct platform_pwm_backlight_data *data)
371{
372 return -ENODEV;
373}
374
375static
376int pwm_backlight_brightness_default(struct device *dev,
377 struct platform_pwm_backlight_data *data,
378 unsigned int period)
379{
380 return -ENODEV;
381}
382#endif
383
384static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
385{
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];
389
390
391
392
393
394 unsigned int slope = (128 * (max_val - min_val)) / nlevels;
395 unsigned int margin = (max_val - min_val) / 20;
396 int i;
397
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]);
401
402 if (delta > margin)
403 return false;
404 }
405
406 return true;
407}
408
409static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
410{
411 struct device_node *node = pb->dev->of_node;
412
413
414 if (!node || !node->phandle)
415 return FB_BLANK_UNBLANK;
416
417
418
419
420
421
422
423
424
425 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
426 return FB_BLANK_POWERDOWN;
427
428
429 if (!regulator_is_enabled(pb->power_supply))
430 return FB_BLANK_POWERDOWN;
431
432
433 if (!pwm_is_enabled(pb->pwm))
434 return FB_BLANK_POWERDOWN;
435
436 return FB_BLANK_UNBLANK;
437}
438
439static int pwm_backlight_probe(struct platform_device *pdev)
440{
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;
448 unsigned int i;
449 int ret;
450
451 if (!data) {
452 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
453 if (ret < 0) {
454 dev_err(&pdev->dev, "failed to find platform data\n");
455 return ret;
456 }
457
458 data = &defdata;
459 }
460
461 if (data->init) {
462 ret = data->init(&pdev->dev);
463 if (ret < 0)
464 return ret;
465 }
466
467 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
468 if (!pb) {
469 ret = -ENOMEM;
470 goto err_alloc;
471 }
472
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;
478 pb->enabled = false;
479 pb->post_pwm_on_delay = data->post_pwm_on_delay;
480 pb->pwm_off_delay = data->pwm_off_delay;
481
482 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
483 GPIOD_ASIS);
484 if (IS_ERR(pb->enable_gpio)) {
485 ret = PTR_ERR(pb->enable_gpio);
486 goto err_alloc;
487 }
488
489
490
491
492
493
494
495
496
497 if (pb->enable_gpio &&
498 gpiod_get_direction(pb->enable_gpio) != 0)
499 gpiod_direction_output(pb->enable_gpio, 1);
500
501 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
502 if (IS_ERR(pb->power_supply)) {
503 ret = PTR_ERR(pb->power_supply);
504 goto err_alloc;
505 }
506
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");
510 pb->legacy = true;
511 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
512 }
513
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");
518 goto err_alloc;
519 }
520
521 dev_dbg(&pdev->dev, "got pwm for backlight\n");
522
523
524 pwm_init_state(pb->pwm, &state);
525
526
527
528
529
530
531
532 if (!state.period && (data->pwm_period_ns > 0))
533 state.period = data->pwm_period_ns;
534
535 ret = pwm_apply_state(pb->pwm, &state);
536 if (ret) {
537 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
538 ret);
539 goto err_alloc;
540 }
541
542 memset(&props, 0, sizeof(struct backlight_properties));
543
544 if (data->levels) {
545 pb->levels = data->levels;
546
547
548
549
550
551
552 for (i = 0; i <= data->max_brightness; i++)
553 if (data->levels[i] > pb->scale)
554 pb->scale = data->levels[i];
555
556 if (pwm_backlight_is_linear(data))
557 props.scale = BACKLIGHT_SCALE_LINEAR;
558 else
559 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
560 } else if (!data->max_brightness) {
561
562
563
564
565
566
567
568
569
570 pwm_get_state(pb->pwm, &state);
571
572 ret = pwm_backlight_brightness_default(&pdev->dev, data,
573 state.period);
574 if (ret < 0) {
575 dev_err(&pdev->dev,
576 "failed to setup default brightness table\n");
577 goto err_alloc;
578 }
579
580 for (i = 0; i <= data->max_brightness; i++) {
581 if (data->levels[i] > pb->scale)
582 pb->scale = data->levels[i];
583
584 pb->levels = data->levels;
585 }
586
587 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
588 } else {
589
590
591
592
593 pb->scale = data->max_brightness;
594 }
595
596 pb->lth_brightness = data->lth_brightness * (div_u64(state.period,
597 pb->scale));
598
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);
603 if (IS_ERR(bl)) {
604 dev_err(&pdev->dev, "failed to register backlight\n");
605 ret = PTR_ERR(bl);
606 if (pb->legacy)
607 pwm_free(pb->pwm);
608 goto err_alloc;
609 }
610
611 if (data->dft_brightness > data->max_brightness) {
612 dev_warn(&pdev->dev,
613 "invalid default brightness level: %u, using %u\n",
614 data->dft_brightness, data->max_brightness);
615 data->dft_brightness = data->max_brightness;
616 }
617
618 bl->props.brightness = data->dft_brightness;
619 bl->props.power = pwm_backlight_initial_power_state(pb);
620 backlight_update_status(bl);
621
622 platform_set_drvdata(pdev, bl);
623 return 0;
624
625err_alloc:
626 if (data->exit)
627 data->exit(&pdev->dev);
628 return ret;
629}
630
631static int pwm_backlight_remove(struct platform_device *pdev)
632{
633 struct backlight_device *bl = platform_get_drvdata(pdev);
634 struct pwm_bl_data *pb = bl_get_data(bl);
635
636 backlight_device_unregister(bl);
637 pwm_backlight_power_off(pb);
638
639 if (pb->exit)
640 pb->exit(&pdev->dev);
641 if (pb->legacy)
642 pwm_free(pb->pwm);
643
644 return 0;
645}
646
647static void pwm_backlight_shutdown(struct platform_device *pdev)
648{
649 struct backlight_device *bl = platform_get_drvdata(pdev);
650 struct pwm_bl_data *pb = bl_get_data(bl);
651
652 pwm_backlight_power_off(pb);
653}
654
655#ifdef CONFIG_PM_SLEEP
656static int pwm_backlight_suspend(struct device *dev)
657{
658 struct backlight_device *bl = dev_get_drvdata(dev);
659 struct pwm_bl_data *pb = bl_get_data(bl);
660
661 if (pb->notify)
662 pb->notify(pb->dev, 0);
663
664 pwm_backlight_power_off(pb);
665
666 if (pb->notify_after)
667 pb->notify_after(pb->dev, 0);
668
669 return 0;
670}
671
672static int pwm_backlight_resume(struct device *dev)
673{
674 struct backlight_device *bl = dev_get_drvdata(dev);
675
676 backlight_update_status(bl);
677
678 return 0;
679}
680#endif
681
682static 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,
688#endif
689};
690
691static struct platform_driver pwm_backlight_driver = {
692 .driver = {
693 .name = "pwm-backlight",
694 .pm = &pwm_backlight_pm_ops,
695 .of_match_table = of_match_ptr(pwm_backlight_of_match),
696 },
697 .probe = pwm_backlight_probe,
698 .remove = pwm_backlight_remove,
699 .shutdown = pwm_backlight_shutdown,
700};
701
702module_platform_driver(pwm_backlight_driver);
703
704MODULE_DESCRIPTION("PWM based Backlight Driver");
705MODULE_LICENSE("GPL v2");
706MODULE_ALIAS("platform:pwm-backlight");
707