Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3022156ybi; Sun, 26 May 2019 12:42:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqy+S7j6HIy8tzL77BuNbGA6Qf24eyh1ApAi7WnCEPMxoacjcgQkXv+WoYB/L5A/Ajb7ni+j X-Received: by 2002:aa7:90d3:: with SMTP id k19mr128657889pfk.1.1558899773487; Sun, 26 May 2019 12:42:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558899773; cv=none; d=google.com; s=arc-20160816; b=HyauFOuwPxq9Q//STh0GMKwu9A4eaF5iKc/NP8Qh8Ty88ZRJIcqSLcbsX343bbTh3T WmkBo36OTZ7amALVjCJfFh6/NyBUAVynbO80fZXZeeyoto/fT5yOCCJYJ9aIDmzr6KWS dvtDNwdgKYpvlRW9OWwDQr9dSPfvb6EmguBewOGcMAo4C4D0aE34r7w2j8vBx+6hbN35 Cq4FoBbT98S3UlE8gxqO7RHEYogxPi/UFFnkMo8alNB1Obdz9OjHZ4Ai5qTXJ+TR0A6g gBXDBxxoW3TwLTVhTl+Zso/2yWsukAjx7QuatxLE8rRgTvs5nt7sf87foFXZWM+S9GQs vrjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=IO7pT9Jh+7IvXycuSEJq6smDGJfk4h4uTPJGgedCrOs=; b=kyoqqZxiVmLqqe/hOM+m3fndGao2qQ4TH/JhVIitfUF9v8FJAfhwdNuiIQCLuzJjko GTe2g2/sGZ35tYvbtjPP3YvbKXTBm1yaQW7doo87zhNkB+DZ0MFYMz8y9Z/4aISN+Lut 3dtmtKFU3Tp0pROuQX6k6Do4Yr9cd9tE70ACUhvxAf59Z+BQLG0u/CMUHLEnIZSS+vJq pjh1iZ+/quPhl3q6ZP8WiacWiygack0qB4nYHvk2AumwxiPZEjJQoLGlJR2C8G+zY5AS tEt8YNWzEJHqJCI+26OKXNVIawCJ3yN6Km/2pJzf8Yt/UHt90knhT2Wec7pxb4Ak17Kt BQjw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id cn12si16091504plb.349.2019.05.26.12.42.37; Sun, 26 May 2019 12:42:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727264AbfEZTle (ORCPT + 99 others); Sun, 26 May 2019 15:41:34 -0400 Received: from metis.ext.pengutronix.de ([85.220.165.71]:40241 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727097AbfEZTld (ORCPT ); Sun, 26 May 2019 15:41:33 -0400 Received: from pty.hi.pengutronix.de ([2001:67c:670:100:1d::c5]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hUz1F-0004BC-5J; Sun, 26 May 2019 21:41:21 +0200 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1hUz1E-00018L-9V; Sun, 26 May 2019 21:41:20 +0200 Date: Sun, 26 May 2019 21:41:20 +0200 From: Uwe =?iso-8859-1?Q?Kleine-K=F6nig?= To: Martin Blumenstingl , Thierry Reding Cc: linux-amlogic@lists.infradead.org, linux-pwm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel@pengutronix.de Subject: Re: [PATCH 10/14] pwm: meson: simplify the calculation of the pre-divider and count Message-ID: <20190526194120.uzuq6ncz5l2z4hfm@pengutronix.de> References: <20190525181133.4875-1-martin.blumenstingl@googlemail.com> <20190525181133.4875-11-martin.blumenstingl@googlemail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20190525181133.4875-11-martin.blumenstingl@googlemail.com> User-Agent: NeoMutt/20170113 (1.7.2) X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c5 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, May 25, 2019 at 08:11:29PM +0200, Martin Blumenstingl wrote: > Replace the loop to calculate the pre-divider and count with two > separate div64_u64() calculations. This makes the code easier to read > and improves the precision. > > Two example cases: > 1) 32.768kHz LPO clock for the SDIO wifi chip on Khadas VIM > clock input: 500MHz (FCLK_DIV4) > period: 30518ns > duty cycle: 15259ns > old algorithm: pre_div=0, cnt=15259 > new algorithm: pre_div=0, cnt=15259 > (no difference in calculated values) > > 2) PWM LED on Khadas VIM > clock input: 24MHz (XTAL) > period: 7812500ns > duty cycle: 7812500ns > old algorithm: pre_div=2, cnt=62004 > new algorithm: pre_div=2, cnt=62500 > Using a scope (24MHz sampling rate) shows the actual difference: > - old: 7753000ns, off by -59500ns (0.7616%) > - new: 7815000ns, off by +2500ns (0.032%) > > Suggested-by: Uwe Kleine-K?nig > Signed-off-by: Martin Blumenstingl > --- > drivers/pwm/pwm-meson.c | 25 ++++++++++--------------- > 1 file changed, 10 insertions(+), 15 deletions(-) > > diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c > index 27915d6475e3..9afa1e5aaebf 100644 > --- a/drivers/pwm/pwm-meson.c > +++ b/drivers/pwm/pwm-meson.c > @@ -12,6 +12,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -145,7 +146,6 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm, > struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); > unsigned int duty, period, pre_div, cnt, duty_cnt; > unsigned long fin_freq = -1; > - u64 fin_ps; > > duty = state->duty_cycle; > period = state->period; > @@ -164,24 +164,19 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm, > } > > dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq); > - fin_ps = (u64)NSEC_PER_SEC * 1000; > - do_div(fin_ps, fin_freq); > - > - /* Calc pre_div with the period */ > - for (pre_div = 0; pre_div <= MISC_CLK_DIV_MASK; pre_div++) { > - cnt = DIV_ROUND_CLOSEST_ULL((u64)period * 1000, > - fin_ps * (pre_div + 1)); > - dev_dbg(meson->chip.dev, "fin_ps=%llu pre_div=%u cnt=%u\n", > - fin_ps, pre_div, cnt); > - if (cnt <= 0xffff) > - break; > - } > > + pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL); > if (pre_div > MISC_CLK_DIV_MASK) { > dev_err(meson->chip.dev, "unable to get period pre_div\n"); > return -EINVAL; > } > > + cnt = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * (pre_div + 1)); > + if (cnt > 0xffff) { > + dev_err(meson->chip.dev, "unable to get period cnt\n"); > + return -EINVAL; > + } > + There is a slight modification in the calculation of pre_div that isn't catched by the examples above. Before this patch we had: pick smallest pre_div such that round_closest(period * 1000 / (round_down(1e12 / fin_freq) * (pre_div + 1)) <= 0xffff New approach is: pre_div = round_down(fin_freq * period / (1e9 * 0xffff)) An advantage of the new approach is better as it rounds only once and is easier. Consider fin_freq = 99990001 and period = 655355, then the old algorithm picks pre_div = 1 while the new picks pre_div = 0. I didn't continue here to check which are the resulting waveforms, I assume they are different though. As there is currently no definition what is a "better" approximation for a given requested pair (duty_cycle, period) I cannot say if these changes are good or not. And that's a pity, so I still think there should be a documented definition that lays down how a lowlevel driver should round. Without that a consumer that cares about fine differences can not rely an the abstraction provided by the PWM framework because each low-level driver might behave differently. @Thierry: So can you please continue the discussion about this topic. The longer this is delayed the more patches are created and submitted that eventually might be wrong which is a waste of developer and reviewer time. Assuming the people who care about meson don't object after reading this I wouldn't want to stop this patch going in though. So: Acked-by: Uwe Kleine-K?nig Best regards Uwe -- Pengutronix e.K. | Uwe Kleine-K?nig | Industrial Linux Solutions | http://www.pengutronix.de/ |