xref: /linux/drivers/spi/spi.c (revision 8105936684681195d9073880b06a123b2e316811)
1b445bfcbSMarco Felsch // SPDX-License-Identifier: GPL-2.0-or-later
2787f4889SMark Brown // SPI init/core code
3787f4889SMark Brown //
4787f4889SMark Brown // Copyright (C) 2005 David Brownell
5787f4889SMark Brown // Copyright (C) 2008 Secret Lab Technologies Ltd.
68ae12a0dSDavid Brownell 
78ae12a0dSDavid Brownell #include <linux/kernel.h>
88ae12a0dSDavid Brownell #include <linux/device.h>
98ae12a0dSDavid Brownell #include <linux/init.h>
108ae12a0dSDavid Brownell #include <linux/cache.h>
1199adef31SMark Brown #include <linux/dma-mapping.h>
1299adef31SMark Brown #include <linux/dmaengine.h>
1394040828SMatthias Kaehlcke #include <linux/mutex.h>
142b7a32f7SSinan Akman #include <linux/of_device.h>
15d57a4282SGrant Likely #include <linux/of_irq.h>
1686be408bSSylwester Nawrocki #include <linux/clk/clk-conf.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
18e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h>
198ae12a0dSDavid Brownell #include <linux/spi/spi.h>
20b5932f5cSBoris Brezillon #include <linux/spi/spi-mem.h>
2174317984SJean-Christophe PLAGNIOL-VILLARD #include <linux/of_gpio.h>
22f3186dd8SLinus Walleij #include <linux/gpio/consumer.h>
233ae22e8cSMark Brown #include <linux/pm_runtime.h>
24f48c767cSUlf Hansson #include <linux/pm_domain.h>
25826cf175SDmitry Torokhov #include <linux/property.h>
26025ed130SPaul Gortmaker #include <linux/export.h>
278bd75c77SClark Williams #include <linux/sched/rt.h>
28ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h>
29ffbbdd21SLinus Walleij #include <linux/delay.h>
30ffbbdd21SLinus Walleij #include <linux/kthread.h>
3164bee4d2SMika Westerberg #include <linux/ioport.h>
3264bee4d2SMika Westerberg #include <linux/acpi.h>
33b1b8153cSVignesh R #include <linux/highmem.h>
349b61e302SSuniel Mahesh #include <linux/idr.h>
358a2e487eSLukas Wunner #include <linux/platform_data/x86/apple.h>
368ae12a0dSDavid Brownell 
3756ec1978SMark Brown #define CREATE_TRACE_POINTS
3856ec1978SMark Brown #include <trace/events/spi.h>
39ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
40ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
419b61e302SSuniel Mahesh 
4246336966SBoris Brezillon #include "internals.h"
4346336966SBoris Brezillon 
449b61e302SSuniel Mahesh static DEFINE_IDR(spi_master_idr);
4556ec1978SMark Brown 
468ae12a0dSDavid Brownell static void spidev_release(struct device *dev)
478ae12a0dSDavid Brownell {
480ffa0285SHans-Peter Nilsson 	struct spi_device	*spi = to_spi_device(dev);
498ae12a0dSDavid Brownell 
508caab75fSGeert Uytterhoeven 	/* spi controllers may cleanup for released devices */
518caab75fSGeert Uytterhoeven 	if (spi->controller->cleanup)
528caab75fSGeert Uytterhoeven 		spi->controller->cleanup(spi);
538ae12a0dSDavid Brownell 
548caab75fSGeert Uytterhoeven 	spi_controller_put(spi->controller);
555039563eSTrent Piepho 	kfree(spi->driver_override);
5607a389feSRoman Tereshonkov 	kfree(spi);
578ae12a0dSDavid Brownell }
588ae12a0dSDavid Brownell 
598ae12a0dSDavid Brownell static ssize_t
608ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf)
618ae12a0dSDavid Brownell {
628ae12a0dSDavid Brownell 	const struct spi_device	*spi = to_spi_device(dev);
638c4ff6d0SZhang Rui 	int len;
648c4ff6d0SZhang Rui 
658c4ff6d0SZhang Rui 	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
668c4ff6d0SZhang Rui 	if (len != -ENODEV)
678c4ff6d0SZhang Rui 		return len;
688ae12a0dSDavid Brownell 
69d8e328b3SGrant Likely 	return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
708ae12a0dSDavid Brownell }
71aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
728ae12a0dSDavid Brownell 
735039563eSTrent Piepho static ssize_t driver_override_store(struct device *dev,
745039563eSTrent Piepho 				     struct device_attribute *a,
755039563eSTrent Piepho 				     const char *buf, size_t count)
765039563eSTrent Piepho {
775039563eSTrent Piepho 	struct spi_device *spi = to_spi_device(dev);
785039563eSTrent Piepho 	const char *end = memchr(buf, '\n', count);
795039563eSTrent Piepho 	const size_t len = end ? end - buf : count;
805039563eSTrent Piepho 	const char *driver_override, *old;
815039563eSTrent Piepho 
825039563eSTrent Piepho 	/* We need to keep extra room for a newline when displaying value */
835039563eSTrent Piepho 	if (len >= (PAGE_SIZE - 1))
845039563eSTrent Piepho 		return -EINVAL;
855039563eSTrent Piepho 
865039563eSTrent Piepho 	driver_override = kstrndup(buf, len, GFP_KERNEL);
875039563eSTrent Piepho 	if (!driver_override)
885039563eSTrent Piepho 		return -ENOMEM;
895039563eSTrent Piepho 
905039563eSTrent Piepho 	device_lock(dev);
915039563eSTrent Piepho 	old = spi->driver_override;
925039563eSTrent Piepho 	if (len) {
935039563eSTrent Piepho 		spi->driver_override = driver_override;
945039563eSTrent Piepho 	} else {
955039563eSTrent Piepho 		/* Emptry string, disable driver override */
965039563eSTrent Piepho 		spi->driver_override = NULL;
975039563eSTrent Piepho 		kfree(driver_override);
985039563eSTrent Piepho 	}
995039563eSTrent Piepho 	device_unlock(dev);
1005039563eSTrent Piepho 	kfree(old);
1015039563eSTrent Piepho 
1025039563eSTrent Piepho 	return count;
1035039563eSTrent Piepho }
1045039563eSTrent Piepho 
1055039563eSTrent Piepho static ssize_t driver_override_show(struct device *dev,
1065039563eSTrent Piepho 				    struct device_attribute *a, char *buf)
1075039563eSTrent Piepho {
1085039563eSTrent Piepho 	const struct spi_device *spi = to_spi_device(dev);
1095039563eSTrent Piepho 	ssize_t len;
1105039563eSTrent Piepho 
1115039563eSTrent Piepho 	device_lock(dev);
1125039563eSTrent Piepho 	len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
1135039563eSTrent Piepho 	device_unlock(dev);
1145039563eSTrent Piepho 	return len;
1155039563eSTrent Piepho }
1165039563eSTrent Piepho static DEVICE_ATTR_RW(driver_override);
1175039563eSTrent Piepho 
118eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file)				\
1198caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev,	\
120eca2ebc7SMartin Sperl 					     struct device_attribute *attr, \
121eca2ebc7SMartin Sperl 					     char *buf)			\
122eca2ebc7SMartin Sperl {									\
1238caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = container_of(dev,			\
1248caab75fSGeert Uytterhoeven 					 struct spi_controller, dev);	\
1258caab75fSGeert Uytterhoeven 	return spi_statistics_##field##_show(&ctlr->statistics, buf);	\
126eca2ebc7SMartin Sperl }									\
1278caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = {	\
128ad25c92eSGeert Uytterhoeven 	.attr = { .name = file, .mode = 0444 },				\
1298caab75fSGeert Uytterhoeven 	.show = spi_controller_##field##_show,				\
130eca2ebc7SMartin Sperl };									\
131eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev,		\
132eca2ebc7SMartin Sperl 					 struct device_attribute *attr,	\
133eca2ebc7SMartin Sperl 					char *buf)			\
134eca2ebc7SMartin Sperl {									\
135d1eba93bSGeliang Tang 	struct spi_device *spi = to_spi_device(dev);			\
136eca2ebc7SMartin Sperl 	return spi_statistics_##field##_show(&spi->statistics, buf);	\
137eca2ebc7SMartin Sperl }									\
138eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = {		\
139ad25c92eSGeert Uytterhoeven 	.attr = { .name = file, .mode = 0444 },				\
140eca2ebc7SMartin Sperl 	.show = spi_device_##field##_show,				\
141eca2ebc7SMartin Sperl }
142eca2ebc7SMartin Sperl 
143eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string)	\
144eca2ebc7SMartin Sperl static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
145eca2ebc7SMartin Sperl 					    char *buf)			\
146eca2ebc7SMartin Sperl {									\
147eca2ebc7SMartin Sperl 	unsigned long flags;						\
148eca2ebc7SMartin Sperl 	ssize_t len;							\
149eca2ebc7SMartin Sperl 	spin_lock_irqsave(&stat->lock, flags);				\
150eca2ebc7SMartin Sperl 	len = sprintf(buf, format_string, stat->field);			\
151eca2ebc7SMartin Sperl 	spin_unlock_irqrestore(&stat->lock, flags);			\
152eca2ebc7SMartin Sperl 	return len;							\
153eca2ebc7SMartin Sperl }									\
154eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file)
155eca2ebc7SMartin Sperl 
156eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW(field, format_string)			\
157eca2ebc7SMartin Sperl 	SPI_STATISTICS_SHOW_NAME(field, __stringify(field),		\
158eca2ebc7SMartin Sperl 				 field, format_string)
159eca2ebc7SMartin Sperl 
160eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(messages, "%lu");
161eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(transfers, "%lu");
162eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(errors, "%lu");
163eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(timedout, "%lu");
164eca2ebc7SMartin Sperl 
165eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync, "%lu");
166eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
167eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_async, "%lu");
168eca2ebc7SMartin Sperl 
169eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes, "%llu");
170eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_rx, "%llu");
171eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_tx, "%llu");
172eca2ebc7SMartin Sperl 
1736b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number)		\
1746b7bc061SMartin Sperl 	SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index,		\
1756b7bc061SMartin Sperl 				 "transfer_bytes_histo_" number,	\
1766b7bc061SMartin Sperl 				 transfer_bytes_histo[index],  "%lu")
1776b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0,  "0-1");
1786b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1,  "2-3");
1796b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2,  "4-7");
1806b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3,  "8-15");
1816b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4,  "16-31");
1826b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5,  "32-63");
1836b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6,  "64-127");
1846b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7,  "128-255");
1856b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8,  "256-511");
1866b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9,  "512-1023");
1876b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
1886b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
1896b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
1906b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
1916b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
1926b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
1936b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
1946b7bc061SMartin Sperl 
195d9f12122SMartin Sperl SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
196d9f12122SMartin Sperl 
197aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = {
198aa7da564SGreg Kroah-Hartman 	&dev_attr_modalias.attr,
1995039563eSTrent Piepho 	&dev_attr_driver_override.attr,
200aa7da564SGreg Kroah-Hartman 	NULL,
2018ae12a0dSDavid Brownell };
202eca2ebc7SMartin Sperl 
203eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = {
204eca2ebc7SMartin Sperl 	.attrs  = spi_dev_attrs,
205eca2ebc7SMartin Sperl };
206eca2ebc7SMartin Sperl 
207eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = {
208eca2ebc7SMartin Sperl 	&dev_attr_spi_device_messages.attr,
209eca2ebc7SMartin Sperl 	&dev_attr_spi_device_transfers.attr,
210eca2ebc7SMartin Sperl 	&dev_attr_spi_device_errors.attr,
211eca2ebc7SMartin Sperl 	&dev_attr_spi_device_timedout.attr,
212eca2ebc7SMartin Sperl 	&dev_attr_spi_device_spi_sync.attr,
213eca2ebc7SMartin Sperl 	&dev_attr_spi_device_spi_sync_immediate.attr,
214eca2ebc7SMartin Sperl 	&dev_attr_spi_device_spi_async.attr,
215eca2ebc7SMartin Sperl 	&dev_attr_spi_device_bytes.attr,
216eca2ebc7SMartin Sperl 	&dev_attr_spi_device_bytes_rx.attr,
217eca2ebc7SMartin Sperl 	&dev_attr_spi_device_bytes_tx.attr,
2186b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo0.attr,
2196b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo1.attr,
2206b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo2.attr,
2216b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo3.attr,
2226b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo4.attr,
2236b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo5.attr,
2246b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo6.attr,
2256b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo7.attr,
2266b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo8.attr,
2276b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo9.attr,
2286b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo10.attr,
2296b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo11.attr,
2306b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo12.attr,
2316b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo13.attr,
2326b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo14.attr,
2336b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo15.attr,
2346b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo16.attr,
235d9f12122SMartin Sperl 	&dev_attr_spi_device_transfers_split_maxsize.attr,
236eca2ebc7SMartin Sperl 	NULL,
237eca2ebc7SMartin Sperl };
238eca2ebc7SMartin Sperl 
239eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = {
240eca2ebc7SMartin Sperl 	.name  = "statistics",
241eca2ebc7SMartin Sperl 	.attrs  = spi_device_statistics_attrs,
242eca2ebc7SMartin Sperl };
243eca2ebc7SMartin Sperl 
244eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = {
245eca2ebc7SMartin Sperl 	&spi_dev_group,
246eca2ebc7SMartin Sperl 	&spi_device_statistics_group,
247eca2ebc7SMartin Sperl 	NULL,
248eca2ebc7SMartin Sperl };
249eca2ebc7SMartin Sperl 
2508caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = {
2518caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_messages.attr,
2528caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfers.attr,
2538caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_errors.attr,
2548caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_timedout.attr,
2558caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_spi_sync.attr,
2568caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_spi_sync_immediate.attr,
2578caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_spi_async.attr,
2588caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_bytes.attr,
2598caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_bytes_rx.attr,
2608caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_bytes_tx.attr,
2618caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo0.attr,
2628caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo1.attr,
2638caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo2.attr,
2648caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo3.attr,
2658caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo4.attr,
2668caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo5.attr,
2678caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo6.attr,
2688caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo7.attr,
2698caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo8.attr,
2708caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo9.attr,
2718caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo10.attr,
2728caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo11.attr,
2738caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo12.attr,
2748caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo13.attr,
2758caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo14.attr,
2768caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo15.attr,
2778caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo16.attr,
2788caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfers_split_maxsize.attr,
279eca2ebc7SMartin Sperl 	NULL,
280eca2ebc7SMartin Sperl };
281eca2ebc7SMartin Sperl 
2828caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = {
283eca2ebc7SMartin Sperl 	.name  = "statistics",
2848caab75fSGeert Uytterhoeven 	.attrs  = spi_controller_statistics_attrs,
285eca2ebc7SMartin Sperl };
286eca2ebc7SMartin Sperl 
287eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = {
2888caab75fSGeert Uytterhoeven 	&spi_controller_statistics_group,
289eca2ebc7SMartin Sperl 	NULL,
290eca2ebc7SMartin Sperl };
291eca2ebc7SMartin Sperl 
292eca2ebc7SMartin Sperl void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
293eca2ebc7SMartin Sperl 				       struct spi_transfer *xfer,
2948caab75fSGeert Uytterhoeven 				       struct spi_controller *ctlr)
295eca2ebc7SMartin Sperl {
296eca2ebc7SMartin Sperl 	unsigned long flags;
2976b7bc061SMartin Sperl 	int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
2986b7bc061SMartin Sperl 
2996b7bc061SMartin Sperl 	if (l2len < 0)
3006b7bc061SMartin Sperl 		l2len = 0;
301eca2ebc7SMartin Sperl 
302eca2ebc7SMartin Sperl 	spin_lock_irqsave(&stats->lock, flags);
303eca2ebc7SMartin Sperl 
304eca2ebc7SMartin Sperl 	stats->transfers++;
3056b7bc061SMartin Sperl 	stats->transfer_bytes_histo[l2len]++;
306eca2ebc7SMartin Sperl 
307eca2ebc7SMartin Sperl 	stats->bytes += xfer->len;
308eca2ebc7SMartin Sperl 	if ((xfer->tx_buf) &&
3098caab75fSGeert Uytterhoeven 	    (xfer->tx_buf != ctlr->dummy_tx))
310eca2ebc7SMartin Sperl 		stats->bytes_tx += xfer->len;
311eca2ebc7SMartin Sperl 	if ((xfer->rx_buf) &&
3128caab75fSGeert Uytterhoeven 	    (xfer->rx_buf != ctlr->dummy_rx))
313eca2ebc7SMartin Sperl 		stats->bytes_rx += xfer->len;
314eca2ebc7SMartin Sperl 
315eca2ebc7SMartin Sperl 	spin_unlock_irqrestore(&stats->lock, flags);
316eca2ebc7SMartin Sperl }
317eca2ebc7SMartin Sperl EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
3188ae12a0dSDavid Brownell 
3198ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
3208ae12a0dSDavid Brownell  * and the sysfs version makes coldplug work too.
3218ae12a0dSDavid Brownell  */
3228ae12a0dSDavid Brownell 
32375368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
32475368bf6SAnton Vorontsov 						const struct spi_device *sdev)
32575368bf6SAnton Vorontsov {
32675368bf6SAnton Vorontsov 	while (id->name[0]) {
32775368bf6SAnton Vorontsov 		if (!strcmp(sdev->modalias, id->name))
32875368bf6SAnton Vorontsov 			return id;
32975368bf6SAnton Vorontsov 		id++;
33075368bf6SAnton Vorontsov 	}
33175368bf6SAnton Vorontsov 	return NULL;
33275368bf6SAnton Vorontsov }
33375368bf6SAnton Vorontsov 
33475368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
33575368bf6SAnton Vorontsov {
33675368bf6SAnton Vorontsov 	const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
33775368bf6SAnton Vorontsov 
33875368bf6SAnton Vorontsov 	return spi_match_id(sdrv->id_table, sdev);
33975368bf6SAnton Vorontsov }
34075368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id);
34175368bf6SAnton Vorontsov 
3428ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv)
3438ae12a0dSDavid Brownell {
3448ae12a0dSDavid Brownell 	const struct spi_device	*spi = to_spi_device(dev);
34575368bf6SAnton Vorontsov 	const struct spi_driver	*sdrv = to_spi_driver(drv);
34675368bf6SAnton Vorontsov 
3475039563eSTrent Piepho 	/* Check override first, and if set, only use the named driver */
3485039563eSTrent Piepho 	if (spi->driver_override)
3495039563eSTrent Piepho 		return strcmp(spi->driver_override, drv->name) == 0;
3505039563eSTrent Piepho 
3512b7a32f7SSinan Akman 	/* Attempt an OF style match */
3522b7a32f7SSinan Akman 	if (of_driver_match_device(dev, drv))
3532b7a32f7SSinan Akman 		return 1;
3542b7a32f7SSinan Akman 
35564bee4d2SMika Westerberg 	/* Then try ACPI */
35664bee4d2SMika Westerberg 	if (acpi_driver_match_device(dev, drv))
35764bee4d2SMika Westerberg 		return 1;
35864bee4d2SMika Westerberg 
35975368bf6SAnton Vorontsov 	if (sdrv->id_table)
36075368bf6SAnton Vorontsov 		return !!spi_match_id(sdrv->id_table, spi);
3618ae12a0dSDavid Brownell 
36235f74fcaSKay Sievers 	return strcmp(spi->modalias, drv->name) == 0;
3638ae12a0dSDavid Brownell }
3648ae12a0dSDavid Brownell 
3657eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
3668ae12a0dSDavid Brownell {
3678ae12a0dSDavid Brownell 	const struct spi_device		*spi = to_spi_device(dev);
3688c4ff6d0SZhang Rui 	int rc;
3698c4ff6d0SZhang Rui 
3708c4ff6d0SZhang Rui 	rc = acpi_device_uevent_modalias(dev, env);
3718c4ff6d0SZhang Rui 	if (rc != -ENODEV)
3728c4ff6d0SZhang Rui 		return rc;
3738ae12a0dSDavid Brownell 
3742856670fSAndy Shevchenko 	return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
3758ae12a0dSDavid Brownell }
3768ae12a0dSDavid Brownell 
3778ae12a0dSDavid Brownell struct bus_type spi_bus_type = {
3788ae12a0dSDavid Brownell 	.name		= "spi",
379aa7da564SGreg Kroah-Hartman 	.dev_groups	= spi_dev_groups,
3808ae12a0dSDavid Brownell 	.match		= spi_match_device,
3818ae12a0dSDavid Brownell 	.uevent		= spi_uevent,
3828ae12a0dSDavid Brownell };
3838ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type);
3848ae12a0dSDavid Brownell 
385b885244eSDavid Brownell 
386b885244eSDavid Brownell static int spi_drv_probe(struct device *dev)
387b885244eSDavid Brownell {
388b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
38944af7927SJon Hunter 	struct spi_device		*spi = to_spi_device(dev);
39033cf00e5SMika Westerberg 	int ret;
391b885244eSDavid Brownell 
39286be408bSSylwester Nawrocki 	ret = of_clk_set_defaults(dev->of_node, false);
39386be408bSSylwester Nawrocki 	if (ret)
39486be408bSSylwester Nawrocki 		return ret;
39586be408bSSylwester Nawrocki 
39644af7927SJon Hunter 	if (dev->of_node) {
39744af7927SJon Hunter 		spi->irq = of_irq_get(dev->of_node, 0);
39844af7927SJon Hunter 		if (spi->irq == -EPROBE_DEFER)
39944af7927SJon Hunter 			return -EPROBE_DEFER;
40044af7927SJon Hunter 		if (spi->irq < 0)
40144af7927SJon Hunter 			spi->irq = 0;
40244af7927SJon Hunter 	}
40344af7927SJon Hunter 
404676e7c25SUlf Hansson 	ret = dev_pm_domain_attach(dev, true);
40571f277a7SUlf Hansson 	if (ret)
40671f277a7SUlf Hansson 		return ret;
40771f277a7SUlf Hansson 
40844af7927SJon Hunter 	ret = sdrv->probe(spi);
40933cf00e5SMika Westerberg 	if (ret)
410676e7c25SUlf Hansson 		dev_pm_domain_detach(dev, true);
41133cf00e5SMika Westerberg 
41233cf00e5SMika Westerberg 	return ret;
413b885244eSDavid Brownell }
414b885244eSDavid Brownell 
415b885244eSDavid Brownell static int spi_drv_remove(struct device *dev)
416b885244eSDavid Brownell {
417b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
41833cf00e5SMika Westerberg 	int ret;
419b885244eSDavid Brownell 
420aec35f4eSJean Delvare 	ret = sdrv->remove(to_spi_device(dev));
421676e7c25SUlf Hansson 	dev_pm_domain_detach(dev, true);
42233cf00e5SMika Westerberg 
42333cf00e5SMika Westerberg 	return ret;
424b885244eSDavid Brownell }
425b885244eSDavid Brownell 
426b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev)
427b885244eSDavid Brownell {
428b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
429b885244eSDavid Brownell 
430b885244eSDavid Brownell 	sdrv->shutdown(to_spi_device(dev));
431b885244eSDavid Brownell }
432b885244eSDavid Brownell 
43333e34dc6SDavid Brownell /**
434ca5d2485SAndrew F. Davis  * __spi_register_driver - register a SPI driver
43588c9321dSThierry Reding  * @owner: owner module of the driver to register
43633e34dc6SDavid Brownell  * @sdrv: the driver to register
43733e34dc6SDavid Brownell  * Context: can sleep
43897d56dc6SJavier Martinez Canillas  *
43997d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
44033e34dc6SDavid Brownell  */
441ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
442b885244eSDavid Brownell {
443ca5d2485SAndrew F. Davis 	sdrv->driver.owner = owner;
444b885244eSDavid Brownell 	sdrv->driver.bus = &spi_bus_type;
445b885244eSDavid Brownell 	if (sdrv->probe)
446b885244eSDavid Brownell 		sdrv->driver.probe = spi_drv_probe;
447b885244eSDavid Brownell 	if (sdrv->remove)
448b885244eSDavid Brownell 		sdrv->driver.remove = spi_drv_remove;
449b885244eSDavid Brownell 	if (sdrv->shutdown)
450b885244eSDavid Brownell 		sdrv->driver.shutdown = spi_drv_shutdown;
451b885244eSDavid Brownell 	return driver_register(&sdrv->driver);
452b885244eSDavid Brownell }
453ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver);
454b885244eSDavid Brownell 
4558ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
4568ae12a0dSDavid Brownell 
4578ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that
4588caab75fSGeert Uytterhoeven  * would make them board-specific.  Similarly with SPI controller drivers.
4598ae12a0dSDavid Brownell  * Device registration normally goes into like arch/.../mach.../board-YYY.c
4608ae12a0dSDavid Brownell  * with other readonly (flashable) information about mainboard devices.
4618ae12a0dSDavid Brownell  */
4628ae12a0dSDavid Brownell 
4638ae12a0dSDavid Brownell struct boardinfo {
4648ae12a0dSDavid Brownell 	struct list_head	list;
4652b9603a0SFeng Tang 	struct spi_board_info	board_info;
4668ae12a0dSDavid Brownell };
4678ae12a0dSDavid Brownell 
4688ae12a0dSDavid Brownell static LIST_HEAD(board_list);
4698caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list);
4702b9603a0SFeng Tang 
4712b9603a0SFeng Tang /*
4722b9603a0SFeng Tang  * Used to protect add/del opertion for board_info list and
4738caab75fSGeert Uytterhoeven  * spi_controller list, and their matching process
4749b61e302SSuniel Mahesh  * also used to protect object of type struct idr
4752b9603a0SFeng Tang  */
47694040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock);
4778ae12a0dSDavid Brownell 
478dc87c98eSGrant Likely /**
479dc87c98eSGrant Likely  * spi_alloc_device - Allocate a new SPI device
4808caab75fSGeert Uytterhoeven  * @ctlr: Controller to which device is connected
481dc87c98eSGrant Likely  * Context: can sleep
482dc87c98eSGrant Likely  *
483dc87c98eSGrant Likely  * Allows a driver to allocate and initialize a spi_device without
484dc87c98eSGrant Likely  * registering it immediately.  This allows a driver to directly
485dc87c98eSGrant Likely  * fill the spi_device with device parameters before calling
486dc87c98eSGrant Likely  * spi_add_device() on it.
487dc87c98eSGrant Likely  *
488dc87c98eSGrant Likely  * Caller is responsible to call spi_add_device() on the returned
4898caab75fSGeert Uytterhoeven  * spi_device structure to add it to the SPI controller.  If the caller
490dc87c98eSGrant Likely  * needs to discard the spi_device without adding it, then it should
491dc87c98eSGrant Likely  * call spi_dev_put() on it.
492dc87c98eSGrant Likely  *
49397d56dc6SJavier Martinez Canillas  * Return: a pointer to the new device, or NULL.
494dc87c98eSGrant Likely  */
4958caab75fSGeert Uytterhoeven struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
496dc87c98eSGrant Likely {
497dc87c98eSGrant Likely 	struct spi_device	*spi;
498dc87c98eSGrant Likely 
4998caab75fSGeert Uytterhoeven 	if (!spi_controller_get(ctlr))
500dc87c98eSGrant Likely 		return NULL;
501dc87c98eSGrant Likely 
5025fe5f05eSJingoo Han 	spi = kzalloc(sizeof(*spi), GFP_KERNEL);
503dc87c98eSGrant Likely 	if (!spi) {
5048caab75fSGeert Uytterhoeven 		spi_controller_put(ctlr);
505dc87c98eSGrant Likely 		return NULL;
506dc87c98eSGrant Likely 	}
507dc87c98eSGrant Likely 
5088caab75fSGeert Uytterhoeven 	spi->master = spi->controller = ctlr;
5098caab75fSGeert Uytterhoeven 	spi->dev.parent = &ctlr->dev;
510dc87c98eSGrant Likely 	spi->dev.bus = &spi_bus_type;
511dc87c98eSGrant Likely 	spi->dev.release = spidev_release;
512446411e1SAndreas Larsson 	spi->cs_gpio = -ENOENT;
513eca2ebc7SMartin Sperl 
514eca2ebc7SMartin Sperl 	spin_lock_init(&spi->statistics.lock);
515eca2ebc7SMartin Sperl 
516dc87c98eSGrant Likely 	device_initialize(&spi->dev);
517dc87c98eSGrant Likely 	return spi;
518dc87c98eSGrant Likely }
519dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device);
520dc87c98eSGrant Likely 
521e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi)
522e13ac47bSJarkko Nikula {
523e13ac47bSJarkko Nikula 	struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
524e13ac47bSJarkko Nikula 
525e13ac47bSJarkko Nikula 	if (adev) {
526e13ac47bSJarkko Nikula 		dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
527e13ac47bSJarkko Nikula 		return;
528e13ac47bSJarkko Nikula 	}
529e13ac47bSJarkko Nikula 
5308caab75fSGeert Uytterhoeven 	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
531e13ac47bSJarkko Nikula 		     spi->chip_select);
532e13ac47bSJarkko Nikula }
533e13ac47bSJarkko Nikula 
534b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data)
535b6fb8d3aSMika Westerberg {
536b6fb8d3aSMika Westerberg 	struct spi_device *spi = to_spi_device(dev);
537b6fb8d3aSMika Westerberg 	struct spi_device *new_spi = data;
538b6fb8d3aSMika Westerberg 
5398caab75fSGeert Uytterhoeven 	if (spi->controller == new_spi->controller &&
540b6fb8d3aSMika Westerberg 	    spi->chip_select == new_spi->chip_select)
541b6fb8d3aSMika Westerberg 		return -EBUSY;
542b6fb8d3aSMika Westerberg 	return 0;
543b6fb8d3aSMika Westerberg }
544b6fb8d3aSMika Westerberg 
545dc87c98eSGrant Likely /**
546dc87c98eSGrant Likely  * spi_add_device - Add spi_device allocated with spi_alloc_device
547dc87c98eSGrant Likely  * @spi: spi_device to register
548dc87c98eSGrant Likely  *
549dc87c98eSGrant Likely  * Companion function to spi_alloc_device.  Devices allocated with
550dc87c98eSGrant Likely  * spi_alloc_device can be added onto the spi bus with this function.
551dc87c98eSGrant Likely  *
55297d56dc6SJavier Martinez Canillas  * Return: 0 on success; negative errno on failure
553dc87c98eSGrant Likely  */
554dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi)
555dc87c98eSGrant Likely {
556e48880e0SDavid Brownell 	static DEFINE_MUTEX(spi_add_lock);
5578caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
5588caab75fSGeert Uytterhoeven 	struct device *dev = ctlr->dev.parent;
559dc87c98eSGrant Likely 	int status;
560dc87c98eSGrant Likely 
561dc87c98eSGrant Likely 	/* Chipselects are numbered 0..max; validate. */
5628caab75fSGeert Uytterhoeven 	if (spi->chip_select >= ctlr->num_chipselect) {
5638caab75fSGeert Uytterhoeven 		dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
5648caab75fSGeert Uytterhoeven 			ctlr->num_chipselect);
565dc87c98eSGrant Likely 		return -EINVAL;
566dc87c98eSGrant Likely 	}
567dc87c98eSGrant Likely 
568dc87c98eSGrant Likely 	/* Set the bus ID string */
569e13ac47bSJarkko Nikula 	spi_dev_set_name(spi);
570e48880e0SDavid Brownell 
571e48880e0SDavid Brownell 	/* We need to make sure there's no other device with this
572e48880e0SDavid Brownell 	 * chipselect **BEFORE** we call setup(), else we'll trash
573e48880e0SDavid Brownell 	 * its configuration.  Lock against concurrent add() calls.
574e48880e0SDavid Brownell 	 */
575e48880e0SDavid Brownell 	mutex_lock(&spi_add_lock);
576e48880e0SDavid Brownell 
577b6fb8d3aSMika Westerberg 	status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
578b6fb8d3aSMika Westerberg 	if (status) {
579e48880e0SDavid Brownell 		dev_err(dev, "chipselect %d already in use\n",
580e48880e0SDavid Brownell 				spi->chip_select);
581e48880e0SDavid Brownell 		goto done;
582e48880e0SDavid Brownell 	}
583e48880e0SDavid Brownell 
584f3186dd8SLinus Walleij 	/* Descriptors take precedence */
585f3186dd8SLinus Walleij 	if (ctlr->cs_gpiods)
586f3186dd8SLinus Walleij 		spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
587f3186dd8SLinus Walleij 	else if (ctlr->cs_gpios)
5888caab75fSGeert Uytterhoeven 		spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
58974317984SJean-Christophe PLAGNIOL-VILLARD 
590e48880e0SDavid Brownell 	/* Drivers may modify this initial i/o setup, but will
591e48880e0SDavid Brownell 	 * normally rely on the device being setup.  Devices
592e48880e0SDavid Brownell 	 * using SPI_CS_HIGH can't coexist well otherwise...
593e48880e0SDavid Brownell 	 */
5947d077197SDavid Brownell 	status = spi_setup(spi);
595dc87c98eSGrant Likely 	if (status < 0) {
596eb288a1fSLinus Walleij 		dev_err(dev, "can't setup %s, status %d\n",
597eb288a1fSLinus Walleij 				dev_name(&spi->dev), status);
598e48880e0SDavid Brownell 		goto done;
599dc87c98eSGrant Likely 	}
600dc87c98eSGrant Likely 
601e48880e0SDavid Brownell 	/* Device may be bound to an active driver when this returns */
602dc87c98eSGrant Likely 	status = device_add(&spi->dev);
603e48880e0SDavid Brownell 	if (status < 0)
604eb288a1fSLinus Walleij 		dev_err(dev, "can't add %s, status %d\n",
605eb288a1fSLinus Walleij 				dev_name(&spi->dev), status);
606e48880e0SDavid Brownell 	else
60735f74fcaSKay Sievers 		dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
608e48880e0SDavid Brownell 
609e48880e0SDavid Brownell done:
610e48880e0SDavid Brownell 	mutex_unlock(&spi_add_lock);
611e48880e0SDavid Brownell 	return status;
612dc87c98eSGrant Likely }
613dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device);
6148ae12a0dSDavid Brownell 
61533e34dc6SDavid Brownell /**
61633e34dc6SDavid Brownell  * spi_new_device - instantiate one new SPI device
6178caab75fSGeert Uytterhoeven  * @ctlr: Controller to which device is connected
61833e34dc6SDavid Brownell  * @chip: Describes the SPI device
61933e34dc6SDavid Brownell  * Context: can sleep
62033e34dc6SDavid Brownell  *
62133e34dc6SDavid Brownell  * On typical mainboards, this is purely internal; and it's not needed
6228ae12a0dSDavid Brownell  * after board init creates the hard-wired devices.  Some development
6238ae12a0dSDavid Brownell  * platforms may not be able to use spi_register_board_info though, and
6248ae12a0dSDavid Brownell  * this is exported so that for example a USB or parport based adapter
6258ae12a0dSDavid Brownell  * driver could add devices (which it would learn about out-of-band).
626082c8cb4SDavid Brownell  *
62797d56dc6SJavier Martinez Canillas  * Return: the new device, or NULL.
6288ae12a0dSDavid Brownell  */
6298caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr,
630e9d5a461SAdrian Bunk 				  struct spi_board_info *chip)
6318ae12a0dSDavid Brownell {
6328ae12a0dSDavid Brownell 	struct spi_device	*proxy;
6338ae12a0dSDavid Brownell 	int			status;
6348ae12a0dSDavid Brownell 
635082c8cb4SDavid Brownell 	/* NOTE:  caller did any chip->bus_num checks necessary.
636082c8cb4SDavid Brownell 	 *
637082c8cb4SDavid Brownell 	 * Also, unless we change the return value convention to use
638082c8cb4SDavid Brownell 	 * error-or-pointer (not NULL-or-pointer), troubleshootability
639082c8cb4SDavid Brownell 	 * suggests syslogged diagnostics are best here (ugh).
640082c8cb4SDavid Brownell 	 */
641082c8cb4SDavid Brownell 
6428caab75fSGeert Uytterhoeven 	proxy = spi_alloc_device(ctlr);
643dc87c98eSGrant Likely 	if (!proxy)
6448ae12a0dSDavid Brownell 		return NULL;
6458ae12a0dSDavid Brownell 
646102eb975SGrant Likely 	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
647102eb975SGrant Likely 
6488ae12a0dSDavid Brownell 	proxy->chip_select = chip->chip_select;
6498ae12a0dSDavid Brownell 	proxy->max_speed_hz = chip->max_speed_hz;
650980a01c9SDavid Brownell 	proxy->mode = chip->mode;
6518ae12a0dSDavid Brownell 	proxy->irq = chip->irq;
652102eb975SGrant Likely 	strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
6538ae12a0dSDavid Brownell 	proxy->dev.platform_data = (void *) chip->platform_data;
6548ae12a0dSDavid Brownell 	proxy->controller_data = chip->controller_data;
6558ae12a0dSDavid Brownell 	proxy->controller_state = NULL;
6568ae12a0dSDavid Brownell 
657826cf175SDmitry Torokhov 	if (chip->properties) {
658826cf175SDmitry Torokhov 		status = device_add_properties(&proxy->dev, chip->properties);
659826cf175SDmitry Torokhov 		if (status) {
6608caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev,
661826cf175SDmitry Torokhov 				"failed to add properties to '%s': %d\n",
662826cf175SDmitry Torokhov 				chip->modalias, status);
663826cf175SDmitry Torokhov 			goto err_dev_put;
664826cf175SDmitry Torokhov 		}
6658ae12a0dSDavid Brownell 	}
666dc87c98eSGrant Likely 
667826cf175SDmitry Torokhov 	status = spi_add_device(proxy);
668826cf175SDmitry Torokhov 	if (status < 0)
669826cf175SDmitry Torokhov 		goto err_remove_props;
670826cf175SDmitry Torokhov 
671dc87c98eSGrant Likely 	return proxy;
672826cf175SDmitry Torokhov 
673826cf175SDmitry Torokhov err_remove_props:
674826cf175SDmitry Torokhov 	if (chip->properties)
675826cf175SDmitry Torokhov 		device_remove_properties(&proxy->dev);
676826cf175SDmitry Torokhov err_dev_put:
677826cf175SDmitry Torokhov 	spi_dev_put(proxy);
678826cf175SDmitry Torokhov 	return NULL;
679dc87c98eSGrant Likely }
6808ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device);
6818ae12a0dSDavid Brownell 
6823b1884c2SGeert Uytterhoeven /**
6833b1884c2SGeert Uytterhoeven  * spi_unregister_device - unregister a single SPI device
6843b1884c2SGeert Uytterhoeven  * @spi: spi_device to unregister
6853b1884c2SGeert Uytterhoeven  *
6863b1884c2SGeert Uytterhoeven  * Start making the passed SPI device vanish. Normally this would be handled
6878caab75fSGeert Uytterhoeven  * by spi_unregister_controller().
6883b1884c2SGeert Uytterhoeven  */
6893b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi)
6903b1884c2SGeert Uytterhoeven {
691bd6c1644SGeert Uytterhoeven 	if (!spi)
692bd6c1644SGeert Uytterhoeven 		return;
693bd6c1644SGeert Uytterhoeven 
6948324147fSJohan Hovold 	if (spi->dev.of_node) {
695bd6c1644SGeert Uytterhoeven 		of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
6968324147fSJohan Hovold 		of_node_put(spi->dev.of_node);
6978324147fSJohan Hovold 	}
6987f24467fSOctavian Purdila 	if (ACPI_COMPANION(&spi->dev))
6997f24467fSOctavian Purdila 		acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
7003b1884c2SGeert Uytterhoeven 	device_unregister(&spi->dev);
7013b1884c2SGeert Uytterhoeven }
7023b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device);
7033b1884c2SGeert Uytterhoeven 
7048caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
7052b9603a0SFeng Tang 					      struct spi_board_info *bi)
7062b9603a0SFeng Tang {
7072b9603a0SFeng Tang 	struct spi_device *dev;
7082b9603a0SFeng Tang 
7098caab75fSGeert Uytterhoeven 	if (ctlr->bus_num != bi->bus_num)
7102b9603a0SFeng Tang 		return;
7112b9603a0SFeng Tang 
7128caab75fSGeert Uytterhoeven 	dev = spi_new_device(ctlr, bi);
7132b9603a0SFeng Tang 	if (!dev)
7148caab75fSGeert Uytterhoeven 		dev_err(ctlr->dev.parent, "can't create new device for %s\n",
7152b9603a0SFeng Tang 			bi->modalias);
7162b9603a0SFeng Tang }
7172b9603a0SFeng Tang 
71833e34dc6SDavid Brownell /**
71933e34dc6SDavid Brownell  * spi_register_board_info - register SPI devices for a given board
72033e34dc6SDavid Brownell  * @info: array of chip descriptors
72133e34dc6SDavid Brownell  * @n: how many descriptors are provided
72233e34dc6SDavid Brownell  * Context: can sleep
72333e34dc6SDavid Brownell  *
7248ae12a0dSDavid Brownell  * Board-specific early init code calls this (probably during arch_initcall)
7258ae12a0dSDavid Brownell  * with segments of the SPI device table.  Any device nodes are created later,
7268ae12a0dSDavid Brownell  * after the relevant parent SPI controller (bus_num) is defined.  We keep
7278ae12a0dSDavid Brownell  * this table of devices forever, so that reloading a controller driver will
7288ae12a0dSDavid Brownell  * not make Linux forget about these hard-wired devices.
7298ae12a0dSDavid Brownell  *
7308ae12a0dSDavid Brownell  * Other code can also call this, e.g. a particular add-on board might provide
7318ae12a0dSDavid Brownell  * SPI devices through its expansion connector, so code initializing that board
7328ae12a0dSDavid Brownell  * would naturally declare its SPI devices.
7338ae12a0dSDavid Brownell  *
7348ae12a0dSDavid Brownell  * The board info passed can safely be __initdata ... but be careful of
7358ae12a0dSDavid Brownell  * any embedded pointers (platform_data, etc), they're copied as-is.
736826cf175SDmitry Torokhov  * Device properties are deep-copied though.
73797d56dc6SJavier Martinez Canillas  *
73897d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
7398ae12a0dSDavid Brownell  */
740fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n)
7418ae12a0dSDavid Brownell {
7428ae12a0dSDavid Brownell 	struct boardinfo *bi;
7432b9603a0SFeng Tang 	int i;
7448ae12a0dSDavid Brownell 
745c7908a37SXiubo Li 	if (!n)
746f974cf57SDmitry Torokhov 		return 0;
747c7908a37SXiubo Li 
748f9bdb7fdSMarkus Elfring 	bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
7498ae12a0dSDavid Brownell 	if (!bi)
7508ae12a0dSDavid Brownell 		return -ENOMEM;
7518ae12a0dSDavid Brownell 
7522b9603a0SFeng Tang 	for (i = 0; i < n; i++, bi++, info++) {
7538caab75fSGeert Uytterhoeven 		struct spi_controller *ctlr;
7542b9603a0SFeng Tang 
7552b9603a0SFeng Tang 		memcpy(&bi->board_info, info, sizeof(*info));
756826cf175SDmitry Torokhov 		if (info->properties) {
757826cf175SDmitry Torokhov 			bi->board_info.properties =
758826cf175SDmitry Torokhov 					property_entries_dup(info->properties);
759826cf175SDmitry Torokhov 			if (IS_ERR(bi->board_info.properties))
760826cf175SDmitry Torokhov 				return PTR_ERR(bi->board_info.properties);
761826cf175SDmitry Torokhov 		}
762826cf175SDmitry Torokhov 
76394040828SMatthias Kaehlcke 		mutex_lock(&board_lock);
7648ae12a0dSDavid Brownell 		list_add_tail(&bi->list, &board_list);
7658caab75fSGeert Uytterhoeven 		list_for_each_entry(ctlr, &spi_controller_list, list)
7668caab75fSGeert Uytterhoeven 			spi_match_controller_to_boardinfo(ctlr,
7678caab75fSGeert Uytterhoeven 							  &bi->board_info);
76894040828SMatthias Kaehlcke 		mutex_unlock(&board_lock);
7692b9603a0SFeng Tang 	}
7702b9603a0SFeng Tang 
7718ae12a0dSDavid Brownell 	return 0;
7728ae12a0dSDavid Brownell }
7738ae12a0dSDavid Brownell 
7748ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
7758ae12a0dSDavid Brownell 
776b158935fSMark Brown static void spi_set_cs(struct spi_device *spi, bool enable)
777b158935fSMark Brown {
778b158935fSMark Brown 	if (spi->mode & SPI_CS_HIGH)
779b158935fSMark Brown 		enable = !enable;
780b158935fSMark Brown 
781f3186dd8SLinus Walleij 	if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
782f3186dd8SLinus Walleij 		/*
783f3186dd8SLinus Walleij 		 * Honour the SPI_NO_CS flag and invert the enable line, as
784f3186dd8SLinus Walleij 		 * active low is default for SPI. Execution paths that handle
785f3186dd8SLinus Walleij 		 * polarity inversion in gpiolib (such as device tree) will
786f3186dd8SLinus Walleij 		 * enforce active high using the SPI_CS_HIGH resulting in a
787f3186dd8SLinus Walleij 		 * double inversion through the code above.
788f3186dd8SLinus Walleij 		 */
789f3186dd8SLinus Walleij 		if (!(spi->mode & SPI_NO_CS)) {
790f3186dd8SLinus Walleij 			if (spi->cs_gpiod)
79128f7604fSFelix Fietkau 				gpiod_set_value_cansleep(spi->cs_gpiod,
79228f7604fSFelix Fietkau 							 !enable);
793f3186dd8SLinus Walleij 			else
79428f7604fSFelix Fietkau 				gpio_set_value_cansleep(spi->cs_gpio, !enable);
795f3186dd8SLinus Walleij 		}
7968eee6b9dSThor Thayer 		/* Some SPI masters need both GPIO CS & slave_select */
7978caab75fSGeert Uytterhoeven 		if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
7988caab75fSGeert Uytterhoeven 		    spi->controller->set_cs)
7998caab75fSGeert Uytterhoeven 			spi->controller->set_cs(spi, !enable);
8008caab75fSGeert Uytterhoeven 	} else if (spi->controller->set_cs) {
8018caab75fSGeert Uytterhoeven 		spi->controller->set_cs(spi, !enable);
8028eee6b9dSThor Thayer 	}
803b158935fSMark Brown }
804b158935fSMark Brown 
8052de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
80646336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
8076ad45a27SMark Brown 		struct sg_table *sgt, void *buf, size_t len,
8086ad45a27SMark Brown 		enum dma_data_direction dir)
8096ad45a27SMark Brown {
8106ad45a27SMark Brown 	const bool vmalloced_buf = is_vmalloc_addr(buf);
811df88e91bSAndy Shevchenko 	unsigned int max_seg_size = dma_get_max_seg_size(dev);
812b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM
813b1b8153cSVignesh R 	const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
814b1b8153cSVignesh R 				(unsigned long)buf < (PKMAP_BASE +
815b1b8153cSVignesh R 					(LAST_PKMAP * PAGE_SIZE)));
816b1b8153cSVignesh R #else
817b1b8153cSVignesh R 	const bool kmap_buf = false;
818b1b8153cSVignesh R #endif
81965598c13SAndrew Gabbasov 	int desc_len;
82065598c13SAndrew Gabbasov 	int sgs;
8216ad45a27SMark Brown 	struct page *vm_page;
8228dd4a016SJuan Gutierrez 	struct scatterlist *sg;
8236ad45a27SMark Brown 	void *sg_buf;
8246ad45a27SMark Brown 	size_t min;
8256ad45a27SMark Brown 	int i, ret;
8266ad45a27SMark Brown 
827b1b8153cSVignesh R 	if (vmalloced_buf || kmap_buf) {
828df88e91bSAndy Shevchenko 		desc_len = min_t(int, max_seg_size, PAGE_SIZE);
82965598c13SAndrew Gabbasov 		sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
8300569a88fSVignesh R 	} else if (virt_addr_valid(buf)) {
8318caab75fSGeert Uytterhoeven 		desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
83265598c13SAndrew Gabbasov 		sgs = DIV_ROUND_UP(len, desc_len);
8330569a88fSVignesh R 	} else {
8340569a88fSVignesh R 		return -EINVAL;
83565598c13SAndrew Gabbasov 	}
83665598c13SAndrew Gabbasov 
8376ad45a27SMark Brown 	ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
8386ad45a27SMark Brown 	if (ret != 0)
8396ad45a27SMark Brown 		return ret;
8406ad45a27SMark Brown 
8418dd4a016SJuan Gutierrez 	sg = &sgt->sgl[0];
8426ad45a27SMark Brown 	for (i = 0; i < sgs; i++) {
8436ad45a27SMark Brown 
844b1b8153cSVignesh R 		if (vmalloced_buf || kmap_buf) {
845ce99319aSMaxime Chevallier 			/*
846ce99319aSMaxime Chevallier 			 * Next scatterlist entry size is the minimum between
847ce99319aSMaxime Chevallier 			 * the desc_len and the remaining buffer length that
848ce99319aSMaxime Chevallier 			 * fits in a page.
849ce99319aSMaxime Chevallier 			 */
850ce99319aSMaxime Chevallier 			min = min_t(size_t, desc_len,
851ce99319aSMaxime Chevallier 				    min_t(size_t, len,
852ce99319aSMaxime Chevallier 					  PAGE_SIZE - offset_in_page(buf)));
853b1b8153cSVignesh R 			if (vmalloced_buf)
8546ad45a27SMark Brown 				vm_page = vmalloc_to_page(buf);
855b1b8153cSVignesh R 			else
856b1b8153cSVignesh R 				vm_page = kmap_to_page(buf);
8576ad45a27SMark Brown 			if (!vm_page) {
8586ad45a27SMark Brown 				sg_free_table(sgt);
8596ad45a27SMark Brown 				return -ENOMEM;
8606ad45a27SMark Brown 			}
8618dd4a016SJuan Gutierrez 			sg_set_page(sg, vm_page,
862c1aefbddSCharles Keepax 				    min, offset_in_page(buf));
8636ad45a27SMark Brown 		} else {
86465598c13SAndrew Gabbasov 			min = min_t(size_t, len, desc_len);
8656ad45a27SMark Brown 			sg_buf = buf;
8668dd4a016SJuan Gutierrez 			sg_set_buf(sg, sg_buf, min);
8676ad45a27SMark Brown 		}
8686ad45a27SMark Brown 
8696ad45a27SMark Brown 		buf += min;
8706ad45a27SMark Brown 		len -= min;
8718dd4a016SJuan Gutierrez 		sg = sg_next(sg);
8726ad45a27SMark Brown 	}
8736ad45a27SMark Brown 
8746ad45a27SMark Brown 	ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
87589e4b66aSGeert Uytterhoeven 	if (!ret)
87689e4b66aSGeert Uytterhoeven 		ret = -ENOMEM;
8776ad45a27SMark Brown 	if (ret < 0) {
8786ad45a27SMark Brown 		sg_free_table(sgt);
8796ad45a27SMark Brown 		return ret;
8806ad45a27SMark Brown 	}
8816ad45a27SMark Brown 
8826ad45a27SMark Brown 	sgt->nents = ret;
8836ad45a27SMark Brown 
8846ad45a27SMark Brown 	return 0;
8856ad45a27SMark Brown }
8866ad45a27SMark Brown 
88746336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
8886ad45a27SMark Brown 		   struct sg_table *sgt, enum dma_data_direction dir)
8896ad45a27SMark Brown {
8906ad45a27SMark Brown 	if (sgt->orig_nents) {
8916ad45a27SMark Brown 		dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
8926ad45a27SMark Brown 		sg_free_table(sgt);
8936ad45a27SMark Brown 	}
8946ad45a27SMark Brown }
8956ad45a27SMark Brown 
8968caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
89799adef31SMark Brown {
89899adef31SMark Brown 	struct device *tx_dev, *rx_dev;
89999adef31SMark Brown 	struct spi_transfer *xfer;
9006ad45a27SMark Brown 	int ret;
9013a2eba9bSMark Brown 
9028caab75fSGeert Uytterhoeven 	if (!ctlr->can_dma)
90399adef31SMark Brown 		return 0;
90499adef31SMark Brown 
9058caab75fSGeert Uytterhoeven 	if (ctlr->dma_tx)
9068caab75fSGeert Uytterhoeven 		tx_dev = ctlr->dma_tx->device->dev;
907c37f45b5SLeilk Liu 	else
9088caab75fSGeert Uytterhoeven 		tx_dev = ctlr->dev.parent;
909c37f45b5SLeilk Liu 
9108caab75fSGeert Uytterhoeven 	if (ctlr->dma_rx)
9118caab75fSGeert Uytterhoeven 		rx_dev = ctlr->dma_rx->device->dev;
912c37f45b5SLeilk Liu 	else
9138caab75fSGeert Uytterhoeven 		rx_dev = ctlr->dev.parent;
91499adef31SMark Brown 
91599adef31SMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
9168caab75fSGeert Uytterhoeven 		if (!ctlr->can_dma(ctlr, msg->spi, xfer))
91799adef31SMark Brown 			continue;
91899adef31SMark Brown 
91999adef31SMark Brown 		if (xfer->tx_buf != NULL) {
9208caab75fSGeert Uytterhoeven 			ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
9216ad45a27SMark Brown 					  (void *)xfer->tx_buf, xfer->len,
92299adef31SMark Brown 					  DMA_TO_DEVICE);
9236ad45a27SMark Brown 			if (ret != 0)
9246ad45a27SMark Brown 				return ret;
92599adef31SMark Brown 		}
92699adef31SMark Brown 
92799adef31SMark Brown 		if (xfer->rx_buf != NULL) {
9288caab75fSGeert Uytterhoeven 			ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
92999adef31SMark Brown 					  xfer->rx_buf, xfer->len,
93099adef31SMark Brown 					  DMA_FROM_DEVICE);
9316ad45a27SMark Brown 			if (ret != 0) {
9328caab75fSGeert Uytterhoeven 				spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
9336ad45a27SMark Brown 					      DMA_TO_DEVICE);
9346ad45a27SMark Brown 				return ret;
93599adef31SMark Brown 			}
93699adef31SMark Brown 		}
93799adef31SMark Brown 	}
93899adef31SMark Brown 
9398caab75fSGeert Uytterhoeven 	ctlr->cur_msg_mapped = true;
94099adef31SMark Brown 
94199adef31SMark Brown 	return 0;
94299adef31SMark Brown }
94399adef31SMark Brown 
9448caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
94599adef31SMark Brown {
94699adef31SMark Brown 	struct spi_transfer *xfer;
94799adef31SMark Brown 	struct device *tx_dev, *rx_dev;
94899adef31SMark Brown 
9498caab75fSGeert Uytterhoeven 	if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
95099adef31SMark Brown 		return 0;
95199adef31SMark Brown 
9528caab75fSGeert Uytterhoeven 	if (ctlr->dma_tx)
9538caab75fSGeert Uytterhoeven 		tx_dev = ctlr->dma_tx->device->dev;
954c37f45b5SLeilk Liu 	else
9558caab75fSGeert Uytterhoeven 		tx_dev = ctlr->dev.parent;
956c37f45b5SLeilk Liu 
9578caab75fSGeert Uytterhoeven 	if (ctlr->dma_rx)
9588caab75fSGeert Uytterhoeven 		rx_dev = ctlr->dma_rx->device->dev;
959c37f45b5SLeilk Liu 	else
9608caab75fSGeert Uytterhoeven 		rx_dev = ctlr->dev.parent;
96199adef31SMark Brown 
96299adef31SMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
9638caab75fSGeert Uytterhoeven 		if (!ctlr->can_dma(ctlr, msg->spi, xfer))
96499adef31SMark Brown 			continue;
96599adef31SMark Brown 
9668caab75fSGeert Uytterhoeven 		spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
9678caab75fSGeert Uytterhoeven 		spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
96899adef31SMark Brown 	}
96999adef31SMark Brown 
97099adef31SMark Brown 	return 0;
97199adef31SMark Brown }
9722de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
9738caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr,
9742de440f5SGeert Uytterhoeven 				struct spi_message *msg)
9752de440f5SGeert Uytterhoeven {
9762de440f5SGeert Uytterhoeven 	return 0;
9772de440f5SGeert Uytterhoeven }
9782de440f5SGeert Uytterhoeven 
9798caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr,
9802de440f5SGeert Uytterhoeven 				  struct spi_message *msg)
9812de440f5SGeert Uytterhoeven {
9822de440f5SGeert Uytterhoeven 	return 0;
9832de440f5SGeert Uytterhoeven }
9842de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
9852de440f5SGeert Uytterhoeven 
9868caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr,
9874b786458SMartin Sperl 				struct spi_message *msg)
9884b786458SMartin Sperl {
9894b786458SMartin Sperl 	struct spi_transfer *xfer;
9904b786458SMartin Sperl 
9914b786458SMartin Sperl 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
9924b786458SMartin Sperl 		/*
9934b786458SMartin Sperl 		 * Restore the original value of tx_buf or rx_buf if they are
9944b786458SMartin Sperl 		 * NULL.
9954b786458SMartin Sperl 		 */
9968caab75fSGeert Uytterhoeven 		if (xfer->tx_buf == ctlr->dummy_tx)
9974b786458SMartin Sperl 			xfer->tx_buf = NULL;
9988caab75fSGeert Uytterhoeven 		if (xfer->rx_buf == ctlr->dummy_rx)
9994b786458SMartin Sperl 			xfer->rx_buf = NULL;
10004b786458SMartin Sperl 	}
10014b786458SMartin Sperl 
10028caab75fSGeert Uytterhoeven 	return __spi_unmap_msg(ctlr, msg);
10034b786458SMartin Sperl }
10044b786458SMartin Sperl 
10058caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
10062de440f5SGeert Uytterhoeven {
10072de440f5SGeert Uytterhoeven 	struct spi_transfer *xfer;
10082de440f5SGeert Uytterhoeven 	void *tmp;
10092de440f5SGeert Uytterhoeven 	unsigned int max_tx, max_rx;
10102de440f5SGeert Uytterhoeven 
10118caab75fSGeert Uytterhoeven 	if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
10122de440f5SGeert Uytterhoeven 		max_tx = 0;
10132de440f5SGeert Uytterhoeven 		max_rx = 0;
10142de440f5SGeert Uytterhoeven 
10152de440f5SGeert Uytterhoeven 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
10168caab75fSGeert Uytterhoeven 			if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
10172de440f5SGeert Uytterhoeven 			    !xfer->tx_buf)
10182de440f5SGeert Uytterhoeven 				max_tx = max(xfer->len, max_tx);
10198caab75fSGeert Uytterhoeven 			if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
10202de440f5SGeert Uytterhoeven 			    !xfer->rx_buf)
10212de440f5SGeert Uytterhoeven 				max_rx = max(xfer->len, max_rx);
10222de440f5SGeert Uytterhoeven 		}
10232de440f5SGeert Uytterhoeven 
10242de440f5SGeert Uytterhoeven 		if (max_tx) {
10258caab75fSGeert Uytterhoeven 			tmp = krealloc(ctlr->dummy_tx, max_tx,
10262de440f5SGeert Uytterhoeven 				       GFP_KERNEL | GFP_DMA);
10272de440f5SGeert Uytterhoeven 			if (!tmp)
10282de440f5SGeert Uytterhoeven 				return -ENOMEM;
10298caab75fSGeert Uytterhoeven 			ctlr->dummy_tx = tmp;
10302de440f5SGeert Uytterhoeven 			memset(tmp, 0, max_tx);
10312de440f5SGeert Uytterhoeven 		}
10322de440f5SGeert Uytterhoeven 
10332de440f5SGeert Uytterhoeven 		if (max_rx) {
10348caab75fSGeert Uytterhoeven 			tmp = krealloc(ctlr->dummy_rx, max_rx,
10352de440f5SGeert Uytterhoeven 				       GFP_KERNEL | GFP_DMA);
10362de440f5SGeert Uytterhoeven 			if (!tmp)
10372de440f5SGeert Uytterhoeven 				return -ENOMEM;
10388caab75fSGeert Uytterhoeven 			ctlr->dummy_rx = tmp;
10392de440f5SGeert Uytterhoeven 		}
10402de440f5SGeert Uytterhoeven 
10412de440f5SGeert Uytterhoeven 		if (max_tx || max_rx) {
10422de440f5SGeert Uytterhoeven 			list_for_each_entry(xfer, &msg->transfers,
10432de440f5SGeert Uytterhoeven 					    transfer_list) {
10445442dcaaSChris Lesiak 				if (!xfer->len)
10455442dcaaSChris Lesiak 					continue;
10462de440f5SGeert Uytterhoeven 				if (!xfer->tx_buf)
10478caab75fSGeert Uytterhoeven 					xfer->tx_buf = ctlr->dummy_tx;
10482de440f5SGeert Uytterhoeven 				if (!xfer->rx_buf)
10498caab75fSGeert Uytterhoeven 					xfer->rx_buf = ctlr->dummy_rx;
10502de440f5SGeert Uytterhoeven 			}
10512de440f5SGeert Uytterhoeven 		}
10522de440f5SGeert Uytterhoeven 	}
10532de440f5SGeert Uytterhoeven 
10548caab75fSGeert Uytterhoeven 	return __spi_map_msg(ctlr, msg);
10552de440f5SGeert Uytterhoeven }
105699adef31SMark Brown 
1057810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr,
1058810923f3SLubomir Rintel 			     struct spi_message *msg,
1059810923f3SLubomir Rintel 			     struct spi_transfer *xfer)
1060810923f3SLubomir Rintel {
1061810923f3SLubomir Rintel 	struct spi_statistics *statm = &ctlr->statistics;
1062810923f3SLubomir Rintel 	struct spi_statistics *stats = &msg->spi->statistics;
1063810923f3SLubomir Rintel 	unsigned long long ms = 1;
1064810923f3SLubomir Rintel 
1065810923f3SLubomir Rintel 	if (spi_controller_is_slave(ctlr)) {
1066810923f3SLubomir Rintel 		if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1067810923f3SLubomir Rintel 			dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1068810923f3SLubomir Rintel 			return -EINTR;
1069810923f3SLubomir Rintel 		}
1070810923f3SLubomir Rintel 	} else {
1071810923f3SLubomir Rintel 		ms = 8LL * 1000LL * xfer->len;
1072810923f3SLubomir Rintel 		do_div(ms, xfer->speed_hz);
1073810923f3SLubomir Rintel 		ms += ms + 200; /* some tolerance */
1074810923f3SLubomir Rintel 
1075810923f3SLubomir Rintel 		if (ms > UINT_MAX)
1076810923f3SLubomir Rintel 			ms = UINT_MAX;
1077810923f3SLubomir Rintel 
1078810923f3SLubomir Rintel 		ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1079810923f3SLubomir Rintel 						 msecs_to_jiffies(ms));
1080810923f3SLubomir Rintel 
1081810923f3SLubomir Rintel 		if (ms == 0) {
1082810923f3SLubomir Rintel 			SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1083810923f3SLubomir Rintel 			SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1084810923f3SLubomir Rintel 			dev_err(&msg->spi->dev,
1085810923f3SLubomir Rintel 				"SPI transfer timed out\n");
1086810923f3SLubomir Rintel 			return -ETIMEDOUT;
1087810923f3SLubomir Rintel 		}
1088810923f3SLubomir Rintel 	}
1089810923f3SLubomir Rintel 
1090810923f3SLubomir Rintel 	return 0;
1091810923f3SLubomir Rintel }
1092810923f3SLubomir Rintel 
10930ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns)
10940ff2de8bSMartin Sperl {
10950ff2de8bSMartin Sperl 	if (!ns)
10960ff2de8bSMartin Sperl 		return;
10970ff2de8bSMartin Sperl 	if (ns <= 1000) {
10980ff2de8bSMartin Sperl 		ndelay(ns);
10990ff2de8bSMartin Sperl 	} else {
11000ff2de8bSMartin Sperl 		u32 us = DIV_ROUND_UP(ns, 1000);
11010ff2de8bSMartin Sperl 
11020ff2de8bSMartin Sperl 		if (us <= 10)
11030ff2de8bSMartin Sperl 			udelay(us);
11040ff2de8bSMartin Sperl 		else
11050ff2de8bSMartin Sperl 			usleep_range(us, us + DIV_ROUND_UP(us, 10));
11060ff2de8bSMartin Sperl 	}
11070ff2de8bSMartin Sperl }
11080ff2de8bSMartin Sperl 
1109b2c98153SAlexandru Ardelean static int _spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
1110b2c98153SAlexandru Ardelean {
1111b2c98153SAlexandru Ardelean 	u32 delay = _delay->value;
1112b2c98153SAlexandru Ardelean 	u32 unit = _delay->unit;
1113b2c98153SAlexandru Ardelean 	u32 hz;
1114b2c98153SAlexandru Ardelean 
1115b2c98153SAlexandru Ardelean 	if (!delay)
1116b2c98153SAlexandru Ardelean 		return 0;
1117b2c98153SAlexandru Ardelean 
1118b2c98153SAlexandru Ardelean 	switch (unit) {
1119b2c98153SAlexandru Ardelean 	case SPI_DELAY_UNIT_USECS:
1120b2c98153SAlexandru Ardelean 		delay *= 1000;
1121b2c98153SAlexandru Ardelean 		break;
1122b2c98153SAlexandru Ardelean 	case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
1123b2c98153SAlexandru Ardelean 		break;
1124b2c98153SAlexandru Ardelean 	case SPI_DELAY_UNIT_SCK:
1125b2c98153SAlexandru Ardelean 		/* clock cycles need to be obtained from spi_transfer */
1126b2c98153SAlexandru Ardelean 		if (!xfer)
1127b2c98153SAlexandru Ardelean 			return -EINVAL;
1128b2c98153SAlexandru Ardelean 		/* if there is no effective speed know, then approximate
1129b2c98153SAlexandru Ardelean 		 * by underestimating with half the requested hz
1130b2c98153SAlexandru Ardelean 		 */
1131b2c98153SAlexandru Ardelean 		hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1132b2c98153SAlexandru Ardelean 		if (!hz)
1133b2c98153SAlexandru Ardelean 			return -EINVAL;
1134b2c98153SAlexandru Ardelean 		delay *= DIV_ROUND_UP(1000000000, hz);
1135b2c98153SAlexandru Ardelean 		break;
1136b2c98153SAlexandru Ardelean 	default:
1137b2c98153SAlexandru Ardelean 		return -EINVAL;
1138b2c98153SAlexandru Ardelean 	}
1139b2c98153SAlexandru Ardelean 
1140b2c98153SAlexandru Ardelean 	return delay;
1141b2c98153SAlexandru Ardelean }
1142b2c98153SAlexandru Ardelean 
1143b2c98153SAlexandru Ardelean int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1144b2c98153SAlexandru Ardelean {
1145b2c98153SAlexandru Ardelean 	int delay;
1146b2c98153SAlexandru Ardelean 
1147b2c98153SAlexandru Ardelean 	if (!_delay)
1148b2c98153SAlexandru Ardelean 		return -EINVAL;
1149b2c98153SAlexandru Ardelean 
1150b2c98153SAlexandru Ardelean 	delay = _spi_delay_to_ns(_delay, xfer);
1151b2c98153SAlexandru Ardelean 	if (delay < 0)
1152b2c98153SAlexandru Ardelean 		return delay;
1153b2c98153SAlexandru Ardelean 
1154b2c98153SAlexandru Ardelean 	_spi_transfer_delay_ns(delay);
1155b2c98153SAlexandru Ardelean 
1156b2c98153SAlexandru Ardelean 	return 0;
1157b2c98153SAlexandru Ardelean }
1158b2c98153SAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_exec);
1159b2c98153SAlexandru Ardelean 
11600ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg,
11610ff2de8bSMartin Sperl 					  struct spi_transfer *xfer)
11620ff2de8bSMartin Sperl {
1163329f0dacSAlexandru Ardelean 	u32 delay = xfer->cs_change_delay.value;
1164329f0dacSAlexandru Ardelean 	u32 unit = xfer->cs_change_delay.unit;
1165329f0dacSAlexandru Ardelean 	int ret;
11660ff2de8bSMartin Sperl 
11670ff2de8bSMartin Sperl 	/* return early on "fast" mode - for everything but USECS */
11686b3f236aSAlexandru Ardelean 	if (!delay) {
11696b3f236aSAlexandru Ardelean 		if (unit == SPI_DELAY_UNIT_USECS)
11706b3f236aSAlexandru Ardelean 			_spi_transfer_delay_ns(10000);
11710ff2de8bSMartin Sperl 		return;
11726b3f236aSAlexandru Ardelean 	}
11730ff2de8bSMartin Sperl 
1174329f0dacSAlexandru Ardelean 	ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1175329f0dacSAlexandru Ardelean 	if (ret) {
11760ff2de8bSMartin Sperl 		dev_err_once(&msg->spi->dev,
11770ff2de8bSMartin Sperl 			     "Use of unsupported delay unit %i, using default of 10us\n",
1178329f0dacSAlexandru Ardelean 			     unit);
1179329f0dacSAlexandru Ardelean 		_spi_transfer_delay_ns(10000);
11800ff2de8bSMartin Sperl 	}
11810ff2de8bSMartin Sperl }
11820ff2de8bSMartin Sperl 
1183b158935fSMark Brown /*
1184b158935fSMark Brown  * spi_transfer_one_message - Default implementation of transfer_one_message()
1185b158935fSMark Brown  *
1186b158935fSMark Brown  * This is a standard implementation of transfer_one_message() for
11878ba811a7SMoritz Fischer  * drivers which implement a transfer_one() operation.  It provides
1188b158935fSMark Brown  * standard handling of delays and chip select management.
1189b158935fSMark Brown  */
11908caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr,
1191b158935fSMark Brown 				    struct spi_message *msg)
1192b158935fSMark Brown {
1193b158935fSMark Brown 	struct spi_transfer *xfer;
1194b158935fSMark Brown 	bool keep_cs = false;
1195b158935fSMark Brown 	int ret = 0;
11968caab75fSGeert Uytterhoeven 	struct spi_statistics *statm = &ctlr->statistics;
1197eca2ebc7SMartin Sperl 	struct spi_statistics *stats = &msg->spi->statistics;
1198b158935fSMark Brown 
1199b158935fSMark Brown 	spi_set_cs(msg->spi, true);
1200b158935fSMark Brown 
1201eca2ebc7SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1202eca2ebc7SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1203eca2ebc7SMartin Sperl 
1204b158935fSMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1205b158935fSMark Brown 		trace_spi_transfer_start(msg, xfer);
1206b158935fSMark Brown 
12078caab75fSGeert Uytterhoeven 		spi_statistics_add_transfer_stats(statm, xfer, ctlr);
12088caab75fSGeert Uytterhoeven 		spi_statistics_add_transfer_stats(stats, xfer, ctlr);
1209eca2ebc7SMartin Sperl 
1210b42faeeeSVladimir Oltean 		if (!ctlr->ptp_sts_supported) {
1211b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_pre = 0;
1212b42faeeeSVladimir Oltean 			ptp_read_system_prets(xfer->ptp_sts);
1213b42faeeeSVladimir Oltean 		}
1214b42faeeeSVladimir Oltean 
121538ec10f6SMark Brown 		if (xfer->tx_buf || xfer->rx_buf) {
12168caab75fSGeert Uytterhoeven 			reinit_completion(&ctlr->xfer_completion);
1217b158935fSMark Brown 
12188caab75fSGeert Uytterhoeven 			ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1219b158935fSMark Brown 			if (ret < 0) {
1220eca2ebc7SMartin Sperl 				SPI_STATISTICS_INCREMENT_FIELD(statm,
1221eca2ebc7SMartin Sperl 							       errors);
1222eca2ebc7SMartin Sperl 				SPI_STATISTICS_INCREMENT_FIELD(stats,
1223eca2ebc7SMartin Sperl 							       errors);
1224b158935fSMark Brown 				dev_err(&msg->spi->dev,
1225b158935fSMark Brown 					"SPI transfer failed: %d\n", ret);
1226b158935fSMark Brown 				goto out;
1227b158935fSMark Brown 			}
1228b158935fSMark Brown 
1229d57e7960SMark Brown 			if (ret > 0) {
1230810923f3SLubomir Rintel 				ret = spi_transfer_wait(ctlr, msg, xfer);
1231810923f3SLubomir Rintel 				if (ret < 0)
1232810923f3SLubomir Rintel 					msg->status = ret;
1233d57e7960SMark Brown 			}
123438ec10f6SMark Brown 		} else {
123538ec10f6SMark Brown 			if (xfer->len)
123638ec10f6SMark Brown 				dev_err(&msg->spi->dev,
123738ec10f6SMark Brown 					"Bufferless transfer has length %u\n",
123838ec10f6SMark Brown 					xfer->len);
123938ec10f6SMark Brown 		}
1240b158935fSMark Brown 
1241b42faeeeSVladimir Oltean 		if (!ctlr->ptp_sts_supported) {
1242b42faeeeSVladimir Oltean 			ptp_read_system_postts(xfer->ptp_sts);
1243b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_post = xfer->len;
1244b42faeeeSVladimir Oltean 		}
1245b42faeeeSVladimir Oltean 
1246b158935fSMark Brown 		trace_spi_transfer_stop(msg, xfer);
1247b158935fSMark Brown 
1248b158935fSMark Brown 		if (msg->status != -EINPROGRESS)
1249b158935fSMark Brown 			goto out;
1250b158935fSMark Brown 
1251bebcfd27SAlexandru Ardelean 		spi_transfer_delay_exec(xfer);
1252b158935fSMark Brown 
1253b158935fSMark Brown 		if (xfer->cs_change) {
1254b158935fSMark Brown 			if (list_is_last(&xfer->transfer_list,
1255b158935fSMark Brown 					 &msg->transfers)) {
1256b158935fSMark Brown 				keep_cs = true;
1257b158935fSMark Brown 			} else {
12580b73aa63SMark Brown 				spi_set_cs(msg->spi, false);
12590ff2de8bSMartin Sperl 				_spi_transfer_cs_change_delay(msg, xfer);
12600b73aa63SMark Brown 				spi_set_cs(msg->spi, true);
1261b158935fSMark Brown 			}
1262b158935fSMark Brown 		}
1263b158935fSMark Brown 
1264b158935fSMark Brown 		msg->actual_length += xfer->len;
1265b158935fSMark Brown 	}
1266b158935fSMark Brown 
1267b158935fSMark Brown out:
1268b158935fSMark Brown 	if (ret != 0 || !keep_cs)
1269b158935fSMark Brown 		spi_set_cs(msg->spi, false);
1270b158935fSMark Brown 
1271b158935fSMark Brown 	if (msg->status == -EINPROGRESS)
1272b158935fSMark Brown 		msg->status = ret;
1273b158935fSMark Brown 
12748caab75fSGeert Uytterhoeven 	if (msg->status && ctlr->handle_err)
12758caab75fSGeert Uytterhoeven 		ctlr->handle_err(ctlr, msg);
1276b716c4ffSAndy Shevchenko 
1277c9ba7a16SNoralf Trønnes 	spi_res_release(ctlr, msg);
1278c9ba7a16SNoralf Trønnes 
12790ed56252SMark Brown 	spi_finalize_current_message(ctlr);
12800ed56252SMark Brown 
1281b158935fSMark Brown 	return ret;
1282b158935fSMark Brown }
1283b158935fSMark Brown 
1284b158935fSMark Brown /**
1285b158935fSMark Brown  * spi_finalize_current_transfer - report completion of a transfer
12868caab75fSGeert Uytterhoeven  * @ctlr: the controller reporting completion
1287b158935fSMark Brown  *
1288b158935fSMark Brown  * Called by SPI drivers using the core transfer_one_message()
1289b158935fSMark Brown  * implementation to notify it that the current interrupt driven
12909e8f4882SGeert Uytterhoeven  * transfer has finished and the next one may be scheduled.
1291b158935fSMark Brown  */
12928caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr)
1293b158935fSMark Brown {
12948caab75fSGeert Uytterhoeven 	complete(&ctlr->xfer_completion);
1295b158935fSMark Brown }
1296b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1297b158935fSMark Brown 
1298ffbbdd21SLinus Walleij /**
1299fc9e0f71SMark Brown  * __spi_pump_messages - function which processes spi message queue
13008caab75fSGeert Uytterhoeven  * @ctlr: controller to process queue for
1301fc9e0f71SMark Brown  * @in_kthread: true if we are in the context of the message pump thread
1302ffbbdd21SLinus Walleij  *
1303ffbbdd21SLinus Walleij  * This function checks if there is any spi message in the queue that
1304ffbbdd21SLinus Walleij  * needs processing and if so call out to the driver to initialize hardware
1305ffbbdd21SLinus Walleij  * and transfer each message.
1306ffbbdd21SLinus Walleij  *
13070461a414SMark Brown  * Note that it is called both from the kthread itself and also from
13080461a414SMark Brown  * inside spi_sync(); the queue extraction handling at the top of the
13090461a414SMark Brown  * function should deal with this safely.
1310ffbbdd21SLinus Walleij  */
13118caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
1312ffbbdd21SLinus Walleij {
1313b42faeeeSVladimir Oltean 	struct spi_transfer *xfer;
1314d1c44c93SVladimir Oltean 	struct spi_message *msg;
1315ffbbdd21SLinus Walleij 	bool was_busy = false;
1316d1c44c93SVladimir Oltean 	unsigned long flags;
1317ffbbdd21SLinus Walleij 	int ret;
1318ffbbdd21SLinus Walleij 
1319983aee5dSMark Brown 	/* Lock queue */
13208caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
1321983aee5dSMark Brown 
1322983aee5dSMark Brown 	/* Make sure we are not already running a message */
13238caab75fSGeert Uytterhoeven 	if (ctlr->cur_msg) {
13248caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1325983aee5dSMark Brown 		return;
1326983aee5dSMark Brown 	}
1327983aee5dSMark Brown 
1328f0125f1aSMark Brown 	/* If another context is idling the device then defer */
13298caab75fSGeert Uytterhoeven 	if (ctlr->idling) {
13308caab75fSGeert Uytterhoeven 		kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
13318caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
13320461a414SMark Brown 		return;
13330461a414SMark Brown 	}
13340461a414SMark Brown 
1335983aee5dSMark Brown 	/* Check if the queue is idle */
13368caab75fSGeert Uytterhoeven 	if (list_empty(&ctlr->queue) || !ctlr->running) {
13378caab75fSGeert Uytterhoeven 		if (!ctlr->busy) {
13388caab75fSGeert Uytterhoeven 			spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1339ffbbdd21SLinus Walleij 			return;
1340ffbbdd21SLinus Walleij 		}
1341fc9e0f71SMark Brown 
1342f0125f1aSMark Brown 		/* Only do teardown in the thread */
1343f0125f1aSMark Brown 		if (!in_kthread) {
1344f0125f1aSMark Brown 			kthread_queue_work(&ctlr->kworker,
1345f0125f1aSMark Brown 					   &ctlr->pump_messages);
1346f0125f1aSMark Brown 			spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1347f0125f1aSMark Brown 			return;
1348f0125f1aSMark Brown 		}
1349f0125f1aSMark Brown 
1350f0125f1aSMark Brown 		ctlr->busy = false;
1351f0125f1aSMark Brown 		ctlr->idling = true;
1352f0125f1aSMark Brown 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1353f0125f1aSMark Brown 
1354f0125f1aSMark Brown 		kfree(ctlr->dummy_rx);
1355f0125f1aSMark Brown 		ctlr->dummy_rx = NULL;
1356f0125f1aSMark Brown 		kfree(ctlr->dummy_tx);
1357f0125f1aSMark Brown 		ctlr->dummy_tx = NULL;
1358f0125f1aSMark Brown 		if (ctlr->unprepare_transfer_hardware &&
1359f0125f1aSMark Brown 		    ctlr->unprepare_transfer_hardware(ctlr))
1360f0125f1aSMark Brown 			dev_err(&ctlr->dev,
1361f0125f1aSMark Brown 				"failed to unprepare transfer hardware\n");
1362f0125f1aSMark Brown 		if (ctlr->auto_runtime_pm) {
1363f0125f1aSMark Brown 			pm_runtime_mark_last_busy(ctlr->dev.parent);
1364f0125f1aSMark Brown 			pm_runtime_put_autosuspend(ctlr->dev.parent);
1365f0125f1aSMark Brown 		}
1366f0125f1aSMark Brown 		trace_spi_controller_idle(ctlr);
1367f0125f1aSMark Brown 
1368f0125f1aSMark Brown 		spin_lock_irqsave(&ctlr->queue_lock, flags);
1369f0125f1aSMark Brown 		ctlr->idling = false;
13708caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1371ffbbdd21SLinus Walleij 		return;
1372ffbbdd21SLinus Walleij 	}
1373ffbbdd21SLinus Walleij 
1374ffbbdd21SLinus Walleij 	/* Extract head of queue */
1375d1c44c93SVladimir Oltean 	msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1376d1c44c93SVladimir Oltean 	ctlr->cur_msg = msg;
1377ffbbdd21SLinus Walleij 
1378d1c44c93SVladimir Oltean 	list_del_init(&msg->queue);
13798caab75fSGeert Uytterhoeven 	if (ctlr->busy)
1380ffbbdd21SLinus Walleij 		was_busy = true;
1381ffbbdd21SLinus Walleij 	else
13828caab75fSGeert Uytterhoeven 		ctlr->busy = true;
13838caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1384ffbbdd21SLinus Walleij 
13858caab75fSGeert Uytterhoeven 	mutex_lock(&ctlr->io_mutex);
1386ef4d96ecSMark Brown 
13878caab75fSGeert Uytterhoeven 	if (!was_busy && ctlr->auto_runtime_pm) {
13888caab75fSGeert Uytterhoeven 		ret = pm_runtime_get_sync(ctlr->dev.parent);
138949834de2SMark Brown 		if (ret < 0) {
13907e48e23aSTony Lindgren 			pm_runtime_put_noidle(ctlr->dev.parent);
13918caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "Failed to power device: %d\n",
139249834de2SMark Brown 				ret);
13938caab75fSGeert Uytterhoeven 			mutex_unlock(&ctlr->io_mutex);
139449834de2SMark Brown 			return;
139549834de2SMark Brown 		}
139649834de2SMark Brown 	}
139749834de2SMark Brown 
139856ec1978SMark Brown 	if (!was_busy)
13998caab75fSGeert Uytterhoeven 		trace_spi_controller_busy(ctlr);
140056ec1978SMark Brown 
14018caab75fSGeert Uytterhoeven 	if (!was_busy && ctlr->prepare_transfer_hardware) {
14028caab75fSGeert Uytterhoeven 		ret = ctlr->prepare_transfer_hardware(ctlr);
1403ffbbdd21SLinus Walleij 		if (ret) {
14048caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev,
1405f3440d9aSSuper Liu 				"failed to prepare transfer hardware: %d\n",
1406f3440d9aSSuper Liu 				ret);
140749834de2SMark Brown 
14088caab75fSGeert Uytterhoeven 			if (ctlr->auto_runtime_pm)
14098caab75fSGeert Uytterhoeven 				pm_runtime_put(ctlr->dev.parent);
1410f3440d9aSSuper Liu 
1411d1c44c93SVladimir Oltean 			msg->status = ret;
1412f3440d9aSSuper Liu 			spi_finalize_current_message(ctlr);
1413f3440d9aSSuper Liu 
14148caab75fSGeert Uytterhoeven 			mutex_unlock(&ctlr->io_mutex);
1415ffbbdd21SLinus Walleij 			return;
1416ffbbdd21SLinus Walleij 		}
1417ffbbdd21SLinus Walleij 	}
1418ffbbdd21SLinus Walleij 
1419d1c44c93SVladimir Oltean 	trace_spi_message_start(msg);
142056ec1978SMark Brown 
14218caab75fSGeert Uytterhoeven 	if (ctlr->prepare_message) {
1422d1c44c93SVladimir Oltean 		ret = ctlr->prepare_message(ctlr, msg);
14232841a5fcSMark Brown 		if (ret) {
14248caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "failed to prepare message: %d\n",
14258caab75fSGeert Uytterhoeven 				ret);
1426d1c44c93SVladimir Oltean 			msg->status = ret;
14278caab75fSGeert Uytterhoeven 			spi_finalize_current_message(ctlr);
142849023d2eSJon Hunter 			goto out;
14292841a5fcSMark Brown 		}
14308caab75fSGeert Uytterhoeven 		ctlr->cur_msg_prepared = true;
14312841a5fcSMark Brown 	}
14322841a5fcSMark Brown 
1433d1c44c93SVladimir Oltean 	ret = spi_map_msg(ctlr, msg);
143499adef31SMark Brown 	if (ret) {
1435d1c44c93SVladimir Oltean 		msg->status = ret;
14368caab75fSGeert Uytterhoeven 		spi_finalize_current_message(ctlr);
143749023d2eSJon Hunter 		goto out;
143899adef31SMark Brown 	}
143999adef31SMark Brown 
1440b42faeeeSVladimir Oltean 	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1441b42faeeeSVladimir Oltean 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1442b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_pre = 0;
1443b42faeeeSVladimir Oltean 			ptp_read_system_prets(xfer->ptp_sts);
1444b42faeeeSVladimir Oltean 		}
1445b42faeeeSVladimir Oltean 	}
1446b42faeeeSVladimir Oltean 
1447d1c44c93SVladimir Oltean 	ret = ctlr->transfer_one_message(ctlr, msg);
1448ffbbdd21SLinus Walleij 	if (ret) {
14498caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev,
14501f802f82SGeert Uytterhoeven 			"failed to transfer one message from queue\n");
145149023d2eSJon Hunter 		goto out;
1452ffbbdd21SLinus Walleij 	}
145349023d2eSJon Hunter 
145449023d2eSJon Hunter out:
14558caab75fSGeert Uytterhoeven 	mutex_unlock(&ctlr->io_mutex);
145662826970SMark Brown 
145762826970SMark Brown 	/* Prod the scheduler in case transfer_one() was busy waiting */
145849023d2eSJon Hunter 	if (!ret)
145962826970SMark Brown 		cond_resched();
1460ffbbdd21SLinus Walleij }
1461ffbbdd21SLinus Walleij 
1462fc9e0f71SMark Brown /**
1463fc9e0f71SMark Brown  * spi_pump_messages - kthread work function which processes spi message queue
14648caab75fSGeert Uytterhoeven  * @work: pointer to kthread work struct contained in the controller struct
1465fc9e0f71SMark Brown  */
1466fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work)
1467fc9e0f71SMark Brown {
14688caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr =
14698caab75fSGeert Uytterhoeven 		container_of(work, struct spi_controller, pump_messages);
1470fc9e0f71SMark Brown 
14718caab75fSGeert Uytterhoeven 	__spi_pump_messages(ctlr, true);
1472fc9e0f71SMark Brown }
1473fc9e0f71SMark Brown 
1474924b5867SDouglas Anderson /**
1475b42faeeeSVladimir Oltean  * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1476b42faeeeSVladimir Oltean  *			    TX timestamp for the requested byte from the SPI
1477b42faeeeSVladimir Oltean  *			    transfer. The frequency with which this function
1478b42faeeeSVladimir Oltean  *			    must be called (once per word, once for the whole
1479b42faeeeSVladimir Oltean  *			    transfer, once per batch of words etc) is arbitrary
1480b42faeeeSVladimir Oltean  *			    as long as the @tx buffer offset is greater than or
1481b42faeeeSVladimir Oltean  *			    equal to the requested byte at the time of the
1482b42faeeeSVladimir Oltean  *			    call. The timestamp is only taken once, at the
1483b42faeeeSVladimir Oltean  *			    first such call. It is assumed that the driver
1484b42faeeeSVladimir Oltean  *			    advances its @tx buffer pointer monotonically.
1485b42faeeeSVladimir Oltean  * @ctlr: Pointer to the spi_controller structure of the driver
1486b42faeeeSVladimir Oltean  * @xfer: Pointer to the transfer being timestamped
1487b42faeeeSVladimir Oltean  * @tx: Pointer to the current word within the xfer->tx_buf that the driver is
1488b42faeeeSVladimir Oltean  *	preparing to transmit right now.
1489b42faeeeSVladimir Oltean  * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1490b42faeeeSVladimir Oltean  *	      transfer, for less jitter in time measurement. Only compatible
1491b42faeeeSVladimir Oltean  *	      with PIO drivers. If true, must follow up with
1492b42faeeeSVladimir Oltean  *	      spi_take_timestamp_post or otherwise system will crash.
1493b42faeeeSVladimir Oltean  *	      WARNING: for fully predictable results, the CPU frequency must
1494b42faeeeSVladimir Oltean  *	      also be under control (governor).
1495b42faeeeSVladimir Oltean  */
1496b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr,
1497b42faeeeSVladimir Oltean 			    struct spi_transfer *xfer,
1498b42faeeeSVladimir Oltean 			    const void *tx, bool irqs_off)
1499b42faeeeSVladimir Oltean {
1500b42faeeeSVladimir Oltean 	u8 bytes_per_word = DIV_ROUND_UP(xfer->bits_per_word, 8);
1501b42faeeeSVladimir Oltean 
1502b42faeeeSVladimir Oltean 	if (!xfer->ptp_sts)
1503b42faeeeSVladimir Oltean 		return;
1504b42faeeeSVladimir Oltean 
1505b42faeeeSVladimir Oltean 	if (xfer->timestamped_pre)
1506b42faeeeSVladimir Oltean 		return;
1507b42faeeeSVladimir Oltean 
1508b42faeeeSVladimir Oltean 	if (tx < (xfer->tx_buf + xfer->ptp_sts_word_pre * bytes_per_word))
1509b42faeeeSVladimir Oltean 		return;
1510b42faeeeSVladimir Oltean 
1511b42faeeeSVladimir Oltean 	/* Capture the resolution of the timestamp */
1512b42faeeeSVladimir Oltean 	xfer->ptp_sts_word_pre = (tx - xfer->tx_buf) / bytes_per_word;
1513b42faeeeSVladimir Oltean 
1514b42faeeeSVladimir Oltean 	xfer->timestamped_pre = true;
1515b42faeeeSVladimir Oltean 
1516b42faeeeSVladimir Oltean 	if (irqs_off) {
1517b42faeeeSVladimir Oltean 		local_irq_save(ctlr->irq_flags);
1518b42faeeeSVladimir Oltean 		preempt_disable();
1519b42faeeeSVladimir Oltean 	}
1520b42faeeeSVladimir Oltean 
1521b42faeeeSVladimir Oltean 	ptp_read_system_prets(xfer->ptp_sts);
1522b42faeeeSVladimir Oltean }
1523b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1524b42faeeeSVladimir Oltean 
1525b42faeeeSVladimir Oltean /**
1526b42faeeeSVladimir Oltean  * spi_take_timestamp_post - helper for drivers to collect the end of the
1527b42faeeeSVladimir Oltean  *			     TX timestamp for the requested byte from the SPI
1528b42faeeeSVladimir Oltean  *			     transfer. Can be called with an arbitrary
1529b42faeeeSVladimir Oltean  *			     frequency: only the first call where @tx exceeds
1530b42faeeeSVladimir Oltean  *			     or is equal to the requested word will be
1531b42faeeeSVladimir Oltean  *			     timestamped.
1532b42faeeeSVladimir Oltean  * @ctlr: Pointer to the spi_controller structure of the driver
1533b42faeeeSVladimir Oltean  * @xfer: Pointer to the transfer being timestamped
1534b42faeeeSVladimir Oltean  * @tx: Pointer to the current word within the xfer->tx_buf that the driver has
1535b42faeeeSVladimir Oltean  *	just transmitted.
1536b42faeeeSVladimir Oltean  * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1537b42faeeeSVladimir Oltean  */
1538b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr,
1539b42faeeeSVladimir Oltean 			     struct spi_transfer *xfer,
1540b42faeeeSVladimir Oltean 			     const void *tx, bool irqs_off)
1541b42faeeeSVladimir Oltean {
1542b42faeeeSVladimir Oltean 	u8 bytes_per_word = DIV_ROUND_UP(xfer->bits_per_word, 8);
1543b42faeeeSVladimir Oltean 
1544b42faeeeSVladimir Oltean 	if (!xfer->ptp_sts)
1545b42faeeeSVladimir Oltean 		return;
1546b42faeeeSVladimir Oltean 
1547b42faeeeSVladimir Oltean 	if (xfer->timestamped_post)
1548b42faeeeSVladimir Oltean 		return;
1549b42faeeeSVladimir Oltean 
1550b42faeeeSVladimir Oltean 	if (tx < (xfer->tx_buf + xfer->ptp_sts_word_post * bytes_per_word))
1551b42faeeeSVladimir Oltean 		return;
1552b42faeeeSVladimir Oltean 
1553b42faeeeSVladimir Oltean 	ptp_read_system_postts(xfer->ptp_sts);
1554b42faeeeSVladimir Oltean 
1555b42faeeeSVladimir Oltean 	if (irqs_off) {
1556b42faeeeSVladimir Oltean 		local_irq_restore(ctlr->irq_flags);
1557b42faeeeSVladimir Oltean 		preempt_enable();
1558b42faeeeSVladimir Oltean 	}
1559b42faeeeSVladimir Oltean 
1560b42faeeeSVladimir Oltean 	/* Capture the resolution of the timestamp */
1561b42faeeeSVladimir Oltean 	xfer->ptp_sts_word_post = (tx - xfer->tx_buf) / bytes_per_word;
1562b42faeeeSVladimir Oltean 
1563b42faeeeSVladimir Oltean 	xfer->timestamped_post = true;
1564b42faeeeSVladimir Oltean }
1565b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
1566b42faeeeSVladimir Oltean 
1567b42faeeeSVladimir Oltean /**
1568924b5867SDouglas Anderson  * spi_set_thread_rt - set the controller to pump at realtime priority
1569924b5867SDouglas Anderson  * @ctlr: controller to boost priority of
1570924b5867SDouglas Anderson  *
1571924b5867SDouglas Anderson  * This can be called because the controller requested realtime priority
1572924b5867SDouglas Anderson  * (by setting the ->rt value before calling spi_register_controller()) or
1573924b5867SDouglas Anderson  * because a device on the bus said that its transfers needed realtime
1574924b5867SDouglas Anderson  * priority.
1575924b5867SDouglas Anderson  *
1576924b5867SDouglas Anderson  * NOTE: at the moment if any device on a bus says it needs realtime then
1577924b5867SDouglas Anderson  * the thread will be at realtime priority for all transfers on that
1578924b5867SDouglas Anderson  * controller.  If this eventually becomes a problem we may see if we can
1579924b5867SDouglas Anderson  * find a way to boost the priority only temporarily during relevant
1580924b5867SDouglas Anderson  * transfers.
1581924b5867SDouglas Anderson  */
1582924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr)
1583ffbbdd21SLinus Walleij {
15844ff13d00SPeter Zijlstra 	struct sched_param param = { .sched_priority = MAX_RT_PRIO / 2 };
1585ffbbdd21SLinus Walleij 
1586924b5867SDouglas Anderson 	dev_info(&ctlr->dev,
1587924b5867SDouglas Anderson 		"will run message pump with realtime priority\n");
1588924b5867SDouglas Anderson 	sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
1589924b5867SDouglas Anderson }
1590924b5867SDouglas Anderson 
1591924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr)
1592924b5867SDouglas Anderson {
15938caab75fSGeert Uytterhoeven 	ctlr->running = false;
15948caab75fSGeert Uytterhoeven 	ctlr->busy = false;
1595ffbbdd21SLinus Walleij 
15968caab75fSGeert Uytterhoeven 	kthread_init_worker(&ctlr->kworker);
15978caab75fSGeert Uytterhoeven 	ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
15988caab75fSGeert Uytterhoeven 					 "%s", dev_name(&ctlr->dev));
15998caab75fSGeert Uytterhoeven 	if (IS_ERR(ctlr->kworker_task)) {
16008caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "failed to create message pump task\n");
16018caab75fSGeert Uytterhoeven 		return PTR_ERR(ctlr->kworker_task);
1602ffbbdd21SLinus Walleij 	}
16038caab75fSGeert Uytterhoeven 	kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
1604f0125f1aSMark Brown 
1605ffbbdd21SLinus Walleij 	/*
16068caab75fSGeert Uytterhoeven 	 * Controller config will indicate if this controller should run the
1607ffbbdd21SLinus Walleij 	 * message pump with high (realtime) priority to reduce the transfer
1608ffbbdd21SLinus Walleij 	 * latency on the bus by minimising the delay between a transfer
1609ffbbdd21SLinus Walleij 	 * request and the scheduling of the message pump thread. Without this
1610ffbbdd21SLinus Walleij 	 * setting the message pump thread will remain at default priority.
1611ffbbdd21SLinus Walleij 	 */
1612924b5867SDouglas Anderson 	if (ctlr->rt)
1613924b5867SDouglas Anderson 		spi_set_thread_rt(ctlr);
1614ffbbdd21SLinus Walleij 
1615ffbbdd21SLinus Walleij 	return 0;
1616ffbbdd21SLinus Walleij }
1617ffbbdd21SLinus Walleij 
1618ffbbdd21SLinus Walleij /**
1619ffbbdd21SLinus Walleij  * spi_get_next_queued_message() - called by driver to check for queued
1620ffbbdd21SLinus Walleij  * messages
16218caab75fSGeert Uytterhoeven  * @ctlr: the controller to check for queued messages
1622ffbbdd21SLinus Walleij  *
1623ffbbdd21SLinus Walleij  * If there are more messages in the queue, the next message is returned from
1624ffbbdd21SLinus Walleij  * this call.
162597d56dc6SJavier Martinez Canillas  *
162697d56dc6SJavier Martinez Canillas  * Return: the next message in the queue, else NULL if the queue is empty.
1627ffbbdd21SLinus Walleij  */
16288caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
1629ffbbdd21SLinus Walleij {
1630ffbbdd21SLinus Walleij 	struct spi_message *next;
1631ffbbdd21SLinus Walleij 	unsigned long flags;
1632ffbbdd21SLinus Walleij 
1633ffbbdd21SLinus Walleij 	/* get a pointer to the next message, if any */
16348caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
16358caab75fSGeert Uytterhoeven 	next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
16361cfd97f9SAxel Lin 					queue);
16378caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1638ffbbdd21SLinus Walleij 
1639ffbbdd21SLinus Walleij 	return next;
1640ffbbdd21SLinus Walleij }
1641ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1642ffbbdd21SLinus Walleij 
1643ffbbdd21SLinus Walleij /**
1644ffbbdd21SLinus Walleij  * spi_finalize_current_message() - the current message is complete
16458caab75fSGeert Uytterhoeven  * @ctlr: the controller to return the message to
1646ffbbdd21SLinus Walleij  *
1647ffbbdd21SLinus Walleij  * Called by the driver to notify the core that the message in the front of the
1648ffbbdd21SLinus Walleij  * queue is complete and can be removed from the queue.
1649ffbbdd21SLinus Walleij  */
16508caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr)
1651ffbbdd21SLinus Walleij {
1652b42faeeeSVladimir Oltean 	struct spi_transfer *xfer;
1653ffbbdd21SLinus Walleij 	struct spi_message *mesg;
1654ffbbdd21SLinus Walleij 	unsigned long flags;
16552841a5fcSMark Brown 	int ret;
1656ffbbdd21SLinus Walleij 
16578caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
16588caab75fSGeert Uytterhoeven 	mesg = ctlr->cur_msg;
16598caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1660ffbbdd21SLinus Walleij 
1661b42faeeeSVladimir Oltean 	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1662b42faeeeSVladimir Oltean 		list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
1663b42faeeeSVladimir Oltean 			ptp_read_system_postts(xfer->ptp_sts);
1664b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_post = xfer->len;
1665b42faeeeSVladimir Oltean 		}
1666b42faeeeSVladimir Oltean 	}
1667b42faeeeSVladimir Oltean 
16688caab75fSGeert Uytterhoeven 	spi_unmap_msg(ctlr, mesg);
166999adef31SMark Brown 
16708caab75fSGeert Uytterhoeven 	if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
16718caab75fSGeert Uytterhoeven 		ret = ctlr->unprepare_message(ctlr, mesg);
16722841a5fcSMark Brown 		if (ret) {
16738caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
16748caab75fSGeert Uytterhoeven 				ret);
16752841a5fcSMark Brown 		}
16762841a5fcSMark Brown 	}
1677391949b6SUwe Kleine-König 
16788caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
16798caab75fSGeert Uytterhoeven 	ctlr->cur_msg = NULL;
16808caab75fSGeert Uytterhoeven 	ctlr->cur_msg_prepared = false;
16818caab75fSGeert Uytterhoeven 	kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
16828caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
16838e76ef88SMartin Sperl 
16848e76ef88SMartin Sperl 	trace_spi_message_done(mesg);
16852841a5fcSMark Brown 
1686ffbbdd21SLinus Walleij 	mesg->state = NULL;
1687ffbbdd21SLinus Walleij 	if (mesg->complete)
1688ffbbdd21SLinus Walleij 		mesg->complete(mesg->context);
1689ffbbdd21SLinus Walleij }
1690ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1691ffbbdd21SLinus Walleij 
16928caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr)
1693ffbbdd21SLinus Walleij {
1694ffbbdd21SLinus Walleij 	unsigned long flags;
1695ffbbdd21SLinus Walleij 
16968caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
1697ffbbdd21SLinus Walleij 
16988caab75fSGeert Uytterhoeven 	if (ctlr->running || ctlr->busy) {
16998caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1700ffbbdd21SLinus Walleij 		return -EBUSY;
1701ffbbdd21SLinus Walleij 	}
1702ffbbdd21SLinus Walleij 
17038caab75fSGeert Uytterhoeven 	ctlr->running = true;
17048caab75fSGeert Uytterhoeven 	ctlr->cur_msg = NULL;
17058caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1706ffbbdd21SLinus Walleij 
17078caab75fSGeert Uytterhoeven 	kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1708ffbbdd21SLinus Walleij 
1709ffbbdd21SLinus Walleij 	return 0;
1710ffbbdd21SLinus Walleij }
1711ffbbdd21SLinus Walleij 
17128caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr)
1713ffbbdd21SLinus Walleij {
1714ffbbdd21SLinus Walleij 	unsigned long flags;
1715ffbbdd21SLinus Walleij 	unsigned limit = 500;
1716ffbbdd21SLinus Walleij 	int ret = 0;
1717ffbbdd21SLinus Walleij 
17188caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
1719ffbbdd21SLinus Walleij 
1720ffbbdd21SLinus Walleij 	/*
1721ffbbdd21SLinus Walleij 	 * This is a bit lame, but is optimized for the common execution path.
17228caab75fSGeert Uytterhoeven 	 * A wait_queue on the ctlr->busy could be used, but then the common
1723ffbbdd21SLinus Walleij 	 * execution path (pump_messages) would be required to call wake_up or
1724ffbbdd21SLinus Walleij 	 * friends on every SPI message. Do this instead.
1725ffbbdd21SLinus Walleij 	 */
17268caab75fSGeert Uytterhoeven 	while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
17278caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1728f97b26b0SAxel Lin 		usleep_range(10000, 11000);
17298caab75fSGeert Uytterhoeven 		spin_lock_irqsave(&ctlr->queue_lock, flags);
1730ffbbdd21SLinus Walleij 	}
1731ffbbdd21SLinus Walleij 
17328caab75fSGeert Uytterhoeven 	if (!list_empty(&ctlr->queue) || ctlr->busy)
1733ffbbdd21SLinus Walleij 		ret = -EBUSY;
1734ffbbdd21SLinus Walleij 	else
17358caab75fSGeert Uytterhoeven 		ctlr->running = false;
1736ffbbdd21SLinus Walleij 
17378caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1738ffbbdd21SLinus Walleij 
1739ffbbdd21SLinus Walleij 	if (ret) {
17408caab75fSGeert Uytterhoeven 		dev_warn(&ctlr->dev, "could not stop message queue\n");
1741ffbbdd21SLinus Walleij 		return ret;
1742ffbbdd21SLinus Walleij 	}
1743ffbbdd21SLinus Walleij 	return ret;
1744ffbbdd21SLinus Walleij }
1745ffbbdd21SLinus Walleij 
17468caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr)
1747ffbbdd21SLinus Walleij {
1748ffbbdd21SLinus Walleij 	int ret;
1749ffbbdd21SLinus Walleij 
17508caab75fSGeert Uytterhoeven 	ret = spi_stop_queue(ctlr);
1751ffbbdd21SLinus Walleij 
1752ffbbdd21SLinus Walleij 	/*
17533989144fSPetr Mladek 	 * kthread_flush_worker will block until all work is done.
1754ffbbdd21SLinus Walleij 	 * If the reason that stop_queue timed out is that the work will never
1755ffbbdd21SLinus Walleij 	 * finish, then it does no good to call flush/stop thread, so
1756ffbbdd21SLinus Walleij 	 * return anyway.
1757ffbbdd21SLinus Walleij 	 */
1758ffbbdd21SLinus Walleij 	if (ret) {
17598caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem destroying queue\n");
1760ffbbdd21SLinus Walleij 		return ret;
1761ffbbdd21SLinus Walleij 	}
1762ffbbdd21SLinus Walleij 
17638caab75fSGeert Uytterhoeven 	kthread_flush_worker(&ctlr->kworker);
17648caab75fSGeert Uytterhoeven 	kthread_stop(ctlr->kworker_task);
1765ffbbdd21SLinus Walleij 
1766ffbbdd21SLinus Walleij 	return 0;
1767ffbbdd21SLinus Walleij }
1768ffbbdd21SLinus Walleij 
17690461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi,
17700461a414SMark Brown 				 struct spi_message *msg,
17710461a414SMark Brown 				 bool need_pump)
1772ffbbdd21SLinus Walleij {
17738caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
1774ffbbdd21SLinus Walleij 	unsigned long flags;
1775ffbbdd21SLinus Walleij 
17768caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
1777ffbbdd21SLinus Walleij 
17788caab75fSGeert Uytterhoeven 	if (!ctlr->running) {
17798caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1780ffbbdd21SLinus Walleij 		return -ESHUTDOWN;
1781ffbbdd21SLinus Walleij 	}
1782ffbbdd21SLinus Walleij 	msg->actual_length = 0;
1783ffbbdd21SLinus Walleij 	msg->status = -EINPROGRESS;
1784ffbbdd21SLinus Walleij 
17858caab75fSGeert Uytterhoeven 	list_add_tail(&msg->queue, &ctlr->queue);
1786f0125f1aSMark Brown 	if (!ctlr->busy && need_pump)
17878caab75fSGeert Uytterhoeven 		kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1788ffbbdd21SLinus Walleij 
17898caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1790ffbbdd21SLinus Walleij 	return 0;
1791ffbbdd21SLinus Walleij }
1792ffbbdd21SLinus Walleij 
17930461a414SMark Brown /**
17940461a414SMark Brown  * spi_queued_transfer - transfer function for queued transfers
17950461a414SMark Brown  * @spi: spi device which is requesting transfer
17960461a414SMark Brown  * @msg: spi message which is to handled is queued to driver queue
179797d56dc6SJavier Martinez Canillas  *
179897d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
17990461a414SMark Brown  */
18000461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
18010461a414SMark Brown {
18020461a414SMark Brown 	return __spi_queued_transfer(spi, msg, true);
18030461a414SMark Brown }
18040461a414SMark Brown 
18058caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr)
1806ffbbdd21SLinus Walleij {
1807ffbbdd21SLinus Walleij 	int ret;
1808ffbbdd21SLinus Walleij 
18098caab75fSGeert Uytterhoeven 	ctlr->transfer = spi_queued_transfer;
18108caab75fSGeert Uytterhoeven 	if (!ctlr->transfer_one_message)
18118caab75fSGeert Uytterhoeven 		ctlr->transfer_one_message = spi_transfer_one_message;
1812ffbbdd21SLinus Walleij 
1813ffbbdd21SLinus Walleij 	/* Initialize and start queue */
18148caab75fSGeert Uytterhoeven 	ret = spi_init_queue(ctlr);
1815ffbbdd21SLinus Walleij 	if (ret) {
18168caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem initializing queue\n");
1817ffbbdd21SLinus Walleij 		goto err_init_queue;
1818ffbbdd21SLinus Walleij 	}
18198caab75fSGeert Uytterhoeven 	ctlr->queued = true;
18208caab75fSGeert Uytterhoeven 	ret = spi_start_queue(ctlr);
1821ffbbdd21SLinus Walleij 	if (ret) {
18228caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem starting queue\n");
1823ffbbdd21SLinus Walleij 		goto err_start_queue;
1824ffbbdd21SLinus Walleij 	}
1825ffbbdd21SLinus Walleij 
1826ffbbdd21SLinus Walleij 	return 0;
1827ffbbdd21SLinus Walleij 
1828ffbbdd21SLinus Walleij err_start_queue:
18298caab75fSGeert Uytterhoeven 	spi_destroy_queue(ctlr);
1830c3676d5cSMark Brown err_init_queue:
1831ffbbdd21SLinus Walleij 	return ret;
1832ffbbdd21SLinus Walleij }
1833ffbbdd21SLinus Walleij 
1834988f259bSBoris Brezillon /**
1835988f259bSBoris Brezillon  * spi_flush_queue - Send all pending messages in the queue from the callers'
1836988f259bSBoris Brezillon  *		     context
1837988f259bSBoris Brezillon  * @ctlr: controller to process queue for
1838988f259bSBoris Brezillon  *
1839988f259bSBoris Brezillon  * This should be used when one wants to ensure all pending messages have been
1840988f259bSBoris Brezillon  * sent before doing something. Is used by the spi-mem code to make sure SPI
1841988f259bSBoris Brezillon  * memory operations do not preempt regular SPI transfers that have been queued
1842988f259bSBoris Brezillon  * before the spi-mem operation.
1843988f259bSBoris Brezillon  */
1844988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr)
1845988f259bSBoris Brezillon {
1846988f259bSBoris Brezillon 	if (ctlr->transfer == spi_queued_transfer)
1847988f259bSBoris Brezillon 		__spi_pump_messages(ctlr, false);
1848988f259bSBoris Brezillon }
1849988f259bSBoris Brezillon 
1850ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/
1851ffbbdd21SLinus Walleij 
18527cb94361SAndreas Larsson #if defined(CONFIG_OF)
18538caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
1854c2e51ac3SGeert Uytterhoeven 			   struct device_node *nc)
1855d57a4282SGrant Likely {
185689da4293STrent Piepho 	u32 value;
1857c2e51ac3SGeert Uytterhoeven 	int rc;
1858d57a4282SGrant Likely 
1859d57a4282SGrant Likely 	/* Mode (clock phase/polarity/etc.) */
1860e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-cpha"))
1861d57a4282SGrant Likely 		spi->mode |= SPI_CPHA;
1862e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-cpol"))
1863d57a4282SGrant Likely 		spi->mode |= SPI_CPOL;
1864e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-3wire"))
1865c20151dfSLars-Peter Clausen 		spi->mode |= SPI_3WIRE;
1866e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-lsb-first"))
1867cd6339e6SZhao Qiang 		spi->mode |= SPI_LSB_FIRST;
1868d57a4282SGrant Likely 
1869f3186dd8SLinus Walleij 	/*
1870f3186dd8SLinus Walleij 	 * For descriptors associated with the device, polarity inversion is
1871f3186dd8SLinus Walleij 	 * handled in the gpiolib, so all chip selects are "active high" in
1872f3186dd8SLinus Walleij 	 * the logical sense, the gpiolib will invert the line if need be.
1873f3186dd8SLinus Walleij 	 */
1874f3186dd8SLinus Walleij 	if (ctlr->use_gpio_descriptors)
1875f3186dd8SLinus Walleij 		spi->mode |= SPI_CS_HIGH;
1876f3186dd8SLinus Walleij 	else if (of_property_read_bool(nc, "spi-cs-high"))
1877f3186dd8SLinus Walleij 		spi->mode |= SPI_CS_HIGH;
1878f3186dd8SLinus Walleij 
1879f477b7fbSwangyuhang 	/* Device DUAL/QUAD mode */
188089da4293STrent Piepho 	if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
188189da4293STrent Piepho 		switch (value) {
188289da4293STrent Piepho 		case 1:
1883f477b7fbSwangyuhang 			break;
188489da4293STrent Piepho 		case 2:
1885f477b7fbSwangyuhang 			spi->mode |= SPI_TX_DUAL;
1886f477b7fbSwangyuhang 			break;
188789da4293STrent Piepho 		case 4:
1888f477b7fbSwangyuhang 			spi->mode |= SPI_TX_QUAD;
1889f477b7fbSwangyuhang 			break;
18906b03061fSYogesh Narayan Gaur 		case 8:
18916b03061fSYogesh Narayan Gaur 			spi->mode |= SPI_TX_OCTAL;
18926b03061fSYogesh Narayan Gaur 			break;
1893f477b7fbSwangyuhang 		default:
18948caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
1895a110f93dSwangyuhang 				"spi-tx-bus-width %d not supported\n",
189689da4293STrent Piepho 				value);
189780874d8cSGeert Uytterhoeven 			break;
1898f477b7fbSwangyuhang 		}
1899a822e99cSMark Brown 	}
1900f477b7fbSwangyuhang 
190189da4293STrent Piepho 	if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
190289da4293STrent Piepho 		switch (value) {
190389da4293STrent Piepho 		case 1:
1904f477b7fbSwangyuhang 			break;
190589da4293STrent Piepho 		case 2:
1906f477b7fbSwangyuhang 			spi->mode |= SPI_RX_DUAL;
1907f477b7fbSwangyuhang 			break;
190889da4293STrent Piepho 		case 4:
1909f477b7fbSwangyuhang 			spi->mode |= SPI_RX_QUAD;
1910f477b7fbSwangyuhang 			break;
19116b03061fSYogesh Narayan Gaur 		case 8:
19126b03061fSYogesh Narayan Gaur 			spi->mode |= SPI_RX_OCTAL;
19136b03061fSYogesh Narayan Gaur 			break;
1914f477b7fbSwangyuhang 		default:
19158caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
1916a110f93dSwangyuhang 				"spi-rx-bus-width %d not supported\n",
191789da4293STrent Piepho 				value);
191880874d8cSGeert Uytterhoeven 			break;
1919f477b7fbSwangyuhang 		}
1920a822e99cSMark Brown 	}
1921f477b7fbSwangyuhang 
19228caab75fSGeert Uytterhoeven 	if (spi_controller_is_slave(ctlr)) {
1923194276b0SRob Herring 		if (!of_node_name_eq(nc, "slave")) {
192425c56c88SRob Herring 			dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
192525c56c88SRob Herring 				nc);
19266c364062SGeert Uytterhoeven 			return -EINVAL;
19276c364062SGeert Uytterhoeven 		}
19286c364062SGeert Uytterhoeven 		return 0;
19296c364062SGeert Uytterhoeven 	}
19306c364062SGeert Uytterhoeven 
19316c364062SGeert Uytterhoeven 	/* Device address */
19326c364062SGeert Uytterhoeven 	rc = of_property_read_u32(nc, "reg", &value);
19336c364062SGeert Uytterhoeven 	if (rc) {
193425c56c88SRob Herring 		dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
193525c56c88SRob Herring 			nc, rc);
19366c364062SGeert Uytterhoeven 		return rc;
19376c364062SGeert Uytterhoeven 	}
19386c364062SGeert Uytterhoeven 	spi->chip_select = value;
19396c364062SGeert Uytterhoeven 
1940d57a4282SGrant Likely 	/* Device speed */
194189da4293STrent Piepho 	rc = of_property_read_u32(nc, "spi-max-frequency", &value);
194289da4293STrent Piepho 	if (rc) {
19438caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev,
194425c56c88SRob Herring 			"%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
1945c2e51ac3SGeert Uytterhoeven 		return rc;
1946d57a4282SGrant Likely 	}
194789da4293STrent Piepho 	spi->max_speed_hz = value;
1948d57a4282SGrant Likely 
1949c2e51ac3SGeert Uytterhoeven 	return 0;
1950c2e51ac3SGeert Uytterhoeven }
1951c2e51ac3SGeert Uytterhoeven 
1952c2e51ac3SGeert Uytterhoeven static struct spi_device *
19538caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
1954c2e51ac3SGeert Uytterhoeven {
1955c2e51ac3SGeert Uytterhoeven 	struct spi_device *spi;
1956c2e51ac3SGeert Uytterhoeven 	int rc;
1957c2e51ac3SGeert Uytterhoeven 
1958c2e51ac3SGeert Uytterhoeven 	/* Alloc an spi_device */
19598caab75fSGeert Uytterhoeven 	spi = spi_alloc_device(ctlr);
1960c2e51ac3SGeert Uytterhoeven 	if (!spi) {
196125c56c88SRob Herring 		dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
1962c2e51ac3SGeert Uytterhoeven 		rc = -ENOMEM;
1963c2e51ac3SGeert Uytterhoeven 		goto err_out;
1964c2e51ac3SGeert Uytterhoeven 	}
1965c2e51ac3SGeert Uytterhoeven 
1966c2e51ac3SGeert Uytterhoeven 	/* Select device driver */
1967c2e51ac3SGeert Uytterhoeven 	rc = of_modalias_node(nc, spi->modalias,
1968c2e51ac3SGeert Uytterhoeven 				sizeof(spi->modalias));
1969c2e51ac3SGeert Uytterhoeven 	if (rc < 0) {
197025c56c88SRob Herring 		dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
1971c2e51ac3SGeert Uytterhoeven 		goto err_out;
1972c2e51ac3SGeert Uytterhoeven 	}
1973c2e51ac3SGeert Uytterhoeven 
19748caab75fSGeert Uytterhoeven 	rc = of_spi_parse_dt(ctlr, spi, nc);
1975c2e51ac3SGeert Uytterhoeven 	if (rc)
1976c2e51ac3SGeert Uytterhoeven 		goto err_out;
1977c2e51ac3SGeert Uytterhoeven 
1978d57a4282SGrant Likely 	/* Store a pointer to the node in the device structure */
1979d57a4282SGrant Likely 	of_node_get(nc);
1980d57a4282SGrant Likely 	spi->dev.of_node = nc;
1981d57a4282SGrant Likely 
1982d57a4282SGrant Likely 	/* Register the new device */
1983d57a4282SGrant Likely 	rc = spi_add_device(spi);
1984d57a4282SGrant Likely 	if (rc) {
198525c56c88SRob Herring 		dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
19868324147fSJohan Hovold 		goto err_of_node_put;
1987d57a4282SGrant Likely 	}
1988d57a4282SGrant Likely 
1989aff5e3f8SPantelis Antoniou 	return spi;
1990aff5e3f8SPantelis Antoniou 
19918324147fSJohan Hovold err_of_node_put:
19928324147fSJohan Hovold 	of_node_put(nc);
1993aff5e3f8SPantelis Antoniou err_out:
1994aff5e3f8SPantelis Antoniou 	spi_dev_put(spi);
1995aff5e3f8SPantelis Antoniou 	return ERR_PTR(rc);
1996aff5e3f8SPantelis Antoniou }
1997aff5e3f8SPantelis Antoniou 
1998aff5e3f8SPantelis Antoniou /**
1999aff5e3f8SPantelis Antoniou  * of_register_spi_devices() - Register child devices onto the SPI bus
20008caab75fSGeert Uytterhoeven  * @ctlr:	Pointer to spi_controller device
2001aff5e3f8SPantelis Antoniou  *
20026c364062SGeert Uytterhoeven  * Registers an spi_device for each child node of controller node which
20036c364062SGeert Uytterhoeven  * represents a valid SPI slave.
2004aff5e3f8SPantelis Antoniou  */
20058caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr)
2006aff5e3f8SPantelis Antoniou {
2007aff5e3f8SPantelis Antoniou 	struct spi_device *spi;
2008aff5e3f8SPantelis Antoniou 	struct device_node *nc;
2009aff5e3f8SPantelis Antoniou 
20108caab75fSGeert Uytterhoeven 	if (!ctlr->dev.of_node)
2011aff5e3f8SPantelis Antoniou 		return;
2012aff5e3f8SPantelis Antoniou 
20138caab75fSGeert Uytterhoeven 	for_each_available_child_of_node(ctlr->dev.of_node, nc) {
2014bd6c1644SGeert Uytterhoeven 		if (of_node_test_and_set_flag(nc, OF_POPULATED))
2015bd6c1644SGeert Uytterhoeven 			continue;
20168caab75fSGeert Uytterhoeven 		spi = of_register_spi_device(ctlr, nc);
2017e0af98a7SRalf Ramsauer 		if (IS_ERR(spi)) {
20188caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
201925c56c88SRob Herring 				 "Failed to create SPI device for %pOF\n", nc);
2020e0af98a7SRalf Ramsauer 			of_node_clear_flag(nc, OF_POPULATED);
2021e0af98a7SRalf Ramsauer 		}
2022d57a4282SGrant Likely 	}
2023d57a4282SGrant Likely }
2024d57a4282SGrant Likely #else
20258caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { }
2026d57a4282SGrant Likely #endif
2027d57a4282SGrant Likely 
202864bee4d2SMika Westerberg #ifdef CONFIG_ACPI
20294c3c5954SArd Biesheuvel struct acpi_spi_lookup {
20304c3c5954SArd Biesheuvel 	struct spi_controller 	*ctlr;
20314c3c5954SArd Biesheuvel 	u32			max_speed_hz;
20324c3c5954SArd Biesheuvel 	u32			mode;
20334c3c5954SArd Biesheuvel 	int			irq;
20344c3c5954SArd Biesheuvel 	u8			bits_per_word;
20354c3c5954SArd Biesheuvel 	u8			chip_select;
20364c3c5954SArd Biesheuvel };
20374c3c5954SArd Biesheuvel 
20384c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
20394c3c5954SArd Biesheuvel 					    struct acpi_spi_lookup *lookup)
20408a2e487eSLukas Wunner {
20418a2e487eSLukas Wunner 	const union acpi_object *obj;
20428a2e487eSLukas Wunner 
20438a2e487eSLukas Wunner 	if (!x86_apple_machine)
20448a2e487eSLukas Wunner 		return;
20458a2e487eSLukas Wunner 
20468a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
20478a2e487eSLukas Wunner 	    && obj->buffer.length >= 4)
20484c3c5954SArd Biesheuvel 		lookup->max_speed_hz  = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
20498a2e487eSLukas Wunner 
20508a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
20518a2e487eSLukas Wunner 	    && obj->buffer.length == 8)
20524c3c5954SArd Biesheuvel 		lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
20538a2e487eSLukas Wunner 
20548a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
20558a2e487eSLukas Wunner 	    && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
20564c3c5954SArd Biesheuvel 		lookup->mode |= SPI_LSB_FIRST;
20578a2e487eSLukas Wunner 
20588a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
20598a2e487eSLukas Wunner 	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
20604c3c5954SArd Biesheuvel 		lookup->mode |= SPI_CPOL;
20618a2e487eSLukas Wunner 
20628a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
20638a2e487eSLukas Wunner 	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
20644c3c5954SArd Biesheuvel 		lookup->mode |= SPI_CPHA;
20658a2e487eSLukas Wunner }
20668a2e487eSLukas Wunner 
206764bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
206864bee4d2SMika Westerberg {
20694c3c5954SArd Biesheuvel 	struct acpi_spi_lookup *lookup = data;
20704c3c5954SArd Biesheuvel 	struct spi_controller *ctlr = lookup->ctlr;
207164bee4d2SMika Westerberg 
207264bee4d2SMika Westerberg 	if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
207364bee4d2SMika Westerberg 		struct acpi_resource_spi_serialbus *sb;
20744c3c5954SArd Biesheuvel 		acpi_handle parent_handle;
20754c3c5954SArd Biesheuvel 		acpi_status status;
207664bee4d2SMika Westerberg 
207764bee4d2SMika Westerberg 		sb = &ares->data.spi_serial_bus;
207864bee4d2SMika Westerberg 		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
20794c3c5954SArd Biesheuvel 
20804c3c5954SArd Biesheuvel 			status = acpi_get_handle(NULL,
20814c3c5954SArd Biesheuvel 						 sb->resource_source.string_ptr,
20824c3c5954SArd Biesheuvel 						 &parent_handle);
20834c3c5954SArd Biesheuvel 
2084b5e3cf41SArd Biesheuvel 			if (ACPI_FAILURE(status) ||
20854c3c5954SArd Biesheuvel 			    ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
20864c3c5954SArd Biesheuvel 				return -ENODEV;
20874c3c5954SArd Biesheuvel 
2088a0a90718SMika Westerberg 			/*
2089a0a90718SMika Westerberg 			 * ACPI DeviceSelection numbering is handled by the
2090a0a90718SMika Westerberg 			 * host controller driver in Windows and can vary
2091a0a90718SMika Westerberg 			 * from driver to driver. In Linux we always expect
2092a0a90718SMika Westerberg 			 * 0 .. max - 1 so we need to ask the driver to
2093a0a90718SMika Westerberg 			 * translate between the two schemes.
2094a0a90718SMika Westerberg 			 */
20958caab75fSGeert Uytterhoeven 			if (ctlr->fw_translate_cs) {
20968caab75fSGeert Uytterhoeven 				int cs = ctlr->fw_translate_cs(ctlr,
2097a0a90718SMika Westerberg 						sb->device_selection);
2098a0a90718SMika Westerberg 				if (cs < 0)
2099a0a90718SMika Westerberg 					return cs;
21004c3c5954SArd Biesheuvel 				lookup->chip_select = cs;
2101a0a90718SMika Westerberg 			} else {
21024c3c5954SArd Biesheuvel 				lookup->chip_select = sb->device_selection;
2103a0a90718SMika Westerberg 			}
2104a0a90718SMika Westerberg 
21054c3c5954SArd Biesheuvel 			lookup->max_speed_hz = sb->connection_speed;
210664bee4d2SMika Westerberg 
210764bee4d2SMika Westerberg 			if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
21084c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CPHA;
210964bee4d2SMika Westerberg 			if (sb->clock_polarity == ACPI_SPI_START_HIGH)
21104c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CPOL;
211164bee4d2SMika Westerberg 			if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
21124c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CS_HIGH;
211364bee4d2SMika Westerberg 		}
21144c3c5954SArd Biesheuvel 	} else if (lookup->irq < 0) {
211564bee4d2SMika Westerberg 		struct resource r;
211664bee4d2SMika Westerberg 
211764bee4d2SMika Westerberg 		if (acpi_dev_resource_interrupt(ares, 0, &r))
21184c3c5954SArd Biesheuvel 			lookup->irq = r.start;
211964bee4d2SMika Westerberg 	}
212064bee4d2SMika Westerberg 
212164bee4d2SMika Westerberg 	/* Always tell the ACPI core to skip this resource */
212264bee4d2SMika Westerberg 	return 1;
212364bee4d2SMika Westerberg }
212464bee4d2SMika Westerberg 
21258caab75fSGeert Uytterhoeven static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
21267f24467fSOctavian Purdila 					    struct acpi_device *adev)
212764bee4d2SMika Westerberg {
21284c3c5954SArd Biesheuvel 	acpi_handle parent_handle = NULL;
212964bee4d2SMika Westerberg 	struct list_head resource_list;
2130b28944c6SArd Biesheuvel 	struct acpi_spi_lookup lookup = {};
213164bee4d2SMika Westerberg 	struct spi_device *spi;
213264bee4d2SMika Westerberg 	int ret;
213364bee4d2SMika Westerberg 
21347f24467fSOctavian Purdila 	if (acpi_bus_get_status(adev) || !adev->status.present ||
21357f24467fSOctavian Purdila 	    acpi_device_enumerated(adev))
213664bee4d2SMika Westerberg 		return AE_OK;
213764bee4d2SMika Westerberg 
21384c3c5954SArd Biesheuvel 	lookup.ctlr		= ctlr;
21394c3c5954SArd Biesheuvel 	lookup.irq		= -1;
21404c3c5954SArd Biesheuvel 
21414c3c5954SArd Biesheuvel 	INIT_LIST_HEAD(&resource_list);
21424c3c5954SArd Biesheuvel 	ret = acpi_dev_get_resources(adev, &resource_list,
21434c3c5954SArd Biesheuvel 				     acpi_spi_add_resource, &lookup);
21444c3c5954SArd Biesheuvel 	acpi_dev_free_resource_list(&resource_list);
21454c3c5954SArd Biesheuvel 
21464c3c5954SArd Biesheuvel 	if (ret < 0)
21474c3c5954SArd Biesheuvel 		/* found SPI in _CRS but it points to another controller */
21484c3c5954SArd Biesheuvel 		return AE_OK;
21494c3c5954SArd Biesheuvel 
21504c3c5954SArd Biesheuvel 	if (!lookup.max_speed_hz &&
21514c3c5954SArd Biesheuvel 	    !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
21524c3c5954SArd Biesheuvel 	    ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
21534c3c5954SArd Biesheuvel 		/* Apple does not use _CRS but nested devices for SPI slaves */
21544c3c5954SArd Biesheuvel 		acpi_spi_parse_apple_properties(adev, &lookup);
21554c3c5954SArd Biesheuvel 	}
21564c3c5954SArd Biesheuvel 
21574c3c5954SArd Biesheuvel 	if (!lookup.max_speed_hz)
21584c3c5954SArd Biesheuvel 		return AE_OK;
21594c3c5954SArd Biesheuvel 
21608caab75fSGeert Uytterhoeven 	spi = spi_alloc_device(ctlr);
216164bee4d2SMika Westerberg 	if (!spi) {
21628caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
216364bee4d2SMika Westerberg 			dev_name(&adev->dev));
216464bee4d2SMika Westerberg 		return AE_NO_MEMORY;
216564bee4d2SMika Westerberg 	}
216664bee4d2SMika Westerberg 
21677b199811SRafael J. Wysocki 	ACPI_COMPANION_SET(&spi->dev, adev);
21684c3c5954SArd Biesheuvel 	spi->max_speed_hz	= lookup.max_speed_hz;
21694c3c5954SArd Biesheuvel 	spi->mode		= lookup.mode;
21704c3c5954SArd Biesheuvel 	spi->irq		= lookup.irq;
21714c3c5954SArd Biesheuvel 	spi->bits_per_word	= lookup.bits_per_word;
21724c3c5954SArd Biesheuvel 	spi->chip_select	= lookup.chip_select;
217364bee4d2SMika Westerberg 
21740c6543f6SDan O'Donovan 	acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
21750c6543f6SDan O'Donovan 			  sizeof(spi->modalias));
21760c6543f6SDan O'Donovan 
217733ada67dSChristophe RICARD 	if (spi->irq < 0)
217833ada67dSChristophe RICARD 		spi->irq = acpi_dev_gpio_irq_get(adev, 0);
217933ada67dSChristophe RICARD 
21807f24467fSOctavian Purdila 	acpi_device_set_enumerated(adev);
21817f24467fSOctavian Purdila 
218233cf00e5SMika Westerberg 	adev->power.flags.ignore_parent = true;
218364bee4d2SMika Westerberg 	if (spi_add_device(spi)) {
218433cf00e5SMika Westerberg 		adev->power.flags.ignore_parent = false;
21858caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
218664bee4d2SMika Westerberg 			dev_name(&adev->dev));
218764bee4d2SMika Westerberg 		spi_dev_put(spi);
218864bee4d2SMika Westerberg 	}
218964bee4d2SMika Westerberg 
219064bee4d2SMika Westerberg 	return AE_OK;
219164bee4d2SMika Westerberg }
219264bee4d2SMika Westerberg 
21937f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
21947f24467fSOctavian Purdila 				       void *data, void **return_value)
21957f24467fSOctavian Purdila {
21968caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = data;
21977f24467fSOctavian Purdila 	struct acpi_device *adev;
21987f24467fSOctavian Purdila 
21997f24467fSOctavian Purdila 	if (acpi_bus_get_device(handle, &adev))
22007f24467fSOctavian Purdila 		return AE_OK;
22017f24467fSOctavian Purdila 
22028caab75fSGeert Uytterhoeven 	return acpi_register_spi_device(ctlr, adev);
22037f24467fSOctavian Purdila }
22047f24467fSOctavian Purdila 
22054c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH		32
22064c3c5954SArd Biesheuvel 
22078caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr)
220864bee4d2SMika Westerberg {
220964bee4d2SMika Westerberg 	acpi_status status;
221064bee4d2SMika Westerberg 	acpi_handle handle;
221164bee4d2SMika Westerberg 
22128caab75fSGeert Uytterhoeven 	handle = ACPI_HANDLE(ctlr->dev.parent);
221364bee4d2SMika Westerberg 	if (!handle)
221464bee4d2SMika Westerberg 		return;
221564bee4d2SMika Westerberg 
22164c3c5954SArd Biesheuvel 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
22174c3c5954SArd Biesheuvel 				     SPI_ACPI_ENUMERATE_MAX_DEPTH,
22188caab75fSGeert Uytterhoeven 				     acpi_spi_add_device, NULL, ctlr, NULL);
221964bee4d2SMika Westerberg 	if (ACPI_FAILURE(status))
22208caab75fSGeert Uytterhoeven 		dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
222164bee4d2SMika Westerberg }
222264bee4d2SMika Westerberg #else
22238caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
222464bee4d2SMika Westerberg #endif /* CONFIG_ACPI */
222564bee4d2SMika Westerberg 
22268caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev)
22278ae12a0dSDavid Brownell {
22288caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
22298ae12a0dSDavid Brownell 
22308caab75fSGeert Uytterhoeven 	ctlr = container_of(dev, struct spi_controller, dev);
22318caab75fSGeert Uytterhoeven 	kfree(ctlr);
22328ae12a0dSDavid Brownell }
22338ae12a0dSDavid Brownell 
22348ae12a0dSDavid Brownell static struct class spi_master_class = {
22358ae12a0dSDavid Brownell 	.name		= "spi_master",
22368ae12a0dSDavid Brownell 	.owner		= THIS_MODULE,
22378caab75fSGeert Uytterhoeven 	.dev_release	= spi_controller_release,
2238eca2ebc7SMartin Sperl 	.dev_groups	= spi_master_groups,
22398ae12a0dSDavid Brownell };
22408ae12a0dSDavid Brownell 
22416c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE
22426c364062SGeert Uytterhoeven /**
22436c364062SGeert Uytterhoeven  * spi_slave_abort - abort the ongoing transfer request on an SPI slave
22446c364062SGeert Uytterhoeven  *		     controller
22456c364062SGeert Uytterhoeven  * @spi: device used for the current transfer
22466c364062SGeert Uytterhoeven  */
22476c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi)
22486c364062SGeert Uytterhoeven {
22498caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
22506c364062SGeert Uytterhoeven 
22518caab75fSGeert Uytterhoeven 	if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
22528caab75fSGeert Uytterhoeven 		return ctlr->slave_abort(ctlr);
22536c364062SGeert Uytterhoeven 
22546c364062SGeert Uytterhoeven 	return -ENOTSUPP;
22556c364062SGeert Uytterhoeven }
22566c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort);
22576c364062SGeert Uytterhoeven 
22586c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data)
22596c364062SGeert Uytterhoeven {
22606c364062SGeert Uytterhoeven 	return 1;
22616c364062SGeert Uytterhoeven }
22626c364062SGeert Uytterhoeven 
2263cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2264cc8b4659SGeert Uytterhoeven 			  char *buf)
22656c364062SGeert Uytterhoeven {
22668caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
22678caab75fSGeert Uytterhoeven 						   dev);
22686c364062SGeert Uytterhoeven 	struct device *child;
22696c364062SGeert Uytterhoeven 
22706c364062SGeert Uytterhoeven 	child = device_find_child(&ctlr->dev, NULL, match_true);
22716c364062SGeert Uytterhoeven 	return sprintf(buf, "%s\n",
22726c364062SGeert Uytterhoeven 		       child ? to_spi_device(child)->modalias : NULL);
22736c364062SGeert Uytterhoeven }
22746c364062SGeert Uytterhoeven 
2275cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2276cc8b4659SGeert Uytterhoeven 			   const char *buf, size_t count)
22776c364062SGeert Uytterhoeven {
22788caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
22798caab75fSGeert Uytterhoeven 						   dev);
22806c364062SGeert Uytterhoeven 	struct spi_device *spi;
22816c364062SGeert Uytterhoeven 	struct device *child;
22826c364062SGeert Uytterhoeven 	char name[32];
22836c364062SGeert Uytterhoeven 	int rc;
22846c364062SGeert Uytterhoeven 
22856c364062SGeert Uytterhoeven 	rc = sscanf(buf, "%31s", name);
22866c364062SGeert Uytterhoeven 	if (rc != 1 || !name[0])
22876c364062SGeert Uytterhoeven 		return -EINVAL;
22886c364062SGeert Uytterhoeven 
22896c364062SGeert Uytterhoeven 	child = device_find_child(&ctlr->dev, NULL, match_true);
22906c364062SGeert Uytterhoeven 	if (child) {
22916c364062SGeert Uytterhoeven 		/* Remove registered slave */
22926c364062SGeert Uytterhoeven 		device_unregister(child);
22936c364062SGeert Uytterhoeven 		put_device(child);
22946c364062SGeert Uytterhoeven 	}
22956c364062SGeert Uytterhoeven 
22966c364062SGeert Uytterhoeven 	if (strcmp(name, "(null)")) {
22976c364062SGeert Uytterhoeven 		/* Register new slave */
22986c364062SGeert Uytterhoeven 		spi = spi_alloc_device(ctlr);
22996c364062SGeert Uytterhoeven 		if (!spi)
23006c364062SGeert Uytterhoeven 			return -ENOMEM;
23016c364062SGeert Uytterhoeven 
23026c364062SGeert Uytterhoeven 		strlcpy(spi->modalias, name, sizeof(spi->modalias));
23036c364062SGeert Uytterhoeven 
23046c364062SGeert Uytterhoeven 		rc = spi_add_device(spi);
23056c364062SGeert Uytterhoeven 		if (rc) {
23066c364062SGeert Uytterhoeven 			spi_dev_put(spi);
23076c364062SGeert Uytterhoeven 			return rc;
23086c364062SGeert Uytterhoeven 		}
23096c364062SGeert Uytterhoeven 	}
23106c364062SGeert Uytterhoeven 
23116c364062SGeert Uytterhoeven 	return count;
23126c364062SGeert Uytterhoeven }
23136c364062SGeert Uytterhoeven 
2314cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave);
23156c364062SGeert Uytterhoeven 
23166c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = {
23176c364062SGeert Uytterhoeven 	&dev_attr_slave.attr,
23186c364062SGeert Uytterhoeven 	NULL,
23196c364062SGeert Uytterhoeven };
23206c364062SGeert Uytterhoeven 
23216c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = {
23226c364062SGeert Uytterhoeven 	.attrs = spi_slave_attrs,
23236c364062SGeert Uytterhoeven };
23246c364062SGeert Uytterhoeven 
23256c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = {
23268caab75fSGeert Uytterhoeven 	&spi_controller_statistics_group,
23276c364062SGeert Uytterhoeven 	&spi_slave_group,
23286c364062SGeert Uytterhoeven 	NULL,
23296c364062SGeert Uytterhoeven };
23306c364062SGeert Uytterhoeven 
23316c364062SGeert Uytterhoeven static struct class spi_slave_class = {
23326c364062SGeert Uytterhoeven 	.name		= "spi_slave",
23336c364062SGeert Uytterhoeven 	.owner		= THIS_MODULE,
23348caab75fSGeert Uytterhoeven 	.dev_release	= spi_controller_release,
23356c364062SGeert Uytterhoeven 	.dev_groups	= spi_slave_groups,
23366c364062SGeert Uytterhoeven };
23376c364062SGeert Uytterhoeven #else
23386c364062SGeert Uytterhoeven extern struct class spi_slave_class;	/* dummy */
23396c364062SGeert Uytterhoeven #endif
23408ae12a0dSDavid Brownell 
23418ae12a0dSDavid Brownell /**
23426c364062SGeert Uytterhoeven  * __spi_alloc_controller - allocate an SPI master or slave controller
23438ae12a0dSDavid Brownell  * @dev: the controller, possibly using the platform_bus
234433e34dc6SDavid Brownell  * @size: how much zeroed driver-private data to allocate; the pointer to this
2345229e6af1SLukas Wunner  *	memory is in the driver_data field of the returned device, accessible
2346229e6af1SLukas Wunner  *	with spi_controller_get_devdata(); the memory is cacheline aligned;
2347229e6af1SLukas Wunner  *	drivers granting DMA access to portions of their private data need to
2348229e6af1SLukas Wunner  *	round up @size using ALIGN(size, dma_get_cache_alignment()).
23496c364062SGeert Uytterhoeven  * @slave: flag indicating whether to allocate an SPI master (false) or SPI
23506c364062SGeert Uytterhoeven  *	slave (true) controller
235133e34dc6SDavid Brownell  * Context: can sleep
23528ae12a0dSDavid Brownell  *
23536c364062SGeert Uytterhoeven  * This call is used only by SPI controller drivers, which are the
23548ae12a0dSDavid Brownell  * only ones directly touching chip registers.  It's how they allocate
23558caab75fSGeert Uytterhoeven  * an spi_controller structure, prior to calling spi_register_controller().
23568ae12a0dSDavid Brownell  *
235797d56dc6SJavier Martinez Canillas  * This must be called from context that can sleep.
23588ae12a0dSDavid Brownell  *
23596c364062SGeert Uytterhoeven  * The caller is responsible for assigning the bus number and initializing the
23608caab75fSGeert Uytterhoeven  * controller's methods before calling spi_register_controller(); and (after
23618caab75fSGeert Uytterhoeven  * errors adding the device) calling spi_controller_put() to prevent a memory
23628caab75fSGeert Uytterhoeven  * leak.
236397d56dc6SJavier Martinez Canillas  *
23646c364062SGeert Uytterhoeven  * Return: the SPI controller structure on success, else NULL.
23658ae12a0dSDavid Brownell  */
23668caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev,
23676c364062SGeert Uytterhoeven 					      unsigned int size, bool slave)
23688ae12a0dSDavid Brownell {
23698caab75fSGeert Uytterhoeven 	struct spi_controller	*ctlr;
2370229e6af1SLukas Wunner 	size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
23718ae12a0dSDavid Brownell 
23720c868461SDavid Brownell 	if (!dev)
23730c868461SDavid Brownell 		return NULL;
23740c868461SDavid Brownell 
2375229e6af1SLukas Wunner 	ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
23768caab75fSGeert Uytterhoeven 	if (!ctlr)
23778ae12a0dSDavid Brownell 		return NULL;
23788ae12a0dSDavid Brownell 
23798caab75fSGeert Uytterhoeven 	device_initialize(&ctlr->dev);
23808caab75fSGeert Uytterhoeven 	ctlr->bus_num = -1;
23818caab75fSGeert Uytterhoeven 	ctlr->num_chipselect = 1;
23828caab75fSGeert Uytterhoeven 	ctlr->slave = slave;
23836c364062SGeert Uytterhoeven 	if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
23848caab75fSGeert Uytterhoeven 		ctlr->dev.class = &spi_slave_class;
23856c364062SGeert Uytterhoeven 	else
23868caab75fSGeert Uytterhoeven 		ctlr->dev.class = &spi_master_class;
23878caab75fSGeert Uytterhoeven 	ctlr->dev.parent = dev;
23888caab75fSGeert Uytterhoeven 	pm_suspend_ignore_children(&ctlr->dev, true);
2389229e6af1SLukas Wunner 	spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
23908ae12a0dSDavid Brownell 
23918caab75fSGeert Uytterhoeven 	return ctlr;
23928ae12a0dSDavid Brownell }
23936c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller);
23948ae12a0dSDavid Brownell 
239574317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF
239643004f31SLinus Walleij static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
239774317984SJean-Christophe PLAGNIOL-VILLARD {
2398e80beb27SGrant Likely 	int nb, i, *cs;
23998caab75fSGeert Uytterhoeven 	struct device_node *np = ctlr->dev.of_node;
240074317984SJean-Christophe PLAGNIOL-VILLARD 
240174317984SJean-Christophe PLAGNIOL-VILLARD 	if (!np)
240274317984SJean-Christophe PLAGNIOL-VILLARD 		return 0;
240374317984SJean-Christophe PLAGNIOL-VILLARD 
240474317984SJean-Christophe PLAGNIOL-VILLARD 	nb = of_gpio_named_count(np, "cs-gpios");
24058caab75fSGeert Uytterhoeven 	ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
240674317984SJean-Christophe PLAGNIOL-VILLARD 
24078ec5d84eSAndreas Larsson 	/* Return error only for an incorrectly formed cs-gpios property */
24088ec5d84eSAndreas Larsson 	if (nb == 0 || nb == -ENOENT)
240974317984SJean-Christophe PLAGNIOL-VILLARD 		return 0;
24108ec5d84eSAndreas Larsson 	else if (nb < 0)
24118ec5d84eSAndreas Larsson 		return nb;
241274317984SJean-Christophe PLAGNIOL-VILLARD 
2413a86854d0SKees Cook 	cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
241474317984SJean-Christophe PLAGNIOL-VILLARD 			  GFP_KERNEL);
24158caab75fSGeert Uytterhoeven 	ctlr->cs_gpios = cs;
241674317984SJean-Christophe PLAGNIOL-VILLARD 
24178caab75fSGeert Uytterhoeven 	if (!ctlr->cs_gpios)
241874317984SJean-Christophe PLAGNIOL-VILLARD 		return -ENOMEM;
241974317984SJean-Christophe PLAGNIOL-VILLARD 
24208caab75fSGeert Uytterhoeven 	for (i = 0; i < ctlr->num_chipselect; i++)
2421446411e1SAndreas Larsson 		cs[i] = -ENOENT;
242274317984SJean-Christophe PLAGNIOL-VILLARD 
242374317984SJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < nb; i++)
242474317984SJean-Christophe PLAGNIOL-VILLARD 		cs[i] = of_get_named_gpio(np, "cs-gpios", i);
242574317984SJean-Christophe PLAGNIOL-VILLARD 
242674317984SJean-Christophe PLAGNIOL-VILLARD 	return 0;
242774317984SJean-Christophe PLAGNIOL-VILLARD }
242874317984SJean-Christophe PLAGNIOL-VILLARD #else
242943004f31SLinus Walleij static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
243074317984SJean-Christophe PLAGNIOL-VILLARD {
243174317984SJean-Christophe PLAGNIOL-VILLARD 	return 0;
243274317984SJean-Christophe PLAGNIOL-VILLARD }
243374317984SJean-Christophe PLAGNIOL-VILLARD #endif
243474317984SJean-Christophe PLAGNIOL-VILLARD 
2435f3186dd8SLinus Walleij /**
2436f3186dd8SLinus Walleij  * spi_get_gpio_descs() - grab chip select GPIOs for the master
2437f3186dd8SLinus Walleij  * @ctlr: The SPI master to grab GPIO descriptors for
2438f3186dd8SLinus Walleij  */
2439f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr)
2440f3186dd8SLinus Walleij {
2441f3186dd8SLinus Walleij 	int nb, i;
2442f3186dd8SLinus Walleij 	struct gpio_desc **cs;
2443f3186dd8SLinus Walleij 	struct device *dev = &ctlr->dev;
2444f3186dd8SLinus Walleij 
2445f3186dd8SLinus Walleij 	nb = gpiod_count(dev, "cs");
2446f3186dd8SLinus Walleij 	ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2447f3186dd8SLinus Walleij 
2448f3186dd8SLinus Walleij 	/* No GPIOs at all is fine, else return the error */
2449f3186dd8SLinus Walleij 	if (nb == 0 || nb == -ENOENT)
2450f3186dd8SLinus Walleij 		return 0;
2451f3186dd8SLinus Walleij 	else if (nb < 0)
2452f3186dd8SLinus Walleij 		return nb;
2453f3186dd8SLinus Walleij 
2454f3186dd8SLinus Walleij 	cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2455f3186dd8SLinus Walleij 			  GFP_KERNEL);
2456f3186dd8SLinus Walleij 	if (!cs)
2457f3186dd8SLinus Walleij 		return -ENOMEM;
2458f3186dd8SLinus Walleij 	ctlr->cs_gpiods = cs;
2459f3186dd8SLinus Walleij 
2460f3186dd8SLinus Walleij 	for (i = 0; i < nb; i++) {
2461f3186dd8SLinus Walleij 		/*
2462f3186dd8SLinus Walleij 		 * Most chipselects are active low, the inverted
2463f3186dd8SLinus Walleij 		 * semantics are handled by special quirks in gpiolib,
2464f3186dd8SLinus Walleij 		 * so initializing them GPIOD_OUT_LOW here means
2465f3186dd8SLinus Walleij 		 * "unasserted", in most cases this will drive the physical
2466f3186dd8SLinus Walleij 		 * line high.
2467f3186dd8SLinus Walleij 		 */
2468f3186dd8SLinus Walleij 		cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2469f3186dd8SLinus Walleij 						      GPIOD_OUT_LOW);
24701723fdecSGeert Uytterhoeven 		if (IS_ERR(cs[i]))
24711723fdecSGeert Uytterhoeven 			return PTR_ERR(cs[i]);
2472f3186dd8SLinus Walleij 
2473f3186dd8SLinus Walleij 		if (cs[i]) {
2474f3186dd8SLinus Walleij 			/*
2475f3186dd8SLinus Walleij 			 * If we find a CS GPIO, name it after the device and
2476f3186dd8SLinus Walleij 			 * chip select line.
2477f3186dd8SLinus Walleij 			 */
2478f3186dd8SLinus Walleij 			char *gpioname;
2479f3186dd8SLinus Walleij 
2480f3186dd8SLinus Walleij 			gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2481f3186dd8SLinus Walleij 						  dev_name(dev), i);
2482f3186dd8SLinus Walleij 			if (!gpioname)
2483f3186dd8SLinus Walleij 				return -ENOMEM;
2484f3186dd8SLinus Walleij 			gpiod_set_consumer_name(cs[i], gpioname);
2485f3186dd8SLinus Walleij 		}
2486f3186dd8SLinus Walleij 	}
2487f3186dd8SLinus Walleij 
2488f3186dd8SLinus Walleij 	return 0;
2489f3186dd8SLinus Walleij }
2490f3186dd8SLinus Walleij 
2491bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr)
2492bdf3a3b5SBoris Brezillon {
2493bdf3a3b5SBoris Brezillon 	/*
2494b5932f5cSBoris Brezillon 	 * The controller may implement only the high-level SPI-memory like
2495b5932f5cSBoris Brezillon 	 * operations if it does not support regular SPI transfers, and this is
2496b5932f5cSBoris Brezillon 	 * valid use case.
2497b5932f5cSBoris Brezillon 	 * If ->mem_ops is NULL, we request that at least one of the
2498b5932f5cSBoris Brezillon 	 * ->transfer_xxx() method be implemented.
2499bdf3a3b5SBoris Brezillon 	 */
2500b5932f5cSBoris Brezillon 	if (ctlr->mem_ops) {
2501b5932f5cSBoris Brezillon 		if (!ctlr->mem_ops->exec_op)
2502bdf3a3b5SBoris Brezillon 			return -EINVAL;
2503b5932f5cSBoris Brezillon 	} else if (!ctlr->transfer && !ctlr->transfer_one &&
2504b5932f5cSBoris Brezillon 		   !ctlr->transfer_one_message) {
2505b5932f5cSBoris Brezillon 		return -EINVAL;
2506b5932f5cSBoris Brezillon 	}
2507bdf3a3b5SBoris Brezillon 
2508bdf3a3b5SBoris Brezillon 	return 0;
2509bdf3a3b5SBoris Brezillon }
2510bdf3a3b5SBoris Brezillon 
25118ae12a0dSDavid Brownell /**
25128caab75fSGeert Uytterhoeven  * spi_register_controller - register SPI master or slave controller
25138caab75fSGeert Uytterhoeven  * @ctlr: initialized master, originally from spi_alloc_master() or
25148caab75fSGeert Uytterhoeven  *	spi_alloc_slave()
251533e34dc6SDavid Brownell  * Context: can sleep
25168ae12a0dSDavid Brownell  *
25178caab75fSGeert Uytterhoeven  * SPI controllers connect to their drivers using some non-SPI bus,
25188ae12a0dSDavid Brownell  * such as the platform bus.  The final stage of probe() in that code
25198caab75fSGeert Uytterhoeven  * includes calling spi_register_controller() to hook up to this SPI bus glue.
25208ae12a0dSDavid Brownell  *
25218ae12a0dSDavid Brownell  * SPI controllers use board specific (often SOC specific) bus numbers,
25228ae12a0dSDavid Brownell  * and board-specific addressing for SPI devices combines those numbers
25238ae12a0dSDavid Brownell  * with chip select numbers.  Since SPI does not directly support dynamic
25248ae12a0dSDavid Brownell  * device identification, boards need configuration tables telling which
25258ae12a0dSDavid Brownell  * chip is at which address.
25268ae12a0dSDavid Brownell  *
25278ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns zero on
25288caab75fSGeert Uytterhoeven  * success, else a negative error code (dropping the controller's refcount).
25290c868461SDavid Brownell  * After a successful return, the caller is responsible for calling
25308caab75fSGeert Uytterhoeven  * spi_unregister_controller().
253197d56dc6SJavier Martinez Canillas  *
253297d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
25338ae12a0dSDavid Brownell  */
25348caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr)
25358ae12a0dSDavid Brownell {
25368caab75fSGeert Uytterhoeven 	struct device		*dev = ctlr->dev.parent;
25372b9603a0SFeng Tang 	struct boardinfo	*bi;
2538b93318a2SSergei Shtylyov 	int			status;
253942bdd706SLucas Stach 	int			id, first_dynamic;
25408ae12a0dSDavid Brownell 
25410c868461SDavid Brownell 	if (!dev)
25420c868461SDavid Brownell 		return -ENODEV;
25430c868461SDavid Brownell 
2544bdf3a3b5SBoris Brezillon 	/*
2545bdf3a3b5SBoris Brezillon 	 * Make sure all necessary hooks are implemented before registering
2546bdf3a3b5SBoris Brezillon 	 * the SPI controller.
2547bdf3a3b5SBoris Brezillon 	 */
2548bdf3a3b5SBoris Brezillon 	status = spi_controller_check_ops(ctlr);
2549bdf3a3b5SBoris Brezillon 	if (status)
2550bdf3a3b5SBoris Brezillon 		return status;
2551bdf3a3b5SBoris Brezillon 
255204b2d03aSGeert Uytterhoeven 	if (ctlr->bus_num >= 0) {
255304b2d03aSGeert Uytterhoeven 		/* devices with a fixed bus num must check-in with the num */
255404b2d03aSGeert Uytterhoeven 		mutex_lock(&board_lock);
255504b2d03aSGeert Uytterhoeven 		id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
255604b2d03aSGeert Uytterhoeven 			ctlr->bus_num + 1, GFP_KERNEL);
255704b2d03aSGeert Uytterhoeven 		mutex_unlock(&board_lock);
255804b2d03aSGeert Uytterhoeven 		if (WARN(id < 0, "couldn't get idr"))
255904b2d03aSGeert Uytterhoeven 			return id == -ENOSPC ? -EBUSY : id;
256004b2d03aSGeert Uytterhoeven 		ctlr->bus_num = id;
256104b2d03aSGeert Uytterhoeven 	} else if (ctlr->dev.of_node) {
25629b61e302SSuniel Mahesh 		/* allocate dynamic bus number using Linux idr */
25639b61e302SSuniel Mahesh 		id = of_alias_get_id(ctlr->dev.of_node, "spi");
25649b61e302SSuniel Mahesh 		if (id >= 0) {
25659b61e302SSuniel Mahesh 			ctlr->bus_num = id;
25669b61e302SSuniel Mahesh 			mutex_lock(&board_lock);
25679b61e302SSuniel Mahesh 			id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
25689b61e302SSuniel Mahesh 				       ctlr->bus_num + 1, GFP_KERNEL);
25699b61e302SSuniel Mahesh 			mutex_unlock(&board_lock);
25709b61e302SSuniel Mahesh 			if (WARN(id < 0, "couldn't get idr"))
25719b61e302SSuniel Mahesh 				return id == -ENOSPC ? -EBUSY : id;
25729b61e302SSuniel Mahesh 		}
25739b61e302SSuniel Mahesh 	}
25748caab75fSGeert Uytterhoeven 	if (ctlr->bus_num < 0) {
257542bdd706SLucas Stach 		first_dynamic = of_alias_get_highest_id("spi");
257642bdd706SLucas Stach 		if (first_dynamic < 0)
257742bdd706SLucas Stach 			first_dynamic = 0;
257842bdd706SLucas Stach 		else
257942bdd706SLucas Stach 			first_dynamic++;
258042bdd706SLucas Stach 
25819b61e302SSuniel Mahesh 		mutex_lock(&board_lock);
258242bdd706SLucas Stach 		id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
258342bdd706SLucas Stach 			       0, GFP_KERNEL);
25849b61e302SSuniel Mahesh 		mutex_unlock(&board_lock);
25859b61e302SSuniel Mahesh 		if (WARN(id < 0, "couldn't get idr"))
25869b61e302SSuniel Mahesh 			return id;
25879b61e302SSuniel Mahesh 		ctlr->bus_num = id;
25888ae12a0dSDavid Brownell 	}
25898caab75fSGeert Uytterhoeven 	INIT_LIST_HEAD(&ctlr->queue);
25908caab75fSGeert Uytterhoeven 	spin_lock_init(&ctlr->queue_lock);
25918caab75fSGeert Uytterhoeven 	spin_lock_init(&ctlr->bus_lock_spinlock);
25928caab75fSGeert Uytterhoeven 	mutex_init(&ctlr->bus_lock_mutex);
25938caab75fSGeert Uytterhoeven 	mutex_init(&ctlr->io_mutex);
25948caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 0;
25958caab75fSGeert Uytterhoeven 	init_completion(&ctlr->xfer_completion);
25968caab75fSGeert Uytterhoeven 	if (!ctlr->max_dma_len)
25978caab75fSGeert Uytterhoeven 		ctlr->max_dma_len = INT_MAX;
2598cf32b71eSErnst Schwab 
25998ae12a0dSDavid Brownell 	/* register the device, then userspace will see it.
26008ae12a0dSDavid Brownell 	 * registration fails if the bus ID is in use.
26018ae12a0dSDavid Brownell 	 */
26028caab75fSGeert Uytterhoeven 	dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
26030a919ae4SAndrey Smirnov 
26040a919ae4SAndrey Smirnov 	if (!spi_controller_is_slave(ctlr)) {
26050a919ae4SAndrey Smirnov 		if (ctlr->use_gpio_descriptors) {
26060a919ae4SAndrey Smirnov 			status = spi_get_gpio_descs(ctlr);
26070a919ae4SAndrey Smirnov 			if (status)
26080a919ae4SAndrey Smirnov 				return status;
26090a919ae4SAndrey Smirnov 			/*
26100a919ae4SAndrey Smirnov 			 * A controller using GPIO descriptors always
26110a919ae4SAndrey Smirnov 			 * supports SPI_CS_HIGH if need be.
26120a919ae4SAndrey Smirnov 			 */
26130a919ae4SAndrey Smirnov 			ctlr->mode_bits |= SPI_CS_HIGH;
26140a919ae4SAndrey Smirnov 		} else {
26150a919ae4SAndrey Smirnov 			/* Legacy code path for GPIOs from DT */
261643004f31SLinus Walleij 			status = of_spi_get_gpio_numbers(ctlr);
26170a919ae4SAndrey Smirnov 			if (status)
26180a919ae4SAndrey Smirnov 				return status;
26190a919ae4SAndrey Smirnov 		}
26200a919ae4SAndrey Smirnov 	}
26210a919ae4SAndrey Smirnov 
2622f9481b08STudor Ambarus 	/*
2623f9481b08STudor Ambarus 	 * Even if it's just one always-selected device, there must
2624f9481b08STudor Ambarus 	 * be at least one chipselect.
2625f9481b08STudor Ambarus 	 */
2626f9481b08STudor Ambarus 	if (!ctlr->num_chipselect)
2627f9481b08STudor Ambarus 		return -EINVAL;
2628f9481b08STudor Ambarus 
26298caab75fSGeert Uytterhoeven 	status = device_add(&ctlr->dev);
26309b61e302SSuniel Mahesh 	if (status < 0) {
26319b61e302SSuniel Mahesh 		/* free bus id */
26329b61e302SSuniel Mahesh 		mutex_lock(&board_lock);
26339b61e302SSuniel Mahesh 		idr_remove(&spi_master_idr, ctlr->bus_num);
26349b61e302SSuniel Mahesh 		mutex_unlock(&board_lock);
26358ae12a0dSDavid Brownell 		goto done;
26369b61e302SSuniel Mahesh 	}
26379b61e302SSuniel Mahesh 	dev_dbg(dev, "registered %s %s\n",
26388caab75fSGeert Uytterhoeven 			spi_controller_is_slave(ctlr) ? "slave" : "master",
26399b61e302SSuniel Mahesh 			dev_name(&ctlr->dev));
26408ae12a0dSDavid Brownell 
2641b5932f5cSBoris Brezillon 	/*
2642b5932f5cSBoris Brezillon 	 * If we're using a queued driver, start the queue. Note that we don't
2643b5932f5cSBoris Brezillon 	 * need the queueing logic if the driver is only supporting high-level
2644b5932f5cSBoris Brezillon 	 * memory operations.
2645b5932f5cSBoris Brezillon 	 */
2646b5932f5cSBoris Brezillon 	if (ctlr->transfer) {
26478caab75fSGeert Uytterhoeven 		dev_info(dev, "controller is unqueued, this is deprecated\n");
2648b5932f5cSBoris Brezillon 	} else if (ctlr->transfer_one || ctlr->transfer_one_message) {
26498caab75fSGeert Uytterhoeven 		status = spi_controller_initialize_queue(ctlr);
2650ffbbdd21SLinus Walleij 		if (status) {
26518caab75fSGeert Uytterhoeven 			device_del(&ctlr->dev);
26529b61e302SSuniel Mahesh 			/* free bus id */
26539b61e302SSuniel Mahesh 			mutex_lock(&board_lock);
26549b61e302SSuniel Mahesh 			idr_remove(&spi_master_idr, ctlr->bus_num);
26559b61e302SSuniel Mahesh 			mutex_unlock(&board_lock);
2656ffbbdd21SLinus Walleij 			goto done;
2657ffbbdd21SLinus Walleij 		}
2658ffbbdd21SLinus Walleij 	}
2659eca2ebc7SMartin Sperl 	/* add statistics */
26608caab75fSGeert Uytterhoeven 	spin_lock_init(&ctlr->statistics.lock);
2661ffbbdd21SLinus Walleij 
26622b9603a0SFeng Tang 	mutex_lock(&board_lock);
26638caab75fSGeert Uytterhoeven 	list_add_tail(&ctlr->list, &spi_controller_list);
26642b9603a0SFeng Tang 	list_for_each_entry(bi, &board_list, list)
26658caab75fSGeert Uytterhoeven 		spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
26662b9603a0SFeng Tang 	mutex_unlock(&board_lock);
26672b9603a0SFeng Tang 
266864bee4d2SMika Westerberg 	/* Register devices from the device tree and ACPI */
26698caab75fSGeert Uytterhoeven 	of_register_spi_devices(ctlr);
26708caab75fSGeert Uytterhoeven 	acpi_register_spi_devices(ctlr);
26718ae12a0dSDavid Brownell done:
26728ae12a0dSDavid Brownell 	return status;
26738ae12a0dSDavid Brownell }
26748caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller);
26758ae12a0dSDavid Brownell 
2676666d5b4cSMark Brown static void devm_spi_unregister(struct device *dev, void *res)
2677666d5b4cSMark Brown {
26788caab75fSGeert Uytterhoeven 	spi_unregister_controller(*(struct spi_controller **)res);
2679666d5b4cSMark Brown }
2680666d5b4cSMark Brown 
2681666d5b4cSMark Brown /**
26828caab75fSGeert Uytterhoeven  * devm_spi_register_controller - register managed SPI master or slave
26838caab75fSGeert Uytterhoeven  *	controller
26848caab75fSGeert Uytterhoeven  * @dev:    device managing SPI controller
26858caab75fSGeert Uytterhoeven  * @ctlr: initialized controller, originally from spi_alloc_master() or
26868caab75fSGeert Uytterhoeven  *	spi_alloc_slave()
2687666d5b4cSMark Brown  * Context: can sleep
2688666d5b4cSMark Brown  *
26898caab75fSGeert Uytterhoeven  * Register a SPI device as with spi_register_controller() which will
269068b892f1SJohan Hovold  * automatically be unregistered and freed.
269197d56dc6SJavier Martinez Canillas  *
269297d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
2693666d5b4cSMark Brown  */
26948caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev,
26958caab75fSGeert Uytterhoeven 				 struct spi_controller *ctlr)
2696666d5b4cSMark Brown {
26978caab75fSGeert Uytterhoeven 	struct spi_controller **ptr;
2698666d5b4cSMark Brown 	int ret;
2699666d5b4cSMark Brown 
2700666d5b4cSMark Brown 	ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2701666d5b4cSMark Brown 	if (!ptr)
2702666d5b4cSMark Brown 		return -ENOMEM;
2703666d5b4cSMark Brown 
27048caab75fSGeert Uytterhoeven 	ret = spi_register_controller(ctlr);
27054b92894eSStephen Warren 	if (!ret) {
27068caab75fSGeert Uytterhoeven 		*ptr = ctlr;
2707666d5b4cSMark Brown 		devres_add(dev, ptr);
2708666d5b4cSMark Brown 	} else {
2709666d5b4cSMark Brown 		devres_free(ptr);
2710666d5b4cSMark Brown 	}
2711666d5b4cSMark Brown 
2712666d5b4cSMark Brown 	return ret;
2713666d5b4cSMark Brown }
27148caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller);
2715666d5b4cSMark Brown 
271634860089SDavid Lamparter static int __unregister(struct device *dev, void *null)
27178ae12a0dSDavid Brownell {
27180c868461SDavid Brownell 	spi_unregister_device(to_spi_device(dev));
27198ae12a0dSDavid Brownell 	return 0;
27208ae12a0dSDavid Brownell }
27218ae12a0dSDavid Brownell 
27228ae12a0dSDavid Brownell /**
27238caab75fSGeert Uytterhoeven  * spi_unregister_controller - unregister SPI master or slave controller
27248caab75fSGeert Uytterhoeven  * @ctlr: the controller being unregistered
272533e34dc6SDavid Brownell  * Context: can sleep
27268ae12a0dSDavid Brownell  *
27278caab75fSGeert Uytterhoeven  * This call is used only by SPI controller drivers, which are the
27288ae12a0dSDavid Brownell  * only ones directly touching chip registers.
27298ae12a0dSDavid Brownell  *
27308ae12a0dSDavid Brownell  * This must be called from context that can sleep.
273168b892f1SJohan Hovold  *
273268b892f1SJohan Hovold  * Note that this function also drops a reference to the controller.
27338ae12a0dSDavid Brownell  */
27348caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr)
27358ae12a0dSDavid Brownell {
27369b61e302SSuniel Mahesh 	struct spi_controller *found;
273767f7b278SJohan Hovold 	int id = ctlr->bus_num;
273889fc9a1aSJeff Garzik 
27399b61e302SSuniel Mahesh 	/* First make sure that this controller was ever added */
27409b61e302SSuniel Mahesh 	mutex_lock(&board_lock);
274167f7b278SJohan Hovold 	found = idr_find(&spi_master_idr, id);
27429b61e302SSuniel Mahesh 	mutex_unlock(&board_lock);
27438caab75fSGeert Uytterhoeven 	if (ctlr->queued) {
27448caab75fSGeert Uytterhoeven 		if (spi_destroy_queue(ctlr))
27458caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "queue remove failed\n");
2746ffbbdd21SLinus Walleij 	}
27472b9603a0SFeng Tang 	mutex_lock(&board_lock);
27488caab75fSGeert Uytterhoeven 	list_del(&ctlr->list);
27492b9603a0SFeng Tang 	mutex_unlock(&board_lock);
27502b9603a0SFeng Tang 
2751ebc37af5SAndy Shevchenko 	device_for_each_child(&ctlr->dev, NULL, __unregister);
27528caab75fSGeert Uytterhoeven 	device_unregister(&ctlr->dev);
27539b61e302SSuniel Mahesh 	/* free bus id */
27549b61e302SSuniel Mahesh 	mutex_lock(&board_lock);
2755613bd1eaSJarkko Nikula 	if (found == ctlr)
275667f7b278SJohan Hovold 		idr_remove(&spi_master_idr, id);
27579b61e302SSuniel Mahesh 	mutex_unlock(&board_lock);
27588ae12a0dSDavid Brownell }
27598caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller);
27608ae12a0dSDavid Brownell 
27618caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr)
2762ffbbdd21SLinus Walleij {
2763ffbbdd21SLinus Walleij 	int ret;
2764ffbbdd21SLinus Walleij 
27658caab75fSGeert Uytterhoeven 	/* Basically no-ops for non-queued controllers */
27668caab75fSGeert Uytterhoeven 	if (!ctlr->queued)
2767ffbbdd21SLinus Walleij 		return 0;
2768ffbbdd21SLinus Walleij 
27698caab75fSGeert Uytterhoeven 	ret = spi_stop_queue(ctlr);
2770ffbbdd21SLinus Walleij 	if (ret)
27718caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "queue stop failed\n");
2772ffbbdd21SLinus Walleij 
2773ffbbdd21SLinus Walleij 	return ret;
2774ffbbdd21SLinus Walleij }
27758caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend);
2776ffbbdd21SLinus Walleij 
27778caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr)
2778ffbbdd21SLinus Walleij {
2779ffbbdd21SLinus Walleij 	int ret;
2780ffbbdd21SLinus Walleij 
27818caab75fSGeert Uytterhoeven 	if (!ctlr->queued)
2782ffbbdd21SLinus Walleij 		return 0;
2783ffbbdd21SLinus Walleij 
27848caab75fSGeert Uytterhoeven 	ret = spi_start_queue(ctlr);
2785ffbbdd21SLinus Walleij 	if (ret)
27868caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "queue restart failed\n");
2787ffbbdd21SLinus Walleij 
2788ffbbdd21SLinus Walleij 	return ret;
2789ffbbdd21SLinus Walleij }
27908caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume);
2791ffbbdd21SLinus Walleij 
27928caab75fSGeert Uytterhoeven static int __spi_controller_match(struct device *dev, const void *data)
27935ed2c832SDave Young {
27948caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
27959f3b795aSMichał Mirosław 	const u16 *bus_num = data;
27965ed2c832SDave Young 
27978caab75fSGeert Uytterhoeven 	ctlr = container_of(dev, struct spi_controller, dev);
27988caab75fSGeert Uytterhoeven 	return ctlr->bus_num == *bus_num;
27995ed2c832SDave Young }
28005ed2c832SDave Young 
28018ae12a0dSDavid Brownell /**
28028ae12a0dSDavid Brownell  * spi_busnum_to_master - look up master associated with bus_num
28038ae12a0dSDavid Brownell  * @bus_num: the master's bus number
280433e34dc6SDavid Brownell  * Context: can sleep
28058ae12a0dSDavid Brownell  *
28068ae12a0dSDavid Brownell  * This call may be used with devices that are registered after
28078ae12a0dSDavid Brownell  * arch init time.  It returns a refcounted pointer to the relevant
28088caab75fSGeert Uytterhoeven  * spi_controller (which the caller must release), or NULL if there is
28098ae12a0dSDavid Brownell  * no such master registered.
281097d56dc6SJavier Martinez Canillas  *
281197d56dc6SJavier Martinez Canillas  * Return: the SPI master structure on success, else NULL.
28128ae12a0dSDavid Brownell  */
28138caab75fSGeert Uytterhoeven struct spi_controller *spi_busnum_to_master(u16 bus_num)
28148ae12a0dSDavid Brownell {
281549dce689STony Jones 	struct device		*dev;
28168caab75fSGeert Uytterhoeven 	struct spi_controller	*ctlr = NULL;
28178ae12a0dSDavid Brownell 
2818695794aeSGreg Kroah-Hartman 	dev = class_find_device(&spi_master_class, NULL, &bus_num,
28198caab75fSGeert Uytterhoeven 				__spi_controller_match);
28205ed2c832SDave Young 	if (dev)
28218caab75fSGeert Uytterhoeven 		ctlr = container_of(dev, struct spi_controller, dev);
28225ed2c832SDave Young 	/* reference got in class_find_device */
28238caab75fSGeert Uytterhoeven 	return ctlr;
28248ae12a0dSDavid Brownell }
28258ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master);
28268ae12a0dSDavid Brownell 
2827d780c371SMartin Sperl /*-------------------------------------------------------------------------*/
2828d780c371SMartin Sperl 
2829d780c371SMartin Sperl /* Core methods for SPI resource management */
2830d780c371SMartin Sperl 
2831d780c371SMartin Sperl /**
2832d780c371SMartin Sperl  * spi_res_alloc - allocate a spi resource that is life-cycle managed
2833d780c371SMartin Sperl  *                 during the processing of a spi_message while using
2834d780c371SMartin Sperl  *                 spi_transfer_one
2835d780c371SMartin Sperl  * @spi:     the spi device for which we allocate memory
2836d780c371SMartin Sperl  * @release: the release code to execute for this resource
2837d780c371SMartin Sperl  * @size:    size to alloc and return
2838d780c371SMartin Sperl  * @gfp:     GFP allocation flags
2839d780c371SMartin Sperl  *
2840d780c371SMartin Sperl  * Return: the pointer to the allocated data
2841d780c371SMartin Sperl  *
2842d780c371SMartin Sperl  * This may get enhanced in the future to allocate from a memory pool
28438caab75fSGeert Uytterhoeven  * of the @spi_device or @spi_controller to avoid repeated allocations.
2844d780c371SMartin Sperl  */
2845d780c371SMartin Sperl void *spi_res_alloc(struct spi_device *spi,
2846d780c371SMartin Sperl 		    spi_res_release_t release,
2847d780c371SMartin Sperl 		    size_t size, gfp_t gfp)
2848d780c371SMartin Sperl {
2849d780c371SMartin Sperl 	struct spi_res *sres;
2850d780c371SMartin Sperl 
2851d780c371SMartin Sperl 	sres = kzalloc(sizeof(*sres) + size, gfp);
2852d780c371SMartin Sperl 	if (!sres)
2853d780c371SMartin Sperl 		return NULL;
2854d780c371SMartin Sperl 
2855d780c371SMartin Sperl 	INIT_LIST_HEAD(&sres->entry);
2856d780c371SMartin Sperl 	sres->release = release;
2857d780c371SMartin Sperl 
2858d780c371SMartin Sperl 	return sres->data;
2859d780c371SMartin Sperl }
2860d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_alloc);
2861d780c371SMartin Sperl 
2862d780c371SMartin Sperl /**
2863d780c371SMartin Sperl  * spi_res_free - free an spi resource
2864d780c371SMartin Sperl  * @res: pointer to the custom data of a resource
2865d780c371SMartin Sperl  *
2866d780c371SMartin Sperl  */
2867d780c371SMartin Sperl void spi_res_free(void *res)
2868d780c371SMartin Sperl {
2869d780c371SMartin Sperl 	struct spi_res *sres = container_of(res, struct spi_res, data);
2870d780c371SMartin Sperl 
2871d780c371SMartin Sperl 	if (!res)
2872d780c371SMartin Sperl 		return;
2873d780c371SMartin Sperl 
2874d780c371SMartin Sperl 	WARN_ON(!list_empty(&sres->entry));
2875d780c371SMartin Sperl 	kfree(sres);
2876d780c371SMartin Sperl }
2877d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_free);
2878d780c371SMartin Sperl 
2879d780c371SMartin Sperl /**
2880d780c371SMartin Sperl  * spi_res_add - add a spi_res to the spi_message
2881d780c371SMartin Sperl  * @message: the spi message
2882d780c371SMartin Sperl  * @res:     the spi_resource
2883d780c371SMartin Sperl  */
2884d780c371SMartin Sperl void spi_res_add(struct spi_message *message, void *res)
2885d780c371SMartin Sperl {
2886d780c371SMartin Sperl 	struct spi_res *sres = container_of(res, struct spi_res, data);
2887d780c371SMartin Sperl 
2888d780c371SMartin Sperl 	WARN_ON(!list_empty(&sres->entry));
2889d780c371SMartin Sperl 	list_add_tail(&sres->entry, &message->resources);
2890d780c371SMartin Sperl }
2891d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_add);
2892d780c371SMartin Sperl 
2893d780c371SMartin Sperl /**
2894d780c371SMartin Sperl  * spi_res_release - release all spi resources for this message
28958caab75fSGeert Uytterhoeven  * @ctlr:  the @spi_controller
2896d780c371SMartin Sperl  * @message: the @spi_message
2897d780c371SMartin Sperl  */
28988caab75fSGeert Uytterhoeven void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
2899d780c371SMartin Sperl {
2900f5694369SVladimir Zapolskiy 	struct spi_res *res, *tmp;
2901d780c371SMartin Sperl 
2902f5694369SVladimir Zapolskiy 	list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
2903d780c371SMartin Sperl 		if (res->release)
29048caab75fSGeert Uytterhoeven 			res->release(ctlr, message, res->data);
2905d780c371SMartin Sperl 
2906d780c371SMartin Sperl 		list_del(&res->entry);
2907d780c371SMartin Sperl 
2908d780c371SMartin Sperl 		kfree(res);
2909d780c371SMartin Sperl 	}
2910d780c371SMartin Sperl }
2911d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_release);
29128ae12a0dSDavid Brownell 
29138ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
29148ae12a0dSDavid Brownell 
2915523baf5aSMartin Sperl /* Core methods for spi_message alterations */
2916523baf5aSMartin Sperl 
29178caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr,
2918523baf5aSMartin Sperl 					    struct spi_message *msg,
2919523baf5aSMartin Sperl 					    void *res)
2920523baf5aSMartin Sperl {
2921523baf5aSMartin Sperl 	struct spi_replaced_transfers *rxfer = res;
2922523baf5aSMartin Sperl 	size_t i;
2923523baf5aSMartin Sperl 
2924523baf5aSMartin Sperl 	/* call extra callback if requested */
2925523baf5aSMartin Sperl 	if (rxfer->release)
29268caab75fSGeert Uytterhoeven 		rxfer->release(ctlr, msg, res);
2927523baf5aSMartin Sperl 
2928523baf5aSMartin Sperl 	/* insert replaced transfers back into the message */
2929523baf5aSMartin Sperl 	list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2930523baf5aSMartin Sperl 
2931523baf5aSMartin Sperl 	/* remove the formerly inserted entries */
2932523baf5aSMartin Sperl 	for (i = 0; i < rxfer->inserted; i++)
2933523baf5aSMartin Sperl 		list_del(&rxfer->inserted_transfers[i].transfer_list);
2934523baf5aSMartin Sperl }
2935523baf5aSMartin Sperl 
2936523baf5aSMartin Sperl /**
2937523baf5aSMartin Sperl  * spi_replace_transfers - replace transfers with several transfers
2938523baf5aSMartin Sperl  *                         and register change with spi_message.resources
2939523baf5aSMartin Sperl  * @msg:           the spi_message we work upon
2940523baf5aSMartin Sperl  * @xfer_first:    the first spi_transfer we want to replace
2941523baf5aSMartin Sperl  * @remove:        number of transfers to remove
2942523baf5aSMartin Sperl  * @insert:        the number of transfers we want to insert instead
2943523baf5aSMartin Sperl  * @release:       extra release code necessary in some circumstances
2944523baf5aSMartin Sperl  * @extradatasize: extra data to allocate (with alignment guarantees
2945523baf5aSMartin Sperl  *                 of struct @spi_transfer)
294605885397SMartin Sperl  * @gfp:           gfp flags
2947523baf5aSMartin Sperl  *
2948523baf5aSMartin Sperl  * Returns: pointer to @spi_replaced_transfers,
2949523baf5aSMartin Sperl  *          PTR_ERR(...) in case of errors.
2950523baf5aSMartin Sperl  */
2951523baf5aSMartin Sperl struct spi_replaced_transfers *spi_replace_transfers(
2952523baf5aSMartin Sperl 	struct spi_message *msg,
2953523baf5aSMartin Sperl 	struct spi_transfer *xfer_first,
2954523baf5aSMartin Sperl 	size_t remove,
2955523baf5aSMartin Sperl 	size_t insert,
2956523baf5aSMartin Sperl 	spi_replaced_release_t release,
2957523baf5aSMartin Sperl 	size_t extradatasize,
2958523baf5aSMartin Sperl 	gfp_t gfp)
2959523baf5aSMartin Sperl {
2960523baf5aSMartin Sperl 	struct spi_replaced_transfers *rxfer;
2961523baf5aSMartin Sperl 	struct spi_transfer *xfer;
2962523baf5aSMartin Sperl 	size_t i;
2963523baf5aSMartin Sperl 
2964523baf5aSMartin Sperl 	/* allocate the structure using spi_res */
2965523baf5aSMartin Sperl 	rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2966aef97522SGustavo A. R. Silva 			      struct_size(rxfer, inserted_transfers, insert)
2967523baf5aSMartin Sperl 			      + extradatasize,
2968523baf5aSMartin Sperl 			      gfp);
2969523baf5aSMartin Sperl 	if (!rxfer)
2970523baf5aSMartin Sperl 		return ERR_PTR(-ENOMEM);
2971523baf5aSMartin Sperl 
2972523baf5aSMartin Sperl 	/* the release code to invoke before running the generic release */
2973523baf5aSMartin Sperl 	rxfer->release = release;
2974523baf5aSMartin Sperl 
2975523baf5aSMartin Sperl 	/* assign extradata */
2976523baf5aSMartin Sperl 	if (extradatasize)
2977523baf5aSMartin Sperl 		rxfer->extradata =
2978523baf5aSMartin Sperl 			&rxfer->inserted_transfers[insert];
2979523baf5aSMartin Sperl 
2980523baf5aSMartin Sperl 	/* init the replaced_transfers list */
2981523baf5aSMartin Sperl 	INIT_LIST_HEAD(&rxfer->replaced_transfers);
2982523baf5aSMartin Sperl 
2983523baf5aSMartin Sperl 	/* assign the list_entry after which we should reinsert
2984523baf5aSMartin Sperl 	 * the @replaced_transfers - it may be spi_message.messages!
2985523baf5aSMartin Sperl 	 */
2986523baf5aSMartin Sperl 	rxfer->replaced_after = xfer_first->transfer_list.prev;
2987523baf5aSMartin Sperl 
2988523baf5aSMartin Sperl 	/* remove the requested number of transfers */
2989523baf5aSMartin Sperl 	for (i = 0; i < remove; i++) {
2990523baf5aSMartin Sperl 		/* if the entry after replaced_after it is msg->transfers
2991523baf5aSMartin Sperl 		 * then we have been requested to remove more transfers
2992523baf5aSMartin Sperl 		 * than are in the list
2993523baf5aSMartin Sperl 		 */
2994523baf5aSMartin Sperl 		if (rxfer->replaced_after->next == &msg->transfers) {
2995523baf5aSMartin Sperl 			dev_err(&msg->spi->dev,
2996523baf5aSMartin Sperl 				"requested to remove more spi_transfers than are available\n");
2997523baf5aSMartin Sperl 			/* insert replaced transfers back into the message */
2998523baf5aSMartin Sperl 			list_splice(&rxfer->replaced_transfers,
2999523baf5aSMartin Sperl 				    rxfer->replaced_after);
3000523baf5aSMartin Sperl 
3001523baf5aSMartin Sperl 			/* free the spi_replace_transfer structure */
3002523baf5aSMartin Sperl 			spi_res_free(rxfer);
3003523baf5aSMartin Sperl 
3004523baf5aSMartin Sperl 			/* and return with an error */
3005523baf5aSMartin Sperl 			return ERR_PTR(-EINVAL);
3006523baf5aSMartin Sperl 		}
3007523baf5aSMartin Sperl 
3008523baf5aSMartin Sperl 		/* remove the entry after replaced_after from list of
3009523baf5aSMartin Sperl 		 * transfers and add it to list of replaced_transfers
3010523baf5aSMartin Sperl 		 */
3011523baf5aSMartin Sperl 		list_move_tail(rxfer->replaced_after->next,
3012523baf5aSMartin Sperl 			       &rxfer->replaced_transfers);
3013523baf5aSMartin Sperl 	}
3014523baf5aSMartin Sperl 
3015523baf5aSMartin Sperl 	/* create copy of the given xfer with identical settings
3016523baf5aSMartin Sperl 	 * based on the first transfer to get removed
3017523baf5aSMartin Sperl 	 */
3018523baf5aSMartin Sperl 	for (i = 0; i < insert; i++) {
3019523baf5aSMartin Sperl 		/* we need to run in reverse order */
3020523baf5aSMartin Sperl 		xfer = &rxfer->inserted_transfers[insert - 1 - i];
3021523baf5aSMartin Sperl 
3022523baf5aSMartin Sperl 		/* copy all spi_transfer data */
3023523baf5aSMartin Sperl 		memcpy(xfer, xfer_first, sizeof(*xfer));
3024523baf5aSMartin Sperl 
3025523baf5aSMartin Sperl 		/* add to list */
3026523baf5aSMartin Sperl 		list_add(&xfer->transfer_list, rxfer->replaced_after);
3027523baf5aSMartin Sperl 
3028bebcfd27SAlexandru Ardelean 		/* clear cs_change and delay for all but the last */
3029523baf5aSMartin Sperl 		if (i) {
3030523baf5aSMartin Sperl 			xfer->cs_change = false;
3031523baf5aSMartin Sperl 			xfer->delay_usecs = 0;
3032bebcfd27SAlexandru Ardelean 			xfer->delay.value = 0;
3033523baf5aSMartin Sperl 		}
3034523baf5aSMartin Sperl 	}
3035523baf5aSMartin Sperl 
3036523baf5aSMartin Sperl 	/* set up inserted */
3037523baf5aSMartin Sperl 	rxfer->inserted = insert;
3038523baf5aSMartin Sperl 
3039523baf5aSMartin Sperl 	/* and register it with spi_res/spi_message */
3040523baf5aSMartin Sperl 	spi_res_add(msg, rxfer);
3041523baf5aSMartin Sperl 
3042523baf5aSMartin Sperl 	return rxfer;
3043523baf5aSMartin Sperl }
3044523baf5aSMartin Sperl EXPORT_SYMBOL_GPL(spi_replace_transfers);
3045523baf5aSMartin Sperl 
30468caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
3047d9f12122SMartin Sperl 					struct spi_message *msg,
3048d9f12122SMartin Sperl 					struct spi_transfer **xferp,
3049d9f12122SMartin Sperl 					size_t maxsize,
3050d9f12122SMartin Sperl 					gfp_t gfp)
3051d9f12122SMartin Sperl {
3052d9f12122SMartin Sperl 	struct spi_transfer *xfer = *xferp, *xfers;
3053d9f12122SMartin Sperl 	struct spi_replaced_transfers *srt;
3054d9f12122SMartin Sperl 	size_t offset;
3055d9f12122SMartin Sperl 	size_t count, i;
3056d9f12122SMartin Sperl 
3057d9f12122SMartin Sperl 	/* calculate how many we have to replace */
3058d9f12122SMartin Sperl 	count = DIV_ROUND_UP(xfer->len, maxsize);
3059d9f12122SMartin Sperl 
3060d9f12122SMartin Sperl 	/* create replacement */
3061d9f12122SMartin Sperl 	srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
3062657d32efSDan Carpenter 	if (IS_ERR(srt))
3063657d32efSDan Carpenter 		return PTR_ERR(srt);
3064d9f12122SMartin Sperl 	xfers = srt->inserted_transfers;
3065d9f12122SMartin Sperl 
3066d9f12122SMartin Sperl 	/* now handle each of those newly inserted spi_transfers
3067d9f12122SMartin Sperl 	 * note that the replacements spi_transfers all are preset
3068d9f12122SMartin Sperl 	 * to the same values as *xferp, so tx_buf, rx_buf and len
3069d9f12122SMartin Sperl 	 * are all identical (as well as most others)
3070d9f12122SMartin Sperl 	 * so we just have to fix up len and the pointers.
3071d9f12122SMartin Sperl 	 *
3072d9f12122SMartin Sperl 	 * this also includes support for the depreciated
3073d9f12122SMartin Sperl 	 * spi_message.is_dma_mapped interface
3074d9f12122SMartin Sperl 	 */
3075d9f12122SMartin Sperl 
3076d9f12122SMartin Sperl 	/* the first transfer just needs the length modified, so we
3077d9f12122SMartin Sperl 	 * run it outside the loop
3078d9f12122SMartin Sperl 	 */
3079c8dab77aSFabio Estevam 	xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
3080d9f12122SMartin Sperl 
3081d9f12122SMartin Sperl 	/* all the others need rx_buf/tx_buf also set */
3082d9f12122SMartin Sperl 	for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3083d9f12122SMartin Sperl 		/* update rx_buf, tx_buf and dma */
3084d9f12122SMartin Sperl 		if (xfers[i].rx_buf)
3085d9f12122SMartin Sperl 			xfers[i].rx_buf += offset;
3086d9f12122SMartin Sperl 		if (xfers[i].rx_dma)
3087d9f12122SMartin Sperl 			xfers[i].rx_dma += offset;
3088d9f12122SMartin Sperl 		if (xfers[i].tx_buf)
3089d9f12122SMartin Sperl 			xfers[i].tx_buf += offset;
3090d9f12122SMartin Sperl 		if (xfers[i].tx_dma)
3091d9f12122SMartin Sperl 			xfers[i].tx_dma += offset;
3092d9f12122SMartin Sperl 
3093d9f12122SMartin Sperl 		/* update length */
3094d9f12122SMartin Sperl 		xfers[i].len = min(maxsize, xfers[i].len - offset);
3095d9f12122SMartin Sperl 	}
3096d9f12122SMartin Sperl 
3097d9f12122SMartin Sperl 	/* we set up xferp to the last entry we have inserted,
3098d9f12122SMartin Sperl 	 * so that we skip those already split transfers
3099d9f12122SMartin Sperl 	 */
3100d9f12122SMartin Sperl 	*xferp = &xfers[count - 1];
3101d9f12122SMartin Sperl 
3102d9f12122SMartin Sperl 	/* increment statistics counters */
31038caab75fSGeert Uytterhoeven 	SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
3104d9f12122SMartin Sperl 				       transfers_split_maxsize);
3105d9f12122SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
3106d9f12122SMartin Sperl 				       transfers_split_maxsize);
3107d9f12122SMartin Sperl 
3108d9f12122SMartin Sperl 	return 0;
3109d9f12122SMartin Sperl }
3110d9f12122SMartin Sperl 
3111d9f12122SMartin Sperl /**
3112d9f12122SMartin Sperl  * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
3113d9f12122SMartin Sperl  *                              when an individual transfer exceeds a
3114d9f12122SMartin Sperl  *                              certain size
31158caab75fSGeert Uytterhoeven  * @ctlr:    the @spi_controller for this transfer
31163700ce95SMasanari Iida  * @msg:   the @spi_message to transform
31173700ce95SMasanari Iida  * @maxsize:  the maximum when to apply this
311810f11a22SJavier Martinez Canillas  * @gfp: GFP allocation flags
3119d9f12122SMartin Sperl  *
3120d9f12122SMartin Sperl  * Return: status of transformation
3121d9f12122SMartin Sperl  */
31228caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr,
3123d9f12122SMartin Sperl 				struct spi_message *msg,
3124d9f12122SMartin Sperl 				size_t maxsize,
3125d9f12122SMartin Sperl 				gfp_t gfp)
3126d9f12122SMartin Sperl {
3127d9f12122SMartin Sperl 	struct spi_transfer *xfer;
3128d9f12122SMartin Sperl 	int ret;
3129d9f12122SMartin Sperl 
3130d9f12122SMartin Sperl 	/* iterate over the transfer_list,
3131d9f12122SMartin Sperl 	 * but note that xfer is advanced to the last transfer inserted
3132d9f12122SMartin Sperl 	 * to avoid checking sizes again unnecessarily (also xfer does
3133d9f12122SMartin Sperl 	 * potentiall belong to a different list by the time the
3134d9f12122SMartin Sperl 	 * replacement has happened
3135d9f12122SMartin Sperl 	 */
3136d9f12122SMartin Sperl 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3137d9f12122SMartin Sperl 		if (xfer->len > maxsize) {
31388caab75fSGeert Uytterhoeven 			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
31398caab75fSGeert Uytterhoeven 							   maxsize, gfp);
3140d9f12122SMartin Sperl 			if (ret)
3141d9f12122SMartin Sperl 				return ret;
3142d9f12122SMartin Sperl 		}
3143d9f12122SMartin Sperl 	}
3144d9f12122SMartin Sperl 
3145d9f12122SMartin Sperl 	return 0;
3146d9f12122SMartin Sperl }
3147d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
31488ae12a0dSDavid Brownell 
31498ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
31508ae12a0dSDavid Brownell 
31518caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers.  Some of the
31527d077197SDavid Brownell  * other core methods are currently defined as inline functions.
31537d077197SDavid Brownell  */
31547d077197SDavid Brownell 
31558caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
31568caab75fSGeert Uytterhoeven 					u8 bits_per_word)
315763ab645fSStefan Brüns {
31588caab75fSGeert Uytterhoeven 	if (ctlr->bits_per_word_mask) {
315963ab645fSStefan Brüns 		/* Only 32 bits fit in the mask */
316063ab645fSStefan Brüns 		if (bits_per_word > 32)
316163ab645fSStefan Brüns 			return -EINVAL;
31628caab75fSGeert Uytterhoeven 		if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
316363ab645fSStefan Brüns 			return -EINVAL;
316463ab645fSStefan Brüns 	}
316563ab645fSStefan Brüns 
316663ab645fSStefan Brüns 	return 0;
316763ab645fSStefan Brüns }
316863ab645fSStefan Brüns 
31697d077197SDavid Brownell /**
31707d077197SDavid Brownell  * spi_setup - setup SPI mode and clock rate
31717d077197SDavid Brownell  * @spi: the device whose settings are being modified
31727d077197SDavid Brownell  * Context: can sleep, and no requests are queued to the device
31737d077197SDavid Brownell  *
31747d077197SDavid Brownell  * SPI protocol drivers may need to update the transfer mode if the
31757d077197SDavid Brownell  * device doesn't work with its default.  They may likewise need
31767d077197SDavid Brownell  * to update clock rates or word sizes from initial values.  This function
31777d077197SDavid Brownell  * changes those settings, and must be called from a context that can sleep.
31787d077197SDavid Brownell  * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
31797d077197SDavid Brownell  * effect the next time the device is selected and data is transferred to
31807d077197SDavid Brownell  * or from it.  When this function returns, the spi device is deselected.
31817d077197SDavid Brownell  *
31827d077197SDavid Brownell  * Note that this call will fail if the protocol driver specifies an option
31837d077197SDavid Brownell  * that the underlying controller or its driver does not support.  For
31847d077197SDavid Brownell  * example, not all hardware supports wire transfers using nine bit words,
31857d077197SDavid Brownell  * LSB-first wire encoding, or active-high chipselects.
318697d56dc6SJavier Martinez Canillas  *
318797d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
31887d077197SDavid Brownell  */
31897d077197SDavid Brownell int spi_setup(struct spi_device *spi)
31907d077197SDavid Brownell {
319183596fbeSGeert Uytterhoeven 	unsigned	bad_bits, ugly_bits;
31925ab8d262SAndy Shevchenko 	int		status;
31937d077197SDavid Brownell 
3194f477b7fbSwangyuhang 	/* check mode to prevent that DUAL and QUAD set at the same time
3195f477b7fbSwangyuhang 	 */
3196f477b7fbSwangyuhang 	if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
3197f477b7fbSwangyuhang 		((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
3198f477b7fbSwangyuhang 		dev_err(&spi->dev,
3199f477b7fbSwangyuhang 		"setup: can not select dual and quad at the same time\n");
3200f477b7fbSwangyuhang 		return -EINVAL;
3201f477b7fbSwangyuhang 	}
3202f477b7fbSwangyuhang 	/* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
3203f477b7fbSwangyuhang 	 */
3204f477b7fbSwangyuhang 	if ((spi->mode & SPI_3WIRE) && (spi->mode &
32056b03061fSYogesh Narayan Gaur 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
32066b03061fSYogesh Narayan Gaur 		 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
3207f477b7fbSwangyuhang 		return -EINVAL;
3208e7db06b5SDavid Brownell 	/* help drivers fail *cleanly* when they need options
32098caab75fSGeert Uytterhoeven 	 * that aren't supported with their current controller
3210cbaa62e0SDavid Lechner 	 * SPI_CS_WORD has a fallback software implementation,
3211cbaa62e0SDavid Lechner 	 * so it is ignored here.
3212e7db06b5SDavid Brownell 	 */
3213cbaa62e0SDavid Lechner 	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
3214d61ad23cSSerge Semin 	/* nothing prevents from working with active-high CS in case if it
3215d61ad23cSSerge Semin 	 * is driven by GPIO.
3216d61ad23cSSerge Semin 	 */
3217d61ad23cSSerge Semin 	if (gpio_is_valid(spi->cs_gpio))
3218d61ad23cSSerge Semin 		bad_bits &= ~SPI_CS_HIGH;
321983596fbeSGeert Uytterhoeven 	ugly_bits = bad_bits &
32206b03061fSYogesh Narayan Gaur 		    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
32216b03061fSYogesh Narayan Gaur 		     SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
322283596fbeSGeert Uytterhoeven 	if (ugly_bits) {
322383596fbeSGeert Uytterhoeven 		dev_warn(&spi->dev,
322483596fbeSGeert Uytterhoeven 			 "setup: ignoring unsupported mode bits %x\n",
322583596fbeSGeert Uytterhoeven 			 ugly_bits);
322683596fbeSGeert Uytterhoeven 		spi->mode &= ~ugly_bits;
322783596fbeSGeert Uytterhoeven 		bad_bits &= ~ugly_bits;
322883596fbeSGeert Uytterhoeven 	}
3229e7db06b5SDavid Brownell 	if (bad_bits) {
3230eb288a1fSLinus Walleij 		dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
3231e7db06b5SDavid Brownell 			bad_bits);
3232e7db06b5SDavid Brownell 		return -EINVAL;
3233e7db06b5SDavid Brownell 	}
3234e7db06b5SDavid Brownell 
32357d077197SDavid Brownell 	if (!spi->bits_per_word)
32367d077197SDavid Brownell 		spi->bits_per_word = 8;
32377d077197SDavid Brownell 
32388caab75fSGeert Uytterhoeven 	status = __spi_validate_bits_per_word(spi->controller,
32398caab75fSGeert Uytterhoeven 					      spi->bits_per_word);
32405ab8d262SAndy Shevchenko 	if (status)
32415ab8d262SAndy Shevchenko 		return status;
324263ab645fSStefan Brüns 
3243052eb2d4SAxel Lin 	if (!spi->max_speed_hz)
32448caab75fSGeert Uytterhoeven 		spi->max_speed_hz = spi->controller->max_speed_hz;
3245052eb2d4SAxel Lin 
32468caab75fSGeert Uytterhoeven 	if (spi->controller->setup)
32478caab75fSGeert Uytterhoeven 		status = spi->controller->setup(spi);
32487d077197SDavid Brownell 
3249abeedb01SFranklin S Cooper Jr 	spi_set_cs(spi, false);
3250abeedb01SFranklin S Cooper Jr 
3251924b5867SDouglas Anderson 	if (spi->rt && !spi->controller->rt) {
3252924b5867SDouglas Anderson 		spi->controller->rt = true;
3253924b5867SDouglas Anderson 		spi_set_thread_rt(spi->controller);
3254924b5867SDouglas Anderson 	}
3255924b5867SDouglas Anderson 
32565fe5f05eSJingoo Han 	dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
32577d077197SDavid Brownell 			(int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
32587d077197SDavid Brownell 			(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
32597d077197SDavid Brownell 			(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
32607d077197SDavid Brownell 			(spi->mode & SPI_3WIRE) ? "3wire, " : "",
32617d077197SDavid Brownell 			(spi->mode & SPI_LOOP) ? "loopback, " : "",
32627d077197SDavid Brownell 			spi->bits_per_word, spi->max_speed_hz,
32637d077197SDavid Brownell 			status);
32647d077197SDavid Brownell 
32657d077197SDavid Brownell 	return status;
32667d077197SDavid Brownell }
32677d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup);
32687d077197SDavid Brownell 
3269f1ca9992SSowjanya Komatineni /**
3270f1ca9992SSowjanya Komatineni  * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3271f1ca9992SSowjanya Komatineni  * @spi: the device that requires specific CS timing configuration
3272*81059366SAlexandru Ardelean  * @setup: CS setup time specified via @spi_delay
3273*81059366SAlexandru Ardelean  * @hold: CS hold time specified via @spi_delay
3274*81059366SAlexandru Ardelean  * @inactive: CS inactive delay between transfers specified via @spi_delay
3275*81059366SAlexandru Ardelean  *
3276*81059366SAlexandru Ardelean  * Return: zero on success, else a negative error code.
3277f1ca9992SSowjanya Komatineni  */
3278*81059366SAlexandru Ardelean int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup,
3279*81059366SAlexandru Ardelean 		      struct spi_delay *hold, struct spi_delay *inactive)
3280f1ca9992SSowjanya Komatineni {
3281f1ca9992SSowjanya Komatineni 	if (spi->controller->set_cs_timing)
3282*81059366SAlexandru Ardelean 		return spi->controller->set_cs_timing(spi, setup, hold,
3283*81059366SAlexandru Ardelean 						      inactive);
3284*81059366SAlexandru Ardelean 	return -ENOTSUPP;
3285f1ca9992SSowjanya Komatineni }
3286f1ca9992SSowjanya Komatineni EXPORT_SYMBOL_GPL(spi_set_cs_timing);
3287f1ca9992SSowjanya Komatineni 
32886c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
32896c613f68SAlexandru Ardelean 				       struct spi_device *spi)
32906c613f68SAlexandru Ardelean {
32916c613f68SAlexandru Ardelean 	int delay1, delay2;
32926c613f68SAlexandru Ardelean 
32936c613f68SAlexandru Ardelean 	delay1 = _spi_delay_to_ns(&xfer->word_delay, xfer);
32946c613f68SAlexandru Ardelean 	if (delay1 < 0)
32956c613f68SAlexandru Ardelean 		return delay1;
32966c613f68SAlexandru Ardelean 
32976c613f68SAlexandru Ardelean 	delay2 = _spi_delay_to_ns(&spi->word_delay, xfer);
32986c613f68SAlexandru Ardelean 	if (delay2 < 0)
32996c613f68SAlexandru Ardelean 		return delay2;
33006c613f68SAlexandru Ardelean 
33016c613f68SAlexandru Ardelean 	if (delay1 < delay2)
33026c613f68SAlexandru Ardelean 		memcpy(&xfer->word_delay, &spi->word_delay,
33036c613f68SAlexandru Ardelean 		       sizeof(xfer->word_delay));
33046c613f68SAlexandru Ardelean 
33056c613f68SAlexandru Ardelean 	return 0;
33066c613f68SAlexandru Ardelean }
33076c613f68SAlexandru Ardelean 
330890808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message)
3309cf32b71eSErnst Schwab {
33108caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
3311e6811d1dSLaxman Dewangan 	struct spi_transfer *xfer;
33126ea31293SAtsushi Nemoto 	int w_size;
3313cf32b71eSErnst Schwab 
331424a0013aSMark Brown 	if (list_empty(&message->transfers))
331524a0013aSMark Brown 		return -EINVAL;
331624a0013aSMark Brown 
3317cbaa62e0SDavid Lechner 	/* If an SPI controller does not support toggling the CS line on each
331871388b21SDavid Lechner 	 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
331971388b21SDavid Lechner 	 * for the CS line, we can emulate the CS-per-word hardware function by
3320cbaa62e0SDavid Lechner 	 * splitting transfers into one-word transfers and ensuring that
3321cbaa62e0SDavid Lechner 	 * cs_change is set for each transfer.
3322cbaa62e0SDavid Lechner 	 */
332371388b21SDavid Lechner 	if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
3324f3186dd8SLinus Walleij 					  spi->cs_gpiod ||
332571388b21SDavid Lechner 					  gpio_is_valid(spi->cs_gpio))) {
3326cbaa62e0SDavid Lechner 		size_t maxsize;
3327cbaa62e0SDavid Lechner 		int ret;
3328cbaa62e0SDavid Lechner 
3329cbaa62e0SDavid Lechner 		maxsize = (spi->bits_per_word + 7) / 8;
3330cbaa62e0SDavid Lechner 
3331cbaa62e0SDavid Lechner 		/* spi_split_transfers_maxsize() requires message->spi */
3332cbaa62e0SDavid Lechner 		message->spi = spi;
3333cbaa62e0SDavid Lechner 
3334cbaa62e0SDavid Lechner 		ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
3335cbaa62e0SDavid Lechner 						  GFP_KERNEL);
3336cbaa62e0SDavid Lechner 		if (ret)
3337cbaa62e0SDavid Lechner 			return ret;
3338cbaa62e0SDavid Lechner 
3339cbaa62e0SDavid Lechner 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
3340cbaa62e0SDavid Lechner 			/* don't change cs_change on the last entry in the list */
3341cbaa62e0SDavid Lechner 			if (list_is_last(&xfer->transfer_list, &message->transfers))
3342cbaa62e0SDavid Lechner 				break;
3343cbaa62e0SDavid Lechner 			xfer->cs_change = 1;
3344cbaa62e0SDavid Lechner 		}
3345cbaa62e0SDavid Lechner 	}
3346cbaa62e0SDavid Lechner 
3347cf32b71eSErnst Schwab 	/* Half-duplex links include original MicroWire, and ones with
3348cf32b71eSErnst Schwab 	 * only one data pin like SPI_3WIRE (switches direction) or where
3349cf32b71eSErnst Schwab 	 * either MOSI or MISO is missing.  They can also be caused by
3350cf32b71eSErnst Schwab 	 * software limitations.
3351cf32b71eSErnst Schwab 	 */
33528caab75fSGeert Uytterhoeven 	if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
33538caab75fSGeert Uytterhoeven 	    (spi->mode & SPI_3WIRE)) {
33548caab75fSGeert Uytterhoeven 		unsigned flags = ctlr->flags;
3355cf32b71eSErnst Schwab 
3356cf32b71eSErnst Schwab 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
3357cf32b71eSErnst Schwab 			if (xfer->rx_buf && xfer->tx_buf)
3358cf32b71eSErnst Schwab 				return -EINVAL;
33598caab75fSGeert Uytterhoeven 			if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
3360cf32b71eSErnst Schwab 				return -EINVAL;
33618caab75fSGeert Uytterhoeven 			if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
3362cf32b71eSErnst Schwab 				return -EINVAL;
3363cf32b71eSErnst Schwab 		}
3364cf32b71eSErnst Schwab 	}
3365cf32b71eSErnst Schwab 
3366e6811d1dSLaxman Dewangan 	/**
3367059b8ffeSLaxman Dewangan 	 * Set transfer bits_per_word and max speed as spi device default if
3368059b8ffeSLaxman Dewangan 	 * it is not set for this transfer.
3369f477b7fbSwangyuhang 	 * Set transfer tx_nbits and rx_nbits as single transfer default
3370f477b7fbSwangyuhang 	 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
3371b7bb367aSJonas Bonn 	 * Ensure transfer word_delay is at least as long as that required by
3372b7bb367aSJonas Bonn 	 * device itself.
3373e6811d1dSLaxman Dewangan 	 */
337477e80588SMartin Sperl 	message->frame_length = 0;
3375e6811d1dSLaxman Dewangan 	list_for_each_entry(xfer, &message->transfers, transfer_list) {
33765d7e2b5eSMartin Sperl 		xfer->effective_speed_hz = 0;
3377078726ceSSourav Poddar 		message->frame_length += xfer->len;
3378e6811d1dSLaxman Dewangan 		if (!xfer->bits_per_word)
3379e6811d1dSLaxman Dewangan 			xfer->bits_per_word = spi->bits_per_word;
3380a6f87fadSAxel Lin 
3381a6f87fadSAxel Lin 		if (!xfer->speed_hz)
3382059b8ffeSLaxman Dewangan 			xfer->speed_hz = spi->max_speed_hz;
3383a6f87fadSAxel Lin 
33848caab75fSGeert Uytterhoeven 		if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
33858caab75fSGeert Uytterhoeven 			xfer->speed_hz = ctlr->max_speed_hz;
338656ede94aSGabor Juhos 
33878caab75fSGeert Uytterhoeven 		if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
3388543bb255SStephen Warren 			return -EINVAL;
3389a2fd4f9fSMark Brown 
33904d94bd21SIvan T. Ivanov 		/*
33914d94bd21SIvan T. Ivanov 		 * SPI transfer length should be multiple of SPI word size
33924d94bd21SIvan T. Ivanov 		 * where SPI word size should be power-of-two multiple
33934d94bd21SIvan T. Ivanov 		 */
33944d94bd21SIvan T. Ivanov 		if (xfer->bits_per_word <= 8)
33954d94bd21SIvan T. Ivanov 			w_size = 1;
33964d94bd21SIvan T. Ivanov 		else if (xfer->bits_per_word <= 16)
33974d94bd21SIvan T. Ivanov 			w_size = 2;
33984d94bd21SIvan T. Ivanov 		else
33994d94bd21SIvan T. Ivanov 			w_size = 4;
34004d94bd21SIvan T. Ivanov 
34014d94bd21SIvan T. Ivanov 		/* No partial transfers accepted */
34026ea31293SAtsushi Nemoto 		if (xfer->len % w_size)
34034d94bd21SIvan T. Ivanov 			return -EINVAL;
34044d94bd21SIvan T. Ivanov 
34058caab75fSGeert Uytterhoeven 		if (xfer->speed_hz && ctlr->min_speed_hz &&
34068caab75fSGeert Uytterhoeven 		    xfer->speed_hz < ctlr->min_speed_hz)
3407a2fd4f9fSMark Brown 			return -EINVAL;
3408f477b7fbSwangyuhang 
3409f477b7fbSwangyuhang 		if (xfer->tx_buf && !xfer->tx_nbits)
3410f477b7fbSwangyuhang 			xfer->tx_nbits = SPI_NBITS_SINGLE;
3411f477b7fbSwangyuhang 		if (xfer->rx_buf && !xfer->rx_nbits)
3412f477b7fbSwangyuhang 			xfer->rx_nbits = SPI_NBITS_SINGLE;
3413f477b7fbSwangyuhang 		/* check transfer tx/rx_nbits:
34141afd9989SGeert Uytterhoeven 		 * 1. check the value matches one of single, dual and quad
34151afd9989SGeert Uytterhoeven 		 * 2. check tx/rx_nbits match the mode in spi_device
3416f477b7fbSwangyuhang 		 */
3417db90a441SSourav Poddar 		if (xfer->tx_buf) {
3418f477b7fbSwangyuhang 			if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3419f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_DUAL &&
3420f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_QUAD)
3421a2fd4f9fSMark Brown 				return -EINVAL;
3422f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3423f477b7fbSwangyuhang 				!(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3424f477b7fbSwangyuhang 				return -EINVAL;
3425f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3426f477b7fbSwangyuhang 				!(spi->mode & SPI_TX_QUAD))
3427f477b7fbSwangyuhang 				return -EINVAL;
3428db90a441SSourav Poddar 		}
3429f477b7fbSwangyuhang 		/* check transfer rx_nbits */
3430db90a441SSourav Poddar 		if (xfer->rx_buf) {
3431f477b7fbSwangyuhang 			if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3432f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_DUAL &&
3433f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_QUAD)
3434f477b7fbSwangyuhang 				return -EINVAL;
3435f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3436f477b7fbSwangyuhang 				!(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3437f477b7fbSwangyuhang 				return -EINVAL;
3438f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3439f477b7fbSwangyuhang 				!(spi->mode & SPI_RX_QUAD))
3440f477b7fbSwangyuhang 				return -EINVAL;
3441e6811d1dSLaxman Dewangan 		}
3442b7bb367aSJonas Bonn 
34436c613f68SAlexandru Ardelean 		if (_spi_xfer_word_delay_update(xfer, spi))
34446c613f68SAlexandru Ardelean 			return -EINVAL;
3445e6811d1dSLaxman Dewangan 	}
3446e6811d1dSLaxman Dewangan 
3447cf32b71eSErnst Schwab 	message->status = -EINPROGRESS;
344890808738SMark Brown 
344990808738SMark Brown 	return 0;
345090808738SMark Brown }
345190808738SMark Brown 
345290808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message)
345390808738SMark Brown {
34548caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
3455b42faeeeSVladimir Oltean 	struct spi_transfer *xfer;
345690808738SMark Brown 
3457b5932f5cSBoris Brezillon 	/*
3458b5932f5cSBoris Brezillon 	 * Some controllers do not support doing regular SPI transfers. Return
3459b5932f5cSBoris Brezillon 	 * ENOTSUPP when this is the case.
3460b5932f5cSBoris Brezillon 	 */
3461b5932f5cSBoris Brezillon 	if (!ctlr->transfer)
3462b5932f5cSBoris Brezillon 		return -ENOTSUPP;
3463b5932f5cSBoris Brezillon 
346490808738SMark Brown 	message->spi = spi;
346590808738SMark Brown 
34668caab75fSGeert Uytterhoeven 	SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
3467eca2ebc7SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3468eca2ebc7SMartin Sperl 
346990808738SMark Brown 	trace_spi_message_submit(message);
347090808738SMark Brown 
3471b42faeeeSVladimir Oltean 	if (!ctlr->ptp_sts_supported) {
3472b42faeeeSVladimir Oltean 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
3473b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_pre = 0;
3474b42faeeeSVladimir Oltean 			ptp_read_system_prets(xfer->ptp_sts);
3475b42faeeeSVladimir Oltean 		}
3476b42faeeeSVladimir Oltean 	}
3477b42faeeeSVladimir Oltean 
34788caab75fSGeert Uytterhoeven 	return ctlr->transfer(spi, message);
3479cf32b71eSErnst Schwab }
3480cf32b71eSErnst Schwab 
3481568d0697SDavid Brownell /**
3482568d0697SDavid Brownell  * spi_async - asynchronous SPI transfer
3483568d0697SDavid Brownell  * @spi: device with which data will be exchanged
3484568d0697SDavid Brownell  * @message: describes the data transfers, including completion callback
3485568d0697SDavid Brownell  * Context: any (irqs may be blocked, etc)
3486568d0697SDavid Brownell  *
3487568d0697SDavid Brownell  * This call may be used in_irq and other contexts which can't sleep,
3488568d0697SDavid Brownell  * as well as from task contexts which can sleep.
3489568d0697SDavid Brownell  *
3490568d0697SDavid Brownell  * The completion callback is invoked in a context which can't sleep.
3491568d0697SDavid Brownell  * Before that invocation, the value of message->status is undefined.
3492568d0697SDavid Brownell  * When the callback is issued, message->status holds either zero (to
3493568d0697SDavid Brownell  * indicate complete success) or a negative error code.  After that
3494568d0697SDavid Brownell  * callback returns, the driver which issued the transfer request may
3495568d0697SDavid Brownell  * deallocate the associated memory; it's no longer in use by any SPI
3496568d0697SDavid Brownell  * core or controller driver code.
3497568d0697SDavid Brownell  *
3498568d0697SDavid Brownell  * Note that although all messages to a spi_device are handled in
3499568d0697SDavid Brownell  * FIFO order, messages may go to different devices in other orders.
3500568d0697SDavid Brownell  * Some device might be higher priority, or have various "hard" access
3501568d0697SDavid Brownell  * time requirements, for example.
3502568d0697SDavid Brownell  *
3503568d0697SDavid Brownell  * On detection of any fault during the transfer, processing of
3504568d0697SDavid Brownell  * the entire message is aborted, and the device is deselected.
3505568d0697SDavid Brownell  * Until returning from the associated message completion callback,
3506568d0697SDavid Brownell  * no other spi_message queued to that device will be processed.
3507568d0697SDavid Brownell  * (This rule applies equally to all the synchronous transfer calls,
3508568d0697SDavid Brownell  * which are wrappers around this core asynchronous primitive.)
350997d56dc6SJavier Martinez Canillas  *
351097d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
3511568d0697SDavid Brownell  */
3512568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message)
3513568d0697SDavid Brownell {
35148caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
3515cf32b71eSErnst Schwab 	int ret;
3516cf32b71eSErnst Schwab 	unsigned long flags;
3517568d0697SDavid Brownell 
351890808738SMark Brown 	ret = __spi_validate(spi, message);
351990808738SMark Brown 	if (ret != 0)
352090808738SMark Brown 		return ret;
352190808738SMark Brown 
35228caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3523568d0697SDavid Brownell 
35248caab75fSGeert Uytterhoeven 	if (ctlr->bus_lock_flag)
3525cf32b71eSErnst Schwab 		ret = -EBUSY;
3526cf32b71eSErnst Schwab 	else
3527cf32b71eSErnst Schwab 		ret = __spi_async(spi, message);
3528568d0697SDavid Brownell 
35298caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3530cf32b71eSErnst Schwab 
3531cf32b71eSErnst Schwab 	return ret;
3532568d0697SDavid Brownell }
3533568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async);
3534568d0697SDavid Brownell 
3535cf32b71eSErnst Schwab /**
3536cf32b71eSErnst Schwab  * spi_async_locked - version of spi_async with exclusive bus usage
3537cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
3538cf32b71eSErnst Schwab  * @message: describes the data transfers, including completion callback
3539cf32b71eSErnst Schwab  * Context: any (irqs may be blocked, etc)
3540cf32b71eSErnst Schwab  *
3541cf32b71eSErnst Schwab  * This call may be used in_irq and other contexts which can't sleep,
3542cf32b71eSErnst Schwab  * as well as from task contexts which can sleep.
3543cf32b71eSErnst Schwab  *
3544cf32b71eSErnst Schwab  * The completion callback is invoked in a context which can't sleep.
3545cf32b71eSErnst Schwab  * Before that invocation, the value of message->status is undefined.
3546cf32b71eSErnst Schwab  * When the callback is issued, message->status holds either zero (to
3547cf32b71eSErnst Schwab  * indicate complete success) or a negative error code.  After that
3548cf32b71eSErnst Schwab  * callback returns, the driver which issued the transfer request may
3549cf32b71eSErnst Schwab  * deallocate the associated memory; it's no longer in use by any SPI
3550cf32b71eSErnst Schwab  * core or controller driver code.
3551cf32b71eSErnst Schwab  *
3552cf32b71eSErnst Schwab  * Note that although all messages to a spi_device are handled in
3553cf32b71eSErnst Schwab  * FIFO order, messages may go to different devices in other orders.
3554cf32b71eSErnst Schwab  * Some device might be higher priority, or have various "hard" access
3555cf32b71eSErnst Schwab  * time requirements, for example.
3556cf32b71eSErnst Schwab  *
3557cf32b71eSErnst Schwab  * On detection of any fault during the transfer, processing of
3558cf32b71eSErnst Schwab  * the entire message is aborted, and the device is deselected.
3559cf32b71eSErnst Schwab  * Until returning from the associated message completion callback,
3560cf32b71eSErnst Schwab  * no other spi_message queued to that device will be processed.
3561cf32b71eSErnst Schwab  * (This rule applies equally to all the synchronous transfer calls,
3562cf32b71eSErnst Schwab  * which are wrappers around this core asynchronous primitive.)
356397d56dc6SJavier Martinez Canillas  *
356497d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
3565cf32b71eSErnst Schwab  */
3566cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3567cf32b71eSErnst Schwab {
35688caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
3569cf32b71eSErnst Schwab 	int ret;
3570cf32b71eSErnst Schwab 	unsigned long flags;
3571cf32b71eSErnst Schwab 
357290808738SMark Brown 	ret = __spi_validate(spi, message);
357390808738SMark Brown 	if (ret != 0)
357490808738SMark Brown 		return ret;
357590808738SMark Brown 
35768caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3577cf32b71eSErnst Schwab 
3578cf32b71eSErnst Schwab 	ret = __spi_async(spi, message);
3579cf32b71eSErnst Schwab 
35808caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3581cf32b71eSErnst Schwab 
3582cf32b71eSErnst Schwab 	return ret;
3583cf32b71eSErnst Schwab 
3584cf32b71eSErnst Schwab }
3585cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked);
3586cf32b71eSErnst Schwab 
35877d077197SDavid Brownell /*-------------------------------------------------------------------------*/
35887d077197SDavid Brownell 
35898caab75fSGeert Uytterhoeven /* Utility methods for SPI protocol drivers, layered on
35907d077197SDavid Brownell  * top of the core.  Some other utility methods are defined as
35917d077197SDavid Brownell  * inline functions.
35927d077197SDavid Brownell  */
35937d077197SDavid Brownell 
35945d870c8eSAndrew Morton static void spi_complete(void *arg)
35955d870c8eSAndrew Morton {
35965d870c8eSAndrew Morton 	complete(arg);
35975d870c8eSAndrew Morton }
35985d870c8eSAndrew Morton 
3599ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message)
3600cf32b71eSErnst Schwab {
3601cf32b71eSErnst Schwab 	DECLARE_COMPLETION_ONSTACK(done);
3602cf32b71eSErnst Schwab 	int status;
36038caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
36040461a414SMark Brown 	unsigned long flags;
36050461a414SMark Brown 
36060461a414SMark Brown 	status = __spi_validate(spi, message);
36070461a414SMark Brown 	if (status != 0)
36080461a414SMark Brown 		return status;
3609cf32b71eSErnst Schwab 
3610cf32b71eSErnst Schwab 	message->complete = spi_complete;
3611cf32b71eSErnst Schwab 	message->context = &done;
36120461a414SMark Brown 	message->spi = spi;
3613cf32b71eSErnst Schwab 
36148caab75fSGeert Uytterhoeven 	SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
3615eca2ebc7SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3616eca2ebc7SMartin Sperl 
36170461a414SMark Brown 	/* If we're not using the legacy transfer method then we will
36180461a414SMark Brown 	 * try to transfer in the calling context so special case.
36190461a414SMark Brown 	 * This code would be less tricky if we could remove the
36200461a414SMark Brown 	 * support for driver implemented message queues.
36210461a414SMark Brown 	 */
36228caab75fSGeert Uytterhoeven 	if (ctlr->transfer == spi_queued_transfer) {
36238caab75fSGeert Uytterhoeven 		spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
36240461a414SMark Brown 
36250461a414SMark Brown 		trace_spi_message_submit(message);
36260461a414SMark Brown 
36270461a414SMark Brown 		status = __spi_queued_transfer(spi, message, false);
36280461a414SMark Brown 
36298caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
36300461a414SMark Brown 	} else {
3631cf32b71eSErnst Schwab 		status = spi_async_locked(spi, message);
36320461a414SMark Brown 	}
3633cf32b71eSErnst Schwab 
3634cf32b71eSErnst Schwab 	if (status == 0) {
36350461a414SMark Brown 		/* Push out the messages in the calling context if we
36360461a414SMark Brown 		 * can.
36370461a414SMark Brown 		 */
36388caab75fSGeert Uytterhoeven 		if (ctlr->transfer == spi_queued_transfer) {
36398caab75fSGeert Uytterhoeven 			SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
3640eca2ebc7SMartin Sperl 						       spi_sync_immediate);
3641eca2ebc7SMartin Sperl 			SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3642eca2ebc7SMartin Sperl 						       spi_sync_immediate);
36438caab75fSGeert Uytterhoeven 			__spi_pump_messages(ctlr, false);
3644eca2ebc7SMartin Sperl 		}
36450461a414SMark Brown 
3646cf32b71eSErnst Schwab 		wait_for_completion(&done);
3647cf32b71eSErnst Schwab 		status = message->status;
3648cf32b71eSErnst Schwab 	}
3649cf32b71eSErnst Schwab 	message->context = NULL;
3650cf32b71eSErnst Schwab 	return status;
3651cf32b71eSErnst Schwab }
3652cf32b71eSErnst Schwab 
36538ae12a0dSDavid Brownell /**
36548ae12a0dSDavid Brownell  * spi_sync - blocking/synchronous SPI data transfers
36558ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
36568ae12a0dSDavid Brownell  * @message: describes the data transfers
365733e34dc6SDavid Brownell  * Context: can sleep
36588ae12a0dSDavid Brownell  *
36598ae12a0dSDavid Brownell  * This call may only be used from a context that may sleep.  The sleep
36608ae12a0dSDavid Brownell  * is non-interruptible, and has no timeout.  Low-overhead controller
36618ae12a0dSDavid Brownell  * drivers may DMA directly into and out of the message buffers.
36628ae12a0dSDavid Brownell  *
36638ae12a0dSDavid Brownell  * Note that the SPI device's chip select is active during the message,
36648ae12a0dSDavid Brownell  * and then is normally disabled between messages.  Drivers for some
36658ae12a0dSDavid Brownell  * frequently-used devices may want to minimize costs of selecting a chip,
36668ae12a0dSDavid Brownell  * by leaving it selected in anticipation that the next message will go
36678ae12a0dSDavid Brownell  * to the same chip.  (That may increase power usage.)
36688ae12a0dSDavid Brownell  *
36690c868461SDavid Brownell  * Also, the caller is guaranteeing that the memory associated with the
36700c868461SDavid Brownell  * message will not be freed before this call returns.
36710c868461SDavid Brownell  *
367297d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
36738ae12a0dSDavid Brownell  */
36748ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message)
36758ae12a0dSDavid Brownell {
3676ef4d96ecSMark Brown 	int ret;
3677ef4d96ecSMark Brown 
36788caab75fSGeert Uytterhoeven 	mutex_lock(&spi->controller->bus_lock_mutex);
3679ef4d96ecSMark Brown 	ret = __spi_sync(spi, message);
36808caab75fSGeert Uytterhoeven 	mutex_unlock(&spi->controller->bus_lock_mutex);
3681ef4d96ecSMark Brown 
3682ef4d96ecSMark Brown 	return ret;
36838ae12a0dSDavid Brownell }
36848ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync);
36858ae12a0dSDavid Brownell 
3686cf32b71eSErnst Schwab /**
3687cf32b71eSErnst Schwab  * spi_sync_locked - version of spi_sync with exclusive bus usage
3688cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
3689cf32b71eSErnst Schwab  * @message: describes the data transfers
3690cf32b71eSErnst Schwab  * Context: can sleep
3691cf32b71eSErnst Schwab  *
3692cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
3693cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.  Low-overhead controller
3694cf32b71eSErnst Schwab  * drivers may DMA directly into and out of the message buffers.
3695cf32b71eSErnst Schwab  *
3696cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
369725985edcSLucas De Marchi  * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3698cf32b71eSErnst Schwab  * be released by a spi_bus_unlock call when the exclusive access is over.
3699cf32b71eSErnst Schwab  *
370097d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
3701cf32b71eSErnst Schwab  */
3702cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3703cf32b71eSErnst Schwab {
3704ef4d96ecSMark Brown 	return __spi_sync(spi, message);
3705cf32b71eSErnst Schwab }
3706cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked);
3707cf32b71eSErnst Schwab 
3708cf32b71eSErnst Schwab /**
3709cf32b71eSErnst Schwab  * spi_bus_lock - obtain a lock for exclusive SPI bus usage
37108caab75fSGeert Uytterhoeven  * @ctlr: SPI bus master that should be locked for exclusive bus access
3711cf32b71eSErnst Schwab  * Context: can sleep
3712cf32b71eSErnst Schwab  *
3713cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
3714cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
3715cf32b71eSErnst Schwab  *
3716cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
3717cf32b71eSErnst Schwab  * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3718cf32b71eSErnst Schwab  * exclusive access is over. Data transfer must be done by spi_sync_locked
3719cf32b71eSErnst Schwab  * and spi_async_locked calls when the SPI bus lock is held.
3720cf32b71eSErnst Schwab  *
372197d56dc6SJavier Martinez Canillas  * Return: always zero.
3722cf32b71eSErnst Schwab  */
37238caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr)
3724cf32b71eSErnst Schwab {
3725cf32b71eSErnst Schwab 	unsigned long flags;
3726cf32b71eSErnst Schwab 
37278caab75fSGeert Uytterhoeven 	mutex_lock(&ctlr->bus_lock_mutex);
3728cf32b71eSErnst Schwab 
37298caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
37308caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 1;
37318caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3732cf32b71eSErnst Schwab 
3733cf32b71eSErnst Schwab 	/* mutex remains locked until spi_bus_unlock is called */
3734cf32b71eSErnst Schwab 
3735cf32b71eSErnst Schwab 	return 0;
3736cf32b71eSErnst Schwab }
3737cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock);
3738cf32b71eSErnst Schwab 
3739cf32b71eSErnst Schwab /**
3740cf32b71eSErnst Schwab  * spi_bus_unlock - release the lock for exclusive SPI bus usage
37418caab75fSGeert Uytterhoeven  * @ctlr: SPI bus master that was locked for exclusive bus access
3742cf32b71eSErnst Schwab  * Context: can sleep
3743cf32b71eSErnst Schwab  *
3744cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
3745cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
3746cf32b71eSErnst Schwab  *
3747cf32b71eSErnst Schwab  * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3748cf32b71eSErnst Schwab  * call.
3749cf32b71eSErnst Schwab  *
375097d56dc6SJavier Martinez Canillas  * Return: always zero.
3751cf32b71eSErnst Schwab  */
37528caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr)
3753cf32b71eSErnst Schwab {
37548caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 0;
3755cf32b71eSErnst Schwab 
37568caab75fSGeert Uytterhoeven 	mutex_unlock(&ctlr->bus_lock_mutex);
3757cf32b71eSErnst Schwab 
3758cf32b71eSErnst Schwab 	return 0;
3759cf32b71eSErnst Schwab }
3760cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock);
3761cf32b71eSErnst Schwab 
3762a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */
3763a9948b61SDavid Brownell #define	SPI_BUFSIZ	max(32, SMP_CACHE_BYTES)
37648ae12a0dSDavid Brownell 
37658ae12a0dSDavid Brownell static u8	*buf;
37668ae12a0dSDavid Brownell 
37678ae12a0dSDavid Brownell /**
37688ae12a0dSDavid Brownell  * spi_write_then_read - SPI synchronous write followed by read
37698ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
37708ae12a0dSDavid Brownell  * @txbuf: data to be written (need not be dma-safe)
37718ae12a0dSDavid Brownell  * @n_tx: size of txbuf, in bytes
377227570497SJiri Pirko  * @rxbuf: buffer into which data will be read (need not be dma-safe)
377327570497SJiri Pirko  * @n_rx: size of rxbuf, in bytes
377433e34dc6SDavid Brownell  * Context: can sleep
37758ae12a0dSDavid Brownell  *
37768ae12a0dSDavid Brownell  * This performs a half duplex MicroWire style transaction with the
37778ae12a0dSDavid Brownell  * device, sending txbuf and then reading rxbuf.  The return value
37788ae12a0dSDavid Brownell  * is zero for success, else a negative errno status code.
3779b885244eSDavid Brownell  * This call may only be used from a context that may sleep.
37808ae12a0dSDavid Brownell  *
37810c868461SDavid Brownell  * Parameters to this routine are always copied using a small buffer;
378233e34dc6SDavid Brownell  * portable code should never use this for more than 32 bytes.
378333e34dc6SDavid Brownell  * Performance-sensitive or bulk transfer code should instead use
37840c868461SDavid Brownell  * spi_{async,sync}() calls with dma-safe buffers.
378597d56dc6SJavier Martinez Canillas  *
378697d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
37878ae12a0dSDavid Brownell  */
37888ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi,
37890c4a1590SMark Brown 		const void *txbuf, unsigned n_tx,
37900c4a1590SMark Brown 		void *rxbuf, unsigned n_rx)
37918ae12a0dSDavid Brownell {
3792068f4070SDavid Brownell 	static DEFINE_MUTEX(lock);
37938ae12a0dSDavid Brownell 
37948ae12a0dSDavid Brownell 	int			status;
37958ae12a0dSDavid Brownell 	struct spi_message	message;
3796bdff549eSDavid Brownell 	struct spi_transfer	x[2];
37978ae12a0dSDavid Brownell 	u8			*local_buf;
37988ae12a0dSDavid Brownell 
3799b3a223eeSMark Brown 	/* Use preallocated DMA-safe buffer if we can.  We can't avoid
3800b3a223eeSMark Brown 	 * copying here, (as a pure convenience thing), but we can
3801b3a223eeSMark Brown 	 * keep heap costs out of the hot path unless someone else is
3802b3a223eeSMark Brown 	 * using the pre-allocated buffer or the transfer is too large.
38038ae12a0dSDavid Brownell 	 */
3804b3a223eeSMark Brown 	if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
38052cd94c8aSMark Brown 		local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
38062cd94c8aSMark Brown 				    GFP_KERNEL | GFP_DMA);
3807b3a223eeSMark Brown 		if (!local_buf)
3808b3a223eeSMark Brown 			return -ENOMEM;
3809b3a223eeSMark Brown 	} else {
3810b3a223eeSMark Brown 		local_buf = buf;
3811b3a223eeSMark Brown 	}
38128ae12a0dSDavid Brownell 
38138275c642SVitaly Wool 	spi_message_init(&message);
38145fe5f05eSJingoo Han 	memset(x, 0, sizeof(x));
3815bdff549eSDavid Brownell 	if (n_tx) {
3816bdff549eSDavid Brownell 		x[0].len = n_tx;
3817bdff549eSDavid Brownell 		spi_message_add_tail(&x[0], &message);
3818bdff549eSDavid Brownell 	}
3819bdff549eSDavid Brownell 	if (n_rx) {
3820bdff549eSDavid Brownell 		x[1].len = n_rx;
3821bdff549eSDavid Brownell 		spi_message_add_tail(&x[1], &message);
3822bdff549eSDavid Brownell 	}
38238275c642SVitaly Wool 
38248ae12a0dSDavid Brownell 	memcpy(local_buf, txbuf, n_tx);
3825bdff549eSDavid Brownell 	x[0].tx_buf = local_buf;
3826bdff549eSDavid Brownell 	x[1].rx_buf = local_buf + n_tx;
38278ae12a0dSDavid Brownell 
38288ae12a0dSDavid Brownell 	/* do the i/o */
38298ae12a0dSDavid Brownell 	status = spi_sync(spi, &message);
38309b938b74SMarc Pignat 	if (status == 0)
3831bdff549eSDavid Brownell 		memcpy(rxbuf, x[1].rx_buf, n_rx);
38328ae12a0dSDavid Brownell 
3833bdff549eSDavid Brownell 	if (x[0].tx_buf == buf)
3834068f4070SDavid Brownell 		mutex_unlock(&lock);
38358ae12a0dSDavid Brownell 	else
38368ae12a0dSDavid Brownell 		kfree(local_buf);
38378ae12a0dSDavid Brownell 
38388ae12a0dSDavid Brownell 	return status;
38398ae12a0dSDavid Brownell }
38408ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read);
38418ae12a0dSDavid Brownell 
38428ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
38438ae12a0dSDavid Brownell 
38445f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF)
3845ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */
38465f143af7SMarco Felsch struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3847ce79d54aSPantelis Antoniou {
3848cfba5de9SSuzuki K Poulose 	struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
3849cfba5de9SSuzuki K Poulose 
3850ce79d54aSPantelis Antoniou 	return dev ? to_spi_device(dev) : NULL;
3851ce79d54aSPantelis Antoniou }
38525f143af7SMarco Felsch EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
38535f143af7SMarco Felsch #endif /* IS_ENABLED(CONFIG_OF) */
3854ce79d54aSPantelis Antoniou 
38555f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF_DYNAMIC)
38568caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */
38578caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
3858ce79d54aSPantelis Antoniou {
3859ce79d54aSPantelis Antoniou 	struct device *dev;
3860ce79d54aSPantelis Antoniou 
3861cfba5de9SSuzuki K Poulose 	dev = class_find_device_by_of_node(&spi_master_class, node);
38626c364062SGeert Uytterhoeven 	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3863cfba5de9SSuzuki K Poulose 		dev = class_find_device_by_of_node(&spi_slave_class, node);
3864ce79d54aSPantelis Antoniou 	if (!dev)
3865ce79d54aSPantelis Antoniou 		return NULL;
3866ce79d54aSPantelis Antoniou 
3867ce79d54aSPantelis Antoniou 	/* reference got in class_find_device */
38688caab75fSGeert Uytterhoeven 	return container_of(dev, struct spi_controller, dev);
3869ce79d54aSPantelis Antoniou }
3870ce79d54aSPantelis Antoniou 
3871ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3872ce79d54aSPantelis Antoniou 			 void *arg)
3873ce79d54aSPantelis Antoniou {
3874ce79d54aSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
38758caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
3876ce79d54aSPantelis Antoniou 	struct spi_device *spi;
3877ce79d54aSPantelis Antoniou 
3878ce79d54aSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, arg)) {
3879ce79d54aSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
38808caab75fSGeert Uytterhoeven 		ctlr = of_find_spi_controller_by_node(rd->dn->parent);
38818caab75fSGeert Uytterhoeven 		if (ctlr == NULL)
3882ce79d54aSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
3883ce79d54aSPantelis Antoniou 
3884bd6c1644SGeert Uytterhoeven 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
38858caab75fSGeert Uytterhoeven 			put_device(&ctlr->dev);
3886bd6c1644SGeert Uytterhoeven 			return NOTIFY_OK;
3887bd6c1644SGeert Uytterhoeven 		}
3888bd6c1644SGeert Uytterhoeven 
38898caab75fSGeert Uytterhoeven 		spi = of_register_spi_device(ctlr, rd->dn);
38908caab75fSGeert Uytterhoeven 		put_device(&ctlr->dev);
3891ce79d54aSPantelis Antoniou 
3892ce79d54aSPantelis Antoniou 		if (IS_ERR(spi)) {
389325c56c88SRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
389425c56c88SRob Herring 					__func__, rd->dn);
3895e0af98a7SRalf Ramsauer 			of_node_clear_flag(rd->dn, OF_POPULATED);
3896ce79d54aSPantelis Antoniou 			return notifier_from_errno(PTR_ERR(spi));
3897ce79d54aSPantelis Antoniou 		}
3898ce79d54aSPantelis Antoniou 		break;
3899ce79d54aSPantelis Antoniou 
3900ce79d54aSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
3901bd6c1644SGeert Uytterhoeven 		/* already depopulated? */
3902bd6c1644SGeert Uytterhoeven 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
3903bd6c1644SGeert Uytterhoeven 			return NOTIFY_OK;
3904bd6c1644SGeert Uytterhoeven 
3905ce79d54aSPantelis Antoniou 		/* find our device by node */
3906ce79d54aSPantelis Antoniou 		spi = of_find_spi_device_by_node(rd->dn);
3907ce79d54aSPantelis Antoniou 		if (spi == NULL)
3908ce79d54aSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
3909ce79d54aSPantelis Antoniou 
3910ce79d54aSPantelis Antoniou 		/* unregister takes one ref away */
3911ce79d54aSPantelis Antoniou 		spi_unregister_device(spi);
3912ce79d54aSPantelis Antoniou 
3913ce79d54aSPantelis Antoniou 		/* and put the reference of the find */
3914ce79d54aSPantelis Antoniou 		put_device(&spi->dev);
3915ce79d54aSPantelis Antoniou 		break;
3916ce79d54aSPantelis Antoniou 	}
3917ce79d54aSPantelis Antoniou 
3918ce79d54aSPantelis Antoniou 	return NOTIFY_OK;
3919ce79d54aSPantelis Antoniou }
3920ce79d54aSPantelis Antoniou 
3921ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = {
3922ce79d54aSPantelis Antoniou 	.notifier_call = of_spi_notify,
3923ce79d54aSPantelis Antoniou };
3924ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3925ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier;
3926ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3927ce79d54aSPantelis Antoniou 
39287f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI)
39298caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data)
39307f24467fSOctavian Purdila {
39317f24467fSOctavian Purdila 	return ACPI_COMPANION(dev->parent) == data;
39327f24467fSOctavian Purdila }
39337f24467fSOctavian Purdila 
39348caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
39357f24467fSOctavian Purdila {
39367f24467fSOctavian Purdila 	struct device *dev;
39377f24467fSOctavian Purdila 
39387f24467fSOctavian Purdila 	dev = class_find_device(&spi_master_class, NULL, adev,
39398caab75fSGeert Uytterhoeven 				spi_acpi_controller_match);
39406c364062SGeert Uytterhoeven 	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
39416c364062SGeert Uytterhoeven 		dev = class_find_device(&spi_slave_class, NULL, adev,
39428caab75fSGeert Uytterhoeven 					spi_acpi_controller_match);
39437f24467fSOctavian Purdila 	if (!dev)
39447f24467fSOctavian Purdila 		return NULL;
39457f24467fSOctavian Purdila 
39468caab75fSGeert Uytterhoeven 	return container_of(dev, struct spi_controller, dev);
39477f24467fSOctavian Purdila }
39487f24467fSOctavian Purdila 
39497f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
39507f24467fSOctavian Purdila {
39517f24467fSOctavian Purdila 	struct device *dev;
39527f24467fSOctavian Purdila 
395300500147SSuzuki K Poulose 	dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
39547f24467fSOctavian Purdila 	return dev ? to_spi_device(dev) : NULL;
39557f24467fSOctavian Purdila }
39567f24467fSOctavian Purdila 
39577f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
39587f24467fSOctavian Purdila 			   void *arg)
39597f24467fSOctavian Purdila {
39607f24467fSOctavian Purdila 	struct acpi_device *adev = arg;
39618caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
39627f24467fSOctavian Purdila 	struct spi_device *spi;
39637f24467fSOctavian Purdila 
39647f24467fSOctavian Purdila 	switch (value) {
39657f24467fSOctavian Purdila 	case ACPI_RECONFIG_DEVICE_ADD:
39668caab75fSGeert Uytterhoeven 		ctlr = acpi_spi_find_controller_by_adev(adev->parent);
39678caab75fSGeert Uytterhoeven 		if (!ctlr)
39687f24467fSOctavian Purdila 			break;
39697f24467fSOctavian Purdila 
39708caab75fSGeert Uytterhoeven 		acpi_register_spi_device(ctlr, adev);
39718caab75fSGeert Uytterhoeven 		put_device(&ctlr->dev);
39727f24467fSOctavian Purdila 		break;
39737f24467fSOctavian Purdila 	case ACPI_RECONFIG_DEVICE_REMOVE:
39747f24467fSOctavian Purdila 		if (!acpi_device_enumerated(adev))
39757f24467fSOctavian Purdila 			break;
39767f24467fSOctavian Purdila 
39777f24467fSOctavian Purdila 		spi = acpi_spi_find_device_by_adev(adev);
39787f24467fSOctavian Purdila 		if (!spi)
39797f24467fSOctavian Purdila 			break;
39807f24467fSOctavian Purdila 
39817f24467fSOctavian Purdila 		spi_unregister_device(spi);
39827f24467fSOctavian Purdila 		put_device(&spi->dev);
39837f24467fSOctavian Purdila 		break;
39847f24467fSOctavian Purdila 	}
39857f24467fSOctavian Purdila 
39867f24467fSOctavian Purdila 	return NOTIFY_OK;
39877f24467fSOctavian Purdila }
39887f24467fSOctavian Purdila 
39897f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = {
39907f24467fSOctavian Purdila 	.notifier_call = acpi_spi_notify,
39917f24467fSOctavian Purdila };
39927f24467fSOctavian Purdila #else
39937f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier;
39947f24467fSOctavian Purdila #endif
39957f24467fSOctavian Purdila 
39968ae12a0dSDavid Brownell static int __init spi_init(void)
39978ae12a0dSDavid Brownell {
3998b885244eSDavid Brownell 	int	status;
39998ae12a0dSDavid Brownell 
4000e94b1766SChristoph Lameter 	buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
4001b885244eSDavid Brownell 	if (!buf) {
4002b885244eSDavid Brownell 		status = -ENOMEM;
4003b885244eSDavid Brownell 		goto err0;
40048ae12a0dSDavid Brownell 	}
4005b885244eSDavid Brownell 
4006b885244eSDavid Brownell 	status = bus_register(&spi_bus_type);
4007b885244eSDavid Brownell 	if (status < 0)
4008b885244eSDavid Brownell 		goto err1;
4009b885244eSDavid Brownell 
4010b885244eSDavid Brownell 	status = class_register(&spi_master_class);
4011b885244eSDavid Brownell 	if (status < 0)
4012b885244eSDavid Brownell 		goto err2;
4013ce79d54aSPantelis Antoniou 
40146c364062SGeert Uytterhoeven 	if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
40156c364062SGeert Uytterhoeven 		status = class_register(&spi_slave_class);
40166c364062SGeert Uytterhoeven 		if (status < 0)
40176c364062SGeert Uytterhoeven 			goto err3;
40186c364062SGeert Uytterhoeven 	}
40196c364062SGeert Uytterhoeven 
40205267720eSFabio Estevam 	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
4021ce79d54aSPantelis Antoniou 		WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
40227f24467fSOctavian Purdila 	if (IS_ENABLED(CONFIG_ACPI))
40237f24467fSOctavian Purdila 		WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
4024ce79d54aSPantelis Antoniou 
4025b885244eSDavid Brownell 	return 0;
4026b885244eSDavid Brownell 
40276c364062SGeert Uytterhoeven err3:
40286c364062SGeert Uytterhoeven 	class_unregister(&spi_master_class);
4029b885244eSDavid Brownell err2:
4030b885244eSDavid Brownell 	bus_unregister(&spi_bus_type);
4031b885244eSDavid Brownell err1:
4032b885244eSDavid Brownell 	kfree(buf);
4033b885244eSDavid Brownell 	buf = NULL;
4034b885244eSDavid Brownell err0:
4035b885244eSDavid Brownell 	return status;
4036b885244eSDavid Brownell }
4037b885244eSDavid Brownell 
40388ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(),
40398ae12a0dSDavid Brownell  * but even essential drivers wait till later
4040b885244eSDavid Brownell  *
4041b885244eSDavid Brownell  * REVISIT only boardinfo really needs static linking. the rest (device and
4042b885244eSDavid Brownell  * driver registration) _could_ be dynamically linked (modular) ... costs
4043b885244eSDavid Brownell  * include needing to have boardinfo data structures be much more public.
40448ae12a0dSDavid Brownell  */
4045673c0c00SDavid Brownell postcore_initcall(spi_init);
4046