tps65218-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tps65218-regulator.c
  4. *
  5. * Regulator driver for TPS65218 PMIC
  6. *
  7. * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/of_regulator.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/regulator/machine.h>
  20. #include <linux/mfd/tps65218.h>
  21. #define TPS65218_REGULATOR(_name, _of, _id, _type, _ops, _n, _vr, _vm, _er, \
  22. _em, _cr, _cm, _lr, _nlr, _delay, _fuv, _sr, _sm, \
  23. _ct, _ncl) \
  24. { \
  25. .name = _name, \
  26. .of_match = _of, \
  27. .id = _id, \
  28. .ops = &_ops, \
  29. .n_voltages = _n, \
  30. .type = _type, \
  31. .owner = THIS_MODULE, \
  32. .vsel_reg = _vr, \
  33. .vsel_mask = _vm, \
  34. .csel_reg = _cr, \
  35. .csel_mask = _cm, \
  36. .curr_table = _ct, \
  37. .n_current_limits = _ncl, \
  38. .enable_reg = _er, \
  39. .enable_mask = _em, \
  40. .volt_table = NULL, \
  41. .linear_ranges = _lr, \
  42. .n_linear_ranges = _nlr, \
  43. .ramp_delay = _delay, \
  44. .fixed_uV = _fuv, \
  45. .bypass_reg = _sr, \
  46. .bypass_mask = _sm, \
  47. } \
  48. static const struct linear_range dcdc1_dcdc2_ranges[] = {
  49. REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
  50. REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
  51. };
  52. static const struct linear_range ldo1_dcdc3_ranges[] = {
  53. REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
  54. REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
  55. };
  56. static const struct linear_range dcdc4_ranges[] = {
  57. REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
  58. REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
  59. };
  60. static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
  61. unsigned selector)
  62. {
  63. int ret;
  64. struct tps65218 *tps = rdev_get_drvdata(dev);
  65. unsigned int rid = rdev_get_id(dev);
  66. /* Set the voltage based on vsel value and write protect level is 2 */
  67. ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  68. selector, TPS65218_PROTECT_L1);
  69. /* Set GO bit for DCDC1/2 to initiate voltage transistion */
  70. switch (rid) {
  71. case TPS65218_DCDC_1:
  72. case TPS65218_DCDC_2:
  73. ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
  74. TPS65218_SLEW_RATE_GO,
  75. TPS65218_SLEW_RATE_GO,
  76. TPS65218_PROTECT_L1);
  77. break;
  78. }
  79. return ret;
  80. }
  81. static int tps65218_pmic_enable(struct regulator_dev *dev)
  82. {
  83. struct tps65218 *tps = rdev_get_drvdata(dev);
  84. int rid = rdev_get_id(dev);
  85. if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
  86. return -EINVAL;
  87. /* Enable the regulator and password protection is level 1 */
  88. return tps65218_set_bits(tps, dev->desc->enable_reg,
  89. dev->desc->enable_mask, dev->desc->enable_mask,
  90. TPS65218_PROTECT_L1);
  91. }
  92. static int tps65218_pmic_disable(struct regulator_dev *dev)
  93. {
  94. struct tps65218 *tps = rdev_get_drvdata(dev);
  95. int rid = rdev_get_id(dev);
  96. if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
  97. return -EINVAL;
  98. /* Disable the regulator and password protection is level 1 */
  99. return tps65218_clear_bits(tps, dev->desc->enable_reg,
  100. dev->desc->enable_mask, TPS65218_PROTECT_L1);
  101. }
  102. static int tps65218_pmic_set_suspend_enable(struct regulator_dev *dev)
  103. {
  104. struct tps65218 *tps = rdev_get_drvdata(dev);
  105. unsigned int rid = rdev_get_id(dev);
  106. if (rid > TPS65218_LDO_1)
  107. return -EINVAL;
  108. return tps65218_clear_bits(tps, dev->desc->bypass_reg,
  109. dev->desc->bypass_mask,
  110. TPS65218_PROTECT_L1);
  111. }
  112. static int tps65218_pmic_set_suspend_disable(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_LDO_1)
  117. return -EINVAL;
  118. /*
  119. * Certain revisions of TPS65218 will need to have DCDC3 regulator
  120. * enabled always, otherwise an immediate system reboot will occur
  121. * during poweroff.
  122. */
  123. if (rid == TPS65218_DCDC_3 && tps->rev == TPS65218_REV_2_1)
  124. return 0;
  125. if (!tps->strobes[rid]) {
  126. if (rid == TPS65218_DCDC_3)
  127. tps->strobes[rid] = 3;
  128. else
  129. return -EINVAL;
  130. }
  131. return tps65218_set_bits(tps, dev->desc->bypass_reg,
  132. dev->desc->bypass_mask,
  133. tps->strobes[rid], TPS65218_PROTECT_L1);
  134. }
  135. /* Operations permitted on DCDC1, DCDC2 */
  136. static const struct regulator_ops tps65218_dcdc12_ops = {
  137. .is_enabled = regulator_is_enabled_regmap,
  138. .enable = tps65218_pmic_enable,
  139. .disable = tps65218_pmic_disable,
  140. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  141. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  142. .list_voltage = regulator_list_voltage_linear_range,
  143. .map_voltage = regulator_map_voltage_linear_range,
  144. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  145. .set_suspend_enable = tps65218_pmic_set_suspend_enable,
  146. .set_suspend_disable = tps65218_pmic_set_suspend_disable,
  147. };
  148. /* Operations permitted on DCDC3, DCDC4 and LDO1 */
  149. static const struct regulator_ops tps65218_ldo1_dcdc34_ops = {
  150. .is_enabled = regulator_is_enabled_regmap,
  151. .enable = tps65218_pmic_enable,
  152. .disable = tps65218_pmic_disable,
  153. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  154. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  155. .list_voltage = regulator_list_voltage_linear_range,
  156. .map_voltage = regulator_map_voltage_linear_range,
  157. .set_suspend_enable = tps65218_pmic_set_suspend_enable,
  158. .set_suspend_disable = tps65218_pmic_set_suspend_disable,
  159. };
  160. static const unsigned int ls3_currents[] = { 100000, 200000, 500000, 1000000 };
  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 << __builtin_ctz(dev->desc->csel_mask),
  173. TPS65218_PROTECT_L1);
  174. }
  175. static int tps65218_pmic_set_current_limit(struct regulator_dev *dev,
  176. int min_uA, int max_uA)
  177. {
  178. int index = 0;
  179. unsigned int num_currents = ARRAY_SIZE(ls3_currents);
  180. struct tps65218 *tps = rdev_get_drvdata(dev);
  181. while (index < num_currents && ls3_currents[index] <= max_uA)
  182. index++;
  183. index--;
  184. if (index < 0 || ls3_currents[index] < min_uA)
  185. return -EINVAL;
  186. return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
  187. index << __builtin_ctz(dev->desc->csel_mask),
  188. TPS65218_PROTECT_L1);
  189. }
  190. static const struct regulator_ops tps65218_ls23_ops = {
  191. .is_enabled = regulator_is_enabled_regmap,
  192. .enable = tps65218_pmic_enable,
  193. .disable = tps65218_pmic_disable,
  194. .set_input_current_limit = tps65218_pmic_set_input_current_lim,
  195. .set_current_limit = tps65218_pmic_set_current_limit,
  196. .get_current_limit = regulator_get_current_limit_regmap,
  197. };
  198. /* Operations permitted on DCDC5, DCDC6 */
  199. static const struct regulator_ops tps65218_dcdc56_pmic_ops = {
  200. .is_enabled = regulator_is_enabled_regmap,
  201. .enable = tps65218_pmic_enable,
  202. .disable = tps65218_pmic_disable,
  203. .set_suspend_enable = tps65218_pmic_set_suspend_enable,
  204. .set_suspend_disable = tps65218_pmic_set_suspend_disable,
  205. };
  206. static const struct regulator_desc regulators[] = {
  207. TPS65218_REGULATOR("DCDC1", "regulator-dcdc1", TPS65218_DCDC_1,
  208. REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64,
  209. TPS65218_REG_CONTROL_DCDC1,
  210. TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1,
  211. TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges,
  212. 2, 4000, 0, TPS65218_REG_SEQ3,
  213. TPS65218_SEQ3_DC1_SEQ_MASK, NULL, 0),
  214. TPS65218_REGULATOR("DCDC2", "regulator-dcdc2", TPS65218_DCDC_2,
  215. REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64,
  216. TPS65218_REG_CONTROL_DCDC2,
  217. TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1,
  218. TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges,
  219. 2, 4000, 0, TPS65218_REG_SEQ3,
  220. TPS65218_SEQ3_DC2_SEQ_MASK, NULL, 0),
  221. TPS65218_REGULATOR("DCDC3", "regulator-dcdc3", TPS65218_DCDC_3,
  222. REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64,
  223. TPS65218_REG_CONTROL_DCDC3,
  224. TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
  225. TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2,
  226. 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC3_SEQ_MASK,
  227. NULL, 0),
  228. TPS65218_REGULATOR("DCDC4", "regulator-dcdc4", TPS65218_DCDC_4,
  229. REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 53,
  230. TPS65218_REG_CONTROL_DCDC4,
  231. TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1,
  232. TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2,
  233. 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC4_SEQ_MASK,
  234. NULL, 0),
  235. TPS65218_REGULATOR("DCDC5", "regulator-dcdc5", TPS65218_DCDC_5,
  236. REGULATOR_VOLTAGE, tps65218_dcdc56_pmic_ops, 1, -1,
  237. -1, TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC5_EN, 0,
  238. 0, NULL, 0, 0, 1000000, TPS65218_REG_SEQ5,
  239. TPS65218_SEQ5_DC5_SEQ_MASK, NULL, 0),
  240. TPS65218_REGULATOR("DCDC6", "regulator-dcdc6", TPS65218_DCDC_6,
  241. REGULATOR_VOLTAGE, tps65218_dcdc56_pmic_ops, 1, -1,
  242. -1, TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC6_EN, 0,
  243. 0, NULL, 0, 0, 1800000, TPS65218_REG_SEQ5,
  244. TPS65218_SEQ5_DC6_SEQ_MASK, NULL, 0),
  245. TPS65218_REGULATOR("LDO1", "regulator-ldo1", TPS65218_LDO_1,
  246. REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64,
  247. TPS65218_REG_CONTROL_LDO1,
  248. TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
  249. TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
  250. 2, 0, 0, TPS65218_REG_SEQ6,
  251. TPS65218_SEQ6_LDO1_SEQ_MASK, NULL, 0),
  252. TPS65218_REGULATOR("LS2", "regulator-ls2", TPS65218_LS_2,
  253. REGULATOR_CURRENT, tps65218_ls23_ops, 0, 0, 0,
  254. TPS65218_REG_ENABLE2, TPS65218_ENABLE2_LS2_EN,
  255. TPS65218_REG_CONFIG2, TPS65218_CONFIG2_LS2ILIM_MASK,
  256. NULL, 0, 0, 0, 0, 0, ls3_currents,
  257. ARRAY_SIZE(ls3_currents)),
  258. TPS65218_REGULATOR("LS3", "regulator-ls3", TPS65218_LS_3,
  259. REGULATOR_CURRENT, tps65218_ls23_ops, 0, 0, 0,
  260. TPS65218_REG_ENABLE2, TPS65218_ENABLE2_LS3_EN,
  261. TPS65218_REG_CONFIG2, TPS65218_CONFIG2_LS3ILIM_MASK,
  262. NULL, 0, 0, 0, 0, 0, ls3_currents,
  263. ARRAY_SIZE(ls3_currents)),
  264. };
  265. static int tps65218_regulator_probe(struct platform_device *pdev)
  266. {
  267. struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
  268. struct regulator_dev *rdev;
  269. struct regulator_config config = { };
  270. int i, ret;
  271. unsigned int val;
  272. config.dev = &pdev->dev;
  273. config.dev->of_node = tps->dev->of_node;
  274. config.driver_data = tps;
  275. config.regmap = tps->regmap;
  276. /* Allocate memory for strobes */
  277. tps->strobes = devm_kcalloc(&pdev->dev,
  278. TPS65218_NUM_REGULATOR, sizeof(u8),
  279. GFP_KERNEL);
  280. if (!tps->strobes)
  281. return -ENOMEM;
  282. for (i = 0; i < ARRAY_SIZE(regulators); i++) {
  283. rdev = devm_regulator_register(&pdev->dev, &regulators[i],
  284. &config);
  285. if (IS_ERR(rdev)) {
  286. dev_err(tps->dev, "failed to register %s regulator\n",
  287. pdev->name);
  288. return PTR_ERR(rdev);
  289. }
  290. ret = regmap_read(tps->regmap, regulators[i].bypass_reg, &val);
  291. if (ret)
  292. return ret;
  293. tps->strobes[i] = val & regulators[i].bypass_mask;
  294. }
  295. return 0;
  296. }
  297. static const struct platform_device_id tps65218_regulator_id_table[] = {
  298. { "tps65218-regulator", },
  299. { /* sentinel */ }
  300. };
  301. MODULE_DEVICE_TABLE(platform, tps65218_regulator_id_table);
  302. static struct platform_driver tps65218_regulator_driver = {
  303. .driver = {
  304. .name = "tps65218-pmic",
  305. },
  306. .probe = tps65218_regulator_probe,
  307. .id_table = tps65218_regulator_id_table,
  308. };
  309. module_platform_driver(tps65218_regulator_driver);
  310. MODULE_AUTHOR("J Keerthy <[email protected]>");
  311. MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
  312. MODULE_ALIAS("platform:tps65218-pmic");
  313. MODULE_LICENSE("GPL v2");