2 * tps65912.c -- TI tps65912
4 * Copyright 2011 Texas Instruments Inc.
6 * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
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 REGULATOR_LINEAR_RANGE(800000, 0, 32, 25000),
123 REGULATOR_LINEAR_RANGE(1650000, 33, 60, 50000),
124 REGULATOR_LINEAR_RANGE(3100000, 61, 63, 100000),
127 static int tps65912_get_range(struct tps65912_reg *pmic, int id)
129 struct tps65912 *mfd = pmic->mfd;
133 case TPS65912_REG_DCDC1:
134 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
136 case TPS65912_REG_DCDC2:
137 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
139 case TPS65912_REG_DCDC3:
140 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
142 case TPS65912_REG_DCDC4:
143 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
150 range = (range & DCDC_LIMIT_RANGE_MASK)
151 >> DCDC_LIMIT_RANGE_SHIFT;
153 pmic->dcdc_range[id] = range;
157 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
161 uv = ((vsel * 12500) + 500000);
165 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
169 uv = ((vsel * 12500) + 700000);
173 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
177 uv = ((vsel * 25000) + 500000);
181 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
188 uv = ((vsel * 50000) + 500000);
193 static int tps65912_get_ctrl_register(int id)
195 if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4)
196 return id * 3 + TPS65912_DCDC1_AVS;
197 else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10)
198 return id - TPS65912_REG_LDO5 + TPS65912_LDO5;
203 static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
205 struct tps65912 *mfd = pmic->mfd;
209 if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) {
210 opvsel = tps65912_reg_read(mfd, id * 3 + TPS65912_DCDC1_OP);
211 if (opvsel & OP_SELREG_MASK)
212 reg = id * 3 + TPS65912_DCDC1_AVS;
214 reg = id * 3 + TPS65912_DCDC1_OP;
215 } else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) {
216 reg = id - TPS65912_REG_LDO5 + TPS65912_LDO5;
224 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
227 case TPS65912_REG_DCDC1:
228 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
229 pmic->eco_reg = TPS65912_DCDC1_AVS;
231 case TPS65912_REG_DCDC2:
232 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
233 pmic->eco_reg = TPS65912_DCDC2_AVS;
235 case TPS65912_REG_DCDC3:
236 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
237 pmic->eco_reg = TPS65912_DCDC3_AVS;
239 case TPS65912_REG_DCDC4:
240 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
241 pmic->eco_reg = TPS65912_DCDC4_AVS;
250 static int tps65912_reg_is_enabled(struct regulator_dev *dev)
252 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
253 struct tps65912 *mfd = pmic->mfd;
254 int reg, value, id = rdev_get_id(dev);
256 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
259 reg = pmic->get_ctrl_reg(id);
263 value = tps65912_reg_read(mfd, reg);
267 return value & TPS65912_REG_ENABLED;
270 static int tps65912_reg_enable(struct regulator_dev *dev)
272 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
273 struct tps65912 *mfd = pmic->mfd;
274 int id = rdev_get_id(dev);
277 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
280 reg = pmic->get_ctrl_reg(id);
284 return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
287 static int tps65912_reg_disable(struct regulator_dev *dev)
289 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
290 struct tps65912 *mfd = pmic->mfd;
291 int id = rdev_get_id(dev), reg;
293 reg = pmic->get_ctrl_reg(id);
297 return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
300 static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
302 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
303 struct tps65912 *mfd = pmic->mfd;
304 int pwm_mode, eco, id = rdev_get_id(dev);
306 tps65912_get_mode_regiters(pmic, id);
308 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
309 eco = tps65912_reg_read(mfd, pmic->eco_reg);
311 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
312 eco &= DCDC_AVS_ECO_MASK;
315 case REGULATOR_MODE_FAST:
316 /* Verify if mode alredy set */
317 if (pwm_mode && !eco)
319 tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
320 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
322 case REGULATOR_MODE_NORMAL:
323 case REGULATOR_MODE_IDLE:
324 if (!pwm_mode && !eco)
326 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
327 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
329 case REGULATOR_MODE_STANDBY:
330 if (!pwm_mode && eco)
332 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
333 tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
342 static unsigned int tps65912_get_mode(struct regulator_dev *dev)
344 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
345 struct tps65912 *mfd = pmic->mfd;
346 int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);
348 tps65912_get_mode_regiters(pmic, id);
350 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
351 eco = tps65912_reg_read(mfd, pmic->eco_reg);
353 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
354 eco &= DCDC_AVS_ECO_MASK;
356 if (pwm_mode && !eco)
357 mode = REGULATOR_MODE_FAST;
358 else if (!pwm_mode && !eco)
359 mode = REGULATOR_MODE_NORMAL;
360 else if (!pwm_mode && eco)
361 mode = REGULATOR_MODE_STANDBY;
366 static int tps65912_list_voltage(struct regulator_dev *dev, unsigned selector)
368 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
369 int range, voltage = 0, id = rdev_get_id(dev);
371 if (id > TPS65912_REG_DCDC4)
374 range = pmic->dcdc_range[id];
378 /* 0.5 - 1.2875V in 12.5mV steps */
379 voltage = tps65912_vsel_to_uv_range0(selector);
382 /* 0.7 - 1.4875V in 12.5mV steps */
383 voltage = tps65912_vsel_to_uv_range1(selector);
386 /* 0.5 - 2.075V in 25mV steps */
387 voltage = tps65912_vsel_to_uv_range2(selector);
390 /* 0.5 - 3.8V in 50mV steps */
391 voltage = tps65912_vsel_to_uv_range3(selector);
397 static int tps65912_get_voltage_sel(struct regulator_dev *dev)
399 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
400 struct tps65912 *mfd = pmic->mfd;
401 int id = rdev_get_id(dev);
404 reg = tps65912_get_sel_register(pmic, id);
408 vsel = tps65912_reg_read(mfd, reg);
414 static int tps65912_set_voltage_sel(struct regulator_dev *dev,
417 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
418 struct tps65912 *mfd = pmic->mfd;
419 int id = rdev_get_id(dev);
423 reg = tps65912_get_sel_register(pmic, id);
424 value = tps65912_reg_read(mfd, reg);
426 return tps65912_reg_write(mfd, reg, selector | value);
429 /* Operations permitted on DCDCx */
430 static struct regulator_ops tps65912_ops_dcdc = {
431 .is_enabled = tps65912_reg_is_enabled,
432 .enable = tps65912_reg_enable,
433 .disable = tps65912_reg_disable,
434 .set_mode = tps65912_set_mode,
435 .get_mode = tps65912_get_mode,
436 .get_voltage_sel = tps65912_get_voltage_sel,
437 .set_voltage_sel = tps65912_set_voltage_sel,
438 .list_voltage = tps65912_list_voltage,
441 /* Operations permitted on LDOx */
442 static struct regulator_ops tps65912_ops_ldo = {
443 .is_enabled = tps65912_reg_is_enabled,
444 .enable = tps65912_reg_enable,
445 .disable = tps65912_reg_disable,
446 .get_voltage_sel = tps65912_get_voltage_sel,
447 .set_voltage_sel = tps65912_set_voltage_sel,
448 .list_voltage = regulator_list_voltage_linear_range,
449 .map_voltage = regulator_map_voltage_linear_range,
452 static int tps65912_probe(struct platform_device *pdev)
454 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
455 struct regulator_config config = { };
456 struct tps_info *info;
457 struct regulator_init_data *reg_data;
458 struct regulator_dev *rdev;
459 struct tps65912_reg *pmic;
460 struct tps65912_board *pmic_plat_data;
463 pmic_plat_data = dev_get_platdata(tps65912->dev);
467 reg_data = pmic_plat_data->tps65912_pmic_init_data;
469 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
473 mutex_init(&pmic->io_lock);
474 pmic->mfd = tps65912;
475 platform_set_drvdata(pdev, pmic);
477 pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
478 info = tps65912_regs;
480 for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
482 /* Register the regulators */
483 pmic->info[i] = info;
485 pmic->desc[i].name = info->name;
486 pmic->desc[i].id = i;
487 pmic->desc[i].n_voltages = 64;
488 if (i > TPS65912_REG_DCDC4) {
489 pmic->desc[i].ops = &tps65912_ops_ldo;
490 pmic->desc[i].linear_ranges = tps65912_ldo_ranges;
491 pmic->desc[i].n_linear_ranges =
492 ARRAY_SIZE(tps65912_ldo_ranges);
494 pmic->desc[i].ops = &tps65912_ops_dcdc;
496 pmic->desc[i].type = REGULATOR_VOLTAGE;
497 pmic->desc[i].owner = THIS_MODULE;
498 range = tps65912_get_range(pmic, i);
500 config.dev = tps65912->dev;
501 config.init_data = reg_data;
502 config.driver_data = pmic;
504 rdev = devm_regulator_register(&pdev->dev, &pmic->desc[i],
507 dev_err(tps65912->dev,
508 "failed to register %s regulator\n",
510 return PTR_ERR(rdev);
513 /* Save regulator for cleanup */
514 pmic->rdev[i] = rdev;
519 static struct platform_driver tps65912_driver = {
521 .name = "tps65912-pmic",
522 .owner = THIS_MODULE,
524 .probe = tps65912_probe,
527 static int __init tps65912_init(void)
529 return platform_driver_register(&tps65912_driver);
531 subsys_initcall(tps65912_init);
533 static void __exit tps65912_cleanup(void)
535 platform_driver_unregister(&tps65912_driver);
537 module_exit(tps65912_cleanup);
539 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
540 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
541 MODULE_LICENSE("GPL v2");
542 MODULE_ALIAS("platform:tps65912-pmic");