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