[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250421095521.1500427-5-nylon.chen@sifive.com>
Date: Mon, 21 Apr 2025 17:55:20 +0800
From: Nylon Chen <nylon.chen@...ive.com>
To: Conor Dooley <conor@...nel.org>,
Rob Herring <robh@...nel.org>,
Krzysztof Kozlowski <krzk+dt@...nel.org>,
Paul Walmsley <paul.walmsley@...ive.com>,
Palmer Dabbelt <palmer@...belt.com>,
Albert Ou <aou@...s.berkeley.edu>,
Samuel Holland <samuel.holland@...ive.com>,
Uwe Kleine-König <ukleinek@...nel.org>
Cc: linux-riscv@...ts.infradead.org,
devicetree@...r.kernel.org,
linux-kernel@...r.kernel.org,
linux-pwm@...r.kernel.org,
Nylon Chen <nylon.chen@...ive.com>,
Zong Li <zong.li@...ive.com>
Subject: [PATCH v11 4/5] pwm: sifive: Fix rounding issues in apply and get_state functions
Fix PWM apply and get_state rounding to ensure consistency between
setting and reading values
This fixes the reported errors:
pwm-sifive 10021000.pwm: .apply is supposed to round down
duty_cycle (requested: 360/504000, applied: 361/504124)
pwm-sifive 10021000.pwm: .apply is supposed to round down
period (requested: 504000, applied: 504124)
Co-developed-by: Zong Li <zong.li@...ive.com>
Signed-off-by: Zong Li <zong.li@...ive.com>
Signed-off-by: Nylon Chen <nylon.chen@...ive.com>
---
drivers/pwm/pwm-sifive.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c
index 6259f8500f71..1404c383461d 100644
--- a/drivers/pwm/pwm-sifive.c
+++ b/drivers/pwm/pwm-sifive.c
@@ -122,8 +122,8 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
state->enabled = false;
state->period = ddata->real_period;
- state->duty_cycle =
- (u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH;
+ state->duty_cycle = DIV_ROUND_UP_ULL((u64)duty * ddata->real_period,
+ (1U << PWM_SIFIVE_CMPWIDTH));
state->polarity = PWM_POLARITY_NORMAL;
return 0;
@@ -157,7 +157,7 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
* consecutively
*/
num = (u64)duty_cycle * (1U << PWM_SIFIVE_CMPWIDTH);
- frac = DIV64_U64_ROUND_CLOSEST(num, state->period);
+ frac = num / state->period;
/* The hardware cannot generate a 0% duty cycle */
frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1);
inactive = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - frac;
--
2.34.1
Powered by blists - more mailing lists