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 int tps65912_get_range(struct tps65912_reg *pmic, int id)
123 struct tps65912 *mfd = pmic->mfd;
127 case TPS65912_REG_DCDC1:
128 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
130 case TPS65912_REG_DCDC2:
131 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
133 case TPS65912_REG_DCDC3:
134 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
136 case TPS65912_REG_DCDC4:
137 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
144 range = (range & DCDC_LIMIT_RANGE_MASK)
145 >> DCDC_LIMIT_RANGE_SHIFT;
147 pmic->dcdc_range[id] = range;
151 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
155 uv = ((vsel * 12500) + 500000);
159 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
163 uv = ((vsel * 12500) + 700000);
167 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
171 uv = ((vsel * 25000) + 500000);
175 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
182 uv = ((vsel * 50000) + 500000);
187 static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel)
189 unsigned long uv = 0;
192 uv = ((vsel * 25000) + 800000);
193 else if (vsel > 32 && vsel <= 60)
194 uv = (((vsel - 32) * 50000) + 1600000);
196 uv = (((vsel - 60) * 100000) + 3000000);
201 static int tps65912_get_ctrl_register(int id)
203 if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4)
204 return id * 3 + TPS65912_DCDC1_AVS;
205 else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10)
206 return id - TPS65912_REG_LDO5 + TPS65912_LDO5;
211 static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
213 struct tps65912 *mfd = pmic->mfd;
217 if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) {
218 opvsel = tps65912_reg_read(mfd, id * 3 + TPS65912_DCDC1_OP);
219 if (opvsel & OP_SELREG_MASK)
220 reg = id * 3 + TPS65912_DCDC1_AVS;
222 reg = id * 3 + TPS65912_DCDC1_OP;
223 } else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) {
224 reg = id - TPS65912_REG_LDO5 + TPS65912_LDO5;
232 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
235 case TPS65912_REG_DCDC1:
236 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
237 pmic->eco_reg = TPS65912_DCDC1_AVS;
239 case TPS65912_REG_DCDC2:
240 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
241 pmic->eco_reg = TPS65912_DCDC2_AVS;
243 case TPS65912_REG_DCDC3:
244 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
245 pmic->eco_reg = TPS65912_DCDC3_AVS;
247 case TPS65912_REG_DCDC4:
248 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
249 pmic->eco_reg = TPS65912_DCDC4_AVS;
258 static int tps65912_reg_is_enabled(struct regulator_dev *dev)
260 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
261 struct tps65912 *mfd = pmic->mfd;
262 int reg, value, id = rdev_get_id(dev);
264 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
267 reg = pmic->get_ctrl_reg(id);
271 value = tps65912_reg_read(mfd, reg);
275 return value & TPS65912_REG_ENABLED;
278 static int tps65912_reg_enable(struct regulator_dev *dev)
280 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
281 struct tps65912 *mfd = pmic->mfd;
282 int id = rdev_get_id(dev);
285 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
288 reg = pmic->get_ctrl_reg(id);
292 return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
295 static int tps65912_reg_disable(struct regulator_dev *dev)
297 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
298 struct tps65912 *mfd = pmic->mfd;
299 int id = rdev_get_id(dev), reg;
301 reg = pmic->get_ctrl_reg(id);
305 return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
308 static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
310 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
311 struct tps65912 *mfd = pmic->mfd;
312 int pwm_mode, eco, id = rdev_get_id(dev);
314 tps65912_get_mode_regiters(pmic, id);
316 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
317 eco = tps65912_reg_read(mfd, pmic->eco_reg);
319 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
320 eco &= DCDC_AVS_ECO_MASK;
323 case REGULATOR_MODE_FAST:
324 /* Verify if mode alredy set */
325 if (pwm_mode && !eco)
327 tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
328 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
330 case REGULATOR_MODE_NORMAL:
331 case REGULATOR_MODE_IDLE:
332 if (!pwm_mode && !eco)
334 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
335 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
337 case REGULATOR_MODE_STANDBY:
338 if (!pwm_mode && eco)
340 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
341 tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
350 static unsigned int tps65912_get_mode(struct regulator_dev *dev)
352 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
353 struct tps65912 *mfd = pmic->mfd;
354 int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);
356 tps65912_get_mode_regiters(pmic, id);
358 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
359 eco = tps65912_reg_read(mfd, pmic->eco_reg);
361 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
362 eco &= DCDC_AVS_ECO_MASK;
364 if (pwm_mode && !eco)
365 mode = REGULATOR_MODE_FAST;
366 else if (!pwm_mode && !eco)
367 mode = REGULATOR_MODE_NORMAL;
368 else if (!pwm_mode && eco)
369 mode = REGULATOR_MODE_STANDBY;
374 static int tps65912_list_voltage(struct regulator_dev *dev, unsigned selector)
376 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
377 int range, voltage = 0, id = rdev_get_id(dev);
379 if (id >= TPS65912_REG_LDO1 && id <= TPS65912_REG_LDO10)
380 return tps65912_vsel_to_uv_ldo(selector);
382 if (id > TPS65912_REG_DCDC4)
385 range = pmic->dcdc_range[id];
389 /* 0.5 - 1.2875V in 12.5mV steps */
390 voltage = tps65912_vsel_to_uv_range0(selector);
393 /* 0.7 - 1.4875V in 12.5mV steps */
394 voltage = tps65912_vsel_to_uv_range1(selector);
397 /* 0.5 - 2.075V in 25mV steps */
398 voltage = tps65912_vsel_to_uv_range2(selector);
401 /* 0.5 - 3.8V in 50mV steps */
402 voltage = tps65912_vsel_to_uv_range3(selector);
408 static int tps65912_get_voltage_sel(struct regulator_dev *dev)
410 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
411 struct tps65912 *mfd = pmic->mfd;
412 int id = rdev_get_id(dev);
415 reg = tps65912_get_sel_register(pmic, id);
419 vsel = tps65912_reg_read(mfd, reg);
425 static int tps65912_set_voltage_sel(struct regulator_dev *dev,
428 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
429 struct tps65912 *mfd = pmic->mfd;
430 int id = rdev_get_id(dev);
434 reg = tps65912_get_sel_register(pmic, id);
435 value = tps65912_reg_read(mfd, reg);
437 return tps65912_reg_write(mfd, reg, selector | value);
440 /* Operations permitted on DCDCx */
441 static struct regulator_ops tps65912_ops_dcdc = {
442 .is_enabled = tps65912_reg_is_enabled,
443 .enable = tps65912_reg_enable,
444 .disable = tps65912_reg_disable,
445 .set_mode = tps65912_set_mode,
446 .get_mode = tps65912_get_mode,
447 .get_voltage_sel = tps65912_get_voltage_sel,
448 .set_voltage_sel = tps65912_set_voltage_sel,
449 .list_voltage = tps65912_list_voltage,
452 /* Operations permitted on LDOx */
453 static struct regulator_ops tps65912_ops_ldo = {
454 .is_enabled = tps65912_reg_is_enabled,
455 .enable = tps65912_reg_enable,
456 .disable = tps65912_reg_disable,
457 .get_voltage_sel = tps65912_get_voltage_sel,
458 .set_voltage_sel = tps65912_set_voltage_sel,
459 .list_voltage = tps65912_list_voltage,
462 static int tps65912_probe(struct platform_device *pdev)
464 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
465 struct regulator_config config = { };
466 struct tps_info *info;
467 struct regulator_init_data *reg_data;
468 struct regulator_dev *rdev;
469 struct tps65912_reg *pmic;
470 struct tps65912_board *pmic_plat_data;
473 pmic_plat_data = dev_get_platdata(tps65912->dev);
477 reg_data = pmic_plat_data->tps65912_pmic_init_data;
479 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
483 mutex_init(&pmic->io_lock);
484 pmic->mfd = tps65912;
485 platform_set_drvdata(pdev, pmic);
487 pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
488 info = tps65912_regs;
490 for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
492 /* Register the regulators */
493 pmic->info[i] = info;
495 pmic->desc[i].name = info->name;
496 pmic->desc[i].id = i;
497 pmic->desc[i].n_voltages = 64;
498 pmic->desc[i].ops = (i > TPS65912_REG_DCDC4 ?
499 &tps65912_ops_ldo : &tps65912_ops_dcdc);
500 pmic->desc[i].type = REGULATOR_VOLTAGE;
501 pmic->desc[i].owner = THIS_MODULE;
502 range = tps65912_get_range(pmic, i);
504 config.dev = tps65912->dev;
505 config.init_data = reg_data;
506 config.driver_data = pmic;
508 rdev = regulator_register(&pmic->desc[i], &config);
510 dev_err(tps65912->dev,
511 "failed to register %s regulator\n",
517 /* Save regulator for cleanup */
518 pmic->rdev[i] = rdev;
524 regulator_unregister(pmic->rdev[i]);
528 static int tps65912_remove(struct platform_device *pdev)
530 struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
533 for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
534 regulator_unregister(tps65912_reg->rdev[i]);
538 static struct platform_driver tps65912_driver = {
540 .name = "tps65912-pmic",
541 .owner = THIS_MODULE,
543 .probe = tps65912_probe,
544 .remove = tps65912_remove,
547 static int __init tps65912_init(void)
549 return platform_driver_register(&tps65912_driver);
551 subsys_initcall(tps65912_init);
553 static void __exit tps65912_cleanup(void)
555 platform_driver_unregister(&tps65912_driver);
557 module_exit(tps65912_cleanup);
559 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
560 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
561 MODULE_LICENSE("GPL v2");
562 MODULE_ALIAS("platform:tps65912-pmic");