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 #include <linux/of_gpio.h>
23 #include <linux/regulator/of_regulator.h>
25 /* Registers : LP8720/8725 shared */
26 #define LP872X_GENERAL_CFG 0x00
27 #define LP872X_LDO1_VOUT 0x01
28 #define LP872X_LDO2_VOUT 0x02
29 #define LP872X_LDO3_VOUT 0x03
30 #define LP872X_LDO4_VOUT 0x04
31 #define LP872X_LDO5_VOUT 0x05
33 /* Registers : LP8720 */
34 #define LP8720_BUCK_VOUT1 0x06
35 #define LP8720_BUCK_VOUT2 0x07
36 #define LP8720_ENABLE 0x08
38 /* Registers : LP8725 */
39 #define LP8725_LILO1_VOUT 0x06
40 #define LP8725_LILO2_VOUT 0x07
41 #define LP8725_BUCK1_VOUT1 0x08
42 #define LP8725_BUCK1_VOUT2 0x09
43 #define LP8725_BUCK2_VOUT1 0x0A
44 #define LP8725_BUCK2_VOUT2 0x0B
45 #define LP8725_BUCK_CTRL 0x0C
46 #define LP8725_LDO_CTRL 0x0D
48 /* Mask/shift : LP8720/LP8725 shared */
49 #define LP872X_VOUT_M 0x1F
50 #define LP872X_START_DELAY_M 0xE0
51 #define LP872X_START_DELAY_S 5
52 #define LP872X_EN_LDO1_M BIT(0)
53 #define LP872X_EN_LDO2_M BIT(1)
54 #define LP872X_EN_LDO3_M BIT(2)
55 #define LP872X_EN_LDO4_M BIT(3)
56 #define LP872X_EN_LDO5_M BIT(4)
58 /* Mask/shift : LP8720 */
59 #define LP8720_TIMESTEP_S 0 /* Addr 00h */
60 #define LP8720_TIMESTEP_M BIT(0)
61 #define LP8720_EXT_DVS_M BIT(2)
62 #define LP8720_BUCK_FPWM_S 5 /* Addr 07h */
63 #define LP8720_BUCK_FPWM_M BIT(5)
64 #define LP8720_EN_BUCK_M BIT(5) /* Addr 08h */
65 #define LP8720_DVS_SEL_M BIT(7)
67 /* Mask/shift : LP8725 */
68 #define LP8725_TIMESTEP_M 0xC0 /* Addr 00h */
69 #define LP8725_TIMESTEP_S 6
70 #define LP8725_BUCK1_EN_M BIT(0)
71 #define LP8725_DVS1_M BIT(2)
72 #define LP8725_DVS2_M BIT(3)
73 #define LP8725_BUCK2_EN_M BIT(4)
74 #define LP8725_BUCK_CL_M 0xC0 /* Addr 09h, 0Bh */
75 #define LP8725_BUCK_CL_S 6
76 #define LP8725_BUCK1_FPWM_S 1 /* Addr 0Ch */
77 #define LP8725_BUCK1_FPWM_M BIT(1)
78 #define LP8725_BUCK2_FPWM_S 5
79 #define LP8725_BUCK2_FPWM_M BIT(5)
80 #define LP8725_EN_LILO1_M BIT(5) /* Addr 0Dh */
81 #define LP8725_EN_LILO2_M BIT(6)
84 #define LP872X_FORCE_PWM 1
85 #define LP872X_AUTO_PWM 0
87 #define LP8720_NUM_REGULATORS 6
88 #define LP8725_NUM_REGULATORS 9
89 #define EXTERN_DVS_USED 0
92 /* Default DVS Mode */
93 #define LP8720_DEFAULT_DVS 0
94 #define LP8725_DEFAULT_DVS BIT(2)
96 /* dump registers in regmap-debugfs */
97 #define MAX_REGISTERS 0x0F
105 struct regmap *regmap;
107 enum lp872x_id chipid;
108 struct lp872x_platform_data *pdata;
110 enum lp872x_dvs_state dvs_pin;
114 /* LP8720/LP8725 shared voltage table for LDOs */
115 static const unsigned int lp872x_ldo_vtbl[] = {
116 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
117 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
118 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
119 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
122 /* LP8720 LDO4 voltage table */
123 static const unsigned int lp8720_ldo4_vtbl[] = {
124 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000,
125 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
126 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
127 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
130 /* LP8725 LILO(Low Input Low Output) voltage table */
131 static const unsigned int lp8725_lilo_vtbl[] = {
132 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
133 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
134 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
135 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
138 /* LP8720 BUCK voltage table */
139 #define EXT_R 0 /* external resistor divider */
140 static const unsigned int lp8720_buck_vtbl[] = {
141 EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
142 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
143 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
144 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
147 /* LP8725 BUCK voltage table */
148 static const unsigned int lp8725_buck_vtbl[] = {
149 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
150 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
151 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
152 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
155 /* LP8725 BUCK current limit */
156 static const unsigned int lp8725_buck_uA[] = {
157 460000, 780000, 1050000, 1370000,
160 static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
165 ret = regmap_read(lp->regmap, addr, &val);
167 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
175 static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
177 return regmap_write(lp->regmap, addr, data);
180 static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
181 unsigned int mask, u8 data)
183 return regmap_update_bits(lp->regmap, addr, mask, data);
186 static int lp872x_get_timestep_usec(struct lp872x *lp)
188 enum lp872x_id chip = lp->chipid;
190 int *time_usec, size, ret;
191 int lp8720_time_usec[] = { 25, 50 };
192 int lp8725_time_usec[] = { 32, 64, 128, 256 };
196 mask = LP8720_TIMESTEP_M;
197 shift = LP8720_TIMESTEP_S;
198 time_usec = &lp8720_time_usec[0];
199 size = ARRAY_SIZE(lp8720_time_usec);
202 mask = LP8725_TIMESTEP_M;
203 shift = LP8725_TIMESTEP_S;
204 time_usec = &lp8725_time_usec[0];
205 size = ARRAY_SIZE(lp8725_time_usec);
211 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
215 val = (val & mask) >> shift;
219 return *(time_usec + val);
222 static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
224 struct lp872x *lp = rdev_get_drvdata(rdev);
225 enum lp872x_regulator_id rid = rdev_get_id(rdev);
226 int time_step_us = lp872x_get_timestep_usec(lp);
230 if (time_step_us < 0)
234 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
235 addr = LP872X_LDO1_VOUT + rid;
237 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
238 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
240 case LP8725_ID_BUCK2:
241 addr = LP8725_BUCK2_VOUT1;
247 ret = lp872x_read_byte(lp, addr, &val);
251 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
253 return val > MAX_DELAY ? 0 : val * time_step_us;
256 static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
259 enum lp872x_dvs_state state;
261 state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
262 gpio_set_value(gpio, state);
266 static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
267 enum lp872x_regulator_id buck)
271 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
276 if (val & LP8720_EXT_DVS_M) {
277 addr = (lp->dvs_pin == DVS_HIGH) ?
278 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
280 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
283 addr = val & LP8720_DVS_SEL_M ?
284 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
287 case LP8725_ID_BUCK1:
288 if (val & LP8725_DVS1_M)
289 addr = LP8725_BUCK1_VOUT1;
291 addr = (lp->dvs_pin == DVS_HIGH) ?
292 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
294 case LP8725_ID_BUCK2:
295 addr = val & LP8725_DVS2_M ?
296 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
305 static bool lp872x_is_valid_buck_addr(u8 addr)
308 case LP8720_BUCK_VOUT1:
309 case LP8720_BUCK_VOUT2:
310 case LP8725_BUCK1_VOUT1:
311 case LP8725_BUCK1_VOUT2:
312 case LP8725_BUCK2_VOUT1:
313 case LP8725_BUCK2_VOUT2:
320 static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
323 struct lp872x *lp = rdev_get_drvdata(rdev);
324 enum lp872x_regulator_id buck = rdev_get_id(rdev);
325 u8 addr, mask = LP872X_VOUT_M;
326 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
328 if (dvs && gpio_is_valid(dvs->gpio))
329 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
331 addr = lp872x_select_buck_vout_addr(lp, buck);
332 if (!lp872x_is_valid_buck_addr(addr))
335 return lp872x_update_bits(lp, addr, mask, selector);
338 static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
340 struct lp872x *lp = rdev_get_drvdata(rdev);
341 enum lp872x_regulator_id buck = rdev_get_id(rdev);
345 addr = lp872x_select_buck_vout_addr(lp, buck);
346 if (!lp872x_is_valid_buck_addr(addr))
349 ret = lp872x_read_byte(lp, addr, &val);
353 return val & LP872X_VOUT_M;
356 static int lp8725_buck_set_current_limit(struct regulator_dev *rdev,
357 int min_uA, int max_uA)
359 struct lp872x *lp = rdev_get_drvdata(rdev);
360 enum lp872x_regulator_id buck = rdev_get_id(rdev);
365 case LP8725_ID_BUCK1:
366 addr = LP8725_BUCK1_VOUT2;
368 case LP8725_ID_BUCK2:
369 addr = LP8725_BUCK2_VOUT2;
375 for (i = ARRAY_SIZE(lp8725_buck_uA) - 1; i >= 0; i--) {
376 if (lp8725_buck_uA[i] >= min_uA &&
377 lp8725_buck_uA[i] <= max_uA)
378 return lp872x_update_bits(lp, addr,
380 i << LP8725_BUCK_CL_S);
386 static int lp8725_buck_get_current_limit(struct regulator_dev *rdev)
388 struct lp872x *lp = rdev_get_drvdata(rdev);
389 enum lp872x_regulator_id buck = rdev_get_id(rdev);
394 case LP8725_ID_BUCK1:
395 addr = LP8725_BUCK1_VOUT2;
397 case LP8725_ID_BUCK2:
398 addr = LP8725_BUCK2_VOUT2;
404 ret = lp872x_read_byte(lp, addr, &val);
408 val = (val & LP8725_BUCK_CL_M) >> LP8725_BUCK_CL_S;
410 return (val < ARRAY_SIZE(lp8725_buck_uA)) ?
411 lp8725_buck_uA[val] : -EINVAL;
414 static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
416 struct lp872x *lp = rdev_get_drvdata(rdev);
417 enum lp872x_regulator_id buck = rdev_get_id(rdev);
418 u8 addr, mask, shift, val;
422 addr = LP8720_BUCK_VOUT2;
423 mask = LP8720_BUCK_FPWM_M;
424 shift = LP8720_BUCK_FPWM_S;
426 case LP8725_ID_BUCK1:
427 addr = LP8725_BUCK_CTRL;
428 mask = LP8725_BUCK1_FPWM_M;
429 shift = LP8725_BUCK1_FPWM_S;
431 case LP8725_ID_BUCK2:
432 addr = LP8725_BUCK_CTRL;
433 mask = LP8725_BUCK2_FPWM_M;
434 shift = LP8725_BUCK2_FPWM_S;
440 if (mode == REGULATOR_MODE_FAST)
441 val = LP872X_FORCE_PWM << shift;
442 else if (mode == REGULATOR_MODE_NORMAL)
443 val = LP872X_AUTO_PWM << shift;
447 return lp872x_update_bits(lp, addr, mask, val);
450 static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
452 struct lp872x *lp = rdev_get_drvdata(rdev);
453 enum lp872x_regulator_id buck = rdev_get_id(rdev);
459 addr = LP8720_BUCK_VOUT2;
460 mask = LP8720_BUCK_FPWM_M;
462 case LP8725_ID_BUCK1:
463 addr = LP8725_BUCK_CTRL;
464 mask = LP8725_BUCK1_FPWM_M;
466 case LP8725_ID_BUCK2:
467 addr = LP8725_BUCK_CTRL;
468 mask = LP8725_BUCK2_FPWM_M;
474 ret = lp872x_read_byte(lp, addr, &val);
478 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
481 static struct regulator_ops lp872x_ldo_ops = {
482 .list_voltage = regulator_list_voltage_table,
483 .map_voltage = regulator_map_voltage_ascend,
484 .set_voltage_sel = regulator_set_voltage_sel_regmap,
485 .get_voltage_sel = regulator_get_voltage_sel_regmap,
486 .enable = regulator_enable_regmap,
487 .disable = regulator_disable_regmap,
488 .is_enabled = regulator_is_enabled_regmap,
489 .enable_time = lp872x_regulator_enable_time,
492 static struct regulator_ops lp8720_buck_ops = {
493 .list_voltage = regulator_list_voltage_table,
494 .map_voltage = regulator_map_voltage_ascend,
495 .set_voltage_sel = lp872x_buck_set_voltage_sel,
496 .get_voltage_sel = lp872x_buck_get_voltage_sel,
497 .enable = regulator_enable_regmap,
498 .disable = regulator_disable_regmap,
499 .is_enabled = regulator_is_enabled_regmap,
500 .enable_time = lp872x_regulator_enable_time,
501 .set_mode = lp872x_buck_set_mode,
502 .get_mode = lp872x_buck_get_mode,
505 static struct regulator_ops lp8725_buck_ops = {
506 .list_voltage = regulator_list_voltage_table,
507 .map_voltage = regulator_map_voltage_ascend,
508 .set_voltage_sel = lp872x_buck_set_voltage_sel,
509 .get_voltage_sel = lp872x_buck_get_voltage_sel,
510 .enable = regulator_enable_regmap,
511 .disable = regulator_disable_regmap,
512 .is_enabled = regulator_is_enabled_regmap,
513 .enable_time = lp872x_regulator_enable_time,
514 .set_mode = lp872x_buck_set_mode,
515 .get_mode = lp872x_buck_get_mode,
516 .set_current_limit = lp8725_buck_set_current_limit,
517 .get_current_limit = lp8725_buck_get_current_limit,
520 static struct regulator_desc lp8720_regulator_desc[] = {
523 .id = LP8720_ID_LDO1,
524 .ops = &lp872x_ldo_ops,
525 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
526 .volt_table = lp872x_ldo_vtbl,
527 .type = REGULATOR_VOLTAGE,
528 .owner = THIS_MODULE,
529 .vsel_reg = LP872X_LDO1_VOUT,
530 .vsel_mask = LP872X_VOUT_M,
531 .enable_reg = LP8720_ENABLE,
532 .enable_mask = LP872X_EN_LDO1_M,
536 .id = LP8720_ID_LDO2,
537 .ops = &lp872x_ldo_ops,
538 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
539 .volt_table = lp872x_ldo_vtbl,
540 .type = REGULATOR_VOLTAGE,
541 .owner = THIS_MODULE,
542 .vsel_reg = LP872X_LDO2_VOUT,
543 .vsel_mask = LP872X_VOUT_M,
544 .enable_reg = LP8720_ENABLE,
545 .enable_mask = LP872X_EN_LDO2_M,
549 .id = LP8720_ID_LDO3,
550 .ops = &lp872x_ldo_ops,
551 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
552 .volt_table = lp872x_ldo_vtbl,
553 .type = REGULATOR_VOLTAGE,
554 .owner = THIS_MODULE,
555 .vsel_reg = LP872X_LDO3_VOUT,
556 .vsel_mask = LP872X_VOUT_M,
557 .enable_reg = LP8720_ENABLE,
558 .enable_mask = LP872X_EN_LDO3_M,
562 .id = LP8720_ID_LDO4,
563 .ops = &lp872x_ldo_ops,
564 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
565 .volt_table = lp8720_ldo4_vtbl,
566 .type = REGULATOR_VOLTAGE,
567 .owner = THIS_MODULE,
568 .vsel_reg = LP872X_LDO4_VOUT,
569 .vsel_mask = LP872X_VOUT_M,
570 .enable_reg = LP8720_ENABLE,
571 .enable_mask = LP872X_EN_LDO4_M,
575 .id = LP8720_ID_LDO5,
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_LDO5_VOUT,
582 .vsel_mask = LP872X_VOUT_M,
583 .enable_reg = LP8720_ENABLE,
584 .enable_mask = LP872X_EN_LDO5_M,
588 .id = LP8720_ID_BUCK,
589 .ops = &lp8720_buck_ops,
590 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
591 .volt_table = lp8720_buck_vtbl,
592 .type = REGULATOR_VOLTAGE,
593 .owner = THIS_MODULE,
594 .enable_reg = LP8720_ENABLE,
595 .enable_mask = LP8720_EN_BUCK_M,
599 static struct regulator_desc lp8725_regulator_desc[] = {
602 .id = LP8725_ID_LDO1,
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_LDO1_VOUT,
609 .vsel_mask = LP872X_VOUT_M,
610 .enable_reg = LP8725_LDO_CTRL,
611 .enable_mask = LP872X_EN_LDO1_M,
615 .id = LP8725_ID_LDO2,
616 .ops = &lp872x_ldo_ops,
617 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
618 .volt_table = lp872x_ldo_vtbl,
619 .type = REGULATOR_VOLTAGE,
620 .owner = THIS_MODULE,
621 .vsel_reg = LP872X_LDO2_VOUT,
622 .vsel_mask = LP872X_VOUT_M,
623 .enable_reg = LP8725_LDO_CTRL,
624 .enable_mask = LP872X_EN_LDO2_M,
628 .id = LP8725_ID_LDO3,
629 .ops = &lp872x_ldo_ops,
630 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
631 .volt_table = lp872x_ldo_vtbl,
632 .type = REGULATOR_VOLTAGE,
633 .owner = THIS_MODULE,
634 .vsel_reg = LP872X_LDO3_VOUT,
635 .vsel_mask = LP872X_VOUT_M,
636 .enable_reg = LP8725_LDO_CTRL,
637 .enable_mask = LP872X_EN_LDO3_M,
641 .id = LP8725_ID_LDO4,
642 .ops = &lp872x_ldo_ops,
643 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
644 .volt_table = lp872x_ldo_vtbl,
645 .type = REGULATOR_VOLTAGE,
646 .owner = THIS_MODULE,
647 .vsel_reg = LP872X_LDO4_VOUT,
648 .vsel_mask = LP872X_VOUT_M,
649 .enable_reg = LP8725_LDO_CTRL,
650 .enable_mask = LP872X_EN_LDO4_M,
654 .id = LP8725_ID_LDO5,
655 .ops = &lp872x_ldo_ops,
656 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
657 .volt_table = lp872x_ldo_vtbl,
658 .type = REGULATOR_VOLTAGE,
659 .owner = THIS_MODULE,
660 .vsel_reg = LP872X_LDO5_VOUT,
661 .vsel_mask = LP872X_VOUT_M,
662 .enable_reg = LP8725_LDO_CTRL,
663 .enable_mask = LP872X_EN_LDO5_M,
667 .id = LP8725_ID_LILO1,
668 .ops = &lp872x_ldo_ops,
669 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
670 .volt_table = lp8725_lilo_vtbl,
671 .type = REGULATOR_VOLTAGE,
672 .owner = THIS_MODULE,
673 .vsel_reg = LP8725_LILO1_VOUT,
674 .vsel_mask = LP872X_VOUT_M,
675 .enable_reg = LP8725_LDO_CTRL,
676 .enable_mask = LP8725_EN_LILO1_M,
680 .id = LP8725_ID_LILO2,
681 .ops = &lp872x_ldo_ops,
682 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
683 .volt_table = lp8725_lilo_vtbl,
684 .type = REGULATOR_VOLTAGE,
685 .owner = THIS_MODULE,
686 .vsel_reg = LP8725_LILO2_VOUT,
687 .vsel_mask = LP872X_VOUT_M,
688 .enable_reg = LP8725_LDO_CTRL,
689 .enable_mask = LP8725_EN_LILO2_M,
693 .id = LP8725_ID_BUCK1,
694 .ops = &lp8725_buck_ops,
695 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
696 .volt_table = lp8725_buck_vtbl,
697 .type = REGULATOR_VOLTAGE,
698 .owner = THIS_MODULE,
699 .enable_reg = LP872X_GENERAL_CFG,
700 .enable_mask = LP8725_BUCK1_EN_M,
704 .id = LP8725_ID_BUCK2,
705 .ops = &lp8725_buck_ops,
706 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
707 .volt_table = lp8725_buck_vtbl,
708 .type = REGULATOR_VOLTAGE,
709 .owner = THIS_MODULE,
710 .enable_reg = LP872X_GENERAL_CFG,
711 .enable_mask = LP8725_BUCK2_EN_M,
715 static int lp872x_init_dvs(struct lp872x *lp)
718 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
719 enum lp872x_dvs_state pinstate;
720 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
721 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
724 goto set_default_dvs_mode;
727 if (!gpio_is_valid(gpio)) {
728 dev_warn(lp->dev, "invalid gpio: %d\n", gpio);
729 goto set_default_dvs_mode;
732 pinstate = dvs->init_state;
733 ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
735 dev_err(lp->dev, "gpio request err: %d\n", ret);
739 lp->dvs_pin = pinstate;
744 set_default_dvs_mode:
745 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
746 default_dvs_mode[lp->chipid]);
749 static int lp872x_config(struct lp872x *lp)
751 struct lp872x_platform_data *pdata = lp->pdata;
754 if (!pdata || !pdata->update_config)
757 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
762 return lp872x_init_dvs(lp);
765 static struct regulator_init_data
766 *lp872x_find_regulator_init_data(int id, struct lp872x *lp)
768 struct lp872x_platform_data *pdata = lp->pdata;
774 for (i = 0; i < lp->num_regulators; i++) {
775 if (pdata->regulator_data[i].id == id)
776 return pdata->regulator_data[i].init_data;
782 static int lp872x_regulator_register(struct lp872x *lp)
784 struct regulator_desc *desc;
785 struct regulator_config cfg = { };
786 struct regulator_dev *rdev;
789 for (i = 0; i < lp->num_regulators; i++) {
790 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
791 &lp8725_regulator_desc[i];
794 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
795 cfg.driver_data = lp;
796 cfg.regmap = lp->regmap;
798 rdev = devm_regulator_register(lp->dev, desc, &cfg);
800 dev_err(lp->dev, "regulator register err");
801 return PTR_ERR(rdev);
808 static const struct regmap_config lp872x_regmap_config = {
811 .max_register = MAX_REGISTERS,
816 #define LP872X_VALID_OPMODE (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL)
818 static struct of_regulator_match lp8720_matches[] = {
819 { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, },
820 { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, },
821 { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, },
822 { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, },
823 { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, },
824 { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, },
827 static struct of_regulator_match lp8725_matches[] = {
828 { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, },
829 { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, },
830 { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, },
831 { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, },
832 { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, },
833 { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, },
834 { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, },
835 { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, },
836 { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, },
839 static struct lp872x_platform_data
840 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
842 struct device_node *np = dev->of_node;
843 struct lp872x_platform_data *pdata;
844 struct of_regulator_match *match;
850 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
852 return ERR_PTR(-ENOMEM);
854 of_property_read_u8(np, "ti,general-config", &pdata->general_config);
855 if (of_find_property(np, "ti,update-config", NULL))
856 pdata->update_config = true;
858 pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL);
860 return ERR_PTR(-ENOMEM);
862 pdata->dvs->gpio = of_get_named_gpio(np, "ti,dvs-gpio", 0);
863 of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel);
864 of_property_read_u8(np, "ti,dvs-state", &dvs_state);
865 pdata->dvs->init_state = dvs_state ? DVS_HIGH : DVS_LOW;
867 if (of_get_child_count(np) == 0)
872 match = lp8720_matches;
873 num_matches = ARRAY_SIZE(lp8720_matches);
876 match = lp8725_matches;
877 num_matches = ARRAY_SIZE(lp8725_matches);
883 count = of_regulator_match(dev, np, match, num_matches);
887 for (i = 0; i < num_matches; i++) {
888 pdata->regulator_data[i].id =
889 (enum lp872x_regulator_id)match[i].driver_data;
890 pdata->regulator_data[i].init_data = match[i].init_data;
896 static struct lp872x_platform_data
897 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
903 static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
906 struct lp872x_platform_data *pdata;
908 const int lp872x_num_regulators[] = {
909 [LP8720] = LP8720_NUM_REGULATORS,
910 [LP8725] = LP8725_NUM_REGULATORS,
913 if (cl->dev.of_node) {
914 pdata = lp872x_populate_pdata_from_dt(&cl->dev,
915 (enum lp872x_id)id->driver_data);
917 return PTR_ERR(pdata);
919 pdata = dev_get_platdata(&cl->dev);
922 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
926 lp->num_regulators = lp872x_num_regulators[id->driver_data];
928 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
929 if (IS_ERR(lp->regmap)) {
930 ret = PTR_ERR(lp->regmap);
931 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
937 lp->chipid = id->driver_data;
938 i2c_set_clientdata(cl, lp);
940 ret = lp872x_config(lp);
944 return lp872x_regulator_register(lp);
947 static const struct of_device_id lp872x_dt_ids[] = {
948 { .compatible = "ti,lp8720", },
949 { .compatible = "ti,lp8725", },
952 MODULE_DEVICE_TABLE(of, lp872x_dt_ids);
954 static const struct i2c_device_id lp872x_ids[] = {
959 MODULE_DEVICE_TABLE(i2c, lp872x_ids);
961 static struct i2c_driver lp872x_driver = {
964 .of_match_table = of_match_ptr(lp872x_dt_ids),
966 .probe = lp872x_probe,
967 .id_table = lp872x_ids,
970 module_i2c_driver(lp872x_driver);
972 MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
973 MODULE_AUTHOR("Milo Kim");
974 MODULE_LICENSE("GPL");