pwm-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Regulator driver for PWM Regulators
  4. *
  5. * Copyright (C) 2014 - STMicroelectronics Inc.
  6. *
  7. * Author: Lee Jones <lee.jones@linaro.org>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/err.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/regulator/machine.h>
  14. #include <linux/regulator/of_regulator.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pwm.h>
  18. #include <linux/gpio/consumer.h>
  19. struct pwm_continuous_reg_data {
  20. unsigned int min_uV_dutycycle;
  21. unsigned int max_uV_dutycycle;
  22. unsigned int dutycycle_unit;
  23. };
  24. struct pwm_regulator_data {
  25. /* Shared */
  26. struct pwm_device *pwm;
  27. /* Voltage table */
  28. struct pwm_voltages *duty_cycle_table;
  29. /* Continuous mode info */
  30. struct pwm_continuous_reg_data continuous;
  31. /* regulator descriptor */
  32. struct regulator_desc desc;
  33. int state;
  34. /* Enable GPIO */
  35. struct gpio_desc *enb_gpio;
  36. };
  37. struct pwm_voltages {
  38. unsigned int uV;
  39. unsigned int dutycycle;
  40. };
  41. /**
  42. * Voltage table call-backs
  43. */
  44. static void pwm_regulator_init_state(struct regulator_dev *rdev)
  45. {
  46. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  47. struct pwm_state pwm_state;
  48. unsigned int dutycycle;
  49. int i;
  50. pwm_get_state(drvdata->pwm, &pwm_state);
  51. dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
  52. for (i = 0; i < rdev->desc->n_voltages; i++) {
  53. if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
  54. drvdata->state = i;
  55. return;
  56. }
  57. }
  58. }
  59. static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
  60. {
  61. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  62. if (drvdata->state < 0)
  63. pwm_regulator_init_state(rdev);
  64. return drvdata->state;
  65. }
  66. static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
  67. unsigned selector)
  68. {
  69. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  70. struct pwm_state pstate;
  71. int ret;
  72. pwm_init_state(drvdata->pwm, &pstate);
  73. pwm_set_relative_duty_cycle(&pstate,
  74. drvdata->duty_cycle_table[selector].dutycycle, 100);
  75. ret = pwm_apply_state(drvdata->pwm, &pstate);
  76. if (ret) {
  77. dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
  78. return ret;
  79. }
  80. drvdata->state = selector;
  81. return 0;
  82. }
  83. static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
  84. unsigned selector)
  85. {
  86. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  87. if (selector >= rdev->desc->n_voltages)
  88. return -EINVAL;
  89. return drvdata->duty_cycle_table[selector].uV;
  90. }
  91. static int pwm_regulator_enable(struct regulator_dev *dev)
  92. {
  93. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  94. gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
  95. return pwm_enable(drvdata->pwm);
  96. }
  97. static int pwm_regulator_disable(struct regulator_dev *dev)
  98. {
  99. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  100. pwm_disable(drvdata->pwm);
  101. gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
  102. return 0;
  103. }
  104. static int pwm_regulator_is_enabled(struct regulator_dev *dev)
  105. {
  106. struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
  107. if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
  108. return false;
  109. return pwm_is_enabled(drvdata->pwm);
  110. }
  111. static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
  112. {
  113. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  114. unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
  115. unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
  116. unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
  117. int min_uV = rdev->constraints->min_uV;
  118. int max_uV = rdev->constraints->max_uV;
  119. int diff_uV = max_uV - min_uV;
  120. struct pwm_state pstate;
  121. unsigned int diff_duty;
  122. unsigned int voltage;
  123. pwm_get_state(drvdata->pwm, &pstate);
  124. voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
  125. /*
  126. * The dutycycle for min_uV might be greater than the one for max_uV.
  127. * This is happening when the user needs an inversed polarity, but the
  128. * PWM device does not support inversing it in hardware.
  129. */
  130. if (max_uV_duty < min_uV_duty) {
  131. voltage = min_uV_duty - voltage;
  132. diff_duty = min_uV_duty - max_uV_duty;
  133. } else {
  134. voltage = voltage - min_uV_duty;
  135. diff_duty = max_uV_duty - min_uV_duty;
  136. }
  137. voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
  138. return voltage + min_uV;
  139. }
  140. static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
  141. int req_min_uV, int req_max_uV,
  142. unsigned int *selector)
  143. {
  144. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  145. unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
  146. unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
  147. unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
  148. int min_uV = rdev->constraints->min_uV;
  149. int max_uV = rdev->constraints->max_uV;
  150. int diff_uV = max_uV - min_uV;
  151. struct pwm_state pstate;
  152. unsigned int diff_duty;
  153. unsigned int dutycycle;
  154. int ret;
  155. pwm_init_state(drvdata->pwm, &pstate);
  156. /*
  157. * The dutycycle for min_uV might be greater than the one for max_uV.
  158. * This is happening when the user needs an inversed polarity, but the
  159. * PWM device does not support inversing it in hardware.
  160. */
  161. if (max_uV_duty < min_uV_duty)
  162. diff_duty = min_uV_duty - max_uV_duty;
  163. else
  164. diff_duty = max_uV_duty - min_uV_duty;
  165. dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
  166. diff_duty,
  167. diff_uV);
  168. if (max_uV_duty < min_uV_duty)
  169. dutycycle = min_uV_duty - dutycycle;
  170. else
  171. dutycycle = min_uV_duty + dutycycle;
  172. pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
  173. ret = pwm_apply_state(drvdata->pwm, &pstate);
  174. if (ret) {
  175. dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
  176. return ret;
  177. }
  178. return 0;
  179. }
  180. static const struct regulator_ops pwm_regulator_voltage_table_ops = {
  181. .set_voltage_sel = pwm_regulator_set_voltage_sel,
  182. .get_voltage_sel = pwm_regulator_get_voltage_sel,
  183. .list_voltage = pwm_regulator_list_voltage,
  184. .map_voltage = regulator_map_voltage_iterate,
  185. .enable = pwm_regulator_enable,
  186. .disable = pwm_regulator_disable,
  187. .is_enabled = pwm_regulator_is_enabled,
  188. };
  189. static const struct regulator_ops pwm_regulator_voltage_continuous_ops = {
  190. .get_voltage = pwm_regulator_get_voltage,
  191. .set_voltage = pwm_regulator_set_voltage,
  192. .enable = pwm_regulator_enable,
  193. .disable = pwm_regulator_disable,
  194. .is_enabled = pwm_regulator_is_enabled,
  195. };
  196. static const struct regulator_desc pwm_regulator_desc = {
  197. .name = "pwm-regulator",
  198. .type = REGULATOR_VOLTAGE,
  199. .owner = THIS_MODULE,
  200. .supply_name = "pwm",
  201. };
  202. static int pwm_regulator_init_table(struct platform_device *pdev,
  203. struct pwm_regulator_data *drvdata)
  204. {
  205. struct device_node *np = pdev->dev.of_node;
  206. struct pwm_voltages *duty_cycle_table;
  207. unsigned int length = 0;
  208. int ret;
  209. of_find_property(np, "voltage-table", &length);
  210. if ((length < sizeof(*duty_cycle_table)) ||
  211. (length % sizeof(*duty_cycle_table))) {
  212. dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
  213. length);
  214. return -EINVAL;
  215. }
  216. duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
  217. if (!duty_cycle_table)
  218. return -ENOMEM;
  219. ret = of_property_read_u32_array(np, "voltage-table",
  220. (u32 *)duty_cycle_table,
  221. length / sizeof(u32));
  222. if (ret) {
  223. dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
  224. return ret;
  225. }
  226. drvdata->state = -ENOTRECOVERABLE;
  227. drvdata->duty_cycle_table = duty_cycle_table;
  228. drvdata->desc.ops = &pwm_regulator_voltage_table_ops;
  229. drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
  230. return 0;
  231. }
  232. static int pwm_regulator_init_continuous(struct platform_device *pdev,
  233. struct pwm_regulator_data *drvdata)
  234. {
  235. u32 dutycycle_range[2] = { 0, 100 };
  236. u32 dutycycle_unit = 100;
  237. drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops;
  238. drvdata->desc.continuous_voltage_range = true;
  239. of_property_read_u32_array(pdev->dev.of_node,
  240. "pwm-dutycycle-range",
  241. dutycycle_range, 2);
  242. of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
  243. &dutycycle_unit);
  244. if (dutycycle_range[0] > dutycycle_unit ||
  245. dutycycle_range[1] > dutycycle_unit)
  246. return -EINVAL;
  247. drvdata->continuous.dutycycle_unit = dutycycle_unit;
  248. drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
  249. drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
  250. return 0;
  251. }
  252. static int pwm_regulator_probe(struct platform_device *pdev)
  253. {
  254. const struct regulator_init_data *init_data;
  255. struct pwm_regulator_data *drvdata;
  256. struct regulator_dev *regulator;
  257. struct regulator_config config = { };
  258. struct device_node *np = pdev->dev.of_node;
  259. enum gpiod_flags gpio_flags;
  260. int ret;
  261. if (!np) {
  262. dev_err(&pdev->dev, "Device Tree node missing\n");
  263. return -EINVAL;
  264. }
  265. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  266. if (!drvdata)
  267. return -ENOMEM;
  268. memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
  269. if (of_find_property(np, "voltage-table", NULL))
  270. ret = pwm_regulator_init_table(pdev, drvdata);
  271. else
  272. ret = pwm_regulator_init_continuous(pdev, drvdata);
  273. if (ret)
  274. return ret;
  275. init_data = of_get_regulator_init_data(&pdev->dev, np,
  276. &drvdata->desc);
  277. if (!init_data)
  278. return -ENOMEM;
  279. config.of_node = np;
  280. config.dev = &pdev->dev;
  281. config.driver_data = drvdata;
  282. config.init_data = init_data;
  283. drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
  284. if (IS_ERR(drvdata->pwm)) {
  285. ret = PTR_ERR(drvdata->pwm);
  286. dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
  287. return ret;
  288. }
  289. if (init_data->constraints.boot_on || init_data->constraints.always_on)
  290. gpio_flags = GPIOD_OUT_HIGH;
  291. else
  292. gpio_flags = GPIOD_OUT_LOW;
  293. drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
  294. gpio_flags);
  295. if (IS_ERR(drvdata->enb_gpio)) {
  296. ret = PTR_ERR(drvdata->enb_gpio);
  297. dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
  298. return ret;
  299. }
  300. ret = pwm_adjust_config(drvdata->pwm);
  301. if (ret)
  302. return ret;
  303. regulator = devm_regulator_register(&pdev->dev,
  304. &drvdata->desc, &config);
  305. if (IS_ERR(regulator)) {
  306. ret = PTR_ERR(regulator);
  307. dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
  308. drvdata->desc.name, ret);
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. static const struct of_device_id pwm_of_match[] = {
  314. { .compatible = "pwm-regulator" },
  315. { },
  316. };
  317. MODULE_DEVICE_TABLE(of, pwm_of_match);
  318. static struct platform_driver pwm_regulator_driver = {
  319. .driver = {
  320. .name = "pwm-regulator",
  321. .of_match_table = of_match_ptr(pwm_of_match),
  322. },
  323. .probe = pwm_regulator_probe,
  324. };
  325. module_platform_driver(pwm_regulator_driver);
  326. MODULE_LICENSE("GPL");
  327. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
  328. MODULE_DESCRIPTION("PWM Regulator Driver");
  329. MODULE_ALIAS("platform:pwm-regulator");