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.c249
1 files changed, 225 insertions, 24 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 63613a96233c..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>
@@ -58,6 +60,11 @@ static ssize_t
58modalias_show(struct device *dev, struct device_attribute *a, char *buf) 60modalias_show(struct device *dev, struct device_attribute *a, char *buf)
59{ 61{
60 const struct spi_device *spi = to_spi_device(dev); 62 const struct spi_device *spi = to_spi_device(dev);
63 int len;
64
65 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
66 if (len != -ENODEV)
67 return len;
61 68
62 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 69 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
63} 70}
@@ -114,6 +121,11 @@ static int spi_match_device(struct device *dev, struct device_driver *drv)
114static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 121static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
115{ 122{
116 const struct spi_device *spi = to_spi_device(dev); 123 const struct spi_device *spi = to_spi_device(dev);
124 int rc;
125
126 rc = acpi_device_uevent_modalias(dev, env);
127 if (rc != -ENODEV)
128 return rc;
117 129
118 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 130 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
119 return 0; 131 return 0;
@@ -245,13 +257,12 @@ EXPORT_SYMBOL_GPL(spi_bus_type);
245static int spi_drv_probe(struct device *dev) 257static int spi_drv_probe(struct device *dev)
246{ 258{
247 const struct spi_driver *sdrv = to_spi_driver(dev->driver); 259 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
248 struct spi_device *spi = to_spi_device(dev);
249 int ret; 260 int ret;
250 261
251 acpi_dev_pm_attach(&spi->dev, true); 262 acpi_dev_pm_attach(dev, true);
252 ret = sdrv->probe(spi); 263 ret = sdrv->probe(to_spi_device(dev));
253 if (ret) 264 if (ret)
254 acpi_dev_pm_detach(&spi->dev, true); 265 acpi_dev_pm_detach(dev, true);
255 266
256 return ret; 267 return ret;
257} 268}
@@ -259,11 +270,10 @@ static int spi_drv_probe(struct device *dev)
259static int spi_drv_remove(struct device *dev) 270static int spi_drv_remove(struct device *dev)
260{ 271{
261 const struct spi_driver *sdrv = to_spi_driver(dev->driver); 272 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
262 struct spi_device *spi = to_spi_device(dev);
263 int ret; 273 int ret;
264 274
265 ret = sdrv->remove(spi); 275 ret = sdrv->remove(to_spi_device(dev));
266 acpi_dev_pm_detach(&spi->dev, true); 276 acpi_dev_pm_detach(dev, true);
267 277
268 return ret; 278 return ret;
269} 279}
@@ -570,6 +580,169 @@ static void spi_set_cs(struct spi_device *spi, bool enable)
570 spi->master->set_cs(spi, !enable); 580 spi->master->set_cs(spi, !enable);
571} 581}
572 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
573/* 746/*
574 * spi_transfer_one_message - Default implementation of transfer_one_message() 747 * spi_transfer_one_message - Default implementation of transfer_one_message()
575 * 748 *
@@ -581,7 +754,6 @@ static int spi_transfer_one_message(struct spi_master *master,
581 struct spi_message *msg) 754 struct spi_message *msg)
582{ 755{
583 struct spi_transfer *xfer; 756 struct spi_transfer *xfer;
584 bool cur_cs = true;
585 bool keep_cs = false; 757 bool keep_cs = false;
586 int ret = 0; 758 int ret = 0;
587 759
@@ -617,8 +789,9 @@ static int spi_transfer_one_message(struct spi_master *master,
617 &msg->transfers)) { 789 &msg->transfers)) {
618 keep_cs = true; 790 keep_cs = true;
619 } else { 791 } else {
620 cur_cs = !cur_cs; 792 spi_set_cs(msg->spi, false);
621 spi_set_cs(msg->spi, cur_cs); 793 udelay(10);
794 spi_set_cs(msg->spi, true);
622 } 795 }
623 } 796 }
624 797
@@ -676,6 +849,10 @@ static void spi_pump_messages(struct kthread_work *work)
676 } 849 }
677 master->busy = false; 850 master->busy = false;
678 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;
679 if (master->unprepare_transfer_hardware && 856 if (master->unprepare_transfer_hardware &&
680 master->unprepare_transfer_hardware(master)) 857 master->unprepare_transfer_hardware(master))
681 dev_err(&master->dev, 858 dev_err(&master->dev,
@@ -742,14 +919,19 @@ static void spi_pump_messages(struct kthread_work *work)
742 master->cur_msg_prepared = true; 919 master->cur_msg_prepared = true;
743 } 920 }
744 921
745 ret = master->transfer_one_message(master, master->cur_msg); 922 ret = spi_map_msg(master, master->cur_msg);
746 if (ret) { 923 if (ret) {
747 dev_err(&master->dev,
748 "failed to transfer one message from queue: %d\n", ret);
749 master->cur_msg->status = ret; 924 master->cur_msg->status = ret;
750 spi_finalize_current_message(master); 925 spi_finalize_current_message(master);
751 return; 926 return;
752 } 927 }
928
929 ret = master->transfer_one_message(master, master->cur_msg);
930 if (ret) {
931 dev_err(&master->dev,
932 "failed to transfer one message from queue\n");
933 return;
934 }
753} 935}
754 936
755static int spi_init_queue(struct spi_master *master) 937static int spi_init_queue(struct spi_master *master)
@@ -831,6 +1013,8 @@ void spi_finalize_current_message(struct spi_master *master)
831 queue_kthread_work(&master->kworker, &master->pump_messages); 1013 queue_kthread_work(&master->kworker, &master->pump_messages);
832 spin_unlock_irqrestore(&master->queue_lock, flags); 1014 spin_unlock_irqrestore(&master->queue_lock, flags);
833 1015
1016 spi_unmap_msg(master, mesg);
1017
834 if (master->cur_msg_prepared && master->unprepare_message) { 1018 if (master->cur_msg_prepared && master->unprepare_message) {
835 ret = master->unprepare_message(master, mesg); 1019 ret = master->unprepare_message(master, mesg);
836 if (ret) { 1020 if (ret) {
@@ -884,7 +1068,7 @@ static int spi_stop_queue(struct spi_master *master)
884 */ 1068 */
885 while ((!list_empty(&master->queue) || master->busy) && limit--) { 1069 while ((!list_empty(&master->queue) || master->busy) && limit--) {
886 spin_unlock_irqrestore(&master->queue_lock, flags); 1070 spin_unlock_irqrestore(&master->queue_lock, flags);
887 msleep(10); 1071 usleep_range(10000, 11000);
888 spin_lock_irqsave(&master->queue_lock, flags); 1072 spin_lock_irqsave(&master->queue_lock, flags);
889 } 1073 }
890 1074
@@ -1364,6 +1548,8 @@ int spi_register_master(struct spi_master *master)
1364 mutex_init(&master->bus_lock_mutex); 1548 mutex_init(&master->bus_lock_mutex);
1365 master->bus_lock_flag = 0; 1549 master->bus_lock_flag = 0;
1366 init_completion(&master->xfer_completion); 1550 init_completion(&master->xfer_completion);
1551 if (!master->max_dma_len)
1552 master->max_dma_len = INT_MAX;
1367 1553
1368 /* register the device, then userspace will see it. 1554 /* register the device, then userspace will see it.
1369 * registration fails if the bus ID is in use. 1555 * registration fails if the bus ID is in use.
@@ -1589,6 +1775,9 @@ int spi_setup(struct spi_device *spi)
1589 if (!spi->bits_per_word) 1775 if (!spi->bits_per_word)
1590 spi->bits_per_word = 8; 1776 spi->bits_per_word = 8;
1591 1777
1778 if (!spi->max_speed_hz)
1779 spi->max_speed_hz = spi->master->max_speed_hz;
1780
1592 if (spi->master->setup) 1781 if (spi->master->setup)
1593 status = spi->master->setup(spi); 1782 status = spi->master->setup(spi);
1594 1783
@@ -1609,11 +1798,10 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
1609{ 1798{
1610 struct spi_master *master = spi->master; 1799 struct spi_master *master = spi->master;
1611 struct spi_transfer *xfer; 1800 struct spi_transfer *xfer;
1801 int w_size;
1612 1802
1613 if (list_empty(&message->transfers)) 1803 if (list_empty(&message->transfers))
1614 return -EINVAL; 1804 return -EINVAL;
1615 if (!message->complete)
1616 return -EINVAL;
1617 1805
1618 /* Half-duplex links include original MicroWire, and ones with 1806 /* Half-duplex links include original MicroWire, and ones with
1619 * only one data pin like SPI_3WIRE (switches direction) or where 1807 * only one data pin like SPI_3WIRE (switches direction) or where
@@ -1644,12 +1832,13 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
1644 message->frame_length += xfer->len; 1832 message->frame_length += xfer->len;
1645 if (!xfer->bits_per_word) 1833 if (!xfer->bits_per_word)
1646 xfer->bits_per_word = spi->bits_per_word; 1834 xfer->bits_per_word = spi->bits_per_word;
1647 if (!xfer->speed_hz) { 1835
1836 if (!xfer->speed_hz)
1648 xfer->speed_hz = spi->max_speed_hz; 1837 xfer->speed_hz = spi->max_speed_hz;
1649 if (master->max_speed_hz && 1838
1650 xfer->speed_hz > master->max_speed_hz) 1839 if (master->max_speed_hz &&
1651 xfer->speed_hz = master->max_speed_hz; 1840 xfer->speed_hz > master->max_speed_hz)
1652 } 1841 xfer->speed_hz = master->max_speed_hz;
1653 1842
1654 if (master->bits_per_word_mask) { 1843 if (master->bits_per_word_mask) {
1655 /* Only 32 bits fit in the mask */ 1844 /* Only 32 bits fit in the mask */
@@ -1660,12 +1849,24 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
1660 return -EINVAL; 1849 return -EINVAL;
1661 } 1850 }
1662 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
1663 if (xfer->speed_hz && master->min_speed_hz && 1867 if (xfer->speed_hz && master->min_speed_hz &&
1664 xfer->speed_hz < master->min_speed_hz) 1868 xfer->speed_hz < master->min_speed_hz)
1665 return -EINVAL; 1869 return -EINVAL;
1666 if (xfer->speed_hz && master->max_speed_hz &&
1667 xfer->speed_hz > master->max_speed_hz)
1668 return -EINVAL;
1669 1870
1670 if (xfer->tx_buf && !xfer->tx_nbits) 1871 if (xfer->tx_buf && !xfer->tx_nbits)
1671 xfer->tx_nbits = SPI_NBITS_SINGLE; 1872 xfer->tx_nbits = SPI_NBITS_SINGLE;