2 * tps65912.c -- TI tps65912
4 * Copyright 2011 Texas Instruments Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This driver is based on wm8350 implementation.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/slab.h>
24 #include <linux/gpio.h>
25 #include <linux/mfd/tps65912.h>
28 #define TPS65912_REG_DCDC1 0
29 #define TPS65912_REG_DCDC2 1
30 #define TPS65912_REG_DCDC3 2
31 #define TPS65912_REG_DCDC4 3
34 #define TPS65912_REG_LDO1 4
35 #define TPS65912_REG_LDO2 5
36 #define TPS65912_REG_LDO3 6
37 #define TPS65912_REG_LDO4 7
38 #define TPS65912_REG_LDO5 8
39 #define TPS65912_REG_LDO6 9
40 #define TPS65912_REG_LDO7 10
41 #define TPS65912_REG_LDO8 11
42 #define TPS65912_REG_LDO9 12
43 #define TPS65912_REG_LDO10 13
45 /* Number of step-down converters available */
46 #define TPS65912_NUM_DCDC 4
48 /* Number of LDO voltage regulators available */
49 #define TPS65912_NUM_LDO 10
51 /* Number of total regulators available */
52 #define TPS65912_NUM_REGULATOR (TPS65912_NUM_DCDC + TPS65912_NUM_LDO)
54 #define TPS65912_REG_ENABLED 0x80
55 #define OP_SELREG_MASK 0x40
56 #define OP_SELREG_SHIFT 6
62 static struct tps_info tps65912_regs[] = {
107 struct tps65912_reg {
108 struct regulator_desc desc[TPS65912_NUM_REGULATOR];
109 struct tps65912 *mfd;
110 struct regulator_dev *rdev[TPS65912_NUM_REGULATOR];
111 struct tps_info *info[TPS65912_NUM_REGULATOR];
112 /* for read/write access */
113 struct mutex io_lock;
115 int (*get_ctrl_reg)(int);
116 int dcdc_range[TPS65912_NUM_DCDC];
121 static const struct regulator_linear_range tps65912_ldo_ranges[] = {
122 { .min_uV = 800000, .max_uV = 1600000, .min_sel = 0, .max_sel = 32,
124 { .min_uV = 1650000, .max_uV = 3000000, .min_sel = 33, .max_sel = 60,
126 { .min_uV = 3100000, .max_uV = 3300000, .min_sel = 61, .max_sel = 63,
130 static int tps65912_get_range(struct tps65912_reg *pmic, int id)
132 struct tps65912 *mfd = pmic->mfd;
136 case TPS65912_REG_DCDC1:
137 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
139 case TPS65912_REG_DCDC2:
140 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
142 case TPS65912_REG_DCDC3:
143 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
145 case TPS65912_REG_DCDC4:
146 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
153 range = (range & DCDC_LIMIT_RANGE_MASK)
154 >> DCDC_LIMIT_RANGE_SHIFT;
156 pmic->dcdc_range[id] = range;
160 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
164 uv = ((vsel * 12500) + 500000);
168 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
172 uv = ((vsel * 12500) + 700000);
176 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
180 uv = ((vsel * 25000) + 500000);
184 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
191 uv = ((vsel * 50000) + 500000);
196 static int tps65912_get_ctrl_register(int id)
198 if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4)
199 return id * 3 + TPS65912_DCDC1_AVS;
200 else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10)
201 return id - TPS65912_REG_LDO5 + TPS65912_LDO5;
206 static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
208 struct tps65912 *mfd = pmic->mfd;
212 if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) {
213 opvsel = tps65912_reg_read(mfd, id * 3 + TPS65912_DCDC1_OP);
214 if (opvsel & OP_SELREG_MASK)
215 reg = id * 3 + TPS65912_DCDC1_AVS;
217 reg = id * 3 + TPS65912_DCDC1_OP;
218 } else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) {
219 reg = id - TPS65912_REG_LDO5 + TPS65912_LDO5;
227 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
230 case TPS65912_REG_DCDC1:
231 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
232 pmic->eco_reg = TPS65912_DCDC1_AVS;
234 case TPS65912_REG_DCDC2:
235 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
236 pmic->eco_reg = TPS65912_DCDC2_AVS;
238 case TPS65912_REG_DCDC3:
239 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
240 pmic->eco_reg = TPS65912_DCDC3_AVS;
242 case TPS65912_REG_DCDC4:
243 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
244 pmic->eco_reg = TPS65912_DCDC4_AVS;
253 static int tps65912_reg_is_enabled(struct regulator_dev *dev)
255 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
256 struct tps65912 *mfd = pmic->mfd;
257 int reg, value, id = rdev_get_id(dev);
259 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
262 reg = pmic->get_ctrl_reg(id);
266 value = tps65912_reg_read(mfd, reg);
270 return value & TPS65912_REG_ENABLED;
273 static int tps65912_reg_enable(struct regulator_dev *dev)
275 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
276 struct tps65912 *mfd = pmic->mfd;
277 int id = rdev_get_id(dev);
280 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
283 reg = pmic->get_ctrl_reg(id);
287 return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
290 static int tps65912_reg_disable(struct regulator_dev *dev)
292 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
293 struct tps65912 *mfd = pmic->mfd;
294 int id = rdev_get_id(dev), reg;
296 reg = pmic->get_ctrl_reg(id);
300 return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
303 static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
305 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
306 struct tps65912 *mfd = pmic->mfd;
307 int pwm_mode, eco, id = rdev_get_id(dev);
309 tps65912_get_mode_regiters(pmic, id);
311 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
312 eco = tps65912_reg_read(mfd, pmic->eco_reg);
314 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
315 eco &= DCDC_AVS_ECO_MASK;
318 case REGULATOR_MODE_FAST:
319 /* Verify if mode alredy set */
320 if (pwm_mode && !eco)
322 tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
323 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
325 case REGULATOR_MODE_NORMAL:
326 case REGULATOR_MODE_IDLE:
327 if (!pwm_mode && !eco)
329 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
330 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
332 case REGULATOR_MODE_STANDBY:
333 if (!pwm_mode && eco)
335 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
336 tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
345 static unsigned int tps65912_get_mode(struct regulator_dev *dev)
347 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
348 struct tps65912 *mfd = pmic->mfd;
349 int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);
351 tps65912_get_mode_regiters(pmic, id);
353 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
354 eco = tps65912_reg_read(mfd, pmic->eco_reg);
356 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
357 eco &= DCDC_AVS_ECO_MASK;
359 if (pwm_mode && !eco)
360 mode = REGULATOR_MODE_FAST;
361 else if (!pwm_mode && !eco)
362 mode = REGULATOR_MODE_NORMAL;
363 else if (!pwm_mode && eco)
364 mode = REGULATOR_MODE_STANDBY;
369 static int tps65912_list_voltage(struct regulator_dev *dev, unsigned selector)
371 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
372 int range, voltage = 0, id = rdev_get_id(dev);
374 if (id > TPS65912_REG_DCDC4)
377 range = pmic->dcdc_range[id];
381 /* 0.5 - 1.2875V in 12.5mV steps */
382 voltage = tps65912_vsel_to_uv_range0(selector);
385 /* 0.7 - 1.4875V in 12.5mV steps */
386 voltage = tps65912_vsel_to_uv_range1(selector);
389 /* 0.5 - 2.075V in 25mV steps */
390 voltage = tps65912_vsel_to_uv_range2(selector);
393 /* 0.5 - 3.8V in 50mV steps */
394 voltage = tps65912_vsel_to_uv_range3(selector);
400 static int tps65912_get_voltage_sel(struct regulator_dev *dev)
402 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
403 struct tps65912 *mfd = pmic->mfd;
404 int id = rdev_get_id(dev);
407 reg = tps65912_get_sel_register(pmic, id);
411 vsel = tps65912_reg_read(mfd, reg);
417 static int tps65912_set_voltage_sel(struct regulator_dev *dev,
420 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
421 struct tps65912 *mfd = pmic->mfd;
422 int id = rdev_get_id(dev);
426 reg = tps65912_get_sel_register(pmic, id);
427 value = tps65912_reg_read(mfd, reg);
429 return tps65912_reg_write(mfd, reg, selector | value);
432 /* Operations permitted on DCDCx */
433 static struct regulator_ops tps65912_ops_dcdc = {
434 .is_enabled = tps65912_reg_is_enabled,
435 .enable = tps65912_reg_enable,
436 .disable = tps65912_reg_disable,
437 .set_mode = tps65912_set_mode,
438 .get_mode = tps65912_get_mode,
439 .get_voltage_sel = tps65912_get_voltage_sel,
440 .set_voltage_sel = tps65912_set_voltage_sel,
441 .list_voltage = tps65912_list_voltage,
444 /* Operations permitted on LDOx */
445 static struct regulator_ops tps65912_ops_ldo = {
446 .is_enabled = tps65912_reg_is_enabled,
447 .enable = tps65912_reg_enable,
448 .disable = tps65912_reg_disable,
449 .get_voltage_sel = tps65912_get_voltage_sel,
450 .set_voltage_sel = tps65912_set_voltage_sel,
451 .list_voltage = regulator_list_voltage_linear_range,
452 .map_voltage = regulator_map_voltage_linear_range,
455 static int tps65912_probe(struct platform_device *pdev)
457 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
458 struct regulator_config config = { };
459 struct tps_info *info;
460 struct regulator_init_data *reg_data;
461 struct regulator_dev *rdev;
462 struct tps65912_reg *pmic;
463 struct tps65912_board *pmic_plat_data;
466 pmic_plat_data = dev_get_platdata(tps65912->dev);
470 reg_data = pmic_plat_data->tps65912_pmic_init_data;
472 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
476 mutex_init(&pmic->io_lock);
477 pmic->mfd = tps65912;
478 platform_set_drvdata(pdev, pmic);
480 pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
481 info = tps65912_regs;
483 for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
485 /* Register the regulators */
486 pmic->info[i] = info;
488 pmic->desc[i].name = info->name;
489 pmic->desc[i].id = i;
490 pmic->desc[i].n_voltages = 64;
491 if (i > TPS65912_REG_DCDC4) {
492 pmic->desc[i].ops = &tps65912_ops_ldo;
493 pmic->desc[i].linear_ranges = tps65912_ldo_ranges;
494 pmic->desc[i].n_linear_ranges =
495 ARRAY_SIZE(tps65912_ldo_ranges);
497 pmic->desc[i].ops = &tps65912_ops_dcdc;
499 pmic->desc[i].type = REGULATOR_VOLTAGE;
500 pmic->desc[i].owner = THIS_MODULE;
501 range = tps65912_get_range(pmic, i);
503 config.dev = tps65912->dev;
504 config.init_data = reg_data;
505 config.driver_data = pmic;
507 rdev = regulator_register(&pmic->desc[i], &config);
509 dev_err(tps65912->dev,
510 "failed to register %s regulator\n",
516 /* Save regulator for cleanup */
517 pmic->rdev[i] = rdev;
523 regulator_unregister(pmic->rdev[i]);
527 static int tps65912_remove(struct platform_device *pdev)
529 struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
532 for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
533 regulator_unregister(tps65912_reg->rdev[i]);
537 static struct platform_driver tps65912_driver = {
539 .name = "tps65912-pmic",
540 .owner = THIS_MODULE,
542 .probe = tps65912_probe,
543 .remove = tps65912_remove,
546 static int __init tps65912_init(void)
548 return platform_driver_register(&tps65912_driver);
550 subsys_initcall(tps65912_init);
552 static void __exit tps65912_cleanup(void)
554 platform_driver_unregister(&tps65912_driver);
556 module_exit(tps65912_cleanup);
559 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
560 MODULE_LICENSE("GPL v2");
561 MODULE_ALIAS("platform:tps65912-pmic");