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.c54
1 files changed, 51 insertions, 3 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 84c2861d6f4d..1587a4a5ff41 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>
@@ -327,6 +328,7 @@ struct spi_device *spi_alloc_device(struct spi_master *master)
327 spi->dev.parent = &master->dev; 328 spi->dev.parent = &master->dev;
328 spi->dev.bus = &spi_bus_type; 329 spi->dev.bus = &spi_bus_type;
329 spi->dev.release = spidev_release; 330 spi->dev.release = spidev_release;
331 spi->cs_gpio = -EINVAL;
330 device_initialize(&spi->dev); 332 device_initialize(&spi->dev);
331 return spi; 333 return spi;
332} 334}
@@ -344,15 +346,16 @@ EXPORT_SYMBOL_GPL(spi_alloc_device);
344int spi_add_device(struct spi_device *spi) 346int spi_add_device(struct spi_device *spi)
345{ 347{
346 static DEFINE_MUTEX(spi_add_lock); 348 static DEFINE_MUTEX(spi_add_lock);
347 struct device *dev = spi->master->dev.parent; 349 struct spi_master *master = spi->master;
350 struct device *dev = master->dev.parent;
348 struct device *d; 351 struct device *d;
349 int status; 352 int status;
350 353
351 /* Chipselects are numbered 0..max; validate. */ 354 /* Chipselects are numbered 0..max; validate. */
352 if (spi->chip_select >= spi->master->num_chipselect) { 355 if (spi->chip_select >= master->num_chipselect) {
353 dev_err(dev, "cs%d >= max %d\n", 356 dev_err(dev, "cs%d >= max %d\n",
354 spi->chip_select, 357 spi->chip_select,
355 spi->master->num_chipselect); 358 master->num_chipselect);
356 return -EINVAL; 359 return -EINVAL;
357 } 360 }
358 361
@@ -376,6 +379,9 @@ int spi_add_device(struct spi_device *spi)
376 goto done; 379 goto done;
377 } 380 }
378 381
382 if (master->cs_gpios)
383 spi->cs_gpio = master->cs_gpios[spi->chip_select];
384
379 /* Drivers may modify this initial i/o setup, but will 385 /* Drivers may modify this initial i/o setup, but will
380 * normally rely on the device being setup. Devices 386 * normally rely on the device being setup. Devices
381 * using SPI_CS_HIGH can't coexist well otherwise... 387 * using SPI_CS_HIGH can't coexist well otherwise...
@@ -946,6 +952,44 @@ struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
946} 952}
947EXPORT_SYMBOL_GPL(spi_alloc_master); 953EXPORT_SYMBOL_GPL(spi_alloc_master);
948 954
955#ifdef CONFIG_OF
956static int of_spi_register_master(struct spi_master *master)
957{
958 u16 nb;
959 int i, *cs;
960 struct device_node *np = master->dev.of_node;
961
962 if (!np)
963 return 0;
964
965 nb = of_gpio_named_count(np, "cs-gpios");
966 master->num_chipselect = max(nb, master->num_chipselect);
967
968 if (nb < 1)
969 return 0;
970
971 cs = devm_kzalloc(&master->dev,
972 sizeof(int) * master->num_chipselect,
973 GFP_KERNEL);
974 master->cs_gpios = cs;
975
976 if (!master->cs_gpios)
977 return -ENOMEM;
978
979 memset(cs, -EINVAL, master->num_chipselect);
980
981 for (i = 0; i < nb; i++)
982 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
983
984 return 0;
985}
986#else
987static int of_spi_register_master(struct spi_master *master)
988{
989 return 0;
990}
991#endif
992
949/** 993/**
950 * spi_register_master - register SPI master controller 994 * spi_register_master - register SPI master controller
951 * @master: initialized master, originally from spi_alloc_master() 995 * @master: initialized master, originally from spi_alloc_master()
@@ -977,6 +1021,10 @@ int spi_register_master(struct spi_master *master)
977 if (!dev) 1021 if (!dev)
978 return -ENODEV; 1022 return -ENODEV;
979 1023
1024 status = of_spi_register_master(master);
1025 if (status)
1026 return status;
1027
980 /* even if it's just one always-selected device, there must 1028 /* even if it's just one always-selected device, there must
981 * be at least one chipselect 1029 * be at least one chipselect
982 */ 1030 */