tps65218-regulator.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * tps65218-regulator.c
  3. *
  4. * Regulator driver for TPS65218 PMIC
  5. *
  6. * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether expressed or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License version 2 for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/init.h>
  21. #include <linux/err.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/of_device.h>
  24. #include <linux/regulator/of_regulator.h>
  25. #include <linux/regulator/driver.h>
  26. #include <linux/regulator/machine.h>
  27. #include <linux/mfd/tps65218.h>
  28. enum tps65218_regulators { DCDC1, DCDC2, DCDC3, DCDC4, DCDC5, DCDC6, LDO1 };
  29. #define TPS65218_REGULATOR(_name, _id, _ops, _n, _vr, _vm, _er, _em, \
  30. _lr, _nlr, _delay, _fuv) \
  31. { \
  32. .name = _name, \
  33. .id = _id, \
  34. .ops = &_ops, \
  35. .n_voltages = _n, \
  36. .type = REGULATOR_VOLTAGE, \
  37. .owner = THIS_MODULE, \
  38. .vsel_reg = _vr, \
  39. .vsel_mask = _vm, \
  40. .enable_reg = _er, \
  41. .enable_mask = _em, \
  42. .volt_table = NULL, \
  43. .linear_ranges = _lr, \
  44. .n_linear_ranges = _nlr, \
  45. .ramp_delay = _delay, \
  46. .fixed_uV = _fuv \
  47. } \
  48. #define TPS65218_INFO(_id, _nm, _min, _max) \
  49. [_id] = { \
  50. .id = _id, \
  51. .name = _nm, \
  52. .min_uV = _min, \
  53. .max_uV = _max, \
  54. }
  55. static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
  56. REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
  57. REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
  58. };
  59. static const struct regulator_linear_range ldo1_dcdc3_ranges[] = {
  60. REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
  61. REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
  62. };
  63. static const struct regulator_linear_range dcdc4_ranges[] = {
  64. REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
  65. REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
  66. };
  67. static struct tps_info tps65218_pmic_regs[] = {
  68. TPS65218_INFO(DCDC1, "DCDC1", 850000, 167500),
  69. TPS65218_INFO(DCDC2, "DCDC2", 850000, 1675000),
  70. TPS65218_INFO(DCDC3, "DCDC3", 900000, 3400000),
  71. TPS65218_INFO(DCDC4, "DCDC4", 1175000, 3400000),
  72. TPS65218_INFO(DCDC5, "DCDC5", 1000000, 1000000),
  73. TPS65218_INFO(DCDC6, "DCDC6", 1800000, 1800000),
  74. TPS65218_INFO(LDO1, "LDO1", 900000, 3400000),
  75. };
  76. #define TPS65218_OF_MATCH(comp, label) \
  77. { \
  78. .compatible = comp, \
  79. .data = &label, \
  80. }
  81. static const struct of_device_id tps65218_of_match[] = {
  82. TPS65218_OF_MATCH("ti,tps65218-dcdc1", tps65218_pmic_regs[DCDC1]),
  83. TPS65218_OF_MATCH("ti,tps65218-dcdc2", tps65218_pmic_regs[DCDC2]),
  84. TPS65218_OF_MATCH("ti,tps65218-dcdc3", tps65218_pmic_regs[DCDC3]),
  85. TPS65218_OF_MATCH("ti,tps65218-dcdc4", tps65218_pmic_regs[DCDC4]),
  86. TPS65218_OF_MATCH("ti,tps65218-dcdc5", tps65218_pmic_regs[DCDC5]),
  87. TPS65218_OF_MATCH("ti,tps65218-dcdc6", tps65218_pmic_regs[DCDC6]),
  88. TPS65218_OF_MATCH("ti,tps65218-ldo1", tps65218_pmic_regs[LDO1]),
  89. { }
  90. };
  91. MODULE_DEVICE_TABLE(of, tps65218_of_match);
  92. static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
  93. unsigned selector)
  94. {
  95. int ret;
  96. struct tps65218 *tps = rdev_get_drvdata(dev);
  97. unsigned int rid = rdev_get_id(dev);
  98. /* Set the voltage based on vsel value and write protect level is 2 */
  99. ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  100. selector, TPS65218_PROTECT_L1);
  101. /* Set GO bit for DCDC1/2 to initiate voltage transistion */
  102. switch (rid) {
  103. case TPS65218_DCDC_1:
  104. case TPS65218_DCDC_2:
  105. ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
  106. TPS65218_SLEW_RATE_GO,
  107. TPS65218_SLEW_RATE_GO,
  108. TPS65218_PROTECT_L1);
  109. break;
  110. }
  111. return ret;
  112. }
  113. static int tps65218_pmic_enable(struct regulator_dev *dev)
  114. {
  115. struct tps65218 *tps = rdev_get_drvdata(dev);
  116. int rid = rdev_get_id(dev);
  117. if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
  118. return -EINVAL;
  119. /* Enable the regulator and password protection is level 1 */
  120. return tps65218_set_bits(tps, dev->desc->enable_reg,
  121. dev->desc->enable_mask, dev->desc->enable_mask,
  122. TPS65218_PROTECT_L1);
  123. }
  124. static int tps65218_pmic_disable(struct regulator_dev *dev)
  125. {
  126. struct tps65218 *tps = rdev_get_drvdata(dev);
  127. int rid = rdev_get_id(dev);
  128. if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
  129. return -EINVAL;
  130. /* Disable the regulator and password protection is level 1 */
  131. return tps65218_clear_bits(tps, dev->desc->enable_reg,
  132. dev->desc->enable_mask, TPS65218_PROTECT_L1);
  133. }
  134. /* Operations permitted on DCDC1, DCDC2 */
  135. static struct regulator_ops tps65218_dcdc12_ops = {
  136. .is_enabled = regulator_is_enabled_regmap,
  137. .enable = tps65218_pmic_enable,
  138. .disable = tps65218_pmic_disable,
  139. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  140. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  141. .list_voltage = regulator_list_voltage_linear_range,
  142. .map_voltage = regulator_map_voltage_linear_range,
  143. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  144. };
  145. /* Operations permitted on DCDC3, DCDC4 and LDO1 */
  146. static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
  147. .is_enabled = regulator_is_enabled_regmap,
  148. .enable = tps65218_pmic_enable,
  149. .disable = tps65218_pmic_disable,
  150. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  151. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  152. .list_voltage = regulator_list_voltage_linear_range,
  153. .map_voltage = regulator_map_voltage_linear_range,
  154. };
  155. /* Operations permitted on DCDC5, DCDC6 */
  156. static struct regulator_ops tps65218_dcdc56_pmic_ops = {
  157. .is_enabled = regulator_is_enabled_regmap,
  158. .enable = tps65218_pmic_enable,
  159. .disable = tps65218_pmic_disable,
  160. };
  161. static const struct regulator_desc regulators[] = {
  162. TPS65218_REGULATOR("DCDC1", TPS65218_DCDC_1, tps65218_dcdc12_ops, 64,
  163. TPS65218_REG_CONTROL_DCDC1,
  164. TPS65218_CONTROL_DCDC1_MASK,
  165. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC1_EN,
  166. dcdc1_dcdc2_ranges, 2, 4000, 0),
  167. TPS65218_REGULATOR("DCDC2", TPS65218_DCDC_2, tps65218_dcdc12_ops, 64,
  168. TPS65218_REG_CONTROL_DCDC2,
  169. TPS65218_CONTROL_DCDC2_MASK,
  170. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC2_EN,
  171. dcdc1_dcdc2_ranges, 2, 4000, 0),
  172. TPS65218_REGULATOR("DCDC3", TPS65218_DCDC_3, tps65218_ldo1_dcdc34_ops,
  173. 64, TPS65218_REG_CONTROL_DCDC3,
  174. TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
  175. TPS65218_ENABLE1_DC3_EN, ldo1_dcdc3_ranges, 2, 0, 0),
  176. TPS65218_REGULATOR("DCDC4", TPS65218_DCDC_4, tps65218_ldo1_dcdc34_ops,
  177. 53, TPS65218_REG_CONTROL_DCDC4,
  178. TPS65218_CONTROL_DCDC4_MASK,
  179. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC4_EN,
  180. dcdc4_ranges, 2, 0, 0),
  181. TPS65218_REGULATOR("DCDC5", TPS65218_DCDC_5, tps65218_dcdc56_pmic_ops,
  182. 1, -1, -1, TPS65218_REG_ENABLE1,
  183. TPS65218_ENABLE1_DC5_EN, NULL, 0, 0, 1000000),
  184. TPS65218_REGULATOR("DCDC6", TPS65218_DCDC_6, tps65218_dcdc56_pmic_ops,
  185. 1, -1, -1, TPS65218_REG_ENABLE1,
  186. TPS65218_ENABLE1_DC6_EN, NULL, 0, 0, 1800000),
  187. TPS65218_REGULATOR("LDO1", TPS65218_LDO_1, tps65218_ldo1_dcdc34_ops, 64,
  188. TPS65218_REG_CONTROL_LDO1,
  189. TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
  190. TPS65218_ENABLE2_LDO1_EN, ldo1_dcdc3_ranges,
  191. 2, 0, 0),
  192. };
  193. static int tps65218_regulator_probe(struct platform_device *pdev)
  194. {
  195. struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
  196. struct regulator_init_data *init_data;
  197. const struct tps_info *template;
  198. struct regulator_dev *rdev;
  199. const struct of_device_id *match;
  200. struct regulator_config config = { };
  201. int id;
  202. match = of_match_device(tps65218_of_match, &pdev->dev);
  203. if (!match)
  204. return -ENODEV;
  205. template = match->data;
  206. id = template->id;
  207. init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
  208. &regulators[id]);
  209. platform_set_drvdata(pdev, tps);
  210. tps->info[id] = &tps65218_pmic_regs[id];
  211. config.dev = &pdev->dev;
  212. config.init_data = init_data;
  213. config.driver_data = tps;
  214. config.regmap = tps->regmap;
  215. config.of_node = pdev->dev.of_node;
  216. rdev = devm_regulator_register(&pdev->dev, &regulators[id], &config);
  217. if (IS_ERR(rdev)) {
  218. dev_err(tps->dev, "failed to register %s regulator\n",
  219. pdev->name);
  220. return PTR_ERR(rdev);
  221. }
  222. return 0;
  223. }
  224. static struct platform_driver tps65218_regulator_driver = {
  225. .driver = {
  226. .name = "tps65218-pmic",
  227. .of_match_table = tps65218_of_match,
  228. },
  229. .probe = tps65218_regulator_probe,
  230. };
  231. module_platform_driver(tps65218_regulator_driver);
  232. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
  233. MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
  234. MODULE_ALIAS("platform:tps65218-pmic");
  235. MODULE_LICENSE("GPL v2");