pwm-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/platform_device.h>
  13. #include <linux/regulator/driver.h>
  14. #include <linux/regulator/machine.h>
  15. #include <linux/regulator/of_regulator.h>
  16. #include <linux/of.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_might_sleep(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. if (!pstate.enabled) {
  125. if (pstate.polarity == PWM_POLARITY_INVERSED)
  126. pstate.duty_cycle = pstate.period;
  127. else
  128. pstate.duty_cycle = 0;
  129. }
  130. voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
  131. if (voltage < min(max_uV_duty, min_uV_duty) ||
  132. voltage > max(max_uV_duty, min_uV_duty))
  133. return -ENOTRECOVERABLE;
  134. /*
  135. * The dutycycle for min_uV might be greater than the one for max_uV.
  136. * This is happening when the user needs an inversed polarity, but the
  137. * PWM device does not support inversing it in hardware.
  138. */
  139. if (max_uV_duty < min_uV_duty) {
  140. voltage = min_uV_duty - voltage;
  141. diff_duty = min_uV_duty - max_uV_duty;
  142. } else {
  143. voltage = voltage - min_uV_duty;
  144. diff_duty = max_uV_duty - min_uV_duty;
  145. }
  146. voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
  147. return voltage + min_uV;
  148. }
  149. static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
  150. int req_min_uV, int req_max_uV,
  151. unsigned int *selector)
  152. {
  153. struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
  154. unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
  155. unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
  156. unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
  157. int min_uV = rdev->constraints->min_uV;
  158. int max_uV = rdev->constraints->max_uV;
  159. int diff_uV = max_uV - min_uV;
  160. struct pwm_state pstate;
  161. unsigned int diff_duty;
  162. unsigned int dutycycle;
  163. int ret;
  164. pwm_init_state(drvdata->pwm, &pstate);
  165. /*
  166. * The dutycycle for min_uV might be greater than the one for max_uV.
  167. * This is happening when the user needs an inversed polarity, but the
  168. * PWM device does not support inversing it in hardware.
  169. */
  170. if (max_uV_duty < min_uV_duty)
  171. diff_duty = min_uV_duty - max_uV_duty;
  172. else
  173. diff_duty = max_uV_duty - min_uV_duty;
  174. dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
  175. diff_duty,
  176. diff_uV);
  177. if (max_uV_duty < min_uV_duty)
  178. dutycycle = min_uV_duty - dutycycle;
  179. else
  180. dutycycle = min_uV_duty + dutycycle;
  181. pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
  182. ret = pwm_apply_might_sleep(drvdata->pwm, &pstate);
  183. if (ret) {
  184. dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static const struct regulator_ops pwm_regulator_voltage_table_ops = {
  190. .set_voltage_sel = pwm_regulator_set_voltage_sel,
  191. .get_voltage_sel = pwm_regulator_get_voltage_sel,
  192. .list_voltage = pwm_regulator_list_voltage,
  193. .map_voltage = regulator_map_voltage_iterate,
  194. .enable = pwm_regulator_enable,
  195. .disable = pwm_regulator_disable,
  196. .is_enabled = pwm_regulator_is_enabled,
  197. };
  198. static const struct regulator_ops pwm_regulator_voltage_continuous_ops = {
  199. .get_voltage = pwm_regulator_get_voltage,
  200. .set_voltage = pwm_regulator_set_voltage,
  201. .enable = pwm_regulator_enable,
  202. .disable = pwm_regulator_disable,
  203. .is_enabled = pwm_regulator_is_enabled,
  204. };
  205. static const struct regulator_desc pwm_regulator_desc = {
  206. .name = "pwm-regulator",
  207. .type = REGULATOR_VOLTAGE,
  208. .owner = THIS_MODULE,
  209. .supply_name = "pwm",
  210. };
  211. static int pwm_regulator_init_table(struct platform_device *pdev,
  212. struct pwm_regulator_data *drvdata)
  213. {
  214. struct device_node *np = pdev->dev.of_node;
  215. struct pwm_voltages *duty_cycle_table;
  216. unsigned int length = 0;
  217. int ret;
  218. of_find_property(np, "voltage-table", &length);
  219. if ((length < sizeof(*duty_cycle_table)) ||
  220. (length % sizeof(*duty_cycle_table)))
  221. return dev_err_probe(&pdev->dev, -EINVAL,
  222. "voltage-table length(%d) is invalid\n",
  223. length);
  224. duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
  225. if (!duty_cycle_table)
  226. return -ENOMEM;
  227. ret = of_property_read_u32_array(np, "voltage-table",
  228. (u32 *)duty_cycle_table,
  229. length / sizeof(u32));
  230. if (ret)
  231. return dev_err_probe(&pdev->dev, ret,
  232. "Failed to read voltage-table\n");
  233. drvdata->state = -ENOTRECOVERABLE;
  234. drvdata->duty_cycle_table = duty_cycle_table;
  235. drvdata->desc.ops = &pwm_regulator_voltage_table_ops;
  236. drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
  237. return 0;
  238. }
  239. static int pwm_regulator_init_continuous(struct platform_device *pdev,
  240. struct pwm_regulator_data *drvdata)
  241. {
  242. u32 dutycycle_range[2] = { 0, 100 };
  243. u32 dutycycle_unit = 100;
  244. drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops;
  245. drvdata->desc.continuous_voltage_range = true;
  246. of_property_read_u32_array(pdev->dev.of_node,
  247. "pwm-dutycycle-range",
  248. dutycycle_range, 2);
  249. of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
  250. &dutycycle_unit);
  251. if (dutycycle_range[0] > dutycycle_unit ||
  252. dutycycle_range[1] > dutycycle_unit)
  253. return -EINVAL;
  254. drvdata->continuous.dutycycle_unit = dutycycle_unit;
  255. drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
  256. drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
  257. return 0;
  258. }
  259. static int pwm_regulator_init_boot_on(struct platform_device *pdev,
  260. struct pwm_regulator_data *drvdata,
  261. const struct regulator_init_data *init_data)
  262. {
  263. struct pwm_state pstate;
  264. if (!init_data->constraints.boot_on || drvdata->enb_gpio)
  265. return 0;
  266. pwm_get_state(drvdata->pwm, &pstate);
  267. if (pstate.enabled)
  268. return 0;
  269. /*
  270. * Update the duty cycle so the output does not change
  271. * when the regulator core enables the regulator (and
  272. * thus the PWM channel).
  273. */
  274. if (pstate.polarity == PWM_POLARITY_INVERSED)
  275. pstate.duty_cycle = pstate.period;
  276. else
  277. pstate.duty_cycle = 0;
  278. return pwm_apply_might_sleep(drvdata->pwm, &pstate);
  279. }
  280. static int pwm_regulator_probe(struct platform_device *pdev)
  281. {
  282. const struct regulator_init_data *init_data;
  283. struct pwm_regulator_data *drvdata;
  284. struct regulator_dev *regulator;
  285. struct regulator_config config = { };
  286. struct device_node *np = pdev->dev.of_node;
  287. enum gpiod_flags gpio_flags;
  288. int ret;
  289. if (!np)
  290. return dev_err_probe(&pdev->dev, -EINVAL,
  291. "Device Tree node missing\n");
  292. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  293. if (!drvdata)
  294. return -ENOMEM;
  295. memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
  296. if (of_property_present(np, "voltage-table"))
  297. ret = pwm_regulator_init_table(pdev, drvdata);
  298. else
  299. ret = pwm_regulator_init_continuous(pdev, drvdata);
  300. if (ret)
  301. return ret;
  302. init_data = of_get_regulator_init_data(&pdev->dev, np,
  303. &drvdata->desc);
  304. if (!init_data)
  305. return -ENOMEM;
  306. config.of_node = np;
  307. config.dev = &pdev->dev;
  308. config.driver_data = drvdata;
  309. config.init_data = init_data;
  310. drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
  311. if (IS_ERR(drvdata->pwm))
  312. return dev_err_probe(&pdev->dev, PTR_ERR(drvdata->pwm),
  313. "Failed to get PWM\n");
  314. if (init_data->constraints.boot_on || init_data->constraints.always_on)
  315. gpio_flags = GPIOD_OUT_HIGH;
  316. else
  317. gpio_flags = GPIOD_OUT_LOW;
  318. drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
  319. gpio_flags);
  320. if (IS_ERR(drvdata->enb_gpio)) {
  321. ret = PTR_ERR(drvdata->enb_gpio);
  322. return dev_err_probe(&pdev->dev, ret, "Failed to get enable GPIO\n");
  323. }
  324. ret = pwm_adjust_config(drvdata->pwm);
  325. if (ret)
  326. return ret;
  327. ret = pwm_regulator_init_boot_on(pdev, drvdata, init_data);
  328. if (ret)
  329. return dev_err_probe(&pdev->dev, ret,
  330. "Failed to apply boot_on settings\n");
  331. regulator = devm_regulator_register(&pdev->dev,
  332. &drvdata->desc, &config);
  333. if (IS_ERR(regulator)) {
  334. ret = PTR_ERR(regulator);
  335. return dev_err_probe(&pdev->dev, ret,
  336. "Failed to register regulator %s\n",
  337. drvdata->desc.name);
  338. }
  339. return 0;
  340. }
  341. static const struct of_device_id __maybe_unused pwm_of_match[] = {
  342. { .compatible = "pwm-regulator" },
  343. { },
  344. };
  345. MODULE_DEVICE_TABLE(of, pwm_of_match);
  346. static struct platform_driver pwm_regulator_driver = {
  347. .driver = {
  348. .name = "pwm-regulator",
  349. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  350. .of_match_table = of_match_ptr(pwm_of_match),
  351. },
  352. .probe = pwm_regulator_probe,
  353. };
  354. module_platform_driver(pwm_regulator_driver);
  355. MODULE_LICENSE("GPL");
  356. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
  357. MODULE_DESCRIPTION("PWM Regulator Driver");
  358. MODULE_ALIAS("platform:pwm-regulator");