From: Christian Marangi <ansuelsmth@gmail.com>
To: "Uwe Kleine-König" <ukleinek@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org,
Benjamin Larsson <benjamin.larsson@genexis.eu>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>,
Lorenzo Bianconi <lorenzo@kernel.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Subject: Re: [PATCH v23] pwm: airoha: Add support for EN7581 SoC
Date: Tue, 9 Sep 2025 13:48:09 +0200 [thread overview]
Message-ID: <68c013fb.050a0220.702b3.6a13@mx.google.com> (raw)
In-Reply-To: <o32quqohph6xq73f65izjocjdhv2ri4dld4tcmmmtisa632ucq@lpz4ewja3xtd>
On Tue, Sep 09, 2025 at 12:26:48PM +0200, Uwe Kleine-König wrote:
> Hello Christian,
>
> On Mon, Sep 08, 2025 at 08:48:38PM +0200, Christian Marangi wrote:
> > On Fri, Aug 01, 2025 at 11:15:41AM +0200, Uwe Kleine-König wrote:
> > > On Tue, Jul 08, 2025 at 04:50:52PM +0200, Christian Marangi wrote:
> > > > + duty_ticks = airoha_pwm_get_duty_ticks_from_ns(period_ns, duty_ns);
> > >
> > > As duty_ticks depends on the selected period_ticks, I think the bucket
> > > selection algorithm is still wrong.
> > >
> > > Consider a request to implement
> > >
> > > period_ns = 256 ms
> > > duty_ns = 128 ms
> > >
> > > which at first correctly results in
> > >
> > > period_ticks = 64
> > > duty_ticks = 127
> > >
> > > If however all buckets are used and we only find one with say 62 period
> > > ticks we get period_ns = 248 and with that duty_ticks should better be
> > > 131 and not 127.
> >
> > sorry for checking this only now and maybe we need to catch this again.
>
> no need to be sorry here. Taking time for replies is fine for me.
>
> > Maybe we are getting confused here but itsn't this already handled by
> > the upper condition?
> >
> > /* Ignore bucket with invalid configs */
> > if (bucket_period_ticks > period_ticks ||
> > bucket_duty_ticks > duty_ticks)
> > continue;
> >
> > /*
> > * Search for a bucket closer to the requested period/duty
> > * that has the maximal possible period that isn't bigger
> > * than the requested period. For that period pick the maximal
> > * duty cycle that isn't bigger than the requested duty_cycle.
> > */
> > if (bucket_period_ticks > best_period_ticks ||
> > (bucket_period_ticks == best_period_ticks &&
> > bucket_duty_ticks > best_duty_ticks)) {
> > best_period_ticks = bucket_period_ticks;
> > best_duty_ticks = bucket_duty_ticks;
> > best = i;
> > }
> >
> > We first limit for a bucket that doesn't got over both period and duty
> > and then we search for period and best duty. This should account for
> > never exceeding a duty since both period and duty are precalculated for
> > the current bucket and even if duty depends on period, again it's
> > precalculated. Am I missing something?
>
> Let me describe the issue in more detail:
>
> The period length is configured in the AIROHA_PWM_WAVE_GEN_CYCLE
> register in multiples of 4 ms. The duty length is configured in the
> AIROHA_PWM_GPIO_FLASH_PRD_HIGH register in multiples of
> $period_length/255.
>
> So if you calcultate the number of multiples you need for duty_ns = 128
> ms based on the assumption that period_ns = 256 ms the result becomes
> wrong when you are forced to switch to period_ns = 248.
>
> So to implement a request for period = 256 ms (64 ticks) and duty_cycle
> = 128 ms (127.5 duty ticks) having the choice between the two buckets:
>
> a) period_ticks = 62; duty_ticks = 127
> (period = 248 ms, duty_cycle = 123.51372549019608 ms)
> b) period_ticks = 62; duty_ticks = 131
> (period = 248 ms, duty_cycle = 127.40392156862744 ms)
>
> b) is the better one despite 127 duty_ticks would be an exact match for
> period_ticks = 64. So the issue is that the "Ignore bucket with invalid
> configs" kicks out b). That's wrong because
>
> bucket_duty_ticks > duty_ticks
>
> doesn't imply
>
> bucket_duty > duty
>
> .
>
Thanks for the quick feedback hope we can takle this quick so we can
have this finally merged.
I changed the logic to this. What do you think? (I introduced an helper
to calculate the ns from raw ticks)
duty_ns = airoha_pwm_get_duty_ns_from_ticks(period_ticks, duty_ticks);
...
/* Ignore bucket with invalid period */
if (bucket_period_ticks > period_ticks)
continue;
/*
* Search for a bucket closer to the requested period
* that has the maximal possible period that isn't bigger
* than the requested period. For that period pick the maximal
* duty cycle that isn't bigger than the requested duty_cycle.
*/
if (bucket_period_ticks >= best_period_ticks) {
bucket_duty_ns = airoha_pwm_get_duty_ns_from_ticks(bucket_period_ticks,
bucket_duty_ticks);
/* Skip bucket that goes over the requested duty */
if (bucket_duty_ns > duty_ns)
continue;
if (bucket_duty_ns > best_duty_ns) {
best_period_ticks = bucket_period_ticks;
best_duty_ns = bucket_duty_ns;
best = i;
}
}
We first search the period and then we calculate the duty in NS and
calculate the duty for each bucket. Should comply with the fact that
duty depends on period right?
--
Ansuel
next prev parent reply other threads:[~2025-09-09 11:48 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-08 14:50 [PATCH v23] pwm: airoha: Add support for EN7581 SoC Christian Marangi
2025-08-01 9:15 ` Uwe Kleine-König
2025-09-08 18:48 ` Christian Marangi
2025-09-09 10:26 ` Uwe Kleine-König
2025-09-09 11:48 ` Christian Marangi [this message]
2025-09-09 11:20 ` Andy Shevchenko
2025-09-09 11:51 ` Christian Marangi
2025-09-09 13:16 ` Andy Shevchenko
2025-09-26 17:41 ` Uwe Kleine-König
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=68c013fb.050a0220.702b3.6a13@mx.google.com \
--to=ansuelsmth@gmail.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=benjamin.larsson@genexis.eu \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=lorenzo@kernel.org \
--cc=ukleinek@kernel.org \
/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