xref: /linux/drivers/spi/spi.c (revision b6fb8d3a1f156c50a35f88b9b55f404034493938)
18ae12a0dSDavid Brownell /*
2ca632f55SGrant Likely  * SPI init/core code
38ae12a0dSDavid Brownell  *
48ae12a0dSDavid Brownell  * Copyright (C) 2005 David Brownell
5d57a4282SGrant Likely  * Copyright (C) 2008 Secret Lab Technologies Ltd.
68ae12a0dSDavid Brownell  *
78ae12a0dSDavid Brownell  * This program is free software; you can redistribute it and/or modify
88ae12a0dSDavid Brownell  * it under the terms of the GNU General Public License as published by
98ae12a0dSDavid Brownell  * the Free Software Foundation; either version 2 of the License, or
108ae12a0dSDavid Brownell  * (at your option) any later version.
118ae12a0dSDavid Brownell  *
128ae12a0dSDavid Brownell  * This program is distributed in the hope that it will be useful,
138ae12a0dSDavid Brownell  * but WITHOUT ANY WARRANTY; without even the implied warranty of
148ae12a0dSDavid Brownell  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158ae12a0dSDavid Brownell  * GNU General Public License for more details.
168ae12a0dSDavid Brownell  *
178ae12a0dSDavid Brownell  * You should have received a copy of the GNU General Public License
188ae12a0dSDavid Brownell  * along with this program; if not, write to the Free Software
198ae12a0dSDavid Brownell  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
208ae12a0dSDavid Brownell  */
218ae12a0dSDavid Brownell 
228ae12a0dSDavid Brownell #include <linux/kernel.h>
23d57a4282SGrant Likely #include <linux/kmod.h>
248ae12a0dSDavid Brownell #include <linux/device.h>
258ae12a0dSDavid Brownell #include <linux/init.h>
268ae12a0dSDavid Brownell #include <linux/cache.h>
2794040828SMatthias Kaehlcke #include <linux/mutex.h>
282b7a32f7SSinan Akman #include <linux/of_device.h>
29d57a4282SGrant Likely #include <linux/of_irq.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h>
328ae12a0dSDavid Brownell #include <linux/spi/spi.h>
3374317984SJean-Christophe PLAGNIOL-VILLARD #include <linux/of_gpio.h>
343ae22e8cSMark Brown #include <linux/pm_runtime.h>
35025ed130SPaul Gortmaker #include <linux/export.h>
368bd75c77SClark Williams #include <linux/sched/rt.h>
37ffbbdd21SLinus Walleij #include <linux/delay.h>
38ffbbdd21SLinus Walleij #include <linux/kthread.h>
3964bee4d2SMika Westerberg #include <linux/ioport.h>
4064bee4d2SMika Westerberg #include <linux/acpi.h>
418ae12a0dSDavid Brownell 
4256ec1978SMark Brown #define CREATE_TRACE_POINTS
4356ec1978SMark Brown #include <trace/events/spi.h>
4456ec1978SMark Brown 
458ae12a0dSDavid Brownell static void spidev_release(struct device *dev)
468ae12a0dSDavid Brownell {
470ffa0285SHans-Peter Nilsson 	struct spi_device	*spi = to_spi_device(dev);
488ae12a0dSDavid Brownell 
498ae12a0dSDavid Brownell 	/* spi masters may cleanup for released devices */
508ae12a0dSDavid Brownell 	if (spi->master->cleanup)
518ae12a0dSDavid Brownell 		spi->master->cleanup(spi);
528ae12a0dSDavid Brownell 
530c868461SDavid Brownell 	spi_master_put(spi->master);
5407a389feSRoman Tereshonkov 	kfree(spi);
558ae12a0dSDavid Brownell }
568ae12a0dSDavid Brownell 
578ae12a0dSDavid Brownell static ssize_t
588ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf)
598ae12a0dSDavid Brownell {
608ae12a0dSDavid Brownell 	const struct spi_device	*spi = to_spi_device(dev);
618ae12a0dSDavid Brownell 
62d8e328b3SGrant Likely 	return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
638ae12a0dSDavid Brownell }
64aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
658ae12a0dSDavid Brownell 
66aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = {
67aa7da564SGreg Kroah-Hartman 	&dev_attr_modalias.attr,
68aa7da564SGreg Kroah-Hartman 	NULL,
698ae12a0dSDavid Brownell };
70aa7da564SGreg Kroah-Hartman ATTRIBUTE_GROUPS(spi_dev);
718ae12a0dSDavid Brownell 
728ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
738ae12a0dSDavid Brownell  * and the sysfs version makes coldplug work too.
748ae12a0dSDavid Brownell  */
758ae12a0dSDavid Brownell 
7675368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
7775368bf6SAnton Vorontsov 						const struct spi_device *sdev)
7875368bf6SAnton Vorontsov {
7975368bf6SAnton Vorontsov 	while (id->name[0]) {
8075368bf6SAnton Vorontsov 		if (!strcmp(sdev->modalias, id->name))
8175368bf6SAnton Vorontsov 			return id;
8275368bf6SAnton Vorontsov 		id++;
8375368bf6SAnton Vorontsov 	}
8475368bf6SAnton Vorontsov 	return NULL;
8575368bf6SAnton Vorontsov }
8675368bf6SAnton Vorontsov 
8775368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
8875368bf6SAnton Vorontsov {
8975368bf6SAnton Vorontsov 	const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
9075368bf6SAnton Vorontsov 
9175368bf6SAnton Vorontsov 	return spi_match_id(sdrv->id_table, sdev);
9275368bf6SAnton Vorontsov }
9375368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id);
9475368bf6SAnton Vorontsov 
958ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv)
968ae12a0dSDavid Brownell {
978ae12a0dSDavid Brownell 	const struct spi_device	*spi = to_spi_device(dev);
9875368bf6SAnton Vorontsov 	const struct spi_driver	*sdrv = to_spi_driver(drv);
9975368bf6SAnton Vorontsov 
1002b7a32f7SSinan Akman 	/* Attempt an OF style match */
1012b7a32f7SSinan Akman 	if (of_driver_match_device(dev, drv))
1022b7a32f7SSinan Akman 		return 1;
1032b7a32f7SSinan Akman 
10464bee4d2SMika Westerberg 	/* Then try ACPI */
10564bee4d2SMika Westerberg 	if (acpi_driver_match_device(dev, drv))
10664bee4d2SMika Westerberg 		return 1;
10764bee4d2SMika Westerberg 
10875368bf6SAnton Vorontsov 	if (sdrv->id_table)
10975368bf6SAnton Vorontsov 		return !!spi_match_id(sdrv->id_table, spi);
1108ae12a0dSDavid Brownell 
11135f74fcaSKay Sievers 	return strcmp(spi->modalias, drv->name) == 0;
1128ae12a0dSDavid Brownell }
1138ae12a0dSDavid Brownell 
1147eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
1158ae12a0dSDavid Brownell {
1168ae12a0dSDavid Brownell 	const struct spi_device		*spi = to_spi_device(dev);
1178ae12a0dSDavid Brownell 
118e0626e38SAnton Vorontsov 	add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
1198ae12a0dSDavid Brownell 	return 0;
1208ae12a0dSDavid Brownell }
1218ae12a0dSDavid Brownell 
1223ae22e8cSMark Brown #ifdef CONFIG_PM_SLEEP
1233ae22e8cSMark Brown static int spi_legacy_suspend(struct device *dev, pm_message_t message)
1248ae12a0dSDavid Brownell {
1253c72426fSDavid Brownell 	int			value = 0;
126b885244eSDavid Brownell 	struct spi_driver	*drv = to_spi_driver(dev->driver);
1278ae12a0dSDavid Brownell 
1288ae12a0dSDavid Brownell 	/* suspend will stop irqs and dma; no more i/o */
1293c72426fSDavid Brownell 	if (drv) {
1303c72426fSDavid Brownell 		if (drv->suspend)
131b885244eSDavid Brownell 			value = drv->suspend(to_spi_device(dev), message);
1323c72426fSDavid Brownell 		else
1333c72426fSDavid Brownell 			dev_dbg(dev, "... can't suspend\n");
1343c72426fSDavid Brownell 	}
1358ae12a0dSDavid Brownell 	return value;
1368ae12a0dSDavid Brownell }
1378ae12a0dSDavid Brownell 
1383ae22e8cSMark Brown static int spi_legacy_resume(struct device *dev)
1398ae12a0dSDavid Brownell {
1403c72426fSDavid Brownell 	int			value = 0;
141b885244eSDavid Brownell 	struct spi_driver	*drv = to_spi_driver(dev->driver);
1428ae12a0dSDavid Brownell 
1438ae12a0dSDavid Brownell 	/* resume may restart the i/o queue */
1443c72426fSDavid Brownell 	if (drv) {
1453c72426fSDavid Brownell 		if (drv->resume)
146b885244eSDavid Brownell 			value = drv->resume(to_spi_device(dev));
1473c72426fSDavid Brownell 		else
1483c72426fSDavid Brownell 			dev_dbg(dev, "... can't resume\n");
1493c72426fSDavid Brownell 	}
1508ae12a0dSDavid Brownell 	return value;
1518ae12a0dSDavid Brownell }
1528ae12a0dSDavid Brownell 
1533ae22e8cSMark Brown static int spi_pm_suspend(struct device *dev)
1543ae22e8cSMark Brown {
1553ae22e8cSMark Brown 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1563ae22e8cSMark Brown 
1573ae22e8cSMark Brown 	if (pm)
1583ae22e8cSMark Brown 		return pm_generic_suspend(dev);
1593ae22e8cSMark Brown 	else
1603ae22e8cSMark Brown 		return spi_legacy_suspend(dev, PMSG_SUSPEND);
1613ae22e8cSMark Brown }
1623ae22e8cSMark Brown 
1633ae22e8cSMark Brown static int spi_pm_resume(struct device *dev)
1643ae22e8cSMark Brown {
1653ae22e8cSMark Brown 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1663ae22e8cSMark Brown 
1673ae22e8cSMark Brown 	if (pm)
1683ae22e8cSMark Brown 		return pm_generic_resume(dev);
1693ae22e8cSMark Brown 	else
1703ae22e8cSMark Brown 		return spi_legacy_resume(dev);
1713ae22e8cSMark Brown }
1723ae22e8cSMark Brown 
1733ae22e8cSMark Brown static int spi_pm_freeze(struct device *dev)
1743ae22e8cSMark Brown {
1753ae22e8cSMark Brown 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1763ae22e8cSMark Brown 
1773ae22e8cSMark Brown 	if (pm)
1783ae22e8cSMark Brown 		return pm_generic_freeze(dev);
1793ae22e8cSMark Brown 	else
1803ae22e8cSMark Brown 		return spi_legacy_suspend(dev, PMSG_FREEZE);
1813ae22e8cSMark Brown }
1823ae22e8cSMark Brown 
1833ae22e8cSMark Brown static int spi_pm_thaw(struct device *dev)
1843ae22e8cSMark Brown {
1853ae22e8cSMark Brown 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1863ae22e8cSMark Brown 
1873ae22e8cSMark Brown 	if (pm)
1883ae22e8cSMark Brown 		return pm_generic_thaw(dev);
1893ae22e8cSMark Brown 	else
1903ae22e8cSMark Brown 		return spi_legacy_resume(dev);
1913ae22e8cSMark Brown }
1923ae22e8cSMark Brown 
1933ae22e8cSMark Brown static int spi_pm_poweroff(struct device *dev)
1943ae22e8cSMark Brown {
1953ae22e8cSMark Brown 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1963ae22e8cSMark Brown 
1973ae22e8cSMark Brown 	if (pm)
1983ae22e8cSMark Brown 		return pm_generic_poweroff(dev);
1993ae22e8cSMark Brown 	else
2003ae22e8cSMark Brown 		return spi_legacy_suspend(dev, PMSG_HIBERNATE);
2013ae22e8cSMark Brown }
2023ae22e8cSMark Brown 
2033ae22e8cSMark Brown static int spi_pm_restore(struct device *dev)
2043ae22e8cSMark Brown {
2053ae22e8cSMark Brown 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
2063ae22e8cSMark Brown 
2073ae22e8cSMark Brown 	if (pm)
2083ae22e8cSMark Brown 		return pm_generic_restore(dev);
2093ae22e8cSMark Brown 	else
2103ae22e8cSMark Brown 		return spi_legacy_resume(dev);
2113ae22e8cSMark Brown }
2128ae12a0dSDavid Brownell #else
2133ae22e8cSMark Brown #define spi_pm_suspend	NULL
2143ae22e8cSMark Brown #define spi_pm_resume	NULL
2153ae22e8cSMark Brown #define spi_pm_freeze	NULL
2163ae22e8cSMark Brown #define spi_pm_thaw	NULL
2173ae22e8cSMark Brown #define spi_pm_poweroff	NULL
2183ae22e8cSMark Brown #define spi_pm_restore	NULL
2198ae12a0dSDavid Brownell #endif
2208ae12a0dSDavid Brownell 
2213ae22e8cSMark Brown static const struct dev_pm_ops spi_pm = {
2223ae22e8cSMark Brown 	.suspend = spi_pm_suspend,
2233ae22e8cSMark Brown 	.resume = spi_pm_resume,
2243ae22e8cSMark Brown 	.freeze = spi_pm_freeze,
2253ae22e8cSMark Brown 	.thaw = spi_pm_thaw,
2263ae22e8cSMark Brown 	.poweroff = spi_pm_poweroff,
2273ae22e8cSMark Brown 	.restore = spi_pm_restore,
2283ae22e8cSMark Brown 	SET_RUNTIME_PM_OPS(
2293ae22e8cSMark Brown 		pm_generic_runtime_suspend,
2303ae22e8cSMark Brown 		pm_generic_runtime_resume,
23145f0a85cSRafael J. Wysocki 		NULL
2323ae22e8cSMark Brown 	)
2333ae22e8cSMark Brown };
2343ae22e8cSMark Brown 
2358ae12a0dSDavid Brownell struct bus_type spi_bus_type = {
2368ae12a0dSDavid Brownell 	.name		= "spi",
237aa7da564SGreg Kroah-Hartman 	.dev_groups	= spi_dev_groups,
2388ae12a0dSDavid Brownell 	.match		= spi_match_device,
2398ae12a0dSDavid Brownell 	.uevent		= spi_uevent,
2403ae22e8cSMark Brown 	.pm		= &spi_pm,
2418ae12a0dSDavid Brownell };
2428ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type);
2438ae12a0dSDavid Brownell 
244b885244eSDavid Brownell 
245b885244eSDavid Brownell static int spi_drv_probe(struct device *dev)
246b885244eSDavid Brownell {
247b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
24833cf00e5SMika Westerberg 	struct spi_device		*spi = to_spi_device(dev);
24933cf00e5SMika Westerberg 	int ret;
250b885244eSDavid Brownell 
25133cf00e5SMika Westerberg 	acpi_dev_pm_attach(&spi->dev, true);
25233cf00e5SMika Westerberg 	ret = sdrv->probe(spi);
25333cf00e5SMika Westerberg 	if (ret)
25433cf00e5SMika Westerberg 		acpi_dev_pm_detach(&spi->dev, true);
25533cf00e5SMika Westerberg 
25633cf00e5SMika Westerberg 	return ret;
257b885244eSDavid Brownell }
258b885244eSDavid Brownell 
259b885244eSDavid Brownell static int spi_drv_remove(struct device *dev)
260b885244eSDavid Brownell {
261b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
26233cf00e5SMika Westerberg 	struct spi_device		*spi = to_spi_device(dev);
26333cf00e5SMika Westerberg 	int ret;
264b885244eSDavid Brownell 
26533cf00e5SMika Westerberg 	ret = sdrv->remove(spi);
26633cf00e5SMika Westerberg 	acpi_dev_pm_detach(&spi->dev, true);
26733cf00e5SMika Westerberg 
26833cf00e5SMika Westerberg 	return ret;
269b885244eSDavid Brownell }
270b885244eSDavid Brownell 
271b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev)
272b885244eSDavid Brownell {
273b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
274b885244eSDavid Brownell 
275b885244eSDavid Brownell 	sdrv->shutdown(to_spi_device(dev));
276b885244eSDavid Brownell }
277b885244eSDavid Brownell 
27833e34dc6SDavid Brownell /**
27933e34dc6SDavid Brownell  * spi_register_driver - register a SPI driver
28033e34dc6SDavid Brownell  * @sdrv: the driver to register
28133e34dc6SDavid Brownell  * Context: can sleep
28233e34dc6SDavid Brownell  */
283b885244eSDavid Brownell int spi_register_driver(struct spi_driver *sdrv)
284b885244eSDavid Brownell {
285b885244eSDavid Brownell 	sdrv->driver.bus = &spi_bus_type;
286b885244eSDavid Brownell 	if (sdrv->probe)
287b885244eSDavid Brownell 		sdrv->driver.probe = spi_drv_probe;
288b885244eSDavid Brownell 	if (sdrv->remove)
289b885244eSDavid Brownell 		sdrv->driver.remove = spi_drv_remove;
290b885244eSDavid Brownell 	if (sdrv->shutdown)
291b885244eSDavid Brownell 		sdrv->driver.shutdown = spi_drv_shutdown;
292b885244eSDavid Brownell 	return driver_register(&sdrv->driver);
293b885244eSDavid Brownell }
294b885244eSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_driver);
295b885244eSDavid Brownell 
2968ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
2978ae12a0dSDavid Brownell 
2988ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that
2998ae12a0dSDavid Brownell  * would make them board-specific.  Similarly with SPI master drivers.
3008ae12a0dSDavid Brownell  * Device registration normally goes into like arch/.../mach.../board-YYY.c
3018ae12a0dSDavid Brownell  * with other readonly (flashable) information about mainboard devices.
3028ae12a0dSDavid Brownell  */
3038ae12a0dSDavid Brownell 
3048ae12a0dSDavid Brownell struct boardinfo {
3058ae12a0dSDavid Brownell 	struct list_head	list;
3062b9603a0SFeng Tang 	struct spi_board_info	board_info;
3078ae12a0dSDavid Brownell };
3088ae12a0dSDavid Brownell 
3098ae12a0dSDavid Brownell static LIST_HEAD(board_list);
3102b9603a0SFeng Tang static LIST_HEAD(spi_master_list);
3112b9603a0SFeng Tang 
3122b9603a0SFeng Tang /*
3132b9603a0SFeng Tang  * Used to protect add/del opertion for board_info list and
3142b9603a0SFeng Tang  * spi_master list, and their matching process
3152b9603a0SFeng Tang  */
31694040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock);
3178ae12a0dSDavid Brownell 
318dc87c98eSGrant Likely /**
319dc87c98eSGrant Likely  * spi_alloc_device - Allocate a new SPI device
320dc87c98eSGrant Likely  * @master: Controller to which device is connected
321dc87c98eSGrant Likely  * Context: can sleep
322dc87c98eSGrant Likely  *
323dc87c98eSGrant Likely  * Allows a driver to allocate and initialize a spi_device without
324dc87c98eSGrant Likely  * registering it immediately.  This allows a driver to directly
325dc87c98eSGrant Likely  * fill the spi_device with device parameters before calling
326dc87c98eSGrant Likely  * spi_add_device() on it.
327dc87c98eSGrant Likely  *
328dc87c98eSGrant Likely  * Caller is responsible to call spi_add_device() on the returned
329dc87c98eSGrant Likely  * spi_device structure to add it to the SPI master.  If the caller
330dc87c98eSGrant Likely  * needs to discard the spi_device without adding it, then it should
331dc87c98eSGrant Likely  * call spi_dev_put() on it.
332dc87c98eSGrant Likely  *
333dc87c98eSGrant Likely  * Returns a pointer to the new device, or NULL.
334dc87c98eSGrant Likely  */
335dc87c98eSGrant Likely struct spi_device *spi_alloc_device(struct spi_master *master)
336dc87c98eSGrant Likely {
337dc87c98eSGrant Likely 	struct spi_device	*spi;
338dc87c98eSGrant Likely 	struct device		*dev = master->dev.parent;
339dc87c98eSGrant Likely 
340dc87c98eSGrant Likely 	if (!spi_master_get(master))
341dc87c98eSGrant Likely 		return NULL;
342dc87c98eSGrant Likely 
3435fe5f05eSJingoo Han 	spi = kzalloc(sizeof(*spi), GFP_KERNEL);
344dc87c98eSGrant Likely 	if (!spi) {
345dc87c98eSGrant Likely 		dev_err(dev, "cannot alloc spi_device\n");
346dc87c98eSGrant Likely 		spi_master_put(master);
347dc87c98eSGrant Likely 		return NULL;
348dc87c98eSGrant Likely 	}
349dc87c98eSGrant Likely 
350dc87c98eSGrant Likely 	spi->master = master;
351178db7d3SLaurent Pinchart 	spi->dev.parent = &master->dev;
352dc87c98eSGrant Likely 	spi->dev.bus = &spi_bus_type;
353dc87c98eSGrant Likely 	spi->dev.release = spidev_release;
354446411e1SAndreas Larsson 	spi->cs_gpio = -ENOENT;
355dc87c98eSGrant Likely 	device_initialize(&spi->dev);
356dc87c98eSGrant Likely 	return spi;
357dc87c98eSGrant Likely }
358dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device);
359dc87c98eSGrant Likely 
360e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi)
361e13ac47bSJarkko Nikula {
362e13ac47bSJarkko Nikula 	struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
363e13ac47bSJarkko Nikula 
364e13ac47bSJarkko Nikula 	if (adev) {
365e13ac47bSJarkko Nikula 		dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
366e13ac47bSJarkko Nikula 		return;
367e13ac47bSJarkko Nikula 	}
368e13ac47bSJarkko Nikula 
369e13ac47bSJarkko Nikula 	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
370e13ac47bSJarkko Nikula 		     spi->chip_select);
371e13ac47bSJarkko Nikula }
372e13ac47bSJarkko Nikula 
373*b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data)
374*b6fb8d3aSMika Westerberg {
375*b6fb8d3aSMika Westerberg 	struct spi_device *spi = to_spi_device(dev);
376*b6fb8d3aSMika Westerberg 	struct spi_device *new_spi = data;
377*b6fb8d3aSMika Westerberg 
378*b6fb8d3aSMika Westerberg 	if (spi->master == new_spi->master &&
379*b6fb8d3aSMika Westerberg 	    spi->chip_select == new_spi->chip_select)
380*b6fb8d3aSMika Westerberg 		return -EBUSY;
381*b6fb8d3aSMika Westerberg 	return 0;
382*b6fb8d3aSMika Westerberg }
383*b6fb8d3aSMika Westerberg 
384dc87c98eSGrant Likely /**
385dc87c98eSGrant Likely  * spi_add_device - Add spi_device allocated with spi_alloc_device
386dc87c98eSGrant Likely  * @spi: spi_device to register
387dc87c98eSGrant Likely  *
388dc87c98eSGrant Likely  * Companion function to spi_alloc_device.  Devices allocated with
389dc87c98eSGrant Likely  * spi_alloc_device can be added onto the spi bus with this function.
390dc87c98eSGrant Likely  *
391e48880e0SDavid Brownell  * Returns 0 on success; negative errno on failure
392dc87c98eSGrant Likely  */
393dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi)
394dc87c98eSGrant Likely {
395e48880e0SDavid Brownell 	static DEFINE_MUTEX(spi_add_lock);
39674317984SJean-Christophe PLAGNIOL-VILLARD 	struct spi_master *master = spi->master;
39774317984SJean-Christophe PLAGNIOL-VILLARD 	struct device *dev = master->dev.parent;
398dc87c98eSGrant Likely 	int status;
399dc87c98eSGrant Likely 
400dc87c98eSGrant Likely 	/* Chipselects are numbered 0..max; validate. */
40174317984SJean-Christophe PLAGNIOL-VILLARD 	if (spi->chip_select >= master->num_chipselect) {
402dc87c98eSGrant Likely 		dev_err(dev, "cs%d >= max %d\n",
403dc87c98eSGrant Likely 			spi->chip_select,
40474317984SJean-Christophe PLAGNIOL-VILLARD 			master->num_chipselect);
405dc87c98eSGrant Likely 		return -EINVAL;
406dc87c98eSGrant Likely 	}
407dc87c98eSGrant Likely 
408dc87c98eSGrant Likely 	/* Set the bus ID string */
409e13ac47bSJarkko Nikula 	spi_dev_set_name(spi);
410e48880e0SDavid Brownell 
411e48880e0SDavid Brownell 	/* We need to make sure there's no other device with this
412e48880e0SDavid Brownell 	 * chipselect **BEFORE** we call setup(), else we'll trash
413e48880e0SDavid Brownell 	 * its configuration.  Lock against concurrent add() calls.
414e48880e0SDavid Brownell 	 */
415e48880e0SDavid Brownell 	mutex_lock(&spi_add_lock);
416e48880e0SDavid Brownell 
417*b6fb8d3aSMika Westerberg 	status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
418*b6fb8d3aSMika Westerberg 	if (status) {
419e48880e0SDavid Brownell 		dev_err(dev, "chipselect %d already in use\n",
420e48880e0SDavid Brownell 				spi->chip_select);
421e48880e0SDavid Brownell 		goto done;
422e48880e0SDavid Brownell 	}
423e48880e0SDavid Brownell 
42474317984SJean-Christophe PLAGNIOL-VILLARD 	if (master->cs_gpios)
42574317984SJean-Christophe PLAGNIOL-VILLARD 		spi->cs_gpio = master->cs_gpios[spi->chip_select];
42674317984SJean-Christophe PLAGNIOL-VILLARD 
427e48880e0SDavid Brownell 	/* Drivers may modify this initial i/o setup, but will
428e48880e0SDavid Brownell 	 * normally rely on the device being setup.  Devices
429e48880e0SDavid Brownell 	 * using SPI_CS_HIGH can't coexist well otherwise...
430e48880e0SDavid Brownell 	 */
4317d077197SDavid Brownell 	status = spi_setup(spi);
432dc87c98eSGrant Likely 	if (status < 0) {
433eb288a1fSLinus Walleij 		dev_err(dev, "can't setup %s, status %d\n",
434eb288a1fSLinus Walleij 				dev_name(&spi->dev), status);
435e48880e0SDavid Brownell 		goto done;
436dc87c98eSGrant Likely 	}
437dc87c98eSGrant Likely 
438e48880e0SDavid Brownell 	/* Device may be bound to an active driver when this returns */
439dc87c98eSGrant Likely 	status = device_add(&spi->dev);
440e48880e0SDavid Brownell 	if (status < 0)
441eb288a1fSLinus Walleij 		dev_err(dev, "can't add %s, status %d\n",
442eb288a1fSLinus Walleij 				dev_name(&spi->dev), status);
443e48880e0SDavid Brownell 	else
44435f74fcaSKay Sievers 		dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
445e48880e0SDavid Brownell 
446e48880e0SDavid Brownell done:
447e48880e0SDavid Brownell 	mutex_unlock(&spi_add_lock);
448e48880e0SDavid Brownell 	return status;
449dc87c98eSGrant Likely }
450dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device);
4518ae12a0dSDavid Brownell 
45233e34dc6SDavid Brownell /**
45333e34dc6SDavid Brownell  * spi_new_device - instantiate one new SPI device
45433e34dc6SDavid Brownell  * @master: Controller to which device is connected
45533e34dc6SDavid Brownell  * @chip: Describes the SPI device
45633e34dc6SDavid Brownell  * Context: can sleep
45733e34dc6SDavid Brownell  *
45833e34dc6SDavid Brownell  * On typical mainboards, this is purely internal; and it's not needed
4598ae12a0dSDavid Brownell  * after board init creates the hard-wired devices.  Some development
4608ae12a0dSDavid Brownell  * platforms may not be able to use spi_register_board_info though, and
4618ae12a0dSDavid Brownell  * this is exported so that for example a USB or parport based adapter
4628ae12a0dSDavid Brownell  * driver could add devices (which it would learn about out-of-band).
463082c8cb4SDavid Brownell  *
464082c8cb4SDavid Brownell  * Returns the new device, or NULL.
4658ae12a0dSDavid Brownell  */
466e9d5a461SAdrian Bunk struct spi_device *spi_new_device(struct spi_master *master,
467e9d5a461SAdrian Bunk 				  struct spi_board_info *chip)
4688ae12a0dSDavid Brownell {
4698ae12a0dSDavid Brownell 	struct spi_device	*proxy;
4708ae12a0dSDavid Brownell 	int			status;
4718ae12a0dSDavid Brownell 
472082c8cb4SDavid Brownell 	/* NOTE:  caller did any chip->bus_num checks necessary.
473082c8cb4SDavid Brownell 	 *
474082c8cb4SDavid Brownell 	 * Also, unless we change the return value convention to use
475082c8cb4SDavid Brownell 	 * error-or-pointer (not NULL-or-pointer), troubleshootability
476082c8cb4SDavid Brownell 	 * suggests syslogged diagnostics are best here (ugh).
477082c8cb4SDavid Brownell 	 */
478082c8cb4SDavid Brownell 
479dc87c98eSGrant Likely 	proxy = spi_alloc_device(master);
480dc87c98eSGrant Likely 	if (!proxy)
4818ae12a0dSDavid Brownell 		return NULL;
4828ae12a0dSDavid Brownell 
483102eb975SGrant Likely 	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
484102eb975SGrant Likely 
4858ae12a0dSDavid Brownell 	proxy->chip_select = chip->chip_select;
4868ae12a0dSDavid Brownell 	proxy->max_speed_hz = chip->max_speed_hz;
487980a01c9SDavid Brownell 	proxy->mode = chip->mode;
4888ae12a0dSDavid Brownell 	proxy->irq = chip->irq;
489102eb975SGrant Likely 	strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
4908ae12a0dSDavid Brownell 	proxy->dev.platform_data = (void *) chip->platform_data;
4918ae12a0dSDavid Brownell 	proxy->controller_data = chip->controller_data;
4928ae12a0dSDavid Brownell 	proxy->controller_state = NULL;
4938ae12a0dSDavid Brownell 
494dc87c98eSGrant Likely 	status = spi_add_device(proxy);
4958ae12a0dSDavid Brownell 	if (status < 0) {
496dc87c98eSGrant Likely 		spi_dev_put(proxy);
4978ae12a0dSDavid Brownell 		return NULL;
4988ae12a0dSDavid Brownell 	}
499dc87c98eSGrant Likely 
500dc87c98eSGrant Likely 	return proxy;
501dc87c98eSGrant Likely }
5028ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device);
5038ae12a0dSDavid Brownell 
5042b9603a0SFeng Tang static void spi_match_master_to_boardinfo(struct spi_master *master,
5052b9603a0SFeng Tang 				struct spi_board_info *bi)
5062b9603a0SFeng Tang {
5072b9603a0SFeng Tang 	struct spi_device *dev;
5082b9603a0SFeng Tang 
5092b9603a0SFeng Tang 	if (master->bus_num != bi->bus_num)
5102b9603a0SFeng Tang 		return;
5112b9603a0SFeng Tang 
5122b9603a0SFeng Tang 	dev = spi_new_device(master, bi);
5132b9603a0SFeng Tang 	if (!dev)
5142b9603a0SFeng Tang 		dev_err(master->dev.parent, "can't create new device for %s\n",
5152b9603a0SFeng Tang 			bi->modalias);
5162b9603a0SFeng Tang }
5172b9603a0SFeng Tang 
51833e34dc6SDavid Brownell /**
51933e34dc6SDavid Brownell  * spi_register_board_info - register SPI devices for a given board
52033e34dc6SDavid Brownell  * @info: array of chip descriptors
52133e34dc6SDavid Brownell  * @n: how many descriptors are provided
52233e34dc6SDavid Brownell  * Context: can sleep
52333e34dc6SDavid Brownell  *
5248ae12a0dSDavid Brownell  * Board-specific early init code calls this (probably during arch_initcall)
5258ae12a0dSDavid Brownell  * with segments of the SPI device table.  Any device nodes are created later,
5268ae12a0dSDavid Brownell  * after the relevant parent SPI controller (bus_num) is defined.  We keep
5278ae12a0dSDavid Brownell  * this table of devices forever, so that reloading a controller driver will
5288ae12a0dSDavid Brownell  * not make Linux forget about these hard-wired devices.
5298ae12a0dSDavid Brownell  *
5308ae12a0dSDavid Brownell  * Other code can also call this, e.g. a particular add-on board might provide
5318ae12a0dSDavid Brownell  * SPI devices through its expansion connector, so code initializing that board
5328ae12a0dSDavid Brownell  * would naturally declare its SPI devices.
5338ae12a0dSDavid Brownell  *
5348ae12a0dSDavid Brownell  * The board info passed can safely be __initdata ... but be careful of
5358ae12a0dSDavid Brownell  * any embedded pointers (platform_data, etc), they're copied as-is.
5368ae12a0dSDavid Brownell  */
537fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n)
5388ae12a0dSDavid Brownell {
5398ae12a0dSDavid Brownell 	struct boardinfo *bi;
5402b9603a0SFeng Tang 	int i;
5418ae12a0dSDavid Brownell 
5422b9603a0SFeng Tang 	bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
5438ae12a0dSDavid Brownell 	if (!bi)
5448ae12a0dSDavid Brownell 		return -ENOMEM;
5458ae12a0dSDavid Brownell 
5462b9603a0SFeng Tang 	for (i = 0; i < n; i++, bi++, info++) {
5472b9603a0SFeng Tang 		struct spi_master *master;
5482b9603a0SFeng Tang 
5492b9603a0SFeng Tang 		memcpy(&bi->board_info, info, sizeof(*info));
55094040828SMatthias Kaehlcke 		mutex_lock(&board_lock);
5518ae12a0dSDavid Brownell 		list_add_tail(&bi->list, &board_list);
5522b9603a0SFeng Tang 		list_for_each_entry(master, &spi_master_list, list)
5532b9603a0SFeng Tang 			spi_match_master_to_boardinfo(master, &bi->board_info);
55494040828SMatthias Kaehlcke 		mutex_unlock(&board_lock);
5552b9603a0SFeng Tang 	}
5562b9603a0SFeng Tang 
5578ae12a0dSDavid Brownell 	return 0;
5588ae12a0dSDavid Brownell }
5598ae12a0dSDavid Brownell 
5608ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
5618ae12a0dSDavid Brownell 
562b158935fSMark Brown static void spi_set_cs(struct spi_device *spi, bool enable)
563b158935fSMark Brown {
564b158935fSMark Brown 	if (spi->mode & SPI_CS_HIGH)
565b158935fSMark Brown 		enable = !enable;
566b158935fSMark Brown 
567b158935fSMark Brown 	if (spi->cs_gpio >= 0)
568b158935fSMark Brown 		gpio_set_value(spi->cs_gpio, !enable);
569b158935fSMark Brown 	else if (spi->master->set_cs)
570b158935fSMark Brown 		spi->master->set_cs(spi, !enable);
571b158935fSMark Brown }
572b158935fSMark Brown 
573b158935fSMark Brown /*
574b158935fSMark Brown  * spi_transfer_one_message - Default implementation of transfer_one_message()
575b158935fSMark Brown  *
576b158935fSMark Brown  * This is a standard implementation of transfer_one_message() for
577b158935fSMark Brown  * drivers which impelment a transfer_one() operation.  It provides
578b158935fSMark Brown  * standard handling of delays and chip select management.
579b158935fSMark Brown  */
580b158935fSMark Brown static int spi_transfer_one_message(struct spi_master *master,
581b158935fSMark Brown 				    struct spi_message *msg)
582b158935fSMark Brown {
583b158935fSMark Brown 	struct spi_transfer *xfer;
584b158935fSMark Brown 	bool cur_cs = true;
585b158935fSMark Brown 	bool keep_cs = false;
586b158935fSMark Brown 	int ret = 0;
587b158935fSMark Brown 
588b158935fSMark Brown 	spi_set_cs(msg->spi, true);
589b158935fSMark Brown 
590b158935fSMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
591b158935fSMark Brown 		trace_spi_transfer_start(msg, xfer);
592b158935fSMark Brown 
59316735d02SWolfram Sang 		reinit_completion(&master->xfer_completion);
594b158935fSMark Brown 
595b158935fSMark Brown 		ret = master->transfer_one(master, msg->spi, xfer);
596b158935fSMark Brown 		if (ret < 0) {
597b158935fSMark Brown 			dev_err(&msg->spi->dev,
598b158935fSMark Brown 				"SPI transfer failed: %d\n", ret);
599b158935fSMark Brown 			goto out;
600b158935fSMark Brown 		}
601b158935fSMark Brown 
602b158935fSMark Brown 		if (ret > 0)
603b158935fSMark Brown 			wait_for_completion(&master->xfer_completion);
604b158935fSMark Brown 
605b158935fSMark Brown 		trace_spi_transfer_stop(msg, xfer);
606b158935fSMark Brown 
607b158935fSMark Brown 		if (msg->status != -EINPROGRESS)
608b158935fSMark Brown 			goto out;
609b158935fSMark Brown 
610b158935fSMark Brown 		if (xfer->delay_usecs)
611b158935fSMark Brown 			udelay(xfer->delay_usecs);
612b158935fSMark Brown 
613b158935fSMark Brown 		if (xfer->cs_change) {
614b158935fSMark Brown 			if (list_is_last(&xfer->transfer_list,
615b158935fSMark Brown 					 &msg->transfers)) {
616b158935fSMark Brown 				keep_cs = true;
617b158935fSMark Brown 			} else {
618b158935fSMark Brown 				cur_cs = !cur_cs;
619b158935fSMark Brown 				spi_set_cs(msg->spi, cur_cs);
620b158935fSMark Brown 			}
621b158935fSMark Brown 		}
622b158935fSMark Brown 
623b158935fSMark Brown 		msg->actual_length += xfer->len;
624b158935fSMark Brown 	}
625b158935fSMark Brown 
626b158935fSMark Brown out:
627b158935fSMark Brown 	if (ret != 0 || !keep_cs)
628b158935fSMark Brown 		spi_set_cs(msg->spi, false);
629b158935fSMark Brown 
630b158935fSMark Brown 	if (msg->status == -EINPROGRESS)
631b158935fSMark Brown 		msg->status = ret;
632b158935fSMark Brown 
633b158935fSMark Brown 	spi_finalize_current_message(master);
634b158935fSMark Brown 
635b158935fSMark Brown 	return ret;
636b158935fSMark Brown }
637b158935fSMark Brown 
638b158935fSMark Brown /**
639b158935fSMark Brown  * spi_finalize_current_transfer - report completion of a transfer
640b158935fSMark Brown  *
641b158935fSMark Brown  * Called by SPI drivers using the core transfer_one_message()
642b158935fSMark Brown  * implementation to notify it that the current interrupt driven
643b158935fSMark Brown  * transfer has finised and the next one may be scheduled.
644b158935fSMark Brown  */
645b158935fSMark Brown void spi_finalize_current_transfer(struct spi_master *master)
646b158935fSMark Brown {
647b158935fSMark Brown 	complete(&master->xfer_completion);
648b158935fSMark Brown }
649b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
650b158935fSMark Brown 
651ffbbdd21SLinus Walleij /**
652ffbbdd21SLinus Walleij  * spi_pump_messages - kthread work function which processes spi message queue
653ffbbdd21SLinus Walleij  * @work: pointer to kthread work struct contained in the master struct
654ffbbdd21SLinus Walleij  *
655ffbbdd21SLinus Walleij  * This function checks if there is any spi message in the queue that
656ffbbdd21SLinus Walleij  * needs processing and if so call out to the driver to initialize hardware
657ffbbdd21SLinus Walleij  * and transfer each message.
658ffbbdd21SLinus Walleij  *
659ffbbdd21SLinus Walleij  */
660ffbbdd21SLinus Walleij static void spi_pump_messages(struct kthread_work *work)
661ffbbdd21SLinus Walleij {
662ffbbdd21SLinus Walleij 	struct spi_master *master =
663ffbbdd21SLinus Walleij 		container_of(work, struct spi_master, pump_messages);
664ffbbdd21SLinus Walleij 	unsigned long flags;
665ffbbdd21SLinus Walleij 	bool was_busy = false;
666ffbbdd21SLinus Walleij 	int ret;
667ffbbdd21SLinus Walleij 
668ffbbdd21SLinus Walleij 	/* Lock queue and check for queue work */
669ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
670ffbbdd21SLinus Walleij 	if (list_empty(&master->queue) || !master->running) {
671b0b36b86SBryan Freed 		if (!master->busy) {
6729af4acc0SDan Carpenter 			spin_unlock_irqrestore(&master->queue_lock, flags);
673ffbbdd21SLinus Walleij 			return;
674ffbbdd21SLinus Walleij 		}
675ffbbdd21SLinus Walleij 		master->busy = false;
676ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
677b0b36b86SBryan Freed 		if (master->unprepare_transfer_hardware &&
678b0b36b86SBryan Freed 		    master->unprepare_transfer_hardware(master))
679b0b36b86SBryan Freed 			dev_err(&master->dev,
680b0b36b86SBryan Freed 				"failed to unprepare transfer hardware\n");
68149834de2SMark Brown 		if (master->auto_runtime_pm) {
68249834de2SMark Brown 			pm_runtime_mark_last_busy(master->dev.parent);
68349834de2SMark Brown 			pm_runtime_put_autosuspend(master->dev.parent);
68449834de2SMark Brown 		}
68556ec1978SMark Brown 		trace_spi_master_idle(master);
686ffbbdd21SLinus Walleij 		return;
687ffbbdd21SLinus Walleij 	}
688ffbbdd21SLinus Walleij 
689ffbbdd21SLinus Walleij 	/* Make sure we are not already running a message */
690ffbbdd21SLinus Walleij 	if (master->cur_msg) {
691ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
692ffbbdd21SLinus Walleij 		return;
693ffbbdd21SLinus Walleij 	}
694ffbbdd21SLinus Walleij 	/* Extract head of queue */
695ffbbdd21SLinus Walleij 	master->cur_msg =
696ffbbdd21SLinus Walleij 	    list_entry(master->queue.next, struct spi_message, queue);
697ffbbdd21SLinus Walleij 
698ffbbdd21SLinus Walleij 	list_del_init(&master->cur_msg->queue);
699ffbbdd21SLinus Walleij 	if (master->busy)
700ffbbdd21SLinus Walleij 		was_busy = true;
701ffbbdd21SLinus Walleij 	else
702ffbbdd21SLinus Walleij 		master->busy = true;
703ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
704ffbbdd21SLinus Walleij 
70549834de2SMark Brown 	if (!was_busy && master->auto_runtime_pm) {
70649834de2SMark Brown 		ret = pm_runtime_get_sync(master->dev.parent);
70749834de2SMark Brown 		if (ret < 0) {
70849834de2SMark Brown 			dev_err(&master->dev, "Failed to power device: %d\n",
70949834de2SMark Brown 				ret);
71049834de2SMark Brown 			return;
71149834de2SMark Brown 		}
71249834de2SMark Brown 	}
71349834de2SMark Brown 
71456ec1978SMark Brown 	if (!was_busy)
71556ec1978SMark Brown 		trace_spi_master_busy(master);
71656ec1978SMark Brown 
7177dfd2bd7SShubhrajyoti D 	if (!was_busy && master->prepare_transfer_hardware) {
718ffbbdd21SLinus Walleij 		ret = master->prepare_transfer_hardware(master);
719ffbbdd21SLinus Walleij 		if (ret) {
720ffbbdd21SLinus Walleij 			dev_err(&master->dev,
721ffbbdd21SLinus Walleij 				"failed to prepare transfer hardware\n");
72249834de2SMark Brown 
72349834de2SMark Brown 			if (master->auto_runtime_pm)
72449834de2SMark Brown 				pm_runtime_put(master->dev.parent);
725ffbbdd21SLinus Walleij 			return;
726ffbbdd21SLinus Walleij 		}
727ffbbdd21SLinus Walleij 	}
728ffbbdd21SLinus Walleij 
72956ec1978SMark Brown 	trace_spi_message_start(master->cur_msg);
73056ec1978SMark Brown 
7312841a5fcSMark Brown 	if (master->prepare_message) {
7322841a5fcSMark Brown 		ret = master->prepare_message(master, master->cur_msg);
7332841a5fcSMark Brown 		if (ret) {
7342841a5fcSMark Brown 			dev_err(&master->dev,
7352841a5fcSMark Brown 				"failed to prepare message: %d\n", ret);
7362841a5fcSMark Brown 			master->cur_msg->status = ret;
7372841a5fcSMark Brown 			spi_finalize_current_message(master);
7382841a5fcSMark Brown 			return;
7392841a5fcSMark Brown 		}
7402841a5fcSMark Brown 		master->cur_msg_prepared = true;
7412841a5fcSMark Brown 	}
7422841a5fcSMark Brown 
743ffbbdd21SLinus Walleij 	ret = master->transfer_one_message(master, master->cur_msg);
744ffbbdd21SLinus Walleij 	if (ret) {
745ffbbdd21SLinus Walleij 		dev_err(&master->dev,
746e120cc0dSDaniel Santos 			"failed to transfer one message from queue: %d\n", ret);
747e120cc0dSDaniel Santos 		master->cur_msg->status = ret;
748e120cc0dSDaniel Santos 		spi_finalize_current_message(master);
749ffbbdd21SLinus Walleij 		return;
750ffbbdd21SLinus Walleij 	}
751ffbbdd21SLinus Walleij }
752ffbbdd21SLinus Walleij 
753ffbbdd21SLinus Walleij static int spi_init_queue(struct spi_master *master)
754ffbbdd21SLinus Walleij {
755ffbbdd21SLinus Walleij 	struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
756ffbbdd21SLinus Walleij 
757ffbbdd21SLinus Walleij 	INIT_LIST_HEAD(&master->queue);
758ffbbdd21SLinus Walleij 	spin_lock_init(&master->queue_lock);
759ffbbdd21SLinus Walleij 
760ffbbdd21SLinus Walleij 	master->running = false;
761ffbbdd21SLinus Walleij 	master->busy = false;
762ffbbdd21SLinus Walleij 
763ffbbdd21SLinus Walleij 	init_kthread_worker(&master->kworker);
764ffbbdd21SLinus Walleij 	master->kworker_task = kthread_run(kthread_worker_fn,
765f170168bSKees Cook 					   &master->kworker, "%s",
766ffbbdd21SLinus Walleij 					   dev_name(&master->dev));
767ffbbdd21SLinus Walleij 	if (IS_ERR(master->kworker_task)) {
768ffbbdd21SLinus Walleij 		dev_err(&master->dev, "failed to create message pump task\n");
769ffbbdd21SLinus Walleij 		return -ENOMEM;
770ffbbdd21SLinus Walleij 	}
771ffbbdd21SLinus Walleij 	init_kthread_work(&master->pump_messages, spi_pump_messages);
772ffbbdd21SLinus Walleij 
773ffbbdd21SLinus Walleij 	/*
774ffbbdd21SLinus Walleij 	 * Master config will indicate if this controller should run the
775ffbbdd21SLinus Walleij 	 * message pump with high (realtime) priority to reduce the transfer
776ffbbdd21SLinus Walleij 	 * latency on the bus by minimising the delay between a transfer
777ffbbdd21SLinus Walleij 	 * request and the scheduling of the message pump thread. Without this
778ffbbdd21SLinus Walleij 	 * setting the message pump thread will remain at default priority.
779ffbbdd21SLinus Walleij 	 */
780ffbbdd21SLinus Walleij 	if (master->rt) {
781ffbbdd21SLinus Walleij 		dev_info(&master->dev,
782ffbbdd21SLinus Walleij 			"will run message pump with realtime priority\n");
783ffbbdd21SLinus Walleij 		sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
784ffbbdd21SLinus Walleij 	}
785ffbbdd21SLinus Walleij 
786ffbbdd21SLinus Walleij 	return 0;
787ffbbdd21SLinus Walleij }
788ffbbdd21SLinus Walleij 
789ffbbdd21SLinus Walleij /**
790ffbbdd21SLinus Walleij  * spi_get_next_queued_message() - called by driver to check for queued
791ffbbdd21SLinus Walleij  * messages
792ffbbdd21SLinus Walleij  * @master: the master to check for queued messages
793ffbbdd21SLinus Walleij  *
794ffbbdd21SLinus Walleij  * If there are more messages in the queue, the next message is returned from
795ffbbdd21SLinus Walleij  * this call.
796ffbbdd21SLinus Walleij  */
797ffbbdd21SLinus Walleij struct spi_message *spi_get_next_queued_message(struct spi_master *master)
798ffbbdd21SLinus Walleij {
799ffbbdd21SLinus Walleij 	struct spi_message *next;
800ffbbdd21SLinus Walleij 	unsigned long flags;
801ffbbdd21SLinus Walleij 
802ffbbdd21SLinus Walleij 	/* get a pointer to the next message, if any */
803ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
804ffbbdd21SLinus Walleij 	if (list_empty(&master->queue))
805ffbbdd21SLinus Walleij 		next = NULL;
806ffbbdd21SLinus Walleij 	else
807ffbbdd21SLinus Walleij 		next = list_entry(master->queue.next,
808ffbbdd21SLinus Walleij 				  struct spi_message, queue);
809ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
810ffbbdd21SLinus Walleij 
811ffbbdd21SLinus Walleij 	return next;
812ffbbdd21SLinus Walleij }
813ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
814ffbbdd21SLinus Walleij 
815ffbbdd21SLinus Walleij /**
816ffbbdd21SLinus Walleij  * spi_finalize_current_message() - the current message is complete
817ffbbdd21SLinus Walleij  * @master: the master to return the message to
818ffbbdd21SLinus Walleij  *
819ffbbdd21SLinus Walleij  * Called by the driver to notify the core that the message in the front of the
820ffbbdd21SLinus Walleij  * queue is complete and can be removed from the queue.
821ffbbdd21SLinus Walleij  */
822ffbbdd21SLinus Walleij void spi_finalize_current_message(struct spi_master *master)
823ffbbdd21SLinus Walleij {
824ffbbdd21SLinus Walleij 	struct spi_message *mesg;
825ffbbdd21SLinus Walleij 	unsigned long flags;
8262841a5fcSMark Brown 	int ret;
827ffbbdd21SLinus Walleij 
828ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
829ffbbdd21SLinus Walleij 	mesg = master->cur_msg;
830ffbbdd21SLinus Walleij 	master->cur_msg = NULL;
831ffbbdd21SLinus Walleij 
832ffbbdd21SLinus Walleij 	queue_kthread_work(&master->kworker, &master->pump_messages);
833ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
834ffbbdd21SLinus Walleij 
8352841a5fcSMark Brown 	if (master->cur_msg_prepared && master->unprepare_message) {
8362841a5fcSMark Brown 		ret = master->unprepare_message(master, mesg);
8372841a5fcSMark Brown 		if (ret) {
8382841a5fcSMark Brown 			dev_err(&master->dev,
8392841a5fcSMark Brown 				"failed to unprepare message: %d\n", ret);
8402841a5fcSMark Brown 		}
8412841a5fcSMark Brown 	}
8422841a5fcSMark Brown 	master->cur_msg_prepared = false;
8432841a5fcSMark Brown 
844ffbbdd21SLinus Walleij 	mesg->state = NULL;
845ffbbdd21SLinus Walleij 	if (mesg->complete)
846ffbbdd21SLinus Walleij 		mesg->complete(mesg->context);
84756ec1978SMark Brown 
84856ec1978SMark Brown 	trace_spi_message_done(mesg);
849ffbbdd21SLinus Walleij }
850ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message);
851ffbbdd21SLinus Walleij 
852ffbbdd21SLinus Walleij static int spi_start_queue(struct spi_master *master)
853ffbbdd21SLinus Walleij {
854ffbbdd21SLinus Walleij 	unsigned long flags;
855ffbbdd21SLinus Walleij 
856ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
857ffbbdd21SLinus Walleij 
858ffbbdd21SLinus Walleij 	if (master->running || master->busy) {
859ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
860ffbbdd21SLinus Walleij 		return -EBUSY;
861ffbbdd21SLinus Walleij 	}
862ffbbdd21SLinus Walleij 
863ffbbdd21SLinus Walleij 	master->running = true;
864ffbbdd21SLinus Walleij 	master->cur_msg = NULL;
865ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
866ffbbdd21SLinus Walleij 
867ffbbdd21SLinus Walleij 	queue_kthread_work(&master->kworker, &master->pump_messages);
868ffbbdd21SLinus Walleij 
869ffbbdd21SLinus Walleij 	return 0;
870ffbbdd21SLinus Walleij }
871ffbbdd21SLinus Walleij 
872ffbbdd21SLinus Walleij static int spi_stop_queue(struct spi_master *master)
873ffbbdd21SLinus Walleij {
874ffbbdd21SLinus Walleij 	unsigned long flags;
875ffbbdd21SLinus Walleij 	unsigned limit = 500;
876ffbbdd21SLinus Walleij 	int ret = 0;
877ffbbdd21SLinus Walleij 
878ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
879ffbbdd21SLinus Walleij 
880ffbbdd21SLinus Walleij 	/*
881ffbbdd21SLinus Walleij 	 * This is a bit lame, but is optimized for the common execution path.
882ffbbdd21SLinus Walleij 	 * A wait_queue on the master->busy could be used, but then the common
883ffbbdd21SLinus Walleij 	 * execution path (pump_messages) would be required to call wake_up or
884ffbbdd21SLinus Walleij 	 * friends on every SPI message. Do this instead.
885ffbbdd21SLinus Walleij 	 */
886ffbbdd21SLinus Walleij 	while ((!list_empty(&master->queue) || master->busy) && limit--) {
887ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
888ffbbdd21SLinus Walleij 		msleep(10);
889ffbbdd21SLinus Walleij 		spin_lock_irqsave(&master->queue_lock, flags);
890ffbbdd21SLinus Walleij 	}
891ffbbdd21SLinus Walleij 
892ffbbdd21SLinus Walleij 	if (!list_empty(&master->queue) || master->busy)
893ffbbdd21SLinus Walleij 		ret = -EBUSY;
894ffbbdd21SLinus Walleij 	else
895ffbbdd21SLinus Walleij 		master->running = false;
896ffbbdd21SLinus Walleij 
897ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
898ffbbdd21SLinus Walleij 
899ffbbdd21SLinus Walleij 	if (ret) {
900ffbbdd21SLinus Walleij 		dev_warn(&master->dev,
901ffbbdd21SLinus Walleij 			 "could not stop message queue\n");
902ffbbdd21SLinus Walleij 		return ret;
903ffbbdd21SLinus Walleij 	}
904ffbbdd21SLinus Walleij 	return ret;
905ffbbdd21SLinus Walleij }
906ffbbdd21SLinus Walleij 
907ffbbdd21SLinus Walleij static int spi_destroy_queue(struct spi_master *master)
908ffbbdd21SLinus Walleij {
909ffbbdd21SLinus Walleij 	int ret;
910ffbbdd21SLinus Walleij 
911ffbbdd21SLinus Walleij 	ret = spi_stop_queue(master);
912ffbbdd21SLinus Walleij 
913ffbbdd21SLinus Walleij 	/*
914ffbbdd21SLinus Walleij 	 * flush_kthread_worker will block until all work is done.
915ffbbdd21SLinus Walleij 	 * If the reason that stop_queue timed out is that the work will never
916ffbbdd21SLinus Walleij 	 * finish, then it does no good to call flush/stop thread, so
917ffbbdd21SLinus Walleij 	 * return anyway.
918ffbbdd21SLinus Walleij 	 */
919ffbbdd21SLinus Walleij 	if (ret) {
920ffbbdd21SLinus Walleij 		dev_err(&master->dev, "problem destroying queue\n");
921ffbbdd21SLinus Walleij 		return ret;
922ffbbdd21SLinus Walleij 	}
923ffbbdd21SLinus Walleij 
924ffbbdd21SLinus Walleij 	flush_kthread_worker(&master->kworker);
925ffbbdd21SLinus Walleij 	kthread_stop(master->kworker_task);
926ffbbdd21SLinus Walleij 
927ffbbdd21SLinus Walleij 	return 0;
928ffbbdd21SLinus Walleij }
929ffbbdd21SLinus Walleij 
930ffbbdd21SLinus Walleij /**
931ffbbdd21SLinus Walleij  * spi_queued_transfer - transfer function for queued transfers
932ffbbdd21SLinus Walleij  * @spi: spi device which is requesting transfer
933ffbbdd21SLinus Walleij  * @msg: spi message which is to handled is queued to driver queue
934ffbbdd21SLinus Walleij  */
935ffbbdd21SLinus Walleij static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
936ffbbdd21SLinus Walleij {
937ffbbdd21SLinus Walleij 	struct spi_master *master = spi->master;
938ffbbdd21SLinus Walleij 	unsigned long flags;
939ffbbdd21SLinus Walleij 
940ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
941ffbbdd21SLinus Walleij 
942ffbbdd21SLinus Walleij 	if (!master->running) {
943ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
944ffbbdd21SLinus Walleij 		return -ESHUTDOWN;
945ffbbdd21SLinus Walleij 	}
946ffbbdd21SLinus Walleij 	msg->actual_length = 0;
947ffbbdd21SLinus Walleij 	msg->status = -EINPROGRESS;
948ffbbdd21SLinus Walleij 
949ffbbdd21SLinus Walleij 	list_add_tail(&msg->queue, &master->queue);
95096b3eaceSAxel Lin 	if (!master->busy)
951ffbbdd21SLinus Walleij 		queue_kthread_work(&master->kworker, &master->pump_messages);
952ffbbdd21SLinus Walleij 
953ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
954ffbbdd21SLinus Walleij 	return 0;
955ffbbdd21SLinus Walleij }
956ffbbdd21SLinus Walleij 
957ffbbdd21SLinus Walleij static int spi_master_initialize_queue(struct spi_master *master)
958ffbbdd21SLinus Walleij {
959ffbbdd21SLinus Walleij 	int ret;
960ffbbdd21SLinus Walleij 
961ffbbdd21SLinus Walleij 	master->queued = true;
962ffbbdd21SLinus Walleij 	master->transfer = spi_queued_transfer;
963b158935fSMark Brown 	if (!master->transfer_one_message)
964b158935fSMark Brown 		master->transfer_one_message = spi_transfer_one_message;
965ffbbdd21SLinus Walleij 
966ffbbdd21SLinus Walleij 	/* Initialize and start queue */
967ffbbdd21SLinus Walleij 	ret = spi_init_queue(master);
968ffbbdd21SLinus Walleij 	if (ret) {
969ffbbdd21SLinus Walleij 		dev_err(&master->dev, "problem initializing queue\n");
970ffbbdd21SLinus Walleij 		goto err_init_queue;
971ffbbdd21SLinus Walleij 	}
972ffbbdd21SLinus Walleij 	ret = spi_start_queue(master);
973ffbbdd21SLinus Walleij 	if (ret) {
974ffbbdd21SLinus Walleij 		dev_err(&master->dev, "problem starting queue\n");
975ffbbdd21SLinus Walleij 		goto err_start_queue;
976ffbbdd21SLinus Walleij 	}
977ffbbdd21SLinus Walleij 
978ffbbdd21SLinus Walleij 	return 0;
979ffbbdd21SLinus Walleij 
980ffbbdd21SLinus Walleij err_start_queue:
981ffbbdd21SLinus Walleij err_init_queue:
982ffbbdd21SLinus Walleij 	spi_destroy_queue(master);
983ffbbdd21SLinus Walleij 	return ret;
984ffbbdd21SLinus Walleij }
985ffbbdd21SLinus Walleij 
986ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/
987ffbbdd21SLinus Walleij 
9887cb94361SAndreas Larsson #if defined(CONFIG_OF)
989d57a4282SGrant Likely /**
990d57a4282SGrant Likely  * of_register_spi_devices() - Register child devices onto the SPI bus
991d57a4282SGrant Likely  * @master:	Pointer to spi_master device
992d57a4282SGrant Likely  *
993d57a4282SGrant Likely  * Registers an spi_device for each child node of master node which has a 'reg'
994d57a4282SGrant Likely  * property.
995d57a4282SGrant Likely  */
996d57a4282SGrant Likely static void of_register_spi_devices(struct spi_master *master)
997d57a4282SGrant Likely {
998d57a4282SGrant Likely 	struct spi_device *spi;
999d57a4282SGrant Likely 	struct device_node *nc;
1000d57a4282SGrant Likely 	int rc;
100189da4293STrent Piepho 	u32 value;
1002d57a4282SGrant Likely 
1003d57a4282SGrant Likely 	if (!master->dev.of_node)
1004d57a4282SGrant Likely 		return;
1005d57a4282SGrant Likely 
1006f3b6159eSAlexander Sverdlin 	for_each_available_child_of_node(master->dev.of_node, nc) {
1007d57a4282SGrant Likely 		/* Alloc an spi_device */
1008d57a4282SGrant Likely 		spi = spi_alloc_device(master);
1009d57a4282SGrant Likely 		if (!spi) {
1010d57a4282SGrant Likely 			dev_err(&master->dev, "spi_device alloc error for %s\n",
1011d57a4282SGrant Likely 				nc->full_name);
1012d57a4282SGrant Likely 			spi_dev_put(spi);
1013d57a4282SGrant Likely 			continue;
1014d57a4282SGrant Likely 		}
1015d57a4282SGrant Likely 
1016d57a4282SGrant Likely 		/* Select device driver */
1017d57a4282SGrant Likely 		if (of_modalias_node(nc, spi->modalias,
1018d57a4282SGrant Likely 				     sizeof(spi->modalias)) < 0) {
1019d57a4282SGrant Likely 			dev_err(&master->dev, "cannot find modalias for %s\n",
1020d57a4282SGrant Likely 				nc->full_name);
1021d57a4282SGrant Likely 			spi_dev_put(spi);
1022d57a4282SGrant Likely 			continue;
1023d57a4282SGrant Likely 		}
1024d57a4282SGrant Likely 
1025d57a4282SGrant Likely 		/* Device address */
102689da4293STrent Piepho 		rc = of_property_read_u32(nc, "reg", &value);
102789da4293STrent Piepho 		if (rc) {
102889da4293STrent Piepho 			dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
102989da4293STrent Piepho 				nc->full_name, rc);
1030d57a4282SGrant Likely 			spi_dev_put(spi);
1031d57a4282SGrant Likely 			continue;
1032d57a4282SGrant Likely 		}
103389da4293STrent Piepho 		spi->chip_select = value;
1034d57a4282SGrant Likely 
1035d57a4282SGrant Likely 		/* Mode (clock phase/polarity/etc.) */
1036d57a4282SGrant Likely 		if (of_find_property(nc, "spi-cpha", NULL))
1037d57a4282SGrant Likely 			spi->mode |= SPI_CPHA;
1038d57a4282SGrant Likely 		if (of_find_property(nc, "spi-cpol", NULL))
1039d57a4282SGrant Likely 			spi->mode |= SPI_CPOL;
1040d57a4282SGrant Likely 		if (of_find_property(nc, "spi-cs-high", NULL))
1041d57a4282SGrant Likely 			spi->mode |= SPI_CS_HIGH;
1042c20151dfSLars-Peter Clausen 		if (of_find_property(nc, "spi-3wire", NULL))
1043c20151dfSLars-Peter Clausen 			spi->mode |= SPI_3WIRE;
1044d57a4282SGrant Likely 
1045f477b7fbSwangyuhang 		/* Device DUAL/QUAD mode */
104689da4293STrent Piepho 		if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
104789da4293STrent Piepho 			switch (value) {
104889da4293STrent Piepho 			case 1:
1049f477b7fbSwangyuhang 				break;
105089da4293STrent Piepho 			case 2:
1051f477b7fbSwangyuhang 				spi->mode |= SPI_TX_DUAL;
1052f477b7fbSwangyuhang 				break;
105389da4293STrent Piepho 			case 4:
1054f477b7fbSwangyuhang 				spi->mode |= SPI_TX_QUAD;
1055f477b7fbSwangyuhang 				break;
1056f477b7fbSwangyuhang 			default:
1057a822e99cSMark Brown 				dev_err(&master->dev,
1058a110f93dSwangyuhang 					"spi-tx-bus-width %d not supported\n",
105989da4293STrent Piepho 					value);
1060f477b7fbSwangyuhang 				spi_dev_put(spi);
1061f477b7fbSwangyuhang 				continue;
1062f477b7fbSwangyuhang 			}
1063a822e99cSMark Brown 		}
1064f477b7fbSwangyuhang 
106589da4293STrent Piepho 		if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
106689da4293STrent Piepho 			switch (value) {
106789da4293STrent Piepho 			case 1:
1068f477b7fbSwangyuhang 				break;
106989da4293STrent Piepho 			case 2:
1070f477b7fbSwangyuhang 				spi->mode |= SPI_RX_DUAL;
1071f477b7fbSwangyuhang 				break;
107289da4293STrent Piepho 			case 4:
1073f477b7fbSwangyuhang 				spi->mode |= SPI_RX_QUAD;
1074f477b7fbSwangyuhang 				break;
1075f477b7fbSwangyuhang 			default:
1076a822e99cSMark Brown 				dev_err(&master->dev,
1077a110f93dSwangyuhang 					"spi-rx-bus-width %d not supported\n",
107889da4293STrent Piepho 					value);
1079f477b7fbSwangyuhang 				spi_dev_put(spi);
1080f477b7fbSwangyuhang 				continue;
1081f477b7fbSwangyuhang 			}
1082a822e99cSMark Brown 		}
1083f477b7fbSwangyuhang 
1084d57a4282SGrant Likely 		/* Device speed */
108589da4293STrent Piepho 		rc = of_property_read_u32(nc, "spi-max-frequency", &value);
108689da4293STrent Piepho 		if (rc) {
108789da4293STrent Piepho 			dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
108889da4293STrent Piepho 				nc->full_name, rc);
1089d57a4282SGrant Likely 			spi_dev_put(spi);
1090d57a4282SGrant Likely 			continue;
1091d57a4282SGrant Likely 		}
109289da4293STrent Piepho 		spi->max_speed_hz = value;
1093d57a4282SGrant Likely 
1094d57a4282SGrant Likely 		/* IRQ */
1095d57a4282SGrant Likely 		spi->irq = irq_of_parse_and_map(nc, 0);
1096d57a4282SGrant Likely 
1097d57a4282SGrant Likely 		/* Store a pointer to the node in the device structure */
1098d57a4282SGrant Likely 		of_node_get(nc);
1099d57a4282SGrant Likely 		spi->dev.of_node = nc;
1100d57a4282SGrant Likely 
1101d57a4282SGrant Likely 		/* Register the new device */
110270fac17cSMathias Krause 		request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
1103d57a4282SGrant Likely 		rc = spi_add_device(spi);
1104d57a4282SGrant Likely 		if (rc) {
1105d57a4282SGrant Likely 			dev_err(&master->dev, "spi_device register error %s\n",
1106d57a4282SGrant Likely 				nc->full_name);
1107d57a4282SGrant Likely 			spi_dev_put(spi);
1108d57a4282SGrant Likely 		}
1109d57a4282SGrant Likely 
1110d57a4282SGrant Likely 	}
1111d57a4282SGrant Likely }
1112d57a4282SGrant Likely #else
1113d57a4282SGrant Likely static void of_register_spi_devices(struct spi_master *master) { }
1114d57a4282SGrant Likely #endif
1115d57a4282SGrant Likely 
111664bee4d2SMika Westerberg #ifdef CONFIG_ACPI
111764bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
111864bee4d2SMika Westerberg {
111964bee4d2SMika Westerberg 	struct spi_device *spi = data;
112064bee4d2SMika Westerberg 
112164bee4d2SMika Westerberg 	if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
112264bee4d2SMika Westerberg 		struct acpi_resource_spi_serialbus *sb;
112364bee4d2SMika Westerberg 
112464bee4d2SMika Westerberg 		sb = &ares->data.spi_serial_bus;
112564bee4d2SMika Westerberg 		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
112664bee4d2SMika Westerberg 			spi->chip_select = sb->device_selection;
112764bee4d2SMika Westerberg 			spi->max_speed_hz = sb->connection_speed;
112864bee4d2SMika Westerberg 
112964bee4d2SMika Westerberg 			if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
113064bee4d2SMika Westerberg 				spi->mode |= SPI_CPHA;
113164bee4d2SMika Westerberg 			if (sb->clock_polarity == ACPI_SPI_START_HIGH)
113264bee4d2SMika Westerberg 				spi->mode |= SPI_CPOL;
113364bee4d2SMika Westerberg 			if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
113464bee4d2SMika Westerberg 				spi->mode |= SPI_CS_HIGH;
113564bee4d2SMika Westerberg 		}
113664bee4d2SMika Westerberg 	} else if (spi->irq < 0) {
113764bee4d2SMika Westerberg 		struct resource r;
113864bee4d2SMika Westerberg 
113964bee4d2SMika Westerberg 		if (acpi_dev_resource_interrupt(ares, 0, &r))
114064bee4d2SMika Westerberg 			spi->irq = r.start;
114164bee4d2SMika Westerberg 	}
114264bee4d2SMika Westerberg 
114364bee4d2SMika Westerberg 	/* Always tell the ACPI core to skip this resource */
114464bee4d2SMika Westerberg 	return 1;
114564bee4d2SMika Westerberg }
114664bee4d2SMika Westerberg 
114764bee4d2SMika Westerberg static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
114864bee4d2SMika Westerberg 				       void *data, void **return_value)
114964bee4d2SMika Westerberg {
115064bee4d2SMika Westerberg 	struct spi_master *master = data;
115164bee4d2SMika Westerberg 	struct list_head resource_list;
115264bee4d2SMika Westerberg 	struct acpi_device *adev;
115364bee4d2SMika Westerberg 	struct spi_device *spi;
115464bee4d2SMika Westerberg 	int ret;
115564bee4d2SMika Westerberg 
115664bee4d2SMika Westerberg 	if (acpi_bus_get_device(handle, &adev))
115764bee4d2SMika Westerberg 		return AE_OK;
115864bee4d2SMika Westerberg 	if (acpi_bus_get_status(adev) || !adev->status.present)
115964bee4d2SMika Westerberg 		return AE_OK;
116064bee4d2SMika Westerberg 
116164bee4d2SMika Westerberg 	spi = spi_alloc_device(master);
116264bee4d2SMika Westerberg 	if (!spi) {
116364bee4d2SMika Westerberg 		dev_err(&master->dev, "failed to allocate SPI device for %s\n",
116464bee4d2SMika Westerberg 			dev_name(&adev->dev));
116564bee4d2SMika Westerberg 		return AE_NO_MEMORY;
116664bee4d2SMika Westerberg 	}
116764bee4d2SMika Westerberg 
11687b199811SRafael J. Wysocki 	ACPI_COMPANION_SET(&spi->dev, adev);
116964bee4d2SMika Westerberg 	spi->irq = -1;
117064bee4d2SMika Westerberg 
117164bee4d2SMika Westerberg 	INIT_LIST_HEAD(&resource_list);
117264bee4d2SMika Westerberg 	ret = acpi_dev_get_resources(adev, &resource_list,
117364bee4d2SMika Westerberg 				     acpi_spi_add_resource, spi);
117464bee4d2SMika Westerberg 	acpi_dev_free_resource_list(&resource_list);
117564bee4d2SMika Westerberg 
117664bee4d2SMika Westerberg 	if (ret < 0 || !spi->max_speed_hz) {
117764bee4d2SMika Westerberg 		spi_dev_put(spi);
117864bee4d2SMika Westerberg 		return AE_OK;
117964bee4d2SMika Westerberg 	}
118064bee4d2SMika Westerberg 
118133cf00e5SMika Westerberg 	adev->power.flags.ignore_parent = true;
1182cf9eb39cSJarkko Nikula 	strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
118364bee4d2SMika Westerberg 	if (spi_add_device(spi)) {
118433cf00e5SMika Westerberg 		adev->power.flags.ignore_parent = false;
118564bee4d2SMika Westerberg 		dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
118664bee4d2SMika Westerberg 			dev_name(&adev->dev));
118764bee4d2SMika Westerberg 		spi_dev_put(spi);
118864bee4d2SMika Westerberg 	}
118964bee4d2SMika Westerberg 
119064bee4d2SMika Westerberg 	return AE_OK;
119164bee4d2SMika Westerberg }
119264bee4d2SMika Westerberg 
119364bee4d2SMika Westerberg static void acpi_register_spi_devices(struct spi_master *master)
119464bee4d2SMika Westerberg {
119564bee4d2SMika Westerberg 	acpi_status status;
119664bee4d2SMika Westerberg 	acpi_handle handle;
119764bee4d2SMika Westerberg 
119829896178SRafael J. Wysocki 	handle = ACPI_HANDLE(master->dev.parent);
119964bee4d2SMika Westerberg 	if (!handle)
120064bee4d2SMika Westerberg 		return;
120164bee4d2SMika Westerberg 
120264bee4d2SMika Westerberg 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
120364bee4d2SMika Westerberg 				     acpi_spi_add_device, NULL,
120464bee4d2SMika Westerberg 				     master, NULL);
120564bee4d2SMika Westerberg 	if (ACPI_FAILURE(status))
120664bee4d2SMika Westerberg 		dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
120764bee4d2SMika Westerberg }
120864bee4d2SMika Westerberg #else
120964bee4d2SMika Westerberg static inline void acpi_register_spi_devices(struct spi_master *master) {}
121064bee4d2SMika Westerberg #endif /* CONFIG_ACPI */
121164bee4d2SMika Westerberg 
121249dce689STony Jones static void spi_master_release(struct device *dev)
12138ae12a0dSDavid Brownell {
12148ae12a0dSDavid Brownell 	struct spi_master *master;
12158ae12a0dSDavid Brownell 
121649dce689STony Jones 	master = container_of(dev, struct spi_master, dev);
12178ae12a0dSDavid Brownell 	kfree(master);
12188ae12a0dSDavid Brownell }
12198ae12a0dSDavid Brownell 
12208ae12a0dSDavid Brownell static struct class spi_master_class = {
12218ae12a0dSDavid Brownell 	.name		= "spi_master",
12228ae12a0dSDavid Brownell 	.owner		= THIS_MODULE,
122349dce689STony Jones 	.dev_release	= spi_master_release,
12248ae12a0dSDavid Brownell };
12258ae12a0dSDavid Brownell 
12268ae12a0dSDavid Brownell 
1227ffbbdd21SLinus Walleij 
12288ae12a0dSDavid Brownell /**
12298ae12a0dSDavid Brownell  * spi_alloc_master - allocate SPI master controller
12308ae12a0dSDavid Brownell  * @dev: the controller, possibly using the platform_bus
123133e34dc6SDavid Brownell  * @size: how much zeroed driver-private data to allocate; the pointer to this
123249dce689STony Jones  *	memory is in the driver_data field of the returned device,
12330c868461SDavid Brownell  *	accessible with spi_master_get_devdata().
123433e34dc6SDavid Brownell  * Context: can sleep
12358ae12a0dSDavid Brownell  *
12368ae12a0dSDavid Brownell  * This call is used only by SPI master controller drivers, which are the
12378ae12a0dSDavid Brownell  * only ones directly touching chip registers.  It's how they allocate
1238ba1a0513Sdmitry pervushin  * an spi_master structure, prior to calling spi_register_master().
12398ae12a0dSDavid Brownell  *
12408ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns the SPI
12418ae12a0dSDavid Brownell  * master structure on success, else NULL.
12428ae12a0dSDavid Brownell  *
12438ae12a0dSDavid Brownell  * The caller is responsible for assigning the bus number and initializing
1244ba1a0513Sdmitry pervushin  * the master's methods before calling spi_register_master(); and (after errors
1245eb4af0f5SUwe Kleine-König  * adding the device) calling spi_master_put() and kfree() to prevent a memory
1246eb4af0f5SUwe Kleine-König  * leak.
12478ae12a0dSDavid Brownell  */
1248e9d5a461SAdrian Bunk struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
12498ae12a0dSDavid Brownell {
12508ae12a0dSDavid Brownell 	struct spi_master	*master;
12518ae12a0dSDavid Brownell 
12520c868461SDavid Brownell 	if (!dev)
12530c868461SDavid Brownell 		return NULL;
12540c868461SDavid Brownell 
12555fe5f05eSJingoo Han 	master = kzalloc(size + sizeof(*master), GFP_KERNEL);
12568ae12a0dSDavid Brownell 	if (!master)
12578ae12a0dSDavid Brownell 		return NULL;
12588ae12a0dSDavid Brownell 
125949dce689STony Jones 	device_initialize(&master->dev);
12601e8a52e1SGrant Likely 	master->bus_num = -1;
12611e8a52e1SGrant Likely 	master->num_chipselect = 1;
126249dce689STony Jones 	master->dev.class = &spi_master_class;
126349dce689STony Jones 	master->dev.parent = get_device(dev);
12640c868461SDavid Brownell 	spi_master_set_devdata(master, &master[1]);
12658ae12a0dSDavid Brownell 
12668ae12a0dSDavid Brownell 	return master;
12678ae12a0dSDavid Brownell }
12688ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_alloc_master);
12698ae12a0dSDavid Brownell 
127074317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF
127174317984SJean-Christophe PLAGNIOL-VILLARD static int of_spi_register_master(struct spi_master *master)
127274317984SJean-Christophe PLAGNIOL-VILLARD {
1273e80beb27SGrant Likely 	int nb, i, *cs;
127474317984SJean-Christophe PLAGNIOL-VILLARD 	struct device_node *np = master->dev.of_node;
127574317984SJean-Christophe PLAGNIOL-VILLARD 
127674317984SJean-Christophe PLAGNIOL-VILLARD 	if (!np)
127774317984SJean-Christophe PLAGNIOL-VILLARD 		return 0;
127874317984SJean-Christophe PLAGNIOL-VILLARD 
127974317984SJean-Christophe PLAGNIOL-VILLARD 	nb = of_gpio_named_count(np, "cs-gpios");
12805fe5f05eSJingoo Han 	master->num_chipselect = max_t(int, nb, master->num_chipselect);
128174317984SJean-Christophe PLAGNIOL-VILLARD 
12828ec5d84eSAndreas Larsson 	/* Return error only for an incorrectly formed cs-gpios property */
12838ec5d84eSAndreas Larsson 	if (nb == 0 || nb == -ENOENT)
128474317984SJean-Christophe PLAGNIOL-VILLARD 		return 0;
12858ec5d84eSAndreas Larsson 	else if (nb < 0)
12868ec5d84eSAndreas Larsson 		return nb;
128774317984SJean-Christophe PLAGNIOL-VILLARD 
128874317984SJean-Christophe PLAGNIOL-VILLARD 	cs = devm_kzalloc(&master->dev,
128974317984SJean-Christophe PLAGNIOL-VILLARD 			  sizeof(int) * master->num_chipselect,
129074317984SJean-Christophe PLAGNIOL-VILLARD 			  GFP_KERNEL);
129174317984SJean-Christophe PLAGNIOL-VILLARD 	master->cs_gpios = cs;
129274317984SJean-Christophe PLAGNIOL-VILLARD 
129374317984SJean-Christophe PLAGNIOL-VILLARD 	if (!master->cs_gpios)
129474317984SJean-Christophe PLAGNIOL-VILLARD 		return -ENOMEM;
129574317984SJean-Christophe PLAGNIOL-VILLARD 
12960da83bb1SAndreas Larsson 	for (i = 0; i < master->num_chipselect; i++)
1297446411e1SAndreas Larsson 		cs[i] = -ENOENT;
129874317984SJean-Christophe PLAGNIOL-VILLARD 
129974317984SJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < nb; i++)
130074317984SJean-Christophe PLAGNIOL-VILLARD 		cs[i] = of_get_named_gpio(np, "cs-gpios", i);
130174317984SJean-Christophe PLAGNIOL-VILLARD 
130274317984SJean-Christophe PLAGNIOL-VILLARD 	return 0;
130374317984SJean-Christophe PLAGNIOL-VILLARD }
130474317984SJean-Christophe PLAGNIOL-VILLARD #else
130574317984SJean-Christophe PLAGNIOL-VILLARD static int of_spi_register_master(struct spi_master *master)
130674317984SJean-Christophe PLAGNIOL-VILLARD {
130774317984SJean-Christophe PLAGNIOL-VILLARD 	return 0;
130874317984SJean-Christophe PLAGNIOL-VILLARD }
130974317984SJean-Christophe PLAGNIOL-VILLARD #endif
131074317984SJean-Christophe PLAGNIOL-VILLARD 
13118ae12a0dSDavid Brownell /**
13128ae12a0dSDavid Brownell  * spi_register_master - register SPI master controller
13138ae12a0dSDavid Brownell  * @master: initialized master, originally from spi_alloc_master()
131433e34dc6SDavid Brownell  * Context: can sleep
13158ae12a0dSDavid Brownell  *
13168ae12a0dSDavid Brownell  * SPI master controllers connect to their drivers using some non-SPI bus,
13178ae12a0dSDavid Brownell  * such as the platform bus.  The final stage of probe() in that code
13188ae12a0dSDavid Brownell  * includes calling spi_register_master() to hook up to this SPI bus glue.
13198ae12a0dSDavid Brownell  *
13208ae12a0dSDavid Brownell  * SPI controllers use board specific (often SOC specific) bus numbers,
13218ae12a0dSDavid Brownell  * and board-specific addressing for SPI devices combines those numbers
13228ae12a0dSDavid Brownell  * with chip select numbers.  Since SPI does not directly support dynamic
13238ae12a0dSDavid Brownell  * device identification, boards need configuration tables telling which
13248ae12a0dSDavid Brownell  * chip is at which address.
13258ae12a0dSDavid Brownell  *
13268ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns zero on
13278ae12a0dSDavid Brownell  * success, else a negative error code (dropping the master's refcount).
13280c868461SDavid Brownell  * After a successful return, the caller is responsible for calling
13290c868461SDavid Brownell  * spi_unregister_master().
13308ae12a0dSDavid Brownell  */
1331e9d5a461SAdrian Bunk int spi_register_master(struct spi_master *master)
13328ae12a0dSDavid Brownell {
1333e44a45aeSDavid Brownell 	static atomic_t		dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
133449dce689STony Jones 	struct device		*dev = master->dev.parent;
13352b9603a0SFeng Tang 	struct boardinfo	*bi;
13368ae12a0dSDavid Brownell 	int			status = -ENODEV;
13378ae12a0dSDavid Brownell 	int			dynamic = 0;
13388ae12a0dSDavid Brownell 
13390c868461SDavid Brownell 	if (!dev)
13400c868461SDavid Brownell 		return -ENODEV;
13410c868461SDavid Brownell 
134274317984SJean-Christophe PLAGNIOL-VILLARD 	status = of_spi_register_master(master);
134374317984SJean-Christophe PLAGNIOL-VILLARD 	if (status)
134474317984SJean-Christophe PLAGNIOL-VILLARD 		return status;
134574317984SJean-Christophe PLAGNIOL-VILLARD 
1346082c8cb4SDavid Brownell 	/* even if it's just one always-selected device, there must
1347082c8cb4SDavid Brownell 	 * be at least one chipselect
1348082c8cb4SDavid Brownell 	 */
1349082c8cb4SDavid Brownell 	if (master->num_chipselect == 0)
1350082c8cb4SDavid Brownell 		return -EINVAL;
1351082c8cb4SDavid Brownell 
1352bb29785eSGrant Likely 	if ((master->bus_num < 0) && master->dev.of_node)
1353bb29785eSGrant Likely 		master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1354bb29785eSGrant Likely 
13558ae12a0dSDavid Brownell 	/* convention:  dynamically assigned bus IDs count down from the max */
1356a020ed75SDavid Brownell 	if (master->bus_num < 0) {
1357082c8cb4SDavid Brownell 		/* FIXME switch to an IDR based scheme, something like
1358082c8cb4SDavid Brownell 		 * I2C now uses, so we can't run out of "dynamic" IDs
1359082c8cb4SDavid Brownell 		 */
13608ae12a0dSDavid Brownell 		master->bus_num = atomic_dec_return(&dyn_bus_id);
1361b885244eSDavid Brownell 		dynamic = 1;
13628ae12a0dSDavid Brownell 	}
13638ae12a0dSDavid Brownell 
1364cf32b71eSErnst Schwab 	spin_lock_init(&master->bus_lock_spinlock);
1365cf32b71eSErnst Schwab 	mutex_init(&master->bus_lock_mutex);
1366cf32b71eSErnst Schwab 	master->bus_lock_flag = 0;
1367b158935fSMark Brown 	init_completion(&master->xfer_completion);
1368cf32b71eSErnst Schwab 
13698ae12a0dSDavid Brownell 	/* register the device, then userspace will see it.
13708ae12a0dSDavid Brownell 	 * registration fails if the bus ID is in use.
13718ae12a0dSDavid Brownell 	 */
137235f74fcaSKay Sievers 	dev_set_name(&master->dev, "spi%u", master->bus_num);
137349dce689STony Jones 	status = device_add(&master->dev);
1374b885244eSDavid Brownell 	if (status < 0)
13758ae12a0dSDavid Brownell 		goto done;
137635f74fcaSKay Sievers 	dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
13778ae12a0dSDavid Brownell 			dynamic ? " (dynamic)" : "");
13788ae12a0dSDavid Brownell 
1379ffbbdd21SLinus Walleij 	/* If we're using a queued driver, start the queue */
1380ffbbdd21SLinus Walleij 	if (master->transfer)
1381ffbbdd21SLinus Walleij 		dev_info(dev, "master is unqueued, this is deprecated\n");
1382ffbbdd21SLinus Walleij 	else {
1383ffbbdd21SLinus Walleij 		status = spi_master_initialize_queue(master);
1384ffbbdd21SLinus Walleij 		if (status) {
1385e93b0724SAxel Lin 			device_del(&master->dev);
1386ffbbdd21SLinus Walleij 			goto done;
1387ffbbdd21SLinus Walleij 		}
1388ffbbdd21SLinus Walleij 	}
1389ffbbdd21SLinus Walleij 
13902b9603a0SFeng Tang 	mutex_lock(&board_lock);
13912b9603a0SFeng Tang 	list_add_tail(&master->list, &spi_master_list);
13922b9603a0SFeng Tang 	list_for_each_entry(bi, &board_list, list)
13932b9603a0SFeng Tang 		spi_match_master_to_boardinfo(master, &bi->board_info);
13942b9603a0SFeng Tang 	mutex_unlock(&board_lock);
13952b9603a0SFeng Tang 
139664bee4d2SMika Westerberg 	/* Register devices from the device tree and ACPI */
139712b15e83SAnatolij Gustschin 	of_register_spi_devices(master);
139864bee4d2SMika Westerberg 	acpi_register_spi_devices(master);
13998ae12a0dSDavid Brownell done:
14008ae12a0dSDavid Brownell 	return status;
14018ae12a0dSDavid Brownell }
14028ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_master);
14038ae12a0dSDavid Brownell 
1404666d5b4cSMark Brown static void devm_spi_unregister(struct device *dev, void *res)
1405666d5b4cSMark Brown {
1406666d5b4cSMark Brown 	spi_unregister_master(*(struct spi_master **)res);
1407666d5b4cSMark Brown }
1408666d5b4cSMark Brown 
1409666d5b4cSMark Brown /**
1410666d5b4cSMark Brown  * dev_spi_register_master - register managed SPI master controller
1411666d5b4cSMark Brown  * @dev:    device managing SPI master
1412666d5b4cSMark Brown  * @master: initialized master, originally from spi_alloc_master()
1413666d5b4cSMark Brown  * Context: can sleep
1414666d5b4cSMark Brown  *
1415666d5b4cSMark Brown  * Register a SPI device as with spi_register_master() which will
1416666d5b4cSMark Brown  * automatically be unregister
1417666d5b4cSMark Brown  */
1418666d5b4cSMark Brown int devm_spi_register_master(struct device *dev, struct spi_master *master)
1419666d5b4cSMark Brown {
1420666d5b4cSMark Brown 	struct spi_master **ptr;
1421666d5b4cSMark Brown 	int ret;
1422666d5b4cSMark Brown 
1423666d5b4cSMark Brown 	ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1424666d5b4cSMark Brown 	if (!ptr)
1425666d5b4cSMark Brown 		return -ENOMEM;
1426666d5b4cSMark Brown 
1427666d5b4cSMark Brown 	ret = spi_register_master(master);
1428666d5b4cSMark Brown 	if (ret != 0) {
1429666d5b4cSMark Brown 		*ptr = master;
1430666d5b4cSMark Brown 		devres_add(dev, ptr);
1431666d5b4cSMark Brown 	} else {
1432666d5b4cSMark Brown 		devres_free(ptr);
1433666d5b4cSMark Brown 	}
1434666d5b4cSMark Brown 
1435666d5b4cSMark Brown 	return ret;
1436666d5b4cSMark Brown }
1437666d5b4cSMark Brown EXPORT_SYMBOL_GPL(devm_spi_register_master);
1438666d5b4cSMark Brown 
143934860089SDavid Lamparter static int __unregister(struct device *dev, void *null)
14408ae12a0dSDavid Brownell {
14410c868461SDavid Brownell 	spi_unregister_device(to_spi_device(dev));
14428ae12a0dSDavid Brownell 	return 0;
14438ae12a0dSDavid Brownell }
14448ae12a0dSDavid Brownell 
14458ae12a0dSDavid Brownell /**
14468ae12a0dSDavid Brownell  * spi_unregister_master - unregister SPI master controller
14478ae12a0dSDavid Brownell  * @master: the master being unregistered
144833e34dc6SDavid Brownell  * Context: can sleep
14498ae12a0dSDavid Brownell  *
14508ae12a0dSDavid Brownell  * This call is used only by SPI master controller drivers, which are the
14518ae12a0dSDavid Brownell  * only ones directly touching chip registers.
14528ae12a0dSDavid Brownell  *
14538ae12a0dSDavid Brownell  * This must be called from context that can sleep.
14548ae12a0dSDavid Brownell  */
14558ae12a0dSDavid Brownell void spi_unregister_master(struct spi_master *master)
14568ae12a0dSDavid Brownell {
145789fc9a1aSJeff Garzik 	int dummy;
145889fc9a1aSJeff Garzik 
1459ffbbdd21SLinus Walleij 	if (master->queued) {
1460ffbbdd21SLinus Walleij 		if (spi_destroy_queue(master))
1461ffbbdd21SLinus Walleij 			dev_err(&master->dev, "queue remove failed\n");
1462ffbbdd21SLinus Walleij 	}
1463ffbbdd21SLinus Walleij 
14642b9603a0SFeng Tang 	mutex_lock(&board_lock);
14652b9603a0SFeng Tang 	list_del(&master->list);
14662b9603a0SFeng Tang 	mutex_unlock(&board_lock);
14672b9603a0SFeng Tang 
146897dbf37dSSebastian Andrzej Siewior 	dummy = device_for_each_child(&master->dev, NULL, __unregister);
146949dce689STony Jones 	device_unregister(&master->dev);
14708ae12a0dSDavid Brownell }
14718ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_unregister_master);
14728ae12a0dSDavid Brownell 
1473ffbbdd21SLinus Walleij int spi_master_suspend(struct spi_master *master)
1474ffbbdd21SLinus Walleij {
1475ffbbdd21SLinus Walleij 	int ret;
1476ffbbdd21SLinus Walleij 
1477ffbbdd21SLinus Walleij 	/* Basically no-ops for non-queued masters */
1478ffbbdd21SLinus Walleij 	if (!master->queued)
1479ffbbdd21SLinus Walleij 		return 0;
1480ffbbdd21SLinus Walleij 
1481ffbbdd21SLinus Walleij 	ret = spi_stop_queue(master);
1482ffbbdd21SLinus Walleij 	if (ret)
1483ffbbdd21SLinus Walleij 		dev_err(&master->dev, "queue stop failed\n");
1484ffbbdd21SLinus Walleij 
1485ffbbdd21SLinus Walleij 	return ret;
1486ffbbdd21SLinus Walleij }
1487ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_master_suspend);
1488ffbbdd21SLinus Walleij 
1489ffbbdd21SLinus Walleij int spi_master_resume(struct spi_master *master)
1490ffbbdd21SLinus Walleij {
1491ffbbdd21SLinus Walleij 	int ret;
1492ffbbdd21SLinus Walleij 
1493ffbbdd21SLinus Walleij 	if (!master->queued)
1494ffbbdd21SLinus Walleij 		return 0;
1495ffbbdd21SLinus Walleij 
1496ffbbdd21SLinus Walleij 	ret = spi_start_queue(master);
1497ffbbdd21SLinus Walleij 	if (ret)
1498ffbbdd21SLinus Walleij 		dev_err(&master->dev, "queue restart failed\n");
1499ffbbdd21SLinus Walleij 
1500ffbbdd21SLinus Walleij 	return ret;
1501ffbbdd21SLinus Walleij }
1502ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_master_resume);
1503ffbbdd21SLinus Walleij 
15049f3b795aSMichał Mirosław static int __spi_master_match(struct device *dev, const void *data)
15055ed2c832SDave Young {
15065ed2c832SDave Young 	struct spi_master *m;
15079f3b795aSMichał Mirosław 	const u16 *bus_num = data;
15085ed2c832SDave Young 
15095ed2c832SDave Young 	m = container_of(dev, struct spi_master, dev);
15105ed2c832SDave Young 	return m->bus_num == *bus_num;
15115ed2c832SDave Young }
15125ed2c832SDave Young 
15138ae12a0dSDavid Brownell /**
15148ae12a0dSDavid Brownell  * spi_busnum_to_master - look up master associated with bus_num
15158ae12a0dSDavid Brownell  * @bus_num: the master's bus number
151633e34dc6SDavid Brownell  * Context: can sleep
15178ae12a0dSDavid Brownell  *
15188ae12a0dSDavid Brownell  * This call may be used with devices that are registered after
15198ae12a0dSDavid Brownell  * arch init time.  It returns a refcounted pointer to the relevant
15208ae12a0dSDavid Brownell  * spi_master (which the caller must release), or NULL if there is
15218ae12a0dSDavid Brownell  * no such master registered.
15228ae12a0dSDavid Brownell  */
15238ae12a0dSDavid Brownell struct spi_master *spi_busnum_to_master(u16 bus_num)
15248ae12a0dSDavid Brownell {
152549dce689STony Jones 	struct device		*dev;
15261e9a51dcSAtsushi Nemoto 	struct spi_master	*master = NULL;
15278ae12a0dSDavid Brownell 
1528695794aeSGreg Kroah-Hartman 	dev = class_find_device(&spi_master_class, NULL, &bus_num,
15295ed2c832SDave Young 				__spi_master_match);
15305ed2c832SDave Young 	if (dev)
15315ed2c832SDave Young 		master = container_of(dev, struct spi_master, dev);
15325ed2c832SDave Young 	/* reference got in class_find_device */
15331e9a51dcSAtsushi Nemoto 	return master;
15348ae12a0dSDavid Brownell }
15358ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master);
15368ae12a0dSDavid Brownell 
15378ae12a0dSDavid Brownell 
15388ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
15398ae12a0dSDavid Brownell 
15407d077197SDavid Brownell /* Core methods for SPI master protocol drivers.  Some of the
15417d077197SDavid Brownell  * other core methods are currently defined as inline functions.
15427d077197SDavid Brownell  */
15437d077197SDavid Brownell 
15447d077197SDavid Brownell /**
15457d077197SDavid Brownell  * spi_setup - setup SPI mode and clock rate
15467d077197SDavid Brownell  * @spi: the device whose settings are being modified
15477d077197SDavid Brownell  * Context: can sleep, and no requests are queued to the device
15487d077197SDavid Brownell  *
15497d077197SDavid Brownell  * SPI protocol drivers may need to update the transfer mode if the
15507d077197SDavid Brownell  * device doesn't work with its default.  They may likewise need
15517d077197SDavid Brownell  * to update clock rates or word sizes from initial values.  This function
15527d077197SDavid Brownell  * changes those settings, and must be called from a context that can sleep.
15537d077197SDavid Brownell  * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
15547d077197SDavid Brownell  * effect the next time the device is selected and data is transferred to
15557d077197SDavid Brownell  * or from it.  When this function returns, the spi device is deselected.
15567d077197SDavid Brownell  *
15577d077197SDavid Brownell  * Note that this call will fail if the protocol driver specifies an option
15587d077197SDavid Brownell  * that the underlying controller or its driver does not support.  For
15597d077197SDavid Brownell  * example, not all hardware supports wire transfers using nine bit words,
15607d077197SDavid Brownell  * LSB-first wire encoding, or active-high chipselects.
15617d077197SDavid Brownell  */
15627d077197SDavid Brownell int spi_setup(struct spi_device *spi)
15637d077197SDavid Brownell {
1564e7db06b5SDavid Brownell 	unsigned	bad_bits;
1565caae070cSLaxman Dewangan 	int		status = 0;
15667d077197SDavid Brownell 
1567f477b7fbSwangyuhang 	/* check mode to prevent that DUAL and QUAD set at the same time
1568f477b7fbSwangyuhang 	 */
1569f477b7fbSwangyuhang 	if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1570f477b7fbSwangyuhang 		((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1571f477b7fbSwangyuhang 		dev_err(&spi->dev,
1572f477b7fbSwangyuhang 		"setup: can not select dual and quad at the same time\n");
1573f477b7fbSwangyuhang 		return -EINVAL;
1574f477b7fbSwangyuhang 	}
1575f477b7fbSwangyuhang 	/* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1576f477b7fbSwangyuhang 	 */
1577f477b7fbSwangyuhang 	if ((spi->mode & SPI_3WIRE) && (spi->mode &
1578f477b7fbSwangyuhang 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1579f477b7fbSwangyuhang 		return -EINVAL;
1580e7db06b5SDavid Brownell 	/* help drivers fail *cleanly* when they need options
1581e7db06b5SDavid Brownell 	 * that aren't supported with their current master
1582e7db06b5SDavid Brownell 	 */
1583e7db06b5SDavid Brownell 	bad_bits = spi->mode & ~spi->master->mode_bits;
1584e7db06b5SDavid Brownell 	if (bad_bits) {
1585eb288a1fSLinus Walleij 		dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
1586e7db06b5SDavid Brownell 			bad_bits);
1587e7db06b5SDavid Brownell 		return -EINVAL;
1588e7db06b5SDavid Brownell 	}
1589e7db06b5SDavid Brownell 
15907d077197SDavid Brownell 	if (!spi->bits_per_word)
15917d077197SDavid Brownell 		spi->bits_per_word = 8;
15927d077197SDavid Brownell 
1593caae070cSLaxman Dewangan 	if (spi->master->setup)
15947d077197SDavid Brownell 		status = spi->master->setup(spi);
15957d077197SDavid Brownell 
15965fe5f05eSJingoo Han 	dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
15977d077197SDavid Brownell 			(int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
15987d077197SDavid Brownell 			(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
15997d077197SDavid Brownell 			(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
16007d077197SDavid Brownell 			(spi->mode & SPI_3WIRE) ? "3wire, " : "",
16017d077197SDavid Brownell 			(spi->mode & SPI_LOOP) ? "loopback, " : "",
16027d077197SDavid Brownell 			spi->bits_per_word, spi->max_speed_hz,
16037d077197SDavid Brownell 			status);
16047d077197SDavid Brownell 
16057d077197SDavid Brownell 	return status;
16067d077197SDavid Brownell }
16077d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup);
16087d077197SDavid Brownell 
1609cf32b71eSErnst Schwab static int __spi_async(struct spi_device *spi, struct spi_message *message)
1610cf32b71eSErnst Schwab {
1611cf32b71eSErnst Schwab 	struct spi_master *master = spi->master;
1612e6811d1dSLaxman Dewangan 	struct spi_transfer *xfer;
1613cf32b71eSErnst Schwab 
161456ec1978SMark Brown 	message->spi = spi;
161556ec1978SMark Brown 
161656ec1978SMark Brown 	trace_spi_message_submit(message);
161756ec1978SMark Brown 
161824a0013aSMark Brown 	if (list_empty(&message->transfers))
161924a0013aSMark Brown 		return -EINVAL;
162024a0013aSMark Brown 	if (!message->complete)
162124a0013aSMark Brown 		return -EINVAL;
162224a0013aSMark Brown 
1623cf32b71eSErnst Schwab 	/* Half-duplex links include original MicroWire, and ones with
1624cf32b71eSErnst Schwab 	 * only one data pin like SPI_3WIRE (switches direction) or where
1625cf32b71eSErnst Schwab 	 * either MOSI or MISO is missing.  They can also be caused by
1626cf32b71eSErnst Schwab 	 * software limitations.
1627cf32b71eSErnst Schwab 	 */
1628cf32b71eSErnst Schwab 	if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1629cf32b71eSErnst Schwab 			|| (spi->mode & SPI_3WIRE)) {
1630cf32b71eSErnst Schwab 		unsigned flags = master->flags;
1631cf32b71eSErnst Schwab 
1632cf32b71eSErnst Schwab 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
1633cf32b71eSErnst Schwab 			if (xfer->rx_buf && xfer->tx_buf)
1634cf32b71eSErnst Schwab 				return -EINVAL;
1635cf32b71eSErnst Schwab 			if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
1636cf32b71eSErnst Schwab 				return -EINVAL;
1637cf32b71eSErnst Schwab 			if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
1638cf32b71eSErnst Schwab 				return -EINVAL;
1639cf32b71eSErnst Schwab 		}
1640cf32b71eSErnst Schwab 	}
1641cf32b71eSErnst Schwab 
1642e6811d1dSLaxman Dewangan 	/**
1643059b8ffeSLaxman Dewangan 	 * Set transfer bits_per_word and max speed as spi device default if
1644059b8ffeSLaxman Dewangan 	 * it is not set for this transfer.
1645f477b7fbSwangyuhang 	 * Set transfer tx_nbits and rx_nbits as single transfer default
1646f477b7fbSwangyuhang 	 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
1647e6811d1dSLaxman Dewangan 	 */
1648e6811d1dSLaxman Dewangan 	list_for_each_entry(xfer, &message->transfers, transfer_list) {
1649078726ceSSourav Poddar 		message->frame_length += xfer->len;
1650e6811d1dSLaxman Dewangan 		if (!xfer->bits_per_word)
1651e6811d1dSLaxman Dewangan 			xfer->bits_per_word = spi->bits_per_word;
165256ede94aSGabor Juhos 		if (!xfer->speed_hz) {
1653059b8ffeSLaxman Dewangan 			xfer->speed_hz = spi->max_speed_hz;
165456ede94aSGabor Juhos 			if (master->max_speed_hz &&
165556ede94aSGabor Juhos 			    xfer->speed_hz > master->max_speed_hz)
165656ede94aSGabor Juhos 				xfer->speed_hz = master->max_speed_hz;
165756ede94aSGabor Juhos 		}
165856ede94aSGabor Juhos 
1659543bb255SStephen Warren 		if (master->bits_per_word_mask) {
1660543bb255SStephen Warren 			/* Only 32 bits fit in the mask */
1661543bb255SStephen Warren 			if (xfer->bits_per_word > 32)
1662543bb255SStephen Warren 				return -EINVAL;
1663543bb255SStephen Warren 			if (!(master->bits_per_word_mask &
1664543bb255SStephen Warren 					BIT(xfer->bits_per_word - 1)))
1665543bb255SStephen Warren 				return -EINVAL;
1666543bb255SStephen Warren 		}
1667a2fd4f9fSMark Brown 
1668a2fd4f9fSMark Brown 		if (xfer->speed_hz && master->min_speed_hz &&
1669a2fd4f9fSMark Brown 		    xfer->speed_hz < master->min_speed_hz)
1670a2fd4f9fSMark Brown 			return -EINVAL;
1671a2fd4f9fSMark Brown 		if (xfer->speed_hz && master->max_speed_hz &&
1672a2fd4f9fSMark Brown 		    xfer->speed_hz > master->max_speed_hz)
1673d5ee722aSwangyuhang 			return -EINVAL;
1674f477b7fbSwangyuhang 
1675f477b7fbSwangyuhang 		if (xfer->tx_buf && !xfer->tx_nbits)
1676f477b7fbSwangyuhang 			xfer->tx_nbits = SPI_NBITS_SINGLE;
1677f477b7fbSwangyuhang 		if (xfer->rx_buf && !xfer->rx_nbits)
1678f477b7fbSwangyuhang 			xfer->rx_nbits = SPI_NBITS_SINGLE;
1679f477b7fbSwangyuhang 		/* check transfer tx/rx_nbits:
1680f477b7fbSwangyuhang 		 * 1. keep the value is not out of single, dual and quad
1681f477b7fbSwangyuhang 		 * 2. keep tx/rx_nbits is contained by mode in spi_device
1682f477b7fbSwangyuhang 		 * 3. if SPI_3WIRE, tx/rx_nbits should be in single
1683f477b7fbSwangyuhang 		 */
1684db90a441SSourav Poddar 		if (xfer->tx_buf) {
1685f477b7fbSwangyuhang 			if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
1686f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_DUAL &&
1687f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_QUAD)
1688a2fd4f9fSMark Brown 				return -EINVAL;
1689f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
1690f477b7fbSwangyuhang 				!(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
1691f477b7fbSwangyuhang 				return -EINVAL;
1692f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
1693f477b7fbSwangyuhang 				!(spi->mode & SPI_TX_QUAD))
1694f477b7fbSwangyuhang 				return -EINVAL;
1695f477b7fbSwangyuhang 			if ((spi->mode & SPI_3WIRE) &&
1696f477b7fbSwangyuhang 				(xfer->tx_nbits != SPI_NBITS_SINGLE))
1697f477b7fbSwangyuhang 				return -EINVAL;
1698db90a441SSourav Poddar 		}
1699f477b7fbSwangyuhang 		/* check transfer rx_nbits */
1700db90a441SSourav Poddar 		if (xfer->rx_buf) {
1701f477b7fbSwangyuhang 			if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
1702f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_DUAL &&
1703f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_QUAD)
1704f477b7fbSwangyuhang 				return -EINVAL;
1705f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
1706f477b7fbSwangyuhang 				!(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
1707f477b7fbSwangyuhang 				return -EINVAL;
1708f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
1709f477b7fbSwangyuhang 				!(spi->mode & SPI_RX_QUAD))
1710f477b7fbSwangyuhang 				return -EINVAL;
1711f477b7fbSwangyuhang 			if ((spi->mode & SPI_3WIRE) &&
1712f477b7fbSwangyuhang 				(xfer->rx_nbits != SPI_NBITS_SINGLE))
1713cf32b71eSErnst Schwab 				return -EINVAL;
1714568d0697SDavid Brownell 		}
17157d077197SDavid Brownell 	}
17167d077197SDavid Brownell 
17177d077197SDavid Brownell 	message->status = -EINPROGRESS;
17187d077197SDavid Brownell 	return master->transfer(spi, message);
17197d077197SDavid Brownell }
17207d077197SDavid Brownell 
1721568d0697SDavid Brownell /**
1722568d0697SDavid Brownell  * spi_async - asynchronous SPI transfer
1723568d0697SDavid Brownell  * @spi: device with which data will be exchanged
1724568d0697SDavid Brownell  * @message: describes the data transfers, including completion callback
1725568d0697SDavid Brownell  * Context: any (irqs may be blocked, etc)
1726568d0697SDavid Brownell  *
1727568d0697SDavid Brownell  * This call may be used in_irq and other contexts which can't sleep,
1728568d0697SDavid Brownell  * as well as from task contexts which can sleep.
1729568d0697SDavid Brownell  *
1730568d0697SDavid Brownell  * The completion callback is invoked in a context which can't sleep.
1731568d0697SDavid Brownell  * Before that invocation, the value of message->status is undefined.
1732568d0697SDavid Brownell  * When the callback is issued, message->status holds either zero (to
1733568d0697SDavid Brownell  * indicate complete success) or a negative error code.  After that
1734568d0697SDavid Brownell  * callback returns, the driver which issued the transfer request may
1735568d0697SDavid Brownell  * deallocate the associated memory; it's no longer in use by any SPI
1736568d0697SDavid Brownell  * core or controller driver code.
1737568d0697SDavid Brownell  *
1738568d0697SDavid Brownell  * Note that although all messages to a spi_device are handled in
1739568d0697SDavid Brownell  * FIFO order, messages may go to different devices in other orders.
1740568d0697SDavid Brownell  * Some device might be higher priority, or have various "hard" access
1741568d0697SDavid Brownell  * time requirements, for example.
1742568d0697SDavid Brownell  *
1743568d0697SDavid Brownell  * On detection of any fault during the transfer, processing of
1744568d0697SDavid Brownell  * the entire message is aborted, and the device is deselected.
1745568d0697SDavid Brownell  * Until returning from the associated message completion callback,
1746568d0697SDavid Brownell  * no other spi_message queued to that device will be processed.
1747568d0697SDavid Brownell  * (This rule applies equally to all the synchronous transfer calls,
1748568d0697SDavid Brownell  * which are wrappers around this core asynchronous primitive.)
1749568d0697SDavid Brownell  */
1750568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message)
1751568d0697SDavid Brownell {
1752568d0697SDavid Brownell 	struct spi_master *master = spi->master;
1753cf32b71eSErnst Schwab 	int ret;
1754cf32b71eSErnst Schwab 	unsigned long flags;
1755568d0697SDavid Brownell 
1756cf32b71eSErnst Schwab 	spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1757568d0697SDavid Brownell 
1758cf32b71eSErnst Schwab 	if (master->bus_lock_flag)
1759cf32b71eSErnst Schwab 		ret = -EBUSY;
1760cf32b71eSErnst Schwab 	else
1761cf32b71eSErnst Schwab 		ret = __spi_async(spi, message);
1762568d0697SDavid Brownell 
1763cf32b71eSErnst Schwab 	spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1764cf32b71eSErnst Schwab 
1765cf32b71eSErnst Schwab 	return ret;
1766568d0697SDavid Brownell }
1767568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async);
1768568d0697SDavid Brownell 
1769cf32b71eSErnst Schwab /**
1770cf32b71eSErnst Schwab  * spi_async_locked - version of spi_async with exclusive bus usage
1771cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
1772cf32b71eSErnst Schwab  * @message: describes the data transfers, including completion callback
1773cf32b71eSErnst Schwab  * Context: any (irqs may be blocked, etc)
1774cf32b71eSErnst Schwab  *
1775cf32b71eSErnst Schwab  * This call may be used in_irq and other contexts which can't sleep,
1776cf32b71eSErnst Schwab  * as well as from task contexts which can sleep.
1777cf32b71eSErnst Schwab  *
1778cf32b71eSErnst Schwab  * The completion callback is invoked in a context which can't sleep.
1779cf32b71eSErnst Schwab  * Before that invocation, the value of message->status is undefined.
1780cf32b71eSErnst Schwab  * When the callback is issued, message->status holds either zero (to
1781cf32b71eSErnst Schwab  * indicate complete success) or a negative error code.  After that
1782cf32b71eSErnst Schwab  * callback returns, the driver which issued the transfer request may
1783cf32b71eSErnst Schwab  * deallocate the associated memory; it's no longer in use by any SPI
1784cf32b71eSErnst Schwab  * core or controller driver code.
1785cf32b71eSErnst Schwab  *
1786cf32b71eSErnst Schwab  * Note that although all messages to a spi_device are handled in
1787cf32b71eSErnst Schwab  * FIFO order, messages may go to different devices in other orders.
1788cf32b71eSErnst Schwab  * Some device might be higher priority, or have various "hard" access
1789cf32b71eSErnst Schwab  * time requirements, for example.
1790cf32b71eSErnst Schwab  *
1791cf32b71eSErnst Schwab  * On detection of any fault during the transfer, processing of
1792cf32b71eSErnst Schwab  * the entire message is aborted, and the device is deselected.
1793cf32b71eSErnst Schwab  * Until returning from the associated message completion callback,
1794cf32b71eSErnst Schwab  * no other spi_message queued to that device will be processed.
1795cf32b71eSErnst Schwab  * (This rule applies equally to all the synchronous transfer calls,
1796cf32b71eSErnst Schwab  * which are wrappers around this core asynchronous primitive.)
1797cf32b71eSErnst Schwab  */
1798cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message)
1799cf32b71eSErnst Schwab {
1800cf32b71eSErnst Schwab 	struct spi_master *master = spi->master;
1801cf32b71eSErnst Schwab 	int ret;
1802cf32b71eSErnst Schwab 	unsigned long flags;
1803cf32b71eSErnst Schwab 
1804cf32b71eSErnst Schwab 	spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1805cf32b71eSErnst Schwab 
1806cf32b71eSErnst Schwab 	ret = __spi_async(spi, message);
1807cf32b71eSErnst Schwab 
1808cf32b71eSErnst Schwab 	spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1809cf32b71eSErnst Schwab 
1810cf32b71eSErnst Schwab 	return ret;
1811cf32b71eSErnst Schwab 
1812cf32b71eSErnst Schwab }
1813cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked);
1814cf32b71eSErnst Schwab 
18157d077197SDavid Brownell 
18167d077197SDavid Brownell /*-------------------------------------------------------------------------*/
18177d077197SDavid Brownell 
18187d077197SDavid Brownell /* Utility methods for SPI master protocol drivers, layered on
18197d077197SDavid Brownell  * top of the core.  Some other utility methods are defined as
18207d077197SDavid Brownell  * inline functions.
18217d077197SDavid Brownell  */
18227d077197SDavid Brownell 
18235d870c8eSAndrew Morton static void spi_complete(void *arg)
18245d870c8eSAndrew Morton {
18255d870c8eSAndrew Morton 	complete(arg);
18265d870c8eSAndrew Morton }
18275d870c8eSAndrew Morton 
1828cf32b71eSErnst Schwab static int __spi_sync(struct spi_device *spi, struct spi_message *message,
1829cf32b71eSErnst Schwab 		      int bus_locked)
1830cf32b71eSErnst Schwab {
1831cf32b71eSErnst Schwab 	DECLARE_COMPLETION_ONSTACK(done);
1832cf32b71eSErnst Schwab 	int status;
1833cf32b71eSErnst Schwab 	struct spi_master *master = spi->master;
1834cf32b71eSErnst Schwab 
1835cf32b71eSErnst Schwab 	message->complete = spi_complete;
1836cf32b71eSErnst Schwab 	message->context = &done;
1837cf32b71eSErnst Schwab 
1838cf32b71eSErnst Schwab 	if (!bus_locked)
1839cf32b71eSErnst Schwab 		mutex_lock(&master->bus_lock_mutex);
1840cf32b71eSErnst Schwab 
1841cf32b71eSErnst Schwab 	status = spi_async_locked(spi, message);
1842cf32b71eSErnst Schwab 
1843cf32b71eSErnst Schwab 	if (!bus_locked)
1844cf32b71eSErnst Schwab 		mutex_unlock(&master->bus_lock_mutex);
1845cf32b71eSErnst Schwab 
1846cf32b71eSErnst Schwab 	if (status == 0) {
1847cf32b71eSErnst Schwab 		wait_for_completion(&done);
1848cf32b71eSErnst Schwab 		status = message->status;
1849cf32b71eSErnst Schwab 	}
1850cf32b71eSErnst Schwab 	message->context = NULL;
1851cf32b71eSErnst Schwab 	return status;
1852cf32b71eSErnst Schwab }
1853cf32b71eSErnst Schwab 
18548ae12a0dSDavid Brownell /**
18558ae12a0dSDavid Brownell  * spi_sync - blocking/synchronous SPI data transfers
18568ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
18578ae12a0dSDavid Brownell  * @message: describes the data transfers
185833e34dc6SDavid Brownell  * Context: can sleep
18598ae12a0dSDavid Brownell  *
18608ae12a0dSDavid Brownell  * This call may only be used from a context that may sleep.  The sleep
18618ae12a0dSDavid Brownell  * is non-interruptible, and has no timeout.  Low-overhead controller
18628ae12a0dSDavid Brownell  * drivers may DMA directly into and out of the message buffers.
18638ae12a0dSDavid Brownell  *
18648ae12a0dSDavid Brownell  * Note that the SPI device's chip select is active during the message,
18658ae12a0dSDavid Brownell  * and then is normally disabled between messages.  Drivers for some
18668ae12a0dSDavid Brownell  * frequently-used devices may want to minimize costs of selecting a chip,
18678ae12a0dSDavid Brownell  * by leaving it selected in anticipation that the next message will go
18688ae12a0dSDavid Brownell  * to the same chip.  (That may increase power usage.)
18698ae12a0dSDavid Brownell  *
18700c868461SDavid Brownell  * Also, the caller is guaranteeing that the memory associated with the
18710c868461SDavid Brownell  * message will not be freed before this call returns.
18720c868461SDavid Brownell  *
18739b938b74SMarc Pignat  * It returns zero on success, else a negative error code.
18748ae12a0dSDavid Brownell  */
18758ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message)
18768ae12a0dSDavid Brownell {
1877cf32b71eSErnst Schwab 	return __spi_sync(spi, message, 0);
18788ae12a0dSDavid Brownell }
18798ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync);
18808ae12a0dSDavid Brownell 
1881cf32b71eSErnst Schwab /**
1882cf32b71eSErnst Schwab  * spi_sync_locked - version of spi_sync with exclusive bus usage
1883cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
1884cf32b71eSErnst Schwab  * @message: describes the data transfers
1885cf32b71eSErnst Schwab  * Context: can sleep
1886cf32b71eSErnst Schwab  *
1887cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
1888cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.  Low-overhead controller
1889cf32b71eSErnst Schwab  * drivers may DMA directly into and out of the message buffers.
1890cf32b71eSErnst Schwab  *
1891cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
189225985edcSLucas De Marchi  * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
1893cf32b71eSErnst Schwab  * be released by a spi_bus_unlock call when the exclusive access is over.
1894cf32b71eSErnst Schwab  *
1895cf32b71eSErnst Schwab  * It returns zero on success, else a negative error code.
1896cf32b71eSErnst Schwab  */
1897cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
1898cf32b71eSErnst Schwab {
1899cf32b71eSErnst Schwab 	return __spi_sync(spi, message, 1);
1900cf32b71eSErnst Schwab }
1901cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked);
1902cf32b71eSErnst Schwab 
1903cf32b71eSErnst Schwab /**
1904cf32b71eSErnst Schwab  * spi_bus_lock - obtain a lock for exclusive SPI bus usage
1905cf32b71eSErnst Schwab  * @master: SPI bus master that should be locked for exclusive bus access
1906cf32b71eSErnst Schwab  * Context: can sleep
1907cf32b71eSErnst Schwab  *
1908cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
1909cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
1910cf32b71eSErnst Schwab  *
1911cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
1912cf32b71eSErnst Schwab  * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
1913cf32b71eSErnst Schwab  * exclusive access is over. Data transfer must be done by spi_sync_locked
1914cf32b71eSErnst Schwab  * and spi_async_locked calls when the SPI bus lock is held.
1915cf32b71eSErnst Schwab  *
1916cf32b71eSErnst Schwab  * It returns zero on success, else a negative error code.
1917cf32b71eSErnst Schwab  */
1918cf32b71eSErnst Schwab int spi_bus_lock(struct spi_master *master)
1919cf32b71eSErnst Schwab {
1920cf32b71eSErnst Schwab 	unsigned long flags;
1921cf32b71eSErnst Schwab 
1922cf32b71eSErnst Schwab 	mutex_lock(&master->bus_lock_mutex);
1923cf32b71eSErnst Schwab 
1924cf32b71eSErnst Schwab 	spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1925cf32b71eSErnst Schwab 	master->bus_lock_flag = 1;
1926cf32b71eSErnst Schwab 	spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1927cf32b71eSErnst Schwab 
1928cf32b71eSErnst Schwab 	/* mutex remains locked until spi_bus_unlock is called */
1929cf32b71eSErnst Schwab 
1930cf32b71eSErnst Schwab 	return 0;
1931cf32b71eSErnst Schwab }
1932cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock);
1933cf32b71eSErnst Schwab 
1934cf32b71eSErnst Schwab /**
1935cf32b71eSErnst Schwab  * spi_bus_unlock - release the lock for exclusive SPI bus usage
1936cf32b71eSErnst Schwab  * @master: SPI bus master that was locked for exclusive bus access
1937cf32b71eSErnst Schwab  * Context: can sleep
1938cf32b71eSErnst Schwab  *
1939cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
1940cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
1941cf32b71eSErnst Schwab  *
1942cf32b71eSErnst Schwab  * This call releases an SPI bus lock previously obtained by an spi_bus_lock
1943cf32b71eSErnst Schwab  * call.
1944cf32b71eSErnst Schwab  *
1945cf32b71eSErnst Schwab  * It returns zero on success, else a negative error code.
1946cf32b71eSErnst Schwab  */
1947cf32b71eSErnst Schwab int spi_bus_unlock(struct spi_master *master)
1948cf32b71eSErnst Schwab {
1949cf32b71eSErnst Schwab 	master->bus_lock_flag = 0;
1950cf32b71eSErnst Schwab 
1951cf32b71eSErnst Schwab 	mutex_unlock(&master->bus_lock_mutex);
1952cf32b71eSErnst Schwab 
1953cf32b71eSErnst Schwab 	return 0;
1954cf32b71eSErnst Schwab }
1955cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock);
1956cf32b71eSErnst Schwab 
1957a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */
1958a9948b61SDavid Brownell #define	SPI_BUFSIZ	max(32, SMP_CACHE_BYTES)
19598ae12a0dSDavid Brownell 
19608ae12a0dSDavid Brownell static u8	*buf;
19618ae12a0dSDavid Brownell 
19628ae12a0dSDavid Brownell /**
19638ae12a0dSDavid Brownell  * spi_write_then_read - SPI synchronous write followed by read
19648ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
19658ae12a0dSDavid Brownell  * @txbuf: data to be written (need not be dma-safe)
19668ae12a0dSDavid Brownell  * @n_tx: size of txbuf, in bytes
196727570497SJiri Pirko  * @rxbuf: buffer into which data will be read (need not be dma-safe)
196827570497SJiri Pirko  * @n_rx: size of rxbuf, in bytes
196933e34dc6SDavid Brownell  * Context: can sleep
19708ae12a0dSDavid Brownell  *
19718ae12a0dSDavid Brownell  * This performs a half duplex MicroWire style transaction with the
19728ae12a0dSDavid Brownell  * device, sending txbuf and then reading rxbuf.  The return value
19738ae12a0dSDavid Brownell  * is zero for success, else a negative errno status code.
1974b885244eSDavid Brownell  * This call may only be used from a context that may sleep.
19758ae12a0dSDavid Brownell  *
19760c868461SDavid Brownell  * Parameters to this routine are always copied using a small buffer;
197733e34dc6SDavid Brownell  * portable code should never use this for more than 32 bytes.
197833e34dc6SDavid Brownell  * Performance-sensitive or bulk transfer code should instead use
19790c868461SDavid Brownell  * spi_{async,sync}() calls with dma-safe buffers.
19808ae12a0dSDavid Brownell  */
19818ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi,
19820c4a1590SMark Brown 		const void *txbuf, unsigned n_tx,
19830c4a1590SMark Brown 		void *rxbuf, unsigned n_rx)
19848ae12a0dSDavid Brownell {
1985068f4070SDavid Brownell 	static DEFINE_MUTEX(lock);
19868ae12a0dSDavid Brownell 
19878ae12a0dSDavid Brownell 	int			status;
19888ae12a0dSDavid Brownell 	struct spi_message	message;
1989bdff549eSDavid Brownell 	struct spi_transfer	x[2];
19908ae12a0dSDavid Brownell 	u8			*local_buf;
19918ae12a0dSDavid Brownell 
1992b3a223eeSMark Brown 	/* Use preallocated DMA-safe buffer if we can.  We can't avoid
1993b3a223eeSMark Brown 	 * copying here, (as a pure convenience thing), but we can
1994b3a223eeSMark Brown 	 * keep heap costs out of the hot path unless someone else is
1995b3a223eeSMark Brown 	 * using the pre-allocated buffer or the transfer is too large.
19968ae12a0dSDavid Brownell 	 */
1997b3a223eeSMark Brown 	if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
19982cd94c8aSMark Brown 		local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
19992cd94c8aSMark Brown 				    GFP_KERNEL | GFP_DMA);
2000b3a223eeSMark Brown 		if (!local_buf)
2001b3a223eeSMark Brown 			return -ENOMEM;
2002b3a223eeSMark Brown 	} else {
2003b3a223eeSMark Brown 		local_buf = buf;
2004b3a223eeSMark Brown 	}
20058ae12a0dSDavid Brownell 
20068275c642SVitaly Wool 	spi_message_init(&message);
20075fe5f05eSJingoo Han 	memset(x, 0, sizeof(x));
2008bdff549eSDavid Brownell 	if (n_tx) {
2009bdff549eSDavid Brownell 		x[0].len = n_tx;
2010bdff549eSDavid Brownell 		spi_message_add_tail(&x[0], &message);
2011bdff549eSDavid Brownell 	}
2012bdff549eSDavid Brownell 	if (n_rx) {
2013bdff549eSDavid Brownell 		x[1].len = n_rx;
2014bdff549eSDavid Brownell 		spi_message_add_tail(&x[1], &message);
2015bdff549eSDavid Brownell 	}
20168275c642SVitaly Wool 
20178ae12a0dSDavid Brownell 	memcpy(local_buf, txbuf, n_tx);
2018bdff549eSDavid Brownell 	x[0].tx_buf = local_buf;
2019bdff549eSDavid Brownell 	x[1].rx_buf = local_buf + n_tx;
20208ae12a0dSDavid Brownell 
20218ae12a0dSDavid Brownell 	/* do the i/o */
20228ae12a0dSDavid Brownell 	status = spi_sync(spi, &message);
20239b938b74SMarc Pignat 	if (status == 0)
2024bdff549eSDavid Brownell 		memcpy(rxbuf, x[1].rx_buf, n_rx);
20258ae12a0dSDavid Brownell 
2026bdff549eSDavid Brownell 	if (x[0].tx_buf == buf)
2027068f4070SDavid Brownell 		mutex_unlock(&lock);
20288ae12a0dSDavid Brownell 	else
20298ae12a0dSDavid Brownell 		kfree(local_buf);
20308ae12a0dSDavid Brownell 
20318ae12a0dSDavid Brownell 	return status;
20328ae12a0dSDavid Brownell }
20338ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read);
20348ae12a0dSDavid Brownell 
20358ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
20368ae12a0dSDavid Brownell 
20378ae12a0dSDavid Brownell static int __init spi_init(void)
20388ae12a0dSDavid Brownell {
2039b885244eSDavid Brownell 	int	status;
20408ae12a0dSDavid Brownell 
2041e94b1766SChristoph Lameter 	buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
2042b885244eSDavid Brownell 	if (!buf) {
2043b885244eSDavid Brownell 		status = -ENOMEM;
2044b885244eSDavid Brownell 		goto err0;
20458ae12a0dSDavid Brownell 	}
2046b885244eSDavid Brownell 
2047b885244eSDavid Brownell 	status = bus_register(&spi_bus_type);
2048b885244eSDavid Brownell 	if (status < 0)
2049b885244eSDavid Brownell 		goto err1;
2050b885244eSDavid Brownell 
2051b885244eSDavid Brownell 	status = class_register(&spi_master_class);
2052b885244eSDavid Brownell 	if (status < 0)
2053b885244eSDavid Brownell 		goto err2;
2054b885244eSDavid Brownell 	return 0;
2055b885244eSDavid Brownell 
2056b885244eSDavid Brownell err2:
2057b885244eSDavid Brownell 	bus_unregister(&spi_bus_type);
2058b885244eSDavid Brownell err1:
2059b885244eSDavid Brownell 	kfree(buf);
2060b885244eSDavid Brownell 	buf = NULL;
2061b885244eSDavid Brownell err0:
2062b885244eSDavid Brownell 	return status;
2063b885244eSDavid Brownell }
2064b885244eSDavid Brownell 
20658ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(),
20668ae12a0dSDavid Brownell  * but even essential drivers wait till later
2067b885244eSDavid Brownell  *
2068b885244eSDavid Brownell  * REVISIT only boardinfo really needs static linking. the rest (device and
2069b885244eSDavid Brownell  * driver registration) _could_ be dynamically linked (modular) ... costs
2070b885244eSDavid Brownell  * include needing to have boardinfo data structures be much more public.
20718ae12a0dSDavid Brownell  */
2072673c0c00SDavid Brownell postcore_initcall(spi_init);
20738ae12a0dSDavid Brownell 
2074