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 unsigned int buck_voltage_map
[] = {
69 0, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
70 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
71 1550000, 1600000, 1650000, 1700000, 1800000, 1900000, 2500000, 2800000,
75 #define BUCK_TARGET_VOL_MASK 0x3f
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 static const unsigned int ldo45_voltage_map
[] = {
100 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000,
101 1400000, 1500000, 1800000, 1900000, 2500000, 2800000, 3000000, 3300000,
104 static const unsigned int ldo123_voltage_map
[] = {
105 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
106 2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
109 #define LDO_VOL_MIN_IDX 0x00
110 #define LDO_VOL_MAX_IDX 0x0f
112 static int lp3971_ldo_is_enabled(struct regulator_dev
*dev
)
114 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
115 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
116 u16 mask
= 1 << (1 + ldo
);
119 val
= lp3971_reg_read(lp3971
, LP3971_LDO_ENABLE_REG
);
120 return (val
& mask
) != 0;
123 static int lp3971_ldo_enable(struct regulator_dev
*dev
)
125 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
126 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
127 u16 mask
= 1 << (1 + ldo
);
129 return lp3971_set_bits(lp3971
, LP3971_LDO_ENABLE_REG
, mask
, mask
);
132 static int lp3971_ldo_disable(struct regulator_dev
*dev
)
134 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
135 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
136 u16 mask
= 1 << (1 + ldo
);
138 return lp3971_set_bits(lp3971
, LP3971_LDO_ENABLE_REG
, mask
, 0);
141 static int lp3971_ldo_get_voltage_sel(struct regulator_dev
*dev
)
143 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
144 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
147 reg
= lp3971_reg_read(lp3971
, LP3971_LDO_VOL_CONTR_REG(ldo
));
148 val
= (reg
>> LDO_VOL_CONTR_SHIFT(ldo
)) & LDO_VOL_CONTR_MASK
;
153 static int lp3971_ldo_set_voltage_sel(struct regulator_dev
*dev
,
154 unsigned int selector
)
156 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
157 int ldo
= rdev_get_id(dev
) - LP3971_LDO1
;
159 return lp3971_set_bits(lp3971
, LP3971_LDO_VOL_CONTR_REG(ldo
),
160 LDO_VOL_CONTR_MASK
<< LDO_VOL_CONTR_SHIFT(ldo
),
161 selector
<< LDO_VOL_CONTR_SHIFT(ldo
));
164 static struct regulator_ops lp3971_ldo_ops
= {
165 .list_voltage
= regulator_list_voltage_table
,
166 .is_enabled
= lp3971_ldo_is_enabled
,
167 .enable
= lp3971_ldo_enable
,
168 .disable
= lp3971_ldo_disable
,
169 .get_voltage_sel
= lp3971_ldo_get_voltage_sel
,
170 .set_voltage_sel
= lp3971_ldo_set_voltage_sel
,
173 static int lp3971_dcdc_is_enabled(struct regulator_dev
*dev
)
175 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
176 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
177 u16 mask
= 1 << (buck
* 2);
180 val
= lp3971_reg_read(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
);
181 return (val
& mask
) != 0;
184 static int lp3971_dcdc_enable(struct regulator_dev
*dev
)
186 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
187 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
188 u16 mask
= 1 << (buck
* 2);
190 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
, mask
, mask
);
193 static int lp3971_dcdc_disable(struct regulator_dev
*dev
)
195 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
196 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
197 u16 mask
= 1 << (buck
* 2);
199 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_ENABLE_REG
, mask
, 0);
202 static int lp3971_dcdc_get_voltage_sel(struct regulator_dev
*dev
)
204 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
205 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
208 reg
= lp3971_reg_read(lp3971
, LP3971_BUCK_TARGET_VOL1_REG(buck
));
209 reg
&= BUCK_TARGET_VOL_MASK
;
214 static int lp3971_dcdc_set_voltage_sel(struct regulator_dev
*dev
,
215 unsigned int selector
)
217 struct lp3971
*lp3971
= rdev_get_drvdata(dev
);
218 int buck
= rdev_get_id(dev
) - LP3971_DCDC1
;
221 ret
= lp3971_set_bits(lp3971
, LP3971_BUCK_TARGET_VOL1_REG(buck
),
222 BUCK_TARGET_VOL_MASK
, selector
);
226 ret
= lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_CHANGE_REG
,
227 BUCK_VOL_CHANGE_FLAG_MASK
<< BUCK_VOL_CHANGE_SHIFT(buck
),
228 BUCK_VOL_CHANGE_FLAG_GO
<< BUCK_VOL_CHANGE_SHIFT(buck
));
232 return lp3971_set_bits(lp3971
, LP3971_BUCK_VOL_CHANGE_REG
,
233 BUCK_VOL_CHANGE_FLAG_MASK
<< BUCK_VOL_CHANGE_SHIFT(buck
),
234 0 << BUCK_VOL_CHANGE_SHIFT(buck
));
237 static struct regulator_ops lp3971_dcdc_ops
= {
238 .list_voltage
= regulator_list_voltage_table
,
239 .is_enabled
= lp3971_dcdc_is_enabled
,
240 .enable
= lp3971_dcdc_enable
,
241 .disable
= lp3971_dcdc_disable
,
242 .get_voltage_sel
= lp3971_dcdc_get_voltage_sel
,
243 .set_voltage_sel
= lp3971_dcdc_set_voltage_sel
,
246 static const struct regulator_desc regulators
[] = {
250 .ops
= &lp3971_ldo_ops
,
251 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
252 .volt_table
= ldo123_voltage_map
,
253 .type
= REGULATOR_VOLTAGE
,
254 .owner
= THIS_MODULE
,
259 .ops
= &lp3971_ldo_ops
,
260 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
261 .volt_table
= ldo123_voltage_map
,
262 .type
= REGULATOR_VOLTAGE
,
263 .owner
= THIS_MODULE
,
268 .ops
= &lp3971_ldo_ops
,
269 .n_voltages
= ARRAY_SIZE(ldo123_voltage_map
),
270 .volt_table
= ldo123_voltage_map
,
271 .type
= REGULATOR_VOLTAGE
,
272 .owner
= THIS_MODULE
,
277 .ops
= &lp3971_ldo_ops
,
278 .n_voltages
= ARRAY_SIZE(ldo45_voltage_map
),
279 .volt_table
= ldo45_voltage_map
,
280 .type
= REGULATOR_VOLTAGE
,
281 .owner
= THIS_MODULE
,
286 .ops
= &lp3971_ldo_ops
,
287 .n_voltages
= ARRAY_SIZE(ldo45_voltage_map
),
288 .volt_table
= ldo45_voltage_map
,
289 .type
= REGULATOR_VOLTAGE
,
290 .owner
= THIS_MODULE
,
295 .ops
= &lp3971_dcdc_ops
,
296 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
297 .volt_table
= buck_voltage_map
,
298 .type
= REGULATOR_VOLTAGE
,
299 .owner
= THIS_MODULE
,
304 .ops
= &lp3971_dcdc_ops
,
305 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
306 .volt_table
= buck_voltage_map
,
307 .type
= REGULATOR_VOLTAGE
,
308 .owner
= THIS_MODULE
,
313 .ops
= &lp3971_dcdc_ops
,
314 .n_voltages
= ARRAY_SIZE(buck_voltage_map
),
315 .volt_table
= buck_voltage_map
,
316 .type
= REGULATOR_VOLTAGE
,
317 .owner
= THIS_MODULE
,
321 static int lp3971_i2c_read(struct i2c_client
*i2c
, char reg
, int count
,
328 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
336 static int lp3971_i2c_write(struct i2c_client
*i2c
, char reg
, int count
,
341 return i2c_smbus_write_byte_data(i2c
, reg
, *src
);
344 static u8
lp3971_reg_read(struct lp3971
*lp3971
, u8 reg
)
348 mutex_lock(&lp3971
->io_lock
);
350 lp3971_i2c_read(lp3971
->i2c
, reg
, 1, &val
);
352 dev_dbg(lp3971
->dev
, "reg read 0x%02x -> 0x%02x\n", (int)reg
,
355 mutex_unlock(&lp3971
->io_lock
);
360 static int lp3971_set_bits(struct lp3971
*lp3971
, u8 reg
, u16 mask
, u16 val
)
365 mutex_lock(&lp3971
->io_lock
);
367 ret
= lp3971_i2c_read(lp3971
->i2c
, reg
, 1, &tmp
);
368 tmp
= (tmp
& ~mask
) | val
;
370 ret
= lp3971_i2c_write(lp3971
->i2c
, reg
, 1, &tmp
);
371 dev_dbg(lp3971
->dev
, "reg write 0x%02x -> 0x%02x\n", (int)reg
,
374 mutex_unlock(&lp3971
->io_lock
);
379 static int setup_regulators(struct lp3971
*lp3971
,
380 struct lp3971_platform_data
*pdata
)
384 lp3971
->num_regulators
= pdata
->num_regulators
;
385 lp3971
->rdev
= kcalloc(pdata
->num_regulators
,
386 sizeof(struct regulator_dev
*), GFP_KERNEL
);
392 /* Instantiate the regulators */
393 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
394 struct regulator_config config
= { };
395 struct lp3971_regulator_subdev
*reg
= &pdata
->regulators
[i
];
397 config
.dev
= lp3971
->dev
;
398 config
.init_data
= reg
->initdata
;
399 config
.driver_data
= lp3971
;
401 lp3971
->rdev
[i
] = regulator_register(®ulators
[reg
->id
],
403 if (IS_ERR(lp3971
->rdev
[i
])) {
404 err
= PTR_ERR(lp3971
->rdev
[i
]);
405 dev_err(lp3971
->dev
, "regulator init failed: %d\n",
415 regulator_unregister(lp3971
->rdev
[i
]);
422 static int lp3971_i2c_probe(struct i2c_client
*i2c
,
423 const struct i2c_device_id
*id
)
425 struct lp3971
*lp3971
;
426 struct lp3971_platform_data
*pdata
= i2c
->dev
.platform_data
;
431 dev_dbg(&i2c
->dev
, "No platform init data supplied\n");
435 lp3971
= kzalloc(sizeof(struct lp3971
), GFP_KERNEL
);
440 lp3971
->dev
= &i2c
->dev
;
442 mutex_init(&lp3971
->io_lock
);
445 ret
= lp3971_i2c_read(i2c
, LP3971_SYS_CONTROL1_REG
, 1, &val
);
446 if (ret
== 0 && (val
& SYS_CONTROL1_INIT_MASK
) != SYS_CONTROL1_INIT_VAL
)
449 dev_err(&i2c
->dev
, "failed to detect device\n");
453 ret
= setup_regulators(lp3971
, pdata
);
457 i2c_set_clientdata(i2c
, lp3971
);
465 static int lp3971_i2c_remove(struct i2c_client
*i2c
)
467 struct lp3971
*lp3971
= i2c_get_clientdata(i2c
);
470 for (i
= 0; i
< lp3971
->num_regulators
; i
++)
471 regulator_unregister(lp3971
->rdev
[i
]);
479 static const struct i2c_device_id lp3971_i2c_id
[] = {
483 MODULE_DEVICE_TABLE(i2c
, lp3971_i2c_id
);
485 static struct i2c_driver lp3971_i2c_driver
= {
488 .owner
= THIS_MODULE
,
490 .probe
= lp3971_i2c_probe
,
491 .remove
= lp3971_i2c_remove
,
492 .id_table
= lp3971_i2c_id
,
495 module_i2c_driver(lp3971_i2c_driver
);
497 MODULE_LICENSE("GPL");
498 MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
499 MODULE_DESCRIPTION("LP3971 PMIC driver");