blob: 2d276bbeedf27220fa6f8b2657546a7c3fa47277 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Axel Lin5976f092010-09-16 16:48:49 +08002/*
3 * Regulator driver for National Semiconductors LP3972 PMIC chip
4 *
5 * Based on lp3971.c
Axel Lin5976f092010-09-16 16:48:49 +08006 */
7
8#include <linux/bug.h>
9#include <linux/err.h>
10#include <linux/i2c.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040011#include <linux/module.h>
Axel Lin5976f092010-09-16 16:48:49 +080012#include <linux/kernel.h>
13#include <linux/regulator/driver.h>
14#include <linux/regulator/lp3972.h>
15#include <linux/slab.h>
16
17struct lp3972 {
18 struct device *dev;
19 struct mutex io_lock;
20 struct i2c_client *i2c;
Axel Lin5976f092010-09-16 16:48:49 +080021};
22
23/* LP3972 Control Registers */
24#define LP3972_SCR_REG 0x07
25#define LP3972_OVER1_REG 0x10
26#define LP3972_OVSR1_REG 0x11
27#define LP3972_OVER2_REG 0x12
28#define LP3972_OVSR2_REG 0x13
29#define LP3972_VCC1_REG 0x20
30#define LP3972_ADTV1_REG 0x23
31#define LP3972_ADTV2_REG 0x24
32#define LP3972_AVRC_REG 0x25
33#define LP3972_CDTC1_REG 0x26
34#define LP3972_CDTC2_REG 0x27
35#define LP3972_SDTV1_REG 0x29
36#define LP3972_SDTV2_REG 0x2A
37#define LP3972_MDTV1_REG 0x32
38#define LP3972_MDTV2_REG 0x33
39#define LP3972_L2VCR_REG 0x39
40#define LP3972_L34VCR_REG 0x3A
41#define LP3972_SCR1_REG 0x80
42#define LP3972_SCR2_REG 0x81
43#define LP3972_OEN3_REG 0x82
44#define LP3972_OSR3_REG 0x83
45#define LP3972_LOER4_REG 0x84
46#define LP3972_B2TV_REG 0x85
47#define LP3972_B3TV_REG 0x86
48#define LP3972_B32RC_REG 0x87
49#define LP3972_ISRA_REG 0x88
50#define LP3972_BCCR_REG 0x89
51#define LP3972_II1RR_REG 0x8E
52#define LP3972_II2RR_REG 0x8F
53
54#define LP3972_SYS_CONTROL1_REG LP3972_SCR1_REG
55/* System control register 1 initial value,
56 * bits 5, 6 and 7 are EPROM programmable */
57#define SYS_CONTROL1_INIT_VAL 0x02
58#define SYS_CONTROL1_INIT_MASK 0x1F
59
60#define LP3972_VOL_CHANGE_REG LP3972_VCC1_REG
61#define LP3972_VOL_CHANGE_FLAG_GO 0x01
62#define LP3972_VOL_CHANGE_FLAG_MASK 0x03
63
64/* LDO output enable mask */
65#define LP3972_OEN3_L1EN BIT(0)
66#define LP3972_OVER2_LDO2_EN BIT(2)
67#define LP3972_OVER2_LDO3_EN BIT(3)
68#define LP3972_OVER2_LDO4_EN BIT(4)
69#define LP3972_OVER1_S_EN BIT(2)
70
Axel Lin4f73cca2012-05-20 10:36:17 +080071static const unsigned int ldo1_voltage_map[] = {
72 1700000, 1725000, 1750000, 1775000, 1800000, 1825000, 1850000, 1875000,
73 1900000, 1925000, 1950000, 1975000, 2000000,
Axel Lin5976f092010-09-16 16:48:49 +080074};
75
Axel Lin4f73cca2012-05-20 10:36:17 +080076static const unsigned int ldo23_voltage_map[] = {
77 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
78 2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
Axel Lin5976f092010-09-16 16:48:49 +080079};
80
Axel Lin4f73cca2012-05-20 10:36:17 +080081static const unsigned int ldo4_voltage_map[] = {
82 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000,
83 1400000, 1500000, 1800000, 1900000, 2500000, 2800000, 3000000, 3300000,
Axel Lin5976f092010-09-16 16:48:49 +080084};
85
Axel Lin4f73cca2012-05-20 10:36:17 +080086static const unsigned int ldo5_voltage_map[] = {
87 0, 0, 0, 0, 0, 850000, 875000, 900000,
88 925000, 950000, 975000, 1000000, 1025000, 1050000, 1075000, 1100000,
89 1125000, 1150000, 1175000, 1200000, 1225000, 1250000, 1275000, 1300000,
90 1325000, 1350000, 1375000, 1400000, 1425000, 1450000, 1475000, 1500000,
Axel Lin5976f092010-09-16 16:48:49 +080091};
92
Axel Lin4f73cca2012-05-20 10:36:17 +080093static const unsigned int buck1_voltage_map[] = {
94 725000, 750000, 775000, 800000, 825000, 850000, 875000, 900000,
95 925000, 950000, 975000, 1000000, 1025000, 1050000, 1075000, 1100000,
96 1125000, 1150000, 1175000, 1200000, 1225000, 1250000, 1275000, 1300000,
97 1325000, 1350000, 1375000, 1400000, 1425000, 1450000, 1475000, 1500000,
Axel Lin5976f092010-09-16 16:48:49 +080098};
99
Axel Lin4f73cca2012-05-20 10:36:17 +0800100static const unsigned int buck23_voltage_map[] = {
101 0, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
102 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
103 1550000, 1600000, 1650000, 1700000, 1800000, 1900000, 2500000, 2800000,
104 3000000, 3300000,
Axel Lin5976f092010-09-16 16:48:49 +0800105};
106
107static const int ldo_output_enable_mask[] = {
108 LP3972_OEN3_L1EN,
109 LP3972_OVER2_LDO2_EN,
110 LP3972_OVER2_LDO3_EN,
111 LP3972_OVER2_LDO4_EN,
112 LP3972_OVER1_S_EN,
113};
114
115static const int ldo_output_enable_addr[] = {
116 LP3972_OEN3_REG,
117 LP3972_OVER2_REG,
118 LP3972_OVER2_REG,
119 LP3972_OVER2_REG,
120 LP3972_OVER1_REG,
121};
122
123static const int ldo_vol_ctl_addr[] = {
124 LP3972_MDTV1_REG,
125 LP3972_L2VCR_REG,
126 LP3972_L34VCR_REG,
127 LP3972_L34VCR_REG,
128 LP3972_SDTV1_REG,
129};
130
131static const int buck_vol_enable_addr[] = {
132 LP3972_OVER1_REG,
133 LP3972_OEN3_REG,
134 LP3972_OEN3_REG,
135};
136
137static const int buck_base_addr[] = {
138 LP3972_ADTV1_REG,
139 LP3972_B2TV_REG,
140 LP3972_B3TV_REG,
141};
142
Axel Lin5976f092010-09-16 16:48:49 +0800143#define LP3972_LDO_OUTPUT_ENABLE_MASK(x) (ldo_output_enable_mask[x])
144#define LP3972_LDO_OUTPUT_ENABLE_REG(x) (ldo_output_enable_addr[x])
145
146/* LDO voltage control registers shift:
147 LP3972_LDO1 -> 0, LP3972_LDO2 -> 4
148 LP3972_LDO3 -> 0, LP3972_LDO4 -> 4
149 LP3972_LDO5 -> 0
150*/
151#define LP3972_LDO_VOL_CONTR_SHIFT(x) (((x) & 1) << 2)
152#define LP3972_LDO_VOL_CONTR_REG(x) (ldo_vol_ctl_addr[x])
153#define LP3972_LDO_VOL_CHANGE_SHIFT(x) ((x) ? 4 : 6)
154
155#define LP3972_LDO_VOL_MASK(x) (((x) % 4) ? 0x0f : 0x1f)
156#define LP3972_LDO_VOL_MIN_IDX(x) (((x) == 4) ? 0x05 : 0x00)
157#define LP3972_LDO_VOL_MAX_IDX(x) ((x) ? (((x) == 4) ? 0x1f : 0x0f) : 0x0c)
158
Axel Lin5976f092010-09-16 16:48:49 +0800159#define LP3972_BUCK_VOL_ENABLE_REG(x) (buck_vol_enable_addr[x])
160#define LP3972_BUCK_VOL1_REG(x) (buck_base_addr[x])
161#define LP3972_BUCK_VOL_MASK 0x1f
Axel Lin5976f092010-09-16 16:48:49 +0800162
163static int lp3972_i2c_read(struct i2c_client *i2c, char reg, int count,
164 u16 *dest)
165{
166 int ret;
167
168 if (count != 1)
169 return -EIO;
170 ret = i2c_smbus_read_byte_data(i2c, reg);
171 if (ret < 0)
Axel Lin993af7c2010-09-18 13:13:28 +0800172 return ret;
Axel Lin5976f092010-09-16 16:48:49 +0800173
174 *dest = ret;
175 return 0;
176}
177
178static int lp3972_i2c_write(struct i2c_client *i2c, char reg, int count,
179 const u16 *src)
180{
181 if (count != 1)
182 return -EIO;
183 return i2c_smbus_write_byte_data(i2c, reg, *src);
184}
185
186static u8 lp3972_reg_read(struct lp3972 *lp3972, u8 reg)
187{
188 u16 val = 0;
189
190 mutex_lock(&lp3972->io_lock);
191
192 lp3972_i2c_read(lp3972->i2c, reg, 1, &val);
193
194 dev_dbg(lp3972->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
Axel Lin993af7c2010-09-18 13:13:28 +0800195 (unsigned)val & 0xff);
Axel Lin5976f092010-09-16 16:48:49 +0800196
197 mutex_unlock(&lp3972->io_lock);
198
199 return val & 0xff;
200}
201
202static int lp3972_set_bits(struct lp3972 *lp3972, u8 reg, u16 mask, u16 val)
203{
204 u16 tmp;
205 int ret;
206
207 mutex_lock(&lp3972->io_lock);
208
209 ret = lp3972_i2c_read(lp3972->i2c, reg, 1, &tmp);
Axel Lin5976f092010-09-16 16:48:49 +0800210 if (ret == 0) {
Dan Carpenter7cb73482016-04-14 12:34:07 +0300211 tmp = (tmp & ~mask) | val;
Axel Lin5976f092010-09-16 16:48:49 +0800212 ret = lp3972_i2c_write(lp3972->i2c, reg, 1, &tmp);
213 dev_dbg(lp3972->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
Axel Lin993af7c2010-09-18 13:13:28 +0800214 (unsigned)val & 0xff);
Axel Lin5976f092010-09-16 16:48:49 +0800215 }
216 mutex_unlock(&lp3972->io_lock);
217
218 return ret;
219}
220
Axel Lin5976f092010-09-16 16:48:49 +0800221static int lp3972_ldo_is_enabled(struct regulator_dev *dev)
222{
223 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
224 int ldo = rdev_get_id(dev) - LP3972_LDO1;
225 u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
226 u16 val;
227
228 val = lp3972_reg_read(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo));
229 return !!(val & mask);
230}
231
232static int lp3972_ldo_enable(struct regulator_dev *dev)
233{
234 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
235 int ldo = rdev_get_id(dev) - LP3972_LDO1;
236 u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
237
238 return lp3972_set_bits(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo),
239 mask, mask);
240}
241
242static int lp3972_ldo_disable(struct regulator_dev *dev)
243{
244 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
245 int ldo = rdev_get_id(dev) - LP3972_LDO1;
246 u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
247
248 return lp3972_set_bits(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo),
249 mask, 0);
250}
251
Axel Linc8c14a32012-12-28 08:23:16 +0800252static int lp3972_ldo_get_voltage_sel(struct regulator_dev *dev)
Axel Lin5976f092010-09-16 16:48:49 +0800253{
254 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
255 int ldo = rdev_get_id(dev) - LP3972_LDO1;
256 u16 mask = LP3972_LDO_VOL_MASK(ldo);
257 u16 val, reg;
258
259 reg = lp3972_reg_read(lp3972, LP3972_LDO_VOL_CONTR_REG(ldo));
260 val = (reg >> LP3972_LDO_VOL_CONTR_SHIFT(ldo)) & mask;
261
Axel Linc8c14a32012-12-28 08:23:16 +0800262 return val;
Axel Lin5976f092010-09-16 16:48:49 +0800263}
264
Axel Lin24c896f2012-03-23 10:51:14 +0800265static int lp3972_ldo_set_voltage_sel(struct regulator_dev *dev,
266 unsigned int selector)
Axel Lin5976f092010-09-16 16:48:49 +0800267{
268 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
269 int ldo = rdev_get_id(dev) - LP3972_LDO1;
Axel Lin5976f092010-09-16 16:48:49 +0800270 int shift, ret;
271
Axel Lin5976f092010-09-16 16:48:49 +0800272 shift = LP3972_LDO_VOL_CONTR_SHIFT(ldo);
273 ret = lp3972_set_bits(lp3972, LP3972_LDO_VOL_CONTR_REG(ldo),
Axel Lin24c896f2012-03-23 10:51:14 +0800274 LP3972_LDO_VOL_MASK(ldo) << shift, selector << shift);
Axel Lin5976f092010-09-16 16:48:49 +0800275
276 if (ret)
277 return ret;
278
Axel Lin993af7c2010-09-18 13:13:28 +0800279 /*
280 * LDO1 and LDO5 support voltage control by either target voltage1
281 * or target voltage2 register.
282 * We use target voltage1 register for LDO1 and LDO5 in this driver.
283 * We need to update voltage change control register(0x20) to enable
284 * LDO1 and LDO5 to change to their programmed target values.
285 */
Axel Lin5976f092010-09-16 16:48:49 +0800286 switch (ldo) {
287 case LP3972_LDO1:
288 case LP3972_LDO5:
289 shift = LP3972_LDO_VOL_CHANGE_SHIFT(ldo);
290 ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
291 LP3972_VOL_CHANGE_FLAG_MASK << shift,
292 LP3972_VOL_CHANGE_FLAG_GO << shift);
293 if (ret)
294 return ret;
295
296 ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
297 LP3972_VOL_CHANGE_FLAG_MASK << shift, 0);
298 break;
299 }
300
301 return ret;
302}
303
Axel Linf75b4c52019-01-26 11:39:01 +0800304static const struct regulator_ops lp3972_ldo_ops = {
Axel Lin4f73cca2012-05-20 10:36:17 +0800305 .list_voltage = regulator_list_voltage_table,
Axel Linb50003b2013-04-21 00:36:56 +0800306 .map_voltage = regulator_map_voltage_ascend,
Axel Lin5976f092010-09-16 16:48:49 +0800307 .is_enabled = lp3972_ldo_is_enabled,
308 .enable = lp3972_ldo_enable,
309 .disable = lp3972_ldo_disable,
Axel Linc8c14a32012-12-28 08:23:16 +0800310 .get_voltage_sel = lp3972_ldo_get_voltage_sel,
Axel Lin24c896f2012-03-23 10:51:14 +0800311 .set_voltage_sel = lp3972_ldo_set_voltage_sel,
Axel Lin5976f092010-09-16 16:48:49 +0800312};
313
Axel Lin5976f092010-09-16 16:48:49 +0800314static int lp3972_dcdc_is_enabled(struct regulator_dev *dev)
315{
316 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
317 int buck = rdev_get_id(dev) - LP3972_DCDC1;
318 u16 mask = 1 << (buck * 2);
319 u16 val;
320
321 val = lp3972_reg_read(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck));
322 return !!(val & mask);
323}
324
325static int lp3972_dcdc_enable(struct regulator_dev *dev)
326{
327 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
328 int buck = rdev_get_id(dev) - LP3972_DCDC1;
329 u16 mask = 1 << (buck * 2);
330 u16 val;
331
332 val = lp3972_set_bits(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck),
333 mask, mask);
334 return val;
335}
336
337static int lp3972_dcdc_disable(struct regulator_dev *dev)
338{
339 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
340 int buck = rdev_get_id(dev) - LP3972_DCDC1;
341 u16 mask = 1 << (buck * 2);
342 u16 val;
343
344 val = lp3972_set_bits(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck),
345 mask, 0);
346 return val;
347}
348
Axel Linc8c14a32012-12-28 08:23:16 +0800349static int lp3972_dcdc_get_voltage_sel(struct regulator_dev *dev)
Axel Lin5976f092010-09-16 16:48:49 +0800350{
351 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
352 int buck = rdev_get_id(dev) - LP3972_DCDC1;
353 u16 reg;
Axel Lin5976f092010-09-16 16:48:49 +0800354
355 reg = lp3972_reg_read(lp3972, LP3972_BUCK_VOL1_REG(buck));
356 reg &= LP3972_BUCK_VOL_MASK;
Axel Lin5976f092010-09-16 16:48:49 +0800357
Axel Linc8c14a32012-12-28 08:23:16 +0800358 return reg;
Axel Lin5976f092010-09-16 16:48:49 +0800359}
360
Axel Lin24c896f2012-03-23 10:51:14 +0800361static int lp3972_dcdc_set_voltage_sel(struct regulator_dev *dev,
362 unsigned int selector)
Axel Lin5976f092010-09-16 16:48:49 +0800363{
364 struct lp3972 *lp3972 = rdev_get_drvdata(dev);
365 int buck = rdev_get_id(dev) - LP3972_DCDC1;
Axel Lin5976f092010-09-16 16:48:49 +0800366 int ret;
367
Axel Lin5976f092010-09-16 16:48:49 +0800368 ret = lp3972_set_bits(lp3972, LP3972_BUCK_VOL1_REG(buck),
Axel Lin24c896f2012-03-23 10:51:14 +0800369 LP3972_BUCK_VOL_MASK, selector);
Axel Lin5976f092010-09-16 16:48:49 +0800370 if (ret)
371 return ret;
372
373 if (buck != 0)
374 return ret;
375
376 ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
377 LP3972_VOL_CHANGE_FLAG_MASK, LP3972_VOL_CHANGE_FLAG_GO);
378 if (ret)
379 return ret;
380
381 return lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
382 LP3972_VOL_CHANGE_FLAG_MASK, 0);
383}
384
Axel Linf75b4c52019-01-26 11:39:01 +0800385static const struct regulator_ops lp3972_dcdc_ops = {
Axel Lin4f73cca2012-05-20 10:36:17 +0800386 .list_voltage = regulator_list_voltage_table,
Axel Linb50003b2013-04-21 00:36:56 +0800387 .map_voltage = regulator_map_voltage_ascend,
Axel Lin5976f092010-09-16 16:48:49 +0800388 .is_enabled = lp3972_dcdc_is_enabled,
389 .enable = lp3972_dcdc_enable,
390 .disable = lp3972_dcdc_disable,
Axel Linc8c14a32012-12-28 08:23:16 +0800391 .get_voltage_sel = lp3972_dcdc_get_voltage_sel,
Axel Lin24c896f2012-03-23 10:51:14 +0800392 .set_voltage_sel = lp3972_dcdc_set_voltage_sel,
Axel Lin5976f092010-09-16 16:48:49 +0800393};
394
Axel Lin1bdcf112012-04-05 12:00:32 +0800395static const struct regulator_desc regulators[] = {
Axel Lin5976f092010-09-16 16:48:49 +0800396 {
397 .name = "LDO1",
398 .id = LP3972_LDO1,
399 .ops = &lp3972_ldo_ops,
400 .n_voltages = ARRAY_SIZE(ldo1_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800401 .volt_table = ldo1_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800402 .type = REGULATOR_VOLTAGE,
403 .owner = THIS_MODULE,
404 },
405 {
406 .name = "LDO2",
407 .id = LP3972_LDO2,
408 .ops = &lp3972_ldo_ops,
409 .n_voltages = ARRAY_SIZE(ldo23_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800410 .volt_table = ldo23_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800411 .type = REGULATOR_VOLTAGE,
412 .owner = THIS_MODULE,
413 },
414 {
415 .name = "LDO3",
416 .id = LP3972_LDO3,
417 .ops = &lp3972_ldo_ops,
418 .n_voltages = ARRAY_SIZE(ldo23_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800419 .volt_table = ldo23_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800420 .type = REGULATOR_VOLTAGE,
421 .owner = THIS_MODULE,
422 },
423 {
424 .name = "LDO4",
425 .id = LP3972_LDO4,
426 .ops = &lp3972_ldo_ops,
427 .n_voltages = ARRAY_SIZE(ldo4_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800428 .volt_table = ldo4_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800429 .type = REGULATOR_VOLTAGE,
430 .owner = THIS_MODULE,
431 },
432 {
433 .name = "LDO5",
434 .id = LP3972_LDO5,
435 .ops = &lp3972_ldo_ops,
436 .n_voltages = ARRAY_SIZE(ldo5_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800437 .volt_table = ldo5_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800438 .type = REGULATOR_VOLTAGE,
439 .owner = THIS_MODULE,
440 },
441 {
442 .name = "DCDC1",
443 .id = LP3972_DCDC1,
444 .ops = &lp3972_dcdc_ops,
445 .n_voltages = ARRAY_SIZE(buck1_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800446 .volt_table = buck1_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800447 .type = REGULATOR_VOLTAGE,
448 .owner = THIS_MODULE,
449 },
450 {
451 .name = "DCDC2",
452 .id = LP3972_DCDC2,
453 .ops = &lp3972_dcdc_ops,
454 .n_voltages = ARRAY_SIZE(buck23_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800455 .volt_table = buck23_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800456 .type = REGULATOR_VOLTAGE,
457 .owner = THIS_MODULE,
458 },
459 {
460 .name = "DCDC3",
461 .id = LP3972_DCDC3,
462 .ops = &lp3972_dcdc_ops,
463 .n_voltages = ARRAY_SIZE(buck23_voltage_map),
Axel Lin4f73cca2012-05-20 10:36:17 +0800464 .volt_table = buck23_voltage_map,
Axel Lin5976f092010-09-16 16:48:49 +0800465 .type = REGULATOR_VOLTAGE,
466 .owner = THIS_MODULE,
467 },
468};
469
Bill Pembertona5023572012-11-19 13:22:22 -0500470static int setup_regulators(struct lp3972 *lp3972,
Axel Lin5976f092010-09-16 16:48:49 +0800471 struct lp3972_platform_data *pdata)
472{
473 int i, err;
474
Axel Lin5976f092010-09-16 16:48:49 +0800475 /* Instantiate the regulators */
476 for (i = 0; i < pdata->num_regulators; i++) {
477 struct lp3972_regulator_subdev *reg = &pdata->regulators[i];
Mark Brownc172708d2012-04-04 00:50:22 +0100478 struct regulator_config config = { };
Axel Lin93227c82013-11-13 14:55:14 +0800479 struct regulator_dev *rdev;
Axel Lin5976f092010-09-16 16:48:49 +0800480
Mark Brownc172708d2012-04-04 00:50:22 +0100481 config.dev = lp3972->dev;
482 config.init_data = reg->initdata;
483 config.driver_data = lp3972;
484
Axel Lin93227c82013-11-13 14:55:14 +0800485 rdev = devm_regulator_register(lp3972->dev,
486 &regulators[reg->id], &config);
487 if (IS_ERR(rdev)) {
488 err = PTR_ERR(rdev);
Axel Lin5976f092010-09-16 16:48:49 +0800489 dev_err(lp3972->dev, "regulator init failed: %d\n",
490 err);
Axel Lin93227c82013-11-13 14:55:14 +0800491 return err;
Axel Lin5976f092010-09-16 16:48:49 +0800492 }
493 }
494
495 return 0;
Axel Lin5976f092010-09-16 16:48:49 +0800496}
497
Bill Pembertona5023572012-11-19 13:22:22 -0500498static int lp3972_i2c_probe(struct i2c_client *i2c,
Axel Lin5976f092010-09-16 16:48:49 +0800499 const struct i2c_device_id *id)
500{
501 struct lp3972 *lp3972;
Jingoo Handff91d02013-07-30 17:20:47 +0900502 struct lp3972_platform_data *pdata = dev_get_platdata(&i2c->dev);
Axel Lin5976f092010-09-16 16:48:49 +0800503 int ret;
504 u16 val;
505
506 if (!pdata) {
507 dev_dbg(&i2c->dev, "No platform init data supplied\n");
508 return -ENODEV;
509 }
510
Nikolay Balandin2af0af62013-05-25 14:50:40 -0700511 lp3972 = devm_kzalloc(&i2c->dev, sizeof(struct lp3972), GFP_KERNEL);
Axel Lin5976f092010-09-16 16:48:49 +0800512 if (!lp3972)
513 return -ENOMEM;
514
515 lp3972->i2c = i2c;
516 lp3972->dev = &i2c->dev;
517
518 mutex_init(&lp3972->io_lock);
519
520 /* Detect LP3972 */
521 ret = lp3972_i2c_read(i2c, LP3972_SYS_CONTROL1_REG, 1, &val);
Axel Lin993af7c2010-09-18 13:13:28 +0800522 if (ret == 0 &&
523 (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL) {
Axel Lin5976f092010-09-16 16:48:49 +0800524 ret = -ENODEV;
Axel Lin993af7c2010-09-18 13:13:28 +0800525 dev_err(&i2c->dev, "chip reported: val = 0x%x\n", val);
526 }
Axel Lin5976f092010-09-16 16:48:49 +0800527 if (ret < 0) {
Axel Lin993af7c2010-09-18 13:13:28 +0800528 dev_err(&i2c->dev, "failed to detect device. ret = %d\n", ret);
Nikolay Balandin2af0af62013-05-25 14:50:40 -0700529 return ret;
Axel Lin5976f092010-09-16 16:48:49 +0800530 }
531
532 ret = setup_regulators(lp3972, pdata);
533 if (ret < 0)
Nikolay Balandin2af0af62013-05-25 14:50:40 -0700534 return ret;
Axel Lin5976f092010-09-16 16:48:49 +0800535
536 i2c_set_clientdata(i2c, lp3972);
537 return 0;
Axel Lin5976f092010-09-16 16:48:49 +0800538}
539
Axel Lin5976f092010-09-16 16:48:49 +0800540static const struct i2c_device_id lp3972_i2c_id[] = {
541 { "lp3972", 0 },
542 { }
543};
544MODULE_DEVICE_TABLE(i2c, lp3972_i2c_id);
545
546static struct i2c_driver lp3972_i2c_driver = {
547 .driver = {
548 .name = "lp3972",
Axel Lin5976f092010-09-16 16:48:49 +0800549 },
550 .probe = lp3972_i2c_probe,
Axel Lin5976f092010-09-16 16:48:49 +0800551 .id_table = lp3972_i2c_id,
552};
553
554static int __init lp3972_module_init(void)
555{
556 return i2c_add_driver(&lp3972_i2c_driver);
557}
558subsys_initcall(lp3972_module_init);
559
560static void __exit lp3972_module_exit(void)
561{
562 i2c_del_driver(&lp3972_i2c_driver);
563}
564module_exit(lp3972_module_exit);
565
566MODULE_LICENSE("GPL");
567MODULE_AUTHOR("Axel Lin <axel.lin@gmail.com>");
568MODULE_DESCRIPTION("LP3972 PMIC driver");