staging/sb105x: PARPORT config is not good enough must use PARPORT_PC
[linux-2.6.git] / drivers / regulator / tps6507x-regulator.c
blob0233cfb5656058a57ccd0dce40591701c16ce8b6
1 /*
2 * tps6507x-regulator.c
4 * Regulator driver for TPS65073 PMIC
6 * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
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.
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.
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>
25 #include <linux/regulator/tps6507x.h>
26 #include <linux/slab.h>
27 #include <linux/mfd/tps6507x.h>
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
37 #define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
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)
46 /* Supported voltage values for regulators (in microVolts) */
47 static 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,
66 static 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,
73 /* The voltage mapping table for LDO2 is the same as VDCDCx */
74 #define LDO2_VSEL_table VDCDCx_VSEL_table
76 struct tps_info {
77 const char *name;
78 u8 table_len;
79 const unsigned int *table;
81 /* Does DCDC high or the low register defines output voltage? */
82 bool defdcdc_default;
85 static struct tps_info tps6507x_pmic_regs[] = {
87 .name = "VDCDC1",
88 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
89 .table = VDCDCx_VSEL_table,
92 .name = "VDCDC2",
93 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
94 .table = VDCDCx_VSEL_table,
97 .name = "VDCDC3",
98 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
99 .table = VDCDCx_VSEL_table,
102 .name = "LDO1",
103 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
104 .table = LDO1_VSEL_table,
107 .name = "LDO2",
108 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
109 .table = LDO2_VSEL_table,
113 struct tps6507x_pmic {
114 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
115 struct tps6507x_dev *mfd;
116 struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
117 struct tps_info *info[TPS6507X_NUM_REGULATOR];
118 struct mutex io_lock;
120 static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
122 u8 val;
123 int err;
125 err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
127 if (err)
128 return err;
130 return val;
133 static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
135 return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
138 static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
140 int err, data;
142 mutex_lock(&tps->io_lock);
144 data = tps6507x_pmic_read(tps, reg);
145 if (data < 0) {
146 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
147 err = data;
148 goto out;
151 data |= mask;
152 err = tps6507x_pmic_write(tps, reg, data);
153 if (err)
154 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
156 out:
157 mutex_unlock(&tps->io_lock);
158 return err;
161 static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
163 int err, data;
165 mutex_lock(&tps->io_lock);
167 data = tps6507x_pmic_read(tps, reg);
168 if (data < 0) {
169 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
170 err = data;
171 goto out;
174 data &= ~mask;
175 err = tps6507x_pmic_write(tps, reg, data);
176 if (err)
177 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
179 out:
180 mutex_unlock(&tps->io_lock);
181 return err;
184 static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
186 int data;
188 mutex_lock(&tps->io_lock);
190 data = tps6507x_pmic_read(tps, reg);
191 if (data < 0)
192 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
194 mutex_unlock(&tps->io_lock);
195 return data;
198 static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
200 int err;
202 mutex_lock(&tps->io_lock);
204 err = tps6507x_pmic_write(tps, reg, val);
205 if (err < 0)
206 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
208 mutex_unlock(&tps->io_lock);
209 return err;
212 static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
214 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
215 int data, rid = rdev_get_id(dev);
216 u8 shift;
218 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
219 return -EINVAL;
221 shift = TPS6507X_MAX_REG_ID - rid;
222 data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
224 if (data < 0)
225 return data;
226 else
227 return (data & 1<<shift) ? 1 : 0;
230 static int tps6507x_pmic_enable(struct regulator_dev *dev)
232 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
233 int rid = rdev_get_id(dev);
234 u8 shift;
236 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
237 return -EINVAL;
239 shift = TPS6507X_MAX_REG_ID - rid;
240 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
243 static int tps6507x_pmic_disable(struct regulator_dev *dev)
245 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
246 int rid = rdev_get_id(dev);
247 u8 shift;
249 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
250 return -EINVAL;
252 shift = TPS6507X_MAX_REG_ID - rid;
253 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
254 1 << shift);
257 static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
259 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
260 int data, rid = rdev_get_id(dev);
261 u8 reg, mask;
263 switch (rid) {
264 case TPS6507X_DCDC_1:
265 reg = TPS6507X_REG_DEFDCDC1;
266 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
267 break;
268 case TPS6507X_DCDC_2:
269 if (tps->info[rid]->defdcdc_default)
270 reg = TPS6507X_REG_DEFDCDC2_HIGH;
271 else
272 reg = TPS6507X_REG_DEFDCDC2_LOW;
273 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
274 break;
275 case TPS6507X_DCDC_3:
276 if (tps->info[rid]->defdcdc_default)
277 reg = TPS6507X_REG_DEFDCDC3_HIGH;
278 else
279 reg = TPS6507X_REG_DEFDCDC3_LOW;
280 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
281 break;
282 case TPS6507X_LDO_1:
283 reg = TPS6507X_REG_LDO_CTRL1;
284 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
285 break;
286 case TPS6507X_LDO_2:
287 reg = TPS6507X_REG_DEFLDO2;
288 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
289 break;
290 default:
291 return -EINVAL;
294 data = tps6507x_pmic_reg_read(tps, reg);
295 if (data < 0)
296 return data;
298 data &= mask;
299 return data;
302 static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
303 unsigned selector)
305 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
306 int data, rid = rdev_get_id(dev);
307 u8 reg, mask;
309 switch (rid) {
310 case TPS6507X_DCDC_1:
311 reg = TPS6507X_REG_DEFDCDC1;
312 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
313 break;
314 case TPS6507X_DCDC_2:
315 if (tps->info[rid]->defdcdc_default)
316 reg = TPS6507X_REG_DEFDCDC2_HIGH;
317 else
318 reg = TPS6507X_REG_DEFDCDC2_LOW;
319 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
320 break;
321 case TPS6507X_DCDC_3:
322 if (tps->info[rid]->defdcdc_default)
323 reg = TPS6507X_REG_DEFDCDC3_HIGH;
324 else
325 reg = TPS6507X_REG_DEFDCDC3_LOW;
326 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
327 break;
328 case TPS6507X_LDO_1:
329 reg = TPS6507X_REG_LDO_CTRL1;
330 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
331 break;
332 case TPS6507X_LDO_2:
333 reg = TPS6507X_REG_DEFLDO2;
334 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
335 break;
336 default:
337 return -EINVAL;
340 data = tps6507x_pmic_reg_read(tps, reg);
341 if (data < 0)
342 return data;
344 data &= ~mask;
345 data |= selector;
347 return tps6507x_pmic_reg_write(tps, reg, data);
350 static struct regulator_ops tps6507x_pmic_ops = {
351 .is_enabled = tps6507x_pmic_is_enabled,
352 .enable = tps6507x_pmic_enable,
353 .disable = tps6507x_pmic_disable,
354 .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
355 .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
356 .list_voltage = regulator_list_voltage_table,
359 static int tps6507x_pmic_probe(struct platform_device *pdev)
361 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
362 struct tps_info *info = &tps6507x_pmic_regs[0];
363 struct regulator_config config = { };
364 struct regulator_init_data *init_data;
365 struct regulator_dev *rdev;
366 struct tps6507x_pmic *tps;
367 struct tps6507x_board *tps_board;
368 int i;
369 int error;
372 * tps_board points to pmic related constants
373 * coming from the board-evm file.
376 tps_board = dev_get_platdata(tps6507x_dev->dev);
377 if (!tps_board)
378 return -EINVAL;
381 * init_data points to array of regulator_init structures
382 * coming from the board-evm file.
384 init_data = tps_board->tps6507x_pmic_init_data;
385 if (!init_data)
386 return -EINVAL;
388 tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
389 if (!tps)
390 return -ENOMEM;
392 mutex_init(&tps->io_lock);
394 /* common for all regulators */
395 tps->mfd = tps6507x_dev;
397 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
398 /* Register the regulators */
399 tps->info[i] = info;
400 if (init_data->driver_data) {
401 struct tps6507x_reg_platform_data *data =
402 init_data->driver_data;
403 tps->info[i]->defdcdc_default = data->defdcdc_default;
406 tps->desc[i].name = info->name;
407 tps->desc[i].id = i;
408 tps->desc[i].n_voltages = info->table_len;
409 tps->desc[i].volt_table = info->table;
410 tps->desc[i].ops = &tps6507x_pmic_ops;
411 tps->desc[i].type = REGULATOR_VOLTAGE;
412 tps->desc[i].owner = THIS_MODULE;
414 config.dev = tps6507x_dev->dev;
415 config.init_data = init_data;
416 config.driver_data = tps;
418 rdev = regulator_register(&tps->desc[i], &config);
419 if (IS_ERR(rdev)) {
420 dev_err(tps6507x_dev->dev,
421 "failed to register %s regulator\n",
422 pdev->name);
423 error = PTR_ERR(rdev);
424 goto fail;
427 /* Save regulator for cleanup */
428 tps->rdev[i] = rdev;
431 tps6507x_dev->pmic = tps;
432 platform_set_drvdata(pdev, tps6507x_dev);
434 return 0;
436 fail:
437 while (--i >= 0)
438 regulator_unregister(tps->rdev[i]);
439 return error;
442 static int tps6507x_pmic_remove(struct platform_device *pdev)
444 struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
445 struct tps6507x_pmic *tps = tps6507x_dev->pmic;
446 int i;
448 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
449 regulator_unregister(tps->rdev[i]);
450 return 0;
453 static struct platform_driver tps6507x_pmic_driver = {
454 .driver = {
455 .name = "tps6507x-pmic",
456 .owner = THIS_MODULE,
458 .probe = tps6507x_pmic_probe,
459 .remove = tps6507x_pmic_remove,
462 static int __init tps6507x_pmic_init(void)
464 return platform_driver_register(&tps6507x_pmic_driver);
466 subsys_initcall(tps6507x_pmic_init);
468 static void __exit tps6507x_pmic_cleanup(void)
470 platform_driver_unregister(&tps6507x_pmic_driver);
472 module_exit(tps6507x_pmic_cleanup);
474 MODULE_AUTHOR("Texas Instruments");
475 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
476 MODULE_LICENSE("GPL v2");
477 MODULE_ALIAS("platform:tps6507x-pmic");