xref: /linux/drivers/spi/spi.c (revision e48880e02e7e7ead9daa47fe3a20486f550668d3)
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 
181dc87c98eSGrant Likely /**
182dc87c98eSGrant Likely  * spi_alloc_device - Allocate a new SPI device
183dc87c98eSGrant Likely  * @master: Controller to which device is connected
184dc87c98eSGrant Likely  * Context: can sleep
185dc87c98eSGrant Likely  *
186dc87c98eSGrant Likely  * Allows a driver to allocate and initialize a spi_device without
187dc87c98eSGrant Likely  * registering it immediately.  This allows a driver to directly
188dc87c98eSGrant Likely  * fill the spi_device with device parameters before calling
189dc87c98eSGrant Likely  * spi_add_device() on it.
190dc87c98eSGrant Likely  *
191dc87c98eSGrant Likely  * Caller is responsible to call spi_add_device() on the returned
192dc87c98eSGrant Likely  * spi_device structure to add it to the SPI master.  If the caller
193dc87c98eSGrant Likely  * needs to discard the spi_device without adding it, then it should
194dc87c98eSGrant Likely  * call spi_dev_put() on it.
195dc87c98eSGrant Likely  *
196dc87c98eSGrant Likely  * Returns a pointer to the new device, or NULL.
197dc87c98eSGrant Likely  */
198dc87c98eSGrant Likely struct spi_device *spi_alloc_device(struct spi_master *master)
199dc87c98eSGrant Likely {
200dc87c98eSGrant Likely 	struct spi_device	*spi;
201dc87c98eSGrant Likely 	struct device		*dev = master->dev.parent;
202dc87c98eSGrant Likely 
203dc87c98eSGrant Likely 	if (!spi_master_get(master))
204dc87c98eSGrant Likely 		return NULL;
205dc87c98eSGrant Likely 
206dc87c98eSGrant Likely 	spi = kzalloc(sizeof *spi, GFP_KERNEL);
207dc87c98eSGrant Likely 	if (!spi) {
208dc87c98eSGrant Likely 		dev_err(dev, "cannot alloc spi_device\n");
209dc87c98eSGrant Likely 		spi_master_put(master);
210dc87c98eSGrant Likely 		return NULL;
211dc87c98eSGrant Likely 	}
212dc87c98eSGrant Likely 
213dc87c98eSGrant Likely 	spi->master = master;
214dc87c98eSGrant Likely 	spi->dev.parent = dev;
215dc87c98eSGrant Likely 	spi->dev.bus = &spi_bus_type;
216dc87c98eSGrant Likely 	spi->dev.release = spidev_release;
217dc87c98eSGrant Likely 	device_initialize(&spi->dev);
218dc87c98eSGrant Likely 	return spi;
219dc87c98eSGrant Likely }
220dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device);
221dc87c98eSGrant Likely 
222dc87c98eSGrant Likely /**
223dc87c98eSGrant Likely  * spi_add_device - Add spi_device allocated with spi_alloc_device
224dc87c98eSGrant Likely  * @spi: spi_device to register
225dc87c98eSGrant Likely  *
226dc87c98eSGrant Likely  * Companion function to spi_alloc_device.  Devices allocated with
227dc87c98eSGrant Likely  * spi_alloc_device can be added onto the spi bus with this function.
228dc87c98eSGrant Likely  *
229*e48880e0SDavid Brownell  * Returns 0 on success; negative errno on failure
230dc87c98eSGrant Likely  */
231dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi)
232dc87c98eSGrant Likely {
233*e48880e0SDavid Brownell 	static DEFINE_MUTEX(spi_add_lock);
234dc87c98eSGrant Likely 	struct device *dev = spi->master->dev.parent;
235dc87c98eSGrant Likely 	int status;
236dc87c98eSGrant Likely 
237dc87c98eSGrant Likely 	/* Chipselects are numbered 0..max; validate. */
238dc87c98eSGrant Likely 	if (spi->chip_select >= spi->master->num_chipselect) {
239dc87c98eSGrant Likely 		dev_err(dev, "cs%d >= max %d\n",
240dc87c98eSGrant Likely 			spi->chip_select,
241dc87c98eSGrant Likely 			spi->master->num_chipselect);
242dc87c98eSGrant Likely 		return -EINVAL;
243dc87c98eSGrant Likely 	}
244dc87c98eSGrant Likely 
245dc87c98eSGrant Likely 	/* Set the bus ID string */
246dc87c98eSGrant Likely 	snprintf(spi->dev.bus_id, sizeof spi->dev.bus_id,
247dc87c98eSGrant Likely 			"%s.%u", spi->master->dev.bus_id,
248dc87c98eSGrant Likely 			spi->chip_select);
249dc87c98eSGrant Likely 
250*e48880e0SDavid Brownell 
251*e48880e0SDavid Brownell 	/* We need to make sure there's no other device with this
252*e48880e0SDavid Brownell 	 * chipselect **BEFORE** we call setup(), else we'll trash
253*e48880e0SDavid Brownell 	 * its configuration.  Lock against concurrent add() calls.
254*e48880e0SDavid Brownell 	 */
255*e48880e0SDavid Brownell 	mutex_lock(&spi_add_lock);
256*e48880e0SDavid Brownell 
257*e48880e0SDavid Brownell 	if (bus_find_device_by_name(&spi_bus_type, NULL, spi->dev.bus_id)
258*e48880e0SDavid Brownell 			!= NULL) {
259*e48880e0SDavid Brownell 		dev_err(dev, "chipselect %d already in use\n",
260*e48880e0SDavid Brownell 				spi->chip_select);
261*e48880e0SDavid Brownell 		status = -EBUSY;
262*e48880e0SDavid Brownell 		goto done;
263*e48880e0SDavid Brownell 	}
264*e48880e0SDavid Brownell 
265*e48880e0SDavid Brownell 	/* Drivers may modify this initial i/o setup, but will
266*e48880e0SDavid Brownell 	 * normally rely on the device being setup.  Devices
267*e48880e0SDavid Brownell 	 * using SPI_CS_HIGH can't coexist well otherwise...
268*e48880e0SDavid Brownell 	 */
269dc87c98eSGrant Likely 	status = spi->master->setup(spi);
270dc87c98eSGrant Likely 	if (status < 0) {
271dc87c98eSGrant Likely 		dev_err(dev, "can't %s %s, status %d\n",
272dc87c98eSGrant Likely 				"setup", spi->dev.bus_id, status);
273*e48880e0SDavid Brownell 		goto done;
274dc87c98eSGrant Likely 	}
275dc87c98eSGrant Likely 
276*e48880e0SDavid Brownell 	/* Device may be bound to an active driver when this returns */
277dc87c98eSGrant Likely 	status = device_add(&spi->dev);
278*e48880e0SDavid Brownell 	if (status < 0)
279dc87c98eSGrant Likely 		dev_err(dev, "can't %s %s, status %d\n",
280dc87c98eSGrant Likely 				"add", spi->dev.bus_id, status);
281*e48880e0SDavid Brownell 	else
282dc87c98eSGrant Likely 		dev_dbg(dev, "registered child %s\n", spi->dev.bus_id);
283*e48880e0SDavid Brownell 
284*e48880e0SDavid Brownell done:
285*e48880e0SDavid Brownell 	mutex_unlock(&spi_add_lock);
286*e48880e0SDavid Brownell 	return status;
287dc87c98eSGrant Likely }
288dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device);
2898ae12a0dSDavid Brownell 
29033e34dc6SDavid Brownell /**
29133e34dc6SDavid Brownell  * spi_new_device - instantiate one new SPI device
29233e34dc6SDavid Brownell  * @master: Controller to which device is connected
29333e34dc6SDavid Brownell  * @chip: Describes the SPI device
29433e34dc6SDavid Brownell  * Context: can sleep
29533e34dc6SDavid Brownell  *
29633e34dc6SDavid Brownell  * On typical mainboards, this is purely internal; and it's not needed
2978ae12a0dSDavid Brownell  * after board init creates the hard-wired devices.  Some development
2988ae12a0dSDavid Brownell  * platforms may not be able to use spi_register_board_info though, and
2998ae12a0dSDavid Brownell  * this is exported so that for example a USB or parport based adapter
3008ae12a0dSDavid Brownell  * driver could add devices (which it would learn about out-of-band).
301082c8cb4SDavid Brownell  *
302082c8cb4SDavid Brownell  * Returns the new device, or NULL.
3038ae12a0dSDavid Brownell  */
304e9d5a461SAdrian Bunk struct spi_device *spi_new_device(struct spi_master *master,
305e9d5a461SAdrian Bunk 				  struct spi_board_info *chip)
3068ae12a0dSDavid Brownell {
3078ae12a0dSDavid Brownell 	struct spi_device	*proxy;
3088ae12a0dSDavid Brownell 	int			status;
3098ae12a0dSDavid Brownell 
310082c8cb4SDavid Brownell 	/* NOTE:  caller did any chip->bus_num checks necessary.
311082c8cb4SDavid Brownell 	 *
312082c8cb4SDavid Brownell 	 * Also, unless we change the return value convention to use
313082c8cb4SDavid Brownell 	 * error-or-pointer (not NULL-or-pointer), troubleshootability
314082c8cb4SDavid Brownell 	 * suggests syslogged diagnostics are best here (ugh).
315082c8cb4SDavid Brownell 	 */
316082c8cb4SDavid Brownell 
317dc87c98eSGrant Likely 	proxy = spi_alloc_device(master);
318dc87c98eSGrant Likely 	if (!proxy)
3198ae12a0dSDavid Brownell 		return NULL;
3208ae12a0dSDavid Brownell 
321102eb975SGrant Likely 	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
322102eb975SGrant Likely 
3238ae12a0dSDavid Brownell 	proxy->chip_select = chip->chip_select;
3248ae12a0dSDavid Brownell 	proxy->max_speed_hz = chip->max_speed_hz;
325980a01c9SDavid Brownell 	proxy->mode = chip->mode;
3268ae12a0dSDavid Brownell 	proxy->irq = chip->irq;
327102eb975SGrant Likely 	strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
3288ae12a0dSDavid Brownell 	proxy->dev.platform_data = (void *) chip->platform_data;
3298ae12a0dSDavid Brownell 	proxy->controller_data = chip->controller_data;
3308ae12a0dSDavid Brownell 	proxy->controller_state = NULL;
3318ae12a0dSDavid Brownell 
332dc87c98eSGrant Likely 	status = spi_add_device(proxy);
3338ae12a0dSDavid Brownell 	if (status < 0) {
334dc87c98eSGrant Likely 		spi_dev_put(proxy);
3358ae12a0dSDavid Brownell 		return NULL;
3368ae12a0dSDavid Brownell 	}
337dc87c98eSGrant Likely 
338dc87c98eSGrant Likely 	return proxy;
339dc87c98eSGrant Likely }
3408ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device);
3418ae12a0dSDavid Brownell 
34233e34dc6SDavid Brownell /**
34333e34dc6SDavid Brownell  * spi_register_board_info - register SPI devices for a given board
34433e34dc6SDavid Brownell  * @info: array of chip descriptors
34533e34dc6SDavid Brownell  * @n: how many descriptors are provided
34633e34dc6SDavid Brownell  * Context: can sleep
34733e34dc6SDavid Brownell  *
3488ae12a0dSDavid Brownell  * Board-specific early init code calls this (probably during arch_initcall)
3498ae12a0dSDavid Brownell  * with segments of the SPI device table.  Any device nodes are created later,
3508ae12a0dSDavid Brownell  * after the relevant parent SPI controller (bus_num) is defined.  We keep
3518ae12a0dSDavid Brownell  * this table of devices forever, so that reloading a controller driver will
3528ae12a0dSDavid Brownell  * not make Linux forget about these hard-wired devices.
3538ae12a0dSDavid Brownell  *
3548ae12a0dSDavid Brownell  * Other code can also call this, e.g. a particular add-on board might provide
3558ae12a0dSDavid Brownell  * SPI devices through its expansion connector, so code initializing that board
3568ae12a0dSDavid Brownell  * would naturally declare its SPI devices.
3578ae12a0dSDavid Brownell  *
3588ae12a0dSDavid Brownell  * The board info passed can safely be __initdata ... but be careful of
3598ae12a0dSDavid Brownell  * any embedded pointers (platform_data, etc), they're copied as-is.
3608ae12a0dSDavid Brownell  */
3618ae12a0dSDavid Brownell int __init
3628ae12a0dSDavid Brownell spi_register_board_info(struct spi_board_info const *info, unsigned n)
3638ae12a0dSDavid Brownell {
3648ae12a0dSDavid Brownell 	struct boardinfo	*bi;
3658ae12a0dSDavid Brownell 
366b885244eSDavid Brownell 	bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);
3678ae12a0dSDavid Brownell 	if (!bi)
3688ae12a0dSDavid Brownell 		return -ENOMEM;
3698ae12a0dSDavid Brownell 	bi->n_board_info = n;
370b885244eSDavid Brownell 	memcpy(bi->board_info, info, n * sizeof *info);
3718ae12a0dSDavid Brownell 
37294040828SMatthias Kaehlcke 	mutex_lock(&board_lock);
3738ae12a0dSDavid Brownell 	list_add_tail(&bi->list, &board_list);
37494040828SMatthias Kaehlcke 	mutex_unlock(&board_lock);
3758ae12a0dSDavid Brownell 	return 0;
3768ae12a0dSDavid Brownell }
3778ae12a0dSDavid Brownell 
3788ae12a0dSDavid Brownell /* FIXME someone should add support for a __setup("spi", ...) that
3798ae12a0dSDavid Brownell  * creates board info from kernel command lines
3808ae12a0dSDavid Brownell  */
3818ae12a0dSDavid Brownell 
382149a6501SAdrian Bunk static void scan_boardinfo(struct spi_master *master)
3838ae12a0dSDavid Brownell {
3848ae12a0dSDavid Brownell 	struct boardinfo	*bi;
3858ae12a0dSDavid Brownell 
38694040828SMatthias Kaehlcke 	mutex_lock(&board_lock);
3878ae12a0dSDavid Brownell 	list_for_each_entry(bi, &board_list, list) {
3888ae12a0dSDavid Brownell 		struct spi_board_info	*chip = bi->board_info;
3898ae12a0dSDavid Brownell 		unsigned		n;
3908ae12a0dSDavid Brownell 
3918ae12a0dSDavid Brownell 		for (n = bi->n_board_info; n > 0; n--, chip++) {
3928ae12a0dSDavid Brownell 			if (chip->bus_num != master->bus_num)
3938ae12a0dSDavid Brownell 				continue;
394082c8cb4SDavid Brownell 			/* NOTE: this relies on spi_new_device to
395082c8cb4SDavid Brownell 			 * issue diagnostics when given bogus inputs
3968ae12a0dSDavid Brownell 			 */
3978ae12a0dSDavid Brownell 			(void) spi_new_device(master, chip);
3988ae12a0dSDavid Brownell 		}
3998ae12a0dSDavid Brownell 	}
40094040828SMatthias Kaehlcke 	mutex_unlock(&board_lock);
4018ae12a0dSDavid Brownell }
4028ae12a0dSDavid Brownell 
4038ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
4048ae12a0dSDavid Brownell 
40549dce689STony Jones static void spi_master_release(struct device *dev)
4068ae12a0dSDavid Brownell {
4078ae12a0dSDavid Brownell 	struct spi_master *master;
4088ae12a0dSDavid Brownell 
40949dce689STony Jones 	master = container_of(dev, struct spi_master, dev);
4108ae12a0dSDavid Brownell 	kfree(master);
4118ae12a0dSDavid Brownell }
4128ae12a0dSDavid Brownell 
4138ae12a0dSDavid Brownell static struct class spi_master_class = {
4148ae12a0dSDavid Brownell 	.name		= "spi_master",
4158ae12a0dSDavid Brownell 	.owner		= THIS_MODULE,
41649dce689STony Jones 	.dev_release	= spi_master_release,
4178ae12a0dSDavid Brownell };
4188ae12a0dSDavid Brownell 
4198ae12a0dSDavid Brownell 
4208ae12a0dSDavid Brownell /**
4218ae12a0dSDavid Brownell  * spi_alloc_master - allocate SPI master controller
4228ae12a0dSDavid Brownell  * @dev: the controller, possibly using the platform_bus
42333e34dc6SDavid Brownell  * @size: how much zeroed driver-private data to allocate; the pointer to this
42449dce689STony Jones  *	memory is in the driver_data field of the returned device,
4250c868461SDavid Brownell  *	accessible with spi_master_get_devdata().
42633e34dc6SDavid Brownell  * Context: can sleep
4278ae12a0dSDavid Brownell  *
4288ae12a0dSDavid Brownell  * This call is used only by SPI master controller drivers, which are the
4298ae12a0dSDavid Brownell  * only ones directly touching chip registers.  It's how they allocate
430ba1a0513Sdmitry pervushin  * an spi_master structure, prior to calling spi_register_master().
4318ae12a0dSDavid Brownell  *
4328ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns the SPI
4338ae12a0dSDavid Brownell  * master structure on success, else NULL.
4348ae12a0dSDavid Brownell  *
4358ae12a0dSDavid Brownell  * The caller is responsible for assigning the bus number and initializing
436ba1a0513Sdmitry pervushin  * the master's methods before calling spi_register_master(); and (after errors
4370c868461SDavid Brownell  * adding the device) calling spi_master_put() to prevent a memory leak.
4388ae12a0dSDavid Brownell  */
439e9d5a461SAdrian Bunk struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
4408ae12a0dSDavid Brownell {
4418ae12a0dSDavid Brownell 	struct spi_master	*master;
4428ae12a0dSDavid Brownell 
4430c868461SDavid Brownell 	if (!dev)
4440c868461SDavid Brownell 		return NULL;
4450c868461SDavid Brownell 
446e94b1766SChristoph Lameter 	master = kzalloc(size + sizeof *master, GFP_KERNEL);
4478ae12a0dSDavid Brownell 	if (!master)
4488ae12a0dSDavid Brownell 		return NULL;
4498ae12a0dSDavid Brownell 
45049dce689STony Jones 	device_initialize(&master->dev);
45149dce689STony Jones 	master->dev.class = &spi_master_class;
45249dce689STony Jones 	master->dev.parent = get_device(dev);
4530c868461SDavid Brownell 	spi_master_set_devdata(master, &master[1]);
4548ae12a0dSDavid Brownell 
4558ae12a0dSDavid Brownell 	return master;
4568ae12a0dSDavid Brownell }
4578ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_alloc_master);
4588ae12a0dSDavid Brownell 
4598ae12a0dSDavid Brownell /**
4608ae12a0dSDavid Brownell  * spi_register_master - register SPI master controller
4618ae12a0dSDavid Brownell  * @master: initialized master, originally from spi_alloc_master()
46233e34dc6SDavid Brownell  * Context: can sleep
4638ae12a0dSDavid Brownell  *
4648ae12a0dSDavid Brownell  * SPI master controllers connect to their drivers using some non-SPI bus,
4658ae12a0dSDavid Brownell  * such as the platform bus.  The final stage of probe() in that code
4668ae12a0dSDavid Brownell  * includes calling spi_register_master() to hook up to this SPI bus glue.
4678ae12a0dSDavid Brownell  *
4688ae12a0dSDavid Brownell  * SPI controllers use board specific (often SOC specific) bus numbers,
4698ae12a0dSDavid Brownell  * and board-specific addressing for SPI devices combines those numbers
4708ae12a0dSDavid Brownell  * with chip select numbers.  Since SPI does not directly support dynamic
4718ae12a0dSDavid Brownell  * device identification, boards need configuration tables telling which
4728ae12a0dSDavid Brownell  * chip is at which address.
4738ae12a0dSDavid Brownell  *
4748ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns zero on
4758ae12a0dSDavid Brownell  * success, else a negative error code (dropping the master's refcount).
4760c868461SDavid Brownell  * After a successful return, the caller is responsible for calling
4770c868461SDavid Brownell  * spi_unregister_master().
4788ae12a0dSDavid Brownell  */
479e9d5a461SAdrian Bunk int spi_register_master(struct spi_master *master)
4808ae12a0dSDavid Brownell {
481e44a45aeSDavid Brownell 	static atomic_t		dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
48249dce689STony Jones 	struct device		*dev = master->dev.parent;
4838ae12a0dSDavid Brownell 	int			status = -ENODEV;
4848ae12a0dSDavid Brownell 	int			dynamic = 0;
4858ae12a0dSDavid Brownell 
4860c868461SDavid Brownell 	if (!dev)
4870c868461SDavid Brownell 		return -ENODEV;
4880c868461SDavid Brownell 
489082c8cb4SDavid Brownell 	/* even if it's just one always-selected device, there must
490082c8cb4SDavid Brownell 	 * be at least one chipselect
491082c8cb4SDavid Brownell 	 */
492082c8cb4SDavid Brownell 	if (master->num_chipselect == 0)
493082c8cb4SDavid Brownell 		return -EINVAL;
494082c8cb4SDavid Brownell 
4958ae12a0dSDavid Brownell 	/* convention:  dynamically assigned bus IDs count down from the max */
496a020ed75SDavid Brownell 	if (master->bus_num < 0) {
497082c8cb4SDavid Brownell 		/* FIXME switch to an IDR based scheme, something like
498082c8cb4SDavid Brownell 		 * I2C now uses, so we can't run out of "dynamic" IDs
499082c8cb4SDavid Brownell 		 */
5008ae12a0dSDavid Brownell 		master->bus_num = atomic_dec_return(&dyn_bus_id);
501b885244eSDavid Brownell 		dynamic = 1;
5028ae12a0dSDavid Brownell 	}
5038ae12a0dSDavid Brownell 
5048ae12a0dSDavid Brownell 	/* register the device, then userspace will see it.
5058ae12a0dSDavid Brownell 	 * registration fails if the bus ID is in use.
5068ae12a0dSDavid Brownell 	 */
50749dce689STony Jones 	snprintf(master->dev.bus_id, sizeof master->dev.bus_id,
5088ae12a0dSDavid Brownell 		"spi%u", master->bus_num);
50949dce689STony Jones 	status = device_add(&master->dev);
510b885244eSDavid Brownell 	if (status < 0)
5118ae12a0dSDavid Brownell 		goto done;
51249dce689STony Jones 	dev_dbg(dev, "registered master %s%s\n", master->dev.bus_id,
5138ae12a0dSDavid Brownell 			dynamic ? " (dynamic)" : "");
5148ae12a0dSDavid Brownell 
5158ae12a0dSDavid Brownell 	/* populate children from any spi device tables */
5168ae12a0dSDavid Brownell 	scan_boardinfo(master);
5178ae12a0dSDavid Brownell 	status = 0;
5188ae12a0dSDavid Brownell done:
5198ae12a0dSDavid Brownell 	return status;
5208ae12a0dSDavid Brownell }
5218ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_master);
5228ae12a0dSDavid Brownell 
5238ae12a0dSDavid Brownell 
524350d0076SAtsushi Nemoto static int __unregister(struct device *dev, void *master_dev)
5258ae12a0dSDavid Brownell {
5268ae12a0dSDavid Brownell 	/* note: before about 2.6.14-rc1 this would corrupt memory: */
527350d0076SAtsushi Nemoto 	if (dev != master_dev)
5280c868461SDavid Brownell 		spi_unregister_device(to_spi_device(dev));
5298ae12a0dSDavid Brownell 	return 0;
5308ae12a0dSDavid Brownell }
5318ae12a0dSDavid Brownell 
5328ae12a0dSDavid Brownell /**
5338ae12a0dSDavid Brownell  * spi_unregister_master - unregister SPI master controller
5348ae12a0dSDavid Brownell  * @master: the master being unregistered
53533e34dc6SDavid Brownell  * Context: can sleep
5368ae12a0dSDavid Brownell  *
5378ae12a0dSDavid Brownell  * This call is used only by SPI master controller drivers, which are the
5388ae12a0dSDavid Brownell  * only ones directly touching chip registers.
5398ae12a0dSDavid Brownell  *
5408ae12a0dSDavid Brownell  * This must be called from context that can sleep.
5418ae12a0dSDavid Brownell  */
5428ae12a0dSDavid Brownell void spi_unregister_master(struct spi_master *master)
5438ae12a0dSDavid Brownell {
54489fc9a1aSJeff Garzik 	int dummy;
54589fc9a1aSJeff Garzik 
546350d0076SAtsushi Nemoto 	dummy = device_for_each_child(master->dev.parent, &master->dev,
547350d0076SAtsushi Nemoto 					__unregister);
54849dce689STony Jones 	device_unregister(&master->dev);
5498ae12a0dSDavid Brownell }
5508ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_unregister_master);
5518ae12a0dSDavid Brownell 
5525ed2c832SDave Young static int __spi_master_match(struct device *dev, void *data)
5535ed2c832SDave Young {
5545ed2c832SDave Young 	struct spi_master *m;
5555ed2c832SDave Young 	u16 *bus_num = data;
5565ed2c832SDave Young 
5575ed2c832SDave Young 	m = container_of(dev, struct spi_master, dev);
5585ed2c832SDave Young 	return m->bus_num == *bus_num;
5595ed2c832SDave Young }
5605ed2c832SDave Young 
5618ae12a0dSDavid Brownell /**
5628ae12a0dSDavid Brownell  * spi_busnum_to_master - look up master associated with bus_num
5638ae12a0dSDavid Brownell  * @bus_num: the master's bus number
56433e34dc6SDavid Brownell  * Context: can sleep
5658ae12a0dSDavid Brownell  *
5668ae12a0dSDavid Brownell  * This call may be used with devices that are registered after
5678ae12a0dSDavid Brownell  * arch init time.  It returns a refcounted pointer to the relevant
5688ae12a0dSDavid Brownell  * spi_master (which the caller must release), or NULL if there is
5698ae12a0dSDavid Brownell  * no such master registered.
5708ae12a0dSDavid Brownell  */
5718ae12a0dSDavid Brownell struct spi_master *spi_busnum_to_master(u16 bus_num)
5728ae12a0dSDavid Brownell {
57349dce689STony Jones 	struct device		*dev;
5741e9a51dcSAtsushi Nemoto 	struct spi_master	*master = NULL;
5758ae12a0dSDavid Brownell 
576695794aeSGreg Kroah-Hartman 	dev = class_find_device(&spi_master_class, NULL, &bus_num,
5775ed2c832SDave Young 				__spi_master_match);
5785ed2c832SDave Young 	if (dev)
5795ed2c832SDave Young 		master = container_of(dev, struct spi_master, dev);
5805ed2c832SDave Young 	/* reference got in class_find_device */
5811e9a51dcSAtsushi Nemoto 	return master;
5828ae12a0dSDavid Brownell }
5838ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master);
5848ae12a0dSDavid Brownell 
5858ae12a0dSDavid Brownell 
5868ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
5878ae12a0dSDavid Brownell 
5885d870c8eSAndrew Morton static void spi_complete(void *arg)
5895d870c8eSAndrew Morton {
5905d870c8eSAndrew Morton 	complete(arg);
5915d870c8eSAndrew Morton }
5925d870c8eSAndrew Morton 
5938ae12a0dSDavid Brownell /**
5948ae12a0dSDavid Brownell  * spi_sync - blocking/synchronous SPI data transfers
5958ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
5968ae12a0dSDavid Brownell  * @message: describes the data transfers
59733e34dc6SDavid Brownell  * Context: can sleep
5988ae12a0dSDavid Brownell  *
5998ae12a0dSDavid Brownell  * This call may only be used from a context that may sleep.  The sleep
6008ae12a0dSDavid Brownell  * is non-interruptible, and has no timeout.  Low-overhead controller
6018ae12a0dSDavid Brownell  * drivers may DMA directly into and out of the message buffers.
6028ae12a0dSDavid Brownell  *
6038ae12a0dSDavid Brownell  * Note that the SPI device's chip select is active during the message,
6048ae12a0dSDavid Brownell  * and then is normally disabled between messages.  Drivers for some
6058ae12a0dSDavid Brownell  * frequently-used devices may want to minimize costs of selecting a chip,
6068ae12a0dSDavid Brownell  * by leaving it selected in anticipation that the next message will go
6078ae12a0dSDavid Brownell  * to the same chip.  (That may increase power usage.)
6088ae12a0dSDavid Brownell  *
6090c868461SDavid Brownell  * Also, the caller is guaranteeing that the memory associated with the
6100c868461SDavid Brownell  * message will not be freed before this call returns.
6110c868461SDavid Brownell  *
6129b938b74SMarc Pignat  * It returns zero on success, else a negative error code.
6138ae12a0dSDavid Brownell  */
6148ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message)
6158ae12a0dSDavid Brownell {
61660be6b9aSIngo Molnar 	DECLARE_COMPLETION_ONSTACK(done);
6178ae12a0dSDavid Brownell 	int status;
6188ae12a0dSDavid Brownell 
6195d870c8eSAndrew Morton 	message->complete = spi_complete;
6208ae12a0dSDavid Brownell 	message->context = &done;
6218ae12a0dSDavid Brownell 	status = spi_async(spi, message);
6229b938b74SMarc Pignat 	if (status == 0) {
6238ae12a0dSDavid Brownell 		wait_for_completion(&done);
6249b938b74SMarc Pignat 		status = message->status;
6259b938b74SMarc Pignat 	}
6268ae12a0dSDavid Brownell 	message->context = NULL;
6278ae12a0dSDavid Brownell 	return status;
6288ae12a0dSDavid Brownell }
6298ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync);
6308ae12a0dSDavid Brownell 
631a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */
632a9948b61SDavid Brownell #define	SPI_BUFSIZ	max(32,SMP_CACHE_BYTES)
6338ae12a0dSDavid Brownell 
6348ae12a0dSDavid Brownell static u8	*buf;
6358ae12a0dSDavid Brownell 
6368ae12a0dSDavid Brownell /**
6378ae12a0dSDavid Brownell  * spi_write_then_read - SPI synchronous write followed by read
6388ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
6398ae12a0dSDavid Brownell  * @txbuf: data to be written (need not be dma-safe)
6408ae12a0dSDavid Brownell  * @n_tx: size of txbuf, in bytes
6418ae12a0dSDavid Brownell  * @rxbuf: buffer into which data will be read
6428ae12a0dSDavid Brownell  * @n_rx: size of rxbuf, in bytes (need not be dma-safe)
64333e34dc6SDavid Brownell  * Context: can sleep
6448ae12a0dSDavid Brownell  *
6458ae12a0dSDavid Brownell  * This performs a half duplex MicroWire style transaction with the
6468ae12a0dSDavid Brownell  * device, sending txbuf and then reading rxbuf.  The return value
6478ae12a0dSDavid Brownell  * is zero for success, else a negative errno status code.
648b885244eSDavid Brownell  * This call may only be used from a context that may sleep.
6498ae12a0dSDavid Brownell  *
6500c868461SDavid Brownell  * Parameters to this routine are always copied using a small buffer;
65133e34dc6SDavid Brownell  * portable code should never use this for more than 32 bytes.
65233e34dc6SDavid Brownell  * Performance-sensitive or bulk transfer code should instead use
6530c868461SDavid Brownell  * spi_{async,sync}() calls with dma-safe buffers.
6548ae12a0dSDavid Brownell  */
6558ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi,
6568ae12a0dSDavid Brownell 		const u8 *txbuf, unsigned n_tx,
6578ae12a0dSDavid Brownell 		u8 *rxbuf, unsigned n_rx)
6588ae12a0dSDavid Brownell {
659068f4070SDavid Brownell 	static DEFINE_MUTEX(lock);
6608ae12a0dSDavid Brownell 
6618ae12a0dSDavid Brownell 	int			status;
6628ae12a0dSDavid Brownell 	struct spi_message	message;
6638ae12a0dSDavid Brownell 	struct spi_transfer	x[2];
6648ae12a0dSDavid Brownell 	u8			*local_buf;
6658ae12a0dSDavid Brownell 
6668ae12a0dSDavid Brownell 	/* Use preallocated DMA-safe buffer.  We can't avoid copying here,
6678ae12a0dSDavid Brownell 	 * (as a pure convenience thing), but we can keep heap costs
6688ae12a0dSDavid Brownell 	 * out of the hot path ...
6698ae12a0dSDavid Brownell 	 */
6708ae12a0dSDavid Brownell 	if ((n_tx + n_rx) > SPI_BUFSIZ)
6718ae12a0dSDavid Brownell 		return -EINVAL;
6728ae12a0dSDavid Brownell 
6738275c642SVitaly Wool 	spi_message_init(&message);
6748275c642SVitaly Wool 	memset(x, 0, sizeof x);
6758275c642SVitaly Wool 	if (n_tx) {
6768275c642SVitaly Wool 		x[0].len = n_tx;
6778275c642SVitaly Wool 		spi_message_add_tail(&x[0], &message);
6788275c642SVitaly Wool 	}
6798275c642SVitaly Wool 	if (n_rx) {
6808275c642SVitaly Wool 		x[1].len = n_rx;
6818275c642SVitaly Wool 		spi_message_add_tail(&x[1], &message);
6828275c642SVitaly Wool 	}
6838275c642SVitaly Wool 
6848ae12a0dSDavid Brownell 	/* ... unless someone else is using the pre-allocated buffer */
685068f4070SDavid Brownell 	if (!mutex_trylock(&lock)) {
6868ae12a0dSDavid Brownell 		local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
6878ae12a0dSDavid Brownell 		if (!local_buf)
6888ae12a0dSDavid Brownell 			return -ENOMEM;
6898ae12a0dSDavid Brownell 	} else
6908ae12a0dSDavid Brownell 		local_buf = buf;
6918ae12a0dSDavid Brownell 
6928ae12a0dSDavid Brownell 	memcpy(local_buf, txbuf, n_tx);
6938ae12a0dSDavid Brownell 	x[0].tx_buf = local_buf;
6948ae12a0dSDavid Brownell 	x[1].rx_buf = local_buf + n_tx;
6958ae12a0dSDavid Brownell 
6968ae12a0dSDavid Brownell 	/* do the i/o */
6978ae12a0dSDavid Brownell 	status = spi_sync(spi, &message);
6989b938b74SMarc Pignat 	if (status == 0)
6998ae12a0dSDavid Brownell 		memcpy(rxbuf, x[1].rx_buf, n_rx);
7008ae12a0dSDavid Brownell 
7018ae12a0dSDavid Brownell 	if (x[0].tx_buf == buf)
702068f4070SDavid Brownell 		mutex_unlock(&lock);
7038ae12a0dSDavid Brownell 	else
7048ae12a0dSDavid Brownell 		kfree(local_buf);
7058ae12a0dSDavid Brownell 
7068ae12a0dSDavid Brownell 	return status;
7078ae12a0dSDavid Brownell }
7088ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read);
7098ae12a0dSDavid Brownell 
7108ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
7118ae12a0dSDavid Brownell 
7128ae12a0dSDavid Brownell static int __init spi_init(void)
7138ae12a0dSDavid Brownell {
714b885244eSDavid Brownell 	int	status;
7158ae12a0dSDavid Brownell 
716e94b1766SChristoph Lameter 	buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
717b885244eSDavid Brownell 	if (!buf) {
718b885244eSDavid Brownell 		status = -ENOMEM;
719b885244eSDavid Brownell 		goto err0;
7208ae12a0dSDavid Brownell 	}
721b885244eSDavid Brownell 
722b885244eSDavid Brownell 	status = bus_register(&spi_bus_type);
723b885244eSDavid Brownell 	if (status < 0)
724b885244eSDavid Brownell 		goto err1;
725b885244eSDavid Brownell 
726b885244eSDavid Brownell 	status = class_register(&spi_master_class);
727b885244eSDavid Brownell 	if (status < 0)
728b885244eSDavid Brownell 		goto err2;
729b885244eSDavid Brownell 	return 0;
730b885244eSDavid Brownell 
731b885244eSDavid Brownell err2:
732b885244eSDavid Brownell 	bus_unregister(&spi_bus_type);
733b885244eSDavid Brownell err1:
734b885244eSDavid Brownell 	kfree(buf);
735b885244eSDavid Brownell 	buf = NULL;
736b885244eSDavid Brownell err0:
737b885244eSDavid Brownell 	return status;
738b885244eSDavid Brownell }
739b885244eSDavid Brownell 
7408ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(),
7418ae12a0dSDavid Brownell  * but even essential drivers wait till later
742b885244eSDavid Brownell  *
743b885244eSDavid Brownell  * REVISIT only boardinfo really needs static linking. the rest (device and
744b885244eSDavid Brownell  * driver registration) _could_ be dynamically linked (modular) ... costs
745b885244eSDavid Brownell  * include needing to have boardinfo data structures be much more public.
7468ae12a0dSDavid Brownell  */
7478ae12a0dSDavid Brownell subsys_initcall(spi_init);
7488ae12a0dSDavid Brownell 
749