blob: 9cafaa0f94555a1e8fef93b4c0171be0aaea969f [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[] = {
Axel Lin8828bae2013-10-11 09:32:18 +0800122 REGULATOR_LINEAR_RANGE(800000, 0, 32, 25000),
123 REGULATOR_LINEAR_RANGE(1650000, 33, 60, 50000),
124 REGULATOR_LINEAR_RANGE(3100000, 61, 63, 100000),
Axel Linfba65832013-07-20 16:56:45 +0800125};
126
Margarita Olaya9260ad982011-06-09 14:50:27 -0500127static int tps65912_get_range(struct tps65912_reg *pmic, int id)
128{
129 struct tps65912 *mfd = pmic->mfd;
Axel Lin85c5d862012-03-09 07:05:41 +0800130 int range;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500131
132 switch (id) {
133 case TPS65912_REG_DCDC1:
Axel Lin85c5d862012-03-09 07:05:41 +0800134 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
135 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500136 case TPS65912_REG_DCDC2:
Axel Lin85c5d862012-03-09 07:05:41 +0800137 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
138 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500139 case TPS65912_REG_DCDC3:
Axel Lin85c5d862012-03-09 07:05:41 +0800140 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
141 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500142 case TPS65912_REG_DCDC4:
Axel Lin85c5d862012-03-09 07:05:41 +0800143 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
144 break;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500145 default:
146 return 0;
147 }
Axel Lin85c5d862012-03-09 07:05:41 +0800148
149 if (range >= 0)
150 range = (range & DCDC_LIMIT_RANGE_MASK)
151 >> DCDC_LIMIT_RANGE_SHIFT;
152
153 pmic->dcdc_range[id] = range;
154 return range;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500155}
156
157static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
158{
159 unsigned long uv;
160
161 uv = ((vsel * 12500) + 500000);
162 return uv;
163}
164
165static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
166{
167 unsigned long uv;
168
169 uv = ((vsel * 12500) + 700000);
170 return uv;
171}
172
173static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
174{
175 unsigned long uv;
176
177 uv = ((vsel * 25000) + 500000);
178 return uv;
179}
180
181static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
182{
183 unsigned long uv;
184
185 if (vsel == 0x3f)
186 uv = 3800000;
187 else
188 uv = ((vsel * 50000) + 500000);
189
190 return uv;
191}
192
Margarita Olaya9260ad982011-06-09 14:50:27 -0500193static int tps65912_get_ctrl_register(int id)
194{
Axel Lin42b5efe2012-03-13 06:40:48 +0800195 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
Margarita Olaya9260ad982011-06-09 14:50:27 -0500200 return -EINVAL;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500201}
202
Axel Lin42b5efe2012-03-13 06:40:48 +0800203static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500204{
205 struct tps65912 *mfd = pmic->mfd;
Axel Lin42b5efe2012-03-13 06:40:48 +0800206 int opvsel;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500207 u8 reg = 0;
208
Axel Lin42b5efe2012-03-13 06:40:48 +0800209 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 {
Margarita Olaya9260ad982011-06-09 14:50:27 -0500218 return -EINVAL;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500219 }
220
221 return reg;
222}
223
224static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
225{
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;
245 }
246
247 return 0;
248}
249
250static int tps65912_reg_is_enabled(struct regulator_dev *dev)
251{
252 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
253 struct tps65912 *mfd = pmic->mfd;
254 int reg, value, id = rdev_get_id(dev);
255
256 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
257 return -EINVAL;
258
259 reg = pmic->get_ctrl_reg(id);
260 if (reg < 0)
261 return reg;
262
263 value = tps65912_reg_read(mfd, reg);
264 if (value < 0)
265 return value;
266
267 return value & TPS65912_REG_ENABLED;
268}
269
270static int tps65912_reg_enable(struct regulator_dev *dev)
271{
272 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
273 struct tps65912 *mfd = pmic->mfd;
274 int id = rdev_get_id(dev);
Dan Carpenter6daa6632011-07-06 21:57:18 +0300275 int reg;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500276
277 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
278 return -EINVAL;
279
280 reg = pmic->get_ctrl_reg(id);
281 if (reg < 0)
282 return reg;
283
284 return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
285}
286
287static int tps65912_reg_disable(struct regulator_dev *dev)
288{
289 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
290 struct tps65912 *mfd = pmic->mfd;
291 int id = rdev_get_id(dev), reg;
292
293 reg = pmic->get_ctrl_reg(id);
294 if (reg < 0)
295 return reg;
296
297 return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
298}
299
300static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
301{
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);
305
306 tps65912_get_mode_regiters(pmic, id);
307
308 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
309 eco = tps65912_reg_read(mfd, pmic->eco_reg);
310
311 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
312 eco &= DCDC_AVS_ECO_MASK;
313
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;
337 }
338
339 return 0;
340}
341
342static unsigned int tps65912_get_mode(struct regulator_dev *dev)
343{
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);
347
348 tps65912_get_mode_regiters(pmic, id);
349
350 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
351 eco = tps65912_reg_read(mfd, pmic->eco_reg);
352
353 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
354 eco &= DCDC_AVS_ECO_MASK;
355
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;
362
363 return mode;
364}
365
Axel Linf72d6432012-03-15 17:17:36 +0800366static int tps65912_list_voltage(struct regulator_dev *dev, unsigned selector)
Axel Lin844775e2012-03-08 12:07:37 +0800367{
368 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
369 int range, voltage = 0, id = rdev_get_id(dev);
370
Axel Lin85c5d862012-03-09 07:05:41 +0800371 if (id > TPS65912_REG_DCDC4)
Axel Lin844775e2012-03-08 12:07:37 +0800372 return -EINVAL;
Axel Lin85c5d862012-03-09 07:05:41 +0800373
374 range = pmic->dcdc_range[id];
Axel Lin844775e2012-03-08 12:07:37 +0800375
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;
393 }
394 return voltage;
395}
396
Axel Lin9db7f052012-04-21 11:03:34 +0800397static int tps65912_get_voltage_sel(struct regulator_dev *dev)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500398{
399 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
400 struct tps65912 *mfd = pmic->mfd;
Axel Lin844775e2012-03-08 12:07:37 +0800401 int id = rdev_get_id(dev);
Axel Linc5675562012-03-14 10:20:01 +0800402 int reg, vsel;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500403
Axel Linc5675562012-03-14 10:20:01 +0800404 reg = tps65912_get_sel_register(pmic, id);
405 if (reg < 0)
406 return reg;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500407
Axel Linc5675562012-03-14 10:20:01 +0800408 vsel = tps65912_reg_read(mfd, reg);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500409 vsel &= 0x3F;
410
Axel Lin9db7f052012-04-21 11:03:34 +0800411 return vsel;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500412}
413
Axel Lin831c9862012-03-14 10:18:45 +0800414static int tps65912_set_voltage_sel(struct regulator_dev *dev,
Axel Lin94732b92012-03-09 10:22:20 +0800415 unsigned selector)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500416{
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;
422
Axel Lin42b5efe2012-03-13 06:40:48 +0800423 reg = tps65912_get_sel_register(pmic, id);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500424 value = tps65912_reg_read(mfd, reg);
425 value &= 0xC0;
426 return tps65912_reg_write(mfd, reg, selector | value);
427}
428
Margarita Olaya9260ad982011-06-09 14:50:27 -0500429/* Operations permitted on DCDCx */
430static 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,
Axel Lin9db7f052012-04-21 11:03:34 +0800436 .get_voltage_sel = tps65912_get_voltage_sel,
Axel Lin831c9862012-03-14 10:18:45 +0800437 .set_voltage_sel = tps65912_set_voltage_sel,
Axel Linf72d6432012-03-15 17:17:36 +0800438 .list_voltage = tps65912_list_voltage,
Margarita Olaya9260ad982011-06-09 14:50:27 -0500439};
440
441/* Operations permitted on LDOx */
442static struct regulator_ops tps65912_ops_ldo = {
443 .is_enabled = tps65912_reg_is_enabled,
444 .enable = tps65912_reg_enable,
445 .disable = tps65912_reg_disable,
Axel Lin9db7f052012-04-21 11:03:34 +0800446 .get_voltage_sel = tps65912_get_voltage_sel,
Axel Lin831c9862012-03-14 10:18:45 +0800447 .set_voltage_sel = tps65912_set_voltage_sel,
Axel Linfba65832013-07-20 16:56:45 +0800448 .list_voltage = regulator_list_voltage_linear_range,
449 .map_voltage = regulator_map_voltage_linear_range,
Margarita Olaya9260ad982011-06-09 14:50:27 -0500450};
451
Bill Pembertona5023572012-11-19 13:22:22 -0500452static int tps65912_probe(struct platform_device *pdev)
Margarita Olaya9260ad982011-06-09 14:50:27 -0500453{
454 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
Mark Brownc1727082012-04-04 00:50:22 +0100455 struct regulator_config config = { };
Margarita Olaya9260ad982011-06-09 14:50:27 -0500456 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;
Sachin Kamatab1d65e2013-09-04 17:17:52 +0530461 int i;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500462
463 pmic_plat_data = dev_get_platdata(tps65912->dev);
464 if (!pmic_plat_data)
465 return -EINVAL;
466
467 reg_data = pmic_plat_data->tps65912_pmic_init_data;
468
Axel Lin9eb0c422012-04-11 14:40:18 +0800469 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500470 if (!pmic)
471 return -ENOMEM;
472
473 mutex_init(&pmic->io_lock);
474 pmic->mfd = tps65912;
475 platform_set_drvdata(pdev, pmic);
476
477 pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
478 info = tps65912_regs;
479
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;
484
485 pmic->desc[i].name = info->name;
486 pmic->desc[i].id = i;
487 pmic->desc[i].n_voltages = 64;
Axel Linfba65832013-07-20 16:56:45 +0800488 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;
495 }
Margarita Olaya9260ad982011-06-09 14:50:27 -0500496 pmic->desc[i].type = REGULATOR_VOLTAGE;
497 pmic->desc[i].owner = THIS_MODULE;
498 range = tps65912_get_range(pmic, i);
Mark Brownc1727082012-04-04 00:50:22 +0100499
500 config.dev = tps65912->dev;
501 config.init_data = reg_data;
502 config.driver_data = pmic;
503
Sachin Kamatab1d65e2013-09-04 17:17:52 +0530504 rdev = devm_regulator_register(&pdev->dev, &pmic->desc[i],
505 &config);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500506 if (IS_ERR(rdev)) {
507 dev_err(tps65912->dev,
508 "failed to register %s regulator\n",
509 pdev->name);
Sachin Kamatab1d65e2013-09-04 17:17:52 +0530510 return PTR_ERR(rdev);
Margarita Olaya9260ad982011-06-09 14:50:27 -0500511 }
512
513 /* Save regulator for cleanup */
514 pmic->rdev[i] = rdev;
515 }
516 return 0;
Margarita Olaya9260ad982011-06-09 14:50:27 -0500517}
518
519static struct platform_driver tps65912_driver = {
520 .driver = {
521 .name = "tps65912-pmic",
522 .owner = THIS_MODULE,
523 },
524 .probe = tps65912_probe,
Margarita Olaya9260ad982011-06-09 14:50:27 -0500525};
526
Margarita Olaya9260ad982011-06-09 14:50:27 -0500527static int __init tps65912_init(void)
528{
529 return platform_driver_register(&tps65912_driver);
530}
531subsys_initcall(tps65912_init);
532
Margarita Olaya9260ad982011-06-09 14:50:27 -0500533static void __exit tps65912_cleanup(void)
534{
535 platform_driver_unregister(&tps65912_driver);
536}
537module_exit(tps65912_cleanup);
538
539MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
540MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
541MODULE_LICENSE("GPL v2");
542MODULE_ALIAS("platform:tps65912-pmic");