1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI LP8788 MFD - buck regulator driver
5 * Copyright 2012 Texas Instruments
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/err.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/mfd/lp8788.h>
16 #include <linux/gpio/consumer.h>
18 /* register address */
19 #define LP8788_EN_BUCK 0x0C
20 #define LP8788_BUCK_DVS_SEL 0x1D
21 #define LP8788_BUCK1_VOUT0 0x1E
22 #define LP8788_BUCK1_VOUT1 0x1F
23 #define LP8788_BUCK1_VOUT2 0x20
24 #define LP8788_BUCK1_VOUT3 0x21
25 #define LP8788_BUCK2_VOUT0 0x22
26 #define LP8788_BUCK2_VOUT1 0x23
27 #define LP8788_BUCK2_VOUT2 0x24
28 #define LP8788_BUCK2_VOUT3 0x25
29 #define LP8788_BUCK3_VOUT 0x26
30 #define LP8788_BUCK4_VOUT 0x27
31 #define LP8788_BUCK1_TIMESTEP 0x28
32 #define LP8788_BUCK_PWM 0x2D
35 #define LP8788_EN_BUCK1_M BIT(0) /* Addr 0Ch */
36 #define LP8788_EN_BUCK2_M BIT(1)
37 #define LP8788_EN_BUCK3_M BIT(2)
38 #define LP8788_EN_BUCK4_M BIT(3)
39 #define LP8788_BUCK1_DVS_SEL_M 0x04 /* Addr 1Dh */
40 #define LP8788_BUCK1_DVS_M 0x03
41 #define LP8788_BUCK1_DVS_S 0
42 #define LP8788_BUCK2_DVS_SEL_M 0x40
43 #define LP8788_BUCK2_DVS_M 0x30
44 #define LP8788_BUCK2_DVS_S 4
45 #define LP8788_BUCK1_DVS_I2C BIT(2)
46 #define LP8788_BUCK2_DVS_I2C BIT(6)
47 #define LP8788_BUCK1_DVS_PIN (0 << 2)
48 #define LP8788_BUCK2_DVS_PIN (0 << 6)
49 #define LP8788_VOUT_M 0x1F /* Addr 1Eh ~ 27h */
50 #define LP8788_STARTUP_TIME_M 0xF8 /* Addr 28h ~ 2Bh */
51 #define LP8788_STARTUP_TIME_S 3
52 #define LP8788_FPWM_BUCK1_M BIT(0) /* Addr 2Dh */
53 #define LP8788_FPWM_BUCK1_S 0
54 #define LP8788_FPWM_BUCK2_M BIT(1)
55 #define LP8788_FPWM_BUCK2_S 1
56 #define LP8788_FPWM_BUCK3_M BIT(2)
57 #define LP8788_FPWM_BUCK3_S 2
58 #define LP8788_FPWM_BUCK4_M BIT(3)
59 #define LP8788_FPWM_BUCK4_S 3
61 #define INVALID_ADDR 0xFF
62 #define LP8788_FORCE_PWM 1
63 #define LP8788_AUTO_PWM 0
66 #define ENABLE_TIME_USEC 32
68 #define BUCK_FPWM_MASK(x) (1 << (x))
69 #define BUCK_FPWM_SHIFT(x) (x)
71 enum lp8788_dvs_state
{
76 enum lp8788_dvs_mode
{
90 struct regulator_dev
*regulator
;
92 struct gpio_desc
*gpio1
;
93 struct gpio_desc
*gpio2
; /* Only used on BUCK2 */
96 /* BUCK 1 ~ 4 voltage ranges */
97 static const struct linear_range buck_volt_ranges
[] = {
98 REGULATOR_LINEAR_RANGE(500000, 0, 0, 0),
99 REGULATOR_LINEAR_RANGE(800000, 1, 25, 50000),
102 static void lp8788_buck1_set_dvs(struct lp8788_buck
*buck
)
104 struct lp8788_buck1_dvs
*dvs
= (struct lp8788_buck1_dvs
*)buck
->dvs
;
105 enum lp8788_dvs_state pinstate
;
110 pinstate
= dvs
->vsel
== DVS_SEL_V0
? DVS_LOW
: DVS_HIGH
;
111 gpiod_set_value(buck
->gpio1
, pinstate
);
114 static void lp8788_buck2_set_dvs(struct lp8788_buck
*buck
)
116 struct lp8788_buck2_dvs
*dvs
= (struct lp8788_buck2_dvs
*)buck
->dvs
;
117 enum lp8788_dvs_state pin1
, pin2
;
143 gpiod_set_value(buck
->gpio1
, pin1
);
144 gpiod_set_value(buck
->gpio2
, pin2
);
147 static void lp8788_set_dvs(struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
151 lp8788_buck1_set_dvs(buck
);
154 lp8788_buck2_set_dvs(buck
);
161 static enum lp8788_dvs_mode
162 lp8788_get_buck_dvs_ctrl_mode(struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
168 mask
= LP8788_BUCK1_DVS_SEL_M
;
171 mask
= LP8788_BUCK2_DVS_SEL_M
;
177 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
179 return val
& mask
? REGISTER
: EXTPIN
;
182 static bool lp8788_is_valid_buck_addr(u8 addr
)
185 case LP8788_BUCK1_VOUT0
:
186 case LP8788_BUCK1_VOUT1
:
187 case LP8788_BUCK1_VOUT2
:
188 case LP8788_BUCK1_VOUT3
:
189 case LP8788_BUCK2_VOUT0
:
190 case LP8788_BUCK2_VOUT1
:
191 case LP8788_BUCK2_VOUT2
:
192 case LP8788_BUCK2_VOUT3
:
199 static u8
lp8788_select_buck_vout_addr(struct lp8788_buck
*buck
,
200 enum lp8788_buck_id id
)
202 enum lp8788_dvs_mode mode
= lp8788_get_buck_dvs_ctrl_mode(buck
, id
);
208 if (mode
== EXTPIN
) {
209 idx
= gpiod_get_value(buck
->gpio1
);
211 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
212 idx
= (val
& LP8788_BUCK1_DVS_M
) >> LP8788_BUCK1_DVS_S
;
214 addr
= LP8788_BUCK1_VOUT0
+ idx
;
217 if (mode
== EXTPIN
) {
218 pin1
= gpiod_get_value(buck
->gpio1
);
219 pin2
= gpiod_get_value(buck
->gpio2
);
221 if (pin1
== PIN_LOW
&& pin2
== PIN_LOW
)
223 else if (pin1
== PIN_LOW
&& pin2
== PIN_HIGH
)
225 else if (pin1
== PIN_HIGH
&& pin2
== PIN_LOW
)
230 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
231 idx
= (val
& LP8788_BUCK2_DVS_M
) >> LP8788_BUCK2_DVS_S
;
233 addr
= LP8788_BUCK2_VOUT0
+ idx
;
244 static int lp8788_buck12_set_voltage_sel(struct regulator_dev
*rdev
,
247 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
248 enum lp8788_buck_id id
= rdev_get_id(rdev
);
252 lp8788_set_dvs(buck
, id
);
254 addr
= lp8788_select_buck_vout_addr(buck
, id
);
255 if (!lp8788_is_valid_buck_addr(addr
))
258 return lp8788_update_bits(buck
->lp
, addr
, LP8788_VOUT_M
, selector
);
261 static int lp8788_buck12_get_voltage_sel(struct regulator_dev
*rdev
)
263 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
264 enum lp8788_buck_id id
= rdev_get_id(rdev
);
268 addr
= lp8788_select_buck_vout_addr(buck
, id
);
269 if (!lp8788_is_valid_buck_addr(addr
))
272 ret
= lp8788_read_byte(buck
->lp
, addr
, &val
);
276 return val
& LP8788_VOUT_M
;
279 static int lp8788_buck_enable_time(struct regulator_dev
*rdev
)
281 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
282 enum lp8788_buck_id id
= rdev_get_id(rdev
);
283 u8 val
, addr
= LP8788_BUCK1_TIMESTEP
+ id
;
285 if (lp8788_read_byte(buck
->lp
, addr
, &val
))
288 val
= (val
& LP8788_STARTUP_TIME_M
) >> LP8788_STARTUP_TIME_S
;
290 return ENABLE_TIME_USEC
* val
;
293 static int lp8788_buck_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
295 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
296 enum lp8788_buck_id id
= rdev_get_id(rdev
);
299 mask
= BUCK_FPWM_MASK(id
);
301 case REGULATOR_MODE_FAST
:
302 val
= LP8788_FORCE_PWM
<< BUCK_FPWM_SHIFT(id
);
304 case REGULATOR_MODE_NORMAL
:
305 val
= LP8788_AUTO_PWM
<< BUCK_FPWM_SHIFT(id
);
311 return lp8788_update_bits(buck
->lp
, LP8788_BUCK_PWM
, mask
, val
);
314 static unsigned int lp8788_buck_get_mode(struct regulator_dev
*rdev
)
316 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
317 enum lp8788_buck_id id
= rdev_get_id(rdev
);
321 ret
= lp8788_read_byte(buck
->lp
, LP8788_BUCK_PWM
, &val
);
325 return val
& BUCK_FPWM_MASK(id
) ?
326 REGULATOR_MODE_FAST
: REGULATOR_MODE_NORMAL
;
329 static const struct regulator_ops lp8788_buck12_ops
= {
330 .list_voltage
= regulator_list_voltage_linear_range
,
331 .map_voltage
= regulator_map_voltage_linear_range
,
332 .set_voltage_sel
= lp8788_buck12_set_voltage_sel
,
333 .get_voltage_sel
= lp8788_buck12_get_voltage_sel
,
334 .enable
= regulator_enable_regmap
,
335 .disable
= regulator_disable_regmap
,
336 .is_enabled
= regulator_is_enabled_regmap
,
337 .enable_time
= lp8788_buck_enable_time
,
338 .set_mode
= lp8788_buck_set_mode
,
339 .get_mode
= lp8788_buck_get_mode
,
342 static const struct regulator_ops lp8788_buck34_ops
= {
343 .list_voltage
= regulator_list_voltage_linear_range
,
344 .map_voltage
= regulator_map_voltage_linear_range
,
345 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
346 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
347 .enable
= regulator_enable_regmap
,
348 .disable
= regulator_disable_regmap
,
349 .is_enabled
= regulator_is_enabled_regmap
,
350 .enable_time
= lp8788_buck_enable_time
,
351 .set_mode
= lp8788_buck_set_mode
,
352 .get_mode
= lp8788_buck_get_mode
,
355 static const struct regulator_desc lp8788_buck_desc
[] = {
359 .ops
= &lp8788_buck12_ops
,
361 .linear_ranges
= buck_volt_ranges
,
362 .n_linear_ranges
= ARRAY_SIZE(buck_volt_ranges
),
363 .type
= REGULATOR_VOLTAGE
,
364 .owner
= THIS_MODULE
,
365 .enable_reg
= LP8788_EN_BUCK
,
366 .enable_mask
= LP8788_EN_BUCK1_M
,
371 .ops
= &lp8788_buck12_ops
,
373 .linear_ranges
= buck_volt_ranges
,
374 .n_linear_ranges
= ARRAY_SIZE(buck_volt_ranges
),
375 .type
= REGULATOR_VOLTAGE
,
376 .owner
= THIS_MODULE
,
377 .enable_reg
= LP8788_EN_BUCK
,
378 .enable_mask
= LP8788_EN_BUCK2_M
,
383 .ops
= &lp8788_buck34_ops
,
385 .linear_ranges
= buck_volt_ranges
,
386 .n_linear_ranges
= ARRAY_SIZE(buck_volt_ranges
),
387 .type
= REGULATOR_VOLTAGE
,
388 .owner
= THIS_MODULE
,
389 .vsel_reg
= LP8788_BUCK3_VOUT
,
390 .vsel_mask
= LP8788_VOUT_M
,
391 .enable_reg
= LP8788_EN_BUCK
,
392 .enable_mask
= LP8788_EN_BUCK3_M
,
397 .ops
= &lp8788_buck34_ops
,
399 .linear_ranges
= buck_volt_ranges
,
400 .n_linear_ranges
= ARRAY_SIZE(buck_volt_ranges
),
401 .type
= REGULATOR_VOLTAGE
,
402 .owner
= THIS_MODULE
,
403 .vsel_reg
= LP8788_BUCK4_VOUT
,
404 .vsel_mask
= LP8788_VOUT_M
,
405 .enable_reg
= LP8788_EN_BUCK
,
406 .enable_mask
= LP8788_EN_BUCK4_M
,
410 static int lp8788_dvs_gpio_request(struct platform_device
*pdev
,
411 struct lp8788_buck
*buck
,
412 enum lp8788_buck_id id
)
414 struct lp8788_platform_data
*pdata
= buck
->lp
->pdata
;
415 struct device
*dev
= &pdev
->dev
;
419 buck
->gpio1
= devm_gpiod_get(dev
, "dvs", GPIOD_OUT_LOW
);
420 if (IS_ERR(buck
->gpio1
))
421 return PTR_ERR(buck
->gpio1
);
422 gpiod_set_consumer_name(buck
->gpio1
, "LP8788_B1_DVS");
424 buck
->dvs
= pdata
->buck1_dvs
;
427 buck
->gpio1
= devm_gpiod_get_index(dev
, "dvs", 0, GPIOD_OUT_LOW
);
428 if (IS_ERR(buck
->gpio1
))
429 return PTR_ERR(buck
->gpio1
);
430 gpiod_set_consumer_name(buck
->gpio1
, "LP8788_B2_DVS1");
432 buck
->gpio2
= devm_gpiod_get_index(dev
, "dvs", 1, GPIOD_OUT_LOW
);
433 if (IS_ERR(buck
->gpio2
))
434 return PTR_ERR(buck
->gpio2
);
435 gpiod_set_consumer_name(buck
->gpio2
, "LP8788_B2_DVS2");
437 buck
->dvs
= pdata
->buck2_dvs
;
446 static int lp8788_init_dvs(struct platform_device
*pdev
,
447 struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
449 struct lp8788_platform_data
*pdata
= buck
->lp
->pdata
;
450 u8 mask
[] = { LP8788_BUCK1_DVS_SEL_M
, LP8788_BUCK2_DVS_SEL_M
};
451 u8 val
[] = { LP8788_BUCK1_DVS_PIN
, LP8788_BUCK2_DVS_PIN
};
452 u8 default_dvs_mode
[] = { LP8788_BUCK1_DVS_I2C
, LP8788_BUCK2_DVS_I2C
};
454 /* no dvs for buck3, 4 */
458 /* no dvs platform data, then dvs will be selected by I2C registers */
460 goto set_default_dvs_mode
;
462 if ((id
== BUCK1
&& !pdata
->buck1_dvs
) ||
463 (id
== BUCK2
&& !pdata
->buck2_dvs
))
464 goto set_default_dvs_mode
;
466 if (lp8788_dvs_gpio_request(pdev
, buck
, id
))
467 goto set_default_dvs_mode
;
469 return lp8788_update_bits(buck
->lp
, LP8788_BUCK_DVS_SEL
, mask
[id
],
472 set_default_dvs_mode
:
473 return lp8788_update_bits(buck
->lp
, LP8788_BUCK_DVS_SEL
, mask
[id
],
474 default_dvs_mode
[id
]);
477 static int lp8788_buck_probe(struct platform_device
*pdev
)
479 struct lp8788
*lp
= dev_get_drvdata(pdev
->dev
.parent
);
481 struct lp8788_buck
*buck
;
482 struct regulator_config cfg
= { };
483 struct regulator_dev
*rdev
;
486 if (id
>= LP8788_NUM_BUCKS
)
489 buck
= devm_kzalloc(&pdev
->dev
, sizeof(struct lp8788_buck
), GFP_KERNEL
);
495 ret
= lp8788_init_dvs(pdev
, buck
, id
);
499 cfg
.dev
= pdev
->dev
.parent
;
500 cfg
.init_data
= lp
->pdata
? lp
->pdata
->buck_data
[id
] : NULL
;
501 cfg
.driver_data
= buck
;
502 cfg
.regmap
= lp
->regmap
;
504 rdev
= devm_regulator_register(&pdev
->dev
, &lp8788_buck_desc
[id
], &cfg
);
507 dev_err(&pdev
->dev
, "BUCK%d regulator register err = %d\n",
512 buck
->regulator
= rdev
;
513 platform_set_drvdata(pdev
, buck
);
518 static struct platform_driver lp8788_buck_driver
= {
519 .probe
= lp8788_buck_probe
,
521 .name
= LP8788_DEV_BUCK
,
522 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
526 static int __init
lp8788_buck_init(void)
528 return platform_driver_register(&lp8788_buck_driver
);
530 subsys_initcall(lp8788_buck_init
);
532 static void __exit
lp8788_buck_exit(void)
534 platform_driver_unregister(&lp8788_buck_driver
);
536 module_exit(lp8788_buck_exit
);
538 MODULE_DESCRIPTION("TI LP8788 BUCK Driver");
539 MODULE_AUTHOR("Milo Kim");
540 MODULE_LICENSE("GPL");
541 MODULE_ALIAS("platform:lp8788-buck");