pwm-imx27.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * simple driver for PWM (Pulse Width Modulator) controller
  4. *
  5. * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
  6. *
  7. * Limitations:
  8. * - When disabled the output is driven to 0 independent of the configured
  9. * polarity.
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/bitops.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pwm.h>
  22. #include <linux/slab.h>
  23. #define MX3_PWMCR 0x00 /* PWM Control Register */
  24. #define MX3_PWMSR 0x04 /* PWM Status Register */
  25. #define MX3_PWMSAR 0x0C /* PWM Sample Register */
  26. #define MX3_PWMPR 0x10 /* PWM Period Register */
  27. #define MX3_PWMCNR 0x14 /* PWM Counter Register */
  28. #define MX3_PWMCR_FWM GENMASK(27, 26)
  29. #define MX3_PWMCR_STOPEN BIT(25)
  30. #define MX3_PWMCR_DOZEN BIT(24)
  31. #define MX3_PWMCR_WAITEN BIT(23)
  32. #define MX3_PWMCR_DBGEN BIT(22)
  33. #define MX3_PWMCR_BCTR BIT(21)
  34. #define MX3_PWMCR_HCTR BIT(20)
  35. #define MX3_PWMCR_POUTC GENMASK(19, 18)
  36. #define MX3_PWMCR_POUTC_NORMAL 0
  37. #define MX3_PWMCR_POUTC_INVERTED 1
  38. #define MX3_PWMCR_POUTC_OFF 2
  39. #define MX3_PWMCR_CLKSRC GENMASK(17, 16)
  40. #define MX3_PWMCR_CLKSRC_OFF 0
  41. #define MX3_PWMCR_CLKSRC_IPG 1
  42. #define MX3_PWMCR_CLKSRC_IPG_HIGH 2
  43. #define MX3_PWMCR_CLKSRC_IPG_32K 3
  44. #define MX3_PWMCR_PRESCALER GENMASK(15, 4)
  45. #define MX3_PWMCR_SWR BIT(3)
  46. #define MX3_PWMCR_REPEAT GENMASK(2, 1)
  47. #define MX3_PWMCR_REPEAT_1X 0
  48. #define MX3_PWMCR_REPEAT_2X 1
  49. #define MX3_PWMCR_REPEAT_4X 2
  50. #define MX3_PWMCR_REPEAT_8X 3
  51. #define MX3_PWMCR_EN BIT(0)
  52. #define MX3_PWMSR_FWE BIT(6)
  53. #define MX3_PWMSR_CMP BIT(5)
  54. #define MX3_PWMSR_ROV BIT(4)
  55. #define MX3_PWMSR_FE BIT(3)
  56. #define MX3_PWMSR_FIFOAV GENMASK(2, 0)
  57. #define MX3_PWMSR_FIFOAV_EMPTY 0
  58. #define MX3_PWMSR_FIFOAV_1WORD 1
  59. #define MX3_PWMSR_FIFOAV_2WORDS 2
  60. #define MX3_PWMSR_FIFOAV_3WORDS 3
  61. #define MX3_PWMSR_FIFOAV_4WORDS 4
  62. #define MX3_PWMCR_PRESCALER_SET(x) FIELD_PREP(MX3_PWMCR_PRESCALER, (x) - 1)
  63. #define MX3_PWMCR_PRESCALER_GET(x) (FIELD_GET(MX3_PWMCR_PRESCALER, \
  64. (x)) + 1)
  65. #define MX3_PWM_SWR_LOOP 5
  66. /* PWMPR register value of 0xffff has the same effect as 0xfffe */
  67. #define MX3_PWMPR_MAX 0xfffe
  68. static const char * const pwm_imx27_clks[] = {"ipg", "per"};
  69. #define PWM_IMX27_PER 1
  70. struct pwm_imx27_chip {
  71. struct clk_bulk_data clks[ARRAY_SIZE(pwm_imx27_clks)];
  72. int clks_cnt;
  73. void __iomem *mmio_base;
  74. /*
  75. * The driver cannot read the current duty cycle from the hardware if
  76. * the hardware is disabled. Cache the last programmed duty cycle
  77. * value to return in that case.
  78. */
  79. unsigned int duty_cycle;
  80. };
  81. static inline struct pwm_imx27_chip *to_pwm_imx27_chip(struct pwm_chip *chip)
  82. {
  83. return pwmchip_get_drvdata(chip);
  84. }
  85. static int pwm_imx27_get_state(struct pwm_chip *chip,
  86. struct pwm_device *pwm, struct pwm_state *state)
  87. {
  88. struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
  89. u32 period, prescaler, pwm_clk, val;
  90. u64 tmp;
  91. int ret;
  92. ret = clk_bulk_prepare_enable(imx->clks_cnt, imx->clks);
  93. if (ret < 0)
  94. return ret;
  95. val = readl(imx->mmio_base + MX3_PWMCR);
  96. if (val & MX3_PWMCR_EN)
  97. state->enabled = true;
  98. else
  99. state->enabled = false;
  100. switch (FIELD_GET(MX3_PWMCR_POUTC, val)) {
  101. case MX3_PWMCR_POUTC_NORMAL:
  102. state->polarity = PWM_POLARITY_NORMAL;
  103. break;
  104. case MX3_PWMCR_POUTC_INVERTED:
  105. state->polarity = PWM_POLARITY_INVERSED;
  106. break;
  107. default:
  108. dev_warn(pwmchip_parent(chip), "can't set polarity, output disconnected");
  109. }
  110. prescaler = MX3_PWMCR_PRESCALER_GET(val);
  111. pwm_clk = clk_get_rate(imx->clks[PWM_IMX27_PER].clk);
  112. val = readl(imx->mmio_base + MX3_PWMPR);
  113. period = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
  114. /* PWMOUT (Hz) = PWMCLK / (PWMPR + 2) */
  115. tmp = NSEC_PER_SEC * (u64)(period + 2) * prescaler;
  116. state->period = DIV_ROUND_UP_ULL(tmp, pwm_clk);
  117. /*
  118. * PWMSAR can be read only if PWM is enabled. If the PWM is disabled,
  119. * use the cached value.
  120. */
  121. if (state->enabled)
  122. val = readl(imx->mmio_base + MX3_PWMSAR);
  123. else
  124. val = imx->duty_cycle;
  125. tmp = NSEC_PER_SEC * (u64)(val) * prescaler;
  126. state->duty_cycle = DIV_ROUND_UP_ULL(tmp, pwm_clk);
  127. clk_bulk_disable_unprepare(imx->clks_cnt, imx->clks);
  128. return 0;
  129. }
  130. static void pwm_imx27_sw_reset(struct pwm_chip *chip)
  131. {
  132. struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
  133. struct device *dev = pwmchip_parent(chip);
  134. int wait_count = 0;
  135. u32 cr;
  136. writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
  137. do {
  138. usleep_range(200, 1000);
  139. cr = readl(imx->mmio_base + MX3_PWMCR);
  140. } while ((cr & MX3_PWMCR_SWR) &&
  141. (wait_count++ < MX3_PWM_SWR_LOOP));
  142. if (cr & MX3_PWMCR_SWR)
  143. dev_warn(dev, "software reset timeout\n");
  144. }
  145. static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip,
  146. struct pwm_device *pwm)
  147. {
  148. struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
  149. struct device *dev = pwmchip_parent(chip);
  150. unsigned int period_ms;
  151. int fifoav;
  152. u32 sr;
  153. sr = readl(imx->mmio_base + MX3_PWMSR);
  154. fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr);
  155. if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
  156. period_ms = DIV_ROUND_UP_ULL(pwm->state.period,
  157. NSEC_PER_MSEC);
  158. msleep(period_ms);
  159. sr = readl(imx->mmio_base + MX3_PWMSR);
  160. if (fifoav == FIELD_GET(MX3_PWMSR_FIFOAV, sr))
  161. dev_warn(dev, "there is no free FIFO slot\n");
  162. }
  163. }
  164. static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  165. const struct pwm_state *state)
  166. {
  167. unsigned long period_cycles, duty_cycles, prescale, period_us, tmp;
  168. struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
  169. unsigned long long c;
  170. unsigned long long clkrate;
  171. unsigned long flags;
  172. int val;
  173. int ret;
  174. u32 cr;
  175. clkrate = clk_get_rate(imx->clks[PWM_IMX27_PER].clk);
  176. c = clkrate * state->period;
  177. do_div(c, NSEC_PER_SEC);
  178. period_cycles = c;
  179. prescale = period_cycles / 0x10000 + 1;
  180. period_cycles /= prescale;
  181. c = clkrate * state->duty_cycle;
  182. do_div(c, NSEC_PER_SEC);
  183. duty_cycles = c;
  184. duty_cycles /= prescale;
  185. /*
  186. * according to imx pwm RM, the real period value should be PERIOD
  187. * value in PWMPR plus 2.
  188. */
  189. if (period_cycles > 2)
  190. period_cycles -= 2;
  191. else
  192. period_cycles = 0;
  193. /*
  194. * Wait for a free FIFO slot if the PWM is already enabled, and flush
  195. * the FIFO if the PWM was disabled and is about to be enabled.
  196. */
  197. if (pwm->state.enabled) {
  198. pwm_imx27_wait_fifo_slot(chip, pwm);
  199. } else {
  200. ret = clk_bulk_prepare_enable(imx->clks_cnt, imx->clks);
  201. if (ret)
  202. return ret;
  203. pwm_imx27_sw_reset(chip);
  204. }
  205. val = readl(imx->mmio_base + MX3_PWMPR);
  206. val = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
  207. cr = readl(imx->mmio_base + MX3_PWMCR);
  208. tmp = NSEC_PER_SEC * (u64)(val + 2) * MX3_PWMCR_PRESCALER_GET(cr);
  209. tmp = DIV_ROUND_UP_ULL(tmp, clkrate);
  210. period_us = DIV_ROUND_UP_ULL(tmp, 1000);
  211. /*
  212. * ERR051198:
  213. * PWM: PWM output may not function correctly if the FIFO is empty when
  214. * a new SAR value is programmed
  215. *
  216. * Description:
  217. * When the PWM FIFO is empty, a new value programmed to the PWM Sample
  218. * register (PWM_PWMSAR) will be directly applied even if the current
  219. * timer period has not expired.
  220. *
  221. * If the new SAMPLE value programmed in the PWM_PWMSAR register is
  222. * less than the previous value, and the PWM counter register
  223. * (PWM_PWMCNR) that contains the current COUNT value is greater than
  224. * the new programmed SAMPLE value, the current period will not flip
  225. * the level. This may result in an output pulse with a duty cycle of
  226. * 100%.
  227. *
  228. * Consider a change from
  229. * ________
  230. * / \______/
  231. * ^ * ^
  232. * to
  233. * ____
  234. * / \__________/
  235. * ^ ^
  236. * At the time marked by *, the new write value will be directly applied
  237. * to SAR even the current period is not over if FIFO is empty.
  238. *
  239. * ________ ____________________
  240. * / \______/ \__________/
  241. * ^ ^ * ^ ^
  242. * |<-- old SAR -->| |<-- new SAR -->|
  243. *
  244. * That is the output is active for a whole period.
  245. *
  246. * Workaround:
  247. * Check new SAR less than old SAR and current counter is in errata
  248. * windows, write extra old SAR into FIFO and new SAR will effect at
  249. * next period.
  250. *
  251. * Sometime period is quite long, such as over 1 second. If add old SAR
  252. * into FIFO unconditional, new SAR have to wait for next period. It
  253. * may be too long.
  254. *
  255. * Turn off the interrupt to ensure that not IRQ and schedule happen
  256. * during above operations. If any irq and schedule happen, counter
  257. * in PWM will be out of data and take wrong action.
  258. *
  259. * Add a safety margin 1.5us because it needs some time to complete
  260. * IO write.
  261. *
  262. * Use writel_relaxed() to minimize the interval between two writes to
  263. * the SAR register to increase the fastest PWM frequency supported.
  264. *
  265. * When the PWM period is longer than 2us(or <500kHz), this workaround
  266. * can solve this problem. No software workaround is available if PWM
  267. * period is shorter than IO write. Just try best to fill old data
  268. * into FIFO.
  269. */
  270. c = clkrate * 1500;
  271. do_div(c, NSEC_PER_SEC);
  272. local_irq_save(flags);
  273. val = FIELD_GET(MX3_PWMSR_FIFOAV, readl_relaxed(imx->mmio_base + MX3_PWMSR));
  274. if (duty_cycles < imx->duty_cycle && (cr & MX3_PWMCR_EN)) {
  275. if (period_us < 2) { /* 2us = 500 kHz */
  276. /* Best effort attempt to fix up >500 kHz case */
  277. udelay(3 * period_us);
  278. writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
  279. writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
  280. } else if (val < MX3_PWMSR_FIFOAV_2WORDS) {
  281. val = readl_relaxed(imx->mmio_base + MX3_PWMCNR);
  282. /*
  283. * If counter is close to period, controller may roll over when
  284. * next IO write.
  285. */
  286. if ((val + c >= duty_cycles && val < imx->duty_cycle) ||
  287. val + c >= period_cycles)
  288. writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
  289. }
  290. }
  291. writel_relaxed(duty_cycles, imx->mmio_base + MX3_PWMSAR);
  292. local_irq_restore(flags);
  293. writel(period_cycles, imx->mmio_base + MX3_PWMPR);
  294. /*
  295. * Store the duty cycle for future reference in cases where the
  296. * MX3_PWMSAR register can't be read (i.e. when the PWM is disabled).
  297. */
  298. imx->duty_cycle = duty_cycles;
  299. cr = MX3_PWMCR_PRESCALER_SET(prescale) |
  300. MX3_PWMCR_STOPEN | MX3_PWMCR_DOZEN | MX3_PWMCR_WAITEN |
  301. FIELD_PREP(MX3_PWMCR_CLKSRC, MX3_PWMCR_CLKSRC_IPG_HIGH) |
  302. MX3_PWMCR_DBGEN;
  303. if (state->polarity == PWM_POLARITY_INVERSED)
  304. cr |= FIELD_PREP(MX3_PWMCR_POUTC,
  305. MX3_PWMCR_POUTC_INVERTED);
  306. if (state->enabled)
  307. cr |= MX3_PWMCR_EN;
  308. writel(cr, imx->mmio_base + MX3_PWMCR);
  309. if (!state->enabled)
  310. clk_bulk_disable_unprepare(imx->clks_cnt, imx->clks);
  311. return 0;
  312. }
  313. static const struct pwm_ops pwm_imx27_ops = {
  314. .apply = pwm_imx27_apply,
  315. .get_state = pwm_imx27_get_state,
  316. };
  317. static const struct of_device_id pwm_imx27_dt_ids[] = {
  318. { .compatible = "fsl,imx27-pwm", },
  319. { /* sentinel */ }
  320. };
  321. MODULE_DEVICE_TABLE(of, pwm_imx27_dt_ids);
  322. static int pwm_imx27_probe(struct platform_device *pdev)
  323. {
  324. struct pwm_chip *chip;
  325. struct pwm_imx27_chip *imx;
  326. int ret;
  327. u32 pwmcr;
  328. int i;
  329. chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*imx));
  330. if (IS_ERR(chip))
  331. return PTR_ERR(chip);
  332. imx = to_pwm_imx27_chip(chip);
  333. imx->clks_cnt = ARRAY_SIZE(pwm_imx27_clks);
  334. for (i = 0; i < imx->clks_cnt; ++i)
  335. imx->clks[i].id = pwm_imx27_clks[i];
  336. ret = devm_clk_bulk_get(&pdev->dev, imx->clks_cnt, imx->clks);
  337. if (ret)
  338. return dev_err_probe(&pdev->dev, ret,
  339. "getting clocks failed\n");
  340. chip->ops = &pwm_imx27_ops;
  341. imx->mmio_base = devm_platform_ioremap_resource(pdev, 0);
  342. if (IS_ERR(imx->mmio_base))
  343. return PTR_ERR(imx->mmio_base);
  344. ret = clk_bulk_prepare_enable(imx->clks_cnt, imx->clks);
  345. if (ret)
  346. return ret;
  347. /* keep clks on if pwm is running */
  348. pwmcr = readl(imx->mmio_base + MX3_PWMCR);
  349. if (!(pwmcr & MX3_PWMCR_EN))
  350. clk_bulk_disable_unprepare(imx->clks_cnt, imx->clks);
  351. return devm_pwmchip_add(&pdev->dev, chip);
  352. }
  353. static struct platform_driver imx_pwm_driver = {
  354. .driver = {
  355. .name = "pwm-imx27",
  356. .of_match_table = pwm_imx27_dt_ids,
  357. },
  358. .probe = pwm_imx27_probe,
  359. };
  360. module_platform_driver(imx_pwm_driver);
  361. MODULE_DESCRIPTION("i.MX27 and later i.MX SoCs Pulse Width Modulator driver");
  362. MODULE_LICENSE("GPL v2");
  363. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");