83ccb3d940fae577413d5a5668031fe1fc44b602
[platform/kernel/linux-starfive.git] / drivers / video / backlight / sky81452-backlight.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sky81452-backlight.c SKY81452 backlight driver
4  *
5  * Copyright 2014 Skyworks Solutions Inc.
6  * Author : Gyungoh Yoo <jack.yoo@skyworksinc.com>
7  */
8
9 #include <linux/backlight.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19
20 /* registers */
21 #define SKY81452_REG0   0x00
22 #define SKY81452_REG1   0x01
23 #define SKY81452_REG2   0x02
24 #define SKY81452_REG4   0x04
25 #define SKY81452_REG5   0x05
26
27 /* bit mask */
28 #define SKY81452_CS     0xFF
29 #define SKY81452_EN     0x3F
30 #define SKY81452_IGPW   0x20
31 #define SKY81452_PWMMD  0x10
32 #define SKY81452_PHASE  0x08
33 #define SKY81452_ILIM   0x04
34 #define SKY81452_VSHRT  0x03
35 #define SKY81452_OCP    0x80
36 #define SKY81452_OTMP   0x40
37 #define SKY81452_SHRT   0x3F
38 #define SKY81452_OPN    0x3F
39
40 #define SKY81452_DEFAULT_NAME "lcd-backlight"
41 #define SKY81452_MAX_BRIGHTNESS (SKY81452_CS + 1)
42
43 /**
44  * struct sky81452_platform_data
45  * @name:       backlight driver name.
46                 If it is not defined, default name is lcd-backlight.
47  * @gpios_enable:GPIO descriptor which control EN pin
48  * @enable:     Enable mask for current sink channel 1, 2, 3, 4, 5 and 6.
49  * @ignore_pwm: true if DPWMI should be ignored.
50  * @dpwm_mode:  true is DPWM dimming mode, otherwise Analog dimming mode.
51  * @phase_shift:true is phase shift mode.
52  * @short_detecion_threshold:   It should be one of 4, 5, 6 and 7V.
53  * @boost_current_limit:        It should be one of 2300, 2750mA.
54  */
55 struct sky81452_bl_platform_data {
56         const char *name;
57         struct gpio_desc *gpiod_enable;
58         unsigned int enable;
59         bool ignore_pwm;
60         bool dpwm_mode;
61         bool phase_shift;
62         unsigned int short_detection_threshold;
63         unsigned int boost_current_limit;
64 };
65
66 #define CTZ(b) __builtin_ctz(b)
67
68 static int sky81452_bl_update_status(struct backlight_device *bd)
69 {
70         const struct sky81452_bl_platform_data *pdata =
71                         dev_get_platdata(bd->dev.parent);
72         const unsigned int brightness = (unsigned int)bd->props.brightness;
73         struct regmap *regmap = bl_get_data(bd);
74         int ret;
75
76         if (brightness > 0) {
77                 ret = regmap_write(regmap, SKY81452_REG0, brightness - 1);
78                 if (ret < 0)
79                         return ret;
80
81                 return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN,
82                                         pdata->enable << CTZ(SKY81452_EN));
83         }
84
85         return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN, 0);
86 }
87
88 static const struct backlight_ops sky81452_bl_ops = {
89         .update_status = sky81452_bl_update_status,
90 };
91
92 static ssize_t sky81452_bl_store_enable(struct device *dev,
93                 struct device_attribute *attr, const char *buf, size_t count)
94 {
95         struct regmap *regmap = bl_get_data(to_backlight_device(dev));
96         unsigned long value;
97         int ret;
98
99         ret = kstrtoul(buf, 16, &value);
100         if (ret < 0)
101                 return ret;
102
103         ret = regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN,
104                                         value << CTZ(SKY81452_EN));
105         if (ret < 0)
106                 return ret;
107
108         return count;
109 }
110
111 static ssize_t sky81452_bl_show_open_short(struct device *dev,
112                 struct device_attribute *attr, char *buf)
113 {
114         struct regmap *regmap = bl_get_data(to_backlight_device(dev));
115         unsigned int reg, value = 0;
116         char tmp[3];
117         int i, ret;
118
119         reg = !strcmp(attr->attr.name, "open") ? SKY81452_REG5 : SKY81452_REG4;
120         ret = regmap_read(regmap, reg, &value);
121         if (ret < 0)
122                 return ret;
123
124         if (value & SKY81452_SHRT) {
125                 *buf = 0;
126                 for (i = 0; i < 6; i++) {
127                         if (value & 0x01) {
128                                 sprintf(tmp, "%d ", i + 1);
129                                 strcat(buf, tmp);
130                         }
131                         value >>= 1;
132                 }
133                 strcat(buf, "\n");
134         } else {
135                 strcpy(buf, "none\n");
136         }
137
138         return strlen(buf);
139 }
140
141 static ssize_t sky81452_bl_show_fault(struct device *dev,
142                 struct device_attribute *attr, char *buf)
143 {
144         struct regmap *regmap = bl_get_data(to_backlight_device(dev));
145         unsigned int value = 0;
146         int ret;
147
148         ret = regmap_read(regmap, SKY81452_REG4, &value);
149         if (ret < 0)
150                 return ret;
151
152         *buf = 0;
153
154         if (value & SKY81452_OCP)
155                 strcat(buf, "over-current ");
156
157         if (value & SKY81452_OTMP)
158                 strcat(buf, "over-temperature");
159
160         strcat(buf, "\n");
161         return strlen(buf);
162 }
163
164 static DEVICE_ATTR(enable, S_IWGRP | S_IWUSR, NULL, sky81452_bl_store_enable);
165 static DEVICE_ATTR(open, S_IRUGO, sky81452_bl_show_open_short, NULL);
166 static DEVICE_ATTR(short, S_IRUGO, sky81452_bl_show_open_short, NULL);
167 static DEVICE_ATTR(fault, S_IRUGO, sky81452_bl_show_fault, NULL);
168
169 static struct attribute *sky81452_bl_attribute[] = {
170         &dev_attr_enable.attr,
171         &dev_attr_open.attr,
172         &dev_attr_short.attr,
173         &dev_attr_fault.attr,
174         NULL
175 };
176
177 static const struct attribute_group sky81452_bl_attr_group = {
178         .attrs = sky81452_bl_attribute,
179 };
180
181 #ifdef CONFIG_OF
182 static struct sky81452_bl_platform_data *sky81452_bl_parse_dt(
183                                                         struct device *dev)
184 {
185         struct device_node *np = of_node_get(dev->of_node);
186         struct sky81452_bl_platform_data *pdata;
187         int num_entry;
188         unsigned int sources[6];
189         int ret;
190
191         if (!np) {
192                 dev_err(dev, "backlight node not found.\n");
193                 return ERR_PTR(-ENODATA);
194         }
195
196         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
197         if (!pdata) {
198                 of_node_put(np);
199                 return ERR_PTR(-ENOMEM);
200         }
201
202         of_property_read_string(np, "name", &pdata->name);
203         pdata->ignore_pwm = of_property_read_bool(np, "skyworks,ignore-pwm");
204         pdata->dpwm_mode = of_property_read_bool(np, "skyworks,dpwm-mode");
205         pdata->phase_shift = of_property_read_bool(np, "skyworks,phase-shift");
206         pdata->gpiod_enable = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
207
208         ret = of_property_count_u32_elems(np, "led-sources");
209         if (ret < 0) {
210                 pdata->enable = SKY81452_EN >> CTZ(SKY81452_EN);
211         } else {
212                 num_entry = ret;
213                 if (num_entry > 6)
214                         num_entry = 6;
215
216                 ret = of_property_read_u32_array(np, "led-sources", sources,
217                                         num_entry);
218                 if (ret < 0) {
219                         dev_err(dev, "led-sources node is invalid.\n");
220                         return ERR_PTR(-EINVAL);
221                 }
222
223                 pdata->enable = 0;
224                 while (--num_entry)
225                         pdata->enable |= (1 << sources[num_entry]);
226         }
227
228         ret = of_property_read_u32(np,
229                         "skyworks,short-detection-threshold-volt",
230                         &pdata->short_detection_threshold);
231         if (ret < 0)
232                 pdata->short_detection_threshold = 7;
233
234         ret = of_property_read_u32(np, "skyworks,current-limit-mA",
235                         &pdata->boost_current_limit);
236         if (ret < 0)
237                 pdata->boost_current_limit = 2750;
238
239         of_node_put(np);
240         return pdata;
241 }
242 #else
243 static struct sky81452_bl_platform_data *sky81452_bl_parse_dt(
244                                                         struct device *dev)
245 {
246         return ERR_PTR(-EINVAL);
247 }
248 #endif
249
250 static int sky81452_bl_init_device(struct regmap *regmap,
251                 struct sky81452_bl_platform_data *pdata)
252 {
253         unsigned int value;
254
255         value = pdata->ignore_pwm ? SKY81452_IGPW : 0;
256         value |= pdata->dpwm_mode ? SKY81452_PWMMD : 0;
257         value |= pdata->phase_shift ? 0 : SKY81452_PHASE;
258
259         if (pdata->boost_current_limit == 2300)
260                 value |= SKY81452_ILIM;
261         else if (pdata->boost_current_limit != 2750)
262                 return -EINVAL;
263
264         if (pdata->short_detection_threshold < 4 ||
265                                 pdata->short_detection_threshold > 7)
266                 return -EINVAL;
267         value |= (7 - pdata->short_detection_threshold) << CTZ(SKY81452_VSHRT);
268
269         return regmap_write(regmap, SKY81452_REG2, value);
270 }
271
272 static int sky81452_bl_probe(struct platform_device *pdev)
273 {
274         struct device *dev = &pdev->dev;
275         struct regmap *regmap = dev_get_drvdata(dev->parent);
276         struct sky81452_bl_platform_data *pdata;
277         struct backlight_device *bd;
278         struct backlight_properties props;
279         const char *name;
280         int ret;
281
282         pdata = sky81452_bl_parse_dt(dev);
283         if (IS_ERR(pdata))
284                 return PTR_ERR(pdata);
285
286         ret = sky81452_bl_init_device(regmap, pdata);
287         if (ret < 0) {
288                 dev_err(dev, "failed to initialize. err=%d\n", ret);
289                 return ret;
290         }
291
292         memset(&props, 0, sizeof(props));
293         props.max_brightness = SKY81452_MAX_BRIGHTNESS,
294         name = pdata->name ? pdata->name : SKY81452_DEFAULT_NAME;
295         bd = devm_backlight_device_register(dev, name, dev, regmap,
296                                                 &sky81452_bl_ops, &props);
297         if (IS_ERR(bd)) {
298                 dev_err(dev, "failed to register. err=%ld\n", PTR_ERR(bd));
299                 return PTR_ERR(bd);
300         }
301
302         platform_set_drvdata(pdev, bd);
303
304         ret = sysfs_create_group(&bd->dev.kobj, &sky81452_bl_attr_group);
305         if (ret < 0) {
306                 dev_err(dev, "failed to create attribute. err=%d\n", ret);
307                 return ret;
308         }
309
310         return ret;
311 }
312
313 static int sky81452_bl_remove(struct platform_device *pdev)
314 {
315         const struct sky81452_bl_platform_data *pdata =
316                                                 dev_get_platdata(&pdev->dev);
317         struct backlight_device *bd = platform_get_drvdata(pdev);
318
319         sysfs_remove_group(&bd->dev.kobj, &sky81452_bl_attr_group);
320
321         bd->props.power = FB_BLANK_UNBLANK;
322         bd->props.brightness = 0;
323         backlight_update_status(bd);
324
325         if (pdata->gpiod_enable)
326                 gpiod_set_value_cansleep(pdata->gpiod_enable, 0);
327
328         return 0;
329 }
330
331 #ifdef CONFIG_OF
332 static const struct of_device_id sky81452_bl_of_match[] = {
333         { .compatible = "skyworks,sky81452-backlight", },
334         { }
335 };
336 MODULE_DEVICE_TABLE(of, sky81452_bl_of_match);
337 #endif
338
339 static struct platform_driver sky81452_bl_driver = {
340         .driver = {
341                 .name = "sky81452-backlight",
342                 .of_match_table = of_match_ptr(sky81452_bl_of_match),
343         },
344         .probe = sky81452_bl_probe,
345         .remove = sky81452_bl_remove,
346 };
347
348 module_platform_driver(sky81452_bl_driver);
349
350 MODULE_DESCRIPTION("Skyworks SKY81452 backlight driver");
351 MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@skyworksinc.com>");
352 MODULE_LICENSE("GPL v2");