1/*
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/delay.h>
19#include <linux/regulator/lp872x.h>
20#include <linux/regulator/driver.h>
21#include <linux/platform_device.h>
22#include <linux/of.h>
23#include <linux/of_gpio.h>
24#include <linux/regulator/of_regulator.h>
25
26/* Registers : LP8720/8725 shared */
27#define LP872X_GENERAL_CFG 0x00
28#define LP872X_LDO1_VOUT 0x01
29#define LP872X_LDO2_VOUT 0x02
30#define LP872X_LDO3_VOUT 0x03
31#define LP872X_LDO4_VOUT 0x04
32#define LP872X_LDO5_VOUT 0x05
33
34/* Registers : LP8720 */
35#define LP8720_BUCK_VOUT1 0x06
36#define LP8720_BUCK_VOUT2 0x07
37#define LP8720_ENABLE 0x08
38
39/* Registers : LP8725 */
40#define LP8725_LILO1_VOUT 0x06
41#define LP8725_LILO2_VOUT 0x07
42#define LP8725_BUCK1_VOUT1 0x08
43#define LP8725_BUCK1_VOUT2 0x09
44#define LP8725_BUCK2_VOUT1 0x0A
45#define LP8725_BUCK2_VOUT2 0x0B
46#define LP8725_BUCK_CTRL 0x0C
47#define LP8725_LDO_CTRL 0x0D
48
49/* Mask/shift : LP8720/LP8725 shared */
50#define LP872X_VOUT_M 0x1F
51#define LP872X_START_DELAY_M 0xE0
52#define LP872X_START_DELAY_S 5
53#define LP872X_EN_LDO1_M BIT(0)
54#define LP872X_EN_LDO2_M BIT(1)
55#define LP872X_EN_LDO3_M BIT(2)
56#define LP872X_EN_LDO4_M BIT(3)
57#define LP872X_EN_LDO5_M BIT(4)
58
59/* Mask/shift : LP8720 */
60#define LP8720_TIMESTEP_S 0 /* Addr 00h */
61#define LP8720_TIMESTEP_M BIT(0)
62#define LP8720_EXT_DVS_M BIT(2)
63#define LP8720_BUCK_FPWM_S 5 /* Addr 07h */
64#define LP8720_BUCK_FPWM_M BIT(5)
65#define LP8720_EN_BUCK_M BIT(5) /* Addr 08h */
66#define LP8720_DVS_SEL_M BIT(7)
67
68/* Mask/shift : LP8725 */
69#define LP8725_TIMESTEP_M 0xC0 /* Addr 00h */
70#define LP8725_TIMESTEP_S 6
71#define LP8725_BUCK1_EN_M BIT(0)
72#define LP8725_DVS1_M BIT(2)
73#define LP8725_DVS2_M BIT(3)
74#define LP8725_BUCK2_EN_M BIT(4)
75#define LP8725_BUCK_CL_M 0xC0 /* Addr 09h, 0Bh */
76#define LP8725_BUCK_CL_S 6
77#define LP8725_BUCK1_FPWM_S 1 /* Addr 0Ch */
78#define LP8725_BUCK1_FPWM_M BIT(1)
79#define LP8725_BUCK2_FPWM_S 5
80#define LP8725_BUCK2_FPWM_M BIT(5)
81#define LP8725_EN_LILO1_M BIT(5) /* Addr 0Dh */
82#define LP8725_EN_LILO2_M BIT(6)
83
84/* PWM mode */
85#define LP872X_FORCE_PWM 1
86#define LP872X_AUTO_PWM 0
87
88#define LP8720_NUM_REGULATORS 6
89#define LP8725_NUM_REGULATORS 9
90#define EXTERN_DVS_USED 0
91#define MAX_DELAY 6
92
93/* Default DVS Mode */
94#define LP8720_DEFAULT_DVS 0
95#define LP8725_DEFAULT_DVS BIT(2)
96
97/* dump registers in regmap-debugfs */
98#define MAX_REGISTERS 0x0F
99
100enum lp872x_id {
101 LP8720,
102 LP8725,
103};
104
105struct lp872x {
106 struct regmap *regmap;
107 struct device *dev;
108 enum lp872x_id chipid;
109 struct lp872x_platform_data *pdata;
110 int num_regulators;
111 enum lp872x_dvs_state dvs_pin;
112};
113
114/* LP8720/LP8725 shared voltage table for LDOs */
115static 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,
120};
121
122/* LP8720 LDO4 voltage table */
123static 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,
128};
129
130/* LP8725 LILO(Low Input Low Output) voltage table */
131static 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,
136};
137
138/* LP8720 BUCK voltage table */
139#define EXT_R 0 /* external resistor divider */
140static 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,
145};
146
147/* LP8725 BUCK voltage table */
148static 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,
153};
154
155/* LP8725 BUCK current limit */
156static const unsigned int lp8725_buck_uA[] = {
157 460000, 780000, 1050000, 1370000,
158};
159
160static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
161{
162 int ret;
163 unsigned int val;
164
165 ret = regmap_read(lp->regmap, addr, &val);
166 if (ret < 0) {
167 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
168 return ret;
169 }
170
171 *data = (u8)val;
172 return 0;
173}
174
175static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
176{
177 return regmap_write(lp->regmap, addr, data);
178}
179
180static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
181 unsigned int mask, u8 data)
182{
183 return regmap_update_bits(lp->regmap, addr, mask, data);
184}
185
186static int lp872x_get_timestep_usec(struct lp872x *lp)
187{
188 enum lp872x_id chip = lp->chipid;
189 u8 val, mask, shift;
190 int *time_usec, size, ret;
191 int lp8720_time_usec[] = { 25, 50 };
192 int lp8725_time_usec[] = { 32, 64, 128, 256 };
193
194 switch (chip) {
195 case LP8720:
196 mask = LP8720_TIMESTEP_M;
197 shift = LP8720_TIMESTEP_S;
198 time_usec = &lp8720_time_usec[0];
199 size = ARRAY_SIZE(lp8720_time_usec);
200 break;
201 case LP8725:
202 mask = LP8725_TIMESTEP_M;
203 shift = LP8725_TIMESTEP_S;
204 time_usec = &lp8725_time_usec[0];
205 size = ARRAY_SIZE(lp8725_time_usec);
206 break;
207 default:
208 return -EINVAL;
209 }
210
211 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
212 if (ret)
213 return ret;
214
215 val = (val & mask) >> shift;
216 if (val >= size)
217 return -EINVAL;
218
219 return *(time_usec + val);
220}
221
222static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
223{
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);
227 int ret;
228 u8 addr, val;
229
230 if (time_step_us < 0)
231 return time_step_us;
232
233 switch (rid) {
234 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
235 addr = LP872X_LDO1_VOUT + rid;
236 break;
237 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
238 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
239 break;
240 case LP8725_ID_BUCK2:
241 addr = LP8725_BUCK2_VOUT1;
242 break;
243 default:
244 return -EINVAL;
245 }
246
247 ret = lp872x_read_byte(lp, addr, &val);
248 if (ret)
249 return ret;
250
251 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
252
253 return val > MAX_DELAY ? 0 : val * time_step_us;
254}
255
256static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
257 int gpio)
258{
259 enum lp872x_dvs_state state;
260
261 state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
262 gpio_set_value(gpio, state);
263 lp->dvs_pin = state;
264}
265
266static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
267 enum lp872x_regulator_id buck)
268{
269 u8 val, addr;
270
271 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
272 return 0;
273
274 switch (buck) {
275 case LP8720_ID_BUCK:
276 if (val & LP8720_EXT_DVS_M) {
277 addr = (lp->dvs_pin == DVS_HIGH) ?
278 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
279 } else {
280 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
281 return 0;
282
283 addr = val & LP8720_DVS_SEL_M ?
284 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
285 }
286 break;
287 case LP8725_ID_BUCK1:
288 if (val & LP8725_DVS1_M)
289 addr = LP8725_BUCK1_VOUT1;
290 else
291 addr = (lp->dvs_pin == DVS_HIGH) ?
292 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
293 break;
294 case LP8725_ID_BUCK2:
295 addr = val & LP8725_DVS2_M ?
296 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
297 break;
298 default:
299 return 0;
300 }
301
302 return addr;
303}
304
305static bool lp872x_is_valid_buck_addr(u8 addr)
306{
307 switch (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:
314 return true;
315 default:
316 return false;
317 }
318}
319
320static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
321 unsigned selector)
322{
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;
327
328 if (dvs && gpio_is_valid(dvs->gpio))
329 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
330
331 addr = lp872x_select_buck_vout_addr(lp, buck);
332 if (!lp872x_is_valid_buck_addr(addr))
333 return -EINVAL;
334
335 return lp872x_update_bits(lp, addr, mask, selector);
336}
337
338static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
339{
340 struct lp872x *lp = rdev_get_drvdata(rdev);
341 enum lp872x_regulator_id buck = rdev_get_id(rdev);
342 u8 addr, val;
343 int ret;
344
345 addr = lp872x_select_buck_vout_addr(lp, buck);
346 if (!lp872x_is_valid_buck_addr(addr))
347 return -EINVAL;
348
349 ret = lp872x_read_byte(lp, addr, &val);
350 if (ret)
351 return ret;
352
353 return val & LP872X_VOUT_M;
354}
355
356static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
357{
358 struct lp872x *lp = rdev_get_drvdata(rdev);
359 enum lp872x_regulator_id buck = rdev_get_id(rdev);
360 u8 addr, mask, shift, val;
361
362 switch (buck) {
363 case LP8720_ID_BUCK:
364 addr = LP8720_BUCK_VOUT2;
365 mask = LP8720_BUCK_FPWM_M;
366 shift = LP8720_BUCK_FPWM_S;
367 break;
368 case LP8725_ID_BUCK1:
369 addr = LP8725_BUCK_CTRL;
370 mask = LP8725_BUCK1_FPWM_M;
371 shift = LP8725_BUCK1_FPWM_S;
372 break;
373 case LP8725_ID_BUCK2:
374 addr = LP8725_BUCK_CTRL;
375 mask = LP8725_BUCK2_FPWM_M;
376 shift = LP8725_BUCK2_FPWM_S;
377 break;
378 default:
379 return -EINVAL;
380 }
381
382 if (mode == REGULATOR_MODE_FAST)
383 val = LP872X_FORCE_PWM << shift;
384 else if (mode == REGULATOR_MODE_NORMAL)
385 val = LP872X_AUTO_PWM << shift;
386 else
387 return -EINVAL;
388
389 return lp872x_update_bits(lp, addr, mask, val);
390}
391
392static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
393{
394 struct lp872x *lp = rdev_get_drvdata(rdev);
395 enum lp872x_regulator_id buck = rdev_get_id(rdev);
396 u8 addr, mask, val;
397 int ret;
398
399 switch (buck) {
400 case LP8720_ID_BUCK:
401 addr = LP8720_BUCK_VOUT2;
402 mask = LP8720_BUCK_FPWM_M;
403 break;
404 case LP8725_ID_BUCK1:
405 addr = LP8725_BUCK_CTRL;
406 mask = LP8725_BUCK1_FPWM_M;
407 break;
408 case LP8725_ID_BUCK2:
409 addr = LP8725_BUCK_CTRL;
410 mask = LP8725_BUCK2_FPWM_M;
411 break;
412 default:
413 return -EINVAL;
414 }
415
416 ret = lp872x_read_byte(lp, addr, &val);
417 if (ret)
418 return ret;
419
420 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
421}
422
423static const struct regulator_ops lp872x_ldo_ops = {
424 .list_voltage = regulator_list_voltage_table,
425 .map_voltage = regulator_map_voltage_ascend,
426 .set_voltage_sel = regulator_set_voltage_sel_regmap,
427 .get_voltage_sel = regulator_get_voltage_sel_regmap,
428 .enable = regulator_enable_regmap,
429 .disable = regulator_disable_regmap,
430 .is_enabled = regulator_is_enabled_regmap,
431 .enable_time = lp872x_regulator_enable_time,
432};
433
434static const struct regulator_ops lp8720_buck_ops = {
435 .list_voltage = regulator_list_voltage_table,
436 .map_voltage = regulator_map_voltage_ascend,
437 .set_voltage_sel = lp872x_buck_set_voltage_sel,
438 .get_voltage_sel = lp872x_buck_get_voltage_sel,
439 .enable = regulator_enable_regmap,
440 .disable = regulator_disable_regmap,
441 .is_enabled = regulator_is_enabled_regmap,
442 .enable_time = lp872x_regulator_enable_time,
443 .set_mode = lp872x_buck_set_mode,
444 .get_mode = lp872x_buck_get_mode,
445};
446
447static const struct regulator_ops lp8725_buck_ops = {
448 .list_voltage = regulator_list_voltage_table,
449 .map_voltage = regulator_map_voltage_ascend,
450 .set_voltage_sel = lp872x_buck_set_voltage_sel,
451 .get_voltage_sel = lp872x_buck_get_voltage_sel,
452 .enable = regulator_enable_regmap,
453 .disable = regulator_disable_regmap,
454 .is_enabled = regulator_is_enabled_regmap,
455 .enable_time = lp872x_regulator_enable_time,
456 .set_mode = lp872x_buck_set_mode,
457 .get_mode = lp872x_buck_get_mode,
458 .set_current_limit = regulator_set_current_limit_regmap,
459 .get_current_limit = regulator_get_current_limit_regmap,
460};
461
462static const struct regulator_desc lp8720_regulator_desc[] = {
463 {
464 .name = "ldo1",
465 .of_match = of_match_ptr("ldo1"),
466 .id = LP8720_ID_LDO1,
467 .ops = &lp872x_ldo_ops,
468 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
469 .volt_table = lp872x_ldo_vtbl,
470 .type = REGULATOR_VOLTAGE,
471 .owner = THIS_MODULE,
472 .vsel_reg = LP872X_LDO1_VOUT,
473 .vsel_mask = LP872X_VOUT_M,
474 .enable_reg = LP8720_ENABLE,
475 .enable_mask = LP872X_EN_LDO1_M,
476 },
477 {
478 .name = "ldo2",
479 .of_match = of_match_ptr("ldo2"),
480 .id = LP8720_ID_LDO2,
481 .ops = &lp872x_ldo_ops,
482 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
483 .volt_table = lp872x_ldo_vtbl,
484 .type = REGULATOR_VOLTAGE,
485 .owner = THIS_MODULE,
486 .vsel_reg = LP872X_LDO2_VOUT,
487 .vsel_mask = LP872X_VOUT_M,
488 .enable_reg = LP8720_ENABLE,
489 .enable_mask = LP872X_EN_LDO2_M,
490 },
491 {
492 .name = "ldo3",
493 .of_match = of_match_ptr("ldo3"),
494 .id = LP8720_ID_LDO3,
495 .ops = &lp872x_ldo_ops,
496 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
497 .volt_table = lp872x_ldo_vtbl,
498 .type = REGULATOR_VOLTAGE,
499 .owner = THIS_MODULE,
500 .vsel_reg = LP872X_LDO3_VOUT,
501 .vsel_mask = LP872X_VOUT_M,
502 .enable_reg = LP8720_ENABLE,
503 .enable_mask = LP872X_EN_LDO3_M,
504 },
505 {
506 .name = "ldo4",
507 .of_match = of_match_ptr("ldo4"),
508 .id = LP8720_ID_LDO4,
509 .ops = &lp872x_ldo_ops,
510 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
511 .volt_table = lp8720_ldo4_vtbl,
512 .type = REGULATOR_VOLTAGE,
513 .owner = THIS_MODULE,
514 .vsel_reg = LP872X_LDO4_VOUT,
515 .vsel_mask = LP872X_VOUT_M,
516 .enable_reg = LP8720_ENABLE,
517 .enable_mask = LP872X_EN_LDO4_M,
518 },
519 {
520 .name = "ldo5",
521 .of_match = of_match_ptr("ldo5"),
522 .id = LP8720_ID_LDO5,
523 .ops = &lp872x_ldo_ops,
524 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
525 .volt_table = lp872x_ldo_vtbl,
526 .type = REGULATOR_VOLTAGE,
527 .owner = THIS_MODULE,
528 .vsel_reg = LP872X_LDO5_VOUT,
529 .vsel_mask = LP872X_VOUT_M,
530 .enable_reg = LP8720_ENABLE,
531 .enable_mask = LP872X_EN_LDO5_M,
532 },
533 {
534 .name = "buck",
535 .of_match = of_match_ptr("buck"),
536 .id = LP8720_ID_BUCK,
537 .ops = &lp8720_buck_ops,
538 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
539 .volt_table = lp8720_buck_vtbl,
540 .type = REGULATOR_VOLTAGE,
541 .owner = THIS_MODULE,
542 .enable_reg = LP8720_ENABLE,
543 .enable_mask = LP8720_EN_BUCK_M,
544 },
545};
546
547static const struct regulator_desc lp8725_regulator_desc[] = {
548 {
549 .name = "ldo1",
550 .of_match = of_match_ptr("ldo1"),
551 .id = LP8725_ID_LDO1,
552 .ops = &lp872x_ldo_ops,
553 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
554 .volt_table = lp872x_ldo_vtbl,
555 .type = REGULATOR_VOLTAGE,
556 .owner = THIS_MODULE,
557 .vsel_reg = LP872X_LDO1_VOUT,
558 .vsel_mask = LP872X_VOUT_M,
559 .enable_reg = LP8725_LDO_CTRL,
560 .enable_mask = LP872X_EN_LDO1_M,
561 },
562 {
563 .name = "ldo2",
564 .of_match = of_match_ptr("ldo2"),
565 .id = LP8725_ID_LDO2,
566 .ops = &lp872x_ldo_ops,
567 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
568 .volt_table = lp872x_ldo_vtbl,
569 .type = REGULATOR_VOLTAGE,
570 .owner = THIS_MODULE,
571 .vsel_reg = LP872X_LDO2_VOUT,
572 .vsel_mask = LP872X_VOUT_M,
573 .enable_reg = LP8725_LDO_CTRL,
574 .enable_mask = LP872X_EN_LDO2_M,
575 },
576 {
577 .name = "ldo3",
578 .of_match = of_match_ptr("ldo3"),
579 .id = LP8725_ID_LDO3,
580 .ops = &lp872x_ldo_ops,
581 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
582 .volt_table = lp872x_ldo_vtbl,
583 .type = REGULATOR_VOLTAGE,
584 .owner = THIS_MODULE,
585 .vsel_reg = LP872X_LDO3_VOUT,
586 .vsel_mask = LP872X_VOUT_M,
587 .enable_reg = LP8725_LDO_CTRL,
588 .enable_mask = LP872X_EN_LDO3_M,
589 },
590 {
591 .name = "ldo4",
592 .of_match = of_match_ptr("ldo4"),
593 .id = LP8725_ID_LDO4,
594 .ops = &lp872x_ldo_ops,
595 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
596 .volt_table = lp872x_ldo_vtbl,
597 .type = REGULATOR_VOLTAGE,
598 .owner = THIS_MODULE,
599 .vsel_reg = LP872X_LDO4_VOUT,
600 .vsel_mask = LP872X_VOUT_M,
601 .enable_reg = LP8725_LDO_CTRL,
602 .enable_mask = LP872X_EN_LDO4_M,
603 },
604 {
605 .name = "ldo5",
606 .of_match = of_match_ptr("ldo5"),
607 .id = LP8725_ID_LDO5,
608 .ops = &lp872x_ldo_ops,
609 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
610 .volt_table = lp872x_ldo_vtbl,
611 .type = REGULATOR_VOLTAGE,
612 .owner = THIS_MODULE,
613 .vsel_reg = LP872X_LDO5_VOUT,
614 .vsel_mask = LP872X_VOUT_M,
615 .enable_reg = LP8725_LDO_CTRL,
616 .enable_mask = LP872X_EN_LDO5_M,
617 },
618 {
619 .name = "lilo1",
620 .of_match = of_match_ptr("lilo1"),
621 .id = LP8725_ID_LILO1,
622 .ops = &lp872x_ldo_ops,
623 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
624 .volt_table = lp8725_lilo_vtbl,
625 .type = REGULATOR_VOLTAGE,
626 .owner = THIS_MODULE,
627 .vsel_reg = LP8725_LILO1_VOUT,
628 .vsel_mask = LP872X_VOUT_M,
629 .enable_reg = LP8725_LDO_CTRL,
630 .enable_mask = LP8725_EN_LILO1_M,
631 },
632 {
633 .name = "lilo2",
634 .of_match = of_match_ptr("lilo2"),
635 .id = LP8725_ID_LILO2,
636 .ops = &lp872x_ldo_ops,
637 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
638 .volt_table = lp8725_lilo_vtbl,
639 .type = REGULATOR_VOLTAGE,
640 .owner = THIS_MODULE,
641 .vsel_reg = LP8725_LILO2_VOUT,
642 .vsel_mask = LP872X_VOUT_M,
643 .enable_reg = LP8725_LDO_CTRL,
644 .enable_mask = LP8725_EN_LILO2_M,
645 },
646 {
647 .name = "buck1",
648 .of_match = of_match_ptr("buck1"),
649 .id = LP8725_ID_BUCK1,
650 .ops = &lp8725_buck_ops,
651 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
652 .volt_table = lp8725_buck_vtbl,
653 .type = REGULATOR_VOLTAGE,
654 .owner = THIS_MODULE,
655 .enable_reg = LP872X_GENERAL_CFG,
656 .enable_mask = LP8725_BUCK1_EN_M,
657 .curr_table = lp8725_buck_uA,
658 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
659 .csel_reg = LP8725_BUCK1_VOUT2,
660 .csel_mask = LP8725_BUCK_CL_M,
661 },
662 {
663 .name = "buck2",
664 .of_match = of_match_ptr("buck2"),
665 .id = LP8725_ID_BUCK2,
666 .ops = &lp8725_buck_ops,
667 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
668 .volt_table = lp8725_buck_vtbl,
669 .type = REGULATOR_VOLTAGE,
670 .owner = THIS_MODULE,
671 .enable_reg = LP872X_GENERAL_CFG,
672 .enable_mask = LP8725_BUCK2_EN_M,
673 .curr_table = lp8725_buck_uA,
674 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
675 .csel_reg = LP8725_BUCK2_VOUT2,
676 .csel_mask = LP8725_BUCK_CL_M,
677 },
678};
679
680static int lp872x_init_dvs(struct lp872x *lp)
681{
682 int ret, gpio;
683 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
684 enum lp872x_dvs_state pinstate;
685 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
686 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
687
688 if (!dvs)
689 goto set_default_dvs_mode;
690
691 gpio = dvs->gpio;
692 if (!gpio_is_valid(gpio))
693 goto set_default_dvs_mode;
694
695 pinstate = dvs->init_state;
696 ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
697 if (ret) {
698 dev_err(lp->dev, "gpio request err: %d\n", ret);
699 return ret;
700 }
701
702 lp->dvs_pin = pinstate;
703
704 return 0;
705
706set_default_dvs_mode:
707 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
708 default_dvs_mode[lp->chipid]);
709}
710
711static int lp872x_hw_enable(struct lp872x *lp)
712{
713 int ret, gpio;
714
715 if (!lp->pdata)
716 return -EINVAL;
717
718 gpio = lp->pdata->enable_gpio;
719 if (!gpio_is_valid(gpio))
720 return 0;
721
722 /* Always set enable GPIO high. */
723 ret = devm_gpio_request_one(lp->dev, gpio, GPIOF_OUT_INIT_HIGH, "LP872X EN");
724 if (ret) {
725 dev_err(lp->dev, "gpio request err: %d\n", ret);
726 return ret;
727 }
728
729 /* Each chip has a different enable delay. */
730 if (lp->chipid == LP8720)
731 usleep_range(LP8720_ENABLE_DELAY, 1.5 * LP8720_ENABLE_DELAY);
732 else
733 usleep_range(LP8725_ENABLE_DELAY, 1.5 * LP8725_ENABLE_DELAY);
734
735 return 0;
736}
737
738static int lp872x_config(struct lp872x *lp)
739{
740 struct lp872x_platform_data *pdata = lp->pdata;
741 int ret;
742
743 if (!pdata || !pdata->update_config)
744 goto init_dvs;
745
746 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
747 if (ret)
748 return ret;
749
750init_dvs:
751 return lp872x_init_dvs(lp);
752}
753
754static struct regulator_init_data
755*lp872x_find_regulator_init_data(int id, struct lp872x *lp)
756{
757 struct lp872x_platform_data *pdata = lp->pdata;
758 int i;
759
760 if (!pdata)
761 return NULL;
762
763 for (i = 0; i < lp->num_regulators; i++) {
764 if (pdata->regulator_data[i].id == id)
765 return pdata->regulator_data[i].init_data;
766 }
767
768 return NULL;
769}
770
771static int lp872x_regulator_register(struct lp872x *lp)
772{
773 const struct regulator_desc *desc;
774 struct regulator_config cfg = { };
775 struct regulator_dev *rdev;
776 int i;
777
778 for (i = 0; i < lp->num_regulators; i++) {
779 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
780 &lp8725_regulator_desc[i];
781
782 cfg.dev = lp->dev;
783 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
784 cfg.driver_data = lp;
785 cfg.regmap = lp->regmap;
786
787 rdev = devm_regulator_register(lp->dev, desc, &cfg);
788 if (IS_ERR(rdev)) {
789 dev_err(lp->dev, "regulator register err");
790 return PTR_ERR(rdev);
791 }
792 }
793
794 return 0;
795}
796
797static const struct regmap_config lp872x_regmap_config = {
798 .reg_bits = 8,
799 .val_bits = 8,
800 .max_register = MAX_REGISTERS,
801};
802
803#ifdef CONFIG_OF
804
805#define LP872X_VALID_OPMODE (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL)
806
807static struct of_regulator_match lp8720_matches[] = {
808 { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, },
809 { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, },
810 { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, },
811 { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, },
812 { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, },
813 { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, },
814};
815
816static struct of_regulator_match lp8725_matches[] = {
817 { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, },
818 { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, },
819 { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, },
820 { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, },
821 { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, },
822 { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, },
823 { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, },
824 { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, },
825 { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, },
826};
827
828static struct lp872x_platform_data
829*lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
830{
831 struct device_node *np = dev->of_node;
832 struct lp872x_platform_data *pdata;
833 struct of_regulator_match *match;
834 int num_matches;
835 int count;
836 int i;
837 u8 dvs_state;
838
839 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
840 if (!pdata)
841 return ERR_PTR(-ENOMEM);
842
843 of_property_read_u8(np, "ti,general-config", &pdata->general_config);
844 if (of_find_property(np, "ti,update-config", NULL))
845 pdata->update_config = true;
846
847 pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL);
848 if (!pdata->dvs)
849 return ERR_PTR(-ENOMEM);
850
851 pdata->dvs->gpio = of_get_named_gpio(np, "ti,dvs-gpio", 0);
852 of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel);
853 of_property_read_u8(np, "ti,dvs-state", &dvs_state);
854 pdata->dvs->init_state = dvs_state ? DVS_HIGH : DVS_LOW;
855
856 pdata->enable_gpio = of_get_named_gpio(np, "enable-gpios", 0);
857
858 if (of_get_child_count(np) == 0)
859 goto out;
860
861 switch (which) {
862 case LP8720:
863 match = lp8720_matches;
864 num_matches = ARRAY_SIZE(lp8720_matches);
865 break;
866 case LP8725:
867 match = lp8725_matches;
868 num_matches = ARRAY_SIZE(lp8725_matches);
869 break;
870 default:
871 goto out;
872 }
873
874 count = of_regulator_match(dev, np, match, num_matches);
875 if (count <= 0)
876 goto out;
877
878 for (i = 0; i < num_matches; i++) {
879 pdata->regulator_data[i].id =
880 (enum lp872x_regulator_id)match[i].driver_data;
881 pdata->regulator_data[i].init_data = match[i].init_data;
882 }
883out:
884 return pdata;
885}
886#else
887static struct lp872x_platform_data
888*lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
889{
890 return NULL;
891}
892#endif
893
894static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
895{
896 struct lp872x *lp;
897 struct lp872x_platform_data *pdata;
898 int ret;
899 const int lp872x_num_regulators[] = {
900 [LP8720] = LP8720_NUM_REGULATORS,
901 [LP8725] = LP8725_NUM_REGULATORS,
902 };
903
904 if (cl->dev.of_node) {
905 pdata = lp872x_populate_pdata_from_dt(&cl->dev,
906 (enum lp872x_id)id->driver_data);
907 if (IS_ERR(pdata))
908 return PTR_ERR(pdata);
909 } else {
910 pdata = dev_get_platdata(&cl->dev);
911 }
912
913 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
914 if (!lp)
915 return -ENOMEM;
916
917 lp->num_regulators = lp872x_num_regulators[id->driver_data];
918
919 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
920 if (IS_ERR(lp->regmap)) {
921 ret = PTR_ERR(lp->regmap);
922 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
923 return ret;
924 }
925
926 lp->dev = &cl->dev;
927 lp->pdata = pdata;
928 lp->chipid = id->driver_data;
929 i2c_set_clientdata(cl, lp);
930
931 ret = lp872x_hw_enable(lp);
932 if (ret)
933 return ret;
934
935 ret = lp872x_config(lp);
936 if (ret)
937 return ret;
938
939 return lp872x_regulator_register(lp);
940}
941
942static const struct of_device_id lp872x_dt_ids[] = {
943 { .compatible = "ti,lp8720", },
944 { .compatible = "ti,lp8725", },
945 { }
946};
947MODULE_DEVICE_TABLE(of, lp872x_dt_ids);
948
949static const struct i2c_device_id lp872x_ids[] = {
950 {"lp8720", LP8720},
951 {"lp8725", LP8725},
952 { }
953};
954MODULE_DEVICE_TABLE(i2c, lp872x_ids);
955
956static struct i2c_driver lp872x_driver = {
957 .driver = {
958 .name = "lp872x",
959 .of_match_table = of_match_ptr(lp872x_dt_ids),
960 },
961 .probe = lp872x_probe,
962 .id_table = lp872x_ids,
963};
964
965module_i2c_driver(lp872x_driver);
966
967MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
968MODULE_AUTHOR("Milo Kim");
969MODULE_LICENSE("GPL");
970