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