12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
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
31 #define LP8720_BUCK_VOUT1 0x06
32 #define LP8720_BUCK_VOUT2 0x07
33 #define LP8720_ENABLE 0x08
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
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)
56 #define LP8720_TIMESTEP_S 0
57 #define LP8720_TIMESTEP_M BIT(0)
58 #define LP8720_EXT_DVS_M BIT(2)
59 #define LP8720_BUCK_FPWM_S 5
60 #define LP8720_BUCK_FPWM_M BIT(5)
61 #define LP8720_EN_BUCK_M BIT(5)
62 #define LP8720_DVS_SEL_M BIT(7)
65 #define LP8725_TIMESTEP_M 0xC0
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
72 #define LP8725_BUCK_CL_S 6
73 #define LP8725_BUCK1_FPWM_S 1
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)
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
90 #define LP8720_DEFAULT_DVS 0
91 #define LP8725_DEFAULT_DVS BIT(2)
94 #define MAX_REGISTERS 0x0F
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,
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,
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,
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,
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,
154 static const unsigned int lp8725_buck_uA[] = {
155 460000, 780000, 1050000, 1370000,
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)
178 static inline int lp872x_update_bits(
struct lp872x *lp,
u8 addr,
179 unsigned int mask,
u8 data)
198 static int lp872x_get_timestep_usec(
struct lp872x *lp)
203 int lp8720_time_usec[] = { 25, 50 };
204 int lp8725_time_usec[] = { 32, 64, 128, 256 };
210 time_usec = &lp8720_time_usec[0];
216 time_usec = &lp8725_time_usec[0];
227 val = (val &
mask) >> shift;
231 return *(time_usec +
val);
234 static int lp872x_regulator_enable_time(
struct regulator_dev *rdev)
238 int time_step_us = lp872x_get_timestep_usec(lp);
242 if (time_step_us < 0)
248 offset = _rdev_to_offset(rdev);
267 ret = lp872x_read_byte(lp, addr, &val);
273 return val >
MAX_DELAY ? 0 : val * time_step_us;
286 static u8 lp872x_select_buck_vout_addr(
struct lp872x *lp,
325 static bool lp872x_is_valid_buck_addr(
u8 addr)
340 static int lp872x_buck_set_voltage_sel(
struct regulator_dev *rdev,
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)
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);
376 static int lp8725_buck_set_current_limit(
struct regulator_dev *rdev,
377 int min_uA,
int max_uA)
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,
406 static int lp8725_buck_get_current_limit(
struct regulator_dev *rdev)
424 ret = lp872x_read_byte(lp, addr, &val);
431 lp8725_buck_uA[val] : -
EINVAL;
467 return lp872x_update_bits(lp, addr, mask, val);
470 static unsigned int lp872x_buck_get_mode(
struct regulator_dev *rdev)
494 ret = lp872x_read_byte(lp, addr, &val);
508 .enable_time = lp872x_regulator_enable_time,
513 .set_voltage_sel = lp872x_buck_set_voltage_sel,
514 .get_voltage_sel = lp872x_buck_get_voltage_sel,
518 .enable_time = lp872x_regulator_enable_time,
519 .set_mode = lp872x_buck_set_mode,
520 .get_mode = lp872x_buck_get_mode,
525 .set_voltage_sel = lp872x_buck_set_voltage_sel,
526 .get_voltage_sel = lp872x_buck_get_voltage_sel,
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,
541 .ops = &lp872x_ldo_ops,
543 .volt_table = lp872x_ldo_vtbl,
554 .ops = &lp872x_ldo_ops,
556 .volt_table = lp872x_ldo_vtbl,
567 .ops = &lp872x_ldo_ops,
569 .volt_table = lp872x_ldo_vtbl,
580 .ops = &lp872x_ldo_ops,
582 .volt_table = lp8720_ldo4_vtbl,
593 .ops = &lp872x_ldo_ops,
595 .volt_table = lp872x_ldo_vtbl,
606 .ops = &lp8720_buck_ops,
608 .volt_table = lp8720_buck_vtbl,
620 .ops = &lp872x_ldo_ops,
622 .volt_table = lp872x_ldo_vtbl,
633 .ops = &lp872x_ldo_ops,
635 .volt_table = lp872x_ldo_vtbl,
646 .ops = &lp872x_ldo_ops,
648 .volt_table = lp872x_ldo_vtbl,
659 .ops = &lp872x_ldo_ops,
661 .volt_table = lp872x_ldo_vtbl,
672 .ops = &lp872x_ldo_ops,
674 .volt_table = lp872x_ldo_vtbl,
685 .ops = &lp872x_ldo_ops,
687 .volt_table = lp8725_lilo_vtbl,
698 .ops = &lp872x_ldo_ops,
700 .volt_table = lp8725_lilo_vtbl,
711 .ops = &lp8725_buck_ops,
713 .volt_table = lp8725_buck_vtbl,
722 .ops = &lp8725_buck_ops,
724 .volt_table = lp8725_buck_vtbl,
732 static int lp872x_init_dvs(
struct lp872x *lp)
741 goto set_default_dvs_mode;
744 if (!gpio_is_valid(gpio)) {
752 dev_err(lp->
dev,
"gpio request err: %d\n", ret);
761 set_default_dvs_mode:
763 default_dvs_mode[lp->
chipid]);
766 static int lp872x_config(
struct lp872x *lp)
779 return lp872x_init_dvs(lp);
783 *lp872x_find_regulator_init_data(
int id,
struct lp872x *lp)
799 static int lp872x_regulator_register(
struct lp872x *lp)
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);
834 static void lp872x_regulator_unregister(
struct lp872x *lp)
845 static const struct regmap_config lp872x_regmap_config = {
855 const int lp872x_num_regulators[] = {
864 num_regulators = lp872x_num_regulators[
id->driver_data];
873 ret = PTR_ERR(lp->
regmap);
874 dev_err(&cl->
dev,
"regmap init i2c err: %d\n", ret);
880 lp->
chipid =
id->driver_data;
882 i2c_set_clientdata(cl, lp);
884 ret = lp872x_config(lp);
888 return lp872x_regulator_register(lp);
898 struct lp872x *lp = i2c_get_clientdata(cl);
900 lp872x_regulator_unregister(lp);
916 .probe = lp872x_probe,
918 .id_table = lp872x_ids,