tps65217-regulator.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * tps65217-regulator.c
  3. *
  4. * Regulator driver for TPS65217 PMIC
  5. *
  6. * Copyright (C) 2011 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 as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License 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/regulator/of_regulator.h>
  24. #include <linux/regulator/driver.h>
  25. #include <linux/regulator/machine.h>
  26. #include <linux/mfd/tps65217.h>
  27. #define TPS65217_REGULATOR(_name, _id, _ops, _n, _vr, _vm, _em, _t, _lr, _nlr) \
  28. { \
  29. .name = _name, \
  30. .id = _id, \
  31. .ops = &_ops, \
  32. .n_voltages = _n, \
  33. .type = REGULATOR_VOLTAGE, \
  34. .owner = THIS_MODULE, \
  35. .vsel_reg = _vr, \
  36. .vsel_mask = _vm, \
  37. .enable_reg = TPS65217_REG_ENABLE, \
  38. .enable_mask = _em, \
  39. .volt_table = _t, \
  40. .linear_ranges = _lr, \
  41. .n_linear_ranges = _nlr, \
  42. } \
  43. static const unsigned int LDO1_VSEL_table[] = {
  44. 1000000, 1100000, 1200000, 1250000,
  45. 1300000, 1350000, 1400000, 1500000,
  46. 1600000, 1800000, 2500000, 2750000,
  47. 2800000, 3000000, 3100000, 3300000,
  48. };
  49. static const struct regulator_linear_range tps65217_uv1_ranges[] = {
  50. { .min_uV = 900000, .max_uV = 1500000, .min_sel = 0, .max_sel = 24,
  51. .uV_step = 25000 },
  52. { .min_uV = 1550000, .max_uV = 1800000, .min_sel = 25, .max_sel = 30,
  53. .uV_step = 50000 },
  54. { .min_uV = 1850000, .max_uV = 2900000, .min_sel = 31, .max_sel = 52,
  55. .uV_step = 50000 },
  56. { .min_uV = 3000000, .max_uV = 3200000, .min_sel = 53, .max_sel = 55,
  57. .uV_step = 100000 },
  58. { .min_uV = 3300000, .max_uV = 3300000, .min_sel = 56, .max_sel = 62,
  59. .uV_step = 0 },
  60. };
  61. static const struct regulator_linear_range tps65217_uv2_ranges[] = {
  62. { .min_uV = 1500000, .max_uV = 1900000, .min_sel = 0, .max_sel = 8,
  63. .uV_step = 50000 },
  64. { .min_uV = 2000000, .max_uV = 2400000, .min_sel = 9, .max_sel = 13,
  65. .uV_step = 100000 },
  66. { .min_uV = 2450000, .max_uV = 3300000, .min_sel = 14, .max_sel = 31,
  67. .uV_step = 50000 },
  68. };
  69. static int tps65217_pmic_enable(struct regulator_dev *dev)
  70. {
  71. struct tps65217 *tps = rdev_get_drvdata(dev);
  72. unsigned int rid = rdev_get_id(dev);
  73. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  74. return -EINVAL;
  75. /* Enable the regulator and password protection is level 1 */
  76. return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
  77. dev->desc->enable_mask, dev->desc->enable_mask,
  78. TPS65217_PROTECT_L1);
  79. }
  80. static int tps65217_pmic_disable(struct regulator_dev *dev)
  81. {
  82. struct tps65217 *tps = rdev_get_drvdata(dev);
  83. unsigned int rid = rdev_get_id(dev);
  84. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  85. return -EINVAL;
  86. /* Disable the regulator and password protection is level 1 */
  87. return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
  88. dev->desc->enable_mask, TPS65217_PROTECT_L1);
  89. }
  90. static int tps65217_pmic_set_voltage_sel(struct regulator_dev *dev,
  91. unsigned selector)
  92. {
  93. int ret;
  94. struct tps65217 *tps = rdev_get_drvdata(dev);
  95. unsigned int rid = rdev_get_id(dev);
  96. /* Set the voltage based on vsel value and write protect level is 2 */
  97. ret = tps65217_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  98. selector, TPS65217_PROTECT_L2);
  99. /* Set GO bit for DCDCx to initiate voltage transistion */
  100. switch (rid) {
  101. case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
  102. ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
  103. TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
  104. TPS65217_PROTECT_L2);
  105. break;
  106. }
  107. return ret;
  108. }
  109. /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
  110. static struct regulator_ops tps65217_pmic_ops = {
  111. .is_enabled = regulator_is_enabled_regmap,
  112. .enable = tps65217_pmic_enable,
  113. .disable = tps65217_pmic_disable,
  114. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  115. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  116. .list_voltage = regulator_list_voltage_linear_range,
  117. .map_voltage = regulator_map_voltage_linear_range,
  118. };
  119. /* Operations permitted on LDO1 */
  120. static struct regulator_ops tps65217_pmic_ldo1_ops = {
  121. .is_enabled = regulator_is_enabled_regmap,
  122. .enable = tps65217_pmic_enable,
  123. .disable = tps65217_pmic_disable,
  124. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  125. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  126. .list_voltage = regulator_list_voltage_table,
  127. };
  128. static const struct regulator_desc regulators[] = {
  129. TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, tps65217_pmic_ops, 64,
  130. TPS65217_REG_DEFDCDC1, TPS65217_DEFDCDCX_DCDC_MASK,
  131. TPS65217_ENABLE_DC1_EN, NULL, tps65217_uv1_ranges,
  132. 2), /* DCDC1 voltage range: 900000 ~ 1800000 */
  133. TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, tps65217_pmic_ops, 64,
  134. TPS65217_REG_DEFDCDC2, TPS65217_DEFDCDCX_DCDC_MASK,
  135. TPS65217_ENABLE_DC2_EN, NULL, tps65217_uv1_ranges,
  136. ARRAY_SIZE(tps65217_uv1_ranges)),
  137. TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, tps65217_pmic_ops, 64,
  138. TPS65217_REG_DEFDCDC3, TPS65217_DEFDCDCX_DCDC_MASK,
  139. TPS65217_ENABLE_DC3_EN, NULL, tps65217_uv1_ranges,
  140. 1), /* DCDC3 voltage range: 900000 ~ 1500000 */
  141. TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, tps65217_pmic_ldo1_ops, 16,
  142. TPS65217_REG_DEFLDO1, TPS65217_DEFLDO1_LDO1_MASK,
  143. TPS65217_ENABLE_LDO1_EN, LDO1_VSEL_table, NULL, 0),
  144. TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, tps65217_pmic_ops, 64,
  145. TPS65217_REG_DEFLDO2, TPS65217_DEFLDO2_LDO2_MASK,
  146. TPS65217_ENABLE_LDO2_EN, NULL, tps65217_uv1_ranges,
  147. ARRAY_SIZE(tps65217_uv1_ranges)),
  148. TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, tps65217_pmic_ops, 32,
  149. TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK,
  150. TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN,
  151. NULL, tps65217_uv2_ranges,
  152. ARRAY_SIZE(tps65217_uv2_ranges)),
  153. TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, tps65217_pmic_ops, 32,
  154. TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK,
  155. TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN,
  156. NULL, tps65217_uv2_ranges,
  157. ARRAY_SIZE(tps65217_uv2_ranges)),
  158. };
  159. #ifdef CONFIG_OF
  160. static struct of_regulator_match reg_matches[] = {
  161. { .name = "dcdc1", .driver_data = (void *)TPS65217_DCDC_1 },
  162. { .name = "dcdc2", .driver_data = (void *)TPS65217_DCDC_2 },
  163. { .name = "dcdc3", .driver_data = (void *)TPS65217_DCDC_3 },
  164. { .name = "ldo1", .driver_data = (void *)TPS65217_LDO_1 },
  165. { .name = "ldo2", .driver_data = (void *)TPS65217_LDO_2 },
  166. { .name = "ldo3", .driver_data = (void *)TPS65217_LDO_3 },
  167. { .name = "ldo4", .driver_data = (void *)TPS65217_LDO_4 },
  168. };
  169. static struct tps65217_board *tps65217_parse_dt(struct platform_device *pdev)
  170. {
  171. struct tps65217 *tps = dev_get_drvdata(pdev->dev.parent);
  172. struct device_node *node = tps->dev->of_node;
  173. struct tps65217_board *pdata;
  174. struct device_node *regs;
  175. int i, count;
  176. regs = of_find_node_by_name(node, "regulators");
  177. if (!regs)
  178. return NULL;
  179. count = of_regulator_match(&pdev->dev, regs, reg_matches,
  180. TPS65217_NUM_REGULATOR);
  181. of_node_put(regs);
  182. if ((count < 0) || (count > TPS65217_NUM_REGULATOR))
  183. return NULL;
  184. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  185. if (!pdata)
  186. return NULL;
  187. for (i = 0; i < count; i++) {
  188. if (!reg_matches[i].init_data || !reg_matches[i].of_node)
  189. continue;
  190. pdata->tps65217_init_data[i] = reg_matches[i].init_data;
  191. pdata->of_node[i] = reg_matches[i].of_node;
  192. }
  193. return pdata;
  194. }
  195. #else
  196. static struct tps65217_board *tps65217_parse_dt(struct platform_device *pdev)
  197. {
  198. return NULL;
  199. }
  200. #endif
  201. static int tps65217_regulator_probe(struct platform_device *pdev)
  202. {
  203. struct tps65217 *tps = dev_get_drvdata(pdev->dev.parent);
  204. struct tps65217_board *pdata = dev_get_platdata(tps->dev);
  205. struct regulator_init_data *reg_data;
  206. struct regulator_dev *rdev;
  207. struct regulator_config config = { };
  208. int i, ret;
  209. if (tps->dev->of_node)
  210. pdata = tps65217_parse_dt(pdev);
  211. if (!pdata) {
  212. dev_err(&pdev->dev, "Platform data not found\n");
  213. return -EINVAL;
  214. }
  215. if (tps65217_chip_id(tps) != TPS65217) {
  216. dev_err(&pdev->dev, "Invalid tps chip version\n");
  217. return -ENODEV;
  218. }
  219. platform_set_drvdata(pdev, tps);
  220. for (i = 0; i < TPS65217_NUM_REGULATOR; i++) {
  221. reg_data = pdata->tps65217_init_data[i];
  222. /*
  223. * Regulator API handles empty constraints but not NULL
  224. * constraints
  225. */
  226. if (!reg_data)
  227. continue;
  228. /* Register the regulators */
  229. config.dev = tps->dev;
  230. config.init_data = reg_data;
  231. config.driver_data = tps;
  232. config.regmap = tps->regmap;
  233. if (tps->dev->of_node)
  234. config.of_node = pdata->of_node[i];
  235. rdev = regulator_register(&regulators[i], &config);
  236. if (IS_ERR(rdev)) {
  237. dev_err(tps->dev, "failed to register %s regulator\n",
  238. pdev->name);
  239. ret = PTR_ERR(rdev);
  240. goto err_unregister_regulator;
  241. }
  242. /* Save regulator for cleanup */
  243. tps->rdev[i] = rdev;
  244. }
  245. return 0;
  246. err_unregister_regulator:
  247. while (--i >= 0)
  248. regulator_unregister(tps->rdev[i]);
  249. return ret;
  250. }
  251. static int tps65217_regulator_remove(struct platform_device *pdev)
  252. {
  253. struct tps65217 *tps = platform_get_drvdata(pdev);
  254. unsigned int i;
  255. for (i = 0; i < TPS65217_NUM_REGULATOR; i++)
  256. regulator_unregister(tps->rdev[i]);
  257. return 0;
  258. }
  259. static struct platform_driver tps65217_regulator_driver = {
  260. .driver = {
  261. .name = "tps65217-pmic",
  262. },
  263. .probe = tps65217_regulator_probe,
  264. .remove = tps65217_regulator_remove,
  265. };
  266. static int __init tps65217_regulator_init(void)
  267. {
  268. return platform_driver_register(&tps65217_regulator_driver);
  269. }
  270. subsys_initcall(tps65217_regulator_init);
  271. static void __exit tps65217_regulator_exit(void)
  272. {
  273. platform_driver_unregister(&tps65217_regulator_driver);
  274. }
  275. module_exit(tps65217_regulator_exit);
  276. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  277. MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
  278. MODULE_ALIAS("platform:tps65217-pmic");
  279. MODULE_LICENSE("GPL v2");