tps65090-regulator.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/mfd/tps65090.h>
  23. #include <linux/regulator/tps65090-regulator.h>
  24. struct tps65090_regulator {
  25. int id;
  26. /* Regulator register address.*/
  27. u8 reg_en_reg;
  28. u8 en_bit;
  29. /* used by regulator core */
  30. struct regulator_desc desc;
  31. /* Device */
  32. struct device *dev;
  33. };
  34. static inline struct device *to_tps65090_dev(struct regulator_dev *rdev)
  35. {
  36. return rdev_get_dev(rdev)->parent->parent;
  37. }
  38. static int tps65090_reg_is_enabled(struct regulator_dev *rdev)
  39. {
  40. struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
  41. struct device *parent = to_tps65090_dev(rdev);
  42. uint8_t control;
  43. int ret;
  44. ret = tps65090_read(parent, ri->reg_en_reg, &control);
  45. if (ret < 0) {
  46. dev_err(&rdev->dev, "Error in reading reg 0x%x\n",
  47. ri->reg_en_reg);
  48. return ret;
  49. }
  50. return (((control >> ri->en_bit) & 1) == 1);
  51. }
  52. static int tps65090_reg_enable(struct regulator_dev *rdev)
  53. {
  54. struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
  55. struct device *parent = to_tps65090_dev(rdev);
  56. int ret;
  57. ret = tps65090_set_bits(parent, ri->reg_en_reg, ri->en_bit);
  58. if (ret < 0)
  59. dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
  60. ri->reg_en_reg);
  61. return ret;
  62. }
  63. static int tps65090_reg_disable(struct regulator_dev *rdev)
  64. {
  65. struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
  66. struct device *parent = to_tps65090_dev(rdev);
  67. int ret;
  68. ret = tps65090_clr_bits(parent, ri->reg_en_reg, ri->en_bit);
  69. if (ret < 0)
  70. dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
  71. ri->reg_en_reg);
  72. return ret;
  73. }
  74. static struct regulator_ops tps65090_ops = {
  75. .enable = tps65090_reg_enable,
  76. .disable = tps65090_reg_disable,
  77. .is_enabled = tps65090_reg_is_enabled,
  78. };
  79. #define tps65090_REG(_id) \
  80. { \
  81. .reg_en_reg = (TPS65090_ID_##_id) + 12, \
  82. .en_bit = 0, \
  83. .id = TPS65090_ID_##_id, \
  84. .desc = { \
  85. .name = tps65090_rails(_id), \
  86. .id = TPS65090_ID_##_id, \
  87. .ops = &tps65090_ops, \
  88. .type = REGULATOR_VOLTAGE, \
  89. .owner = THIS_MODULE, \
  90. }, \
  91. }
  92. static struct tps65090_regulator TPS65090_regulator[] = {
  93. tps65090_REG(DCDC1),
  94. tps65090_REG(DCDC2),
  95. tps65090_REG(DCDC3),
  96. tps65090_REG(FET1),
  97. tps65090_REG(FET2),
  98. tps65090_REG(FET3),
  99. tps65090_REG(FET4),
  100. tps65090_REG(FET5),
  101. tps65090_REG(FET6),
  102. tps65090_REG(FET7),
  103. };
  104. static inline struct tps65090_regulator *find_regulator_info(int id)
  105. {
  106. struct tps65090_regulator *ri;
  107. int i;
  108. for (i = 0; i < ARRAY_SIZE(TPS65090_regulator); i++) {
  109. ri = &TPS65090_regulator[i];
  110. if (ri->desc.id == id)
  111. return ri;
  112. }
  113. return NULL;
  114. }
  115. static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
  116. {
  117. struct tps65090_regulator *ri = NULL;
  118. struct regulator_config config = { };
  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. config.dev = &pdev->dev;
  131. config.init_data = &tps_pdata->regulator;
  132. config.driver_data = ri;
  133. rdev = regulator_register(&ri->desc, &config);
  134. if (IS_ERR(rdev)) {
  135. dev_err(&pdev->dev, "failed to register regulator %s\n",
  136. ri->desc.name);
  137. return PTR_ERR(rdev);
  138. }
  139. platform_set_drvdata(pdev, rdev);
  140. return 0;
  141. }
  142. static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
  143. {
  144. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  145. regulator_unregister(rdev);
  146. return 0;
  147. }
  148. static struct platform_driver tps65090_regulator_driver = {
  149. .driver = {
  150. .name = "tps65090-regulator",
  151. .owner = THIS_MODULE,
  152. },
  153. .probe = tps65090_regulator_probe,
  154. .remove = __devexit_p(tps65090_regulator_remove),
  155. };
  156. static int __init tps65090_regulator_init(void)
  157. {
  158. return platform_driver_register(&tps65090_regulator_driver);
  159. }
  160. subsys_initcall(tps65090_regulator_init);
  161. static void __exit tps65090_regulator_exit(void)
  162. {
  163. platform_driver_unregister(&tps65090_regulator_driver);
  164. }
  165. module_exit(tps65090_regulator_exit);
  166. MODULE_DESCRIPTION("tps65090 regulator driver");
  167. MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
  168. MODULE_LICENSE("GPL v2");