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 enum lp8788_dvs_state
{
73 DVS_LOW
= GPIOF_OUT_INIT_LOW
,
74 DVS_HIGH
= GPIOF_OUT_INIT_HIGH
,
77 enum lp8788_dvs_mode
{
89 struct lp8788_pwm_map
{
96 struct regulator_dev
*regulator
;
97 struct lp8788_pwm_map
*pmap
;
101 /* BUCK 1 ~ 4 voltage table */
102 static const int lp8788_buck_vtbl
[] = {
103 500000, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
104 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
105 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
109 /* buck pwm mode selection : used for set/get_mode in regulator ops
110 * @forced pwm : fast mode
111 * @auto pwm : normal mode
113 static struct lp8788_pwm_map buck_pmap
[] = {
115 .mask
= LP8788_FPWM_BUCK1_M
,
116 .shift
= LP8788_FPWM_BUCK1_S
,
119 .mask
= LP8788_FPWM_BUCK2_M
,
120 .shift
= LP8788_FPWM_BUCK2_S
,
123 .mask
= LP8788_FPWM_BUCK3_M
,
124 .shift
= LP8788_FPWM_BUCK3_S
,
127 .mask
= LP8788_FPWM_BUCK4_M
,
128 .shift
= LP8788_FPWM_BUCK4_S
,
132 static const u8 buck1_vout_addr
[] = {
133 LP8788_BUCK1_VOUT0
, LP8788_BUCK1_VOUT1
,
134 LP8788_BUCK1_VOUT2
, LP8788_BUCK1_VOUT3
,
137 static const u8 buck2_vout_addr
[] = {
138 LP8788_BUCK2_VOUT0
, LP8788_BUCK2_VOUT1
,
139 LP8788_BUCK2_VOUT2
, LP8788_BUCK2_VOUT3
,
142 static void lp8788_buck1_set_dvs(struct lp8788_buck
*buck
)
144 struct lp8788_buck1_dvs
*dvs
= (struct lp8788_buck1_dvs
*)buck
->dvs
;
145 enum lp8788_dvs_state pinstate
;
150 pinstate
= dvs
->vsel
== DVS_SEL_V0
? DVS_LOW
: DVS_HIGH
;
151 if (gpio_is_valid(dvs
->gpio
))
152 gpio_set_value(dvs
->gpio
, pinstate
);
155 static void lp8788_buck2_set_dvs(struct lp8788_buck
*buck
)
157 struct lp8788_buck2_dvs
*dvs
= (struct lp8788_buck2_dvs
*)buck
->dvs
;
158 enum lp8788_dvs_state pin1
, pin2
;
184 if (gpio_is_valid(dvs
->gpio
[0]))
185 gpio_set_value(dvs
->gpio
[0], pin1
);
187 if (gpio_is_valid(dvs
->gpio
[1]))
188 gpio_set_value(dvs
->gpio
[1], pin2
);
191 static void lp8788_set_dvs(struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
195 lp8788_buck1_set_dvs(buck
);
198 lp8788_buck2_set_dvs(buck
);
205 static enum lp8788_dvs_mode
206 lp8788_get_buck_dvs_ctrl_mode(struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
212 mask
= LP8788_BUCK1_DVS_SEL_M
;
215 mask
= LP8788_BUCK2_DVS_SEL_M
;
221 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
223 return val
& mask
? REGISTER
: EXTPIN
;
226 static bool lp8788_is_valid_buck_addr(u8 addr
)
229 case LP8788_BUCK1_VOUT0
:
230 case LP8788_BUCK1_VOUT1
:
231 case LP8788_BUCK1_VOUT2
:
232 case LP8788_BUCK1_VOUT3
:
233 case LP8788_BUCK2_VOUT0
:
234 case LP8788_BUCK2_VOUT1
:
235 case LP8788_BUCK2_VOUT2
:
236 case LP8788_BUCK2_VOUT3
:
243 static u8
lp8788_select_buck_vout_addr(struct lp8788_buck
*buck
,
244 enum lp8788_buck_id id
)
246 enum lp8788_dvs_mode mode
= lp8788_get_buck_dvs_ctrl_mode(buck
, id
);
247 struct lp8788_buck1_dvs
*b1_dvs
;
248 struct lp8788_buck2_dvs
*b2_dvs
;
254 if (mode
== EXTPIN
) {
255 b1_dvs
= (struct lp8788_buck1_dvs
*)buck
->dvs
;
259 idx
= gpio_get_value(b1_dvs
->gpio
) ? 1 : 0;
261 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
262 idx
= (val
& LP8788_BUCK1_DVS_M
) >> LP8788_BUCK1_DVS_S
;
264 addr
= buck1_vout_addr
[idx
];
267 if (mode
== EXTPIN
) {
268 b2_dvs
= (struct lp8788_buck2_dvs
*)buck
->dvs
;
272 pin1
= gpio_get_value(b2_dvs
->gpio
[0]);
273 pin2
= gpio_get_value(b2_dvs
->gpio
[1]);
275 if (pin1
== PIN_LOW
&& pin2
== PIN_LOW
)
277 else if (pin1
== PIN_LOW
&& pin2
== PIN_HIGH
)
279 else if (pin1
== PIN_HIGH
&& pin2
== PIN_LOW
)
284 lp8788_read_byte(buck
->lp
, LP8788_BUCK_DVS_SEL
, &val
);
285 idx
= (val
& LP8788_BUCK2_DVS_M
) >> LP8788_BUCK2_DVS_S
;
287 addr
= buck2_vout_addr
[idx
];
298 static int lp8788_buck12_set_voltage_sel(struct regulator_dev
*rdev
,
301 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
302 enum lp8788_buck_id id
= rdev_get_id(rdev
);
306 lp8788_set_dvs(buck
, id
);
308 addr
= lp8788_select_buck_vout_addr(buck
, id
);
309 if (!lp8788_is_valid_buck_addr(addr
))
312 return lp8788_update_bits(buck
->lp
, addr
, LP8788_VOUT_M
, selector
);
315 static int lp8788_buck12_get_voltage_sel(struct regulator_dev
*rdev
)
317 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
318 enum lp8788_buck_id id
= rdev_get_id(rdev
);
322 addr
= lp8788_select_buck_vout_addr(buck
, id
);
323 if (!lp8788_is_valid_buck_addr(addr
))
326 ret
= lp8788_read_byte(buck
->lp
, addr
, &val
);
330 return val
& LP8788_VOUT_M
;
333 static int lp8788_buck_enable_time(struct regulator_dev
*rdev
)
335 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
336 enum lp8788_buck_id id
= rdev_get_id(rdev
);
337 u8 val
, addr
= LP8788_BUCK1_TIMESTEP
+ id
;
339 if (lp8788_read_byte(buck
->lp
, addr
, &val
))
342 val
= (val
& LP8788_STARTUP_TIME_M
) >> LP8788_STARTUP_TIME_S
;
344 return ENABLE_TIME_USEC
* val
;
347 static int lp8788_buck_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
349 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
350 struct lp8788_pwm_map
*pmap
= buck
->pmap
;
357 case REGULATOR_MODE_FAST
:
358 val
= LP8788_FORCE_PWM
<< pmap
->shift
;
360 case REGULATOR_MODE_NORMAL
:
361 val
= LP8788_AUTO_PWM
<< pmap
->shift
;
367 return lp8788_update_bits(buck
->lp
, LP8788_BUCK_PWM
, pmap
->mask
, val
);
370 static unsigned int lp8788_buck_get_mode(struct regulator_dev
*rdev
)
372 struct lp8788_buck
*buck
= rdev_get_drvdata(rdev
);
373 struct lp8788_pwm_map
*pmap
= buck
->pmap
;
380 ret
= lp8788_read_byte(buck
->lp
, LP8788_BUCK_PWM
, &val
);
384 return val
& pmap
->mask
? REGULATOR_MODE_FAST
: REGULATOR_MODE_NORMAL
;
387 static struct regulator_ops lp8788_buck12_ops
= {
388 .list_voltage
= regulator_list_voltage_table
,
389 .set_voltage_sel
= lp8788_buck12_set_voltage_sel
,
390 .get_voltage_sel
= lp8788_buck12_get_voltage_sel
,
391 .enable
= regulator_enable_regmap
,
392 .disable
= regulator_disable_regmap
,
393 .is_enabled
= regulator_is_enabled_regmap
,
394 .enable_time
= lp8788_buck_enable_time
,
395 .set_mode
= lp8788_buck_set_mode
,
396 .get_mode
= lp8788_buck_get_mode
,
399 static struct regulator_ops lp8788_buck34_ops
= {
400 .list_voltage
= regulator_list_voltage_table
,
401 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
402 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
403 .enable
= regulator_enable_regmap
,
404 .disable
= regulator_disable_regmap
,
405 .is_enabled
= regulator_is_enabled_regmap
,
406 .enable_time
= lp8788_buck_enable_time
,
407 .set_mode
= lp8788_buck_set_mode
,
408 .get_mode
= lp8788_buck_get_mode
,
411 static struct regulator_desc lp8788_buck_desc
[] = {
415 .ops
= &lp8788_buck12_ops
,
416 .n_voltages
= ARRAY_SIZE(lp8788_buck_vtbl
),
417 .volt_table
= lp8788_buck_vtbl
,
418 .type
= REGULATOR_VOLTAGE
,
419 .owner
= THIS_MODULE
,
420 .enable_reg
= LP8788_EN_BUCK
,
421 .enable_mask
= LP8788_EN_BUCK1_M
,
426 .ops
= &lp8788_buck12_ops
,
427 .n_voltages
= ARRAY_SIZE(lp8788_buck_vtbl
),
428 .volt_table
= lp8788_buck_vtbl
,
429 .type
= REGULATOR_VOLTAGE
,
430 .owner
= THIS_MODULE
,
431 .enable_reg
= LP8788_EN_BUCK
,
432 .enable_mask
= LP8788_EN_BUCK2_M
,
437 .ops
= &lp8788_buck34_ops
,
438 .n_voltages
= ARRAY_SIZE(lp8788_buck_vtbl
),
439 .volt_table
= lp8788_buck_vtbl
,
440 .type
= REGULATOR_VOLTAGE
,
441 .owner
= THIS_MODULE
,
442 .vsel_reg
= LP8788_BUCK3_VOUT
,
443 .vsel_mask
= LP8788_VOUT_M
,
444 .enable_reg
= LP8788_EN_BUCK
,
445 .enable_mask
= LP8788_EN_BUCK3_M
,
450 .ops
= &lp8788_buck34_ops
,
451 .n_voltages
= ARRAY_SIZE(lp8788_buck_vtbl
),
452 .volt_table
= lp8788_buck_vtbl
,
453 .type
= REGULATOR_VOLTAGE
,
454 .owner
= THIS_MODULE
,
455 .vsel_reg
= LP8788_BUCK4_VOUT
,
456 .vsel_mask
= LP8788_VOUT_M
,
457 .enable_reg
= LP8788_EN_BUCK
,
458 .enable_mask
= LP8788_EN_BUCK4_M
,
462 static int lp8788_set_default_dvs_ctrl_mode(struct lp8788
*lp
,
463 enum lp8788_buck_id id
)
469 mask
= LP8788_BUCK1_DVS_SEL_M
;
470 val
= LP8788_BUCK1_DVS_I2C
;
473 mask
= LP8788_BUCK2_DVS_SEL_M
;
474 val
= LP8788_BUCK2_DVS_I2C
;
480 return lp8788_update_bits(lp
, LP8788_BUCK_DVS_SEL
, mask
, val
);
483 static int _gpio_request(struct lp8788_buck
*buck
, int gpio
, char *name
)
485 struct device
*dev
= buck
->lp
->dev
;
487 if (!gpio_is_valid(gpio
)) {
488 dev_err(dev
, "invalid gpio: %d\n", gpio
);
492 return devm_gpio_request_one(dev
, gpio
, DVS_LOW
, name
);
495 static int lp8788_dvs_gpio_request(struct lp8788_buck
*buck
,
496 enum lp8788_buck_id id
)
498 struct lp8788_platform_data
*pdata
= buck
->lp
->pdata
;
499 char *b1_name
= "LP8788_B1_DVS";
500 char *b2_name
[] = { "LP8788_B2_DVS1", "LP8788_B2_DVS2" };
505 gpio
= pdata
->buck1_dvs
->gpio
;
506 ret
= _gpio_request(buck
, gpio
, b1_name
);
510 buck
->dvs
= pdata
->buck1_dvs
;
513 for (i
= 0 ; i
< LP8788_NUM_BUCK2_DVS
; i
++) {
514 gpio
= pdata
->buck2_dvs
->gpio
[i
];
515 ret
= _gpio_request(buck
, gpio
, b2_name
[i
]);
519 buck
->dvs
= pdata
->buck2_dvs
;
528 static int lp8788_init_dvs(struct lp8788_buck
*buck
, enum lp8788_buck_id id
)
530 struct lp8788_platform_data
*pdata
= buck
->lp
->pdata
;
531 u8 mask
[] = { LP8788_BUCK1_DVS_SEL_M
, LP8788_BUCK2_DVS_SEL_M
};
532 u8 val
[] = { LP8788_BUCK1_DVS_PIN
, LP8788_BUCK2_DVS_PIN
};
534 /* no dvs for buck3, 4 */
535 if (id
== BUCK3
|| id
== BUCK4
)
538 /* no dvs platform data, then dvs will be selected by I2C registers */
540 goto set_default_dvs_mode
;
542 if ((id
== BUCK1
&& !pdata
->buck1_dvs
) ||
543 (id
== BUCK2
&& !pdata
->buck2_dvs
))
544 goto set_default_dvs_mode
;
546 if (lp8788_dvs_gpio_request(buck
, id
))
547 goto set_default_dvs_mode
;
549 return lp8788_update_bits(buck
->lp
, LP8788_BUCK_DVS_SEL
, mask
[id
],
552 set_default_dvs_mode
:
553 return lp8788_set_default_dvs_ctrl_mode(buck
->lp
, id
);
556 static __devinit
int lp8788_buck_probe(struct platform_device
*pdev
)
558 struct lp8788
*lp
= dev_get_drvdata(pdev
->dev
.parent
);
560 struct lp8788_buck
*buck
;
561 struct regulator_config cfg
= { };
562 struct regulator_dev
*rdev
;
565 buck
= devm_kzalloc(lp
->dev
, sizeof(struct lp8788_buck
), GFP_KERNEL
);
570 buck
->pmap
= &buck_pmap
[id
];
572 ret
= lp8788_init_dvs(buck
, id
);
577 cfg
.init_data
= lp
->pdata
? lp
->pdata
->buck_data
[id
] : NULL
;
578 cfg
.driver_data
= buck
;
579 cfg
.regmap
= lp
->regmap
;
581 rdev
= regulator_register(&lp8788_buck_desc
[id
], &cfg
);
584 dev_err(lp
->dev
, "BUCK%d regulator register err = %d\n",
589 buck
->regulator
= rdev
;
590 platform_set_drvdata(pdev
, buck
);
595 static int __devexit
lp8788_buck_remove(struct platform_device
*pdev
)
597 struct lp8788_buck
*buck
= platform_get_drvdata(pdev
);
599 platform_set_drvdata(pdev
, NULL
);
600 regulator_unregister(buck
->regulator
);
605 static struct platform_driver lp8788_buck_driver
= {
606 .probe
= lp8788_buck_probe
,
607 .remove
= __devexit_p(lp8788_buck_remove
),
609 .name
= LP8788_DEV_BUCK
,
610 .owner
= THIS_MODULE
,
614 static int __init
lp8788_buck_init(void)
616 return platform_driver_register(&lp8788_buck_driver
);
618 subsys_initcall(lp8788_buck_init
);
620 static void __exit
lp8788_buck_exit(void)
622 platform_driver_unregister(&lp8788_buck_driver
);
624 module_exit(lp8788_buck_exit
);
626 MODULE_DESCRIPTION("TI LP8788 BUCK Driver");
627 MODULE_AUTHOR("Milo Kim");
628 MODULE_LICENSE("GPL");
629 MODULE_ALIAS("platform:lp8788-buck");