2 * Regulator driver for National Semiconductors LP3971 PMIC chip
4 * Copyright (C) 2009 Samsung Electronics
5 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/bug.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/lp3971.h>
22 #include <linux/slab.h>
27 struct i2c_client
*i2c
;
29 struct regulator_dev
**rdev
;
32 static u8
lp3971_reg_read(struct lp3971
*lp3971
, u8 reg
);
33 static int lp3971_set_bits(struct lp3971
*lp3971
, u8 reg
, u16 mask
, u16 val
);
35 #define LP3971_SYS_CONTROL1_REG 0x07
37 /* System control register 1 initial value,
38 bits 4 and 5 are EPROM programmable */
39 #define SYS_CONTROL1_INIT_VAL 0x40
40 #define SYS_CONTROL1_INIT_MASK 0xCF
42 #define LP3971_BUCK_VOL_ENABLE_REG 0x10
43 #define LP3971_BUCK_VOL_CHANGE_REG 0x20
45 /* Voltage control registers shift:
50 #define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01))
51 #define BUCK_VOL_CHANGE_FLAG_GO 0x01
52 #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
53 #define BUCK_VOL_CHANGE_FLAG_MASK 0x03
55 #define LP3971_BUCK1_BASE 0x23
56 #define LP3971_BUCK2_BASE 0x29
57 #define LP3971_BUCK3_BASE 0x32
59 static const int buck_base_addr
[] = {
65 #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
66 #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
68 static const int buck_voltage_map
[] = {
69 0, 800, 850, 900, 950, 1000, 1050, 1100,
70 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500,
71 1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800,
75 #define BUCK_TARGET_VOL_MASK 0x3f
76 #define BUCK_TARGET_VOL_MIN_IDX 0x01
77 #define BUCK_TARGET_VOL_MAX_IDX 0x19
79 #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
81 #define LP3971_LDO_ENABLE_REG 0x12
82 #define LP3971_LDO_VOL_CONTR_BASE 0x39
84 /* Voltage control registers:
85 LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
86 LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
87 LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
88 LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
89 LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
91 #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
93 /* Voltage control registers shift:
94 LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
95 LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
98 #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
99 #define LDO_VOL_CONTR_MASK 0x0f
101 static const int ldo45_voltage_map
[] = {
102 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350,
103 1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300,
106 static const int ldo123_voltage_map
[] = {
107 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500,
108 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300,
111 static const int *ldo_voltage_map
[] = {
112 ldo123_voltage_map
, /* LDO1 */
113 ldo123_voltage_map
, /* LDO2 */
114 ldo123_voltage_map
, /* LDO3 */
115 ldo45_voltage_map
, /* LDO4 */
116 ldo45_voltage_map
, /* LDO5 */
119 #define LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[(x - LP3971_LDO1)])
121 #define LDO_VOL_MIN_IDX 0x00
122 #define LDO_VOL_MAX_IDX 0x0f
124 static int lp3971_ldo_list_voltage(struct regulator_dev
*dev
, unsigned index
)
126 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
128 if (index
> LDO_VOL_MAX_IDX
)
131 return 1000 * LDO_VOL_VALUE_MAP(ldo
)[index
];
134 static int lp3971_ldo_is_enabled(struct regulator_dev
*dev
)
136 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
137 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
138 u16 mask
= 1 << (1 + ldo
);
141 val
= lp3971_reg_read(lp3971
, LP3971_LDO_ENABLE_REG
);
142 return (val
& mask
) != 0;
145 static int lp3971_ldo_enable(struct regulator_dev
*dev
)
147 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
148 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
149 u16 mask
= 1 << (1 + ldo
);
151 return lp3971_set_bits(lp3971
, LP3971_LDO_ENABLE_REG
, mask
, mask
);
154 static int lp3971_ldo_disable(struct regulator_dev
*dev
)
156 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
157 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
158 u16 mask
= 1 << (1 + ldo
);
160 return lp3971_set_bits(lp3971
, LP3971_LDO_ENABLE_REG
, mask
, 0);
163 static int lp3971_ldo_get_voltage(struct regulator_dev
*dev
)
165 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
166 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
169 reg
= lp3971_reg_read(lp3971
, LP3971_LDO_VOL_CONTR_REG(ldo
));
170 val
= (reg
>> LDO_VOL_CONTR_SHIFT(ldo
)) & LDO_VOL_CONTR_MASK
;
172 return 1000 * LDO_VOL_VALUE_MAP(ldo
)[val
];
175 static int lp3971_ldo_set_voltage_sel(struct regulator_dev
*dev
,
176 unsigned int selector
)
178 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
179 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
181 return lp3971_set_bits(lp3971
, LP3971_LDO_VOL_CONTR_REG(ldo
),
182 LDO_VOL_CONTR_MASK
<< LDO_VOL_CONTR_SHIFT(ldo
),
183 selector
<< LDO_VOL_CONTR_SHIFT(ldo
));
186 static struct regulator_ops lp3971_ldo_ops
= {
187 .list_voltage
= lp3971_ldo_list_voltage
,
188 .is_enabled
= lp3971_ldo_is_enabled
,
189 .enable
= lp3971_ldo_enable
,
190 .disable
= lp3971_ldo_disable
,
191 .get_voltage
= lp3971_ldo_get_voltage
,
192 .set_voltage_sel
= lp3971_ldo_set_voltage_sel
,
195 static int lp3971_dcdc_list_voltage(struct regulator_dev
*dev
, unsigned index
)
197 if (index
< BUCK_TARGET_VOL_MIN_IDX
|| index
> BUCK_TARGET_VOL_MAX_IDX
)
200 return 1000 * buck_voltage_map
[index
];
203 static int lp3971_dcdc_is_enabled(struct regulator_dev
*dev
)
205 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
206 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
207 u16 mask
= 1 << (buck
* 2);
210 val
= lp3971_reg_read(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
);
211 return (val
& mask
) != 0;
214 static int lp3971_dcdc_enable(struct regulator_dev
*dev
)
216 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
217 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
218 u16 mask
= 1 << (buck
* 2);
220 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
, mask
, mask
);
223 static int lp3971_dcdc_disable(struct regulator_dev
*dev
)
225 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
226 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
227 u16 mask
= 1 << (buck
* 2);
229 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
, mask
, 0);
232 static int lp3971_dcdc_get_voltage(struct regulator_dev
*dev
)
234 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
235 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
239 reg
= lp3971_reg_read(lp3971
, LP3971_BUCK_TARGET_VOL1_REG(buck
));
240 reg
&= BUCK_TARGET_VOL_MASK
;
242 if (reg
<= BUCK_TARGET_VOL_MAX_IDX
)
243 val
= 1000 * buck_voltage_map
[reg
];
246 dev_warn(&dev
->dev
, "chip reported incorrect voltage value.\n");
252 static int lp3971_dcdc_set_voltage_sel(struct regulator_dev
*dev
,
253 unsigned int selector
)
255 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
256 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
259 ret
= lp3971_set_bits(lp3971
, LP3971_BUCK_TARGET_VOL1_REG(buck
),
260 BUCK_TARGET_VOL_MASK
, selector
);
264 ret
= lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_CHANGE_REG
,
265 BUCK_VOL_CHANGE_FLAG_MASK
<< BUCK_VOL_CHANGE_SHIFT(buck
),
266 BUCK_VOL_CHANGE_FLAG_GO
<< BUCK_VOL_CHANGE_SHIFT(buck
));
270 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_CHANGE_REG
,
271 BUCK_VOL_CHANGE_FLAG_MASK
<< BUCK_VOL_CHANGE_SHIFT(buck
),
272 0 << BUCK_VOL_CHANGE_SHIFT(buck
));
275 static struct regulator_ops lp3971_dcdc_ops
= {
276 .list_voltage
= lp3971_dcdc_list_voltage
,
277 .is_enabled
= lp3971_dcdc_is_enabled
,
278 .enable
= lp3971_dcdc_enable
,
279 .disable
= lp3971_dcdc_disable
,
280 .get_voltage
= lp3971_dcdc_get_voltage
,
281 .set_voltage_sel
= lp3971_dcdc_set_voltage_sel
,
284 static const struct regulator_desc regulators
[] = {
288 .ops
= &lp3971_ldo_ops
,
289 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
290 .type
= REGULATOR_VOLTAGE
,
291 .owner
= THIS_MODULE
,
296 .ops
= &lp3971_ldo_ops
,
297 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
298 .type
= REGULATOR_VOLTAGE
,
299 .owner
= THIS_MODULE
,
304 .ops
= &lp3971_ldo_ops
,
305 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
306 .type
= REGULATOR_VOLTAGE
,
307 .owner
= THIS_MODULE
,
312 .ops
= &lp3971_ldo_ops
,
313 .n_voltages
= ARRAY_SIZE(ldo45_voltage_map
),
314 .type
= REGULATOR_VOLTAGE
,
315 .owner
= THIS_MODULE
,
320 .ops
= &lp3971_ldo_ops
,
321 .n_voltages
= ARRAY_SIZE(ldo45_voltage_map
),
322 .type
= REGULATOR_VOLTAGE
,
323 .owner
= THIS_MODULE
,
328 .ops
= &lp3971_dcdc_ops
,
329 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
330 .type
= REGULATOR_VOLTAGE
,
331 .owner
= THIS_MODULE
,
336 .ops
= &lp3971_dcdc_ops
,
337 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
338 .type
= REGULATOR_VOLTAGE
,
339 .owner
= THIS_MODULE
,
344 .ops
= &lp3971_dcdc_ops
,
345 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
346 .type
= REGULATOR_VOLTAGE
,
347 .owner
= THIS_MODULE
,
351 static int lp3971_i2c_read(struct i2c_client
*i2c
, char reg
, int count
,
358 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
366 static int lp3971_i2c_write(struct i2c_client
*i2c
, char reg
, int count
,
371 return i2c_smbus_write_byte_data(i2c
, reg
, *src
);
374 static u8
lp3971_reg_read(struct lp3971
*lp3971
, u8 reg
)
378 mutex_lock(&lp3971
->io_lock
);
380 lp3971_i2c_read(lp3971
->i2c
, reg
, 1, &val
);
382 dev_dbg(lp3971
->dev
, "reg read 0x%02x -> 0x%02x\n", (int)reg
,
385 mutex_unlock(&lp3971
->io_lock
);
390 static int lp3971_set_bits(struct lp3971
*lp3971
, u8 reg
, u16 mask
, u16 val
)
395 mutex_lock(&lp3971
->io_lock
);
397 ret
= lp3971_i2c_read(lp3971
->i2c
, reg
, 1, &tmp
);
398 tmp
= (tmp
& ~mask
) | val
;
400 ret
= lp3971_i2c_write(lp3971
->i2c
, reg
, 1, &tmp
);
401 dev_dbg(lp3971
->dev
, "reg write 0x%02x -> 0x%02x\n", (int)reg
,
404 mutex_unlock(&lp3971
->io_lock
);
409 static int __devinit
setup_regulators(struct lp3971
*lp3971
,
410 struct lp3971_platform_data
*pdata
)
414 lp3971
->num_regulators
= pdata
->num_regulators
;
415 lp3971
->rdev
= kcalloc(pdata
->num_regulators
,
416 sizeof(struct regulator_dev
*), GFP_KERNEL
);
422 /* Instantiate the regulators */
423 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
424 struct regulator_config config
= { };
425 struct lp3971_regulator_subdev
*reg
= &pdata
->regulators
[i
];
427 config
.dev
= lp3971
->dev
;
428 config
.init_data
= reg
->initdata
;
429 config
.driver_data
= lp3971
;
431 lp3971
->rdev
[i
] = regulator_register(®ulators
[reg
->id
],
433 if (IS_ERR(lp3971
->rdev
[i
])) {
434 err
= PTR_ERR(lp3971
->rdev
[i
]);
435 dev_err(lp3971
->dev
, "regulator init failed: %d\n",
445 regulator_unregister(lp3971
->rdev
[i
]);
452 static int __devinit
lp3971_i2c_probe(struct i2c_client
*i2c
,
453 const struct i2c_device_id
*id
)
455 struct lp3971
*lp3971
;
456 struct lp3971_platform_data
*pdata
= i2c
->dev
.platform_data
;
461 dev_dbg(&i2c
->dev
, "No platform init data supplied\n");
465 lp3971
= kzalloc(sizeof(struct lp3971
), GFP_KERNEL
);
470 lp3971
->dev
= &i2c
->dev
;
472 mutex_init(&lp3971
->io_lock
);
475 ret
= lp3971_i2c_read(i2c
, LP3971_SYS_CONTROL1_REG
, 1, &val
);
476 if (ret
== 0 && (val
& SYS_CONTROL1_INIT_MASK
) != SYS_CONTROL1_INIT_VAL
)
479 dev_err(&i2c
->dev
, "failed to detect device\n");
483 ret
= setup_regulators(lp3971
, pdata
);
487 i2c_set_clientdata(i2c
, lp3971
);
495 static int __devexit
lp3971_i2c_remove(struct i2c_client
*i2c
)
497 struct lp3971
*lp3971
= i2c_get_clientdata(i2c
);
500 for (i
= 0; i
< lp3971
->num_regulators
; i
++)
501 regulator_unregister(lp3971
->rdev
[i
]);
509 static const struct i2c_device_id lp3971_i2c_id
[] = {
513 MODULE_DEVICE_TABLE(i2c
, lp3971_i2c_id
);
515 static struct i2c_driver lp3971_i2c_driver
= {
518 .owner
= THIS_MODULE
,
520 .probe
= lp3971_i2c_probe
,
521 .remove
= __devexit_p(lp3971_i2c_remove
),
522 .id_table
= lp3971_i2c_id
,
525 module_i2c_driver(lp3971_i2c_driver
);
527 MODULE_LICENSE("GPL");
528 MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
529 MODULE_DESCRIPTION("LP3971 PMIC driver");