x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / regulator / lp872x.c
blob2b84b727a3c498c6e22f97fe9d297d06ee1470c8
1 /*
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>
21 #include <linux/of.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)
83 /* PWM mode */
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
90 #define MAX_DELAY 6
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
99 enum lp872x_id {
100 LP8720,
101 LP8725,
104 struct lp872x {
105 struct regmap *regmap;
106 struct device *dev;
107 enum lp872x_id chipid;
108 struct lp872x_platform_data *pdata;
109 struct regulator_dev **regulators;
110 int num_regulators;
111 enum lp872x_dvs_state dvs_pin;
112 int dvs_gpio;
115 /* LP8720/LP8725 shared voltage table for LDOs */
116 static const unsigned int lp872x_ldo_vtbl[] = {
117 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
118 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
119 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
120 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
123 /* LP8720 LDO4 voltage table */
124 static const unsigned int lp8720_ldo4_vtbl[] = {
125 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000,
126 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
127 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
128 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
131 /* LP8725 LILO(Low Input Low Output) voltage table */
132 static const unsigned int lp8725_lilo_vtbl[] = {
133 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
134 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
135 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
136 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
139 /* LP8720 BUCK voltage table */
140 #define EXT_R 0 /* external resistor divider */
141 static const unsigned int lp8720_buck_vtbl[] = {
142 EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
143 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
144 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
145 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
148 /* LP8725 BUCK voltage table */
149 static const unsigned int lp8725_buck_vtbl[] = {
150 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
151 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
152 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
153 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
156 /* LP8725 BUCK current limit */
157 static const unsigned int lp8725_buck_uA[] = {
158 460000, 780000, 1050000, 1370000,
161 static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
163 int ret;
164 unsigned int val;
166 ret = regmap_read(lp->regmap, addr, &val);
167 if (ret < 0) {
168 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
169 return ret;
172 *data = (u8)val;
173 return 0;
176 static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
178 return regmap_write(lp->regmap, addr, data);
181 static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
182 unsigned int mask, u8 data)
184 return regmap_update_bits(lp->regmap, addr, mask, data);
187 static int lp872x_get_timestep_usec(struct lp872x *lp)
189 enum lp872x_id chip = lp->chipid;
190 u8 val, mask, shift;
191 int *time_usec, size, ret;
192 int lp8720_time_usec[] = { 25, 50 };
193 int lp8725_time_usec[] = { 32, 64, 128, 256 };
195 switch (chip) {
196 case LP8720:
197 mask = LP8720_TIMESTEP_M;
198 shift = LP8720_TIMESTEP_S;
199 time_usec = &lp8720_time_usec[0];
200 size = ARRAY_SIZE(lp8720_time_usec);
201 break;
202 case LP8725:
203 mask = LP8725_TIMESTEP_M;
204 shift = LP8725_TIMESTEP_S;
205 time_usec = &lp8725_time_usec[0];
206 size = ARRAY_SIZE(lp8725_time_usec);
207 break;
208 default:
209 return -EINVAL;
212 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
213 if (ret)
214 return -EINVAL;
216 val = (val & mask) >> shift;
217 if (val >= size)
218 return -EINVAL;
220 return *(time_usec + val);
223 static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
225 struct lp872x *lp = rdev_get_drvdata(rdev);
226 enum lp872x_regulator_id rid = rdev_get_id(rdev);
227 int time_step_us = lp872x_get_timestep_usec(lp);
228 int ret;
229 u8 addr, val;
231 if (time_step_us < 0)
232 return -EINVAL;
234 switch (rid) {
235 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
236 addr = LP872X_LDO1_VOUT + rid;
237 break;
238 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
239 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
240 break;
241 case LP8725_ID_BUCK2:
242 addr = LP8725_BUCK2_VOUT1;
243 break;
244 default:
245 return -EINVAL;
248 ret = lp872x_read_byte(lp, addr, &val);
249 if (ret)
250 return ret;
252 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
254 return val > MAX_DELAY ? 0 : val * time_step_us;
257 static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
258 int gpio)
260 enum lp872x_dvs_state state;
262 state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
263 gpio_set_value(gpio, state);
264 lp->dvs_pin = state;
267 static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
268 enum lp872x_regulator_id buck)
270 u8 val, addr;
272 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
273 return 0;
275 switch (buck) {
276 case LP8720_ID_BUCK:
277 if (val & LP8720_EXT_DVS_M) {
278 addr = (lp->dvs_pin == DVS_HIGH) ?
279 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
280 } else {
281 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
282 return 0;
284 addr = val & LP8720_DVS_SEL_M ?
285 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
287 break;
288 case LP8725_ID_BUCK1:
289 if (val & LP8725_DVS1_M)
290 addr = LP8725_BUCK1_VOUT1;
291 else
292 addr = (lp->dvs_pin == DVS_HIGH) ?
293 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
294 break;
295 case LP8725_ID_BUCK2:
296 addr = val & LP8725_DVS2_M ?
297 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
298 break;
299 default:
300 return 0;
303 return addr;
306 static bool lp872x_is_valid_buck_addr(u8 addr)
308 switch (addr) {
309 case LP8720_BUCK_VOUT1:
310 case LP8720_BUCK_VOUT2:
311 case LP8725_BUCK1_VOUT1:
312 case LP8725_BUCK1_VOUT2:
313 case LP8725_BUCK2_VOUT1:
314 case LP8725_BUCK2_VOUT2:
315 return true;
316 default:
317 return false;
321 static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
322 unsigned selector)
324 struct lp872x *lp = rdev_get_drvdata(rdev);
325 enum lp872x_regulator_id buck = rdev_get_id(rdev);
326 u8 addr, mask = LP872X_VOUT_M;
327 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
329 if (dvs && gpio_is_valid(dvs->gpio))
330 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
332 addr = lp872x_select_buck_vout_addr(lp, buck);
333 if (!lp872x_is_valid_buck_addr(addr))
334 return -EINVAL;
336 return lp872x_update_bits(lp, addr, mask, selector);
339 static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
341 struct lp872x *lp = rdev_get_drvdata(rdev);
342 enum lp872x_regulator_id buck = rdev_get_id(rdev);
343 u8 addr, val;
344 int ret;
346 addr = lp872x_select_buck_vout_addr(lp, buck);
347 if (!lp872x_is_valid_buck_addr(addr))
348 return -EINVAL;
350 ret = lp872x_read_byte(lp, addr, &val);
351 if (ret)
352 return ret;
354 return val & LP872X_VOUT_M;
357 static int lp8725_buck_set_current_limit(struct regulator_dev *rdev,
358 int min_uA, int max_uA)
360 struct lp872x *lp = rdev_get_drvdata(rdev);
361 enum lp872x_regulator_id buck = rdev_get_id(rdev);
362 int i;
363 u8 addr;
365 switch (buck) {
366 case LP8725_ID_BUCK1:
367 addr = LP8725_BUCK1_VOUT2;
368 break;
369 case LP8725_ID_BUCK2:
370 addr = LP8725_BUCK2_VOUT2;
371 break;
372 default:
373 return -EINVAL;
376 for (i = ARRAY_SIZE(lp8725_buck_uA) - 1; i >= 0; i--) {
377 if (lp8725_buck_uA[i] >= min_uA &&
378 lp8725_buck_uA[i] <= max_uA)
379 return lp872x_update_bits(lp, addr,
380 LP8725_BUCK_CL_M,
381 i << LP8725_BUCK_CL_S);
384 return -EINVAL;
387 static int lp8725_buck_get_current_limit(struct regulator_dev *rdev)
389 struct lp872x *lp = rdev_get_drvdata(rdev);
390 enum lp872x_regulator_id buck = rdev_get_id(rdev);
391 u8 addr, val;
392 int ret;
394 switch (buck) {
395 case LP8725_ID_BUCK1:
396 addr = LP8725_BUCK1_VOUT2;
397 break;
398 case LP8725_ID_BUCK2:
399 addr = LP8725_BUCK2_VOUT2;
400 break;
401 default:
402 return -EINVAL;
405 ret = lp872x_read_byte(lp, addr, &val);
406 if (ret)
407 return ret;
409 val = (val & LP8725_BUCK_CL_M) >> LP8725_BUCK_CL_S;
411 return (val < ARRAY_SIZE(lp8725_buck_uA)) ?
412 lp8725_buck_uA[val] : -EINVAL;
415 static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
417 struct lp872x *lp = rdev_get_drvdata(rdev);
418 enum lp872x_regulator_id buck = rdev_get_id(rdev);
419 u8 addr, mask, shift, val;
421 switch (buck) {
422 case LP8720_ID_BUCK:
423 addr = LP8720_BUCK_VOUT2;
424 mask = LP8720_BUCK_FPWM_M;
425 shift = LP8720_BUCK_FPWM_S;
426 break;
427 case LP8725_ID_BUCK1:
428 addr = LP8725_BUCK_CTRL;
429 mask = LP8725_BUCK1_FPWM_M;
430 shift = LP8725_BUCK1_FPWM_S;
431 break;
432 case LP8725_ID_BUCK2:
433 addr = LP8725_BUCK_CTRL;
434 mask = LP8725_BUCK2_FPWM_M;
435 shift = LP8725_BUCK2_FPWM_S;
436 break;
437 default:
438 return -EINVAL;
441 if (mode == REGULATOR_MODE_FAST)
442 val = LP872X_FORCE_PWM << shift;
443 else if (mode == REGULATOR_MODE_NORMAL)
444 val = LP872X_AUTO_PWM << shift;
445 else
446 return -EINVAL;
448 return lp872x_update_bits(lp, addr, mask, val);
451 static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
453 struct lp872x *lp = rdev_get_drvdata(rdev);
454 enum lp872x_regulator_id buck = rdev_get_id(rdev);
455 u8 addr, mask, val;
456 int ret;
458 switch (buck) {
459 case LP8720_ID_BUCK:
460 addr = LP8720_BUCK_VOUT2;
461 mask = LP8720_BUCK_FPWM_M;
462 break;
463 case LP8725_ID_BUCK1:
464 addr = LP8725_BUCK_CTRL;
465 mask = LP8725_BUCK1_FPWM_M;
466 break;
467 case LP8725_ID_BUCK2:
468 addr = LP8725_BUCK_CTRL;
469 mask = LP8725_BUCK2_FPWM_M;
470 break;
471 default:
472 return -EINVAL;
475 ret = lp872x_read_byte(lp, addr, &val);
476 if (ret)
477 return ret;
479 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
482 static struct regulator_ops lp872x_ldo_ops = {
483 .list_voltage = regulator_list_voltage_table,
484 .map_voltage = regulator_map_voltage_ascend,
485 .set_voltage_sel = regulator_set_voltage_sel_regmap,
486 .get_voltage_sel = regulator_get_voltage_sel_regmap,
487 .enable = regulator_enable_regmap,
488 .disable = regulator_disable_regmap,
489 .is_enabled = regulator_is_enabled_regmap,
490 .enable_time = lp872x_regulator_enable_time,
493 static struct regulator_ops lp8720_buck_ops = {
494 .list_voltage = regulator_list_voltage_table,
495 .map_voltage = regulator_map_voltage_ascend,
496 .set_voltage_sel = lp872x_buck_set_voltage_sel,
497 .get_voltage_sel = lp872x_buck_get_voltage_sel,
498 .enable = regulator_enable_regmap,
499 .disable = regulator_disable_regmap,
500 .is_enabled = regulator_is_enabled_regmap,
501 .enable_time = lp872x_regulator_enable_time,
502 .set_mode = lp872x_buck_set_mode,
503 .get_mode = lp872x_buck_get_mode,
506 static struct regulator_ops lp8725_buck_ops = {
507 .list_voltage = regulator_list_voltage_table,
508 .map_voltage = regulator_map_voltage_ascend,
509 .set_voltage_sel = lp872x_buck_set_voltage_sel,
510 .get_voltage_sel = lp872x_buck_get_voltage_sel,
511 .enable = regulator_enable_regmap,
512 .disable = regulator_disable_regmap,
513 .is_enabled = regulator_is_enabled_regmap,
514 .enable_time = lp872x_regulator_enable_time,
515 .set_mode = lp872x_buck_set_mode,
516 .get_mode = lp872x_buck_get_mode,
517 .set_current_limit = lp8725_buck_set_current_limit,
518 .get_current_limit = lp8725_buck_get_current_limit,
521 static struct regulator_desc lp8720_regulator_desc[] = {
523 .name = "ldo1",
524 .id = LP8720_ID_LDO1,
525 .ops = &lp872x_ldo_ops,
526 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
527 .volt_table = lp872x_ldo_vtbl,
528 .type = REGULATOR_VOLTAGE,
529 .owner = THIS_MODULE,
530 .vsel_reg = LP872X_LDO1_VOUT,
531 .vsel_mask = LP872X_VOUT_M,
532 .enable_reg = LP8720_ENABLE,
533 .enable_mask = LP872X_EN_LDO1_M,
536 .name = "ldo2",
537 .id = LP8720_ID_LDO2,
538 .ops = &lp872x_ldo_ops,
539 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
540 .volt_table = lp872x_ldo_vtbl,
541 .type = REGULATOR_VOLTAGE,
542 .owner = THIS_MODULE,
543 .vsel_reg = LP872X_LDO2_VOUT,
544 .vsel_mask = LP872X_VOUT_M,
545 .enable_reg = LP8720_ENABLE,
546 .enable_mask = LP872X_EN_LDO2_M,
549 .name = "ldo3",
550 .id = LP8720_ID_LDO3,
551 .ops = &lp872x_ldo_ops,
552 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
553 .volt_table = lp872x_ldo_vtbl,
554 .type = REGULATOR_VOLTAGE,
555 .owner = THIS_MODULE,
556 .vsel_reg = LP872X_LDO3_VOUT,
557 .vsel_mask = LP872X_VOUT_M,
558 .enable_reg = LP8720_ENABLE,
559 .enable_mask = LP872X_EN_LDO3_M,
562 .name = "ldo4",
563 .id = LP8720_ID_LDO4,
564 .ops = &lp872x_ldo_ops,
565 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
566 .volt_table = lp8720_ldo4_vtbl,
567 .type = REGULATOR_VOLTAGE,
568 .owner = THIS_MODULE,
569 .vsel_reg = LP872X_LDO4_VOUT,
570 .vsel_mask = LP872X_VOUT_M,
571 .enable_reg = LP8720_ENABLE,
572 .enable_mask = LP872X_EN_LDO4_M,
575 .name = "ldo5",
576 .id = LP8720_ID_LDO5,
577 .ops = &lp872x_ldo_ops,
578 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
579 .volt_table = lp872x_ldo_vtbl,
580 .type = REGULATOR_VOLTAGE,
581 .owner = THIS_MODULE,
582 .vsel_reg = LP872X_LDO5_VOUT,
583 .vsel_mask = LP872X_VOUT_M,
584 .enable_reg = LP8720_ENABLE,
585 .enable_mask = LP872X_EN_LDO5_M,
588 .name = "buck",
589 .id = LP8720_ID_BUCK,
590 .ops = &lp8720_buck_ops,
591 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
592 .volt_table = lp8720_buck_vtbl,
593 .type = REGULATOR_VOLTAGE,
594 .owner = THIS_MODULE,
595 .enable_reg = LP8720_ENABLE,
596 .enable_mask = LP8720_EN_BUCK_M,
600 static struct regulator_desc lp8725_regulator_desc[] = {
602 .name = "ldo1",
603 .id = LP8725_ID_LDO1,
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_LDO1_VOUT,
610 .vsel_mask = LP872X_VOUT_M,
611 .enable_reg = LP8725_LDO_CTRL,
612 .enable_mask = LP872X_EN_LDO1_M,
615 .name = "ldo2",
616 .id = LP8725_ID_LDO2,
617 .ops = &lp872x_ldo_ops,
618 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
619 .volt_table = lp872x_ldo_vtbl,
620 .type = REGULATOR_VOLTAGE,
621 .owner = THIS_MODULE,
622 .vsel_reg = LP872X_LDO2_VOUT,
623 .vsel_mask = LP872X_VOUT_M,
624 .enable_reg = LP8725_LDO_CTRL,
625 .enable_mask = LP872X_EN_LDO2_M,
628 .name = "ldo3",
629 .id = LP8725_ID_LDO3,
630 .ops = &lp872x_ldo_ops,
631 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
632 .volt_table = lp872x_ldo_vtbl,
633 .type = REGULATOR_VOLTAGE,
634 .owner = THIS_MODULE,
635 .vsel_reg = LP872X_LDO3_VOUT,
636 .vsel_mask = LP872X_VOUT_M,
637 .enable_reg = LP8725_LDO_CTRL,
638 .enable_mask = LP872X_EN_LDO3_M,
641 .name = "ldo4",
642 .id = LP8725_ID_LDO4,
643 .ops = &lp872x_ldo_ops,
644 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
645 .volt_table = lp872x_ldo_vtbl,
646 .type = REGULATOR_VOLTAGE,
647 .owner = THIS_MODULE,
648 .vsel_reg = LP872X_LDO4_VOUT,
649 .vsel_mask = LP872X_VOUT_M,
650 .enable_reg = LP8725_LDO_CTRL,
651 .enable_mask = LP872X_EN_LDO4_M,
654 .name = "ldo5",
655 .id = LP8725_ID_LDO5,
656 .ops = &lp872x_ldo_ops,
657 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
658 .volt_table = lp872x_ldo_vtbl,
659 .type = REGULATOR_VOLTAGE,
660 .owner = THIS_MODULE,
661 .vsel_reg = LP872X_LDO5_VOUT,
662 .vsel_mask = LP872X_VOUT_M,
663 .enable_reg = LP8725_LDO_CTRL,
664 .enable_mask = LP872X_EN_LDO5_M,
667 .name = "lilo1",
668 .id = LP8725_ID_LILO1,
669 .ops = &lp872x_ldo_ops,
670 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
671 .volt_table = lp8725_lilo_vtbl,
672 .type = REGULATOR_VOLTAGE,
673 .owner = THIS_MODULE,
674 .vsel_reg = LP8725_LILO1_VOUT,
675 .vsel_mask = LP872X_VOUT_M,
676 .enable_reg = LP8725_LDO_CTRL,
677 .enable_mask = LP8725_EN_LILO1_M,
680 .name = "lilo2",
681 .id = LP8725_ID_LILO2,
682 .ops = &lp872x_ldo_ops,
683 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
684 .volt_table = lp8725_lilo_vtbl,
685 .type = REGULATOR_VOLTAGE,
686 .owner = THIS_MODULE,
687 .vsel_reg = LP8725_LILO2_VOUT,
688 .vsel_mask = LP872X_VOUT_M,
689 .enable_reg = LP8725_LDO_CTRL,
690 .enable_mask = LP8725_EN_LILO2_M,
693 .name = "buck1",
694 .id = LP8725_ID_BUCK1,
695 .ops = &lp8725_buck_ops,
696 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
697 .volt_table = lp8725_buck_vtbl,
698 .type = REGULATOR_VOLTAGE,
699 .owner = THIS_MODULE,
700 .enable_reg = LP872X_GENERAL_CFG,
701 .enable_mask = LP8725_BUCK1_EN_M,
704 .name = "buck2",
705 .id = LP8725_ID_BUCK2,
706 .ops = &lp8725_buck_ops,
707 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
708 .volt_table = lp8725_buck_vtbl,
709 .type = REGULATOR_VOLTAGE,
710 .owner = THIS_MODULE,
711 .enable_reg = LP872X_GENERAL_CFG,
712 .enable_mask = LP8725_BUCK2_EN_M,
716 static int lp872x_init_dvs(struct lp872x *lp)
718 int ret, gpio;
719 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
720 enum lp872x_dvs_state pinstate;
721 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
722 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
724 if (!dvs)
725 goto set_default_dvs_mode;
727 gpio = dvs->gpio;
728 if (!gpio_is_valid(gpio)) {
729 dev_warn(lp->dev, "invalid gpio: %d\n", gpio);
730 goto set_default_dvs_mode;
733 pinstate = dvs->init_state;
734 ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
735 if (ret) {
736 dev_err(lp->dev, "gpio request err: %d\n", ret);
737 return ret;
740 lp->dvs_pin = pinstate;
741 lp->dvs_gpio = gpio;
743 return 0;
745 set_default_dvs_mode:
746 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
747 default_dvs_mode[lp->chipid]);
750 static int lp872x_config(struct lp872x *lp)
752 struct lp872x_platform_data *pdata = lp->pdata;
753 int ret;
755 if (!pdata || !pdata->update_config)
756 goto init_dvs;
758 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
759 if (ret)
760 return ret;
762 init_dvs:
763 return lp872x_init_dvs(lp);
766 static struct regulator_init_data
767 *lp872x_find_regulator_init_data(int id, struct lp872x *lp)
769 struct lp872x_platform_data *pdata = lp->pdata;
770 int i;
772 if (!pdata)
773 return NULL;
775 for (i = 0; i < lp->num_regulators; i++) {
776 if (pdata->regulator_data[i].id == id)
777 return pdata->regulator_data[i].init_data;
780 return NULL;
783 static int lp872x_regulator_register(struct lp872x *lp)
785 struct regulator_desc *desc;
786 struct regulator_config cfg = { };
787 struct regulator_dev *rdev;
788 int i, ret;
790 for (i = 0; i < lp->num_regulators; i++) {
791 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
792 &lp8725_regulator_desc[i];
794 cfg.dev = lp->dev;
795 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
796 cfg.driver_data = lp;
797 cfg.regmap = lp->regmap;
799 rdev = regulator_register(desc, &cfg);
800 if (IS_ERR(rdev)) {
801 dev_err(lp->dev, "regulator register err");
802 ret = PTR_ERR(rdev);
803 goto err;
806 *(lp->regulators + i) = rdev;
809 return 0;
810 err:
811 while (--i >= 0) {
812 rdev = *(lp->regulators + i);
813 regulator_unregister(rdev);
815 return ret;
818 static void lp872x_regulator_unregister(struct lp872x *lp)
820 struct regulator_dev *rdev;
821 int i;
823 for (i = 0; i < lp->num_regulators; i++) {
824 rdev = *(lp->regulators + i);
825 regulator_unregister(rdev);
829 static const struct regmap_config lp872x_regmap_config = {
830 .reg_bits = 8,
831 .val_bits = 8,
832 .max_register = MAX_REGISTERS,
835 #ifdef CONFIG_OF
837 #define LP872X_VALID_OPMODE (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL)
839 static struct of_regulator_match lp8720_matches[] = {
840 { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, },
841 { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, },
842 { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, },
843 { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, },
844 { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, },
845 { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, },
848 static struct of_regulator_match lp8725_matches[] = {
849 { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, },
850 { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, },
851 { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, },
852 { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, },
853 { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, },
854 { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, },
855 { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, },
856 { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, },
857 { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, },
860 static struct lp872x_platform_data
861 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
863 struct device_node *np = dev->of_node;
864 struct lp872x_platform_data *pdata;
865 struct of_regulator_match *match;
866 struct regulator_init_data *d;
867 int num_matches;
868 int count;
869 int i;
870 u8 dvs_state;
872 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
873 if (!pdata)
874 goto out;
876 of_property_read_u8(np, "ti,general-config", &pdata->general_config);
877 if (of_find_property(np, "ti,update-config", NULL))
878 pdata->update_config = true;
880 pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL);
881 if (!pdata->dvs)
882 goto out;
884 pdata->dvs->gpio = of_get_named_gpio(np, "ti,dvs-gpio", 0);
885 of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel);
886 of_property_read_u8(np, "ti,dvs-state", &dvs_state);
887 pdata->dvs->init_state = dvs_state ? DVS_HIGH : DVS_LOW;
889 if (of_get_child_count(np) == 0)
890 goto out;
892 switch (which) {
893 case LP8720:
894 match = lp8720_matches;
895 num_matches = ARRAY_SIZE(lp8720_matches);
896 break;
897 case LP8725:
898 match = lp8725_matches;
899 num_matches = ARRAY_SIZE(lp8725_matches);
900 break;
901 default:
902 goto out;
905 count = of_regulator_match(dev, np, match, num_matches);
906 if (count <= 0)
907 goto out;
909 for (i = 0; i < num_matches; i++) {
910 pdata->regulator_data[i].id =
911 (enum lp872x_regulator_id)match[i].driver_data;
912 pdata->regulator_data[i].init_data = match[i].init_data;
914 /* Operation mode configuration for buck/buck1/buck2 */
915 if (strncmp(match[i].name, "buck", 4))
916 continue;
918 d = pdata->regulator_data[i].init_data;
919 d->constraints.valid_modes_mask |= LP872X_VALID_OPMODE;
920 d->constraints.valid_ops_mask |= REGULATOR_CHANGE_MODE;
922 out:
923 return pdata;
925 #else
926 static struct lp872x_platform_data
927 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
929 return NULL;
931 #endif
933 static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
935 struct lp872x *lp;
936 int ret, size, num_regulators;
937 const int lp872x_num_regulators[] = {
938 [LP8720] = LP8720_NUM_REGULATORS,
939 [LP8725] = LP8725_NUM_REGULATORS,
942 if (cl->dev.of_node)
943 cl->dev.platform_data = lp872x_populate_pdata_from_dt(&cl->dev,
944 (enum lp872x_id)id->driver_data);
946 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
947 if (!lp)
948 goto err_mem;
950 num_regulators = lp872x_num_regulators[id->driver_data];
951 size = sizeof(struct regulator_dev *) * num_regulators;
953 lp->regulators = devm_kzalloc(&cl->dev, size, GFP_KERNEL);
954 if (!lp->regulators)
955 goto err_mem;
957 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
958 if (IS_ERR(lp->regmap)) {
959 ret = PTR_ERR(lp->regmap);
960 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
961 goto err_dev;
964 lp->dev = &cl->dev;
965 lp->pdata = dev_get_platdata(&cl->dev);
966 lp->chipid = id->driver_data;
967 lp->num_regulators = num_regulators;
968 i2c_set_clientdata(cl, lp);
970 ret = lp872x_config(lp);
971 if (ret)
972 goto err_dev;
974 return lp872x_regulator_register(lp);
976 err_mem:
977 return -ENOMEM;
978 err_dev:
979 return ret;
982 static int lp872x_remove(struct i2c_client *cl)
984 struct lp872x *lp = i2c_get_clientdata(cl);
986 lp872x_regulator_unregister(lp);
987 return 0;
990 static const struct of_device_id lp872x_dt_ids[] = {
991 { .compatible = "ti,lp8720", },
992 { .compatible = "ti,lp8725", },
995 MODULE_DEVICE_TABLE(of, lp872x_dt_ids);
997 static const struct i2c_device_id lp872x_ids[] = {
998 {"lp8720", LP8720},
999 {"lp8725", LP8725},
1002 MODULE_DEVICE_TABLE(i2c, lp872x_ids);
1004 static struct i2c_driver lp872x_driver = {
1005 .driver = {
1006 .name = "lp872x",
1007 .owner = THIS_MODULE,
1008 .of_match_table = of_match_ptr(lp872x_dt_ids),
1010 .probe = lp872x_probe,
1011 .remove = lp872x_remove,
1012 .id_table = lp872x_ids,
1015 module_i2c_driver(lp872x_driver);
1017 MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
1018 MODULE_AUTHOR("Milo Kim");
1019 MODULE_LICENSE("GPL");