18ae12a0dSDavid Brownell /* 2ca632f55SGrant Likely * SPI init/core code 38ae12a0dSDavid Brownell * 48ae12a0dSDavid Brownell * Copyright (C) 2005 David Brownell 5d57a4282SGrant Likely * Copyright (C) 2008 Secret Lab Technologies Ltd. 68ae12a0dSDavid Brownell * 78ae12a0dSDavid Brownell * This program is free software; you can redistribute it and/or modify 88ae12a0dSDavid Brownell * it under the terms of the GNU General Public License as published by 98ae12a0dSDavid Brownell * the Free Software Foundation; either version 2 of the License, or 108ae12a0dSDavid Brownell * (at your option) any later version. 118ae12a0dSDavid Brownell * 128ae12a0dSDavid Brownell * This program is distributed in the hope that it will be useful, 138ae12a0dSDavid Brownell * but WITHOUT ANY WARRANTY; without even the implied warranty of 148ae12a0dSDavid Brownell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158ae12a0dSDavid Brownell * GNU General Public License for more details. 168ae12a0dSDavid Brownell * 178ae12a0dSDavid Brownell * You should have received a copy of the GNU General Public License 188ae12a0dSDavid Brownell * along with this program; if not, write to the Free Software 198ae12a0dSDavid Brownell * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 208ae12a0dSDavid Brownell */ 218ae12a0dSDavid Brownell 228ae12a0dSDavid Brownell #include <linux/kernel.h> 23d57a4282SGrant Likely #include <linux/kmod.h> 248ae12a0dSDavid Brownell #include <linux/device.h> 258ae12a0dSDavid Brownell #include <linux/init.h> 268ae12a0dSDavid Brownell #include <linux/cache.h> 2794040828SMatthias Kaehlcke #include <linux/mutex.h> 282b7a32f7SSinan Akman #include <linux/of_device.h> 29d57a4282SGrant Likely #include <linux/of_irq.h> 305a0e3ad6STejun Heo #include <linux/slab.h> 31e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h> 328ae12a0dSDavid Brownell #include <linux/spi/spi.h> 3374317984SJean-Christophe PLAGNIOL-VILLARD #include <linux/of_gpio.h> 343ae22e8cSMark Brown #include <linux/pm_runtime.h> 35025ed130SPaul Gortmaker #include <linux/export.h> 36ffbbdd21SLinus Walleij #include <linux/sched.h> 37ffbbdd21SLinus Walleij #include <linux/delay.h> 38ffbbdd21SLinus Walleij #include <linux/kthread.h> 398ae12a0dSDavid Brownell 408ae12a0dSDavid Brownell static void spidev_release(struct device *dev) 418ae12a0dSDavid Brownell { 420ffa0285SHans-Peter Nilsson struct spi_device *spi = to_spi_device(dev); 438ae12a0dSDavid Brownell 448ae12a0dSDavid Brownell /* spi masters may cleanup for released devices */ 458ae12a0dSDavid Brownell if (spi->master->cleanup) 468ae12a0dSDavid Brownell spi->master->cleanup(spi); 478ae12a0dSDavid Brownell 480c868461SDavid Brownell spi_master_put(spi->master); 4907a389feSRoman Tereshonkov kfree(spi); 508ae12a0dSDavid Brownell } 518ae12a0dSDavid Brownell 528ae12a0dSDavid Brownell static ssize_t 538ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 548ae12a0dSDavid Brownell { 558ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 568ae12a0dSDavid Brownell 57d8e328b3SGrant Likely return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 588ae12a0dSDavid Brownell } 598ae12a0dSDavid Brownell 608ae12a0dSDavid Brownell static struct device_attribute spi_dev_attrs[] = { 618ae12a0dSDavid Brownell __ATTR_RO(modalias), 628ae12a0dSDavid Brownell __ATTR_NULL, 638ae12a0dSDavid Brownell }; 648ae12a0dSDavid Brownell 658ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 668ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 678ae12a0dSDavid Brownell */ 688ae12a0dSDavid Brownell 6975368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 7075368bf6SAnton Vorontsov const struct spi_device *sdev) 7175368bf6SAnton Vorontsov { 7275368bf6SAnton Vorontsov while (id->name[0]) { 7375368bf6SAnton Vorontsov if (!strcmp(sdev->modalias, id->name)) 7475368bf6SAnton Vorontsov return id; 7575368bf6SAnton Vorontsov id++; 7675368bf6SAnton Vorontsov } 7775368bf6SAnton Vorontsov return NULL; 7875368bf6SAnton Vorontsov } 7975368bf6SAnton Vorontsov 8075368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 8175368bf6SAnton Vorontsov { 8275368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 8375368bf6SAnton Vorontsov 8475368bf6SAnton Vorontsov return spi_match_id(sdrv->id_table, sdev); 8575368bf6SAnton Vorontsov } 8675368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 8775368bf6SAnton Vorontsov 888ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 898ae12a0dSDavid Brownell { 908ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 9175368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 9275368bf6SAnton Vorontsov 932b7a32f7SSinan Akman /* Attempt an OF style match */ 942b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 952b7a32f7SSinan Akman return 1; 962b7a32f7SSinan Akman 9775368bf6SAnton Vorontsov if (sdrv->id_table) 9875368bf6SAnton Vorontsov return !!spi_match_id(sdrv->id_table, spi); 998ae12a0dSDavid Brownell 10035f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 1018ae12a0dSDavid Brownell } 1028ae12a0dSDavid Brownell 1037eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 1048ae12a0dSDavid Brownell { 1058ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 1068ae12a0dSDavid Brownell 107e0626e38SAnton Vorontsov add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 1088ae12a0dSDavid Brownell return 0; 1098ae12a0dSDavid Brownell } 1108ae12a0dSDavid Brownell 1113ae22e8cSMark Brown #ifdef CONFIG_PM_SLEEP 1123ae22e8cSMark Brown static int spi_legacy_suspend(struct device *dev, pm_message_t message) 1138ae12a0dSDavid Brownell { 1143c72426fSDavid Brownell int value = 0; 115b885244eSDavid Brownell struct spi_driver *drv = to_spi_driver(dev->driver); 1168ae12a0dSDavid Brownell 1178ae12a0dSDavid Brownell /* suspend will stop irqs and dma; no more i/o */ 1183c72426fSDavid Brownell if (drv) { 1193c72426fSDavid Brownell if (drv->suspend) 120b885244eSDavid Brownell value = drv->suspend(to_spi_device(dev), message); 1213c72426fSDavid Brownell else 1223c72426fSDavid Brownell dev_dbg(dev, "... can't suspend\n"); 1233c72426fSDavid Brownell } 1248ae12a0dSDavid Brownell return value; 1258ae12a0dSDavid Brownell } 1268ae12a0dSDavid Brownell 1273ae22e8cSMark Brown static int spi_legacy_resume(struct device *dev) 1288ae12a0dSDavid Brownell { 1293c72426fSDavid Brownell int value = 0; 130b885244eSDavid Brownell struct spi_driver *drv = to_spi_driver(dev->driver); 1318ae12a0dSDavid Brownell 1328ae12a0dSDavid Brownell /* resume may restart the i/o queue */ 1333c72426fSDavid Brownell if (drv) { 1343c72426fSDavid Brownell if (drv->resume) 135b885244eSDavid Brownell value = drv->resume(to_spi_device(dev)); 1363c72426fSDavid Brownell else 1373c72426fSDavid Brownell dev_dbg(dev, "... can't resume\n"); 1383c72426fSDavid Brownell } 1398ae12a0dSDavid Brownell return value; 1408ae12a0dSDavid Brownell } 1418ae12a0dSDavid Brownell 1423ae22e8cSMark Brown static int spi_pm_suspend(struct device *dev) 1433ae22e8cSMark Brown { 1443ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1453ae22e8cSMark Brown 1463ae22e8cSMark Brown if (pm) 1473ae22e8cSMark Brown return pm_generic_suspend(dev); 1483ae22e8cSMark Brown else 1493ae22e8cSMark Brown return spi_legacy_suspend(dev, PMSG_SUSPEND); 1503ae22e8cSMark Brown } 1513ae22e8cSMark Brown 1523ae22e8cSMark Brown static int spi_pm_resume(struct device *dev) 1533ae22e8cSMark Brown { 1543ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1553ae22e8cSMark Brown 1563ae22e8cSMark Brown if (pm) 1573ae22e8cSMark Brown return pm_generic_resume(dev); 1583ae22e8cSMark Brown else 1593ae22e8cSMark Brown return spi_legacy_resume(dev); 1603ae22e8cSMark Brown } 1613ae22e8cSMark Brown 1623ae22e8cSMark Brown static int spi_pm_freeze(struct device *dev) 1633ae22e8cSMark Brown { 1643ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1653ae22e8cSMark Brown 1663ae22e8cSMark Brown if (pm) 1673ae22e8cSMark Brown return pm_generic_freeze(dev); 1683ae22e8cSMark Brown else 1693ae22e8cSMark Brown return spi_legacy_suspend(dev, PMSG_FREEZE); 1703ae22e8cSMark Brown } 1713ae22e8cSMark Brown 1723ae22e8cSMark Brown static int spi_pm_thaw(struct device *dev) 1733ae22e8cSMark Brown { 1743ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1753ae22e8cSMark Brown 1763ae22e8cSMark Brown if (pm) 1773ae22e8cSMark Brown return pm_generic_thaw(dev); 1783ae22e8cSMark Brown else 1793ae22e8cSMark Brown return spi_legacy_resume(dev); 1803ae22e8cSMark Brown } 1813ae22e8cSMark Brown 1823ae22e8cSMark Brown static int spi_pm_poweroff(struct device *dev) 1833ae22e8cSMark Brown { 1843ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1853ae22e8cSMark Brown 1863ae22e8cSMark Brown if (pm) 1873ae22e8cSMark Brown return pm_generic_poweroff(dev); 1883ae22e8cSMark Brown else 1893ae22e8cSMark Brown return spi_legacy_suspend(dev, PMSG_HIBERNATE); 1903ae22e8cSMark Brown } 1913ae22e8cSMark Brown 1923ae22e8cSMark Brown static int spi_pm_restore(struct device *dev) 1933ae22e8cSMark Brown { 1943ae22e8cSMark Brown const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1953ae22e8cSMark Brown 1963ae22e8cSMark Brown if (pm) 1973ae22e8cSMark Brown return pm_generic_restore(dev); 1983ae22e8cSMark Brown else 1993ae22e8cSMark Brown return spi_legacy_resume(dev); 2003ae22e8cSMark Brown } 2018ae12a0dSDavid Brownell #else 2023ae22e8cSMark Brown #define spi_pm_suspend NULL 2033ae22e8cSMark Brown #define spi_pm_resume NULL 2043ae22e8cSMark Brown #define spi_pm_freeze NULL 2053ae22e8cSMark Brown #define spi_pm_thaw NULL 2063ae22e8cSMark Brown #define spi_pm_poweroff NULL 2073ae22e8cSMark Brown #define spi_pm_restore NULL 2088ae12a0dSDavid Brownell #endif 2098ae12a0dSDavid Brownell 2103ae22e8cSMark Brown static const struct dev_pm_ops spi_pm = { 2113ae22e8cSMark Brown .suspend = spi_pm_suspend, 2123ae22e8cSMark Brown .resume = spi_pm_resume, 2133ae22e8cSMark Brown .freeze = spi_pm_freeze, 2143ae22e8cSMark Brown .thaw = spi_pm_thaw, 2153ae22e8cSMark Brown .poweroff = spi_pm_poweroff, 2163ae22e8cSMark Brown .restore = spi_pm_restore, 2173ae22e8cSMark Brown SET_RUNTIME_PM_OPS( 2183ae22e8cSMark Brown pm_generic_runtime_suspend, 2193ae22e8cSMark Brown pm_generic_runtime_resume, 2203ae22e8cSMark Brown pm_generic_runtime_idle 2213ae22e8cSMark Brown ) 2223ae22e8cSMark Brown }; 2233ae22e8cSMark Brown 2248ae12a0dSDavid Brownell struct bus_type spi_bus_type = { 2258ae12a0dSDavid Brownell .name = "spi", 2268ae12a0dSDavid Brownell .dev_attrs = spi_dev_attrs, 2278ae12a0dSDavid Brownell .match = spi_match_device, 2288ae12a0dSDavid Brownell .uevent = spi_uevent, 2293ae22e8cSMark Brown .pm = &spi_pm, 2308ae12a0dSDavid Brownell }; 2318ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type); 2328ae12a0dSDavid Brownell 233b885244eSDavid Brownell 234b885244eSDavid Brownell static int spi_drv_probe(struct device *dev) 235b885244eSDavid Brownell { 236b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 237b885244eSDavid Brownell 238b885244eSDavid Brownell return sdrv->probe(to_spi_device(dev)); 239b885244eSDavid Brownell } 240b885244eSDavid Brownell 241b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 242b885244eSDavid Brownell { 243b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 244b885244eSDavid Brownell 245b885244eSDavid Brownell return sdrv->remove(to_spi_device(dev)); 246b885244eSDavid Brownell } 247b885244eSDavid Brownell 248b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 249b885244eSDavid Brownell { 250b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 251b885244eSDavid Brownell 252b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 253b885244eSDavid Brownell } 254b885244eSDavid Brownell 25533e34dc6SDavid Brownell /** 25633e34dc6SDavid Brownell * spi_register_driver - register a SPI driver 25733e34dc6SDavid Brownell * @sdrv: the driver to register 25833e34dc6SDavid Brownell * Context: can sleep 25933e34dc6SDavid Brownell */ 260b885244eSDavid Brownell int spi_register_driver(struct spi_driver *sdrv) 261b885244eSDavid Brownell { 262b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 263b885244eSDavid Brownell if (sdrv->probe) 264b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 265b885244eSDavid Brownell if (sdrv->remove) 266b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 267b885244eSDavid Brownell if (sdrv->shutdown) 268b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 269b885244eSDavid Brownell return driver_register(&sdrv->driver); 270b885244eSDavid Brownell } 271b885244eSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_driver); 272b885244eSDavid Brownell 2738ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 2748ae12a0dSDavid Brownell 2758ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 2768ae12a0dSDavid Brownell * would make them board-specific. Similarly with SPI master drivers. 2778ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 2788ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 2798ae12a0dSDavid Brownell */ 2808ae12a0dSDavid Brownell 2818ae12a0dSDavid Brownell struct boardinfo { 2828ae12a0dSDavid Brownell struct list_head list; 2832b9603a0SFeng Tang struct spi_board_info board_info; 2848ae12a0dSDavid Brownell }; 2858ae12a0dSDavid Brownell 2868ae12a0dSDavid Brownell static LIST_HEAD(board_list); 2872b9603a0SFeng Tang static LIST_HEAD(spi_master_list); 2882b9603a0SFeng Tang 2892b9603a0SFeng Tang /* 2902b9603a0SFeng Tang * Used to protect add/del opertion for board_info list and 2912b9603a0SFeng Tang * spi_master list, and their matching process 2922b9603a0SFeng Tang */ 29394040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 2948ae12a0dSDavid Brownell 295dc87c98eSGrant Likely /** 296dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 297dc87c98eSGrant Likely * @master: Controller to which device is connected 298dc87c98eSGrant Likely * Context: can sleep 299dc87c98eSGrant Likely * 300dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 301dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 302dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 303dc87c98eSGrant Likely * spi_add_device() on it. 304dc87c98eSGrant Likely * 305dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 306dc87c98eSGrant Likely * spi_device structure to add it to the SPI master. If the caller 307dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 308dc87c98eSGrant Likely * call spi_dev_put() on it. 309dc87c98eSGrant Likely * 310dc87c98eSGrant Likely * Returns a pointer to the new device, or NULL. 311dc87c98eSGrant Likely */ 312dc87c98eSGrant Likely struct spi_device *spi_alloc_device(struct spi_master *master) 313dc87c98eSGrant Likely { 314dc87c98eSGrant Likely struct spi_device *spi; 315dc87c98eSGrant Likely struct device *dev = master->dev.parent; 316dc87c98eSGrant Likely 317dc87c98eSGrant Likely if (!spi_master_get(master)) 318dc87c98eSGrant Likely return NULL; 319dc87c98eSGrant Likely 320dc87c98eSGrant Likely spi = kzalloc(sizeof *spi, GFP_KERNEL); 321dc87c98eSGrant Likely if (!spi) { 322dc87c98eSGrant Likely dev_err(dev, "cannot alloc spi_device\n"); 323dc87c98eSGrant Likely spi_master_put(master); 324dc87c98eSGrant Likely return NULL; 325dc87c98eSGrant Likely } 326dc87c98eSGrant Likely 327dc87c98eSGrant Likely spi->master = master; 328178db7d3SLaurent Pinchart spi->dev.parent = &master->dev; 329dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 330dc87c98eSGrant Likely spi->dev.release = spidev_release; 33174317984SJean-Christophe PLAGNIOL-VILLARD spi->cs_gpio = -EINVAL; 332dc87c98eSGrant Likely device_initialize(&spi->dev); 333dc87c98eSGrant Likely return spi; 334dc87c98eSGrant Likely } 335dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device); 336dc87c98eSGrant Likely 337dc87c98eSGrant Likely /** 338dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 339dc87c98eSGrant Likely * @spi: spi_device to register 340dc87c98eSGrant Likely * 341dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 342dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 343dc87c98eSGrant Likely * 344e48880e0SDavid Brownell * Returns 0 on success; negative errno on failure 345dc87c98eSGrant Likely */ 346dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi) 347dc87c98eSGrant Likely { 348e48880e0SDavid Brownell static DEFINE_MUTEX(spi_add_lock); 34974317984SJean-Christophe PLAGNIOL-VILLARD struct spi_master *master = spi->master; 35074317984SJean-Christophe PLAGNIOL-VILLARD struct device *dev = master->dev.parent; 3518ec130a0SRoman Tereshonkov struct device *d; 352dc87c98eSGrant Likely int status; 353dc87c98eSGrant Likely 354dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 35574317984SJean-Christophe PLAGNIOL-VILLARD if (spi->chip_select >= master->num_chipselect) { 356dc87c98eSGrant Likely dev_err(dev, "cs%d >= max %d\n", 357dc87c98eSGrant Likely spi->chip_select, 35874317984SJean-Christophe PLAGNIOL-VILLARD master->num_chipselect); 359dc87c98eSGrant Likely return -EINVAL; 360dc87c98eSGrant Likely } 361dc87c98eSGrant Likely 362dc87c98eSGrant Likely /* Set the bus ID string */ 36335f74fcaSKay Sievers dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev), 364dc87c98eSGrant Likely spi->chip_select); 365dc87c98eSGrant Likely 366e48880e0SDavid Brownell 367e48880e0SDavid Brownell /* We need to make sure there's no other device with this 368e48880e0SDavid Brownell * chipselect **BEFORE** we call setup(), else we'll trash 369e48880e0SDavid Brownell * its configuration. Lock against concurrent add() calls. 370e48880e0SDavid Brownell */ 371e48880e0SDavid Brownell mutex_lock(&spi_add_lock); 372e48880e0SDavid Brownell 3738ec130a0SRoman Tereshonkov d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev)); 3748ec130a0SRoman Tereshonkov if (d != NULL) { 375e48880e0SDavid Brownell dev_err(dev, "chipselect %d already in use\n", 376e48880e0SDavid Brownell spi->chip_select); 3778ec130a0SRoman Tereshonkov put_device(d); 378e48880e0SDavid Brownell status = -EBUSY; 379e48880e0SDavid Brownell goto done; 380e48880e0SDavid Brownell } 381e48880e0SDavid Brownell 38274317984SJean-Christophe PLAGNIOL-VILLARD if (master->cs_gpios) 38374317984SJean-Christophe PLAGNIOL-VILLARD spi->cs_gpio = master->cs_gpios[spi->chip_select]; 38474317984SJean-Christophe PLAGNIOL-VILLARD 385e48880e0SDavid Brownell /* Drivers may modify this initial i/o setup, but will 386e48880e0SDavid Brownell * normally rely on the device being setup. Devices 387e48880e0SDavid Brownell * using SPI_CS_HIGH can't coexist well otherwise... 388e48880e0SDavid Brownell */ 3897d077197SDavid Brownell status = spi_setup(spi); 390dc87c98eSGrant Likely if (status < 0) { 391eb288a1fSLinus Walleij dev_err(dev, "can't setup %s, status %d\n", 392eb288a1fSLinus Walleij dev_name(&spi->dev), status); 393e48880e0SDavid Brownell goto done; 394dc87c98eSGrant Likely } 395dc87c98eSGrant Likely 396e48880e0SDavid Brownell /* Device may be bound to an active driver when this returns */ 397dc87c98eSGrant Likely status = device_add(&spi->dev); 398e48880e0SDavid Brownell if (status < 0) 399eb288a1fSLinus Walleij dev_err(dev, "can't add %s, status %d\n", 400eb288a1fSLinus Walleij dev_name(&spi->dev), status); 401e48880e0SDavid Brownell else 40235f74fcaSKay Sievers dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 403e48880e0SDavid Brownell 404e48880e0SDavid Brownell done: 405e48880e0SDavid Brownell mutex_unlock(&spi_add_lock); 406e48880e0SDavid Brownell return status; 407dc87c98eSGrant Likely } 408dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device); 4098ae12a0dSDavid Brownell 41033e34dc6SDavid Brownell /** 41133e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 41233e34dc6SDavid Brownell * @master: Controller to which device is connected 41333e34dc6SDavid Brownell * @chip: Describes the SPI device 41433e34dc6SDavid Brownell * Context: can sleep 41533e34dc6SDavid Brownell * 41633e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 4178ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 4188ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 4198ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 4208ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 421082c8cb4SDavid Brownell * 422082c8cb4SDavid Brownell * Returns the new device, or NULL. 4238ae12a0dSDavid Brownell */ 424e9d5a461SAdrian Bunk struct spi_device *spi_new_device(struct spi_master *master, 425e9d5a461SAdrian Bunk struct spi_board_info *chip) 4268ae12a0dSDavid Brownell { 4278ae12a0dSDavid Brownell struct spi_device *proxy; 4288ae12a0dSDavid Brownell int status; 4298ae12a0dSDavid Brownell 430082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 431082c8cb4SDavid Brownell * 432082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 433082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 434082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 435082c8cb4SDavid Brownell */ 436082c8cb4SDavid Brownell 437dc87c98eSGrant Likely proxy = spi_alloc_device(master); 438dc87c98eSGrant Likely if (!proxy) 4398ae12a0dSDavid Brownell return NULL; 4408ae12a0dSDavid Brownell 441102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 442102eb975SGrant Likely 4438ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 4448ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 445980a01c9SDavid Brownell proxy->mode = chip->mode; 4468ae12a0dSDavid Brownell proxy->irq = chip->irq; 447102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 4488ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 4498ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 4508ae12a0dSDavid Brownell proxy->controller_state = NULL; 4518ae12a0dSDavid Brownell 452dc87c98eSGrant Likely status = spi_add_device(proxy); 4538ae12a0dSDavid Brownell if (status < 0) { 454dc87c98eSGrant Likely spi_dev_put(proxy); 4558ae12a0dSDavid Brownell return NULL; 4568ae12a0dSDavid Brownell } 457dc87c98eSGrant Likely 458dc87c98eSGrant Likely return proxy; 459dc87c98eSGrant Likely } 4608ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 4618ae12a0dSDavid Brownell 4622b9603a0SFeng Tang static void spi_match_master_to_boardinfo(struct spi_master *master, 4632b9603a0SFeng Tang struct spi_board_info *bi) 4642b9603a0SFeng Tang { 4652b9603a0SFeng Tang struct spi_device *dev; 4662b9603a0SFeng Tang 4672b9603a0SFeng Tang if (master->bus_num != bi->bus_num) 4682b9603a0SFeng Tang return; 4692b9603a0SFeng Tang 4702b9603a0SFeng Tang dev = spi_new_device(master, bi); 4712b9603a0SFeng Tang if (!dev) 4722b9603a0SFeng Tang dev_err(master->dev.parent, "can't create new device for %s\n", 4732b9603a0SFeng Tang bi->modalias); 4742b9603a0SFeng Tang } 4752b9603a0SFeng Tang 47633e34dc6SDavid Brownell /** 47733e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 47833e34dc6SDavid Brownell * @info: array of chip descriptors 47933e34dc6SDavid Brownell * @n: how many descriptors are provided 48033e34dc6SDavid Brownell * Context: can sleep 48133e34dc6SDavid Brownell * 4828ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 4838ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 4848ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 4858ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 4868ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 4878ae12a0dSDavid Brownell * 4888ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 4898ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 4908ae12a0dSDavid Brownell * would naturally declare its SPI devices. 4918ae12a0dSDavid Brownell * 4928ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 4938ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 4948ae12a0dSDavid Brownell */ 495690fb11bSMark Brown int __devinit 4968ae12a0dSDavid Brownell spi_register_board_info(struct spi_board_info const *info, unsigned n) 4978ae12a0dSDavid Brownell { 4988ae12a0dSDavid Brownell struct boardinfo *bi; 4992b9603a0SFeng Tang int i; 5008ae12a0dSDavid Brownell 5012b9603a0SFeng Tang bi = kzalloc(n * sizeof(*bi), GFP_KERNEL); 5028ae12a0dSDavid Brownell if (!bi) 5038ae12a0dSDavid Brownell return -ENOMEM; 5048ae12a0dSDavid Brownell 5052b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 5062b9603a0SFeng Tang struct spi_master *master; 5072b9603a0SFeng Tang 5082b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 50994040828SMatthias Kaehlcke mutex_lock(&board_lock); 5108ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 5112b9603a0SFeng Tang list_for_each_entry(master, &spi_master_list, list) 5122b9603a0SFeng Tang spi_match_master_to_boardinfo(master, &bi->board_info); 51394040828SMatthias Kaehlcke mutex_unlock(&board_lock); 5142b9603a0SFeng Tang } 5152b9603a0SFeng Tang 5168ae12a0dSDavid Brownell return 0; 5178ae12a0dSDavid Brownell } 5188ae12a0dSDavid Brownell 5198ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 5208ae12a0dSDavid Brownell 521ffbbdd21SLinus Walleij /** 522ffbbdd21SLinus Walleij * spi_pump_messages - kthread work function which processes spi message queue 523ffbbdd21SLinus Walleij * @work: pointer to kthread work struct contained in the master struct 524ffbbdd21SLinus Walleij * 525ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 526ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 527ffbbdd21SLinus Walleij * and transfer each message. 528ffbbdd21SLinus Walleij * 529ffbbdd21SLinus Walleij */ 530ffbbdd21SLinus Walleij static void spi_pump_messages(struct kthread_work *work) 531ffbbdd21SLinus Walleij { 532ffbbdd21SLinus Walleij struct spi_master *master = 533ffbbdd21SLinus Walleij container_of(work, struct spi_master, pump_messages); 534ffbbdd21SLinus Walleij unsigned long flags; 535ffbbdd21SLinus Walleij bool was_busy = false; 536ffbbdd21SLinus Walleij int ret; 537ffbbdd21SLinus Walleij 538ffbbdd21SLinus Walleij /* Lock queue and check for queue work */ 539ffbbdd21SLinus Walleij spin_lock_irqsave(&master->queue_lock, flags); 540ffbbdd21SLinus Walleij if (list_empty(&master->queue) || !master->running) { 5417dfd2bd7SShubhrajyoti D if (master->busy && master->unprepare_transfer_hardware) { 542ffbbdd21SLinus Walleij ret = master->unprepare_transfer_hardware(master); 543ffbbdd21SLinus Walleij if (ret) { 5449af4acc0SDan Carpenter spin_unlock_irqrestore(&master->queue_lock, flags); 545ffbbdd21SLinus Walleij dev_err(&master->dev, 546ffbbdd21SLinus Walleij "failed to unprepare transfer hardware\n"); 547ffbbdd21SLinus Walleij return; 548ffbbdd21SLinus Walleij } 549ffbbdd21SLinus Walleij } 550ffbbdd21SLinus Walleij master->busy = false; 551ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 552ffbbdd21SLinus Walleij return; 553ffbbdd21SLinus Walleij } 554ffbbdd21SLinus Walleij 555ffbbdd21SLinus Walleij /* Make sure we are not already running a message */ 556ffbbdd21SLinus Walleij if (master->cur_msg) { 557ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 558ffbbdd21SLinus Walleij return; 559ffbbdd21SLinus Walleij } 560ffbbdd21SLinus Walleij /* Extract head of queue */ 561ffbbdd21SLinus Walleij master->cur_msg = 562ffbbdd21SLinus Walleij list_entry(master->queue.next, struct spi_message, queue); 563ffbbdd21SLinus Walleij 564ffbbdd21SLinus Walleij list_del_init(&master->cur_msg->queue); 565ffbbdd21SLinus Walleij if (master->busy) 566ffbbdd21SLinus Walleij was_busy = true; 567ffbbdd21SLinus Walleij else 568ffbbdd21SLinus Walleij master->busy = true; 569ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 570ffbbdd21SLinus Walleij 5717dfd2bd7SShubhrajyoti D if (!was_busy && master->prepare_transfer_hardware) { 572ffbbdd21SLinus Walleij ret = master->prepare_transfer_hardware(master); 573ffbbdd21SLinus Walleij if (ret) { 574ffbbdd21SLinus Walleij dev_err(&master->dev, 575ffbbdd21SLinus Walleij "failed to prepare transfer hardware\n"); 576ffbbdd21SLinus Walleij return; 577ffbbdd21SLinus Walleij } 578ffbbdd21SLinus Walleij } 579ffbbdd21SLinus Walleij 580ffbbdd21SLinus Walleij ret = master->transfer_one_message(master, master->cur_msg); 581ffbbdd21SLinus Walleij if (ret) { 582ffbbdd21SLinus Walleij dev_err(&master->dev, 583ffbbdd21SLinus Walleij "failed to transfer one message from queue\n"); 584ffbbdd21SLinus Walleij return; 585ffbbdd21SLinus Walleij } 586ffbbdd21SLinus Walleij } 587ffbbdd21SLinus Walleij 588ffbbdd21SLinus Walleij static int spi_init_queue(struct spi_master *master) 589ffbbdd21SLinus Walleij { 590ffbbdd21SLinus Walleij struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; 591ffbbdd21SLinus Walleij 592ffbbdd21SLinus Walleij INIT_LIST_HEAD(&master->queue); 593ffbbdd21SLinus Walleij spin_lock_init(&master->queue_lock); 594ffbbdd21SLinus Walleij 595ffbbdd21SLinus Walleij master->running = false; 596ffbbdd21SLinus Walleij master->busy = false; 597ffbbdd21SLinus Walleij 598ffbbdd21SLinus Walleij init_kthread_worker(&master->kworker); 599ffbbdd21SLinus Walleij master->kworker_task = kthread_run(kthread_worker_fn, 600ffbbdd21SLinus Walleij &master->kworker, 601ffbbdd21SLinus Walleij dev_name(&master->dev)); 602ffbbdd21SLinus Walleij if (IS_ERR(master->kworker_task)) { 603ffbbdd21SLinus Walleij dev_err(&master->dev, "failed to create message pump task\n"); 604ffbbdd21SLinus Walleij return -ENOMEM; 605ffbbdd21SLinus Walleij } 606ffbbdd21SLinus Walleij init_kthread_work(&master->pump_messages, spi_pump_messages); 607ffbbdd21SLinus Walleij 608ffbbdd21SLinus Walleij /* 609ffbbdd21SLinus Walleij * Master config will indicate if this controller should run the 610ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 611ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 612ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 613ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 614ffbbdd21SLinus Walleij */ 615ffbbdd21SLinus Walleij if (master->rt) { 616ffbbdd21SLinus Walleij dev_info(&master->dev, 617ffbbdd21SLinus Walleij "will run message pump with realtime priority\n"); 618ffbbdd21SLinus Walleij sched_setscheduler(master->kworker_task, SCHED_FIFO, ¶m); 619ffbbdd21SLinus Walleij } 620ffbbdd21SLinus Walleij 621ffbbdd21SLinus Walleij return 0; 622ffbbdd21SLinus Walleij } 623ffbbdd21SLinus Walleij 624ffbbdd21SLinus Walleij /** 625ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 626ffbbdd21SLinus Walleij * messages 627ffbbdd21SLinus Walleij * @master: the master to check for queued messages 628ffbbdd21SLinus Walleij * 629ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 630ffbbdd21SLinus Walleij * this call. 631ffbbdd21SLinus Walleij */ 632ffbbdd21SLinus Walleij struct spi_message *spi_get_next_queued_message(struct spi_master *master) 633ffbbdd21SLinus Walleij { 634ffbbdd21SLinus Walleij struct spi_message *next; 635ffbbdd21SLinus Walleij unsigned long flags; 636ffbbdd21SLinus Walleij 637ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 638ffbbdd21SLinus Walleij spin_lock_irqsave(&master->queue_lock, flags); 639ffbbdd21SLinus Walleij if (list_empty(&master->queue)) 640ffbbdd21SLinus Walleij next = NULL; 641ffbbdd21SLinus Walleij else 642ffbbdd21SLinus Walleij next = list_entry(master->queue.next, 643ffbbdd21SLinus Walleij struct spi_message, queue); 644ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 645ffbbdd21SLinus Walleij 646ffbbdd21SLinus Walleij return next; 647ffbbdd21SLinus Walleij } 648ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 649ffbbdd21SLinus Walleij 650ffbbdd21SLinus Walleij /** 651ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 652ffbbdd21SLinus Walleij * @master: the master to return the message to 653ffbbdd21SLinus Walleij * 654ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 655ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 656ffbbdd21SLinus Walleij */ 657ffbbdd21SLinus Walleij void spi_finalize_current_message(struct spi_master *master) 658ffbbdd21SLinus Walleij { 659ffbbdd21SLinus Walleij struct spi_message *mesg; 660ffbbdd21SLinus Walleij unsigned long flags; 661ffbbdd21SLinus Walleij 662ffbbdd21SLinus Walleij spin_lock_irqsave(&master->queue_lock, flags); 663ffbbdd21SLinus Walleij mesg = master->cur_msg; 664ffbbdd21SLinus Walleij master->cur_msg = NULL; 665ffbbdd21SLinus Walleij 666ffbbdd21SLinus Walleij queue_kthread_work(&master->kworker, &master->pump_messages); 667ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 668ffbbdd21SLinus Walleij 669ffbbdd21SLinus Walleij mesg->state = NULL; 670ffbbdd21SLinus Walleij if (mesg->complete) 671ffbbdd21SLinus Walleij mesg->complete(mesg->context); 672ffbbdd21SLinus Walleij } 673ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 674ffbbdd21SLinus Walleij 675ffbbdd21SLinus Walleij static int spi_start_queue(struct spi_master *master) 676ffbbdd21SLinus Walleij { 677ffbbdd21SLinus Walleij unsigned long flags; 678ffbbdd21SLinus Walleij 679ffbbdd21SLinus Walleij spin_lock_irqsave(&master->queue_lock, flags); 680ffbbdd21SLinus Walleij 681ffbbdd21SLinus Walleij if (master->running || master->busy) { 682ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 683ffbbdd21SLinus Walleij return -EBUSY; 684ffbbdd21SLinus Walleij } 685ffbbdd21SLinus Walleij 686ffbbdd21SLinus Walleij master->running = true; 687ffbbdd21SLinus Walleij master->cur_msg = NULL; 688ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 689ffbbdd21SLinus Walleij 690ffbbdd21SLinus Walleij queue_kthread_work(&master->kworker, &master->pump_messages); 691ffbbdd21SLinus Walleij 692ffbbdd21SLinus Walleij return 0; 693ffbbdd21SLinus Walleij } 694ffbbdd21SLinus Walleij 695ffbbdd21SLinus Walleij static int spi_stop_queue(struct spi_master *master) 696ffbbdd21SLinus Walleij { 697ffbbdd21SLinus Walleij unsigned long flags; 698ffbbdd21SLinus Walleij unsigned limit = 500; 699ffbbdd21SLinus Walleij int ret = 0; 700ffbbdd21SLinus Walleij 701ffbbdd21SLinus Walleij spin_lock_irqsave(&master->queue_lock, flags); 702ffbbdd21SLinus Walleij 703ffbbdd21SLinus Walleij /* 704ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 705ffbbdd21SLinus Walleij * A wait_queue on the master->busy could be used, but then the common 706ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 707ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 708ffbbdd21SLinus Walleij */ 709ffbbdd21SLinus Walleij while ((!list_empty(&master->queue) || master->busy) && limit--) { 710ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 711ffbbdd21SLinus Walleij msleep(10); 712ffbbdd21SLinus Walleij spin_lock_irqsave(&master->queue_lock, flags); 713ffbbdd21SLinus Walleij } 714ffbbdd21SLinus Walleij 715ffbbdd21SLinus Walleij if (!list_empty(&master->queue) || master->busy) 716ffbbdd21SLinus Walleij ret = -EBUSY; 717ffbbdd21SLinus Walleij else 718ffbbdd21SLinus Walleij master->running = false; 719ffbbdd21SLinus Walleij 720ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 721ffbbdd21SLinus Walleij 722ffbbdd21SLinus Walleij if (ret) { 723ffbbdd21SLinus Walleij dev_warn(&master->dev, 724ffbbdd21SLinus Walleij "could not stop message queue\n"); 725ffbbdd21SLinus Walleij return ret; 726ffbbdd21SLinus Walleij } 727ffbbdd21SLinus Walleij return ret; 728ffbbdd21SLinus Walleij } 729ffbbdd21SLinus Walleij 730ffbbdd21SLinus Walleij static int spi_destroy_queue(struct spi_master *master) 731ffbbdd21SLinus Walleij { 732ffbbdd21SLinus Walleij int ret; 733ffbbdd21SLinus Walleij 734ffbbdd21SLinus Walleij ret = spi_stop_queue(master); 735ffbbdd21SLinus Walleij 736ffbbdd21SLinus Walleij /* 737ffbbdd21SLinus Walleij * flush_kthread_worker will block until all work is done. 738ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 739ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 740ffbbdd21SLinus Walleij * return anyway. 741ffbbdd21SLinus Walleij */ 742ffbbdd21SLinus Walleij if (ret) { 743ffbbdd21SLinus Walleij dev_err(&master->dev, "problem destroying queue\n"); 744ffbbdd21SLinus Walleij return ret; 745ffbbdd21SLinus Walleij } 746ffbbdd21SLinus Walleij 747ffbbdd21SLinus Walleij flush_kthread_worker(&master->kworker); 748ffbbdd21SLinus Walleij kthread_stop(master->kworker_task); 749ffbbdd21SLinus Walleij 750ffbbdd21SLinus Walleij return 0; 751ffbbdd21SLinus Walleij } 752ffbbdd21SLinus Walleij 753ffbbdd21SLinus Walleij /** 754ffbbdd21SLinus Walleij * spi_queued_transfer - transfer function for queued transfers 755ffbbdd21SLinus Walleij * @spi: spi device which is requesting transfer 756ffbbdd21SLinus Walleij * @msg: spi message which is to handled is queued to driver queue 757ffbbdd21SLinus Walleij */ 758ffbbdd21SLinus Walleij static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 759ffbbdd21SLinus Walleij { 760ffbbdd21SLinus Walleij struct spi_master *master = spi->master; 761ffbbdd21SLinus Walleij unsigned long flags; 762ffbbdd21SLinus Walleij 763ffbbdd21SLinus Walleij spin_lock_irqsave(&master->queue_lock, flags); 764ffbbdd21SLinus Walleij 765ffbbdd21SLinus Walleij if (!master->running) { 766ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 767ffbbdd21SLinus Walleij return -ESHUTDOWN; 768ffbbdd21SLinus Walleij } 769ffbbdd21SLinus Walleij msg->actual_length = 0; 770ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 771ffbbdd21SLinus Walleij 772ffbbdd21SLinus Walleij list_add_tail(&msg->queue, &master->queue); 773ffbbdd21SLinus Walleij if (master->running && !master->busy) 774ffbbdd21SLinus Walleij queue_kthread_work(&master->kworker, &master->pump_messages); 775ffbbdd21SLinus Walleij 776ffbbdd21SLinus Walleij spin_unlock_irqrestore(&master->queue_lock, flags); 777ffbbdd21SLinus Walleij return 0; 778ffbbdd21SLinus Walleij } 779ffbbdd21SLinus Walleij 780ffbbdd21SLinus Walleij static int spi_master_initialize_queue(struct spi_master *master) 781ffbbdd21SLinus Walleij { 782ffbbdd21SLinus Walleij int ret; 783ffbbdd21SLinus Walleij 784ffbbdd21SLinus Walleij master->queued = true; 785ffbbdd21SLinus Walleij master->transfer = spi_queued_transfer; 786ffbbdd21SLinus Walleij 787ffbbdd21SLinus Walleij /* Initialize and start queue */ 788ffbbdd21SLinus Walleij ret = spi_init_queue(master); 789ffbbdd21SLinus Walleij if (ret) { 790ffbbdd21SLinus Walleij dev_err(&master->dev, "problem initializing queue\n"); 791ffbbdd21SLinus Walleij goto err_init_queue; 792ffbbdd21SLinus Walleij } 793ffbbdd21SLinus Walleij ret = spi_start_queue(master); 794ffbbdd21SLinus Walleij if (ret) { 795ffbbdd21SLinus Walleij dev_err(&master->dev, "problem starting queue\n"); 796ffbbdd21SLinus Walleij goto err_start_queue; 797ffbbdd21SLinus Walleij } 798ffbbdd21SLinus Walleij 799ffbbdd21SLinus Walleij return 0; 800ffbbdd21SLinus Walleij 801ffbbdd21SLinus Walleij err_start_queue: 802ffbbdd21SLinus Walleij err_init_queue: 803ffbbdd21SLinus Walleij spi_destroy_queue(master); 804ffbbdd21SLinus Walleij return ret; 805ffbbdd21SLinus Walleij } 806ffbbdd21SLinus Walleij 807ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 808ffbbdd21SLinus Walleij 809d57a4282SGrant Likely #if defined(CONFIG_OF) && !defined(CONFIG_SPARC) 810d57a4282SGrant Likely /** 811d57a4282SGrant Likely * of_register_spi_devices() - Register child devices onto the SPI bus 812d57a4282SGrant Likely * @master: Pointer to spi_master device 813d57a4282SGrant Likely * 814d57a4282SGrant Likely * Registers an spi_device for each child node of master node which has a 'reg' 815d57a4282SGrant Likely * property. 816d57a4282SGrant Likely */ 817d57a4282SGrant Likely static void of_register_spi_devices(struct spi_master *master) 818d57a4282SGrant Likely { 819d57a4282SGrant Likely struct spi_device *spi; 820d57a4282SGrant Likely struct device_node *nc; 821d57a4282SGrant Likely const __be32 *prop; 822d57a4282SGrant Likely int rc; 823d57a4282SGrant Likely int len; 824d57a4282SGrant Likely 825d57a4282SGrant Likely if (!master->dev.of_node) 826d57a4282SGrant Likely return; 827d57a4282SGrant Likely 828d57a4282SGrant Likely for_each_child_of_node(master->dev.of_node, nc) { 829d57a4282SGrant Likely /* Alloc an spi_device */ 830d57a4282SGrant Likely spi = spi_alloc_device(master); 831d57a4282SGrant Likely if (!spi) { 832d57a4282SGrant Likely dev_err(&master->dev, "spi_device alloc error for %s\n", 833d57a4282SGrant Likely nc->full_name); 834d57a4282SGrant Likely spi_dev_put(spi); 835d57a4282SGrant Likely continue; 836d57a4282SGrant Likely } 837d57a4282SGrant Likely 838d57a4282SGrant Likely /* Select device driver */ 839d57a4282SGrant Likely if (of_modalias_node(nc, spi->modalias, 840d57a4282SGrant Likely sizeof(spi->modalias)) < 0) { 841d57a4282SGrant Likely dev_err(&master->dev, "cannot find modalias for %s\n", 842d57a4282SGrant Likely nc->full_name); 843d57a4282SGrant Likely spi_dev_put(spi); 844d57a4282SGrant Likely continue; 845d57a4282SGrant Likely } 846d57a4282SGrant Likely 847d57a4282SGrant Likely /* Device address */ 848d57a4282SGrant Likely prop = of_get_property(nc, "reg", &len); 849d57a4282SGrant Likely if (!prop || len < sizeof(*prop)) { 850d57a4282SGrant Likely dev_err(&master->dev, "%s has no 'reg' property\n", 851d57a4282SGrant Likely nc->full_name); 852d57a4282SGrant Likely spi_dev_put(spi); 853d57a4282SGrant Likely continue; 854d57a4282SGrant Likely } 855d57a4282SGrant Likely spi->chip_select = be32_to_cpup(prop); 856d57a4282SGrant Likely 857d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 858d57a4282SGrant Likely if (of_find_property(nc, "spi-cpha", NULL)) 859d57a4282SGrant Likely spi->mode |= SPI_CPHA; 860d57a4282SGrant Likely if (of_find_property(nc, "spi-cpol", NULL)) 861d57a4282SGrant Likely spi->mode |= SPI_CPOL; 862d57a4282SGrant Likely if (of_find_property(nc, "spi-cs-high", NULL)) 863d57a4282SGrant Likely spi->mode |= SPI_CS_HIGH; 864d57a4282SGrant Likely 865d57a4282SGrant Likely /* Device speed */ 866d57a4282SGrant Likely prop = of_get_property(nc, "spi-max-frequency", &len); 867d57a4282SGrant Likely if (!prop || len < sizeof(*prop)) { 868d57a4282SGrant Likely dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n", 869d57a4282SGrant Likely nc->full_name); 870d57a4282SGrant Likely spi_dev_put(spi); 871d57a4282SGrant Likely continue; 872d57a4282SGrant Likely } 873d57a4282SGrant Likely spi->max_speed_hz = be32_to_cpup(prop); 874d57a4282SGrant Likely 875d57a4282SGrant Likely /* IRQ */ 876d57a4282SGrant Likely spi->irq = irq_of_parse_and_map(nc, 0); 877d57a4282SGrant Likely 878d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 879d57a4282SGrant Likely of_node_get(nc); 880d57a4282SGrant Likely spi->dev.of_node = nc; 881d57a4282SGrant Likely 882d57a4282SGrant Likely /* Register the new device */ 883d57a4282SGrant Likely request_module(spi->modalias); 884d57a4282SGrant Likely rc = spi_add_device(spi); 885d57a4282SGrant Likely if (rc) { 886d57a4282SGrant Likely dev_err(&master->dev, "spi_device register error %s\n", 887d57a4282SGrant Likely nc->full_name); 888d57a4282SGrant Likely spi_dev_put(spi); 889d57a4282SGrant Likely } 890d57a4282SGrant Likely 891d57a4282SGrant Likely } 892d57a4282SGrant Likely } 893d57a4282SGrant Likely #else 894d57a4282SGrant Likely static void of_register_spi_devices(struct spi_master *master) { } 895d57a4282SGrant Likely #endif 896d57a4282SGrant Likely 89749dce689STony Jones static void spi_master_release(struct device *dev) 8988ae12a0dSDavid Brownell { 8998ae12a0dSDavid Brownell struct spi_master *master; 9008ae12a0dSDavid Brownell 90149dce689STony Jones master = container_of(dev, struct spi_master, dev); 9028ae12a0dSDavid Brownell kfree(master); 9038ae12a0dSDavid Brownell } 9048ae12a0dSDavid Brownell 9058ae12a0dSDavid Brownell static struct class spi_master_class = { 9068ae12a0dSDavid Brownell .name = "spi_master", 9078ae12a0dSDavid Brownell .owner = THIS_MODULE, 90849dce689STony Jones .dev_release = spi_master_release, 9098ae12a0dSDavid Brownell }; 9108ae12a0dSDavid Brownell 9118ae12a0dSDavid Brownell 912ffbbdd21SLinus Walleij 9138ae12a0dSDavid Brownell /** 9148ae12a0dSDavid Brownell * spi_alloc_master - allocate SPI master controller 9158ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 91633e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 91749dce689STony Jones * memory is in the driver_data field of the returned device, 9180c868461SDavid Brownell * accessible with spi_master_get_devdata(). 91933e34dc6SDavid Brownell * Context: can sleep 9208ae12a0dSDavid Brownell * 9218ae12a0dSDavid Brownell * This call is used only by SPI master controller drivers, which are the 9228ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 923ba1a0513Sdmitry pervushin * an spi_master structure, prior to calling spi_register_master(). 9248ae12a0dSDavid Brownell * 9258ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns the SPI 9268ae12a0dSDavid Brownell * master structure on success, else NULL. 9278ae12a0dSDavid Brownell * 9288ae12a0dSDavid Brownell * The caller is responsible for assigning the bus number and initializing 929ba1a0513Sdmitry pervushin * the master's methods before calling spi_register_master(); and (after errors 930eb4af0f5SUwe Kleine-König * adding the device) calling spi_master_put() and kfree() to prevent a memory 931eb4af0f5SUwe Kleine-König * leak. 9328ae12a0dSDavid Brownell */ 933e9d5a461SAdrian Bunk struct spi_master *spi_alloc_master(struct device *dev, unsigned size) 9348ae12a0dSDavid Brownell { 9358ae12a0dSDavid Brownell struct spi_master *master; 9368ae12a0dSDavid Brownell 9370c868461SDavid Brownell if (!dev) 9380c868461SDavid Brownell return NULL; 9390c868461SDavid Brownell 940e94b1766SChristoph Lameter master = kzalloc(size + sizeof *master, GFP_KERNEL); 9418ae12a0dSDavid Brownell if (!master) 9428ae12a0dSDavid Brownell return NULL; 9438ae12a0dSDavid Brownell 94449dce689STony Jones device_initialize(&master->dev); 9451e8a52e1SGrant Likely master->bus_num = -1; 9461e8a52e1SGrant Likely master->num_chipselect = 1; 94749dce689STony Jones master->dev.class = &spi_master_class; 94849dce689STony Jones master->dev.parent = get_device(dev); 9490c868461SDavid Brownell spi_master_set_devdata(master, &master[1]); 9508ae12a0dSDavid Brownell 9518ae12a0dSDavid Brownell return master; 9528ae12a0dSDavid Brownell } 9538ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_alloc_master); 9548ae12a0dSDavid Brownell 95574317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF 95674317984SJean-Christophe PLAGNIOL-VILLARD static int of_spi_register_master(struct spi_master *master) 95774317984SJean-Christophe PLAGNIOL-VILLARD { 95874317984SJean-Christophe PLAGNIOL-VILLARD u16 nb; 95974317984SJean-Christophe PLAGNIOL-VILLARD int i, *cs; 96074317984SJean-Christophe PLAGNIOL-VILLARD struct device_node *np = master->dev.of_node; 96174317984SJean-Christophe PLAGNIOL-VILLARD 96274317984SJean-Christophe PLAGNIOL-VILLARD if (!np) 96374317984SJean-Christophe PLAGNIOL-VILLARD return 0; 96474317984SJean-Christophe PLAGNIOL-VILLARD 96574317984SJean-Christophe PLAGNIOL-VILLARD nb = of_gpio_named_count(np, "cs-gpios"); 96674317984SJean-Christophe PLAGNIOL-VILLARD master->num_chipselect = max(nb, master->num_chipselect); 96774317984SJean-Christophe PLAGNIOL-VILLARD 96874317984SJean-Christophe PLAGNIOL-VILLARD if (nb < 1) 96974317984SJean-Christophe PLAGNIOL-VILLARD return 0; 97074317984SJean-Christophe PLAGNIOL-VILLARD 97174317984SJean-Christophe PLAGNIOL-VILLARD cs = devm_kzalloc(&master->dev, 97274317984SJean-Christophe PLAGNIOL-VILLARD sizeof(int) * master->num_chipselect, 97374317984SJean-Christophe PLAGNIOL-VILLARD GFP_KERNEL); 97474317984SJean-Christophe PLAGNIOL-VILLARD master->cs_gpios = cs; 97574317984SJean-Christophe PLAGNIOL-VILLARD 97674317984SJean-Christophe PLAGNIOL-VILLARD if (!master->cs_gpios) 97774317984SJean-Christophe PLAGNIOL-VILLARD return -ENOMEM; 97874317984SJean-Christophe PLAGNIOL-VILLARD 97974317984SJean-Christophe PLAGNIOL-VILLARD memset(cs, -EINVAL, master->num_chipselect); 98074317984SJean-Christophe PLAGNIOL-VILLARD 98174317984SJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < nb; i++) 98274317984SJean-Christophe PLAGNIOL-VILLARD cs[i] = of_get_named_gpio(np, "cs-gpios", i); 98374317984SJean-Christophe PLAGNIOL-VILLARD 98474317984SJean-Christophe PLAGNIOL-VILLARD return 0; 98574317984SJean-Christophe PLAGNIOL-VILLARD } 98674317984SJean-Christophe PLAGNIOL-VILLARD #else 98774317984SJean-Christophe PLAGNIOL-VILLARD static int of_spi_register_master(struct spi_master *master) 98874317984SJean-Christophe PLAGNIOL-VILLARD { 98974317984SJean-Christophe PLAGNIOL-VILLARD return 0; 99074317984SJean-Christophe PLAGNIOL-VILLARD } 99174317984SJean-Christophe PLAGNIOL-VILLARD #endif 99274317984SJean-Christophe PLAGNIOL-VILLARD 9938ae12a0dSDavid Brownell /** 9948ae12a0dSDavid Brownell * spi_register_master - register SPI master controller 9958ae12a0dSDavid Brownell * @master: initialized master, originally from spi_alloc_master() 99633e34dc6SDavid Brownell * Context: can sleep 9978ae12a0dSDavid Brownell * 9988ae12a0dSDavid Brownell * SPI master controllers connect to their drivers using some non-SPI bus, 9998ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 10008ae12a0dSDavid Brownell * includes calling spi_register_master() to hook up to this SPI bus glue. 10018ae12a0dSDavid Brownell * 10028ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 10038ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 10048ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 10058ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 10068ae12a0dSDavid Brownell * chip is at which address. 10078ae12a0dSDavid Brownell * 10088ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 10098ae12a0dSDavid Brownell * success, else a negative error code (dropping the master's refcount). 10100c868461SDavid Brownell * After a successful return, the caller is responsible for calling 10110c868461SDavid Brownell * spi_unregister_master(). 10128ae12a0dSDavid Brownell */ 1013e9d5a461SAdrian Bunk int spi_register_master(struct spi_master *master) 10148ae12a0dSDavid Brownell { 1015e44a45aeSDavid Brownell static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); 101649dce689STony Jones struct device *dev = master->dev.parent; 10172b9603a0SFeng Tang struct boardinfo *bi; 10188ae12a0dSDavid Brownell int status = -ENODEV; 10198ae12a0dSDavid Brownell int dynamic = 0; 10208ae12a0dSDavid Brownell 10210c868461SDavid Brownell if (!dev) 10220c868461SDavid Brownell return -ENODEV; 10230c868461SDavid Brownell 102474317984SJean-Christophe PLAGNIOL-VILLARD status = of_spi_register_master(master); 102574317984SJean-Christophe PLAGNIOL-VILLARD if (status) 102674317984SJean-Christophe PLAGNIOL-VILLARD return status; 102774317984SJean-Christophe PLAGNIOL-VILLARD 1028082c8cb4SDavid Brownell /* even if it's just one always-selected device, there must 1029082c8cb4SDavid Brownell * be at least one chipselect 1030082c8cb4SDavid Brownell */ 1031082c8cb4SDavid Brownell if (master->num_chipselect == 0) 1032082c8cb4SDavid Brownell return -EINVAL; 1033082c8cb4SDavid Brownell 10348ae12a0dSDavid Brownell /* convention: dynamically assigned bus IDs count down from the max */ 1035a020ed75SDavid Brownell if (master->bus_num < 0) { 1036082c8cb4SDavid Brownell /* FIXME switch to an IDR based scheme, something like 1037082c8cb4SDavid Brownell * I2C now uses, so we can't run out of "dynamic" IDs 1038082c8cb4SDavid Brownell */ 10398ae12a0dSDavid Brownell master->bus_num = atomic_dec_return(&dyn_bus_id); 1040b885244eSDavid Brownell dynamic = 1; 10418ae12a0dSDavid Brownell } 10428ae12a0dSDavid Brownell 1043cf32b71eSErnst Schwab spin_lock_init(&master->bus_lock_spinlock); 1044cf32b71eSErnst Schwab mutex_init(&master->bus_lock_mutex); 1045cf32b71eSErnst Schwab master->bus_lock_flag = 0; 1046cf32b71eSErnst Schwab 10478ae12a0dSDavid Brownell /* register the device, then userspace will see it. 10488ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 10498ae12a0dSDavid Brownell */ 105035f74fcaSKay Sievers dev_set_name(&master->dev, "spi%u", master->bus_num); 105149dce689STony Jones status = device_add(&master->dev); 1052b885244eSDavid Brownell if (status < 0) 10538ae12a0dSDavid Brownell goto done; 105435f74fcaSKay Sievers dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev), 10558ae12a0dSDavid Brownell dynamic ? " (dynamic)" : ""); 10568ae12a0dSDavid Brownell 1057ffbbdd21SLinus Walleij /* If we're using a queued driver, start the queue */ 1058ffbbdd21SLinus Walleij if (master->transfer) 1059ffbbdd21SLinus Walleij dev_info(dev, "master is unqueued, this is deprecated\n"); 1060ffbbdd21SLinus Walleij else { 1061ffbbdd21SLinus Walleij status = spi_master_initialize_queue(master); 1062ffbbdd21SLinus Walleij if (status) { 1063ffbbdd21SLinus Walleij device_unregister(&master->dev); 1064ffbbdd21SLinus Walleij goto done; 1065ffbbdd21SLinus Walleij } 1066ffbbdd21SLinus Walleij } 1067ffbbdd21SLinus Walleij 10682b9603a0SFeng Tang mutex_lock(&board_lock); 10692b9603a0SFeng Tang list_add_tail(&master->list, &spi_master_list); 10702b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 10712b9603a0SFeng Tang spi_match_master_to_boardinfo(master, &bi->board_info); 10722b9603a0SFeng Tang mutex_unlock(&board_lock); 10732b9603a0SFeng Tang 107412b15e83SAnatolij Gustschin /* Register devices from the device tree */ 107512b15e83SAnatolij Gustschin of_register_spi_devices(master); 10768ae12a0dSDavid Brownell done: 10778ae12a0dSDavid Brownell return status; 10788ae12a0dSDavid Brownell } 10798ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_master); 10808ae12a0dSDavid Brownell 108134860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 10828ae12a0dSDavid Brownell { 10830c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 10848ae12a0dSDavid Brownell return 0; 10858ae12a0dSDavid Brownell } 10868ae12a0dSDavid Brownell 10878ae12a0dSDavid Brownell /** 10888ae12a0dSDavid Brownell * spi_unregister_master - unregister SPI master controller 10898ae12a0dSDavid Brownell * @master: the master being unregistered 109033e34dc6SDavid Brownell * Context: can sleep 10918ae12a0dSDavid Brownell * 10928ae12a0dSDavid Brownell * This call is used only by SPI master controller drivers, which are the 10938ae12a0dSDavid Brownell * only ones directly touching chip registers. 10948ae12a0dSDavid Brownell * 10958ae12a0dSDavid Brownell * This must be called from context that can sleep. 10968ae12a0dSDavid Brownell */ 10978ae12a0dSDavid Brownell void spi_unregister_master(struct spi_master *master) 10988ae12a0dSDavid Brownell { 109989fc9a1aSJeff Garzik int dummy; 110089fc9a1aSJeff Garzik 1101ffbbdd21SLinus Walleij if (master->queued) { 1102ffbbdd21SLinus Walleij if (spi_destroy_queue(master)) 1103ffbbdd21SLinus Walleij dev_err(&master->dev, "queue remove failed\n"); 1104ffbbdd21SLinus Walleij } 1105ffbbdd21SLinus Walleij 11062b9603a0SFeng Tang mutex_lock(&board_lock); 11072b9603a0SFeng Tang list_del(&master->list); 11082b9603a0SFeng Tang mutex_unlock(&board_lock); 11092b9603a0SFeng Tang 111097dbf37dSSebastian Andrzej Siewior dummy = device_for_each_child(&master->dev, NULL, __unregister); 111149dce689STony Jones device_unregister(&master->dev); 11128ae12a0dSDavid Brownell } 11138ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_unregister_master); 11148ae12a0dSDavid Brownell 1115ffbbdd21SLinus Walleij int spi_master_suspend(struct spi_master *master) 1116ffbbdd21SLinus Walleij { 1117ffbbdd21SLinus Walleij int ret; 1118ffbbdd21SLinus Walleij 1119ffbbdd21SLinus Walleij /* Basically no-ops for non-queued masters */ 1120ffbbdd21SLinus Walleij if (!master->queued) 1121ffbbdd21SLinus Walleij return 0; 1122ffbbdd21SLinus Walleij 1123ffbbdd21SLinus Walleij ret = spi_stop_queue(master); 1124ffbbdd21SLinus Walleij if (ret) 1125ffbbdd21SLinus Walleij dev_err(&master->dev, "queue stop failed\n"); 1126ffbbdd21SLinus Walleij 1127ffbbdd21SLinus Walleij return ret; 1128ffbbdd21SLinus Walleij } 1129ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_master_suspend); 1130ffbbdd21SLinus Walleij 1131ffbbdd21SLinus Walleij int spi_master_resume(struct spi_master *master) 1132ffbbdd21SLinus Walleij { 1133ffbbdd21SLinus Walleij int ret; 1134ffbbdd21SLinus Walleij 1135ffbbdd21SLinus Walleij if (!master->queued) 1136ffbbdd21SLinus Walleij return 0; 1137ffbbdd21SLinus Walleij 1138ffbbdd21SLinus Walleij ret = spi_start_queue(master); 1139ffbbdd21SLinus Walleij if (ret) 1140ffbbdd21SLinus Walleij dev_err(&master->dev, "queue restart failed\n"); 1141ffbbdd21SLinus Walleij 1142ffbbdd21SLinus Walleij return ret; 1143ffbbdd21SLinus Walleij } 1144ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_master_resume); 1145ffbbdd21SLinus Walleij 11465ed2c832SDave Young static int __spi_master_match(struct device *dev, void *data) 11475ed2c832SDave Young { 11485ed2c832SDave Young struct spi_master *m; 11495ed2c832SDave Young u16 *bus_num = data; 11505ed2c832SDave Young 11515ed2c832SDave Young m = container_of(dev, struct spi_master, dev); 11525ed2c832SDave Young return m->bus_num == *bus_num; 11535ed2c832SDave Young } 11545ed2c832SDave Young 11558ae12a0dSDavid Brownell /** 11568ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 11578ae12a0dSDavid Brownell * @bus_num: the master's bus number 115833e34dc6SDavid Brownell * Context: can sleep 11598ae12a0dSDavid Brownell * 11608ae12a0dSDavid Brownell * This call may be used with devices that are registered after 11618ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 11628ae12a0dSDavid Brownell * spi_master (which the caller must release), or NULL if there is 11638ae12a0dSDavid Brownell * no such master registered. 11648ae12a0dSDavid Brownell */ 11658ae12a0dSDavid Brownell struct spi_master *spi_busnum_to_master(u16 bus_num) 11668ae12a0dSDavid Brownell { 116749dce689STony Jones struct device *dev; 11681e9a51dcSAtsushi Nemoto struct spi_master *master = NULL; 11698ae12a0dSDavid Brownell 1170695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 11715ed2c832SDave Young __spi_master_match); 11725ed2c832SDave Young if (dev) 11735ed2c832SDave Young master = container_of(dev, struct spi_master, dev); 11745ed2c832SDave Young /* reference got in class_find_device */ 11751e9a51dcSAtsushi Nemoto return master; 11768ae12a0dSDavid Brownell } 11778ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 11788ae12a0dSDavid Brownell 11798ae12a0dSDavid Brownell 11808ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 11818ae12a0dSDavid Brownell 11827d077197SDavid Brownell /* Core methods for SPI master protocol drivers. Some of the 11837d077197SDavid Brownell * other core methods are currently defined as inline functions. 11847d077197SDavid Brownell */ 11857d077197SDavid Brownell 11867d077197SDavid Brownell /** 11877d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 11887d077197SDavid Brownell * @spi: the device whose settings are being modified 11897d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 11907d077197SDavid Brownell * 11917d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 11927d077197SDavid Brownell * device doesn't work with its default. They may likewise need 11937d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 11947d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 11957d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 11967d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 11977d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 11987d077197SDavid Brownell * 11997d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 12007d077197SDavid Brownell * that the underlying controller or its driver does not support. For 12017d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 12027d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 12037d077197SDavid Brownell */ 12047d077197SDavid Brownell int spi_setup(struct spi_device *spi) 12057d077197SDavid Brownell { 1206e7db06b5SDavid Brownell unsigned bad_bits; 1207caae070cSLaxman Dewangan int status = 0; 12087d077197SDavid Brownell 1209e7db06b5SDavid Brownell /* help drivers fail *cleanly* when they need options 1210e7db06b5SDavid Brownell * that aren't supported with their current master 1211e7db06b5SDavid Brownell */ 1212e7db06b5SDavid Brownell bad_bits = spi->mode & ~spi->master->mode_bits; 1213e7db06b5SDavid Brownell if (bad_bits) { 1214eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 1215e7db06b5SDavid Brownell bad_bits); 1216e7db06b5SDavid Brownell return -EINVAL; 1217e7db06b5SDavid Brownell } 1218e7db06b5SDavid Brownell 12197d077197SDavid Brownell if (!spi->bits_per_word) 12207d077197SDavid Brownell spi->bits_per_word = 8; 12217d077197SDavid Brownell 1222caae070cSLaxman Dewangan if (spi->master->setup) 12237d077197SDavid Brownell status = spi->master->setup(spi); 12247d077197SDavid Brownell 12257d077197SDavid Brownell dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s" 12267d077197SDavid Brownell "%u bits/w, %u Hz max --> %d\n", 12277d077197SDavid Brownell (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 12287d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 12297d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 12307d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 12317d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 12327d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 12337d077197SDavid Brownell status); 12347d077197SDavid Brownell 12357d077197SDavid Brownell return status; 12367d077197SDavid Brownell } 12377d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 12387d077197SDavid Brownell 1239cf32b71eSErnst Schwab static int __spi_async(struct spi_device *spi, struct spi_message *message) 1240cf32b71eSErnst Schwab { 1241cf32b71eSErnst Schwab struct spi_master *master = spi->master; 1242e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 1243cf32b71eSErnst Schwab 1244cf32b71eSErnst Schwab /* Half-duplex links include original MicroWire, and ones with 1245cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 1246cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 1247cf32b71eSErnst Schwab * software limitations. 1248cf32b71eSErnst Schwab */ 1249cf32b71eSErnst Schwab if ((master->flags & SPI_MASTER_HALF_DUPLEX) 1250cf32b71eSErnst Schwab || (spi->mode & SPI_3WIRE)) { 1251cf32b71eSErnst Schwab unsigned flags = master->flags; 1252cf32b71eSErnst Schwab 1253cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 1254cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 1255cf32b71eSErnst Schwab return -EINVAL; 1256cf32b71eSErnst Schwab if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) 1257cf32b71eSErnst Schwab return -EINVAL; 1258cf32b71eSErnst Schwab if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf) 1259cf32b71eSErnst Schwab return -EINVAL; 1260cf32b71eSErnst Schwab } 1261cf32b71eSErnst Schwab } 1262cf32b71eSErnst Schwab 1263e6811d1dSLaxman Dewangan /** 1264e6811d1dSLaxman Dewangan * Set transfer bits_per_word as spi device default if it is not 1265e6811d1dSLaxman Dewangan * set for this transfer. 1266e6811d1dSLaxman Dewangan */ 1267e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 1268e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 1269e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 1270e6811d1dSLaxman Dewangan } 1271e6811d1dSLaxman Dewangan 1272cf32b71eSErnst Schwab message->spi = spi; 1273cf32b71eSErnst Schwab message->status = -EINPROGRESS; 1274cf32b71eSErnst Schwab return master->transfer(spi, message); 1275cf32b71eSErnst Schwab } 1276cf32b71eSErnst Schwab 1277568d0697SDavid Brownell /** 1278568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 1279568d0697SDavid Brownell * @spi: device with which data will be exchanged 1280568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 1281568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 1282568d0697SDavid Brownell * 1283568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 1284568d0697SDavid Brownell * as well as from task contexts which can sleep. 1285568d0697SDavid Brownell * 1286568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 1287568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 1288568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 1289568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 1290568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 1291568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 1292568d0697SDavid Brownell * core or controller driver code. 1293568d0697SDavid Brownell * 1294568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 1295568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 1296568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 1297568d0697SDavid Brownell * time requirements, for example. 1298568d0697SDavid Brownell * 1299568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 1300568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 1301568d0697SDavid Brownell * Until returning from the associated message completion callback, 1302568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 1303568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 1304568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 1305568d0697SDavid Brownell */ 1306568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 1307568d0697SDavid Brownell { 1308568d0697SDavid Brownell struct spi_master *master = spi->master; 1309cf32b71eSErnst Schwab int ret; 1310cf32b71eSErnst Schwab unsigned long flags; 1311568d0697SDavid Brownell 1312cf32b71eSErnst Schwab spin_lock_irqsave(&master->bus_lock_spinlock, flags); 1313568d0697SDavid Brownell 1314cf32b71eSErnst Schwab if (master->bus_lock_flag) 1315cf32b71eSErnst Schwab ret = -EBUSY; 1316cf32b71eSErnst Schwab else 1317cf32b71eSErnst Schwab ret = __spi_async(spi, message); 1318568d0697SDavid Brownell 1319cf32b71eSErnst Schwab spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); 1320cf32b71eSErnst Schwab 1321cf32b71eSErnst Schwab return ret; 1322568d0697SDavid Brownell } 1323568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 1324568d0697SDavid Brownell 1325cf32b71eSErnst Schwab /** 1326cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 1327cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 1328cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 1329cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 1330cf32b71eSErnst Schwab * 1331cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 1332cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 1333cf32b71eSErnst Schwab * 1334cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 1335cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 1336cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 1337cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 1338cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 1339cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 1340cf32b71eSErnst Schwab * core or controller driver code. 1341cf32b71eSErnst Schwab * 1342cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 1343cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 1344cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 1345cf32b71eSErnst Schwab * time requirements, for example. 1346cf32b71eSErnst Schwab * 1347cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 1348cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 1349cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 1350cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 1351cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 1352cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 1353cf32b71eSErnst Schwab */ 1354cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message) 1355cf32b71eSErnst Schwab { 1356cf32b71eSErnst Schwab struct spi_master *master = spi->master; 1357cf32b71eSErnst Schwab int ret; 1358cf32b71eSErnst Schwab unsigned long flags; 1359cf32b71eSErnst Schwab 1360cf32b71eSErnst Schwab spin_lock_irqsave(&master->bus_lock_spinlock, flags); 1361cf32b71eSErnst Schwab 1362cf32b71eSErnst Schwab ret = __spi_async(spi, message); 1363cf32b71eSErnst Schwab 1364cf32b71eSErnst Schwab spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); 1365cf32b71eSErnst Schwab 1366cf32b71eSErnst Schwab return ret; 1367cf32b71eSErnst Schwab 1368cf32b71eSErnst Schwab } 1369cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked); 1370cf32b71eSErnst Schwab 13717d077197SDavid Brownell 13727d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 13737d077197SDavid Brownell 13747d077197SDavid Brownell /* Utility methods for SPI master protocol drivers, layered on 13757d077197SDavid Brownell * top of the core. Some other utility methods are defined as 13767d077197SDavid Brownell * inline functions. 13777d077197SDavid Brownell */ 13787d077197SDavid Brownell 13795d870c8eSAndrew Morton static void spi_complete(void *arg) 13805d870c8eSAndrew Morton { 13815d870c8eSAndrew Morton complete(arg); 13825d870c8eSAndrew Morton } 13835d870c8eSAndrew Morton 1384cf32b71eSErnst Schwab static int __spi_sync(struct spi_device *spi, struct spi_message *message, 1385cf32b71eSErnst Schwab int bus_locked) 1386cf32b71eSErnst Schwab { 1387cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 1388cf32b71eSErnst Schwab int status; 1389cf32b71eSErnst Schwab struct spi_master *master = spi->master; 1390cf32b71eSErnst Schwab 1391cf32b71eSErnst Schwab message->complete = spi_complete; 1392cf32b71eSErnst Schwab message->context = &done; 1393cf32b71eSErnst Schwab 1394cf32b71eSErnst Schwab if (!bus_locked) 1395cf32b71eSErnst Schwab mutex_lock(&master->bus_lock_mutex); 1396cf32b71eSErnst Schwab 1397cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 1398cf32b71eSErnst Schwab 1399cf32b71eSErnst Schwab if (!bus_locked) 1400cf32b71eSErnst Schwab mutex_unlock(&master->bus_lock_mutex); 1401cf32b71eSErnst Schwab 1402cf32b71eSErnst Schwab if (status == 0) { 1403cf32b71eSErnst Schwab wait_for_completion(&done); 1404cf32b71eSErnst Schwab status = message->status; 1405cf32b71eSErnst Schwab } 1406cf32b71eSErnst Schwab message->context = NULL; 1407cf32b71eSErnst Schwab return status; 1408cf32b71eSErnst Schwab } 1409cf32b71eSErnst Schwab 14108ae12a0dSDavid Brownell /** 14118ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 14128ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 14138ae12a0dSDavid Brownell * @message: describes the data transfers 141433e34dc6SDavid Brownell * Context: can sleep 14158ae12a0dSDavid Brownell * 14168ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 14178ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 14188ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 14198ae12a0dSDavid Brownell * 14208ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 14218ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 14228ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 14238ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 14248ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 14258ae12a0dSDavid Brownell * 14260c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 14270c868461SDavid Brownell * message will not be freed before this call returns. 14280c868461SDavid Brownell * 14299b938b74SMarc Pignat * It returns zero on success, else a negative error code. 14308ae12a0dSDavid Brownell */ 14318ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 14328ae12a0dSDavid Brownell { 1433cf32b71eSErnst Schwab return __spi_sync(spi, message, 0); 14348ae12a0dSDavid Brownell } 14358ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 14368ae12a0dSDavid Brownell 1437cf32b71eSErnst Schwab /** 1438cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 1439cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 1440cf32b71eSErnst Schwab * @message: describes the data transfers 1441cf32b71eSErnst Schwab * Context: can sleep 1442cf32b71eSErnst Schwab * 1443cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 1444cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 1445cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 1446cf32b71eSErnst Schwab * 1447cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 144825985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 1449cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 1450cf32b71eSErnst Schwab * 1451cf32b71eSErnst Schwab * It returns zero on success, else a negative error code. 1452cf32b71eSErnst Schwab */ 1453cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 1454cf32b71eSErnst Schwab { 1455cf32b71eSErnst Schwab return __spi_sync(spi, message, 1); 1456cf32b71eSErnst Schwab } 1457cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 1458cf32b71eSErnst Schwab 1459cf32b71eSErnst Schwab /** 1460cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 1461cf32b71eSErnst Schwab * @master: SPI bus master that should be locked for exclusive bus access 1462cf32b71eSErnst Schwab * Context: can sleep 1463cf32b71eSErnst Schwab * 1464cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 1465cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 1466cf32b71eSErnst Schwab * 1467cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 1468cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 1469cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 1470cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 1471cf32b71eSErnst Schwab * 1472cf32b71eSErnst Schwab * It returns zero on success, else a negative error code. 1473cf32b71eSErnst Schwab */ 1474cf32b71eSErnst Schwab int spi_bus_lock(struct spi_master *master) 1475cf32b71eSErnst Schwab { 1476cf32b71eSErnst Schwab unsigned long flags; 1477cf32b71eSErnst Schwab 1478cf32b71eSErnst Schwab mutex_lock(&master->bus_lock_mutex); 1479cf32b71eSErnst Schwab 1480cf32b71eSErnst Schwab spin_lock_irqsave(&master->bus_lock_spinlock, flags); 1481cf32b71eSErnst Schwab master->bus_lock_flag = 1; 1482cf32b71eSErnst Schwab spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); 1483cf32b71eSErnst Schwab 1484cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 1485cf32b71eSErnst Schwab 1486cf32b71eSErnst Schwab return 0; 1487cf32b71eSErnst Schwab } 1488cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 1489cf32b71eSErnst Schwab 1490cf32b71eSErnst Schwab /** 1491cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 1492cf32b71eSErnst Schwab * @master: SPI bus master that was locked for exclusive bus access 1493cf32b71eSErnst Schwab * Context: can sleep 1494cf32b71eSErnst Schwab * 1495cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 1496cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 1497cf32b71eSErnst Schwab * 1498cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 1499cf32b71eSErnst Schwab * call. 1500cf32b71eSErnst Schwab * 1501cf32b71eSErnst Schwab * It returns zero on success, else a negative error code. 1502cf32b71eSErnst Schwab */ 1503cf32b71eSErnst Schwab int spi_bus_unlock(struct spi_master *master) 1504cf32b71eSErnst Schwab { 1505cf32b71eSErnst Schwab master->bus_lock_flag = 0; 1506cf32b71eSErnst Schwab 1507cf32b71eSErnst Schwab mutex_unlock(&master->bus_lock_mutex); 1508cf32b71eSErnst Schwab 1509cf32b71eSErnst Schwab return 0; 1510cf32b71eSErnst Schwab } 1511cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 1512cf32b71eSErnst Schwab 1513a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 1514a9948b61SDavid Brownell #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) 15158ae12a0dSDavid Brownell 15168ae12a0dSDavid Brownell static u8 *buf; 15178ae12a0dSDavid Brownell 15188ae12a0dSDavid Brownell /** 15198ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 15208ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 15218ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 15228ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 152327570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 152427570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 152533e34dc6SDavid Brownell * Context: can sleep 15268ae12a0dSDavid Brownell * 15278ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 15288ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 15298ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 1530b885244eSDavid Brownell * This call may only be used from a context that may sleep. 15318ae12a0dSDavid Brownell * 15320c868461SDavid Brownell * Parameters to this routine are always copied using a small buffer; 153333e34dc6SDavid Brownell * portable code should never use this for more than 32 bytes. 153433e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 15350c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 15368ae12a0dSDavid Brownell */ 15378ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 15380c4a1590SMark Brown const void *txbuf, unsigned n_tx, 15390c4a1590SMark Brown void *rxbuf, unsigned n_rx) 15408ae12a0dSDavid Brownell { 1541068f4070SDavid Brownell static DEFINE_MUTEX(lock); 15428ae12a0dSDavid Brownell 15438ae12a0dSDavid Brownell int status; 15448ae12a0dSDavid Brownell struct spi_message message; 1545bdff549eSDavid Brownell struct spi_transfer x[2]; 15468ae12a0dSDavid Brownell u8 *local_buf; 15478ae12a0dSDavid Brownell 1548b3a223eeSMark Brown /* Use preallocated DMA-safe buffer if we can. We can't avoid 1549b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 1550b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 1551b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 15528ae12a0dSDavid Brownell */ 1553b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 1554*5323f498SGrant Likely local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), GFP_KERNEL); 1555b3a223eeSMark Brown if (!local_buf) 1556b3a223eeSMark Brown return -ENOMEM; 1557b3a223eeSMark Brown } else { 1558b3a223eeSMark Brown local_buf = buf; 1559b3a223eeSMark Brown } 15608ae12a0dSDavid Brownell 15618275c642SVitaly Wool spi_message_init(&message); 1562bdff549eSDavid Brownell memset(x, 0, sizeof x); 1563bdff549eSDavid Brownell if (n_tx) { 1564bdff549eSDavid Brownell x[0].len = n_tx; 1565bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 1566bdff549eSDavid Brownell } 1567bdff549eSDavid Brownell if (n_rx) { 1568bdff549eSDavid Brownell x[1].len = n_rx; 1569bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 1570bdff549eSDavid Brownell } 15718275c642SVitaly Wool 15728ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 1573bdff549eSDavid Brownell x[0].tx_buf = local_buf; 1574bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 15758ae12a0dSDavid Brownell 15768ae12a0dSDavid Brownell /* do the i/o */ 15778ae12a0dSDavid Brownell status = spi_sync(spi, &message); 15789b938b74SMarc Pignat if (status == 0) 1579bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 15808ae12a0dSDavid Brownell 1581bdff549eSDavid Brownell if (x[0].tx_buf == buf) 1582068f4070SDavid Brownell mutex_unlock(&lock); 15838ae12a0dSDavid Brownell else 15848ae12a0dSDavid Brownell kfree(local_buf); 15858ae12a0dSDavid Brownell 15868ae12a0dSDavid Brownell return status; 15878ae12a0dSDavid Brownell } 15888ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 15898ae12a0dSDavid Brownell 15908ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 15918ae12a0dSDavid Brownell 15928ae12a0dSDavid Brownell static int __init spi_init(void) 15938ae12a0dSDavid Brownell { 1594b885244eSDavid Brownell int status; 15958ae12a0dSDavid Brownell 1596e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 1597b885244eSDavid Brownell if (!buf) { 1598b885244eSDavid Brownell status = -ENOMEM; 1599b885244eSDavid Brownell goto err0; 16008ae12a0dSDavid Brownell } 1601b885244eSDavid Brownell 1602b885244eSDavid Brownell status = bus_register(&spi_bus_type); 1603b885244eSDavid Brownell if (status < 0) 1604b885244eSDavid Brownell goto err1; 1605b885244eSDavid Brownell 1606b885244eSDavid Brownell status = class_register(&spi_master_class); 1607b885244eSDavid Brownell if (status < 0) 1608b885244eSDavid Brownell goto err2; 1609b885244eSDavid Brownell return 0; 1610b885244eSDavid Brownell 1611b885244eSDavid Brownell err2: 1612b885244eSDavid Brownell bus_unregister(&spi_bus_type); 1613b885244eSDavid Brownell err1: 1614b885244eSDavid Brownell kfree(buf); 1615b885244eSDavid Brownell buf = NULL; 1616b885244eSDavid Brownell err0: 1617b885244eSDavid Brownell return status; 1618b885244eSDavid Brownell } 1619b885244eSDavid Brownell 16208ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 16218ae12a0dSDavid Brownell * but even essential drivers wait till later 1622b885244eSDavid Brownell * 1623b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 1624b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 1625b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 16268ae12a0dSDavid Brownell */ 1627673c0c00SDavid Brownell postcore_initcall(spi_init); 16288ae12a0dSDavid Brownell 1629