aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-12-13 22:15:11 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2012-12-13 22:15:11 -0500
commitd3b43e12b2c8c69f79ab76dcdc5956f47c376378 (patch)
tree4fba9e425d7407e5abd5ab50d5ac09398ec5bff6 /drivers/spi/spi.c
parent15de0599277f3477ddd11766282587f12d214252 (diff)
parent7cb943615aabbd72624f77f0a84b8c5d627cf846 (diff)
Merge tag 'spi-for-linus' of git://git.secretlab.ca/git/linux-2.6
Pull SPI updates from Grant Likely: "Primarily SPI device driver bug fixes, one removal of an old driver, and some new tegra support. There is some core code change too, but all in all pretty small stuff. The new features to note are: - Common code for describing GPIO CS lines in the device tree - Remove the SPI_BUFSIZ limitation on spi_write_the_read() - core spi ensures bits_per_word is set correctly - SPARC can now use SPI" * tag 'spi-for-linus' of git://git.secretlab.ca/git/linux-2.6: (36 commits) spi/sparc: Allow of_register_spi_devices for sparc spi: Remove HOTPLUG section attributes spi: Add support for specifying 3-wire mode via device tree spi: Fix comparison of different integer types spi/orion: Add SPI_CHPA and SPI_CPOL support to kirkwood driver. spi/sh: Add SH Mobile series as dependency to MSIOF controller spi/sh-msiof: Remove unneeded clock name spi: Remove SPI_BUFSIZ restriction on spi_write_then_read() spi/stmp: remove obsolete driver spi/clps711x: New SPI master driver spi: omap2-mcspi: remove duplicate inclusion of linux/err.h spi: omap2-mcspi: Fix the redifine warning spi/sh-hspi: add CS manual control support of_spi: add generic binding support to specify cs gpio spi: omap2-mcspi: remove duplicated include from spi-omap2-mcspi.c spi/bitbang: (cosmetic) simplify list manipulation spi/bitbang: avoid needless loop flow manipulations spi/omap: fix D0/D1 direction confusion spi: tegra: add spi driver for sflash controller spi: Dont call master->setup if not populated ...
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r--drivers/spi/spi.c101
1 files changed, 79 insertions, 22 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 718cc1f49230..ab095acdb2a8 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -30,6 +30,7 @@
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>
@@ -333,6 +334,7 @@ struct spi_device *spi_alloc_device(struct spi_master *master)
333 spi->dev.parent = &master->dev; 334 spi->dev.parent = &master->dev;
334 spi->dev.bus = &spi_bus_type; 335 spi->dev.bus = &spi_bus_type;
335 spi->dev.release = spidev_release; 336 spi->dev.release = spidev_release;
337 spi->cs_gpio = -EINVAL;
336 device_initialize(&spi->dev); 338 device_initialize(&spi->dev);
337 return spi; 339 return spi;
338} 340}
@@ -350,15 +352,16 @@ EXPORT_SYMBOL_GPL(spi_alloc_device);
350int spi_add_device(struct spi_device *spi) 352int spi_add_device(struct spi_device *spi)
351{ 353{
352 static DEFINE_MUTEX(spi_add_lock); 354 static DEFINE_MUTEX(spi_add_lock);
353 struct device *dev = spi->master->dev.parent; 355 struct spi_master *master = spi->master;
356 struct device *dev = master->dev.parent;
354 struct device *d; 357 struct device *d;
355 int status; 358 int status;
356 359
357 /* Chipselects are numbered 0..max; validate. */ 360 /* Chipselects are numbered 0..max; validate. */
358 if (spi->chip_select >= spi->master->num_chipselect) { 361 if (spi->chip_select >= master->num_chipselect) {
359 dev_err(dev, "cs%d >= max %d\n", 362 dev_err(dev, "cs%d >= max %d\n",
360 spi->chip_select, 363 spi->chip_select,
361 spi->master->num_chipselect); 364 master->num_chipselect);
362 return -EINVAL; 365 return -EINVAL;
363 } 366 }
364 367
@@ -382,6 +385,9 @@ int spi_add_device(struct spi_device *spi)
382 goto done; 385 goto done;
383 } 386 }
384 387
388 if (master->cs_gpios)
389 spi->cs_gpio = master->cs_gpios[spi->chip_select];
390
385 /* Drivers may modify this initial i/o setup, but will 391 /* Drivers may modify this initial i/o setup, but will
386 * normally rely on the device being setup. Devices 392 * normally rely on the device being setup. Devices
387 * using SPI_CS_HIGH can't coexist well otherwise... 393 * using SPI_CS_HIGH can't coexist well otherwise...
@@ -492,8 +498,7 @@ static void spi_match_master_to_boardinfo(struct spi_master *master,
492 * 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
493 * any embedded pointers (platform_data, etc), they're copied as-is. 499 * any embedded pointers (platform_data, etc), they're copied as-is.
494 */ 500 */
495int __devinit 501int spi_register_board_info(struct spi_board_info const *info, unsigned n)
496spi_register_board_info(struct spi_board_info const *info, unsigned n)
497{ 502{
498 struct boardinfo *bi; 503 struct boardinfo *bi;
499 int i; 504 int i;
@@ -806,7 +811,7 @@ err_init_queue:
806 811
807/*-------------------------------------------------------------------------*/ 812/*-------------------------------------------------------------------------*/
808 813
809#if defined(CONFIG_OF) && !defined(CONFIG_SPARC) 814#if defined(CONFIG_OF)
810/** 815/**
811 * of_register_spi_devices() - Register child devices onto the SPI bus 816 * of_register_spi_devices() - Register child devices onto the SPI bus
812 * @master: Pointer to spi_master device 817 * @master: Pointer to spi_master device
@@ -861,6 +866,8 @@ static void of_register_spi_devices(struct spi_master *master)
861 spi->mode |= SPI_CPOL; 866 spi->mode |= SPI_CPOL;
862 if (of_find_property(nc, "spi-cs-high", NULL)) 867 if (of_find_property(nc, "spi-cs-high", NULL))
863 spi->mode |= SPI_CS_HIGH; 868 spi->mode |= SPI_CS_HIGH;
869 if (of_find_property(nc, "spi-3wire", NULL))
870 spi->mode |= SPI_3WIRE;
864 871
865 /* Device speed */ 872 /* Device speed */
866 prop = of_get_property(nc, "spi-max-frequency", &len); 873 prop = of_get_property(nc, "spi-max-frequency", &len);
@@ -1046,6 +1053,44 @@ struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
1046} 1053}
1047EXPORT_SYMBOL_GPL(spi_alloc_master); 1054EXPORT_SYMBOL_GPL(spi_alloc_master);
1048 1055
1056#ifdef CONFIG_OF
1057static int of_spi_register_master(struct spi_master *master)
1058{
1059 u16 nb;
1060 int i, *cs;
1061 struct device_node *np = master->dev.of_node;
1062
1063 if (!np)
1064 return 0;
1065
1066 nb = of_gpio_named_count(np, "cs-gpios");
1067 master->num_chipselect = max(nb, master->num_chipselect);
1068
1069 if (nb < 1)
1070 return 0;
1071
1072 cs = devm_kzalloc(&master->dev,
1073 sizeof(int) * master->num_chipselect,
1074 GFP_KERNEL);
1075 master->cs_gpios = cs;
1076
1077 if (!master->cs_gpios)
1078 return -ENOMEM;
1079
1080 memset(cs, -EINVAL, master->num_chipselect);
1081
1082 for (i = 0; i < nb; i++)
1083 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1084
1085 return 0;
1086}
1087#else
1088static int of_spi_register_master(struct spi_master *master)
1089{
1090 return 0;
1091}
1092#endif
1093
1049/** 1094/**
1050 * spi_register_master - register SPI master controller 1095 * spi_register_master - register SPI master controller
1051 * @master: initialized master, originally from spi_alloc_master() 1096 * @master: initialized master, originally from spi_alloc_master()
@@ -1077,6 +1122,10 @@ int spi_register_master(struct spi_master *master)
1077 if (!dev) 1122 if (!dev)
1078 return -ENODEV; 1123 return -ENODEV;
1079 1124
1125 status = of_spi_register_master(master);
1126 if (status)
1127 return status;
1128
1080 /* even if it's just one always-selected device, there must 1129 /* even if it's just one always-selected device, there must
1081 * be at least one chipselect 1130 * be at least one chipselect
1082 */ 1131 */
@@ -1257,7 +1306,7 @@ EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1257int spi_setup(struct spi_device *spi) 1306int spi_setup(struct spi_device *spi)
1258{ 1307{
1259 unsigned bad_bits; 1308 unsigned bad_bits;
1260 int status; 1309 int status = 0;
1261 1310
1262 /* help drivers fail *cleanly* when they need options 1311 /* help drivers fail *cleanly* when they need options
1263 * that aren't supported with their current master 1312 * that aren't supported with their current master
@@ -1272,7 +1321,8 @@ int spi_setup(struct spi_device *spi)
1272 if (!spi->bits_per_word) 1321 if (!spi->bits_per_word)
1273 spi->bits_per_word = 8; 1322 spi->bits_per_word = 8;
1274 1323
1275 status = spi->master->setup(spi); 1324 if (spi->master->setup)
1325 status = spi->master->setup(spi);
1276 1326
1277 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s" 1327 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
1278 "%u bits/w, %u Hz max --> %d\n", 1328 "%u bits/w, %u Hz max --> %d\n",
@@ -1291,6 +1341,7 @@ EXPORT_SYMBOL_GPL(spi_setup);
1291static int __spi_async(struct spi_device *spi, struct spi_message *message) 1341static int __spi_async(struct spi_device *spi, struct spi_message *message)
1292{ 1342{
1293 struct spi_master *master = spi->master; 1343 struct spi_master *master = spi->master;
1344 struct spi_transfer *xfer;
1294 1345
1295 /* Half-duplex links include original MicroWire, and ones with 1346 /* Half-duplex links include original MicroWire, and ones with
1296 * only one data pin like SPI_3WIRE (switches direction) or where 1347 * only one data pin like SPI_3WIRE (switches direction) or where
@@ -1299,7 +1350,6 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
1299 */ 1350 */
1300 if ((master->flags & SPI_MASTER_HALF_DUPLEX) 1351 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1301 || (spi->mode & SPI_3WIRE)) { 1352 || (spi->mode & SPI_3WIRE)) {
1302 struct spi_transfer *xfer;
1303 unsigned flags = master->flags; 1353 unsigned flags = master->flags;
1304 1354
1305 list_for_each_entry(xfer, &message->transfers, transfer_list) { 1355 list_for_each_entry(xfer, &message->transfers, transfer_list) {
@@ -1312,6 +1362,15 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
1312 } 1362 }
1313 } 1363 }
1314 1364
1365 /**
1366 * Set transfer bits_per_word as spi device default if it is not
1367 * set for this transfer.
1368 */
1369 list_for_each_entry(xfer, &message->transfers, transfer_list) {
1370 if (!xfer->bits_per_word)
1371 xfer->bits_per_word = spi->bits_per_word;
1372 }
1373
1315 message->spi = spi; 1374 message->spi = spi;
1316 message->status = -EINPROGRESS; 1375 message->status = -EINPROGRESS;
1317 return master->transfer(spi, message); 1376 return master->transfer(spi, message);
@@ -1588,12 +1647,18 @@ int spi_write_then_read(struct spi_device *spi,
1588 struct spi_transfer x[2]; 1647 struct spi_transfer x[2];
1589 u8 *local_buf; 1648 u8 *local_buf;
1590 1649
1591 /* Use preallocated DMA-safe buffer. We can't avoid copying here, 1650 /* Use preallocated DMA-safe buffer if we can. We can't avoid
1592 * (as a pure convenience thing), but we can keep heap costs 1651 * copying here, (as a pure convenience thing), but we can
1593 * out of the hot path ... 1652 * keep heap costs out of the hot path unless someone else is
1653 * using the pre-allocated buffer or the transfer is too large.
1594 */ 1654 */
1595 if ((n_tx + n_rx) > SPI_BUFSIZ) 1655 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
1596 return -EINVAL; 1656 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), GFP_KERNEL);
1657 if (!local_buf)
1658 return -ENOMEM;
1659 } else {
1660 local_buf = buf;
1661 }
1597 1662
1598 spi_message_init(&message); 1663 spi_message_init(&message);
1599 memset(x, 0, sizeof x); 1664 memset(x, 0, sizeof x);
@@ -1606,14 +1671,6 @@ int spi_write_then_read(struct spi_device *spi,
1606 spi_message_add_tail(&x[1], &message); 1671 spi_message_add_tail(&x[1], &message);
1607 } 1672 }
1608 1673
1609 /* ... unless someone else is using the pre-allocated buffer */
1610 if (!mutex_trylock(&lock)) {
1611 local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
1612 if (!local_buf)
1613 return -ENOMEM;
1614 } else
1615 local_buf = buf;
1616
1617 memcpy(local_buf, txbuf, n_tx); 1674 memcpy(local_buf, txbuf, n_tx);
1618 x[0].tx_buf = local_buf; 1675 x[0].tx_buf = local_buf;
1619 x[1].rx_buf = local_buf + n_tx; 1676 x[1].rx_buf = local_buf + n_tx;