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/autoconf.h> 228ae12a0dSDavid Brownell #include <linux/kernel.h> 238ae12a0dSDavid Brownell #include <linux/device.h> 248ae12a0dSDavid Brownell #include <linux/init.h> 258ae12a0dSDavid Brownell #include <linux/cache.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 { 358ae12a0dSDavid Brownell const 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 698ae12a0dSDavid Brownell static int spi_uevent(struct device *dev, char **envp, int num_envp, 708ae12a0dSDavid Brownell char *buffer, int buffer_size) 718ae12a0dSDavid Brownell { 728ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 738ae12a0dSDavid Brownell 748ae12a0dSDavid Brownell envp[0] = buffer; 758ae12a0dSDavid Brownell snprintf(buffer, buffer_size, "MODALIAS=%s", spi->modalias); 768ae12a0dSDavid Brownell envp[1] = NULL; 778ae12a0dSDavid Brownell return 0; 788ae12a0dSDavid Brownell } 798ae12a0dSDavid Brownell 808ae12a0dSDavid Brownell #ifdef CONFIG_PM 818ae12a0dSDavid Brownell 82b885244eSDavid Brownell /* 838ae12a0dSDavid Brownell * NOTE: the suspend() method for an spi_master controller driver 848ae12a0dSDavid Brownell * should verify that all its child devices are marked as suspended; 858ae12a0dSDavid Brownell * suspend requests delivered through sysfs power/state files don't 868ae12a0dSDavid Brownell * enforce such constraints. 878ae12a0dSDavid Brownell */ 888ae12a0dSDavid Brownell static int spi_suspend(struct device *dev, pm_message_t message) 898ae12a0dSDavid Brownell { 908ae12a0dSDavid Brownell int value; 91b885244eSDavid Brownell struct spi_driver *drv = to_spi_driver(dev->driver); 928ae12a0dSDavid Brownell 93d2799f08SStephen Street if (!drv || !drv->suspend) 948ae12a0dSDavid Brownell return 0; 958ae12a0dSDavid Brownell 968ae12a0dSDavid Brownell /* suspend will stop irqs and dma; no more i/o */ 97b885244eSDavid Brownell value = drv->suspend(to_spi_device(dev), message); 988ae12a0dSDavid Brownell if (value == 0) 998ae12a0dSDavid Brownell dev->power.power_state = message; 1008ae12a0dSDavid Brownell return value; 1018ae12a0dSDavid Brownell } 1028ae12a0dSDavid Brownell 1038ae12a0dSDavid Brownell static int spi_resume(struct device *dev) 1048ae12a0dSDavid Brownell { 1058ae12a0dSDavid Brownell int value; 106b885244eSDavid Brownell struct spi_driver *drv = to_spi_driver(dev->driver); 1078ae12a0dSDavid Brownell 108d2799f08SStephen Street if (!drv || !drv->resume) 1098ae12a0dSDavid Brownell return 0; 1108ae12a0dSDavid Brownell 1118ae12a0dSDavid Brownell /* resume may restart the i/o queue */ 112b885244eSDavid Brownell value = drv->resume(to_spi_device(dev)); 1138ae12a0dSDavid Brownell if (value == 0) 1148ae12a0dSDavid Brownell dev->power.power_state = PMSG_ON; 1158ae12a0dSDavid Brownell return value; 1168ae12a0dSDavid Brownell } 1178ae12a0dSDavid Brownell 1188ae12a0dSDavid Brownell #else 1198ae12a0dSDavid Brownell #define spi_suspend NULL 1208ae12a0dSDavid Brownell #define spi_resume NULL 1218ae12a0dSDavid Brownell #endif 1228ae12a0dSDavid Brownell 1238ae12a0dSDavid Brownell struct bus_type spi_bus_type = { 1248ae12a0dSDavid Brownell .name = "spi", 1258ae12a0dSDavid Brownell .dev_attrs = spi_dev_attrs, 1268ae12a0dSDavid Brownell .match = spi_match_device, 1278ae12a0dSDavid Brownell .uevent = spi_uevent, 1288ae12a0dSDavid Brownell .suspend = spi_suspend, 1298ae12a0dSDavid Brownell .resume = spi_resume, 1308ae12a0dSDavid Brownell }; 1318ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type); 1328ae12a0dSDavid Brownell 133b885244eSDavid Brownell 134b885244eSDavid Brownell static int spi_drv_probe(struct device *dev) 135b885244eSDavid Brownell { 136b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 137b885244eSDavid Brownell 138b885244eSDavid Brownell return sdrv->probe(to_spi_device(dev)); 139b885244eSDavid Brownell } 140b885244eSDavid Brownell 141b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 142b885244eSDavid Brownell { 143b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 144b885244eSDavid Brownell 145b885244eSDavid Brownell return sdrv->remove(to_spi_device(dev)); 146b885244eSDavid Brownell } 147b885244eSDavid Brownell 148b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 149b885244eSDavid Brownell { 150b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 151b885244eSDavid Brownell 152b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 153b885244eSDavid Brownell } 154b885244eSDavid Brownell 155b885244eSDavid Brownell int spi_register_driver(struct spi_driver *sdrv) 156b885244eSDavid Brownell { 157b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 158b885244eSDavid Brownell if (sdrv->probe) 159b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 160b885244eSDavid Brownell if (sdrv->remove) 161b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 162b885244eSDavid Brownell if (sdrv->shutdown) 163b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 164b885244eSDavid Brownell return driver_register(&sdrv->driver); 165b885244eSDavid Brownell } 166b885244eSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_driver); 167b885244eSDavid Brownell 1688ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 1698ae12a0dSDavid Brownell 1708ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 1718ae12a0dSDavid Brownell * would make them board-specific. Similarly with SPI master drivers. 1728ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 1738ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 1748ae12a0dSDavid Brownell */ 1758ae12a0dSDavid Brownell 1768ae12a0dSDavid Brownell struct boardinfo { 1778ae12a0dSDavid Brownell struct list_head list; 1788ae12a0dSDavid Brownell unsigned n_board_info; 1798ae12a0dSDavid Brownell struct spi_board_info board_info[0]; 1808ae12a0dSDavid Brownell }; 1818ae12a0dSDavid Brownell 1828ae12a0dSDavid Brownell static LIST_HEAD(board_list); 1838ae12a0dSDavid Brownell static DECLARE_MUTEX(board_lock); 1848ae12a0dSDavid Brownell 1858ae12a0dSDavid Brownell 1868ae12a0dSDavid Brownell /* On typical mainboards, this is purely internal; and it's not needed 1878ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 1888ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 1898ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 1908ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 1918ae12a0dSDavid Brownell */ 1928ae12a0dSDavid Brownell struct spi_device *__init_or_module 1938ae12a0dSDavid Brownell spi_new_device(struct spi_master *master, struct spi_board_info *chip) 1948ae12a0dSDavid Brownell { 1958ae12a0dSDavid Brownell struct spi_device *proxy; 1968ae12a0dSDavid Brownell struct device *dev = master->cdev.dev; 1978ae12a0dSDavid Brownell int status; 1988ae12a0dSDavid Brownell 1998ae12a0dSDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary */ 2008ae12a0dSDavid Brownell 2010c868461SDavid Brownell if (!spi_master_get(master)) 2028ae12a0dSDavid Brownell return NULL; 2038ae12a0dSDavid Brownell 2048ae12a0dSDavid Brownell proxy = kzalloc(sizeof *proxy, GFP_KERNEL); 2058ae12a0dSDavid Brownell if (!proxy) { 2068ae12a0dSDavid Brownell dev_err(dev, "can't alloc dev for cs%d\n", 2078ae12a0dSDavid Brownell chip->chip_select); 2088ae12a0dSDavid Brownell goto fail; 2098ae12a0dSDavid Brownell } 2108ae12a0dSDavid Brownell proxy->master = master; 2118ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 2128ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 213980a01c9SDavid Brownell proxy->mode = chip->mode; 2148ae12a0dSDavid Brownell proxy->irq = chip->irq; 2158ae12a0dSDavid Brownell proxy->modalias = chip->modalias; 2168ae12a0dSDavid Brownell 2178ae12a0dSDavid Brownell snprintf(proxy->dev.bus_id, sizeof proxy->dev.bus_id, 2188ae12a0dSDavid Brownell "%s.%u", master->cdev.class_id, 2198ae12a0dSDavid Brownell chip->chip_select); 2208ae12a0dSDavid Brownell proxy->dev.parent = dev; 2218ae12a0dSDavid Brownell proxy->dev.bus = &spi_bus_type; 2228ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 2238ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 2248ae12a0dSDavid Brownell proxy->controller_state = NULL; 2258ae12a0dSDavid Brownell proxy->dev.release = spidev_release; 2268ae12a0dSDavid Brownell 2278ae12a0dSDavid Brownell /* drivers may modify this default i/o setup */ 2288ae12a0dSDavid Brownell status = master->setup(proxy); 2298ae12a0dSDavid Brownell if (status < 0) { 2308ae12a0dSDavid Brownell dev_dbg(dev, "can't %s %s, status %d\n", 2318ae12a0dSDavid Brownell "setup", proxy->dev.bus_id, status); 2328ae12a0dSDavid Brownell goto fail; 2338ae12a0dSDavid Brownell } 2348ae12a0dSDavid Brownell 2358ae12a0dSDavid Brownell /* driver core catches callers that misbehave by defining 2368ae12a0dSDavid Brownell * devices that already exist. 2378ae12a0dSDavid Brownell */ 2388ae12a0dSDavid Brownell status = device_register(&proxy->dev); 2398ae12a0dSDavid Brownell if (status < 0) { 2408ae12a0dSDavid Brownell dev_dbg(dev, "can't %s %s, status %d\n", 2418ae12a0dSDavid Brownell "add", proxy->dev.bus_id, status); 242b885244eSDavid Brownell goto fail; 243b885244eSDavid Brownell } 244b885244eSDavid Brownell dev_dbg(dev, "registered child %s\n", proxy->dev.bus_id); 245b885244eSDavid Brownell return proxy; 246b885244eSDavid Brownell 2478ae12a0dSDavid Brownell fail: 2480c868461SDavid Brownell spi_master_put(master); 2498ae12a0dSDavid Brownell kfree(proxy); 2508ae12a0dSDavid Brownell return NULL; 2518ae12a0dSDavid Brownell } 2528ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 2538ae12a0dSDavid Brownell 2548ae12a0dSDavid Brownell /* 2558ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 2568ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 2578ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 2588ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 2598ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 2608ae12a0dSDavid Brownell * 2618ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 2628ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 2638ae12a0dSDavid Brownell * would naturally declare its SPI devices. 2648ae12a0dSDavid Brownell * 2658ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 2668ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 2678ae12a0dSDavid Brownell */ 2688ae12a0dSDavid Brownell int __init 2698ae12a0dSDavid Brownell spi_register_board_info(struct spi_board_info const *info, unsigned n) 2708ae12a0dSDavid Brownell { 2718ae12a0dSDavid Brownell struct boardinfo *bi; 2728ae12a0dSDavid Brownell 273b885244eSDavid Brownell bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL); 2748ae12a0dSDavid Brownell if (!bi) 2758ae12a0dSDavid Brownell return -ENOMEM; 2768ae12a0dSDavid Brownell bi->n_board_info = n; 277b885244eSDavid Brownell memcpy(bi->board_info, info, n * sizeof *info); 2788ae12a0dSDavid Brownell 2798ae12a0dSDavid Brownell down(&board_lock); 2808ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 2818ae12a0dSDavid Brownell up(&board_lock); 2828ae12a0dSDavid Brownell return 0; 2838ae12a0dSDavid Brownell } 2848ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_board_info); 2858ae12a0dSDavid Brownell 2868ae12a0dSDavid Brownell /* FIXME someone should add support for a __setup("spi", ...) that 2878ae12a0dSDavid Brownell * creates board info from kernel command lines 2888ae12a0dSDavid Brownell */ 2898ae12a0dSDavid Brownell 2908ae12a0dSDavid Brownell static void __init_or_module 2918ae12a0dSDavid Brownell scan_boardinfo(struct spi_master *master) 2928ae12a0dSDavid Brownell { 2938ae12a0dSDavid Brownell struct boardinfo *bi; 2948ae12a0dSDavid Brownell struct device *dev = master->cdev.dev; 2958ae12a0dSDavid Brownell 2968ae12a0dSDavid Brownell down(&board_lock); 2978ae12a0dSDavid Brownell list_for_each_entry(bi, &board_list, list) { 2988ae12a0dSDavid Brownell struct spi_board_info *chip = bi->board_info; 2998ae12a0dSDavid Brownell unsigned n; 3008ae12a0dSDavid Brownell 3018ae12a0dSDavid Brownell for (n = bi->n_board_info; n > 0; n--, chip++) { 3028ae12a0dSDavid Brownell if (chip->bus_num != master->bus_num) 3038ae12a0dSDavid Brownell continue; 3048ae12a0dSDavid Brownell /* some controllers only have one chip, so they 3058ae12a0dSDavid Brownell * might not use chipselects. otherwise, the 3068ae12a0dSDavid Brownell * chipselects are numbered 0..max. 3078ae12a0dSDavid Brownell */ 3088ae12a0dSDavid Brownell if (chip->chip_select >= master->num_chipselect 3098ae12a0dSDavid Brownell && master->num_chipselect) { 3108ae12a0dSDavid Brownell dev_dbg(dev, "cs%d > max %d\n", 3118ae12a0dSDavid Brownell chip->chip_select, 3128ae12a0dSDavid Brownell master->num_chipselect); 3138ae12a0dSDavid Brownell continue; 3148ae12a0dSDavid Brownell } 3158ae12a0dSDavid Brownell (void) spi_new_device(master, chip); 3168ae12a0dSDavid Brownell } 3178ae12a0dSDavid Brownell } 3188ae12a0dSDavid Brownell up(&board_lock); 3198ae12a0dSDavid Brownell } 3208ae12a0dSDavid Brownell 3218ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 3228ae12a0dSDavid Brownell 3238ae12a0dSDavid Brownell static void spi_master_release(struct class_device *cdev) 3248ae12a0dSDavid Brownell { 3258ae12a0dSDavid Brownell struct spi_master *master; 3268ae12a0dSDavid Brownell 3278ae12a0dSDavid Brownell master = container_of(cdev, struct spi_master, cdev); 3288ae12a0dSDavid Brownell kfree(master); 3298ae12a0dSDavid Brownell } 3308ae12a0dSDavid Brownell 3318ae12a0dSDavid Brownell static struct class spi_master_class = { 3328ae12a0dSDavid Brownell .name = "spi_master", 3338ae12a0dSDavid Brownell .owner = THIS_MODULE, 3348ae12a0dSDavid Brownell .release = spi_master_release, 3358ae12a0dSDavid Brownell }; 3368ae12a0dSDavid Brownell 3378ae12a0dSDavid Brownell 3388ae12a0dSDavid Brownell /** 3398ae12a0dSDavid Brownell * spi_alloc_master - allocate SPI master controller 3408ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 3410c868461SDavid Brownell * @size: how much driver-private data to preallocate; the pointer to this 3420c868461SDavid Brownell * memory is in the class_data field of the returned class_device, 3430c868461SDavid Brownell * accessible with spi_master_get_devdata(). 3448ae12a0dSDavid Brownell * 3458ae12a0dSDavid Brownell * This call is used only by SPI master controller drivers, which are the 3468ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 347ba1a0513Sdmitry pervushin * an spi_master structure, prior to calling spi_register_master(). 3488ae12a0dSDavid Brownell * 3498ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns the SPI 3508ae12a0dSDavid Brownell * master structure on success, else NULL. 3518ae12a0dSDavid Brownell * 3528ae12a0dSDavid Brownell * The caller is responsible for assigning the bus number and initializing 353ba1a0513Sdmitry pervushin * the master's methods before calling spi_register_master(); and (after errors 3540c868461SDavid Brownell * adding the device) calling spi_master_put() to prevent a memory leak. 3558ae12a0dSDavid Brownell */ 3568ae12a0dSDavid Brownell struct spi_master * __init_or_module 3578ae12a0dSDavid Brownell spi_alloc_master(struct device *dev, unsigned size) 3588ae12a0dSDavid Brownell { 3598ae12a0dSDavid Brownell struct spi_master *master; 3608ae12a0dSDavid Brownell 3610c868461SDavid Brownell if (!dev) 3620c868461SDavid Brownell return NULL; 3630c868461SDavid Brownell 3648ae12a0dSDavid Brownell master = kzalloc(size + sizeof *master, SLAB_KERNEL); 3658ae12a0dSDavid Brownell if (!master) 3668ae12a0dSDavid Brownell return NULL; 3678ae12a0dSDavid Brownell 368b885244eSDavid Brownell class_device_initialize(&master->cdev); 3698ae12a0dSDavid Brownell master->cdev.class = &spi_master_class; 3708ae12a0dSDavid Brownell master->cdev.dev = get_device(dev); 3710c868461SDavid Brownell spi_master_set_devdata(master, &master[1]); 3728ae12a0dSDavid Brownell 3738ae12a0dSDavid Brownell return master; 3748ae12a0dSDavid Brownell } 3758ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_alloc_master); 3768ae12a0dSDavid Brownell 3778ae12a0dSDavid Brownell /** 3788ae12a0dSDavid Brownell * spi_register_master - register SPI master controller 3798ae12a0dSDavid Brownell * @master: initialized master, originally from spi_alloc_master() 3808ae12a0dSDavid Brownell * 3818ae12a0dSDavid Brownell * SPI master controllers connect to their drivers using some non-SPI bus, 3828ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 3838ae12a0dSDavid Brownell * includes calling spi_register_master() to hook up to this SPI bus glue. 3848ae12a0dSDavid Brownell * 3858ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 3868ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 3878ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 3888ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 3898ae12a0dSDavid Brownell * chip is at which address. 3908ae12a0dSDavid Brownell * 3918ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 3928ae12a0dSDavid Brownell * success, else a negative error code (dropping the master's refcount). 3930c868461SDavid Brownell * After a successful return, the caller is responsible for calling 3940c868461SDavid Brownell * spi_unregister_master(). 3958ae12a0dSDavid Brownell */ 3968ae12a0dSDavid Brownell int __init_or_module 3978ae12a0dSDavid Brownell spi_register_master(struct spi_master *master) 3988ae12a0dSDavid Brownell { 399a020ed75SDavid Brownell static atomic_t dyn_bus_id = ATOMIC_INIT((1<<16) - 1); 4008ae12a0dSDavid Brownell struct device *dev = master->cdev.dev; 4018ae12a0dSDavid Brownell int status = -ENODEV; 4028ae12a0dSDavid Brownell int dynamic = 0; 4038ae12a0dSDavid Brownell 4040c868461SDavid Brownell if (!dev) 4050c868461SDavid Brownell return -ENODEV; 4060c868461SDavid Brownell 4078ae12a0dSDavid Brownell /* convention: dynamically assigned bus IDs count down from the max */ 408a020ed75SDavid Brownell if (master->bus_num < 0) { 4098ae12a0dSDavid Brownell master->bus_num = atomic_dec_return(&dyn_bus_id); 410b885244eSDavid Brownell dynamic = 1; 4118ae12a0dSDavid Brownell } 4128ae12a0dSDavid Brownell 4138ae12a0dSDavid Brownell /* register the device, then userspace will see it. 4148ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 4158ae12a0dSDavid Brownell */ 4168ae12a0dSDavid Brownell snprintf(master->cdev.class_id, sizeof master->cdev.class_id, 4178ae12a0dSDavid Brownell "spi%u", master->bus_num); 418b885244eSDavid Brownell status = class_device_add(&master->cdev); 419b885244eSDavid Brownell if (status < 0) 4208ae12a0dSDavid Brownell goto done; 4218ae12a0dSDavid Brownell dev_dbg(dev, "registered master %s%s\n", master->cdev.class_id, 4228ae12a0dSDavid Brownell dynamic ? " (dynamic)" : ""); 4238ae12a0dSDavid Brownell 4248ae12a0dSDavid Brownell /* populate children from any spi device tables */ 4258ae12a0dSDavid Brownell scan_boardinfo(master); 4268ae12a0dSDavid Brownell status = 0; 4278ae12a0dSDavid Brownell done: 4288ae12a0dSDavid Brownell return status; 4298ae12a0dSDavid Brownell } 4308ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_master); 4318ae12a0dSDavid Brownell 4328ae12a0dSDavid Brownell 4338ae12a0dSDavid Brownell static int __unregister(struct device *dev, void *unused) 4348ae12a0dSDavid Brownell { 4358ae12a0dSDavid Brownell /* note: before about 2.6.14-rc1 this would corrupt memory: */ 4360c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 4378ae12a0dSDavid Brownell return 0; 4388ae12a0dSDavid Brownell } 4398ae12a0dSDavid Brownell 4408ae12a0dSDavid Brownell /** 4418ae12a0dSDavid Brownell * spi_unregister_master - unregister SPI master controller 4428ae12a0dSDavid Brownell * @master: the master being unregistered 4438ae12a0dSDavid Brownell * 4448ae12a0dSDavid Brownell * This call is used only by SPI master controller drivers, which are the 4458ae12a0dSDavid Brownell * only ones directly touching chip registers. 4468ae12a0dSDavid Brownell * 4478ae12a0dSDavid Brownell * This must be called from context that can sleep. 4488ae12a0dSDavid Brownell */ 4498ae12a0dSDavid Brownell void spi_unregister_master(struct spi_master *master) 4508ae12a0dSDavid Brownell { 4518ae12a0dSDavid Brownell (void) device_for_each_child(master->cdev.dev, NULL, __unregister); 4520c868461SDavid Brownell class_device_unregister(&master->cdev); 4538ae12a0dSDavid Brownell } 4548ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_unregister_master); 4558ae12a0dSDavid Brownell 4568ae12a0dSDavid Brownell /** 4578ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 4588ae12a0dSDavid Brownell * @bus_num: the master's bus number 4598ae12a0dSDavid Brownell * 4608ae12a0dSDavid Brownell * This call may be used with devices that are registered after 4618ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 4628ae12a0dSDavid Brownell * spi_master (which the caller must release), or NULL if there is 4638ae12a0dSDavid Brownell * no such master registered. 4648ae12a0dSDavid Brownell */ 4658ae12a0dSDavid Brownell struct spi_master *spi_busnum_to_master(u16 bus_num) 4668ae12a0dSDavid Brownell { 4678ae12a0dSDavid Brownell if (bus_num) { 4688ae12a0dSDavid Brownell char name[8]; 4698ae12a0dSDavid Brownell struct kobject *bus; 4708ae12a0dSDavid Brownell 4718ae12a0dSDavid Brownell snprintf(name, sizeof name, "spi%u", bus_num); 4728ae12a0dSDavid Brownell bus = kset_find_obj(&spi_master_class.subsys.kset, name); 4738ae12a0dSDavid Brownell if (bus) 4748ae12a0dSDavid Brownell return container_of(bus, struct spi_master, cdev.kobj); 4758ae12a0dSDavid Brownell } 4768ae12a0dSDavid Brownell return NULL; 4778ae12a0dSDavid Brownell } 4788ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 4798ae12a0dSDavid Brownell 4808ae12a0dSDavid Brownell 4818ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 4828ae12a0dSDavid Brownell 4835d870c8eSAndrew Morton static void spi_complete(void *arg) 4845d870c8eSAndrew Morton { 4855d870c8eSAndrew Morton complete(arg); 4865d870c8eSAndrew Morton } 4875d870c8eSAndrew Morton 4888ae12a0dSDavid Brownell /** 4898ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 4908ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 4918ae12a0dSDavid Brownell * @message: describes the data transfers 4928ae12a0dSDavid Brownell * 4938ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 4948ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 4958ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 4968ae12a0dSDavid Brownell * 4978ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 4988ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 4998ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 5008ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 5018ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 5028ae12a0dSDavid Brownell * 5030c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 5040c868461SDavid Brownell * message will not be freed before this call returns. 5050c868461SDavid Brownell * 5068ae12a0dSDavid Brownell * The return value is a negative error code if the message could not be 5078ae12a0dSDavid Brownell * submitted, else zero. When the value is zero, then message->status is 5088ae12a0dSDavid Brownell * also defined: it's the completion code for the transfer, either zero 5098ae12a0dSDavid Brownell * or a negative error code from the controller driver. 5108ae12a0dSDavid Brownell */ 5118ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 5128ae12a0dSDavid Brownell { 513*60be6b9aSIngo Molnar DECLARE_COMPLETION_ONSTACK(done); 5148ae12a0dSDavid Brownell int status; 5158ae12a0dSDavid Brownell 5165d870c8eSAndrew Morton message->complete = spi_complete; 5178ae12a0dSDavid Brownell message->context = &done; 5188ae12a0dSDavid Brownell status = spi_async(spi, message); 5198ae12a0dSDavid Brownell if (status == 0) 5208ae12a0dSDavid Brownell wait_for_completion(&done); 5218ae12a0dSDavid Brownell message->context = NULL; 5228ae12a0dSDavid Brownell return status; 5238ae12a0dSDavid Brownell } 5248ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 5258ae12a0dSDavid Brownell 526a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 527a9948b61SDavid Brownell #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) 5288ae12a0dSDavid Brownell 5298ae12a0dSDavid Brownell static u8 *buf; 5308ae12a0dSDavid Brownell 5318ae12a0dSDavid Brownell /** 5328ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 5338ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 5348ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 5358ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 5368ae12a0dSDavid Brownell * @rxbuf: buffer into which data will be read 5378ae12a0dSDavid Brownell * @n_rx: size of rxbuf, in bytes (need not be dma-safe) 5388ae12a0dSDavid Brownell * 5398ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 5408ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 5418ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 542b885244eSDavid Brownell * This call may only be used from a context that may sleep. 5438ae12a0dSDavid Brownell * 5440c868461SDavid Brownell * Parameters to this routine are always copied using a small buffer; 5450c868461SDavid Brownell * performance-sensitive or bulk transfer code should instead use 5460c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 5478ae12a0dSDavid Brownell */ 5488ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 5498ae12a0dSDavid Brownell const u8 *txbuf, unsigned n_tx, 5508ae12a0dSDavid Brownell u8 *rxbuf, unsigned n_rx) 5518ae12a0dSDavid Brownell { 5528ae12a0dSDavid Brownell static DECLARE_MUTEX(lock); 5538ae12a0dSDavid Brownell 5548ae12a0dSDavid Brownell int status; 5558ae12a0dSDavid Brownell struct spi_message message; 5568ae12a0dSDavid Brownell struct spi_transfer x[2]; 5578ae12a0dSDavid Brownell u8 *local_buf; 5588ae12a0dSDavid Brownell 5598ae12a0dSDavid Brownell /* Use preallocated DMA-safe buffer. We can't avoid copying here, 5608ae12a0dSDavid Brownell * (as a pure convenience thing), but we can keep heap costs 5618ae12a0dSDavid Brownell * out of the hot path ... 5628ae12a0dSDavid Brownell */ 5638ae12a0dSDavid Brownell if ((n_tx + n_rx) > SPI_BUFSIZ) 5648ae12a0dSDavid Brownell return -EINVAL; 5658ae12a0dSDavid Brownell 5668275c642SVitaly Wool spi_message_init(&message); 5678275c642SVitaly Wool memset(x, 0, sizeof x); 5688275c642SVitaly Wool if (n_tx) { 5698275c642SVitaly Wool x[0].len = n_tx; 5708275c642SVitaly Wool spi_message_add_tail(&x[0], &message); 5718275c642SVitaly Wool } 5728275c642SVitaly Wool if (n_rx) { 5738275c642SVitaly Wool x[1].len = n_rx; 5748275c642SVitaly Wool spi_message_add_tail(&x[1], &message); 5758275c642SVitaly Wool } 5768275c642SVitaly Wool 5778ae12a0dSDavid Brownell /* ... unless someone else is using the pre-allocated buffer */ 5788ae12a0dSDavid Brownell if (down_trylock(&lock)) { 5798ae12a0dSDavid Brownell local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 5808ae12a0dSDavid Brownell if (!local_buf) 5818ae12a0dSDavid Brownell return -ENOMEM; 5828ae12a0dSDavid Brownell } else 5838ae12a0dSDavid Brownell local_buf = buf; 5848ae12a0dSDavid Brownell 5858ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 5868ae12a0dSDavid Brownell x[0].tx_buf = local_buf; 5878ae12a0dSDavid Brownell x[1].rx_buf = local_buf + n_tx; 5888ae12a0dSDavid Brownell 5898ae12a0dSDavid Brownell /* do the i/o */ 5908ae12a0dSDavid Brownell status = spi_sync(spi, &message); 5918ae12a0dSDavid Brownell if (status == 0) { 5928ae12a0dSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 5938ae12a0dSDavid Brownell status = message.status; 5948ae12a0dSDavid Brownell } 5958ae12a0dSDavid Brownell 5968ae12a0dSDavid Brownell if (x[0].tx_buf == buf) 5978ae12a0dSDavid Brownell up(&lock); 5988ae12a0dSDavid Brownell else 5998ae12a0dSDavid Brownell kfree(local_buf); 6008ae12a0dSDavid Brownell 6018ae12a0dSDavid Brownell return status; 6028ae12a0dSDavid Brownell } 6038ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 6048ae12a0dSDavid Brownell 6058ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 6068ae12a0dSDavid Brownell 6078ae12a0dSDavid Brownell static int __init spi_init(void) 6088ae12a0dSDavid Brownell { 609b885244eSDavid Brownell int status; 6108ae12a0dSDavid Brownell 611b885244eSDavid Brownell buf = kmalloc(SPI_BUFSIZ, SLAB_KERNEL); 612b885244eSDavid Brownell if (!buf) { 613b885244eSDavid Brownell status = -ENOMEM; 614b885244eSDavid Brownell goto err0; 6158ae12a0dSDavid Brownell } 616b885244eSDavid Brownell 617b885244eSDavid Brownell status = bus_register(&spi_bus_type); 618b885244eSDavid Brownell if (status < 0) 619b885244eSDavid Brownell goto err1; 620b885244eSDavid Brownell 621b885244eSDavid Brownell status = class_register(&spi_master_class); 622b885244eSDavid Brownell if (status < 0) 623b885244eSDavid Brownell goto err2; 624b885244eSDavid Brownell return 0; 625b885244eSDavid Brownell 626b885244eSDavid Brownell err2: 627b885244eSDavid Brownell bus_unregister(&spi_bus_type); 628b885244eSDavid Brownell err1: 629b885244eSDavid Brownell kfree(buf); 630b885244eSDavid Brownell buf = NULL; 631b885244eSDavid Brownell err0: 632b885244eSDavid Brownell return status; 633b885244eSDavid Brownell } 634b885244eSDavid Brownell 6358ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 6368ae12a0dSDavid Brownell * but even essential drivers wait till later 637b885244eSDavid Brownell * 638b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 639b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 640b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 6418ae12a0dSDavid Brownell */ 6428ae12a0dSDavid Brownell subsys_initcall(spi_init); 6438ae12a0dSDavid Brownell 644