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.c211
1 files changed, 186 insertions, 25 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 84c2861d6f4d..19ee901577da 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -30,11 +30,14 @@
30#include <linux/slab.h> 30#include <linux/slab.h>
31#include <linux/mod_devicetable.h> 31#include <linux/mod_devicetable.h>
32#include <linux/spi/spi.h> 32#include <linux/spi/spi.h>
33#include <linux/of_gpio.h>
33#include <linux/pm_runtime.h> 34#include <linux/pm_runtime.h>
34#include <linux/export.h> 35#include <linux/export.h>
35#include <linux/sched.h> 36#include <linux/sched.h>
36#include <linux/delay.h> 37#include <linux/delay.h>
37#include <linux/kthread.h> 38#include <linux/kthread.h>
39#include <linux/ioport.h>
40#include <linux/acpi.h>
38 41
39static void spidev_release(struct device *dev) 42static void spidev_release(struct device *dev)
40{ 43{
@@ -93,6 +96,10 @@ static int spi_match_device(struct device *dev, struct device_driver *drv)
93 if (of_driver_match_device(dev, drv)) 96 if (of_driver_match_device(dev, drv))
94 return 1; 97 return 1;
95 98
99 /* Then try ACPI */
100 if (acpi_driver_match_device(dev, drv))
101 return 1;
102
96 if (sdrv->id_table) 103 if (sdrv->id_table)
97 return !!spi_match_id(sdrv->id_table, spi); 104 return !!spi_match_id(sdrv->id_table, spi);
98 105
@@ -327,6 +334,7 @@ struct spi_device *spi_alloc_device(struct spi_master *master)
327 spi->dev.parent = &master->dev; 334 spi->dev.parent = &master->dev;
328 spi->dev.bus = &spi_bus_type; 335 spi->dev.bus = &spi_bus_type;
329 spi->dev.release = spidev_release; 336 spi->dev.release = spidev_release;
337 spi->cs_gpio = -EINVAL;
330 device_initialize(&spi->dev); 338 device_initialize(&spi->dev);
331 return spi; 339 return spi;
332} 340}
@@ -344,15 +352,16 @@ EXPORT_SYMBOL_GPL(spi_alloc_device);
344int spi_add_device(struct spi_device *spi) 352int spi_add_device(struct spi_device *spi)
345{ 353{
346 static DEFINE_MUTEX(spi_add_lock); 354 static DEFINE_MUTEX(spi_add_lock);
347 struct device *dev = spi->master->dev.parent; 355 struct spi_master *master = spi->master;
356 struct device *dev = master->dev.parent;
348 struct device *d; 357 struct device *d;
349 int status; 358 int status;
350 359
351 /* Chipselects are numbered 0..max; validate. */ 360 /* Chipselects are numbered 0..max; validate. */
352 if (spi->chip_select >= spi->master->num_chipselect) { 361 if (spi->chip_select >= master->num_chipselect) {
353 dev_err(dev, "cs%d >= max %d\n", 362 dev_err(dev, "cs%d >= max %d\n",
354 spi->chip_select, 363 spi->chip_select,
355 spi->master->num_chipselect); 364 master->num_chipselect);
356 return -EINVAL; 365 return -EINVAL;
357 } 366 }
358 367
@@ -376,6 +385,9 @@ int spi_add_device(struct spi_device *spi)
376 goto done; 385 goto done;
377 } 386 }
378 387
388 if (master->cs_gpios)
389 spi->cs_gpio = master->cs_gpios[spi->chip_select];
390
379 /* Drivers may modify this initial i/o setup, but will 391 /* Drivers may modify this initial i/o setup, but will
380 * normally rely on the device being setup. Devices 392 * normally rely on the device being setup. Devices
381 * using SPI_CS_HIGH can't coexist well otherwise... 393 * using SPI_CS_HIGH can't coexist well otherwise...
@@ -486,8 +498,7 @@ static void spi_match_master_to_boardinfo(struct spi_master *master,
486 * The board info passed can safely be __initdata ... but be careful of 498 * The board info passed can safely be __initdata ... but be careful of
487 * any embedded pointers (platform_data, etc), they're copied as-is. 499 * any embedded pointers (platform_data, etc), they're copied as-is.
488 */ 500 */
489int __devinit 501int spi_register_board_info(struct spi_board_info const *info, unsigned n)
490spi_register_board_info(struct spi_board_info const *info, unsigned n)
491{ 502{
492 struct boardinfo *bi; 503 struct boardinfo *bi;
493 int i; 504 int i;
@@ -800,7 +811,7 @@ err_init_queue:
800 811
801/*-------------------------------------------------------------------------*/ 812/*-------------------------------------------------------------------------*/
802 813
803#if defined(CONFIG_OF) && !defined(CONFIG_SPARC) 814#if defined(CONFIG_OF)
804/** 815/**
805 * of_register_spi_devices() - Register child devices onto the SPI bus 816 * of_register_spi_devices() - Register child devices onto the SPI bus
806 * @master: Pointer to spi_master device 817 * @master: Pointer to spi_master device
@@ -813,13 +824,14 @@ static void of_register_spi_devices(struct spi_master *master)
813 struct spi_device *spi; 824 struct spi_device *spi;
814 struct device_node *nc; 825 struct device_node *nc;
815 const __be32 *prop; 826 const __be32 *prop;
827 char modalias[SPI_NAME_SIZE + 4];
816 int rc; 828 int rc;
817 int len; 829 int len;
818 830
819 if (!master->dev.of_node) 831 if (!master->dev.of_node)
820 return; 832 return;
821 833
822 for_each_child_of_node(master->dev.of_node, nc) { 834 for_each_available_child_of_node(master->dev.of_node, nc) {
823 /* Alloc an spi_device */ 835 /* Alloc an spi_device */
824 spi = spi_alloc_device(master); 836 spi = spi_alloc_device(master);
825 if (!spi) { 837 if (!spi) {
@@ -855,6 +867,8 @@ static void of_register_spi_devices(struct spi_master *master)
855 spi->mode |= SPI_CPOL; 867 spi->mode |= SPI_CPOL;
856 if (of_find_property(nc, "spi-cs-high", NULL)) 868 if (of_find_property(nc, "spi-cs-high", NULL))
857 spi->mode |= SPI_CS_HIGH; 869 spi->mode |= SPI_CS_HIGH;
870 if (of_find_property(nc, "spi-3wire", NULL))
871 spi->mode |= SPI_3WIRE;
858 872
859 /* Device speed */ 873 /* Device speed */
860 prop = of_get_property(nc, "spi-max-frequency", &len); 874 prop = of_get_property(nc, "spi-max-frequency", &len);
@@ -874,7 +888,9 @@ static void of_register_spi_devices(struct spi_master *master)
874 spi->dev.of_node = nc; 888 spi->dev.of_node = nc;
875 889
876 /* Register the new device */ 890 /* Register the new device */
877 request_module(spi->modalias); 891 snprintf(modalias, sizeof(modalias), "%s%s", SPI_MODULE_PREFIX,
892 spi->modalias);
893 request_module(modalias);
878 rc = spi_add_device(spi); 894 rc = spi_add_device(spi);
879 if (rc) { 895 if (rc) {
880 dev_err(&master->dev, "spi_device register error %s\n", 896 dev_err(&master->dev, "spi_device register error %s\n",
@@ -888,6 +904,100 @@ static void of_register_spi_devices(struct spi_master *master)
888static void of_register_spi_devices(struct spi_master *master) { } 904static void of_register_spi_devices(struct spi_master *master) { }
889#endif 905#endif
890 906
907#ifdef CONFIG_ACPI
908static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
909{
910 struct spi_device *spi = data;
911
912 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
913 struct acpi_resource_spi_serialbus *sb;
914
915 sb = &ares->data.spi_serial_bus;
916 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
917 spi->chip_select = sb->device_selection;
918 spi->max_speed_hz = sb->connection_speed;
919
920 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
921 spi->mode |= SPI_CPHA;
922 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
923 spi->mode |= SPI_CPOL;
924 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
925 spi->mode |= SPI_CS_HIGH;
926 }
927 } else if (spi->irq < 0) {
928 struct resource r;
929
930 if (acpi_dev_resource_interrupt(ares, 0, &r))
931 spi->irq = r.start;
932 }
933
934 /* Always tell the ACPI core to skip this resource */
935 return 1;
936}
937
938static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
939 void *data, void **return_value)
940{
941 struct spi_master *master = data;
942 struct list_head resource_list;
943 struct acpi_device *adev;
944 struct spi_device *spi;
945 int ret;
946
947 if (acpi_bus_get_device(handle, &adev))
948 return AE_OK;
949 if (acpi_bus_get_status(adev) || !adev->status.present)
950 return AE_OK;
951
952 spi = spi_alloc_device(master);
953 if (!spi) {
954 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
955 dev_name(&adev->dev));
956 return AE_NO_MEMORY;
957 }
958
959 ACPI_HANDLE_SET(&spi->dev, handle);
960 spi->irq = -1;
961
962 INIT_LIST_HEAD(&resource_list);
963 ret = acpi_dev_get_resources(adev, &resource_list,
964 acpi_spi_add_resource, spi);
965 acpi_dev_free_resource_list(&resource_list);
966
967 if (ret < 0 || !spi->max_speed_hz) {
968 spi_dev_put(spi);
969 return AE_OK;
970 }
971
972 strlcpy(spi->modalias, dev_name(&adev->dev), sizeof(spi->modalias));
973 if (spi_add_device(spi)) {
974 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
975 dev_name(&adev->dev));
976 spi_dev_put(spi);
977 }
978
979 return AE_OK;
980}
981
982static void acpi_register_spi_devices(struct spi_master *master)
983{
984 acpi_status status;
985 acpi_handle handle;
986
987 handle = ACPI_HANDLE(&master->dev);
988 if (!handle)
989 return;
990
991 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
992 acpi_spi_add_device, NULL,
993 master, NULL);
994 if (ACPI_FAILURE(status))
995 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
996}
997#else
998static inline void acpi_register_spi_devices(struct spi_master *master) {}
999#endif /* CONFIG_ACPI */
1000
891static void spi_master_release(struct device *dev) 1001static void spi_master_release(struct device *dev)
892{ 1002{
893 struct spi_master *master; 1003 struct spi_master *master;
@@ -946,6 +1056,44 @@ struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
946} 1056}
947EXPORT_SYMBOL_GPL(spi_alloc_master); 1057EXPORT_SYMBOL_GPL(spi_alloc_master);
948 1058
1059#ifdef CONFIG_OF
1060static int of_spi_register_master(struct spi_master *master)
1061{
1062 u16 nb;
1063 int i, *cs;
1064 struct device_node *np = master->dev.of_node;
1065
1066 if (!np)
1067 return 0;
1068
1069 nb = of_gpio_named_count(np, "cs-gpios");
1070 master->num_chipselect = max(nb, master->num_chipselect);
1071
1072 if (nb < 1)
1073 return 0;
1074
1075 cs = devm_kzalloc(&master->dev,
1076 sizeof(int) * master->num_chipselect,
1077 GFP_KERNEL);
1078 master->cs_gpios = cs;
1079
1080 if (!master->cs_gpios)
1081 return -ENOMEM;
1082
1083 memset(cs, -EINVAL, master->num_chipselect);
1084
1085 for (i = 0; i < nb; i++)
1086 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1087
1088 return 0;
1089}
1090#else
1091static int of_spi_register_master(struct spi_master *master)
1092{
1093 return 0;
1094}
1095#endif
1096
949/** 1097/**
950 * spi_register_master - register SPI master controller 1098 * spi_register_master - register SPI master controller
951 * @master: initialized master, originally from spi_alloc_master() 1099 * @master: initialized master, originally from spi_alloc_master()
@@ -977,6 +1125,10 @@ int spi_register_master(struct spi_master *master)
977 if (!dev) 1125 if (!dev)
978 return -ENODEV; 1126 return -ENODEV;
979 1127
1128 status = of_spi_register_master(master);
1129 if (status)
1130 return status;
1131
980 /* even if it's just one always-selected device, there must 1132 /* even if it's just one always-selected device, there must
981 * be at least one chipselect 1133 * be at least one chipselect
982 */ 1134 */
@@ -1023,8 +1175,9 @@ int spi_register_master(struct spi_master *master)
1023 spi_match_master_to_boardinfo(master, &bi->board_info); 1175 spi_match_master_to_boardinfo(master, &bi->board_info);
1024 mutex_unlock(&board_lock); 1176 mutex_unlock(&board_lock);
1025 1177
1026 /* Register devices from the device tree */ 1178 /* Register devices from the device tree and ACPI */
1027 of_register_spi_devices(master); 1179 of_register_spi_devices(master);
1180 acpi_register_spi_devices(master);
1028done: 1181done:
1029 return status; 1182 return status;
1030} 1183}
@@ -1156,7 +1309,7 @@ EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1156int spi_setup(struct spi_device *spi) 1309int spi_setup(struct spi_device *spi)
1157{ 1310{
1158 unsigned bad_bits; 1311 unsigned bad_bits;
1159 int status; 1312 int status = 0;
1160 1313
1161 /* help drivers fail *cleanly* when they need options 1314 /* help drivers fail *cleanly* when they need options
1162 * that aren't supported with their current master 1315 * that aren't supported with their current master
@@ -1171,7 +1324,8 @@ int spi_setup(struct spi_device *spi)
1171 if (!spi->bits_per_word) 1324 if (!spi->bits_per_word)
1172 spi->bits_per_word = 8; 1325 spi->bits_per_word = 8;
1173 1326
1174 status = spi->master->setup(spi); 1327 if (spi->master->setup)
1328 status = spi->master->setup(spi);
1175 1329
1176 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s" 1330 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
1177 "%u bits/w, %u Hz max --> %d\n", 1331 "%u bits/w, %u Hz max --> %d\n",
@@ -1190,6 +1344,7 @@ EXPORT_SYMBOL_GPL(spi_setup);
1190static int __spi_async(struct spi_device *spi, struct spi_message *message) 1344static int __spi_async(struct spi_device *spi, struct spi_message *message)
1191{ 1345{
1192 struct spi_master *master = spi->master; 1346 struct spi_master *master = spi->master;
1347 struct spi_transfer *xfer;
1193 1348
1194 /* Half-duplex links include original MicroWire, and ones with 1349 /* Half-duplex links include original MicroWire, and ones with
1195 * only one data pin like SPI_3WIRE (switches direction) or where 1350 * only one data pin like SPI_3WIRE (switches direction) or where
@@ -1198,7 +1353,6 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
1198 */ 1353 */
1199 if ((master->flags & SPI_MASTER_HALF_DUPLEX) 1354 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1200 || (spi->mode & SPI_3WIRE)) { 1355 || (spi->mode & SPI_3WIRE)) {
1201 struct spi_transfer *xfer;
1202 unsigned flags = master->flags; 1356 unsigned flags = master->flags;
1203 1357
1204 list_for_each_entry(xfer, &message->transfers, transfer_list) { 1358 list_for_each_entry(xfer, &message->transfers, transfer_list) {
@@ -1211,6 +1365,15 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
1211 } 1365 }
1212 } 1366 }
1213 1367
1368 /**
1369 * Set transfer bits_per_word as spi device default if it is not
1370 * set for this transfer.
1371 */
1372 list_for_each_entry(xfer, &message->transfers, transfer_list) {
1373 if (!xfer->bits_per_word)
1374 xfer->bits_per_word = spi->bits_per_word;
1375 }
1376
1214 message->spi = spi; 1377 message->spi = spi;
1215 message->status = -EINPROGRESS; 1378 message->status = -EINPROGRESS;
1216 return master->transfer(spi, message); 1379 return master->transfer(spi, message);
@@ -1487,12 +1650,18 @@ int spi_write_then_read(struct spi_device *spi,
1487 struct spi_transfer x[2]; 1650 struct spi_transfer x[2];
1488 u8 *local_buf; 1651 u8 *local_buf;
1489 1652
1490 /* Use preallocated DMA-safe buffer. We can't avoid copying here, 1653 /* Use preallocated DMA-safe buffer if we can. We can't avoid
1491 * (as a pure convenience thing), but we can keep heap costs 1654 * copying here, (as a pure convenience thing), but we can
1492 * out of the hot path ... 1655 * keep heap costs out of the hot path unless someone else is
1656 * using the pre-allocated buffer or the transfer is too large.
1493 */ 1657 */
1494 if ((n_tx + n_rx) > SPI_BUFSIZ) 1658 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
1495 return -EINVAL; 1659 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), GFP_KERNEL);
1660 if (!local_buf)
1661 return -ENOMEM;
1662 } else {
1663 local_buf = buf;
1664 }
1496 1665
1497 spi_message_init(&message); 1666 spi_message_init(&message);
1498 memset(x, 0, sizeof x); 1667 memset(x, 0, sizeof x);
@@ -1505,14 +1674,6 @@ int spi_write_then_read(struct spi_device *spi,
1505 spi_message_add_tail(&x[1], &message); 1674 spi_message_add_tail(&x[1], &message);
1506 } 1675 }
1507 1676
1508 /* ... unless someone else is using the pre-allocated buffer */
1509 if (!mutex_trylock(&lock)) {
1510 local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
1511 if (!local_buf)
1512 return -ENOMEM;
1513 } else
1514 local_buf = buf;
1515
1516 memcpy(local_buf, txbuf, n_tx); 1677 memcpy(local_buf, txbuf, n_tx);
1517 x[0].tx_buf = local_buf; 1678 x[0].tx_buf = local_buf;
1518 x[1].rx_buf = local_buf + n_tx; 1679 x[1].rx_buf = local_buf + n_tx;