tracing: Add function probe to trigger a ftrace dump to console
[linux-2.6.git] / drivers / regulator / lp872x.c
blobf5fc4a142cdf3b5fd9d7ecd293a865883b1b374b
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>
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)
80 /* PWM mode */
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
87 #define MAX_DELAY 6
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
96 enum lp872x_id {
97 LP8720,
98 LP8725,
101 struct lp872x {
102 struct regmap *regmap;
103 struct device *dev;
104 enum lp872x_id chipid;
105 struct lp872x_platform_data *pdata;
106 struct regulator_dev **regulators;
107 int num_regulators;
108 enum lp872x_dvs_state dvs_pin;
109 int dvs_gpio;
112 /* LP8720/LP8725 shared voltage table for LDOs */
113 static const unsigned int lp872x_ldo_vtbl[] = {
114 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
115 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
116 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
117 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
120 /* LP8720 LDO4 voltage table */
121 static const unsigned int lp8720_ldo4_vtbl[] = {
122 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000,
123 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
124 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
125 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
128 /* LP8725 LILO(Low Input Low Output) voltage table */
129 static const unsigned int lp8725_lilo_vtbl[] = {
130 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
131 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
132 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
133 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
136 /* LP8720 BUCK voltage table */
137 #define EXT_R 0 /* external resistor divider */
138 static const unsigned int lp8720_buck_vtbl[] = {
139 EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
140 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
141 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
142 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
145 /* LP8725 BUCK voltage table */
146 static const unsigned int lp8725_buck_vtbl[] = {
147 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
148 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
149 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
150 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
153 /* LP8725 BUCK current limit */
154 static const unsigned int lp8725_buck_uA[] = {
155 460000, 780000, 1050000, 1370000,
158 static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
160 int ret;
161 unsigned int val;
163 ret = regmap_read(lp->regmap, addr, &val);
164 if (ret < 0) {
165 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
166 return ret;
169 *data = (u8)val;
170 return 0;
173 static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
175 return regmap_write(lp->regmap, addr, data);
178 static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
179 unsigned int mask, u8 data)
181 return regmap_update_bits(lp->regmap, addr, mask, data);
184 static int lp872x_get_timestep_usec(struct lp872x *lp)
186 enum lp872x_id chip = lp->chipid;
187 u8 val, mask, shift;
188 int *time_usec, size, ret;
189 int lp8720_time_usec[] = { 25, 50 };
190 int lp8725_time_usec[] = { 32, 64, 128, 256 };
192 switch (chip) {
193 case LP8720:
194 mask = LP8720_TIMESTEP_M;
195 shift = LP8720_TIMESTEP_S;
196 time_usec = &lp8720_time_usec[0];
197 size = ARRAY_SIZE(lp8720_time_usec);
198 break;
199 case LP8725:
200 mask = LP8725_TIMESTEP_M;
201 shift = LP8725_TIMESTEP_S;
202 time_usec = &lp8725_time_usec[0];
203 size = ARRAY_SIZE(lp8725_time_usec);
204 break;
205 default:
206 return -EINVAL;
209 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
210 if (ret)
211 return -EINVAL;
213 val = (val & mask) >> shift;
214 if (val >= size)
215 return -EINVAL;
217 return *(time_usec + val);
220 static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
222 struct lp872x *lp = rdev_get_drvdata(rdev);
223 enum lp872x_regulator_id rid = rdev_get_id(rdev);
224 int time_step_us = lp872x_get_timestep_usec(lp);
225 int ret;
226 u8 addr, val;
228 if (time_step_us < 0)
229 return -EINVAL;
231 switch (rid) {
232 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
233 addr = LP872X_LDO1_VOUT + rid;
234 break;
235 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
236 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
237 break;
238 case LP8725_ID_BUCK2:
239 addr = LP8725_BUCK2_VOUT1;
240 break;
241 default:
242 return -EINVAL;
245 ret = lp872x_read_byte(lp, addr, &val);
246 if (ret)
247 return ret;
249 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
251 return val > MAX_DELAY ? 0 : val * time_step_us;
254 static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
255 int gpio)
257 enum lp872x_dvs_state state;
259 state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
260 gpio_set_value(gpio, state);
261 lp->dvs_pin = state;
264 static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
265 enum lp872x_regulator_id buck)
267 u8 val, addr;
269 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
270 return 0;
272 switch (buck) {
273 case LP8720_ID_BUCK:
274 if (val & LP8720_EXT_DVS_M) {
275 addr = (lp->dvs_pin == DVS_HIGH) ?
276 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
277 } else {
278 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
279 return 0;
281 addr = val & LP8720_DVS_SEL_M ?
282 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
284 break;
285 case LP8725_ID_BUCK1:
286 if (val & LP8725_DVS1_M)
287 addr = LP8725_BUCK1_VOUT1;
288 else
289 addr = (lp->dvs_pin == DVS_HIGH) ?
290 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
291 break;
292 case LP8725_ID_BUCK2:
293 addr = val & LP8725_DVS2_M ?
294 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
295 break;
296 default:
297 return 0;
300 return addr;
303 static bool lp872x_is_valid_buck_addr(u8 addr)
305 switch (addr) {
306 case LP8720_BUCK_VOUT1:
307 case LP8720_BUCK_VOUT2:
308 case LP8725_BUCK1_VOUT1:
309 case LP8725_BUCK1_VOUT2:
310 case LP8725_BUCK2_VOUT1:
311 case LP8725_BUCK2_VOUT2:
312 return true;
313 default:
314 return false;
318 static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
319 unsigned selector)
321 struct lp872x *lp = rdev_get_drvdata(rdev);
322 enum lp872x_regulator_id buck = rdev_get_id(rdev);
323 u8 addr, mask = LP872X_VOUT_M;
324 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
326 if (dvs && gpio_is_valid(dvs->gpio))
327 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
329 addr = lp872x_select_buck_vout_addr(lp, buck);
330 if (!lp872x_is_valid_buck_addr(addr))
331 return -EINVAL;
333 return lp872x_update_bits(lp, addr, mask, selector);
336 static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
338 struct lp872x *lp = rdev_get_drvdata(rdev);
339 enum lp872x_regulator_id buck = rdev_get_id(rdev);
340 u8 addr, val;
341 int ret;
343 addr = lp872x_select_buck_vout_addr(lp, buck);
344 if (!lp872x_is_valid_buck_addr(addr))
345 return -EINVAL;
347 ret = lp872x_read_byte(lp, addr, &val);
348 if (ret)
349 return ret;
351 return val & LP872X_VOUT_M;
354 static int lp8725_buck_set_current_limit(struct regulator_dev *rdev,
355 int min_uA, int max_uA)
357 struct lp872x *lp = rdev_get_drvdata(rdev);
358 enum lp872x_regulator_id buck = rdev_get_id(rdev);
359 int i;
360 u8 addr;
362 switch (buck) {
363 case LP8725_ID_BUCK1:
364 addr = LP8725_BUCK1_VOUT2;
365 break;
366 case LP8725_ID_BUCK2:
367 addr = LP8725_BUCK2_VOUT2;
368 break;
369 default:
370 return -EINVAL;
373 for (i = ARRAY_SIZE(lp8725_buck_uA) - 1 ; i >= 0; i--) {
374 if (lp8725_buck_uA[i] >= min_uA &&
375 lp8725_buck_uA[i] <= max_uA)
376 return lp872x_update_bits(lp, addr,
377 LP8725_BUCK_CL_M,
378 i << LP8725_BUCK_CL_S);
381 return -EINVAL;
384 static int lp8725_buck_get_current_limit(struct regulator_dev *rdev)
386 struct lp872x *lp = rdev_get_drvdata(rdev);
387 enum lp872x_regulator_id buck = rdev_get_id(rdev);
388 u8 addr, val;
389 int ret;
391 switch (buck) {
392 case LP8725_ID_BUCK1:
393 addr = LP8725_BUCK1_VOUT2;
394 break;
395 case LP8725_ID_BUCK2:
396 addr = LP8725_BUCK2_VOUT2;
397 break;
398 default:
399 return -EINVAL;
402 ret = lp872x_read_byte(lp, addr, &val);
403 if (ret)
404 return ret;
406 val = (val & LP8725_BUCK_CL_M) >> LP8725_BUCK_CL_S;
408 return (val < ARRAY_SIZE(lp8725_buck_uA)) ?
409 lp8725_buck_uA[val] : -EINVAL;
412 static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
414 struct lp872x *lp = rdev_get_drvdata(rdev);
415 enum lp872x_regulator_id buck = rdev_get_id(rdev);
416 u8 addr, mask, shift, val;
418 switch (buck) {
419 case LP8720_ID_BUCK:
420 addr = LP8720_BUCK_VOUT2;
421 mask = LP8720_BUCK_FPWM_M;
422 shift = LP8720_BUCK_FPWM_S;
423 break;
424 case LP8725_ID_BUCK1:
425 addr = LP8725_BUCK_CTRL;
426 mask = LP8725_BUCK1_FPWM_M;
427 shift = LP8725_BUCK1_FPWM_S;
428 break;
429 case LP8725_ID_BUCK2:
430 addr = LP8725_BUCK_CTRL;
431 mask = LP8725_BUCK2_FPWM_M;
432 shift = LP8725_BUCK2_FPWM_S;
433 break;
434 default:
435 return -EINVAL;
438 if (mode == REGULATOR_MODE_FAST)
439 val = LP872X_FORCE_PWM << shift;
440 else if (mode == REGULATOR_MODE_NORMAL)
441 val = LP872X_AUTO_PWM << shift;
442 else
443 return -EINVAL;
445 return lp872x_update_bits(lp, addr, mask, val);
448 static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
450 struct lp872x *lp = rdev_get_drvdata(rdev);
451 enum lp872x_regulator_id buck = rdev_get_id(rdev);
452 u8 addr, mask, val;
453 int ret;
455 switch (buck) {
456 case LP8720_ID_BUCK:
457 addr = LP8720_BUCK_VOUT2;
458 mask = LP8720_BUCK_FPWM_M;
459 break;
460 case LP8725_ID_BUCK1:
461 addr = LP8725_BUCK_CTRL;
462 mask = LP8725_BUCK1_FPWM_M;
463 break;
464 case LP8725_ID_BUCK2:
465 addr = LP8725_BUCK_CTRL;
466 mask = LP8725_BUCK2_FPWM_M;
467 break;
468 default:
469 return -EINVAL;
472 ret = lp872x_read_byte(lp, addr, &val);
473 if (ret)
474 return ret;
476 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
479 static struct regulator_ops lp872x_ldo_ops = {
480 .list_voltage = regulator_list_voltage_table,
481 .map_voltage = regulator_map_voltage_ascend,
482 .set_voltage_sel = regulator_set_voltage_sel_regmap,
483 .get_voltage_sel = regulator_get_voltage_sel_regmap,
484 .enable = regulator_enable_regmap,
485 .disable = regulator_disable_regmap,
486 .is_enabled = regulator_is_enabled_regmap,
487 .enable_time = lp872x_regulator_enable_time,
490 static struct regulator_ops lp8720_buck_ops = {
491 .list_voltage = regulator_list_voltage_table,
492 .map_voltage = regulator_map_voltage_ascend,
493 .set_voltage_sel = lp872x_buck_set_voltage_sel,
494 .get_voltage_sel = lp872x_buck_get_voltage_sel,
495 .enable = regulator_enable_regmap,
496 .disable = regulator_disable_regmap,
497 .is_enabled = regulator_is_enabled_regmap,
498 .enable_time = lp872x_regulator_enable_time,
499 .set_mode = lp872x_buck_set_mode,
500 .get_mode = lp872x_buck_get_mode,
503 static struct regulator_ops lp8725_buck_ops = {
504 .list_voltage = regulator_list_voltage_table,
505 .map_voltage = regulator_map_voltage_ascend,
506 .set_voltage_sel = lp872x_buck_set_voltage_sel,
507 .get_voltage_sel = lp872x_buck_get_voltage_sel,
508 .enable = regulator_enable_regmap,
509 .disable = regulator_disable_regmap,
510 .is_enabled = regulator_is_enabled_regmap,
511 .enable_time = lp872x_regulator_enable_time,
512 .set_mode = lp872x_buck_set_mode,
513 .get_mode = lp872x_buck_get_mode,
514 .set_current_limit = lp8725_buck_set_current_limit,
515 .get_current_limit = lp8725_buck_get_current_limit,
518 static struct regulator_desc lp8720_regulator_desc[] = {
520 .name = "ldo1",
521 .id = LP8720_ID_LDO1,
522 .ops = &lp872x_ldo_ops,
523 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
524 .volt_table = lp872x_ldo_vtbl,
525 .type = REGULATOR_VOLTAGE,
526 .owner = THIS_MODULE,
527 .vsel_reg = LP872X_LDO1_VOUT,
528 .vsel_mask = LP872X_VOUT_M,
529 .enable_reg = LP8720_ENABLE,
530 .enable_mask = LP872X_EN_LDO1_M,
533 .name = "ldo2",
534 .id = LP8720_ID_LDO2,
535 .ops = &lp872x_ldo_ops,
536 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
537 .volt_table = lp872x_ldo_vtbl,
538 .type = REGULATOR_VOLTAGE,
539 .owner = THIS_MODULE,
540 .vsel_reg = LP872X_LDO2_VOUT,
541 .vsel_mask = LP872X_VOUT_M,
542 .enable_reg = LP8720_ENABLE,
543 .enable_mask = LP872X_EN_LDO2_M,
546 .name = "ldo3",
547 .id = LP8720_ID_LDO3,
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_LDO3_VOUT,
554 .vsel_mask = LP872X_VOUT_M,
555 .enable_reg = LP8720_ENABLE,
556 .enable_mask = LP872X_EN_LDO3_M,
559 .name = "ldo4",
560 .id = LP8720_ID_LDO4,
561 .ops = &lp872x_ldo_ops,
562 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
563 .volt_table = lp8720_ldo4_vtbl,
564 .type = REGULATOR_VOLTAGE,
565 .owner = THIS_MODULE,
566 .vsel_reg = LP872X_LDO4_VOUT,
567 .vsel_mask = LP872X_VOUT_M,
568 .enable_reg = LP8720_ENABLE,
569 .enable_mask = LP872X_EN_LDO4_M,
572 .name = "ldo5",
573 .id = LP8720_ID_LDO5,
574 .ops = &lp872x_ldo_ops,
575 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
576 .volt_table = lp872x_ldo_vtbl,
577 .type = REGULATOR_VOLTAGE,
578 .owner = THIS_MODULE,
579 .vsel_reg = LP872X_LDO5_VOUT,
580 .vsel_mask = LP872X_VOUT_M,
581 .enable_reg = LP8720_ENABLE,
582 .enable_mask = LP872X_EN_LDO5_M,
585 .name = "buck",
586 .id = LP8720_ID_BUCK,
587 .ops = &lp8720_buck_ops,
588 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
589 .volt_table = lp8720_buck_vtbl,
590 .type = REGULATOR_VOLTAGE,
591 .owner = THIS_MODULE,
592 .enable_reg = LP8720_ENABLE,
593 .enable_mask = LP8720_EN_BUCK_M,
597 static struct regulator_desc lp8725_regulator_desc[] = {
599 .name = "ldo1",
600 .id = LP8725_ID_LDO1,
601 .ops = &lp872x_ldo_ops,
602 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
603 .volt_table = lp872x_ldo_vtbl,
604 .type = REGULATOR_VOLTAGE,
605 .owner = THIS_MODULE,
606 .vsel_reg = LP872X_LDO1_VOUT,
607 .vsel_mask = LP872X_VOUT_M,
608 .enable_reg = LP8725_LDO_CTRL,
609 .enable_mask = LP872X_EN_LDO1_M,
612 .name = "ldo2",
613 .id = LP8725_ID_LDO2,
614 .ops = &lp872x_ldo_ops,
615 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
616 .volt_table = lp872x_ldo_vtbl,
617 .type = REGULATOR_VOLTAGE,
618 .owner = THIS_MODULE,
619 .vsel_reg = LP872X_LDO2_VOUT,
620 .vsel_mask = LP872X_VOUT_M,
621 .enable_reg = LP8725_LDO_CTRL,
622 .enable_mask = LP872X_EN_LDO2_M,
625 .name = "ldo3",
626 .id = LP8725_ID_LDO3,
627 .ops = &lp872x_ldo_ops,
628 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
629 .volt_table = lp872x_ldo_vtbl,
630 .type = REGULATOR_VOLTAGE,
631 .owner = THIS_MODULE,
632 .vsel_reg = LP872X_LDO3_VOUT,
633 .vsel_mask = LP872X_VOUT_M,
634 .enable_reg = LP8725_LDO_CTRL,
635 .enable_mask = LP872X_EN_LDO3_M,
638 .name = "ldo4",
639 .id = LP8725_ID_LDO4,
640 .ops = &lp872x_ldo_ops,
641 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
642 .volt_table = lp872x_ldo_vtbl,
643 .type = REGULATOR_VOLTAGE,
644 .owner = THIS_MODULE,
645 .vsel_reg = LP872X_LDO4_VOUT,
646 .vsel_mask = LP872X_VOUT_M,
647 .enable_reg = LP8725_LDO_CTRL,
648 .enable_mask = LP872X_EN_LDO4_M,
651 .name = "ldo5",
652 .id = LP8725_ID_LDO5,
653 .ops = &lp872x_ldo_ops,
654 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
655 .volt_table = lp872x_ldo_vtbl,
656 .type = REGULATOR_VOLTAGE,
657 .owner = THIS_MODULE,
658 .vsel_reg = LP872X_LDO5_VOUT,
659 .vsel_mask = LP872X_VOUT_M,
660 .enable_reg = LP8725_LDO_CTRL,
661 .enable_mask = LP872X_EN_LDO5_M,
664 .name = "lilo1",
665 .id = LP8725_ID_LILO1,
666 .ops = &lp872x_ldo_ops,
667 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
668 .volt_table = lp8725_lilo_vtbl,
669 .type = REGULATOR_VOLTAGE,
670 .owner = THIS_MODULE,
671 .vsel_reg = LP8725_LILO1_VOUT,
672 .vsel_mask = LP872X_VOUT_M,
673 .enable_reg = LP8725_LDO_CTRL,
674 .enable_mask = LP8725_EN_LILO1_M,
677 .name = "lilo2",
678 .id = LP8725_ID_LILO2,
679 .ops = &lp872x_ldo_ops,
680 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
681 .volt_table = lp8725_lilo_vtbl,
682 .type = REGULATOR_VOLTAGE,
683 .owner = THIS_MODULE,
684 .vsel_reg = LP8725_LILO2_VOUT,
685 .vsel_mask = LP872X_VOUT_M,
686 .enable_reg = LP8725_LDO_CTRL,
687 .enable_mask = LP8725_EN_LILO2_M,
690 .name = "buck1",
691 .id = LP8725_ID_BUCK1,
692 .ops = &lp8725_buck_ops,
693 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
694 .volt_table = lp8725_buck_vtbl,
695 .type = REGULATOR_VOLTAGE,
696 .owner = THIS_MODULE,
697 .enable_reg = LP872X_GENERAL_CFG,
698 .enable_mask = LP8725_BUCK1_EN_M,
701 .name = "buck2",
702 .id = LP8725_ID_BUCK2,
703 .ops = &lp8725_buck_ops,
704 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
705 .volt_table = lp8725_buck_vtbl,
706 .type = REGULATOR_VOLTAGE,
707 .owner = THIS_MODULE,
708 .enable_reg = LP872X_GENERAL_CFG,
709 .enable_mask = LP8725_BUCK2_EN_M,
713 static int lp872x_init_dvs(struct lp872x *lp)
715 int ret, gpio;
716 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
717 enum lp872x_dvs_state pinstate;
718 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
719 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
721 if (!dvs)
722 goto set_default_dvs_mode;
724 gpio = dvs->gpio;
725 if (!gpio_is_valid(gpio)) {
726 dev_err(lp->dev, "invalid gpio: %d\n", gpio);
727 return -EINVAL;
730 pinstate = dvs->init_state;
731 ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
732 if (ret) {
733 dev_err(lp->dev, "gpio request err: %d\n", ret);
734 return ret;
737 lp->dvs_pin = pinstate;
738 lp->dvs_gpio = gpio;
740 return 0;
742 set_default_dvs_mode:
743 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
744 default_dvs_mode[lp->chipid]);
747 static int lp872x_config(struct lp872x *lp)
749 struct lp872x_platform_data *pdata = lp->pdata;
750 int ret;
752 if (!pdata || !pdata->update_config)
753 goto init_dvs;
755 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
756 if (ret)
757 return ret;
759 init_dvs:
760 return lp872x_init_dvs(lp);
763 static struct regulator_init_data
764 *lp872x_find_regulator_init_data(int id, struct lp872x *lp)
766 struct lp872x_platform_data *pdata = lp->pdata;
767 int i;
769 if (!pdata)
770 return NULL;
772 for (i = 0; i < lp->num_regulators; i++) {
773 if (pdata->regulator_data[i].id == id)
774 return pdata->regulator_data[i].init_data;
777 return NULL;
780 static int lp872x_regulator_register(struct lp872x *lp)
782 struct regulator_desc *desc;
783 struct regulator_config cfg = { };
784 struct regulator_dev *rdev;
785 int i, ret;
787 for (i = 0 ; i < lp->num_regulators ; i++) {
788 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
789 &lp8725_regulator_desc[i];
791 cfg.dev = lp->dev;
792 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
793 cfg.driver_data = lp;
794 cfg.regmap = lp->regmap;
796 rdev = regulator_register(desc, &cfg);
797 if (IS_ERR(rdev)) {
798 dev_err(lp->dev, "regulator register err");
799 ret = PTR_ERR(rdev);
800 goto err;
803 *(lp->regulators + i) = rdev;
806 return 0;
807 err:
808 while (--i >= 0) {
809 rdev = *(lp->regulators + i);
810 regulator_unregister(rdev);
812 return ret;
815 static void lp872x_regulator_unregister(struct lp872x *lp)
817 struct regulator_dev *rdev;
818 int i;
820 for (i = 0 ; i < lp->num_regulators ; i++) {
821 rdev = *(lp->regulators + i);
822 regulator_unregister(rdev);
826 static const struct regmap_config lp872x_regmap_config = {
827 .reg_bits = 8,
828 .val_bits = 8,
829 .max_register = MAX_REGISTERS,
832 static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
834 struct lp872x *lp;
835 int ret, size, num_regulators;
836 const int lp872x_num_regulators[] = {
837 [LP8720] = LP8720_NUM_REGULATORS,
838 [LP8725] = LP8725_NUM_REGULATORS,
841 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
842 if (!lp)
843 goto err_mem;
845 num_regulators = lp872x_num_regulators[id->driver_data];
846 size = sizeof(struct regulator_dev *) * num_regulators;
848 lp->regulators = devm_kzalloc(&cl->dev, size, GFP_KERNEL);
849 if (!lp->regulators)
850 goto err_mem;
852 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
853 if (IS_ERR(lp->regmap)) {
854 ret = PTR_ERR(lp->regmap);
855 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
856 goto err_dev;
859 lp->dev = &cl->dev;
860 lp->pdata = cl->dev.platform_data;
861 lp->chipid = id->driver_data;
862 lp->num_regulators = num_regulators;
863 i2c_set_clientdata(cl, lp);
865 ret = lp872x_config(lp);
866 if (ret)
867 goto err_dev;
869 return lp872x_regulator_register(lp);
871 err_mem:
872 return -ENOMEM;
873 err_dev:
874 return ret;
877 static int lp872x_remove(struct i2c_client *cl)
879 struct lp872x *lp = i2c_get_clientdata(cl);
881 lp872x_regulator_unregister(lp);
882 return 0;
885 static const struct i2c_device_id lp872x_ids[] = {
886 {"lp8720", LP8720},
887 {"lp8725", LP8725},
890 MODULE_DEVICE_TABLE(i2c, lp872x_ids);
892 static struct i2c_driver lp872x_driver = {
893 .driver = {
894 .name = "lp872x",
895 .owner = THIS_MODULE,
897 .probe = lp872x_probe,
898 .remove = lp872x_remove,
899 .id_table = lp872x_ids,
902 module_i2c_driver(lp872x_driver);
904 MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
905 MODULE_AUTHOR("Milo Kim");
906 MODULE_LICENSE("GPL");