pwm-xilinx.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Sean Anderson <[email protected]>
  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. struct xilinx_pwm_device {
  76. struct pwm_chip chip;
  77. struct xilinx_timer_priv priv;
  78. };
  79. static inline struct xilinx_timer_priv
  80. *xilinx_pwm_chip_to_priv(struct pwm_chip *chip)
  81. {
  82. return &container_of(chip, struct xilinx_pwm_device, chip)->priv;
  83. }
  84. static bool xilinx_timer_pwm_enabled(u32 tcsr0, u32 tcsr1)
  85. {
  86. return ((TCSR_PWM_MASK | TCSR_CASC) & tcsr0) == TCSR_PWM_SET &&
  87. (TCSR_PWM_MASK & tcsr1) == TCSR_PWM_SET;
  88. }
  89. static int xilinx_pwm_apply(struct pwm_chip *chip, struct pwm_device *unused,
  90. const struct pwm_state *state)
  91. {
  92. struct xilinx_timer_priv *priv = xilinx_pwm_chip_to_priv(chip);
  93. u32 tlr0, tlr1, tcsr0, tcsr1;
  94. u64 period_cycles, duty_cycles;
  95. unsigned long rate;
  96. if (state->polarity != PWM_POLARITY_NORMAL)
  97. return -EINVAL;
  98. /*
  99. * To be representable by TLR, cycles must be between 2 and
  100. * priv->max + 2. To enforce this we can reduce the cycles, but we may
  101. * not increase them. Caveat emptor: while this does result in more
  102. * predictable rounding, it may also result in a completely different
  103. * duty cycle (% high time) than what was requested.
  104. */
  105. rate = clk_get_rate(priv->clk);
  106. /* Avoid overflow */
  107. period_cycles = min_t(u64, state->period, U32_MAX * NSEC_PER_SEC);
  108. period_cycles = mul_u64_u32_div(period_cycles, rate, NSEC_PER_SEC);
  109. period_cycles = min_t(u64, period_cycles, priv->max + 2);
  110. if (period_cycles < 2)
  111. return -ERANGE;
  112. /* Same thing for duty cycles */
  113. duty_cycles = min_t(u64, state->duty_cycle, U32_MAX * NSEC_PER_SEC);
  114. duty_cycles = mul_u64_u32_div(duty_cycles, rate, NSEC_PER_SEC);
  115. duty_cycles = min_t(u64, duty_cycles, priv->max + 2);
  116. /*
  117. * If we specify 100% duty cycle, we will get 0% instead, so decrease
  118. * the duty cycle count by one.
  119. */
  120. if (duty_cycles >= period_cycles)
  121. duty_cycles = period_cycles - 1;
  122. /* Round down to 0% duty cycle for unrepresentable duty cycles */
  123. if (duty_cycles < 2)
  124. duty_cycles = period_cycles;
  125. regmap_read(priv->map, TCSR0, &tcsr0);
  126. regmap_read(priv->map, TCSR1, &tcsr1);
  127. tlr0 = xilinx_timer_tlr_cycles(priv, tcsr0, period_cycles);
  128. tlr1 = xilinx_timer_tlr_cycles(priv, tcsr1, duty_cycles);
  129. regmap_write(priv->map, TLR0, tlr0);
  130. regmap_write(priv->map, TLR1, tlr1);
  131. if (state->enabled) {
  132. /*
  133. * If the PWM is already running, then the counters will be
  134. * reloaded at the end of the current cycle.
  135. */
  136. if (!xilinx_timer_pwm_enabled(tcsr0, tcsr1)) {
  137. /* Load TLR into TCR */
  138. regmap_write(priv->map, TCSR0, tcsr0 | TCSR_LOAD);
  139. regmap_write(priv->map, TCSR1, tcsr1 | TCSR_LOAD);
  140. /* Enable timers all at once with ENALL */
  141. tcsr0 = (TCSR_PWM_SET & ~TCSR_ENT) | (tcsr0 & TCSR_UDT);
  142. tcsr1 = TCSR_PWM_SET | TCSR_ENALL | (tcsr1 & TCSR_UDT);
  143. regmap_write(priv->map, TCSR0, tcsr0);
  144. regmap_write(priv->map, TCSR1, tcsr1);
  145. }
  146. } else {
  147. regmap_write(priv->map, TCSR0, 0);
  148. regmap_write(priv->map, TCSR1, 0);
  149. }
  150. return 0;
  151. }
  152. static int xilinx_pwm_get_state(struct pwm_chip *chip,
  153. struct pwm_device *unused,
  154. struct pwm_state *state)
  155. {
  156. struct xilinx_timer_priv *priv = xilinx_pwm_chip_to_priv(chip);
  157. u32 tlr0, tlr1, tcsr0, tcsr1;
  158. regmap_read(priv->map, TLR0, &tlr0);
  159. regmap_read(priv->map, TLR1, &tlr1);
  160. regmap_read(priv->map, TCSR0, &tcsr0);
  161. regmap_read(priv->map, TCSR1, &tcsr1);
  162. state->period = xilinx_timer_get_period(priv, tlr0, tcsr0);
  163. state->duty_cycle = xilinx_timer_get_period(priv, tlr1, tcsr1);
  164. state->enabled = xilinx_timer_pwm_enabled(tcsr0, tcsr1);
  165. state->polarity = PWM_POLARITY_NORMAL;
  166. /*
  167. * 100% duty cycle results in constant low output. This may be (very)
  168. * wrong if rate > 1 GHz, so fix this if you have such hardware :)
  169. */
  170. if (state->period == state->duty_cycle)
  171. state->duty_cycle = 0;
  172. return 0;
  173. }
  174. static const struct pwm_ops xilinx_pwm_ops = {
  175. .apply = xilinx_pwm_apply,
  176. .get_state = xilinx_pwm_get_state,
  177. .owner = THIS_MODULE,
  178. };
  179. static const struct regmap_config xilinx_pwm_regmap_config = {
  180. .reg_bits = 32,
  181. .reg_stride = 4,
  182. .val_bits = 32,
  183. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  184. .max_register = TCR1,
  185. };
  186. static int xilinx_pwm_probe(struct platform_device *pdev)
  187. {
  188. int ret;
  189. struct device *dev = &pdev->dev;
  190. struct device_node *np = dev->of_node;
  191. struct xilinx_timer_priv *priv;
  192. struct xilinx_pwm_device *xilinx_pwm;
  193. u32 pwm_cells, one_timer, width;
  194. void __iomem *regs;
  195. /* If there are no PWM cells, this binding is for a timer */
  196. ret = of_property_read_u32(np, "#pwm-cells", &pwm_cells);
  197. if (ret == -EINVAL)
  198. return -ENODEV;
  199. if (ret)
  200. return dev_err_probe(dev, ret, "could not read #pwm-cells\n");
  201. xilinx_pwm = devm_kzalloc(dev, sizeof(*xilinx_pwm), GFP_KERNEL);
  202. if (!xilinx_pwm)
  203. return -ENOMEM;
  204. platform_set_drvdata(pdev, xilinx_pwm);
  205. priv = &xilinx_pwm->priv;
  206. regs = devm_platform_ioremap_resource(pdev, 0);
  207. if (IS_ERR(regs))
  208. return PTR_ERR(regs);
  209. priv->map = devm_regmap_init_mmio(dev, regs,
  210. &xilinx_pwm_regmap_config);
  211. if (IS_ERR(priv->map))
  212. return dev_err_probe(dev, PTR_ERR(priv->map),
  213. "Could not create regmap\n");
  214. ret = of_property_read_u32(np, "xlnx,one-timer-only", &one_timer);
  215. if (ret)
  216. return dev_err_probe(dev, ret,
  217. "Could not read xlnx,one-timer-only\n");
  218. if (one_timer)
  219. return dev_err_probe(dev, -EINVAL,
  220. "Two timers required for PWM mode\n");
  221. ret = of_property_read_u32(np, "xlnx,count-width", &width);
  222. if (ret == -EINVAL)
  223. width = 32;
  224. else if (ret)
  225. return dev_err_probe(dev, ret,
  226. "Could not read xlnx,count-width\n");
  227. if (width != 8 && width != 16 && width != 32)
  228. return dev_err_probe(dev, -EINVAL,
  229. "Invalid counter width %d\n", width);
  230. priv->max = BIT_ULL(width) - 1;
  231. /*
  232. * The polarity of the Generate Out signals must be active high for PWM
  233. * mode to work. We could determine this from the device tree, but
  234. * alas, such properties are not allowed to be used.
  235. */
  236. priv->clk = devm_clk_get(dev, "s_axi_aclk");
  237. if (IS_ERR(priv->clk))
  238. return dev_err_probe(dev, PTR_ERR(priv->clk),
  239. "Could not get clock\n");
  240. ret = clk_prepare_enable(priv->clk);
  241. if (ret)
  242. return dev_err_probe(dev, ret, "Clock enable failed\n");
  243. clk_rate_exclusive_get(priv->clk);
  244. xilinx_pwm->chip.dev = dev;
  245. xilinx_pwm->chip.ops = &xilinx_pwm_ops;
  246. xilinx_pwm->chip.npwm = 1;
  247. ret = pwmchip_add(&xilinx_pwm->chip);
  248. if (ret) {
  249. clk_rate_exclusive_put(priv->clk);
  250. clk_disable_unprepare(priv->clk);
  251. return dev_err_probe(dev, ret, "Could not register PWM chip\n");
  252. }
  253. return 0;
  254. }
  255. static int xilinx_pwm_remove(struct platform_device *pdev)
  256. {
  257. struct xilinx_pwm_device *xilinx_pwm = platform_get_drvdata(pdev);
  258. pwmchip_remove(&xilinx_pwm->chip);
  259. clk_rate_exclusive_put(xilinx_pwm->priv.clk);
  260. clk_disable_unprepare(xilinx_pwm->priv.clk);
  261. return 0;
  262. }
  263. static const struct of_device_id xilinx_pwm_of_match[] = {
  264. { .compatible = "xlnx,xps-timer-1.00.a", },
  265. {},
  266. };
  267. MODULE_DEVICE_TABLE(of, xilinx_pwm_of_match);
  268. static struct platform_driver xilinx_pwm_driver = {
  269. .probe = xilinx_pwm_probe,
  270. .remove = xilinx_pwm_remove,
  271. .driver = {
  272. .name = "xilinx-pwm",
  273. .of_match_table = of_match_ptr(xilinx_pwm_of_match),
  274. },
  275. };
  276. module_platform_driver(xilinx_pwm_driver);
  277. MODULE_ALIAS("platform:xilinx-pwm");
  278. MODULE_DESCRIPTION("PWM driver for Xilinx LogiCORE IP AXI Timer");
  279. MODULE_LICENSE("GPL");