All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] can: mcp251x: add support for half duplex controllers
@ 2020-05-28 15:27 Tim Harvey
  2020-06-30 17:11 ` Tim Harvey
  0 siblings, 1 reply; 2+ messages in thread
From: Tim Harvey @ 2020-05-28 15:27 UTC (permalink / raw)
  To: Wolfgang Grandegger, Marc Kleine-Budde, linux-can, netdev, linux-kernel
  Cc: David S . Miller, Sean Nyekjaer, Andy Shevchenko,
	Timo Schlüßler, Gustavo A . R . Silva, Tim Harvey

Some SPI host controllers do not support full-duplex SPI and are
marked as such via the SPI_CONTROLLER_HALF_DUPLEX controller flag.

For such controllers use half duplex transactions but retain full
duplex transactions for the controllers that can handle those.

Signed-off-by: Tim Harvey <tharvey@gateworks.com>
---
 drivers/net/can/spi/mcp251x.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/drivers/net/can/spi/mcp251x.c b/drivers/net/can/spi/mcp251x.c
index 5009ff2..203ef20 100644
--- a/drivers/net/can/spi/mcp251x.c
+++ b/drivers/net/can/spi/mcp251x.c
@@ -290,8 +290,12 @@ static u8 mcp251x_read_reg(struct spi_device *spi, u8 reg)
 	priv->spi_tx_buf[0] = INSTRUCTION_READ;
 	priv->spi_tx_buf[1] = reg;
 
-	mcp251x_spi_trans(spi, 3);
-	val = priv->spi_rx_buf[2];
+	if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
+		spi_write_then_read(spi, priv->spi_tx_buf, 2, &val, 1);
+	} else {
+		mcp251x_spi_trans(spi, 3);
+		val = priv->spi_rx_buf[2];
+	}
 
 	return val;
 }
@@ -303,10 +307,18 @@ static void mcp251x_read_2regs(struct spi_device *spi, u8 reg, u8 *v1, u8 *v2)
 	priv->spi_tx_buf[0] = INSTRUCTION_READ;
 	priv->spi_tx_buf[1] = reg;
 
-	mcp251x_spi_trans(spi, 4);
+	if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
+		u8 val[2] = { 0 };
 
-	*v1 = priv->spi_rx_buf[2];
-	*v2 = priv->spi_rx_buf[3];
+		spi_write_then_read(spi, priv->spi_tx_buf, 2, val, 2);
+		*v1 = val[0];
+		*v2 = val[1];
+	} else {
+		mcp251x_spi_trans(spi, 4);
+
+		*v1 = priv->spi_rx_buf[2];
+		*v2 = priv->spi_rx_buf[3];
+	}
 }
 
 static void mcp251x_write_reg(struct spi_device *spi, u8 reg, u8 val)
@@ -409,8 +421,16 @@ static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf,
 			buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i);
 	} else {
 		priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx);
-		mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN);
-		memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN);
+		if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
+			spi_write_then_read(spi, priv->spi_tx_buf, 1,
+					    priv->spi_rx_buf,
+					    SPI_TRANSFER_BUF_LEN);
+			memcpy(buf + 1, priv->spi_rx_buf,
+			       SPI_TRANSFER_BUF_LEN - 1);
+		} else {
+			mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN);
+			memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN);
+		}
 	}
 }
 
-- 
2.7.4

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

* Re: [PATCH] can: mcp251x: add support for half duplex controllers
  2020-05-28 15:27 [PATCH] can: mcp251x: add support for half duplex controllers Tim Harvey
@ 2020-06-30 17:11 ` Tim Harvey
  0 siblings, 0 replies; 2+ messages in thread
From: Tim Harvey @ 2020-06-30 17:11 UTC (permalink / raw)
  To: Wolfgang Grandegger, Marc Kleine-Budde, linux-can, netdev, open list
  Cc: David S . Miller, Sean Nyekjaer, Andy Shevchenko,
	Timo Schlüßler, Gustavo A . R . Silva

On Thu, May 28, 2020 at 8:28 AM Tim Harvey <tharvey@gateworks.com> wrote:
>
> Some SPI host controllers do not support full-duplex SPI and are
> marked as such via the SPI_CONTROLLER_HALF_DUPLEX controller flag.
>
> For such controllers use half duplex transactions but retain full
> duplex transactions for the controllers that can handle those.
>
> Signed-off-by: Tim Harvey <tharvey@gateworks.com>
> ---
>  drivers/net/can/spi/mcp251x.c | 34 +++++++++++++++++++++++++++-------
>  1 file changed, 27 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/net/can/spi/mcp251x.c b/drivers/net/can/spi/mcp251x.c
> index 5009ff2..203ef20 100644
> --- a/drivers/net/can/spi/mcp251x.c
> +++ b/drivers/net/can/spi/mcp251x.c
> @@ -290,8 +290,12 @@ static u8 mcp251x_read_reg(struct spi_device *spi, u8 reg)
>         priv->spi_tx_buf[0] = INSTRUCTION_READ;
>         priv->spi_tx_buf[1] = reg;
>
> -       mcp251x_spi_trans(spi, 3);
> -       val = priv->spi_rx_buf[2];
> +       if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
> +               spi_write_then_read(spi, priv->spi_tx_buf, 2, &val, 1);
> +       } else {
> +               mcp251x_spi_trans(spi, 3);
> +               val = priv->spi_rx_buf[2];
> +       }
>
>         return val;
>  }
> @@ -303,10 +307,18 @@ static void mcp251x_read_2regs(struct spi_device *spi, u8 reg, u8 *v1, u8 *v2)
>         priv->spi_tx_buf[0] = INSTRUCTION_READ;
>         priv->spi_tx_buf[1] = reg;
>
> -       mcp251x_spi_trans(spi, 4);
> +       if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
> +               u8 val[2] = { 0 };
>
> -       *v1 = priv->spi_rx_buf[2];
> -       *v2 = priv->spi_rx_buf[3];
> +               spi_write_then_read(spi, priv->spi_tx_buf, 2, val, 2);
> +               *v1 = val[0];
> +               *v2 = val[1];
> +       } else {
> +               mcp251x_spi_trans(spi, 4);
> +
> +               *v1 = priv->spi_rx_buf[2];
> +               *v2 = priv->spi_rx_buf[3];
> +       }
>  }
>
>  static void mcp251x_write_reg(struct spi_device *spi, u8 reg, u8 val)
> @@ -409,8 +421,16 @@ static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf,
>                         buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i);
>         } else {
>                 priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx);
> -               mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN);
> -               memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN);
> +               if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
> +                       spi_write_then_read(spi, priv->spi_tx_buf, 1,
> +                                           priv->spi_rx_buf,
> +                                           SPI_TRANSFER_BUF_LEN);
> +                       memcpy(buf + 1, priv->spi_rx_buf,
> +                              SPI_TRANSFER_BUF_LEN - 1);
> +               } else {
> +                       mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN);
> +                       memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN);
> +               }
>         }
>  }
>
> --
> 2.7.4
>

Marc / Wolfgang,

Any feedback on this?

Best Regards,

Tim

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

end of thread, other threads:[~2020-06-30 17:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-28 15:27 [PATCH] can: mcp251x: add support for half duplex controllers Tim Harvey
2020-06-30 17:11 ` Tim Harvey

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.