mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / regulator / tps65912-regulator.c
blob281e52ac64ba0e3caf66c0322bd23ac91455a8a4
1 /*
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>
27 /* DCDC's */
28 #define TPS65912_REG_DCDC1 0
29 #define TPS65912_REG_DCDC2 1
30 #define TPS65912_REG_DCDC3 2
31 #define TPS65912_REG_DCDC4 3
33 /* LDOs */
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
58 struct tps_info {
59 const char *name;
62 static struct tps_info tps65912_regs[] = {
64 .name = "DCDC1",
67 .name = "DCDC2",
70 .name = "DCDC3",
73 .name = "DCDC4",
76 .name = "LDO1",
79 .name = "LDO2",
82 .name = "LDO3",
85 .name = "LDO4",
88 .name = "LDO5",
91 .name = "LDO6",
94 .name = "LDO7",
97 .name = "LDO8",
100 .name = "LDO9",
103 .name = "LDO10",
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;
114 int mode;
115 int (*get_ctrl_reg)(int);
116 int dcdc_range[TPS65912_NUM_DCDC];
117 int pwm_mode_reg;
118 int eco_reg;
121 static const struct regulator_linear_range tps65912_ldo_ranges[] = {
122 { .min_uV = 800000, .max_uV = 1600000, .min_sel = 0, .max_sel = 32,
123 .uV_step = 25000 },
124 { .min_uV = 1650000, .max_uV = 3000000, .min_sel = 33, .max_sel = 60,
125 .uV_step = 50000 },
126 { .min_uV = 3100000, .max_uV = 3300000, .min_sel = 61, .max_sel = 63,
127 .uV_step = 100000 },
130 static int tps65912_get_range(struct tps65912_reg *pmic, int id)
132 struct tps65912 *mfd = pmic->mfd;
133 int range;
135 switch (id) {
136 case TPS65912_REG_DCDC1:
137 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
138 break;
139 case TPS65912_REG_DCDC2:
140 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
141 break;
142 case TPS65912_REG_DCDC3:
143 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
144 break;
145 case TPS65912_REG_DCDC4:
146 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
147 break;
148 default:
149 return 0;
152 if (range >= 0)
153 range = (range & DCDC_LIMIT_RANGE_MASK)
154 >> DCDC_LIMIT_RANGE_SHIFT;
156 pmic->dcdc_range[id] = range;
157 return range;
160 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
162 unsigned long uv;
164 uv = ((vsel * 12500) + 500000);
165 return uv;
168 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
170 unsigned long uv;
172 uv = ((vsel * 12500) + 700000);
173 return uv;
176 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
178 unsigned long uv;
180 uv = ((vsel * 25000) + 500000);
181 return uv;
184 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
186 unsigned long uv;
188 if (vsel == 0x3f)
189 uv = 3800000;
190 else
191 uv = ((vsel * 50000) + 500000);
193 return uv;
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;
202 else
203 return -EINVAL;
206 static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
208 struct tps65912 *mfd = pmic->mfd;
209 int opvsel;
210 u8 reg = 0;
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;
216 else
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;
220 } else {
221 return -EINVAL;
224 return reg;
227 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
229 switch (id) {
230 case TPS65912_REG_DCDC1:
231 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
232 pmic->eco_reg = TPS65912_DCDC1_AVS;
233 break;
234 case TPS65912_REG_DCDC2:
235 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
236 pmic->eco_reg = TPS65912_DCDC2_AVS;
237 break;
238 case TPS65912_REG_DCDC3:
239 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
240 pmic->eco_reg = TPS65912_DCDC3_AVS;
241 break;
242 case TPS65912_REG_DCDC4:
243 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
244 pmic->eco_reg = TPS65912_DCDC4_AVS;
245 break;
246 default:
247 return -EINVAL;
250 return 0;
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)
260 return -EINVAL;
262 reg = pmic->get_ctrl_reg(id);
263 if (reg < 0)
264 return reg;
266 value = tps65912_reg_read(mfd, reg);
267 if (value < 0)
268 return value;
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);
278 int reg;
280 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
281 return -EINVAL;
283 reg = pmic->get_ctrl_reg(id);
284 if (reg < 0)
285 return reg;
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);
297 if (reg < 0)
298 return reg;
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;
317 switch (mode) {
318 case REGULATOR_MODE_FAST:
319 /* Verify if mode alredy set */
320 if (pwm_mode && !eco)
321 break;
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);
324 break;
325 case REGULATOR_MODE_NORMAL:
326 case REGULATOR_MODE_IDLE:
327 if (!pwm_mode && !eco)
328 break;
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);
331 break;
332 case REGULATOR_MODE_STANDBY:
333 if (!pwm_mode && eco)
334 break;
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);
337 break;
338 default:
339 return -EINVAL;
342 return 0;
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;
366 return mode;
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)
375 return -EINVAL;
377 range = pmic->dcdc_range[id];
379 switch (range) {
380 case 0:
381 /* 0.5 - 1.2875V in 12.5mV steps */
382 voltage = tps65912_vsel_to_uv_range0(selector);
383 break;
384 case 1:
385 /* 0.7 - 1.4875V in 12.5mV steps */
386 voltage = tps65912_vsel_to_uv_range1(selector);
387 break;
388 case 2:
389 /* 0.5 - 2.075V in 25mV steps */
390 voltage = tps65912_vsel_to_uv_range2(selector);
391 break;
392 case 3:
393 /* 0.5 - 3.8V in 50mV steps */
394 voltage = tps65912_vsel_to_uv_range3(selector);
395 break;
397 return voltage;
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);
405 int reg, vsel;
407 reg = tps65912_get_sel_register(pmic, id);
408 if (reg < 0)
409 return reg;
411 vsel = tps65912_reg_read(mfd, reg);
412 vsel &= 0x3F;
414 return vsel;
417 static int tps65912_set_voltage_sel(struct regulator_dev *dev,
418 unsigned selector)
420 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
421 struct tps65912 *mfd = pmic->mfd;
422 int id = rdev_get_id(dev);
423 int value;
424 u8 reg;
426 reg = tps65912_get_sel_register(pmic, id);
427 value = tps65912_reg_read(mfd, reg);
428 value &= 0xC0;
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;
464 int i, err;
466 pmic_plat_data = dev_get_platdata(tps65912->dev);
467 if (!pmic_plat_data)
468 return -EINVAL;
470 reg_data = pmic_plat_data->tps65912_pmic_init_data;
472 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
473 if (!pmic)
474 return -ENOMEM;
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++) {
484 int range = 0;
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);
496 } else {
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);
508 if (IS_ERR(rdev)) {
509 dev_err(tps65912->dev,
510 "failed to register %s regulator\n",
511 pdev->name);
512 err = PTR_ERR(rdev);
513 goto err;
516 /* Save regulator for cleanup */
517 pmic->rdev[i] = rdev;
519 return 0;
521 err:
522 while (--i >= 0)
523 regulator_unregister(pmic->rdev[i]);
524 return err;
527 static int tps65912_remove(struct platform_device *pdev)
529 struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
530 int i;
532 for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
533 regulator_unregister(tps65912_reg->rdev[i]);
534 return 0;
537 static struct platform_driver tps65912_driver = {
538 .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);
558 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
559 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
560 MODULE_LICENSE("GPL v2");
561 MODULE_ALIAS("platform:tps65912-pmic");