2 * AS3711 PMIC backlight driver, using DCDC Step Up Converters
4 * Copyright (C) 2012 Renesas Electronics Corporation
5 * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the version 2 of the GNU General Public License as
9 * published by the Free Software Foundation
12 #include <linux/backlight.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/as3711.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
29 struct as3711_bl_data
{
32 struct device
*fb_dev
;
33 enum as3711_bl_type type
;
35 struct backlight_device
*bl
;
38 struct as3711_bl_supply
{
39 struct as3711_bl_data su1
;
40 struct as3711_bl_data su2
;
41 const struct as3711_bl_pdata
*pdata
;
42 struct as3711
*as3711
;
45 static struct as3711_bl_supply
*to_supply(struct as3711_bl_data
*su
)
49 return container_of(su
, struct as3711_bl_supply
, su1
);
51 return container_of(su
, struct as3711_bl_supply
, su2
);
56 static int as3711_set_brightness_auto_i(struct as3711_bl_data
*data
,
57 unsigned int brightness
)
59 struct as3711_bl_supply
*supply
= to_supply(data
);
60 struct as3711
*as3711
= supply
->as3711
;
61 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
64 /* Only all equal current values are supported */
65 if (pdata
->su2_auto_curr1
)
66 ret
= regmap_write(as3711
->regmap
, AS3711_CURR1_VALUE
,
68 if (!ret
&& pdata
->su2_auto_curr2
)
69 ret
= regmap_write(as3711
->regmap
, AS3711_CURR2_VALUE
,
71 if (!ret
&& pdata
->su2_auto_curr3
)
72 ret
= regmap_write(as3711
->regmap
, AS3711_CURR3_VALUE
,
78 static int as3711_set_brightness_v(struct as3711
*as3711
,
79 unsigned int brightness
,
85 return regmap_update_bits(as3711
->regmap
, reg
, 0xf0,
89 static int as3711_bl_su2_reset(struct as3711_bl_supply
*supply
)
91 struct as3711
*as3711
= supply
->as3711
;
92 int ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_5
,
93 3, supply
->pdata
->su2_fbprot
);
95 ret
= regmap_update_bits(as3711
->regmap
,
96 AS3711_STEPUP_CONTROL_2
, 1, 0);
98 ret
= regmap_update_bits(as3711
->regmap
,
99 AS3711_STEPUP_CONTROL_2
, 1, 1);
104 * Someone with less fragile or less expensive hardware could try to simplify
105 * the brightness adjustment procedure.
107 static int as3711_bl_update_status(struct backlight_device
*bl
)
109 struct as3711_bl_data
*data
= bl_get_data(bl
);
110 struct as3711_bl_supply
*supply
= to_supply(data
);
111 struct as3711
*as3711
= supply
->as3711
;
112 int brightness
= bl
->props
.brightness
;
115 dev_dbg(&bl
->dev
, "%s(): brightness %u, pwr %x, blank %x, state %x\n",
116 __func__
, bl
->props
.brightness
, bl
->props
.power
,
117 bl
->props
.fb_blank
, bl
->props
.state
);
119 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
120 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
121 bl
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
124 if (data
->type
== AS3711_BL_SU1
) {
125 ret
= as3711_set_brightness_v(as3711
, brightness
,
126 AS3711_STEPUP_CONTROL_1
);
128 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
130 switch (pdata
->su2_feedback
) {
131 case AS3711_SU2_VOLTAGE
:
132 ret
= as3711_set_brightness_v(as3711
, brightness
,
133 AS3711_STEPUP_CONTROL_2
);
135 case AS3711_SU2_CURR_AUTO
:
136 ret
= as3711_set_brightness_auto_i(data
, brightness
/ 4);
140 ret
= as3711_bl_su2_reset(supply
);
144 ret
= as3711_set_brightness_auto_i(data
, brightness
);
146 ret
= regmap_update_bits(as3711
->regmap
,
147 AS3711_STEPUP_CONTROL_2
, 1, 0);
150 /* Manual one current feedback pin below */
151 case AS3711_SU2_CURR1
:
152 ret
= regmap_write(as3711
->regmap
, AS3711_CURR1_VALUE
,
155 case AS3711_SU2_CURR2
:
156 ret
= regmap_write(as3711
->regmap
, AS3711_CURR2_VALUE
,
159 case AS3711_SU2_CURR3
:
160 ret
= regmap_write(as3711
->regmap
, AS3711_CURR3_VALUE
,
168 data
->brightness
= brightness
;
173 static int as3711_bl_get_brightness(struct backlight_device
*bl
)
175 struct as3711_bl_data
*data
= bl_get_data(bl
);
177 return data
->brightness
;
180 static const struct backlight_ops as3711_bl_ops
= {
181 .update_status
= as3711_bl_update_status
,
182 .get_brightness
= as3711_bl_get_brightness
,
185 static int as3711_bl_init_su2(struct as3711_bl_supply
*supply
)
187 struct as3711
*as3711
= supply
->as3711
;
188 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
192 dev_dbg(as3711
->dev
, "%s(): use %u\n", __func__
, pdata
->su2_feedback
);
195 ret
= regmap_write(as3711
->regmap
, AS3711_STEPUP_CONTROL_2
, 0);
199 switch (pdata
->su2_feedback
) {
200 case AS3711_SU2_VOLTAGE
:
201 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 0);
203 case AS3711_SU2_CURR1
:
205 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 1);
207 case AS3711_SU2_CURR2
:
209 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 2);
211 case AS3711_SU2_CURR3
:
213 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 3);
215 case AS3711_SU2_CURR_AUTO
:
216 if (pdata
->su2_auto_curr1
)
218 if (pdata
->su2_auto_curr2
)
220 if (pdata
->su2_auto_curr3
)
229 ret
= regmap_write(as3711
->regmap
, AS3711_CURR_CONTROL
, ctl
);
234 static int as3711_bl_register(struct platform_device
*pdev
,
235 unsigned int max_brightness
, struct as3711_bl_data
*su
)
237 struct backlight_properties props
= {.type
= BACKLIGHT_RAW
,};
238 struct backlight_device
*bl
;
240 /* max tuning I = 31uA for voltage- and 38250uA for current-feedback */
241 props
.max_brightness
= max_brightness
;
243 bl
= backlight_device_register(su
->type
== AS3711_BL_SU1
?
244 "as3711-su1" : "as3711-su2",
246 &as3711_bl_ops
, &props
);
248 dev_err(&pdev
->dev
, "failed to register backlight\n");
252 bl
->props
.brightness
= props
.max_brightness
;
254 backlight_update_status(bl
);
261 static int as3711_backlight_parse_dt(struct device
*dev
)
263 struct as3711_bl_pdata
*pdata
= dev_get_platdata(dev
);
264 struct device_node
*bl
=
265 of_find_node_by_name(dev
->parent
->of_node
, "backlight"), *fb
;
269 dev_dbg(dev
, "backlight node not found\n");
273 fb
= of_parse_phandle(bl
, "su1-dev", 0);
275 pdata
->su1_fb
= fb
->full_name
;
277 ret
= of_property_read_u32(bl
, "su1-max-uA", &pdata
->su1_max_uA
);
278 if (pdata
->su1_max_uA
<= 0)
284 fb
= of_parse_phandle(bl
, "su2-dev", 0);
288 pdata
->su2_fb
= fb
->full_name
;
290 ret
= of_property_read_u32(bl
, "su2-max-uA", &pdata
->su2_max_uA
);
291 if (pdata
->su2_max_uA
<= 0)
296 if (of_find_property(bl
, "su2-feedback-voltage", NULL
)) {
297 pdata
->su2_feedback
= AS3711_SU2_VOLTAGE
;
300 if (of_find_property(bl
, "su2-feedback-curr1", NULL
)) {
301 pdata
->su2_feedback
= AS3711_SU2_CURR1
;
304 if (of_find_property(bl
, "su2-feedback-curr2", NULL
)) {
305 pdata
->su2_feedback
= AS3711_SU2_CURR2
;
308 if (of_find_property(bl
, "su2-feedback-curr3", NULL
)) {
309 pdata
->su2_feedback
= AS3711_SU2_CURR3
;
312 if (of_find_property(bl
, "su2-feedback-curr-auto", NULL
)) {
313 pdata
->su2_feedback
= AS3711_SU2_CURR_AUTO
;
320 if (of_find_property(bl
, "su2-fbprot-lx-sd4", NULL
)) {
321 pdata
->su2_fbprot
= AS3711_SU2_LX_SD4
;
324 if (of_find_property(bl
, "su2-fbprot-gpio2", NULL
)) {
325 pdata
->su2_fbprot
= AS3711_SU2_GPIO2
;
328 if (of_find_property(bl
, "su2-fbprot-gpio3", NULL
)) {
329 pdata
->su2_fbprot
= AS3711_SU2_GPIO3
;
332 if (of_find_property(bl
, "su2-fbprot-gpio4", NULL
)) {
333 pdata
->su2_fbprot
= AS3711_SU2_GPIO4
;
340 if (of_find_property(bl
, "su2-auto-curr1", NULL
)) {
341 pdata
->su2_auto_curr1
= true;
344 if (of_find_property(bl
, "su2-auto-curr2", NULL
)) {
345 pdata
->su2_auto_curr2
= true;
348 if (of_find_property(bl
, "su2-auto-curr3", NULL
)) {
349 pdata
->su2_auto_curr3
= true;
354 * At least one su2-auto-curr* must be specified iff
355 * AS3711_SU2_CURR_AUTO is used
357 if (!count
^ (pdata
->su2_feedback
!= AS3711_SU2_CURR_AUTO
))
364 static int as3711_backlight_probe(struct platform_device
*pdev
)
366 struct as3711_bl_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
367 struct as3711
*as3711
= dev_get_drvdata(pdev
->dev
.parent
);
368 struct as3711_bl_supply
*supply
;
369 struct as3711_bl_data
*su
;
370 unsigned int max_brightness
;
374 dev_err(&pdev
->dev
, "No platform data, exiting...\n");
378 if (pdev
->dev
.parent
->of_node
) {
379 ret
= as3711_backlight_parse_dt(&pdev
->dev
);
381 dev_err(&pdev
->dev
, "DT parsing failed: %d\n", ret
);
386 if (!pdata
->su1_fb
&& !pdata
->su2_fb
) {
387 dev_err(&pdev
->dev
, "No framebuffer specified\n");
392 * Due to possible hardware damage I chose to block all modes,
393 * unsupported on my hardware. Anyone, wishing to use any of those modes
394 * will have to first review the code, then activate and test it.
397 pdata
->su2_fbprot
!= AS3711_SU2_GPIO4
||
398 pdata
->su2_feedback
!= AS3711_SU2_CURR_AUTO
) {
400 "Attention! An untested mode has been chosen!\n"
401 "Please, review the code, enable, test, and report success:-)\n");
405 supply
= devm_kzalloc(&pdev
->dev
, sizeof(*supply
), GFP_KERNEL
);
409 supply
->as3711
= as3711
;
410 supply
->pdata
= pdata
;
414 su
->fb_name
= pdata
->su1_fb
;
415 su
->type
= AS3711_BL_SU1
;
417 max_brightness
= min(pdata
->su1_max_uA
, 31);
418 ret
= as3711_bl_register(pdev
, max_brightness
, su
);
425 su
->fb_name
= pdata
->su2_fb
;
426 su
->type
= AS3711_BL_SU2
;
428 switch (pdata
->su2_fbprot
) {
429 case AS3711_SU2_GPIO2
:
430 case AS3711_SU2_GPIO3
:
431 case AS3711_SU2_GPIO4
:
432 case AS3711_SU2_LX_SD4
:
439 switch (pdata
->su2_feedback
) {
440 case AS3711_SU2_VOLTAGE
:
441 max_brightness
= min(pdata
->su2_max_uA
, 31);
443 case AS3711_SU2_CURR1
:
444 case AS3711_SU2_CURR2
:
445 case AS3711_SU2_CURR3
:
446 case AS3711_SU2_CURR_AUTO
:
447 max_brightness
= min(pdata
->su2_max_uA
/ 150, 255);
454 ret
= as3711_bl_init_su2(supply
);
458 ret
= as3711_bl_register(pdev
, max_brightness
, su
);
463 platform_set_drvdata(pdev
, supply
);
468 backlight_device_unregister(supply
->su1
.bl
);
472 static int as3711_backlight_remove(struct platform_device
*pdev
)
474 struct as3711_bl_supply
*supply
= platform_get_drvdata(pdev
);
476 backlight_device_unregister(supply
->su1
.bl
);
477 backlight_device_unregister(supply
->su2
.bl
);
482 static struct platform_driver as3711_backlight_driver
= {
484 .name
= "as3711-backlight",
485 .owner
= THIS_MODULE
,
487 .probe
= as3711_backlight_probe
,
488 .remove
= as3711_backlight_remove
,
491 module_platform_driver(as3711_backlight_driver
);
493 MODULE_DESCRIPTION("Backlight Driver for AS3711 PMICs");
494 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de");
495 MODULE_LICENSE("GPL");
496 MODULE_ALIAS("platform:as3711-backlight");