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.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 {
72 DVS_LOW = GPIOF_OUT_INIT_LOW,
73 DVS_HIGH = GPIOF_OUT_INIT_HIGH,
76 enum lp8788_dvs_mode {
90 struct regulator_dev *regulator;
94 /* BUCK 1 ~ 4 voltage ranges */
95 static const struct linear_range buck_volt_ranges[] = {
96 REGULATOR_LINEAR_RANGE(500000, 0, 0, 0),
97 REGULATOR_LINEAR_RANGE(800000, 1, 25, 50000),
100 static void lp8788_buck1_set_dvs(struct lp8788_buck *buck)
102 struct lp8788_buck1_dvs *dvs = (struct lp8788_buck1_dvs *)buck->dvs;
103 enum lp8788_dvs_state pinstate;
108 pinstate = dvs->vsel == DVS_SEL_V0 ? DVS_LOW : DVS_HIGH;
109 if (gpio_is_valid(dvs->gpio))
110 gpio_set_value(dvs->gpio, pinstate);
113 static void lp8788_buck2_set_dvs(struct lp8788_buck *buck)
115 struct lp8788_buck2_dvs *dvs = (struct lp8788_buck2_dvs *)buck->dvs;
116 enum lp8788_dvs_state pin1, pin2;
142 if (gpio_is_valid(dvs->gpio[0]))
143 gpio_set_value(dvs->gpio[0], pin1);
145 if (gpio_is_valid(dvs->gpio[1]))
146 gpio_set_value(dvs->gpio[1], pin2);
149 static void lp8788_set_dvs(struct lp8788_buck *buck, enum lp8788_buck_id id)
153 lp8788_buck1_set_dvs(buck);
156 lp8788_buck2_set_dvs(buck);
163 static enum lp8788_dvs_mode
164 lp8788_get_buck_dvs_ctrl_mode(struct lp8788_buck *buck, enum lp8788_buck_id id)
170 mask = LP8788_BUCK1_DVS_SEL_M;
173 mask = LP8788_BUCK2_DVS_SEL_M;
179 lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
181 return val & mask ? REGISTER : EXTPIN;
184 static bool lp8788_is_valid_buck_addr(u8 addr)
187 case LP8788_BUCK1_VOUT0:
188 case LP8788_BUCK1_VOUT1:
189 case LP8788_BUCK1_VOUT2:
190 case LP8788_BUCK1_VOUT3:
191 case LP8788_BUCK2_VOUT0:
192 case LP8788_BUCK2_VOUT1:
193 case LP8788_BUCK2_VOUT2:
194 case LP8788_BUCK2_VOUT3:
201 static u8 lp8788_select_buck_vout_addr(struct lp8788_buck *buck,
202 enum lp8788_buck_id id)
204 enum lp8788_dvs_mode mode = lp8788_get_buck_dvs_ctrl_mode(buck, id);
205 struct lp8788_buck1_dvs *b1_dvs;
206 struct lp8788_buck2_dvs *b2_dvs;
212 if (mode == EXTPIN) {
213 b1_dvs = (struct lp8788_buck1_dvs *)buck->dvs;
217 idx = gpio_get_value(b1_dvs->gpio) ? 1 : 0;
219 lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
220 idx = (val & LP8788_BUCK1_DVS_M) >> LP8788_BUCK1_DVS_S;
222 addr = LP8788_BUCK1_VOUT0 + idx;
225 if (mode == EXTPIN) {
226 b2_dvs = (struct lp8788_buck2_dvs *)buck->dvs;
230 pin1 = gpio_get_value(b2_dvs->gpio[0]);
231 pin2 = gpio_get_value(b2_dvs->gpio[1]);
233 if (pin1 == PIN_LOW && pin2 == PIN_LOW)
235 else if (pin1 == PIN_LOW && pin2 == PIN_HIGH)
237 else if (pin1 == PIN_HIGH && pin2 == PIN_LOW)
242 lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
243 idx = (val & LP8788_BUCK2_DVS_M) >> LP8788_BUCK2_DVS_S;
245 addr = LP8788_BUCK2_VOUT0 + idx;
256 static int lp8788_buck12_set_voltage_sel(struct regulator_dev *rdev,
259 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
260 enum lp8788_buck_id id = rdev_get_id(rdev);
264 lp8788_set_dvs(buck, id);
266 addr = lp8788_select_buck_vout_addr(buck, id);
267 if (!lp8788_is_valid_buck_addr(addr))
270 return lp8788_update_bits(buck->lp, addr, LP8788_VOUT_M, selector);
273 static int lp8788_buck12_get_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 addr = lp8788_select_buck_vout_addr(buck, id);
281 if (!lp8788_is_valid_buck_addr(addr))
284 ret = lp8788_read_byte(buck->lp, addr, &val);
288 return val & LP8788_VOUT_M;
291 static int lp8788_buck_enable_time(struct regulator_dev *rdev)
293 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
294 enum lp8788_buck_id id = rdev_get_id(rdev);
295 u8 val, addr = LP8788_BUCK1_TIMESTEP + id;
297 if (lp8788_read_byte(buck->lp, addr, &val))
300 val = (val & LP8788_STARTUP_TIME_M) >> LP8788_STARTUP_TIME_S;
302 return ENABLE_TIME_USEC * val;
305 static int lp8788_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
307 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
308 enum lp8788_buck_id id = rdev_get_id(rdev);
311 mask = BUCK_FPWM_MASK(id);
313 case REGULATOR_MODE_FAST:
314 val = LP8788_FORCE_PWM << BUCK_FPWM_SHIFT(id);
316 case REGULATOR_MODE_NORMAL:
317 val = LP8788_AUTO_PWM << BUCK_FPWM_SHIFT(id);
323 return lp8788_update_bits(buck->lp, LP8788_BUCK_PWM, mask, val);
326 static unsigned int lp8788_buck_get_mode(struct regulator_dev *rdev)
328 struct lp8788_buck *buck = rdev_get_drvdata(rdev);
329 enum lp8788_buck_id id = rdev_get_id(rdev);
333 ret = lp8788_read_byte(buck->lp, LP8788_BUCK_PWM, &val);
337 return val & BUCK_FPWM_MASK(id) ?
338 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
341 static const struct regulator_ops lp8788_buck12_ops = {
342 .list_voltage = regulator_list_voltage_linear_range,
343 .map_voltage = regulator_map_voltage_linear_range,
344 .set_voltage_sel = lp8788_buck12_set_voltage_sel,
345 .get_voltage_sel = lp8788_buck12_get_voltage_sel,
346 .enable = regulator_enable_regmap,
347 .disable = regulator_disable_regmap,
348 .is_enabled = regulator_is_enabled_regmap,
349 .enable_time = lp8788_buck_enable_time,
350 .set_mode = lp8788_buck_set_mode,
351 .get_mode = lp8788_buck_get_mode,
354 static const struct regulator_ops lp8788_buck34_ops = {
355 .list_voltage = regulator_list_voltage_linear_range,
356 .map_voltage = regulator_map_voltage_linear_range,
357 .set_voltage_sel = regulator_set_voltage_sel_regmap,
358 .get_voltage_sel = regulator_get_voltage_sel_regmap,
359 .enable = regulator_enable_regmap,
360 .disable = regulator_disable_regmap,
361 .is_enabled = regulator_is_enabled_regmap,
362 .enable_time = lp8788_buck_enable_time,
363 .set_mode = lp8788_buck_set_mode,
364 .get_mode = lp8788_buck_get_mode,
367 static const struct regulator_desc lp8788_buck_desc[] = {
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_BUCK1_M,
383 .ops = &lp8788_buck12_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 .enable_reg = LP8788_EN_BUCK,
390 .enable_mask = LP8788_EN_BUCK2_M,
395 .ops = &lp8788_buck34_ops,
397 .linear_ranges = buck_volt_ranges,
398 .n_linear_ranges = ARRAY_SIZE(buck_volt_ranges),
399 .type = REGULATOR_VOLTAGE,
400 .owner = THIS_MODULE,
401 .vsel_reg = LP8788_BUCK3_VOUT,
402 .vsel_mask = LP8788_VOUT_M,
403 .enable_reg = LP8788_EN_BUCK,
404 .enable_mask = LP8788_EN_BUCK3_M,
409 .ops = &lp8788_buck34_ops,
411 .linear_ranges = buck_volt_ranges,
412 .n_linear_ranges = ARRAY_SIZE(buck_volt_ranges),
413 .type = REGULATOR_VOLTAGE,
414 .owner = THIS_MODULE,
415 .vsel_reg = LP8788_BUCK4_VOUT,
416 .vsel_mask = LP8788_VOUT_M,
417 .enable_reg = LP8788_EN_BUCK,
418 .enable_mask = LP8788_EN_BUCK4_M,
422 static int lp8788_dvs_gpio_request(struct platform_device *pdev,
423 struct lp8788_buck *buck,
424 enum lp8788_buck_id id)
426 struct lp8788_platform_data *pdata = buck->lp->pdata;
427 char *b1_name = "LP8788_B1_DVS";
428 char *b2_name[] = { "LP8788_B2_DVS1", "LP8788_B2_DVS2" };
433 gpio = pdata->buck1_dvs->gpio;
434 ret = devm_gpio_request_one(&pdev->dev, gpio, DVS_LOW,
439 buck->dvs = pdata->buck1_dvs;
442 for (i = 0; i < LP8788_NUM_BUCK2_DVS; i++) {
443 gpio = pdata->buck2_dvs->gpio[i];
444 ret = devm_gpio_request_one(&pdev->dev, gpio,
445 DVS_LOW, b2_name[i]);
449 buck->dvs = pdata->buck2_dvs;
458 static int lp8788_init_dvs(struct platform_device *pdev,
459 struct lp8788_buck *buck, enum lp8788_buck_id id)
461 struct lp8788_platform_data *pdata = buck->lp->pdata;
462 u8 mask[] = { LP8788_BUCK1_DVS_SEL_M, LP8788_BUCK2_DVS_SEL_M };
463 u8 val[] = { LP8788_BUCK1_DVS_PIN, LP8788_BUCK2_DVS_PIN };
464 u8 default_dvs_mode[] = { LP8788_BUCK1_DVS_I2C, LP8788_BUCK2_DVS_I2C };
466 /* no dvs for buck3, 4 */
470 /* no dvs platform data, then dvs will be selected by I2C registers */
472 goto set_default_dvs_mode;
474 if ((id == BUCK1 && !pdata->buck1_dvs) ||
475 (id == BUCK2 && !pdata->buck2_dvs))
476 goto set_default_dvs_mode;
478 if (lp8788_dvs_gpio_request(pdev, buck, id))
479 goto set_default_dvs_mode;
481 return lp8788_update_bits(buck->lp, LP8788_BUCK_DVS_SEL, mask[id],
484 set_default_dvs_mode:
485 return lp8788_update_bits(buck->lp, LP8788_BUCK_DVS_SEL, mask[id],
486 default_dvs_mode[id]);
489 static int lp8788_buck_probe(struct platform_device *pdev)
491 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
493 struct lp8788_buck *buck;
494 struct regulator_config cfg = { };
495 struct regulator_dev *rdev;
498 if (id >= LP8788_NUM_BUCKS)
501 buck = devm_kzalloc(&pdev->dev, sizeof(struct lp8788_buck), GFP_KERNEL);
507 ret = lp8788_init_dvs(pdev, buck, id);
511 cfg.dev = pdev->dev.parent;
512 cfg.init_data = lp->pdata ? lp->pdata->buck_data[id] : NULL;
513 cfg.driver_data = buck;
514 cfg.regmap = lp->regmap;
516 rdev = devm_regulator_register(&pdev->dev, &lp8788_buck_desc[id], &cfg);
519 dev_err(&pdev->dev, "BUCK%d regulator register err = %d\n",
524 buck->regulator = rdev;
525 platform_set_drvdata(pdev, buck);
530 static struct platform_driver lp8788_buck_driver = {
531 .probe = lp8788_buck_probe,
533 .name = LP8788_DEV_BUCK,
534 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
538 static int __init lp8788_buck_init(void)
540 return platform_driver_register(&lp8788_buck_driver);
542 subsys_initcall(lp8788_buck_init);
544 static void __exit lp8788_buck_exit(void)
546 platform_driver_unregister(&lp8788_buck_driver);
548 module_exit(lp8788_buck_exit);
550 MODULE_DESCRIPTION("TI LP8788 BUCK Driver");
551 MODULE_AUTHOR("Milo Kim");
552 MODULE_LICENSE("GPL");
553 MODULE_ALIAS("platform:lp8788-buck");