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> 268ae12a0dSDavid Brownell #include <linux/spi/spi.h> 278ae12a0dSDavid Brownell 288ae12a0dSDavid Brownell 29b885244eSDavid Brownell /* SPI bustype and spi_master class are registered after board init code 30b885244eSDavid Brownell * provides the SPI device tables, ensuring that both are present by the 31b885244eSDavid Brownell * time controller driver registration causes spi_devices to "enumerate". 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); 428ae12a0dSDavid Brownell kfree(dev); 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 508ae12a0dSDavid Brownell return snprintf(buf, BUS_ID_SIZE + 1, "%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 628ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 638ae12a0dSDavid Brownell { 648ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 658ae12a0dSDavid Brownell 668ae12a0dSDavid Brownell return strncmp(spi->modalias, drv->name, BUS_ID_SIZE) == 0; 678ae12a0dSDavid Brownell } 688ae12a0dSDavid Brownell 697eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 708ae12a0dSDavid Brownell { 718ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 728ae12a0dSDavid Brownell 737eff2e7aSKay Sievers add_uevent_var(env, "MODALIAS=%s", spi->modalias); 748ae12a0dSDavid Brownell return 0; 758ae12a0dSDavid Brownell } 768ae12a0dSDavid Brownell 778ae12a0dSDavid Brownell #ifdef CONFIG_PM 788ae12a0dSDavid Brownell 798ae12a0dSDavid Brownell static int spi_suspend(struct device *dev, pm_message_t message) 808ae12a0dSDavid Brownell { 813c72426fSDavid Brownell int value = 0; 82b885244eSDavid Brownell struct spi_driver *drv = to_spi_driver(dev->driver); 838ae12a0dSDavid Brownell 848ae12a0dSDavid Brownell /* suspend will stop irqs and dma; no more i/o */ 853c72426fSDavid Brownell if (drv) { 863c72426fSDavid Brownell if (drv->suspend) 87b885244eSDavid Brownell value = drv->suspend(to_spi_device(dev), message); 883c72426fSDavid Brownell else 893c72426fSDavid Brownell dev_dbg(dev, "... can't suspend\n"); 903c72426fSDavid Brownell } 918ae12a0dSDavid Brownell return value; 928ae12a0dSDavid Brownell } 938ae12a0dSDavid Brownell 948ae12a0dSDavid Brownell static int spi_resume(struct device *dev) 958ae12a0dSDavid Brownell { 963c72426fSDavid Brownell int value = 0; 97b885244eSDavid Brownell struct spi_driver *drv = to_spi_driver(dev->driver); 988ae12a0dSDavid Brownell 998ae12a0dSDavid Brownell /* resume may restart the i/o queue */ 1003c72426fSDavid Brownell if (drv) { 1013c72426fSDavid Brownell if (drv->resume) 102b885244eSDavid Brownell value = drv->resume(to_spi_device(dev)); 1033c72426fSDavid Brownell else 1043c72426fSDavid Brownell dev_dbg(dev, "... can't resume\n"); 1053c72426fSDavid Brownell } 1068ae12a0dSDavid Brownell return value; 1078ae12a0dSDavid Brownell } 1088ae12a0dSDavid Brownell 1098ae12a0dSDavid Brownell #else 1108ae12a0dSDavid Brownell #define spi_suspend NULL 1118ae12a0dSDavid Brownell #define spi_resume NULL 1128ae12a0dSDavid Brownell #endif 1138ae12a0dSDavid Brownell 1148ae12a0dSDavid Brownell struct bus_type spi_bus_type = { 1158ae12a0dSDavid Brownell .name = "spi", 1168ae12a0dSDavid Brownell .dev_attrs = spi_dev_attrs, 1178ae12a0dSDavid Brownell .match = spi_match_device, 1188ae12a0dSDavid Brownell .uevent = spi_uevent, 1198ae12a0dSDavid Brownell .suspend = spi_suspend, 1208ae12a0dSDavid Brownell .resume = spi_resume, 1218ae12a0dSDavid Brownell }; 1228ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type); 1238ae12a0dSDavid Brownell 124b885244eSDavid Brownell 125b885244eSDavid Brownell static int spi_drv_probe(struct device *dev) 126b885244eSDavid Brownell { 127b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 128b885244eSDavid Brownell 129b885244eSDavid Brownell return sdrv->probe(to_spi_device(dev)); 130b885244eSDavid Brownell } 131b885244eSDavid Brownell 132b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 133b885244eSDavid Brownell { 134b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 135b885244eSDavid Brownell 136b885244eSDavid Brownell return sdrv->remove(to_spi_device(dev)); 137b885244eSDavid Brownell } 138b885244eSDavid Brownell 139b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 140b885244eSDavid Brownell { 141b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 142b885244eSDavid Brownell 143b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 144b885244eSDavid Brownell } 145b885244eSDavid Brownell 14633e34dc6SDavid Brownell /** 14733e34dc6SDavid Brownell * spi_register_driver - register a SPI driver 14833e34dc6SDavid Brownell * @sdrv: the driver to register 14933e34dc6SDavid Brownell * Context: can sleep 15033e34dc6SDavid Brownell */ 151b885244eSDavid Brownell int spi_register_driver(struct spi_driver *sdrv) 152b885244eSDavid Brownell { 153b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 154b885244eSDavid Brownell if (sdrv->probe) 155b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 156b885244eSDavid Brownell if (sdrv->remove) 157b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 158b885244eSDavid Brownell if (sdrv->shutdown) 159b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 160b885244eSDavid Brownell return driver_register(&sdrv->driver); 161b885244eSDavid Brownell } 162b885244eSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_driver); 163b885244eSDavid Brownell 1648ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 1658ae12a0dSDavid Brownell 1668ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 1678ae12a0dSDavid Brownell * would make them board-specific. Similarly with SPI master drivers. 1688ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 1698ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 1708ae12a0dSDavid Brownell */ 1718ae12a0dSDavid Brownell 1728ae12a0dSDavid Brownell struct boardinfo { 1738ae12a0dSDavid Brownell struct list_head list; 1748ae12a0dSDavid Brownell unsigned n_board_info; 1758ae12a0dSDavid Brownell struct spi_board_info board_info[0]; 1768ae12a0dSDavid Brownell }; 1778ae12a0dSDavid Brownell 1788ae12a0dSDavid Brownell static LIST_HEAD(board_list); 17994040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 1808ae12a0dSDavid Brownell 1818ae12a0dSDavid Brownell 18233e34dc6SDavid Brownell /** 18333e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 18433e34dc6SDavid Brownell * @master: Controller to which device is connected 18533e34dc6SDavid Brownell * @chip: Describes the SPI device 18633e34dc6SDavid Brownell * Context: can sleep 18733e34dc6SDavid Brownell * 18833e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 1898ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 1908ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 1918ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 1928ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 193082c8cb4SDavid Brownell * 194082c8cb4SDavid Brownell * Returns the new device, or NULL. 1958ae12a0dSDavid Brownell */ 196e9d5a461SAdrian Bunk struct spi_device *spi_new_device(struct spi_master *master, 197e9d5a461SAdrian Bunk struct spi_board_info *chip) 1988ae12a0dSDavid Brownell { 1998ae12a0dSDavid Brownell struct spi_device *proxy; 20049dce689STony Jones struct device *dev = master->dev.parent; 2018ae12a0dSDavid Brownell int status; 2028ae12a0dSDavid Brownell 203082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 204082c8cb4SDavid Brownell * 205082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 206082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 207082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 208082c8cb4SDavid Brownell */ 209082c8cb4SDavid Brownell 210082c8cb4SDavid Brownell /* Chipselects are numbered 0..max; validate. */ 211082c8cb4SDavid Brownell if (chip->chip_select >= master->num_chipselect) { 212082c8cb4SDavid Brownell dev_err(dev, "cs%d > max %d\n", 213082c8cb4SDavid Brownell chip->chip_select, 214082c8cb4SDavid Brownell master->num_chipselect); 215082c8cb4SDavid Brownell return NULL; 216082c8cb4SDavid Brownell } 2178ae12a0dSDavid Brownell 2180c868461SDavid Brownell if (!spi_master_get(master)) 2198ae12a0dSDavid Brownell return NULL; 2208ae12a0dSDavid Brownell 221*102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 222*102eb975SGrant Likely 2238ae12a0dSDavid Brownell proxy = kzalloc(sizeof *proxy, GFP_KERNEL); 2248ae12a0dSDavid Brownell if (!proxy) { 2258ae12a0dSDavid Brownell dev_err(dev, "can't alloc dev for cs%d\n", 2268ae12a0dSDavid Brownell chip->chip_select); 2278ae12a0dSDavid Brownell goto fail; 2288ae12a0dSDavid Brownell } 2298ae12a0dSDavid Brownell proxy->master = master; 2308ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 2318ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 232980a01c9SDavid Brownell proxy->mode = chip->mode; 2338ae12a0dSDavid Brownell proxy->irq = chip->irq; 234*102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 2358ae12a0dSDavid Brownell 2368ae12a0dSDavid Brownell snprintf(proxy->dev.bus_id, sizeof proxy->dev.bus_id, 23749dce689STony Jones "%s.%u", master->dev.bus_id, 2388ae12a0dSDavid Brownell chip->chip_select); 2398ae12a0dSDavid Brownell proxy->dev.parent = dev; 2408ae12a0dSDavid Brownell proxy->dev.bus = &spi_bus_type; 2418ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 2428ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 2438ae12a0dSDavid Brownell proxy->controller_state = NULL; 2448ae12a0dSDavid Brownell proxy->dev.release = spidev_release; 2458ae12a0dSDavid Brownell 246082c8cb4SDavid Brownell /* drivers may modify this initial i/o setup */ 2478ae12a0dSDavid Brownell status = master->setup(proxy); 2488ae12a0dSDavid Brownell if (status < 0) { 249082c8cb4SDavid Brownell dev_err(dev, "can't %s %s, status %d\n", 2508ae12a0dSDavid Brownell "setup", proxy->dev.bus_id, status); 2518ae12a0dSDavid Brownell goto fail; 2528ae12a0dSDavid Brownell } 2538ae12a0dSDavid Brownell 2548ae12a0dSDavid Brownell /* driver core catches callers that misbehave by defining 2558ae12a0dSDavid Brownell * devices that already exist. 2568ae12a0dSDavid Brownell */ 2578ae12a0dSDavid Brownell status = device_register(&proxy->dev); 2588ae12a0dSDavid Brownell if (status < 0) { 259082c8cb4SDavid Brownell dev_err(dev, "can't %s %s, status %d\n", 2608ae12a0dSDavid Brownell "add", proxy->dev.bus_id, status); 261b885244eSDavid Brownell goto fail; 262b885244eSDavid Brownell } 263b885244eSDavid Brownell dev_dbg(dev, "registered child %s\n", proxy->dev.bus_id); 264b885244eSDavid Brownell return proxy; 265b885244eSDavid Brownell 2668ae12a0dSDavid Brownell fail: 2670c868461SDavid Brownell spi_master_put(master); 2688ae12a0dSDavid Brownell kfree(proxy); 2698ae12a0dSDavid Brownell return NULL; 2708ae12a0dSDavid Brownell } 2718ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 2728ae12a0dSDavid Brownell 27333e34dc6SDavid Brownell /** 27433e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 27533e34dc6SDavid Brownell * @info: array of chip descriptors 27633e34dc6SDavid Brownell * @n: how many descriptors are provided 27733e34dc6SDavid Brownell * Context: can sleep 27833e34dc6SDavid Brownell * 2798ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 2808ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 2818ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 2828ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 2838ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 2848ae12a0dSDavid Brownell * 2858ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 2868ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 2878ae12a0dSDavid Brownell * would naturally declare its SPI devices. 2888ae12a0dSDavid Brownell * 2898ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 2908ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 2918ae12a0dSDavid Brownell */ 2928ae12a0dSDavid Brownell int __init 2938ae12a0dSDavid Brownell spi_register_board_info(struct spi_board_info const *info, unsigned n) 2948ae12a0dSDavid Brownell { 2958ae12a0dSDavid Brownell struct boardinfo *bi; 2968ae12a0dSDavid Brownell 297b885244eSDavid Brownell bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL); 2988ae12a0dSDavid Brownell if (!bi) 2998ae12a0dSDavid Brownell return -ENOMEM; 3008ae12a0dSDavid Brownell bi->n_board_info = n; 301b885244eSDavid Brownell memcpy(bi->board_info, info, n * sizeof *info); 3028ae12a0dSDavid Brownell 30394040828SMatthias Kaehlcke mutex_lock(&board_lock); 3048ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 30594040828SMatthias Kaehlcke mutex_unlock(&board_lock); 3068ae12a0dSDavid Brownell return 0; 3078ae12a0dSDavid Brownell } 3088ae12a0dSDavid Brownell 3098ae12a0dSDavid Brownell /* FIXME someone should add support for a __setup("spi", ...) that 3108ae12a0dSDavid Brownell * creates board info from kernel command lines 3118ae12a0dSDavid Brownell */ 3128ae12a0dSDavid Brownell 313149a6501SAdrian Bunk static void scan_boardinfo(struct spi_master *master) 3148ae12a0dSDavid Brownell { 3158ae12a0dSDavid Brownell struct boardinfo *bi; 3168ae12a0dSDavid Brownell 31794040828SMatthias Kaehlcke mutex_lock(&board_lock); 3188ae12a0dSDavid Brownell list_for_each_entry(bi, &board_list, list) { 3198ae12a0dSDavid Brownell struct spi_board_info *chip = bi->board_info; 3208ae12a0dSDavid Brownell unsigned n; 3218ae12a0dSDavid Brownell 3228ae12a0dSDavid Brownell for (n = bi->n_board_info; n > 0; n--, chip++) { 3238ae12a0dSDavid Brownell if (chip->bus_num != master->bus_num) 3248ae12a0dSDavid Brownell continue; 325082c8cb4SDavid Brownell /* NOTE: this relies on spi_new_device to 326082c8cb4SDavid Brownell * issue diagnostics when given bogus inputs 3278ae12a0dSDavid Brownell */ 3288ae12a0dSDavid Brownell (void) spi_new_device(master, chip); 3298ae12a0dSDavid Brownell } 3308ae12a0dSDavid Brownell } 33194040828SMatthias Kaehlcke mutex_unlock(&board_lock); 3328ae12a0dSDavid Brownell } 3338ae12a0dSDavid Brownell 3348ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 3358ae12a0dSDavid Brownell 33649dce689STony Jones static void spi_master_release(struct device *dev) 3378ae12a0dSDavid Brownell { 3388ae12a0dSDavid Brownell struct spi_master *master; 3398ae12a0dSDavid Brownell 34049dce689STony Jones master = container_of(dev, struct spi_master, dev); 3418ae12a0dSDavid Brownell kfree(master); 3428ae12a0dSDavid Brownell } 3438ae12a0dSDavid Brownell 3448ae12a0dSDavid Brownell static struct class spi_master_class = { 3458ae12a0dSDavid Brownell .name = "spi_master", 3468ae12a0dSDavid Brownell .owner = THIS_MODULE, 34749dce689STony Jones .dev_release = spi_master_release, 3488ae12a0dSDavid Brownell }; 3498ae12a0dSDavid Brownell 3508ae12a0dSDavid Brownell 3518ae12a0dSDavid Brownell /** 3528ae12a0dSDavid Brownell * spi_alloc_master - allocate SPI master controller 3538ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 35433e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 35549dce689STony Jones * memory is in the driver_data field of the returned device, 3560c868461SDavid Brownell * accessible with spi_master_get_devdata(). 35733e34dc6SDavid Brownell * Context: can sleep 3588ae12a0dSDavid Brownell * 3598ae12a0dSDavid Brownell * This call is used only by SPI master controller drivers, which are the 3608ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 361ba1a0513Sdmitry pervushin * an spi_master structure, prior to calling spi_register_master(). 3628ae12a0dSDavid Brownell * 3638ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns the SPI 3648ae12a0dSDavid Brownell * master structure on success, else NULL. 3658ae12a0dSDavid Brownell * 3668ae12a0dSDavid Brownell * The caller is responsible for assigning the bus number and initializing 367ba1a0513Sdmitry pervushin * the master's methods before calling spi_register_master(); and (after errors 3680c868461SDavid Brownell * adding the device) calling spi_master_put() to prevent a memory leak. 3698ae12a0dSDavid Brownell */ 370e9d5a461SAdrian Bunk struct spi_master *spi_alloc_master(struct device *dev, unsigned size) 3718ae12a0dSDavid Brownell { 3728ae12a0dSDavid Brownell struct spi_master *master; 3738ae12a0dSDavid Brownell 3740c868461SDavid Brownell if (!dev) 3750c868461SDavid Brownell return NULL; 3760c868461SDavid Brownell 377e94b1766SChristoph Lameter master = kzalloc(size + sizeof *master, GFP_KERNEL); 3788ae12a0dSDavid Brownell if (!master) 3798ae12a0dSDavid Brownell return NULL; 3808ae12a0dSDavid Brownell 38149dce689STony Jones device_initialize(&master->dev); 38249dce689STony Jones master->dev.class = &spi_master_class; 38349dce689STony Jones master->dev.parent = get_device(dev); 3840c868461SDavid Brownell spi_master_set_devdata(master, &master[1]); 3858ae12a0dSDavid Brownell 3868ae12a0dSDavid Brownell return master; 3878ae12a0dSDavid Brownell } 3888ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_alloc_master); 3898ae12a0dSDavid Brownell 3908ae12a0dSDavid Brownell /** 3918ae12a0dSDavid Brownell * spi_register_master - register SPI master controller 3928ae12a0dSDavid Brownell * @master: initialized master, originally from spi_alloc_master() 39333e34dc6SDavid Brownell * Context: can sleep 3948ae12a0dSDavid Brownell * 3958ae12a0dSDavid Brownell * SPI master controllers connect to their drivers using some non-SPI bus, 3968ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 3978ae12a0dSDavid Brownell * includes calling spi_register_master() to hook up to this SPI bus glue. 3988ae12a0dSDavid Brownell * 3998ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 4008ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 4018ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 4028ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 4038ae12a0dSDavid Brownell * chip is at which address. 4048ae12a0dSDavid Brownell * 4058ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 4068ae12a0dSDavid Brownell * success, else a negative error code (dropping the master's refcount). 4070c868461SDavid Brownell * After a successful return, the caller is responsible for calling 4080c868461SDavid Brownell * spi_unregister_master(). 4098ae12a0dSDavid Brownell */ 410e9d5a461SAdrian Bunk int spi_register_master(struct spi_master *master) 4118ae12a0dSDavid Brownell { 412e44a45aeSDavid Brownell static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); 41349dce689STony Jones struct device *dev = master->dev.parent; 4148ae12a0dSDavid Brownell int status = -ENODEV; 4158ae12a0dSDavid Brownell int dynamic = 0; 4168ae12a0dSDavid Brownell 4170c868461SDavid Brownell if (!dev) 4180c868461SDavid Brownell return -ENODEV; 4190c868461SDavid Brownell 420082c8cb4SDavid Brownell /* even if it's just one always-selected device, there must 421082c8cb4SDavid Brownell * be at least one chipselect 422082c8cb4SDavid Brownell */ 423082c8cb4SDavid Brownell if (master->num_chipselect == 0) 424082c8cb4SDavid Brownell return -EINVAL; 425082c8cb4SDavid Brownell 4268ae12a0dSDavid Brownell /* convention: dynamically assigned bus IDs count down from the max */ 427a020ed75SDavid Brownell if (master->bus_num < 0) { 428082c8cb4SDavid Brownell /* FIXME switch to an IDR based scheme, something like 429082c8cb4SDavid Brownell * I2C now uses, so we can't run out of "dynamic" IDs 430082c8cb4SDavid Brownell */ 4318ae12a0dSDavid Brownell master->bus_num = atomic_dec_return(&dyn_bus_id); 432b885244eSDavid Brownell dynamic = 1; 4338ae12a0dSDavid Brownell } 4348ae12a0dSDavid Brownell 4358ae12a0dSDavid Brownell /* register the device, then userspace will see it. 4368ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 4378ae12a0dSDavid Brownell */ 43849dce689STony Jones snprintf(master->dev.bus_id, sizeof master->dev.bus_id, 4398ae12a0dSDavid Brownell "spi%u", master->bus_num); 44049dce689STony Jones status = device_add(&master->dev); 441b885244eSDavid Brownell if (status < 0) 4428ae12a0dSDavid Brownell goto done; 44349dce689STony Jones dev_dbg(dev, "registered master %s%s\n", master->dev.bus_id, 4448ae12a0dSDavid Brownell dynamic ? " (dynamic)" : ""); 4458ae12a0dSDavid Brownell 4468ae12a0dSDavid Brownell /* populate children from any spi device tables */ 4478ae12a0dSDavid Brownell scan_boardinfo(master); 4488ae12a0dSDavid Brownell status = 0; 4498ae12a0dSDavid Brownell done: 4508ae12a0dSDavid Brownell return status; 4518ae12a0dSDavid Brownell } 4528ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_master); 4538ae12a0dSDavid Brownell 4548ae12a0dSDavid Brownell 455350d0076SAtsushi Nemoto static int __unregister(struct device *dev, void *master_dev) 4568ae12a0dSDavid Brownell { 4578ae12a0dSDavid Brownell /* note: before about 2.6.14-rc1 this would corrupt memory: */ 458350d0076SAtsushi Nemoto if (dev != master_dev) 4590c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 4608ae12a0dSDavid Brownell return 0; 4618ae12a0dSDavid Brownell } 4628ae12a0dSDavid Brownell 4638ae12a0dSDavid Brownell /** 4648ae12a0dSDavid Brownell * spi_unregister_master - unregister SPI master controller 4658ae12a0dSDavid Brownell * @master: the master being unregistered 46633e34dc6SDavid Brownell * Context: can sleep 4678ae12a0dSDavid Brownell * 4688ae12a0dSDavid Brownell * This call is used only by SPI master controller drivers, which are the 4698ae12a0dSDavid Brownell * only ones directly touching chip registers. 4708ae12a0dSDavid Brownell * 4718ae12a0dSDavid Brownell * This must be called from context that can sleep. 4728ae12a0dSDavid Brownell */ 4738ae12a0dSDavid Brownell void spi_unregister_master(struct spi_master *master) 4748ae12a0dSDavid Brownell { 47589fc9a1aSJeff Garzik int dummy; 47689fc9a1aSJeff Garzik 477350d0076SAtsushi Nemoto dummy = device_for_each_child(master->dev.parent, &master->dev, 478350d0076SAtsushi Nemoto __unregister); 47949dce689STony Jones device_unregister(&master->dev); 4808ae12a0dSDavid Brownell } 4818ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_unregister_master); 4828ae12a0dSDavid Brownell 4835ed2c832SDave Young static int __spi_master_match(struct device *dev, void *data) 4845ed2c832SDave Young { 4855ed2c832SDave Young struct spi_master *m; 4865ed2c832SDave Young u16 *bus_num = data; 4875ed2c832SDave Young 4885ed2c832SDave Young m = container_of(dev, struct spi_master, dev); 4895ed2c832SDave Young return m->bus_num == *bus_num; 4905ed2c832SDave Young } 4915ed2c832SDave Young 4928ae12a0dSDavid Brownell /** 4938ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 4948ae12a0dSDavid Brownell * @bus_num: the master's bus number 49533e34dc6SDavid Brownell * Context: can sleep 4968ae12a0dSDavid Brownell * 4978ae12a0dSDavid Brownell * This call may be used with devices that are registered after 4988ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 4998ae12a0dSDavid Brownell * spi_master (which the caller must release), or NULL if there is 5008ae12a0dSDavid Brownell * no such master registered. 5018ae12a0dSDavid Brownell */ 5028ae12a0dSDavid Brownell struct spi_master *spi_busnum_to_master(u16 bus_num) 5038ae12a0dSDavid Brownell { 50449dce689STony Jones struct device *dev; 5051e9a51dcSAtsushi Nemoto struct spi_master *master = NULL; 5068ae12a0dSDavid Brownell 507695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 5085ed2c832SDave Young __spi_master_match); 5095ed2c832SDave Young if (dev) 5105ed2c832SDave Young master = container_of(dev, struct spi_master, dev); 5115ed2c832SDave Young /* reference got in class_find_device */ 5121e9a51dcSAtsushi Nemoto return master; 5138ae12a0dSDavid Brownell } 5148ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 5158ae12a0dSDavid Brownell 5168ae12a0dSDavid Brownell 5178ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 5188ae12a0dSDavid Brownell 5195d870c8eSAndrew Morton static void spi_complete(void *arg) 5205d870c8eSAndrew Morton { 5215d870c8eSAndrew Morton complete(arg); 5225d870c8eSAndrew Morton } 5235d870c8eSAndrew Morton 5248ae12a0dSDavid Brownell /** 5258ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 5268ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 5278ae12a0dSDavid Brownell * @message: describes the data transfers 52833e34dc6SDavid Brownell * Context: can sleep 5298ae12a0dSDavid Brownell * 5308ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 5318ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 5328ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 5338ae12a0dSDavid Brownell * 5348ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 5358ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 5368ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 5378ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 5388ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 5398ae12a0dSDavid Brownell * 5400c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 5410c868461SDavid Brownell * message will not be freed before this call returns. 5420c868461SDavid Brownell * 5439b938b74SMarc Pignat * It returns zero on success, else a negative error code. 5448ae12a0dSDavid Brownell */ 5458ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 5468ae12a0dSDavid Brownell { 54760be6b9aSIngo Molnar DECLARE_COMPLETION_ONSTACK(done); 5488ae12a0dSDavid Brownell int status; 5498ae12a0dSDavid Brownell 5505d870c8eSAndrew Morton message->complete = spi_complete; 5518ae12a0dSDavid Brownell message->context = &done; 5528ae12a0dSDavid Brownell status = spi_async(spi, message); 5539b938b74SMarc Pignat if (status == 0) { 5548ae12a0dSDavid Brownell wait_for_completion(&done); 5559b938b74SMarc Pignat status = message->status; 5569b938b74SMarc Pignat } 5578ae12a0dSDavid Brownell message->context = NULL; 5588ae12a0dSDavid Brownell return status; 5598ae12a0dSDavid Brownell } 5608ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 5618ae12a0dSDavid Brownell 562a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 563a9948b61SDavid Brownell #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) 5648ae12a0dSDavid Brownell 5658ae12a0dSDavid Brownell static u8 *buf; 5668ae12a0dSDavid Brownell 5678ae12a0dSDavid Brownell /** 5688ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 5698ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 5708ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 5718ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 5728ae12a0dSDavid Brownell * @rxbuf: buffer into which data will be read 5738ae12a0dSDavid Brownell * @n_rx: size of rxbuf, in bytes (need not be dma-safe) 57433e34dc6SDavid Brownell * Context: can sleep 5758ae12a0dSDavid Brownell * 5768ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 5778ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 5788ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 579b885244eSDavid Brownell * This call may only be used from a context that may sleep. 5808ae12a0dSDavid Brownell * 5810c868461SDavid Brownell * Parameters to this routine are always copied using a small buffer; 58233e34dc6SDavid Brownell * portable code should never use this for more than 32 bytes. 58333e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 5840c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 5858ae12a0dSDavid Brownell */ 5868ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 5878ae12a0dSDavid Brownell const u8 *txbuf, unsigned n_tx, 5888ae12a0dSDavid Brownell u8 *rxbuf, unsigned n_rx) 5898ae12a0dSDavid Brownell { 590068f4070SDavid Brownell static DEFINE_MUTEX(lock); 5918ae12a0dSDavid Brownell 5928ae12a0dSDavid Brownell int status; 5938ae12a0dSDavid Brownell struct spi_message message; 5948ae12a0dSDavid Brownell struct spi_transfer x[2]; 5958ae12a0dSDavid Brownell u8 *local_buf; 5968ae12a0dSDavid Brownell 5978ae12a0dSDavid Brownell /* Use preallocated DMA-safe buffer. We can't avoid copying here, 5988ae12a0dSDavid Brownell * (as a pure convenience thing), but we can keep heap costs 5998ae12a0dSDavid Brownell * out of the hot path ... 6008ae12a0dSDavid Brownell */ 6018ae12a0dSDavid Brownell if ((n_tx + n_rx) > SPI_BUFSIZ) 6028ae12a0dSDavid Brownell return -EINVAL; 6038ae12a0dSDavid Brownell 6048275c642SVitaly Wool spi_message_init(&message); 6058275c642SVitaly Wool memset(x, 0, sizeof x); 6068275c642SVitaly Wool if (n_tx) { 6078275c642SVitaly Wool x[0].len = n_tx; 6088275c642SVitaly Wool spi_message_add_tail(&x[0], &message); 6098275c642SVitaly Wool } 6108275c642SVitaly Wool if (n_rx) { 6118275c642SVitaly Wool x[1].len = n_rx; 6128275c642SVitaly Wool spi_message_add_tail(&x[1], &message); 6138275c642SVitaly Wool } 6148275c642SVitaly Wool 6158ae12a0dSDavid Brownell /* ... unless someone else is using the pre-allocated buffer */ 616068f4070SDavid Brownell if (!mutex_trylock(&lock)) { 6178ae12a0dSDavid Brownell local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 6188ae12a0dSDavid Brownell if (!local_buf) 6198ae12a0dSDavid Brownell return -ENOMEM; 6208ae12a0dSDavid Brownell } else 6218ae12a0dSDavid Brownell local_buf = buf; 6228ae12a0dSDavid Brownell 6238ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 6248ae12a0dSDavid Brownell x[0].tx_buf = local_buf; 6258ae12a0dSDavid Brownell x[1].rx_buf = local_buf + n_tx; 6268ae12a0dSDavid Brownell 6278ae12a0dSDavid Brownell /* do the i/o */ 6288ae12a0dSDavid Brownell status = spi_sync(spi, &message); 6299b938b74SMarc Pignat if (status == 0) 6308ae12a0dSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 6318ae12a0dSDavid Brownell 6328ae12a0dSDavid Brownell if (x[0].tx_buf == buf) 633068f4070SDavid Brownell mutex_unlock(&lock); 6348ae12a0dSDavid Brownell else 6358ae12a0dSDavid Brownell kfree(local_buf); 6368ae12a0dSDavid Brownell 6378ae12a0dSDavid Brownell return status; 6388ae12a0dSDavid Brownell } 6398ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 6408ae12a0dSDavid Brownell 6418ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 6428ae12a0dSDavid Brownell 6438ae12a0dSDavid Brownell static int __init spi_init(void) 6448ae12a0dSDavid Brownell { 645b885244eSDavid Brownell int status; 6468ae12a0dSDavid Brownell 647e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 648b885244eSDavid Brownell if (!buf) { 649b885244eSDavid Brownell status = -ENOMEM; 650b885244eSDavid Brownell goto err0; 6518ae12a0dSDavid Brownell } 652b885244eSDavid Brownell 653b885244eSDavid Brownell status = bus_register(&spi_bus_type); 654b885244eSDavid Brownell if (status < 0) 655b885244eSDavid Brownell goto err1; 656b885244eSDavid Brownell 657b885244eSDavid Brownell status = class_register(&spi_master_class); 658b885244eSDavid Brownell if (status < 0) 659b885244eSDavid Brownell goto err2; 660b885244eSDavid Brownell return 0; 661b885244eSDavid Brownell 662b885244eSDavid Brownell err2: 663b885244eSDavid Brownell bus_unregister(&spi_bus_type); 664b885244eSDavid Brownell err1: 665b885244eSDavid Brownell kfree(buf); 666b885244eSDavid Brownell buf = NULL; 667b885244eSDavid Brownell err0: 668b885244eSDavid Brownell return status; 669b885244eSDavid Brownell } 670b885244eSDavid Brownell 6718ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 6728ae12a0dSDavid Brownell * but even essential drivers wait till later 673b885244eSDavid Brownell * 674b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 675b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 676b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 6778ae12a0dSDavid Brownell */ 6788ae12a0dSDavid Brownell subsys_initcall(spi_init); 6798ae12a0dSDavid Brownell 680