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 milliVolts) */
47 static const u16 VDCDCx_VSEL_table
[] = {
51 1025, 1050, 1075, 1100,
52 1125, 1150, 1175, 1200,
53 1225, 1250, 1275, 1300,
54 1325, 1350, 1375, 1400,
55 1425, 1450, 1475, 1500,
56 1550, 1600, 1650, 1700,
57 1750, 1800, 1850, 1900,
58 1950, 2000, 2050, 2100,
59 2150, 2200, 2250, 2300,
60 2350, 2400, 2450, 2500,
61 2550, 2600, 2650, 2700,
62 2750, 2800, 2850, 2900,
63 3000, 3100, 3200, 3300,
66 static const u16 LDO1_VSEL_table
[] = {
67 1000, 1100, 1200, 1250,
68 1300, 1350, 1400, 1500,
69 1600, 1800, 2500, 2750,
70 2800, 3000, 3100, 3300,
73 static const u16 LDO2_VSEL_table
[] = {
77 1025, 1050, 1075, 1100,
78 1125, 1150, 1175, 1200,
79 1225, 1250, 1275, 1300,
80 1325, 1350, 1375, 1400,
81 1425, 1450, 1475, 1500,
82 1550, 1600, 1650, 1700,
83 1750, 1800, 1850, 1900,
84 1950, 2000, 2050, 2100,
85 2150, 2200, 2250, 2300,
86 2350, 2400, 2450, 2500,
87 2550, 2600, 2650, 2700,
88 2750, 2800, 2850, 2900,
89 3000, 3100, 3200, 3300,
99 /* Does DCDC high or the low register defines output voltage? */
100 bool defdcdc_default
;
103 static struct tps_info tps6507x_pmic_regs
[] = {
108 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
109 .table
= VDCDCx_VSEL_table
,
115 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
116 .table
= VDCDCx_VSEL_table
,
122 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
123 .table
= VDCDCx_VSEL_table
,
129 .table_len
= ARRAY_SIZE(LDO1_VSEL_table
),
130 .table
= LDO1_VSEL_table
,
136 .table_len
= ARRAY_SIZE(LDO2_VSEL_table
),
137 .table
= LDO2_VSEL_table
,
141 struct tps6507x_pmic
{
142 struct regulator_desc desc
[TPS6507X_NUM_REGULATOR
];
143 struct tps6507x_dev
*mfd
;
144 struct regulator_dev
*rdev
[TPS6507X_NUM_REGULATOR
];
145 struct tps_info
*info
[TPS6507X_NUM_REGULATOR
];
146 struct mutex io_lock
;
148 static inline int tps6507x_pmic_read(struct tps6507x_pmic
*tps
, u8 reg
)
153 err
= tps
->mfd
->read_dev(tps
->mfd
, reg
, 1, &val
);
161 static inline int tps6507x_pmic_write(struct tps6507x_pmic
*tps
, u8 reg
, u8 val
)
163 return tps
->mfd
->write_dev(tps
->mfd
, reg
, 1, &val
);
166 static int tps6507x_pmic_set_bits(struct tps6507x_pmic
*tps
, u8 reg
, u8 mask
)
170 mutex_lock(&tps
->io_lock
);
172 data
= tps6507x_pmic_read(tps
, reg
);
174 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
180 err
= tps6507x_pmic_write(tps
, reg
, data
);
182 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
185 mutex_unlock(&tps
->io_lock
);
189 static int tps6507x_pmic_clear_bits(struct tps6507x_pmic
*tps
, u8 reg
, u8 mask
)
193 mutex_lock(&tps
->io_lock
);
195 data
= tps6507x_pmic_read(tps
, reg
);
197 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
203 err
= tps6507x_pmic_write(tps
, reg
, data
);
205 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
208 mutex_unlock(&tps
->io_lock
);
212 static int tps6507x_pmic_reg_read(struct tps6507x_pmic
*tps
, u8 reg
)
216 mutex_lock(&tps
->io_lock
);
218 data
= tps6507x_pmic_read(tps
, reg
);
220 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
222 mutex_unlock(&tps
->io_lock
);
226 static int tps6507x_pmic_reg_write(struct tps6507x_pmic
*tps
, u8 reg
, u8 val
)
230 mutex_lock(&tps
->io_lock
);
232 err
= tps6507x_pmic_write(tps
, reg
, val
);
234 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
236 mutex_unlock(&tps
->io_lock
);
240 static int tps6507x_pmic_is_enabled(struct regulator_dev
*dev
)
242 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
243 int data
, rid
= rdev_get_id(dev
);
246 if (rid
< TPS6507X_DCDC_1
|| rid
> TPS6507X_LDO_2
)
249 shift
= TPS6507X_MAX_REG_ID
- rid
;
250 data
= tps6507x_pmic_reg_read(tps
, TPS6507X_REG_CON_CTRL1
);
255 return (data
& 1<<shift
) ? 1 : 0;
258 static int tps6507x_pmic_enable(struct regulator_dev
*dev
)
260 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
261 int rid
= rdev_get_id(dev
);
264 if (rid
< TPS6507X_DCDC_1
|| rid
> TPS6507X_LDO_2
)
267 shift
= TPS6507X_MAX_REG_ID
- rid
;
268 return tps6507x_pmic_set_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
271 static int tps6507x_pmic_disable(struct regulator_dev
*dev
)
273 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
274 int rid
= rdev_get_id(dev
);
277 if (rid
< TPS6507X_DCDC_1
|| rid
> TPS6507X_LDO_2
)
280 shift
= TPS6507X_MAX_REG_ID
- rid
;
281 return tps6507x_pmic_clear_bits(tps
, TPS6507X_REG_CON_CTRL1
,
285 static int tps6507x_pmic_get_voltage_sel(struct regulator_dev
*dev
)
287 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
288 int data
, rid
= rdev_get_id(dev
);
292 case TPS6507X_DCDC_1
:
293 reg
= TPS6507X_REG_DEFDCDC1
;
294 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
296 case TPS6507X_DCDC_2
:
297 if (tps
->info
[rid
]->defdcdc_default
)
298 reg
= TPS6507X_REG_DEFDCDC2_HIGH
;
300 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
301 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
303 case TPS6507X_DCDC_3
:
304 if (tps
->info
[rid
]->defdcdc_default
)
305 reg
= TPS6507X_REG_DEFDCDC3_HIGH
;
307 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
308 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
311 reg
= TPS6507X_REG_LDO_CTRL1
;
312 mask
= TPS6507X_REG_LDO_CTRL1_LDO1_MASK
;
315 reg
= TPS6507X_REG_DEFLDO2
;
316 mask
= TPS6507X_REG_DEFLDO2_LDO2_MASK
;
322 data
= tps6507x_pmic_reg_read(tps
, reg
);
330 static int tps6507x_pmic_set_voltage_sel(struct regulator_dev
*dev
,
333 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
334 int data
, rid
= rdev_get_id(dev
);
338 case TPS6507X_DCDC_1
:
339 reg
= TPS6507X_REG_DEFDCDC1
;
340 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
342 case TPS6507X_DCDC_2
:
343 if (tps
->info
[rid
]->defdcdc_default
)
344 reg
= TPS6507X_REG_DEFDCDC2_HIGH
;
346 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
347 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
349 case TPS6507X_DCDC_3
:
350 if (tps
->info
[rid
]->defdcdc_default
)
351 reg
= TPS6507X_REG_DEFDCDC3_HIGH
;
353 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
354 mask
= TPS6507X_DEFDCDCX_DCDC_MASK
;
357 reg
= TPS6507X_REG_LDO_CTRL1
;
358 mask
= TPS6507X_REG_LDO_CTRL1_LDO1_MASK
;
361 reg
= TPS6507X_REG_DEFLDO2
;
362 mask
= TPS6507X_REG_DEFLDO2_LDO2_MASK
;
368 data
= tps6507x_pmic_reg_read(tps
, reg
);
375 return tps6507x_pmic_reg_write(tps
, reg
, data
);
378 static int tps6507x_pmic_list_voltage(struct regulator_dev
*dev
,
381 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
382 int rid
= rdev_get_id(dev
);
384 if (rid
< TPS6507X_DCDC_1
|| rid
> TPS6507X_LDO_2
)
387 if (selector
>= tps
->info
[rid
]->table_len
)
390 return tps
->info
[rid
]->table
[selector
] * 1000;
393 static struct regulator_ops tps6507x_pmic_ops
= {
394 .is_enabled
= tps6507x_pmic_is_enabled
,
395 .enable
= tps6507x_pmic_enable
,
396 .disable
= tps6507x_pmic_disable
,
397 .get_voltage_sel
= tps6507x_pmic_get_voltage_sel
,
398 .set_voltage_sel
= tps6507x_pmic_set_voltage_sel
,
399 .list_voltage
= tps6507x_pmic_list_voltage
,
402 static __devinit
int tps6507x_pmic_probe(struct platform_device
*pdev
)
404 struct tps6507x_dev
*tps6507x_dev
= dev_get_drvdata(pdev
->dev
.parent
);
405 struct tps_info
*info
= &tps6507x_pmic_regs
[0];
406 struct regulator_config config
= { };
407 struct regulator_init_data
*init_data
;
408 struct regulator_dev
*rdev
;
409 struct tps6507x_pmic
*tps
;
410 struct tps6507x_board
*tps_board
;
415 * tps_board points to pmic related constants
416 * coming from the board-evm file.
419 tps_board
= dev_get_platdata(tps6507x_dev
->dev
);
424 * init_data points to array of regulator_init structures
425 * coming from the board-evm file.
427 init_data
= tps_board
->tps6507x_pmic_init_data
;
431 tps
= devm_kzalloc(&pdev
->dev
, sizeof(*tps
), GFP_KERNEL
);
435 mutex_init(&tps
->io_lock
);
437 /* common for all regulators */
438 tps
->mfd
= tps6507x_dev
;
440 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++, info
++, init_data
++) {
441 /* Register the regulators */
443 if (init_data
->driver_data
) {
444 struct tps6507x_reg_platform_data
*data
=
445 init_data
->driver_data
;
446 tps
->info
[i
]->defdcdc_default
= data
->defdcdc_default
;
449 tps
->desc
[i
].name
= info
->name
;
451 tps
->desc
[i
].n_voltages
= info
->table_len
;
452 tps
->desc
[i
].ops
= &tps6507x_pmic_ops
;
453 tps
->desc
[i
].type
= REGULATOR_VOLTAGE
;
454 tps
->desc
[i
].owner
= THIS_MODULE
;
456 config
.dev
= tps6507x_dev
->dev
;
457 config
.init_data
= init_data
;
458 config
.driver_data
= tps
;
460 rdev
= regulator_register(&tps
->desc
[i
], &config
);
462 dev_err(tps6507x_dev
->dev
,
463 "failed to register %s regulator\n",
465 error
= PTR_ERR(rdev
);
469 /* Save regulator for cleanup */
473 tps6507x_dev
->pmic
= tps
;
474 platform_set_drvdata(pdev
, tps6507x_dev
);
480 regulator_unregister(tps
->rdev
[i
]);
484 static int __devexit
tps6507x_pmic_remove(struct platform_device
*pdev
)
486 struct tps6507x_dev
*tps6507x_dev
= platform_get_drvdata(pdev
);
487 struct tps6507x_pmic
*tps
= tps6507x_dev
->pmic
;
490 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++)
491 regulator_unregister(tps
->rdev
[i
]);
495 static struct platform_driver tps6507x_pmic_driver
= {
497 .name
= "tps6507x-pmic",
498 .owner
= THIS_MODULE
,
500 .probe
= tps6507x_pmic_probe
,
501 .remove
= __devexit_p(tps6507x_pmic_remove
),
504 static int __init
tps6507x_pmic_init(void)
506 return platform_driver_register(&tps6507x_pmic_driver
);
508 subsys_initcall(tps6507x_pmic_init
);
510 static void __exit
tps6507x_pmic_cleanup(void)
512 platform_driver_unregister(&tps6507x_pmic_driver
);
514 module_exit(tps6507x_pmic_cleanup
);
516 MODULE_AUTHOR("Texas Instruments");
517 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
518 MODULE_LICENSE("GPL v2");
519 MODULE_ALIAS("platform:tps6507x-pmic");