treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / video / backlight / lm3630a_bl.c
blobee320883b7108596d19bf838a2c9df13f2d5eb53
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Simple driver for Texas Instruments LM3630A Backlight driver chip
4 * Copyright (C) 2012 Texas Instruments
5 */
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/i2c.h>
9 #include <linux/backlight.h>
10 #include <linux/err.h>
11 #include <linux/delay.h>
12 #include <linux/uaccess.h>
13 #include <linux/interrupt.h>
14 #include <linux/regmap.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/pwm.h>
17 #include <linux/platform_data/lm3630a_bl.h>
19 #define REG_CTRL 0x00
20 #define REG_BOOST 0x02
21 #define REG_CONFIG 0x01
22 #define REG_BRT_A 0x03
23 #define REG_BRT_B 0x04
24 #define REG_I_A 0x05
25 #define REG_I_B 0x06
26 #define REG_INT_STATUS 0x09
27 #define REG_INT_EN 0x0A
28 #define REG_FAULT 0x0B
29 #define REG_PWM_OUTLOW 0x12
30 #define REG_PWM_OUTHIGH 0x13
31 #define REG_FILTER_STRENGTH 0x50
32 #define REG_MAX 0x50
34 #define INT_DEBOUNCE_MSEC 10
36 #define LM3630A_BANK_0 0
37 #define LM3630A_BANK_1 1
39 #define LM3630A_NUM_SINKS 2
40 #define LM3630A_SINK_0 0
41 #define LM3630A_SINK_1 1
43 struct lm3630a_chip {
44 struct device *dev;
45 struct delayed_work work;
47 int irq;
48 struct workqueue_struct *irqthread;
49 struct lm3630a_platform_data *pdata;
50 struct backlight_device *bleda;
51 struct backlight_device *bledb;
52 struct gpio_desc *enable_gpio;
53 struct regmap *regmap;
54 struct pwm_device *pwmd;
57 /* i2c access */
58 static int lm3630a_read(struct lm3630a_chip *pchip, unsigned int reg)
60 int rval;
61 unsigned int reg_val;
63 rval = regmap_read(pchip->regmap, reg, &reg_val);
64 if (rval < 0)
65 return rval;
66 return reg_val & 0xFF;
69 static int lm3630a_write(struct lm3630a_chip *pchip,
70 unsigned int reg, unsigned int data)
72 return regmap_write(pchip->regmap, reg, data);
75 static int lm3630a_update(struct lm3630a_chip *pchip,
76 unsigned int reg, unsigned int mask,
77 unsigned int data)
79 return regmap_update_bits(pchip->regmap, reg, mask, data);
82 /* initialize chip */
83 static int lm3630a_chip_init(struct lm3630a_chip *pchip)
85 int rval;
86 struct lm3630a_platform_data *pdata = pchip->pdata;
88 usleep_range(1000, 2000);
89 /* set Filter Strength Register */
90 rval = lm3630a_write(pchip, REG_FILTER_STRENGTH, 0x03);
91 /* set Cofig. register */
92 rval |= lm3630a_update(pchip, REG_CONFIG, 0x07, pdata->pwm_ctrl);
93 /* set boost control */
94 rval |= lm3630a_write(pchip, REG_BOOST, 0x38);
95 /* set current A */
96 rval |= lm3630a_update(pchip, REG_I_A, 0x1F, 0x1F);
97 /* set current B */
98 rval |= lm3630a_write(pchip, REG_I_B, 0x1F);
99 /* set control */
100 rval |= lm3630a_update(pchip, REG_CTRL, 0x14, pdata->leda_ctrl);
101 rval |= lm3630a_update(pchip, REG_CTRL, 0x0B, pdata->ledb_ctrl);
102 usleep_range(1000, 2000);
103 /* set brightness A and B */
104 rval |= lm3630a_write(pchip, REG_BRT_A, pdata->leda_init_brt);
105 rval |= lm3630a_write(pchip, REG_BRT_B, pdata->ledb_init_brt);
107 if (rval < 0)
108 dev_err(pchip->dev, "i2c failed to access register\n");
109 return rval;
112 /* interrupt handling */
113 static void lm3630a_delayed_func(struct work_struct *work)
115 int rval;
116 struct lm3630a_chip *pchip;
118 pchip = container_of(work, struct lm3630a_chip, work.work);
120 rval = lm3630a_read(pchip, REG_INT_STATUS);
121 if (rval < 0) {
122 dev_err(pchip->dev,
123 "i2c failed to access REG_INT_STATUS Register\n");
124 return;
127 dev_info(pchip->dev, "REG_INT_STATUS Register is 0x%x\n", rval);
130 static irqreturn_t lm3630a_isr_func(int irq, void *chip)
132 int rval;
133 struct lm3630a_chip *pchip = chip;
134 unsigned long delay = msecs_to_jiffies(INT_DEBOUNCE_MSEC);
136 queue_delayed_work(pchip->irqthread, &pchip->work, delay);
138 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
139 if (rval < 0) {
140 dev_err(pchip->dev, "i2c failed to access register\n");
141 return IRQ_NONE;
143 return IRQ_HANDLED;
146 static int lm3630a_intr_config(struct lm3630a_chip *pchip)
148 int rval;
150 rval = lm3630a_write(pchip, REG_INT_EN, 0x87);
151 if (rval < 0)
152 return rval;
154 INIT_DELAYED_WORK(&pchip->work, lm3630a_delayed_func);
155 pchip->irqthread = create_singlethread_workqueue("lm3630a-irqthd");
156 if (!pchip->irqthread) {
157 dev_err(pchip->dev, "create irq thread fail\n");
158 return -ENOMEM;
160 if (request_threaded_irq
161 (pchip->irq, NULL, lm3630a_isr_func,
162 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm3630a_irq", pchip)) {
163 dev_err(pchip->dev, "request threaded irq fail\n");
164 destroy_workqueue(pchip->irqthread);
165 return -ENOMEM;
167 return rval;
170 static void lm3630a_pwm_ctrl(struct lm3630a_chip *pchip, int br, int br_max)
172 unsigned int period = pchip->pdata->pwm_period;
173 unsigned int duty = br * period / br_max;
175 pwm_config(pchip->pwmd, duty, period);
176 if (duty)
177 pwm_enable(pchip->pwmd);
178 else
179 pwm_disable(pchip->pwmd);
182 /* update and get brightness */
183 static int lm3630a_bank_a_update_status(struct backlight_device *bl)
185 int ret;
186 struct lm3630a_chip *pchip = bl_get_data(bl);
187 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
189 /* pwm control */
190 if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
191 lm3630a_pwm_ctrl(pchip, bl->props.brightness,
192 bl->props.max_brightness);
193 return bl->props.brightness;
196 /* disable sleep */
197 ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
198 if (ret < 0)
199 goto out_i2c_err;
200 usleep_range(1000, 2000);
201 /* minimum brightness is 0x04 */
202 ret = lm3630a_write(pchip, REG_BRT_A, bl->props.brightness);
203 if (bl->props.brightness < 0x4)
204 ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDA_ENABLE, 0);
205 else
206 ret |= lm3630a_update(pchip, REG_CTRL,
207 LM3630A_LEDA_ENABLE, LM3630A_LEDA_ENABLE);
208 if (ret < 0)
209 goto out_i2c_err;
210 return 0;
212 out_i2c_err:
213 dev_err(pchip->dev, "i2c failed to access\n");
214 return bl->props.brightness;
217 static int lm3630a_bank_a_get_brightness(struct backlight_device *bl)
219 int brightness, rval;
220 struct lm3630a_chip *pchip = bl_get_data(bl);
221 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
223 if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
224 rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
225 if (rval < 0)
226 goto out_i2c_err;
227 brightness = (rval & 0x01) << 8;
228 rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
229 if (rval < 0)
230 goto out_i2c_err;
231 brightness |= rval;
232 goto out;
235 /* disable sleep */
236 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
237 if (rval < 0)
238 goto out_i2c_err;
239 usleep_range(1000, 2000);
240 rval = lm3630a_read(pchip, REG_BRT_A);
241 if (rval < 0)
242 goto out_i2c_err;
243 brightness = rval;
245 out:
246 bl->props.brightness = brightness;
247 return bl->props.brightness;
248 out_i2c_err:
249 dev_err(pchip->dev, "i2c failed to access register\n");
250 return 0;
253 static const struct backlight_ops lm3630a_bank_a_ops = {
254 .options = BL_CORE_SUSPENDRESUME,
255 .update_status = lm3630a_bank_a_update_status,
256 .get_brightness = lm3630a_bank_a_get_brightness,
259 /* update and get brightness */
260 static int lm3630a_bank_b_update_status(struct backlight_device *bl)
262 int ret;
263 struct lm3630a_chip *pchip = bl_get_data(bl);
264 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
266 /* pwm control */
267 if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
268 lm3630a_pwm_ctrl(pchip, bl->props.brightness,
269 bl->props.max_brightness);
270 return bl->props.brightness;
273 /* disable sleep */
274 ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
275 if (ret < 0)
276 goto out_i2c_err;
277 usleep_range(1000, 2000);
278 /* minimum brightness is 0x04 */
279 ret = lm3630a_write(pchip, REG_BRT_B, bl->props.brightness);
280 if (bl->props.brightness < 0x4)
281 ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDB_ENABLE, 0);
282 else
283 ret |= lm3630a_update(pchip, REG_CTRL,
284 LM3630A_LEDB_ENABLE, LM3630A_LEDB_ENABLE);
285 if (ret < 0)
286 goto out_i2c_err;
287 return 0;
289 out_i2c_err:
290 dev_err(pchip->dev, "i2c failed to access REG_CTRL\n");
291 return bl->props.brightness;
294 static int lm3630a_bank_b_get_brightness(struct backlight_device *bl)
296 int brightness, rval;
297 struct lm3630a_chip *pchip = bl_get_data(bl);
298 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
300 if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
301 rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
302 if (rval < 0)
303 goto out_i2c_err;
304 brightness = (rval & 0x01) << 8;
305 rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
306 if (rval < 0)
307 goto out_i2c_err;
308 brightness |= rval;
309 goto out;
312 /* disable sleep */
313 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
314 if (rval < 0)
315 goto out_i2c_err;
316 usleep_range(1000, 2000);
317 rval = lm3630a_read(pchip, REG_BRT_B);
318 if (rval < 0)
319 goto out_i2c_err;
320 brightness = rval;
322 out:
323 bl->props.brightness = brightness;
324 return bl->props.brightness;
325 out_i2c_err:
326 dev_err(pchip->dev, "i2c failed to access register\n");
327 return 0;
330 static const struct backlight_ops lm3630a_bank_b_ops = {
331 .options = BL_CORE_SUSPENDRESUME,
332 .update_status = lm3630a_bank_b_update_status,
333 .get_brightness = lm3630a_bank_b_get_brightness,
336 static int lm3630a_backlight_register(struct lm3630a_chip *pchip)
338 struct lm3630a_platform_data *pdata = pchip->pdata;
339 struct backlight_properties props;
340 const char *label;
342 props.type = BACKLIGHT_RAW;
343 if (pdata->leda_ctrl != LM3630A_LEDA_DISABLE) {
344 props.brightness = pdata->leda_init_brt;
345 props.max_brightness = pdata->leda_max_brt;
346 label = pdata->leda_label ? pdata->leda_label : "lm3630a_leda";
347 pchip->bleda =
348 devm_backlight_device_register(pchip->dev, label,
349 pchip->dev, pchip,
350 &lm3630a_bank_a_ops, &props);
351 if (IS_ERR(pchip->bleda))
352 return PTR_ERR(pchip->bleda);
355 if ((pdata->ledb_ctrl != LM3630A_LEDB_DISABLE) &&
356 (pdata->ledb_ctrl != LM3630A_LEDB_ON_A)) {
357 props.brightness = pdata->ledb_init_brt;
358 props.max_brightness = pdata->ledb_max_brt;
359 label = pdata->ledb_label ? pdata->ledb_label : "lm3630a_ledb";
360 pchip->bledb =
361 devm_backlight_device_register(pchip->dev, label,
362 pchip->dev, pchip,
363 &lm3630a_bank_b_ops, &props);
364 if (IS_ERR(pchip->bledb))
365 return PTR_ERR(pchip->bledb);
367 return 0;
370 static const struct regmap_config lm3630a_regmap = {
371 .reg_bits = 8,
372 .val_bits = 8,
373 .max_register = REG_MAX,
376 static int lm3630a_parse_led_sources(struct fwnode_handle *node,
377 int default_led_sources)
379 u32 sources[LM3630A_NUM_SINKS];
380 int ret, num_sources, i;
382 num_sources = fwnode_property_count_u32(node, "led-sources");
383 if (num_sources < 0)
384 return default_led_sources;
385 else if (num_sources > ARRAY_SIZE(sources))
386 return -EINVAL;
388 ret = fwnode_property_read_u32_array(node, "led-sources", sources,
389 num_sources);
390 if (ret)
391 return ret;
393 for (i = 0; i < num_sources; i++) {
394 if (sources[i] < LM3630A_SINK_0 || sources[i] > LM3630A_SINK_1)
395 return -EINVAL;
397 ret |= BIT(sources[i]);
400 return ret;
403 static int lm3630a_parse_bank(struct lm3630a_platform_data *pdata,
404 struct fwnode_handle *node, int *seen_led_sources)
406 int led_sources, ret;
407 const char *label;
408 u32 bank, val;
409 bool linear;
411 ret = fwnode_property_read_u32(node, "reg", &bank);
412 if (ret)
413 return ret;
415 if (bank < LM3630A_BANK_0 || bank > LM3630A_BANK_1)
416 return -EINVAL;
418 led_sources = lm3630a_parse_led_sources(node, BIT(bank));
419 if (led_sources < 0)
420 return led_sources;
422 if (*seen_led_sources & led_sources)
423 return -EINVAL;
425 *seen_led_sources |= led_sources;
427 linear = fwnode_property_read_bool(node,
428 "ti,linear-mapping-mode");
429 if (bank) {
430 if (led_sources & BIT(LM3630A_SINK_0) ||
431 !(led_sources & BIT(LM3630A_SINK_1)))
432 return -EINVAL;
434 pdata->ledb_ctrl = linear ?
435 LM3630A_LEDB_ENABLE_LINEAR :
436 LM3630A_LEDB_ENABLE;
437 } else {
438 if (!(led_sources & BIT(LM3630A_SINK_0)))
439 return -EINVAL;
441 pdata->leda_ctrl = linear ?
442 LM3630A_LEDA_ENABLE_LINEAR :
443 LM3630A_LEDA_ENABLE;
445 if (led_sources & BIT(LM3630A_SINK_1))
446 pdata->ledb_ctrl = LM3630A_LEDB_ON_A;
449 ret = fwnode_property_read_string(node, "label", &label);
450 if (!ret) {
451 if (bank)
452 pdata->ledb_label = label;
453 else
454 pdata->leda_label = label;
457 ret = fwnode_property_read_u32(node, "default-brightness",
458 &val);
459 if (!ret) {
460 if (bank)
461 pdata->ledb_init_brt = val;
462 else
463 pdata->leda_init_brt = val;
466 ret = fwnode_property_read_u32(node, "max-brightness", &val);
467 if (!ret) {
468 if (bank)
469 pdata->ledb_max_brt = val;
470 else
471 pdata->leda_max_brt = val;
474 return 0;
477 static int lm3630a_parse_node(struct lm3630a_chip *pchip,
478 struct lm3630a_platform_data *pdata)
480 int ret = -ENODEV, seen_led_sources = 0;
481 struct fwnode_handle *node;
483 device_for_each_child_node(pchip->dev, node) {
484 ret = lm3630a_parse_bank(pdata, node, &seen_led_sources);
485 if (ret)
486 return ret;
489 return ret;
492 static int lm3630a_probe(struct i2c_client *client,
493 const struct i2c_device_id *id)
495 struct lm3630a_platform_data *pdata = dev_get_platdata(&client->dev);
496 struct lm3630a_chip *pchip;
497 int rval;
499 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
500 dev_err(&client->dev, "fail : i2c functionality check\n");
501 return -EOPNOTSUPP;
504 pchip = devm_kzalloc(&client->dev, sizeof(struct lm3630a_chip),
505 GFP_KERNEL);
506 if (!pchip)
507 return -ENOMEM;
508 pchip->dev = &client->dev;
510 pchip->regmap = devm_regmap_init_i2c(client, &lm3630a_regmap);
511 if (IS_ERR(pchip->regmap)) {
512 rval = PTR_ERR(pchip->regmap);
513 dev_err(&client->dev, "fail : allocate reg. map: %d\n", rval);
514 return rval;
517 i2c_set_clientdata(client, pchip);
518 if (pdata == NULL) {
519 pdata = devm_kzalloc(pchip->dev,
520 sizeof(struct lm3630a_platform_data),
521 GFP_KERNEL);
522 if (pdata == NULL)
523 return -ENOMEM;
525 /* default values */
526 pdata->leda_max_brt = LM3630A_MAX_BRIGHTNESS;
527 pdata->ledb_max_brt = LM3630A_MAX_BRIGHTNESS;
528 pdata->leda_init_brt = LM3630A_MAX_BRIGHTNESS;
529 pdata->ledb_init_brt = LM3630A_MAX_BRIGHTNESS;
531 rval = lm3630a_parse_node(pchip, pdata);
532 if (rval) {
533 dev_err(&client->dev, "fail : parse node\n");
534 return rval;
537 pchip->pdata = pdata;
539 pchip->enable_gpio = devm_gpiod_get_optional(&client->dev, "enable",
540 GPIOD_OUT_HIGH);
541 if (IS_ERR(pchip->enable_gpio)) {
542 rval = PTR_ERR(pchip->enable_gpio);
543 return rval;
546 /* chip initialize */
547 rval = lm3630a_chip_init(pchip);
548 if (rval < 0) {
549 dev_err(&client->dev, "fail : init chip\n");
550 return rval;
552 /* backlight register */
553 rval = lm3630a_backlight_register(pchip);
554 if (rval < 0) {
555 dev_err(&client->dev, "fail : backlight register.\n");
556 return rval;
558 /* pwm */
559 if (pdata->pwm_ctrl != LM3630A_PWM_DISABLE) {
560 pchip->pwmd = devm_pwm_get(pchip->dev, "lm3630a-pwm");
561 if (IS_ERR(pchip->pwmd)) {
562 dev_err(&client->dev, "fail : get pwm device\n");
563 return PTR_ERR(pchip->pwmd);
567 * FIXME: pwm_apply_args() should be removed when switching to
568 * the atomic PWM API.
570 pwm_apply_args(pchip->pwmd);
573 /* interrupt enable : irq 0 is not allowed */
574 pchip->irq = client->irq;
575 if (pchip->irq) {
576 rval = lm3630a_intr_config(pchip);
577 if (rval < 0)
578 return rval;
580 dev_info(&client->dev, "LM3630A backlight register OK.\n");
581 return 0;
584 static int lm3630a_remove(struct i2c_client *client)
586 int rval;
587 struct lm3630a_chip *pchip = i2c_get_clientdata(client);
589 rval = lm3630a_write(pchip, REG_BRT_A, 0);
590 if (rval < 0)
591 dev_err(pchip->dev, "i2c failed to access register\n");
593 rval = lm3630a_write(pchip, REG_BRT_B, 0);
594 if (rval < 0)
595 dev_err(pchip->dev, "i2c failed to access register\n");
597 if (pchip->irq) {
598 free_irq(pchip->irq, pchip);
599 flush_workqueue(pchip->irqthread);
600 destroy_workqueue(pchip->irqthread);
602 return 0;
605 static const struct i2c_device_id lm3630a_id[] = {
606 {LM3630A_NAME, 0},
610 MODULE_DEVICE_TABLE(i2c, lm3630a_id);
612 static const struct of_device_id lm3630a_match_table[] = {
613 { .compatible = "ti,lm3630a", },
614 { },
617 MODULE_DEVICE_TABLE(of, lm3630a_match_table);
619 static struct i2c_driver lm3630a_i2c_driver = {
620 .driver = {
621 .name = LM3630A_NAME,
622 .of_match_table = lm3630a_match_table,
624 .probe = lm3630a_probe,
625 .remove = lm3630a_remove,
626 .id_table = lm3630a_id,
629 module_i2c_driver(lm3630a_i2c_driver);
631 MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
632 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
633 MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
634 MODULE_LICENSE("GPL v2");