2 * TI LP8788 MFD - buck regulator driver
4 * Copyright 2012 Texas Instruments
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/mfd/lp8788.h>
20 #include <linux/gpio.h>
22 /* register address */
23 #define LP8788_EN_BUCK 0x0C
24 #define LP8788_BUCK_DVS_SEL 0x1D
25 #define LP8788_BUCK1_VOUT0 0x1E
26 #define LP8788_BUCK1_VOUT1 0x1F
27 #define LP8788_BUCK1_VOUT2 0x20
28 #define LP8788_BUCK1_VOUT3 0x21
29 #define LP8788_BUCK2_VOUT0 0x22
30 #define LP8788_BUCK2_VOUT1 0x23
31 #define LP8788_BUCK2_VOUT2 0x24
32 #define LP8788_BUCK2_VOUT3 0x25
33 #define LP8788_BUCK3_VOUT 0x26
34 #define LP8788_BUCK4_VOUT 0x27
35 #define LP8788_BUCK1_TIMESTEP 0x28
36 #define LP8788_BUCK_PWM 0x2D
39 #define LP8788_EN_BUCK1_M BIT(0) /* Addr 0Ch */
40 #define LP8788_EN_BUCK2_M BIT(1)
41 #define LP8788_EN_BUCK3_M BIT(2)
42 #define LP8788_EN_BUCK4_M BIT(3)
43 #define LP8788_BUCK1_DVS_SEL_M 0x04 /* Addr 1Dh */
44 #define LP8788_BUCK1_DVS_M 0x03
45 #define LP8788_BUCK1_DVS_S 0
46 #define LP8788_BUCK2_DVS_SEL_M 0x40
47 #define LP8788_BUCK2_DVS_M 0x30
48 #define LP8788_BUCK2_DVS_S 4
49 #define LP8788_BUCK1_DVS_I2C BIT(2)
50 #define LP8788_BUCK2_DVS_I2C BIT(6)
51 #define LP8788_BUCK1_DVS_PIN (0 << 2)
52 #define LP8788_BUCK2_DVS_PIN (0 << 6)
53 #define LP8788_VOUT_M 0x1F /* Addr 1Eh ~ 27h */
54 #define LP8788_STARTUP_TIME_M 0xF8 /* Addr 28h ~ 2Bh */
55 #define LP8788_STARTUP_TIME_S 3
56 #define LP8788_FPWM_BUCK1_M BIT(0) /* Addr 2Dh */
57 #define LP8788_FPWM_BUCK1_S 0
58 #define LP8788_FPWM_BUCK2_M BIT(1)
59 #define LP8788_FPWM_BUCK2_S 1
60 #define LP8788_FPWM_BUCK3_M BIT(2)
61 #define LP8788_FPWM_BUCK3_S 2
62 #define LP8788_FPWM_BUCK4_M BIT(3)
63 #define LP8788_FPWM_BUCK4_S 3
65 #define INVALID_ADDR 0xFF
66 #define LP8788_FORCE_PWM 1
67 #define LP8788_AUTO_PWM 0
70 #define ENABLE_TIME_USEC 32
72 #define BUCK_FPWM_MASK(x) (1 << (x))
73 #define BUCK_FPWM_SHIFT(x) (x)
75 enum lp8788_dvs_state
{
76 DVS_LOW
= GPIOF_OUT_INIT_LOW
,
77 DVS_HIGH
= GPIOF_OUT_INIT_HIGH
,
80 enum lp8788_dvs_mode
{
94 struct regulator_dev
*regulator
;
98 /* BUCK 1 ~ 4 voltage ranges */
99 static const struct regulator_linear_range buck_volt_ranges
[] = {
100 REGULATOR_LINEAR_RANGE(500000, 0, 0, 0),
101 REGULATOR_LINEAR_RANGE(800000, 1, 25, 50000),
104 static void lp8788_buck1_set_dvs(struct lp8788_buck
*buck
)
106 struct lp8788_buck1_dvs
*dvs
= (struct lp8788_buck1_dvs
*)buck
->dvs
;
107 enum lp8788_dvs_state pinstate
;
112 pinstate
= dvs
->vsel
== DVS_SEL_V0
? DVS_LOW
: DVS_HIGH
;
113 if (gpio_is_valid(dvs
->gpio
))
114 gpio_set_value(dvs
->gpio
, pinstate
);
117 static void lp8788_buck2_set_dvs(struct lp8788_buck
*buck
)
119 struct lp8788_buck2_dvs
*dvs
= (struct lp8788_buck2_dvs
*)buck
->dvs
;
120 enum lp8788_dvs_state pin1
, pin2
;
146 if (gpio_is_valid(dvs
->gpio
[0]))
147 gpio_set_value(dvs
->gpio
[0], pin1
);
149 if (gpio_is_valid(dvs
->gpio
[1]))
150 gpio_set_value(dvs
->gpio
[1], pin2
);
153 static void lp8788_set_dvs(struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
157 lp8788_buck1_set_dvs(buck
);
160 lp8788_buck2_set_dvs(buck
);
167 static enum lp8788_dvs_mode
168 lp8788_get_buck_dvs_ctrl_mode(struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
174 mask
= LP8788_BUCK1_DVS_SEL_M
;
177 mask
= LP8788_BUCK2_DVS_SEL_M
;
183 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
185 return val
& mask
? REGISTER
: EXTPIN
;
188 static bool lp8788_is_valid_buck_addr(u8 addr
)
191 case LP8788_BUCK1_VOUT0
:
192 case LP8788_BUCK1_VOUT1
:
193 case LP8788_BUCK1_VOUT2
:
194 case LP8788_BUCK1_VOUT3
:
195 case LP8788_BUCK2_VOUT0
:
196 case LP8788_BUCK2_VOUT1
:
197 case LP8788_BUCK2_VOUT2
:
198 case LP8788_BUCK2_VOUT3
:
205 static u8
lp8788_select_buck_vout_addr(struct lp8788_buck
*buck
,
206 enum lp8788_buck_id id
)
208 enum lp8788_dvs_mode mode
= lp8788_get_buck_dvs_ctrl_mode(buck
, id
);
209 struct lp8788_buck1_dvs
*b1_dvs
;
210 struct lp8788_buck2_dvs
*b2_dvs
;
216 if (mode
== EXTPIN
) {
217 b1_dvs
= (struct lp8788_buck1_dvs
*)buck
->dvs
;
221 idx
= gpio_get_value(b1_dvs
->gpio
) ? 1 : 0;
223 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
224 idx
= (val
& LP8788_BUCK1_DVS_M
) >> LP8788_BUCK1_DVS_S
;
226 addr
= LP8788_BUCK1_VOUT0
+ idx
;
229 if (mode
== EXTPIN
) {
230 b2_dvs
= (struct lp8788_buck2_dvs
*)buck
->dvs
;
234 pin1
= gpio_get_value(b2_dvs
->gpio
[0]);
235 pin2
= gpio_get_value(b2_dvs
->gpio
[1]);
237 if (pin1
== PIN_LOW
&& pin2
== PIN_LOW
)
239 else if (pin1
== PIN_LOW
&& pin2
== PIN_HIGH
)
241 else if (pin1
== PIN_HIGH
&& pin2
== PIN_LOW
)
246 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
247 idx
= (val
& LP8788_BUCK2_DVS_M
) >> LP8788_BUCK2_DVS_S
;
249 addr
= LP8788_BUCK2_VOUT0
+ idx
;
260 static int lp8788_buck12_set_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 lp8788_set_dvs(buck
, id
);
270 addr
= lp8788_select_buck_vout_addr(buck
, id
);
271 if (!lp8788_is_valid_buck_addr(addr
))
274 return lp8788_update_bits(buck
->lp
, addr
, LP8788_VOUT_M
, selector
);
277 static int lp8788_buck12_get_voltage_sel(struct regulator_dev
*rdev
)
279 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
280 enum lp8788_buck_id id
= rdev_get_id(rdev
);
284 addr
= lp8788_select_buck_vout_addr(buck
, id
);
285 if (!lp8788_is_valid_buck_addr(addr
))
288 ret
= lp8788_read_byte(buck
->lp
, addr
, &val
);
292 return val
& LP8788_VOUT_M
;
295 static int lp8788_buck_enable_time(struct regulator_dev
*rdev
)
297 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
298 enum lp8788_buck_id id
= rdev_get_id(rdev
);
299 u8 val
, addr
= LP8788_BUCK1_TIMESTEP
+ id
;
301 if (lp8788_read_byte(buck
->lp
, addr
, &val
))
304 val
= (val
& LP8788_STARTUP_TIME_M
) >> LP8788_STARTUP_TIME_S
;
306 return ENABLE_TIME_USEC
* val
;
309 static int lp8788_buck_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
311 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
312 enum lp8788_buck_id id
= rdev_get_id(rdev
);
315 mask
= BUCK_FPWM_MASK(id
);
317 case REGULATOR_MODE_FAST
:
318 val
= LP8788_FORCE_PWM
<< BUCK_FPWM_SHIFT(id
);
320 case REGULATOR_MODE_NORMAL
:
321 val
= LP8788_AUTO_PWM
<< BUCK_FPWM_SHIFT(id
);
327 return lp8788_update_bits(buck
->lp
, LP8788_BUCK_PWM
, mask
, val
);
330 static unsigned int lp8788_buck_get_mode(struct regulator_dev
*rdev
)
332 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
333 enum lp8788_buck_id id
= rdev_get_id(rdev
);
337 ret
= lp8788_read_byte(buck
->lp
, LP8788_BUCK_PWM
, &val
);
341 return val
& BUCK_FPWM_MASK(id
) ?
342 REGULATOR_MODE_FAST
: REGULATOR_MODE_NORMAL
;
345 static const struct regulator_ops lp8788_buck12_ops
= {
346 .list_voltage
= regulator_list_voltage_linear_range
,
347 .map_voltage
= regulator_map_voltage_linear_range
,
348 .set_voltage_sel
= lp8788_buck12_set_voltage_sel
,
349 .get_voltage_sel
= lp8788_buck12_get_voltage_sel
,
350 .enable
= regulator_enable_regmap
,
351 .disable
= regulator_disable_regmap
,
352 .is_enabled
= regulator_is_enabled_regmap
,
353 .enable_time
= lp8788_buck_enable_time
,
354 .set_mode
= lp8788_buck_set_mode
,
355 .get_mode
= lp8788_buck_get_mode
,
358 static const struct regulator_ops lp8788_buck34_ops
= {
359 .list_voltage
= regulator_list_voltage_linear_range
,
360 .map_voltage
= regulator_map_voltage_linear_range
,
361 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
362 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
363 .enable
= regulator_enable_regmap
,
364 .disable
= regulator_disable_regmap
,
365 .is_enabled
= regulator_is_enabled_regmap
,
366 .enable_time
= lp8788_buck_enable_time
,
367 .set_mode
= lp8788_buck_set_mode
,
368 .get_mode
= lp8788_buck_get_mode
,
371 static const struct regulator_desc lp8788_buck_desc
[] = {
375 .ops
= &lp8788_buck12_ops
,
377 .linear_ranges
= buck_volt_ranges
,
378 .n_linear_ranges
= ARRAY_SIZE(buck_volt_ranges
),
379 .type
= REGULATOR_VOLTAGE
,
380 .owner
= THIS_MODULE
,
381 .enable_reg
= LP8788_EN_BUCK
,
382 .enable_mask
= LP8788_EN_BUCK1_M
,
387 .ops
= &lp8788_buck12_ops
,
389 .linear_ranges
= buck_volt_ranges
,
390 .n_linear_ranges
= ARRAY_SIZE(buck_volt_ranges
),
391 .type
= REGULATOR_VOLTAGE
,
392 .owner
= THIS_MODULE
,
393 .enable_reg
= LP8788_EN_BUCK
,
394 .enable_mask
= LP8788_EN_BUCK2_M
,
399 .ops
= &lp8788_buck34_ops
,
401 .linear_ranges
= buck_volt_ranges
,
402 .n_linear_ranges
= ARRAY_SIZE(buck_volt_ranges
),
403 .type
= REGULATOR_VOLTAGE
,
404 .owner
= THIS_MODULE
,
405 .vsel_reg
= LP8788_BUCK3_VOUT
,
406 .vsel_mask
= LP8788_VOUT_M
,
407 .enable_reg
= LP8788_EN_BUCK
,
408 .enable_mask
= LP8788_EN_BUCK3_M
,
413 .ops
= &lp8788_buck34_ops
,
415 .linear_ranges
= buck_volt_ranges
,
416 .n_linear_ranges
= ARRAY_SIZE(buck_volt_ranges
),
417 .type
= REGULATOR_VOLTAGE
,
418 .owner
= THIS_MODULE
,
419 .vsel_reg
= LP8788_BUCK4_VOUT
,
420 .vsel_mask
= LP8788_VOUT_M
,
421 .enable_reg
= LP8788_EN_BUCK
,
422 .enable_mask
= LP8788_EN_BUCK4_M
,
426 static int lp8788_dvs_gpio_request(struct platform_device
*pdev
,
427 struct lp8788_buck
*buck
,
428 enum lp8788_buck_id id
)
430 struct lp8788_platform_data
*pdata
= buck
->lp
->pdata
;
431 char *b1_name
= "LP8788_B1_DVS";
432 char *b2_name
[] = { "LP8788_B2_DVS1", "LP8788_B2_DVS2" };
437 gpio
= pdata
->buck1_dvs
->gpio
;
438 ret
= devm_gpio_request_one(&pdev
->dev
, gpio
, DVS_LOW
,
443 buck
->dvs
= pdata
->buck1_dvs
;
446 for (i
= 0; i
< LP8788_NUM_BUCK2_DVS
; i
++) {
447 gpio
= pdata
->buck2_dvs
->gpio
[i
];
448 ret
= devm_gpio_request_one(&pdev
->dev
, gpio
,
449 DVS_LOW
, b2_name
[i
]);
453 buck
->dvs
= pdata
->buck2_dvs
;
462 static int lp8788_init_dvs(struct platform_device
*pdev
,
463 struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
465 struct lp8788_platform_data
*pdata
= buck
->lp
->pdata
;
466 u8 mask
[] = { LP8788_BUCK1_DVS_SEL_M
, LP8788_BUCK2_DVS_SEL_M
};
467 u8 val
[] = { LP8788_BUCK1_DVS_PIN
, LP8788_BUCK2_DVS_PIN
};
468 u8 default_dvs_mode
[] = { LP8788_BUCK1_DVS_I2C
, LP8788_BUCK2_DVS_I2C
};
470 /* no dvs for buck3, 4 */
474 /* no dvs platform data, then dvs will be selected by I2C registers */
476 goto set_default_dvs_mode
;
478 if ((id
== BUCK1
&& !pdata
->buck1_dvs
) ||
479 (id
== BUCK2
&& !pdata
->buck2_dvs
))
480 goto set_default_dvs_mode
;
482 if (lp8788_dvs_gpio_request(pdev
, buck
, id
))
483 goto set_default_dvs_mode
;
485 return lp8788_update_bits(buck
->lp
, LP8788_BUCK_DVS_SEL
, mask
[id
],
488 set_default_dvs_mode
:
489 return lp8788_update_bits(buck
->lp
, LP8788_BUCK_DVS_SEL
, mask
[id
],
490 default_dvs_mode
[id
]);
493 static int lp8788_buck_probe(struct platform_device
*pdev
)
495 struct lp8788
*lp
= dev_get_drvdata(pdev
->dev
.parent
);
497 struct lp8788_buck
*buck
;
498 struct regulator_config cfg
= { };
499 struct regulator_dev
*rdev
;
502 if (id
>= LP8788_NUM_BUCKS
)
505 buck
= devm_kzalloc(&pdev
->dev
, sizeof(struct lp8788_buck
), GFP_KERNEL
);
511 ret
= lp8788_init_dvs(pdev
, buck
, id
);
515 cfg
.dev
= pdev
->dev
.parent
;
516 cfg
.init_data
= lp
->pdata
? lp
->pdata
->buck_data
[id
] : NULL
;
517 cfg
.driver_data
= buck
;
518 cfg
.regmap
= lp
->regmap
;
520 rdev
= devm_regulator_register(&pdev
->dev
, &lp8788_buck_desc
[id
], &cfg
);
523 dev_err(&pdev
->dev
, "BUCK%d regulator register err = %d\n",
528 buck
->regulator
= rdev
;
529 platform_set_drvdata(pdev
, buck
);
534 static struct platform_driver lp8788_buck_driver
= {
535 .probe
= lp8788_buck_probe
,
537 .name
= LP8788_DEV_BUCK
,
541 static int __init
lp8788_buck_init(void)
543 return platform_driver_register(&lp8788_buck_driver
);
545 subsys_initcall(lp8788_buck_init
);
547 static void __exit
lp8788_buck_exit(void)
549 platform_driver_unregister(&lp8788_buck_driver
);
551 module_exit(lp8788_buck_exit
);
553 MODULE_DESCRIPTION("TI LP8788 BUCK Driver");
554 MODULE_AUTHOR("Milo Kim");
555 MODULE_LICENSE("GPL");
556 MODULE_ALIAS("platform:lp8788-buck");