WIP FPC-III support
[linux/fpc-iii.git] / drivers / video / backlight / as3711_bl.c
blob3b60019cdc2bbac1b0c5fd123c6f6e42d016e0dd
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
7 */
9 #include <linux/backlight.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/fb.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>
21 enum as3711_bl_type {
22 AS3711_BL_SU1,
23 AS3711_BL_SU2,
26 struct as3711_bl_data {
27 bool powered;
28 enum as3711_bl_type type;
29 int brightness;
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)
42 switch (su->type) {
43 case AS3711_BL_SU1:
44 return container_of(su, struct as3711_bl_supply, su1);
45 case AS3711_BL_SU2:
46 return container_of(su, struct as3711_bl_supply, su2);
48 return NULL;
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;
57 int ret = 0;
59 /* Only all equal current values are supported */
60 if (pdata->su2_auto_curr1)
61 ret = regmap_write(as3711->regmap, AS3711_CURR1_VALUE,
62 brightness);
63 if (!ret && pdata->su2_auto_curr2)
64 ret = regmap_write(as3711->regmap, AS3711_CURR2_VALUE,
65 brightness);
66 if (!ret && pdata->su2_auto_curr3)
67 ret = regmap_write(as3711->regmap, AS3711_CURR3_VALUE,
68 brightness);
70 return ret;
73 static int as3711_set_brightness_v(struct as3711 *as3711,
74 unsigned int brightness,
75 unsigned int reg)
77 if (brightness > 31)
78 return -EINVAL;
80 return regmap_update_bits(as3711->regmap, reg, 0xf0,
81 brightness << 4);
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);
89 if (!ret)
90 ret = regmap_update_bits(as3711->regmap,
91 AS3711_STEPUP_CONTROL_2, 1, 0);
92 if (!ret)
93 ret = regmap_update_bits(as3711->regmap,
94 AS3711_STEPUP_CONTROL_2, 1, 1);
95 return ret;
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;
108 int ret = 0;
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);
115 } else {
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);
122 break;
123 case AS3711_SU2_CURR_AUTO:
124 ret = as3711_set_brightness_auto_i(data, brightness / 4);
125 if (ret < 0)
126 return ret;
127 if (brightness) {
128 ret = as3711_bl_su2_reset(supply);
129 if (ret < 0)
130 return ret;
131 udelay(500);
132 ret = as3711_set_brightness_auto_i(data, brightness);
133 } else {
134 ret = regmap_update_bits(as3711->regmap,
135 AS3711_STEPUP_CONTROL_2, 1, 0);
137 break;
138 /* Manual one current feedback pin below */
139 case AS3711_SU2_CURR1:
140 ret = regmap_write(as3711->regmap, AS3711_CURR1_VALUE,
141 brightness);
142 break;
143 case AS3711_SU2_CURR2:
144 ret = regmap_write(as3711->regmap, AS3711_CURR2_VALUE,
145 brightness);
146 break;
147 case AS3711_SU2_CURR3:
148 ret = regmap_write(as3711->regmap, AS3711_CURR3_VALUE,
149 brightness);
150 break;
151 default:
152 ret = -EINVAL;
155 if (!ret)
156 data->brightness = brightness;
158 return ret;
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;
177 u8 ctl = 0;
178 int ret;
180 dev_dbg(as3711->dev, "%s(): use %u\n", __func__, pdata->su2_feedback);
182 /* Turn SU2 off */
183 ret = regmap_write(as3711->regmap, AS3711_STEPUP_CONTROL_2, 0);
184 if (ret < 0)
185 return ret;
187 switch (pdata->su2_feedback) {
188 case AS3711_SU2_VOLTAGE:
189 ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 0);
190 break;
191 case AS3711_SU2_CURR1:
192 ctl = 1;
193 ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 1);
194 break;
195 case AS3711_SU2_CURR2:
196 ctl = 4;
197 ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 2);
198 break;
199 case AS3711_SU2_CURR3:
200 ctl = 0x10;
201 ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 3);
202 break;
203 case AS3711_SU2_CURR_AUTO:
204 if (pdata->su2_auto_curr1)
205 ctl = 2;
206 if (pdata->su2_auto_curr2)
207 ctl |= 8;
208 if (pdata->su2_auto_curr3)
209 ctl |= 0x20;
210 ret = 0;
211 break;
212 default:
213 return -EINVAL;
216 if (!ret)
217 ret = regmap_write(as3711->regmap, AS3711_CURR_CONTROL, ctl);
219 return ret;
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",
234 &pdev->dev, su,
235 &as3711_bl_ops, &props);
236 if (IS_ERR(bl)) {
237 dev_err(&pdev->dev, "failed to register backlight\n");
238 return PTR_ERR(bl);
241 bl->props.brightness = props.max_brightness;
243 backlight_update_status(bl);
245 su->bl = bl;
247 return 0;
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;
254 int ret;
256 bl = of_get_child_by_name(dev->parent->of_node, "backlight");
257 if (!bl) {
258 dev_dbg(dev, "backlight node not found\n");
259 return -ENODEV;
262 fb = of_parse_phandle(bl, "su1-dev", 0);
263 if (fb) {
264 of_node_put(fb);
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)
270 ret = -EINVAL;
271 if (ret < 0)
272 goto err_put_bl;
275 fb = of_parse_phandle(bl, "su2-dev", 0);
276 if (fb) {
277 int count = 0;
279 of_node_put(fb);
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)
285 ret = -EINVAL;
286 if (ret < 0)
287 goto err_put_bl;
289 if (of_find_property(bl, "su2-feedback-voltage", NULL)) {
290 pdata->su2_feedback = AS3711_SU2_VOLTAGE;
291 count++;
293 if (of_find_property(bl, "su2-feedback-curr1", NULL)) {
294 pdata->su2_feedback = AS3711_SU2_CURR1;
295 count++;
297 if (of_find_property(bl, "su2-feedback-curr2", NULL)) {
298 pdata->su2_feedback = AS3711_SU2_CURR2;
299 count++;
301 if (of_find_property(bl, "su2-feedback-curr3", NULL)) {
302 pdata->su2_feedback = AS3711_SU2_CURR3;
303 count++;
305 if (of_find_property(bl, "su2-feedback-curr-auto", NULL)) {
306 pdata->su2_feedback = AS3711_SU2_CURR_AUTO;
307 count++;
309 if (count != 1) {
310 ret = -EINVAL;
311 goto err_put_bl;
314 count = 0;
315 if (of_find_property(bl, "su2-fbprot-lx-sd4", NULL)) {
316 pdata->su2_fbprot = AS3711_SU2_LX_SD4;
317 count++;
319 if (of_find_property(bl, "su2-fbprot-gpio2", NULL)) {
320 pdata->su2_fbprot = AS3711_SU2_GPIO2;
321 count++;
323 if (of_find_property(bl, "su2-fbprot-gpio3", NULL)) {
324 pdata->su2_fbprot = AS3711_SU2_GPIO3;
325 count++;
327 if (of_find_property(bl, "su2-fbprot-gpio4", NULL)) {
328 pdata->su2_fbprot = AS3711_SU2_GPIO4;
329 count++;
331 if (count != 1) {
332 ret = -EINVAL;
333 goto err_put_bl;
336 count = 0;
337 if (of_find_property(bl, "su2-auto-curr1", NULL)) {
338 pdata->su2_auto_curr1 = true;
339 count++;
341 if (of_find_property(bl, "su2-auto-curr2", NULL)) {
342 pdata->su2_auto_curr2 = true;
343 count++;
345 if (of_find_property(bl, "su2-auto-curr3", NULL)) {
346 pdata->su2_auto_curr3 = true;
347 count++;
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)) {
355 ret = -EINVAL;
356 goto err_put_bl;
360 of_node_put(bl);
362 return 0;
364 err_put_bl:
365 of_node_put(bl);
367 return ret;
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;
377 int ret;
379 if (!pdata) {
380 dev_err(&pdev->dev, "No platform data, exiting...\n");
381 return -ENODEV;
384 if (pdev->dev.parent->of_node) {
385 ret = as3711_backlight_parse_dt(&pdev->dev);
386 if (ret < 0) {
387 dev_err(&pdev->dev, "DT parsing failed: %d\n", ret);
388 return ret;
392 if (!pdata->su1_fb && !pdata->su2_fb) {
393 dev_err(&pdev->dev, "No framebuffer specified\n");
394 return -EINVAL;
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.
402 if (pdata->su1_fb ||
403 pdata->su2_fbprot != AS3711_SU2_GPIO4 ||
404 pdata->su2_feedback != AS3711_SU2_CURR_AUTO) {
405 dev_warn(&pdev->dev,
406 "Attention! An untested mode has been chosen!\n"
407 "Please, review the code, enable, test, and report success:-)\n");
408 return -EINVAL;
411 supply = devm_kzalloc(&pdev->dev, sizeof(*supply), GFP_KERNEL);
412 if (!supply)
413 return -ENOMEM;
415 supply->as3711 = as3711;
416 supply->pdata = pdata;
418 if (pdata->su1_fb) {
419 su = &supply->su1;
420 su->type = AS3711_BL_SU1;
422 max_brightness = min(pdata->su1_max_uA, 31);
423 ret = as3711_bl_register(pdev, max_brightness, su);
424 if (ret < 0)
425 return ret;
428 if (pdata->su2_fb) {
429 su = &supply->su2;
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:
437 break;
438 default:
439 return -EINVAL;
442 switch (pdata->su2_feedback) {
443 case AS3711_SU2_VOLTAGE:
444 max_brightness = min(pdata->su2_max_uA, 31);
445 break;
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);
451 break;
452 default:
453 return -EINVAL;
456 ret = as3711_bl_init_su2(supply);
457 if (ret < 0)
458 return ret;
460 ret = as3711_bl_register(pdev, max_brightness, su);
461 if (ret < 0)
462 return ret;
465 platform_set_drvdata(pdev, supply);
467 return 0;
470 static struct platform_driver as3711_backlight_driver = {
471 .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");