1 // SPDX-License-Identifier: GPL-2.0
3 * simple driver for PWM (Pulse Width Modulator) controller
8 * - When disabled the output is driven to 0 independent of the configured
12 #include <linux/bitfield.h>
13 #include <linux/bitops.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/pwm.h>
23 #include <linux/slab.h>
25 #define MX3_PWMCR 0x00 /* PWM Control Register */
26 #define MX3_PWMSR 0x04 /* PWM Status Register */
27 #define MX3_PWMSAR 0x0C /* PWM Sample Register */
28 #define MX3_PWMPR 0x10 /* PWM Period Register */
29 #define MX3_PWMCNR 0x14 /* PWM Counter Register */
31 #define MX3_PWMCR_FWM GENMASK(27, 26)
32 #define MX3_PWMCR_STOPEN BIT(25)
33 #define MX3_PWMCR_DOZEN BIT(24)
34 #define MX3_PWMCR_WAITEN BIT(23)
35 #define MX3_PWMCR_DBGEN BIT(22)
36 #define MX3_PWMCR_BCTR BIT(21)
37 #define MX3_PWMCR_HCTR BIT(20)
39 #define MX3_PWMCR_POUTC GENMASK(19, 18)
40 #define MX3_PWMCR_POUTC_NORMAL 0
41 #define MX3_PWMCR_POUTC_INVERTED 1
42 #define MX3_PWMCR_POUTC_OFF 2
44 #define MX3_PWMCR_CLKSRC GENMASK(17, 16)
45 #define MX3_PWMCR_CLKSRC_OFF 0
46 #define MX3_PWMCR_CLKSRC_IPG 1
47 #define MX3_PWMCR_CLKSRC_IPG_HIGH 2
48 #define MX3_PWMCR_CLKSRC_IPG_32K 3
50 #define MX3_PWMCR_PRESCALER GENMASK(15, 4)
52 #define MX3_PWMCR_SWR BIT(3)
54 #define MX3_PWMCR_REPEAT GENMASK(2, 1)
55 #define MX3_PWMCR_REPEAT_1X 0
56 #define MX3_PWMCR_REPEAT_2X 1
57 #define MX3_PWMCR_REPEAT_4X 2
58 #define MX3_PWMCR_REPEAT_8X 3
60 #define MX3_PWMCR_EN BIT(0)
62 #define MX3_PWMSR_FWE BIT(6)
63 #define MX3_PWMSR_CMP BIT(5)
64 #define MX3_PWMSR_ROV BIT(4)
65 #define MX3_PWMSR_FE BIT(3)
67 #define MX3_PWMSR_FIFOAV GENMASK(2, 0)
68 #define MX3_PWMSR_FIFOAV_EMPTY 0
69 #define MX3_PWMSR_FIFOAV_1WORD 1
70 #define MX3_PWMSR_FIFOAV_2WORDS 2
71 #define MX3_PWMSR_FIFOAV_3WORDS 3
72 #define MX3_PWMSR_FIFOAV_4WORDS 4
74 #define MX3_PWMCR_PRESCALER_SET(x) FIELD_PREP(MX3_PWMCR_PRESCALER, (x) - 1)
75 #define MX3_PWMCR_PRESCALER_GET(x) (FIELD_GET(MX3_PWMCR_PRESCALER, \
78 #define MX3_PWM_SWR_LOOP 5
80 /* PWMPR register value of 0xffff has the same effect as 0xfffe */
81 #define MX3_PWMPR_MAX 0xfffe
83 struct pwm_imx27_chip {
86 void __iomem *mmio_base;
89 * The driver cannot read the current duty cycle from the hardware if
90 * the hardware is disabled. Cache the last programmed duty cycle
91 * value to return in that case.
93 unsigned int duty_cycle;
96 static inline struct pwm_imx27_chip *to_pwm_imx27_chip(struct pwm_chip *chip)
98 return pwmchip_get_drvdata(chip);
101 static int pwm_imx27_clk_prepare_enable(struct pwm_imx27_chip *imx)
105 ret = clk_prepare_enable(imx->clk_ipg);
109 ret = clk_prepare_enable(imx->clk_per);
111 clk_disable_unprepare(imx->clk_ipg);
118 static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)
120 clk_disable_unprepare(imx->clk_per);
121 clk_disable_unprepare(imx->clk_ipg);
124 static int pwm_imx27_get_state(struct pwm_chip *chip,
125 struct pwm_device *pwm, struct pwm_state *state)
127 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
128 u32 period, prescaler, pwm_clk, val;
132 ret = pwm_imx27_clk_prepare_enable(imx);
136 val = readl(imx->mmio_base + MX3_PWMCR);
138 if (val & MX3_PWMCR_EN)
139 state->enabled = true;
141 state->enabled = false;
143 switch (FIELD_GET(MX3_PWMCR_POUTC, val)) {
144 case MX3_PWMCR_POUTC_NORMAL:
145 state->polarity = PWM_POLARITY_NORMAL;
147 case MX3_PWMCR_POUTC_INVERTED:
148 state->polarity = PWM_POLARITY_INVERSED;
151 dev_warn(pwmchip_parent(chip), "can't set polarity, output disconnected");
154 prescaler = MX3_PWMCR_PRESCALER_GET(val);
155 pwm_clk = clk_get_rate(imx->clk_per);
156 val = readl(imx->mmio_base + MX3_PWMPR);
157 period = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
159 /* PWMOUT (Hz) = PWMCLK / (PWMPR + 2) */
160 tmp = NSEC_PER_SEC * (u64)(period + 2) * prescaler;
161 state->period = DIV_ROUND_UP_ULL(tmp, pwm_clk);
164 * PWMSAR can be read only if PWM is enabled. If the PWM is disabled,
165 * use the cached value.
168 val = readl(imx->mmio_base + MX3_PWMSAR);
170 val = imx->duty_cycle;
172 tmp = NSEC_PER_SEC * (u64)(val) * prescaler;
173 state->duty_cycle = DIV_ROUND_UP_ULL(tmp, pwm_clk);
175 pwm_imx27_clk_disable_unprepare(imx);
180 static void pwm_imx27_sw_reset(struct pwm_chip *chip)
182 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
183 struct device *dev = pwmchip_parent(chip);
187 writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
189 usleep_range(200, 1000);
190 cr = readl(imx->mmio_base + MX3_PWMCR);
191 } while ((cr & MX3_PWMCR_SWR) &&
192 (wait_count++ < MX3_PWM_SWR_LOOP));
194 if (cr & MX3_PWMCR_SWR)
195 dev_warn(dev, "software reset timeout\n");
198 static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip,
199 struct pwm_device *pwm)
201 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
202 struct device *dev = pwmchip_parent(chip);
203 unsigned int period_ms;
207 sr = readl(imx->mmio_base + MX3_PWMSR);
208 fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr);
209 if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
210 period_ms = DIV_ROUND_UP_ULL(pwm->state.period,
214 sr = readl(imx->mmio_base + MX3_PWMSR);
215 if (fifoav == FIELD_GET(MX3_PWMSR_FIFOAV, sr))
216 dev_warn(dev, "there is no free FIFO slot\n");
220 static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,
221 const struct pwm_state *state)
223 unsigned long period_cycles, duty_cycles, prescale, period_us, tmp;
224 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
225 unsigned long long c;
226 unsigned long long clkrate;
232 clkrate = clk_get_rate(imx->clk_per);
233 c = clkrate * state->period;
235 do_div(c, NSEC_PER_SEC);
238 prescale = period_cycles / 0x10000 + 1;
240 period_cycles /= prescale;
241 c = clkrate * state->duty_cycle;
242 do_div(c, NSEC_PER_SEC);
244 duty_cycles /= prescale;
247 * according to imx pwm RM, the real period value should be PERIOD
248 * value in PWMPR plus 2.
250 if (period_cycles > 2)
256 * Wait for a free FIFO slot if the PWM is already enabled, and flush
257 * the FIFO if the PWM was disabled and is about to be enabled.
259 if (pwm->state.enabled) {
260 pwm_imx27_wait_fifo_slot(chip, pwm);
262 ret = pwm_imx27_clk_prepare_enable(imx);
266 pwm_imx27_sw_reset(chip);
269 val = readl(imx->mmio_base + MX3_PWMPR);
270 val = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
271 cr = readl(imx->mmio_base + MX3_PWMCR);
272 tmp = NSEC_PER_SEC * (u64)(val + 2) * MX3_PWMCR_PRESCALER_GET(cr);
273 tmp = DIV_ROUND_UP_ULL(tmp, clkrate);
274 period_us = DIV_ROUND_UP_ULL(tmp, 1000);
278 * PWM: PWM output may not function correctly if the FIFO is empty when
279 * a new SAR value is programmed
282 * When the PWM FIFO is empty, a new value programmed to the PWM Sample
283 * register (PWM_PWMSAR) will be directly applied even if the current
284 * timer period has not expired.
286 * If the new SAMPLE value programmed in the PWM_PWMSAR register is
287 * less than the previous value, and the PWM counter register
288 * (PWM_PWMCNR) that contains the current COUNT value is greater than
289 * the new programmed SAMPLE value, the current period will not flip
290 * the level. This may result in an output pulse with a duty cycle of
293 * Consider a change from
301 * At the time marked by *, the new write value will be directly applied
302 * to SAR even the current period is not over if FIFO is empty.
304 * ________ ____________________
305 * / \______/ \__________/
307 * |<-- old SAR -->| |<-- new SAR -->|
309 * That is the output is active for a whole period.
312 * Check new SAR less than old SAR and current counter is in errata
313 * windows, write extra old SAR into FIFO and new SAR will effect at
316 * Sometime period is quite long, such as over 1 second. If add old SAR
317 * into FIFO unconditional, new SAR have to wait for next period. It
320 * Turn off the interrupt to ensure that not IRQ and schedule happen
321 * during above operations. If any irq and schedule happen, counter
322 * in PWM will be out of data and take wrong action.
324 * Add a safety margin 1.5us because it needs some time to complete
327 * Use writel_relaxed() to minimize the interval between two writes to
328 * the SAR register to increase the fastest PWM frequency supported.
330 * When the PWM period is longer than 2us(or <500kHz), this workaround
331 * can solve this problem. No software workaround is available if PWM
332 * period is shorter than IO write. Just try best to fill old data
336 do_div(c, NSEC_PER_SEC);
338 local_irq_save(flags);
339 val = FIELD_GET(MX3_PWMSR_FIFOAV, readl_relaxed(imx->mmio_base + MX3_PWMSR));
341 if (duty_cycles < imx->duty_cycle && (cr & MX3_PWMCR_EN)) {
342 if (period_us < 2) { /* 2us = 500 kHz */
343 /* Best effort attempt to fix up >500 kHz case */
344 udelay(3 * period_us);
345 writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
346 writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
347 } else if (val < MX3_PWMSR_FIFOAV_2WORDS) {
348 val = readl_relaxed(imx->mmio_base + MX3_PWMCNR);
350 * If counter is close to period, controller may roll over when
353 if ((val + c >= duty_cycles && val < imx->duty_cycle) ||
354 val + c >= period_cycles)
355 writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
358 writel_relaxed(duty_cycles, imx->mmio_base + MX3_PWMSAR);
359 local_irq_restore(flags);
361 writel(period_cycles, imx->mmio_base + MX3_PWMPR);
364 * Store the duty cycle for future reference in cases where the
365 * MX3_PWMSAR register can't be read (i.e. when the PWM is disabled).
367 imx->duty_cycle = duty_cycles;
369 cr = MX3_PWMCR_PRESCALER_SET(prescale) |
370 MX3_PWMCR_STOPEN | MX3_PWMCR_DOZEN | MX3_PWMCR_WAITEN |
371 FIELD_PREP(MX3_PWMCR_CLKSRC, MX3_PWMCR_CLKSRC_IPG_HIGH) |
374 if (state->polarity == PWM_POLARITY_INVERSED)
375 cr |= FIELD_PREP(MX3_PWMCR_POUTC,
376 MX3_PWMCR_POUTC_INVERTED);
381 writel(cr, imx->mmio_base + MX3_PWMCR);
384 pwm_imx27_clk_disable_unprepare(imx);
389 static const struct pwm_ops pwm_imx27_ops = {
390 .apply = pwm_imx27_apply,
391 .get_state = pwm_imx27_get_state,
394 static const struct of_device_id pwm_imx27_dt_ids[] = {
395 { .compatible = "fsl,imx27-pwm", },
398 MODULE_DEVICE_TABLE(of, pwm_imx27_dt_ids);
400 static int pwm_imx27_probe(struct platform_device *pdev)
402 struct pwm_chip *chip;
403 struct pwm_imx27_chip *imx;
407 chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*imx));
409 return PTR_ERR(chip);
410 imx = to_pwm_imx27_chip(chip);
412 imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
413 if (IS_ERR(imx->clk_ipg))
414 return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_ipg),
415 "getting ipg clock failed\n");
417 imx->clk_per = devm_clk_get(&pdev->dev, "per");
418 if (IS_ERR(imx->clk_per))
419 return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_per),
420 "failed to get peripheral clock\n");
422 chip->ops = &pwm_imx27_ops;
424 imx->mmio_base = devm_platform_ioremap_resource(pdev, 0);
425 if (IS_ERR(imx->mmio_base))
426 return PTR_ERR(imx->mmio_base);
428 ret = pwm_imx27_clk_prepare_enable(imx);
432 /* keep clks on if pwm is running */
433 pwmcr = readl(imx->mmio_base + MX3_PWMCR);
434 if (!(pwmcr & MX3_PWMCR_EN))
435 pwm_imx27_clk_disable_unprepare(imx);
437 return devm_pwmchip_add(&pdev->dev, chip);
440 static struct platform_driver imx_pwm_driver = {
443 .of_match_table = pwm_imx27_dt_ids,
445 .probe = pwm_imx27_probe,
447 module_platform_driver(imx_pwm_driver);
449 MODULE_DESCRIPTION("i.MX27 and later i.MX SoCs Pulse Width Modulator driver");
450 MODULE_LICENSE("GPL v2");