1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 Texas Instruments
5 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/i2c.h>
11 #include <linux/regmap.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/delay.h>
15 #include <linux/regulator/lp872x.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/platform_device.h>
19 #include <linux/regulator/of_regulator.h>
21 /* Registers : LP8720/8725 shared */
22 #define LP872X_GENERAL_CFG 0x00
23 #define LP872X_LDO1_VOUT 0x01
24 #define LP872X_LDO2_VOUT 0x02
25 #define LP872X_LDO3_VOUT 0x03
26 #define LP872X_LDO4_VOUT 0x04
27 #define LP872X_LDO5_VOUT 0x05
29 /* Registers : LP8720 */
30 #define LP8720_BUCK_VOUT1 0x06
31 #define LP8720_BUCK_VOUT2 0x07
32 #define LP8720_ENABLE 0x08
34 /* Registers : LP8725 */
35 #define LP8725_LILO1_VOUT 0x06
36 #define LP8725_LILO2_VOUT 0x07
37 #define LP8725_BUCK1_VOUT1 0x08
38 #define LP8725_BUCK1_VOUT2 0x09
39 #define LP8725_BUCK2_VOUT1 0x0A
40 #define LP8725_BUCK2_VOUT2 0x0B
41 #define LP8725_BUCK_CTRL 0x0C
42 #define LP8725_LDO_CTRL 0x0D
44 /* Mask/shift : LP8720/LP8725 shared */
45 #define LP872X_VOUT_M 0x1F
46 #define LP872X_START_DELAY_M 0xE0
47 #define LP872X_START_DELAY_S 5
48 #define LP872X_EN_LDO1_M BIT(0)
49 #define LP872X_EN_LDO2_M BIT(1)
50 #define LP872X_EN_LDO3_M BIT(2)
51 #define LP872X_EN_LDO4_M BIT(3)
52 #define LP872X_EN_LDO5_M BIT(4)
54 /* Mask/shift : LP8720 */
55 #define LP8720_TIMESTEP_S 0 /* Addr 00h */
56 #define LP8720_TIMESTEP_M BIT(0)
57 #define LP8720_EXT_DVS_M BIT(2)
58 #define LP8720_BUCK_FPWM_S 5 /* Addr 07h */
59 #define LP8720_BUCK_FPWM_M BIT(5)
60 #define LP8720_EN_BUCK_M BIT(5) /* Addr 08h */
61 #define LP8720_DVS_SEL_M BIT(7)
63 /* Mask/shift : LP8725 */
64 #define LP8725_TIMESTEP_M 0xC0 /* Addr 00h */
65 #define LP8725_TIMESTEP_S 6
66 #define LP8725_BUCK1_EN_M BIT(0)
67 #define LP8725_DVS1_M BIT(2)
68 #define LP8725_DVS2_M BIT(3)
69 #define LP8725_BUCK2_EN_M BIT(4)
70 #define LP8725_BUCK_CL_M 0xC0 /* Addr 09h, 0Bh */
71 #define LP8725_BUCK_CL_S 6
72 #define LP8725_BUCK1_FPWM_S 1 /* Addr 0Ch */
73 #define LP8725_BUCK1_FPWM_M BIT(1)
74 #define LP8725_BUCK2_FPWM_S 5
75 #define LP8725_BUCK2_FPWM_M BIT(5)
76 #define LP8725_EN_LILO1_M BIT(5) /* Addr 0Dh */
77 #define LP8725_EN_LILO2_M BIT(6)
80 #define LP872X_FORCE_PWM 1
81 #define LP872X_AUTO_PWM 0
83 #define LP8720_NUM_REGULATORS 6
84 #define LP8725_NUM_REGULATORS 9
85 #define EXTERN_DVS_USED 0
88 /* Default DVS Mode */
89 #define LP8720_DEFAULT_DVS 0
90 #define LP8725_DEFAULT_DVS BIT(2)
92 /* dump registers in regmap-debugfs */
93 #define MAX_REGISTERS 0x0F
101 struct regmap *regmap;
103 enum lp872x_id chipid;
104 struct lp872x_platform_data *pdata;
106 enum gpiod_flags dvs_pin;
109 /* LP8720/LP8725 shared voltage table for LDOs */
110 static const unsigned int lp872x_ldo_vtbl[] = {
111 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
112 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
113 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
114 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
117 /* LP8720 LDO4 voltage table */
118 static const unsigned int lp8720_ldo4_vtbl[] = {
119 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000,
120 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
121 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
122 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
125 /* LP8725 LILO(Low Input Low Output) voltage table */
126 static const unsigned int lp8725_lilo_vtbl[] = {
127 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
128 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
129 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
130 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
133 /* LP8720 BUCK voltage table */
134 #define EXT_R 0 /* external resistor divider */
135 static const unsigned int lp8720_buck_vtbl[] = {
136 EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
137 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
138 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
139 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
142 /* LP8725 BUCK voltage table */
143 static const unsigned int lp8725_buck_vtbl[] = {
144 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
145 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
146 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
147 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
150 /* LP8725 BUCK current limit */
151 static const unsigned int lp8725_buck_uA[] = {
152 460000, 780000, 1050000, 1370000,
155 static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
160 ret = regmap_read(lp->regmap, addr, &val);
162 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
170 static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
172 return regmap_write(lp->regmap, addr, data);
175 static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
176 unsigned int mask, u8 data)
178 return regmap_update_bits(lp->regmap, addr, mask, data);
181 static int lp872x_get_timestep_usec(struct lp872x *lp)
183 enum lp872x_id chip = lp->chipid;
185 int *time_usec, size, ret;
186 int lp8720_time_usec[] = { 25, 50 };
187 int lp8725_time_usec[] = { 32, 64, 128, 256 };
191 mask = LP8720_TIMESTEP_M;
192 shift = LP8720_TIMESTEP_S;
193 time_usec = &lp8720_time_usec[0];
194 size = ARRAY_SIZE(lp8720_time_usec);
197 mask = LP8725_TIMESTEP_M;
198 shift = LP8725_TIMESTEP_S;
199 time_usec = &lp8725_time_usec[0];
200 size = ARRAY_SIZE(lp8725_time_usec);
206 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
210 val = (val & mask) >> shift;
214 return *(time_usec + val);
217 static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
219 struct lp872x *lp = rdev_get_drvdata(rdev);
220 enum lp872x_regulator_id rid = rdev_get_id(rdev);
221 int time_step_us = lp872x_get_timestep_usec(lp);
225 if (time_step_us < 0)
229 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
230 addr = LP872X_LDO1_VOUT + rid;
232 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
233 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
235 case LP8725_ID_BUCK2:
236 addr = LP8725_BUCK2_VOUT1;
242 ret = lp872x_read_byte(lp, addr, &val);
246 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
248 return val > MAX_DELAY ? 0 : val * time_step_us;
251 static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
252 struct gpio_desc *gpio)
254 enum gpiod_flags state;
256 state = dvs_sel == SEL_V1 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
257 gpiod_set_value(gpio, state);
261 static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
262 enum lp872x_regulator_id buck)
266 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
271 if (val & LP8720_EXT_DVS_M) {
272 addr = (lp->dvs_pin == GPIOD_OUT_HIGH) ?
273 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
275 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
278 addr = val & LP8720_DVS_SEL_M ?
279 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
282 case LP8725_ID_BUCK1:
283 if (val & LP8725_DVS1_M)
284 addr = LP8725_BUCK1_VOUT1;
286 addr = (lp->dvs_pin == GPIOD_OUT_HIGH) ?
287 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
289 case LP8725_ID_BUCK2:
290 addr = val & LP8725_DVS2_M ?
291 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
300 static bool lp872x_is_valid_buck_addr(u8 addr)
303 case LP8720_BUCK_VOUT1:
304 case LP8720_BUCK_VOUT2:
305 case LP8725_BUCK1_VOUT1:
306 case LP8725_BUCK1_VOUT2:
307 case LP8725_BUCK2_VOUT1:
308 case LP8725_BUCK2_VOUT2:
315 static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
318 struct lp872x *lp = rdev_get_drvdata(rdev);
319 enum lp872x_regulator_id buck = rdev_get_id(rdev);
320 u8 addr, mask = LP872X_VOUT_M;
321 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
323 if (dvs && dvs->gpio)
324 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
326 addr = lp872x_select_buck_vout_addr(lp, buck);
327 if (!lp872x_is_valid_buck_addr(addr))
330 return lp872x_update_bits(lp, addr, mask, selector);
333 static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
335 struct lp872x *lp = rdev_get_drvdata(rdev);
336 enum lp872x_regulator_id buck = rdev_get_id(rdev);
340 addr = lp872x_select_buck_vout_addr(lp, buck);
341 if (!lp872x_is_valid_buck_addr(addr))
344 ret = lp872x_read_byte(lp, addr, &val);
348 return val & LP872X_VOUT_M;
351 static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
353 struct lp872x *lp = rdev_get_drvdata(rdev);
354 enum lp872x_regulator_id buck = rdev_get_id(rdev);
355 u8 addr, mask, shift, val;
359 addr = LP8720_BUCK_VOUT2;
360 mask = LP8720_BUCK_FPWM_M;
361 shift = LP8720_BUCK_FPWM_S;
363 case LP8725_ID_BUCK1:
364 addr = LP8725_BUCK_CTRL;
365 mask = LP8725_BUCK1_FPWM_M;
366 shift = LP8725_BUCK1_FPWM_S;
368 case LP8725_ID_BUCK2:
369 addr = LP8725_BUCK_CTRL;
370 mask = LP8725_BUCK2_FPWM_M;
371 shift = LP8725_BUCK2_FPWM_S;
377 if (mode == REGULATOR_MODE_FAST)
378 val = LP872X_FORCE_PWM << shift;
379 else if (mode == REGULATOR_MODE_NORMAL)
380 val = LP872X_AUTO_PWM << shift;
384 return lp872x_update_bits(lp, addr, mask, val);
387 static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
389 struct lp872x *lp = rdev_get_drvdata(rdev);
390 enum lp872x_regulator_id buck = rdev_get_id(rdev);
396 addr = LP8720_BUCK_VOUT2;
397 mask = LP8720_BUCK_FPWM_M;
399 case LP8725_ID_BUCK1:
400 addr = LP8725_BUCK_CTRL;
401 mask = LP8725_BUCK1_FPWM_M;
403 case LP8725_ID_BUCK2:
404 addr = LP8725_BUCK_CTRL;
405 mask = LP8725_BUCK2_FPWM_M;
411 ret = lp872x_read_byte(lp, addr, &val);
415 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
418 static const struct regulator_ops lp872x_ldo_ops = {
419 .list_voltage = regulator_list_voltage_table,
420 .map_voltage = regulator_map_voltage_ascend,
421 .set_voltage_sel = regulator_set_voltage_sel_regmap,
422 .get_voltage_sel = regulator_get_voltage_sel_regmap,
423 .enable = regulator_enable_regmap,
424 .disable = regulator_disable_regmap,
425 .is_enabled = regulator_is_enabled_regmap,
426 .enable_time = lp872x_regulator_enable_time,
429 static const struct regulator_ops lp8720_buck_ops = {
430 .list_voltage = regulator_list_voltage_table,
431 .map_voltage = regulator_map_voltage_ascend,
432 .set_voltage_sel = lp872x_buck_set_voltage_sel,
433 .get_voltage_sel = lp872x_buck_get_voltage_sel,
434 .enable = regulator_enable_regmap,
435 .disable = regulator_disable_regmap,
436 .is_enabled = regulator_is_enabled_regmap,
437 .enable_time = lp872x_regulator_enable_time,
438 .set_mode = lp872x_buck_set_mode,
439 .get_mode = lp872x_buck_get_mode,
442 static const struct regulator_ops lp8725_buck_ops = {
443 .list_voltage = regulator_list_voltage_table,
444 .map_voltage = regulator_map_voltage_ascend,
445 .set_voltage_sel = lp872x_buck_set_voltage_sel,
446 .get_voltage_sel = lp872x_buck_get_voltage_sel,
447 .enable = regulator_enable_regmap,
448 .disable = regulator_disable_regmap,
449 .is_enabled = regulator_is_enabled_regmap,
450 .enable_time = lp872x_regulator_enable_time,
451 .set_mode = lp872x_buck_set_mode,
452 .get_mode = lp872x_buck_get_mode,
453 .set_current_limit = regulator_set_current_limit_regmap,
454 .get_current_limit = regulator_get_current_limit_regmap,
457 static const struct regulator_desc lp8720_regulator_desc[] = {
460 .of_match = of_match_ptr("ldo1"),
461 .id = LP8720_ID_LDO1,
462 .ops = &lp872x_ldo_ops,
463 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
464 .volt_table = lp872x_ldo_vtbl,
465 .type = REGULATOR_VOLTAGE,
466 .owner = THIS_MODULE,
467 .vsel_reg = LP872X_LDO1_VOUT,
468 .vsel_mask = LP872X_VOUT_M,
469 .enable_reg = LP8720_ENABLE,
470 .enable_mask = LP872X_EN_LDO1_M,
474 .of_match = of_match_ptr("ldo2"),
475 .id = LP8720_ID_LDO2,
476 .ops = &lp872x_ldo_ops,
477 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
478 .volt_table = lp872x_ldo_vtbl,
479 .type = REGULATOR_VOLTAGE,
480 .owner = THIS_MODULE,
481 .vsel_reg = LP872X_LDO2_VOUT,
482 .vsel_mask = LP872X_VOUT_M,
483 .enable_reg = LP8720_ENABLE,
484 .enable_mask = LP872X_EN_LDO2_M,
488 .of_match = of_match_ptr("ldo3"),
489 .id = LP8720_ID_LDO3,
490 .ops = &lp872x_ldo_ops,
491 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
492 .volt_table = lp872x_ldo_vtbl,
493 .type = REGULATOR_VOLTAGE,
494 .owner = THIS_MODULE,
495 .vsel_reg = LP872X_LDO3_VOUT,
496 .vsel_mask = LP872X_VOUT_M,
497 .enable_reg = LP8720_ENABLE,
498 .enable_mask = LP872X_EN_LDO3_M,
502 .of_match = of_match_ptr("ldo4"),
503 .id = LP8720_ID_LDO4,
504 .ops = &lp872x_ldo_ops,
505 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
506 .volt_table = lp8720_ldo4_vtbl,
507 .type = REGULATOR_VOLTAGE,
508 .owner = THIS_MODULE,
509 .vsel_reg = LP872X_LDO4_VOUT,
510 .vsel_mask = LP872X_VOUT_M,
511 .enable_reg = LP8720_ENABLE,
512 .enable_mask = LP872X_EN_LDO4_M,
516 .of_match = of_match_ptr("ldo5"),
517 .id = LP8720_ID_LDO5,
518 .ops = &lp872x_ldo_ops,
519 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
520 .volt_table = lp872x_ldo_vtbl,
521 .type = REGULATOR_VOLTAGE,
522 .owner = THIS_MODULE,
523 .vsel_reg = LP872X_LDO5_VOUT,
524 .vsel_mask = LP872X_VOUT_M,
525 .enable_reg = LP8720_ENABLE,
526 .enable_mask = LP872X_EN_LDO5_M,
530 .of_match = of_match_ptr("buck"),
531 .id = LP8720_ID_BUCK,
532 .ops = &lp8720_buck_ops,
533 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
534 .volt_table = lp8720_buck_vtbl,
535 .type = REGULATOR_VOLTAGE,
536 .owner = THIS_MODULE,
537 .enable_reg = LP8720_ENABLE,
538 .enable_mask = LP8720_EN_BUCK_M,
542 static const struct regulator_desc lp8725_regulator_desc[] = {
545 .of_match = of_match_ptr("ldo1"),
546 .id = LP8725_ID_LDO1,
547 .ops = &lp872x_ldo_ops,
548 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
549 .volt_table = lp872x_ldo_vtbl,
550 .type = REGULATOR_VOLTAGE,
551 .owner = THIS_MODULE,
552 .vsel_reg = LP872X_LDO1_VOUT,
553 .vsel_mask = LP872X_VOUT_M,
554 .enable_reg = LP8725_LDO_CTRL,
555 .enable_mask = LP872X_EN_LDO1_M,
559 .of_match = of_match_ptr("ldo2"),
560 .id = LP8725_ID_LDO2,
561 .ops = &lp872x_ldo_ops,
562 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
563 .volt_table = lp872x_ldo_vtbl,
564 .type = REGULATOR_VOLTAGE,
565 .owner = THIS_MODULE,
566 .vsel_reg = LP872X_LDO2_VOUT,
567 .vsel_mask = LP872X_VOUT_M,
568 .enable_reg = LP8725_LDO_CTRL,
569 .enable_mask = LP872X_EN_LDO2_M,
573 .of_match = of_match_ptr("ldo3"),
574 .id = LP8725_ID_LDO3,
575 .ops = &lp872x_ldo_ops,
576 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
577 .volt_table = lp872x_ldo_vtbl,
578 .type = REGULATOR_VOLTAGE,
579 .owner = THIS_MODULE,
580 .vsel_reg = LP872X_LDO3_VOUT,
581 .vsel_mask = LP872X_VOUT_M,
582 .enable_reg = LP8725_LDO_CTRL,
583 .enable_mask = LP872X_EN_LDO3_M,
587 .of_match = of_match_ptr("ldo4"),
588 .id = LP8725_ID_LDO4,
589 .ops = &lp872x_ldo_ops,
590 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
591 .volt_table = lp872x_ldo_vtbl,
592 .type = REGULATOR_VOLTAGE,
593 .owner = THIS_MODULE,
594 .vsel_reg = LP872X_LDO4_VOUT,
595 .vsel_mask = LP872X_VOUT_M,
596 .enable_reg = LP8725_LDO_CTRL,
597 .enable_mask = LP872X_EN_LDO4_M,
601 .of_match = of_match_ptr("ldo5"),
602 .id = LP8725_ID_LDO5,
603 .ops = &lp872x_ldo_ops,
604 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
605 .volt_table = lp872x_ldo_vtbl,
606 .type = REGULATOR_VOLTAGE,
607 .owner = THIS_MODULE,
608 .vsel_reg = LP872X_LDO5_VOUT,
609 .vsel_mask = LP872X_VOUT_M,
610 .enable_reg = LP8725_LDO_CTRL,
611 .enable_mask = LP872X_EN_LDO5_M,
615 .of_match = of_match_ptr("lilo1"),
616 .id = LP8725_ID_LILO1,
617 .ops = &lp872x_ldo_ops,
618 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
619 .volt_table = lp8725_lilo_vtbl,
620 .type = REGULATOR_VOLTAGE,
621 .owner = THIS_MODULE,
622 .vsel_reg = LP8725_LILO1_VOUT,
623 .vsel_mask = LP872X_VOUT_M,
624 .enable_reg = LP8725_LDO_CTRL,
625 .enable_mask = LP8725_EN_LILO1_M,
629 .of_match = of_match_ptr("lilo2"),
630 .id = LP8725_ID_LILO2,
631 .ops = &lp872x_ldo_ops,
632 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
633 .volt_table = lp8725_lilo_vtbl,
634 .type = REGULATOR_VOLTAGE,
635 .owner = THIS_MODULE,
636 .vsel_reg = LP8725_LILO2_VOUT,
637 .vsel_mask = LP872X_VOUT_M,
638 .enable_reg = LP8725_LDO_CTRL,
639 .enable_mask = LP8725_EN_LILO2_M,
643 .of_match = of_match_ptr("buck1"),
644 .id = LP8725_ID_BUCK1,
645 .ops = &lp8725_buck_ops,
646 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
647 .volt_table = lp8725_buck_vtbl,
648 .type = REGULATOR_VOLTAGE,
649 .owner = THIS_MODULE,
650 .enable_reg = LP872X_GENERAL_CFG,
651 .enable_mask = LP8725_BUCK1_EN_M,
652 .curr_table = lp8725_buck_uA,
653 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
654 .csel_reg = LP8725_BUCK1_VOUT2,
655 .csel_mask = LP8725_BUCK_CL_M,
659 .of_match = of_match_ptr("buck2"),
660 .id = LP8725_ID_BUCK2,
661 .ops = &lp8725_buck_ops,
662 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
663 .volt_table = lp8725_buck_vtbl,
664 .type = REGULATOR_VOLTAGE,
665 .owner = THIS_MODULE,
666 .enable_reg = LP872X_GENERAL_CFG,
667 .enable_mask = LP8725_BUCK2_EN_M,
668 .curr_table = lp8725_buck_uA,
669 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
670 .csel_reg = LP8725_BUCK2_VOUT2,
671 .csel_mask = LP8725_BUCK_CL_M,
675 static int lp872x_init_dvs(struct lp872x *lp)
677 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
678 enum gpiod_flags pinstate;
679 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
680 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
683 goto set_default_dvs_mode;
686 goto set_default_dvs_mode;
688 pinstate = dvs->init_state;
689 dvs->gpio = devm_gpiod_get_optional(lp->dev, "ti,dvs", pinstate);
691 if (IS_ERR(dvs->gpio)) {
692 dev_err(lp->dev, "gpio request err: %ld\n", PTR_ERR(dvs->gpio));
693 return PTR_ERR(dvs->gpio);
696 lp->dvs_pin = pinstate;
700 set_default_dvs_mode:
701 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
702 default_dvs_mode[lp->chipid]);
705 static int lp872x_hw_enable(struct lp872x *lp)
710 if (!lp->pdata->enable_gpio)
713 /* Always set enable GPIO high. */
714 lp->pdata->enable_gpio = devm_gpiod_get_optional(lp->dev, "enable", GPIOD_OUT_HIGH);
715 if (IS_ERR(lp->pdata->enable_gpio)) {
716 dev_err(lp->dev, "gpio request err: %ld\n", PTR_ERR(lp->pdata->enable_gpio));
717 return PTR_ERR(lp->pdata->enable_gpio);
720 /* Each chip has a different enable delay. */
721 if (lp->chipid == LP8720)
722 usleep_range(LP8720_ENABLE_DELAY, 1.5 * LP8720_ENABLE_DELAY);
724 usleep_range(LP8725_ENABLE_DELAY, 1.5 * LP8725_ENABLE_DELAY);
729 static int lp872x_config(struct lp872x *lp)
731 struct lp872x_platform_data *pdata = lp->pdata;
734 if (!pdata || !pdata->update_config)
737 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
742 return lp872x_init_dvs(lp);
745 static struct regulator_init_data
746 *lp872x_find_regulator_init_data(int id, struct lp872x *lp)
748 struct lp872x_platform_data *pdata = lp->pdata;
754 for (i = 0; i < lp->num_regulators; i++) {
755 if (pdata->regulator_data[i].id == id)
756 return pdata->regulator_data[i].init_data;
762 static int lp872x_regulator_register(struct lp872x *lp)
764 const struct regulator_desc *desc;
765 struct regulator_config cfg = { };
766 struct regulator_dev *rdev;
769 for (i = 0; i < lp->num_regulators; i++) {
770 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
771 &lp8725_regulator_desc[i];
774 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
775 cfg.driver_data = lp;
776 cfg.regmap = lp->regmap;
778 rdev = devm_regulator_register(lp->dev, desc, &cfg);
780 dev_err(lp->dev, "regulator register err");
781 return PTR_ERR(rdev);
788 static const struct regmap_config lp872x_regmap_config = {
791 .max_register = MAX_REGISTERS,
796 #define LP872X_VALID_OPMODE (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL)
798 static struct of_regulator_match lp8720_matches[] = {
799 { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, },
800 { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, },
801 { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, },
802 { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, },
803 { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, },
804 { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, },
807 static struct of_regulator_match lp8725_matches[] = {
808 { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, },
809 { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, },
810 { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, },
811 { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, },
812 { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, },
813 { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, },
814 { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, },
815 { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, },
816 { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, },
819 static struct lp872x_platform_data
820 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
822 struct device_node *np = dev->of_node;
823 struct lp872x_platform_data *pdata;
824 struct of_regulator_match *match;
830 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
832 return ERR_PTR(-ENOMEM);
834 of_property_read_u8(np, "ti,general-config", &pdata->general_config);
835 pdata->update_config = of_property_read_bool(np, "ti,update-config");
837 pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL);
839 return ERR_PTR(-ENOMEM);
841 of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel);
842 of_property_read_u8(np, "ti,dvs-state", &dvs_state);
843 pdata->dvs->init_state = dvs_state ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
845 if (of_get_child_count(np) == 0)
850 match = lp8720_matches;
851 num_matches = ARRAY_SIZE(lp8720_matches);
854 match = lp8725_matches;
855 num_matches = ARRAY_SIZE(lp8725_matches);
861 count = of_regulator_match(dev, np, match, num_matches);
865 for (i = 0; i < num_matches; i++) {
866 pdata->regulator_data[i].id =
867 (uintptr_t)match[i].driver_data;
868 pdata->regulator_data[i].init_data = match[i].init_data;
874 static struct lp872x_platform_data
875 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
881 static int lp872x_probe(struct i2c_client *cl)
883 const struct i2c_device_id *id = i2c_client_get_device_id(cl);
885 struct lp872x_platform_data *pdata;
887 static const int lp872x_num_regulators[] = {
888 [LP8720] = LP8720_NUM_REGULATORS,
889 [LP8725] = LP8725_NUM_REGULATORS,
892 if (cl->dev.of_node) {
893 pdata = lp872x_populate_pdata_from_dt(&cl->dev,
894 (enum lp872x_id)id->driver_data);
896 return PTR_ERR(pdata);
898 pdata = dev_get_platdata(&cl->dev);
901 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
905 lp->num_regulators = lp872x_num_regulators[id->driver_data];
907 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
908 if (IS_ERR(lp->regmap)) {
909 ret = PTR_ERR(lp->regmap);
910 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
916 lp->chipid = id->driver_data;
917 i2c_set_clientdata(cl, lp);
919 ret = lp872x_hw_enable(lp);
923 ret = lp872x_config(lp);
927 return lp872x_regulator_register(lp);
930 static const struct of_device_id lp872x_dt_ids[] __maybe_unused = {
931 { .compatible = "ti,lp8720", },
932 { .compatible = "ti,lp8725", },
935 MODULE_DEVICE_TABLE(of, lp872x_dt_ids);
937 static const struct i2c_device_id lp872x_ids[] = {
942 MODULE_DEVICE_TABLE(i2c, lp872x_ids);
944 static struct i2c_driver lp872x_driver = {
947 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
948 .of_match_table = of_match_ptr(lp872x_dt_ids),
950 .probe = lp872x_probe,
951 .id_table = lp872x_ids,
954 module_i2c_driver(lp872x_driver);
956 MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
957 MODULE_AUTHOR("Milo Kim");
958 MODULE_LICENSE("GPL");