pwm-gpio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic software PWM for modulating GPIOs
  4. *
  5. * Copyright (C) 2020 Axis Communications AB
  6. * Copyright (C) 2020 Nicola Di Lieto
  7. * Copyright (C) 2024 Stefan Wahren
  8. * Copyright (C) 2024 Linus Walleij
  9. */
  10. #include <linux/cleanup.h>
  11. #include <linux/container_of.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/hrtimer.h>
  16. #include <linux/math.h>
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/property.h>
  21. #include <linux/pwm.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/time.h>
  24. #include <linux/types.h>
  25. struct pwm_gpio {
  26. struct hrtimer gpio_timer;
  27. struct gpio_desc *gpio;
  28. struct pwm_state state;
  29. struct pwm_state next_state;
  30. /* Protect internal state between pwm_ops and hrtimer */
  31. spinlock_t lock;
  32. bool changing;
  33. bool running;
  34. bool level;
  35. };
  36. static void pwm_gpio_round(struct pwm_state *dest, const struct pwm_state *src)
  37. {
  38. u64 dividend;
  39. u32 remainder;
  40. *dest = *src;
  41. /* Round down to hrtimer resolution */
  42. dividend = dest->period;
  43. remainder = do_div(dividend, hrtimer_resolution);
  44. dest->period -= remainder;
  45. dividend = dest->duty_cycle;
  46. remainder = do_div(dividend, hrtimer_resolution);
  47. dest->duty_cycle -= remainder;
  48. }
  49. static u64 pwm_gpio_toggle(struct pwm_gpio *gpwm, bool level)
  50. {
  51. const struct pwm_state *state = &gpwm->state;
  52. bool invert = state->polarity == PWM_POLARITY_INVERSED;
  53. gpwm->level = level;
  54. gpiod_set_value(gpwm->gpio, gpwm->level ^ invert);
  55. if (!state->duty_cycle || state->duty_cycle == state->period) {
  56. gpwm->running = false;
  57. return 0;
  58. }
  59. gpwm->running = true;
  60. return level ? state->duty_cycle : state->period - state->duty_cycle;
  61. }
  62. static enum hrtimer_restart pwm_gpio_timer(struct hrtimer *gpio_timer)
  63. {
  64. struct pwm_gpio *gpwm = container_of(gpio_timer, struct pwm_gpio,
  65. gpio_timer);
  66. u64 next_toggle;
  67. bool new_level;
  68. guard(spinlock_irqsave)(&gpwm->lock);
  69. /* Apply new state at end of current period */
  70. if (!gpwm->level && gpwm->changing) {
  71. gpwm->changing = false;
  72. gpwm->state = gpwm->next_state;
  73. new_level = !!gpwm->state.duty_cycle;
  74. } else {
  75. new_level = !gpwm->level;
  76. }
  77. next_toggle = pwm_gpio_toggle(gpwm, new_level);
  78. if (next_toggle)
  79. hrtimer_forward(gpio_timer, hrtimer_get_expires(gpio_timer),
  80. ns_to_ktime(next_toggle));
  81. return next_toggle ? HRTIMER_RESTART : HRTIMER_NORESTART;
  82. }
  83. static int pwm_gpio_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  84. const struct pwm_state *state)
  85. {
  86. struct pwm_gpio *gpwm = pwmchip_get_drvdata(chip);
  87. bool invert = state->polarity == PWM_POLARITY_INVERSED;
  88. if (state->duty_cycle && state->duty_cycle < hrtimer_resolution)
  89. return -EINVAL;
  90. if (state->duty_cycle != state->period &&
  91. (state->period - state->duty_cycle < hrtimer_resolution))
  92. return -EINVAL;
  93. if (!state->enabled) {
  94. hrtimer_cancel(&gpwm->gpio_timer);
  95. } else if (!gpwm->running) {
  96. int ret;
  97. /*
  98. * This just enables the output, but pwm_gpio_toggle()
  99. * really starts the duty cycle.
  100. */
  101. ret = gpiod_direction_output(gpwm->gpio, invert);
  102. if (ret)
  103. return ret;
  104. }
  105. guard(spinlock_irqsave)(&gpwm->lock);
  106. if (!state->enabled) {
  107. pwm_gpio_round(&gpwm->state, state);
  108. gpwm->running = false;
  109. gpwm->changing = false;
  110. gpiod_set_value(gpwm->gpio, invert);
  111. } else if (gpwm->running) {
  112. pwm_gpio_round(&gpwm->next_state, state);
  113. gpwm->changing = true;
  114. } else {
  115. unsigned long next_toggle;
  116. pwm_gpio_round(&gpwm->state, state);
  117. gpwm->changing = false;
  118. next_toggle = pwm_gpio_toggle(gpwm, !!state->duty_cycle);
  119. if (next_toggle)
  120. hrtimer_start(&gpwm->gpio_timer, next_toggle,
  121. HRTIMER_MODE_REL);
  122. }
  123. return 0;
  124. }
  125. static int pwm_gpio_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  126. struct pwm_state *state)
  127. {
  128. struct pwm_gpio *gpwm = pwmchip_get_drvdata(chip);
  129. guard(spinlock_irqsave)(&gpwm->lock);
  130. if (gpwm->changing)
  131. *state = gpwm->next_state;
  132. else
  133. *state = gpwm->state;
  134. return 0;
  135. }
  136. static const struct pwm_ops pwm_gpio_ops = {
  137. .apply = pwm_gpio_apply,
  138. .get_state = pwm_gpio_get_state,
  139. };
  140. static void pwm_gpio_disable_hrtimer(void *data)
  141. {
  142. struct pwm_gpio *gpwm = data;
  143. hrtimer_cancel(&gpwm->gpio_timer);
  144. }
  145. static int pwm_gpio_probe(struct platform_device *pdev)
  146. {
  147. struct device *dev = &pdev->dev;
  148. struct pwm_chip *chip;
  149. struct pwm_gpio *gpwm;
  150. int ret;
  151. chip = devm_pwmchip_alloc(dev, 1, sizeof(*gpwm));
  152. if (IS_ERR(chip))
  153. return PTR_ERR(chip);
  154. gpwm = pwmchip_get_drvdata(chip);
  155. spin_lock_init(&gpwm->lock);
  156. gpwm->gpio = devm_gpiod_get(dev, NULL, GPIOD_ASIS);
  157. if (IS_ERR(gpwm->gpio))
  158. return dev_err_probe(dev, PTR_ERR(gpwm->gpio),
  159. "%pfw: could not get gpio\n",
  160. dev_fwnode(dev));
  161. if (gpiod_cansleep(gpwm->gpio))
  162. return dev_err_probe(dev, -EINVAL,
  163. "%pfw: sleeping GPIO not supported\n",
  164. dev_fwnode(dev));
  165. chip->ops = &pwm_gpio_ops;
  166. chip->atomic = true;
  167. hrtimer_init(&gpwm->gpio_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  168. ret = devm_add_action_or_reset(dev, pwm_gpio_disable_hrtimer, gpwm);
  169. if (ret)
  170. return ret;
  171. gpwm->gpio_timer.function = pwm_gpio_timer;
  172. ret = pwmchip_add(chip);
  173. if (ret < 0)
  174. return dev_err_probe(dev, ret, "could not add pwmchip\n");
  175. return 0;
  176. }
  177. static const struct of_device_id pwm_gpio_dt_ids[] = {
  178. { .compatible = "pwm-gpio" },
  179. { /* sentinel */ }
  180. };
  181. MODULE_DEVICE_TABLE(of, pwm_gpio_dt_ids);
  182. static struct platform_driver pwm_gpio_driver = {
  183. .driver = {
  184. .name = "pwm-gpio",
  185. .of_match_table = pwm_gpio_dt_ids,
  186. },
  187. .probe = pwm_gpio_probe,
  188. };
  189. module_platform_driver(pwm_gpio_driver);
  190. MODULE_DESCRIPTION("PWM GPIO driver");
  191. MODULE_AUTHOR("Vincent Whitchurch");
  192. MODULE_LICENSE("GPL");