xref: /linux/drivers/video/backlight/lm3630a_bl.c (revision 29554f2e9138d60012d0e0d53e6792f5f4da1fb6)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
228e64a68SDaniel Jeong /*
328e64a68SDaniel Jeong * Simple driver for Texas Instruments LM3630A Backlight driver chip
428e64a68SDaniel Jeong * Copyright (C) 2012 Texas Instruments
528e64a68SDaniel Jeong */
628e64a68SDaniel Jeong #include <linux/module.h>
728e64a68SDaniel Jeong #include <linux/slab.h>
828e64a68SDaniel Jeong #include <linux/i2c.h>
928e64a68SDaniel Jeong #include <linux/backlight.h>
1028e64a68SDaniel Jeong #include <linux/err.h>
1128e64a68SDaniel Jeong #include <linux/delay.h>
1228e64a68SDaniel Jeong #include <linux/uaccess.h>
1328e64a68SDaniel Jeong #include <linux/interrupt.h>
1428e64a68SDaniel Jeong #include <linux/regmap.h>
150e0e78e3SAndreas Kemnade #include <linux/gpio/consumer.h>
1628e64a68SDaniel Jeong #include <linux/pwm.h>
1728e64a68SDaniel Jeong #include <linux/platform_data/lm3630a_bl.h>
1828e64a68SDaniel Jeong 
1928e64a68SDaniel Jeong #define REG_CTRL	0x00
2028e64a68SDaniel Jeong #define REG_BOOST	0x02
2128e64a68SDaniel Jeong #define REG_CONFIG	0x01
2228e64a68SDaniel Jeong #define REG_BRT_A	0x03
2328e64a68SDaniel Jeong #define REG_BRT_B	0x04
2428e64a68SDaniel Jeong #define REG_I_A		0x05
2528e64a68SDaniel Jeong #define REG_I_B		0x06
2628e64a68SDaniel Jeong #define REG_INT_STATUS	0x09
2728e64a68SDaniel Jeong #define REG_INT_EN	0x0A
2828e64a68SDaniel Jeong #define REG_FAULT	0x0B
2928e64a68SDaniel Jeong #define REG_PWM_OUTLOW	0x12
3028e64a68SDaniel Jeong #define REG_PWM_OUTHIGH	0x13
3106168a64SBhushan Shah #define REG_FILTER_STRENGTH	0x50
3206168a64SBhushan Shah #define REG_MAX		0x50
3328e64a68SDaniel Jeong 
3428e64a68SDaniel Jeong #define INT_DEBOUNCE_MSEC	10
358fbce8efSBrian Masney 
368fbce8efSBrian Masney #define LM3630A_BANK_0		0
378fbce8efSBrian Masney #define LM3630A_BANK_1		1
388fbce8efSBrian Masney 
398fbce8efSBrian Masney #define LM3630A_NUM_SINKS	2
408fbce8efSBrian Masney #define LM3630A_SINK_0		0
418fbce8efSBrian Masney #define LM3630A_SINK_1		1
428fbce8efSBrian Masney 
4328e64a68SDaniel Jeong struct lm3630a_chip {
4428e64a68SDaniel Jeong 	struct device *dev;
4528e64a68SDaniel Jeong 	struct delayed_work work;
4628e64a68SDaniel Jeong 
4728e64a68SDaniel Jeong 	int irq;
4828e64a68SDaniel Jeong 	struct workqueue_struct *irqthread;
4928e64a68SDaniel Jeong 	struct lm3630a_platform_data *pdata;
5028e64a68SDaniel Jeong 	struct backlight_device *bleda;
5128e64a68SDaniel Jeong 	struct backlight_device *bledb;
520e0e78e3SAndreas Kemnade 	struct gpio_desc *enable_gpio;
5328e64a68SDaniel Jeong 	struct regmap *regmap;
5428e64a68SDaniel Jeong 	struct pwm_device *pwmd;
551181f216SUwe Kleine-König 	struct pwm_state pwmd_state;
5628e64a68SDaniel Jeong };
5728e64a68SDaniel Jeong 
5828e64a68SDaniel Jeong /* i2c access */
5928e64a68SDaniel Jeong static int lm3630a_read(struct lm3630a_chip *pchip, unsigned int reg)
6028e64a68SDaniel Jeong {
6128e64a68SDaniel Jeong 	int rval;
6228e64a68SDaniel Jeong 	unsigned int reg_val;
6328e64a68SDaniel Jeong 
6428e64a68SDaniel Jeong 	rval = regmap_read(pchip->regmap, reg, &reg_val);
6528e64a68SDaniel Jeong 	if (rval < 0)
6628e64a68SDaniel Jeong 		return rval;
6728e64a68SDaniel Jeong 	return reg_val & 0xFF;
6828e64a68SDaniel Jeong }
6928e64a68SDaniel Jeong 
7028e64a68SDaniel Jeong static int lm3630a_write(struct lm3630a_chip *pchip,
7128e64a68SDaniel Jeong 			 unsigned int reg, unsigned int data)
7228e64a68SDaniel Jeong {
7328e64a68SDaniel Jeong 	return regmap_write(pchip->regmap, reg, data);
7428e64a68SDaniel Jeong }
7528e64a68SDaniel Jeong 
7628e64a68SDaniel Jeong static int lm3630a_update(struct lm3630a_chip *pchip,
7728e64a68SDaniel Jeong 			  unsigned int reg, unsigned int mask,
7828e64a68SDaniel Jeong 			  unsigned int data)
7928e64a68SDaniel Jeong {
8028e64a68SDaniel Jeong 	return regmap_update_bits(pchip->regmap, reg, mask, data);
8128e64a68SDaniel Jeong }
8228e64a68SDaniel Jeong 
8328e64a68SDaniel Jeong /* initialize chip */
8428e64a68SDaniel Jeong static int lm3630a_chip_init(struct lm3630a_chip *pchip)
8528e64a68SDaniel Jeong {
8628e64a68SDaniel Jeong 	int rval;
8728e64a68SDaniel Jeong 	struct lm3630a_platform_data *pdata = pchip->pdata;
8828e64a68SDaniel Jeong 
8928e64a68SDaniel Jeong 	usleep_range(1000, 2000);
9028e64a68SDaniel Jeong 	/* set Filter Strength Register */
9106168a64SBhushan Shah 	rval = lm3630a_write(pchip, REG_FILTER_STRENGTH, 0x03);
9228e64a68SDaniel Jeong 	/* set Cofig. register */
9328e64a68SDaniel Jeong 	rval |= lm3630a_update(pchip, REG_CONFIG, 0x07, pdata->pwm_ctrl);
9428e64a68SDaniel Jeong 	/* set boost control */
9528e64a68SDaniel Jeong 	rval |= lm3630a_write(pchip, REG_BOOST, 0x38);
9628e64a68SDaniel Jeong 	/* set current A */
9728e64a68SDaniel Jeong 	rval |= lm3630a_update(pchip, REG_I_A, 0x1F, 0x1F);
9828e64a68SDaniel Jeong 	/* set current B */
9928e64a68SDaniel Jeong 	rval |= lm3630a_write(pchip, REG_I_B, 0x1F);
10028e64a68SDaniel Jeong 	/* set control */
1014c4d2a3aSDaniel Jeong 	rval |= lm3630a_update(pchip, REG_CTRL, 0x14, pdata->leda_ctrl);
1024c4d2a3aSDaniel Jeong 	rval |= lm3630a_update(pchip, REG_CTRL, 0x0B, pdata->ledb_ctrl);
10328e64a68SDaniel Jeong 	usleep_range(1000, 2000);
10428e64a68SDaniel Jeong 	/* set brightness A and B */
10528e64a68SDaniel Jeong 	rval |= lm3630a_write(pchip, REG_BRT_A, pdata->leda_init_brt);
10628e64a68SDaniel Jeong 	rval |= lm3630a_write(pchip, REG_BRT_B, pdata->ledb_init_brt);
10728e64a68SDaniel Jeong 
10828e64a68SDaniel Jeong 	if (rval < 0)
10928e64a68SDaniel Jeong 		dev_err(pchip->dev, "i2c failed to access register\n");
11028e64a68SDaniel Jeong 	return rval;
11128e64a68SDaniel Jeong }
11228e64a68SDaniel Jeong 
11328e64a68SDaniel Jeong /* interrupt handling */
11428e64a68SDaniel Jeong static void lm3630a_delayed_func(struct work_struct *work)
11528e64a68SDaniel Jeong {
1162a0c316bSDan Carpenter 	int rval;
11728e64a68SDaniel Jeong 	struct lm3630a_chip *pchip;
11828e64a68SDaniel Jeong 
11928e64a68SDaniel Jeong 	pchip = container_of(work, struct lm3630a_chip, work.work);
12028e64a68SDaniel Jeong 
12128e64a68SDaniel Jeong 	rval = lm3630a_read(pchip, REG_INT_STATUS);
12228e64a68SDaniel Jeong 	if (rval < 0) {
12328e64a68SDaniel Jeong 		dev_err(pchip->dev,
12428e64a68SDaniel Jeong 			"i2c failed to access REG_INT_STATUS Register\n");
12528e64a68SDaniel Jeong 		return;
12628e64a68SDaniel Jeong 	}
12728e64a68SDaniel Jeong 
12828e64a68SDaniel Jeong 	dev_info(pchip->dev, "REG_INT_STATUS Register is 0x%x\n", rval);
12928e64a68SDaniel Jeong }
13028e64a68SDaniel Jeong 
13128e64a68SDaniel Jeong static irqreturn_t lm3630a_isr_func(int irq, void *chip)
13228e64a68SDaniel Jeong {
13328e64a68SDaniel Jeong 	int rval;
13428e64a68SDaniel Jeong 	struct lm3630a_chip *pchip = chip;
13528e64a68SDaniel Jeong 	unsigned long delay = msecs_to_jiffies(INT_DEBOUNCE_MSEC);
13628e64a68SDaniel Jeong 
13728e64a68SDaniel Jeong 	queue_delayed_work(pchip->irqthread, &pchip->work, delay);
13828e64a68SDaniel Jeong 
13928e64a68SDaniel Jeong 	rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
14028e64a68SDaniel Jeong 	if (rval < 0) {
14128e64a68SDaniel Jeong 		dev_err(pchip->dev, "i2c failed to access register\n");
14228e64a68SDaniel Jeong 		return IRQ_NONE;
14328e64a68SDaniel Jeong 	}
14428e64a68SDaniel Jeong 	return IRQ_HANDLED;
14528e64a68SDaniel Jeong }
14628e64a68SDaniel Jeong 
14728e64a68SDaniel Jeong static int lm3630a_intr_config(struct lm3630a_chip *pchip)
14828e64a68SDaniel Jeong {
14928e64a68SDaniel Jeong 	int rval;
15028e64a68SDaniel Jeong 
15128e64a68SDaniel Jeong 	rval = lm3630a_write(pchip, REG_INT_EN, 0x87);
15228e64a68SDaniel Jeong 	if (rval < 0)
15328e64a68SDaniel Jeong 		return rval;
15428e64a68SDaniel Jeong 
15528e64a68SDaniel Jeong 	INIT_DELAYED_WORK(&pchip->work, lm3630a_delayed_func);
15628e64a68SDaniel Jeong 	pchip->irqthread = create_singlethread_workqueue("lm3630a-irqthd");
15728e64a68SDaniel Jeong 	if (!pchip->irqthread) {
15828e64a68SDaniel Jeong 		dev_err(pchip->dev, "create irq thread fail\n");
15928e64a68SDaniel Jeong 		return -ENOMEM;
16028e64a68SDaniel Jeong 	}
16128e64a68SDaniel Jeong 	if (request_threaded_irq
16228e64a68SDaniel Jeong 	    (pchip->irq, NULL, lm3630a_isr_func,
16328e64a68SDaniel Jeong 	     IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm3630a_irq", pchip)) {
16428e64a68SDaniel Jeong 		dev_err(pchip->dev, "request threaded irq fail\n");
165bcd5b416SWei Yongjun 		destroy_workqueue(pchip->irqthread);
16628e64a68SDaniel Jeong 		return -ENOMEM;
16728e64a68SDaniel Jeong 	}
16828e64a68SDaniel Jeong 	return rval;
16928e64a68SDaniel Jeong }
17028e64a68SDaniel Jeong 
1711181f216SUwe Kleine-König static int lm3630a_pwm_ctrl(struct lm3630a_chip *pchip, int br, int br_max)
17228e64a68SDaniel Jeong {
1731181f216SUwe Kleine-König 	int err;
17428e64a68SDaniel Jeong 
1751181f216SUwe Kleine-König 	pchip->pwmd_state.period = pchip->pdata->pwm_period;
1761181f216SUwe Kleine-König 
1771181f216SUwe Kleine-König 	err = pwm_set_relative_duty_cycle(&pchip->pwmd_state, br, br_max);
1781181f216SUwe Kleine-König 	if (err)
1791181f216SUwe Kleine-König 		return err;
1801181f216SUwe Kleine-König 
1811181f216SUwe Kleine-König 	pchip->pwmd_state.enabled = pchip->pwmd_state.duty_cycle ? true : false;
1821181f216SUwe Kleine-König 
1831181f216SUwe Kleine-König 	return pwm_apply_state(pchip->pwmd, &pchip->pwmd_state);
18428e64a68SDaniel Jeong }
18528e64a68SDaniel Jeong 
18628e64a68SDaniel Jeong /* update and get brightness */
18728e64a68SDaniel Jeong static int lm3630a_bank_a_update_status(struct backlight_device *bl)
18828e64a68SDaniel Jeong {
18928e64a68SDaniel Jeong 	int ret;
19028e64a68SDaniel Jeong 	struct lm3630a_chip *pchip = bl_get_data(bl);
19128e64a68SDaniel Jeong 	enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
19228e64a68SDaniel Jeong 
19328e64a68SDaniel Jeong 	/* pwm control */
1941181f216SUwe Kleine-König 	if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0)
1951181f216SUwe Kleine-König 		return lm3630a_pwm_ctrl(pchip, bl->props.brightness,
19628e64a68SDaniel Jeong 					bl->props.max_brightness);
19728e64a68SDaniel Jeong 
19828e64a68SDaniel Jeong 	/* disable sleep */
19928e64a68SDaniel Jeong 	ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
20028e64a68SDaniel Jeong 	if (ret < 0)
20128e64a68SDaniel Jeong 		goto out_i2c_err;
20228e64a68SDaniel Jeong 	usleep_range(1000, 2000);
20328e64a68SDaniel Jeong 	/* minimum brightness is 0x04 */
20428e64a68SDaniel Jeong 	ret = lm3630a_write(pchip, REG_BRT_A, bl->props.brightness);
205ae9c4808SMaximilian Weigand 
206ae9c4808SMaximilian Weigand 	if (backlight_is_blank(bl) || (backlight_get_brightness(bl) < 0x4))
207ae9c4808SMaximilian Weigand 		/* turn the string off  */
20828e64a68SDaniel Jeong 		ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDA_ENABLE, 0);
20928e64a68SDaniel Jeong 	else
21028e64a68SDaniel Jeong 		ret |= lm3630a_update(pchip, REG_CTRL,
21128e64a68SDaniel Jeong 				      LM3630A_LEDA_ENABLE, LM3630A_LEDA_ENABLE);
21228e64a68SDaniel Jeong 	if (ret < 0)
21328e64a68SDaniel Jeong 		goto out_i2c_err;
214d3f48ec0SBrian Masney 	return 0;
21528e64a68SDaniel Jeong 
21628e64a68SDaniel Jeong out_i2c_err:
217b9481a66SUwe Kleine-König 	dev_err(pchip->dev, "i2c failed to access (%pe)\n", ERR_PTR(ret));
218b9481a66SUwe Kleine-König 	return ret;
21928e64a68SDaniel Jeong }
22028e64a68SDaniel Jeong 
22128e64a68SDaniel Jeong static int lm3630a_bank_a_get_brightness(struct backlight_device *bl)
22228e64a68SDaniel Jeong {
22328e64a68SDaniel Jeong 	int brightness, rval;
22428e64a68SDaniel Jeong 	struct lm3630a_chip *pchip = bl_get_data(bl);
22528e64a68SDaniel Jeong 	enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
22628e64a68SDaniel Jeong 
22728e64a68SDaniel Jeong 	if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
22828e64a68SDaniel Jeong 		rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
22928e64a68SDaniel Jeong 		if (rval < 0)
23028e64a68SDaniel Jeong 			goto out_i2c_err;
23128e64a68SDaniel Jeong 		brightness = (rval & 0x01) << 8;
23228e64a68SDaniel Jeong 		rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
23328e64a68SDaniel Jeong 		if (rval < 0)
23428e64a68SDaniel Jeong 			goto out_i2c_err;
23528e64a68SDaniel Jeong 		brightness |= rval;
23628e64a68SDaniel Jeong 		goto out;
23728e64a68SDaniel Jeong 	}
23828e64a68SDaniel Jeong 
23928e64a68SDaniel Jeong 	/* disable sleep */
24028e64a68SDaniel Jeong 	rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
24128e64a68SDaniel Jeong 	if (rval < 0)
24228e64a68SDaniel Jeong 		goto out_i2c_err;
24328e64a68SDaniel Jeong 	usleep_range(1000, 2000);
24428e64a68SDaniel Jeong 	rval = lm3630a_read(pchip, REG_BRT_A);
24528e64a68SDaniel Jeong 	if (rval < 0)
24628e64a68SDaniel Jeong 		goto out_i2c_err;
24728e64a68SDaniel Jeong 	brightness = rval;
24828e64a68SDaniel Jeong 
24928e64a68SDaniel Jeong out:
25028e64a68SDaniel Jeong 	bl->props.brightness = brightness;
25128e64a68SDaniel Jeong 	return bl->props.brightness;
25228e64a68SDaniel Jeong out_i2c_err:
25328e64a68SDaniel Jeong 	dev_err(pchip->dev, "i2c failed to access register\n");
25428e64a68SDaniel Jeong 	return 0;
25528e64a68SDaniel Jeong }
25628e64a68SDaniel Jeong 
25728e64a68SDaniel Jeong static const struct backlight_ops lm3630a_bank_a_ops = {
25828e64a68SDaniel Jeong 	.options = BL_CORE_SUSPENDRESUME,
25928e64a68SDaniel Jeong 	.update_status = lm3630a_bank_a_update_status,
26028e64a68SDaniel Jeong 	.get_brightness = lm3630a_bank_a_get_brightness,
26128e64a68SDaniel Jeong };
26228e64a68SDaniel Jeong 
26328e64a68SDaniel Jeong /* update and get brightness */
26428e64a68SDaniel Jeong static int lm3630a_bank_b_update_status(struct backlight_device *bl)
26528e64a68SDaniel Jeong {
26628e64a68SDaniel Jeong 	int ret;
26728e64a68SDaniel Jeong 	struct lm3630a_chip *pchip = bl_get_data(bl);
26828e64a68SDaniel Jeong 	enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
26928e64a68SDaniel Jeong 
27028e64a68SDaniel Jeong 	/* pwm control */
2711181f216SUwe Kleine-König 	if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0)
2721181f216SUwe Kleine-König 		return lm3630a_pwm_ctrl(pchip, bl->props.brightness,
27328e64a68SDaniel Jeong 					bl->props.max_brightness);
27428e64a68SDaniel Jeong 
27528e64a68SDaniel Jeong 	/* disable sleep */
27628e64a68SDaniel Jeong 	ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
27728e64a68SDaniel Jeong 	if (ret < 0)
27828e64a68SDaniel Jeong 		goto out_i2c_err;
27928e64a68SDaniel Jeong 	usleep_range(1000, 2000);
28028e64a68SDaniel Jeong 	/* minimum brightness is 0x04 */
28128e64a68SDaniel Jeong 	ret = lm3630a_write(pchip, REG_BRT_B, bl->props.brightness);
282ae9c4808SMaximilian Weigand 
283ae9c4808SMaximilian Weigand 	if (backlight_is_blank(bl) || (backlight_get_brightness(bl) < 0x4))
284ae9c4808SMaximilian Weigand 		/* turn the string off  */
28528e64a68SDaniel Jeong 		ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDB_ENABLE, 0);
28628e64a68SDaniel Jeong 	else
28728e64a68SDaniel Jeong 		ret |= lm3630a_update(pchip, REG_CTRL,
28828e64a68SDaniel Jeong 				      LM3630A_LEDB_ENABLE, LM3630A_LEDB_ENABLE);
28928e64a68SDaniel Jeong 	if (ret < 0)
29028e64a68SDaniel Jeong 		goto out_i2c_err;
291d3f48ec0SBrian Masney 	return 0;
29228e64a68SDaniel Jeong 
29328e64a68SDaniel Jeong out_i2c_err:
294b9481a66SUwe Kleine-König 	dev_err(pchip->dev, "i2c failed to access (%pe)\n", ERR_PTR(ret));
295b9481a66SUwe Kleine-König 	return ret;
29628e64a68SDaniel Jeong }
29728e64a68SDaniel Jeong 
29828e64a68SDaniel Jeong static int lm3630a_bank_b_get_brightness(struct backlight_device *bl)
29928e64a68SDaniel Jeong {
30028e64a68SDaniel Jeong 	int brightness, rval;
30128e64a68SDaniel Jeong 	struct lm3630a_chip *pchip = bl_get_data(bl);
30228e64a68SDaniel Jeong 	enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
30328e64a68SDaniel Jeong 
30428e64a68SDaniel Jeong 	if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
30528e64a68SDaniel Jeong 		rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
30628e64a68SDaniel Jeong 		if (rval < 0)
30728e64a68SDaniel Jeong 			goto out_i2c_err;
30828e64a68SDaniel Jeong 		brightness = (rval & 0x01) << 8;
30928e64a68SDaniel Jeong 		rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
31028e64a68SDaniel Jeong 		if (rval < 0)
31128e64a68SDaniel Jeong 			goto out_i2c_err;
31228e64a68SDaniel Jeong 		brightness |= rval;
31328e64a68SDaniel Jeong 		goto out;
31428e64a68SDaniel Jeong 	}
31528e64a68SDaniel Jeong 
31628e64a68SDaniel Jeong 	/* disable sleep */
31728e64a68SDaniel Jeong 	rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
31828e64a68SDaniel Jeong 	if (rval < 0)
31928e64a68SDaniel Jeong 		goto out_i2c_err;
32028e64a68SDaniel Jeong 	usleep_range(1000, 2000);
32128e64a68SDaniel Jeong 	rval = lm3630a_read(pchip, REG_BRT_B);
32228e64a68SDaniel Jeong 	if (rval < 0)
32328e64a68SDaniel Jeong 		goto out_i2c_err;
32428e64a68SDaniel Jeong 	brightness = rval;
32528e64a68SDaniel Jeong 
32628e64a68SDaniel Jeong out:
32728e64a68SDaniel Jeong 	bl->props.brightness = brightness;
32828e64a68SDaniel Jeong 	return bl->props.brightness;
32928e64a68SDaniel Jeong out_i2c_err:
33028e64a68SDaniel Jeong 	dev_err(pchip->dev, "i2c failed to access register\n");
33128e64a68SDaniel Jeong 	return 0;
33228e64a68SDaniel Jeong }
33328e64a68SDaniel Jeong 
33428e64a68SDaniel Jeong static const struct backlight_ops lm3630a_bank_b_ops = {
33528e64a68SDaniel Jeong 	.options = BL_CORE_SUSPENDRESUME,
33628e64a68SDaniel Jeong 	.update_status = lm3630a_bank_b_update_status,
33728e64a68SDaniel Jeong 	.get_brightness = lm3630a_bank_b_get_brightness,
33828e64a68SDaniel Jeong };
33928e64a68SDaniel Jeong 
34028e64a68SDaniel Jeong static int lm3630a_backlight_register(struct lm3630a_chip *pchip)
34128e64a68SDaniel Jeong {
34228e64a68SDaniel Jeong 	struct lm3630a_platform_data *pdata = pchip->pdata;
3438fbce8efSBrian Masney 	struct backlight_properties props;
3448fbce8efSBrian Masney 	const char *label;
34528e64a68SDaniel Jeong 
34628e64a68SDaniel Jeong 	props.type = BACKLIGHT_RAW;
34728e64a68SDaniel Jeong 	if (pdata->leda_ctrl != LM3630A_LEDA_DISABLE) {
34828e64a68SDaniel Jeong 		props.brightness = pdata->leda_init_brt;
34928e64a68SDaniel Jeong 		props.max_brightness = pdata->leda_max_brt;
3508fbce8efSBrian Masney 		label = pdata->leda_label ? pdata->leda_label : "lm3630a_leda";
35128e64a68SDaniel Jeong 		pchip->bleda =
3528fbce8efSBrian Masney 		    devm_backlight_device_register(pchip->dev, label,
35328e64a68SDaniel Jeong 						   pchip->dev, pchip,
35428e64a68SDaniel Jeong 						   &lm3630a_bank_a_ops, &props);
35528e64a68SDaniel Jeong 		if (IS_ERR(pchip->bleda))
35628e64a68SDaniel Jeong 			return PTR_ERR(pchip->bleda);
35728e64a68SDaniel Jeong 	}
35828e64a68SDaniel Jeong 
35928e64a68SDaniel Jeong 	if ((pdata->ledb_ctrl != LM3630A_LEDB_DISABLE) &&
36028e64a68SDaniel Jeong 	    (pdata->ledb_ctrl != LM3630A_LEDB_ON_A)) {
36128e64a68SDaniel Jeong 		props.brightness = pdata->ledb_init_brt;
36228e64a68SDaniel Jeong 		props.max_brightness = pdata->ledb_max_brt;
3638fbce8efSBrian Masney 		label = pdata->ledb_label ? pdata->ledb_label : "lm3630a_ledb";
36428e64a68SDaniel Jeong 		pchip->bledb =
3658fbce8efSBrian Masney 		    devm_backlight_device_register(pchip->dev, label,
36628e64a68SDaniel Jeong 						   pchip->dev, pchip,
36728e64a68SDaniel Jeong 						   &lm3630a_bank_b_ops, &props);
36828e64a68SDaniel Jeong 		if (IS_ERR(pchip->bledb))
36928e64a68SDaniel Jeong 			return PTR_ERR(pchip->bledb);
37028e64a68SDaniel Jeong 	}
37128e64a68SDaniel Jeong 	return 0;
37228e64a68SDaniel Jeong }
37328e64a68SDaniel Jeong 
37428e64a68SDaniel Jeong static const struct regmap_config lm3630a_regmap = {
37528e64a68SDaniel Jeong 	.reg_bits = 8,
37628e64a68SDaniel Jeong 	.val_bits = 8,
37728e64a68SDaniel Jeong 	.max_register = REG_MAX,
37828e64a68SDaniel Jeong };
37928e64a68SDaniel Jeong 
3808fbce8efSBrian Masney static int lm3630a_parse_led_sources(struct fwnode_handle *node,
3818fbce8efSBrian Masney 				     int default_led_sources)
3828fbce8efSBrian Masney {
3838fbce8efSBrian Masney 	u32 sources[LM3630A_NUM_SINKS];
3848fbce8efSBrian Masney 	int ret, num_sources, i;
3858fbce8efSBrian Masney 
386072e2c81SAndy Shevchenko 	num_sources = fwnode_property_count_u32(node, "led-sources");
3878fbce8efSBrian Masney 	if (num_sources < 0)
3888fbce8efSBrian Masney 		return default_led_sources;
3898fbce8efSBrian Masney 	else if (num_sources > ARRAY_SIZE(sources))
3908fbce8efSBrian Masney 		return -EINVAL;
3918fbce8efSBrian Masney 
3928fbce8efSBrian Masney 	ret = fwnode_property_read_u32_array(node, "led-sources", sources,
3938fbce8efSBrian Masney 					     num_sources);
3948fbce8efSBrian Masney 	if (ret)
3958fbce8efSBrian Masney 		return ret;
3968fbce8efSBrian Masney 
3978fbce8efSBrian Masney 	for (i = 0; i < num_sources; i++) {
3983e799ccdSLee Jones 		if (sources[i] != LM3630A_SINK_0 && sources[i] != LM3630A_SINK_1)
3998fbce8efSBrian Masney 			return -EINVAL;
4008fbce8efSBrian Masney 
4018fbce8efSBrian Masney 		ret |= BIT(sources[i]);
4028fbce8efSBrian Masney 	}
4038fbce8efSBrian Masney 
4048fbce8efSBrian Masney 	return ret;
4058fbce8efSBrian Masney }
4068fbce8efSBrian Masney 
4078fbce8efSBrian Masney static int lm3630a_parse_bank(struct lm3630a_platform_data *pdata,
4088fbce8efSBrian Masney 			      struct fwnode_handle *node, int *seen_led_sources)
4098fbce8efSBrian Masney {
4108fbce8efSBrian Masney 	int led_sources, ret;
4118fbce8efSBrian Masney 	const char *label;
4128fbce8efSBrian Masney 	u32 bank, val;
4138fbce8efSBrian Masney 	bool linear;
4148fbce8efSBrian Masney 
4158fbce8efSBrian Masney 	ret = fwnode_property_read_u32(node, "reg", &bank);
4168fbce8efSBrian Masney 	if (ret)
4178fbce8efSBrian Masney 		return ret;
4188fbce8efSBrian Masney 
4193e799ccdSLee Jones 	if (bank != LM3630A_BANK_0 && bank != LM3630A_BANK_1)
4208fbce8efSBrian Masney 		return -EINVAL;
4218fbce8efSBrian Masney 
4228fbce8efSBrian Masney 	led_sources = lm3630a_parse_led_sources(node, BIT(bank));
4238fbce8efSBrian Masney 	if (led_sources < 0)
4248fbce8efSBrian Masney 		return led_sources;
4258fbce8efSBrian Masney 
4268fbce8efSBrian Masney 	if (*seen_led_sources & led_sources)
4278fbce8efSBrian Masney 		return -EINVAL;
4288fbce8efSBrian Masney 
4298fbce8efSBrian Masney 	*seen_led_sources |= led_sources;
4308fbce8efSBrian Masney 
4318fbce8efSBrian Masney 	linear = fwnode_property_read_bool(node,
4328fbce8efSBrian Masney 					   "ti,linear-mapping-mode");
4338fbce8efSBrian Masney 	if (bank) {
4348fbce8efSBrian Masney 		if (led_sources & BIT(LM3630A_SINK_0) ||
4358fbce8efSBrian Masney 		    !(led_sources & BIT(LM3630A_SINK_1)))
4368fbce8efSBrian Masney 			return -EINVAL;
4378fbce8efSBrian Masney 
4388fbce8efSBrian Masney 		pdata->ledb_ctrl = linear ?
4398fbce8efSBrian Masney 			LM3630A_LEDB_ENABLE_LINEAR :
4408fbce8efSBrian Masney 			LM3630A_LEDB_ENABLE;
4418fbce8efSBrian Masney 	} else {
4428fbce8efSBrian Masney 		if (!(led_sources & BIT(LM3630A_SINK_0)))
4438fbce8efSBrian Masney 			return -EINVAL;
4448fbce8efSBrian Masney 
4458fbce8efSBrian Masney 		pdata->leda_ctrl = linear ?
4468fbce8efSBrian Masney 			LM3630A_LEDA_ENABLE_LINEAR :
4478fbce8efSBrian Masney 			LM3630A_LEDA_ENABLE;
4488fbce8efSBrian Masney 
4498fbce8efSBrian Masney 		if (led_sources & BIT(LM3630A_SINK_1))
4508fbce8efSBrian Masney 			pdata->ledb_ctrl = LM3630A_LEDB_ON_A;
4518fbce8efSBrian Masney 	}
4528fbce8efSBrian Masney 
4538fbce8efSBrian Masney 	ret = fwnode_property_read_string(node, "label", &label);
4548fbce8efSBrian Masney 	if (!ret) {
4558fbce8efSBrian Masney 		if (bank)
4568fbce8efSBrian Masney 			pdata->ledb_label = label;
4578fbce8efSBrian Masney 		else
4588fbce8efSBrian Masney 			pdata->leda_label = label;
4598fbce8efSBrian Masney 	}
4608fbce8efSBrian Masney 
4618fbce8efSBrian Masney 	ret = fwnode_property_read_u32(node, "default-brightness",
4628fbce8efSBrian Masney 				       &val);
4638fbce8efSBrian Masney 	if (!ret) {
4648fbce8efSBrian Masney 		if (bank)
4658fbce8efSBrian Masney 			pdata->ledb_init_brt = val;
4668fbce8efSBrian Masney 		else
4678fbce8efSBrian Masney 			pdata->leda_init_brt = val;
4688fbce8efSBrian Masney 	}
4698fbce8efSBrian Masney 
4708fbce8efSBrian Masney 	ret = fwnode_property_read_u32(node, "max-brightness", &val);
4718fbce8efSBrian Masney 	if (!ret) {
4728fbce8efSBrian Masney 		if (bank)
4738fbce8efSBrian Masney 			pdata->ledb_max_brt = val;
4748fbce8efSBrian Masney 		else
4758fbce8efSBrian Masney 			pdata->leda_max_brt = val;
4768fbce8efSBrian Masney 	}
4778fbce8efSBrian Masney 
4788fbce8efSBrian Masney 	return 0;
4798fbce8efSBrian Masney }
4808fbce8efSBrian Masney 
4818fbce8efSBrian Masney static int lm3630a_parse_node(struct lm3630a_chip *pchip,
4828fbce8efSBrian Masney 			      struct lm3630a_platform_data *pdata)
4838fbce8efSBrian Masney {
4848fbce8efSBrian Masney 	int ret = -ENODEV, seen_led_sources = 0;
4858fbce8efSBrian Masney 	struct fwnode_handle *node;
4868fbce8efSBrian Masney 
4878fbce8efSBrian Masney 	device_for_each_child_node(pchip->dev, node) {
4888fbce8efSBrian Masney 		ret = lm3630a_parse_bank(pdata, node, &seen_led_sources);
4896d1c32dbSAndy Shevchenko 		if (ret) {
4906d1c32dbSAndy Shevchenko 			fwnode_handle_put(node);
4918fbce8efSBrian Masney 			return ret;
4928fbce8efSBrian Masney 		}
4936d1c32dbSAndy Shevchenko 	}
4948fbce8efSBrian Masney 
4958fbce8efSBrian Masney 	return ret;
4968fbce8efSBrian Masney }
4978fbce8efSBrian Masney 
498b2d4f93fSUwe Kleine-König static int lm3630a_probe(struct i2c_client *client)
49928e64a68SDaniel Jeong {
500c512794cSJingoo Han 	struct lm3630a_platform_data *pdata = dev_get_platdata(&client->dev);
50128e64a68SDaniel Jeong 	struct lm3630a_chip *pchip;
50228e64a68SDaniel Jeong 	int rval;
50328e64a68SDaniel Jeong 
50428e64a68SDaniel Jeong 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
50528e64a68SDaniel Jeong 		dev_err(&client->dev, "fail : i2c functionality check\n");
50628e64a68SDaniel Jeong 		return -EOPNOTSUPP;
50728e64a68SDaniel Jeong 	}
50828e64a68SDaniel Jeong 
50928e64a68SDaniel Jeong 	pchip = devm_kzalloc(&client->dev, sizeof(struct lm3630a_chip),
51028e64a68SDaniel Jeong 			     GFP_KERNEL);
51128e64a68SDaniel Jeong 	if (!pchip)
51228e64a68SDaniel Jeong 		return -ENOMEM;
51328e64a68SDaniel Jeong 	pchip->dev = &client->dev;
51428e64a68SDaniel Jeong 
51528e64a68SDaniel Jeong 	pchip->regmap = devm_regmap_init_i2c(client, &lm3630a_regmap);
51628e64a68SDaniel Jeong 	if (IS_ERR(pchip->regmap)) {
51728e64a68SDaniel Jeong 		rval = PTR_ERR(pchip->regmap);
51828e64a68SDaniel Jeong 		dev_err(&client->dev, "fail : allocate reg. map: %d\n", rval);
51928e64a68SDaniel Jeong 		return rval;
52028e64a68SDaniel Jeong 	}
52128e64a68SDaniel Jeong 
52228e64a68SDaniel Jeong 	i2c_set_clientdata(client, pchip);
52328e64a68SDaniel Jeong 	if (pdata == NULL) {
524e19493c1SDan Carpenter 		pdata = devm_kzalloc(pchip->dev,
525e19493c1SDan Carpenter 				     sizeof(struct lm3630a_platform_data),
52628e64a68SDaniel Jeong 				     GFP_KERNEL);
527e19493c1SDan Carpenter 		if (pdata == NULL)
52828e64a68SDaniel Jeong 			return -ENOMEM;
5298fbce8efSBrian Masney 
53028e64a68SDaniel Jeong 		/* default values */
531e19493c1SDan Carpenter 		pdata->leda_max_brt = LM3630A_MAX_BRIGHTNESS;
532e19493c1SDan Carpenter 		pdata->ledb_max_brt = LM3630A_MAX_BRIGHTNESS;
533e19493c1SDan Carpenter 		pdata->leda_init_brt = LM3630A_MAX_BRIGHTNESS;
534e19493c1SDan Carpenter 		pdata->ledb_init_brt = LM3630A_MAX_BRIGHTNESS;
5358fbce8efSBrian Masney 
5368fbce8efSBrian Masney 		rval = lm3630a_parse_node(pchip, pdata);
5378fbce8efSBrian Masney 		if (rval) {
5388fbce8efSBrian Masney 			dev_err(&client->dev, "fail : parse node\n");
5398fbce8efSBrian Masney 			return rval;
5408fbce8efSBrian Masney 		}
54128e64a68SDaniel Jeong 	}
542e19493c1SDan Carpenter 	pchip->pdata = pdata;
543e19493c1SDan Carpenter 
5440e0e78e3SAndreas Kemnade 	pchip->enable_gpio = devm_gpiod_get_optional(&client->dev, "enable",
5450e0e78e3SAndreas Kemnade 						GPIOD_OUT_HIGH);
5460e0e78e3SAndreas Kemnade 	if (IS_ERR(pchip->enable_gpio)) {
5470e0e78e3SAndreas Kemnade 		rval = PTR_ERR(pchip->enable_gpio);
5480e0e78e3SAndreas Kemnade 		return rval;
5490e0e78e3SAndreas Kemnade 	}
5500e0e78e3SAndreas Kemnade 
55128e64a68SDaniel Jeong 	/* chip initialize */
55228e64a68SDaniel Jeong 	rval = lm3630a_chip_init(pchip);
55328e64a68SDaniel Jeong 	if (rval < 0) {
55428e64a68SDaniel Jeong 		dev_err(&client->dev, "fail : init chip\n");
55528e64a68SDaniel Jeong 		return rval;
55628e64a68SDaniel Jeong 	}
55728e64a68SDaniel Jeong 	/* backlight register */
55828e64a68SDaniel Jeong 	rval = lm3630a_backlight_register(pchip);
55928e64a68SDaniel Jeong 	if (rval < 0) {
56028e64a68SDaniel Jeong 		dev_err(&client->dev, "fail : backlight register.\n");
56128e64a68SDaniel Jeong 		return rval;
56228e64a68SDaniel Jeong 	}
56328e64a68SDaniel Jeong 	/* pwm */
56428e64a68SDaniel Jeong 	if (pdata->pwm_ctrl != LM3630A_PWM_DISABLE) {
56528e64a68SDaniel Jeong 		pchip->pwmd = devm_pwm_get(pchip->dev, "lm3630a-pwm");
56628e64a68SDaniel Jeong 		if (IS_ERR(pchip->pwmd)) {
56728e64a68SDaniel Jeong 			dev_err(&client->dev, "fail : get pwm device\n");
56828e64a68SDaniel Jeong 			return PTR_ERR(pchip->pwmd);
56928e64a68SDaniel Jeong 		}
5707ff666bcSBoris Brezillon 
5711181f216SUwe Kleine-König 		pwm_init_state(pchip->pwmd, &pchip->pwmd_state);
57228e64a68SDaniel Jeong 	}
57328e64a68SDaniel Jeong 
57428e64a68SDaniel Jeong 	/* interrupt enable  : irq 0 is not allowed */
57528e64a68SDaniel Jeong 	pchip->irq = client->irq;
57628e64a68SDaniel Jeong 	if (pchip->irq) {
57728e64a68SDaniel Jeong 		rval = lm3630a_intr_config(pchip);
57828e64a68SDaniel Jeong 		if (rval < 0)
57928e64a68SDaniel Jeong 			return rval;
58028e64a68SDaniel Jeong 	}
58128e64a68SDaniel Jeong 	dev_info(&client->dev, "LM3630A backlight register OK.\n");
58228e64a68SDaniel Jeong 	return 0;
58328e64a68SDaniel Jeong }
58428e64a68SDaniel Jeong 
585ed5c2f5fSUwe Kleine-König static void lm3630a_remove(struct i2c_client *client)
58628e64a68SDaniel Jeong {
58728e64a68SDaniel Jeong 	int rval;
58828e64a68SDaniel Jeong 	struct lm3630a_chip *pchip = i2c_get_clientdata(client);
58928e64a68SDaniel Jeong 
59028e64a68SDaniel Jeong 	rval = lm3630a_write(pchip, REG_BRT_A, 0);
59128e64a68SDaniel Jeong 	if (rval < 0)
59228e64a68SDaniel Jeong 		dev_err(pchip->dev, "i2c failed to access register\n");
59328e64a68SDaniel Jeong 
59428e64a68SDaniel Jeong 	rval = lm3630a_write(pchip, REG_BRT_B, 0);
59528e64a68SDaniel Jeong 	if (rval < 0)
59628e64a68SDaniel Jeong 		dev_err(pchip->dev, "i2c failed to access register\n");
59728e64a68SDaniel Jeong 
59828e64a68SDaniel Jeong 	if (pchip->irq) {
59928e64a68SDaniel Jeong 		free_irq(pchip->irq, pchip);
60028e64a68SDaniel Jeong 		destroy_workqueue(pchip->irqthread);
60128e64a68SDaniel Jeong 	}
60228e64a68SDaniel Jeong }
60328e64a68SDaniel Jeong 
60428e64a68SDaniel Jeong static const struct i2c_device_id lm3630a_id[] = {
60528e64a68SDaniel Jeong 	{LM3630A_NAME, 0},
60628e64a68SDaniel Jeong 	{}
60728e64a68SDaniel Jeong };
60828e64a68SDaniel Jeong 
6098ad003e7SAndreas Kemnade MODULE_DEVICE_TABLE(i2c, lm3630a_id);
6108ad003e7SAndreas Kemnade 
6118fbce8efSBrian Masney static const struct of_device_id lm3630a_match_table[] = {
6128fbce8efSBrian Masney 	{ .compatible = "ti,lm3630a", },
6138fbce8efSBrian Masney 	{ },
6148fbce8efSBrian Masney };
6158fbce8efSBrian Masney 
6168ad003e7SAndreas Kemnade MODULE_DEVICE_TABLE(of, lm3630a_match_table);
61728e64a68SDaniel Jeong 
61828e64a68SDaniel Jeong static struct i2c_driver lm3630a_i2c_driver = {
61928e64a68SDaniel Jeong 	.driver = {
62028e64a68SDaniel Jeong 		   .name = LM3630A_NAME,
6218fbce8efSBrian Masney 		   .of_match_table = lm3630a_match_table,
62228e64a68SDaniel Jeong 		   },
623*29554f2eSUwe Kleine-König 	.probe = lm3630a_probe,
62428e64a68SDaniel Jeong 	.remove = lm3630a_remove,
62528e64a68SDaniel Jeong 	.id_table = lm3630a_id,
62628e64a68SDaniel Jeong };
62728e64a68SDaniel Jeong 
62828e64a68SDaniel Jeong module_i2c_driver(lm3630a_i2c_driver);
62928e64a68SDaniel Jeong 
63028e64a68SDaniel Jeong MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
63128e64a68SDaniel Jeong MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
63228e64a68SDaniel Jeong MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
63328e64a68SDaniel Jeong MODULE_LICENSE("GPL v2");
634