tps65090-regulator.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Regulator driver for tps65090 power management chip.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.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/mfd/tps65090.h>
  24. #include <linux/regulator/tps65090-regulator.h>
  25. struct tps65090_regulator {
  26. int id;
  27. /* Regulator register address.*/
  28. u8 reg_en_reg;
  29. u8 en_bit;
  30. /* used by regulator core */
  31. struct regulator_desc desc;
  32. /* Device */
  33. struct device *dev;
  34. };
  35. static inline struct device *to_tps65090_dev(struct regulator_dev *rdev)
  36. {
  37. return rdev_get_dev(rdev)->parent->parent;
  38. }
  39. static int tps65090_reg_is_enabled(struct regulator_dev *rdev)
  40. {
  41. struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
  42. struct device *parent = to_tps65090_dev(rdev);
  43. uint8_t control;
  44. int ret;
  45. ret = tps65090_read(parent, ri->reg_en_reg, &control);
  46. if (ret < 0) {
  47. dev_err(&rdev->dev, "Error in reading reg 0x%x\n",
  48. ri->reg_en_reg);
  49. return ret;
  50. }
  51. return (((control >> ri->en_bit) & 1) == 1);
  52. }
  53. static int tps65090_reg_enable(struct regulator_dev *rdev)
  54. {
  55. struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
  56. struct device *parent = to_tps65090_dev(rdev);
  57. int ret;
  58. ret = tps65090_set_bits(parent, ri->reg_en_reg, ri->en_bit);
  59. if (ret < 0)
  60. dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
  61. ri->reg_en_reg);
  62. return ret;
  63. }
  64. static int tps65090_reg_disable(struct regulator_dev *rdev)
  65. {
  66. struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
  67. struct device *parent = to_tps65090_dev(rdev);
  68. int ret;
  69. ret = tps65090_clr_bits(parent, ri->reg_en_reg, ri->en_bit);
  70. if (ret < 0)
  71. dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
  72. ri->reg_en_reg);
  73. return ret;
  74. }
  75. static struct regulator_ops tps65090_ops = {
  76. .enable = tps65090_reg_enable,
  77. .disable = tps65090_reg_disable,
  78. .is_enabled = tps65090_reg_is_enabled,
  79. };
  80. #define tps65090_REG(_id, _en_reg, _en_bit, _ops) \
  81. { \
  82. .reg_en_reg = _en_reg, \
  83. .en_bit = _en_bit, \
  84. .id = TPS65090_ID_##_id, \
  85. .desc = { \
  86. .name = tps65090_rails(_id), \
  87. .id = TPS65090_ID_##_id, \
  88. .ops = &_ops, \
  89. .type = REGULATOR_VOLTAGE, \
  90. .owner = THIS_MODULE, \
  91. }, \
  92. }
  93. static struct tps65090_regulator TPS65090_regulator[] = {
  94. tps65090_REG(DCDC1, 12, 0, tps65090_ops),
  95. tps65090_REG(DCDC2, 13, 0, tps65090_ops),
  96. tps65090_REG(DCDC3, 14, 0, tps65090_ops),
  97. tps65090_REG(FET1, 15, 0, tps65090_ops),
  98. tps65090_REG(FET2, 16, 0, tps65090_ops),
  99. tps65090_REG(FET3, 17, 0, tps65090_ops),
  100. tps65090_REG(FET4, 18, 0, tps65090_ops),
  101. tps65090_REG(FET5, 19, 0, tps65090_ops),
  102. tps65090_REG(FET6, 20, 0, tps65090_ops),
  103. tps65090_REG(FET7, 21, 0, tps65090_ops),
  104. };
  105. static inline struct tps65090_regulator *find_regulator_info(int id)
  106. {
  107. struct tps65090_regulator *ri;
  108. int i;
  109. for (i = 0; i < ARRAY_SIZE(TPS65090_regulator); i++) {
  110. ri = &TPS65090_regulator[i];
  111. if (ri->desc.id == id)
  112. return ri;
  113. }
  114. return NULL;
  115. }
  116. static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
  117. {
  118. struct tps65090_regulator *ri = NULL;
  119. struct regulator_dev *rdev;
  120. struct tps65090_regulator_platform_data *tps_pdata;
  121. int id = pdev->id;
  122. dev_dbg(&pdev->dev, "Probing regulator %d\n", id);
  123. ri = find_regulator_info(id);
  124. if (ri == NULL) {
  125. dev_err(&pdev->dev, "invalid regulator ID specified\n");
  126. return -EINVAL;
  127. }
  128. tps_pdata = pdev->dev.platform_data;
  129. ri->dev = &pdev->dev;
  130. rdev = regulator_register(&ri->desc, &pdev->dev,
  131. &tps_pdata->regulator, ri, NULL);
  132. if (IS_ERR_OR_NULL(rdev)) {
  133. dev_err(&pdev->dev, "failed to register regulator %s\n",
  134. ri->desc.name);
  135. return PTR_ERR(rdev);
  136. }
  137. platform_set_drvdata(pdev, rdev);
  138. return 0;
  139. }
  140. static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
  141. {
  142. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  143. regulator_unregister(rdev);
  144. return 0;
  145. }
  146. static struct platform_driver tps65090_regulator_driver = {
  147. .driver = {
  148. .name = "tps65090-regulator",
  149. .owner = THIS_MODULE,
  150. },
  151. .probe = tps65090_regulator_probe,
  152. .remove = __devexit_p(tps65090_regulator_remove),
  153. };
  154. static int __init tps65090_regulator_init(void)
  155. {
  156. return platform_driver_register(&tps65090_regulator_driver);
  157. }
  158. subsys_initcall(tps65090_regulator_init);
  159. static void __exit tps65090_regulator_exit(void)
  160. {
  161. platform_driver_unregister(&tps65090_regulator_driver);
  162. }
  163. module_exit(tps65090_regulator_exit);
  164. MODULE_DESCRIPTION("tps65090 regulator driver");
  165. MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
  166. MODULE_LICENSE("GPL v2");