diff options
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r-- | drivers/spi/spi.c | 229 |
1 files changed, 196 insertions, 33 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index b3a1f9259b62..a9e5c79ae52a 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #include <linux/slab.h> | 26 | #include <linux/slab.h> |
27 | #include <linux/mod_devicetable.h> | 27 | #include <linux/mod_devicetable.h> |
28 | #include <linux/spi/spi.h> | 28 | #include <linux/spi/spi.h> |
29 | #include <linux/of_spi.h> | ||
29 | 30 | ||
30 | 31 | ||
31 | /* SPI bustype and spi_master class are registered after board init code | 32 | /* SPI bustype and spi_master class are registered after board init code |
@@ -527,6 +528,10 @@ int spi_register_master(struct spi_master *master) | |||
527 | dynamic = 1; | 528 | dynamic = 1; |
528 | } | 529 | } |
529 | 530 | ||
531 | spin_lock_init(&master->bus_lock_spinlock); | ||
532 | mutex_init(&master->bus_lock_mutex); | ||
533 | master->bus_lock_flag = 0; | ||
534 | |||
530 | /* register the device, then userspace will see it. | 535 | /* register the device, then userspace will see it. |
531 | * registration fails if the bus ID is in use. | 536 | * registration fails if the bus ID is in use. |
532 | */ | 537 | */ |
@@ -540,6 +545,9 @@ int spi_register_master(struct spi_master *master) | |||
540 | /* populate children from any spi device tables */ | 545 | /* populate children from any spi device tables */ |
541 | scan_boardinfo(master); | 546 | scan_boardinfo(master); |
542 | status = 0; | 547 | status = 0; |
548 | |||
549 | /* Register devices from the device tree */ | ||
550 | of_register_spi_devices(master); | ||
543 | done: | 551 | done: |
544 | return status; | 552 | return status; |
545 | } | 553 | } |
@@ -666,6 +674,35 @@ int spi_setup(struct spi_device *spi) | |||
666 | } | 674 | } |
667 | EXPORT_SYMBOL_GPL(spi_setup); | 675 | EXPORT_SYMBOL_GPL(spi_setup); |
668 | 676 | ||
677 | static 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 | |||
669 | /** | 706 | /** |
670 | * spi_async - asynchronous SPI transfer | 707 | * spi_async - asynchronous SPI transfer |
671 | * @spi: device with which data will be exchanged | 708 | * @spi: device with which data will be exchanged |
@@ -698,33 +735,68 @@ EXPORT_SYMBOL_GPL(spi_setup); | |||
698 | int spi_async(struct spi_device *spi, struct spi_message *message) | 735 | int spi_async(struct spi_device *spi, struct spi_message *message) |
699 | { | 736 | { |
700 | struct spi_master *master = spi->master; | 737 | struct spi_master *master = spi->master; |
738 | int ret; | ||
739 | unsigned long flags; | ||
701 | 740 | ||
702 | /* Half-duplex links include original MicroWire, and ones with | 741 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
703 | * only one data pin like SPI_3WIRE (switches direction) or where | ||
704 | * either MOSI or MISO is missing. They can also be caused by | ||
705 | * software limitations. | ||
706 | */ | ||
707 | if ((master->flags & SPI_MASTER_HALF_DUPLEX) | ||
708 | || (spi->mode & SPI_3WIRE)) { | ||
709 | struct spi_transfer *xfer; | ||
710 | unsigned flags = master->flags; | ||
711 | 742 | ||
712 | list_for_each_entry(xfer, &message->transfers, transfer_list) { | 743 | if (master->bus_lock_flag) |
713 | if (xfer->rx_buf && xfer->tx_buf) | 744 | ret = -EBUSY; |
714 | return -EINVAL; | 745 | else |
715 | if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) | 746 | ret = __spi_async(spi, message); |
716 | return -EINVAL; | ||
717 | if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf) | ||
718 | return -EINVAL; | ||
719 | } | ||
720 | } | ||
721 | 747 | ||
722 | message->spi = spi; | 748 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
723 | message->status = -EINPROGRESS; | 749 | |
724 | return master->transfer(spi, message); | 750 | return ret; |
725 | } | 751 | } |
726 | EXPORT_SYMBOL_GPL(spi_async); | 752 | EXPORT_SYMBOL_GPL(spi_async); |
727 | 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 | */ | ||
783 | int 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 | } | ||
798 | EXPORT_SYMBOL_GPL(spi_async_locked); | ||
799 | |||
728 | 800 | ||
729 | /*-------------------------------------------------------------------------*/ | 801 | /*-------------------------------------------------------------------------*/ |
730 | 802 | ||
@@ -738,6 +810,32 @@ static void spi_complete(void *arg) | |||
738 | complete(arg); | 810 | complete(arg); |
739 | } | 811 | } |
740 | 812 | ||
813 | static 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 | |||
741 | /** | 839 | /** |
742 | * spi_sync - blocking/synchronous SPI data transfers | 840 | * spi_sync - blocking/synchronous SPI data transfers |
743 | * @spi: device with which data will be exchanged | 841 | * @spi: device with which data will be exchanged |
@@ -761,21 +859,86 @@ static void spi_complete(void *arg) | |||
761 | */ | 859 | */ |
762 | int spi_sync(struct spi_device *spi, struct spi_message *message) | 860 | int spi_sync(struct spi_device *spi, struct spi_message *message) |
763 | { | 861 | { |
764 | DECLARE_COMPLETION_ONSTACK(done); | 862 | return __spi_sync(spi, message, 0); |
765 | int status; | ||
766 | |||
767 | message->complete = spi_complete; | ||
768 | message->context = &done; | ||
769 | status = spi_async(spi, message); | ||
770 | if (status == 0) { | ||
771 | wait_for_completion(&done); | ||
772 | status = message->status; | ||
773 | } | ||
774 | message->context = NULL; | ||
775 | return status; | ||
776 | } | 863 | } |
777 | EXPORT_SYMBOL_GPL(spi_sync); | 864 | EXPORT_SYMBOL_GPL(spi_sync); |
778 | 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 | */ | ||
882 | int spi_sync_locked(struct spi_device *spi, struct spi_message *message) | ||
883 | { | ||
884 | return __spi_sync(spi, message, 1); | ||
885 | } | ||
886 | EXPORT_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 | */ | ||
903 | int 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 | } | ||
917 | EXPORT_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 | */ | ||
932 | int 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 | } | ||
940 | EXPORT_SYMBOL_GPL(spi_bus_unlock); | ||
941 | |||
779 | /* portable code must never pass more than 32 bytes */ | 942 | /* portable code must never pass more than 32 bytes */ |
780 | #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) | 943 | #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) |
781 | 944 | ||