blob: 35d826fe9def11eb12008581dbe6331ee9a68896 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Kim, Miloaf8b5fc2012-06-19 07:08:22 +00002/*
3 * Copyright 2012 Texas Instruments
4 *
5 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
Kim, Miloaf8b5fc2012-06-19 07:08:22 +00006 */
7
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>
MaĆ­ra Canal72bf80c2021-10-15 12:14:35 -030013#include <linux/gpio/consumer.h>
Paul Kocialkowski7e6213f2016-02-05 19:42:19 +010014#include <linux/delay.h>
Kim, Miloaf8b5fc2012-06-19 07:08:22 +000015#include <linux/regulator/lp872x.h>
16#include <linux/regulator/driver.h>
17#include <linux/platform_device.h>
Kim, Milo00fd6e62013-05-20 12:36:07 +000018#include <linux/of.h>
Kim, Milo00fd6e62013-05-20 12:36:07 +000019#include <linux/regulator/of_regulator.h>
Kim, Miloaf8b5fc2012-06-19 07:08:22 +000020
21/* Registers : LP8720/8725 shared */
22#define LP872X_GENERAL_CFG 0x00
23#define LP872X_LDO1_VOUT 0x01
24#define LP872X_LDO2_VOUT 0x02
25#define LP872X_LDO3_VOUT 0x03
26#define LP872X_LDO4_VOUT 0x04
27#define LP872X_LDO5_VOUT 0x05
28
29/* Registers : LP8720 */
30#define LP8720_BUCK_VOUT1 0x06
31#define LP8720_BUCK_VOUT2 0x07
32#define LP8720_ENABLE 0x08
33
34/* Registers : LP8725 */
35#define LP8725_LILO1_VOUT 0x06
36#define LP8725_LILO2_VOUT 0x07
37#define LP8725_BUCK1_VOUT1 0x08
38#define LP8725_BUCK1_VOUT2 0x09
39#define LP8725_BUCK2_VOUT1 0x0A
40#define LP8725_BUCK2_VOUT2 0x0B
41#define LP8725_BUCK_CTRL 0x0C
42#define LP8725_LDO_CTRL 0x0D
43
44/* Mask/shift : LP8720/LP8725 shared */
45#define LP872X_VOUT_M 0x1F
46#define LP872X_START_DELAY_M 0xE0
47#define LP872X_START_DELAY_S 5
48#define LP872X_EN_LDO1_M BIT(0)
49#define LP872X_EN_LDO2_M BIT(1)
50#define LP872X_EN_LDO3_M BIT(2)
51#define LP872X_EN_LDO4_M BIT(3)
52#define LP872X_EN_LDO5_M BIT(4)
53
54/* Mask/shift : LP8720 */
55#define LP8720_TIMESTEP_S 0 /* Addr 00h */
56#define LP8720_TIMESTEP_M BIT(0)
57#define LP8720_EXT_DVS_M BIT(2)
58#define LP8720_BUCK_FPWM_S 5 /* Addr 07h */
59#define LP8720_BUCK_FPWM_M BIT(5)
60#define LP8720_EN_BUCK_M BIT(5) /* Addr 08h */
61#define LP8720_DVS_SEL_M BIT(7)
62
63/* Mask/shift : LP8725 */
64#define LP8725_TIMESTEP_M 0xC0 /* Addr 00h */
65#define LP8725_TIMESTEP_S 6
66#define LP8725_BUCK1_EN_M BIT(0)
67#define LP8725_DVS1_M BIT(2)
68#define LP8725_DVS2_M BIT(3)
69#define LP8725_BUCK2_EN_M BIT(4)
70#define LP8725_BUCK_CL_M 0xC0 /* Addr 09h, 0Bh */
71#define LP8725_BUCK_CL_S 6
72#define LP8725_BUCK1_FPWM_S 1 /* Addr 0Ch */
73#define LP8725_BUCK1_FPWM_M BIT(1)
74#define LP8725_BUCK2_FPWM_S 5
75#define LP8725_BUCK2_FPWM_M BIT(5)
76#define LP8725_EN_LILO1_M BIT(5) /* Addr 0Dh */
77#define LP8725_EN_LILO2_M BIT(6)
78
79/* PWM mode */
80#define LP872X_FORCE_PWM 1
81#define LP872X_AUTO_PWM 0
82
83#define LP8720_NUM_REGULATORS 6
84#define LP8725_NUM_REGULATORS 9
85#define EXTERN_DVS_USED 0
86#define MAX_DELAY 6
87
Kim, Milob158fba2012-09-05 04:39:16 +000088/* Default DVS Mode */
89#define LP8720_DEFAULT_DVS 0
90#define LP8725_DEFAULT_DVS BIT(2)
91
Kim, Miloaf8b5fc2012-06-19 07:08:22 +000092/* dump registers in regmap-debugfs */
93#define MAX_REGISTERS 0x0F
94
95enum lp872x_id {
96 LP8720,
97 LP8725,
98};
99
100struct lp872x {
101 struct regmap *regmap;
102 struct device *dev;
103 enum lp872x_id chipid;
104 struct lp872x_platform_data *pdata;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000105 int num_regulators;
Nathan Chancellor061514d2021-10-18 17:43:35 -0700106 enum gpiod_flags dvs_pin;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000107};
108
109/* LP8720/LP8725 shared voltage table for LDOs */
110static const unsigned int lp872x_ldo_vtbl[] = {
111 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
112 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
113 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
114 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
115};
116
117/* LP8720 LDO4 voltage table */
118static const unsigned int lp8720_ldo4_vtbl[] = {
119 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000,
120 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
121 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
122 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
123};
124
125/* LP8725 LILO(Low Input Low Output) voltage table */
126static const unsigned int lp8725_lilo_vtbl[] = {
127 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
128 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
129 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
130 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
131};
132
133/* LP8720 BUCK voltage table */
134#define EXT_R 0 /* external resistor divider */
135static const unsigned int lp8720_buck_vtbl[] = {
136 EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
137 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
138 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
139 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
140};
141
142/* LP8725 BUCK voltage table */
143static const unsigned int lp8725_buck_vtbl[] = {
144 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
145 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
146 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
147 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
148};
149
150/* LP8725 BUCK current limit */
151static const unsigned int lp8725_buck_uA[] = {
152 460000, 780000, 1050000, 1370000,
153};
154
155static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
156{
157 int ret;
158 unsigned int val;
159
160 ret = regmap_read(lp->regmap, addr, &val);
161 if (ret < 0) {
162 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
163 return ret;
164 }
165
166 *data = (u8)val;
167 return 0;
168}
169
170static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
171{
172 return regmap_write(lp->regmap, addr, data);
173}
174
175static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
176 unsigned int mask, u8 data)
177{
178 return regmap_update_bits(lp->regmap, addr, mask, data);
179}
180
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000181static int lp872x_get_timestep_usec(struct lp872x *lp)
182{
183 enum lp872x_id chip = lp->chipid;
184 u8 val, mask, shift;
185 int *time_usec, size, ret;
186 int lp8720_time_usec[] = { 25, 50 };
187 int lp8725_time_usec[] = { 32, 64, 128, 256 };
188
189 switch (chip) {
190 case LP8720:
191 mask = LP8720_TIMESTEP_M;
192 shift = LP8720_TIMESTEP_S;
193 time_usec = &lp8720_time_usec[0];
194 size = ARRAY_SIZE(lp8720_time_usec);
195 break;
196 case LP8725:
197 mask = LP8725_TIMESTEP_M;
198 shift = LP8725_TIMESTEP_S;
199 time_usec = &lp8725_time_usec[0];
200 size = ARRAY_SIZE(lp8725_time_usec);
201 break;
202 default:
203 return -EINVAL;
204 }
205
206 ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
207 if (ret)
Sachin Kamatad5ec6c2014-02-18 16:11:02 +0530208 return ret;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000209
210 val = (val & mask) >> shift;
211 if (val >= size)
212 return -EINVAL;
213
214 return *(time_usec + val);
215}
216
217static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
218{
219 struct lp872x *lp = rdev_get_drvdata(rdev);
Axel Lin2c129922012-12-08 09:56:48 +0800220 enum lp872x_regulator_id rid = rdev_get_id(rdev);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000221 int time_step_us = lp872x_get_timestep_usec(lp);
Axel Lin2c129922012-12-08 09:56:48 +0800222 int ret;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000223 u8 addr, val;
224
225 if (time_step_us < 0)
Sachin Kamatad5ec6c2014-02-18 16:11:02 +0530226 return time_step_us;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000227
Axel Lin2c129922012-12-08 09:56:48 +0800228 switch (rid) {
229 case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
230 addr = LP872X_LDO1_VOUT + rid;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000231 break;
Axel Lin2c129922012-12-08 09:56:48 +0800232 case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
233 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000234 break;
235 case LP8725_ID_BUCK2:
236 addr = LP8725_BUCK2_VOUT1;
237 break;
238 default:
239 return -EINVAL;
240 }
241
242 ret = lp872x_read_byte(lp, addr, &val);
243 if (ret)
244 return ret;
245
246 val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
247
248 return val > MAX_DELAY ? 0 : val * time_step_us;
249}
250
Kim, Milo9d6da6f2012-09-05 04:39:33 +0000251static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
MaĆ­ra Canal72bf80c2021-10-15 12:14:35 -0300252 struct gpio_desc *gpio)
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000253{
Nathan Chancellor061514d2021-10-18 17:43:35 -0700254 enum gpiod_flags state;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000255
Nathan Chancellor061514d2021-10-18 17:43:35 -0700256 state = dvs_sel == SEL_V1 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
MaĆ­ra Canal72bf80c2021-10-15 12:14:35 -0300257 gpiod_set_value(gpio, state);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000258 lp->dvs_pin = state;
259}
260
261static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
262 enum lp872x_regulator_id buck)
263{
264 u8 val, addr;
265
266 if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
267 return 0;
268
269 switch (buck) {
270 case LP8720_ID_BUCK:
271 if (val & LP8720_EXT_DVS_M) {
Nathan Chancellor061514d2021-10-18 17:43:35 -0700272 addr = (lp->dvs_pin == GPIOD_OUT_HIGH) ?
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000273 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
274 } else {
275 if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
276 return 0;
277
278 addr = val & LP8720_DVS_SEL_M ?
279 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
280 }
281 break;
282 case LP8725_ID_BUCK1:
283 if (val & LP8725_DVS1_M)
284 addr = LP8725_BUCK1_VOUT1;
285 else
Nathan Chancellor061514d2021-10-18 17:43:35 -0700286 addr = (lp->dvs_pin == GPIOD_OUT_HIGH) ?
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000287 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
288 break;
289 case LP8725_ID_BUCK2:
290 addr = val & LP8725_DVS2_M ?
291 LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
292 break;
293 default:
294 return 0;
295 }
296
297 return addr;
298}
299
300static bool lp872x_is_valid_buck_addr(u8 addr)
301{
302 switch (addr) {
303 case LP8720_BUCK_VOUT1:
304 case LP8720_BUCK_VOUT2:
305 case LP8725_BUCK1_VOUT1:
306 case LP8725_BUCK1_VOUT2:
307 case LP8725_BUCK2_VOUT1:
308 case LP8725_BUCK2_VOUT2:
309 return true;
310 default:
311 return false;
312 }
313}
314
315static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
316 unsigned selector)
317{
318 struct lp872x *lp = rdev_get_drvdata(rdev);
319 enum lp872x_regulator_id buck = rdev_get_id(rdev);
320 u8 addr, mask = LP872X_VOUT_M;
Kim, Milo9d6da6f2012-09-05 04:39:33 +0000321 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000322
MaĆ­ra Canal72bf80c2021-10-15 12:14:35 -0300323 if (dvs && dvs->gpio)
Kim, Milo9d6da6f2012-09-05 04:39:33 +0000324 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000325
326 addr = lp872x_select_buck_vout_addr(lp, buck);
327 if (!lp872x_is_valid_buck_addr(addr))
328 return -EINVAL;
329
330 return lp872x_update_bits(lp, addr, mask, selector);
331}
332
333static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
334{
335 struct lp872x *lp = rdev_get_drvdata(rdev);
336 enum lp872x_regulator_id buck = rdev_get_id(rdev);
337 u8 addr, val;
338 int ret;
339
340 addr = lp872x_select_buck_vout_addr(lp, buck);
341 if (!lp872x_is_valid_buck_addr(addr))
342 return -EINVAL;
343
344 ret = lp872x_read_byte(lp, addr, &val);
345 if (ret)
346 return ret;
347
348 return val & LP872X_VOUT_M;
349}
350
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000351static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
352{
353 struct lp872x *lp = rdev_get_drvdata(rdev);
354 enum lp872x_regulator_id buck = rdev_get_id(rdev);
355 u8 addr, mask, shift, val;
356
357 switch (buck) {
358 case LP8720_ID_BUCK:
359 addr = LP8720_BUCK_VOUT2;
360 mask = LP8720_BUCK_FPWM_M;
361 shift = LP8720_BUCK_FPWM_S;
362 break;
363 case LP8725_ID_BUCK1:
364 addr = LP8725_BUCK_CTRL;
365 mask = LP8725_BUCK1_FPWM_M;
366 shift = LP8725_BUCK1_FPWM_S;
367 break;
368 case LP8725_ID_BUCK2:
369 addr = LP8725_BUCK_CTRL;
370 mask = LP8725_BUCK2_FPWM_M;
371 shift = LP8725_BUCK2_FPWM_S;
372 break;
373 default:
374 return -EINVAL;
375 }
376
377 if (mode == REGULATOR_MODE_FAST)
378 val = LP872X_FORCE_PWM << shift;
379 else if (mode == REGULATOR_MODE_NORMAL)
380 val = LP872X_AUTO_PWM << shift;
381 else
382 return -EINVAL;
383
384 return lp872x_update_bits(lp, addr, mask, val);
385}
386
387static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
388{
389 struct lp872x *lp = rdev_get_drvdata(rdev);
390 enum lp872x_regulator_id buck = rdev_get_id(rdev);
391 u8 addr, mask, val;
392 int ret;
393
394 switch (buck) {
395 case LP8720_ID_BUCK:
396 addr = LP8720_BUCK_VOUT2;
397 mask = LP8720_BUCK_FPWM_M;
398 break;
399 case LP8725_ID_BUCK1:
400 addr = LP8725_BUCK_CTRL;
401 mask = LP8725_BUCK1_FPWM_M;
402 break;
403 case LP8725_ID_BUCK2:
404 addr = LP8725_BUCK_CTRL;
405 mask = LP8725_BUCK2_FPWM_M;
406 break;
407 default:
408 return -EINVAL;
409 }
410
411 ret = lp872x_read_byte(lp, addr, &val);
412 if (ret)
413 return ret;
414
415 return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
416}
417
Axel Linf9664042019-01-26 11:39:02 +0800418static const struct regulator_ops lp872x_ldo_ops = {
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000419 .list_voltage = regulator_list_voltage_table,
Axel Lina32f9e02013-04-24 20:44:47 +0800420 .map_voltage = regulator_map_voltage_ascend,
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000421 .set_voltage_sel = regulator_set_voltage_sel_regmap,
422 .get_voltage_sel = regulator_get_voltage_sel_regmap,
423 .enable = regulator_enable_regmap,
424 .disable = regulator_disable_regmap,
425 .is_enabled = regulator_is_enabled_regmap,
426 .enable_time = lp872x_regulator_enable_time,
427};
428
Axel Linf9664042019-01-26 11:39:02 +0800429static const struct regulator_ops lp8720_buck_ops = {
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000430 .list_voltage = regulator_list_voltage_table,
Axel Lina32f9e02013-04-24 20:44:47 +0800431 .map_voltage = regulator_map_voltage_ascend,
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000432 .set_voltage_sel = lp872x_buck_set_voltage_sel,
433 .get_voltage_sel = lp872x_buck_get_voltage_sel,
434 .enable = regulator_enable_regmap,
435 .disable = regulator_disable_regmap,
436 .is_enabled = regulator_is_enabled_regmap,
437 .enable_time = lp872x_regulator_enable_time,
438 .set_mode = lp872x_buck_set_mode,
439 .get_mode = lp872x_buck_get_mode,
440};
441
Axel Linf9664042019-01-26 11:39:02 +0800442static const struct regulator_ops lp8725_buck_ops = {
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000443 .list_voltage = regulator_list_voltage_table,
Axel Lina32f9e02013-04-24 20:44:47 +0800444 .map_voltage = regulator_map_voltage_ascend,
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000445 .set_voltage_sel = lp872x_buck_set_voltage_sel,
446 .get_voltage_sel = lp872x_buck_get_voltage_sel,
447 .enable = regulator_enable_regmap,
448 .disable = regulator_disable_regmap,
449 .is_enabled = regulator_is_enabled_regmap,
450 .enable_time = lp872x_regulator_enable_time,
451 .set_mode = lp872x_buck_set_mode,
452 .get_mode = lp872x_buck_get_mode,
Axel Lin8918f062019-02-28 21:40:16 +0800453 .set_current_limit = regulator_set_current_limit_regmap,
454 .get_current_limit = regulator_get_current_limit_regmap,
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000455};
456
Axel Linf9664042019-01-26 11:39:02 +0800457static const struct regulator_desc lp8720_regulator_desc[] = {
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000458 {
459 .name = "ldo1",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100460 .of_match = of_match_ptr("ldo1"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000461 .id = LP8720_ID_LDO1,
462 .ops = &lp872x_ldo_ops,
463 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
464 .volt_table = lp872x_ldo_vtbl,
465 .type = REGULATOR_VOLTAGE,
466 .owner = THIS_MODULE,
467 .vsel_reg = LP872X_LDO1_VOUT,
468 .vsel_mask = LP872X_VOUT_M,
469 .enable_reg = LP8720_ENABLE,
470 .enable_mask = LP872X_EN_LDO1_M,
471 },
472 {
473 .name = "ldo2",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100474 .of_match = of_match_ptr("ldo2"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000475 .id = LP8720_ID_LDO2,
476 .ops = &lp872x_ldo_ops,
477 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
478 .volt_table = lp872x_ldo_vtbl,
479 .type = REGULATOR_VOLTAGE,
480 .owner = THIS_MODULE,
481 .vsel_reg = LP872X_LDO2_VOUT,
482 .vsel_mask = LP872X_VOUT_M,
483 .enable_reg = LP8720_ENABLE,
484 .enable_mask = LP872X_EN_LDO2_M,
485 },
486 {
487 .name = "ldo3",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100488 .of_match = of_match_ptr("ldo3"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000489 .id = LP8720_ID_LDO3,
490 .ops = &lp872x_ldo_ops,
491 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
492 .volt_table = lp872x_ldo_vtbl,
493 .type = REGULATOR_VOLTAGE,
494 .owner = THIS_MODULE,
495 .vsel_reg = LP872X_LDO3_VOUT,
496 .vsel_mask = LP872X_VOUT_M,
497 .enable_reg = LP8720_ENABLE,
498 .enable_mask = LP872X_EN_LDO3_M,
499 },
500 {
501 .name = "ldo4",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100502 .of_match = of_match_ptr("ldo4"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000503 .id = LP8720_ID_LDO4,
504 .ops = &lp872x_ldo_ops,
505 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
506 .volt_table = lp8720_ldo4_vtbl,
507 .type = REGULATOR_VOLTAGE,
508 .owner = THIS_MODULE,
509 .vsel_reg = LP872X_LDO4_VOUT,
510 .vsel_mask = LP872X_VOUT_M,
511 .enable_reg = LP8720_ENABLE,
512 .enable_mask = LP872X_EN_LDO4_M,
513 },
514 {
515 .name = "ldo5",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100516 .of_match = of_match_ptr("ldo5"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000517 .id = LP8720_ID_LDO5,
518 .ops = &lp872x_ldo_ops,
519 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
520 .volt_table = lp872x_ldo_vtbl,
521 .type = REGULATOR_VOLTAGE,
522 .owner = THIS_MODULE,
523 .vsel_reg = LP872X_LDO5_VOUT,
524 .vsel_mask = LP872X_VOUT_M,
525 .enable_reg = LP8720_ENABLE,
526 .enable_mask = LP872X_EN_LDO5_M,
527 },
528 {
529 .name = "buck",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100530 .of_match = of_match_ptr("buck"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000531 .id = LP8720_ID_BUCK,
532 .ops = &lp8720_buck_ops,
533 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
534 .volt_table = lp8720_buck_vtbl,
535 .type = REGULATOR_VOLTAGE,
536 .owner = THIS_MODULE,
537 .enable_reg = LP8720_ENABLE,
538 .enable_mask = LP8720_EN_BUCK_M,
539 },
540};
541
Axel Linf9664042019-01-26 11:39:02 +0800542static const struct regulator_desc lp8725_regulator_desc[] = {
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000543 {
544 .name = "ldo1",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100545 .of_match = of_match_ptr("ldo1"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000546 .id = LP8725_ID_LDO1,
547 .ops = &lp872x_ldo_ops,
548 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
549 .volt_table = lp872x_ldo_vtbl,
550 .type = REGULATOR_VOLTAGE,
551 .owner = THIS_MODULE,
552 .vsel_reg = LP872X_LDO1_VOUT,
553 .vsel_mask = LP872X_VOUT_M,
554 .enable_reg = LP8725_LDO_CTRL,
555 .enable_mask = LP872X_EN_LDO1_M,
556 },
557 {
558 .name = "ldo2",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100559 .of_match = of_match_ptr("ldo2"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000560 .id = LP8725_ID_LDO2,
561 .ops = &lp872x_ldo_ops,
562 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
563 .volt_table = lp872x_ldo_vtbl,
564 .type = REGULATOR_VOLTAGE,
565 .owner = THIS_MODULE,
566 .vsel_reg = LP872X_LDO2_VOUT,
567 .vsel_mask = LP872X_VOUT_M,
568 .enable_reg = LP8725_LDO_CTRL,
569 .enable_mask = LP872X_EN_LDO2_M,
570 },
571 {
572 .name = "ldo3",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100573 .of_match = of_match_ptr("ldo3"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000574 .id = LP8725_ID_LDO3,
575 .ops = &lp872x_ldo_ops,
576 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
577 .volt_table = lp872x_ldo_vtbl,
578 .type = REGULATOR_VOLTAGE,
579 .owner = THIS_MODULE,
580 .vsel_reg = LP872X_LDO3_VOUT,
581 .vsel_mask = LP872X_VOUT_M,
582 .enable_reg = LP8725_LDO_CTRL,
583 .enable_mask = LP872X_EN_LDO3_M,
584 },
585 {
586 .name = "ldo4",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100587 .of_match = of_match_ptr("ldo4"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000588 .id = LP8725_ID_LDO4,
589 .ops = &lp872x_ldo_ops,
590 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
591 .volt_table = lp872x_ldo_vtbl,
592 .type = REGULATOR_VOLTAGE,
593 .owner = THIS_MODULE,
594 .vsel_reg = LP872X_LDO4_VOUT,
595 .vsel_mask = LP872X_VOUT_M,
596 .enable_reg = LP8725_LDO_CTRL,
597 .enable_mask = LP872X_EN_LDO4_M,
598 },
599 {
600 .name = "ldo5",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100601 .of_match = of_match_ptr("ldo5"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000602 .id = LP8725_ID_LDO5,
603 .ops = &lp872x_ldo_ops,
604 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
605 .volt_table = lp872x_ldo_vtbl,
606 .type = REGULATOR_VOLTAGE,
607 .owner = THIS_MODULE,
608 .vsel_reg = LP872X_LDO5_VOUT,
609 .vsel_mask = LP872X_VOUT_M,
610 .enable_reg = LP8725_LDO_CTRL,
611 .enable_mask = LP872X_EN_LDO5_M,
612 },
613 {
614 .name = "lilo1",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100615 .of_match = of_match_ptr("lilo1"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000616 .id = LP8725_ID_LILO1,
617 .ops = &lp872x_ldo_ops,
618 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
619 .volt_table = lp8725_lilo_vtbl,
620 .type = REGULATOR_VOLTAGE,
621 .owner = THIS_MODULE,
622 .vsel_reg = LP8725_LILO1_VOUT,
623 .vsel_mask = LP872X_VOUT_M,
624 .enable_reg = LP8725_LDO_CTRL,
625 .enable_mask = LP8725_EN_LILO1_M,
626 },
627 {
628 .name = "lilo2",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100629 .of_match = of_match_ptr("lilo2"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000630 .id = LP8725_ID_LILO2,
631 .ops = &lp872x_ldo_ops,
632 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
633 .volt_table = lp8725_lilo_vtbl,
634 .type = REGULATOR_VOLTAGE,
635 .owner = THIS_MODULE,
636 .vsel_reg = LP8725_LILO2_VOUT,
637 .vsel_mask = LP872X_VOUT_M,
638 .enable_reg = LP8725_LDO_CTRL,
639 .enable_mask = LP8725_EN_LILO2_M,
640 },
641 {
642 .name = "buck1",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100643 .of_match = of_match_ptr("buck1"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000644 .id = LP8725_ID_BUCK1,
645 .ops = &lp8725_buck_ops,
646 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
647 .volt_table = lp8725_buck_vtbl,
648 .type = REGULATOR_VOLTAGE,
649 .owner = THIS_MODULE,
650 .enable_reg = LP872X_GENERAL_CFG,
651 .enable_mask = LP8725_BUCK1_EN_M,
Axel Lin8918f062019-02-28 21:40:16 +0800652 .curr_table = lp8725_buck_uA,
653 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
654 .csel_reg = LP8725_BUCK1_VOUT2,
655 .csel_mask = LP8725_BUCK_CL_M,
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000656 },
657 {
658 .name = "buck2",
Paul Kocialkowski1f97fe42015-12-23 11:58:34 +0100659 .of_match = of_match_ptr("buck2"),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000660 .id = LP8725_ID_BUCK2,
661 .ops = &lp8725_buck_ops,
662 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
663 .volt_table = lp8725_buck_vtbl,
664 .type = REGULATOR_VOLTAGE,
665 .owner = THIS_MODULE,
666 .enable_reg = LP872X_GENERAL_CFG,
667 .enable_mask = LP8725_BUCK2_EN_M,
Axel Lin8918f062019-02-28 21:40:16 +0800668 .curr_table = lp8725_buck_uA,
669 .n_current_limits = ARRAY_SIZE(lp8725_buck_uA),
670 .csel_reg = LP8725_BUCK2_VOUT2,
671 .csel_mask = LP8725_BUCK_CL_M,
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000672 },
673};
674
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000675static int lp872x_init_dvs(struct lp872x *lp)
676{
Kim, Milob158fba2012-09-05 04:39:16 +0000677 struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
Nathan Chancellor061514d2021-10-18 17:43:35 -0700678 enum gpiod_flags pinstate;
Kim, Milob158fba2012-09-05 04:39:16 +0000679 u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
680 u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
681
682 if (!dvs)
683 goto set_default_dvs_mode;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000684
MaĆ­ra Canal72bf80c2021-10-15 12:14:35 -0300685 if (!dvs->gpio)
Kim, Milo00fd6e62013-05-20 12:36:07 +0000686 goto set_default_dvs_mode;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000687
688 pinstate = dvs->init_state;
MaĆ­ra Canal72bf80c2021-10-15 12:14:35 -0300689 dvs->gpio = devm_gpiod_get_optional(lp->dev, "ti,dvs", pinstate);
690
691 if (IS_ERR(dvs->gpio)) {
692 dev_err(lp->dev, "gpio request err: %ld\n", PTR_ERR(dvs->gpio));
693 return PTR_ERR(dvs->gpio);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000694 }
695
696 lp->dvs_pin = pinstate;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000697
698 return 0;
Kim, Milob158fba2012-09-05 04:39:16 +0000699
700set_default_dvs_mode:
701 return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
702 default_dvs_mode[lp->chipid]);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000703}
704
Paul Kocialkowski7e6213f2016-02-05 19:42:19 +0100705static int lp872x_hw_enable(struct lp872x *lp)
706{
Paul Kocialkowski7e6213f2016-02-05 19:42:19 +0100707 if (!lp->pdata)
708 return -EINVAL;
709
MaĆ­ra Canal72bf80c2021-10-15 12:14:35 -0300710 if (!lp->pdata->enable_gpio)
Paul Kocialkowski7e6213f2016-02-05 19:42:19 +0100711 return 0;
712
713 /* Always set enable GPIO high. */
MaĆ­ra Canal72bf80c2021-10-15 12:14:35 -0300714 lp->pdata->enable_gpio = devm_gpiod_get_optional(lp->dev, "enable", GPIOD_OUT_HIGH);
715 if (IS_ERR(lp->pdata->enable_gpio)) {
716 dev_err(lp->dev, "gpio request err: %ld\n", PTR_ERR(lp->pdata->enable_gpio));
717 return PTR_ERR(lp->pdata->enable_gpio);
Paul Kocialkowski7e6213f2016-02-05 19:42:19 +0100718 }
719
720 /* Each chip has a different enable delay. */
721 if (lp->chipid == LP8720)
722 usleep_range(LP8720_ENABLE_DELAY, 1.5 * LP8720_ENABLE_DELAY);
723 else
724 usleep_range(LP8725_ENABLE_DELAY, 1.5 * LP8725_ENABLE_DELAY);
725
726 return 0;
727}
728
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000729static int lp872x_config(struct lp872x *lp)
730{
731 struct lp872x_platform_data *pdata = lp->pdata;
732 int ret;
733
Kim, Milo86b3fef2012-09-05 04:39:23 +0000734 if (!pdata || !pdata->update_config)
735 goto init_dvs;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000736
737 ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
738 if (ret)
739 return ret;
740
Kim, Milo86b3fef2012-09-05 04:39:23 +0000741init_dvs:
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000742 return lp872x_init_dvs(lp);
743}
744
745static struct regulator_init_data
Axel Lin5bae0622012-07-15 16:57:12 +0800746*lp872x_find_regulator_init_data(int id, struct lp872x *lp)
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000747{
Kim, Milo9ffaa862012-09-05 04:39:11 +0000748 struct lp872x_platform_data *pdata = lp->pdata;
Axel Lin5bae0622012-07-15 16:57:12 +0800749 int i;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000750
Kim, Milo9ffaa862012-09-05 04:39:11 +0000751 if (!pdata)
752 return NULL;
753
Axel Lin5bae0622012-07-15 16:57:12 +0800754 for (i = 0; i < lp->num_regulators; i++) {
Kim, Milo9ffaa862012-09-05 04:39:11 +0000755 if (pdata->regulator_data[i].id == id)
756 return pdata->regulator_data[i].init_data;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000757 }
758
Axel Lin5bae0622012-07-15 16:57:12 +0800759 return NULL;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000760}
761
762static int lp872x_regulator_register(struct lp872x *lp)
763{
Axel Linf9664042019-01-26 11:39:02 +0800764 const struct regulator_desc *desc;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000765 struct regulator_config cfg = { };
766 struct regulator_dev *rdev;
Jingoo Haned6025342013-09-30 09:54:34 +0900767 int i;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000768
Milo Kim37a6f432013-07-03 10:30:57 +0900769 for (i = 0; i < lp->num_regulators; i++) {
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000770 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
771 &lp8725_regulator_desc[i];
772
773 cfg.dev = lp->dev;
Axel Lin5bae0622012-07-15 16:57:12 +0800774 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000775 cfg.driver_data = lp;
776 cfg.regmap = lp->regmap;
777
Jingoo Haned6025342013-09-30 09:54:34 +0900778 rdev = devm_regulator_register(lp->dev, desc, &cfg);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000779 if (IS_ERR(rdev)) {
780 dev_err(lp->dev, "regulator register err");
Jingoo Haned6025342013-09-30 09:54:34 +0900781 return PTR_ERR(rdev);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000782 }
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000783 }
784
785 return 0;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000786}
787
788static const struct regmap_config lp872x_regmap_config = {
789 .reg_bits = 8,
790 .val_bits = 8,
791 .max_register = MAX_REGISTERS,
792};
793
Kim, Milo00fd6e62013-05-20 12:36:07 +0000794#ifdef CONFIG_OF
795
796#define LP872X_VALID_OPMODE (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL)
797
798static struct of_regulator_match lp8720_matches[] = {
799 { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, },
800 { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, },
801 { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, },
802 { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, },
803 { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, },
804 { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, },
805};
806
807static struct of_regulator_match lp8725_matches[] = {
808 { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, },
809 { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, },
810 { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, },
811 { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, },
812 { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, },
813 { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, },
814 { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, },
815 { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, },
816 { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, },
817};
818
819static struct lp872x_platform_data
820*lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
821{
822 struct device_node *np = dev->of_node;
823 struct lp872x_platform_data *pdata;
824 struct of_regulator_match *match;
Kim, Milo00fd6e62013-05-20 12:36:07 +0000825 int num_matches;
826 int count;
827 int i;
828 u8 dvs_state;
829
830 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
831 if (!pdata)
Milo Kim804486b2015-08-25 14:54:12 +0900832 return ERR_PTR(-ENOMEM);
Kim, Milo00fd6e62013-05-20 12:36:07 +0000833
834 of_property_read_u8(np, "ti,general-config", &pdata->general_config);
835 if (of_find_property(np, "ti,update-config", NULL))
836 pdata->update_config = true;
837
838 pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL);
839 if (!pdata->dvs)
Milo Kim804486b2015-08-25 14:54:12 +0900840 return ERR_PTR(-ENOMEM);
Kim, Milo00fd6e62013-05-20 12:36:07 +0000841
Kim, Milo00fd6e62013-05-20 12:36:07 +0000842 of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel);
843 of_property_read_u8(np, "ti,dvs-state", &dvs_state);
Nathan Chancellor061514d2021-10-18 17:43:35 -0700844 pdata->dvs->init_state = dvs_state ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
Kim, Milo00fd6e62013-05-20 12:36:07 +0000845
846 if (of_get_child_count(np) == 0)
847 goto out;
848
849 switch (which) {
850 case LP8720:
851 match = lp8720_matches;
852 num_matches = ARRAY_SIZE(lp8720_matches);
853 break;
854 case LP8725:
855 match = lp8725_matches;
856 num_matches = ARRAY_SIZE(lp8725_matches);
857 break;
858 default:
859 goto out;
860 }
861
862 count = of_regulator_match(dev, np, match, num_matches);
863 if (count <= 0)
864 goto out;
865
866 for (i = 0; i < num_matches; i++) {
Milo Kim37a6f432013-07-03 10:30:57 +0900867 pdata->regulator_data[i].id =
868 (enum lp872x_regulator_id)match[i].driver_data;
Kim, Milo00fd6e62013-05-20 12:36:07 +0000869 pdata->regulator_data[i].init_data = match[i].init_data;
Kim, Milo00fd6e62013-05-20 12:36:07 +0000870 }
871out:
872 return pdata;
873}
874#else
875static struct lp872x_platform_data
876*lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
877{
878 return NULL;
879}
880#endif
881
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000882static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
883{
884 struct lp872x *lp;
Milo Kimd9ffae12015-08-25 14:54:11 +0900885 struct lp872x_platform_data *pdata;
Axel Lin8538c402015-01-26 10:25:13 +0800886 int ret;
Colin Ian King390d8282020-10-16 23:22:35 +0100887 static const int lp872x_num_regulators[] = {
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000888 [LP8720] = LP8720_NUM_REGULATORS,
889 [LP8725] = LP8725_NUM_REGULATORS,
890 };
891
Milo Kim804486b2015-08-25 14:54:12 +0900892 if (cl->dev.of_node) {
Milo Kimd9ffae12015-08-25 14:54:11 +0900893 pdata = lp872x_populate_pdata_from_dt(&cl->dev,
Kim, Milo00fd6e62013-05-20 12:36:07 +0000894 (enum lp872x_id)id->driver_data);
Milo Kim804486b2015-08-25 14:54:12 +0900895 if (IS_ERR(pdata))
896 return PTR_ERR(pdata);
897 } else {
Milo Kimd9ffae12015-08-25 14:54:11 +0900898 pdata = dev_get_platdata(&cl->dev);
Milo Kim804486b2015-08-25 14:54:12 +0900899 }
Kim, Milo00fd6e62013-05-20 12:36:07 +0000900
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000901 lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
902 if (!lp)
Axel Lin8538c402015-01-26 10:25:13 +0800903 return -ENOMEM;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000904
Axel Lin8538c402015-01-26 10:25:13 +0800905 lp->num_regulators = lp872x_num_regulators[id->driver_data];
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000906
907 lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
908 if (IS_ERR(lp->regmap)) {
909 ret = PTR_ERR(lp->regmap);
910 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
Axel Lin8538c402015-01-26 10:25:13 +0800911 return ret;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000912 }
913
914 lp->dev = &cl->dev;
Milo Kimd9ffae12015-08-25 14:54:11 +0900915 lp->pdata = pdata;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000916 lp->chipid = id->driver_data;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000917 i2c_set_clientdata(cl, lp);
918
Paul Kocialkowski7e6213f2016-02-05 19:42:19 +0100919 ret = lp872x_hw_enable(lp);
920 if (ret)
921 return ret;
922
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000923 ret = lp872x_config(lp);
924 if (ret)
Axel Lin8538c402015-01-26 10:25:13 +0800925 return ret;
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000926
927 return lp872x_regulator_register(lp);
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000928}
929
Kim, Milo00fd6e62013-05-20 12:36:07 +0000930static const struct of_device_id lp872x_dt_ids[] = {
931 { .compatible = "ti,lp8720", },
932 { .compatible = "ti,lp8725", },
933 { }
934};
935MODULE_DEVICE_TABLE(of, lp872x_dt_ids);
936
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000937static const struct i2c_device_id lp872x_ids[] = {
938 {"lp8720", LP8720},
939 {"lp8725", LP8725},
940 { }
941};
942MODULE_DEVICE_TABLE(i2c, lp872x_ids);
943
944static struct i2c_driver lp872x_driver = {
945 .driver = {
946 .name = "lp872x",
Kim, Milo00fd6e62013-05-20 12:36:07 +0000947 .of_match_table = of_match_ptr(lp872x_dt_ids),
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000948 },
949 .probe = lp872x_probe,
Kim, Miloaf8b5fc2012-06-19 07:08:22 +0000950 .id_table = lp872x_ids,
951};
952
953module_i2c_driver(lp872x_driver);
954
955MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
956MODULE_AUTHOR("Milo Kim");
957MODULE_LICENSE("GPL");