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
{
31 enum as3711_bl_type type
;
33 struct backlight_device
*bl
;
36 struct as3711_bl_supply
{
37 struct as3711_bl_data su1
;
38 struct as3711_bl_data su2
;
39 const struct as3711_bl_pdata
*pdata
;
40 struct as3711
*as3711
;
43 static struct as3711_bl_supply
*to_supply(struct as3711_bl_data
*su
)
47 return container_of(su
, struct as3711_bl_supply
, su1
);
49 return container_of(su
, struct as3711_bl_supply
, su2
);
54 static int as3711_set_brightness_auto_i(struct as3711_bl_data
*data
,
55 unsigned int brightness
)
57 struct as3711_bl_supply
*supply
= to_supply(data
);
58 struct as3711
*as3711
= supply
->as3711
;
59 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
62 /* Only all equal current values are supported */
63 if (pdata
->su2_auto_curr1
)
64 ret
= regmap_write(as3711
->regmap
, AS3711_CURR1_VALUE
,
66 if (!ret
&& pdata
->su2_auto_curr2
)
67 ret
= regmap_write(as3711
->regmap
, AS3711_CURR2_VALUE
,
69 if (!ret
&& pdata
->su2_auto_curr3
)
70 ret
= regmap_write(as3711
->regmap
, AS3711_CURR3_VALUE
,
76 static int as3711_set_brightness_v(struct as3711
*as3711
,
77 unsigned int brightness
,
83 return regmap_update_bits(as3711
->regmap
, reg
, 0xf0,
87 static int as3711_bl_su2_reset(struct as3711_bl_supply
*supply
)
89 struct as3711
*as3711
= supply
->as3711
;
90 int ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_5
,
91 3, supply
->pdata
->su2_fbprot
);
93 ret
= regmap_update_bits(as3711
->regmap
,
94 AS3711_STEPUP_CONTROL_2
, 1, 0);
96 ret
= regmap_update_bits(as3711
->regmap
,
97 AS3711_STEPUP_CONTROL_2
, 1, 1);
102 * Someone with less fragile or less expensive hardware could try to simplify
103 * the brightness adjustment procedure.
105 static int as3711_bl_update_status(struct backlight_device
*bl
)
107 struct as3711_bl_data
*data
= bl_get_data(bl
);
108 struct as3711_bl_supply
*supply
= to_supply(data
);
109 struct as3711
*as3711
= supply
->as3711
;
110 int brightness
= bl
->props
.brightness
;
113 dev_dbg(&bl
->dev
, "%s(): brightness %u, pwr %x, blank %x, state %x\n",
114 __func__
, bl
->props
.brightness
, bl
->props
.power
,
115 bl
->props
.fb_blank
, bl
->props
.state
);
117 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
118 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
119 bl
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
122 if (data
->type
== AS3711_BL_SU1
) {
123 ret
= as3711_set_brightness_v(as3711
, brightness
,
124 AS3711_STEPUP_CONTROL_1
);
126 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
128 switch (pdata
->su2_feedback
) {
129 case AS3711_SU2_VOLTAGE
:
130 ret
= as3711_set_brightness_v(as3711
, brightness
,
131 AS3711_STEPUP_CONTROL_2
);
133 case AS3711_SU2_CURR_AUTO
:
134 ret
= as3711_set_brightness_auto_i(data
, brightness
/ 4);
138 ret
= as3711_bl_su2_reset(supply
);
142 ret
= as3711_set_brightness_auto_i(data
, brightness
);
144 ret
= regmap_update_bits(as3711
->regmap
,
145 AS3711_STEPUP_CONTROL_2
, 1, 0);
148 /* Manual one current feedback pin below */
149 case AS3711_SU2_CURR1
:
150 ret
= regmap_write(as3711
->regmap
, AS3711_CURR1_VALUE
,
153 case AS3711_SU2_CURR2
:
154 ret
= regmap_write(as3711
->regmap
, AS3711_CURR2_VALUE
,
157 case AS3711_SU2_CURR3
:
158 ret
= regmap_write(as3711
->regmap
, AS3711_CURR3_VALUE
,
166 data
->brightness
= brightness
;
171 static int as3711_bl_get_brightness(struct backlight_device
*bl
)
173 struct as3711_bl_data
*data
= bl_get_data(bl
);
175 return data
->brightness
;
178 static const struct backlight_ops as3711_bl_ops
= {
179 .update_status
= as3711_bl_update_status
,
180 .get_brightness
= as3711_bl_get_brightness
,
183 static int as3711_bl_init_su2(struct as3711_bl_supply
*supply
)
185 struct as3711
*as3711
= supply
->as3711
;
186 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
190 dev_dbg(as3711
->dev
, "%s(): use %u\n", __func__
, pdata
->su2_feedback
);
193 ret
= regmap_write(as3711
->regmap
, AS3711_STEPUP_CONTROL_2
, 0);
197 switch (pdata
->su2_feedback
) {
198 case AS3711_SU2_VOLTAGE
:
199 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 0);
201 case AS3711_SU2_CURR1
:
203 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 1);
205 case AS3711_SU2_CURR2
:
207 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 2);
209 case AS3711_SU2_CURR3
:
211 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 3);
213 case AS3711_SU2_CURR_AUTO
:
214 if (pdata
->su2_auto_curr1
)
216 if (pdata
->su2_auto_curr2
)
218 if (pdata
->su2_auto_curr3
)
227 ret
= regmap_write(as3711
->regmap
, AS3711_CURR_CONTROL
, ctl
);
232 static int as3711_bl_register(struct platform_device
*pdev
,
233 unsigned int max_brightness
, struct as3711_bl_data
*su
)
235 struct backlight_properties props
= {.type
= BACKLIGHT_RAW
,};
236 struct backlight_device
*bl
;
238 /* max tuning I = 31uA for voltage- and 38250uA for current-feedback */
239 props
.max_brightness
= max_brightness
;
241 bl
= devm_backlight_device_register(&pdev
->dev
,
242 su
->type
== AS3711_BL_SU1
?
243 "as3711-su1" : "as3711-su2",
245 &as3711_bl_ops
, &props
);
247 dev_err(&pdev
->dev
, "failed to register backlight\n");
251 bl
->props
.brightness
= props
.max_brightness
;
253 backlight_update_status(bl
);
260 static int as3711_backlight_parse_dt(struct device
*dev
)
262 struct as3711_bl_pdata
*pdata
= dev_get_platdata(dev
);
263 struct device_node
*bl
, *fb
;
266 bl
= of_get_child_by_name(dev
->parent
->of_node
, "backlight");
268 dev_dbg(dev
, "backlight node not found\n");
272 fb
= of_parse_phandle(bl
, "su1-dev", 0);
276 pdata
->su1_fb
= true;
278 ret
= of_property_read_u32(bl
, "su1-max-uA", &pdata
->su1_max_uA
);
279 if (pdata
->su1_max_uA
<= 0)
285 fb
= of_parse_phandle(bl
, "su2-dev", 0);
291 pdata
->su2_fb
= true;
293 ret
= of_property_read_u32(bl
, "su2-max-uA", &pdata
->su2_max_uA
);
294 if (pdata
->su2_max_uA
<= 0)
299 if (of_find_property(bl
, "su2-feedback-voltage", NULL
)) {
300 pdata
->su2_feedback
= AS3711_SU2_VOLTAGE
;
303 if (of_find_property(bl
, "su2-feedback-curr1", NULL
)) {
304 pdata
->su2_feedback
= AS3711_SU2_CURR1
;
307 if (of_find_property(bl
, "su2-feedback-curr2", NULL
)) {
308 pdata
->su2_feedback
= AS3711_SU2_CURR2
;
311 if (of_find_property(bl
, "su2-feedback-curr3", NULL
)) {
312 pdata
->su2_feedback
= AS3711_SU2_CURR3
;
315 if (of_find_property(bl
, "su2-feedback-curr-auto", NULL
)) {
316 pdata
->su2_feedback
= AS3711_SU2_CURR_AUTO
;
325 if (of_find_property(bl
, "su2-fbprot-lx-sd4", NULL
)) {
326 pdata
->su2_fbprot
= AS3711_SU2_LX_SD4
;
329 if (of_find_property(bl
, "su2-fbprot-gpio2", NULL
)) {
330 pdata
->su2_fbprot
= AS3711_SU2_GPIO2
;
333 if (of_find_property(bl
, "su2-fbprot-gpio3", NULL
)) {
334 pdata
->su2_fbprot
= AS3711_SU2_GPIO3
;
337 if (of_find_property(bl
, "su2-fbprot-gpio4", NULL
)) {
338 pdata
->su2_fbprot
= AS3711_SU2_GPIO4
;
347 if (of_find_property(bl
, "su2-auto-curr1", NULL
)) {
348 pdata
->su2_auto_curr1
= true;
351 if (of_find_property(bl
, "su2-auto-curr2", NULL
)) {
352 pdata
->su2_auto_curr2
= true;
355 if (of_find_property(bl
, "su2-auto-curr3", NULL
)) {
356 pdata
->su2_auto_curr3
= true;
361 * At least one su2-auto-curr* must be specified iff
362 * AS3711_SU2_CURR_AUTO is used
364 if (!count
^ (pdata
->su2_feedback
!= AS3711_SU2_CURR_AUTO
)) {
380 static int as3711_backlight_probe(struct platform_device
*pdev
)
382 struct as3711_bl_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
383 struct as3711
*as3711
= dev_get_drvdata(pdev
->dev
.parent
);
384 struct as3711_bl_supply
*supply
;
385 struct as3711_bl_data
*su
;
386 unsigned int max_brightness
;
390 dev_err(&pdev
->dev
, "No platform data, exiting...\n");
394 if (pdev
->dev
.parent
->of_node
) {
395 ret
= as3711_backlight_parse_dt(&pdev
->dev
);
397 dev_err(&pdev
->dev
, "DT parsing failed: %d\n", ret
);
402 if (!pdata
->su1_fb
&& !pdata
->su2_fb
) {
403 dev_err(&pdev
->dev
, "No framebuffer specified\n");
408 * Due to possible hardware damage I chose to block all modes,
409 * unsupported on my hardware. Anyone, wishing to use any of those modes
410 * will have to first review the code, then activate and test it.
413 pdata
->su2_fbprot
!= AS3711_SU2_GPIO4
||
414 pdata
->su2_feedback
!= AS3711_SU2_CURR_AUTO
) {
416 "Attention! An untested mode has been chosen!\n"
417 "Please, review the code, enable, test, and report success:-)\n");
421 supply
= devm_kzalloc(&pdev
->dev
, sizeof(*supply
), GFP_KERNEL
);
425 supply
->as3711
= as3711
;
426 supply
->pdata
= pdata
;
430 su
->type
= AS3711_BL_SU1
;
432 max_brightness
= min(pdata
->su1_max_uA
, 31);
433 ret
= as3711_bl_register(pdev
, max_brightness
, su
);
440 su
->type
= AS3711_BL_SU2
;
442 switch (pdata
->su2_fbprot
) {
443 case AS3711_SU2_GPIO2
:
444 case AS3711_SU2_GPIO3
:
445 case AS3711_SU2_GPIO4
:
446 case AS3711_SU2_LX_SD4
:
452 switch (pdata
->su2_feedback
) {
453 case AS3711_SU2_VOLTAGE
:
454 max_brightness
= min(pdata
->su2_max_uA
, 31);
456 case AS3711_SU2_CURR1
:
457 case AS3711_SU2_CURR2
:
458 case AS3711_SU2_CURR3
:
459 case AS3711_SU2_CURR_AUTO
:
460 max_brightness
= min(pdata
->su2_max_uA
/ 150, 255);
466 ret
= as3711_bl_init_su2(supply
);
470 ret
= as3711_bl_register(pdev
, max_brightness
, su
);
475 platform_set_drvdata(pdev
, supply
);
480 static struct platform_driver as3711_backlight_driver
= {
482 .name
= "as3711-backlight",
484 .probe
= as3711_backlight_probe
,
487 module_platform_driver(as3711_backlight_driver
);
489 MODULE_DESCRIPTION("Backlight Driver for AS3711 PMICs");
490 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de");
491 MODULE_LICENSE("GPL");
492 MODULE_ALIAS("platform:as3711-backlight");