Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi: Add SPI_NO_TX/RX support

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>
Link: https://lore.kernel.org/r/20201221152936.53873-2-alexandru.ardelean@analog.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Dragos Bogdan and committed by
Mark Brown
d962608c f7005142

+47 -5
+20 -5
drivers/spi/spi.c
··· 1941 1941 /* Device DUAL/QUAD mode */ 1942 1942 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 1943 1943 switch (value) { 1944 + case 0: 1945 + spi->mode |= SPI_NO_TX; 1946 + break; 1944 1947 case 1: 1945 1948 break; 1946 1949 case 2: ··· 1965 1962 1966 1963 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 1967 1964 switch (value) { 1965 + case 0: 1966 + spi->mode |= SPI_NO_RX; 1967 + break; 1968 1968 case 1: 1969 1969 break; 1970 1970 case 2: ··· 3335 3329 unsigned bad_bits, ugly_bits; 3336 3330 int status; 3337 3331 3338 - /* check mode to prevent that DUAL and QUAD set at the same time 3332 + /* 3333 + * check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO 3334 + * are set at the same time 3339 3335 */ 3340 - if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || 3341 - ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { 3336 + if ((hweight_long(spi->mode & 3337 + (SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) || 3338 + (hweight_long(spi->mode & 3339 + (SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) { 3342 3340 dev_err(&spi->dev, 3343 - "setup: can not select dual and quad at the same time\n"); 3341 + "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); 3344 3342 return -EINVAL; 3345 3343 } 3346 3344 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden ··· 3358 3348 * SPI_CS_WORD has a fallback software implementation, 3359 3349 * so it is ignored here. 3360 3350 */ 3361 - bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); 3351 + bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | 3352 + SPI_NO_TX | SPI_NO_RX); 3362 3353 /* nothing prevents from working with active-high CS in case if it 3363 3354 * is driven by GPIO. 3364 3355 */ ··· 3621 3610 * 2. check tx/rx_nbits match the mode in spi_device 3622 3611 */ 3623 3612 if (xfer->tx_buf) { 3613 + if (spi->mode & SPI_NO_TX) 3614 + return -EINVAL; 3624 3615 if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3625 3616 xfer->tx_nbits != SPI_NBITS_DUAL && 3626 3617 xfer->tx_nbits != SPI_NBITS_QUAD) ··· 3636 3623 } 3637 3624 /* check transfer rx_nbits */ 3638 3625 if (xfer->rx_buf) { 3626 + if (spi->mode & SPI_NO_RX) 3627 + return -EINVAL; 3639 3628 if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3640 3629 xfer->rx_nbits != SPI_NBITS_DUAL && 3641 3630 xfer->rx_nbits != SPI_NBITS_QUAD)
+17
include/linux/spi/spi.h
··· 6 6 #ifndef __LINUX_SPI_H 7 7 #define __LINUX_SPI_H 8 8 9 + #include <linux/bits.h> 9 10 #include <linux/device.h> 10 11 #include <linux/mod_devicetable.h> 11 12 #include <linux/slab.h> ··· 167 166 u8 chip_select; 168 167 u8 bits_per_word; 169 168 bool rt; 169 + #define SPI_NO_TX BIT(31) /* no transmit wire */ 170 + #define SPI_NO_RX BIT(30) /* no receive wire */ 171 + /* 172 + * All bits defined above should be covered by SPI_MODE_KERNEL_MASK. 173 + * The SPI_MODE_KERNEL_MASK has the SPI_MODE_USER_MASK counterpart, 174 + * which is defined in 'include/uapi/linux/spi/spi.h'. 175 + * The bits defined here are from bit 31 downwards, while in 176 + * SPI_MODE_USER_MASK are from 0 upwards. 177 + * These bits must not overlap. A static assert check should make sure of that. 178 + * If adding extra bits, make sure to decrease the bit index below as well. 179 + */ 180 + #define SPI_MODE_KERNEL_MASK (~(BIT(30) - 1)) 170 181 u32 mode; 171 182 int irq; 172 183 void *controller_state; ··· 201 188 * - ... 202 189 */ 203 190 }; 191 + 192 + /* Make sure that SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK don't overlap */ 193 + static_assert((SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK) == 0, 194 + "SPI_MODE_USER_MASK & SPI_MODE_KERNEL_MASK must not overlap"); 204 195 205 196 static inline struct spi_device *to_spi_device(struct device *dev) 206 197 {
+10
include/uapi/linux/spi/spi.h
··· 28 28 #define SPI_RX_OCTAL _BITUL(14) /* receive with 8 wires */ 29 29 #define SPI_3WIRE_HIZ _BITUL(15) /* high impedance turnaround */ 30 30 31 + /* 32 + * All the bits defined above should be covered by SPI_MODE_USER_MASK. 33 + * The SPI_MODE_USER_MASK has the SPI_MODE_KERNEL_MASK counterpart in 34 + * 'include/linux/spi/spi.h'. The bits defined here are from bit 0 upwards 35 + * while in SPI_MODE_KERNEL_MASK they are from the other end downwards. 36 + * These bits must not overlap. A static assert check should make sure of that. 37 + * If adding extra bits, make sure to increase the bit index below as well. 38 + */ 39 + #define SPI_MODE_USER_MASK (_BITUL(16) - 1) 40 + 31 41 #endif /* _UAPI_SPI_H */