summaryrefslogtreecommitdiff
path: root/drivers/pwm/pwm-bcm2835.c
diff options
context:
space:
mode:
authorSean Young <sean@mess.org>2019-06-03 12:00:58 +0300
committerThierry Reding <thierry.reding@gmail.com>2019-06-26 12:39:12 +0300
commit11fc4edc483bea8bf0efa0cc726886d2342f6fa6 (patch)
tree19f3bdfc1f4cc7971343688f35770b629c28b5c9 /drivers/pwm/pwm-bcm2835.c
parent3f467ebe9e898c6ceec5334d92bccac2bd9bc7d2 (diff)
downloadlinux-11fc4edc483bea8bf0efa0cc726886d2342f6fa6.tar.xz
pwm: bcm2835: Improve precision of PWM
If sending IR with carrier of 455kHz using the pwm-ir-tx driver, the carrier ends up being 476kHz. The clock is set to bcm2835-pwm with a rate of 10MHz. A carrier of 455kHz has a period of 2198ns, but the arithmetic truncates this to 2100ns rather than 2200ns. So, use DIV_ROUND_CLOSEST() to reduce rounding errors, and we have a much more accurate carrier of 454.5kHz. Reported-by: Andreas Christ <andreas@christ-faesch.ch> Signed-off-by: Sean Young <sean@mess.org> Acked-by: Stefan Wahren <wahrenst@gmx.net> Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
Diffstat (limited to 'drivers/pwm/pwm-bcm2835.c')
-rw-r--r--drivers/pwm/pwm-bcm2835.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c
index 5652f461d994..f6fe0b922e1e 100644
--- a/drivers/pwm/pwm-bcm2835.c
+++ b/drivers/pwm/pwm-bcm2835.c
@@ -70,7 +70,7 @@ static int bcm2835_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
return -EINVAL;
}
- scaler = NSEC_PER_SEC / rate;
+ scaler = DIV_ROUND_CLOSEST(NSEC_PER_SEC, rate);
if (period_ns <= MIN_PERIOD) {
dev_err(pc->dev, "period %d not supported, minimum %d\n",
@@ -78,8 +78,10 @@ static int bcm2835_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
return -EINVAL;
}
- writel(duty_ns / scaler, pc->base + DUTY(pwm->hwpwm));
- writel(period_ns / scaler, pc->base + PERIOD(pwm->hwpwm));
+ writel(DIV_ROUND_CLOSEST(duty_ns, scaler),
+ pc->base + DUTY(pwm->hwpwm));
+ writel(DIV_ROUND_CLOSEST(period_ns, scaler),
+ pc->base + PERIOD(pwm->hwpwm));
return 0;
}