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