tps65218-regulator.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, _t, \
  30. _lr, _nlr, _delay) \
  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 = _t, \
  43. .linear_ranges = _lr, \
  44. .n_linear_ranges = _nlr, \
  45. .ramp_delay = _delay, \
  46. } \
  47. #define TPS65218_INFO(_id, _nm, _min, _max) \
  48. { \
  49. .id = _id, \
  50. .name = _nm, \
  51. .min_uV = _min, \
  52. .max_uV = _max, \
  53. }
  54. static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
  55. REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
  56. REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
  57. };
  58. static const struct regulator_linear_range ldo1_dcdc3_ranges[] = {
  59. REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
  60. REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
  61. };
  62. static const struct regulator_linear_range dcdc4_ranges[] = {
  63. REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
  64. REGULATOR_LINEAR_RANGE(1550000, 0x10, 0x34, 50000),
  65. };
  66. static struct tps_info tps65218_pmic_regs[] = {
  67. TPS65218_INFO(0, "DCDC1", 850000, 167500),
  68. TPS65218_INFO(1, "DCDC2", 850000, 1675000),
  69. TPS65218_INFO(2, "DCDC3", 900000, 3400000),
  70. TPS65218_INFO(3, "DCDC4", 1175000, 3400000),
  71. TPS65218_INFO(4, "DCDC5", 1000000, 1000000),
  72. TPS65218_INFO(5, "DCDC6", 1800000, 1800000),
  73. TPS65218_INFO(6, "LDO1", 900000, 3400000),
  74. };
  75. #define TPS65218_OF_MATCH(comp, label) \
  76. { \
  77. .compatible = comp, \
  78. .data = &label, \
  79. }
  80. static const struct of_device_id tps65218_of_match[] = {
  81. TPS65218_OF_MATCH("ti,tps65218-dcdc1", tps65218_pmic_regs[DCDC1]),
  82. TPS65218_OF_MATCH("ti,tps65218-dcdc2", tps65218_pmic_regs[DCDC2]),
  83. TPS65218_OF_MATCH("ti,tps65218-dcdc3", tps65218_pmic_regs[DCDC3]),
  84. TPS65218_OF_MATCH("ti,tps65218-dcdc4", tps65218_pmic_regs[DCDC4]),
  85. TPS65218_OF_MATCH("ti,tps65218-dcdc5", tps65218_pmic_regs[DCDC5]),
  86. TPS65218_OF_MATCH("ti,tps65218-dcdc6", tps65218_pmic_regs[DCDC6]),
  87. TPS65218_OF_MATCH("ti,tps65218-ldo1", tps65218_pmic_regs[LDO1]),
  88. { }
  89. };
  90. MODULE_DEVICE_TABLE(of, tps65218_of_match);
  91. static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
  92. unsigned selector)
  93. {
  94. int ret;
  95. struct tps65218 *tps = rdev_get_drvdata(dev);
  96. unsigned int rid = rdev_get_id(dev);
  97. /* Set the voltage based on vsel value and write protect level is 2 */
  98. ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  99. selector, TPS65218_PROTECT_L1);
  100. /* Set GO bit for DCDC1/2 to initiate voltage transistion */
  101. switch (rid) {
  102. case TPS65218_DCDC_1:
  103. case TPS65218_DCDC_2:
  104. ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
  105. TPS65218_SLEW_RATE_GO,
  106. TPS65218_SLEW_RATE_GO,
  107. TPS65218_PROTECT_L1);
  108. break;
  109. }
  110. return ret;
  111. }
  112. static int tps65218_pmic_enable(struct regulator_dev *dev)
  113. {
  114. struct tps65218 *tps = rdev_get_drvdata(dev);
  115. unsigned int rid = rdev_get_id(dev);
  116. if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
  117. return -EINVAL;
  118. /* Enable the regulator and password protection is level 1 */
  119. return tps65218_set_bits(tps, dev->desc->enable_reg,
  120. dev->desc->enable_mask, dev->desc->enable_mask,
  121. TPS65218_PROTECT_L1);
  122. }
  123. static int tps65218_pmic_disable(struct regulator_dev *dev)
  124. {
  125. struct tps65218 *tps = rdev_get_drvdata(dev);
  126. unsigned int rid = rdev_get_id(dev);
  127. if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
  128. return -EINVAL;
  129. /* Disable the regulator and password protection is level 1 */
  130. return tps65218_clear_bits(tps, dev->desc->enable_reg,
  131. dev->desc->enable_mask, TPS65218_PROTECT_L1);
  132. }
  133. /* Operations permitted on DCDC1, DCDC2 */
  134. static struct regulator_ops tps65218_dcdc12_ops = {
  135. .is_enabled = regulator_is_enabled_regmap,
  136. .enable = tps65218_pmic_enable,
  137. .disable = tps65218_pmic_disable,
  138. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  139. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  140. .list_voltage = regulator_list_voltage_linear_range,
  141. .map_voltage = regulator_map_voltage_linear_range,
  142. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  143. };
  144. /* Operations permitted on DCDC3, DCDC4 and LDO1 */
  145. static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
  146. .is_enabled = regulator_is_enabled_regmap,
  147. .enable = tps65218_pmic_enable,
  148. .disable = tps65218_pmic_disable,
  149. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  150. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  151. .list_voltage = regulator_list_voltage_linear_range,
  152. .map_voltage = regulator_map_voltage_linear_range,
  153. };
  154. /* Operations permitted on DCDC5, DCDC6 */
  155. static struct regulator_ops tps65218_dcdc56_pmic_ops = {
  156. .is_enabled = regulator_is_enabled_regmap,
  157. .enable = tps65218_pmic_enable,
  158. .disable = tps65218_pmic_disable,
  159. };
  160. static const struct regulator_desc regulators[] = {
  161. TPS65218_REGULATOR("DCDC1", TPS65218_DCDC_1, tps65218_dcdc12_ops, 64,
  162. TPS65218_REG_CONTROL_DCDC1,
  163. TPS65218_CONTROL_DCDC1_MASK,
  164. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC1_EN, NULL,
  165. dcdc1_dcdc2_ranges, 2, 4000),
  166. TPS65218_REGULATOR("DCDC2", TPS65218_DCDC_2, tps65218_dcdc12_ops, 64,
  167. TPS65218_REG_CONTROL_DCDC2,
  168. TPS65218_CONTROL_DCDC2_MASK,
  169. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC2_EN, NULL,
  170. dcdc1_dcdc2_ranges, 2, 4000),
  171. TPS65218_REGULATOR("DCDC3", TPS65218_DCDC_3, tps65218_ldo1_dcdc34_ops,
  172. 64, TPS65218_REG_CONTROL_DCDC3,
  173. TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
  174. TPS65218_ENABLE1_DC3_EN, NULL,
  175. ldo1_dcdc3_ranges, 2, 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, NULL,
  180. dcdc4_ranges, 2, 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, NULL, 0, 0),
  184. TPS65218_REGULATOR("DCDC6", TPS65218_DCDC_6, tps65218_dcdc56_pmic_ops,
  185. 1, -1, -1, TPS65218_REG_ENABLE1,
  186. TPS65218_ENABLE1_DC6_EN, NULL, NULL, 0, 0),
  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, NULL, ldo1_dcdc3_ranges,
  191. 2, 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. platform_set_drvdata(pdev, tps);
  209. tps->info[id] = &tps65218_pmic_regs[id];
  210. config.dev = &pdev->dev;
  211. config.init_data = init_data;
  212. config.driver_data = tps;
  213. config.regmap = tps->regmap;
  214. config.of_node = pdev->dev.of_node;
  215. rdev = devm_regulator_register(&pdev->dev, &regulators[id], &config);
  216. if (IS_ERR(rdev)) {
  217. dev_err(tps->dev, "failed to register %s regulator\n",
  218. pdev->name);
  219. return PTR_ERR(rdev);
  220. }
  221. return 0;
  222. }
  223. static struct platform_driver tps65218_regulator_driver = {
  224. .driver = {
  225. .name = "tps65218-pmic",
  226. .owner = THIS_MODULE,
  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");