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 /* REG_CTRL2 bitfields */
67 #define TPS65023_REG_CTRL2_GO BIT(7)
68 #define TPS65023_REG_CTRL2_CORE_ADJ BIT(6)
69 #define TPS65023_REG_CTRL2_DCDC2 BIT(2)
70 #define TPS65023_REG_CTRL2_DCDC1 BIT(1)
71 #define TPS65023_REG_CTRL2_DCDC3 BIT(0)
73 /* LDO_CTRL bitfields */
74 #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
75 #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0xF0 >> ((ldo_id)*4))
77 /* Number of step-down converters available */
78 #define TPS65023_NUM_DCDC 3
79 /* Number of LDO voltage regulators available */
80 #define TPS65023_NUM_LDO 2
81 /* Number of total regulators available */
82 #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
85 #define TPS65023_DCDC_1 0
86 #define TPS65023_DCDC_2 1
87 #define TPS65023_DCDC_3 2
89 #define TPS65023_LDO_1 3
90 #define TPS65023_LDO_2 4
92 #define TPS65023_MAX_REG_ID TPS65023_LDO_2
94 /* Supported voltage values for regulators */
95 static const u16 VCORE_VSEL_table
[] = {
98 1000, 1025, 1050, 1075,
99 1100, 1125, 1150, 1175,
100 1200, 1225, 1250, 1275,
101 1300, 1325, 1350, 1375,
102 1400, 1425, 1450, 1475,
103 1500, 1525, 1550, 1600,
106 /* Supported voltage values for LDO regulators for tps65020 */
107 static const u16 TPS65020_LDO1_VSEL_table
[] = {
108 1000, 1050, 1100, 1300,
109 1800, 2500, 3000, 3300,
112 static const u16 TPS65020_LDO2_VSEL_table
[] = {
113 1000, 1050, 1100, 1300,
114 1800, 2500, 3000, 3300,
117 /* Supported voltage values for LDO regulators
118 * for tps65021 and tps65023 */
119 static const u16 TPS65023_LDO1_VSEL_table
[] = {
120 1000, 1100, 1300, 1800,
121 2200, 2600, 2800, 3150,
124 static const u16 TPS65023_LDO2_VSEL_table
[] = {
125 1050, 1200, 1300, 1800,
126 2500, 2800, 3000, 3300,
129 /* Regulator specific details */
141 struct regulator_desc desc
[TPS65023_NUM_REGULATOR
];
142 struct i2c_client
*client
;
143 struct regulator_dev
*rdev
[TPS65023_NUM_REGULATOR
];
144 const struct tps_info
*info
[TPS65023_NUM_REGULATOR
];
145 struct regmap
*regmap
;
149 /* Struct passed as driver data */
150 struct tps_driver_data
{
151 const struct tps_info
*info
;
155 static int tps65023_dcdc_is_enabled(struct regulator_dev
*dev
)
157 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
158 int data
, dcdc
= rdev_get_id(dev
);
162 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
165 shift
= TPS65023_NUM_REGULATOR
- dcdc
;
166 ret
= regmap_read(tps
->regmap
, TPS65023_REG_REG_CTRL
, &data
);
171 return (data
& 1<<shift
) ? 1 : 0;
174 static int tps65023_ldo_is_enabled(struct regulator_dev
*dev
)
176 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
177 int data
, ldo
= rdev_get_id(dev
);
181 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
184 shift
= (ldo
== TPS65023_LDO_1
? 1 : 2);
185 ret
= regmap_read(tps
->regmap
, TPS65023_REG_REG_CTRL
, &data
);
190 return (data
& 1<<shift
) ? 1 : 0;
193 static int tps65023_dcdc_enable(struct regulator_dev
*dev
)
195 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
196 int dcdc
= rdev_get_id(dev
);
199 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
202 shift
= TPS65023_NUM_REGULATOR
- dcdc
;
203 return regmap_update_bits(tps
->regmap
, TPS65023_REG_REG_CTRL
, 1 << shift
, 1 << shift
);
206 static int tps65023_dcdc_disable(struct regulator_dev
*dev
)
208 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
209 int dcdc
= rdev_get_id(dev
);
212 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
215 shift
= TPS65023_NUM_REGULATOR
- dcdc
;
216 return regmap_update_bits(tps
->regmap
, TPS65023_REG_REG_CTRL
, 1 << shift
, 0);
219 static int tps65023_ldo_enable(struct regulator_dev
*dev
)
221 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
222 int ldo
= rdev_get_id(dev
);
225 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
228 shift
= (ldo
== TPS65023_LDO_1
? 1 : 2);
229 return regmap_update_bits(tps
->regmap
, TPS65023_REG_REG_CTRL
, 1 << shift
, 1 << shift
);
232 static int tps65023_ldo_disable(struct regulator_dev
*dev
)
234 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
235 int ldo
= rdev_get_id(dev
);
238 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
241 shift
= (ldo
== TPS65023_LDO_1
? 1 : 2);
242 return regmap_update_bits(tps
->regmap
, TPS65023_REG_REG_CTRL
, 1 << shift
, 0);
245 static int tps65023_dcdc_get_voltage(struct regulator_dev
*dev
)
247 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
249 int data
, dcdc
= rdev_get_id(dev
);
251 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
254 if (dcdc
== tps
->core_regulator
) {
255 ret
= regmap_read(tps
->regmap
, TPS65023_REG_DEF_CORE
, &data
);
258 data
&= (tps
->info
[dcdc
]->table_len
- 1);
259 return tps
->info
[dcdc
]->table
[data
] * 1000;
261 return tps
->info
[dcdc
]->min_uV
;
264 static int tps65023_dcdc_set_voltage(struct regulator_dev
*dev
,
265 int min_uV
, int max_uV
,
268 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
269 int dcdc
= rdev_get_id(dev
);
273 if (dcdc
!= tps
->core_regulator
)
275 if (min_uV
< tps
->info
[dcdc
]->min_uV
276 || min_uV
> tps
->info
[dcdc
]->max_uV
)
278 if (max_uV
< tps
->info
[dcdc
]->min_uV
279 || max_uV
> tps
->info
[dcdc
]->max_uV
)
282 for (vsel
= 0; vsel
< tps
->info
[dcdc
]->table_len
; vsel
++) {
283 int mV
= tps
->info
[dcdc
]->table
[vsel
];
286 /* Break at the first in-range value */
287 if (min_uV
<= uV
&& uV
<= max_uV
)
293 if (vsel
== tps
->info
[dcdc
]->table_len
)
296 ret
= regmap_write(tps
->regmap
, TPS65023_REG_DEF_CORE
, vsel
);
298 /* Tell the chip that we have changed the value in DEFCORE
299 * and its time to update the core voltage
301 regmap_update_bits(tps
->regmap
, TPS65023_REG_CON_CTRL2
,
302 TPS65023_REG_CTRL2_GO
, TPS65023_REG_CTRL2_GO
);
310 static int tps65023_ldo_get_voltage(struct regulator_dev
*dev
)
312 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
313 int data
, ldo
= rdev_get_id(dev
);
316 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
319 ret
= regmap_read(tps
->regmap
, TPS65023_REG_LDO_CTRL
, &data
);
323 data
>>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo
- TPS65023_LDO_1
));
324 data
&= (tps
->info
[ldo
]->table_len
- 1);
325 return tps
->info
[ldo
]->table
[data
] * 1000;
328 static int tps65023_ldo_set_voltage(struct regulator_dev
*dev
,
329 int min_uV
, int max_uV
, unsigned *selector
)
331 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
332 int data
, vsel
, ldo
= rdev_get_id(dev
);
335 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
338 if (min_uV
< tps
->info
[ldo
]->min_uV
|| min_uV
> tps
->info
[ldo
]->max_uV
)
340 if (max_uV
< tps
->info
[ldo
]->min_uV
|| max_uV
> tps
->info
[ldo
]->max_uV
)
343 for (vsel
= 0; vsel
< tps
->info
[ldo
]->table_len
; vsel
++) {
344 int mV
= tps
->info
[ldo
]->table
[vsel
];
347 /* Break at the first in-range value */
348 if (min_uV
<= uV
&& uV
<= max_uV
)
352 if (vsel
== tps
->info
[ldo
]->table_len
)
357 ret
= regmap_read(tps
->regmap
, TPS65023_REG_LDO_CTRL
, &data
);
361 data
&= TPS65023_LDO_CTRL_LDOx_MASK(ldo
- TPS65023_LDO_1
);
362 data
|= (vsel
<< (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo
- TPS65023_LDO_1
)));
363 return regmap_write(tps
->regmap
, TPS65023_REG_LDO_CTRL
, data
);
366 static int tps65023_dcdc_list_voltage(struct regulator_dev
*dev
,
369 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
370 int dcdc
= rdev_get_id(dev
);
372 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
375 if (dcdc
== tps
->core_regulator
) {
376 if (selector
>= tps
->info
[dcdc
]->table_len
)
379 return tps
->info
[dcdc
]->table
[selector
] * 1000;
381 return tps
->info
[dcdc
]->min_uV
;
384 static int tps65023_ldo_list_voltage(struct regulator_dev
*dev
,
387 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
388 int ldo
= rdev_get_id(dev
);
390 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
393 if (selector
>= tps
->info
[ldo
]->table_len
)
396 return tps
->info
[ldo
]->table
[selector
] * 1000;
399 /* Operations permitted on VDCDCx */
400 static struct regulator_ops tps65023_dcdc_ops
= {
401 .is_enabled
= tps65023_dcdc_is_enabled
,
402 .enable
= tps65023_dcdc_enable
,
403 .disable
= tps65023_dcdc_disable
,
404 .get_voltage
= tps65023_dcdc_get_voltage
,
405 .set_voltage
= tps65023_dcdc_set_voltage
,
406 .list_voltage
= tps65023_dcdc_list_voltage
,
409 /* Operations permitted on LDOx */
410 static struct regulator_ops tps65023_ldo_ops
= {
411 .is_enabled
= tps65023_ldo_is_enabled
,
412 .enable
= tps65023_ldo_enable
,
413 .disable
= tps65023_ldo_disable
,
414 .get_voltage
= tps65023_ldo_get_voltage
,
415 .set_voltage
= tps65023_ldo_set_voltage
,
416 .list_voltage
= tps65023_ldo_list_voltage
,
419 static struct regmap_config tps65023_regmap_config
= {
424 static int __devinit
tps_65023_probe(struct i2c_client
*client
,
425 const struct i2c_device_id
*id
)
427 const struct tps_driver_data
*drv_data
= (void *)id
->driver_data
;
428 const struct tps_info
*info
= drv_data
->info
;
429 struct regulator_init_data
*init_data
;
430 struct regulator_dev
*rdev
;
431 struct tps_pmic
*tps
;
435 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
439 * init_data points to array of regulator_init structures
440 * coming from the board-evm file.
442 init_data
= client
->dev
.platform_data
;
446 tps
= kzalloc(sizeof(*tps
), GFP_KERNEL
);
450 tps
->regmap
= regmap_init_i2c(client
, &tps65023_regmap_config
);
451 if (IS_ERR(tps
->regmap
)) {
452 error
= PTR_ERR(tps
->regmap
);
453 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
458 /* common for all regulators */
459 tps
->client
= client
;
460 tps
->core_regulator
= drv_data
->core_regulator
;
462 for (i
= 0; i
< TPS65023_NUM_REGULATOR
; i
++, info
++, init_data
++) {
463 /* Store regulator specific information */
466 tps
->desc
[i
].name
= info
->name
;
468 tps
->desc
[i
].n_voltages
= info
->table_len
;
469 tps
->desc
[i
].ops
= (i
> TPS65023_DCDC_3
?
470 &tps65023_ldo_ops
: &tps65023_dcdc_ops
);
471 tps
->desc
[i
].type
= REGULATOR_VOLTAGE
;
472 tps
->desc
[i
].owner
= THIS_MODULE
;
474 /* Register the regulators */
475 rdev
= regulator_register(&tps
->desc
[i
], &client
->dev
,
476 init_data
, tps
, NULL
);
478 dev_err(&client
->dev
, "failed to register %s\n",
480 error
= PTR_ERR(rdev
);
484 /* Save regulator for cleanup */
488 i2c_set_clientdata(client
, tps
);
490 /* Enable setting output voltage by I2C */
491 regmap_update_bits(tps
->regmap
, TPS65023_REG_CON_CTRL2
,
492 TPS65023_REG_CTRL2_CORE_ADJ
, TPS65023_REG_CTRL2_CORE_ADJ
);
494 /* Enable setting output voltage by I2C */
495 regmap_update_bits(tps
->regmap
, TPS65023_REG_CON_CTRL2
,
496 TPS65023_REG_CTRL2_CORE_ADJ
, TPS65023_REG_CTRL2_CORE_ADJ
);
502 regulator_unregister(tps
->rdev
[i
]);
504 regmap_exit(tps
->regmap
);
511 * tps_65023_remove - TPS65023 driver i2c remove handler
512 * @client: i2c driver client device structure
514 * Unregister TPS driver as an i2c client device driver
516 static int __devexit
tps_65023_remove(struct i2c_client
*client
)
518 struct tps_pmic
*tps
= i2c_get_clientdata(client
);
521 for (i
= 0; i
< TPS65023_NUM_REGULATOR
; i
++)
522 regulator_unregister(tps
->rdev
[i
]);
524 regmap_exit(tps
->regmap
);
530 static const struct tps_info tps65020_regs
[] = {
547 .table_len
= ARRAY_SIZE(VCORE_VSEL_table
),
548 .table
= VCORE_VSEL_table
,
555 .table_len
= ARRAY_SIZE(TPS65020_LDO1_VSEL_table
),
556 .table
= TPS65020_LDO1_VSEL_table
,
562 .table_len
= ARRAY_SIZE(TPS65020_LDO2_VSEL_table
),
563 .table
= TPS65020_LDO2_VSEL_table
,
567 static const struct tps_info tps65021_regs
[] = {
584 .table_len
= ARRAY_SIZE(VCORE_VSEL_table
),
585 .table
= VCORE_VSEL_table
,
591 .table_len
= ARRAY_SIZE(TPS65023_LDO1_VSEL_table
),
592 .table
= TPS65023_LDO1_VSEL_table
,
598 .table_len
= ARRAY_SIZE(TPS65023_LDO2_VSEL_table
),
599 .table
= TPS65023_LDO2_VSEL_table
,
603 static const struct tps_info tps65023_regs
[] = {
608 .table_len
= ARRAY_SIZE(VCORE_VSEL_table
),
609 .table
= VCORE_VSEL_table
,
627 .table_len
= ARRAY_SIZE(TPS65023_LDO1_VSEL_table
),
628 .table
= TPS65023_LDO1_VSEL_table
,
634 .table_len
= ARRAY_SIZE(TPS65023_LDO2_VSEL_table
),
635 .table
= TPS65023_LDO2_VSEL_table
,
639 static struct tps_driver_data tps65020_drv_data
= {
640 .info
= tps65020_regs
,
641 .core_regulator
= TPS65023_DCDC_3
,
644 static struct tps_driver_data tps65021_drv_data
= {
645 .info
= tps65021_regs
,
646 .core_regulator
= TPS65023_DCDC_3
,
649 static struct tps_driver_data tps65023_drv_data
= {
650 .info
= tps65023_regs
,
651 .core_regulator
= TPS65023_DCDC_1
,
654 static const struct i2c_device_id tps_65023_id
[] = {
656 .driver_data
= (unsigned long) &tps65023_drv_data
},
658 .driver_data
= (unsigned long) &tps65021_drv_data
,},
660 .driver_data
= (unsigned long) &tps65020_drv_data
},
664 MODULE_DEVICE_TABLE(i2c
, tps_65023_id
);
666 static struct i2c_driver tps_65023_i2c_driver
= {
669 .owner
= THIS_MODULE
,
671 .probe
= tps_65023_probe
,
672 .remove
= __devexit_p(tps_65023_remove
),
673 .id_table
= tps_65023_id
,
679 * Module init function
681 static int __init
tps_65023_init(void)
683 return i2c_add_driver(&tps_65023_i2c_driver
);
685 subsys_initcall(tps_65023_init
);
690 * Module exit function
692 static void __exit
tps_65023_cleanup(void)
694 i2c_del_driver(&tps_65023_i2c_driver
);
696 module_exit(tps_65023_cleanup
);
698 MODULE_AUTHOR("Texas Instruments");
699 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
700 MODULE_LICENSE("GPL v2");