RE: [PATCH v3 13/14] counter: rz-mtu3-cnt: drop conditional locks

From: Biju Das

Date: Tue Nov 17 2026 - 12:10:10 EST




> -----Original Message-----
> From: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@xxxxxxxxxxx>
> Sent: 17 November 2026 10:26
> Subject: [PATCH v3 13/14] counter: rz-mtu3-cnt: drop conditional locks
>
> All read and write operations now only access the hardware if the respective count is enabled.
>
> As a count cannot be enabled without the underlying MTU channel(s) being claimed as busy by the count
> driver, drop conditional locks and replace them with simple locks.
>
> Use guard() where possible.
>
> Remove the conditional locking functions as there are no users left.
>
> Signed-off-by: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@xxxxxxxxxxx>

Reviewed-by: Biju Das <biju.das.jz@xxxxxxxxxxxxxx>

> ---
>
> V3:
> * adapt to "put runtime PM ref if counter init fails" changes
>
> V2:
> * no changes
>
> drivers/counter/rz-mtu3-cnt.c | 120 ++++++++--------------------------
> 1 file changed, 26 insertions(+), 94 deletions(-)
>
> diff --git a/drivers/counter/rz-mtu3-cnt.c b/drivers/counter/rz-mtu3-cnt.c index
> 07dd7f588911..8252dee6d04d 100644
> --- a/drivers/counter/rz-mtu3-cnt.c
> +++ b/drivers/counter/rz-mtu3-cnt.c
> @@ -104,20 +104,6 @@ static inline struct rz_mtu3_channel *rz_mtu3_get_ch(struct counter_device *coun
> return &priv->ch[ch_id];
> }
>
> -static int rz_mtu3_lock_if_count_is_enabled(struct rz_mtu3_channel *const ch,
> - struct rz_mtu3_cnt *const priv,
> - int id)
> -{
> - mutex_lock(&priv->lock);
> -
> - if (ch->is_busy && !priv->count_is_enabled[id]) {
> - mutex_unlock(&priv->lock);
> - return -EINVAL;
> - }
> -
> - return 0;
> -}
> -
> static void rz_mtu3_set_ceiling(struct rz_mtu3_channel *const ch, int id,
> u64 ceiling)
> {
> @@ -171,11 +157,11 @@ static int rz_mtu3_count_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);
>
> - mutex_lock(&priv->lock);
> + guard(mutex)(&priv->lock);
> +
> if (priv->count_is_enabled[count->id])
> priv->count[count->id] = rz_mtu3_get_count(ch, count->id);
> *val = priv->count[count->id];
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -190,11 +176,11 @@ static int rz_mtu3_count_write(struct counter_device *counter,
> if (count->id != RZ_MTU3_32_BIT_CH)
> count_val &= U16_MAX;
>
> - mutex_lock(&priv->lock);
> + guard(mutex)(&priv->lock);
> +
> if (priv->count_is_enabled[count->id])
> rz_mtu3_set_count(ch, count->id, count_val);
> priv->count[count->id] = count_val;
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -227,18 +213,11 @@ static int rz_mtu3_count_function_read(struct counter_device *counter,
> struct counter_count *count,
> enum counter_function *function) {
> - struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
> - int ret;
>
> - ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
> - if (ret)
> - return ret;
> + guard(mutex)(&priv->lock);
>
> - ret = rz_mtu3_count_function_read_helper(priv, count->id, function);
> - mutex_unlock(&priv->lock);
> -
> - return ret;
> + return rz_mtu3_count_function_read_helper(priv, count->id, function);
> }
>
> static int rz_mtu3_count_function_write(struct counter_device *counter, @@ -248,11 +227,6 @@ static
> int rz_mtu3_count_function_write(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);
> u8 timer_mode;
> - int ret;
> -
> - ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
> - if (ret)
> - return ret;
>
> switch (function) {
> case COUNTER_FUNCTION_QUADRATURE_X4:
> @@ -270,14 +244,14 @@ static int rz_mtu3_count_function_write(struct counter_device *counter,
> * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_3
> * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_5
> */
> - mutex_unlock(&priv->lock);
> return -EINVAL;
> }
>
> + guard(mutex)(&priv->lock);
> +
> if (priv->count_is_enabled[count->id])
> rz_mtu3_8bit_ch_write(ch, RZ_MTU3_TMDR1, timer_mode);
> priv->timer_mode[count->id] = timer_mode;
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -288,11 +262,8 @@ 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;
>
> - ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
> - if (ret)
> - return ret;
> + guard(mutex)(&priv->lock);
>
> if (priv->count_is_enabled[count->id])
> priv->direction[count->id] = rz_mtu3_get_direction(ch); @@ -300,8 +271,6 @@ static int
> rz_mtu3_count_direction_read(struct counter_device *counter,
> *direction = priv->direction[count->id] ?
> COUNTER_COUNT_DIRECTION_FORWARD : COUNTER_COUNT_DIRECTION_BACKWARD;
>
> - mutex_unlock(&priv->lock);
> -
> return 0;
> }
>
> @@ -311,9 +280,9 @@ static int rz_mtu3_count_ceiling_read(struct counter_device *counter, {
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
>
> - mutex_lock(&priv->lock);
> + guard(mutex)(&priv->lock);
> *ceiling = priv->ceiling[count->id];
> - mutex_unlock(&priv->lock);
> +
> return 0;
> }
>
> @@ -324,32 +293,26 @@ static int rz_mtu3_count_ceiling_write(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);
>
> - mutex_lock(&priv->lock);
> -
> switch (count->id) {
> case RZ_MTU3_16_BIT_MTU1_CH:
> case RZ_MTU3_16_BIT_MTU2_CH:
> - if (ceiling > U16_MAX) {
> - mutex_unlock(&priv->lock);
> + if (ceiling > U16_MAX)
> return -ERANGE;
> - }
> break;
> case RZ_MTU3_32_BIT_CH:
> - if (ceiling > U32_MAX) {
> - mutex_unlock(&priv->lock);
> + if (ceiling > U32_MAX)
> return -ERANGE;
> - }
> break;
> default:
> /* should never reach this path */
> - mutex_unlock(&priv->lock);
> return -EINVAL;
> }
>
> + guard(mutex)(&priv->lock);
> +
> if (priv->count_is_enabled[count->id])
> rz_mtu3_set_ceiling(ch, count->id, ceiling);
> priv->ceiling[count->id] = ceiling;
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -454,9 +417,8 @@ static int rz_mtu3_count_enable_read(struct counter_device *counter, {
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
>
> - mutex_lock(&priv->lock);
> + guard(mutex)(&priv->lock);
> *enable = priv->count_is_enabled[count->id];
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -467,17 +429,17 @@ static int rz_mtu3_count_enable_write(struct counter_device *counter,
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
> int ret = 0;
>
> - mutex_lock(&priv->lock);
> + guard(mutex)(&priv->lock);
>
> if (priv->count_is_enabled[count->id] == enable)
> - goto exit;
> + return 0;
>
> if (enable) {
> pm_runtime_get_sync(counter->parent);
> ret = rz_mtu3_initialize_counter(counter, count->id);
> if (ret < 0) {
> pm_runtime_put(counter->parent);
> - goto exit;
> + return ret;
> }
> } else {
> rz_mtu3_terminate_counter(counter, count->id); @@ -486,32 +448,16 @@ static int
> rz_mtu3_count_enable_write(struct counter_device *counter,
>
> priv->count_is_enabled[count->id] = enable;
>
> -exit:
> - mutex_unlock(&priv->lock);
> -
> return ret;
> }
>
> -static int rz_mtu3_lock_if_ch0_is_enabled(struct rz_mtu3_cnt *const priv) -{
> - mutex_lock(&priv->lock);
> - if (priv->ch->is_busy && !(priv->count_is_enabled[RZ_MTU3_16_BIT_MTU1_CH] ||
> - priv->count_is_enabled[RZ_MTU3_32_BIT_CH])) {
> - mutex_unlock(&priv->lock);
> - return -EINVAL;
> - }
> -
> - return 0;
> -}
> -
> static int rz_mtu3_cascade_counts_enable_get(struct counter_device *counter,
> u8 *cascade_enable)
> {
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
>
> - mutex_lock(&priv->lock);
> + guard(mutex)(&priv->lock);
> *cascade_enable = priv->cascade_enable;
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -521,9 +467,8 @@ static int rz_mtu3_cascade_counts_enable_set(struct counter_device *counter, {
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
>
> - mutex_lock(&priv->lock);
> + guard(mutex)(&priv->lock);
> priv->cascade_enable = cascade_enable;
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -532,14 +477,9 @@ static int rz_mtu3_ext_input_phase_clock_select_get(struct counter_device *count
> u32 *ext_input_phase_clock_select) {
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
> - int ret;
> -
> - ret = rz_mtu3_lock_if_ch0_is_enabled(priv);
> - if (ret)
> - return ret;
>
> + guard(mutex)(&priv->lock);
> *ext_input_phase_clock_select = priv->mtclkc_mtclkd;
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -548,17 +488,13 @@ static int rz_mtu3_ext_input_phase_clock_select_set(struct counter_device *count
> u32 ext_input_phase_clock_select) {
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
> - int ret;
>
> - ret = rz_mtu3_lock_if_ch0_is_enabled(priv);
> - if (ret)
> - return ret;
> + guard(mutex)(&priv->lock);
>
> if (priv->count_is_enabled[RZ_MTU3_16_BIT_MTU2_CH] ||
> priv->count_is_enabled[RZ_MTU3_32_BIT_CH])
> rz_mtu3_set_phcksel(priv->ch, ext_input_phase_clock_select);
> priv->mtclkc_mtclkd = ext_input_phase_clock_select;
> - mutex_unlock(&priv->lock);
>
> return 0;
> }
> @@ -584,14 +520,11 @@ static int rz_mtu3_action_read(struct counter_device *counter, {
> const bool is_signal_ab = (synapse->signal->id == SIGNAL_A_ID) ||
> (synapse->signal->id == SIGNAL_B_ID);
> - struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
> struct rz_mtu3_cnt *const priv = counter_priv(counter);
> enum counter_function function;
> int ret;
>
> - ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
> - if (ret)
> - return ret;
> + mutex_lock(&priv->lock);
>
> ret = rz_mtu3_count_function_read_helper(priv, count->id, &function);
> if (ret) {
> @@ -610,6 +543,8 @@ static int rz_mtu3_action_read(struct counter_device *counter,
> }
> }
>
> + mutex_unlock(&priv->lock);
> +
> switch (function) {
> case COUNTER_FUNCTION_PULSE_DIRECTION:
> /*
> @@ -636,12 +571,9 @@ static int rz_mtu3_action_read(struct counter_device *counter,
> break;
> default:
> /* should never reach this path */
> - mutex_unlock(&priv->lock);
> return -EINVAL;
> }
>
> - mutex_unlock(&priv->lock);
> -
> return 0;
> }
>
> --
> 2.56.0