2 * Regulator driver for National Semiconductors LP3972 PMIC chip
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/bug.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/lp3972.h>
18 #include <linux/slab.h>
23 struct i2c_client
*i2c
;
25 struct regulator_dev
**rdev
;
28 /* LP3972 Control Registers */
29 #define LP3972_SCR_REG 0x07
30 #define LP3972_OVER1_REG 0x10
31 #define LP3972_OVSR1_REG 0x11
32 #define LP3972_OVER2_REG 0x12
33 #define LP3972_OVSR2_REG 0x13
34 #define LP3972_VCC1_REG 0x20
35 #define LP3972_ADTV1_REG 0x23
36 #define LP3972_ADTV2_REG 0x24
37 #define LP3972_AVRC_REG 0x25
38 #define LP3972_CDTC1_REG 0x26
39 #define LP3972_CDTC2_REG 0x27
40 #define LP3972_SDTV1_REG 0x29
41 #define LP3972_SDTV2_REG 0x2A
42 #define LP3972_MDTV1_REG 0x32
43 #define LP3972_MDTV2_REG 0x33
44 #define LP3972_L2VCR_REG 0x39
45 #define LP3972_L34VCR_REG 0x3A
46 #define LP3972_SCR1_REG 0x80
47 #define LP3972_SCR2_REG 0x81
48 #define LP3972_OEN3_REG 0x82
49 #define LP3972_OSR3_REG 0x83
50 #define LP3972_LOER4_REG 0x84
51 #define LP3972_B2TV_REG 0x85
52 #define LP3972_B3TV_REG 0x86
53 #define LP3972_B32RC_REG 0x87
54 #define LP3972_ISRA_REG 0x88
55 #define LP3972_BCCR_REG 0x89
56 #define LP3972_II1RR_REG 0x8E
57 #define LP3972_II2RR_REG 0x8F
59 #define LP3972_SYS_CONTROL1_REG LP3972_SCR1_REG
60 /* System control register 1 initial value,
61 * bits 5, 6 and 7 are EPROM programmable */
62 #define SYS_CONTROL1_INIT_VAL 0x02
63 #define SYS_CONTROL1_INIT_MASK 0x1F
65 #define LP3972_VOL_CHANGE_REG LP3972_VCC1_REG
66 #define LP3972_VOL_CHANGE_FLAG_GO 0x01
67 #define LP3972_VOL_CHANGE_FLAG_MASK 0x03
69 /* LDO output enable mask */
70 #define LP3972_OEN3_L1EN BIT(0)
71 #define LP3972_OVER2_LDO2_EN BIT(2)
72 #define LP3972_OVER2_LDO3_EN BIT(3)
73 #define LP3972_OVER2_LDO4_EN BIT(4)
74 #define LP3972_OVER1_S_EN BIT(2)
76 static const int ldo1_voltage_map
[] = {
77 1700, 1725, 1750, 1775, 1800, 1825, 1850, 1875,
78 1900, 1925, 1950, 1975, 2000,
81 static const int ldo23_voltage_map
[] = {
82 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500,
83 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300,
86 static const int ldo4_voltage_map
[] = {
87 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350,
88 1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300,
91 static const int ldo5_voltage_map
[] = {
92 0, 0, 0, 0, 0, 850, 875, 900,
93 925, 950, 975, 1000, 1025, 1050, 1075, 1100,
94 1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
95 1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
98 static const int buck1_voltage_map
[] = {
99 725, 750, 775, 800, 825, 850, 875, 900,
100 925, 950, 975, 1000, 1025, 1050, 1075, 1100,
101 1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
102 1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
105 static const int buck23_voltage_map
[] = {
106 0, 800, 850, 900, 950, 1000, 1050, 1100,
107 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500,
108 1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800,
112 static const int *ldo_voltage_map
[] = {
120 static const int *buck_voltage_map
[] = {
126 static const int ldo_output_enable_mask
[] = {
128 LP3972_OVER2_LDO2_EN
,
129 LP3972_OVER2_LDO3_EN
,
130 LP3972_OVER2_LDO4_EN
,
134 static const int ldo_output_enable_addr
[] = {
142 static const int ldo_vol_ctl_addr
[] = {
150 static const int buck_vol_enable_addr
[] = {
156 static const int buck_base_addr
[] = {
162 #define LP3972_LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[x])
163 #define LP3972_LDO_OUTPUT_ENABLE_MASK(x) (ldo_output_enable_mask[x])
164 #define LP3972_LDO_OUTPUT_ENABLE_REG(x) (ldo_output_enable_addr[x])
166 /* LDO voltage control registers shift:
167 LP3972_LDO1 -> 0, LP3972_LDO2 -> 4
168 LP3972_LDO3 -> 0, LP3972_LDO4 -> 4
171 #define LP3972_LDO_VOL_CONTR_SHIFT(x) (((x) & 1) << 2)
172 #define LP3972_LDO_VOL_CONTR_REG(x) (ldo_vol_ctl_addr[x])
173 #define LP3972_LDO_VOL_CHANGE_SHIFT(x) ((x) ? 4 : 6)
175 #define LP3972_LDO_VOL_MASK(x) (((x) % 4) ? 0x0f : 0x1f)
176 #define LP3972_LDO_VOL_MIN_IDX(x) (((x) == 4) ? 0x05 : 0x00)
177 #define LP3972_LDO_VOL_MAX_IDX(x) ((x) ? (((x) == 4) ? 0x1f : 0x0f) : 0x0c)
179 #define LP3972_BUCK_VOL_VALUE_MAP(x) (buck_voltage_map[x])
180 #define LP3972_BUCK_VOL_ENABLE_REG(x) (buck_vol_enable_addr[x])
181 #define LP3972_BUCK_VOL1_REG(x) (buck_base_addr[x])
182 #define LP3972_BUCK_VOL_MASK 0x1f
183 #define LP3972_BUCK_VOL_MIN_IDX(x) ((x) ? 0x01 : 0x00)
184 #define LP3972_BUCK_VOL_MAX_IDX(x) ((x) ? 0x19 : 0x1f)
186 static int lp3972_i2c_read(struct i2c_client
*i2c
, char reg
, int count
,
193 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
201 static int lp3972_i2c_write(struct i2c_client
*i2c
, char reg
, int count
,
206 return i2c_smbus_write_byte_data(i2c
, reg
, *src
);
209 static u8
lp3972_reg_read(struct lp3972
*lp3972
, u8 reg
)
213 mutex_lock(&lp3972
->io_lock
);
215 lp3972_i2c_read(lp3972
->i2c
, reg
, 1, &val
);
217 dev_dbg(lp3972
->dev
, "reg read 0x%02x -> 0x%02x\n", (int)reg
,
218 (unsigned)val
& 0xff);
220 mutex_unlock(&lp3972
->io_lock
);
225 static int lp3972_set_bits(struct lp3972
*lp3972
, u8 reg
, u16 mask
, u16 val
)
230 mutex_lock(&lp3972
->io_lock
);
232 ret
= lp3972_i2c_read(lp3972
->i2c
, reg
, 1, &tmp
);
233 tmp
= (tmp
& ~mask
) | val
;
235 ret
= lp3972_i2c_write(lp3972
->i2c
, reg
, 1, &tmp
);
236 dev_dbg(lp3972
->dev
, "reg write 0x%02x -> 0x%02x\n", (int)reg
,
237 (unsigned)val
& 0xff);
239 mutex_unlock(&lp3972
->io_lock
);
244 static int lp3972_ldo_list_voltage(struct regulator_dev
*dev
, unsigned index
)
246 int ldo
= rdev_get_id(dev
) - LP3972_LDO1
;
247 return 1000 * LP3972_LDO_VOL_VALUE_MAP(ldo
)[index
];
250 static int lp3972_ldo_is_enabled(struct regulator_dev
*dev
)
252 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
253 int ldo
= rdev_get_id(dev
) - LP3972_LDO1
;
254 u16 mask
= LP3972_LDO_OUTPUT_ENABLE_MASK(ldo
);
257 val
= lp3972_reg_read(lp3972
, LP3972_LDO_OUTPUT_ENABLE_REG(ldo
));
258 return !!(val
& mask
);
261 static int lp3972_ldo_enable(struct regulator_dev
*dev
)
263 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
264 int ldo
= rdev_get_id(dev
) - LP3972_LDO1
;
265 u16 mask
= LP3972_LDO_OUTPUT_ENABLE_MASK(ldo
);
267 return lp3972_set_bits(lp3972
, LP3972_LDO_OUTPUT_ENABLE_REG(ldo
),
271 static int lp3972_ldo_disable(struct regulator_dev
*dev
)
273 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
274 int ldo
= rdev_get_id(dev
) - LP3972_LDO1
;
275 u16 mask
= LP3972_LDO_OUTPUT_ENABLE_MASK(ldo
);
277 return lp3972_set_bits(lp3972
, LP3972_LDO_OUTPUT_ENABLE_REG(ldo
),
281 static int lp3972_ldo_get_voltage(struct regulator_dev
*dev
)
283 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
284 int ldo
= rdev_get_id(dev
) - LP3972_LDO1
;
285 u16 mask
= LP3972_LDO_VOL_MASK(ldo
);
288 reg
= lp3972_reg_read(lp3972
, LP3972_LDO_VOL_CONTR_REG(ldo
));
289 val
= (reg
>> LP3972_LDO_VOL_CONTR_SHIFT(ldo
)) & mask
;
291 return 1000 * LP3972_LDO_VOL_VALUE_MAP(ldo
)[val
];
294 static int lp3972_ldo_set_voltage(struct regulator_dev
*dev
,
295 int min_uV
, int max_uV
,
296 unsigned int *selector
)
298 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
299 int ldo
= rdev_get_id(dev
) - LP3972_LDO1
;
300 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
301 const int *vol_map
= LP3972_LDO_VOL_VALUE_MAP(ldo
);
305 if (min_vol
< vol_map
[LP3972_LDO_VOL_MIN_IDX(ldo
)] ||
306 min_vol
> vol_map
[LP3972_LDO_VOL_MAX_IDX(ldo
)])
309 for (val
= LP3972_LDO_VOL_MIN_IDX(ldo
);
310 val
<= LP3972_LDO_VOL_MAX_IDX(ldo
); val
++)
311 if (vol_map
[val
] >= min_vol
)
314 if (val
> LP3972_LDO_VOL_MAX_IDX(ldo
) || vol_map
[val
] > max_vol
)
319 shift
= LP3972_LDO_VOL_CONTR_SHIFT(ldo
);
320 ret
= lp3972_set_bits(lp3972
, LP3972_LDO_VOL_CONTR_REG(ldo
),
321 LP3972_LDO_VOL_MASK(ldo
) << shift
, val
<< shift
);
327 * LDO1 and LDO5 support voltage control by either target voltage1
328 * or target voltage2 register.
329 * We use target voltage1 register for LDO1 and LDO5 in this driver.
330 * We need to update voltage change control register(0x20) to enable
331 * LDO1 and LDO5 to change to their programmed target values.
336 shift
= LP3972_LDO_VOL_CHANGE_SHIFT(ldo
);
337 ret
= lp3972_set_bits(lp3972
, LP3972_VOL_CHANGE_REG
,
338 LP3972_VOL_CHANGE_FLAG_MASK
<< shift
,
339 LP3972_VOL_CHANGE_FLAG_GO
<< shift
);
343 ret
= lp3972_set_bits(lp3972
, LP3972_VOL_CHANGE_REG
,
344 LP3972_VOL_CHANGE_FLAG_MASK
<< shift
, 0);
351 static struct regulator_ops lp3972_ldo_ops
= {
352 .list_voltage
= lp3972_ldo_list_voltage
,
353 .is_enabled
= lp3972_ldo_is_enabled
,
354 .enable
= lp3972_ldo_enable
,
355 .disable
= lp3972_ldo_disable
,
356 .get_voltage
= lp3972_ldo_get_voltage
,
357 .set_voltage
= lp3972_ldo_set_voltage
,
360 static int lp3972_dcdc_list_voltage(struct regulator_dev
*dev
, unsigned index
)
362 int buck
= rdev_get_id(dev
) - LP3972_DCDC1
;
363 return 1000 * buck_voltage_map
[buck
][index
];
366 static int lp3972_dcdc_is_enabled(struct regulator_dev
*dev
)
368 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
369 int buck
= rdev_get_id(dev
) - LP3972_DCDC1
;
370 u16 mask
= 1 << (buck
* 2);
373 val
= lp3972_reg_read(lp3972
, LP3972_BUCK_VOL_ENABLE_REG(buck
));
374 return !!(val
& mask
);
377 static int lp3972_dcdc_enable(struct regulator_dev
*dev
)
379 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
380 int buck
= rdev_get_id(dev
) - LP3972_DCDC1
;
381 u16 mask
= 1 << (buck
* 2);
384 val
= lp3972_set_bits(lp3972
, LP3972_BUCK_VOL_ENABLE_REG(buck
),
389 static int lp3972_dcdc_disable(struct regulator_dev
*dev
)
391 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
392 int buck
= rdev_get_id(dev
) - LP3972_DCDC1
;
393 u16 mask
= 1 << (buck
* 2);
396 val
= lp3972_set_bits(lp3972
, LP3972_BUCK_VOL_ENABLE_REG(buck
),
401 static int lp3972_dcdc_get_voltage(struct regulator_dev
*dev
)
403 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
404 int buck
= rdev_get_id(dev
) - LP3972_DCDC1
;
408 reg
= lp3972_reg_read(lp3972
, LP3972_BUCK_VOL1_REG(buck
));
409 reg
&= LP3972_BUCK_VOL_MASK
;
410 if (reg
<= LP3972_BUCK_VOL_MAX_IDX(buck
))
411 val
= 1000 * buck_voltage_map
[buck
][reg
];
414 dev_warn(&dev
->dev
, "chip reported incorrect voltage value."
421 static int lp3972_dcdc_set_voltage(struct regulator_dev
*dev
,
422 int min_uV
, int max_uV
,
423 unsigned int *selector
)
425 struct lp3972
*lp3972
= rdev_get_drvdata(dev
);
426 int buck
= rdev_get_id(dev
) - LP3972_DCDC1
;
427 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
428 const int *vol_map
= buck_voltage_map
[buck
];
432 if (min_vol
< vol_map
[LP3972_BUCK_VOL_MIN_IDX(buck
)] ||
433 min_vol
> vol_map
[LP3972_BUCK_VOL_MAX_IDX(buck
)])
436 for (val
= LP3972_BUCK_VOL_MIN_IDX(buck
);
437 val
<= LP3972_BUCK_VOL_MAX_IDX(buck
); val
++)
438 if (vol_map
[val
] >= min_vol
)
441 if (val
> LP3972_BUCK_VOL_MAX_IDX(buck
) ||
442 vol_map
[val
] > max_vol
)
447 ret
= lp3972_set_bits(lp3972
, LP3972_BUCK_VOL1_REG(buck
),
448 LP3972_BUCK_VOL_MASK
, val
);
455 ret
= lp3972_set_bits(lp3972
, LP3972_VOL_CHANGE_REG
,
456 LP3972_VOL_CHANGE_FLAG_MASK
, LP3972_VOL_CHANGE_FLAG_GO
);
460 return lp3972_set_bits(lp3972
, LP3972_VOL_CHANGE_REG
,
461 LP3972_VOL_CHANGE_FLAG_MASK
, 0);
464 static struct regulator_ops lp3972_dcdc_ops
= {
465 .list_voltage
= lp3972_dcdc_list_voltage
,
466 .is_enabled
= lp3972_dcdc_is_enabled
,
467 .enable
= lp3972_dcdc_enable
,
468 .disable
= lp3972_dcdc_disable
,
469 .get_voltage
= lp3972_dcdc_get_voltage
,
470 .set_voltage
= lp3972_dcdc_set_voltage
,
473 static struct regulator_desc regulators
[] = {
477 .ops
= &lp3972_ldo_ops
,
478 .n_voltages
= ARRAY_SIZE(ldo1_voltage_map
),
479 .type
= REGULATOR_VOLTAGE
,
480 .owner
= THIS_MODULE
,
485 .ops
= &lp3972_ldo_ops
,
486 .n_voltages
= ARRAY_SIZE(ldo23_voltage_map
),
487 .type
= REGULATOR_VOLTAGE
,
488 .owner
= THIS_MODULE
,
493 .ops
= &lp3972_ldo_ops
,
494 .n_voltages
= ARRAY_SIZE(ldo23_voltage_map
),
495 .type
= REGULATOR_VOLTAGE
,
496 .owner
= THIS_MODULE
,
501 .ops
= &lp3972_ldo_ops
,
502 .n_voltages
= ARRAY_SIZE(ldo4_voltage_map
),
503 .type
= REGULATOR_VOLTAGE
,
504 .owner
= THIS_MODULE
,
509 .ops
= &lp3972_ldo_ops
,
510 .n_voltages
= ARRAY_SIZE(ldo5_voltage_map
),
511 .type
= REGULATOR_VOLTAGE
,
512 .owner
= THIS_MODULE
,
517 .ops
= &lp3972_dcdc_ops
,
518 .n_voltages
= ARRAY_SIZE(buck1_voltage_map
),
519 .type
= REGULATOR_VOLTAGE
,
520 .owner
= THIS_MODULE
,
525 .ops
= &lp3972_dcdc_ops
,
526 .n_voltages
= ARRAY_SIZE(buck23_voltage_map
),
527 .type
= REGULATOR_VOLTAGE
,
528 .owner
= THIS_MODULE
,
533 .ops
= &lp3972_dcdc_ops
,
534 .n_voltages
= ARRAY_SIZE(buck23_voltage_map
),
535 .type
= REGULATOR_VOLTAGE
,
536 .owner
= THIS_MODULE
,
540 static int __devinit
setup_regulators(struct lp3972
*lp3972
,
541 struct lp3972_platform_data
*pdata
)
545 lp3972
->num_regulators
= pdata
->num_regulators
;
546 lp3972
->rdev
= kcalloc(pdata
->num_regulators
,
547 sizeof(struct regulator_dev
*), GFP_KERNEL
);
553 /* Instantiate the regulators */
554 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
555 struct lp3972_regulator_subdev
*reg
= &pdata
->regulators
[i
];
556 lp3972
->rdev
[i
] = regulator_register(®ulators
[reg
->id
],
557 lp3972
->dev
, reg
->initdata
, lp3972
);
559 if (IS_ERR(lp3972
->rdev
[i
])) {
560 err
= PTR_ERR(lp3972
->rdev
[i
]);
561 dev_err(lp3972
->dev
, "regulator init failed: %d\n",
570 regulator_unregister(lp3972
->rdev
[i
]);
577 static int __devinit
lp3972_i2c_probe(struct i2c_client
*i2c
,
578 const struct i2c_device_id
*id
)
580 struct lp3972
*lp3972
;
581 struct lp3972_platform_data
*pdata
= i2c
->dev
.platform_data
;
586 dev_dbg(&i2c
->dev
, "No platform init data supplied\n");
590 lp3972
= kzalloc(sizeof(struct lp3972
), GFP_KERNEL
);
595 lp3972
->dev
= &i2c
->dev
;
597 mutex_init(&lp3972
->io_lock
);
600 ret
= lp3972_i2c_read(i2c
, LP3972_SYS_CONTROL1_REG
, 1, &val
);
602 (val
& SYS_CONTROL1_INIT_MASK
) != SYS_CONTROL1_INIT_VAL
) {
604 dev_err(&i2c
->dev
, "chip reported: val = 0x%x\n", val
);
607 dev_err(&i2c
->dev
, "failed to detect device. ret = %d\n", ret
);
611 ret
= setup_regulators(lp3972
, pdata
);
615 i2c_set_clientdata(i2c
, lp3972
);
623 static int __devexit
lp3972_i2c_remove(struct i2c_client
*i2c
)
625 struct lp3972
*lp3972
= i2c_get_clientdata(i2c
);
628 for (i
= 0; i
< lp3972
->num_regulators
; i
++)
629 regulator_unregister(lp3972
->rdev
[i
]);
636 static const struct i2c_device_id lp3972_i2c_id
[] = {
640 MODULE_DEVICE_TABLE(i2c
, lp3972_i2c_id
);
642 static struct i2c_driver lp3972_i2c_driver
= {
645 .owner
= THIS_MODULE
,
647 .probe
= lp3972_i2c_probe
,
648 .remove
= __devexit_p(lp3972_i2c_remove
),
649 .id_table
= lp3972_i2c_id
,
652 static int __init
lp3972_module_init(void)
654 return i2c_add_driver(&lp3972_i2c_driver
);
656 subsys_initcall(lp3972_module_init
);
658 static void __exit
lp3972_module_exit(void)
660 i2c_del_driver(&lp3972_i2c_driver
);
662 module_exit(lp3972_module_exit
);
664 MODULE_LICENSE("GPL");
665 MODULE_AUTHOR("Axel Lin <axel.lin@gmail.com>");
666 MODULE_DESCRIPTION("LP3972 PMIC driver");