regulator: tps6507x: Remove unused min_uV and max_uV from struct tps_info
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / regulator / tps6507x-regulator.c
CommitLineData
3fa5b8e0
AA
1/*
2 * tps6507x-regulator.c
3 *
4 * Regulator driver for TPS65073 PMIC
5 *
6 * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/machine.h>
7d14831e 25#include <linux/regulator/tps6507x.h>
5a0e3ad6 26#include <linux/slab.h>
d183fcc9 27#include <linux/mfd/tps6507x.h>
3fa5b8e0
AA
28
29/* DCDC's */
30#define TPS6507X_DCDC_1 0
31#define TPS6507X_DCDC_2 1
32#define TPS6507X_DCDC_3 2
33/* LDOs */
34#define TPS6507X_LDO_1 3
35#define TPS6507X_LDO_2 4
36
37#define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
38
39/* Number of step-down converters available */
40#define TPS6507X_NUM_DCDC 3
41/* Number of LDO voltage regulators available */
42#define TPS6507X_NUM_LDO 2
43/* Number of total regulators available */
44#define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
45
055917ac
AL
46/* Supported voltage values for regulators (in microVolts) */
47static const unsigned int VDCDCx_VSEL_table[] = {
48 725000, 750000, 775000, 800000,
49 825000, 850000, 875000, 900000,
50 925000, 950000, 975000, 1000000,
51 1025000, 1050000, 1075000, 1100000,
52 1125000, 1150000, 1175000, 1200000,
53 1225000, 1250000, 1275000, 1300000,
54 1325000, 1350000, 1375000, 1400000,
55 1425000, 1450000, 1475000, 1500000,
56 1550000, 1600000, 1650000, 1700000,
57 1750000, 1800000, 1850000, 1900000,
58 1950000, 2000000, 2050000, 2100000,
59 2150000, 2200000, 2250000, 2300000,
60 2350000, 2400000, 2450000, 2500000,
61 2550000, 2600000, 2650000, 2700000,
62 2750000, 2800000, 2850000, 2900000,
63 3000000, 3100000, 3200000, 3300000,
3fa5b8e0
AA
64};
65
055917ac
AL
66static const unsigned int LDO1_VSEL_table[] = {
67 1000000, 1100000, 1200000, 1250000,
68 1300000, 1350000, 1400000, 1500000,
69 1600000, 1800000, 2500000, 2750000,
70 2800000, 3000000, 3100000, 3300000,
3fa5b8e0
AA
71};
72
055917ac
AL
73static const unsigned int LDO2_VSEL_table[] = {
74 725000, 750000, 775000, 800000,
75 825000, 850000, 875000, 900000,
76 925000, 950000, 975000, 1000000,
77 1025000, 1050000, 1075000, 1100000,
78 1125000, 1150000, 1175000, 1200000,
79 1225000, 1250000, 1275000, 1300000,
80 1325000, 1350000, 1375000, 1400000,
81 1425000, 1450000, 1475000, 1500000,
82 1550000, 1600000, 1650000, 1700000,
83 1750000, 1800000, 1850000, 1900000,
84 1950000, 2000000, 2050000, 2100000,
85 2150000, 2200000, 2250000, 2300000,
86 2350000, 2400000, 2450000, 2500000,
87 2550000, 2600000, 2650000, 2700000,
88 2750000, 2800000, 2850000, 2900000,
89 3000000, 3100000, 3200000, 3300000,
3fa5b8e0
AA
90};
91
3fa5b8e0
AA
92struct tps_info {
93 const char *name;
3fa5b8e0 94 u8 table_len;
055917ac 95 const unsigned int *table;
7d14831e
AA
96
97 /* Does DCDC high or the low register defines output voltage? */
98 bool defdcdc_default;
3fa5b8e0
AA
99};
100
7d14831e 101static struct tps_info tps6507x_pmic_regs[] = {
31dd6a26
TF
102 {
103 .name = "VDCDC1",
31dd6a26
TF
104 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
105 .table = VDCDCx_VSEL_table,
106 },
107 {
108 .name = "VDCDC2",
31dd6a26
TF
109 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
110 .table = VDCDCx_VSEL_table,
111 },
112 {
113 .name = "VDCDC3",
31dd6a26
TF
114 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
115 .table = VDCDCx_VSEL_table,
116 },
117 {
118 .name = "LDO1",
31dd6a26
TF
119 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
120 .table = LDO1_VSEL_table,
121 },
122 {
123 .name = "LDO2",
31dd6a26
TF
124 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
125 .table = LDO2_VSEL_table,
126 },
127};
128
4ce5ba5b 129struct tps6507x_pmic {
3fa5b8e0 130 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
31dd6a26 131 struct tps6507x_dev *mfd;
3fa5b8e0 132 struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
7d14831e 133 struct tps_info *info[TPS6507X_NUM_REGULATOR];
3fa5b8e0
AA
134 struct mutex io_lock;
135};
4ce5ba5b 136static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
3fa5b8e0 137{
31dd6a26
TF
138 u8 val;
139 int err;
140
141 err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
142
143 if (err)
144 return err;
145
146 return val;
3fa5b8e0
AA
147}
148
4ce5ba5b 149static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
3fa5b8e0 150{
31dd6a26 151 return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
3fa5b8e0
AA
152}
153
4ce5ba5b 154static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
3fa5b8e0
AA
155{
156 int err, data;
157
158 mutex_lock(&tps->io_lock);
159
4ce5ba5b 160 data = tps6507x_pmic_read(tps, reg);
3fa5b8e0 161 if (data < 0) {
31dd6a26 162 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
3fa5b8e0
AA
163 err = data;
164 goto out;
165 }
166
167 data |= mask;
4ce5ba5b 168 err = tps6507x_pmic_write(tps, reg, data);
3fa5b8e0 169 if (err)
31dd6a26 170 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
3fa5b8e0
AA
171
172out:
173 mutex_unlock(&tps->io_lock);
174 return err;
175}
176
4ce5ba5b 177static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
3fa5b8e0
AA
178{
179 int err, data;
180
181 mutex_lock(&tps->io_lock);
182
4ce5ba5b 183 data = tps6507x_pmic_read(tps, reg);
3fa5b8e0 184 if (data < 0) {
31dd6a26 185 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
3fa5b8e0
AA
186 err = data;
187 goto out;
188 }
189
190 data &= ~mask;
4ce5ba5b 191 err = tps6507x_pmic_write(tps, reg, data);
3fa5b8e0 192 if (err)
31dd6a26 193 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
3fa5b8e0
AA
194
195out:
196 mutex_unlock(&tps->io_lock);
197 return err;
198}
199
4ce5ba5b 200static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
3fa5b8e0
AA
201{
202 int data;
203
204 mutex_lock(&tps->io_lock);
205
4ce5ba5b 206 data = tps6507x_pmic_read(tps, reg);
3fa5b8e0 207 if (data < 0)
31dd6a26 208 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
3fa5b8e0
AA
209
210 mutex_unlock(&tps->io_lock);
211 return data;
212}
213
4ce5ba5b 214static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
3fa5b8e0
AA
215{
216 int err;
217
218 mutex_lock(&tps->io_lock);
219
4ce5ba5b 220 err = tps6507x_pmic_write(tps, reg, val);
3fa5b8e0 221 if (err < 0)
31dd6a26 222 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
3fa5b8e0
AA
223
224 mutex_unlock(&tps->io_lock);
225 return err;
226}
227
f2933d33 228static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
3fa5b8e0 229{
4ce5ba5b 230 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
f2933d33 231 int data, rid = rdev_get_id(dev);
3fa5b8e0
AA
232 u8 shift;
233
f2933d33 234 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
3fa5b8e0
AA
235 return -EINVAL;
236
f2933d33 237 shift = TPS6507X_MAX_REG_ID - rid;
4ce5ba5b 238 data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
3fa5b8e0
AA
239
240 if (data < 0)
241 return data;
242 else
243 return (data & 1<<shift) ? 1 : 0;
244}
245
f2933d33 246static int tps6507x_pmic_enable(struct regulator_dev *dev)
3fa5b8e0 247{
4ce5ba5b 248 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
f2933d33 249 int rid = rdev_get_id(dev);
3fa5b8e0
AA
250 u8 shift;
251
f2933d33 252 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
3fa5b8e0
AA
253 return -EINVAL;
254
f2933d33 255 shift = TPS6507X_MAX_REG_ID - rid;
4ce5ba5b 256 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
3fa5b8e0
AA
257}
258
f2933d33 259static int tps6507x_pmic_disable(struct regulator_dev *dev)
3fa5b8e0 260{
4ce5ba5b 261 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
f2933d33 262 int rid = rdev_get_id(dev);
3fa5b8e0
AA
263 u8 shift;
264
f2933d33 265 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
3fa5b8e0
AA
266 return -EINVAL;
267
f2933d33 268 shift = TPS6507X_MAX_REG_ID - rid;
4ce5ba5b
TF
269 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
270 1 << shift);
3fa5b8e0
AA
271}
272
7c842a1d 273static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
3fa5b8e0 274{
4ce5ba5b 275 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
f2933d33
AL
276 int data, rid = rdev_get_id(dev);
277 u8 reg, mask;
3fa5b8e0 278
f2933d33 279 switch (rid) {
3fa5b8e0
AA
280 case TPS6507X_DCDC_1:
281 reg = TPS6507X_REG_DEFDCDC1;
f2933d33 282 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
3fa5b8e0
AA
283 break;
284 case TPS6507X_DCDC_2:
f2933d33 285 if (tps->info[rid]->defdcdc_default)
7d14831e
AA
286 reg = TPS6507X_REG_DEFDCDC2_HIGH;
287 else
288 reg = TPS6507X_REG_DEFDCDC2_LOW;
f2933d33 289 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
3fa5b8e0
AA
290 break;
291 case TPS6507X_DCDC_3:
f2933d33 292 if (tps->info[rid]->defdcdc_default)
7d14831e
AA
293 reg = TPS6507X_REG_DEFDCDC3_HIGH;
294 else
295 reg = TPS6507X_REG_DEFDCDC3_LOW;
f2933d33
AL
296 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
297 break;
298 case TPS6507X_LDO_1:
299 reg = TPS6507X_REG_LDO_CTRL1;
300 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
301 break;
302 case TPS6507X_LDO_2:
303 reg = TPS6507X_REG_DEFLDO2;
304 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
3fa5b8e0
AA
305 break;
306 default:
307 return -EINVAL;
308 }
309
4ce5ba5b 310 data = tps6507x_pmic_reg_read(tps, reg);
3fa5b8e0
AA
311 if (data < 0)
312 return data;
313
f2933d33 314 data &= mask;
7c842a1d 315 return data;
3fa5b8e0
AA
316}
317
ca61a7bf
AL
318static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
319 unsigned selector)
3fa5b8e0 320{
4ce5ba5b 321 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
ca61a7bf 322 int data, rid = rdev_get_id(dev);
f2933d33 323 u8 reg, mask;
3fa5b8e0 324
f2933d33 325 switch (rid) {
3fa5b8e0
AA
326 case TPS6507X_DCDC_1:
327 reg = TPS6507X_REG_DEFDCDC1;
f2933d33 328 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
3fa5b8e0
AA
329 break;
330 case TPS6507X_DCDC_2:
f2933d33 331 if (tps->info[rid]->defdcdc_default)
7d14831e
AA
332 reg = TPS6507X_REG_DEFDCDC2_HIGH;
333 else
334 reg = TPS6507X_REG_DEFDCDC2_LOW;
f2933d33 335 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
3fa5b8e0
AA
336 break;
337 case TPS6507X_DCDC_3:
f2933d33 338 if (tps->info[rid]->defdcdc_default)
7d14831e
AA
339 reg = TPS6507X_REG_DEFDCDC3_HIGH;
340 else
341 reg = TPS6507X_REG_DEFDCDC3_LOW;
f2933d33
AL
342 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
343 break;
344 case TPS6507X_LDO_1:
345 reg = TPS6507X_REG_LDO_CTRL1;
346 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
347 break;
348 case TPS6507X_LDO_2:
349 reg = TPS6507X_REG_DEFLDO2;
350 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
3fa5b8e0
AA
351 break;
352 default:
353 return -EINVAL;
354 }
355
4ce5ba5b 356 data = tps6507x_pmic_reg_read(tps, reg);
3fa5b8e0
AA
357 if (data < 0)
358 return data;
359
360 data &= ~mask;
ca61a7bf 361 data |= selector;
3fa5b8e0 362
4ce5ba5b 363 return tps6507x_pmic_reg_write(tps, reg, data);
3fa5b8e0
AA
364}
365
f2933d33
AL
366static struct regulator_ops tps6507x_pmic_ops = {
367 .is_enabled = tps6507x_pmic_is_enabled,
368 .enable = tps6507x_pmic_enable,
369 .disable = tps6507x_pmic_disable,
7c842a1d 370 .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
ca61a7bf 371 .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
055917ac 372 .list_voltage = regulator_list_voltage_table,
3fa5b8e0
AA
373};
374
f2933d33 375static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
3fa5b8e0 376{
31dd6a26 377 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
7d14831e 378 struct tps_info *info = &tps6507x_pmic_regs[0];
c172708d 379 struct regulator_config config = { };
3fa5b8e0
AA
380 struct regulator_init_data *init_data;
381 struct regulator_dev *rdev;
4ce5ba5b 382 struct tps6507x_pmic *tps;
0bc20bba 383 struct tps6507x_board *tps_board;
3fa5b8e0 384 int i;
56c23492 385 int error;
3fa5b8e0 386
0bc20bba
TF
387 /**
388 * tps_board points to pmic related constants
389 * coming from the board-evm file.
390 */
391
31dd6a26 392 tps_board = dev_get_platdata(tps6507x_dev->dev);
0bc20bba
TF
393 if (!tps_board)
394 return -EINVAL;
395
3fa5b8e0
AA
396 /**
397 * init_data points to array of regulator_init structures
398 * coming from the board-evm file.
399 */
0bc20bba 400 init_data = tps_board->tps6507x_pmic_init_data;
3fa5b8e0 401 if (!init_data)
0bc20bba 402 return -EINVAL;
3fa5b8e0 403
9eb0c421 404 tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
3fa5b8e0
AA
405 if (!tps)
406 return -ENOMEM;
407
408 mutex_init(&tps->io_lock);
409
410 /* common for all regulators */
31dd6a26 411 tps->mfd = tps6507x_dev;
3fa5b8e0
AA
412
413 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
414 /* Register the regulators */
415 tps->info[i] = info;
7d14831e
AA
416 if (init_data->driver_data) {
417 struct tps6507x_reg_platform_data *data =
418 init_data->driver_data;
419 tps->info[i]->defdcdc_default = data->defdcdc_default;
420 }
421
3fa5b8e0 422 tps->desc[i].name = info->name;
77fa44d0 423 tps->desc[i].id = i;
0fcdb109 424 tps->desc[i].n_voltages = info->table_len;
055917ac 425 tps->desc[i].volt_table = info->table;
f2933d33 426 tps->desc[i].ops = &tps6507x_pmic_ops;
3fa5b8e0
AA
427 tps->desc[i].type = REGULATOR_VOLTAGE;
428 tps->desc[i].owner = THIS_MODULE;
429
c172708d
MB
430 config.dev = tps6507x_dev->dev;
431 config.init_data = init_data;
432 config.driver_data = tps;
433
434 rdev = regulator_register(&tps->desc[i], &config);
3fa5b8e0 435 if (IS_ERR(rdev)) {
31dd6a26
TF
436 dev_err(tps6507x_dev->dev,
437 "failed to register %s regulator\n",
438 pdev->name);
56c23492
DT
439 error = PTR_ERR(rdev);
440 goto fail;
3fa5b8e0
AA
441 }
442
443 /* Save regulator for cleanup */
444 tps->rdev[i] = rdev;
445 }
446
31dd6a26 447 tps6507x_dev->pmic = tps;
d7399fa8 448 platform_set_drvdata(pdev, tps6507x_dev);
3fa5b8e0
AA
449
450 return 0;
56c23492
DT
451
452fail:
453 while (--i >= 0)
454 regulator_unregister(tps->rdev[i]);
56c23492 455 return error;
3fa5b8e0
AA
456}
457
31dd6a26 458static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
3fa5b8e0 459{
31dd6a26
TF
460 struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
461 struct tps6507x_pmic *tps = tps6507x_dev->pmic;
3fa5b8e0
AA
462 int i;
463
464 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
465 regulator_unregister(tps->rdev[i]);
3fa5b8e0
AA
466 return 0;
467}
468
31dd6a26 469static struct platform_driver tps6507x_pmic_driver = {
3fa5b8e0 470 .driver = {
31dd6a26 471 .name = "tps6507x-pmic",
3fa5b8e0
AA
472 .owner = THIS_MODULE,
473 },
4ce5ba5b
TF
474 .probe = tps6507x_pmic_probe,
475 .remove = __devexit_p(tps6507x_pmic_remove),
3fa5b8e0
AA
476};
477
4ce5ba5b 478static int __init tps6507x_pmic_init(void)
3fa5b8e0 479{
31dd6a26 480 return platform_driver_register(&tps6507x_pmic_driver);
3fa5b8e0 481}
4ce5ba5b 482subsys_initcall(tps6507x_pmic_init);
3fa5b8e0 483
4ce5ba5b 484static void __exit tps6507x_pmic_cleanup(void)
3fa5b8e0 485{
31dd6a26 486 platform_driver_unregister(&tps6507x_pmic_driver);
3fa5b8e0 487}
4ce5ba5b 488module_exit(tps6507x_pmic_cleanup);
3fa5b8e0
AA
489
490MODULE_AUTHOR("Texas Instruments");
491MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
9e108d33 492MODULE_LICENSE("GPL v2");
31dd6a26 493MODULE_ALIAS("platform:tps6507x-pmic");