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>
25 struct i2c_client
*i2c
;
27 struct regulator_dev
**rdev
;
30 static u8
lp3971_reg_read(struct lp3971
*lp3971
, u8 reg
);
31 static int lp3971_set_bits(struct lp3971
*lp3971
, u8 reg
, u16 mask
, u16 val
);
33 #define LP3971_SYS_CONTROL1_REG 0x07
35 /* System control register 1 initial value,
36 bits 4 and 5 are EPROM programmable */
37 #define SYS_CONTROL1_INIT_VAL 0x40
38 #define SYS_CONTROL1_INIT_MASK 0xCF
40 #define LP3971_BUCK_VOL_ENABLE_REG 0x10
41 #define LP3971_BUCK_VOL_CHANGE_REG 0x20
43 /* Voltage control registers shift:
48 #define BUCK_VOL_CHANGE_SHIFT(x) (((1 << x) & ~0x01) << 1)
49 #define BUCK_VOL_CHANGE_FLAG_GO 0x01
50 #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
51 #define BUCK_VOL_CHANGE_FLAG_MASK 0x03
53 #define LP3971_BUCK1_BASE 0x23
54 #define LP3971_BUCK2_BASE 0x29
55 #define LP3971_BUCK3_BASE 0x32
57 const static int buck_base_addr
[] = {
63 #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
64 #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
66 const static int buck_voltage_map
[] = {
67 0, 800, 850, 900, 950, 1000, 1050, 1100,
68 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500,
69 1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800,
73 #define BUCK_TARGET_VOL_MASK 0x3f
74 #define BUCK_TARGET_VOL_MIN_IDX 0x01
75 #define BUCK_TARGET_VOL_MAX_IDX 0x19
77 #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
79 #define LP3971_LDO_ENABLE_REG 0x12
80 #define LP3971_LDO_VOL_CONTR_BASE 0x39
82 /* Voltage control registers:
83 LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
84 LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
85 LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
86 LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
87 LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
89 #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
91 /* Voltage control registers shift:
92 LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
93 LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
96 #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
97 #define LDO_VOL_CONTR_MASK 0x0f
99 const static int ldo45_voltage_map
[] = {
100 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350,
101 1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300,
104 const static int ldo123_voltage_map
[] = {
105 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500,
106 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300,
109 const static int *ldo_voltage_map
[] = {
110 ldo123_voltage_map
, /* LDO1 */
111 ldo123_voltage_map
, /* LDO2 */
112 ldo123_voltage_map
, /* LDO3 */
113 ldo45_voltage_map
, /* LDO4 */
114 ldo45_voltage_map
, /* LDO5 */
117 #define LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[(x - LP3971_LDO1)])
119 #define LDO_VOL_MIN_IDX 0x00
120 #define LDO_VOL_MAX_IDX 0x0f
122 static int lp3971_ldo_list_voltage(struct regulator_dev
*dev
, unsigned index
)
124 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
125 return 1000 * LDO_VOL_VALUE_MAP(ldo
)[index
];
128 static int lp3971_ldo_is_enabled(struct regulator_dev
*dev
)
130 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
131 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
132 u16 mask
= 1 << (1 + ldo
);
135 val
= lp3971_reg_read(lp3971
, LP3971_LDO_ENABLE_REG
);
136 return (val
& mask
) != 0;
139 static int lp3971_ldo_enable(struct regulator_dev
*dev
)
141 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
142 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
143 u16 mask
= 1 << (1 + ldo
);
145 return lp3971_set_bits(lp3971
, LP3971_LDO_ENABLE_REG
, mask
, mask
);
148 static int lp3971_ldo_disable(struct regulator_dev
*dev
)
150 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
151 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
152 u16 mask
= 1 << (1 + ldo
);
154 return lp3971_set_bits(lp3971
, LP3971_LDO_ENABLE_REG
, mask
, 0);
157 static int lp3971_ldo_get_voltage(struct regulator_dev
*dev
)
159 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
160 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
163 reg
= lp3971_reg_read(lp3971
, LP3971_LDO_VOL_CONTR_REG(ldo
));
164 val
= (reg
>> LDO_VOL_CONTR_SHIFT(ldo
)) & LDO_VOL_CONTR_MASK
;
166 return 1000 * LDO_VOL_VALUE_MAP(ldo
)[val
];
169 static int lp3971_ldo_set_voltage(struct regulator_dev
*dev
,
170 int min_uV
, int max_uV
)
172 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
173 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
174 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
175 const int *vol_map
= LDO_VOL_VALUE_MAP(ldo
);
178 if (min_vol
< vol_map
[LDO_VOL_MIN_IDX
] ||
179 min_vol
> vol_map
[LDO_VOL_MAX_IDX
])
182 for (val
= LDO_VOL_MIN_IDX
; val
<= LDO_VOL_MAX_IDX
; val
++)
183 if (vol_map
[val
] >= min_vol
)
186 if (vol_map
[val
] > max_vol
)
189 return lp3971_set_bits(lp3971
, LP3971_LDO_VOL_CONTR_REG(ldo
),
190 LDO_VOL_CONTR_MASK
<< LDO_VOL_CONTR_SHIFT(ldo
), val
);
193 static struct regulator_ops lp3971_ldo_ops
= {
194 .list_voltage
= lp3971_ldo_list_voltage
,
195 .is_enabled
= lp3971_ldo_is_enabled
,
196 .enable
= lp3971_ldo_enable
,
197 .disable
= lp3971_ldo_disable
,
198 .get_voltage
= lp3971_ldo_get_voltage
,
199 .set_voltage
= lp3971_ldo_set_voltage
,
202 static int lp3971_dcdc_list_voltage(struct regulator_dev
*dev
, unsigned index
)
204 return 1000 * buck_voltage_map
[index
];
207 static int lp3971_dcdc_is_enabled(struct regulator_dev
*dev
)
209 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
210 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
211 u16 mask
= 1 << (buck
* 2);
214 val
= lp3971_reg_read(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
);
215 return (val
& mask
) != 0;
218 static int lp3971_dcdc_enable(struct regulator_dev
*dev
)
220 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
221 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
222 u16 mask
= 1 << (buck
* 2);
224 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
, mask
, mask
);
227 static int lp3971_dcdc_disable(struct regulator_dev
*dev
)
229 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
230 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
231 u16 mask
= 1 << (buck
* 2);
233 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
, mask
, 0);
236 static int lp3971_dcdc_get_voltage(struct regulator_dev
*dev
)
238 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
239 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
243 reg
= lp3971_reg_read(lp3971
, LP3971_BUCK_TARGET_VOL1_REG(buck
));
244 reg
&= BUCK_TARGET_VOL_MASK
;
246 if (reg
<= BUCK_TARGET_VOL_MAX_IDX
)
247 val
= 1000 * buck_voltage_map
[reg
];
250 dev_warn(&dev
->dev
, "chip reported incorrect voltage value.\n");
256 static int lp3971_dcdc_set_voltage(struct regulator_dev
*dev
,
257 int min_uV
, int max_uV
)
259 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
260 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
261 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
262 const int *vol_map
= buck_voltage_map
;
266 if (min_vol
< vol_map
[BUCK_TARGET_VOL_MIN_IDX
] ||
267 min_vol
> vol_map
[BUCK_TARGET_VOL_MAX_IDX
])
270 for (val
= BUCK_TARGET_VOL_MIN_IDX
; val
<= BUCK_TARGET_VOL_MAX_IDX
;
272 if (vol_map
[val
] >= min_vol
)
275 if (vol_map
[val
] > max_vol
)
278 ret
= lp3971_set_bits(lp3971
, LP3971_BUCK_TARGET_VOL1_REG(buck
),
279 BUCK_TARGET_VOL_MASK
, val
);
283 ret
= lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_CHANGE_REG
,
284 BUCK_VOL_CHANGE_FLAG_MASK
<< BUCK_VOL_CHANGE_SHIFT(buck
),
285 BUCK_VOL_CHANGE_FLAG_GO
<< BUCK_VOL_CHANGE_SHIFT(buck
));
289 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_CHANGE_REG
,
290 BUCK_VOL_CHANGE_FLAG_MASK
<< BUCK_VOL_CHANGE_SHIFT(buck
),
291 0 << BUCK_VOL_CHANGE_SHIFT(buck
));
294 static struct regulator_ops lp3971_dcdc_ops
= {
295 .list_voltage
= lp3971_dcdc_list_voltage
,
296 .is_enabled
= lp3971_dcdc_is_enabled
,
297 .enable
= lp3971_dcdc_enable
,
298 .disable
= lp3971_dcdc_disable
,
299 .get_voltage
= lp3971_dcdc_get_voltage
,
300 .set_voltage
= lp3971_dcdc_set_voltage
,
303 static struct regulator_desc regulators
[] = {
307 .ops
= &lp3971_ldo_ops
,
308 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
309 .type
= REGULATOR_VOLTAGE
,
310 .owner
= THIS_MODULE
,
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(ldo45_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_dcdc_ops
,
348 .n_voltages
= ARRAY_SIZE(buck_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
,
370 static int lp3971_i2c_read(struct i2c_client
*i2c
, char reg
, int count
,
377 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
378 if (ret
< 0 || count
!= 1)
385 static int lp3971_i2c_write(struct i2c_client
*i2c
, char reg
, int count
,
392 ret
= i2c_smbus_write_byte_data(i2c
, reg
, *src
);
399 static u8
lp3971_reg_read(struct lp3971
*lp3971
, u8 reg
)
403 mutex_lock(&lp3971
->io_lock
);
405 lp3971_i2c_read(lp3971
->i2c
, reg
, 1, &val
);
407 dev_dbg(lp3971
->dev
, "reg read 0x%02x -> 0x%02x\n", (int)reg
,
410 mutex_unlock(&lp3971
->io_lock
);
415 static int lp3971_set_bits(struct lp3971
*lp3971
, u8 reg
, u16 mask
, u16 val
)
420 mutex_lock(&lp3971
->io_lock
);
422 ret
= lp3971_i2c_read(lp3971
->i2c
, reg
, 1, &tmp
);
423 tmp
= (tmp
& ~mask
) | val
;
425 ret
= lp3971_i2c_write(lp3971
->i2c
, reg
, 1, &tmp
);
426 dev_dbg(lp3971
->dev
, "reg write 0x%02x -> 0x%02x\n", (int)reg
,
429 mutex_unlock(&lp3971
->io_lock
);
434 static int setup_regulators(struct lp3971
*lp3971
,
435 struct lp3971_platform_data
*pdata
)
438 int num_regulators
= pdata
->num_regulators
;
439 lp3971
->num_regulators
= num_regulators
;
440 lp3971
->rdev
= kzalloc(sizeof(struct regulator_dev
*) * num_regulators
,
443 /* Instantiate the regulators */
444 for (i
= 0; i
< num_regulators
; i
++) {
445 int id
= pdata
->regulators
[i
].id
;
446 lp3971
->rdev
[i
] = regulator_register(®ulators
[id
],
447 lp3971
->dev
, pdata
->regulators
[i
].initdata
, lp3971
);
449 err
= IS_ERR(lp3971
->rdev
[i
]);
451 dev_err(lp3971
->dev
, "regulator init failed: %d\n",
459 for (i
= 0; i
< num_regulators
; i
++)
461 regulator_unregister(lp3971
->rdev
[i
]);
467 static int __devinit
lp3971_i2c_probe(struct i2c_client
*i2c
,
468 const struct i2c_device_id
*id
)
470 struct lp3971
*lp3971
;
471 struct lp3971_platform_data
*pdata
= i2c
->dev
.platform_data
;
475 lp3971
= kzalloc(sizeof(struct lp3971
), GFP_KERNEL
);
476 if (lp3971
== NULL
) {
482 lp3971
->dev
= &i2c
->dev
;
483 i2c_set_clientdata(i2c
, lp3971
);
485 mutex_init(&lp3971
->io_lock
);
488 ret
= lp3971_i2c_read(i2c
, LP3971_SYS_CONTROL1_REG
, 1, &val
);
489 if (ret
== 0 && (val
& SYS_CONTROL1_INIT_MASK
) != SYS_CONTROL1_INIT_VAL
)
492 dev_err(&i2c
->dev
, "failed to detect device\n");
497 ret
= setup_regulators(lp3971
, pdata
);
501 dev_warn(lp3971
->dev
, "No platform init data supplied\n");
506 i2c_set_clientdata(i2c
, NULL
);
512 static int __devexit
lp3971_i2c_remove(struct i2c_client
*i2c
)
514 struct lp3971
*lp3971
= i2c_get_clientdata(i2c
);
516 for (i
= 0; i
< lp3971
->num_regulators
; i
++)
518 regulator_unregister(lp3971
->rdev
[i
]);
520 i2c_set_clientdata(i2c
, NULL
);
526 static const struct i2c_device_id lp3971_i2c_id
[] = {
530 MODULE_DEVICE_TABLE(i2c
, lp3971_i2c_id
);
532 static struct i2c_driver lp3971_i2c_driver
= {
535 .owner
= THIS_MODULE
,
537 .probe
= lp3971_i2c_probe
,
538 .remove
= __devexit_p(lp3971_i2c_remove
),
539 .id_table
= lp3971_i2c_id
,
542 static int __init
lp3971_module_init(void)
546 ret
= i2c_add_driver(&lp3971_i2c_driver
);
548 pr_err("Failed to register I2C driver: %d\n", ret
);
552 module_init(lp3971_module_init
);
554 static void __exit
lp3971_module_exit(void)
556 i2c_del_driver(&lp3971_i2c_driver
);
558 module_exit(lp3971_module_exit
);
560 MODULE_LICENSE("GPL");
561 MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
562 MODULE_DESCRIPTION("LP3971 PMIC driver");