18ae12a0dSDavid Brownell /* 28ae12a0dSDavid Brownell * spi.c - SPI init/core code 38ae12a0dSDavid Brownell * 48ae12a0dSDavid Brownell * Copyright (C) 2005 David Brownell 58ae12a0dSDavid Brownell * 68ae12a0dSDavid Brownell * This program is free software; you can redistribute it and/or modify 78ae12a0dSDavid Brownell * it under the terms of the GNU General Public License as published by 88ae12a0dSDavid Brownell * the Free Software Foundation; either version 2 of the License, or 98ae12a0dSDavid Brownell * (at your option) any later version. 108ae12a0dSDavid Brownell * 118ae12a0dSDavid Brownell * This program is distributed in the hope that it will be useful, 128ae12a0dSDavid Brownell * but WITHOUT ANY WARRANTY; without even the implied warranty of 138ae12a0dSDavid Brownell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 148ae12a0dSDavid Brownell * GNU General Public License for more details. 158ae12a0dSDavid Brownell * 168ae12a0dSDavid Brownell * You should have received a copy of the GNU General Public License 178ae12a0dSDavid Brownell * along with this program; if not, write to the Free Software 188ae12a0dSDavid Brownell * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 198ae12a0dSDavid Brownell */ 208ae12a0dSDavid Brownell 218ae12a0dSDavid Brownell #include <linux/kernel.h> 228ae12a0dSDavid Brownell #include <linux/device.h> 238ae12a0dSDavid Brownell #include <linux/init.h> 248ae12a0dSDavid Brownell #include <linux/cache.h> 2594040828SMatthias Kaehlcke #include <linux/mutex.h> 262b7a32f7SSinan Akman #include <linux/of_device.h> 275a0e3ad6STejun Heo #include <linux/slab.h> 28e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h> 298ae12a0dSDavid Brownell #include <linux/spi/spi.h> 3012b15e83SAnatolij Gustschin #include <linux/of_spi.h> 313ae22e8cSMark Brown #include <linux/pm_runtime.h> 328ae12a0dSDavid Brownell 338ae12a0dSDavid Brownell static void spidev_release(struct device *dev) 348ae12a0dSDavid Brownell { 350ffa0285SHans-Peter Nilsson struct spi_device *spi = to_spi_device(dev); 368ae12a0dSDavid Brownell 378ae12a0dSDavid Brownell /* spi masters may cleanup for released devices */ 388ae12a0dSDavid Brownell if (spi->master->cleanup) 398ae12a0dSDavid Brownell spi->master->cleanup(spi); 408ae12a0dSDavid Brownell 410c868461SDavid Brownell spi_master_put(spi->master); 4207a389feSRoman Tereshonkov kfree(spi); 438ae12a0dSDavid Brownell } 448ae12a0dSDavid Brownell 458ae12a0dSDavid Brownell static ssize_t 468ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 478ae12a0dSDavid Brownell { 488ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 498ae12a0dSDavid Brownell 5035f74fcaSKay Sievers return sprintf(buf, "%s\n", spi->modalias); 518ae12a0dSDavid Brownell } 528ae12a0dSDavid Brownell 538ae12a0dSDavid Brownell static struct device_attribute spi_dev_attrs[] = { 548ae12a0dSDavid Brownell __ATTR_RO(modalias), 558ae12a0dSDavid Brownell __ATTR_NULL, 568ae12a0dSDavid Brownell }; 578ae12a0dSDavid Brownell 588ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 598ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 608ae12a0dSDavid Brownell */ 618ae12a0dSDavid Brownell 6275368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 6375368bf6SAnton Vorontsov const struct spi_device *sdev) 6475368bf6SAnton Vorontsov { 6575368bf6SAnton Vorontsov while (id->name[0]) { 6675368bf6SAnton Vorontsov if (!strcmp(sdev->modalias, id->name)) 6775368bf6SAnton Vorontsov return id; 6875368bf6SAnton Vorontsov id++; 6975368bf6SAnton Vorontsov } 7075368bf6SAnton Vorontsov return NULL; 7175368bf6SAnton Vorontsov } 7275368bf6SAnton Vorontsov 7375368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 7475368bf6SAnton Vorontsov { 7575368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 7675368bf6SAnton Vorontsov 7775368bf6SAnton Vorontsov return spi_match_id(sdrv->id_table, sdev); 7875368bf6SAnton Vorontsov } 7975368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 8075368bf6SAnton Vorontsov 818ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 828ae12a0dSDavid Brownell { 838ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 8475368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 8575368bf6SAnton Vorontsov 862b7a32f7SSinan Akman /* Attempt an OF style match */ 872b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 882b7a32f7SSinan Akman return 1; 892b7a32f7SSinan Akman 9075368bf6SAnton Vorontsov if (sdrv->id_table) 9175368bf6SAnton Vorontsov return !!spi_match_id(sdrv->id_table, spi); 928ae12a0dSDavid Brownell 9335f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 948ae12a0dSDavid Brownell } 958ae12a0dSDavid Brownell 967eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 978ae12a0dSDavid Brownell { 988ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 998ae12a0dSDavid Brownell 100e0626e38SAnton Vorontsov add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 1018ae12a0dSDavid Brownell return 0; 1028ae12a0dSDavid Brownell } 1038ae12a0dSDavid Brownell 1043ae22e8cSMark Brown #ifdef CONFIG_PM_SLEEP 1053ae22e8cSMark Brown static int spi_legacy_suspend(struct device *dev, pm_message_t message) 1068ae12a0dSDavid Brownell { 1073c72426fSDavid Brownell int value = 0; 108b885244eSDavid Brownell struct spi_driver *drv = to_spi_driver(dev->driver); 1098ae12a0dSDavid Brownell 1108ae12a0dSDavid Brownell /* suspend will stop irqs and dma; no more i/o */ 1113c72426fSDavid Brownell if (drv) { 1123c72426fSDavid Brownell if (drv->suspend) 113b885244eSDavid Brownell value = drv->suspend(to_spi_device(dev), message); 1143c72426fSDavid Brownell else 1153c72426fSDavid Brownell dev_dbg(dev, "... can't suspend\n"); 1163c72426fSDavid Brownell } 1178ae12a0dSDavid Brownell return value; 1188ae12a0dSDavid Brownell } 1198ae12a0dSDavid Brownell 1203ae22e8cSMark Brown static int spi_legacy_resume(struct device *dev) 1218ae12a0dSDavid Brownell { 1223c72426fSDavid Brownell int value = 0; 123b885244eSDavid Brownell struct spi_driver *drv = to_spi_driver(dev->driver); 1248ae12a0dSDavid Brownell 1258ae12a0dSDavid Brownell /* resume may restart the i/o queue */ 1263c72426fSDavid Brownell if (drv) { 1273c72426fSDavid Brownell if (drv->resume) 128b885244eSDavid Brownell value = drv->resume(to_spi_device(dev)); 1293c72426fSDavid Brownell else 1303c72426fSDavid Brownell dev_dbg(dev, "... can't resume\n"); 1313c72426fSDavid Brownell } 1328ae12a0dSDavid Brownell return value; 1338ae12a0dSDavid Brownell } 1348ae12a0dSDavid Brownell 1353ae22e8cSMark Brown static int spi_pm_suspend(struct device *dev) 1363ae22e8cSMark Brown { 1373ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1383ae22e8cSMark Brown 1393ae22e8cSMark Brown if (pm) 1403ae22e8cSMark Brown return pm_generic_suspend(dev); 1413ae22e8cSMark Brown else 1423ae22e8cSMark Brown return spi_legacy_suspend(dev, PMSG_SUSPEND); 1433ae22e8cSMark Brown } 1443ae22e8cSMark Brown 1453ae22e8cSMark Brown static int spi_pm_resume(struct device *dev) 1463ae22e8cSMark Brown { 1473ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1483ae22e8cSMark Brown 1493ae22e8cSMark Brown if (pm) 1503ae22e8cSMark Brown return pm_generic_resume(dev); 1513ae22e8cSMark Brown else 1523ae22e8cSMark Brown return spi_legacy_resume(dev); 1533ae22e8cSMark Brown } 1543ae22e8cSMark Brown 1553ae22e8cSMark Brown static int spi_pm_freeze(struct device *dev) 1563ae22e8cSMark Brown { 1573ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1583ae22e8cSMark Brown 1593ae22e8cSMark Brown if (pm) 1603ae22e8cSMark Brown return pm_generic_freeze(dev); 1613ae22e8cSMark Brown else 1623ae22e8cSMark Brown return spi_legacy_suspend(dev, PMSG_FREEZE); 1633ae22e8cSMark Brown } 1643ae22e8cSMark Brown 1653ae22e8cSMark Brown static int spi_pm_thaw(struct device *dev) 1663ae22e8cSMark Brown { 1673ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1683ae22e8cSMark Brown 1693ae22e8cSMark Brown if (pm) 1703ae22e8cSMark Brown return pm_generic_thaw(dev); 1713ae22e8cSMark Brown else 1723ae22e8cSMark Brown return spi_legacy_resume(dev); 1733ae22e8cSMark Brown } 1743ae22e8cSMark Brown 1753ae22e8cSMark Brown static int spi_pm_poweroff(struct device *dev) 1763ae22e8cSMark Brown { 1773ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1783ae22e8cSMark Brown 1793ae22e8cSMark Brown if (pm) 1803ae22e8cSMark Brown return pm_generic_poweroff(dev); 1813ae22e8cSMark Brown else 1823ae22e8cSMark Brown return spi_legacy_suspend(dev, PMSG_HIBERNATE); 1833ae22e8cSMark Brown } 1843ae22e8cSMark Brown 1853ae22e8cSMark Brown static int spi_pm_restore(struct device *dev) 1863ae22e8cSMark Brown { 1873ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1883ae22e8cSMark Brown 1893ae22e8cSMark Brown if (pm) 1903ae22e8cSMark Brown return pm_generic_restore(dev); 1913ae22e8cSMark Brown else 1923ae22e8cSMark Brown return spi_legacy_resume(dev); 1933ae22e8cSMark Brown } 1948ae12a0dSDavid Brownell #else 1953ae22e8cSMark Brown #define spi_pm_suspend NULL 1963ae22e8cSMark Brown #define spi_pm_resume NULL 1973ae22e8cSMark Brown #define spi_pm_freeze NULL 1983ae22e8cSMark Brown #define spi_pm_thaw NULL 1993ae22e8cSMark Brown #define spi_pm_poweroff NULL 2003ae22e8cSMark Brown #define spi_pm_restore NULL 2018ae12a0dSDavid Brownell #endif 2028ae12a0dSDavid Brownell 2033ae22e8cSMark Brown static const struct dev_pm_ops spi_pm = { 2043ae22e8cSMark Brown .suspend = spi_pm_suspend, 2053ae22e8cSMark Brown .resume = spi_pm_resume, 2063ae22e8cSMark Brown .freeze = spi_pm_freeze, 2073ae22e8cSMark Brown .thaw = spi_pm_thaw, 2083ae22e8cSMark Brown .poweroff = spi_pm_poweroff, 2093ae22e8cSMark Brown .restore = spi_pm_restore, 2103ae22e8cSMark Brown SET_RUNTIME_PM_OPS( 2113ae22e8cSMark Brown pm_generic_runtime_suspend, 2123ae22e8cSMark Brown pm_generic_runtime_resume, 2133ae22e8cSMark Brown pm_generic_runtime_idle 2143ae22e8cSMark Brown ) 2153ae22e8cSMark Brown }; 2163ae22e8cSMark Brown 2178ae12a0dSDavid Brownell struct bus_type spi_bus_type = { 2188ae12a0dSDavid Brownell .name = "spi", 2198ae12a0dSDavid Brownell .dev_attrs = spi_dev_attrs, 2208ae12a0dSDavid Brownell .match = spi_match_device, 2218ae12a0dSDavid Brownell .uevent = spi_uevent, 2223ae22e8cSMark Brown .pm = &spi_pm, 2238ae12a0dSDavid Brownell }; 2248ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type); 2258ae12a0dSDavid Brownell 226b885244eSDavid Brownell 227b885244eSDavid Brownell static int spi_drv_probe(struct device *dev) 228b885244eSDavid Brownell { 229b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 230b885244eSDavid Brownell 231b885244eSDavid Brownell return sdrv->probe(to_spi_device(dev)); 232b885244eSDavid Brownell } 233b885244eSDavid Brownell 234b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 235b885244eSDavid Brownell { 236b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 237b885244eSDavid Brownell 238b885244eSDavid Brownell return sdrv->remove(to_spi_device(dev)); 239b885244eSDavid Brownell } 240b885244eSDavid Brownell 241b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 242b885244eSDavid Brownell { 243b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 244b885244eSDavid Brownell 245b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 246b885244eSDavid Brownell } 247b885244eSDavid Brownell 24833e34dc6SDavid Brownell /** 24933e34dc6SDavid Brownell * spi_register_driver - register a SPI driver 25033e34dc6SDavid Brownell * @sdrv: the driver to register 25133e34dc6SDavid Brownell * Context: can sleep 25233e34dc6SDavid Brownell */ 253b885244eSDavid Brownell int spi_register_driver(struct spi_driver *sdrv) 254b885244eSDavid Brownell { 255b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 256b885244eSDavid Brownell if (sdrv->probe) 257b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 258b885244eSDavid Brownell if (sdrv->remove) 259b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 260b885244eSDavid Brownell if (sdrv->shutdown) 261b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 262b885244eSDavid Brownell return driver_register(&sdrv->driver); 263b885244eSDavid Brownell } 264b885244eSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_driver); 265b885244eSDavid Brownell 2668ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 2678ae12a0dSDavid Brownell 2688ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 2698ae12a0dSDavid Brownell * would make them board-specific. Similarly with SPI master drivers. 2708ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 2718ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 2728ae12a0dSDavid Brownell */ 2738ae12a0dSDavid Brownell 2748ae12a0dSDavid Brownell struct boardinfo { 2758ae12a0dSDavid Brownell struct list_head list; 2762b9603a0SFeng Tang struct spi_board_info board_info; 2778ae12a0dSDavid Brownell }; 2788ae12a0dSDavid Brownell 2798ae12a0dSDavid Brownell static LIST_HEAD(board_list); 2802b9603a0SFeng Tang static LIST_HEAD(spi_master_list); 2812b9603a0SFeng Tang 2822b9603a0SFeng Tang /* 2832b9603a0SFeng Tang * Used to protect add/del opertion for board_info list and 2842b9603a0SFeng Tang * spi_master list, and their matching process 2852b9603a0SFeng Tang */ 28694040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 2878ae12a0dSDavid Brownell 288dc87c98eSGrant Likely /** 289dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 290dc87c98eSGrant Likely * @master: Controller to which device is connected 291dc87c98eSGrant Likely * Context: can sleep 292dc87c98eSGrant Likely * 293dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 294dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 295dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 296dc87c98eSGrant Likely * spi_add_device() on it. 297dc87c98eSGrant Likely * 298dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 299dc87c98eSGrant Likely * spi_device structure to add it to the SPI master. If the caller 300dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 301dc87c98eSGrant Likely * call spi_dev_put() on it. 302dc87c98eSGrant Likely * 303dc87c98eSGrant Likely * Returns a pointer to the new device, or NULL. 304dc87c98eSGrant Likely */ 305dc87c98eSGrant Likely struct spi_device *spi_alloc_device(struct spi_master *master) 306dc87c98eSGrant Likely { 307dc87c98eSGrant Likely struct spi_device *spi; 308dc87c98eSGrant Likely struct device *dev = master->dev.parent; 309dc87c98eSGrant Likely 310dc87c98eSGrant Likely if (!spi_master_get(master)) 311dc87c98eSGrant Likely return NULL; 312dc87c98eSGrant Likely 313dc87c98eSGrant Likely spi = kzalloc(sizeof *spi, GFP_KERNEL); 314dc87c98eSGrant Likely if (!spi) { 315dc87c98eSGrant Likely dev_err(dev, "cannot alloc spi_device\n"); 316dc87c98eSGrant Likely spi_master_put(master); 317dc87c98eSGrant Likely return NULL; 318dc87c98eSGrant Likely } 319dc87c98eSGrant Likely 320dc87c98eSGrant Likely spi->master = master; 321dc87c98eSGrant Likely spi->dev.parent = dev; 322dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 323dc87c98eSGrant Likely spi->dev.release = spidev_release; 324dc87c98eSGrant Likely device_initialize(&spi->dev); 325dc87c98eSGrant Likely return spi; 326dc87c98eSGrant Likely } 327dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device); 328dc87c98eSGrant Likely 329dc87c98eSGrant Likely /** 330dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 331dc87c98eSGrant Likely * @spi: spi_device to register 332dc87c98eSGrant Likely * 333dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 334dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 335dc87c98eSGrant Likely * 336e48880e0SDavid Brownell * Returns 0 on success; negative errno on failure 337dc87c98eSGrant Likely */ 338dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi) 339dc87c98eSGrant Likely { 340e48880e0SDavid Brownell static DEFINE_MUTEX(spi_add_lock); 341dc87c98eSGrant Likely struct device *dev = spi->master->dev.parent; 3428ec130a0SRoman Tereshonkov struct device *d; 343dc87c98eSGrant Likely int status; 344dc87c98eSGrant Likely 345dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 346dc87c98eSGrant Likely if (spi->chip_select >= spi->master->num_chipselect) { 347dc87c98eSGrant Likely dev_err(dev, "cs%d >= max %d\n", 348dc87c98eSGrant Likely spi->chip_select, 349dc87c98eSGrant Likely spi->master->num_chipselect); 350dc87c98eSGrant Likely return -EINVAL; 351dc87c98eSGrant Likely } 352dc87c98eSGrant Likely 353dc87c98eSGrant Likely /* Set the bus ID string */ 35435f74fcaSKay Sievers dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev), 355dc87c98eSGrant Likely spi->chip_select); 356dc87c98eSGrant Likely 357e48880e0SDavid Brownell 358e48880e0SDavid Brownell /* We need to make sure there's no other device with this 359e48880e0SDavid Brownell * chipselect **BEFORE** we call setup(), else we'll trash 360e48880e0SDavid Brownell * its configuration. Lock against concurrent add() calls. 361e48880e0SDavid Brownell */ 362e48880e0SDavid Brownell mutex_lock(&spi_add_lock); 363e48880e0SDavid Brownell 3648ec130a0SRoman Tereshonkov d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev)); 3658ec130a0SRoman Tereshonkov if (d != NULL) { 366e48880e0SDavid Brownell dev_err(dev, "chipselect %d already in use\n", 367e48880e0SDavid Brownell spi->chip_select); 3688ec130a0SRoman Tereshonkov put_device(d); 369e48880e0SDavid Brownell status = -EBUSY; 370e48880e0SDavid Brownell goto done; 371e48880e0SDavid Brownell } 372e48880e0SDavid Brownell 373e48880e0SDavid Brownell /* Drivers may modify this initial i/o setup, but will 374e48880e0SDavid Brownell * normally rely on the device being setup. Devices 375e48880e0SDavid Brownell * using SPI_CS_HIGH can't coexist well otherwise... 376e48880e0SDavid Brownell */ 3777d077197SDavid Brownell status = spi_setup(spi); 378dc87c98eSGrant Likely if (status < 0) { 379eb288a1fSLinus Walleij dev_err(dev, "can't setup %s, status %d\n", 380eb288a1fSLinus Walleij dev_name(&spi->dev), status); 381e48880e0SDavid Brownell goto done; 382dc87c98eSGrant Likely } 383dc87c98eSGrant Likely 384e48880e0SDavid Brownell /* Device may be bound to an active driver when this returns */ 385dc87c98eSGrant Likely status = device_add(&spi->dev); 386e48880e0SDavid Brownell if (status < 0) 387eb288a1fSLinus Walleij dev_err(dev, "can't add %s, status %d\n", 388eb288a1fSLinus Walleij dev_name(&spi->dev), status); 389e48880e0SDavid Brownell else 39035f74fcaSKay Sievers dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 391e48880e0SDavid Brownell 392e48880e0SDavid Brownell done: 393e48880e0SDavid Brownell mutex_unlock(&spi_add_lock); 394e48880e0SDavid Brownell return status; 395dc87c98eSGrant Likely } 396dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device); 3978ae12a0dSDavid Brownell 39833e34dc6SDavid Brownell /** 39933e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 40033e34dc6SDavid Brownell * @master: Controller to which device is connected 40133e34dc6SDavid Brownell * @chip: Describes the SPI device 40233e34dc6SDavid Brownell * Context: can sleep 40333e34dc6SDavid Brownell * 40433e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 4058ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 4068ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 4078ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 4088ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 409082c8cb4SDavid Brownell * 410082c8cb4SDavid Brownell * Returns the new device, or NULL. 4118ae12a0dSDavid Brownell */ 412e9d5a461SAdrian Bunk struct spi_device *spi_new_device(struct spi_master *master, 413e9d5a461SAdrian Bunk struct spi_board_info *chip) 4148ae12a0dSDavid Brownell { 4158ae12a0dSDavid Brownell struct spi_device *proxy; 4168ae12a0dSDavid Brownell int status; 4178ae12a0dSDavid Brownell 418082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 419082c8cb4SDavid Brownell * 420082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 421082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 422082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 423082c8cb4SDavid Brownell */ 424082c8cb4SDavid Brownell 425dc87c98eSGrant Likely proxy = spi_alloc_device(master); 426dc87c98eSGrant Likely if (!proxy) 4278ae12a0dSDavid Brownell return NULL; 4288ae12a0dSDavid Brownell 429102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 430102eb975SGrant Likely 4318ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 4328ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 433980a01c9SDavid Brownell proxy->mode = chip->mode; 4348ae12a0dSDavid Brownell proxy->irq = chip->irq; 435102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 4368ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 4378ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 4388ae12a0dSDavid Brownell proxy->controller_state = NULL; 4398ae12a0dSDavid Brownell 440dc87c98eSGrant Likely status = spi_add_device(proxy); 4418ae12a0dSDavid Brownell if (status < 0) { 442dc87c98eSGrant Likely spi_dev_put(proxy); 4438ae12a0dSDavid Brownell return NULL; 4448ae12a0dSDavid Brownell } 445dc87c98eSGrant Likely 446dc87c98eSGrant Likely return proxy; 447dc87c98eSGrant Likely } 4488ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 4498ae12a0dSDavid Brownell 4502b9603a0SFeng Tang static void spi_match_master_to_boardinfo(struct spi_master *master, 4512b9603a0SFeng Tang struct spi_board_info *bi) 4522b9603a0SFeng Tang { 4532b9603a0SFeng Tang struct spi_device *dev; 4542b9603a0SFeng Tang 4552b9603a0SFeng Tang if (master->bus_num != bi->bus_num) 4562b9603a0SFeng Tang return; 4572b9603a0SFeng Tang 4582b9603a0SFeng Tang dev = spi_new_device(master, bi); 4592b9603a0SFeng Tang if (!dev) 4602b9603a0SFeng Tang dev_err(master->dev.parent, "can't create new device for %s\n", 4612b9603a0SFeng Tang bi->modalias); 4622b9603a0SFeng Tang } 4632b9603a0SFeng Tang 46433e34dc6SDavid Brownell /** 46533e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 46633e34dc6SDavid Brownell * @info: array of chip descriptors 46733e34dc6SDavid Brownell * @n: how many descriptors are provided 46833e34dc6SDavid Brownell * Context: can sleep 46933e34dc6SDavid Brownell * 4708ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 4718ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 4728ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 4738ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 4748ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 4758ae12a0dSDavid Brownell * 4768ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 4778ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 4788ae12a0dSDavid Brownell * would naturally declare its SPI devices. 4798ae12a0dSDavid Brownell * 4808ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 4818ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 4828ae12a0dSDavid Brownell */ 4838ae12a0dSDavid Brownell int __init 4848ae12a0dSDavid Brownell spi_register_board_info(struct spi_board_info const *info, unsigned n) 4858ae12a0dSDavid Brownell { 4868ae12a0dSDavid Brownell struct boardinfo *bi; 4872b9603a0SFeng Tang int i; 4888ae12a0dSDavid Brownell 4892b9603a0SFeng Tang bi = kzalloc(n * sizeof(*bi), GFP_KERNEL); 4908ae12a0dSDavid Brownell if (!bi) 4918ae12a0dSDavid Brownell return -ENOMEM; 4928ae12a0dSDavid Brownell 4932b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 4942b9603a0SFeng Tang struct spi_master *master; 4952b9603a0SFeng Tang 4962b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 49794040828SMatthias Kaehlcke mutex_lock(&board_lock); 4988ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 4992b9603a0SFeng Tang list_for_each_entry(master, &spi_master_list, list) 5002b9603a0SFeng Tang spi_match_master_to_boardinfo(master, &bi->board_info); 50194040828SMatthias Kaehlcke mutex_unlock(&board_lock); 5022b9603a0SFeng Tang } 5032b9603a0SFeng Tang 5048ae12a0dSDavid Brownell return 0; 5058ae12a0dSDavid Brownell } 5068ae12a0dSDavid Brownell 5078ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 5088ae12a0dSDavid Brownell 50949dce689STony Jones static void spi_master_release(struct device *dev) 5108ae12a0dSDavid Brownell { 5118ae12a0dSDavid Brownell struct spi_master *master; 5128ae12a0dSDavid Brownell 51349dce689STony Jones master = container_of(dev, struct spi_master, dev); 5148ae12a0dSDavid Brownell kfree(master); 5158ae12a0dSDavid Brownell } 5168ae12a0dSDavid Brownell 5178ae12a0dSDavid Brownell static struct class spi_master_class = { 5188ae12a0dSDavid Brownell .name = "spi_master", 5198ae12a0dSDavid Brownell .owner = THIS_MODULE, 52049dce689STony Jones .dev_release = spi_master_release, 5218ae12a0dSDavid Brownell }; 5228ae12a0dSDavid Brownell 5238ae12a0dSDavid Brownell 5248ae12a0dSDavid Brownell /** 5258ae12a0dSDavid Brownell * spi_alloc_master - allocate SPI master controller 5268ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 52733e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 52849dce689STony Jones * memory is in the driver_data field of the returned device, 5290c868461SDavid Brownell * accessible with spi_master_get_devdata(). 53033e34dc6SDavid Brownell * Context: can sleep 5318ae12a0dSDavid Brownell * 5328ae12a0dSDavid Brownell * This call is used only by SPI master controller drivers, which are the 5338ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 534ba1a0513Sdmitry pervushin * an spi_master structure, prior to calling spi_register_master(). 5358ae12a0dSDavid Brownell * 5368ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns the SPI 5378ae12a0dSDavid Brownell * master structure on success, else NULL. 5388ae12a0dSDavid Brownell * 5398ae12a0dSDavid Brownell * The caller is responsible for assigning the bus number and initializing 540ba1a0513Sdmitry pervushin * the master's methods before calling spi_register_master(); and (after errors 5410c868461SDavid Brownell * adding the device) calling spi_master_put() to prevent a memory leak. 5428ae12a0dSDavid Brownell */ 543e9d5a461SAdrian Bunk struct spi_master *spi_alloc_master(struct device *dev, unsigned size) 5448ae12a0dSDavid Brownell { 5458ae12a0dSDavid Brownell struct spi_master *master; 5468ae12a0dSDavid Brownell 5470c868461SDavid Brownell if (!dev) 5480c868461SDavid Brownell return NULL; 5490c868461SDavid Brownell 550e94b1766SChristoph Lameter master = kzalloc(size + sizeof *master, GFP_KERNEL); 5518ae12a0dSDavid Brownell if (!master) 5528ae12a0dSDavid Brownell return NULL; 5538ae12a0dSDavid Brownell 55449dce689STony Jones device_initialize(&master->dev); 55549dce689STony Jones master->dev.class = &spi_master_class; 55649dce689STony Jones master->dev.parent = get_device(dev); 5570c868461SDavid Brownell spi_master_set_devdata(master, &master[1]); 5588ae12a0dSDavid Brownell 5598ae12a0dSDavid Brownell return master; 5608ae12a0dSDavid Brownell } 5618ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_alloc_master); 5628ae12a0dSDavid Brownell 5638ae12a0dSDavid Brownell /** 5648ae12a0dSDavid Brownell * spi_register_master - register SPI master controller 5658ae12a0dSDavid Brownell * @master: initialized master, originally from spi_alloc_master() 56633e34dc6SDavid Brownell * Context: can sleep 5678ae12a0dSDavid Brownell * 5688ae12a0dSDavid Brownell * SPI master controllers connect to their drivers using some non-SPI bus, 5698ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 5708ae12a0dSDavid Brownell * includes calling spi_register_master() to hook up to this SPI bus glue. 5718ae12a0dSDavid Brownell * 5728ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 5738ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 5748ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 5758ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 5768ae12a0dSDavid Brownell * chip is at which address. 5778ae12a0dSDavid Brownell * 5788ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 5798ae12a0dSDavid Brownell * success, else a negative error code (dropping the master's refcount). 5800c868461SDavid Brownell * After a successful return, the caller is responsible for calling 5810c868461SDavid Brownell * spi_unregister_master(). 5828ae12a0dSDavid Brownell */ 583e9d5a461SAdrian Bunk int spi_register_master(struct spi_master *master) 5848ae12a0dSDavid Brownell { 585e44a45aeSDavid Brownell static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); 58649dce689STony Jones struct device *dev = master->dev.parent; 5872b9603a0SFeng Tang struct boardinfo *bi; 5888ae12a0dSDavid Brownell int status = -ENODEV; 5898ae12a0dSDavid Brownell int dynamic = 0; 5908ae12a0dSDavid Brownell 5910c868461SDavid Brownell if (!dev) 5920c868461SDavid Brownell return -ENODEV; 5930c868461SDavid Brownell 594082c8cb4SDavid Brownell /* even if it's just one always-selected device, there must 595082c8cb4SDavid Brownell * be at least one chipselect 596082c8cb4SDavid Brownell */ 597082c8cb4SDavid Brownell if (master->num_chipselect == 0) 598082c8cb4SDavid Brownell return -EINVAL; 599082c8cb4SDavid Brownell 6008ae12a0dSDavid Brownell /* convention: dynamically assigned bus IDs count down from the max */ 601a020ed75SDavid Brownell if (master->bus_num < 0) { 602082c8cb4SDavid Brownell /* FIXME switch to an IDR based scheme, something like 603082c8cb4SDavid Brownell * I2C now uses, so we can't run out of "dynamic" IDs 604082c8cb4SDavid Brownell */ 6058ae12a0dSDavid Brownell master->bus_num = atomic_dec_return(&dyn_bus_id); 606b885244eSDavid Brownell dynamic = 1; 6078ae12a0dSDavid Brownell } 6088ae12a0dSDavid Brownell 609cf32b71eSErnst Schwab spin_lock_init(&master->bus_lock_spinlock); 610cf32b71eSErnst Schwab mutex_init(&master->bus_lock_mutex); 611cf32b71eSErnst Schwab master->bus_lock_flag = 0; 612cf32b71eSErnst Schwab 6138ae12a0dSDavid Brownell /* register the device, then userspace will see it. 6148ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 6158ae12a0dSDavid Brownell */ 61635f74fcaSKay Sievers dev_set_name(&master->dev, "spi%u", master->bus_num); 61749dce689STony Jones status = device_add(&master->dev); 618b885244eSDavid Brownell if (status < 0) 6198ae12a0dSDavid Brownell goto done; 62035f74fcaSKay Sievers dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev), 6218ae12a0dSDavid Brownell dynamic ? " (dynamic)" : ""); 6228ae12a0dSDavid Brownell 6232b9603a0SFeng Tang mutex_lock(&board_lock); 6242b9603a0SFeng Tang list_add_tail(&master->list, &spi_master_list); 6252b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 6262b9603a0SFeng Tang spi_match_master_to_boardinfo(master, &bi->board_info); 6272b9603a0SFeng Tang mutex_unlock(&board_lock); 6282b9603a0SFeng Tang 6298ae12a0dSDavid Brownell status = 0; 63012b15e83SAnatolij Gustschin 63112b15e83SAnatolij Gustschin /* Register devices from the device tree */ 63212b15e83SAnatolij Gustschin of_register_spi_devices(master); 6338ae12a0dSDavid Brownell done: 6348ae12a0dSDavid Brownell return status; 6358ae12a0dSDavid Brownell } 6368ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_master); 6378ae12a0dSDavid Brownell 6388ae12a0dSDavid Brownell 63934860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 6408ae12a0dSDavid Brownell { 6410c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 6428ae12a0dSDavid Brownell return 0; 6438ae12a0dSDavid Brownell } 6448ae12a0dSDavid Brownell 6458ae12a0dSDavid Brownell /** 6468ae12a0dSDavid Brownell * spi_unregister_master - unregister SPI master controller 6478ae12a0dSDavid Brownell * @master: the master being unregistered 64833e34dc6SDavid Brownell * Context: can sleep 6498ae12a0dSDavid Brownell * 6508ae12a0dSDavid Brownell * This call is used only by SPI master controller drivers, which are the 6518ae12a0dSDavid Brownell * only ones directly touching chip registers. 6528ae12a0dSDavid Brownell * 6538ae12a0dSDavid Brownell * This must be called from context that can sleep. 6548ae12a0dSDavid Brownell */ 6558ae12a0dSDavid Brownell void spi_unregister_master(struct spi_master *master) 6568ae12a0dSDavid Brownell { 65789fc9a1aSJeff Garzik int dummy; 65889fc9a1aSJeff Garzik 6592b9603a0SFeng Tang mutex_lock(&board_lock); 6602b9603a0SFeng Tang list_del(&master->list); 6612b9603a0SFeng Tang mutex_unlock(&board_lock); 6622b9603a0SFeng Tang 66397dbf37dSSebastian Andrzej Siewior dummy = device_for_each_child(&master->dev, NULL, __unregister); 66449dce689STony Jones device_unregister(&master->dev); 6658ae12a0dSDavid Brownell } 6668ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_unregister_master); 6678ae12a0dSDavid Brownell 6685ed2c832SDave Young static int __spi_master_match(struct device *dev, void *data) 6695ed2c832SDave Young { 6705ed2c832SDave Young struct spi_master *m; 6715ed2c832SDave Young u16 *bus_num = data; 6725ed2c832SDave Young 6735ed2c832SDave Young m = container_of(dev, struct spi_master, dev); 6745ed2c832SDave Young return m->bus_num == *bus_num; 6755ed2c832SDave Young } 6765ed2c832SDave Young 6778ae12a0dSDavid Brownell /** 6788ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 6798ae12a0dSDavid Brownell * @bus_num: the master's bus number 68033e34dc6SDavid Brownell * Context: can sleep 6818ae12a0dSDavid Brownell * 6828ae12a0dSDavid Brownell * This call may be used with devices that are registered after 6838ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 6848ae12a0dSDavid Brownell * spi_master (which the caller must release), or NULL if there is 6858ae12a0dSDavid Brownell * no such master registered. 6868ae12a0dSDavid Brownell */ 6878ae12a0dSDavid Brownell struct spi_master *spi_busnum_to_master(u16 bus_num) 6888ae12a0dSDavid Brownell { 68949dce689STony Jones struct device *dev; 6901e9a51dcSAtsushi Nemoto struct spi_master *master = NULL; 6918ae12a0dSDavid Brownell 692695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 6935ed2c832SDave Young __spi_master_match); 6945ed2c832SDave Young if (dev) 6955ed2c832SDave Young master = container_of(dev, struct spi_master, dev); 6965ed2c832SDave Young /* reference got in class_find_device */ 6971e9a51dcSAtsushi Nemoto return master; 6988ae12a0dSDavid Brownell } 6998ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 7008ae12a0dSDavid Brownell 7018ae12a0dSDavid Brownell 7028ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 7038ae12a0dSDavid Brownell 7047d077197SDavid Brownell /* Core methods for SPI master protocol drivers. Some of the 7057d077197SDavid Brownell * other core methods are currently defined as inline functions. 7067d077197SDavid Brownell */ 7077d077197SDavid Brownell 7087d077197SDavid Brownell /** 7097d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 7107d077197SDavid Brownell * @spi: the device whose settings are being modified 7117d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 7127d077197SDavid Brownell * 7137d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 7147d077197SDavid Brownell * device doesn't work with its default. They may likewise need 7157d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 7167d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 7177d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 7187d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 7197d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 7207d077197SDavid Brownell * 7217d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 7227d077197SDavid Brownell * that the underlying controller or its driver does not support. For 7237d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 7247d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 7257d077197SDavid Brownell */ 7267d077197SDavid Brownell int spi_setup(struct spi_device *spi) 7277d077197SDavid Brownell { 728e7db06b5SDavid Brownell unsigned bad_bits; 7297d077197SDavid Brownell int status; 7307d077197SDavid Brownell 731e7db06b5SDavid Brownell /* help drivers fail *cleanly* when they need options 732e7db06b5SDavid Brownell * that aren't supported with their current master 733e7db06b5SDavid Brownell */ 734e7db06b5SDavid Brownell bad_bits = spi->mode & ~spi->master->mode_bits; 735e7db06b5SDavid Brownell if (bad_bits) { 736eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 737e7db06b5SDavid Brownell bad_bits); 738e7db06b5SDavid Brownell return -EINVAL; 739e7db06b5SDavid Brownell } 740e7db06b5SDavid Brownell 7417d077197SDavid Brownell if (!spi->bits_per_word) 7427d077197SDavid Brownell spi->bits_per_word = 8; 7437d077197SDavid Brownell 7447d077197SDavid Brownell status = spi->master->setup(spi); 7457d077197SDavid Brownell 7467d077197SDavid Brownell dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s" 7477d077197SDavid Brownell "%u bits/w, %u Hz max --> %d\n", 7487d077197SDavid Brownell (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 7497d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 7507d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 7517d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 7527d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 7537d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 7547d077197SDavid Brownell status); 7557d077197SDavid Brownell 7567d077197SDavid Brownell return status; 7577d077197SDavid Brownell } 7587d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 7597d077197SDavid Brownell 760cf32b71eSErnst Schwab static int __spi_async(struct spi_device *spi, struct spi_message *message) 761cf32b71eSErnst Schwab { 762cf32b71eSErnst Schwab struct spi_master *master = spi->master; 763cf32b71eSErnst Schwab 764cf32b71eSErnst Schwab /* Half-duplex links include original MicroWire, and ones with 765cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 766cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 767cf32b71eSErnst Schwab * software limitations. 768cf32b71eSErnst Schwab */ 769cf32b71eSErnst Schwab if ((master->flags & SPI_MASTER_HALF_DUPLEX) 770cf32b71eSErnst Schwab || (spi->mode & SPI_3WIRE)) { 771cf32b71eSErnst Schwab struct spi_transfer *xfer; 772cf32b71eSErnst Schwab unsigned flags = master->flags; 773cf32b71eSErnst Schwab 774cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 775cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 776cf32b71eSErnst Schwab return -EINVAL; 777cf32b71eSErnst Schwab if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) 778cf32b71eSErnst Schwab return -EINVAL; 779cf32b71eSErnst Schwab if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf) 780cf32b71eSErnst Schwab return -EINVAL; 781cf32b71eSErnst Schwab } 782cf32b71eSErnst Schwab } 783cf32b71eSErnst Schwab 784cf32b71eSErnst Schwab message->spi = spi; 785cf32b71eSErnst Schwab message->status = -EINPROGRESS; 786cf32b71eSErnst Schwab return master->transfer(spi, message); 787cf32b71eSErnst Schwab } 788cf32b71eSErnst Schwab 789568d0697SDavid Brownell /** 790568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 791568d0697SDavid Brownell * @spi: device with which data will be exchanged 792568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 793568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 794568d0697SDavid Brownell * 795568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 796568d0697SDavid Brownell * as well as from task contexts which can sleep. 797568d0697SDavid Brownell * 798568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 799568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 800568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 801568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 802568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 803568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 804568d0697SDavid Brownell * core or controller driver code. 805568d0697SDavid Brownell * 806568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 807568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 808568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 809568d0697SDavid Brownell * time requirements, for example. 810568d0697SDavid Brownell * 811568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 812568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 813568d0697SDavid Brownell * Until returning from the associated message completion callback, 814568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 815568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 816568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 817568d0697SDavid Brownell */ 818568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 819568d0697SDavid Brownell { 820568d0697SDavid Brownell struct spi_master *master = spi->master; 821cf32b71eSErnst Schwab int ret; 822cf32b71eSErnst Schwab unsigned long flags; 823568d0697SDavid Brownell 824cf32b71eSErnst Schwab spin_lock_irqsave(&master->bus_lock_spinlock, flags); 825568d0697SDavid Brownell 826cf32b71eSErnst Schwab if (master->bus_lock_flag) 827cf32b71eSErnst Schwab ret = -EBUSY; 828cf32b71eSErnst Schwab else 829cf32b71eSErnst Schwab ret = __spi_async(spi, message); 830568d0697SDavid Brownell 831cf32b71eSErnst Schwab spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); 832cf32b71eSErnst Schwab 833cf32b71eSErnst Schwab return ret; 834568d0697SDavid Brownell } 835568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 836568d0697SDavid Brownell 837cf32b71eSErnst Schwab /** 838cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 839cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 840cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 841cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 842cf32b71eSErnst Schwab * 843cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 844cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 845cf32b71eSErnst Schwab * 846cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 847cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 848cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 849cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 850cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 851cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 852cf32b71eSErnst Schwab * core or controller driver code. 853cf32b71eSErnst Schwab * 854cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 855cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 856cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 857cf32b71eSErnst Schwab * time requirements, for example. 858cf32b71eSErnst Schwab * 859cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 860cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 861cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 862cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 863cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 864cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 865cf32b71eSErnst Schwab */ 866cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message) 867cf32b71eSErnst Schwab { 868cf32b71eSErnst Schwab struct spi_master *master = spi->master; 869cf32b71eSErnst Schwab int ret; 870cf32b71eSErnst Schwab unsigned long flags; 871cf32b71eSErnst Schwab 872cf32b71eSErnst Schwab spin_lock_irqsave(&master->bus_lock_spinlock, flags); 873cf32b71eSErnst Schwab 874cf32b71eSErnst Schwab ret = __spi_async(spi, message); 875cf32b71eSErnst Schwab 876cf32b71eSErnst Schwab spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); 877cf32b71eSErnst Schwab 878cf32b71eSErnst Schwab return ret; 879cf32b71eSErnst Schwab 880cf32b71eSErnst Schwab } 881cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked); 882cf32b71eSErnst Schwab 8837d077197SDavid Brownell 8847d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 8857d077197SDavid Brownell 8867d077197SDavid Brownell /* Utility methods for SPI master protocol drivers, layered on 8877d077197SDavid Brownell * top of the core. Some other utility methods are defined as 8887d077197SDavid Brownell * inline functions. 8897d077197SDavid Brownell */ 8907d077197SDavid Brownell 8915d870c8eSAndrew Morton static void spi_complete(void *arg) 8925d870c8eSAndrew Morton { 8935d870c8eSAndrew Morton complete(arg); 8945d870c8eSAndrew Morton } 8955d870c8eSAndrew Morton 896cf32b71eSErnst Schwab static int __spi_sync(struct spi_device *spi, struct spi_message *message, 897cf32b71eSErnst Schwab int bus_locked) 898cf32b71eSErnst Schwab { 899cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 900cf32b71eSErnst Schwab int status; 901cf32b71eSErnst Schwab struct spi_master *master = spi->master; 902cf32b71eSErnst Schwab 903cf32b71eSErnst Schwab message->complete = spi_complete; 904cf32b71eSErnst Schwab message->context = &done; 905cf32b71eSErnst Schwab 906cf32b71eSErnst Schwab if (!bus_locked) 907cf32b71eSErnst Schwab mutex_lock(&master->bus_lock_mutex); 908cf32b71eSErnst Schwab 909cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 910cf32b71eSErnst Schwab 911cf32b71eSErnst Schwab if (!bus_locked) 912cf32b71eSErnst Schwab mutex_unlock(&master->bus_lock_mutex); 913cf32b71eSErnst Schwab 914cf32b71eSErnst Schwab if (status == 0) { 915cf32b71eSErnst Schwab wait_for_completion(&done); 916cf32b71eSErnst Schwab status = message->status; 917cf32b71eSErnst Schwab } 918cf32b71eSErnst Schwab message->context = NULL; 919cf32b71eSErnst Schwab return status; 920cf32b71eSErnst Schwab } 921cf32b71eSErnst Schwab 9228ae12a0dSDavid Brownell /** 9238ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 9248ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 9258ae12a0dSDavid Brownell * @message: describes the data transfers 92633e34dc6SDavid Brownell * Context: can sleep 9278ae12a0dSDavid Brownell * 9288ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 9298ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 9308ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 9318ae12a0dSDavid Brownell * 9328ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 9338ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 9348ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 9358ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 9368ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 9378ae12a0dSDavid Brownell * 9380c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 9390c868461SDavid Brownell * message will not be freed before this call returns. 9400c868461SDavid Brownell * 9419b938b74SMarc Pignat * It returns zero on success, else a negative error code. 9428ae12a0dSDavid Brownell */ 9438ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 9448ae12a0dSDavid Brownell { 945cf32b71eSErnst Schwab return __spi_sync(spi, message, 0); 9468ae12a0dSDavid Brownell } 9478ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 9488ae12a0dSDavid Brownell 949cf32b71eSErnst Schwab /** 950cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 951cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 952cf32b71eSErnst Schwab * @message: describes the data transfers 953cf32b71eSErnst Schwab * Context: can sleep 954cf32b71eSErnst Schwab * 955cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 956cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 957cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 958cf32b71eSErnst Schwab * 959cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 960*25985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 961cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 962cf32b71eSErnst Schwab * 963cf32b71eSErnst Schwab * It returns zero on success, else a negative error code. 964cf32b71eSErnst Schwab */ 965cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 966cf32b71eSErnst Schwab { 967cf32b71eSErnst Schwab return __spi_sync(spi, message, 1); 968cf32b71eSErnst Schwab } 969cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 970cf32b71eSErnst Schwab 971cf32b71eSErnst Schwab /** 972cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 973cf32b71eSErnst Schwab * @master: SPI bus master that should be locked for exclusive bus access 974cf32b71eSErnst Schwab * Context: can sleep 975cf32b71eSErnst Schwab * 976cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 977cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 978cf32b71eSErnst Schwab * 979cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 980cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 981cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 982cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 983cf32b71eSErnst Schwab * 984cf32b71eSErnst Schwab * It returns zero on success, else a negative error code. 985cf32b71eSErnst Schwab */ 986cf32b71eSErnst Schwab int spi_bus_lock(struct spi_master *master) 987cf32b71eSErnst Schwab { 988cf32b71eSErnst Schwab unsigned long flags; 989cf32b71eSErnst Schwab 990cf32b71eSErnst Schwab mutex_lock(&master->bus_lock_mutex); 991cf32b71eSErnst Schwab 992cf32b71eSErnst Schwab spin_lock_irqsave(&master->bus_lock_spinlock, flags); 993cf32b71eSErnst Schwab master->bus_lock_flag = 1; 994cf32b71eSErnst Schwab spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); 995cf32b71eSErnst Schwab 996cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 997cf32b71eSErnst Schwab 998cf32b71eSErnst Schwab return 0; 999cf32b71eSErnst Schwab } 1000cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 1001cf32b71eSErnst Schwab 1002cf32b71eSErnst Schwab /** 1003cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 1004cf32b71eSErnst Schwab * @master: SPI bus master that was locked for exclusive bus access 1005cf32b71eSErnst Schwab * Context: can sleep 1006cf32b71eSErnst Schwab * 1007cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 1008cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 1009cf32b71eSErnst Schwab * 1010cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 1011cf32b71eSErnst Schwab * call. 1012cf32b71eSErnst Schwab * 1013cf32b71eSErnst Schwab * It returns zero on success, else a negative error code. 1014cf32b71eSErnst Schwab */ 1015cf32b71eSErnst Schwab int spi_bus_unlock(struct spi_master *master) 1016cf32b71eSErnst Schwab { 1017cf32b71eSErnst Schwab master->bus_lock_flag = 0; 1018cf32b71eSErnst Schwab 1019cf32b71eSErnst Schwab mutex_unlock(&master->bus_lock_mutex); 1020cf32b71eSErnst Schwab 1021cf32b71eSErnst Schwab return 0; 1022cf32b71eSErnst Schwab } 1023cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 1024cf32b71eSErnst Schwab 1025a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 1026a9948b61SDavid Brownell #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) 10278ae12a0dSDavid Brownell 10288ae12a0dSDavid Brownell static u8 *buf; 10298ae12a0dSDavid Brownell 10308ae12a0dSDavid Brownell /** 10318ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 10328ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 10338ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 10348ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 103527570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 103627570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 103733e34dc6SDavid Brownell * Context: can sleep 10388ae12a0dSDavid Brownell * 10398ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 10408ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 10418ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 1042b885244eSDavid Brownell * This call may only be used from a context that may sleep. 10438ae12a0dSDavid Brownell * 10440c868461SDavid Brownell * Parameters to this routine are always copied using a small buffer; 104533e34dc6SDavid Brownell * portable code should never use this for more than 32 bytes. 104633e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 10470c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 10488ae12a0dSDavid Brownell */ 10498ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 10508ae12a0dSDavid Brownell const u8 *txbuf, unsigned n_tx, 10518ae12a0dSDavid Brownell u8 *rxbuf, unsigned n_rx) 10528ae12a0dSDavid Brownell { 1053068f4070SDavid Brownell static DEFINE_MUTEX(lock); 10548ae12a0dSDavid Brownell 10558ae12a0dSDavid Brownell int status; 10568ae12a0dSDavid Brownell struct spi_message message; 1057bdff549eSDavid Brownell struct spi_transfer x[2]; 10588ae12a0dSDavid Brownell u8 *local_buf; 10598ae12a0dSDavid Brownell 10608ae12a0dSDavid Brownell /* Use preallocated DMA-safe buffer. We can't avoid copying here, 10618ae12a0dSDavid Brownell * (as a pure convenience thing), but we can keep heap costs 10628ae12a0dSDavid Brownell * out of the hot path ... 10638ae12a0dSDavid Brownell */ 10648ae12a0dSDavid Brownell if ((n_tx + n_rx) > SPI_BUFSIZ) 10658ae12a0dSDavid Brownell return -EINVAL; 10668ae12a0dSDavid Brownell 10678275c642SVitaly Wool spi_message_init(&message); 1068bdff549eSDavid Brownell memset(x, 0, sizeof x); 1069bdff549eSDavid Brownell if (n_tx) { 1070bdff549eSDavid Brownell x[0].len = n_tx; 1071bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 1072bdff549eSDavid Brownell } 1073bdff549eSDavid Brownell if (n_rx) { 1074bdff549eSDavid Brownell x[1].len = n_rx; 1075bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 1076bdff549eSDavid Brownell } 10778275c642SVitaly Wool 10788ae12a0dSDavid Brownell /* ... unless someone else is using the pre-allocated buffer */ 1079068f4070SDavid Brownell if (!mutex_trylock(&lock)) { 10808ae12a0dSDavid Brownell local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 10818ae12a0dSDavid Brownell if (!local_buf) 10828ae12a0dSDavid Brownell return -ENOMEM; 10838ae12a0dSDavid Brownell } else 10848ae12a0dSDavid Brownell local_buf = buf; 10858ae12a0dSDavid Brownell 10868ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 1087bdff549eSDavid Brownell x[0].tx_buf = local_buf; 1088bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 10898ae12a0dSDavid Brownell 10908ae12a0dSDavid Brownell /* do the i/o */ 10918ae12a0dSDavid Brownell status = spi_sync(spi, &message); 10929b938b74SMarc Pignat if (status == 0) 1093bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 10948ae12a0dSDavid Brownell 1095bdff549eSDavid Brownell if (x[0].tx_buf == buf) 1096068f4070SDavid Brownell mutex_unlock(&lock); 10978ae12a0dSDavid Brownell else 10988ae12a0dSDavid Brownell kfree(local_buf); 10998ae12a0dSDavid Brownell 11008ae12a0dSDavid Brownell return status; 11018ae12a0dSDavid Brownell } 11028ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 11038ae12a0dSDavid Brownell 11048ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 11058ae12a0dSDavid Brownell 11068ae12a0dSDavid Brownell static int __init spi_init(void) 11078ae12a0dSDavid Brownell { 1108b885244eSDavid Brownell int status; 11098ae12a0dSDavid Brownell 1110e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 1111b885244eSDavid Brownell if (!buf) { 1112b885244eSDavid Brownell status = -ENOMEM; 1113b885244eSDavid Brownell goto err0; 11148ae12a0dSDavid Brownell } 1115b885244eSDavid Brownell 1116b885244eSDavid Brownell status = bus_register(&spi_bus_type); 1117b885244eSDavid Brownell if (status < 0) 1118b885244eSDavid Brownell goto err1; 1119b885244eSDavid Brownell 1120b885244eSDavid Brownell status = class_register(&spi_master_class); 1121b885244eSDavid Brownell if (status < 0) 1122b885244eSDavid Brownell goto err2; 1123b885244eSDavid Brownell return 0; 1124b885244eSDavid Brownell 1125b885244eSDavid Brownell err2: 1126b885244eSDavid Brownell bus_unregister(&spi_bus_type); 1127b885244eSDavid Brownell err1: 1128b885244eSDavid Brownell kfree(buf); 1129b885244eSDavid Brownell buf = NULL; 1130b885244eSDavid Brownell err0: 1131b885244eSDavid Brownell return status; 1132b885244eSDavid Brownell } 1133b885244eSDavid Brownell 11348ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 11358ae12a0dSDavid Brownell * but even essential drivers wait till later 1136b885244eSDavid Brownell * 1137b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 1138b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 1139b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 11408ae12a0dSDavid Brownell */ 1141673c0c00SDavid Brownell postcore_initcall(spi_init); 11428ae12a0dSDavid Brownell 1143