xref: /linux/drivers/spi/spi.c (revision f53835f110f19934271c48f01e463c0aa302827d)
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,
31552267fe8SDavid Lechner 					      struct spi_message *msg)
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);
33152267fe8SDavid Lechner 	if (spi_valid_txbuf(msg, xfer))
3326598b91bSDavid Jander 		u64_stats_add(&stats->bytes_tx, xfer->len);
33352267fe8SDavid Lechner 	if (spi_valid_rxbuf(msg, xfer))
3346598b91bSDavid Jander 		u64_stats_add(&stats->bytes_rx, xfer->len);
335eca2ebc7SMartin Sperl 
3366598b91bSDavid Jander 	u64_stats_update_end(&stats->syncp);
33767b9d641SDavid Jander 	put_cpu();
338eca2ebc7SMartin Sperl }
3398ae12a0dSDavid Brownell 
340350de7ceSAndy Shevchenko /*
341350de7ceSAndy Shevchenko  * modalias support makes "modprobe $MODALIAS" new-style hotplug work,
3428ae12a0dSDavid Brownell  * and the sysfs version makes coldplug work too.
3438ae12a0dSDavid Brownell  */
3443f076575SAndy Shevchenko static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, const char *name)
34575368bf6SAnton Vorontsov {
34675368bf6SAnton Vorontsov 	while (id->name[0]) {
3473f076575SAndy Shevchenko 		if (!strcmp(name, id->name))
34875368bf6SAnton Vorontsov 			return id;
34975368bf6SAnton Vorontsov 		id++;
35075368bf6SAnton Vorontsov 	}
35175368bf6SAnton Vorontsov 	return NULL;
35275368bf6SAnton Vorontsov }
35375368bf6SAnton Vorontsov 
35475368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
35575368bf6SAnton Vorontsov {
35675368bf6SAnton Vorontsov 	const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
35775368bf6SAnton Vorontsov 
3583f076575SAndy Shevchenko 	return spi_match_id(sdrv->id_table, sdev->modalias);
35975368bf6SAnton Vorontsov }
36075368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id);
36175368bf6SAnton Vorontsov 
362aea672d0SAndy Shevchenko const void *spi_get_device_match_data(const struct spi_device *sdev)
363aea672d0SAndy Shevchenko {
364aea672d0SAndy Shevchenko 	const void *match;
365aea672d0SAndy Shevchenko 
366aea672d0SAndy Shevchenko 	match = device_get_match_data(&sdev->dev);
367aea672d0SAndy Shevchenko 	if (match)
368aea672d0SAndy Shevchenko 		return match;
369aea672d0SAndy Shevchenko 
370aea672d0SAndy Shevchenko 	return (const void *)spi_get_device_id(sdev)->driver_data;
371aea672d0SAndy Shevchenko }
372aea672d0SAndy Shevchenko EXPORT_SYMBOL_GPL(spi_get_device_match_data);
373aea672d0SAndy Shevchenko 
374d69d8048SGreg Kroah-Hartman static int spi_match_device(struct device *dev, const struct device_driver *drv)
3758ae12a0dSDavid Brownell {
3768ae12a0dSDavid Brownell 	const struct spi_device	*spi = to_spi_device(dev);
37775368bf6SAnton Vorontsov 	const struct spi_driver	*sdrv = to_spi_driver(drv);
37875368bf6SAnton Vorontsov 
3795039563eSTrent Piepho 	/* Check override first, and if set, only use the named driver */
3805039563eSTrent Piepho 	if (spi->driver_override)
3815039563eSTrent Piepho 		return strcmp(spi->driver_override, drv->name) == 0;
3825039563eSTrent Piepho 
3832b7a32f7SSinan Akman 	/* Attempt an OF style match */
3842b7a32f7SSinan Akman 	if (of_driver_match_device(dev, drv))
3852b7a32f7SSinan Akman 		return 1;
3862b7a32f7SSinan Akman 
38764bee4d2SMika Westerberg 	/* Then try ACPI */
38864bee4d2SMika Westerberg 	if (acpi_driver_match_device(dev, drv))
38964bee4d2SMika Westerberg 		return 1;
39064bee4d2SMika Westerberg 
39175368bf6SAnton Vorontsov 	if (sdrv->id_table)
3923f076575SAndy Shevchenko 		return !!spi_match_id(sdrv->id_table, spi->modalias);
3938ae12a0dSDavid Brownell 
39435f74fcaSKay Sievers 	return strcmp(spi->modalias, drv->name) == 0;
3958ae12a0dSDavid Brownell }
3968ae12a0dSDavid Brownell 
3972a81ada3SGreg Kroah-Hartman static int spi_uevent(const struct device *dev, struct kobj_uevent_env *env)
3988ae12a0dSDavid Brownell {
3998ae12a0dSDavid Brownell 	const struct spi_device		*spi = to_spi_device(dev);
4008c4ff6d0SZhang Rui 	int rc;
4018c4ff6d0SZhang Rui 
4028c4ff6d0SZhang Rui 	rc = acpi_device_uevent_modalias(dev, env);
4038c4ff6d0SZhang Rui 	if (rc != -ENODEV)
4048c4ff6d0SZhang Rui 		return rc;
4058ae12a0dSDavid Brownell 
4062856670fSAndy Shevchenko 	return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
4078ae12a0dSDavid Brownell }
4088ae12a0dSDavid Brownell 
4099db34ee6SUwe Kleine-König static int spi_probe(struct device *dev)
410b885244eSDavid Brownell {
411b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
41244af7927SJon Hunter 	struct spi_device		*spi = to_spi_device(dev);
41333cf00e5SMika Westerberg 	int ret;
414b885244eSDavid Brownell 
41586be408bSSylwester Nawrocki 	ret = of_clk_set_defaults(dev->of_node, false);
41686be408bSSylwester Nawrocki 	if (ret)
41786be408bSSylwester Nawrocki 		return ret;
41886be408bSSylwester Nawrocki 
41944af7927SJon Hunter 	if (dev->of_node) {
42044af7927SJon Hunter 		spi->irq = of_irq_get(dev->of_node, 0);
42144af7927SJon Hunter 		if (spi->irq == -EPROBE_DEFER)
42244af7927SJon Hunter 			return -EPROBE_DEFER;
42344af7927SJon Hunter 		if (spi->irq < 0)
42444af7927SJon Hunter 			spi->irq = 0;
42544af7927SJon Hunter 	}
42644af7927SJon Hunter 
427676e7c25SUlf Hansson 	ret = dev_pm_domain_attach(dev, true);
42871f277a7SUlf Hansson 	if (ret)
42971f277a7SUlf Hansson 		return ret;
43071f277a7SUlf Hansson 
431440408dbSUwe Kleine-König 	if (sdrv->probe) {
43244af7927SJon Hunter 		ret = sdrv->probe(spi);
43333cf00e5SMika Westerberg 		if (ret)
434676e7c25SUlf Hansson 			dev_pm_domain_detach(dev, true);
435440408dbSUwe Kleine-König 	}
43633cf00e5SMika Westerberg 
43733cf00e5SMika Westerberg 	return ret;
438b885244eSDavid Brownell }
439b885244eSDavid Brownell 
440fc7a6209SUwe Kleine-König static void spi_remove(struct device *dev)
441b885244eSDavid Brownell {
442b885244eSDavid Brownell 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
443b885244eSDavid Brownell 
444a0386bbaSUwe Kleine-König 	if (sdrv->remove)
445a0386bbaSUwe Kleine-König 		sdrv->remove(to_spi_device(dev));
4467795d475SUwe Kleine-König 
447676e7c25SUlf Hansson 	dev_pm_domain_detach(dev, true);
448b885244eSDavid Brownell }
449b885244eSDavid Brownell 
4509db34ee6SUwe Kleine-König static void spi_shutdown(struct device *dev)
451b885244eSDavid Brownell {
452a6f483b2SMarek Szyprowski 	if (dev->driver) {
453b885244eSDavid Brownell 		const struct spi_driver	*sdrv = to_spi_driver(dev->driver);
454b885244eSDavid Brownell 
4559db34ee6SUwe Kleine-König 		if (sdrv->shutdown)
456b885244eSDavid Brownell 			sdrv->shutdown(to_spi_device(dev));
457b885244eSDavid Brownell 	}
458a6f483b2SMarek Szyprowski }
459b885244eSDavid Brownell 
4606df534ccSGreg Kroah-Hartman const struct bus_type spi_bus_type = {
4619db34ee6SUwe Kleine-König 	.name		= "spi",
4629db34ee6SUwe Kleine-König 	.dev_groups	= spi_dev_groups,
4639db34ee6SUwe Kleine-König 	.match		= spi_match_device,
4649db34ee6SUwe Kleine-König 	.uevent		= spi_uevent,
4659db34ee6SUwe Kleine-König 	.probe		= spi_probe,
4669db34ee6SUwe Kleine-König 	.remove		= spi_remove,
4679db34ee6SUwe Kleine-König 	.shutdown	= spi_shutdown,
4689db34ee6SUwe Kleine-König };
4699db34ee6SUwe Kleine-König EXPORT_SYMBOL_GPL(spi_bus_type);
4709db34ee6SUwe Kleine-König 
47133e34dc6SDavid Brownell /**
472ca5d2485SAndrew F. Davis  * __spi_register_driver - register a SPI driver
47388c9321dSThierry Reding  * @owner: owner module of the driver to register
47433e34dc6SDavid Brownell  * @sdrv: the driver to register
47533e34dc6SDavid Brownell  * Context: can sleep
47697d56dc6SJavier Martinez Canillas  *
47797d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
47833e34dc6SDavid Brownell  */
479ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
480b885244eSDavid Brownell {
481ca5d2485SAndrew F. Davis 	sdrv->driver.owner = owner;
482b885244eSDavid Brownell 	sdrv->driver.bus = &spi_bus_type;
4835fa6863bSMark Brown 
4845fa6863bSMark Brown 	/*
4855fa6863bSMark Brown 	 * For Really Good Reasons we use spi: modaliases not of:
4865fa6863bSMark Brown 	 * modaliases for DT so module autoloading won't work if we
4875fa6863bSMark Brown 	 * don't have a spi_device_id as well as a compatible string.
4885fa6863bSMark Brown 	 */
4895fa6863bSMark Brown 	if (sdrv->driver.of_match_table) {
4905fa6863bSMark Brown 		const struct of_device_id *of_id;
4915fa6863bSMark Brown 
4925fa6863bSMark Brown 		for (of_id = sdrv->driver.of_match_table; of_id->compatible[0];
4935fa6863bSMark Brown 		     of_id++) {
4945fa6863bSMark Brown 			const char *of_name;
4955fa6863bSMark Brown 
4965fa6863bSMark Brown 			/* Strip off any vendor prefix */
4975fa6863bSMark Brown 			of_name = strnchr(of_id->compatible,
4985fa6863bSMark Brown 					  sizeof(of_id->compatible), ',');
4995fa6863bSMark Brown 			if (of_name)
5005fa6863bSMark Brown 				of_name++;
5015fa6863bSMark Brown 			else
5025fa6863bSMark Brown 				of_name = of_id->compatible;
5035fa6863bSMark Brown 
5045fa6863bSMark Brown 			if (sdrv->id_table) {
5055fa6863bSMark Brown 				const struct spi_device_id *spi_id;
5065fa6863bSMark Brown 
5073f076575SAndy Shevchenko 				spi_id = spi_match_id(sdrv->id_table, of_name);
508b79332efSAndy Shevchenko 				if (spi_id)
5095fa6863bSMark Brown 					continue;
5105fa6863bSMark Brown 			} else {
5115fa6863bSMark Brown 				if (strcmp(sdrv->driver.name, of_name) == 0)
5125fa6863bSMark Brown 					continue;
5135fa6863bSMark Brown 			}
5145fa6863bSMark Brown 
5155fa6863bSMark Brown 			pr_warn("SPI driver %s has no spi_device_id for %s\n",
5165fa6863bSMark Brown 				sdrv->driver.name, of_id->compatible);
5175fa6863bSMark Brown 		}
5185fa6863bSMark Brown 	}
5195fa6863bSMark Brown 
520b885244eSDavid Brownell 	return driver_register(&sdrv->driver);
521b885244eSDavid Brownell }
522ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver);
523b885244eSDavid Brownell 
5248ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
5258ae12a0dSDavid Brownell 
526350de7ceSAndy Shevchenko /*
527350de7ceSAndy Shevchenko  * SPI devices should normally not be created by SPI device drivers; that
5288caab75fSGeert Uytterhoeven  * would make them board-specific.  Similarly with SPI controller drivers.
5298ae12a0dSDavid Brownell  * Device registration normally goes into like arch/.../mach.../board-YYY.c
5308ae12a0dSDavid Brownell  * with other readonly (flashable) information about mainboard devices.
5318ae12a0dSDavid Brownell  */
5328ae12a0dSDavid Brownell 
5338ae12a0dSDavid Brownell struct boardinfo {
5348ae12a0dSDavid Brownell 	struct list_head	list;
5352b9603a0SFeng Tang 	struct spi_board_info	board_info;
5368ae12a0dSDavid Brownell };
5378ae12a0dSDavid Brownell 
5388ae12a0dSDavid Brownell static LIST_HEAD(board_list);
5398caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list);
5402b9603a0SFeng Tang 
5412b9603a0SFeng Tang /*
542be73e323SAndy Shevchenko  * Used to protect add/del operation for board_info list and
543350de7ceSAndy Shevchenko  * spi_controller list, and their matching process also used
544350de7ceSAndy Shevchenko  * to protect object of type struct idr.
5452b9603a0SFeng Tang  */
54694040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock);
5478ae12a0dSDavid Brownell 
548dc87c98eSGrant Likely /**
549dc87c98eSGrant Likely  * spi_alloc_device - Allocate a new SPI device
5508caab75fSGeert Uytterhoeven  * @ctlr: Controller to which device is connected
551dc87c98eSGrant Likely  * Context: can sleep
552dc87c98eSGrant Likely  *
553dc87c98eSGrant Likely  * Allows a driver to allocate and initialize a spi_device without
554dc87c98eSGrant Likely  * registering it immediately.  This allows a driver to directly
555dc87c98eSGrant Likely  * fill the spi_device with device parameters before calling
556dc87c98eSGrant Likely  * spi_add_device() on it.
557dc87c98eSGrant Likely  *
558dc87c98eSGrant Likely  * Caller is responsible to call spi_add_device() on the returned
5598caab75fSGeert Uytterhoeven  * spi_device structure to add it to the SPI controller.  If the caller
560dc87c98eSGrant Likely  * needs to discard the spi_device without adding it, then it should
561dc87c98eSGrant Likely  * call spi_dev_put() on it.
562dc87c98eSGrant Likely  *
56397d56dc6SJavier Martinez Canillas  * Return: a pointer to the new device, or NULL.
564dc87c98eSGrant Likely  */
565e3dc1399SStefan Binding struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
566dc87c98eSGrant Likely {
567dc87c98eSGrant Likely 	struct spi_device	*spi;
568dc87c98eSGrant Likely 
5698caab75fSGeert Uytterhoeven 	if (!spi_controller_get(ctlr))
570dc87c98eSGrant Likely 		return NULL;
571dc87c98eSGrant Likely 
5725fe5f05eSJingoo Han 	spi = kzalloc(sizeof(*spi), GFP_KERNEL);
573dc87c98eSGrant Likely 	if (!spi) {
5748caab75fSGeert Uytterhoeven 		spi_controller_put(ctlr);
575dc87c98eSGrant Likely 		return NULL;
576dc87c98eSGrant Likely 	}
577dc87c98eSGrant Likely 
5786598b91bSDavid Jander 	spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL);
5796598b91bSDavid Jander 	if (!spi->pcpu_statistics) {
5806598b91bSDavid Jander 		kfree(spi);
5816598b91bSDavid Jander 		spi_controller_put(ctlr);
5826598b91bSDavid Jander 		return NULL;
5836598b91bSDavid Jander 	}
5846598b91bSDavid Jander 
585620d269fSUwe Kleine-König 	spi->controller = ctlr;
5868caab75fSGeert Uytterhoeven 	spi->dev.parent = &ctlr->dev;
587dc87c98eSGrant Likely 	spi->dev.bus = &spi_bus_type;
588dc87c98eSGrant Likely 	spi->dev.release = spidev_release;
589ea235786SJohn Garry 	spi->mode = ctlr->buswidth_override_bits;
590eca2ebc7SMartin Sperl 
591dc87c98eSGrant Likely 	device_initialize(&spi->dev);
592dc87c98eSGrant Likely 	return spi;
593dc87c98eSGrant Likely }
594e3dc1399SStefan Binding EXPORT_SYMBOL_GPL(spi_alloc_device);
595dc87c98eSGrant Likely 
596e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi)
597e13ac47bSJarkko Nikula {
5988a101146SCharles Keepax 	struct device *dev = &spi->dev;
5998a101146SCharles Keepax 	struct fwnode_handle *fwnode = dev_fwnode(dev);
600e13ac47bSJarkko Nikula 
6018a101146SCharles Keepax 	if (is_acpi_device_node(fwnode)) {
6028a101146SCharles Keepax 		dev_set_name(dev, "spi-%s", acpi_dev_name(to_acpi_device_node(fwnode)));
603e13ac47bSJarkko Nikula 		return;
604e13ac47bSJarkko Nikula 	}
605e13ac47bSJarkko Nikula 
606ed892118SCharles Keepax 	if (is_software_node(fwnode)) {
607ed892118SCharles Keepax 		dev_set_name(dev, "spi-%pfwP", fwnode);
608e13ac47bSJarkko Nikula 		return;
609e13ac47bSJarkko Nikula 	}
610e13ac47bSJarkko Nikula 
6118caab75fSGeert Uytterhoeven 	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
612303feb3cSAmit Kumar Mahapatra 		     spi_get_chipselect(spi, 0));
613e13ac47bSJarkko Nikula }
614e13ac47bSJarkko Nikula 
615be84be4aSAndy Shevchenko /*
616be84be4aSAndy Shevchenko  * Zero(0) is a valid physical CS value and can be located at any
617be84be4aSAndy Shevchenko  * logical CS in the spi->chip_select[]. If all the physical CS
618be84be4aSAndy Shevchenko  * are initialized to 0 then It would be difficult to differentiate
619be84be4aSAndy Shevchenko  * between a valid physical CS 0 & an unused logical CS whose physical
620be84be4aSAndy Shevchenko  * CS can be 0. As a solution to this issue initialize all the CS to -1.
621be84be4aSAndy Shevchenko  * Now all the unused logical CS will have -1 physical CS value & can be
622be84be4aSAndy Shevchenko  * ignored while performing physical CS validity checks.
623be84be4aSAndy Shevchenko  */
624be84be4aSAndy Shevchenko #define SPI_INVALID_CS		((s8)-1)
625be84be4aSAndy Shevchenko 
626be84be4aSAndy Shevchenko static inline bool is_valid_cs(s8 chip_select)
627be84be4aSAndy Shevchenko {
628be84be4aSAndy Shevchenko 	return chip_select != SPI_INVALID_CS;
629be84be4aSAndy Shevchenko }
630be84be4aSAndy Shevchenko 
6319086d0f2SAndy Shevchenko static inline int spi_dev_check_cs(struct device *dev,
6329086d0f2SAndy Shevchenko 				   struct spi_device *spi, u8 idx,
6339086d0f2SAndy Shevchenko 				   struct spi_device *new_spi, u8 new_idx)
6349086d0f2SAndy Shevchenko {
6359086d0f2SAndy Shevchenko 	u8 cs, cs_new;
6369086d0f2SAndy Shevchenko 	u8 idx_new;
6379086d0f2SAndy Shevchenko 
6389086d0f2SAndy Shevchenko 	cs = spi_get_chipselect(spi, idx);
6399086d0f2SAndy Shevchenko 	for (idx_new = new_idx; idx_new < SPI_CS_CNT_MAX; idx_new++) {
6409086d0f2SAndy Shevchenko 		cs_new = spi_get_chipselect(new_spi, idx_new);
641be84be4aSAndy Shevchenko 		if (is_valid_cs(cs) && is_valid_cs(cs_new) && cs == cs_new) {
6429086d0f2SAndy Shevchenko 			dev_err(dev, "chipselect %u already in use\n", cs_new);
6439086d0f2SAndy Shevchenko 			return -EBUSY;
6449086d0f2SAndy Shevchenko 		}
6459086d0f2SAndy Shevchenko 	}
6469086d0f2SAndy Shevchenko 	return 0;
6479086d0f2SAndy Shevchenko }
6489086d0f2SAndy Shevchenko 
649b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data)
650b6fb8d3aSMika Westerberg {
651b6fb8d3aSMika Westerberg 	struct spi_device *spi = to_spi_device(dev);
652b6fb8d3aSMika Westerberg 	struct spi_device *new_spi = data;
6539086d0f2SAndy Shevchenko 	int status, idx;
654b6fb8d3aSMika Westerberg 
6554d8ff6b0SAmit Kumar Mahapatra 	if (spi->controller == new_spi->controller) {
6564d8ff6b0SAmit Kumar Mahapatra 		for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
6579086d0f2SAndy Shevchenko 			status = spi_dev_check_cs(dev, spi, idx, new_spi, 0);
6589086d0f2SAndy Shevchenko 			if (status)
6599086d0f2SAndy Shevchenko 				return status;
6604d8ff6b0SAmit Kumar Mahapatra 		}
6614d8ff6b0SAmit Kumar Mahapatra 	}
662b6fb8d3aSMika Westerberg 	return 0;
663b6fb8d3aSMika Westerberg }
664b6fb8d3aSMika Westerberg 
665c7299feaSSaravana Kannan static void spi_cleanup(struct spi_device *spi)
666c7299feaSSaravana Kannan {
667c7299feaSSaravana Kannan 	if (spi->controller->cleanup)
668c7299feaSSaravana Kannan 		spi->controller->cleanup(spi);
669c7299feaSSaravana Kannan }
670c7299feaSSaravana Kannan 
6710c79378cSSebastian Reichel static int __spi_add_device(struct spi_device *spi)
6720c79378cSSebastian Reichel {
6730c79378cSSebastian Reichel 	struct spi_controller *ctlr = spi->controller;
6740c79378cSSebastian Reichel 	struct device *dev = ctlr->dev.parent;
6759086d0f2SAndy Shevchenko 	int status, idx;
6769086d0f2SAndy Shevchenko 	u8 cs;
6770c79378cSSebastian Reichel 
6784d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
67936124deaSAndy Shevchenko 		/* Chipselects are numbered 0..max; validate. */
6804d8ff6b0SAmit Kumar Mahapatra 		cs = spi_get_chipselect(spi, idx);
681be84be4aSAndy Shevchenko 		if (is_valid_cs(cs) && cs >= ctlr->num_chipselect) {
6824d8ff6b0SAmit Kumar Mahapatra 			dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx),
68336124deaSAndy Shevchenko 				ctlr->num_chipselect);
68436124deaSAndy Shevchenko 			return -EINVAL;
68536124deaSAndy Shevchenko 		}
6864d8ff6b0SAmit Kumar Mahapatra 	}
6874d8ff6b0SAmit Kumar Mahapatra 
6884d8ff6b0SAmit Kumar Mahapatra 	/*
6894d8ff6b0SAmit Kumar Mahapatra 	 * Make sure that multiple logical CS doesn't map to the same physical CS.
6904d8ff6b0SAmit Kumar Mahapatra 	 * For example, spi->chip_select[0] != spi->chip_select[1] and so on.
6914d8ff6b0SAmit Kumar Mahapatra 	 */
6922c1b7bbeSAmit Kumar Mahapatra 	if (!spi_controller_is_target(ctlr)) {
6934d8ff6b0SAmit Kumar Mahapatra 		for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
6949086d0f2SAndy Shevchenko 			status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1);
6959086d0f2SAndy Shevchenko 			if (status)
6969086d0f2SAndy Shevchenko 				return status;
6974d8ff6b0SAmit Kumar Mahapatra 		}
6982c1b7bbeSAmit Kumar Mahapatra 	}
69936124deaSAndy Shevchenko 
70036124deaSAndy Shevchenko 	/* Set the bus ID string */
70136124deaSAndy Shevchenko 	spi_dev_set_name(spi);
70236124deaSAndy Shevchenko 
7036bfb15f3SUwe Kleine-König 	/*
7046bfb15f3SUwe Kleine-König 	 * We need to make sure there's no other device with this
7056bfb15f3SUwe Kleine-König 	 * chipselect **BEFORE** we call setup(), else we'll trash
7066bfb15f3SUwe Kleine-König 	 * its configuration.
7076bfb15f3SUwe Kleine-König 	 */
7080c79378cSSebastian Reichel 	status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
7094d8ff6b0SAmit Kumar Mahapatra 	if (status)
7100c79378cSSebastian Reichel 		return status;
7110c79378cSSebastian Reichel 
7120c79378cSSebastian Reichel 	/* Controller may unregister concurrently */
7130c79378cSSebastian Reichel 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC) &&
7140c79378cSSebastian Reichel 	    !device_is_registered(&ctlr->dev)) {
7150c79378cSSebastian Reichel 		return -ENODEV;
7160c79378cSSebastian Reichel 	}
7170c79378cSSebastian Reichel 
7184d8ff6b0SAmit Kumar Mahapatra 	if (ctlr->cs_gpiods) {
7194d8ff6b0SAmit Kumar Mahapatra 		u8 cs;
7204d8ff6b0SAmit Kumar Mahapatra 
7214d8ff6b0SAmit Kumar Mahapatra 		for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
7224d8ff6b0SAmit Kumar Mahapatra 			cs = spi_get_chipselect(spi, idx);
723be84be4aSAndy Shevchenko 			if (is_valid_cs(cs))
7244d8ff6b0SAmit Kumar Mahapatra 				spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]);
7254d8ff6b0SAmit Kumar Mahapatra 		}
7264d8ff6b0SAmit Kumar Mahapatra 	}
7270c79378cSSebastian Reichel 
728350de7ceSAndy Shevchenko 	/*
729350de7ceSAndy Shevchenko 	 * Drivers may modify this initial i/o setup, but will
7300c79378cSSebastian Reichel 	 * normally rely on the device being setup.  Devices
7310c79378cSSebastian Reichel 	 * using SPI_CS_HIGH can't coexist well otherwise...
7320c79378cSSebastian Reichel 	 */
7330c79378cSSebastian Reichel 	status = spi_setup(spi);
7340c79378cSSebastian Reichel 	if (status < 0) {
7350c79378cSSebastian Reichel 		dev_err(dev, "can't setup %s, status %d\n",
7360c79378cSSebastian Reichel 				dev_name(&spi->dev), status);
7370c79378cSSebastian Reichel 		return status;
7380c79378cSSebastian Reichel 	}
7390c79378cSSebastian Reichel 
7400c79378cSSebastian Reichel 	/* Device may be bound to an active driver when this returns */
7410c79378cSSebastian Reichel 	status = device_add(&spi->dev);
7420c79378cSSebastian Reichel 	if (status < 0) {
7430c79378cSSebastian Reichel 		dev_err(dev, "can't add %s, status %d\n",
7440c79378cSSebastian Reichel 				dev_name(&spi->dev), status);
7450c79378cSSebastian Reichel 		spi_cleanup(spi);
7460c79378cSSebastian Reichel 	} else {
7470c79378cSSebastian Reichel 		dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
7480c79378cSSebastian Reichel 	}
7490c79378cSSebastian Reichel 
7500c79378cSSebastian Reichel 	return status;
7510c79378cSSebastian Reichel }
7520c79378cSSebastian Reichel 
753dc87c98eSGrant Likely /**
754dc87c98eSGrant Likely  * spi_add_device - Add spi_device allocated with spi_alloc_device
755dc87c98eSGrant Likely  * @spi: spi_device to register
756dc87c98eSGrant Likely  *
757dc87c98eSGrant Likely  * Companion function to spi_alloc_device.  Devices allocated with
758702ca026SAndy Shevchenko  * spi_alloc_device can be added onto the SPI bus with this function.
759dc87c98eSGrant Likely  *
76097d56dc6SJavier Martinez Canillas  * Return: 0 on success; negative errno on failure
761dc87c98eSGrant Likely  */
762e3dc1399SStefan Binding int spi_add_device(struct spi_device *spi)
763dc87c98eSGrant Likely {
7648caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
765dc87c98eSGrant Likely 	int status;
766dc87c98eSGrant Likely 
7674d8ff6b0SAmit Kumar Mahapatra 	/* Set the bus ID string */
7684d8ff6b0SAmit Kumar Mahapatra 	spi_dev_set_name(spi);
7694d8ff6b0SAmit Kumar Mahapatra 
7706098475dSMark Brown 	mutex_lock(&ctlr->add_lock);
7710c79378cSSebastian Reichel 	status = __spi_add_device(spi);
7726098475dSMark Brown 	mutex_unlock(&ctlr->add_lock);
773e48880e0SDavid Brownell 	return status;
774dc87c98eSGrant Likely }
775e3dc1399SStefan Binding EXPORT_SYMBOL_GPL(spi_add_device);
7768ae12a0dSDavid Brownell 
7775ee91605SAndy Shevchenko static void spi_set_all_cs_unused(struct spi_device *spi)
7785ee91605SAndy Shevchenko {
7795ee91605SAndy Shevchenko 	u8 idx;
7805ee91605SAndy Shevchenko 
7815ee91605SAndy Shevchenko 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
782be84be4aSAndy Shevchenko 		spi_set_chipselect(spi, idx, SPI_INVALID_CS);
7835ee91605SAndy Shevchenko }
7845ee91605SAndy Shevchenko 
78533e34dc6SDavid Brownell /**
78633e34dc6SDavid Brownell  * spi_new_device - instantiate one new SPI device
7878caab75fSGeert Uytterhoeven  * @ctlr: Controller to which device is connected
78833e34dc6SDavid Brownell  * @chip: Describes the SPI device
78933e34dc6SDavid Brownell  * Context: can sleep
79033e34dc6SDavid Brownell  *
79133e34dc6SDavid Brownell  * On typical mainboards, this is purely internal; and it's not needed
7928ae12a0dSDavid Brownell  * after board init creates the hard-wired devices.  Some development
7938ae12a0dSDavid Brownell  * platforms may not be able to use spi_register_board_info though, and
7948ae12a0dSDavid Brownell  * this is exported so that for example a USB or parport based adapter
7958ae12a0dSDavid Brownell  * driver could add devices (which it would learn about out-of-band).
796082c8cb4SDavid Brownell  *
79797d56dc6SJavier Martinez Canillas  * Return: the new device, or NULL.
7988ae12a0dSDavid Brownell  */
7998caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr,
800e9d5a461SAdrian Bunk 				  struct spi_board_info *chip)
8018ae12a0dSDavid Brownell {
8028ae12a0dSDavid Brownell 	struct spi_device	*proxy;
8038ae12a0dSDavid Brownell 	int			status;
8048ae12a0dSDavid Brownell 
805350de7ceSAndy Shevchenko 	/*
806350de7ceSAndy Shevchenko 	 * NOTE:  caller did any chip->bus_num checks necessary.
807082c8cb4SDavid Brownell 	 *
808082c8cb4SDavid Brownell 	 * Also, unless we change the return value convention to use
809082c8cb4SDavid Brownell 	 * error-or-pointer (not NULL-or-pointer), troubleshootability
810082c8cb4SDavid Brownell 	 * suggests syslogged diagnostics are best here (ugh).
811082c8cb4SDavid Brownell 	 */
812082c8cb4SDavid Brownell 
8138caab75fSGeert Uytterhoeven 	proxy = spi_alloc_device(ctlr);
814dc87c98eSGrant Likely 	if (!proxy)
8158ae12a0dSDavid Brownell 		return NULL;
8168ae12a0dSDavid Brownell 
817102eb975SGrant Likely 	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
818102eb975SGrant Likely 
8195ee91605SAndy Shevchenko 	/* Use provided chip-select for proxy device */
8205ee91605SAndy Shevchenko 	spi_set_all_cs_unused(proxy);
821303feb3cSAmit Kumar Mahapatra 	spi_set_chipselect(proxy, 0, chip->chip_select);
8225ee91605SAndy Shevchenko 
8238ae12a0dSDavid Brownell 	proxy->max_speed_hz = chip->max_speed_hz;
824980a01c9SDavid Brownell 	proxy->mode = chip->mode;
8258ae12a0dSDavid Brownell 	proxy->irq = chip->irq;
82651e99de5SWolfram Sang 	strscpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
8278ae12a0dSDavid Brownell 	proxy->dev.platform_data = (void *) chip->platform_data;
8288ae12a0dSDavid Brownell 	proxy->controller_data = chip->controller_data;
8298ae12a0dSDavid Brownell 	proxy->controller_state = NULL;
8304d8ff6b0SAmit Kumar Mahapatra 	/*
831bb409962SAndy Shevchenko 	 * By default spi->chip_select[0] will hold the physical CS number,
832bb409962SAndy Shevchenko 	 * so set bit 0 in spi->cs_index_mask.
8334d8ff6b0SAmit Kumar Mahapatra 	 */
834bb409962SAndy Shevchenko 	proxy->cs_index_mask = BIT(0);
8358ae12a0dSDavid Brownell 
83647afc77bSHeikki Krogerus 	if (chip->swnode) {
83747afc77bSHeikki Krogerus 		status = device_add_software_node(&proxy->dev, chip->swnode);
838826cf175SDmitry Torokhov 		if (status) {
8399d902c2aSColin Ian King 			dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n",
840826cf175SDmitry Torokhov 				chip->modalias, status);
841826cf175SDmitry Torokhov 			goto err_dev_put;
842826cf175SDmitry Torokhov 		}
8438ae12a0dSDavid Brownell 	}
844dc87c98eSGrant Likely 
845826cf175SDmitry Torokhov 	status = spi_add_device(proxy);
846826cf175SDmitry Torokhov 	if (status < 0)
847df41a5daSHeikki Krogerus 		goto err_dev_put;
848826cf175SDmitry Torokhov 
849dc87c98eSGrant Likely 	return proxy;
850826cf175SDmitry Torokhov 
851826cf175SDmitry Torokhov err_dev_put:
852df41a5daSHeikki Krogerus 	device_remove_software_node(&proxy->dev);
853826cf175SDmitry Torokhov 	spi_dev_put(proxy);
854826cf175SDmitry Torokhov 	return NULL;
855dc87c98eSGrant Likely }
8568ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device);
8578ae12a0dSDavid Brownell 
8583b1884c2SGeert Uytterhoeven /**
8593b1884c2SGeert Uytterhoeven  * spi_unregister_device - unregister a single SPI device
8603b1884c2SGeert Uytterhoeven  * @spi: spi_device to unregister
8613b1884c2SGeert Uytterhoeven  *
8623b1884c2SGeert Uytterhoeven  * Start making the passed SPI device vanish. Normally this would be handled
8638caab75fSGeert Uytterhoeven  * by spi_unregister_controller().
8643b1884c2SGeert Uytterhoeven  */
8653b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi)
8663b1884c2SGeert Uytterhoeven {
867bd6c1644SGeert Uytterhoeven 	if (!spi)
868bd6c1644SGeert Uytterhoeven 		return;
869bd6c1644SGeert Uytterhoeven 
8708324147fSJohan Hovold 	if (spi->dev.of_node) {
871bd6c1644SGeert Uytterhoeven 		of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
8728324147fSJohan Hovold 		of_node_put(spi->dev.of_node);
8738324147fSJohan Hovold 	}
8747f24467fSOctavian Purdila 	if (ACPI_COMPANION(&spi->dev))
8757f24467fSOctavian Purdila 		acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
87647afc77bSHeikki Krogerus 	device_remove_software_node(&spi->dev);
87727e7db56SSaravana Kannan 	device_del(&spi->dev);
87827e7db56SSaravana Kannan 	spi_cleanup(spi);
87927e7db56SSaravana Kannan 	put_device(&spi->dev);
8803b1884c2SGeert Uytterhoeven }
8813b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device);
8823b1884c2SGeert Uytterhoeven 
8838caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
8842b9603a0SFeng Tang 					      struct spi_board_info *bi)
8852b9603a0SFeng Tang {
8862b9603a0SFeng Tang 	struct spi_device *dev;
8872b9603a0SFeng Tang 
8888caab75fSGeert Uytterhoeven 	if (ctlr->bus_num != bi->bus_num)
8892b9603a0SFeng Tang 		return;
8902b9603a0SFeng Tang 
8918caab75fSGeert Uytterhoeven 	dev = spi_new_device(ctlr, bi);
8922b9603a0SFeng Tang 	if (!dev)
8938caab75fSGeert Uytterhoeven 		dev_err(ctlr->dev.parent, "can't create new device for %s\n",
8942b9603a0SFeng Tang 			bi->modalias);
8952b9603a0SFeng Tang }
8962b9603a0SFeng Tang 
89733e34dc6SDavid Brownell /**
89833e34dc6SDavid Brownell  * spi_register_board_info - register SPI devices for a given board
89933e34dc6SDavid Brownell  * @info: array of chip descriptors
90033e34dc6SDavid Brownell  * @n: how many descriptors are provided
90133e34dc6SDavid Brownell  * Context: can sleep
90233e34dc6SDavid Brownell  *
9038ae12a0dSDavid Brownell  * Board-specific early init code calls this (probably during arch_initcall)
9048ae12a0dSDavid Brownell  * with segments of the SPI device table.  Any device nodes are created later,
9058ae12a0dSDavid Brownell  * after the relevant parent SPI controller (bus_num) is defined.  We keep
9068ae12a0dSDavid Brownell  * this table of devices forever, so that reloading a controller driver will
9078ae12a0dSDavid Brownell  * not make Linux forget about these hard-wired devices.
9088ae12a0dSDavid Brownell  *
9098ae12a0dSDavid Brownell  * Other code can also call this, e.g. a particular add-on board might provide
9108ae12a0dSDavid Brownell  * SPI devices through its expansion connector, so code initializing that board
9118ae12a0dSDavid Brownell  * would naturally declare its SPI devices.
9128ae12a0dSDavid Brownell  *
9138ae12a0dSDavid Brownell  * The board info passed can safely be __initdata ... but be careful of
9148ae12a0dSDavid Brownell  * any embedded pointers (platform_data, etc), they're copied as-is.
91597d56dc6SJavier Martinez Canillas  *
91697d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
9178ae12a0dSDavid Brownell  */
918fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n)
9198ae12a0dSDavid Brownell {
9208ae12a0dSDavid Brownell 	struct boardinfo *bi;
9212b9603a0SFeng Tang 	int i;
9228ae12a0dSDavid Brownell 
923c7908a37SXiubo Li 	if (!n)
924f974cf57SDmitry Torokhov 		return 0;
925c7908a37SXiubo Li 
926f9bdb7fdSMarkus Elfring 	bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
9278ae12a0dSDavid Brownell 	if (!bi)
9288ae12a0dSDavid Brownell 		return -ENOMEM;
9298ae12a0dSDavid Brownell 
9302b9603a0SFeng Tang 	for (i = 0; i < n; i++, bi++, info++) {
9318caab75fSGeert Uytterhoeven 		struct spi_controller *ctlr;
9322b9603a0SFeng Tang 
9332b9603a0SFeng Tang 		memcpy(&bi->board_info, info, sizeof(*info));
934826cf175SDmitry Torokhov 
93594040828SMatthias Kaehlcke 		mutex_lock(&board_lock);
9368ae12a0dSDavid Brownell 		list_add_tail(&bi->list, &board_list);
9378caab75fSGeert Uytterhoeven 		list_for_each_entry(ctlr, &spi_controller_list, list)
9388caab75fSGeert Uytterhoeven 			spi_match_controller_to_boardinfo(ctlr,
9398caab75fSGeert Uytterhoeven 							  &bi->board_info);
94094040828SMatthias Kaehlcke 		mutex_unlock(&board_lock);
9412b9603a0SFeng Tang 	}
9422b9603a0SFeng Tang 
9438ae12a0dSDavid Brownell 	return 0;
9448ae12a0dSDavid Brownell }
9458ae12a0dSDavid Brownell 
9468ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
9478ae12a0dSDavid Brownell 
948fb51601bSUwe Kleine-König /* Core methods for SPI resource management */
949fb51601bSUwe Kleine-König 
950fb51601bSUwe Kleine-König /**
951fb51601bSUwe Kleine-König  * spi_res_alloc - allocate a spi resource that is life-cycle managed
952fb51601bSUwe Kleine-König  *                 during the processing of a spi_message while using
953fb51601bSUwe Kleine-König  *                 spi_transfer_one
954702ca026SAndy Shevchenko  * @spi:     the SPI device for which we allocate memory
955fb51601bSUwe Kleine-König  * @release: the release code to execute for this resource
956fb51601bSUwe Kleine-König  * @size:    size to alloc and return
957fb51601bSUwe Kleine-König  * @gfp:     GFP allocation flags
958fb51601bSUwe Kleine-König  *
959fb51601bSUwe Kleine-König  * Return: the pointer to the allocated data
960fb51601bSUwe Kleine-König  *
961fb51601bSUwe Kleine-König  * This may get enhanced in the future to allocate from a memory pool
962fb51601bSUwe Kleine-König  * of the @spi_device or @spi_controller to avoid repeated allocations.
963fb51601bSUwe Kleine-König  */
964da21fde0SUwe Kleine-König static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release,
965fb51601bSUwe Kleine-König 			   size_t size, gfp_t gfp)
966fb51601bSUwe Kleine-König {
967fb51601bSUwe Kleine-König 	struct spi_res *sres;
968fb51601bSUwe Kleine-König 
969fb51601bSUwe Kleine-König 	sres = kzalloc(sizeof(*sres) + size, gfp);
970fb51601bSUwe Kleine-König 	if (!sres)
971fb51601bSUwe Kleine-König 		return NULL;
972fb51601bSUwe Kleine-König 
973fb51601bSUwe Kleine-König 	INIT_LIST_HEAD(&sres->entry);
974fb51601bSUwe Kleine-König 	sres->release = release;
975fb51601bSUwe Kleine-König 
976fb51601bSUwe Kleine-König 	return sres->data;
977fb51601bSUwe Kleine-König }
978fb51601bSUwe Kleine-König 
979fb51601bSUwe Kleine-König /**
980702ca026SAndy Shevchenko  * spi_res_free - free an SPI resource
981fb51601bSUwe Kleine-König  * @res: pointer to the custom data of a resource
982fb51601bSUwe Kleine-König  */
983da21fde0SUwe Kleine-König static void spi_res_free(void *res)
984fb51601bSUwe Kleine-König {
985fb51601bSUwe Kleine-König 	struct spi_res *sres = container_of(res, struct spi_res, data);
986fb51601bSUwe Kleine-König 
987fb51601bSUwe Kleine-König 	if (!res)
988fb51601bSUwe Kleine-König 		return;
989fb51601bSUwe Kleine-König 
990fb51601bSUwe Kleine-König 	WARN_ON(!list_empty(&sres->entry));
991fb51601bSUwe Kleine-König 	kfree(sres);
992fb51601bSUwe Kleine-König }
993fb51601bSUwe Kleine-König 
994fb51601bSUwe Kleine-König /**
995fb51601bSUwe Kleine-König  * spi_res_add - add a spi_res to the spi_message
996702ca026SAndy Shevchenko  * @message: the SPI message
997fb51601bSUwe Kleine-König  * @res:     the spi_resource
998fb51601bSUwe Kleine-König  */
999da21fde0SUwe Kleine-König static void spi_res_add(struct spi_message *message, void *res)
1000fb51601bSUwe Kleine-König {
1001fb51601bSUwe Kleine-König 	struct spi_res *sres = container_of(res, struct spi_res, data);
1002fb51601bSUwe Kleine-König 
1003fb51601bSUwe Kleine-König 	WARN_ON(!list_empty(&sres->entry));
1004fb51601bSUwe Kleine-König 	list_add_tail(&sres->entry, &message->resources);
1005fb51601bSUwe Kleine-König }
1006fb51601bSUwe Kleine-König 
1007fb51601bSUwe Kleine-König /**
1008702ca026SAndy Shevchenko  * spi_res_release - release all SPI resources for this message
1009fb51601bSUwe Kleine-König  * @ctlr:  the @spi_controller
1010fb51601bSUwe Kleine-König  * @message: the @spi_message
1011fb51601bSUwe Kleine-König  */
1012da21fde0SUwe Kleine-König static void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
1013fb51601bSUwe Kleine-König {
1014fb51601bSUwe Kleine-König 	struct spi_res *res, *tmp;
1015fb51601bSUwe Kleine-König 
1016fb51601bSUwe Kleine-König 	list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
1017fb51601bSUwe Kleine-König 		if (res->release)
1018fb51601bSUwe Kleine-König 			res->release(ctlr, message, res->data);
1019fb51601bSUwe Kleine-König 
1020fb51601bSUwe Kleine-König 		list_del(&res->entry);
1021fb51601bSUwe Kleine-König 
1022fb51601bSUwe Kleine-König 		kfree(res);
1023fb51601bSUwe Kleine-König 	}
1024fb51601bSUwe Kleine-König }
1025fb51601bSUwe Kleine-König 
1026fb51601bSUwe Kleine-König /*-------------------------------------------------------------------------*/
1027d707530bSAndy Shevchenko #define spi_for_each_valid_cs(spi, idx)				\
1028d707530bSAndy Shevchenko 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)		\
1029d707530bSAndy Shevchenko 		if (!(spi->cs_index_mask & BIT(idx))) {} else
1030d707530bSAndy Shevchenko 
10314d8ff6b0SAmit Kumar Mahapatra static inline bool spi_is_last_cs(struct spi_device *spi)
10324d8ff6b0SAmit Kumar Mahapatra {
10334d8ff6b0SAmit Kumar Mahapatra 	u8 idx;
10344d8ff6b0SAmit Kumar Mahapatra 	bool last = false;
10354d8ff6b0SAmit Kumar Mahapatra 
1036d707530bSAndy Shevchenko 	spi_for_each_valid_cs(spi, idx) {
10374d8ff6b0SAmit Kumar Mahapatra 		if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx))
10384d8ff6b0SAmit Kumar Mahapatra 			last = true;
10394d8ff6b0SAmit Kumar Mahapatra 	}
10404d8ff6b0SAmit Kumar Mahapatra 	return last;
10414d8ff6b0SAmit Kumar Mahapatra }
10424d8ff6b0SAmit Kumar Mahapatra 
1043e81582c0SAndy Shevchenko static void spi_toggle_csgpiod(struct spi_device *spi, u8 idx, bool enable, bool activate)
1044e81582c0SAndy Shevchenko {
1045e81582c0SAndy Shevchenko 	/*
1046e81582c0SAndy Shevchenko 	 * Historically ACPI has no means of the GPIO polarity and
1047e81582c0SAndy Shevchenko 	 * thus the SPISerialBus() resource defines it on the per-chip
1048e81582c0SAndy Shevchenko 	 * basis. In order to avoid a chain of negations, the GPIO
1049e81582c0SAndy Shevchenko 	 * polarity is considered being Active High. Even for the cases
1050e81582c0SAndy Shevchenko 	 * when _DSD() is involved (in the updated versions of ACPI)
1051e81582c0SAndy Shevchenko 	 * the GPIO CS polarity must be defined Active High to avoid
1052e81582c0SAndy Shevchenko 	 * ambiguity. That's why we use enable, that takes SPI_CS_HIGH
1053e81582c0SAndy Shevchenko 	 * into account.
1054e81582c0SAndy Shevchenko 	 */
1055e81582c0SAndy Shevchenko 	if (has_acpi_companion(&spi->dev))
1056e81582c0SAndy Shevchenko 		gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), !enable);
1057e81582c0SAndy Shevchenko 	else
1058e81582c0SAndy Shevchenko 		/* Polarity handled by GPIO library */
1059e81582c0SAndy Shevchenko 		gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), activate);
1060e81582c0SAndy Shevchenko 
1061e81582c0SAndy Shevchenko 	if (activate)
1062e81582c0SAndy Shevchenko 		spi_delay_exec(&spi->cs_setup, NULL);
1063e81582c0SAndy Shevchenko 	else
1064e81582c0SAndy Shevchenko 		spi_delay_exec(&spi->cs_inactive, NULL);
1065e81582c0SAndy Shevchenko }
1066fb51601bSUwe Kleine-König 
1067d347b4aaSDavid Bauer static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
1068b158935fSMark Brown {
106986527bcbSAndy Shevchenko 	bool activate = enable;
10704d8ff6b0SAmit Kumar Mahapatra 	u8 idx;
107125093bdeSAlexandru Ardelean 
1072d40f0b6fSDouglas Anderson 	/*
1073d40f0b6fSDouglas Anderson 	 * Avoid calling into the driver (or doing delays) if the chip select
1074d40f0b6fSDouglas Anderson 	 * isn't actually changing from the last time this was called.
1075d40f0b6fSDouglas Anderson 	 */
10764d8ff6b0SAmit Kumar Mahapatra 	if (!force && ((enable && spi->controller->last_cs_index_mask == spi->cs_index_mask &&
10774d8ff6b0SAmit Kumar Mahapatra 			spi_is_last_cs(spi)) ||
10784d8ff6b0SAmit Kumar Mahapatra 		       (!enable && spi->controller->last_cs_index_mask == spi->cs_index_mask &&
10794d8ff6b0SAmit Kumar Mahapatra 			!spi_is_last_cs(spi))) &&
1080d40f0b6fSDouglas Anderson 	    (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
1081d40f0b6fSDouglas Anderson 		return;
1082d40f0b6fSDouglas Anderson 
10835cb4e1f3SAndy Shevchenko 	trace_spi_set_cs(spi, activate);
10845cb4e1f3SAndy Shevchenko 
10854d8ff6b0SAmit Kumar Mahapatra 	spi->controller->last_cs_index_mask = spi->cs_index_mask;
10864d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
1087be84be4aSAndy Shevchenko 		spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : SPI_INVALID_CS;
1088d40f0b6fSDouglas Anderson 	spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
1089d40f0b6fSDouglas Anderson 
1090b158935fSMark Brown 	if (spi->mode & SPI_CS_HIGH)
1091b158935fSMark Brown 		enable = !enable;
1092b158935fSMark Brown 
1093aa0162dcSJanne Grunau 	/*
1094aa0162dcSJanne Grunau 	 * Handle chip select delays for GPIO based CS or controllers without
1095aa0162dcSJanne Grunau 	 * programmable chip select timing.
1096aa0162dcSJanne Grunau 	 */
1097aa0162dcSJanne Grunau 	if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate)
10984d8ff6b0SAmit Kumar Mahapatra 		spi_delay_exec(&spi->cs_hold, NULL);
10994d8ff6b0SAmit Kumar Mahapatra 
1100aa0162dcSJanne Grunau 	if (spi_is_csgpiod(spi)) {
1101f48dc6b9SLinus Walleij 		if (!(spi->mode & SPI_NO_CS)) {
1102d707530bSAndy Shevchenko 			spi_for_each_valid_cs(spi, idx) {
1103d707530bSAndy Shevchenko 				if (spi_get_csgpiod(spi, idx))
1104e81582c0SAndy Shevchenko 					spi_toggle_csgpiod(spi, idx, enable, activate);
11054d8ff6b0SAmit Kumar Mahapatra 			}
11066b695469SAndy Shevchenko 		}
11078eee6b9dSThor Thayer 		/* Some SPI masters need both GPIO CS & slave_select */
110882238d2cSAndy Shevchenko 		if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) &&
11098caab75fSGeert Uytterhoeven 		    spi->controller->set_cs)
11108caab75fSGeert Uytterhoeven 			spi->controller->set_cs(spi, !enable);
1111aa0162dcSJanne Grunau 	} else if (spi->controller->set_cs) {
1112aa0162dcSJanne Grunau 		spi->controller->set_cs(spi, !enable);
1113aa0162dcSJanne Grunau 	}
111425093bdeSAlexandru Ardelean 
1115aa0162dcSJanne Grunau 	if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) {
111695c07247SHector Martin 		if (activate)
111795c07247SHector Martin 			spi_delay_exec(&spi->cs_setup, NULL);
111895c07247SHector Martin 		else
11198c33ebfeSMason Zhang 			spi_delay_exec(&spi->cs_inactive, NULL);
112025093bdeSAlexandru Ardelean 	}
1121b158935fSMark Brown }
1122b158935fSMark Brown 
11232de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
11240c17ba73SVincent Whitchurch static int spi_map_buf_attrs(struct spi_controller *ctlr, struct device *dev,
11256ad45a27SMark Brown 			     struct sg_table *sgt, void *buf, size_t len,
11260c17ba73SVincent Whitchurch 			     enum dma_data_direction dir, unsigned long attrs)
11276ad45a27SMark Brown {
11286ad45a27SMark Brown 	const bool vmalloced_buf = is_vmalloc_addr(buf);
1129df88e91bSAndy Shevchenko 	unsigned int max_seg_size = dma_get_max_seg_size(dev);
1130b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM
1131b1b8153cSVignesh R 	const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
1132b1b8153cSVignesh R 				(unsigned long)buf < (PKMAP_BASE +
1133b1b8153cSVignesh R 					(LAST_PKMAP * PAGE_SIZE)));
1134b1b8153cSVignesh R #else
1135b1b8153cSVignesh R 	const bool kmap_buf = false;
1136b1b8153cSVignesh R #endif
113765598c13SAndrew Gabbasov 	int desc_len;
113865598c13SAndrew Gabbasov 	int sgs;
11396ad45a27SMark Brown 	struct page *vm_page;
11408dd4a016SJuan Gutierrez 	struct scatterlist *sg;
11416ad45a27SMark Brown 	void *sg_buf;
11426ad45a27SMark Brown 	size_t min;
11436ad45a27SMark Brown 	int i, ret;
11446ad45a27SMark Brown 
1145b1b8153cSVignesh R 	if (vmalloced_buf || kmap_buf) {
1146ebc4cb43SBiju Das 		desc_len = min_t(unsigned long, max_seg_size, PAGE_SIZE);
114765598c13SAndrew Gabbasov 		sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
11480569a88fSVignesh R 	} else if (virt_addr_valid(buf)) {
1149ebc4cb43SBiju Das 		desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len);
115065598c13SAndrew Gabbasov 		sgs = DIV_ROUND_UP(len, desc_len);
11510569a88fSVignesh R 	} else {
11520569a88fSVignesh R 		return -EINVAL;
115365598c13SAndrew Gabbasov 	}
115465598c13SAndrew Gabbasov 
11556ad45a27SMark Brown 	ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
11566ad45a27SMark Brown 	if (ret != 0)
11576ad45a27SMark Brown 		return ret;
11586ad45a27SMark Brown 
11598dd4a016SJuan Gutierrez 	sg = &sgt->sgl[0];
11606ad45a27SMark Brown 	for (i = 0; i < sgs; i++) {
11616ad45a27SMark Brown 
1162b1b8153cSVignesh R 		if (vmalloced_buf || kmap_buf) {
1163ce99319aSMaxime Chevallier 			/*
1164ce99319aSMaxime Chevallier 			 * Next scatterlist entry size is the minimum between
1165ce99319aSMaxime Chevallier 			 * the desc_len and the remaining buffer length that
1166ce99319aSMaxime Chevallier 			 * fits in a page.
1167ce99319aSMaxime Chevallier 			 */
1168ce99319aSMaxime Chevallier 			min = min_t(size_t, desc_len,
1169ce99319aSMaxime Chevallier 				    min_t(size_t, len,
1170ce99319aSMaxime Chevallier 					  PAGE_SIZE - offset_in_page(buf)));
1171b1b8153cSVignesh R 			if (vmalloced_buf)
11726ad45a27SMark Brown 				vm_page = vmalloc_to_page(buf);
1173b1b8153cSVignesh R 			else
1174b1b8153cSVignesh R 				vm_page = kmap_to_page(buf);
11756ad45a27SMark Brown 			if (!vm_page) {
11766ad45a27SMark Brown 				sg_free_table(sgt);
11776ad45a27SMark Brown 				return -ENOMEM;
11786ad45a27SMark Brown 			}
11798dd4a016SJuan Gutierrez 			sg_set_page(sg, vm_page,
1180c1aefbddSCharles Keepax 				    min, offset_in_page(buf));
11816ad45a27SMark Brown 		} else {
118265598c13SAndrew Gabbasov 			min = min_t(size_t, len, desc_len);
11836ad45a27SMark Brown 			sg_buf = buf;
11848dd4a016SJuan Gutierrez 			sg_set_buf(sg, sg_buf, min);
11856ad45a27SMark Brown 		}
11866ad45a27SMark Brown 
11876ad45a27SMark Brown 		buf += min;
11886ad45a27SMark Brown 		len -= min;
11898dd4a016SJuan Gutierrez 		sg = sg_next(sg);
11906ad45a27SMark Brown 	}
11916ad45a27SMark Brown 
11920c17ba73SVincent Whitchurch 	ret = dma_map_sgtable(dev, sgt, dir, attrs);
11936ad45a27SMark Brown 	if (ret < 0) {
11946ad45a27SMark Brown 		sg_free_table(sgt);
11956ad45a27SMark Brown 		return ret;
11966ad45a27SMark Brown 	}
11976ad45a27SMark Brown 
11986ad45a27SMark Brown 	return 0;
11996ad45a27SMark Brown }
12006ad45a27SMark Brown 
12010c17ba73SVincent Whitchurch int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
12020c17ba73SVincent Whitchurch 		struct sg_table *sgt, void *buf, size_t len,
12030c17ba73SVincent Whitchurch 		enum dma_data_direction dir)
12040c17ba73SVincent Whitchurch {
12050c17ba73SVincent Whitchurch 	return spi_map_buf_attrs(ctlr, dev, sgt, buf, len, dir, 0);
12060c17ba73SVincent Whitchurch }
12070c17ba73SVincent Whitchurch 
12080c17ba73SVincent Whitchurch static void spi_unmap_buf_attrs(struct spi_controller *ctlr,
12090c17ba73SVincent Whitchurch 				struct device *dev, struct sg_table *sgt,
12100c17ba73SVincent Whitchurch 				enum dma_data_direction dir,
12110c17ba73SVincent Whitchurch 				unsigned long attrs)
12120c17ba73SVincent Whitchurch {
12130c17ba73SVincent Whitchurch 	dma_unmap_sgtable(dev, sgt, dir, attrs);
12140c17ba73SVincent Whitchurch 	sg_free_table(sgt);
12158e9204cdSMarek Szyprowski 	sgt->orig_nents = 0;
12168e9204cdSMarek Szyprowski 	sgt->nents = 0;
12170c17ba73SVincent Whitchurch }
12180c17ba73SVincent Whitchurch 
121946336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
12206ad45a27SMark Brown 		   struct sg_table *sgt, enum dma_data_direction dir)
12216ad45a27SMark Brown {
12220c17ba73SVincent Whitchurch 	spi_unmap_buf_attrs(ctlr, dev, sgt, dir, 0);
12236ad45a27SMark Brown }
12246ad45a27SMark Brown 
12258caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
122699adef31SMark Brown {
122799adef31SMark Brown 	struct device *tx_dev, *rx_dev;
122899adef31SMark Brown 	struct spi_transfer *xfer;
12296ad45a27SMark Brown 	int ret;
12303a2eba9bSMark Brown 
12318caab75fSGeert Uytterhoeven 	if (!ctlr->can_dma)
123299adef31SMark Brown 		return 0;
123399adef31SMark Brown 
12348caab75fSGeert Uytterhoeven 	if (ctlr->dma_tx)
12358caab75fSGeert Uytterhoeven 		tx_dev = ctlr->dma_tx->device->dev;
1236b470e10eSVinod Koul 	else if (ctlr->dma_map_dev)
1237b470e10eSVinod Koul 		tx_dev = ctlr->dma_map_dev;
1238c37f45b5SLeilk Liu 	else
12398caab75fSGeert Uytterhoeven 		tx_dev = ctlr->dev.parent;
1240c37f45b5SLeilk Liu 
12418caab75fSGeert Uytterhoeven 	if (ctlr->dma_rx)
12428caab75fSGeert Uytterhoeven 		rx_dev = ctlr->dma_rx->device->dev;
1243b470e10eSVinod Koul 	else if (ctlr->dma_map_dev)
1244b470e10eSVinod Koul 		rx_dev = ctlr->dma_map_dev;
1245c37f45b5SLeilk Liu 	else
12468caab75fSGeert Uytterhoeven 		rx_dev = ctlr->dev.parent;
124799adef31SMark Brown 
12489f788ba4SAndy Shevchenko 	ret = -ENOMSG;
124999adef31SMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
12500c17ba73SVincent Whitchurch 		/* The sync is done before each transfer. */
12510c17ba73SVincent Whitchurch 		unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
12520c17ba73SVincent Whitchurch 
12538caab75fSGeert Uytterhoeven 		if (!ctlr->can_dma(ctlr, msg->spi, xfer))
125499adef31SMark Brown 			continue;
125599adef31SMark Brown 
125699adef31SMark Brown 		if (xfer->tx_buf != NULL) {
12570c17ba73SVincent Whitchurch 			ret = spi_map_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
12580c17ba73SVincent Whitchurch 						(void *)xfer->tx_buf,
12590c17ba73SVincent Whitchurch 						xfer->len, DMA_TO_DEVICE,
12600c17ba73SVincent Whitchurch 						attrs);
12616ad45a27SMark Brown 			if (ret != 0)
12626ad45a27SMark Brown 				return ret;
1263e289df82SAndy Shevchenko 
1264e289df82SAndy Shevchenko 			xfer->tx_sg_mapped = true;
126599adef31SMark Brown 		}
126699adef31SMark Brown 
126799adef31SMark Brown 		if (xfer->rx_buf != NULL) {
12680c17ba73SVincent Whitchurch 			ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
126999adef31SMark Brown 						xfer->rx_buf, xfer->len,
12700c17ba73SVincent Whitchurch 						DMA_FROM_DEVICE, attrs);
12716ad45a27SMark Brown 			if (ret != 0) {
12720c17ba73SVincent Whitchurch 				spi_unmap_buf_attrs(ctlr, tx_dev,
12730c17ba73SVincent Whitchurch 						&xfer->tx_sg, DMA_TO_DEVICE,
12740c17ba73SVincent Whitchurch 						attrs);
12750c17ba73SVincent Whitchurch 
12766ad45a27SMark Brown 				return ret;
127799adef31SMark Brown 			}
1278e289df82SAndy Shevchenko 
1279e289df82SAndy Shevchenko 			xfer->rx_sg_mapped = true;
128099adef31SMark Brown 		}
128199adef31SMark Brown 	}
12829f788ba4SAndy Shevchenko 	/* No transfer has been mapped, bail out with success */
12839f788ba4SAndy Shevchenko 	if (ret)
12849f788ba4SAndy Shevchenko 		return 0;
128599adef31SMark Brown 
1286f25723dcSVincent Whitchurch 	ctlr->cur_rx_dma_dev = rx_dev;
1287f25723dcSVincent Whitchurch 	ctlr->cur_tx_dma_dev = tx_dev;
128899adef31SMark Brown 
128999adef31SMark Brown 	return 0;
129099adef31SMark Brown }
129199adef31SMark Brown 
12928caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
129399adef31SMark Brown {
1294f25723dcSVincent Whitchurch 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
1295f25723dcSVincent Whitchurch 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
129699adef31SMark Brown 	struct spi_transfer *xfer;
129799adef31SMark Brown 
129899adef31SMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
12990c17ba73SVincent Whitchurch 		/* The sync has already been done after each transfer. */
13000c17ba73SVincent Whitchurch 		unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
13010c17ba73SVincent Whitchurch 
1302e289df82SAndy Shevchenko 		if (xfer->rx_sg_mapped)
13030c17ba73SVincent Whitchurch 			spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
13040c17ba73SVincent Whitchurch 					    DMA_FROM_DEVICE, attrs);
1305e289df82SAndy Shevchenko 		xfer->rx_sg_mapped = false;
1306e289df82SAndy Shevchenko 
1307e289df82SAndy Shevchenko 		if (xfer->tx_sg_mapped)
13080c17ba73SVincent Whitchurch 			spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
13090c17ba73SVincent Whitchurch 					    DMA_TO_DEVICE, attrs);
1310e289df82SAndy Shevchenko 		xfer->tx_sg_mapped = false;
131199adef31SMark Brown 	}
131299adef31SMark Brown 
131399adef31SMark Brown 	return 0;
131499adef31SMark Brown }
13150c17ba73SVincent Whitchurch 
1316e289df82SAndy Shevchenko static void spi_dma_sync_for_device(struct spi_controller *ctlr,
13170c17ba73SVincent Whitchurch 				    struct spi_transfer *xfer)
13180c17ba73SVincent Whitchurch {
13190c17ba73SVincent Whitchurch 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
13200c17ba73SVincent Whitchurch 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
13210c17ba73SVincent Whitchurch 
1322e289df82SAndy Shevchenko 	if (xfer->tx_sg_mapped)
13230c17ba73SVincent Whitchurch 		dma_sync_sgtable_for_device(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
1324e289df82SAndy Shevchenko 	if (xfer->rx_sg_mapped)
13250c17ba73SVincent Whitchurch 		dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
13260c17ba73SVincent Whitchurch }
13270c17ba73SVincent Whitchurch 
1328e289df82SAndy Shevchenko static void spi_dma_sync_for_cpu(struct spi_controller *ctlr,
13290c17ba73SVincent Whitchurch 				 struct spi_transfer *xfer)
13300c17ba73SVincent Whitchurch {
13310c17ba73SVincent Whitchurch 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
13320c17ba73SVincent Whitchurch 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
13330c17ba73SVincent Whitchurch 
1334e289df82SAndy Shevchenko 	if (xfer->rx_sg_mapped)
13350c17ba73SVincent Whitchurch 		dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1336e289df82SAndy Shevchenko 	if (xfer->tx_sg_mapped)
13370c17ba73SVincent Whitchurch 		dma_sync_sgtable_for_cpu(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
13380c17ba73SVincent Whitchurch }
13392de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
13408caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr,
13412de440f5SGeert Uytterhoeven 				struct spi_message *msg)
13422de440f5SGeert Uytterhoeven {
13432de440f5SGeert Uytterhoeven 	return 0;
13442de440f5SGeert Uytterhoeven }
13452de440f5SGeert Uytterhoeven 
13468caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr,
13472de440f5SGeert Uytterhoeven 				  struct spi_message *msg)
13482de440f5SGeert Uytterhoeven {
13492de440f5SGeert Uytterhoeven 	return 0;
13502de440f5SGeert Uytterhoeven }
13510c17ba73SVincent Whitchurch 
13520c17ba73SVincent Whitchurch static void spi_dma_sync_for_device(struct spi_controller *ctrl,
13530c17ba73SVincent Whitchurch 				    struct spi_transfer *xfer)
13540c17ba73SVincent Whitchurch {
13550c17ba73SVincent Whitchurch }
13560c17ba73SVincent Whitchurch 
13570c17ba73SVincent Whitchurch static void spi_dma_sync_for_cpu(struct spi_controller *ctrl,
13580c17ba73SVincent Whitchurch 				 struct spi_transfer *xfer)
13590c17ba73SVincent Whitchurch {
13600c17ba73SVincent Whitchurch }
13612de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
13622de440f5SGeert Uytterhoeven 
13638caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr,
13644b786458SMartin Sperl 				struct spi_message *msg)
13654b786458SMartin Sperl {
13664b786458SMartin Sperl 	struct spi_transfer *xfer;
13674b786458SMartin Sperl 
13684b786458SMartin Sperl 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
13694b786458SMartin Sperl 		/*
13704b786458SMartin Sperl 		 * Restore the original value of tx_buf or rx_buf if they are
13714b786458SMartin Sperl 		 * NULL.
13724b786458SMartin Sperl 		 */
13738caab75fSGeert Uytterhoeven 		if (xfer->tx_buf == ctlr->dummy_tx)
13744b786458SMartin Sperl 			xfer->tx_buf = NULL;
13758caab75fSGeert Uytterhoeven 		if (xfer->rx_buf == ctlr->dummy_rx)
13764b786458SMartin Sperl 			xfer->rx_buf = NULL;
13774b786458SMartin Sperl 	}
13784b786458SMartin Sperl 
13798caab75fSGeert Uytterhoeven 	return __spi_unmap_msg(ctlr, msg);
13804b786458SMartin Sperl }
13814b786458SMartin Sperl 
13828caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
13832de440f5SGeert Uytterhoeven {
13842de440f5SGeert Uytterhoeven 	struct spi_transfer *xfer;
13852de440f5SGeert Uytterhoeven 	void *tmp;
13862de440f5SGeert Uytterhoeven 	unsigned int max_tx, max_rx;
13872de440f5SGeert Uytterhoeven 
1388aee67fe8Sdillon min 	if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX))
1389aee67fe8Sdillon min 		&& !(msg->spi->mode & SPI_3WIRE)) {
13902de440f5SGeert Uytterhoeven 		max_tx = 0;
13912de440f5SGeert Uytterhoeven 		max_rx = 0;
13922de440f5SGeert Uytterhoeven 
13932de440f5SGeert Uytterhoeven 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
13948caab75fSGeert Uytterhoeven 			if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
13952de440f5SGeert Uytterhoeven 			    !xfer->tx_buf)
13962de440f5SGeert Uytterhoeven 				max_tx = max(xfer->len, max_tx);
13978caab75fSGeert Uytterhoeven 			if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
13982de440f5SGeert Uytterhoeven 			    !xfer->rx_buf)
13992de440f5SGeert Uytterhoeven 				max_rx = max(xfer->len, max_rx);
14002de440f5SGeert Uytterhoeven 		}
14012de440f5SGeert Uytterhoeven 
14022de440f5SGeert Uytterhoeven 		if (max_tx) {
14038caab75fSGeert Uytterhoeven 			tmp = krealloc(ctlr->dummy_tx, max_tx,
1404b00bab9dSAndy Shevchenko 				       GFP_KERNEL | GFP_DMA | __GFP_ZERO);
14052de440f5SGeert Uytterhoeven 			if (!tmp)
14062de440f5SGeert Uytterhoeven 				return -ENOMEM;
14078caab75fSGeert Uytterhoeven 			ctlr->dummy_tx = tmp;
14082de440f5SGeert Uytterhoeven 		}
14092de440f5SGeert Uytterhoeven 
14102de440f5SGeert Uytterhoeven 		if (max_rx) {
14118caab75fSGeert Uytterhoeven 			tmp = krealloc(ctlr->dummy_rx, max_rx,
14122de440f5SGeert Uytterhoeven 				       GFP_KERNEL | GFP_DMA);
14132de440f5SGeert Uytterhoeven 			if (!tmp)
14142de440f5SGeert Uytterhoeven 				return -ENOMEM;
14158caab75fSGeert Uytterhoeven 			ctlr->dummy_rx = tmp;
14162de440f5SGeert Uytterhoeven 		}
14172de440f5SGeert Uytterhoeven 
14182de440f5SGeert Uytterhoeven 		if (max_tx || max_rx) {
14192de440f5SGeert Uytterhoeven 			list_for_each_entry(xfer, &msg->transfers,
14202de440f5SGeert Uytterhoeven 					    transfer_list) {
14215442dcaaSChris Lesiak 				if (!xfer->len)
14225442dcaaSChris Lesiak 					continue;
14232de440f5SGeert Uytterhoeven 				if (!xfer->tx_buf)
14248caab75fSGeert Uytterhoeven 					xfer->tx_buf = ctlr->dummy_tx;
14252de440f5SGeert Uytterhoeven 				if (!xfer->rx_buf)
14268caab75fSGeert Uytterhoeven 					xfer->rx_buf = ctlr->dummy_rx;
14272de440f5SGeert Uytterhoeven 			}
14282de440f5SGeert Uytterhoeven 		}
14292de440f5SGeert Uytterhoeven 	}
14302de440f5SGeert Uytterhoeven 
14318caab75fSGeert Uytterhoeven 	return __spi_map_msg(ctlr, msg);
14322de440f5SGeert Uytterhoeven }
143399adef31SMark Brown 
1434810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr,
1435810923f3SLubomir Rintel 			     struct spi_message *msg,
1436810923f3SLubomir Rintel 			     struct spi_transfer *xfer)
1437810923f3SLubomir Rintel {
1438d501cc4cSDavid Jander 	struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1439d501cc4cSDavid Jander 	struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
14406170d077SXu Yilun 	u32 speed_hz = xfer->speed_hz;
144149686df5SColin Ian King 	unsigned long long ms;
1442810923f3SLubomir Rintel 
1443810923f3SLubomir Rintel 	if (spi_controller_is_slave(ctlr)) {
1444810923f3SLubomir Rintel 		if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1445810923f3SLubomir Rintel 			dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1446810923f3SLubomir Rintel 			return -EINTR;
1447810923f3SLubomir Rintel 		}
1448810923f3SLubomir Rintel 	} else {
14496170d077SXu Yilun 		if (!speed_hz)
14506170d077SXu Yilun 			speed_hz = 100000;
14516170d077SXu Yilun 
145286b8bff7SAndy Shevchenko 		/*
145386b8bff7SAndy Shevchenko 		 * For each byte we wait for 8 cycles of the SPI clock.
145486b8bff7SAndy Shevchenko 		 * Since speed is defined in Hz and we want milliseconds,
145586b8bff7SAndy Shevchenko 		 * use respective multiplier, but before the division,
145686b8bff7SAndy Shevchenko 		 * otherwise we may get 0 for short transfers.
145786b8bff7SAndy Shevchenko 		 */
145886b8bff7SAndy Shevchenko 		ms = 8LL * MSEC_PER_SEC * xfer->len;
14596170d077SXu Yilun 		do_div(ms, speed_hz);
1460810923f3SLubomir Rintel 
146186b8bff7SAndy Shevchenko 		/*
146286b8bff7SAndy Shevchenko 		 * Increase it twice and add 200 ms tolerance, use
146386b8bff7SAndy Shevchenko 		 * predefined maximum in case of overflow.
146486b8bff7SAndy Shevchenko 		 */
146586b8bff7SAndy Shevchenko 		ms += ms + 200;
1466810923f3SLubomir Rintel 		if (ms > UINT_MAX)
1467810923f3SLubomir Rintel 			ms = UINT_MAX;
1468810923f3SLubomir Rintel 
1469810923f3SLubomir Rintel 		ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1470810923f3SLubomir Rintel 						 msecs_to_jiffies(ms));
1471810923f3SLubomir Rintel 
1472810923f3SLubomir Rintel 		if (ms == 0) {
1473810923f3SLubomir Rintel 			SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1474810923f3SLubomir Rintel 			SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1475810923f3SLubomir Rintel 			dev_err(&msg->spi->dev,
1476810923f3SLubomir Rintel 				"SPI transfer timed out\n");
1477810923f3SLubomir Rintel 			return -ETIMEDOUT;
1478810923f3SLubomir Rintel 		}
147939cefd85SNam Cao 
148039cefd85SNam Cao 		if (xfer->error & SPI_TRANS_FAIL_IO)
148139cefd85SNam Cao 			return -EIO;
1482810923f3SLubomir Rintel 	}
1483810923f3SLubomir Rintel 
1484810923f3SLubomir Rintel 	return 0;
1485810923f3SLubomir Rintel }
1486810923f3SLubomir Rintel 
14870ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns)
14880ff2de8bSMartin Sperl {
14890ff2de8bSMartin Sperl 	if (!ns)
14900ff2de8bSMartin Sperl 		return;
149186b8bff7SAndy Shevchenko 	if (ns <= NSEC_PER_USEC) {
14920ff2de8bSMartin Sperl 		ndelay(ns);
14930ff2de8bSMartin Sperl 	} else {
149486b8bff7SAndy Shevchenko 		u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
14950ff2de8bSMartin Sperl 
14960ff2de8bSMartin Sperl 		if (us <= 10)
14970ff2de8bSMartin Sperl 			udelay(us);
14980ff2de8bSMartin Sperl 		else
14990ff2de8bSMartin Sperl 			usleep_range(us, us + DIV_ROUND_UP(us, 10));
15000ff2de8bSMartin Sperl 	}
15010ff2de8bSMartin Sperl }
15020ff2de8bSMartin Sperl 
15033984d39bSAlexandru Ardelean int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
15040ff2de8bSMartin Sperl {
1505b2c98153SAlexandru Ardelean 	u32 delay = _delay->value;
1506b2c98153SAlexandru Ardelean 	u32 unit = _delay->unit;
1507d5864e5bSMartin Sperl 	u32 hz;
15080ff2de8bSMartin Sperl 
1509b2c98153SAlexandru Ardelean 	if (!delay)
1510b2c98153SAlexandru Ardelean 		return 0;
15110ff2de8bSMartin Sperl 
15120ff2de8bSMartin Sperl 	switch (unit) {
15130ff2de8bSMartin Sperl 	case SPI_DELAY_UNIT_USECS:
151486b8bff7SAndy Shevchenko 		delay *= NSEC_PER_USEC;
15150ff2de8bSMartin Sperl 		break;
151686b8bff7SAndy Shevchenko 	case SPI_DELAY_UNIT_NSECS:
151786b8bff7SAndy Shevchenko 		/* Nothing to do here */
15180ff2de8bSMartin Sperl 		break;
1519d5864e5bSMartin Sperl 	case SPI_DELAY_UNIT_SCK:
152095c8222fSDavid Jander 		/* Clock cycles need to be obtained from spi_transfer */
1521b2c98153SAlexandru Ardelean 		if (!xfer)
1522b2c98153SAlexandru Ardelean 			return -EINVAL;
152386b8bff7SAndy Shevchenko 		/*
152486b8bff7SAndy Shevchenko 		 * If there is unknown effective speed, approximate it
1525702ca026SAndy Shevchenko 		 * by underestimating with half of the requested Hz.
1526d5864e5bSMartin Sperl 		 */
1527d5864e5bSMartin Sperl 		hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1528b2c98153SAlexandru Ardelean 		if (!hz)
1529b2c98153SAlexandru Ardelean 			return -EINVAL;
153086b8bff7SAndy Shevchenko 
153186b8bff7SAndy Shevchenko 		/* Convert delay to nanoseconds */
153286b8bff7SAndy Shevchenko 		delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz);
1533d5864e5bSMartin Sperl 		break;
15340ff2de8bSMartin Sperl 	default:
1535b2c98153SAlexandru Ardelean 		return -EINVAL;
1536b2c98153SAlexandru Ardelean 	}
1537b2c98153SAlexandru Ardelean 
1538b2c98153SAlexandru Ardelean 	return delay;
1539b2c98153SAlexandru Ardelean }
15403984d39bSAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_to_ns);
1541b2c98153SAlexandru Ardelean 
1542b2c98153SAlexandru Ardelean int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1543b2c98153SAlexandru Ardelean {
1544b2c98153SAlexandru Ardelean 	int delay;
1545b2c98153SAlexandru Ardelean 
15468fede89fSMark Brown 	might_sleep();
15478fede89fSMark Brown 
1548b2c98153SAlexandru Ardelean 	if (!_delay)
1549b2c98153SAlexandru Ardelean 		return -EINVAL;
1550b2c98153SAlexandru Ardelean 
15513984d39bSAlexandru Ardelean 	delay = spi_delay_to_ns(_delay, xfer);
1552b2c98153SAlexandru Ardelean 	if (delay < 0)
1553b2c98153SAlexandru Ardelean 		return delay;
1554b2c98153SAlexandru Ardelean 
1555b2c98153SAlexandru Ardelean 	_spi_transfer_delay_ns(delay);
1556b2c98153SAlexandru Ardelean 
1557b2c98153SAlexandru Ardelean 	return 0;
1558b2c98153SAlexandru Ardelean }
1559b2c98153SAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_exec);
1560b2c98153SAlexandru Ardelean 
15610ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg,
15620ff2de8bSMartin Sperl 					  struct spi_transfer *xfer)
15630ff2de8bSMartin Sperl {
156486b8bff7SAndy Shevchenko 	u32 default_delay_ns = 10 * NSEC_PER_USEC;
1565329f0dacSAlexandru Ardelean 	u32 delay = xfer->cs_change_delay.value;
1566329f0dacSAlexandru Ardelean 	u32 unit = xfer->cs_change_delay.unit;
1567329f0dacSAlexandru Ardelean 	int ret;
15680ff2de8bSMartin Sperl 
156995c8222fSDavid Jander 	/* Return early on "fast" mode - for everything but USECS */
15706b3f236aSAlexandru Ardelean 	if (!delay) {
15716b3f236aSAlexandru Ardelean 		if (unit == SPI_DELAY_UNIT_USECS)
157286b8bff7SAndy Shevchenko 			_spi_transfer_delay_ns(default_delay_ns);
15730ff2de8bSMartin Sperl 		return;
15746b3f236aSAlexandru Ardelean 	}
15750ff2de8bSMartin Sperl 
1576329f0dacSAlexandru Ardelean 	ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1577329f0dacSAlexandru Ardelean 	if (ret) {
15780ff2de8bSMartin Sperl 		dev_err_once(&msg->spi->dev,
157986b8bff7SAndy Shevchenko 			     "Use of unsupported delay unit %i, using default of %luus\n",
158086b8bff7SAndy Shevchenko 			     unit, default_delay_ns / NSEC_PER_USEC);
158186b8bff7SAndy Shevchenko 		_spi_transfer_delay_ns(default_delay_ns);
15820ff2de8bSMartin Sperl 	}
15830ff2de8bSMartin Sperl }
15840ff2de8bSMartin Sperl 
15856e80133aSWilliam Zhang void spi_transfer_cs_change_delay_exec(struct spi_message *msg,
15866e80133aSWilliam Zhang 						  struct spi_transfer *xfer)
15876e80133aSWilliam Zhang {
15886e80133aSWilliam Zhang 	_spi_transfer_cs_change_delay(msg, xfer);
15896e80133aSWilliam Zhang }
15906e80133aSWilliam Zhang EXPORT_SYMBOL_GPL(spi_transfer_cs_change_delay_exec);
15916e80133aSWilliam Zhang 
1592b158935fSMark Brown /*
1593b158935fSMark Brown  * spi_transfer_one_message - Default implementation of transfer_one_message()
1594b158935fSMark Brown  *
1595b158935fSMark Brown  * This is a standard implementation of transfer_one_message() for
15968ba811a7SMoritz Fischer  * drivers which implement a transfer_one() operation.  It provides
1597b158935fSMark Brown  * standard handling of delays and chip select management.
1598b158935fSMark Brown  */
15998caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr,
1600b158935fSMark Brown 				    struct spi_message *msg)
1601b158935fSMark Brown {
1602b158935fSMark Brown 	struct spi_transfer *xfer;
1603b158935fSMark Brown 	bool keep_cs = false;
1604b158935fSMark Brown 	int ret = 0;
1605d501cc4cSDavid Jander 	struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1606d501cc4cSDavid Jander 	struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
1607b158935fSMark Brown 
16085e0531f6SChristophe Leroy 	xfer = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list);
16095e0531f6SChristophe Leroy 	spi_set_cs(msg->spi, !xfer->cs_off, false);
1610b158935fSMark Brown 
1611eca2ebc7SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1612eca2ebc7SMartin Sperl 	SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1613eca2ebc7SMartin Sperl 
1614b158935fSMark Brown 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1615b158935fSMark Brown 		trace_spi_transfer_start(msg, xfer);
1616b158935fSMark Brown 
161752267fe8SDavid Lechner 		spi_statistics_add_transfer_stats(statm, xfer, msg);
161852267fe8SDavid Lechner 		spi_statistics_add_transfer_stats(stats, xfer, msg);
1619eca2ebc7SMartin Sperl 
1620b42faeeeSVladimir Oltean 		if (!ctlr->ptp_sts_supported) {
1621b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_pre = 0;
1622b42faeeeSVladimir Oltean 			ptp_read_system_prets(xfer->ptp_sts);
1623b42faeeeSVladimir Oltean 		}
1624b42faeeeSVladimir Oltean 
1625b3063203SNicolas Saenz Julienne 		if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
16268caab75fSGeert Uytterhoeven 			reinit_completion(&ctlr->xfer_completion);
1627b158935fSMark Brown 
1628809b1b04SRobin Gong fallback_pio:
1629e289df82SAndy Shevchenko 			spi_dma_sync_for_device(ctlr, xfer);
16308caab75fSGeert Uytterhoeven 			ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1631b158935fSMark Brown 			if (ret < 0) {
1632e289df82SAndy Shevchenko 				spi_dma_sync_for_cpu(ctlr, xfer);
16330c17ba73SVincent Whitchurch 
1634e289df82SAndy Shevchenko 				if ((xfer->tx_sg_mapped || xfer->rx_sg_mapped) &&
1635809b1b04SRobin Gong 				    (xfer->error & SPI_TRANS_FAIL_NO_START)) {
1636809b1b04SRobin Gong 					__spi_unmap_msg(ctlr, msg);
1637809b1b04SRobin Gong 					ctlr->fallback = true;
1638809b1b04SRobin Gong 					xfer->error &= ~SPI_TRANS_FAIL_NO_START;
1639809b1b04SRobin Gong 					goto fallback_pio;
1640809b1b04SRobin Gong 				}
1641809b1b04SRobin Gong 
1642eca2ebc7SMartin Sperl 				SPI_STATISTICS_INCREMENT_FIELD(statm,
1643eca2ebc7SMartin Sperl 							       errors);
1644eca2ebc7SMartin Sperl 				SPI_STATISTICS_INCREMENT_FIELD(stats,
1645eca2ebc7SMartin Sperl 							       errors);
1646b158935fSMark Brown 				dev_err(&msg->spi->dev,
1647b158935fSMark Brown 					"SPI transfer failed: %d\n", ret);
1648b158935fSMark Brown 				goto out;
1649b158935fSMark Brown 			}
1650b158935fSMark Brown 
1651d57e7960SMark Brown 			if (ret > 0) {
1652810923f3SLubomir Rintel 				ret = spi_transfer_wait(ctlr, msg, xfer);
1653810923f3SLubomir Rintel 				if (ret < 0)
1654810923f3SLubomir Rintel 					msg->status = ret;
1655d57e7960SMark Brown 			}
16560c17ba73SVincent Whitchurch 
1657e289df82SAndy Shevchenko 			spi_dma_sync_for_cpu(ctlr, xfer);
165838ec10f6SMark Brown 		} else {
165938ec10f6SMark Brown 			if (xfer->len)
166038ec10f6SMark Brown 				dev_err(&msg->spi->dev,
166138ec10f6SMark Brown 					"Bufferless transfer has length %u\n",
166238ec10f6SMark Brown 					xfer->len);
166338ec10f6SMark Brown 		}
1664b158935fSMark Brown 
1665b42faeeeSVladimir Oltean 		if (!ctlr->ptp_sts_supported) {
1666b42faeeeSVladimir Oltean 			ptp_read_system_postts(xfer->ptp_sts);
1667b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_post = xfer->len;
1668b42faeeeSVladimir Oltean 		}
1669b42faeeeSVladimir Oltean 
1670b158935fSMark Brown 		trace_spi_transfer_stop(msg, xfer);
1671b158935fSMark Brown 
1672b158935fSMark Brown 		if (msg->status != -EINPROGRESS)
1673b158935fSMark Brown 			goto out;
1674b158935fSMark Brown 
1675bebcfd27SAlexandru Ardelean 		spi_transfer_delay_exec(xfer);
1676b158935fSMark Brown 
1677b158935fSMark Brown 		if (xfer->cs_change) {
1678b158935fSMark Brown 			if (list_is_last(&xfer->transfer_list,
1679b158935fSMark Brown 					 &msg->transfers)) {
1680b158935fSMark Brown 				keep_cs = true;
1681b158935fSMark Brown 			} else {
16825e0531f6SChristophe Leroy 				if (!xfer->cs_off)
1683d347b4aaSDavid Bauer 					spi_set_cs(msg->spi, false, false);
16840ff2de8bSMartin Sperl 				_spi_transfer_cs_change_delay(msg, xfer);
16855e0531f6SChristophe Leroy 				if (!list_next_entry(xfer, transfer_list)->cs_off)
1686d347b4aaSDavid Bauer 					spi_set_cs(msg->spi, true, false);
1687b158935fSMark Brown 			}
16885e0531f6SChristophe Leroy 		} else if (!list_is_last(&xfer->transfer_list, &msg->transfers) &&
16895e0531f6SChristophe Leroy 			   xfer->cs_off != list_next_entry(xfer, transfer_list)->cs_off) {
16905e0531f6SChristophe Leroy 			spi_set_cs(msg->spi, xfer->cs_off, false);
1691b158935fSMark Brown 		}
1692b158935fSMark Brown 
1693b158935fSMark Brown 		msg->actual_length += xfer->len;
1694b158935fSMark Brown 	}
1695b158935fSMark Brown 
1696b158935fSMark Brown out:
1697b158935fSMark Brown 	if (ret != 0 || !keep_cs)
1698d347b4aaSDavid Bauer 		spi_set_cs(msg->spi, false, false);
1699b158935fSMark Brown 
1700b158935fSMark Brown 	if (msg->status == -EINPROGRESS)
1701b158935fSMark Brown 		msg->status = ret;
1702b158935fSMark Brown 
17038caab75fSGeert Uytterhoeven 	if (msg->status && ctlr->handle_err)
17048caab75fSGeert Uytterhoeven 		ctlr->handle_err(ctlr, msg);
1705b716c4ffSAndy Shevchenko 
17060ed56252SMark Brown 	spi_finalize_current_message(ctlr);
17070ed56252SMark Brown 
1708b158935fSMark Brown 	return ret;
1709b158935fSMark Brown }
1710b158935fSMark Brown 
1711b158935fSMark Brown /**
1712b158935fSMark Brown  * spi_finalize_current_transfer - report completion of a transfer
17138caab75fSGeert Uytterhoeven  * @ctlr: the controller reporting completion
1714b158935fSMark Brown  *
1715b158935fSMark Brown  * Called by SPI drivers using the core transfer_one_message()
1716b158935fSMark Brown  * implementation to notify it that the current interrupt driven
17179e8f4882SGeert Uytterhoeven  * transfer has finished and the next one may be scheduled.
1718b158935fSMark Brown  */
17198caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr)
1720b158935fSMark Brown {
17218caab75fSGeert Uytterhoeven 	complete(&ctlr->xfer_completion);
1722b158935fSMark Brown }
1723b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1724b158935fSMark Brown 
1725e1268597SMark Brown static void spi_idle_runtime_pm(struct spi_controller *ctlr)
1726e1268597SMark Brown {
1727e1268597SMark Brown 	if (ctlr->auto_runtime_pm) {
1728e1268597SMark Brown 		pm_runtime_mark_last_busy(ctlr->dev.parent);
1729e1268597SMark Brown 		pm_runtime_put_autosuspend(ctlr->dev.parent);
1730e1268597SMark Brown 	}
1731e1268597SMark Brown }
1732e1268597SMark Brown 
1733ae7d2346SDavid Jander static int __spi_pump_transfer_message(struct spi_controller *ctlr,
1734ae7d2346SDavid Jander 		struct spi_message *msg, bool was_busy)
1735ae7d2346SDavid Jander {
1736ae7d2346SDavid Jander 	struct spi_transfer *xfer;
1737ae7d2346SDavid Jander 	int ret;
1738ae7d2346SDavid Jander 
1739ae7d2346SDavid Jander 	if (!was_busy && ctlr->auto_runtime_pm) {
1740ae7d2346SDavid Jander 		ret = pm_runtime_get_sync(ctlr->dev.parent);
1741ae7d2346SDavid Jander 		if (ret < 0) {
1742ae7d2346SDavid Jander 			pm_runtime_put_noidle(ctlr->dev.parent);
1743ae7d2346SDavid Jander 			dev_err(&ctlr->dev, "Failed to power device: %d\n",
1744ae7d2346SDavid Jander 				ret);
17458c2ae772SDavid Lechner 
17468c2ae772SDavid Lechner 			msg->status = ret;
17478c2ae772SDavid Lechner 			spi_finalize_current_message(ctlr);
17488c2ae772SDavid Lechner 
1749ae7d2346SDavid Jander 			return ret;
1750ae7d2346SDavid Jander 		}
1751ae7d2346SDavid Jander 	}
1752ae7d2346SDavid Jander 
1753ae7d2346SDavid Jander 	if (!was_busy)
1754ae7d2346SDavid Jander 		trace_spi_controller_busy(ctlr);
1755ae7d2346SDavid Jander 
1756ae7d2346SDavid Jander 	if (!was_busy && ctlr->prepare_transfer_hardware) {
1757ae7d2346SDavid Jander 		ret = ctlr->prepare_transfer_hardware(ctlr);
1758ae7d2346SDavid Jander 		if (ret) {
1759ae7d2346SDavid Jander 			dev_err(&ctlr->dev,
1760ae7d2346SDavid Jander 				"failed to prepare transfer hardware: %d\n",
1761ae7d2346SDavid Jander 				ret);
1762ae7d2346SDavid Jander 
1763ae7d2346SDavid Jander 			if (ctlr->auto_runtime_pm)
1764ae7d2346SDavid Jander 				pm_runtime_put(ctlr->dev.parent);
1765ae7d2346SDavid Jander 
1766ae7d2346SDavid Jander 			msg->status = ret;
1767ae7d2346SDavid Jander 			spi_finalize_current_message(ctlr);
1768ae7d2346SDavid Jander 
1769ae7d2346SDavid Jander 			return ret;
1770ae7d2346SDavid Jander 		}
1771ae7d2346SDavid Jander 	}
1772ae7d2346SDavid Jander 
1773ae7d2346SDavid Jander 	trace_spi_message_start(msg);
1774ae7d2346SDavid Jander 
1775ae7d2346SDavid Jander 	if (ctlr->prepare_message) {
1776ae7d2346SDavid Jander 		ret = ctlr->prepare_message(ctlr, msg);
1777ae7d2346SDavid Jander 		if (ret) {
1778ae7d2346SDavid Jander 			dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1779ae7d2346SDavid Jander 				ret);
1780ae7d2346SDavid Jander 			msg->status = ret;
1781ae7d2346SDavid Jander 			spi_finalize_current_message(ctlr);
1782ae7d2346SDavid Jander 			return ret;
1783ae7d2346SDavid Jander 		}
1784ae7d2346SDavid Jander 		msg->prepared = true;
1785ae7d2346SDavid Jander 	}
1786ae7d2346SDavid Jander 
1787ae7d2346SDavid Jander 	ret = spi_map_msg(ctlr, msg);
1788ae7d2346SDavid Jander 	if (ret) {
1789ae7d2346SDavid Jander 		msg->status = ret;
1790ae7d2346SDavid Jander 		spi_finalize_current_message(ctlr);
1791ae7d2346SDavid Jander 		return ret;
1792ae7d2346SDavid Jander 	}
1793ae7d2346SDavid Jander 
1794ae7d2346SDavid Jander 	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1795ae7d2346SDavid Jander 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1796ae7d2346SDavid Jander 			xfer->ptp_sts_word_pre = 0;
1797ae7d2346SDavid Jander 			ptp_read_system_prets(xfer->ptp_sts);
1798ae7d2346SDavid Jander 		}
1799ae7d2346SDavid Jander 	}
1800ae7d2346SDavid Jander 
1801dc302905SDavid Jander 	/*
1802dc302905SDavid Jander 	 * Drivers implementation of transfer_one_message() must arrange for
1803dc302905SDavid Jander 	 * spi_finalize_current_message() to get called. Most drivers will do
1804dc302905SDavid Jander 	 * this in the calling context, but some don't. For those cases, a
1805dc302905SDavid Jander 	 * completion is used to guarantee that this function does not return
1806dc302905SDavid Jander 	 * until spi_finalize_current_message() is done accessing
1807dc302905SDavid Jander 	 * ctlr->cur_msg.
1808dc302905SDavid Jander 	 * Use of the following two flags enable to opportunistically skip the
1809dc302905SDavid Jander 	 * use of the completion since its use involves expensive spin locks.
1810dc302905SDavid Jander 	 * In case of a race with the context that calls
1811dc302905SDavid Jander 	 * spi_finalize_current_message() the completion will always be used,
1812dc302905SDavid Jander 	 * due to strict ordering of these flags using barriers.
1813dc302905SDavid Jander 	 */
1814dc302905SDavid Jander 	WRITE_ONCE(ctlr->cur_msg_incomplete, true);
1815dc302905SDavid Jander 	WRITE_ONCE(ctlr->cur_msg_need_completion, false);
181669fa9590SDavid Jander 	reinit_completion(&ctlr->cur_msg_completion);
181795c8222fSDavid Jander 	smp_wmb(); /* Make these available to spi_finalize_current_message() */
1818dc302905SDavid Jander 
1819ae7d2346SDavid Jander 	ret = ctlr->transfer_one_message(ctlr, msg);
1820ae7d2346SDavid Jander 	if (ret) {
1821ae7d2346SDavid Jander 		dev_err(&ctlr->dev,
1822ae7d2346SDavid Jander 			"failed to transfer one message from queue\n");
1823ae7d2346SDavid Jander 		return ret;
182431d4c1bdSDavid Jander 	}
182531d4c1bdSDavid Jander 
1826dc302905SDavid Jander 	WRITE_ONCE(ctlr->cur_msg_need_completion, true);
182731d4c1bdSDavid Jander 	smp_mb(); /* See spi_finalize_current_message()... */
1828dc302905SDavid Jander 	if (READ_ONCE(ctlr->cur_msg_incomplete))
182969fa9590SDavid Jander 		wait_for_completion(&ctlr->cur_msg_completion);
1830ae7d2346SDavid Jander 
1831ae7d2346SDavid Jander 	return 0;
1832ae7d2346SDavid Jander }
1833ae7d2346SDavid Jander 
1834ffbbdd21SLinus Walleij /**
1835702ca026SAndy Shevchenko  * __spi_pump_messages - function which processes SPI message queue
18368caab75fSGeert Uytterhoeven  * @ctlr: controller to process queue for
1837fc9e0f71SMark Brown  * @in_kthread: true if we are in the context of the message pump thread
1838ffbbdd21SLinus Walleij  *
1839702ca026SAndy Shevchenko  * This function checks if there is any SPI message in the queue that
1840ffbbdd21SLinus Walleij  * needs processing and if so call out to the driver to initialize hardware
1841ffbbdd21SLinus Walleij  * and transfer each message.
1842ffbbdd21SLinus Walleij  *
18430461a414SMark Brown  * Note that it is called both from the kthread itself and also from
18440461a414SMark Brown  * inside spi_sync(); the queue extraction handling at the top of the
18450461a414SMark Brown  * function should deal with this safely.
1846ffbbdd21SLinus Walleij  */
18478caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
1848ffbbdd21SLinus Walleij {
1849d1c44c93SVladimir Oltean 	struct spi_message *msg;
1850ffbbdd21SLinus Walleij 	bool was_busy = false;
1851d1c44c93SVladimir Oltean 	unsigned long flags;
1852ffbbdd21SLinus Walleij 	int ret;
1853ffbbdd21SLinus Walleij 
1854702ca026SAndy Shevchenko 	/* Take the I/O mutex */
1855c1038165SDavid Jander 	mutex_lock(&ctlr->io_mutex);
1856c1038165SDavid Jander 
1857983aee5dSMark Brown 	/* Lock queue */
18588caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
1859983aee5dSMark Brown 
1860983aee5dSMark Brown 	/* Make sure we are not already running a message */
18618711a2abSDavid Jander 	if (ctlr->cur_msg)
1862c1038165SDavid Jander 		goto out_unlock;
1863983aee5dSMark Brown 
1864983aee5dSMark Brown 	/* Check if the queue is idle */
18658caab75fSGeert Uytterhoeven 	if (list_empty(&ctlr->queue) || !ctlr->running) {
18668711a2abSDavid Jander 		if (!ctlr->busy)
1867c1038165SDavid Jander 			goto out_unlock;
1868fc9e0f71SMark Brown 
1869e1268597SMark Brown 		/* Defer any non-atomic teardown to the thread */
1870f0125f1aSMark Brown 		if (!in_kthread) {
1871e1268597SMark Brown 			if (!ctlr->dummy_rx && !ctlr->dummy_tx &&
1872e1268597SMark Brown 			    !ctlr->unprepare_transfer_hardware) {
1873e1268597SMark Brown 				spi_idle_runtime_pm(ctlr);
1874e1268597SMark Brown 				ctlr->busy = false;
1875ae7d2346SDavid Jander 				ctlr->queue_empty = true;
1876e1268597SMark Brown 				trace_spi_controller_idle(ctlr);
1877e1268597SMark Brown 			} else {
187860a883d1SMarek Szyprowski 				kthread_queue_work(ctlr->kworker,
1879f0125f1aSMark Brown 						   &ctlr->pump_messages);
1880e1268597SMark Brown 			}
1881c1038165SDavid Jander 			goto out_unlock;
1882f0125f1aSMark Brown 		}
1883f0125f1aSMark Brown 
1884f0125f1aSMark Brown 		ctlr->busy = false;
1885f0125f1aSMark Brown 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1886f0125f1aSMark Brown 
1887f0125f1aSMark Brown 		kfree(ctlr->dummy_rx);
1888f0125f1aSMark Brown 		ctlr->dummy_rx = NULL;
1889f0125f1aSMark Brown 		kfree(ctlr->dummy_tx);
1890f0125f1aSMark Brown 		ctlr->dummy_tx = NULL;
1891f0125f1aSMark Brown 		if (ctlr->unprepare_transfer_hardware &&
1892f0125f1aSMark Brown 		    ctlr->unprepare_transfer_hardware(ctlr))
1893f0125f1aSMark Brown 			dev_err(&ctlr->dev,
1894f0125f1aSMark Brown 				"failed to unprepare transfer hardware\n");
1895e1268597SMark Brown 		spi_idle_runtime_pm(ctlr);
1896f0125f1aSMark Brown 		trace_spi_controller_idle(ctlr);
1897f0125f1aSMark Brown 
1898f0125f1aSMark Brown 		spin_lock_irqsave(&ctlr->queue_lock, flags);
1899ae7d2346SDavid Jander 		ctlr->queue_empty = true;
1900c1038165SDavid Jander 		goto out_unlock;
1901ffbbdd21SLinus Walleij 	}
1902ffbbdd21SLinus Walleij 
1903ffbbdd21SLinus Walleij 	/* Extract head of queue */
1904d1c44c93SVladimir Oltean 	msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1905d1c44c93SVladimir Oltean 	ctlr->cur_msg = msg;
1906ffbbdd21SLinus Walleij 
1907d1c44c93SVladimir Oltean 	list_del_init(&msg->queue);
19088caab75fSGeert Uytterhoeven 	if (ctlr->busy)
1909ffbbdd21SLinus Walleij 		was_busy = true;
1910ffbbdd21SLinus Walleij 	else
19118caab75fSGeert Uytterhoeven 		ctlr->busy = true;
19128caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1913ffbbdd21SLinus Walleij 
1914ae7d2346SDavid Jander 	ret = __spi_pump_transfer_message(ctlr, msg, was_busy);
191569fa9590SDavid Jander 	kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
1916c191543eSDavid Jander 
191769fa9590SDavid Jander 	ctlr->cur_msg = NULL;
191869fa9590SDavid Jander 	ctlr->fallback = false;
191969fa9590SDavid Jander 
19208caab75fSGeert Uytterhoeven 	mutex_unlock(&ctlr->io_mutex);
192162826970SMark Brown 
192262826970SMark Brown 	/* Prod the scheduler in case transfer_one() was busy waiting */
192349023d2eSJon Hunter 	if (!ret)
192462826970SMark Brown 		cond_resched();
1925c1038165SDavid Jander 	return;
1926c1038165SDavid Jander 
1927c1038165SDavid Jander out_unlock:
19288711a2abSDavid Jander 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1929c1038165SDavid Jander 	mutex_unlock(&ctlr->io_mutex);
1930ffbbdd21SLinus Walleij }
1931ffbbdd21SLinus Walleij 
1932fc9e0f71SMark Brown /**
1933fc9e0f71SMark Brown  * spi_pump_messages - kthread work function which processes spi message queue
19348caab75fSGeert Uytterhoeven  * @work: pointer to kthread work struct contained in the controller struct
1935fc9e0f71SMark Brown  */
1936fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work)
1937fc9e0f71SMark Brown {
19388caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr =
19398caab75fSGeert Uytterhoeven 		container_of(work, struct spi_controller, pump_messages);
1940fc9e0f71SMark Brown 
19418caab75fSGeert Uytterhoeven 	__spi_pump_messages(ctlr, true);
1942fc9e0f71SMark Brown }
1943fc9e0f71SMark Brown 
1944924b5867SDouglas Anderson /**
1945350de7ceSAndy Shevchenko  * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp
1946b42faeeeSVladimir Oltean  * @ctlr: Pointer to the spi_controller structure of the driver
1947b42faeeeSVladimir Oltean  * @xfer: Pointer to the transfer being timestamped
1948862dd2a9SVladimir Oltean  * @progress: How many words (not bytes) have been transferred so far
1949b42faeeeSVladimir Oltean  * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1950b42faeeeSVladimir Oltean  *	      transfer, for less jitter in time measurement. Only compatible
1951b42faeeeSVladimir Oltean  *	      with PIO drivers. If true, must follow up with
1952b42faeeeSVladimir Oltean  *	      spi_take_timestamp_post or otherwise system will crash.
1953b42faeeeSVladimir Oltean  *	      WARNING: for fully predictable results, the CPU frequency must
1954b42faeeeSVladimir Oltean  *	      also be under control (governor).
1955350de7ceSAndy Shevchenko  *
1956350de7ceSAndy Shevchenko  * This is a helper for drivers to collect the beginning of the TX timestamp
1957350de7ceSAndy Shevchenko  * for the requested byte from the SPI transfer. The frequency with which this
1958350de7ceSAndy Shevchenko  * function must be called (once per word, once for the whole transfer, once
1959350de7ceSAndy Shevchenko  * per batch of words etc) is arbitrary as long as the @tx buffer offset is
1960350de7ceSAndy Shevchenko  * greater than or equal to the requested byte at the time of the call. The
1961350de7ceSAndy Shevchenko  * timestamp is only taken once, at the first such call. It is assumed that
1962350de7ceSAndy Shevchenko  * the driver advances its @tx buffer pointer monotonically.
1963b42faeeeSVladimir Oltean  */
1964b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr,
1965b42faeeeSVladimir Oltean 			    struct spi_transfer *xfer,
1966862dd2a9SVladimir Oltean 			    size_t progress, bool irqs_off)
1967b42faeeeSVladimir Oltean {
1968b42faeeeSVladimir Oltean 	if (!xfer->ptp_sts)
1969b42faeeeSVladimir Oltean 		return;
1970b42faeeeSVladimir Oltean 
19716a726824SVladimir Oltean 	if (xfer->timestamped)
1972b42faeeeSVladimir Oltean 		return;
1973b42faeeeSVladimir Oltean 
19746a726824SVladimir Oltean 	if (progress > xfer->ptp_sts_word_pre)
1975b42faeeeSVladimir Oltean 		return;
1976b42faeeeSVladimir Oltean 
1977b42faeeeSVladimir Oltean 	/* Capture the resolution of the timestamp */
1978862dd2a9SVladimir Oltean 	xfer->ptp_sts_word_pre = progress;
1979b42faeeeSVladimir Oltean 
1980b42faeeeSVladimir Oltean 	if (irqs_off) {
1981b42faeeeSVladimir Oltean 		local_irq_save(ctlr->irq_flags);
1982b42faeeeSVladimir Oltean 		preempt_disable();
1983b42faeeeSVladimir Oltean 	}
1984b42faeeeSVladimir Oltean 
1985b42faeeeSVladimir Oltean 	ptp_read_system_prets(xfer->ptp_sts);
1986b42faeeeSVladimir Oltean }
1987b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1988b42faeeeSVladimir Oltean 
1989b42faeeeSVladimir Oltean /**
1990350de7ceSAndy Shevchenko  * spi_take_timestamp_post - helper to collect the end of the TX timestamp
1991b42faeeeSVladimir Oltean  * @ctlr: Pointer to the spi_controller structure of the driver
1992b42faeeeSVladimir Oltean  * @xfer: Pointer to the transfer being timestamped
1993862dd2a9SVladimir Oltean  * @progress: How many words (not bytes) have been transferred so far
1994b42faeeeSVladimir Oltean  * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1995350de7ceSAndy Shevchenko  *
1996350de7ceSAndy Shevchenko  * This is a helper for drivers to collect the end of the TX timestamp for
1997350de7ceSAndy Shevchenko  * the requested byte from the SPI transfer. Can be called with an arbitrary
1998350de7ceSAndy Shevchenko  * frequency: only the first call where @tx exceeds or is equal to the
1999350de7ceSAndy Shevchenko  * requested word will be timestamped.
2000b42faeeeSVladimir Oltean  */
2001b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr,
2002b42faeeeSVladimir Oltean 			     struct spi_transfer *xfer,
2003862dd2a9SVladimir Oltean 			     size_t progress, bool irqs_off)
2004b42faeeeSVladimir Oltean {
2005b42faeeeSVladimir Oltean 	if (!xfer->ptp_sts)
2006b42faeeeSVladimir Oltean 		return;
2007b42faeeeSVladimir Oltean 
20086a726824SVladimir Oltean 	if (xfer->timestamped)
2009b42faeeeSVladimir Oltean 		return;
2010b42faeeeSVladimir Oltean 
2011862dd2a9SVladimir Oltean 	if (progress < xfer->ptp_sts_word_post)
2012b42faeeeSVladimir Oltean 		return;
2013b42faeeeSVladimir Oltean 
2014b42faeeeSVladimir Oltean 	ptp_read_system_postts(xfer->ptp_sts);
2015b42faeeeSVladimir Oltean 
2016b42faeeeSVladimir Oltean 	if (irqs_off) {
2017b42faeeeSVladimir Oltean 		local_irq_restore(ctlr->irq_flags);
2018b42faeeeSVladimir Oltean 		preempt_enable();
2019b42faeeeSVladimir Oltean 	}
2020b42faeeeSVladimir Oltean 
2021b42faeeeSVladimir Oltean 	/* Capture the resolution of the timestamp */
2022862dd2a9SVladimir Oltean 	xfer->ptp_sts_word_post = progress;
2023b42faeeeSVladimir Oltean 
20249d77522bSChristophe JAILLET 	xfer->timestamped = 1;
2025b42faeeeSVladimir Oltean }
2026b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
2027b42faeeeSVladimir Oltean 
2028b42faeeeSVladimir Oltean /**
2029924b5867SDouglas Anderson  * spi_set_thread_rt - set the controller to pump at realtime priority
2030924b5867SDouglas Anderson  * @ctlr: controller to boost priority of
2031924b5867SDouglas Anderson  *
2032924b5867SDouglas Anderson  * This can be called because the controller requested realtime priority
2033924b5867SDouglas Anderson  * (by setting the ->rt value before calling spi_register_controller()) or
2034924b5867SDouglas Anderson  * because a device on the bus said that its transfers needed realtime
2035924b5867SDouglas Anderson  * priority.
2036924b5867SDouglas Anderson  *
2037924b5867SDouglas Anderson  * NOTE: at the moment if any device on a bus says it needs realtime then
2038924b5867SDouglas Anderson  * the thread will be at realtime priority for all transfers on that
2039924b5867SDouglas Anderson  * controller.  If this eventually becomes a problem we may see if we can
2040924b5867SDouglas Anderson  * find a way to boost the priority only temporarily during relevant
2041924b5867SDouglas Anderson  * transfers.
2042924b5867SDouglas Anderson  */
2043924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr)
2044ffbbdd21SLinus Walleij {
2045924b5867SDouglas Anderson 	dev_info(&ctlr->dev,
2046924b5867SDouglas Anderson 		"will run message pump with realtime priority\n");
20476d2b84a4SLinus Torvalds 	sched_set_fifo(ctlr->kworker->task);
2048924b5867SDouglas Anderson }
2049924b5867SDouglas Anderson 
2050924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr)
2051924b5867SDouglas Anderson {
20528caab75fSGeert Uytterhoeven 	ctlr->running = false;
20538caab75fSGeert Uytterhoeven 	ctlr->busy = false;
2054ae7d2346SDavid Jander 	ctlr->queue_empty = true;
2055ffbbdd21SLinus Walleij 
205660a883d1SMarek Szyprowski 	ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev));
205760a883d1SMarek Szyprowski 	if (IS_ERR(ctlr->kworker)) {
205860a883d1SMarek Szyprowski 		dev_err(&ctlr->dev, "failed to create message pump kworker\n");
205960a883d1SMarek Szyprowski 		return PTR_ERR(ctlr->kworker);
2060ffbbdd21SLinus Walleij 	}
206160a883d1SMarek Szyprowski 
20628caab75fSGeert Uytterhoeven 	kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
2063f0125f1aSMark Brown 
2064ffbbdd21SLinus Walleij 	/*
20658caab75fSGeert Uytterhoeven 	 * Controller config will indicate if this controller should run the
2066ffbbdd21SLinus Walleij 	 * message pump with high (realtime) priority to reduce the transfer
2067ffbbdd21SLinus Walleij 	 * latency on the bus by minimising the delay between a transfer
2068ffbbdd21SLinus Walleij 	 * request and the scheduling of the message pump thread. Without this
2069ffbbdd21SLinus Walleij 	 * setting the message pump thread will remain at default priority.
2070ffbbdd21SLinus Walleij 	 */
2071924b5867SDouglas Anderson 	if (ctlr->rt)
2072924b5867SDouglas Anderson 		spi_set_thread_rt(ctlr);
2073ffbbdd21SLinus Walleij 
2074ffbbdd21SLinus Walleij 	return 0;
2075ffbbdd21SLinus Walleij }
2076ffbbdd21SLinus Walleij 
2077ffbbdd21SLinus Walleij /**
2078ffbbdd21SLinus Walleij  * spi_get_next_queued_message() - called by driver to check for queued
2079ffbbdd21SLinus Walleij  * messages
20808caab75fSGeert Uytterhoeven  * @ctlr: the controller to check for queued messages
2081ffbbdd21SLinus Walleij  *
2082ffbbdd21SLinus Walleij  * If there are more messages in the queue, the next message is returned from
2083ffbbdd21SLinus Walleij  * this call.
208497d56dc6SJavier Martinez Canillas  *
208597d56dc6SJavier Martinez Canillas  * Return: the next message in the queue, else NULL if the queue is empty.
2086ffbbdd21SLinus Walleij  */
20878caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
2088ffbbdd21SLinus Walleij {
2089ffbbdd21SLinus Walleij 	struct spi_message *next;
2090ffbbdd21SLinus Walleij 	unsigned long flags;
2091ffbbdd21SLinus Walleij 
209295c8222fSDavid Jander 	/* Get a pointer to the next message, if any */
20938caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
20948caab75fSGeert Uytterhoeven 	next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
20951cfd97f9SAxel Lin 					queue);
20968caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2097ffbbdd21SLinus Walleij 
2098ffbbdd21SLinus Walleij 	return next;
2099ffbbdd21SLinus Walleij }
2100ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
2101ffbbdd21SLinus Walleij 
21027b1d87afSDavid Lechner /*
21037b1d87afSDavid Lechner  * __spi_unoptimize_message - shared implementation of spi_unoptimize_message()
21047b1d87afSDavid Lechner  *                            and spi_maybe_unoptimize_message()
21057b1d87afSDavid Lechner  * @msg: the message to unoptimize
21067b1d87afSDavid Lechner  *
21077b1d87afSDavid Lechner  * Peripheral drivers should use spi_unoptimize_message() and callers inside
21087b1d87afSDavid Lechner  * core should use spi_maybe_unoptimize_message() rather than calling this
21097b1d87afSDavid Lechner  * function directly.
21107b1d87afSDavid Lechner  *
21117b1d87afSDavid Lechner  * It is not valid to call this on a message that is not currently optimized.
21127b1d87afSDavid Lechner  */
21137b1d87afSDavid Lechner static void __spi_unoptimize_message(struct spi_message *msg)
21147b1d87afSDavid Lechner {
21157b1d87afSDavid Lechner 	struct spi_controller *ctlr = msg->spi->controller;
21167b1d87afSDavid Lechner 
21177b1d87afSDavid Lechner 	if (ctlr->unoptimize_message)
21187b1d87afSDavid Lechner 		ctlr->unoptimize_message(msg);
21197b1d87afSDavid Lechner 
2120fab53feaSDavid Lechner 	spi_res_release(ctlr, msg);
2121fab53feaSDavid Lechner 
21227b1d87afSDavid Lechner 	msg->optimized = false;
21237b1d87afSDavid Lechner 	msg->opt_state = NULL;
21247b1d87afSDavid Lechner }
21257b1d87afSDavid Lechner 
21267b1d87afSDavid Lechner /*
21277b1d87afSDavid Lechner  * spi_maybe_unoptimize_message - unoptimize msg not managed by a peripheral
21287b1d87afSDavid Lechner  * @msg: the message to unoptimize
21297b1d87afSDavid Lechner  *
21307b1d87afSDavid Lechner  * This function is used to unoptimize a message if and only if it was
21317b1d87afSDavid Lechner  * optimized by the core (via spi_maybe_optimize_message()).
21327b1d87afSDavid Lechner  */
21337b1d87afSDavid Lechner static void spi_maybe_unoptimize_message(struct spi_message *msg)
21347b1d87afSDavid Lechner {
2135ca52aa4cSDavid Lechner 	if (!msg->pre_optimized && msg->optimized &&
2136ca52aa4cSDavid Lechner 	    !msg->spi->controller->defer_optimize_message)
21377b1d87afSDavid Lechner 		__spi_unoptimize_message(msg);
21387b1d87afSDavid Lechner }
21397b1d87afSDavid Lechner 
2140ffbbdd21SLinus Walleij /**
2141ffbbdd21SLinus Walleij  * spi_finalize_current_message() - the current message is complete
21428caab75fSGeert Uytterhoeven  * @ctlr: the controller to return the message to
2143ffbbdd21SLinus Walleij  *
2144ffbbdd21SLinus Walleij  * Called by the driver to notify the core that the message in the front of the
2145ffbbdd21SLinus Walleij  * queue is complete and can be removed from the queue.
2146ffbbdd21SLinus Walleij  */
21478caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr)
2148ffbbdd21SLinus Walleij {
2149b42faeeeSVladimir Oltean 	struct spi_transfer *xfer;
2150ffbbdd21SLinus Walleij 	struct spi_message *mesg;
21512841a5fcSMark Brown 	int ret;
2152ffbbdd21SLinus Walleij 
21538caab75fSGeert Uytterhoeven 	mesg = ctlr->cur_msg;
2154ffbbdd21SLinus Walleij 
2155b42faeeeSVladimir Oltean 	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
2156b42faeeeSVladimir Oltean 		list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
2157b42faeeeSVladimir Oltean 			ptp_read_system_postts(xfer->ptp_sts);
2158b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_post = xfer->len;
2159b42faeeeSVladimir Oltean 		}
2160b42faeeeSVladimir Oltean 	}
2161b42faeeeSVladimir Oltean 
21626a726824SVladimir Oltean 	if (unlikely(ctlr->ptp_sts_supported))
21636a726824SVladimir Oltean 		list_for_each_entry(xfer, &mesg->transfers, transfer_list)
21646a726824SVladimir Oltean 			WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped);
2165f971a207SVladimir Oltean 
21668caab75fSGeert Uytterhoeven 	spi_unmap_msg(ctlr, mesg);
216799adef31SMark Brown 
21681714582aSDavid Jander 	if (mesg->prepared && ctlr->unprepare_message) {
21698caab75fSGeert Uytterhoeven 		ret = ctlr->unprepare_message(ctlr, mesg);
21702841a5fcSMark Brown 		if (ret) {
21718caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
21728caab75fSGeert Uytterhoeven 				ret);
21732841a5fcSMark Brown 		}
21742841a5fcSMark Brown 	}
2175391949b6SUwe Kleine-König 
21761714582aSDavid Jander 	mesg->prepared = false;
21771714582aSDavid Jander 
21787b1d87afSDavid Lechner 	spi_maybe_unoptimize_message(mesg);
21797b1d87afSDavid Lechner 
2180dc302905SDavid Jander 	WRITE_ONCE(ctlr->cur_msg_incomplete, false);
2181dc302905SDavid Jander 	smp_mb(); /* See __spi_pump_transfer_message()... */
2182dc302905SDavid Jander 	if (READ_ONCE(ctlr->cur_msg_need_completion))
218369fa9590SDavid Jander 		complete(&ctlr->cur_msg_completion);
21848e76ef88SMartin Sperl 
21858e76ef88SMartin Sperl 	trace_spi_message_done(mesg);
21862841a5fcSMark Brown 
2187ffbbdd21SLinus Walleij 	mesg->state = NULL;
2188ffbbdd21SLinus Walleij 	if (mesg->complete)
2189ffbbdd21SLinus Walleij 		mesg->complete(mesg->context);
2190ffbbdd21SLinus Walleij }
2191ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message);
2192ffbbdd21SLinus Walleij 
21938caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr)
2194ffbbdd21SLinus Walleij {
2195ffbbdd21SLinus Walleij 	unsigned long flags;
2196ffbbdd21SLinus Walleij 
21978caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
2198ffbbdd21SLinus Walleij 
21998caab75fSGeert Uytterhoeven 	if (ctlr->running || ctlr->busy) {
22008caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2201ffbbdd21SLinus Walleij 		return -EBUSY;
2202ffbbdd21SLinus Walleij 	}
2203ffbbdd21SLinus Walleij 
22048caab75fSGeert Uytterhoeven 	ctlr->running = true;
22058caab75fSGeert Uytterhoeven 	ctlr->cur_msg = NULL;
22068caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2207ffbbdd21SLinus Walleij 
220860a883d1SMarek Szyprowski 	kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2209ffbbdd21SLinus Walleij 
2210ffbbdd21SLinus Walleij 	return 0;
2211ffbbdd21SLinus Walleij }
2212ffbbdd21SLinus Walleij 
22138caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr)
2214ffbbdd21SLinus Walleij {
2215a71b7845SAndy Shevchenko 	unsigned int limit = 500;
2216ffbbdd21SLinus Walleij 	unsigned long flags;
2217ffbbdd21SLinus Walleij 
2218ffbbdd21SLinus Walleij 	/*
2219ffbbdd21SLinus Walleij 	 * This is a bit lame, but is optimized for the common execution path.
22208caab75fSGeert Uytterhoeven 	 * A wait_queue on the ctlr->busy could be used, but then the common
2221ffbbdd21SLinus Walleij 	 * execution path (pump_messages) would be required to call wake_up or
2222ffbbdd21SLinus Walleij 	 * friends on every SPI message. Do this instead.
2223ffbbdd21SLinus Walleij 	 */
2224a71b7845SAndy Shevchenko 	do {
2225a71b7845SAndy Shevchenko 		spin_lock_irqsave(&ctlr->queue_lock, flags);
2226a71b7845SAndy Shevchenko 		if (list_empty(&ctlr->queue) && !ctlr->busy) {
2227a71b7845SAndy Shevchenko 			ctlr->running = false;
2228a71b7845SAndy Shevchenko 			spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2229a71b7845SAndy Shevchenko 			return 0;
2230a71b7845SAndy Shevchenko 		}
22318caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2232f97b26b0SAxel Lin 		usleep_range(10000, 11000);
2233a71b7845SAndy Shevchenko 	} while (--limit);
2234ffbbdd21SLinus Walleij 
2235a71b7845SAndy Shevchenko 	return -EBUSY;
2236ffbbdd21SLinus Walleij }
2237ffbbdd21SLinus Walleij 
22388caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr)
2239ffbbdd21SLinus Walleij {
2240ffbbdd21SLinus Walleij 	int ret;
2241ffbbdd21SLinus Walleij 
22428caab75fSGeert Uytterhoeven 	ret = spi_stop_queue(ctlr);
2243ffbbdd21SLinus Walleij 
2244ffbbdd21SLinus Walleij 	/*
22453989144fSPetr Mladek 	 * kthread_flush_worker will block until all work is done.
2246ffbbdd21SLinus Walleij 	 * If the reason that stop_queue timed out is that the work will never
2247ffbbdd21SLinus Walleij 	 * finish, then it does no good to call flush/stop thread, so
2248ffbbdd21SLinus Walleij 	 * return anyway.
2249ffbbdd21SLinus Walleij 	 */
2250ffbbdd21SLinus Walleij 	if (ret) {
22518caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem destroying queue\n");
2252ffbbdd21SLinus Walleij 		return ret;
2253ffbbdd21SLinus Walleij 	}
2254ffbbdd21SLinus Walleij 
225560a883d1SMarek Szyprowski 	kthread_destroy_worker(ctlr->kworker);
2256ffbbdd21SLinus Walleij 
2257ffbbdd21SLinus Walleij 	return 0;
2258ffbbdd21SLinus Walleij }
2259ffbbdd21SLinus Walleij 
22600461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi,
22610461a414SMark Brown 				 struct spi_message *msg,
22620461a414SMark Brown 				 bool need_pump)
2263ffbbdd21SLinus Walleij {
22648caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
2265ffbbdd21SLinus Walleij 	unsigned long flags;
2266ffbbdd21SLinus Walleij 
22678caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->queue_lock, flags);
2268ffbbdd21SLinus Walleij 
22698caab75fSGeert Uytterhoeven 	if (!ctlr->running) {
22708caab75fSGeert Uytterhoeven 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2271ffbbdd21SLinus Walleij 		return -ESHUTDOWN;
2272ffbbdd21SLinus Walleij 	}
2273ffbbdd21SLinus Walleij 	msg->actual_length = 0;
2274ffbbdd21SLinus Walleij 	msg->status = -EINPROGRESS;
2275ffbbdd21SLinus Walleij 
22768caab75fSGeert Uytterhoeven 	list_add_tail(&msg->queue, &ctlr->queue);
2277ae7d2346SDavid Jander 	ctlr->queue_empty = false;
2278f0125f1aSMark Brown 	if (!ctlr->busy && need_pump)
227960a883d1SMarek Szyprowski 		kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2280ffbbdd21SLinus Walleij 
22818caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2282ffbbdd21SLinus Walleij 	return 0;
2283ffbbdd21SLinus Walleij }
2284ffbbdd21SLinus Walleij 
22850461a414SMark Brown /**
22860461a414SMark Brown  * spi_queued_transfer - transfer function for queued transfers
2287702ca026SAndy Shevchenko  * @spi: SPI device which is requesting transfer
2288702ca026SAndy Shevchenko  * @msg: SPI message which is to handled is queued to driver queue
228997d56dc6SJavier Martinez Canillas  *
229097d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
22910461a414SMark Brown  */
22920461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
22930461a414SMark Brown {
22940461a414SMark Brown 	return __spi_queued_transfer(spi, msg, true);
22950461a414SMark Brown }
22960461a414SMark Brown 
22978caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr)
2298ffbbdd21SLinus Walleij {
2299ffbbdd21SLinus Walleij 	int ret;
2300ffbbdd21SLinus Walleij 
23018caab75fSGeert Uytterhoeven 	ctlr->transfer = spi_queued_transfer;
23028caab75fSGeert Uytterhoeven 	if (!ctlr->transfer_one_message)
23038caab75fSGeert Uytterhoeven 		ctlr->transfer_one_message = spi_transfer_one_message;
2304ffbbdd21SLinus Walleij 
2305ffbbdd21SLinus Walleij 	/* Initialize and start queue */
23068caab75fSGeert Uytterhoeven 	ret = spi_init_queue(ctlr);
2307ffbbdd21SLinus Walleij 	if (ret) {
23088caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem initializing queue\n");
2309ffbbdd21SLinus Walleij 		goto err_init_queue;
2310ffbbdd21SLinus Walleij 	}
23118caab75fSGeert Uytterhoeven 	ctlr->queued = true;
23128caab75fSGeert Uytterhoeven 	ret = spi_start_queue(ctlr);
2313ffbbdd21SLinus Walleij 	if (ret) {
23148caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "problem starting queue\n");
2315ffbbdd21SLinus Walleij 		goto err_start_queue;
2316ffbbdd21SLinus Walleij 	}
2317ffbbdd21SLinus Walleij 
2318ffbbdd21SLinus Walleij 	return 0;
2319ffbbdd21SLinus Walleij 
2320ffbbdd21SLinus Walleij err_start_queue:
23218caab75fSGeert Uytterhoeven 	spi_destroy_queue(ctlr);
2322c3676d5cSMark Brown err_init_queue:
2323ffbbdd21SLinus Walleij 	return ret;
2324ffbbdd21SLinus Walleij }
2325ffbbdd21SLinus Walleij 
2326988f259bSBoris Brezillon /**
2327988f259bSBoris Brezillon  * spi_flush_queue - Send all pending messages in the queue from the callers'
2328988f259bSBoris Brezillon  *		     context
2329988f259bSBoris Brezillon  * @ctlr: controller to process queue for
2330988f259bSBoris Brezillon  *
2331988f259bSBoris Brezillon  * This should be used when one wants to ensure all pending messages have been
2332988f259bSBoris Brezillon  * sent before doing something. Is used by the spi-mem code to make sure SPI
2333988f259bSBoris Brezillon  * memory operations do not preempt regular SPI transfers that have been queued
2334988f259bSBoris Brezillon  * before the spi-mem operation.
2335988f259bSBoris Brezillon  */
2336988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr)
2337988f259bSBoris Brezillon {
2338988f259bSBoris Brezillon 	if (ctlr->transfer == spi_queued_transfer)
2339988f259bSBoris Brezillon 		__spi_pump_messages(ctlr, false);
2340988f259bSBoris Brezillon }
2341988f259bSBoris Brezillon 
2342ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/
2343ffbbdd21SLinus Walleij 
23447cb94361SAndreas Larsson #if defined(CONFIG_OF)
2345f276aacfSJanne Grunau static void of_spi_parse_dt_cs_delay(struct device_node *nc,
2346f276aacfSJanne Grunau 				     struct spi_delay *delay, const char *prop)
2347f276aacfSJanne Grunau {
2348f276aacfSJanne Grunau 	u32 value;
2349f276aacfSJanne Grunau 
2350f276aacfSJanne Grunau 	if (!of_property_read_u32(nc, prop, &value)) {
2351f276aacfSJanne Grunau 		if (value > U16_MAX) {
2352f276aacfSJanne Grunau 			delay->value = DIV_ROUND_UP(value, 1000);
2353f276aacfSJanne Grunau 			delay->unit = SPI_DELAY_UNIT_USECS;
2354f276aacfSJanne Grunau 		} else {
2355f276aacfSJanne Grunau 			delay->value = value;
2356f276aacfSJanne Grunau 			delay->unit = SPI_DELAY_UNIT_NSECS;
2357f276aacfSJanne Grunau 		}
2358f276aacfSJanne Grunau 	}
2359f276aacfSJanne Grunau }
2360f276aacfSJanne Grunau 
23618caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
2362c2e51ac3SGeert Uytterhoeven 			   struct device_node *nc)
2363d57a4282SGrant Likely {
23644d8ff6b0SAmit Kumar Mahapatra 	u32 value, cs[SPI_CS_CNT_MAX];
23654d8ff6b0SAmit Kumar Mahapatra 	int rc, idx;
2366d57a4282SGrant Likely 
2367d57a4282SGrant Likely 	/* Mode (clock phase/polarity/etc.) */
2368e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-cpha"))
2369d57a4282SGrant Likely 		spi->mode |= SPI_CPHA;
2370e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-cpol"))
2371d57a4282SGrant Likely 		spi->mode |= SPI_CPOL;
2372e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-3wire"))
2373c20151dfSLars-Peter Clausen 		spi->mode |= SPI_3WIRE;
2374e0bcb680SSergei Shtylyov 	if (of_property_read_bool(nc, "spi-lsb-first"))
2375cd6339e6SZhao Qiang 		spi->mode |= SPI_LSB_FIRST;
23763e5ec1dbSGregory CLEMENT 	if (of_property_read_bool(nc, "spi-cs-high"))
2377f3186dd8SLinus Walleij 		spi->mode |= SPI_CS_HIGH;
2378f3186dd8SLinus Walleij 
2379f477b7fbSwangyuhang 	/* Device DUAL/QUAD mode */
238089da4293STrent Piepho 	if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
238189da4293STrent Piepho 		switch (value) {
2382d962608cSDragos Bogdan 		case 0:
2383d962608cSDragos Bogdan 			spi->mode |= SPI_NO_TX;
2384d962608cSDragos Bogdan 			break;
238589da4293STrent Piepho 		case 1:
2386f477b7fbSwangyuhang 			break;
238789da4293STrent Piepho 		case 2:
2388f477b7fbSwangyuhang 			spi->mode |= SPI_TX_DUAL;
2389f477b7fbSwangyuhang 			break;
239089da4293STrent Piepho 		case 4:
2391f477b7fbSwangyuhang 			spi->mode |= SPI_TX_QUAD;
2392f477b7fbSwangyuhang 			break;
23936b03061fSYogesh Narayan Gaur 		case 8:
23946b03061fSYogesh Narayan Gaur 			spi->mode |= SPI_TX_OCTAL;
23956b03061fSYogesh Narayan Gaur 			break;
2396f477b7fbSwangyuhang 		default:
23978caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
2398a110f93dSwangyuhang 				"spi-tx-bus-width %d not supported\n",
239989da4293STrent Piepho 				value);
240080874d8cSGeert Uytterhoeven 			break;
2401f477b7fbSwangyuhang 		}
2402a822e99cSMark Brown 	}
2403f477b7fbSwangyuhang 
240489da4293STrent Piepho 	if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
240589da4293STrent Piepho 		switch (value) {
2406d962608cSDragos Bogdan 		case 0:
2407d962608cSDragos Bogdan 			spi->mode |= SPI_NO_RX;
2408d962608cSDragos Bogdan 			break;
240989da4293STrent Piepho 		case 1:
2410f477b7fbSwangyuhang 			break;
241189da4293STrent Piepho 		case 2:
2412f477b7fbSwangyuhang 			spi->mode |= SPI_RX_DUAL;
2413f477b7fbSwangyuhang 			break;
241489da4293STrent Piepho 		case 4:
2415f477b7fbSwangyuhang 			spi->mode |= SPI_RX_QUAD;
2416f477b7fbSwangyuhang 			break;
24176b03061fSYogesh Narayan Gaur 		case 8:
24186b03061fSYogesh Narayan Gaur 			spi->mode |= SPI_RX_OCTAL;
24196b03061fSYogesh Narayan Gaur 			break;
2420f477b7fbSwangyuhang 		default:
24218caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
2422a110f93dSwangyuhang 				"spi-rx-bus-width %d not supported\n",
242389da4293STrent Piepho 				value);
242480874d8cSGeert Uytterhoeven 			break;
2425f477b7fbSwangyuhang 		}
2426a822e99cSMark Brown 	}
2427f477b7fbSwangyuhang 
24288caab75fSGeert Uytterhoeven 	if (spi_controller_is_slave(ctlr)) {
2429194276b0SRob Herring 		if (!of_node_name_eq(nc, "slave")) {
243025c56c88SRob Herring 			dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
243125c56c88SRob Herring 				nc);
24326c364062SGeert Uytterhoeven 			return -EINVAL;
24336c364062SGeert Uytterhoeven 		}
24346c364062SGeert Uytterhoeven 		return 0;
24356c364062SGeert Uytterhoeven 	}
24366c364062SGeert Uytterhoeven 
24374d8ff6b0SAmit Kumar Mahapatra 	if (ctlr->num_chipselect > SPI_CS_CNT_MAX) {
24384d8ff6b0SAmit Kumar Mahapatra 		dev_err(&ctlr->dev, "No. of CS is more than max. no. of supported CS\n");
24394d8ff6b0SAmit Kumar Mahapatra 		return -EINVAL;
24404d8ff6b0SAmit Kumar Mahapatra 	}
24414d8ff6b0SAmit Kumar Mahapatra 
24425ee91605SAndy Shevchenko 	spi_set_all_cs_unused(spi);
24434d8ff6b0SAmit Kumar Mahapatra 
24446c364062SGeert Uytterhoeven 	/* Device address */
24454d8ff6b0SAmit Kumar Mahapatra 	rc = of_property_read_variable_u32_array(nc, "reg", &cs[0], 1,
24464d8ff6b0SAmit Kumar Mahapatra 						 SPI_CS_CNT_MAX);
24474d8ff6b0SAmit Kumar Mahapatra 	if (rc < 0) {
244825c56c88SRob Herring 		dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
244925c56c88SRob Herring 			nc, rc);
24506c364062SGeert Uytterhoeven 		return rc;
24516c364062SGeert Uytterhoeven 	}
24524d8ff6b0SAmit Kumar Mahapatra 	if (rc > ctlr->num_chipselect) {
24534d8ff6b0SAmit Kumar Mahapatra 		dev_err(&ctlr->dev, "%pOF has number of CS > ctlr->num_chipselect (%d)\n",
24544d8ff6b0SAmit Kumar Mahapatra 			nc, rc);
24554d8ff6b0SAmit Kumar Mahapatra 		return rc;
24564d8ff6b0SAmit Kumar Mahapatra 	}
24574d8ff6b0SAmit Kumar Mahapatra 	if ((of_property_read_bool(nc, "parallel-memories")) &&
24584d8ff6b0SAmit Kumar Mahapatra 	    (!(ctlr->flags & SPI_CONTROLLER_MULTI_CS))) {
24594d8ff6b0SAmit Kumar Mahapatra 		dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n");
24604d8ff6b0SAmit Kumar Mahapatra 		return -EINVAL;
24614d8ff6b0SAmit Kumar Mahapatra 	}
24624d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < rc; idx++)
24634d8ff6b0SAmit Kumar Mahapatra 		spi_set_chipselect(spi, idx, cs[idx]);
24644d8ff6b0SAmit Kumar Mahapatra 
24654d8ff6b0SAmit Kumar Mahapatra 	/*
24661209c556SAndy Shevchenko 	 * By default spi->chip_select[0] will hold the physical CS number,
24671209c556SAndy Shevchenko 	 * so set bit 0 in spi->cs_index_mask.
24684d8ff6b0SAmit Kumar Mahapatra 	 */
24691209c556SAndy Shevchenko 	spi->cs_index_mask = BIT(0);
24706c364062SGeert Uytterhoeven 
2471d57a4282SGrant Likely 	/* Device speed */
2472671c3bf5SChuanhong Guo 	if (!of_property_read_u32(nc, "spi-max-frequency", &value))
247389da4293STrent Piepho 		spi->max_speed_hz = value;
2474d57a4282SGrant Likely 
2475f276aacfSJanne Grunau 	/* Device CS delays */
2476f276aacfSJanne Grunau 	of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns");
24775827b31dSJanne Grunau 	of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns");
24785827b31dSJanne Grunau 	of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns");
247933a2fde5STudor Ambarus 
2480c2e51ac3SGeert Uytterhoeven 	return 0;
2481c2e51ac3SGeert Uytterhoeven }
2482c2e51ac3SGeert Uytterhoeven 
2483c2e51ac3SGeert Uytterhoeven static struct spi_device *
24848caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
2485c2e51ac3SGeert Uytterhoeven {
2486c2e51ac3SGeert Uytterhoeven 	struct spi_device *spi;
2487c2e51ac3SGeert Uytterhoeven 	int rc;
2488c2e51ac3SGeert Uytterhoeven 
2489c2e51ac3SGeert Uytterhoeven 	/* Alloc an spi_device */
24908caab75fSGeert Uytterhoeven 	spi = spi_alloc_device(ctlr);
2491c2e51ac3SGeert Uytterhoeven 	if (!spi) {
249225c56c88SRob Herring 		dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
2493c2e51ac3SGeert Uytterhoeven 		rc = -ENOMEM;
2494c2e51ac3SGeert Uytterhoeven 		goto err_out;
2495c2e51ac3SGeert Uytterhoeven 	}
2496c2e51ac3SGeert Uytterhoeven 
2497c2e51ac3SGeert Uytterhoeven 	/* Select device driver */
2498673aa1edSMiquel Raynal 	rc = of_alias_from_compatible(nc, spi->modalias,
2499c2e51ac3SGeert Uytterhoeven 				      sizeof(spi->modalias));
2500c2e51ac3SGeert Uytterhoeven 	if (rc < 0) {
250125c56c88SRob Herring 		dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
2502c2e51ac3SGeert Uytterhoeven 		goto err_out;
2503c2e51ac3SGeert Uytterhoeven 	}
2504c2e51ac3SGeert Uytterhoeven 
25058caab75fSGeert Uytterhoeven 	rc = of_spi_parse_dt(ctlr, spi, nc);
2506c2e51ac3SGeert Uytterhoeven 	if (rc)
2507c2e51ac3SGeert Uytterhoeven 		goto err_out;
2508c2e51ac3SGeert Uytterhoeven 
2509d57a4282SGrant Likely 	/* Store a pointer to the node in the device structure */
2510d57a4282SGrant Likely 	of_node_get(nc);
2511c7cc588bSAndy Shevchenko 
2512c7cc588bSAndy Shevchenko 	device_set_node(&spi->dev, of_fwnode_handle(nc));
2513d57a4282SGrant Likely 
2514d57a4282SGrant Likely 	/* Register the new device */
2515d57a4282SGrant Likely 	rc = spi_add_device(spi);
2516d57a4282SGrant Likely 	if (rc) {
251725c56c88SRob Herring 		dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
25188324147fSJohan Hovold 		goto err_of_node_put;
2519d57a4282SGrant Likely 	}
2520d57a4282SGrant Likely 
2521aff5e3f8SPantelis Antoniou 	return spi;
2522aff5e3f8SPantelis Antoniou 
25238324147fSJohan Hovold err_of_node_put:
25248324147fSJohan Hovold 	of_node_put(nc);
2525aff5e3f8SPantelis Antoniou err_out:
2526aff5e3f8SPantelis Antoniou 	spi_dev_put(spi);
2527aff5e3f8SPantelis Antoniou 	return ERR_PTR(rc);
2528aff5e3f8SPantelis Antoniou }
2529aff5e3f8SPantelis Antoniou 
2530aff5e3f8SPantelis Antoniou /**
2531aff5e3f8SPantelis Antoniou  * of_register_spi_devices() - Register child devices onto the SPI bus
25328caab75fSGeert Uytterhoeven  * @ctlr:	Pointer to spi_controller device
2533aff5e3f8SPantelis Antoniou  *
25346c364062SGeert Uytterhoeven  * Registers an spi_device for each child node of controller node which
25356c364062SGeert Uytterhoeven  * represents a valid SPI slave.
2536aff5e3f8SPantelis Antoniou  */
25378caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr)
2538aff5e3f8SPantelis Antoniou {
2539aff5e3f8SPantelis Antoniou 	struct spi_device *spi;
2540aff5e3f8SPantelis Antoniou 	struct device_node *nc;
2541aff5e3f8SPantelis Antoniou 
25428caab75fSGeert Uytterhoeven 	for_each_available_child_of_node(ctlr->dev.of_node, nc) {
2543bd6c1644SGeert Uytterhoeven 		if (of_node_test_and_set_flag(nc, OF_POPULATED))
2544bd6c1644SGeert Uytterhoeven 			continue;
25458caab75fSGeert Uytterhoeven 		spi = of_register_spi_device(ctlr, nc);
2546e0af98a7SRalf Ramsauer 		if (IS_ERR(spi)) {
25478caab75fSGeert Uytterhoeven 			dev_warn(&ctlr->dev,
254825c56c88SRob Herring 				 "Failed to create SPI device for %pOF\n", nc);
2549e0af98a7SRalf Ramsauer 			of_node_clear_flag(nc, OF_POPULATED);
2550e0af98a7SRalf Ramsauer 		}
2551d57a4282SGrant Likely 	}
2552d57a4282SGrant Likely }
2553d57a4282SGrant Likely #else
25548caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { }
2555d57a4282SGrant Likely #endif
2556d57a4282SGrant Likely 
25570c79378cSSebastian Reichel /**
25580c79378cSSebastian Reichel  * spi_new_ancillary_device() - Register ancillary SPI device
25590c79378cSSebastian Reichel  * @spi:         Pointer to the main SPI device registering the ancillary device
25600c79378cSSebastian Reichel  * @chip_select: Chip Select of the ancillary device
25610c79378cSSebastian Reichel  *
25620c79378cSSebastian Reichel  * Register an ancillary SPI device; for example some chips have a chip-select
25630c79378cSSebastian Reichel  * for normal device usage and another one for setup/firmware upload.
25640c79378cSSebastian Reichel  *
25650c79378cSSebastian Reichel  * This may only be called from main SPI device's probe routine.
25660c79378cSSebastian Reichel  *
25670c79378cSSebastian Reichel  * Return: 0 on success; negative errno on failure
25680c79378cSSebastian Reichel  */
25690c79378cSSebastian Reichel struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
25700c79378cSSebastian Reichel 					     u8 chip_select)
25710c79378cSSebastian Reichel {
25727b5c6a54SAndy Shevchenko 	struct spi_controller *ctlr = spi->controller;
25730c79378cSSebastian Reichel 	struct spi_device *ancillary;
25740f2ecc3fSLi zeming 	int rc;
25750c79378cSSebastian Reichel 
25760c79378cSSebastian Reichel 	/* Alloc an spi_device */
25777b5c6a54SAndy Shevchenko 	ancillary = spi_alloc_device(ctlr);
25780c79378cSSebastian Reichel 	if (!ancillary) {
25790c79378cSSebastian Reichel 		rc = -ENOMEM;
25800c79378cSSebastian Reichel 		goto err_out;
25810c79378cSSebastian Reichel 	}
25820c79378cSSebastian Reichel 
258351e99de5SWolfram Sang 	strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias));
25840c79378cSSebastian Reichel 
25850c79378cSSebastian Reichel 	/* Use provided chip-select for ancillary device */
25865ee91605SAndy Shevchenko 	spi_set_all_cs_unused(ancillary);
2587303feb3cSAmit Kumar Mahapatra 	spi_set_chipselect(ancillary, 0, chip_select);
25880c79378cSSebastian Reichel 
25890c79378cSSebastian Reichel 	/* Take over SPI mode/speed from SPI main device */
25900c79378cSSebastian Reichel 	ancillary->max_speed_hz = spi->max_speed_hz;
2591b01d5506SColin Ian King 	ancillary->mode = spi->mode;
25924d8ff6b0SAmit Kumar Mahapatra 	/*
25931209c556SAndy Shevchenko 	 * By default spi->chip_select[0] will hold the physical CS number,
25941209c556SAndy Shevchenko 	 * so set bit 0 in spi->cs_index_mask.
25954d8ff6b0SAmit Kumar Mahapatra 	 */
25961209c556SAndy Shevchenko 	ancillary->cs_index_mask = BIT(0);
25970c79378cSSebastian Reichel 
25987b5c6a54SAndy Shevchenko 	WARN_ON(!mutex_is_locked(&ctlr->add_lock));
25997b5c6a54SAndy Shevchenko 
26000c79378cSSebastian Reichel 	/* Register the new device */
26017b5c6a54SAndy Shevchenko 	rc = __spi_add_device(ancillary);
26020c79378cSSebastian Reichel 	if (rc) {
26030c79378cSSebastian Reichel 		dev_err(&spi->dev, "failed to register ancillary device\n");
26040c79378cSSebastian Reichel 		goto err_out;
26050c79378cSSebastian Reichel 	}
26060c79378cSSebastian Reichel 
26070c79378cSSebastian Reichel 	return ancillary;
26080c79378cSSebastian Reichel 
26090c79378cSSebastian Reichel err_out:
26100c79378cSSebastian Reichel 	spi_dev_put(ancillary);
26110c79378cSSebastian Reichel 	return ERR_PTR(rc);
26120c79378cSSebastian Reichel }
26130c79378cSSebastian Reichel EXPORT_SYMBOL_GPL(spi_new_ancillary_device);
26140c79378cSSebastian Reichel 
261564bee4d2SMika Westerberg #ifdef CONFIG_ACPI
26164c3c5954SArd Biesheuvel struct acpi_spi_lookup {
26174c3c5954SArd Biesheuvel 	struct spi_controller 	*ctlr;
26184c3c5954SArd Biesheuvel 	u32			max_speed_hz;
26194c3c5954SArd Biesheuvel 	u32			mode;
26204c3c5954SArd Biesheuvel 	int			irq;
26214c3c5954SArd Biesheuvel 	u8			bits_per_word;
26224c3c5954SArd Biesheuvel 	u8			chip_select;
262387e59b36SStefan Binding 	int			n;
262487e59b36SStefan Binding 	int			index;
26254c3c5954SArd Biesheuvel };
26264c3c5954SArd Biesheuvel 
2627e612af7aSStefan Binding static int acpi_spi_count(struct acpi_resource *ares, void *data)
2628e612af7aSStefan Binding {
2629e612af7aSStefan Binding 	struct acpi_resource_spi_serialbus *sb;
2630e612af7aSStefan Binding 	int *count = data;
2631e612af7aSStefan Binding 
2632e612af7aSStefan Binding 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2633e612af7aSStefan Binding 		return 1;
2634e612af7aSStefan Binding 
2635e612af7aSStefan Binding 	sb = &ares->data.spi_serial_bus;
2636e612af7aSStefan Binding 	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI)
2637e612af7aSStefan Binding 		return 1;
2638e612af7aSStefan Binding 
2639e612af7aSStefan Binding 	*count = *count + 1;
2640e612af7aSStefan Binding 
2641e612af7aSStefan Binding 	return 1;
2642e612af7aSStefan Binding }
2643e612af7aSStefan Binding 
2644e612af7aSStefan Binding /**
2645e612af7aSStefan Binding  * acpi_spi_count_resources - Count the number of SpiSerialBus resources
2646e612af7aSStefan Binding  * @adev:	ACPI device
2647e612af7aSStefan Binding  *
2648702ca026SAndy Shevchenko  * Return: the number of SpiSerialBus resources in the ACPI-device's
2649e612af7aSStefan Binding  * resource-list; or a negative error code.
2650e612af7aSStefan Binding  */
2651e612af7aSStefan Binding int acpi_spi_count_resources(struct acpi_device *adev)
2652e612af7aSStefan Binding {
2653e612af7aSStefan Binding 	LIST_HEAD(r);
2654e612af7aSStefan Binding 	int count = 0;
2655e612af7aSStefan Binding 	int ret;
2656e612af7aSStefan Binding 
2657e612af7aSStefan Binding 	ret = acpi_dev_get_resources(adev, &r, acpi_spi_count, &count);
2658e612af7aSStefan Binding 	if (ret < 0)
2659e612af7aSStefan Binding 		return ret;
2660e612af7aSStefan Binding 
2661e612af7aSStefan Binding 	acpi_dev_free_resource_list(&r);
2662e612af7aSStefan Binding 
2663e612af7aSStefan Binding 	return count;
2664e612af7aSStefan Binding }
2665e612af7aSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_count_resources);
2666e612af7aSStefan Binding 
26674c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
26684c3c5954SArd Biesheuvel 					    struct acpi_spi_lookup *lookup)
26698a2e487eSLukas Wunner {
26708a2e487eSLukas Wunner 	const union acpi_object *obj;
26718a2e487eSLukas Wunner 
26728a2e487eSLukas Wunner 	if (!x86_apple_machine)
26738a2e487eSLukas Wunner 		return;
26748a2e487eSLukas Wunner 
26758a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
26768a2e487eSLukas Wunner 	    && obj->buffer.length >= 4)
26774c3c5954SArd Biesheuvel 		lookup->max_speed_hz  = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
26788a2e487eSLukas Wunner 
26798a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
26808a2e487eSLukas Wunner 	    && obj->buffer.length == 8)
26814c3c5954SArd Biesheuvel 		lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
26828a2e487eSLukas Wunner 
26838a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
26848a2e487eSLukas Wunner 	    && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
26854c3c5954SArd Biesheuvel 		lookup->mode |= SPI_LSB_FIRST;
26868a2e487eSLukas Wunner 
26878a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
26888a2e487eSLukas Wunner 	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
26894c3c5954SArd Biesheuvel 		lookup->mode |= SPI_CPOL;
26908a2e487eSLukas Wunner 
26918a2e487eSLukas Wunner 	if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
26928a2e487eSLukas Wunner 	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
26934c3c5954SArd Biesheuvel 		lookup->mode |= SPI_CPHA;
26948a2e487eSLukas Wunner }
26958a2e487eSLukas Wunner 
269664bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
269764bee4d2SMika Westerberg {
26984c3c5954SArd Biesheuvel 	struct acpi_spi_lookup *lookup = data;
26994c3c5954SArd Biesheuvel 	struct spi_controller *ctlr = lookup->ctlr;
270064bee4d2SMika Westerberg 
270164bee4d2SMika Westerberg 	if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
270264bee4d2SMika Westerberg 		struct acpi_resource_spi_serialbus *sb;
27034c3c5954SArd Biesheuvel 		acpi_handle parent_handle;
27044c3c5954SArd Biesheuvel 		acpi_status status;
270564bee4d2SMika Westerberg 
270664bee4d2SMika Westerberg 		sb = &ares->data.spi_serial_bus;
270764bee4d2SMika Westerberg 		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
27084c3c5954SArd Biesheuvel 
270987e59b36SStefan Binding 			if (lookup->index != -1 && lookup->n++ != lookup->index)
271087e59b36SStefan Binding 				return 1;
271187e59b36SStefan Binding 
27124c3c5954SArd Biesheuvel 			status = acpi_get_handle(NULL,
27134c3c5954SArd Biesheuvel 						 sb->resource_source.string_ptr,
27144c3c5954SArd Biesheuvel 						 &parent_handle);
27154c3c5954SArd Biesheuvel 
271687e59b36SStefan Binding 			if (ACPI_FAILURE(status))
27174c3c5954SArd Biesheuvel 				return -ENODEV;
27184c3c5954SArd Biesheuvel 
271987e59b36SStefan Binding 			if (ctlr) {
27202d19ea9eSAndy Shevchenko 				if (!device_match_acpi_handle(ctlr->dev.parent, parent_handle))
272187e59b36SStefan Binding 					return -ENODEV;
272287e59b36SStefan Binding 			} else {
272387e59b36SStefan Binding 				struct acpi_device *adev;
272487e59b36SStefan Binding 
2725ac2a3feeSRafael J. Wysocki 				adev = acpi_fetch_acpi_dev(parent_handle);
2726ac2a3feeSRafael J. Wysocki 				if (!adev)
272787e59b36SStefan Binding 					return -ENODEV;
272887e59b36SStefan Binding 
272987e59b36SStefan Binding 				ctlr = acpi_spi_find_controller_by_adev(adev);
273087e59b36SStefan Binding 				if (!ctlr)
27319c22ec4aSAndy Shevchenko 					return -EPROBE_DEFER;
273287e59b36SStefan Binding 
273387e59b36SStefan Binding 				lookup->ctlr = ctlr;
273487e59b36SStefan Binding 			}
273587e59b36SStefan Binding 
2736a0a90718SMika Westerberg 			/*
2737a0a90718SMika Westerberg 			 * ACPI DeviceSelection numbering is handled by the
2738a0a90718SMika Westerberg 			 * host controller driver in Windows and can vary
2739a0a90718SMika Westerberg 			 * from driver to driver. In Linux we always expect
2740a0a90718SMika Westerberg 			 * 0 .. max - 1 so we need to ask the driver to
2741a0a90718SMika Westerberg 			 * translate between the two schemes.
2742a0a90718SMika Westerberg 			 */
27438caab75fSGeert Uytterhoeven 			if (ctlr->fw_translate_cs) {
27448caab75fSGeert Uytterhoeven 				int cs = ctlr->fw_translate_cs(ctlr,
2745a0a90718SMika Westerberg 						sb->device_selection);
2746a0a90718SMika Westerberg 				if (cs < 0)
2747a0a90718SMika Westerberg 					return cs;
27484c3c5954SArd Biesheuvel 				lookup->chip_select = cs;
2749a0a90718SMika Westerberg 			} else {
27504c3c5954SArd Biesheuvel 				lookup->chip_select = sb->device_selection;
2751a0a90718SMika Westerberg 			}
2752a0a90718SMika Westerberg 
27534c3c5954SArd Biesheuvel 			lookup->max_speed_hz = sb->connection_speed;
27540dadde34SAndy Shevchenko 			lookup->bits_per_word = sb->data_bit_length;
275564bee4d2SMika Westerberg 
275664bee4d2SMika Westerberg 			if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
27574c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CPHA;
275864bee4d2SMika Westerberg 			if (sb->clock_polarity == ACPI_SPI_START_HIGH)
27594c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CPOL;
276064bee4d2SMika Westerberg 			if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
27614c3c5954SArd Biesheuvel 				lookup->mode |= SPI_CS_HIGH;
276264bee4d2SMika Westerberg 		}
27634c3c5954SArd Biesheuvel 	} else if (lookup->irq < 0) {
276464bee4d2SMika Westerberg 		struct resource r;
276564bee4d2SMika Westerberg 
276664bee4d2SMika Westerberg 		if (acpi_dev_resource_interrupt(ares, 0, &r))
27674c3c5954SArd Biesheuvel 			lookup->irq = r.start;
276864bee4d2SMika Westerberg 	}
276964bee4d2SMika Westerberg 
277064bee4d2SMika Westerberg 	/* Always tell the ACPI core to skip this resource */
277164bee4d2SMika Westerberg 	return 1;
277264bee4d2SMika Westerberg }
277364bee4d2SMika Westerberg 
2774000bee0eSStefan Binding /**
2775000bee0eSStefan Binding  * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2776000bee0eSStefan Binding  * @ctlr: controller to which the spi device belongs
2777000bee0eSStefan Binding  * @adev: ACPI Device for the spi device
277887e59b36SStefan Binding  * @index: Index of the spi resource inside the ACPI Node
2779000bee0eSStefan Binding  *
2780702ca026SAndy Shevchenko  * This should be used to allocate a new SPI device from and ACPI Device node.
2781702ca026SAndy Shevchenko  * The caller is responsible for calling spi_add_device to register the SPI device.
2782000bee0eSStefan Binding  *
2783702ca026SAndy Shevchenko  * If ctlr is set to NULL, the Controller for the SPI device will be looked up
278487e59b36SStefan Binding  * using the resource.
278587e59b36SStefan Binding  * If index is set to -1, index is not used.
278687e59b36SStefan Binding  * Note: If index is -1, ctlr must be set.
278787e59b36SStefan Binding  *
2788000bee0eSStefan Binding  * Return: a pointer to the new device, or ERR_PTR on error.
2789000bee0eSStefan Binding  */
2790000bee0eSStefan Binding struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
279187e59b36SStefan Binding 					 struct acpi_device *adev,
279287e59b36SStefan Binding 					 int index)
279364bee4d2SMika Westerberg {
27944c3c5954SArd Biesheuvel 	acpi_handle parent_handle = NULL;
279564bee4d2SMika Westerberg 	struct list_head resource_list;
2796b28944c6SArd Biesheuvel 	struct acpi_spi_lookup lookup = {};
279764bee4d2SMika Westerberg 	struct spi_device *spi;
279864bee4d2SMika Westerberg 	int ret;
279964bee4d2SMika Westerberg 
280087e59b36SStefan Binding 	if (!ctlr && index == -1)
280187e59b36SStefan Binding 		return ERR_PTR(-EINVAL);
280287e59b36SStefan Binding 
28034c3c5954SArd Biesheuvel 	lookup.ctlr		= ctlr;
28044c3c5954SArd Biesheuvel 	lookup.irq		= -1;
280587e59b36SStefan Binding 	lookup.index		= index;
280687e59b36SStefan Binding 	lookup.n		= 0;
28074c3c5954SArd Biesheuvel 
28084c3c5954SArd Biesheuvel 	INIT_LIST_HEAD(&resource_list);
28094c3c5954SArd Biesheuvel 	ret = acpi_dev_get_resources(adev, &resource_list,
28104c3c5954SArd Biesheuvel 				     acpi_spi_add_resource, &lookup);
28114c3c5954SArd Biesheuvel 	acpi_dev_free_resource_list(&resource_list);
28124c3c5954SArd Biesheuvel 
28134c3c5954SArd Biesheuvel 	if (ret < 0)
281495c8222fSDavid Jander 		/* Found SPI in _CRS but it points to another controller */
2815b6747f4fSAndy Shevchenko 		return ERR_PTR(ret);
28164c3c5954SArd Biesheuvel 
28174c3c5954SArd Biesheuvel 	if (!lookup.max_speed_hz &&
281810e92724SBjorn Helgaas 	    ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) &&
28192d19ea9eSAndy Shevchenko 	    device_match_acpi_handle(lookup.ctlr->dev.parent, parent_handle)) {
28204c3c5954SArd Biesheuvel 		/* Apple does not use _CRS but nested devices for SPI slaves */
28214c3c5954SArd Biesheuvel 		acpi_spi_parse_apple_properties(adev, &lookup);
28224c3c5954SArd Biesheuvel 	}
28234c3c5954SArd Biesheuvel 
28244c3c5954SArd Biesheuvel 	if (!lookup.max_speed_hz)
2825000bee0eSStefan Binding 		return ERR_PTR(-ENODEV);
28264c3c5954SArd Biesheuvel 
282787e59b36SStefan Binding 	spi = spi_alloc_device(lookup.ctlr);
282864bee4d2SMika Westerberg 	if (!spi) {
282987e59b36SStefan Binding 		dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n",
283064bee4d2SMika Westerberg 			dev_name(&adev->dev));
2831000bee0eSStefan Binding 		return ERR_PTR(-ENOMEM);
283264bee4d2SMika Westerberg 	}
283364bee4d2SMika Westerberg 
28345ee91605SAndy Shevchenko 	spi_set_all_cs_unused(spi);
28355ee91605SAndy Shevchenko 	spi_set_chipselect(spi, 0, lookup.chip_select);
28364d8ff6b0SAmit Kumar Mahapatra 
28377b199811SRafael J. Wysocki 	ACPI_COMPANION_SET(&spi->dev, adev);
28384c3c5954SArd Biesheuvel 	spi->max_speed_hz	= lookup.max_speed_hz;
2839ea235786SJohn Garry 	spi->mode		|= lookup.mode;
28404c3c5954SArd Biesheuvel 	spi->irq		= lookup.irq;
28414c3c5954SArd Biesheuvel 	spi->bits_per_word	= lookup.bits_per_word;
28424d8ff6b0SAmit Kumar Mahapatra 	/*
28431209c556SAndy Shevchenko 	 * By default spi->chip_select[0] will hold the physical CS number,
28441209c556SAndy Shevchenko 	 * so set bit 0 in spi->cs_index_mask.
28454d8ff6b0SAmit Kumar Mahapatra 	 */
28461209c556SAndy Shevchenko 	spi->cs_index_mask	= BIT(0);
284764bee4d2SMika Westerberg 
2848000bee0eSStefan Binding 	return spi;
2849000bee0eSStefan Binding }
2850000bee0eSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_device_alloc);
2851000bee0eSStefan Binding 
2852000bee0eSStefan Binding static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
2853000bee0eSStefan Binding 					    struct acpi_device *adev)
2854000bee0eSStefan Binding {
2855000bee0eSStefan Binding 	struct spi_device *spi;
2856000bee0eSStefan Binding 
2857000bee0eSStefan Binding 	if (acpi_bus_get_status(adev) || !adev->status.present ||
2858000bee0eSStefan Binding 	    acpi_device_enumerated(adev))
2859000bee0eSStefan Binding 		return AE_OK;
2860000bee0eSStefan Binding 
286187e59b36SStefan Binding 	spi = acpi_spi_device_alloc(ctlr, adev, -1);
2862000bee0eSStefan Binding 	if (IS_ERR(spi)) {
2863000bee0eSStefan Binding 		if (PTR_ERR(spi) == -ENOMEM)
2864000bee0eSStefan Binding 			return AE_NO_MEMORY;
2865000bee0eSStefan Binding 		else
2866000bee0eSStefan Binding 			return AE_OK;
2867000bee0eSStefan Binding 	}
2868000bee0eSStefan Binding 
28690c6543f6SDan O'Donovan 	acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
28700c6543f6SDan O'Donovan 			  sizeof(spi->modalias));
28710c6543f6SDan O'Donovan 
287233ada67dSChristophe RICARD 	if (spi->irq < 0)
287333ada67dSChristophe RICARD 		spi->irq = acpi_dev_gpio_irq_get(adev, 0);
287433ada67dSChristophe RICARD 
28757f24467fSOctavian Purdila 	acpi_device_set_enumerated(adev);
28767f24467fSOctavian Purdila 
287733cf00e5SMika Westerberg 	adev->power.flags.ignore_parent = true;
287864bee4d2SMika Westerberg 	if (spi_add_device(spi)) {
287933cf00e5SMika Westerberg 		adev->power.flags.ignore_parent = false;
28808caab75fSGeert Uytterhoeven 		dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
288164bee4d2SMika Westerberg 			dev_name(&adev->dev));
288264bee4d2SMika Westerberg 		spi_dev_put(spi);
288364bee4d2SMika Westerberg 	}
288464bee4d2SMika Westerberg 
288564bee4d2SMika Westerberg 	return AE_OK;
288664bee4d2SMika Westerberg }
288764bee4d2SMika Westerberg 
28887f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
28897f24467fSOctavian Purdila 				       void *data, void **return_value)
28907f24467fSOctavian Purdila {
28917030c428SRafael J. Wysocki 	struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
28928caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = data;
28937f24467fSOctavian Purdila 
28947030c428SRafael J. Wysocki 	if (!adev)
28957f24467fSOctavian Purdila 		return AE_OK;
28967f24467fSOctavian Purdila 
28978caab75fSGeert Uytterhoeven 	return acpi_register_spi_device(ctlr, adev);
28987f24467fSOctavian Purdila }
28997f24467fSOctavian Purdila 
29004c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH		32
29014c3c5954SArd Biesheuvel 
29028caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr)
290364bee4d2SMika Westerberg {
290464bee4d2SMika Westerberg 	acpi_status status;
290564bee4d2SMika Westerberg 	acpi_handle handle;
290664bee4d2SMika Westerberg 
29078caab75fSGeert Uytterhoeven 	handle = ACPI_HANDLE(ctlr->dev.parent);
290864bee4d2SMika Westerberg 	if (!handle)
290964bee4d2SMika Westerberg 		return;
291064bee4d2SMika Westerberg 
29114c3c5954SArd Biesheuvel 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
29124c3c5954SArd Biesheuvel 				     SPI_ACPI_ENUMERATE_MAX_DEPTH,
29138caab75fSGeert Uytterhoeven 				     acpi_spi_add_device, NULL, ctlr, NULL);
291464bee4d2SMika Westerberg 	if (ACPI_FAILURE(status))
29158caab75fSGeert Uytterhoeven 		dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
291664bee4d2SMika Westerberg }
291764bee4d2SMika Westerberg #else
29188caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
291964bee4d2SMika Westerberg #endif /* CONFIG_ACPI */
292064bee4d2SMika Westerberg 
29218caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev)
29228ae12a0dSDavid Brownell {
29238caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
29248ae12a0dSDavid Brownell 
29258caab75fSGeert Uytterhoeven 	ctlr = container_of(dev, struct spi_controller, dev);
29268caab75fSGeert Uytterhoeven 	kfree(ctlr);
29278ae12a0dSDavid Brownell }
29288ae12a0dSDavid Brownell 
29298ae12a0dSDavid Brownell static struct class spi_master_class = {
29308ae12a0dSDavid Brownell 	.name		= "spi_master",
29318caab75fSGeert Uytterhoeven 	.dev_release	= spi_controller_release,
2932eca2ebc7SMartin Sperl 	.dev_groups	= spi_master_groups,
29338ae12a0dSDavid Brownell };
29348ae12a0dSDavid Brownell 
29356c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE
29366c364062SGeert Uytterhoeven /**
29376c364062SGeert Uytterhoeven  * spi_slave_abort - abort the ongoing transfer request on an SPI slave
29386c364062SGeert Uytterhoeven  *		     controller
29396c364062SGeert Uytterhoeven  * @spi: device used for the current transfer
29406c364062SGeert Uytterhoeven  */
29416c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi)
29426c364062SGeert Uytterhoeven {
29438caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
29446c364062SGeert Uytterhoeven 
29458caab75fSGeert Uytterhoeven 	if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
29468caab75fSGeert Uytterhoeven 		return ctlr->slave_abort(ctlr);
29476c364062SGeert Uytterhoeven 
29486c364062SGeert Uytterhoeven 	return -ENOTSUPP;
29496c364062SGeert Uytterhoeven }
29506c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort);
29516c364062SGeert Uytterhoeven 
2952b8d3b056SYang Yingliang int spi_target_abort(struct spi_device *spi)
2953b8d3b056SYang Yingliang {
2954b8d3b056SYang Yingliang 	struct spi_controller *ctlr = spi->controller;
2955b8d3b056SYang Yingliang 
2956b8d3b056SYang Yingliang 	if (spi_controller_is_target(ctlr) && ctlr->target_abort)
2957b8d3b056SYang Yingliang 		return ctlr->target_abort(ctlr);
2958b8d3b056SYang Yingliang 
2959b8d3b056SYang Yingliang 	return -ENOTSUPP;
2960b8d3b056SYang Yingliang }
2961b8d3b056SYang Yingliang EXPORT_SYMBOL_GPL(spi_target_abort);
2962b8d3b056SYang Yingliang 
2963cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2964cc8b4659SGeert Uytterhoeven 			  char *buf)
29656c364062SGeert Uytterhoeven {
29668caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
29678caab75fSGeert Uytterhoeven 						   dev);
29686c364062SGeert Uytterhoeven 	struct device *child;
29696c364062SGeert Uytterhoeven 
2970c21b0837SAndy Shevchenko 	child = device_find_any_child(&ctlr->dev);
2971f2daa466SAndy Shevchenko 	return sysfs_emit(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL);
29726c364062SGeert Uytterhoeven }
29736c364062SGeert Uytterhoeven 
2974cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2975cc8b4659SGeert Uytterhoeven 			   const char *buf, size_t count)
29766c364062SGeert Uytterhoeven {
29778caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
29788caab75fSGeert Uytterhoeven 						   dev);
29796c364062SGeert Uytterhoeven 	struct spi_device *spi;
29806c364062SGeert Uytterhoeven 	struct device *child;
29816c364062SGeert Uytterhoeven 	char name[32];
29826c364062SGeert Uytterhoeven 	int rc;
29836c364062SGeert Uytterhoeven 
29846c364062SGeert Uytterhoeven 	rc = sscanf(buf, "%31s", name);
29856c364062SGeert Uytterhoeven 	if (rc != 1 || !name[0])
29866c364062SGeert Uytterhoeven 		return -EINVAL;
29876c364062SGeert Uytterhoeven 
2988c21b0837SAndy Shevchenko 	child = device_find_any_child(&ctlr->dev);
29896c364062SGeert Uytterhoeven 	if (child) {
29906c364062SGeert Uytterhoeven 		/* Remove registered slave */
29916c364062SGeert Uytterhoeven 		device_unregister(child);
29926c364062SGeert Uytterhoeven 		put_device(child);
29936c364062SGeert Uytterhoeven 	}
29946c364062SGeert Uytterhoeven 
29956c364062SGeert Uytterhoeven 	if (strcmp(name, "(null)")) {
29966c364062SGeert Uytterhoeven 		/* Register new slave */
29976c364062SGeert Uytterhoeven 		spi = spi_alloc_device(ctlr);
29986c364062SGeert Uytterhoeven 		if (!spi)
29996c364062SGeert Uytterhoeven 			return -ENOMEM;
30006c364062SGeert Uytterhoeven 
300151e99de5SWolfram Sang 		strscpy(spi->modalias, name, sizeof(spi->modalias));
30026c364062SGeert Uytterhoeven 
30036c364062SGeert Uytterhoeven 		rc = spi_add_device(spi);
30046c364062SGeert Uytterhoeven 		if (rc) {
30056c364062SGeert Uytterhoeven 			spi_dev_put(spi);
30066c364062SGeert Uytterhoeven 			return rc;
30076c364062SGeert Uytterhoeven 		}
30086c364062SGeert Uytterhoeven 	}
30096c364062SGeert Uytterhoeven 
30106c364062SGeert Uytterhoeven 	return count;
30116c364062SGeert Uytterhoeven }
30126c364062SGeert Uytterhoeven 
3013cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave);
30146c364062SGeert Uytterhoeven 
30156c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = {
30166c364062SGeert Uytterhoeven 	&dev_attr_slave.attr,
30176c364062SGeert Uytterhoeven 	NULL,
30186c364062SGeert Uytterhoeven };
30196c364062SGeert Uytterhoeven 
30206c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = {
30216c364062SGeert Uytterhoeven 	.attrs = spi_slave_attrs,
30226c364062SGeert Uytterhoeven };
30236c364062SGeert Uytterhoeven 
30246c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = {
30258caab75fSGeert Uytterhoeven 	&spi_controller_statistics_group,
30266c364062SGeert Uytterhoeven 	&spi_slave_group,
30276c364062SGeert Uytterhoeven 	NULL,
30286c364062SGeert Uytterhoeven };
30296c364062SGeert Uytterhoeven 
30306c364062SGeert Uytterhoeven static struct class spi_slave_class = {
30316c364062SGeert Uytterhoeven 	.name		= "spi_slave",
30328caab75fSGeert Uytterhoeven 	.dev_release	= spi_controller_release,
30336c364062SGeert Uytterhoeven 	.dev_groups	= spi_slave_groups,
30346c364062SGeert Uytterhoeven };
30356c364062SGeert Uytterhoeven #else
30366c364062SGeert Uytterhoeven extern struct class spi_slave_class;	/* dummy */
30376c364062SGeert Uytterhoeven #endif
30388ae12a0dSDavid Brownell 
30398ae12a0dSDavid Brownell /**
30406c364062SGeert Uytterhoeven  * __spi_alloc_controller - allocate an SPI master or slave controller
30418ae12a0dSDavid Brownell  * @dev: the controller, possibly using the platform_bus
304233e34dc6SDavid Brownell  * @size: how much zeroed driver-private data to allocate; the pointer to this
3043229e6af1SLukas Wunner  *	memory is in the driver_data field of the returned device, accessible
3044229e6af1SLukas Wunner  *	with spi_controller_get_devdata(); the memory is cacheline aligned;
3045229e6af1SLukas Wunner  *	drivers granting DMA access to portions of their private data need to
3046229e6af1SLukas Wunner  *	round up @size using ALIGN(size, dma_get_cache_alignment()).
30476c364062SGeert Uytterhoeven  * @slave: flag indicating whether to allocate an SPI master (false) or SPI
30486c364062SGeert Uytterhoeven  *	slave (true) controller
304933e34dc6SDavid Brownell  * Context: can sleep
30508ae12a0dSDavid Brownell  *
30516c364062SGeert Uytterhoeven  * This call is used only by SPI controller drivers, which are the
30528ae12a0dSDavid Brownell  * only ones directly touching chip registers.  It's how they allocate
30538caab75fSGeert Uytterhoeven  * an spi_controller structure, prior to calling spi_register_controller().
30548ae12a0dSDavid Brownell  *
305597d56dc6SJavier Martinez Canillas  * This must be called from context that can sleep.
30568ae12a0dSDavid Brownell  *
30576c364062SGeert Uytterhoeven  * The caller is responsible for assigning the bus number and initializing the
30588caab75fSGeert Uytterhoeven  * controller's methods before calling spi_register_controller(); and (after
30598caab75fSGeert Uytterhoeven  * errors adding the device) calling spi_controller_put() to prevent a memory
30608caab75fSGeert Uytterhoeven  * leak.
306197d56dc6SJavier Martinez Canillas  *
30626c364062SGeert Uytterhoeven  * Return: the SPI controller structure on success, else NULL.
30638ae12a0dSDavid Brownell  */
30648caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev,
30656c364062SGeert Uytterhoeven 					      unsigned int size, bool slave)
30668ae12a0dSDavid Brownell {
30678caab75fSGeert Uytterhoeven 	struct spi_controller	*ctlr;
3068229e6af1SLukas Wunner 	size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
30698ae12a0dSDavid Brownell 
30700c868461SDavid Brownell 	if (!dev)
30710c868461SDavid Brownell 		return NULL;
30720c868461SDavid Brownell 
3073229e6af1SLukas Wunner 	ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
30748caab75fSGeert Uytterhoeven 	if (!ctlr)
30758ae12a0dSDavid Brownell 		return NULL;
30768ae12a0dSDavid Brownell 
30778caab75fSGeert Uytterhoeven 	device_initialize(&ctlr->dev);
307816a8e2fbSUwe Kleine-König 	INIT_LIST_HEAD(&ctlr->queue);
307916a8e2fbSUwe Kleine-König 	spin_lock_init(&ctlr->queue_lock);
308016a8e2fbSUwe Kleine-König 	spin_lock_init(&ctlr->bus_lock_spinlock);
308116a8e2fbSUwe Kleine-König 	mutex_init(&ctlr->bus_lock_mutex);
308216a8e2fbSUwe Kleine-König 	mutex_init(&ctlr->io_mutex);
308316a8e2fbSUwe Kleine-König 	mutex_init(&ctlr->add_lock);
30848caab75fSGeert Uytterhoeven 	ctlr->bus_num = -1;
30858caab75fSGeert Uytterhoeven 	ctlr->num_chipselect = 1;
30868caab75fSGeert Uytterhoeven 	ctlr->slave = slave;
30876c364062SGeert Uytterhoeven 	if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
30888caab75fSGeert Uytterhoeven 		ctlr->dev.class = &spi_slave_class;
30896c364062SGeert Uytterhoeven 	else
30908caab75fSGeert Uytterhoeven 		ctlr->dev.class = &spi_master_class;
30918caab75fSGeert Uytterhoeven 	ctlr->dev.parent = dev;
30928caab75fSGeert Uytterhoeven 	pm_suspend_ignore_children(&ctlr->dev, true);
3093229e6af1SLukas Wunner 	spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
30948ae12a0dSDavid Brownell 
30958caab75fSGeert Uytterhoeven 	return ctlr;
30968ae12a0dSDavid Brownell }
30976c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller);
30988ae12a0dSDavid Brownell 
30995e844cc3SLukas Wunner static void devm_spi_release_controller(struct device *dev, void *ctlr)
31005e844cc3SLukas Wunner {
31015e844cc3SLukas Wunner 	spi_controller_put(*(struct spi_controller **)ctlr);
31025e844cc3SLukas Wunner }
31035e844cc3SLukas Wunner 
31045e844cc3SLukas Wunner /**
31055e844cc3SLukas Wunner  * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
31065e844cc3SLukas Wunner  * @dev: physical device of SPI controller
31075e844cc3SLukas Wunner  * @size: how much zeroed driver-private data to allocate
31085e844cc3SLukas Wunner  * @slave: whether to allocate an SPI master (false) or SPI slave (true)
31095e844cc3SLukas Wunner  * Context: can sleep
31105e844cc3SLukas Wunner  *
31115e844cc3SLukas Wunner  * Allocate an SPI controller and automatically release a reference on it
31125e844cc3SLukas Wunner  * when @dev is unbound from its driver.  Drivers are thus relieved from
31135e844cc3SLukas Wunner  * having to call spi_controller_put().
31145e844cc3SLukas Wunner  *
31155e844cc3SLukas Wunner  * The arguments to this function are identical to __spi_alloc_controller().
31165e844cc3SLukas Wunner  *
31175e844cc3SLukas Wunner  * Return: the SPI controller structure on success, else NULL.
31185e844cc3SLukas Wunner  */
31195e844cc3SLukas Wunner struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
31205e844cc3SLukas Wunner 						   unsigned int size,
31215e844cc3SLukas Wunner 						   bool slave)
31225e844cc3SLukas Wunner {
31235e844cc3SLukas Wunner 	struct spi_controller **ptr, *ctlr;
31245e844cc3SLukas Wunner 
31255e844cc3SLukas Wunner 	ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr),
31265e844cc3SLukas Wunner 			   GFP_KERNEL);
31275e844cc3SLukas Wunner 	if (!ptr)
31285e844cc3SLukas Wunner 		return NULL;
31295e844cc3SLukas Wunner 
31305e844cc3SLukas Wunner 	ctlr = __spi_alloc_controller(dev, size, slave);
31315e844cc3SLukas Wunner 	if (ctlr) {
3132794aaf01SWilliam A. Kennington III 		ctlr->devm_allocated = true;
31335e844cc3SLukas Wunner 		*ptr = ctlr;
31345e844cc3SLukas Wunner 		devres_add(dev, ptr);
31355e844cc3SLukas Wunner 	} else {
31365e844cc3SLukas Wunner 		devres_free(ptr);
31375e844cc3SLukas Wunner 	}
31385e844cc3SLukas Wunner 
31395e844cc3SLukas Wunner 	return ctlr;
31405e844cc3SLukas Wunner }
31415e844cc3SLukas Wunner EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
31425e844cc3SLukas Wunner 
3143f3186dd8SLinus Walleij /**
3144f3186dd8SLinus Walleij  * spi_get_gpio_descs() - grab chip select GPIOs for the master
3145f3186dd8SLinus Walleij  * @ctlr: The SPI master to grab GPIO descriptors for
3146f3186dd8SLinus Walleij  */
3147f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr)
3148f3186dd8SLinus Walleij {
3149f3186dd8SLinus Walleij 	int nb, i;
3150f3186dd8SLinus Walleij 	struct gpio_desc **cs;
3151f3186dd8SLinus Walleij 	struct device *dev = &ctlr->dev;
31527d93aecdSGeert Uytterhoeven 	unsigned long native_cs_mask = 0;
31537d93aecdSGeert Uytterhoeven 	unsigned int num_cs_gpios = 0;
3154f3186dd8SLinus Walleij 
3155f3186dd8SLinus Walleij 	nb = gpiod_count(dev, "cs");
315631ed8ebcSAndy Shevchenko 	if (nb < 0) {
3157f3186dd8SLinus Walleij 		/* No GPIOs at all is fine, else return the error */
315831ed8ebcSAndy Shevchenko 		if (nb == -ENOENT)
3159f3186dd8SLinus Walleij 			return 0;
3160f3186dd8SLinus Walleij 		return nb;
316131ed8ebcSAndy Shevchenko 	}
316231ed8ebcSAndy Shevchenko 
316331ed8ebcSAndy Shevchenko 	ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
3164f3186dd8SLinus Walleij 
3165f3186dd8SLinus Walleij 	cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
3166f3186dd8SLinus Walleij 			  GFP_KERNEL);
3167f3186dd8SLinus Walleij 	if (!cs)
3168f3186dd8SLinus Walleij 		return -ENOMEM;
3169f3186dd8SLinus Walleij 	ctlr->cs_gpiods = cs;
3170f3186dd8SLinus Walleij 
3171f3186dd8SLinus Walleij 	for (i = 0; i < nb; i++) {
3172f3186dd8SLinus Walleij 		/*
3173f3186dd8SLinus Walleij 		 * Most chipselects are active low, the inverted
3174f3186dd8SLinus Walleij 		 * semantics are handled by special quirks in gpiolib,
3175f3186dd8SLinus Walleij 		 * so initializing them GPIOD_OUT_LOW here means
3176f3186dd8SLinus Walleij 		 * "unasserted", in most cases this will drive the physical
3177f3186dd8SLinus Walleij 		 * line high.
3178f3186dd8SLinus Walleij 		 */
3179f3186dd8SLinus Walleij 		cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
3180f3186dd8SLinus Walleij 						      GPIOD_OUT_LOW);
31811723fdecSGeert Uytterhoeven 		if (IS_ERR(cs[i]))
31821723fdecSGeert Uytterhoeven 			return PTR_ERR(cs[i]);
3183f3186dd8SLinus Walleij 
3184f3186dd8SLinus Walleij 		if (cs[i]) {
3185f3186dd8SLinus Walleij 			/*
3186f3186dd8SLinus Walleij 			 * If we find a CS GPIO, name it after the device and
3187f3186dd8SLinus Walleij 			 * chip select line.
3188f3186dd8SLinus Walleij 			 */
3189f3186dd8SLinus Walleij 			char *gpioname;
3190f3186dd8SLinus Walleij 
3191f3186dd8SLinus Walleij 			gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
3192f3186dd8SLinus Walleij 						  dev_name(dev), i);
3193f3186dd8SLinus Walleij 			if (!gpioname)
3194f3186dd8SLinus Walleij 				return -ENOMEM;
3195f3186dd8SLinus Walleij 			gpiod_set_consumer_name(cs[i], gpioname);
31967d93aecdSGeert Uytterhoeven 			num_cs_gpios++;
31977d93aecdSGeert Uytterhoeven 			continue;
3198f3186dd8SLinus Walleij 		}
31997d93aecdSGeert Uytterhoeven 
32007d93aecdSGeert Uytterhoeven 		if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
32017d93aecdSGeert Uytterhoeven 			dev_err(dev, "Invalid native chip select %d\n", i);
32027d93aecdSGeert Uytterhoeven 			return -EINVAL;
32037d93aecdSGeert Uytterhoeven 		}
32047d93aecdSGeert Uytterhoeven 		native_cs_mask |= BIT(i);
32057d93aecdSGeert Uytterhoeven 	}
32067d93aecdSGeert Uytterhoeven 
3207f60d7270SAndy Shevchenko 	ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
3208dbaca8e5SAndy Shevchenko 
320982238d2cSAndy Shevchenko 	if ((ctlr->flags & SPI_CONTROLLER_GPIO_SS) && num_cs_gpios &&
3210dbaca8e5SAndy Shevchenko 	    ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
32117d93aecdSGeert Uytterhoeven 		dev_err(dev, "No unused native chip select available\n");
32127d93aecdSGeert Uytterhoeven 		return -EINVAL;
3213f3186dd8SLinus Walleij 	}
3214f3186dd8SLinus Walleij 
3215f3186dd8SLinus Walleij 	return 0;
3216f3186dd8SLinus Walleij }
3217f3186dd8SLinus Walleij 
3218bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr)
3219bdf3a3b5SBoris Brezillon {
3220bdf3a3b5SBoris Brezillon 	/*
3221b5932f5cSBoris Brezillon 	 * The controller may implement only the high-level SPI-memory like
3222b5932f5cSBoris Brezillon 	 * operations if it does not support regular SPI transfers, and this is
3223b5932f5cSBoris Brezillon 	 * valid use case.
322476a85704SWilliam Zhang 	 * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least
322576a85704SWilliam Zhang 	 * one of the ->transfer_xxx() method be implemented.
3226bdf3a3b5SBoris Brezillon 	 */
322720064c47SWilliam Zhang 	if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
322876a85704SWilliam Zhang 		if (!ctlr->transfer && !ctlr->transfer_one &&
3229b5932f5cSBoris Brezillon 		   !ctlr->transfer_one_message) {
3230b5932f5cSBoris Brezillon 			return -EINVAL;
3231b5932f5cSBoris Brezillon 		}
323276a85704SWilliam Zhang 	}
3233bdf3a3b5SBoris Brezillon 
3234bdf3a3b5SBoris Brezillon 	return 0;
3235bdf3a3b5SBoris Brezillon }
3236bdf3a3b5SBoris Brezillon 
3237440c4733SAndy Shevchenko /* Allocate dynamic bus number using Linux idr */
3238440c4733SAndy Shevchenko static int spi_controller_id_alloc(struct spi_controller *ctlr, int start, int end)
3239440c4733SAndy Shevchenko {
3240440c4733SAndy Shevchenko 	int id;
3241440c4733SAndy Shevchenko 
3242440c4733SAndy Shevchenko 	mutex_lock(&board_lock);
3243440c4733SAndy Shevchenko 	id = idr_alloc(&spi_master_idr, ctlr, start, end, GFP_KERNEL);
3244440c4733SAndy Shevchenko 	mutex_unlock(&board_lock);
3245440c4733SAndy Shevchenko 	if (WARN(id < 0, "couldn't get idr"))
3246440c4733SAndy Shevchenko 		return id == -ENOSPC ? -EBUSY : id;
3247440c4733SAndy Shevchenko 	ctlr->bus_num = id;
3248440c4733SAndy Shevchenko 	return 0;
3249440c4733SAndy Shevchenko }
3250440c4733SAndy Shevchenko 
32518ae12a0dSDavid Brownell /**
32528caab75fSGeert Uytterhoeven  * spi_register_controller - register SPI master or slave controller
32538caab75fSGeert Uytterhoeven  * @ctlr: initialized master, originally from spi_alloc_master() or
32548caab75fSGeert Uytterhoeven  *	spi_alloc_slave()
325533e34dc6SDavid Brownell  * Context: can sleep
32568ae12a0dSDavid Brownell  *
32578caab75fSGeert Uytterhoeven  * SPI controllers connect to their drivers using some non-SPI bus,
32588ae12a0dSDavid Brownell  * such as the platform bus.  The final stage of probe() in that code
32598caab75fSGeert Uytterhoeven  * includes calling spi_register_controller() to hook up to this SPI bus glue.
32608ae12a0dSDavid Brownell  *
32618ae12a0dSDavid Brownell  * SPI controllers use board specific (often SOC specific) bus numbers,
32628ae12a0dSDavid Brownell  * and board-specific addressing for SPI devices combines those numbers
32638ae12a0dSDavid Brownell  * with chip select numbers.  Since SPI does not directly support dynamic
32648ae12a0dSDavid Brownell  * device identification, boards need configuration tables telling which
32658ae12a0dSDavid Brownell  * chip is at which address.
32668ae12a0dSDavid Brownell  *
32678ae12a0dSDavid Brownell  * This must be called from context that can sleep.  It returns zero on
32688caab75fSGeert Uytterhoeven  * success, else a negative error code (dropping the controller's refcount).
32690c868461SDavid Brownell  * After a successful return, the caller is responsible for calling
32708caab75fSGeert Uytterhoeven  * spi_unregister_controller().
327197d56dc6SJavier Martinez Canillas  *
327297d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
32738ae12a0dSDavid Brownell  */
32748caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr)
32758ae12a0dSDavid Brownell {
32768caab75fSGeert Uytterhoeven 	struct device		*dev = ctlr->dev.parent;
32772b9603a0SFeng Tang 	struct boardinfo	*bi;
3278440c4733SAndy Shevchenko 	int			first_dynamic;
3279b93318a2SSergei Shtylyov 	int			status;
32804d8ff6b0SAmit Kumar Mahapatra 	int			idx;
32818ae12a0dSDavid Brownell 
32820c868461SDavid Brownell 	if (!dev)
32830c868461SDavid Brownell 		return -ENODEV;
32840c868461SDavid Brownell 
3285bdf3a3b5SBoris Brezillon 	/*
3286bdf3a3b5SBoris Brezillon 	 * Make sure all necessary hooks are implemented before registering
3287bdf3a3b5SBoris Brezillon 	 * the SPI controller.
3288bdf3a3b5SBoris Brezillon 	 */
3289bdf3a3b5SBoris Brezillon 	status = spi_controller_check_ops(ctlr);
3290bdf3a3b5SBoris Brezillon 	if (status)
3291bdf3a3b5SBoris Brezillon 		return status;
3292bdf3a3b5SBoris Brezillon 
3293440c4733SAndy Shevchenko 	if (ctlr->bus_num < 0)
3294440c4733SAndy Shevchenko 		ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi");
329504b2d03aSGeert Uytterhoeven 	if (ctlr->bus_num >= 0) {
329695c8222fSDavid Jander 		/* Devices with a fixed bus num must check-in with the num */
3297440c4733SAndy Shevchenko 		status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1);
3298440c4733SAndy Shevchenko 		if (status)
3299440c4733SAndy Shevchenko 			return status;
33009b61e302SSuniel Mahesh 	}
33018caab75fSGeert Uytterhoeven 	if (ctlr->bus_num < 0) {
330242bdd706SLucas Stach 		first_dynamic = of_alias_get_highest_id("spi");
330342bdd706SLucas Stach 		if (first_dynamic < 0)
330442bdd706SLucas Stach 			first_dynamic = 0;
330542bdd706SLucas Stach 		else
330642bdd706SLucas Stach 			first_dynamic++;
330742bdd706SLucas Stach 
3308440c4733SAndy Shevchenko 		status = spi_controller_id_alloc(ctlr, first_dynamic, 0);
3309440c4733SAndy Shevchenko 		if (status)
3310440c4733SAndy Shevchenko 			return status;
33118ae12a0dSDavid Brownell 	}
33128caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 0;
33138caab75fSGeert Uytterhoeven 	init_completion(&ctlr->xfer_completion);
331469fa9590SDavid Jander 	init_completion(&ctlr->cur_msg_completion);
33158caab75fSGeert Uytterhoeven 	if (!ctlr->max_dma_len)
33168caab75fSGeert Uytterhoeven 		ctlr->max_dma_len = INT_MAX;
3317cf32b71eSErnst Schwab 
3318350de7ceSAndy Shevchenko 	/*
3319350de7ceSAndy Shevchenko 	 * Register the device, then userspace will see it.
3320350de7ceSAndy Shevchenko 	 * Registration fails if the bus ID is in use.
33218ae12a0dSDavid Brownell 	 */
33228caab75fSGeert Uytterhoeven 	dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
33230a919ae4SAndrey Smirnov 
3324f48dc6b9SLinus Walleij 	if (!spi_controller_is_slave(ctlr) && ctlr->use_gpio_descriptors) {
33250a919ae4SAndrey Smirnov 		status = spi_get_gpio_descs(ctlr);
33260a919ae4SAndrey Smirnov 		if (status)
3327f9981d4fSAaro Koskinen 			goto free_bus_id;
33280a919ae4SAndrey Smirnov 		/*
33290a919ae4SAndrey Smirnov 		 * A controller using GPIO descriptors always
33300a919ae4SAndrey Smirnov 		 * supports SPI_CS_HIGH if need be.
33310a919ae4SAndrey Smirnov 		 */
33320a919ae4SAndrey Smirnov 		ctlr->mode_bits |= SPI_CS_HIGH;
33330a919ae4SAndrey Smirnov 	}
33340a919ae4SAndrey Smirnov 
3335f9481b08STudor Ambarus 	/*
3336f9481b08STudor Ambarus 	 * Even if it's just one always-selected device, there must
3337f9481b08STudor Ambarus 	 * be at least one chipselect.
3338f9481b08STudor Ambarus 	 */
3339f9981d4fSAaro Koskinen 	if (!ctlr->num_chipselect) {
3340f9981d4fSAaro Koskinen 		status = -EINVAL;
3341f9981d4fSAaro Koskinen 		goto free_bus_id;
3342f9981d4fSAaro Koskinen 	}
3343f9481b08STudor Ambarus 
3344be84be4aSAndy Shevchenko 	/* Setting last_cs to SPI_INVALID_CS means no chip selected */
33454d8ff6b0SAmit Kumar Mahapatra 	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
3346be84be4aSAndy Shevchenko 		ctlr->last_cs[idx] = SPI_INVALID_CS;
33476bb477dfSYun Zhou 
33488caab75fSGeert Uytterhoeven 	status = device_add(&ctlr->dev);
3349f9981d4fSAaro Koskinen 	if (status < 0)
3350f9981d4fSAaro Koskinen 		goto free_bus_id;
33519b61e302SSuniel Mahesh 	dev_dbg(dev, "registered %s %s\n",
33528caab75fSGeert Uytterhoeven 			spi_controller_is_slave(ctlr) ? "slave" : "master",
33539b61e302SSuniel Mahesh 			dev_name(&ctlr->dev));
33548ae12a0dSDavid Brownell 
3355b5932f5cSBoris Brezillon 	/*
3356b5932f5cSBoris Brezillon 	 * If we're using a queued driver, start the queue. Note that we don't
3357b5932f5cSBoris Brezillon 	 * need the queueing logic if the driver is only supporting high-level
3358b5932f5cSBoris Brezillon 	 * memory operations.
3359b5932f5cSBoris Brezillon 	 */
3360b5932f5cSBoris Brezillon 	if (ctlr->transfer) {
33618caab75fSGeert Uytterhoeven 		dev_info(dev, "controller is unqueued, this is deprecated\n");
3362b5932f5cSBoris Brezillon 	} else if (ctlr->transfer_one || ctlr->transfer_one_message) {
33638caab75fSGeert Uytterhoeven 		status = spi_controller_initialize_queue(ctlr);
3364ffbbdd21SLinus Walleij 		if (status) {
33658caab75fSGeert Uytterhoeven 			device_del(&ctlr->dev);
3366f9981d4fSAaro Koskinen 			goto free_bus_id;
3367ffbbdd21SLinus Walleij 		}
3368ffbbdd21SLinus Walleij 	}
336995c8222fSDavid Jander 	/* Add statistics */
33706598b91bSDavid Jander 	ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev);
33716598b91bSDavid Jander 	if (!ctlr->pcpu_statistics) {
33726598b91bSDavid Jander 		dev_err(dev, "Error allocating per-cpu statistics\n");
3373d52b095bSDan Carpenter 		status = -ENOMEM;
33746598b91bSDavid Jander 		goto destroy_queue;
33756598b91bSDavid Jander 	}
3376ffbbdd21SLinus Walleij 
33772b9603a0SFeng Tang 	mutex_lock(&board_lock);
33788caab75fSGeert Uytterhoeven 	list_add_tail(&ctlr->list, &spi_controller_list);
33792b9603a0SFeng Tang 	list_for_each_entry(bi, &board_list, list)
33808caab75fSGeert Uytterhoeven 		spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
33812b9603a0SFeng Tang 	mutex_unlock(&board_lock);
33822b9603a0SFeng Tang 
338364bee4d2SMika Westerberg 	/* Register devices from the device tree and ACPI */
33848caab75fSGeert Uytterhoeven 	of_register_spi_devices(ctlr);
33858caab75fSGeert Uytterhoeven 	acpi_register_spi_devices(ctlr);
3386f9981d4fSAaro Koskinen 	return status;
3387f9981d4fSAaro Koskinen 
33886598b91bSDavid Jander destroy_queue:
33896598b91bSDavid Jander 	spi_destroy_queue(ctlr);
3390f9981d4fSAaro Koskinen free_bus_id:
3391f9981d4fSAaro Koskinen 	mutex_lock(&board_lock);
3392f9981d4fSAaro Koskinen 	idr_remove(&spi_master_idr, ctlr->bus_num);
3393f9981d4fSAaro Koskinen 	mutex_unlock(&board_lock);
33948ae12a0dSDavid Brownell 	return status;
33958ae12a0dSDavid Brownell }
33968caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller);
33978ae12a0dSDavid Brownell 
339843cc5a0aSYang Yingliang static void devm_spi_unregister(struct device *dev, void *res)
3399666d5b4cSMark Brown {
340043cc5a0aSYang Yingliang 	spi_unregister_controller(*(struct spi_controller **)res);
3401666d5b4cSMark Brown }
3402666d5b4cSMark Brown 
3403666d5b4cSMark Brown /**
34048caab75fSGeert Uytterhoeven  * devm_spi_register_controller - register managed SPI master or slave
34058caab75fSGeert Uytterhoeven  *	controller
34068caab75fSGeert Uytterhoeven  * @dev:    device managing SPI controller
34078caab75fSGeert Uytterhoeven  * @ctlr: initialized controller, originally from spi_alloc_master() or
34088caab75fSGeert Uytterhoeven  *	spi_alloc_slave()
3409666d5b4cSMark Brown  * Context: can sleep
3410666d5b4cSMark Brown  *
34118caab75fSGeert Uytterhoeven  * Register a SPI device as with spi_register_controller() which will
341268b892f1SJohan Hovold  * automatically be unregistered and freed.
341397d56dc6SJavier Martinez Canillas  *
341497d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
3415666d5b4cSMark Brown  */
34168caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev,
34178caab75fSGeert Uytterhoeven 				 struct spi_controller *ctlr)
3418666d5b4cSMark Brown {
341943cc5a0aSYang Yingliang 	struct spi_controller **ptr;
3420666d5b4cSMark Brown 	int ret;
3421666d5b4cSMark Brown 
342243cc5a0aSYang Yingliang 	ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
342343cc5a0aSYang Yingliang 	if (!ptr)
342443cc5a0aSYang Yingliang 		return -ENOMEM;
342559ebbe40STian Tao 
342643cc5a0aSYang Yingliang 	ret = spi_register_controller(ctlr);
342743cc5a0aSYang Yingliang 	if (!ret) {
342843cc5a0aSYang Yingliang 		*ptr = ctlr;
342943cc5a0aSYang Yingliang 		devres_add(dev, ptr);
343043cc5a0aSYang Yingliang 	} else {
343143cc5a0aSYang Yingliang 		devres_free(ptr);
343243cc5a0aSYang Yingliang 	}
343343cc5a0aSYang Yingliang 
343443cc5a0aSYang Yingliang 	return ret;
3435666d5b4cSMark Brown }
34368caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller);
3437666d5b4cSMark Brown 
343834860089SDavid Lamparter static int __unregister(struct device *dev, void *null)
34398ae12a0dSDavid Brownell {
34400c868461SDavid Brownell 	spi_unregister_device(to_spi_device(dev));
34418ae12a0dSDavid Brownell 	return 0;
34428ae12a0dSDavid Brownell }
34438ae12a0dSDavid Brownell 
34448ae12a0dSDavid Brownell /**
34458caab75fSGeert Uytterhoeven  * spi_unregister_controller - unregister SPI master or slave controller
34468caab75fSGeert Uytterhoeven  * @ctlr: the controller being unregistered
344733e34dc6SDavid Brownell  * Context: can sleep
34488ae12a0dSDavid Brownell  *
34498caab75fSGeert Uytterhoeven  * This call is used only by SPI controller drivers, which are the
34508ae12a0dSDavid Brownell  * only ones directly touching chip registers.
34518ae12a0dSDavid Brownell  *
34528ae12a0dSDavid Brownell  * This must be called from context that can sleep.
345368b892f1SJohan Hovold  *
345468b892f1SJohan Hovold  * Note that this function also drops a reference to the controller.
34558ae12a0dSDavid Brownell  */
34568caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr)
34578ae12a0dSDavid Brownell {
34589b61e302SSuniel Mahesh 	struct spi_controller *found;
345967f7b278SJohan Hovold 	int id = ctlr->bus_num;
346089fc9a1aSJeff Garzik 
3461ddf75be4SLukas Wunner 	/* Prevent addition of new devices, unregister existing ones */
3462ddf75be4SLukas Wunner 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
34636098475dSMark Brown 		mutex_lock(&ctlr->add_lock);
3464ddf75be4SLukas Wunner 
346584855678SLukas Wunner 	device_for_each_child(&ctlr->dev, NULL, __unregister);
346684855678SLukas Wunner 
34679b61e302SSuniel Mahesh 	/* First make sure that this controller was ever added */
34689b61e302SSuniel Mahesh 	mutex_lock(&board_lock);
346967f7b278SJohan Hovold 	found = idr_find(&spi_master_idr, id);
34709b61e302SSuniel Mahesh 	mutex_unlock(&board_lock);
34718caab75fSGeert Uytterhoeven 	if (ctlr->queued) {
34728caab75fSGeert Uytterhoeven 		if (spi_destroy_queue(ctlr))
34738caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "queue remove failed\n");
3474ffbbdd21SLinus Walleij 	}
34752b9603a0SFeng Tang 	mutex_lock(&board_lock);
34768caab75fSGeert Uytterhoeven 	list_del(&ctlr->list);
34772b9603a0SFeng Tang 	mutex_unlock(&board_lock);
34782b9603a0SFeng Tang 
34795e844cc3SLukas Wunner 	device_del(&ctlr->dev);
34805e844cc3SLukas Wunner 
348195c8222fSDavid Jander 	/* Free bus id */
34829b61e302SSuniel Mahesh 	mutex_lock(&board_lock);
3483613bd1eaSJarkko Nikula 	if (found == ctlr)
348467f7b278SJohan Hovold 		idr_remove(&spi_master_idr, id);
34859b61e302SSuniel Mahesh 	mutex_unlock(&board_lock);
3486ddf75be4SLukas Wunner 
3487ddf75be4SLukas Wunner 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
34886098475dSMark Brown 		mutex_unlock(&ctlr->add_lock);
34896c53b45cSMichael Walle 
3490702ca026SAndy Shevchenko 	/*
3491702ca026SAndy Shevchenko 	 * Release the last reference on the controller if its driver
34926c53b45cSMichael Walle 	 * has not yet been converted to devm_spi_alloc_master/slave().
34936c53b45cSMichael Walle 	 */
34946c53b45cSMichael Walle 	if (!ctlr->devm_allocated)
34956c53b45cSMichael Walle 		put_device(&ctlr->dev);
34968ae12a0dSDavid Brownell }
34978caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller);
34988ae12a0dSDavid Brownell 
3499bef4a48fSMark Hasemeyer static inline int __spi_check_suspended(const struct spi_controller *ctlr)
3500bef4a48fSMark Hasemeyer {
3501bef4a48fSMark Hasemeyer 	return ctlr->flags & SPI_CONTROLLER_SUSPENDED ? -ESHUTDOWN : 0;
3502bef4a48fSMark Hasemeyer }
3503bef4a48fSMark Hasemeyer 
3504bef4a48fSMark Hasemeyer static inline void __spi_mark_suspended(struct spi_controller *ctlr)
3505bef4a48fSMark Hasemeyer {
3506bef4a48fSMark Hasemeyer 	mutex_lock(&ctlr->bus_lock_mutex);
3507bef4a48fSMark Hasemeyer 	ctlr->flags |= SPI_CONTROLLER_SUSPENDED;
3508bef4a48fSMark Hasemeyer 	mutex_unlock(&ctlr->bus_lock_mutex);
3509bef4a48fSMark Hasemeyer }
3510bef4a48fSMark Hasemeyer 
3511bef4a48fSMark Hasemeyer static inline void __spi_mark_resumed(struct spi_controller *ctlr)
3512bef4a48fSMark Hasemeyer {
3513bef4a48fSMark Hasemeyer 	mutex_lock(&ctlr->bus_lock_mutex);
3514bef4a48fSMark Hasemeyer 	ctlr->flags &= ~SPI_CONTROLLER_SUSPENDED;
3515bef4a48fSMark Hasemeyer 	mutex_unlock(&ctlr->bus_lock_mutex);
3516bef4a48fSMark Hasemeyer }
3517bef4a48fSMark Hasemeyer 
35188caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr)
3519ffbbdd21SLinus Walleij {
3520bef4a48fSMark Hasemeyer 	int ret = 0;
3521ffbbdd21SLinus Walleij 
35228caab75fSGeert Uytterhoeven 	/* Basically no-ops for non-queued controllers */
3523bef4a48fSMark Hasemeyer 	if (ctlr->queued) {
35248caab75fSGeert Uytterhoeven 		ret = spi_stop_queue(ctlr);
3525ffbbdd21SLinus Walleij 		if (ret)
35268caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "queue stop failed\n");
3527bef4a48fSMark Hasemeyer 	}
3528ffbbdd21SLinus Walleij 
3529bef4a48fSMark Hasemeyer 	__spi_mark_suspended(ctlr);
3530ffbbdd21SLinus Walleij 	return ret;
3531ffbbdd21SLinus Walleij }
35328caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend);
3533ffbbdd21SLinus Walleij 
35348caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr)
3535ffbbdd21SLinus Walleij {
3536bef4a48fSMark Hasemeyer 	int ret = 0;
3537ffbbdd21SLinus Walleij 
3538bef4a48fSMark Hasemeyer 	__spi_mark_resumed(ctlr);
3539ffbbdd21SLinus Walleij 
3540bef4a48fSMark Hasemeyer 	if (ctlr->queued) {
35418caab75fSGeert Uytterhoeven 		ret = spi_start_queue(ctlr);
3542ffbbdd21SLinus Walleij 		if (ret)
35438caab75fSGeert Uytterhoeven 			dev_err(&ctlr->dev, "queue restart failed\n");
3544bef4a48fSMark Hasemeyer 	}
3545ffbbdd21SLinus Walleij 	return ret;
3546ffbbdd21SLinus Walleij }
35478caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume);
3548ffbbdd21SLinus Walleij 
35498ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
35508ae12a0dSDavid Brownell 
3551523baf5aSMartin Sperl /* Core methods for spi_message alterations */
3552523baf5aSMartin Sperl 
35538caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr,
3554523baf5aSMartin Sperl 					    struct spi_message *msg,
3555523baf5aSMartin Sperl 					    void *res)
3556523baf5aSMartin Sperl {
3557523baf5aSMartin Sperl 	struct spi_replaced_transfers *rxfer = res;
3558523baf5aSMartin Sperl 	size_t i;
3559523baf5aSMartin Sperl 
356095c8222fSDavid Jander 	/* Call extra callback if requested */
3561523baf5aSMartin Sperl 	if (rxfer->release)
35628caab75fSGeert Uytterhoeven 		rxfer->release(ctlr, msg, res);
3563523baf5aSMartin Sperl 
356495c8222fSDavid Jander 	/* Insert replaced transfers back into the message */
3565523baf5aSMartin Sperl 	list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
3566523baf5aSMartin Sperl 
356795c8222fSDavid Jander 	/* Remove the formerly inserted entries */
3568523baf5aSMartin Sperl 	for (i = 0; i < rxfer->inserted; i++)
3569523baf5aSMartin Sperl 		list_del(&rxfer->inserted_transfers[i].transfer_list);
3570523baf5aSMartin Sperl }
3571523baf5aSMartin Sperl 
3572523baf5aSMartin Sperl /**
3573523baf5aSMartin Sperl  * spi_replace_transfers - replace transfers with several transfers
3574523baf5aSMartin Sperl  *                         and register change with spi_message.resources
3575523baf5aSMartin Sperl  * @msg:           the spi_message we work upon
3576523baf5aSMartin Sperl  * @xfer_first:    the first spi_transfer we want to replace
3577523baf5aSMartin Sperl  * @remove:        number of transfers to remove
3578523baf5aSMartin Sperl  * @insert:        the number of transfers we want to insert instead
3579523baf5aSMartin Sperl  * @release:       extra release code necessary in some circumstances
3580523baf5aSMartin Sperl  * @extradatasize: extra data to allocate (with alignment guarantees
3581523baf5aSMartin Sperl  *                 of struct @spi_transfer)
358205885397SMartin Sperl  * @gfp:           gfp flags
3583523baf5aSMartin Sperl  *
3584523baf5aSMartin Sperl  * Returns: pointer to @spi_replaced_transfers,
3585523baf5aSMartin Sperl  *          PTR_ERR(...) in case of errors.
3586523baf5aSMartin Sperl  */
3587da21fde0SUwe Kleine-König static struct spi_replaced_transfers *spi_replace_transfers(
3588523baf5aSMartin Sperl 	struct spi_message *msg,
3589523baf5aSMartin Sperl 	struct spi_transfer *xfer_first,
3590523baf5aSMartin Sperl 	size_t remove,
3591523baf5aSMartin Sperl 	size_t insert,
3592523baf5aSMartin Sperl 	spi_replaced_release_t release,
3593523baf5aSMartin Sperl 	size_t extradatasize,
3594523baf5aSMartin Sperl 	gfp_t gfp)
3595523baf5aSMartin Sperl {
3596523baf5aSMartin Sperl 	struct spi_replaced_transfers *rxfer;
3597523baf5aSMartin Sperl 	struct spi_transfer *xfer;
3598523baf5aSMartin Sperl 	size_t i;
3599523baf5aSMartin Sperl 
360095c8222fSDavid Jander 	/* Allocate the structure using spi_res */
3601523baf5aSMartin Sperl 	rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
3602aef97522SGustavo A. R. Silva 			      struct_size(rxfer, inserted_transfers, insert)
3603523baf5aSMartin Sperl 			      + extradatasize,
3604523baf5aSMartin Sperl 			      gfp);
3605523baf5aSMartin Sperl 	if (!rxfer)
3606523baf5aSMartin Sperl 		return ERR_PTR(-ENOMEM);
3607523baf5aSMartin Sperl 
360895c8222fSDavid Jander 	/* The release code to invoke before running the generic release */
3609523baf5aSMartin Sperl 	rxfer->release = release;
3610523baf5aSMartin Sperl 
361195c8222fSDavid Jander 	/* Assign extradata */
3612523baf5aSMartin Sperl 	if (extradatasize)
3613523baf5aSMartin Sperl 		rxfer->extradata =
3614523baf5aSMartin Sperl 			&rxfer->inserted_transfers[insert];
3615523baf5aSMartin Sperl 
361695c8222fSDavid Jander 	/* Init the replaced_transfers list */
3617523baf5aSMartin Sperl 	INIT_LIST_HEAD(&rxfer->replaced_transfers);
3618523baf5aSMartin Sperl 
3619350de7ceSAndy Shevchenko 	/*
3620350de7ceSAndy Shevchenko 	 * Assign the list_entry after which we should reinsert
3621523baf5aSMartin Sperl 	 * the @replaced_transfers - it may be spi_message.messages!
3622523baf5aSMartin Sperl 	 */
3623523baf5aSMartin Sperl 	rxfer->replaced_after = xfer_first->transfer_list.prev;
3624523baf5aSMartin Sperl 
362595c8222fSDavid Jander 	/* Remove the requested number of transfers */
3626523baf5aSMartin Sperl 	for (i = 0; i < remove; i++) {
3627350de7ceSAndy Shevchenko 		/*
3628350de7ceSAndy Shevchenko 		 * If the entry after replaced_after it is msg->transfers
3629523baf5aSMartin Sperl 		 * then we have been requested to remove more transfers
3630350de7ceSAndy Shevchenko 		 * than are in the list.
3631523baf5aSMartin Sperl 		 */
3632523baf5aSMartin Sperl 		if (rxfer->replaced_after->next == &msg->transfers) {
3633523baf5aSMartin Sperl 			dev_err(&msg->spi->dev,
3634523baf5aSMartin Sperl 				"requested to remove more spi_transfers than are available\n");
363595c8222fSDavid Jander 			/* Insert replaced transfers back into the message */
3636523baf5aSMartin Sperl 			list_splice(&rxfer->replaced_transfers,
3637523baf5aSMartin Sperl 				    rxfer->replaced_after);
3638523baf5aSMartin Sperl 
363995c8222fSDavid Jander 			/* Free the spi_replace_transfer structure... */
3640523baf5aSMartin Sperl 			spi_res_free(rxfer);
3641523baf5aSMartin Sperl 
364295c8222fSDavid Jander 			/* ...and return with an error */
3643523baf5aSMartin Sperl 			return ERR_PTR(-EINVAL);
3644523baf5aSMartin Sperl 		}
3645523baf5aSMartin Sperl 
3646350de7ceSAndy Shevchenko 		/*
3647350de7ceSAndy Shevchenko 		 * Remove the entry after replaced_after from list of
3648350de7ceSAndy Shevchenko 		 * transfers and add it to list of replaced_transfers.
3649523baf5aSMartin Sperl 		 */
3650523baf5aSMartin Sperl 		list_move_tail(rxfer->replaced_after->next,
3651523baf5aSMartin Sperl 			       &rxfer->replaced_transfers);
3652523baf5aSMartin Sperl 	}
3653523baf5aSMartin Sperl 
3654350de7ceSAndy Shevchenko 	/*
3655350de7ceSAndy Shevchenko 	 * Create copy of the given xfer with identical settings
3656350de7ceSAndy Shevchenko 	 * based on the first transfer to get removed.
3657523baf5aSMartin Sperl 	 */
3658523baf5aSMartin Sperl 	for (i = 0; i < insert; i++) {
365995c8222fSDavid Jander 		/* We need to run in reverse order */
3660523baf5aSMartin Sperl 		xfer = &rxfer->inserted_transfers[insert - 1 - i];
3661523baf5aSMartin Sperl 
366295c8222fSDavid Jander 		/* Copy all spi_transfer data */
3663523baf5aSMartin Sperl 		memcpy(xfer, xfer_first, sizeof(*xfer));
3664523baf5aSMartin Sperl 
366595c8222fSDavid Jander 		/* Add to list */
3666523baf5aSMartin Sperl 		list_add(&xfer->transfer_list, rxfer->replaced_after);
3667523baf5aSMartin Sperl 
366895c8222fSDavid Jander 		/* Clear cs_change and delay for all but the last */
3669523baf5aSMartin Sperl 		if (i) {
3670523baf5aSMartin Sperl 			xfer->cs_change = false;
3671bebcfd27SAlexandru Ardelean 			xfer->delay.value = 0;
3672523baf5aSMartin Sperl 		}
3673523baf5aSMartin Sperl 	}
3674523baf5aSMartin Sperl 
367595c8222fSDavid Jander 	/* Set up inserted... */
3676523baf5aSMartin Sperl 	rxfer->inserted = insert;
3677523baf5aSMartin Sperl 
367895c8222fSDavid Jander 	/* ...and register it with spi_res/spi_message */
3679523baf5aSMartin Sperl 	spi_res_add(msg, rxfer);
3680523baf5aSMartin Sperl 
3681523baf5aSMartin Sperl 	return rxfer;
3682523baf5aSMartin Sperl }
3683523baf5aSMartin Sperl 
36848caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
3685d9f12122SMartin Sperl 					struct spi_message *msg,
3686d9f12122SMartin Sperl 					struct spi_transfer **xferp,
3687c0c0293cSDavid Lechner 					size_t maxsize)
3688d9f12122SMartin Sperl {
3689d9f12122SMartin Sperl 	struct spi_transfer *xfer = *xferp, *xfers;
3690d9f12122SMartin Sperl 	struct spi_replaced_transfers *srt;
3691d9f12122SMartin Sperl 	size_t offset;
3692d9f12122SMartin Sperl 	size_t count, i;
3693d9f12122SMartin Sperl 
369495c8222fSDavid Jander 	/* Calculate how many we have to replace */
3695d9f12122SMartin Sperl 	count = DIV_ROUND_UP(xfer->len, maxsize);
3696d9f12122SMartin Sperl 
369795c8222fSDavid Jander 	/* Create replacement */
3698c0c0293cSDavid Lechner 	srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, GFP_KERNEL);
3699657d32efSDan Carpenter 	if (IS_ERR(srt))
3700657d32efSDan Carpenter 		return PTR_ERR(srt);
3701d9f12122SMartin Sperl 	xfers = srt->inserted_transfers;
3702d9f12122SMartin Sperl 
3703350de7ceSAndy Shevchenko 	/*
3704350de7ceSAndy Shevchenko 	 * Now handle each of those newly inserted spi_transfers.
3705350de7ceSAndy Shevchenko 	 * Note that the replacements spi_transfers all are preset
3706d9f12122SMartin Sperl 	 * to the same values as *xferp, so tx_buf, rx_buf and len
3707d9f12122SMartin Sperl 	 * are all identical (as well as most others)
3708d9f12122SMartin Sperl 	 * so we just have to fix up len and the pointers.
3709d9f12122SMartin Sperl 	 */
3710d9f12122SMartin Sperl 
3711350de7ceSAndy Shevchenko 	/*
3712350de7ceSAndy Shevchenko 	 * The first transfer just needs the length modified, so we
3713350de7ceSAndy Shevchenko 	 * run it outside the loop.
3714d9f12122SMartin Sperl 	 */
3715c8dab77aSFabio Estevam 	xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
3716d9f12122SMartin Sperl 
371795c8222fSDavid Jander 	/* All the others need rx_buf/tx_buf also set */
3718d9f12122SMartin Sperl 	for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3719702ca026SAndy Shevchenko 		/* Update rx_buf, tx_buf and DMA */
3720d9f12122SMartin Sperl 		if (xfers[i].rx_buf)
3721d9f12122SMartin Sperl 			xfers[i].rx_buf += offset;
3722d9f12122SMartin Sperl 		if (xfers[i].tx_buf)
3723d9f12122SMartin Sperl 			xfers[i].tx_buf += offset;
3724d9f12122SMartin Sperl 
372595c8222fSDavid Jander 		/* Update length */
3726d9f12122SMartin Sperl 		xfers[i].len = min(maxsize, xfers[i].len - offset);
3727d9f12122SMartin Sperl 	}
3728d9f12122SMartin Sperl 
3729350de7ceSAndy Shevchenko 	/*
3730350de7ceSAndy Shevchenko 	 * We set up xferp to the last entry we have inserted,
3731350de7ceSAndy Shevchenko 	 * so that we skip those already split transfers.
3732d9f12122SMartin Sperl 	 */
3733d9f12122SMartin Sperl 	*xferp = &xfers[count - 1];
3734d9f12122SMartin Sperl 
373595c8222fSDavid Jander 	/* Increment statistics counters */
37366598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics,
3737d9f12122SMartin Sperl 				       transfers_split_maxsize);
37386598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics,
3739d9f12122SMartin Sperl 				       transfers_split_maxsize);
3740d9f12122SMartin Sperl 
3741d9f12122SMartin Sperl 	return 0;
3742d9f12122SMartin Sperl }
3743d9f12122SMartin Sperl 
3744d9f12122SMartin Sperl /**
3745ce2424d7SMauro Carvalho Chehab  * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3746d9f12122SMartin Sperl  *                               when an individual transfer exceeds a
3747d9f12122SMartin Sperl  *                               certain size
37488caab75fSGeert Uytterhoeven  * @ctlr:    the @spi_controller for this transfer
37493700ce95SMasanari Iida  * @msg:   the @spi_message to transform
37503700ce95SMasanari Iida  * @maxsize:  the maximum when to apply this
3751d9f12122SMartin Sperl  *
3752fab53feaSDavid Lechner  * This function allocates resources that are automatically freed during the
3753fab53feaSDavid Lechner  * spi message unoptimize phase so this function should only be called from
3754fab53feaSDavid Lechner  * optimize_message callbacks.
3755fab53feaSDavid Lechner  *
3756d9f12122SMartin Sperl  * Return: status of transformation
3757d9f12122SMartin Sperl  */
37588caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr,
3759d9f12122SMartin Sperl 				struct spi_message *msg,
3760c0c0293cSDavid Lechner 				size_t maxsize)
3761d9f12122SMartin Sperl {
3762d9f12122SMartin Sperl 	struct spi_transfer *xfer;
3763d9f12122SMartin Sperl 	int ret;
3764d9f12122SMartin Sperl 
3765350de7ceSAndy Shevchenko 	/*
3766350de7ceSAndy Shevchenko 	 * Iterate over the transfer_list,
3767d9f12122SMartin Sperl 	 * but note that xfer is advanced to the last transfer inserted
3768d9f12122SMartin Sperl 	 * to avoid checking sizes again unnecessarily (also xfer does
3769350de7ceSAndy Shevchenko 	 * potentially belong to a different list by the time the
3770350de7ceSAndy Shevchenko 	 * replacement has happened).
3771d9f12122SMartin Sperl 	 */
3772d9f12122SMartin Sperl 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3773d9f12122SMartin Sperl 		if (xfer->len > maxsize) {
37748caab75fSGeert Uytterhoeven 			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3775c0c0293cSDavid Lechner 							   maxsize);
3776d9f12122SMartin Sperl 			if (ret)
3777d9f12122SMartin Sperl 				return ret;
3778d9f12122SMartin Sperl 		}
3779d9f12122SMartin Sperl 	}
3780d9f12122SMartin Sperl 
3781d9f12122SMartin Sperl 	return 0;
3782d9f12122SMartin Sperl }
3783d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
37848ae12a0dSDavid Brownell 
3785027781f3SLeonard Göhrs 
3786027781f3SLeonard Göhrs /**
3787702ca026SAndy Shevchenko  * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3788027781f3SLeonard Göhrs  *                                when an individual transfer exceeds a
3789027781f3SLeonard Göhrs  *                                certain number of SPI words
3790027781f3SLeonard Göhrs  * @ctlr:     the @spi_controller for this transfer
3791027781f3SLeonard Göhrs  * @msg:      the @spi_message to transform
3792027781f3SLeonard Göhrs  * @maxwords: the number of words to limit each transfer to
3793027781f3SLeonard Göhrs  *
3794fab53feaSDavid Lechner  * This function allocates resources that are automatically freed during the
3795fab53feaSDavid Lechner  * spi message unoptimize phase so this function should only be called from
3796fab53feaSDavid Lechner  * optimize_message callbacks.
3797fab53feaSDavid Lechner  *
3798027781f3SLeonard Göhrs  * Return: status of transformation
3799027781f3SLeonard Göhrs  */
3800027781f3SLeonard Göhrs int spi_split_transfers_maxwords(struct spi_controller *ctlr,
3801027781f3SLeonard Göhrs 				 struct spi_message *msg,
3802c0c0293cSDavid Lechner 				 size_t maxwords)
3803027781f3SLeonard Göhrs {
3804027781f3SLeonard Göhrs 	struct spi_transfer *xfer;
3805027781f3SLeonard Göhrs 
3806027781f3SLeonard Göhrs 	/*
3807027781f3SLeonard Göhrs 	 * Iterate over the transfer_list,
3808027781f3SLeonard Göhrs 	 * but note that xfer is advanced to the last transfer inserted
3809027781f3SLeonard Göhrs 	 * to avoid checking sizes again unnecessarily (also xfer does
3810027781f3SLeonard Göhrs 	 * potentially belong to a different list by the time the
3811027781f3SLeonard Göhrs 	 * replacement has happened).
3812027781f3SLeonard Göhrs 	 */
3813027781f3SLeonard Göhrs 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3814027781f3SLeonard Göhrs 		size_t maxsize;
3815027781f3SLeonard Göhrs 		int ret;
3816027781f3SLeonard Göhrs 
38172b308e71SAndy Shevchenko 		maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word));
3818027781f3SLeonard Göhrs 		if (xfer->len > maxsize) {
3819027781f3SLeonard Göhrs 			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3820c0c0293cSDavid Lechner 							   maxsize);
3821027781f3SLeonard Göhrs 			if (ret)
3822027781f3SLeonard Göhrs 				return ret;
3823027781f3SLeonard Göhrs 		}
3824027781f3SLeonard Göhrs 	}
3825027781f3SLeonard Göhrs 
3826027781f3SLeonard Göhrs 	return 0;
3827027781f3SLeonard Göhrs }
3828027781f3SLeonard Göhrs EXPORT_SYMBOL_GPL(spi_split_transfers_maxwords);
3829027781f3SLeonard Göhrs 
38308ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
38318ae12a0dSDavid Brownell 
3832702ca026SAndy Shevchenko /*
3833702ca026SAndy Shevchenko  * Core methods for SPI controller protocol drivers. Some of the
38347d077197SDavid Brownell  * other core methods are currently defined as inline functions.
38357d077197SDavid Brownell  */
38367d077197SDavid Brownell 
38378caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
38388caab75fSGeert Uytterhoeven 					u8 bits_per_word)
383963ab645fSStefan Brüns {
38408caab75fSGeert Uytterhoeven 	if (ctlr->bits_per_word_mask) {
384163ab645fSStefan Brüns 		/* Only 32 bits fit in the mask */
384263ab645fSStefan Brüns 		if (bits_per_word > 32)
384363ab645fSStefan Brüns 			return -EINVAL;
38448caab75fSGeert Uytterhoeven 		if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
384563ab645fSStefan Brüns 			return -EINVAL;
384663ab645fSStefan Brüns 	}
384763ab645fSStefan Brüns 
384863ab645fSStefan Brüns 	return 0;
384963ab645fSStefan Brüns }
385063ab645fSStefan Brüns 
38517d077197SDavid Brownell /**
3852684a4784STudor Ambarus  * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3853684a4784STudor Ambarus  * @spi: the device that requires specific CS timing configuration
3854684a4784STudor Ambarus  *
3855684a4784STudor Ambarus  * Return: zero on success, else a negative error code.
3856684a4784STudor Ambarus  */
3857684a4784STudor Ambarus static int spi_set_cs_timing(struct spi_device *spi)
3858684a4784STudor Ambarus {
3859684a4784STudor Ambarus 	struct device *parent = spi->controller->dev.parent;
3860684a4784STudor Ambarus 	int status = 0;
3861684a4784STudor Ambarus 
3862303feb3cSAmit Kumar Mahapatra 	if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) {
3863684a4784STudor Ambarus 		if (spi->controller->auto_runtime_pm) {
3864684a4784STudor Ambarus 			status = pm_runtime_get_sync(parent);
3865684a4784STudor Ambarus 			if (status < 0) {
3866684a4784STudor Ambarus 				pm_runtime_put_noidle(parent);
3867684a4784STudor Ambarus 				dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3868684a4784STudor Ambarus 					status);
3869684a4784STudor Ambarus 				return status;
3870684a4784STudor Ambarus 			}
3871684a4784STudor Ambarus 
3872684a4784STudor Ambarus 			status = spi->controller->set_cs_timing(spi);
3873684a4784STudor Ambarus 			pm_runtime_mark_last_busy(parent);
3874684a4784STudor Ambarus 			pm_runtime_put_autosuspend(parent);
3875684a4784STudor Ambarus 		} else {
3876684a4784STudor Ambarus 			status = spi->controller->set_cs_timing(spi);
3877684a4784STudor Ambarus 		}
3878684a4784STudor Ambarus 	}
3879684a4784STudor Ambarus 	return status;
3880684a4784STudor Ambarus }
3881684a4784STudor Ambarus 
3882684a4784STudor Ambarus /**
38837d077197SDavid Brownell  * spi_setup - setup SPI mode and clock rate
38847d077197SDavid Brownell  * @spi: the device whose settings are being modified
38857d077197SDavid Brownell  * Context: can sleep, and no requests are queued to the device
38867d077197SDavid Brownell  *
38877d077197SDavid Brownell  * SPI protocol drivers may need to update the transfer mode if the
38887d077197SDavid Brownell  * device doesn't work with its default.  They may likewise need
38897d077197SDavid Brownell  * to update clock rates or word sizes from initial values.  This function
38907d077197SDavid Brownell  * changes those settings, and must be called from a context that can sleep.
38917d077197SDavid Brownell  * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
38927d077197SDavid Brownell  * effect the next time the device is selected and data is transferred to
3893702ca026SAndy Shevchenko  * or from it.  When this function returns, the SPI device is deselected.
38947d077197SDavid Brownell  *
38957d077197SDavid Brownell  * Note that this call will fail if the protocol driver specifies an option
38967d077197SDavid Brownell  * that the underlying controller or its driver does not support.  For
38977d077197SDavid Brownell  * example, not all hardware supports wire transfers using nine bit words,
38987d077197SDavid Brownell  * LSB-first wire encoding, or active-high chipselects.
389997d56dc6SJavier Martinez Canillas  *
390097d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
39017d077197SDavid Brownell  */
39027d077197SDavid Brownell int spi_setup(struct spi_device *spi)
39037d077197SDavid Brownell {
390483596fbeSGeert Uytterhoeven 	unsigned	bad_bits, ugly_bits;
39053bca1a38SLi zeming 	int		status;
39067d077197SDavid Brownell 
3907d962608cSDragos Bogdan 	/*
3908350de7ceSAndy Shevchenko 	 * Check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO
3909350de7ceSAndy Shevchenko 	 * are set at the same time.
3910f477b7fbSwangyuhang 	 */
3911d962608cSDragos Bogdan 	if ((hweight_long(spi->mode &
3912d962608cSDragos Bogdan 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) ||
3913d962608cSDragos Bogdan 	    (hweight_long(spi->mode &
3914d962608cSDragos Bogdan 		(SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) {
3915f477b7fbSwangyuhang 		dev_err(&spi->dev,
3916d962608cSDragos Bogdan 		"setup: can not select any two of dual, quad and no-rx/tx at the same time\n");
3917f477b7fbSwangyuhang 		return -EINVAL;
3918f477b7fbSwangyuhang 	}
3919350de7ceSAndy Shevchenko 	/* If it is SPI_3WIRE mode, DUAL and QUAD should be forbidden */
3920f477b7fbSwangyuhang 	if ((spi->mode & SPI_3WIRE) && (spi->mode &
39216b03061fSYogesh Narayan Gaur 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
39226b03061fSYogesh Narayan Gaur 		 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
3923f477b7fbSwangyuhang 		return -EINVAL;
3924*f58872f4SMarcelo Schmitt 	/* Check against conflicting MOSI idle configuration */
3925*f58872f4SMarcelo Schmitt 	if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) {
3926*f58872f4SMarcelo Schmitt 		dev_err(&spi->dev,
3927*f58872f4SMarcelo Schmitt 			"setup: MOSI configured to idle low and high at the same time.\n");
3928*f58872f4SMarcelo Schmitt 		return -EINVAL;
3929*f58872f4SMarcelo Schmitt 	}
3930350de7ceSAndy Shevchenko 	/*
3931350de7ceSAndy Shevchenko 	 * Help drivers fail *cleanly* when they need options
3932350de7ceSAndy Shevchenko 	 * that aren't supported with their current controller.
3933cbaa62e0SDavid Lechner 	 * SPI_CS_WORD has a fallback software implementation,
3934cbaa62e0SDavid Lechner 	 * so it is ignored here.
3935e7db06b5SDavid Brownell 	 */
3936d962608cSDragos Bogdan 	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
3937d962608cSDragos Bogdan 				 SPI_NO_TX | SPI_NO_RX);
393883596fbeSGeert Uytterhoeven 	ugly_bits = bad_bits &
39396b03061fSYogesh Narayan Gaur 		    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
39406b03061fSYogesh Narayan Gaur 		     SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
394183596fbeSGeert Uytterhoeven 	if (ugly_bits) {
394283596fbeSGeert Uytterhoeven 		dev_warn(&spi->dev,
394383596fbeSGeert Uytterhoeven 			 "setup: ignoring unsupported mode bits %x\n",
394483596fbeSGeert Uytterhoeven 			 ugly_bits);
394583596fbeSGeert Uytterhoeven 		spi->mode &= ~ugly_bits;
394683596fbeSGeert Uytterhoeven 		bad_bits &= ~ugly_bits;
394783596fbeSGeert Uytterhoeven 	}
3948e7db06b5SDavid Brownell 	if (bad_bits) {
3949eb288a1fSLinus Walleij 		dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
3950e7db06b5SDavid Brownell 			bad_bits);
3951e7db06b5SDavid Brownell 		return -EINVAL;
3952e7db06b5SDavid Brownell 	}
3953e7db06b5SDavid Brownell 
3954b3fe2e51SPaul Kocialkowski 	if (!spi->bits_per_word) {
39557d077197SDavid Brownell 		spi->bits_per_word = 8;
3956b3fe2e51SPaul Kocialkowski 	} else {
3957b3fe2e51SPaul Kocialkowski 		/*
3958b3fe2e51SPaul Kocialkowski 		 * Some controllers may not support the default 8 bits-per-word
3959b3fe2e51SPaul Kocialkowski 		 * so only perform the check when this is explicitly provided.
3960b3fe2e51SPaul Kocialkowski 		 */
39618caab75fSGeert Uytterhoeven 		status = __spi_validate_bits_per_word(spi->controller,
39628caab75fSGeert Uytterhoeven 						      spi->bits_per_word);
39635ab8d262SAndy Shevchenko 		if (status)
39645ab8d262SAndy Shevchenko 			return status;
3965b3fe2e51SPaul Kocialkowski 	}
396663ab645fSStefan Brüns 
39676820e812STudor Ambarus 	if (spi->controller->max_speed_hz &&
39686820e812STudor Ambarus 	    (!spi->max_speed_hz ||
39696820e812STudor Ambarus 	     spi->max_speed_hz > spi->controller->max_speed_hz))
39708caab75fSGeert Uytterhoeven 		spi->max_speed_hz = spi->controller->max_speed_hz;
3971052eb2d4SAxel Lin 
39724fae3a58SSerge Semin 	mutex_lock(&spi->controller->io_mutex);
39734fae3a58SSerge Semin 
3974c914dbf8SJoe Burmeister 	if (spi->controller->setup) {
39758caab75fSGeert Uytterhoeven 		status = spi->controller->setup(spi);
3976c914dbf8SJoe Burmeister 		if (status) {
3977c914dbf8SJoe Burmeister 			mutex_unlock(&spi->controller->io_mutex);
3978c914dbf8SJoe Burmeister 			dev_err(&spi->controller->dev, "Failed to setup device: %d\n",
3979c914dbf8SJoe Burmeister 				status);
3980c914dbf8SJoe Burmeister 			return status;
3981c914dbf8SJoe Burmeister 		}
3982c914dbf8SJoe Burmeister 	}
39837d077197SDavid Brownell 
3984684a4784STudor Ambarus 	status = spi_set_cs_timing(spi);
3985684a4784STudor Ambarus 	if (status) {
3986684a4784STudor Ambarus 		mutex_unlock(&spi->controller->io_mutex);
3987684a4784STudor Ambarus 		return status;
3988684a4784STudor Ambarus 	}
3989684a4784STudor Ambarus 
3990d948e6caSLuhua Xu 	if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3991dd769f15SMinghao Chi 		status = pm_runtime_resume_and_get(spi->controller->dev.parent);
3992d948e6caSLuhua Xu 		if (status < 0) {
39934fae3a58SSerge Semin 			mutex_unlock(&spi->controller->io_mutex);
3994d948e6caSLuhua Xu 			dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3995d948e6caSLuhua Xu 				status);
3996d948e6caSLuhua Xu 			return status;
3997d948e6caSLuhua Xu 		}
399857a94607STony Lindgren 
399957a94607STony Lindgren 		/*
400057a94607STony Lindgren 		 * We do not want to return positive value from pm_runtime_get,
400157a94607STony Lindgren 		 * there are many instances of devices calling spi_setup() and
400257a94607STony Lindgren 		 * checking for a non-zero return value instead of a negative
400357a94607STony Lindgren 		 * return value.
400457a94607STony Lindgren 		 */
400557a94607STony Lindgren 		status = 0;
400657a94607STony Lindgren 
4007d347b4aaSDavid Bauer 		spi_set_cs(spi, false, true);
4008d948e6caSLuhua Xu 		pm_runtime_mark_last_busy(spi->controller->dev.parent);
4009d948e6caSLuhua Xu 		pm_runtime_put_autosuspend(spi->controller->dev.parent);
4010d948e6caSLuhua Xu 	} else {
4011d347b4aaSDavid Bauer 		spi_set_cs(spi, false, true);
4012d948e6caSLuhua Xu 	}
4013abeedb01SFranklin S Cooper Jr 
40144fae3a58SSerge Semin 	mutex_unlock(&spi->controller->io_mutex);
40154fae3a58SSerge Semin 
4016924b5867SDouglas Anderson 	if (spi->rt && !spi->controller->rt) {
4017924b5867SDouglas Anderson 		spi->controller->rt = true;
4018924b5867SDouglas Anderson 		spi_set_thread_rt(spi->controller);
4019924b5867SDouglas Anderson 	}
4020924b5867SDouglas Anderson 
40215cb4e1f3SAndy Shevchenko 	trace_spi_setup(spi, status);
40225cb4e1f3SAndy Shevchenko 
402340b82c2dSAndy Shevchenko 	dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
402440b82c2dSAndy Shevchenko 			spi->mode & SPI_MODE_X_MASK,
40257d077197SDavid Brownell 			(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
40267d077197SDavid Brownell 			(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
40277d077197SDavid Brownell 			(spi->mode & SPI_3WIRE) ? "3wire, " : "",
40287d077197SDavid Brownell 			(spi->mode & SPI_LOOP) ? "loopback, " : "",
40297d077197SDavid Brownell 			spi->bits_per_word, spi->max_speed_hz,
40307d077197SDavid Brownell 			status);
40317d077197SDavid Brownell 
40327d077197SDavid Brownell 	return status;
40337d077197SDavid Brownell }
40347d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup);
40357d077197SDavid Brownell 
40366c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
40376c613f68SAlexandru Ardelean 				       struct spi_device *spi)
40386c613f68SAlexandru Ardelean {
40396c613f68SAlexandru Ardelean 	int delay1, delay2;
40406c613f68SAlexandru Ardelean 
40413984d39bSAlexandru Ardelean 	delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
40426c613f68SAlexandru Ardelean 	if (delay1 < 0)
40436c613f68SAlexandru Ardelean 		return delay1;
40446c613f68SAlexandru Ardelean 
40453984d39bSAlexandru Ardelean 	delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
40466c613f68SAlexandru Ardelean 	if (delay2 < 0)
40476c613f68SAlexandru Ardelean 		return delay2;
40486c613f68SAlexandru Ardelean 
40496c613f68SAlexandru Ardelean 	if (delay1 < delay2)
40506c613f68SAlexandru Ardelean 		memcpy(&xfer->word_delay, &spi->word_delay,
40516c613f68SAlexandru Ardelean 		       sizeof(xfer->word_delay));
40526c613f68SAlexandru Ardelean 
40536c613f68SAlexandru Ardelean 	return 0;
40546c613f68SAlexandru Ardelean }
40556c613f68SAlexandru Ardelean 
405690808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message)
4057cf32b71eSErnst Schwab {
40588caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
4059e6811d1dSLaxman Dewangan 	struct spi_transfer *xfer;
40606ea31293SAtsushi Nemoto 	int w_size;
4061cf32b71eSErnst Schwab 
406224a0013aSMark Brown 	if (list_empty(&message->transfers))
406324a0013aSMark Brown 		return -EINVAL;
406424a0013aSMark Brown 
4065b204aa0fSDavid Lechner 	message->spi = spi;
4066b204aa0fSDavid Lechner 
4067350de7ceSAndy Shevchenko 	/*
4068350de7ceSAndy Shevchenko 	 * Half-duplex links include original MicroWire, and ones with
4069cf32b71eSErnst Schwab 	 * only one data pin like SPI_3WIRE (switches direction) or where
4070cf32b71eSErnst Schwab 	 * either MOSI or MISO is missing.  They can also be caused by
4071cf32b71eSErnst Schwab 	 * software limitations.
4072cf32b71eSErnst Schwab 	 */
40738caab75fSGeert Uytterhoeven 	if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
40748caab75fSGeert Uytterhoeven 	    (spi->mode & SPI_3WIRE)) {
40758caab75fSGeert Uytterhoeven 		unsigned flags = ctlr->flags;
4076cf32b71eSErnst Schwab 
4077cf32b71eSErnst Schwab 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
4078cf32b71eSErnst Schwab 			if (xfer->rx_buf && xfer->tx_buf)
4079cf32b71eSErnst Schwab 				return -EINVAL;
40808caab75fSGeert Uytterhoeven 			if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
4081cf32b71eSErnst Schwab 				return -EINVAL;
40828caab75fSGeert Uytterhoeven 			if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
4083cf32b71eSErnst Schwab 				return -EINVAL;
4084cf32b71eSErnst Schwab 		}
4085cf32b71eSErnst Schwab 	}
4086cf32b71eSErnst Schwab 
4087350de7ceSAndy Shevchenko 	/*
4088059b8ffeSLaxman Dewangan 	 * Set transfer bits_per_word and max speed as spi device default if
4089059b8ffeSLaxman Dewangan 	 * it is not set for this transfer.
4090f477b7fbSwangyuhang 	 * Set transfer tx_nbits and rx_nbits as single transfer default
4091f477b7fbSwangyuhang 	 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
4092b7bb367aSJonas Bonn 	 * Ensure transfer word_delay is at least as long as that required by
4093b7bb367aSJonas Bonn 	 * device itself.
4094e6811d1dSLaxman Dewangan 	 */
409577e80588SMartin Sperl 	message->frame_length = 0;
4096e6811d1dSLaxman Dewangan 	list_for_each_entry(xfer, &message->transfers, transfer_list) {
40975d7e2b5eSMartin Sperl 		xfer->effective_speed_hz = 0;
4098078726ceSSourav Poddar 		message->frame_length += xfer->len;
4099e6811d1dSLaxman Dewangan 		if (!xfer->bits_per_word)
4100e6811d1dSLaxman Dewangan 			xfer->bits_per_word = spi->bits_per_word;
4101a6f87fadSAxel Lin 
4102a6f87fadSAxel Lin 		if (!xfer->speed_hz)
4103059b8ffeSLaxman Dewangan 			xfer->speed_hz = spi->max_speed_hz;
4104a6f87fadSAxel Lin 
41058caab75fSGeert Uytterhoeven 		if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
41068caab75fSGeert Uytterhoeven 			xfer->speed_hz = ctlr->max_speed_hz;
410756ede94aSGabor Juhos 
41088caab75fSGeert Uytterhoeven 		if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
4109543bb255SStephen Warren 			return -EINVAL;
4110a2fd4f9fSMark Brown 
41114d94bd21SIvan T. Ivanov 		/*
41124d94bd21SIvan T. Ivanov 		 * SPI transfer length should be multiple of SPI word size
4113350de7ceSAndy Shevchenko 		 * where SPI word size should be power-of-two multiple.
41144d94bd21SIvan T. Ivanov 		 */
41154d94bd21SIvan T. Ivanov 		if (xfer->bits_per_word <= 8)
41164d94bd21SIvan T. Ivanov 			w_size = 1;
41174d94bd21SIvan T. Ivanov 		else if (xfer->bits_per_word <= 16)
41184d94bd21SIvan T. Ivanov 			w_size = 2;
41194d94bd21SIvan T. Ivanov 		else
41204d94bd21SIvan T. Ivanov 			w_size = 4;
41214d94bd21SIvan T. Ivanov 
41224d94bd21SIvan T. Ivanov 		/* No partial transfers accepted */
41236ea31293SAtsushi Nemoto 		if (xfer->len % w_size)
41244d94bd21SIvan T. Ivanov 			return -EINVAL;
41254d94bd21SIvan T. Ivanov 
41268caab75fSGeert Uytterhoeven 		if (xfer->speed_hz && ctlr->min_speed_hz &&
41278caab75fSGeert Uytterhoeven 		    xfer->speed_hz < ctlr->min_speed_hz)
4128a2fd4f9fSMark Brown 			return -EINVAL;
4129f477b7fbSwangyuhang 
4130f477b7fbSwangyuhang 		if (xfer->tx_buf && !xfer->tx_nbits)
4131f477b7fbSwangyuhang 			xfer->tx_nbits = SPI_NBITS_SINGLE;
4132f477b7fbSwangyuhang 		if (xfer->rx_buf && !xfer->rx_nbits)
4133f477b7fbSwangyuhang 			xfer->rx_nbits = SPI_NBITS_SINGLE;
4134350de7ceSAndy Shevchenko 		/*
4135350de7ceSAndy Shevchenko 		 * Check transfer tx/rx_nbits:
41361afd9989SGeert Uytterhoeven 		 * 1. check the value matches one of single, dual and quad
41371afd9989SGeert Uytterhoeven 		 * 2. check tx/rx_nbits match the mode in spi_device
4138f477b7fbSwangyuhang 		 */
4139db90a441SSourav Poddar 		if (xfer->tx_buf) {
4140d962608cSDragos Bogdan 			if (spi->mode & SPI_NO_TX)
4141d962608cSDragos Bogdan 				return -EINVAL;
4142f477b7fbSwangyuhang 			if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
4143f477b7fbSwangyuhang 				xfer->tx_nbits != SPI_NBITS_DUAL &&
4144d6a711a8SPatrice Chotard 				xfer->tx_nbits != SPI_NBITS_QUAD &&
4145d6a711a8SPatrice Chotard 				xfer->tx_nbits != SPI_NBITS_OCTAL)
4146a2fd4f9fSMark Brown 				return -EINVAL;
4147f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
4148f477b7fbSwangyuhang 				!(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
4149f477b7fbSwangyuhang 				return -EINVAL;
4150f477b7fbSwangyuhang 			if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
4151f477b7fbSwangyuhang 				!(spi->mode & SPI_TX_QUAD))
4152f477b7fbSwangyuhang 				return -EINVAL;
4153db90a441SSourav Poddar 		}
415495c8222fSDavid Jander 		/* Check transfer rx_nbits */
4155db90a441SSourav Poddar 		if (xfer->rx_buf) {
4156d962608cSDragos Bogdan 			if (spi->mode & SPI_NO_RX)
4157d962608cSDragos Bogdan 				return -EINVAL;
4158f477b7fbSwangyuhang 			if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
4159f477b7fbSwangyuhang 				xfer->rx_nbits != SPI_NBITS_DUAL &&
4160d6a711a8SPatrice Chotard 				xfer->rx_nbits != SPI_NBITS_QUAD &&
4161d6a711a8SPatrice Chotard 				xfer->rx_nbits != SPI_NBITS_OCTAL)
4162f477b7fbSwangyuhang 				return -EINVAL;
4163f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
4164f477b7fbSwangyuhang 				!(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
4165f477b7fbSwangyuhang 				return -EINVAL;
4166f477b7fbSwangyuhang 			if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
4167f477b7fbSwangyuhang 				!(spi->mode & SPI_RX_QUAD))
4168f477b7fbSwangyuhang 				return -EINVAL;
4169e6811d1dSLaxman Dewangan 		}
4170b7bb367aSJonas Bonn 
41716c613f68SAlexandru Ardelean 		if (_spi_xfer_word_delay_update(xfer, spi))
41726c613f68SAlexandru Ardelean 			return -EINVAL;
4173e6811d1dSLaxman Dewangan 	}
4174e6811d1dSLaxman Dewangan 
4175cf32b71eSErnst Schwab 	message->status = -EINPROGRESS;
417690808738SMark Brown 
417790808738SMark Brown 	return 0;
417890808738SMark Brown }
417990808738SMark Brown 
41807b1d87afSDavid Lechner /*
4181fab53feaSDavid Lechner  * spi_split_transfers - generic handling of transfer splitting
4182fab53feaSDavid Lechner  * @msg: the message to split
4183fab53feaSDavid Lechner  *
4184fab53feaSDavid Lechner  * Under certain conditions, a SPI controller may not support arbitrary
4185fab53feaSDavid Lechner  * transfer sizes or other features required by a peripheral. This function
4186fab53feaSDavid Lechner  * will split the transfers in the message into smaller transfers that are
4187fab53feaSDavid Lechner  * supported by the controller.
4188fab53feaSDavid Lechner  *
4189fab53feaSDavid Lechner  * Controllers with special requirements not covered here can also split
4190fab53feaSDavid Lechner  * transfers in the optimize_message() callback.
4191fab53feaSDavid Lechner  *
4192fab53feaSDavid Lechner  * Context: can sleep
4193fab53feaSDavid Lechner  * Return: zero on success, else a negative error code
4194fab53feaSDavid Lechner  */
4195fab53feaSDavid Lechner static int spi_split_transfers(struct spi_message *msg)
4196fab53feaSDavid Lechner {
4197fab53feaSDavid Lechner 	struct spi_controller *ctlr = msg->spi->controller;
4198fab53feaSDavid Lechner 	struct spi_transfer *xfer;
4199fab53feaSDavid Lechner 	int ret;
4200fab53feaSDavid Lechner 
4201fab53feaSDavid Lechner 	/*
4202fab53feaSDavid Lechner 	 * If an SPI controller does not support toggling the CS line on each
4203fab53feaSDavid Lechner 	 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
4204fab53feaSDavid Lechner 	 * for the CS line, we can emulate the CS-per-word hardware function by
4205fab53feaSDavid Lechner 	 * splitting transfers into one-word transfers and ensuring that
4206fab53feaSDavid Lechner 	 * cs_change is set for each transfer.
4207fab53feaSDavid Lechner 	 */
4208fab53feaSDavid Lechner 	if ((msg->spi->mode & SPI_CS_WORD) &&
4209fab53feaSDavid Lechner 	    (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) {
4210fab53feaSDavid Lechner 		ret = spi_split_transfers_maxwords(ctlr, msg, 1);
4211fab53feaSDavid Lechner 		if (ret)
4212fab53feaSDavid Lechner 			return ret;
4213fab53feaSDavid Lechner 
4214fab53feaSDavid Lechner 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
4215fab53feaSDavid Lechner 			/* Don't change cs_change on the last entry in the list */
4216fab53feaSDavid Lechner 			if (list_is_last(&xfer->transfer_list, &msg->transfers))
4217fab53feaSDavid Lechner 				break;
4218fab53feaSDavid Lechner 
4219fab53feaSDavid Lechner 			xfer->cs_change = 1;
4220fab53feaSDavid Lechner 		}
4221fab53feaSDavid Lechner 	} else {
4222fab53feaSDavid Lechner 		ret = spi_split_transfers_maxsize(ctlr, msg,
4223fab53feaSDavid Lechner 						  spi_max_transfer_size(msg->spi));
4224fab53feaSDavid Lechner 		if (ret)
4225fab53feaSDavid Lechner 			return ret;
4226fab53feaSDavid Lechner 	}
4227fab53feaSDavid Lechner 
4228fab53feaSDavid Lechner 	return 0;
4229fab53feaSDavid Lechner }
4230fab53feaSDavid Lechner 
4231fab53feaSDavid Lechner /*
42327b1d87afSDavid Lechner  * __spi_optimize_message - shared implementation for spi_optimize_message()
42337b1d87afSDavid Lechner  *                          and spi_maybe_optimize_message()
42347b1d87afSDavid Lechner  * @spi: the device that will be used for the message
42357b1d87afSDavid Lechner  * @msg: the message to optimize
42367b1d87afSDavid Lechner  *
42377b1d87afSDavid Lechner  * Peripheral drivers will call spi_optimize_message() and the spi core will
42387b1d87afSDavid Lechner  * call spi_maybe_optimize_message() instead of calling this directly.
42397b1d87afSDavid Lechner  *
42407b1d87afSDavid Lechner  * It is not valid to call this on a message that has already been optimized.
42417b1d87afSDavid Lechner  *
42427b1d87afSDavid Lechner  * Return: zero on success, else a negative error code
42437b1d87afSDavid Lechner  */
42447b1d87afSDavid Lechner static int __spi_optimize_message(struct spi_device *spi,
42457b1d87afSDavid Lechner 				  struct spi_message *msg)
42467b1d87afSDavid Lechner {
42477b1d87afSDavid Lechner 	struct spi_controller *ctlr = spi->controller;
42487b1d87afSDavid Lechner 	int ret;
42497b1d87afSDavid Lechner 
42507b1d87afSDavid Lechner 	ret = __spi_validate(spi, msg);
42517b1d87afSDavid Lechner 	if (ret)
42527b1d87afSDavid Lechner 		return ret;
42537b1d87afSDavid Lechner 
4254fab53feaSDavid Lechner 	ret = spi_split_transfers(msg);
42557b1d87afSDavid Lechner 	if (ret)
42567b1d87afSDavid Lechner 		return ret;
4257fab53feaSDavid Lechner 
4258fab53feaSDavid Lechner 	if (ctlr->optimize_message) {
4259fab53feaSDavid Lechner 		ret = ctlr->optimize_message(msg);
4260fab53feaSDavid Lechner 		if (ret) {
4261fab53feaSDavid Lechner 			spi_res_release(ctlr, msg);
4262fab53feaSDavid Lechner 			return ret;
4263fab53feaSDavid Lechner 		}
42647b1d87afSDavid Lechner 	}
42657b1d87afSDavid Lechner 
42667b1d87afSDavid Lechner 	msg->optimized = true;
42677b1d87afSDavid Lechner 
42687b1d87afSDavid Lechner 	return 0;
42697b1d87afSDavid Lechner }
42707b1d87afSDavid Lechner 
42717b1d87afSDavid Lechner /*
42727b1d87afSDavid Lechner  * spi_maybe_optimize_message - optimize message if it isn't already pre-optimized
42737b1d87afSDavid Lechner  * @spi: the device that will be used for the message
42747b1d87afSDavid Lechner  * @msg: the message to optimize
42757b1d87afSDavid Lechner  * Return: zero on success, else a negative error code
42767b1d87afSDavid Lechner  */
42777b1d87afSDavid Lechner static int spi_maybe_optimize_message(struct spi_device *spi,
42787b1d87afSDavid Lechner 				      struct spi_message *msg)
42797b1d87afSDavid Lechner {
4280ca52aa4cSDavid Lechner 	if (spi->controller->defer_optimize_message) {
4281ca52aa4cSDavid Lechner 		msg->spi = spi;
4282ca52aa4cSDavid Lechner 		return 0;
4283ca52aa4cSDavid Lechner 	}
4284ca52aa4cSDavid Lechner 
42857b1d87afSDavid Lechner 	if (msg->pre_optimized)
42867b1d87afSDavid Lechner 		return 0;
42877b1d87afSDavid Lechner 
42887b1d87afSDavid Lechner 	return __spi_optimize_message(spi, msg);
42897b1d87afSDavid Lechner }
42907b1d87afSDavid Lechner 
42917b1d87afSDavid Lechner /**
42927b1d87afSDavid Lechner  * spi_optimize_message - do any one-time validation and setup for a SPI message
42937b1d87afSDavid Lechner  * @spi: the device that will be used for the message
42947b1d87afSDavid Lechner  * @msg: the message to optimize
42957b1d87afSDavid Lechner  *
42967b1d87afSDavid Lechner  * Peripheral drivers that reuse the same message repeatedly may call this to
42977b1d87afSDavid Lechner  * perform as much message prep as possible once, rather than repeating it each
42987b1d87afSDavid Lechner  * time a message transfer is performed to improve throughput and reduce CPU
42997b1d87afSDavid Lechner  * usage.
43007b1d87afSDavid Lechner  *
43017b1d87afSDavid Lechner  * Once a message has been optimized, it cannot be modified with the exception
43027b1d87afSDavid Lechner  * of updating the contents of any xfer->tx_buf (the pointer can't be changed,
43037b1d87afSDavid Lechner  * only the data in the memory it points to).
43047b1d87afSDavid Lechner  *
43057b1d87afSDavid Lechner  * Calls to this function must be balanced with calls to spi_unoptimize_message()
43067b1d87afSDavid Lechner  * to avoid leaking resources.
43077b1d87afSDavid Lechner  *
43087b1d87afSDavid Lechner  * Context: can sleep
43097b1d87afSDavid Lechner  * Return: zero on success, else a negative error code
43107b1d87afSDavid Lechner  */
43117b1d87afSDavid Lechner int spi_optimize_message(struct spi_device *spi, struct spi_message *msg)
43127b1d87afSDavid Lechner {
43137b1d87afSDavid Lechner 	int ret;
43147b1d87afSDavid Lechner 
4315ca52aa4cSDavid Lechner 	/*
4316ca52aa4cSDavid Lechner 	 * Pre-optimization is not supported and optimization is deferred e.g.
4317ca52aa4cSDavid Lechner 	 * when using spi-mux.
4318ca52aa4cSDavid Lechner 	 */
4319ca52aa4cSDavid Lechner 	if (spi->controller->defer_optimize_message)
4320ca52aa4cSDavid Lechner 		return 0;
4321ca52aa4cSDavid Lechner 
43227b1d87afSDavid Lechner 	ret = __spi_optimize_message(spi, msg);
43237b1d87afSDavid Lechner 	if (ret)
43247b1d87afSDavid Lechner 		return ret;
43257b1d87afSDavid Lechner 
43267b1d87afSDavid Lechner 	/*
43277b1d87afSDavid Lechner 	 * This flag indicates that the peripheral driver called spi_optimize_message()
43287b1d87afSDavid Lechner 	 * and therefore we shouldn't unoptimize message automatically when finalizing
43297b1d87afSDavid Lechner 	 * the message but rather wait until spi_unoptimize_message() is called
43307b1d87afSDavid Lechner 	 * by the peripheral driver.
43317b1d87afSDavid Lechner 	 */
43327b1d87afSDavid Lechner 	msg->pre_optimized = true;
43337b1d87afSDavid Lechner 
43347b1d87afSDavid Lechner 	return 0;
43357b1d87afSDavid Lechner }
43367b1d87afSDavid Lechner EXPORT_SYMBOL_GPL(spi_optimize_message);
43377b1d87afSDavid Lechner 
43387b1d87afSDavid Lechner /**
43397b1d87afSDavid Lechner  * spi_unoptimize_message - releases any resources allocated by spi_optimize_message()
43407b1d87afSDavid Lechner  * @msg: the message to unoptimize
43417b1d87afSDavid Lechner  *
43427b1d87afSDavid Lechner  * Calls to this function must be balanced with calls to spi_optimize_message().
43437b1d87afSDavid Lechner  *
43447b1d87afSDavid Lechner  * Context: can sleep
43457b1d87afSDavid Lechner  */
43467b1d87afSDavid Lechner void spi_unoptimize_message(struct spi_message *msg)
43477b1d87afSDavid Lechner {
4348ca52aa4cSDavid Lechner 	if (msg->spi->controller->defer_optimize_message)
4349ca52aa4cSDavid Lechner 		return;
4350ca52aa4cSDavid Lechner 
43517b1d87afSDavid Lechner 	__spi_unoptimize_message(msg);
43527b1d87afSDavid Lechner 	msg->pre_optimized = false;
43537b1d87afSDavid Lechner }
43547b1d87afSDavid Lechner EXPORT_SYMBOL_GPL(spi_unoptimize_message);
43557b1d87afSDavid Lechner 
435690808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message)
435790808738SMark Brown {
43588caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
4359b42faeeeSVladimir Oltean 	struct spi_transfer *xfer;
436090808738SMark Brown 
4361b5932f5cSBoris Brezillon 	/*
4362b5932f5cSBoris Brezillon 	 * Some controllers do not support doing regular SPI transfers. Return
4363b5932f5cSBoris Brezillon 	 * ENOTSUPP when this is the case.
4364b5932f5cSBoris Brezillon 	 */
4365b5932f5cSBoris Brezillon 	if (!ctlr->transfer)
4366b5932f5cSBoris Brezillon 		return -ENOTSUPP;
4367b5932f5cSBoris Brezillon 
43686598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async);
43696598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async);
4370eca2ebc7SMartin Sperl 
437190808738SMark Brown 	trace_spi_message_submit(message);
437290808738SMark Brown 
4373b42faeeeSVladimir Oltean 	if (!ctlr->ptp_sts_supported) {
4374b42faeeeSVladimir Oltean 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
4375b42faeeeSVladimir Oltean 			xfer->ptp_sts_word_pre = 0;
4376b42faeeeSVladimir Oltean 			ptp_read_system_prets(xfer->ptp_sts);
4377b42faeeeSVladimir Oltean 		}
4378b42faeeeSVladimir Oltean 	}
4379b42faeeeSVladimir Oltean 
43808caab75fSGeert Uytterhoeven 	return ctlr->transfer(spi, message);
4381cf32b71eSErnst Schwab }
4382cf32b71eSErnst Schwab 
4383d4a0055fSDavid Lechner static void devm_spi_unoptimize_message(void *msg)
4384d4a0055fSDavid Lechner {
4385d4a0055fSDavid Lechner 	spi_unoptimize_message(msg);
4386d4a0055fSDavid Lechner }
4387d4a0055fSDavid Lechner 
4388d4a0055fSDavid Lechner /**
4389d4a0055fSDavid Lechner  * devm_spi_optimize_message - managed version of spi_optimize_message()
4390d4a0055fSDavid Lechner  * @dev: the device that manages @msg (usually @spi->dev)
4391d4a0055fSDavid Lechner  * @spi: the device that will be used for the message
4392d4a0055fSDavid Lechner  * @msg: the message to optimize
4393d4a0055fSDavid Lechner  * Return: zero on success, else a negative error code
4394d4a0055fSDavid Lechner  *
4395d4a0055fSDavid Lechner  * spi_unoptimize_message() will automatically be called when the device is
4396d4a0055fSDavid Lechner  * removed.
4397d4a0055fSDavid Lechner  */
4398d4a0055fSDavid Lechner int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,
4399d4a0055fSDavid Lechner 			      struct spi_message *msg)
4400d4a0055fSDavid Lechner {
4401d4a0055fSDavid Lechner 	int ret;
4402d4a0055fSDavid Lechner 
4403d4a0055fSDavid Lechner 	ret = spi_optimize_message(spi, msg);
4404d4a0055fSDavid Lechner 	if (ret)
4405d4a0055fSDavid Lechner 		return ret;
4406d4a0055fSDavid Lechner 
4407d4a0055fSDavid Lechner 	return devm_add_action_or_reset(dev, devm_spi_unoptimize_message, msg);
4408d4a0055fSDavid Lechner }
44097e74a45cSDavid Lechner EXPORT_SYMBOL_GPL(devm_spi_optimize_message);
4410d4a0055fSDavid Lechner 
4411568d0697SDavid Brownell /**
4412568d0697SDavid Brownell  * spi_async - asynchronous SPI transfer
4413568d0697SDavid Brownell  * @spi: device with which data will be exchanged
4414568d0697SDavid Brownell  * @message: describes the data transfers, including completion callback
4415702ca026SAndy Shevchenko  * Context: any (IRQs may be blocked, etc)
4416568d0697SDavid Brownell  *
4417568d0697SDavid Brownell  * This call may be used in_irq and other contexts which can't sleep,
4418568d0697SDavid Brownell  * as well as from task contexts which can sleep.
4419568d0697SDavid Brownell  *
4420568d0697SDavid Brownell  * The completion callback is invoked in a context which can't sleep.
4421568d0697SDavid Brownell  * Before that invocation, the value of message->status is undefined.
4422568d0697SDavid Brownell  * When the callback is issued, message->status holds either zero (to
4423568d0697SDavid Brownell  * indicate complete success) or a negative error code.  After that
4424568d0697SDavid Brownell  * callback returns, the driver which issued the transfer request may
4425568d0697SDavid Brownell  * deallocate the associated memory; it's no longer in use by any SPI
4426568d0697SDavid Brownell  * core or controller driver code.
4427568d0697SDavid Brownell  *
4428568d0697SDavid Brownell  * Note that although all messages to a spi_device are handled in
4429568d0697SDavid Brownell  * FIFO order, messages may go to different devices in other orders.
4430568d0697SDavid Brownell  * Some device might be higher priority, or have various "hard" access
4431568d0697SDavid Brownell  * time requirements, for example.
4432568d0697SDavid Brownell  *
4433568d0697SDavid Brownell  * On detection of any fault during the transfer, processing of
4434568d0697SDavid Brownell  * the entire message is aborted, and the device is deselected.
4435568d0697SDavid Brownell  * Until returning from the associated message completion callback,
4436568d0697SDavid Brownell  * no other spi_message queued to that device will be processed.
4437568d0697SDavid Brownell  * (This rule applies equally to all the synchronous transfer calls,
4438568d0697SDavid Brownell  * which are wrappers around this core asynchronous primitive.)
443997d56dc6SJavier Martinez Canillas  *
444097d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
4441568d0697SDavid Brownell  */
4442568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message)
4443568d0697SDavid Brownell {
44448caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
4445cf32b71eSErnst Schwab 	int ret;
4446cf32b71eSErnst Schwab 	unsigned long flags;
4447568d0697SDavid Brownell 
44487b1d87afSDavid Lechner 	ret = spi_maybe_optimize_message(spi, message);
44497b1d87afSDavid Lechner 	if (ret)
445090808738SMark Brown 		return ret;
445190808738SMark Brown 
44528caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4453568d0697SDavid Brownell 
44548caab75fSGeert Uytterhoeven 	if (ctlr->bus_lock_flag)
4455cf32b71eSErnst Schwab 		ret = -EBUSY;
4456cf32b71eSErnst Schwab 	else
4457cf32b71eSErnst Schwab 		ret = __spi_async(spi, message);
4458568d0697SDavid Brownell 
44598caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4460cf32b71eSErnst Schwab 
4461cf32b71eSErnst Schwab 	return ret;
4462568d0697SDavid Brownell }
4463568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async);
4464568d0697SDavid Brownell 
4465ae7d2346SDavid Jander static void __spi_transfer_message_noqueue(struct spi_controller *ctlr, struct spi_message *msg)
4466ae7d2346SDavid Jander {
4467ae7d2346SDavid Jander 	bool was_busy;
4468ae7d2346SDavid Jander 	int ret;
4469ae7d2346SDavid Jander 
4470ae7d2346SDavid Jander 	mutex_lock(&ctlr->io_mutex);
4471ae7d2346SDavid Jander 
44721a9cafcbSDavid Jander 	was_busy = ctlr->busy;
4473ae7d2346SDavid Jander 
447472c5c59bSDavid Jander 	ctlr->cur_msg = msg;
4475ae7d2346SDavid Jander 	ret = __spi_pump_transfer_message(ctlr, msg, was_busy);
4476ae7d2346SDavid Jander 	if (ret)
4477bef4a48fSMark Hasemeyer 		dev_err(&ctlr->dev, "noqueue transfer failed\n");
447869fa9590SDavid Jander 	ctlr->cur_msg = NULL;
447969fa9590SDavid Jander 	ctlr->fallback = false;
448069fa9590SDavid Jander 
4481ae7d2346SDavid Jander 	if (!was_busy) {
4482ae7d2346SDavid Jander 		kfree(ctlr->dummy_rx);
4483ae7d2346SDavid Jander 		ctlr->dummy_rx = NULL;
4484ae7d2346SDavid Jander 		kfree(ctlr->dummy_tx);
4485ae7d2346SDavid Jander 		ctlr->dummy_tx = NULL;
4486ae7d2346SDavid Jander 		if (ctlr->unprepare_transfer_hardware &&
4487ae7d2346SDavid Jander 		    ctlr->unprepare_transfer_hardware(ctlr))
4488ae7d2346SDavid Jander 			dev_err(&ctlr->dev,
4489ae7d2346SDavid Jander 				"failed to unprepare transfer hardware\n");
4490ae7d2346SDavid Jander 		spi_idle_runtime_pm(ctlr);
4491ae7d2346SDavid Jander 	}
4492ae7d2346SDavid Jander 
4493ae7d2346SDavid Jander 	mutex_unlock(&ctlr->io_mutex);
4494ae7d2346SDavid Jander }
4495ae7d2346SDavid Jander 
44967d077197SDavid Brownell /*-------------------------------------------------------------------------*/
44977d077197SDavid Brownell 
4498350de7ceSAndy Shevchenko /*
4499350de7ceSAndy Shevchenko  * Utility methods for SPI protocol drivers, layered on
45007d077197SDavid Brownell  * top of the core.  Some other utility methods are defined as
45017d077197SDavid Brownell  * inline functions.
45027d077197SDavid Brownell  */
45037d077197SDavid Brownell 
45045d870c8eSAndrew Morton static void spi_complete(void *arg)
45055d870c8eSAndrew Morton {
45065d870c8eSAndrew Morton 	complete(arg);
45075d870c8eSAndrew Morton }
45085d870c8eSAndrew Morton 
4509ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message)
4510cf32b71eSErnst Schwab {
4511cf32b71eSErnst Schwab 	DECLARE_COMPLETION_ONSTACK(done);
45120da9a579SDavid Lechner 	unsigned long flags;
4513cf32b71eSErnst Schwab 	int status;
45148caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr = spi->controller;
45150461a414SMark Brown 
4516bef4a48fSMark Hasemeyer 	if (__spi_check_suspended(ctlr)) {
4517bef4a48fSMark Hasemeyer 		dev_warn_once(&spi->dev, "Attempted to sync while suspend\n");
4518bef4a48fSMark Hasemeyer 		return -ESHUTDOWN;
4519bef4a48fSMark Hasemeyer 	}
4520bef4a48fSMark Hasemeyer 
45217b1d87afSDavid Lechner 	status = spi_maybe_optimize_message(spi, message);
45227b1d87afSDavid Lechner 	if (status)
45230461a414SMark Brown 		return status;
4524cf32b71eSErnst Schwab 
45256598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync);
45266598b91bSDavid Jander 	SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync);
4527eca2ebc7SMartin Sperl 
4528350de7ceSAndy Shevchenko 	/*
4529ae7d2346SDavid Jander 	 * Checking queue_empty here only guarantees async/sync message
4530ae7d2346SDavid Jander 	 * ordering when coming from the same context. It does not need to
4531ae7d2346SDavid Jander 	 * guard against reentrancy from a different context. The io_mutex
4532ae7d2346SDavid Jander 	 * will catch those cases.
45330461a414SMark Brown 	 */
4534b30f7c8eSMark Brown 	if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) {
4535ae7d2346SDavid Jander 		message->actual_length = 0;
4536ae7d2346SDavid Jander 		message->status = -EINPROGRESS;
45370461a414SMark Brown 
45380461a414SMark Brown 		trace_spi_message_submit(message);
45390461a414SMark Brown 
4540ae7d2346SDavid Jander 		SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate);
4541ae7d2346SDavid Jander 		SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate);
45420461a414SMark Brown 
4543ae7d2346SDavid Jander 		__spi_transfer_message_noqueue(ctlr, message);
4544ae7d2346SDavid Jander 
4545ae7d2346SDavid Jander 		return message->status;
4546ae7d2346SDavid Jander 	}
4547ae7d2346SDavid Jander 
4548ae7d2346SDavid Jander 	/*
4549ae7d2346SDavid Jander 	 * There are messages in the async queue that could have originated
4550ae7d2346SDavid Jander 	 * from the same context, so we need to preserve ordering.
4551ae7d2346SDavid Jander 	 * Therefor we send the message to the async queue and wait until they
4552ae7d2346SDavid Jander 	 * are completed.
4553ae7d2346SDavid Jander 	 */
4554ae7d2346SDavid Jander 	message->complete = spi_complete;
4555ae7d2346SDavid Jander 	message->context = &done;
45560da9a579SDavid Lechner 
45570da9a579SDavid Lechner 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
45580da9a579SDavid Lechner 	status = __spi_async(spi, message);
45590da9a579SDavid Lechner 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
45600da9a579SDavid Lechner 
4561cf32b71eSErnst Schwab 	if (status == 0) {
4562cf32b71eSErnst Schwab 		wait_for_completion(&done);
4563cf32b71eSErnst Schwab 		status = message->status;
4564cf32b71eSErnst Schwab 	}
45654756fa52SMans Rullgard 	message->complete = NULL;
4566cf32b71eSErnst Schwab 	message->context = NULL;
4567ae7d2346SDavid Jander 
4568cf32b71eSErnst Schwab 	return status;
4569cf32b71eSErnst Schwab }
4570cf32b71eSErnst Schwab 
45718ae12a0dSDavid Brownell /**
45728ae12a0dSDavid Brownell  * spi_sync - blocking/synchronous SPI data transfers
45738ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
45748ae12a0dSDavid Brownell  * @message: describes the data transfers
457533e34dc6SDavid Brownell  * Context: can sleep
45768ae12a0dSDavid Brownell  *
45778ae12a0dSDavid Brownell  * This call may only be used from a context that may sleep.  The sleep
45788ae12a0dSDavid Brownell  * is non-interruptible, and has no timeout.  Low-overhead controller
45798ae12a0dSDavid Brownell  * drivers may DMA directly into and out of the message buffers.
45808ae12a0dSDavid Brownell  *
45818ae12a0dSDavid Brownell  * Note that the SPI device's chip select is active during the message,
45828ae12a0dSDavid Brownell  * and then is normally disabled between messages.  Drivers for some
45838ae12a0dSDavid Brownell  * frequently-used devices may want to minimize costs of selecting a chip,
45848ae12a0dSDavid Brownell  * by leaving it selected in anticipation that the next message will go
45858ae12a0dSDavid Brownell  * to the same chip.  (That may increase power usage.)
45868ae12a0dSDavid Brownell  *
45870c868461SDavid Brownell  * Also, the caller is guaranteeing that the memory associated with the
45880c868461SDavid Brownell  * message will not be freed before this call returns.
45890c868461SDavid Brownell  *
459097d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
45918ae12a0dSDavid Brownell  */
45928ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message)
45938ae12a0dSDavid Brownell {
4594ef4d96ecSMark Brown 	int ret;
4595ef4d96ecSMark Brown 
45968caab75fSGeert Uytterhoeven 	mutex_lock(&spi->controller->bus_lock_mutex);
4597ef4d96ecSMark Brown 	ret = __spi_sync(spi, message);
45988caab75fSGeert Uytterhoeven 	mutex_unlock(&spi->controller->bus_lock_mutex);
4599ef4d96ecSMark Brown 
4600ef4d96ecSMark Brown 	return ret;
46018ae12a0dSDavid Brownell }
46028ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync);
46038ae12a0dSDavid Brownell 
4604cf32b71eSErnst Schwab /**
4605cf32b71eSErnst Schwab  * spi_sync_locked - version of spi_sync with exclusive bus usage
4606cf32b71eSErnst Schwab  * @spi: device with which data will be exchanged
4607cf32b71eSErnst Schwab  * @message: describes the data transfers
4608cf32b71eSErnst Schwab  * Context: can sleep
4609cf32b71eSErnst Schwab  *
4610cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
4611cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.  Low-overhead controller
4612cf32b71eSErnst Schwab  * drivers may DMA directly into and out of the message buffers.
4613cf32b71eSErnst Schwab  *
4614cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
461525985edcSLucas De Marchi  * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4616cf32b71eSErnst Schwab  * be released by a spi_bus_unlock call when the exclusive access is over.
4617cf32b71eSErnst Schwab  *
461897d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
4619cf32b71eSErnst Schwab  */
4620cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
4621cf32b71eSErnst Schwab {
4622ef4d96ecSMark Brown 	return __spi_sync(spi, message);
4623cf32b71eSErnst Schwab }
4624cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked);
4625cf32b71eSErnst Schwab 
4626cf32b71eSErnst Schwab /**
4627cf32b71eSErnst Schwab  * spi_bus_lock - obtain a lock for exclusive SPI bus usage
46288caab75fSGeert Uytterhoeven  * @ctlr: SPI bus master that should be locked for exclusive bus access
4629cf32b71eSErnst Schwab  * Context: can sleep
4630cf32b71eSErnst Schwab  *
4631cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
4632cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
4633cf32b71eSErnst Schwab  *
4634cf32b71eSErnst Schwab  * This call should be used by drivers that require exclusive access to the
4635cf32b71eSErnst Schwab  * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4636cf32b71eSErnst Schwab  * exclusive access is over. Data transfer must be done by spi_sync_locked
4637cf32b71eSErnst Schwab  * and spi_async_locked calls when the SPI bus lock is held.
4638cf32b71eSErnst Schwab  *
463997d56dc6SJavier Martinez Canillas  * Return: always zero.
4640cf32b71eSErnst Schwab  */
46418caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr)
4642cf32b71eSErnst Schwab {
4643cf32b71eSErnst Schwab 	unsigned long flags;
4644cf32b71eSErnst Schwab 
46458caab75fSGeert Uytterhoeven 	mutex_lock(&ctlr->bus_lock_mutex);
4646cf32b71eSErnst Schwab 
46478caab75fSGeert Uytterhoeven 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
46488caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 1;
46498caab75fSGeert Uytterhoeven 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4650cf32b71eSErnst Schwab 
465195c8222fSDavid Jander 	/* Mutex remains locked until spi_bus_unlock() is called */
4652cf32b71eSErnst Schwab 
4653cf32b71eSErnst Schwab 	return 0;
4654cf32b71eSErnst Schwab }
4655cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock);
4656cf32b71eSErnst Schwab 
4657cf32b71eSErnst Schwab /**
4658cf32b71eSErnst Schwab  * spi_bus_unlock - release the lock for exclusive SPI bus usage
46598caab75fSGeert Uytterhoeven  * @ctlr: SPI bus master that was locked for exclusive bus access
4660cf32b71eSErnst Schwab  * Context: can sleep
4661cf32b71eSErnst Schwab  *
4662cf32b71eSErnst Schwab  * This call may only be used from a context that may sleep.  The sleep
4663cf32b71eSErnst Schwab  * is non-interruptible, and has no timeout.
4664cf32b71eSErnst Schwab  *
4665cf32b71eSErnst Schwab  * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4666cf32b71eSErnst Schwab  * call.
4667cf32b71eSErnst Schwab  *
466897d56dc6SJavier Martinez Canillas  * Return: always zero.
4669cf32b71eSErnst Schwab  */
46708caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr)
4671cf32b71eSErnst Schwab {
46728caab75fSGeert Uytterhoeven 	ctlr->bus_lock_flag = 0;
4673cf32b71eSErnst Schwab 
46748caab75fSGeert Uytterhoeven 	mutex_unlock(&ctlr->bus_lock_mutex);
4675cf32b71eSErnst Schwab 
4676cf32b71eSErnst Schwab 	return 0;
4677cf32b71eSErnst Schwab }
4678cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock);
4679cf32b71eSErnst Schwab 
468095c8222fSDavid Jander /* Portable code must never pass more than 32 bytes */
4681a9948b61SDavid Brownell #define	SPI_BUFSIZ	max(32, SMP_CACHE_BYTES)
46828ae12a0dSDavid Brownell 
46838ae12a0dSDavid Brownell static u8	*buf;
46848ae12a0dSDavid Brownell 
46858ae12a0dSDavid Brownell /**
46868ae12a0dSDavid Brownell  * spi_write_then_read - SPI synchronous write followed by read
46878ae12a0dSDavid Brownell  * @spi: device with which data will be exchanged
4688702ca026SAndy Shevchenko  * @txbuf: data to be written (need not be DMA-safe)
46898ae12a0dSDavid Brownell  * @n_tx: size of txbuf, in bytes
4690702ca026SAndy Shevchenko  * @rxbuf: buffer into which data will be read (need not be DMA-safe)
469127570497SJiri Pirko  * @n_rx: size of rxbuf, in bytes
469233e34dc6SDavid Brownell  * Context: can sleep
46938ae12a0dSDavid Brownell  *
46948ae12a0dSDavid Brownell  * This performs a half duplex MicroWire style transaction with the
46958ae12a0dSDavid Brownell  * device, sending txbuf and then reading rxbuf.  The return value
46968ae12a0dSDavid Brownell  * is zero for success, else a negative errno status code.
4697b885244eSDavid Brownell  * This call may only be used from a context that may sleep.
46988ae12a0dSDavid Brownell  *
4699c373643bSMark Brown  * Parameters to this routine are always copied using a small buffer.
470033e34dc6SDavid Brownell  * Performance-sensitive or bulk transfer code should instead use
4701702ca026SAndy Shevchenko  * spi_{async,sync}() calls with DMA-safe buffers.
470297d56dc6SJavier Martinez Canillas  *
470397d56dc6SJavier Martinez Canillas  * Return: zero on success, else a negative error code.
47048ae12a0dSDavid Brownell  */
47058ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi,
47060c4a1590SMark Brown 		const void *txbuf, unsigned n_tx,
47070c4a1590SMark Brown 		void *rxbuf, unsigned n_rx)
47088ae12a0dSDavid Brownell {
4709068f4070SDavid Brownell 	static DEFINE_MUTEX(lock);
47108ae12a0dSDavid Brownell 
47118ae12a0dSDavid Brownell 	int			status;
47128ae12a0dSDavid Brownell 	struct spi_message	message;
4713bdff549eSDavid Brownell 	struct spi_transfer	x[2];
47148ae12a0dSDavid Brownell 	u8			*local_buf;
47158ae12a0dSDavid Brownell 
4716350de7ceSAndy Shevchenko 	/*
4717350de7ceSAndy Shevchenko 	 * Use preallocated DMA-safe buffer if we can. We can't avoid
4718b3a223eeSMark Brown 	 * copying here, (as a pure convenience thing), but we can
4719b3a223eeSMark Brown 	 * keep heap costs out of the hot path unless someone else is
4720b3a223eeSMark Brown 	 * using the pre-allocated buffer or the transfer is too large.
47218ae12a0dSDavid Brownell 	 */
4722b3a223eeSMark Brown 	if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
47232cd94c8aSMark Brown 		local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
47242cd94c8aSMark Brown 				    GFP_KERNEL | GFP_DMA);
4725b3a223eeSMark Brown 		if (!local_buf)
4726b3a223eeSMark Brown 			return -ENOMEM;
4727b3a223eeSMark Brown 	} else {
4728b3a223eeSMark Brown 		local_buf = buf;
4729b3a223eeSMark Brown 	}
47308ae12a0dSDavid Brownell 
47318275c642SVitaly Wool 	spi_message_init(&message);
47325fe5f05eSJingoo Han 	memset(x, 0, sizeof(x));
4733bdff549eSDavid Brownell 	if (n_tx) {
4734bdff549eSDavid Brownell 		x[0].len = n_tx;
4735bdff549eSDavid Brownell 		spi_message_add_tail(&x[0], &message);
4736bdff549eSDavid Brownell 	}
4737bdff549eSDavid Brownell 	if (n_rx) {
4738bdff549eSDavid Brownell 		x[1].len = n_rx;
4739bdff549eSDavid Brownell 		spi_message_add_tail(&x[1], &message);
4740bdff549eSDavid Brownell 	}
47418275c642SVitaly Wool 
47428ae12a0dSDavid Brownell 	memcpy(local_buf, txbuf, n_tx);
4743bdff549eSDavid Brownell 	x[0].tx_buf = local_buf;
4744bdff549eSDavid Brownell 	x[1].rx_buf = local_buf + n_tx;
47458ae12a0dSDavid Brownell 
4746702ca026SAndy Shevchenko 	/* Do the I/O */
47478ae12a0dSDavid Brownell 	status = spi_sync(spi, &message);
47489b938b74SMarc Pignat 	if (status == 0)
4749bdff549eSDavid Brownell 		memcpy(rxbuf, x[1].rx_buf, n_rx);
47508ae12a0dSDavid Brownell 
4751bdff549eSDavid Brownell 	if (x[0].tx_buf == buf)
4752068f4070SDavid Brownell 		mutex_unlock(&lock);
47538ae12a0dSDavid Brownell 	else
47548ae12a0dSDavid Brownell 		kfree(local_buf);
47558ae12a0dSDavid Brownell 
47568ae12a0dSDavid Brownell 	return status;
47578ae12a0dSDavid Brownell }
47588ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read);
47598ae12a0dSDavid Brownell 
47608ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/
47618ae12a0dSDavid Brownell 
4762da21fde0SUwe Kleine-König #if IS_ENABLED(CONFIG_OF_DYNAMIC)
476395c8222fSDavid Jander /* Must call put_device() when done with returned spi_device device */
4764da21fde0SUwe Kleine-König static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
4765ce79d54aSPantelis Antoniou {
4766cfba5de9SSuzuki K Poulose 	struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
4767cfba5de9SSuzuki K Poulose 
4768ce79d54aSPantelis Antoniou 	return dev ? to_spi_device(dev) : NULL;
4769ce79d54aSPantelis Antoniou }
4770ce79d54aSPantelis Antoniou 
477195c8222fSDavid Jander /* The spi controllers are not using spi_bus, so we find it with another way */
47728caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
4773ce79d54aSPantelis Antoniou {
4774ce79d54aSPantelis Antoniou 	struct device *dev;
4775ce79d54aSPantelis Antoniou 
4776cfba5de9SSuzuki K Poulose 	dev = class_find_device_by_of_node(&spi_master_class, node);
47776c364062SGeert Uytterhoeven 	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4778cfba5de9SSuzuki K Poulose 		dev = class_find_device_by_of_node(&spi_slave_class, node);
4779ce79d54aSPantelis Antoniou 	if (!dev)
4780ce79d54aSPantelis Antoniou 		return NULL;
4781ce79d54aSPantelis Antoniou 
478295c8222fSDavid Jander 	/* Reference got in class_find_device */
47838caab75fSGeert Uytterhoeven 	return container_of(dev, struct spi_controller, dev);
4784ce79d54aSPantelis Antoniou }
4785ce79d54aSPantelis Antoniou 
4786ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action,
4787ce79d54aSPantelis Antoniou 			 void *arg)
4788ce79d54aSPantelis Antoniou {
4789ce79d54aSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
47908caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
4791ce79d54aSPantelis Antoniou 	struct spi_device *spi;
4792ce79d54aSPantelis Antoniou 
4793ce79d54aSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, arg)) {
4794ce79d54aSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
47958caab75fSGeert Uytterhoeven 		ctlr = of_find_spi_controller_by_node(rd->dn->parent);
47968caab75fSGeert Uytterhoeven 		if (ctlr == NULL)
479795c8222fSDavid Jander 			return NOTIFY_OK;	/* Not for us */
4798ce79d54aSPantelis Antoniou 
4799bd6c1644SGeert Uytterhoeven 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
48008caab75fSGeert Uytterhoeven 			put_device(&ctlr->dev);
4801bd6c1644SGeert Uytterhoeven 			return NOTIFY_OK;
4802bd6c1644SGeert Uytterhoeven 		}
4803bd6c1644SGeert Uytterhoeven 
48041a50d940SGeert Uytterhoeven 		/*
48051a50d940SGeert Uytterhoeven 		 * Clear the flag before adding the device so that fw_devlink
48061a50d940SGeert Uytterhoeven 		 * doesn't skip adding consumers to this device.
48071a50d940SGeert Uytterhoeven 		 */
48081a50d940SGeert Uytterhoeven 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
48098caab75fSGeert Uytterhoeven 		spi = of_register_spi_device(ctlr, rd->dn);
48108caab75fSGeert Uytterhoeven 		put_device(&ctlr->dev);
4811ce79d54aSPantelis Antoniou 
4812ce79d54aSPantelis Antoniou 		if (IS_ERR(spi)) {
481325c56c88SRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
481425c56c88SRob Herring 					__func__, rd->dn);
4815e0af98a7SRalf Ramsauer 			of_node_clear_flag(rd->dn, OF_POPULATED);
4816ce79d54aSPantelis Antoniou 			return notifier_from_errno(PTR_ERR(spi));
4817ce79d54aSPantelis Antoniou 		}
4818ce79d54aSPantelis Antoniou 		break;
4819ce79d54aSPantelis Antoniou 
4820ce79d54aSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
482195c8222fSDavid Jander 		/* Already depopulated? */
4822bd6c1644SGeert Uytterhoeven 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
4823bd6c1644SGeert Uytterhoeven 			return NOTIFY_OK;
4824bd6c1644SGeert Uytterhoeven 
482595c8222fSDavid Jander 		/* Find our device by node */
4826ce79d54aSPantelis Antoniou 		spi = of_find_spi_device_by_node(rd->dn);
4827ce79d54aSPantelis Antoniou 		if (spi == NULL)
482895c8222fSDavid Jander 			return NOTIFY_OK;	/* No? not meant for us */
4829ce79d54aSPantelis Antoniou 
483095c8222fSDavid Jander 		/* Unregister takes one ref away */
4831ce79d54aSPantelis Antoniou 		spi_unregister_device(spi);
4832ce79d54aSPantelis Antoniou 
483395c8222fSDavid Jander 		/* And put the reference of the find */
4834ce79d54aSPantelis Antoniou 		put_device(&spi->dev);
4835ce79d54aSPantelis Antoniou 		break;
4836ce79d54aSPantelis Antoniou 	}
4837ce79d54aSPantelis Antoniou 
4838ce79d54aSPantelis Antoniou 	return NOTIFY_OK;
4839ce79d54aSPantelis Antoniou }
4840ce79d54aSPantelis Antoniou 
4841ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = {
4842ce79d54aSPantelis Antoniou 	.notifier_call = of_spi_notify,
4843ce79d54aSPantelis Antoniou };
4844ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4845ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier;
4846ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4847ce79d54aSPantelis Antoniou 
48487f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI)
48498caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data)
48507f24467fSOctavian Purdila {
48517f24467fSOctavian Purdila 	return ACPI_COMPANION(dev->parent) == data;
48527f24467fSOctavian Purdila }
48537f24467fSOctavian Purdila 
4854a8ecbc54SHans de Goede struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
48557f24467fSOctavian Purdila {
48567f24467fSOctavian Purdila 	struct device *dev;
48577f24467fSOctavian Purdila 
48587f24467fSOctavian Purdila 	dev = class_find_device(&spi_master_class, NULL, adev,
48598caab75fSGeert Uytterhoeven 				spi_acpi_controller_match);
48606c364062SGeert Uytterhoeven 	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
48616c364062SGeert Uytterhoeven 		dev = class_find_device(&spi_slave_class, NULL, adev,
48628caab75fSGeert Uytterhoeven 					spi_acpi_controller_match);
48637f24467fSOctavian Purdila 	if (!dev)
48647f24467fSOctavian Purdila 		return NULL;
48657f24467fSOctavian Purdila 
48668caab75fSGeert Uytterhoeven 	return container_of(dev, struct spi_controller, dev);
48677f24467fSOctavian Purdila }
4868a8ecbc54SHans de Goede EXPORT_SYMBOL_GPL(acpi_spi_find_controller_by_adev);
48697f24467fSOctavian Purdila 
48707f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
48717f24467fSOctavian Purdila {
48727f24467fSOctavian Purdila 	struct device *dev;
48737f24467fSOctavian Purdila 
487400500147SSuzuki K Poulose 	dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
48755b16668eSWolfram Sang 	return to_spi_device(dev);
48767f24467fSOctavian Purdila }
48777f24467fSOctavian Purdila 
48787f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
48797f24467fSOctavian Purdila 			   void *arg)
48807f24467fSOctavian Purdila {
48817f24467fSOctavian Purdila 	struct acpi_device *adev = arg;
48828caab75fSGeert Uytterhoeven 	struct spi_controller *ctlr;
48837f24467fSOctavian Purdila 	struct spi_device *spi;
48847f24467fSOctavian Purdila 
48857f24467fSOctavian Purdila 	switch (value) {
48867f24467fSOctavian Purdila 	case ACPI_RECONFIG_DEVICE_ADD:
488762fcb99bSRafael J. Wysocki 		ctlr = acpi_spi_find_controller_by_adev(acpi_dev_parent(adev));
48888caab75fSGeert Uytterhoeven 		if (!ctlr)
48897f24467fSOctavian Purdila 			break;
48907f24467fSOctavian Purdila 
48918caab75fSGeert Uytterhoeven 		acpi_register_spi_device(ctlr, adev);
48928caab75fSGeert Uytterhoeven 		put_device(&ctlr->dev);
48937f24467fSOctavian Purdila 		break;
48947f24467fSOctavian Purdila 	case ACPI_RECONFIG_DEVICE_REMOVE:
48957f24467fSOctavian Purdila 		if (!acpi_device_enumerated(adev))
48967f24467fSOctavian Purdila 			break;
48977f24467fSOctavian Purdila 
48987f24467fSOctavian Purdila 		spi = acpi_spi_find_device_by_adev(adev);
48997f24467fSOctavian Purdila 		if (!spi)
49007f24467fSOctavian Purdila 			break;
49017f24467fSOctavian Purdila 
49027f24467fSOctavian Purdila 		spi_unregister_device(spi);
49037f24467fSOctavian Purdila 		put_device(&spi->dev);
49047f24467fSOctavian Purdila 		break;
49057f24467fSOctavian Purdila 	}
49067f24467fSOctavian Purdila 
49077f24467fSOctavian Purdila 	return NOTIFY_OK;
49087f24467fSOctavian Purdila }
49097f24467fSOctavian Purdila 
49107f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = {
49117f24467fSOctavian Purdila 	.notifier_call = acpi_spi_notify,
49127f24467fSOctavian Purdila };
49137f24467fSOctavian Purdila #else
49147f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier;
49157f24467fSOctavian Purdila #endif
49167f24467fSOctavian Purdila 
49178ae12a0dSDavid Brownell static int __init spi_init(void)
49188ae12a0dSDavid Brownell {
4919b885244eSDavid Brownell 	int	status;
49208ae12a0dSDavid Brownell 
4921e94b1766SChristoph Lameter 	buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
4922b885244eSDavid Brownell 	if (!buf) {
4923b885244eSDavid Brownell 		status = -ENOMEM;
4924b885244eSDavid Brownell 		goto err0;
49258ae12a0dSDavid Brownell 	}
4926b885244eSDavid Brownell 
4927b885244eSDavid Brownell 	status = bus_register(&spi_bus_type);
4928b885244eSDavid Brownell 	if (status < 0)
4929b885244eSDavid Brownell 		goto err1;
4930b885244eSDavid Brownell 
4931b885244eSDavid Brownell 	status = class_register(&spi_master_class);
4932b885244eSDavid Brownell 	if (status < 0)
4933b885244eSDavid Brownell 		goto err2;
4934ce79d54aSPantelis Antoniou 
49356c364062SGeert Uytterhoeven 	if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
49366c364062SGeert Uytterhoeven 		status = class_register(&spi_slave_class);
49376c364062SGeert Uytterhoeven 		if (status < 0)
49386c364062SGeert Uytterhoeven 			goto err3;
49396c364062SGeert Uytterhoeven 	}
49406c364062SGeert Uytterhoeven 
49415267720eSFabio Estevam 	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
4942ce79d54aSPantelis Antoniou 		WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
49437f24467fSOctavian Purdila 	if (IS_ENABLED(CONFIG_ACPI))
49447f24467fSOctavian Purdila 		WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
4945ce79d54aSPantelis Antoniou 
4946b885244eSDavid Brownell 	return 0;
4947b885244eSDavid Brownell 
49486c364062SGeert Uytterhoeven err3:
49496c364062SGeert Uytterhoeven 	class_unregister(&spi_master_class);
4950b885244eSDavid Brownell err2:
4951b885244eSDavid Brownell 	bus_unregister(&spi_bus_type);
4952b885244eSDavid Brownell err1:
4953b885244eSDavid Brownell 	kfree(buf);
4954b885244eSDavid Brownell 	buf = NULL;
4955b885244eSDavid Brownell err0:
4956b885244eSDavid Brownell 	return status;
4957b885244eSDavid Brownell }
4958b885244eSDavid Brownell 
4959350de7ceSAndy Shevchenko /*
4960350de7ceSAndy Shevchenko  * A board_info is normally registered in arch_initcall(),
4961350de7ceSAndy Shevchenko  * but even essential drivers wait till later.
4962b885244eSDavid Brownell  *
4963350de7ceSAndy Shevchenko  * REVISIT only boardinfo really needs static linking. The rest (device and
4964350de7ceSAndy Shevchenko  * driver registration) _could_ be dynamically linked (modular) ... Costs
4965b885244eSDavid Brownell  * include needing to have boardinfo data structures be much more public.
49668ae12a0dSDavid Brownell  */
4967673c0c00SDavid Brownell postcore_initcall(spi_init);
4968