pwm-xilinx.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Sean Anderson <sean.anderson@seco.com>
  4. *
  5. * Limitations:
  6. * - When changing both duty cycle and period, we may end up with one cycle
  7. * with the old duty cycle and the new period. This is because the counters
  8. * may only be reloaded by first stopping them, or by letting them be
  9. * automatically reloaded at the end of a cycle. If this automatic reload
  10. * happens after we set TLR0 but before we set TLR1 then we will have a
  11. * bad cycle. This could probably be fixed by reading TCR0 just before
  12. * reprogramming, but I think it would add complexity for little gain.
  13. * - Cannot produce 100% duty cycle by configuring the TLRs. This might be
  14. * possible by stopping the counters at an appropriate point in the cycle,
  15. * but this is not (yet) implemented.
  16. * - Only produces "normal" output.
  17. * - Always produces low output if disabled.
  18. */
  19. #include <clocksource/timer-xilinx.h>
  20. #include <linux/clk.h>
  21. #include <linux/clk-provider.h>
  22. #include <linux/device.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pwm.h>
  27. #include <linux/regmap.h>
  28. /*
  29. * The following functions are "common" to drivers for this device, and may be
  30. * exported at a future date.
  31. */
  32. u32 xilinx_timer_tlr_cycles(struct xilinx_timer_priv *priv, u32 tcsr,
  33. u64 cycles)
  34. {
  35. WARN_ON(cycles < 2 || cycles - 2 > priv->max);
  36. if (tcsr & TCSR_UDT)
  37. return cycles - 2;
  38. return priv->max - cycles + 2;
  39. }
  40. unsigned int xilinx_timer_get_period(struct xilinx_timer_priv *priv,
  41. u32 tlr, u32 tcsr)
  42. {
  43. u64 cycles;
  44. if (tcsr & TCSR_UDT)
  45. cycles = tlr + 2;
  46. else
  47. cycles = (u64)priv->max - tlr + 2;
  48. /* cycles has a max of 2^32 + 2, so we can't overflow */
  49. return DIV64_U64_ROUND_UP(cycles * NSEC_PER_SEC,
  50. clk_get_rate(priv->clk));
  51. }
  52. /*
  53. * The idea here is to capture whether the PWM is actually running (e.g.
  54. * because we or the bootloader set it up) and we need to be careful to ensure
  55. * we don't cause a glitch. According to the data sheet, to enable the PWM we
  56. * need to
  57. *
  58. * - Set both timers to generate mode (MDT=1)
  59. * - Set both timers to PWM mode (PWMA=1)
  60. * - Enable the generate out signals (GENT=1)
  61. *
  62. * In addition,
  63. *
  64. * - The timer must be running (ENT=1)
  65. * - The timer must auto-reload TLR into TCR (ARHT=1)
  66. * - We must not be in the process of loading TLR into TCR (LOAD=0)
  67. * - Cascade mode must be disabled (CASC=0)
  68. *
  69. * If any of these differ from usual, then the PWM is either disabled, or is
  70. * running in a mode that this driver does not support.
  71. */
  72. #define TCSR_PWM_SET (TCSR_GENT | TCSR_ARHT | TCSR_ENT | TCSR_PWMA)
  73. #define TCSR_PWM_CLEAR (TCSR_MDT | TCSR_LOAD)
  74. #define TCSR_PWM_MASK (TCSR_PWM_SET | TCSR_PWM_CLEAR)
  75. static inline struct xilinx_timer_priv
  76. *xilinx_pwm_chip_to_priv(struct pwm_chip *chip)
  77. {
  78. return pwmchip_get_drvdata(chip);
  79. }
  80. static bool xilinx_timer_pwm_enabled(u32 tcsr0, u32 tcsr1)
  81. {
  82. return ((TCSR_PWM_MASK | TCSR_CASC) & tcsr0) == TCSR_PWM_SET &&
  83. (TCSR_PWM_MASK & tcsr1) == TCSR_PWM_SET;
  84. }
  85. static int xilinx_pwm_apply(struct pwm_chip *chip, struct pwm_device *unused,
  86. const struct pwm_state *state)
  87. {
  88. struct xilinx_timer_priv *priv = xilinx_pwm_chip_to_priv(chip);
  89. u32 tlr0, tlr1, tcsr0, tcsr1;
  90. u64 period_cycles, duty_cycles;
  91. unsigned long rate;
  92. if (state->polarity != PWM_POLARITY_NORMAL)
  93. return -EINVAL;
  94. /*
  95. * To be representable by TLR, cycles must be between 2 and
  96. * priv->max + 2. To enforce this we can reduce the cycles, but we may
  97. * not increase them. Caveat emptor: while this does result in more
  98. * predictable rounding, it may also result in a completely different
  99. * duty cycle (% high time) than what was requested.
  100. */
  101. rate = clk_get_rate(priv->clk);
  102. /* Avoid overflow */
  103. period_cycles = min_t(u64, state->period, U32_MAX * NSEC_PER_SEC);
  104. period_cycles = mul_u64_u32_div(period_cycles, rate, NSEC_PER_SEC);
  105. period_cycles = min_t(u64, period_cycles, priv->max + 2);
  106. if (period_cycles < 2)
  107. return -ERANGE;
  108. /* Same thing for duty cycles */
  109. duty_cycles = min_t(u64, state->duty_cycle, U32_MAX * NSEC_PER_SEC);
  110. duty_cycles = mul_u64_u32_div(duty_cycles, rate, NSEC_PER_SEC);
  111. duty_cycles = min_t(u64, duty_cycles, priv->max + 2);
  112. /*
  113. * If we specify 100% duty cycle, we will get 0% instead, so decrease
  114. * the duty cycle count by one.
  115. */
  116. if (duty_cycles >= period_cycles)
  117. duty_cycles = period_cycles - 1;
  118. /* Round down to 0% duty cycle for unrepresentable duty cycles */
  119. if (duty_cycles < 2)
  120. duty_cycles = period_cycles;
  121. regmap_read(priv->map, TCSR0, &tcsr0);
  122. regmap_read(priv->map, TCSR1, &tcsr1);
  123. tlr0 = xilinx_timer_tlr_cycles(priv, tcsr0, period_cycles);
  124. tlr1 = xilinx_timer_tlr_cycles(priv, tcsr1, duty_cycles);
  125. regmap_write(priv->map, TLR0, tlr0);
  126. regmap_write(priv->map, TLR1, tlr1);
  127. if (state->enabled) {
  128. /*
  129. * If the PWM is already running, then the counters will be
  130. * reloaded at the end of the current cycle.
  131. */
  132. if (!xilinx_timer_pwm_enabled(tcsr0, tcsr1)) {
  133. /* Load TLR into TCR */
  134. regmap_write(priv->map, TCSR0, tcsr0 | TCSR_LOAD);
  135. regmap_write(priv->map, TCSR1, tcsr1 | TCSR_LOAD);
  136. /* Enable timers all at once with ENALL */
  137. tcsr0 = (TCSR_PWM_SET & ~TCSR_ENT) | (tcsr0 & TCSR_UDT);
  138. tcsr1 = TCSR_PWM_SET | TCSR_ENALL | (tcsr1 & TCSR_UDT);
  139. regmap_write(priv->map, TCSR0, tcsr0);
  140. regmap_write(priv->map, TCSR1, tcsr1);
  141. }
  142. } else {
  143. regmap_write(priv->map, TCSR0, 0);
  144. regmap_write(priv->map, TCSR1, 0);
  145. }
  146. return 0;
  147. }
  148. static int xilinx_pwm_get_state(struct pwm_chip *chip,
  149. struct pwm_device *unused,
  150. struct pwm_state *state)
  151. {
  152. struct xilinx_timer_priv *priv = xilinx_pwm_chip_to_priv(chip);
  153. u32 tlr0, tlr1, tcsr0, tcsr1;
  154. regmap_read(priv->map, TLR0, &tlr0);
  155. regmap_read(priv->map, TLR1, &tlr1);
  156. regmap_read(priv->map, TCSR0, &tcsr0);
  157. regmap_read(priv->map, TCSR1, &tcsr1);
  158. state->period = xilinx_timer_get_period(priv, tlr0, tcsr0);
  159. state->duty_cycle = xilinx_timer_get_period(priv, tlr1, tcsr1);
  160. state->enabled = xilinx_timer_pwm_enabled(tcsr0, tcsr1);
  161. state->polarity = PWM_POLARITY_NORMAL;
  162. /*
  163. * 100% duty cycle results in constant low output. This may be (very)
  164. * wrong if rate > 1 GHz, so fix this if you have such hardware :)
  165. */
  166. if (state->period == state->duty_cycle)
  167. state->duty_cycle = 0;
  168. return 0;
  169. }
  170. static const struct pwm_ops xilinx_pwm_ops = {
  171. .apply = xilinx_pwm_apply,
  172. .get_state = xilinx_pwm_get_state,
  173. };
  174. static const struct regmap_config xilinx_pwm_regmap_config = {
  175. .reg_bits = 32,
  176. .reg_stride = 4,
  177. .val_bits = 32,
  178. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  179. .max_register = TCR1,
  180. };
  181. static int xilinx_pwm_probe(struct platform_device *pdev)
  182. {
  183. int ret;
  184. struct device *dev = &pdev->dev;
  185. struct device_node *np = dev->of_node;
  186. struct xilinx_timer_priv *priv;
  187. struct pwm_chip *chip;
  188. u32 pwm_cells, one_timer, width;
  189. void __iomem *regs;
  190. /* If there are no PWM cells, this binding is for a timer */
  191. ret = of_property_read_u32(np, "#pwm-cells", &pwm_cells);
  192. if (ret == -EINVAL)
  193. return -ENODEV;
  194. if (ret)
  195. return dev_err_probe(dev, ret, "could not read #pwm-cells\n");
  196. chip = devm_pwmchip_alloc(dev, 1, sizeof(*priv));
  197. if (IS_ERR(chip))
  198. return PTR_ERR(chip);
  199. priv = xilinx_pwm_chip_to_priv(chip);
  200. regs = devm_platform_ioremap_resource(pdev, 0);
  201. if (IS_ERR(regs))
  202. return PTR_ERR(regs);
  203. priv->map = devm_regmap_init_mmio(dev, regs,
  204. &xilinx_pwm_regmap_config);
  205. if (IS_ERR(priv->map))
  206. return dev_err_probe(dev, PTR_ERR(priv->map),
  207. "Could not create regmap\n");
  208. ret = of_property_read_u32(np, "xlnx,one-timer-only", &one_timer);
  209. if (ret)
  210. return dev_err_probe(dev, ret,
  211. "Could not read xlnx,one-timer-only\n");
  212. if (one_timer)
  213. return dev_err_probe(dev, -EINVAL,
  214. "Two timers required for PWM mode\n");
  215. ret = of_property_read_u32(np, "xlnx,count-width", &width);
  216. if (ret == -EINVAL)
  217. width = 32;
  218. else if (ret)
  219. return dev_err_probe(dev, ret,
  220. "Could not read xlnx,count-width\n");
  221. if (width != 8 && width != 16 && width != 32)
  222. return dev_err_probe(dev, -EINVAL,
  223. "Invalid counter width %d\n", width);
  224. priv->max = BIT_ULL(width) - 1;
  225. /*
  226. * The polarity of the Generate Out signals must be active high for PWM
  227. * mode to work. We could determine this from the device tree, but
  228. * alas, such properties are not allowed to be used.
  229. */
  230. priv->clk = devm_clk_get_enabled(dev, "s_axi_aclk");
  231. if (IS_ERR(priv->clk))
  232. return dev_err_probe(dev, PTR_ERR(priv->clk),
  233. "Could not get clock\n");
  234. ret = devm_clk_rate_exclusive_get(dev, priv->clk);
  235. if (ret)
  236. return dev_err_probe(dev, ret,
  237. "Failed to lock clock rate\n");
  238. chip->ops = &xilinx_pwm_ops;
  239. ret = devm_pwmchip_add(dev, chip);
  240. if (ret)
  241. return dev_err_probe(dev, ret, "Could not register PWM chip\n");
  242. return 0;
  243. }
  244. static const struct of_device_id xilinx_pwm_of_match[] = {
  245. { .compatible = "xlnx,xps-timer-1.00.a", },
  246. {},
  247. };
  248. MODULE_DEVICE_TABLE(of, xilinx_pwm_of_match);
  249. static struct platform_driver xilinx_pwm_driver = {
  250. .probe = xilinx_pwm_probe,
  251. .driver = {
  252. .name = "xilinx-pwm",
  253. .of_match_table = of_match_ptr(xilinx_pwm_of_match),
  254. },
  255. };
  256. module_platform_driver(xilinx_pwm_driver);
  257. MODULE_ALIAS("platform:xilinx-pwm");
  258. MODULE_DESCRIPTION("PWM driver for Xilinx LogiCORE IP AXI Timer");
  259. MODULE_LICENSE("GPL");