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/delay.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,
92 static unsigned int num_voltages
[] = {ARRAY_SIZE(VDCDCx_VSEL_table
),
93 ARRAY_SIZE(VDCDCx_VSEL_table
),
94 ARRAY_SIZE(VDCDCx_VSEL_table
),
95 ARRAY_SIZE(LDO1_VSEL_table
),
96 ARRAY_SIZE(LDO2_VSEL_table
)};
106 static const struct tps_info tps6507x_pmic_regs
[] = {
111 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
112 .table
= VDCDCx_VSEL_table
,
118 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
119 .table
= VDCDCx_VSEL_table
,
125 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
126 .table
= VDCDCx_VSEL_table
,
132 .table_len
= ARRAY_SIZE(LDO1_VSEL_table
),
133 .table
= LDO1_VSEL_table
,
139 .table_len
= ARRAY_SIZE(LDO2_VSEL_table
),
140 .table
= LDO2_VSEL_table
,
144 struct tps6507x_pmic
{
145 struct regulator_desc desc
[TPS6507X_NUM_REGULATOR
];
146 struct tps6507x_dev
*mfd
;
147 struct regulator_dev
*rdev
[TPS6507X_NUM_REGULATOR
];
148 const struct tps_info
*info
[TPS6507X_NUM_REGULATOR
];
149 struct mutex io_lock
;
151 static inline int tps6507x_pmic_read(struct tps6507x_pmic
*tps
, u8 reg
)
156 err
= tps
->mfd
->read_dev(tps
->mfd
, reg
, 1, &val
);
164 static inline int tps6507x_pmic_write(struct tps6507x_pmic
*tps
, u8 reg
, u8 val
)
166 return tps
->mfd
->write_dev(tps
->mfd
, reg
, 1, &val
);
169 static int tps6507x_pmic_set_bits(struct tps6507x_pmic
*tps
, u8 reg
, u8 mask
)
173 mutex_lock(&tps
->io_lock
);
175 data
= tps6507x_pmic_read(tps
, reg
);
177 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
183 err
= tps6507x_pmic_write(tps
, reg
, data
);
185 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
188 mutex_unlock(&tps
->io_lock
);
192 static int tps6507x_pmic_clear_bits(struct tps6507x_pmic
*tps
, u8 reg
, u8 mask
)
196 mutex_lock(&tps
->io_lock
);
198 data
= tps6507x_pmic_read(tps
, reg
);
200 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
206 err
= tps6507x_pmic_write(tps
, reg
, data
);
208 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
211 mutex_unlock(&tps
->io_lock
);
215 static int tps6507x_pmic_reg_read(struct tps6507x_pmic
*tps
, u8 reg
)
219 mutex_lock(&tps
->io_lock
);
221 data
= tps6507x_pmic_read(tps
, reg
);
223 dev_err(tps
->mfd
->dev
, "Read from reg 0x%x failed\n", reg
);
225 mutex_unlock(&tps
->io_lock
);
229 static int tps6507x_pmic_reg_write(struct tps6507x_pmic
*tps
, u8 reg
, u8 val
)
233 mutex_lock(&tps
->io_lock
);
235 err
= tps6507x_pmic_write(tps
, reg
, val
);
237 dev_err(tps
->mfd
->dev
, "Write for reg 0x%x failed\n", reg
);
239 mutex_unlock(&tps
->io_lock
);
243 static int tps6507x_pmic_dcdc_is_enabled(struct regulator_dev
*dev
)
245 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
246 int data
, dcdc
= rdev_get_id(dev
);
249 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
252 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
253 data
= tps6507x_pmic_reg_read(tps
, TPS6507X_REG_CON_CTRL1
);
258 return (data
& 1<<shift
) ? 1 : 0;
261 static int tps6507x_pmic_ldo_is_enabled(struct regulator_dev
*dev
)
263 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
264 int data
, ldo
= rdev_get_id(dev
);
267 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
270 shift
= TPS6507X_MAX_REG_ID
- ldo
;
271 data
= tps6507x_pmic_reg_read(tps
, TPS6507X_REG_CON_CTRL1
);
276 return (data
& 1<<shift
) ? 1 : 0;
279 static int tps6507x_pmic_dcdc_enable(struct regulator_dev
*dev
)
281 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
282 int dcdc
= rdev_get_id(dev
);
285 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
288 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
289 return tps6507x_pmic_set_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
292 static int tps6507x_pmic_dcdc_disable(struct regulator_dev
*dev
)
294 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
295 int dcdc
= rdev_get_id(dev
);
298 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
301 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
302 return tps6507x_pmic_clear_bits(tps
, TPS6507X_REG_CON_CTRL1
,
306 static int tps6507x_pmic_ldo_enable(struct regulator_dev
*dev
)
308 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
309 int ldo
= rdev_get_id(dev
);
312 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
315 shift
= TPS6507X_MAX_REG_ID
- ldo
;
316 return tps6507x_pmic_set_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
319 static int tps6507x_pmic_ldo_disable(struct regulator_dev
*dev
)
321 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
322 int ldo
= rdev_get_id(dev
);
325 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
328 shift
= TPS6507X_MAX_REG_ID
- ldo
;
329 return tps6507x_pmic_clear_bits(tps
, TPS6507X_REG_CON_CTRL1
,
333 static int tps6507x_pmic_dcdc_get_voltage(struct regulator_dev
*dev
)
335 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
336 int data
, dcdc
= rdev_get_id(dev
);
340 case TPS6507X_DCDC_1
:
341 reg
= TPS6507X_REG_DEFDCDC1
;
343 case TPS6507X_DCDC_2
:
344 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
346 case TPS6507X_DCDC_3
:
347 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
353 data
= tps6507x_pmic_reg_read(tps
, reg
);
357 data
&= TPS6507X_DEFDCDCX_DCDC_MASK
;
358 return tps
->info
[dcdc
]->table
[data
] * 1000;
361 static int tps6507x_pmic_dcdc_set_voltage(struct regulator_dev
*dev
,
362 int min_uV
, int max_uV
)
364 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
365 int data
, vsel
, dcdc
= rdev_get_id(dev
);
369 case TPS6507X_DCDC_1
:
370 reg
= TPS6507X_REG_DEFDCDC1
;
372 case TPS6507X_DCDC_2
:
373 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
375 case TPS6507X_DCDC_3
:
376 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
382 if (min_uV
< tps
->info
[dcdc
]->min_uV
383 || min_uV
> tps
->info
[dcdc
]->max_uV
)
385 if (max_uV
< tps
->info
[dcdc
]->min_uV
386 || max_uV
> tps
->info
[dcdc
]->max_uV
)
389 for (vsel
= 0; vsel
< tps
->info
[dcdc
]->table_len
; vsel
++) {
390 int mV
= tps
->info
[dcdc
]->table
[vsel
];
393 /* Break at the first in-range value */
394 if (min_uV
<= uV
&& uV
<= max_uV
)
398 /* write to the register in case we found a match */
399 if (vsel
== tps
->info
[dcdc
]->table_len
)
402 data
= tps6507x_pmic_reg_read(tps
, reg
);
406 data
&= ~TPS6507X_DEFDCDCX_DCDC_MASK
;
409 return tps6507x_pmic_reg_write(tps
, reg
, data
);
412 static int tps6507x_pmic_ldo_get_voltage(struct regulator_dev
*dev
)
414 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
415 int data
, ldo
= rdev_get_id(dev
);
418 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
421 reg
= (ldo
== TPS6507X_LDO_1
?
422 TPS6507X_REG_LDO_CTRL1
: TPS6507X_REG_DEFLDO2
);
423 mask
= (ldo
== TPS6507X_LDO_1
?
424 TPS6507X_REG_LDO_CTRL1_LDO1_MASK
:
425 TPS6507X_REG_DEFLDO2_LDO2_MASK
);
428 data
= tps6507x_pmic_reg_read(tps
, reg
);
433 return tps
->info
[ldo
]->table
[data
] * 1000;
436 static int tps6507x_pmic_ldo_set_voltage(struct regulator_dev
*dev
,
437 int min_uV
, int max_uV
)
439 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
440 int data
, vsel
, ldo
= rdev_get_id(dev
);
443 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
446 reg
= (ldo
== TPS6507X_LDO_1
?
447 TPS6507X_REG_LDO_CTRL1
: TPS6507X_REG_DEFLDO2
);
448 mask
= (ldo
== TPS6507X_LDO_1
?
449 TPS6507X_REG_LDO_CTRL1_LDO1_MASK
:
450 TPS6507X_REG_DEFLDO2_LDO2_MASK
);
453 if (min_uV
< tps
->info
[ldo
]->min_uV
|| min_uV
> tps
->info
[ldo
]->max_uV
)
455 if (max_uV
< tps
->info
[ldo
]->min_uV
|| max_uV
> tps
->info
[ldo
]->max_uV
)
458 for (vsel
= 0; vsel
< tps
->info
[ldo
]->table_len
; vsel
++) {
459 int mV
= tps
->info
[ldo
]->table
[vsel
];
462 /* Break at the first in-range value */
463 if (min_uV
<= uV
&& uV
<= max_uV
)
467 if (vsel
== tps
->info
[ldo
]->table_len
)
470 data
= tps6507x_pmic_reg_read(tps
, reg
);
477 return tps6507x_pmic_reg_write(tps
, reg
, data
);
480 static int tps6507x_pmic_dcdc_list_voltage(struct regulator_dev
*dev
,
483 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
484 int dcdc
= rdev_get_id(dev
);
486 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
489 if (selector
>= tps
->info
[dcdc
]->table_len
)
492 return tps
->info
[dcdc
]->table
[selector
] * 1000;
495 static int tps6507x_pmic_ldo_list_voltage(struct regulator_dev
*dev
,
498 struct tps6507x_pmic
*tps
= rdev_get_drvdata(dev
);
499 int ldo
= rdev_get_id(dev
);
501 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
504 if (selector
>= tps
->info
[ldo
]->table_len
)
507 return tps
->info
[ldo
]->table
[selector
] * 1000;
510 /* Operations permitted on VDCDCx */
511 static struct regulator_ops tps6507x_pmic_dcdc_ops
= {
512 .is_enabled
= tps6507x_pmic_dcdc_is_enabled
,
513 .enable
= tps6507x_pmic_dcdc_enable
,
514 .disable
= tps6507x_pmic_dcdc_disable
,
515 .get_voltage
= tps6507x_pmic_dcdc_get_voltage
,
516 .set_voltage
= tps6507x_pmic_dcdc_set_voltage
,
517 .list_voltage
= tps6507x_pmic_dcdc_list_voltage
,
520 /* Operations permitted on LDOx */
521 static struct regulator_ops tps6507x_pmic_ldo_ops
= {
522 .is_enabled
= tps6507x_pmic_ldo_is_enabled
,
523 .enable
= tps6507x_pmic_ldo_enable
,
524 .disable
= tps6507x_pmic_ldo_disable
,
525 .get_voltage
= tps6507x_pmic_ldo_get_voltage
,
526 .set_voltage
= tps6507x_pmic_ldo_set_voltage
,
527 .list_voltage
= tps6507x_pmic_ldo_list_voltage
,
531 int tps6507x_pmic_probe(struct platform_device
*pdev
)
533 struct tps6507x_dev
*tps6507x_dev
= dev_get_drvdata(pdev
->dev
.parent
);
535 const struct tps_info
*info
= &tps6507x_pmic_regs
[0];
536 struct regulator_init_data
*init_data
;
537 struct regulator_dev
*rdev
;
538 struct tps6507x_pmic
*tps
;
539 struct tps6507x_board
*tps_board
;
544 * tps_board points to pmic related constants
545 * coming from the board-evm file.
548 tps_board
= dev_get_platdata(tps6507x_dev
->dev
);
553 * init_data points to array of regulator_init structures
554 * coming from the board-evm file.
556 init_data
= tps_board
->tps6507x_pmic_init_data
;
560 tps
= kzalloc(sizeof(*tps
), GFP_KERNEL
);
564 mutex_init(&tps
->io_lock
);
566 /* common for all regulators */
567 tps
->mfd
= tps6507x_dev
;
569 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++, info
++, init_data
++) {
570 /* Register the regulators */
572 tps
->desc
[i
].name
= info
->name
;
573 tps
->desc
[i
].id
= desc_id
++;
574 tps
->desc
[i
].n_voltages
= num_voltages
[i
];
575 tps
->desc
[i
].ops
= (i
> TPS6507X_DCDC_3
?
576 &tps6507x_pmic_ldo_ops
: &tps6507x_pmic_dcdc_ops
);
577 tps
->desc
[i
].type
= REGULATOR_VOLTAGE
;
578 tps
->desc
[i
].owner
= THIS_MODULE
;
580 rdev
= regulator_register(&tps
->desc
[i
],
581 tps6507x_dev
->dev
, init_data
, tps
);
583 dev_err(tps6507x_dev
->dev
,
584 "failed to register %s regulator\n",
586 error
= PTR_ERR(rdev
);
590 /* Save regulator for cleanup */
594 tps6507x_dev
->pmic
= tps
;
600 regulator_unregister(tps
->rdev
[i
]);
607 * tps6507x_remove - TPS6507x driver i2c remove handler
608 * @client: i2c driver client device structure
610 * Unregister TPS driver as an i2c client device driver
612 static int __devexit
tps6507x_pmic_remove(struct platform_device
*pdev
)
614 struct tps6507x_dev
*tps6507x_dev
= platform_get_drvdata(pdev
);
615 struct tps6507x_pmic
*tps
= tps6507x_dev
->pmic
;
618 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++)
619 regulator_unregister(tps
->rdev
[i
]);
626 static struct platform_driver tps6507x_pmic_driver
= {
628 .name
= "tps6507x-pmic",
629 .owner
= THIS_MODULE
,
631 .probe
= tps6507x_pmic_probe
,
632 .remove
= __devexit_p(tps6507x_pmic_remove
),
638 * Module init function
640 static int __init
tps6507x_pmic_init(void)
642 return platform_driver_register(&tps6507x_pmic_driver
);
644 subsys_initcall(tps6507x_pmic_init
);
647 * tps6507x_pmic_cleanup
649 * Module exit function
651 static void __exit
tps6507x_pmic_cleanup(void)
653 platform_driver_unregister(&tps6507x_pmic_driver
);
655 module_exit(tps6507x_pmic_cleanup
);
657 MODULE_AUTHOR("Texas Instruments");
658 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
659 MODULE_LICENSE("GPL v2");
660 MODULE_ALIAS("platform:tps6507x-pmic");