aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r--drivers/spi/spi.c70
1 files changed, 67 insertions, 3 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 8eba98c8ed1e..70845ccd85c3 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -265,7 +265,7 @@ int spi_add_device(struct spi_device *spi)
265 * normally rely on the device being setup. Devices 265 * normally rely on the device being setup. Devices
266 * using SPI_CS_HIGH can't coexist well otherwise... 266 * using SPI_CS_HIGH can't coexist well otherwise...
267 */ 267 */
268 status = spi->master->setup(spi); 268 status = spi_setup(spi);
269 if (status < 0) { 269 if (status < 0) {
270 dev_err(dev, "can't %s %s, status %d\n", 270 dev_err(dev, "can't %s %s, status %d\n",
271 "setup", dev_name(&spi->dev), status); 271 "setup", dev_name(&spi->dev), status);
@@ -583,6 +583,70 @@ EXPORT_SYMBOL_GPL(spi_busnum_to_master);
583 583
584/*-------------------------------------------------------------------------*/ 584/*-------------------------------------------------------------------------*/
585 585
586/* Core methods for SPI master protocol drivers. Some of the
587 * other core methods are currently defined as inline functions.
588 */
589
590/**
591 * spi_setup - setup SPI mode and clock rate
592 * @spi: the device whose settings are being modified
593 * Context: can sleep, and no requests are queued to the device
594 *
595 * SPI protocol drivers may need to update the transfer mode if the
596 * device doesn't work with its default. They may likewise need
597 * to update clock rates or word sizes from initial values. This function
598 * changes those settings, and must be called from a context that can sleep.
599 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
600 * effect the next time the device is selected and data is transferred to
601 * or from it. When this function returns, the spi device is deselected.
602 *
603 * Note that this call will fail if the protocol driver specifies an option
604 * that the underlying controller or its driver does not support. For
605 * example, not all hardware supports wire transfers using nine bit words,
606 * LSB-first wire encoding, or active-high chipselects.
607 */
608int spi_setup(struct spi_device *spi)
609{
610 unsigned bad_bits;
611 int status;
612
613 /* help drivers fail *cleanly* when they need options
614 * that aren't supported with their current master
615 */
616 bad_bits = spi->mode & ~spi->master->mode_bits;
617 if (bad_bits) {
618 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
619 bad_bits);
620 return -EINVAL;
621 }
622
623 if (!spi->bits_per_word)
624 spi->bits_per_word = 8;
625
626 status = spi->master->setup(spi);
627
628 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
629 "%u bits/w, %u Hz max --> %d\n",
630 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
631 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
632 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
633 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
634 (spi->mode & SPI_LOOP) ? "loopback, " : "",
635 spi->bits_per_word, spi->max_speed_hz,
636 status);
637
638 return status;
639}
640EXPORT_SYMBOL_GPL(spi_setup);
641
642
643/*-------------------------------------------------------------------------*/
644
645/* Utility methods for SPI master protocol drivers, layered on
646 * top of the core. Some other utility methods are defined as
647 * inline functions.
648 */
649
586static void spi_complete(void *arg) 650static void spi_complete(void *arg)
587{ 651{
588 complete(arg); 652 complete(arg);
@@ -636,8 +700,8 @@ static u8 *buf;
636 * @spi: device with which data will be exchanged 700 * @spi: device with which data will be exchanged
637 * @txbuf: data to be written (need not be dma-safe) 701 * @txbuf: data to be written (need not be dma-safe)
638 * @n_tx: size of txbuf, in bytes 702 * @n_tx: size of txbuf, in bytes
639 * @rxbuf: buffer into which data will be read 703 * @rxbuf: buffer into which data will be read (need not be dma-safe)
640 * @n_rx: size of rxbuf, in bytes (need not be dma-safe) 704 * @n_rx: size of rxbuf, in bytes
641 * Context: can sleep 705 * Context: can sleep
642 * 706 *
643 * This performs a half duplex MicroWire style transaction with the 707 * This performs a half duplex MicroWire style transaction with the