aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/spi/spi.c119
-rw-r--r--include/linux/spi/spi.h28
2 files changed, 144 insertions, 3 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 361cced68069..76e6ddf00ccc 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -885,6 +885,51 @@ static void of_register_spi_devices(struct spi_master *master)
885 if (of_find_property(nc, "spi-3wire", NULL)) 885 if (of_find_property(nc, "spi-3wire", NULL))
886 spi->mode |= SPI_3WIRE; 886 spi->mode |= SPI_3WIRE;
887 887
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
888 /* Device speed */ 933 /* Device speed */
889 prop = of_get_property(nc, "spi-max-frequency", &len); 934 prop = of_get_property(nc, "spi-max-frequency", &len);
890 if (!prop || len < sizeof(*prop)) { 935 if (!prop || len < sizeof(*prop)) {
@@ -1332,6 +1377,19 @@ int spi_setup(struct spi_device *spi)
1332 unsigned bad_bits; 1377 unsigned bad_bits;
1333 int status = 0; 1378 int status = 0;
1334 1379
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;
1335 /* help drivers fail *cleanly* when they need options 1393 /* help drivers fail *cleanly* when they need options
1336 * that aren't supported with their current master 1394 * that aren't supported with their current master
1337 */ 1395 */
@@ -1367,6 +1425,11 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
1367 struct spi_master *master = spi->master; 1425 struct spi_master *master = spi->master;
1368 struct spi_transfer *xfer; 1426 struct spi_transfer *xfer;
1369 1427
1428 if (list_empty(&message->transfers))
1429 return -EINVAL;
1430 if (!message->complete)
1431 return -EINVAL;
1432
1370 /* Half-duplex links include original MicroWire, and ones with 1433 /* Half-duplex links include original MicroWire, and ones with
1371 * only one data pin like SPI_3WIRE (switches direction) or where 1434 * only one data pin like SPI_3WIRE (switches direction) or where
1372 * either MOSI or MISO is missing. They can also be caused by 1435 * 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)
1389 /** 1452 /**
1390 * Set transfer bits_per_word and max speed as spi device default if 1453 * Set transfer bits_per_word and max speed as spi device default if
1391 * it is not set for this transfer. 1454 * 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.
1392 */ 1457 */
1393 list_for_each_entry(xfer, &message->transfers, transfer_list) { 1458 list_for_each_entry(xfer, &message->transfers, transfer_list) {
1394 if (!xfer->bits_per_word) 1459 if (!xfer->bits_per_word)
1395 xfer->bits_per_word = spi->bits_per_word; 1460 xfer->bits_per_word = spi->bits_per_word;
1396 if (!xfer->speed_hz) 1461 if (!xfer->speed_hz) {
1397 xfer->speed_hz = spi->max_speed_hz; 1462 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
1398 if (master->bits_per_word_mask) { 1468 if (master->bits_per_word_mask) {
1399 /* Only 32 bits fit in the mask */ 1469 /* Only 32 bits fit in the mask */
1400 if (xfer->bits_per_word > 32) 1470 if (xfer->bits_per_word > 32)
@@ -1403,6 +1473,53 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
1403 BIT(xfer->bits_per_word - 1))) 1473 BIT(xfer->bits_per_word - 1)))
1404 return -EINVAL; 1474 return -EINVAL;
1405 } 1475 }
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 }
1406 } 1523 }
1407 1524
1408 message->spi = spi; 1525 message->spi = spi;
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index bf0204c00053..d4a16a68b9cf 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -74,7 +74,7 @@ struct spi_device {
74 struct spi_master *master; 74 struct spi_master *master;
75 u32 max_speed_hz; 75 u32 max_speed_hz;
76 u8 chip_select; 76 u8 chip_select;
77 u8 mode; 77 u16 mode;
78#define SPI_CPHA 0x01 /* clock phase */ 78#define SPI_CPHA 0x01 /* clock phase */
79#define SPI_CPOL 0x02 /* clock polarity */ 79#define SPI_CPOL 0x02 /* clock polarity */
80#define SPI_MODE_0 (0|0) /* (original MicroWire) */ 80#define SPI_MODE_0 (0|0) /* (original MicroWire) */
@@ -87,6 +87,10 @@ struct spi_device {
87#define SPI_LOOP 0x20 /* loopback mode */ 87#define SPI_LOOP 0x20 /* loopback mode */
88#define SPI_NO_CS 0x40 /* 1 dev/bus, no chipselect */ 88#define SPI_NO_CS 0x40 /* 1 dev/bus, no chipselect */
89#define SPI_READY 0x80 /* slave pulls low to pause */ 89#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 */
90 u8 bits_per_word; 94 u8 bits_per_word;
91 int irq; 95 int irq;
92 void *controller_state; 96 void *controller_state;
@@ -233,6 +237,8 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
233 * suported. If set, the SPI core will reject any transfer with an 237 * suported. If set, the SPI core will reject any transfer with an
234 * unsupported bits_per_word. If not set, this value is simply ignored, 238 * unsupported bits_per_word. If not set, this value is simply ignored,
235 * and it's up to the individual driver to perform any validation. 239 * 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
236 * @flags: other constraints relevant to this driver 242 * @flags: other constraints relevant to this driver
237 * @bus_lock_spinlock: spinlock for SPI bus locking 243 * @bus_lock_spinlock: spinlock for SPI bus locking
238 * @bus_lock_mutex: mutex for SPI bus locking 244 * @bus_lock_mutex: mutex for SPI bus locking
@@ -315,6 +321,10 @@ struct spi_master {
315#define SPI_BIT_MASK(bits) (((bits) == 32) ? ~0UL : (BIT(bits) - 1)) 321#define SPI_BIT_MASK(bits) (((bits) == 32) ? ~0UL : (BIT(bits) - 1))
316#define SPI_BPW_RANGE_MASK(min, max) (SPI_BIT_MASK(max) - SPI_BIT_MASK(min - 1)) 322#define SPI_BPW_RANGE_MASK(min, max) (SPI_BIT_MASK(max) - SPI_BIT_MASK(min - 1))
317 323
324 /* limits on transfer speed */
325 u32 min_speed_hz;
326 u32 max_speed_hz;
327
318 /* other constraints relevant to this driver */ 328 /* other constraints relevant to this driver */
319 u16 flags; 329 u16 flags;
320#define SPI_MASTER_HALF_DUPLEX BIT(0) /* can't do full duplex */ 330#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);
453 * @rx_buf: data to be read (dma-safe memory), or NULL 463 * @rx_buf: data to be read (dma-safe memory), or NULL
454 * @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped 464 * @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped
455 * @rx_dma: DMA address of rx_buf, if @spi_message.is_dma_mapped 465 * @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.
456 * @len: size of rx and tx buffers (in bytes) 470 * @len: size of rx and tx buffers (in bytes)
457 * @speed_hz: Select a speed other than the device default for this 471 * @speed_hz: Select a speed other than the device default for this
458 * transfer. If 0 the default (from @spi_device) is used. 472 * transfer. If 0 the default (from @spi_device) is used.
@@ -507,6 +521,11 @@ extern struct spi_master *spi_busnum_to_master(u16 busnum);
507 * by the results of previous messages and where the whole transaction 521 * by the results of previous messages and where the whole transaction
508 * ends when the chipselect goes intactive. 522 * ends when the chipselect goes intactive.
509 * 523 *
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 *
510 * The code that submits an spi_message (and its spi_transfers) 529 * The code that submits an spi_message (and its spi_transfers)
511 * to the lower layers is responsible for managing its memory. 530 * to the lower layers is responsible for managing its memory.
512 * Zero-initialize every field you don't set up explicitly, to 531 * Zero-initialize every field you don't set up explicitly, to
@@ -527,6 +546,11 @@ struct spi_transfer {
527 dma_addr_t rx_dma; 546 dma_addr_t rx_dma;
528 547
529 unsigned cs_change:1; 548 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 */
530 u8 bits_per_word; 554 u8 bits_per_word;
531 u16 delay_usecs; 555 u16 delay_usecs;
532 u32 speed_hz; 556 u32 speed_hz;
@@ -874,7 +898,7 @@ struct spi_board_info {
874 /* mode becomes spi_device.mode, and is essential for chips 898 /* mode becomes spi_device.mode, and is essential for chips
875 * where the default of SPI_CS_HIGH = 0 is wrong. 899 * where the default of SPI_CS_HIGH = 0 is wrong.
876 */ 900 */
877 u8 mode; 901 u16 mode;
878 902
879 /* ... may need additional spi_device chip config data here. 903 /* ... may need additional spi_device chip config data here.
880 * avoid stuff protocol drivers can set; but include stuff 904 * avoid stuff protocol drivers can set; but include stuff