linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] counter: microchip-tcb-capture: Handle Signal1 read and Synapse
@ 2022-10-17 22:54 William Breathitt Gray
  2022-10-18  8:13 ` Kamel Bouhara
  0 siblings, 1 reply; 3+ messages in thread
From: William Breathitt Gray @ 2022-10-17 22:54 UTC (permalink / raw)
  To: linux-iio
  Cc: linux-kernel, linux-arm-kernel, stable, William Breathitt Gray,
	stable, Kamel Bouhara

The signal_read(), action_read(), and action_write() callbacks have been
assuming Signal0 is requested without checking. This results in requests
for Signal1 returning data for Signal0. This patch fixes these
oversights by properly checking for the Signal's id in the respective
callbacks and handling accordingly based on the particular Signal
requested. The trig_inverted member of the mchp_tc_data is removed as
superfluous.

Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
Cc: stable@kernel.org
Cc: Kamel Bouhara <kamel.bouhara@bootlin.com>
Signed-off-by: William Breathitt Gray <william.gray@linaro.org>
---
 drivers/counter/microchip-tcb-capture.c | 57 ++++++++++++++++---------
 1 file changed, 38 insertions(+), 19 deletions(-)

diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
index f9dee15d9777..438f82b07a03 100644
--- a/drivers/counter/microchip-tcb-capture.c
+++ b/drivers/counter/microchip-tcb-capture.c
@@ -28,7 +28,6 @@ struct mchp_tc_data {
 	int qdec_mode;
 	int num_channels;
 	int channel[2];
-	bool trig_inverted;
 };
 
 static const enum counter_function mchp_tc_count_functions[] = {
@@ -153,7 +152,7 @@ static int mchp_tc_count_signal_read(struct counter_device *counter,
 
 	regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
 
-	if (priv->trig_inverted)
+	if (signal->id == 1)
 		sigstatus = (sr & ATMEL_TC_MTIOB);
 	else
 		sigstatus = (sr & ATMEL_TC_MTIOA);
@@ -169,26 +168,46 @@ static int mchp_tc_count_action_read(struct counter_device *counter,
 				     enum counter_synapse_action *action)
 {
 	struct mchp_tc_data *const priv = counter_priv(counter);
+	const unsigned int cmr_reg = ATMEL_TC_REG(priv->channel[0], CMR);
+	enum counter_function function;
+	int err;
 	u32 cmr;
 
-	regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
+	err = mchp_tc_count_function_read(counter, count, &function);
+	if (err)
+		return err;
 
-	switch (cmr & ATMEL_TC_ETRGEDG) {
-	default:
-		*action = COUNTER_SYNAPSE_ACTION_NONE;
-		break;
-	case ATMEL_TC_ETRGEDG_RISING:
-		*action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
-		break;
-	case ATMEL_TC_ETRGEDG_FALLING:
-		*action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
-		break;
-	case ATMEL_TC_ETRGEDG_BOTH:
+	/* Default action mode */
+	*action = COUNTER_SYNAPSE_ACTION_NONE;
+
+	switch (function) {
+	case COUNTER_FUNCTION_INCREASE:
+		/* TIOB signal is ignored */
+		if (synapse->signal->id == 1)
+			return 0;
+
+		regmap_read(priv->regmap, cmr_reg, &cmr);
+
+		switch (cmr & ATMEL_TC_ETRGEDG) {
+		case ATMEL_TC_ETRGEDG_RISING:
+			*action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
+			return 0;
+		case ATMEL_TC_ETRGEDG_FALLING:
+			*action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
+			return 0;
+		case ATMEL_TC_ETRGEDG_BOTH:
+			*action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
+			return 0;
+		default:
+			return 0;
+		}
+	case COUNTER_FUNCTION_QUADRATURE_X4:
 		*action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
-		break;
+		return 0;
+	default:
+		/* should never reach this path */
+		return -EINVAL;
 	}
-
-	return 0;
 }
 
 static int mchp_tc_count_action_write(struct counter_device *counter,
@@ -199,8 +218,8 @@ static int mchp_tc_count_action_write(struct counter_device *counter,
 	struct mchp_tc_data *const priv = counter_priv(counter);
 	u32 edge = ATMEL_TC_ETRGEDG_NONE;
 
-	/* QDEC mode is rising edge only */
-	if (priv->qdec_mode)
+	/* QDEC mode is rising edge only; only TIOA handled in non-QDEC mode */
+	if (priv->qdec_mode || synapse->signal->id != 0)
 		return -EINVAL;
 
 	switch (action) {
-- 
2.37.3


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] counter: microchip-tcb-capture: Handle Signal1 read and Synapse
  2022-10-17 22:54 [PATCH] counter: microchip-tcb-capture: Handle Signal1 read and Synapse William Breathitt Gray
@ 2022-10-18  8:13 ` Kamel Bouhara
  2022-10-18 11:53   ` William Breathitt Gray
  0 siblings, 1 reply; 3+ messages in thread
From: Kamel Bouhara @ 2022-10-18  8:13 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, linux-kernel, linux-arm-kernel, stable, stable

On Mon, Oct 17, 2022 at 06:54:04PM -0400, William Breathitt Gray wrote:
> The signal_read(), action_read(), and action_write() callbacks have been
> assuming Signal0 is requested without checking. This results in requests
> for Signal1 returning data for Signal0. This patch fixes these
> oversights by properly checking for the Signal's id in the respective
> callbacks and handling accordingly based on the particular Signal
> requested. The trig_inverted member of the mchp_tc_data is removed as
> superfluous.
>
> Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
> Cc: stable@kernel.org
> Cc: Kamel Bouhara <kamel.bouhara@bootlin.com>
> Signed-off-by: William Breathitt Gray <william.gray@linaro.org>

Looks ok to me, thanks.

Reviewed-by: Kamel Bouhara <kamel.bouhara@bootlin.com>

> ---
>  drivers/counter/microchip-tcb-capture.c | 57 ++++++++++++++++---------
>  1 file changed, 38 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
> index f9dee15d9777..438f82b07a03 100644
> --- a/drivers/counter/microchip-tcb-capture.c
> +++ b/drivers/counter/microchip-tcb-capture.c
> @@ -28,7 +28,6 @@ struct mchp_tc_data {
>  	int qdec_mode;
>  	int num_channels;
>  	int channel[2];
> -	bool trig_inverted;
>  };
>
>  static const enum counter_function mchp_tc_count_functions[] = {
> @@ -153,7 +152,7 @@ static int mchp_tc_count_signal_read(struct counter_device *counter,
>
>  	regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
>
> -	if (priv->trig_inverted)
> +	if (signal->id == 1)
>  		sigstatus = (sr & ATMEL_TC_MTIOB);
>  	else
>  		sigstatus = (sr & ATMEL_TC_MTIOA);
> @@ -169,26 +168,46 @@ static int mchp_tc_count_action_read(struct counter_device *counter,
>  				     enum counter_synapse_action *action)
>  {
>  	struct mchp_tc_data *const priv = counter_priv(counter);
> +	const unsigned int cmr_reg = ATMEL_TC_REG(priv->channel[0], CMR);
> +	enum counter_function function;
> +	int err;
>  	u32 cmr;
>
> -	regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
> +	err = mchp_tc_count_function_read(counter, count, &function);
> +	if (err)
> +		return err;
>
> -	switch (cmr & ATMEL_TC_ETRGEDG) {
> -	default:
> -		*action = COUNTER_SYNAPSE_ACTION_NONE;
> -		break;
> -	case ATMEL_TC_ETRGEDG_RISING:
> -		*action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
> -		break;
> -	case ATMEL_TC_ETRGEDG_FALLING:
> -		*action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
> -		break;
> -	case ATMEL_TC_ETRGEDG_BOTH:
> +	/* Default action mode */
> +	*action = COUNTER_SYNAPSE_ACTION_NONE;
> +
> +	switch (function) {
> +	case COUNTER_FUNCTION_INCREASE:
> +		/* TIOB signal is ignored */
> +		if (synapse->signal->id == 1)
> +			return 0;
> +
> +		regmap_read(priv->regmap, cmr_reg, &cmr);
> +
> +		switch (cmr & ATMEL_TC_ETRGEDG) {
> +		case ATMEL_TC_ETRGEDG_RISING:
> +			*action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
> +			return 0;
> +		case ATMEL_TC_ETRGEDG_FALLING:
> +			*action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
> +			return 0;
> +		case ATMEL_TC_ETRGEDG_BOTH:
> +			*action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
> +			return 0;
> +		default:
> +			return 0;
> +		}
> +	case COUNTER_FUNCTION_QUADRATURE_X4:
>  		*action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
> -		break;
> +		return 0;
> +	default:
> +		/* should never reach this path */
> +		return -EINVAL;
>  	}
> -
> -	return 0;
>  }
>
>  static int mchp_tc_count_action_write(struct counter_device *counter,
> @@ -199,8 +218,8 @@ static int mchp_tc_count_action_write(struct counter_device *counter,
>  	struct mchp_tc_data *const priv = counter_priv(counter);
>  	u32 edge = ATMEL_TC_ETRGEDG_NONE;
>
> -	/* QDEC mode is rising edge only */
> -	if (priv->qdec_mode)
> +	/* QDEC mode is rising edge only; only TIOA handled in non-QDEC mode */
> +	if (priv->qdec_mode || synapse->signal->id != 0)
>  		return -EINVAL;
>
>  	switch (action) {
> --
> 2.37.3
>

--
Kamel Bouhara, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] counter: microchip-tcb-capture: Handle Signal1 read and Synapse
  2022-10-18  8:13 ` Kamel Bouhara
