All of lore.kernel.org
 help / color / mirror / Atom feed
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: Mon, 8 Sep 2025 20:48:38 +0200	[thread overview]
Message-ID: <68bf2509.050a0220.702b3.c003@mx.google.com> (raw)
In-Reply-To: <xsblhw36y3corxx3pxe6223auirrsqr3efovfnrm5lbo4xy3lf@wf3ytlivzv6g>

On Fri, Aug 01, 2025 at 11:15:41AM +0200, Uwe Kleine-König wrote:
> Hello Christian,
> 
> On Tue, Jul 08, 2025 at 04:50:52PM +0200, Christian Marangi wrote:
> > +static u32 airoha_pwm_get_period_ticks_from_ns(u32 period_ns)
> > +{
> > +	return period_ns / AIROHA_PWM_PERIOD_TICK_NS;
> > +}
> > +
> > +static u32 airoha_pwm_get_duty_ticks_from_ns(u32 period_ns, u32 duty_ns)
> > +{
> > +	return mul_u64_u32_div(duty_ns, AIROHA_PWM_DUTY_FULL, period_ns);
> > +}
> > +
> > [...]
> > +static int airoha_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> > +			    const struct pwm_state *state)
> > +{
> > [...]
> > +	/*
> > +	 * Period goes at 4ns step, normalize it to check if we can
> 
> 4 ms or 4 ns?
>

4ms you are right

> > +	 * share a generator.
> > +	 */
> > +	period_ns = rounddown(period_ns, AIROHA_PWM_PERIOD_TICK_NS);
> > +
> > +	/* Convert ns to ticks */
> > +	period_ticks = airoha_pwm_get_period_ticks_from_ns(period_ns);
> 
> Rounding down to the next multiple of 4ns isn't needed for
> airoha_pwm_get_period_ticks_from_ns() which is just a division by
> AIROHA_PWM_PERIOD_TICK_NS.
> 

Ok will drop.

> > +	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.
> 

Hi Uwe,

sorry for checking this only now and maybe we need to catch this again.

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?

> > +
> > +	return airoha_pwm_config(pc, pwm, period_ticks, duty_ticks);
> > +}
> 
> Best regards
> Uwe



-- 
	Ansuel

  reply	other threads:[~2025-09-08 18: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 [this message]
2025-09-09 10:26     ` Uwe Kleine-König
2025-09-09 11:48       ` Christian Marangi
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=68bf2509.050a0220.702b3.c003@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.