tps65217-regulator.c 9.7 KB

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