Skip to content

Commit d962608

Browse files
dbogdanbroonie
authored andcommitted
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>
1 parent f700514 commit d962608

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎drivers/spi/spi.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,9 @@ static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
19411941
/* Device DUAL/QUAD mode */
19421942
if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
19431943
switch (value) {
1944+
case 0:
1945+
spi->mode |= SPI_NO_TX;
1946+
break;
19441947
case 1:
19451948
break;
19461949
case 2:
@@ -1962,6 +1965,9 @@ static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
19621965

19631966
if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
19641967
switch (value) {
1968+
case 0:
1969+
spi->mode |= SPI_NO_RX;
1970+
break;
19651971
case 1:
19661972
break;
19671973
case 2:
@@ -3329,12 +3335,16 @@ int spi_setup(struct spi_device *spi)
33293335
unsigned bad_bits, ugly_bits;
33303336
int status;
33313337

3332-
/* check mode to prevent that DUAL and QUAD set at the same time
3338+
/*
3339+
* check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO
3340+
* are set at the same time
33333341
*/
3334-
if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
3335-
((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
3342+
if ((hweight_long(spi->mode &
3343+
(SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) ||
3344+
(hweight_long(spi->mode &
3345+
(SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) {
33363346
dev_err(&spi->dev,
3337-
"setup: can not select dual and quad at the same time\n");
3347+
"setup: can not select any two of dual, quad and no-rx/tx at the same time\n");
33383348
return -EINVAL;
33393349
}
33403350
/* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
@@ -3348,7 +3358,8 @@ int spi_setup(struct spi_device *spi)
33483358
* SPI_CS_WORD has a fallback software implementation,
33493359
* so it is ignored here.
33503360
*/
3351-
bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
3361+
bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
3362+
SPI_NO_TX | SPI_NO_RX);
33523363
/* nothing prevents from working with active-high CS in case if it
33533364
* is driven by GPIO.
33543365
*/
@@ -3610,6 +3621,8 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
36103621
* 2. check tx/rx_nbits match the mode in spi_device
36113622
*/
36123623
if (xfer->tx_buf) {
3624+
if (spi->mode & SPI_NO_TX)
3625+
return -EINVAL;
36133626
if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
36143627
xfer->tx_nbits != SPI_NBITS_DUAL &&
36153628
xfer->tx_nbits != SPI_NBITS_QUAD)
@@ -3623,6 +3636,8 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
36233636
}
36243637
/* check transfer rx_nbits */
36253638
if (xfer->rx_buf) {
3639+
if (spi->mode & SPI_NO_RX)
3640+
return -EINVAL;
36263641
if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
36273642
xfer->rx_nbits != SPI_NBITS_DUAL &&
36283643
xfer->rx_nbits != SPI_NBITS_QUAD)

‎include/linux/spi/spi.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef __LINUX_SPI_H
77
#define __LINUX_SPI_H
88

9+
#include <linux/bits.h>
910
#include <linux/device.h>
1011
#include <linux/mod_devicetable.h>
1112
#include <linux/slab.h>
@@ -166,6 +167,18 @@ struct spi_device {
166167
u8 chip_select;
167168
u8 bits_per_word;
168169
bool rt;
170+
#define SPI_NO_TX BIT(31) /* no transmit wire */
171+
#define SPI_NO_RX BIT(30) /* no receive wire */
172+
/*
173+
* All bits defined above should be covered by SPI_MODE_KERNEL_MASK.
174+
* The SPI_MODE_KERNEL_MASK has the SPI_MODE_USER_MASK counterpart,
175+
* which is defined in 'include/uapi/linux/spi/spi.h'.
176+
* The bits defined here are from bit 31 downwards, while in
177+
* SPI_MODE_USER_MASK are from 0 upwards.
178+
* These bits must not overlap. A static assert check should make sure of that.
179+
* If adding extra bits, make sure to decrease the bit index below as well.
180+
*/
181+
#define SPI_MODE_KERNEL_MASK (~(BIT(30) - 1))
169182
u32 mode;
170183
int irq;
171184
void *controller_state;
@@ -189,6 +202,10 @@ struct spi_device {
189202
*/
190203
};
191204

205+
/* Make sure that SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK don't overlap */
206+
static_assert((SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK) == 0,
207+
"SPI_MODE_USER_MASK & SPI_MODE_KERNEL_MASK must not overlap");
208+
192209
static inline struct spi_device *to_spi_device(struct device *dev)
193210
{
194211
return dev ? container_of(dev, struct spi_device, dev) : NULL;

‎include/uapi/linux/spi/spi.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@
2828
#define SPI_RX_OCTAL _BITUL(14) /* receive with 8 wires */
2929
#define SPI_3WIRE_HIZ _BITUL(15) /* high impedance turnaround */
3030

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+
3141
#endif /* _UAPI_SPI_H */

0 commit comments

Comments
 (0)