blob: 0baabcfb578afb728d86b3a6eb84c5abe04801c6 [file] [log] [blame]
Axel Lin5976f092010-09-16 16:48:49 +08001/*
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>
Paul Gortmaker65602c32011-07-17 16:28:23 -040015#include <linux/module.h>
Axel Lin5976f092010-09-16 16:48:49 +080016#include <linux/kernel.h>
17#include <linux/regulator/driver.h>
18#include <linux/regulator/lp3972.h>
19#include <linux/slab.h>
20
21struct 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
Axel Lin4f73cca2012-05-20 10:36:17 +080077static const unsigned int ldo1_voltage_map[] = {
78 1700000, 1725000, 1750000, 1775000, 1800000, 1825000, 1850000, 1875000,
79 1900000, 1925000, 1950000, 1975000, 2000000,
Axel Lin5976f092010-09-16 16:48:49 +080080};
81
Axel Lin4f73cca2012-05-20 10:36:17 +080082static const unsigned int ldo23_voltage_map[] = {
83 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
84 2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
Axel Lin5976f092010-09-16 16:48:49 +080085};
86
Axel Lin4f73cca2012-05-20 10:36:17 +080087static const unsigned int ldo4_voltage_map[] = {
88 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000,
89 1400000, 1500000, 1800000, 1900000, 2500000, 2800000, 3000000, 3300000,
Axel Lin5976f092010-09-16 16:48:49 +080090};
91
Axel Lin4f73cca2012-05-20 10:36:17 +080092static const unsigned int ldo5_voltage_map[] = {
93 0, 0, 0, 0, 0, 850000, 875000, 900000,
94 925000, 950000, 975000, 1000000, 1025000, 1050000, 1075000, 1100000,
95 1125000, 1150000, 1175000, 1200000, 1225000, 1250000, 1275000, 1300000,
96 1325000, 1350000, 1375000, 1400000, 1425000, 1450000, 1475000, 1500000,
Axel Lin5976f092010-09-16 16:48:49 +080097};
98
Axel Lin4f73cca2012-05-20 10:36:17 +080099static const unsigned int buck1_voltage_map[] = {
100 725000, 750000, 775000, 800000, 825000, 850000, 875000, 900000,
101 925000, 950000, 975000, 1000000, 1025000, 1050000, 1075000, 1100000,
102 1125000, 1150000, 1175000, 1200000, 1225000, 1250000, 1275000, 1300000,
103 1325000, 1350000, 1375000, 1400000, 1425000, 1450000, 1475000, 1500000,
Axel Lin5976f092010-09-16 16:48:49 +0800104};
105
Axel Lin4f73cca2012-05-20 10:36:17 +0800106static const unsigned int buck23_voltage_map[] = {
107 0, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
108 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
109 1550000, 1600000, 1650000, 1700000, 1800000, 1900000, 2500000, 2800000,
110 3000000, 3300000,
Axel Lin5976f092010-09-16 16:48:49 +0800111};
112
113static const int ldo_output_enable_mask[] = {
114 LP3972_OEN3_L1EN,
115 LP3972_OVER2_LDO2_EN,
116 LP3972_OVER2_LDO3_EN,
117 LP3972_OVER2_LDO4_EN,
118 LP3972_OVER1_S_EN,
119};
120
121static const int ldo_output_enable_addr[] = {
122 LP3972_OEN3_REG,
123 LP3972_OVER2_REG,
124 LP3972_OVER2_REG,
125 LP3972_OVER2_REG,
126 LP3972_OVER1_REG,
127};
128
129static const int ldo_vol_ctl_addr[] = {
130 LP3972_MDTV1_REG,
131 LP3972_L2VCR_REG,
132 LP3972_L34VCR_REG,
133 LP3972_L34VCR_REG,
134 LP3972_SDTV1_REG,
135};
136
137static const int buck_vol_enable_addr[] = {
138 LP3972_OVER1_REG,
139 LP3972_OEN3_REG,
140 LP3972_OEN3_REG,
141};
142
143static const int buck_base_addr[] = {
144 LP3972_ADTV1_REG,
145 LP3972_B2TV_REG,
146 LP3972_B3TV_REG,
147};
148
Axel Lin5976f092010-09-16 16:48:49 +0800149#define LP3972_LDO_OUTPUT_ENABLE_MASK(x) (ldo_output_enable_mask[x])
150#define LP3972_LDO_OUTPUT_ENABLE_REG(x) (ldo_output_enable_addr[x])
151
152/* LDO voltage control registers shift:
153 LP3972_LDO1 -> 0, LP3972_LDO2 -> 4
154 LP3972_LDO3 -> 0, LP3972_LDO4 -> 4
155 LP3972_LDO5 -> 0
156*/
157#define LP3972_LDO_VOL_CONTR_SHIFT(x) (((x) & 1) << 2)
158#define LP3972_LDO_VOL_CONTR_REG(x) (ldo_vol_ctl_addr[x])
159#define LP3972_LDO_VOL_CHANGE_SHIFT(x) ((x) ? 4 : 6)
160
161#define LP3972_LDO_VOL_MASK(x) (((x) % 4) ? 0x0f : 0x1f)
162#define LP3972_LDO_VOL_MIN_IDX(x) (((x) == 4) ? 0x05 : 0x00)
163#define LP3972_LDO_VOL_MAX_IDX(x) ((x) ? (((x) == 4) ? 0x1f : 0x0f) : 0x0c)
164
Axel Lin5976f092010-09-16 16:48:49 +0800165#define LP3972_BUCK_VOL_ENABLE_REG(x) (buck_vol_enable_addr[x])
166#define LP3972_BUCK_VOL1_REG(x) (buck_base_addr[x])
167#define LP3972_BUCK_VOL_MASK 0x1f
Axel Lin5976f092010-09-16 16:48:49 +0800168
169static int lp3972_i2c_read(struct i2c_client *i2c, char reg, int count,
170 u16 *dest)
171{
172 int ret;
173
174 if (count != 1)
175 return -EIO;
176 ret = i2c_smbus_read_byte_data(i2c, reg);
177 if (ret < 0)
Axel Lin993af7c2010-09-18 13:13:28 +0800178 return ret;
Axel Lin5976f092010-09-16 16:48:49 +0800179
180 *dest = ret;
181 return 0;
182}
183
184static int lp3972_i2c_write(struct i2c_client *i2c, char reg, int count,
185 const u16 *src)
186{
187 if (count != 1)
188 return -EIO;
189 return i2c_smbus_write_byte_data(i2c, reg, *src);
190}
191
192static u8 lp3972_reg_read(struct lp3972 *lp3972, u8 reg)
193{
194 u16 val = 0;
195
196 mutex_lock(&lp3972->io_lock);
197
198 lp3972_i2c_read(lp3972->i2c, reg, 1, &val);
199
200 dev_dbg(lp3972->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
Axel Lin993af7c2010-09-18 13:13:28 +0800201 (unsigned)val & 0xff);
Axel Lin5976f092010-09-16 16:48:49 +0800202
203 mutex_unlock(&lp3972->io_lock);
204
205 return val & 0xff;
206}
207
208static int lp3972_set_bits(struct lp3972 *lp3972, u8 reg, u16 mask, u16 val)
209{
210 u16 tmp;
211 int ret;
212
213 mutex_lock(&lp3972->io_lock);
214
215 ret = lp3972_i2c_read(lp3972->i2c, reg, 1, &tmp);
216 tmp = (tmp & ~mask) | val;
217 if (ret == 0) {
218 ret = lp3972_i2c_write(lp3972->i2c, reg, 1, &tmp);
219 dev_dbg(lp3972->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
Axel Lin993af7c2010-09-18 13:13:28 +0800220 (unsigned)val & 0xff);
Axel Lin5976f092010-09-16 16:48:49 +0800221 }
222 mutex_unlock(&lp3972->io_lock);
223
224 return ret;
225}
226
Axel Lin5976f092010-09-16 16:48:49 +0800227static int lp3972_ldo_is_enabled(struct regulator_dev *dev)
228{
229 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
230 int ldo = rdev_get_id(dev) - LP3972_LDO1;
231 u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
232 u16 val;
233
234 val = lp3972_reg_read(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo));
235 return !!(val & mask);
236}
237
238static int lp3972_ldo_enable(struct regulator_dev *dev)
239{
240 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
241 int ldo = rdev_get_id(dev) - LP3972_LDO1;
242 u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
243
244 return lp3972_set_bits(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo),
245 mask, mask);
246}
247
248static int lp3972_ldo_disable(struct regulator_dev *dev)
249{
250 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
251 int ldo = rdev_get_id(dev) - LP3972_LDO1;
252 u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
253
254 return lp3972_set_bits(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo),
255 mask, 0);
256}
257
Axel Linc8c14a32012-12-28 08:23:16 +0800258static int lp3972_ldo_get_voltage_sel(struct regulator_dev *dev)
Axel Lin5976f092010-09-16 16:48:49 +0800259{
260 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
261 int ldo = rdev_get_id(dev) - LP3972_LDO1;
262 u16 mask = LP3972_LDO_VOL_MASK(ldo);
263 u16 val, reg;
264
265 reg = lp3972_reg_read(lp3972, LP3972_LDO_VOL_CONTR_REG(ldo));
266 val = (reg >> LP3972_LDO_VOL_CONTR_SHIFT(ldo)) & mask;
267
Axel Linc8c14a32012-12-28 08:23:16 +0800268 return val;
Axel Lin5976f092010-09-16 16:48:49 +0800269}
270
Axel Lin24c896f2012-03-23 10:51:14 +0800271static int lp3972_ldo_set_voltage_sel(struct regulator_dev *dev,
272 unsigned int selector)
Axel Lin5976f092010-09-16 16:48:49 +0800273{
274 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
275 int ldo = rdev_get_id(dev) - LP3972_LDO1;
Axel Lin5976f092010-09-16 16:48:49 +0800276 int shift, ret;
277
Axel Lin5976f092010-09-16 16:48:49 +0800278 shift = LP3972_LDO_VOL_CONTR_SHIFT(ldo);
279 ret = lp3972_set_bits(lp3972, LP3972_LDO_VOL_CONTR_REG(ldo),
Axel Lin24c896f2012-03-23 10:51:14 +0800280 LP3972_LDO_VOL_MASK(ldo) << shift, selector << shift);
Axel Lin5976f092010-09-16 16:48:49 +0800281
282 if (ret)
283 return ret;
284
Axel Lin993af7c2010-09-18 13:13:28 +0800285 /*
286 * LDO1 and LDO5 support voltage control by either target voltage1
287 * or target voltage2 register.
288 * We use target voltage1 register for LDO1 and LDO5 in this driver.
289 * We need to update voltage change control register(0x20) to enable
290 * LDO1 and LDO5 to change to their programmed target values.
291 */
Axel Lin5976f092010-09-16 16:48:49 +0800292 switch (ldo) {
293 case LP3972_LDO1:
294 case LP3972_LDO5:
295 shift = LP3972_LDO_VOL_CHANGE_SHIFT(ldo);
296 ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
297 LP3972_VOL_CHANGE_FLAG_MASK << shift,
298 LP3972_VOL_CHANGE_FLAG_GO << shift);
299 if (ret)
300 return ret;
301
302 ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
303 LP3972_VOL_CHANGE_FLAG_MASK << shift, 0);
304 break;
305 }
306
307 return ret;
308}
309
310static struct regulator_ops lp3972_ldo_ops = {
Axel Lin4f73cca2012-05-20 10:36:17 +0800311 .list_voltage = regulator_list_voltage_table,
Axel Lin5976f092010-09-16 16:48:49 +0800312 .is_enabled = lp3972_ldo_is_enabled,
313 .enable = lp3972_ldo_enable,
314 .disable = lp3972_ldo_disable,
Axel Linc8c14a32012-12-28 08:23:16 +0800315 .get_voltage_sel = lp3972_ldo_get_voltage_sel,
Axel Lin24c896f2012-03-23 10:51:14 +0800316 .set_voltage_sel = lp3972_ldo_set_voltage_sel,
Axel Lin5976f092010-09-16 16:48:49 +0800317};
318
Axel Lin5976f092010-09-16 16:48:49 +0800319static int lp3972_dcdc_is_enabled(struct regulator_dev *dev)
320{
321 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
322 int buck = rdev_get_id(dev) - LP3972_DCDC1;
323 u16 mask = 1 << (buck * 2);
324 u16 val;
325
326 val = lp3972_reg_read(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck));
327 return !!(val & mask);
328}
329
330static int lp3972_dcdc_enable(struct regulator_dev *dev)
331{
332 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
333 int buck = rdev_get_id(dev) - LP3972_DCDC1;
334 u16 mask = 1 << (buck * 2);
335 u16 val;
336
337 val = lp3972_set_bits(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck),
338 mask, mask);
339 return val;
340}
341
342static int lp3972_dcdc_disable(struct regulator_dev *dev)
343{
344 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
345 int buck = rdev_get_id(dev) - LP3972_DCDC1;
346 u16 mask = 1 << (buck * 2);
347 u16 val;
348
349 val = lp3972_set_bits(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck),
350 mask, 0);
351 return val;
352}
353
Axel Linc8c14a32012-12-28 08:23:16 +0800354static int lp3972_dcdc_get_voltage_sel(struct regulator_dev *dev)
Axel Lin5976f092010-09-16 16:48:49 +0800355{
356 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
357 int buck = rdev_get_id(dev) - LP3972_DCDC1;
358 u16 reg;
Axel Lin5976f092010-09-16 16:48:49 +0800359
360 reg = lp3972_reg_read(lp3972, LP3972_BUCK_VOL1_REG(buck));
361 reg &= LP3972_BUCK_VOL_MASK;
Axel Lin5976f092010-09-16 16:48:49 +0800362
Axel Linc8c14a32012-12-28 08:23:16 +0800363 return reg;
Axel Lin5976f092010-09-16 16:48:49 +0800364}
365
Axel Lin24c896f2012-03-23 10:51:14 +0800366static int lp3972_dcdc_set_voltage_sel(struct regulator_dev *dev,
367 unsigned int selector)
Axel Lin5976f092010-09-16 16:48:49 +0800368{
369 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
370 int buck = rdev_get_id(dev) - LP3972_DCDC1;
Axel Lin5976f092010-09-16 16:48:49 +0800371 int ret;
372
Axel Lin5976f092010-09-16 16:48:49 +0800373 ret = lp3972_set_bits(lp3972, LP3972_BUCK_VOL1_REG(buck),
Axel Lin24c896f2012-03-23 10:51:14 +0800374 LP3972_BUCK_VOL_MASK, selector);
Axel Lin5976f092010-09-16 16:48:49 +0800375 if (ret)
376 return ret;
377
378 if (buck != 0)
379 return ret;
380
381 ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
382 LP3972_VOL_CHANGE_FLAG_MASK, LP3972_VOL_CHANGE_FLAG_GO);
383 if (ret)
384 return ret;
385
386 return lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
387 LP3972_VOL_CHANGE_FLAG_MASK, 0);
388}
389
390static struct regulator_ops lp3972_dcdc_ops = {
Axel Lin4f73cca2012-05-20 10:36:17 +0800391 .list_voltage = regulator_list_voltage_table,
Axel Lin5976f092010-09-16 16:48:49 +0800392 .is_enabled = lp3972_dcdc_is_enabled,
393 .enable = lp3972_dcdc_enable,
394 .disable = lp3972_dcdc_disable,
Axel Linc8c14a32012-12-28 08:23:16 +0800395 .get_voltage_sel = lp3972_dcdc_get_voltage_sel,
Axel Lin24c896f2012-03-23 10:51:14 +0800396 .set_voltage_sel = lp3972_dcdc_set_voltage_sel,
Axel Lin5976f092010-09-16 16:48:49 +0800397};
398
Axel Lin1bdcf112012-04-05 12:00:32 +0800399static const struct regulator_desc regulators[] = {
Axel Lin5976f092010-09-16 16:48:49 +0800400 {
401 .name = "LDO1",
402 .id = LP3972_LDO1,
403 .ops = &lp3972_ldo_ops,
404 .n_voltages = ARRAY_SIZE(ldo1_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800405 .volt_table = ldo1_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800406 .type = REGULATOR_VOLTAGE,
407 .owner = THIS_MODULE,
408 },
409 {
410 .name = "LDO2",
411 .id = LP3972_LDO2,
412 .ops = &lp3972_ldo_ops,
413 .n_voltages = ARRAY_SIZE(ldo23_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800414 .volt_table = ldo23_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800415 .type = REGULATOR_VOLTAGE,
416 .owner = THIS_MODULE,
417 },
418 {
419 .name = "LDO3",
420 .id = LP3972_LDO3,
421 .ops = &lp3972_ldo_ops,
422 .n_voltages = ARRAY_SIZE(ldo23_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800423 .volt_table = ldo23_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800424 .type = REGULATOR_VOLTAGE,
425 .owner = THIS_MODULE,
426 },
427 {
428 .name = "LDO4",
429 .id = LP3972_LDO4,
430 .ops = &lp3972_ldo_ops,
431 .n_voltages = ARRAY_SIZE(ldo4_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800432 .volt_table = ldo4_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800433 .type = REGULATOR_VOLTAGE,
434 .owner = THIS_MODULE,
435 },
436 {
437 .name = "LDO5",
438 .id = LP3972_LDO5,
439 .ops = &lp3972_ldo_ops,
440 .n_voltages = ARRAY_SIZE(ldo5_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800441 .volt_table = ldo5_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800442 .type = REGULATOR_VOLTAGE,
443 .owner = THIS_MODULE,
444 },
445 {
446 .name = "DCDC1",
447 .id = LP3972_DCDC1,
448 .ops = &lp3972_dcdc_ops,
449 .n_voltages = ARRAY_SIZE(buck1_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800450 .volt_table = buck1_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800451 .type = REGULATOR_VOLTAGE,
452 .owner = THIS_MODULE,
453 },
454 {
455 .name = "DCDC2",
456 .id = LP3972_DCDC2,
457 .ops = &lp3972_dcdc_ops,
458 .n_voltages = ARRAY_SIZE(buck23_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800459 .volt_table = buck23_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800460 .type = REGULATOR_VOLTAGE,
461 .owner = THIS_MODULE,
462 },
463 {
464 .name = "DCDC3",
465 .id = LP3972_DCDC3,
466 .ops = &lp3972_dcdc_ops,
467 .n_voltages = ARRAY_SIZE(buck23_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800468 .volt_table = buck23_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800469 .type = REGULATOR_VOLTAGE,
470 .owner = THIS_MODULE,
471 },
472};
473
Bill Pembertona5023572012-11-19 13:22:22 -0500474static int setup_regulators(struct lp3972 *lp3972,
Axel Lin5976f092010-09-16 16:48:49 +0800475 struct lp3972_platform_data *pdata)
476{
477 int i, err;
478
479 lp3972->num_regulators = pdata->num_regulators;
480 lp3972->rdev = kcalloc(pdata->num_regulators,
481 sizeof(struct regulator_dev *), GFP_KERNEL);
482 if (!lp3972->rdev) {
483 err = -ENOMEM;
484 goto err_nomem;
485 }
486
487 /* Instantiate the regulators */
488 for (i = 0; i < pdata->num_regulators; i++) {
489 struct lp3972_regulator_subdev *reg = &pdata->regulators[i];
Mark Brownc1727082012-04-04 00:50:22 +0100490 struct regulator_config config = { };
Axel Lin5976f092010-09-16 16:48:49 +0800491
Mark Brownc1727082012-04-04 00:50:22 +0100492 config.dev = lp3972->dev;
493 config.init_data = reg->initdata;
494 config.driver_data = lp3972;
495
496 lp3972->rdev[i] = regulator_register(&regulators[reg->id],
497 &config);
Axel Lin5976f092010-09-16 16:48:49 +0800498 if (IS_ERR(lp3972->rdev[i])) {
499 err = PTR_ERR(lp3972->rdev[i]);
500 dev_err(lp3972->dev, "regulator init failed: %d\n",
501 err);
502 goto error;
503 }
504 }
505
506 return 0;
507error:
508 while (--i >= 0)
509 regulator_unregister(lp3972->rdev[i]);
510 kfree(lp3972->rdev);
511 lp3972->rdev = NULL;
512err_nomem:
513 return err;
514}
515
Bill Pembertona5023572012-11-19 13:22:22 -0500516static int lp3972_i2c_probe(struct i2c_client *i2c,
Axel Lin5976f092010-09-16 16:48:49 +0800517 const struct i2c_device_id *id)
518{
519 struct lp3972 *lp3972;
520 struct lp3972_platform_data *pdata = i2c->dev.platform_data;
521 int ret;
522 u16 val;
523
524 if (!pdata) {
525 dev_dbg(&i2c->dev, "No platform init data supplied\n");
526 return -ENODEV;
527 }
528
529 lp3972 = kzalloc(sizeof(struct lp3972), GFP_KERNEL);
530 if (!lp3972)
531 return -ENOMEM;
532
533 lp3972->i2c = i2c;
534 lp3972->dev = &i2c->dev;
535
536 mutex_init(&lp3972->io_lock);
537
538 /* Detect LP3972 */
539 ret = lp3972_i2c_read(i2c, LP3972_SYS_CONTROL1_REG, 1, &val);
Axel Lin993af7c2010-09-18 13:13:28 +0800540 if (ret == 0 &&
541 (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL) {
Axel Lin5976f092010-09-16 16:48:49 +0800542 ret = -ENODEV;
Axel Lin993af7c2010-09-18 13:13:28 +0800543 dev_err(&i2c->dev, "chip reported: val = 0x%x\n", val);
544 }
Axel Lin5976f092010-09-16 16:48:49 +0800545 if (ret < 0) {
Axel Lin993af7c2010-09-18 13:13:28 +0800546 dev_err(&i2c->dev, "failed to detect device. ret = %d\n", ret);
Axel Lin5976f092010-09-16 16:48:49 +0800547 goto err_detect;
548 }
549
550 ret = setup_regulators(lp3972, pdata);
551 if (ret < 0)
552 goto err_detect;
553
554 i2c_set_clientdata(i2c, lp3972);
555 return 0;
556
557err_detect:
558 kfree(lp3972);
559 return ret;
560}
561
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500562static int lp3972_i2c_remove(struct i2c_client *i2c)
Axel Lin5976f092010-09-16 16:48:49 +0800563{
564 struct lp3972 *lp3972 = i2c_get_clientdata(i2c);
565 int i;
566
567 for (i = 0; i < lp3972->num_regulators; i++)
568 regulator_unregister(lp3972->rdev[i]);
569 kfree(lp3972->rdev);
570 kfree(lp3972);
571
572 return 0;
573}
574
575static const struct i2c_device_id lp3972_i2c_id[] = {
576 { "lp3972", 0 },
577 { }
578};
579MODULE_DEVICE_TABLE(i2c, lp3972_i2c_id);
580
581static struct i2c_driver lp3972_i2c_driver = {
582 .driver = {
583 .name = "lp3972",
584 .owner = THIS_MODULE,
585 },
586 .probe = lp3972_i2c_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500587 .remove = lp3972_i2c_remove,
Axel Lin5976f092010-09-16 16:48:49 +0800588 .id_table = lp3972_i2c_id,
589};
590
591static int __init lp3972_module_init(void)
592{
593 return i2c_add_driver(&lp3972_i2c_driver);
594}
595subsys_initcall(lp3972_module_init);
596
597static void __exit lp3972_module_exit(void)
598{
599 i2c_del_driver(&lp3972_i2c_driver);
600}
601module_exit(lp3972_module_exit);
602
603MODULE_LICENSE("GPL");
604MODULE_AUTHOR("Axel Lin <axel.lin@gmail.com>");
605MODULE_DESCRIPTION("LP3972 PMIC driver");