aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-08-14 14:54:09 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-08-14 14:54:09 -0400
commitb171aa27700c78511086a759383b033949c9d7f0 (patch)
tree2775b0682ea342dd49c5b997de248da73ec83049 /drivers/spi/spi.c
parent11ac552477e32835cb6970bf0a70c210807f5673 (diff)
parentb4225885deb569f7afcf1f3a9f069f74cc9db591 (diff)
Merge branch 'next-spi' of git://git.secretlab.ca/git/linux-2.6
* 'next-spi' of git://git.secretlab.ca/git/linux-2.6: spi/amba_pl022: Fix probe and remove hook section annotations. spi/mpc5121: change annotations for probe and remove functions spi/bitbang: reinitialize transfer parameters for every message spi/spi-gpio: add support for controllers without MISO or MOSI pin spi/bitbang: add support for SPI_MASTER_NO_{TX, RX} modes SPI100k: Fix 8-bit and RX-only transfers spi/mmc_spi: mmc_spi adaptations for SPI bus locking API spi/mmc_spi: SPI bus locking API, using mutex Fix trivial conflict in drivers/spi/mpc512x_psc_spi.c due to 'struct of_device' => 'struct platform_device' rename and __init/__exit to __devinit/__devexit fix.
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r--drivers/spi/spi.c225
1 files changed, 192 insertions, 33 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 1bb1b88780ce..a9e5c79ae52a 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -528,6 +528,10 @@ int spi_register_master(struct spi_master *master)
528 dynamic = 1; 528 dynamic = 1;
529 } 529 }
530 530
531 spin_lock_init(&master->bus_lock_spinlock);
532 mutex_init(&master->bus_lock_mutex);
533 master->bus_lock_flag = 0;
534
531 /* register the device, then userspace will see it. 535 /* register the device, then userspace will see it.
532 * registration fails if the bus ID is in use. 536 * registration fails if the bus ID is in use.
533 */ 537 */
@@ -670,6 +674,35 @@ int spi_setup(struct spi_device *spi)
670} 674}
671EXPORT_SYMBOL_GPL(spi_setup); 675EXPORT_SYMBOL_GPL(spi_setup);
672 676
677static int __spi_async(struct spi_device *spi, struct spi_message *message)
678{
679 struct spi_master *master = spi->master;
680
681 /* Half-duplex links include original MicroWire, and ones with
682 * only one data pin like SPI_3WIRE (switches direction) or where
683 * either MOSI or MISO is missing. They can also be caused by
684 * software limitations.
685 */
686 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
687 || (spi->mode & SPI_3WIRE)) {
688 struct spi_transfer *xfer;
689 unsigned flags = master->flags;
690
691 list_for_each_entry(xfer, &message->transfers, transfer_list) {
692 if (xfer->rx_buf && xfer->tx_buf)
693 return -EINVAL;
694 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
695 return -EINVAL;
696 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
697 return -EINVAL;
698 }
699 }
700
701 message->spi = spi;
702 message->status = -EINPROGRESS;
703 return master->transfer(spi, message);
704}
705
673/** 706/**
674 * spi_async - asynchronous SPI transfer 707 * spi_async - asynchronous SPI transfer
675 * @spi: device with which data will be exchanged 708 * @spi: device with which data will be exchanged
@@ -702,33 +735,68 @@ EXPORT_SYMBOL_GPL(spi_setup);
702int spi_async(struct spi_device *spi, struct spi_message *message) 735int spi_async(struct spi_device *spi, struct spi_message *message)
703{ 736{
704 struct spi_master *master = spi->master; 737 struct spi_master *master = spi->master;
738 int ret;
739 unsigned long flags;
705 740
706 /* Half-duplex links include original MicroWire, and ones with 741 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
707 * only one data pin like SPI_3WIRE (switches direction) or where
708 * either MOSI or MISO is missing. They can also be caused by
709 * software limitations.
710 */
711 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
712 || (spi->mode & SPI_3WIRE)) {
713 struct spi_transfer *xfer;
714 unsigned flags = master->flags;
715 742
716 list_for_each_entry(xfer, &message->transfers, transfer_list) { 743 if (master->bus_lock_flag)
717 if (xfer->rx_buf && xfer->tx_buf) 744 ret = -EBUSY;
718 return -EINVAL; 745 else
719 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) 746 ret = __spi_async(spi, message);
720 return -EINVAL;
721 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
722 return -EINVAL;
723 }
724 }
725 747
726 message->spi = spi; 748 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
727 message->status = -EINPROGRESS; 749
728 return master->transfer(spi, message); 750 return ret;
729} 751}
730EXPORT_SYMBOL_GPL(spi_async); 752EXPORT_SYMBOL_GPL(spi_async);
731 753
754/**
755 * spi_async_locked - version of spi_async with exclusive bus usage
756 * @spi: device with which data will be exchanged
757 * @message: describes the data transfers, including completion callback
758 * Context: any (irqs may be blocked, etc)
759 *
760 * This call may be used in_irq and other contexts which can't sleep,
761 * as well as from task contexts which can sleep.
762 *
763 * The completion callback is invoked in a context which can't sleep.
764 * Before that invocation, the value of message->status is undefined.
765 * When the callback is issued, message->status holds either zero (to
766 * indicate complete success) or a negative error code. After that
767 * callback returns, the driver which issued the transfer request may
768 * deallocate the associated memory; it's no longer in use by any SPI
769 * core or controller driver code.
770 *
771 * Note that although all messages to a spi_device are handled in
772 * FIFO order, messages may go to different devices in other orders.
773 * Some device might be higher priority, or have various "hard" access
774 * time requirements, for example.
775 *
776 * On detection of any fault during the transfer, processing of
777 * the entire message is aborted, and the device is deselected.
778 * Until returning from the associated message completion callback,
779 * no other spi_message queued to that device will be processed.
780 * (This rule applies equally to all the synchronous transfer calls,
781 * which are wrappers around this core asynchronous primitive.)
782 */
783int spi_async_locked(struct spi_device *spi, struct spi_message *message)
784{
785 struct spi_master *master = spi->master;
786 int ret;
787 unsigned long flags;
788
789 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
790
791 ret = __spi_async(spi, message);
792
793 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
794
795 return ret;
796
797}
798EXPORT_SYMBOL_GPL(spi_async_locked);
799
732 800
733/*-------------------------------------------------------------------------*/ 801/*-------------------------------------------------------------------------*/
734 802
@@ -742,6 +810,32 @@ static void spi_complete(void *arg)
742 complete(arg); 810 complete(arg);
743} 811}
744 812
813static int __spi_sync(struct spi_device *spi, struct spi_message *message,
814 int bus_locked)
815{
816 DECLARE_COMPLETION_ONSTACK(done);
817 int status;
818 struct spi_master *master = spi->master;
819
820 message->complete = spi_complete;
821 message->context = &done;
822
823 if (!bus_locked)
824 mutex_lock(&master->bus_lock_mutex);
825
826 status = spi_async_locked(spi, message);
827
828 if (!bus_locked)
829 mutex_unlock(&master->bus_lock_mutex);
830
831 if (status == 0) {
832 wait_for_completion(&done);
833 status = message->status;
834 }
835 message->context = NULL;
836 return status;
837}
838
745/** 839/**
746 * spi_sync - blocking/synchronous SPI data transfers 840 * spi_sync - blocking/synchronous SPI data transfers
747 * @spi: device with which data will be exchanged 841 * @spi: device with which data will be exchanged
@@ -765,21 +859,86 @@ static void spi_complete(void *arg)
765 */ 859 */
766int spi_sync(struct spi_device *spi, struct spi_message *message) 860int spi_sync(struct spi_device *spi, struct spi_message *message)
767{ 861{
768 DECLARE_COMPLETION_ONSTACK(done); 862 return __spi_sync(spi, message, 0);
769 int status;
770
771 message->complete = spi_complete;
772 message->context = &done;
773 status = spi_async(spi, message);
774 if (status == 0) {
775 wait_for_completion(&done);
776 status = message->status;
777 }
778 message->context = NULL;
779 return status;
780} 863}
781EXPORT_SYMBOL_GPL(spi_sync); 864EXPORT_SYMBOL_GPL(spi_sync);
782 865
866/**
867 * spi_sync_locked - version of spi_sync with exclusive bus usage
868 * @spi: device with which data will be exchanged
869 * @message: describes the data transfers
870 * Context: can sleep
871 *
872 * This call may only be used from a context that may sleep. The sleep
873 * is non-interruptible, and has no timeout. Low-overhead controller
874 * drivers may DMA directly into and out of the message buffers.
875 *
876 * This call should be used by drivers that require exclusive access to the
877 * SPI bus. It has to be preceeded by a spi_bus_lock call. The SPI bus must
878 * be released by a spi_bus_unlock call when the exclusive access is over.
879 *
880 * It returns zero on success, else a negative error code.
881 */
882int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
883{
884 return __spi_sync(spi, message, 1);
885}
886EXPORT_SYMBOL_GPL(spi_sync_locked);
887
888/**
889 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
890 * @master: SPI bus master that should be locked for exclusive bus access
891 * Context: can sleep
892 *
893 * This call may only be used from a context that may sleep. The sleep
894 * is non-interruptible, and has no timeout.
895 *
896 * This call should be used by drivers that require exclusive access to the
897 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
898 * exclusive access is over. Data transfer must be done by spi_sync_locked
899 * and spi_async_locked calls when the SPI bus lock is held.
900 *
901 * It returns zero on success, else a negative error code.
902 */
903int spi_bus_lock(struct spi_master *master)
904{
905 unsigned long flags;
906
907 mutex_lock(&master->bus_lock_mutex);
908
909 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
910 master->bus_lock_flag = 1;
911 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
912
913 /* mutex remains locked until spi_bus_unlock is called */
914
915 return 0;
916}
917EXPORT_SYMBOL_GPL(spi_bus_lock);
918
919/**
920 * spi_bus_unlock - release the lock for exclusive SPI bus usage
921 * @master: SPI bus master that was locked for exclusive bus access
922 * Context: can sleep
923 *
924 * This call may only be used from a context that may sleep. The sleep
925 * is non-interruptible, and has no timeout.
926 *
927 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
928 * call.
929 *
930 * It returns zero on success, else a negative error code.
931 */
932int spi_bus_unlock(struct spi_master *master)
933{
934 master->bus_lock_flag = 0;
935
936 mutex_unlock(&master->bus_lock_mutex);
937
938 return 0;
939}
940EXPORT_SYMBOL_GPL(spi_bus_unlock);
941
783/* portable code must never pass more than 32 bytes */ 942/* portable code must never pass more than 32 bytes */
784#define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) 943#define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
785 944