xref: /openbmc/linux/drivers/regulator/lp3972.c (revision 9cdb81c7)
1 /*
2  * Regulator driver for National Semiconductors LP3972 PMIC chip
3  *
4  * Based on lp3971.c
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11 
12 #include <linux/bug.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/lp3972.h>
19 #include <linux/slab.h>
20 
21 struct lp3972 {
22 	struct device *dev;
23 	struct mutex io_lock;
24 	struct i2c_client *i2c;
25 	int num_regulators;
26 	struct regulator_dev **rdev;
27 };
28 
29 /* LP3972 Control Registers */
30 #define LP3972_SCR_REG		0x07
31 #define LP3972_OVER1_REG	0x10
32 #define LP3972_OVSR1_REG	0x11
33 #define LP3972_OVER2_REG	0x12
34 #define LP3972_OVSR2_REG	0x13
35 #define LP3972_VCC1_REG		0x20
36 #define LP3972_ADTV1_REG	0x23
37 #define LP3972_ADTV2_REG	0x24
38 #define LP3972_AVRC_REG		0x25
39 #define LP3972_CDTC1_REG	0x26
40 #define LP3972_CDTC2_REG	0x27
41 #define LP3972_SDTV1_REG	0x29
42 #define LP3972_SDTV2_REG	0x2A
43 #define LP3972_MDTV1_REG	0x32
44 #define LP3972_MDTV2_REG	0x33
45 #define LP3972_L2VCR_REG	0x39
46 #define LP3972_L34VCR_REG	0x3A
47 #define LP3972_SCR1_REG		0x80
48 #define LP3972_SCR2_REG		0x81
49 #define LP3972_OEN3_REG		0x82
50 #define LP3972_OSR3_REG		0x83
51 #define LP3972_LOER4_REG	0x84
52 #define LP3972_B2TV_REG		0x85
53 #define LP3972_B3TV_REG		0x86
54 #define LP3972_B32RC_REG	0x87
55 #define LP3972_ISRA_REG		0x88
56 #define LP3972_BCCR_REG		0x89
57 #define LP3972_II1RR_REG	0x8E
58 #define LP3972_II2RR_REG	0x8F
59 
60 #define LP3972_SYS_CONTROL1_REG		LP3972_SCR1_REG
61 /* System control register 1 initial value,
62  * bits 5, 6 and 7 are EPROM programmable */
63 #define SYS_CONTROL1_INIT_VAL		0x02
64 #define SYS_CONTROL1_INIT_MASK		0x1F
65 
66 #define LP3972_VOL_CHANGE_REG		LP3972_VCC1_REG
67 #define LP3972_VOL_CHANGE_FLAG_GO	0x01
68 #define LP3972_VOL_CHANGE_FLAG_MASK	0x03
69 
70 /* LDO output enable mask */
71 #define LP3972_OEN3_L1EN	BIT(0)
72 #define LP3972_OVER2_LDO2_EN	BIT(2)
73 #define LP3972_OVER2_LDO3_EN	BIT(3)
74 #define LP3972_OVER2_LDO4_EN	BIT(4)
75 #define LP3972_OVER1_S_EN	BIT(2)
76 
77 static const int ldo1_voltage_map[] = {
78 	1700, 1725, 1750, 1775, 1800, 1825, 1850, 1875,
79 	1900, 1925, 1950, 1975, 2000,
80 };
81 
82 static const int ldo23_voltage_map[] = {
83 	1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500,
84 	2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300,
85 };
86 
87 static const int ldo4_voltage_map[] = {
88 	1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350,
89 	1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300,
90 };
91 
92 static const int ldo5_voltage_map[] = {
93 	   0,    0,    0,    0,    0,  850,  875,  900,
94 	 925,  950,  975, 1000, 1025, 1050, 1075, 1100,
95 	1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
96 	1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
97 };
98 
99 static const int buck1_voltage_map[] = {
100 	 725,  750,  775,  800,  825,  850,  875,  900,
101 	 925,  950,  975, 1000, 1025, 1050, 1075, 1100,
102 	1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
103 	1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
104 };
105 
106 static const int buck23_voltage_map[] = {
107 	   0,  800,  850,  900,  950, 1000, 1050, 1100,
108 	1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500,
109 	1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800,
110 	3000, 3300,
111 };
112 
113 static const int *ldo_voltage_map[] = {
114 	ldo1_voltage_map,
115 	ldo23_voltage_map,
116 	ldo23_voltage_map,
117 	ldo4_voltage_map,
118 	ldo5_voltage_map,
119 };
120 
121 static const int *buck_voltage_map[] = {
122 	buck1_voltage_map,
123 	buck23_voltage_map,
124 	buck23_voltage_map,
125 };
126 
127 static const int ldo_output_enable_mask[] = {
128 	LP3972_OEN3_L1EN,
129 	LP3972_OVER2_LDO2_EN,
130 	LP3972_OVER2_LDO3_EN,
131 	LP3972_OVER2_LDO4_EN,
132 	LP3972_OVER1_S_EN,
133 };
134 
135 static const int ldo_output_enable_addr[] = {
136 	LP3972_OEN3_REG,
137 	LP3972_OVER2_REG,
138 	LP3972_OVER2_REG,
139 	LP3972_OVER2_REG,
140 	LP3972_OVER1_REG,
141 };
142 
143 static const int ldo_vol_ctl_addr[] = {
144 	LP3972_MDTV1_REG,
145 	LP3972_L2VCR_REG,
146 	LP3972_L34VCR_REG,
147 	LP3972_L34VCR_REG,
148 	LP3972_SDTV1_REG,
149 };
150 
151 static const int buck_vol_enable_addr[] = {
152 	LP3972_OVER1_REG,
153 	LP3972_OEN3_REG,
154 	LP3972_OEN3_REG,
155 };
156 
157 static const int buck_base_addr[] = {
158 	LP3972_ADTV1_REG,
159 	LP3972_B2TV_REG,
160 	LP3972_B3TV_REG,
161 };
162 
163 #define LP3972_LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[x])
164 #define LP3972_LDO_OUTPUT_ENABLE_MASK(x) (ldo_output_enable_mask[x])
165 #define LP3972_LDO_OUTPUT_ENABLE_REG(x) (ldo_output_enable_addr[x])
166 
167 /*	LDO voltage control registers shift:
168 	LP3972_LDO1 -> 0, LP3972_LDO2 -> 4
169 	LP3972_LDO3 -> 0, LP3972_LDO4 -> 4
170 	LP3972_LDO5 -> 0
171 */
172 #define LP3972_LDO_VOL_CONTR_SHIFT(x) (((x) & 1) << 2)
173 #define LP3972_LDO_VOL_CONTR_REG(x) (ldo_vol_ctl_addr[x])
174 #define LP3972_LDO_VOL_CHANGE_SHIFT(x) ((x) ? 4 : 6)
175 
176 #define LP3972_LDO_VOL_MASK(x) (((x) % 4) ? 0x0f : 0x1f)
177 #define LP3972_LDO_VOL_MIN_IDX(x) (((x) == 4) ? 0x05 : 0x00)
178 #define LP3972_LDO_VOL_MAX_IDX(x) ((x) ? (((x) == 4) ? 0x1f : 0x0f) : 0x0c)
179 
180 #define LP3972_BUCK_VOL_VALUE_MAP(x) (buck_voltage_map[x])
181 #define LP3972_BUCK_VOL_ENABLE_REG(x) (buck_vol_enable_addr[x])
182 #define LP3972_BUCK_VOL1_REG(x) (buck_base_addr[x])
183 #define LP3972_BUCK_VOL_MASK 0x1f
184 #define LP3972_BUCK_VOL_MIN_IDX(x) ((x) ? 0x01 : 0x00)
185 #define LP3972_BUCK_VOL_MAX_IDX(x) ((x) ? 0x19 : 0x1f)
186 
187 static int lp3972_i2c_read(struct i2c_client *i2c, char reg, int count,
188 	u16 *dest)
189 {
190 	int ret;
191 
192 	if (count != 1)
193 		return -EIO;
194 	ret = i2c_smbus_read_byte_data(i2c, reg);
195 	if (ret < 0)
196 		return ret;
197 
198 	*dest = ret;
199 	return 0;
200 }
201 
202 static int lp3972_i2c_write(struct i2c_client *i2c, char reg, int count,
203 	const u16 *src)
204 {
205 	if (count != 1)
206 		return -EIO;
207 	return i2c_smbus_write_byte_data(i2c, reg, *src);
208 }
209 
210 static u8 lp3972_reg_read(struct lp3972 *lp3972, u8 reg)
211 {
212 	u16 val = 0;
213 
214 	mutex_lock(&lp3972->io_lock);
215 
216 	lp3972_i2c_read(lp3972->i2c, reg, 1, &val);
217 
218 	dev_dbg(lp3972->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
219 		(unsigned)val & 0xff);
220 
221 	mutex_unlock(&lp3972->io_lock);
222 
223 	return val & 0xff;
224 }
225 
226 static int lp3972_set_bits(struct lp3972 *lp3972, u8 reg, u16 mask, u16 val)
227 {
228 	u16 tmp;
229 	int ret;
230 
231 	mutex_lock(&lp3972->io_lock);
232 
233 	ret = lp3972_i2c_read(lp3972->i2c, reg, 1, &tmp);
234 	tmp = (tmp & ~mask) | val;
235 	if (ret == 0) {
236 		ret = lp3972_i2c_write(lp3972->i2c, reg, 1, &tmp);
237 		dev_dbg(lp3972->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
238 			(unsigned)val & 0xff);
239 	}
240 	mutex_unlock(&lp3972->io_lock);
241 
242 	return ret;
243 }
244 
245 static int lp3972_ldo_list_voltage(struct regulator_dev *dev, unsigned index)
246 {
247 	int ldo = rdev_get_id(dev) - LP3972_LDO1;
248 	return 1000 * LP3972_LDO_VOL_VALUE_MAP(ldo)[index];
249 }
250 
251 static int lp3972_ldo_is_enabled(struct regulator_dev *dev)
252 {
253 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
254 	int ldo = rdev_get_id(dev) - LP3972_LDO1;
255 	u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
256 	u16 val;
257 
258 	val = lp3972_reg_read(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo));
259 	return !!(val & mask);
260 }
261 
262 static int lp3972_ldo_enable(struct regulator_dev *dev)
263 {
264 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
265 	int ldo = rdev_get_id(dev) - LP3972_LDO1;
266 	u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
267 
268 	return lp3972_set_bits(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo),
269 				mask, mask);
270 }
271 
272 static int lp3972_ldo_disable(struct regulator_dev *dev)
273 {
274 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
275 	int ldo = rdev_get_id(dev) - LP3972_LDO1;
276 	u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
277 
278 	return lp3972_set_bits(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo),
279 				mask, 0);
280 }
281 
282 static int lp3972_ldo_get_voltage(struct regulator_dev *dev)
283 {
284 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
285 	int ldo = rdev_get_id(dev) - LP3972_LDO1;
286 	u16 mask = LP3972_LDO_VOL_MASK(ldo);
287 	u16 val, reg;
288 
289 	reg = lp3972_reg_read(lp3972, LP3972_LDO_VOL_CONTR_REG(ldo));
290 	val = (reg >> LP3972_LDO_VOL_CONTR_SHIFT(ldo)) & mask;
291 
292 	return 1000 * LP3972_LDO_VOL_VALUE_MAP(ldo)[val];
293 }
294 
295 static int lp3972_ldo_set_voltage(struct regulator_dev *dev,
296 				  int min_uV, int max_uV,
297 				  unsigned int *selector)
298 {
299 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
300 	int ldo = rdev_get_id(dev) - LP3972_LDO1;
301 	int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
302 	const int *vol_map = LP3972_LDO_VOL_VALUE_MAP(ldo);
303 	u16 val;
304 	int shift, ret;
305 
306 	if (min_vol < vol_map[LP3972_LDO_VOL_MIN_IDX(ldo)] ||
307 	    min_vol > vol_map[LP3972_LDO_VOL_MAX_IDX(ldo)])
308 		return -EINVAL;
309 
310 	for (val = LP3972_LDO_VOL_MIN_IDX(ldo);
311 		val <= LP3972_LDO_VOL_MAX_IDX(ldo); val++)
312 		if (vol_map[val] >= min_vol)
313 			break;
314 
315 	if (val > LP3972_LDO_VOL_MAX_IDX(ldo) || vol_map[val] > max_vol)
316 		return -EINVAL;
317 
318 	*selector = val;
319 
320 	shift = LP3972_LDO_VOL_CONTR_SHIFT(ldo);
321 	ret = lp3972_set_bits(lp3972, LP3972_LDO_VOL_CONTR_REG(ldo),
322 		LP3972_LDO_VOL_MASK(ldo) << shift, val << shift);
323 
324 	if (ret)
325 		return ret;
326 
327 	/*
328 	 * LDO1 and LDO5 support voltage control by either target voltage1
329 	 * or target voltage2 register.
330 	 * We use target voltage1 register for LDO1 and LDO5 in this driver.
331 	 * We need to update voltage change control register(0x20) to enable
332 	 * LDO1 and LDO5 to change to their programmed target values.
333 	 */
334 	switch (ldo) {
335 	case LP3972_LDO1:
336 	case LP3972_LDO5:
337 		shift = LP3972_LDO_VOL_CHANGE_SHIFT(ldo);
338 		ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
339 			LP3972_VOL_CHANGE_FLAG_MASK << shift,
340 			LP3972_VOL_CHANGE_FLAG_GO << shift);
341 		if (ret)
342 			return ret;
343 
344 		ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
345 			LP3972_VOL_CHANGE_FLAG_MASK << shift, 0);
346 		break;
347 	}
348 
349 	return ret;
350 }
351 
352 static struct regulator_ops lp3972_ldo_ops = {
353 	.list_voltage = lp3972_ldo_list_voltage,
354 	.is_enabled = lp3972_ldo_is_enabled,
355 	.enable = lp3972_ldo_enable,
356 	.disable = lp3972_ldo_disable,
357 	.get_voltage = lp3972_ldo_get_voltage,
358 	.set_voltage = lp3972_ldo_set_voltage,
359 };
360 
361 static int lp3972_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
362 {
363 	int buck = rdev_get_id(dev) - LP3972_DCDC1;
364 	return 1000 * buck_voltage_map[buck][index];
365 }
366 
367 static int lp3972_dcdc_is_enabled(struct regulator_dev *dev)
368 {
369 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
370 	int buck = rdev_get_id(dev) - LP3972_DCDC1;
371 	u16 mask = 1 << (buck * 2);
372 	u16 val;
373 
374 	val = lp3972_reg_read(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck));
375 	return !!(val & mask);
376 }
377 
378 static int lp3972_dcdc_enable(struct regulator_dev *dev)
379 {
380 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
381 	int buck = rdev_get_id(dev) - LP3972_DCDC1;
382 	u16 mask = 1 << (buck * 2);
383 	u16 val;
384 
385 	val = lp3972_set_bits(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck),
386 				mask, mask);
387 	return val;
388 }
389 
390 static int lp3972_dcdc_disable(struct regulator_dev *dev)
391 {
392 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
393 	int buck = rdev_get_id(dev) - LP3972_DCDC1;
394 	u16 mask = 1 << (buck * 2);
395 	u16 val;
396 
397 	val = lp3972_set_bits(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck),
398 				mask, 0);
399 	return val;
400 }
401 
402 static int lp3972_dcdc_get_voltage(struct regulator_dev *dev)
403 {
404 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
405 	int buck = rdev_get_id(dev) - LP3972_DCDC1;
406 	u16 reg;
407 	int val;
408 
409 	reg = lp3972_reg_read(lp3972, LP3972_BUCK_VOL1_REG(buck));
410 	reg &= LP3972_BUCK_VOL_MASK;
411 	if (reg <= LP3972_BUCK_VOL_MAX_IDX(buck))
412 		val = 1000 * buck_voltage_map[buck][reg];
413 	else {
414 		val = 0;
415 		dev_warn(&dev->dev, "chip reported incorrect voltage value."
416 				    " reg = %d\n", reg);
417 	}
418 
419 	return val;
420 }
421 
422 static int lp3972_dcdc_set_voltage(struct regulator_dev *dev,
423 				   int min_uV, int max_uV,
424 				   unsigned int *selector)
425 {
426 	struct lp3972 *lp3972 = rdev_get_drvdata(dev);
427 	int buck = rdev_get_id(dev) - LP3972_DCDC1;
428 	int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
429 	const int *vol_map = buck_voltage_map[buck];
430 	u16 val;
431 	int ret;
432 
433 	if (min_vol < vol_map[LP3972_BUCK_VOL_MIN_IDX(buck)] ||
434 	    min_vol > vol_map[LP3972_BUCK_VOL_MAX_IDX(buck)])
435 		return -EINVAL;
436 
437 	for (val = LP3972_BUCK_VOL_MIN_IDX(buck);
438 		val <= LP3972_BUCK_VOL_MAX_IDX(buck); val++)
439 		if (vol_map[val] >= min_vol)
440 			break;
441 
442 	if (val > LP3972_BUCK_VOL_MAX_IDX(buck) ||
443 	    vol_map[val] > max_vol)
444 		return -EINVAL;
445 
446 	*selector = val;
447 
448 	ret = lp3972_set_bits(lp3972, LP3972_BUCK_VOL1_REG(buck),
449 				LP3972_BUCK_VOL_MASK, val);
450 	if (ret)
451 		return ret;
452 
453 	if (buck != 0)
454 		return ret;
455 
456 	ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
457 		LP3972_VOL_CHANGE_FLAG_MASK, LP3972_VOL_CHANGE_FLAG_GO);
458 	if (ret)
459 		return ret;
460 
461 	return lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
462 				LP3972_VOL_CHANGE_FLAG_MASK, 0);
463 }
464 
465 static struct regulator_ops lp3972_dcdc_ops = {
466 	.list_voltage = lp3972_dcdc_list_voltage,
467 	.is_enabled = lp3972_dcdc_is_enabled,
468 	.enable = lp3972_dcdc_enable,
469 	.disable = lp3972_dcdc_disable,
470 	.get_voltage = lp3972_dcdc_get_voltage,
471 	.set_voltage = lp3972_dcdc_set_voltage,
472 };
473 
474 static struct regulator_desc regulators[] = {
475 	{
476 		.name = "LDO1",
477 		.id = LP3972_LDO1,
478 		.ops = &lp3972_ldo_ops,
479 		.n_voltages = ARRAY_SIZE(ldo1_voltage_map),
480 		.type = REGULATOR_VOLTAGE,
481 		.owner = THIS_MODULE,
482 	},
483 	{
484 		.name = "LDO2",
485 		.id = LP3972_LDO2,
486 		.ops = &lp3972_ldo_ops,
487 		.n_voltages = ARRAY_SIZE(ldo23_voltage_map),
488 		.type = REGULATOR_VOLTAGE,
489 		.owner = THIS_MODULE,
490 	},
491 	{
492 		.name = "LDO3",
493 		.id = LP3972_LDO3,
494 		.ops = &lp3972_ldo_ops,
495 		.n_voltages = ARRAY_SIZE(ldo23_voltage_map),
496 		.type = REGULATOR_VOLTAGE,
497 		.owner = THIS_MODULE,
498 	},
499 	{
500 		.name = "LDO4",
501 		.id = LP3972_LDO4,
502 		.ops = &lp3972_ldo_ops,
503 		.n_voltages = ARRAY_SIZE(ldo4_voltage_map),
504 		.type = REGULATOR_VOLTAGE,
505 		.owner = THIS_MODULE,
506 	},
507 	{
508 		.name = "LDO5",
509 		.id = LP3972_LDO5,
510 		.ops = &lp3972_ldo_ops,
511 		.n_voltages = ARRAY_SIZE(ldo5_voltage_map),
512 		.type = REGULATOR_VOLTAGE,
513 		.owner = THIS_MODULE,
514 	},
515 	{
516 		.name = "DCDC1",
517 		.id = LP3972_DCDC1,
518 		.ops = &lp3972_dcdc_ops,
519 		.n_voltages = ARRAY_SIZE(buck1_voltage_map),
520 		.type = REGULATOR_VOLTAGE,
521 		.owner = THIS_MODULE,
522 	},
523 	{
524 		.name = "DCDC2",
525 		.id = LP3972_DCDC2,
526 		.ops = &lp3972_dcdc_ops,
527 		.n_voltages = ARRAY_SIZE(buck23_voltage_map),
528 		.type = REGULATOR_VOLTAGE,
529 		.owner = THIS_MODULE,
530 	},
531 	{
532 		.name = "DCDC3",
533 		.id = LP3972_DCDC3,
534 		.ops = &lp3972_dcdc_ops,
535 		.n_voltages = ARRAY_SIZE(buck23_voltage_map),
536 		.type = REGULATOR_VOLTAGE,
537 		.owner = THIS_MODULE,
538 	},
539 };
540 
541 static int __devinit setup_regulators(struct lp3972 *lp3972,
542 	struct lp3972_platform_data *pdata)
543 {
544 	int i, err;
545 
546 	lp3972->num_regulators = pdata->num_regulators;
547 	lp3972->rdev = kcalloc(pdata->num_regulators,
548 				sizeof(struct regulator_dev *), GFP_KERNEL);
549 	if (!lp3972->rdev) {
550 		err = -ENOMEM;
551 		goto err_nomem;
552 	}
553 
554 	/* Instantiate the regulators */
555 	for (i = 0; i < pdata->num_regulators; i++) {
556 		struct lp3972_regulator_subdev *reg = &pdata->regulators[i];
557 		lp3972->rdev[i] = regulator_register(&regulators[reg->id],
558 				lp3972->dev, reg->initdata, lp3972, NULL);
559 
560 		if (IS_ERR(lp3972->rdev[i])) {
561 			err = PTR_ERR(lp3972->rdev[i]);
562 			dev_err(lp3972->dev, "regulator init failed: %d\n",
563 				err);
564 			goto error;
565 		}
566 	}
567 
568 	return 0;
569 error:
570 	while (--i >= 0)
571 		regulator_unregister(lp3972->rdev[i]);
572 	kfree(lp3972->rdev);
573 	lp3972->rdev = NULL;
574 err_nomem:
575 	return err;
576 }
577 
578 static int __devinit lp3972_i2c_probe(struct i2c_client *i2c,
579 			    const struct i2c_device_id *id)
580 {
581 	struct lp3972 *lp3972;
582 	struct lp3972_platform_data *pdata = i2c->dev.platform_data;
583 	int ret;
584 	u16 val;
585 
586 	if (!pdata) {
587 		dev_dbg(&i2c->dev, "No platform init data supplied\n");
588 		return -ENODEV;
589 	}
590 
591 	lp3972 = kzalloc(sizeof(struct lp3972), GFP_KERNEL);
592 	if (!lp3972)
593 		return -ENOMEM;
594 
595 	lp3972->i2c = i2c;
596 	lp3972->dev = &i2c->dev;
597 
598 	mutex_init(&lp3972->io_lock);
599 
600 	/* Detect LP3972 */
601 	ret = lp3972_i2c_read(i2c, LP3972_SYS_CONTROL1_REG, 1, &val);
602 	if (ret == 0 &&
603 		(val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL) {
604 		ret = -ENODEV;
605 		dev_err(&i2c->dev, "chip reported: val = 0x%x\n", val);
606 	}
607 	if (ret < 0) {
608 		dev_err(&i2c->dev, "failed to detect device. ret = %d\n", ret);
609 		goto err_detect;
610 	}
611 
612 	ret = setup_regulators(lp3972, pdata);
613 	if (ret < 0)
614 		goto err_detect;
615 
616 	i2c_set_clientdata(i2c, lp3972);
617 	return 0;
618 
619 err_detect:
620 	kfree(lp3972);
621 	return ret;
622 }
623 
624 static int __devexit lp3972_i2c_remove(struct i2c_client *i2c)
625 {
626 	struct lp3972 *lp3972 = i2c_get_clientdata(i2c);
627 	int i;
628 
629 	for (i = 0; i < lp3972->num_regulators; i++)
630 		regulator_unregister(lp3972->rdev[i]);
631 	kfree(lp3972->rdev);
632 	kfree(lp3972);
633 
634 	return 0;
635 }
636 
637 static const struct i2c_device_id lp3972_i2c_id[] = {
638 	{ "lp3972", 0 },
639 	{ }
640 };
641 MODULE_DEVICE_TABLE(i2c, lp3972_i2c_id);
642 
643 static struct i2c_driver lp3972_i2c_driver = {
644 	.driver = {
645 		.name = "lp3972",
646 		.owner = THIS_MODULE,
647 	},
648 	.probe    = lp3972_i2c_probe,
649 	.remove   = __devexit_p(lp3972_i2c_remove),
650 	.id_table = lp3972_i2c_id,
651 };
652 
653 static int __init lp3972_module_init(void)
654 {
655 	return i2c_add_driver(&lp3972_i2c_driver);
656 }
657 subsys_initcall(lp3972_module_init);
658 
659 static void __exit lp3972_module_exit(void)
660 {
661 	i2c_del_driver(&lp3972_i2c_driver);
662 }
663 module_exit(lp3972_module_exit);
664 
665 MODULE_LICENSE("GPL");
666 MODULE_AUTHOR("Axel Lin <axel.lin@gmail.com>");
667 MODULE_DESCRIPTION("LP3972 PMIC driver");
668