RE: [PATCH v3 11/14] counter: rz-mtu3-cnt: cache direction values

From: Biju Das

Date: Tue Nov 17 2026 - 08:49:49 EST


Hi Cosmin,

Thanks for the patch.

> -----Original Message-----
> From: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@xxxxxxxxxxx>
> 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@xxxxxxxxxxx>
> ---
>
> 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