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 table */
99 static const int lp8788_buck_vtbl[] = {
100 500000, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
101 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
102 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
106 static const u8 buck1_vout_addr[] = {
107 LP8788_BUCK1_VOUT0, LP8788_BUCK1_VOUT1,
108 LP8788_BUCK1_VOUT2, LP8788_BUCK1_VOUT3,
111 static const u8 buck2_vout_addr[] = {
112 LP8788_BUCK2_VOUT0, LP8788_BUCK2_VOUT1,
113 LP8788_BUCK2_VOUT2, LP8788_BUCK2_VOUT3,
116 static void lp8788_buck1_set_dvs(struct lp8788_buck *buck)
118 struct lp8788_buck1_dvs *dvs = (struct lp8788_buck1_dvs *)buck->dvs;
119 enum lp8788_dvs_state pinstate;
124 pinstate = dvs->vsel == DVS_SEL_V0 ? DVS_LOW : DVS_HIGH;
125 if (gpio_is_valid(dvs->gpio))
126 gpio_set_value(dvs->gpio, pinstate);
129 static void lp8788_buck2_set_dvs(struct lp8788_buck *buck)
131 struct lp8788_buck2_dvs *dvs = (struct lp8788_buck2_dvs *)buck->dvs;
132 enum lp8788_dvs_state pin1, pin2;
158 if (gpio_is_valid(dvs->gpio[0]))
159 gpio_set_value(dvs->gpio[0], pin1);
161 if (gpio_is_valid(dvs->gpio[1]))
162 gpio_set_value(dvs->gpio[1], pin2);
165 static void lp8788_set_dvs(struct lp8788_buck *buck, enum lp8788_buck_id id)
169 lp8788_buck1_set_dvs(buck);
172 lp8788_buck2_set_dvs(buck);
179 static enum lp8788_dvs_mode
180 lp8788_get_buck_dvs_ctrl_mode(struct lp8788_buck *buck, enum lp8788_buck_id id)
186 mask = LP8788_BUCK1_DVS_SEL_M;
189 mask = LP8788_BUCK2_DVS_SEL_M;
195 lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
197 return val & mask ? REGISTER : EXTPIN;
200 static bool lp8788_is_valid_buck_addr(u8 addr)
203 case LP8788_BUCK1_VOUT0:
204 case LP8788_BUCK1_VOUT1:
205 case LP8788_BUCK1_VOUT2:
206 case LP8788_BUCK1_VOUT3:
207 case LP8788_BUCK2_VOUT0:
208 case LP8788_BUCK2_VOUT1:
209 case LP8788_BUCK2_VOUT2:
210 case LP8788_BUCK2_VOUT3:
217 static u8 lp8788_select_buck_vout_addr(struct lp8788_buck *buck,
218 enum lp8788_buck_id id)
220 enum lp8788_dvs_mode mode = lp8788_get_buck_dvs_ctrl_mode(buck, id);
221 struct lp8788_buck1_dvs *b1_dvs;
222 struct lp8788_buck2_dvs *b2_dvs;
228 if (mode == EXTPIN) {
229 b1_dvs = (struct lp8788_buck1_dvs *)buck->dvs;
233 idx = gpio_get_value(b1_dvs->gpio) ? 1 : 0;
235 lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
236 idx = (val & LP8788_BUCK1_DVS_M) >> LP8788_BUCK1_DVS_S;
238 addr = buck1_vout_addr[idx];
241 if (mode == EXTPIN) {
242 b2_dvs = (struct lp8788_buck2_dvs *)buck->dvs;
246 pin1 = gpio_get_value(b2_dvs->gpio[0]);
247 pin2 = gpio_get_value(b2_dvs->gpio[1]);
249 if (pin1 == PIN_LOW && pin2 == PIN_LOW)
251 else if (pin1 == PIN_LOW && pin2 == PIN_HIGH)
253 else if (pin1 == PIN_HIGH && pin2 == PIN_LOW)
258 lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
259 idx = (val & LP8788_BUCK2_DVS_M) >> LP8788_BUCK2_DVS_S;
261 addr = buck2_vout_addr[idx];
272 static int lp8788_buck12_set_voltage_sel(struct regulator_dev *rdev,
275 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
276 enum lp8788_buck_id id = rdev_get_id(rdev);
280 lp8788_set_dvs(buck, id);
282 addr = lp8788_select_buck_vout_addr(buck, id);
283 if (!lp8788_is_valid_buck_addr(addr))
286 return lp8788_update_bits(buck->lp, addr, LP8788_VOUT_M, selector);
289 static int lp8788_buck12_get_voltage_sel(struct regulator_dev *rdev)
291 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
292 enum lp8788_buck_id id = rdev_get_id(rdev);
296 addr = lp8788_select_buck_vout_addr(buck, id);
297 if (!lp8788_is_valid_buck_addr(addr))
300 ret = lp8788_read_byte(buck->lp, addr, &val);
304 return val & LP8788_VOUT_M;
307 static int lp8788_buck_enable_time(struct regulator_dev *rdev)
309 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
310 enum lp8788_buck_id id = rdev_get_id(rdev);
311 u8 val, addr = LP8788_BUCK1_TIMESTEP + id;
313 if (lp8788_read_byte(buck->lp, addr, &val))
316 val = (val & LP8788_STARTUP_TIME_M) >> LP8788_STARTUP_TIME_S;
318 return ENABLE_TIME_USEC * val;
321 static int lp8788_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
323 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
324 enum lp8788_buck_id id = rdev_get_id(rdev);
327 mask = BUCK_FPWM_MASK(id);
329 case REGULATOR_MODE_FAST:
330 val = LP8788_FORCE_PWM << BUCK_FPWM_SHIFT(id);
332 case REGULATOR_MODE_NORMAL:
333 val = LP8788_AUTO_PWM << BUCK_FPWM_SHIFT(id);
339 return lp8788_update_bits(buck->lp, LP8788_BUCK_PWM, mask, val);
342 static unsigned int lp8788_buck_get_mode(struct regulator_dev *rdev)
344 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
345 enum lp8788_buck_id id = rdev_get_id(rdev);
349 ret = lp8788_read_byte(buck->lp, LP8788_BUCK_PWM, &val);
353 return val & BUCK_FPWM_MASK(id) ?
354 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
357 static struct regulator_ops lp8788_buck12_ops = {
358 .list_voltage = regulator_list_voltage_table,
359 .set_voltage_sel = lp8788_buck12_set_voltage_sel,
360 .get_voltage_sel = lp8788_buck12_get_voltage_sel,
361 .enable = regulator_enable_regmap,
362 .disable = regulator_disable_regmap,
363 .is_enabled = regulator_is_enabled_regmap,
364 .enable_time = lp8788_buck_enable_time,
365 .set_mode = lp8788_buck_set_mode,
366 .get_mode = lp8788_buck_get_mode,
369 static struct regulator_ops lp8788_buck34_ops = {
370 .list_voltage = regulator_list_voltage_table,
371 .set_voltage_sel = regulator_set_voltage_sel_regmap,
372 .get_voltage_sel = regulator_get_voltage_sel_regmap,
373 .enable = regulator_enable_regmap,
374 .disable = regulator_disable_regmap,
375 .is_enabled = regulator_is_enabled_regmap,
376 .enable_time = lp8788_buck_enable_time,
377 .set_mode = lp8788_buck_set_mode,
378 .get_mode = lp8788_buck_get_mode,
381 static struct regulator_desc lp8788_buck_desc[] = {
385 .ops = &lp8788_buck12_ops,
386 .n_voltages = ARRAY_SIZE(lp8788_buck_vtbl),
387 .volt_table = lp8788_buck_vtbl,
388 .type = REGULATOR_VOLTAGE,
389 .owner = THIS_MODULE,
390 .enable_reg = LP8788_EN_BUCK,
391 .enable_mask = LP8788_EN_BUCK1_M,
396 .ops = &lp8788_buck12_ops,
397 .n_voltages = ARRAY_SIZE(lp8788_buck_vtbl),
398 .volt_table = lp8788_buck_vtbl,
399 .type = REGULATOR_VOLTAGE,
400 .owner = THIS_MODULE,
401 .enable_reg = LP8788_EN_BUCK,
402 .enable_mask = LP8788_EN_BUCK2_M,
407 .ops = &lp8788_buck34_ops,
408 .n_voltages = ARRAY_SIZE(lp8788_buck_vtbl),
409 .volt_table = lp8788_buck_vtbl,
410 .type = REGULATOR_VOLTAGE,
411 .owner = THIS_MODULE,
412 .vsel_reg = LP8788_BUCK3_VOUT,
413 .vsel_mask = LP8788_VOUT_M,
414 .enable_reg = LP8788_EN_BUCK,
415 .enable_mask = LP8788_EN_BUCK3_M,
420 .ops = &lp8788_buck34_ops,
421 .n_voltages = ARRAY_SIZE(lp8788_buck_vtbl),
422 .volt_table = lp8788_buck_vtbl,
423 .type = REGULATOR_VOLTAGE,
424 .owner = THIS_MODULE,
425 .vsel_reg = LP8788_BUCK4_VOUT,
426 .vsel_mask = LP8788_VOUT_M,
427 .enable_reg = LP8788_EN_BUCK,
428 .enable_mask = LP8788_EN_BUCK4_M,
432 static int _gpio_request(struct lp8788_buck *buck, int gpio, char *name)
434 struct device *dev = buck->lp->dev;
436 if (!gpio_is_valid(gpio)) {
437 dev_err(dev, "invalid gpio: %d\n", gpio);
441 return devm_gpio_request_one(dev, gpio, DVS_LOW, name);
444 static int lp8788_dvs_gpio_request(struct lp8788_buck *buck,
445 enum lp8788_buck_id id)
447 struct lp8788_platform_data *pdata = buck->lp->pdata;
448 char *b1_name = "LP8788_B1_DVS";
449 char *b2_name[] = { "LP8788_B2_DVS1", "LP8788_B2_DVS2" };
454 gpio = pdata->buck1_dvs->gpio;
455 ret = _gpio_request(buck, gpio, b1_name);
459 buck->dvs = pdata->buck1_dvs;
462 for (i = 0 ; i < LP8788_NUM_BUCK2_DVS ; i++) {
463 gpio = pdata->buck2_dvs->gpio[i];
464 ret = _gpio_request(buck, gpio, b2_name[i]);
468 buck->dvs = pdata->buck2_dvs;
477 static int lp8788_init_dvs(struct lp8788_buck *buck, enum lp8788_buck_id id)
479 struct lp8788_platform_data *pdata = buck->lp->pdata;
480 u8 mask[] = { LP8788_BUCK1_DVS_SEL_M, LP8788_BUCK2_DVS_SEL_M };
481 u8 val[] = { LP8788_BUCK1_DVS_PIN, LP8788_BUCK2_DVS_PIN };
482 u8 default_dvs_mode[] = { LP8788_BUCK1_DVS_I2C, LP8788_BUCK2_DVS_I2C };
484 /* no dvs for buck3, 4 */
485 if (id == BUCK3 || id == BUCK4)
488 /* no dvs platform data, then dvs will be selected by I2C registers */
490 goto set_default_dvs_mode;
492 if ((id == BUCK1 && !pdata->buck1_dvs) ||
493 (id == BUCK2 && !pdata->buck2_dvs))
494 goto set_default_dvs_mode;
496 if (lp8788_dvs_gpio_request(buck, id))
497 goto set_default_dvs_mode;
499 return lp8788_update_bits(buck->lp, LP8788_BUCK_DVS_SEL, mask[id],
502 set_default_dvs_mode:
503 return lp8788_update_bits(buck->lp, LP8788_BUCK_DVS_SEL, mask[id],
504 default_dvs_mode[id]);
507 static __devinit int lp8788_buck_probe(struct platform_device *pdev)
509 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
511 struct lp8788_buck *buck;
512 struct regulator_config cfg = { };
513 struct regulator_dev *rdev;
516 buck = devm_kzalloc(lp->dev, sizeof(struct lp8788_buck), GFP_KERNEL);
522 ret = lp8788_init_dvs(buck, id);
527 cfg.init_data = lp->pdata ? lp->pdata->buck_data[id] : NULL;
528 cfg.driver_data = buck;
529 cfg.regmap = lp->regmap;
531 rdev = regulator_register(&lp8788_buck_desc[id], &cfg);
534 dev_err(lp->dev, "BUCK%d regulator register err = %d\n",
539 buck->regulator = rdev;
540 platform_set_drvdata(pdev, buck);
545 static int __devexit lp8788_buck_remove(struct platform_device *pdev)
547 struct lp8788_buck *buck = platform_get_drvdata(pdev);
549 platform_set_drvdata(pdev, NULL);
550 regulator_unregister(buck->regulator);
555 static struct platform_driver lp8788_buck_driver = {
556 .probe = lp8788_buck_probe,
557 .remove = __devexit_p(lp8788_buck_remove),
559 .name = LP8788_DEV_BUCK,
560 .owner = THIS_MODULE,
564 static int __init lp8788_buck_init(void)
566 return platform_driver_register(&lp8788_buck_driver);
568 subsys_initcall(lp8788_buck_init);
570 static void __exit lp8788_buck_exit(void)
572 platform_driver_unregister(&lp8788_buck_driver);
574 module_exit(lp8788_buck_exit);
576 MODULE_DESCRIPTION("TI LP8788 BUCK Driver");
577 MODULE_AUTHOR("Milo Kim");
578 MODULE_LICENSE("GPL");
579 MODULE_ALIAS("platform:lp8788-buck");