pwm-regulator.c 11 KB

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