xref: /openbmc/linux/drivers/regulator/lp8755.c (revision e4d48f64)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b59320ccSDaniel Jeong /*
3b59320ccSDaniel Jeong  * LP8755 High Performance Power Management Unit : System Interface Driver
4b59320ccSDaniel Jeong  * (based on rev. 0.26)
5b59320ccSDaniel Jeong  * Copyright 2012 Texas Instruments
6b59320ccSDaniel Jeong  *
7b59320ccSDaniel Jeong  * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com>
8b59320ccSDaniel Jeong  */
9b59320ccSDaniel Jeong 
10b59320ccSDaniel Jeong #include <linux/module.h>
11b59320ccSDaniel Jeong #include <linux/slab.h>
12b59320ccSDaniel Jeong #include <linux/i2c.h>
13b59320ccSDaniel Jeong #include <linux/err.h>
14b59320ccSDaniel Jeong #include <linux/irq.h>
15b59320ccSDaniel Jeong #include <linux/interrupt.h>
16b59320ccSDaniel Jeong #include <linux/regmap.h>
17b59320ccSDaniel Jeong #include <linux/uaccess.h>
18b59320ccSDaniel Jeong #include <linux/regulator/driver.h>
19b59320ccSDaniel Jeong #include <linux/regulator/machine.h>
20b59320ccSDaniel Jeong #include <linux/platform_data/lp8755.h>
21b59320ccSDaniel Jeong 
22b59320ccSDaniel Jeong #define LP8755_REG_BUCK0	0x00
23b59320ccSDaniel Jeong #define LP8755_REG_BUCK1	0x03
24b59320ccSDaniel Jeong #define LP8755_REG_BUCK2	0x04
25b59320ccSDaniel Jeong #define LP8755_REG_BUCK3	0x01
26b59320ccSDaniel Jeong #define LP8755_REG_BUCK4	0x05
27b59320ccSDaniel Jeong #define LP8755_REG_BUCK5	0x02
28b59320ccSDaniel Jeong #define LP8755_REG_MAX		0xFF
29b59320ccSDaniel Jeong 
30b59320ccSDaniel Jeong #define LP8755_BUCK_EN_M	BIT(7)
31b59320ccSDaniel Jeong #define LP8755_BUCK_LINEAR_OUT_MAX	0x76
32b59320ccSDaniel Jeong #define LP8755_BUCK_VOUT_M	0x7F
33b59320ccSDaniel Jeong 
34b59320ccSDaniel Jeong struct lp8755_mphase {
35b59320ccSDaniel Jeong 	int nreg;
36b59320ccSDaniel Jeong 	int buck_num[LP8755_BUCK_MAX];
37b59320ccSDaniel Jeong };
38b59320ccSDaniel Jeong 
39b59320ccSDaniel Jeong struct lp8755_chip {
40b59320ccSDaniel Jeong 	struct device *dev;
41b59320ccSDaniel Jeong 	struct regmap *regmap;
42b59320ccSDaniel Jeong 	struct lp8755_platform_data *pdata;
43b59320ccSDaniel Jeong 
44b59320ccSDaniel Jeong 	int irq;
45b59320ccSDaniel Jeong 	unsigned int irqmask;
46b59320ccSDaniel Jeong 
47b59320ccSDaniel Jeong 	int mphase;
48b59320ccSDaniel Jeong 	struct regulator_dev *rdev[LP8755_BUCK_MAX];
49b59320ccSDaniel Jeong };
50b59320ccSDaniel Jeong 
lp8755_buck_enable_time(struct regulator_dev * rdev)51b59320ccSDaniel Jeong static int lp8755_buck_enable_time(struct regulator_dev *rdev)
52b59320ccSDaniel Jeong {
53b59320ccSDaniel Jeong 	int ret;
54b59320ccSDaniel Jeong 	unsigned int regval;
55b59320ccSDaniel Jeong 	enum lp8755_bucks id = rdev_get_id(rdev);
56b59320ccSDaniel Jeong 
574cf12735SAxel Lin 	ret = regmap_read(rdev->regmap, 0x12 + id, &regval);
58b59320ccSDaniel Jeong 	if (ret < 0) {
594cf12735SAxel Lin 		dev_err(&rdev->dev, "i2c access error %s\n", __func__);
60b59320ccSDaniel Jeong 		return ret;
61b59320ccSDaniel Jeong 	}
62b59320ccSDaniel Jeong 	return (regval & 0xff) * 100;
63b59320ccSDaniel Jeong }
64b59320ccSDaniel Jeong 
lp8755_buck_set_mode(struct regulator_dev * rdev,unsigned int mode)65b59320ccSDaniel Jeong static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
66b59320ccSDaniel Jeong {
67b59320ccSDaniel Jeong 	int ret;
68b59320ccSDaniel Jeong 	unsigned int regbval = 0x0;
69b59320ccSDaniel Jeong 	enum lp8755_bucks id = rdev_get_id(rdev);
70b59320ccSDaniel Jeong 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
71b59320ccSDaniel Jeong 
72b59320ccSDaniel Jeong 	switch (mode) {
73b59320ccSDaniel Jeong 	case REGULATOR_MODE_FAST:
74b59320ccSDaniel Jeong 		/* forced pwm mode */
75b59320ccSDaniel Jeong 		regbval = (0x01 << id);
76b59320ccSDaniel Jeong 		break;
77b59320ccSDaniel Jeong 	case REGULATOR_MODE_NORMAL:
78b59320ccSDaniel Jeong 		/* enable automatic pwm/pfm mode */
794cf12735SAxel Lin 		ret = regmap_update_bits(rdev->regmap, 0x08 + id, 0x20, 0x00);
80b59320ccSDaniel Jeong 		if (ret < 0)
81b59320ccSDaniel Jeong 			goto err_i2c;
82b59320ccSDaniel Jeong 		break;
83b59320ccSDaniel Jeong 	case REGULATOR_MODE_IDLE:
84b59320ccSDaniel Jeong 		/* enable automatic pwm/pfm/lppfm mode */
854cf12735SAxel Lin 		ret = regmap_update_bits(rdev->regmap, 0x08 + id, 0x20, 0x20);
86b59320ccSDaniel Jeong 		if (ret < 0)
87b59320ccSDaniel Jeong 			goto err_i2c;
88b59320ccSDaniel Jeong 
894cf12735SAxel Lin 		ret = regmap_update_bits(rdev->regmap, 0x10, 0x01, 0x01);
90b59320ccSDaniel Jeong 		if (ret < 0)
91b59320ccSDaniel Jeong 			goto err_i2c;
92b59320ccSDaniel Jeong 		break;
93b59320ccSDaniel Jeong 	default:
94b59320ccSDaniel Jeong 		dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
95b59320ccSDaniel Jeong 		/* forced pwm mode */
96b59320ccSDaniel Jeong 		regbval = (0x01 << id);
97b59320ccSDaniel Jeong 	}
98b59320ccSDaniel Jeong 
994cf12735SAxel Lin 	ret = regmap_update_bits(rdev->regmap, 0x06, 0x01 << id, regbval);
100b59320ccSDaniel Jeong 	if (ret < 0)
101b59320ccSDaniel Jeong 		goto err_i2c;
102b59320ccSDaniel Jeong 	return ret;
103b59320ccSDaniel Jeong err_i2c:
1044cf12735SAxel Lin 	dev_err(&rdev->dev, "i2c access error %s\n", __func__);
105b59320ccSDaniel Jeong 	return ret;
106b59320ccSDaniel Jeong }
107b59320ccSDaniel Jeong 
lp8755_buck_get_mode(struct regulator_dev * rdev)108b59320ccSDaniel Jeong static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
109b59320ccSDaniel Jeong {
110b59320ccSDaniel Jeong 	int ret;
111b59320ccSDaniel Jeong 	unsigned int regval;
112b59320ccSDaniel Jeong 	enum lp8755_bucks id = rdev_get_id(rdev);
113b59320ccSDaniel Jeong 
1144cf12735SAxel Lin 	ret = regmap_read(rdev->regmap, 0x06, &regval);
115b59320ccSDaniel Jeong 	if (ret < 0)
116b59320ccSDaniel Jeong 		goto err_i2c;
117b59320ccSDaniel Jeong 
118b59320ccSDaniel Jeong 	/* mode fast means forced pwm mode */
119b59320ccSDaniel Jeong 	if (regval & (0x01 << id))
120b59320ccSDaniel Jeong 		return REGULATOR_MODE_FAST;
121b59320ccSDaniel Jeong 
1224cf12735SAxel Lin 	ret = regmap_read(rdev->regmap, 0x08 + id, &regval);
123b59320ccSDaniel Jeong 	if (ret < 0)
124b59320ccSDaniel Jeong 		goto err_i2c;
125b59320ccSDaniel Jeong 
126b59320ccSDaniel Jeong 	/* mode idle means automatic pwm/pfm/lppfm mode */
127b59320ccSDaniel Jeong 	if (regval & 0x20)
128b59320ccSDaniel Jeong 		return REGULATOR_MODE_IDLE;
129b59320ccSDaniel Jeong 
130b59320ccSDaniel Jeong 	/* mode normal means automatic pwm/pfm mode */
131b59320ccSDaniel Jeong 	return REGULATOR_MODE_NORMAL;
132b59320ccSDaniel Jeong 
133b59320ccSDaniel Jeong err_i2c:
1344cf12735SAxel Lin 	dev_err(&rdev->dev, "i2c access error %s\n", __func__);
135b59320ccSDaniel Jeong 	return 0;
136b59320ccSDaniel Jeong }
137b59320ccSDaniel Jeong 
138fbd168cdSAxel Lin static const unsigned int lp8755_buck_ramp_table[] = {
139fbd168cdSAxel Lin 	30000, 15000, 7500, 3800, 1900, 940, 470, 230
140fbd168cdSAxel Lin };
141b59320ccSDaniel Jeong 
142d42797a4SBhumika Goyal static const struct regulator_ops lp8755_buck_ops = {
14356a942e9SMark Brown 	.map_voltage = regulator_map_voltage_linear,
144b59320ccSDaniel Jeong 	.list_voltage = regulator_list_voltage_linear,
145b59320ccSDaniel Jeong 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
146b59320ccSDaniel Jeong 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
147b59320ccSDaniel Jeong 	.enable = regulator_enable_regmap,
148b59320ccSDaniel Jeong 	.disable = regulator_disable_regmap,
149b59320ccSDaniel Jeong 	.is_enabled = regulator_is_enabled_regmap,
150b59320ccSDaniel Jeong 	.enable_time = lp8755_buck_enable_time,
151b59320ccSDaniel Jeong 	.set_mode = lp8755_buck_set_mode,
152b59320ccSDaniel Jeong 	.get_mode = lp8755_buck_get_mode,
153fbd168cdSAxel Lin 	.set_ramp_delay = regulator_set_ramp_delay_regmap,
154b59320ccSDaniel Jeong };
155b59320ccSDaniel Jeong 
156b59320ccSDaniel Jeong #define lp8755_rail(_id) "lp8755_buck"#_id
157b59320ccSDaniel Jeong #define lp8755_buck_init(_id)\
158b59320ccSDaniel Jeong {\
159b59320ccSDaniel Jeong 	.constraints = {\
160b59320ccSDaniel Jeong 		.name = lp8755_rail(_id),\
161b59320ccSDaniel Jeong 		.valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
162b59320ccSDaniel Jeong 		.min_uV = 500000,\
163b59320ccSDaniel Jeong 		.max_uV = 1675000,\
164b59320ccSDaniel Jeong 	},\
165b59320ccSDaniel Jeong }
166b59320ccSDaniel Jeong 
167b59320ccSDaniel Jeong static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = {
168510799eaSAxel Lin 	[LP8755_BUCK0] = lp8755_buck_init(0),
169510799eaSAxel Lin 	[LP8755_BUCK1] = lp8755_buck_init(1),
170510799eaSAxel Lin 	[LP8755_BUCK2] = lp8755_buck_init(2),
171510799eaSAxel Lin 	[LP8755_BUCK3] = lp8755_buck_init(3),
172510799eaSAxel Lin 	[LP8755_BUCK4] = lp8755_buck_init(4),
173510799eaSAxel Lin 	[LP8755_BUCK5] = lp8755_buck_init(5),
174b59320ccSDaniel Jeong };
175b59320ccSDaniel Jeong 
176b59320ccSDaniel Jeong static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
177510799eaSAxel Lin 	{ 3, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK5 } },
178510799eaSAxel Lin 	{ 6, { LP8755_BUCK0, LP8755_BUCK1, LP8755_BUCK2, LP8755_BUCK3,
179510799eaSAxel Lin 	       LP8755_BUCK4, LP8755_BUCK5 } },
180510799eaSAxel Lin 	{ 5, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK4,
181510799eaSAxel Lin 	       LP8755_BUCK5} },
182510799eaSAxel Lin 	{ 4, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK4, LP8755_BUCK5} },
183510799eaSAxel Lin 	{ 3, { LP8755_BUCK0, LP8755_BUCK4, LP8755_BUCK5} },
184510799eaSAxel Lin 	{ 2, { LP8755_BUCK0, LP8755_BUCK5} },
185510799eaSAxel Lin 	{ 1, { LP8755_BUCK0} },
186510799eaSAxel Lin 	{ 2, { LP8755_BUCK0, LP8755_BUCK3} },
187510799eaSAxel Lin 	{ 4, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK5} },
188b59320ccSDaniel Jeong };
189b59320ccSDaniel Jeong 
lp8755_init_data(struct lp8755_chip * pchip)190b59320ccSDaniel Jeong static int lp8755_init_data(struct lp8755_chip *pchip)
191b59320ccSDaniel Jeong {
192b59320ccSDaniel Jeong 	unsigned int regval;
193b59320ccSDaniel Jeong 	int ret, icnt, buck_num;
194b59320ccSDaniel Jeong 	struct lp8755_platform_data *pdata = pchip->pdata;
195b59320ccSDaniel Jeong 
196b59320ccSDaniel Jeong 	/* read back  muti-phase configuration */
1974cf12735SAxel Lin 	ret = regmap_read(pchip->regmap, 0x3D, &regval);
198b59320ccSDaniel Jeong 	if (ret < 0)
199b59320ccSDaniel Jeong 		goto out_i2c_error;
200cad877efSAxel Lin 	pchip->mphase = regval & 0x0F;
201b59320ccSDaniel Jeong 
202b59320ccSDaniel Jeong 	/* set default data based on multi-phase config */
203b59320ccSDaniel Jeong 	for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
204b59320ccSDaniel Jeong 		buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
205b59320ccSDaniel Jeong 		pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num];
206b59320ccSDaniel Jeong 	}
207b59320ccSDaniel Jeong 	return ret;
208b59320ccSDaniel Jeong 
209b59320ccSDaniel Jeong out_i2c_error:
21080aec6f5SColin Ian King 	dev_err(pchip->dev, "i2c access error %s\n", __func__);
211b59320ccSDaniel Jeong 	return ret;
212b59320ccSDaniel Jeong }
213b59320ccSDaniel Jeong 
214b59320ccSDaniel Jeong #define lp8755_buck_desc(_id)\
215b59320ccSDaniel Jeong {\
216b59320ccSDaniel Jeong 	.name = lp8755_rail(_id),\
217b59320ccSDaniel Jeong 	.id   = LP8755_BUCK##_id,\
218b59320ccSDaniel Jeong 	.ops  = &lp8755_buck_ops,\
219b59320ccSDaniel Jeong 	.n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
220b59320ccSDaniel Jeong 	.uV_step = 10000,\
221b59320ccSDaniel Jeong 	.min_uV = 500000,\
222b59320ccSDaniel Jeong 	.type = REGULATOR_VOLTAGE,\
223b59320ccSDaniel Jeong 	.owner = THIS_MODULE,\
224b59320ccSDaniel Jeong 	.enable_reg = LP8755_REG_BUCK##_id,\
225b59320ccSDaniel Jeong 	.enable_mask = LP8755_BUCK_EN_M,\
226b59320ccSDaniel Jeong 	.vsel_reg = LP8755_REG_BUCK##_id,\
227b59320ccSDaniel Jeong 	.vsel_mask = LP8755_BUCK_VOUT_M,\
228fbd168cdSAxel Lin 	.ramp_reg = (LP8755_BUCK##_id) + 0x7,\
229fbd168cdSAxel Lin 	.ramp_mask = 0x7,\
230fbd168cdSAxel Lin 	.ramp_delay_table = lp8755_buck_ramp_table,\
231fbd168cdSAxel Lin 	.n_ramp_values = ARRAY_SIZE(lp8755_buck_ramp_table),\
232b59320ccSDaniel Jeong }
233b59320ccSDaniel Jeong 
234367e90d1SAxel Lin static const struct regulator_desc lp8755_regulators[] = {
235b59320ccSDaniel Jeong 	lp8755_buck_desc(0),
236b59320ccSDaniel Jeong 	lp8755_buck_desc(1),
237b59320ccSDaniel Jeong 	lp8755_buck_desc(2),
238b59320ccSDaniel Jeong 	lp8755_buck_desc(3),
239b59320ccSDaniel Jeong 	lp8755_buck_desc(4),
240b59320ccSDaniel Jeong 	lp8755_buck_desc(5),
241b59320ccSDaniel Jeong };
242b59320ccSDaniel Jeong 
lp8755_regulator_init(struct lp8755_chip * pchip)243b59320ccSDaniel Jeong static int lp8755_regulator_init(struct lp8755_chip *pchip)
244b59320ccSDaniel Jeong {
245b59320ccSDaniel Jeong 	int ret, icnt, buck_num;
246b59320ccSDaniel Jeong 	struct lp8755_platform_data *pdata = pchip->pdata;
247b59320ccSDaniel Jeong 	struct regulator_config rconfig = { };
248b59320ccSDaniel Jeong 
249b59320ccSDaniel Jeong 	rconfig.regmap = pchip->regmap;
250b59320ccSDaniel Jeong 	rconfig.dev = pchip->dev;
251b59320ccSDaniel Jeong 	rconfig.driver_data = pchip;
252b59320ccSDaniel Jeong 
253b59320ccSDaniel Jeong 	for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
254b59320ccSDaniel Jeong 		buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
255b59320ccSDaniel Jeong 		rconfig.init_data = pdata->buck_data[buck_num];
256b59320ccSDaniel Jeong 		rconfig.of_node = pchip->dev->of_node;
257b59320ccSDaniel Jeong 		pchip->rdev[buck_num] =
25857135250SHimangi Saraogi 		    devm_regulator_register(pchip->dev,
25957135250SHimangi Saraogi 				    &lp8755_regulators[buck_num], &rconfig);
260b59320ccSDaniel Jeong 		if (IS_ERR(pchip->rdev[buck_num])) {
261b59320ccSDaniel Jeong 			ret = PTR_ERR(pchip->rdev[buck_num]);
262a1a41ab4SAxel Lin 			pchip->rdev[buck_num] = NULL;
263a1a41ab4SAxel Lin 			dev_err(pchip->dev, "regulator init failed: buck %d\n",
264a1a41ab4SAxel Lin 				buck_num);
26557135250SHimangi Saraogi 			return ret;
266b59320ccSDaniel Jeong 		}
267b59320ccSDaniel Jeong 	}
268b59320ccSDaniel Jeong 
269b59320ccSDaniel Jeong 	return 0;
270b59320ccSDaniel Jeong }
271b59320ccSDaniel Jeong 
lp8755_irq_handler(int irq,void * data)272b59320ccSDaniel Jeong static irqreturn_t lp8755_irq_handler(int irq, void *data)
273b59320ccSDaniel Jeong {
274b59320ccSDaniel Jeong 	int ret, icnt;
275b59320ccSDaniel Jeong 	unsigned int flag0, flag1;
276b59320ccSDaniel Jeong 	struct lp8755_chip *pchip = data;
277b59320ccSDaniel Jeong 
278b59320ccSDaniel Jeong 	/* read flag0 register */
2794cf12735SAxel Lin 	ret = regmap_read(pchip->regmap, 0x0D, &flag0);
280b59320ccSDaniel Jeong 	if (ret < 0)
281b59320ccSDaniel Jeong 		goto err_i2c;
282b59320ccSDaniel Jeong 	/* clear flag register to pull up int. pin */
2834cf12735SAxel Lin 	ret = regmap_write(pchip->regmap, 0x0D, 0x00);
284b59320ccSDaniel Jeong 	if (ret < 0)
285b59320ccSDaniel Jeong 		goto err_i2c;
286b59320ccSDaniel Jeong 
287b59320ccSDaniel Jeong 	/* sent power fault detection event to specific regulator */
2881200c60bSAxel Lin 	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
289b59320ccSDaniel Jeong 		if ((flag0 & (0x4 << icnt))
290b59320ccSDaniel Jeong 		    && (pchip->irqmask & (0x04 << icnt))
29189b2758cSSteve Twiss 		    && (pchip->rdev[icnt] != NULL)) {
292b59320ccSDaniel Jeong 			regulator_notifier_call_chain(pchip->rdev[icnt],
293b59320ccSDaniel Jeong 						      LP8755_EVENT_PWR_FAULT,
294b59320ccSDaniel Jeong 						      NULL);
29589b2758cSSteve Twiss 		}
296b59320ccSDaniel Jeong 
297b59320ccSDaniel Jeong 	/* read flag1 register */
2984cf12735SAxel Lin 	ret = regmap_read(pchip->regmap, 0x0E, &flag1);
299b59320ccSDaniel Jeong 	if (ret < 0)
300b59320ccSDaniel Jeong 		goto err_i2c;
301b59320ccSDaniel Jeong 	/* clear flag register to pull up int. pin */
3024cf12735SAxel Lin 	ret = regmap_write(pchip->regmap, 0x0E, 0x00);
303b59320ccSDaniel Jeong 	if (ret < 0)
304b59320ccSDaniel Jeong 		goto err_i2c;
305b59320ccSDaniel Jeong 
30648f1b4efSKrzysztof Kozlowski 	/* send OCP event to all regulator devices */
307b59320ccSDaniel Jeong 	if ((flag1 & 0x01) && (pchip->irqmask & 0x01))
308b59320ccSDaniel Jeong 		for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
30989b2758cSSteve Twiss 			if (pchip->rdev[icnt] != NULL) {
310b59320ccSDaniel Jeong 				regulator_notifier_call_chain(pchip->rdev[icnt],
311b59320ccSDaniel Jeong 							      LP8755_EVENT_OCP,
312b59320ccSDaniel Jeong 							      NULL);
31389b2758cSSteve Twiss 			}
314b59320ccSDaniel Jeong 
31548f1b4efSKrzysztof Kozlowski 	/* send OVP event to all regulator devices */
316b59320ccSDaniel Jeong 	if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
317b59320ccSDaniel Jeong 		for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
31889b2758cSSteve Twiss 			if (pchip->rdev[icnt] != NULL) {
319b59320ccSDaniel Jeong 				regulator_notifier_call_chain(pchip->rdev[icnt],
320b59320ccSDaniel Jeong 							      LP8755_EVENT_OVP,
321b59320ccSDaniel Jeong 							      NULL);
32289b2758cSSteve Twiss 			}
323b59320ccSDaniel Jeong 	return IRQ_HANDLED;
324b59320ccSDaniel Jeong 
325b59320ccSDaniel Jeong err_i2c:
32680aec6f5SColin Ian King 	dev_err(pchip->dev, "i2c access error %s\n", __func__);
327b59320ccSDaniel Jeong 	return IRQ_NONE;
328b59320ccSDaniel Jeong }
329b59320ccSDaniel Jeong 
lp8755_int_config(struct lp8755_chip * pchip)330b59320ccSDaniel Jeong static int lp8755_int_config(struct lp8755_chip *pchip)
331b59320ccSDaniel Jeong {
332b59320ccSDaniel Jeong 	int ret;
333b59320ccSDaniel Jeong 	unsigned int regval;
334b59320ccSDaniel Jeong 
335b59320ccSDaniel Jeong 	if (pchip->irq == 0) {
336b59320ccSDaniel Jeong 		dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
337b59320ccSDaniel Jeong 		return 0;
338b59320ccSDaniel Jeong 	}
339b59320ccSDaniel Jeong 
3404cf12735SAxel Lin 	ret = regmap_read(pchip->regmap, 0x0F, &regval);
341840499aaSAxel Lin 	if (ret < 0) {
34280aec6f5SColin Ian King 		dev_err(pchip->dev, "i2c access error %s\n", __func__);
343b59320ccSDaniel Jeong 		return ret;
344b59320ccSDaniel Jeong 	}
345b59320ccSDaniel Jeong 
346840499aaSAxel Lin 	pchip->irqmask = regval;
347840499aaSAxel Lin 	return devm_request_threaded_irq(pchip->dev, pchip->irq, NULL,
348840499aaSAxel Lin 					 lp8755_irq_handler,
349840499aaSAxel Lin 					 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
350840499aaSAxel Lin 					 "lp8755-irq", pchip);
351840499aaSAxel Lin }
352840499aaSAxel Lin 
353b59320ccSDaniel Jeong static const struct regmap_config lp8755_regmap = {
354b59320ccSDaniel Jeong 	.reg_bits = 8,
355b59320ccSDaniel Jeong 	.val_bits = 8,
356b59320ccSDaniel Jeong 	.max_register = LP8755_REG_MAX,
357b59320ccSDaniel Jeong };
358b59320ccSDaniel Jeong 
lp8755_probe(struct i2c_client * client)359cb28f74bSUwe Kleine-König static int lp8755_probe(struct i2c_client *client)
360b59320ccSDaniel Jeong {
361b59320ccSDaniel Jeong 	int ret, icnt;
362b59320ccSDaniel Jeong 	struct lp8755_chip *pchip;
363dff91d0bSJingoo Han 	struct lp8755_platform_data *pdata = dev_get_platdata(&client->dev);
364b59320ccSDaniel Jeong 
365b59320ccSDaniel Jeong 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
366b59320ccSDaniel Jeong 		dev_err(&client->dev, "i2c functionality check fail.\n");
367b59320ccSDaniel Jeong 		return -EOPNOTSUPP;
368b59320ccSDaniel Jeong 	}
369b59320ccSDaniel Jeong 
370b59320ccSDaniel Jeong 	pchip = devm_kzalloc(&client->dev,
371b59320ccSDaniel Jeong 			     sizeof(struct lp8755_chip), GFP_KERNEL);
372b59320ccSDaniel Jeong 	if (!pchip)
373b59320ccSDaniel Jeong 		return -ENOMEM;
374b59320ccSDaniel Jeong 
375b59320ccSDaniel Jeong 	pchip->dev = &client->dev;
376b59320ccSDaniel Jeong 	pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
377b59320ccSDaniel Jeong 	if (IS_ERR(pchip->regmap)) {
378b59320ccSDaniel Jeong 		ret = PTR_ERR(pchip->regmap);
379b59320ccSDaniel Jeong 		dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
380b59320ccSDaniel Jeong 		return ret;
381b59320ccSDaniel Jeong 	}
382b59320ccSDaniel Jeong 	i2c_set_clientdata(client, pchip);
383b59320ccSDaniel Jeong 
384b59320ccSDaniel Jeong 	if (pdata != NULL) {
385b59320ccSDaniel Jeong 		pchip->pdata = pdata;
386b59320ccSDaniel Jeong 		pchip->mphase = pdata->mphase;
387b59320ccSDaniel Jeong 	} else {
388b59320ccSDaniel Jeong 		pchip->pdata = devm_kzalloc(pchip->dev,
389b59320ccSDaniel Jeong 					    sizeof(struct lp8755_platform_data),
390b59320ccSDaniel Jeong 					    GFP_KERNEL);
391b59320ccSDaniel Jeong 		if (!pchip->pdata)
392b59320ccSDaniel Jeong 			return -ENOMEM;
393b59320ccSDaniel Jeong 		ret = lp8755_init_data(pchip);
394240a5291SAxel Lin 		if (ret < 0) {
395240a5291SAxel Lin 			dev_err(&client->dev, "fail to initialize chip\n");
396240a5291SAxel Lin 			return ret;
397240a5291SAxel Lin 		}
398b59320ccSDaniel Jeong 	}
399b59320ccSDaniel Jeong 
400b59320ccSDaniel Jeong 	ret = lp8755_regulator_init(pchip);
401240a5291SAxel Lin 	if (ret < 0) {
402240a5291SAxel Lin 		dev_err(&client->dev, "fail to initialize regulators\n");
40357135250SHimangi Saraogi 		goto err;
404240a5291SAxel Lin 	}
405b59320ccSDaniel Jeong 
406b59320ccSDaniel Jeong 	pchip->irq = client->irq;
407b59320ccSDaniel Jeong 	ret = lp8755_int_config(pchip);
408240a5291SAxel Lin 	if (ret < 0) {
409240a5291SAxel Lin 		dev_err(&client->dev, "fail to irq config\n");
41057135250SHimangi Saraogi 		goto err;
411240a5291SAxel Lin 	}
412b59320ccSDaniel Jeong 
413b59320ccSDaniel Jeong 	return ret;
414b59320ccSDaniel Jeong 
41557135250SHimangi Saraogi err:
416b59320ccSDaniel Jeong 	/* output disable */
4171200c60bSAxel Lin 	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
4184cf12735SAxel Lin 		regmap_write(pchip->regmap, icnt, 0x00);
419b59320ccSDaniel Jeong 
420b59320ccSDaniel Jeong 	return ret;
421b59320ccSDaniel Jeong }
422b59320ccSDaniel Jeong 
lp8755_remove(struct i2c_client * client)423ed5c2f5fSUwe Kleine-König static void lp8755_remove(struct i2c_client *client)
424b59320ccSDaniel Jeong {
425b59320ccSDaniel Jeong 	int icnt;
426b59320ccSDaniel Jeong 	struct lp8755_chip *pchip = i2c_get_clientdata(client);
427b59320ccSDaniel Jeong 
4281200c60bSAxel Lin 	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
4294cf12735SAxel Lin 		regmap_write(pchip->regmap, icnt, 0x00);
430b59320ccSDaniel Jeong }
431b59320ccSDaniel Jeong 
432b59320ccSDaniel Jeong static const struct i2c_device_id lp8755_id[] = {
433b59320ccSDaniel Jeong 	{LP8755_NAME, 0},
434b59320ccSDaniel Jeong 	{}
435b59320ccSDaniel Jeong };
436b59320ccSDaniel Jeong 
437b59320ccSDaniel Jeong MODULE_DEVICE_TABLE(i2c, lp8755_id);
438b59320ccSDaniel Jeong 
439b59320ccSDaniel Jeong static struct i2c_driver lp8755_i2c_driver = {
440b59320ccSDaniel Jeong 	.driver = {
441b59320ccSDaniel Jeong 		   .name = LP8755_NAME,
442259b93b2SDouglas Anderson 		   .probe_type = PROBE_PREFER_ASYNCHRONOUS,
443b59320ccSDaniel Jeong 		   },
444*964e1865SUwe Kleine-König 	.probe = lp8755_probe,
445a1a41ab4SAxel Lin 	.remove = lp8755_remove,
446b59320ccSDaniel Jeong 	.id_table = lp8755_id,
447b59320ccSDaniel Jeong };
448b59320ccSDaniel Jeong 
lp8755_init(void)449b59320ccSDaniel Jeong static int __init lp8755_init(void)
450b59320ccSDaniel Jeong {
451b59320ccSDaniel Jeong 	return i2c_add_driver(&lp8755_i2c_driver);
452b59320ccSDaniel Jeong }
453b59320ccSDaniel Jeong 
454b59320ccSDaniel Jeong subsys_initcall(lp8755_init);
455b59320ccSDaniel Jeong 
lp8755_exit(void)456b59320ccSDaniel Jeong static void __exit lp8755_exit(void)
457b59320ccSDaniel Jeong {
458b59320ccSDaniel Jeong 	i2c_del_driver(&lp8755_i2c_driver);
459b59320ccSDaniel Jeong }
460b59320ccSDaniel Jeong 
461b59320ccSDaniel Jeong module_exit(lp8755_exit);
462b59320ccSDaniel Jeong 
463b59320ccSDaniel Jeong MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
464b59320ccSDaniel Jeong MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
465b59320ccSDaniel Jeong MODULE_LICENSE("GPL v2");
466