fq_codel: dont reinit flow state
[linux-2.6/cjktty.git] / drivers / regulator / tps65912-regulator.c
blob18b2a1dcb4b5d6ba189fa74dfe4c8f2d23fbe1d8
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 int tps65912_get_range(struct tps65912_reg *pmic, int id)
123 struct tps65912 *mfd = pmic->mfd;
124 int range;
126 switch (id) {
127 case TPS65912_REG_DCDC1:
128 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
129 break;
130 case TPS65912_REG_DCDC2:
131 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
132 break;
133 case TPS65912_REG_DCDC3:
134 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
135 break;
136 case TPS65912_REG_DCDC4:
137 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
138 break;
139 default:
140 return 0;
143 if (range >= 0)
144 range = (range & DCDC_LIMIT_RANGE_MASK)
145 >> DCDC_LIMIT_RANGE_SHIFT;
147 pmic->dcdc_range[id] = range;
148 return range;
151 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
153 unsigned long uv;
155 uv = ((vsel * 12500) + 500000);
156 return uv;
159 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
161 unsigned long uv;
163 uv = ((vsel * 12500) + 700000);
164 return uv;
167 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
169 unsigned long uv;
171 uv = ((vsel * 25000) + 500000);
172 return uv;
175 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
177 unsigned long uv;
179 if (vsel == 0x3f)
180 uv = 3800000;
181 else
182 uv = ((vsel * 50000) + 500000);
184 return uv;
187 static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel)
189 unsigned long uv = 0;
191 if (vsel <= 32)
192 uv = ((vsel * 25000) + 800000);
193 else if (vsel > 32 && vsel <= 60)
194 uv = (((vsel - 32) * 50000) + 1600000);
195 else if (vsel > 60)
196 uv = (((vsel - 60) * 100000) + 3000000);
198 return uv;
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;
207 else
208 return -EINVAL;
211 static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
213 struct tps65912 *mfd = pmic->mfd;
214 int opvsel;
215 u8 reg = 0;
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;
221 else
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;
225 } else {
226 return -EINVAL;
229 return reg;
232 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
234 switch (id) {
235 case TPS65912_REG_DCDC1:
236 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
237 pmic->eco_reg = TPS65912_DCDC1_AVS;
238 break;
239 case TPS65912_REG_DCDC2:
240 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
241 pmic->eco_reg = TPS65912_DCDC2_AVS;
242 break;
243 case TPS65912_REG_DCDC3:
244 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
245 pmic->eco_reg = TPS65912_DCDC3_AVS;
246 break;
247 case TPS65912_REG_DCDC4:
248 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
249 pmic->eco_reg = TPS65912_DCDC4_AVS;
250 break;
251 default:
252 return -EINVAL;
255 return 0;
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)
265 return -EINVAL;
267 reg = pmic->get_ctrl_reg(id);
268 if (reg < 0)
269 return reg;
271 value = tps65912_reg_read(mfd, reg);
272 if (value < 0)
273 return value;
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);
283 int reg;
285 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
286 return -EINVAL;
288 reg = pmic->get_ctrl_reg(id);
289 if (reg < 0)
290 return reg;
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);
302 if (reg < 0)
303 return reg;
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;
322 switch (mode) {
323 case REGULATOR_MODE_FAST:
324 /* Verify if mode alredy set */
325 if (pwm_mode && !eco)
326 break;
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);
329 break;
330 case REGULATOR_MODE_NORMAL:
331 case REGULATOR_MODE_IDLE:
332 if (!pwm_mode && !eco)
333 break;
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);
336 break;
337 case REGULATOR_MODE_STANDBY:
338 if (!pwm_mode && eco)
339 break;
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);
342 break;
343 default:
344 return -EINVAL;
347 return 0;
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;
371 return mode;
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)
383 return -EINVAL;
385 range = pmic->dcdc_range[id];
387 switch (range) {
388 case 0:
389 /* 0.5 - 1.2875V in 12.5mV steps */
390 voltage = tps65912_vsel_to_uv_range0(selector);
391 break;
392 case 1:
393 /* 0.7 - 1.4875V in 12.5mV steps */
394 voltage = tps65912_vsel_to_uv_range1(selector);
395 break;
396 case 2:
397 /* 0.5 - 2.075V in 25mV steps */
398 voltage = tps65912_vsel_to_uv_range2(selector);
399 break;
400 case 3:
401 /* 0.5 - 3.8V in 50mV steps */
402 voltage = tps65912_vsel_to_uv_range3(selector);
403 break;
405 return voltage;
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);
413 int reg, vsel;
415 reg = tps65912_get_sel_register(pmic, id);
416 if (reg < 0)
417 return reg;
419 vsel = tps65912_reg_read(mfd, reg);
420 vsel &= 0x3F;
422 return vsel;
425 static int tps65912_set_voltage_sel(struct regulator_dev *dev,
426 unsigned selector)
428 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
429 struct tps65912 *mfd = pmic->mfd;
430 int id = rdev_get_id(dev);
431 int value;
432 u8 reg;
434 reg = tps65912_get_sel_register(pmic, id);
435 value = tps65912_reg_read(mfd, reg);
436 value &= 0xC0;
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 __devinit 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;
471 int i, err;
473 pmic_plat_data = dev_get_platdata(tps65912->dev);
474 if (!pmic_plat_data)
475 return -EINVAL;
477 reg_data = pmic_plat_data->tps65912_pmic_init_data;
479 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
480 if (!pmic)
481 return -ENOMEM;
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++) {
491 int range = 0;
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);
509 if (IS_ERR(rdev)) {
510 dev_err(tps65912->dev,
511 "failed to register %s regulator\n",
512 pdev->name);
513 err = PTR_ERR(rdev);
514 goto err;
517 /* Save regulator for cleanup */
518 pmic->rdev[i] = rdev;
520 return 0;
522 err:
523 while (--i >= 0)
524 regulator_unregister(pmic->rdev[i]);
525 return err;
528 static int __devexit tps65912_remove(struct platform_device *pdev)
530 struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
531 int i;
533 for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
534 regulator_unregister(tps65912_reg->rdev[i]);
535 return 0;
538 static struct platform_driver tps65912_driver = {
539 .driver = {
540 .name = "tps65912-pmic",
541 .owner = THIS_MODULE,
543 .probe = tps65912_probe,
544 .remove = __devexit_p(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");