treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / video / backlight / sky81452-backlight.c
blob2355f00f577327730221e488abeb742a632a6dea
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * sky81452-backlight.c SKY81452 backlight driver
5 * Copyright 2014 Skyworks Solutions Inc.
6 * Author : Gyungoh Yoo <jack.yoo@skyworksinc.com>
7 */
9 #include <linux/backlight.h>
10 #include <linux/err.h>
11 #include <linux/gpio.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_gpio.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/platform_data/sky81452-backlight.h>
20 #include <linux/slab.h>
22 /* registers */
23 #define SKY81452_REG0 0x00
24 #define SKY81452_REG1 0x01
25 #define SKY81452_REG2 0x02
26 #define SKY81452_REG4 0x04
27 #define SKY81452_REG5 0x05
29 /* bit mask */
30 #define SKY81452_CS 0xFF
31 #define SKY81452_EN 0x3F
32 #define SKY81452_IGPW 0x20
33 #define SKY81452_PWMMD 0x10
34 #define SKY81452_PHASE 0x08
35 #define SKY81452_ILIM 0x04
36 #define SKY81452_VSHRT 0x03
37 #define SKY81452_OCP 0x80
38 #define SKY81452_OTMP 0x40
39 #define SKY81452_SHRT 0x3F
40 #define SKY81452_OPN 0x3F
42 #define SKY81452_DEFAULT_NAME "lcd-backlight"
43 #define SKY81452_MAX_BRIGHTNESS (SKY81452_CS + 1)
45 #define CTZ(b) __builtin_ctz(b)
47 static int sky81452_bl_update_status(struct backlight_device *bd)
49 const struct sky81452_bl_platform_data *pdata =
50 dev_get_platdata(bd->dev.parent);
51 const unsigned int brightness = (unsigned int)bd->props.brightness;
52 struct regmap *regmap = bl_get_data(bd);
53 int ret;
55 if (brightness > 0) {
56 ret = regmap_write(regmap, SKY81452_REG0, brightness - 1);
57 if (ret < 0)
58 return ret;
60 return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN,
61 pdata->enable << CTZ(SKY81452_EN));
64 return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN, 0);
67 static const struct backlight_ops sky81452_bl_ops = {
68 .update_status = sky81452_bl_update_status,
71 static ssize_t sky81452_bl_store_enable(struct device *dev,
72 struct device_attribute *attr, const char *buf, size_t count)
74 struct regmap *regmap = bl_get_data(to_backlight_device(dev));
75 unsigned long value;
76 int ret;
78 ret = kstrtoul(buf, 16, &value);
79 if (ret < 0)
80 return ret;
82 ret = regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN,
83 value << CTZ(SKY81452_EN));
84 if (ret < 0)
85 return ret;
87 return count;
90 static ssize_t sky81452_bl_show_open_short(struct device *dev,
91 struct device_attribute *attr, char *buf)
93 struct regmap *regmap = bl_get_data(to_backlight_device(dev));
94 unsigned int reg, value = 0;
95 char tmp[3];
96 int i, ret;
98 reg = !strcmp(attr->attr.name, "open") ? SKY81452_REG5 : SKY81452_REG4;
99 ret = regmap_read(regmap, reg, &value);
100 if (ret < 0)
101 return ret;
103 if (value & SKY81452_SHRT) {
104 *buf = 0;
105 for (i = 0; i < 6; i++) {
106 if (value & 0x01) {
107 sprintf(tmp, "%d ", i + 1);
108 strcat(buf, tmp);
110 value >>= 1;
112 strcat(buf, "\n");
113 } else {
114 strcpy(buf, "none\n");
117 return strlen(buf);
120 static ssize_t sky81452_bl_show_fault(struct device *dev,
121 struct device_attribute *attr, char *buf)
123 struct regmap *regmap = bl_get_data(to_backlight_device(dev));
124 unsigned int value = 0;
125 int ret;
127 ret = regmap_read(regmap, SKY81452_REG4, &value);
128 if (ret < 0)
129 return ret;
131 *buf = 0;
133 if (value & SKY81452_OCP)
134 strcat(buf, "over-current ");
136 if (value & SKY81452_OTMP)
137 strcat(buf, "over-temperature");
139 strcat(buf, "\n");
140 return strlen(buf);
143 static DEVICE_ATTR(enable, S_IWGRP | S_IWUSR, NULL, sky81452_bl_store_enable);
144 static DEVICE_ATTR(open, S_IRUGO, sky81452_bl_show_open_short, NULL);
145 static DEVICE_ATTR(short, S_IRUGO, sky81452_bl_show_open_short, NULL);
146 static DEVICE_ATTR(fault, S_IRUGO, sky81452_bl_show_fault, NULL);
148 static struct attribute *sky81452_bl_attribute[] = {
149 &dev_attr_enable.attr,
150 &dev_attr_open.attr,
151 &dev_attr_short.attr,
152 &dev_attr_fault.attr,
153 NULL
156 static const struct attribute_group sky81452_bl_attr_group = {
157 .attrs = sky81452_bl_attribute,
160 #ifdef CONFIG_OF
161 static struct sky81452_bl_platform_data *sky81452_bl_parse_dt(
162 struct device *dev)
164 struct device_node *np = of_node_get(dev->of_node);
165 struct sky81452_bl_platform_data *pdata;
166 int num_entry;
167 unsigned int sources[6];
168 int ret;
170 if (!np) {
171 dev_err(dev, "backlight node not found.\n");
172 return ERR_PTR(-ENODATA);
175 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
176 if (!pdata) {
177 of_node_put(np);
178 return ERR_PTR(-ENOMEM);
181 of_property_read_string(np, "name", &pdata->name);
182 pdata->ignore_pwm = of_property_read_bool(np, "skyworks,ignore-pwm");
183 pdata->dpwm_mode = of_property_read_bool(np, "skyworks,dpwm-mode");
184 pdata->phase_shift = of_property_read_bool(np, "skyworks,phase-shift");
185 pdata->gpio_enable = of_get_gpio(np, 0);
187 ret = of_property_count_u32_elems(np, "led-sources");
188 if (ret < 0) {
189 pdata->enable = SKY81452_EN >> CTZ(SKY81452_EN);
190 } else {
191 num_entry = ret;
192 if (num_entry > 6)
193 num_entry = 6;
195 ret = of_property_read_u32_array(np, "led-sources", sources,
196 num_entry);
197 if (ret < 0) {
198 dev_err(dev, "led-sources node is invalid.\n");
199 return ERR_PTR(-EINVAL);
202 pdata->enable = 0;
203 while (--num_entry)
204 pdata->enable |= (1 << sources[num_entry]);
207 ret = of_property_read_u32(np,
208 "skyworks,short-detection-threshold-volt",
209 &pdata->short_detection_threshold);
210 if (ret < 0)
211 pdata->short_detection_threshold = 7;
213 ret = of_property_read_u32(np, "skyworks,current-limit-mA",
214 &pdata->boost_current_limit);
215 if (ret < 0)
216 pdata->boost_current_limit = 2750;
218 of_node_put(np);
219 return pdata;
221 #else
222 static struct sky81452_bl_platform_data *sky81452_bl_parse_dt(
223 struct device *dev)
225 return ERR_PTR(-EINVAL);
227 #endif
229 static int sky81452_bl_init_device(struct regmap *regmap,
230 struct sky81452_bl_platform_data *pdata)
232 unsigned int value;
234 value = pdata->ignore_pwm ? SKY81452_IGPW : 0;
235 value |= pdata->dpwm_mode ? SKY81452_PWMMD : 0;
236 value |= pdata->phase_shift ? 0 : SKY81452_PHASE;
238 if (pdata->boost_current_limit == 2300)
239 value |= SKY81452_ILIM;
240 else if (pdata->boost_current_limit != 2750)
241 return -EINVAL;
243 if (pdata->short_detection_threshold < 4 ||
244 pdata->short_detection_threshold > 7)
245 return -EINVAL;
246 value |= (7 - pdata->short_detection_threshold) << CTZ(SKY81452_VSHRT);
248 return regmap_write(regmap, SKY81452_REG2, value);
251 static int sky81452_bl_probe(struct platform_device *pdev)
253 struct device *dev = &pdev->dev;
254 struct regmap *regmap = dev_get_drvdata(dev->parent);
255 struct sky81452_bl_platform_data *pdata = dev_get_platdata(dev);
256 struct backlight_device *bd;
257 struct backlight_properties props;
258 const char *name;
259 int ret;
261 if (!pdata) {
262 pdata = sky81452_bl_parse_dt(dev);
263 if (IS_ERR(pdata))
264 return PTR_ERR(pdata);
267 if (gpio_is_valid(pdata->gpio_enable)) {
268 ret = devm_gpio_request_one(dev, pdata->gpio_enable,
269 GPIOF_OUT_INIT_HIGH, "sky81452-en");
270 if (ret < 0) {
271 dev_err(dev, "failed to request GPIO. err=%d\n", ret);
272 return ret;
276 ret = sky81452_bl_init_device(regmap, pdata);
277 if (ret < 0) {
278 dev_err(dev, "failed to initialize. err=%d\n", ret);
279 return ret;
282 memset(&props, 0, sizeof(props));
283 props.max_brightness = SKY81452_MAX_BRIGHTNESS,
284 name = pdata->name ? pdata->name : SKY81452_DEFAULT_NAME;
285 bd = devm_backlight_device_register(dev, name, dev, regmap,
286 &sky81452_bl_ops, &props);
287 if (IS_ERR(bd)) {
288 dev_err(dev, "failed to register. err=%ld\n", PTR_ERR(bd));
289 return PTR_ERR(bd);
292 platform_set_drvdata(pdev, bd);
294 ret = sysfs_create_group(&bd->dev.kobj, &sky81452_bl_attr_group);
295 if (ret < 0) {
296 dev_err(dev, "failed to create attribute. err=%d\n", ret);
297 return ret;
300 return ret;
303 static int sky81452_bl_remove(struct platform_device *pdev)
305 const struct sky81452_bl_platform_data *pdata =
306 dev_get_platdata(&pdev->dev);
307 struct backlight_device *bd = platform_get_drvdata(pdev);
309 sysfs_remove_group(&bd->dev.kobj, &sky81452_bl_attr_group);
311 bd->props.power = FB_BLANK_UNBLANK;
312 bd->props.brightness = 0;
313 backlight_update_status(bd);
315 if (gpio_is_valid(pdata->gpio_enable))
316 gpio_set_value_cansleep(pdata->gpio_enable, 0);
318 return 0;
321 #ifdef CONFIG_OF
322 static const struct of_device_id sky81452_bl_of_match[] = {
323 { .compatible = "skyworks,sky81452-backlight", },
326 MODULE_DEVICE_TABLE(of, sky81452_bl_of_match);
327 #endif
329 static struct platform_driver sky81452_bl_driver = {
330 .driver = {
331 .name = "sky81452-backlight",
332 .of_match_table = of_match_ptr(sky81452_bl_of_match),
334 .probe = sky81452_bl_probe,
335 .remove = sky81452_bl_remove,
338 module_platform_driver(sky81452_bl_driver);
340 MODULE_DESCRIPTION("Skyworks SKY81452 backlight driver");
341 MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@skyworksinc.com>");
342 MODULE_LICENSE("GPL v2");