pwm-regulator.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. int state;
  28. /* Continuous voltage */
  29. int volt_uV;
  30. };
  31. struct pwm_voltages {
  32. unsigned int uV;
  33. unsigned int dutycycle;
  34. };
  35. /**
  36. * Voltage table call-backs
  37. */
  38. static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
  39. {
  40. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  41. return drvdata->state;
  42. }
  43. static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
  44. unsigned selector)
  45. {
  46. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  47. unsigned int pwm_reg_period;
  48. int dutycycle;
  49. int ret;
  50. pwm_reg_period = pwm_get_period(drvdata->pwm);
  51. dutycycle = (pwm_reg_period *
  52. drvdata->duty_cycle_table[selector].dutycycle) / 100;
  53. ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
  54. if (ret) {
  55. dev_err(&rdev->dev, "Failed to configure PWM\n");
  56. return ret;
  57. }
  58. drvdata->state = selector;
  59. return 0;
  60. }
  61. static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
  62. unsigned selector)
  63. {
  64. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  65. if (selector >= rdev->desc->n_voltages)
  66. return -EINVAL;
  67. return drvdata->duty_cycle_table[selector].uV;
  68. }
  69. static int pwm_regulator_enable(struct regulator_dev *dev)
  70. {
  71. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  72. return pwm_enable(drvdata->pwm);
  73. }
  74. static int pwm_regulator_disable(struct regulator_dev *dev)
  75. {
  76. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  77. pwm_disable(drvdata->pwm);
  78. return 0;
  79. }
  80. static int pwm_regulator_is_enabled(struct regulator_dev *dev)
  81. {
  82. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  83. return pwm_is_enabled(drvdata->pwm);
  84. }
  85. /**
  86. * Continuous voltage call-backs
  87. */
  88. static int pwm_voltage_to_duty_cycle_percentage(struct regulator_dev *rdev, int req_uV)
  89. {
  90. int min_uV = rdev->constraints->min_uV;
  91. int max_uV = rdev->constraints->max_uV;
  92. int diff = max_uV - min_uV;
  93. return 100 - (((req_uV * 100) - (min_uV * 100)) / diff);
  94. }
  95. static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
  96. {
  97. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  98. return drvdata->volt_uV;
  99. }
  100. static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
  101. int min_uV, int max_uV,
  102. unsigned *selector)
  103. {
  104. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  105. unsigned int ramp_delay = rdev->constraints->ramp_delay;
  106. unsigned int period = pwm_get_period(drvdata->pwm);
  107. int duty_cycle;
  108. int ret;
  109. duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
  110. ret = pwm_config(drvdata->pwm, (period / 100) * duty_cycle, period);
  111. if (ret) {
  112. dev_err(&rdev->dev, "Failed to configure PWM\n");
  113. return ret;
  114. }
  115. ret = pwm_enable(drvdata->pwm);
  116. if (ret) {
  117. dev_err(&rdev->dev, "Failed to enable PWM\n");
  118. return ret;
  119. }
  120. drvdata->volt_uV = min_uV;
  121. /* Delay required by PWM regulator to settle to the new voltage */
  122. usleep_range(ramp_delay, ramp_delay + 1000);
  123. return 0;
  124. }
  125. static struct regulator_ops pwm_regulator_voltage_table_ops = {
  126. .set_voltage_sel = pwm_regulator_set_voltage_sel,
  127. .get_voltage_sel = pwm_regulator_get_voltage_sel,
  128. .list_voltage = pwm_regulator_list_voltage,
  129. .map_voltage = regulator_map_voltage_iterate,
  130. .enable = pwm_regulator_enable,
  131. .disable = pwm_regulator_disable,
  132. .is_enabled = pwm_regulator_is_enabled,
  133. };
  134. static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
  135. .get_voltage = pwm_regulator_get_voltage,
  136. .set_voltage = pwm_regulator_set_voltage,
  137. .enable = pwm_regulator_enable,
  138. .disable = pwm_regulator_disable,
  139. .is_enabled = pwm_regulator_is_enabled,
  140. };
  141. static struct regulator_desc pwm_regulator_desc = {
  142. .name = "pwm-regulator",
  143. .type = REGULATOR_VOLTAGE,
  144. .owner = THIS_MODULE,
  145. .supply_name = "pwm",
  146. };
  147. static int pwm_regulator_init_table(struct platform_device *pdev,
  148. struct pwm_regulator_data *drvdata)
  149. {
  150. struct device_node *np = pdev->dev.of_node;
  151. struct pwm_voltages *duty_cycle_table;
  152. unsigned int length = 0;
  153. int ret;
  154. of_find_property(np, "voltage-table", &length);
  155. if ((length < sizeof(*duty_cycle_table)) ||
  156. (length % sizeof(*duty_cycle_table))) {
  157. dev_err(&pdev->dev,
  158. "voltage-table length(%d) is invalid\n",
  159. length);
  160. return -EINVAL;
  161. }
  162. duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
  163. if (!duty_cycle_table)
  164. return -ENOMEM;
  165. ret = of_property_read_u32_array(np, "voltage-table",
  166. (u32 *)duty_cycle_table,
  167. length / sizeof(u32));
  168. if (ret) {
  169. dev_err(&pdev->dev, "Failed to read voltage-table\n");
  170. return ret;
  171. }
  172. drvdata->duty_cycle_table = duty_cycle_table;
  173. pwm_regulator_desc.ops = &pwm_regulator_voltage_table_ops;
  174. pwm_regulator_desc.n_voltages = length / sizeof(*duty_cycle_table);
  175. return 0;
  176. }
  177. static int pwm_regulator_init_continuous(struct platform_device *pdev,
  178. struct pwm_regulator_data *drvdata)
  179. {
  180. pwm_regulator_desc.ops = &pwm_regulator_voltage_continuous_ops;
  181. pwm_regulator_desc.continuous_voltage_range = true;
  182. return 0;
  183. }
  184. static int pwm_regulator_probe(struct platform_device *pdev)
  185. {
  186. const struct regulator_init_data *init_data;
  187. struct pwm_regulator_data *drvdata;
  188. struct regulator_dev *regulator;
  189. struct regulator_config config = { };
  190. struct device_node *np = pdev->dev.of_node;
  191. int ret;
  192. if (!np) {
  193. dev_err(&pdev->dev, "Device Tree node missing\n");
  194. return -EINVAL;
  195. }
  196. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  197. if (!drvdata)
  198. return -ENOMEM;
  199. if (of_find_property(np, "voltage-table", NULL))
  200. ret = pwm_regulator_init_table(pdev, drvdata);
  201. else
  202. ret = pwm_regulator_init_continuous(pdev, drvdata);
  203. if (ret)
  204. return ret;
  205. init_data = of_get_regulator_init_data(&pdev->dev, np,
  206. &pwm_regulator_desc);
  207. if (!init_data)
  208. return -ENOMEM;
  209. config.of_node = np;
  210. config.dev = &pdev->dev;
  211. config.driver_data = drvdata;
  212. config.init_data = init_data;
  213. drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
  214. if (IS_ERR(drvdata->pwm)) {
  215. dev_err(&pdev->dev, "Failed to get PWM\n");
  216. return PTR_ERR(drvdata->pwm);
  217. }
  218. regulator = devm_regulator_register(&pdev->dev,
  219. &pwm_regulator_desc, &config);
  220. if (IS_ERR(regulator)) {
  221. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  222. pwm_regulator_desc.name);
  223. return PTR_ERR(regulator);
  224. }
  225. return 0;
  226. }
  227. static const struct of_device_id pwm_of_match[] = {
  228. { .compatible = "pwm-regulator" },
  229. { },
  230. };
  231. MODULE_DEVICE_TABLE(of, pwm_of_match);
  232. static struct platform_driver pwm_regulator_driver = {
  233. .driver = {
  234. .name = "pwm-regulator",
  235. .of_match_table = of_match_ptr(pwm_of_match),
  236. },
  237. .probe = pwm_regulator_probe,
  238. };
  239. module_platform_driver(pwm_regulator_driver);
  240. MODULE_LICENSE("GPL");
  241. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
  242. MODULE_DESCRIPTION("PWM Regulator Driver");
  243. MODULE_ALIAS("platform:pwm-regulator");