WIP FPC-III support
[linux/fpc-iii.git] / drivers / video / backlight / da9052_bl.c
blob882359dd288c06743a50fbd49c30fbe73666d093
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Backlight Driver for Dialog DA9052 PMICs
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
7 * Author: David Dajun Chen <dchen@diasemi.com>
8 */
10 #include <linux/backlight.h>
11 #include <linux/delay.h>
12 #include <linux/fb.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
16 #include <linux/mfd/da9052/da9052.h>
17 #include <linux/mfd/da9052/reg.h>
19 #define DA9052_MAX_BRIGHTNESS 0xFF
21 enum {
22 DA9052_WLEDS_OFF,
23 DA9052_WLEDS_ON,
26 enum {
27 DA9052_TYPE_WLED1,
28 DA9052_TYPE_WLED2,
29 DA9052_TYPE_WLED3,
32 static const unsigned char wled_bank[] = {
33 DA9052_LED1_CONF_REG,
34 DA9052_LED2_CONF_REG,
35 DA9052_LED3_CONF_REG,
38 struct da9052_bl {
39 struct da9052 *da9052;
40 uint brightness;
41 uint state;
42 uint led_reg;
45 static int da9052_adjust_wled_brightness(struct da9052_bl *wleds)
47 unsigned char boost_en;
48 unsigned char i_sink;
49 int ret;
51 boost_en = 0x3F;
52 i_sink = 0xFF;
53 if (wleds->state == DA9052_WLEDS_OFF) {
54 boost_en = 0x00;
55 i_sink = 0x00;
58 ret = da9052_reg_write(wleds->da9052, DA9052_BOOST_REG, boost_en);
59 if (ret < 0)
60 return ret;
62 ret = da9052_reg_write(wleds->da9052, DA9052_LED_CONT_REG, i_sink);
63 if (ret < 0)
64 return ret;
66 ret = da9052_reg_write(wleds->da9052, wled_bank[wleds->led_reg], 0x0);
67 if (ret < 0)
68 return ret;
70 usleep_range(10000, 11000);
72 if (wleds->brightness) {
73 ret = da9052_reg_write(wleds->da9052, wled_bank[wleds->led_reg],
74 wleds->brightness);
75 if (ret < 0)
76 return ret;
79 return 0;
82 static int da9052_backlight_update_status(struct backlight_device *bl)
84 int brightness = bl->props.brightness;
85 struct da9052_bl *wleds = bl_get_data(bl);
87 wleds->brightness = brightness;
88 wleds->state = DA9052_WLEDS_ON;
90 return da9052_adjust_wled_brightness(wleds);
93 static int da9052_backlight_get_brightness(struct backlight_device *bl)
95 struct da9052_bl *wleds = bl_get_data(bl);
97 return wleds->brightness;
100 static const struct backlight_ops da9052_backlight_ops = {
101 .update_status = da9052_backlight_update_status,
102 .get_brightness = da9052_backlight_get_brightness,
105 static int da9052_backlight_probe(struct platform_device *pdev)
107 struct backlight_device *bl;
108 struct backlight_properties props;
109 struct da9052_bl *wleds;
111 wleds = devm_kzalloc(&pdev->dev, sizeof(struct da9052_bl), GFP_KERNEL);
112 if (!wleds)
113 return -ENOMEM;
115 wleds->da9052 = dev_get_drvdata(pdev->dev.parent);
116 wleds->brightness = 0;
117 wleds->led_reg = platform_get_device_id(pdev)->driver_data;
118 wleds->state = DA9052_WLEDS_OFF;
120 props.type = BACKLIGHT_RAW;
121 props.max_brightness = DA9052_MAX_BRIGHTNESS;
123 bl = devm_backlight_device_register(&pdev->dev, pdev->name,
124 wleds->da9052->dev, wleds,
125 &da9052_backlight_ops, &props);
126 if (IS_ERR(bl)) {
127 dev_err(&pdev->dev, "Failed to register backlight\n");
128 return PTR_ERR(bl);
131 bl->props.max_brightness = DA9052_MAX_BRIGHTNESS;
132 bl->props.brightness = 0;
133 platform_set_drvdata(pdev, bl);
135 return da9052_adjust_wled_brightness(wleds);
138 static int da9052_backlight_remove(struct platform_device *pdev)
140 struct backlight_device *bl = platform_get_drvdata(pdev);
141 struct da9052_bl *wleds = bl_get_data(bl);
143 wleds->brightness = 0;
144 wleds->state = DA9052_WLEDS_OFF;
145 da9052_adjust_wled_brightness(wleds);
147 return 0;
150 static const struct platform_device_id da9052_wled_ids[] = {
152 .name = "da9052-wled1",
153 .driver_data = DA9052_TYPE_WLED1,
156 .name = "da9052-wled2",
157 .driver_data = DA9052_TYPE_WLED2,
160 .name = "da9052-wled3",
161 .driver_data = DA9052_TYPE_WLED3,
163 { },
165 MODULE_DEVICE_TABLE(platform, da9052_wled_ids);
167 static struct platform_driver da9052_wled_driver = {
168 .probe = da9052_backlight_probe,
169 .remove = da9052_backlight_remove,
170 .id_table = da9052_wled_ids,
171 .driver = {
172 .name = "da9052-wled",
176 module_platform_driver(da9052_wled_driver);
178 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
179 MODULE_DESCRIPTION("Backlight driver for DA9052 PMIC");
180 MODULE_LICENSE("GPL");