1 // SPDX-License-Identifier: GPL-2.0
3 * AS3711 PMIC backlight driver, using DCDC Step Up Converters
5 * Copyright (C) 2012 Renesas Electronics Corporation
6 * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
9 #include <linux/backlight.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/as3711.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
26 struct as3711_bl_data
{
28 enum as3711_bl_type type
;
30 struct backlight_device
*bl
;
33 struct as3711_bl_supply
{
34 struct as3711_bl_data su1
;
35 struct as3711_bl_data su2
;
36 const struct as3711_bl_pdata
*pdata
;
37 struct as3711
*as3711
;
40 static struct as3711_bl_supply
*to_supply(struct as3711_bl_data
*su
)
44 return container_of(su
, struct as3711_bl_supply
, su1
);
46 return container_of(su
, struct as3711_bl_supply
, su2
);
51 static int as3711_set_brightness_auto_i(struct as3711_bl_data
*data
,
52 unsigned int brightness
)
54 struct as3711_bl_supply
*supply
= to_supply(data
);
55 struct as3711
*as3711
= supply
->as3711
;
56 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
59 /* Only all equal current values are supported */
60 if (pdata
->su2_auto_curr1
)
61 ret
= regmap_write(as3711
->regmap
, AS3711_CURR1_VALUE
,
63 if (!ret
&& pdata
->su2_auto_curr2
)
64 ret
= regmap_write(as3711
->regmap
, AS3711_CURR2_VALUE
,
66 if (!ret
&& pdata
->su2_auto_curr3
)
67 ret
= regmap_write(as3711
->regmap
, AS3711_CURR3_VALUE
,
73 static int as3711_set_brightness_v(struct as3711
*as3711
,
74 unsigned int brightness
,
80 return regmap_update_bits(as3711
->regmap
, reg
, 0xf0,
84 static int as3711_bl_su2_reset(struct as3711_bl_supply
*supply
)
86 struct as3711
*as3711
= supply
->as3711
;
87 int ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_5
,
88 3, supply
->pdata
->su2_fbprot
);
90 ret
= regmap_update_bits(as3711
->regmap
,
91 AS3711_STEPUP_CONTROL_2
, 1, 0);
93 ret
= regmap_update_bits(as3711
->regmap
,
94 AS3711_STEPUP_CONTROL_2
, 1, 1);
99 * Someone with less fragile or less expensive hardware could try to simplify
100 * the brightness adjustment procedure.
102 static int as3711_bl_update_status(struct backlight_device
*bl
)
104 struct as3711_bl_data
*data
= bl_get_data(bl
);
105 struct as3711_bl_supply
*supply
= to_supply(data
);
106 struct as3711
*as3711
= supply
->as3711
;
107 int brightness
= bl
->props
.brightness
;
110 dev_dbg(&bl
->dev
, "%s(): brightness %u, pwr %x, blank %x, state %x\n",
111 __func__
, bl
->props
.brightness
, bl
->props
.power
,
112 bl
->props
.fb_blank
, bl
->props
.state
);
114 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
115 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
116 bl
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
119 if (data
->type
== AS3711_BL_SU1
) {
120 ret
= as3711_set_brightness_v(as3711
, brightness
,
121 AS3711_STEPUP_CONTROL_1
);
123 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
125 switch (pdata
->su2_feedback
) {
126 case AS3711_SU2_VOLTAGE
:
127 ret
= as3711_set_brightness_v(as3711
, brightness
,
128 AS3711_STEPUP_CONTROL_2
);
130 case AS3711_SU2_CURR_AUTO
:
131 ret
= as3711_set_brightness_auto_i(data
, brightness
/ 4);
135 ret
= as3711_bl_su2_reset(supply
);
139 ret
= as3711_set_brightness_auto_i(data
, brightness
);
141 ret
= regmap_update_bits(as3711
->regmap
,
142 AS3711_STEPUP_CONTROL_2
, 1, 0);
145 /* Manual one current feedback pin below */
146 case AS3711_SU2_CURR1
:
147 ret
= regmap_write(as3711
->regmap
, AS3711_CURR1_VALUE
,
150 case AS3711_SU2_CURR2
:
151 ret
= regmap_write(as3711
->regmap
, AS3711_CURR2_VALUE
,
154 case AS3711_SU2_CURR3
:
155 ret
= regmap_write(as3711
->regmap
, AS3711_CURR3_VALUE
,
163 data
->brightness
= brightness
;
168 static int as3711_bl_get_brightness(struct backlight_device
*bl
)
170 struct as3711_bl_data
*data
= bl_get_data(bl
);
172 return data
->brightness
;
175 static const struct backlight_ops as3711_bl_ops
= {
176 .update_status
= as3711_bl_update_status
,
177 .get_brightness
= as3711_bl_get_brightness
,
180 static int as3711_bl_init_su2(struct as3711_bl_supply
*supply
)
182 struct as3711
*as3711
= supply
->as3711
;
183 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
187 dev_dbg(as3711
->dev
, "%s(): use %u\n", __func__
, pdata
->su2_feedback
);
190 ret
= regmap_write(as3711
->regmap
, AS3711_STEPUP_CONTROL_2
, 0);
194 switch (pdata
->su2_feedback
) {
195 case AS3711_SU2_VOLTAGE
:
196 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 0);
198 case AS3711_SU2_CURR1
:
200 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 1);
202 case AS3711_SU2_CURR2
:
204 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 2);
206 case AS3711_SU2_CURR3
:
208 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 3);
210 case AS3711_SU2_CURR_AUTO
:
211 if (pdata
->su2_auto_curr1
)
213 if (pdata
->su2_auto_curr2
)
215 if (pdata
->su2_auto_curr3
)
224 ret
= regmap_write(as3711
->regmap
, AS3711_CURR_CONTROL
, ctl
);
229 static int as3711_bl_register(struct platform_device
*pdev
,
230 unsigned int max_brightness
, struct as3711_bl_data
*su
)
232 struct backlight_properties props
= {.type
= BACKLIGHT_RAW
,};
233 struct backlight_device
*bl
;
235 /* max tuning I = 31uA for voltage- and 38250uA for current-feedback */
236 props
.max_brightness
= max_brightness
;
238 bl
= devm_backlight_device_register(&pdev
->dev
,
239 su
->type
== AS3711_BL_SU1
?
240 "as3711-su1" : "as3711-su2",
242 &as3711_bl_ops
, &props
);
244 dev_err(&pdev
->dev
, "failed to register backlight\n");
248 bl
->props
.brightness
= props
.max_brightness
;
250 backlight_update_status(bl
);
257 static int as3711_backlight_parse_dt(struct device
*dev
)
259 struct as3711_bl_pdata
*pdata
= dev_get_platdata(dev
);
260 struct device_node
*bl
, *fb
;
263 bl
= of_get_child_by_name(dev
->parent
->of_node
, "backlight");
265 dev_dbg(dev
, "backlight node not found\n");
269 fb
= of_parse_phandle(bl
, "su1-dev", 0);
273 pdata
->su1_fb
= true;
275 ret
= of_property_read_u32(bl
, "su1-max-uA", &pdata
->su1_max_uA
);
276 if (pdata
->su1_max_uA
<= 0)
282 fb
= of_parse_phandle(bl
, "su2-dev", 0);
288 pdata
->su2_fb
= true;
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
;
322 if (of_find_property(bl
, "su2-fbprot-lx-sd4", NULL
)) {
323 pdata
->su2_fbprot
= AS3711_SU2_LX_SD4
;
326 if (of_find_property(bl
, "su2-fbprot-gpio2", NULL
)) {
327 pdata
->su2_fbprot
= AS3711_SU2_GPIO2
;
330 if (of_find_property(bl
, "su2-fbprot-gpio3", NULL
)) {
331 pdata
->su2_fbprot
= AS3711_SU2_GPIO3
;
334 if (of_find_property(bl
, "su2-fbprot-gpio4", NULL
)) {
335 pdata
->su2_fbprot
= AS3711_SU2_GPIO4
;
344 if (of_find_property(bl
, "su2-auto-curr1", NULL
)) {
345 pdata
->su2_auto_curr1
= true;
348 if (of_find_property(bl
, "su2-auto-curr2", NULL
)) {
349 pdata
->su2_auto_curr2
= true;
352 if (of_find_property(bl
, "su2-auto-curr3", NULL
)) {
353 pdata
->su2_auto_curr3
= true;
358 * At least one su2-auto-curr* must be specified iff
359 * AS3711_SU2_CURR_AUTO is used
361 if (!count
^ (pdata
->su2_feedback
!= AS3711_SU2_CURR_AUTO
)) {
377 static int as3711_backlight_probe(struct platform_device
*pdev
)
379 struct as3711_bl_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
380 struct as3711
*as3711
= dev_get_drvdata(pdev
->dev
.parent
);
381 struct as3711_bl_supply
*supply
;
382 struct as3711_bl_data
*su
;
383 unsigned int max_brightness
;
387 dev_err(&pdev
->dev
, "No platform data, exiting...\n");
391 if (pdev
->dev
.parent
->of_node
) {
392 ret
= as3711_backlight_parse_dt(&pdev
->dev
);
394 dev_err(&pdev
->dev
, "DT parsing failed: %d\n", ret
);
399 if (!pdata
->su1_fb
&& !pdata
->su2_fb
) {
400 dev_err(&pdev
->dev
, "No framebuffer specified\n");
405 * Due to possible hardware damage I chose to block all modes,
406 * unsupported on my hardware. Anyone, wishing to use any of those modes
407 * will have to first review the code, then activate and test it.
410 pdata
->su2_fbprot
!= AS3711_SU2_GPIO4
||
411 pdata
->su2_feedback
!= AS3711_SU2_CURR_AUTO
) {
413 "Attention! An untested mode has been chosen!\n"
414 "Please, review the code, enable, test, and report success:-)\n");
418 supply
= devm_kzalloc(&pdev
->dev
, sizeof(*supply
), GFP_KERNEL
);
422 supply
->as3711
= as3711
;
423 supply
->pdata
= pdata
;
427 su
->type
= AS3711_BL_SU1
;
429 max_brightness
= min(pdata
->su1_max_uA
, 31);
430 ret
= as3711_bl_register(pdev
, max_brightness
, su
);
437 su
->type
= AS3711_BL_SU2
;
439 switch (pdata
->su2_fbprot
) {
440 case AS3711_SU2_GPIO2
:
441 case AS3711_SU2_GPIO3
:
442 case AS3711_SU2_GPIO4
:
443 case AS3711_SU2_LX_SD4
:
449 switch (pdata
->su2_feedback
) {
450 case AS3711_SU2_VOLTAGE
:
451 max_brightness
= min(pdata
->su2_max_uA
, 31);
453 case AS3711_SU2_CURR1
:
454 case AS3711_SU2_CURR2
:
455 case AS3711_SU2_CURR3
:
456 case AS3711_SU2_CURR_AUTO
:
457 max_brightness
= min(pdata
->su2_max_uA
/ 150, 255);
463 ret
= as3711_bl_init_su2(supply
);
467 ret
= as3711_bl_register(pdev
, max_brightness
, su
);
472 platform_set_drvdata(pdev
, supply
);
477 static struct platform_driver as3711_backlight_driver
= {
479 .name
= "as3711-backlight",
481 .probe
= as3711_backlight_probe
,
484 module_platform_driver(as3711_backlight_driver
);
486 MODULE_DESCRIPTION("Backlight Driver for AS3711 PMICs");
487 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de");
488 MODULE_LICENSE("GPL v2");
489 MODULE_ALIAS("platform:as3711-backlight");