2 * tps65910.c -- TI tps65910
4 * Copyright 2010 Texas Instruments Inc.
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/gpio.h>
26 #include <linux/mfd/tps65910.h>
28 #define TPS65910_REG_VRTC 0
29 #define TPS65910_REG_VIO 1
30 #define TPS65910_REG_VDD1 2
31 #define TPS65910_REG_VDD2 3
32 #define TPS65910_REG_VDD3 4
33 #define TPS65910_REG_VDIG1 5
34 #define TPS65910_REG_VDIG2 6
35 #define TPS65910_REG_VPLL 7
36 #define TPS65910_REG_VDAC 8
37 #define TPS65910_REG_VAUX1 9
38 #define TPS65910_REG_VAUX2 10
39 #define TPS65910_REG_VAUX33 11
40 #define TPS65910_REG_VMMC 12
42 #define TPS65910_NUM_REGULATOR 13
44 #define TPS65910_SUPPLY_STATE_ENABLED 0x1
46 /* supported VIO voltages in milivolts */
47 static const u16 VIO_VSEL_table[] = {
48 1500, 1800, 2500, 3300,
51 /* supported VIO voltages in milivolts */
52 static const u16 VDD3_VSEL_table[] = {
56 /* supported VDIG1 voltages in milivolts */
57 static const u16 VDIG1_VSEL_table[] = {
58 1200, 1500, 1800, 2700,
61 /* supported VDIG2 voltages in milivolts */
62 static const u16 VDIG2_VSEL_table[] = {
63 1000, 1100, 1200, 1800,
66 /* supported VPLL voltages in milivolts */
67 static const u16 VPLL_VSEL_table[] = {
68 1000, 1100, 1800, 2500,
71 /* supported VDAC voltages in milivolts */
72 static const u16 VDAC_VSEL_table[] = {
73 1800, 2600, 2800, 2850,
76 /* supported VAUX1 voltages in milivolts */
77 static const u16 VAUX1_VSEL_table[] = {
78 1800, 2500, 2800, 2850,
81 /* supported VAUX2 voltages in milivolts */
82 static const u16 VAUX2_VSEL_table[] = {
83 1800, 2800, 2900, 3300,
86 /* supported VAUX33 voltages in milivolts */
87 static const u16 VAUX33_VSEL_table[] = {
88 1800, 2000, 2800, 3300,
91 /* supported VMMC voltages in milivolts */
92 static const u16 VMMC_VSEL_table[] = {
93 1800, 2800, 3000, 3300,
104 static struct tps_info tps65910_regs[] = {
112 .table_len = ARRAY_SIZE(VIO_VSEL_table),
113 .table = VIO_VSEL_table,
129 .table_len = ARRAY_SIZE(VDD3_VSEL_table),
130 .table = VDD3_VSEL_table,
136 .table_len = ARRAY_SIZE(VDIG1_VSEL_table),
137 .table = VDIG1_VSEL_table,
143 .table_len = ARRAY_SIZE(VDIG2_VSEL_table),
144 .table = VDIG2_VSEL_table,
150 .table_len = ARRAY_SIZE(VPLL_VSEL_table),
151 .table = VPLL_VSEL_table,
157 .table_len = ARRAY_SIZE(VDAC_VSEL_table),
158 .table = VDAC_VSEL_table,
164 .table_len = ARRAY_SIZE(VAUX1_VSEL_table),
165 .table = VAUX1_VSEL_table,
171 .table_len = ARRAY_SIZE(VAUX2_VSEL_table),
172 .table = VAUX2_VSEL_table,
178 .table_len = ARRAY_SIZE(VAUX33_VSEL_table),
179 .table = VAUX33_VSEL_table,
185 .table_len = ARRAY_SIZE(VMMC_VSEL_table),
186 .table = VMMC_VSEL_table,
190 struct tps65910_reg {
191 struct regulator_desc desc[TPS65910_NUM_REGULATOR];
192 struct tps65910 *mfd;
193 struct regulator_dev *rdev[TPS65910_NUM_REGULATOR];
194 struct tps_info *info[TPS65910_NUM_REGULATOR];
199 static inline int tps65910_read(struct tps65910_reg *pmic, u8 reg)
204 err = pmic->mfd->read(pmic->mfd, reg, 1, &val);
211 static inline int tps65910_write(struct tps65910_reg *pmic, u8 reg, u8 val)
213 return pmic->mfd->write(pmic->mfd, reg, 1, &val);
216 static int tps65910_modify_bits(struct tps65910_reg *pmic, u8 reg,
217 u8 set_mask, u8 clear_mask)
221 mutex_lock(&pmic->mutex);
223 data = tps65910_read(pmic, reg);
225 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
232 err = tps65910_write(pmic, reg, data);
234 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
237 mutex_unlock(&pmic->mutex);
241 static int tps65910_reg_read(struct tps65910_reg *pmic, u8 reg)
245 mutex_lock(&pmic->mutex);
247 data = tps65910_read(pmic, reg);
249 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
251 mutex_unlock(&pmic->mutex);
255 static int tps65910_reg_write(struct tps65910_reg *pmic, u8 reg, u8 val)
259 mutex_lock(&pmic->mutex);
261 err = tps65910_write(pmic, reg, val);
263 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
265 mutex_unlock(&pmic->mutex);
269 static int tps65910_get_ctrl_register(int id)
272 case TPS65910_REG_VRTC:
273 return TPS65910_VRTC;
274 case TPS65910_REG_VIO:
276 case TPS65910_REG_VDD1:
277 return TPS65910_VDD1;
278 case TPS65910_REG_VDD2:
279 return TPS65910_VDD2;
280 case TPS65910_REG_VDD3:
281 return TPS65910_VDD3;
282 case TPS65910_REG_VDIG1:
283 return TPS65910_VDIG1;
284 case TPS65910_REG_VDIG2:
285 return TPS65910_VDIG2;
286 case TPS65910_REG_VPLL:
287 return TPS65910_VPLL;
288 case TPS65910_REG_VDAC:
289 return TPS65910_VDAC;
290 case TPS65910_REG_VAUX1:
291 return TPS65910_VAUX1;
292 case TPS65910_REG_VAUX2:
293 return TPS65910_VAUX2;
294 case TPS65910_REG_VAUX33:
295 return TPS65910_VAUX33;
296 case TPS65910_REG_VMMC:
297 return TPS65910_VMMC;
303 static int tps65910_is_enabled(struct regulator_dev *dev)
305 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
306 int reg, value, id = rdev_get_id(dev);
308 reg = tps65910_get_ctrl_register(id);
312 value = tps65910_reg_read(pmic, reg);
316 return value & TPS65910_SUPPLY_STATE_ENABLED;
319 static int tps65910_enable(struct regulator_dev *dev)
321 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
322 struct tps65910 *mfd = pmic->mfd;
323 int reg, id = rdev_get_id(dev);
325 reg = tps65910_get_ctrl_register(id);
329 return tps65910_set_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
332 static int tps65910_disable(struct regulator_dev *dev)
334 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
335 struct tps65910 *mfd = pmic->mfd;
336 int reg, id = rdev_get_id(dev);
338 reg = tps65910_get_ctrl_register(id);
342 return tps65910_clear_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
346 static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
348 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
349 struct tps65910 *mfd = pmic->mfd;
350 int reg, value, id = rdev_get_id(dev);
351 reg = tps65910_get_ctrl_register(id);
356 case REGULATOR_MODE_NORMAL:
357 return tps65910_modify_bits(pmic, reg, LDO_ST_ON_BIT,
359 case REGULATOR_MODE_IDLE:
360 value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
361 return tps65910_set_bits(mfd, reg, value);
362 case REGULATOR_MODE_STANDBY:
363 return tps65910_clear_bits(mfd, reg, LDO_ST_ON_BIT);
369 static unsigned int tps65910_get_mode(struct regulator_dev *dev)
371 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
372 int reg, value, id = rdev_get_id(dev);
374 reg = tps65910_get_ctrl_register(id);
378 value = tps65910_reg_read(pmic, reg);
382 if (value & LDO_ST_ON_BIT)
383 return REGULATOR_MODE_STANDBY;
384 else if (value & LDO_ST_MODE_BIT)
385 return REGULATOR_MODE_IDLE;
387 return REGULATOR_MODE_NORMAL;
390 static int tps65910_get_voltage_dcdc(struct regulator_dev *dev)
392 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
393 int id = rdev_get_id(dev), voltage = 0;
394 int opvsel = 0, srvsel = 0, mult = 0, sr = 0;
397 case TPS65910_REG_VDD1:
398 opvsel = tps65910_reg_read(pmic, TPS65910_VDD1_OP);
399 mult = tps65910_reg_read(pmic, TPS65910_VDD1);
400 mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
401 srvsel = tps65910_reg_read(pmic, TPS65910_VDD1_SR);
402 sr = opvsel & VDD1_OP_CMD_MASK;
403 opvsel &= VDD1_OP_SEL_MASK;
404 srvsel &= VDD1_SR_SEL_MASK;
406 case TPS65910_REG_VDD2:
407 opvsel = tps65910_reg_read(pmic, TPS65910_VDD2_OP);
408 mult = tps65910_reg_read(pmic, TPS65910_VDD2);
409 mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
410 srvsel = tps65910_reg_read(pmic, TPS65910_VDD2_SR);
411 sr = opvsel & VDD2_OP_CMD_MASK;
412 opvsel &= VDD2_OP_SEL_MASK;
413 srvsel &= VDD2_SR_SEL_MASK;
417 /* multiplier 0 == 1 but 2,3 normal */
422 /* Valid range is 3-75 so normalise */
423 if (srvsel < 3) srvsel = 3;
424 if (srvsel > 75) srvsel = 75;
427 voltage = (srvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
430 /* Valid range is 3-75 so normalise */
431 if (opvsel < 3) opvsel = 3;
432 if (opvsel > 75) opvsel = 75;
435 voltage = (opvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
443 static int tps65910_get_voltage(struct regulator_dev *dev)
445 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
446 int reg, value, id = rdev_get_id(dev), voltage = 0;
448 reg = tps65910_get_ctrl_register(id);
452 value = tps65910_reg_read(pmic, reg);
457 case TPS65910_REG_VIO:
458 case TPS65910_REG_VDIG1:
459 case TPS65910_REG_VDIG2:
460 case TPS65910_REG_VPLL:
461 case TPS65910_REG_VDAC:
462 case TPS65910_REG_VAUX1:
463 case TPS65910_REG_VAUX2:
464 case TPS65910_REG_VAUX33:
465 case TPS65910_REG_VMMC:
466 value &= LDO_SEL_MASK;
467 value >>= LDO_SEL_SHIFT;
473 voltage = pmic->info[id]->table[value] * 1000;
478 static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
480 return 5 * 1000 * 1000;
483 static int tps65910_set_voltage_dcdc(struct regulator_dev *dev,
486 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
487 int id = rdev_get_id(dev), vsel;
490 /* Split vsel into appropriate registers */
491 dcdc_mult = (selector / VDD1_2_NUM_VOLTS) + 1;
492 if (dcdc_mult == 1) dcdc_mult--;
494 vsel = (selector % VDD1_2_NUM_VOLTS) + 3;
496 if (id == TPS65910_REG_VDD1) {
497 tps65910_modify_bits(pmic, TPS65910_VDD1,
498 (dcdc_mult << VDD1_VGAIN_SEL_SHIFT),
499 VDD1_VGAIN_SEL_MASK);
500 tps65910_reg_write(pmic, TPS65910_VDD1_OP, vsel);
502 tps65910_modify_bits(pmic, TPS65910_VDD2,
503 (dcdc_mult << VDD2_VGAIN_SEL_SHIFT),
504 VDD1_VGAIN_SEL_MASK);
505 tps65910_reg_write(pmic, TPS65910_VDD2_OP, vsel);
511 static int tps65910_set_voltage(struct regulator_dev *dev, unsigned selector)
513 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
514 int reg, id = rdev_get_id(dev);
516 reg = tps65910_get_ctrl_register(id);
521 case TPS65910_REG_VIO:
522 case TPS65910_REG_VDIG1:
523 case TPS65910_REG_VDIG2:
524 case TPS65910_REG_VPLL:
525 case TPS65910_REG_VDAC:
526 case TPS65910_REG_VAUX1:
527 case TPS65910_REG_VAUX2:
528 case TPS65910_REG_VAUX33:
529 case TPS65910_REG_VMMC:
530 return tps65910_modify_bits(pmic, reg,
531 (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
537 static int tps65910_list_voltage_dcdc(struct regulator_dev *dev,
542 mult = (selector / VDD1_2_NUM_VOLTS) + 1;
544 volt = VDD1_2_MIN_VOLT + (selector % VDD1_2_NUM_VOLTS) * VDD1_2_OFFSET;
546 return volt * 100 * mult;
549 static int tps65910_list_voltage(struct regulator_dev *dev,
552 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
553 int id = rdev_get_id(dev), voltage;
555 if (id < TPS65910_REG_VIO || id > TPS65910_REG_VMMC)
558 if (selector >= pmic->info[id]->table_len)
561 voltage = pmic->info[id]->table[selector] * 1000;
566 /* Regulator ops (except VRTC) */
567 static struct regulator_ops tps65910_ops_dcdc = {
568 .is_enabled = tps65910_is_enabled,
569 .enable = tps65910_enable,
570 .disable = tps65910_disable,
571 .set_mode = tps65910_set_mode,
572 .get_mode = tps65910_get_mode,
573 .get_voltage = tps65910_get_voltage_dcdc,
574 .set_voltage_sel = tps65910_set_voltage_dcdc,
575 .list_voltage = tps65910_list_voltage_dcdc,
578 static struct regulator_ops tps65910_ops_vdd3 = {
579 .is_enabled = tps65910_is_enabled,
580 .enable = tps65910_enable,
581 .disable = tps65910_disable,
582 .set_mode = tps65910_set_mode,
583 .get_mode = tps65910_get_mode,
584 .get_voltage = tps65910_get_voltage_vdd3,
585 .list_voltage = tps65910_list_voltage,
588 static struct regulator_ops tps65910_ops = {
589 .is_enabled = tps65910_is_enabled,
590 .enable = tps65910_enable,
591 .disable = tps65910_disable,
592 .set_mode = tps65910_set_mode,
593 .get_mode = tps65910_get_mode,
594 .get_voltage = tps65910_get_voltage,
595 .set_voltage_sel = tps65910_set_voltage,
596 .list_voltage = tps65910_list_voltage,
599 static __devinit int tps65910_probe(struct platform_device *pdev)
601 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
602 struct tps_info *info = tps65910_regs;
603 struct regulator_init_data *reg_data;
604 struct regulator_dev *rdev;
605 struct tps65910_reg *pmic;
606 struct tps65910_board *pmic_plat_data;
609 pmic_plat_data = dev_get_platdata(tps65910->dev);
613 reg_data = pmic_plat_data->tps65910_pmic_init_data;
615 pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
619 mutex_init(&pmic->mutex);
620 pmic->mfd = tps65910;
621 platform_set_drvdata(pdev, pmic);
623 /* Give control of all register to control port */
624 tps65910_set_bits(pmic->mfd, TPS65910_DEVCTRL,
625 DEVCTRL_SR_CTL_I2C_SEL_MASK);
627 for (i = 0; i < TPS65910_NUM_REGULATOR; i++, info++, reg_data++) {
628 /* Register the regulators */
629 pmic->info[i] = info;
631 pmic->desc[i].name = info->name;
632 pmic->desc[i].id = i;
633 pmic->desc[i].n_voltages = info->table_len;
635 if ((i == TPS65910_REG_VDD1) || (i == TPS65910_REG_VDD2))
636 pmic->desc[i].ops = &tps65910_ops_dcdc;
637 else if (i == TPS65910_REG_VDD3)
638 pmic->desc[i].ops = &tps65910_ops_vdd3;
640 pmic->desc[i].ops = &tps65910_ops;
642 pmic->desc[i].type = REGULATOR_VOLTAGE;
643 pmic->desc[i].owner = THIS_MODULE;
645 rdev = regulator_register(&pmic->desc[i],
646 tps65910->dev, reg_data, pmic);
648 dev_err(tps65910->dev,
649 "failed to register %s regulator\n",
655 /* Save regulator for cleanup */
656 pmic->rdev[i] = rdev;
662 regulator_unregister(pmic->rdev[i]);
668 static int __devexit tps65910_remove(struct platform_device *pdev)
670 struct tps65910_reg *tps65910_reg = platform_get_drvdata(pdev);
673 for (i = 0; i < TPS65910_NUM_REGULATOR; i++)
674 regulator_unregister(tps65910_reg->rdev[i]);
680 static struct platform_driver tps65910_driver = {
682 .name = "tps65910-pmic",
683 .owner = THIS_MODULE,
685 .probe = tps65910_probe,
686 .remove = __devexit_p(tps65910_remove),
689 static int __init tps65910_init(void)
691 return platform_driver_register(&tps65910_driver);
693 subsys_initcall(tps65910_init);
695 static void __exit tps65910_cleanup(void)
697 platform_driver_unregister(&tps65910_driver);
699 module_exit(tps65910_cleanup);
701 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
702 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
703 MODULE_LICENSE("GPL v2");
704 MODULE_ALIAS("platform:tps65910-pmic");