2 * Copyright 2012 Texas Instruments
4 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
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/module.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/regmap.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/regulator/lp872x.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/platform_device.h>
22 /* Registers : LP8720/8725 shared */
23 #define LP872X_GENERAL_CFG 0x00
24 #define LP872X_LDO1_VOUT 0x01
25 #define LP872X_LDO2_VOUT 0x02
26 #define LP872X_LDO3_VOUT 0x03
27 #define LP872X_LDO4_VOUT 0x04
28 #define LP872X_LDO5_VOUT 0x05
30 /* Registers : LP8720 */
31 #define LP8720_BUCK_VOUT1 0x06
32 #define LP8720_BUCK_VOUT2 0x07
33 #define LP8720_ENABLE 0x08
35 /* Registers : LP8725 */
36 #define LP8725_LILO1_VOUT 0x06
37 #define LP8725_LILO2_VOUT 0x07
38 #define LP8725_BUCK1_VOUT1 0x08
39 #define LP8725_BUCK1_VOUT2 0x09
40 #define LP8725_BUCK2_VOUT1 0x0A
41 #define LP8725_BUCK2_VOUT2 0x0B
42 #define LP8725_BUCK_CTRL 0x0C
43 #define LP8725_LDO_CTRL 0x0D
45 /* Mask/shift : LP8720/LP8725 shared */
46 #define LP872X_VOUT_M 0x1F
47 #define LP872X_START_DELAY_M 0xE0
48 #define LP872X_START_DELAY_S 5
49 #define LP872X_EN_LDO1_M BIT(0)
50 #define LP872X_EN_LDO2_M BIT(1)
51 #define LP872X_EN_LDO3_M BIT(2)
52 #define LP872X_EN_LDO4_M BIT(3)
53 #define LP872X_EN_LDO5_M BIT(4)
55 /* Mask/shift : LP8720 */
56 #define LP8720_TIMESTEP_S 0 /* Addr 00h */
57 #define LP8720_TIMESTEP_M BIT(0)
58 #define LP8720_EXT_DVS_M BIT(2)
59 #define LP8720_BUCK_FPWM_S 5 /* Addr 07h */
60 #define LP8720_BUCK_FPWM_M BIT(5)
61 #define LP8720_EN_BUCK_M BIT(5) /* Addr 08h */
62 #define LP8720_DVS_SEL_M BIT(7)
64 /* Mask/shift : LP8725 */
65 #define LP8725_TIMESTEP_M 0xC0 /* Addr 00h */
66 #define LP8725_TIMESTEP_S 6
67 #define LP8725_BUCK1_EN_M BIT(0)
68 #define LP8725_DVS1_M BIT(2)
69 #define LP8725_DVS2_M BIT(3)
70 #define LP8725_BUCK2_EN_M BIT(4)
71 #define LP8725_BUCK_CL_M 0xC0 /* Addr 09h, 0Bh */
72 #define LP8725_BUCK_CL_S 6
73 #define LP8725_BUCK1_FPWM_S 1 /* Addr 0Ch */
74 #define LP8725_BUCK1_FPWM_M BIT(1)
75 #define LP8725_BUCK2_FPWM_S 5
76 #define LP8725_BUCK2_FPWM_M BIT(5)
77 #define LP8725_EN_LILO1_M BIT(5) /* Addr 0Dh */
78 #define LP8725_EN_LILO2_M BIT(6)
81 #define LP872X_FORCE_PWM 1
82 #define LP872X_AUTO_PWM 0
84 #define LP8720_NUM_REGULATORS 6
85 #define LP8725_NUM_REGULATORS 9
86 #define EXTERN_DVS_USED 0
89 /* Default DVS Mode */
90 #define LP8720_DEFAULT_DVS 0
91 #define LP8725_DEFAULT_DVS BIT(2)
93 /* dump registers in regmap-debugfs */
94 #define MAX_REGISTERS 0x0F
102 struct regmap *regmap;
104 enum lp872x_id chipid;
105 struct lp872x_platform_data *pdata;
106 struct regulator_dev **regulators;
108 enum lp872x_dvs_state dvs_pin;
112 /* LP8720/LP8725 shared voltage table for LDOs */
113 static const unsigned int lp872x_ldo_vtbl[] = {
114 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
115 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
116 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
117 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
120 /* LP8720 LDO4 voltage table */
121 static const unsigned int lp8720_ldo4_vtbl[] = {
122 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000,
123 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
124 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
125 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
128 /* LP8725 LILO(Low Input Low Output) voltage table */
129 static const unsigned int lp8725_lilo_vtbl[] = {
130 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
131 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
132 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
133 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
136 /* LP8720 BUCK voltage table */
137 #define EXT_R 0 /* external resistor divider */
138 static const unsigned int lp8720_buck_vtbl[] = {
139 EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
140 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
141 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
142 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
145 /* LP8725 BUCK voltage table */
146 static const unsigned int lp8725_buck_vtbl[] = {
147 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
148 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
149 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
150 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
153 /* LP8725 BUCK current limit */
154 static const unsigned int lp8725_buck_uA[] = {
155 460000, 780000, 1050000, 1370000,
158 static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
163 ret = regmap_read(lp->regmap, addr, &val);
165 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
173 static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
175 return regmap_write(lp->regmap, addr, data);
178 static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
179 unsigned int mask, u8 data)
181 return regmap_update_bits(lp->regmap, addr, mask, data);
184 static int _rdev_to_offset(struct regulator_dev *rdev)
186 enum lp872x_regulator_id id = rdev_get_id(rdev);
189 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
191 case LP8725_ID_LDO1 ... LP8725_ID_BUCK2:
192 return id - LP8725_ID_BASE;
198 static int lp872x_get_timestep_usec(struct lp872x *lp)
200 enum lp872x_id chip = lp->chipid;
202 int *time_usec, size, ret;
203 int lp8720_time_usec[] = { 25, 50 };
204 int lp8725_time_usec[] = { 32, 64, 128, 256 };
208 mask = LP8720_TIMESTEP_M;
209 shift = LP8720_TIMESTEP_S;
210 time_usec = &lp8720_time_usec[0];
211 size = ARRAY_SIZE(lp8720_time_usec);
214 mask = LP8725_TIMESTEP_M;
215 shift = LP8725_TIMESTEP_S;
216 time_usec = &lp8725_time_usec[0];
217 size = ARRAY_SIZE(lp8725_time_usec);
223 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
227 val = (val & mask) >> shift;
231 return *(time_usec + val);
234 static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
236 struct lp872x *lp = rdev_get_drvdata(rdev);
237 enum lp872x_regulator_id regulator = rdev_get_id(rdev);
238 int time_step_us = lp872x_get_timestep_usec(lp);
242 if (time_step_us < 0)
246 case LP8720_ID_LDO1 ... LP8720_ID_LDO5:
247 case LP8725_ID_LDO1 ... LP8725_ID_LILO2:
248 offset = _rdev_to_offset(rdev);
252 addr = LP872X_LDO1_VOUT + offset;
255 addr = LP8720_BUCK_VOUT1;
257 case LP8725_ID_BUCK1:
258 addr = LP8725_BUCK1_VOUT1;
260 case LP8725_ID_BUCK2:
261 addr = LP8725_BUCK2_VOUT1;
267 ret = lp872x_read_byte(lp, addr, &val);
271 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
273 return val > MAX_DELAY ? 0 : val * time_step_us;
276 static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
279 enum lp872x_dvs_state state;
281 state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
282 gpio_set_value(gpio, state);
286 static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
287 enum lp872x_regulator_id buck)
291 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
296 if (val & LP8720_EXT_DVS_M) {
297 addr = (lp->dvs_pin == DVS_HIGH) ?
298 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
300 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
303 addr = val & LP8720_DVS_SEL_M ?
304 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
307 case LP8725_ID_BUCK1:
308 if (val & LP8725_DVS1_M)
309 addr = LP8725_BUCK1_VOUT1;
311 addr = (lp->dvs_pin == DVS_HIGH) ?
312 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
314 case LP8725_ID_BUCK2:
315 addr = val & LP8725_DVS2_M ?
316 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
325 static bool lp872x_is_valid_buck_addr(u8 addr)
328 case LP8720_BUCK_VOUT1:
329 case LP8720_BUCK_VOUT2:
330 case LP8725_BUCK1_VOUT1:
331 case LP8725_BUCK1_VOUT2:
332 case LP8725_BUCK2_VOUT1:
333 case LP8725_BUCK2_VOUT2:
340 static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
343 struct lp872x *lp = rdev_get_drvdata(rdev);
344 enum lp872x_regulator_id buck = rdev_get_id(rdev);
345 u8 addr, mask = LP872X_VOUT_M;
346 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
348 if (dvs && gpio_is_valid(dvs->gpio))
349 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
351 addr = lp872x_select_buck_vout_addr(lp, buck);
352 if (!lp872x_is_valid_buck_addr(addr))
355 return lp872x_update_bits(lp, addr, mask, selector);
358 static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
360 struct lp872x *lp = rdev_get_drvdata(rdev);
361 enum lp872x_regulator_id buck = rdev_get_id(rdev);
365 addr = lp872x_select_buck_vout_addr(lp, buck);
366 if (!lp872x_is_valid_buck_addr(addr))
369 ret = lp872x_read_byte(lp, addr, &val);
373 return val & LP872X_VOUT_M;
376 static int lp8725_buck_set_current_limit(struct regulator_dev *rdev,
377 int min_uA, int max_uA)
379 struct lp872x *lp = rdev_get_drvdata(rdev);
380 enum lp872x_regulator_id buck = rdev_get_id(rdev);
385 case LP8725_ID_BUCK1:
386 addr = LP8725_BUCK1_VOUT2;
388 case LP8725_ID_BUCK2:
389 addr = LP8725_BUCK2_VOUT2;
395 for (i = ARRAY_SIZE(lp8725_buck_uA) - 1 ; i >= 0; i--) {
396 if (lp8725_buck_uA[i] >= min_uA &&
397 lp8725_buck_uA[i] <= max_uA)
398 return lp872x_update_bits(lp, addr,
400 i << LP8725_BUCK_CL_S);
406 static int lp8725_buck_get_current_limit(struct regulator_dev *rdev)
408 struct lp872x *lp = rdev_get_drvdata(rdev);
409 enum lp872x_regulator_id buck = rdev_get_id(rdev);
414 case LP8725_ID_BUCK1:
415 addr = LP8725_BUCK1_VOUT2;
417 case LP8725_ID_BUCK2:
418 addr = LP8725_BUCK2_VOUT2;
424 ret = lp872x_read_byte(lp, addr, &val);
428 val = (val & LP8725_BUCK_CL_M) >> LP8725_BUCK_CL_S;
430 return (val < ARRAY_SIZE(lp8725_buck_uA)) ?
431 lp8725_buck_uA[val] : -EINVAL;
434 static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
436 struct lp872x *lp = rdev_get_drvdata(rdev);
437 enum lp872x_regulator_id buck = rdev_get_id(rdev);
438 u8 addr, mask, shift, val;
442 addr = LP8720_BUCK_VOUT2;
443 mask = LP8720_BUCK_FPWM_M;
444 shift = LP8720_BUCK_FPWM_S;
446 case LP8725_ID_BUCK1:
447 addr = LP8725_BUCK_CTRL;
448 mask = LP8725_BUCK1_FPWM_M;
449 shift = LP8725_BUCK1_FPWM_S;
451 case LP8725_ID_BUCK2:
452 addr = LP8725_BUCK_CTRL;
453 mask = LP8725_BUCK2_FPWM_M;
454 shift = LP8725_BUCK2_FPWM_S;
460 if (mode == REGULATOR_MODE_FAST)
461 val = LP872X_FORCE_PWM << shift;
462 else if (mode == REGULATOR_MODE_NORMAL)
463 val = LP872X_AUTO_PWM << shift;
467 return lp872x_update_bits(lp, addr, mask, val);
470 static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
472 struct lp872x *lp = rdev_get_drvdata(rdev);
473 enum lp872x_regulator_id buck = rdev_get_id(rdev);
479 addr = LP8720_BUCK_VOUT2;
480 mask = LP8720_BUCK_FPWM_M;
482 case LP8725_ID_BUCK1:
483 addr = LP8725_BUCK_CTRL;
484 mask = LP8725_BUCK1_FPWM_M;
486 case LP8725_ID_BUCK2:
487 addr = LP8725_BUCK_CTRL;
488 mask = LP8725_BUCK2_FPWM_M;
494 ret = lp872x_read_byte(lp, addr, &val);
498 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
501 static struct regulator_ops lp872x_ldo_ops = {
502 .list_voltage = regulator_list_voltage_table,
503 .set_voltage_sel = regulator_set_voltage_sel_regmap,
504 .get_voltage_sel = regulator_get_voltage_sel_regmap,
505 .enable = regulator_enable_regmap,
506 .disable = regulator_disable_regmap,
507 .is_enabled = regulator_is_enabled_regmap,
508 .enable_time = lp872x_regulator_enable_time,
511 static struct regulator_ops lp8720_buck_ops = {
512 .list_voltage = regulator_list_voltage_table,
513 .set_voltage_sel = lp872x_buck_set_voltage_sel,
514 .get_voltage_sel = lp872x_buck_get_voltage_sel,
515 .enable = regulator_enable_regmap,
516 .disable = regulator_disable_regmap,
517 .is_enabled = regulator_is_enabled_regmap,
518 .enable_time = lp872x_regulator_enable_time,
519 .set_mode = lp872x_buck_set_mode,
520 .get_mode = lp872x_buck_get_mode,
523 static struct regulator_ops lp8725_buck_ops = {
524 .list_voltage = regulator_list_voltage_table,
525 .set_voltage_sel = lp872x_buck_set_voltage_sel,
526 .get_voltage_sel = lp872x_buck_get_voltage_sel,
527 .enable = regulator_enable_regmap,
528 .disable = regulator_disable_regmap,
529 .is_enabled = regulator_is_enabled_regmap,
530 .enable_time = lp872x_regulator_enable_time,
531 .set_mode = lp872x_buck_set_mode,
532 .get_mode = lp872x_buck_get_mode,
533 .set_current_limit = lp8725_buck_set_current_limit,
534 .get_current_limit = lp8725_buck_get_current_limit,
537 static struct regulator_desc lp8720_regulator_desc[] = {
540 .id = LP8720_ID_LDO1,
541 .ops = &lp872x_ldo_ops,
542 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
543 .volt_table = lp872x_ldo_vtbl,
544 .type = REGULATOR_VOLTAGE,
545 .owner = THIS_MODULE,
546 .vsel_reg = LP872X_LDO1_VOUT,
547 .vsel_mask = LP872X_VOUT_M,
548 .enable_reg = LP8720_ENABLE,
549 .enable_mask = LP872X_EN_LDO1_M,
553 .id = LP8720_ID_LDO2,
554 .ops = &lp872x_ldo_ops,
555 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
556 .volt_table = lp872x_ldo_vtbl,
557 .type = REGULATOR_VOLTAGE,
558 .owner = THIS_MODULE,
559 .vsel_reg = LP872X_LDO2_VOUT,
560 .vsel_mask = LP872X_VOUT_M,
561 .enable_reg = LP8720_ENABLE,
562 .enable_mask = LP872X_EN_LDO2_M,
566 .id = LP8720_ID_LDO3,
567 .ops = &lp872x_ldo_ops,
568 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
569 .volt_table = lp872x_ldo_vtbl,
570 .type = REGULATOR_VOLTAGE,
571 .owner = THIS_MODULE,
572 .vsel_reg = LP872X_LDO3_VOUT,
573 .vsel_mask = LP872X_VOUT_M,
574 .enable_reg = LP8720_ENABLE,
575 .enable_mask = LP872X_EN_LDO3_M,
579 .id = LP8720_ID_LDO4,
580 .ops = &lp872x_ldo_ops,
581 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
582 .volt_table = lp8720_ldo4_vtbl,
583 .type = REGULATOR_VOLTAGE,
584 .owner = THIS_MODULE,
585 .vsel_reg = LP872X_LDO4_VOUT,
586 .vsel_mask = LP872X_VOUT_M,
587 .enable_reg = LP8720_ENABLE,
588 .enable_mask = LP872X_EN_LDO4_M,
592 .id = LP8720_ID_LDO5,
593 .ops = &lp872x_ldo_ops,
594 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
595 .volt_table = lp872x_ldo_vtbl,
596 .type = REGULATOR_VOLTAGE,
597 .owner = THIS_MODULE,
598 .vsel_reg = LP872X_LDO5_VOUT,
599 .vsel_mask = LP872X_VOUT_M,
600 .enable_reg = LP8720_ENABLE,
601 .enable_mask = LP872X_EN_LDO5_M,
605 .id = LP8720_ID_BUCK,
606 .ops = &lp8720_buck_ops,
607 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
608 .volt_table = lp8720_buck_vtbl,
609 .type = REGULATOR_VOLTAGE,
610 .owner = THIS_MODULE,
611 .enable_reg = LP8720_ENABLE,
612 .enable_mask = LP8720_EN_BUCK_M,
616 static struct regulator_desc lp8725_regulator_desc[] = {
619 .id = LP8725_ID_LDO1,
620 .ops = &lp872x_ldo_ops,
621 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
622 .volt_table = lp872x_ldo_vtbl,
623 .type = REGULATOR_VOLTAGE,
624 .owner = THIS_MODULE,
625 .vsel_reg = LP872X_LDO1_VOUT,
626 .vsel_mask = LP872X_VOUT_M,
627 .enable_reg = LP8725_LDO_CTRL,
628 .enable_mask = LP872X_EN_LDO1_M,
632 .id = LP8725_ID_LDO2,
633 .ops = &lp872x_ldo_ops,
634 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
635 .volt_table = lp872x_ldo_vtbl,
636 .type = REGULATOR_VOLTAGE,
637 .owner = THIS_MODULE,
638 .vsel_reg = LP872X_LDO2_VOUT,
639 .vsel_mask = LP872X_VOUT_M,
640 .enable_reg = LP8725_LDO_CTRL,
641 .enable_mask = LP872X_EN_LDO2_M,
645 .id = LP8725_ID_LDO3,
646 .ops = &lp872x_ldo_ops,
647 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
648 .volt_table = lp872x_ldo_vtbl,
649 .type = REGULATOR_VOLTAGE,
650 .owner = THIS_MODULE,
651 .vsel_reg = LP872X_LDO3_VOUT,
652 .vsel_mask = LP872X_VOUT_M,
653 .enable_reg = LP8725_LDO_CTRL,
654 .enable_mask = LP872X_EN_LDO3_M,
658 .id = LP8725_ID_LDO4,
659 .ops = &lp872x_ldo_ops,
660 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
661 .volt_table = lp872x_ldo_vtbl,
662 .type = REGULATOR_VOLTAGE,
663 .owner = THIS_MODULE,
664 .vsel_reg = LP872X_LDO4_VOUT,
665 .vsel_mask = LP872X_VOUT_M,
666 .enable_reg = LP8725_LDO_CTRL,
667 .enable_mask = LP872X_EN_LDO4_M,
671 .id = LP8725_ID_LDO5,
672 .ops = &lp872x_ldo_ops,
673 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
674 .volt_table = lp872x_ldo_vtbl,
675 .type = REGULATOR_VOLTAGE,
676 .owner = THIS_MODULE,
677 .vsel_reg = LP872X_LDO5_VOUT,
678 .vsel_mask = LP872X_VOUT_M,
679 .enable_reg = LP8725_LDO_CTRL,
680 .enable_mask = LP872X_EN_LDO5_M,
684 .id = LP8725_ID_LILO1,
685 .ops = &lp872x_ldo_ops,
686 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
687 .volt_table = lp8725_lilo_vtbl,
688 .type = REGULATOR_VOLTAGE,
689 .owner = THIS_MODULE,
690 .vsel_reg = LP8725_LILO1_VOUT,
691 .vsel_mask = LP872X_VOUT_M,
692 .enable_reg = LP8725_LDO_CTRL,
693 .enable_mask = LP8725_EN_LILO1_M,
697 .id = LP8725_ID_LILO2,
698 .ops = &lp872x_ldo_ops,
699 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
700 .volt_table = lp8725_lilo_vtbl,
701 .type = REGULATOR_VOLTAGE,
702 .owner = THIS_MODULE,
703 .vsel_reg = LP8725_LILO2_VOUT,
704 .vsel_mask = LP872X_VOUT_M,
705 .enable_reg = LP8725_LDO_CTRL,
706 .enable_mask = LP8725_EN_LILO2_M,
710 .id = LP8725_ID_BUCK1,
711 .ops = &lp8725_buck_ops,
712 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
713 .volt_table = lp8725_buck_vtbl,
714 .type = REGULATOR_VOLTAGE,
715 .owner = THIS_MODULE,
716 .enable_reg = LP872X_GENERAL_CFG,
717 .enable_mask = LP8725_BUCK1_EN_M,
721 .id = LP8725_ID_BUCK2,
722 .ops = &lp8725_buck_ops,
723 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
724 .volt_table = lp8725_buck_vtbl,
725 .type = REGULATOR_VOLTAGE,
726 .owner = THIS_MODULE,
727 .enable_reg = LP872X_GENERAL_CFG,
728 .enable_mask = LP8725_BUCK2_EN_M,
732 static int lp872x_init_dvs(struct lp872x *lp)
735 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
736 enum lp872x_dvs_state pinstate;
737 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
738 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
741 goto set_default_dvs_mode;
744 if (!gpio_is_valid(gpio)) {
745 dev_err(lp->dev, "invalid gpio: %d\n", gpio);
749 pinstate = dvs->init_state;
750 ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
752 dev_err(lp->dev, "gpio request err: %d\n", ret);
756 lp->dvs_pin = pinstate;
761 set_default_dvs_mode:
762 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
763 default_dvs_mode[lp->chipid]);
766 static int lp872x_config(struct lp872x *lp)
768 struct lp872x_platform_data *pdata = lp->pdata;
771 if (!pdata || !pdata->update_config)
774 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
779 return lp872x_init_dvs(lp);
782 static struct regulator_init_data
783 *lp872x_find_regulator_init_data(int id, struct lp872x *lp)
785 struct lp872x_platform_data *pdata = lp->pdata;
791 for (i = 0; i < lp->num_regulators; i++) {
792 if (pdata->regulator_data[i].id == id)
793 return pdata->regulator_data[i].init_data;
799 static int lp872x_regulator_register(struct lp872x *lp)
801 struct regulator_desc *desc;
802 struct regulator_config cfg = { };
803 struct regulator_dev *rdev;
806 for (i = 0 ; i < lp->num_regulators ; i++) {
807 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
808 &lp8725_regulator_desc[i];
811 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
812 cfg.driver_data = lp;
813 cfg.regmap = lp->regmap;
815 rdev = regulator_register(desc, &cfg);
817 dev_err(lp->dev, "regulator register err");
822 *(lp->regulators + i) = rdev;
828 rdev = *(lp->regulators + i);
829 regulator_unregister(rdev);
834 static void lp872x_regulator_unregister(struct lp872x *lp)
836 struct regulator_dev *rdev;
839 for (i = 0 ; i < lp->num_regulators ; i++) {
840 rdev = *(lp->regulators + i);
841 regulator_unregister(rdev);
845 static const struct regmap_config lp872x_regmap_config = {
848 .max_register = MAX_REGISTERS,
851 static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
854 int ret, size, num_regulators;
855 const int lp872x_num_regulators[] = {
856 [LP8720] = LP8720_NUM_REGULATORS,
857 [LP8725] = LP8725_NUM_REGULATORS,
860 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
864 num_regulators = lp872x_num_regulators[id->driver_data];
865 size = sizeof(struct regulator_dev *) * num_regulators;
867 lp->regulators = devm_kzalloc(&cl->dev, size, GFP_KERNEL);
871 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
872 if (IS_ERR(lp->regmap)) {
873 ret = PTR_ERR(lp->regmap);
874 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
879 lp->pdata = cl->dev.platform_data;
880 lp->chipid = id->driver_data;
881 lp->num_regulators = num_regulators;
882 i2c_set_clientdata(cl, lp);
884 ret = lp872x_config(lp);
888 return lp872x_regulator_register(lp);
896 static int __devexit lp872x_remove(struct i2c_client *cl)
898 struct lp872x *lp = i2c_get_clientdata(cl);
900 lp872x_regulator_unregister(lp);
904 static const struct i2c_device_id lp872x_ids[] = {
909 MODULE_DEVICE_TABLE(i2c, lp872x_ids);
911 static struct i2c_driver lp872x_driver = {
914 .owner = THIS_MODULE,
916 .probe = lp872x_probe,
917 .remove = __devexit_p(lp872x_remove),
918 .id_table = lp872x_ids,
921 module_i2c_driver(lp872x_driver);
923 MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
924 MODULE_AUTHOR("Milo Kim");
925 MODULE_LICENSE("GPL");