tps65218-regulator.c 13 KB

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