blob: b36799b1f530d0bf81484d471eea483c30a4914e [file] [log] [blame]
Margarita Olaya9260ad982011-06-09 14:50:27 -05001/*
2 * tps65912.c -- TI tps65912
3 *
4 * Copyright 2011 Texas Instruments Inc.
5 *
6 * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
7 *
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.
12 *
13 * This driver is based on wm8350 implementation.
14 */
15
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/delay.h>
24#include <linux/slab.h>
25#include <linux/gpio.h>
26#include <linux/mfd/tps65912.h>
27
28/* DCDC's */
29#define TPS65912_REG_DCDC1 0
30#define TPS65912_REG_DCDC2 1
31#define TPS65912_REG_DCDC3 2
32#define TPS65912_REG_DCDC4 3
33
34/* LDOs */
35#define TPS65912_REG_LDO1 4
36#define TPS65912_REG_LDO2 5
37#define TPS65912_REG_LDO3 6
38#define TPS65912_REG_LDO4 7
39#define TPS65912_REG_LDO5 8
40#define TPS65912_REG_LDO6 9
41#define TPS65912_REG_LDO7 10
42#define TPS65912_REG_LDO8 11
43#define TPS65912_REG_LDO9 12
44#define TPS65912_REG_LDO10 13
45
Margarita Olaya9260ad982011-06-09 14:50:27 -050046/* Number of step-down converters available */
47#define TPS65912_NUM_DCDC 4
48
49/* Number of LDO voltage regulators available */
50#define TPS65912_NUM_LDO 10
51
52/* Number of total regulators available */
53#define TPS65912_NUM_REGULATOR (TPS65912_NUM_DCDC + TPS65912_NUM_LDO)
54
55#define TPS65912_REG_ENABLED 0x80
56#define OP_SELREG_MASK 0x40
57#define OP_SELREG_SHIFT 6
58
59struct tps_info {
60 const char *name;
61};
62
63static struct tps_info tps65912_regs[] = {
64 {
65 .name = "DCDC1",
66 },
67 {
68 .name = "DCDC2",
69 },
70 {
71 .name = "DCDC3",
72 },
73 {
74 .name = "DCDC4",
75 },
76 {
77 .name = "LDO1",
78 },
79 {
80 .name = "LDO2",
81 },
82 {
83 .name = "LDO3",
84 },
85 {
86 .name = "LDO4",
87 },
88 {
89 .name = "LDO5",
90 },
91 {
92 .name = "LDO6",
93 },
94 {
95 .name = "LDO7",
96 },
97 {
98 .name = "LDO8",
99 },
100 {
101 .name = "LDO9",
102 },
103 {
104 .name = "LDO10",
105 },
106};
107
108struct tps65912_reg {
109 struct regulator_desc desc[TPS65912_NUM_REGULATOR];
110 struct tps65912 *mfd;
111 struct regulator_dev *rdev[TPS65912_NUM_REGULATOR];
112 struct tps_info *info[TPS65912_NUM_REGULATOR];
113 /* for read/write access */
114 struct mutex io_lock;
115 int mode;
116 int (*get_ctrl_reg)(int);
Axel Lin85c5d862012-03-09 07:05:41 +0800117 int dcdc_range[TPS65912_NUM_DCDC];
Margarita Olaya9260ad982011-06-09 14:50:27 -0500118 int pwm_mode_reg;
119 int eco_reg;
120};
121
122static int tps65912_get_range(struct tps65912_reg *pmic, int id)
123{
124 struct tps65912 *mfd = pmic->mfd;
Axel Lin85c5d862012-03-09 07:05:41 +0800125 int range;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500126
127 switch (id) {
128 case TPS65912_REG_DCDC1:
Axel Lin85c5d862012-03-09 07:05:41 +0800129 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
130 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500131 case TPS65912_REG_DCDC2:
Axel Lin85c5d862012-03-09 07:05:41 +0800132 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
133 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500134 case TPS65912_REG_DCDC3:
Axel Lin85c5d862012-03-09 07:05:41 +0800135 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
136 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500137 case TPS65912_REG_DCDC4:
Axel Lin85c5d862012-03-09 07:05:41 +0800138 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
139 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500140 default:
141 return 0;
142 }
Axel Lin85c5d862012-03-09 07:05:41 +0800143
144 if (range >= 0)
145 range = (range & DCDC_LIMIT_RANGE_MASK)
146 >> DCDC_LIMIT_RANGE_SHIFT;
147
148 pmic->dcdc_range[id] = range;
149 return range;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500150}
151
152static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
153{
154 unsigned long uv;
155
156 uv = ((vsel * 12500) + 500000);
157 return uv;
158}
159
160static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
161{
162 unsigned long uv;
163
164 uv = ((vsel * 12500) + 700000);
165 return uv;
166}
167
168static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
169{
170 unsigned long uv;
171
172 uv = ((vsel * 25000) + 500000);
173 return uv;
174}
175
176static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
177{
178 unsigned long uv;
179
180 if (vsel == 0x3f)
181 uv = 3800000;
182 else
183 uv = ((vsel * 50000) + 500000);
184
185 return uv;
186}
187
188static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel)
189{
190 unsigned long uv = 0;
191
192 if (vsel <= 32)
193 uv = ((vsel * 25000) + 800000);
194 else if (vsel > 32 && vsel <= 60)
195 uv = (((vsel - 32) * 50000) + 1600000);
196 else if (vsel > 60)
197 uv = (((vsel - 60) * 100000) + 3000000);
198
199 return uv;
200}
201
202static int tps65912_get_ctrl_register(int id)
203{
Axel Lin42b5efe2012-03-13 06:40:48 +0800204 if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4)
205 return id * 3 + TPS65912_DCDC1_AVS;
206 else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10)
207 return id - TPS65912_REG_LDO5 + TPS65912_LDO5;
208 else
Margarita Olaya9260ad982011-06-09 14:50:27 -0500209 return -EINVAL;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500210}
211
Axel Lin42b5efe2012-03-13 06:40:48 +0800212static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500213{
214 struct tps65912 *mfd = pmic->mfd;
Axel Lin42b5efe2012-03-13 06:40:48 +0800215 int opvsel;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500216 u8 reg = 0;
217
Axel Lin42b5efe2012-03-13 06:40:48 +0800218 if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) {
219 opvsel = tps65912_reg_read(mfd, id * 3 + TPS65912_DCDC1_OP);
220 if (opvsel & OP_SELREG_MASK)
221 reg = id * 3 + TPS65912_DCDC1_AVS;
222 else
223 reg = id * 3 + TPS65912_DCDC1_OP;
224 } else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) {
225 reg = id - TPS65912_REG_LDO5 + TPS65912_LDO5;
226 } else {
Margarita Olaya9260ad982011-06-09 14:50:27 -0500227 return -EINVAL;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500228 }
229
230 return reg;
231}
232
233static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
234{
235 switch (id) {
236 case TPS65912_REG_DCDC1:
237 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
238 pmic->eco_reg = TPS65912_DCDC1_AVS;
239 break;
240 case TPS65912_REG_DCDC2:
241 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
242 pmic->eco_reg = TPS65912_DCDC2_AVS;
243 break;
244 case TPS65912_REG_DCDC3:
245 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
246 pmic->eco_reg = TPS65912_DCDC3_AVS;
247 break;
248 case TPS65912_REG_DCDC4:
249 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
250 pmic->eco_reg = TPS65912_DCDC4_AVS;
251 break;
252 default:
253 return -EINVAL;
254 }
255
256 return 0;
257}
258
259static int tps65912_reg_is_enabled(struct regulator_dev *dev)
260{
261 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
262 struct tps65912 *mfd = pmic->mfd;
263 int reg, value, id = rdev_get_id(dev);
264
265 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
266 return -EINVAL;
267
268 reg = pmic->get_ctrl_reg(id);
269 if (reg < 0)
270 return reg;
271
272 value = tps65912_reg_read(mfd, reg);
273 if (value < 0)
274 return value;
275
276 return value & TPS65912_REG_ENABLED;
277}
278
279static int tps65912_reg_enable(struct regulator_dev *dev)
280{
281 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
282 struct tps65912 *mfd = pmic->mfd;
283 int id = rdev_get_id(dev);
Dan Carpenter6daa6632011-07-06 21:57:18 +0300284 int reg;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500285
286 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
287 return -EINVAL;
288
289 reg = pmic->get_ctrl_reg(id);
290 if (reg < 0)
291 return reg;
292
293 return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
294}
295
296static int tps65912_reg_disable(struct regulator_dev *dev)
297{
298 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
299 struct tps65912 *mfd = pmic->mfd;
300 int id = rdev_get_id(dev), reg;
301
302 reg = pmic->get_ctrl_reg(id);
303 if (reg < 0)
304 return reg;
305
306 return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
307}
308
309static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
310{
311 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
312 struct tps65912 *mfd = pmic->mfd;
313 int pwm_mode, eco, id = rdev_get_id(dev);
314
315 tps65912_get_mode_regiters(pmic, id);
316
317 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
318 eco = tps65912_reg_read(mfd, pmic->eco_reg);
319
320 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
321 eco &= DCDC_AVS_ECO_MASK;
322
323 switch (mode) {
324 case REGULATOR_MODE_FAST:
325 /* Verify if mode alredy set */
326 if (pwm_mode && !eco)
327 break;
328 tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
329 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
330 break;
331 case REGULATOR_MODE_NORMAL:
332 case REGULATOR_MODE_IDLE:
333 if (!pwm_mode && !eco)
334 break;
335 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
336 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
337 break;
338 case REGULATOR_MODE_STANDBY:
339 if (!pwm_mode && eco)
340 break;
341 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
342 tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
343 break;
344 default:
345 return -EINVAL;
346 }
347
348 return 0;
349}
350
351static unsigned int tps65912_get_mode(struct regulator_dev *dev)
352{
353 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
354 struct tps65912 *mfd = pmic->mfd;
355 int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);
356
357 tps65912_get_mode_regiters(pmic, id);
358
359 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
360 eco = tps65912_reg_read(mfd, pmic->eco_reg);
361
362 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
363 eco &= DCDC_AVS_ECO_MASK;
364
365 if (pwm_mode && !eco)
366 mode = REGULATOR_MODE_FAST;
367 else if (!pwm_mode && !eco)
368 mode = REGULATOR_MODE_NORMAL;
369 else if (!pwm_mode && eco)
370 mode = REGULATOR_MODE_STANDBY;
371
372 return mode;
373}
374
Axel Lin844775e2012-03-08 12:07:37 +0800375static int tps65912_list_voltage_dcdc(struct regulator_dev *dev,
376 unsigned selector)
377{
378 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
379 int range, voltage = 0, id = rdev_get_id(dev);
380
Axel Lin85c5d862012-03-09 07:05:41 +0800381 if (id > TPS65912_REG_DCDC4)
Axel Lin844775e2012-03-08 12:07:37 +0800382 return -EINVAL;
Axel Lin85c5d862012-03-09 07:05:41 +0800383
384 range = pmic->dcdc_range[id];
Axel Lin844775e2012-03-08 12:07:37 +0800385
386 switch (range) {
387 case 0:
388 /* 0.5 - 1.2875V in 12.5mV steps */
389 voltage = tps65912_vsel_to_uv_range0(selector);
390 break;
391 case 1:
392 /* 0.7 - 1.4875V in 12.5mV steps */
393 voltage = tps65912_vsel_to_uv_range1(selector);
394 break;
395 case 2:
396 /* 0.5 - 2.075V in 25mV steps */
397 voltage = tps65912_vsel_to_uv_range2(selector);
398 break;
399 case 3:
400 /* 0.5 - 3.8V in 50mV steps */
401 voltage = tps65912_vsel_to_uv_range3(selector);
402 break;
403 }
404 return voltage;
405}
406
Margarita Olaya9260ad982011-06-09 14:50:27 -0500407static int tps65912_get_voltage_dcdc(struct regulator_dev *dev)
408{
409 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
410 struct tps65912 *mfd = pmic->mfd;
Axel Lin844775e2012-03-08 12:07:37 +0800411 int id = rdev_get_id(dev);
Axel Linc5675562012-03-14 10:20:01 +0800412 int reg, vsel;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500413
Axel Linc5675562012-03-14 10:20:01 +0800414 reg = tps65912_get_sel_register(pmic, id);
415 if (reg < 0)
416 return reg;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500417
Axel Linc5675562012-03-14 10:20:01 +0800418 vsel = tps65912_reg_read(mfd, reg);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500419 vsel &= 0x3F;
420
Axel Lin844775e2012-03-08 12:07:37 +0800421 return tps65912_list_voltage_dcdc(dev, vsel);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500422}
423
Axel Lin831c9862012-03-14 10:18:45 +0800424static int tps65912_set_voltage_sel(struct regulator_dev *dev,
Axel Lin94732b92012-03-09 10:22:20 +0800425 unsigned selector)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500426{
427 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
428 struct tps65912 *mfd = pmic->mfd;
429 int id = rdev_get_id(dev);
430 int value;
431 u8 reg;
432
Axel Lin42b5efe2012-03-13 06:40:48 +0800433 reg = tps65912_get_sel_register(pmic, id);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500434 value = tps65912_reg_read(mfd, reg);
435 value &= 0xC0;
436 return tps65912_reg_write(mfd, reg, selector | value);
437}
438
439static int tps65912_get_voltage_ldo(struct regulator_dev *dev)
440{
441 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
442 struct tps65912 *mfd = pmic->mfd;
443 int id = rdev_get_id(dev);
444 int vsel = 0;
445 u8 reg;
446
Axel Lin42b5efe2012-03-13 06:40:48 +0800447 reg = tps65912_get_sel_register(pmic, id);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500448 vsel = tps65912_reg_read(mfd, reg);
449 vsel &= 0x3F;
450
451 return tps65912_vsel_to_uv_ldo(vsel);
452}
453
Margarita Olaya9260ad982011-06-09 14:50:27 -0500454static int tps65912_list_voltage_ldo(struct regulator_dev *dev,
455 unsigned selector)
456{
457 int ldo = rdev_get_id(dev);
458
459 if (ldo < TPS65912_REG_LDO1 || ldo > TPS65912_REG_LDO10)
460 return -EINVAL;
461
462 return tps65912_vsel_to_uv_ldo(selector);
463}
464
465/* Operations permitted on DCDCx */
466static struct regulator_ops tps65912_ops_dcdc = {
467 .is_enabled = tps65912_reg_is_enabled,
468 .enable = tps65912_reg_enable,
469 .disable = tps65912_reg_disable,
470 .set_mode = tps65912_set_mode,
471 .get_mode = tps65912_get_mode,
472 .get_voltage = tps65912_get_voltage_dcdc,
Axel Lin831c9862012-03-14 10:18:45 +0800473 .set_voltage_sel = tps65912_set_voltage_sel,
Margarita Olaya9260ad982011-06-09 14:50:27 -0500474 .list_voltage = tps65912_list_voltage_dcdc,
475};
476
477/* Operations permitted on LDOx */
478static struct regulator_ops tps65912_ops_ldo = {
479 .is_enabled = tps65912_reg_is_enabled,
480 .enable = tps65912_reg_enable,
481 .disable = tps65912_reg_disable,
482 .get_voltage = tps65912_get_voltage_ldo,
Axel Lin831c9862012-03-14 10:18:45 +0800483 .set_voltage_sel = tps65912_set_voltage_sel,
Margarita Olaya9260ad982011-06-09 14:50:27 -0500484 .list_voltage = tps65912_list_voltage_ldo,
485};
486
487static __devinit int tps65912_probe(struct platform_device *pdev)
488{
489 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
490 struct tps_info *info;
491 struct regulator_init_data *reg_data;
492 struct regulator_dev *rdev;
493 struct tps65912_reg *pmic;
494 struct tps65912_board *pmic_plat_data;
495 int i, err;
496
497 pmic_plat_data = dev_get_platdata(tps65912->dev);
498 if (!pmic_plat_data)
499 return -EINVAL;
500
501 reg_data = pmic_plat_data->tps65912_pmic_init_data;
502
503 pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
504 if (!pmic)
505 return -ENOMEM;
506
507 mutex_init(&pmic->io_lock);
508 pmic->mfd = tps65912;
509 platform_set_drvdata(pdev, pmic);
510
511 pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
512 info = tps65912_regs;
513
514 for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
515 int range = 0;
516 /* Register the regulators */
517 pmic->info[i] = info;
518
519 pmic->desc[i].name = info->name;
520 pmic->desc[i].id = i;
521 pmic->desc[i].n_voltages = 64;
522 pmic->desc[i].ops = (i > TPS65912_REG_DCDC4 ?
523 &tps65912_ops_ldo : &tps65912_ops_dcdc);
524 pmic->desc[i].type = REGULATOR_VOLTAGE;
525 pmic->desc[i].owner = THIS_MODULE;
526 range = tps65912_get_range(pmic, i);
527 rdev = regulator_register(&pmic->desc[i],
Rajendra Nayak2c043bc2011-11-18 16:47:19 +0530528 tps65912->dev, reg_data, pmic, NULL);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500529 if (IS_ERR(rdev)) {
530 dev_err(tps65912->dev,
531 "failed to register %s regulator\n",
532 pdev->name);
533 err = PTR_ERR(rdev);
534 goto err;
535 }
536
537 /* Save regulator for cleanup */
538 pmic->rdev[i] = rdev;
539 }
540 return 0;
541
542err:
543 while (--i >= 0)
544 regulator_unregister(pmic->rdev[i]);
545
546 kfree(pmic);
547 return err;
548}
549
550static int __devexit tps65912_remove(struct platform_device *pdev)
551{
552 struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
553 int i;
554
555 for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
556 regulator_unregister(tps65912_reg->rdev[i]);
557
558 kfree(tps65912_reg);
559 return 0;
560}
561
562static struct platform_driver tps65912_driver = {
563 .driver = {
564 .name = "tps65912-pmic",
565 .owner = THIS_MODULE,
566 },
567 .probe = tps65912_probe,
568 .remove = __devexit_p(tps65912_remove),
569};
570
Margarita Olaya9260ad982011-06-09 14:50:27 -0500571static int __init tps65912_init(void)
572{
573 return platform_driver_register(&tps65912_driver);
574}
575subsys_initcall(tps65912_init);
576
Margarita Olaya9260ad982011-06-09 14:50:27 -0500577static void __exit tps65912_cleanup(void)
578{
579 platform_driver_unregister(&tps65912_driver);
580}
581module_exit(tps65912_cleanup);
582
583MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
584MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
585MODULE_LICENSE("GPL v2");
586MODULE_ALIAS("platform:tps65912-pmic");