/* 神奇的分割线 */
626/*-------------------------------------------------------------------------*/
627
628/* Core methods for SPI master protocol drivers. Some of the
629 * other core methods are currently defined as inline functions.
630 */
631
632/**
633 * spi_setup - setup SPI mode and clock rate
634 * @spi: the device whose settings are being modified
635 * Context: can sleep, and no requests are queued to the device
636 *
637 * SPI protocol drivers may need to update the transfer mode if the
638 * device doesn't work with its default. They may likewise need
639 * to update clock rates or word sizes from initial values. This function
640 * changes those settings, and must be called from a context that can sleep.
641 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
642 * effect the next time the device is selected and data is transferred to
643 * or from it. When this function returns, the spi device is deselected.
644 *
645 * Note that this call will fail if the protocol driver specifies an option
646 * that the underlying controller or its driver does not support. For
647 * example, not all hardware supports wire transfers using nine bit words,
648 * LSB-first wire encoding, or active-high chipselects.
649 */
650int spi_setup(struct spi_device *spi)
651{
652 unsigned bad_bits;
653 int status;
654
655 /* help drivers fail *cleanly* when they need options
656 * that aren't supported with their current master
657 */
/* 显示驱动不支持的模式 */
658 bad_bits = spi->mode & ~spi->master->mode_bits;
659 if (bad_bits) {
660 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
661 bad_bits);
662 return -EINVAL;
663 }
664
/* 设置传输位数 */
665 if (!spi->bits_per_word)
666 spi->bits_per_word = 8;
667
/* 调用主机驱动的设置方法设置驱动 */
668 status = spi->master->setup(spi);
669
670 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
671 "%u bits/w, %u Hz max --> %d\n",
672 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
673 (spi->mode & SPI_CS_HIGH) ? "cs_high," : "",
674 (spi->mode & SPI_LSB_FIRST) ? "lsb," : "",
675 (spi->mode & SPI_3WIRE) ? "3wire," : "",
676 (spi->mode & SPI_LOOP) ? "loopback," : "",
677 spi->bits_per_word, spi->max_speed_hz,
678 status);
679
680 return status;
681}
682EXPORT_SYMBOL_GPL(spi_setup);
683
684static int __spi_async(struct spi_device *spi, struct spi_message *message)
685{
686 struct spi_master *master = spi->master;
687
688 /* Half-duplex links include original MicroWire, and ones with
689 * only one data pin like SPI_3WIRE (switches direction) or where
690 * either MOSI or MISO is missing. They can also be caused by
691 * software limitations.
692 */
/* 半双工模式 或者 3线SPI */
693 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
694 || (spi->mode & SPI_3WIRE)) {
/* struct spi_transfer 读写缓存 */
695 struct spi_transfer *xfer;
696 unsigned flags = master->flags;
697
/* 遍历message->transfers链表判断缓存 */
698 list_for_each_entry(xfer, &message->transfers, transfer_list) {
/* 半双工 */
699 if (xfer->rx_buf && xfer->tx_buf)
700 return -EINVAL;
/* 当前模式没有发送,而有发送缓存 */
701 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
702 return -EINVAL;
/* 当前模式没有接收,而有接收缓存 */
703 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
704 return -EINVAL;
705 }
706 }
707
/* 该消息使用的设备为SPI */
708 message->spi = spi;
/* 消息正在处理 */
709 message->status = -EINPROGRESS;
/* 调用主机驱动的传输方法 */
710 return master->transfer(spi, message);
711}
712
713/**
714 * spi_async - asynchronous SPI transfer
715 * @spi: device with which data will be exchanged
716 * @message: describes the data transfers, including completion callback
717 * Context: any (irqs may be blocked, etc)
718 *
719 * This call may be used in_irq and other contexts which can't sleep,
720 * as well as from task contexts which can sleep.
721 *
722 * The completion callback is invoked in a context which can't sleep.
723 * Before that invocation, the value of message->status is undefined.
724 * When the callback is issued, message->status holds either zero (to
725 * indicate complete success) or a negative error code. After that
726 * callback returns, the driver which issued the transfer request may
727 * deallocate the associated memory; it's no longer in use by any SPI
728 * core or controller driver code.
729 *
730 * Note that although all messages to a spi_device are handled in
731 * FIFO order, messages may go to different devices in other orders.
732 * Some device might be higher priority, or have various "hard" access
733 * time requirements, for example.
734 *
735 * On detection of any fault during the transfer, processing of
736 * the entire message is aborted, and the device is deselected.
737 * Until returning from the associated message completion callback,
738 * no other spi_message queued to that device will be processed.
739 * (This rule applies equally to all the synchronous transfer calls,
740 * which are wrappers around this core asynchronous primitive.)
741 */
742int spi_async(struct spi_device *spi, struct spi_message *message)
743{
744 struct spi_master *master = spi->master;
745 int ret;
746 unsigned long flags;
747
/* 禁止终端,同时请求持有自旋锁 */
748 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
749
750 if (master->bus_lock_flag)
751 ret = -EBUSY;
752 else
/* 异步传输消息 */
753 ret = __spi_async(spi, message);
754
/* 恢复终端,解锁自旋锁 */
755 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
756
757 return ret;
758}
759EXPORT_SYMBOL_GPL(spi_async);
760
761/**
762 * spi_async_locked - version of spi_async with exclusive bus usage
763 * @spi: device with which data will be exchanged
764 * @message: describes the data transfers, including completion callback
765 * Context: any (irqs may be blocked, etc)
766 *
767 * This call may be used in_irq and other contexts which can't sleep,
768 * as well as from task contexts which can sleep.
769 *
770 * The completion callback is invoked in a context which can't sleep.
771 * Before that invocation, the value of message->status is undefined.
772 * When the callback is issued, message->status holds either zero (to
773 * indicate complete success) or a negative error code. After that
774 * callback returns, the driver which issued the transfer request may
775 * deallocate the associated memory; it's no longer in use by any SPI
776 * core or controller driver code.
777 *
778 * Note that although all messages to a spi_device are handled in
779 * FIFO order, messages may go to different devices in other orders.
780 * Some device might be higher priority, or have various "hard" access
781 * time requirements, for example.
782 *
783 * On detection of any fault during the transfer, processing of
784 * the entire message is aborted, and the device is deselected.
785 * Until returning from the associated message completion callback,
786 * no other spi_message queued to that device will be processed.
787 * (This rule applies equally to all the synchronous transfer calls,
788 * which are wrappers around this core asynchronous primitive.)
789 */
/* 另一个版本的spi_async,不包括使用总线 */
790int spi_async_locked(struct spi_device *spi, struct spi_message *message)
791{
792 struct spi_master *master = spi->master;
793 int ret;
794 unsigned long flags;
795
796 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
797
798 ret = __spi_async(spi, message);
799
800 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
801
802 return ret;
803
804}
805EXPORT_SYMBOL_GPL(spi_async_locked);
806
807
808/*-------------------------------------------------------------------------*/