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.c235
1 files changed, 214 insertions, 21 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index d0b28bba38be..ffc1a2ebc4ca 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);
255static int spi_drv_probe(struct device *dev) 257static 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)
269static int spi_drv_remove(struct device *dev) 270static 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
583static 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
631static 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
640static 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
724static 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,7 +754,6 @@ 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;
597 759
@@ -627,8 +789,9 @@ static int spi_transfer_one_message(struct spi_master *master,
627 &msg->transfers)) { 789 &msg->transfers)) {
628 keep_cs = true; 790 keep_cs = true;
629 } else { 791 } else {
630 cur_cs = !cur_cs; 792 spi_set_cs(msg->spi, false);
631 spi_set_cs(msg->spi, cur_cs); 793 udelay(10);
794 spi_set_cs(msg->spi, true);
632 } 795 }
633 } 796 }
634 797
@@ -686,6 +849,10 @@ static void spi_pump_messages(struct kthread_work *work)
686 } 849 }
687 master->busy = false; 850 master->busy = false;
688 spin_unlock_irqrestore(&master->queue_lock, flags); 851 spin_unlock_irqrestore(&master->queue_lock, flags);
852 kfree(master->dummy_rx);
853 master->dummy_rx = NULL;
854 kfree(master->dummy_tx);
855 master->dummy_tx = NULL;
689 if (master->unprepare_transfer_hardware && 856 if (master->unprepare_transfer_hardware &&
690 master->unprepare_transfer_hardware(master)) 857 master->unprepare_transfer_hardware(master))
691 dev_err(&master->dev, 858 dev_err(&master->dev,
@@ -752,6 +919,13 @@ static void spi_pump_messages(struct kthread_work *work)
752 master->cur_msg_prepared = true; 919 master->cur_msg_prepared = true;
753 } 920 }
754 921
922 ret = spi_map_msg(master, master->cur_msg);
923 if (ret) {
924 master->cur_msg->status = ret;
925 spi_finalize_current_message(master);
926 return;
927 }
928
755 ret = master->transfer_one_message(master, master->cur_msg); 929 ret = master->transfer_one_message(master, master->cur_msg);
756 if (ret) { 930 if (ret) {
757 dev_err(&master->dev, 931 dev_err(&master->dev,
@@ -839,6 +1013,8 @@ void spi_finalize_current_message(struct spi_master *master)
839 queue_kthread_work(&master->kworker, &master->pump_messages); 1013 queue_kthread_work(&master->kworker, &master->pump_messages);
840 spin_unlock_irqrestore(&master->queue_lock, flags); 1014 spin_unlock_irqrestore(&master->queue_lock, flags);
841 1015
1016 spi_unmap_msg(master, mesg);
1017
842 if (master->cur_msg_prepared && master->unprepare_message) { 1018 if (master->cur_msg_prepared && master->unprepare_message) {
843 ret = master->unprepare_message(master, mesg); 1019 ret = master->unprepare_message(master, mesg);
844 if (ret) { 1020 if (ret) {
@@ -892,7 +1068,7 @@ static int spi_stop_queue(struct spi_master *master)
892 */ 1068 */
893 while ((!list_empty(&master->queue) || master->busy) && limit--) { 1069 while ((!list_empty(&master->queue) || master->busy) && limit--) {
894 spin_unlock_irqrestore(&master->queue_lock, flags); 1070 spin_unlock_irqrestore(&master->queue_lock, flags);
895 msleep(10); 1071 usleep_range(10000, 11000);
896 spin_lock_irqsave(&master->queue_lock, flags); 1072 spin_lock_irqsave(&master->queue_lock, flags);
897 } 1073 }
898 1074
@@ -1372,6 +1548,8 @@ int spi_register_master(struct spi_master *master)
1372 mutex_init(&master->bus_lock_mutex); 1548 mutex_init(&master->bus_lock_mutex);
1373 master->bus_lock_flag = 0; 1549 master->bus_lock_flag = 0;
1374 init_completion(&master->xfer_completion); 1550 init_completion(&master->xfer_completion);
1551 if (!master->max_dma_len)
1552 master->max_dma_len = INT_MAX;
1375 1553
1376 /* register the device, then userspace will see it. 1554 /* register the device, then userspace will see it.
1377 * registration fails if the bus ID is in use. 1555 * registration fails if the bus ID is in use.
@@ -1597,6 +1775,9 @@ int spi_setup(struct spi_device *spi)
1597 if (!spi->bits_per_word) 1775 if (!spi->bits_per_word)
1598 spi->bits_per_word = 8; 1776 spi->bits_per_word = 8;
1599 1777
1778 if (!spi->max_speed_hz)
1779 spi->max_speed_hz = spi->master->max_speed_hz;
1780
1600 if (spi->master->setup) 1781 if (spi->master->setup)
1601 status = spi->master->setup(spi); 1782 status = spi->master->setup(spi);
1602 1783
@@ -1617,11 +1798,10 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
1617{ 1798{
1618 struct spi_master *master = spi->master; 1799 struct spi_master *master = spi->master;
1619 struct spi_transfer *xfer; 1800 struct spi_transfer *xfer;
1801 int w_size;
1620 1802
1621 if (list_empty(&message->transfers)) 1803 if (list_empty(&message->transfers))
1622 return -EINVAL; 1804 return -EINVAL;
1623 if (!message->complete)
1624 return -EINVAL;
1625 1805
1626 /* Half-duplex links include original MicroWire, and ones with 1806 /* Half-duplex links include original MicroWire, and ones with
1627 * only one data pin like SPI_3WIRE (switches direction) or where 1807 * only one data pin like SPI_3WIRE (switches direction) or where
@@ -1652,12 +1832,13 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
1652 message->frame_length += xfer->len; 1832 message->frame_length += xfer->len;
1653 if (!xfer->bits_per_word) 1833 if (!xfer->bits_per_word)
1654 xfer->bits_per_word = spi->bits_per_word; 1834 xfer->bits_per_word = spi->bits_per_word;
1655 if (!xfer->speed_hz) { 1835
1836 if (!xfer->speed_hz)
1656 xfer->speed_hz = spi->max_speed_hz; 1837 xfer->speed_hz = spi->max_speed_hz;
1657 if (master->max_speed_hz && 1838
1658 xfer->speed_hz > master->max_speed_hz) 1839 if (master->max_speed_hz &&
1659 xfer->speed_hz = master->max_speed_hz; 1840 xfer->speed_hz > master->max_speed_hz)
1660 } 1841 xfer->speed_hz = master->max_speed_hz;
1661 1842
1662 if (master->bits_per_word_mask) { 1843 if (master->bits_per_word_mask) {
1663 /* Only 32 bits fit in the mask */ 1844 /* Only 32 bits fit in the mask */
@@ -1668,12 +1849,24 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
1668 return -EINVAL; 1849 return -EINVAL;
1669 } 1850 }
1670 1851
1852 /*
1853 * SPI transfer length should be multiple of SPI word size
1854 * where SPI word size should be power-of-two multiple
1855 */
1856 if (xfer->bits_per_word <= 8)
1857 w_size = 1;
1858 else if (xfer->bits_per_word <= 16)
1859 w_size = 2;
1860 else
1861 w_size = 4;
1862
1863 /* No partial transfers accepted */
1864 if (xfer->len % w_size)
1865 return -EINVAL;
1866
1671 if (xfer->speed_hz && master->min_speed_hz && 1867 if (xfer->speed_hz && master->min_speed_hz &&
1672 xfer->speed_hz < master->min_speed_hz) 1868 xfer->speed_hz < master->min_speed_hz)
1673 return -EINVAL; 1869 return -EINVAL;
1674 if (xfer->speed_hz && master->max_speed_hz &&
1675 xfer->speed_hz > master->max_speed_hz)
1676 return -EINVAL;
1677 1870
1678 if (xfer->tx_buf && !xfer->tx_nbits) 1871 if (xfer->tx_buf && !xfer->tx_nbits)
1679 xfer->tx_nbits = SPI_NBITS_SINGLE; 1872 xfer->tx_nbits = SPI_NBITS_SINGLE;