lkml.org 
[lkml]   [2024]   [Jan]   [25]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH V2 3/6] pwm: sprd: Optimize the calculation method of duty
Date
Use DIV_ROUND_CLOSEST_ULL to avoid overflow and improve accuracy
when calculating duty.

Signed-off-by: Wenhua Lin <Wenhua.Lin@unisoc.com>
---
drivers/pwm/pwm-sprd.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/pwm/pwm-sprd.c b/drivers/pwm/pwm-sprd.c
index cc54aa77c7e6..8de3f9e154ce 100644
--- a/drivers/pwm/pwm-sprd.c
+++ b/drivers/pwm/pwm-sprd.c
@@ -156,7 +156,8 @@ static int sprd_pwm_config(struct sprd_pwm_chip *spc, struct pwm_device *pwm,
* given settings (MOD and input clock).
*/
mod = spc->mod[pwm->hwpwm];
- duty = duty_ns * mod / period_ns;
+ tmp = (u64)duty_ns * mod;
+ duty = DIV_ROUND_CLOSEST_ULL(tmp, period_ns);

tmp = (u64)chn->clk_rate * period_ns;
do_div(tmp, NSEC_PER_SEC);
--
2.17.1

\
 
 \ /
  Last update: 2024-05-27 14:33    [from the cache]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and my Meterkast|Read the blog