s390/ptrace: get rid of long longs in psw_bits
[linux/fpc-iii.git] / drivers / regulator / tps65912-regulator.c
blob9503d5481a52370716c70c0a197917f7e964275b
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 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;
130 int range;
132 switch (id) {
133 case TPS65912_REG_DCDC1:
134 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
135 break;
136 case TPS65912_REG_DCDC2:
137 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
138 break;
139 case TPS65912_REG_DCDC3:
140 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
141 break;
142 case TPS65912_REG_DCDC4:
143 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
144 break;
145 default:
146 return 0;
149 if (range >= 0)
150 range = (range & DCDC_LIMIT_RANGE_MASK)
151 >> DCDC_LIMIT_RANGE_SHIFT;
153 pmic->dcdc_range[id] = range;
154 return range;
157 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
159 unsigned long uv;
161 uv = ((vsel * 12500) + 500000);
162 return uv;
165 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
167 unsigned long uv;
169 uv = ((vsel * 12500) + 700000);
170 return uv;
173 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
175 unsigned long uv;
177 uv = ((vsel * 25000) + 500000);
178 return uv;
181 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
183 unsigned long uv;
185 if (vsel == 0x3f)
186 uv = 3800000;
187 else
188 uv = ((vsel * 50000) + 500000);
190 return uv;
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;
199 else
200 return -EINVAL;
203 static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
205 struct tps65912 *mfd = pmic->mfd;
206 int opvsel;
207 u8 reg = 0;
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;
213 else
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;
217 } else {
218 return -EINVAL;
221 return reg;
224 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
226 switch (id) {
227 case TPS65912_REG_DCDC1:
228 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
229 pmic->eco_reg = TPS65912_DCDC1_AVS;
230 break;
231 case TPS65912_REG_DCDC2:
232 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
233 pmic->eco_reg = TPS65912_DCDC2_AVS;
234 break;
235 case TPS65912_REG_DCDC3:
236 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
237 pmic->eco_reg = TPS65912_DCDC3_AVS;
238 break;
239 case TPS65912_REG_DCDC4:
240 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
241 pmic->eco_reg = TPS65912_DCDC4_AVS;
242 break;
243 default:
244 return -EINVAL;
247 return 0;
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)
257 return -EINVAL;
259 reg = pmic->get_ctrl_reg(id);
260 if (reg < 0)
261 return reg;
263 value = tps65912_reg_read(mfd, reg);
264 if (value < 0)
265 return value;
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);
275 int reg;
277 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
278 return -EINVAL;
280 reg = pmic->get_ctrl_reg(id);
281 if (reg < 0)
282 return reg;
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);
294 if (reg < 0)
295 return reg;
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;
314 switch (mode) {
315 case REGULATOR_MODE_FAST:
316 /* Verify if mode alredy set */
317 if (pwm_mode && !eco)
318 break;
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);
321 break;
322 case REGULATOR_MODE_NORMAL:
323 case REGULATOR_MODE_IDLE:
324 if (!pwm_mode && !eco)
325 break;
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);
328 break;
329 case REGULATOR_MODE_STANDBY:
330 if (!pwm_mode && eco)
331 break;
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);
334 break;
335 default:
336 return -EINVAL;
339 return 0;
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;
363 return mode;
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)
372 return -EINVAL;
374 range = pmic->dcdc_range[id];
376 switch (range) {
377 case 0:
378 /* 0.5 - 1.2875V in 12.5mV steps */
379 voltage = tps65912_vsel_to_uv_range0(selector);
380 break;
381 case 1:
382 /* 0.7 - 1.4875V in 12.5mV steps */
383 voltage = tps65912_vsel_to_uv_range1(selector);
384 break;
385 case 2:
386 /* 0.5 - 2.075V in 25mV steps */
387 voltage = tps65912_vsel_to_uv_range2(selector);
388 break;
389 case 3:
390 /* 0.5 - 3.8V in 50mV steps */
391 voltage = tps65912_vsel_to_uv_range3(selector);
392 break;
394 return voltage;
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);
402 int reg, vsel;
404 reg = tps65912_get_sel_register(pmic, id);
405 if (reg < 0)
406 return reg;
408 vsel = tps65912_reg_read(mfd, reg);
409 vsel &= 0x3F;
411 return vsel;
414 static int tps65912_set_voltage_sel(struct regulator_dev *dev,
415 unsigned selector)
417 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
418 struct tps65912 *mfd = pmic->mfd;
419 int id = rdev_get_id(dev);
420 int value;
421 u8 reg;
423 reg = tps65912_get_sel_register(pmic, id);
424 value = tps65912_reg_read(mfd, reg);
425 value &= 0xC0;
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;
461 int i;
463 pmic_plat_data = dev_get_platdata(tps65912->dev);
464 if (!pmic_plat_data)
465 return -EINVAL;
467 reg_data = pmic_plat_data->tps65912_pmic_init_data;
469 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
470 if (!pmic)
471 return -ENOMEM;
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++) {
481 int range = 0;
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);
493 } else {
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],
505 &config);
506 if (IS_ERR(rdev)) {
507 dev_err(tps65912->dev,
508 "failed to register %s regulator\n",
509 pdev->name);
510 return PTR_ERR(rdev);
513 /* Save regulator for cleanup */
514 pmic->rdev[i] = rdev;
516 return 0;
519 static struct platform_driver tps65912_driver = {
520 .driver = {
521 .name = "tps65912-pmic",
523 .probe = tps65912_probe,
526 static int __init tps65912_init(void)
528 return platform_driver_register(&tps65912_driver);
530 subsys_initcall(tps65912_init);
532 static void __exit tps65912_cleanup(void)
534 platform_driver_unregister(&tps65912_driver);
536 module_exit(tps65912_cleanup);
538 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
539 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
540 MODULE_LICENSE("GPL v2");
541 MODULE_ALIAS("platform:tps65912-pmic");