mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Biju Das <biju.das.jz@bp.renesas.com>
To: Cosmin-Gabriel Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>,
	William Breathitt Gray <wbg@kernel.org>
Cc: "linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>,
	"linux-renesas-soc@vger.kernel.org"
	<linux-renesas-soc@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Cosmin-Gabriel Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>
Subject: RE: [PATCH v3 11/14] counter: rz-mtu3-cnt: cache direction values
Date: Tue, 17 Nov 2026 13:49:40 +0000	[thread overview]
Message-ID: <TYYPR01MB66036EDFF90414052C73ACF3867B2@TYYPR01MB6603.jpnprd01.prod.outlook.com> (raw)
In-Reply-To: <20261117102607.2504551-12-cosmin-gabriel.tanislav.xa@renesas.com>

Hi Cosmin,

Thanks for the patch.

> -----Original Message-----
> From: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>
> Sent: 17 November 2026 10:26
> Subject: [PATCH v3 11/14] counter: rz-mtu3-cnt: cache direction values
> 
> Reading registers without the count being enabled is an issue as the
> ch->is_busy check inside rz_mtu3_lock_if_count_is_enabled() can race
> with PWM operations.
> 
> Cache the direction value on every read and on disable, and return the cached value if the count is
> disabled.
> 
> Since runtime PM is ensured to be resumed if the count is enabled, drop the pm_runtime_get_sync() and
> pm_runtime_put() calls.
> 
> Signed-off-by: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>
> ---
> 
> V3:
>  * no changes
> 
> V2:
>  * add struct rz_mtu3_cnt::direction documentation
> 
>  drivers/counter/rz-mtu3-cnt.c | 19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/counter/rz-mtu3-cnt.c b/drivers/counter/rz-mtu3-cnt.c index
> 4b13716d6cb7..e3578850f024 100644
> --- a/drivers/counter/rz-mtu3-cnt.c
> +++ b/drivers/counter/rz-mtu3-cnt.c
> @@ -70,6 +70,7 @@
>   * @count: Cache for count values
>   * @timer_mode: Cache for timer mode values
>   * @mtclkc_mtclkd: Cache for external input phase clock select value
> + * @direction: Cache for direction values
>   */
>  struct rz_mtu3_cnt {
>  	struct mutex lock;
> @@ -78,6 +79,7 @@ struct rz_mtu3_cnt {
>  	u32 count[RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS];
>  	u8 timer_mode[RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS];
>  	bool count_is_enabled[RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS];
> +	bool direction[RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS];
>  	bool mtclkc_mtclkd;
>  };
> 
> @@ -186,6 +188,13 @@ static void rz_mtu3_set_phcksel(struct rz_mtu3_channel *const ch, bool enable)
>  				      enable);
>  }
> 
> +static bool rz_mtu3_get_direction(struct rz_mtu3_channel *const ch) {
> +	u8 tsr = rz_mtu3_8bit_ch_read(ch, RZ_MTU3_TSR);
> +
> +	return !!(tsr & RZ_MTU3_TSR_TCFD);
> +}
> +
>  static int rz_mtu3_count_read(struct counter_device *counter,
>  			      struct counter_count *count, u64 *val)  { @@ -318,18 +327,17 @@ static int
> rz_mtu3_count_direction_read(struct counter_device *counter,
>  	struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
>  	int ret;
> -	u8 tsr;
> 
>  	ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
>  	if (ret)
>  		return ret;
> 
> -	pm_runtime_get_sync(counter->parent);
> -	tsr = rz_mtu3_8bit_ch_read(ch, RZ_MTU3_TSR);
> -	pm_runtime_put(counter->parent);
> +	if (priv->count_is_enabled[count->id])
> +		priv->direction[count->id] = rz_mtu3_get_direction(ch);

Now priv->direction[count->id] is zero-initialised, previously it was SoC register's reset
value. Should we initialize these values in probe so that there is no change in behaviour?

Cheers,
Biju

> 
> -	*direction = (tsr & RZ_MTU3_TSR_TCFD) ?
> +	*direction = priv->direction[count->id] ?
>  		COUNTER_COUNT_DIRECTION_FORWARD : COUNTER_COUNT_DIRECTION_BACKWARD;
> +
>  	mutex_unlock(&priv->lock);
> 
>  	return 0;
> @@ -472,6 +480,7 @@ static void rz_mtu3_terminate_counter(struct counter_device *counter, int id)
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
> 
>  	priv->count[id] = rz_mtu3_get_count(ch, id);
> +	priv->direction[id] = rz_mtu3_get_direction(ch);
> 
>  	if (id == RZ_MTU3_32_BIT_CH) {
>  		rz_mtu3_disable(ch2);
> --
> 2.56.0

  reply	other threads:[~2026-11-17 13:49 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 10:25 [PATCH v3 00/14] MTU3 counter fixes and improvements Cosmin Tanislav
2026-11-17 10:25 ` [PATCH v3 01/14] counter: rz-mtu3-cnt: do not assign struct rz_mtu3_channel::dev Cosmin Tanislav
2026-11-17 14:04   ` Biju Das
2026-11-17 10:25 ` [PATCH v3 02/14] counter: rz-mtu3-cnt: remove manual runtime PM handling Cosmin Tanislav
2026-11-17 14:04   ` Biju Das
2026-11-17 10:25 ` [PATCH v3 03/14] counter: rz-mtu3-cnt: use device-managed pm_runtime_enable() Cosmin Tanislav
2026-11-17 10:25 ` [PATCH v3 04/14] counter: rz-mtu3-cnt: read enable value from cache Cosmin Tanislav
2026-11-17 14:05   ` Biju Das
2026-11-17 10:25 ` [PATCH v3 05/14] counter: rz-mtu3-cnt: disable channel before releasing Cosmin Tanislav
2026-11-17 14:05   ` Biju Das
2026-11-17 10:25 ` [PATCH v3 06/14] counter: rz-mtu3-cnt: unify ceiling values Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 07/14] counter: rz-mtu3-cnt: cache " Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 08/14] counter: rz-mtu3-cnt: cache count values Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 09/14] counter: rz-mtu3-cnt: cache function values Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 10/14] counter: rz-mtu3-cnt: cache external input phase clock value Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 11/14] counter: rz-mtu3-cnt: cache direction values Cosmin Tanislav
2026-11-17 13:49   ` Biju Das [this message]
2026-11-17 10:26 ` [PATCH v3 12/14] counter: rz-mtu3-cnt: keep cascade_counts_enable in sync with usage Cosmin Tanislav
2026-11-17 14:28   ` Biju Das
2026-11-17 14:42     ` Cosmin-Gabriel Tanislav
2026-11-17 14:57       ` Biju Das
2026-11-17 15:01         ` Biju Das
2026-11-17 10:26 ` [PATCH v3 13/14] counter: rz-mtu3-cnt: drop conditional locks Cosmin Tanislav
2026-11-17 17:10   ` Biju Das
2026-11-17 10:26 ` [PATCH v3 14/14] counter: rz-mtu3-cnt: use pm_runtime_resume_and_get() Cosmin Tanislav
2026-11-17 17:04   ` Biju Das

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=TYYPR01MB66036EDFF90414052C73ACF3867B2@TYYPR01MB6603.jpnprd01.prod.outlook.com \
    --to=biju.das.jz@bp.renesas.com \
    --cc=cosmin-gabriel.tanislav.xa@renesas.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=wbg@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

all inboxes | Powered by JetHome®