From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>,
thierry.reding@gmail.com, linux-pwm@vger.kernel.org,
linux-renesas-soc@vger.kernel.org
Subject: Re: pwm: rcar: improve calculation of divider
Date: Sun, 09 Dec 2018 22:55:00 +0200 [thread overview]
Message-ID: <1828832.OCLkmioh6N@avalon> (raw)
In-Reply-To: <20181207214932.xckuuarcka733v7y@pengutronix.de>
Hi Uwe,
On Friday, 7 December 2018 23:49:32 EET Uwe Kleine-König wrote:
> Hello,
>
> while looking at the driver I noticed another patch opportunity: In
> rcar_pwm_get_clock_division() there is a loop:
>
> for (div = 0; div <= RCAR_PWM_MAX_DIVISION; div++) {
> max = (unsigned long long)NSEC_PER_SEC * RCAR_PWM_MAX_CYCLE *
> (1 << div);
> do_div(max, clk_rate);
> if (period_ns <= max)
> break;
> }
>
> The value of div should be calculatable without a loop. Something like:
>
> divider = NSEC_PER_SEC * RCAR_PWM_MAX_CYCLE;
> tmp = (unsigned long long)period_ns * clk_rate + (divider - 1);
> do_div(tmp, divider);
> div = ilog2(tmp - 1) + 1;
>
> You might want to check if my maths are right, I didn't test.
I've noticed the same, and wrote the following patch last week, also untested.
I was planning to give it a try before sending it out, but as you've noticed
the same issue, here's the code if anyone wants to give it a try before I can.
Our calculations are similar, the main difference is the last line, and I
think yours read better.
From 22f7149916f590d3dbcc673dacc738441c741900 Mon Sep 17 00:00:00 2001
From: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
Date: Sun, 25 Nov 2018 16:02:39 +0200
Subject: [PATCH] pwm: rcar: Optimize rcar_pwm_get_clock_division()
Get rid of the loop over all possible divisor values by computing the
divisor directly.
Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
---
drivers/pwm/pwm-rcar.c | 26 ++++++++++++++++----------
1 file changed, 16 insertions(+), 10 deletions(-)
diff --git a/drivers/pwm/pwm-rcar.c b/drivers/pwm/pwm-rcar.c
index a41812fc6f95..e6d73b94d5cf 100644
--- a/drivers/pwm/pwm-rcar.c
+++ b/drivers/pwm/pwm-rcar.c
@@ -68,21 +68,27 @@ static void rcar_pwm_update(struct rcar_pwm_chip *rp, u32 mask, u32 data,
static int rcar_pwm_get_clock_division(struct rcar_pwm_chip *rp, int period_ns)
{
unsigned long clk_rate = clk_get_rate(rp->clk);
- unsigned long long max; /* max cycle / nanoseconds */
- unsigned int div;
+ u64 max_period_ns;
+ u32 div;
if (clk_rate == 0)
return -EINVAL;
- for (div = 0; div <= RCAR_PWM_MAX_DIVISION; div++) {
- max = (unsigned long long)NSEC_PER_SEC * RCAR_PWM_MAX_CYCLE *
- (1 << div);
- do_div(max, clk_rate);
- if (period_ns <= max)
- break;
- }
+ /*
+ * The maximum achievable period is 2^24 * 1023 cycles of the internal
+ * bus clock.
+ */
+ max_period_ns = (1ULL << RCAR_PWM_MAX_DIVISION) * RCAR_PWM_MAX_CYCLE
+ * NSEC_PER_SEC;
+ do_div(max_period_ns, clk_rate);
+
+ if (period_ns > max_period_ns)
+ return -ERANGE;
- return (div <= RCAR_PWM_MAX_DIVISION) ? div : -ERANGE;
+ /* Calculate the divisor and round it up to the next power of two. */
+ div = DIV64_U64_ROUND_UP((u64)period_ns * clk_rate,
+ (u64)RCAR_PWM_MAX_CYCLE * NSEC_PER_SEC);
+ return fls(2 * div - 1) - 1;
}
static void rcar_pwm_set_clock_control(struct rcar_pwm_chip *rp,
--
Regards,
Laurent Pinchart
next prev parent reply other threads:[~2018-12-09 20:54 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-07 8:29 [PATCH 0/5] pwm: rcar: Add support "atomic" API and workaround Yoshihiro Shimoda
2018-12-07 8:29 ` [PATCH 1/5] pwm: rcar: add rcar_pwm_calc_counter() to calculate PWMCNT value only Yoshihiro Shimoda
2018-12-07 9:00 ` Uwe Kleine-König
2018-12-07 8:29 ` [PATCH 2/5] pwm: rcar: Add support "atomic" API Yoshihiro Shimoda
2018-12-07 9:07 ` Uwe Kleine-König
2018-12-07 9:57 ` Geert Uytterhoeven
2018-12-07 10:45 ` Uwe Kleine-König
2018-12-10 4:58 ` Yoshihiro Shimoda
2018-12-07 8:29 ` [PATCH 3/5] pwm: rcar: Use "atomic" API on rcar_pwm_resume() Yoshihiro Shimoda
2018-12-07 8:29 ` [PATCH 4/5] pwm: rcar: remove legacy APIs Yoshihiro Shimoda
2018-12-07 8:29 ` [PATCH 5/5] pwm: rcar: add workaround to output "pseudo" low level Yoshihiro Shimoda
2018-12-07 9:13 ` Uwe Kleine-König
2018-12-10 4:49 ` Yoshihiro Shimoda
2018-12-10 8:11 ` Uwe Kleine-König
2018-12-12 3:19 ` Yoshihiro Shimoda
2018-12-12 7:37 ` Uwe Kleine-König
2018-12-12 10:49 ` Yoshihiro Shimoda
2018-12-13 9:47 ` Yoshihiro Shimoda
2018-12-13 9:52 ` Uwe Kleine-König
2018-12-13 10:53 ` Yoshihiro Shimoda
2018-12-07 21:49 ` pwm: rcar: improve calculation of divider Uwe Kleine-König
2018-12-09 20:55 ` Laurent Pinchart [this message]
2018-12-10 5:09 ` Yoshihiro Shimoda
2018-12-10 8:04 ` Uwe Kleine-König
2018-12-12 3:13 ` Yoshihiro Shimoda
2018-12-09 22:48 ` [PATCH 0/5] pwm: rcar: Add support "atomic" API and workaround Laurent Pinchart
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1828832.OCLkmioh6N@avalon \
--to=laurent.pinchart@ideasonboard.com \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-renesas-soc@vger.kernel.org \
--cc=thierry.reding@gmail.com \
--cc=u.kleine-koenig@pengutronix.de \
--cc=yoshihiro.shimoda.uh@renesas.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox