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.c244
1 files changed, 201 insertions, 43 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 9e039c60c068..8d05accf706c 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -39,6 +39,9 @@
39#include <linux/ioport.h> 39#include <linux/ioport.h>
40#include <linux/acpi.h> 40#include <linux/acpi.h>
41 41
42#define CREATE_TRACE_POINTS
43#include <trace/events/spi.h>
44
42static void spidev_release(struct device *dev) 45static void spidev_release(struct device *dev)
43{ 46{
44 struct spi_device *spi = to_spi_device(dev); 47 struct spi_device *spi = to_spi_device(dev);
@@ -58,11 +61,13 @@ modalias_show(struct device *dev, struct device_attribute *a, char *buf)
58 61
59 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 62 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
60} 63}
64static DEVICE_ATTR_RO(modalias);
61 65
62static struct device_attribute spi_dev_attrs[] = { 66static struct attribute *spi_dev_attrs[] = {
63 __ATTR_RO(modalias), 67 &dev_attr_modalias.attr,
64 __ATTR_NULL, 68 NULL,
65}; 69};
70ATTRIBUTE_GROUPS(spi_dev);
66 71
67/* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 72/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
68 * and the sysfs version makes coldplug work too. 73 * and the sysfs version makes coldplug work too.
@@ -229,7 +234,7 @@ static const struct dev_pm_ops spi_pm = {
229 234
230struct bus_type spi_bus_type = { 235struct bus_type spi_bus_type = {
231 .name = "spi", 236 .name = "spi",
232 .dev_attrs = spi_dev_attrs, 237 .dev_groups = spi_dev_groups,
233 .match = spi_match_device, 238 .match = spi_match_device,
234 .uevent = spi_uevent, 239 .uevent = spi_uevent,
235 .pm = &spi_pm, 240 .pm = &spi_pm,
@@ -323,7 +328,7 @@ struct spi_device *spi_alloc_device(struct spi_master *master)
323 if (!spi_master_get(master)) 328 if (!spi_master_get(master))
324 return NULL; 329 return NULL;
325 330
326 spi = kzalloc(sizeof *spi, GFP_KERNEL); 331 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
327 if (!spi) { 332 if (!spi) {
328 dev_err(dev, "cannot alloc spi_device\n"); 333 dev_err(dev, "cannot alloc spi_device\n");
329 spi_master_put(master); 334 spi_master_put(master);
@@ -523,6 +528,95 @@ int spi_register_board_info(struct spi_board_info const *info, unsigned n)
523 528
524/*-------------------------------------------------------------------------*/ 529/*-------------------------------------------------------------------------*/
525 530
531static void spi_set_cs(struct spi_device *spi, bool enable)
532{
533 if (spi->mode & SPI_CS_HIGH)
534 enable = !enable;
535
536 if (spi->cs_gpio >= 0)
537 gpio_set_value(spi->cs_gpio, !enable);
538 else if (spi->master->set_cs)
539 spi->master->set_cs(spi, !enable);
540}
541
542/*
543 * spi_transfer_one_message - Default implementation of transfer_one_message()
544 *
545 * This is a standard implementation of transfer_one_message() for
546 * drivers which impelment a transfer_one() operation. It provides
547 * standard handling of delays and chip select management.
548 */
549static int spi_transfer_one_message(struct spi_master *master,
550 struct spi_message *msg)
551{
552 struct spi_transfer *xfer;
553 bool cur_cs = true;
554 bool keep_cs = false;
555 int ret = 0;
556
557 spi_set_cs(msg->spi, true);
558
559 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
560 trace_spi_transfer_start(msg, xfer);
561
562 INIT_COMPLETION(master->xfer_completion);
563
564 ret = master->transfer_one(master, msg->spi, xfer);
565 if (ret < 0) {
566 dev_err(&msg->spi->dev,
567 "SPI transfer failed: %d\n", ret);
568 goto out;
569 }
570
571 if (ret > 0)
572 wait_for_completion(&master->xfer_completion);
573
574 trace_spi_transfer_stop(msg, xfer);
575
576 if (msg->status != -EINPROGRESS)
577 goto out;
578
579 if (xfer->delay_usecs)
580 udelay(xfer->delay_usecs);
581
582 if (xfer->cs_change) {
583 if (list_is_last(&xfer->transfer_list,
584 &msg->transfers)) {
585 keep_cs = true;
586 } else {
587 cur_cs = !cur_cs;
588 spi_set_cs(msg->spi, cur_cs);
589 }
590 }
591
592 msg->actual_length += xfer->len;
593 }
594
595out:
596 if (ret != 0 || !keep_cs)
597 spi_set_cs(msg->spi, false);
598
599 if (msg->status == -EINPROGRESS)
600 msg->status = ret;
601
602 spi_finalize_current_message(master);
603
604 return ret;
605}
606
607/**
608 * spi_finalize_current_transfer - report completion of a transfer
609 *
610 * Called by SPI drivers using the core transfer_one_message()
611 * implementation to notify it that the current interrupt driven
612 * transfer has finised and the next one may be scheduled.
613 */
614void spi_finalize_current_transfer(struct spi_master *master)
615{
616 complete(&master->xfer_completion);
617}
618EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
619
526/** 620/**
527 * spi_pump_messages - kthread work function which processes spi message queue 621 * spi_pump_messages - kthread work function which processes spi message queue
528 * @work: pointer to kthread work struct contained in the master struct 622 * @work: pointer to kthread work struct contained in the master struct
@@ -557,6 +651,7 @@ static void spi_pump_messages(struct kthread_work *work)
557 pm_runtime_mark_last_busy(master->dev.parent); 651 pm_runtime_mark_last_busy(master->dev.parent);
558 pm_runtime_put_autosuspend(master->dev.parent); 652 pm_runtime_put_autosuspend(master->dev.parent);
559 } 653 }
654 trace_spi_master_idle(master);
560 return; 655 return;
561 } 656 }
562 657
@@ -585,6 +680,9 @@ static void spi_pump_messages(struct kthread_work *work)
585 } 680 }
586 } 681 }
587 682
683 if (!was_busy)
684 trace_spi_master_busy(master);
685
588 if (!was_busy && master->prepare_transfer_hardware) { 686 if (!was_busy && master->prepare_transfer_hardware) {
589 ret = master->prepare_transfer_hardware(master); 687 ret = master->prepare_transfer_hardware(master);
590 if (ret) { 688 if (ret) {
@@ -597,6 +695,20 @@ static void spi_pump_messages(struct kthread_work *work)
597 } 695 }
598 } 696 }
599 697
698 trace_spi_message_start(master->cur_msg);
699
700 if (master->prepare_message) {
701 ret = master->prepare_message(master, master->cur_msg);
702 if (ret) {
703 dev_err(&master->dev,
704 "failed to prepare message: %d\n", ret);
705 master->cur_msg->status = ret;
706 spi_finalize_current_message(master);
707 return;
708 }
709 master->cur_msg_prepared = true;
710 }
711
600 ret = master->transfer_one_message(master, master->cur_msg); 712 ret = master->transfer_one_message(master, master->cur_msg);
601 if (ret) { 713 if (ret) {
602 dev_err(&master->dev, 714 dev_err(&master->dev,
@@ -678,6 +790,7 @@ void spi_finalize_current_message(struct spi_master *master)
678{ 790{
679 struct spi_message *mesg; 791 struct spi_message *mesg;
680 unsigned long flags; 792 unsigned long flags;
793 int ret;
681 794
682 spin_lock_irqsave(&master->queue_lock, flags); 795 spin_lock_irqsave(&master->queue_lock, flags);
683 mesg = master->cur_msg; 796 mesg = master->cur_msg;
@@ -686,9 +799,20 @@ void spi_finalize_current_message(struct spi_master *master)
686 queue_kthread_work(&master->kworker, &master->pump_messages); 799 queue_kthread_work(&master->kworker, &master->pump_messages);
687 spin_unlock_irqrestore(&master->queue_lock, flags); 800 spin_unlock_irqrestore(&master->queue_lock, flags);
688 801
802 if (master->cur_msg_prepared && master->unprepare_message) {
803 ret = master->unprepare_message(master, mesg);
804 if (ret) {
805 dev_err(&master->dev,
806 "failed to unprepare message: %d\n", ret);
807 }
808 }
809 master->cur_msg_prepared = false;
810
689 mesg->state = NULL; 811 mesg->state = NULL;
690 if (mesg->complete) 812 if (mesg->complete)
691 mesg->complete(mesg->context); 813 mesg->complete(mesg->context);
814
815 trace_spi_message_done(mesg);
692} 816}
693EXPORT_SYMBOL_GPL(spi_finalize_current_message); 817EXPORT_SYMBOL_GPL(spi_finalize_current_message);
694 818
@@ -803,6 +927,8 @@ static int spi_master_initialize_queue(struct spi_master *master)
803 927
804 master->queued = true; 928 master->queued = true;
805 master->transfer = spi_queued_transfer; 929 master->transfer = spi_queued_transfer;
930 if (!master->transfer_one_message)
931 master->transfer_one_message = spi_transfer_one_message;
806 932
807 /* Initialize and start queue */ 933 /* Initialize and start queue */
808 ret = spi_init_queue(master); 934 ret = spi_init_queue(master);
@@ -838,10 +964,8 @@ static void of_register_spi_devices(struct spi_master *master)
838{ 964{
839 struct spi_device *spi; 965 struct spi_device *spi;
840 struct device_node *nc; 966 struct device_node *nc;
841 const __be32 *prop;
842 char modalias[SPI_NAME_SIZE + 4];
843 int rc; 967 int rc;
844 int len; 968 u32 value;
845 969
846 if (!master->dev.of_node) 970 if (!master->dev.of_node)
847 return; 971 return;
@@ -866,14 +990,14 @@ static void of_register_spi_devices(struct spi_master *master)
866 } 990 }
867 991
868 /* Device address */ 992 /* Device address */
869 prop = of_get_property(nc, "reg", &len); 993 rc = of_property_read_u32(nc, "reg", &value);
870 if (!prop || len < sizeof(*prop)) { 994 if (rc) {
871 dev_err(&master->dev, "%s has no 'reg' property\n", 995 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
872 nc->full_name); 996 nc->full_name, rc);
873 spi_dev_put(spi); 997 spi_dev_put(spi);
874 continue; 998 continue;
875 } 999 }
876 spi->chip_select = be32_to_cpup(prop); 1000 spi->chip_select = value;
877 1001
878 /* Mode (clock phase/polarity/etc.) */ 1002 /* Mode (clock phase/polarity/etc.) */
879 if (of_find_property(nc, "spi-cpha", NULL)) 1003 if (of_find_property(nc, "spi-cpha", NULL))
@@ -886,55 +1010,53 @@ static void of_register_spi_devices(struct spi_master *master)
886 spi->mode |= SPI_3WIRE; 1010 spi->mode |= SPI_3WIRE;
887 1011
888 /* Device DUAL/QUAD mode */ 1012 /* Device DUAL/QUAD mode */
889 prop = of_get_property(nc, "spi-tx-bus-width", &len); 1013 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
890 if (prop && len == sizeof(*prop)) { 1014 switch (value) {
891 switch (be32_to_cpup(prop)) { 1015 case 1:
892 case SPI_NBITS_SINGLE:
893 break; 1016 break;
894 case SPI_NBITS_DUAL: 1017 case 2:
895 spi->mode |= SPI_TX_DUAL; 1018 spi->mode |= SPI_TX_DUAL;
896 break; 1019 break;
897 case SPI_NBITS_QUAD: 1020 case 4:
898 spi->mode |= SPI_TX_QUAD; 1021 spi->mode |= SPI_TX_QUAD;
899 break; 1022 break;
900 default: 1023 default:
901 dev_err(&master->dev, 1024 dev_err(&master->dev,
902 "spi-tx-bus-width %d not supported\n", 1025 "spi-tx-bus-width %d not supported\n",
903 be32_to_cpup(prop)); 1026 value);
904 spi_dev_put(spi); 1027 spi_dev_put(spi);
905 continue; 1028 continue;
906 } 1029 }
907 } 1030 }
908 1031
909 prop = of_get_property(nc, "spi-rx-bus-width", &len); 1032 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
910 if (prop && len == sizeof(*prop)) { 1033 switch (value) {
911 switch (be32_to_cpup(prop)) { 1034 case 1:
912 case SPI_NBITS_SINGLE:
913 break; 1035 break;
914 case SPI_NBITS_DUAL: 1036 case 2:
915 spi->mode |= SPI_RX_DUAL; 1037 spi->mode |= SPI_RX_DUAL;
916 break; 1038 break;
917 case SPI_NBITS_QUAD: 1039 case 4:
918 spi->mode |= SPI_RX_QUAD; 1040 spi->mode |= SPI_RX_QUAD;
919 break; 1041 break;
920 default: 1042 default:
921 dev_err(&master->dev, 1043 dev_err(&master->dev,
922 "spi-rx-bus-width %d not supported\n", 1044 "spi-rx-bus-width %d not supported\n",
923 be32_to_cpup(prop)); 1045 value);
924 spi_dev_put(spi); 1046 spi_dev_put(spi);
925 continue; 1047 continue;
926 } 1048 }
927 } 1049 }
928 1050
929 /* Device speed */ 1051 /* Device speed */
930 prop = of_get_property(nc, "spi-max-frequency", &len); 1052 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
931 if (!prop || len < sizeof(*prop)) { 1053 if (rc) {
932 dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n", 1054 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
933 nc->full_name); 1055 nc->full_name, rc);
934 spi_dev_put(spi); 1056 spi_dev_put(spi);
935 continue; 1057 continue;
936 } 1058 }
937 spi->max_speed_hz = be32_to_cpup(prop); 1059 spi->max_speed_hz = value;
938 1060
939 /* IRQ */ 1061 /* IRQ */
940 spi->irq = irq_of_parse_and_map(nc, 0); 1062 spi->irq = irq_of_parse_and_map(nc, 0);
@@ -944,9 +1066,7 @@ static void of_register_spi_devices(struct spi_master *master)
944 spi->dev.of_node = nc; 1066 spi->dev.of_node = nc;
945 1067
946 /* Register the new device */ 1068 /* Register the new device */
947 snprintf(modalias, sizeof(modalias), "%s%s", SPI_MODULE_PREFIX, 1069 request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
948 spi->modalias);
949 request_module(modalias);
950 rc = spi_add_device(spi); 1070 rc = spi_add_device(spi);
951 if (rc) { 1071 if (rc) {
952 dev_err(&master->dev, "spi_device register error %s\n", 1072 dev_err(&master->dev, "spi_device register error %s\n",
@@ -1025,7 +1145,7 @@ static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1025 return AE_OK; 1145 return AE_OK;
1026 } 1146 }
1027 1147
1028 strlcpy(spi->modalias, dev_name(&adev->dev), sizeof(spi->modalias)); 1148 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
1029 if (spi_add_device(spi)) { 1149 if (spi_add_device(spi)) {
1030 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n", 1150 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1031 dev_name(&adev->dev)); 1151 dev_name(&adev->dev));
@@ -1097,7 +1217,7 @@ struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
1097 if (!dev) 1217 if (!dev)
1098 return NULL; 1218 return NULL;
1099 1219
1100 master = kzalloc(size + sizeof *master, GFP_KERNEL); 1220 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
1101 if (!master) 1221 if (!master)
1102 return NULL; 1222 return NULL;
1103 1223
@@ -1122,7 +1242,7 @@ static int of_spi_register_master(struct spi_master *master)
1122 return 0; 1242 return 0;
1123 1243
1124 nb = of_gpio_named_count(np, "cs-gpios"); 1244 nb = of_gpio_named_count(np, "cs-gpios");
1125 master->num_chipselect = max(nb, (int)master->num_chipselect); 1245 master->num_chipselect = max_t(int, nb, master->num_chipselect);
1126 1246
1127 /* Return error only for an incorrectly formed cs-gpios property */ 1247 /* Return error only for an incorrectly formed cs-gpios property */
1128 if (nb == 0 || nb == -ENOENT) 1248 if (nb == 0 || nb == -ENOENT)
@@ -1209,6 +1329,7 @@ int spi_register_master(struct spi_master *master)
1209 spin_lock_init(&master->bus_lock_spinlock); 1329 spin_lock_init(&master->bus_lock_spinlock);
1210 mutex_init(&master->bus_lock_mutex); 1330 mutex_init(&master->bus_lock_mutex);
1211 master->bus_lock_flag = 0; 1331 master->bus_lock_flag = 0;
1332 init_completion(&master->xfer_completion);
1212 1333
1213 /* register the device, then userspace will see it. 1334 /* register the device, then userspace will see it.
1214 * registration fails if the bus ID is in use. 1335 * registration fails if the bus ID is in use.
@@ -1245,6 +1366,41 @@ done:
1245} 1366}
1246EXPORT_SYMBOL_GPL(spi_register_master); 1367EXPORT_SYMBOL_GPL(spi_register_master);
1247 1368
1369static void devm_spi_unregister(struct device *dev, void *res)
1370{
1371 spi_unregister_master(*(struct spi_master **)res);
1372}
1373
1374/**
1375 * dev_spi_register_master - register managed SPI master controller
1376 * @dev: device managing SPI master
1377 * @master: initialized master, originally from spi_alloc_master()
1378 * Context: can sleep
1379 *
1380 * Register a SPI device as with spi_register_master() which will
1381 * automatically be unregister
1382 */
1383int devm_spi_register_master(struct device *dev, struct spi_master *master)
1384{
1385 struct spi_master **ptr;
1386 int ret;
1387
1388 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1389 if (!ptr)
1390 return -ENOMEM;
1391
1392 ret = spi_register_master(master);
1393 if (ret != 0) {
1394 *ptr = master;
1395 devres_add(dev, ptr);
1396 } else {
1397 devres_free(ptr);
1398 }
1399
1400 return ret;
1401}
1402EXPORT_SYMBOL_GPL(devm_spi_register_master);
1403
1248static int __unregister(struct device *dev, void *null) 1404static int __unregister(struct device *dev, void *null)
1249{ 1405{
1250 spi_unregister_device(to_spi_device(dev)); 1406 spi_unregister_device(to_spi_device(dev));
@@ -1402,8 +1558,7 @@ int spi_setup(struct spi_device *spi)
1402 if (spi->master->setup) 1558 if (spi->master->setup)
1403 status = spi->master->setup(spi); 1559 status = spi->master->setup(spi);
1404 1560
1405 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s" 1561 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
1406 "%u bits/w, %u Hz max --> %d\n",
1407 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 1562 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
1408 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 1563 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
1409 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 1564 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
@@ -1421,6 +1576,10 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
1421 struct spi_master *master = spi->master; 1576 struct spi_master *master = spi->master;
1422 struct spi_transfer *xfer; 1577 struct spi_transfer *xfer;
1423 1578
1579 message->spi = spi;
1580
1581 trace_spi_message_submit(message);
1582
1424 if (list_empty(&message->transfers)) 1583 if (list_empty(&message->transfers))
1425 return -EINVAL; 1584 return -EINVAL;
1426 if (!message->complete) 1585 if (!message->complete)
@@ -1520,7 +1679,6 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
1520 } 1679 }
1521 } 1680 }
1522 1681
1523 message->spi = spi;
1524 message->status = -EINPROGRESS; 1682 message->status = -EINPROGRESS;
1525 return master->transfer(spi, message); 1683 return master->transfer(spi, message);
1526} 1684}
@@ -1762,7 +1920,7 @@ int spi_bus_unlock(struct spi_master *master)
1762EXPORT_SYMBOL_GPL(spi_bus_unlock); 1920EXPORT_SYMBOL_GPL(spi_bus_unlock);
1763 1921
1764/* portable code must never pass more than 32 bytes */ 1922/* portable code must never pass more than 32 bytes */
1765#define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) 1923#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
1766 1924
1767static u8 *buf; 1925static u8 *buf;
1768 1926
@@ -1811,7 +1969,7 @@ int spi_write_then_read(struct spi_device *spi,
1811 } 1969 }
1812 1970
1813 spi_message_init(&message); 1971 spi_message_init(&message);
1814 memset(x, 0, sizeof x); 1972 memset(x, 0, sizeof(x));
1815 if (n_tx) { 1973 if (n_tx) {
1816 x[0].len = n_tx; 1974 x[0].len = n_tx;
1817 spi_message_add_tail(&x[0], &message); 1975 spi_message_add_tail(&x[0], &message);