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
;
110 brightness
= backlight_get_brightness(bl
);
112 if (data
->type
== AS3711_BL_SU1
) {
113 ret
= as3711_set_brightness_v(as3711
, brightness
,
114 AS3711_STEPUP_CONTROL_1
);
116 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
118 switch (pdata
->su2_feedback
) {
119 case AS3711_SU2_VOLTAGE
:
120 ret
= as3711_set_brightness_v(as3711
, brightness
,
121 AS3711_STEPUP_CONTROL_2
);
123 case AS3711_SU2_CURR_AUTO
:
124 ret
= as3711_set_brightness_auto_i(data
, brightness
/ 4);
128 ret
= as3711_bl_su2_reset(supply
);
132 ret
= as3711_set_brightness_auto_i(data
, brightness
);
134 ret
= regmap_update_bits(as3711
->regmap
,
135 AS3711_STEPUP_CONTROL_2
, 1, 0);
138 /* Manual one current feedback pin below */
139 case AS3711_SU2_CURR1
:
140 ret
= regmap_write(as3711
->regmap
, AS3711_CURR1_VALUE
,
143 case AS3711_SU2_CURR2
:
144 ret
= regmap_write(as3711
->regmap
, AS3711_CURR2_VALUE
,
147 case AS3711_SU2_CURR3
:
148 ret
= regmap_write(as3711
->regmap
, AS3711_CURR3_VALUE
,
156 data
->brightness
= brightness
;
161 static int as3711_bl_get_brightness(struct backlight_device
*bl
)
163 struct as3711_bl_data
*data
= bl_get_data(bl
);
165 return data
->brightness
;
168 static const struct backlight_ops as3711_bl_ops
= {
169 .update_status
= as3711_bl_update_status
,
170 .get_brightness
= as3711_bl_get_brightness
,
173 static int as3711_bl_init_su2(struct as3711_bl_supply
*supply
)
175 struct as3711
*as3711
= supply
->as3711
;
176 const struct as3711_bl_pdata
*pdata
= supply
->pdata
;
180 dev_dbg(as3711
->dev
, "%s(): use %u\n", __func__
, pdata
->su2_feedback
);
183 ret
= regmap_write(as3711
->regmap
, AS3711_STEPUP_CONTROL_2
, 0);
187 switch (pdata
->su2_feedback
) {
188 case AS3711_SU2_VOLTAGE
:
189 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 0);
191 case AS3711_SU2_CURR1
:
193 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 1);
195 case AS3711_SU2_CURR2
:
197 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 2);
199 case AS3711_SU2_CURR3
:
201 ret
= regmap_update_bits(as3711
->regmap
, AS3711_STEPUP_CONTROL_4
, 3, 3);
203 case AS3711_SU2_CURR_AUTO
:
204 if (pdata
->su2_auto_curr1
)
206 if (pdata
->su2_auto_curr2
)
208 if (pdata
->su2_auto_curr3
)
217 ret
= regmap_write(as3711
->regmap
, AS3711_CURR_CONTROL
, ctl
);
222 static int as3711_bl_register(struct platform_device
*pdev
,
223 unsigned int max_brightness
, struct as3711_bl_data
*su
)
225 struct backlight_properties props
= {.type
= BACKLIGHT_RAW
,};
226 struct backlight_device
*bl
;
228 /* max tuning I = 31uA for voltage- and 38250uA for current-feedback */
229 props
.max_brightness
= max_brightness
;
231 bl
= devm_backlight_device_register(&pdev
->dev
,
232 su
->type
== AS3711_BL_SU1
?
233 "as3711-su1" : "as3711-su2",
235 &as3711_bl_ops
, &props
);
237 dev_err(&pdev
->dev
, "failed to register backlight\n");
241 bl
->props
.brightness
= props
.max_brightness
;
243 backlight_update_status(bl
);
250 static int as3711_backlight_parse_dt(struct device
*dev
)
252 struct as3711_bl_pdata
*pdata
= dev_get_platdata(dev
);
253 struct device_node
*bl
, *fb
;
256 bl
= of_get_child_by_name(dev
->parent
->of_node
, "backlight");
258 dev_dbg(dev
, "backlight node not found\n");
262 fb
= of_parse_phandle(bl
, "su1-dev", 0);
266 pdata
->su1_fb
= true;
268 ret
= of_property_read_u32(bl
, "su1-max-uA", &pdata
->su1_max_uA
);
269 if (pdata
->su1_max_uA
<= 0)
275 fb
= of_parse_phandle(bl
, "su2-dev", 0);
281 pdata
->su2_fb
= true;
283 ret
= of_property_read_u32(bl
, "su2-max-uA", &pdata
->su2_max_uA
);
284 if (pdata
->su2_max_uA
<= 0)
289 if (of_find_property(bl
, "su2-feedback-voltage", NULL
)) {
290 pdata
->su2_feedback
= AS3711_SU2_VOLTAGE
;
293 if (of_find_property(bl
, "su2-feedback-curr1", NULL
)) {
294 pdata
->su2_feedback
= AS3711_SU2_CURR1
;
297 if (of_find_property(bl
, "su2-feedback-curr2", NULL
)) {
298 pdata
->su2_feedback
= AS3711_SU2_CURR2
;
301 if (of_find_property(bl
, "su2-feedback-curr3", NULL
)) {
302 pdata
->su2_feedback
= AS3711_SU2_CURR3
;
305 if (of_find_property(bl
, "su2-feedback-curr-auto", NULL
)) {
306 pdata
->su2_feedback
= AS3711_SU2_CURR_AUTO
;
315 if (of_find_property(bl
, "su2-fbprot-lx-sd4", NULL
)) {
316 pdata
->su2_fbprot
= AS3711_SU2_LX_SD4
;
319 if (of_find_property(bl
, "su2-fbprot-gpio2", NULL
)) {
320 pdata
->su2_fbprot
= AS3711_SU2_GPIO2
;
323 if (of_find_property(bl
, "su2-fbprot-gpio3", NULL
)) {
324 pdata
->su2_fbprot
= AS3711_SU2_GPIO3
;
327 if (of_find_property(bl
, "su2-fbprot-gpio4", NULL
)) {
328 pdata
->su2_fbprot
= AS3711_SU2_GPIO4
;
337 if (of_find_property(bl
, "su2-auto-curr1", NULL
)) {
338 pdata
->su2_auto_curr1
= true;
341 if (of_find_property(bl
, "su2-auto-curr2", NULL
)) {
342 pdata
->su2_auto_curr2
= true;
345 if (of_find_property(bl
, "su2-auto-curr3", NULL
)) {
346 pdata
->su2_auto_curr3
= true;
351 * At least one su2-auto-curr* must be specified iff
352 * AS3711_SU2_CURR_AUTO is used
354 if (!count
^ (pdata
->su2_feedback
!= AS3711_SU2_CURR_AUTO
)) {
370 static int as3711_backlight_probe(struct platform_device
*pdev
)
372 struct as3711_bl_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
373 struct as3711
*as3711
= dev_get_drvdata(pdev
->dev
.parent
);
374 struct as3711_bl_supply
*supply
;
375 struct as3711_bl_data
*su
;
376 unsigned int max_brightness
;
380 dev_err(&pdev
->dev
, "No platform data, exiting...\n");
384 if (pdev
->dev
.parent
->of_node
) {
385 ret
= as3711_backlight_parse_dt(&pdev
->dev
);
387 dev_err(&pdev
->dev
, "DT parsing failed: %d\n", ret
);
392 if (!pdata
->su1_fb
&& !pdata
->su2_fb
) {
393 dev_err(&pdev
->dev
, "No framebuffer specified\n");
398 * Due to possible hardware damage I chose to block all modes,
399 * unsupported on my hardware. Anyone, wishing to use any of those modes
400 * will have to first review the code, then activate and test it.
403 pdata
->su2_fbprot
!= AS3711_SU2_GPIO4
||
404 pdata
->su2_feedback
!= AS3711_SU2_CURR_AUTO
) {
406 "Attention! An untested mode has been chosen!\n"
407 "Please, review the code, enable, test, and report success:-)\n");
411 supply
= devm_kzalloc(&pdev
->dev
, sizeof(*supply
), GFP_KERNEL
);
415 supply
->as3711
= as3711
;
416 supply
->pdata
= pdata
;
420 su
->type
= AS3711_BL_SU1
;
422 max_brightness
= min(pdata
->su1_max_uA
, 31);
423 ret
= as3711_bl_register(pdev
, max_brightness
, su
);
430 su
->type
= AS3711_BL_SU2
;
432 switch (pdata
->su2_fbprot
) {
433 case AS3711_SU2_GPIO2
:
434 case AS3711_SU2_GPIO3
:
435 case AS3711_SU2_GPIO4
:
436 case AS3711_SU2_LX_SD4
:
442 switch (pdata
->su2_feedback
) {
443 case AS3711_SU2_VOLTAGE
:
444 max_brightness
= min(pdata
->su2_max_uA
, 31);
446 case AS3711_SU2_CURR1
:
447 case AS3711_SU2_CURR2
:
448 case AS3711_SU2_CURR3
:
449 case AS3711_SU2_CURR_AUTO
:
450 max_brightness
= min(pdata
->su2_max_uA
/ 150, 255);
456 ret
= as3711_bl_init_su2(supply
);
460 ret
= as3711_bl_register(pdev
, max_brightness
, su
);
465 platform_set_drvdata(pdev
, supply
);
470 static struct platform_driver as3711_backlight_driver
= {
472 .name
= "as3711-backlight",
474 .probe
= as3711_backlight_probe
,
477 module_platform_driver(as3711_backlight_driver
);
479 MODULE_DESCRIPTION("Backlight Driver for AS3711 PMICs");
480 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de");
481 MODULE_LICENSE("GPL v2");
482 MODULE_ALIAS("platform:as3711-backlight");