tps65218-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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,
  29. DCDC5, DCDC6, LDO1, LS3 };
  30. #define TPS65218_REGULATOR(_name, _id, _type, _ops, _n, _vr, _vm, _er, _em, \
  31. _cr, _cm, _lr, _nlr, _delay, _fuv) \
  32. { \
  33. .name = _name, \
  34. .id = _id, \
  35. .ops = &_ops, \
  36. .n_voltages = _n, \
  37. .type = _type, \
  38. .owner = THIS_MODULE, \
  39. .vsel_reg = _vr, \
  40. .vsel_mask = _vm, \
  41. .csel_reg = _cr, \
  42. .csel_mask = _cm, \
  43. .enable_reg = _er, \
  44. .enable_mask = _em, \
  45. .volt_table = NULL, \
  46. .linear_ranges = _lr, \
  47. .n_linear_ranges = _nlr, \
  48. .ramp_delay = _delay, \
  49. .fixed_uV = _fuv \
  50. } \
  51. #define TPS65218_INFO(_id, _nm, _min, _max) \
  52. [_id] = { \
  53. .id = _id, \
  54. .name = _nm, \
  55. .min_uV = _min, \
  56. .max_uV = _max, \
  57. }
  58. static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
  59. REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
  60. REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
  61. };
  62. static const struct regulator_linear_range ldo1_dcdc3_ranges[] = {
  63. REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
  64. REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
  65. };
  66. static const struct regulator_linear_range dcdc4_ranges[] = {
  67. REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
  68. REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
  69. };
  70. static struct tps_info tps65218_pmic_regs[] = {
  71. TPS65218_INFO(DCDC1, "DCDC1", 850000, 1675000),
  72. TPS65218_INFO(DCDC2, "DCDC2", 850000, 1675000),
  73. TPS65218_INFO(DCDC3, "DCDC3", 900000, 3400000),
  74. TPS65218_INFO(DCDC4, "DCDC4", 1175000, 3400000),
  75. TPS65218_INFO(DCDC5, "DCDC5", 1000000, 1000000),
  76. TPS65218_INFO(DCDC6, "DCDC6", 1800000, 1800000),
  77. TPS65218_INFO(LDO1, "LDO1", 900000, 3400000),
  78. TPS65218_INFO(LS3, "LS3", -1, -1),
  79. };
  80. #define TPS65218_OF_MATCH(comp, label) \
  81. { \
  82. .compatible = comp, \
  83. .data = &label, \
  84. }
  85. static const struct of_device_id tps65218_of_match[] = {
  86. TPS65218_OF_MATCH("ti,tps65218-dcdc1", tps65218_pmic_regs[DCDC1]),
  87. TPS65218_OF_MATCH("ti,tps65218-dcdc2", tps65218_pmic_regs[DCDC2]),
  88. TPS65218_OF_MATCH("ti,tps65218-dcdc3", tps65218_pmic_regs[DCDC3]),
  89. TPS65218_OF_MATCH("ti,tps65218-dcdc4", tps65218_pmic_regs[DCDC4]),
  90. TPS65218_OF_MATCH("ti,tps65218-dcdc5", tps65218_pmic_regs[DCDC5]),
  91. TPS65218_OF_MATCH("ti,tps65218-dcdc6", tps65218_pmic_regs[DCDC6]),
  92. TPS65218_OF_MATCH("ti,tps65218-ldo1", tps65218_pmic_regs[LDO1]),
  93. TPS65218_OF_MATCH("ti,tps65218-ls3", tps65218_pmic_regs[LS3]),
  94. { }
  95. };
  96. MODULE_DEVICE_TABLE(of, tps65218_of_match);
  97. static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
  98. unsigned selector)
  99. {
  100. int ret;
  101. struct tps65218 *tps = rdev_get_drvdata(dev);
  102. unsigned int rid = rdev_get_id(dev);
  103. /* Set the voltage based on vsel value and write protect level is 2 */
  104. ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  105. selector, TPS65218_PROTECT_L1);
  106. /* Set GO bit for DCDC1/2 to initiate voltage transistion */
  107. switch (rid) {
  108. case TPS65218_DCDC_1:
  109. case TPS65218_DCDC_2:
  110. ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
  111. TPS65218_SLEW_RATE_GO,
  112. TPS65218_SLEW_RATE_GO,
  113. TPS65218_PROTECT_L1);
  114. break;
  115. }
  116. return ret;
  117. }
  118. static int tps65218_pmic_enable(struct regulator_dev *dev)
  119. {
  120. struct tps65218 *tps = rdev_get_drvdata(dev);
  121. int rid = rdev_get_id(dev);
  122. if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
  123. return -EINVAL;
  124. /* Enable the regulator and password protection is level 1 */
  125. return tps65218_set_bits(tps, dev->desc->enable_reg,
  126. dev->desc->enable_mask, dev->desc->enable_mask,
  127. TPS65218_PROTECT_L1);
  128. }
  129. static int tps65218_pmic_disable(struct regulator_dev *dev)
  130. {
  131. struct tps65218 *tps = rdev_get_drvdata(dev);
  132. int rid = rdev_get_id(dev);
  133. if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
  134. return -EINVAL;
  135. /* Disable the regulator and password protection is level 1 */
  136. return tps65218_clear_bits(tps, dev->desc->enable_reg,
  137. dev->desc->enable_mask, TPS65218_PROTECT_L1);
  138. }
  139. /* Operations permitted on DCDC1, DCDC2 */
  140. static struct regulator_ops tps65218_dcdc12_ops = {
  141. .is_enabled = regulator_is_enabled_regmap,
  142. .enable = tps65218_pmic_enable,
  143. .disable = tps65218_pmic_disable,
  144. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  145. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  146. .list_voltage = regulator_list_voltage_linear_range,
  147. .map_voltage = regulator_map_voltage_linear_range,
  148. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  149. };
  150. /* Operations permitted on DCDC3, DCDC4 and LDO1 */
  151. static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
  152. .is_enabled = regulator_is_enabled_regmap,
  153. .enable = tps65218_pmic_enable,
  154. .disable = tps65218_pmic_disable,
  155. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  156. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  157. .list_voltage = regulator_list_voltage_linear_range,
  158. .map_voltage = regulator_map_voltage_linear_range,
  159. };
  160. static const int ls3_currents[] = { 100, 200, 500, 1000 };
  161. static int tps65218_pmic_set_input_current_lim(struct regulator_dev *dev,
  162. int lim_uA)
  163. {
  164. unsigned int index = 0;
  165. unsigned int num_currents = ARRAY_SIZE(ls3_currents);
  166. struct tps65218 *tps = rdev_get_drvdata(dev);
  167. while (index < num_currents && ls3_currents[index] != lim_uA)
  168. index++;
  169. if (index == num_currents)
  170. return -EINVAL;
  171. return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
  172. index << 2, TPS65218_PROTECT_L1);
  173. }
  174. static int tps65218_pmic_set_current_limit(struct regulator_dev *dev,
  175. int min_uA, int max_uA)
  176. {
  177. int index = 0;
  178. unsigned int num_currents = ARRAY_SIZE(ls3_currents);
  179. struct tps65218 *tps = rdev_get_drvdata(dev);
  180. while (index < num_currents && ls3_currents[index] < max_uA)
  181. index++;
  182. index--;
  183. if (index < 0 || ls3_currents[index] < min_uA)
  184. return -EINVAL;
  185. return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
  186. index << 2, TPS65218_PROTECT_L1);
  187. }
  188. static int tps65218_pmic_get_current_limit(struct regulator_dev *dev)
  189. {
  190. int retval;
  191. unsigned int index;
  192. struct tps65218 *tps = rdev_get_drvdata(dev);
  193. retval = tps65218_reg_read(tps, dev->desc->csel_reg, &index);
  194. if (retval < 0)
  195. return retval;
  196. index = (index & dev->desc->csel_mask) >> 2;
  197. return ls3_currents[index];
  198. }
  199. static struct regulator_ops tps65218_ls3_ops = {
  200. .is_enabled = regulator_is_enabled_regmap,
  201. .enable = tps65218_pmic_enable,
  202. .disable = tps65218_pmic_disable,
  203. .set_input_current_limit = tps65218_pmic_set_input_current_lim,
  204. .set_current_limit = tps65218_pmic_set_current_limit,
  205. .get_current_limit = tps65218_pmic_get_current_limit,
  206. };
  207. /* Operations permitted on DCDC5, DCDC6 */
  208. static struct regulator_ops tps65218_dcdc56_pmic_ops = {
  209. .is_enabled = regulator_is_enabled_regmap,
  210. .enable = tps65218_pmic_enable,
  211. .disable = tps65218_pmic_disable,
  212. };
  213. static const struct regulator_desc regulators[] = {
  214. TPS65218_REGULATOR("DCDC1", TPS65218_DCDC_1, REGULATOR_VOLTAGE,
  215. tps65218_dcdc12_ops, 64, TPS65218_REG_CONTROL_DCDC1,
  216. TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1,
  217. TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges,
  218. 2, 4000, 0),
  219. TPS65218_REGULATOR("DCDC2", TPS65218_DCDC_2, REGULATOR_VOLTAGE,
  220. tps65218_dcdc12_ops, 64, TPS65218_REG_CONTROL_DCDC2,
  221. TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1,
  222. TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges,
  223. 2, 4000, 0),
  224. TPS65218_REGULATOR("DCDC3", TPS65218_DCDC_3, REGULATOR_VOLTAGE,
  225. tps65218_ldo1_dcdc34_ops, 64,
  226. TPS65218_REG_CONTROL_DCDC3,
  227. TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
  228. TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2,
  229. 0, 0),
  230. TPS65218_REGULATOR("DCDC4", TPS65218_DCDC_4, REGULATOR_VOLTAGE,
  231. tps65218_ldo1_dcdc34_ops, 53,
  232. TPS65218_REG_CONTROL_DCDC4,
  233. TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1,
  234. TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2,
  235. 0, 0),
  236. TPS65218_REGULATOR("DCDC5", TPS65218_DCDC_5, REGULATOR_VOLTAGE,
  237. tps65218_dcdc56_pmic_ops, 1, -1, -1,
  238. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC5_EN, 0, 0,
  239. NULL, 0, 0, 1000000),
  240. TPS65218_REGULATOR("DCDC6", TPS65218_DCDC_6, REGULATOR_VOLTAGE,
  241. tps65218_dcdc56_pmic_ops, 1, -1, -1,
  242. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC6_EN, 0, 0,
  243. NULL, 0, 0, 1800000),
  244. TPS65218_REGULATOR("LDO1", TPS65218_LDO_1, REGULATOR_VOLTAGE,
  245. tps65218_ldo1_dcdc34_ops, 64,
  246. TPS65218_REG_CONTROL_LDO1,
  247. TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
  248. TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
  249. 2, 0, 0),
  250. TPS65218_REGULATOR("LS3", TPS65218_LS_3, REGULATOR_CURRENT,
  251. tps65218_ls3_ops, 0, 0, 0, TPS65218_REG_ENABLE2,
  252. TPS65218_ENABLE2_LS3_EN, TPS65218_REG_CONFIG2,
  253. TPS65218_CONFIG2_LS3ILIM_MASK, NULL, 0, 0, 0),
  254. };
  255. static int tps65218_regulator_probe(struct platform_device *pdev)
  256. {
  257. struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
  258. struct regulator_init_data *init_data;
  259. const struct tps_info *template;
  260. struct regulator_dev *rdev;
  261. const struct of_device_id *match;
  262. struct regulator_config config = { };
  263. int id;
  264. match = of_match_device(tps65218_of_match, &pdev->dev);
  265. if (!match)
  266. return -ENODEV;
  267. template = match->data;
  268. id = template->id;
  269. init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
  270. &regulators[id]);
  271. platform_set_drvdata(pdev, tps);
  272. tps->info[id] = &tps65218_pmic_regs[id];
  273. config.dev = &pdev->dev;
  274. config.init_data = init_data;
  275. config.driver_data = tps;
  276. config.regmap = tps->regmap;
  277. config.of_node = pdev->dev.of_node;
  278. rdev = devm_regulator_register(&pdev->dev, &regulators[id], &config);
  279. if (IS_ERR(rdev)) {
  280. dev_err(tps->dev, "failed to register %s regulator\n",
  281. pdev->name);
  282. return PTR_ERR(rdev);
  283. }
  284. return 0;
  285. }
  286. static struct platform_driver tps65218_regulator_driver = {
  287. .driver = {
  288. .name = "tps65218-pmic",
  289. .of_match_table = tps65218_of_match,
  290. },
  291. .probe = tps65218_regulator_probe,
  292. };
  293. module_platform_driver(tps65218_regulator_driver);
  294. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
  295. MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
  296. MODULE_ALIAS("platform:tps65218-pmic");
  297. MODULE_LICENSE("GPL v2");