xref: /linux/drivers/spi/spi.c (revision 8c2ae772fe08e33f3d7a83849e85539320701abd)
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 
7edf6a864SAndy Shevchenko #include <linux/acpi.h>
88ae12a0dSDavid Brownell #include <linux/cache.h>
9edf6a864SAndy Shevchenko #include <linux/clk/clk-conf.h>
10edf6a864SAndy Shevchenko #include <linux/delay.h>
11edf6a864SAndy Shevchenko #include <linux/device.h>
1299adef31SMark Brown #include <linux/dmaengine.h>
13edf6a864SAndy Shevchenko #include <linux/dma-mapping.h>
14edf6a864SAndy Shevchenko #include <linux/export.h>
15edf6a864SAndy Shevchenko #include <linux/gpio/consumer.h>
16edf6a864SAndy Shevchenko #include <linux/highmem.h>
17edf6a864SAndy Shevchenko #include <linux/idr.h>
18edf6a864SAndy Shevchenko #include <linux/init.h>
19edf6a864SAndy Shevchenko #include <linux/ioport.h>
20edf6a864SAndy Shevchenko #include <linux/kernel.h>
21edf6a864SAndy Shevchenko #include <linux/kthread.h>
22edf6a864SAndy Shevchenko #include <linux/mod_devicetable.h>
2394040828SMatthias Kaehlcke #include <linux/mutex.h>
242b7a32f7SSinan Akman #include <linux/of_device.h>
25d57a4282SGrant Likely #include <linux/of_irq.h>
26edf6a864SAndy Shevchenko #include <linux/percpu.h>
27edf6a864SAndy Shevchenko #include <linux/platform_data/x86/apple.h>
28edf6a864SAndy Shevchenko #include <linux/pm_domain.h>
29edf6a864SAndy Shevchenko #include <linux/pm_runtime.h>
30edf6a864SAndy Shevchenko #include <linux/property.h>
31edf6a864SAndy Shevchenko #include <linux/ptp_clock_kernel.h>
32edf6a864SAndy Shevchenko #include <linux/sched/rt.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
348ae12a0dSDavid Brownell #include <linux/spi/spi.h>
35b5932f5cSBoris Brezillon #include <linux/spi/spi-mem.h>
36ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h>
378ae12a0dSDavid Brownell 
3856ec1978SMark Brown #define CREATE_TRACE_POINTS
3956ec1978SMark Brown #include <trace/events/spi.h>
40ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
41ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
429b61e302SSuniel Mahesh 
4346336966SBoris Brezillon #include "internals.h"
4446336966SBoris Brezillon 
459b61e302SSuniel Mahesh static DEFINE_IDR(spi_master_idr);
4656ec1978SMark Brown 
478ae12a0dSDavid Brownell static void spidev_release(struct device *dev)
488ae12a0dSDavid Brownell {
490ffa0285SHans-Peter Nilsson 	struct spi_device	*spi = to_spi_device(dev);
508ae12a0dSDavid Brownell 
518caab75fSGeert Uytterhoeven 	spi_controller_put(spi->controller);
525039563eSTrent Piepho 	kfree(spi->driver_override);
536598b91bSDavid Jander 	free_percpu(spi->pcpu_statistics);
5407a389feSRoman Tereshonkov 	kfree(spi);
558ae12a0dSDavid Brownell }
568ae12a0dSDavid Brownell 
578ae12a0dSDavid Brownell static ssize_t
588ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf)
598ae12a0dSDavid Brownell {
608ae12a0dSDavid Brownell 	const struct spi_device	*spi = to_spi_device(dev);
618c4ff6d0SZhang Rui 	int len;
628c4ff6d0SZhang Rui 
638c4ff6d0SZhang Rui 	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
648c4ff6d0SZhang Rui 	if (len != -ENODEV)
658c4ff6d0SZhang Rui 		return len;
668ae12a0dSDavid Brownell 
67f2daa466SAndy Shevchenko 	return sysfs_emit(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
688ae12a0dSDavid Brownell }
69aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
708ae12a0dSDavid Brownell 
715039563eSTrent Piepho static ssize_t driver_override_store(struct device *dev,
725039563eSTrent Piepho 				     struct device_attribute *a,
735039563eSTrent Piepho 				     const char *buf, size_t count)
745039563eSTrent Piepho {
755039563eSTrent Piepho 	struct spi_device *spi = to_spi_device(dev);
7619368f0fSKrzysztof Kozlowski 	int ret;
775039563eSTrent Piepho 
7819368f0fSKrzysztof Kozlowski 	ret = driver_set_override(dev, &spi->driver_override, buf, count);
7919368f0fSKrzysztof Kozlowski 	if (ret)
8019368f0fSKrzysztof Kozlowski 		return ret;
815039563eSTrent Piepho 
825039563eSTrent Piepho 	return count;
835039563eSTrent Piepho }
845039563eSTrent Piepho 
855039563eSTrent Piepho static ssize_t driver_override_show(struct device *dev,
865039563eSTrent Piepho 				    struct device_attribute *a, char *buf)
875039563eSTrent Piepho {
885039563eSTrent Piepho 	const struct spi_device *spi = to_spi_device(dev);
895039563eSTrent Piepho 	ssize_t len;
905039563eSTrent Piepho 
915039563eSTrent Piepho 	device_lock(dev);
92f2daa466SAndy Shevchenko 	len = sysfs_emit(buf, "%s\n", spi->driver_override ? : "");
935039563eSTrent Piepho 	device_unlock(dev);
945039563eSTrent Piepho 	return len;
955039563eSTrent Piepho }
965039563eSTrent Piepho static DEVICE_ATTR_RW(driver_override);
975039563eSTrent Piepho 
98d501cc4cSDavid Jander static struct spi_statistics __percpu *spi_alloc_pcpu_stats(struct device *dev)
996598b91bSDavid Jander {
1006598b91bSDavid Jander 	struct spi_statistics __percpu *pcpu_stats;
1016598b91bSDavid Jander 
1026598b91bSDavid Jander 	if (dev)
1036598b91bSDavid Jander 		pcpu_stats = devm_alloc_percpu(dev, struct spi_statistics);
1046598b91bSDavid Jander 	else
1056598b91bSDavid Jander 		pcpu_stats = alloc_percpu_gfp(struct spi_statistics, GFP_KERNEL);
1066598b91bSDavid Jander 
1076598b91bSDavid Jander 	if (pcpu_stats) {
1086598b91bSDavid Jander 		int cpu;
1096598b91bSDavid Jander 
1106598b91bSDavid Jander 		for_each_possible_cpu(cpu) {
1116598b91bSDavid Jander 			struct spi_statistics *stat;
1126598b91bSDavid Jander 
1136598b91bSDavid Jander 			stat = per_cpu_ptr(pcpu_stats, cpu);
1146598b91bSDavid Jander 			u64_stats_init(&stat->syncp);
1156598b91bSDavid Jander 		}
1166598b91bSDavid Jander 	}
1176598b91bSDavid Jander 	return pcpu_stats;
1186598b91bSDavid Jander }
1196598b91bSDavid Jander 
120fc12d4bbSGeert Uytterhoeven static ssize_t spi_emit_pcpu_stats(struct spi_statistics __percpu *stat,
121fc12d4bbSGeert Uytterhoeven 				   char *buf, size_t offset)
122fc12d4bbSGeert Uytterhoeven {
123fc12d4bbSGeert Uytterhoeven 	u64 val = 0;
124fc12d4bbSGeert Uytterhoeven 	int i;
125fc12d4bbSGeert Uytterhoeven 
126fc12d4bbSGeert Uytterhoeven 	for_each_possible_cpu(i) {
127fc12d4bbSGeert Uytterhoeven 		const struct spi_statistics *pcpu_stats;
128fc12d4bbSGeert Uytterhoeven 		u64_stats_t *field;
129fc12d4bbSGeert Uytterhoeven 		unsigned int start;
130fc12d4bbSGeert Uytterhoeven 		u64 inc;
131fc12d4bbSGeert Uytterhoeven 
132fc12d4bbSGeert Uytterhoeven 		pcpu_stats = per_cpu_ptr(stat, i);
133fc12d4bbSGeert Uytterhoeven 		field = (void *)pcpu_stats + offset;
134fc12d4bbSGeert Uytterhoeven 		do {
135fc12d4bbSGeert Uytterhoeven 			start = u64_stats_fetch_begin(&pcpu_stats->syncp);
136fc12d4bbSGeert Uytterhoeven 			inc = u64_stats_read(field);
137fc12d4bbSGeert Uytterhoeven 		} while (u64_stats_fetch_retry(&pcpu_stats->syncp, start));
138fc12d4bbSGeert Uytterhoeven 		val += inc;
139fc12d4bbSGeert Uytterhoeven 	}
140fc12d4bbSGeert Uytterhoeven 	return sysfs_emit(buf, "%llu\n", val);
141fc12d4bbSGeert Uytterhoeven }
1426598b91bSDavid Jander 
143eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file)				\
1448caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev,	\
145eca2ebc7SMartin Sperl 					     struct device_attribute *attr, \
146eca2ebc7SMartin Sperl 					     char *buf)			\
147eca2ebc7SMartin Sperl {									\
1488caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = container_of(dev,			\
1498caab75fSGeert Uytterhoeven 					 struct spi_controller, dev);	\
1506598b91bSDavid Jander 	return spi_statistics_##field##_show(ctlr->pcpu_statistics, buf); \
151eca2ebc7SMartin Sperl }									\
1528caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = {	\
153ad25c92eSGeert Uytterhoeven 	.attr = { .name = file, .mode = 0444 },				\
1548caab75fSGeert Uytterhoeven 	.show = spi_controller_##field##_show,				\
155eca2ebc7SMartin Sperl };									\
156eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev,		\
157eca2ebc7SMartin Sperl 					 struct device_attribute *attr,	\
158eca2ebc7SMartin Sperl 					char *buf)			\
159eca2ebc7SMartin Sperl {									\
160d1eba93bSGeliang Tang 	struct spi_device *spi = to_spi_device(dev);			\
1616598b91bSDavid Jander 	return spi_statistics_##field##_show(spi->pcpu_statistics, buf); \
162eca2ebc7SMartin Sperl }									\
163eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = {		\
164ad25c92eSGeert Uytterhoeven 	.attr = { .name = file, .mode = 0444 },				\
165eca2ebc7SMartin Sperl 	.show = spi_device_##field##_show,				\
166eca2ebc7SMartin Sperl }
167eca2ebc7SMartin Sperl 
1686598b91bSDavid Jander #define SPI_STATISTICS_SHOW_NAME(name, file, field)			\
169d501cc4cSDavid Jander static ssize_t spi_statistics_##name##_show(struct spi_statistics __percpu *stat, \
170eca2ebc7SMartin Sperl 					    char *buf)			\
171eca2ebc7SMartin Sperl {									\
172fc12d4bbSGeert Uytterhoeven 	return spi_emit_pcpu_stats(stat, buf,				\
173fc12d4bbSGeert Uytterhoeven 			offsetof(struct spi_statistics, field));	\
174eca2ebc7SMartin Sperl }									\
175eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file)
176eca2ebc7SMartin Sperl 
1776598b91bSDavid Jander #define SPI_STATISTICS_SHOW(field)					\
178eca2ebc7SMartin Sperl 	SPI_STATISTICS_SHOW_NAME(field, __stringify(field),		\
1796598b91bSDavid Jander 				 field)
180eca2ebc7SMartin Sperl 
1816598b91bSDavid Jander SPI_STATISTICS_SHOW(messages);
1826598b91bSDavid Jander SPI_STATISTICS_SHOW(transfers);
1836598b91bSDavid Jander SPI_STATISTICS_SHOW(errors);
1846598b91bSDavid Jander SPI_STATISTICS_SHOW(timedout);
185eca2ebc7SMartin Sperl 
1866598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_sync);
1876598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_sync_immediate);
1886598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_async);
189eca2ebc7SMartin Sperl 
1906598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes);
1916598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes_rx);
1926598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes_tx);
193eca2ebc7SMartin Sperl 
1946b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number)		\
1956b7bc061SMartin Sperl 	SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index,		\
1966b7bc061SMartin Sperl 				 "transfer_bytes_histo_" number,	\
1976598b91bSDavid Jander 				 transfer_bytes_histo[index])
1986b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0,  "0-1");
1996b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1,  "2-3");
2006b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2,  "4-7");
2016b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3,  "8-15");
2026b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4,  "16-31");
2036b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5,  "32-63");
2046b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6,  "64-127");
2056b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7,  "128-255");
2066b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8,  "256-511");
2076b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9,  "512-1023");
2086b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
2096b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
2106b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
2116b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
2126b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
2136b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
2146b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
2156b7bc061SMartin Sperl 
2166598b91bSDavid Jander SPI_STATISTICS_SHOW(transfers_split_maxsize);
217d9f12122SMartin Sperl 
218aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = {
219aa7da564SGreg Kroah-Hartman 	&dev_attr_modalias.attr,
2205039563eSTrent Piepho 	&dev_attr_driver_override.attr,
221aa7da564SGreg Kroah-Hartman 	NULL,
2228ae12a0dSDavid Brownell };
223eca2ebc7SMartin Sperl 
224eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = {
225eca2ebc7SMartin Sperl 	.attrs  = spi_dev_attrs,
226eca2ebc7SMartin Sperl };
227eca2ebc7SMartin Sperl 
228eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = {
229eca2ebc7SMartin Sperl 	&dev_attr_spi_device_messages.attr,
230eca2ebc7SMartin Sperl 	&dev_attr_spi_device_transfers.attr,
231eca2ebc7SMartin Sperl 	&dev_attr_spi_device_errors.attr,
232eca2ebc7SMartin Sperl 	&dev_attr_spi_device_timedout.attr,
233eca2ebc7SMartin Sperl 	&dev_attr_spi_device_spi_sync.attr,
234eca2ebc7SMartin Sperl 	&dev_attr_spi_device_spi_sync_immediate.attr,
235eca2ebc7SMartin Sperl 	&dev_attr_spi_device_spi_async.attr,
236eca2ebc7SMartin Sperl 	&dev_attr_spi_device_bytes.attr,
237eca2ebc7SMartin Sperl 	&dev_attr_spi_device_bytes_rx.attr,
238eca2ebc7SMartin Sperl 	&dev_attr_spi_device_bytes_tx.attr,
2396b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo0.attr,
2406b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo1.attr,
2416b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo2.attr,
2426b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo3.attr,
2436b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo4.attr,
2446b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo5.attr,
2456b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo6.attr,
2466b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo7.attr,
2476b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo8.attr,
2486b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo9.attr,
2496b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo10.attr,
2506b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo11.attr,
2516b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo12.attr,
2526b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo13.attr,
2536b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo14.attr,
2546b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo15.attr,
2556b7bc061SMartin Sperl 	&dev_attr_spi_device_transfer_bytes_histo16.attr,
256d9f12122SMartin Sperl 	&dev_attr_spi_device_transfers_split_maxsize.attr,
257eca2ebc7SMartin Sperl 	NULL,
258eca2ebc7SMartin Sperl };
259eca2ebc7SMartin Sperl 
260eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = {
261eca2ebc7SMartin Sperl 	.name  = "statistics",
262eca2ebc7SMartin Sperl 	.attrs  = spi_device_statistics_attrs,
263eca2ebc7SMartin Sperl };
264eca2ebc7SMartin Sperl 
265eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = {
266eca2ebc7SMartin Sperl 	&spi_dev_group,
267eca2ebc7SMartin Sperl 	&spi_device_statistics_group,
268eca2ebc7SMartin Sperl 	NULL,
269eca2ebc7SMartin Sperl };
270eca2ebc7SMartin Sperl 
2718caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = {
2728caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_messages.attr,
2738caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfers.attr,
2748caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_errors.attr,
2758caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_timedout.attr,
2768caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_spi_sync.attr,
2778caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_spi_sync_immediate.attr,
2788caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_spi_async.attr,
2798caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_bytes.attr,
2808caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_bytes_rx.attr,
2818caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_bytes_tx.attr,
2828caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo0.attr,
2838caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo1.attr,
2848caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo2.attr,
2858caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo3.attr,
2868caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo4.attr,
2878caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo5.attr,
2888caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo6.attr,
2898caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo7.attr,
2908caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo8.attr,
2918caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo9.attr,
2928caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo10.attr,
2938caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo11.attr,
2948caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo12.attr,
2958caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo13.attr,
2968caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo14.attr,
2978caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo15.attr,
2988caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfer_bytes_histo16.attr,
2998caab75fSGeert Uytterhoeven 	&dev_attr_spi_controller_transfers_split_maxsize.attr,
300eca2ebc7SMartin Sperl 	NULL,
301eca2ebc7SMartin Sperl };
302eca2ebc7SMartin Sperl 
3038caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = {
304eca2ebc7SMartin Sperl 	.name  = "statistics",
3058caab75fSGeert Uytterhoeven 	.attrs  = spi_controller_statistics_attrs,
306eca2ebc7SMartin Sperl };
307eca2ebc7SMartin Sperl 
308eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = {
3098caab75fSGeert Uytterhoeven 	&spi_controller_statistics_group,
310eca2ebc7SMartin Sperl 	NULL,
311eca2ebc7SMartin Sperl };
312eca2ebc7SMartin Sperl 
313d501cc4cSDavid Jander static void spi_statistics_add_transfer_stats(struct spi_statistics __percpu *pcpu_stats,
314eca2ebc7SMartin Sperl 					      struct spi_transfer *xfer,
3158caab75fSGeert Uytterhoeven 					      struct spi_controller *ctlr)
316eca2ebc7SMartin Sperl {
3176b7bc061SMartin Sperl 	int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
31867b9d641SDavid Jander 	struct spi_statistics *stats;
3196b7bc061SMartin Sperl 
3206b7bc061SMartin Sperl 	if (l2len < 0)
3216b7bc061SMartin Sperl 		l2len = 0;
322eca2ebc7SMartin Sperl 
32367b9d641SDavid Jander 	get_cpu();
32467b9d641SDavid Jander 	stats = this_cpu_ptr(pcpu_stats);
3256598b91bSDavid Jander 	u64_stats_update_begin(&stats->syncp);
326eca2ebc7SMartin Sperl 
3276598b91bSDavid Jander 	u64_stats_inc(&stats->transfers);
3286598b91bSDavid Jander 	u64_stats_inc(&stats->transfer_bytes_histo[l2len]);
329eca2ebc7SMartin Sperl 
3306598b91bSDavid Jander 	u64_stats_add(&stats->bytes, xfer->len);
331eca2ebc7SMartin Sperl 	if ((xfer->tx_buf) &&
3328caab75fSGeert Uytterhoeven 	    (xfer->tx_buf != ctlr->dummy_tx))
3336598b91bSDavid Jander 		u64_stats_add(&stats->bytes_tx, xfer->len);
334eca2ebc7SMartin Sperl 	if ((xfer->rx_buf) &&
3358caab75fSGeert Uytterhoeven 	    (xfer->rx_buf != ctlr->dummy_rx))
3366598b91bSDavid Jander 		u64_stats_add(&stats->bytes_rx, xfer->len);
337eca2ebc7SMartin Sperl 
3386598b91bSDavid Jander 	u64_stats_update_end(&stats->syncp);
33967b9d641SDavid Jander 	put_cpu();
340eca2ebc7SMartin Sperl }
3418ae12a0dSDavid Brownell 
342350de7ceSAndy Shevchenko /*
343350de7ceSAndy Shevchenko  * modalias support makes "modprobe $MODALIAS" new-style hotplug work,
3448ae12a0dSDavid Brownell  * and the sysfs version makes coldplug work too.
3458ae12a0dSDavid Brownell  */
3463f076575SAndy Shevchenko static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, const char *name)
34775368bf6SAnton Vorontsov {
34875368bf6SAnton Vorontsov 	while (id->name[0]) {
3493f076575SAndy Shevchenko 		if (!strcmp(name, id->name))
35075368bf6SAnton Vorontsov 			return id;
35175368bf6SAnton Vorontsov 		id++;
35275368bf6SAnton Vorontsov 	}
35375368bf6SAnton Vorontsov 	return NULL;
35475368bf6SAnton Vorontsov }
35575368bf6SAnton Vorontsov 
35675368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
35775368bf6SAnton Vorontsov {
35875368bf6SAnton Vorontsov 	const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
35975368bf6SAnton Vorontsov 
3603f076575SAndy Shevchenko 	return spi_match_id(sdrv->id_table, sdev->modalias);
36175368bf6SAnton Vorontsov }
36275368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id);
36375368bf6SAnton Vorontsov 
364aea672d0SAndy Shevchenko const void *spi_get_device_match_data(const struct spi_device *sdev)
365aea672d0SAndy Shevchenko {
366aea672d0SAndy Shevchenko 	const void *match;
367aea672d0SAndy Shevchenko 
368aea672d0SAndy Shevchenko 	match = device_get_match_data(&sdev->dev);
369aea672d0SAndy Shevchenko 	if (match)
370aea672d0SAndy Shevchenko 		return match;
371aea672d0SAndy Shevchenko 
372aea672d0SAndy Shevchenko 	return (const void *)spi_get_device_id(sdev)->driver_data;
373aea672d0SAndy Shevchenko }
374aea672d0SAndy Shevchenko EXPORT_SYMBOL_GPL(spi_get_device_match_data);
375aea672d0SAndy Shevchenko 
3768ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv)
3778ae12a0dSDavid Brownell {
3788ae12a0dSDavid Brownell 	const struct spi_device	*spi = to_spi_device(dev);
37975368bf6SAnton Vorontsov 	const struct spi_driver	*sdrv = to_spi_driver(drv);
38075368bf6SAnton Vorontsov 
3815039563eSTrent Piepho 	/* Check override first, and if set, only use the named driver */
3825039563eSTrent Piepho 	if (spi->driver_override)
3835039563eSTrent Piepho 		return strcmp(spi->driver_override, drv->name) == 0;
3845039563eSTrent Piepho 
3852b7a32f7SSinan Akman 	/* Attempt an OF style match */
3862b7a32f7SSinan Akman 	if (of_driver_match_device(dev, drv))
3872b7a32f7SSinan Akman 		return 1;
3882b7a32f7SSinan Akman 
38964bee4d2SMika Westerberg 	/* Then try ACPI */
39064bee4d2SMika Westerberg 	if (acpi_driver_match_device(dev, drv))
39164bee4d2SMika Westerberg 		return 1;
39264bee4d2SMika Westerberg 
39375368bf6SAnton Vorontsov 	if (sdrv->id_table)
3943f076575SAndy Shevchenko 		return !!spi_match_id(sdrv->id_table, spi->modalias);
3958ae12a0dSDavid Brownell 
39635f74fcaSKay Sievers 	return strcmp(spi->modalias, drv->name) == 0;
3978ae12a0dSDavid Brownell }
3988ae12a0dSDavid Brownell 
3992a81ada3SGreg Kroah-Hartman static int spi_uevent(const struct device *dev, struct kobj_uevent_env *env)
4008ae12a0dSDavid Brownell {
4018ae12a0dSDavid Brownell 	const struct spi_device		*spi = to_spi_device(dev);
4028c4ff6d0SZhang Rui 	int rc;
4038c4ff6d0SZhang Rui 
4048c4ff6d0SZhang Rui 	rc = acpi_device_uevent_modalias(dev, env);
4058c4ff6d0SZhang Rui 	if (rc != -ENODEV)
4068c4ff6d0SZhang Rui 		return rc;
4078ae12a0dSDavid Brownell 
4082856670fSAndy Shevchenko 	return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
4098ae12a0dSDavid Brownell }
4108ae12a0dSDavid Brownell 
4119db34ee6SUwe Kleine-König static int spi_probe(struct device *dev)
412b885244eSDavid Brownell {
413b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
41444af7927SJon Hunter 	struct spi_device		*spi = to_spi_device(dev);
41533cf00e5SMika Westerberg 	int ret;
416b885244eSDavid Brownell 
41786be408bSSylwester Nawrocki 	ret = of_clk_set_defaults(dev->of_node, false);
41886be408bSSylwester Nawrocki 	if (ret)
41986be408bSSylwester Nawrocki 		return ret;
42086be408bSSylwester Nawrocki 
42144af7927SJon Hunter 	if (dev->of_node) {
42244af7927SJon Hunter 		spi->irq = of_irq_get(dev->of_node, 0);
42344af7927SJon Hunter 		if (spi->irq == -EPROBE_DEFER)
42444af7927SJon Hunter 			return -EPROBE_DEFER;
42544af7927SJon Hunter 		if (spi->irq < 0)
42644af7927SJon Hunter 			spi->irq = 0;
42744af7927SJon Hunter 	}
42844af7927SJon Hunter 
429676e7c25SUlf Hansson 	ret = dev_pm_domain_attach(dev, true);
43071f277a7SUlf Hansson 	if (ret)
43171f277a7SUlf Hansson 		return ret;
43271f277a7SUlf Hansson 
433440408dbSUwe Kleine-König 	if (sdrv->probe) {
43444af7927SJon Hunter 		ret = sdrv->probe(spi);
43533cf00e5SMika Westerberg 		if (ret)
436676e7c25SUlf Hansson 			dev_pm_domain_detach(dev, true);
437440408dbSUwe Kleine-König 	}
43833cf00e5SMika Westerberg 
43933cf00e5SMika Westerberg 	return ret;
440b885244eSDavid Brownell }
441b885244eSDavid Brownell 
442fc7a6209SUwe Kleine-König static void spi_remove(struct device *dev)
443b885244eSDavid Brownell {
444b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
445b885244eSDavid Brownell 
446a0386bbaSUwe Kleine-König 	if (sdrv->remove)
447a0386bbaSUwe Kleine-König 		sdrv->remove(to_spi_device(dev));
4487795d475SUwe Kleine-König 
449676e7c25SUlf Hansson 	dev_pm_domain_detach(dev, true);
450b885244eSDavid Brownell }
451b885244eSDavid Brownell 
4529db34ee6SUwe Kleine-König static void spi_shutdown(struct device *dev)
453b885244eSDavid Brownell {
454a6f483b2SMarek Szyprowski 	if (dev->driver) {
455b885244eSDavid Brownell 		const struct spi_driver	*sdrv = to_spi_driver(dev->driver);
456b885244eSDavid Brownell 
4579db34ee6SUwe Kleine-König 		if (sdrv->shutdown)
458b885244eSDavid Brownell 			sdrv->shutdown(to_spi_device(dev));
459b885244eSDavid Brownell 	}
460a6f483b2SMarek Szyprowski }
461b885244eSDavid Brownell 
4629db34ee6SUwe Kleine-König struct bus_type spi_bus_type = {
4639db34ee6SUwe Kleine-König 	.name		= "spi",
4649db34ee6SUwe Kleine-König 	.dev_groups	= spi_dev_groups,
4659db34ee6SUwe Kleine-König 	.match		= spi_match_device,
4669db34ee6SUwe Kleine-König 	.uevent		= spi_uevent,
4679db34ee6SUwe Kleine-König 	.probe		= spi_probe,
4689db34ee6SUwe Kleine-König 	.remove		= spi_remove,
4699db34ee6SUwe Kleine-König 	.shutdown	= spi_shutdown,
4709db34ee6SUwe Kleine-König };
4719db34ee6SUwe Kleine-König EXPORT_SYMBOL_GPL(spi_bus_type);
4729db34ee6SUwe Kleine-König 
47333e34dc6SDavid Brownell /**
474ca5d2485SAndrew F. Davis  * __spi_register_driver - register a SPI driver
47588c9321dSThierry Reding  * @owner: owner module of the driver to register
47633e34dc6SDavid Brownell  * @sdrv: the driver to register
47733e34dc6SDavid Brownell  * Context: can sleep
47897d56dc6SJavier Martinez Canillas  *
47997d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
48033e34dc6SDavid Brownell  */
481ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
482b885244eSDavid Brownell {
483ca5d2485SAndrew F. Davis 	sdrv->driver.owner = owner;
484b885244eSDavid Brownell 	sdrv->driver.bus = &spi_bus_type;
4855fa6863bSMark Brown 
4865fa6863bSMark Brown 	/*
4875fa6863bSMark Brown 	 * For Really Good Reasons we use spi: modaliases not of:
4885fa6863bSMark Brown 	 * modaliases for DT so module autoloading won't work if we
4895fa6863bSMark Brown 	 * don't have a spi_device_id as well as a compatible string.
4905fa6863bSMark Brown 	 */
4915fa6863bSMark Brown 	if (sdrv->driver.of_match_table) {
4925fa6863bSMark Brown 		const struct of_device_id *of_id;
4935fa6863bSMark Brown 
4945fa6863bSMark Brown 		for (of_id = sdrv->driver.of_match_table; of_id->compatible[0];
4955fa6863bSMark Brown 		     of_id++) {
4965fa6863bSMark Brown 			const char *of_name;
4975fa6863bSMark Brown 
4985fa6863bSMark Brown 			/* Strip off any vendor prefix */
4995fa6863bSMark Brown 			of_name = strnchr(of_id->compatible,
5005fa6863bSMark Brown 					  sizeof(of_id->compatible), ',');
5015fa6863bSMark Brown 			if (of_name)
5025fa6863bSMark Brown 				of_name++;
5035fa6863bSMark Brown 			else
5045fa6863bSMark Brown 				of_name = of_id->compatible;
5055fa6863bSMark Brown 
5065fa6863bSMark Brown 			if (sdrv->id_table) {
5075fa6863bSMark Brown 				const struct spi_device_id *spi_id;
5085fa6863bSMark Brown 
5093f076575SAndy Shevchenko 				spi_id = spi_match_id(sdrv->id_table, of_name);
510b79332efSAndy Shevchenko 				if (spi_id)
5115fa6863bSMark Brown 					continue;
5125fa6863bSMark Brown 			} else {
5135fa6863bSMark Brown 				if (strcmp(sdrv->driver.name, of_name) == 0)
5145fa6863bSMark Brown 					continue;
5155fa6863bSMark Brown 			}
5165fa6863bSMark Brown 
5175fa6863bSMark Brown 			pr_warn("SPI driver %s has no spi_device_id for %s\n",
5185fa6863bSMark Brown 				sdrv->driver.name, of_id->compatible);
5195fa6863bSMark Brown 		}
5205fa6863bSMark Brown 	}
5215fa6863bSMark Brown 
522b885244eSDavid Brownell 	return driver_register(&sdrv->driver);
523b885244eSDavid Brownell }
524ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver);
525b885244eSDavid Brownell 
5268ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
5278ae12a0dSDavid Brownell 
528350de7ceSAndy Shevchenko /*
529350de7ceSAndy Shevchenko  * SPI devices should normally not be created by SPI device drivers; that
5308caab75fSGeert Uytterhoeven  * would make them board-specific.  Similarly with SPI controller drivers.
5318ae12a0dSDavid Brownell  * Device registration normally goes into like arch/.../mach.../board-YYY.c
5328ae12a0dSDavid Brownell  * with other readonly (flashable) information about mainboard devices.
5338ae12a0dSDavid Brownell  */
5348ae12a0dSDavid Brownell 
5358ae12a0dSDavid Brownell struct boardinfo {
5368ae12a0dSDavid Brownell 	struct list_head	list;
5372b9603a0SFeng Tang 	struct spi_board_info	board_info;
5388ae12a0dSDavid Brownell };
5398ae12a0dSDavid Brownell 
5408ae12a0dSDavid Brownell static LIST_HEAD(board_list);
5418caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list);
5422b9603a0SFeng Tang 
5432b9603a0SFeng Tang /*
544be73e323SAndy Shevchenko  * Used to protect add/del operation for board_info list and
545350de7ceSAndy Shevchenko  * spi_controller list, and their matching process also used
546350de7ceSAndy Shevchenko  * to protect object of type struct idr.
5472b9603a0SFeng Tang  */
54894040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock);
5498ae12a0dSDavid Brownell 
550dc87c98eSGrant Likely /**
551dc87c98eSGrant Likely  * spi_alloc_device - Allocate a new SPI device
5528caab75fSGeert Uytterhoeven  * @ctlr: Controller to which device is connected
553dc87c98eSGrant Likely  * Context: can sleep
554dc87c98eSGrant Likely  *
555dc87c98eSGrant Likely  * Allows a driver to allocate and initialize a spi_device without
556dc87c98eSGrant Likely  * registering it immediately.  This allows a driver to directly
557dc87c98eSGrant Likely  * fill the spi_device with device parameters before calling
558dc87c98eSGrant Likely  * spi_add_device() on it.
559dc87c98eSGrant Likely  *
560dc87c98eSGrant Likely  * Caller is responsible to call spi_add_device() on the returned
5618caab75fSGeert Uytterhoeven  * spi_device structure to add it to the SPI controller.  If the caller
562dc87c98eSGrant Likely  * needs to discard the spi_device without adding it, then it should
563dc87c98eSGrant Likely  * call spi_dev_put() on it.
564dc87c98eSGrant Likely  *
56597d56dc6SJavier Martinez Canillas  * Return: a pointer to the new device, or NULL.
566dc87c98eSGrant Likely  */
567e3dc1399SStefan Binding struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
568dc87c98eSGrant Likely {
569dc87c98eSGrant Likely 	struct spi_device	*spi;
570dc87c98eSGrant Likely 
5718caab75fSGeert Uytterhoeven 	if (!spi_controller_get(ctlr))
572dc87c98eSGrant Likely 		return NULL;
573dc87c98eSGrant Likely 
5745fe5f05eSJingoo Han 	spi = kzalloc(sizeof(*spi), GFP_KERNEL);
575dc87c98eSGrant Likely 	if (!spi) {
5768caab75fSGeert Uytterhoeven 		spi_controller_put(ctlr);
577dc87c98eSGrant Likely 		return NULL;
578dc87c98eSGrant Likely 	}
579dc87c98eSGrant Likely 
5806598b91bSDavid Jander 	spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL);
5816598b91bSDavid Jander 	if (!spi->pcpu_statistics) {
5826598b91bSDavid Jander 		kfree(spi);
5836598b91bSDavid Jander 		spi_controller_put(ctlr);
5846598b91bSDavid Jander 		return NULL;
5856598b91bSDavid Jander 	}
5866598b91bSDavid Jander 
5878caab75fSGeert Uytterhoeven 	spi->master = spi->controller = ctlr;
5888caab75fSGeert Uytterhoeven 	spi->dev.parent = &ctlr->dev;
589dc87c98eSGrant Likely 	spi->dev.bus = &spi_bus_type;
590dc87c98eSGrant Likely 	spi->dev.release = spidev_release;
591ea235786SJohn Garry 	spi->mode = ctlr->buswidth_override_bits;
592eca2ebc7SMartin Sperl 
593dc87c98eSGrant Likely 	device_initialize(&spi->dev);
594dc87c98eSGrant Likely 	return spi;
595dc87c98eSGrant Likely }
596e3dc1399SStefan Binding EXPORT_SYMBOL_GPL(spi_alloc_device);
597dc87c98eSGrant Likely 
598e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi)
599e13ac47bSJarkko Nikula {
600e13ac47bSJarkko Nikula 	struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
601e13ac47bSJarkko Nikula 
602e13ac47bSJarkko Nikula 	if (adev) {
603e13ac47bSJarkko Nikula 		dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
604e13ac47bSJarkko Nikula 		return;
605e13ac47bSJarkko Nikula 	}
606e13ac47bSJarkko Nikula 
6078caab75fSGeert Uytterhoeven 	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
608303feb3cSAmit Kumar Mahapatra 		     spi_get_chipselect(spi, 0));
609e13ac47bSJarkko Nikula }
610e13ac47bSJarkko Nikula 
611b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data)
612b6fb8d3aSMika Westerberg {
613b6fb8d3aSMika Westerberg 	struct spi_device *spi = to_spi_device(dev);
614b6fb8d3aSMika Westerberg 	struct spi_device *new_spi = data;
6154d8ff6b0SAmit Kumar Mahapatra 	int idx, nw_idx;
6164d8ff6b0SAmit Kumar Mahapatra 	u8 cs, cs_nw;
617b6fb8d3aSMika Westerberg 
6184d8ff6b0SAmit Kumar Mahapatra 	if (spi->controller == new_spi->controller) {
6194d8ff6b0SAmit Kumar Mahapatra 		for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
6204d8ff6b0SAmit Kumar Mahapatra 			cs = spi_get_chipselect(spi, idx);
6214d8ff6b0SAmit Kumar Mahapatra 			for (nw_idx = 0; nw_idx < SPI_CS_CNT_MAX; nw_idx++) {
6224d8ff6b0SAmit Kumar Mahapatra 				cs_nw = spi_get_chipselect(new_spi, nw_idx);
6234d8ff6b0SAmit Kumar Mahapatra 				if (cs != 0xFF && cs_nw != 0xFF && cs == cs_nw) {
6244d8ff6b0SAmit Kumar Mahapatra 					dev_err(dev, "chipselect %d already in use\n", cs_nw);
625b6fb8d3aSMika Westerberg 					return -EBUSY;
6264d8ff6b0SAmit Kumar Mahapatra 				}
6274d8ff6b0SAmit Kumar Mahapatra 			}
6284d8ff6b0SAmit Kumar Mahapatra 		}
6294d8ff6b0SAmit Kumar Mahapatra 	}
630b6fb8d3aSMika Westerberg 	return 0;
631b6fb8d3aSMika Westerberg }
632b6fb8d3aSMika Westerberg 
633c7299feaSSaravana Kannan static void spi_cleanup(struct spi_device *spi)
634c7299feaSSaravana Kannan {
635c7299feaSSaravana Kannan 	if (spi->controller->cleanup)
636c7299feaSSaravana Kannan 		spi->controller->cleanup(spi);
637c7299feaSSaravana Kannan }
638c7299feaSSaravana Kannan 
6390c79378cSSebastian Reichel static int __spi_add_device(struct spi_device *spi)
6400c79378cSSebastian Reichel {
6410c79378cSSebastian Reichel 	struct spi_controller *ctlr = spi->controller;
6420c79378cSSebastian Reichel 	struct device *dev = ctlr->dev.parent;
6434d8ff6b0SAmit Kumar Mahapatra 	int status, idx, nw_idx;
6444d8ff6b0SAmit Kumar Mahapatra 	u8 cs, nw_cs;
6450c79378cSSebastian Reichel 
6464d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
64736124deaSAndy Shevchenko 		/* Chipselects are numbered 0..max; validate. */
6484d8ff6b0SAmit Kumar Mahapatra 		cs = spi_get_chipselect(spi, idx);
6494d8ff6b0SAmit Kumar Mahapatra 		if (cs != 0xFF && cs >= ctlr->num_chipselect) {
6504d8ff6b0SAmit Kumar Mahapatra 			dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx),
65136124deaSAndy Shevchenko 				ctlr->num_chipselect);
65236124deaSAndy Shevchenko 			return -EINVAL;
65336124deaSAndy Shevchenko 		}
6544d8ff6b0SAmit Kumar Mahapatra 	}
6554d8ff6b0SAmit Kumar Mahapatra 
6564d8ff6b0SAmit Kumar Mahapatra 	/*
6574d8ff6b0SAmit Kumar Mahapatra 	 * Make sure that multiple logical CS doesn't map to the same physical CS.
6584d8ff6b0SAmit Kumar Mahapatra 	 * For example, spi->chip_select[0] != spi->chip_select[1] and so on.
6594d8ff6b0SAmit Kumar Mahapatra 	 */
6604d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
6614d8ff6b0SAmit Kumar Mahapatra 		cs = spi_get_chipselect(spi, idx);
6624d8ff6b0SAmit Kumar Mahapatra 		for (nw_idx = idx + 1; nw_idx < SPI_CS_CNT_MAX; nw_idx++) {
6634d8ff6b0SAmit Kumar Mahapatra 			nw_cs = spi_get_chipselect(spi, nw_idx);
6644d8ff6b0SAmit Kumar Mahapatra 			if (cs != 0xFF && nw_cs != 0xFF && cs == nw_cs) {
6654d8ff6b0SAmit Kumar Mahapatra 				dev_err(dev, "chipselect %d already in use\n", nw_cs);
6664d8ff6b0SAmit Kumar Mahapatra 				return -EBUSY;
6674d8ff6b0SAmit Kumar Mahapatra 			}
6684d8ff6b0SAmit Kumar Mahapatra 		}
6694d8ff6b0SAmit Kumar Mahapatra 	}
67036124deaSAndy Shevchenko 
67136124deaSAndy Shevchenko 	/* Set the bus ID string */
67236124deaSAndy Shevchenko 	spi_dev_set_name(spi);
67336124deaSAndy Shevchenko 
6746bfb15f3SUwe Kleine-König 	/*
6756bfb15f3SUwe Kleine-König 	 * We need to make sure there's no other device with this
6766bfb15f3SUwe Kleine-König 	 * chipselect **BEFORE** we call setup(), else we'll trash
6776bfb15f3SUwe Kleine-König 	 * its configuration.
6786bfb15f3SUwe Kleine-König 	 */
6790c79378cSSebastian Reichel 	status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
6804d8ff6b0SAmit Kumar Mahapatra 	if (status)
6810c79378cSSebastian Reichel 		return status;
6820c79378cSSebastian Reichel 
6830c79378cSSebastian Reichel 	/* Controller may unregister concurrently */
6840c79378cSSebastian Reichel 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC) &&
6850c79378cSSebastian Reichel 	    !device_is_registered(&ctlr->dev)) {
6860c79378cSSebastian Reichel 		return -ENODEV;
6870c79378cSSebastian Reichel 	}
6880c79378cSSebastian Reichel 
6894d8ff6b0SAmit Kumar Mahapatra 	if (ctlr->cs_gpiods) {
6904d8ff6b0SAmit Kumar Mahapatra 		u8 cs;
6914d8ff6b0SAmit Kumar Mahapatra 
6924d8ff6b0SAmit Kumar Mahapatra 		for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
6934d8ff6b0SAmit Kumar Mahapatra 			cs = spi_get_chipselect(spi, idx);
6944d8ff6b0SAmit Kumar Mahapatra 			if (cs != 0xFF)
6954d8ff6b0SAmit Kumar Mahapatra 				spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]);
6964d8ff6b0SAmit Kumar Mahapatra 		}
6974d8ff6b0SAmit Kumar Mahapatra 	}
6980c79378cSSebastian Reichel 
699350de7ceSAndy Shevchenko 	/*
700350de7ceSAndy Shevchenko 	 * Drivers may modify this initial i/o setup, but will
7010c79378cSSebastian Reichel 	 * normally rely on the device being setup.  Devices
7020c79378cSSebastian Reichel 	 * using SPI_CS_HIGH can't coexist well otherwise...
7030c79378cSSebastian Reichel 	 */
7040c79378cSSebastian Reichel 	status = spi_setup(spi);
7050c79378cSSebastian Reichel 	if (status < 0) {
7060c79378cSSebastian Reichel 		dev_err(dev, "can't setup %s, status %d\n",
7070c79378cSSebastian Reichel 				dev_name(&spi->dev), status);
7080c79378cSSebastian Reichel 		return status;
7090c79378cSSebastian Reichel 	}
7100c79378cSSebastian Reichel 
7110c79378cSSebastian Reichel 	/* Device may be bound to an active driver when this returns */
7120c79378cSSebastian Reichel 	status = device_add(&spi->dev);
7130c79378cSSebastian Reichel 	if (status < 0) {
7140c79378cSSebastian Reichel 		dev_err(dev, "can't add %s, status %d\n",
7150c79378cSSebastian Reichel 				dev_name(&spi->dev), status);
7160c79378cSSebastian Reichel 		spi_cleanup(spi);
7170c79378cSSebastian Reichel 	} else {
7180c79378cSSebastian Reichel 		dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
7190c79378cSSebastian Reichel 	}
7200c79378cSSebastian Reichel 
7210c79378cSSebastian Reichel 	return status;
7220c79378cSSebastian Reichel }
7230c79378cSSebastian Reichel 
724dc87c98eSGrant Likely /**
725dc87c98eSGrant Likely  * spi_add_device - Add spi_device allocated with spi_alloc_device
726dc87c98eSGrant Likely  * @spi: spi_device to register
727dc87c98eSGrant Likely  *
728dc87c98eSGrant Likely  * Companion function to spi_alloc_device.  Devices allocated with
729702ca026SAndy Shevchenko  * spi_alloc_device can be added onto the SPI bus with this function.
730dc87c98eSGrant Likely  *
73197d56dc6SJavier Martinez Canillas  * Return: 0 on success; negative errno on failure
732dc87c98eSGrant Likely  */
733e3dc1399SStefan Binding int spi_add_device(struct spi_device *spi)
734dc87c98eSGrant Likely {
7358caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
736dc87c98eSGrant Likely 	int status;
737dc87c98eSGrant Likely 
7384d8ff6b0SAmit Kumar Mahapatra 	/* Set the bus ID string */
7394d8ff6b0SAmit Kumar Mahapatra 	spi_dev_set_name(spi);
7404d8ff6b0SAmit Kumar Mahapatra 
7416098475dSMark Brown 	mutex_lock(&ctlr->add_lock);
7420c79378cSSebastian Reichel 	status = __spi_add_device(spi);
7436098475dSMark Brown 	mutex_unlock(&ctlr->add_lock);
744e48880e0SDavid Brownell 	return status;
745dc87c98eSGrant Likely }
746e3dc1399SStefan Binding EXPORT_SYMBOL_GPL(spi_add_device);
7478ae12a0dSDavid Brownell 
74833e34dc6SDavid Brownell /**
74933e34dc6SDavid Brownell  * spi_new_device - instantiate one new SPI device
7508caab75fSGeert Uytterhoeven  * @ctlr: Controller to which device is connected
75133e34dc6SDavid Brownell  * @chip: Describes the SPI device
75233e34dc6SDavid Brownell  * Context: can sleep
75333e34dc6SDavid Brownell  *
75433e34dc6SDavid Brownell  * On typical mainboards, this is purely internal; and it's not needed
7558ae12a0dSDavid Brownell  * after board init creates the hard-wired devices.  Some development
7568ae12a0dSDavid Brownell  * platforms may not be able to use spi_register_board_info though, and
7578ae12a0dSDavid Brownell  * this is exported so that for example a USB or parport based adapter
7588ae12a0dSDavid Brownell  * driver could add devices (which it would learn about out-of-band).
759082c8cb4SDavid Brownell  *
76097d56dc6SJavier Martinez Canillas  * Return: the new device, or NULL.
7618ae12a0dSDavid Brownell  */
7628caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr,
763e9d5a461SAdrian Bunk 				  struct spi_board_info *chip)
7648ae12a0dSDavid Brownell {
7658ae12a0dSDavid Brownell 	struct spi_device	*proxy;
7668ae12a0dSDavid Brownell 	int			status;
7674d8ff6b0SAmit Kumar Mahapatra 	u8                      idx;
7688ae12a0dSDavid Brownell 
769350de7ceSAndy Shevchenko 	/*
770350de7ceSAndy Shevchenko 	 * NOTE:  caller did any chip->bus_num checks necessary.
771082c8cb4SDavid Brownell 	 *
772082c8cb4SDavid Brownell 	 * Also, unless we change the return value convention to use
773082c8cb4SDavid Brownell 	 * error-or-pointer (not NULL-or-pointer), troubleshootability
774082c8cb4SDavid Brownell 	 * suggests syslogged diagnostics are best here (ugh).
775082c8cb4SDavid Brownell 	 */
776082c8cb4SDavid Brownell 
7778caab75fSGeert Uytterhoeven 	proxy = spi_alloc_device(ctlr);
778dc87c98eSGrant Likely 	if (!proxy)
7798ae12a0dSDavid Brownell 		return NULL;
7808ae12a0dSDavid Brownell 
781102eb975SGrant Likely 	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
782102eb975SGrant Likely 
7834d8ff6b0SAmit Kumar Mahapatra 	/*
7844d8ff6b0SAmit Kumar Mahapatra 	 * Zero(0) is a valid physical CS value and can be located at any
7854d8ff6b0SAmit Kumar Mahapatra 	 * logical CS in the spi->chip_select[]. If all the physical CS
7864d8ff6b0SAmit Kumar Mahapatra 	 * are initialized to 0 then It would be difficult to differentiate
7874d8ff6b0SAmit Kumar Mahapatra 	 * between a valid physical CS 0 & an unused logical CS whose physical
7884d8ff6b0SAmit Kumar Mahapatra 	 * CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
7894d8ff6b0SAmit Kumar Mahapatra 	 * Now all the unused logical CS will have 0xFF physical CS value & can be
7904d8ff6b0SAmit Kumar Mahapatra 	 * ignore while performing physical CS validity checks.
7914d8ff6b0SAmit Kumar Mahapatra 	 */
7924d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
7934d8ff6b0SAmit Kumar Mahapatra 		spi_set_chipselect(proxy, idx, 0xFF);
7944d8ff6b0SAmit Kumar Mahapatra 
795303feb3cSAmit Kumar Mahapatra 	spi_set_chipselect(proxy, 0, chip->chip_select);
7968ae12a0dSDavid Brownell 	proxy->max_speed_hz = chip->max_speed_hz;
797980a01c9SDavid Brownell 	proxy->mode = chip->mode;
7988ae12a0dSDavid Brownell 	proxy->irq = chip->irq;
79951e99de5SWolfram Sang 	strscpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
8008ae12a0dSDavid Brownell 	proxy->dev.platform_data = (void *) chip->platform_data;
8018ae12a0dSDavid Brownell 	proxy->controller_data = chip->controller_data;
8028ae12a0dSDavid Brownell 	proxy->controller_state = NULL;
8034d8ff6b0SAmit Kumar Mahapatra 	/*
8044d8ff6b0SAmit Kumar Mahapatra 	 * spi->chip_select[i] gives the corresponding physical CS for logical CS i
8054d8ff6b0SAmit Kumar Mahapatra 	 * logical CS number is represented by setting the ith bit in spi->cs_index_mask
8064d8ff6b0SAmit Kumar Mahapatra 	 * So, for example, if spi->cs_index_mask = 0x01 then logical CS number is 0 and
8074d8ff6b0SAmit Kumar Mahapatra 	 * spi->chip_select[0] will give the physical CS.
8084d8ff6b0SAmit Kumar Mahapatra 	 * By default spi->chip_select[0] will hold the physical CS number so, set
8094d8ff6b0SAmit Kumar Mahapatra 	 * spi->cs_index_mask as 0x01.
8104d8ff6b0SAmit Kumar Mahapatra 	 */
8114d8ff6b0SAmit Kumar Mahapatra 	proxy->cs_index_mask = 0x01;
8128ae12a0dSDavid Brownell 
81347afc77bSHeikki Krogerus 	if (chip->swnode) {
81447afc77bSHeikki Krogerus 		status = device_add_software_node(&proxy->dev, chip->swnode);
815826cf175SDmitry Torokhov 		if (status) {
8169d902c2aSColin Ian King 			dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n",
817826cf175SDmitry Torokhov 				chip->modalias, status);
818826cf175SDmitry Torokhov 			goto err_dev_put;
819826cf175SDmitry Torokhov 		}
8208ae12a0dSDavid Brownell 	}
821dc87c98eSGrant Likely 
822826cf175SDmitry Torokhov 	status = spi_add_device(proxy);
823826cf175SDmitry Torokhov 	if (status < 0)
824df41a5daSHeikki Krogerus 		goto err_dev_put;
825826cf175SDmitry Torokhov 
826dc87c98eSGrant Likely 	return proxy;
827826cf175SDmitry Torokhov 
828826cf175SDmitry Torokhov err_dev_put:
829df41a5daSHeikki Krogerus 	device_remove_software_node(&proxy->dev);
830826cf175SDmitry Torokhov 	spi_dev_put(proxy);
831826cf175SDmitry Torokhov 	return NULL;
832dc87c98eSGrant Likely }
8338ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device);
8348ae12a0dSDavid Brownell 
8353b1884c2SGeert Uytterhoeven /**
8363b1884c2SGeert Uytterhoeven  * spi_unregister_device - unregister a single SPI device
8373b1884c2SGeert Uytterhoeven  * @spi: spi_device to unregister
8383b1884c2SGeert Uytterhoeven  *
8393b1884c2SGeert Uytterhoeven  * Start making the passed SPI device vanish. Normally this would be handled
8408caab75fSGeert Uytterhoeven  * by spi_unregister_controller().
8413b1884c2SGeert Uytterhoeven  */
8423b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi)
8433b1884c2SGeert Uytterhoeven {
844bd6c1644SGeert Uytterhoeven 	if (!spi)
845bd6c1644SGeert Uytterhoeven 		return;
846bd6c1644SGeert Uytterhoeven 
8478324147fSJohan Hovold 	if (spi->dev.of_node) {
848bd6c1644SGeert Uytterhoeven 		of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
8498324147fSJohan Hovold 		of_node_put(spi->dev.of_node);
8508324147fSJohan Hovold 	}
8517f24467fSOctavian Purdila 	if (ACPI_COMPANION(&spi->dev))
8527f24467fSOctavian Purdila 		acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
85347afc77bSHeikki Krogerus 	device_remove_software_node(&spi->dev);
85427e7db56SSaravana Kannan 	device_del(&spi->dev);
85527e7db56SSaravana Kannan 	spi_cleanup(spi);
85627e7db56SSaravana Kannan 	put_device(&spi->dev);
8573b1884c2SGeert Uytterhoeven }
8583b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device);
8593b1884c2SGeert Uytterhoeven 
8608caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
8612b9603a0SFeng Tang 					      struct spi_board_info *bi)
8622b9603a0SFeng Tang {
8632b9603a0SFeng Tang 	struct spi_device *dev;
8642b9603a0SFeng Tang 
8658caab75fSGeert Uytterhoeven 	if (ctlr->bus_num != bi->bus_num)
8662b9603a0SFeng Tang 		return;
8672b9603a0SFeng Tang 
8688caab75fSGeert Uytterhoeven 	dev = spi_new_device(ctlr, bi);
8692b9603a0SFeng Tang 	if (!dev)
8708caab75fSGeert Uytterhoeven 		dev_err(ctlr->dev.parent, "can't create new device for %s\n",
8712b9603a0SFeng Tang 			bi->modalias);
8722b9603a0SFeng Tang }
8732b9603a0SFeng Tang 
87433e34dc6SDavid Brownell /**
87533e34dc6SDavid Brownell  * spi_register_board_info - register SPI devices for a given board
87633e34dc6SDavid Brownell  * @info: array of chip descriptors
87733e34dc6SDavid Brownell  * @n: how many descriptors are provided
87833e34dc6SDavid Brownell  * Context: can sleep
87933e34dc6SDavid Brownell  *
8808ae12a0dSDavid Brownell  * Board-specific early init code calls this (probably during arch_initcall)
8818ae12a0dSDavid Brownell  * with segments of the SPI device table.  Any device nodes are created later,
8828ae12a0dSDavid Brownell  * after the relevant parent SPI controller (bus_num) is defined.  We keep
8838ae12a0dSDavid Brownell  * this table of devices forever, so that reloading a controller driver will
8848ae12a0dSDavid Brownell  * not make Linux forget about these hard-wired devices.
8858ae12a0dSDavid Brownell  *
8868ae12a0dSDavid Brownell  * Other code can also call this, e.g. a particular add-on board might provide
8878ae12a0dSDavid Brownell  * SPI devices through its expansion connector, so code initializing that board
8888ae12a0dSDavid Brownell  * would naturally declare its SPI devices.
8898ae12a0dSDavid Brownell  *
8908ae12a0dSDavid Brownell  * The board info passed can safely be __initdata ... but be careful of
8918ae12a0dSDavid Brownell  * any embedded pointers (platform_data, etc), they're copied as-is.
89297d56dc6SJavier Martinez Canillas  *
89397d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
8948ae12a0dSDavid Brownell  */
895fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n)
8968ae12a0dSDavid Brownell {
8978ae12a0dSDavid Brownell 	struct boardinfo *bi;
8982b9603a0SFeng Tang 	int i;
8998ae12a0dSDavid Brownell 
900c7908a37SXiubo Li 	if (!n)
901f974cf57SDmitry Torokhov 		return 0;
902c7908a37SXiubo Li 
903f9bdb7fdSMarkus Elfring 	bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
9048ae12a0dSDavid Brownell 	if (!bi)
9058ae12a0dSDavid Brownell 		return -ENOMEM;
9068ae12a0dSDavid Brownell 
9072b9603a0SFeng Tang 	for (i = 0; i < n; i++, bi++, info++) {
9088caab75fSGeert Uytterhoeven 		struct spi_controller *ctlr;
9092b9603a0SFeng Tang 
9102b9603a0SFeng Tang 		memcpy(&bi->board_info, info, sizeof(*info));
911826cf175SDmitry Torokhov 
91294040828SMatthias Kaehlcke 		mutex_lock(&board_lock);
9138ae12a0dSDavid Brownell 		list_add_tail(&bi->list, &board_list);
9148caab75fSGeert Uytterhoeven 		list_for_each_entry(ctlr, &spi_controller_list, list)
9158caab75fSGeert Uytterhoeven 			spi_match_controller_to_boardinfo(ctlr,
9168caab75fSGeert Uytterhoeven 							  &bi->board_info);
91794040828SMatthias Kaehlcke 		mutex_unlock(&board_lock);
9182b9603a0SFeng Tang 	}
9192b9603a0SFeng Tang 
9208ae12a0dSDavid Brownell 	return 0;
9218ae12a0dSDavid Brownell }
9228ae12a0dSDavid Brownell 
9238ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
9248ae12a0dSDavid Brownell 
925fb51601bSUwe Kleine-König /* Core methods for SPI resource management */
926fb51601bSUwe Kleine-König 
927fb51601bSUwe Kleine-König /**
928fb51601bSUwe Kleine-König  * spi_res_alloc - allocate a spi resource that is life-cycle managed
929fb51601bSUwe Kleine-König  *                 during the processing of a spi_message while using
930fb51601bSUwe Kleine-König  *                 spi_transfer_one
931702ca026SAndy Shevchenko  * @spi:     the SPI device for which we allocate memory
932fb51601bSUwe Kleine-König  * @release: the release code to execute for this resource
933fb51601bSUwe Kleine-König  * @size:    size to alloc and return
934fb51601bSUwe Kleine-König  * @gfp:     GFP allocation flags
935fb51601bSUwe Kleine-König  *
936fb51601bSUwe Kleine-König  * Return: the pointer to the allocated data
937fb51601bSUwe Kleine-König  *
938fb51601bSUwe Kleine-König  * This may get enhanced in the future to allocate from a memory pool
939fb51601bSUwe Kleine-König  * of the @spi_device or @spi_controller to avoid repeated allocations.
940fb51601bSUwe Kleine-König  */
941da21fde0SUwe Kleine-König static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release,
942fb51601bSUwe Kleine-König 			   size_t size, gfp_t gfp)
943fb51601bSUwe Kleine-König {
944fb51601bSUwe Kleine-König 	struct spi_res *sres;
945fb51601bSUwe Kleine-König 
946fb51601bSUwe Kleine-König 	sres = kzalloc(sizeof(*sres) + size, gfp);
947fb51601bSUwe Kleine-König 	if (!sres)
948fb51601bSUwe Kleine-König 		return NULL;
949fb51601bSUwe Kleine-König 
950fb51601bSUwe Kleine-König 	INIT_LIST_HEAD(&sres->entry);
951fb51601bSUwe Kleine-König 	sres->release = release;
952fb51601bSUwe Kleine-König 
953fb51601bSUwe Kleine-König 	return sres->data;
954fb51601bSUwe Kleine-König }
955fb51601bSUwe Kleine-König 
956fb51601bSUwe Kleine-König /**
957702ca026SAndy Shevchenko  * spi_res_free - free an SPI resource
958fb51601bSUwe Kleine-König  * @res: pointer to the custom data of a resource
959fb51601bSUwe Kleine-König  */
960da21fde0SUwe Kleine-König static void spi_res_free(void *res)
961fb51601bSUwe Kleine-König {
962fb51601bSUwe Kleine-König 	struct spi_res *sres = container_of(res, struct spi_res, data);
963fb51601bSUwe Kleine-König 
964fb51601bSUwe Kleine-König 	if (!res)
965fb51601bSUwe Kleine-König 		return;
966fb51601bSUwe Kleine-König 
967fb51601bSUwe Kleine-König 	WARN_ON(!list_empty(&sres->entry));
968fb51601bSUwe Kleine-König 	kfree(sres);
969fb51601bSUwe Kleine-König }
970fb51601bSUwe Kleine-König 
971fb51601bSUwe Kleine-König /**
972fb51601bSUwe Kleine-König  * spi_res_add - add a spi_res to the spi_message
973702ca026SAndy Shevchenko  * @message: the SPI message
974fb51601bSUwe Kleine-König  * @res:     the spi_resource
975fb51601bSUwe Kleine-König  */
976da21fde0SUwe Kleine-König static void spi_res_add(struct spi_message *message, void *res)
977fb51601bSUwe Kleine-König {
978fb51601bSUwe Kleine-König 	struct spi_res *sres = container_of(res, struct spi_res, data);
979fb51601bSUwe Kleine-König 
980fb51601bSUwe Kleine-König 	WARN_ON(!list_empty(&sres->entry));
981fb51601bSUwe Kleine-König 	list_add_tail(&sres->entry, &message->resources);
982fb51601bSUwe Kleine-König }
983fb51601bSUwe Kleine-König 
984fb51601bSUwe Kleine-König /**
985702ca026SAndy Shevchenko  * spi_res_release - release all SPI resources for this message
986fb51601bSUwe Kleine-König  * @ctlr:  the @spi_controller
987fb51601bSUwe Kleine-König  * @message: the @spi_message
988fb51601bSUwe Kleine-König  */
989da21fde0SUwe Kleine-König static void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
990fb51601bSUwe Kleine-König {
991fb51601bSUwe Kleine-König 	struct spi_res *res, *tmp;
992fb51601bSUwe Kleine-König 
993fb51601bSUwe Kleine-König 	list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
994fb51601bSUwe Kleine-König 		if (res->release)
995fb51601bSUwe Kleine-König 			res->release(ctlr, message, res->data);
996fb51601bSUwe Kleine-König 
997fb51601bSUwe Kleine-König 		list_del(&res->entry);
998fb51601bSUwe Kleine-König 
999fb51601bSUwe Kleine-König 		kfree(res);
1000fb51601bSUwe Kleine-König 	}
1001fb51601bSUwe Kleine-König }
1002fb51601bSUwe Kleine-König 
1003fb51601bSUwe Kleine-König /*-------------------------------------------------------------------------*/
10044d8ff6b0SAmit Kumar Mahapatra static inline bool spi_is_last_cs(struct spi_device *spi)
10054d8ff6b0SAmit Kumar Mahapatra {
10064d8ff6b0SAmit Kumar Mahapatra 	u8 idx;
10074d8ff6b0SAmit Kumar Mahapatra 	bool last = false;
10084d8ff6b0SAmit Kumar Mahapatra 
10094d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
10104d8ff6b0SAmit Kumar Mahapatra 		if ((spi->cs_index_mask >> idx) & 0x01) {
10114d8ff6b0SAmit Kumar Mahapatra 			if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx))
10124d8ff6b0SAmit Kumar Mahapatra 				last = true;
10134d8ff6b0SAmit Kumar Mahapatra 		}
10144d8ff6b0SAmit Kumar Mahapatra 	}
10154d8ff6b0SAmit Kumar Mahapatra 	return last;
10164d8ff6b0SAmit Kumar Mahapatra }
10174d8ff6b0SAmit Kumar Mahapatra 
1018fb51601bSUwe Kleine-König 
1019d347b4aaSDavid Bauer static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
1020b158935fSMark Brown {
102186527bcbSAndy Shevchenko 	bool activate = enable;
10224d8ff6b0SAmit Kumar Mahapatra 	u8 idx;
102325093bdeSAlexandru Ardelean 
1024d40f0b6fSDouglas Anderson 	/*
1025d40f0b6fSDouglas Anderson 	 * Avoid calling into the driver (or doing delays) if the chip select
1026d40f0b6fSDouglas Anderson 	 * isn't actually changing from the last time this was called.
1027d40f0b6fSDouglas Anderson 	 */
10284d8ff6b0SAmit Kumar Mahapatra 	if (!force && ((enable && spi->controller->last_cs_index_mask == spi->cs_index_mask &&
10294d8ff6b0SAmit Kumar Mahapatra 			spi_is_last_cs(spi)) ||
10304d8ff6b0SAmit Kumar Mahapatra 		       (!enable && spi->controller->last_cs_index_mask == spi->cs_index_mask &&
10314d8ff6b0SAmit Kumar Mahapatra 			!spi_is_last_cs(spi))) &&
1032d40f0b6fSDouglas Anderson 	    (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
1033d40f0b6fSDouglas Anderson 		return;
1034d40f0b6fSDouglas Anderson 
10355cb4e1f3SAndy Shevchenko 	trace_spi_set_cs(spi, activate);
10365cb4e1f3SAndy Shevchenko 
10374d8ff6b0SAmit Kumar Mahapatra 	spi->controller->last_cs_index_mask = spi->cs_index_mask;
10384d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
10394d8ff6b0SAmit Kumar Mahapatra 		spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : -1;
1040d40f0b6fSDouglas Anderson 	spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
1041d40f0b6fSDouglas Anderson 
1042b158935fSMark Brown 	if (spi->mode & SPI_CS_HIGH)
1043b158935fSMark Brown 		enable = !enable;
1044b158935fSMark Brown 
10454d8ff6b0SAmit Kumar Mahapatra 	if (spi_is_csgpiod(spi)) {
10464d8ff6b0SAmit Kumar Mahapatra 		if (!spi->controller->set_cs_timing && !activate)
10474d8ff6b0SAmit Kumar Mahapatra 			spi_delay_exec(&spi->cs_hold, NULL);
10484d8ff6b0SAmit Kumar Mahapatra 
1049f48dc6b9SLinus Walleij 		if (!(spi->mode & SPI_NO_CS)) {
10506b695469SAndy Shevchenko 			/*
10516b695469SAndy Shevchenko 			 * Historically ACPI has no means of the GPIO polarity and
10526b695469SAndy Shevchenko 			 * thus the SPISerialBus() resource defines it on the per-chip
10536b695469SAndy Shevchenko 			 * basis. In order to avoid a chain of negations, the GPIO
10546b695469SAndy Shevchenko 			 * polarity is considered being Active High. Even for the cases
10556b695469SAndy Shevchenko 			 * when _DSD() is involved (in the updated versions of ACPI)
10566b695469SAndy Shevchenko 			 * the GPIO CS polarity must be defined Active High to avoid
10576b695469SAndy Shevchenko 			 * ambiguity. That's why we use enable, that takes SPI_CS_HIGH
10586b695469SAndy Shevchenko 			 * into account.
10596b695469SAndy Shevchenko 			 */
10604d8ff6b0SAmit Kumar Mahapatra 			for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
10614d8ff6b0SAmit Kumar Mahapatra 				if (((spi->cs_index_mask >> idx) & 0x01) &&
10624d8ff6b0SAmit Kumar Mahapatra 				    spi_get_csgpiod(spi, idx)) {
10636b695469SAndy Shevchenko 					if (has_acpi_companion(&spi->dev))
10644d8ff6b0SAmit Kumar Mahapatra 						gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx),
10654d8ff6b0SAmit Kumar Mahapatra 									 !enable);
1066f3186dd8SLinus Walleij 					else
10676b695469SAndy Shevchenko 						/* Polarity handled by GPIO library */
10684d8ff6b0SAmit Kumar Mahapatra 						gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx),
10694d8ff6b0SAmit Kumar Mahapatra 									 activate);
10704d8ff6b0SAmit Kumar Mahapatra 
10714d8ff6b0SAmit Kumar Mahapatra 					if (activate)
10724d8ff6b0SAmit Kumar Mahapatra 						spi_delay_exec(&spi->cs_setup, NULL);
10734d8ff6b0SAmit Kumar Mahapatra 					else
10744d8ff6b0SAmit Kumar Mahapatra 						spi_delay_exec(&spi->cs_inactive, NULL);
10754d8ff6b0SAmit Kumar Mahapatra 				}
10764d8ff6b0SAmit Kumar Mahapatra 			}
10776b695469SAndy Shevchenko 		}
10788eee6b9dSThor Thayer 		/* Some SPI masters need both GPIO CS & slave_select */
107982238d2cSAndy Shevchenko 		if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) &&
10808caab75fSGeert Uytterhoeven 		    spi->controller->set_cs)
10818caab75fSGeert Uytterhoeven 			spi->controller->set_cs(spi, !enable);
108225093bdeSAlexandru Ardelean 
10834d8ff6b0SAmit Kumar Mahapatra 		if (!spi->controller->set_cs_timing) {
108495c07247SHector Martin 			if (activate)
108595c07247SHector Martin 				spi_delay_exec(&spi->cs_setup, NULL);
108695c07247SHector Martin 			else
10878c33ebfeSMason Zhang 				spi_delay_exec(&spi->cs_inactive, NULL);
108825093bdeSAlexandru Ardelean 		}
10894d8ff6b0SAmit Kumar Mahapatra 	} else if (spi->controller->set_cs) {
10904d8ff6b0SAmit Kumar Mahapatra 		spi->controller->set_cs(spi, !enable);
10914d8ff6b0SAmit Kumar Mahapatra 	}
1092b158935fSMark Brown }
1093b158935fSMark Brown 
10942de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
10950c17ba73SVincent Whitchurch static int spi_map_buf_attrs(struct spi_controller *ctlr, struct device *dev,
10966ad45a27SMark Brown 			     struct sg_table *sgt, void *buf, size_t len,
10970c17ba73SVincent Whitchurch 			     enum dma_data_direction dir, unsigned long attrs)
10986ad45a27SMark Brown {
10996ad45a27SMark Brown 	const bool vmalloced_buf = is_vmalloc_addr(buf);
1100df88e91bSAndy Shevchenko 	unsigned int max_seg_size = dma_get_max_seg_size(dev);
1101b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM
1102b1b8153cSVignesh R 	const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
1103b1b8153cSVignesh R 				(unsigned long)buf < (PKMAP_BASE +
1104b1b8153cSVignesh R 					(LAST_PKMAP * PAGE_SIZE)));
1105b1b8153cSVignesh R #else
1106b1b8153cSVignesh R 	const bool kmap_buf = false;
1107b1b8153cSVignesh R #endif
110865598c13SAndrew Gabbasov 	int desc_len;
110965598c13SAndrew Gabbasov 	int sgs;
11106ad45a27SMark Brown 	struct page *vm_page;
11118dd4a016SJuan Gutierrez 	struct scatterlist *sg;
11126ad45a27SMark Brown 	void *sg_buf;
11136ad45a27SMark Brown 	size_t min;
11146ad45a27SMark Brown 	int i, ret;
11156ad45a27SMark Brown 
1116b1b8153cSVignesh R 	if (vmalloced_buf || kmap_buf) {
1117ebc4cb43SBiju Das 		desc_len = min_t(unsigned long, max_seg_size, PAGE_SIZE);
111865598c13SAndrew Gabbasov 		sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
11190569a88fSVignesh R 	} else if (virt_addr_valid(buf)) {
1120ebc4cb43SBiju Das 		desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len);
112165598c13SAndrew Gabbasov 		sgs = DIV_ROUND_UP(len, desc_len);
11220569a88fSVignesh R 	} else {
11230569a88fSVignesh R 		return -EINVAL;
112465598c13SAndrew Gabbasov 	}
112565598c13SAndrew Gabbasov 
11266ad45a27SMark Brown 	ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
11276ad45a27SMark Brown 	if (ret != 0)
11286ad45a27SMark Brown 		return ret;
11296ad45a27SMark Brown 
11308dd4a016SJuan Gutierrez 	sg = &sgt->sgl[0];
11316ad45a27SMark Brown 	for (i = 0; i < sgs; i++) {
11326ad45a27SMark Brown 
1133b1b8153cSVignesh R 		if (vmalloced_buf || kmap_buf) {
1134ce99319aSMaxime Chevallier 			/*
1135ce99319aSMaxime Chevallier 			 * Next scatterlist entry size is the minimum between
1136ce99319aSMaxime Chevallier 			 * the desc_len and the remaining buffer length that
1137ce99319aSMaxime Chevallier 			 * fits in a page.
1138ce99319aSMaxime Chevallier 			 */
1139ce99319aSMaxime Chevallier 			min = min_t(size_t, desc_len,
1140ce99319aSMaxime Chevallier 				    min_t(size_t, len,
1141ce99319aSMaxime Chevallier 					  PAGE_SIZE - offset_in_page(buf)));
1142b1b8153cSVignesh R 			if (vmalloced_buf)
11436ad45a27SMark Brown 				vm_page = vmalloc_to_page(buf);
1144b1b8153cSVignesh R 			else
1145b1b8153cSVignesh R 				vm_page = kmap_to_page(buf);
11466ad45a27SMark Brown 			if (!vm_page) {
11476ad45a27SMark Brown 				sg_free_table(sgt);
11486ad45a27SMark Brown 				return -ENOMEM;
11496ad45a27SMark Brown 			}
11508dd4a016SJuan Gutierrez 			sg_set_page(sg, vm_page,
1151c1aefbddSCharles Keepax 				    min, offset_in_page(buf));
11526ad45a27SMark Brown 		} else {
115365598c13SAndrew Gabbasov 			min = min_t(size_t, len, desc_len);
11546ad45a27SMark Brown 			sg_buf = buf;
11558dd4a016SJuan Gutierrez 			sg_set_buf(sg, sg_buf, min);
11566ad45a27SMark Brown 		}
11576ad45a27SMark Brown 
11586ad45a27SMark Brown 		buf += min;
11596ad45a27SMark Brown 		len -= min;
11608dd4a016SJuan Gutierrez 		sg = sg_next(sg);
11616ad45a27SMark Brown 	}
11626ad45a27SMark Brown 
11630c17ba73SVincent Whitchurch 	ret = dma_map_sgtable(dev, sgt, dir, attrs);
11646ad45a27SMark Brown 	if (ret < 0) {
11656ad45a27SMark Brown 		sg_free_table(sgt);
11666ad45a27SMark Brown 		return ret;
11676ad45a27SMark Brown 	}
11686ad45a27SMark Brown 
11696ad45a27SMark Brown 	return 0;
11706ad45a27SMark Brown }
11716ad45a27SMark Brown 
11720c17ba73SVincent Whitchurch int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
11730c17ba73SVincent Whitchurch 		struct sg_table *sgt, void *buf, size_t len,
11740c17ba73SVincent Whitchurch 		enum dma_data_direction dir)
11750c17ba73SVincent Whitchurch {
11760c17ba73SVincent Whitchurch 	return spi_map_buf_attrs(ctlr, dev, sgt, buf, len, dir, 0);
11770c17ba73SVincent Whitchurch }
11780c17ba73SVincent Whitchurch 
11790c17ba73SVincent Whitchurch static void spi_unmap_buf_attrs(struct spi_controller *ctlr,
11800c17ba73SVincent Whitchurch 				struct device *dev, struct sg_table *sgt,
11810c17ba73SVincent Whitchurch 				enum dma_data_direction dir,
11820c17ba73SVincent Whitchurch 				unsigned long attrs)
11830c17ba73SVincent Whitchurch {
11840c17ba73SVincent Whitchurch 	if (sgt->orig_nents) {
11850c17ba73SVincent Whitchurch 		dma_unmap_sgtable(dev, sgt, dir, attrs);
11860c17ba73SVincent Whitchurch 		sg_free_table(sgt);
11878e9204cdSMarek Szyprowski 		sgt->orig_nents = 0;
11888e9204cdSMarek Szyprowski 		sgt->nents = 0;
11890c17ba73SVincent Whitchurch 	}
11900c17ba73SVincent Whitchurch }
11910c17ba73SVincent Whitchurch 
119246336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
11936ad45a27SMark Brown 		   struct sg_table *sgt, enum dma_data_direction dir)
11946ad45a27SMark Brown {
11950c17ba73SVincent Whitchurch 	spi_unmap_buf_attrs(ctlr, dev, sgt, dir, 0);
11966ad45a27SMark Brown }
11976ad45a27SMark Brown 
11988caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
119999adef31SMark Brown {
120099adef31SMark Brown 	struct device *tx_dev, *rx_dev;
120199adef31SMark Brown 	struct spi_transfer *xfer;
12026ad45a27SMark Brown 	int ret;
12033a2eba9bSMark Brown 
12048caab75fSGeert Uytterhoeven 	if (!ctlr->can_dma)
120599adef31SMark Brown 		return 0;
120699adef31SMark Brown 
12078caab75fSGeert Uytterhoeven 	if (ctlr->dma_tx)
12088caab75fSGeert Uytterhoeven 		tx_dev = ctlr->dma_tx->device->dev;
1209b470e10eSVinod Koul 	else if (ctlr->dma_map_dev)
1210b470e10eSVinod Koul 		tx_dev = ctlr->dma_map_dev;
1211c37f45b5SLeilk Liu 	else
12128caab75fSGeert Uytterhoeven 		tx_dev = ctlr->dev.parent;
1213c37f45b5SLeilk Liu 
12148caab75fSGeert Uytterhoeven 	if (ctlr->dma_rx)
12158caab75fSGeert Uytterhoeven 		rx_dev = ctlr->dma_rx->device->dev;
1216b470e10eSVinod Koul 	else if (ctlr->dma_map_dev)
1217b470e10eSVinod Koul 		rx_dev = ctlr->dma_map_dev;
1218c37f45b5SLeilk Liu 	else
12198caab75fSGeert Uytterhoeven 		rx_dev = ctlr->dev.parent;
122099adef31SMark Brown 
122199adef31SMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
12220c17ba73SVincent Whitchurch 		/* The sync is done before each transfer. */
12230c17ba73SVincent Whitchurch 		unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
12240c17ba73SVincent Whitchurch 
12258caab75fSGeert Uytterhoeven 		if (!ctlr->can_dma(ctlr, msg->spi, xfer))
122699adef31SMark Brown 			continue;
122799adef31SMark Brown 
122899adef31SMark Brown 		if (xfer->tx_buf != NULL) {
12290c17ba73SVincent Whitchurch 			ret = spi_map_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
12300c17ba73SVincent Whitchurch 						(void *)xfer->tx_buf,
12310c17ba73SVincent Whitchurch 						xfer->len, DMA_TO_DEVICE,
12320c17ba73SVincent Whitchurch 						attrs);
12336ad45a27SMark Brown 			if (ret != 0)
12346ad45a27SMark Brown 				return ret;
123599adef31SMark Brown 		}
123699adef31SMark Brown 
123799adef31SMark Brown 		if (xfer->rx_buf != NULL) {
12380c17ba73SVincent Whitchurch 			ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
123999adef31SMark Brown 						xfer->rx_buf, xfer->len,
12400c17ba73SVincent Whitchurch 						DMA_FROM_DEVICE, attrs);
12416ad45a27SMark Brown 			if (ret != 0) {
12420c17ba73SVincent Whitchurch 				spi_unmap_buf_attrs(ctlr, tx_dev,
12430c17ba73SVincent Whitchurch 						&xfer->tx_sg, DMA_TO_DEVICE,
12440c17ba73SVincent Whitchurch 						attrs);
12450c17ba73SVincent Whitchurch 
12466ad45a27SMark Brown 				return ret;
124799adef31SMark Brown 			}
124899adef31SMark Brown 		}
124999adef31SMark Brown 	}
125099adef31SMark Brown 
1251f25723dcSVincent Whitchurch 	ctlr->cur_rx_dma_dev = rx_dev;
1252f25723dcSVincent Whitchurch 	ctlr->cur_tx_dma_dev = tx_dev;
12538caab75fSGeert Uytterhoeven 	ctlr->cur_msg_mapped = true;
125499adef31SMark Brown 
125599adef31SMark Brown 	return 0;
125699adef31SMark Brown }
125799adef31SMark Brown 
12588caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
125999adef31SMark Brown {
1260f25723dcSVincent Whitchurch 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
1261f25723dcSVincent Whitchurch 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
126299adef31SMark Brown 	struct spi_transfer *xfer;
126399adef31SMark Brown 
12648caab75fSGeert Uytterhoeven 	if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
126599adef31SMark Brown 		return 0;
126699adef31SMark Brown 
126799adef31SMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
12680c17ba73SVincent Whitchurch 		/* The sync has already been done after each transfer. */
12690c17ba73SVincent Whitchurch 		unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
12700c17ba73SVincent Whitchurch 
12718caab75fSGeert Uytterhoeven 		if (!ctlr->can_dma(ctlr, msg->spi, xfer))
127299adef31SMark Brown 			continue;
127399adef31SMark Brown 
12740c17ba73SVincent Whitchurch 		spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
12750c17ba73SVincent Whitchurch 				    DMA_FROM_DEVICE, attrs);
12760c17ba73SVincent Whitchurch 		spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
12770c17ba73SVincent Whitchurch 				    DMA_TO_DEVICE, attrs);
127899adef31SMark Brown 	}
127999adef31SMark Brown 
1280809b1b04SRobin Gong 	ctlr->cur_msg_mapped = false;
1281809b1b04SRobin Gong 
128299adef31SMark Brown 	return 0;
128399adef31SMark Brown }
12840c17ba73SVincent Whitchurch 
12850c17ba73SVincent Whitchurch static void spi_dma_sync_for_device(struct spi_controller *ctlr,
12860c17ba73SVincent Whitchurch 				    struct spi_transfer *xfer)
12870c17ba73SVincent Whitchurch {
12880c17ba73SVincent Whitchurch 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
12890c17ba73SVincent Whitchurch 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
12900c17ba73SVincent Whitchurch 
12910c17ba73SVincent Whitchurch 	if (!ctlr->cur_msg_mapped)
12920c17ba73SVincent Whitchurch 		return;
12930c17ba73SVincent Whitchurch 
12940c17ba73SVincent Whitchurch 	if (xfer->tx_sg.orig_nents)
12950c17ba73SVincent Whitchurch 		dma_sync_sgtable_for_device(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
12960c17ba73SVincent Whitchurch 	if (xfer->rx_sg.orig_nents)
12970c17ba73SVincent Whitchurch 		dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
12980c17ba73SVincent Whitchurch }
12990c17ba73SVincent Whitchurch 
13000c17ba73SVincent Whitchurch static void spi_dma_sync_for_cpu(struct spi_controller *ctlr,
13010c17ba73SVincent Whitchurch 				 struct spi_transfer *xfer)
13020c17ba73SVincent Whitchurch {
13030c17ba73SVincent Whitchurch 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
13040c17ba73SVincent Whitchurch 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
13050c17ba73SVincent Whitchurch 
13060c17ba73SVincent Whitchurch 	if (!ctlr->cur_msg_mapped)
13070c17ba73SVincent Whitchurch 		return;
13080c17ba73SVincent Whitchurch 
13090c17ba73SVincent Whitchurch 	if (xfer->rx_sg.orig_nents)
13100c17ba73SVincent Whitchurch 		dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
13110c17ba73SVincent Whitchurch 	if (xfer->tx_sg.orig_nents)
13120c17ba73SVincent Whitchurch 		dma_sync_sgtable_for_cpu(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
13130c17ba73SVincent Whitchurch }
13142de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
13158caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr,
13162de440f5SGeert Uytterhoeven 				struct spi_message *msg)
13172de440f5SGeert Uytterhoeven {
13182de440f5SGeert Uytterhoeven 	return 0;
13192de440f5SGeert Uytterhoeven }
13202de440f5SGeert Uytterhoeven 
13218caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr,
13222de440f5SGeert Uytterhoeven 				  struct spi_message *msg)
13232de440f5SGeert Uytterhoeven {
13242de440f5SGeert Uytterhoeven 	return 0;
13252de440f5SGeert Uytterhoeven }
13260c17ba73SVincent Whitchurch 
13270c17ba73SVincent Whitchurch static void spi_dma_sync_for_device(struct spi_controller *ctrl,
13280c17ba73SVincent Whitchurch 				    struct spi_transfer *xfer)
13290c17ba73SVincent Whitchurch {
13300c17ba73SVincent Whitchurch }
13310c17ba73SVincent Whitchurch 
13320c17ba73SVincent Whitchurch static void spi_dma_sync_for_cpu(struct spi_controller *ctrl,
13330c17ba73SVincent Whitchurch 				 struct spi_transfer *xfer)
13340c17ba73SVincent Whitchurch {
13350c17ba73SVincent Whitchurch }
13362de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
13372de440f5SGeert Uytterhoeven 
13388caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr,
13394b786458SMartin Sperl 				struct spi_message *msg)
13404b786458SMartin Sperl {
13414b786458SMartin Sperl 	struct spi_transfer *xfer;
13424b786458SMartin Sperl 
13434b786458SMartin Sperl 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
13444b786458SMartin Sperl 		/*
13454b786458SMartin Sperl 		 * Restore the original value of tx_buf or rx_buf if they are
13464b786458SMartin Sperl 		 * NULL.
13474b786458SMartin Sperl 		 */
13488caab75fSGeert Uytterhoeven 		if (xfer->tx_buf == ctlr->dummy_tx)
13494b786458SMartin Sperl 			xfer->tx_buf = NULL;
13508caab75fSGeert Uytterhoeven 		if (xfer->rx_buf == ctlr->dummy_rx)
13514b786458SMartin Sperl 			xfer->rx_buf = NULL;
13524b786458SMartin Sperl 	}
13534b786458SMartin Sperl 
13548caab75fSGeert Uytterhoeven 	return __spi_unmap_msg(ctlr, msg);
13554b786458SMartin Sperl }
13564b786458SMartin Sperl 
13578caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
13582de440f5SGeert Uytterhoeven {
13592de440f5SGeert Uytterhoeven 	struct spi_transfer *xfer;
13602de440f5SGeert Uytterhoeven 	void *tmp;
13612de440f5SGeert Uytterhoeven 	unsigned int max_tx, max_rx;
13622de440f5SGeert Uytterhoeven 
1363aee67fe8Sdillon min 	if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX))
1364aee67fe8Sdillon min 		&& !(msg->spi->mode & SPI_3WIRE)) {
13652de440f5SGeert Uytterhoeven 		max_tx = 0;
13662de440f5SGeert Uytterhoeven 		max_rx = 0;
13672de440f5SGeert Uytterhoeven 
13682de440f5SGeert Uytterhoeven 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
13698caab75fSGeert Uytterhoeven 			if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
13702de440f5SGeert Uytterhoeven 			    !xfer->tx_buf)
13712de440f5SGeert Uytterhoeven 				max_tx = max(xfer->len, max_tx);
13728caab75fSGeert Uytterhoeven 			if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
13732de440f5SGeert Uytterhoeven 			    !xfer->rx_buf)
13742de440f5SGeert Uytterhoeven 				max_rx = max(xfer->len, max_rx);
13752de440f5SGeert Uytterhoeven 		}
13762de440f5SGeert Uytterhoeven 
13772de440f5SGeert Uytterhoeven 		if (max_tx) {
13788caab75fSGeert Uytterhoeven 			tmp = krealloc(ctlr->dummy_tx, max_tx,
1379b00bab9dSAndy Shevchenko 				       GFP_KERNEL | GFP_DMA | __GFP_ZERO);
13802de440f5SGeert Uytterhoeven 			if (!tmp)
13812de440f5SGeert Uytterhoeven 				return -ENOMEM;
13828caab75fSGeert Uytterhoeven 			ctlr->dummy_tx = tmp;
13832de440f5SGeert Uytterhoeven 		}
13842de440f5SGeert Uytterhoeven 
13852de440f5SGeert Uytterhoeven 		if (max_rx) {
13868caab75fSGeert Uytterhoeven 			tmp = krealloc(ctlr->dummy_rx, max_rx,
13872de440f5SGeert Uytterhoeven 				       GFP_KERNEL | GFP_DMA);
13882de440f5SGeert Uytterhoeven 			if (!tmp)
13892de440f5SGeert Uytterhoeven 				return -ENOMEM;
13908caab75fSGeert Uytterhoeven 			ctlr->dummy_rx = tmp;
13912de440f5SGeert Uytterhoeven 		}
13922de440f5SGeert Uytterhoeven 
13932de440f5SGeert Uytterhoeven 		if (max_tx || max_rx) {
13942de440f5SGeert Uytterhoeven 			list_for_each_entry(xfer, &msg->transfers,
13952de440f5SGeert Uytterhoeven 					    transfer_list) {
13965442dcaaSChris Lesiak 				if (!xfer->len)
13975442dcaaSChris Lesiak 					continue;
13982de440f5SGeert Uytterhoeven 				if (!xfer->tx_buf)
13998caab75fSGeert Uytterhoeven 					xfer->tx_buf = ctlr->dummy_tx;
14002de440f5SGeert Uytterhoeven 				if (!xfer->rx_buf)
14018caab75fSGeert Uytterhoeven 					xfer->rx_buf = ctlr->dummy_rx;
14022de440f5SGeert Uytterhoeven 			}
14032de440f5SGeert Uytterhoeven 		}
14042de440f5SGeert Uytterhoeven 	}
14052de440f5SGeert Uytterhoeven 
14068caab75fSGeert Uytterhoeven 	return __spi_map_msg(ctlr, msg);
14072de440f5SGeert Uytterhoeven }
140899adef31SMark Brown 
1409810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr,
1410810923f3SLubomir Rintel 			     struct spi_message *msg,
1411810923f3SLubomir Rintel 			     struct spi_transfer *xfer)
1412810923f3SLubomir Rintel {
1413d501cc4cSDavid Jander 	struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1414d501cc4cSDavid Jander 	struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
14156170d077SXu Yilun 	u32 speed_hz = xfer->speed_hz;
141649686df5SColin Ian King 	unsigned long long ms;
1417810923f3SLubomir Rintel 
1418810923f3SLubomir Rintel 	if (spi_controller_is_slave(ctlr)) {
1419810923f3SLubomir Rintel 		if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1420810923f3SLubomir Rintel 			dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1421810923f3SLubomir Rintel 			return -EINTR;
1422810923f3SLubomir Rintel 		}
1423810923f3SLubomir Rintel 	} else {
14246170d077SXu Yilun 		if (!speed_hz)
14256170d077SXu Yilun 			speed_hz = 100000;
14266170d077SXu Yilun 
142786b8bff7SAndy Shevchenko 		/*
142886b8bff7SAndy Shevchenko 		 * For each byte we wait for 8 cycles of the SPI clock.
142986b8bff7SAndy Shevchenko 		 * Since speed is defined in Hz and we want milliseconds,
143086b8bff7SAndy Shevchenko 		 * use respective multiplier, but before the division,
143186b8bff7SAndy Shevchenko 		 * otherwise we may get 0 for short transfers.
143286b8bff7SAndy Shevchenko 		 */
143386b8bff7SAndy Shevchenko 		ms = 8LL * MSEC_PER_SEC * xfer->len;
14346170d077SXu Yilun 		do_div(ms, speed_hz);
1435810923f3SLubomir Rintel 
143686b8bff7SAndy Shevchenko 		/*
143786b8bff7SAndy Shevchenko 		 * Increase it twice and add 200 ms tolerance, use
143886b8bff7SAndy Shevchenko 		 * predefined maximum in case of overflow.
143986b8bff7SAndy Shevchenko 		 */
144086b8bff7SAndy Shevchenko 		ms += ms + 200;
1441810923f3SLubomir Rintel 		if (ms > UINT_MAX)
1442810923f3SLubomir Rintel 			ms = UINT_MAX;
1443810923f3SLubomir Rintel 
1444810923f3SLubomir Rintel 		ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1445810923f3SLubomir Rintel 						 msecs_to_jiffies(ms));
1446810923f3SLubomir Rintel 
1447810923f3SLubomir Rintel 		if (ms == 0) {
1448810923f3SLubomir Rintel 			SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1449810923f3SLubomir Rintel 			SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1450810923f3SLubomir Rintel 			dev_err(&msg->spi->dev,
1451810923f3SLubomir Rintel 				"SPI transfer timed out\n");
1452810923f3SLubomir Rintel 			return -ETIMEDOUT;
1453810923f3SLubomir Rintel 		}
145439cefd85SNam Cao 
145539cefd85SNam Cao 		if (xfer->error & SPI_TRANS_FAIL_IO)
145639cefd85SNam Cao 			return -EIO;
1457810923f3SLubomir Rintel 	}
1458810923f3SLubomir Rintel 
1459810923f3SLubomir Rintel 	return 0;
1460810923f3SLubomir Rintel }
1461810923f3SLubomir Rintel 
14620ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns)
14630ff2de8bSMartin Sperl {
14640ff2de8bSMartin Sperl 	if (!ns)
14650ff2de8bSMartin Sperl 		return;
146686b8bff7SAndy Shevchenko 	if (ns <= NSEC_PER_USEC) {
14670ff2de8bSMartin Sperl 		ndelay(ns);
14680ff2de8bSMartin Sperl 	} else {
146986b8bff7SAndy Shevchenko 		u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
14700ff2de8bSMartin Sperl 
14710ff2de8bSMartin Sperl 		if (us <= 10)
14720ff2de8bSMartin Sperl 			udelay(us);
14730ff2de8bSMartin Sperl 		else
14740ff2de8bSMartin Sperl 			usleep_range(us, us + DIV_ROUND_UP(us, 10));
14750ff2de8bSMartin Sperl 	}
14760ff2de8bSMartin Sperl }
14770ff2de8bSMartin Sperl 
14783984d39bSAlexandru Ardelean int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
14790ff2de8bSMartin Sperl {
1480b2c98153SAlexandru Ardelean 	u32 delay = _delay->value;
1481b2c98153SAlexandru Ardelean 	u32 unit = _delay->unit;
1482d5864e5bSMartin Sperl 	u32 hz;
14830ff2de8bSMartin Sperl 
1484b2c98153SAlexandru Ardelean 	if (!delay)
1485b2c98153SAlexandru Ardelean 		return 0;
14860ff2de8bSMartin Sperl 
14870ff2de8bSMartin Sperl 	switch (unit) {
14880ff2de8bSMartin Sperl 	case SPI_DELAY_UNIT_USECS:
148986b8bff7SAndy Shevchenko 		delay *= NSEC_PER_USEC;
14900ff2de8bSMartin Sperl 		break;
149186b8bff7SAndy Shevchenko 	case SPI_DELAY_UNIT_NSECS:
149286b8bff7SAndy Shevchenko 		/* Nothing to do here */
14930ff2de8bSMartin Sperl 		break;
1494d5864e5bSMartin Sperl 	case SPI_DELAY_UNIT_SCK:
149595c8222fSDavid Jander 		/* Clock cycles need to be obtained from spi_transfer */
1496b2c98153SAlexandru Ardelean 		if (!xfer)
1497b2c98153SAlexandru Ardelean 			return -EINVAL;
149886b8bff7SAndy Shevchenko 		/*
149986b8bff7SAndy Shevchenko 		 * If there is unknown effective speed, approximate it
1500702ca026SAndy Shevchenko 		 * by underestimating with half of the requested Hz.
1501d5864e5bSMartin Sperl 		 */
1502d5864e5bSMartin Sperl 		hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1503b2c98153SAlexandru Ardelean 		if (!hz)
1504b2c98153SAlexandru Ardelean 			return -EINVAL;
150586b8bff7SAndy Shevchenko 
150686b8bff7SAndy Shevchenko 		/* Convert delay to nanoseconds */
150786b8bff7SAndy Shevchenko 		delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz);
1508d5864e5bSMartin Sperl 		break;
15090ff2de8bSMartin Sperl 	default:
1510b2c98153SAlexandru Ardelean 		return -EINVAL;
1511b2c98153SAlexandru Ardelean 	}
1512b2c98153SAlexandru Ardelean 
1513b2c98153SAlexandru Ardelean 	return delay;
1514b2c98153SAlexandru Ardelean }
15153984d39bSAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_to_ns);
1516b2c98153SAlexandru Ardelean 
1517b2c98153SAlexandru Ardelean int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1518b2c98153SAlexandru Ardelean {
1519b2c98153SAlexandru Ardelean 	int delay;
1520b2c98153SAlexandru Ardelean 
15218fede89fSMark Brown 	might_sleep();
15228fede89fSMark Brown 
1523b2c98153SAlexandru Ardelean 	if (!_delay)
1524b2c98153SAlexandru Ardelean 		return -EINVAL;
1525b2c98153SAlexandru Ardelean 
15263984d39bSAlexandru Ardelean 	delay = spi_delay_to_ns(_delay, xfer);
1527b2c98153SAlexandru Ardelean 	if (delay < 0)
1528b2c98153SAlexandru Ardelean 		return delay;
1529b2c98153SAlexandru Ardelean 
1530b2c98153SAlexandru Ardelean 	_spi_transfer_delay_ns(delay);
1531b2c98153SAlexandru Ardelean 
1532b2c98153SAlexandru Ardelean 	return 0;
1533b2c98153SAlexandru Ardelean }
1534b2c98153SAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_exec);
1535b2c98153SAlexandru Ardelean 
15360ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg,
15370ff2de8bSMartin Sperl 					  struct spi_transfer *xfer)
15380ff2de8bSMartin Sperl {
153986b8bff7SAndy Shevchenko 	u32 default_delay_ns = 10 * NSEC_PER_USEC;
1540329f0dacSAlexandru Ardelean 	u32 delay = xfer->cs_change_delay.value;
1541329f0dacSAlexandru Ardelean 	u32 unit = xfer->cs_change_delay.unit;
1542329f0dacSAlexandru Ardelean 	int ret;
15430ff2de8bSMartin Sperl 
154495c8222fSDavid Jander 	/* Return early on "fast" mode - for everything but USECS */
15456b3f236aSAlexandru Ardelean 	if (!delay) {
15466b3f236aSAlexandru Ardelean 		if (unit == SPI_DELAY_UNIT_USECS)
154786b8bff7SAndy Shevchenko 			_spi_transfer_delay_ns(default_delay_ns);
15480ff2de8bSMartin Sperl 		return;
15496b3f236aSAlexandru Ardelean 	}
15500ff2de8bSMartin Sperl 
1551329f0dacSAlexandru Ardelean 	ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1552329f0dacSAlexandru Ardelean 	if (ret) {
15530ff2de8bSMartin Sperl 		dev_err_once(&msg->spi->dev,
155486b8bff7SAndy Shevchenko 			     "Use of unsupported delay unit %i, using default of %luus\n",
155586b8bff7SAndy Shevchenko 			     unit, default_delay_ns / NSEC_PER_USEC);
155686b8bff7SAndy Shevchenko 		_spi_transfer_delay_ns(default_delay_ns);
15570ff2de8bSMartin Sperl 	}
15580ff2de8bSMartin Sperl }
15590ff2de8bSMartin Sperl 
15606e80133aSWilliam Zhang void spi_transfer_cs_change_delay_exec(struct spi_message *msg,
15616e80133aSWilliam Zhang 						  struct spi_transfer *xfer)
15626e80133aSWilliam Zhang {
15636e80133aSWilliam Zhang 	_spi_transfer_cs_change_delay(msg, xfer);
15646e80133aSWilliam Zhang }
15656e80133aSWilliam Zhang EXPORT_SYMBOL_GPL(spi_transfer_cs_change_delay_exec);
15666e80133aSWilliam Zhang 
1567b158935fSMark Brown /*
1568b158935fSMark Brown  * spi_transfer_one_message - Default implementation of transfer_one_message()
1569b158935fSMark Brown  *
1570b158935fSMark Brown  * This is a standard implementation of transfer_one_message() for
15718ba811a7SMoritz Fischer  * drivers which implement a transfer_one() operation.  It provides
1572b158935fSMark Brown  * standard handling of delays and chip select management.
1573b158935fSMark Brown  */
15748caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr,
1575b158935fSMark Brown 				    struct spi_message *msg)
1576b158935fSMark Brown {
1577b158935fSMark Brown 	struct spi_transfer *xfer;
1578b158935fSMark Brown 	bool keep_cs = false;
1579b158935fSMark Brown 	int ret = 0;
1580d501cc4cSDavid Jander 	struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1581d501cc4cSDavid Jander 	struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
1582b158935fSMark Brown 
15835e0531f6SChristophe Leroy 	xfer = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list);
15845e0531f6SChristophe Leroy 	spi_set_cs(msg->spi, !xfer->cs_off, false);
1585b158935fSMark Brown 
1586eca2ebc7SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1587eca2ebc7SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1588eca2ebc7SMartin Sperl 
1589b158935fSMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1590b158935fSMark Brown 		trace_spi_transfer_start(msg, xfer);
1591b158935fSMark Brown 
15928caab75fSGeert Uytterhoeven 		spi_statistics_add_transfer_stats(statm, xfer, ctlr);
15938caab75fSGeert Uytterhoeven 		spi_statistics_add_transfer_stats(stats, xfer, ctlr);
1594eca2ebc7SMartin Sperl 
1595b42faeeeSVladimir Oltean 		if (!ctlr->ptp_sts_supported) {
1596b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_pre = 0;
1597b42faeeeSVladimir Oltean 			ptp_read_system_prets(xfer->ptp_sts);
1598b42faeeeSVladimir Oltean 		}
1599b42faeeeSVladimir Oltean 
1600b3063203SNicolas Saenz Julienne 		if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
16018caab75fSGeert Uytterhoeven 			reinit_completion(&ctlr->xfer_completion);
1602b158935fSMark Brown 
1603809b1b04SRobin Gong fallback_pio:
16040c17ba73SVincent Whitchurch 			spi_dma_sync_for_device(ctlr, xfer);
16058caab75fSGeert Uytterhoeven 			ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1606b158935fSMark Brown 			if (ret < 0) {
16070c17ba73SVincent Whitchurch 				spi_dma_sync_for_cpu(ctlr, xfer);
16080c17ba73SVincent Whitchurch 
1609809b1b04SRobin Gong 				if (ctlr->cur_msg_mapped &&
1610809b1b04SRobin Gong 				   (xfer->error & SPI_TRANS_FAIL_NO_START)) {
1611809b1b04SRobin Gong 					__spi_unmap_msg(ctlr, msg);
1612809b1b04SRobin Gong 					ctlr->fallback = true;
1613809b1b04SRobin Gong 					xfer->error &= ~SPI_TRANS_FAIL_NO_START;
1614809b1b04SRobin Gong 					goto fallback_pio;
1615809b1b04SRobin Gong 				}
1616809b1b04SRobin Gong 
1617eca2ebc7SMartin Sperl 				SPI_STATISTICS_INCREMENT_FIELD(statm,
1618eca2ebc7SMartin Sperl 							       errors);
1619eca2ebc7SMartin Sperl 				SPI_STATISTICS_INCREMENT_FIELD(stats,
1620eca2ebc7SMartin Sperl 							       errors);
1621b158935fSMark Brown 				dev_err(&msg->spi->dev,
1622b158935fSMark Brown 					"SPI transfer failed: %d\n", ret);
1623b158935fSMark Brown 				goto out;
1624b158935fSMark Brown 			}
1625b158935fSMark Brown 
1626d57e7960SMark Brown 			if (ret > 0) {
1627810923f3SLubomir Rintel 				ret = spi_transfer_wait(ctlr, msg, xfer);
1628810923f3SLubomir Rintel 				if (ret < 0)
1629810923f3SLubomir Rintel 					msg->status = ret;
1630d57e7960SMark Brown 			}
16310c17ba73SVincent Whitchurch 
16320c17ba73SVincent Whitchurch 			spi_dma_sync_for_cpu(ctlr, xfer);
163338ec10f6SMark Brown 		} else {
163438ec10f6SMark Brown 			if (xfer->len)
163538ec10f6SMark Brown 				dev_err(&msg->spi->dev,
163638ec10f6SMark Brown 					"Bufferless transfer has length %u\n",
163738ec10f6SMark Brown 					xfer->len);
163838ec10f6SMark Brown 		}
1639b158935fSMark Brown 
1640b42faeeeSVladimir Oltean 		if (!ctlr->ptp_sts_supported) {
1641b42faeeeSVladimir Oltean 			ptp_read_system_postts(xfer->ptp_sts);
1642b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_post = xfer->len;
1643b42faeeeSVladimir Oltean 		}
1644b42faeeeSVladimir Oltean 
1645b158935fSMark Brown 		trace_spi_transfer_stop(msg, xfer);
1646b158935fSMark Brown 
1647b158935fSMark Brown 		if (msg->status != -EINPROGRESS)
1648b158935fSMark Brown 			goto out;
1649b158935fSMark Brown 
1650bebcfd27SAlexandru Ardelean 		spi_transfer_delay_exec(xfer);
1651b158935fSMark Brown 
1652b158935fSMark Brown 		if (xfer->cs_change) {
1653b158935fSMark Brown 			if (list_is_last(&xfer->transfer_list,
1654b158935fSMark Brown 					 &msg->transfers)) {
1655b158935fSMark Brown 				keep_cs = true;
1656b158935fSMark Brown 			} else {
16575e0531f6SChristophe Leroy 				if (!xfer->cs_off)
1658d347b4aaSDavid Bauer 					spi_set_cs(msg->spi, false, false);
16590ff2de8bSMartin Sperl 				_spi_transfer_cs_change_delay(msg, xfer);
16605e0531f6SChristophe Leroy 				if (!list_next_entry(xfer, transfer_list)->cs_off)
1661d347b4aaSDavid Bauer 					spi_set_cs(msg->spi, true, false);
1662b158935fSMark Brown 			}
16635e0531f6SChristophe Leroy 		} else if (!list_is_last(&xfer->transfer_list, &msg->transfers) &&
16645e0531f6SChristophe Leroy 			   xfer->cs_off != list_next_entry(xfer, transfer_list)->cs_off) {
16655e0531f6SChristophe Leroy 			spi_set_cs(msg->spi, xfer->cs_off, false);
1666b158935fSMark Brown 		}
1667b158935fSMark Brown 
1668b158935fSMark Brown 		msg->actual_length += xfer->len;
1669b158935fSMark Brown 	}
1670b158935fSMark Brown 
1671b158935fSMark Brown out:
1672b158935fSMark Brown 	if (ret != 0 || !keep_cs)
1673d347b4aaSDavid Bauer 		spi_set_cs(msg->spi, false, false);
1674b158935fSMark Brown 
1675b158935fSMark Brown 	if (msg->status == -EINPROGRESS)
1676b158935fSMark Brown 		msg->status = ret;
1677b158935fSMark Brown 
16788caab75fSGeert Uytterhoeven 	if (msg->status && ctlr->handle_err)
16798caab75fSGeert Uytterhoeven 		ctlr->handle_err(ctlr, msg);
1680b716c4ffSAndy Shevchenko 
16810ed56252SMark Brown 	spi_finalize_current_message(ctlr);
16820ed56252SMark Brown 
1683b158935fSMark Brown 	return ret;
1684b158935fSMark Brown }
1685b158935fSMark Brown 
1686b158935fSMark Brown /**
1687b158935fSMark Brown  * spi_finalize_current_transfer - report completion of a transfer
16888caab75fSGeert Uytterhoeven  * @ctlr: the controller reporting completion
1689b158935fSMark Brown  *
1690b158935fSMark Brown  * Called by SPI drivers using the core transfer_one_message()
1691b158935fSMark Brown  * implementation to notify it that the current interrupt driven
16929e8f4882SGeert Uytterhoeven  * transfer has finished and the next one may be scheduled.
1693b158935fSMark Brown  */
16948caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr)
1695b158935fSMark Brown {
16968caab75fSGeert Uytterhoeven 	complete(&ctlr->xfer_completion);
1697b158935fSMark Brown }
1698b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1699b158935fSMark Brown 
1700e1268597SMark Brown static void spi_idle_runtime_pm(struct spi_controller *ctlr)
1701e1268597SMark Brown {
1702e1268597SMark Brown 	if (ctlr->auto_runtime_pm) {
1703e1268597SMark Brown 		pm_runtime_mark_last_busy(ctlr->dev.parent);
1704e1268597SMark Brown 		pm_runtime_put_autosuspend(ctlr->dev.parent);
1705e1268597SMark Brown 	}
1706e1268597SMark Brown }
1707e1268597SMark Brown 
1708ae7d2346SDavid Jander static int __spi_pump_transfer_message(struct spi_controller *ctlr,
1709ae7d2346SDavid Jander 		struct spi_message *msg, bool was_busy)
1710ae7d2346SDavid Jander {
1711ae7d2346SDavid Jander 	struct spi_transfer *xfer;
1712ae7d2346SDavid Jander 	int ret;
1713ae7d2346SDavid Jander 
1714ae7d2346SDavid Jander 	if (!was_busy && ctlr->auto_runtime_pm) {
1715ae7d2346SDavid Jander 		ret = pm_runtime_get_sync(ctlr->dev.parent);
1716ae7d2346SDavid Jander 		if (ret < 0) {
1717ae7d2346SDavid Jander 			pm_runtime_put_noidle(ctlr->dev.parent);
1718ae7d2346SDavid Jander 			dev_err(&ctlr->dev, "Failed to power device: %d\n",
1719ae7d2346SDavid Jander 				ret);
1720*8c2ae772SDavid Lechner 
1721*8c2ae772SDavid Lechner 			msg->status = ret;
1722*8c2ae772SDavid Lechner 			spi_finalize_current_message(ctlr);
1723*8c2ae772SDavid Lechner 
1724ae7d2346SDavid Jander 			return ret;
1725ae7d2346SDavid Jander 		}
1726ae7d2346SDavid Jander 	}
1727ae7d2346SDavid Jander 
1728ae7d2346SDavid Jander 	if (!was_busy)
1729ae7d2346SDavid Jander 		trace_spi_controller_busy(ctlr);
1730ae7d2346SDavid Jander 
1731ae7d2346SDavid Jander 	if (!was_busy && ctlr->prepare_transfer_hardware) {
1732ae7d2346SDavid Jander 		ret = ctlr->prepare_transfer_hardware(ctlr);
1733ae7d2346SDavid Jander 		if (ret) {
1734ae7d2346SDavid Jander 			dev_err(&ctlr->dev,
1735ae7d2346SDavid Jander 				"failed to prepare transfer hardware: %d\n",
1736ae7d2346SDavid Jander 				ret);
1737ae7d2346SDavid Jander 
1738ae7d2346SDavid Jander 			if (ctlr->auto_runtime_pm)
1739ae7d2346SDavid Jander 				pm_runtime_put(ctlr->dev.parent);
1740ae7d2346SDavid Jander 
1741ae7d2346SDavid Jander 			msg->status = ret;
1742ae7d2346SDavid Jander 			spi_finalize_current_message(ctlr);
1743ae7d2346SDavid Jander 
1744ae7d2346SDavid Jander 			return ret;
1745ae7d2346SDavid Jander 		}
1746ae7d2346SDavid Jander 	}
1747ae7d2346SDavid Jander 
1748ae7d2346SDavid Jander 	trace_spi_message_start(msg);
1749ae7d2346SDavid Jander 
17508d699ff9SVincent Whitchurch 	ret = spi_split_transfers_maxsize(ctlr, msg,
17518d699ff9SVincent Whitchurch 					  spi_max_transfer_size(msg->spi),
17528d699ff9SVincent Whitchurch 					  GFP_KERNEL | GFP_DMA);
17538d699ff9SVincent Whitchurch 	if (ret) {
17548d699ff9SVincent Whitchurch 		msg->status = ret;
17558d699ff9SVincent Whitchurch 		spi_finalize_current_message(ctlr);
17568d699ff9SVincent Whitchurch 		return ret;
17578d699ff9SVincent Whitchurch 	}
17588d699ff9SVincent Whitchurch 
1759ae7d2346SDavid Jander 	if (ctlr->prepare_message) {
1760ae7d2346SDavid Jander 		ret = ctlr->prepare_message(ctlr, msg);
1761ae7d2346SDavid Jander 		if (ret) {
1762ae7d2346SDavid Jander 			dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1763ae7d2346SDavid Jander 				ret);
1764ae7d2346SDavid Jander 			msg->status = ret;
1765ae7d2346SDavid Jander 			spi_finalize_current_message(ctlr);
1766ae7d2346SDavid Jander 			return ret;
1767ae7d2346SDavid Jander 		}
1768ae7d2346SDavid Jander 		msg->prepared = true;
1769ae7d2346SDavid Jander 	}
1770ae7d2346SDavid Jander 
1771ae7d2346SDavid Jander 	ret = spi_map_msg(ctlr, msg);
1772ae7d2346SDavid Jander 	if (ret) {
1773ae7d2346SDavid Jander 		msg->status = ret;
1774ae7d2346SDavid Jander 		spi_finalize_current_message(ctlr);
1775ae7d2346SDavid Jander 		return ret;
1776ae7d2346SDavid Jander 	}
1777ae7d2346SDavid Jander 
1778ae7d2346SDavid Jander 	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1779ae7d2346SDavid Jander 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1780ae7d2346SDavid Jander 			xfer->ptp_sts_word_pre = 0;
1781ae7d2346SDavid Jander 			ptp_read_system_prets(xfer->ptp_sts);
1782ae7d2346SDavid Jander 		}
1783ae7d2346SDavid Jander 	}
1784ae7d2346SDavid Jander 
1785dc302905SDavid Jander 	/*
1786dc302905SDavid Jander 	 * Drivers implementation of transfer_one_message() must arrange for
1787dc302905SDavid Jander 	 * spi_finalize_current_message() to get called. Most drivers will do
1788dc302905SDavid Jander 	 * this in the calling context, but some don't. For those cases, a
1789dc302905SDavid Jander 	 * completion is used to guarantee that this function does not return
1790dc302905SDavid Jander 	 * until spi_finalize_current_message() is done accessing
1791dc302905SDavid Jander 	 * ctlr->cur_msg.
1792dc302905SDavid Jander 	 * Use of the following two flags enable to opportunistically skip the
1793dc302905SDavid Jander 	 * use of the completion since its use involves expensive spin locks.
1794dc302905SDavid Jander 	 * In case of a race with the context that calls
1795dc302905SDavid Jander 	 * spi_finalize_current_message() the completion will always be used,
1796dc302905SDavid Jander 	 * due to strict ordering of these flags using barriers.
1797dc302905SDavid Jander 	 */
1798dc302905SDavid Jander 	WRITE_ONCE(ctlr->cur_msg_incomplete, true);
1799dc302905SDavid Jander 	WRITE_ONCE(ctlr->cur_msg_need_completion, false);
180069fa9590SDavid Jander 	reinit_completion(&ctlr->cur_msg_completion);
180195c8222fSDavid Jander 	smp_wmb(); /* Make these available to spi_finalize_current_message() */
1802dc302905SDavid Jander 
1803ae7d2346SDavid Jander 	ret = ctlr->transfer_one_message(ctlr, msg);
1804ae7d2346SDavid Jander 	if (ret) {
1805ae7d2346SDavid Jander 		dev_err(&ctlr->dev,
1806ae7d2346SDavid Jander 			"failed to transfer one message from queue\n");
1807ae7d2346SDavid Jander 		return ret;
180831d4c1bdSDavid Jander 	}
180931d4c1bdSDavid Jander 
1810dc302905SDavid Jander 	WRITE_ONCE(ctlr->cur_msg_need_completion, true);
181131d4c1bdSDavid Jander 	smp_mb(); /* See spi_finalize_current_message()... */
1812dc302905SDavid Jander 	if (READ_ONCE(ctlr->cur_msg_incomplete))
181369fa9590SDavid Jander 		wait_for_completion(&ctlr->cur_msg_completion);
1814ae7d2346SDavid Jander 
1815ae7d2346SDavid Jander 	return 0;
1816ae7d2346SDavid Jander }
1817ae7d2346SDavid Jander 
1818ffbbdd21SLinus Walleij /**
1819702ca026SAndy Shevchenko  * __spi_pump_messages - function which processes SPI message queue
18208caab75fSGeert Uytterhoeven  * @ctlr: controller to process queue for
1821fc9e0f71SMark Brown  * @in_kthread: true if we are in the context of the message pump thread
1822ffbbdd21SLinus Walleij  *
1823702ca026SAndy Shevchenko  * This function checks if there is any SPI message in the queue that
1824ffbbdd21SLinus Walleij  * needs processing and if so call out to the driver to initialize hardware
1825ffbbdd21SLinus Walleij  * and transfer each message.
1826ffbbdd21SLinus Walleij  *
18270461a414SMark Brown  * Note that it is called both from the kthread itself and also from
18280461a414SMark Brown  * inside spi_sync(); the queue extraction handling at the top of the
18290461a414SMark Brown  * function should deal with this safely.
1830ffbbdd21SLinus Walleij  */
18318caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
1832ffbbdd21SLinus Walleij {
1833d1c44c93SVladimir Oltean 	struct spi_message *msg;
1834ffbbdd21SLinus Walleij 	bool was_busy = false;
1835d1c44c93SVladimir Oltean 	unsigned long flags;
1836ffbbdd21SLinus Walleij 	int ret;
1837ffbbdd21SLinus Walleij 
1838702ca026SAndy Shevchenko 	/* Take the I/O mutex */
1839c1038165SDavid Jander 	mutex_lock(&ctlr->io_mutex);
1840c1038165SDavid Jander 
1841983aee5dSMark Brown 	/* Lock queue */
18428caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
1843983aee5dSMark Brown 
1844983aee5dSMark Brown 	/* Make sure we are not already running a message */
18458711a2abSDavid Jander 	if (ctlr->cur_msg)
1846c1038165SDavid Jander 		goto out_unlock;
1847983aee5dSMark Brown 
1848983aee5dSMark Brown 	/* Check if the queue is idle */
18498caab75fSGeert Uytterhoeven 	if (list_empty(&ctlr->queue) || !ctlr->running) {
18508711a2abSDavid Jander 		if (!ctlr->busy)
1851c1038165SDavid Jander 			goto out_unlock;
1852fc9e0f71SMark Brown 
1853e1268597SMark Brown 		/* Defer any non-atomic teardown to the thread */
1854f0125f1aSMark Brown 		if (!in_kthread) {
1855e1268597SMark Brown 			if (!ctlr->dummy_rx && !ctlr->dummy_tx &&
1856e1268597SMark Brown 			    !ctlr->unprepare_transfer_hardware) {
1857e1268597SMark Brown 				spi_idle_runtime_pm(ctlr);
1858e1268597SMark Brown 				ctlr->busy = false;
1859ae7d2346SDavid Jander 				ctlr->queue_empty = true;
1860e1268597SMark Brown 				trace_spi_controller_idle(ctlr);
1861e1268597SMark Brown 			} else {
186260a883d1SMarek Szyprowski 				kthread_queue_work(ctlr->kworker,
1863f0125f1aSMark Brown 						   &ctlr->pump_messages);
1864e1268597SMark Brown 			}
1865c1038165SDavid Jander 			goto out_unlock;
1866f0125f1aSMark Brown 		}
1867f0125f1aSMark Brown 
1868f0125f1aSMark Brown 		ctlr->busy = false;
1869f0125f1aSMark Brown 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1870f0125f1aSMark Brown 
1871f0125f1aSMark Brown 		kfree(ctlr->dummy_rx);
1872f0125f1aSMark Brown 		ctlr->dummy_rx = NULL;
1873f0125f1aSMark Brown 		kfree(ctlr->dummy_tx);
1874f0125f1aSMark Brown 		ctlr->dummy_tx = NULL;
1875f0125f1aSMark Brown 		if (ctlr->unprepare_transfer_hardware &&
1876f0125f1aSMark Brown 		    ctlr->unprepare_transfer_hardware(ctlr))
1877f0125f1aSMark Brown 			dev_err(&ctlr->dev,
1878f0125f1aSMark Brown 				"failed to unprepare transfer hardware\n");
1879e1268597SMark Brown 		spi_idle_runtime_pm(ctlr);
1880f0125f1aSMark Brown 		trace_spi_controller_idle(ctlr);
1881f0125f1aSMark Brown 
1882f0125f1aSMark Brown 		spin_lock_irqsave(&ctlr->queue_lock, flags);
1883ae7d2346SDavid Jander 		ctlr->queue_empty = true;
1884c1038165SDavid Jander 		goto out_unlock;
1885ffbbdd21SLinus Walleij 	}
1886ffbbdd21SLinus Walleij 
1887ffbbdd21SLinus Walleij 	/* Extract head of queue */
1888d1c44c93SVladimir Oltean 	msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1889d1c44c93SVladimir Oltean 	ctlr->cur_msg = msg;
1890ffbbdd21SLinus Walleij 
1891d1c44c93SVladimir Oltean 	list_del_init(&msg->queue);
18928caab75fSGeert Uytterhoeven 	if (ctlr->busy)
1893ffbbdd21SLinus Walleij 		was_busy = true;
1894ffbbdd21SLinus Walleij 	else
18958caab75fSGeert Uytterhoeven 		ctlr->busy = true;
18968caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1897ffbbdd21SLinus Walleij 
1898ae7d2346SDavid Jander 	ret = __spi_pump_transfer_message(ctlr, msg, was_busy);
189969fa9590SDavid Jander 	kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
1900c191543eSDavid Jander 
190169fa9590SDavid Jander 	ctlr->cur_msg = NULL;
190269fa9590SDavid Jander 	ctlr->fallback = false;
190369fa9590SDavid Jander 
19048caab75fSGeert Uytterhoeven 	mutex_unlock(&ctlr->io_mutex);
190562826970SMark Brown 
190662826970SMark Brown 	/* Prod the scheduler in case transfer_one() was busy waiting */
190749023d2eSJon Hunter 	if (!ret)
190862826970SMark Brown 		cond_resched();
1909c1038165SDavid Jander 	return;
1910c1038165SDavid Jander 
1911c1038165SDavid Jander out_unlock:
19128711a2abSDavid Jander 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1913c1038165SDavid Jander 	mutex_unlock(&ctlr->io_mutex);
1914ffbbdd21SLinus Walleij }
1915ffbbdd21SLinus Walleij 
1916fc9e0f71SMark Brown /**
1917fc9e0f71SMark Brown  * spi_pump_messages - kthread work function which processes spi message queue
19188caab75fSGeert Uytterhoeven  * @work: pointer to kthread work struct contained in the controller struct
1919fc9e0f71SMark Brown  */
1920fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work)
1921fc9e0f71SMark Brown {
19228caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr =
19238caab75fSGeert Uytterhoeven 		container_of(work, struct spi_controller, pump_messages);
1924fc9e0f71SMark Brown 
19258caab75fSGeert Uytterhoeven 	__spi_pump_messages(ctlr, true);
1926fc9e0f71SMark Brown }
1927fc9e0f71SMark Brown 
1928924b5867SDouglas Anderson /**
1929350de7ceSAndy Shevchenko  * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp
1930b42faeeeSVladimir Oltean  * @ctlr: Pointer to the spi_controller structure of the driver
1931b42faeeeSVladimir Oltean  * @xfer: Pointer to the transfer being timestamped
1932862dd2a9SVladimir Oltean  * @progress: How many words (not bytes) have been transferred so far
1933b42faeeeSVladimir Oltean  * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1934b42faeeeSVladimir Oltean  *	      transfer, for less jitter in time measurement. Only compatible
1935b42faeeeSVladimir Oltean  *	      with PIO drivers. If true, must follow up with
1936b42faeeeSVladimir Oltean  *	      spi_take_timestamp_post or otherwise system will crash.
1937b42faeeeSVladimir Oltean  *	      WARNING: for fully predictable results, the CPU frequency must
1938b42faeeeSVladimir Oltean  *	      also be under control (governor).
1939350de7ceSAndy Shevchenko  *
1940350de7ceSAndy Shevchenko  * This is a helper for drivers to collect the beginning of the TX timestamp
1941350de7ceSAndy Shevchenko  * for the requested byte from the SPI transfer. The frequency with which this
1942350de7ceSAndy Shevchenko  * function must be called (once per word, once for the whole transfer, once
1943350de7ceSAndy Shevchenko  * per batch of words etc) is arbitrary as long as the @tx buffer offset is
1944350de7ceSAndy Shevchenko  * greater than or equal to the requested byte at the time of the call. The
1945350de7ceSAndy Shevchenko  * timestamp is only taken once, at the first such call. It is assumed that
1946350de7ceSAndy Shevchenko  * the driver advances its @tx buffer pointer monotonically.
1947b42faeeeSVladimir Oltean  */
1948b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr,
1949b42faeeeSVladimir Oltean 			    struct spi_transfer *xfer,
1950862dd2a9SVladimir Oltean 			    size_t progress, bool irqs_off)
1951b42faeeeSVladimir Oltean {
1952b42faeeeSVladimir Oltean 	if (!xfer->ptp_sts)
1953b42faeeeSVladimir Oltean 		return;
1954b42faeeeSVladimir Oltean 
19556a726824SVladimir Oltean 	if (xfer->timestamped)
1956b42faeeeSVladimir Oltean 		return;
1957b42faeeeSVladimir Oltean 
19586a726824SVladimir Oltean 	if (progress > xfer->ptp_sts_word_pre)
1959b42faeeeSVladimir Oltean 		return;
1960b42faeeeSVladimir Oltean 
1961b42faeeeSVladimir Oltean 	/* Capture the resolution of the timestamp */
1962862dd2a9SVladimir Oltean 	xfer->ptp_sts_word_pre = progress;
1963b42faeeeSVladimir Oltean 
1964b42faeeeSVladimir Oltean 	if (irqs_off) {
1965b42faeeeSVladimir Oltean 		local_irq_save(ctlr->irq_flags);
1966b42faeeeSVladimir Oltean 		preempt_disable();
1967b42faeeeSVladimir Oltean 	}
1968b42faeeeSVladimir Oltean 
1969b42faeeeSVladimir Oltean 	ptp_read_system_prets(xfer->ptp_sts);
1970b42faeeeSVladimir Oltean }
1971b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1972b42faeeeSVladimir Oltean 
1973b42faeeeSVladimir Oltean /**
1974350de7ceSAndy Shevchenko  * spi_take_timestamp_post - helper to collect the end of the TX timestamp
1975b42faeeeSVladimir Oltean  * @ctlr: Pointer to the spi_controller structure of the driver
1976b42faeeeSVladimir Oltean  * @xfer: Pointer to the transfer being timestamped
1977862dd2a9SVladimir Oltean  * @progress: How many words (not bytes) have been transferred so far
1978b42faeeeSVladimir Oltean  * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1979350de7ceSAndy Shevchenko  *
1980350de7ceSAndy Shevchenko  * This is a helper for drivers to collect the end of the TX timestamp for
1981350de7ceSAndy Shevchenko  * the requested byte from the SPI transfer. Can be called with an arbitrary
1982350de7ceSAndy Shevchenko  * frequency: only the first call where @tx exceeds or is equal to the
1983350de7ceSAndy Shevchenko  * requested word will be timestamped.
1984b42faeeeSVladimir Oltean  */
1985b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr,
1986b42faeeeSVladimir Oltean 			     struct spi_transfer *xfer,
1987862dd2a9SVladimir Oltean 			     size_t progress, bool irqs_off)
1988b42faeeeSVladimir Oltean {
1989b42faeeeSVladimir Oltean 	if (!xfer->ptp_sts)
1990b42faeeeSVladimir Oltean 		return;
1991b42faeeeSVladimir Oltean 
19926a726824SVladimir Oltean 	if (xfer->timestamped)
1993b42faeeeSVladimir Oltean 		return;
1994b42faeeeSVladimir Oltean 
1995862dd2a9SVladimir Oltean 	if (progress < xfer->ptp_sts_word_post)
1996b42faeeeSVladimir Oltean 		return;
1997b42faeeeSVladimir Oltean 
1998b42faeeeSVladimir Oltean 	ptp_read_system_postts(xfer->ptp_sts);
1999b42faeeeSVladimir Oltean 
2000b42faeeeSVladimir Oltean 	if (irqs_off) {
2001b42faeeeSVladimir Oltean 		local_irq_restore(ctlr->irq_flags);
2002b42faeeeSVladimir Oltean 		preempt_enable();
2003b42faeeeSVladimir Oltean 	}
2004b42faeeeSVladimir Oltean 
2005b42faeeeSVladimir Oltean 	/* Capture the resolution of the timestamp */
2006862dd2a9SVladimir Oltean 	xfer->ptp_sts_word_post = progress;
2007b42faeeeSVladimir Oltean 
20089d77522bSChristophe JAILLET 	xfer->timestamped = 1;
2009b42faeeeSVladimir Oltean }
2010b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
2011b42faeeeSVladimir Oltean 
2012b42faeeeSVladimir Oltean /**
2013924b5867SDouglas Anderson  * spi_set_thread_rt - set the controller to pump at realtime priority
2014924b5867SDouglas Anderson  * @ctlr: controller to boost priority of
2015924b5867SDouglas Anderson  *
2016924b5867SDouglas Anderson  * This can be called because the controller requested realtime priority
2017924b5867SDouglas Anderson  * (by setting the ->rt value before calling spi_register_controller()) or
2018924b5867SDouglas Anderson  * because a device on the bus said that its transfers needed realtime
2019924b5867SDouglas Anderson  * priority.
2020924b5867SDouglas Anderson  *
2021924b5867SDouglas Anderson  * NOTE: at the moment if any device on a bus says it needs realtime then
2022924b5867SDouglas Anderson  * the thread will be at realtime priority for all transfers on that
2023924b5867SDouglas Anderson  * controller.  If this eventually becomes a problem we may see if we can
2024924b5867SDouglas Anderson  * find a way to boost the priority only temporarily during relevant
2025924b5867SDouglas Anderson  * transfers.
2026924b5867SDouglas Anderson  */
2027924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr)
2028ffbbdd21SLinus Walleij {
2029924b5867SDouglas Anderson 	dev_info(&ctlr->dev,
2030924b5867SDouglas Anderson 		"will run message pump with realtime priority\n");
20316d2b84a4SLinus Torvalds 	sched_set_fifo(ctlr->kworker->task);
2032924b5867SDouglas Anderson }
2033924b5867SDouglas Anderson 
2034924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr)
2035924b5867SDouglas Anderson {
20368caab75fSGeert Uytterhoeven 	ctlr->running = false;
20378caab75fSGeert Uytterhoeven 	ctlr->busy = false;
2038ae7d2346SDavid Jander 	ctlr->queue_empty = true;
2039ffbbdd21SLinus Walleij 
204060a883d1SMarek Szyprowski 	ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev));
204160a883d1SMarek Szyprowski 	if (IS_ERR(ctlr->kworker)) {
204260a883d1SMarek Szyprowski 		dev_err(&ctlr->dev, "failed to create message pump kworker\n");
204360a883d1SMarek Szyprowski 		return PTR_ERR(ctlr->kworker);
2044ffbbdd21SLinus Walleij 	}
204560a883d1SMarek Szyprowski 
20468caab75fSGeert Uytterhoeven 	kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
2047f0125f1aSMark Brown 
2048ffbbdd21SLinus Walleij 	/*
20498caab75fSGeert Uytterhoeven 	 * Controller config will indicate if this controller should run the
2050ffbbdd21SLinus Walleij 	 * message pump with high (realtime) priority to reduce the transfer
2051ffbbdd21SLinus Walleij 	 * latency on the bus by minimising the delay between a transfer
2052ffbbdd21SLinus Walleij 	 * request and the scheduling of the message pump thread. Without this
2053ffbbdd21SLinus Walleij 	 * setting the message pump thread will remain at default priority.
2054ffbbdd21SLinus Walleij 	 */
2055924b5867SDouglas Anderson 	if (ctlr->rt)
2056924b5867SDouglas Anderson 		spi_set_thread_rt(ctlr);
2057ffbbdd21SLinus Walleij 
2058ffbbdd21SLinus Walleij 	return 0;
2059ffbbdd21SLinus Walleij }
2060ffbbdd21SLinus Walleij 
2061ffbbdd21SLinus Walleij /**
2062ffbbdd21SLinus Walleij  * spi_get_next_queued_message() - called by driver to check for queued
2063ffbbdd21SLinus Walleij  * messages
20648caab75fSGeert Uytterhoeven  * @ctlr: the controller to check for queued messages
2065ffbbdd21SLinus Walleij  *
2066ffbbdd21SLinus Walleij  * If there are more messages in the queue, the next message is returned from
2067ffbbdd21SLinus Walleij  * this call.
206897d56dc6SJavier Martinez Canillas  *
206997d56dc6SJavier Martinez Canillas  * Return: the next message in the queue, else NULL if the queue is empty.
2070ffbbdd21SLinus Walleij  */
20718caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
2072ffbbdd21SLinus Walleij {
2073ffbbdd21SLinus Walleij 	struct spi_message *next;
2074ffbbdd21SLinus Walleij 	unsigned long flags;
2075ffbbdd21SLinus Walleij 
207695c8222fSDavid Jander 	/* Get a pointer to the next message, if any */
20778caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
20788caab75fSGeert Uytterhoeven 	next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
20791cfd97f9SAxel Lin 					queue);
20808caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2081ffbbdd21SLinus Walleij 
2082ffbbdd21SLinus Walleij 	return next;
2083ffbbdd21SLinus Walleij }
2084ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
2085ffbbdd21SLinus Walleij 
2086ffbbdd21SLinus Walleij /**
2087ffbbdd21SLinus Walleij  * spi_finalize_current_message() - the current message is complete
20888caab75fSGeert Uytterhoeven  * @ctlr: the controller to return the message to
2089ffbbdd21SLinus Walleij  *
2090ffbbdd21SLinus Walleij  * Called by the driver to notify the core that the message in the front of the
2091ffbbdd21SLinus Walleij  * queue is complete and can be removed from the queue.
2092ffbbdd21SLinus Walleij  */
20938caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr)
2094ffbbdd21SLinus Walleij {
2095b42faeeeSVladimir Oltean 	struct spi_transfer *xfer;
2096ffbbdd21SLinus Walleij 	struct spi_message *mesg;
20972841a5fcSMark Brown 	int ret;
2098ffbbdd21SLinus Walleij 
20998caab75fSGeert Uytterhoeven 	mesg = ctlr->cur_msg;
2100ffbbdd21SLinus Walleij 
2101b42faeeeSVladimir Oltean 	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
2102b42faeeeSVladimir Oltean 		list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
2103b42faeeeSVladimir Oltean 			ptp_read_system_postts(xfer->ptp_sts);
2104b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_post = xfer->len;
2105b42faeeeSVladimir Oltean 		}
2106b42faeeeSVladimir Oltean 	}
2107b42faeeeSVladimir Oltean 
21086a726824SVladimir Oltean 	if (unlikely(ctlr->ptp_sts_supported))
21096a726824SVladimir Oltean 		list_for_each_entry(xfer, &mesg->transfers, transfer_list)
21106a726824SVladimir Oltean 			WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped);
2111f971a207SVladimir Oltean 
21128caab75fSGeert Uytterhoeven 	spi_unmap_msg(ctlr, mesg);
211399adef31SMark Brown 
2114350de7ceSAndy Shevchenko 	/*
2115350de7ceSAndy Shevchenko 	 * In the prepare_messages callback the SPI bus has the opportunity
2116350de7ceSAndy Shevchenko 	 * to split a transfer to smaller chunks.
2117350de7ceSAndy Shevchenko 	 *
2118350de7ceSAndy Shevchenko 	 * Release the split transfers here since spi_map_msg() is done on
2119350de7ceSAndy Shevchenko 	 * the split transfers.
2120b59a7ca1SGustav Wiklander 	 */
2121b59a7ca1SGustav Wiklander 	spi_res_release(ctlr, mesg);
2122b59a7ca1SGustav Wiklander 
21231714582aSDavid Jander 	if (mesg->prepared && ctlr->unprepare_message) {
21248caab75fSGeert Uytterhoeven 		ret = ctlr->unprepare_message(ctlr, mesg);
21252841a5fcSMark Brown 		if (ret) {
21268caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
21278caab75fSGeert Uytterhoeven 				ret);
21282841a5fcSMark Brown 		}
21292841a5fcSMark Brown 	}
2130391949b6SUwe Kleine-König 
21311714582aSDavid Jander 	mesg->prepared = false;
21321714582aSDavid Jander 
2133dc302905SDavid Jander 	WRITE_ONCE(ctlr->cur_msg_incomplete, false);
2134dc302905SDavid Jander 	smp_mb(); /* See __spi_pump_transfer_message()... */
2135dc302905SDavid Jander 	if (READ_ONCE(ctlr->cur_msg_need_completion))
213669fa9590SDavid Jander 		complete(&ctlr->cur_msg_completion);
21378e76ef88SMartin Sperl 
21388e76ef88SMartin Sperl 	trace_spi_message_done(mesg);
21392841a5fcSMark Brown 
2140ffbbdd21SLinus Walleij 	mesg->state = NULL;
2141ffbbdd21SLinus Walleij 	if (mesg->complete)
2142ffbbdd21SLinus Walleij 		mesg->complete(mesg->context);
2143ffbbdd21SLinus Walleij }
2144ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message);
2145ffbbdd21SLinus Walleij 
21468caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr)
2147ffbbdd21SLinus Walleij {
2148ffbbdd21SLinus Walleij 	unsigned long flags;
2149ffbbdd21SLinus Walleij 
21508caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
2151ffbbdd21SLinus Walleij 
21528caab75fSGeert Uytterhoeven 	if (ctlr->running || ctlr->busy) {
21538caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2154ffbbdd21SLinus Walleij 		return -EBUSY;
2155ffbbdd21SLinus Walleij 	}
2156ffbbdd21SLinus Walleij 
21578caab75fSGeert Uytterhoeven 	ctlr->running = true;
21588caab75fSGeert Uytterhoeven 	ctlr->cur_msg = NULL;
21598caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2160ffbbdd21SLinus Walleij 
216160a883d1SMarek Szyprowski 	kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2162ffbbdd21SLinus Walleij 
2163ffbbdd21SLinus Walleij 	return 0;
2164ffbbdd21SLinus Walleij }
2165ffbbdd21SLinus Walleij 
21668caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr)
2167ffbbdd21SLinus Walleij {
2168ffbbdd21SLinus Walleij 	unsigned long flags;
2169ffbbdd21SLinus Walleij 	unsigned limit = 500;
2170ffbbdd21SLinus Walleij 	int ret = 0;
2171ffbbdd21SLinus Walleij 
21728caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
2173ffbbdd21SLinus Walleij 
2174ffbbdd21SLinus Walleij 	/*
2175ffbbdd21SLinus Walleij 	 * This is a bit lame, but is optimized for the common execution path.
21768caab75fSGeert Uytterhoeven 	 * A wait_queue on the ctlr->busy could be used, but then the common
2177ffbbdd21SLinus Walleij 	 * execution path (pump_messages) would be required to call wake_up or
2178ffbbdd21SLinus Walleij 	 * friends on every SPI message. Do this instead.
2179ffbbdd21SLinus Walleij 	 */
21808caab75fSGeert Uytterhoeven 	while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
21818caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2182f97b26b0SAxel Lin 		usleep_range(10000, 11000);
21838caab75fSGeert Uytterhoeven 		spin_lock_irqsave(&ctlr->queue_lock, flags);
2184ffbbdd21SLinus Walleij 	}
2185ffbbdd21SLinus Walleij 
21868caab75fSGeert Uytterhoeven 	if (!list_empty(&ctlr->queue) || ctlr->busy)
2187ffbbdd21SLinus Walleij 		ret = -EBUSY;
2188ffbbdd21SLinus Walleij 	else
21898caab75fSGeert Uytterhoeven 		ctlr->running = false;
2190ffbbdd21SLinus Walleij 
21918caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2192ffbbdd21SLinus Walleij 
2193ffbbdd21SLinus Walleij 	return ret;
2194ffbbdd21SLinus Walleij }
2195ffbbdd21SLinus Walleij 
21968caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr)
2197ffbbdd21SLinus Walleij {
2198ffbbdd21SLinus Walleij 	int ret;
2199ffbbdd21SLinus Walleij 
22008caab75fSGeert Uytterhoeven 	ret = spi_stop_queue(ctlr);
2201ffbbdd21SLinus Walleij 
2202ffbbdd21SLinus Walleij 	/*
22033989144fSPetr Mladek 	 * kthread_flush_worker will block until all work is done.
2204ffbbdd21SLinus Walleij 	 * If the reason that stop_queue timed out is that the work will never
2205ffbbdd21SLinus Walleij 	 * finish, then it does no good to call flush/stop thread, so
2206ffbbdd21SLinus Walleij 	 * return anyway.
2207ffbbdd21SLinus Walleij 	 */
2208ffbbdd21SLinus Walleij 	if (ret) {
22098caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem destroying queue\n");
2210ffbbdd21SLinus Walleij 		return ret;
2211ffbbdd21SLinus Walleij 	}
2212ffbbdd21SLinus Walleij 
221360a883d1SMarek Szyprowski 	kthread_destroy_worker(ctlr->kworker);
2214ffbbdd21SLinus Walleij 
2215ffbbdd21SLinus Walleij 	return 0;
2216ffbbdd21SLinus Walleij }
2217ffbbdd21SLinus Walleij 
22180461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi,
22190461a414SMark Brown 				 struct spi_message *msg,
22200461a414SMark Brown 				 bool need_pump)
2221ffbbdd21SLinus Walleij {
22228caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
2223ffbbdd21SLinus Walleij 	unsigned long flags;
2224ffbbdd21SLinus Walleij 
22258caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
2226ffbbdd21SLinus Walleij 
22278caab75fSGeert Uytterhoeven 	if (!ctlr->running) {
22288caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2229ffbbdd21SLinus Walleij 		return -ESHUTDOWN;
2230ffbbdd21SLinus Walleij 	}
2231ffbbdd21SLinus Walleij 	msg->actual_length = 0;
2232ffbbdd21SLinus Walleij 	msg->status = -EINPROGRESS;
2233ffbbdd21SLinus Walleij 
22348caab75fSGeert Uytterhoeven 	list_add_tail(&msg->queue, &ctlr->queue);
2235ae7d2346SDavid Jander 	ctlr->queue_empty = false;
2236f0125f1aSMark Brown 	if (!ctlr->busy && need_pump)
223760a883d1SMarek Szyprowski 		kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2238ffbbdd21SLinus Walleij 
22398caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2240ffbbdd21SLinus Walleij 	return 0;
2241ffbbdd21SLinus Walleij }
2242ffbbdd21SLinus Walleij 
22430461a414SMark Brown /**
22440461a414SMark Brown  * spi_queued_transfer - transfer function for queued transfers
2245702ca026SAndy Shevchenko  * @spi: SPI device which is requesting transfer
2246702ca026SAndy Shevchenko  * @msg: SPI message which is to handled is queued to driver queue
224797d56dc6SJavier Martinez Canillas  *
224897d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
22490461a414SMark Brown  */
22500461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
22510461a414SMark Brown {
22520461a414SMark Brown 	return __spi_queued_transfer(spi, msg, true);
22530461a414SMark Brown }
22540461a414SMark Brown 
22558caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr)
2256ffbbdd21SLinus Walleij {
2257ffbbdd21SLinus Walleij 	int ret;
2258ffbbdd21SLinus Walleij 
22598caab75fSGeert Uytterhoeven 	ctlr->transfer = spi_queued_transfer;
22608caab75fSGeert Uytterhoeven 	if (!ctlr->transfer_one_message)
22618caab75fSGeert Uytterhoeven 		ctlr->transfer_one_message = spi_transfer_one_message;
2262ffbbdd21SLinus Walleij 
2263ffbbdd21SLinus Walleij 	/* Initialize and start queue */
22648caab75fSGeert Uytterhoeven 	ret = spi_init_queue(ctlr);
2265ffbbdd21SLinus Walleij 	if (ret) {
22668caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem initializing queue\n");
2267ffbbdd21SLinus Walleij 		goto err_init_queue;
2268ffbbdd21SLinus Walleij 	}
22698caab75fSGeert Uytterhoeven 	ctlr->queued = true;
22708caab75fSGeert Uytterhoeven 	ret = spi_start_queue(ctlr);
2271ffbbdd21SLinus Walleij 	if (ret) {
22728caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem starting queue\n");
2273ffbbdd21SLinus Walleij 		goto err_start_queue;
2274ffbbdd21SLinus Walleij 	}
2275ffbbdd21SLinus Walleij 
2276ffbbdd21SLinus Walleij 	return 0;
2277ffbbdd21SLinus Walleij 
2278ffbbdd21SLinus Walleij err_start_queue:
22798caab75fSGeert Uytterhoeven 	spi_destroy_queue(ctlr);
2280c3676d5cSMark Brown err_init_queue:
2281ffbbdd21SLinus Walleij 	return ret;
2282ffbbdd21SLinus Walleij }
2283ffbbdd21SLinus Walleij 
2284988f259bSBoris Brezillon /**
2285988f259bSBoris Brezillon  * spi_flush_queue - Send all pending messages in the queue from the callers'
2286988f259bSBoris Brezillon  *		     context
2287988f259bSBoris Brezillon  * @ctlr: controller to process queue for
2288988f259bSBoris Brezillon  *
2289988f259bSBoris Brezillon  * This should be used when one wants to ensure all pending messages have been
2290988f259bSBoris Brezillon  * sent before doing something. Is used by the spi-mem code to make sure SPI
2291988f259bSBoris Brezillon  * memory operations do not preempt regular SPI transfers that have been queued
2292988f259bSBoris Brezillon  * before the spi-mem operation.
2293988f259bSBoris Brezillon  */
2294988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr)
2295988f259bSBoris Brezillon {
2296988f259bSBoris Brezillon 	if (ctlr->transfer == spi_queued_transfer)
2297988f259bSBoris Brezillon 		__spi_pump_messages(ctlr, false);
2298988f259bSBoris Brezillon }
2299988f259bSBoris Brezillon 
2300ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/
2301ffbbdd21SLinus Walleij 
23027cb94361SAndreas Larsson #if defined(CONFIG_OF)
2303f276aacfSJanne Grunau static void of_spi_parse_dt_cs_delay(struct device_node *nc,
2304f276aacfSJanne Grunau 				     struct spi_delay *delay, const char *prop)
2305f276aacfSJanne Grunau {
2306f276aacfSJanne Grunau 	u32 value;
2307f276aacfSJanne Grunau 
2308f276aacfSJanne Grunau 	if (!of_property_read_u32(nc, prop, &value)) {
2309f276aacfSJanne Grunau 		if (value > U16_MAX) {
2310f276aacfSJanne Grunau 			delay->value = DIV_ROUND_UP(value, 1000);
2311f276aacfSJanne Grunau 			delay->unit = SPI_DELAY_UNIT_USECS;
2312f276aacfSJanne Grunau 		} else {
2313f276aacfSJanne Grunau 			delay->value = value;
2314f276aacfSJanne Grunau 			delay->unit = SPI_DELAY_UNIT_NSECS;
2315f276aacfSJanne Grunau 		}
2316f276aacfSJanne Grunau 	}
2317f276aacfSJanne Grunau }
2318f276aacfSJanne Grunau 
23198caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
2320c2e51ac3SGeert Uytterhoeven 			   struct device_node *nc)
2321d57a4282SGrant Likely {
23224d8ff6b0SAmit Kumar Mahapatra 	u32 value, cs[SPI_CS_CNT_MAX];
23234d8ff6b0SAmit Kumar Mahapatra 	int rc, idx;
2324d57a4282SGrant Likely 
2325d57a4282SGrant Likely 	/* Mode (clock phase/polarity/etc.) */
2326e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-cpha"))
2327d57a4282SGrant Likely 		spi->mode |= SPI_CPHA;
2328e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-cpol"))
2329d57a4282SGrant Likely 		spi->mode |= SPI_CPOL;
2330e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-3wire"))
2331c20151dfSLars-Peter Clausen 		spi->mode |= SPI_3WIRE;
2332e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-lsb-first"))
2333cd6339e6SZhao Qiang 		spi->mode |= SPI_LSB_FIRST;
23343e5ec1dbSGregory CLEMENT 	if (of_property_read_bool(nc, "spi-cs-high"))
2335f3186dd8SLinus Walleij 		spi->mode |= SPI_CS_HIGH;
2336f3186dd8SLinus Walleij 
2337f477b7fbSwangyuhang 	/* Device DUAL/QUAD mode */
233889da4293STrent Piepho 	if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
233989da4293STrent Piepho 		switch (value) {
2340d962608cSDragos Bogdan 		case 0:
2341d962608cSDragos Bogdan 			spi->mode |= SPI_NO_TX;
2342d962608cSDragos Bogdan 			break;
234389da4293STrent Piepho 		case 1:
2344f477b7fbSwangyuhang 			break;
234589da4293STrent Piepho 		case 2:
2346f477b7fbSwangyuhang 			spi->mode |= SPI_TX_DUAL;
2347f477b7fbSwangyuhang 			break;
234889da4293STrent Piepho 		case 4:
2349f477b7fbSwangyuhang 			spi->mode |= SPI_TX_QUAD;
2350f477b7fbSwangyuhang 			break;
23516b03061fSYogesh Narayan Gaur 		case 8:
23526b03061fSYogesh Narayan Gaur 			spi->mode |= SPI_TX_OCTAL;
23536b03061fSYogesh Narayan Gaur 			break;
2354f477b7fbSwangyuhang 		default:
23558caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
2356a110f93dSwangyuhang 				"spi-tx-bus-width %d not supported\n",
235789da4293STrent Piepho 				value);
235880874d8cSGeert Uytterhoeven 			break;
2359f477b7fbSwangyuhang 		}
2360a822e99cSMark Brown 	}
2361f477b7fbSwangyuhang 
236289da4293STrent Piepho 	if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
236389da4293STrent Piepho 		switch (value) {
2364d962608cSDragos Bogdan 		case 0:
2365d962608cSDragos Bogdan 			spi->mode |= SPI_NO_RX;
2366d962608cSDragos Bogdan 			break;
236789da4293STrent Piepho 		case 1:
2368f477b7fbSwangyuhang 			break;
236989da4293STrent Piepho 		case 2:
2370f477b7fbSwangyuhang 			spi->mode |= SPI_RX_DUAL;
2371f477b7fbSwangyuhang 			break;
237289da4293STrent Piepho 		case 4:
2373f477b7fbSwangyuhang 			spi->mode |= SPI_RX_QUAD;
2374f477b7fbSwangyuhang 			break;
23756b03061fSYogesh Narayan Gaur 		case 8:
23766b03061fSYogesh Narayan Gaur 			spi->mode |= SPI_RX_OCTAL;
23776b03061fSYogesh Narayan Gaur 			break;
2378f477b7fbSwangyuhang 		default:
23798caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
2380a110f93dSwangyuhang 				"spi-rx-bus-width %d not supported\n",
238189da4293STrent Piepho 				value);
238280874d8cSGeert Uytterhoeven 			break;
2383f477b7fbSwangyuhang 		}
2384a822e99cSMark Brown 	}
2385f477b7fbSwangyuhang 
23868caab75fSGeert Uytterhoeven 	if (spi_controller_is_slave(ctlr)) {
2387194276b0SRob Herring 		if (!of_node_name_eq(nc, "slave")) {
238825c56c88SRob Herring 			dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
238925c56c88SRob Herring 				nc);
23906c364062SGeert Uytterhoeven 			return -EINVAL;
23916c364062SGeert Uytterhoeven 		}
23926c364062SGeert Uytterhoeven 		return 0;
23936c364062SGeert Uytterhoeven 	}
23946c364062SGeert Uytterhoeven 
23954d8ff6b0SAmit Kumar Mahapatra 	if (ctlr->num_chipselect > SPI_CS_CNT_MAX) {
23964d8ff6b0SAmit Kumar Mahapatra 		dev_err(&ctlr->dev, "No. of CS is more than max. no. of supported CS\n");
23974d8ff6b0SAmit Kumar Mahapatra 		return -EINVAL;
23984d8ff6b0SAmit Kumar Mahapatra 	}
23994d8ff6b0SAmit Kumar Mahapatra 
24004d8ff6b0SAmit Kumar Mahapatra 	/*
24014d8ff6b0SAmit Kumar Mahapatra 	 * Zero(0) is a valid physical CS value and can be located at any
24024d8ff6b0SAmit Kumar Mahapatra 	 * logical CS in the spi->chip_select[]. If all the physical CS
24034d8ff6b0SAmit Kumar Mahapatra 	 * are initialized to 0 then It would be difficult to differentiate
24044d8ff6b0SAmit Kumar Mahapatra 	 * between a valid physical CS 0 & an unused logical CS whose physical
24054d8ff6b0SAmit Kumar Mahapatra 	 * CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
24064d8ff6b0SAmit Kumar Mahapatra 	 * Now all the unused logical CS will have 0xFF physical CS value & can be
24074d8ff6b0SAmit Kumar Mahapatra 	 * ignore while performing physical CS validity checks.
24084d8ff6b0SAmit Kumar Mahapatra 	 */
24094d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
24104d8ff6b0SAmit Kumar Mahapatra 		spi_set_chipselect(spi, idx, 0xFF);
24114d8ff6b0SAmit Kumar Mahapatra 
24126c364062SGeert Uytterhoeven 	/* Device address */
24134d8ff6b0SAmit Kumar Mahapatra 	rc = of_property_read_variable_u32_array(nc, "reg", &cs[0], 1,
24144d8ff6b0SAmit Kumar Mahapatra 						 SPI_CS_CNT_MAX);
24154d8ff6b0SAmit Kumar Mahapatra 	if (rc < 0) {
241625c56c88SRob Herring 		dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
241725c56c88SRob Herring 			nc, rc);
24186c364062SGeert Uytterhoeven 		return rc;
24196c364062SGeert Uytterhoeven 	}
24204d8ff6b0SAmit Kumar Mahapatra 	if (rc > ctlr->num_chipselect) {
24214d8ff6b0SAmit Kumar Mahapatra 		dev_err(&ctlr->dev, "%pOF has number of CS > ctlr->num_chipselect (%d)\n",
24224d8ff6b0SAmit Kumar Mahapatra 			nc, rc);
24234d8ff6b0SAmit Kumar Mahapatra 		return rc;
24244d8ff6b0SAmit Kumar Mahapatra 	}
24254d8ff6b0SAmit Kumar Mahapatra 	if ((of_property_read_bool(nc, "parallel-memories")) &&
24264d8ff6b0SAmit Kumar Mahapatra 	    (!(ctlr->flags & SPI_CONTROLLER_MULTI_CS))) {
24274d8ff6b0SAmit Kumar Mahapatra 		dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n");
24284d8ff6b0SAmit Kumar Mahapatra 		return -EINVAL;
24294d8ff6b0SAmit Kumar Mahapatra 	}
24304d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < rc; idx++)
24314d8ff6b0SAmit Kumar Mahapatra 		spi_set_chipselect(spi, idx, cs[idx]);
24324d8ff6b0SAmit Kumar Mahapatra 
24334d8ff6b0SAmit Kumar Mahapatra 	/*
24344d8ff6b0SAmit Kumar Mahapatra 	 * spi->chip_select[i] gives the corresponding physical CS for logical CS i
24354d8ff6b0SAmit Kumar Mahapatra 	 * logical CS number is represented by setting the ith bit in spi->cs_index_mask
24364d8ff6b0SAmit Kumar Mahapatra 	 * So, for example, if spi->cs_index_mask = 0x01 then logical CS number is 0 and
24374d8ff6b0SAmit Kumar Mahapatra 	 * spi->chip_select[0] will give the physical CS.
24384d8ff6b0SAmit Kumar Mahapatra 	 * By default spi->chip_select[0] will hold the physical CS number so, set
24394d8ff6b0SAmit Kumar Mahapatra 	 * spi->cs_index_mask as 0x01.
24404d8ff6b0SAmit Kumar Mahapatra 	 */
24414d8ff6b0SAmit Kumar Mahapatra 	spi->cs_index_mask = 0x01;
24426c364062SGeert Uytterhoeven 
2443d57a4282SGrant Likely 	/* Device speed */
2444671c3bf5SChuanhong Guo 	if (!of_property_read_u32(nc, "spi-max-frequency", &value))
244589da4293STrent Piepho 		spi->max_speed_hz = value;
2446d57a4282SGrant Likely 
2447f276aacfSJanne Grunau 	/* Device CS delays */
2448f276aacfSJanne Grunau 	of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns");
24495827b31dSJanne Grunau 	of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns");
24505827b31dSJanne Grunau 	of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns");
245133a2fde5STudor Ambarus 
2452c2e51ac3SGeert Uytterhoeven 	return 0;
2453c2e51ac3SGeert Uytterhoeven }
2454c2e51ac3SGeert Uytterhoeven 
2455c2e51ac3SGeert Uytterhoeven static struct spi_device *
24568caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
2457c2e51ac3SGeert Uytterhoeven {
2458c2e51ac3SGeert Uytterhoeven 	struct spi_device *spi;
2459c2e51ac3SGeert Uytterhoeven 	int rc;
2460c2e51ac3SGeert Uytterhoeven 
2461c2e51ac3SGeert Uytterhoeven 	/* Alloc an spi_device */
24628caab75fSGeert Uytterhoeven 	spi = spi_alloc_device(ctlr);
2463c2e51ac3SGeert Uytterhoeven 	if (!spi) {
246425c56c88SRob Herring 		dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
2465c2e51ac3SGeert Uytterhoeven 		rc = -ENOMEM;
2466c2e51ac3SGeert Uytterhoeven 		goto err_out;
2467c2e51ac3SGeert Uytterhoeven 	}
2468c2e51ac3SGeert Uytterhoeven 
2469c2e51ac3SGeert Uytterhoeven 	/* Select device driver */
2470673aa1edSMiquel Raynal 	rc = of_alias_from_compatible(nc, spi->modalias,
2471c2e51ac3SGeert Uytterhoeven 				      sizeof(spi->modalias));
2472c2e51ac3SGeert Uytterhoeven 	if (rc < 0) {
247325c56c88SRob Herring 		dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
2474c2e51ac3SGeert Uytterhoeven 		goto err_out;
2475c2e51ac3SGeert Uytterhoeven 	}
2476c2e51ac3SGeert Uytterhoeven 
24778caab75fSGeert Uytterhoeven 	rc = of_spi_parse_dt(ctlr, spi, nc);
2478c2e51ac3SGeert Uytterhoeven 	if (rc)
2479c2e51ac3SGeert Uytterhoeven 		goto err_out;
2480c2e51ac3SGeert Uytterhoeven 
2481d57a4282SGrant Likely 	/* Store a pointer to the node in the device structure */
2482d57a4282SGrant Likely 	of_node_get(nc);
2483c7cc588bSAndy Shevchenko 
2484c7cc588bSAndy Shevchenko 	device_set_node(&spi->dev, of_fwnode_handle(nc));
2485d57a4282SGrant Likely 
2486d57a4282SGrant Likely 	/* Register the new device */
2487d57a4282SGrant Likely 	rc = spi_add_device(spi);
2488d57a4282SGrant Likely 	if (rc) {
248925c56c88SRob Herring 		dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
24908324147fSJohan Hovold 		goto err_of_node_put;
2491d57a4282SGrant Likely 	}
2492d57a4282SGrant Likely 
2493aff5e3f8SPantelis Antoniou 	return spi;
2494aff5e3f8SPantelis Antoniou 
24958324147fSJohan Hovold err_of_node_put:
24968324147fSJohan Hovold 	of_node_put(nc);
2497aff5e3f8SPantelis Antoniou err_out:
2498aff5e3f8SPantelis Antoniou 	spi_dev_put(spi);
2499aff5e3f8SPantelis Antoniou 	return ERR_PTR(rc);
2500aff5e3f8SPantelis Antoniou }
2501aff5e3f8SPantelis Antoniou 
2502aff5e3f8SPantelis Antoniou /**
2503aff5e3f8SPantelis Antoniou  * of_register_spi_devices() - Register child devices onto the SPI bus
25048caab75fSGeert Uytterhoeven  * @ctlr:	Pointer to spi_controller device
2505aff5e3f8SPantelis Antoniou  *
25066c364062SGeert Uytterhoeven  * Registers an spi_device for each child node of controller node which
25076c364062SGeert Uytterhoeven  * represents a valid SPI slave.
2508aff5e3f8SPantelis Antoniou  */
25098caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr)
2510aff5e3f8SPantelis Antoniou {
2511aff5e3f8SPantelis Antoniou 	struct spi_device *spi;
2512aff5e3f8SPantelis Antoniou 	struct device_node *nc;
2513aff5e3f8SPantelis Antoniou 
25148caab75fSGeert Uytterhoeven 	for_each_available_child_of_node(ctlr->dev.of_node, nc) {
2515bd6c1644SGeert Uytterhoeven 		if (of_node_test_and_set_flag(nc, OF_POPULATED))
2516bd6c1644SGeert Uytterhoeven 			continue;
25178caab75fSGeert Uytterhoeven 		spi = of_register_spi_device(ctlr, nc);
2518e0af98a7SRalf Ramsauer 		if (IS_ERR(spi)) {
25198caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
252025c56c88SRob Herring 				 "Failed to create SPI device for %pOF\n", nc);
2521e0af98a7SRalf Ramsauer 			of_node_clear_flag(nc, OF_POPULATED);
2522e0af98a7SRalf Ramsauer 		}
2523d57a4282SGrant Likely 	}
2524d57a4282SGrant Likely }
2525d57a4282SGrant Likely #else
25268caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { }
2527d57a4282SGrant Likely #endif
2528d57a4282SGrant Likely 
25290c79378cSSebastian Reichel /**
25300c79378cSSebastian Reichel  * spi_new_ancillary_device() - Register ancillary SPI device
25310c79378cSSebastian Reichel  * @spi:         Pointer to the main SPI device registering the ancillary device
25320c79378cSSebastian Reichel  * @chip_select: Chip Select of the ancillary device
25330c79378cSSebastian Reichel  *
25340c79378cSSebastian Reichel  * Register an ancillary SPI device; for example some chips have a chip-select
25350c79378cSSebastian Reichel  * for normal device usage and another one for setup/firmware upload.
25360c79378cSSebastian Reichel  *
25370c79378cSSebastian Reichel  * This may only be called from main SPI device's probe routine.
25380c79378cSSebastian Reichel  *
25390c79378cSSebastian Reichel  * Return: 0 on success; negative errno on failure
25400c79378cSSebastian Reichel  */
25410c79378cSSebastian Reichel struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
25420c79378cSSebastian Reichel 					     u8 chip_select)
25430c79378cSSebastian Reichel {
25447b5c6a54SAndy Shevchenko 	struct spi_controller *ctlr = spi->controller;
25450c79378cSSebastian Reichel 	struct spi_device *ancillary;
25460c79378cSSebastian Reichel 	int rc = 0;
25474d8ff6b0SAmit Kumar Mahapatra 	u8 idx;
25480c79378cSSebastian Reichel 
25490c79378cSSebastian Reichel 	/* Alloc an spi_device */
25507b5c6a54SAndy Shevchenko 	ancillary = spi_alloc_device(ctlr);
25510c79378cSSebastian Reichel 	if (!ancillary) {
25520c79378cSSebastian Reichel 		rc = -ENOMEM;
25530c79378cSSebastian Reichel 		goto err_out;
25540c79378cSSebastian Reichel 	}
25550c79378cSSebastian Reichel 
255651e99de5SWolfram Sang 	strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias));
25570c79378cSSebastian Reichel 
25584d8ff6b0SAmit Kumar Mahapatra 	/*
25594d8ff6b0SAmit Kumar Mahapatra 	 * Zero(0) is a valid physical CS value and can be located at any
25604d8ff6b0SAmit Kumar Mahapatra 	 * logical CS in the spi->chip_select[]. If all the physical CS
25614d8ff6b0SAmit Kumar Mahapatra 	 * are initialized to 0 then It would be difficult to differentiate
25624d8ff6b0SAmit Kumar Mahapatra 	 * between a valid physical CS 0 & an unused logical CS whose physical
25634d8ff6b0SAmit Kumar Mahapatra 	 * CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
25644d8ff6b0SAmit Kumar Mahapatra 	 * Now all the unused logical CS will have 0xFF physical CS value & can be
25654d8ff6b0SAmit Kumar Mahapatra 	 * ignore while performing physical CS validity checks.
25664d8ff6b0SAmit Kumar Mahapatra 	 */
25674d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
25684d8ff6b0SAmit Kumar Mahapatra 		spi_set_chipselect(ancillary, idx, 0xFF);
25694d8ff6b0SAmit Kumar Mahapatra 
25700c79378cSSebastian Reichel 	/* Use provided chip-select for ancillary device */
2571303feb3cSAmit Kumar Mahapatra 	spi_set_chipselect(ancillary, 0, chip_select);
25720c79378cSSebastian Reichel 
25730c79378cSSebastian Reichel 	/* Take over SPI mode/speed from SPI main device */
25740c79378cSSebastian Reichel 	ancillary->max_speed_hz = spi->max_speed_hz;
2575b01d5506SColin Ian King 	ancillary->mode = spi->mode;
25764d8ff6b0SAmit Kumar Mahapatra 	/*
25774d8ff6b0SAmit Kumar Mahapatra 	 * spi->chip_select[i] gives the corresponding physical CS for logical CS i
25784d8ff6b0SAmit Kumar Mahapatra 	 * logical CS number is represented by setting the ith bit in spi->cs_index_mask
25794d8ff6b0SAmit Kumar Mahapatra 	 * So, for example, if spi->cs_index_mask = 0x01 then logical CS number is 0 and
25804d8ff6b0SAmit Kumar Mahapatra 	 * spi->chip_select[0] will give the physical CS.
25814d8ff6b0SAmit Kumar Mahapatra 	 * By default spi->chip_select[0] will hold the physical CS number so, set
25824d8ff6b0SAmit Kumar Mahapatra 	 * spi->cs_index_mask as 0x01.
25834d8ff6b0SAmit Kumar Mahapatra 	 */
25844d8ff6b0SAmit Kumar Mahapatra 	ancillary->cs_index_mask = 0x01;
25850c79378cSSebastian Reichel 
25867b5c6a54SAndy Shevchenko 	WARN_ON(!mutex_is_locked(&ctlr->add_lock));
25877b5c6a54SAndy Shevchenko 
25880c79378cSSebastian Reichel 	/* Register the new device */
25897b5c6a54SAndy Shevchenko 	rc = __spi_add_device(ancillary);
25900c79378cSSebastian Reichel 	if (rc) {
25910c79378cSSebastian Reichel 		dev_err(&spi->dev, "failed to register ancillary device\n");
25920c79378cSSebastian Reichel 		goto err_out;
25930c79378cSSebastian Reichel 	}
25940c79378cSSebastian Reichel 
25950c79378cSSebastian Reichel 	return ancillary;
25960c79378cSSebastian Reichel 
25970c79378cSSebastian Reichel err_out:
25980c79378cSSebastian Reichel 	spi_dev_put(ancillary);
25990c79378cSSebastian Reichel 	return ERR_PTR(rc);
26000c79378cSSebastian Reichel }
26010c79378cSSebastian Reichel EXPORT_SYMBOL_GPL(spi_new_ancillary_device);
26020c79378cSSebastian Reichel 
260364bee4d2SMika Westerberg #ifdef CONFIG_ACPI
26044c3c5954SArd Biesheuvel struct acpi_spi_lookup {
26054c3c5954SArd Biesheuvel 	struct spi_controller 	*ctlr;
26064c3c5954SArd Biesheuvel 	u32			max_speed_hz;
26074c3c5954SArd Biesheuvel 	u32			mode;
26084c3c5954SArd Biesheuvel 	int			irq;
26094c3c5954SArd Biesheuvel 	u8			bits_per_word;
26104c3c5954SArd Biesheuvel 	u8			chip_select;
261187e59b36SStefan Binding 	int			n;
261287e59b36SStefan Binding 	int			index;
26134c3c5954SArd Biesheuvel };
26144c3c5954SArd Biesheuvel 
2615e612af7aSStefan Binding static int acpi_spi_count(struct acpi_resource *ares, void *data)
2616e612af7aSStefan Binding {
2617e612af7aSStefan Binding 	struct acpi_resource_spi_serialbus *sb;
2618e612af7aSStefan Binding 	int *count = data;
2619e612af7aSStefan Binding 
2620e612af7aSStefan Binding 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2621e612af7aSStefan Binding 		return 1;
2622e612af7aSStefan Binding 
2623e612af7aSStefan Binding 	sb = &ares->data.spi_serial_bus;
2624e612af7aSStefan Binding 	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI)
2625e612af7aSStefan Binding 		return 1;
2626e612af7aSStefan Binding 
2627e612af7aSStefan Binding 	*count = *count + 1;
2628e612af7aSStefan Binding 
2629e612af7aSStefan Binding 	return 1;
2630e612af7aSStefan Binding }
2631e612af7aSStefan Binding 
2632e612af7aSStefan Binding /**
2633e612af7aSStefan Binding  * acpi_spi_count_resources - Count the number of SpiSerialBus resources
2634e612af7aSStefan Binding  * @adev:	ACPI device
2635e612af7aSStefan Binding  *
2636702ca026SAndy Shevchenko  * Return: the number of SpiSerialBus resources in the ACPI-device's
2637e612af7aSStefan Binding  * resource-list; or a negative error code.
2638e612af7aSStefan Binding  */
2639e612af7aSStefan Binding int acpi_spi_count_resources(struct acpi_device *adev)
2640e612af7aSStefan Binding {
2641e612af7aSStefan Binding 	LIST_HEAD(r);
2642e612af7aSStefan Binding 	int count = 0;
2643e612af7aSStefan Binding 	int ret;
2644e612af7aSStefan Binding 
2645e612af7aSStefan Binding 	ret = acpi_dev_get_resources(adev, &r, acpi_spi_count, &count);
2646e612af7aSStefan Binding 	if (ret < 0)
2647e612af7aSStefan Binding 		return ret;
2648e612af7aSStefan Binding 
2649e612af7aSStefan Binding 	acpi_dev_free_resource_list(&r);
2650e612af7aSStefan Binding 
2651e612af7aSStefan Binding 	return count;
2652e612af7aSStefan Binding }
2653e612af7aSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_count_resources);
2654e612af7aSStefan Binding 
26554c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
26564c3c5954SArd Biesheuvel 					    struct acpi_spi_lookup *lookup)
26578a2e487eSLukas Wunner {
26588a2e487eSLukas Wunner 	const union acpi_object *obj;
26598a2e487eSLukas Wunner 
26608a2e487eSLukas Wunner 	if (!x86_apple_machine)
26618a2e487eSLukas Wunner 		return;
26628a2e487eSLukas Wunner 
26638a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
26648a2e487eSLukas Wunner 	    && obj->buffer.length >= 4)
26654c3c5954SArd Biesheuvel 		lookup->max_speed_hz  = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
26668a2e487eSLukas Wunner 
26678a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
26688a2e487eSLukas Wunner 	    && obj->buffer.length == 8)
26694c3c5954SArd Biesheuvel 		lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
26708a2e487eSLukas Wunner 
26718a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
26728a2e487eSLukas Wunner 	    && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
26734c3c5954SArd Biesheuvel 		lookup->mode |= SPI_LSB_FIRST;
26748a2e487eSLukas Wunner 
26758a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
26768a2e487eSLukas Wunner 	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
26774c3c5954SArd Biesheuvel 		lookup->mode |= SPI_CPOL;
26788a2e487eSLukas Wunner 
26798a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
26808a2e487eSLukas Wunner 	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
26814c3c5954SArd Biesheuvel 		lookup->mode |= SPI_CPHA;
26828a2e487eSLukas Wunner }
26838a2e487eSLukas Wunner 
268464bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
268564bee4d2SMika Westerberg {
26864c3c5954SArd Biesheuvel 	struct acpi_spi_lookup *lookup = data;
26874c3c5954SArd Biesheuvel 	struct spi_controller *ctlr = lookup->ctlr;
268864bee4d2SMika Westerberg 
268964bee4d2SMika Westerberg 	if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
269064bee4d2SMika Westerberg 		struct acpi_resource_spi_serialbus *sb;
26914c3c5954SArd Biesheuvel 		acpi_handle parent_handle;
26924c3c5954SArd Biesheuvel 		acpi_status status;
269364bee4d2SMika Westerberg 
269464bee4d2SMika Westerberg 		sb = &ares->data.spi_serial_bus;
269564bee4d2SMika Westerberg 		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
26964c3c5954SArd Biesheuvel 
269787e59b36SStefan Binding 			if (lookup->index != -1 && lookup->n++ != lookup->index)
269887e59b36SStefan Binding 				return 1;
269987e59b36SStefan Binding 
27004c3c5954SArd Biesheuvel 			status = acpi_get_handle(NULL,
27014c3c5954SArd Biesheuvel 						 sb->resource_source.string_ptr,
27024c3c5954SArd Biesheuvel 						 &parent_handle);
27034c3c5954SArd Biesheuvel 
270487e59b36SStefan Binding 			if (ACPI_FAILURE(status))
27054c3c5954SArd Biesheuvel 				return -ENODEV;
27064c3c5954SArd Biesheuvel 
270787e59b36SStefan Binding 			if (ctlr) {
270887e59b36SStefan Binding 				if (ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
270987e59b36SStefan Binding 					return -ENODEV;
271087e59b36SStefan Binding 			} else {
271187e59b36SStefan Binding 				struct acpi_device *adev;
271287e59b36SStefan Binding 
2713ac2a3feeSRafael J. Wysocki 				adev = acpi_fetch_acpi_dev(parent_handle);
2714ac2a3feeSRafael J. Wysocki 				if (!adev)
271587e59b36SStefan Binding 					return -ENODEV;
271687e59b36SStefan Binding 
271787e59b36SStefan Binding 				ctlr = acpi_spi_find_controller_by_adev(adev);
271887e59b36SStefan Binding 				if (!ctlr)
27199c22ec4aSAndy Shevchenko 					return -EPROBE_DEFER;
272087e59b36SStefan Binding 
272187e59b36SStefan Binding 				lookup->ctlr = ctlr;
272287e59b36SStefan Binding 			}
272387e59b36SStefan Binding 
2724a0a90718SMika Westerberg 			/*
2725a0a90718SMika Westerberg 			 * ACPI DeviceSelection numbering is handled by the
2726a0a90718SMika Westerberg 			 * host controller driver in Windows and can vary
2727a0a90718SMika Westerberg 			 * from driver to driver. In Linux we always expect
2728a0a90718SMika Westerberg 			 * 0 .. max - 1 so we need to ask the driver to
2729a0a90718SMika Westerberg 			 * translate between the two schemes.
2730a0a90718SMika Westerberg 			 */
27318caab75fSGeert Uytterhoeven 			if (ctlr->fw_translate_cs) {
27328caab75fSGeert Uytterhoeven 				int cs = ctlr->fw_translate_cs(ctlr,
2733a0a90718SMika Westerberg 						sb->device_selection);
2734a0a90718SMika Westerberg 				if (cs < 0)
2735a0a90718SMika Westerberg 					return cs;
27364c3c5954SArd Biesheuvel 				lookup->chip_select = cs;
2737a0a90718SMika Westerberg 			} else {
27384c3c5954SArd Biesheuvel 				lookup->chip_select = sb->device_selection;
2739a0a90718SMika Westerberg 			}
2740a0a90718SMika Westerberg 
27414c3c5954SArd Biesheuvel 			lookup->max_speed_hz = sb->connection_speed;
27420dadde34SAndy Shevchenko 			lookup->bits_per_word = sb->data_bit_length;
274364bee4d2SMika Westerberg 
274464bee4d2SMika Westerberg 			if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
27454c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CPHA;
274664bee4d2SMika Westerberg 			if (sb->clock_polarity == ACPI_SPI_START_HIGH)
27474c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CPOL;
274864bee4d2SMika Westerberg 			if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
27494c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CS_HIGH;
275064bee4d2SMika Westerberg 		}
27514c3c5954SArd Biesheuvel 	} else if (lookup->irq < 0) {
275264bee4d2SMika Westerberg 		struct resource r;
275364bee4d2SMika Westerberg 
275464bee4d2SMika Westerberg 		if (acpi_dev_resource_interrupt(ares, 0, &r))
27554c3c5954SArd Biesheuvel 			lookup->irq = r.start;
275664bee4d2SMika Westerberg 	}
275764bee4d2SMika Westerberg 
275864bee4d2SMika Westerberg 	/* Always tell the ACPI core to skip this resource */
275964bee4d2SMika Westerberg 	return 1;
276064bee4d2SMika Westerberg }
276164bee4d2SMika Westerberg 
2762000bee0eSStefan Binding /**
2763000bee0eSStefan Binding  * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2764000bee0eSStefan Binding  * @ctlr: controller to which the spi device belongs
2765000bee0eSStefan Binding  * @adev: ACPI Device for the spi device
276687e59b36SStefan Binding  * @index: Index of the spi resource inside the ACPI Node
2767000bee0eSStefan Binding  *
2768702ca026SAndy Shevchenko  * This should be used to allocate a new SPI device from and ACPI Device node.
2769702ca026SAndy Shevchenko  * The caller is responsible for calling spi_add_device to register the SPI device.
2770000bee0eSStefan Binding  *
2771702ca026SAndy Shevchenko  * If ctlr is set to NULL, the Controller for the SPI device will be looked up
277287e59b36SStefan Binding  * using the resource.
277387e59b36SStefan Binding  * If index is set to -1, index is not used.
277487e59b36SStefan Binding  * Note: If index is -1, ctlr must be set.
277587e59b36SStefan Binding  *
2776000bee0eSStefan Binding  * Return: a pointer to the new device, or ERR_PTR on error.
2777000bee0eSStefan Binding  */
2778000bee0eSStefan Binding struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
277987e59b36SStefan Binding 					 struct acpi_device *adev,
278087e59b36SStefan Binding 					 int index)
278164bee4d2SMika Westerberg {
27824c3c5954SArd Biesheuvel 	acpi_handle parent_handle = NULL;
278364bee4d2SMika Westerberg 	struct list_head resource_list;
2784b28944c6SArd Biesheuvel 	struct acpi_spi_lookup lookup = {};
278564bee4d2SMika Westerberg 	struct spi_device *spi;
278664bee4d2SMika Westerberg 	int ret;
27874d8ff6b0SAmit Kumar Mahapatra 	u8 idx;
278864bee4d2SMika Westerberg 
278987e59b36SStefan Binding 	if (!ctlr && index == -1)
279087e59b36SStefan Binding 		return ERR_PTR(-EINVAL);
279187e59b36SStefan Binding 
27924c3c5954SArd Biesheuvel 	lookup.ctlr		= ctlr;
27934c3c5954SArd Biesheuvel 	lookup.irq		= -1;
279487e59b36SStefan Binding 	lookup.index		= index;
279587e59b36SStefan Binding 	lookup.n		= 0;
27964c3c5954SArd Biesheuvel 
27974c3c5954SArd Biesheuvel 	INIT_LIST_HEAD(&resource_list);
27984c3c5954SArd Biesheuvel 	ret = acpi_dev_get_resources(adev, &resource_list,
27994c3c5954SArd Biesheuvel 				     acpi_spi_add_resource, &lookup);
28004c3c5954SArd Biesheuvel 	acpi_dev_free_resource_list(&resource_list);
28014c3c5954SArd Biesheuvel 
28024c3c5954SArd Biesheuvel 	if (ret < 0)
280395c8222fSDavid Jander 		/* Found SPI in _CRS but it points to another controller */
2804b6747f4fSAndy Shevchenko 		return ERR_PTR(ret);
28054c3c5954SArd Biesheuvel 
28064c3c5954SArd Biesheuvel 	if (!lookup.max_speed_hz &&
280710e92724SBjorn Helgaas 	    ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) &&
280887e59b36SStefan Binding 	    ACPI_HANDLE(lookup.ctlr->dev.parent) == parent_handle) {
28094c3c5954SArd Biesheuvel 		/* Apple does not use _CRS but nested devices for SPI slaves */
28104c3c5954SArd Biesheuvel 		acpi_spi_parse_apple_properties(adev, &lookup);
28114c3c5954SArd Biesheuvel 	}
28124c3c5954SArd Biesheuvel 
28134c3c5954SArd Biesheuvel 	if (!lookup.max_speed_hz)
2814000bee0eSStefan Binding 		return ERR_PTR(-ENODEV);
28154c3c5954SArd Biesheuvel 
281687e59b36SStefan Binding 	spi = spi_alloc_device(lookup.ctlr);
281764bee4d2SMika Westerberg 	if (!spi) {
281887e59b36SStefan Binding 		dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n",
281964bee4d2SMika Westerberg 			dev_name(&adev->dev));
2820000bee0eSStefan Binding 		return ERR_PTR(-ENOMEM);
282164bee4d2SMika Westerberg 	}
282264bee4d2SMika Westerberg 
28234d8ff6b0SAmit Kumar Mahapatra 	/*
28244d8ff6b0SAmit Kumar Mahapatra 	 * Zero(0) is a valid physical CS value and can be located at any
28254d8ff6b0SAmit Kumar Mahapatra 	 * logical CS in the spi->chip_select[]. If all the physical CS
28264d8ff6b0SAmit Kumar Mahapatra 	 * are initialized to 0 then It would be difficult to differentiate
28274d8ff6b0SAmit Kumar Mahapatra 	 * between a valid physical CS 0 & an unused logical CS whose physical
28284d8ff6b0SAmit Kumar Mahapatra 	 * CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
28294d8ff6b0SAmit Kumar Mahapatra 	 * Now all the unused logical CS will have 0xFF physical CS value & can be
28304d8ff6b0SAmit Kumar Mahapatra 	 * ignore while performing physical CS validity checks.
28314d8ff6b0SAmit Kumar Mahapatra 	 */
28324d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
28334d8ff6b0SAmit Kumar Mahapatra 		spi_set_chipselect(spi, idx, 0xFF);
28344d8ff6b0SAmit Kumar Mahapatra 
28357b199811SRafael J. Wysocki 	ACPI_COMPANION_SET(&spi->dev, adev);
28364c3c5954SArd Biesheuvel 	spi->max_speed_hz	= lookup.max_speed_hz;
2837ea235786SJohn Garry 	spi->mode		|= lookup.mode;
28384c3c5954SArd Biesheuvel 	spi->irq		= lookup.irq;
28394c3c5954SArd Biesheuvel 	spi->bits_per_word	= lookup.bits_per_word;
2840303feb3cSAmit Kumar Mahapatra 	spi_set_chipselect(spi, 0, lookup.chip_select);
28414d8ff6b0SAmit Kumar Mahapatra 	/*
28424d8ff6b0SAmit Kumar Mahapatra 	 * spi->chip_select[i] gives the corresponding physical CS for logical CS i
28434d8ff6b0SAmit Kumar Mahapatra 	 * logical CS number is represented by setting the ith bit in spi->cs_index_mask
28444d8ff6b0SAmit Kumar Mahapatra 	 * So, for example, if spi->cs_index_mask = 0x01 then logical CS number is 0 and
28454d8ff6b0SAmit Kumar Mahapatra 	 * spi->chip_select[0] will give the physical CS.
28464d8ff6b0SAmit Kumar Mahapatra 	 * By default spi->chip_select[0] will hold the physical CS number so, set
28474d8ff6b0SAmit Kumar Mahapatra 	 * spi->cs_index_mask as 0x01.
28484d8ff6b0SAmit Kumar Mahapatra 	 */
28494d8ff6b0SAmit Kumar Mahapatra 	spi->cs_index_mask	= 0x01;
285064bee4d2SMika Westerberg 
2851000bee0eSStefan Binding 	return spi;
2852000bee0eSStefan Binding }
2853000bee0eSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_device_alloc);
2854000bee0eSStefan Binding 
2855000bee0eSStefan Binding static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
2856000bee0eSStefan Binding 					    struct acpi_device *adev)
2857000bee0eSStefan Binding {
2858000bee0eSStefan Binding 	struct spi_device *spi;
2859000bee0eSStefan Binding 
2860000bee0eSStefan Binding 	if (acpi_bus_get_status(adev) || !adev->status.present ||
2861000bee0eSStefan Binding 	    acpi_device_enumerated(adev))
2862000bee0eSStefan Binding 		return AE_OK;
2863000bee0eSStefan Binding 
286487e59b36SStefan Binding 	spi = acpi_spi_device_alloc(ctlr, adev, -1);
2865000bee0eSStefan Binding 	if (IS_ERR(spi)) {
2866000bee0eSStefan Binding 		if (PTR_ERR(spi) == -ENOMEM)
2867000bee0eSStefan Binding 			return AE_NO_MEMORY;
2868000bee0eSStefan Binding 		else
2869000bee0eSStefan Binding 			return AE_OK;
2870000bee0eSStefan Binding 	}
2871000bee0eSStefan Binding 
28720c6543f6SDan O'Donovan 	acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
28730c6543f6SDan O'Donovan 			  sizeof(spi->modalias));
28740c6543f6SDan O'Donovan 
287533ada67dSChristophe RICARD 	if (spi->irq < 0)
287633ada67dSChristophe RICARD 		spi->irq = acpi_dev_gpio_irq_get(adev, 0);
287733ada67dSChristophe RICARD 
28787f24467fSOctavian Purdila 	acpi_device_set_enumerated(adev);
28797f24467fSOctavian Purdila 
288033cf00e5SMika Westerberg 	adev->power.flags.ignore_parent = true;
288164bee4d2SMika Westerberg 	if (spi_add_device(spi)) {
288233cf00e5SMika Westerberg 		adev->power.flags.ignore_parent = false;
28838caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
288464bee4d2SMika Westerberg 			dev_name(&adev->dev));
288564bee4d2SMika Westerberg 		spi_dev_put(spi);
288664bee4d2SMika Westerberg 	}
288764bee4d2SMika Westerberg 
288864bee4d2SMika Westerberg 	return AE_OK;
288964bee4d2SMika Westerberg }
289064bee4d2SMika Westerberg 
28917f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
28927f24467fSOctavian Purdila 				       void *data, void **return_value)
28937f24467fSOctavian Purdila {
28947030c428SRafael J. Wysocki 	struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
28958caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = data;
28967f24467fSOctavian Purdila 
28977030c428SRafael J. Wysocki 	if (!adev)
28987f24467fSOctavian Purdila 		return AE_OK;
28997f24467fSOctavian Purdila 
29008caab75fSGeert Uytterhoeven 	return acpi_register_spi_device(ctlr, adev);
29017f24467fSOctavian Purdila }
29027f24467fSOctavian Purdila 
29034c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH		32
29044c3c5954SArd Biesheuvel 
29058caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr)
290664bee4d2SMika Westerberg {
290764bee4d2SMika Westerberg 	acpi_status status;
290864bee4d2SMika Westerberg 	acpi_handle handle;
290964bee4d2SMika Westerberg 
29108caab75fSGeert Uytterhoeven 	handle = ACPI_HANDLE(ctlr->dev.parent);
291164bee4d2SMika Westerberg 	if (!handle)
291264bee4d2SMika Westerberg 		return;
291364bee4d2SMika Westerberg 
29144c3c5954SArd Biesheuvel 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
29154c3c5954SArd Biesheuvel 				     SPI_ACPI_ENUMERATE_MAX_DEPTH,
29168caab75fSGeert Uytterhoeven 				     acpi_spi_add_device, NULL, ctlr, NULL);
291764bee4d2SMika Westerberg 	if (ACPI_FAILURE(status))
29188caab75fSGeert Uytterhoeven 		dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
291964bee4d2SMika Westerberg }
292064bee4d2SMika Westerberg #else
29218caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
292264bee4d2SMika Westerberg #endif /* CONFIG_ACPI */
292364bee4d2SMika Westerberg 
29248caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev)
29258ae12a0dSDavid Brownell {
29268caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
29278ae12a0dSDavid Brownell 
29288caab75fSGeert Uytterhoeven 	ctlr = container_of(dev, struct spi_controller, dev);
29298caab75fSGeert Uytterhoeven 	kfree(ctlr);
29308ae12a0dSDavid Brownell }
29318ae12a0dSDavid Brownell 
29328ae12a0dSDavid Brownell static struct class spi_master_class = {
29338ae12a0dSDavid Brownell 	.name		= "spi_master",
29348caab75fSGeert Uytterhoeven 	.dev_release	= spi_controller_release,
2935eca2ebc7SMartin Sperl 	.dev_groups	= spi_master_groups,
29368ae12a0dSDavid Brownell };
29378ae12a0dSDavid Brownell 
29386c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE
29396c364062SGeert Uytterhoeven /**
29406c364062SGeert Uytterhoeven  * spi_slave_abort - abort the ongoing transfer request on an SPI slave
29416c364062SGeert Uytterhoeven  *		     controller
29426c364062SGeert Uytterhoeven  * @spi: device used for the current transfer
29436c364062SGeert Uytterhoeven  */
29446c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi)
29456c364062SGeert Uytterhoeven {
29468caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
29476c364062SGeert Uytterhoeven 
29488caab75fSGeert Uytterhoeven 	if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
29498caab75fSGeert Uytterhoeven 		return ctlr->slave_abort(ctlr);
29506c364062SGeert Uytterhoeven 
29516c364062SGeert Uytterhoeven 	return -ENOTSUPP;
29526c364062SGeert Uytterhoeven }
29536c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort);
29546c364062SGeert Uytterhoeven 
2955b8d3b056SYang Yingliang int spi_target_abort(struct spi_device *spi)
2956b8d3b056SYang Yingliang {
2957b8d3b056SYang Yingliang 	struct spi_controller *ctlr = spi->controller;
2958b8d3b056SYang Yingliang 
2959b8d3b056SYang Yingliang 	if (spi_controller_is_target(ctlr) && ctlr->target_abort)
2960b8d3b056SYang Yingliang 		return ctlr->target_abort(ctlr);
2961b8d3b056SYang Yingliang 
2962b8d3b056SYang Yingliang 	return -ENOTSUPP;
2963b8d3b056SYang Yingliang }
2964b8d3b056SYang Yingliang EXPORT_SYMBOL_GPL(spi_target_abort);
2965b8d3b056SYang Yingliang 
2966cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2967cc8b4659SGeert Uytterhoeven 			  char *buf)
29686c364062SGeert Uytterhoeven {
29698caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
29708caab75fSGeert Uytterhoeven 						   dev);
29716c364062SGeert Uytterhoeven 	struct device *child;
29726c364062SGeert Uytterhoeven 
2973c21b0837SAndy Shevchenko 	child = device_find_any_child(&ctlr->dev);
2974f2daa466SAndy Shevchenko 	return sysfs_emit(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL);
29756c364062SGeert Uytterhoeven }
29766c364062SGeert Uytterhoeven 
2977cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2978cc8b4659SGeert Uytterhoeven 			   const char *buf, size_t count)
29796c364062SGeert Uytterhoeven {
29808caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
29818caab75fSGeert Uytterhoeven 						   dev);
29826c364062SGeert Uytterhoeven 	struct spi_device *spi;
29836c364062SGeert Uytterhoeven 	struct device *child;
29846c364062SGeert Uytterhoeven 	char name[32];
29856c364062SGeert Uytterhoeven 	int rc;
29866c364062SGeert Uytterhoeven 
29876c364062SGeert Uytterhoeven 	rc = sscanf(buf, "%31s", name);
29886c364062SGeert Uytterhoeven 	if (rc != 1 || !name[0])
29896c364062SGeert Uytterhoeven 		return -EINVAL;
29906c364062SGeert Uytterhoeven 
2991c21b0837SAndy Shevchenko 	child = device_find_any_child(&ctlr->dev);
29926c364062SGeert Uytterhoeven 	if (child) {
29936c364062SGeert Uytterhoeven 		/* Remove registered slave */
29946c364062SGeert Uytterhoeven 		device_unregister(child);
29956c364062SGeert Uytterhoeven 		put_device(child);
29966c364062SGeert Uytterhoeven 	}
29976c364062SGeert Uytterhoeven 
29986c364062SGeert Uytterhoeven 	if (strcmp(name, "(null)")) {
29996c364062SGeert Uytterhoeven 		/* Register new slave */
30006c364062SGeert Uytterhoeven 		spi = spi_alloc_device(ctlr);
30016c364062SGeert Uytterhoeven 		if (!spi)
30026c364062SGeert Uytterhoeven 			return -ENOMEM;
30036c364062SGeert Uytterhoeven 
300451e99de5SWolfram Sang 		strscpy(spi->modalias, name, sizeof(spi->modalias));
30056c364062SGeert Uytterhoeven 
30066c364062SGeert Uytterhoeven 		rc = spi_add_device(spi);
30076c364062SGeert Uytterhoeven 		if (rc) {
30086c364062SGeert Uytterhoeven 			spi_dev_put(spi);
30096c364062SGeert Uytterhoeven 			return rc;
30106c364062SGeert Uytterhoeven 		}
30116c364062SGeert Uytterhoeven 	}
30126c364062SGeert Uytterhoeven 
30136c364062SGeert Uytterhoeven 	return count;
30146c364062SGeert Uytterhoeven }
30156c364062SGeert Uytterhoeven 
3016cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave);
30176c364062SGeert Uytterhoeven 
30186c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = {
30196c364062SGeert Uytterhoeven 	&dev_attr_slave.attr,
30206c364062SGeert Uytterhoeven 	NULL,
30216c364062SGeert Uytterhoeven };
30226c364062SGeert Uytterhoeven 
30236c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = {
30246c364062SGeert Uytterhoeven 	.attrs = spi_slave_attrs,
30256c364062SGeert Uytterhoeven };
30266c364062SGeert Uytterhoeven 
30276c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = {
30288caab75fSGeert Uytterhoeven 	&spi_controller_statistics_group,
30296c364062SGeert Uytterhoeven 	&spi_slave_group,
30306c364062SGeert Uytterhoeven 	NULL,
30316c364062SGeert Uytterhoeven };
30326c364062SGeert Uytterhoeven 
30336c364062SGeert Uytterhoeven static struct class spi_slave_class = {
30346c364062SGeert Uytterhoeven 	.name		= "spi_slave",
30358caab75fSGeert Uytterhoeven 	.dev_release	= spi_controller_release,
30366c364062SGeert Uytterhoeven 	.dev_groups	= spi_slave_groups,
30376c364062SGeert Uytterhoeven };
30386c364062SGeert Uytterhoeven #else
30396c364062SGeert Uytterhoeven extern struct class spi_slave_class;	/* dummy */
30406c364062SGeert Uytterhoeven #endif
30418ae12a0dSDavid Brownell 
30428ae12a0dSDavid Brownell /**
30436c364062SGeert Uytterhoeven  * __spi_alloc_controller - allocate an SPI master or slave controller
30448ae12a0dSDavid Brownell  * @dev: the controller, possibly using the platform_bus
304533e34dc6SDavid Brownell  * @size: how much zeroed driver-private data to allocate; the pointer to this
3046229e6af1SLukas Wunner  *	memory is in the driver_data field of the returned device, accessible
3047229e6af1SLukas Wunner  *	with spi_controller_get_devdata(); the memory is cacheline aligned;
3048229e6af1SLukas Wunner  *	drivers granting DMA access to portions of their private data need to
3049229e6af1SLukas Wunner  *	round up @size using ALIGN(size, dma_get_cache_alignment()).
30506c364062SGeert Uytterhoeven  * @slave: flag indicating whether to allocate an SPI master (false) or SPI
30516c364062SGeert Uytterhoeven  *	slave (true) controller
305233e34dc6SDavid Brownell  * Context: can sleep
30538ae12a0dSDavid Brownell  *
30546c364062SGeert Uytterhoeven  * This call is used only by SPI controller drivers, which are the
30558ae12a0dSDavid Brownell  * only ones directly touching chip registers.  It's how they allocate
30568caab75fSGeert Uytterhoeven  * an spi_controller structure, prior to calling spi_register_controller().
30578ae12a0dSDavid Brownell  *
305897d56dc6SJavier Martinez Canillas  * This must be called from context that can sleep.
30598ae12a0dSDavid Brownell  *
30606c364062SGeert Uytterhoeven  * The caller is responsible for assigning the bus number and initializing the
30618caab75fSGeert Uytterhoeven  * controller's methods before calling spi_register_controller(); and (after
30628caab75fSGeert Uytterhoeven  * errors adding the device) calling spi_controller_put() to prevent a memory
30638caab75fSGeert Uytterhoeven  * leak.
306497d56dc6SJavier Martinez Canillas  *
30656c364062SGeert Uytterhoeven  * Return: the SPI controller structure on success, else NULL.
30668ae12a0dSDavid Brownell  */
30678caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev,
30686c364062SGeert Uytterhoeven 					      unsigned int size, bool slave)
30698ae12a0dSDavid Brownell {
30708caab75fSGeert Uytterhoeven 	struct spi_controller	*ctlr;
3071229e6af1SLukas Wunner 	size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
30728ae12a0dSDavid Brownell 
30730c868461SDavid Brownell 	if (!dev)
30740c868461SDavid Brownell 		return NULL;
30750c868461SDavid Brownell 
3076229e6af1SLukas Wunner 	ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
30778caab75fSGeert Uytterhoeven 	if (!ctlr)
30788ae12a0dSDavid Brownell 		return NULL;
30798ae12a0dSDavid Brownell 
30808caab75fSGeert Uytterhoeven 	device_initialize(&ctlr->dev);
308116a8e2fbSUwe Kleine-König 	INIT_LIST_HEAD(&ctlr->queue);
308216a8e2fbSUwe Kleine-König 	spin_lock_init(&ctlr->queue_lock);
308316a8e2fbSUwe Kleine-König 	spin_lock_init(&ctlr->bus_lock_spinlock);
308416a8e2fbSUwe Kleine-König 	mutex_init(&ctlr->bus_lock_mutex);
308516a8e2fbSUwe Kleine-König 	mutex_init(&ctlr->io_mutex);
308616a8e2fbSUwe Kleine-König 	mutex_init(&ctlr->add_lock);
30878caab75fSGeert Uytterhoeven 	ctlr->bus_num = -1;
30888caab75fSGeert Uytterhoeven 	ctlr->num_chipselect = 1;
30898caab75fSGeert Uytterhoeven 	ctlr->slave = slave;
30906c364062SGeert Uytterhoeven 	if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
30918caab75fSGeert Uytterhoeven 		ctlr->dev.class = &spi_slave_class;
30926c364062SGeert Uytterhoeven 	else
30938caab75fSGeert Uytterhoeven 		ctlr->dev.class = &spi_master_class;
30948caab75fSGeert Uytterhoeven 	ctlr->dev.parent = dev;
30958caab75fSGeert Uytterhoeven 	pm_suspend_ignore_children(&ctlr->dev, true);
3096229e6af1SLukas Wunner 	spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
30978ae12a0dSDavid Brownell 
30988caab75fSGeert Uytterhoeven 	return ctlr;
30998ae12a0dSDavid Brownell }
31006c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller);
31018ae12a0dSDavid Brownell 
31025e844cc3SLukas Wunner static void devm_spi_release_controller(struct device *dev, void *ctlr)
31035e844cc3SLukas Wunner {
31045e844cc3SLukas Wunner 	spi_controller_put(*(struct spi_controller **)ctlr);
31055e844cc3SLukas Wunner }
31065e844cc3SLukas Wunner 
31075e844cc3SLukas Wunner /**
31085e844cc3SLukas Wunner  * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
31095e844cc3SLukas Wunner  * @dev: physical device of SPI controller
31105e844cc3SLukas Wunner  * @size: how much zeroed driver-private data to allocate
31115e844cc3SLukas Wunner  * @slave: whether to allocate an SPI master (false) or SPI slave (true)
31125e844cc3SLukas Wunner  * Context: can sleep
31135e844cc3SLukas Wunner  *
31145e844cc3SLukas Wunner  * Allocate an SPI controller and automatically release a reference on it
31155e844cc3SLukas Wunner  * when @dev is unbound from its driver.  Drivers are thus relieved from
31165e844cc3SLukas Wunner  * having to call spi_controller_put().
31175e844cc3SLukas Wunner  *
31185e844cc3SLukas Wunner  * The arguments to this function are identical to __spi_alloc_controller().
31195e844cc3SLukas Wunner  *
31205e844cc3SLukas Wunner  * Return: the SPI controller structure on success, else NULL.
31215e844cc3SLukas Wunner  */
31225e844cc3SLukas Wunner struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
31235e844cc3SLukas Wunner 						   unsigned int size,
31245e844cc3SLukas Wunner 						   bool slave)
31255e844cc3SLukas Wunner {
31265e844cc3SLukas Wunner 	struct spi_controller **ptr, *ctlr;
31275e844cc3SLukas Wunner 
31285e844cc3SLukas Wunner 	ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr),
31295e844cc3SLukas Wunner 			   GFP_KERNEL);
31305e844cc3SLukas Wunner 	if (!ptr)
31315e844cc3SLukas Wunner 		return NULL;
31325e844cc3SLukas Wunner 
31335e844cc3SLukas Wunner 	ctlr = __spi_alloc_controller(dev, size, slave);
31345e844cc3SLukas Wunner 	if (ctlr) {
3135794aaf01SWilliam A. Kennington III 		ctlr->devm_allocated = true;
31365e844cc3SLukas Wunner 		*ptr = ctlr;
31375e844cc3SLukas Wunner 		devres_add(dev, ptr);
31385e844cc3SLukas Wunner 	} else {
31395e844cc3SLukas Wunner 		devres_free(ptr);
31405e844cc3SLukas Wunner 	}
31415e844cc3SLukas Wunner 
31425e844cc3SLukas Wunner 	return ctlr;
31435e844cc3SLukas Wunner }
31445e844cc3SLukas Wunner EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
31455e844cc3SLukas Wunner 
3146f3186dd8SLinus Walleij /**
3147f3186dd8SLinus Walleij  * spi_get_gpio_descs() - grab chip select GPIOs for the master
3148f3186dd8SLinus Walleij  * @ctlr: The SPI master to grab GPIO descriptors for
3149f3186dd8SLinus Walleij  */
3150f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr)
3151f3186dd8SLinus Walleij {
3152f3186dd8SLinus Walleij 	int nb, i;
3153f3186dd8SLinus Walleij 	struct gpio_desc **cs;
3154f3186dd8SLinus Walleij 	struct device *dev = &ctlr->dev;
31557d93aecdSGeert Uytterhoeven 	unsigned long native_cs_mask = 0;
31567d93aecdSGeert Uytterhoeven 	unsigned int num_cs_gpios = 0;
3157f3186dd8SLinus Walleij 
3158f3186dd8SLinus Walleij 	nb = gpiod_count(dev, "cs");
315931ed8ebcSAndy Shevchenko 	if (nb < 0) {
3160f3186dd8SLinus Walleij 		/* No GPIOs at all is fine, else return the error */
316131ed8ebcSAndy Shevchenko 		if (nb == -ENOENT)
3162f3186dd8SLinus Walleij 			return 0;
3163f3186dd8SLinus Walleij 		return nb;
316431ed8ebcSAndy Shevchenko 	}
316531ed8ebcSAndy Shevchenko 
316631ed8ebcSAndy Shevchenko 	ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
3167f3186dd8SLinus Walleij 
3168f3186dd8SLinus Walleij 	cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
3169f3186dd8SLinus Walleij 			  GFP_KERNEL);
3170f3186dd8SLinus Walleij 	if (!cs)
3171f3186dd8SLinus Walleij 		return -ENOMEM;
3172f3186dd8SLinus Walleij 	ctlr->cs_gpiods = cs;
3173f3186dd8SLinus Walleij 
3174f3186dd8SLinus Walleij 	for (i = 0; i < nb; i++) {
3175f3186dd8SLinus Walleij 		/*
3176f3186dd8SLinus Walleij 		 * Most chipselects are active low, the inverted
3177f3186dd8SLinus Walleij 		 * semantics are handled by special quirks in gpiolib,
3178f3186dd8SLinus Walleij 		 * so initializing them GPIOD_OUT_LOW here means
3179f3186dd8SLinus Walleij 		 * "unasserted", in most cases this will drive the physical
3180f3186dd8SLinus Walleij 		 * line high.
3181f3186dd8SLinus Walleij 		 */
3182f3186dd8SLinus Walleij 		cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
3183f3186dd8SLinus Walleij 						      GPIOD_OUT_LOW);
31841723fdecSGeert Uytterhoeven 		if (IS_ERR(cs[i]))
31851723fdecSGeert Uytterhoeven 			return PTR_ERR(cs[i]);
3186f3186dd8SLinus Walleij 
3187f3186dd8SLinus Walleij 		if (cs[i]) {
3188f3186dd8SLinus Walleij 			/*
3189f3186dd8SLinus Walleij 			 * If we find a CS GPIO, name it after the device and
3190f3186dd8SLinus Walleij 			 * chip select line.
3191f3186dd8SLinus Walleij 			 */
3192f3186dd8SLinus Walleij 			char *gpioname;
3193f3186dd8SLinus Walleij 
3194f3186dd8SLinus Walleij 			gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
3195f3186dd8SLinus Walleij 						  dev_name(dev), i);
3196f3186dd8SLinus Walleij 			if (!gpioname)
3197f3186dd8SLinus Walleij 				return -ENOMEM;
3198f3186dd8SLinus Walleij 			gpiod_set_consumer_name(cs[i], gpioname);
31997d93aecdSGeert Uytterhoeven 			num_cs_gpios++;
32007d93aecdSGeert Uytterhoeven 			continue;
3201f3186dd8SLinus Walleij 		}
32027d93aecdSGeert Uytterhoeven 
32037d93aecdSGeert Uytterhoeven 		if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
32047d93aecdSGeert Uytterhoeven 			dev_err(dev, "Invalid native chip select %d\n", i);
32057d93aecdSGeert Uytterhoeven 			return -EINVAL;
32067d93aecdSGeert Uytterhoeven 		}
32077d93aecdSGeert Uytterhoeven 		native_cs_mask |= BIT(i);
32087d93aecdSGeert Uytterhoeven 	}
32097d93aecdSGeert Uytterhoeven 
3210f60d7270SAndy Shevchenko 	ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
3211dbaca8e5SAndy Shevchenko 
321282238d2cSAndy Shevchenko 	if ((ctlr->flags & SPI_CONTROLLER_GPIO_SS) && num_cs_gpios &&
3213dbaca8e5SAndy Shevchenko 	    ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
32147d93aecdSGeert Uytterhoeven 		dev_err(dev, "No unused native chip select available\n");
32157d93aecdSGeert Uytterhoeven 		return -EINVAL;
3216f3186dd8SLinus Walleij 	}
3217f3186dd8SLinus Walleij 
3218f3186dd8SLinus Walleij 	return 0;
3219f3186dd8SLinus Walleij }
3220f3186dd8SLinus Walleij 
3221bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr)
3222bdf3a3b5SBoris Brezillon {
3223bdf3a3b5SBoris Brezillon 	/*
3224b5932f5cSBoris Brezillon 	 * The controller may implement only the high-level SPI-memory like
3225b5932f5cSBoris Brezillon 	 * operations if it does not support regular SPI transfers, and this is
3226b5932f5cSBoris Brezillon 	 * valid use case.
322776a85704SWilliam Zhang 	 * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least
322876a85704SWilliam Zhang 	 * one of the ->transfer_xxx() method be implemented.
3229bdf3a3b5SBoris Brezillon 	 */
323020064c47SWilliam Zhang 	if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
323176a85704SWilliam Zhang 		if (!ctlr->transfer && !ctlr->transfer_one &&
3232b5932f5cSBoris Brezillon 		   !ctlr->transfer_one_message) {
3233b5932f5cSBoris Brezillon 			return -EINVAL;
3234b5932f5cSBoris Brezillon 		}
323576a85704SWilliam Zhang 	}
3236bdf3a3b5SBoris Brezillon 
3237bdf3a3b5SBoris Brezillon 	return 0;
3238bdf3a3b5SBoris Brezillon }
3239bdf3a3b5SBoris Brezillon 
3240440c4733SAndy Shevchenko /* Allocate dynamic bus number using Linux idr */
3241440c4733SAndy Shevchenko static int spi_controller_id_alloc(struct spi_controller *ctlr, int start, int end)
3242440c4733SAndy Shevchenko {
3243440c4733SAndy Shevchenko 	int id;
3244440c4733SAndy Shevchenko 
3245440c4733SAndy Shevchenko 	mutex_lock(&board_lock);
3246440c4733SAndy Shevchenko 	id = idr_alloc(&spi_master_idr, ctlr, start, end, GFP_KERNEL);
3247440c4733SAndy Shevchenko 	mutex_unlock(&board_lock);
3248440c4733SAndy Shevchenko 	if (WARN(id < 0, "couldn't get idr"))
3249440c4733SAndy Shevchenko 		return id == -ENOSPC ? -EBUSY : id;
3250440c4733SAndy Shevchenko 	ctlr->bus_num = id;
3251440c4733SAndy Shevchenko 	return 0;
3252440c4733SAndy Shevchenko }
3253440c4733SAndy Shevchenko 
32548ae12a0dSDavid Brownell /**
32558caab75fSGeert Uytterhoeven  * spi_register_controller - register SPI master or slave controller
32568caab75fSGeert Uytterhoeven  * @ctlr: initialized master, originally from spi_alloc_master() or
32578caab75fSGeert Uytterhoeven  *	spi_alloc_slave()
325833e34dc6SDavid Brownell  * Context: can sleep
32598ae12a0dSDavid Brownell  *
32608caab75fSGeert Uytterhoeven  * SPI controllers connect to their drivers using some non-SPI bus,
32618ae12a0dSDavid Brownell  * such as the platform bus.  The final stage of probe() in that code
32628caab75fSGeert Uytterhoeven  * includes calling spi_register_controller() to hook up to this SPI bus glue.
32638ae12a0dSDavid Brownell  *
32648ae12a0dSDavid Brownell  * SPI controllers use board specific (often SOC specific) bus numbers,
32658ae12a0dSDavid Brownell  * and board-specific addressing for SPI devices combines those numbers
32668ae12a0dSDavid Brownell  * with chip select numbers.  Since SPI does not directly support dynamic
32678ae12a0dSDavid Brownell  * device identification, boards need configuration tables telling which
32688ae12a0dSDavid Brownell  * chip is at which address.
32698ae12a0dSDavid Brownell  *
32708ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns zero on
32718caab75fSGeert Uytterhoeven  * success, else a negative error code (dropping the controller's refcount).
32720c868461SDavid Brownell  * After a successful return, the caller is responsible for calling
32738caab75fSGeert Uytterhoeven  * spi_unregister_controller().
327497d56dc6SJavier Martinez Canillas  *
327597d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
32768ae12a0dSDavid Brownell  */
32778caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr)
32788ae12a0dSDavid Brownell {
32798caab75fSGeert Uytterhoeven 	struct device		*dev = ctlr->dev.parent;
32802b9603a0SFeng Tang 	struct boardinfo	*bi;
3281440c4733SAndy Shevchenko 	int			first_dynamic;
3282b93318a2SSergei Shtylyov 	int			status;
32834d8ff6b0SAmit Kumar Mahapatra 	int			idx;
32848ae12a0dSDavid Brownell 
32850c868461SDavid Brownell 	if (!dev)
32860c868461SDavid Brownell 		return -ENODEV;
32870c868461SDavid Brownell 
3288bdf3a3b5SBoris Brezillon 	/*
3289bdf3a3b5SBoris Brezillon 	 * Make sure all necessary hooks are implemented before registering
3290bdf3a3b5SBoris Brezillon 	 * the SPI controller.
3291bdf3a3b5SBoris Brezillon 	 */
3292bdf3a3b5SBoris Brezillon 	status = spi_controller_check_ops(ctlr);
3293bdf3a3b5SBoris Brezillon 	if (status)
3294bdf3a3b5SBoris Brezillon 		return status;
3295bdf3a3b5SBoris Brezillon 
3296440c4733SAndy Shevchenko 	if (ctlr->bus_num < 0)
3297440c4733SAndy Shevchenko 		ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi");
329804b2d03aSGeert Uytterhoeven 	if (ctlr->bus_num >= 0) {
329995c8222fSDavid Jander 		/* Devices with a fixed bus num must check-in with the num */
3300440c4733SAndy Shevchenko 		status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1);
3301440c4733SAndy Shevchenko 		if (status)
3302440c4733SAndy Shevchenko 			return status;
33039b61e302SSuniel Mahesh 	}
33048caab75fSGeert Uytterhoeven 	if (ctlr->bus_num < 0) {
330542bdd706SLucas Stach 		first_dynamic = of_alias_get_highest_id("spi");
330642bdd706SLucas Stach 		if (first_dynamic < 0)
330742bdd706SLucas Stach 			first_dynamic = 0;
330842bdd706SLucas Stach 		else
330942bdd706SLucas Stach 			first_dynamic++;
331042bdd706SLucas Stach 
3311440c4733SAndy Shevchenko 		status = spi_controller_id_alloc(ctlr, first_dynamic, 0);
3312440c4733SAndy Shevchenko 		if (status)
3313440c4733SAndy Shevchenko 			return status;
33148ae12a0dSDavid Brownell 	}
33158caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 0;
33168caab75fSGeert Uytterhoeven 	init_completion(&ctlr->xfer_completion);
331769fa9590SDavid Jander 	init_completion(&ctlr->cur_msg_completion);
33188caab75fSGeert Uytterhoeven 	if (!ctlr->max_dma_len)
33198caab75fSGeert Uytterhoeven 		ctlr->max_dma_len = INT_MAX;
3320cf32b71eSErnst Schwab 
3321350de7ceSAndy Shevchenko 	/*
3322350de7ceSAndy Shevchenko 	 * Register the device, then userspace will see it.
3323350de7ceSAndy Shevchenko 	 * Registration fails if the bus ID is in use.
33248ae12a0dSDavid Brownell 	 */
33258caab75fSGeert Uytterhoeven 	dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
33260a919ae4SAndrey Smirnov 
3327f48dc6b9SLinus Walleij 	if (!spi_controller_is_slave(ctlr) && ctlr->use_gpio_descriptors) {
33280a919ae4SAndrey Smirnov 		status = spi_get_gpio_descs(ctlr);
33290a919ae4SAndrey Smirnov 		if (status)
3330f9981d4fSAaro Koskinen 			goto free_bus_id;
33310a919ae4SAndrey Smirnov 		/*
33320a919ae4SAndrey Smirnov 		 * A controller using GPIO descriptors always
33330a919ae4SAndrey Smirnov 		 * supports SPI_CS_HIGH if need be.
33340a919ae4SAndrey Smirnov 		 */
33350a919ae4SAndrey Smirnov 		ctlr->mode_bits |= SPI_CS_HIGH;
33360a919ae4SAndrey Smirnov 	}
33370a919ae4SAndrey Smirnov 
3338f9481b08STudor Ambarus 	/*
3339f9481b08STudor Ambarus 	 * Even if it's just one always-selected device, there must
3340f9481b08STudor Ambarus 	 * be at least one chipselect.
3341f9481b08STudor Ambarus 	 */
3342f9981d4fSAaro Koskinen 	if (!ctlr->num_chipselect) {
3343f9981d4fSAaro Koskinen 		status = -EINVAL;
3344f9981d4fSAaro Koskinen 		goto free_bus_id;
3345f9981d4fSAaro Koskinen 	}
3346f9481b08STudor Ambarus 
334795c8222fSDavid Jander 	/* Setting last_cs to -1 means no chip selected */
33484d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
33494d8ff6b0SAmit Kumar Mahapatra 		ctlr->last_cs[idx] = -1;
33506bb477dfSYun Zhou 
33518caab75fSGeert Uytterhoeven 	status = device_add(&ctlr->dev);
3352f9981d4fSAaro Koskinen 	if (status < 0)
3353f9981d4fSAaro Koskinen 		goto free_bus_id;
33549b61e302SSuniel Mahesh 	dev_dbg(dev, "registered %s %s\n",
33558caab75fSGeert Uytterhoeven 			spi_controller_is_slave(ctlr) ? "slave" : "master",
33569b61e302SSuniel Mahesh 			dev_name(&ctlr->dev));
33578ae12a0dSDavid Brownell 
3358b5932f5cSBoris Brezillon 	/*
3359b5932f5cSBoris Brezillon 	 * If we're using a queued driver, start the queue. Note that we don't
3360b5932f5cSBoris Brezillon 	 * need the queueing logic if the driver is only supporting high-level
3361b5932f5cSBoris Brezillon 	 * memory operations.
3362b5932f5cSBoris Brezillon 	 */
3363b5932f5cSBoris Brezillon 	if (ctlr->transfer) {
33648caab75fSGeert Uytterhoeven 		dev_info(dev, "controller is unqueued, this is deprecated\n");
3365b5932f5cSBoris Brezillon 	} else if (ctlr->transfer_one || ctlr->transfer_one_message) {
33668caab75fSGeert Uytterhoeven 		status = spi_controller_initialize_queue(ctlr);
3367ffbbdd21SLinus Walleij 		if (status) {
33688caab75fSGeert Uytterhoeven 			device_del(&ctlr->dev);
3369f9981d4fSAaro Koskinen 			goto free_bus_id;
3370ffbbdd21SLinus Walleij 		}
3371ffbbdd21SLinus Walleij 	}
337295c8222fSDavid Jander 	/* Add statistics */
33736598b91bSDavid Jander 	ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev);
33746598b91bSDavid Jander 	if (!ctlr->pcpu_statistics) {
33756598b91bSDavid Jander 		dev_err(dev, "Error allocating per-cpu statistics\n");
3376d52b095bSDan Carpenter 		status = -ENOMEM;
33776598b91bSDavid Jander 		goto destroy_queue;
33786598b91bSDavid Jander 	}
3379ffbbdd21SLinus Walleij 
33802b9603a0SFeng Tang 	mutex_lock(&board_lock);
33818caab75fSGeert Uytterhoeven 	list_add_tail(&ctlr->list, &spi_controller_list);
33822b9603a0SFeng Tang 	list_for_each_entry(bi, &board_list, list)
33838caab75fSGeert Uytterhoeven 		spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
33842b9603a0SFeng Tang 	mutex_unlock(&board_lock);
33852b9603a0SFeng Tang 
338664bee4d2SMika Westerberg 	/* Register devices from the device tree and ACPI */
33878caab75fSGeert Uytterhoeven 	of_register_spi_devices(ctlr);
33888caab75fSGeert Uytterhoeven 	acpi_register_spi_devices(ctlr);
3389f9981d4fSAaro Koskinen 	return status;
3390f9981d4fSAaro Koskinen 
33916598b91bSDavid Jander destroy_queue:
33926598b91bSDavid Jander 	spi_destroy_queue(ctlr);
3393f9981d4fSAaro Koskinen free_bus_id:
3394f9981d4fSAaro Koskinen 	mutex_lock(&board_lock);
3395f9981d4fSAaro Koskinen 	idr_remove(&spi_master_idr, ctlr->bus_num);
3396f9981d4fSAaro Koskinen 	mutex_unlock(&board_lock);
33978ae12a0dSDavid Brownell 	return status;
33988ae12a0dSDavid Brownell }
33998caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller);
34008ae12a0dSDavid Brownell 
340143cc5a0aSYang Yingliang static void devm_spi_unregister(struct device *dev, void *res)
3402666d5b4cSMark Brown {
340343cc5a0aSYang Yingliang 	spi_unregister_controller(*(struct spi_controller **)res);
3404666d5b4cSMark Brown }
3405666d5b4cSMark Brown 
3406666d5b4cSMark Brown /**
34078caab75fSGeert Uytterhoeven  * devm_spi_register_controller - register managed SPI master or slave
34088caab75fSGeert Uytterhoeven  *	controller
34098caab75fSGeert Uytterhoeven  * @dev:    device managing SPI controller
34108caab75fSGeert Uytterhoeven  * @ctlr: initialized controller, originally from spi_alloc_master() or
34118caab75fSGeert Uytterhoeven  *	spi_alloc_slave()
3412666d5b4cSMark Brown  * Context: can sleep
3413666d5b4cSMark Brown  *
34148caab75fSGeert Uytterhoeven  * Register a SPI device as with spi_register_controller() which will
341568b892f1SJohan Hovold  * automatically be unregistered and freed.
341697d56dc6SJavier Martinez Canillas  *
341797d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
3418666d5b4cSMark Brown  */
34198caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev,
34208caab75fSGeert Uytterhoeven 				 struct spi_controller *ctlr)
3421666d5b4cSMark Brown {
342243cc5a0aSYang Yingliang 	struct spi_controller **ptr;
3423666d5b4cSMark Brown 	int ret;
3424666d5b4cSMark Brown 
342543cc5a0aSYang Yingliang 	ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
342643cc5a0aSYang Yingliang 	if (!ptr)
342743cc5a0aSYang Yingliang 		return -ENOMEM;
342859ebbe40STian Tao 
342943cc5a0aSYang Yingliang 	ret = spi_register_controller(ctlr);
343043cc5a0aSYang Yingliang 	if (!ret) {
343143cc5a0aSYang Yingliang 		*ptr = ctlr;
343243cc5a0aSYang Yingliang 		devres_add(dev, ptr);
343343cc5a0aSYang Yingliang 	} else {
343443cc5a0aSYang Yingliang 		devres_free(ptr);
343543cc5a0aSYang Yingliang 	}
343643cc5a0aSYang Yingliang 
343743cc5a0aSYang Yingliang 	return ret;
3438666d5b4cSMark Brown }
34398caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller);
3440666d5b4cSMark Brown 
344134860089SDavid Lamparter static int __unregister(struct device *dev, void *null)
34428ae12a0dSDavid Brownell {
34430c868461SDavid Brownell 	spi_unregister_device(to_spi_device(dev));
34448ae12a0dSDavid Brownell 	return 0;
34458ae12a0dSDavid Brownell }
34468ae12a0dSDavid Brownell 
34478ae12a0dSDavid Brownell /**
34488caab75fSGeert Uytterhoeven  * spi_unregister_controller - unregister SPI master or slave controller
34498caab75fSGeert Uytterhoeven  * @ctlr: the controller being unregistered
345033e34dc6SDavid Brownell  * Context: can sleep
34518ae12a0dSDavid Brownell  *
34528caab75fSGeert Uytterhoeven  * This call is used only by SPI controller drivers, which are the
34538ae12a0dSDavid Brownell  * only ones directly touching chip registers.
34548ae12a0dSDavid Brownell  *
34558ae12a0dSDavid Brownell  * This must be called from context that can sleep.
345668b892f1SJohan Hovold  *
345768b892f1SJohan Hovold  * Note that this function also drops a reference to the controller.
34588ae12a0dSDavid Brownell  */
34598caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr)
34608ae12a0dSDavid Brownell {
34619b61e302SSuniel Mahesh 	struct spi_controller *found;
346267f7b278SJohan Hovold 	int id = ctlr->bus_num;
346389fc9a1aSJeff Garzik 
3464ddf75be4SLukas Wunner 	/* Prevent addition of new devices, unregister existing ones */
3465ddf75be4SLukas Wunner 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
34666098475dSMark Brown 		mutex_lock(&ctlr->add_lock);
3467ddf75be4SLukas Wunner 
346884855678SLukas Wunner 	device_for_each_child(&ctlr->dev, NULL, __unregister);
346984855678SLukas Wunner 
34709b61e302SSuniel Mahesh 	/* First make sure that this controller was ever added */
34719b61e302SSuniel Mahesh 	mutex_lock(&board_lock);
347267f7b278SJohan Hovold 	found = idr_find(&spi_master_idr, id);
34739b61e302SSuniel Mahesh 	mutex_unlock(&board_lock);
34748caab75fSGeert Uytterhoeven 	if (ctlr->queued) {
34758caab75fSGeert Uytterhoeven 		if (spi_destroy_queue(ctlr))
34768caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "queue remove failed\n");
3477ffbbdd21SLinus Walleij 	}
34782b9603a0SFeng Tang 	mutex_lock(&board_lock);
34798caab75fSGeert Uytterhoeven 	list_del(&ctlr->list);
34802b9603a0SFeng Tang 	mutex_unlock(&board_lock);
34812b9603a0SFeng Tang 
34825e844cc3SLukas Wunner 	device_del(&ctlr->dev);
34835e844cc3SLukas Wunner 
348495c8222fSDavid Jander 	/* Free bus id */
34859b61e302SSuniel Mahesh 	mutex_lock(&board_lock);
3486613bd1eaSJarkko Nikula 	if (found == ctlr)
348767f7b278SJohan Hovold 		idr_remove(&spi_master_idr, id);
34889b61e302SSuniel Mahesh 	mutex_unlock(&board_lock);
3489ddf75be4SLukas Wunner 
3490ddf75be4SLukas Wunner 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
34916098475dSMark Brown 		mutex_unlock(&ctlr->add_lock);
34926c53b45cSMichael Walle 
3493702ca026SAndy Shevchenko 	/*
3494702ca026SAndy Shevchenko 	 * Release the last reference on the controller if its driver
34956c53b45cSMichael Walle 	 * has not yet been converted to devm_spi_alloc_master/slave().
34966c53b45cSMichael Walle 	 */
34976c53b45cSMichael Walle 	if (!ctlr->devm_allocated)
34986c53b45cSMichael Walle 		put_device(&ctlr->dev);
34998ae12a0dSDavid Brownell }
35008caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller);
35018ae12a0dSDavid Brownell 
3502bef4a48fSMark Hasemeyer static inline int __spi_check_suspended(const struct spi_controller *ctlr)
3503bef4a48fSMark Hasemeyer {
3504bef4a48fSMark Hasemeyer 	return ctlr->flags & SPI_CONTROLLER_SUSPENDED ? -ESHUTDOWN : 0;
3505bef4a48fSMark Hasemeyer }
3506bef4a48fSMark Hasemeyer 
3507bef4a48fSMark Hasemeyer static inline void __spi_mark_suspended(struct spi_controller *ctlr)
3508bef4a48fSMark Hasemeyer {
3509bef4a48fSMark Hasemeyer 	mutex_lock(&ctlr->bus_lock_mutex);
3510bef4a48fSMark Hasemeyer 	ctlr->flags |= SPI_CONTROLLER_SUSPENDED;
3511bef4a48fSMark Hasemeyer 	mutex_unlock(&ctlr->bus_lock_mutex);
3512bef4a48fSMark Hasemeyer }
3513bef4a48fSMark Hasemeyer 
3514bef4a48fSMark Hasemeyer static inline void __spi_mark_resumed(struct spi_controller *ctlr)
3515bef4a48fSMark Hasemeyer {
3516bef4a48fSMark Hasemeyer 	mutex_lock(&ctlr->bus_lock_mutex);
3517bef4a48fSMark Hasemeyer 	ctlr->flags &= ~SPI_CONTROLLER_SUSPENDED;
3518bef4a48fSMark Hasemeyer 	mutex_unlock(&ctlr->bus_lock_mutex);
3519bef4a48fSMark Hasemeyer }
3520bef4a48fSMark Hasemeyer 
35218caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr)
3522ffbbdd21SLinus Walleij {
3523bef4a48fSMark Hasemeyer 	int ret = 0;
3524ffbbdd21SLinus Walleij 
35258caab75fSGeert Uytterhoeven 	/* Basically no-ops for non-queued controllers */
3526bef4a48fSMark Hasemeyer 	if (ctlr->queued) {
35278caab75fSGeert Uytterhoeven 		ret = spi_stop_queue(ctlr);
3528ffbbdd21SLinus Walleij 		if (ret)
35298caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "queue stop failed\n");
3530bef4a48fSMark Hasemeyer 	}
3531ffbbdd21SLinus Walleij 
3532bef4a48fSMark Hasemeyer 	__spi_mark_suspended(ctlr);
3533ffbbdd21SLinus Walleij 	return ret;
3534ffbbdd21SLinus Walleij }
35358caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend);
3536ffbbdd21SLinus Walleij 
35378caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr)
3538ffbbdd21SLinus Walleij {
3539bef4a48fSMark Hasemeyer 	int ret = 0;
3540ffbbdd21SLinus Walleij 
3541bef4a48fSMark Hasemeyer 	__spi_mark_resumed(ctlr);
3542ffbbdd21SLinus Walleij 
3543bef4a48fSMark Hasemeyer 	if (ctlr->queued) {
35448caab75fSGeert Uytterhoeven 		ret = spi_start_queue(ctlr);
3545ffbbdd21SLinus Walleij 		if (ret)
35468caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "queue restart failed\n");
3547bef4a48fSMark Hasemeyer 	}
3548ffbbdd21SLinus Walleij 	return ret;
3549ffbbdd21SLinus Walleij }
35508caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume);
3551ffbbdd21SLinus Walleij 
35528ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
35538ae12a0dSDavid Brownell 
3554523baf5aSMartin Sperl /* Core methods for spi_message alterations */
3555523baf5aSMartin Sperl 
35568caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr,
3557523baf5aSMartin Sperl 					    struct spi_message *msg,
3558523baf5aSMartin Sperl 					    void *res)
3559523baf5aSMartin Sperl {
3560523baf5aSMartin Sperl 	struct spi_replaced_transfers *rxfer = res;
3561523baf5aSMartin Sperl 	size_t i;
3562523baf5aSMartin Sperl 
356395c8222fSDavid Jander 	/* Call extra callback if requested */
3564523baf5aSMartin Sperl 	if (rxfer->release)
35658caab75fSGeert Uytterhoeven 		rxfer->release(ctlr, msg, res);
3566523baf5aSMartin Sperl 
356795c8222fSDavid Jander 	/* Insert replaced transfers back into the message */
3568523baf5aSMartin Sperl 	list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
3569523baf5aSMartin Sperl 
357095c8222fSDavid Jander 	/* Remove the formerly inserted entries */
3571523baf5aSMartin Sperl 	for (i = 0; i < rxfer->inserted; i++)
3572523baf5aSMartin Sperl 		list_del(&rxfer->inserted_transfers[i].transfer_list);
3573523baf5aSMartin Sperl }
3574523baf5aSMartin Sperl 
3575523baf5aSMartin Sperl /**
3576523baf5aSMartin Sperl  * spi_replace_transfers - replace transfers with several transfers
3577523baf5aSMartin Sperl  *                         and register change with spi_message.resources
3578523baf5aSMartin Sperl  * @msg:           the spi_message we work upon
3579523baf5aSMartin Sperl  * @xfer_first:    the first spi_transfer we want to replace
3580523baf5aSMartin Sperl  * @remove:        number of transfers to remove
3581523baf5aSMartin Sperl  * @insert:        the number of transfers we want to insert instead
3582523baf5aSMartin Sperl  * @release:       extra release code necessary in some circumstances
3583523baf5aSMartin Sperl  * @extradatasize: extra data to allocate (with alignment guarantees
3584523baf5aSMartin Sperl  *                 of struct @spi_transfer)
358505885397SMartin Sperl  * @gfp:           gfp flags
3586523baf5aSMartin Sperl  *
3587523baf5aSMartin Sperl  * Returns: pointer to @spi_replaced_transfers,
3588523baf5aSMartin Sperl  *          PTR_ERR(...) in case of errors.
3589523baf5aSMartin Sperl  */
3590da21fde0SUwe Kleine-König static struct spi_replaced_transfers *spi_replace_transfers(
3591523baf5aSMartin Sperl 	struct spi_message *msg,
3592523baf5aSMartin Sperl 	struct spi_transfer *xfer_first,
3593523baf5aSMartin Sperl 	size_t remove,
3594523baf5aSMartin Sperl 	size_t insert,
3595523baf5aSMartin Sperl 	spi_replaced_release_t release,
3596523baf5aSMartin Sperl 	size_t extradatasize,
3597523baf5aSMartin Sperl 	gfp_t gfp)
3598523baf5aSMartin Sperl {
3599523baf5aSMartin Sperl 	struct spi_replaced_transfers *rxfer;
3600523baf5aSMartin Sperl 	struct spi_transfer *xfer;
3601523baf5aSMartin Sperl 	size_t i;
3602523baf5aSMartin Sperl 
360395c8222fSDavid Jander 	/* Allocate the structure using spi_res */
3604523baf5aSMartin Sperl 	rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
3605aef97522SGustavo A. R. Silva 			      struct_size(rxfer, inserted_transfers, insert)
3606523baf5aSMartin Sperl 			      + extradatasize,
3607523baf5aSMartin Sperl 			      gfp);
3608523baf5aSMartin Sperl 	if (!rxfer)
3609523baf5aSMartin Sperl 		return ERR_PTR(-ENOMEM);
3610523baf5aSMartin Sperl 
361195c8222fSDavid Jander 	/* The release code to invoke before running the generic release */
3612523baf5aSMartin Sperl 	rxfer->release = release;
3613523baf5aSMartin Sperl 
361495c8222fSDavid Jander 	/* Assign extradata */
3615523baf5aSMartin Sperl 	if (extradatasize)
3616523baf5aSMartin Sperl 		rxfer->extradata =
3617523baf5aSMartin Sperl 			&rxfer->inserted_transfers[insert];
3618523baf5aSMartin Sperl 
361995c8222fSDavid Jander 	/* Init the replaced_transfers list */
3620523baf5aSMartin Sperl 	INIT_LIST_HEAD(&rxfer->replaced_transfers);
3621523baf5aSMartin Sperl 
3622350de7ceSAndy Shevchenko 	/*
3623350de7ceSAndy Shevchenko 	 * Assign the list_entry after which we should reinsert
3624523baf5aSMartin Sperl 	 * the @replaced_transfers - it may be spi_message.messages!
3625523baf5aSMartin Sperl 	 */
3626523baf5aSMartin Sperl 	rxfer->replaced_after = xfer_first->transfer_list.prev;
3627523baf5aSMartin Sperl 
362895c8222fSDavid Jander 	/* Remove the requested number of transfers */
3629523baf5aSMartin Sperl 	for (i = 0; i < remove; i++) {
3630350de7ceSAndy Shevchenko 		/*
3631350de7ceSAndy Shevchenko 		 * If the entry after replaced_after it is msg->transfers
3632523baf5aSMartin Sperl 		 * then we have been requested to remove more transfers
3633350de7ceSAndy Shevchenko 		 * than are in the list.
3634523baf5aSMartin Sperl 		 */
3635523baf5aSMartin Sperl 		if (rxfer->replaced_after->next == &msg->transfers) {
3636523baf5aSMartin Sperl 			dev_err(&msg->spi->dev,
3637523baf5aSMartin Sperl 				"requested to remove more spi_transfers than are available\n");
363895c8222fSDavid Jander 			/* Insert replaced transfers back into the message */
3639523baf5aSMartin Sperl 			list_splice(&rxfer->replaced_transfers,
3640523baf5aSMartin Sperl 				    rxfer->replaced_after);
3641523baf5aSMartin Sperl 
364295c8222fSDavid Jander 			/* Free the spi_replace_transfer structure... */
3643523baf5aSMartin Sperl 			spi_res_free(rxfer);
3644523baf5aSMartin Sperl 
364595c8222fSDavid Jander 			/* ...and return with an error */
3646523baf5aSMartin Sperl 			return ERR_PTR(-EINVAL);
3647523baf5aSMartin Sperl 		}
3648523baf5aSMartin Sperl 
3649350de7ceSAndy Shevchenko 		/*
3650350de7ceSAndy Shevchenko 		 * Remove the entry after replaced_after from list of
3651350de7ceSAndy Shevchenko 		 * transfers and add it to list of replaced_transfers.
3652523baf5aSMartin Sperl 		 */
3653523baf5aSMartin Sperl 		list_move_tail(rxfer->replaced_after->next,
3654523baf5aSMartin Sperl 			       &rxfer->replaced_transfers);
3655523baf5aSMartin Sperl 	}
3656523baf5aSMartin Sperl 
3657350de7ceSAndy Shevchenko 	/*
3658350de7ceSAndy Shevchenko 	 * Create copy of the given xfer with identical settings
3659350de7ceSAndy Shevchenko 	 * based on the first transfer to get removed.
3660523baf5aSMartin Sperl 	 */
3661523baf5aSMartin Sperl 	for (i = 0; i < insert; i++) {
366295c8222fSDavid Jander 		/* We need to run in reverse order */
3663523baf5aSMartin Sperl 		xfer = &rxfer->inserted_transfers[insert - 1 - i];
3664523baf5aSMartin Sperl 
366595c8222fSDavid Jander 		/* Copy all spi_transfer data */
3666523baf5aSMartin Sperl 		memcpy(xfer, xfer_first, sizeof(*xfer));
3667523baf5aSMartin Sperl 
366895c8222fSDavid Jander 		/* Add to list */
3669523baf5aSMartin Sperl 		list_add(&xfer->transfer_list, rxfer->replaced_after);
3670523baf5aSMartin Sperl 
367195c8222fSDavid Jander 		/* Clear cs_change and delay for all but the last */
3672523baf5aSMartin Sperl 		if (i) {
3673523baf5aSMartin Sperl 			xfer->cs_change = false;
3674bebcfd27SAlexandru Ardelean 			xfer->delay.value = 0;
3675523baf5aSMartin Sperl 		}
3676523baf5aSMartin Sperl 	}
3677523baf5aSMartin Sperl 
367895c8222fSDavid Jander 	/* Set up inserted... */
3679523baf5aSMartin Sperl 	rxfer->inserted = insert;
3680523baf5aSMartin Sperl 
368195c8222fSDavid Jander 	/* ...and register it with spi_res/spi_message */
3682523baf5aSMartin Sperl 	spi_res_add(msg, rxfer);
3683523baf5aSMartin Sperl 
3684523baf5aSMartin Sperl 	return rxfer;
3685523baf5aSMartin Sperl }
3686523baf5aSMartin Sperl 
36878caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
3688d9f12122SMartin Sperl 					struct spi_message *msg,
3689d9f12122SMartin Sperl 					struct spi_transfer **xferp,
3690d9f12122SMartin Sperl 					size_t maxsize,
3691d9f12122SMartin Sperl 					gfp_t gfp)
3692d9f12122SMartin Sperl {
3693d9f12122SMartin Sperl 	struct spi_transfer *xfer = *xferp, *xfers;
3694d9f12122SMartin Sperl 	struct spi_replaced_transfers *srt;
3695d9f12122SMartin Sperl 	size_t offset;
3696d9f12122SMartin Sperl 	size_t count, i;
3697d9f12122SMartin Sperl 
369895c8222fSDavid Jander 	/* Calculate how many we have to replace */
3699d9f12122SMartin Sperl 	count = DIV_ROUND_UP(xfer->len, maxsize);
3700d9f12122SMartin Sperl 
370195c8222fSDavid Jander 	/* Create replacement */
3702d9f12122SMartin Sperl 	srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
3703657d32efSDan Carpenter 	if (IS_ERR(srt))
3704657d32efSDan Carpenter 		return PTR_ERR(srt);
3705d9f12122SMartin Sperl 	xfers = srt->inserted_transfers;
3706d9f12122SMartin Sperl 
3707350de7ceSAndy Shevchenko 	/*
3708350de7ceSAndy Shevchenko 	 * Now handle each of those newly inserted spi_transfers.
3709350de7ceSAndy Shevchenko 	 * Note that the replacements spi_transfers all are preset
3710d9f12122SMartin Sperl 	 * to the same values as *xferp, so tx_buf, rx_buf and len
3711d9f12122SMartin Sperl 	 * are all identical (as well as most others)
3712d9f12122SMartin Sperl 	 * so we just have to fix up len and the pointers.
3713d9f12122SMartin Sperl 	 *
3714350de7ceSAndy Shevchenko 	 * This also includes support for the depreciated
3715350de7ceSAndy Shevchenko 	 * spi_message.is_dma_mapped interface.
3716d9f12122SMartin Sperl 	 */
3717d9f12122SMartin Sperl 
3718350de7ceSAndy Shevchenko 	/*
3719350de7ceSAndy Shevchenko 	 * The first transfer just needs the length modified, so we
3720350de7ceSAndy Shevchenko 	 * run it outside the loop.
3721d9f12122SMartin Sperl 	 */
3722c8dab77aSFabio Estevam 	xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
3723d9f12122SMartin Sperl 
372495c8222fSDavid Jander 	/* All the others need rx_buf/tx_buf also set */
3725d9f12122SMartin Sperl 	for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3726702ca026SAndy Shevchenko 		/* Update rx_buf, tx_buf and DMA */
3727d9f12122SMartin Sperl 		if (xfers[i].rx_buf)
3728d9f12122SMartin Sperl 			xfers[i].rx_buf += offset;
3729d9f12122SMartin Sperl 		if (xfers[i].rx_dma)
3730d9f12122SMartin Sperl 			xfers[i].rx_dma += offset;
3731d9f12122SMartin Sperl 		if (xfers[i].tx_buf)
3732d9f12122SMartin Sperl 			xfers[i].tx_buf += offset;
3733d9f12122SMartin Sperl 		if (xfers[i].tx_dma)
3734d9f12122SMartin Sperl 			xfers[i].tx_dma += offset;
3735d9f12122SMartin Sperl 
373695c8222fSDavid Jander 		/* Update length */
3737d9f12122SMartin Sperl 		xfers[i].len = min(maxsize, xfers[i].len - offset);
3738d9f12122SMartin Sperl 	}
3739d9f12122SMartin Sperl 
3740350de7ceSAndy Shevchenko 	/*
3741350de7ceSAndy Shevchenko 	 * We set up xferp to the last entry we have inserted,
3742350de7ceSAndy Shevchenko 	 * so that we skip those already split transfers.
3743d9f12122SMartin Sperl 	 */
3744d9f12122SMartin Sperl 	*xferp = &xfers[count - 1];
3745d9f12122SMartin Sperl 
374695c8222fSDavid Jander 	/* Increment statistics counters */
37476598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics,
3748d9f12122SMartin Sperl 				       transfers_split_maxsize);
37496598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics,
3750d9f12122SMartin Sperl 				       transfers_split_maxsize);
3751d9f12122SMartin Sperl 
3752d9f12122SMartin Sperl 	return 0;
3753d9f12122SMartin Sperl }
3754d9f12122SMartin Sperl 
3755d9f12122SMartin Sperl /**
3756ce2424d7SMauro Carvalho Chehab  * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3757d9f12122SMartin Sperl  *                               when an individual transfer exceeds a
3758d9f12122SMartin Sperl  *                               certain size
37598caab75fSGeert Uytterhoeven  * @ctlr:    the @spi_controller for this transfer
37603700ce95SMasanari Iida  * @msg:   the @spi_message to transform
37613700ce95SMasanari Iida  * @maxsize:  the maximum when to apply this
376210f11a22SJavier Martinez Canillas  * @gfp: GFP allocation flags
3763d9f12122SMartin Sperl  *
3764d9f12122SMartin Sperl  * Return: status of transformation
3765d9f12122SMartin Sperl  */
37668caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr,
3767d9f12122SMartin Sperl 				struct spi_message *msg,
3768d9f12122SMartin Sperl 				size_t maxsize,
3769d9f12122SMartin Sperl 				gfp_t gfp)
3770d9f12122SMartin Sperl {
3771d9f12122SMartin Sperl 	struct spi_transfer *xfer;
3772d9f12122SMartin Sperl 	int ret;
3773d9f12122SMartin Sperl 
3774350de7ceSAndy Shevchenko 	/*
3775350de7ceSAndy Shevchenko 	 * Iterate over the transfer_list,
3776d9f12122SMartin Sperl 	 * but note that xfer is advanced to the last transfer inserted
3777d9f12122SMartin Sperl 	 * to avoid checking sizes again unnecessarily (also xfer does
3778350de7ceSAndy Shevchenko 	 * potentially belong to a different list by the time the
3779350de7ceSAndy Shevchenko 	 * replacement has happened).
3780d9f12122SMartin Sperl 	 */
3781d9f12122SMartin Sperl 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3782d9f12122SMartin Sperl 		if (xfer->len > maxsize) {
37838caab75fSGeert Uytterhoeven 			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
37848caab75fSGeert Uytterhoeven 							   maxsize, gfp);
3785d9f12122SMartin Sperl 			if (ret)
3786d9f12122SMartin Sperl 				return ret;
3787d9f12122SMartin Sperl 		}
3788d9f12122SMartin Sperl 	}
3789d9f12122SMartin Sperl 
3790d9f12122SMartin Sperl 	return 0;
3791d9f12122SMartin Sperl }
3792d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
37938ae12a0dSDavid Brownell 
3794027781f3SLeonard Göhrs 
3795027781f3SLeonard Göhrs /**
3796702ca026SAndy Shevchenko  * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3797027781f3SLeonard Göhrs  *                                when an individual transfer exceeds a
3798027781f3SLeonard Göhrs  *                                certain number of SPI words
3799027781f3SLeonard Göhrs  * @ctlr:     the @spi_controller for this transfer
3800027781f3SLeonard Göhrs  * @msg:      the @spi_message to transform
3801027781f3SLeonard Göhrs  * @maxwords: the number of words to limit each transfer to
3802027781f3SLeonard Göhrs  * @gfp:      GFP allocation flags
3803027781f3SLeonard Göhrs  *
3804027781f3SLeonard Göhrs  * Return: status of transformation
3805027781f3SLeonard Göhrs  */
3806027781f3SLeonard Göhrs int spi_split_transfers_maxwords(struct spi_controller *ctlr,
3807027781f3SLeonard Göhrs 				 struct spi_message *msg,
3808027781f3SLeonard Göhrs 				 size_t maxwords,
3809027781f3SLeonard Göhrs 				 gfp_t gfp)
3810027781f3SLeonard Göhrs {
3811027781f3SLeonard Göhrs 	struct spi_transfer *xfer;
3812027781f3SLeonard Göhrs 
3813027781f3SLeonard Göhrs 	/*
3814027781f3SLeonard Göhrs 	 * Iterate over the transfer_list,
3815027781f3SLeonard Göhrs 	 * but note that xfer is advanced to the last transfer inserted
3816027781f3SLeonard Göhrs 	 * to avoid checking sizes again unnecessarily (also xfer does
3817027781f3SLeonard Göhrs 	 * potentially belong to a different list by the time the
3818027781f3SLeonard Göhrs 	 * replacement has happened).
3819027781f3SLeonard Göhrs 	 */
3820027781f3SLeonard Göhrs 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3821027781f3SLeonard Göhrs 		size_t maxsize;
3822027781f3SLeonard Göhrs 		int ret;
3823027781f3SLeonard Göhrs 
38242b308e71SAndy Shevchenko 		maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word));
3825027781f3SLeonard Göhrs 		if (xfer->len > maxsize) {
3826027781f3SLeonard Göhrs 			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3827027781f3SLeonard Göhrs 							   maxsize, gfp);
3828027781f3SLeonard Göhrs 			if (ret)
3829027781f3SLeonard Göhrs 				return ret;
3830027781f3SLeonard Göhrs 		}
3831027781f3SLeonard Göhrs 	}
3832027781f3SLeonard Göhrs 
3833027781f3SLeonard Göhrs 	return 0;
3834027781f3SLeonard Göhrs }
3835027781f3SLeonard Göhrs EXPORT_SYMBOL_GPL(spi_split_transfers_maxwords);
3836027781f3SLeonard Göhrs 
38378ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
38388ae12a0dSDavid Brownell 
3839702ca026SAndy Shevchenko /*
3840702ca026SAndy Shevchenko  * Core methods for SPI controller protocol drivers. Some of the
38417d077197SDavid Brownell  * other core methods are currently defined as inline functions.
38427d077197SDavid Brownell  */
38437d077197SDavid Brownell 
38448caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
38458caab75fSGeert Uytterhoeven 					u8 bits_per_word)
384663ab645fSStefan Brüns {
38478caab75fSGeert Uytterhoeven 	if (ctlr->bits_per_word_mask) {
384863ab645fSStefan Brüns 		/* Only 32 bits fit in the mask */
384963ab645fSStefan Brüns 		if (bits_per_word > 32)
385063ab645fSStefan Brüns 			return -EINVAL;
38518caab75fSGeert Uytterhoeven 		if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
385263ab645fSStefan Brüns 			return -EINVAL;
385363ab645fSStefan Brüns 	}
385463ab645fSStefan Brüns 
385563ab645fSStefan Brüns 	return 0;
385663ab645fSStefan Brüns }
385763ab645fSStefan Brüns 
38587d077197SDavid Brownell /**
3859684a4784STudor Ambarus  * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3860684a4784STudor Ambarus  * @spi: the device that requires specific CS timing configuration
3861684a4784STudor Ambarus  *
3862684a4784STudor Ambarus  * Return: zero on success, else a negative error code.
3863684a4784STudor Ambarus  */
3864684a4784STudor Ambarus static int spi_set_cs_timing(struct spi_device *spi)
3865684a4784STudor Ambarus {
3866684a4784STudor Ambarus 	struct device *parent = spi->controller->dev.parent;
3867684a4784STudor Ambarus 	int status = 0;
3868684a4784STudor Ambarus 
3869303feb3cSAmit Kumar Mahapatra 	if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) {
3870684a4784STudor Ambarus 		if (spi->controller->auto_runtime_pm) {
3871684a4784STudor Ambarus 			status = pm_runtime_get_sync(parent);
3872684a4784STudor Ambarus 			if (status < 0) {
3873684a4784STudor Ambarus 				pm_runtime_put_noidle(parent);
3874684a4784STudor Ambarus 				dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3875684a4784STudor Ambarus 					status);
3876684a4784STudor Ambarus 				return status;
3877684a4784STudor Ambarus 			}
3878684a4784STudor Ambarus 
3879684a4784STudor Ambarus 			status = spi->controller->set_cs_timing(spi);
3880684a4784STudor Ambarus 			pm_runtime_mark_last_busy(parent);
3881684a4784STudor Ambarus 			pm_runtime_put_autosuspend(parent);
3882684a4784STudor Ambarus 		} else {
3883684a4784STudor Ambarus 			status = spi->controller->set_cs_timing(spi);
3884684a4784STudor Ambarus 		}
3885684a4784STudor Ambarus 	}
3886684a4784STudor Ambarus 	return status;
3887684a4784STudor Ambarus }
3888684a4784STudor Ambarus 
3889684a4784STudor Ambarus /**
38907d077197SDavid Brownell  * spi_setup - setup SPI mode and clock rate
38917d077197SDavid Brownell  * @spi: the device whose settings are being modified
38927d077197SDavid Brownell  * Context: can sleep, and no requests are queued to the device
38937d077197SDavid Brownell  *
38947d077197SDavid Brownell  * SPI protocol drivers may need to update the transfer mode if the
38957d077197SDavid Brownell  * device doesn't work with its default.  They may likewise need
38967d077197SDavid Brownell  * to update clock rates or word sizes from initial values.  This function
38977d077197SDavid Brownell  * changes those settings, and must be called from a context that can sleep.
38987d077197SDavid Brownell  * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
38997d077197SDavid Brownell  * effect the next time the device is selected and data is transferred to
3900702ca026SAndy Shevchenko  * or from it.  When this function returns, the SPI device is deselected.
39017d077197SDavid Brownell  *
39027d077197SDavid Brownell  * Note that this call will fail if the protocol driver specifies an option
39037d077197SDavid Brownell  * that the underlying controller or its driver does not support.  For
39047d077197SDavid Brownell  * example, not all hardware supports wire transfers using nine bit words,
39057d077197SDavid Brownell  * LSB-first wire encoding, or active-high chipselects.
390697d56dc6SJavier Martinez Canillas  *
390797d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
39087d077197SDavid Brownell  */
39097d077197SDavid Brownell int spi_setup(struct spi_device *spi)
39107d077197SDavid Brownell {
391183596fbeSGeert Uytterhoeven 	unsigned	bad_bits, ugly_bits;
391273f93db5SPaul Kocialkowski 	int		status = 0;
39137d077197SDavid Brownell 
3914d962608cSDragos Bogdan 	/*
3915350de7ceSAndy Shevchenko 	 * Check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO
3916350de7ceSAndy Shevchenko 	 * are set at the same time.
3917f477b7fbSwangyuhang 	 */
3918d962608cSDragos Bogdan 	if ((hweight_long(spi->mode &
3919d962608cSDragos Bogdan 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) ||
3920d962608cSDragos Bogdan 	    (hweight_long(spi->mode &
3921d962608cSDragos Bogdan 		(SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) {
3922f477b7fbSwangyuhang 		dev_err(&spi->dev,
3923d962608cSDragos Bogdan 		"setup: can not select any two of dual, quad and no-rx/tx at the same time\n");
3924f477b7fbSwangyuhang 		return -EINVAL;
3925f477b7fbSwangyuhang 	}
3926350de7ceSAndy Shevchenko 	/* If it is SPI_3WIRE mode, DUAL and QUAD should be forbidden */
3927f477b7fbSwangyuhang 	if ((spi->mode & SPI_3WIRE) && (spi->mode &
39286b03061fSYogesh Narayan Gaur 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
39296b03061fSYogesh Narayan Gaur 		 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
3930f477b7fbSwangyuhang 		return -EINVAL;
3931350de7ceSAndy Shevchenko 	/*
3932350de7ceSAndy Shevchenko 	 * Help drivers fail *cleanly* when they need options
3933350de7ceSAndy Shevchenko 	 * that aren't supported with their current controller.
3934cbaa62e0SDavid Lechner 	 * SPI_CS_WORD has a fallback software implementation,
3935cbaa62e0SDavid Lechner 	 * so it is ignored here.
3936e7db06b5SDavid Brownell 	 */
3937d962608cSDragos Bogdan 	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
3938d962608cSDragos Bogdan 				 SPI_NO_TX | SPI_NO_RX);
393983596fbeSGeert Uytterhoeven 	ugly_bits = bad_bits &
39406b03061fSYogesh Narayan Gaur 		    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
39416b03061fSYogesh Narayan Gaur 		     SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
394283596fbeSGeert Uytterhoeven 	if (ugly_bits) {
394383596fbeSGeert Uytterhoeven 		dev_warn(&spi->dev,
394483596fbeSGeert Uytterhoeven 			 "setup: ignoring unsupported mode bits %x\n",
394583596fbeSGeert Uytterhoeven 			 ugly_bits);
394683596fbeSGeert Uytterhoeven 		spi->mode &= ~ugly_bits;
394783596fbeSGeert Uytterhoeven 		bad_bits &= ~ugly_bits;
394883596fbeSGeert Uytterhoeven 	}
3949e7db06b5SDavid Brownell 	if (bad_bits) {
3950eb288a1fSLinus Walleij 		dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
3951e7db06b5SDavid Brownell 			bad_bits);
3952e7db06b5SDavid Brownell 		return -EINVAL;
3953e7db06b5SDavid Brownell 	}
3954e7db06b5SDavid Brownell 
3955b3fe2e51SPaul Kocialkowski 	if (!spi->bits_per_word) {
39567d077197SDavid Brownell 		spi->bits_per_word = 8;
3957b3fe2e51SPaul Kocialkowski 	} else {
3958b3fe2e51SPaul Kocialkowski 		/*
3959b3fe2e51SPaul Kocialkowski 		 * Some controllers may not support the default 8 bits-per-word
3960b3fe2e51SPaul Kocialkowski 		 * so only perform the check when this is explicitly provided.
3961b3fe2e51SPaul Kocialkowski 		 */
39628caab75fSGeert Uytterhoeven 		status = __spi_validate_bits_per_word(spi->controller,
39638caab75fSGeert Uytterhoeven 						      spi->bits_per_word);
39645ab8d262SAndy Shevchenko 		if (status)
39655ab8d262SAndy Shevchenko 			return status;
3966b3fe2e51SPaul Kocialkowski 	}
396763ab645fSStefan Brüns 
39686820e812STudor Ambarus 	if (spi->controller->max_speed_hz &&
39696820e812STudor Ambarus 	    (!spi->max_speed_hz ||
39706820e812STudor Ambarus 	     spi->max_speed_hz > spi->controller->max_speed_hz))
39718caab75fSGeert Uytterhoeven 		spi->max_speed_hz = spi->controller->max_speed_hz;
3972052eb2d4SAxel Lin 
39734fae3a58SSerge Semin 	mutex_lock(&spi->controller->io_mutex);
39744fae3a58SSerge Semin 
3975c914dbf8SJoe Burmeister 	if (spi->controller->setup) {
39768caab75fSGeert Uytterhoeven 		status = spi->controller->setup(spi);
3977c914dbf8SJoe Burmeister 		if (status) {
3978c914dbf8SJoe Burmeister 			mutex_unlock(&spi->controller->io_mutex);
3979c914dbf8SJoe Burmeister 			dev_err(&spi->controller->dev, "Failed to setup device: %d\n",
3980c914dbf8SJoe Burmeister 				status);
3981c914dbf8SJoe Burmeister 			return status;
3982c914dbf8SJoe Burmeister 		}
3983c914dbf8SJoe Burmeister 	}
39847d077197SDavid Brownell 
3985684a4784STudor Ambarus 	status = spi_set_cs_timing(spi);
3986684a4784STudor Ambarus 	if (status) {
3987684a4784STudor Ambarus 		mutex_unlock(&spi->controller->io_mutex);
3988684a4784STudor Ambarus 		return status;
3989684a4784STudor Ambarus 	}
3990684a4784STudor Ambarus 
3991d948e6caSLuhua Xu 	if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3992dd769f15SMinghao Chi 		status = pm_runtime_resume_and_get(spi->controller->dev.parent);
3993d948e6caSLuhua Xu 		if (status < 0) {
39944fae3a58SSerge Semin 			mutex_unlock(&spi->controller->io_mutex);
3995d948e6caSLuhua Xu 			dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3996d948e6caSLuhua Xu 				status);
3997d948e6caSLuhua Xu 			return status;
3998d948e6caSLuhua Xu 		}
399957a94607STony Lindgren 
400057a94607STony Lindgren 		/*
400157a94607STony Lindgren 		 * We do not want to return positive value from pm_runtime_get,
400257a94607STony Lindgren 		 * there are many instances of devices calling spi_setup() and
400357a94607STony Lindgren 		 * checking for a non-zero return value instead of a negative
400457a94607STony Lindgren 		 * return value.
400557a94607STony Lindgren 		 */
400657a94607STony Lindgren 		status = 0;
400757a94607STony Lindgren 
4008d347b4aaSDavid Bauer 		spi_set_cs(spi, false, true);
4009d948e6caSLuhua Xu 		pm_runtime_mark_last_busy(spi->controller->dev.parent);
4010d948e6caSLuhua Xu 		pm_runtime_put_autosuspend(spi->controller->dev.parent);
4011d948e6caSLuhua Xu 	} else {
4012d347b4aaSDavid Bauer 		spi_set_cs(spi, false, true);
4013d948e6caSLuhua Xu 	}
4014abeedb01SFranklin S Cooper Jr 
40154fae3a58SSerge Semin 	mutex_unlock(&spi->controller->io_mutex);
40164fae3a58SSerge Semin 
4017924b5867SDouglas Anderson 	if (spi->rt && !spi->controller->rt) {
4018924b5867SDouglas Anderson 		spi->controller->rt = true;
4019924b5867SDouglas Anderson 		spi_set_thread_rt(spi->controller);
4020924b5867SDouglas Anderson 	}
4021924b5867SDouglas Anderson 
40225cb4e1f3SAndy Shevchenko 	trace_spi_setup(spi, status);
40235cb4e1f3SAndy Shevchenko 
402440b82c2dSAndy Shevchenko 	dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
402540b82c2dSAndy Shevchenko 			spi->mode & SPI_MODE_X_MASK,
40267d077197SDavid Brownell 			(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
40277d077197SDavid Brownell 			(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
40287d077197SDavid Brownell 			(spi->mode & SPI_3WIRE) ? "3wire, " : "",
40297d077197SDavid Brownell 			(spi->mode & SPI_LOOP) ? "loopback, " : "",
40307d077197SDavid Brownell 			spi->bits_per_word, spi->max_speed_hz,
40317d077197SDavid Brownell 			status);
40327d077197SDavid Brownell 
40337d077197SDavid Brownell 	return status;
40347d077197SDavid Brownell }
40357d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup);
40367d077197SDavid Brownell 
40376c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
40386c613f68SAlexandru Ardelean 				       struct spi_device *spi)
40396c613f68SAlexandru Ardelean {
40406c613f68SAlexandru Ardelean 	int delay1, delay2;
40416c613f68SAlexandru Ardelean 
40423984d39bSAlexandru Ardelean 	delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
40436c613f68SAlexandru Ardelean 	if (delay1 < 0)
40446c613f68SAlexandru Ardelean 		return delay1;
40456c613f68SAlexandru Ardelean 
40463984d39bSAlexandru Ardelean 	delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
40476c613f68SAlexandru Ardelean 	if (delay2 < 0)
40486c613f68SAlexandru Ardelean 		return delay2;
40496c613f68SAlexandru Ardelean 
40506c613f68SAlexandru Ardelean 	if (delay1 < delay2)
40516c613f68SAlexandru Ardelean 		memcpy(&xfer->word_delay, &spi->word_delay,
40526c613f68SAlexandru Ardelean 		       sizeof(xfer->word_delay));
40536c613f68SAlexandru Ardelean 
40546c613f68SAlexandru Ardelean 	return 0;
40556c613f68SAlexandru Ardelean }
40566c613f68SAlexandru Ardelean 
405790808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message)
4058cf32b71eSErnst Schwab {
40598caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
4060e6811d1dSLaxman Dewangan 	struct spi_transfer *xfer;
40616ea31293SAtsushi Nemoto 	int w_size;
4062cf32b71eSErnst Schwab 
406324a0013aSMark Brown 	if (list_empty(&message->transfers))
406424a0013aSMark Brown 		return -EINVAL;
406524a0013aSMark Brown 
4066350de7ceSAndy Shevchenko 	/*
4067350de7ceSAndy Shevchenko 	 * If an SPI controller does not support toggling the CS line on each
406871388b21SDavid Lechner 	 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
406971388b21SDavid Lechner 	 * for the CS line, we can emulate the CS-per-word hardware function by
4070cbaa62e0SDavid Lechner 	 * splitting transfers into one-word transfers and ensuring that
4071cbaa62e0SDavid Lechner 	 * cs_change is set for each transfer.
4072cbaa62e0SDavid Lechner 	 */
407371388b21SDavid Lechner 	if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
40744d8ff6b0SAmit Kumar Mahapatra 					  spi_is_csgpiod(spi))) {
4075169f5312SAndy Shevchenko 		size_t maxsize = BITS_TO_BYTES(spi->bits_per_word);
4076cbaa62e0SDavid Lechner 		int ret;
4077cbaa62e0SDavid Lechner 
4078cbaa62e0SDavid Lechner 		/* spi_split_transfers_maxsize() requires message->spi */
4079cbaa62e0SDavid Lechner 		message->spi = spi;
4080cbaa62e0SDavid Lechner 
4081cbaa62e0SDavid Lechner 		ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
4082cbaa62e0SDavid Lechner 						  GFP_KERNEL);
4083cbaa62e0SDavid Lechner 		if (ret)
4084cbaa62e0SDavid Lechner 			return ret;
4085cbaa62e0SDavid Lechner 
4086cbaa62e0SDavid Lechner 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
408795c8222fSDavid Jander 			/* Don't change cs_change on the last entry in the list */
4088cbaa62e0SDavid Lechner 			if (list_is_last(&xfer->transfer_list, &message->transfers))
4089cbaa62e0SDavid Lechner 				break;
4090cbaa62e0SDavid Lechner 			xfer->cs_change = 1;
4091cbaa62e0SDavid Lechner 		}
4092cbaa62e0SDavid Lechner 	}
4093cbaa62e0SDavid Lechner 
4094350de7ceSAndy Shevchenko 	/*
4095350de7ceSAndy Shevchenko 	 * Half-duplex links include original MicroWire, and ones with
4096cf32b71eSErnst Schwab 	 * only one data pin like SPI_3WIRE (switches direction) or where
4097cf32b71eSErnst Schwab 	 * either MOSI or MISO is missing.  They can also be caused by
4098cf32b71eSErnst Schwab 	 * software limitations.
4099cf32b71eSErnst Schwab 	 */
41008caab75fSGeert Uytterhoeven 	if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
41018caab75fSGeert Uytterhoeven 	    (spi->mode & SPI_3WIRE)) {
41028caab75fSGeert Uytterhoeven 		unsigned flags = ctlr->flags;
4103cf32b71eSErnst Schwab 
4104cf32b71eSErnst Schwab 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
4105cf32b71eSErnst Schwab 			if (xfer->rx_buf && xfer->tx_buf)
4106cf32b71eSErnst Schwab 				return -EINVAL;
41078caab75fSGeert Uytterhoeven 			if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
4108cf32b71eSErnst Schwab 				return -EINVAL;
41098caab75fSGeert Uytterhoeven 			if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
4110cf32b71eSErnst Schwab 				return -EINVAL;
4111cf32b71eSErnst Schwab 		}
4112cf32b71eSErnst Schwab 	}
4113cf32b71eSErnst Schwab 
4114350de7ceSAndy Shevchenko 	/*
4115059b8ffeSLaxman Dewangan 	 * Set transfer bits_per_word and max speed as spi device default if
4116059b8ffeSLaxman Dewangan 	 * it is not set for this transfer.
4117f477b7fbSwangyuhang 	 * Set transfer tx_nbits and rx_nbits as single transfer default
4118f477b7fbSwangyuhang 	 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
4119b7bb367aSJonas Bonn 	 * Ensure transfer word_delay is at least as long as that required by
4120b7bb367aSJonas Bonn 	 * device itself.
4121e6811d1dSLaxman Dewangan 	 */
412277e80588SMartin Sperl 	message->frame_length = 0;
4123e6811d1dSLaxman Dewangan 	list_for_each_entry(xfer, &message->transfers, transfer_list) {
41245d7e2b5eSMartin Sperl 		xfer->effective_speed_hz = 0;
4125078726ceSSourav Poddar 		message->frame_length += xfer->len;
4126e6811d1dSLaxman Dewangan 		if (!xfer->bits_per_word)
4127e6811d1dSLaxman Dewangan 			xfer->bits_per_word = spi->bits_per_word;
4128a6f87fadSAxel Lin 
4129a6f87fadSAxel Lin 		if (!xfer->speed_hz)
4130059b8ffeSLaxman Dewangan 			xfer->speed_hz = spi->max_speed_hz;
4131a6f87fadSAxel Lin 
41328caab75fSGeert Uytterhoeven 		if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
41338caab75fSGeert Uytterhoeven 			xfer->speed_hz = ctlr->max_speed_hz;
413456ede94aSGabor Juhos 
41358caab75fSGeert Uytterhoeven 		if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
4136543bb255SStephen Warren 			return -EINVAL;
4137a2fd4f9fSMark Brown 
41384d94bd21SIvan T. Ivanov 		/*
41394d94bd21SIvan T. Ivanov 		 * SPI transfer length should be multiple of SPI word size
4140350de7ceSAndy Shevchenko 		 * where SPI word size should be power-of-two multiple.
41414d94bd21SIvan T. Ivanov 		 */
41424d94bd21SIvan T. Ivanov 		if (xfer->bits_per_word <= 8)
41434d94bd21SIvan T. Ivanov 			w_size = 1;
41444d94bd21SIvan T. Ivanov 		else if (xfer->bits_per_word <= 16)
41454d94bd21SIvan T. Ivanov 			w_size = 2;
41464d94bd21SIvan T. Ivanov 		else
41474d94bd21SIvan T. Ivanov 			w_size = 4;
41484d94bd21SIvan T. Ivanov 
41494d94bd21SIvan T. Ivanov 		/* No partial transfers accepted */
41506ea31293SAtsushi Nemoto 		if (xfer->len % w_size)
41514d94bd21SIvan T. Ivanov 			return -EINVAL;
41524d94bd21SIvan T. Ivanov 
41538caab75fSGeert Uytterhoeven 		if (xfer->speed_hz && ctlr->min_speed_hz &&
41548caab75fSGeert Uytterhoeven 		    xfer->speed_hz < ctlr->min_speed_hz)
4155a2fd4f9fSMark Brown 			return -EINVAL;
4156f477b7fbSwangyuhang 
4157f477b7fbSwangyuhang 		if (xfer->tx_buf && !xfer->tx_nbits)
4158f477b7fbSwangyuhang 			xfer->tx_nbits = SPI_NBITS_SINGLE;
4159f477b7fbSwangyuhang 		if (xfer->rx_buf && !xfer->rx_nbits)
4160f477b7fbSwangyuhang 			xfer->rx_nbits = SPI_NBITS_SINGLE;
4161350de7ceSAndy Shevchenko 		/*
4162350de7ceSAndy Shevchenko 		 * Check transfer tx/rx_nbits:
41631afd9989SGeert Uytterhoeven 		 * 1. check the value matches one of single, dual and quad
41641afd9989SGeert Uytterhoeven 		 * 2. check tx/rx_nbits match the mode in spi_device
4165f477b7fbSwangyuhang 		 */
4166db90a441SSourav Poddar 		if (xfer->tx_buf) {
4167d962608cSDragos Bogdan 			if (spi->mode & SPI_NO_TX)
4168d962608cSDragos Bogdan 				return -EINVAL;
4169f477b7fbSwangyuhang 			if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
4170f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_DUAL &&
4171f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_QUAD)
4172a2fd4f9fSMark Brown 				return -EINVAL;
4173f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
4174f477b7fbSwangyuhang 				!(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
4175f477b7fbSwangyuhang 				return -EINVAL;
4176f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
4177f477b7fbSwangyuhang 				!(spi->mode & SPI_TX_QUAD))
4178f477b7fbSwangyuhang 				return -EINVAL;
4179db90a441SSourav Poddar 		}
418095c8222fSDavid Jander 		/* Check transfer rx_nbits */
4181db90a441SSourav Poddar 		if (xfer->rx_buf) {
4182d962608cSDragos Bogdan 			if (spi->mode & SPI_NO_RX)
4183d962608cSDragos Bogdan 				return -EINVAL;
4184f477b7fbSwangyuhang 			if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
4185f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_DUAL &&
4186f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_QUAD)
4187f477b7fbSwangyuhang 				return -EINVAL;
4188f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
4189f477b7fbSwangyuhang 				!(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
4190f477b7fbSwangyuhang 				return -EINVAL;
4191f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
4192f477b7fbSwangyuhang 				!(spi->mode & SPI_RX_QUAD))
4193f477b7fbSwangyuhang 				return -EINVAL;
4194e6811d1dSLaxman Dewangan 		}
4195b7bb367aSJonas Bonn 
41966c613f68SAlexandru Ardelean 		if (_spi_xfer_word_delay_update(xfer, spi))
41976c613f68SAlexandru Ardelean 			return -EINVAL;
4198e6811d1dSLaxman Dewangan 	}
4199e6811d1dSLaxman Dewangan 
4200cf32b71eSErnst Schwab 	message->status = -EINPROGRESS;
420190808738SMark Brown 
420290808738SMark Brown 	return 0;
420390808738SMark Brown }
420490808738SMark Brown 
420590808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message)
420690808738SMark Brown {
42078caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
4208b42faeeeSVladimir Oltean 	struct spi_transfer *xfer;
420990808738SMark Brown 
4210b5932f5cSBoris Brezillon 	/*
4211b5932f5cSBoris Brezillon 	 * Some controllers do not support doing regular SPI transfers. Return
4212b5932f5cSBoris Brezillon 	 * ENOTSUPP when this is the case.
4213b5932f5cSBoris Brezillon 	 */
4214b5932f5cSBoris Brezillon 	if (!ctlr->transfer)
4215b5932f5cSBoris Brezillon 		return -ENOTSUPP;
4216b5932f5cSBoris Brezillon 
421790808738SMark Brown 	message->spi = spi;
421890808738SMark Brown 
42196598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async);
42206598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async);
4221eca2ebc7SMartin Sperl 
422290808738SMark Brown 	trace_spi_message_submit(message);
422390808738SMark Brown 
4224b42faeeeSVladimir Oltean 	if (!ctlr->ptp_sts_supported) {
4225b42faeeeSVladimir Oltean 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
4226b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_pre = 0;
4227b42faeeeSVladimir Oltean 			ptp_read_system_prets(xfer->ptp_sts);
4228b42faeeeSVladimir Oltean 		}
4229b42faeeeSVladimir Oltean 	}
4230b42faeeeSVladimir Oltean 
42318caab75fSGeert Uytterhoeven 	return ctlr->transfer(spi, message);
4232cf32b71eSErnst Schwab }
4233cf32b71eSErnst Schwab 
4234568d0697SDavid Brownell /**
4235568d0697SDavid Brownell  * spi_async - asynchronous SPI transfer
4236568d0697SDavid Brownell  * @spi: device with which data will be exchanged
4237568d0697SDavid Brownell  * @message: describes the data transfers, including completion callback
4238702ca026SAndy Shevchenko  * Context: any (IRQs may be blocked, etc)
4239568d0697SDavid Brownell  *
4240568d0697SDavid Brownell  * This call may be used in_irq and other contexts which can't sleep,
4241568d0697SDavid Brownell  * as well as from task contexts which can sleep.
4242568d0697SDavid Brownell  *
4243568d0697SDavid Brownell  * The completion callback is invoked in a context which can't sleep.
4244568d0697SDavid Brownell  * Before that invocation, the value of message->status is undefined.
4245568d0697SDavid Brownell  * When the callback is issued, message->status holds either zero (to
4246568d0697SDavid Brownell  * indicate complete success) or a negative error code.  After that
4247568d0697SDavid Brownell  * callback returns, the driver which issued the transfer request may
4248568d0697SDavid Brownell  * deallocate the associated memory; it's no longer in use by any SPI
4249568d0697SDavid Brownell  * core or controller driver code.
4250568d0697SDavid Brownell  *
4251568d0697SDavid Brownell  * Note that although all messages to a spi_device are handled in
4252568d0697SDavid Brownell  * FIFO order, messages may go to different devices in other orders.
4253568d0697SDavid Brownell  * Some device might be higher priority, or have various "hard" access
4254568d0697SDavid Brownell  * time requirements, for example.
4255568d0697SDavid Brownell  *
4256568d0697SDavid Brownell  * On detection of any fault during the transfer, processing of
4257568d0697SDavid Brownell  * the entire message is aborted, and the device is deselected.
4258568d0697SDavid Brownell  * Until returning from the associated message completion callback,
4259568d0697SDavid Brownell  * no other spi_message queued to that device will be processed.
4260568d0697SDavid Brownell  * (This rule applies equally to all the synchronous transfer calls,
4261568d0697SDavid Brownell  * which are wrappers around this core asynchronous primitive.)
426297d56dc6SJavier Martinez Canillas  *
426397d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
4264568d0697SDavid Brownell  */
4265568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message)
4266568d0697SDavid Brownell {
42678caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
4268cf32b71eSErnst Schwab 	int ret;
4269cf32b71eSErnst Schwab 	unsigned long flags;
4270568d0697SDavid Brownell 
427190808738SMark Brown 	ret = __spi_validate(spi, message);
427290808738SMark Brown 	if (ret != 0)
427390808738SMark Brown 		return ret;
427490808738SMark Brown 
42758caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4276568d0697SDavid Brownell 
42778caab75fSGeert Uytterhoeven 	if (ctlr->bus_lock_flag)
4278cf32b71eSErnst Schwab 		ret = -EBUSY;
4279cf32b71eSErnst Schwab 	else
4280cf32b71eSErnst Schwab 		ret = __spi_async(spi, message);
4281568d0697SDavid Brownell 
42828caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4283cf32b71eSErnst Schwab 
4284cf32b71eSErnst Schwab 	return ret;
4285568d0697SDavid Brownell }
4286568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async);
4287568d0697SDavid Brownell 
4288cf32b71eSErnst Schwab /**
4289cf32b71eSErnst Schwab  * spi_async_locked - version of spi_async with exclusive bus usage
4290cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
4291cf32b71eSErnst Schwab  * @message: describes the data transfers, including completion callback
4292702ca026SAndy Shevchenko  * Context: any (IRQs may be blocked, etc)
4293cf32b71eSErnst Schwab  *
4294cf32b71eSErnst Schwab  * This call may be used in_irq and other contexts which can't sleep,
4295cf32b71eSErnst Schwab  * as well as from task contexts which can sleep.
4296cf32b71eSErnst Schwab  *
4297cf32b71eSErnst Schwab  * The completion callback is invoked in a context which can't sleep.
4298cf32b71eSErnst Schwab  * Before that invocation, the value of message->status is undefined.
4299cf32b71eSErnst Schwab  * When the callback is issued, message->status holds either zero (to
4300cf32b71eSErnst Schwab  * indicate complete success) or a negative error code.  After that
4301cf32b71eSErnst Schwab  * callback returns, the driver which issued the transfer request may
4302cf32b71eSErnst Schwab  * deallocate the associated memory; it's no longer in use by any SPI
4303cf32b71eSErnst Schwab  * core or controller driver code.
4304cf32b71eSErnst Schwab  *
4305cf32b71eSErnst Schwab  * Note that although all messages to a spi_device are handled in
4306cf32b71eSErnst Schwab  * FIFO order, messages may go to different devices in other orders.
4307cf32b71eSErnst Schwab  * Some device might be higher priority, or have various "hard" access
4308cf32b71eSErnst Schwab  * time requirements, for example.
4309cf32b71eSErnst Schwab  *
4310cf32b71eSErnst Schwab  * On detection of any fault during the transfer, processing of
4311cf32b71eSErnst Schwab  * the entire message is aborted, and the device is deselected.
4312cf32b71eSErnst Schwab  * Until returning from the associated message completion callback,
4313cf32b71eSErnst Schwab  * no other spi_message queued to that device will be processed.
4314cf32b71eSErnst Schwab  * (This rule applies equally to all the synchronous transfer calls,
4315cf32b71eSErnst Schwab  * which are wrappers around this core asynchronous primitive.)
431697d56dc6SJavier Martinez Canillas  *
431797d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
4318cf32b71eSErnst Schwab  */
4319da21fde0SUwe Kleine-König static int spi_async_locked(struct spi_device *spi, struct spi_message *message)
4320cf32b71eSErnst Schwab {
43218caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
4322cf32b71eSErnst Schwab 	int ret;
4323cf32b71eSErnst Schwab 	unsigned long flags;
4324cf32b71eSErnst Schwab 
432590808738SMark Brown 	ret = __spi_validate(spi, message);
432690808738SMark Brown 	if (ret != 0)
432790808738SMark Brown 		return ret;
432890808738SMark Brown 
43298caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4330cf32b71eSErnst Schwab 
4331cf32b71eSErnst Schwab 	ret = __spi_async(spi, message);
4332cf32b71eSErnst Schwab 
43338caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4334cf32b71eSErnst Schwab 
4335cf32b71eSErnst Schwab 	return ret;
4336cf32b71eSErnst Schwab 
4337cf32b71eSErnst Schwab }
4338cf32b71eSErnst Schwab 
4339ae7d2346SDavid Jander static void __spi_transfer_message_noqueue(struct spi_controller *ctlr, struct spi_message *msg)
4340ae7d2346SDavid Jander {
4341ae7d2346SDavid Jander 	bool was_busy;
4342ae7d2346SDavid Jander 	int ret;
4343ae7d2346SDavid Jander 
4344ae7d2346SDavid Jander 	mutex_lock(&ctlr->io_mutex);
4345ae7d2346SDavid Jander 
43461a9cafcbSDavid Jander 	was_busy = ctlr->busy;
4347ae7d2346SDavid Jander 
434872c5c59bSDavid Jander 	ctlr->cur_msg = msg;
4349ae7d2346SDavid Jander 	ret = __spi_pump_transfer_message(ctlr, msg, was_busy);
4350ae7d2346SDavid Jander 	if (ret)
4351bef4a48fSMark Hasemeyer 		dev_err(&ctlr->dev, "noqueue transfer failed\n");
435269fa9590SDavid Jander 	ctlr->cur_msg = NULL;
435369fa9590SDavid Jander 	ctlr->fallback = false;
435469fa9590SDavid Jander 
4355ae7d2346SDavid Jander 	if (!was_busy) {
4356ae7d2346SDavid Jander 		kfree(ctlr->dummy_rx);
4357ae7d2346SDavid Jander 		ctlr->dummy_rx = NULL;
4358ae7d2346SDavid Jander 		kfree(ctlr->dummy_tx);
4359ae7d2346SDavid Jander 		ctlr->dummy_tx = NULL;
4360ae7d2346SDavid Jander 		if (ctlr->unprepare_transfer_hardware &&
4361ae7d2346SDavid Jander 		    ctlr->unprepare_transfer_hardware(ctlr))
4362ae7d2346SDavid Jander 			dev_err(&ctlr->dev,
4363ae7d2346SDavid Jander 				"failed to unprepare transfer hardware\n");
4364ae7d2346SDavid Jander 		spi_idle_runtime_pm(ctlr);
4365ae7d2346SDavid Jander 	}
4366ae7d2346SDavid Jander 
4367ae7d2346SDavid Jander 	mutex_unlock(&ctlr->io_mutex);
4368ae7d2346SDavid Jander }
4369ae7d2346SDavid Jander 
43707d077197SDavid Brownell /*-------------------------------------------------------------------------*/
43717d077197SDavid Brownell 
4372350de7ceSAndy Shevchenko /*
4373350de7ceSAndy Shevchenko  * Utility methods for SPI protocol drivers, layered on
43747d077197SDavid Brownell  * top of the core.  Some other utility methods are defined as
43757d077197SDavid Brownell  * inline functions.
43767d077197SDavid Brownell  */
43777d077197SDavid Brownell 
43785d870c8eSAndrew Morton static void spi_complete(void *arg)
43795d870c8eSAndrew Morton {
43805d870c8eSAndrew Morton 	complete(arg);
43815d870c8eSAndrew Morton }
43825d870c8eSAndrew Morton 
4383ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message)
4384cf32b71eSErnst Schwab {
4385cf32b71eSErnst Schwab 	DECLARE_COMPLETION_ONSTACK(done);
4386cf32b71eSErnst Schwab 	int status;
43878caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
43880461a414SMark Brown 
4389bef4a48fSMark Hasemeyer 	if (__spi_check_suspended(ctlr)) {
4390bef4a48fSMark Hasemeyer 		dev_warn_once(&spi->dev, "Attempted to sync while suspend\n");
4391bef4a48fSMark Hasemeyer 		return -ESHUTDOWN;
4392bef4a48fSMark Hasemeyer 	}
4393bef4a48fSMark Hasemeyer 
43940461a414SMark Brown 	status = __spi_validate(spi, message);
43950461a414SMark Brown 	if (status != 0)
43960461a414SMark Brown 		return status;
4397cf32b71eSErnst Schwab 
43980461a414SMark Brown 	message->spi = spi;
4399cf32b71eSErnst Schwab 
44006598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync);
44016598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync);
4402eca2ebc7SMartin Sperl 
4403350de7ceSAndy Shevchenko 	/*
4404ae7d2346SDavid Jander 	 * Checking queue_empty here only guarantees async/sync message
4405ae7d2346SDavid Jander 	 * ordering when coming from the same context. It does not need to
4406ae7d2346SDavid Jander 	 * guard against reentrancy from a different context. The io_mutex
4407ae7d2346SDavid Jander 	 * will catch those cases.
44080461a414SMark Brown 	 */
4409b30f7c8eSMark Brown 	if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) {
4410ae7d2346SDavid Jander 		message->actual_length = 0;
4411ae7d2346SDavid Jander 		message->status = -EINPROGRESS;
44120461a414SMark Brown 
44130461a414SMark Brown 		trace_spi_message_submit(message);
44140461a414SMark Brown 
4415ae7d2346SDavid Jander 		SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate);
4416ae7d2346SDavid Jander 		SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate);
44170461a414SMark Brown 
4418ae7d2346SDavid Jander 		__spi_transfer_message_noqueue(ctlr, message);
4419ae7d2346SDavid Jander 
4420ae7d2346SDavid Jander 		return message->status;
4421ae7d2346SDavid Jander 	}
4422ae7d2346SDavid Jander 
4423ae7d2346SDavid Jander 	/*
4424ae7d2346SDavid Jander 	 * There are messages in the async queue that could have originated
4425ae7d2346SDavid Jander 	 * from the same context, so we need to preserve ordering.
4426ae7d2346SDavid Jander 	 * Therefor we send the message to the async queue and wait until they
4427ae7d2346SDavid Jander 	 * are completed.
4428ae7d2346SDavid Jander 	 */
4429ae7d2346SDavid Jander 	message->complete = spi_complete;
4430ae7d2346SDavid Jander 	message->context = &done;
4431cf32b71eSErnst Schwab 	status = spi_async_locked(spi, message);
4432cf32b71eSErnst Schwab 	if (status == 0) {
4433cf32b71eSErnst Schwab 		wait_for_completion(&done);
4434cf32b71eSErnst Schwab 		status = message->status;
4435cf32b71eSErnst Schwab 	}
4436cf32b71eSErnst Schwab 	message->context = NULL;
4437ae7d2346SDavid Jander 
4438cf32b71eSErnst Schwab 	return status;
4439cf32b71eSErnst Schwab }
4440cf32b71eSErnst Schwab 
44418ae12a0dSDavid Brownell /**
44428ae12a0dSDavid Brownell  * spi_sync - blocking/synchronous SPI data transfers
44438ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
44448ae12a0dSDavid Brownell  * @message: describes the data transfers
444533e34dc6SDavid Brownell  * Context: can sleep
44468ae12a0dSDavid Brownell  *
44478ae12a0dSDavid Brownell  * This call may only be used from a context that may sleep.  The sleep
44488ae12a0dSDavid Brownell  * is non-interruptible, and has no timeout.  Low-overhead controller
44498ae12a0dSDavid Brownell  * drivers may DMA directly into and out of the message buffers.
44508ae12a0dSDavid Brownell  *
44518ae12a0dSDavid Brownell  * Note that the SPI device's chip select is active during the message,
44528ae12a0dSDavid Brownell  * and then is normally disabled between messages.  Drivers for some
44538ae12a0dSDavid Brownell  * frequently-used devices may want to minimize costs of selecting a chip,
44548ae12a0dSDavid Brownell  * by leaving it selected in anticipation that the next message will go
44558ae12a0dSDavid Brownell  * to the same chip.  (That may increase power usage.)
44568ae12a0dSDavid Brownell  *
44570c868461SDavid Brownell  * Also, the caller is guaranteeing that the memory associated with the
44580c868461SDavid Brownell  * message will not be freed before this call returns.
44590c868461SDavid Brownell  *
446097d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
44618ae12a0dSDavid Brownell  */
44628ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message)
44638ae12a0dSDavid Brownell {
4464ef4d96ecSMark Brown 	int ret;
4465ef4d96ecSMark Brown 
44668caab75fSGeert Uytterhoeven 	mutex_lock(&spi->controller->bus_lock_mutex);
4467ef4d96ecSMark Brown 	ret = __spi_sync(spi, message);
44688caab75fSGeert Uytterhoeven 	mutex_unlock(&spi->controller->bus_lock_mutex);
4469ef4d96ecSMark Brown 
4470ef4d96ecSMark Brown 	return ret;
44718ae12a0dSDavid Brownell }
44728ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync);
44738ae12a0dSDavid Brownell 
4474cf32b71eSErnst Schwab /**
4475cf32b71eSErnst Schwab  * spi_sync_locked - version of spi_sync with exclusive bus usage
4476cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
4477cf32b71eSErnst Schwab  * @message: describes the data transfers
4478cf32b71eSErnst Schwab  * Context: can sleep
4479cf32b71eSErnst Schwab  *
4480cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
4481cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.  Low-overhead controller
4482cf32b71eSErnst Schwab  * drivers may DMA directly into and out of the message buffers.
4483cf32b71eSErnst Schwab  *
4484cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
448525985edcSLucas De Marchi  * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4486cf32b71eSErnst Schwab  * be released by a spi_bus_unlock call when the exclusive access is over.
4487cf32b71eSErnst Schwab  *
448897d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
4489cf32b71eSErnst Schwab  */
4490cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
4491cf32b71eSErnst Schwab {
4492ef4d96ecSMark Brown 	return __spi_sync(spi, message);
4493cf32b71eSErnst Schwab }
4494cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked);
4495cf32b71eSErnst Schwab 
4496cf32b71eSErnst Schwab /**
4497cf32b71eSErnst Schwab  * spi_bus_lock - obtain a lock for exclusive SPI bus usage
44988caab75fSGeert Uytterhoeven  * @ctlr: SPI bus master that should be locked for exclusive bus access
4499cf32b71eSErnst Schwab  * Context: can sleep
4500cf32b71eSErnst Schwab  *
4501cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
4502cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
4503cf32b71eSErnst Schwab  *
4504cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
4505cf32b71eSErnst Schwab  * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4506cf32b71eSErnst Schwab  * exclusive access is over. Data transfer must be done by spi_sync_locked
4507cf32b71eSErnst Schwab  * and spi_async_locked calls when the SPI bus lock is held.
4508cf32b71eSErnst Schwab  *
450997d56dc6SJavier Martinez Canillas  * Return: always zero.
4510cf32b71eSErnst Schwab  */
45118caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr)
4512cf32b71eSErnst Schwab {
4513cf32b71eSErnst Schwab 	unsigned long flags;
4514cf32b71eSErnst Schwab 
45158caab75fSGeert Uytterhoeven 	mutex_lock(&ctlr->bus_lock_mutex);
4516cf32b71eSErnst Schwab 
45178caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
45188caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 1;
45198caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4520cf32b71eSErnst Schwab 
452195c8222fSDavid Jander 	/* Mutex remains locked until spi_bus_unlock() is called */
4522cf32b71eSErnst Schwab 
4523cf32b71eSErnst Schwab 	return 0;
4524cf32b71eSErnst Schwab }
4525cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock);
4526cf32b71eSErnst Schwab 
4527cf32b71eSErnst Schwab /**
4528cf32b71eSErnst Schwab  * spi_bus_unlock - release the lock for exclusive SPI bus usage
45298caab75fSGeert Uytterhoeven  * @ctlr: SPI bus master that was locked for exclusive bus access
4530cf32b71eSErnst Schwab  * Context: can sleep
4531cf32b71eSErnst Schwab  *
4532cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
4533cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
4534cf32b71eSErnst Schwab  *
4535cf32b71eSErnst Schwab  * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4536cf32b71eSErnst Schwab  * call.
4537cf32b71eSErnst Schwab  *
453897d56dc6SJavier Martinez Canillas  * Return: always zero.
4539cf32b71eSErnst Schwab  */
45408caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr)
4541cf32b71eSErnst Schwab {
45428caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 0;
4543cf32b71eSErnst Schwab 
45448caab75fSGeert Uytterhoeven 	mutex_unlock(&ctlr->bus_lock_mutex);
4545cf32b71eSErnst Schwab 
4546cf32b71eSErnst Schwab 	return 0;
4547cf32b71eSErnst Schwab }
4548cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock);
4549cf32b71eSErnst Schwab 
455095c8222fSDavid Jander /* Portable code must never pass more than 32 bytes */
4551a9948b61SDavid Brownell #define	SPI_BUFSIZ	max(32, SMP_CACHE_BYTES)
45528ae12a0dSDavid Brownell 
45538ae12a0dSDavid Brownell static u8	*buf;
45548ae12a0dSDavid Brownell 
45558ae12a0dSDavid Brownell /**
45568ae12a0dSDavid Brownell  * spi_write_then_read - SPI synchronous write followed by read
45578ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
4558702ca026SAndy Shevchenko  * @txbuf: data to be written (need not be DMA-safe)
45598ae12a0dSDavid Brownell  * @n_tx: size of txbuf, in bytes
4560702ca026SAndy Shevchenko  * @rxbuf: buffer into which data will be read (need not be DMA-safe)
456127570497SJiri Pirko  * @n_rx: size of rxbuf, in bytes
456233e34dc6SDavid Brownell  * Context: can sleep
45638ae12a0dSDavid Brownell  *
45648ae12a0dSDavid Brownell  * This performs a half duplex MicroWire style transaction with the
45658ae12a0dSDavid Brownell  * device, sending txbuf and then reading rxbuf.  The return value
45668ae12a0dSDavid Brownell  * is zero for success, else a negative errno status code.
4567b885244eSDavid Brownell  * This call may only be used from a context that may sleep.
45688ae12a0dSDavid Brownell  *
4569c373643bSMark Brown  * Parameters to this routine are always copied using a small buffer.
457033e34dc6SDavid Brownell  * Performance-sensitive or bulk transfer code should instead use
4571702ca026SAndy Shevchenko  * spi_{async,sync}() calls with DMA-safe buffers.
457297d56dc6SJavier Martinez Canillas  *
457397d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
45748ae12a0dSDavid Brownell  */
45758ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi,
45760c4a1590SMark Brown 		const void *txbuf, unsigned n_tx,
45770c4a1590SMark Brown 		void *rxbuf, unsigned n_rx)
45788ae12a0dSDavid Brownell {
4579068f4070SDavid Brownell 	static DEFINE_MUTEX(lock);
45808ae12a0dSDavid Brownell 
45818ae12a0dSDavid Brownell 	int			status;
45828ae12a0dSDavid Brownell 	struct spi_message	message;
4583bdff549eSDavid Brownell 	struct spi_transfer	x[2];
45848ae12a0dSDavid Brownell 	u8			*local_buf;
45858ae12a0dSDavid Brownell 
4586350de7ceSAndy Shevchenko 	/*
4587350de7ceSAndy Shevchenko 	 * Use preallocated DMA-safe buffer if we can. We can't avoid
4588b3a223eeSMark Brown 	 * copying here, (as a pure convenience thing), but we can
4589b3a223eeSMark Brown 	 * keep heap costs out of the hot path unless someone else is
4590b3a223eeSMark Brown 	 * using the pre-allocated buffer or the transfer is too large.
45918ae12a0dSDavid Brownell 	 */
4592b3a223eeSMark Brown 	if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
45932cd94c8aSMark Brown 		local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
45942cd94c8aSMark Brown 				    GFP_KERNEL | GFP_DMA);
4595b3a223eeSMark Brown 		if (!local_buf)
4596b3a223eeSMark Brown 			return -ENOMEM;
4597b3a223eeSMark Brown 	} else {
4598b3a223eeSMark Brown 		local_buf = buf;
4599b3a223eeSMark Brown 	}
46008ae12a0dSDavid Brownell 
46018275c642SVitaly Wool 	spi_message_init(&message);
46025fe5f05eSJingoo Han 	memset(x, 0, sizeof(x));
4603bdff549eSDavid Brownell 	if (n_tx) {
4604bdff549eSDavid Brownell 		x[0].len = n_tx;
4605bdff549eSDavid Brownell 		spi_message_add_tail(&x[0], &message);
4606bdff549eSDavid Brownell 	}
4607bdff549eSDavid Brownell 	if (n_rx) {
4608bdff549eSDavid Brownell 		x[1].len = n_rx;
4609bdff549eSDavid Brownell 		spi_message_add_tail(&x[1], &message);
4610bdff549eSDavid Brownell 	}
46118275c642SVitaly Wool 
46128ae12a0dSDavid Brownell 	memcpy(local_buf, txbuf, n_tx);
4613bdff549eSDavid Brownell 	x[0].tx_buf = local_buf;
4614bdff549eSDavid Brownell 	x[1].rx_buf = local_buf + n_tx;
46158ae12a0dSDavid Brownell 
4616702ca026SAndy Shevchenko 	/* Do the I/O */
46178ae12a0dSDavid Brownell 	status = spi_sync(spi, &message);
46189b938b74SMarc Pignat 	if (status == 0)
4619bdff549eSDavid Brownell 		memcpy(rxbuf, x[1].rx_buf, n_rx);
46208ae12a0dSDavid Brownell 
4621bdff549eSDavid Brownell 	if (x[0].tx_buf == buf)
4622068f4070SDavid Brownell 		mutex_unlock(&lock);
46238ae12a0dSDavid Brownell 	else
46248ae12a0dSDavid Brownell 		kfree(local_buf);
46258ae12a0dSDavid Brownell 
46268ae12a0dSDavid Brownell 	return status;
46278ae12a0dSDavid Brownell }
46288ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read);
46298ae12a0dSDavid Brownell 
46308ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
46318ae12a0dSDavid Brownell 
4632da21fde0SUwe Kleine-König #if IS_ENABLED(CONFIG_OF_DYNAMIC)
463395c8222fSDavid Jander /* Must call put_device() when done with returned spi_device device */
4634da21fde0SUwe Kleine-König static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
4635ce79d54aSPantelis Antoniou {
4636cfba5de9SSuzuki K Poulose 	struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
4637cfba5de9SSuzuki K Poulose 
4638ce79d54aSPantelis Antoniou 	return dev ? to_spi_device(dev) : NULL;
4639ce79d54aSPantelis Antoniou }
4640ce79d54aSPantelis Antoniou 
464195c8222fSDavid Jander /* The spi controllers are not using spi_bus, so we find it with another way */
46428caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
4643ce79d54aSPantelis Antoniou {
4644ce79d54aSPantelis Antoniou 	struct device *dev;
4645ce79d54aSPantelis Antoniou 
4646cfba5de9SSuzuki K Poulose 	dev = class_find_device_by_of_node(&spi_master_class, node);
46476c364062SGeert Uytterhoeven 	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4648cfba5de9SSuzuki K Poulose 		dev = class_find_device_by_of_node(&spi_slave_class, node);
4649ce79d54aSPantelis Antoniou 	if (!dev)
4650ce79d54aSPantelis Antoniou 		return NULL;
4651ce79d54aSPantelis Antoniou 
465295c8222fSDavid Jander 	/* Reference got in class_find_device */
46538caab75fSGeert Uytterhoeven 	return container_of(dev, struct spi_controller, dev);
4654ce79d54aSPantelis Antoniou }
4655ce79d54aSPantelis Antoniou 
4656ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action,
4657ce79d54aSPantelis Antoniou 			 void *arg)
4658ce79d54aSPantelis Antoniou {
4659ce79d54aSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
46608caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
4661ce79d54aSPantelis Antoniou 	struct spi_device *spi;
4662ce79d54aSPantelis Antoniou 
4663ce79d54aSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, arg)) {
4664ce79d54aSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
46658caab75fSGeert Uytterhoeven 		ctlr = of_find_spi_controller_by_node(rd->dn->parent);
46668caab75fSGeert Uytterhoeven 		if (ctlr == NULL)
466795c8222fSDavid Jander 			return NOTIFY_OK;	/* Not for us */
4668ce79d54aSPantelis Antoniou 
4669bd6c1644SGeert Uytterhoeven 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
46708caab75fSGeert Uytterhoeven 			put_device(&ctlr->dev);
4671bd6c1644SGeert Uytterhoeven 			return NOTIFY_OK;
4672bd6c1644SGeert Uytterhoeven 		}
4673bd6c1644SGeert Uytterhoeven 
46741a50d940SGeert Uytterhoeven 		/*
46751a50d940SGeert Uytterhoeven 		 * Clear the flag before adding the device so that fw_devlink
46761a50d940SGeert Uytterhoeven 		 * doesn't skip adding consumers to this device.
46771a50d940SGeert Uytterhoeven 		 */
46781a50d940SGeert Uytterhoeven 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
46798caab75fSGeert Uytterhoeven 		spi = of_register_spi_device(ctlr, rd->dn);
46808caab75fSGeert Uytterhoeven 		put_device(&ctlr->dev);
4681ce79d54aSPantelis Antoniou 
4682ce79d54aSPantelis Antoniou 		if (IS_ERR(spi)) {
468325c56c88SRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
468425c56c88SRob Herring 					__func__, rd->dn);
4685e0af98a7SRalf Ramsauer 			of_node_clear_flag(rd->dn, OF_POPULATED);
4686ce79d54aSPantelis Antoniou 			return notifier_from_errno(PTR_ERR(spi));
4687ce79d54aSPantelis Antoniou 		}
4688ce79d54aSPantelis Antoniou 		break;
4689ce79d54aSPantelis Antoniou 
4690ce79d54aSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
469195c8222fSDavid Jander 		/* Already depopulated? */
4692bd6c1644SGeert Uytterhoeven 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
4693bd6c1644SGeert Uytterhoeven 			return NOTIFY_OK;
4694bd6c1644SGeert Uytterhoeven 
469595c8222fSDavid Jander 		/* Find our device by node */
4696ce79d54aSPantelis Antoniou 		spi = of_find_spi_device_by_node(rd->dn);
4697ce79d54aSPantelis Antoniou 		if (spi == NULL)
469895c8222fSDavid Jander 			return NOTIFY_OK;	/* No? not meant for us */
4699ce79d54aSPantelis Antoniou 
470095c8222fSDavid Jander 		/* Unregister takes one ref away */
4701ce79d54aSPantelis Antoniou 		spi_unregister_device(spi);
4702ce79d54aSPantelis Antoniou 
470395c8222fSDavid Jander 		/* And put the reference of the find */
4704ce79d54aSPantelis Antoniou 		put_device(&spi->dev);
4705ce79d54aSPantelis Antoniou 		break;
4706ce79d54aSPantelis Antoniou 	}
4707ce79d54aSPantelis Antoniou 
4708ce79d54aSPantelis Antoniou 	return NOTIFY_OK;
4709ce79d54aSPantelis Antoniou }
4710ce79d54aSPantelis Antoniou 
4711ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = {
4712ce79d54aSPantelis Antoniou 	.notifier_call = of_spi_notify,
4713ce79d54aSPantelis Antoniou };
4714ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4715ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier;
4716ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4717ce79d54aSPantelis Antoniou 
47187f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI)
47198caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data)
47207f24467fSOctavian Purdila {
47217f24467fSOctavian Purdila 	return ACPI_COMPANION(dev->parent) == data;
47227f24467fSOctavian Purdila }
47237f24467fSOctavian Purdila 
4724a8ecbc54SHans de Goede struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
47257f24467fSOctavian Purdila {
47267f24467fSOctavian Purdila 	struct device *dev;
47277f24467fSOctavian Purdila 
47287f24467fSOctavian Purdila 	dev = class_find_device(&spi_master_class, NULL, adev,
47298caab75fSGeert Uytterhoeven 				spi_acpi_controller_match);
47306c364062SGeert Uytterhoeven 	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
47316c364062SGeert Uytterhoeven 		dev = class_find_device(&spi_slave_class, NULL, adev,
47328caab75fSGeert Uytterhoeven 					spi_acpi_controller_match);
47337f24467fSOctavian Purdila 	if (!dev)
47347f24467fSOctavian Purdila 		return NULL;
47357f24467fSOctavian Purdila 
47368caab75fSGeert Uytterhoeven 	return container_of(dev, struct spi_controller, dev);
47377f24467fSOctavian Purdila }
4738a8ecbc54SHans de Goede EXPORT_SYMBOL_GPL(acpi_spi_find_controller_by_adev);
47397f24467fSOctavian Purdila 
47407f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
47417f24467fSOctavian Purdila {
47427f24467fSOctavian Purdila 	struct device *dev;
47437f24467fSOctavian Purdila 
474400500147SSuzuki K Poulose 	dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
47455b16668eSWolfram Sang 	return to_spi_device(dev);
47467f24467fSOctavian Purdila }
47477f24467fSOctavian Purdila 
47487f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
47497f24467fSOctavian Purdila 			   void *arg)
47507f24467fSOctavian Purdila {
47517f24467fSOctavian Purdila 	struct acpi_device *adev = arg;
47528caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
47537f24467fSOctavian Purdila 	struct spi_device *spi;
47547f24467fSOctavian Purdila 
47557f24467fSOctavian Purdila 	switch (value) {
47567f24467fSOctavian Purdila 	case ACPI_RECONFIG_DEVICE_ADD:
475762fcb99bSRafael J. Wysocki 		ctlr = acpi_spi_find_controller_by_adev(acpi_dev_parent(adev));
47588caab75fSGeert Uytterhoeven 		if (!ctlr)
47597f24467fSOctavian Purdila 			break;
47607f24467fSOctavian Purdila 
47618caab75fSGeert Uytterhoeven 		acpi_register_spi_device(ctlr, adev);
47628caab75fSGeert Uytterhoeven 		put_device(&ctlr->dev);
47637f24467fSOctavian Purdila 		break;
47647f24467fSOctavian Purdila 	case ACPI_RECONFIG_DEVICE_REMOVE:
47657f24467fSOctavian Purdila 		if (!acpi_device_enumerated(adev))
47667f24467fSOctavian Purdila 			break;
47677f24467fSOctavian Purdila 
47687f24467fSOctavian Purdila 		spi = acpi_spi_find_device_by_adev(adev);
47697f24467fSOctavian Purdila 		if (!spi)
47707f24467fSOctavian Purdila 			break;
47717f24467fSOctavian Purdila 
47727f24467fSOctavian Purdila 		spi_unregister_device(spi);
47737f24467fSOctavian Purdila 		put_device(&spi->dev);
47747f24467fSOctavian Purdila 		break;
47757f24467fSOctavian Purdila 	}
47767f24467fSOctavian Purdila 
47777f24467fSOctavian Purdila 	return NOTIFY_OK;
47787f24467fSOctavian Purdila }
47797f24467fSOctavian Purdila 
47807f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = {
47817f24467fSOctavian Purdila 	.notifier_call = acpi_spi_notify,
47827f24467fSOctavian Purdila };
47837f24467fSOctavian Purdila #else
47847f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier;
47857f24467fSOctavian Purdila #endif
47867f24467fSOctavian Purdila 
47878ae12a0dSDavid Brownell static int __init spi_init(void)
47888ae12a0dSDavid Brownell {
4789b885244eSDavid Brownell 	int	status;
47908ae12a0dSDavid Brownell 
4791e94b1766SChristoph Lameter 	buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
4792b885244eSDavid Brownell 	if (!buf) {
4793b885244eSDavid Brownell 		status = -ENOMEM;
4794b885244eSDavid Brownell 		goto err0;
47958ae12a0dSDavid Brownell 	}
4796b885244eSDavid Brownell 
4797b885244eSDavid Brownell 	status = bus_register(&spi_bus_type);
4798b885244eSDavid Brownell 	if (status < 0)
4799b885244eSDavid Brownell 		goto err1;
4800b885244eSDavid Brownell 
4801b885244eSDavid Brownell 	status = class_register(&spi_master_class);
4802b885244eSDavid Brownell 	if (status < 0)
4803b885244eSDavid Brownell 		goto err2;
4804ce79d54aSPantelis Antoniou 
48056c364062SGeert Uytterhoeven 	if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
48066c364062SGeert Uytterhoeven 		status = class_register(&spi_slave_class);
48076c364062SGeert Uytterhoeven 		if (status < 0)
48086c364062SGeert Uytterhoeven 			goto err3;
48096c364062SGeert Uytterhoeven 	}
48106c364062SGeert Uytterhoeven 
48115267720eSFabio Estevam 	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
4812ce79d54aSPantelis Antoniou 		WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
48137f24467fSOctavian Purdila 	if (IS_ENABLED(CONFIG_ACPI))
48147f24467fSOctavian Purdila 		WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
4815ce79d54aSPantelis Antoniou 
4816b885244eSDavid Brownell 	return 0;
4817b885244eSDavid Brownell 
48186c364062SGeert Uytterhoeven err3:
48196c364062SGeert Uytterhoeven 	class_unregister(&spi_master_class);
4820b885244eSDavid Brownell err2:
4821b885244eSDavid Brownell 	bus_unregister(&spi_bus_type);
4822b885244eSDavid Brownell err1:
4823b885244eSDavid Brownell 	kfree(buf);
4824b885244eSDavid Brownell 	buf = NULL;
4825b885244eSDavid Brownell err0:
4826b885244eSDavid Brownell 	return status;
4827b885244eSDavid Brownell }
4828b885244eSDavid Brownell 
4829350de7ceSAndy Shevchenko /*
4830350de7ceSAndy Shevchenko  * A board_info is normally registered in arch_initcall(),
4831350de7ceSAndy Shevchenko  * but even essential drivers wait till later.
4832b885244eSDavid Brownell  *
4833350de7ceSAndy Shevchenko  * REVISIT only boardinfo really needs static linking. The rest (device and
4834350de7ceSAndy Shevchenko  * driver registration) _could_ be dynamically linked (modular) ... Costs
4835b885244eSDavid Brownell  * include needing to have boardinfo data structures be much more public.
48368ae12a0dSDavid Brownell  */
4837673c0c00SDavid Brownell postcore_initcall(spi_init);
4838