blob: 8e3c7ae0047f01fbe77871c79cf36e6e34e0b166 [file] [log] [blame]
Kim, Miloaf8b5fc2012-06-19 07:08:22 +00001/*
2 * Copyright 2012 Texas Instruments
3 *
4 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
5 *
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.
9 *
10 */
11
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
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
29
30/* Registers : LP8720 */
31#define LP8720_BUCK_VOUT1 0x06
32#define LP8720_BUCK_VOUT2 0x07
33#define LP8720_ENABLE 0x08
34
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
44
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)
54
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)
63
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)
79
80/* PWM mode */
81#define LP872X_FORCE_PWM 1
82#define LP872X_AUTO_PWM 0
83
84#define LP8720_NUM_REGULATORS 6
85#define LP8725_NUM_REGULATORS 9
86#define EXTERN_DVS_USED 0
87#define MAX_DELAY 6
88
Kim, Milob158fba2012-09-05 04:39:16 +000089/* Default DVS Mode */
90#define LP8720_DEFAULT_DVS 0
91#define LP8725_DEFAULT_DVS BIT(2)
92
Kim, Miloaf8b5fc2012-06-19 07:08:22 +000093/* dump registers in regmap-debugfs */
94#define MAX_REGISTERS 0x0F
95
96enum lp872x_id {
97 LP8720,
98 LP8725,
99};
100
101struct 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;
110};
111
112/* LP8720/LP8725 shared voltage table for LDOs */
113static 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,
118};
119
120/* LP8720 LDO4 voltage table */
121static 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,
126};
127
128/* LP8725 LILO(Low Input Low Output) voltage table */
129static 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,
134};
135
136/* LP8720 BUCK voltage table */
137#define EXT_R 0 /* external resistor divider */
138static 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,
143};
144
145/* LP8725 BUCK voltage table */
146static 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,
151};
152
153/* LP8725 BUCK current limit */
154static const unsigned int lp8725_buck_uA[] = {
155 460000, 780000, 1050000, 1370000,
156};
157
158static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
159{
160 int ret;
161 unsigned int val;
162
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;
167 }
168
169 *data = (u8)val;
170 return 0;
171}
172
173static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
174{
175 return regmap_write(lp->regmap, addr, data);
176}
177
178static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
179 unsigned int mask, u8 data)
180{
181 return regmap_update_bits(lp->regmap, addr, mask, data);
182}
183
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000184static int lp872x_get_timestep_usec(struct lp872x *lp)
185{
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 };
191
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;
207 }
208
209 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
210 if (ret)
211 return -EINVAL;
212
213 val = (val & mask) >> shift;
214 if (val >= size)
215 return -EINVAL;
216
217 return *(time_usec + val);
218}
219
220static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
221{
222 struct lp872x *lp = rdev_get_drvdata(rdev);
Axel Lin2c129922012-12-08 09:56:48 +0800223 enum lp872x_regulator_id rid = rdev_get_id(rdev);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000224 int time_step_us = lp872x_get_timestep_usec(lp);
Axel Lin2c129922012-12-08 09:56:48 +0800225 int ret;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000226 u8 addr, val;
227
228 if (time_step_us < 0)
229 return -EINVAL;
230
Axel Lin2c129922012-12-08 09:56:48 +0800231 switch (rid) {
232 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
233 addr = LP872X_LDO1_VOUT + rid;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000234 break;
Axel Lin2c129922012-12-08 09:56:48 +0800235 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
236 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000237 break;
238 case LP8725_ID_BUCK2:
239 addr = LP8725_BUCK2_VOUT1;
240 break;
241 default:
242 return -EINVAL;
243 }
244
245 ret = lp872x_read_byte(lp, addr, &val);
246 if (ret)
247 return ret;
248
249 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
250
251 return val > MAX_DELAY ? 0 : val * time_step_us;
252}
253
Kim, Milo9d6da6f2012-09-05 04:39:33 +0000254static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
255 int gpio)
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000256{
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000257 enum lp872x_dvs_state state;
258
259 state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
260 gpio_set_value(gpio, state);
261 lp->dvs_pin = state;
262}
263
264static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
265 enum lp872x_regulator_id buck)
266{
267 u8 val, addr;
268
269 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
270 return 0;
271
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;
280
281 addr = val & LP8720_DVS_SEL_M ?
282 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
283 }
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;
298 }
299
300 return addr;
301}
302
303static bool lp872x_is_valid_buck_addr(u8 addr)
304{
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;
315 }
316}
317
318static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
319 unsigned selector)
320{
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;
Kim, Milo9d6da6f2012-09-05 04:39:33 +0000324 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000325
326 if (dvs && gpio_is_valid(dvs->gpio))
Kim, Milo9d6da6f2012-09-05 04:39:33 +0000327 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000328
329 addr = lp872x_select_buck_vout_addr(lp, buck);
330 if (!lp872x_is_valid_buck_addr(addr))
331 return -EINVAL;
332
333 return lp872x_update_bits(lp, addr, mask, selector);
334}
335
336static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
337{
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;
342
343 addr = lp872x_select_buck_vout_addr(lp, buck);
344 if (!lp872x_is_valid_buck_addr(addr))
345 return -EINVAL;
346
347 ret = lp872x_read_byte(lp, addr, &val);
348 if (ret)
349 return ret;
350
351 return val & LP872X_VOUT_M;
352}
353
354static int lp8725_buck_set_current_limit(struct regulator_dev *rdev,
355 int min_uA, int max_uA)
356{
357 struct lp872x *lp = rdev_get_drvdata(rdev);
358 enum lp872x_regulator_id buck = rdev_get_id(rdev);
Axel Lin4e1d67e2012-08-08 20:22:35 +0800359 int i;
360 u8 addr;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000361
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;
371 }
372
Axel Lin4e1d67e2012-08-08 20:22:35 +0800373 for (i = ARRAY_SIZE(lp8725_buck_uA) - 1 ; i >= 0; i--) {
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000374 if (lp8725_buck_uA[i] >= min_uA &&
375 lp8725_buck_uA[i] <= max_uA)
Axel Lin4e1d67e2012-08-08 20:22:35 +0800376 return lp872x_update_bits(lp, addr,
377 LP8725_BUCK_CL_M,
378 i << LP8725_BUCK_CL_S);
379 }
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000380
Axel Lin4e1d67e2012-08-08 20:22:35 +0800381 return -EINVAL;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000382}
383
384static int lp8725_buck_get_current_limit(struct regulator_dev *rdev)
385{
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;
390
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;
400 }
401
402 ret = lp872x_read_byte(lp, addr, &val);
403 if (ret)
404 return ret;
405
406 val = (val & LP8725_BUCK_CL_M) >> LP8725_BUCK_CL_S;
407
408 return (val < ARRAY_SIZE(lp8725_buck_uA)) ?
409 lp8725_buck_uA[val] : -EINVAL;
410}
411
412static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
413{
414 struct lp872x *lp = rdev_get_drvdata(rdev);
415 enum lp872x_regulator_id buck = rdev_get_id(rdev);
416 u8 addr, mask, shift, val;
417
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;
436 }
437
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;
444
445 return lp872x_update_bits(lp, addr, mask, val);
446}
447
448static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
449{
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;
454
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;
470 }
471
472 ret = lp872x_read_byte(lp, addr, &val);
473 if (ret)
474 return ret;
475
476 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
477}
478
479static struct regulator_ops lp872x_ldo_ops = {
480 .list_voltage = regulator_list_voltage_table,
481 .set_voltage_sel = regulator_set_voltage_sel_regmap,
482 .get_voltage_sel = regulator_get_voltage_sel_regmap,
483 .enable = regulator_enable_regmap,
484 .disable = regulator_disable_regmap,
485 .is_enabled = regulator_is_enabled_regmap,
486 .enable_time = lp872x_regulator_enable_time,
487};
488
489static struct regulator_ops lp8720_buck_ops = {
490 .list_voltage = regulator_list_voltage_table,
491 .set_voltage_sel = lp872x_buck_set_voltage_sel,
492 .get_voltage_sel = lp872x_buck_get_voltage_sel,
493 .enable = regulator_enable_regmap,
494 .disable = regulator_disable_regmap,
495 .is_enabled = regulator_is_enabled_regmap,
496 .enable_time = lp872x_regulator_enable_time,
497 .set_mode = lp872x_buck_set_mode,
498 .get_mode = lp872x_buck_get_mode,
499};
500
501static struct regulator_ops lp8725_buck_ops = {
502 .list_voltage = regulator_list_voltage_table,
503 .set_voltage_sel = lp872x_buck_set_voltage_sel,
504 .get_voltage_sel = lp872x_buck_get_voltage_sel,
505 .enable = regulator_enable_regmap,
506 .disable = regulator_disable_regmap,
507 .is_enabled = regulator_is_enabled_regmap,
508 .enable_time = lp872x_regulator_enable_time,
509 .set_mode = lp872x_buck_set_mode,
510 .get_mode = lp872x_buck_get_mode,
511 .set_current_limit = lp8725_buck_set_current_limit,
512 .get_current_limit = lp8725_buck_get_current_limit,
513};
514
515static struct regulator_desc lp8720_regulator_desc[] = {
516 {
517 .name = "ldo1",
518 .id = LP8720_ID_LDO1,
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_LDO1_VOUT,
525 .vsel_mask = LP872X_VOUT_M,
526 .enable_reg = LP8720_ENABLE,
527 .enable_mask = LP872X_EN_LDO1_M,
528 },
529 {
530 .name = "ldo2",
531 .id = LP8720_ID_LDO2,
532 .ops = &lp872x_ldo_ops,
533 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
534 .volt_table = lp872x_ldo_vtbl,
535 .type = REGULATOR_VOLTAGE,
536 .owner = THIS_MODULE,
537 .vsel_reg = LP872X_LDO2_VOUT,
538 .vsel_mask = LP872X_VOUT_M,
539 .enable_reg = LP8720_ENABLE,
540 .enable_mask = LP872X_EN_LDO2_M,
541 },
542 {
543 .name = "ldo3",
544 .id = LP8720_ID_LDO3,
545 .ops = &lp872x_ldo_ops,
546 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
547 .volt_table = lp872x_ldo_vtbl,
548 .type = REGULATOR_VOLTAGE,
549 .owner = THIS_MODULE,
550 .vsel_reg = LP872X_LDO3_VOUT,
551 .vsel_mask = LP872X_VOUT_M,
552 .enable_reg = LP8720_ENABLE,
553 .enable_mask = LP872X_EN_LDO3_M,
554 },
555 {
556 .name = "ldo4",
557 .id = LP8720_ID_LDO4,
558 .ops = &lp872x_ldo_ops,
559 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
560 .volt_table = lp8720_ldo4_vtbl,
561 .type = REGULATOR_VOLTAGE,
562 .owner = THIS_MODULE,
563 .vsel_reg = LP872X_LDO4_VOUT,
564 .vsel_mask = LP872X_VOUT_M,
565 .enable_reg = LP8720_ENABLE,
566 .enable_mask = LP872X_EN_LDO4_M,
567 },
568 {
569 .name = "ldo5",
570 .id = LP8720_ID_LDO5,
571 .ops = &lp872x_ldo_ops,
572 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
573 .volt_table = lp872x_ldo_vtbl,
574 .type = REGULATOR_VOLTAGE,
575 .owner = THIS_MODULE,
576 .vsel_reg = LP872X_LDO5_VOUT,
577 .vsel_mask = LP872X_VOUT_M,
578 .enable_reg = LP8720_ENABLE,
579 .enable_mask = LP872X_EN_LDO5_M,
580 },
581 {
582 .name = "buck",
583 .id = LP8720_ID_BUCK,
584 .ops = &lp8720_buck_ops,
585 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
586 .volt_table = lp8720_buck_vtbl,
587 .type = REGULATOR_VOLTAGE,
588 .owner = THIS_MODULE,
589 .enable_reg = LP8720_ENABLE,
590 .enable_mask = LP8720_EN_BUCK_M,
591 },
592};
593
594static struct regulator_desc lp8725_regulator_desc[] = {
595 {
596 .name = "ldo1",
597 .id = LP8725_ID_LDO1,
598 .ops = &lp872x_ldo_ops,
599 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
600 .volt_table = lp872x_ldo_vtbl,
601 .type = REGULATOR_VOLTAGE,
602 .owner = THIS_MODULE,
603 .vsel_reg = LP872X_LDO1_VOUT,
604 .vsel_mask = LP872X_VOUT_M,
605 .enable_reg = LP8725_LDO_CTRL,
606 .enable_mask = LP872X_EN_LDO1_M,
607 },
608 {
609 .name = "ldo2",
610 .id = LP8725_ID_LDO2,
611 .ops = &lp872x_ldo_ops,
612 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
613 .volt_table = lp872x_ldo_vtbl,
614 .type = REGULATOR_VOLTAGE,
615 .owner = THIS_MODULE,
616 .vsel_reg = LP872X_LDO2_VOUT,
617 .vsel_mask = LP872X_VOUT_M,
618 .enable_reg = LP8725_LDO_CTRL,
619 .enable_mask = LP872X_EN_LDO2_M,
620 },
621 {
622 .name = "ldo3",
623 .id = LP8725_ID_LDO3,
624 .ops = &lp872x_ldo_ops,
625 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
626 .volt_table = lp872x_ldo_vtbl,
627 .type = REGULATOR_VOLTAGE,
628 .owner = THIS_MODULE,
629 .vsel_reg = LP872X_LDO3_VOUT,
630 .vsel_mask = LP872X_VOUT_M,
631 .enable_reg = LP8725_LDO_CTRL,
632 .enable_mask = LP872X_EN_LDO3_M,
633 },
634 {
635 .name = "ldo4",
636 .id = LP8725_ID_LDO4,
637 .ops = &lp872x_ldo_ops,
638 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
639 .volt_table = lp872x_ldo_vtbl,
640 .type = REGULATOR_VOLTAGE,
641 .owner = THIS_MODULE,
642 .vsel_reg = LP872X_LDO4_VOUT,
643 .vsel_mask = LP872X_VOUT_M,
644 .enable_reg = LP8725_LDO_CTRL,
645 .enable_mask = LP872X_EN_LDO4_M,
646 },
647 {
648 .name = "ldo5",
649 .id = LP8725_ID_LDO5,
650 .ops = &lp872x_ldo_ops,
651 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
652 .volt_table = lp872x_ldo_vtbl,
653 .type = REGULATOR_VOLTAGE,
654 .owner = THIS_MODULE,
655 .vsel_reg = LP872X_LDO5_VOUT,
656 .vsel_mask = LP872X_VOUT_M,
657 .enable_reg = LP8725_LDO_CTRL,
658 .enable_mask = LP872X_EN_LDO5_M,
659 },
660 {
661 .name = "lilo1",
662 .id = LP8725_ID_LILO1,
663 .ops = &lp872x_ldo_ops,
664 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
665 .volt_table = lp8725_lilo_vtbl,
666 .type = REGULATOR_VOLTAGE,
667 .owner = THIS_MODULE,
668 .vsel_reg = LP8725_LILO1_VOUT,
669 .vsel_mask = LP872X_VOUT_M,
670 .enable_reg = LP8725_LDO_CTRL,
671 .enable_mask = LP8725_EN_LILO1_M,
672 },
673 {
674 .name = "lilo2",
675 .id = LP8725_ID_LILO2,
676 .ops = &lp872x_ldo_ops,
677 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
678 .volt_table = lp8725_lilo_vtbl,
679 .type = REGULATOR_VOLTAGE,
680 .owner = THIS_MODULE,
681 .vsel_reg = LP8725_LILO2_VOUT,
682 .vsel_mask = LP872X_VOUT_M,
683 .enable_reg = LP8725_LDO_CTRL,
684 .enable_mask = LP8725_EN_LILO2_M,
685 },
686 {
687 .name = "buck1",
688 .id = LP8725_ID_BUCK1,
689 .ops = &lp8725_buck_ops,
690 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
691 .volt_table = lp8725_buck_vtbl,
692 .type = REGULATOR_VOLTAGE,
693 .owner = THIS_MODULE,
694 .enable_reg = LP872X_GENERAL_CFG,
695 .enable_mask = LP8725_BUCK1_EN_M,
696 },
697 {
698 .name = "buck2",
699 .id = LP8725_ID_BUCK2,
700 .ops = &lp8725_buck_ops,
701 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
702 .volt_table = lp8725_buck_vtbl,
703 .type = REGULATOR_VOLTAGE,
704 .owner = THIS_MODULE,
705 .enable_reg = LP872X_GENERAL_CFG,
706 .enable_mask = LP8725_BUCK2_EN_M,
707 },
708};
709
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000710static int lp872x_init_dvs(struct lp872x *lp)
711{
712 int ret, gpio;
Kim, Milob158fba2012-09-05 04:39:16 +0000713 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000714 enum lp872x_dvs_state pinstate;
Kim, Milob158fba2012-09-05 04:39:16 +0000715 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
716 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
717
718 if (!dvs)
719 goto set_default_dvs_mode;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000720
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000721 gpio = dvs->gpio;
722 if (!gpio_is_valid(gpio)) {
723 dev_err(lp->dev, "invalid gpio: %d\n", gpio);
724 return -EINVAL;
725 }
726
727 pinstate = dvs->init_state;
728 ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
729 if (ret) {
730 dev_err(lp->dev, "gpio request err: %d\n", ret);
731 return ret;
732 }
733
734 lp->dvs_pin = pinstate;
735 lp->dvs_gpio = gpio;
736
737 return 0;
Kim, Milob158fba2012-09-05 04:39:16 +0000738
739set_default_dvs_mode:
740 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
741 default_dvs_mode[lp->chipid]);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000742}
743
744static int lp872x_config(struct lp872x *lp)
745{
746 struct lp872x_platform_data *pdata = lp->pdata;
747 int ret;
748
Kim, Milo86b3fef2012-09-05 04:39:23 +0000749 if (!pdata || !pdata->update_config)
750 goto init_dvs;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000751
752 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
753 if (ret)
754 return ret;
755
Kim, Milo86b3fef2012-09-05 04:39:23 +0000756init_dvs:
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000757 return lp872x_init_dvs(lp);
758}
759
760static struct regulator_init_data
Axel Lin5bae0622012-07-15 16:57:12 +0800761*lp872x_find_regulator_init_data(int id, struct lp872x *lp)
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000762{
Kim, Milo9ffaa862012-09-05 04:39:11 +0000763 struct lp872x_platform_data *pdata = lp->pdata;
Axel Lin5bae0622012-07-15 16:57:12 +0800764 int i;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000765
Kim, Milo9ffaa862012-09-05 04:39:11 +0000766 if (!pdata)
767 return NULL;
768
Axel Lin5bae0622012-07-15 16:57:12 +0800769 for (i = 0; i < lp->num_regulators; i++) {
Kim, Milo9ffaa862012-09-05 04:39:11 +0000770 if (pdata->regulator_data[i].id == id)
771 return pdata->regulator_data[i].init_data;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000772 }
773
Axel Lin5bae0622012-07-15 16:57:12 +0800774 return NULL;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000775}
776
777static int lp872x_regulator_register(struct lp872x *lp)
778{
779 struct regulator_desc *desc;
780 struct regulator_config cfg = { };
781 struct regulator_dev *rdev;
782 int i, ret;
783
784 for (i = 0 ; i < lp->num_regulators ; i++) {
785 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
786 &lp8725_regulator_desc[i];
787
788 cfg.dev = lp->dev;
Axel Lin5bae0622012-07-15 16:57:12 +0800789 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000790 cfg.driver_data = lp;
791 cfg.regmap = lp->regmap;
792
793 rdev = regulator_register(desc, &cfg);
794 if (IS_ERR(rdev)) {
795 dev_err(lp->dev, "regulator register err");
796 ret = PTR_ERR(rdev);
797 goto err;
798 }
799
800 *(lp->regulators + i) = rdev;
801 }
802
803 return 0;
804err:
805 while (--i >= 0) {
806 rdev = *(lp->regulators + i);
807 regulator_unregister(rdev);
808 }
809 return ret;
810}
811
812static void lp872x_regulator_unregister(struct lp872x *lp)
813{
814 struct regulator_dev *rdev;
815 int i;
816
817 for (i = 0 ; i < lp->num_regulators ; i++) {
818 rdev = *(lp->regulators + i);
819 regulator_unregister(rdev);
820 }
821}
822
823static const struct regmap_config lp872x_regmap_config = {
824 .reg_bits = 8,
825 .val_bits = 8,
826 .max_register = MAX_REGISTERS,
827};
828
829static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
830{
831 struct lp872x *lp;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000832 int ret, size, num_regulators;
833 const int lp872x_num_regulators[] = {
834 [LP8720] = LP8720_NUM_REGULATORS,
835 [LP8725] = LP8725_NUM_REGULATORS,
836 };
837
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000838 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
839 if (!lp)
840 goto err_mem;
841
842 num_regulators = lp872x_num_regulators[id->driver_data];
843 size = sizeof(struct regulator_dev *) * num_regulators;
844
845 lp->regulators = devm_kzalloc(&cl->dev, size, GFP_KERNEL);
846 if (!lp->regulators)
847 goto err_mem;
848
849 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
850 if (IS_ERR(lp->regmap)) {
851 ret = PTR_ERR(lp->regmap);
852 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
853 goto err_dev;
854 }
855
856 lp->dev = &cl->dev;
Kim, Milo9ffaa862012-09-05 04:39:11 +0000857 lp->pdata = cl->dev.platform_data;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000858 lp->chipid = id->driver_data;
859 lp->num_regulators = num_regulators;
860 i2c_set_clientdata(cl, lp);
861
862 ret = lp872x_config(lp);
863 if (ret)
864 goto err_dev;
865
866 return lp872x_regulator_register(lp);
867
868err_mem:
869 return -ENOMEM;
870err_dev:
871 return ret;
872}
873
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500874static int lp872x_remove(struct i2c_client *cl)
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000875{
876 struct lp872x *lp = i2c_get_clientdata(cl);
877
878 lp872x_regulator_unregister(lp);
879 return 0;
880}
881
882static const struct i2c_device_id lp872x_ids[] = {
883 {"lp8720", LP8720},
884 {"lp8725", LP8725},
885 { }
886};
887MODULE_DEVICE_TABLE(i2c, lp872x_ids);
888
889static struct i2c_driver lp872x_driver = {
890 .driver = {
891 .name = "lp872x",
892 .owner = THIS_MODULE,
893 },
894 .probe = lp872x_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500895 .remove = lp872x_remove,
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000896 .id_table = lp872x_ids,
897};
898
899module_i2c_driver(lp872x_driver);
900
901MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
902MODULE_AUTHOR("Milo Kim");
903MODULE_LICENSE("GPL");