pwm-regulator.c 11 KB

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