tps65218-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. if (!tps->info[rid]->strobe) {
  158. if (rid == TPS65218_DCDC_3)
  159. tps->info[rid]->strobe = 3;
  160. else
  161. return -EINVAL;
  162. }
  163. return tps65218_set_bits(tps, dev->desc->bypass_reg,
  164. dev->desc->bypass_mask,
  165. tps->info[rid]->strobe,
  166. TPS65218_PROTECT_L1);
  167. }
  168. /* Operations permitted on DCDC1, DCDC2 */
  169. static struct regulator_ops tps65218_dcdc12_ops = {
  170. .is_enabled = regulator_is_enabled_regmap,
  171. .enable = tps65218_pmic_enable,
  172. .disable = tps65218_pmic_disable,
  173. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  174. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  175. .list_voltage = regulator_list_voltage_linear_range,
  176. .map_voltage = regulator_map_voltage_linear_range,
  177. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  178. .set_suspend_enable = tps65218_pmic_set_suspend_enable,
  179. .set_suspend_disable = tps65218_pmic_set_suspend_disable,
  180. };
  181. /* Operations permitted on DCDC3, DCDC4 and LDO1 */
  182. static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
  183. .is_enabled = regulator_is_enabled_regmap,
  184. .enable = tps65218_pmic_enable,
  185. .disable = tps65218_pmic_disable,
  186. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  187. .set_voltage_sel = tps65218_pmic_set_voltage_sel,
  188. .list_voltage = regulator_list_voltage_linear_range,
  189. .map_voltage = regulator_map_voltage_linear_range,
  190. .set_suspend_enable = tps65218_pmic_set_suspend_enable,
  191. .set_suspend_disable = tps65218_pmic_set_suspend_disable,
  192. };
  193. static const int ls3_currents[] = { 100, 200, 500, 1000 };
  194. static int tps65218_pmic_set_input_current_lim(struct regulator_dev *dev,
  195. int lim_uA)
  196. {
  197. unsigned int index = 0;
  198. unsigned int num_currents = ARRAY_SIZE(ls3_currents);
  199. struct tps65218 *tps = rdev_get_drvdata(dev);
  200. while (index < num_currents && ls3_currents[index] != lim_uA)
  201. index++;
  202. if (index == num_currents)
  203. return -EINVAL;
  204. return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
  205. index << 2, TPS65218_PROTECT_L1);
  206. }
  207. static int tps65218_pmic_set_current_limit(struct regulator_dev *dev,
  208. int min_uA, int max_uA)
  209. {
  210. int index = 0;
  211. unsigned int num_currents = ARRAY_SIZE(ls3_currents);
  212. struct tps65218 *tps = rdev_get_drvdata(dev);
  213. while (index < num_currents && ls3_currents[index] < max_uA)
  214. index++;
  215. index--;
  216. if (index < 0 || ls3_currents[index] < min_uA)
  217. return -EINVAL;
  218. return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
  219. index << 2, TPS65218_PROTECT_L1);
  220. }
  221. static int tps65218_pmic_get_current_limit(struct regulator_dev *dev)
  222. {
  223. int retval;
  224. unsigned int index;
  225. struct tps65218 *tps = rdev_get_drvdata(dev);
  226. retval = tps65218_reg_read(tps, dev->desc->csel_reg, &index);
  227. if (retval < 0)
  228. return retval;
  229. index = (index & dev->desc->csel_mask) >> 2;
  230. return ls3_currents[index];
  231. }
  232. static struct regulator_ops tps65218_ls3_ops = {
  233. .is_enabled = regulator_is_enabled_regmap,
  234. .enable = tps65218_pmic_enable,
  235. .disable = tps65218_pmic_disable,
  236. .set_input_current_limit = tps65218_pmic_set_input_current_lim,
  237. .set_current_limit = tps65218_pmic_set_current_limit,
  238. .get_current_limit = tps65218_pmic_get_current_limit,
  239. };
  240. /* Operations permitted on DCDC5, DCDC6 */
  241. static struct regulator_ops tps65218_dcdc56_pmic_ops = {
  242. .is_enabled = regulator_is_enabled_regmap,
  243. .enable = tps65218_pmic_enable,
  244. .disable = tps65218_pmic_disable,
  245. .set_suspend_enable = tps65218_pmic_set_suspend_enable,
  246. .set_suspend_disable = tps65218_pmic_set_suspend_disable,
  247. };
  248. static const struct regulator_desc regulators[] = {
  249. TPS65218_REGULATOR("DCDC1", TPS65218_DCDC_1, REGULATOR_VOLTAGE,
  250. tps65218_dcdc12_ops, 64, TPS65218_REG_CONTROL_DCDC1,
  251. TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1,
  252. TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges,
  253. 2, 4000, 0, TPS65218_REG_SEQ3,
  254. TPS65218_SEQ3_DC1_SEQ_MASK),
  255. TPS65218_REGULATOR("DCDC2", TPS65218_DCDC_2, REGULATOR_VOLTAGE,
  256. tps65218_dcdc12_ops, 64, TPS65218_REG_CONTROL_DCDC2,
  257. TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1,
  258. TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges,
  259. 2, 4000, 0, TPS65218_REG_SEQ3,
  260. TPS65218_SEQ3_DC2_SEQ_MASK),
  261. TPS65218_REGULATOR("DCDC3", TPS65218_DCDC_3, REGULATOR_VOLTAGE,
  262. tps65218_ldo1_dcdc34_ops, 64,
  263. TPS65218_REG_CONTROL_DCDC3,
  264. TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
  265. TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2,
  266. 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC3_SEQ_MASK),
  267. TPS65218_REGULATOR("DCDC4", TPS65218_DCDC_4, REGULATOR_VOLTAGE,
  268. tps65218_ldo1_dcdc34_ops, 53,
  269. TPS65218_REG_CONTROL_DCDC4,
  270. TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1,
  271. TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2,
  272. 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC4_SEQ_MASK),
  273. TPS65218_REGULATOR("DCDC5", TPS65218_DCDC_5, REGULATOR_VOLTAGE,
  274. tps65218_dcdc56_pmic_ops, 1, -1, -1,
  275. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC5_EN, 0, 0,
  276. NULL, 0, 0, 1000000, TPS65218_REG_SEQ5,
  277. TPS65218_SEQ5_DC5_SEQ_MASK),
  278. TPS65218_REGULATOR("DCDC6", TPS65218_DCDC_6, REGULATOR_VOLTAGE,
  279. tps65218_dcdc56_pmic_ops, 1, -1, -1,
  280. TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC6_EN, 0, 0,
  281. NULL, 0, 0, 1800000, TPS65218_REG_SEQ5,
  282. TPS65218_SEQ5_DC6_SEQ_MASK),
  283. TPS65218_REGULATOR("LDO1", TPS65218_LDO_1, REGULATOR_VOLTAGE,
  284. tps65218_ldo1_dcdc34_ops, 64,
  285. TPS65218_REG_CONTROL_LDO1,
  286. TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
  287. TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
  288. 2, 0, 0, TPS65218_REG_SEQ6,
  289. TPS65218_SEQ6_LDO1_SEQ_MASK),
  290. TPS65218_REGULATOR("LS3", TPS65218_LS_3, REGULATOR_CURRENT,
  291. tps65218_ls3_ops, 0, 0, 0, TPS65218_REG_ENABLE2,
  292. TPS65218_ENABLE2_LS3_EN, TPS65218_REG_CONFIG2,
  293. TPS65218_CONFIG2_LS3ILIM_MASK, NULL, 0, 0, 0, 0, 0),
  294. };
  295. static int tps65218_regulator_probe(struct platform_device *pdev)
  296. {
  297. struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
  298. struct regulator_init_data *init_data;
  299. const struct tps_info *template;
  300. struct regulator_dev *rdev;
  301. const struct of_device_id *match;
  302. struct regulator_config config = { };
  303. int id, ret;
  304. unsigned int val;
  305. match = of_match_device(tps65218_of_match, &pdev->dev);
  306. if (!match)
  307. return -ENODEV;
  308. template = match->data;
  309. id = template->id;
  310. init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
  311. &regulators[id]);
  312. platform_set_drvdata(pdev, tps);
  313. tps->info[id] = &tps65218_pmic_regs[id];
  314. config.dev = &pdev->dev;
  315. config.init_data = init_data;
  316. config.driver_data = tps;
  317. config.regmap = tps->regmap;
  318. config.of_node = pdev->dev.of_node;
  319. rdev = devm_regulator_register(&pdev->dev, &regulators[id], &config);
  320. if (IS_ERR(rdev)) {
  321. dev_err(tps->dev, "failed to register %s regulator\n",
  322. pdev->name);
  323. return PTR_ERR(rdev);
  324. }
  325. ret = tps65218_reg_read(tps, regulators[id].bypass_reg, &val);
  326. if (ret)
  327. return ret;
  328. tps->info[id]->strobe = val & regulators[id].bypass_mask;
  329. return 0;
  330. }
  331. static struct platform_driver tps65218_regulator_driver = {
  332. .driver = {
  333. .name = "tps65218-pmic",
  334. .of_match_table = tps65218_of_match,
  335. },
  336. .probe = tps65218_regulator_probe,
  337. };
  338. module_platform_driver(tps65218_regulator_driver);
  339. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
  340. MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
  341. MODULE_ALIAS("platform:tps65218-pmic");
  342. MODULE_LICENSE("GPL v2");