4 * Supports TPS65023 Regulator
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/i2c.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/regmap.h>
30 /* Register definitions */
31 #define TPS65023_REG_VERSION 0
32 #define TPS65023_REG_PGOODZ 1
33 #define TPS65023_REG_MASK 2
34 #define TPS65023_REG_REG_CTRL 3
35 #define TPS65023_REG_CON_CTRL 4
36 #define TPS65023_REG_CON_CTRL2 5
37 #define TPS65023_REG_DEF_CORE 6
38 #define TPS65023_REG_DEFSLEW 7
39 #define TPS65023_REG_LDO_CTRL 8
41 /* PGOODZ bitfields */
42 #define TPS65023_PGOODZ_PWRFAILZ BIT(7)
43 #define TPS65023_PGOODZ_LOWBATTZ BIT(6)
44 #define TPS65023_PGOODZ_VDCDC1 BIT(5)
45 #define TPS65023_PGOODZ_VDCDC2 BIT(4)
46 #define TPS65023_PGOODZ_VDCDC3 BIT(3)
47 #define TPS65023_PGOODZ_LDO2 BIT(2)
48 #define TPS65023_PGOODZ_LDO1 BIT(1)
51 #define TPS65023_MASK_PWRFAILZ BIT(7)
52 #define TPS65023_MASK_LOWBATTZ BIT(6)
53 #define TPS65023_MASK_VDCDC1 BIT(5)
54 #define TPS65023_MASK_VDCDC2 BIT(4)
55 #define TPS65023_MASK_VDCDC3 BIT(3)
56 #define TPS65023_MASK_LDO2 BIT(2)
57 #define TPS65023_MASK_LDO1 BIT(1)
59 /* REG_CTRL bitfields */
60 #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
61 #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
62 #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
63 #define TPS65023_REG_CTRL_LDO2_EN BIT(2)
64 #define TPS65023_REG_CTRL_LDO1_EN BIT(1)
66 /* LDO_CTRL bitfields */
67 #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
68 #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0xF0 >> ((ldo_id)*4))
70 /* Number of step-down converters available */
71 #define TPS65023_NUM_DCDC 3
72 /* Number of LDO voltage regulators available */
73 #define TPS65023_NUM_LDO 2
74 /* Number of total regulators available */
75 #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
78 #define TPS65023_DCDC_1 0
79 #define TPS65023_DCDC_2 1
80 #define TPS65023_DCDC_3 2
82 #define TPS65023_LDO_1 3
83 #define TPS65023_LDO_2 4
85 #define TPS65023_MAX_REG_ID TPS65023_LDO_2
87 /* Supported voltage values for regulators */
88 static const u16 VDCDC1_VSEL_table
[] = {
91 1000, 1025, 1050, 1075,
92 1100, 1125, 1150, 1175,
93 1200, 1225, 1250, 1275,
94 1300, 1325, 1350, 1375,
95 1400, 1425, 1450, 1475,
96 1500, 1525, 1550, 1600,
99 static const u16 LDO1_VSEL_table
[] = {
100 1000, 1100, 1300, 1800,
101 2200, 2600, 2800, 3150,
104 static const u16 LDO2_VSEL_table
[] = {
105 1050, 1200, 1300, 1800,
106 2500, 2800, 3000, 3300,
109 static unsigned int num_voltages
[] = {ARRAY_SIZE(VDCDC1_VSEL_table
),
110 0, 0, ARRAY_SIZE(LDO1_VSEL_table
),
111 ARRAY_SIZE(LDO2_VSEL_table
)};
113 /* Regulator specific details */
125 struct regulator_desc desc
[TPS65023_NUM_REGULATOR
];
126 struct i2c_client
*client
;
127 struct regulator_dev
*rdev
[TPS65023_NUM_REGULATOR
];
128 const struct tps_info
*info
[TPS65023_NUM_REGULATOR
];
129 struct regmap
*regmap
;
132 static int tps_65023_set_bits(struct tps_pmic
*tps
, u8 reg
, u8 mask
)
134 return regmap_update_bits(tps
->regmap
, reg
, mask
, mask
);
137 static int tps_65023_clear_bits(struct tps_pmic
*tps
, u8 reg
, u8 mask
)
139 return regmap_update_bits(tps
->regmap
, reg
, mask
, 0);
142 static int tps_65023_reg_read(struct tps_pmic
*tps
, u8 reg
)
147 ret
= regmap_read(tps
->regmap
, reg
, &val
);
155 static int tps_65023_reg_write(struct tps_pmic
*tps
, u8 reg
, u8 val
)
157 return regmap_write(tps
->regmap
, reg
, val
);
160 static int tps65023_dcdc_is_enabled(struct regulator_dev
*dev
)
162 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
163 int data
, dcdc
= rdev_get_id(dev
);
166 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
169 shift
= TPS65023_NUM_REGULATOR
- dcdc
;
170 data
= tps_65023_reg_read(tps
, TPS65023_REG_REG_CTRL
);
175 return (data
& 1<<shift
) ? 1 : 0;
178 static int tps65023_ldo_is_enabled(struct regulator_dev
*dev
)
180 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
181 int data
, ldo
= rdev_get_id(dev
);
184 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
187 shift
= (ldo
== TPS65023_LDO_1
? 1 : 2);
188 data
= tps_65023_reg_read(tps
, TPS65023_REG_REG_CTRL
);
193 return (data
& 1<<shift
) ? 1 : 0;
196 static int tps65023_dcdc_enable(struct regulator_dev
*dev
)
198 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
199 int dcdc
= rdev_get_id(dev
);
202 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
205 shift
= TPS65023_NUM_REGULATOR
- dcdc
;
206 return tps_65023_set_bits(tps
, TPS65023_REG_REG_CTRL
, 1 << shift
);
209 static int tps65023_dcdc_disable(struct regulator_dev
*dev
)
211 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
212 int dcdc
= rdev_get_id(dev
);
215 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
218 shift
= TPS65023_NUM_REGULATOR
- dcdc
;
219 return tps_65023_clear_bits(tps
, TPS65023_REG_REG_CTRL
, 1 << shift
);
222 static int tps65023_ldo_enable(struct regulator_dev
*dev
)
224 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
225 int ldo
= rdev_get_id(dev
);
228 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
231 shift
= (ldo
== TPS65023_LDO_1
? 1 : 2);
232 return tps_65023_set_bits(tps
, TPS65023_REG_REG_CTRL
, 1 << shift
);
235 static int tps65023_ldo_disable(struct regulator_dev
*dev
)
237 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
238 int ldo
= rdev_get_id(dev
);
241 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
244 shift
= (ldo
== TPS65023_LDO_1
? 1 : 2);
245 return tps_65023_clear_bits(tps
, TPS65023_REG_REG_CTRL
, 1 << shift
);
248 static int tps65023_dcdc_get_voltage(struct regulator_dev
*dev
)
250 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
251 int data
, dcdc
= rdev_get_id(dev
);
253 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
256 if (dcdc
== TPS65023_DCDC_1
) {
257 data
= tps_65023_reg_read(tps
, TPS65023_REG_DEF_CORE
);
260 data
&= (tps
->info
[dcdc
]->table_len
- 1);
261 return tps
->info
[dcdc
]->table
[data
] * 1000;
263 return tps
->info
[dcdc
]->min_uV
;
266 static int tps65023_dcdc_set_voltage(struct regulator_dev
*dev
,
267 int min_uV
, int max_uV
,
270 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
271 int dcdc
= rdev_get_id(dev
);
274 if (dcdc
!= TPS65023_DCDC_1
)
277 if (min_uV
< tps
->info
[dcdc
]->min_uV
278 || min_uV
> tps
->info
[dcdc
]->max_uV
)
280 if (max_uV
< tps
->info
[dcdc
]->min_uV
281 || max_uV
> tps
->info
[dcdc
]->max_uV
)
284 for (vsel
= 0; vsel
< tps
->info
[dcdc
]->table_len
; vsel
++) {
285 int mV
= tps
->info
[dcdc
]->table
[vsel
];
288 /* Break at the first in-range value */
289 if (min_uV
<= uV
&& uV
<= max_uV
)
295 /* write to the register in case we found a match */
296 if (vsel
== tps
->info
[dcdc
]->table_len
)
299 return tps_65023_reg_write(tps
, TPS65023_REG_DEF_CORE
, vsel
);
302 static int tps65023_ldo_get_voltage(struct regulator_dev
*dev
)
304 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
305 int data
, ldo
= rdev_get_id(dev
);
307 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
310 data
= tps_65023_reg_read(tps
, TPS65023_REG_LDO_CTRL
);
314 data
>>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo
- TPS65023_LDO_1
));
315 data
&= (tps
->info
[ldo
]->table_len
- 1);
316 return tps
->info
[ldo
]->table
[data
] * 1000;
319 static int tps65023_ldo_set_voltage(struct regulator_dev
*dev
,
320 int min_uV
, int max_uV
, unsigned *selector
)
322 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
323 int data
, vsel
, ldo
= rdev_get_id(dev
);
325 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
328 if (min_uV
< tps
->info
[ldo
]->min_uV
|| min_uV
> tps
->info
[ldo
]->max_uV
)
330 if (max_uV
< tps
->info
[ldo
]->min_uV
|| max_uV
> tps
->info
[ldo
]->max_uV
)
333 for (vsel
= 0; vsel
< tps
->info
[ldo
]->table_len
; vsel
++) {
334 int mV
= tps
->info
[ldo
]->table
[vsel
];
337 /* Break at the first in-range value */
338 if (min_uV
<= uV
&& uV
<= max_uV
)
342 if (vsel
== tps
->info
[ldo
]->table_len
)
347 data
= tps_65023_reg_read(tps
, TPS65023_REG_LDO_CTRL
);
351 data
&= TPS65023_LDO_CTRL_LDOx_MASK(ldo
- TPS65023_LDO_1
);
352 data
|= (vsel
<< (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo
- TPS65023_LDO_1
)));
353 return tps_65023_reg_write(tps
, TPS65023_REG_LDO_CTRL
, data
);
356 static int tps65023_dcdc_list_voltage(struct regulator_dev
*dev
,
359 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
360 int dcdc
= rdev_get_id(dev
);
362 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
365 if (dcdc
== TPS65023_DCDC_1
) {
366 if (selector
>= tps
->info
[dcdc
]->table_len
)
369 return tps
->info
[dcdc
]->table
[selector
] * 1000;
371 return tps
->info
[dcdc
]->min_uV
;
374 static int tps65023_ldo_list_voltage(struct regulator_dev
*dev
,
377 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
378 int ldo
= rdev_get_id(dev
);
380 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
383 if (selector
>= tps
->info
[ldo
]->table_len
)
386 return tps
->info
[ldo
]->table
[selector
] * 1000;
389 /* Operations permitted on VDCDCx */
390 static struct regulator_ops tps65023_dcdc_ops
= {
391 .is_enabled
= tps65023_dcdc_is_enabled
,
392 .enable
= tps65023_dcdc_enable
,
393 .disable
= tps65023_dcdc_disable
,
394 .get_voltage
= tps65023_dcdc_get_voltage
,
395 .set_voltage
= tps65023_dcdc_set_voltage
,
396 .list_voltage
= tps65023_dcdc_list_voltage
,
399 /* Operations permitted on LDOx */
400 static struct regulator_ops tps65023_ldo_ops
= {
401 .is_enabled
= tps65023_ldo_is_enabled
,
402 .enable
= tps65023_ldo_enable
,
403 .disable
= tps65023_ldo_disable
,
404 .get_voltage
= tps65023_ldo_get_voltage
,
405 .set_voltage
= tps65023_ldo_set_voltage
,
406 .list_voltage
= tps65023_ldo_list_voltage
,
409 static struct regmap_config tps65023_regmap_config
= {
414 static int __devinit
tps_65023_probe(struct i2c_client
*client
,
415 const struct i2c_device_id
*id
)
417 const struct tps_info
*info
= (void *)id
->driver_data
;
418 struct regulator_init_data
*init_data
;
419 struct regulator_dev
*rdev
;
420 struct tps_pmic
*tps
;
424 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
428 * init_data points to array of regulator_init structures
429 * coming from the board-evm file.
431 init_data
= client
->dev
.platform_data
;
435 tps
= kzalloc(sizeof(*tps
), GFP_KERNEL
);
439 tps
->regmap
= regmap_init_i2c(client
, &tps65023_regmap_config
);
440 if (IS_ERR(tps
->regmap
)) {
441 error
= PTR_ERR(tps
->regmap
);
442 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
447 /* common for all regulators */
448 tps
->client
= client
;
450 for (i
= 0; i
< TPS65023_NUM_REGULATOR
; i
++, info
++, init_data
++) {
451 /* Store regulator specific information */
454 tps
->desc
[i
].name
= info
->name
;
456 tps
->desc
[i
].n_voltages
= num_voltages
[i
];
457 tps
->desc
[i
].ops
= (i
> TPS65023_DCDC_3
?
458 &tps65023_ldo_ops
: &tps65023_dcdc_ops
);
459 tps
->desc
[i
].type
= REGULATOR_VOLTAGE
;
460 tps
->desc
[i
].owner
= THIS_MODULE
;
462 /* Register the regulators */
463 rdev
= regulator_register(&tps
->desc
[i
], &client
->dev
,
466 dev_err(&client
->dev
, "failed to register %s\n",
468 error
= PTR_ERR(rdev
);
472 /* Save regulator for cleanup */
476 i2c_set_clientdata(client
, tps
);
482 regulator_unregister(tps
->rdev
[i
]);
484 regmap_exit(tps
->regmap
);
491 * tps_65023_remove - TPS65023 driver i2c remove handler
492 * @client: i2c driver client device structure
494 * Unregister TPS driver as an i2c client device driver
496 static int __devexit
tps_65023_remove(struct i2c_client
*client
)
498 struct tps_pmic
*tps
= i2c_get_clientdata(client
);
501 for (i
= 0; i
< TPS65023_NUM_REGULATOR
; i
++)
502 regulator_unregister(tps
->rdev
[i
]);
504 regmap_exit(tps
->regmap
);
510 static const struct tps_info tps65023_regs
[] = {
515 .table_len
= ARRAY_SIZE(VDCDC1_VSEL_table
),
516 .table
= VDCDC1_VSEL_table
,
534 .table_len
= ARRAY_SIZE(LDO1_VSEL_table
),
535 .table
= LDO1_VSEL_table
,
541 .table_len
= ARRAY_SIZE(LDO2_VSEL_table
),
542 .table
= LDO2_VSEL_table
,
546 static const struct i2c_device_id tps_65023_id
[] = {
548 .driver_data
= (unsigned long) tps65023_regs
,},
550 .driver_data
= (unsigned long) tps65023_regs
,},
554 MODULE_DEVICE_TABLE(i2c
, tps_65023_id
);
556 static struct i2c_driver tps_65023_i2c_driver
= {
559 .owner
= THIS_MODULE
,
561 .probe
= tps_65023_probe
,
562 .remove
= __devexit_p(tps_65023_remove
),
563 .id_table
= tps_65023_id
,
569 * Module init function
571 static int __init
tps_65023_init(void)
573 return i2c_add_driver(&tps_65023_i2c_driver
);
575 subsys_initcall(tps_65023_init
);
580 * Module exit function
582 static void __exit
tps_65023_cleanup(void)
584 i2c_del_driver(&tps_65023_i2c_driver
);
586 module_exit(tps_65023_cleanup
);
588 MODULE_AUTHOR("Texas Instruments");
589 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
590 MODULE_LICENSE("GPL v2");