tps65090-regulator.c 7.4 KB

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