xref: /linux/drivers/spi/spi.c (revision c41cfae42c75a40783a6fc402f66c3c7852961e2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPI init/core code
3 //
4 // Copyright (C) 2005 David Brownell
5 // Copyright (C) 2008 Secret Lab Technologies Ltd.
6 
7 #include <linux/acpi.h>
8 #include <linux/cache.h>
9 #include <linux/clk/clk-conf.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dmaengine.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/export.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/highmem.h>
17 #include <linux/idr.h>
18 #include <linux/init.h>
19 #include <linux/ioport.h>
20 #include <linux/kernel.h>
21 #include <linux/kthread.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/mutex.h>
24 #include <linux/of_device.h>
25 #include <linux/of_irq.h>
26 #include <linux/percpu.h>
27 #include <linux/platform_data/x86/apple.h>
28 #include <linux/pm_domain.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/property.h>
31 #include <linux/ptp_clock_kernel.h>
32 #include <linux/sched/rt.h>
33 #include <linux/slab.h>
34 #include <linux/spi/offload/types.h>
35 #include <linux/spi/spi.h>
36 #include <linux/spi/spi-mem.h>
37 #include <uapi/linux/sched/types.h>
38 
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/spi.h>
41 EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
42 EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
43 
44 #include "internals.h"
45 
46 static int __spi_setup(struct spi_device *spi, bool initial_setup);
47 
48 static DEFINE_IDR(spi_controller_idr);
49 
50 static void spidev_release(struct device *dev)
51 {
52 	struct spi_device	*spi = to_spi_device(dev);
53 
54 	spi_controller_put(spi->controller);
55 	free_percpu(spi->pcpu_statistics);
56 	kfree(spi);
57 }
58 
59 static ssize_t
60 modalias_show(struct device *dev, struct device_attribute *a, char *buf)
61 {
62 	const struct spi_device	*spi = to_spi_device(dev);
63 	int len;
64 
65 	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
66 	if (len != -ENODEV)
67 		return len;
68 
69 	return sysfs_emit(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
70 }
71 static DEVICE_ATTR_RO(modalias);
72 
73 static ssize_t driver_override_store(struct device *dev,
74 				     struct device_attribute *a,
75 				     const char *buf, size_t count)
76 {
77 	int ret;
78 
79 	ret = __device_set_driver_override(dev, buf, count);
80 	if (ret)
81 		return ret;
82 
83 	return count;
84 }
85 
86 static ssize_t driver_override_show(struct device *dev,
87 				    struct device_attribute *a, char *buf)
88 {
89 	guard(spinlock)(&dev->driver_override.lock);
90 	return sysfs_emit(buf, "%s\n", dev->driver_override.name ?: "");
91 }
92 static DEVICE_ATTR_RW(driver_override);
93 
94 static struct spi_statistics __percpu *spi_alloc_pcpu_stats(void)
95 {
96 	struct spi_statistics __percpu *pcpu_stats;
97 	int cpu;
98 
99 	pcpu_stats = alloc_percpu_gfp(struct spi_statistics, GFP_KERNEL);
100 	if (!pcpu_stats)
101 		return NULL;
102 
103 	for_each_possible_cpu(cpu) {
104 		struct spi_statistics *stat;
105 
106 		stat = per_cpu_ptr(pcpu_stats, cpu);
107 		u64_stats_init(&stat->syncp);
108 	}
109 
110 	return pcpu_stats;
111 }
112 
113 static ssize_t spi_emit_pcpu_stats(struct spi_statistics __percpu *stat,
114 				   char *buf, size_t offset)
115 {
116 	u64 val = 0;
117 	int i;
118 
119 	for_each_possible_cpu(i) {
120 		const struct spi_statistics *pcpu_stats;
121 		u64_stats_t *field;
122 		unsigned int start;
123 		u64 inc;
124 
125 		pcpu_stats = per_cpu_ptr(stat, i);
126 		field = (void *)pcpu_stats + offset;
127 		do {
128 			start = u64_stats_fetch_begin(&pcpu_stats->syncp);
129 			inc = u64_stats_read(field);
130 		} while (u64_stats_fetch_retry(&pcpu_stats->syncp, start));
131 		val += inc;
132 	}
133 	return sysfs_emit(buf, "%llu\n", val);
134 }
135 
136 #define SPI_STATISTICS_ATTRS(field, file)				\
137 static ssize_t spi_controller_##field##_show(struct device *dev,	\
138 					     struct device_attribute *attr, \
139 					     char *buf)			\
140 {									\
141 	struct spi_controller *ctlr = container_of(dev,			\
142 					 struct spi_controller, dev);	\
143 	return spi_statistics_##field##_show(ctlr->pcpu_statistics, buf); \
144 }									\
145 static struct device_attribute dev_attr_spi_controller_##field = {	\
146 	.attr = { .name = file, .mode = 0444 },				\
147 	.show = spi_controller_##field##_show,				\
148 };									\
149 static ssize_t spi_device_##field##_show(struct device *dev,		\
150 					 struct device_attribute *attr,	\
151 					char *buf)			\
152 {									\
153 	struct spi_device *spi = to_spi_device(dev);			\
154 	return spi_statistics_##field##_show(spi->pcpu_statistics, buf); \
155 }									\
156 static struct device_attribute dev_attr_spi_device_##field = {		\
157 	.attr = { .name = file, .mode = 0444 },				\
158 	.show = spi_device_##field##_show,				\
159 }
160 
161 #define SPI_STATISTICS_SHOW_NAME(name, file, field)			\
162 static ssize_t spi_statistics_##name##_show(struct spi_statistics __percpu *stat, \
163 					    char *buf)			\
164 {									\
165 	return spi_emit_pcpu_stats(stat, buf,				\
166 			offsetof(struct spi_statistics, field));	\
167 }									\
168 SPI_STATISTICS_ATTRS(name, file)
169 
170 #define SPI_STATISTICS_SHOW(field)					\
171 	SPI_STATISTICS_SHOW_NAME(field, __stringify(field),		\
172 				 field)
173 
174 SPI_STATISTICS_SHOW(messages);
175 SPI_STATISTICS_SHOW(transfers);
176 SPI_STATISTICS_SHOW(errors);
177 SPI_STATISTICS_SHOW(timedout);
178 
179 SPI_STATISTICS_SHOW(spi_sync);
180 SPI_STATISTICS_SHOW(spi_sync_immediate);
181 SPI_STATISTICS_SHOW(spi_async);
182 
183 SPI_STATISTICS_SHOW(bytes);
184 SPI_STATISTICS_SHOW(bytes_rx);
185 SPI_STATISTICS_SHOW(bytes_tx);
186 
187 #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number)		\
188 	SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index,		\
189 				 "transfer_bytes_histo_" number,	\
190 				 transfer_bytes_histo[index])
191 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0,  "0-1");
192 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1,  "2-3");
193 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2,  "4-7");
194 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3,  "8-15");
195 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4,  "16-31");
196 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5,  "32-63");
197 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6,  "64-127");
198 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7,  "128-255");
199 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8,  "256-511");
200 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9,  "512-1023");
201 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
202 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
203 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
204 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
205 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
206 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
207 SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
208 
209 SPI_STATISTICS_SHOW(transfers_split_maxsize);
210 
211 static struct attribute *spi_dev_attrs[] = {
212 	&dev_attr_modalias.attr,
213 	&dev_attr_driver_override.attr,
214 	NULL,
215 };
216 
217 static const struct attribute_group spi_dev_group = {
218 	.attrs  = spi_dev_attrs,
219 };
220 
221 static struct attribute *spi_device_statistics_attrs[] = {
222 	&dev_attr_spi_device_messages.attr,
223 	&dev_attr_spi_device_transfers.attr,
224 	&dev_attr_spi_device_errors.attr,
225 	&dev_attr_spi_device_timedout.attr,
226 	&dev_attr_spi_device_spi_sync.attr,
227 	&dev_attr_spi_device_spi_sync_immediate.attr,
228 	&dev_attr_spi_device_spi_async.attr,
229 	&dev_attr_spi_device_bytes.attr,
230 	&dev_attr_spi_device_bytes_rx.attr,
231 	&dev_attr_spi_device_bytes_tx.attr,
232 	&dev_attr_spi_device_transfer_bytes_histo0.attr,
233 	&dev_attr_spi_device_transfer_bytes_histo1.attr,
234 	&dev_attr_spi_device_transfer_bytes_histo2.attr,
235 	&dev_attr_spi_device_transfer_bytes_histo3.attr,
236 	&dev_attr_spi_device_transfer_bytes_histo4.attr,
237 	&dev_attr_spi_device_transfer_bytes_histo5.attr,
238 	&dev_attr_spi_device_transfer_bytes_histo6.attr,
239 	&dev_attr_spi_device_transfer_bytes_histo7.attr,
240 	&dev_attr_spi_device_transfer_bytes_histo8.attr,
241 	&dev_attr_spi_device_transfer_bytes_histo9.attr,
242 	&dev_attr_spi_device_transfer_bytes_histo10.attr,
243 	&dev_attr_spi_device_transfer_bytes_histo11.attr,
244 	&dev_attr_spi_device_transfer_bytes_histo12.attr,
245 	&dev_attr_spi_device_transfer_bytes_histo13.attr,
246 	&dev_attr_spi_device_transfer_bytes_histo14.attr,
247 	&dev_attr_spi_device_transfer_bytes_histo15.attr,
248 	&dev_attr_spi_device_transfer_bytes_histo16.attr,
249 	&dev_attr_spi_device_transfers_split_maxsize.attr,
250 	NULL,
251 };
252 
253 static const struct attribute_group spi_device_statistics_group = {
254 	.name  = "statistics",
255 	.attrs  = spi_device_statistics_attrs,
256 };
257 
258 static const struct attribute_group *spi_dev_groups[] = {
259 	&spi_dev_group,
260 	&spi_device_statistics_group,
261 	NULL,
262 };
263 
264 static struct attribute *spi_controller_statistics_attrs[] = {
265 	&dev_attr_spi_controller_messages.attr,
266 	&dev_attr_spi_controller_transfers.attr,
267 	&dev_attr_spi_controller_errors.attr,
268 	&dev_attr_spi_controller_timedout.attr,
269 	&dev_attr_spi_controller_spi_sync.attr,
270 	&dev_attr_spi_controller_spi_sync_immediate.attr,
271 	&dev_attr_spi_controller_spi_async.attr,
272 	&dev_attr_spi_controller_bytes.attr,
273 	&dev_attr_spi_controller_bytes_rx.attr,
274 	&dev_attr_spi_controller_bytes_tx.attr,
275 	&dev_attr_spi_controller_transfer_bytes_histo0.attr,
276 	&dev_attr_spi_controller_transfer_bytes_histo1.attr,
277 	&dev_attr_spi_controller_transfer_bytes_histo2.attr,
278 	&dev_attr_spi_controller_transfer_bytes_histo3.attr,
279 	&dev_attr_spi_controller_transfer_bytes_histo4.attr,
280 	&dev_attr_spi_controller_transfer_bytes_histo5.attr,
281 	&dev_attr_spi_controller_transfer_bytes_histo6.attr,
282 	&dev_attr_spi_controller_transfer_bytes_histo7.attr,
283 	&dev_attr_spi_controller_transfer_bytes_histo8.attr,
284 	&dev_attr_spi_controller_transfer_bytes_histo9.attr,
285 	&dev_attr_spi_controller_transfer_bytes_histo10.attr,
286 	&dev_attr_spi_controller_transfer_bytes_histo11.attr,
287 	&dev_attr_spi_controller_transfer_bytes_histo12.attr,
288 	&dev_attr_spi_controller_transfer_bytes_histo13.attr,
289 	&dev_attr_spi_controller_transfer_bytes_histo14.attr,
290 	&dev_attr_spi_controller_transfer_bytes_histo15.attr,
291 	&dev_attr_spi_controller_transfer_bytes_histo16.attr,
292 	&dev_attr_spi_controller_transfers_split_maxsize.attr,
293 	NULL,
294 };
295 
296 static const struct attribute_group spi_controller_statistics_group = {
297 	.name  = "statistics",
298 	.attrs  = spi_controller_statistics_attrs,
299 };
300 
301 static const struct attribute_group *spi_controller_groups[] = {
302 	&spi_controller_statistics_group,
303 	NULL,
304 };
305 
306 static void spi_statistics_add_transfer_stats(struct spi_statistics __percpu *pcpu_stats,
307 					      struct spi_transfer *xfer,
308 					      struct spi_message *msg)
309 {
310 	int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
311 	struct spi_statistics *stats;
312 
313 	if (l2len < 0)
314 		l2len = 0;
315 
316 	get_cpu();
317 	stats = this_cpu_ptr(pcpu_stats);
318 	u64_stats_update_begin(&stats->syncp);
319 
320 	u64_stats_inc(&stats->transfers);
321 	u64_stats_inc(&stats->transfer_bytes_histo[l2len]);
322 
323 	u64_stats_add(&stats->bytes, xfer->len);
324 	if (spi_valid_txbuf(msg, xfer))
325 		u64_stats_add(&stats->bytes_tx, xfer->len);
326 	if (spi_valid_rxbuf(msg, xfer))
327 		u64_stats_add(&stats->bytes_rx, xfer->len);
328 
329 	u64_stats_update_end(&stats->syncp);
330 	put_cpu();
331 }
332 
333 /*
334  * modalias support makes "modprobe $MODALIAS" new-style hotplug work,
335  * and the sysfs version makes coldplug work too.
336  */
337 static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, const char *name)
338 {
339 	while (id->name[0]) {
340 		if (!strcmp(name, id->name))
341 			return id;
342 		id++;
343 	}
344 	return NULL;
345 }
346 
347 const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
348 {
349 	const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
350 
351 	return spi_match_id(sdrv->id_table, sdev->modalias);
352 }
353 EXPORT_SYMBOL_GPL(spi_get_device_id);
354 
355 const void *spi_get_device_match_data(const struct spi_device *sdev)
356 {
357 	const void *match;
358 
359 	match = device_get_match_data(&sdev->dev);
360 	if (match)
361 		return match;
362 
363 	return (const void *)spi_get_device_id(sdev)->driver_data;
364 }
365 EXPORT_SYMBOL_GPL(spi_get_device_match_data);
366 
367 static int spi_match_device(struct device *dev, const struct device_driver *drv)
368 {
369 	const struct spi_device	*spi = to_spi_device(dev);
370 	const struct spi_driver	*sdrv = to_spi_driver(drv);
371 	int ret;
372 
373 	/* Check override first, and if set, only use the named driver */
374 	ret = device_match_driver_override(dev, drv);
375 	if (ret >= 0)
376 		return ret;
377 
378 	/* Attempt an OF style match */
379 	if (of_driver_match_device(dev, drv))
380 		return 1;
381 
382 	/* Then try ACPI */
383 	if (acpi_driver_match_device(dev, drv))
384 		return 1;
385 
386 	if (sdrv->id_table)
387 		return !!spi_match_id(sdrv->id_table, spi->modalias);
388 
389 	return strcmp(spi->modalias, drv->name) == 0;
390 }
391 
392 static int spi_uevent(const struct device *dev, struct kobj_uevent_env *env)
393 {
394 	const struct spi_device		*spi = to_spi_device(dev);
395 	int rc;
396 
397 	rc = acpi_device_uevent_modalias(dev, env);
398 	if (rc != -ENODEV)
399 		return rc;
400 
401 	return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
402 }
403 
404 static int spi_probe(struct device *dev)
405 {
406 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
407 	struct spi_device		*spi = to_spi_device(dev);
408 	struct fwnode_handle		*fwnode = dev_fwnode(dev);
409 	int ret;
410 
411 	ret = of_clk_set_defaults(dev->of_node, false);
412 	if (ret)
413 		return ret;
414 
415 	if (is_of_node(fwnode))
416 		spi->irq = of_irq_get(dev->of_node, 0);
417 	else if (is_acpi_device_node(fwnode) && spi->irq < 0)
418 		spi->irq = acpi_dev_gpio_irq_get(to_acpi_device_node(fwnode), 0);
419 	if (spi->irq == -EPROBE_DEFER)
420 		return dev_err_probe(dev, spi->irq, "Failed to get irq\n");
421 	if (spi->irq < 0)
422 		spi->irq = 0;
423 
424 	ret = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON |
425 					PD_FLAG_DETACH_POWER_OFF);
426 	if (ret)
427 		return ret;
428 
429 	if (sdrv->probe)
430 		ret = sdrv->probe(spi);
431 
432 	return ret;
433 }
434 
435 static void spi_remove(struct device *dev)
436 {
437 	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
438 
439 	if (sdrv->remove)
440 		sdrv->remove(to_spi_device(dev));
441 }
442 
443 static void spi_shutdown(struct device *dev)
444 {
445 	if (dev->driver) {
446 		const struct spi_driver	*sdrv = to_spi_driver(dev->driver);
447 
448 		if (sdrv->shutdown)
449 			sdrv->shutdown(to_spi_device(dev));
450 	}
451 }
452 
453 const struct bus_type spi_bus_type = {
454 	.name		= "spi",
455 	.dev_groups	= spi_dev_groups,
456 	.match		= spi_match_device,
457 	.uevent		= spi_uevent,
458 	.probe		= spi_probe,
459 	.remove		= spi_remove,
460 	.shutdown	= spi_shutdown,
461 };
462 EXPORT_SYMBOL_GPL(spi_bus_type);
463 
464 /**
465  * __spi_register_driver - register a SPI driver
466  * @owner: owner module of the driver to register
467  * @sdrv: the driver to register
468  * Context: can sleep
469  *
470  * Return: zero on success, else a negative error code.
471  */
472 int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
473 {
474 	sdrv->driver.owner = owner;
475 	sdrv->driver.bus = &spi_bus_type;
476 
477 	/*
478 	 * For Really Good Reasons we use spi: modaliases not of:
479 	 * modaliases for DT so module autoloading won't work if we
480 	 * don't have a spi_device_id as well as a compatible string.
481 	 */
482 	if (sdrv->driver.of_match_table) {
483 		const struct of_device_id *of_id;
484 
485 		for (of_id = sdrv->driver.of_match_table; of_id->compatible[0];
486 		     of_id++) {
487 			const char *of_name;
488 
489 			/* Strip off any vendor prefix */
490 			of_name = strnchr(of_id->compatible,
491 					  sizeof(of_id->compatible), ',');
492 			if (of_name)
493 				of_name++;
494 			else
495 				of_name = of_id->compatible;
496 
497 			if (sdrv->id_table) {
498 				const struct spi_device_id *spi_id;
499 
500 				spi_id = spi_match_id(sdrv->id_table, of_name);
501 				if (spi_id)
502 					continue;
503 			} else {
504 				if (strcmp(sdrv->driver.name, of_name) == 0)
505 					continue;
506 			}
507 
508 			pr_warn("SPI driver %s has no spi_device_id for %s\n",
509 				sdrv->driver.name, of_id->compatible);
510 		}
511 	}
512 
513 	return driver_register(&sdrv->driver);
514 }
515 EXPORT_SYMBOL_GPL(__spi_register_driver);
516 
517 /*-------------------------------------------------------------------------*/
518 
519 /*
520  * SPI devices should normally not be created by SPI device drivers; that
521  * would make them board-specific.  Similarly with SPI controller drivers.
522  * Device registration normally goes into like arch/.../mach.../board-YYY.c
523  * with other readonly (flashable) information about mainboard devices.
524  */
525 
526 struct boardinfo {
527 	struct list_head	list;
528 	struct spi_board_info	board_info;
529 };
530 
531 static LIST_HEAD(board_list);
532 static LIST_HEAD(spi_controller_list);
533 
534 /*
535  * Used to protect add/del operation for board_info list and
536  * spi_controller list, and their matching process also used
537  * to protect object of type struct idr.
538  */
539 static DEFINE_MUTEX(board_lock);
540 
541 /**
542  * spi_alloc_device - Allocate a new SPI device
543  * @ctlr: Controller to which device is connected
544  * Context: can sleep
545  *
546  * Allows a driver to allocate and initialize a spi_device without
547  * registering it immediately.  This allows a driver to directly
548  * fill the spi_device with device parameters before calling
549  * spi_add_device() on it.
550  *
551  * Caller is responsible to call spi_add_device() on the returned
552  * spi_device structure to add it to the SPI controller.  If the caller
553  * needs to discard the spi_device without adding it, then it should
554  * call spi_dev_put() on it.
555  *
556  * Return: a pointer to the new device, or NULL.
557  */
558 struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
559 {
560 	struct spi_device	*spi;
561 
562 	if (!spi_controller_get(ctlr))
563 		return NULL;
564 
565 	spi = kzalloc_obj(*spi);
566 	if (!spi) {
567 		spi_controller_put(ctlr);
568 		return NULL;
569 	}
570 
571 	spi->pcpu_statistics = spi_alloc_pcpu_stats();
572 	if (!spi->pcpu_statistics) {
573 		kfree(spi);
574 		spi_controller_put(ctlr);
575 		return NULL;
576 	}
577 
578 	spi->controller = ctlr;
579 	spi->dev.parent = &ctlr->dev;
580 	spi->dev.bus = &spi_bus_type;
581 	spi->dev.release = spidev_release;
582 	spi->mode = ctlr->buswidth_override_bits;
583 	spi->num_chipselect = 1;
584 
585 	device_initialize(&spi->dev);
586 	return spi;
587 }
588 EXPORT_SYMBOL_GPL(spi_alloc_device);
589 
590 static void spi_dev_set_name(struct spi_device *spi)
591 {
592 	struct device *dev = &spi->dev;
593 	struct fwnode_handle *fwnode = dev_fwnode(dev);
594 
595 	if (is_acpi_device_node(fwnode)) {
596 		dev_set_name(dev, "spi-%s", acpi_dev_name(to_acpi_device_node(fwnode)));
597 		return;
598 	}
599 
600 	if (is_software_node(fwnode)) {
601 		dev_set_name(dev, "spi-%pfwP", fwnode);
602 		return;
603 	}
604 
605 	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
606 		     spi_get_chipselect(spi, 0));
607 }
608 
609 /*
610  * Zero(0) is a valid physical CS value and can be located at any
611  * logical CS in the spi->chip_select[]. If all the physical CS
612  * are initialized to 0 then It would be difficult to differentiate
613  * between a valid physical CS 0 & an unused logical CS whose physical
614  * CS can be 0. As a solution to this issue initialize all the CS to -1.
615  * Now all the unused logical CS will have -1 physical CS value & can be
616  * ignored while performing physical CS validity checks.
617  */
618 #define SPI_INVALID_CS		((s8)-1)
619 
620 static inline int spi_dev_check_cs(struct device *dev,
621 				   struct spi_device *spi, u8 idx,
622 				   struct spi_device *new_spi, u8 new_idx)
623 {
624 	u8 cs, cs_new;
625 	u8 idx_new;
626 
627 	cs = spi_get_chipselect(spi, idx);
628 	for (idx_new = new_idx; idx_new < new_spi->num_chipselect; idx_new++) {
629 		cs_new = spi_get_chipselect(new_spi, idx_new);
630 		if (cs == cs_new) {
631 			dev_err(dev, "chipselect %u already in use\n", cs_new);
632 			return -EBUSY;
633 		}
634 	}
635 	return 0;
636 }
637 
638 struct spi_dev_check_info {
639 	struct spi_device *new_spi;
640 	struct spi_device *parent;	/* set for ancillary devices */
641 };
642 
643 static int spi_dev_check(struct device *dev, void *data)
644 {
645 	struct spi_device *spi = to_spi_device(dev);
646 	struct spi_dev_check_info *info = data;
647 	struct spi_device *new_spi = info->new_spi;
648 	int status, idx;
649 
650 	/*
651 	 * When registering an ancillary device, skip checking against the
652 	 * parent device since the ancillary is intentionally using one of
653 	 * the parent's chip selects.
654 	 */
655 	if (info->parent && spi == info->parent)
656 		return 0;
657 
658 	if (spi->controller == new_spi->controller) {
659 		for (idx = 0; idx < spi->num_chipselect; idx++) {
660 			status = spi_dev_check_cs(dev, spi, idx, new_spi, 0);
661 			if (status)
662 				return status;
663 		}
664 	}
665 	return 0;
666 }
667 
668 static void spi_cleanup(struct spi_device *spi)
669 {
670 	if (spi->controller->cleanup)
671 		spi->controller->cleanup(spi);
672 }
673 
674 static int __spi_add_device(struct spi_device *spi, struct spi_device *parent)
675 {
676 	struct spi_controller *ctlr = spi->controller;
677 	struct device *dev = ctlr->dev.parent;
678 	struct spi_dev_check_info check_info;
679 	int status, idx;
680 	u8 cs;
681 
682 	if (spi->num_chipselect > SPI_DEVICE_CS_CNT_MAX) {
683 		dev_err(dev, "num_cs %d > max %d\n", spi->num_chipselect,
684 			SPI_DEVICE_CS_CNT_MAX);
685 		return -EOVERFLOW;
686 	}
687 
688 	for (idx = 0; idx < spi->num_chipselect; idx++) {
689 		/* Chipselects are numbered 0..max; validate. */
690 		cs = spi_get_chipselect(spi, idx);
691 		if (cs >= ctlr->num_chipselect) {
692 			dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx),
693 				ctlr->num_chipselect);
694 			return -EINVAL;
695 		}
696 	}
697 
698 	/*
699 	 * Make sure that multiple logical CS doesn't map to the same physical CS.
700 	 * For example, spi->chip_select[0] != spi->chip_select[1] and so on.
701 	 */
702 	if (!spi_controller_is_target(ctlr)) {
703 		for (idx = 0; idx < spi->num_chipselect; idx++) {
704 			status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1);
705 			if (status)
706 				return status;
707 		}
708 	}
709 
710 	/* Initialize unused logical CS as invalid */
711 	for (idx = spi->num_chipselect; idx < SPI_DEVICE_CS_CNT_MAX; idx++)
712 		spi_set_chipselect(spi, idx, SPI_INVALID_CS);
713 
714 	/* Set the bus ID string */
715 	spi_dev_set_name(spi);
716 
717 	/*
718 	 * We need to make sure there's no other device with this
719 	 * chipselect **BEFORE** we call setup(), else we'll trash
720 	 * its configuration.
721 	 */
722 	check_info.new_spi = spi;
723 	check_info.parent = parent;
724 	status = bus_for_each_dev(&spi_bus_type, NULL, &check_info, spi_dev_check);
725 	if (status)
726 		return status;
727 
728 	/* Controller may unregister concurrently */
729 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC) &&
730 	    !device_is_registered(&ctlr->dev)) {
731 		return -ENODEV;
732 	}
733 
734 	if (ctlr->cs_gpiods) {
735 		for (idx = 0; idx < spi->num_chipselect; idx++) {
736 			cs = spi_get_chipselect(spi, idx);
737 			spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]);
738 		}
739 	}
740 
741 	/*
742 	 * Drivers may modify this initial i/o setup, but will
743 	 * normally rely on the device being setup.  Devices
744 	 * using SPI_CS_HIGH can't coexist well otherwise...
745 	 */
746 	status = __spi_setup(spi, true);
747 	if (status < 0) {
748 		dev_err(dev, "can't setup %s, status %d\n",
749 				dev_name(&spi->dev), status);
750 		return status;
751 	}
752 
753 	/* Device may be bound to an active driver when this returns */
754 	status = device_add(&spi->dev);
755 	if (status < 0) {
756 		dev_err(dev, "can't add %s, status %d\n",
757 				dev_name(&spi->dev), status);
758 		spi_cleanup(spi);
759 	} else {
760 		dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
761 	}
762 
763 	return status;
764 }
765 
766 /**
767  * spi_add_device - Add spi_device allocated with spi_alloc_device
768  * @spi: spi_device to register
769  *
770  * Companion function to spi_alloc_device.  Devices allocated with
771  * spi_alloc_device can be added onto the SPI bus with this function.
772  *
773  * Return: 0 on success; negative errno on failure
774  */
775 int spi_add_device(struct spi_device *spi)
776 {
777 	struct spi_controller *ctlr = spi->controller;
778 	int status;
779 
780 	/* Set the bus ID string */
781 	spi_dev_set_name(spi);
782 
783 	mutex_lock(&ctlr->add_lock);
784 	status = __spi_add_device(spi, NULL);
785 	mutex_unlock(&ctlr->add_lock);
786 	return status;
787 }
788 EXPORT_SYMBOL_GPL(spi_add_device);
789 
790 /**
791  * spi_new_device - instantiate one new SPI device
792  * @ctlr: Controller to which device is connected
793  * @chip: Describes the SPI device
794  * Context: can sleep
795  *
796  * On typical mainboards, this is purely internal; and it's not needed
797  * after board init creates the hard-wired devices.  Some development
798  * platforms may not be able to use spi_register_board_info though, and
799  * this is exported so that for example a USB or parport based adapter
800  * driver could add devices (which it would learn about out-of-band).
801  *
802  * Return: the new device, or NULL.
803  */
804 struct spi_device *spi_new_device(struct spi_controller *ctlr,
805 				  struct spi_board_info *chip)
806 {
807 	struct spi_device	*proxy;
808 	int			status;
809 
810 	/*
811 	 * NOTE:  caller did any chip->bus_num checks necessary.
812 	 *
813 	 * Also, unless we change the return value convention to use
814 	 * error-or-pointer (not NULL-or-pointer), troubleshootability
815 	 * suggests syslogged diagnostics are best here (ugh).
816 	 */
817 
818 	proxy = spi_alloc_device(ctlr);
819 	if (!proxy)
820 		return NULL;
821 
822 	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
823 
824 	/* Use provided chip-select for proxy device */
825 	spi_set_chipselect(proxy, 0, chip->chip_select);
826 
827 	proxy->max_speed_hz = chip->max_speed_hz;
828 	proxy->mode = chip->mode;
829 	proxy->irq = chip->irq;
830 	strscpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
831 	proxy->dev.platform_data = (void *) chip->platform_data;
832 	proxy->controller_data = chip->controller_data;
833 	proxy->controller_state = NULL;
834 	/*
835 	 * By default spi->chip_select[0] will hold the physical CS number,
836 	 * so set bit 0 in spi->cs_index_mask.
837 	 */
838 	proxy->cs_index_mask = BIT(0);
839 
840 	if (chip->swnode) {
841 		status = device_add_software_node(&proxy->dev, chip->swnode);
842 		if (status) {
843 			dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n",
844 				chip->modalias, status);
845 			goto err_dev_put;
846 		}
847 	}
848 
849 	status = spi_add_device(proxy);
850 	if (status < 0)
851 		goto err_dev_put;
852 
853 	return proxy;
854 
855 err_dev_put:
856 	device_remove_software_node(&proxy->dev);
857 	spi_dev_put(proxy);
858 	return NULL;
859 }
860 EXPORT_SYMBOL_GPL(spi_new_device);
861 
862 /**
863  * spi_unregister_device - unregister a single SPI device
864  * @spi: spi_device to unregister
865  *
866  * Start making the passed SPI device vanish. Normally this would be handled
867  * by spi_unregister_controller().
868  */
869 void spi_unregister_device(struct spi_device *spi)
870 {
871 	struct fwnode_handle *fwnode;
872 
873 	if (!spi)
874 		return;
875 
876 	fwnode = dev_fwnode(&spi->dev);
877 	if (is_of_node(fwnode)) {
878 		of_node_clear_flag(to_of_node(fwnode), OF_POPULATED);
879 		of_node_put(to_of_node(fwnode));
880 	} else if (is_acpi_device_node(fwnode)) {
881 		acpi_device_clear_enumerated(to_acpi_device_node(fwnode));
882 	}
883 	device_remove_software_node(&spi->dev);
884 	device_del(&spi->dev);
885 	spi_cleanup(spi);
886 	put_device(&spi->dev);
887 }
888 EXPORT_SYMBOL_GPL(spi_unregister_device);
889 
890 static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
891 					      struct spi_board_info *bi)
892 {
893 	struct spi_device *dev;
894 
895 	if (ctlr->bus_num != bi->bus_num)
896 		return;
897 
898 	dev = spi_new_device(ctlr, bi);
899 	if (!dev)
900 		dev_err(ctlr->dev.parent, "can't create new device for %s\n",
901 			bi->modalias);
902 }
903 
904 /**
905  * spi_register_board_info - register SPI devices for a given board
906  * @info: array of chip descriptors
907  * @n: how many descriptors are provided
908  * Context: can sleep
909  *
910  * Board-specific early init code calls this (probably during arch_initcall)
911  * with segments of the SPI device table.  Any device nodes are created later,
912  * after the relevant parent SPI controller (bus_num) is defined.  We keep
913  * this table of devices forever, so that reloading a controller driver will
914  * not make Linux forget about these hard-wired devices.
915  *
916  * Other code can also call this, e.g. a particular add-on board might provide
917  * SPI devices through its expansion connector, so code initializing that board
918  * would naturally declare its SPI devices.
919  *
920  * The board info passed can safely be __initdata ... but be careful of
921  * any embedded pointers (platform_data, etc), they're copied as-is.
922  *
923  * Return: zero on success, else a negative error code.
924  */
925 int spi_register_board_info(struct spi_board_info const *info, unsigned n)
926 {
927 	struct boardinfo *bi;
928 	int i;
929 
930 	if (!n)
931 		return 0;
932 
933 	bi = kzalloc_objs(*bi, n);
934 	if (!bi)
935 		return -ENOMEM;
936 
937 	for (i = 0; i < n; i++, bi++, info++) {
938 		struct spi_controller *ctlr;
939 
940 		memcpy(&bi->board_info, info, sizeof(*info));
941 
942 		mutex_lock(&board_lock);
943 		list_add_tail(&bi->list, &board_list);
944 		list_for_each_entry(ctlr, &spi_controller_list, list)
945 			spi_match_controller_to_boardinfo(ctlr,
946 							  &bi->board_info);
947 		mutex_unlock(&board_lock);
948 	}
949 
950 	return 0;
951 }
952 
953 /*-------------------------------------------------------------------------*/
954 
955 /* Core methods for SPI resource management */
956 
957 /**
958  * spi_res_alloc - allocate a spi resource that is life-cycle managed
959  *                 during the processing of a spi_message while using
960  *                 spi_transfer_one
961  * @spi:     the SPI device for which we allocate memory
962  * @release: the release code to execute for this resource
963  * @size:    size to alloc and return
964  * @gfp:     GFP allocation flags
965  *
966  * Return: the pointer to the allocated data
967  *
968  * This may get enhanced in the future to allocate from a memory pool
969  * of the @spi_device or @spi_controller to avoid repeated allocations.
970  */
971 static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release,
972 			   size_t size, gfp_t gfp)
973 {
974 	struct spi_res *sres;
975 
976 	sres = kzalloc(sizeof(*sres) + size, gfp);
977 	if (!sres)
978 		return NULL;
979 
980 	INIT_LIST_HEAD(&sres->entry);
981 	sres->release = release;
982 
983 	return sres->data;
984 }
985 
986 /**
987  * spi_res_free - free an SPI resource
988  * @res: pointer to the custom data of a resource
989  */
990 static void spi_res_free(void *res)
991 {
992 	struct spi_res *sres = container_of(res, struct spi_res, data);
993 
994 	WARN_ON(!list_empty(&sres->entry));
995 	kfree(sres);
996 }
997 
998 /**
999  * spi_res_add - add a spi_res to the spi_message
1000  * @message: the SPI message
1001  * @res:     the spi_resource
1002  */
1003 static void spi_res_add(struct spi_message *message, void *res)
1004 {
1005 	struct spi_res *sres = container_of(res, struct spi_res, data);
1006 
1007 	WARN_ON(!list_empty(&sres->entry));
1008 	list_add_tail(&sres->entry, &message->resources);
1009 }
1010 
1011 /**
1012  * spi_res_release - release all SPI resources for this message
1013  * @ctlr:  the @spi_controller
1014  * @message: the @spi_message
1015  */
1016 static void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
1017 {
1018 	struct spi_res *res, *tmp;
1019 
1020 	list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
1021 		if (res->release)
1022 			res->release(ctlr, message, res->data);
1023 
1024 		list_del(&res->entry);
1025 
1026 		kfree(res);
1027 	}
1028 }
1029 
1030 /*-------------------------------------------------------------------------*/
1031 #define spi_for_each_valid_cs(spi, idx)				\
1032 	for (idx = 0; idx < spi->num_chipselect; idx++)		\
1033 		if (!(spi->cs_index_mask & BIT(idx))) {} else
1034 
1035 static inline bool spi_is_last_cs(struct spi_device *spi)
1036 {
1037 	u8 idx;
1038 	bool last = false;
1039 
1040 	spi_for_each_valid_cs(spi, idx) {
1041 		if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx))
1042 			last = true;
1043 	}
1044 	return last;
1045 }
1046 
1047 static void spi_toggle_csgpiod(struct spi_device *spi, u8 idx, bool enable, bool activate)
1048 {
1049 	/*
1050 	 * Historically ACPI has no means of the GPIO polarity and
1051 	 * thus the SPISerialBus() resource defines it on the per-chip
1052 	 * basis. In order to avoid a chain of negations, the GPIO
1053 	 * polarity is considered being Active High. Even for the cases
1054 	 * when _DSD() is involved (in the updated versions of ACPI)
1055 	 * the GPIO CS polarity must be defined Active High to avoid
1056 	 * ambiguity. That's why we use enable, that takes SPI_CS_HIGH
1057 	 * into account.
1058 	 */
1059 	if (is_acpi_device_node(dev_fwnode(&spi->dev)))
1060 		gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), !enable);
1061 	else
1062 		/* Polarity handled by GPIO library */
1063 		gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), activate);
1064 
1065 	if (activate)
1066 		spi_delay_exec(&spi->cs_setup, NULL);
1067 	else
1068 		spi_delay_exec(&spi->cs_inactive, NULL);
1069 }
1070 
1071 static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
1072 {
1073 	bool activate = enable;
1074 	u8 idx;
1075 
1076 	/*
1077 	 * Avoid calling into the driver (or doing delays) if the chip select
1078 	 * isn't actually changing from the last time this was called.
1079 	 */
1080 	if (!force && (enable == spi_is_last_cs(spi)) &&
1081 	    (spi->controller->last_cs_index_mask == spi->cs_index_mask) &&
1082 	    (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
1083 		return;
1084 
1085 	trace_spi_set_cs(spi, activate);
1086 
1087 	spi->controller->last_cs_index_mask = spi->cs_index_mask;
1088 	for (idx = 0; idx < SPI_DEVICE_CS_CNT_MAX; idx++) {
1089 		if (enable && idx < spi->num_chipselect)
1090 			spi->controller->last_cs[idx] = spi_get_chipselect(spi, 0);
1091 		else
1092 			spi->controller->last_cs[idx] = SPI_INVALID_CS;
1093 	}
1094 
1095 	spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
1096 	if (spi->controller->last_cs_mode_high)
1097 		enable = !enable;
1098 
1099 	/*
1100 	 * Handle chip select delays for GPIO based CS or controllers without
1101 	 * programmable chip select timing.
1102 	 */
1103 	if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate)
1104 		spi_delay_exec(&spi->cs_hold, NULL);
1105 
1106 	if (spi_is_csgpiod(spi)) {
1107 		if (!(spi->mode & SPI_NO_CS)) {
1108 			spi_for_each_valid_cs(spi, idx) {
1109 				if (spi_get_csgpiod(spi, idx))
1110 					spi_toggle_csgpiod(spi, idx, enable, activate);
1111 			}
1112 		}
1113 		/* Some SPI controllers need both GPIO CS & ->set_cs() */
1114 		if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) &&
1115 		    spi->controller->set_cs)
1116 			spi->controller->set_cs(spi, !enable);
1117 	} else if (spi->controller->set_cs) {
1118 		spi->controller->set_cs(spi, !enable);
1119 	}
1120 
1121 	if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) {
1122 		if (activate)
1123 			spi_delay_exec(&spi->cs_setup, NULL);
1124 		else
1125 			spi_delay_exec(&spi->cs_inactive, NULL);
1126 	}
1127 }
1128 
1129 #ifdef CONFIG_HAS_DMA
1130 static int spi_map_buf_attrs(struct spi_controller *ctlr, struct device *dev,
1131 			     struct sg_table *sgt, void *buf, size_t len,
1132 			     enum dma_data_direction dir, unsigned long attrs)
1133 {
1134 	const bool vmalloced_buf = is_vmalloc_addr(buf);
1135 	unsigned int max_seg_size = dma_get_max_seg_size(dev);
1136 #ifdef CONFIG_HIGHMEM
1137 	const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
1138 				(unsigned long)buf < (PKMAP_BASE +
1139 					(LAST_PKMAP * PAGE_SIZE)));
1140 #else
1141 	const bool kmap_buf = false;
1142 #endif
1143 	int desc_len;
1144 	int sgs;
1145 	struct page *vm_page;
1146 	struct scatterlist *sg;
1147 	void *sg_buf;
1148 	size_t min;
1149 	int i, ret;
1150 
1151 	if (vmalloced_buf || kmap_buf) {
1152 		desc_len = min_t(unsigned long, max_seg_size, PAGE_SIZE);
1153 		sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
1154 	} else if (virt_addr_valid(buf)) {
1155 		desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len);
1156 		sgs = DIV_ROUND_UP(len, desc_len);
1157 	} else {
1158 		return -EINVAL;
1159 	}
1160 
1161 	ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
1162 	if (ret != 0)
1163 		return ret;
1164 
1165 	sg = &sgt->sgl[0];
1166 	for (i = 0; i < sgs; i++) {
1167 
1168 		if (vmalloced_buf || kmap_buf) {
1169 			/*
1170 			 * Next scatterlist entry size is the minimum between
1171 			 * the desc_len and the remaining buffer length that
1172 			 * fits in a page.
1173 			 */
1174 			min = min_t(size_t, desc_len,
1175 				    min_t(size_t, len,
1176 					  PAGE_SIZE - offset_in_page(buf)));
1177 			if (vmalloced_buf)
1178 				vm_page = vmalloc_to_page(buf);
1179 			else
1180 				vm_page = kmap_to_page(buf);
1181 			if (!vm_page) {
1182 				sg_free_table(sgt);
1183 				return -ENOMEM;
1184 			}
1185 			sg_set_page(sg, vm_page,
1186 				    min, offset_in_page(buf));
1187 		} else {
1188 			min = min_t(size_t, len, desc_len);
1189 			sg_buf = buf;
1190 			sg_set_buf(sg, sg_buf, min);
1191 		}
1192 
1193 		buf += min;
1194 		len -= min;
1195 		sg = sg_next(sg);
1196 	}
1197 
1198 	ret = dma_map_sgtable(dev, sgt, dir, attrs);
1199 	if (ret < 0) {
1200 		sg_free_table(sgt);
1201 		return ret;
1202 	}
1203 
1204 	return 0;
1205 }
1206 
1207 int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
1208 		struct sg_table *sgt, void *buf, size_t len,
1209 		enum dma_data_direction dir)
1210 {
1211 	return spi_map_buf_attrs(ctlr, dev, sgt, buf, len, dir, 0);
1212 }
1213 
1214 static void spi_unmap_buf_attrs(struct spi_controller *ctlr,
1215 				struct device *dev, struct sg_table *sgt,
1216 				enum dma_data_direction dir,
1217 				unsigned long attrs)
1218 {
1219 	dma_unmap_sgtable(dev, sgt, dir, attrs);
1220 	sg_free_table(sgt);
1221 	sgt->orig_nents = 0;
1222 	sgt->nents = 0;
1223 }
1224 
1225 void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
1226 		   struct sg_table *sgt, enum dma_data_direction dir)
1227 {
1228 	spi_unmap_buf_attrs(ctlr, dev, sgt, dir, 0);
1229 }
1230 
1231 static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
1232 {
1233 	struct device *tx_dev, *rx_dev;
1234 	struct spi_transfer *xfer;
1235 	int ret;
1236 
1237 	if (!ctlr->can_dma)
1238 		return 0;
1239 
1240 	if (ctlr->dma_tx)
1241 		tx_dev = ctlr->dma_tx->device->dev;
1242 	else if (ctlr->dma_map_dev)
1243 		tx_dev = ctlr->dma_map_dev;
1244 	else
1245 		tx_dev = ctlr->dev.parent;
1246 
1247 	if (ctlr->dma_rx)
1248 		rx_dev = ctlr->dma_rx->device->dev;
1249 	else if (ctlr->dma_map_dev)
1250 		rx_dev = ctlr->dma_map_dev;
1251 	else
1252 		rx_dev = ctlr->dev.parent;
1253 
1254 	ret = -ENOMSG;
1255 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1256 		/* The sync is done before each transfer. */
1257 		unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
1258 
1259 		if (!ctlr->can_dma(ctlr, msg->spi, xfer))
1260 			continue;
1261 
1262 		if (xfer->tx_buf != NULL) {
1263 			ret = spi_map_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
1264 						(void *)xfer->tx_buf,
1265 						xfer->len, DMA_TO_DEVICE,
1266 						attrs);
1267 			if (ret != 0)
1268 				return ret;
1269 
1270 			xfer->tx_sg_mapped = true;
1271 		}
1272 
1273 		if (xfer->rx_buf != NULL) {
1274 			ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
1275 						xfer->rx_buf, xfer->len,
1276 						DMA_FROM_DEVICE, attrs);
1277 			if (ret != 0) {
1278 				spi_unmap_buf_attrs(ctlr, tx_dev,
1279 						&xfer->tx_sg, DMA_TO_DEVICE,
1280 						attrs);
1281 
1282 				return ret;
1283 			}
1284 
1285 			xfer->rx_sg_mapped = true;
1286 		}
1287 	}
1288 	/* No transfer has been mapped, bail out with success */
1289 	if (ret)
1290 		return 0;
1291 
1292 	ctlr->cur_rx_dma_dev = rx_dev;
1293 	ctlr->cur_tx_dma_dev = tx_dev;
1294 
1295 	return 0;
1296 }
1297 
1298 static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
1299 {
1300 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
1301 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
1302 	struct spi_transfer *xfer;
1303 
1304 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1305 		/* The sync has already been done after each transfer. */
1306 		unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
1307 
1308 		if (xfer->rx_sg_mapped)
1309 			spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
1310 					    DMA_FROM_DEVICE, attrs);
1311 		xfer->rx_sg_mapped = false;
1312 
1313 		if (xfer->tx_sg_mapped)
1314 			spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
1315 					    DMA_TO_DEVICE, attrs);
1316 		xfer->tx_sg_mapped = false;
1317 	}
1318 
1319 	return 0;
1320 }
1321 
1322 static void spi_dma_sync_for_device(struct spi_controller *ctlr,
1323 				    struct spi_transfer *xfer)
1324 {
1325 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
1326 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
1327 
1328 	if (xfer->tx_sg_mapped)
1329 		dma_sync_sgtable_for_device(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
1330 	if (xfer->rx_sg_mapped)
1331 		dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1332 }
1333 
1334 static void spi_dma_sync_for_cpu(struct spi_controller *ctlr,
1335 				 struct spi_transfer *xfer)
1336 {
1337 	struct device *rx_dev = ctlr->cur_rx_dma_dev;
1338 	struct device *tx_dev = ctlr->cur_tx_dma_dev;
1339 
1340 	if (xfer->rx_sg_mapped)
1341 		dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1342 	if (xfer->tx_sg_mapped)
1343 		dma_sync_sgtable_for_cpu(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
1344 }
1345 #else /* !CONFIG_HAS_DMA */
1346 static inline int __spi_map_msg(struct spi_controller *ctlr,
1347 				struct spi_message *msg)
1348 {
1349 	return 0;
1350 }
1351 
1352 static inline int __spi_unmap_msg(struct spi_controller *ctlr,
1353 				  struct spi_message *msg)
1354 {
1355 	return 0;
1356 }
1357 
1358 static void spi_dma_sync_for_device(struct spi_controller *ctrl,
1359 				    struct spi_transfer *xfer)
1360 {
1361 }
1362 
1363 static void spi_dma_sync_for_cpu(struct spi_controller *ctrl,
1364 				 struct spi_transfer *xfer)
1365 {
1366 }
1367 #endif /* !CONFIG_HAS_DMA */
1368 
1369 static inline int spi_unmap_msg(struct spi_controller *ctlr,
1370 				struct spi_message *msg)
1371 {
1372 	struct spi_transfer *xfer;
1373 
1374 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1375 		/*
1376 		 * Restore the original value of tx_buf or rx_buf if they are
1377 		 * NULL.
1378 		 */
1379 		if (xfer->tx_buf == ctlr->dummy_tx)
1380 			xfer->tx_buf = NULL;
1381 		if (xfer->rx_buf == ctlr->dummy_rx)
1382 			xfer->rx_buf = NULL;
1383 	}
1384 
1385 	return __spi_unmap_msg(ctlr, msg);
1386 }
1387 
1388 static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
1389 {
1390 	struct spi_transfer *xfer;
1391 	void *tmp;
1392 	unsigned int max_tx, max_rx;
1393 
1394 	if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX))
1395 		&& !(msg->spi->mode & SPI_3WIRE)) {
1396 		max_tx = 0;
1397 		max_rx = 0;
1398 
1399 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1400 			if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
1401 			    !xfer->tx_buf)
1402 				max_tx = max(xfer->len, max_tx);
1403 			if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
1404 			    !xfer->rx_buf)
1405 				max_rx = max(xfer->len, max_rx);
1406 		}
1407 
1408 		if (max_tx) {
1409 			tmp = krealloc(ctlr->dummy_tx, max_tx,
1410 				       GFP_KERNEL | GFP_DMA | __GFP_ZERO);
1411 			if (!tmp)
1412 				return -ENOMEM;
1413 			ctlr->dummy_tx = tmp;
1414 		}
1415 
1416 		if (max_rx) {
1417 			tmp = krealloc(ctlr->dummy_rx, max_rx,
1418 				       GFP_KERNEL | GFP_DMA);
1419 			if (!tmp)
1420 				return -ENOMEM;
1421 			ctlr->dummy_rx = tmp;
1422 		}
1423 
1424 		if (max_tx || max_rx) {
1425 			list_for_each_entry(xfer, &msg->transfers,
1426 					    transfer_list) {
1427 				if (!xfer->len)
1428 					continue;
1429 				if (!xfer->tx_buf)
1430 					xfer->tx_buf = ctlr->dummy_tx;
1431 				if (!xfer->rx_buf)
1432 					xfer->rx_buf = ctlr->dummy_rx;
1433 			}
1434 		}
1435 	}
1436 
1437 	return __spi_map_msg(ctlr, msg);
1438 }
1439 
1440 static int spi_transfer_wait(struct spi_controller *ctlr,
1441 			     struct spi_message *msg,
1442 			     struct spi_transfer *xfer)
1443 {
1444 	struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1445 	struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
1446 	u32 speed_hz = xfer->speed_hz;
1447 	unsigned long long ms;
1448 
1449 	if (spi_controller_is_target(ctlr)) {
1450 		if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1451 			dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1452 			return -EINTR;
1453 		}
1454 	} else {
1455 		if (!speed_hz)
1456 			speed_hz = 100000;
1457 
1458 		/*
1459 		 * For each byte we wait for 8 cycles of the SPI clock.
1460 		 * Since speed is defined in Hz and we want milliseconds,
1461 		 * use respective multiplier, but before the division,
1462 		 * otherwise we may get 0 for short transfers.
1463 		 */
1464 		ms = 8LL * MSEC_PER_SEC * xfer->len;
1465 		do_div(ms, speed_hz);
1466 
1467 		/*
1468 		 * Increase it twice and add 200 ms tolerance, use
1469 		 * predefined maximum in case of overflow.
1470 		 */
1471 		ms += ms + 200;
1472 		if (ms > UINT_MAX)
1473 			ms = UINT_MAX;
1474 
1475 		ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1476 						 msecs_to_jiffies(ms));
1477 
1478 		if (ms == 0) {
1479 			SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1480 			SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1481 			dev_err(&msg->spi->dev,
1482 				"SPI transfer timed out\n");
1483 			return -ETIMEDOUT;
1484 		}
1485 
1486 		if (xfer->error & SPI_TRANS_FAIL_IO)
1487 			return -EIO;
1488 	}
1489 
1490 	return 0;
1491 }
1492 
1493 static void _spi_transfer_delay_ns(u32 ns)
1494 {
1495 	if (!ns)
1496 		return;
1497 	if (ns <= NSEC_PER_USEC) {
1498 		ndelay(ns);
1499 	} else {
1500 		u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
1501 
1502 		fsleep(us);
1503 	}
1504 }
1505 
1506 int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
1507 {
1508 	u32 delay = _delay->value;
1509 	u32 unit = _delay->unit;
1510 	u32 hz;
1511 
1512 	if (!delay)
1513 		return 0;
1514 
1515 	switch (unit) {
1516 	case SPI_DELAY_UNIT_USECS:
1517 		delay *= NSEC_PER_USEC;
1518 		break;
1519 	case SPI_DELAY_UNIT_NSECS:
1520 		/* Nothing to do here */
1521 		break;
1522 	case SPI_DELAY_UNIT_SCK:
1523 		/* Clock cycles need to be obtained from spi_transfer */
1524 		if (!xfer)
1525 			return -EINVAL;
1526 		/*
1527 		 * If there is unknown effective speed, approximate it
1528 		 * by underestimating with half of the requested Hz.
1529 		 */
1530 		hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1531 		if (!hz)
1532 			return -EINVAL;
1533 
1534 		/* Convert delay to nanoseconds */
1535 		delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz);
1536 		break;
1537 	default:
1538 		return -EINVAL;
1539 	}
1540 
1541 	return delay;
1542 }
1543 EXPORT_SYMBOL_GPL(spi_delay_to_ns);
1544 
1545 int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1546 {
1547 	int delay;
1548 
1549 	might_sleep();
1550 
1551 	if (!_delay)
1552 		return -EINVAL;
1553 
1554 	delay = spi_delay_to_ns(_delay, xfer);
1555 	if (delay < 0)
1556 		return delay;
1557 
1558 	_spi_transfer_delay_ns(delay);
1559 
1560 	return 0;
1561 }
1562 EXPORT_SYMBOL_GPL(spi_delay_exec);
1563 
1564 static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1565 					  struct spi_transfer *xfer)
1566 {
1567 	u32 default_delay_ns = 10 * NSEC_PER_USEC;
1568 	u32 delay = xfer->cs_change_delay.value;
1569 	u32 unit = xfer->cs_change_delay.unit;
1570 	int ret;
1571 
1572 	/* Return early on "fast" mode - for everything but USECS */
1573 	if (!delay) {
1574 		if (unit == SPI_DELAY_UNIT_USECS)
1575 			_spi_transfer_delay_ns(default_delay_ns);
1576 		return;
1577 	}
1578 
1579 	ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1580 	if (ret) {
1581 		dev_err_once(&msg->spi->dev,
1582 			     "Use of unsupported delay unit %i, using default of %luus\n",
1583 			     unit, default_delay_ns / NSEC_PER_USEC);
1584 		_spi_transfer_delay_ns(default_delay_ns);
1585 	}
1586 }
1587 
1588 void spi_transfer_cs_change_delay_exec(struct spi_message *msg,
1589 						  struct spi_transfer *xfer)
1590 {
1591 	_spi_transfer_cs_change_delay(msg, xfer);
1592 }
1593 EXPORT_SYMBOL_GPL(spi_transfer_cs_change_delay_exec);
1594 
1595 /*
1596  * spi_transfer_one_message - Default implementation of transfer_one_message()
1597  *
1598  * This is a standard implementation of transfer_one_message() for
1599  * drivers which implement a transfer_one() operation.  It provides
1600  * standard handling of delays and chip select management.
1601  */
1602 static int spi_transfer_one_message(struct spi_controller *ctlr,
1603 				    struct spi_message *msg)
1604 {
1605 	struct spi_transfer *xfer;
1606 	bool keep_cs = false;
1607 	int ret = 0;
1608 	struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1609 	struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
1610 
1611 	xfer = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list);
1612 	spi_set_cs(msg->spi, !xfer->cs_off, false);
1613 
1614 	SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1615 	SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1616 
1617 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1618 		trace_spi_transfer_start(msg, xfer);
1619 
1620 		spi_statistics_add_transfer_stats(statm, xfer, msg);
1621 		spi_statistics_add_transfer_stats(stats, xfer, msg);
1622 
1623 		if (!ctlr->ptp_sts_supported) {
1624 			xfer->ptp_sts_word_pre = 0;
1625 			ptp_read_system_prets(xfer->ptp_sts);
1626 		}
1627 
1628 		if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
1629 			reinit_completion(&ctlr->xfer_completion);
1630 
1631 fallback_pio:
1632 			spi_dma_sync_for_device(ctlr, xfer);
1633 			ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1634 			if (ret < 0) {
1635 				spi_dma_sync_for_cpu(ctlr, xfer);
1636 
1637 				if ((xfer->tx_sg_mapped || xfer->rx_sg_mapped) &&
1638 				    (xfer->error & SPI_TRANS_FAIL_NO_START)) {
1639 					__spi_unmap_msg(ctlr, msg);
1640 					ctlr->fallback = true;
1641 					xfer->error &= ~SPI_TRANS_FAIL_NO_START;
1642 					goto fallback_pio;
1643 				}
1644 
1645 				SPI_STATISTICS_INCREMENT_FIELD(statm,
1646 							       errors);
1647 				SPI_STATISTICS_INCREMENT_FIELD(stats,
1648 							       errors);
1649 				dev_err(&msg->spi->dev,
1650 					"SPI transfer failed: %d\n", ret);
1651 				goto out;
1652 			}
1653 
1654 			if (ret > 0) {
1655 				ret = spi_transfer_wait(ctlr, msg, xfer);
1656 				if (ret < 0)
1657 					msg->status = ret;
1658 			}
1659 
1660 			spi_dma_sync_for_cpu(ctlr, xfer);
1661 		} else {
1662 			if (xfer->len)
1663 				dev_err(&msg->spi->dev,
1664 					"Bufferless transfer has length %u\n",
1665 					xfer->len);
1666 		}
1667 
1668 		if (!ctlr->ptp_sts_supported) {
1669 			ptp_read_system_postts(xfer->ptp_sts);
1670 			xfer->ptp_sts_word_post = xfer->len;
1671 		}
1672 
1673 		trace_spi_transfer_stop(msg, xfer);
1674 
1675 		if (msg->status != -EINPROGRESS)
1676 			goto out;
1677 
1678 		spi_transfer_delay_exec(xfer);
1679 
1680 		if (xfer->cs_change) {
1681 			if (list_is_last(&xfer->transfer_list,
1682 					 &msg->transfers)) {
1683 				keep_cs = true;
1684 			} else {
1685 				if (!xfer->cs_off)
1686 					spi_set_cs(msg->spi, false, false);
1687 				_spi_transfer_cs_change_delay(msg, xfer);
1688 				if (!list_next_entry(xfer, transfer_list)->cs_off)
1689 					spi_set_cs(msg->spi, true, false);
1690 			}
1691 		} else if (!list_is_last(&xfer->transfer_list, &msg->transfers) &&
1692 			   xfer->cs_off != list_next_entry(xfer, transfer_list)->cs_off) {
1693 			spi_set_cs(msg->spi, xfer->cs_off, false);
1694 		}
1695 
1696 		msg->actual_length += xfer->len;
1697 	}
1698 
1699 out:
1700 	if (ret != 0 || !keep_cs)
1701 		spi_set_cs(msg->spi, false, false);
1702 
1703 	if (msg->status == -EINPROGRESS)
1704 		msg->status = ret;
1705 
1706 	if (msg->status && ctlr->handle_err)
1707 		ctlr->handle_err(ctlr, msg);
1708 
1709 	spi_finalize_current_message(ctlr);
1710 
1711 	return ret;
1712 }
1713 
1714 /**
1715  * spi_finalize_current_transfer - report completion of a transfer
1716  * @ctlr: the controller reporting completion
1717  *
1718  * Called by SPI drivers using the core transfer_one_message()
1719  * implementation to notify it that the current interrupt driven
1720  * transfer has finished and the next one may be scheduled.
1721  */
1722 void spi_finalize_current_transfer(struct spi_controller *ctlr)
1723 {
1724 	complete(&ctlr->xfer_completion);
1725 }
1726 EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1727 
1728 static void spi_idle_runtime_pm(struct spi_controller *ctlr)
1729 {
1730 	if (ctlr->auto_runtime_pm) {
1731 		pm_runtime_put_autosuspend(ctlr->dev.parent);
1732 	}
1733 }
1734 
1735 static int __spi_pump_transfer_message(struct spi_controller *ctlr,
1736 		struct spi_message *msg, bool was_busy)
1737 {
1738 	struct spi_transfer *xfer;
1739 	int ret;
1740 
1741 	if (!was_busy && ctlr->auto_runtime_pm) {
1742 		ret = pm_runtime_get_sync(ctlr->dev.parent);
1743 		if (ret < 0) {
1744 			pm_runtime_put_noidle(ctlr->dev.parent);
1745 			dev_err(&ctlr->dev, "Failed to power device: %d\n",
1746 				ret);
1747 
1748 			msg->status = ret;
1749 			spi_finalize_current_message(ctlr);
1750 
1751 			return ret;
1752 		}
1753 	}
1754 
1755 	if (!was_busy)
1756 		trace_spi_controller_busy(ctlr);
1757 
1758 	if (!was_busy && ctlr->prepare_transfer_hardware) {
1759 		ret = ctlr->prepare_transfer_hardware(ctlr);
1760 		if (ret) {
1761 			dev_err(&ctlr->dev,
1762 				"failed to prepare transfer hardware: %d\n",
1763 				ret);
1764 
1765 			if (ctlr->auto_runtime_pm)
1766 				pm_runtime_put(ctlr->dev.parent);
1767 
1768 			msg->status = ret;
1769 			spi_finalize_current_message(ctlr);
1770 
1771 			return ret;
1772 		}
1773 	}
1774 
1775 	trace_spi_message_start(msg);
1776 
1777 	if (ctlr->prepare_message) {
1778 		ret = ctlr->prepare_message(ctlr, msg);
1779 		if (ret) {
1780 			dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1781 				ret);
1782 			msg->status = ret;
1783 			spi_finalize_current_message(ctlr);
1784 			return ret;
1785 		}
1786 		msg->prepared = true;
1787 	}
1788 
1789 	ret = spi_map_msg(ctlr, msg);
1790 	if (ret) {
1791 		msg->status = ret;
1792 		spi_finalize_current_message(ctlr);
1793 		return ret;
1794 	}
1795 
1796 	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1797 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1798 			xfer->ptp_sts_word_pre = 0;
1799 			ptp_read_system_prets(xfer->ptp_sts);
1800 		}
1801 	}
1802 
1803 	/*
1804 	 * Drivers implementation of transfer_one_message() must arrange for
1805 	 * spi_finalize_current_message() to get called. Most drivers will do
1806 	 * this in the calling context, but some don't. For those cases, a
1807 	 * completion is used to guarantee that this function does not return
1808 	 * until spi_finalize_current_message() is done accessing
1809 	 * ctlr->cur_msg.
1810 	 * Use of the following two flags enable to opportunistically skip the
1811 	 * use of the completion since its use involves expensive spin locks.
1812 	 * In case of a race with the context that calls
1813 	 * spi_finalize_current_message() the completion will always be used,
1814 	 * due to strict ordering of these flags using barriers.
1815 	 */
1816 	WRITE_ONCE(ctlr->cur_msg_incomplete, true);
1817 	WRITE_ONCE(ctlr->cur_msg_need_completion, false);
1818 	reinit_completion(&ctlr->cur_msg_completion);
1819 	smp_wmb(); /* Make these available to spi_finalize_current_message() */
1820 
1821 	ret = ctlr->transfer_one_message(ctlr, msg);
1822 	if (ret) {
1823 		dev_err(&ctlr->dev,
1824 			"failed to transfer one message from queue\n");
1825 		return ret;
1826 	}
1827 
1828 	WRITE_ONCE(ctlr->cur_msg_need_completion, true);
1829 	smp_mb(); /* See spi_finalize_current_message()... */
1830 	if (READ_ONCE(ctlr->cur_msg_incomplete))
1831 		wait_for_completion(&ctlr->cur_msg_completion);
1832 
1833 	return 0;
1834 }
1835 
1836 /**
1837  * __spi_pump_messages - function which processes SPI message queue
1838  * @ctlr: controller to process queue for
1839  * @in_kthread: true if we are in the context of the message pump thread
1840  *
1841  * This function checks if there is any SPI message in the queue that
1842  * needs processing and if so call out to the driver to initialize hardware
1843  * and transfer each message.
1844  *
1845  * Note that it is called both from the kthread itself and also from
1846  * inside spi_sync(); the queue extraction handling at the top of the
1847  * function should deal with this safely.
1848  */
1849 static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
1850 {
1851 	struct spi_message *msg;
1852 	bool was_busy = false;
1853 	unsigned long flags;
1854 	int ret;
1855 
1856 	/* Take the I/O mutex */
1857 	mutex_lock(&ctlr->io_mutex);
1858 
1859 	/* Lock queue */
1860 	spin_lock_irqsave(&ctlr->queue_lock, flags);
1861 
1862 	/* Make sure we are not already running a message */
1863 	if (ctlr->cur_msg)
1864 		goto out_unlock;
1865 
1866 	/* Check if the queue is idle */
1867 	if (list_empty(&ctlr->queue) || !ctlr->running) {
1868 		if (!ctlr->busy)
1869 			goto out_unlock;
1870 
1871 		/* Defer any non-atomic teardown to the thread */
1872 		if (!in_kthread) {
1873 			if (!ctlr->dummy_rx && !ctlr->dummy_tx &&
1874 			    !ctlr->unprepare_transfer_hardware) {
1875 				spi_idle_runtime_pm(ctlr);
1876 				ctlr->busy = false;
1877 				ctlr->queue_empty = true;
1878 				trace_spi_controller_idle(ctlr);
1879 			} else {
1880 				kthread_queue_work(ctlr->kworker,
1881 						   &ctlr->pump_messages);
1882 			}
1883 			goto out_unlock;
1884 		}
1885 
1886 		ctlr->busy = false;
1887 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1888 
1889 		kfree(ctlr->dummy_rx);
1890 		ctlr->dummy_rx = NULL;
1891 		kfree(ctlr->dummy_tx);
1892 		ctlr->dummy_tx = NULL;
1893 		if (ctlr->unprepare_transfer_hardware &&
1894 		    ctlr->unprepare_transfer_hardware(ctlr))
1895 			dev_err(&ctlr->dev,
1896 				"failed to unprepare transfer hardware\n");
1897 		spi_idle_runtime_pm(ctlr);
1898 		trace_spi_controller_idle(ctlr);
1899 
1900 		spin_lock_irqsave(&ctlr->queue_lock, flags);
1901 		ctlr->queue_empty = true;
1902 		goto out_unlock;
1903 	}
1904 
1905 	/* Extract head of queue */
1906 	msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1907 	ctlr->cur_msg = msg;
1908 
1909 	list_del_init(&msg->queue);
1910 	if (ctlr->busy)
1911 		was_busy = true;
1912 	else
1913 		ctlr->busy = true;
1914 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1915 
1916 	ret = __spi_pump_transfer_message(ctlr, msg, was_busy);
1917 	kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
1918 
1919 	ctlr->cur_msg = NULL;
1920 	ctlr->fallback = false;
1921 
1922 	mutex_unlock(&ctlr->io_mutex);
1923 
1924 	/* Prod the scheduler in case transfer_one() was busy waiting */
1925 	if (!ret)
1926 		cond_resched();
1927 	return;
1928 
1929 out_unlock:
1930 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1931 	mutex_unlock(&ctlr->io_mutex);
1932 }
1933 
1934 /**
1935  * spi_pump_messages - kthread work function which processes spi message queue
1936  * @work: pointer to kthread work struct contained in the controller struct
1937  */
1938 static void spi_pump_messages(struct kthread_work *work)
1939 {
1940 	struct spi_controller *ctlr =
1941 		container_of(work, struct spi_controller, pump_messages);
1942 
1943 	__spi_pump_messages(ctlr, true);
1944 }
1945 
1946 /**
1947  * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp
1948  * @ctlr: Pointer to the spi_controller structure of the driver
1949  * @xfer: Pointer to the transfer being timestamped
1950  * @progress: How many words (not bytes) have been transferred so far
1951  * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1952  *	      transfer, for less jitter in time measurement. Only compatible
1953  *	      with PIO drivers. If true, must follow up with
1954  *	      spi_take_timestamp_post or otherwise system will crash.
1955  *	      WARNING: for fully predictable results, the CPU frequency must
1956  *	      also be under control (governor).
1957  *
1958  * This is a helper for drivers to collect the beginning of the TX timestamp
1959  * for the requested byte from the SPI transfer. The frequency with which this
1960  * function must be called (once per word, once for the whole transfer, once
1961  * per batch of words etc) is arbitrary as long as the @tx buffer offset is
1962  * greater than or equal to the requested byte at the time of the call. The
1963  * timestamp is only taken once, at the first such call. It is assumed that
1964  * the driver advances its @tx buffer pointer monotonically.
1965  */
1966 void spi_take_timestamp_pre(struct spi_controller *ctlr,
1967 			    struct spi_transfer *xfer,
1968 			    size_t progress, bool irqs_off)
1969 {
1970 	if (!xfer->ptp_sts)
1971 		return;
1972 
1973 	if (xfer->timestamped)
1974 		return;
1975 
1976 	if (progress > xfer->ptp_sts_word_pre)
1977 		return;
1978 
1979 	/* Capture the resolution of the timestamp */
1980 	xfer->ptp_sts_word_pre = progress;
1981 
1982 	if (irqs_off) {
1983 		local_irq_save(ctlr->irq_flags);
1984 		preempt_disable();
1985 	}
1986 
1987 	ptp_read_system_prets(xfer->ptp_sts);
1988 }
1989 EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1990 
1991 /**
1992  * spi_take_timestamp_post - helper to collect the end of the TX timestamp
1993  * @ctlr: Pointer to the spi_controller structure of the driver
1994  * @xfer: Pointer to the transfer being timestamped
1995  * @progress: How many words (not bytes) have been transferred so far
1996  * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1997  *
1998  * This is a helper for drivers to collect the end of the TX timestamp for
1999  * the requested byte from the SPI transfer. Can be called with an arbitrary
2000  * frequency: only the first call where @tx exceeds or is equal to the
2001  * requested word will be timestamped.
2002  */
2003 void spi_take_timestamp_post(struct spi_controller *ctlr,
2004 			     struct spi_transfer *xfer,
2005 			     size_t progress, bool irqs_off)
2006 {
2007 	if (!xfer->ptp_sts)
2008 		return;
2009 
2010 	if (xfer->timestamped)
2011 		return;
2012 
2013 	if (progress < xfer->ptp_sts_word_post)
2014 		return;
2015 
2016 	ptp_read_system_postts(xfer->ptp_sts);
2017 
2018 	if (irqs_off) {
2019 		local_irq_restore(ctlr->irq_flags);
2020 		preempt_enable();
2021 	}
2022 
2023 	/* Capture the resolution of the timestamp */
2024 	xfer->ptp_sts_word_post = progress;
2025 
2026 	xfer->timestamped = 1;
2027 }
2028 EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
2029 
2030 /**
2031  * spi_set_thread_rt - set the controller to pump at realtime priority
2032  * @ctlr: controller to boost priority of
2033  *
2034  * This can be called because the controller requested realtime priority
2035  * (by setting the ->rt value before calling spi_register_controller()) or
2036  * because a device on the bus said that its transfers needed realtime
2037  * priority.
2038  *
2039  * NOTE: at the moment if any device on a bus says it needs realtime then
2040  * the thread will be at realtime priority for all transfers on that
2041  * controller.  If this eventually becomes a problem we may see if we can
2042  * find a way to boost the priority only temporarily during relevant
2043  * transfers.
2044  */
2045 static void spi_set_thread_rt(struct spi_controller *ctlr)
2046 {
2047 	dev_info(&ctlr->dev,
2048 		"will run message pump with realtime priority\n");
2049 	sched_set_fifo(ctlr->kworker->task);
2050 }
2051 
2052 static int spi_init_queue(struct spi_controller *ctlr)
2053 {
2054 	ctlr->running = false;
2055 	ctlr->busy = false;
2056 	ctlr->queue_empty = true;
2057 
2058 	ctlr->kworker = kthread_run_worker(0, dev_name(&ctlr->dev));
2059 	if (IS_ERR(ctlr->kworker)) {
2060 		dev_err(&ctlr->dev, "failed to create message pump kworker\n");
2061 		return PTR_ERR(ctlr->kworker);
2062 	}
2063 
2064 	kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
2065 
2066 	/*
2067 	 * Controller config will indicate if this controller should run the
2068 	 * message pump with high (realtime) priority to reduce the transfer
2069 	 * latency on the bus by minimising the delay between a transfer
2070 	 * request and the scheduling of the message pump thread. Without this
2071 	 * setting the message pump thread will remain at default priority.
2072 	 */
2073 	if (ctlr->rt)
2074 		spi_set_thread_rt(ctlr);
2075 
2076 	return 0;
2077 }
2078 
2079 /**
2080  * spi_get_next_queued_message() - called by driver to check for queued
2081  * messages
2082  * @ctlr: the controller to check for queued messages
2083  *
2084  * If there are more messages in the queue, the next message is returned from
2085  * this call.
2086  *
2087  * Return: the next message in the queue, else NULL if the queue is empty.
2088  */
2089 struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
2090 {
2091 	struct spi_message *next;
2092 	unsigned long flags;
2093 
2094 	/* Get a pointer to the next message, if any */
2095 	spin_lock_irqsave(&ctlr->queue_lock, flags);
2096 	next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
2097 					queue);
2098 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2099 
2100 	return next;
2101 }
2102 EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
2103 
2104 /*
2105  * __spi_unoptimize_message - shared implementation of spi_unoptimize_message()
2106  *                            and spi_maybe_unoptimize_message()
2107  * @msg: the message to unoptimize
2108  *
2109  * Peripheral drivers should use spi_unoptimize_message() and callers inside
2110  * core should use spi_maybe_unoptimize_message() rather than calling this
2111  * function directly.
2112  *
2113  * It is not valid to call this on a message that is not currently optimized.
2114  */
2115 static void __spi_unoptimize_message(struct spi_message *msg)
2116 {
2117 	struct spi_controller *ctlr = msg->spi->controller;
2118 
2119 	if (ctlr->unoptimize_message)
2120 		ctlr->unoptimize_message(msg);
2121 
2122 	spi_res_release(ctlr, msg);
2123 
2124 	msg->optimized = false;
2125 	msg->opt_state = NULL;
2126 }
2127 
2128 /*
2129  * spi_maybe_unoptimize_message - unoptimize msg not managed by a peripheral
2130  * @msg: the message to unoptimize
2131  *
2132  * This function is used to unoptimize a message if and only if it was
2133  * optimized by the core (via spi_maybe_optimize_message()).
2134  */
2135 static void spi_maybe_unoptimize_message(struct spi_message *msg)
2136 {
2137 	if (!msg->pre_optimized && msg->optimized &&
2138 	    !msg->spi->controller->defer_optimize_message)
2139 		__spi_unoptimize_message(msg);
2140 }
2141 
2142 /**
2143  * spi_finalize_current_message() - the current message is complete
2144  * @ctlr: the controller to return the message to
2145  *
2146  * Called by the driver to notify the core that the message in the front of the
2147  * queue is complete and can be removed from the queue.
2148  */
2149 void spi_finalize_current_message(struct spi_controller *ctlr)
2150 {
2151 	struct spi_transfer *xfer;
2152 	struct spi_message *mesg;
2153 	int ret;
2154 
2155 	mesg = ctlr->cur_msg;
2156 
2157 	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
2158 		list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
2159 			ptp_read_system_postts(xfer->ptp_sts);
2160 			xfer->ptp_sts_word_post = xfer->len;
2161 		}
2162 	}
2163 
2164 	if (unlikely(ctlr->ptp_sts_supported))
2165 		list_for_each_entry(xfer, &mesg->transfers, transfer_list)
2166 			WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped);
2167 
2168 	spi_unmap_msg(ctlr, mesg);
2169 
2170 	if (mesg->prepared && ctlr->unprepare_message) {
2171 		ret = ctlr->unprepare_message(ctlr, mesg);
2172 		if (ret) {
2173 			dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
2174 				ret);
2175 		}
2176 	}
2177 
2178 	mesg->prepared = false;
2179 
2180 	spi_maybe_unoptimize_message(mesg);
2181 
2182 	WRITE_ONCE(ctlr->cur_msg_incomplete, false);
2183 	smp_mb(); /* See __spi_pump_transfer_message()... */
2184 	if (READ_ONCE(ctlr->cur_msg_need_completion))
2185 		complete(&ctlr->cur_msg_completion);
2186 
2187 	trace_spi_message_done(mesg);
2188 
2189 	mesg->state = NULL;
2190 	if (mesg->complete)
2191 		mesg->complete(mesg->context);
2192 }
2193 EXPORT_SYMBOL_GPL(spi_finalize_current_message);
2194 
2195 static int spi_start_queue(struct spi_controller *ctlr)
2196 {
2197 	unsigned long flags;
2198 
2199 	spin_lock_irqsave(&ctlr->queue_lock, flags);
2200 
2201 	if (ctlr->running || ctlr->busy) {
2202 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2203 		return -EBUSY;
2204 	}
2205 
2206 	ctlr->running = true;
2207 	ctlr->cur_msg = NULL;
2208 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2209 
2210 	kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2211 
2212 	return 0;
2213 }
2214 
2215 static int spi_stop_queue(struct spi_controller *ctlr)
2216 {
2217 	unsigned int limit = 500;
2218 	unsigned long flags;
2219 
2220 	/*
2221 	 * This is a bit lame, but is optimized for the common execution path.
2222 	 * A wait_queue on the ctlr->busy could be used, but then the common
2223 	 * execution path (pump_messages) would be required to call wake_up or
2224 	 * friends on every SPI message. Do this instead.
2225 	 */
2226 	do {
2227 		spin_lock_irqsave(&ctlr->queue_lock, flags);
2228 		if (list_empty(&ctlr->queue) && !ctlr->busy) {
2229 			ctlr->running = false;
2230 			spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2231 			return 0;
2232 		}
2233 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2234 		usleep_range(10000, 11000);
2235 	} while (--limit);
2236 
2237 	return -EBUSY;
2238 }
2239 
2240 static int spi_destroy_queue(struct spi_controller *ctlr)
2241 {
2242 	int ret;
2243 
2244 	ret = spi_stop_queue(ctlr);
2245 
2246 	/*
2247 	 * kthread_flush_worker will block until all work is done.
2248 	 * If the reason that stop_queue timed out is that the work will never
2249 	 * finish, then it does no good to call flush/stop thread, so
2250 	 * return anyway.
2251 	 */
2252 	if (ret) {
2253 		dev_err(&ctlr->dev, "problem destroying queue\n");
2254 		return ret;
2255 	}
2256 
2257 	kthread_destroy_worker(ctlr->kworker);
2258 
2259 	return 0;
2260 }
2261 
2262 static int __spi_queued_transfer(struct spi_device *spi,
2263 				 struct spi_message *msg,
2264 				 bool need_pump)
2265 {
2266 	struct spi_controller *ctlr = spi->controller;
2267 	unsigned long flags;
2268 
2269 	spin_lock_irqsave(&ctlr->queue_lock, flags);
2270 
2271 	if (!ctlr->running) {
2272 		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2273 		return -ESHUTDOWN;
2274 	}
2275 	msg->actual_length = 0;
2276 	msg->status = -EINPROGRESS;
2277 
2278 	list_add_tail(&msg->queue, &ctlr->queue);
2279 	ctlr->queue_empty = false;
2280 	if (!ctlr->busy && need_pump)
2281 		kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2282 
2283 	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2284 	return 0;
2285 }
2286 
2287 /**
2288  * spi_queued_transfer - transfer function for queued transfers
2289  * @spi: SPI device which is requesting transfer
2290  * @msg: SPI message which is to handled is queued to driver queue
2291  *
2292  * Return: zero on success, else a negative error code.
2293  */
2294 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
2295 {
2296 	return __spi_queued_transfer(spi, msg, true);
2297 }
2298 
2299 static int spi_controller_initialize_queue(struct spi_controller *ctlr)
2300 {
2301 	int ret;
2302 
2303 	ctlr->transfer = spi_queued_transfer;
2304 	if (!ctlr->transfer_one_message)
2305 		ctlr->transfer_one_message = spi_transfer_one_message;
2306 
2307 	/* Initialize and start queue */
2308 	ret = spi_init_queue(ctlr);
2309 	if (ret) {
2310 		dev_err(&ctlr->dev, "problem initializing queue\n");
2311 		goto err_init_queue;
2312 	}
2313 	ctlr->queued = true;
2314 	ret = spi_start_queue(ctlr);
2315 	if (ret) {
2316 		dev_err(&ctlr->dev, "problem starting queue\n");
2317 		goto err_start_queue;
2318 	}
2319 
2320 	return 0;
2321 
2322 err_start_queue:
2323 	spi_destroy_queue(ctlr);
2324 err_init_queue:
2325 	return ret;
2326 }
2327 
2328 /**
2329  * spi_flush_queue - Send all pending messages in the queue from the callers'
2330  *		     context
2331  * @ctlr: controller to process queue for
2332  *
2333  * This should be used when one wants to ensure all pending messages have been
2334  * sent before doing something. Is used by the spi-mem code to make sure SPI
2335  * memory operations do not preempt regular SPI transfers that have been queued
2336  * before the spi-mem operation.
2337  */
2338 void spi_flush_queue(struct spi_controller *ctlr)
2339 {
2340 	if (ctlr->transfer == spi_queued_transfer)
2341 		__spi_pump_messages(ctlr, false);
2342 }
2343 
2344 /*-------------------------------------------------------------------------*/
2345 
2346 #if defined(CONFIG_OF)
2347 static void of_spi_parse_dt_cs_delay(struct device_node *nc,
2348 				     struct spi_delay *delay, const char *prop)
2349 {
2350 	u32 value;
2351 
2352 	if (!of_property_read_u32(nc, prop, &value)) {
2353 		if (value > U16_MAX) {
2354 			delay->value = DIV_ROUND_UP(value, 1000);
2355 			delay->unit = SPI_DELAY_UNIT_USECS;
2356 		} else {
2357 			delay->value = value;
2358 			delay->unit = SPI_DELAY_UNIT_NSECS;
2359 		}
2360 	}
2361 }
2362 
2363 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
2364 			   struct device_node *nc)
2365 {
2366 	u32 value, cs[SPI_DEVICE_CS_CNT_MAX], map[SPI_DEVICE_DATA_LANE_CNT_MAX];
2367 	int rc, idx, max_num_data_lanes;
2368 
2369 	/* Mode (clock phase/polarity/etc.) */
2370 	if (of_property_read_bool(nc, "spi-cpha"))
2371 		spi->mode |= SPI_CPHA;
2372 	if (of_property_read_bool(nc, "spi-cpol"))
2373 		spi->mode |= SPI_CPOL;
2374 	if (of_property_read_bool(nc, "spi-3wire"))
2375 		spi->mode |= SPI_3WIRE;
2376 	if (of_property_read_bool(nc, "spi-lsb-first"))
2377 		spi->mode |= SPI_LSB_FIRST;
2378 	if (of_property_read_bool(nc, "spi-cs-high"))
2379 		spi->mode |= SPI_CS_HIGH;
2380 
2381 	/* Device DUAL/QUAD mode */
2382 
2383 	rc = of_property_read_variable_u32_array(nc, "spi-tx-lane-map", map, 1,
2384 						 ARRAY_SIZE(map));
2385 	if (rc >= 0) {
2386 		max_num_data_lanes = rc;
2387 		for (idx = 0; idx < max_num_data_lanes; idx++)
2388 			spi->tx_lane_map[idx] = map[idx];
2389 	} else if (rc == -EINVAL) {
2390 		/* Default lane map is identity mapping. */
2391 		max_num_data_lanes = ARRAY_SIZE(spi->tx_lane_map);
2392 		for (idx = 0; idx < max_num_data_lanes; idx++)
2393 			spi->tx_lane_map[idx] = idx;
2394 	} else {
2395 		dev_err(&ctlr->dev,
2396 			"failed to read spi-tx-lane-map property: %d\n", rc);
2397 		return rc;
2398 	}
2399 
2400 	rc = of_property_count_u32_elems(nc, "spi-tx-bus-width");
2401 	if (rc < 0 && rc != -EINVAL) {
2402 		dev_err(&ctlr->dev,
2403 			"failed to read spi-tx-bus-width property: %d\n", rc);
2404 		return rc;
2405 	}
2406 	if (rc > max_num_data_lanes) {
2407 		dev_err(&ctlr->dev,
2408 			"spi-tx-bus-width has more elements (%d) than spi-tx-lane-map (%d)\n",
2409 			rc, max_num_data_lanes);
2410 		return -EINVAL;
2411 	}
2412 
2413 	if (rc == -EINVAL) {
2414 		/* Default when property is not present. */
2415 		spi->num_tx_lanes = 1;
2416 	} else {
2417 		u32 first_value;
2418 
2419 		spi->num_tx_lanes = rc;
2420 
2421 		for (idx = 0; idx < spi->num_tx_lanes; idx++) {
2422 			rc = of_property_read_u32_index(nc, "spi-tx-bus-width",
2423 							idx, &value);
2424 			if (rc)
2425 				return rc;
2426 
2427 			/*
2428 			 * For now, we only support all lanes having the same
2429 			 * width so we can keep using the existing mode flags.
2430 			 */
2431 			if (!idx)
2432 				first_value = value;
2433 			else if (first_value != value) {
2434 				dev_err(&ctlr->dev,
2435 					"spi-tx-bus-width has inconsistent values: first %d vs later %d\n",
2436 					first_value, value);
2437 				return -EINVAL;
2438 			}
2439 		}
2440 
2441 		switch (value) {
2442 		case 0:
2443 			spi->mode |= SPI_NO_TX;
2444 			break;
2445 		case 1:
2446 			break;
2447 		case 2:
2448 			spi->mode |= SPI_TX_DUAL;
2449 			break;
2450 		case 4:
2451 			spi->mode |= SPI_TX_QUAD;
2452 			break;
2453 		case 8:
2454 			spi->mode |= SPI_TX_OCTAL;
2455 			break;
2456 		default:
2457 			dev_warn(&ctlr->dev,
2458 				"spi-tx-bus-width %d not supported\n",
2459 				value);
2460 			break;
2461 		}
2462 	}
2463 
2464 	for (idx = 0; idx < spi->num_tx_lanes; idx++) {
2465 		if (spi->tx_lane_map[idx] >= spi->controller->num_data_lanes) {
2466 			dev_err(&ctlr->dev,
2467 				"spi-tx-lane-map has invalid value %d (num_data_lanes=%d)\n",
2468 				spi->tx_lane_map[idx],
2469 				spi->controller->num_data_lanes);
2470 			return -EINVAL;
2471 		}
2472 	}
2473 
2474 	rc = of_property_read_variable_u32_array(nc, "spi-rx-lane-map", map, 1,
2475 						 ARRAY_SIZE(map));
2476 	if (rc >= 0) {
2477 		max_num_data_lanes = rc;
2478 		for (idx = 0; idx < max_num_data_lanes; idx++)
2479 			spi->rx_lane_map[idx] = map[idx];
2480 	} else if (rc == -EINVAL) {
2481 		/* Default lane map is identity mapping. */
2482 		max_num_data_lanes = ARRAY_SIZE(spi->rx_lane_map);
2483 		for (idx = 0; idx < max_num_data_lanes; idx++)
2484 			spi->rx_lane_map[idx] = idx;
2485 	} else {
2486 		dev_err(&ctlr->dev,
2487 			"failed to read spi-rx-lane-map property: %d\n", rc);
2488 		return rc;
2489 	}
2490 
2491 	rc = of_property_count_u32_elems(nc, "spi-rx-bus-width");
2492 	if (rc < 0 && rc != -EINVAL) {
2493 		dev_err(&ctlr->dev,
2494 			"failed to read spi-rx-bus-width property: %d\n", rc);
2495 		return rc;
2496 	}
2497 	if (rc > max_num_data_lanes) {
2498 		dev_err(&ctlr->dev,
2499 			"spi-rx-bus-width has more elements (%d) than spi-rx-lane-map (%d)\n",
2500 			rc, max_num_data_lanes);
2501 		return -EINVAL;
2502 	}
2503 
2504 	if (rc == -EINVAL) {
2505 		/* Default when property is not present. */
2506 		spi->num_rx_lanes = 1;
2507 	} else {
2508 		u32 first_value;
2509 
2510 		spi->num_rx_lanes = rc;
2511 
2512 		for (idx = 0; idx < spi->num_rx_lanes; idx++) {
2513 			rc = of_property_read_u32_index(nc, "spi-rx-bus-width",
2514 							idx, &value);
2515 			if (rc)
2516 				return rc;
2517 
2518 			/*
2519 			 * For now, we only support all lanes having the same
2520 			 * width so we can keep using the existing mode flags.
2521 			 */
2522 			if (!idx)
2523 				first_value = value;
2524 			else if (first_value != value) {
2525 				dev_err(&ctlr->dev,
2526 					"spi-rx-bus-width has inconsistent values: first %d vs later %d\n",
2527 					first_value, value);
2528 				return -EINVAL;
2529 			}
2530 		}
2531 
2532 		switch (value) {
2533 		case 0:
2534 			spi->mode |= SPI_NO_RX;
2535 			break;
2536 		case 1:
2537 			break;
2538 		case 2:
2539 			spi->mode |= SPI_RX_DUAL;
2540 			break;
2541 		case 4:
2542 			spi->mode |= SPI_RX_QUAD;
2543 			break;
2544 		case 8:
2545 			spi->mode |= SPI_RX_OCTAL;
2546 			break;
2547 		default:
2548 			dev_warn(&ctlr->dev,
2549 				"spi-rx-bus-width %d not supported\n",
2550 				value);
2551 			break;
2552 		}
2553 	}
2554 
2555 	for (idx = 0; idx < spi->num_rx_lanes; idx++) {
2556 		if (spi->rx_lane_map[idx] >= spi->controller->num_data_lanes) {
2557 			dev_err(&ctlr->dev,
2558 				"spi-rx-lane-map has invalid value %d (num_data_lanes=%d)\n",
2559 				spi->rx_lane_map[idx],
2560 				spi->controller->num_data_lanes);
2561 			return -EINVAL;
2562 		}
2563 	}
2564 
2565 	if (spi_controller_is_target(ctlr)) {
2566 		if (!of_node_name_eq(nc, "slave")) {
2567 			dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
2568 				nc);
2569 			return -EINVAL;
2570 		}
2571 		return 0;
2572 	}
2573 
2574 	/* Device address */
2575 	rc = of_property_read_variable_u32_array(nc, "reg", &cs[0], 1,
2576 						 SPI_DEVICE_CS_CNT_MAX);
2577 	if (rc < 0) {
2578 		dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
2579 			nc, rc);
2580 		return rc;
2581 	}
2582 
2583 	if ((of_property_present(nc, "parallel-memories")) &&
2584 	    (!(ctlr->flags & SPI_CONTROLLER_MULTI_CS))) {
2585 		dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n");
2586 		return -EINVAL;
2587 	}
2588 
2589 	spi->num_chipselect = rc;
2590 	for (idx = 0; idx < rc; idx++)
2591 		spi_set_chipselect(spi, idx, cs[idx]);
2592 
2593 	/*
2594 	 * By default spi->chip_select[0] will hold the physical CS number,
2595 	 * so set bit 0 in spi->cs_index_mask.
2596 	 */
2597 	spi->cs_index_mask = BIT(0);
2598 
2599 	/* Device speed */
2600 	if (!of_property_read_u32(nc, "spi-max-frequency", &value))
2601 		spi->max_speed_hz = value;
2602 
2603 	/* Device CS delays */
2604 	of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns");
2605 	of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns");
2606 	of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns");
2607 
2608 	return 0;
2609 }
2610 
2611 static struct spi_device *
2612 of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
2613 {
2614 	struct spi_device *spi;
2615 	int rc;
2616 
2617 	/* Alloc an spi_device */
2618 	spi = spi_alloc_device(ctlr);
2619 	if (!spi) {
2620 		dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
2621 		rc = -ENOMEM;
2622 		goto err_out;
2623 	}
2624 
2625 	/* Select device driver */
2626 	rc = of_alias_from_compatible(nc, spi->modalias,
2627 				      sizeof(spi->modalias));
2628 	if (rc < 0) {
2629 		dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
2630 		goto err_out;
2631 	}
2632 
2633 	rc = of_spi_parse_dt(ctlr, spi, nc);
2634 	if (rc)
2635 		goto err_out;
2636 
2637 	/* Store a pointer to the node in the device structure */
2638 	of_node_get(nc);
2639 
2640 	device_set_node(&spi->dev, of_fwnode_handle(nc));
2641 
2642 	/* Register the new device */
2643 	rc = spi_add_device(spi);
2644 	if (rc) {
2645 		dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
2646 		goto err_of_node_put;
2647 	}
2648 
2649 	return spi;
2650 
2651 err_of_node_put:
2652 	of_node_put(nc);
2653 err_out:
2654 	spi_dev_put(spi);
2655 	return ERR_PTR(rc);
2656 }
2657 
2658 /**
2659  * of_register_spi_devices() - Register child devices onto the SPI bus
2660  * @ctlr:	Pointer to spi_controller device
2661  *
2662  * Registers an spi_device for each child node of controller node which
2663  * represents a valid SPI target device.
2664  */
2665 static void of_register_spi_devices(struct spi_controller *ctlr)
2666 {
2667 	struct spi_device *spi;
2668 	struct device_node *nc;
2669 
2670 	for_each_available_child_of_node(ctlr->dev.of_node, nc) {
2671 		if (of_node_test_and_set_flag(nc, OF_POPULATED))
2672 			continue;
2673 		spi = of_register_spi_device(ctlr, nc);
2674 		if (IS_ERR(spi)) {
2675 			dev_warn(&ctlr->dev,
2676 				 "Failed to create SPI device for %pOF\n", nc);
2677 			of_node_clear_flag(nc, OF_POPULATED);
2678 		}
2679 	}
2680 }
2681 #else
2682 static void of_register_spi_devices(struct spi_controller *ctlr) { }
2683 #endif
2684 
2685 /**
2686  * spi_new_ancillary_device() - Register ancillary SPI device
2687  * @spi:         Pointer to the main SPI device registering the ancillary device
2688  * @chip_select: Chip Select of the ancillary device
2689  *
2690  * Register an ancillary SPI device; for example some chips have a chip-select
2691  * for normal device usage and another one for setup/firmware upload.
2692  *
2693  * This may only be called from main SPI device's probe routine.
2694  *
2695  * Return: 0 on success; negative errno on failure
2696  */
2697 struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
2698 					     u8 chip_select)
2699 {
2700 	struct spi_controller *ctlr = spi->controller;
2701 	struct spi_device *ancillary;
2702 	int rc;
2703 
2704 	/* Alloc an spi_device */
2705 	ancillary = spi_alloc_device(ctlr);
2706 	if (!ancillary) {
2707 		rc = -ENOMEM;
2708 		goto err_out;
2709 	}
2710 
2711 	strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias));
2712 
2713 	/* Use provided chip-select for ancillary device */
2714 	spi_set_chipselect(ancillary, 0, chip_select);
2715 
2716 	/* Take over SPI mode/speed from SPI main device */
2717 	ancillary->max_speed_hz = spi->max_speed_hz;
2718 	ancillary->mode = spi->mode;
2719 	/*
2720 	 * By default spi->chip_select[0] will hold the physical CS number,
2721 	 * so set bit 0 in spi->cs_index_mask.
2722 	 */
2723 	ancillary->cs_index_mask = BIT(0);
2724 
2725 	WARN_ON(!mutex_is_locked(&ctlr->add_lock));
2726 
2727 	/* Register the new device, passing the parent to skip CS conflict check */
2728 	rc = __spi_add_device(ancillary, spi);
2729 	if (rc) {
2730 		dev_err(&spi->dev, "failed to register ancillary device\n");
2731 		goto err_out;
2732 	}
2733 
2734 	return ancillary;
2735 
2736 err_out:
2737 	spi_dev_put(ancillary);
2738 	return ERR_PTR(rc);
2739 }
2740 EXPORT_SYMBOL_GPL(spi_new_ancillary_device);
2741 
2742 static void devm_spi_unregister_device(void *spi)
2743 {
2744 	spi_unregister_device(spi);
2745 }
2746 
2747 /**
2748  * devm_spi_new_ancillary_device() - Register managed ancillary SPI device
2749  * @spi:         Pointer to the main SPI device registering the ancillary device
2750  * @chip_select: Chip Select of the ancillary device
2751  *
2752  * Register an ancillary SPI device; for example some chips have a chip-select
2753  * for normal device usage and another one for setup/firmware upload.
2754  *
2755  * This is the managed version of spi_new_ancillary_device(). The ancillary
2756  * device will be unregistered automatically when the parent SPI device is
2757  * unregistered.
2758  *
2759  * This may only be called from main SPI device's probe routine.
2760  *
2761  * Return: Pointer to new ancillary device on success; ERR_PTR on failure
2762  */
2763 struct spi_device *devm_spi_new_ancillary_device(struct spi_device *spi,
2764 						 u8 chip_select)
2765 {
2766 	struct spi_device *ancillary;
2767 	int ret;
2768 
2769 	ancillary = spi_new_ancillary_device(spi, chip_select);
2770 	if (IS_ERR(ancillary))
2771 		return ancillary;
2772 
2773 	ret = devm_add_action_or_reset(&spi->dev, devm_spi_unregister_device,
2774 				       ancillary);
2775 	if (ret)
2776 		return ERR_PTR(ret);
2777 
2778 	return ancillary;
2779 }
2780 EXPORT_SYMBOL_GPL(devm_spi_new_ancillary_device);
2781 
2782 #ifdef CONFIG_ACPI
2783 struct acpi_spi_lookup {
2784 	struct spi_controller 	*ctlr;
2785 	u32			max_speed_hz;
2786 	u32			mode;
2787 	int			irq;
2788 	u8			bits_per_word;
2789 	u8			chip_select;
2790 	int			n;
2791 	int			index;
2792 };
2793 
2794 static int acpi_spi_count(struct acpi_resource *ares, void *data)
2795 {
2796 	struct acpi_resource_spi_serialbus *sb;
2797 	int *count = data;
2798 
2799 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2800 		return 1;
2801 
2802 	sb = &ares->data.spi_serial_bus;
2803 	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI)
2804 		return 1;
2805 
2806 	*count = *count + 1;
2807 
2808 	return 1;
2809 }
2810 
2811 /**
2812  * acpi_spi_count_resources - Count the number of SpiSerialBus resources
2813  * @adev:	ACPI device
2814  *
2815  * Return: the number of SpiSerialBus resources in the ACPI-device's
2816  * resource-list; or a negative error code.
2817  */
2818 int acpi_spi_count_resources(struct acpi_device *adev)
2819 {
2820 	LIST_HEAD(r);
2821 	int count = 0;
2822 	int ret;
2823 
2824 	ret = acpi_dev_get_resources(adev, &r, acpi_spi_count, &count);
2825 	if (ret < 0)
2826 		return ret;
2827 
2828 	acpi_dev_free_resource_list(&r);
2829 
2830 	return count;
2831 }
2832 EXPORT_SYMBOL_GPL(acpi_spi_count_resources);
2833 
2834 static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
2835 					    struct acpi_spi_lookup *lookup)
2836 {
2837 	const union acpi_object *obj;
2838 
2839 	if (!x86_apple_machine)
2840 		return;
2841 
2842 	if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
2843 	    && obj->buffer.length >= 4)
2844 		lookup->max_speed_hz  = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
2845 
2846 	if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
2847 	    && obj->buffer.length == 8)
2848 		lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
2849 
2850 	if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
2851 	    && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
2852 		lookup->mode |= SPI_LSB_FIRST;
2853 
2854 	if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
2855 	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
2856 		lookup->mode |= SPI_CPOL;
2857 
2858 	if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
2859 	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
2860 		lookup->mode |= SPI_CPHA;
2861 }
2862 
2863 static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
2864 {
2865 	struct acpi_spi_lookup *lookup = data;
2866 	struct spi_controller *ctlr = lookup->ctlr;
2867 
2868 	if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
2869 		struct acpi_resource_spi_serialbus *sb;
2870 		acpi_handle parent_handle;
2871 		acpi_status status;
2872 
2873 		sb = &ares->data.spi_serial_bus;
2874 		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
2875 
2876 			if (lookup->index != -1 && lookup->n++ != lookup->index)
2877 				return 1;
2878 
2879 			status = acpi_get_handle(NULL,
2880 						 sb->resource_source.string_ptr,
2881 						 &parent_handle);
2882 
2883 			if (ACPI_FAILURE(status))
2884 				return -ENODEV;
2885 
2886 			if (ctlr) {
2887 				if (!device_match_acpi_handle(ctlr->dev.parent, parent_handle))
2888 					return -ENODEV;
2889 			} else {
2890 				struct acpi_device *adev;
2891 
2892 				adev = acpi_fetch_acpi_dev(parent_handle);
2893 				if (!adev)
2894 					return -ENODEV;
2895 
2896 				ctlr = acpi_spi_find_controller_by_adev(adev);
2897 				if (!ctlr)
2898 					return -EPROBE_DEFER;
2899 
2900 				lookup->ctlr = ctlr;
2901 			}
2902 
2903 			/*
2904 			 * ACPI DeviceSelection numbering is handled by the
2905 			 * host controller driver in Windows and can vary
2906 			 * from driver to driver. In Linux we always expect
2907 			 * 0 .. max - 1 so we need to ask the driver to
2908 			 * translate between the two schemes.
2909 			 */
2910 			if (ctlr->fw_translate_cs) {
2911 				int cs = ctlr->fw_translate_cs(ctlr,
2912 						sb->device_selection);
2913 				if (cs < 0)
2914 					return cs;
2915 				lookup->chip_select = cs;
2916 			} else {
2917 				lookup->chip_select = sb->device_selection;
2918 			}
2919 
2920 			lookup->max_speed_hz = sb->connection_speed;
2921 			lookup->bits_per_word = sb->data_bit_length;
2922 
2923 			if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
2924 				lookup->mode |= SPI_CPHA;
2925 			if (sb->clock_polarity == ACPI_SPI_START_HIGH)
2926 				lookup->mode |= SPI_CPOL;
2927 			if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
2928 				lookup->mode |= SPI_CS_HIGH;
2929 		}
2930 	} else if (lookup->irq < 0) {
2931 		struct resource r;
2932 
2933 		if (acpi_dev_resource_interrupt(ares, 0, &r))
2934 			lookup->irq = r.start;
2935 	}
2936 
2937 	/* Always tell the ACPI core to skip this resource */
2938 	return 1;
2939 }
2940 
2941 /**
2942  * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2943  * @ctlr: controller to which the spi device belongs
2944  * @adev: ACPI Device for the spi device
2945  * @index: Index of the spi resource inside the ACPI Node
2946  *
2947  * This should be used to allocate a new SPI device from and ACPI Device node.
2948  * The caller is responsible for calling spi_add_device to register the SPI device.
2949  *
2950  * If ctlr is set to NULL, the Controller for the SPI device will be looked up
2951  * using the resource.
2952  * If index is set to -1, index is not used.
2953  * Note: If index is -1, ctlr must be set.
2954  *
2955  * Return: a pointer to the new device, or ERR_PTR on error.
2956  */
2957 struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
2958 					 struct acpi_device *adev,
2959 					 int index)
2960 {
2961 	acpi_handle parent_handle = NULL;
2962 	struct list_head resource_list;
2963 	struct acpi_spi_lookup lookup = {};
2964 	struct spi_device *spi;
2965 	int ret;
2966 
2967 	if (!ctlr && index == -1)
2968 		return ERR_PTR(-EINVAL);
2969 
2970 	lookup.ctlr		= ctlr;
2971 	lookup.irq		= -1;
2972 	lookup.index		= index;
2973 	lookup.n		= 0;
2974 
2975 	INIT_LIST_HEAD(&resource_list);
2976 	ret = acpi_dev_get_resources(adev, &resource_list,
2977 				     acpi_spi_add_resource, &lookup);
2978 	acpi_dev_free_resource_list(&resource_list);
2979 
2980 	if (ret < 0)
2981 		/* Found SPI in _CRS but it points to another controller */
2982 		return ERR_PTR(ret);
2983 
2984 	if (!lookup.max_speed_hz &&
2985 	    ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) &&
2986 	    device_match_acpi_handle(lookup.ctlr->dev.parent, parent_handle)) {
2987 		/* Apple does not use _CRS but nested devices for SPI target devices */
2988 		acpi_spi_parse_apple_properties(adev, &lookup);
2989 	}
2990 
2991 	if (!lookup.max_speed_hz)
2992 		return ERR_PTR(-ENODEV);
2993 
2994 	spi = spi_alloc_device(lookup.ctlr);
2995 	if (!spi) {
2996 		dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n",
2997 			dev_name(&adev->dev));
2998 		return ERR_PTR(-ENOMEM);
2999 	}
3000 
3001 	spi_set_chipselect(spi, 0, lookup.chip_select);
3002 
3003 	ACPI_COMPANION_SET(&spi->dev, adev);
3004 	spi->max_speed_hz	= lookup.max_speed_hz;
3005 	spi->mode		|= lookup.mode;
3006 	spi->irq		= lookup.irq;
3007 	spi->bits_per_word	= lookup.bits_per_word;
3008 	/*
3009 	 * By default spi->chip_select[0] will hold the physical CS number,
3010 	 * so set bit 0 in spi->cs_index_mask.
3011 	 */
3012 	spi->cs_index_mask	= BIT(0);
3013 
3014 	return spi;
3015 }
3016 EXPORT_SYMBOL_GPL(acpi_spi_device_alloc);
3017 
3018 static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
3019 					    struct acpi_device *adev)
3020 {
3021 	struct spi_device *spi;
3022 
3023 	if (acpi_bus_get_status(adev) || !adev->status.present ||
3024 	    acpi_device_enumerated(adev))
3025 		return AE_OK;
3026 
3027 	spi = acpi_spi_device_alloc(ctlr, adev, -1);
3028 	if (IS_ERR(spi)) {
3029 		if (PTR_ERR(spi) == -ENOMEM)
3030 			return AE_NO_MEMORY;
3031 		else
3032 			return AE_OK;
3033 	}
3034 
3035 	acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
3036 			  sizeof(spi->modalias));
3037 
3038 	/*
3039 	 * This gets re-tried in spi_probe() for -EPROBE_DEFER handling in case
3040 	 * the GPIO controller does not have a driver yet. This needs to be done
3041 	 * here too, because this call sets the GPIO direction and/or bias.
3042 	 * Setting these needs to be done even if there is no driver, in which
3043 	 * case spi_probe() will never get called.
3044 	 * TODO: ideally the setup of the GPIO should be handled in a generic
3045 	 * manner in the ACPI/gpiolib core code.
3046 	 */
3047 	if (spi->irq < 0)
3048 		spi->irq = acpi_dev_gpio_irq_get(adev, 0);
3049 
3050 	acpi_device_set_enumerated(adev);
3051 
3052 	adev->power.flags.ignore_parent = true;
3053 	if (spi_add_device(spi)) {
3054 		adev->power.flags.ignore_parent = false;
3055 		dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
3056 			dev_name(&adev->dev));
3057 		spi_dev_put(spi);
3058 	}
3059 
3060 	return AE_OK;
3061 }
3062 
3063 static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
3064 				       void *data, void **return_value)
3065 {
3066 	struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
3067 	struct spi_controller *ctlr = data;
3068 
3069 	if (!adev)
3070 		return AE_OK;
3071 
3072 	return acpi_register_spi_device(ctlr, adev);
3073 }
3074 
3075 #define SPI_ACPI_ENUMERATE_MAX_DEPTH		32
3076 
3077 static void acpi_register_spi_devices(struct spi_controller *ctlr)
3078 {
3079 	acpi_status status;
3080 	acpi_handle handle;
3081 
3082 	handle = ACPI_HANDLE(ctlr->dev.parent);
3083 	if (!handle)
3084 		return;
3085 
3086 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
3087 				     SPI_ACPI_ENUMERATE_MAX_DEPTH,
3088 				     acpi_spi_add_device, NULL, ctlr, NULL);
3089 	if (ACPI_FAILURE(status))
3090 		dev_warn(&ctlr->dev, "failed to enumerate SPI target devices\n");
3091 }
3092 #else
3093 static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
3094 #endif /* CONFIG_ACPI */
3095 
3096 static void spi_controller_release(struct device *dev)
3097 {
3098 	struct spi_controller *ctlr;
3099 
3100 	ctlr = container_of(dev, struct spi_controller, dev);
3101 
3102 	free_percpu(ctlr->pcpu_statistics);
3103 	kfree(ctlr);
3104 }
3105 
3106 static const struct class spi_controller_class = {
3107 	.name		= "spi_master",
3108 	.dev_release	= spi_controller_release,
3109 	.dev_groups	= spi_controller_groups,
3110 };
3111 
3112 #ifdef CONFIG_SPI_SLAVE
3113 /**
3114  * spi_target_abort - abort the ongoing transfer request on an SPI target controller
3115  * @spi: device used for the current transfer
3116  */
3117 int spi_target_abort(struct spi_device *spi)
3118 {
3119 	struct spi_controller *ctlr = spi->controller;
3120 
3121 	if (spi_controller_is_target(ctlr) && ctlr->target_abort)
3122 		return ctlr->target_abort(ctlr);
3123 
3124 	return -ENOTSUPP;
3125 }
3126 EXPORT_SYMBOL_GPL(spi_target_abort);
3127 
3128 static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
3129 			  char *buf)
3130 {
3131 	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
3132 						   dev);
3133 	struct device *child;
3134 	int ret;
3135 
3136 	child = device_find_any_child(&ctlr->dev);
3137 	ret = sysfs_emit(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL);
3138 	put_device(child);
3139 
3140 	return ret;
3141 }
3142 
3143 static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
3144 			   const char *buf, size_t count)
3145 {
3146 	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
3147 						   dev);
3148 	struct spi_device *spi;
3149 	struct device *child;
3150 	char name[32];
3151 	int rc;
3152 
3153 	rc = sscanf(buf, "%31s", name);
3154 	if (rc != 1 || !name[0])
3155 		return -EINVAL;
3156 
3157 	child = device_find_any_child(&ctlr->dev);
3158 	if (child) {
3159 		/* Remove registered target device */
3160 		device_unregister(child);
3161 		put_device(child);
3162 	}
3163 
3164 	if (strcmp(name, "(null)")) {
3165 		/* Register new target device */
3166 		spi = spi_alloc_device(ctlr);
3167 		if (!spi)
3168 			return -ENOMEM;
3169 
3170 		strscpy(spi->modalias, name, sizeof(spi->modalias));
3171 
3172 		rc = spi_add_device(spi);
3173 		if (rc) {
3174 			spi_dev_put(spi);
3175 			return rc;
3176 		}
3177 	}
3178 
3179 	return count;
3180 }
3181 
3182 static DEVICE_ATTR_RW(slave);
3183 
3184 static struct attribute *spi_target_attrs[] = {
3185 	&dev_attr_slave.attr,
3186 	NULL,
3187 };
3188 
3189 static const struct attribute_group spi_target_group = {
3190 	.attrs = spi_target_attrs,
3191 };
3192 
3193 static const struct attribute_group *spi_target_groups[] = {
3194 	&spi_controller_statistics_group,
3195 	&spi_target_group,
3196 	NULL,
3197 };
3198 
3199 static const struct class spi_target_class = {
3200 	.name		= "spi_slave",
3201 	.dev_release	= spi_controller_release,
3202 	.dev_groups	= spi_target_groups,
3203 };
3204 #else
3205 extern struct class spi_target_class;	/* dummy */
3206 #endif
3207 
3208 /**
3209  * __spi_alloc_controller - allocate an SPI host or target controller
3210  * @dev: the controller, possibly using the platform_bus
3211  * @size: how much zeroed driver-private data to allocate; the pointer to this
3212  *	memory is in the driver_data field of the returned device, accessible
3213  *	with spi_controller_get_devdata(); the memory is cacheline aligned;
3214  *	drivers granting DMA access to portions of their private data need to
3215  *	round up @size using ALIGN(size, dma_get_cache_alignment()).
3216  * @target: flag indicating whether to allocate an SPI host (false) or SPI target (true)
3217  *	controller
3218  * Context: can sleep
3219  *
3220  * This call is used only by SPI controller drivers, which are the
3221  * only ones directly touching chip registers.  It's how they allocate
3222  * an spi_controller structure, prior to calling spi_register_controller().
3223  *
3224  * This must be called from context that can sleep.
3225  *
3226  * The caller is responsible for assigning the bus number and initializing the
3227  * controller's methods before calling spi_register_controller(); and calling
3228  * spi_controller_put() to prevent a memory leak when done with the
3229  * controller.
3230  *
3231  * Return: the SPI controller structure on success, else NULL.
3232  */
3233 struct spi_controller *__spi_alloc_controller(struct device *dev,
3234 					      unsigned int size, bool target)
3235 {
3236 	struct spi_controller	*ctlr;
3237 	size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
3238 
3239 	if (!dev)
3240 		return NULL;
3241 
3242 	ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
3243 	if (!ctlr)
3244 		return NULL;
3245 
3246 	ctlr->pcpu_statistics = spi_alloc_pcpu_stats();
3247 	if (!ctlr->pcpu_statistics) {
3248 		kfree(ctlr);
3249 		return NULL;
3250 	}
3251 
3252 	device_initialize(&ctlr->dev);
3253 	INIT_LIST_HEAD(&ctlr->queue);
3254 	spin_lock_init(&ctlr->queue_lock);
3255 	spin_lock_init(&ctlr->bus_lock_spinlock);
3256 	mutex_init(&ctlr->bus_lock_mutex);
3257 	mutex_init(&ctlr->io_mutex);
3258 	mutex_init(&ctlr->add_lock);
3259 	ctlr->bus_num = -1;
3260 	ctlr->num_chipselect = 1;
3261 	ctlr->num_data_lanes = 1;
3262 	ctlr->target = target;
3263 	if (IS_ENABLED(CONFIG_SPI_SLAVE) && target)
3264 		ctlr->dev.class = &spi_target_class;
3265 	else
3266 		ctlr->dev.class = &spi_controller_class;
3267 	ctlr->dev.parent = dev;
3268 
3269 	device_set_node(&ctlr->dev, dev_fwnode(dev));
3270 
3271 	pm_suspend_ignore_children(&ctlr->dev, true);
3272 	spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
3273 
3274 	return ctlr;
3275 }
3276 EXPORT_SYMBOL_GPL(__spi_alloc_controller);
3277 
3278 static void devm_spi_release_controller(void *ctlr)
3279 {
3280 	spi_controller_put(ctlr);
3281 }
3282 
3283 /**
3284  * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
3285  * @dev: physical device of SPI controller
3286  * @size: how much zeroed driver-private data to allocate
3287  * @target: whether to allocate an SPI host (false) or SPI target (true) controller
3288  * Context: can sleep
3289  *
3290  * Allocate an SPI controller and automatically release a reference on it
3291  * when @dev is unbound from its driver.  Drivers are thus relieved from
3292  * having to call spi_controller_put().
3293  *
3294  * The arguments to this function are identical to __spi_alloc_controller().
3295  *
3296  * Return: the SPI controller structure on success, else NULL.
3297  */
3298 struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
3299 						   unsigned int size,
3300 						   bool target)
3301 {
3302 	struct spi_controller *ctlr;
3303 	int ret;
3304 
3305 	ctlr = __spi_alloc_controller(dev, size, target);
3306 	if (!ctlr)
3307 		return NULL;
3308 
3309 	ret = devm_add_action_or_reset(dev, devm_spi_release_controller, ctlr);
3310 	if (ret)
3311 		return NULL;
3312 
3313 	return ctlr;
3314 }
3315 EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
3316 
3317 /**
3318  * spi_get_gpio_descs() - grab chip select GPIOs for the controller
3319  * @ctlr: The SPI controller to grab GPIO descriptors for
3320  */
3321 static int spi_get_gpio_descs(struct spi_controller *ctlr)
3322 {
3323 	int nb, i;
3324 	struct gpio_desc **cs;
3325 	struct device *dev = &ctlr->dev;
3326 	unsigned long native_cs_mask = 0;
3327 	unsigned int num_cs_gpios = 0;
3328 
3329 	nb = gpiod_count(dev, "cs");
3330 	if (nb < 0) {
3331 		/* No GPIOs at all is fine, else return the error */
3332 		if (nb == -ENOENT)
3333 			return 0;
3334 		return nb;
3335 	}
3336 
3337 	ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
3338 
3339 	cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
3340 			  GFP_KERNEL);
3341 	if (!cs)
3342 		return -ENOMEM;
3343 	ctlr->cs_gpiods = cs;
3344 
3345 	for (i = 0; i < nb; i++) {
3346 		/*
3347 		 * Most chipselects are active low, the inverted
3348 		 * semantics are handled by special quirks in gpiolib,
3349 		 * so initializing them GPIOD_OUT_LOW here means
3350 		 * "unasserted", in most cases this will drive the physical
3351 		 * line high.
3352 		 */
3353 		cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
3354 						      GPIOD_OUT_LOW);
3355 		if (IS_ERR(cs[i]))
3356 			return PTR_ERR(cs[i]);
3357 
3358 		if (cs[i]) {
3359 			/*
3360 			 * If we find a CS GPIO, name it after the device and
3361 			 * chip select line.
3362 			 */
3363 			char *gpioname;
3364 
3365 			gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
3366 						  dev_name(dev), i);
3367 			if (!gpioname)
3368 				return -ENOMEM;
3369 			gpiod_set_consumer_name(cs[i], gpioname);
3370 			num_cs_gpios++;
3371 			continue;
3372 		}
3373 
3374 		if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
3375 			dev_err(dev, "Invalid native chip select %d\n", i);
3376 			return -EINVAL;
3377 		}
3378 		native_cs_mask |= BIT(i);
3379 	}
3380 
3381 	ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
3382 
3383 	if ((ctlr->flags & SPI_CONTROLLER_GPIO_SS) && num_cs_gpios &&
3384 	    ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
3385 		dev_err(dev, "No unused native chip select available\n");
3386 		return -EINVAL;
3387 	}
3388 
3389 	return 0;
3390 }
3391 
3392 static int spi_controller_check_ops(struct spi_controller *ctlr)
3393 {
3394 	/*
3395 	 * The controller may implement only the high-level SPI-memory like
3396 	 * operations if it does not support regular SPI transfers, and this is
3397 	 * valid use case.
3398 	 * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least
3399 	 * one of the ->transfer_xxx() method be implemented.
3400 	 */
3401 	if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
3402 		if (!ctlr->transfer && !ctlr->transfer_one &&
3403 		   !ctlr->transfer_one_message) {
3404 			return -EINVAL;
3405 		}
3406 	}
3407 
3408 	return 0;
3409 }
3410 
3411 /* Allocate dynamic bus number using Linux idr */
3412 static int spi_controller_id_alloc(struct spi_controller *ctlr, int start, int end)
3413 {
3414 	int id;
3415 
3416 	mutex_lock(&board_lock);
3417 	id = idr_alloc(&spi_controller_idr, ctlr, start, end, GFP_KERNEL);
3418 	mutex_unlock(&board_lock);
3419 	if (WARN(id < 0, "couldn't get idr"))
3420 		return id == -ENOSPC ? -EBUSY : id;
3421 	ctlr->bus_num = id;
3422 	return 0;
3423 }
3424 
3425 /**
3426  * spi_register_controller - register SPI host or target controller
3427  * @ctlr: initialized controller, originally from spi_alloc_host() or
3428  *	spi_alloc_target()
3429  * Context: can sleep
3430  *
3431  * SPI controllers connect to their drivers using some non-SPI bus,
3432  * such as the platform bus.  The final stage of probe() in that code
3433  * includes calling spi_register_controller() to hook up to this SPI bus glue.
3434  *
3435  * SPI controllers use board specific (often SOC specific) bus numbers,
3436  * and board-specific addressing for SPI devices combines those numbers
3437  * with chip select numbers.  Since SPI does not directly support dynamic
3438  * device identification, boards need configuration tables telling which
3439  * chip is at which address.
3440  *
3441  * This must be called from context that can sleep.
3442  *
3443  * After a successful return, the caller is responsible for calling
3444  * spi_unregister_controller().
3445  *
3446  * Return: zero on success, else a negative error code.
3447  */
3448 int spi_register_controller(struct spi_controller *ctlr)
3449 {
3450 	struct device		*dev = ctlr->dev.parent;
3451 	struct boardinfo	*bi;
3452 	int			first_dynamic;
3453 	int			status;
3454 	int			idx;
3455 
3456 	if (!dev)
3457 		return -ENODEV;
3458 
3459 	/*
3460 	 * Make sure all necessary hooks are implemented before registering
3461 	 * the SPI controller.
3462 	 */
3463 	status = spi_controller_check_ops(ctlr);
3464 	if (status)
3465 		return status;
3466 
3467 	if (ctlr->bus_num < 0)
3468 		ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi");
3469 	if (ctlr->bus_num >= 0) {
3470 		/* Devices with a fixed bus num must check-in with the num */
3471 		status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1);
3472 		if (status)
3473 			return status;
3474 	}
3475 	if (ctlr->bus_num < 0) {
3476 		first_dynamic = of_alias_get_highest_id("spi");
3477 		if (first_dynamic < 0)
3478 			first_dynamic = 0;
3479 		else
3480 			first_dynamic++;
3481 
3482 		status = spi_controller_id_alloc(ctlr, first_dynamic, 0);
3483 		if (status)
3484 			return status;
3485 	}
3486 	ctlr->bus_lock_flag = 0;
3487 	init_completion(&ctlr->xfer_completion);
3488 	init_completion(&ctlr->cur_msg_completion);
3489 	if (!ctlr->max_dma_len)
3490 		ctlr->max_dma_len = INT_MAX;
3491 
3492 	/*
3493 	 * Register the device, then userspace will see it.
3494 	 * Registration fails if the bus ID is in use.
3495 	 */
3496 	dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
3497 
3498 	if (!spi_controller_is_target(ctlr) && ctlr->use_gpio_descriptors) {
3499 		status = spi_get_gpio_descs(ctlr);
3500 		if (status)
3501 			goto free_bus_id;
3502 		/*
3503 		 * A controller using GPIO descriptors always
3504 		 * supports SPI_CS_HIGH if need be.
3505 		 */
3506 		ctlr->mode_bits |= SPI_CS_HIGH;
3507 	}
3508 
3509 	/*
3510 	 * Even if it's just one always-selected device, there must
3511 	 * be at least one chipselect.
3512 	 */
3513 	if (!ctlr->num_chipselect) {
3514 		status = -EINVAL;
3515 		goto free_bus_id;
3516 	}
3517 
3518 	/* Setting last_cs to SPI_INVALID_CS means no chip selected */
3519 	for (idx = 0; idx < SPI_DEVICE_CS_CNT_MAX; idx++)
3520 		ctlr->last_cs[idx] = SPI_INVALID_CS;
3521 
3522 	status = device_add(&ctlr->dev);
3523 	if (status < 0)
3524 		goto free_bus_id;
3525 	dev_dbg(dev, "registered %s %s\n",
3526 			spi_controller_is_target(ctlr) ? "target" : "host",
3527 			dev_name(&ctlr->dev));
3528 
3529 	/*
3530 	 * If we're using a queued driver, start the queue. Note that we don't
3531 	 * need the queueing logic if the driver is only supporting high-level
3532 	 * memory operations.
3533 	 */
3534 	if (ctlr->transfer) {
3535 		dev_info(dev, "controller is unqueued, this is deprecated\n");
3536 	} else if (ctlr->transfer_one || ctlr->transfer_one_message) {
3537 		status = spi_controller_initialize_queue(ctlr);
3538 		if (status)
3539 			goto del_ctrl;
3540 	}
3541 
3542 	mutex_lock(&board_lock);
3543 	list_add_tail(&ctlr->list, &spi_controller_list);
3544 	list_for_each_entry(bi, &board_list, list)
3545 		spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
3546 	mutex_unlock(&board_lock);
3547 
3548 	/* Register devices from the device tree and ACPI */
3549 	of_register_spi_devices(ctlr);
3550 	acpi_register_spi_devices(ctlr);
3551 
3552 	return 0;
3553 
3554 del_ctrl:
3555 	device_del(&ctlr->dev);
3556 free_bus_id:
3557 	mutex_lock(&board_lock);
3558 	idr_remove(&spi_controller_idr, ctlr->bus_num);
3559 	mutex_unlock(&board_lock);
3560 
3561 	return status;
3562 }
3563 EXPORT_SYMBOL_GPL(spi_register_controller);
3564 
3565 static void devm_spi_unregister_controller(void *ctlr)
3566 {
3567 	spi_unregister_controller(ctlr);
3568 }
3569 
3570 /**
3571  * devm_spi_register_controller - register managed SPI host or target controller
3572  * @dev:    device managing SPI controller
3573  * @ctlr: initialized controller, originally from spi_alloc_host() or
3574  *	spi_alloc_target()
3575  * Context: can sleep
3576  *
3577  * Register a SPI device as with spi_register_controller() which will
3578  * automatically be unregistered.
3579  *
3580  * Return: zero on success, else a negative error code.
3581  */
3582 int devm_spi_register_controller(struct device *dev,
3583 				 struct spi_controller *ctlr)
3584 {
3585 	int ret;
3586 
3587 	ret = spi_register_controller(ctlr);
3588 	if (ret)
3589 		return ret;
3590 
3591 	return devm_add_action_or_reset(dev, devm_spi_unregister_controller, ctlr);
3592 }
3593 EXPORT_SYMBOL_GPL(devm_spi_register_controller);
3594 
3595 static int __unregister(struct device *dev, void *null)
3596 {
3597 	spi_unregister_device(to_spi_device(dev));
3598 	return 0;
3599 }
3600 
3601 /**
3602  * spi_unregister_controller - unregister SPI host or target controller
3603  * @ctlr: the controller being unregistered
3604  * Context: can sleep
3605  *
3606  * This call is used only by SPI controller drivers, which are the
3607  * only ones directly touching chip registers.
3608  *
3609  * This must be called from context that can sleep.
3610  */
3611 void spi_unregister_controller(struct spi_controller *ctlr)
3612 {
3613 	struct spi_controller *found;
3614 	int id = ctlr->bus_num;
3615 
3616 	/* Prevent addition of new devices, unregister existing ones */
3617 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
3618 		mutex_lock(&ctlr->add_lock);
3619 
3620 	device_for_each_child(&ctlr->dev, NULL, __unregister);
3621 
3622 	/* First make sure that this controller was ever added */
3623 	mutex_lock(&board_lock);
3624 	found = idr_find(&spi_controller_idr, id);
3625 	mutex_unlock(&board_lock);
3626 	if (ctlr->queued) {
3627 		if (spi_destroy_queue(ctlr))
3628 			dev_err(&ctlr->dev, "queue remove failed\n");
3629 	}
3630 	mutex_lock(&board_lock);
3631 	list_del(&ctlr->list);
3632 	mutex_unlock(&board_lock);
3633 
3634 	device_del(&ctlr->dev);
3635 
3636 	/* Free bus id */
3637 	mutex_lock(&board_lock);
3638 	if (found == ctlr)
3639 		idr_remove(&spi_controller_idr, id);
3640 	mutex_unlock(&board_lock);
3641 
3642 	if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
3643 		mutex_unlock(&ctlr->add_lock);
3644 }
3645 EXPORT_SYMBOL_GPL(spi_unregister_controller);
3646 
3647 static inline int __spi_check_suspended(const struct spi_controller *ctlr)
3648 {
3649 	return ctlr->flags & SPI_CONTROLLER_SUSPENDED ? -ESHUTDOWN : 0;
3650 }
3651 
3652 static inline void __spi_mark_suspended(struct spi_controller *ctlr)
3653 {
3654 	mutex_lock(&ctlr->bus_lock_mutex);
3655 	ctlr->flags |= SPI_CONTROLLER_SUSPENDED;
3656 	mutex_unlock(&ctlr->bus_lock_mutex);
3657 }
3658 
3659 static inline void __spi_mark_resumed(struct spi_controller *ctlr)
3660 {
3661 	mutex_lock(&ctlr->bus_lock_mutex);
3662 	ctlr->flags &= ~SPI_CONTROLLER_SUSPENDED;
3663 	mutex_unlock(&ctlr->bus_lock_mutex);
3664 }
3665 
3666 int spi_controller_suspend(struct spi_controller *ctlr)
3667 {
3668 	int ret = 0;
3669 
3670 	/* Basically no-ops for non-queued controllers */
3671 	if (ctlr->queued) {
3672 		ret = spi_stop_queue(ctlr);
3673 		if (ret)
3674 			dev_err(&ctlr->dev, "queue stop failed\n");
3675 	}
3676 
3677 	__spi_mark_suspended(ctlr);
3678 	return ret;
3679 }
3680 EXPORT_SYMBOL_GPL(spi_controller_suspend);
3681 
3682 int spi_controller_resume(struct spi_controller *ctlr)
3683 {
3684 	int ret = 0;
3685 
3686 	__spi_mark_resumed(ctlr);
3687 
3688 	if (ctlr->queued) {
3689 		ret = spi_start_queue(ctlr);
3690 		if (ret)
3691 			dev_err(&ctlr->dev, "queue restart failed\n");
3692 	}
3693 	return ret;
3694 }
3695 EXPORT_SYMBOL_GPL(spi_controller_resume);
3696 
3697 /*-------------------------------------------------------------------------*/
3698 
3699 /* Core methods for spi_message alterations */
3700 
3701 static void __spi_replace_transfers_release(struct spi_controller *ctlr,
3702 					    struct spi_message *msg,
3703 					    void *res)
3704 {
3705 	struct spi_replaced_transfers *rxfer = res;
3706 	size_t i;
3707 
3708 	/* Call extra callback if requested */
3709 	if (rxfer->release)
3710 		rxfer->release(ctlr, msg, res);
3711 
3712 	/* Insert replaced transfers back into the message */
3713 	list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
3714 
3715 	/* Remove the formerly inserted entries */
3716 	for (i = 0; i < rxfer->inserted; i++)
3717 		list_del(&rxfer->inserted_transfers[i].transfer_list);
3718 }
3719 
3720 /**
3721  * spi_replace_transfers - replace transfers with several transfers
3722  *                         and register change with spi_message.resources
3723  * @msg:           the spi_message we work upon
3724  * @xfer_first:    the first spi_transfer we want to replace
3725  * @remove:        number of transfers to remove
3726  * @insert:        the number of transfers we want to insert instead
3727  * @release:       extra release code necessary in some circumstances
3728  * @extradatasize: extra data to allocate (with alignment guarantees
3729  *                 of struct @spi_transfer)
3730  * @gfp:           gfp flags
3731  *
3732  * Returns: pointer to @spi_replaced_transfers,
3733  *          PTR_ERR(...) in case of errors.
3734  */
3735 static struct spi_replaced_transfers *spi_replace_transfers(
3736 	struct spi_message *msg,
3737 	struct spi_transfer *xfer_first,
3738 	size_t remove,
3739 	size_t insert,
3740 	spi_replaced_release_t release,
3741 	size_t extradatasize,
3742 	gfp_t gfp)
3743 {
3744 	struct spi_replaced_transfers *rxfer;
3745 	struct spi_transfer *xfer;
3746 	size_t i;
3747 
3748 	/* Allocate the structure using spi_res */
3749 	rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
3750 			      struct_size(rxfer, inserted_transfers, insert)
3751 			      + extradatasize,
3752 			      gfp);
3753 	if (!rxfer)
3754 		return ERR_PTR(-ENOMEM);
3755 
3756 	/* The release code to invoke before running the generic release */
3757 	rxfer->release = release;
3758 
3759 	/* Assign extradata */
3760 	if (extradatasize)
3761 		rxfer->extradata =
3762 			&rxfer->inserted_transfers[insert];
3763 
3764 	/* Init the replaced_transfers list */
3765 	INIT_LIST_HEAD(&rxfer->replaced_transfers);
3766 
3767 	/*
3768 	 * Assign the list_entry after which we should reinsert
3769 	 * the @replaced_transfers - it may be spi_message.messages!
3770 	 */
3771 	rxfer->replaced_after = xfer_first->transfer_list.prev;
3772 
3773 	/* Remove the requested number of transfers */
3774 	for (i = 0; i < remove; i++) {
3775 		/*
3776 		 * If the entry after replaced_after it is msg->transfers
3777 		 * then we have been requested to remove more transfers
3778 		 * than are in the list.
3779 		 */
3780 		if (rxfer->replaced_after->next == &msg->transfers) {
3781 			dev_err(&msg->spi->dev,
3782 				"requested to remove more spi_transfers than are available\n");
3783 			/* Insert replaced transfers back into the message */
3784 			list_splice(&rxfer->replaced_transfers,
3785 				    rxfer->replaced_after);
3786 
3787 			/* Free the spi_replace_transfer structure... */
3788 			spi_res_free(rxfer);
3789 
3790 			/* ...and return with an error */
3791 			return ERR_PTR(-EINVAL);
3792 		}
3793 
3794 		/*
3795 		 * Remove the entry after replaced_after from list of
3796 		 * transfers and add it to list of replaced_transfers.
3797 		 */
3798 		list_move_tail(rxfer->replaced_after->next,
3799 			       &rxfer->replaced_transfers);
3800 	}
3801 
3802 	/*
3803 	 * Create copy of the given xfer with identical settings
3804 	 * based on the first transfer to get removed.
3805 	 */
3806 	for (i = 0; i < insert; i++) {
3807 		/* We need to run in reverse order */
3808 		xfer = &rxfer->inserted_transfers[insert - 1 - i];
3809 
3810 		/* Copy all spi_transfer data */
3811 		memcpy(xfer, xfer_first, sizeof(*xfer));
3812 
3813 		/* Add to list */
3814 		list_add(&xfer->transfer_list, rxfer->replaced_after);
3815 
3816 		/* Clear cs_change and delay for all but the last */
3817 		if (i) {
3818 			xfer->cs_change = false;
3819 			xfer->delay.value = 0;
3820 		}
3821 	}
3822 
3823 	/* Set up inserted... */
3824 	rxfer->inserted = insert;
3825 
3826 	/* ...and register it with spi_res/spi_message */
3827 	spi_res_add(msg, rxfer);
3828 
3829 	return rxfer;
3830 }
3831 
3832 static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
3833 					struct spi_message *msg,
3834 					struct spi_transfer **xferp,
3835 					size_t maxsize)
3836 {
3837 	struct spi_transfer *xfer = *xferp, *xfers;
3838 	struct spi_replaced_transfers *srt;
3839 	size_t offset;
3840 	size_t count, i;
3841 
3842 	/* Calculate how many we have to replace */
3843 	count = DIV_ROUND_UP(xfer->len, maxsize);
3844 
3845 	/* Create replacement */
3846 	srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, GFP_KERNEL);
3847 	if (IS_ERR(srt))
3848 		return PTR_ERR(srt);
3849 	xfers = srt->inserted_transfers;
3850 
3851 	/*
3852 	 * Now handle each of those newly inserted spi_transfers.
3853 	 * Note that the replacements spi_transfers all are preset
3854 	 * to the same values as *xferp, so tx_buf, rx_buf and len
3855 	 * are all identical (as well as most others)
3856 	 * so we just have to fix up len and the pointers.
3857 	 */
3858 
3859 	/*
3860 	 * The first transfer just needs the length modified, so we
3861 	 * run it outside the loop.
3862 	 */
3863 	xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
3864 
3865 	/* All the others need rx_buf/tx_buf also set */
3866 	for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3867 		/* Update rx_buf, tx_buf and DMA */
3868 		if (xfers[i].rx_buf)
3869 			xfers[i].rx_buf += offset;
3870 		if (xfers[i].tx_buf)
3871 			xfers[i].tx_buf += offset;
3872 
3873 		/* Update length */
3874 		xfers[i].len = min(maxsize, xfers[i].len - offset);
3875 	}
3876 
3877 	/*
3878 	 * We set up xferp to the last entry we have inserted,
3879 	 * so that we skip those already split transfers.
3880 	 */
3881 	*xferp = &xfers[count - 1];
3882 
3883 	/* Increment statistics counters */
3884 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics,
3885 				       transfers_split_maxsize);
3886 	SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics,
3887 				       transfers_split_maxsize);
3888 
3889 	return 0;
3890 }
3891 
3892 /**
3893  * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3894  *                               when an individual transfer exceeds a
3895  *                               certain size
3896  * @ctlr:    the @spi_controller for this transfer
3897  * @msg:   the @spi_message to transform
3898  * @maxsize:  the maximum when to apply this
3899  *
3900  * This function allocates resources that are automatically freed during the
3901  * spi message unoptimize phase so this function should only be called from
3902  * optimize_message callbacks.
3903  *
3904  * Return: status of transformation
3905  */
3906 int spi_split_transfers_maxsize(struct spi_controller *ctlr,
3907 				struct spi_message *msg,
3908 				size_t maxsize)
3909 {
3910 	struct spi_transfer *xfer;
3911 	int ret;
3912 
3913 	/*
3914 	 * Iterate over the transfer_list,
3915 	 * but note that xfer is advanced to the last transfer inserted
3916 	 * to avoid checking sizes again unnecessarily (also xfer does
3917 	 * potentially belong to a different list by the time the
3918 	 * replacement has happened).
3919 	 */
3920 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3921 		if (xfer->len > maxsize) {
3922 			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3923 							   maxsize);
3924 			if (ret)
3925 				return ret;
3926 		}
3927 	}
3928 
3929 	return 0;
3930 }
3931 EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
3932 
3933 
3934 /**
3935  * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3936  *                                when an individual transfer exceeds a
3937  *                                certain number of SPI words
3938  * @ctlr:     the @spi_controller for this transfer
3939  * @msg:      the @spi_message to transform
3940  * @maxwords: the number of words to limit each transfer to
3941  *
3942  * This function allocates resources that are automatically freed during the
3943  * spi message unoptimize phase so this function should only be called from
3944  * optimize_message callbacks.
3945  *
3946  * Return: status of transformation
3947  */
3948 int spi_split_transfers_maxwords(struct spi_controller *ctlr,
3949 				 struct spi_message *msg,
3950 				 size_t maxwords)
3951 {
3952 	struct spi_transfer *xfer;
3953 
3954 	/*
3955 	 * Iterate over the transfer_list,
3956 	 * but note that xfer is advanced to the last transfer inserted
3957 	 * to avoid checking sizes again unnecessarily (also xfer does
3958 	 * potentially belong to a different list by the time the
3959 	 * replacement has happened).
3960 	 */
3961 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3962 		size_t maxsize;
3963 		int ret;
3964 
3965 		maxsize = maxwords * spi_bpw_to_bytes(xfer->bits_per_word);
3966 		if (xfer->len > maxsize) {
3967 			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3968 							   maxsize);
3969 			if (ret)
3970 				return ret;
3971 		}
3972 	}
3973 
3974 	return 0;
3975 }
3976 EXPORT_SYMBOL_GPL(spi_split_transfers_maxwords);
3977 
3978 /*-------------------------------------------------------------------------*/
3979 
3980 /*
3981  * Core methods for SPI controller protocol drivers. Some of the
3982  * other core methods are currently defined as inline functions.
3983  */
3984 
3985 static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
3986 					u8 bits_per_word)
3987 {
3988 	if (ctlr->bits_per_word_mask) {
3989 		/* Only 32 bits fit in the mask */
3990 		if (bits_per_word > 32)
3991 			return -EINVAL;
3992 		if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
3993 			return -EINVAL;
3994 	}
3995 
3996 	return 0;
3997 }
3998 
3999 /**
4000  * spi_set_cs_timing - configure CS setup, hold, and inactive delays
4001  * @spi: the device that requires specific CS timing configuration
4002  *
4003  * Return: zero on success, else a negative error code.
4004  */
4005 static int spi_set_cs_timing(struct spi_device *spi)
4006 {
4007 	struct device *parent = spi->controller->dev.parent;
4008 	int status = 0;
4009 
4010 	if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) {
4011 		if (spi->controller->auto_runtime_pm) {
4012 			status = pm_runtime_get_sync(parent);
4013 			if (status < 0) {
4014 				pm_runtime_put_noidle(parent);
4015 				dev_err(&spi->controller->dev, "Failed to power device: %d\n",
4016 					status);
4017 				return status;
4018 			}
4019 
4020 			status = spi->controller->set_cs_timing(spi);
4021 			pm_runtime_put_autosuspend(parent);
4022 		} else {
4023 			status = spi->controller->set_cs_timing(spi);
4024 		}
4025 	}
4026 	return status;
4027 }
4028 
4029 static int __spi_setup(struct spi_device *spi, bool initial_setup)
4030 {
4031 	unsigned	bad_bits, ugly_bits;
4032 	int		status;
4033 
4034 	/*
4035 	 * Check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO
4036 	 * are set at the same time.
4037 	 */
4038 	if ((hweight_long(spi->mode &
4039 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) ||
4040 	    (hweight_long(spi->mode &
4041 		(SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) {
4042 		dev_err(&spi->dev,
4043 		"setup: can not select any two of dual, quad and no-rx/tx at the same time\n");
4044 		return -EINVAL;
4045 	}
4046 	/* If it is SPI_3WIRE mode, DUAL and QUAD should be forbidden */
4047 	if ((spi->mode & SPI_3WIRE) && (spi->mode &
4048 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
4049 		 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
4050 		return -EINVAL;
4051 	/* Check against conflicting MOSI idle configuration */
4052 	if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) {
4053 		dev_err(&spi->dev,
4054 			"setup: MOSI configured to idle low and high at the same time.\n");
4055 		return -EINVAL;
4056 	}
4057 	/*
4058 	 * Help drivers fail *cleanly* when they need options
4059 	 * that aren't supported with their current controller.
4060 	 * SPI_CS_WORD has a fallback software implementation,
4061 	 * so it is ignored here.
4062 	 */
4063 	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
4064 				 SPI_NO_TX | SPI_NO_RX);
4065 	ugly_bits = bad_bits &
4066 		    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
4067 		     SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
4068 	if (ugly_bits) {
4069 		dev_warn(&spi->dev,
4070 			 "setup: ignoring unsupported mode bits %x\n",
4071 			 ugly_bits);
4072 		spi->mode &= ~ugly_bits;
4073 		bad_bits &= ~ugly_bits;
4074 	}
4075 	if (bad_bits) {
4076 		dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
4077 			bad_bits);
4078 		return -EINVAL;
4079 	}
4080 
4081 	if (!spi->bits_per_word) {
4082 		spi->bits_per_word = 8;
4083 	} else {
4084 		/*
4085 		 * Some controllers may not support the default 8 bits-per-word
4086 		 * so only perform the check when this is explicitly provided.
4087 		 */
4088 		status = __spi_validate_bits_per_word(spi->controller,
4089 						      spi->bits_per_word);
4090 		if (status)
4091 			return status;
4092 	}
4093 
4094 	if (spi->controller->max_speed_hz &&
4095 	    (!spi->max_speed_hz ||
4096 	     spi->max_speed_hz > spi->controller->max_speed_hz))
4097 		spi->max_speed_hz = spi->controller->max_speed_hz;
4098 
4099 	mutex_lock(&spi->controller->io_mutex);
4100 
4101 	if (spi->controller->setup) {
4102 		status = spi->controller->setup(spi);
4103 		if (status) {
4104 			mutex_unlock(&spi->controller->io_mutex);
4105 			dev_err(&spi->controller->dev, "Failed to setup device: %d\n",
4106 				status);
4107 			return status;
4108 		}
4109 	}
4110 
4111 	status = spi_set_cs_timing(spi);
4112 	if (status) {
4113 		mutex_unlock(&spi->controller->io_mutex);
4114 		goto err_cleanup;
4115 	}
4116 
4117 	if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
4118 		status = pm_runtime_resume_and_get(spi->controller->dev.parent);
4119 		if (status < 0) {
4120 			mutex_unlock(&spi->controller->io_mutex);
4121 			dev_err(&spi->controller->dev, "Failed to power device: %d\n",
4122 				status);
4123 			goto err_cleanup;
4124 		}
4125 
4126 		/*
4127 		 * We do not want to return positive value from pm_runtime_get,
4128 		 * there are many instances of devices calling spi_setup() and
4129 		 * checking for a non-zero return value instead of a negative
4130 		 * return value.
4131 		 */
4132 		status = 0;
4133 
4134 		spi_set_cs(spi, false, true);
4135 		pm_runtime_put_autosuspend(spi->controller->dev.parent);
4136 	} else {
4137 		spi_set_cs(spi, false, true);
4138 	}
4139 
4140 	mutex_unlock(&spi->controller->io_mutex);
4141 
4142 	if (spi->rt && !spi->controller->rt) {
4143 		spi->controller->rt = true;
4144 		spi_set_thread_rt(spi->controller);
4145 	}
4146 
4147 	trace_spi_setup(spi, status);
4148 
4149 	dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
4150 			spi->mode & SPI_MODE_X_MASK,
4151 			(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
4152 			(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
4153 			(spi->mode & SPI_3WIRE) ? "3wire, " : "",
4154 			(spi->mode & SPI_LOOP) ? "loopback, " : "",
4155 			spi->bits_per_word, spi->max_speed_hz,
4156 			status);
4157 
4158 	return status;
4159 
4160 err_cleanup:
4161 	if (initial_setup)
4162 		spi_cleanup(spi);
4163 
4164 	return status;
4165 }
4166 
4167 /**
4168  * spi_setup - setup SPI mode and clock rate
4169  * @spi: the device whose settings are being modified
4170  * Context: can sleep, and no requests are queued to the device
4171  *
4172  * SPI protocol drivers may need to update the transfer mode if the
4173  * device doesn't work with its default.  They may likewise need
4174  * to update clock rates or word sizes from initial values.  This function
4175  * changes those settings, and must be called from a context that can sleep.
4176  * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
4177  * effect the next time the device is selected and data is transferred to
4178  * or from it.  When this function returns, the SPI device is deselected.
4179  *
4180  * Note that this call will fail if the protocol driver specifies an option
4181  * that the underlying controller or its driver does not support.  For
4182  * example, not all hardware supports wire transfers using nine bit words,
4183  * LSB-first wire encoding, or active-high chipselects.
4184  *
4185  * Return: zero on success, else a negative error code.
4186  */
4187 int spi_setup(struct spi_device *spi)
4188 {
4189 	return __spi_setup(spi, false);
4190 }
4191 EXPORT_SYMBOL_GPL(spi_setup);
4192 
4193 static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
4194 				       struct spi_device *spi)
4195 {
4196 	int delay1, delay2;
4197 
4198 	delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
4199 	if (delay1 < 0)
4200 		return delay1;
4201 
4202 	delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
4203 	if (delay2 < 0)
4204 		return delay2;
4205 
4206 	if (delay1 < delay2)
4207 		memcpy(&xfer->word_delay, &spi->word_delay,
4208 		       sizeof(xfer->word_delay));
4209 
4210 	return 0;
4211 }
4212 
4213 static int __spi_validate(struct spi_device *spi, struct spi_message *message)
4214 {
4215 	struct spi_controller *ctlr = spi->controller;
4216 	struct spi_transfer *xfer;
4217 	int w_size;
4218 
4219 	if (list_empty(&message->transfers))
4220 		return -EINVAL;
4221 
4222 	message->spi = spi;
4223 
4224 	/*
4225 	 * Half-duplex links include original MicroWire, and ones with
4226 	 * only one data pin like SPI_3WIRE (switches direction) or where
4227 	 * either MOSI or MISO is missing.  They can also be caused by
4228 	 * software limitations.
4229 	 */
4230 	if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
4231 	    (spi->mode & SPI_3WIRE)) {
4232 		unsigned flags = ctlr->flags;
4233 
4234 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
4235 			if (xfer->rx_buf && xfer->tx_buf)
4236 				return -EINVAL;
4237 			if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
4238 				return -EINVAL;
4239 			if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
4240 				return -EINVAL;
4241 		}
4242 	}
4243 
4244 	/*
4245 	 * Set transfer bits_per_word and max speed as spi device default if
4246 	 * it is not set for this transfer.
4247 	 * Set transfer tx_nbits and rx_nbits as single transfer default
4248 	 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
4249 	 * Ensure transfer word_delay is at least as long as that required by
4250 	 * device itself.
4251 	 */
4252 	message->frame_length = 0;
4253 	list_for_each_entry(xfer, &message->transfers, transfer_list) {
4254 		xfer->effective_speed_hz = 0;
4255 		message->frame_length += xfer->len;
4256 		if (!xfer->bits_per_word)
4257 			xfer->bits_per_word = spi->bits_per_word;
4258 
4259 		if (!xfer->speed_hz)
4260 			xfer->speed_hz = spi->max_speed_hz;
4261 
4262 		if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
4263 			xfer->speed_hz = ctlr->max_speed_hz;
4264 
4265 		if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
4266 			return -EINVAL;
4267 
4268 		/* DDR mode is supported only if controller has dtr_caps=true.
4269 		 * default considered as SDR mode for SPI and QSPI controller.
4270 		 * Note: This is applicable only to QSPI controller.
4271 		 */
4272 		if (xfer->dtr_mode && !ctlr->dtr_caps)
4273 			return -EINVAL;
4274 
4275 		/*
4276 		 * SPI transfer length should be multiple of SPI word size
4277 		 * where SPI word size should be power-of-two multiple.
4278 		 */
4279 		w_size = spi_bpw_to_bytes(xfer->bits_per_word);
4280 
4281 		/* No partial transfers accepted */
4282 		if (xfer->len % w_size)
4283 			return -EINVAL;
4284 
4285 		if (xfer->speed_hz && ctlr->min_speed_hz &&
4286 		    xfer->speed_hz < ctlr->min_speed_hz)
4287 			return -EINVAL;
4288 
4289 		if (xfer->tx_buf && !xfer->tx_nbits)
4290 			xfer->tx_nbits = SPI_NBITS_SINGLE;
4291 		if (xfer->rx_buf && !xfer->rx_nbits)
4292 			xfer->rx_nbits = SPI_NBITS_SINGLE;
4293 		/*
4294 		 * Check transfer tx/rx_nbits:
4295 		 * 1. check the value matches one of single, dual and quad
4296 		 * 2. check tx/rx_nbits match the mode in spi_device
4297 		 */
4298 		if (xfer->tx_buf) {
4299 			if (spi->mode & SPI_NO_TX)
4300 				return -EINVAL;
4301 			if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
4302 				xfer->tx_nbits != SPI_NBITS_DUAL &&
4303 				xfer->tx_nbits != SPI_NBITS_QUAD &&
4304 				xfer->tx_nbits != SPI_NBITS_OCTAL)
4305 				return -EINVAL;
4306 			if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
4307 				!(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL)))
4308 				return -EINVAL;
4309 			if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
4310 				!(spi->mode & (SPI_TX_QUAD | SPI_TX_OCTAL)))
4311 				return -EINVAL;
4312 			if ((xfer->tx_nbits == SPI_NBITS_OCTAL) &&
4313 				!(spi->mode & SPI_TX_OCTAL))
4314 				return -EINVAL;
4315 		}
4316 		/* Check transfer rx_nbits */
4317 		if (xfer->rx_buf) {
4318 			if (spi->mode & SPI_NO_RX)
4319 				return -EINVAL;
4320 			if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
4321 				xfer->rx_nbits != SPI_NBITS_DUAL &&
4322 				xfer->rx_nbits != SPI_NBITS_QUAD &&
4323 				xfer->rx_nbits != SPI_NBITS_OCTAL)
4324 				return -EINVAL;
4325 			if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
4326 				!(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
4327 				return -EINVAL;
4328 			if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
4329 				!(spi->mode & (SPI_RX_QUAD | SPI_RX_OCTAL)))
4330 				return -EINVAL;
4331 			if ((xfer->rx_nbits == SPI_NBITS_OCTAL) &&
4332 				!(spi->mode & SPI_RX_OCTAL))
4333 				return -EINVAL;
4334 		}
4335 
4336 		if (_spi_xfer_word_delay_update(xfer, spi))
4337 			return -EINVAL;
4338 
4339 		/* Make sure controller supports required offload features. */
4340 		if (xfer->offload_flags) {
4341 			if (!message->offload)
4342 				return -EINVAL;
4343 
4344 			if (xfer->offload_flags & ~message->offload->xfer_flags)
4345 				return -EINVAL;
4346 		}
4347 	}
4348 
4349 	message->status = -EINPROGRESS;
4350 
4351 	return 0;
4352 }
4353 
4354 /*
4355  * spi_split_transfers - generic handling of transfer splitting
4356  * @msg: the message to split
4357  *
4358  * Under certain conditions, a SPI controller may not support arbitrary
4359  * transfer sizes or other features required by a peripheral. This function
4360  * will split the transfers in the message into smaller transfers that are
4361  * supported by the controller.
4362  *
4363  * Controllers with special requirements not covered here can also split
4364  * transfers in the optimize_message() callback.
4365  *
4366  * Context: can sleep
4367  * Return: zero on success, else a negative error code
4368  */
4369 static int spi_split_transfers(struct spi_message *msg)
4370 {
4371 	struct spi_controller *ctlr = msg->spi->controller;
4372 	struct spi_transfer *xfer;
4373 	int ret;
4374 
4375 	/*
4376 	 * If an SPI controller does not support toggling the CS line on each
4377 	 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
4378 	 * for the CS line, we can emulate the CS-per-word hardware function by
4379 	 * splitting transfers into one-word transfers and ensuring that
4380 	 * cs_change is set for each transfer.
4381 	 */
4382 	if ((msg->spi->mode & SPI_CS_WORD) &&
4383 	    (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) {
4384 		ret = spi_split_transfers_maxwords(ctlr, msg, 1);
4385 		if (ret)
4386 			return ret;
4387 
4388 		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
4389 			/* Don't change cs_change on the last entry in the list */
4390 			if (list_is_last(&xfer->transfer_list, &msg->transfers))
4391 				break;
4392 
4393 			xfer->cs_change = 1;
4394 		}
4395 	} else {
4396 		ret = spi_split_transfers_maxsize(ctlr, msg,
4397 						  spi_max_transfer_size(msg->spi));
4398 		if (ret)
4399 			return ret;
4400 	}
4401 
4402 	return 0;
4403 }
4404 
4405 /*
4406  * __spi_optimize_message - shared implementation for spi_optimize_message()
4407  *                          and spi_maybe_optimize_message()
4408  * @spi: the device that will be used for the message
4409  * @msg: the message to optimize
4410  *
4411  * Peripheral drivers will call spi_optimize_message() and the spi core will
4412  * call spi_maybe_optimize_message() instead of calling this directly.
4413  *
4414  * It is not valid to call this on a message that has already been optimized.
4415  *
4416  * Return: zero on success, else a negative error code
4417  */
4418 static int __spi_optimize_message(struct spi_device *spi,
4419 				  struct spi_message *msg)
4420 {
4421 	struct spi_controller *ctlr = spi->controller;
4422 	int ret;
4423 
4424 	ret = __spi_validate(spi, msg);
4425 	if (ret)
4426 		return ret;
4427 
4428 	ret = spi_split_transfers(msg);
4429 	if (ret)
4430 		return ret;
4431 
4432 	if (ctlr->optimize_message) {
4433 		ret = ctlr->optimize_message(msg);
4434 		if (ret) {
4435 			spi_res_release(ctlr, msg);
4436 			return ret;
4437 		}
4438 	}
4439 
4440 	msg->optimized = true;
4441 
4442 	return 0;
4443 }
4444 
4445 /*
4446  * spi_maybe_optimize_message - optimize message if it isn't already pre-optimized
4447  * @spi: the device that will be used for the message
4448  * @msg: the message to optimize
4449  * Return: zero on success, else a negative error code
4450  */
4451 static int spi_maybe_optimize_message(struct spi_device *spi,
4452 				      struct spi_message *msg)
4453 {
4454 	if (spi->controller->defer_optimize_message) {
4455 		msg->spi = spi;
4456 		return 0;
4457 	}
4458 
4459 	if (msg->pre_optimized)
4460 		return 0;
4461 
4462 	return __spi_optimize_message(spi, msg);
4463 }
4464 
4465 /**
4466  * spi_optimize_message - do any one-time validation and setup for a SPI message
4467  * @spi: the device that will be used for the message
4468  * @msg: the message to optimize
4469  *
4470  * Peripheral drivers that reuse the same message repeatedly may call this to
4471  * perform as much message prep as possible once, rather than repeating it each
4472  * time a message transfer is performed to improve throughput and reduce CPU
4473  * usage.
4474  *
4475  * Once a message has been optimized, it cannot be modified with the exception
4476  * of updating the contents of any xfer->tx_buf (the pointer can't be changed,
4477  * only the data in the memory it points to).
4478  *
4479  * Calls to this function must be balanced with calls to spi_unoptimize_message()
4480  * to avoid leaking resources.
4481  *
4482  * Context: can sleep
4483  * Return: zero on success, else a negative error code
4484  */
4485 int spi_optimize_message(struct spi_device *spi, struct spi_message *msg)
4486 {
4487 	int ret;
4488 
4489 	/*
4490 	 * Pre-optimization is not supported and optimization is deferred e.g.
4491 	 * when using spi-mux.
4492 	 */
4493 	if (spi->controller->defer_optimize_message)
4494 		return 0;
4495 
4496 	ret = __spi_optimize_message(spi, msg);
4497 	if (ret)
4498 		return ret;
4499 
4500 	/*
4501 	 * This flag indicates that the peripheral driver called spi_optimize_message()
4502 	 * and therefore we shouldn't unoptimize message automatically when finalizing
4503 	 * the message but rather wait until spi_unoptimize_message() is called
4504 	 * by the peripheral driver.
4505 	 */
4506 	msg->pre_optimized = true;
4507 
4508 	return 0;
4509 }
4510 EXPORT_SYMBOL_GPL(spi_optimize_message);
4511 
4512 /**
4513  * spi_unoptimize_message - releases any resources allocated by spi_optimize_message()
4514  * @msg: the message to unoptimize
4515  *
4516  * Calls to this function must be balanced with calls to spi_optimize_message().
4517  *
4518  * Context: can sleep
4519  */
4520 void spi_unoptimize_message(struct spi_message *msg)
4521 {
4522 	if (msg->spi->controller->defer_optimize_message)
4523 		return;
4524 
4525 	__spi_unoptimize_message(msg);
4526 	msg->pre_optimized = false;
4527 }
4528 EXPORT_SYMBOL_GPL(spi_unoptimize_message);
4529 
4530 static int __spi_async(struct spi_device *spi, struct spi_message *message)
4531 {
4532 	struct spi_controller *ctlr = spi->controller;
4533 	struct spi_transfer *xfer;
4534 
4535 	/*
4536 	 * Some controllers do not support doing regular SPI transfers. Return
4537 	 * ENOTSUPP when this is the case.
4538 	 */
4539 	if (!ctlr->transfer)
4540 		return -ENOTSUPP;
4541 
4542 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async);
4543 	SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async);
4544 
4545 	trace_spi_message_submit(message);
4546 
4547 	if (!ctlr->ptp_sts_supported) {
4548 		list_for_each_entry(xfer, &message->transfers, transfer_list) {
4549 			xfer->ptp_sts_word_pre = 0;
4550 			ptp_read_system_prets(xfer->ptp_sts);
4551 		}
4552 	}
4553 
4554 	return ctlr->transfer(spi, message);
4555 }
4556 
4557 static void devm_spi_unoptimize_message(void *msg)
4558 {
4559 	spi_unoptimize_message(msg);
4560 }
4561 
4562 /**
4563  * devm_spi_optimize_message - managed version of spi_optimize_message()
4564  * @dev: the device that manages @msg (usually @spi->dev)
4565  * @spi: the device that will be used for the message
4566  * @msg: the message to optimize
4567  * Return: zero on success, else a negative error code
4568  *
4569  * spi_unoptimize_message() will automatically be called when the device is
4570  * removed.
4571  */
4572 int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,
4573 			      struct spi_message *msg)
4574 {
4575 	int ret;
4576 
4577 	ret = spi_optimize_message(spi, msg);
4578 	if (ret)
4579 		return ret;
4580 
4581 	return devm_add_action_or_reset(dev, devm_spi_unoptimize_message, msg);
4582 }
4583 EXPORT_SYMBOL_GPL(devm_spi_optimize_message);
4584 
4585 /**
4586  * spi_async - asynchronous SPI transfer
4587  * @spi: device with which data will be exchanged
4588  * @message: describes the data transfers, including completion callback
4589  * Context: any (IRQs may be blocked, etc)
4590  *
4591  * This call may be used in_irq and other contexts which can't sleep,
4592  * as well as from task contexts which can sleep.
4593  *
4594  * The completion callback is invoked in a context which can't sleep.
4595  * Before that invocation, the value of message->status is undefined.
4596  * When the callback is issued, message->status holds either zero (to
4597  * indicate complete success) or a negative error code.  After that
4598  * callback returns, the driver which issued the transfer request may
4599  * deallocate the associated memory; it's no longer in use by any SPI
4600  * core or controller driver code.
4601  *
4602  * Note that although all messages to a spi_device are handled in
4603  * FIFO order, messages may go to different devices in other orders.
4604  * Some device might be higher priority, or have various "hard" access
4605  * time requirements, for example.
4606  *
4607  * On detection of any fault during the transfer, processing of
4608  * the entire message is aborted, and the device is deselected.
4609  * Until returning from the associated message completion callback,
4610  * no other spi_message queued to that device will be processed.
4611  * (This rule applies equally to all the synchronous transfer calls,
4612  * which are wrappers around this core asynchronous primitive.)
4613  *
4614  * Return: zero on success, else a negative error code.
4615  */
4616 int spi_async(struct spi_device *spi, struct spi_message *message)
4617 {
4618 	struct spi_controller *ctlr = spi->controller;
4619 	int ret;
4620 	unsigned long flags;
4621 
4622 	ret = spi_maybe_optimize_message(spi, message);
4623 	if (ret)
4624 		return ret;
4625 
4626 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4627 
4628 	if (ctlr->bus_lock_flag)
4629 		ret = -EBUSY;
4630 	else
4631 		ret = __spi_async(spi, message);
4632 
4633 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4634 
4635 	return ret;
4636 }
4637 EXPORT_SYMBOL_GPL(spi_async);
4638 
4639 static void __spi_transfer_message_noqueue(struct spi_controller *ctlr, struct spi_message *msg)
4640 {
4641 	bool was_busy;
4642 	int ret;
4643 
4644 	mutex_lock(&ctlr->io_mutex);
4645 
4646 	was_busy = ctlr->busy;
4647 
4648 	ctlr->cur_msg = msg;
4649 	ret = __spi_pump_transfer_message(ctlr, msg, was_busy);
4650 	if (ret)
4651 		dev_err(&ctlr->dev, "noqueue transfer failed\n");
4652 	ctlr->cur_msg = NULL;
4653 	ctlr->fallback = false;
4654 
4655 	if (!was_busy) {
4656 		kfree(ctlr->dummy_rx);
4657 		ctlr->dummy_rx = NULL;
4658 		kfree(ctlr->dummy_tx);
4659 		ctlr->dummy_tx = NULL;
4660 		if (ctlr->unprepare_transfer_hardware &&
4661 		    ctlr->unprepare_transfer_hardware(ctlr))
4662 			dev_err(&ctlr->dev,
4663 				"failed to unprepare transfer hardware\n");
4664 		spi_idle_runtime_pm(ctlr);
4665 	}
4666 
4667 	mutex_unlock(&ctlr->io_mutex);
4668 }
4669 
4670 /*-------------------------------------------------------------------------*/
4671 
4672 /*
4673  * Utility methods for SPI protocol drivers, layered on
4674  * top of the core.  Some other utility methods are defined as
4675  * inline functions.
4676  */
4677 
4678 static void spi_complete(void *arg)
4679 {
4680 	complete(arg);
4681 }
4682 
4683 static int __spi_sync(struct spi_device *spi, struct spi_message *message)
4684 {
4685 	DECLARE_COMPLETION_ONSTACK(done);
4686 	unsigned long flags;
4687 	int status;
4688 	struct spi_controller *ctlr = spi->controller;
4689 
4690 	if (__spi_check_suspended(ctlr)) {
4691 		dev_warn_once(&spi->dev, "Attempted to sync while suspend\n");
4692 		return -ESHUTDOWN;
4693 	}
4694 
4695 	status = spi_maybe_optimize_message(spi, message);
4696 	if (status)
4697 		return status;
4698 
4699 	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync);
4700 	SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync);
4701 
4702 	/*
4703 	 * Checking queue_empty here only guarantees async/sync message
4704 	 * ordering when coming from the same context. It does not need to
4705 	 * guard against reentrancy from a different context. The io_mutex
4706 	 * will catch those cases.
4707 	 */
4708 	if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) {
4709 		message->actual_length = 0;
4710 		message->status = -EINPROGRESS;
4711 
4712 		trace_spi_message_submit(message);
4713 
4714 		SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate);
4715 		SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate);
4716 
4717 		__spi_transfer_message_noqueue(ctlr, message);
4718 
4719 		return message->status;
4720 	}
4721 
4722 	/*
4723 	 * There are messages in the async queue that could have originated
4724 	 * from the same context, so we need to preserve ordering.
4725 	 * Therefor we send the message to the async queue and wait until they
4726 	 * are completed.
4727 	 */
4728 	message->complete = spi_complete;
4729 	message->context = &done;
4730 
4731 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4732 	status = __spi_async(spi, message);
4733 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4734 
4735 	if (status == 0) {
4736 		wait_for_completion(&done);
4737 		status = message->status;
4738 	}
4739 	message->complete = NULL;
4740 	message->context = NULL;
4741 
4742 	return status;
4743 }
4744 
4745 /**
4746  * spi_sync - blocking/synchronous SPI data transfers
4747  * @spi: device with which data will be exchanged
4748  * @message: describes the data transfers
4749  * Context: can sleep
4750  *
4751  * This call may only be used from a context that may sleep.  The sleep
4752  * is non-interruptible, and has no timeout.  Low-overhead controller
4753  * drivers may DMA directly into and out of the message buffers.
4754  *
4755  * Note that the SPI device's chip select is active during the message,
4756  * and then is normally disabled between messages.  Drivers for some
4757  * frequently-used devices may want to minimize costs of selecting a chip,
4758  * by leaving it selected in anticipation that the next message will go
4759  * to the same chip.  (That may increase power usage.)
4760  *
4761  * Also, the caller is guaranteeing that the memory associated with the
4762  * message will not be freed before this call returns.
4763  *
4764  * Return: zero on success, else a negative error code.
4765  */
4766 int spi_sync(struct spi_device *spi, struct spi_message *message)
4767 {
4768 	int ret;
4769 
4770 	mutex_lock(&spi->controller->bus_lock_mutex);
4771 	ret = __spi_sync(spi, message);
4772 	mutex_unlock(&spi->controller->bus_lock_mutex);
4773 
4774 	return ret;
4775 }
4776 EXPORT_SYMBOL_GPL(spi_sync);
4777 
4778 /**
4779  * spi_sync_locked - version of spi_sync with exclusive bus usage
4780  * @spi: device with which data will be exchanged
4781  * @message: describes the data transfers
4782  * Context: can sleep
4783  *
4784  * This call may only be used from a context that may sleep.  The sleep
4785  * is non-interruptible, and has no timeout.  Low-overhead controller
4786  * drivers may DMA directly into and out of the message buffers.
4787  *
4788  * This call should be used by drivers that require exclusive access to the
4789  * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4790  * be released by a spi_bus_unlock call when the exclusive access is over.
4791  *
4792  * Return: zero on success, else a negative error code.
4793  */
4794 int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
4795 {
4796 	return __spi_sync(spi, message);
4797 }
4798 EXPORT_SYMBOL_GPL(spi_sync_locked);
4799 
4800 /**
4801  * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4802  * @ctlr: SPI bus controller that should be locked for exclusive bus access
4803  * Context: can sleep
4804  *
4805  * This call may only be used from a context that may sleep.  The sleep
4806  * is non-interruptible, and has no timeout.
4807  *
4808  * This call should be used by drivers that require exclusive access to the
4809  * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4810  * exclusive access is over. Data transfer must be done by spi_sync_locked
4811  * and spi_async_locked calls when the SPI bus lock is held.
4812  *
4813  * Return: always zero.
4814  */
4815 int spi_bus_lock(struct spi_controller *ctlr)
4816 {
4817 	unsigned long flags;
4818 
4819 	mutex_lock(&ctlr->bus_lock_mutex);
4820 
4821 	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4822 	ctlr->bus_lock_flag = 1;
4823 	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4824 
4825 	/* Mutex remains locked until spi_bus_unlock() is called */
4826 
4827 	return 0;
4828 }
4829 EXPORT_SYMBOL_GPL(spi_bus_lock);
4830 
4831 /**
4832  * spi_bus_unlock - release the lock for exclusive SPI bus usage
4833  * @ctlr: SPI bus controller that was locked for exclusive bus access
4834  * Context: can sleep
4835  *
4836  * This call may only be used from a context that may sleep.  The sleep
4837  * is non-interruptible, and has no timeout.
4838  *
4839  * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4840  * call.
4841  *
4842  * Return: always zero.
4843  */
4844 int spi_bus_unlock(struct spi_controller *ctlr)
4845 {
4846 	ctlr->bus_lock_flag = 0;
4847 
4848 	mutex_unlock(&ctlr->bus_lock_mutex);
4849 
4850 	return 0;
4851 }
4852 EXPORT_SYMBOL_GPL(spi_bus_unlock);
4853 
4854 /* Portable code must never pass more than 32 bytes */
4855 #define	SPI_BUFSIZ	max(32, SMP_CACHE_BYTES)
4856 
4857 static u8	*buf;
4858 
4859 /**
4860  * spi_write_then_read - SPI synchronous write followed by read
4861  * @spi: device with which data will be exchanged
4862  * @txbuf: data to be written (need not be DMA-safe)
4863  * @n_tx: size of txbuf, in bytes
4864  * @rxbuf: buffer into which data will be read (need not be DMA-safe)
4865  * @n_rx: size of rxbuf, in bytes
4866  * Context: can sleep
4867  *
4868  * This performs a half duplex MicroWire style transaction with the
4869  * device, sending txbuf and then reading rxbuf.  The return value
4870  * is zero for success, else a negative errno status code.
4871  * This call may only be used from a context that may sleep.
4872  *
4873  * Parameters to this routine are always copied using a small buffer.
4874  * Performance-sensitive or bulk transfer code should instead use
4875  * spi_{async,sync}() calls with DMA-safe buffers.
4876  *
4877  * Return: zero on success, else a negative error code.
4878  */
4879 int spi_write_then_read(struct spi_device *spi,
4880 		const void *txbuf, unsigned n_tx,
4881 		void *rxbuf, unsigned n_rx)
4882 {
4883 	static DEFINE_MUTEX(lock);
4884 
4885 	int			status;
4886 	struct spi_message	message;
4887 	struct spi_transfer	x[2];
4888 	u8			*local_buf;
4889 
4890 	/*
4891 	 * Use preallocated DMA-safe buffer if we can. We can't avoid
4892 	 * copying here, (as a pure convenience thing), but we can
4893 	 * keep heap costs out of the hot path unless someone else is
4894 	 * using the pre-allocated buffer or the transfer is too large.
4895 	 */
4896 	if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
4897 		local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
4898 				    GFP_KERNEL | GFP_DMA);
4899 		if (!local_buf)
4900 			return -ENOMEM;
4901 	} else {
4902 		local_buf = buf;
4903 	}
4904 
4905 	spi_message_init(&message);
4906 	memset(x, 0, sizeof(x));
4907 	if (n_tx) {
4908 		x[0].len = n_tx;
4909 		spi_message_add_tail(&x[0], &message);
4910 	}
4911 	if (n_rx) {
4912 		x[1].len = n_rx;
4913 		spi_message_add_tail(&x[1], &message);
4914 	}
4915 
4916 	memcpy(local_buf, txbuf, n_tx);
4917 	x[0].tx_buf = local_buf;
4918 	x[1].rx_buf = local_buf + n_tx;
4919 
4920 	/* Do the I/O */
4921 	status = spi_sync(spi, &message);
4922 	if (status == 0)
4923 		memcpy(rxbuf, x[1].rx_buf, n_rx);
4924 
4925 	if (x[0].tx_buf == buf)
4926 		mutex_unlock(&lock);
4927 	else
4928 		kfree(local_buf);
4929 
4930 	return status;
4931 }
4932 EXPORT_SYMBOL_GPL(spi_write_then_read);
4933 
4934 /*-------------------------------------------------------------------------*/
4935 
4936 #if IS_ENABLED(CONFIG_OF)
4937 /* The spi controllers are not using spi_bus, so we find it with another way */
4938 struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
4939 {
4940 	struct device *dev;
4941 
4942 	dev = class_find_device_by_of_node(&spi_controller_class, node);
4943 	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4944 		dev = class_find_device_by_of_node(&spi_target_class, node);
4945 	if (!dev)
4946 		return NULL;
4947 
4948 	/* Reference got in class_find_device */
4949 	return container_of(dev, struct spi_controller, dev);
4950 }
4951 EXPORT_SYMBOL_GPL(of_find_spi_controller_by_node);
4952 #endif
4953 
4954 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
4955 /* Must call put_device() when done with returned spi_device device */
4956 static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
4957 {
4958 	struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
4959 
4960 	return dev ? to_spi_device(dev) : NULL;
4961 }
4962 
4963 static int of_spi_notify(struct notifier_block *nb, unsigned long action,
4964 			 void *arg)
4965 {
4966 	struct of_reconfig_data *rd = arg;
4967 	struct spi_controller *ctlr;
4968 	struct spi_device *spi;
4969 
4970 	switch (of_reconfig_get_state_change(action, arg)) {
4971 	case OF_RECONFIG_CHANGE_ADD:
4972 		ctlr = of_find_spi_controller_by_node(rd->dn->parent);
4973 		if (ctlr == NULL)
4974 			return NOTIFY_OK;	/* Not for us */
4975 
4976 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
4977 			put_device(&ctlr->dev);
4978 			return NOTIFY_OK;
4979 		}
4980 
4981 		spi = of_register_spi_device(ctlr, rd->dn);
4982 		put_device(&ctlr->dev);
4983 
4984 		if (IS_ERR(spi)) {
4985 			pr_err("%s: failed to create for '%pOF'\n",
4986 					__func__, rd->dn);
4987 			of_node_clear_flag(rd->dn, OF_POPULATED);
4988 			return notifier_from_errno(PTR_ERR(spi));
4989 		}
4990 		break;
4991 
4992 	case OF_RECONFIG_CHANGE_REMOVE:
4993 		/* Already depopulated? */
4994 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
4995 			return NOTIFY_OK;
4996 
4997 		/* Find our device by node */
4998 		spi = of_find_spi_device_by_node(rd->dn);
4999 		if (spi == NULL)
5000 			return NOTIFY_OK;	/* No? not meant for us */
5001 
5002 		/* Unregister takes one ref away */
5003 		spi_unregister_device(spi);
5004 
5005 		/* And put the reference of the find */
5006 		put_device(&spi->dev);
5007 		break;
5008 	}
5009 
5010 	return NOTIFY_OK;
5011 }
5012 
5013 static struct notifier_block spi_of_notifier = {
5014 	.notifier_call = of_spi_notify,
5015 };
5016 #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
5017 extern struct notifier_block spi_of_notifier;
5018 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
5019 
5020 #if IS_ENABLED(CONFIG_ACPI)
5021 static int spi_acpi_controller_match(struct device *dev, const void *data)
5022 {
5023 	return device_match_acpi_dev(dev->parent, data);
5024 }
5025 
5026 struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
5027 {
5028 	struct device *dev;
5029 
5030 	dev = class_find_device(&spi_controller_class, NULL, adev,
5031 				spi_acpi_controller_match);
5032 	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
5033 		dev = class_find_device(&spi_target_class, NULL, adev,
5034 					spi_acpi_controller_match);
5035 	if (!dev)
5036 		return NULL;
5037 
5038 	return container_of(dev, struct spi_controller, dev);
5039 }
5040 EXPORT_SYMBOL_GPL(acpi_spi_find_controller_by_adev);
5041 
5042 static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
5043 {
5044 	struct device *dev;
5045 
5046 	dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
5047 	return to_spi_device(dev);
5048 }
5049 
5050 static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
5051 			   void *arg)
5052 {
5053 	struct acpi_device *adev = arg;
5054 	struct spi_controller *ctlr;
5055 	struct spi_device *spi;
5056 
5057 	switch (value) {
5058 	case ACPI_RECONFIG_DEVICE_ADD:
5059 		ctlr = acpi_spi_find_controller_by_adev(acpi_dev_parent(adev));
5060 		if (!ctlr)
5061 			break;
5062 
5063 		acpi_register_spi_device(ctlr, adev);
5064 		put_device(&ctlr->dev);
5065 		break;
5066 	case ACPI_RECONFIG_DEVICE_REMOVE:
5067 		if (!acpi_device_enumerated(adev))
5068 			break;
5069 
5070 		spi = acpi_spi_find_device_by_adev(adev);
5071 		if (!spi)
5072 			break;
5073 
5074 		spi_unregister_device(spi);
5075 		put_device(&spi->dev);
5076 		break;
5077 	}
5078 
5079 	return NOTIFY_OK;
5080 }
5081 
5082 static struct notifier_block spi_acpi_notifier = {
5083 	.notifier_call = acpi_spi_notify,
5084 };
5085 #else
5086 extern struct notifier_block spi_acpi_notifier;
5087 #endif
5088 
5089 static int __init spi_init(void)
5090 {
5091 	int	status;
5092 
5093 	buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
5094 	if (!buf) {
5095 		status = -ENOMEM;
5096 		goto err0;
5097 	}
5098 
5099 	status = bus_register(&spi_bus_type);
5100 	if (status < 0)
5101 		goto err1;
5102 
5103 	status = class_register(&spi_controller_class);
5104 	if (status < 0)
5105 		goto err2;
5106 
5107 	if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
5108 		status = class_register(&spi_target_class);
5109 		if (status < 0)
5110 			goto err3;
5111 	}
5112 
5113 	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
5114 		WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
5115 	if (IS_ENABLED(CONFIG_ACPI))
5116 		WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
5117 
5118 	return 0;
5119 
5120 err3:
5121 	class_unregister(&spi_controller_class);
5122 err2:
5123 	bus_unregister(&spi_bus_type);
5124 err1:
5125 	kfree(buf);
5126 	buf = NULL;
5127 err0:
5128 	return status;
5129 }
5130 
5131 /*
5132  * A board_info is normally registered in arch_initcall(),
5133  * but even essential drivers wait till later.
5134  *
5135  * REVISIT only boardinfo really needs static linking. The rest (device and
5136  * driver registration) _could_ be dynamically linked (modular) ... Costs
5137  * include needing to have boardinfo data structures be much more public.
5138  */
5139 postcore_initcall(spi_init);
5140