tps65090-regulator.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. struct tps65090_regulator {
  24. struct device *dev;
  25. struct regulator_desc *desc;
  26. struct regulator_dev *rdev;
  27. };
  28. static struct regulator_ops tps65090_ops = {
  29. .enable = regulator_enable_regmap,
  30. .disable = regulator_disable_regmap,
  31. .is_enabled = regulator_is_enabled_regmap,
  32. };
  33. #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \
  34. { \
  35. .name = "TPS65090_RAILS"#_id, \
  36. .supply_name = _sname, \
  37. .id = TPS65090_REGULATOR_##_id, \
  38. .ops = &_ops, \
  39. .enable_reg = _en_reg, \
  40. .enable_mask = BIT(0), \
  41. .type = REGULATOR_VOLTAGE, \
  42. .owner = THIS_MODULE, \
  43. }
  44. static struct regulator_desc tps65090_regulator_desc[] = {
  45. tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_ops),
  46. tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_ops),
  47. tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_ops),
  48. tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_ops),
  49. tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_ops),
  50. tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_ops),
  51. tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_ops),
  52. tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_ops),
  53. tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_ops),
  54. tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_ops),
  55. };
  56. static inline bool is_dcdc(int id)
  57. {
  58. switch (id) {
  59. case TPS65090_REGULATOR_DCDC1:
  60. case TPS65090_REGULATOR_DCDC2:
  61. case TPS65090_REGULATOR_DCDC3:
  62. return true;
  63. default:
  64. return false;
  65. }
  66. }
  67. static int __devinit tps65090_config_ext_control(
  68. struct tps65090_regulator *ri, bool enable)
  69. {
  70. int ret;
  71. struct device *parent = ri->dev->parent;
  72. unsigned int reg_en_reg = ri->desc->enable_reg;
  73. if (enable)
  74. ret = tps65090_set_bits(parent, reg_en_reg, 1);
  75. else
  76. ret = tps65090_clr_bits(parent, reg_en_reg, 1);
  77. if (ret < 0)
  78. dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
  79. return ret;
  80. }
  81. static int __devinit tps65090_regulator_disable_ext_control(
  82. struct tps65090_regulator *ri,
  83. struct tps65090_regulator_plat_data *tps_pdata)
  84. {
  85. int ret = 0;
  86. struct device *parent = ri->dev->parent;
  87. unsigned int reg_en_reg = ri->desc->enable_reg;
  88. /*
  89. * First enable output for internal control if require.
  90. * And then disable external control.
  91. */
  92. if (tps_pdata->reg_init_data->constraints.always_on ||
  93. tps_pdata->reg_init_data->constraints.boot_on) {
  94. ret = tps65090_set_bits(parent, reg_en_reg, 0);
  95. if (ret < 0) {
  96. dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
  97. return ret;
  98. }
  99. }
  100. return tps65090_config_ext_control(ri, false);
  101. }
  102. static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
  103. {
  104. struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
  105. struct tps65090_regulator *ri = NULL;
  106. struct regulator_config config = { };
  107. struct regulator_dev *rdev;
  108. struct tps65090_regulator_plat_data *tps_pdata;
  109. struct tps65090_regulator *pmic;
  110. struct tps65090_platform_data *tps65090_pdata;
  111. int num;
  112. int ret;
  113. dev_dbg(&pdev->dev, "Probing regulator\n");
  114. tps65090_pdata = dev_get_platdata(pdev->dev.parent);
  115. if (!tps65090_pdata) {
  116. dev_err(&pdev->dev, "Platform data missing\n");
  117. return -EINVAL;
  118. }
  119. pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
  120. GFP_KERNEL);
  121. if (!pmic) {
  122. dev_err(&pdev->dev, "mem alloc for pmic failed\n");
  123. return -ENOMEM;
  124. }
  125. for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
  126. tps_pdata = tps65090_pdata->reg_pdata[num];
  127. ri = &pmic[num];
  128. ri->dev = &pdev->dev;
  129. ri->desc = &tps65090_regulator_desc[num];
  130. /*
  131. * TPS5090 DCDC support the control from external digital input.
  132. * It may be possible that during boot, the external control is
  133. * enabled. Disabling external control for DCDC.
  134. */
  135. if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
  136. ret = tps65090_regulator_disable_ext_control(
  137. ri, tps_pdata);
  138. if (ret < 0) {
  139. dev_err(&pdev->dev,
  140. "failed disable ext control\n");
  141. goto scrub;
  142. }
  143. }
  144. config.dev = &pdev->dev;
  145. config.driver_data = ri;
  146. config.regmap = tps65090_mfd->rmap;
  147. if (tps_pdata)
  148. config.init_data = tps_pdata->reg_init_data;
  149. else
  150. config.init_data = NULL;
  151. rdev = regulator_register(ri->desc, &config);
  152. if (IS_ERR(rdev)) {
  153. dev_err(&pdev->dev, "failed to register regulator %s\n",
  154. ri->desc->name);
  155. ret = PTR_ERR(rdev);
  156. goto scrub;
  157. }
  158. ri->rdev = rdev;
  159. }
  160. platform_set_drvdata(pdev, pmic);
  161. return 0;
  162. scrub:
  163. while (--num >= 0) {
  164. ri = &pmic[num];
  165. regulator_unregister(ri->rdev);
  166. }
  167. return ret;
  168. }
  169. static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
  170. {
  171. struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
  172. struct tps65090_regulator *ri;
  173. int num;
  174. for (num = 0; num < TPS65090_REGULATOR_MAX; ++num) {
  175. ri = &pmic[num];
  176. regulator_unregister(ri->rdev);
  177. }
  178. return 0;
  179. }
  180. static struct platform_driver tps65090_regulator_driver = {
  181. .driver = {
  182. .name = "tps65090-pmic",
  183. .owner = THIS_MODULE,
  184. },
  185. .probe = tps65090_regulator_probe,
  186. .remove = __devexit_p(tps65090_regulator_remove),
  187. };
  188. static int __init tps65090_regulator_init(void)
  189. {
  190. return platform_driver_register(&tps65090_regulator_driver);
  191. }
  192. subsys_initcall(tps65090_regulator_init);
  193. static void __exit tps65090_regulator_exit(void)
  194. {
  195. platform_driver_unregister(&tps65090_regulator_driver);
  196. }
  197. module_exit(tps65090_regulator_exit);
  198. MODULE_DESCRIPTION("tps65090 regulator driver");
  199. MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
  200. MODULE_LICENSE("GPL v2");