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/regulator/driver.h>
20 #include <linux/regulator/lp3971.h>
21 #include <linux/slab.h>
26 struct i2c_client
*i2c
;
28 struct regulator_dev
**rdev
;
31 static u8
lp3971_reg_read(struct lp3971
*lp3971
, u8 reg
);
32 static int lp3971_set_bits(struct lp3971
*lp3971
, u8 reg
, u16 mask
, u16 val
);
34 #define LP3971_SYS_CONTROL1_REG 0x07
36 /* System control register 1 initial value,
37 bits 4 and 5 are EPROM programmable */
38 #define SYS_CONTROL1_INIT_VAL 0x40
39 #define SYS_CONTROL1_INIT_MASK 0xCF
41 #define LP3971_BUCK_VOL_ENABLE_REG 0x10
42 #define LP3971_BUCK_VOL_CHANGE_REG 0x20
44 /* Voltage control registers shift:
49 #define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01))
50 #define BUCK_VOL_CHANGE_FLAG_GO 0x01
51 #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
52 #define BUCK_VOL_CHANGE_FLAG_MASK 0x03
54 #define LP3971_BUCK1_BASE 0x23
55 #define LP3971_BUCK2_BASE 0x29
56 #define LP3971_BUCK3_BASE 0x32
58 static const int buck_base_addr
[] = {
64 #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
65 #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
67 static const int buck_voltage_map
[] = {
68 0, 800, 850, 900, 950, 1000, 1050, 1100,
69 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500,
70 1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800,
74 #define BUCK_TARGET_VOL_MASK 0x3f
75 #define BUCK_TARGET_VOL_MIN_IDX 0x01
76 #define BUCK_TARGET_VOL_MAX_IDX 0x19
78 #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
80 #define LP3971_LDO_ENABLE_REG 0x12
81 #define LP3971_LDO_VOL_CONTR_BASE 0x39
83 /* Voltage control registers:
84 LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
85 LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
86 LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
87 LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
88 LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
90 #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
92 /* Voltage control registers shift:
93 LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
94 LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
97 #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
98 #define LDO_VOL_CONTR_MASK 0x0f
100 static const int ldo45_voltage_map
[] = {
101 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350,
102 1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300,
105 static const int ldo123_voltage_map
[] = {
106 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500,
107 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300,
110 static const int *ldo_voltage_map
[] = {
111 ldo123_voltage_map
, /* LDO1 */
112 ldo123_voltage_map
, /* LDO2 */
113 ldo123_voltage_map
, /* LDO3 */
114 ldo45_voltage_map
, /* LDO4 */
115 ldo45_voltage_map
, /* LDO5 */
118 #define LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[(x - LP3971_LDO1)])
120 #define LDO_VOL_MIN_IDX 0x00
121 #define LDO_VOL_MAX_IDX 0x0f
123 static int lp3971_ldo_list_voltage(struct regulator_dev
*dev
, unsigned index
)
125 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
126 return 1000 * LDO_VOL_VALUE_MAP(ldo
)[index
];
129 static int lp3971_ldo_is_enabled(struct regulator_dev
*dev
)
131 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
132 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
133 u16 mask
= 1 << (1 + ldo
);
136 val
= lp3971_reg_read(lp3971
, LP3971_LDO_ENABLE_REG
);
137 return (val
& mask
) != 0;
140 static int lp3971_ldo_enable(struct regulator_dev
*dev
)
142 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
143 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
144 u16 mask
= 1 << (1 + ldo
);
146 return lp3971_set_bits(lp3971
, LP3971_LDO_ENABLE_REG
, mask
, mask
);
149 static int lp3971_ldo_disable(struct regulator_dev
*dev
)
151 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
152 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
153 u16 mask
= 1 << (1 + ldo
);
155 return lp3971_set_bits(lp3971
, LP3971_LDO_ENABLE_REG
, mask
, 0);
158 static int lp3971_ldo_get_voltage(struct regulator_dev
*dev
)
160 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
161 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
164 reg
= lp3971_reg_read(lp3971
, LP3971_LDO_VOL_CONTR_REG(ldo
));
165 val
= (reg
>> LDO_VOL_CONTR_SHIFT(ldo
)) & LDO_VOL_CONTR_MASK
;
167 return 1000 * LDO_VOL_VALUE_MAP(ldo
)[val
];
170 static int lp3971_ldo_set_voltage(struct regulator_dev
*dev
,
171 int min_uV
, int max_uV
,
172 unsigned int *selector
)
174 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
175 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
176 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
177 const int *vol_map
= LDO_VOL_VALUE_MAP(ldo
);
180 if (min_vol
< vol_map
[LDO_VOL_MIN_IDX
] ||
181 min_vol
> vol_map
[LDO_VOL_MAX_IDX
])
184 for (val
= LDO_VOL_MIN_IDX
; val
<= LDO_VOL_MAX_IDX
; val
++)
185 if (vol_map
[val
] >= min_vol
)
188 if (val
> LDO_VOL_MAX_IDX
|| vol_map
[val
] > max_vol
)
193 return lp3971_set_bits(lp3971
, LP3971_LDO_VOL_CONTR_REG(ldo
),
194 LDO_VOL_CONTR_MASK
<< LDO_VOL_CONTR_SHIFT(ldo
),
195 val
<< LDO_VOL_CONTR_SHIFT(ldo
));
198 static struct regulator_ops lp3971_ldo_ops
= {
199 .list_voltage
= lp3971_ldo_list_voltage
,
200 .is_enabled
= lp3971_ldo_is_enabled
,
201 .enable
= lp3971_ldo_enable
,
202 .disable
= lp3971_ldo_disable
,
203 .get_voltage
= lp3971_ldo_get_voltage
,
204 .set_voltage
= lp3971_ldo_set_voltage
,
207 static int lp3971_dcdc_list_voltage(struct regulator_dev
*dev
, unsigned index
)
209 return 1000 * buck_voltage_map
[index
];
212 static int lp3971_dcdc_is_enabled(struct regulator_dev
*dev
)
214 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
215 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
216 u16 mask
= 1 << (buck
* 2);
219 val
= lp3971_reg_read(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
);
220 return (val
& mask
) != 0;
223 static int lp3971_dcdc_enable(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
, mask
);
232 static int lp3971_dcdc_disable(struct regulator_dev
*dev
)
234 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
235 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
236 u16 mask
= 1 << (buck
* 2);
238 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
, mask
, 0);
241 static int lp3971_dcdc_get_voltage(struct regulator_dev
*dev
)
243 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
244 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
248 reg
= lp3971_reg_read(lp3971
, LP3971_BUCK_TARGET_VOL1_REG(buck
));
249 reg
&= BUCK_TARGET_VOL_MASK
;
251 if (reg
<= BUCK_TARGET_VOL_MAX_IDX
)
252 val
= 1000 * buck_voltage_map
[reg
];
255 dev_warn(&dev
->dev
, "chip reported incorrect voltage value.\n");
261 static int lp3971_dcdc_set_voltage(struct regulator_dev
*dev
,
262 int min_uV
, int max_uV
,
263 unsigned int *selector
)
265 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
266 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
267 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
268 const int *vol_map
= buck_voltage_map
;
272 if (min_vol
< vol_map
[BUCK_TARGET_VOL_MIN_IDX
] ||
273 min_vol
> vol_map
[BUCK_TARGET_VOL_MAX_IDX
])
276 for (val
= BUCK_TARGET_VOL_MIN_IDX
; val
<= BUCK_TARGET_VOL_MAX_IDX
;
278 if (vol_map
[val
] >= min_vol
)
281 if (val
> BUCK_TARGET_VOL_MAX_IDX
|| vol_map
[val
] > max_vol
)
286 ret
= lp3971_set_bits(lp3971
, LP3971_BUCK_TARGET_VOL1_REG(buck
),
287 BUCK_TARGET_VOL_MASK
, val
);
291 ret
= lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_CHANGE_REG
,
292 BUCK_VOL_CHANGE_FLAG_MASK
<< BUCK_VOL_CHANGE_SHIFT(buck
),
293 BUCK_VOL_CHANGE_FLAG_GO
<< BUCK_VOL_CHANGE_SHIFT(buck
));
297 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_CHANGE_REG
,
298 BUCK_VOL_CHANGE_FLAG_MASK
<< BUCK_VOL_CHANGE_SHIFT(buck
),
299 0 << BUCK_VOL_CHANGE_SHIFT(buck
));
302 static struct regulator_ops lp3971_dcdc_ops
= {
303 .list_voltage
= lp3971_dcdc_list_voltage
,
304 .is_enabled
= lp3971_dcdc_is_enabled
,
305 .enable
= lp3971_dcdc_enable
,
306 .disable
= lp3971_dcdc_disable
,
307 .get_voltage
= lp3971_dcdc_get_voltage
,
308 .set_voltage
= lp3971_dcdc_set_voltage
,
311 static struct regulator_desc regulators
[] = {
315 .ops
= &lp3971_ldo_ops
,
316 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
317 .type
= REGULATOR_VOLTAGE
,
318 .owner
= THIS_MODULE
,
323 .ops
= &lp3971_ldo_ops
,
324 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
325 .type
= REGULATOR_VOLTAGE
,
326 .owner
= THIS_MODULE
,
331 .ops
= &lp3971_ldo_ops
,
332 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
333 .type
= REGULATOR_VOLTAGE
,
334 .owner
= THIS_MODULE
,
339 .ops
= &lp3971_ldo_ops
,
340 .n_voltages
= ARRAY_SIZE(ldo45_voltage_map
),
341 .type
= REGULATOR_VOLTAGE
,
342 .owner
= THIS_MODULE
,
347 .ops
= &lp3971_ldo_ops
,
348 .n_voltages
= ARRAY_SIZE(ldo45_voltage_map
),
349 .type
= REGULATOR_VOLTAGE
,
350 .owner
= THIS_MODULE
,
355 .ops
= &lp3971_dcdc_ops
,
356 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
357 .type
= REGULATOR_VOLTAGE
,
358 .owner
= THIS_MODULE
,
363 .ops
= &lp3971_dcdc_ops
,
364 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
365 .type
= REGULATOR_VOLTAGE
,
366 .owner
= THIS_MODULE
,
371 .ops
= &lp3971_dcdc_ops
,
372 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
373 .type
= REGULATOR_VOLTAGE
,
374 .owner
= THIS_MODULE
,
378 static int lp3971_i2c_read(struct i2c_client
*i2c
, char reg
, int count
,
385 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
393 static int lp3971_i2c_write(struct i2c_client
*i2c
, char reg
, int count
,
398 return i2c_smbus_write_byte_data(i2c
, reg
, *src
);
401 static u8
lp3971_reg_read(struct lp3971
*lp3971
, u8 reg
)
405 mutex_lock(&lp3971
->io_lock
);
407 lp3971_i2c_read(lp3971
->i2c
, reg
, 1, &val
);
409 dev_dbg(lp3971
->dev
, "reg read 0x%02x -> 0x%02x\n", (int)reg
,
412 mutex_unlock(&lp3971
->io_lock
);
417 static int lp3971_set_bits(struct lp3971
*lp3971
, u8 reg
, u16 mask
, u16 val
)
422 mutex_lock(&lp3971
->io_lock
);
424 ret
= lp3971_i2c_read(lp3971
->i2c
, reg
, 1, &tmp
);
425 tmp
= (tmp
& ~mask
) | val
;
427 ret
= lp3971_i2c_write(lp3971
->i2c
, reg
, 1, &tmp
);
428 dev_dbg(lp3971
->dev
, "reg write 0x%02x -> 0x%02x\n", (int)reg
,
431 mutex_unlock(&lp3971
->io_lock
);
436 static int __devinit
setup_regulators(struct lp3971
*lp3971
,
437 struct lp3971_platform_data
*pdata
)
441 lp3971
->num_regulators
= pdata
->num_regulators
;
442 lp3971
->rdev
= kcalloc(pdata
->num_regulators
,
443 sizeof(struct regulator_dev
*), GFP_KERNEL
);
449 /* Instantiate the regulators */
450 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
451 struct lp3971_regulator_subdev
*reg
= &pdata
->regulators
[i
];
452 lp3971
->rdev
[i
] = regulator_register(®ulators
[reg
->id
],
453 lp3971
->dev
, reg
->initdata
, lp3971
);
455 if (IS_ERR(lp3971
->rdev
[i
])) {
456 err
= PTR_ERR(lp3971
->rdev
[i
]);
457 dev_err(lp3971
->dev
, "regulator init failed: %d\n",
467 regulator_unregister(lp3971
->rdev
[i
]);
474 static int __devinit
lp3971_i2c_probe(struct i2c_client
*i2c
,
475 const struct i2c_device_id
*id
)
477 struct lp3971
*lp3971
;
478 struct lp3971_platform_data
*pdata
= i2c
->dev
.platform_data
;
483 dev_dbg(&i2c
->dev
, "No platform init data supplied\n");
487 lp3971
= kzalloc(sizeof(struct lp3971
), GFP_KERNEL
);
492 lp3971
->dev
= &i2c
->dev
;
494 mutex_init(&lp3971
->io_lock
);
497 ret
= lp3971_i2c_read(i2c
, LP3971_SYS_CONTROL1_REG
, 1, &val
);
498 if (ret
== 0 && (val
& SYS_CONTROL1_INIT_MASK
) != SYS_CONTROL1_INIT_VAL
)
501 dev_err(&i2c
->dev
, "failed to detect device\n");
505 ret
= setup_regulators(lp3971
, pdata
);
509 i2c_set_clientdata(i2c
, lp3971
);
517 static int __devexit
lp3971_i2c_remove(struct i2c_client
*i2c
)
519 struct lp3971
*lp3971
= i2c_get_clientdata(i2c
);
522 for (i
= 0; i
< lp3971
->num_regulators
; i
++)
523 regulator_unregister(lp3971
->rdev
[i
]);
531 static const struct i2c_device_id lp3971_i2c_id
[] = {
535 MODULE_DEVICE_TABLE(i2c
, lp3971_i2c_id
);
537 static struct i2c_driver lp3971_i2c_driver
= {
540 .owner
= THIS_MODULE
,
542 .probe
= lp3971_i2c_probe
,
543 .remove
= __devexit_p(lp3971_i2c_remove
),
544 .id_table
= lp3971_i2c_id
,
547 static int __init
lp3971_module_init(void)
551 ret
= i2c_add_driver(&lp3971_i2c_driver
);
553 pr_err("Failed to register I2C driver: %d\n", ret
);
557 module_init(lp3971_module_init
);
559 static void __exit
lp3971_module_exit(void)
561 i2c_del_driver(&lp3971_i2c_driver
);
563 module_exit(lp3971_module_exit
);
565 MODULE_LICENSE("GPL");
566 MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
567 MODULE_DESCRIPTION("LP3971 PMIC driver");