pwm-aspeed.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2022 Aspeed Technology Inc.
  4. *
  5. * PWM controller driver for Aspeed ast2600 SoCs.
  6. * This drivers doesn't support earlier version of the IP.
  7. *
  8. * The formula of pwm period duration:
  9. * period duration = ((DIV_L + 1) * (PERIOD + 1) << DIV_H) / input-clk
  10. *
  11. * The formula of pwm duty cycle duration:
  12. * duty cycle duration = period duration * DUTY_CYCLE_FALLING_POINT / (PERIOD + 1)
  13. * = ((DIV_L + 1) * DUTY_CYCLE_FALLING_POINT << DIV_H) / input-clk
  14. *
  15. * The software driver fixes the period to 255, which causes the high-frequency
  16. * precision of the PWM to be coarse, in exchange for the fineness of the duty cycle.
  17. *
  18. * Register usage:
  19. * PIN_ENABLE: When it is unset the pwm controller will always output low to the extern.
  20. * Use to determine whether the PWM channel is enabled or disabled
  21. * CLK_ENABLE: When it is unset the pwm controller will reset the duty counter to 0 and
  22. * output low to the PIN_ENABLE mux after that the driver can still change the pwm period
  23. * and duty and the value will apply when CLK_ENABLE be set again.
  24. * Use to determine whether duty_cycle bigger than 0.
  25. * PWM_ASPEED_CTRL_INVERSE: When it is toggled the output value will inverse immediately.
  26. * PWM_ASPEED_DUTY_CYCLE_FALLING_POINT/PWM_ASPEED_DUTY_CYCLE_RISING_POINT: When these two
  27. * values are equal it means the duty cycle = 100%.
  28. *
  29. * Limitations:
  30. * - When changing both duty cycle and period, we cannot prevent in
  31. * software that the output might produce a period with mixed
  32. * settings.
  33. * - Disabling the PWM doesn't complete the current period.
  34. *
  35. * Improvements:
  36. * - When only changing one of duty cycle or period, our pwm controller will not
  37. * generate the glitch, the configure will change at next cycle of pwm.
  38. * This improvement can disable/enable through PWM_ASPEED_CTRL_DUTY_SYNC_DISABLE.
  39. */
  40. #include <common.h>
  41. #include <div64.h>
  42. #include <dm.h>
  43. #include <pwm.h>
  44. #include <clk.h>
  45. #include <reset.h>
  46. #include <regmap.h>
  47. #include <syscon.h>
  48. #include <dm/device_compat.h>
  49. #include <linux/math64.h>
  50. #include <linux/bitfield.h>
  51. #include <asm/io.h>
  52. /* The channel number of Aspeed pwm controller */
  53. #define PWM_ASPEED_NR_PWMS 16
  54. /* PWM Control Register */
  55. #define PWM_ASPEED_CTRL(ch) ((ch) * 0x10 + 0x00)
  56. #define PWM_ASPEED_CTRL_LOAD_SEL_RISING_AS_WDT BIT(19)
  57. #define PWM_ASPEED_CTRL_DUTY_LOAD_AS_WDT_ENABLE BIT(18)
  58. #define PWM_ASPEED_CTRL_DUTY_SYNC_DISABLE BIT(17)
  59. #define PWM_ASPEED_CTRL_CLK_ENABLE BIT(16)
  60. #define PWM_ASPEED_CTRL_LEVEL_OUTPUT BIT(15)
  61. #define PWM_ASPEED_CTRL_INVERSE BIT(14)
  62. #define PWM_ASPEED_CTRL_OPEN_DRAIN_ENABLE BIT(13)
  63. #define PWM_ASPEED_CTRL_PIN_ENABLE BIT(12)
  64. #define PWM_ASPEED_CTRL_CLK_DIV_H GENMASK(11, 8)
  65. #define PWM_ASPEED_CTRL_CLK_DIV_L GENMASK(7, 0)
  66. /* PWM Duty Cycle Register */
  67. #define PWM_ASPEED_DUTY_CYCLE(ch) ((ch) * 0x10 + 0x04)
  68. #define PWM_ASPEED_DUTY_CYCLE_PERIOD GENMASK(31, 24)
  69. #define PWM_ASPEED_DUTY_CYCLE_POINT_AS_WDT GENMASK(23, 16)
  70. #define PWM_ASPEED_DUTY_CYCLE_FALLING_POINT GENMASK(15, 8)
  71. #define PWM_ASPEED_DUTY_CYCLE_RISING_POINT GENMASK(7, 0)
  72. /* PWM fixed value */
  73. #define PWM_ASPEED_FIXED_PERIOD 0xff
  74. #define NSEC_PER_SEC 1000000000L
  75. struct aspeed_pwm_priv {
  76. struct clk clk;
  77. struct regmap *regmap;
  78. struct reset_ctl reset;
  79. };
  80. static int aspeed_pwm_set_invert(struct udevice *dev, uint channel, bool polarity)
  81. {
  82. struct aspeed_pwm_priv *priv = dev_get_priv(dev);
  83. if (channel >= PWM_ASPEED_NR_PWMS)
  84. return -EINVAL;
  85. regmap_update_bits(priv->regmap, PWM_ASPEED_CTRL(channel),
  86. PWM_ASPEED_CTRL_INVERSE,
  87. FIELD_PREP(PWM_ASPEED_CTRL_INVERSE,
  88. polarity));
  89. return 0;
  90. }
  91. static int aspeed_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
  92. {
  93. struct aspeed_pwm_priv *priv = dev_get_priv(dev);
  94. if (channel >= PWM_ASPEED_NR_PWMS)
  95. return -EINVAL;
  96. regmap_update_bits(priv->regmap, PWM_ASPEED_CTRL(channel),
  97. PWM_ASPEED_CTRL_PIN_ENABLE,
  98. enable ? PWM_ASPEED_CTRL_PIN_ENABLE : 0);
  99. return 0;
  100. }
  101. static int aspeed_pwm_set_config(struct udevice *dev, uint channel,
  102. uint period_ns, uint duty_ns)
  103. {
  104. struct aspeed_pwm_priv *priv = dev_get_priv(dev);
  105. u32 duty_pt;
  106. unsigned long rate;
  107. u64 div_h, div_l, divisor;
  108. bool clk_en;
  109. if (channel >= PWM_ASPEED_NR_PWMS)
  110. return -EINVAL;
  111. dev_dbg(dev, "expect period: %dns, duty_cycle: %dns\n", period_ns,
  112. duty_ns);
  113. rate = clk_get_rate(&priv->clk);
  114. /*
  115. * Pick the smallest value for div_h so that div_l can be the biggest
  116. * which results in a finer resolution near the target period value.
  117. */
  118. divisor = (u64)NSEC_PER_SEC * (PWM_ASPEED_FIXED_PERIOD + 1) *
  119. (PWM_ASPEED_CTRL_CLK_DIV_L + 1);
  120. div_h = order_base_2(div64_u64((u64)rate * period_ns + divisor - 1, divisor));
  121. if (div_h > 0xf)
  122. div_h = 0xf;
  123. divisor = ((u64)NSEC_PER_SEC * (PWM_ASPEED_FIXED_PERIOD + 1)) << div_h;
  124. div_l = div64_u64((u64)rate * period_ns, divisor);
  125. if (div_l == 0)
  126. return -ERANGE;
  127. div_l -= 1;
  128. if (div_l > 255)
  129. div_l = 255;
  130. dev_dbg(dev, "clk source: %ld div_h %lld, div_l : %lld\n", rate, div_h,
  131. div_l);
  132. /* duty_pt = duty_cycle * (PERIOD + 1) / period */
  133. duty_pt = div64_u64(duty_ns * (u64)rate,
  134. (u64)NSEC_PER_SEC * (div_l + 1) << div_h);
  135. dev_dbg(dev, "duty_cycle = %d, duty_pt = %d\n", duty_ns,
  136. duty_pt);
  137. if (duty_pt == 0) {
  138. clk_en = 0;
  139. } else {
  140. clk_en = 1;
  141. if (duty_pt >= (PWM_ASPEED_FIXED_PERIOD + 1))
  142. duty_pt = 0;
  143. /*
  144. * Fixed DUTY_CYCLE_PERIOD to its max value to get a
  145. * fine-grained resolution for duty_cycle at the expense of a
  146. * coarser period resolution.
  147. */
  148. regmap_update_bits(priv->regmap, PWM_ASPEED_DUTY_CYCLE(channel),
  149. PWM_ASPEED_DUTY_CYCLE_PERIOD |
  150. PWM_ASPEED_DUTY_CYCLE_RISING_POINT |
  151. PWM_ASPEED_DUTY_CYCLE_FALLING_POINT,
  152. FIELD_PREP(PWM_ASPEED_DUTY_CYCLE_PERIOD,
  153. PWM_ASPEED_FIXED_PERIOD) |
  154. FIELD_PREP(PWM_ASPEED_DUTY_CYCLE_FALLING_POINT,
  155. duty_pt));
  156. }
  157. regmap_update_bits(priv->regmap, PWM_ASPEED_CTRL(channel),
  158. PWM_ASPEED_CTRL_CLK_DIV_H |
  159. PWM_ASPEED_CTRL_CLK_DIV_L |
  160. PWM_ASPEED_CTRL_CLK_ENABLE,
  161. FIELD_PREP(PWM_ASPEED_CTRL_CLK_DIV_H, div_h) |
  162. FIELD_PREP(PWM_ASPEED_CTRL_CLK_DIV_L, div_l) |
  163. FIELD_PREP(PWM_ASPEED_CTRL_CLK_ENABLE, clk_en));
  164. return 0;
  165. }
  166. static int aspeed_pwm_probe(struct udevice *dev)
  167. {
  168. int ret;
  169. struct aspeed_pwm_priv *priv = dev_get_priv(dev);
  170. struct udevice *parent_dev = dev_get_parent(dev);
  171. priv->regmap = syscon_node_to_regmap(dev_ofnode(dev->parent));
  172. if (IS_ERR(priv->regmap)) {
  173. dev_err(dev, "Couldn't get regmap\n");
  174. return PTR_ERR(priv->regmap);
  175. }
  176. ret = clk_get_by_index(parent_dev, 0, &priv->clk);
  177. if (ret < 0) {
  178. dev_err(dev, "get clock failed\n");
  179. return ret;
  180. }
  181. ret = reset_get_by_index(parent_dev, 0, &priv->reset);
  182. if (ret) {
  183. dev_err(dev, "get reset failed\n");
  184. return ret;
  185. }
  186. ret = reset_deassert(&priv->reset);
  187. if (ret) {
  188. dev_err(dev, "cannot deassert reset control: %pe\n",
  189. ERR_PTR(ret));
  190. return ret;
  191. }
  192. return 0;
  193. }
  194. static int aspeed_pwm_remove(struct udevice *dev)
  195. {
  196. struct aspeed_pwm_priv *priv = dev_get_priv(dev);
  197. reset_assert(&priv->reset);
  198. return 0;
  199. }
  200. static const struct pwm_ops aspeed_pwm_ops = {
  201. .set_invert = aspeed_pwm_set_invert,
  202. .set_config = aspeed_pwm_set_config,
  203. .set_enable = aspeed_pwm_set_enable,
  204. };
  205. static const struct udevice_id aspeed_pwm_ids[] = {
  206. { .compatible = "aspeed,ast2600-pwm" },
  207. { }
  208. };
  209. U_BOOT_DRIVER(aspeed_pwm) = {
  210. .name = "aspeed_pwm",
  211. .id = UCLASS_PWM,
  212. .of_match = aspeed_pwm_ids,
  213. .ops = &aspeed_pwm_ops,
  214. .probe = aspeed_pwm_probe,
  215. .remove = aspeed_pwm_remove,
  216. .priv_auto = sizeof(struct aspeed_pwm_priv),
  217. };