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/slab.h>
27 #include <linux/regmap.h>
29 /* Register definitions */
30 #define TPS65023_REG_VERSION 0
31 #define TPS65023_REG_PGOODZ 1
32 #define TPS65023_REG_MASK 2
33 #define TPS65023_REG_REG_CTRL 3
34 #define TPS65023_REG_CON_CTRL 4
35 #define TPS65023_REG_CON_CTRL2 5
36 #define TPS65023_REG_DEF_CORE 6
37 #define TPS65023_REG_DEFSLEW 7
38 #define TPS65023_REG_LDO_CTRL 8
40 /* PGOODZ bitfields */
41 #define TPS65023_PGOODZ_PWRFAILZ BIT(7)
42 #define TPS65023_PGOODZ_LOWBATTZ BIT(6)
43 #define TPS65023_PGOODZ_VDCDC1 BIT(5)
44 #define TPS65023_PGOODZ_VDCDC2 BIT(4)
45 #define TPS65023_PGOODZ_VDCDC3 BIT(3)
46 #define TPS65023_PGOODZ_LDO2 BIT(2)
47 #define TPS65023_PGOODZ_LDO1 BIT(1)
50 #define TPS65023_MASK_PWRFAILZ BIT(7)
51 #define TPS65023_MASK_LOWBATTZ BIT(6)
52 #define TPS65023_MASK_VDCDC1 BIT(5)
53 #define TPS65023_MASK_VDCDC2 BIT(4)
54 #define TPS65023_MASK_VDCDC3 BIT(3)
55 #define TPS65023_MASK_LDO2 BIT(2)
56 #define TPS65023_MASK_LDO1 BIT(1)
58 /* REG_CTRL bitfields */
59 #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
60 #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
61 #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
62 #define TPS65023_REG_CTRL_LDO2_EN BIT(2)
63 #define TPS65023_REG_CTRL_LDO1_EN BIT(1)
65 /* REG_CTRL2 bitfields */
66 #define TPS65023_REG_CTRL2_GO BIT(7)
67 #define TPS65023_REG_CTRL2_CORE_ADJ BIT(6)
68 #define TPS65023_REG_CTRL2_DCDC2 BIT(2)
69 #define TPS65023_REG_CTRL2_DCDC1 BIT(1)
70 #define TPS65023_REG_CTRL2_DCDC3 BIT(0)
72 /* LDO_CTRL bitfields */
73 #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
74 #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0x07 << ((ldo_id)*4))
76 /* Number of step-down converters available */
77 #define TPS65023_NUM_DCDC 3
78 /* Number of LDO voltage regulators available */
79 #define TPS65023_NUM_LDO 2
80 /* Number of total regulators available */
81 #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
84 #define TPS65023_DCDC_1 0
85 #define TPS65023_DCDC_2 1
86 #define TPS65023_DCDC_3 2
88 #define TPS65023_LDO_1 3
89 #define TPS65023_LDO_2 4
91 #define TPS65023_MAX_REG_ID TPS65023_LDO_2
93 /* Supported voltage values for regulators */
94 static const u16 VCORE_VSEL_table
[] = {
97 1000, 1025, 1050, 1075,
98 1100, 1125, 1150, 1175,
99 1200, 1225, 1250, 1275,
100 1300, 1325, 1350, 1375,
101 1400, 1425, 1450, 1475,
102 1500, 1525, 1550, 1600,
105 /* Supported voltage values for LDO regulators for tps65020 */
106 static const u16 TPS65020_LDO1_VSEL_table
[] = {
107 1000, 1050, 1100, 1300,
108 1800, 2500, 3000, 3300,
111 static const u16 TPS65020_LDO2_VSEL_table
[] = {
112 1000, 1050, 1100, 1300,
113 1800, 2500, 3000, 3300,
116 /* Supported voltage values for LDO regulators
117 * for tps65021 and tps65023 */
118 static const u16 TPS65023_LDO1_VSEL_table
[] = {
119 1000, 1100, 1300, 1800,
120 2200, 2600, 2800, 3150,
123 static const u16 TPS65023_LDO2_VSEL_table
[] = {
124 1050, 1200, 1300, 1800,
125 2500, 2800, 3000, 3300,
128 /* Regulator specific details */
140 struct regulator_desc desc
[TPS65023_NUM_REGULATOR
];
141 struct regulator_dev
*rdev
[TPS65023_NUM_REGULATOR
];
142 const struct tps_info
*info
[TPS65023_NUM_REGULATOR
];
143 struct regmap
*regmap
;
147 /* Struct passed as driver data */
148 struct tps_driver_data
{
149 const struct tps_info
*info
;
153 static int tps65023_dcdc_get_voltage(struct regulator_dev
*dev
)
155 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
157 int data
, dcdc
= rdev_get_id(dev
);
159 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
162 if (dcdc
== tps
->core_regulator
) {
163 ret
= regmap_read(tps
->regmap
, TPS65023_REG_DEF_CORE
, &data
);
166 data
&= (tps
->info
[dcdc
]->table_len
- 1);
167 return tps
->info
[dcdc
]->table
[data
] * 1000;
169 return tps
->info
[dcdc
]->min_uV
;
172 static int tps65023_dcdc_set_voltage_sel(struct regulator_dev
*dev
,
175 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
176 int dcdc
= rdev_get_id(dev
);
179 if (dcdc
!= tps
->core_regulator
)
182 ret
= regmap_write(tps
->regmap
, TPS65023_REG_DEF_CORE
, selector
);
186 /* Tell the chip that we have changed the value in DEFCORE
187 * and its time to update the core voltage
189 ret
= regmap_update_bits(tps
->regmap
, TPS65023_REG_CON_CTRL2
,
190 TPS65023_REG_CTRL2_GO
, TPS65023_REG_CTRL2_GO
);
196 static int tps65023_ldo_get_voltage(struct regulator_dev
*dev
)
198 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
199 int data
, ldo
= rdev_get_id(dev
);
202 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
205 ret
= regmap_read(tps
->regmap
, TPS65023_REG_LDO_CTRL
, &data
);
209 data
>>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo
- TPS65023_LDO_1
));
210 data
&= (tps
->info
[ldo
]->table_len
- 1);
211 return tps
->info
[ldo
]->table
[data
] * 1000;
214 static int tps65023_ldo_set_voltage_sel(struct regulator_dev
*dev
,
217 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
218 int ldo_index
= rdev_get_id(dev
) - TPS65023_LDO_1
;
220 return regmap_update_bits(tps
->regmap
, TPS65023_REG_LDO_CTRL
,
221 TPS65023_LDO_CTRL_LDOx_MASK(ldo_index
),
222 selector
<< TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_index
));
225 static int tps65023_dcdc_list_voltage(struct regulator_dev
*dev
,
228 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
229 int dcdc
= rdev_get_id(dev
);
231 if (dcdc
< TPS65023_DCDC_1
|| dcdc
> TPS65023_DCDC_3
)
234 if (dcdc
== tps
->core_regulator
) {
235 if (selector
>= tps
->info
[dcdc
]->table_len
)
238 return tps
->info
[dcdc
]->table
[selector
] * 1000;
240 return tps
->info
[dcdc
]->min_uV
;
243 static int tps65023_ldo_list_voltage(struct regulator_dev
*dev
,
246 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
247 int ldo
= rdev_get_id(dev
);
249 if (ldo
< TPS65023_LDO_1
|| ldo
> TPS65023_LDO_2
)
252 if (selector
>= tps
->info
[ldo
]->table_len
)
255 return tps
->info
[ldo
]->table
[selector
] * 1000;
258 /* Operations permitted on VDCDCx */
259 static struct regulator_ops tps65023_dcdc_ops
= {
260 .is_enabled
= regulator_is_enabled_regmap
,
261 .enable
= regulator_enable_regmap
,
262 .disable
= regulator_disable_regmap
,
263 .get_voltage
= tps65023_dcdc_get_voltage
,
264 .set_voltage_sel
= tps65023_dcdc_set_voltage_sel
,
265 .list_voltage
= tps65023_dcdc_list_voltage
,
268 /* Operations permitted on LDOx */
269 static struct regulator_ops tps65023_ldo_ops
= {
270 .is_enabled
= regulator_is_enabled_regmap
,
271 .enable
= regulator_enable_regmap
,
272 .disable
= regulator_disable_regmap
,
273 .get_voltage
= tps65023_ldo_get_voltage
,
274 .set_voltage_sel
= tps65023_ldo_set_voltage_sel
,
275 .list_voltage
= tps65023_ldo_list_voltage
,
278 static struct regmap_config tps65023_regmap_config
= {
283 static int __devinit
tps_65023_probe(struct i2c_client
*client
,
284 const struct i2c_device_id
*id
)
286 const struct tps_driver_data
*drv_data
= (void *)id
->driver_data
;
287 const struct tps_info
*info
= drv_data
->info
;
288 struct regulator_config config
= { };
289 struct regulator_init_data
*init_data
;
290 struct regulator_dev
*rdev
;
291 struct tps_pmic
*tps
;
295 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
299 * init_data points to array of regulator_init structures
300 * coming from the board-evm file.
302 init_data
= client
->dev
.platform_data
;
306 tps
= devm_kzalloc(&client
->dev
, sizeof(*tps
), GFP_KERNEL
);
310 tps
->regmap
= devm_regmap_init_i2c(client
, &tps65023_regmap_config
);
311 if (IS_ERR(tps
->regmap
)) {
312 error
= PTR_ERR(tps
->regmap
);
313 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
318 /* common for all regulators */
319 tps
->core_regulator
= drv_data
->core_regulator
;
321 for (i
= 0; i
< TPS65023_NUM_REGULATOR
; i
++, info
++, init_data
++) {
322 /* Store regulator specific information */
325 tps
->desc
[i
].name
= info
->name
;
327 tps
->desc
[i
].n_voltages
= info
->table_len
;
328 tps
->desc
[i
].ops
= (i
> TPS65023_DCDC_3
?
329 &tps65023_ldo_ops
: &tps65023_dcdc_ops
);
330 tps
->desc
[i
].type
= REGULATOR_VOLTAGE
;
331 tps
->desc
[i
].owner
= THIS_MODULE
;
333 tps
->desc
[i
].enable_reg
= TPS65023_REG_REG_CTRL
;
334 if (i
== TPS65023_LDO_1
)
335 tps
->desc
[i
].enable_mask
= 1 << 1;
336 else if (i
== TPS65023_LDO_2
)
337 tps
->desc
[i
].enable_mask
= 1 << 2;
339 tps
->desc
[i
].enable_mask
=
340 1 << (TPS65023_NUM_REGULATOR
- i
);
342 config
.dev
= &client
->dev
;
343 config
.init_data
= init_data
;
344 config
.driver_data
= tps
;
345 config
.regmap
= tps
->regmap
;
347 /* Register the regulators */
348 rdev
= regulator_register(&tps
->desc
[i
], &config
);
350 dev_err(&client
->dev
, "failed to register %s\n",
352 error
= PTR_ERR(rdev
);
356 /* Save regulator for cleanup */
360 i2c_set_clientdata(client
, tps
);
362 /* Enable setting output voltage by I2C */
363 regmap_update_bits(tps
->regmap
, TPS65023_REG_CON_CTRL2
,
364 TPS65023_REG_CTRL2_CORE_ADJ
, TPS65023_REG_CTRL2_CORE_ADJ
);
370 regulator_unregister(tps
->rdev
[i
]);
374 static int __devexit
tps_65023_remove(struct i2c_client
*client
)
376 struct tps_pmic
*tps
= i2c_get_clientdata(client
);
379 for (i
= 0; i
< TPS65023_NUM_REGULATOR
; i
++)
380 regulator_unregister(tps
->rdev
[i
]);
384 static const struct tps_info tps65020_regs
[] = {
401 .table_len
= ARRAY_SIZE(VCORE_VSEL_table
),
402 .table
= VCORE_VSEL_table
,
409 .table_len
= ARRAY_SIZE(TPS65020_LDO1_VSEL_table
),
410 .table
= TPS65020_LDO1_VSEL_table
,
416 .table_len
= ARRAY_SIZE(TPS65020_LDO2_VSEL_table
),
417 .table
= TPS65020_LDO2_VSEL_table
,
421 static const struct tps_info tps65021_regs
[] = {
438 .table_len
= ARRAY_SIZE(VCORE_VSEL_table
),
439 .table
= VCORE_VSEL_table
,
445 .table_len
= ARRAY_SIZE(TPS65023_LDO1_VSEL_table
),
446 .table
= TPS65023_LDO1_VSEL_table
,
452 .table_len
= ARRAY_SIZE(TPS65023_LDO2_VSEL_table
),
453 .table
= TPS65023_LDO2_VSEL_table
,
457 static const struct tps_info tps65023_regs
[] = {
462 .table_len
= ARRAY_SIZE(VCORE_VSEL_table
),
463 .table
= VCORE_VSEL_table
,
481 .table_len
= ARRAY_SIZE(TPS65023_LDO1_VSEL_table
),
482 .table
= TPS65023_LDO1_VSEL_table
,
488 .table_len
= ARRAY_SIZE(TPS65023_LDO2_VSEL_table
),
489 .table
= TPS65023_LDO2_VSEL_table
,
493 static struct tps_driver_data tps65020_drv_data
= {
494 .info
= tps65020_regs
,
495 .core_regulator
= TPS65023_DCDC_3
,
498 static struct tps_driver_data tps65021_drv_data
= {
499 .info
= tps65021_regs
,
500 .core_regulator
= TPS65023_DCDC_3
,
503 static struct tps_driver_data tps65023_drv_data
= {
504 .info
= tps65023_regs
,
505 .core_regulator
= TPS65023_DCDC_1
,
508 static const struct i2c_device_id tps_65023_id
[] = {
510 .driver_data
= (unsigned long) &tps65023_drv_data
},
512 .driver_data
= (unsigned long) &tps65021_drv_data
,},
514 .driver_data
= (unsigned long) &tps65020_drv_data
},
518 MODULE_DEVICE_TABLE(i2c
, tps_65023_id
);
520 static struct i2c_driver tps_65023_i2c_driver
= {
523 .owner
= THIS_MODULE
,
525 .probe
= tps_65023_probe
,
526 .remove
= __devexit_p(tps_65023_remove
),
527 .id_table
= tps_65023_id
,
530 static int __init
tps_65023_init(void)
532 return i2c_add_driver(&tps_65023_i2c_driver
);
534 subsys_initcall(tps_65023_init
);
536 static void __exit
tps_65023_cleanup(void)
538 i2c_del_driver(&tps_65023_i2c_driver
);
540 module_exit(tps_65023_cleanup
);
542 MODULE_AUTHOR("Texas Instruments");
543 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
544 MODULE_LICENSE("GPL v2");