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)};
107 struct tps6507x_pmic {
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 tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
117 return i2c_smbus_read_byte_data(tps->client, reg);
120 static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
122 return i2c_smbus_write_byte_data(tps->client, reg, val);
125 static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
129 mutex_lock(&tps->io_lock);
131 data = tps6507x_pmic_read(tps, reg);
133 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
139 err = tps6507x_pmic_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 tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
152 mutex_lock(&tps->io_lock);
154 data = tps6507x_pmic_read(tps, reg);
156 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
162 err = tps6507x_pmic_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 tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
175 mutex_lock(&tps->io_lock);
177 data = tps6507x_pmic_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 tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
189 mutex_lock(&tps->io_lock);
191 err = tps6507x_pmic_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_pmic_dcdc_is_enabled(struct regulator_dev *dev)
201 struct tps6507x_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 = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
214 return (data & 1<<shift) ? 1 : 0;
217 static int tps6507x_pmic_ldo_is_enabled(struct regulator_dev *dev)
219 struct tps6507x_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 = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
232 return (data & 1<<shift) ? 1 : 0;
235 static int tps6507x_pmic_dcdc_enable(struct regulator_dev *dev)
237 struct tps6507x_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 tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
248 static int tps6507x_pmic_dcdc_disable(struct regulator_dev *dev)
250 struct tps6507x_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 tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
262 static int tps6507x_pmic_ldo_enable(struct regulator_dev *dev)
264 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
265 int ldo = rdev_get_id(dev);
268 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
271 shift = TPS6507X_MAX_REG_ID - ldo;
272 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
275 static int tps6507x_pmic_ldo_disable(struct regulator_dev *dev)
277 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
278 int ldo = rdev_get_id(dev);
281 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
284 shift = TPS6507X_MAX_REG_ID - ldo;
285 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
289 static int tps6507x_pmic_dcdc_get_voltage(struct regulator_dev *dev)
291 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
292 int data, dcdc = rdev_get_id(dev);
296 case TPS6507X_DCDC_1:
297 reg = TPS6507X_REG_DEFDCDC1;
299 case TPS6507X_DCDC_2:
300 reg = TPS6507X_REG_DEFDCDC2_LOW;
302 case TPS6507X_DCDC_3:
303 reg = TPS6507X_REG_DEFDCDC3_LOW;
309 data = tps6507x_pmic_reg_read(tps, reg);
313 data &= TPS6507X_DEFDCDCX_DCDC_MASK;
314 return tps->info[dcdc]->table[data] * 1000;
317 static int tps6507x_pmic_dcdc_set_voltage(struct regulator_dev *dev,
318 int min_uV, int max_uV)
320 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
321 int data, vsel, dcdc = rdev_get_id(dev);
325 case TPS6507X_DCDC_1:
326 reg = TPS6507X_REG_DEFDCDC1;
328 case TPS6507X_DCDC_2:
329 reg = TPS6507X_REG_DEFDCDC2_LOW;
331 case TPS6507X_DCDC_3:
332 reg = TPS6507X_REG_DEFDCDC3_LOW;
338 if (min_uV < tps->info[dcdc]->min_uV
339 || min_uV > tps->info[dcdc]->max_uV)
341 if (max_uV < tps->info[dcdc]->min_uV
342 || max_uV > tps->info[dcdc]->max_uV)
345 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
346 int mV = tps->info[dcdc]->table[vsel];
349 /* Break at the first in-range value */
350 if (min_uV <= uV && uV <= max_uV)
354 /* write to the register in case we found a match */
355 if (vsel == tps->info[dcdc]->table_len)
358 data = tps6507x_pmic_reg_read(tps, reg);
362 data &= ~TPS6507X_DEFDCDCX_DCDC_MASK;
365 return tps6507x_pmic_reg_write(tps, reg, data);
368 static int tps6507x_pmic_ldo_get_voltage(struct regulator_dev *dev)
370 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
371 int data, ldo = rdev_get_id(dev);
374 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
377 reg = (ldo == TPS6507X_LDO_1 ?
378 TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
379 mask = (ldo == TPS6507X_LDO_1 ?
380 TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
381 TPS6507X_REG_DEFLDO2_LDO2_MASK);
384 data = tps6507x_pmic_reg_read(tps, reg);
389 return tps->info[ldo]->table[data] * 1000;
392 static int tps6507x_pmic_ldo_set_voltage(struct regulator_dev *dev,
393 int min_uV, int max_uV)
395 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
396 int data, vsel, ldo = rdev_get_id(dev);
399 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
402 reg = (ldo == TPS6507X_LDO_1 ?
403 TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
404 mask = (ldo == TPS6507X_LDO_1 ?
405 TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
406 TPS6507X_REG_DEFLDO2_LDO2_MASK);
409 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
411 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
414 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
415 int mV = tps->info[ldo]->table[vsel];
418 /* Break at the first in-range value */
419 if (min_uV <= uV && uV <= max_uV)
423 if (vsel == tps->info[ldo]->table_len)
426 data = tps6507x_pmic_reg_read(tps, reg);
433 return tps6507x_pmic_reg_write(tps, reg, data);
436 static int tps6507x_pmic_dcdc_list_voltage(struct regulator_dev *dev,
439 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
440 int dcdc = rdev_get_id(dev);
442 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
445 if (selector >= tps->info[dcdc]->table_len)
448 return tps->info[dcdc]->table[selector] * 1000;
451 static int tps6507x_pmic_ldo_list_voltage(struct regulator_dev *dev,
454 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
455 int ldo = rdev_get_id(dev);
457 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
460 if (selector >= tps->info[ldo]->table_len)
463 return tps->info[ldo]->table[selector] * 1000;
466 /* Operations permitted on VDCDCx */
467 static struct regulator_ops tps6507x_pmic_dcdc_ops = {
468 .is_enabled = tps6507x_pmic_dcdc_is_enabled,
469 .enable = tps6507x_pmic_dcdc_enable,
470 .disable = tps6507x_pmic_dcdc_disable,
471 .get_voltage = tps6507x_pmic_dcdc_get_voltage,
472 .set_voltage = tps6507x_pmic_dcdc_set_voltage,
473 .list_voltage = tps6507x_pmic_dcdc_list_voltage,
476 /* Operations permitted on LDOx */
477 static struct regulator_ops tps6507x_pmic_ldo_ops = {
478 .is_enabled = tps6507x_pmic_ldo_is_enabled,
479 .enable = tps6507x_pmic_ldo_enable,
480 .disable = tps6507x_pmic_ldo_disable,
481 .get_voltage = tps6507x_pmic_ldo_get_voltage,
482 .set_voltage = tps6507x_pmic_ldo_set_voltage,
483 .list_voltage = tps6507x_pmic_ldo_list_voltage,
486 static int __devinit tps6507x_pmic_probe(struct i2c_client *client,
487 const struct i2c_device_id *id)
490 const struct tps_info *info = (void *)id->driver_data;
491 struct regulator_init_data *init_data;
492 struct regulator_dev *rdev;
493 struct tps6507x_pmic *tps;
494 struct tps6507x_board *tps_board;
498 if (!i2c_check_functionality(client->adapter,
499 I2C_FUNC_SMBUS_BYTE_DATA))
503 * tps_board points to pmic related constants
504 * coming from the board-evm file.
507 tps_board = dev_get_platdata(&client->dev);
512 * init_data points to array of regulator_init structures
513 * coming from the board-evm file.
515 init_data = tps_board->tps6507x_pmic_init_data;
519 tps = kzalloc(sizeof(*tps), GFP_KERNEL);
523 mutex_init(&tps->io_lock);
525 /* common for all regulators */
526 tps->client = client;
528 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
529 /* Register the regulators */
531 tps->desc[i].name = info->name;
532 tps->desc[i].id = desc_id++;
533 tps->desc[i].n_voltages = num_voltages[i];
534 tps->desc[i].ops = (i > TPS6507X_DCDC_3 ?
535 &tps6507x_pmic_ldo_ops : &tps6507x_pmic_dcdc_ops);
536 tps->desc[i].type = REGULATOR_VOLTAGE;
537 tps->desc[i].owner = THIS_MODULE;
539 rdev = regulator_register(&tps->desc[i],
540 &client->dev, init_data, tps);
542 dev_err(&client->dev, "failed to register %s\n",
544 error = PTR_ERR(rdev);
548 /* Save regulator for cleanup */
552 i2c_set_clientdata(client, tps);
558 regulator_unregister(tps->rdev[i]);
565 * tps6507x_remove - TPS6507x driver i2c remove handler
566 * @client: i2c driver client device structure
568 * Unregister TPS driver as an i2c client device driver
570 static int __devexit tps6507x_pmic_remove(struct i2c_client *client)
572 struct tps6507x_pmic *tps = i2c_get_clientdata(client);
575 /* clear the client data in i2c */
576 i2c_set_clientdata(client, NULL);
578 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
579 regulator_unregister(tps->rdev[i]);
586 static const struct tps_info tps6507x_pmic_regs[] = {
591 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
592 .table = VDCDCx_VSEL_table,
598 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
599 .table = VDCDCx_VSEL_table,
605 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
606 .table = VDCDCx_VSEL_table,
612 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
613 .table = LDO1_VSEL_table,
619 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
620 .table = LDO2_VSEL_table,
624 static const struct i2c_device_id tps6507x_pmic_id[] = {
626 .driver_data = (unsigned long) tps6507x_pmic_regs,},
629 MODULE_DEVICE_TABLE(i2c, tps6507x_pmic_id);
631 static struct i2c_driver tps6507x_i2c_driver = {
634 .owner = THIS_MODULE,
636 .probe = tps6507x_pmic_probe,
637 .remove = __devexit_p(tps6507x_pmic_remove),
638 .id_table = tps6507x_pmic_id,
644 * Module init function
646 static int __init tps6507x_pmic_init(void)
648 return i2c_add_driver(&tps6507x_i2c_driver);
650 subsys_initcall(tps6507x_pmic_init);
653 * tps6507x_pmic_cleanup
655 * Module exit function
657 static void __exit tps6507x_pmic_cleanup(void)
659 i2c_del_driver(&tps6507x_i2c_driver);
661 module_exit(tps6507x_pmic_cleanup);
663 MODULE_AUTHOR("Texas Instruments");
664 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
665 MODULE_LICENSE("GPL v2");