diff options
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r-- | drivers/spi/spi.c | 251 |
1 files changed, 226 insertions, 25 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 23756b0f9036..4eb9bf02996c 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c | |||
@@ -24,6 +24,8 @@ | |||
24 | #include <linux/device.h> | 24 | #include <linux/device.h> |
25 | #include <linux/init.h> | 25 | #include <linux/init.h> |
26 | #include <linux/cache.h> | 26 | #include <linux/cache.h> |
27 | #include <linux/dma-mapping.h> | ||
28 | #include <linux/dmaengine.h> | ||
27 | #include <linux/mutex.h> | 29 | #include <linux/mutex.h> |
28 | #include <linux/of_device.h> | 30 | #include <linux/of_device.h> |
29 | #include <linux/of_irq.h> | 31 | #include <linux/of_irq.h> |
@@ -255,13 +257,12 @@ EXPORT_SYMBOL_GPL(spi_bus_type); | |||
255 | static int spi_drv_probe(struct device *dev) | 257 | static int spi_drv_probe(struct device *dev) |
256 | { | 258 | { |
257 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); | 259 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
258 | struct spi_device *spi = to_spi_device(dev); | ||
259 | int ret; | 260 | int ret; |
260 | 261 | ||
261 | acpi_dev_pm_attach(&spi->dev, true); | 262 | acpi_dev_pm_attach(dev, true); |
262 | ret = sdrv->probe(spi); | 263 | ret = sdrv->probe(to_spi_device(dev)); |
263 | if (ret) | 264 | if (ret) |
264 | acpi_dev_pm_detach(&spi->dev, true); | 265 | acpi_dev_pm_detach(dev, true); |
265 | 266 | ||
266 | return ret; | 267 | return ret; |
267 | } | 268 | } |
@@ -269,11 +270,10 @@ static int spi_drv_probe(struct device *dev) | |||
269 | static int spi_drv_remove(struct device *dev) | 270 | static int spi_drv_remove(struct device *dev) |
270 | { | 271 | { |
271 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); | 272 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
272 | struct spi_device *spi = to_spi_device(dev); | ||
273 | int ret; | 273 | int ret; |
274 | 274 | ||
275 | ret = sdrv->remove(spi); | 275 | ret = sdrv->remove(to_spi_device(dev)); |
276 | acpi_dev_pm_detach(&spi->dev, true); | 276 | acpi_dev_pm_detach(dev, true); |
277 | 277 | ||
278 | return ret; | 278 | return ret; |
279 | } | 279 | } |
@@ -580,6 +580,169 @@ static void spi_set_cs(struct spi_device *spi, bool enable) | |||
580 | spi->master->set_cs(spi, !enable); | 580 | spi->master->set_cs(spi, !enable); |
581 | } | 581 | } |
582 | 582 | ||
583 | static int spi_map_buf(struct spi_master *master, struct device *dev, | ||
584 | struct sg_table *sgt, void *buf, size_t len, | ||
585 | enum dma_data_direction dir) | ||
586 | { | ||
587 | const bool vmalloced_buf = is_vmalloc_addr(buf); | ||
588 | const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len; | ||
589 | const int sgs = DIV_ROUND_UP(len, desc_len); | ||
590 | struct page *vm_page; | ||
591 | void *sg_buf; | ||
592 | size_t min; | ||
593 | int i, ret; | ||
594 | |||
595 | ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); | ||
596 | if (ret != 0) | ||
597 | return ret; | ||
598 | |||
599 | for (i = 0; i < sgs; i++) { | ||
600 | min = min_t(size_t, len, desc_len); | ||
601 | |||
602 | if (vmalloced_buf) { | ||
603 | vm_page = vmalloc_to_page(buf); | ||
604 | if (!vm_page) { | ||
605 | sg_free_table(sgt); | ||
606 | return -ENOMEM; | ||
607 | } | ||
608 | sg_buf = page_address(vm_page) + | ||
609 | ((size_t)buf & ~PAGE_MASK); | ||
610 | } else { | ||
611 | sg_buf = buf; | ||
612 | } | ||
613 | |||
614 | sg_set_buf(&sgt->sgl[i], sg_buf, min); | ||
615 | |||
616 | buf += min; | ||
617 | len -= min; | ||
618 | } | ||
619 | |||
620 | ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); | ||
621 | if (ret < 0) { | ||
622 | sg_free_table(sgt); | ||
623 | return ret; | ||
624 | } | ||
625 | |||
626 | sgt->nents = ret; | ||
627 | |||
628 | return 0; | ||
629 | } | ||
630 | |||
631 | static void spi_unmap_buf(struct spi_master *master, struct device *dev, | ||
632 | struct sg_table *sgt, enum dma_data_direction dir) | ||
633 | { | ||
634 | if (sgt->orig_nents) { | ||
635 | dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); | ||
636 | sg_free_table(sgt); | ||
637 | } | ||
638 | } | ||
639 | |||
640 | static int spi_map_msg(struct spi_master *master, struct spi_message *msg) | ||
641 | { | ||
642 | struct device *tx_dev, *rx_dev; | ||
643 | struct spi_transfer *xfer; | ||
644 | void *tmp; | ||
645 | unsigned int max_tx, max_rx; | ||
646 | int ret; | ||
647 | |||
648 | if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) { | ||
649 | max_tx = 0; | ||
650 | max_rx = 0; | ||
651 | |||
652 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { | ||
653 | if ((master->flags & SPI_MASTER_MUST_TX) && | ||
654 | !xfer->tx_buf) | ||
655 | max_tx = max(xfer->len, max_tx); | ||
656 | if ((master->flags & SPI_MASTER_MUST_RX) && | ||
657 | !xfer->rx_buf) | ||
658 | max_rx = max(xfer->len, max_rx); | ||
659 | } | ||
660 | |||
661 | if (max_tx) { | ||
662 | tmp = krealloc(master->dummy_tx, max_tx, | ||
663 | GFP_KERNEL | GFP_DMA); | ||
664 | if (!tmp) | ||
665 | return -ENOMEM; | ||
666 | master->dummy_tx = tmp; | ||
667 | memset(tmp, 0, max_tx); | ||
668 | } | ||
669 | |||
670 | if (max_rx) { | ||
671 | tmp = krealloc(master->dummy_rx, max_rx, | ||
672 | GFP_KERNEL | GFP_DMA); | ||
673 | if (!tmp) | ||
674 | return -ENOMEM; | ||
675 | master->dummy_rx = tmp; | ||
676 | } | ||
677 | |||
678 | if (max_tx || max_rx) { | ||
679 | list_for_each_entry(xfer, &msg->transfers, | ||
680 | transfer_list) { | ||
681 | if (!xfer->tx_buf) | ||
682 | xfer->tx_buf = master->dummy_tx; | ||
683 | if (!xfer->rx_buf) | ||
684 | xfer->rx_buf = master->dummy_rx; | ||
685 | } | ||
686 | } | ||
687 | } | ||
688 | |||
689 | if (!master->can_dma) | ||
690 | return 0; | ||
691 | |||
692 | tx_dev = &master->dma_tx->dev->device; | ||
693 | rx_dev = &master->dma_rx->dev->device; | ||
694 | |||
695 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { | ||
696 | if (!master->can_dma(master, msg->spi, xfer)) | ||
697 | continue; | ||
698 | |||
699 | if (xfer->tx_buf != NULL) { | ||
700 | ret = spi_map_buf(master, tx_dev, &xfer->tx_sg, | ||
701 | (void *)xfer->tx_buf, xfer->len, | ||
702 | DMA_TO_DEVICE); | ||
703 | if (ret != 0) | ||
704 | return ret; | ||
705 | } | ||
706 | |||
707 | if (xfer->rx_buf != NULL) { | ||
708 | ret = spi_map_buf(master, rx_dev, &xfer->rx_sg, | ||
709 | xfer->rx_buf, xfer->len, | ||
710 | DMA_FROM_DEVICE); | ||
711 | if (ret != 0) { | ||
712 | spi_unmap_buf(master, tx_dev, &xfer->tx_sg, | ||
713 | DMA_TO_DEVICE); | ||
714 | return ret; | ||
715 | } | ||
716 | } | ||
717 | } | ||
718 | |||
719 | master->cur_msg_mapped = true; | ||
720 | |||
721 | return 0; | ||
722 | } | ||
723 | |||
724 | static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg) | ||
725 | { | ||
726 | struct spi_transfer *xfer; | ||
727 | struct device *tx_dev, *rx_dev; | ||
728 | |||
729 | if (!master->cur_msg_mapped || !master->can_dma) | ||
730 | return 0; | ||
731 | |||
732 | tx_dev = &master->dma_tx->dev->device; | ||
733 | rx_dev = &master->dma_rx->dev->device; | ||
734 | |||
735 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { | ||
736 | if (!master->can_dma(master, msg->spi, xfer)) | ||
737 | continue; | ||
738 | |||
739 | spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); | ||
740 | spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); | ||
741 | } | ||
742 | |||
743 | return 0; | ||
744 | } | ||
745 | |||
583 | /* | 746 | /* |
584 | * spi_transfer_one_message - Default implementation of transfer_one_message() | 747 | * spi_transfer_one_message - Default implementation of transfer_one_message() |
585 | * | 748 | * |
@@ -591,9 +754,9 @@ static int spi_transfer_one_message(struct spi_master *master, | |||
591 | struct spi_message *msg) | 754 | struct spi_message *msg) |
592 | { | 755 | { |
593 | struct spi_transfer *xfer; | 756 | struct spi_transfer *xfer; |
594 | bool cur_cs = true; | ||
595 | bool keep_cs = false; | 757 | bool keep_cs = false; |
596 | int ret = 0; | 758 | int ret = 0; |
759 | int ms = 1; | ||
597 | 760 | ||
598 | spi_set_cs(msg->spi, true); | 761 | spi_set_cs(msg->spi, true); |
599 | 762 | ||
@@ -611,7 +774,16 @@ static int spi_transfer_one_message(struct spi_master *master, | |||
611 | 774 | ||
612 | if (ret > 0) { | 775 | if (ret > 0) { |
613 | ret = 0; | 776 | ret = 0; |
614 | wait_for_completion(&master->xfer_completion); | 777 | ms = xfer->len * 8 * 1000 / xfer->speed_hz; |
778 | ms += 10; /* some tolerance */ | ||
779 | |||
780 | ms = wait_for_completion_timeout(&master->xfer_completion, | ||
781 | msecs_to_jiffies(ms)); | ||
782 | } | ||
783 | |||
784 | if (ms == 0) { | ||
785 | dev_err(&msg->spi->dev, "SPI transfer timed out\n"); | ||
786 | msg->status = -ETIMEDOUT; | ||
615 | } | 787 | } |
616 | 788 | ||
617 | trace_spi_transfer_stop(msg, xfer); | 789 | trace_spi_transfer_stop(msg, xfer); |
@@ -627,8 +799,9 @@ static int spi_transfer_one_message(struct spi_master *master, | |||
627 | &msg->transfers)) { | 799 | &msg->transfers)) { |
628 | keep_cs = true; | 800 | keep_cs = true; |
629 | } else { | 801 | } else { |
630 | cur_cs = !cur_cs; | 802 | spi_set_cs(msg->spi, false); |
631 | spi_set_cs(msg->spi, cur_cs); | 803 | udelay(10); |
804 | spi_set_cs(msg->spi, true); | ||
632 | } | 805 | } |
633 | } | 806 | } |
634 | 807 | ||
@@ -686,6 +859,10 @@ static void spi_pump_messages(struct kthread_work *work) | |||
686 | } | 859 | } |
687 | master->busy = false; | 860 | master->busy = false; |
688 | spin_unlock_irqrestore(&master->queue_lock, flags); | 861 | spin_unlock_irqrestore(&master->queue_lock, flags); |
862 | kfree(master->dummy_rx); | ||
863 | master->dummy_rx = NULL; | ||
864 | kfree(master->dummy_tx); | ||
865 | master->dummy_tx = NULL; | ||
689 | if (master->unprepare_transfer_hardware && | 866 | if (master->unprepare_transfer_hardware && |
690 | master->unprepare_transfer_hardware(master)) | 867 | master->unprepare_transfer_hardware(master)) |
691 | dev_err(&master->dev, | 868 | dev_err(&master->dev, |
@@ -752,14 +929,19 @@ static void spi_pump_messages(struct kthread_work *work) | |||
752 | master->cur_msg_prepared = true; | 929 | master->cur_msg_prepared = true; |
753 | } | 930 | } |
754 | 931 | ||
755 | ret = master->transfer_one_message(master, master->cur_msg); | 932 | ret = spi_map_msg(master, master->cur_msg); |
756 | if (ret) { | 933 | if (ret) { |
757 | dev_err(&master->dev, | ||
758 | "failed to transfer one message from queue: %d\n", ret); | ||
759 | master->cur_msg->status = ret; | 934 | master->cur_msg->status = ret; |
760 | spi_finalize_current_message(master); | 935 | spi_finalize_current_message(master); |
761 | return; | 936 | return; |
762 | } | 937 | } |
938 | |||
939 | ret = master->transfer_one_message(master, master->cur_msg); | ||
940 | if (ret) { | ||
941 | dev_err(&master->dev, | ||
942 | "failed to transfer one message from queue\n"); | ||
943 | return; | ||
944 | } | ||
763 | } | 945 | } |
764 | 946 | ||
765 | static int spi_init_queue(struct spi_master *master) | 947 | static int spi_init_queue(struct spi_master *master) |
@@ -841,6 +1023,8 @@ void spi_finalize_current_message(struct spi_master *master) | |||
841 | queue_kthread_work(&master->kworker, &master->pump_messages); | 1023 | queue_kthread_work(&master->kworker, &master->pump_messages); |
842 | spin_unlock_irqrestore(&master->queue_lock, flags); | 1024 | spin_unlock_irqrestore(&master->queue_lock, flags); |
843 | 1025 | ||
1026 | spi_unmap_msg(master, mesg); | ||
1027 | |||
844 | if (master->cur_msg_prepared && master->unprepare_message) { | 1028 | if (master->cur_msg_prepared && master->unprepare_message) { |
845 | ret = master->unprepare_message(master, mesg); | 1029 | ret = master->unprepare_message(master, mesg); |
846 | if (ret) { | 1030 | if (ret) { |
@@ -894,7 +1078,7 @@ static int spi_stop_queue(struct spi_master *master) | |||
894 | */ | 1078 | */ |
895 | while ((!list_empty(&master->queue) || master->busy) && limit--) { | 1079 | while ((!list_empty(&master->queue) || master->busy) && limit--) { |
896 | spin_unlock_irqrestore(&master->queue_lock, flags); | 1080 | spin_unlock_irqrestore(&master->queue_lock, flags); |
897 | msleep(10); | 1081 | usleep_range(10000, 11000); |
898 | spin_lock_irqsave(&master->queue_lock, flags); | 1082 | spin_lock_irqsave(&master->queue_lock, flags); |
899 | } | 1083 | } |
900 | 1084 | ||
@@ -1374,6 +1558,8 @@ int spi_register_master(struct spi_master *master) | |||
1374 | mutex_init(&master->bus_lock_mutex); | 1558 | mutex_init(&master->bus_lock_mutex); |
1375 | master->bus_lock_flag = 0; | 1559 | master->bus_lock_flag = 0; |
1376 | init_completion(&master->xfer_completion); | 1560 | init_completion(&master->xfer_completion); |
1561 | if (!master->max_dma_len) | ||
1562 | master->max_dma_len = INT_MAX; | ||
1377 | 1563 | ||
1378 | /* register the device, then userspace will see it. | 1564 | /* register the device, then userspace will see it. |
1379 | * registration fails if the bus ID is in use. | 1565 | * registration fails if the bus ID is in use. |
@@ -1599,6 +1785,9 @@ int spi_setup(struct spi_device *spi) | |||
1599 | if (!spi->bits_per_word) | 1785 | if (!spi->bits_per_word) |
1600 | spi->bits_per_word = 8; | 1786 | spi->bits_per_word = 8; |
1601 | 1787 | ||
1788 | if (!spi->max_speed_hz) | ||
1789 | spi->max_speed_hz = spi->master->max_speed_hz; | ||
1790 | |||
1602 | if (spi->master->setup) | 1791 | if (spi->master->setup) |
1603 | status = spi->master->setup(spi); | 1792 | status = spi->master->setup(spi); |
1604 | 1793 | ||
@@ -1619,11 +1808,10 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message) | |||
1619 | { | 1808 | { |
1620 | struct spi_master *master = spi->master; | 1809 | struct spi_master *master = spi->master; |
1621 | struct spi_transfer *xfer; | 1810 | struct spi_transfer *xfer; |
1811 | int w_size; | ||
1622 | 1812 | ||
1623 | if (list_empty(&message->transfers)) | 1813 | if (list_empty(&message->transfers)) |
1624 | return -EINVAL; | 1814 | return -EINVAL; |
1625 | if (!message->complete) | ||
1626 | return -EINVAL; | ||
1627 | 1815 | ||
1628 | /* Half-duplex links include original MicroWire, and ones with | 1816 | /* Half-duplex links include original MicroWire, and ones with |
1629 | * only one data pin like SPI_3WIRE (switches direction) or where | 1817 | * only one data pin like SPI_3WIRE (switches direction) or where |
@@ -1654,12 +1842,13 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message) | |||
1654 | message->frame_length += xfer->len; | 1842 | message->frame_length += xfer->len; |
1655 | if (!xfer->bits_per_word) | 1843 | if (!xfer->bits_per_word) |
1656 | xfer->bits_per_word = spi->bits_per_word; | 1844 | xfer->bits_per_word = spi->bits_per_word; |
1657 | if (!xfer->speed_hz) { | 1845 | |
1846 | if (!xfer->speed_hz) | ||
1658 | xfer->speed_hz = spi->max_speed_hz; | 1847 | xfer->speed_hz = spi->max_speed_hz; |
1659 | if (master->max_speed_hz && | 1848 | |
1660 | xfer->speed_hz > master->max_speed_hz) | 1849 | if (master->max_speed_hz && |
1661 | xfer->speed_hz = master->max_speed_hz; | 1850 | xfer->speed_hz > master->max_speed_hz) |
1662 | } | 1851 | xfer->speed_hz = master->max_speed_hz; |
1663 | 1852 | ||
1664 | if (master->bits_per_word_mask) { | 1853 | if (master->bits_per_word_mask) { |
1665 | /* Only 32 bits fit in the mask */ | 1854 | /* Only 32 bits fit in the mask */ |
@@ -1670,12 +1859,24 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message) | |||
1670 | return -EINVAL; | 1859 | return -EINVAL; |
1671 | } | 1860 | } |
1672 | 1861 | ||
1862 | /* | ||
1863 | * SPI transfer length should be multiple of SPI word size | ||
1864 | * where SPI word size should be power-of-two multiple | ||
1865 | */ | ||
1866 | if (xfer->bits_per_word <= 8) | ||
1867 | w_size = 1; | ||
1868 | else if (xfer->bits_per_word <= 16) | ||
1869 | w_size = 2; | ||
1870 | else | ||
1871 | w_size = 4; | ||
1872 | |||
1873 | /* No partial transfers accepted */ | ||
1874 | if (xfer->len % w_size) | ||
1875 | return -EINVAL; | ||
1876 | |||
1673 | if (xfer->speed_hz && master->min_speed_hz && | 1877 | if (xfer->speed_hz && master->min_speed_hz && |
1674 | xfer->speed_hz < master->min_speed_hz) | 1878 | xfer->speed_hz < master->min_speed_hz) |
1675 | return -EINVAL; | 1879 | return -EINVAL; |
1676 | if (xfer->speed_hz && master->max_speed_hz && | ||
1677 | xfer->speed_hz > master->max_speed_hz) | ||
1678 | return -EINVAL; | ||
1679 | 1880 | ||
1680 | if (xfer->tx_buf && !xfer->tx_nbits) | 1881 | if (xfer->tx_buf && !xfer->tx_nbits) |
1681 | xfer->tx_nbits = SPI_NBITS_SINGLE; | 1882 | xfer->tx_nbits = SPI_NBITS_SINGLE; |