@ 2022-10-18 11:53   ` William Breathitt Gray
  0 siblings, 0 replies; 3+ messages in thread
From: William Breathitt Gray @ 2022-10-18 11:53 UTC (permalink / raw)
  To: Kamel Bouhara; +Cc: linux-iio, linux-kernel, linux-arm-kernel, stable, stable


[-- Attachment #1.1: Type: text/plain, Size: 1125 bytes --]

On Tue, Oct 18, 2022 at 10:13:51AM +0200, Kamel Bouhara wrote:
> On Mon, Oct 17, 2022 at 06:54:04PM -0400, William Breathitt Gray wrote:
> > The signal_read(), action_read(), and action_write() callbacks have been
> > assuming Signal0 is requested without checking. This results in requests
> > for Signal1 returning data for Signal0. This patch fixes these
> > oversights by properly checking for the Signal's id in the respective
> > callbacks and handling accordingly based on the particular Signal
> > requested. The trig_inverted member of the mchp_tc_data is removed as
> > superfluous.
> >
> > Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
> > Cc: stable@kernel.org
> > Cc: Kamel Bouhara <kamel.bouhara@bootlin.com>
> > Signed-off-by: William Breathitt Gray <william.gray@linaro.org>
> 
> Looks ok to me, thanks.
> 
> Reviewed-by: Kamel Bouhara <kamel.bouhara@bootlin.com>

I'm going to submit a v2 of this patch for the sake of making it a tad
easier to backport and also simpler to handle if a lock is introduced at
a later point for this driver.

William Breathitt Gray

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

[-- Attachment #2: Type: text/plain, Size: 176 bytes --]

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-10-18 11:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-17 22:54 [PATCH] counter: microchip-tcb-capture: Handle Signal1 read and Synapse William Breathitt Gray
2022-10-18  8:13 ` Kamel Bouhara
2022-10-18 11:53   ` William Breathitt Gray

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).