pwm-sunplus.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PWM device driver for SUNPLUS SP7021 SoC
  4. *
  5. * Links:
  6. * Reference Manual:
  7. * https://sunplus-tibbo.atlassian.net/wiki/spaces/doc/overview
  8. *
  9. * Reference Manual(PWM module):
  10. * https://sunplus.atlassian.net/wiki/spaces/doc/pages/461144198/12.+Pulse+Width+Modulation+PWM
  11. *
  12. * Limitations:
  13. * - Only supports normal polarity.
  14. * - It output low when PWM channel disabled.
  15. * - When the parameters change, current running period will not be completed
  16. * and run new settings immediately.
  17. * - In .apply() PWM output need to write register FREQ and DUTY. When first write FREQ
  18. * done and not yet write DUTY, it has short timing gap use new FREQ and old DUTY.
  19. *
  20. * Author: Hammer Hsieh <[email protected]>
  21. */
  22. #include <linux/bitfield.h>
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pwm.h>
  29. #define SP7021_PWM_MODE0 0x000
  30. #define SP7021_PWM_MODE0_PWMEN(ch) BIT(ch)
  31. #define SP7021_PWM_MODE0_BYPASS(ch) BIT(8 + (ch))
  32. #define SP7021_PWM_MODE1 0x004
  33. #define SP7021_PWM_MODE1_CNT_EN(ch) BIT(ch)
  34. #define SP7021_PWM_FREQ(ch) (0x008 + 4 * (ch))
  35. #define SP7021_PWM_FREQ_MAX GENMASK(15, 0)
  36. #define SP7021_PWM_DUTY(ch) (0x018 + 4 * (ch))
  37. #define SP7021_PWM_DUTY_DD_SEL(ch) FIELD_PREP(GENMASK(9, 8), ch)
  38. #define SP7021_PWM_DUTY_MAX GENMASK(7, 0)
  39. #define SP7021_PWM_DUTY_MASK SP7021_PWM_DUTY_MAX
  40. #define SP7021_PWM_FREQ_SCALER 256
  41. #define SP7021_PWM_NUM 4
  42. struct sunplus_pwm {
  43. struct pwm_chip chip;
  44. void __iomem *base;
  45. struct clk *clk;
  46. };
  47. static inline struct sunplus_pwm *to_sunplus_pwm(struct pwm_chip *chip)
  48. {
  49. return container_of(chip, struct sunplus_pwm, chip);
  50. }
  51. static int sunplus_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  52. const struct pwm_state *state)
  53. {
  54. struct sunplus_pwm *priv = to_sunplus_pwm(chip);
  55. u32 dd_freq, duty, mode0, mode1;
  56. u64 clk_rate;
  57. if (state->polarity != pwm->state.polarity)
  58. return -EINVAL;
  59. if (!state->enabled) {
  60. /* disable pwm channel output */
  61. mode0 = readl(priv->base + SP7021_PWM_MODE0);
  62. mode0 &= ~SP7021_PWM_MODE0_PWMEN(pwm->hwpwm);
  63. writel(mode0, priv->base + SP7021_PWM_MODE0);
  64. /* disable pwm channel clk source */
  65. mode1 = readl(priv->base + SP7021_PWM_MODE1);
  66. mode1 &= ~SP7021_PWM_MODE1_CNT_EN(pwm->hwpwm);
  67. writel(mode1, priv->base + SP7021_PWM_MODE1);
  68. return 0;
  69. }
  70. clk_rate = clk_get_rate(priv->clk);
  71. /*
  72. * The following calculations might overflow if clk is bigger
  73. * than 256 GHz. In practise it's 202.5MHz, so this limitation
  74. * is only theoretic.
  75. */
  76. if (clk_rate > (u64)SP7021_PWM_FREQ_SCALER * NSEC_PER_SEC)
  77. return -EINVAL;
  78. /*
  79. * With clk_rate limited above we have dd_freq <= state->period,
  80. * so this cannot overflow.
  81. */
  82. dd_freq = mul_u64_u64_div_u64(clk_rate, state->period, (u64)SP7021_PWM_FREQ_SCALER
  83. * NSEC_PER_SEC);
  84. if (dd_freq == 0)
  85. return -EINVAL;
  86. if (dd_freq > SP7021_PWM_FREQ_MAX)
  87. dd_freq = SP7021_PWM_FREQ_MAX;
  88. writel(dd_freq, priv->base + SP7021_PWM_FREQ(pwm->hwpwm));
  89. /* cal and set pwm duty */
  90. mode0 = readl(priv->base + SP7021_PWM_MODE0);
  91. mode0 |= SP7021_PWM_MODE0_PWMEN(pwm->hwpwm);
  92. mode1 = readl(priv->base + SP7021_PWM_MODE1);
  93. mode1 |= SP7021_PWM_MODE1_CNT_EN(pwm->hwpwm);
  94. if (state->duty_cycle == state->period) {
  95. /* PWM channel output = high */
  96. mode0 |= SP7021_PWM_MODE0_BYPASS(pwm->hwpwm);
  97. duty = SP7021_PWM_DUTY_DD_SEL(pwm->hwpwm) | SP7021_PWM_DUTY_MAX;
  98. } else {
  99. mode0 &= ~SP7021_PWM_MODE0_BYPASS(pwm->hwpwm);
  100. /*
  101. * duty_ns <= period_ns 27 bits, clk_rate 28 bits, won't overflow.
  102. */
  103. duty = mul_u64_u64_div_u64(state->duty_cycle, clk_rate,
  104. (u64)dd_freq * NSEC_PER_SEC);
  105. duty = SP7021_PWM_DUTY_DD_SEL(pwm->hwpwm) | duty;
  106. }
  107. writel(duty, priv->base + SP7021_PWM_DUTY(pwm->hwpwm));
  108. writel(mode1, priv->base + SP7021_PWM_MODE1);
  109. writel(mode0, priv->base + SP7021_PWM_MODE0);
  110. return 0;
  111. }
  112. static int sunplus_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  113. struct pwm_state *state)
  114. {
  115. struct sunplus_pwm *priv = to_sunplus_pwm(chip);
  116. u32 mode0, dd_freq, duty;
  117. u64 clk_rate;
  118. mode0 = readl(priv->base + SP7021_PWM_MODE0);
  119. if (mode0 & BIT(pwm->hwpwm)) {
  120. clk_rate = clk_get_rate(priv->clk);
  121. dd_freq = readl(priv->base + SP7021_PWM_FREQ(pwm->hwpwm));
  122. duty = readl(priv->base + SP7021_PWM_DUTY(pwm->hwpwm));
  123. duty = FIELD_GET(SP7021_PWM_DUTY_MASK, duty);
  124. /*
  125. * dd_freq 16 bits, SP7021_PWM_FREQ_SCALER 8 bits
  126. * NSEC_PER_SEC 30 bits, won't overflow.
  127. */
  128. state->period = DIV64_U64_ROUND_UP((u64)dd_freq * (u64)SP7021_PWM_FREQ_SCALER
  129. * NSEC_PER_SEC, clk_rate);
  130. /*
  131. * dd_freq 16 bits, duty 8 bits, NSEC_PER_SEC 30 bits, won't overflow.
  132. */
  133. state->duty_cycle = DIV64_U64_ROUND_UP((u64)dd_freq * (u64)duty * NSEC_PER_SEC,
  134. clk_rate);
  135. state->enabled = true;
  136. } else {
  137. state->enabled = false;
  138. }
  139. state->polarity = PWM_POLARITY_NORMAL;
  140. return 0;
  141. }
  142. static const struct pwm_ops sunplus_pwm_ops = {
  143. .apply = sunplus_pwm_apply,
  144. .get_state = sunplus_pwm_get_state,
  145. .owner = THIS_MODULE,
  146. };
  147. static void sunplus_pwm_clk_release(void *data)
  148. {
  149. struct clk *clk = data;
  150. clk_disable_unprepare(clk);
  151. }
  152. static int sunplus_pwm_probe(struct platform_device *pdev)
  153. {
  154. struct device *dev = &pdev->dev;
  155. struct sunplus_pwm *priv;
  156. int ret;
  157. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  158. if (!priv)
  159. return -ENOMEM;
  160. priv->base = devm_platform_ioremap_resource(pdev, 0);
  161. if (IS_ERR(priv->base))
  162. return PTR_ERR(priv->base);
  163. priv->clk = devm_clk_get(dev, NULL);
  164. if (IS_ERR(priv->clk))
  165. return dev_err_probe(dev, PTR_ERR(priv->clk),
  166. "get pwm clock failed\n");
  167. ret = clk_prepare_enable(priv->clk);
  168. if (ret < 0) {
  169. dev_err(dev, "failed to enable clock: %d\n", ret);
  170. return ret;
  171. }
  172. ret = devm_add_action_or_reset(dev, sunplus_pwm_clk_release, priv->clk);
  173. if (ret < 0) {
  174. dev_err(dev, "failed to release clock: %d\n", ret);
  175. return ret;
  176. }
  177. priv->chip.dev = dev;
  178. priv->chip.ops = &sunplus_pwm_ops;
  179. priv->chip.npwm = SP7021_PWM_NUM;
  180. ret = devm_pwmchip_add(dev, &priv->chip);
  181. if (ret < 0)
  182. return dev_err_probe(dev, ret, "Cannot register sunplus PWM\n");
  183. return 0;
  184. }
  185. static const struct of_device_id sunplus_pwm_of_match[] = {
  186. { .compatible = "sunplus,sp7021-pwm", },
  187. {}
  188. };
  189. MODULE_DEVICE_TABLE(of, sunplus_pwm_of_match);
  190. static struct platform_driver sunplus_pwm_driver = {
  191. .probe = sunplus_pwm_probe,
  192. .driver = {
  193. .name = "sunplus-pwm",
  194. .of_match_table = sunplus_pwm_of_match,
  195. },
  196. };
  197. module_platform_driver(sunplus_pwm_driver);
  198. MODULE_DESCRIPTION("Sunplus SoC PWM Driver");
  199. MODULE_AUTHOR("Hammer Hsieh <[email protected]>");
  200. MODULE_LICENSE("GPL");