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>
30 #define TPS6507X_DCDC_1 0
31 #define TPS6507X_DCDC_2 1
32 #define TPS6507X_DCDC_3 2
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
79 const unsigned int *table
;
81 /* Does DCDC high or the low register defines output voltage? */
85 static struct tps_info tps6507x_pmic_regs
[] = {
88 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
89 .table
= VDCDCx_VSEL_table
,
93 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
94 .table
= VDCDCx_VSEL_table
,
98 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
99 .table
= VDCDCx_VSEL_table
,
103 .table_len
= ARRAY_SIZE(LDO1_VSEL_table
),
104 .table
= LDO1_VSEL_table
,
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
)
125 err
= tps
->mfd
->read_dev(tps
->mfd
, reg
, 1, &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
)
142 mutex_lock(&tps
->io_lock
);
144 data
= tps6507x_pmic_read(tps
, reg
);
146 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
152 err
= tps6507x_pmic_write(tps
, reg
, data
);
154 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
157 mutex_unlock(&tps
->io_lock
);
161 static int tps6507x_pmic_clear_bits(struct tps6507x_pmic
*tps
, u8 reg
, u8 mask
)
165 mutex_lock(&tps
->io_lock
);
167 data
= tps6507x_pmic_read(tps
, reg
);
169 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
175 err
= tps6507x_pmic_write(tps
, reg
, data
);
177 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
180 mutex_unlock(&tps
->io_lock
);
184 static int tps6507x_pmic_reg_read(struct tps6507x_pmic
*tps
, u8 reg
)
188 mutex_lock(&tps
->io_lock
);
190 data
= tps6507x_pmic_read(tps
, reg
);
192 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
194 mutex_unlock(&tps
->io_lock
);
198 static int tps6507x_pmic_reg_write(struct tps6507x_pmic
*tps
, u8 reg
, u8 val
)
202 mutex_lock(&tps
->io_lock
);
204 err
= tps6507x_pmic_write(tps
, reg
, val
);
206 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
208 mutex_unlock(&tps
->io_lock
);
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
);
218 if (rid
< TPS6507X_DCDC_1
|| rid
> TPS6507X_LDO_2
)
221 shift
= TPS6507X_MAX_REG_ID
- rid
;
222 data
= tps6507x_pmic_reg_read(tps
, TPS6507X_REG_CON_CTRL1
);
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
);
236 if (rid
< TPS6507X_DCDC_1
|| rid
> TPS6507X_LDO_2
)
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
);
249 if (rid
< TPS6507X_DCDC_1
|| rid
> TPS6507X_LDO_2
)
252 shift
= TPS6507X_MAX_REG_ID
- rid
;
253 return tps6507x_pmic_clear_bits(tps
, TPS6507X_REG_CON_CTRL1
,
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
);
264 case TPS6507X_DCDC_1
:
265 reg
= TPS6507X_REG_DEFDCDC1
;
266 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
268 case TPS6507X_DCDC_2
:
269 if (tps
->info
[rid
]->defdcdc_default
)
270 reg
= TPS6507X_REG_DEFDCDC2_HIGH
;
272 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
273 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
275 case TPS6507X_DCDC_3
:
276 if (tps
->info
[rid
]->defdcdc_default
)
277 reg
= TPS6507X_REG_DEFDCDC3_HIGH
;
279 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
280 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
283 reg
= TPS6507X_REG_LDO_CTRL1
;
284 mask
= TPS6507X_REG_LDO_CTRL1_LDO1_MASK
;
287 reg
= TPS6507X_REG_DEFLDO2
;
288 mask
= TPS6507X_REG_DEFLDO2_LDO2_MASK
;
294 data
= tps6507x_pmic_reg_read(tps
, reg
);
302 static int tps6507x_pmic_set_voltage_sel(struct regulator_dev
*dev
,
305 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
306 int data
, rid
= rdev_get_id(dev
);
310 case TPS6507X_DCDC_1
:
311 reg
= TPS6507X_REG_DEFDCDC1
;
312 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
314 case TPS6507X_DCDC_2
:
315 if (tps
->info
[rid
]->defdcdc_default
)
316 reg
= TPS6507X_REG_DEFDCDC2_HIGH
;
318 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
319 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
321 case TPS6507X_DCDC_3
:
322 if (tps
->info
[rid
]->defdcdc_default
)
323 reg
= TPS6507X_REG_DEFDCDC3_HIGH
;
325 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
326 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
329 reg
= TPS6507X_REG_LDO_CTRL1
;
330 mask
= TPS6507X_REG_LDO_CTRL1_LDO1_MASK
;
333 reg
= TPS6507X_REG_DEFLDO2
;
334 mask
= TPS6507X_REG_DEFLDO2_LDO2_MASK
;
340 data
= tps6507x_pmic_reg_read(tps
, reg
);
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
;
372 * tps_board points to pmic related constants
373 * coming from the board-evm file.
376 tps_board
= dev_get_platdata(tps6507x_dev
->dev
);
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
;
388 tps
= devm_kzalloc(&pdev
->dev
, sizeof(*tps
), GFP_KERNEL
);
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 */
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
;
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
);
420 dev_err(tps6507x_dev
->dev
,
421 "failed to register %s regulator\n",
423 error
= PTR_ERR(rdev
);
427 /* Save regulator for cleanup */
431 tps6507x_dev
->pmic
= tps
;
432 platform_set_drvdata(pdev
, tps6507x_dev
);
438 regulator_unregister(tps
->rdev
[i
]);
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
;
448 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++)
449 regulator_unregister(tps
->rdev
[i
]);
453 static struct platform_driver tps6507x_pmic_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");