Skip to content

Commit 21e34a3

Browse files
committed
Merge remote-tracking branch 'spi/topic/quad' into spi-qspi
2 parents 505a149 + db90a44 commit 21e34a3

2 files changed

Lines changed: 144 additions & 3 deletions

File tree

‎drivers/spi/spi.c‎

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,51 @@ static void of_register_spi_devices(struct spi_master *master)
885885
if (of_find_property(nc, "spi-3wire", NULL))
886886
spi->mode |= SPI_3WIRE;
887887

888+
/* Device DUAL/QUAD mode */
889+
prop = of_get_property(nc, "spi-tx-nbits", &len);
890+
if (!prop || len < sizeof(*prop)) {
891+
dev_err(&master->dev, "%s has no 'spi-tx-nbits' property\n",
892+
nc->full_name);
893+
spi_dev_put(spi);
894+
continue;
895+
}
896+
switch (be32_to_cpup(prop)) {
897+
case SPI_NBITS_SINGLE:
898+
break;
899+
case SPI_NBITS_DUAL:
900+
spi->mode |= SPI_TX_DUAL;
901+
break;
902+
case SPI_NBITS_QUAD:
903+
spi->mode |= SPI_TX_QUAD;
904+
break;
905+
default:
906+
dev_err(&master->dev, "spi-tx-nbits value is not supported\n");
907+
spi_dev_put(spi);
908+
continue;
909+
}
910+
911+
prop = of_get_property(nc, "spi-rx-nbits", &len);
912+
if (!prop || len < sizeof(*prop)) {
913+
dev_err(&master->dev, "%s has no 'spi-rx-nbits' property\n",
914+
nc->full_name);
915+
spi_dev_put(spi);
916+
continue;
917+
}
918+
switch (be32_to_cpup(prop)) {
919+
case SPI_NBITS_SINGLE:
920+
break;
921+
case SPI_NBITS_DUAL:
922+
spi->mode |= SPI_RX_DUAL;
923+
break;
924+
case SPI_NBITS_QUAD:
925+
spi->mode |= SPI_RX_QUAD;
926+
break;
927+
default:
928+
dev_err(&master->dev, "spi-rx-nbits value is not supported\n");
929+
spi_dev_put(spi);
930+
continue;
931+
}
932+
888933
/* Device speed */
889934
prop = of_get_property(nc, "spi-max-frequency", &len);
890935
if (!prop || len < sizeof(*prop)) {
@@ -1332,6 +1377,19 @@ int spi_setup(struct spi_device *spi)
13321377
unsigned bad_bits;
13331378
int status = 0;
13341379

1380+
/* check mode to prevent that DUAL and QUAD set at the same time
1381+
*/
1382+
if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1383+
((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1384+
dev_err(&spi->dev,
1385+
"setup: can not select dual and quad at the same time\n");
1386+
return -EINVAL;
1387+
}
1388+
/* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1389+
*/
1390+
if ((spi->mode & SPI_3WIRE) && (spi->mode &
1391+
(SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1392+
return -EINVAL;
13351393
/* help drivers fail *cleanly* when they need options
13361394
* that aren't supported with their current master
13371395
*/
@@ -1367,6 +1425,11 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
13671425
struct spi_master *master = spi->master;
13681426
struct spi_transfer *xfer;
13691427

1428+
if (list_empty(&message->transfers))
1429+
return -EINVAL;
1430+
if (!message->complete)
1431+
return -EINVAL;
1432+
13701433
/* Half-duplex links include original MicroWire, and ones with
13711434
* only one data pin like SPI_3WIRE (switches direction) or where
13721435
* either MOSI or MISO is missing. They can also be caused by
@@ -1389,12 +1452,19 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
13891452
/**
13901453
* Set transfer bits_per_word and max speed as spi device default if
13911454
* it is not set for this transfer.
1455+
* Set transfer tx_nbits and rx_nbits as single transfer default
1456+
* (SPI_NBITS_SINGLE) if it is not set for this transfer.
13921457
*/
13931458
list_for_each_entry(xfer, &message->transfers, transfer_list) {
13941459
if (!xfer->bits_per_word)
13951460
xfer->bits_per_word = spi->bits_per_word;
1396-
if (!xfer->speed_hz)
1461+
if (!xfer->speed_hz) {
13971462
xfer->speed_hz = spi->max_speed_hz;
1463+
if (master->max_speed_hz &&
1464+
xfer->speed_hz > master->max_speed_hz)
1465+
xfer->speed_hz = master->max_speed_hz;
1466+
}
1467+
13981468
if (master->bits_per_word_mask) {
13991469
/* Only 32 bits fit in the mask */
14001470
if (xfer->bits_per_word > 32)
@@ -1403,6 +1473,53 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
14031473
BIT(xfer->bits_per_word - 1)))
14041474
return -EINVAL;
14051475
}
1476+
1477+
if (xfer->speed_hz && master->min_speed_hz &&
1478+
xfer->speed_hz < master->min_speed_hz)
1479+
return -EINVAL;
1480+
if (xfer->speed_hz && master->max_speed_hz &&
1481+
xfer->speed_hz > master->max_speed_hz)
1482+
1483+
if (xfer->tx_buf && !xfer->tx_nbits)
1484+
xfer->tx_nbits = SPI_NBITS_SINGLE;
1485+
if (xfer->rx_buf && !xfer->rx_nbits)
1486+
xfer->rx_nbits = SPI_NBITS_SINGLE;
1487+
/* check transfer tx/rx_nbits:
1488+
* 1. keep the value is not out of single, dual and quad
1489+
* 2. keep tx/rx_nbits is contained by mode in spi_device
1490+
* 3. if SPI_3WIRE, tx/rx_nbits should be in single
1491+
*/
1492+
if (xfer->tx_buf) {
1493+
if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
1494+
xfer->tx_nbits != SPI_NBITS_DUAL &&
1495+
xfer->tx_nbits != SPI_NBITS_QUAD)
1496+
return -EINVAL;
1497+
if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
1498+
!(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
1499+
return -EINVAL;
1500+
if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
1501+
!(spi->mode & SPI_TX_QUAD))
1502+
return -EINVAL;
1503+
if ((spi->mode & SPI_3WIRE) &&
1504+
(xfer->tx_nbits != SPI_NBITS_SINGLE))
1505+
return -EINVAL;
1506+
}
1507+
/* check transfer rx_nbits */
1508+
if (xfer->rx_buf) {
1509+
if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
1510+
xfer->rx_nbits != SPI_NBITS_DUAL &&
1511+
xfer->rx_nbits != SPI_NBITS_QUAD)
1512+
return -EINVAL;
1513+
if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
1514+
!(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
1515+
return -EINVAL;
1516+
if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
1517+
!(spi->mode & SPI_RX_QUAD))
1518+
return -EINVAL;
1519+
if ((spi->mode & SPI_3WIRE) &&
1520+
(xfer->rx_nbits != SPI_NBITS_SINGLE))
1521+
return -EINVAL;
1522+
}
14061523
}
14071524

14081525
message->spi = spi;

‎include/linux/spi/spi.h‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct spi_device {
7474
struct spi_master *master;
7575
u32 max_speed_hz;
7676
u8 chip_select;
77-
u8 mode;
77+
u16 mode;
7878
#define SPI_CPHA 0x01 /* clock phase */
7979
#define SPI_CPOL 0x02 /* clock polarity */
8080
#define SPI_MODE_0 (0|0) /* (original MicroWire) */
@@ -87,6 +87,10 @@ struct spi_device {
8787
#define SPI_LOOP 0x20 /* loopback mode */
8888
#define SPI_NO_CS 0x40 /* 1 dev/bus, no chipselect */
8989
#define SPI_READY 0x80 /* slave pulls low to pause */
90+
#define SPI_TX_DUAL 0x100 /* transmit with 2 wires */
91+
#define SPI_TX_QUAD 0x200 /* transmit with 4 wires */
92+
#define SPI_RX_DUAL 0x400 /* receive with 2 wires */
93+
#define SPI_RX_QUAD 0x800 /* receive with 4 wires */
9094
u8 bits_per_word;
9195
int irq;
9296
void *controller_state;
@@ -233,6 +237,8 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
233237
* suported. If set, the SPI core will reject any transfer with an
234238
* unsupported bits_per_word. If not set, this value is simply ignored,
235239
* and it's up to the individual driver to perform any validation.
240+
* @min_speed_hz: Lowest supported transfer speed
241+
* @max_speed_hz: Highest supported transfer speed
236242
* @flags: other constraints relevant to this driver
237243
* @bus_lock_spinlock: spinlock for SPI bus locking
238244
* @bus_lock_mutex: mutex for SPI bus locking
@@ -315,6 +321,10 @@ struct spi_master {
315321
#define SPI_BIT_MASK(bits) (((bits) == 32) ? ~0UL : (BIT(bits) - 1))
316322
#define SPI_BPW_RANGE_MASK(min, max) (SPI_BIT_MASK(max) - SPI_BIT_MASK(min - 1))
317323

324+
/* limits on transfer speed */
325+
u32 min_speed_hz;
326+
u32 max_speed_hz;
327+
318328
/* other constraints relevant to this driver */
319329
u16 flags;
320330
#define SPI_MASTER_HALF_DUPLEX BIT(0) /* can't do full duplex */
@@ -453,6 +463,10 @@ extern struct spi_master *spi_busnum_to_master(u16 busnum);
453463
* @rx_buf: data to be read (dma-safe memory), or NULL
454464
* @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped
455465
* @rx_dma: DMA address of rx_buf, if @spi_message.is_dma_mapped
466+
* @tx_nbits: number of bits used for writting. If 0 the default
467+
* (SPI_NBITS_SINGLE) is used.
468+
* @rx_nbits: number of bits used for reading. If 0 the default
469+
* (SPI_NBITS_SINGLE) is used.
456470
* @len: size of rx and tx buffers (in bytes)
457471
* @speed_hz: Select a speed other than the device default for this
458472
* transfer. If 0 the default (from @spi_device) is used.
@@ -507,6 +521,11 @@ extern struct spi_master *spi_busnum_to_master(u16 busnum);
507521
* by the results of previous messages and where the whole transaction
508522
* ends when the chipselect goes intactive.
509523
*
524+
* When SPI can transfer in 1x,2x or 4x. It can get this tranfer information
525+
* from device through @tx_nbits and @rx_nbits. In Bi-direction, these
526+
* two should both be set. User can set transfer mode with SPI_NBITS_SINGLE(1x)
527+
* SPI_NBITS_DUAL(2x) and SPI_NBITS_QUAD(4x) to support these three transfer.
528+
*
510529
* The code that submits an spi_message (and its spi_transfers)
511530
* to the lower layers is responsible for managing its memory.
512531
* Zero-initialize every field you don't set up explicitly, to
@@ -527,6 +546,11 @@ struct spi_transfer {
527546
dma_addr_t rx_dma;
528547

529548
unsigned cs_change:1;
549+
u8 tx_nbits;
550+
u8 rx_nbits;
551+
#define SPI_NBITS_SINGLE 0x01 /* 1bit transfer */
552+
#define SPI_NBITS_DUAL 0x02 /* 2bits transfer */
553+
#define SPI_NBITS_QUAD 0x04 /* 4bits transfer */
530554
u8 bits_per_word;
531555
u16 delay_usecs;
532556
u32 speed_hz;
@@ -874,7 +898,7 @@ struct spi_board_info {
874898
/* mode becomes spi_device.mode, and is essential for chips
875899
* where the default of SPI_CS_HIGH = 0 is wrong.
876900
*/
877-
u8 mode;
901+
u16 mode;
878902

879903
/* ... may need additional spi_device chip config data here.
880904
* avoid stuff protocol drivers can set; but include stuff

0 commit comments

Comments
 (0)