treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / video / backlight / lm3533_bl.c
blobee09d1bd02b97290acc7b2bd4e89fb687a317c10
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * lm3533-bl.c -- LM3533 Backlight driver
5 * Copyright (C) 2011-2012 Texas Instruments
7 * Author: Johan Hovold <jhovold@gmail.com>
8 */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/platform_device.h>
13 #include <linux/backlight.h>
14 #include <linux/fb.h>
15 #include <linux/slab.h>
17 #include <linux/mfd/lm3533.h>
20 #define LM3533_HVCTRLBANK_COUNT 2
21 #define LM3533_BL_MAX_BRIGHTNESS 255
23 #define LM3533_REG_CTRLBANK_AB_BCONF 0x1a
26 struct lm3533_bl {
27 struct lm3533 *lm3533;
28 struct lm3533_ctrlbank cb;
29 struct backlight_device *bd;
30 int id;
34 static inline int lm3533_bl_get_ctrlbank_id(struct lm3533_bl *bl)
36 return bl->id;
39 static int lm3533_bl_update_status(struct backlight_device *bd)
41 struct lm3533_bl *bl = bl_get_data(bd);
42 int brightness = bd->props.brightness;
44 if (bd->props.power != FB_BLANK_UNBLANK)
45 brightness = 0;
46 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
47 brightness = 0;
49 return lm3533_ctrlbank_set_brightness(&bl->cb, (u8)brightness);
52 static int lm3533_bl_get_brightness(struct backlight_device *bd)
54 struct lm3533_bl *bl = bl_get_data(bd);
55 u8 val;
56 int ret;
58 ret = lm3533_ctrlbank_get_brightness(&bl->cb, &val);
59 if (ret)
60 return ret;
62 return val;
65 static const struct backlight_ops lm3533_bl_ops = {
66 .get_brightness = lm3533_bl_get_brightness,
67 .update_status = lm3533_bl_update_status,
70 static ssize_t show_id(struct device *dev,
71 struct device_attribute *attr, char *buf)
73 struct lm3533_bl *bl = dev_get_drvdata(dev);
75 return scnprintf(buf, PAGE_SIZE, "%d\n", bl->id);
78 static ssize_t show_als_channel(struct device *dev,
79 struct device_attribute *attr, char *buf)
81 struct lm3533_bl *bl = dev_get_drvdata(dev);
82 unsigned channel = lm3533_bl_get_ctrlbank_id(bl);
84 return scnprintf(buf, PAGE_SIZE, "%u\n", channel);
87 static ssize_t show_als_en(struct device *dev,
88 struct device_attribute *attr, char *buf)
90 struct lm3533_bl *bl = dev_get_drvdata(dev);
91 int ctrlbank = lm3533_bl_get_ctrlbank_id(bl);
92 u8 val;
93 u8 mask;
94 bool enable;
95 int ret;
97 ret = lm3533_read(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, &val);
98 if (ret)
99 return ret;
101 mask = 1 << (2 * ctrlbank);
102 enable = val & mask;
104 return scnprintf(buf, PAGE_SIZE, "%d\n", enable);
107 static ssize_t store_als_en(struct device *dev,
108 struct device_attribute *attr,
109 const char *buf, size_t len)
111 struct lm3533_bl *bl = dev_get_drvdata(dev);
112 int ctrlbank = lm3533_bl_get_ctrlbank_id(bl);
113 int enable;
114 u8 val;
115 u8 mask;
116 int ret;
118 if (kstrtoint(buf, 0, &enable))
119 return -EINVAL;
121 mask = 1 << (2 * ctrlbank);
123 if (enable)
124 val = mask;
125 else
126 val = 0;
128 ret = lm3533_update(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, val,
129 mask);
130 if (ret)
131 return ret;
133 return len;
136 static ssize_t show_linear(struct device *dev,
137 struct device_attribute *attr, char *buf)
139 struct lm3533_bl *bl = dev_get_drvdata(dev);
140 u8 val;
141 u8 mask;
142 int linear;
143 int ret;
145 ret = lm3533_read(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, &val);
146 if (ret)
147 return ret;
149 mask = 1 << (2 * lm3533_bl_get_ctrlbank_id(bl) + 1);
151 if (val & mask)
152 linear = 1;
153 else
154 linear = 0;
156 return scnprintf(buf, PAGE_SIZE, "%x\n", linear);
159 static ssize_t store_linear(struct device *dev,
160 struct device_attribute *attr,
161 const char *buf, size_t len)
163 struct lm3533_bl *bl = dev_get_drvdata(dev);
164 unsigned long linear;
165 u8 mask;
166 u8 val;
167 int ret;
169 if (kstrtoul(buf, 0, &linear))
170 return -EINVAL;
172 mask = 1 << (2 * lm3533_bl_get_ctrlbank_id(bl) + 1);
174 if (linear)
175 val = mask;
176 else
177 val = 0;
179 ret = lm3533_update(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, val,
180 mask);
181 if (ret)
182 return ret;
184 return len;
187 static ssize_t show_pwm(struct device *dev,
188 struct device_attribute *attr,
189 char *buf)
191 struct lm3533_bl *bl = dev_get_drvdata(dev);
192 u8 val;
193 int ret;
195 ret = lm3533_ctrlbank_get_pwm(&bl->cb, &val);
196 if (ret)
197 return ret;
199 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
202 static ssize_t store_pwm(struct device *dev,
203 struct device_attribute *attr,
204 const char *buf, size_t len)
206 struct lm3533_bl *bl = dev_get_drvdata(dev);
207 u8 val;
208 int ret;
210 if (kstrtou8(buf, 0, &val))
211 return -EINVAL;
213 ret = lm3533_ctrlbank_set_pwm(&bl->cb, val);
214 if (ret)
215 return ret;
217 return len;
220 static LM3533_ATTR_RO(als_channel);
221 static LM3533_ATTR_RW(als_en);
222 static LM3533_ATTR_RO(id);
223 static LM3533_ATTR_RW(linear);
224 static LM3533_ATTR_RW(pwm);
226 static struct attribute *lm3533_bl_attributes[] = {
227 &dev_attr_als_channel.attr,
228 &dev_attr_als_en.attr,
229 &dev_attr_id.attr,
230 &dev_attr_linear.attr,
231 &dev_attr_pwm.attr,
232 NULL,
235 static umode_t lm3533_bl_attr_is_visible(struct kobject *kobj,
236 struct attribute *attr, int n)
238 struct device *dev = container_of(kobj, struct device, kobj);
239 struct lm3533_bl *bl = dev_get_drvdata(dev);
240 umode_t mode = attr->mode;
242 if (attr == &dev_attr_als_channel.attr ||
243 attr == &dev_attr_als_en.attr) {
244 if (!bl->lm3533->have_als)
245 mode = 0;
248 return mode;
251 static struct attribute_group lm3533_bl_attribute_group = {
252 .is_visible = lm3533_bl_attr_is_visible,
253 .attrs = lm3533_bl_attributes
256 static int lm3533_bl_setup(struct lm3533_bl *bl,
257 struct lm3533_bl_platform_data *pdata)
259 int ret;
261 ret = lm3533_ctrlbank_set_max_current(&bl->cb, pdata->max_current);
262 if (ret)
263 return ret;
265 return lm3533_ctrlbank_set_pwm(&bl->cb, pdata->pwm);
268 static int lm3533_bl_probe(struct platform_device *pdev)
270 struct lm3533 *lm3533;
271 struct lm3533_bl_platform_data *pdata;
272 struct lm3533_bl *bl;
273 struct backlight_device *bd;
274 struct backlight_properties props;
275 int ret;
277 dev_dbg(&pdev->dev, "%s\n", __func__);
279 lm3533 = dev_get_drvdata(pdev->dev.parent);
280 if (!lm3533)
281 return -EINVAL;
283 pdata = dev_get_platdata(&pdev->dev);
284 if (!pdata) {
285 dev_err(&pdev->dev, "no platform data\n");
286 return -EINVAL;
289 if (pdev->id < 0 || pdev->id >= LM3533_HVCTRLBANK_COUNT) {
290 dev_err(&pdev->dev, "illegal backlight id %d\n", pdev->id);
291 return -EINVAL;
294 bl = devm_kzalloc(&pdev->dev, sizeof(*bl), GFP_KERNEL);
295 if (!bl)
296 return -ENOMEM;
298 bl->lm3533 = lm3533;
299 bl->id = pdev->id;
301 bl->cb.lm3533 = lm3533;
302 bl->cb.id = lm3533_bl_get_ctrlbank_id(bl);
303 bl->cb.dev = NULL; /* until registered */
305 memset(&props, 0, sizeof(props));
306 props.type = BACKLIGHT_RAW;
307 props.max_brightness = LM3533_BL_MAX_BRIGHTNESS;
308 props.brightness = pdata->default_brightness;
309 bd = devm_backlight_device_register(&pdev->dev, pdata->name,
310 pdev->dev.parent, bl, &lm3533_bl_ops,
311 &props);
312 if (IS_ERR(bd)) {
313 dev_err(&pdev->dev, "failed to register backlight device\n");
314 return PTR_ERR(bd);
317 bl->bd = bd;
318 bl->cb.dev = &bl->bd->dev;
320 platform_set_drvdata(pdev, bl);
322 ret = sysfs_create_group(&bd->dev.kobj, &lm3533_bl_attribute_group);
323 if (ret < 0) {
324 dev_err(&pdev->dev, "failed to create sysfs attributes\n");
325 return ret;
328 backlight_update_status(bd);
330 ret = lm3533_bl_setup(bl, pdata);
331 if (ret)
332 goto err_sysfs_remove;
334 ret = lm3533_ctrlbank_enable(&bl->cb);
335 if (ret)
336 goto err_sysfs_remove;
338 return 0;
340 err_sysfs_remove:
341 sysfs_remove_group(&bd->dev.kobj, &lm3533_bl_attribute_group);
343 return ret;
346 static int lm3533_bl_remove(struct platform_device *pdev)
348 struct lm3533_bl *bl = platform_get_drvdata(pdev);
349 struct backlight_device *bd = bl->bd;
351 dev_dbg(&bd->dev, "%s\n", __func__);
353 bd->props.power = FB_BLANK_POWERDOWN;
354 bd->props.brightness = 0;
356 lm3533_ctrlbank_disable(&bl->cb);
357 sysfs_remove_group(&bd->dev.kobj, &lm3533_bl_attribute_group);
359 return 0;
362 #ifdef CONFIG_PM_SLEEP
363 static int lm3533_bl_suspend(struct device *dev)
365 struct lm3533_bl *bl = dev_get_drvdata(dev);
367 dev_dbg(dev, "%s\n", __func__);
369 return lm3533_ctrlbank_disable(&bl->cb);
372 static int lm3533_bl_resume(struct device *dev)
374 struct lm3533_bl *bl = dev_get_drvdata(dev);
376 dev_dbg(dev, "%s\n", __func__);
378 return lm3533_ctrlbank_enable(&bl->cb);
380 #endif
382 static SIMPLE_DEV_PM_OPS(lm3533_bl_pm_ops, lm3533_bl_suspend, lm3533_bl_resume);
384 static void lm3533_bl_shutdown(struct platform_device *pdev)
386 struct lm3533_bl *bl = platform_get_drvdata(pdev);
388 dev_dbg(&pdev->dev, "%s\n", __func__);
390 lm3533_ctrlbank_disable(&bl->cb);
393 static struct platform_driver lm3533_bl_driver = {
394 .driver = {
395 .name = "lm3533-backlight",
396 .pm = &lm3533_bl_pm_ops,
398 .probe = lm3533_bl_probe,
399 .remove = lm3533_bl_remove,
400 .shutdown = lm3533_bl_shutdown,
402 module_platform_driver(lm3533_bl_driver);
404 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
405 MODULE_DESCRIPTION("LM3533 Backlight driver");
406 MODULE_LICENSE("GPL");
407 MODULE_ALIAS("platform:lm3533-backlight");