pwm-regulator.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Regulator driver for PWM Regulators
  3. *
  4. * Copyright (C) 2014 - STMicroelectronics Inc.
  5. *
  6. * Author: Lee Jones <lee.jones@linaro.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/machine.h>
  18. #include <linux/regulator/of_regulator.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/pwm.h>
  22. struct pwm_regulator_data {
  23. /* Shared */
  24. struct pwm_device *pwm;
  25. /* Voltage table */
  26. struct pwm_voltages *duty_cycle_table;
  27. /* regulator descriptor */
  28. struct regulator_desc desc;
  29. /* Regulator ops */
  30. struct regulator_ops ops;
  31. int state;
  32. /* Continuous voltage */
  33. int volt_uV;
  34. };
  35. struct pwm_voltages {
  36. unsigned int uV;
  37. unsigned int dutycycle;
  38. };
  39. /**
  40. * Voltage table call-backs
  41. */
  42. static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
  43. {
  44. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  45. return drvdata->state;
  46. }
  47. static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
  48. unsigned selector)
  49. {
  50. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  51. struct pwm_args pargs;
  52. int dutycycle;
  53. int ret;
  54. pwm_get_args(drvdata->pwm, &pargs);
  55. dutycycle = (pargs.period *
  56. drvdata->duty_cycle_table[selector].dutycycle) / 100;
  57. ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);
  58. if (ret) {
  59. dev_err(&rdev->dev, "Failed to configure PWM\n");
  60. return ret;
  61. }
  62. drvdata->state = selector;
  63. return 0;
  64. }
  65. static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
  66. unsigned selector)
  67. {
  68. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  69. if (selector >= rdev->desc->n_voltages)
  70. return -EINVAL;
  71. return drvdata->duty_cycle_table[selector].uV;
  72. }
  73. static int pwm_regulator_enable(struct regulator_dev *dev)
  74. {
  75. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  76. return pwm_enable(drvdata->pwm);
  77. }
  78. static int pwm_regulator_disable(struct regulator_dev *dev)
  79. {
  80. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  81. pwm_disable(drvdata->pwm);
  82. return 0;
  83. }
  84. static int pwm_regulator_is_enabled(struct regulator_dev *dev)
  85. {
  86. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  87. return pwm_is_enabled(drvdata->pwm);
  88. }
  89. /**
  90. * Continuous voltage call-backs
  91. */
  92. static int pwm_voltage_to_duty_cycle_percentage(struct regulator_dev *rdev, int req_uV)
  93. {
  94. int min_uV = rdev->constraints->min_uV;
  95. int max_uV = rdev->constraints->max_uV;
  96. int diff = max_uV - min_uV;
  97. return ((req_uV * 100) - (min_uV * 100)) / diff;
  98. }
  99. static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
  100. {
  101. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  102. return drvdata->volt_uV;
  103. }
  104. static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
  105. int min_uV, int max_uV,
  106. unsigned *selector)
  107. {
  108. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  109. unsigned int ramp_delay = rdev->constraints->ramp_delay;
  110. struct pwm_args pargs;
  111. int duty_cycle;
  112. int ret;
  113. pwm_get_args(drvdata->pwm, &pargs);
  114. duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
  115. ret = pwm_config(drvdata->pwm, (pargs.period / 100) * duty_cycle,
  116. pargs.period);
  117. if (ret) {
  118. dev_err(&rdev->dev, "Failed to configure PWM\n");
  119. return ret;
  120. }
  121. ret = pwm_enable(drvdata->pwm);
  122. if (ret) {
  123. dev_err(&rdev->dev, "Failed to enable PWM\n");
  124. return ret;
  125. }
  126. drvdata->volt_uV = min_uV;
  127. /* Delay required by PWM regulator to settle to the new voltage */
  128. usleep_range(ramp_delay, ramp_delay + 1000);
  129. return 0;
  130. }
  131. static struct regulator_ops pwm_regulator_voltage_table_ops = {
  132. .set_voltage_sel = pwm_regulator_set_voltage_sel,
  133. .get_voltage_sel = pwm_regulator_get_voltage_sel,
  134. .list_voltage = pwm_regulator_list_voltage,
  135. .map_voltage = regulator_map_voltage_iterate,
  136. .enable = pwm_regulator_enable,
  137. .disable = pwm_regulator_disable,
  138. .is_enabled = pwm_regulator_is_enabled,
  139. };
  140. static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
  141. .get_voltage = pwm_regulator_get_voltage,
  142. .set_voltage = pwm_regulator_set_voltage,
  143. .enable = pwm_regulator_enable,
  144. .disable = pwm_regulator_disable,
  145. .is_enabled = pwm_regulator_is_enabled,
  146. };
  147. static struct regulator_desc pwm_regulator_desc = {
  148. .name = "pwm-regulator",
  149. .type = REGULATOR_VOLTAGE,
  150. .owner = THIS_MODULE,
  151. .supply_name = "pwm",
  152. };
  153. static int pwm_regulator_init_table(struct platform_device *pdev,
  154. struct pwm_regulator_data *drvdata)
  155. {
  156. struct device_node *np = pdev->dev.of_node;
  157. struct pwm_voltages *duty_cycle_table;
  158. unsigned int length = 0;
  159. int ret;
  160. of_find_property(np, "voltage-table", &length);
  161. if ((length < sizeof(*duty_cycle_table)) ||
  162. (length % sizeof(*duty_cycle_table))) {
  163. dev_err(&pdev->dev,
  164. "voltage-table length(%d) is invalid\n",
  165. length);
  166. return -EINVAL;
  167. }
  168. duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
  169. if (!duty_cycle_table)
  170. return -ENOMEM;
  171. ret = of_property_read_u32_array(np, "voltage-table",
  172. (u32 *)duty_cycle_table,
  173. length / sizeof(u32));
  174. if (ret) {
  175. dev_err(&pdev->dev, "Failed to read voltage-table\n");
  176. return ret;
  177. }
  178. drvdata->duty_cycle_table = duty_cycle_table;
  179. memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
  180. sizeof(drvdata->ops));
  181. drvdata->desc.ops = &drvdata->ops;
  182. drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
  183. return 0;
  184. }
  185. static int pwm_regulator_init_continuous(struct platform_device *pdev,
  186. struct pwm_regulator_data *drvdata)
  187. {
  188. memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
  189. sizeof(drvdata->ops));
  190. drvdata->desc.ops = &drvdata->ops;
  191. drvdata->desc.continuous_voltage_range = true;
  192. return 0;
  193. }
  194. static int pwm_regulator_probe(struct platform_device *pdev)
  195. {
  196. const struct regulator_init_data *init_data;
  197. struct pwm_regulator_data *drvdata;
  198. struct regulator_dev *regulator;
  199. struct regulator_config config = { };
  200. struct device_node *np = pdev->dev.of_node;
  201. int ret;
  202. if (!np) {
  203. dev_err(&pdev->dev, "Device Tree node missing\n");
  204. return -EINVAL;
  205. }
  206. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  207. if (!drvdata)
  208. return -ENOMEM;
  209. memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
  210. if (of_find_property(np, "voltage-table", NULL))
  211. ret = pwm_regulator_init_table(pdev, drvdata);
  212. else
  213. ret = pwm_regulator_init_continuous(pdev, drvdata);
  214. if (ret)
  215. return ret;
  216. init_data = of_get_regulator_init_data(&pdev->dev, np,
  217. &drvdata->desc);
  218. if (!init_data)
  219. return -ENOMEM;
  220. config.of_node = np;
  221. config.dev = &pdev->dev;
  222. config.driver_data = drvdata;
  223. config.init_data = init_data;
  224. drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
  225. if (IS_ERR(drvdata->pwm)) {
  226. dev_err(&pdev->dev, "Failed to get PWM\n");
  227. return PTR_ERR(drvdata->pwm);
  228. }
  229. /*
  230. * FIXME: pwm_apply_args() should be removed when switching to the
  231. * atomic PWM API.
  232. */
  233. pwm_apply_args(drvdata->pwm);
  234. regulator = devm_regulator_register(&pdev->dev,
  235. &drvdata->desc, &config);
  236. if (IS_ERR(regulator)) {
  237. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  238. drvdata->desc.name);
  239. return PTR_ERR(regulator);
  240. }
  241. return 0;
  242. }
  243. static const struct of_device_id pwm_of_match[] = {
  244. { .compatible = "pwm-regulator" },
  245. { },
  246. };
  247. MODULE_DEVICE_TABLE(of, pwm_of_match);
  248. static struct platform_driver pwm_regulator_driver = {
  249. .driver = {
  250. .name = "pwm-regulator",
  251. .of_match_table = of_match_ptr(pwm_of_match),
  252. },
  253. .probe = pwm_regulator_probe,
  254. };
  255. module_platform_driver(pwm_regulator_driver);
  256. MODULE_LICENSE("GPL");
  257. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
  258. MODULE_DESCRIPTION("PWM Regulator Driver");
  259. MODULE_ALIAS("platform:pwm-regulator");