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>
28 /* Register definitions */
29 #define TPS65023_REG_VERSION 0
30 #define TPS65023_REG_PGOODZ 1
31 #define TPS65023_REG_MASK 2
32 #define TPS65023_REG_REG_CTRL 3
33 #define TPS65023_REG_CON_CTRL 4
34 #define TPS65023_REG_CON_CTRL2 5
35 #define TPS65023_REG_DEF_CORE 6
36 #define TPS65023_REG_DEFSLEW 7
37 #define TPS65023_REG_LDO_CTRL 8
39 /* PGOODZ bitfields */
40 #define TPS65023_PGOODZ_PWRFAILZ BIT(7)
41 #define TPS65023_PGOODZ_LOWBATTZ BIT(6)
42 #define TPS65023_PGOODZ_VDCDC1 BIT(5)
43 #define TPS65023_PGOODZ_VDCDC2 BIT(4)
44 #define TPS65023_PGOODZ_VDCDC3 BIT(3)
45 #define TPS65023_PGOODZ_LDO2 BIT(2)
46 #define TPS65023_PGOODZ_LDO1 BIT(1)
49 #define TPS65023_MASK_PWRFAILZ BIT(7)
50 #define TPS65023_MASK_LOWBATTZ BIT(6)
51 #define TPS65023_MASK_VDCDC1 BIT(5)
52 #define TPS65023_MASK_VDCDC2 BIT(4)
53 #define TPS65023_MASK_VDCDC3 BIT(3)
54 #define TPS65023_MASK_LDO2 BIT(2)
55 #define TPS65023_MASK_LDO1 BIT(1)
57 /* REG_CTRL bitfields */
58 #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
59 #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
60 #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
61 #define TPS65023_REG_CTRL_LDO2_EN BIT(2)
62 #define TPS65023_REG_CTRL_LDO1_EN BIT(1)
64 /* LDO_CTRL bitfields */
65 #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
66 #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0xF0 >> ((ldo_id)*4))
68 /* Number of step-down converters available */
69 #define TPS65023_NUM_DCDC 3
70 /* Number of LDO voltage regulators available */
71 #define TPS65023_NUM_LDO 2
72 /* Number of total regulators available */
73 #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
76 #define TPS65023_DCDC_1 0
77 #define TPS65023_DCDC_2 1
78 #define TPS65023_DCDC_3 2
80 #define TPS65023_LDO_1 3
81 #define TPS65023_LDO_2 4
83 #define TPS65023_MAX_REG_ID TPS65023_LDO_2
85 /* Supported voltage values for regulators */
86 static const u16 VDCDC1_VSEL_table[] = {
89 1000, 1025, 1050, 1075,
90 1100, 1125, 1150, 1175,
91 1200, 1225, 1250, 1275,
92 1300, 1325, 1350, 1375,
93 1400, 1425, 1450, 1475,
94 1500, 1525, 1550, 1600,
97 static const u16 LDO1_VSEL_table[] = {
98 1000, 1100, 1300, 1800,
99 2200, 2600, 2800, 3150,
102 static const u16 LDO2_VSEL_table[] = {
103 1050, 1200, 1300, 1800,
104 2500, 2800, 3000, 3300,
107 static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDC1_VSEL_table),
108 0, 0, ARRAY_SIZE(LDO1_VSEL_table),
109 ARRAY_SIZE(LDO2_VSEL_table)};
111 /* Regulator specific details */
123 struct regulator_desc desc[TPS65023_NUM_REGULATOR];
124 struct i2c_client *client;
125 struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
126 const struct tps_info *info[TPS65023_NUM_REGULATOR];
127 struct mutex io_lock;
130 static inline int tps_65023_read(struct tps_pmic *tps, u8 reg)
132 return i2c_smbus_read_byte_data(tps->client, reg);
135 static inline int tps_65023_write(struct tps_pmic *tps, u8 reg, u8 val)
137 return i2c_smbus_write_byte_data(tps->client, reg, val);
140 static int tps_65023_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
144 mutex_lock(&tps->io_lock);
146 data = tps_65023_read(tps, reg);
148 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
154 err = tps_65023_write(tps, reg, data);
156 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
159 mutex_unlock(&tps->io_lock);
163 static int tps_65023_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
167 mutex_lock(&tps->io_lock);
169 data = tps_65023_read(tps, reg);
171 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
178 err = tps_65023_write(tps, reg, data);
180 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
183 mutex_unlock(&tps->io_lock);
188 static int tps_65023_reg_read(struct tps_pmic *tps, u8 reg)
192 mutex_lock(&tps->io_lock);
194 data = tps_65023_read(tps, reg);
196 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
198 mutex_unlock(&tps->io_lock);
202 static int tps_65023_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
206 mutex_lock(&tps->io_lock);
208 err = tps_65023_write(tps, reg, val);
210 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
212 mutex_unlock(&tps->io_lock);
216 static int tps65023_dcdc_is_enabled(struct regulator_dev *dev)
218 struct tps_pmic *tps = rdev_get_drvdata(dev);
219 int data, dcdc = rdev_get_id(dev);
222 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
225 shift = TPS65023_NUM_REGULATOR - dcdc;
226 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
231 return (data & 1<<shift) ? 1 : 0;
234 static int tps65023_ldo_is_enabled(struct regulator_dev *dev)
236 struct tps_pmic *tps = rdev_get_drvdata(dev);
237 int data, ldo = rdev_get_id(dev);
240 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
243 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
244 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
249 return (data & 1<<shift) ? 1 : 0;
252 static int tps65023_dcdc_enable(struct regulator_dev *dev)
254 struct tps_pmic *tps = rdev_get_drvdata(dev);
255 int dcdc = rdev_get_id(dev);
258 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
261 shift = TPS65023_NUM_REGULATOR - dcdc;
262 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
265 static int tps65023_dcdc_disable(struct regulator_dev *dev)
267 struct tps_pmic *tps = rdev_get_drvdata(dev);
268 int dcdc = rdev_get_id(dev);
271 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
274 shift = TPS65023_NUM_REGULATOR - dcdc;
275 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
278 static int tps65023_ldo_enable(struct regulator_dev *dev)
280 struct tps_pmic *tps = rdev_get_drvdata(dev);
281 int ldo = rdev_get_id(dev);
284 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
287 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
288 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
291 static int tps65023_ldo_disable(struct regulator_dev *dev)
293 struct tps_pmic *tps = rdev_get_drvdata(dev);
294 int ldo = rdev_get_id(dev);
297 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
300 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
301 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
304 static int tps65023_dcdc_get_voltage(struct regulator_dev *dev)
306 struct tps_pmic *tps = rdev_get_drvdata(dev);
307 int data, dcdc = rdev_get_id(dev);
309 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
312 if (dcdc == TPS65023_DCDC_1) {
313 data = tps_65023_reg_read(tps, TPS65023_REG_DEF_CORE);
316 data &= (tps->info[dcdc]->table_len - 1);
317 return tps->info[dcdc]->table[data] * 1000;
319 return tps->info[dcdc]->min_uV;
322 static int tps65023_dcdc_set_voltage(struct regulator_dev *dev,
323 int min_uV, int max_uV)
325 struct tps_pmic *tps = rdev_get_drvdata(dev);
326 int dcdc = rdev_get_id(dev);
329 if (dcdc != TPS65023_DCDC_1)
332 if (min_uV < tps->info[dcdc]->min_uV
333 || min_uV > tps->info[dcdc]->max_uV)
335 if (max_uV < tps->info[dcdc]->min_uV
336 || max_uV > tps->info[dcdc]->max_uV)
339 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
340 int mV = tps->info[dcdc]->table[vsel];
343 /* Break at the first in-range value */
344 if (min_uV <= uV && uV <= max_uV)
348 /* write to the register in case we found a match */
349 if (vsel == tps->info[dcdc]->table_len)
352 return tps_65023_reg_write(tps, TPS65023_REG_DEF_CORE, vsel);
355 static int tps65023_ldo_get_voltage(struct regulator_dev *dev)
357 struct tps_pmic *tps = rdev_get_drvdata(dev);
358 int data, ldo = rdev_get_id(dev);
360 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
363 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
367 data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
368 data &= (tps->info[ldo]->table_len - 1);
369 return tps->info[ldo]->table[data] * 1000;
372 static int tps65023_ldo_set_voltage(struct regulator_dev *dev,
373 int min_uV, int max_uV)
375 struct tps_pmic *tps = rdev_get_drvdata(dev);
376 int data, vsel, ldo = rdev_get_id(dev);
378 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
381 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
383 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
386 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
387 int mV = tps->info[ldo]->table[vsel];
390 /* Break at the first in-range value */
391 if (min_uV <= uV && uV <= max_uV)
395 if (vsel == tps->info[ldo]->table_len)
398 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
402 data &= TPS65023_LDO_CTRL_LDOx_MASK(ldo - TPS65023_LDO_1);
403 data |= (vsel << (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1)));
404 return tps_65023_reg_write(tps, TPS65023_REG_LDO_CTRL, data);
407 static int tps65023_dcdc_list_voltage(struct regulator_dev *dev,
410 struct tps_pmic *tps = rdev_get_drvdata(dev);
411 int dcdc = rdev_get_id(dev);
413 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
416 if (dcdc == TPS65023_DCDC_1) {
417 if (selector >= tps->info[dcdc]->table_len)
420 return tps->info[dcdc]->table[selector] * 1000;
422 return tps->info[dcdc]->min_uV;
425 static int tps65023_ldo_list_voltage(struct regulator_dev *dev,
428 struct tps_pmic *tps = rdev_get_drvdata(dev);
429 int ldo = rdev_get_id(dev);
431 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
434 if (selector >= tps->info[ldo]->table_len)
437 return tps->info[ldo]->table[selector] * 1000;
440 /* Operations permitted on VDCDCx */
441 static struct regulator_ops tps65023_dcdc_ops = {
442 .is_enabled = tps65023_dcdc_is_enabled,
443 .enable = tps65023_dcdc_enable,
444 .disable = tps65023_dcdc_disable,
445 .get_voltage = tps65023_dcdc_get_voltage,
446 .set_voltage = tps65023_dcdc_set_voltage,
447 .list_voltage = tps65023_dcdc_list_voltage,
450 /* Operations permitted on LDOx */
451 static struct regulator_ops tps65023_ldo_ops = {
452 .is_enabled = tps65023_ldo_is_enabled,
453 .enable = tps65023_ldo_enable,
454 .disable = tps65023_ldo_disable,
455 .get_voltage = tps65023_ldo_get_voltage,
456 .set_voltage = tps65023_ldo_set_voltage,
457 .list_voltage = tps65023_ldo_list_voltage,
461 int tps_65023_probe(struct i2c_client *client, const struct i2c_device_id *id)
464 const struct tps_info *info = (void *)id->driver_data;
465 struct regulator_init_data *init_data;
466 struct regulator_dev *rdev;
467 struct tps_pmic *tps;
470 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
474 * init_data points to array of regulator_init structures
475 * coming from the board-evm file.
477 init_data = client->dev.platform_data;
482 tps = kzalloc(sizeof(*tps), GFP_KERNEL);
486 mutex_init(&tps->io_lock);
488 /* common for all regulators */
489 tps->client = client;
491 for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
492 /* Store regulator specific information */
495 tps->desc[i].name = info->name;
496 tps->desc[i].id = desc_id++;
497 tps->desc[i].n_voltages = num_voltages[i];
498 tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
499 &tps65023_ldo_ops : &tps65023_dcdc_ops);
500 tps->desc[i].type = REGULATOR_VOLTAGE;
501 tps->desc[i].owner = THIS_MODULE;
503 /* Register the regulators */
504 rdev = regulator_register(&tps->desc[i], &client->dev,
507 dev_err(&client->dev, "failed to register %s\n",
512 regulator_unregister(tps->rdev[--i]);
516 /* clear the client data in i2c */
517 i2c_set_clientdata(client, NULL);
519 return PTR_ERR(rdev);
522 /* Save regulator for cleanup */
526 i2c_set_clientdata(client, tps);
532 * tps_65023_remove - TPS65023 driver i2c remove handler
533 * @client: i2c driver client device structure
535 * Unregister TPS driver as an i2c client device driver
537 static int __devexit tps_65023_remove(struct i2c_client *client)
539 struct tps_pmic *tps = i2c_get_clientdata(client);
542 for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
543 regulator_unregister(tps->rdev[i]);
547 /* clear the client data in i2c */
548 i2c_set_clientdata(client, NULL);
554 static const struct tps_info tps65023_regs[] = {
559 .table_len = ARRAY_SIZE(VDCDC1_VSEL_table),
560 .table = VDCDC1_VSEL_table,
578 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
579 .table = LDO1_VSEL_table,
585 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
586 .table = LDO2_VSEL_table,
590 static const struct i2c_device_id tps_65023_id[] = {
592 .driver_data = (unsigned long) tps65023_regs,},
596 MODULE_DEVICE_TABLE(i2c, tps_65023_id);
598 static struct i2c_driver tps_65023_i2c_driver = {
601 .owner = THIS_MODULE,
603 .probe = tps_65023_probe,
604 .remove = __devexit_p(tps_65023_remove),
605 .id_table = tps_65023_id,
611 * Module init function
613 static int __init tps_65023_init(void)
615 return i2c_add_driver(&tps_65023_i2c_driver);
617 subsys_initcall(tps_65023_init);
622 * Module exit function
624 static void __exit tps_65023_cleanup(void)
626 i2c_del_driver(&tps_65023_i2c_driver);
628 module_exit(tps_65023_cleanup);
630 MODULE_AUTHOR("Texas Instruments");
631 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
632 MODULE_LICENSE("GPL v2");