diff mbox series

spi: Add SPI_NO_TX/RX support

Message ID 20201111141529.98147-1-alexandru.ardelean@analog.com
State Superseded
Headers show
Series spi: Add SPI_NO_TX/RX support | expand

Commit Message

Alexandru Ardelean Nov. 11, 2020, 2:15 p.m. UTC
From: Dragos Bogdan <dragos.bogdan@analog.com>

Transmit/receive only is a valid SPI mode. For example, the MOSI/TX line
might be missing from an ADC while for a DAC the MISO/RX line may be
optional. This patch adds these two new modes: SPI_NO_TX and
SPI_NO_RX. This way, the drivers will be able to identify if any of
these two lines is missing and to adjust the transfers accordingly.

Signed-off-by: Dragos Bogdan <dragos.bogdan@analog.com>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 drivers/spi/spi.c       | 25 ++++++++++++++++++++-----
 include/linux/spi/spi.h |  2 ++
 2 files changed, 22 insertions(+), 5 deletions(-)

Comments

Andy Shevchenko Nov. 11, 2020, 2:46 p.m. UTC | #1
On Wed, Nov 11, 2020 at 4:13 PM Alexandru Ardelean
<alexandru.ardelean@analog.com> wrote:
>

> From: Dragos Bogdan <dragos.bogdan@analog.com>

>

> Transmit/receive only is a valid SPI mode. For example, the MOSI/TX line

> might be missing from an ADC while for a DAC the MISO/RX line may be

> optional. This patch adds these two new modes: SPI_NO_TX and

> SPI_NO_RX. This way, the drivers will be able to identify if any of

> these two lines is missing and to adjust the transfers accordingly.


Shouldn't this update a binding documentation?

...

> -       /* check mode to prevent that DUAL and QUAD set at the same time

> +       /* check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO

> +        * are set at the same time

>          */


/*
 * Perhaps switch to proper multi-line comment style
 * at the same time?
 */

...

>  #define        SPI_TX_OCTAL    0x2000                  /* transmit with 8 wires */

>  #define        SPI_RX_OCTAL    0x4000                  /* receive with 8 wires */

>  #define        SPI_3WIRE_HIZ   0x8000                  /* high impedance turnaround */

> +#define        SPI_NO_TX       0x10000                 /* no transmit wire */

> +#define        SPI_NO_RX       0x20000                 /* no receive wire */


Perhaps switch to use BIT() instead at some point (as a prerequisite
patch, for example)

-- 
With Best Regards,
Andy Shevchenko
Alexandru Ardelean Nov. 11, 2020, 2:54 p.m. UTC | #2
On Wed, Nov 11, 2020 at 4:45 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>

> On Wed, Nov 11, 2020 at 4:13 PM Alexandru Ardelean

> <alexandru.ardelean@analog.com> wrote:

> >

> > From: Dragos Bogdan <dragos.bogdan@analog.com>

> >

> > Transmit/receive only is a valid SPI mode. For example, the MOSI/TX line

> > might be missing from an ADC while for a DAC the MISO/RX line may be

> > optional. This patch adds these two new modes: SPI_NO_TX and

> > SPI_NO_RX. This way, the drivers will be able to identify if any of

> > these two lines is missing and to adjust the transfers accordingly.

>

> Shouldn't this update a binding documentation?


oh,
good catch

>

> ...

>

> > -       /* check mode to prevent that DUAL and QUAD set at the same time

> > +       /* check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO

> > +        * are set at the same time

> >          */

>

> /*

>  * Perhaps switch to proper multi-line comment style

>  * at the same time?

>  */


¯\_(ツ)_/¯

Not sure what SPI prefers as multi-line comment style,
But ok.

>

> ...

>

> >  #define        SPI_TX_OCTAL    0x2000                  /* transmit with 8 wires */

> >  #define        SPI_RX_OCTAL    0x4000                  /* receive with 8 wires */

> >  #define        SPI_3WIRE_HIZ   0x8000                  /* high impedance turnaround */

> > +#define        SPI_NO_TX       0x10000                 /* no transmit wire */

> > +#define        SPI_NO_RX       0x20000                 /* no receive wire */

>

> Perhaps switch to use BIT() instead at some point (as a prerequisite

> patch, for example)


I'll leave this up to the maintainer preference.
Maybe I'll do it as a second patch here, so that I can be dropped.

>

> --

> With Best Regards,

> Andy Shevchenko
diff mbox series

Patch

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 22679c8645db..fc12fcf11126 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1934,6 +1934,9 @@  static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
 	/* Device DUAL/QUAD mode */
 	if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
 		switch (value) {
+		case 0:
+			spi->mode |= SPI_NO_TX;
+			break;
 		case 1:
 			break;
 		case 2:
@@ -1955,6 +1958,9 @@  static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
 
 	if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
 		switch (value) {
+		case 0:
+			spi->mode |= SPI_NO_RX;
+			break;
 		case 1:
 			break;
 		case 2:
@@ -3275,12 +3281,16 @@  int spi_setup(struct spi_device *spi)
 	unsigned	bad_bits, ugly_bits;
 	int		status;
 
-	/* check mode to prevent that DUAL and QUAD set at the same time
+	/* check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO
+	 * are set at the same time
 	 */
-	if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
-		((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
+	if ((hweight_long(spi->mode &
+		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) ||
+	    (hweight_long(spi->mode &
+		(SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) {
 		dev_err(&spi->dev,
-		"setup: can not select dual and quad at the same time\n");
+		"setup: can not select any two of dual, quad and no-rx/tx "
+		"at the same time\n");
 		return -EINVAL;
 	}
 	/* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
@@ -3294,7 +3304,8 @@  int spi_setup(struct spi_device *spi)
 	 * SPI_CS_WORD has a fallback software implementation,
 	 * so it is ignored here.
 	 */
-	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
+	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
+				 SPI_NO_TX | SPI_NO_RX);
 	/* nothing prevents from working with active-high CS in case if it
 	 * is driven by GPIO.
 	 */
@@ -3550,6 +3561,8 @@  static int __spi_validate(struct spi_device *spi, struct spi_message *message)
 		 * 2. check tx/rx_nbits match the mode in spi_device
 		 */
 		if (xfer->tx_buf) {
+			if (spi->mode & SPI_NO_TX)
+				return -EINVAL;
 			if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
 				xfer->tx_nbits != SPI_NBITS_DUAL &&
 				xfer->tx_nbits != SPI_NBITS_QUAD)
@@ -3563,6 +3576,8 @@  static int __spi_validate(struct spi_device *spi, struct spi_message *message)
 		}
 		/* check transfer rx_nbits */
 		if (xfer->rx_buf) {
+			if (spi->mode & SPI_NO_RX)
+				return -EINVAL;
 			if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
 				xfer->rx_nbits != SPI_NBITS_DUAL &&
 				xfer->rx_nbits != SPI_NBITS_QUAD)
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 99380c0825db..2e9058f79b2d 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -185,6 +185,8 @@  struct spi_device {
 #define	SPI_TX_OCTAL	0x2000			/* transmit with 8 wires */
 #define	SPI_RX_OCTAL	0x4000			/* receive with 8 wires */
 #define	SPI_3WIRE_HIZ	0x8000			/* high impedance turnaround */
+#define	SPI_NO_TX	0x10000			/* no transmit wire */
+#define	SPI_NO_RX	0x20000			/* no receive wire */
 	int			irq;
 	void			*controller_state;
 	void			*controller_data;