Skip to content

Commit d395280

Browse files
wangxiaoningnxpmehmetb0
authored andcommitted
pwm: imx27: Workaround of the pwm output bug when decrease the duty cycle
BugLink: https://bugs.launchpad.net/bugs/2095283 [ Upstream commit a25351e ] Implement workaround for ERR051198 (https://www.nxp.com/docs/en/errata/IMX8MN_0N14Y.pdf) PWM output may not function correctly if the FIFO is empty when a new SAR value is programmed. Description: When the PWM FIFO is empty, a new value programmed to the PWM Sample register (PWM_PWMSAR) will be directly applied even if the current timer period has not expired. If the new SAMPLE value programmed in the PWM_PWMSAR register is less than the previous value, and the PWM counter register (PWM_PWMCNR) that contains the current COUNT value is greater than the new programmed SAMPLE value, the current period will not flip the level. This may result in an output pulse with a duty cycle of 100%. Workaround: Program the current SAMPLE value in the PWM_PWMSAR register before updating the new duty cycle to the SAMPLE value in the PWM_PWMSAR register. This will ensure that the new SAMPLE value is modified during a non-empty FIFO, and can be successfully updated after the period expires. Write the old SAR value before updating the new duty cycle to SAR. This avoids writing the new value into an empty FIFO. This only resolves the issue when the PWM period is longer than 2us (or <500kHz) because write register is not quick enough when PWM period is very short. Reproduce steps: cd /sys/class/pwm/pwmchip1/pwm0 echo 2000000000 > period # It is easy to observe by using long period echo 1000000000 > duty_cycle echo 1 > enable echo 8000 > duty_cycle # One full high pulse will be seen by scope Fixes: 166091b ("[ARM] MXC: add pwm driver for i.MX SoCs") Reviewed-by: Jun Li <jun.li@nxp.com> Signed-off-by: Clark Wang <xiaoning.wang@nxp.com> Signed-off-by: Frank Li <Frank.Li@nxp.com> Link: https://lore.kernel.org/r/20241008194123.1943141-1-Frank.Li@nxp.com Signed-off-by: Uwe Kleine-König <ukleinek@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Koichiro Den <koichiro.den@canonical.com>
1 parent b61d183 commit d395280

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

drivers/pwm/pwm-imx27.c

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define MX3_PWMSR 0x04 /* PWM Status Register */
2727
#define MX3_PWMSAR 0x0C /* PWM Sample Register */
2828
#define MX3_PWMPR 0x10 /* PWM Period Register */
29+
#define MX3_PWMCNR 0x14 /* PWM Counter Register */
2930

3031
#define MX3_PWMCR_FWM GENMASK(27, 26)
3132
#define MX3_PWMCR_STOPEN BIT(25)
@@ -215,11 +216,13 @@ static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip,
215216
static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,
216217
const struct pwm_state *state)
217218
{
218-
unsigned long period_cycles, duty_cycles, prescale;
219+
unsigned long period_cycles, duty_cycles, prescale, period_us, tmp;
219220
struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
220221
struct pwm_state cstate;
221222
unsigned long long c;
222223
unsigned long long clkrate;
224+
unsigned long flags;
225+
int val;
223226
int ret;
224227
u32 cr;
225228

@@ -262,7 +265,98 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,
262265
pwm_imx27_sw_reset(chip);
263266
}
264267

265-
writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
268+
val = readl(imx->mmio_base + MX3_PWMPR);
269+
val = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
270+
cr = readl(imx->mmio_base + MX3_PWMCR);
271+
tmp = NSEC_PER_SEC * (u64)(val + 2) * MX3_PWMCR_PRESCALER_GET(cr);
272+
tmp = DIV_ROUND_UP_ULL(tmp, clkrate);
273+
period_us = DIV_ROUND_UP_ULL(tmp, 1000);
274+
275+
/*
276+
* ERR051198:
277+
* PWM: PWM output may not function correctly if the FIFO is empty when
278+
* a new SAR value is programmed
279+
*
280+
* Description:
281+
* When the PWM FIFO is empty, a new value programmed to the PWM Sample
282+
* register (PWM_PWMSAR) will be directly applied even if the current
283+
* timer period has not expired.
284+
*
285+
* If the new SAMPLE value programmed in the PWM_PWMSAR register is
286+
* less than the previous value, and the PWM counter register
287+
* (PWM_PWMCNR) that contains the current COUNT value is greater than
288+
* the new programmed SAMPLE value, the current period will not flip
289+
* the level. This may result in an output pulse with a duty cycle of
290+
* 100%.
291+
*
292+
* Consider a change from
293+
* ________
294+
* / \______/
295+
* ^ * ^
296+
* to
297+
* ____
298+
* / \__________/
299+
* ^ ^
300+
* At the time marked by *, the new write value will be directly applied
301+
* to SAR even the current period is not over if FIFO is empty.
302+
*
303+
* ________ ____________________
304+
* / \______/ \__________/
305+
* ^ ^ * ^ ^
306+
* |<-- old SAR -->| |<-- new SAR -->|
307+
*
308+
* That is the output is active for a whole period.
309+
*
310+
* Workaround:
311+
* Check new SAR less than old SAR and current counter is in errata
312+
* windows, write extra old SAR into FIFO and new SAR will effect at
313+
* next period.
314+
*
315+
* Sometime period is quite long, such as over 1 second. If add old SAR
316+
* into FIFO unconditional, new SAR have to wait for next period. It
317+
* may be too long.
318+
*
319+
* Turn off the interrupt to ensure that not IRQ and schedule happen
320+
* during above operations. If any irq and schedule happen, counter
321+
* in PWM will be out of data and take wrong action.
322+
*
323+
* Add a safety margin 1.5us because it needs some time to complete
324+
* IO write.
325+
*
326+
* Use writel_relaxed() to minimize the interval between two writes to
327+
* the SAR register to increase the fastest PWM frequency supported.
328+
*
329+
* When the PWM period is longer than 2us(or <500kHz), this workaround
330+
* can solve this problem. No software workaround is available if PWM
331+
* period is shorter than IO write. Just try best to fill old data
332+
* into FIFO.
333+
*/
334+
c = clkrate * 1500;
335+
do_div(c, NSEC_PER_SEC);
336+
337+
local_irq_save(flags);
338+
val = FIELD_GET(MX3_PWMSR_FIFOAV, readl_relaxed(imx->mmio_base + MX3_PWMSR));
339+
340+
if (duty_cycles < imx->duty_cycle && (cr & MX3_PWMCR_EN)) {
341+
if (period_us < 2) { /* 2us = 500 kHz */
342+
/* Best effort attempt to fix up >500 kHz case */
343+
udelay(3 * period_us);
344+
writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
345+
writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
346+
} else if (val < MX3_PWMSR_FIFOAV_2WORDS) {
347+
val = readl_relaxed(imx->mmio_base + MX3_PWMCNR);
348+
/*
349+
* If counter is close to period, controller may roll over when
350+
* next IO write.
351+
*/
352+
if ((val + c >= duty_cycles && val < imx->duty_cycle) ||
353+
val + c >= period_cycles)
354+
writel_relaxed(imx->duty_cycle, imx->mmio_base + MX3_PWMSAR);
355+
}
356+
}
357+
writel_relaxed(duty_cycles, imx->mmio_base + MX3_PWMSAR);
358+
local_irq_restore(flags);
359+
266360
writel(period_cycles, imx->mmio_base + MX3_PWMPR);
267361

268362
/*

0 commit comments

Comments
 (0)