tps65218-regulator.c 12 KB

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