blob: 1ed4d049abfe0dde3ee9cbcf5596d0c4059373b1 [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>
Margarita Olaya9260ad982011-06-09 14:50:27 -050023#include <linux/slab.h>
24#include <linux/gpio.h>
25#include <linux/mfd/tps65912.h>
26
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
32
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
44
Margarita Olaya9260ad982011-06-09 14:50:27 -050045/* Number of step-down converters available */
46#define TPS65912_NUM_DCDC 4
47
48/* Number of LDO voltage regulators available */
49#define TPS65912_NUM_LDO 10
50
51/* Number of total regulators available */
52#define TPS65912_NUM_REGULATOR (TPS65912_NUM_DCDC + TPS65912_NUM_LDO)
53
54#define TPS65912_REG_ENABLED 0x80
55#define OP_SELREG_MASK 0x40
56#define OP_SELREG_SHIFT 6
57
58struct tps_info {
59 const char *name;
60};
61
62static struct tps_info tps65912_regs[] = {
63 {
64 .name = "DCDC1",
65 },
66 {
67 .name = "DCDC2",
68 },
69 {
70 .name = "DCDC3",
71 },
72 {
73 .name = "DCDC4",
74 },
75 {
76 .name = "LDO1",
77 },
78 {
79 .name = "LDO2",
80 },
81 {
82 .name = "LDO3",
83 },
84 {
85 .name = "LDO4",
86 },
87 {
88 .name = "LDO5",
89 },
90 {
91 .name = "LDO6",
92 },
93 {
94 .name = "LDO7",
95 },
96 {
97 .name = "LDO8",
98 },
99 {
100 .name = "LDO9",
101 },
102 {
103 .name = "LDO10",
104 },
105};
106
107struct 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);
Axel Lin85c5d862012-03-09 07:05:41 +0800116 int dcdc_range[TPS65912_NUM_DCDC];
Margarita Olaya9260ad982011-06-09 14:50:27 -0500117 int pwm_mode_reg;
118 int eco_reg;
119};
120
Axel Linfba65832013-07-20 16:56:45 +0800121static 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 },
128};
129
Margarita Olaya9260ad982011-06-09 14:50:27 -0500130static int tps65912_get_range(struct tps65912_reg *pmic, int id)
131{
132 struct tps65912 *mfd = pmic->mfd;
Axel Lin85c5d862012-03-09 07:05:41 +0800133 int range;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500134
135 switch (id) {
136 case TPS65912_REG_DCDC1:
Axel Lin85c5d862012-03-09 07:05:41 +0800137 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
138 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500139 case TPS65912_REG_DCDC2:
Axel Lin85c5d862012-03-09 07:05:41 +0800140 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
141 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500142 case TPS65912_REG_DCDC3:
Axel Lin85c5d862012-03-09 07:05:41 +0800143 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
144 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500145 case TPS65912_REG_DCDC4:
Axel Lin85c5d862012-03-09 07:05:41 +0800146 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
147 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500148 default:
149 return 0;
150 }
Axel Lin85c5d862012-03-09 07:05:41 +0800151
152 if (range >= 0)
153 range = (range & DCDC_LIMIT_RANGE_MASK)
154 >> DCDC_LIMIT_RANGE_SHIFT;
155
156 pmic->dcdc_range[id] = range;
157 return range;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500158}
159
160static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
161{
162 unsigned long uv;
163
164 uv = ((vsel * 12500) + 500000);
165 return uv;
166}
167
168static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
169{
170 unsigned long uv;
171
172 uv = ((vsel * 12500) + 700000);
173 return uv;
174}
175
176static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
177{
178 unsigned long uv;
179
180 uv = ((vsel * 25000) + 500000);
181 return uv;
182}
183
184static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
185{
186 unsigned long uv;
187
188 if (vsel == 0x3f)
189 uv = 3800000;
190 else
191 uv = ((vsel * 50000) + 500000);
192
193 return uv;
194}
195
Margarita Olaya9260ad982011-06-09 14:50:27 -0500196static int tps65912_get_ctrl_register(int id)
197{
Axel Lin42b5efe2012-03-13 06:40:48 +0800198 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
Margarita Olaya9260ad982011-06-09 14:50:27 -0500203 return -EINVAL;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500204}
205
Axel Lin42b5efe2012-03-13 06:40:48 +0800206static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500207{
208 struct tps65912 *mfd = pmic->mfd;
Axel Lin42b5efe2012-03-13 06:40:48 +0800209 int opvsel;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500210 u8 reg = 0;
211
Axel Lin42b5efe2012-03-13 06:40:48 +0800212 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 {
Margarita Olaya9260ad982011-06-09 14:50:27 -0500221 return -EINVAL;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500222 }
223
224 return reg;
225}
226
227static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
228{
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;
248 }
249
250 return 0;
251}
252
253static int tps65912_reg_is_enabled(struct regulator_dev *dev)
254{
255 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
256 struct tps65912 *mfd = pmic->mfd;
257 int reg, value, id = rdev_get_id(dev);
258
259 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
260 return -EINVAL;
261
262 reg = pmic->get_ctrl_reg(id);
263 if (reg < 0)
264 return reg;
265
266 value = tps65912_reg_read(mfd, reg);
267 if (value < 0)
268 return value;
269
270 return value & TPS65912_REG_ENABLED;
271}
272
273static int tps65912_reg_enable(struct regulator_dev *dev)
274{
275 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
276 struct tps65912 *mfd = pmic->mfd;
277 int id = rdev_get_id(dev);
Dan Carpenter6daa6632011-07-06 21:57:18 +0300278 int reg;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500279
280 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
281 return -EINVAL;
282
283 reg = pmic->get_ctrl_reg(id);
284 if (reg < 0)
285 return reg;
286
287 return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
288}
289
290static int tps65912_reg_disable(struct regulator_dev *dev)
291{
292 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
293 struct tps65912 *mfd = pmic->mfd;
294 int id = rdev_get_id(dev), reg;
295
296 reg = pmic->get_ctrl_reg(id);
297 if (reg < 0)
298 return reg;
299
300 return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
301}
302
303static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
304{
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);
308
309 tps65912_get_mode_regiters(pmic, id);
310
311 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
312 eco = tps65912_reg_read(mfd, pmic->eco_reg);
313
314 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
315 eco &= DCDC_AVS_ECO_MASK;
316
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;
340 }
341
342 return 0;
343}
344
345static unsigned int tps65912_get_mode(struct regulator_dev *dev)
346{
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);
350
351 tps65912_get_mode_regiters(pmic, id);
352
353 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
354 eco = tps65912_reg_read(mfd, pmic->eco_reg);
355
356 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
357 eco &= DCDC_AVS_ECO_MASK;
358
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;
365
366 return mode;
367}
368
Axel Linf72d6432012-03-15 17:17:36 +0800369static int tps65912_list_voltage(struct regulator_dev *dev, unsigned selector)
Axel Lin844775e2012-03-08 12:07:37 +0800370{
371 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
372 int range, voltage = 0, id = rdev_get_id(dev);
373
Axel Lin85c5d862012-03-09 07:05:41 +0800374 if (id > TPS65912_REG_DCDC4)
Axel Lin844775e2012-03-08 12:07:37 +0800375 return -EINVAL;
Axel Lin85c5d862012-03-09 07:05:41 +0800376
377 range = pmic->dcdc_range[id];
Axel Lin844775e2012-03-08 12:07:37 +0800378
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;
396 }
397 return voltage;
398}
399
Axel Lin9db7f052012-04-21 11:03:34 +0800400static int tps65912_get_voltage_sel(struct regulator_dev *dev)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500401{
402 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
403 struct tps65912 *mfd = pmic->mfd;
Axel Lin844775e2012-03-08 12:07:37 +0800404 int id = rdev_get_id(dev);
Axel Linc5675562012-03-14 10:20:01 +0800405 int reg, vsel;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500406
Axel Linc5675562012-03-14 10:20:01 +0800407 reg = tps65912_get_sel_register(pmic, id);
408 if (reg < 0)
409 return reg;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500410
Axel Linc5675562012-03-14 10:20:01 +0800411 vsel = tps65912_reg_read(mfd, reg);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500412 vsel &= 0x3F;
413
Axel Lin9db7f052012-04-21 11:03:34 +0800414 return vsel;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500415}
416
Axel Lin831c9862012-03-14 10:18:45 +0800417static int tps65912_set_voltage_sel(struct regulator_dev *dev,
Axel Lin94732b92012-03-09 10:22:20 +0800418 unsigned selector)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500419{
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;
425
Axel Lin42b5efe2012-03-13 06:40:48 +0800426 reg = tps65912_get_sel_register(pmic, id);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500427 value = tps65912_reg_read(mfd, reg);
428 value &= 0xC0;
429 return tps65912_reg_write(mfd, reg, selector | value);
430}
431
Margarita Olaya9260ad982011-06-09 14:50:27 -0500432/* Operations permitted on DCDCx */
433static 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,
Axel Lin9db7f052012-04-21 11:03:34 +0800439 .get_voltage_sel = tps65912_get_voltage_sel,
Axel Lin831c9862012-03-14 10:18:45 +0800440 .set_voltage_sel = tps65912_set_voltage_sel,
Axel Linf72d6432012-03-15 17:17:36 +0800441 .list_voltage = tps65912_list_voltage,
Margarita Olaya9260ad982011-06-09 14:50:27 -0500442};
443
444/* Operations permitted on LDOx */
445static struct regulator_ops tps65912_ops_ldo = {
446 .is_enabled = tps65912_reg_is_enabled,
447 .enable = tps65912_reg_enable,
448 .disable = tps65912_reg_disable,
Axel Lin9db7f052012-04-21 11:03:34 +0800449 .get_voltage_sel = tps65912_get_voltage_sel,
Axel Lin831c9862012-03-14 10:18:45 +0800450 .set_voltage_sel = tps65912_set_voltage_sel,
Axel Linfba65832013-07-20 16:56:45 +0800451 .list_voltage = regulator_list_voltage_linear_range,
452 .map_voltage = regulator_map_voltage_linear_range,
Margarita Olaya9260ad982011-06-09 14:50:27 -0500453};
454
Bill Pembertona5023572012-11-19 13:22:22 -0500455static int tps65912_probe(struct platform_device *pdev)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500456{
457 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
Mark Brownc1727082012-04-04 00:50:22 +0100458 struct regulator_config config = { };
Margarita Olaya9260ad982011-06-09 14:50:27 -0500459 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;
Sachin Kamatab1d65e2013-09-04 17:17:52 +0530464 int i;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500465
466 pmic_plat_data = dev_get_platdata(tps65912->dev);
467 if (!pmic_plat_data)
468 return -EINVAL;
469
470 reg_data = pmic_plat_data->tps65912_pmic_init_data;
471
Axel Lin9eb0c422012-04-11 14:40:18 +0800472 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500473 if (!pmic)
474 return -ENOMEM;
475
476 mutex_init(&pmic->io_lock);
477 pmic->mfd = tps65912;
478 platform_set_drvdata(pdev, pmic);
479
480 pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
481 info = tps65912_regs;
482
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;
487
488 pmic->desc[i].name = info->name;
489 pmic->desc[i].id = i;
490 pmic->desc[i].n_voltages = 64;
Axel Linfba65832013-07-20 16:56:45 +0800491 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;
498 }
Margarita Olaya9260ad982011-06-09 14:50:27 -0500499 pmic->desc[i].type = REGULATOR_VOLTAGE;
500 pmic->desc[i].owner = THIS_MODULE;
501 range = tps65912_get_range(pmic, i);
Mark Brownc1727082012-04-04 00:50:22 +0100502
503 config.dev = tps65912->dev;
504 config.init_data = reg_data;
505 config.driver_data = pmic;
506
Sachin Kamatab1d65e2013-09-04 17:17:52 +0530507 rdev = devm_regulator_register(&pdev->dev, &pmic->desc[i],
508 &config);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500509 if (IS_ERR(rdev)) {
510 dev_err(tps65912->dev,
511 "failed to register %s regulator\n",
512 pdev->name);
Sachin Kamatab1d65e2013-09-04 17:17:52 +0530513 return PTR_ERR(rdev);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500514 }
515
516 /* Save regulator for cleanup */
517 pmic->rdev[i] = rdev;
518 }
519 return 0;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500520}
521
522static struct platform_driver tps65912_driver = {
523 .driver = {
524 .name = "tps65912-pmic",
525 .owner = THIS_MODULE,
526 },
527 .probe = tps65912_probe,
Margarita Olaya9260ad982011-06-09 14:50:27 -0500528};
529
Margarita Olaya9260ad982011-06-09 14:50:27 -0500530static int __init tps65912_init(void)
531{
532 return platform_driver_register(&tps65912_driver);
533}
534subsys_initcall(tps65912_init);
535
Margarita Olaya9260ad982011-06-09 14:50:27 -0500536static void __exit tps65912_cleanup(void)
537{
538 platform_driver_unregister(&tps65912_driver);
539}
540module_exit(tps65912_cleanup);
541
542MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
543MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
544MODULE_LICENSE("GPL v2");
545MODULE_ALIAS("platform:tps65912-pmic");