media: stv06xx: add missing descriptor sanity checks
[linux/fpc-iii.git] / drivers / regulator / lp872x.c
blob303d7e2dc8384afe7003a06c1491c6034f3467dc
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2012 Texas Instruments
5 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
6 */
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/i2c.h>
11 #include <linux/regmap.h>
12 #include <linux/err.h>
13 #include <linux/gpio.h>
14 #include <linux/delay.h>
15 #include <linux/regulator/lp872x.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/platform_device.h>
18 #include <linux/of.h>
19 #include <linux/of_gpio.h>
20 #include <linux/regulator/of_regulator.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 int num_regulators;
107 enum lp872x_dvs_state dvs_pin;
110 /* LP8720/LP8725 shared voltage table for LDOs */
111 static const unsigned int lp872x_ldo_vtbl[] = {
112 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
113 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
114 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
115 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
118 /* LP8720 LDO4 voltage table */
119 static const unsigned int lp8720_ldo4_vtbl[] = {
120 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000,
121 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
122 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
123 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
126 /* LP8725 LILO(Low Input Low Output) voltage table */
127 static const unsigned int lp8725_lilo_vtbl[] = {
128 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
129 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
130 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
131 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
134 /* LP8720 BUCK voltage table */
135 #define EXT_R 0 /* external resistor divider */
136 static const unsigned int lp8720_buck_vtbl[] = {
137 EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
138 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
139 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
140 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
143 /* LP8725 BUCK voltage table */
144 static const unsigned int lp8725_buck_vtbl[] = {
145 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
146 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
147 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
148 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
151 /* LP8725 BUCK current limit */
152 static const unsigned int lp8725_buck_uA[] = {
153 460000, 780000, 1050000, 1370000,
156 static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
158 int ret;
159 unsigned int val;
161 ret = regmap_read(lp->regmap, addr, &val);
162 if (ret < 0) {
163 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
164 return ret;
167 *data = (u8)val;
168 return 0;
171 static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
173 return regmap_write(lp->regmap, addr, data);
176 static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
177 unsigned int mask, u8 data)
179 return regmap_update_bits(lp->regmap, addr, mask, data);
182 static int lp872x_get_timestep_usec(struct lp872x *lp)
184 enum lp872x_id chip = lp->chipid;
185 u8 val, mask, shift;
186 int *time_usec, size, ret;
187 int lp8720_time_usec[] = { 25, 50 };
188 int lp8725_time_usec[] = { 32, 64, 128, 256 };
190 switch (chip) {
191 case LP8720:
192 mask = LP8720_TIMESTEP_M;
193 shift = LP8720_TIMESTEP_S;
194 time_usec = &lp8720_time_usec[0];
195 size = ARRAY_SIZE(lp8720_time_usec);
196 break;
197 case LP8725:
198 mask = LP8725_TIMESTEP_M;
199 shift = LP8725_TIMESTEP_S;
200 time_usec = &lp8725_time_usec[0];
201 size = ARRAY_SIZE(lp8725_time_usec);
202 break;
203 default:
204 return -EINVAL;
207 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
208 if (ret)
209 return ret;
211 val = (val & mask) >> shift;
212 if (val >= size)
213 return -EINVAL;
215 return *(time_usec + val);
218 static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
220 struct lp872x *lp = rdev_get_drvdata(rdev);
221 enum lp872x_regulator_id rid = rdev_get_id(rdev);
222 int time_step_us = lp872x_get_timestep_usec(lp);
223 int ret;
224 u8 addr, val;
226 if (time_step_us < 0)
227 return time_step_us;
229 switch (rid) {
230 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
231 addr = LP872X_LDO1_VOUT + rid;
232 break;
233 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
234 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
235 break;
236 case LP8725_ID_BUCK2:
237 addr = LP8725_BUCK2_VOUT1;
238 break;
239 default:
240 return -EINVAL;
243 ret = lp872x_read_byte(lp, addr, &val);
244 if (ret)
245 return ret;
247 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
249 return val > MAX_DELAY ? 0 : val * time_step_us;
252 static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
253 int gpio)
255 enum lp872x_dvs_state state;
257 state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
258 gpio_set_value(gpio, state);
259 lp->dvs_pin = state;
262 static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
263 enum lp872x_regulator_id buck)
265 u8 val, addr;
267 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
268 return 0;
270 switch (buck) {
271 case LP8720_ID_BUCK:
272 if (val & LP8720_EXT_DVS_M) {
273 addr = (lp->dvs_pin == DVS_HIGH) ?
274 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
275 } else {
276 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
277 return 0;
279 addr = val & LP8720_DVS_SEL_M ?
280 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
282 break;
283 case LP8725_ID_BUCK1:
284 if (val & LP8725_DVS1_M)
285 addr = LP8725_BUCK1_VOUT1;
286 else
287 addr = (lp->dvs_pin == DVS_HIGH) ?
288 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
289 break;
290 case LP8725_ID_BUCK2:
291 addr = val & LP8725_DVS2_M ?
292 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
293 break;
294 default:
295 return 0;
298 return addr;
301 static bool lp872x_is_valid_buck_addr(u8 addr)
303 switch (addr) {
304 case LP8720_BUCK_VOUT1:
305 case LP8720_BUCK_VOUT2:
306 case LP8725_BUCK1_VOUT1:
307 case LP8725_BUCK1_VOUT2:
308 case LP8725_BUCK2_VOUT1:
309 case LP8725_BUCK2_VOUT2:
310 return true;
311 default:
312 return false;
316 static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
317 unsigned selector)
319 struct lp872x *lp = rdev_get_drvdata(rdev);
320 enum lp872x_regulator_id buck = rdev_get_id(rdev);
321 u8 addr, mask = LP872X_VOUT_M;
322 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
324 if (dvs && gpio_is_valid(dvs->gpio))
325 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
327 addr = lp872x_select_buck_vout_addr(lp, buck);
328 if (!lp872x_is_valid_buck_addr(addr))
329 return -EINVAL;
331 return lp872x_update_bits(lp, addr, mask, selector);
334 static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
336 struct lp872x *lp = rdev_get_drvdata(rdev);
337 enum lp872x_regulator_id buck = rdev_get_id(rdev);
338 u8 addr, val;
339 int ret;
341 addr = lp872x_select_buck_vout_addr(lp, buck);
342 if (!lp872x_is_valid_buck_addr(addr))
343 return -EINVAL;
345 ret = lp872x_read_byte(lp, addr, &val);
346 if (ret)
347 return ret;
349 return val & LP872X_VOUT_M;
352 static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
354 struct lp872x *lp = rdev_get_drvdata(rdev);
355 enum lp872x_regulator_id buck = rdev_get_id(rdev);
356 u8 addr, mask, shift, val;
358 switch (buck) {
359 case LP8720_ID_BUCK:
360 addr = LP8720_BUCK_VOUT2;
361 mask = LP8720_BUCK_FPWM_M;
362 shift = LP8720_BUCK_FPWM_S;
363 break;
364 case LP8725_ID_BUCK1:
365 addr = LP8725_BUCK_CTRL;
366 mask = LP8725_BUCK1_FPWM_M;
367 shift = LP8725_BUCK1_FPWM_S;
368 break;
369 case LP8725_ID_BUCK2:
370 addr = LP8725_BUCK_CTRL;
371 mask = LP8725_BUCK2_FPWM_M;
372 shift = LP8725_BUCK2_FPWM_S;
373 break;
374 default:
375 return -EINVAL;
378 if (mode == REGULATOR_MODE_FAST)
379 val = LP872X_FORCE_PWM << shift;
380 else if (mode == REGULATOR_MODE_NORMAL)
381 val = LP872X_AUTO_PWM << shift;
382 else
383 return -EINVAL;
385 return lp872x_update_bits(lp, addr, mask, val);
388 static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
390 struct lp872x *lp = rdev_get_drvdata(rdev);
391 enum lp872x_regulator_id buck = rdev_get_id(rdev);
392 u8 addr, mask, val;
393 int ret;
395 switch (buck) {
396 case LP8720_ID_BUCK:
397 addr = LP8720_BUCK_VOUT2;
398 mask = LP8720_BUCK_FPWM_M;
399 break;
400 case LP8725_ID_BUCK1:
401 addr = LP8725_BUCK_CTRL;
402 mask = LP8725_BUCK1_FPWM_M;
403 break;
404 case LP8725_ID_BUCK2:
405 addr = LP8725_BUCK_CTRL;
406 mask = LP8725_BUCK2_FPWM_M;
407 break;
408 default:
409 return -EINVAL;
412 ret = lp872x_read_byte(lp, addr, &val);
413 if (ret)
414 return ret;
416 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
419 static const struct regulator_ops lp872x_ldo_ops = {
420 .list_voltage = regulator_list_voltage_table,
421 .map_voltage = regulator_map_voltage_ascend,
422 .set_voltage_sel = regulator_set_voltage_sel_regmap,
423 .get_voltage_sel = regulator_get_voltage_sel_regmap,
424 .enable = regulator_enable_regmap,
425 .disable = regulator_disable_regmap,
426 .is_enabled = regulator_is_enabled_regmap,
427 .enable_time = lp872x_regulator_enable_time,
430 static const struct regulator_ops lp8720_buck_ops = {
431 .list_voltage = regulator_list_voltage_table,
432 .map_voltage = regulator_map_voltage_ascend,
433 .set_voltage_sel = lp872x_buck_set_voltage_sel,
434 .get_voltage_sel = lp872x_buck_get_voltage_sel,
435 .enable = regulator_enable_regmap,
436 .disable = regulator_disable_regmap,
437 .is_enabled = regulator_is_enabled_regmap,
438 .enable_time = lp872x_regulator_enable_time,
439 .set_mode = lp872x_buck_set_mode,
440 .get_mode = lp872x_buck_get_mode,
443 static const struct regulator_ops lp8725_buck_ops = {
444 .list_voltage = regulator_list_voltage_table,
445 .map_voltage = regulator_map_voltage_ascend,
446 .set_voltage_sel = lp872x_buck_set_voltage_sel,
447 .get_voltage_sel = lp872x_buck_get_voltage_sel,
448 .enable = regulator_enable_regmap,
449 .disable = regulator_disable_regmap,
450 .is_enabled = regulator_is_enabled_regmap,
451 .enable_time = lp872x_regulator_enable_time,
452 .set_mode = lp872x_buck_set_mode,
453 .get_mode = lp872x_buck_get_mode,
454 .set_current_limit = regulator_set_current_limit_regmap,
455 .get_current_limit = regulator_get_current_limit_regmap,
458 static const struct regulator_desc lp8720_regulator_desc[] = {
460 .name = "ldo1",
461 .of_match = of_match_ptr("ldo1"),
462 .id = LP8720_ID_LDO1,
463 .ops = &lp872x_ldo_ops,
464 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
465 .volt_table = lp872x_ldo_vtbl,
466 .type = REGULATOR_VOLTAGE,
467 .owner = THIS_MODULE,
468 .vsel_reg = LP872X_LDO1_VOUT,
469 .vsel_mask = LP872X_VOUT_M,
470 .enable_reg = LP8720_ENABLE,
471 .enable_mask = LP872X_EN_LDO1_M,
474 .name = "ldo2",
475 .of_match = of_match_ptr("ldo2"),
476 .id = LP8720_ID_LDO2,
477 .ops = &lp872x_ldo_ops,
478 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
479 .volt_table = lp872x_ldo_vtbl,
480 .type = REGULATOR_VOLTAGE,
481 .owner = THIS_MODULE,
482 .vsel_reg = LP872X_LDO2_VOUT,
483 .vsel_mask = LP872X_VOUT_M,
484 .enable_reg = LP8720_ENABLE,
485 .enable_mask = LP872X_EN_LDO2_M,
488 .name = "ldo3",
489 .of_match = of_match_ptr("ldo3"),
490 .id = LP8720_ID_LDO3,
491 .ops = &lp872x_ldo_ops,
492 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
493 .volt_table = lp872x_ldo_vtbl,
494 .type = REGULATOR_VOLTAGE,
495 .owner = THIS_MODULE,
496 .vsel_reg = LP872X_LDO3_VOUT,
497 .vsel_mask = LP872X_VOUT_M,
498 .enable_reg = LP8720_ENABLE,
499 .enable_mask = LP872X_EN_LDO3_M,
502 .name = "ldo4",
503 .of_match = of_match_ptr("ldo4"),
504 .id = LP8720_ID_LDO4,
505 .ops = &lp872x_ldo_ops,
506 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
507 .volt_table = lp8720_ldo4_vtbl,
508 .type = REGULATOR_VOLTAGE,
509 .owner = THIS_MODULE,
510 .vsel_reg = LP872X_LDO4_VOUT,
511 .vsel_mask = LP872X_VOUT_M,
512 .enable_reg = LP8720_ENABLE,
513 .enable_mask = LP872X_EN_LDO4_M,
516 .name = "ldo5",
517 .of_match = of_match_ptr("ldo5"),
518 .id = LP8720_ID_LDO5,
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_LDO5_VOUT,
525 .vsel_mask = LP872X_VOUT_M,
526 .enable_reg = LP8720_ENABLE,
527 .enable_mask = LP872X_EN_LDO5_M,
530 .name = "buck",
531 .of_match = of_match_ptr("buck"),
532 .id = LP8720_ID_BUCK,
533 .ops = &lp8720_buck_ops,
534 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
535 .volt_table = lp8720_buck_vtbl,
536 .type = REGULATOR_VOLTAGE,
537 .owner = THIS_MODULE,
538 .enable_reg = LP8720_ENABLE,
539 .enable_mask = LP8720_EN_BUCK_M,
543 static const struct regulator_desc lp8725_regulator_desc[] = {
545 .name = "ldo1",
546 .of_match = of_match_ptr("ldo1"),
547 .id = LP8725_ID_LDO1,
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_LDO1_VOUT,
554 .vsel_mask = LP872X_VOUT_M,
555 .enable_reg = LP8725_LDO_CTRL,
556 .enable_mask = LP872X_EN_LDO1_M,
559 .name = "ldo2",
560 .of_match = of_match_ptr("ldo2"),
561 .id = LP8725_ID_LDO2,
562 .ops = &lp872x_ldo_ops,
563 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
564 .volt_table = lp872x_ldo_vtbl,
565 .type = REGULATOR_VOLTAGE,
566 .owner = THIS_MODULE,
567 .vsel_reg = LP872X_LDO2_VOUT,
568 .vsel_mask = LP872X_VOUT_M,
569 .enable_reg = LP8725_LDO_CTRL,
570 .enable_mask = LP872X_EN_LDO2_M,
573 .name = "ldo3",
574 .of_match = of_match_ptr("ldo3"),
575 .id = LP8725_ID_LDO3,
576 .ops = &lp872x_ldo_ops,
577 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
578 .volt_table = lp872x_ldo_vtbl,
579 .type = REGULATOR_VOLTAGE,
580 .owner = THIS_MODULE,
581 .vsel_reg = LP872X_LDO3_VOUT,
582 .vsel_mask = LP872X_VOUT_M,
583 .enable_reg = LP8725_LDO_CTRL,
584 .enable_mask = LP872X_EN_LDO3_M,
587 .name = "ldo4",
588 .of_match = of_match_ptr("ldo4"),
589 .id = LP8725_ID_LDO4,
590 .ops = &lp872x_ldo_ops,
591 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
592 .volt_table = lp872x_ldo_vtbl,
593 .type = REGULATOR_VOLTAGE,
594 .owner = THIS_MODULE,
595 .vsel_reg = LP872X_LDO4_VOUT,
596 .vsel_mask = LP872X_VOUT_M,
597 .enable_reg = LP8725_LDO_CTRL,
598 .enable_mask = LP872X_EN_LDO4_M,
601 .name = "ldo5",
602 .of_match = of_match_ptr("ldo5"),
603 .id = LP8725_ID_LDO5,
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_LDO5_VOUT,
610 .vsel_mask = LP872X_VOUT_M,
611 .enable_reg = LP8725_LDO_CTRL,
612 .enable_mask = LP872X_EN_LDO5_M,
615 .name = "lilo1",
616 .of_match = of_match_ptr("lilo1"),
617 .id = LP8725_ID_LILO1,
618 .ops = &lp872x_ldo_ops,
619 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
620 .volt_table = lp8725_lilo_vtbl,
621 .type = REGULATOR_VOLTAGE,
622 .owner = THIS_MODULE,
623 .vsel_reg = LP8725_LILO1_VOUT,
624 .vsel_mask = LP872X_VOUT_M,
625 .enable_reg = LP8725_LDO_CTRL,
626 .enable_mask = LP8725_EN_LILO1_M,
629 .name = "lilo2",
630 .of_match = of_match_ptr("lilo2"),
631 .id = LP8725_ID_LILO2,
632 .ops = &lp872x_ldo_ops,
633 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
634 .volt_table = lp8725_lilo_vtbl,
635 .type = REGULATOR_VOLTAGE,
636 .owner = THIS_MODULE,
637 .vsel_reg = LP8725_LILO2_VOUT,
638 .vsel_mask = LP872X_VOUT_M,
639 .enable_reg = LP8725_LDO_CTRL,
640 .enable_mask = LP8725_EN_LILO2_M,
643 .name = "buck1",
644 .of_match = of_match_ptr("buck1"),
645 .id = LP8725_ID_BUCK1,
646 .ops = &lp8725_buck_ops,
647 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
648 .volt_table = lp8725_buck_vtbl,
649 .type = REGULATOR_VOLTAGE,
650 .owner = THIS_MODULE,
651 .enable_reg = LP872X_GENERAL_CFG,
652 .enable_mask = LP8725_BUCK1_EN_M,
653 .curr_table = lp8725_buck_uA,
654 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
655 .csel_reg = LP8725_BUCK1_VOUT2,
656 .csel_mask = LP8725_BUCK_CL_M,
659 .name = "buck2",
660 .of_match = of_match_ptr("buck2"),
661 .id = LP8725_ID_BUCK2,
662 .ops = &lp8725_buck_ops,
663 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
664 .volt_table = lp8725_buck_vtbl,
665 .type = REGULATOR_VOLTAGE,
666 .owner = THIS_MODULE,
667 .enable_reg = LP872X_GENERAL_CFG,
668 .enable_mask = LP8725_BUCK2_EN_M,
669 .curr_table = lp8725_buck_uA,
670 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
671 .csel_reg = LP8725_BUCK2_VOUT2,
672 .csel_mask = LP8725_BUCK_CL_M,
676 static int lp872x_init_dvs(struct lp872x *lp)
678 int ret, gpio;
679 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
680 enum lp872x_dvs_state pinstate;
681 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
682 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
684 if (!dvs)
685 goto set_default_dvs_mode;
687 gpio = dvs->gpio;
688 if (!gpio_is_valid(gpio))
689 goto set_default_dvs_mode;
691 pinstate = dvs->init_state;
692 ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
693 if (ret) {
694 dev_err(lp->dev, "gpio request err: %d\n", ret);
695 return ret;
698 lp->dvs_pin = pinstate;
700 return 0;
702 set_default_dvs_mode:
703 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
704 default_dvs_mode[lp->chipid]);
707 static int lp872x_hw_enable(struct lp872x *lp)
709 int ret, gpio;
711 if (!lp->pdata)
712 return -EINVAL;
714 gpio = lp->pdata->enable_gpio;
715 if (!gpio_is_valid(gpio))
716 return 0;
718 /* Always set enable GPIO high. */
719 ret = devm_gpio_request_one(lp->dev, gpio, GPIOF_OUT_INIT_HIGH, "LP872X EN");
720 if (ret) {
721 dev_err(lp->dev, "gpio request err: %d\n", ret);
722 return ret;
725 /* Each chip has a different enable delay. */
726 if (lp->chipid == LP8720)
727 usleep_range(LP8720_ENABLE_DELAY, 1.5 * LP8720_ENABLE_DELAY);
728 else
729 usleep_range(LP8725_ENABLE_DELAY, 1.5 * LP8725_ENABLE_DELAY);
731 return 0;
734 static int lp872x_config(struct lp872x *lp)
736 struct lp872x_platform_data *pdata = lp->pdata;
737 int ret;
739 if (!pdata || !pdata->update_config)
740 goto init_dvs;
742 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
743 if (ret)
744 return ret;
746 init_dvs:
747 return lp872x_init_dvs(lp);
750 static struct regulator_init_data
751 *lp872x_find_regulator_init_data(int id, struct lp872x *lp)
753 struct lp872x_platform_data *pdata = lp->pdata;
754 int i;
756 if (!pdata)
757 return NULL;
759 for (i = 0; i < lp->num_regulators; i++) {
760 if (pdata->regulator_data[i].id == id)
761 return pdata->regulator_data[i].init_data;
764 return NULL;
767 static int lp872x_regulator_register(struct lp872x *lp)
769 const struct regulator_desc *desc;
770 struct regulator_config cfg = { };
771 struct regulator_dev *rdev;
772 int i;
774 for (i = 0; i < lp->num_regulators; i++) {
775 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
776 &lp8725_regulator_desc[i];
778 cfg.dev = lp->dev;
779 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
780 cfg.driver_data = lp;
781 cfg.regmap = lp->regmap;
783 rdev = devm_regulator_register(lp->dev, desc, &cfg);
784 if (IS_ERR(rdev)) {
785 dev_err(lp->dev, "regulator register err");
786 return PTR_ERR(rdev);
790 return 0;
793 static const struct regmap_config lp872x_regmap_config = {
794 .reg_bits = 8,
795 .val_bits = 8,
796 .max_register = MAX_REGISTERS,
799 #ifdef CONFIG_OF
801 #define LP872X_VALID_OPMODE (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL)
803 static struct of_regulator_match lp8720_matches[] = {
804 { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, },
805 { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, },
806 { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, },
807 { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, },
808 { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, },
809 { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, },
812 static struct of_regulator_match lp8725_matches[] = {
813 { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, },
814 { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, },
815 { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, },
816 { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, },
817 { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, },
818 { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, },
819 { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, },
820 { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, },
821 { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, },
824 static struct lp872x_platform_data
825 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
827 struct device_node *np = dev->of_node;
828 struct lp872x_platform_data *pdata;
829 struct of_regulator_match *match;
830 int num_matches;
831 int count;
832 int i;
833 u8 dvs_state;
835 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
836 if (!pdata)
837 return ERR_PTR(-ENOMEM);
839 of_property_read_u8(np, "ti,general-config", &pdata->general_config);
840 if (of_find_property(np, "ti,update-config", NULL))
841 pdata->update_config = true;
843 pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL);
844 if (!pdata->dvs)
845 return ERR_PTR(-ENOMEM);
847 pdata->dvs->gpio = of_get_named_gpio(np, "ti,dvs-gpio", 0);
848 of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel);
849 of_property_read_u8(np, "ti,dvs-state", &dvs_state);
850 pdata->dvs->init_state = dvs_state ? DVS_HIGH : DVS_LOW;
852 pdata->enable_gpio = of_get_named_gpio(np, "enable-gpios", 0);
854 if (of_get_child_count(np) == 0)
855 goto out;
857 switch (which) {
858 case LP8720:
859 match = lp8720_matches;
860 num_matches = ARRAY_SIZE(lp8720_matches);
861 break;
862 case LP8725:
863 match = lp8725_matches;
864 num_matches = ARRAY_SIZE(lp8725_matches);
865 break;
866 default:
867 goto out;
870 count = of_regulator_match(dev, np, match, num_matches);
871 if (count <= 0)
872 goto out;
874 for (i = 0; i < num_matches; i++) {
875 pdata->regulator_data[i].id =
876 (enum lp872x_regulator_id)match[i].driver_data;
877 pdata->regulator_data[i].init_data = match[i].init_data;
879 out:
880 return pdata;
882 #else
883 static struct lp872x_platform_data
884 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
886 return NULL;
888 #endif
890 static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
892 struct lp872x *lp;
893 struct lp872x_platform_data *pdata;
894 int ret;
895 const int lp872x_num_regulators[] = {
896 [LP8720] = LP8720_NUM_REGULATORS,
897 [LP8725] = LP8725_NUM_REGULATORS,
900 if (cl->dev.of_node) {
901 pdata = lp872x_populate_pdata_from_dt(&cl->dev,
902 (enum lp872x_id)id->driver_data);
903 if (IS_ERR(pdata))
904 return PTR_ERR(pdata);
905 } else {
906 pdata = dev_get_platdata(&cl->dev);
909 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
910 if (!lp)
911 return -ENOMEM;
913 lp->num_regulators = lp872x_num_regulators[id->driver_data];
915 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
916 if (IS_ERR(lp->regmap)) {
917 ret = PTR_ERR(lp->regmap);
918 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
919 return ret;
922 lp->dev = &cl->dev;
923 lp->pdata = pdata;
924 lp->chipid = id->driver_data;
925 i2c_set_clientdata(cl, lp);
927 ret = lp872x_hw_enable(lp);
928 if (ret)
929 return ret;
931 ret = lp872x_config(lp);
932 if (ret)
933 return ret;
935 return lp872x_regulator_register(lp);
938 static const struct of_device_id lp872x_dt_ids[] = {
939 { .compatible = "ti,lp8720", },
940 { .compatible = "ti,lp8725", },
943 MODULE_DEVICE_TABLE(of, lp872x_dt_ids);
945 static const struct i2c_device_id lp872x_ids[] = {
946 {"lp8720", LP8720},
947 {"lp8725", LP8725},
950 MODULE_DEVICE_TABLE(i2c, lp872x_ids);
952 static struct i2c_driver lp872x_driver = {
953 .driver = {
954 .name = "lp872x",
955 .of_match_table = of_match_ptr(lp872x_dt_ids),
957 .probe = lp872x_probe,
958 .id_table = lp872x_ids,
961 module_i2c_driver(lp872x_driver);
963 MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
964 MODULE_AUTHOR("Milo Kim");
965 MODULE_LICENSE("GPL");