xref: /linux/drivers/spi/spi.c (revision 1cfd97f93e36b3f8b5d2a26147aaccae0c847a9c)
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 
373dc87c98eSGrant Likely /**
374dc87c98eSGrant Likely  * spi_add_device - Add spi_device allocated with spi_alloc_device
375dc87c98eSGrant Likely  * @spi: spi_device to register
376dc87c98eSGrant Likely  *
377dc87c98eSGrant Likely  * Companion function to spi_alloc_device.  Devices allocated with
378dc87c98eSGrant Likely  * spi_alloc_device can be added onto the spi bus with this function.
379dc87c98eSGrant Likely  *
380e48880e0SDavid Brownell  * Returns 0 on success; negative errno on failure
381dc87c98eSGrant Likely  */
382dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi)
383dc87c98eSGrant Likely {
384e48880e0SDavid Brownell 	static DEFINE_MUTEX(spi_add_lock);
38574317984SJean-Christophe PLAGNIOL-VILLARD 	struct spi_master *master = spi->master;
38674317984SJean-Christophe PLAGNIOL-VILLARD 	struct device *dev = master->dev.parent;
3878ec130a0SRoman Tereshonkov 	struct device *d;
388dc87c98eSGrant Likely 	int status;
389dc87c98eSGrant Likely 
390dc87c98eSGrant Likely 	/* Chipselects are numbered 0..max; validate. */
39174317984SJean-Christophe PLAGNIOL-VILLARD 	if (spi->chip_select >= master->num_chipselect) {
392dc87c98eSGrant Likely 		dev_err(dev, "cs%d >= max %d\n",
393dc87c98eSGrant Likely 			spi->chip_select,
39474317984SJean-Christophe PLAGNIOL-VILLARD 			master->num_chipselect);
395dc87c98eSGrant Likely 		return -EINVAL;
396dc87c98eSGrant Likely 	}
397dc87c98eSGrant Likely 
398dc87c98eSGrant Likely 	/* Set the bus ID string */
399e13ac47bSJarkko Nikula 	spi_dev_set_name(spi);
400e48880e0SDavid Brownell 
401e48880e0SDavid Brownell 	/* We need to make sure there's no other device with this
402e48880e0SDavid Brownell 	 * chipselect **BEFORE** we call setup(), else we'll trash
403e48880e0SDavid Brownell 	 * its configuration.  Lock against concurrent add() calls.
404e48880e0SDavid Brownell 	 */
405e48880e0SDavid Brownell 	mutex_lock(&spi_add_lock);
406e48880e0SDavid Brownell 
4078ec130a0SRoman Tereshonkov 	d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
4088ec130a0SRoman Tereshonkov 	if (d != NULL) {
409e48880e0SDavid Brownell 		dev_err(dev, "chipselect %d already in use\n",
410e48880e0SDavid Brownell 				spi->chip_select);
4118ec130a0SRoman Tereshonkov 		put_device(d);
412e48880e0SDavid Brownell 		status = -EBUSY;
413e48880e0SDavid Brownell 		goto done;
414e48880e0SDavid Brownell 	}
415e48880e0SDavid Brownell 
41674317984SJean-Christophe PLAGNIOL-VILLARD 	if (master->cs_gpios)
41774317984SJean-Christophe PLAGNIOL-VILLARD 		spi->cs_gpio = master->cs_gpios[spi->chip_select];
41874317984SJean-Christophe PLAGNIOL-VILLARD 
419e48880e0SDavid Brownell 	/* Drivers may modify this initial i/o setup, but will
420e48880e0SDavid Brownell 	 * normally rely on the device being setup.  Devices
421e48880e0SDavid Brownell 	 * using SPI_CS_HIGH can't coexist well otherwise...
422e48880e0SDavid Brownell 	 */
4237d077197SDavid Brownell 	status = spi_setup(spi);
424dc87c98eSGrant Likely 	if (status < 0) {
425eb288a1fSLinus Walleij 		dev_err(dev, "can't setup %s, status %d\n",
426eb288a1fSLinus Walleij 				dev_name(&spi->dev), status);
427e48880e0SDavid Brownell 		goto done;
428dc87c98eSGrant Likely 	}
429dc87c98eSGrant Likely 
430e48880e0SDavid Brownell 	/* Device may be bound to an active driver when this returns */
431dc87c98eSGrant Likely 	status = device_add(&spi->dev);
432e48880e0SDavid Brownell 	if (status < 0)
433eb288a1fSLinus Walleij 		dev_err(dev, "can't add %s, status %d\n",
434eb288a1fSLinus Walleij 				dev_name(&spi->dev), status);
435e48880e0SDavid Brownell 	else
43635f74fcaSKay Sievers 		dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
437e48880e0SDavid Brownell 
438e48880e0SDavid Brownell done:
439e48880e0SDavid Brownell 	mutex_unlock(&spi_add_lock);
440e48880e0SDavid Brownell 	return status;
441dc87c98eSGrant Likely }
442dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device);
4438ae12a0dSDavid Brownell 
44433e34dc6SDavid Brownell /**
44533e34dc6SDavid Brownell  * spi_new_device - instantiate one new SPI device
44633e34dc6SDavid Brownell  * @master: Controller to which device is connected
44733e34dc6SDavid Brownell  * @chip: Describes the SPI device
44833e34dc6SDavid Brownell  * Context: can sleep
44933e34dc6SDavid Brownell  *
45033e34dc6SDavid Brownell  * On typical mainboards, this is purely internal; and it's not needed
4518ae12a0dSDavid Brownell  * after board init creates the hard-wired devices.  Some development
4528ae12a0dSDavid Brownell  * platforms may not be able to use spi_register_board_info though, and
4538ae12a0dSDavid Brownell  * this is exported so that for example a USB or parport based adapter
4548ae12a0dSDavid Brownell  * driver could add devices (which it would learn about out-of-band).
455082c8cb4SDavid Brownell  *
456082c8cb4SDavid Brownell  * Returns the new device, or NULL.
4578ae12a0dSDavid Brownell  */
458e9d5a461SAdrian Bunk struct spi_device *spi_new_device(struct spi_master *master,
459e9d5a461SAdrian Bunk 				  struct spi_board_info *chip)
4608ae12a0dSDavid Brownell {
4618ae12a0dSDavid Brownell 	struct spi_device	*proxy;
4628ae12a0dSDavid Brownell 	int			status;
4638ae12a0dSDavid Brownell 
464082c8cb4SDavid Brownell 	/* NOTE:  caller did any chip->bus_num checks necessary.
465082c8cb4SDavid Brownell 	 *
466082c8cb4SDavid Brownell 	 * Also, unless we change the return value convention to use
467082c8cb4SDavid Brownell 	 * error-or-pointer (not NULL-or-pointer), troubleshootability
468082c8cb4SDavid Brownell 	 * suggests syslogged diagnostics are best here (ugh).
469082c8cb4SDavid Brownell 	 */
470082c8cb4SDavid Brownell 
471dc87c98eSGrant Likely 	proxy = spi_alloc_device(master);
472dc87c98eSGrant Likely 	if (!proxy)
4738ae12a0dSDavid Brownell 		return NULL;
4748ae12a0dSDavid Brownell 
475102eb975SGrant Likely 	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
476102eb975SGrant Likely 
4778ae12a0dSDavid Brownell 	proxy->chip_select = chip->chip_select;
4788ae12a0dSDavid Brownell 	proxy->max_speed_hz = chip->max_speed_hz;
479980a01c9SDavid Brownell 	proxy->mode = chip->mode;
4808ae12a0dSDavid Brownell 	proxy->irq = chip->irq;
481102eb975SGrant Likely 	strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
4828ae12a0dSDavid Brownell 	proxy->dev.platform_data = (void *) chip->platform_data;
4838ae12a0dSDavid Brownell 	proxy->controller_data = chip->controller_data;
4848ae12a0dSDavid Brownell 	proxy->controller_state = NULL;
4858ae12a0dSDavid Brownell 
486dc87c98eSGrant Likely 	status = spi_add_device(proxy);
4878ae12a0dSDavid Brownell 	if (status < 0) {
488dc87c98eSGrant Likely 		spi_dev_put(proxy);
4898ae12a0dSDavid Brownell 		return NULL;
4908ae12a0dSDavid Brownell 	}
491dc87c98eSGrant Likely 
492dc87c98eSGrant Likely 	return proxy;
493dc87c98eSGrant Likely }
4948ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device);
4958ae12a0dSDavid Brownell 
4962b9603a0SFeng Tang static void spi_match_master_to_boardinfo(struct spi_master *master,
4972b9603a0SFeng Tang 				struct spi_board_info *bi)
4982b9603a0SFeng Tang {
4992b9603a0SFeng Tang 	struct spi_device *dev;
5002b9603a0SFeng Tang 
5012b9603a0SFeng Tang 	if (master->bus_num != bi->bus_num)
5022b9603a0SFeng Tang 		return;
5032b9603a0SFeng Tang 
5042b9603a0SFeng Tang 	dev = spi_new_device(master, bi);
5052b9603a0SFeng Tang 	if (!dev)
5062b9603a0SFeng Tang 		dev_err(master->dev.parent, "can't create new device for %s\n",
5072b9603a0SFeng Tang 			bi->modalias);
5082b9603a0SFeng Tang }
5092b9603a0SFeng Tang 
51033e34dc6SDavid Brownell /**
51133e34dc6SDavid Brownell  * spi_register_board_info - register SPI devices for a given board
51233e34dc6SDavid Brownell  * @info: array of chip descriptors
51333e34dc6SDavid Brownell  * @n: how many descriptors are provided
51433e34dc6SDavid Brownell  * Context: can sleep
51533e34dc6SDavid Brownell  *
5168ae12a0dSDavid Brownell  * Board-specific early init code calls this (probably during arch_initcall)
5178ae12a0dSDavid Brownell  * with segments of the SPI device table.  Any device nodes are created later,
5188ae12a0dSDavid Brownell  * after the relevant parent SPI controller (bus_num) is defined.  We keep
5198ae12a0dSDavid Brownell  * this table of devices forever, so that reloading a controller driver will
5208ae12a0dSDavid Brownell  * not make Linux forget about these hard-wired devices.
5218ae12a0dSDavid Brownell  *
5228ae12a0dSDavid Brownell  * Other code can also call this, e.g. a particular add-on board might provide
5238ae12a0dSDavid Brownell  * SPI devices through its expansion connector, so code initializing that board
5248ae12a0dSDavid Brownell  * would naturally declare its SPI devices.
5258ae12a0dSDavid Brownell  *
5268ae12a0dSDavid Brownell  * The board info passed can safely be __initdata ... but be careful of
5278ae12a0dSDavid Brownell  * any embedded pointers (platform_data, etc), they're copied as-is.
5288ae12a0dSDavid Brownell  */
529fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n)
5308ae12a0dSDavid Brownell {
5318ae12a0dSDavid Brownell 	struct boardinfo *bi;
5322b9603a0SFeng Tang 	int i;
5338ae12a0dSDavid Brownell 
5342b9603a0SFeng Tang 	bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
5358ae12a0dSDavid Brownell 	if (!bi)
5368ae12a0dSDavid Brownell 		return -ENOMEM;
5378ae12a0dSDavid Brownell 
5382b9603a0SFeng Tang 	for (i = 0; i < n; i++, bi++, info++) {
5392b9603a0SFeng Tang 		struct spi_master *master;
5402b9603a0SFeng Tang 
5412b9603a0SFeng Tang 		memcpy(&bi->board_info, info, sizeof(*info));
54294040828SMatthias Kaehlcke 		mutex_lock(&board_lock);
5438ae12a0dSDavid Brownell 		list_add_tail(&bi->list, &board_list);
5442b9603a0SFeng Tang 		list_for_each_entry(master, &spi_master_list, list)
5452b9603a0SFeng Tang 			spi_match_master_to_boardinfo(master, &bi->board_info);
54694040828SMatthias Kaehlcke 		mutex_unlock(&board_lock);
5472b9603a0SFeng Tang 	}
5482b9603a0SFeng Tang 
5498ae12a0dSDavid Brownell 	return 0;
5508ae12a0dSDavid Brownell }
5518ae12a0dSDavid Brownell 
5528ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
5538ae12a0dSDavid Brownell 
554b158935fSMark Brown static void spi_set_cs(struct spi_device *spi, bool enable)
555b158935fSMark Brown {
556b158935fSMark Brown 	if (spi->mode & SPI_CS_HIGH)
557b158935fSMark Brown 		enable = !enable;
558b158935fSMark Brown 
559b158935fSMark Brown 	if (spi->cs_gpio >= 0)
560b158935fSMark Brown 		gpio_set_value(spi->cs_gpio, !enable);
561b158935fSMark Brown 	else if (spi->master->set_cs)
562b158935fSMark Brown 		spi->master->set_cs(spi, !enable);
563b158935fSMark Brown }
564b158935fSMark Brown 
565b158935fSMark Brown /*
566b158935fSMark Brown  * spi_transfer_one_message - Default implementation of transfer_one_message()
567b158935fSMark Brown  *
568b158935fSMark Brown  * This is a standard implementation of transfer_one_message() for
569b158935fSMark Brown  * drivers which impelment a transfer_one() operation.  It provides
570b158935fSMark Brown  * standard handling of delays and chip select management.
571b158935fSMark Brown  */
572b158935fSMark Brown static int spi_transfer_one_message(struct spi_master *master,
573b158935fSMark Brown 				    struct spi_message *msg)
574b158935fSMark Brown {
575b158935fSMark Brown 	struct spi_transfer *xfer;
576b158935fSMark Brown 	bool cur_cs = true;
577b158935fSMark Brown 	bool keep_cs = false;
578b158935fSMark Brown 	int ret = 0;
579b158935fSMark Brown 
580b158935fSMark Brown 	spi_set_cs(msg->spi, true);
581b158935fSMark Brown 
582b158935fSMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
583b158935fSMark Brown 		trace_spi_transfer_start(msg, xfer);
584b158935fSMark Brown 
58516735d02SWolfram Sang 		reinit_completion(&master->xfer_completion);
586b158935fSMark Brown 
587b158935fSMark Brown 		ret = master->transfer_one(master, msg->spi, xfer);
588b158935fSMark Brown 		if (ret < 0) {
589b158935fSMark Brown 			dev_err(&msg->spi->dev,
590b158935fSMark Brown 				"SPI transfer failed: %d\n", ret);
591b158935fSMark Brown 			goto out;
592b158935fSMark Brown 		}
593b158935fSMark Brown 
594b158935fSMark Brown 		if (ret > 0)
595b158935fSMark Brown 			wait_for_completion(&master->xfer_completion);
596b158935fSMark Brown 
597b158935fSMark Brown 		trace_spi_transfer_stop(msg, xfer);
598b158935fSMark Brown 
599b158935fSMark Brown 		if (msg->status != -EINPROGRESS)
600b158935fSMark Brown 			goto out;
601b158935fSMark Brown 
602b158935fSMark Brown 		if (xfer->delay_usecs)
603b158935fSMark Brown 			udelay(xfer->delay_usecs);
604b158935fSMark Brown 
605b158935fSMark Brown 		if (xfer->cs_change) {
606b158935fSMark Brown 			if (list_is_last(&xfer->transfer_list,
607b158935fSMark Brown 					 &msg->transfers)) {
608b158935fSMark Brown 				keep_cs = true;
609b158935fSMark Brown 			} else {
610b158935fSMark Brown 				cur_cs = !cur_cs;
611b158935fSMark Brown 				spi_set_cs(msg->spi, cur_cs);
612b158935fSMark Brown 			}
613b158935fSMark Brown 		}
614b158935fSMark Brown 
615b158935fSMark Brown 		msg->actual_length += xfer->len;
616b158935fSMark Brown 	}
617b158935fSMark Brown 
618b158935fSMark Brown out:
619b158935fSMark Brown 	if (ret != 0 || !keep_cs)
620b158935fSMark Brown 		spi_set_cs(msg->spi, false);
621b158935fSMark Brown 
622b158935fSMark Brown 	if (msg->status == -EINPROGRESS)
623b158935fSMark Brown 		msg->status = ret;
624b158935fSMark Brown 
625b158935fSMark Brown 	spi_finalize_current_message(master);
626b158935fSMark Brown 
627b158935fSMark Brown 	return ret;
628b158935fSMark Brown }
629b158935fSMark Brown 
630b158935fSMark Brown /**
631b158935fSMark Brown  * spi_finalize_current_transfer - report completion of a transfer
632b158935fSMark Brown  *
633b158935fSMark Brown  * Called by SPI drivers using the core transfer_one_message()
634b158935fSMark Brown  * implementation to notify it that the current interrupt driven
635b158935fSMark Brown  * transfer has finised and the next one may be scheduled.
636b158935fSMark Brown  */
637b158935fSMark Brown void spi_finalize_current_transfer(struct spi_master *master)
638b158935fSMark Brown {
639b158935fSMark Brown 	complete(&master->xfer_completion);
640b158935fSMark Brown }
641b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
642b158935fSMark Brown 
643ffbbdd21SLinus Walleij /**
644ffbbdd21SLinus Walleij  * spi_pump_messages - kthread work function which processes spi message queue
645ffbbdd21SLinus Walleij  * @work: pointer to kthread work struct contained in the master struct
646ffbbdd21SLinus Walleij  *
647ffbbdd21SLinus Walleij  * This function checks if there is any spi message in the queue that
648ffbbdd21SLinus Walleij  * needs processing and if so call out to the driver to initialize hardware
649ffbbdd21SLinus Walleij  * and transfer each message.
650ffbbdd21SLinus Walleij  *
651ffbbdd21SLinus Walleij  */
652ffbbdd21SLinus Walleij static void spi_pump_messages(struct kthread_work *work)
653ffbbdd21SLinus Walleij {
654ffbbdd21SLinus Walleij 	struct spi_master *master =
655ffbbdd21SLinus Walleij 		container_of(work, struct spi_master, pump_messages);
656ffbbdd21SLinus Walleij 	unsigned long flags;
657ffbbdd21SLinus Walleij 	bool was_busy = false;
658ffbbdd21SLinus Walleij 	int ret;
659ffbbdd21SLinus Walleij 
660ffbbdd21SLinus Walleij 	/* Lock queue and check for queue work */
661ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
662ffbbdd21SLinus Walleij 	if (list_empty(&master->queue) || !master->running) {
663b0b36b86SBryan Freed 		if (!master->busy) {
6649af4acc0SDan Carpenter 			spin_unlock_irqrestore(&master->queue_lock, flags);
665ffbbdd21SLinus Walleij 			return;
666ffbbdd21SLinus Walleij 		}
667ffbbdd21SLinus Walleij 		master->busy = false;
668ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
669b0b36b86SBryan Freed 		if (master->unprepare_transfer_hardware &&
670b0b36b86SBryan Freed 		    master->unprepare_transfer_hardware(master))
671b0b36b86SBryan Freed 			dev_err(&master->dev,
672b0b36b86SBryan Freed 				"failed to unprepare transfer hardware\n");
67349834de2SMark Brown 		if (master->auto_runtime_pm) {
67449834de2SMark Brown 			pm_runtime_mark_last_busy(master->dev.parent);
67549834de2SMark Brown 			pm_runtime_put_autosuspend(master->dev.parent);
67649834de2SMark Brown 		}
67756ec1978SMark Brown 		trace_spi_master_idle(master);
678ffbbdd21SLinus Walleij 		return;
679ffbbdd21SLinus Walleij 	}
680ffbbdd21SLinus Walleij 
681ffbbdd21SLinus Walleij 	/* Make sure we are not already running a message */
682ffbbdd21SLinus Walleij 	if (master->cur_msg) {
683ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
684ffbbdd21SLinus Walleij 		return;
685ffbbdd21SLinus Walleij 	}
686ffbbdd21SLinus Walleij 	/* Extract head of queue */
687ffbbdd21SLinus Walleij 	master->cur_msg =
688ffbbdd21SLinus Walleij 	    list_entry(master->queue.next, struct spi_message, queue);
689ffbbdd21SLinus Walleij 
690ffbbdd21SLinus Walleij 	list_del_init(&master->cur_msg->queue);
691ffbbdd21SLinus Walleij 	if (master->busy)
692ffbbdd21SLinus Walleij 		was_busy = true;
693ffbbdd21SLinus Walleij 	else
694ffbbdd21SLinus Walleij 		master->busy = true;
695ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
696ffbbdd21SLinus Walleij 
69749834de2SMark Brown 	if (!was_busy && master->auto_runtime_pm) {
69849834de2SMark Brown 		ret = pm_runtime_get_sync(master->dev.parent);
69949834de2SMark Brown 		if (ret < 0) {
70049834de2SMark Brown 			dev_err(&master->dev, "Failed to power device: %d\n",
70149834de2SMark Brown 				ret);
70249834de2SMark Brown 			return;
70349834de2SMark Brown 		}
70449834de2SMark Brown 	}
70549834de2SMark Brown 
70656ec1978SMark Brown 	if (!was_busy)
70756ec1978SMark Brown 		trace_spi_master_busy(master);
70856ec1978SMark Brown 
7097dfd2bd7SShubhrajyoti D 	if (!was_busy && master->prepare_transfer_hardware) {
710ffbbdd21SLinus Walleij 		ret = master->prepare_transfer_hardware(master);
711ffbbdd21SLinus Walleij 		if (ret) {
712ffbbdd21SLinus Walleij 			dev_err(&master->dev,
713ffbbdd21SLinus Walleij 				"failed to prepare transfer hardware\n");
71449834de2SMark Brown 
71549834de2SMark Brown 			if (master->auto_runtime_pm)
71649834de2SMark Brown 				pm_runtime_put(master->dev.parent);
717ffbbdd21SLinus Walleij 			return;
718ffbbdd21SLinus Walleij 		}
719ffbbdd21SLinus Walleij 	}
720ffbbdd21SLinus Walleij 
72156ec1978SMark Brown 	trace_spi_message_start(master->cur_msg);
72256ec1978SMark Brown 
7232841a5fcSMark Brown 	if (master->prepare_message) {
7242841a5fcSMark Brown 		ret = master->prepare_message(master, master->cur_msg);
7252841a5fcSMark Brown 		if (ret) {
7262841a5fcSMark Brown 			dev_err(&master->dev,
7272841a5fcSMark Brown 				"failed to prepare message: %d\n", ret);
7282841a5fcSMark Brown 			master->cur_msg->status = ret;
7292841a5fcSMark Brown 			spi_finalize_current_message(master);
7302841a5fcSMark Brown 			return;
7312841a5fcSMark Brown 		}
7322841a5fcSMark Brown 		master->cur_msg_prepared = true;
7332841a5fcSMark Brown 	}
7342841a5fcSMark Brown 
735ffbbdd21SLinus Walleij 	ret = master->transfer_one_message(master, master->cur_msg);
736ffbbdd21SLinus Walleij 	if (ret) {
737ffbbdd21SLinus Walleij 		dev_err(&master->dev,
738ffbbdd21SLinus Walleij 			"failed to transfer one message from queue\n");
739ffbbdd21SLinus Walleij 		return;
740ffbbdd21SLinus Walleij 	}
741ffbbdd21SLinus Walleij }
742ffbbdd21SLinus Walleij 
743ffbbdd21SLinus Walleij static int spi_init_queue(struct spi_master *master)
744ffbbdd21SLinus Walleij {
745ffbbdd21SLinus Walleij 	struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
746ffbbdd21SLinus Walleij 
747ffbbdd21SLinus Walleij 	INIT_LIST_HEAD(&master->queue);
748ffbbdd21SLinus Walleij 	spin_lock_init(&master->queue_lock);
749ffbbdd21SLinus Walleij 
750ffbbdd21SLinus Walleij 	master->running = false;
751ffbbdd21SLinus Walleij 	master->busy = false;
752ffbbdd21SLinus Walleij 
753ffbbdd21SLinus Walleij 	init_kthread_worker(&master->kworker);
754ffbbdd21SLinus Walleij 	master->kworker_task = kthread_run(kthread_worker_fn,
755f170168bSKees Cook 					   &master->kworker, "%s",
756ffbbdd21SLinus Walleij 					   dev_name(&master->dev));
757ffbbdd21SLinus Walleij 	if (IS_ERR(master->kworker_task)) {
758ffbbdd21SLinus Walleij 		dev_err(&master->dev, "failed to create message pump task\n");
759ffbbdd21SLinus Walleij 		return -ENOMEM;
760ffbbdd21SLinus Walleij 	}
761ffbbdd21SLinus Walleij 	init_kthread_work(&master->pump_messages, spi_pump_messages);
762ffbbdd21SLinus Walleij 
763ffbbdd21SLinus Walleij 	/*
764ffbbdd21SLinus Walleij 	 * Master config will indicate if this controller should run the
765ffbbdd21SLinus Walleij 	 * message pump with high (realtime) priority to reduce the transfer
766ffbbdd21SLinus Walleij 	 * latency on the bus by minimising the delay between a transfer
767ffbbdd21SLinus Walleij 	 * request and the scheduling of the message pump thread. Without this
768ffbbdd21SLinus Walleij 	 * setting the message pump thread will remain at default priority.
769ffbbdd21SLinus Walleij 	 */
770ffbbdd21SLinus Walleij 	if (master->rt) {
771ffbbdd21SLinus Walleij 		dev_info(&master->dev,
772ffbbdd21SLinus Walleij 			"will run message pump with realtime priority\n");
773ffbbdd21SLinus Walleij 		sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
774ffbbdd21SLinus Walleij 	}
775ffbbdd21SLinus Walleij 
776ffbbdd21SLinus Walleij 	return 0;
777ffbbdd21SLinus Walleij }
778ffbbdd21SLinus Walleij 
779ffbbdd21SLinus Walleij /**
780ffbbdd21SLinus Walleij  * spi_get_next_queued_message() - called by driver to check for queued
781ffbbdd21SLinus Walleij  * messages
782ffbbdd21SLinus Walleij  * @master: the master to check for queued messages
783ffbbdd21SLinus Walleij  *
784ffbbdd21SLinus Walleij  * If there are more messages in the queue, the next message is returned from
785ffbbdd21SLinus Walleij  * this call.
786ffbbdd21SLinus Walleij  */
787ffbbdd21SLinus Walleij struct spi_message *spi_get_next_queued_message(struct spi_master *master)
788ffbbdd21SLinus Walleij {
789ffbbdd21SLinus Walleij 	struct spi_message *next;
790ffbbdd21SLinus Walleij 	unsigned long flags;
791ffbbdd21SLinus Walleij 
792ffbbdd21SLinus Walleij 	/* get a pointer to the next message, if any */
793ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
794*1cfd97f9SAxel Lin 	next = list_first_entry_or_null(&master->queue, struct spi_message,
795*1cfd97f9SAxel Lin 					queue);
796ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
797ffbbdd21SLinus Walleij 
798ffbbdd21SLinus Walleij 	return next;
799ffbbdd21SLinus Walleij }
800ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
801ffbbdd21SLinus Walleij 
802ffbbdd21SLinus Walleij /**
803ffbbdd21SLinus Walleij  * spi_finalize_current_message() - the current message is complete
804ffbbdd21SLinus Walleij  * @master: the master to return the message to
805ffbbdd21SLinus Walleij  *
806ffbbdd21SLinus Walleij  * Called by the driver to notify the core that the message in the front of the
807ffbbdd21SLinus Walleij  * queue is complete and can be removed from the queue.
808ffbbdd21SLinus Walleij  */
809ffbbdd21SLinus Walleij void spi_finalize_current_message(struct spi_master *master)
810ffbbdd21SLinus Walleij {
811ffbbdd21SLinus Walleij 	struct spi_message *mesg;
812ffbbdd21SLinus Walleij 	unsigned long flags;
8132841a5fcSMark Brown 	int ret;
814ffbbdd21SLinus Walleij 
815ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
816ffbbdd21SLinus Walleij 	mesg = master->cur_msg;
817ffbbdd21SLinus Walleij 	master->cur_msg = NULL;
818ffbbdd21SLinus Walleij 
819ffbbdd21SLinus Walleij 	queue_kthread_work(&master->kworker, &master->pump_messages);
820ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
821ffbbdd21SLinus Walleij 
8222841a5fcSMark Brown 	if (master->cur_msg_prepared && master->unprepare_message) {
8232841a5fcSMark Brown 		ret = master->unprepare_message(master, mesg);
8242841a5fcSMark Brown 		if (ret) {
8252841a5fcSMark Brown 			dev_err(&master->dev,
8262841a5fcSMark Brown 				"failed to unprepare message: %d\n", ret);
8272841a5fcSMark Brown 		}
8282841a5fcSMark Brown 	}
8292841a5fcSMark Brown 	master->cur_msg_prepared = false;
8302841a5fcSMark Brown 
831ffbbdd21SLinus Walleij 	mesg->state = NULL;
832ffbbdd21SLinus Walleij 	if (mesg->complete)
833ffbbdd21SLinus Walleij 		mesg->complete(mesg->context);
83456ec1978SMark Brown 
83556ec1978SMark Brown 	trace_spi_message_done(mesg);
836ffbbdd21SLinus Walleij }
837ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message);
838ffbbdd21SLinus Walleij 
839ffbbdd21SLinus Walleij static int spi_start_queue(struct spi_master *master)
840ffbbdd21SLinus Walleij {
841ffbbdd21SLinus Walleij 	unsigned long flags;
842ffbbdd21SLinus Walleij 
843ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
844ffbbdd21SLinus Walleij 
845ffbbdd21SLinus Walleij 	if (master->running || master->busy) {
846ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
847ffbbdd21SLinus Walleij 		return -EBUSY;
848ffbbdd21SLinus Walleij 	}
849ffbbdd21SLinus Walleij 
850ffbbdd21SLinus Walleij 	master->running = true;
851ffbbdd21SLinus Walleij 	master->cur_msg = NULL;
852ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
853ffbbdd21SLinus Walleij 
854ffbbdd21SLinus Walleij 	queue_kthread_work(&master->kworker, &master->pump_messages);
855ffbbdd21SLinus Walleij 
856ffbbdd21SLinus Walleij 	return 0;
857ffbbdd21SLinus Walleij }
858ffbbdd21SLinus Walleij 
859ffbbdd21SLinus Walleij static int spi_stop_queue(struct spi_master *master)
860ffbbdd21SLinus Walleij {
861ffbbdd21SLinus Walleij 	unsigned long flags;
862ffbbdd21SLinus Walleij 	unsigned limit = 500;
863ffbbdd21SLinus Walleij 	int ret = 0;
864ffbbdd21SLinus Walleij 
865ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
866ffbbdd21SLinus Walleij 
867ffbbdd21SLinus Walleij 	/*
868ffbbdd21SLinus Walleij 	 * This is a bit lame, but is optimized for the common execution path.
869ffbbdd21SLinus Walleij 	 * A wait_queue on the master->busy could be used, but then the common
870ffbbdd21SLinus Walleij 	 * execution path (pump_messages) would be required to call wake_up or
871ffbbdd21SLinus Walleij 	 * friends on every SPI message. Do this instead.
872ffbbdd21SLinus Walleij 	 */
873ffbbdd21SLinus Walleij 	while ((!list_empty(&master->queue) || master->busy) && limit--) {
874ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
875ffbbdd21SLinus Walleij 		msleep(10);
876ffbbdd21SLinus Walleij 		spin_lock_irqsave(&master->queue_lock, flags);
877ffbbdd21SLinus Walleij 	}
878ffbbdd21SLinus Walleij 
879ffbbdd21SLinus Walleij 	if (!list_empty(&master->queue) || master->busy)
880ffbbdd21SLinus Walleij 		ret = -EBUSY;
881ffbbdd21SLinus Walleij 	else
882ffbbdd21SLinus Walleij 		master->running = false;
883ffbbdd21SLinus Walleij 
884ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
885ffbbdd21SLinus Walleij 
886ffbbdd21SLinus Walleij 	if (ret) {
887ffbbdd21SLinus Walleij 		dev_warn(&master->dev,
888ffbbdd21SLinus Walleij 			 "could not stop message queue\n");
889ffbbdd21SLinus Walleij 		return ret;
890ffbbdd21SLinus Walleij 	}
891ffbbdd21SLinus Walleij 	return ret;
892ffbbdd21SLinus Walleij }
893ffbbdd21SLinus Walleij 
894ffbbdd21SLinus Walleij static int spi_destroy_queue(struct spi_master *master)
895ffbbdd21SLinus Walleij {
896ffbbdd21SLinus Walleij 	int ret;
897ffbbdd21SLinus Walleij 
898ffbbdd21SLinus Walleij 	ret = spi_stop_queue(master);
899ffbbdd21SLinus Walleij 
900ffbbdd21SLinus Walleij 	/*
901ffbbdd21SLinus Walleij 	 * flush_kthread_worker will block until all work is done.
902ffbbdd21SLinus Walleij 	 * If the reason that stop_queue timed out is that the work will never
903ffbbdd21SLinus Walleij 	 * finish, then it does no good to call flush/stop thread, so
904ffbbdd21SLinus Walleij 	 * return anyway.
905ffbbdd21SLinus Walleij 	 */
906ffbbdd21SLinus Walleij 	if (ret) {
907ffbbdd21SLinus Walleij 		dev_err(&master->dev, "problem destroying queue\n");
908ffbbdd21SLinus Walleij 		return ret;
909ffbbdd21SLinus Walleij 	}
910ffbbdd21SLinus Walleij 
911ffbbdd21SLinus Walleij 	flush_kthread_worker(&master->kworker);
912ffbbdd21SLinus Walleij 	kthread_stop(master->kworker_task);
913ffbbdd21SLinus Walleij 
914ffbbdd21SLinus Walleij 	return 0;
915ffbbdd21SLinus Walleij }
916ffbbdd21SLinus Walleij 
917ffbbdd21SLinus Walleij /**
918ffbbdd21SLinus Walleij  * spi_queued_transfer - transfer function for queued transfers
919ffbbdd21SLinus Walleij  * @spi: spi device which is requesting transfer
920ffbbdd21SLinus Walleij  * @msg: spi message which is to handled is queued to driver queue
921ffbbdd21SLinus Walleij  */
922ffbbdd21SLinus Walleij static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
923ffbbdd21SLinus Walleij {
924ffbbdd21SLinus Walleij 	struct spi_master *master = spi->master;
925ffbbdd21SLinus Walleij 	unsigned long flags;
926ffbbdd21SLinus Walleij 
927ffbbdd21SLinus Walleij 	spin_lock_irqsave(&master->queue_lock, flags);
928ffbbdd21SLinus Walleij 
929ffbbdd21SLinus Walleij 	if (!master->running) {
930ffbbdd21SLinus Walleij 		spin_unlock_irqrestore(&master->queue_lock, flags);
931ffbbdd21SLinus Walleij 		return -ESHUTDOWN;
932ffbbdd21SLinus Walleij 	}
933ffbbdd21SLinus Walleij 	msg->actual_length = 0;
934ffbbdd21SLinus Walleij 	msg->status = -EINPROGRESS;
935ffbbdd21SLinus Walleij 
936ffbbdd21SLinus Walleij 	list_add_tail(&msg->queue, &master->queue);
93796b3eaceSAxel Lin 	if (!master->busy)
938ffbbdd21SLinus Walleij 		queue_kthread_work(&master->kworker, &master->pump_messages);
939ffbbdd21SLinus Walleij 
940ffbbdd21SLinus Walleij 	spin_unlock_irqrestore(&master->queue_lock, flags);
941ffbbdd21SLinus Walleij 	return 0;
942ffbbdd21SLinus Walleij }
943ffbbdd21SLinus Walleij 
944ffbbdd21SLinus Walleij static int spi_master_initialize_queue(struct spi_master *master)
945ffbbdd21SLinus Walleij {
946ffbbdd21SLinus Walleij 	int ret;
947ffbbdd21SLinus Walleij 
948ffbbdd21SLinus Walleij 	master->queued = true;
949ffbbdd21SLinus Walleij 	master->transfer = spi_queued_transfer;
950b158935fSMark Brown 	if (!master->transfer_one_message)
951b158935fSMark Brown 		master->transfer_one_message = spi_transfer_one_message;
952ffbbdd21SLinus Walleij 
953ffbbdd21SLinus Walleij 	/* Initialize and start queue */
954ffbbdd21SLinus Walleij 	ret = spi_init_queue(master);
955ffbbdd21SLinus Walleij 	if (ret) {
956ffbbdd21SLinus Walleij 		dev_err(&master->dev, "problem initializing queue\n");
957ffbbdd21SLinus Walleij 		goto err_init_queue;
958ffbbdd21SLinus Walleij 	}
959ffbbdd21SLinus Walleij 	ret = spi_start_queue(master);
960ffbbdd21SLinus Walleij 	if (ret) {
961ffbbdd21SLinus Walleij 		dev_err(&master->dev, "problem starting queue\n");
962ffbbdd21SLinus Walleij 		goto err_start_queue;
963ffbbdd21SLinus Walleij 	}
964ffbbdd21SLinus Walleij 
965ffbbdd21SLinus Walleij 	return 0;
966ffbbdd21SLinus Walleij 
967ffbbdd21SLinus Walleij err_start_queue:
968ffbbdd21SLinus Walleij err_init_queue:
969ffbbdd21SLinus Walleij 	spi_destroy_queue(master);
970ffbbdd21SLinus Walleij 	return ret;
971ffbbdd21SLinus Walleij }
972ffbbdd21SLinus Walleij 
973ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/
974ffbbdd21SLinus Walleij 
9757cb94361SAndreas Larsson #if defined(CONFIG_OF)
976d57a4282SGrant Likely /**
977d57a4282SGrant Likely  * of_register_spi_devices() - Register child devices onto the SPI bus
978d57a4282SGrant Likely  * @master:	Pointer to spi_master device
979d57a4282SGrant Likely  *
980d57a4282SGrant Likely  * Registers an spi_device for each child node of master node which has a 'reg'
981d57a4282SGrant Likely  * property.
982d57a4282SGrant Likely  */
983d57a4282SGrant Likely static void of_register_spi_devices(struct spi_master *master)
984d57a4282SGrant Likely {
985d57a4282SGrant Likely 	struct spi_device *spi;
986d57a4282SGrant Likely 	struct device_node *nc;
987d57a4282SGrant Likely 	int rc;
98889da4293STrent Piepho 	u32 value;
989d57a4282SGrant Likely 
990d57a4282SGrant Likely 	if (!master->dev.of_node)
991d57a4282SGrant Likely 		return;
992d57a4282SGrant Likely 
993f3b6159eSAlexander Sverdlin 	for_each_available_child_of_node(master->dev.of_node, nc) {
994d57a4282SGrant Likely 		/* Alloc an spi_device */
995d57a4282SGrant Likely 		spi = spi_alloc_device(master);
996d57a4282SGrant Likely 		if (!spi) {
997d57a4282SGrant Likely 			dev_err(&master->dev, "spi_device alloc error for %s\n",
998d57a4282SGrant Likely 				nc->full_name);
999d57a4282SGrant Likely 			spi_dev_put(spi);
1000d57a4282SGrant Likely 			continue;
1001d57a4282SGrant Likely 		}
1002d57a4282SGrant Likely 
1003d57a4282SGrant Likely 		/* Select device driver */
1004d57a4282SGrant Likely 		if (of_modalias_node(nc, spi->modalias,
1005d57a4282SGrant Likely 				     sizeof(spi->modalias)) < 0) {
1006d57a4282SGrant Likely 			dev_err(&master->dev, "cannot find modalias for %s\n",
1007d57a4282SGrant Likely 				nc->full_name);
1008d57a4282SGrant Likely 			spi_dev_put(spi);
1009d57a4282SGrant Likely 			continue;
1010d57a4282SGrant Likely 		}
1011d57a4282SGrant Likely 
1012d57a4282SGrant Likely 		/* Device address */
101389da4293STrent Piepho 		rc = of_property_read_u32(nc, "reg", &value);
101489da4293STrent Piepho 		if (rc) {
101589da4293STrent Piepho 			dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
101689da4293STrent Piepho 				nc->full_name, rc);
1017d57a4282SGrant Likely 			spi_dev_put(spi);
1018d57a4282SGrant Likely 			continue;
1019d57a4282SGrant Likely 		}
102089da4293STrent Piepho 		spi->chip_select = value;
1021d57a4282SGrant Likely 
1022d57a4282SGrant Likely 		/* Mode (clock phase/polarity/etc.) */
1023d57a4282SGrant Likely 		if (of_find_property(nc, "spi-cpha", NULL))
1024d57a4282SGrant Likely 			spi->mode |= SPI_CPHA;
1025d57a4282SGrant Likely 		if (of_find_property(nc, "spi-cpol", NULL))
1026d57a4282SGrant Likely 			spi->mode |= SPI_CPOL;
1027d57a4282SGrant Likely 		if (of_find_property(nc, "spi-cs-high", NULL))
1028d57a4282SGrant Likely 			spi->mode |= SPI_CS_HIGH;
1029c20151dfSLars-Peter Clausen 		if (of_find_property(nc, "spi-3wire", NULL))
1030c20151dfSLars-Peter Clausen 			spi->mode |= SPI_3WIRE;
1031d57a4282SGrant Likely 
1032f477b7fbSwangyuhang 		/* Device DUAL/QUAD mode */
103389da4293STrent Piepho 		if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
103489da4293STrent Piepho 			switch (value) {
103589da4293STrent Piepho 			case 1:
1036f477b7fbSwangyuhang 				break;
103789da4293STrent Piepho 			case 2:
1038f477b7fbSwangyuhang 				spi->mode |= SPI_TX_DUAL;
1039f477b7fbSwangyuhang 				break;
104089da4293STrent Piepho 			case 4:
1041f477b7fbSwangyuhang 				spi->mode |= SPI_TX_QUAD;
1042f477b7fbSwangyuhang 				break;
1043f477b7fbSwangyuhang 			default:
1044a822e99cSMark Brown 				dev_err(&master->dev,
1045a110f93dSwangyuhang 					"spi-tx-bus-width %d not supported\n",
104689da4293STrent Piepho 					value);
1047f477b7fbSwangyuhang 				spi_dev_put(spi);
1048f477b7fbSwangyuhang 				continue;
1049f477b7fbSwangyuhang 			}
1050a822e99cSMark Brown 		}
1051f477b7fbSwangyuhang 
105289da4293STrent Piepho 		if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
105389da4293STrent Piepho 			switch (value) {
105489da4293STrent Piepho 			case 1:
1055f477b7fbSwangyuhang 				break;
105689da4293STrent Piepho 			case 2:
1057f477b7fbSwangyuhang 				spi->mode |= SPI_RX_DUAL;
1058f477b7fbSwangyuhang 				break;
105989da4293STrent Piepho 			case 4:
1060f477b7fbSwangyuhang 				spi->mode |= SPI_RX_QUAD;
1061f477b7fbSwangyuhang 				break;
1062f477b7fbSwangyuhang 			default:
1063a822e99cSMark Brown 				dev_err(&master->dev,
1064a110f93dSwangyuhang 					"spi-rx-bus-width %d not supported\n",
106589da4293STrent Piepho 					value);
1066f477b7fbSwangyuhang 				spi_dev_put(spi);
1067f477b7fbSwangyuhang 				continue;
1068f477b7fbSwangyuhang 			}
1069a822e99cSMark Brown 		}
1070f477b7fbSwangyuhang 
1071d57a4282SGrant Likely 		/* Device speed */
107289da4293STrent Piepho 		rc = of_property_read_u32(nc, "spi-max-frequency", &value);
107389da4293STrent Piepho 		if (rc) {
107489da4293STrent Piepho 			dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
107589da4293STrent Piepho 				nc->full_name, rc);
1076d57a4282SGrant Likely 			spi_dev_put(spi);
1077d57a4282SGrant Likely 			continue;
1078d57a4282SGrant Likely 		}
107989da4293STrent Piepho 		spi->max_speed_hz = value;
1080d57a4282SGrant Likely 
1081d57a4282SGrant Likely 		/* IRQ */
1082d57a4282SGrant Likely 		spi->irq = irq_of_parse_and_map(nc, 0);
1083d57a4282SGrant Likely 
1084d57a4282SGrant Likely 		/* Store a pointer to the node in the device structure */
1085d57a4282SGrant Likely 		of_node_get(nc);
1086d57a4282SGrant Likely 		spi->dev.of_node = nc;
1087d57a4282SGrant Likely 
1088d57a4282SGrant Likely 		/* Register the new device */
108970fac17cSMathias Krause 		request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
1090d57a4282SGrant Likely 		rc = spi_add_device(spi);
1091d57a4282SGrant Likely 		if (rc) {
1092d57a4282SGrant Likely 			dev_err(&master->dev, "spi_device register error %s\n",
1093d57a4282SGrant Likely 				nc->full_name);
1094d57a4282SGrant Likely 			spi_dev_put(spi);
1095d57a4282SGrant Likely 		}
1096d57a4282SGrant Likely 
1097d57a4282SGrant Likely 	}
1098d57a4282SGrant Likely }
1099d57a4282SGrant Likely #else
1100d57a4282SGrant Likely static void of_register_spi_devices(struct spi_master *master) { }
1101d57a4282SGrant Likely #endif
1102d57a4282SGrant Likely 
110364bee4d2SMika Westerberg #ifdef CONFIG_ACPI
110464bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
110564bee4d2SMika Westerberg {
110664bee4d2SMika Westerberg 	struct spi_device *spi = data;
110764bee4d2SMika Westerberg 
110864bee4d2SMika Westerberg 	if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
110964bee4d2SMika Westerberg 		struct acpi_resource_spi_serialbus *sb;
111064bee4d2SMika Westerberg 
111164bee4d2SMika Westerberg 		sb = &ares->data.spi_serial_bus;
111264bee4d2SMika Westerberg 		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
111364bee4d2SMika Westerberg 			spi->chip_select = sb->device_selection;
111464bee4d2SMika Westerberg 			spi->max_speed_hz = sb->connection_speed;
111564bee4d2SMika Westerberg 
111664bee4d2SMika Westerberg 			if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
111764bee4d2SMika Westerberg 				spi->mode |= SPI_CPHA;
111864bee4d2SMika Westerberg 			if (sb->clock_polarity == ACPI_SPI_START_HIGH)
111964bee4d2SMika Westerberg 				spi->mode |= SPI_CPOL;
112064bee4d2SMika Westerberg 			if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
112164bee4d2SMika Westerberg 				spi->mode |= SPI_CS_HIGH;
112264bee4d2SMika Westerberg 		}
112364bee4d2SMika Westerberg 	} else if (spi->irq < 0) {
112464bee4d2SMika Westerberg 		struct resource r;
112564bee4d2SMika Westerberg 
112664bee4d2SMika Westerberg 		if (acpi_dev_resource_interrupt(ares, 0, &r))
112764bee4d2SMika Westerberg 			spi->irq = r.start;
112864bee4d2SMika Westerberg 	}
112964bee4d2SMika Westerberg 
113064bee4d2SMika Westerberg 	/* Always tell the ACPI core to skip this resource */
113164bee4d2SMika Westerberg 	return 1;
113264bee4d2SMika Westerberg }
113364bee4d2SMika Westerberg 
113464bee4d2SMika Westerberg static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
113564bee4d2SMika Westerberg 				       void *data, void **return_value)
113664bee4d2SMika Westerberg {
113764bee4d2SMika Westerberg 	struct spi_master *master = data;
113864bee4d2SMika Westerberg 	struct list_head resource_list;
113964bee4d2SMika Westerberg 	struct acpi_device *adev;
114064bee4d2SMika Westerberg 	struct spi_device *spi;
114164bee4d2SMika Westerberg 	int ret;
114264bee4d2SMika Westerberg 
114364bee4d2SMika Westerberg 	if (acpi_bus_get_device(handle, &adev))
114464bee4d2SMika Westerberg 		return AE_OK;
114564bee4d2SMika Westerberg 	if (acpi_bus_get_status(adev) || !adev->status.present)
114664bee4d2SMika Westerberg 		return AE_OK;
114764bee4d2SMika Westerberg 
114864bee4d2SMika Westerberg 	spi = spi_alloc_device(master);
114964bee4d2SMika Westerberg 	if (!spi) {
115064bee4d2SMika Westerberg 		dev_err(&master->dev, "failed to allocate SPI device for %s\n",
115164bee4d2SMika Westerberg 			dev_name(&adev->dev));
115264bee4d2SMika Westerberg 		return AE_NO_MEMORY;
115364bee4d2SMika Westerberg 	}
115464bee4d2SMika Westerberg 
11557b199811SRafael J. Wysocki 	ACPI_COMPANION_SET(&spi->dev, adev);
115664bee4d2SMika Westerberg 	spi->irq = -1;
115764bee4d2SMika Westerberg 
115864bee4d2SMika Westerberg 	INIT_LIST_HEAD(&resource_list);
115964bee4d2SMika Westerberg 	ret = acpi_dev_get_resources(adev, &resource_list,
116064bee4d2SMika Westerberg 				     acpi_spi_add_resource, spi);
116164bee4d2SMika Westerberg 	acpi_dev_free_resource_list(&resource_list);
116264bee4d2SMika Westerberg 
116364bee4d2SMika Westerberg 	if (ret < 0 || !spi->max_speed_hz) {
116464bee4d2SMika Westerberg 		spi_dev_put(spi);
116564bee4d2SMika Westerberg 		return AE_OK;
116664bee4d2SMika Westerberg 	}
116764bee4d2SMika Westerberg 
116833cf00e5SMika Westerberg 	adev->power.flags.ignore_parent = true;
1169cf9eb39cSJarkko Nikula 	strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
117064bee4d2SMika Westerberg 	if (spi_add_device(spi)) {
117133cf00e5SMika Westerberg 		adev->power.flags.ignore_parent = false;
117264bee4d2SMika Westerberg 		dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
117364bee4d2SMika Westerberg 			dev_name(&adev->dev));
117464bee4d2SMika Westerberg 		spi_dev_put(spi);
117564bee4d2SMika Westerberg 	}
117664bee4d2SMika Westerberg 
117764bee4d2SMika Westerberg 	return AE_OK;
117864bee4d2SMika Westerberg }
117964bee4d2SMika Westerberg 
118064bee4d2SMika Westerberg static void acpi_register_spi_devices(struct spi_master *master)
118164bee4d2SMika Westerberg {
118264bee4d2SMika Westerberg 	acpi_status status;
118364bee4d2SMika Westerberg 	acpi_handle handle;
118464bee4d2SMika Westerberg 
118529896178SRafael J. Wysocki 	handle = ACPI_HANDLE(master->dev.parent);
118664bee4d2SMika Westerberg 	if (!handle)
118764bee4d2SMika Westerberg 		return;
118864bee4d2SMika Westerberg 
118964bee4d2SMika Westerberg 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
119064bee4d2SMika Westerberg 				     acpi_spi_add_device, NULL,
119164bee4d2SMika Westerberg 				     master, NULL);
119264bee4d2SMika Westerberg 	if (ACPI_FAILURE(status))
119364bee4d2SMika Westerberg 		dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
119464bee4d2SMika Westerberg }
119564bee4d2SMika Westerberg #else
119664bee4d2SMika Westerberg static inline void acpi_register_spi_devices(struct spi_master *master) {}
119764bee4d2SMika Westerberg #endif /* CONFIG_ACPI */
119864bee4d2SMika Westerberg 
119949dce689STony Jones static void spi_master_release(struct device *dev)
12008ae12a0dSDavid Brownell {
12018ae12a0dSDavid Brownell 	struct spi_master *master;
12028ae12a0dSDavid Brownell 
120349dce689STony Jones 	master = container_of(dev, struct spi_master, dev);
12048ae12a0dSDavid Brownell 	kfree(master);
12058ae12a0dSDavid Brownell }
12068ae12a0dSDavid Brownell 
12078ae12a0dSDavid Brownell static struct class spi_master_class = {
12088ae12a0dSDavid Brownell 	.name		= "spi_master",
12098ae12a0dSDavid Brownell 	.owner		= THIS_MODULE,
121049dce689STony Jones 	.dev_release	= spi_master_release,
12118ae12a0dSDavid Brownell };
12128ae12a0dSDavid Brownell 
12138ae12a0dSDavid Brownell 
1214ffbbdd21SLinus Walleij 
12158ae12a0dSDavid Brownell /**
12168ae12a0dSDavid Brownell  * spi_alloc_master - allocate SPI master controller
12178ae12a0dSDavid Brownell  * @dev: the controller, possibly using the platform_bus
121833e34dc6SDavid Brownell  * @size: how much zeroed driver-private data to allocate; the pointer to this
121949dce689STony Jones  *	memory is in the driver_data field of the returned device,
12200c868461SDavid Brownell  *	accessible with spi_master_get_devdata().
122133e34dc6SDavid Brownell  * Context: can sleep
12228ae12a0dSDavid Brownell  *
12238ae12a0dSDavid Brownell  * This call is used only by SPI master controller drivers, which are the
12248ae12a0dSDavid Brownell  * only ones directly touching chip registers.  It's how they allocate
1225ba1a0513Sdmitry pervushin  * an spi_master structure, prior to calling spi_register_master().
12268ae12a0dSDavid Brownell  *
12278ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns the SPI
12288ae12a0dSDavid Brownell  * master structure on success, else NULL.
12298ae12a0dSDavid Brownell  *
12308ae12a0dSDavid Brownell  * The caller is responsible for assigning the bus number and initializing
1231ba1a0513Sdmitry pervushin  * the master's methods before calling spi_register_master(); and (after errors
1232eb4af0f5SUwe Kleine-König  * adding the device) calling spi_master_put() and kfree() to prevent a memory
1233eb4af0f5SUwe Kleine-König  * leak.
12348ae12a0dSDavid Brownell  */
1235e9d5a461SAdrian Bunk struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
12368ae12a0dSDavid Brownell {
12378ae12a0dSDavid Brownell 	struct spi_master	*master;
12388ae12a0dSDavid Brownell 
12390c868461SDavid Brownell 	if (!dev)
12400c868461SDavid Brownell 		return NULL;
12410c868461SDavid Brownell 
12425fe5f05eSJingoo Han 	master = kzalloc(size + sizeof(*master), GFP_KERNEL);
12438ae12a0dSDavid Brownell 	if (!master)
12448ae12a0dSDavid Brownell 		return NULL;
12458ae12a0dSDavid Brownell 
124649dce689STony Jones 	device_initialize(&master->dev);
12471e8a52e1SGrant Likely 	master->bus_num = -1;
12481e8a52e1SGrant Likely 	master->num_chipselect = 1;
124949dce689STony Jones 	master->dev.class = &spi_master_class;
125049dce689STony Jones 	master->dev.parent = get_device(dev);
12510c868461SDavid Brownell 	spi_master_set_devdata(master, &master[1]);
12528ae12a0dSDavid Brownell 
12538ae12a0dSDavid Brownell 	return master;
12548ae12a0dSDavid Brownell }
12558ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_alloc_master);
12568ae12a0dSDavid Brownell 
125774317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF
125874317984SJean-Christophe PLAGNIOL-VILLARD static int of_spi_register_master(struct spi_master *master)
125974317984SJean-Christophe PLAGNIOL-VILLARD {
1260e80beb27SGrant Likely 	int nb, i, *cs;
126174317984SJean-Christophe PLAGNIOL-VILLARD 	struct device_node *np = master->dev.of_node;
126274317984SJean-Christophe PLAGNIOL-VILLARD 
126374317984SJean-Christophe PLAGNIOL-VILLARD 	if (!np)
126474317984SJean-Christophe PLAGNIOL-VILLARD 		return 0;
126574317984SJean-Christophe PLAGNIOL-VILLARD 
126674317984SJean-Christophe PLAGNIOL-VILLARD 	nb = of_gpio_named_count(np, "cs-gpios");
12675fe5f05eSJingoo Han 	master->num_chipselect = max_t(int, nb, master->num_chipselect);
126874317984SJean-Christophe PLAGNIOL-VILLARD 
12698ec5d84eSAndreas Larsson 	/* Return error only for an incorrectly formed cs-gpios property */
12708ec5d84eSAndreas Larsson 	if (nb == 0 || nb == -ENOENT)
127174317984SJean-Christophe PLAGNIOL-VILLARD 		return 0;
12728ec5d84eSAndreas Larsson 	else if (nb < 0)
12738ec5d84eSAndreas Larsson 		return nb;
127474317984SJean-Christophe PLAGNIOL-VILLARD 
127574317984SJean-Christophe PLAGNIOL-VILLARD 	cs = devm_kzalloc(&master->dev,
127674317984SJean-Christophe PLAGNIOL-VILLARD 			  sizeof(int) * master->num_chipselect,
127774317984SJean-Christophe PLAGNIOL-VILLARD 			  GFP_KERNEL);
127874317984SJean-Christophe PLAGNIOL-VILLARD 	master->cs_gpios = cs;
127974317984SJean-Christophe PLAGNIOL-VILLARD 
128074317984SJean-Christophe PLAGNIOL-VILLARD 	if (!master->cs_gpios)
128174317984SJean-Christophe PLAGNIOL-VILLARD 		return -ENOMEM;
128274317984SJean-Christophe PLAGNIOL-VILLARD 
12830da83bb1SAndreas Larsson 	for (i = 0; i < master->num_chipselect; i++)
1284446411e1SAndreas Larsson 		cs[i] = -ENOENT;
128574317984SJean-Christophe PLAGNIOL-VILLARD 
128674317984SJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < nb; i++)
128774317984SJean-Christophe PLAGNIOL-VILLARD 		cs[i] = of_get_named_gpio(np, "cs-gpios", i);
128874317984SJean-Christophe PLAGNIOL-VILLARD 
128974317984SJean-Christophe PLAGNIOL-VILLARD 	return 0;
129074317984SJean-Christophe PLAGNIOL-VILLARD }
129174317984SJean-Christophe PLAGNIOL-VILLARD #else
129274317984SJean-Christophe PLAGNIOL-VILLARD static int of_spi_register_master(struct spi_master *master)
129374317984SJean-Christophe PLAGNIOL-VILLARD {
129474317984SJean-Christophe PLAGNIOL-VILLARD 	return 0;
129574317984SJean-Christophe PLAGNIOL-VILLARD }
129674317984SJean-Christophe PLAGNIOL-VILLARD #endif
129774317984SJean-Christophe PLAGNIOL-VILLARD 
12988ae12a0dSDavid Brownell /**
12998ae12a0dSDavid Brownell  * spi_register_master - register SPI master controller
13008ae12a0dSDavid Brownell  * @master: initialized master, originally from spi_alloc_master()
130133e34dc6SDavid Brownell  * Context: can sleep
13028ae12a0dSDavid Brownell  *
13038ae12a0dSDavid Brownell  * SPI master controllers connect to their drivers using some non-SPI bus,
13048ae12a0dSDavid Brownell  * such as the platform bus.  The final stage of probe() in that code
13058ae12a0dSDavid Brownell  * includes calling spi_register_master() to hook up to this SPI bus glue.
13068ae12a0dSDavid Brownell  *
13078ae12a0dSDavid Brownell  * SPI controllers use board specific (often SOC specific) bus numbers,
13088ae12a0dSDavid Brownell  * and board-specific addressing for SPI devices combines those numbers
13098ae12a0dSDavid Brownell  * with chip select numbers.  Since SPI does not directly support dynamic
13108ae12a0dSDavid Brownell  * device identification, boards need configuration tables telling which
13118ae12a0dSDavid Brownell  * chip is at which address.
13128ae12a0dSDavid Brownell  *
13138ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns zero on
13148ae12a0dSDavid Brownell  * success, else a negative error code (dropping the master's refcount).
13150c868461SDavid Brownell  * After a successful return, the caller is responsible for calling
13160c868461SDavid Brownell  * spi_unregister_master().
13178ae12a0dSDavid Brownell  */
1318e9d5a461SAdrian Bunk int spi_register_master(struct spi_master *master)
13198ae12a0dSDavid Brownell {
1320e44a45aeSDavid Brownell 	static atomic_t		dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
132149dce689STony Jones 	struct device		*dev = master->dev.parent;
13222b9603a0SFeng Tang 	struct boardinfo	*bi;
13238ae12a0dSDavid Brownell 	int			status = -ENODEV;
13248ae12a0dSDavid Brownell 	int			dynamic = 0;
13258ae12a0dSDavid Brownell 
13260c868461SDavid Brownell 	if (!dev)
13270c868461SDavid Brownell 		return -ENODEV;
13280c868461SDavid Brownell 
132974317984SJean-Christophe PLAGNIOL-VILLARD 	status = of_spi_register_master(master);
133074317984SJean-Christophe PLAGNIOL-VILLARD 	if (status)
133174317984SJean-Christophe PLAGNIOL-VILLARD 		return status;
133274317984SJean-Christophe PLAGNIOL-VILLARD 
1333082c8cb4SDavid Brownell 	/* even if it's just one always-selected device, there must
1334082c8cb4SDavid Brownell 	 * be at least one chipselect
1335082c8cb4SDavid Brownell 	 */
1336082c8cb4SDavid Brownell 	if (master->num_chipselect == 0)
1337082c8cb4SDavid Brownell 		return -EINVAL;
1338082c8cb4SDavid Brownell 
1339bb29785eSGrant Likely 	if ((master->bus_num < 0) && master->dev.of_node)
1340bb29785eSGrant Likely 		master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1341bb29785eSGrant Likely 
13428ae12a0dSDavid Brownell 	/* convention:  dynamically assigned bus IDs count down from the max */
1343a020ed75SDavid Brownell 	if (master->bus_num < 0) {
1344082c8cb4SDavid Brownell 		/* FIXME switch to an IDR based scheme, something like
1345082c8cb4SDavid Brownell 		 * I2C now uses, so we can't run out of "dynamic" IDs
1346082c8cb4SDavid Brownell 		 */
13478ae12a0dSDavid Brownell 		master->bus_num = atomic_dec_return(&dyn_bus_id);
1348b885244eSDavid Brownell 		dynamic = 1;
13498ae12a0dSDavid Brownell 	}
13508ae12a0dSDavid Brownell 
1351cf32b71eSErnst Schwab 	spin_lock_init(&master->bus_lock_spinlock);
1352cf32b71eSErnst Schwab 	mutex_init(&master->bus_lock_mutex);
1353cf32b71eSErnst Schwab 	master->bus_lock_flag = 0;
1354b158935fSMark Brown 	init_completion(&master->xfer_completion);
1355cf32b71eSErnst Schwab 
13568ae12a0dSDavid Brownell 	/* register the device, then userspace will see it.
13578ae12a0dSDavid Brownell 	 * registration fails if the bus ID is in use.
13588ae12a0dSDavid Brownell 	 */
135935f74fcaSKay Sievers 	dev_set_name(&master->dev, "spi%u", master->bus_num);
136049dce689STony Jones 	status = device_add(&master->dev);
1361b885244eSDavid Brownell 	if (status < 0)
13628ae12a0dSDavid Brownell 		goto done;
136335f74fcaSKay Sievers 	dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
13648ae12a0dSDavid Brownell 			dynamic ? " (dynamic)" : "");
13658ae12a0dSDavid Brownell 
1366ffbbdd21SLinus Walleij 	/* If we're using a queued driver, start the queue */
1367ffbbdd21SLinus Walleij 	if (master->transfer)
1368ffbbdd21SLinus Walleij 		dev_info(dev, "master is unqueued, this is deprecated\n");
1369ffbbdd21SLinus Walleij 	else {
1370ffbbdd21SLinus Walleij 		status = spi_master_initialize_queue(master);
1371ffbbdd21SLinus Walleij 		if (status) {
1372e93b0724SAxel Lin 			device_del(&master->dev);
1373ffbbdd21SLinus Walleij 			goto done;
1374ffbbdd21SLinus Walleij 		}
1375ffbbdd21SLinus Walleij 	}
1376ffbbdd21SLinus Walleij 
13772b9603a0SFeng Tang 	mutex_lock(&board_lock);
13782b9603a0SFeng Tang 	list_add_tail(&master->list, &spi_master_list);
13792b9603a0SFeng Tang 	list_for_each_entry(bi, &board_list, list)
13802b9603a0SFeng Tang 		spi_match_master_to_boardinfo(master, &bi->board_info);
13812b9603a0SFeng Tang 	mutex_unlock(&board_lock);
13822b9603a0SFeng Tang 
138364bee4d2SMika Westerberg 	/* Register devices from the device tree and ACPI */
138412b15e83SAnatolij Gustschin 	of_register_spi_devices(master);
138564bee4d2SMika Westerberg 	acpi_register_spi_devices(master);
13868ae12a0dSDavid Brownell done:
13878ae12a0dSDavid Brownell 	return status;
13888ae12a0dSDavid Brownell }
13898ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_register_master);
13908ae12a0dSDavid Brownell 
1391666d5b4cSMark Brown static void devm_spi_unregister(struct device *dev, void *res)
1392666d5b4cSMark Brown {
1393666d5b4cSMark Brown 	spi_unregister_master(*(struct spi_master **)res);
1394666d5b4cSMark Brown }
1395666d5b4cSMark Brown 
1396666d5b4cSMark Brown /**
1397666d5b4cSMark Brown  * dev_spi_register_master - register managed SPI master controller
1398666d5b4cSMark Brown  * @dev:    device managing SPI master
1399666d5b4cSMark Brown  * @master: initialized master, originally from spi_alloc_master()
1400666d5b4cSMark Brown  * Context: can sleep
1401666d5b4cSMark Brown  *
1402666d5b4cSMark Brown  * Register a SPI device as with spi_register_master() which will
1403666d5b4cSMark Brown  * automatically be unregister
1404666d5b4cSMark Brown  */
1405666d5b4cSMark Brown int devm_spi_register_master(struct device *dev, struct spi_master *master)
1406666d5b4cSMark Brown {
1407666d5b4cSMark Brown 	struct spi_master **ptr;
1408666d5b4cSMark Brown 	int ret;
1409666d5b4cSMark Brown 
1410666d5b4cSMark Brown 	ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1411666d5b4cSMark Brown 	if (!ptr)
1412666d5b4cSMark Brown 		return -ENOMEM;
1413666d5b4cSMark Brown 
1414666d5b4cSMark Brown 	ret = spi_register_master(master);
1415666d5b4cSMark Brown 	if (ret != 0) {
1416666d5b4cSMark Brown 		*ptr = master;
1417666d5b4cSMark Brown 		devres_add(dev, ptr);
1418666d5b4cSMark Brown 	} else {
1419666d5b4cSMark Brown 		devres_free(ptr);
1420666d5b4cSMark Brown 	}
1421666d5b4cSMark Brown 
1422666d5b4cSMark Brown 	return ret;
1423666d5b4cSMark Brown }
1424666d5b4cSMark Brown EXPORT_SYMBOL_GPL(devm_spi_register_master);
1425666d5b4cSMark Brown 
142634860089SDavid Lamparter static int __unregister(struct device *dev, void *null)
14278ae12a0dSDavid Brownell {
14280c868461SDavid Brownell 	spi_unregister_device(to_spi_device(dev));
14298ae12a0dSDavid Brownell 	return 0;
14308ae12a0dSDavid Brownell }
14318ae12a0dSDavid Brownell 
14328ae12a0dSDavid Brownell /**
14338ae12a0dSDavid Brownell  * spi_unregister_master - unregister SPI master controller
14348ae12a0dSDavid Brownell  * @master: the master being unregistered
143533e34dc6SDavid Brownell  * Context: can sleep
14368ae12a0dSDavid Brownell  *
14378ae12a0dSDavid Brownell  * This call is used only by SPI master controller drivers, which are the
14388ae12a0dSDavid Brownell  * only ones directly touching chip registers.
14398ae12a0dSDavid Brownell  *
14408ae12a0dSDavid Brownell  * This must be called from context that can sleep.
14418ae12a0dSDavid Brownell  */
14428ae12a0dSDavid Brownell void spi_unregister_master(struct spi_master *master)
14438ae12a0dSDavid Brownell {
144489fc9a1aSJeff Garzik 	int dummy;
144589fc9a1aSJeff Garzik 
1446ffbbdd21SLinus Walleij 	if (master->queued) {
1447ffbbdd21SLinus Walleij 		if (spi_destroy_queue(master))
1448ffbbdd21SLinus Walleij 			dev_err(&master->dev, "queue remove failed\n");
1449ffbbdd21SLinus Walleij 	}
1450ffbbdd21SLinus Walleij 
14512b9603a0SFeng Tang 	mutex_lock(&board_lock);
14522b9603a0SFeng Tang 	list_del(&master->list);
14532b9603a0SFeng Tang 	mutex_unlock(&board_lock);
14542b9603a0SFeng Tang 
145597dbf37dSSebastian Andrzej Siewior 	dummy = device_for_each_child(&master->dev, NULL, __unregister);
145649dce689STony Jones 	device_unregister(&master->dev);
14578ae12a0dSDavid Brownell }
14588ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_unregister_master);
14598ae12a0dSDavid Brownell 
1460ffbbdd21SLinus Walleij int spi_master_suspend(struct spi_master *master)
1461ffbbdd21SLinus Walleij {
1462ffbbdd21SLinus Walleij 	int ret;
1463ffbbdd21SLinus Walleij 
1464ffbbdd21SLinus Walleij 	/* Basically no-ops for non-queued masters */
1465ffbbdd21SLinus Walleij 	if (!master->queued)
1466ffbbdd21SLinus Walleij 		return 0;
1467ffbbdd21SLinus Walleij 
1468ffbbdd21SLinus Walleij 	ret = spi_stop_queue(master);
1469ffbbdd21SLinus Walleij 	if (ret)
1470ffbbdd21SLinus Walleij 		dev_err(&master->dev, "queue stop failed\n");
1471ffbbdd21SLinus Walleij 
1472ffbbdd21SLinus Walleij 	return ret;
1473ffbbdd21SLinus Walleij }
1474ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_master_suspend);
1475ffbbdd21SLinus Walleij 
1476ffbbdd21SLinus Walleij int spi_master_resume(struct spi_master *master)
1477ffbbdd21SLinus Walleij {
1478ffbbdd21SLinus Walleij 	int ret;
1479ffbbdd21SLinus Walleij 
1480ffbbdd21SLinus Walleij 	if (!master->queued)
1481ffbbdd21SLinus Walleij 		return 0;
1482ffbbdd21SLinus Walleij 
1483ffbbdd21SLinus Walleij 	ret = spi_start_queue(master);
1484ffbbdd21SLinus Walleij 	if (ret)
1485ffbbdd21SLinus Walleij 		dev_err(&master->dev, "queue restart failed\n");
1486ffbbdd21SLinus Walleij 
1487ffbbdd21SLinus Walleij 	return ret;
1488ffbbdd21SLinus Walleij }
1489ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_master_resume);
1490ffbbdd21SLinus Walleij 
14919f3b795aSMichał Mirosław static int __spi_master_match(struct device *dev, const void *data)
14925ed2c832SDave Young {
14935ed2c832SDave Young 	struct spi_master *m;
14949f3b795aSMichał Mirosław 	const u16 *bus_num = data;
14955ed2c832SDave Young 
14965ed2c832SDave Young 	m = container_of(dev, struct spi_master, dev);
14975ed2c832SDave Young 	return m->bus_num == *bus_num;
14985ed2c832SDave Young }
14995ed2c832SDave Young 
15008ae12a0dSDavid Brownell /**
15018ae12a0dSDavid Brownell  * spi_busnum_to_master - look up master associated with bus_num
15028ae12a0dSDavid Brownell  * @bus_num: the master's bus number
150333e34dc6SDavid Brownell  * Context: can sleep
15048ae12a0dSDavid Brownell  *
15058ae12a0dSDavid Brownell  * This call may be used with devices that are registered after
15068ae12a0dSDavid Brownell  * arch init time.  It returns a refcounted pointer to the relevant
15078ae12a0dSDavid Brownell  * spi_master (which the caller must release), or NULL if there is
15088ae12a0dSDavid Brownell  * no such master registered.
15098ae12a0dSDavid Brownell  */
15108ae12a0dSDavid Brownell struct spi_master *spi_busnum_to_master(u16 bus_num)
15118ae12a0dSDavid Brownell {
151249dce689STony Jones 	struct device		*dev;
15131e9a51dcSAtsushi Nemoto 	struct spi_master	*master = NULL;
15148ae12a0dSDavid Brownell 
1515695794aeSGreg Kroah-Hartman 	dev = class_find_device(&spi_master_class, NULL, &bus_num,
15165ed2c832SDave Young 				__spi_master_match);
15175ed2c832SDave Young 	if (dev)
15185ed2c832SDave Young 		master = container_of(dev, struct spi_master, dev);
15195ed2c832SDave Young 	/* reference got in class_find_device */
15201e9a51dcSAtsushi Nemoto 	return master;
15218ae12a0dSDavid Brownell }
15228ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master);
15238ae12a0dSDavid Brownell 
15248ae12a0dSDavid Brownell 
15258ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
15268ae12a0dSDavid Brownell 
15277d077197SDavid Brownell /* Core methods for SPI master protocol drivers.  Some of the
15287d077197SDavid Brownell  * other core methods are currently defined as inline functions.
15297d077197SDavid Brownell  */
15307d077197SDavid Brownell 
15317d077197SDavid Brownell /**
15327d077197SDavid Brownell  * spi_setup - setup SPI mode and clock rate
15337d077197SDavid Brownell  * @spi: the device whose settings are being modified
15347d077197SDavid Brownell  * Context: can sleep, and no requests are queued to the device
15357d077197SDavid Brownell  *
15367d077197SDavid Brownell  * SPI protocol drivers may need to update the transfer mode if the
15377d077197SDavid Brownell  * device doesn't work with its default.  They may likewise need
15387d077197SDavid Brownell  * to update clock rates or word sizes from initial values.  This function
15397d077197SDavid Brownell  * changes those settings, and must be called from a context that can sleep.
15407d077197SDavid Brownell  * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
15417d077197SDavid Brownell  * effect the next time the device is selected and data is transferred to
15427d077197SDavid Brownell  * or from it.  When this function returns, the spi device is deselected.
15437d077197SDavid Brownell  *
15447d077197SDavid Brownell  * Note that this call will fail if the protocol driver specifies an option
15457d077197SDavid Brownell  * that the underlying controller or its driver does not support.  For
15467d077197SDavid Brownell  * example, not all hardware supports wire transfers using nine bit words,
15477d077197SDavid Brownell  * LSB-first wire encoding, or active-high chipselects.
15487d077197SDavid Brownell  */
15497d077197SDavid Brownell int spi_setup(struct spi_device *spi)
15507d077197SDavid Brownell {
1551e7db06b5SDavid Brownell 	unsigned	bad_bits;
1552caae070cSLaxman Dewangan 	int		status = 0;
15537d077197SDavid Brownell 
1554f477b7fbSwangyuhang 	/* check mode to prevent that DUAL and QUAD set at the same time
1555f477b7fbSwangyuhang 	 */
1556f477b7fbSwangyuhang 	if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1557f477b7fbSwangyuhang 		((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1558f477b7fbSwangyuhang 		dev_err(&spi->dev,
1559f477b7fbSwangyuhang 		"setup: can not select dual and quad at the same time\n");
1560f477b7fbSwangyuhang 		return -EINVAL;
1561f477b7fbSwangyuhang 	}
1562f477b7fbSwangyuhang 	/* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1563f477b7fbSwangyuhang 	 */
1564f477b7fbSwangyuhang 	if ((spi->mode & SPI_3WIRE) && (spi->mode &
1565f477b7fbSwangyuhang 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1566f477b7fbSwangyuhang 		return -EINVAL;
1567e7db06b5SDavid Brownell 	/* help drivers fail *cleanly* when they need options
1568e7db06b5SDavid Brownell 	 * that aren't supported with their current master
1569e7db06b5SDavid Brownell 	 */
1570e7db06b5SDavid Brownell 	bad_bits = spi->mode & ~spi->master->mode_bits;
1571e7db06b5SDavid Brownell 	if (bad_bits) {
1572eb288a1fSLinus Walleij 		dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
1573e7db06b5SDavid Brownell 			bad_bits);
1574e7db06b5SDavid Brownell 		return -EINVAL;
1575e7db06b5SDavid Brownell 	}
1576e7db06b5SDavid Brownell 
15777d077197SDavid Brownell 	if (!spi->bits_per_word)
15787d077197SDavid Brownell 		spi->bits_per_word = 8;
15797d077197SDavid Brownell 
1580caae070cSLaxman Dewangan 	if (spi->master->setup)
15817d077197SDavid Brownell 		status = spi->master->setup(spi);
15827d077197SDavid Brownell 
15835fe5f05eSJingoo Han 	dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
15847d077197SDavid Brownell 			(int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
15857d077197SDavid Brownell 			(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
15867d077197SDavid Brownell 			(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
15877d077197SDavid Brownell 			(spi->mode & SPI_3WIRE) ? "3wire, " : "",
15887d077197SDavid Brownell 			(spi->mode & SPI_LOOP) ? "loopback, " : "",
15897d077197SDavid Brownell 			spi->bits_per_word, spi->max_speed_hz,
15907d077197SDavid Brownell 			status);
15917d077197SDavid Brownell 
15927d077197SDavid Brownell 	return status;
15937d077197SDavid Brownell }
15947d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup);
15957d077197SDavid Brownell 
159690808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message)
1597cf32b71eSErnst Schwab {
1598cf32b71eSErnst Schwab 	struct spi_master *master = spi->master;
1599e6811d1dSLaxman Dewangan 	struct spi_transfer *xfer;
1600cf32b71eSErnst Schwab 
160124a0013aSMark Brown 	if (list_empty(&message->transfers))
160224a0013aSMark Brown 		return -EINVAL;
160324a0013aSMark Brown 	if (!message->complete)
160424a0013aSMark Brown 		return -EINVAL;
160524a0013aSMark Brown 
1606cf32b71eSErnst Schwab 	/* Half-duplex links include original MicroWire, and ones with
1607cf32b71eSErnst Schwab 	 * only one data pin like SPI_3WIRE (switches direction) or where
1608cf32b71eSErnst Schwab 	 * either MOSI or MISO is missing.  They can also be caused by
1609cf32b71eSErnst Schwab 	 * software limitations.
1610cf32b71eSErnst Schwab 	 */
1611cf32b71eSErnst Schwab 	if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1612cf32b71eSErnst Schwab 			|| (spi->mode & SPI_3WIRE)) {
1613cf32b71eSErnst Schwab 		unsigned flags = master->flags;
1614cf32b71eSErnst Schwab 
1615cf32b71eSErnst Schwab 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
1616cf32b71eSErnst Schwab 			if (xfer->rx_buf && xfer->tx_buf)
1617cf32b71eSErnst Schwab 				return -EINVAL;
1618cf32b71eSErnst Schwab 			if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
1619cf32b71eSErnst Schwab 				return -EINVAL;
1620cf32b71eSErnst Schwab 			if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
1621cf32b71eSErnst Schwab 				return -EINVAL;
1622cf32b71eSErnst Schwab 		}
1623cf32b71eSErnst Schwab 	}
1624cf32b71eSErnst Schwab 
1625e6811d1dSLaxman Dewangan 	/**
1626059b8ffeSLaxman Dewangan 	 * Set transfer bits_per_word and max speed as spi device default if
1627059b8ffeSLaxman Dewangan 	 * it is not set for this transfer.
1628f477b7fbSwangyuhang 	 * Set transfer tx_nbits and rx_nbits as single transfer default
1629f477b7fbSwangyuhang 	 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
1630e6811d1dSLaxman Dewangan 	 */
1631e6811d1dSLaxman Dewangan 	list_for_each_entry(xfer, &message->transfers, transfer_list) {
1632078726ceSSourav Poddar 		message->frame_length += xfer->len;
1633e6811d1dSLaxman Dewangan 		if (!xfer->bits_per_word)
1634e6811d1dSLaxman Dewangan 			xfer->bits_per_word = spi->bits_per_word;
163556ede94aSGabor Juhos 		if (!xfer->speed_hz) {
1636059b8ffeSLaxman Dewangan 			xfer->speed_hz = spi->max_speed_hz;
163756ede94aSGabor Juhos 			if (master->max_speed_hz &&
163856ede94aSGabor Juhos 			    xfer->speed_hz > master->max_speed_hz)
163956ede94aSGabor Juhos 				xfer->speed_hz = master->max_speed_hz;
164056ede94aSGabor Juhos 		}
164156ede94aSGabor Juhos 
1642543bb255SStephen Warren 		if (master->bits_per_word_mask) {
1643543bb255SStephen Warren 			/* Only 32 bits fit in the mask */
1644543bb255SStephen Warren 			if (xfer->bits_per_word > 32)
1645543bb255SStephen Warren 				return -EINVAL;
1646543bb255SStephen Warren 			if (!(master->bits_per_word_mask &
1647543bb255SStephen Warren 					BIT(xfer->bits_per_word - 1)))
1648543bb255SStephen Warren 				return -EINVAL;
1649543bb255SStephen Warren 		}
1650a2fd4f9fSMark Brown 
1651a2fd4f9fSMark Brown 		if (xfer->speed_hz && master->min_speed_hz &&
1652a2fd4f9fSMark Brown 		    xfer->speed_hz < master->min_speed_hz)
1653a2fd4f9fSMark Brown 			return -EINVAL;
1654a2fd4f9fSMark Brown 		if (xfer->speed_hz && master->max_speed_hz &&
1655a2fd4f9fSMark Brown 		    xfer->speed_hz > master->max_speed_hz)
1656d5ee722aSwangyuhang 			return -EINVAL;
1657f477b7fbSwangyuhang 
1658f477b7fbSwangyuhang 		if (xfer->tx_buf && !xfer->tx_nbits)
1659f477b7fbSwangyuhang 			xfer->tx_nbits = SPI_NBITS_SINGLE;
1660f477b7fbSwangyuhang 		if (xfer->rx_buf && !xfer->rx_nbits)
1661f477b7fbSwangyuhang 			xfer->rx_nbits = SPI_NBITS_SINGLE;
1662f477b7fbSwangyuhang 		/* check transfer tx/rx_nbits:
1663f477b7fbSwangyuhang 		 * 1. keep the value is not out of single, dual and quad
1664f477b7fbSwangyuhang 		 * 2. keep tx/rx_nbits is contained by mode in spi_device
1665f477b7fbSwangyuhang 		 * 3. if SPI_3WIRE, tx/rx_nbits should be in single
1666f477b7fbSwangyuhang 		 */
1667db90a441SSourav Poddar 		if (xfer->tx_buf) {
1668f477b7fbSwangyuhang 			if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
1669f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_DUAL &&
1670f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_QUAD)
1671a2fd4f9fSMark Brown 				return -EINVAL;
1672f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
1673f477b7fbSwangyuhang 				!(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
1674f477b7fbSwangyuhang 				return -EINVAL;
1675f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
1676f477b7fbSwangyuhang 				!(spi->mode & SPI_TX_QUAD))
1677f477b7fbSwangyuhang 				return -EINVAL;
1678db90a441SSourav Poddar 		}
1679f477b7fbSwangyuhang 		/* check transfer rx_nbits */
1680db90a441SSourav Poddar 		if (xfer->rx_buf) {
1681f477b7fbSwangyuhang 			if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
1682f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_DUAL &&
1683f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_QUAD)
1684f477b7fbSwangyuhang 				return -EINVAL;
1685f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
1686f477b7fbSwangyuhang 				!(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
1687f477b7fbSwangyuhang 				return -EINVAL;
1688f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
1689f477b7fbSwangyuhang 				!(spi->mode & SPI_RX_QUAD))
1690f477b7fbSwangyuhang 				return -EINVAL;
1691e6811d1dSLaxman Dewangan 		}
1692e6811d1dSLaxman Dewangan 	}
1693e6811d1dSLaxman Dewangan 
1694cf32b71eSErnst Schwab 	message->status = -EINPROGRESS;
169590808738SMark Brown 
169690808738SMark Brown 	return 0;
169790808738SMark Brown }
169890808738SMark Brown 
169990808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message)
170090808738SMark Brown {
170190808738SMark Brown 	struct spi_master *master = spi->master;
170290808738SMark Brown 
170390808738SMark Brown 	message->spi = spi;
170490808738SMark Brown 
170590808738SMark Brown 	trace_spi_message_submit(message);
170690808738SMark Brown 
1707cf32b71eSErnst Schwab 	return master->transfer(spi, message);
1708cf32b71eSErnst Schwab }
1709cf32b71eSErnst Schwab 
1710568d0697SDavid Brownell /**
1711568d0697SDavid Brownell  * spi_async - asynchronous SPI transfer
1712568d0697SDavid Brownell  * @spi: device with which data will be exchanged
1713568d0697SDavid Brownell  * @message: describes the data transfers, including completion callback
1714568d0697SDavid Brownell  * Context: any (irqs may be blocked, etc)
1715568d0697SDavid Brownell  *
1716568d0697SDavid Brownell  * This call may be used in_irq and other contexts which can't sleep,
1717568d0697SDavid Brownell  * as well as from task contexts which can sleep.
1718568d0697SDavid Brownell  *
1719568d0697SDavid Brownell  * The completion callback is invoked in a context which can't sleep.
1720568d0697SDavid Brownell  * Before that invocation, the value of message->status is undefined.
1721568d0697SDavid Brownell  * When the callback is issued, message->status holds either zero (to
1722568d0697SDavid Brownell  * indicate complete success) or a negative error code.  After that
1723568d0697SDavid Brownell  * callback returns, the driver which issued the transfer request may
1724568d0697SDavid Brownell  * deallocate the associated memory; it's no longer in use by any SPI
1725568d0697SDavid Brownell  * core or controller driver code.
1726568d0697SDavid Brownell  *
1727568d0697SDavid Brownell  * Note that although all messages to a spi_device are handled in
1728568d0697SDavid Brownell  * FIFO order, messages may go to different devices in other orders.
1729568d0697SDavid Brownell  * Some device might be higher priority, or have various "hard" access
1730568d0697SDavid Brownell  * time requirements, for example.
1731568d0697SDavid Brownell  *
1732568d0697SDavid Brownell  * On detection of any fault during the transfer, processing of
1733568d0697SDavid Brownell  * the entire message is aborted, and the device is deselected.
1734568d0697SDavid Brownell  * Until returning from the associated message completion callback,
1735568d0697SDavid Brownell  * no other spi_message queued to that device will be processed.
1736568d0697SDavid Brownell  * (This rule applies equally to all the synchronous transfer calls,
1737568d0697SDavid Brownell  * which are wrappers around this core asynchronous primitive.)
1738568d0697SDavid Brownell  */
1739568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message)
1740568d0697SDavid Brownell {
1741568d0697SDavid Brownell 	struct spi_master *master = spi->master;
1742cf32b71eSErnst Schwab 	int ret;
1743cf32b71eSErnst Schwab 	unsigned long flags;
1744568d0697SDavid Brownell 
174590808738SMark Brown 	ret = __spi_validate(spi, message);
174690808738SMark Brown 	if (ret != 0)
174790808738SMark Brown 		return ret;
174890808738SMark Brown 
1749cf32b71eSErnst Schwab 	spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1750568d0697SDavid Brownell 
1751cf32b71eSErnst Schwab 	if (master->bus_lock_flag)
1752cf32b71eSErnst Schwab 		ret = -EBUSY;
1753cf32b71eSErnst Schwab 	else
1754cf32b71eSErnst Schwab 		ret = __spi_async(spi, message);
1755568d0697SDavid Brownell 
1756cf32b71eSErnst Schwab 	spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1757cf32b71eSErnst Schwab 
1758cf32b71eSErnst Schwab 	return ret;
1759568d0697SDavid Brownell }
1760568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async);
1761568d0697SDavid Brownell 
1762cf32b71eSErnst Schwab /**
1763cf32b71eSErnst Schwab  * spi_async_locked - version of spi_async with exclusive bus usage
1764cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
1765cf32b71eSErnst Schwab  * @message: describes the data transfers, including completion callback
1766cf32b71eSErnst Schwab  * Context: any (irqs may be blocked, etc)
1767cf32b71eSErnst Schwab  *
1768cf32b71eSErnst Schwab  * This call may be used in_irq and other contexts which can't sleep,
1769cf32b71eSErnst Schwab  * as well as from task contexts which can sleep.
1770cf32b71eSErnst Schwab  *
1771cf32b71eSErnst Schwab  * The completion callback is invoked in a context which can't sleep.
1772cf32b71eSErnst Schwab  * Before that invocation, the value of message->status is undefined.
1773cf32b71eSErnst Schwab  * When the callback is issued, message->status holds either zero (to
1774cf32b71eSErnst Schwab  * indicate complete success) or a negative error code.  After that
1775cf32b71eSErnst Schwab  * callback returns, the driver which issued the transfer request may
1776cf32b71eSErnst Schwab  * deallocate the associated memory; it's no longer in use by any SPI
1777cf32b71eSErnst Schwab  * core or controller driver code.
1778cf32b71eSErnst Schwab  *
1779cf32b71eSErnst Schwab  * Note that although all messages to a spi_device are handled in
1780cf32b71eSErnst Schwab  * FIFO order, messages may go to different devices in other orders.
1781cf32b71eSErnst Schwab  * Some device might be higher priority, or have various "hard" access
1782cf32b71eSErnst Schwab  * time requirements, for example.
1783cf32b71eSErnst Schwab  *
1784cf32b71eSErnst Schwab  * On detection of any fault during the transfer, processing of
1785cf32b71eSErnst Schwab  * the entire message is aborted, and the device is deselected.
1786cf32b71eSErnst Schwab  * Until returning from the associated message completion callback,
1787cf32b71eSErnst Schwab  * no other spi_message queued to that device will be processed.
1788cf32b71eSErnst Schwab  * (This rule applies equally to all the synchronous transfer calls,
1789cf32b71eSErnst Schwab  * which are wrappers around this core asynchronous primitive.)
1790cf32b71eSErnst Schwab  */
1791cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message)
1792cf32b71eSErnst Schwab {
1793cf32b71eSErnst Schwab 	struct spi_master *master = spi->master;
1794cf32b71eSErnst Schwab 	int ret;
1795cf32b71eSErnst Schwab 	unsigned long flags;
1796cf32b71eSErnst Schwab 
179790808738SMark Brown 	ret = __spi_validate(spi, message);
179890808738SMark Brown 	if (ret != 0)
179990808738SMark Brown 		return ret;
180090808738SMark Brown 
1801cf32b71eSErnst Schwab 	spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1802cf32b71eSErnst Schwab 
1803cf32b71eSErnst Schwab 	ret = __spi_async(spi, message);
1804cf32b71eSErnst Schwab 
1805cf32b71eSErnst Schwab 	spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1806cf32b71eSErnst Schwab 
1807cf32b71eSErnst Schwab 	return ret;
1808cf32b71eSErnst Schwab 
1809cf32b71eSErnst Schwab }
1810cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked);
1811cf32b71eSErnst Schwab 
18127d077197SDavid Brownell 
18137d077197SDavid Brownell /*-------------------------------------------------------------------------*/
18147d077197SDavid Brownell 
18157d077197SDavid Brownell /* Utility methods for SPI master protocol drivers, layered on
18167d077197SDavid Brownell  * top of the core.  Some other utility methods are defined as
18177d077197SDavid Brownell  * inline functions.
18187d077197SDavid Brownell  */
18197d077197SDavid Brownell 
18205d870c8eSAndrew Morton static void spi_complete(void *arg)
18215d870c8eSAndrew Morton {
18225d870c8eSAndrew Morton 	complete(arg);
18235d870c8eSAndrew Morton }
18245d870c8eSAndrew Morton 
1825cf32b71eSErnst Schwab static int __spi_sync(struct spi_device *spi, struct spi_message *message,
1826cf32b71eSErnst Schwab 		      int bus_locked)
1827cf32b71eSErnst Schwab {
1828cf32b71eSErnst Schwab 	DECLARE_COMPLETION_ONSTACK(done);
1829cf32b71eSErnst Schwab 	int status;
1830cf32b71eSErnst Schwab 	struct spi_master *master = spi->master;
1831cf32b71eSErnst Schwab 
1832cf32b71eSErnst Schwab 	message->complete = spi_complete;
1833cf32b71eSErnst Schwab 	message->context = &done;
1834cf32b71eSErnst Schwab 
1835cf32b71eSErnst Schwab 	if (!bus_locked)
1836cf32b71eSErnst Schwab 		mutex_lock(&master->bus_lock_mutex);
1837cf32b71eSErnst Schwab 
1838cf32b71eSErnst Schwab 	status = spi_async_locked(spi, message);
1839cf32b71eSErnst Schwab 
1840cf32b71eSErnst Schwab 	if (!bus_locked)
1841cf32b71eSErnst Schwab 		mutex_unlock(&master->bus_lock_mutex);
1842cf32b71eSErnst Schwab 
1843cf32b71eSErnst Schwab 	if (status == 0) {
1844cf32b71eSErnst Schwab 		wait_for_completion(&done);
1845cf32b71eSErnst Schwab 		status = message->status;
1846cf32b71eSErnst Schwab 	}
1847cf32b71eSErnst Schwab 	message->context = NULL;
1848cf32b71eSErnst Schwab 	return status;
1849cf32b71eSErnst Schwab }
1850cf32b71eSErnst Schwab 
18518ae12a0dSDavid Brownell /**
18528ae12a0dSDavid Brownell  * spi_sync - blocking/synchronous SPI data transfers
18538ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
18548ae12a0dSDavid Brownell  * @message: describes the data transfers
185533e34dc6SDavid Brownell  * Context: can sleep
18568ae12a0dSDavid Brownell  *
18578ae12a0dSDavid Brownell  * This call may only be used from a context that may sleep.  The sleep
18588ae12a0dSDavid Brownell  * is non-interruptible, and has no timeout.  Low-overhead controller
18598ae12a0dSDavid Brownell  * drivers may DMA directly into and out of the message buffers.
18608ae12a0dSDavid Brownell  *
18618ae12a0dSDavid Brownell  * Note that the SPI device's chip select is active during the message,
18628ae12a0dSDavid Brownell  * and then is normally disabled between messages.  Drivers for some
18638ae12a0dSDavid Brownell  * frequently-used devices may want to minimize costs of selecting a chip,
18648ae12a0dSDavid Brownell  * by leaving it selected in anticipation that the next message will go
18658ae12a0dSDavid Brownell  * to the same chip.  (That may increase power usage.)
18668ae12a0dSDavid Brownell  *
18670c868461SDavid Brownell  * Also, the caller is guaranteeing that the memory associated with the
18680c868461SDavid Brownell  * message will not be freed before this call returns.
18690c868461SDavid Brownell  *
18709b938b74SMarc Pignat  * It returns zero on success, else a negative error code.
18718ae12a0dSDavid Brownell  */
18728ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message)
18738ae12a0dSDavid Brownell {
1874cf32b71eSErnst Schwab 	return __spi_sync(spi, message, 0);
18758ae12a0dSDavid Brownell }
18768ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync);
18778ae12a0dSDavid Brownell 
1878cf32b71eSErnst Schwab /**
1879cf32b71eSErnst Schwab  * spi_sync_locked - version of spi_sync with exclusive bus usage
1880cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
1881cf32b71eSErnst Schwab  * @message: describes the data transfers
1882cf32b71eSErnst Schwab  * Context: can sleep
1883cf32b71eSErnst Schwab  *
1884cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
1885cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.  Low-overhead controller
1886cf32b71eSErnst Schwab  * drivers may DMA directly into and out of the message buffers.
1887cf32b71eSErnst Schwab  *
1888cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
188925985edcSLucas De Marchi  * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
1890cf32b71eSErnst Schwab  * be released by a spi_bus_unlock call when the exclusive access is over.
1891cf32b71eSErnst Schwab  *
1892cf32b71eSErnst Schwab  * It returns zero on success, else a negative error code.
1893cf32b71eSErnst Schwab  */
1894cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
1895cf32b71eSErnst Schwab {
1896cf32b71eSErnst Schwab 	return __spi_sync(spi, message, 1);
1897cf32b71eSErnst Schwab }
1898cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked);
1899cf32b71eSErnst Schwab 
1900cf32b71eSErnst Schwab /**
1901cf32b71eSErnst Schwab  * spi_bus_lock - obtain a lock for exclusive SPI bus usage
1902cf32b71eSErnst Schwab  * @master: SPI bus master that should be locked for exclusive bus access
1903cf32b71eSErnst Schwab  * Context: can sleep
1904cf32b71eSErnst Schwab  *
1905cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
1906cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
1907cf32b71eSErnst Schwab  *
1908cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
1909cf32b71eSErnst Schwab  * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
1910cf32b71eSErnst Schwab  * exclusive access is over. Data transfer must be done by spi_sync_locked
1911cf32b71eSErnst Schwab  * and spi_async_locked calls when the SPI bus lock is held.
1912cf32b71eSErnst Schwab  *
1913cf32b71eSErnst Schwab  * It returns zero on success, else a negative error code.
1914cf32b71eSErnst Schwab  */
1915cf32b71eSErnst Schwab int spi_bus_lock(struct spi_master *master)
1916cf32b71eSErnst Schwab {
1917cf32b71eSErnst Schwab 	unsigned long flags;
1918cf32b71eSErnst Schwab 
1919cf32b71eSErnst Schwab 	mutex_lock(&master->bus_lock_mutex);
1920cf32b71eSErnst Schwab 
1921cf32b71eSErnst Schwab 	spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1922cf32b71eSErnst Schwab 	master->bus_lock_flag = 1;
1923cf32b71eSErnst Schwab 	spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1924cf32b71eSErnst Schwab 
1925cf32b71eSErnst Schwab 	/* mutex remains locked until spi_bus_unlock is called */
1926cf32b71eSErnst Schwab 
1927cf32b71eSErnst Schwab 	return 0;
1928cf32b71eSErnst Schwab }
1929cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock);
1930cf32b71eSErnst Schwab 
1931cf32b71eSErnst Schwab /**
1932cf32b71eSErnst Schwab  * spi_bus_unlock - release the lock for exclusive SPI bus usage
1933cf32b71eSErnst Schwab  * @master: SPI bus master that was locked for exclusive bus access
1934cf32b71eSErnst Schwab  * Context: can sleep
1935cf32b71eSErnst Schwab  *
1936cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
1937cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
1938cf32b71eSErnst Schwab  *
1939cf32b71eSErnst Schwab  * This call releases an SPI bus lock previously obtained by an spi_bus_lock
1940cf32b71eSErnst Schwab  * call.
1941cf32b71eSErnst Schwab  *
1942cf32b71eSErnst Schwab  * It returns zero on success, else a negative error code.
1943cf32b71eSErnst Schwab  */
1944cf32b71eSErnst Schwab int spi_bus_unlock(struct spi_master *master)
1945cf32b71eSErnst Schwab {
1946cf32b71eSErnst Schwab 	master->bus_lock_flag = 0;
1947cf32b71eSErnst Schwab 
1948cf32b71eSErnst Schwab 	mutex_unlock(&master->bus_lock_mutex);
1949cf32b71eSErnst Schwab 
1950cf32b71eSErnst Schwab 	return 0;
1951cf32b71eSErnst Schwab }
1952cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock);
1953cf32b71eSErnst Schwab 
1954a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */
1955a9948b61SDavid Brownell #define	SPI_BUFSIZ	max(32, SMP_CACHE_BYTES)
19568ae12a0dSDavid Brownell 
19578ae12a0dSDavid Brownell static u8	*buf;
19588ae12a0dSDavid Brownell 
19598ae12a0dSDavid Brownell /**
19608ae12a0dSDavid Brownell  * spi_write_then_read - SPI synchronous write followed by read
19618ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
19628ae12a0dSDavid Brownell  * @txbuf: data to be written (need not be dma-safe)
19638ae12a0dSDavid Brownell  * @n_tx: size of txbuf, in bytes
196427570497SJiri Pirko  * @rxbuf: buffer into which data will be read (need not be dma-safe)
196527570497SJiri Pirko  * @n_rx: size of rxbuf, in bytes
196633e34dc6SDavid Brownell  * Context: can sleep
19678ae12a0dSDavid Brownell  *
19688ae12a0dSDavid Brownell  * This performs a half duplex MicroWire style transaction with the
19698ae12a0dSDavid Brownell  * device, sending txbuf and then reading rxbuf.  The return value
19708ae12a0dSDavid Brownell  * is zero for success, else a negative errno status code.
1971b885244eSDavid Brownell  * This call may only be used from a context that may sleep.
19728ae12a0dSDavid Brownell  *
19730c868461SDavid Brownell  * Parameters to this routine are always copied using a small buffer;
197433e34dc6SDavid Brownell  * portable code should never use this for more than 32 bytes.
197533e34dc6SDavid Brownell  * Performance-sensitive or bulk transfer code should instead use
19760c868461SDavid Brownell  * spi_{async,sync}() calls with dma-safe buffers.
19778ae12a0dSDavid Brownell  */
19788ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi,
19790c4a1590SMark Brown 		const void *txbuf, unsigned n_tx,
19800c4a1590SMark Brown 		void *rxbuf, unsigned n_rx)
19818ae12a0dSDavid Brownell {
1982068f4070SDavid Brownell 	static DEFINE_MUTEX(lock);
19838ae12a0dSDavid Brownell 
19848ae12a0dSDavid Brownell 	int			status;
19858ae12a0dSDavid Brownell 	struct spi_message	message;
1986bdff549eSDavid Brownell 	struct spi_transfer	x[2];
19878ae12a0dSDavid Brownell 	u8			*local_buf;
19888ae12a0dSDavid Brownell 
1989b3a223eeSMark Brown 	/* Use preallocated DMA-safe buffer if we can.  We can't avoid
1990b3a223eeSMark Brown 	 * copying here, (as a pure convenience thing), but we can
1991b3a223eeSMark Brown 	 * keep heap costs out of the hot path unless someone else is
1992b3a223eeSMark Brown 	 * using the pre-allocated buffer or the transfer is too large.
19938ae12a0dSDavid Brownell 	 */
1994b3a223eeSMark Brown 	if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
19952cd94c8aSMark Brown 		local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
19962cd94c8aSMark Brown 				    GFP_KERNEL | GFP_DMA);
1997b3a223eeSMark Brown 		if (!local_buf)
1998b3a223eeSMark Brown 			return -ENOMEM;
1999b3a223eeSMark Brown 	} else {
2000b3a223eeSMark Brown 		local_buf = buf;
2001b3a223eeSMark Brown 	}
20028ae12a0dSDavid Brownell 
20038275c642SVitaly Wool 	spi_message_init(&message);
20045fe5f05eSJingoo Han 	memset(x, 0, sizeof(x));
2005bdff549eSDavid Brownell 	if (n_tx) {
2006bdff549eSDavid Brownell 		x[0].len = n_tx;
2007bdff549eSDavid Brownell 		spi_message_add_tail(&x[0], &message);
2008bdff549eSDavid Brownell 	}
2009bdff549eSDavid Brownell 	if (n_rx) {
2010bdff549eSDavid Brownell 		x[1].len = n_rx;
2011bdff549eSDavid Brownell 		spi_message_add_tail(&x[1], &message);
2012bdff549eSDavid Brownell 	}
20138275c642SVitaly Wool 
20148ae12a0dSDavid Brownell 	memcpy(local_buf, txbuf, n_tx);
2015bdff549eSDavid Brownell 	x[0].tx_buf = local_buf;
2016bdff549eSDavid Brownell 	x[1].rx_buf = local_buf + n_tx;
20178ae12a0dSDavid Brownell 
20188ae12a0dSDavid Brownell 	/* do the i/o */
20198ae12a0dSDavid Brownell 	status = spi_sync(spi, &message);
20209b938b74SMarc Pignat 	if (status == 0)
2021bdff549eSDavid Brownell 		memcpy(rxbuf, x[1].rx_buf, n_rx);
20228ae12a0dSDavid Brownell 
2023bdff549eSDavid Brownell 	if (x[0].tx_buf == buf)
2024068f4070SDavid Brownell 		mutex_unlock(&lock);
20258ae12a0dSDavid Brownell 	else
20268ae12a0dSDavid Brownell 		kfree(local_buf);
20278ae12a0dSDavid Brownell 
20288ae12a0dSDavid Brownell 	return status;
20298ae12a0dSDavid Brownell }
20308ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read);
20318ae12a0dSDavid Brownell 
20328ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
20338ae12a0dSDavid Brownell 
20348ae12a0dSDavid Brownell static int __init spi_init(void)
20358ae12a0dSDavid Brownell {
2036b885244eSDavid Brownell 	int	status;
20378ae12a0dSDavid Brownell 
2038e94b1766SChristoph Lameter 	buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
2039b885244eSDavid Brownell 	if (!buf) {
2040b885244eSDavid Brownell 		status = -ENOMEM;
2041b885244eSDavid Brownell 		goto err0;
20428ae12a0dSDavid Brownell 	}
2043b885244eSDavid Brownell 
2044b885244eSDavid Brownell 	status = bus_register(&spi_bus_type);
2045b885244eSDavid Brownell 	if (status < 0)
2046b885244eSDavid Brownell 		goto err1;
2047b885244eSDavid Brownell 
2048b885244eSDavid Brownell 	status = class_register(&spi_master_class);
2049b885244eSDavid Brownell 	if (status < 0)
2050b885244eSDavid Brownell 		goto err2;
2051b885244eSDavid Brownell 	return 0;
2052b885244eSDavid Brownell 
2053b885244eSDavid Brownell err2:
2054b885244eSDavid Brownell 	bus_unregister(&spi_bus_type);
2055b885244eSDavid Brownell err1:
2056b885244eSDavid Brownell 	kfree(buf);
2057b885244eSDavid Brownell 	buf = NULL;
2058b885244eSDavid Brownell err0:
2059b885244eSDavid Brownell 	return status;
2060b885244eSDavid Brownell }
2061b885244eSDavid Brownell 
20628ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(),
20638ae12a0dSDavid Brownell  * but even essential drivers wait till later
2064b885244eSDavid Brownell  *
2065b885244eSDavid Brownell  * REVISIT only boardinfo really needs static linking. the rest (device and
2066b885244eSDavid Brownell  * driver registration) _could_ be dynamically linked (modular) ... costs
2067b885244eSDavid Brownell  * include needing to have boardinfo data structures be much more public.
20688ae12a0dSDavid Brownell  */
2069673c0c00SDavid Brownell postcore_initcall(spi_init);
20708ae12a0dSDavid Brownell 
2071