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