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.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/of_gpio.h>
20 #include <linux/regulator/of_regulator.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;
107 enum lp872x_dvs_state dvs_pin;
110 /* LP8720/LP8725 shared voltage table for LDOs */
111 static const unsigned int lp872x_ldo_vtbl[] = {
112 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
113 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
114 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
115 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
118 /* LP8720 LDO4 voltage table */
119 static const unsigned int lp8720_ldo4_vtbl[] = {
120 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000,
121 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
122 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
123 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
126 /* LP8725 LILO(Low Input Low Output) voltage table */
127 static const unsigned int lp8725_lilo_vtbl[] = {
128 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
129 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
130 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
131 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
134 /* LP8720 BUCK voltage table */
135 #define EXT_R 0 /* external resistor divider */
136 static const unsigned int lp8720_buck_vtbl[] = {
137 EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
138 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
139 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
140 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
143 /* LP8725 BUCK voltage table */
144 static const unsigned int lp8725_buck_vtbl[] = {
145 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
146 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
147 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
148 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
151 /* LP8725 BUCK current limit */
152 static const unsigned int lp8725_buck_uA[] = {
153 460000, 780000, 1050000, 1370000,
156 static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
161 ret = regmap_read(lp->regmap, addr, &val);
163 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
171 static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
173 return regmap_write(lp->regmap, addr, data);
176 static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
177 unsigned int mask, u8 data)
179 return regmap_update_bits(lp->regmap, addr, mask, data);
182 static int lp872x_get_timestep_usec(struct lp872x *lp)
184 enum lp872x_id chip = lp->chipid;
186 int *time_usec, size, ret;
187 int lp8720_time_usec[] = { 25, 50 };
188 int lp8725_time_usec[] = { 32, 64, 128, 256 };
192 mask = LP8720_TIMESTEP_M;
193 shift = LP8720_TIMESTEP_S;
194 time_usec = &lp8720_time_usec[0];
195 size = ARRAY_SIZE(lp8720_time_usec);
198 mask = LP8725_TIMESTEP_M;
199 shift = LP8725_TIMESTEP_S;
200 time_usec = &lp8725_time_usec[0];
201 size = ARRAY_SIZE(lp8725_time_usec);
207 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
211 val = (val & mask) >> shift;
215 return *(time_usec + val);
218 static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
220 struct lp872x *lp = rdev_get_drvdata(rdev);
221 enum lp872x_regulator_id rid = rdev_get_id(rdev);
222 int time_step_us = lp872x_get_timestep_usec(lp);
226 if (time_step_us < 0)
230 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
231 addr = LP872X_LDO1_VOUT + rid;
233 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
234 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
236 case LP8725_ID_BUCK2:
237 addr = LP8725_BUCK2_VOUT1;
243 ret = lp872x_read_byte(lp, addr, &val);
247 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
249 return val > MAX_DELAY ? 0 : val * time_step_us;
252 static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
255 enum lp872x_dvs_state state;
257 state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
258 gpio_set_value(gpio, state);
262 static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
263 enum lp872x_regulator_id buck)
267 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
272 if (val & LP8720_EXT_DVS_M) {
273 addr = (lp->dvs_pin == DVS_HIGH) ?
274 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
276 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
279 addr = val & LP8720_DVS_SEL_M ?
280 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
283 case LP8725_ID_BUCK1:
284 if (val & LP8725_DVS1_M)
285 addr = LP8725_BUCK1_VOUT1;
287 addr = (lp->dvs_pin == DVS_HIGH) ?
288 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
290 case LP8725_ID_BUCK2:
291 addr = val & LP8725_DVS2_M ?
292 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
301 static bool lp872x_is_valid_buck_addr(u8 addr)
304 case LP8720_BUCK_VOUT1:
305 case LP8720_BUCK_VOUT2:
306 case LP8725_BUCK1_VOUT1:
307 case LP8725_BUCK1_VOUT2:
308 case LP8725_BUCK2_VOUT1:
309 case LP8725_BUCK2_VOUT2:
316 static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
319 struct lp872x *lp = rdev_get_drvdata(rdev);
320 enum lp872x_regulator_id buck = rdev_get_id(rdev);
321 u8 addr, mask = LP872X_VOUT_M;
322 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
324 if (dvs && gpio_is_valid(dvs->gpio))
325 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
327 addr = lp872x_select_buck_vout_addr(lp, buck);
328 if (!lp872x_is_valid_buck_addr(addr))
331 return lp872x_update_bits(lp, addr, mask, selector);
334 static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
336 struct lp872x *lp = rdev_get_drvdata(rdev);
337 enum lp872x_regulator_id buck = rdev_get_id(rdev);
341 addr = lp872x_select_buck_vout_addr(lp, buck);
342 if (!lp872x_is_valid_buck_addr(addr))
345 ret = lp872x_read_byte(lp, addr, &val);
349 return val & LP872X_VOUT_M;
352 static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
354 struct lp872x *lp = rdev_get_drvdata(rdev);
355 enum lp872x_regulator_id buck = rdev_get_id(rdev);
356 u8 addr, mask, shift, val;
360 addr = LP8720_BUCK_VOUT2;
361 mask = LP8720_BUCK_FPWM_M;
362 shift = LP8720_BUCK_FPWM_S;
364 case LP8725_ID_BUCK1:
365 addr = LP8725_BUCK_CTRL;
366 mask = LP8725_BUCK1_FPWM_M;
367 shift = LP8725_BUCK1_FPWM_S;
369 case LP8725_ID_BUCK2:
370 addr = LP8725_BUCK_CTRL;
371 mask = LP8725_BUCK2_FPWM_M;
372 shift = LP8725_BUCK2_FPWM_S;
378 if (mode == REGULATOR_MODE_FAST)
379 val = LP872X_FORCE_PWM << shift;
380 else if (mode == REGULATOR_MODE_NORMAL)
381 val = LP872X_AUTO_PWM << shift;
385 return lp872x_update_bits(lp, addr, mask, val);
388 static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
390 struct lp872x *lp = rdev_get_drvdata(rdev);
391 enum lp872x_regulator_id buck = rdev_get_id(rdev);
397 addr = LP8720_BUCK_VOUT2;
398 mask = LP8720_BUCK_FPWM_M;
400 case LP8725_ID_BUCK1:
401 addr = LP8725_BUCK_CTRL;
402 mask = LP8725_BUCK1_FPWM_M;
404 case LP8725_ID_BUCK2:
405 addr = LP8725_BUCK_CTRL;
406 mask = LP8725_BUCK2_FPWM_M;
412 ret = lp872x_read_byte(lp, addr, &val);
416 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
419 static const struct regulator_ops lp872x_ldo_ops = {
420 .list_voltage = regulator_list_voltage_table,
421 .map_voltage = regulator_map_voltage_ascend,
422 .set_voltage_sel = regulator_set_voltage_sel_regmap,
423 .get_voltage_sel = regulator_get_voltage_sel_regmap,
424 .enable = regulator_enable_regmap,
425 .disable = regulator_disable_regmap,
426 .is_enabled = regulator_is_enabled_regmap,
427 .enable_time = lp872x_regulator_enable_time,
430 static const struct regulator_ops lp8720_buck_ops = {
431 .list_voltage = regulator_list_voltage_table,
432 .map_voltage = regulator_map_voltage_ascend,
433 .set_voltage_sel = lp872x_buck_set_voltage_sel,
434 .get_voltage_sel = lp872x_buck_get_voltage_sel,
435 .enable = regulator_enable_regmap,
436 .disable = regulator_disable_regmap,
437 .is_enabled = regulator_is_enabled_regmap,
438 .enable_time = lp872x_regulator_enable_time,
439 .set_mode = lp872x_buck_set_mode,
440 .get_mode = lp872x_buck_get_mode,
443 static const struct regulator_ops lp8725_buck_ops = {
444 .list_voltage = regulator_list_voltage_table,
445 .map_voltage = regulator_map_voltage_ascend,
446 .set_voltage_sel = lp872x_buck_set_voltage_sel,
447 .get_voltage_sel = lp872x_buck_get_voltage_sel,
448 .enable = regulator_enable_regmap,
449 .disable = regulator_disable_regmap,
450 .is_enabled = regulator_is_enabled_regmap,
451 .enable_time = lp872x_regulator_enable_time,
452 .set_mode = lp872x_buck_set_mode,
453 .get_mode = lp872x_buck_get_mode,
454 .set_current_limit = regulator_set_current_limit_regmap,
455 .get_current_limit = regulator_get_current_limit_regmap,
458 static const struct regulator_desc lp8720_regulator_desc[] = {
461 .of_match = of_match_ptr("ldo1"),
462 .id = LP8720_ID_LDO1,
463 .ops = &lp872x_ldo_ops,
464 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
465 .volt_table = lp872x_ldo_vtbl,
466 .type = REGULATOR_VOLTAGE,
467 .owner = THIS_MODULE,
468 .vsel_reg = LP872X_LDO1_VOUT,
469 .vsel_mask = LP872X_VOUT_M,
470 .enable_reg = LP8720_ENABLE,
471 .enable_mask = LP872X_EN_LDO1_M,
475 .of_match = of_match_ptr("ldo2"),
476 .id = LP8720_ID_LDO2,
477 .ops = &lp872x_ldo_ops,
478 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
479 .volt_table = lp872x_ldo_vtbl,
480 .type = REGULATOR_VOLTAGE,
481 .owner = THIS_MODULE,
482 .vsel_reg = LP872X_LDO2_VOUT,
483 .vsel_mask = LP872X_VOUT_M,
484 .enable_reg = LP8720_ENABLE,
485 .enable_mask = LP872X_EN_LDO2_M,
489 .of_match = of_match_ptr("ldo3"),
490 .id = LP8720_ID_LDO3,
491 .ops = &lp872x_ldo_ops,
492 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
493 .volt_table = lp872x_ldo_vtbl,
494 .type = REGULATOR_VOLTAGE,
495 .owner = THIS_MODULE,
496 .vsel_reg = LP872X_LDO3_VOUT,
497 .vsel_mask = LP872X_VOUT_M,
498 .enable_reg = LP8720_ENABLE,
499 .enable_mask = LP872X_EN_LDO3_M,
503 .of_match = of_match_ptr("ldo4"),
504 .id = LP8720_ID_LDO4,
505 .ops = &lp872x_ldo_ops,
506 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
507 .volt_table = lp8720_ldo4_vtbl,
508 .type = REGULATOR_VOLTAGE,
509 .owner = THIS_MODULE,
510 .vsel_reg = LP872X_LDO4_VOUT,
511 .vsel_mask = LP872X_VOUT_M,
512 .enable_reg = LP8720_ENABLE,
513 .enable_mask = LP872X_EN_LDO4_M,
517 .of_match = of_match_ptr("ldo5"),
518 .id = LP8720_ID_LDO5,
519 .ops = &lp872x_ldo_ops,
520 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
521 .volt_table = lp872x_ldo_vtbl,
522 .type = REGULATOR_VOLTAGE,
523 .owner = THIS_MODULE,
524 .vsel_reg = LP872X_LDO5_VOUT,
525 .vsel_mask = LP872X_VOUT_M,
526 .enable_reg = LP8720_ENABLE,
527 .enable_mask = LP872X_EN_LDO5_M,
531 .of_match = of_match_ptr("buck"),
532 .id = LP8720_ID_BUCK,
533 .ops = &lp8720_buck_ops,
534 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
535 .volt_table = lp8720_buck_vtbl,
536 .type = REGULATOR_VOLTAGE,
537 .owner = THIS_MODULE,
538 .enable_reg = LP8720_ENABLE,
539 .enable_mask = LP8720_EN_BUCK_M,
543 static const struct regulator_desc lp8725_regulator_desc[] = {
546 .of_match = of_match_ptr("ldo1"),
547 .id = LP8725_ID_LDO1,
548 .ops = &lp872x_ldo_ops,
549 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
550 .volt_table = lp872x_ldo_vtbl,
551 .type = REGULATOR_VOLTAGE,
552 .owner = THIS_MODULE,
553 .vsel_reg = LP872X_LDO1_VOUT,
554 .vsel_mask = LP872X_VOUT_M,
555 .enable_reg = LP8725_LDO_CTRL,
556 .enable_mask = LP872X_EN_LDO1_M,
560 .of_match = of_match_ptr("ldo2"),
561 .id = LP8725_ID_LDO2,
562 .ops = &lp872x_ldo_ops,
563 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
564 .volt_table = lp872x_ldo_vtbl,
565 .type = REGULATOR_VOLTAGE,
566 .owner = THIS_MODULE,
567 .vsel_reg = LP872X_LDO2_VOUT,
568 .vsel_mask = LP872X_VOUT_M,
569 .enable_reg = LP8725_LDO_CTRL,
570 .enable_mask = LP872X_EN_LDO2_M,
574 .of_match = of_match_ptr("ldo3"),
575 .id = LP8725_ID_LDO3,
576 .ops = &lp872x_ldo_ops,
577 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
578 .volt_table = lp872x_ldo_vtbl,
579 .type = REGULATOR_VOLTAGE,
580 .owner = THIS_MODULE,
581 .vsel_reg = LP872X_LDO3_VOUT,
582 .vsel_mask = LP872X_VOUT_M,
583 .enable_reg = LP8725_LDO_CTRL,
584 .enable_mask = LP872X_EN_LDO3_M,
588 .of_match = of_match_ptr("ldo4"),
589 .id = LP8725_ID_LDO4,
590 .ops = &lp872x_ldo_ops,
591 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
592 .volt_table = lp872x_ldo_vtbl,
593 .type = REGULATOR_VOLTAGE,
594 .owner = THIS_MODULE,
595 .vsel_reg = LP872X_LDO4_VOUT,
596 .vsel_mask = LP872X_VOUT_M,
597 .enable_reg = LP8725_LDO_CTRL,
598 .enable_mask = LP872X_EN_LDO4_M,
602 .of_match = of_match_ptr("ldo5"),
603 .id = LP8725_ID_LDO5,
604 .ops = &lp872x_ldo_ops,
605 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
606 .volt_table = lp872x_ldo_vtbl,
607 .type = REGULATOR_VOLTAGE,
608 .owner = THIS_MODULE,
609 .vsel_reg = LP872X_LDO5_VOUT,
610 .vsel_mask = LP872X_VOUT_M,
611 .enable_reg = LP8725_LDO_CTRL,
612 .enable_mask = LP872X_EN_LDO5_M,
616 .of_match = of_match_ptr("lilo1"),
617 .id = LP8725_ID_LILO1,
618 .ops = &lp872x_ldo_ops,
619 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
620 .volt_table = lp8725_lilo_vtbl,
621 .type = REGULATOR_VOLTAGE,
622 .owner = THIS_MODULE,
623 .vsel_reg = LP8725_LILO1_VOUT,
624 .vsel_mask = LP872X_VOUT_M,
625 .enable_reg = LP8725_LDO_CTRL,
626 .enable_mask = LP8725_EN_LILO1_M,
630 .of_match = of_match_ptr("lilo2"),
631 .id = LP8725_ID_LILO2,
632 .ops = &lp872x_ldo_ops,
633 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
634 .volt_table = lp8725_lilo_vtbl,
635 .type = REGULATOR_VOLTAGE,
636 .owner = THIS_MODULE,
637 .vsel_reg = LP8725_LILO2_VOUT,
638 .vsel_mask = LP872X_VOUT_M,
639 .enable_reg = LP8725_LDO_CTRL,
640 .enable_mask = LP8725_EN_LILO2_M,
644 .of_match = of_match_ptr("buck1"),
645 .id = LP8725_ID_BUCK1,
646 .ops = &lp8725_buck_ops,
647 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
648 .volt_table = lp8725_buck_vtbl,
649 .type = REGULATOR_VOLTAGE,
650 .owner = THIS_MODULE,
651 .enable_reg = LP872X_GENERAL_CFG,
652 .enable_mask = LP8725_BUCK1_EN_M,
653 .curr_table = lp8725_buck_uA,
654 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
655 .csel_reg = LP8725_BUCK1_VOUT2,
656 .csel_mask = LP8725_BUCK_CL_M,
660 .of_match = of_match_ptr("buck2"),
661 .id = LP8725_ID_BUCK2,
662 .ops = &lp8725_buck_ops,
663 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
664 .volt_table = lp8725_buck_vtbl,
665 .type = REGULATOR_VOLTAGE,
666 .owner = THIS_MODULE,
667 .enable_reg = LP872X_GENERAL_CFG,
668 .enable_mask = LP8725_BUCK2_EN_M,
669 .curr_table = lp8725_buck_uA,
670 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
671 .csel_reg = LP8725_BUCK2_VOUT2,
672 .csel_mask = LP8725_BUCK_CL_M,
676 static int lp872x_init_dvs(struct lp872x *lp)
679 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
680 enum lp872x_dvs_state pinstate;
681 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
682 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
685 goto set_default_dvs_mode;
688 if (!gpio_is_valid(gpio))
689 goto set_default_dvs_mode;
691 pinstate = dvs->init_state;
692 ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
694 dev_err(lp->dev, "gpio request err: %d\n", ret);
698 lp->dvs_pin = pinstate;
702 set_default_dvs_mode:
703 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
704 default_dvs_mode[lp->chipid]);
707 static int lp872x_hw_enable(struct lp872x *lp)
714 gpio = lp->pdata->enable_gpio;
715 if (!gpio_is_valid(gpio))
718 /* Always set enable GPIO high. */
719 ret = devm_gpio_request_one(lp->dev, gpio, GPIOF_OUT_INIT_HIGH, "LP872X EN");
721 dev_err(lp->dev, "gpio request err: %d\n", ret);
725 /* Each chip has a different enable delay. */
726 if (lp->chipid == LP8720)
727 usleep_range(LP8720_ENABLE_DELAY, 1.5 * LP8720_ENABLE_DELAY);
729 usleep_range(LP8725_ENABLE_DELAY, 1.5 * LP8725_ENABLE_DELAY);
734 static int lp872x_config(struct lp872x *lp)
736 struct lp872x_platform_data *pdata = lp->pdata;
739 if (!pdata || !pdata->update_config)
742 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
747 return lp872x_init_dvs(lp);
750 static struct regulator_init_data
751 *lp872x_find_regulator_init_data(int id, struct lp872x *lp)
753 struct lp872x_platform_data *pdata = lp->pdata;
759 for (i = 0; i < lp->num_regulators; i++) {
760 if (pdata->regulator_data[i].id == id)
761 return pdata->regulator_data[i].init_data;
767 static int lp872x_regulator_register(struct lp872x *lp)
769 const struct regulator_desc *desc;
770 struct regulator_config cfg = { };
771 struct regulator_dev *rdev;
774 for (i = 0; i < lp->num_regulators; i++) {
775 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
776 &lp8725_regulator_desc[i];
779 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
780 cfg.driver_data = lp;
781 cfg.regmap = lp->regmap;
783 rdev = devm_regulator_register(lp->dev, desc, &cfg);
785 dev_err(lp->dev, "regulator register err");
786 return PTR_ERR(rdev);
793 static const struct regmap_config lp872x_regmap_config = {
796 .max_register = MAX_REGISTERS,
801 #define LP872X_VALID_OPMODE (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL)
803 static struct of_regulator_match lp8720_matches[] = {
804 { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, },
805 { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, },
806 { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, },
807 { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, },
808 { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, },
809 { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, },
812 static struct of_regulator_match lp8725_matches[] = {
813 { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, },
814 { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, },
815 { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, },
816 { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, },
817 { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, },
818 { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, },
819 { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, },
820 { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, },
821 { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, },
824 static struct lp872x_platform_data
825 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
827 struct device_node *np = dev->of_node;
828 struct lp872x_platform_data *pdata;
829 struct of_regulator_match *match;
835 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
837 return ERR_PTR(-ENOMEM);
839 of_property_read_u8(np, "ti,general-config", &pdata->general_config);
840 if (of_find_property(np, "ti,update-config", NULL))
841 pdata->update_config = true;
843 pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL);
845 return ERR_PTR(-ENOMEM);
847 pdata->dvs->gpio = of_get_named_gpio(np, "ti,dvs-gpio", 0);
848 of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel);
849 of_property_read_u8(np, "ti,dvs-state", &dvs_state);
850 pdata->dvs->init_state = dvs_state ? DVS_HIGH : DVS_LOW;
852 pdata->enable_gpio = of_get_named_gpio(np, "enable-gpios", 0);
854 if (of_get_child_count(np) == 0)
859 match = lp8720_matches;
860 num_matches = ARRAY_SIZE(lp8720_matches);
863 match = lp8725_matches;
864 num_matches = ARRAY_SIZE(lp8725_matches);
870 count = of_regulator_match(dev, np, match, num_matches);
874 for (i = 0; i < num_matches; i++) {
875 pdata->regulator_data[i].id =
876 (enum lp872x_regulator_id)match[i].driver_data;
877 pdata->regulator_data[i].init_data = match[i].init_data;
883 static struct lp872x_platform_data
884 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
890 static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
893 struct lp872x_platform_data *pdata;
895 static const int lp872x_num_regulators[] = {
896 [LP8720] = LP8720_NUM_REGULATORS,
897 [LP8725] = LP8725_NUM_REGULATORS,
900 if (cl->dev.of_node) {
901 pdata = lp872x_populate_pdata_from_dt(&cl->dev,
902 (enum lp872x_id)id->driver_data);
904 return PTR_ERR(pdata);
906 pdata = dev_get_platdata(&cl->dev);
909 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
913 lp->num_regulators = lp872x_num_regulators[id->driver_data];
915 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
916 if (IS_ERR(lp->regmap)) {
917 ret = PTR_ERR(lp->regmap);
918 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
924 lp->chipid = id->driver_data;
925 i2c_set_clientdata(cl, lp);
927 ret = lp872x_hw_enable(lp);
931 ret = lp872x_config(lp);
935 return lp872x_regulator_register(lp);
938 static const struct of_device_id lp872x_dt_ids[] = {
939 { .compatible = "ti,lp8720", },
940 { .compatible = "ti,lp8725", },
943 MODULE_DEVICE_TABLE(of, lp872x_dt_ids);
945 static const struct i2c_device_id lp872x_ids[] = {
950 MODULE_DEVICE_TABLE(i2c, lp872x_ids);
952 static struct i2c_driver lp872x_driver = {
955 .of_match_table = of_match_ptr(lp872x_dt_ids),
957 .probe = lp872x_probe,
958 .id_table = lp872x_ids,
961 module_i2c_driver(lp872x_driver);
963 MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
964 MODULE_AUTHOR("Milo Kim");
965 MODULE_LICENSE("GPL");