tps65090-regulator.c 6.4 KB

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