xref: /linux/drivers/spi/spi.c (revision 8275c642ccdce09a2146d0a9eb022e3698ee927e)
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 
930c868461SDavid Brownell 	if (!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 
1080c868461SDavid Brownell 	if (!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;
2138ae12a0dSDavid Brownell 	proxy->irq = chip->irq;
2148ae12a0dSDavid Brownell 	proxy->modalias = chip->modalias;
2158ae12a0dSDavid Brownell 
2168ae12a0dSDavid Brownell 	snprintf(proxy->dev.bus_id, sizeof proxy->dev.bus_id,
2178ae12a0dSDavid Brownell 			"%s.%u", master->cdev.class_id,
2188ae12a0dSDavid Brownell 			chip->chip_select);
2198ae12a0dSDavid Brownell 	proxy->dev.parent = dev;
2208ae12a0dSDavid Brownell 	proxy->dev.bus = &spi_bus_type;
2218ae12a0dSDavid Brownell 	proxy->dev.platform_data = (void *) chip->platform_data;
2228ae12a0dSDavid Brownell 	proxy->controller_data = chip->controller_data;
2238ae12a0dSDavid Brownell 	proxy->controller_state = NULL;
2248ae12a0dSDavid Brownell 	proxy->dev.release = spidev_release;
2258ae12a0dSDavid Brownell 
2268ae12a0dSDavid Brownell 	/* drivers may modify this default i/o setup */
2278ae12a0dSDavid Brownell 	status = master->setup(proxy);
2288ae12a0dSDavid Brownell 	if (status < 0) {
2298ae12a0dSDavid Brownell 		dev_dbg(dev, "can't %s %s, status %d\n",
2308ae12a0dSDavid Brownell 				"setup", proxy->dev.bus_id, status);
2318ae12a0dSDavid Brownell 		goto fail;
2328ae12a0dSDavid Brownell 	}
2338ae12a0dSDavid Brownell 
2348ae12a0dSDavid Brownell 	/* driver core catches callers that misbehave by defining
2358ae12a0dSDavid Brownell 	 * devices that already exist.
2368ae12a0dSDavid Brownell 	 */
2378ae12a0dSDavid Brownell 	status = device_register(&proxy->dev);
2388ae12a0dSDavid Brownell 	if (status < 0) {
2398ae12a0dSDavid Brownell 		dev_dbg(dev, "can't %s %s, status %d\n",
2408ae12a0dSDavid Brownell 				"add", proxy->dev.bus_id, status);
241b885244eSDavid Brownell 		goto fail;
242b885244eSDavid Brownell 	}
243b885244eSDavid Brownell 	dev_dbg(dev, "registered child %s\n", proxy->dev.bus_id);
244b885244eSDavid Brownell 	return proxy;
245b885244eSDavid Brownell 
2468ae12a0dSDavid Brownell fail:
2470c868461SDavid Brownell 	spi_master_put(master);
2488ae12a0dSDavid Brownell 	kfree(proxy);
2498ae12a0dSDavid Brownell 	return NULL;
2508ae12a0dSDavid Brownell }
2518ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device);
2528ae12a0dSDavid Brownell 
2538ae12a0dSDavid Brownell /*
2548ae12a0dSDavid Brownell  * Board-specific early init code calls this (probably during arch_initcall)
2558ae12a0dSDavid Brownell  * with segments of the SPI device table.  Any device nodes are created later,
2568ae12a0dSDavid Brownell  * after the relevant parent SPI controller (bus_num) is defined.  We keep
2578ae12a0dSDavid Brownell  * this table of devices forever, so that reloading a controller driver will
2588ae12a0dSDavid Brownell  * not make Linux forget about these hard-wired devices.
2598ae12a0dSDavid Brownell  *
2608ae12a0dSDavid Brownell  * Other code can also call this, e.g. a particular add-on board might provide
2618ae12a0dSDavid Brownell  * SPI devices through its expansion connector, so code initializing that board
2628ae12a0dSDavid Brownell  * would naturally declare its SPI devices.
2638ae12a0dSDavid Brownell  *
2648ae12a0dSDavid Brownell  * The board info passed can safely be __initdata ... but be careful of
2658ae12a0dSDavid Brownell  * any embedded pointers (platform_data, etc), they're copied as-is.
2668ae12a0dSDavid Brownell  */
2678ae12a0dSDavid Brownell int __init
2688ae12a0dSDavid Brownell spi_register_board_info(struct spi_board_info const *info, unsigned n)
2698ae12a0dSDavid Brownell {
2708ae12a0dSDavid Brownell 	struct boardinfo	*bi;
2718ae12a0dSDavid Brownell 
272b885244eSDavid Brownell 	bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);
2738ae12a0dSDavid Brownell 	if (!bi)
2748ae12a0dSDavid Brownell 		return -ENOMEM;
2758ae12a0dSDavid Brownell 	bi->n_board_info = n;
276b885244eSDavid Brownell 	memcpy(bi->board_info, info, n * sizeof *info);
2778ae12a0dSDavid Brownell 
2788ae12a0dSDavid Brownell 	down(&board_lock);
2798ae12a0dSDavid Brownell 	list_add_tail(&bi->list, &board_list);
2808ae12a0dSDavid Brownell 	up(&board_lock);
2818ae12a0dSDavid Brownell 	return 0;
2828ae12a0dSDavid Brownell }
2838ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_board_info);
2848ae12a0dSDavid Brownell 
2858ae12a0dSDavid Brownell /* FIXME someone should add support for a __setup("spi", ...) that
2868ae12a0dSDavid Brownell  * creates board info from kernel command lines
2878ae12a0dSDavid Brownell  */
2888ae12a0dSDavid Brownell 
2898ae12a0dSDavid Brownell static void __init_or_module
2908ae12a0dSDavid Brownell scan_boardinfo(struct spi_master *master)
2918ae12a0dSDavid Brownell {
2928ae12a0dSDavid Brownell 	struct boardinfo	*bi;
2938ae12a0dSDavid Brownell 	struct device		*dev = master->cdev.dev;
2948ae12a0dSDavid Brownell 
2958ae12a0dSDavid Brownell 	down(&board_lock);
2968ae12a0dSDavid Brownell 	list_for_each_entry(bi, &board_list, list) {
2978ae12a0dSDavid Brownell 		struct spi_board_info	*chip = bi->board_info;
2988ae12a0dSDavid Brownell 		unsigned		n;
2998ae12a0dSDavid Brownell 
3008ae12a0dSDavid Brownell 		for (n = bi->n_board_info; n > 0; n--, chip++) {
3018ae12a0dSDavid Brownell 			if (chip->bus_num != master->bus_num)
3028ae12a0dSDavid Brownell 				continue;
3038ae12a0dSDavid Brownell 			/* some controllers only have one chip, so they
3048ae12a0dSDavid Brownell 			 * might not use chipselects.  otherwise, the
3058ae12a0dSDavid Brownell 			 * chipselects are numbered 0..max.
3068ae12a0dSDavid Brownell 			 */
3078ae12a0dSDavid Brownell 			if (chip->chip_select >= master->num_chipselect
3088ae12a0dSDavid Brownell 					&& master->num_chipselect) {
3098ae12a0dSDavid Brownell 				dev_dbg(dev, "cs%d > max %d\n",
3108ae12a0dSDavid Brownell 					chip->chip_select,
3118ae12a0dSDavid Brownell 					master->num_chipselect);
3128ae12a0dSDavid Brownell 				continue;
3138ae12a0dSDavid Brownell 			}
3148ae12a0dSDavid Brownell 			(void) spi_new_device(master, chip);
3158ae12a0dSDavid Brownell 		}
3168ae12a0dSDavid Brownell 	}
3178ae12a0dSDavid Brownell 	up(&board_lock);
3188ae12a0dSDavid Brownell }
3198ae12a0dSDavid Brownell 
3208ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
3218ae12a0dSDavid Brownell 
3228ae12a0dSDavid Brownell static void spi_master_release(struct class_device *cdev)
3238ae12a0dSDavid Brownell {
3248ae12a0dSDavid Brownell 	struct spi_master *master;
3258ae12a0dSDavid Brownell 
3268ae12a0dSDavid Brownell 	master = container_of(cdev, struct spi_master, cdev);
3278ae12a0dSDavid Brownell 	kfree(master);
3288ae12a0dSDavid Brownell }
3298ae12a0dSDavid Brownell 
3308ae12a0dSDavid Brownell static struct class spi_master_class = {
3318ae12a0dSDavid Brownell 	.name		= "spi_master",
3328ae12a0dSDavid Brownell 	.owner		= THIS_MODULE,
3338ae12a0dSDavid Brownell 	.release	= spi_master_release,
3348ae12a0dSDavid Brownell };
3358ae12a0dSDavid Brownell 
3368ae12a0dSDavid Brownell 
3378ae12a0dSDavid Brownell /**
3388ae12a0dSDavid Brownell  * spi_alloc_master - allocate SPI master controller
3398ae12a0dSDavid Brownell  * @dev: the controller, possibly using the platform_bus
3400c868461SDavid Brownell  * @size: how much driver-private data to preallocate; the pointer to this
3410c868461SDavid Brownell  * 	memory is in the class_data field of the returned class_device,
3420c868461SDavid Brownell  *	accessible with spi_master_get_devdata().
3438ae12a0dSDavid Brownell  *
3448ae12a0dSDavid Brownell  * This call is used only by SPI master controller drivers, which are the
3458ae12a0dSDavid Brownell  * only ones directly touching chip registers.  It's how they allocate
3468ae12a0dSDavid Brownell  * an spi_master structure, prior to calling spi_add_master().
3478ae12a0dSDavid Brownell  *
3488ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns the SPI
3498ae12a0dSDavid Brownell  * master structure on success, else NULL.
3508ae12a0dSDavid Brownell  *
3518ae12a0dSDavid Brownell  * The caller is responsible for assigning the bus number and initializing
3520c868461SDavid Brownell  * the master's methods before calling spi_add_master(); and (after errors
3530c868461SDavid Brownell  * adding the device) calling spi_master_put() to prevent a memory leak.
3548ae12a0dSDavid Brownell  */
3558ae12a0dSDavid Brownell struct spi_master * __init_or_module
3568ae12a0dSDavid Brownell spi_alloc_master(struct device *dev, unsigned size)
3578ae12a0dSDavid Brownell {
3588ae12a0dSDavid Brownell 	struct spi_master	*master;
3598ae12a0dSDavid Brownell 
3600c868461SDavid Brownell 	if (!dev)
3610c868461SDavid Brownell 		return NULL;
3620c868461SDavid Brownell 
3638ae12a0dSDavid Brownell 	master = kzalloc(size + sizeof *master, SLAB_KERNEL);
3648ae12a0dSDavid Brownell 	if (!master)
3658ae12a0dSDavid Brownell 		return NULL;
3668ae12a0dSDavid Brownell 
367b885244eSDavid Brownell 	class_device_initialize(&master->cdev);
3688ae12a0dSDavid Brownell 	master->cdev.class = &spi_master_class;
3698ae12a0dSDavid Brownell 	master->cdev.dev = get_device(dev);
3700c868461SDavid Brownell 	spi_master_set_devdata(master, &master[1]);
3718ae12a0dSDavid Brownell 
3728ae12a0dSDavid Brownell 	return master;
3738ae12a0dSDavid Brownell }
3748ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_alloc_master);
3758ae12a0dSDavid Brownell 
3768ae12a0dSDavid Brownell /**
3778ae12a0dSDavid Brownell  * spi_register_master - register SPI master controller
3788ae12a0dSDavid Brownell  * @master: initialized master, originally from spi_alloc_master()
3798ae12a0dSDavid Brownell  *
3808ae12a0dSDavid Brownell  * SPI master controllers connect to their drivers using some non-SPI bus,
3818ae12a0dSDavid Brownell  * such as the platform bus.  The final stage of probe() in that code
3828ae12a0dSDavid Brownell  * includes calling spi_register_master() to hook up to this SPI bus glue.
3838ae12a0dSDavid Brownell  *
3848ae12a0dSDavid Brownell  * SPI controllers use board specific (often SOC specific) bus numbers,
3858ae12a0dSDavid Brownell  * and board-specific addressing for SPI devices combines those numbers
3868ae12a0dSDavid Brownell  * with chip select numbers.  Since SPI does not directly support dynamic
3878ae12a0dSDavid Brownell  * device identification, boards need configuration tables telling which
3888ae12a0dSDavid Brownell  * chip is at which address.
3898ae12a0dSDavid Brownell  *
3908ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns zero on
3918ae12a0dSDavid Brownell  * success, else a negative error code (dropping the master's refcount).
3920c868461SDavid Brownell  * After a successful return, the caller is responsible for calling
3930c868461SDavid Brownell  * spi_unregister_master().
3948ae12a0dSDavid Brownell  */
3958ae12a0dSDavid Brownell int __init_or_module
3968ae12a0dSDavid Brownell spi_register_master(struct spi_master *master)
3978ae12a0dSDavid Brownell {
3988ae12a0dSDavid Brownell 	static atomic_t		dyn_bus_id = ATOMIC_INIT(0);
3998ae12a0dSDavid Brownell 	struct device		*dev = master->cdev.dev;
4008ae12a0dSDavid Brownell 	int			status = -ENODEV;
4018ae12a0dSDavid Brownell 	int			dynamic = 0;
4028ae12a0dSDavid Brownell 
4030c868461SDavid Brownell 	if (!dev)
4040c868461SDavid Brownell 		return -ENODEV;
4050c868461SDavid Brownell 
4068ae12a0dSDavid Brownell 	/* convention:  dynamically assigned bus IDs count down from the max */
4078ae12a0dSDavid Brownell 	if (master->bus_num == 0) {
4088ae12a0dSDavid Brownell 		master->bus_num = atomic_dec_return(&dyn_bus_id);
409b885244eSDavid Brownell 		dynamic = 1;
4108ae12a0dSDavid Brownell 	}
4118ae12a0dSDavid Brownell 
4128ae12a0dSDavid Brownell 	/* register the device, then userspace will see it.
4138ae12a0dSDavid Brownell 	 * registration fails if the bus ID is in use.
4148ae12a0dSDavid Brownell 	 */
4158ae12a0dSDavid Brownell 	snprintf(master->cdev.class_id, sizeof master->cdev.class_id,
4168ae12a0dSDavid Brownell 		"spi%u", master->bus_num);
417b885244eSDavid Brownell 	status = class_device_add(&master->cdev);
418b885244eSDavid Brownell 	if (status < 0)
4198ae12a0dSDavid Brownell 		goto done;
4208ae12a0dSDavid Brownell 	dev_dbg(dev, "registered master %s%s\n", master->cdev.class_id,
4218ae12a0dSDavid Brownell 			dynamic ? " (dynamic)" : "");
4228ae12a0dSDavid Brownell 
4238ae12a0dSDavid Brownell 	/* populate children from any spi device tables */
4248ae12a0dSDavid Brownell 	scan_boardinfo(master);
4258ae12a0dSDavid Brownell 	status = 0;
4268ae12a0dSDavid Brownell done:
4278ae12a0dSDavid Brownell 	return status;
4288ae12a0dSDavid Brownell }
4298ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_master);
4308ae12a0dSDavid Brownell 
4318ae12a0dSDavid Brownell 
4328ae12a0dSDavid Brownell static int __unregister(struct device *dev, void *unused)
4338ae12a0dSDavid Brownell {
4348ae12a0dSDavid Brownell 	/* note: before about 2.6.14-rc1 this would corrupt memory: */
4350c868461SDavid Brownell 	spi_unregister_device(to_spi_device(dev));
4368ae12a0dSDavid Brownell 	return 0;
4378ae12a0dSDavid Brownell }
4388ae12a0dSDavid Brownell 
4398ae12a0dSDavid Brownell /**
4408ae12a0dSDavid Brownell  * spi_unregister_master - unregister SPI master controller
4418ae12a0dSDavid Brownell  * @master: the master being unregistered
4428ae12a0dSDavid Brownell  *
4438ae12a0dSDavid Brownell  * This call is used only by SPI master controller drivers, which are the
4448ae12a0dSDavid Brownell  * only ones directly touching chip registers.
4458ae12a0dSDavid Brownell  *
4468ae12a0dSDavid Brownell  * This must be called from context that can sleep.
4478ae12a0dSDavid Brownell  */
4488ae12a0dSDavid Brownell void spi_unregister_master(struct spi_master *master)
4498ae12a0dSDavid Brownell {
4508ae12a0dSDavid Brownell 	(void) device_for_each_child(master->cdev.dev, NULL, __unregister);
4510c868461SDavid Brownell 	class_device_unregister(&master->cdev);
4520c868461SDavid Brownell 	master->cdev.dev = NULL;
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 
4838ae12a0dSDavid Brownell /**
4848ae12a0dSDavid Brownell  * spi_sync - blocking/synchronous SPI data transfers
4858ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
4868ae12a0dSDavid Brownell  * @message: describes the data transfers
4878ae12a0dSDavid Brownell  *
4888ae12a0dSDavid Brownell  * This call may only be used from a context that may sleep.  The sleep
4898ae12a0dSDavid Brownell  * is non-interruptible, and has no timeout.  Low-overhead controller
4908ae12a0dSDavid Brownell  * drivers may DMA directly into and out of the message buffers.
4918ae12a0dSDavid Brownell  *
4928ae12a0dSDavid Brownell  * Note that the SPI device's chip select is active during the message,
4938ae12a0dSDavid Brownell  * and then is normally disabled between messages.  Drivers for some
4948ae12a0dSDavid Brownell  * frequently-used devices may want to minimize costs of selecting a chip,
4958ae12a0dSDavid Brownell  * by leaving it selected in anticipation that the next message will go
4968ae12a0dSDavid Brownell  * to the same chip.  (That may increase power usage.)
4978ae12a0dSDavid Brownell  *
4980c868461SDavid Brownell  * Also, the caller is guaranteeing that the memory associated with the
4990c868461SDavid Brownell  * message will not be freed before this call returns.
5000c868461SDavid Brownell  *
5018ae12a0dSDavid Brownell  * The return value is a negative error code if the message could not be
5028ae12a0dSDavid Brownell  * submitted, else zero.  When the value is zero, then message->status is
5038ae12a0dSDavid Brownell  * also defined:  it's the completion code for the transfer, either zero
5048ae12a0dSDavid Brownell  * or a negative error code from the controller driver.
5058ae12a0dSDavid Brownell  */
5068ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message)
5078ae12a0dSDavid Brownell {
5088ae12a0dSDavid Brownell 	DECLARE_COMPLETION(done);
5098ae12a0dSDavid Brownell 	int status;
5108ae12a0dSDavid Brownell 
5118ae12a0dSDavid Brownell 	message->complete = (void (*)(void *)) complete;
5128ae12a0dSDavid Brownell 	message->context = &done;
5138ae12a0dSDavid Brownell 	status = spi_async(spi, message);
5148ae12a0dSDavid Brownell 	if (status == 0)
5158ae12a0dSDavid Brownell 		wait_for_completion(&done);
5168ae12a0dSDavid Brownell 	message->context = NULL;
5178ae12a0dSDavid Brownell 	return status;
5188ae12a0dSDavid Brownell }
5198ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync);
5208ae12a0dSDavid Brownell 
5218ae12a0dSDavid Brownell #define	SPI_BUFSIZ	(SMP_CACHE_BYTES)
5228ae12a0dSDavid Brownell 
5238ae12a0dSDavid Brownell static u8	*buf;
5248ae12a0dSDavid Brownell 
5258ae12a0dSDavid Brownell /**
5268ae12a0dSDavid Brownell  * spi_write_then_read - SPI synchronous write followed by read
5278ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
5288ae12a0dSDavid Brownell  * @txbuf: data to be written (need not be dma-safe)
5298ae12a0dSDavid Brownell  * @n_tx: size of txbuf, in bytes
5308ae12a0dSDavid Brownell  * @rxbuf: buffer into which data will be read
5318ae12a0dSDavid Brownell  * @n_rx: size of rxbuf, in bytes (need not be dma-safe)
5328ae12a0dSDavid Brownell  *
5338ae12a0dSDavid Brownell  * This performs a half duplex MicroWire style transaction with the
5348ae12a0dSDavid Brownell  * device, sending txbuf and then reading rxbuf.  The return value
5358ae12a0dSDavid Brownell  * is zero for success, else a negative errno status code.
536b885244eSDavid Brownell  * This call may only be used from a context that may sleep.
5378ae12a0dSDavid Brownell  *
5380c868461SDavid Brownell  * Parameters to this routine are always copied using a small buffer;
5390c868461SDavid Brownell  * performance-sensitive or bulk transfer code should instead use
5400c868461SDavid Brownell  * spi_{async,sync}() calls with dma-safe buffers.
5418ae12a0dSDavid Brownell  */
5428ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi,
5438ae12a0dSDavid Brownell 		const u8 *txbuf, unsigned n_tx,
5448ae12a0dSDavid Brownell 		u8 *rxbuf, unsigned n_rx)
5458ae12a0dSDavid Brownell {
5468ae12a0dSDavid Brownell 	static DECLARE_MUTEX(lock);
5478ae12a0dSDavid Brownell 
5488ae12a0dSDavid Brownell 	int			status;
5498ae12a0dSDavid Brownell 	struct spi_message	message;
5508ae12a0dSDavid Brownell 	struct spi_transfer	x[2];
5518ae12a0dSDavid Brownell 	u8			*local_buf;
5528ae12a0dSDavid Brownell 
5538ae12a0dSDavid Brownell 	/* Use preallocated DMA-safe buffer.  We can't avoid copying here,
5548ae12a0dSDavid Brownell 	 * (as a pure convenience thing), but we can keep heap costs
5558ae12a0dSDavid Brownell 	 * out of the hot path ...
5568ae12a0dSDavid Brownell 	 */
5578ae12a0dSDavid Brownell 	if ((n_tx + n_rx) > SPI_BUFSIZ)
5588ae12a0dSDavid Brownell 		return -EINVAL;
5598ae12a0dSDavid Brownell 
560*8275c642SVitaly Wool 	spi_message_init(&message);
561*8275c642SVitaly Wool 	memset(x, 0, sizeof x);
562*8275c642SVitaly Wool 	if (n_tx) {
563*8275c642SVitaly Wool 		x[0].len = n_tx;
564*8275c642SVitaly Wool 		spi_message_add_tail(&x[0], &message);
565*8275c642SVitaly Wool 	}
566*8275c642SVitaly Wool 	if (n_rx) {
567*8275c642SVitaly Wool 		x[1].len = n_rx;
568*8275c642SVitaly Wool 		spi_message_add_tail(&x[1], &message);
569*8275c642SVitaly Wool 	}
570*8275c642SVitaly Wool 
5718ae12a0dSDavid Brownell 	/* ... unless someone else is using the pre-allocated buffer */
5728ae12a0dSDavid Brownell 	if (down_trylock(&lock)) {
5738ae12a0dSDavid Brownell 		local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
5748ae12a0dSDavid Brownell 		if (!local_buf)
5758ae12a0dSDavid Brownell 			return -ENOMEM;
5768ae12a0dSDavid Brownell 	} else
5778ae12a0dSDavid Brownell 		local_buf = buf;
5788ae12a0dSDavid Brownell 
5798ae12a0dSDavid Brownell 	memcpy(local_buf, txbuf, n_tx);
5808ae12a0dSDavid Brownell 	x[0].tx_buf = local_buf;
5818ae12a0dSDavid Brownell 	x[1].rx_buf = local_buf + n_tx;
5828ae12a0dSDavid Brownell 
5838ae12a0dSDavid Brownell 	/* do the i/o */
5848ae12a0dSDavid Brownell 	status = spi_sync(spi, &message);
5858ae12a0dSDavid Brownell 	if (status == 0) {
5868ae12a0dSDavid Brownell 		memcpy(rxbuf, x[1].rx_buf, n_rx);
5878ae12a0dSDavid Brownell 		status = message.status;
5888ae12a0dSDavid Brownell 	}
5898ae12a0dSDavid Brownell 
5908ae12a0dSDavid Brownell 	if (x[0].tx_buf == buf)
5918ae12a0dSDavid Brownell 		up(&lock);
5928ae12a0dSDavid Brownell 	else
5938ae12a0dSDavid Brownell 		kfree(local_buf);
5948ae12a0dSDavid Brownell 
5958ae12a0dSDavid Brownell 	return status;
5968ae12a0dSDavid Brownell }
5978ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read);
5988ae12a0dSDavid Brownell 
5998ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
6008ae12a0dSDavid Brownell 
6018ae12a0dSDavid Brownell static int __init spi_init(void)
6028ae12a0dSDavid Brownell {
603b885244eSDavid Brownell 	int	status;
6048ae12a0dSDavid Brownell 
605b885244eSDavid Brownell 	buf = kmalloc(SPI_BUFSIZ, SLAB_KERNEL);
606b885244eSDavid Brownell 	if (!buf) {
607b885244eSDavid Brownell 		status = -ENOMEM;
608b885244eSDavid Brownell 		goto err0;
6098ae12a0dSDavid Brownell 	}
610b885244eSDavid Brownell 
611b885244eSDavid Brownell 	status = bus_register(&spi_bus_type);
612b885244eSDavid Brownell 	if (status < 0)
613b885244eSDavid Brownell 		goto err1;
614b885244eSDavid Brownell 
615b885244eSDavid Brownell 	status = class_register(&spi_master_class);
616b885244eSDavid Brownell 	if (status < 0)
617b885244eSDavid Brownell 		goto err2;
618b885244eSDavid Brownell 	return 0;
619b885244eSDavid Brownell 
620b885244eSDavid Brownell err2:
621b885244eSDavid Brownell 	bus_unregister(&spi_bus_type);
622b885244eSDavid Brownell err1:
623b885244eSDavid Brownell 	kfree(buf);
624b885244eSDavid Brownell 	buf = NULL;
625b885244eSDavid Brownell err0:
626b885244eSDavid Brownell 	return status;
627b885244eSDavid Brownell }
628b885244eSDavid Brownell 
6298ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(),
6308ae12a0dSDavid Brownell  * but even essential drivers wait till later
631b885244eSDavid Brownell  *
632b885244eSDavid Brownell  * REVISIT only boardinfo really needs static linking. the rest (device and
633b885244eSDavid Brownell  * driver registration) _could_ be dynamically linked (modular) ... costs
634b885244eSDavid Brownell  * include needing to have boardinfo data structures be much more public.
6358ae12a0dSDavid Brownell  */
6368ae12a0dSDavid Brownell subsys_initcall(spi_init);
6378ae12a0dSDavid Brownell 
638