pwm-regulator.c 11 KB

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