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/i2c.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/mfd/tps6507x.h>
31 #define TPS6507X_DCDC_1 0
32 #define TPS6507X_DCDC_2 1
33 #define TPS6507X_DCDC_3 2
35 #define TPS6507X_LDO_1 3
36 #define TPS6507X_LDO_2 4
38 #define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
40 /* Number of step-down converters available */
41 #define TPS6507X_NUM_DCDC 3
42 /* Number of LDO voltage regulators available */
43 #define TPS6507X_NUM_LDO 2
44 /* Number of total regulators available */
45 #define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
47 /* Supported voltage values for regulators (in milliVolts) */
48 static const u16 VDCDCx_VSEL_table[] = {
52 1025, 1050, 1075, 1100,
53 1125, 1150, 1175, 1200,
54 1225, 1250, 1275, 1300,
55 1325, 1350, 1375, 1400,
56 1425, 1450, 1475, 1500,
57 1550, 1600, 1650, 1700,
58 1750, 1800, 1850, 1900,
59 1950, 2000, 2050, 2100,
60 2150, 2200, 2250, 2300,
61 2350, 2400, 2450, 2500,
62 2550, 2600, 2650, 2700,
63 2750, 2800, 2850, 2900,
64 3000, 3100, 3200, 3300,
67 static const u16 LDO1_VSEL_table[] = {
68 1000, 1100, 1200, 1250,
69 1300, 1350, 1400, 1500,
70 1600, 1800, 2500, 2750,
71 2800, 3000, 3100, 3300,
74 static const u16 LDO2_VSEL_table[] = {
78 1025, 1050, 1075, 1100,
79 1125, 1150, 1175, 1200,
80 1225, 1250, 1275, 1300,
81 1325, 1350, 1375, 1400,
82 1425, 1450, 1475, 1500,
83 1550, 1600, 1650, 1700,
84 1750, 1800, 1850, 1900,
85 1950, 2000, 2050, 2100,
86 2150, 2200, 2250, 2300,
87 2350, 2400, 2450, 2500,
88 2550, 2600, 2650, 2700,
89 2750, 2800, 2850, 2900,
90 3000, 3100, 3200, 3300,
93 static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDCx_VSEL_table),
94 ARRAY_SIZE(VDCDCx_VSEL_table),
95 ARRAY_SIZE(VDCDCx_VSEL_table),
96 ARRAY_SIZE(LDO1_VSEL_table),
97 ARRAY_SIZE(LDO2_VSEL_table)};
108 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
109 struct i2c_client *client;
110 struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
111 const struct tps_info *info[TPS6507X_NUM_REGULATOR];
112 struct mutex io_lock;
115 static inline int tps_6507x_read(struct tps_pmic *tps, u8 reg)
117 return i2c_smbus_read_byte_data(tps->client, reg);
120 static inline int tps_6507x_write(struct tps_pmic *tps, u8 reg, u8 val)
122 return i2c_smbus_write_byte_data(tps->client, reg, val);
125 static int tps_6507x_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
129 mutex_lock(&tps->io_lock);
131 data = tps_6507x_read(tps, reg);
133 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
139 err = tps_6507x_write(tps, reg, data);
141 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
144 mutex_unlock(&tps->io_lock);
148 static int tps_6507x_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
152 mutex_lock(&tps->io_lock);
154 data = tps_6507x_read(tps, reg);
156 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
162 err = tps_6507x_write(tps, reg, data);
164 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
167 mutex_unlock(&tps->io_lock);
171 static int tps_6507x_reg_read(struct tps_pmic *tps, u8 reg)
175 mutex_lock(&tps->io_lock);
177 data = tps_6507x_read(tps, reg);
179 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
181 mutex_unlock(&tps->io_lock);
185 static int tps_6507x_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
189 mutex_lock(&tps->io_lock);
191 err = tps_6507x_write(tps, reg, val);
193 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
195 mutex_unlock(&tps->io_lock);
199 static int tps6507x_dcdc_is_enabled(struct regulator_dev *dev)
201 struct tps_pmic *tps = rdev_get_drvdata(dev);
202 int data, dcdc = rdev_get_id(dev);
205 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
208 shift = TPS6507X_MAX_REG_ID - dcdc;
209 data = tps_6507x_reg_read(tps, TPS6507X_REG_CON_CTRL1);
214 return (data & 1<<shift) ? 1 : 0;
217 static int tps6507x_ldo_is_enabled(struct regulator_dev *dev)
219 struct tps_pmic *tps = rdev_get_drvdata(dev);
220 int data, ldo = rdev_get_id(dev);
223 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
226 shift = TPS6507X_MAX_REG_ID - ldo;
227 data = tps_6507x_reg_read(tps, TPS6507X_REG_CON_CTRL1);
232 return (data & 1<<shift) ? 1 : 0;
235 static int tps6507x_dcdc_enable(struct regulator_dev *dev)
237 struct tps_pmic *tps = rdev_get_drvdata(dev);
238 int dcdc = rdev_get_id(dev);
241 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
244 shift = TPS6507X_MAX_REG_ID - dcdc;
245 return tps_6507x_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
248 static int tps6507x_dcdc_disable(struct regulator_dev *dev)
250 struct tps_pmic *tps = rdev_get_drvdata(dev);
251 int dcdc = rdev_get_id(dev);
254 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
257 shift = TPS6507X_MAX_REG_ID - dcdc;
258 return tps_6507x_clear_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
261 static int tps6507x_ldo_enable(struct regulator_dev *dev)
263 struct tps_pmic *tps = rdev_get_drvdata(dev);
264 int ldo = rdev_get_id(dev);
267 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
270 shift = TPS6507X_MAX_REG_ID - ldo;
271 return tps_6507x_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
274 static int tps6507x_ldo_disable(struct regulator_dev *dev)
276 struct tps_pmic *tps = rdev_get_drvdata(dev);
277 int ldo = rdev_get_id(dev);
280 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
283 shift = TPS6507X_MAX_REG_ID - ldo;
284 return tps_6507x_clear_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
287 static int tps6507x_dcdc_get_voltage(struct regulator_dev *dev)
289 struct tps_pmic *tps = rdev_get_drvdata(dev);
290 int data, dcdc = rdev_get_id(dev);
294 case TPS6507X_DCDC_1:
295 reg = TPS6507X_REG_DEFDCDC1;
297 case TPS6507X_DCDC_2:
298 reg = TPS6507X_REG_DEFDCDC2_LOW;
300 case TPS6507X_DCDC_3:
301 reg = TPS6507X_REG_DEFDCDC3_LOW;
307 data = tps_6507x_reg_read(tps, reg);
311 data &= TPS6507X_DEFDCDCX_DCDC_MASK;
312 return tps->info[dcdc]->table[data] * 1000;
315 static int tps6507x_dcdc_set_voltage(struct regulator_dev *dev,
316 int min_uV, int max_uV)
318 struct tps_pmic *tps = rdev_get_drvdata(dev);
319 int data, vsel, dcdc = rdev_get_id(dev);
323 case TPS6507X_DCDC_1:
324 reg = TPS6507X_REG_DEFDCDC1;
326 case TPS6507X_DCDC_2:
327 reg = TPS6507X_REG_DEFDCDC2_LOW;
329 case TPS6507X_DCDC_3:
330 reg = TPS6507X_REG_DEFDCDC3_LOW;
336 if (min_uV < tps->info[dcdc]->min_uV
337 || min_uV > tps->info[dcdc]->max_uV)
339 if (max_uV < tps->info[dcdc]->min_uV
340 || max_uV > tps->info[dcdc]->max_uV)
343 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
344 int mV = tps->info[dcdc]->table[vsel];
347 /* Break at the first in-range value */
348 if (min_uV <= uV && uV <= max_uV)
352 /* write to the register in case we found a match */
353 if (vsel == tps->info[dcdc]->table_len)
356 data = tps_6507x_reg_read(tps, reg);
360 data &= ~TPS6507X_DEFDCDCX_DCDC_MASK;
363 return tps_6507x_reg_write(tps, reg, data);
366 static int tps6507x_ldo_get_voltage(struct regulator_dev *dev)
368 struct tps_pmic *tps = rdev_get_drvdata(dev);
369 int data, ldo = rdev_get_id(dev);
372 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
375 reg = (ldo == TPS6507X_LDO_1 ?
376 TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
377 mask = (ldo == TPS6507X_LDO_1 ?
378 TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
379 TPS6507X_REG_DEFLDO2_LDO2_MASK);
382 data = tps_6507x_reg_read(tps, reg);
387 return tps->info[ldo]->table[data] * 1000;
390 static int tps6507x_ldo_set_voltage(struct regulator_dev *dev,
391 int min_uV, int max_uV)
393 struct tps_pmic *tps = rdev_get_drvdata(dev);
394 int data, vsel, ldo = rdev_get_id(dev);
397 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
400 reg = (ldo == TPS6507X_LDO_1 ?
401 TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
402 mask = (ldo == TPS6507X_LDO_1 ?
403 TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
404 TPS6507X_REG_DEFLDO2_LDO2_MASK);
407 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
409 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
412 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
413 int mV = tps->info[ldo]->table[vsel];
416 /* Break at the first in-range value */
417 if (min_uV <= uV && uV <= max_uV)
421 if (vsel == tps->info[ldo]->table_len)
424 data = tps_6507x_reg_read(tps, reg);
431 return tps_6507x_reg_write(tps, reg, data);
434 static int tps6507x_dcdc_list_voltage(struct regulator_dev *dev,
437 struct tps_pmic *tps = rdev_get_drvdata(dev);
438 int dcdc = rdev_get_id(dev);
440 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
443 if (selector >= tps->info[dcdc]->table_len)
446 return tps->info[dcdc]->table[selector] * 1000;
449 static int tps6507x_ldo_list_voltage(struct regulator_dev *dev,
452 struct tps_pmic *tps = rdev_get_drvdata(dev);
453 int ldo = rdev_get_id(dev);
455 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
458 if (selector >= tps->info[ldo]->table_len)
461 return tps->info[ldo]->table[selector] * 1000;
464 /* Operations permitted on VDCDCx */
465 static struct regulator_ops tps6507x_dcdc_ops = {
466 .is_enabled = tps6507x_dcdc_is_enabled,
467 .enable = tps6507x_dcdc_enable,
468 .disable = tps6507x_dcdc_disable,
469 .get_voltage = tps6507x_dcdc_get_voltage,
470 .set_voltage = tps6507x_dcdc_set_voltage,
471 .list_voltage = tps6507x_dcdc_list_voltage,
474 /* Operations permitted on LDOx */
475 static struct regulator_ops tps6507x_ldo_ops = {
476 .is_enabled = tps6507x_ldo_is_enabled,
477 .enable = tps6507x_ldo_enable,
478 .disable = tps6507x_ldo_disable,
479 .get_voltage = tps6507x_ldo_get_voltage,
480 .set_voltage = tps6507x_ldo_set_voltage,
481 .list_voltage = tps6507x_ldo_list_voltage,
484 static int __devinit tps_6507x_probe(struct i2c_client *client,
485 const struct i2c_device_id *id)
488 const struct tps_info *info = (void *)id->driver_data;
489 struct regulator_init_data *init_data;
490 struct regulator_dev *rdev;
491 struct tps_pmic *tps;
495 if (!i2c_check_functionality(client->adapter,
496 I2C_FUNC_SMBUS_BYTE_DATA))
500 * init_data points to array of regulator_init structures
501 * coming from the board-evm file.
503 init_data = client->dev.platform_data;
507 tps = kzalloc(sizeof(*tps), GFP_KERNEL);
511 mutex_init(&tps->io_lock);
513 /* common for all regulators */
514 tps->client = client;
516 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
517 /* Register the regulators */
519 tps->desc[i].name = info->name;
520 tps->desc[i].id = desc_id++;
521 tps->desc[i].n_voltages = num_voltages[i];
522 tps->desc[i].ops = (i > TPS6507X_DCDC_3 ?
523 &tps6507x_ldo_ops : &tps6507x_dcdc_ops);
524 tps->desc[i].type = REGULATOR_VOLTAGE;
525 tps->desc[i].owner = THIS_MODULE;
527 rdev = regulator_register(&tps->desc[i],
528 &client->dev, init_data, tps);
530 dev_err(&client->dev, "failed to register %s\n",
532 error = PTR_ERR(rdev);
536 /* Save regulator for cleanup */
540 i2c_set_clientdata(client, tps);
546 regulator_unregister(tps->rdev[i]);
553 * tps_6507x_remove - TPS6507x driver i2c remove handler
554 * @client: i2c driver client device structure
556 * Unregister TPS driver as an i2c client device driver
558 static int __devexit tps_6507x_remove(struct i2c_client *client)
560 struct tps_pmic *tps = i2c_get_clientdata(client);
563 /* clear the client data in i2c */
564 i2c_set_clientdata(client, NULL);
566 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
567 regulator_unregister(tps->rdev[i]);
574 static const struct tps_info tps6507x_regs[] = {
579 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
580 .table = VDCDCx_VSEL_table,
586 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
587 .table = VDCDCx_VSEL_table,
593 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
594 .table = VDCDCx_VSEL_table,
600 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
601 .table = LDO1_VSEL_table,
607 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
608 .table = LDO2_VSEL_table,
612 static const struct i2c_device_id tps_6507x_id[] = {
614 .driver_data = (unsigned long) tps6507x_regs,},
617 MODULE_DEVICE_TABLE(i2c, tps_6507x_id);
619 static struct i2c_driver tps_6507x_i2c_driver = {
622 .owner = THIS_MODULE,
624 .probe = tps_6507x_probe,
625 .remove = __devexit_p(tps_6507x_remove),
626 .id_table = tps_6507x_id,
632 * Module init function
634 static int __init tps_6507x_init(void)
636 return i2c_add_driver(&tps_6507x_i2c_driver);
638 subsys_initcall(tps_6507x_init);
643 * Module exit function
645 static void __exit tps_6507x_cleanup(void)
647 i2c_del_driver(&tps_6507x_i2c_driver);
649 module_exit(tps_6507x_cleanup);
651 MODULE_AUTHOR("Texas Instruments");
652 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
653 MODULE_LICENSE("GPL v2");