1 // SPDX-License-Identifier: GPL-2.0-or-later
4 // Copyright (C) 2005 David Brownell
5 // Copyright (C) 2008 Secret Lab Technologies Ltd.
7 #include <linux/kernel.h>
8 #include <linux/device.h>
9 #include <linux/init.h>
10 #include <linux/cache.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/mutex.h>
14 #include <linux/of_device.h>
15 #include <linux/of_irq.h>
16 #include <linux/clk/clk-conf.h>
17 #include <linux/slab.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/spi/spi.h>
20 #include <linux/spi/spi-mem.h>
21 #include <linux/of_gpio.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/pm_domain.h>
25 #include <linux/property.h>
26 #include <linux/export.h>
27 #include <linux/sched/rt.h>
28 #include <uapi/linux/sched/types.h>
29 #include <linux/delay.h>
30 #include <linux/kthread.h>
31 #include <linux/ioport.h>
32 #include <linux/acpi.h>
33 #include <linux/highmem.h>
34 #include <linux/idr.h>
35 #include <linux/platform_data/x86/apple.h>
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/spi.h>
39 EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
40 EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
42 #include "internals.h"
44 static DEFINE_IDR(spi_master_idr);
46 static void spidev_release(struct device *dev)
48 struct spi_device *spi = to_spi_device(dev);
50 spi_controller_put(spi->controller);
51 kfree(spi->driver_override);
56 modalias_show(struct device *dev, struct device_attribute *a, char *buf)
58 const struct spi_device *spi = to_spi_device(dev);
61 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
65 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
67 static DEVICE_ATTR_RO(modalias);
69 static ssize_t driver_override_store(struct device *dev,
70 struct device_attribute *a,
71 const char *buf, size_t count)
73 struct spi_device *spi = to_spi_device(dev);
74 const char *end = memchr(buf, '\n', count);
75 const size_t len = end ? end - buf : count;
76 const char *driver_override, *old;
78 /* We need to keep extra room for a newline when displaying value */
79 if (len >= (PAGE_SIZE - 1))
82 driver_override = kstrndup(buf, len, GFP_KERNEL);
87 old = spi->driver_override;
89 spi->driver_override = driver_override;
91 /* Empty string, disable driver override */
92 spi->driver_override = NULL;
93 kfree(driver_override);
101 static ssize_t driver_override_show(struct device *dev,
102 struct device_attribute *a, char *buf)
104 const struct spi_device *spi = to_spi_device(dev);
108 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
112 static DEVICE_ATTR_RW(driver_override);
114 #define SPI_STATISTICS_ATTRS(field, file) \
115 static ssize_t spi_controller_##field##_show(struct device *dev, \
116 struct device_attribute *attr, \
119 struct spi_controller *ctlr = container_of(dev, \
120 struct spi_controller, dev); \
121 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
123 static struct device_attribute dev_attr_spi_controller_##field = { \
124 .attr = { .name = file, .mode = 0444 }, \
125 .show = spi_controller_##field##_show, \
127 static ssize_t spi_device_##field##_show(struct device *dev, \
128 struct device_attribute *attr, \
131 struct spi_device *spi = to_spi_device(dev); \
132 return spi_statistics_##field##_show(&spi->statistics, buf); \
134 static struct device_attribute dev_attr_spi_device_##field = { \
135 .attr = { .name = file, .mode = 0444 }, \
136 .show = spi_device_##field##_show, \
139 #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
140 static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
143 unsigned long flags; \
145 spin_lock_irqsave(&stat->lock, flags); \
146 len = sprintf(buf, format_string, stat->field); \
147 spin_unlock_irqrestore(&stat->lock, flags); \
150 SPI_STATISTICS_ATTRS(name, file)
152 #define SPI_STATISTICS_SHOW(field, format_string) \
153 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
154 field, format_string)
156 SPI_STATISTICS_SHOW(messages, "%lu");
157 SPI_STATISTICS_SHOW(transfers, "%lu");
158 SPI_STATISTICS_SHOW(errors, "%lu");
159 SPI_STATISTICS_SHOW(timedout, "%lu");
161 SPI_STATISTICS_SHOW(spi_sync, "%lu");
162 SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
163 SPI_STATISTICS_SHOW(spi_async, "%lu");
165 SPI_STATISTICS_SHOW(bytes, "%llu");
166 SPI_STATISTICS_SHOW(bytes_rx, "%llu");
167 SPI_STATISTICS_SHOW(bytes_tx, "%llu");
169 #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
170 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
171 "transfer_bytes_histo_" number, \
172 transfer_bytes_histo[index], "%lu")
173 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
174 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
175 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
176 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
177 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
178 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
179 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
180 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
181 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
182 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
183 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
184 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
185 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
186 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
187 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
188 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
189 SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
191 SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
193 static struct attribute *spi_dev_attrs[] = {
194 &dev_attr_modalias.attr,
195 &dev_attr_driver_override.attr,
199 static const struct attribute_group spi_dev_group = {
200 .attrs = spi_dev_attrs,
203 static struct attribute *spi_device_statistics_attrs[] = {
204 &dev_attr_spi_device_messages.attr,
205 &dev_attr_spi_device_transfers.attr,
206 &dev_attr_spi_device_errors.attr,
207 &dev_attr_spi_device_timedout.attr,
208 &dev_attr_spi_device_spi_sync.attr,
209 &dev_attr_spi_device_spi_sync_immediate.attr,
210 &dev_attr_spi_device_spi_async.attr,
211 &dev_attr_spi_device_bytes.attr,
212 &dev_attr_spi_device_bytes_rx.attr,
213 &dev_attr_spi_device_bytes_tx.attr,
214 &dev_attr_spi_device_transfer_bytes_histo0.attr,
215 &dev_attr_spi_device_transfer_bytes_histo1.attr,
216 &dev_attr_spi_device_transfer_bytes_histo2.attr,
217 &dev_attr_spi_device_transfer_bytes_histo3.attr,
218 &dev_attr_spi_device_transfer_bytes_histo4.attr,
219 &dev_attr_spi_device_transfer_bytes_histo5.attr,
220 &dev_attr_spi_device_transfer_bytes_histo6.attr,
221 &dev_attr_spi_device_transfer_bytes_histo7.attr,
222 &dev_attr_spi_device_transfer_bytes_histo8.attr,
223 &dev_attr_spi_device_transfer_bytes_histo9.attr,
224 &dev_attr_spi_device_transfer_bytes_histo10.attr,
225 &dev_attr_spi_device_transfer_bytes_histo11.attr,
226 &dev_attr_spi_device_transfer_bytes_histo12.attr,
227 &dev_attr_spi_device_transfer_bytes_histo13.attr,
228 &dev_attr_spi_device_transfer_bytes_histo14.attr,
229 &dev_attr_spi_device_transfer_bytes_histo15.attr,
230 &dev_attr_spi_device_transfer_bytes_histo16.attr,
231 &dev_attr_spi_device_transfers_split_maxsize.attr,
235 static const struct attribute_group spi_device_statistics_group = {
236 .name = "statistics",
237 .attrs = spi_device_statistics_attrs,
240 static const struct attribute_group *spi_dev_groups[] = {
242 &spi_device_statistics_group,
246 static struct attribute *spi_controller_statistics_attrs[] = {
247 &dev_attr_spi_controller_messages.attr,
248 &dev_attr_spi_controller_transfers.attr,
249 &dev_attr_spi_controller_errors.attr,
250 &dev_attr_spi_controller_timedout.attr,
251 &dev_attr_spi_controller_spi_sync.attr,
252 &dev_attr_spi_controller_spi_sync_immediate.attr,
253 &dev_attr_spi_controller_spi_async.attr,
254 &dev_attr_spi_controller_bytes.attr,
255 &dev_attr_spi_controller_bytes_rx.attr,
256 &dev_attr_spi_controller_bytes_tx.attr,
257 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
258 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
259 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
260 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
261 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
262 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
263 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
264 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
265 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
266 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
267 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
268 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
269 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
270 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
271 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
272 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
273 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
274 &dev_attr_spi_controller_transfers_split_maxsize.attr,
278 static const struct attribute_group spi_controller_statistics_group = {
279 .name = "statistics",
280 .attrs = spi_controller_statistics_attrs,
283 static const struct attribute_group *spi_master_groups[] = {
284 &spi_controller_statistics_group,
288 static void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
289 struct spi_transfer *xfer,
290 struct spi_controller *ctlr)
293 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
298 spin_lock_irqsave(&stats->lock, flags);
301 stats->transfer_bytes_histo[l2len]++;
303 stats->bytes += xfer->len;
304 if ((xfer->tx_buf) &&
305 (xfer->tx_buf != ctlr->dummy_tx))
306 stats->bytes_tx += xfer->len;
307 if ((xfer->rx_buf) &&
308 (xfer->rx_buf != ctlr->dummy_rx))
309 stats->bytes_rx += xfer->len;
311 spin_unlock_irqrestore(&stats->lock, flags);
314 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
315 * and the sysfs version makes coldplug work too.
318 static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
319 const struct spi_device *sdev)
321 while (id->name[0]) {
322 if (!strcmp(sdev->modalias, id->name))
329 const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
331 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
333 return spi_match_id(sdrv->id_table, sdev);
335 EXPORT_SYMBOL_GPL(spi_get_device_id);
337 static int spi_match_device(struct device *dev, struct device_driver *drv)
339 const struct spi_device *spi = to_spi_device(dev);
340 const struct spi_driver *sdrv = to_spi_driver(drv);
342 /* Check override first, and if set, only use the named driver */
343 if (spi->driver_override)
344 return strcmp(spi->driver_override, drv->name) == 0;
346 /* Attempt an OF style match */
347 if (of_driver_match_device(dev, drv))
351 if (acpi_driver_match_device(dev, drv))
355 return !!spi_match_id(sdrv->id_table, spi);
357 return strcmp(spi->modalias, drv->name) == 0;
360 static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
362 const struct spi_device *spi = to_spi_device(dev);
365 rc = acpi_device_uevent_modalias(dev, env);
369 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
372 static int spi_probe(struct device *dev)
374 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
375 struct spi_device *spi = to_spi_device(dev);
378 ret = of_clk_set_defaults(dev->of_node, false);
383 spi->irq = of_irq_get(dev->of_node, 0);
384 if (spi->irq == -EPROBE_DEFER)
385 return -EPROBE_DEFER;
390 ret = dev_pm_domain_attach(dev, true);
395 ret = sdrv->probe(spi);
397 dev_pm_domain_detach(dev, true);
403 static void spi_remove(struct device *dev)
405 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
410 ret = sdrv->remove(to_spi_device(dev));
413 "Failed to unbind driver (%pe), ignoring\n",
417 dev_pm_domain_detach(dev, true);
420 static void spi_shutdown(struct device *dev)
423 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
426 sdrv->shutdown(to_spi_device(dev));
430 struct bus_type spi_bus_type = {
432 .dev_groups = spi_dev_groups,
433 .match = spi_match_device,
434 .uevent = spi_uevent,
436 .remove = spi_remove,
437 .shutdown = spi_shutdown,
439 EXPORT_SYMBOL_GPL(spi_bus_type);
442 * __spi_register_driver - register a SPI driver
443 * @owner: owner module of the driver to register
444 * @sdrv: the driver to register
447 * Return: zero on success, else a negative error code.
449 int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
451 sdrv->driver.owner = owner;
452 sdrv->driver.bus = &spi_bus_type;
455 * For Really Good Reasons we use spi: modaliases not of:
456 * modaliases for DT so module autoloading won't work if we
457 * don't have a spi_device_id as well as a compatible string.
459 if (sdrv->driver.of_match_table) {
460 const struct of_device_id *of_id;
462 for (of_id = sdrv->driver.of_match_table; of_id->compatible[0];
466 /* Strip off any vendor prefix */
467 of_name = strnchr(of_id->compatible,
468 sizeof(of_id->compatible), ',');
472 of_name = of_id->compatible;
474 if (sdrv->id_table) {
475 const struct spi_device_id *spi_id;
477 for (spi_id = sdrv->id_table; spi_id->name[0];
479 if (strcmp(spi_id->name, of_name) == 0)
485 if (strcmp(sdrv->driver.name, of_name) == 0)
489 pr_warn("SPI driver %s has no spi_device_id for %s\n",
490 sdrv->driver.name, of_id->compatible);
494 return driver_register(&sdrv->driver);
496 EXPORT_SYMBOL_GPL(__spi_register_driver);
498 /*-------------------------------------------------------------------------*/
500 /* SPI devices should normally not be created by SPI device drivers; that
501 * would make them board-specific. Similarly with SPI controller drivers.
502 * Device registration normally goes into like arch/.../mach.../board-YYY.c
503 * with other readonly (flashable) information about mainboard devices.
507 struct list_head list;
508 struct spi_board_info board_info;
511 static LIST_HEAD(board_list);
512 static LIST_HEAD(spi_controller_list);
515 * Used to protect add/del operation for board_info list and
516 * spi_controller list, and their matching process
517 * also used to protect object of type struct idr
519 static DEFINE_MUTEX(board_lock);
522 * spi_alloc_device - Allocate a new SPI device
523 * @ctlr: Controller to which device is connected
526 * Allows a driver to allocate and initialize a spi_device without
527 * registering it immediately. This allows a driver to directly
528 * fill the spi_device with device parameters before calling
529 * spi_add_device() on it.
531 * Caller is responsible to call spi_add_device() on the returned
532 * spi_device structure to add it to the SPI controller. If the caller
533 * needs to discard the spi_device without adding it, then it should
534 * call spi_dev_put() on it.
536 * Return: a pointer to the new device, or NULL.
538 static struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
540 struct spi_device *spi;
542 if (!spi_controller_get(ctlr))
545 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
547 spi_controller_put(ctlr);
551 spi->master = spi->controller = ctlr;
552 spi->dev.parent = &ctlr->dev;
553 spi->dev.bus = &spi_bus_type;
554 spi->dev.release = spidev_release;
555 spi->cs_gpio = -ENOENT;
556 spi->mode = ctlr->buswidth_override_bits;
558 spin_lock_init(&spi->statistics.lock);
560 device_initialize(&spi->dev);
564 static void spi_dev_set_name(struct spi_device *spi)
566 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
569 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
573 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
577 static int spi_dev_check(struct device *dev, void *data)
579 struct spi_device *spi = to_spi_device(dev);
580 struct spi_device *new_spi = data;
582 if (spi->controller == new_spi->controller &&
583 spi->chip_select == new_spi->chip_select)
588 static void spi_cleanup(struct spi_device *spi)
590 if (spi->controller->cleanup)
591 spi->controller->cleanup(spi);
594 static int __spi_add_device(struct spi_device *spi)
596 struct spi_controller *ctlr = spi->controller;
597 struct device *dev = ctlr->dev.parent;
601 * We need to make sure there's no other device with this
602 * chipselect **BEFORE** we call setup(), else we'll trash
605 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
607 dev_err(dev, "chipselect %d already in use\n",
612 /* Controller may unregister concurrently */
613 if (IS_ENABLED(CONFIG_SPI_DYNAMIC) &&
614 !device_is_registered(&ctlr->dev)) {
618 /* Descriptors take precedence */
620 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
621 else if (ctlr->cs_gpios)
622 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
624 /* Drivers may modify this initial i/o setup, but will
625 * normally rely on the device being setup. Devices
626 * using SPI_CS_HIGH can't coexist well otherwise...
628 status = spi_setup(spi);
630 dev_err(dev, "can't setup %s, status %d\n",
631 dev_name(&spi->dev), status);
635 /* Device may be bound to an active driver when this returns */
636 status = device_add(&spi->dev);
638 dev_err(dev, "can't add %s, status %d\n",
639 dev_name(&spi->dev), status);
642 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
649 * spi_add_device - Add spi_device allocated with spi_alloc_device
650 * @spi: spi_device to register
652 * Companion function to spi_alloc_device. Devices allocated with
653 * spi_alloc_device can be added onto the spi bus with this function.
655 * Return: 0 on success; negative errno on failure
657 static int spi_add_device(struct spi_device *spi)
659 struct spi_controller *ctlr = spi->controller;
660 struct device *dev = ctlr->dev.parent;
663 /* Chipselects are numbered 0..max; validate. */
664 if (spi->chip_select >= ctlr->num_chipselect) {
665 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
666 ctlr->num_chipselect);
670 /* Set the bus ID string */
671 spi_dev_set_name(spi);
673 mutex_lock(&ctlr->add_lock);
674 status = __spi_add_device(spi);
675 mutex_unlock(&ctlr->add_lock);
679 static int spi_add_device_locked(struct spi_device *spi)
681 struct spi_controller *ctlr = spi->controller;
682 struct device *dev = ctlr->dev.parent;
684 /* Chipselects are numbered 0..max; validate. */
685 if (spi->chip_select >= ctlr->num_chipselect) {
686 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
687 ctlr->num_chipselect);
691 /* Set the bus ID string */
692 spi_dev_set_name(spi);
694 WARN_ON(!mutex_is_locked(&ctlr->add_lock));
695 return __spi_add_device(spi);
699 * spi_new_device - instantiate one new SPI device
700 * @ctlr: Controller to which device is connected
701 * @chip: Describes the SPI device
704 * On typical mainboards, this is purely internal; and it's not needed
705 * after board init creates the hard-wired devices. Some development
706 * platforms may not be able to use spi_register_board_info though, and
707 * this is exported so that for example a USB or parport based adapter
708 * driver could add devices (which it would learn about out-of-band).
710 * Return: the new device, or NULL.
712 struct spi_device *spi_new_device(struct spi_controller *ctlr,
713 struct spi_board_info *chip)
715 struct spi_device *proxy;
718 /* NOTE: caller did any chip->bus_num checks necessary.
720 * Also, unless we change the return value convention to use
721 * error-or-pointer (not NULL-or-pointer), troubleshootability
722 * suggests syslogged diagnostics are best here (ugh).
725 proxy = spi_alloc_device(ctlr);
729 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
731 proxy->chip_select = chip->chip_select;
732 proxy->max_speed_hz = chip->max_speed_hz;
733 proxy->mode = chip->mode;
734 proxy->irq = chip->irq;
735 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
736 proxy->dev.platform_data = (void *) chip->platform_data;
737 proxy->controller_data = chip->controller_data;
738 proxy->controller_state = NULL;
741 status = device_add_software_node(&proxy->dev, chip->swnode);
743 dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n",
744 chip->modalias, status);
749 status = spi_add_device(proxy);
756 device_remove_software_node(&proxy->dev);
760 EXPORT_SYMBOL_GPL(spi_new_device);
763 * spi_unregister_device - unregister a single SPI device
764 * @spi: spi_device to unregister
766 * Start making the passed SPI device vanish. Normally this would be handled
767 * by spi_unregister_controller().
769 void spi_unregister_device(struct spi_device *spi)
774 if (spi->dev.of_node) {
775 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
776 of_node_put(spi->dev.of_node);
778 if (ACPI_COMPANION(&spi->dev))
779 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
780 device_remove_software_node(&spi->dev);
781 device_del(&spi->dev);
783 put_device(&spi->dev);
785 EXPORT_SYMBOL_GPL(spi_unregister_device);
787 static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
788 struct spi_board_info *bi)
790 struct spi_device *dev;
792 if (ctlr->bus_num != bi->bus_num)
795 dev = spi_new_device(ctlr, bi);
797 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
802 * spi_register_board_info - register SPI devices for a given board
803 * @info: array of chip descriptors
804 * @n: how many descriptors are provided
807 * Board-specific early init code calls this (probably during arch_initcall)
808 * with segments of the SPI device table. Any device nodes are created later,
809 * after the relevant parent SPI controller (bus_num) is defined. We keep
810 * this table of devices forever, so that reloading a controller driver will
811 * not make Linux forget about these hard-wired devices.
813 * Other code can also call this, e.g. a particular add-on board might provide
814 * SPI devices through its expansion connector, so code initializing that board
815 * would naturally declare its SPI devices.
817 * The board info passed can safely be __initdata ... but be careful of
818 * any embedded pointers (platform_data, etc), they're copied as-is.
820 * Return: zero on success, else a negative error code.
822 int spi_register_board_info(struct spi_board_info const *info, unsigned n)
824 struct boardinfo *bi;
830 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
834 for (i = 0; i < n; i++, bi++, info++) {
835 struct spi_controller *ctlr;
837 memcpy(&bi->board_info, info, sizeof(*info));
839 mutex_lock(&board_lock);
840 list_add_tail(&bi->list, &board_list);
841 list_for_each_entry(ctlr, &spi_controller_list, list)
842 spi_match_controller_to_boardinfo(ctlr,
844 mutex_unlock(&board_lock);
850 /*-------------------------------------------------------------------------*/
852 /* Core methods for SPI resource management */
855 * spi_res_alloc - allocate a spi resource that is life-cycle managed
856 * during the processing of a spi_message while using
858 * @spi: the spi device for which we allocate memory
859 * @release: the release code to execute for this resource
860 * @size: size to alloc and return
861 * @gfp: GFP allocation flags
863 * Return: the pointer to the allocated data
865 * This may get enhanced in the future to allocate from a memory pool
866 * of the @spi_device or @spi_controller to avoid repeated allocations.
868 static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release,
869 size_t size, gfp_t gfp)
871 struct spi_res *sres;
873 sres = kzalloc(sizeof(*sres) + size, gfp);
877 INIT_LIST_HEAD(&sres->entry);
878 sres->release = release;
884 * spi_res_free - free an spi resource
885 * @res: pointer to the custom data of a resource
888 static void spi_res_free(void *res)
890 struct spi_res *sres = container_of(res, struct spi_res, data);
895 WARN_ON(!list_empty(&sres->entry));
900 * spi_res_add - add a spi_res to the spi_message
901 * @message: the spi message
902 * @res: the spi_resource
904 static void spi_res_add(struct spi_message *message, void *res)
906 struct spi_res *sres = container_of(res, struct spi_res, data);
908 WARN_ON(!list_empty(&sres->entry));
909 list_add_tail(&sres->entry, &message->resources);
913 * spi_res_release - release all spi resources for this message
914 * @ctlr: the @spi_controller
915 * @message: the @spi_message
917 static void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
919 struct spi_res *res, *tmp;
921 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
923 res->release(ctlr, message, res->data);
925 list_del(&res->entry);
931 /*-------------------------------------------------------------------------*/
933 static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
935 bool activate = enable;
938 * Avoid calling into the driver (or doing delays) if the chip select
939 * isn't actually changing from the last time this was called.
941 if (!force && (spi->controller->last_cs_enable == enable) &&
942 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
945 trace_spi_set_cs(spi, activate);
947 spi->controller->last_cs_enable = enable;
948 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
950 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio) ||
951 !spi->controller->set_cs_timing) {
953 spi_delay_exec(&spi->cs_setup, NULL);
955 spi_delay_exec(&spi->cs_hold, NULL);
958 if (spi->mode & SPI_CS_HIGH)
961 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
962 if (!(spi->mode & SPI_NO_CS)) {
965 * Historically ACPI has no means of the GPIO polarity and
966 * thus the SPISerialBus() resource defines it on the per-chip
967 * basis. In order to avoid a chain of negations, the GPIO
968 * polarity is considered being Active High. Even for the cases
969 * when _DSD() is involved (in the updated versions of ACPI)
970 * the GPIO CS polarity must be defined Active High to avoid
971 * ambiguity. That's why we use enable, that takes SPI_CS_HIGH
974 if (has_acpi_companion(&spi->dev))
975 gpiod_set_value_cansleep(spi->cs_gpiod, !enable);
977 /* Polarity handled by GPIO library */
978 gpiod_set_value_cansleep(spi->cs_gpiod, activate);
981 * invert the enable line, as active low is
984 gpio_set_value_cansleep(spi->cs_gpio, !enable);
987 /* Some SPI masters need both GPIO CS & slave_select */
988 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
989 spi->controller->set_cs)
990 spi->controller->set_cs(spi, !enable);
991 } else if (spi->controller->set_cs) {
992 spi->controller->set_cs(spi, !enable);
995 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio) ||
996 !spi->controller->set_cs_timing) {
998 spi_delay_exec(&spi->cs_inactive, NULL);
1002 #ifdef CONFIG_HAS_DMA
1003 int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
1004 struct sg_table *sgt, void *buf, size_t len,
1005 enum dma_data_direction dir)
1007 const bool vmalloced_buf = is_vmalloc_addr(buf);
1008 unsigned int max_seg_size = dma_get_max_seg_size(dev);
1009 #ifdef CONFIG_HIGHMEM
1010 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
1011 (unsigned long)buf < (PKMAP_BASE +
1012 (LAST_PKMAP * PAGE_SIZE)));
1014 const bool kmap_buf = false;
1018 struct page *vm_page;
1019 struct scatterlist *sg;
1024 if (vmalloced_buf || kmap_buf) {
1025 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
1026 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
1027 } else if (virt_addr_valid(buf)) {
1028 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
1029 sgs = DIV_ROUND_UP(len, desc_len);
1034 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
1039 for (i = 0; i < sgs; i++) {
1041 if (vmalloced_buf || kmap_buf) {
1043 * Next scatterlist entry size is the minimum between
1044 * the desc_len and the remaining buffer length that
1047 min = min_t(size_t, desc_len,
1049 PAGE_SIZE - offset_in_page(buf)));
1051 vm_page = vmalloc_to_page(buf);
1053 vm_page = kmap_to_page(buf);
1058 sg_set_page(sg, vm_page,
1059 min, offset_in_page(buf));
1061 min = min_t(size_t, len, desc_len);
1063 sg_set_buf(sg, sg_buf, min);
1071 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
1084 void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
1085 struct sg_table *sgt, enum dma_data_direction dir)
1087 if (sgt->orig_nents) {
1088 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
1093 static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
1095 struct device *tx_dev, *rx_dev;
1096 struct spi_transfer *xfer;
1103 tx_dev = ctlr->dma_tx->device->dev;
1104 else if (ctlr->dma_map_dev)
1105 tx_dev = ctlr->dma_map_dev;
1107 tx_dev = ctlr->dev.parent;
1110 rx_dev = ctlr->dma_rx->device->dev;
1111 else if (ctlr->dma_map_dev)
1112 rx_dev = ctlr->dma_map_dev;
1114 rx_dev = ctlr->dev.parent;
1116 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1117 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
1120 if (xfer->tx_buf != NULL) {
1121 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
1122 (void *)xfer->tx_buf, xfer->len,
1128 if (xfer->rx_buf != NULL) {
1129 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
1130 xfer->rx_buf, xfer->len,
1133 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
1140 ctlr->cur_msg_mapped = true;
1145 static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
1147 struct spi_transfer *xfer;
1148 struct device *tx_dev, *rx_dev;
1150 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
1154 tx_dev = ctlr->dma_tx->device->dev;
1156 tx_dev = ctlr->dev.parent;
1159 rx_dev = ctlr->dma_rx->device->dev;
1161 rx_dev = ctlr->dev.parent;
1163 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1164 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
1167 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1168 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
1171 ctlr->cur_msg_mapped = false;
1175 #else /* !CONFIG_HAS_DMA */
1176 static inline int __spi_map_msg(struct spi_controller *ctlr,
1177 struct spi_message *msg)
1182 static inline int __spi_unmap_msg(struct spi_controller *ctlr,
1183 struct spi_message *msg)
1187 #endif /* !CONFIG_HAS_DMA */
1189 static inline int spi_unmap_msg(struct spi_controller *ctlr,
1190 struct spi_message *msg)
1192 struct spi_transfer *xfer;
1194 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1196 * Restore the original value of tx_buf or rx_buf if they are
1199 if (xfer->tx_buf == ctlr->dummy_tx)
1200 xfer->tx_buf = NULL;
1201 if (xfer->rx_buf == ctlr->dummy_rx)
1202 xfer->rx_buf = NULL;
1205 return __spi_unmap_msg(ctlr, msg);
1208 static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
1210 struct spi_transfer *xfer;
1212 unsigned int max_tx, max_rx;
1214 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX))
1215 && !(msg->spi->mode & SPI_3WIRE)) {
1219 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1220 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
1222 max_tx = max(xfer->len, max_tx);
1223 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
1225 max_rx = max(xfer->len, max_rx);
1229 tmp = krealloc(ctlr->dummy_tx, max_tx,
1230 GFP_KERNEL | GFP_DMA);
1233 ctlr->dummy_tx = tmp;
1234 memset(tmp, 0, max_tx);
1238 tmp = krealloc(ctlr->dummy_rx, max_rx,
1239 GFP_KERNEL | GFP_DMA);
1242 ctlr->dummy_rx = tmp;
1245 if (max_tx || max_rx) {
1246 list_for_each_entry(xfer, &msg->transfers,
1251 xfer->tx_buf = ctlr->dummy_tx;
1253 xfer->rx_buf = ctlr->dummy_rx;
1258 return __spi_map_msg(ctlr, msg);
1261 static int spi_transfer_wait(struct spi_controller *ctlr,
1262 struct spi_message *msg,
1263 struct spi_transfer *xfer)
1265 struct spi_statistics *statm = &ctlr->statistics;
1266 struct spi_statistics *stats = &msg->spi->statistics;
1267 u32 speed_hz = xfer->speed_hz;
1268 unsigned long long ms;
1270 if (spi_controller_is_slave(ctlr)) {
1271 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1272 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1280 * For each byte we wait for 8 cycles of the SPI clock.
1281 * Since speed is defined in Hz and we want milliseconds,
1282 * use respective multiplier, but before the division,
1283 * otherwise we may get 0 for short transfers.
1285 ms = 8LL * MSEC_PER_SEC * xfer->len;
1286 do_div(ms, speed_hz);
1289 * Increase it twice and add 200 ms tolerance, use
1290 * predefined maximum in case of overflow.
1296 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1297 msecs_to_jiffies(ms));
1300 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1301 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1302 dev_err(&msg->spi->dev,
1303 "SPI transfer timed out\n");
1311 static void _spi_transfer_delay_ns(u32 ns)
1315 if (ns <= NSEC_PER_USEC) {
1318 u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
1323 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1327 int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
1329 u32 delay = _delay->value;
1330 u32 unit = _delay->unit;
1337 case SPI_DELAY_UNIT_USECS:
1338 delay *= NSEC_PER_USEC;
1340 case SPI_DELAY_UNIT_NSECS:
1341 /* Nothing to do here */
1343 case SPI_DELAY_UNIT_SCK:
1344 /* clock cycles need to be obtained from spi_transfer */
1348 * If there is unknown effective speed, approximate it
1349 * by underestimating with half of the requested hz.
1351 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1355 /* Convert delay to nanoseconds */
1356 delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz);
1364 EXPORT_SYMBOL_GPL(spi_delay_to_ns);
1366 int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1375 delay = spi_delay_to_ns(_delay, xfer);
1379 _spi_transfer_delay_ns(delay);
1383 EXPORT_SYMBOL_GPL(spi_delay_exec);
1385 static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1386 struct spi_transfer *xfer)
1388 u32 default_delay_ns = 10 * NSEC_PER_USEC;
1389 u32 delay = xfer->cs_change_delay.value;
1390 u32 unit = xfer->cs_change_delay.unit;
1393 /* return early on "fast" mode - for everything but USECS */
1395 if (unit == SPI_DELAY_UNIT_USECS)
1396 _spi_transfer_delay_ns(default_delay_ns);
1400 ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1402 dev_err_once(&msg->spi->dev,
1403 "Use of unsupported delay unit %i, using default of %luus\n",
1404 unit, default_delay_ns / NSEC_PER_USEC);
1405 _spi_transfer_delay_ns(default_delay_ns);
1410 * spi_transfer_one_message - Default implementation of transfer_one_message()
1412 * This is a standard implementation of transfer_one_message() for
1413 * drivers which implement a transfer_one() operation. It provides
1414 * standard handling of delays and chip select management.
1416 static int spi_transfer_one_message(struct spi_controller *ctlr,
1417 struct spi_message *msg)
1419 struct spi_transfer *xfer;
1420 bool keep_cs = false;
1422 struct spi_statistics *statm = &ctlr->statistics;
1423 struct spi_statistics *stats = &msg->spi->statistics;
1425 spi_set_cs(msg->spi, true, false);
1427 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1428 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1430 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1431 trace_spi_transfer_start(msg, xfer);
1433 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1434 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
1436 if (!ctlr->ptp_sts_supported) {
1437 xfer->ptp_sts_word_pre = 0;
1438 ptp_read_system_prets(xfer->ptp_sts);
1441 if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
1442 reinit_completion(&ctlr->xfer_completion);
1445 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1447 if (ctlr->cur_msg_mapped &&
1448 (xfer->error & SPI_TRANS_FAIL_NO_START)) {
1449 __spi_unmap_msg(ctlr, msg);
1450 ctlr->fallback = true;
1451 xfer->error &= ~SPI_TRANS_FAIL_NO_START;
1455 SPI_STATISTICS_INCREMENT_FIELD(statm,
1457 SPI_STATISTICS_INCREMENT_FIELD(stats,
1459 dev_err(&msg->spi->dev,
1460 "SPI transfer failed: %d\n", ret);
1465 ret = spi_transfer_wait(ctlr, msg, xfer);
1471 dev_err(&msg->spi->dev,
1472 "Bufferless transfer has length %u\n",
1476 if (!ctlr->ptp_sts_supported) {
1477 ptp_read_system_postts(xfer->ptp_sts);
1478 xfer->ptp_sts_word_post = xfer->len;
1481 trace_spi_transfer_stop(msg, xfer);
1483 if (msg->status != -EINPROGRESS)
1486 spi_transfer_delay_exec(xfer);
1488 if (xfer->cs_change) {
1489 if (list_is_last(&xfer->transfer_list,
1493 spi_set_cs(msg->spi, false, false);
1494 _spi_transfer_cs_change_delay(msg, xfer);
1495 spi_set_cs(msg->spi, true, false);
1499 msg->actual_length += xfer->len;
1503 if (ret != 0 || !keep_cs)
1504 spi_set_cs(msg->spi, false, false);
1506 if (msg->status == -EINPROGRESS)
1509 if (msg->status && ctlr->handle_err)
1510 ctlr->handle_err(ctlr, msg);
1512 spi_finalize_current_message(ctlr);
1518 * spi_finalize_current_transfer - report completion of a transfer
1519 * @ctlr: the controller reporting completion
1521 * Called by SPI drivers using the core transfer_one_message()
1522 * implementation to notify it that the current interrupt driven
1523 * transfer has finished and the next one may be scheduled.
1525 void spi_finalize_current_transfer(struct spi_controller *ctlr)
1527 complete(&ctlr->xfer_completion);
1529 EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1531 static void spi_idle_runtime_pm(struct spi_controller *ctlr)
1533 if (ctlr->auto_runtime_pm) {
1534 pm_runtime_mark_last_busy(ctlr->dev.parent);
1535 pm_runtime_put_autosuspend(ctlr->dev.parent);
1540 * __spi_pump_messages - function which processes spi message queue
1541 * @ctlr: controller to process queue for
1542 * @in_kthread: true if we are in the context of the message pump thread
1544 * This function checks if there is any spi message in the queue that
1545 * needs processing and if so call out to the driver to initialize hardware
1546 * and transfer each message.
1548 * Note that it is called both from the kthread itself and also from
1549 * inside spi_sync(); the queue extraction handling at the top of the
1550 * function should deal with this safely.
1552 static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
1554 struct spi_transfer *xfer;
1555 struct spi_message *msg;
1556 bool was_busy = false;
1557 unsigned long flags;
1561 spin_lock_irqsave(&ctlr->queue_lock, flags);
1563 /* Make sure we are not already running a message */
1564 if (ctlr->cur_msg) {
1565 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1569 /* If another context is idling the device then defer */
1571 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
1572 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1576 /* Check if the queue is idle */
1577 if (list_empty(&ctlr->queue) || !ctlr->running) {
1579 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1583 /* Defer any non-atomic teardown to the thread */
1585 if (!ctlr->dummy_rx && !ctlr->dummy_tx &&
1586 !ctlr->unprepare_transfer_hardware) {
1587 spi_idle_runtime_pm(ctlr);
1589 trace_spi_controller_idle(ctlr);
1591 kthread_queue_work(ctlr->kworker,
1592 &ctlr->pump_messages);
1594 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1599 ctlr->idling = true;
1600 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1602 kfree(ctlr->dummy_rx);
1603 ctlr->dummy_rx = NULL;
1604 kfree(ctlr->dummy_tx);
1605 ctlr->dummy_tx = NULL;
1606 if (ctlr->unprepare_transfer_hardware &&
1607 ctlr->unprepare_transfer_hardware(ctlr))
1609 "failed to unprepare transfer hardware\n");
1610 spi_idle_runtime_pm(ctlr);
1611 trace_spi_controller_idle(ctlr);
1613 spin_lock_irqsave(&ctlr->queue_lock, flags);
1614 ctlr->idling = false;
1615 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1619 /* Extract head of queue */
1620 msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1621 ctlr->cur_msg = msg;
1623 list_del_init(&msg->queue);
1628 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1630 mutex_lock(&ctlr->io_mutex);
1632 if (!was_busy && ctlr->auto_runtime_pm) {
1633 ret = pm_runtime_get_sync(ctlr->dev.parent);
1635 pm_runtime_put_noidle(ctlr->dev.parent);
1636 dev_err(&ctlr->dev, "Failed to power device: %d\n",
1638 mutex_unlock(&ctlr->io_mutex);
1644 trace_spi_controller_busy(ctlr);
1646 if (!was_busy && ctlr->prepare_transfer_hardware) {
1647 ret = ctlr->prepare_transfer_hardware(ctlr);
1650 "failed to prepare transfer hardware: %d\n",
1653 if (ctlr->auto_runtime_pm)
1654 pm_runtime_put(ctlr->dev.parent);
1657 spi_finalize_current_message(ctlr);
1659 mutex_unlock(&ctlr->io_mutex);
1664 trace_spi_message_start(msg);
1666 if (ctlr->prepare_message) {
1667 ret = ctlr->prepare_message(ctlr, msg);
1669 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1672 spi_finalize_current_message(ctlr);
1675 ctlr->cur_msg_prepared = true;
1678 ret = spi_map_msg(ctlr, msg);
1681 spi_finalize_current_message(ctlr);
1685 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1686 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1687 xfer->ptp_sts_word_pre = 0;
1688 ptp_read_system_prets(xfer->ptp_sts);
1692 ret = ctlr->transfer_one_message(ctlr, msg);
1695 "failed to transfer one message from queue\n");
1700 mutex_unlock(&ctlr->io_mutex);
1702 /* Prod the scheduler in case transfer_one() was busy waiting */
1708 * spi_pump_messages - kthread work function which processes spi message queue
1709 * @work: pointer to kthread work struct contained in the controller struct
1711 static void spi_pump_messages(struct kthread_work *work)
1713 struct spi_controller *ctlr =
1714 container_of(work, struct spi_controller, pump_messages);
1716 __spi_pump_messages(ctlr, true);
1720 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1721 * TX timestamp for the requested byte from the SPI
1722 * transfer. The frequency with which this function
1723 * must be called (once per word, once for the whole
1724 * transfer, once per batch of words etc) is arbitrary
1725 * as long as the @tx buffer offset is greater than or
1726 * equal to the requested byte at the time of the
1727 * call. The timestamp is only taken once, at the
1728 * first such call. It is assumed that the driver
1729 * advances its @tx buffer pointer monotonically.
1730 * @ctlr: Pointer to the spi_controller structure of the driver
1731 * @xfer: Pointer to the transfer being timestamped
1732 * @progress: How many words (not bytes) have been transferred so far
1733 * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1734 * transfer, for less jitter in time measurement. Only compatible
1735 * with PIO drivers. If true, must follow up with
1736 * spi_take_timestamp_post or otherwise system will crash.
1737 * WARNING: for fully predictable results, the CPU frequency must
1738 * also be under control (governor).
1740 void spi_take_timestamp_pre(struct spi_controller *ctlr,
1741 struct spi_transfer *xfer,
1742 size_t progress, bool irqs_off)
1747 if (xfer->timestamped)
1750 if (progress > xfer->ptp_sts_word_pre)
1753 /* Capture the resolution of the timestamp */
1754 xfer->ptp_sts_word_pre = progress;
1757 local_irq_save(ctlr->irq_flags);
1761 ptp_read_system_prets(xfer->ptp_sts);
1763 EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1766 * spi_take_timestamp_post - helper for drivers to collect the end of the
1767 * TX timestamp for the requested byte from the SPI
1768 * transfer. Can be called with an arbitrary
1769 * frequency: only the first call where @tx exceeds
1770 * or is equal to the requested word will be
1772 * @ctlr: Pointer to the spi_controller structure of the driver
1773 * @xfer: Pointer to the transfer being timestamped
1774 * @progress: How many words (not bytes) have been transferred so far
1775 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1777 void spi_take_timestamp_post(struct spi_controller *ctlr,
1778 struct spi_transfer *xfer,
1779 size_t progress, bool irqs_off)
1784 if (xfer->timestamped)
1787 if (progress < xfer->ptp_sts_word_post)
1790 ptp_read_system_postts(xfer->ptp_sts);
1793 local_irq_restore(ctlr->irq_flags);
1797 /* Capture the resolution of the timestamp */
1798 xfer->ptp_sts_word_post = progress;
1800 xfer->timestamped = true;
1802 EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
1805 * spi_set_thread_rt - set the controller to pump at realtime priority
1806 * @ctlr: controller to boost priority of
1808 * This can be called because the controller requested realtime priority
1809 * (by setting the ->rt value before calling spi_register_controller()) or
1810 * because a device on the bus said that its transfers needed realtime
1813 * NOTE: at the moment if any device on a bus says it needs realtime then
1814 * the thread will be at realtime priority for all transfers on that
1815 * controller. If this eventually becomes a problem we may see if we can
1816 * find a way to boost the priority only temporarily during relevant
1819 static void spi_set_thread_rt(struct spi_controller *ctlr)
1821 dev_info(&ctlr->dev,
1822 "will run message pump with realtime priority\n");
1823 sched_set_fifo(ctlr->kworker->task);
1826 static int spi_init_queue(struct spi_controller *ctlr)
1828 ctlr->running = false;
1831 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev));
1832 if (IS_ERR(ctlr->kworker)) {
1833 dev_err(&ctlr->dev, "failed to create message pump kworker\n");
1834 return PTR_ERR(ctlr->kworker);
1837 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
1840 * Controller config will indicate if this controller should run the
1841 * message pump with high (realtime) priority to reduce the transfer
1842 * latency on the bus by minimising the delay between a transfer
1843 * request and the scheduling of the message pump thread. Without this
1844 * setting the message pump thread will remain at default priority.
1847 spi_set_thread_rt(ctlr);
1853 * spi_get_next_queued_message() - called by driver to check for queued
1855 * @ctlr: the controller to check for queued messages
1857 * If there are more messages in the queue, the next message is returned from
1860 * Return: the next message in the queue, else NULL if the queue is empty.
1862 struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
1864 struct spi_message *next;
1865 unsigned long flags;
1867 /* get a pointer to the next message, if any */
1868 spin_lock_irqsave(&ctlr->queue_lock, flags);
1869 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
1871 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1875 EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1878 * spi_finalize_current_message() - the current message is complete
1879 * @ctlr: the controller to return the message to
1881 * Called by the driver to notify the core that the message in the front of the
1882 * queue is complete and can be removed from the queue.
1884 void spi_finalize_current_message(struct spi_controller *ctlr)
1886 struct spi_transfer *xfer;
1887 struct spi_message *mesg;
1888 unsigned long flags;
1891 spin_lock_irqsave(&ctlr->queue_lock, flags);
1892 mesg = ctlr->cur_msg;
1893 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1895 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1896 list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
1897 ptp_read_system_postts(xfer->ptp_sts);
1898 xfer->ptp_sts_word_post = xfer->len;
1902 if (unlikely(ctlr->ptp_sts_supported))
1903 list_for_each_entry(xfer, &mesg->transfers, transfer_list)
1904 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped);
1906 spi_unmap_msg(ctlr, mesg);
1908 /* In the prepare_messages callback the spi bus has the opportunity to
1909 * split a transfer to smaller chunks.
1910 * Release splited transfers here since spi_map_msg is done on the
1911 * splited transfers.
1913 spi_res_release(ctlr, mesg);
1915 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1916 ret = ctlr->unprepare_message(ctlr, mesg);
1918 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1923 spin_lock_irqsave(&ctlr->queue_lock, flags);
1924 ctlr->cur_msg = NULL;
1925 ctlr->cur_msg_prepared = false;
1926 ctlr->fallback = false;
1927 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
1928 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1930 trace_spi_message_done(mesg);
1934 mesg->complete(mesg->context);
1936 EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1938 static int spi_start_queue(struct spi_controller *ctlr)
1940 unsigned long flags;
1942 spin_lock_irqsave(&ctlr->queue_lock, flags);
1944 if (ctlr->running || ctlr->busy) {
1945 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1949 ctlr->running = true;
1950 ctlr->cur_msg = NULL;
1951 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1953 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
1958 static int spi_stop_queue(struct spi_controller *ctlr)
1960 unsigned long flags;
1961 unsigned limit = 500;
1964 spin_lock_irqsave(&ctlr->queue_lock, flags);
1967 * This is a bit lame, but is optimized for the common execution path.
1968 * A wait_queue on the ctlr->busy could be used, but then the common
1969 * execution path (pump_messages) would be required to call wake_up or
1970 * friends on every SPI message. Do this instead.
1972 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1973 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1974 usleep_range(10000, 11000);
1975 spin_lock_irqsave(&ctlr->queue_lock, flags);
1978 if (!list_empty(&ctlr->queue) || ctlr->busy)
1981 ctlr->running = false;
1983 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1986 dev_warn(&ctlr->dev, "could not stop message queue\n");
1992 static int spi_destroy_queue(struct spi_controller *ctlr)
1996 ret = spi_stop_queue(ctlr);
1999 * kthread_flush_worker will block until all work is done.
2000 * If the reason that stop_queue timed out is that the work will never
2001 * finish, then it does no good to call flush/stop thread, so
2005 dev_err(&ctlr->dev, "problem destroying queue\n");
2009 kthread_destroy_worker(ctlr->kworker);
2014 static int __spi_queued_transfer(struct spi_device *spi,
2015 struct spi_message *msg,
2018 struct spi_controller *ctlr = spi->controller;
2019 unsigned long flags;
2021 spin_lock_irqsave(&ctlr->queue_lock, flags);
2023 if (!ctlr->running) {
2024 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2027 msg->actual_length = 0;
2028 msg->status = -EINPROGRESS;
2030 list_add_tail(&msg->queue, &ctlr->queue);
2031 if (!ctlr->busy && need_pump)
2032 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2034 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2039 * spi_queued_transfer - transfer function for queued transfers
2040 * @spi: spi device which is requesting transfer
2041 * @msg: spi message which is to handled is queued to driver queue
2043 * Return: zero on success, else a negative error code.
2045 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
2047 return __spi_queued_transfer(spi, msg, true);
2050 static int spi_controller_initialize_queue(struct spi_controller *ctlr)
2054 ctlr->transfer = spi_queued_transfer;
2055 if (!ctlr->transfer_one_message)
2056 ctlr->transfer_one_message = spi_transfer_one_message;
2058 /* Initialize and start queue */
2059 ret = spi_init_queue(ctlr);
2061 dev_err(&ctlr->dev, "problem initializing queue\n");
2062 goto err_init_queue;
2064 ctlr->queued = true;
2065 ret = spi_start_queue(ctlr);
2067 dev_err(&ctlr->dev, "problem starting queue\n");
2068 goto err_start_queue;
2074 spi_destroy_queue(ctlr);
2080 * spi_flush_queue - Send all pending messages in the queue from the callers'
2082 * @ctlr: controller to process queue for
2084 * This should be used when one wants to ensure all pending messages have been
2085 * sent before doing something. Is used by the spi-mem code to make sure SPI
2086 * memory operations do not preempt regular SPI transfers that have been queued
2087 * before the spi-mem operation.
2089 void spi_flush_queue(struct spi_controller *ctlr)
2091 if (ctlr->transfer == spi_queued_transfer)
2092 __spi_pump_messages(ctlr, false);
2095 /*-------------------------------------------------------------------------*/
2097 #if defined(CONFIG_OF)
2098 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
2099 struct device_node *nc)
2104 /* Mode (clock phase/polarity/etc.) */
2105 if (of_property_read_bool(nc, "spi-cpha"))
2106 spi->mode |= SPI_CPHA;
2107 if (of_property_read_bool(nc, "spi-cpol"))
2108 spi->mode |= SPI_CPOL;
2109 if (of_property_read_bool(nc, "spi-3wire"))
2110 spi->mode |= SPI_3WIRE;
2111 if (of_property_read_bool(nc, "spi-lsb-first"))
2112 spi->mode |= SPI_LSB_FIRST;
2113 if (of_property_read_bool(nc, "spi-cs-high"))
2114 spi->mode |= SPI_CS_HIGH;
2116 /* Device DUAL/QUAD mode */
2117 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
2120 spi->mode |= SPI_NO_TX;
2125 spi->mode |= SPI_TX_DUAL;
2128 spi->mode |= SPI_TX_QUAD;
2131 spi->mode |= SPI_TX_OCTAL;
2134 dev_warn(&ctlr->dev,
2135 "spi-tx-bus-width %d not supported\n",
2141 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
2144 spi->mode |= SPI_NO_RX;
2149 spi->mode |= SPI_RX_DUAL;
2152 spi->mode |= SPI_RX_QUAD;
2155 spi->mode |= SPI_RX_OCTAL;
2158 dev_warn(&ctlr->dev,
2159 "spi-rx-bus-width %d not supported\n",
2165 if (spi_controller_is_slave(ctlr)) {
2166 if (!of_node_name_eq(nc, "slave")) {
2167 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
2174 /* Device address */
2175 rc = of_property_read_u32(nc, "reg", &value);
2177 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
2181 spi->chip_select = value;
2184 if (!of_property_read_u32(nc, "spi-max-frequency", &value))
2185 spi->max_speed_hz = value;
2190 static struct spi_device *
2191 of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
2193 struct spi_device *spi;
2196 /* Alloc an spi_device */
2197 spi = spi_alloc_device(ctlr);
2199 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
2204 /* Select device driver */
2205 rc = of_modalias_node(nc, spi->modalias,
2206 sizeof(spi->modalias));
2208 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
2212 rc = of_spi_parse_dt(ctlr, spi, nc);
2216 /* Store a pointer to the node in the device structure */
2218 spi->dev.of_node = nc;
2219 spi->dev.fwnode = of_fwnode_handle(nc);
2221 /* Register the new device */
2222 rc = spi_add_device(spi);
2224 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
2225 goto err_of_node_put;
2238 * of_register_spi_devices() - Register child devices onto the SPI bus
2239 * @ctlr: Pointer to spi_controller device
2241 * Registers an spi_device for each child node of controller node which
2242 * represents a valid SPI slave.
2244 static void of_register_spi_devices(struct spi_controller *ctlr)
2246 struct spi_device *spi;
2247 struct device_node *nc;
2249 if (!ctlr->dev.of_node)
2252 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
2253 if (of_node_test_and_set_flag(nc, OF_POPULATED))
2255 spi = of_register_spi_device(ctlr, nc);
2257 dev_warn(&ctlr->dev,
2258 "Failed to create SPI device for %pOF\n", nc);
2259 of_node_clear_flag(nc, OF_POPULATED);
2264 static void of_register_spi_devices(struct spi_controller *ctlr) { }
2268 * spi_new_ancillary_device() - Register ancillary SPI device
2269 * @spi: Pointer to the main SPI device registering the ancillary device
2270 * @chip_select: Chip Select of the ancillary device
2272 * Register an ancillary SPI device; for example some chips have a chip-select
2273 * for normal device usage and another one for setup/firmware upload.
2275 * This may only be called from main SPI device's probe routine.
2277 * Return: 0 on success; negative errno on failure
2279 struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
2282 struct spi_device *ancillary;
2285 /* Alloc an spi_device */
2286 ancillary = spi_alloc_device(spi->controller);
2292 strlcpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias));
2294 /* Use provided chip-select for ancillary device */
2295 ancillary->chip_select = chip_select;
2297 /* Take over SPI mode/speed from SPI main device */
2298 ancillary->max_speed_hz = spi->max_speed_hz;
2299 ancillary->mode = spi->mode;
2301 /* Register the new device */
2302 rc = spi_add_device_locked(ancillary);
2304 dev_err(&spi->dev, "failed to register ancillary device\n");
2311 spi_dev_put(ancillary);
2314 EXPORT_SYMBOL_GPL(spi_new_ancillary_device);
2317 struct acpi_spi_lookup {
2318 struct spi_controller *ctlr;
2326 static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
2327 struct acpi_spi_lookup *lookup)
2329 const union acpi_object *obj;
2331 if (!x86_apple_machine)
2334 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
2335 && obj->buffer.length >= 4)
2336 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
2338 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
2339 && obj->buffer.length == 8)
2340 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
2342 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
2343 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
2344 lookup->mode |= SPI_LSB_FIRST;
2346 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
2347 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
2348 lookup->mode |= SPI_CPOL;
2350 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
2351 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
2352 lookup->mode |= SPI_CPHA;
2355 static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
2357 struct acpi_spi_lookup *lookup = data;
2358 struct spi_controller *ctlr = lookup->ctlr;
2360 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
2361 struct acpi_resource_spi_serialbus *sb;
2362 acpi_handle parent_handle;
2365 sb = &ares->data.spi_serial_bus;
2366 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
2368 status = acpi_get_handle(NULL,
2369 sb->resource_source.string_ptr,
2372 if (ACPI_FAILURE(status) ||
2373 ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
2377 * ACPI DeviceSelection numbering is handled by the
2378 * host controller driver in Windows and can vary
2379 * from driver to driver. In Linux we always expect
2380 * 0 .. max - 1 so we need to ask the driver to
2381 * translate between the two schemes.
2383 if (ctlr->fw_translate_cs) {
2384 int cs = ctlr->fw_translate_cs(ctlr,
2385 sb->device_selection);
2388 lookup->chip_select = cs;
2390 lookup->chip_select = sb->device_selection;
2393 lookup->max_speed_hz = sb->connection_speed;
2394 lookup->bits_per_word = sb->data_bit_length;
2396 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
2397 lookup->mode |= SPI_CPHA;
2398 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
2399 lookup->mode |= SPI_CPOL;
2400 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
2401 lookup->mode |= SPI_CS_HIGH;
2403 } else if (lookup->irq < 0) {
2406 if (acpi_dev_resource_interrupt(ares, 0, &r))
2407 lookup->irq = r.start;
2410 /* Always tell the ACPI core to skip this resource */
2414 static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
2415 struct acpi_device *adev)
2417 acpi_handle parent_handle = NULL;
2418 struct list_head resource_list;
2419 struct acpi_spi_lookup lookup = {};
2420 struct spi_device *spi;
2423 if (acpi_bus_get_status(adev) || !adev->status.present ||
2424 acpi_device_enumerated(adev))
2430 INIT_LIST_HEAD(&resource_list);
2431 ret = acpi_dev_get_resources(adev, &resource_list,
2432 acpi_spi_add_resource, &lookup);
2433 acpi_dev_free_resource_list(&resource_list);
2436 /* found SPI in _CRS but it points to another controller */
2439 if (!lookup.max_speed_hz &&
2440 ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) &&
2441 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
2442 /* Apple does not use _CRS but nested devices for SPI slaves */
2443 acpi_spi_parse_apple_properties(adev, &lookup);
2446 if (!lookup.max_speed_hz)
2449 spi = spi_alloc_device(ctlr);
2451 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
2452 dev_name(&adev->dev));
2453 return AE_NO_MEMORY;
2457 ACPI_COMPANION_SET(&spi->dev, adev);
2458 spi->max_speed_hz = lookup.max_speed_hz;
2459 spi->mode |= lookup.mode;
2460 spi->irq = lookup.irq;
2461 spi->bits_per_word = lookup.bits_per_word;
2462 spi->chip_select = lookup.chip_select;
2464 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
2465 sizeof(spi->modalias));
2468 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
2470 acpi_device_set_enumerated(adev);
2472 adev->power.flags.ignore_parent = true;
2473 if (spi_add_device(spi)) {
2474 adev->power.flags.ignore_parent = false;
2475 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
2476 dev_name(&adev->dev));
2483 static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
2484 void *data, void **return_value)
2486 struct spi_controller *ctlr = data;
2487 struct acpi_device *adev;
2489 if (acpi_bus_get_device(handle, &adev))
2492 return acpi_register_spi_device(ctlr, adev);
2495 #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32
2497 static void acpi_register_spi_devices(struct spi_controller *ctlr)
2502 handle = ACPI_HANDLE(ctlr->dev.parent);
2506 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2507 SPI_ACPI_ENUMERATE_MAX_DEPTH,
2508 acpi_spi_add_device, NULL, ctlr, NULL);
2509 if (ACPI_FAILURE(status))
2510 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
2513 static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
2514 #endif /* CONFIG_ACPI */
2516 static void spi_controller_release(struct device *dev)
2518 struct spi_controller *ctlr;
2520 ctlr = container_of(dev, struct spi_controller, dev);
2524 static struct class spi_master_class = {
2525 .name = "spi_master",
2526 .owner = THIS_MODULE,
2527 .dev_release = spi_controller_release,
2528 .dev_groups = spi_master_groups,
2531 #ifdef CONFIG_SPI_SLAVE
2533 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2535 * @spi: device used for the current transfer
2537 int spi_slave_abort(struct spi_device *spi)
2539 struct spi_controller *ctlr = spi->controller;
2541 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
2542 return ctlr->slave_abort(ctlr);
2546 EXPORT_SYMBOL_GPL(spi_slave_abort);
2548 static int match_true(struct device *dev, void *data)
2553 static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2556 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2558 struct device *child;
2560 child = device_find_child(&ctlr->dev, NULL, match_true);
2561 return sprintf(buf, "%s\n",
2562 child ? to_spi_device(child)->modalias : NULL);
2565 static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2566 const char *buf, size_t count)
2568 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2570 struct spi_device *spi;
2571 struct device *child;
2575 rc = sscanf(buf, "%31s", name);
2576 if (rc != 1 || !name[0])
2579 child = device_find_child(&ctlr->dev, NULL, match_true);
2581 /* Remove registered slave */
2582 device_unregister(child);
2586 if (strcmp(name, "(null)")) {
2587 /* Register new slave */
2588 spi = spi_alloc_device(ctlr);
2592 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2594 rc = spi_add_device(spi);
2604 static DEVICE_ATTR_RW(slave);
2606 static struct attribute *spi_slave_attrs[] = {
2607 &dev_attr_slave.attr,
2611 static const struct attribute_group spi_slave_group = {
2612 .attrs = spi_slave_attrs,
2615 static const struct attribute_group *spi_slave_groups[] = {
2616 &spi_controller_statistics_group,
2621 static struct class spi_slave_class = {
2622 .name = "spi_slave",
2623 .owner = THIS_MODULE,
2624 .dev_release = spi_controller_release,
2625 .dev_groups = spi_slave_groups,
2628 extern struct class spi_slave_class; /* dummy */
2632 * __spi_alloc_controller - allocate an SPI master or slave controller
2633 * @dev: the controller, possibly using the platform_bus
2634 * @size: how much zeroed driver-private data to allocate; the pointer to this
2635 * memory is in the driver_data field of the returned device, accessible
2636 * with spi_controller_get_devdata(); the memory is cacheline aligned;
2637 * drivers granting DMA access to portions of their private data need to
2638 * round up @size using ALIGN(size, dma_get_cache_alignment()).
2639 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2640 * slave (true) controller
2641 * Context: can sleep
2643 * This call is used only by SPI controller drivers, which are the
2644 * only ones directly touching chip registers. It's how they allocate
2645 * an spi_controller structure, prior to calling spi_register_controller().
2647 * This must be called from context that can sleep.
2649 * The caller is responsible for assigning the bus number and initializing the
2650 * controller's methods before calling spi_register_controller(); and (after
2651 * errors adding the device) calling spi_controller_put() to prevent a memory
2654 * Return: the SPI controller structure on success, else NULL.
2656 struct spi_controller *__spi_alloc_controller(struct device *dev,
2657 unsigned int size, bool slave)
2659 struct spi_controller *ctlr;
2660 size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
2665 ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
2669 device_initialize(&ctlr->dev);
2670 INIT_LIST_HEAD(&ctlr->queue);
2671 spin_lock_init(&ctlr->queue_lock);
2672 spin_lock_init(&ctlr->bus_lock_spinlock);
2673 mutex_init(&ctlr->bus_lock_mutex);
2674 mutex_init(&ctlr->io_mutex);
2675 mutex_init(&ctlr->add_lock);
2677 ctlr->num_chipselect = 1;
2678 ctlr->slave = slave;
2679 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
2680 ctlr->dev.class = &spi_slave_class;
2682 ctlr->dev.class = &spi_master_class;
2683 ctlr->dev.parent = dev;
2684 pm_suspend_ignore_children(&ctlr->dev, true);
2685 spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
2689 EXPORT_SYMBOL_GPL(__spi_alloc_controller);
2691 static void devm_spi_release_controller(struct device *dev, void *ctlr)
2693 spi_controller_put(*(struct spi_controller **)ctlr);
2697 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
2698 * @dev: physical device of SPI controller
2699 * @size: how much zeroed driver-private data to allocate
2700 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
2701 * Context: can sleep
2703 * Allocate an SPI controller and automatically release a reference on it
2704 * when @dev is unbound from its driver. Drivers are thus relieved from
2705 * having to call spi_controller_put().
2707 * The arguments to this function are identical to __spi_alloc_controller().
2709 * Return: the SPI controller structure on success, else NULL.
2711 struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
2715 struct spi_controller **ptr, *ctlr;
2717 ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr),
2722 ctlr = __spi_alloc_controller(dev, size, slave);
2724 ctlr->devm_allocated = true;
2726 devres_add(dev, ptr);
2733 EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
2736 static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
2739 struct device_node *np = ctlr->dev.of_node;
2744 nb = of_gpio_named_count(np, "cs-gpios");
2745 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2747 /* Return error only for an incorrectly formed cs-gpios property */
2748 if (nb == 0 || nb == -ENOENT)
2753 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
2755 ctlr->cs_gpios = cs;
2757 if (!ctlr->cs_gpios)
2760 for (i = 0; i < ctlr->num_chipselect; i++)
2763 for (i = 0; i < nb; i++)
2764 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2769 static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
2776 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2777 * @ctlr: The SPI master to grab GPIO descriptors for
2779 static int spi_get_gpio_descs(struct spi_controller *ctlr)
2782 struct gpio_desc **cs;
2783 struct device *dev = &ctlr->dev;
2784 unsigned long native_cs_mask = 0;
2785 unsigned int num_cs_gpios = 0;
2787 nb = gpiod_count(dev, "cs");
2789 /* No GPIOs at all is fine, else return the error */
2795 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2797 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2801 ctlr->cs_gpiods = cs;
2803 for (i = 0; i < nb; i++) {
2805 * Most chipselects are active low, the inverted
2806 * semantics are handled by special quirks in gpiolib,
2807 * so initializing them GPIOD_OUT_LOW here means
2808 * "unasserted", in most cases this will drive the physical
2811 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2814 return PTR_ERR(cs[i]);
2818 * If we find a CS GPIO, name it after the device and
2823 gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2827 gpiod_set_consumer_name(cs[i], gpioname);
2832 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
2833 dev_err(dev, "Invalid native chip select %d\n", i);
2836 native_cs_mask |= BIT(i);
2839 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
2841 if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios &&
2842 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
2843 dev_err(dev, "No unused native chip select available\n");
2850 static int spi_controller_check_ops(struct spi_controller *ctlr)
2853 * The controller may implement only the high-level SPI-memory like
2854 * operations if it does not support regular SPI transfers, and this is
2856 * If ->mem_ops is NULL, we request that at least one of the
2857 * ->transfer_xxx() method be implemented.
2859 if (ctlr->mem_ops) {
2860 if (!ctlr->mem_ops->exec_op)
2862 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2863 !ctlr->transfer_one_message) {
2871 * spi_register_controller - register SPI master or slave controller
2872 * @ctlr: initialized master, originally from spi_alloc_master() or
2874 * Context: can sleep
2876 * SPI controllers connect to their drivers using some non-SPI bus,
2877 * such as the platform bus. The final stage of probe() in that code
2878 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2880 * SPI controllers use board specific (often SOC specific) bus numbers,
2881 * and board-specific addressing for SPI devices combines those numbers
2882 * with chip select numbers. Since SPI does not directly support dynamic
2883 * device identification, boards need configuration tables telling which
2884 * chip is at which address.
2886 * This must be called from context that can sleep. It returns zero on
2887 * success, else a negative error code (dropping the controller's refcount).
2888 * After a successful return, the caller is responsible for calling
2889 * spi_unregister_controller().
2891 * Return: zero on success, else a negative error code.
2893 int spi_register_controller(struct spi_controller *ctlr)
2895 struct device *dev = ctlr->dev.parent;
2896 struct boardinfo *bi;
2898 int id, first_dynamic;
2904 * Make sure all necessary hooks are implemented before registering
2905 * the SPI controller.
2907 status = spi_controller_check_ops(ctlr);
2911 if (ctlr->bus_num >= 0) {
2912 /* devices with a fixed bus num must check-in with the num */
2913 mutex_lock(&board_lock);
2914 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2915 ctlr->bus_num + 1, GFP_KERNEL);
2916 mutex_unlock(&board_lock);
2917 if (WARN(id < 0, "couldn't get idr"))
2918 return id == -ENOSPC ? -EBUSY : id;
2920 } else if (ctlr->dev.of_node) {
2921 /* allocate dynamic bus number using Linux idr */
2922 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2925 mutex_lock(&board_lock);
2926 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2927 ctlr->bus_num + 1, GFP_KERNEL);
2928 mutex_unlock(&board_lock);
2929 if (WARN(id < 0, "couldn't get idr"))
2930 return id == -ENOSPC ? -EBUSY : id;
2933 if (ctlr->bus_num < 0) {
2934 first_dynamic = of_alias_get_highest_id("spi");
2935 if (first_dynamic < 0)
2940 mutex_lock(&board_lock);
2941 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2943 mutex_unlock(&board_lock);
2944 if (WARN(id < 0, "couldn't get idr"))
2948 ctlr->bus_lock_flag = 0;
2949 init_completion(&ctlr->xfer_completion);
2950 if (!ctlr->max_dma_len)
2951 ctlr->max_dma_len = INT_MAX;
2953 /* register the device, then userspace will see it.
2954 * registration fails if the bus ID is in use.
2956 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2958 if (!spi_controller_is_slave(ctlr)) {
2959 if (ctlr->use_gpio_descriptors) {
2960 status = spi_get_gpio_descs(ctlr);
2964 * A controller using GPIO descriptors always
2965 * supports SPI_CS_HIGH if need be.
2967 ctlr->mode_bits |= SPI_CS_HIGH;
2969 /* Legacy code path for GPIOs from DT */
2970 status = of_spi_get_gpio_numbers(ctlr);
2977 * Even if it's just one always-selected device, there must
2978 * be at least one chipselect.
2980 if (!ctlr->num_chipselect) {
2985 status = device_add(&ctlr->dev);
2988 dev_dbg(dev, "registered %s %s\n",
2989 spi_controller_is_slave(ctlr) ? "slave" : "master",
2990 dev_name(&ctlr->dev));
2993 * If we're using a queued driver, start the queue. Note that we don't
2994 * need the queueing logic if the driver is only supporting high-level
2995 * memory operations.
2997 if (ctlr->transfer) {
2998 dev_info(dev, "controller is unqueued, this is deprecated\n");
2999 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
3000 status = spi_controller_initialize_queue(ctlr);
3002 device_del(&ctlr->dev);
3006 /* add statistics */
3007 spin_lock_init(&ctlr->statistics.lock);
3009 mutex_lock(&board_lock);
3010 list_add_tail(&ctlr->list, &spi_controller_list);
3011 list_for_each_entry(bi, &board_list, list)
3012 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
3013 mutex_unlock(&board_lock);
3015 /* Register devices from the device tree and ACPI */
3016 of_register_spi_devices(ctlr);
3017 acpi_register_spi_devices(ctlr);
3021 mutex_lock(&board_lock);
3022 idr_remove(&spi_master_idr, ctlr->bus_num);
3023 mutex_unlock(&board_lock);
3026 EXPORT_SYMBOL_GPL(spi_register_controller);
3028 static void devm_spi_unregister(void *ctlr)
3030 spi_unregister_controller(ctlr);
3034 * devm_spi_register_controller - register managed SPI master or slave
3036 * @dev: device managing SPI controller
3037 * @ctlr: initialized controller, originally from spi_alloc_master() or
3039 * Context: can sleep
3041 * Register a SPI device as with spi_register_controller() which will
3042 * automatically be unregistered and freed.
3044 * Return: zero on success, else a negative error code.
3046 int devm_spi_register_controller(struct device *dev,
3047 struct spi_controller *ctlr)
3051 ret = spi_register_controller(ctlr);
3055 return devm_add_action_or_reset(dev, devm_spi_unregister, ctlr);
3057 EXPORT_SYMBOL_GPL(devm_spi_register_controller);
3059 static int __unregister(struct device *dev, void *null)
3061 spi_unregister_device(to_spi_device(dev));
3066 * spi_unregister_controller - unregister SPI master or slave controller
3067 * @ctlr: the controller being unregistered
3068 * Context: can sleep
3070 * This call is used only by SPI controller drivers, which are the
3071 * only ones directly touching chip registers.
3073 * This must be called from context that can sleep.
3075 * Note that this function also drops a reference to the controller.
3077 void spi_unregister_controller(struct spi_controller *ctlr)
3079 struct spi_controller *found;
3080 int id = ctlr->bus_num;
3082 /* Prevent addition of new devices, unregister existing ones */
3083 if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
3084 mutex_lock(&ctlr->add_lock);
3086 device_for_each_child(&ctlr->dev, NULL, __unregister);
3088 /* First make sure that this controller was ever added */
3089 mutex_lock(&board_lock);
3090 found = idr_find(&spi_master_idr, id);
3091 mutex_unlock(&board_lock);
3093 if (spi_destroy_queue(ctlr))
3094 dev_err(&ctlr->dev, "queue remove failed\n");
3096 mutex_lock(&board_lock);
3097 list_del(&ctlr->list);
3098 mutex_unlock(&board_lock);
3100 device_del(&ctlr->dev);
3102 /* Release the last reference on the controller if its driver
3103 * has not yet been converted to devm_spi_alloc_master/slave().
3105 if (!ctlr->devm_allocated)
3106 put_device(&ctlr->dev);
3109 mutex_lock(&board_lock);
3111 idr_remove(&spi_master_idr, id);
3112 mutex_unlock(&board_lock);
3114 if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
3115 mutex_unlock(&ctlr->add_lock);
3117 EXPORT_SYMBOL_GPL(spi_unregister_controller);
3119 int spi_controller_suspend(struct spi_controller *ctlr)
3123 /* Basically no-ops for non-queued controllers */
3127 ret = spi_stop_queue(ctlr);
3129 dev_err(&ctlr->dev, "queue stop failed\n");
3133 EXPORT_SYMBOL_GPL(spi_controller_suspend);
3135 int spi_controller_resume(struct spi_controller *ctlr)
3142 ret = spi_start_queue(ctlr);
3144 dev_err(&ctlr->dev, "queue restart failed\n");
3148 EXPORT_SYMBOL_GPL(spi_controller_resume);
3150 /*-------------------------------------------------------------------------*/
3152 /* Core methods for spi_message alterations */
3154 static void __spi_replace_transfers_release(struct spi_controller *ctlr,
3155 struct spi_message *msg,
3158 struct spi_replaced_transfers *rxfer = res;
3161 /* call extra callback if requested */
3163 rxfer->release(ctlr, msg, res);
3165 /* insert replaced transfers back into the message */
3166 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
3168 /* remove the formerly inserted entries */
3169 for (i = 0; i < rxfer->inserted; i++)
3170 list_del(&rxfer->inserted_transfers[i].transfer_list);
3174 * spi_replace_transfers - replace transfers with several transfers
3175 * and register change with spi_message.resources
3176 * @msg: the spi_message we work upon
3177 * @xfer_first: the first spi_transfer we want to replace
3178 * @remove: number of transfers to remove
3179 * @insert: the number of transfers we want to insert instead
3180 * @release: extra release code necessary in some circumstances
3181 * @extradatasize: extra data to allocate (with alignment guarantees
3182 * of struct @spi_transfer)
3185 * Returns: pointer to @spi_replaced_transfers,
3186 * PTR_ERR(...) in case of errors.
3188 static struct spi_replaced_transfers *spi_replace_transfers(
3189 struct spi_message *msg,
3190 struct spi_transfer *xfer_first,
3193 spi_replaced_release_t release,
3194 size_t extradatasize,
3197 struct spi_replaced_transfers *rxfer;
3198 struct spi_transfer *xfer;
3201 /* allocate the structure using spi_res */
3202 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
3203 struct_size(rxfer, inserted_transfers, insert)
3207 return ERR_PTR(-ENOMEM);
3209 /* the release code to invoke before running the generic release */
3210 rxfer->release = release;
3212 /* assign extradata */
3215 &rxfer->inserted_transfers[insert];
3217 /* init the replaced_transfers list */
3218 INIT_LIST_HEAD(&rxfer->replaced_transfers);
3220 /* assign the list_entry after which we should reinsert
3221 * the @replaced_transfers - it may be spi_message.messages!
3223 rxfer->replaced_after = xfer_first->transfer_list.prev;
3225 /* remove the requested number of transfers */
3226 for (i = 0; i < remove; i++) {
3227 /* if the entry after replaced_after it is msg->transfers
3228 * then we have been requested to remove more transfers
3229 * than are in the list
3231 if (rxfer->replaced_after->next == &msg->transfers) {
3232 dev_err(&msg->spi->dev,
3233 "requested to remove more spi_transfers than are available\n");
3234 /* insert replaced transfers back into the message */
3235 list_splice(&rxfer->replaced_transfers,
3236 rxfer->replaced_after);
3238 /* free the spi_replace_transfer structure */
3239 spi_res_free(rxfer);
3241 /* and return with an error */
3242 return ERR_PTR(-EINVAL);
3245 /* remove the entry after replaced_after from list of
3246 * transfers and add it to list of replaced_transfers
3248 list_move_tail(rxfer->replaced_after->next,
3249 &rxfer->replaced_transfers);
3252 /* create copy of the given xfer with identical settings
3253 * based on the first transfer to get removed
3255 for (i = 0; i < insert; i++) {
3256 /* we need to run in reverse order */
3257 xfer = &rxfer->inserted_transfers[insert - 1 - i];
3259 /* copy all spi_transfer data */
3260 memcpy(xfer, xfer_first, sizeof(*xfer));
3263 list_add(&xfer->transfer_list, rxfer->replaced_after);
3265 /* clear cs_change and delay for all but the last */
3267 xfer->cs_change = false;
3268 xfer->delay.value = 0;
3272 /* set up inserted */
3273 rxfer->inserted = insert;
3275 /* and register it with spi_res/spi_message */
3276 spi_res_add(msg, rxfer);
3281 static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
3282 struct spi_message *msg,
3283 struct spi_transfer **xferp,
3287 struct spi_transfer *xfer = *xferp, *xfers;
3288 struct spi_replaced_transfers *srt;
3292 /* calculate how many we have to replace */
3293 count = DIV_ROUND_UP(xfer->len, maxsize);
3295 /* create replacement */
3296 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
3298 return PTR_ERR(srt);
3299 xfers = srt->inserted_transfers;
3301 /* now handle each of those newly inserted spi_transfers
3302 * note that the replacements spi_transfers all are preset
3303 * to the same values as *xferp, so tx_buf, rx_buf and len
3304 * are all identical (as well as most others)
3305 * so we just have to fix up len and the pointers.
3307 * this also includes support for the depreciated
3308 * spi_message.is_dma_mapped interface
3311 /* the first transfer just needs the length modified, so we
3312 * run it outside the loop
3314 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
3316 /* all the others need rx_buf/tx_buf also set */
3317 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3318 /* update rx_buf, tx_buf and dma */
3319 if (xfers[i].rx_buf)
3320 xfers[i].rx_buf += offset;
3321 if (xfers[i].rx_dma)
3322 xfers[i].rx_dma += offset;
3323 if (xfers[i].tx_buf)
3324 xfers[i].tx_buf += offset;
3325 if (xfers[i].tx_dma)
3326 xfers[i].tx_dma += offset;
3329 xfers[i].len = min(maxsize, xfers[i].len - offset);
3332 /* we set up xferp to the last entry we have inserted,
3333 * so that we skip those already split transfers
3335 *xferp = &xfers[count - 1];
3337 /* increment statistics counters */
3338 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
3339 transfers_split_maxsize);
3340 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
3341 transfers_split_maxsize);
3347 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3348 * when an individual transfer exceeds a
3350 * @ctlr: the @spi_controller for this transfer
3351 * @msg: the @spi_message to transform
3352 * @maxsize: the maximum when to apply this
3353 * @gfp: GFP allocation flags
3355 * Return: status of transformation
3357 int spi_split_transfers_maxsize(struct spi_controller *ctlr,
3358 struct spi_message *msg,
3362 struct spi_transfer *xfer;
3365 /* iterate over the transfer_list,
3366 * but note that xfer is advanced to the last transfer inserted
3367 * to avoid checking sizes again unnecessarily (also xfer does
3368 * potentiall belong to a different list by the time the
3369 * replacement has happened
3371 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3372 if (xfer->len > maxsize) {
3373 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3382 EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
3384 /*-------------------------------------------------------------------------*/
3386 /* Core methods for SPI controller protocol drivers. Some of the
3387 * other core methods are currently defined as inline functions.
3390 static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
3393 if (ctlr->bits_per_word_mask) {
3394 /* Only 32 bits fit in the mask */
3395 if (bits_per_word > 32)
3397 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
3405 * spi_setup - setup SPI mode and clock rate
3406 * @spi: the device whose settings are being modified
3407 * Context: can sleep, and no requests are queued to the device
3409 * SPI protocol drivers may need to update the transfer mode if the
3410 * device doesn't work with its default. They may likewise need
3411 * to update clock rates or word sizes from initial values. This function
3412 * changes those settings, and must be called from a context that can sleep.
3413 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
3414 * effect the next time the device is selected and data is transferred to
3415 * or from it. When this function returns, the spi device is deselected.
3417 * Note that this call will fail if the protocol driver specifies an option
3418 * that the underlying controller or its driver does not support. For
3419 * example, not all hardware supports wire transfers using nine bit words,
3420 * LSB-first wire encoding, or active-high chipselects.
3422 * Return: zero on success, else a negative error code.
3424 int spi_setup(struct spi_device *spi)
3426 unsigned bad_bits, ugly_bits;
3430 * check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO
3431 * are set at the same time
3433 if ((hweight_long(spi->mode &
3434 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) ||
3435 (hweight_long(spi->mode &
3436 (SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) {
3438 "setup: can not select any two of dual, quad and no-rx/tx at the same time\n");
3441 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
3443 if ((spi->mode & SPI_3WIRE) && (spi->mode &
3444 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3445 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
3447 /* help drivers fail *cleanly* when they need options
3448 * that aren't supported with their current controller
3449 * SPI_CS_WORD has a fallback software implementation,
3450 * so it is ignored here.
3452 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
3453 SPI_NO_TX | SPI_NO_RX);
3454 /* nothing prevents from working with active-high CS in case if it
3455 * is driven by GPIO.
3457 if (gpio_is_valid(spi->cs_gpio))
3458 bad_bits &= ~SPI_CS_HIGH;
3459 ugly_bits = bad_bits &
3460 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3461 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
3464 "setup: ignoring unsupported mode bits %x\n",
3466 spi->mode &= ~ugly_bits;
3467 bad_bits &= ~ugly_bits;
3470 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
3475 if (!spi->bits_per_word)
3476 spi->bits_per_word = 8;
3478 status = __spi_validate_bits_per_word(spi->controller,
3479 spi->bits_per_word);
3483 if (spi->controller->max_speed_hz &&
3484 (!spi->max_speed_hz ||
3485 spi->max_speed_hz > spi->controller->max_speed_hz))
3486 spi->max_speed_hz = spi->controller->max_speed_hz;
3488 mutex_lock(&spi->controller->io_mutex);
3490 if (spi->controller->setup) {
3491 status = spi->controller->setup(spi);
3493 mutex_unlock(&spi->controller->io_mutex);
3494 dev_err(&spi->controller->dev, "Failed to setup device: %d\n",
3500 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3501 status = pm_runtime_get_sync(spi->controller->dev.parent);
3503 mutex_unlock(&spi->controller->io_mutex);
3504 pm_runtime_put_noidle(spi->controller->dev.parent);
3505 dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3511 * We do not want to return positive value from pm_runtime_get,
3512 * there are many instances of devices calling spi_setup() and
3513 * checking for a non-zero return value instead of a negative
3518 spi_set_cs(spi, false, true);
3519 pm_runtime_mark_last_busy(spi->controller->dev.parent);
3520 pm_runtime_put_autosuspend(spi->controller->dev.parent);
3522 spi_set_cs(spi, false, true);
3525 mutex_unlock(&spi->controller->io_mutex);
3527 if (spi->rt && !spi->controller->rt) {
3528 spi->controller->rt = true;
3529 spi_set_thread_rt(spi->controller);
3532 trace_spi_setup(spi, status);
3534 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
3535 spi->mode & SPI_MODE_X_MASK,
3536 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
3537 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
3538 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
3539 (spi->mode & SPI_LOOP) ? "loopback, " : "",
3540 spi->bits_per_word, spi->max_speed_hz,
3545 EXPORT_SYMBOL_GPL(spi_setup);
3547 static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
3548 struct spi_device *spi)
3552 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
3556 delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
3560 if (delay1 < delay2)
3561 memcpy(&xfer->word_delay, &spi->word_delay,
3562 sizeof(xfer->word_delay));
3567 static int __spi_validate(struct spi_device *spi, struct spi_message *message)
3569 struct spi_controller *ctlr = spi->controller;
3570 struct spi_transfer *xfer;
3573 if (list_empty(&message->transfers))
3576 /* If an SPI controller does not support toggling the CS line on each
3577 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
3578 * for the CS line, we can emulate the CS-per-word hardware function by
3579 * splitting transfers into one-word transfers and ensuring that
3580 * cs_change is set for each transfer.
3582 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
3584 gpio_is_valid(spi->cs_gpio))) {
3588 maxsize = (spi->bits_per_word + 7) / 8;
3590 /* spi_split_transfers_maxsize() requires message->spi */
3593 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
3598 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3599 /* don't change cs_change on the last entry in the list */
3600 if (list_is_last(&xfer->transfer_list, &message->transfers))
3602 xfer->cs_change = 1;
3606 /* Half-duplex links include original MicroWire, and ones with
3607 * only one data pin like SPI_3WIRE (switches direction) or where
3608 * either MOSI or MISO is missing. They can also be caused by
3609 * software limitations.
3611 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3612 (spi->mode & SPI_3WIRE)) {
3613 unsigned flags = ctlr->flags;
3615 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3616 if (xfer->rx_buf && xfer->tx_buf)
3618 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
3620 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
3626 * Set transfer bits_per_word and max speed as spi device default if
3627 * it is not set for this transfer.
3628 * Set transfer tx_nbits and rx_nbits as single transfer default
3629 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
3630 * Ensure transfer word_delay is at least as long as that required by
3633 message->frame_length = 0;
3634 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3635 xfer->effective_speed_hz = 0;
3636 message->frame_length += xfer->len;
3637 if (!xfer->bits_per_word)
3638 xfer->bits_per_word = spi->bits_per_word;
3640 if (!xfer->speed_hz)
3641 xfer->speed_hz = spi->max_speed_hz;
3643 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3644 xfer->speed_hz = ctlr->max_speed_hz;
3646 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
3650 * SPI transfer length should be multiple of SPI word size
3651 * where SPI word size should be power-of-two multiple
3653 if (xfer->bits_per_word <= 8)
3655 else if (xfer->bits_per_word <= 16)
3660 /* No partial transfers accepted */
3661 if (xfer->len % w_size)
3664 if (xfer->speed_hz && ctlr->min_speed_hz &&
3665 xfer->speed_hz < ctlr->min_speed_hz)
3668 if (xfer->tx_buf && !xfer->tx_nbits)
3669 xfer->tx_nbits = SPI_NBITS_SINGLE;
3670 if (xfer->rx_buf && !xfer->rx_nbits)
3671 xfer->rx_nbits = SPI_NBITS_SINGLE;
3672 /* check transfer tx/rx_nbits:
3673 * 1. check the value matches one of single, dual and quad
3674 * 2. check tx/rx_nbits match the mode in spi_device
3677 if (spi->mode & SPI_NO_TX)
3679 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3680 xfer->tx_nbits != SPI_NBITS_DUAL &&
3681 xfer->tx_nbits != SPI_NBITS_QUAD)
3683 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3684 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3686 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3687 !(spi->mode & SPI_TX_QUAD))
3690 /* check transfer rx_nbits */
3692 if (spi->mode & SPI_NO_RX)
3694 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3695 xfer->rx_nbits != SPI_NBITS_DUAL &&
3696 xfer->rx_nbits != SPI_NBITS_QUAD)
3698 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3699 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3701 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3702 !(spi->mode & SPI_RX_QUAD))
3706 if (_spi_xfer_word_delay_update(xfer, spi))
3710 message->status = -EINPROGRESS;
3715 static int __spi_async(struct spi_device *spi, struct spi_message *message)
3717 struct spi_controller *ctlr = spi->controller;
3718 struct spi_transfer *xfer;
3721 * Some controllers do not support doing regular SPI transfers. Return
3722 * ENOTSUPP when this is the case.
3724 if (!ctlr->transfer)
3729 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
3730 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3732 trace_spi_message_submit(message);
3734 if (!ctlr->ptp_sts_supported) {
3735 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3736 xfer->ptp_sts_word_pre = 0;
3737 ptp_read_system_prets(xfer->ptp_sts);
3741 return ctlr->transfer(spi, message);
3745 * spi_async - asynchronous SPI transfer
3746 * @spi: device with which data will be exchanged
3747 * @message: describes the data transfers, including completion callback
3748 * Context: any (irqs may be blocked, etc)
3750 * This call may be used in_irq and other contexts which can't sleep,
3751 * as well as from task contexts which can sleep.
3753 * The completion callback is invoked in a context which can't sleep.
3754 * Before that invocation, the value of message->status is undefined.
3755 * When the callback is issued, message->status holds either zero (to
3756 * indicate complete success) or a negative error code. After that
3757 * callback returns, the driver which issued the transfer request may
3758 * deallocate the associated memory; it's no longer in use by any SPI
3759 * core or controller driver code.
3761 * Note that although all messages to a spi_device are handled in
3762 * FIFO order, messages may go to different devices in other orders.
3763 * Some device might be higher priority, or have various "hard" access
3764 * time requirements, for example.
3766 * On detection of any fault during the transfer, processing of
3767 * the entire message is aborted, and the device is deselected.
3768 * Until returning from the associated message completion callback,
3769 * no other spi_message queued to that device will be processed.
3770 * (This rule applies equally to all the synchronous transfer calls,
3771 * which are wrappers around this core asynchronous primitive.)
3773 * Return: zero on success, else a negative error code.
3775 int spi_async(struct spi_device *spi, struct spi_message *message)
3777 struct spi_controller *ctlr = spi->controller;
3779 unsigned long flags;
3781 ret = __spi_validate(spi, message);
3785 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3787 if (ctlr->bus_lock_flag)
3790 ret = __spi_async(spi, message);
3792 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3796 EXPORT_SYMBOL_GPL(spi_async);
3799 * spi_async_locked - version of spi_async with exclusive bus usage
3800 * @spi: device with which data will be exchanged
3801 * @message: describes the data transfers, including completion callback
3802 * Context: any (irqs may be blocked, etc)
3804 * This call may be used in_irq and other contexts which can't sleep,
3805 * as well as from task contexts which can sleep.
3807 * The completion callback is invoked in a context which can't sleep.
3808 * Before that invocation, the value of message->status is undefined.
3809 * When the callback is issued, message->status holds either zero (to
3810 * indicate complete success) or a negative error code. After that
3811 * callback returns, the driver which issued the transfer request may
3812 * deallocate the associated memory; it's no longer in use by any SPI
3813 * core or controller driver code.
3815 * Note that although all messages to a spi_device are handled in
3816 * FIFO order, messages may go to different devices in other orders.
3817 * Some device might be higher priority, or have various "hard" access
3818 * time requirements, for example.
3820 * On detection of any fault during the transfer, processing of
3821 * the entire message is aborted, and the device is deselected.
3822 * Until returning from the associated message completion callback,
3823 * no other spi_message queued to that device will be processed.
3824 * (This rule applies equally to all the synchronous transfer calls,
3825 * which are wrappers around this core asynchronous primitive.)
3827 * Return: zero on success, else a negative error code.
3829 static int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3831 struct spi_controller *ctlr = spi->controller;
3833 unsigned long flags;
3835 ret = __spi_validate(spi, message);
3839 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3841 ret = __spi_async(spi, message);
3843 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3849 /*-------------------------------------------------------------------------*/
3851 /* Utility methods for SPI protocol drivers, layered on
3852 * top of the core. Some other utility methods are defined as
3856 static void spi_complete(void *arg)
3861 static int __spi_sync(struct spi_device *spi, struct spi_message *message)
3863 DECLARE_COMPLETION_ONSTACK(done);
3865 struct spi_controller *ctlr = spi->controller;
3866 unsigned long flags;
3868 status = __spi_validate(spi, message);
3872 message->complete = spi_complete;
3873 message->context = &done;
3876 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
3877 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3879 /* If we're not using the legacy transfer method then we will
3880 * try to transfer in the calling context so special case.
3881 * This code would be less tricky if we could remove the
3882 * support for driver implemented message queues.
3884 if (ctlr->transfer == spi_queued_transfer) {
3885 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3887 trace_spi_message_submit(message);
3889 status = __spi_queued_transfer(spi, message, false);
3891 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3893 status = spi_async_locked(spi, message);
3897 /* Push out the messages in the calling context if we
3900 if (ctlr->transfer == spi_queued_transfer) {
3901 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
3902 spi_sync_immediate);
3903 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3904 spi_sync_immediate);
3905 __spi_pump_messages(ctlr, false);
3908 wait_for_completion(&done);
3909 status = message->status;
3911 message->context = NULL;
3916 * spi_sync - blocking/synchronous SPI data transfers
3917 * @spi: device with which data will be exchanged
3918 * @message: describes the data transfers
3919 * Context: can sleep
3921 * This call may only be used from a context that may sleep. The sleep
3922 * is non-interruptible, and has no timeout. Low-overhead controller
3923 * drivers may DMA directly into and out of the message buffers.
3925 * Note that the SPI device's chip select is active during the message,
3926 * and then is normally disabled between messages. Drivers for some
3927 * frequently-used devices may want to minimize costs of selecting a chip,
3928 * by leaving it selected in anticipation that the next message will go
3929 * to the same chip. (That may increase power usage.)
3931 * Also, the caller is guaranteeing that the memory associated with the
3932 * message will not be freed before this call returns.
3934 * Return: zero on success, else a negative error code.
3936 int spi_sync(struct spi_device *spi, struct spi_message *message)
3940 mutex_lock(&spi->controller->bus_lock_mutex);
3941 ret = __spi_sync(spi, message);
3942 mutex_unlock(&spi->controller->bus_lock_mutex);
3946 EXPORT_SYMBOL_GPL(spi_sync);
3949 * spi_sync_locked - version of spi_sync with exclusive bus usage
3950 * @spi: device with which data will be exchanged
3951 * @message: describes the data transfers
3952 * Context: can sleep
3954 * This call may only be used from a context that may sleep. The sleep
3955 * is non-interruptible, and has no timeout. Low-overhead controller
3956 * drivers may DMA directly into and out of the message buffers.
3958 * This call should be used by drivers that require exclusive access to the
3959 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3960 * be released by a spi_bus_unlock call when the exclusive access is over.
3962 * Return: zero on success, else a negative error code.
3964 int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3966 return __spi_sync(spi, message);
3968 EXPORT_SYMBOL_GPL(spi_sync_locked);
3971 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3972 * @ctlr: SPI bus master that should be locked for exclusive bus access
3973 * Context: can sleep
3975 * This call may only be used from a context that may sleep. The sleep
3976 * is non-interruptible, and has no timeout.
3978 * This call should be used by drivers that require exclusive access to the
3979 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3980 * exclusive access is over. Data transfer must be done by spi_sync_locked
3981 * and spi_async_locked calls when the SPI bus lock is held.
3983 * Return: always zero.
3985 int spi_bus_lock(struct spi_controller *ctlr)
3987 unsigned long flags;
3989 mutex_lock(&ctlr->bus_lock_mutex);
3991 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3992 ctlr->bus_lock_flag = 1;
3993 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3995 /* mutex remains locked until spi_bus_unlock is called */
3999 EXPORT_SYMBOL_GPL(spi_bus_lock);
4002 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4003 * @ctlr: SPI bus master that was locked for exclusive bus access
4004 * Context: can sleep
4006 * This call may only be used from a context that may sleep. The sleep
4007 * is non-interruptible, and has no timeout.
4009 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4012 * Return: always zero.
4014 int spi_bus_unlock(struct spi_controller *ctlr)
4016 ctlr->bus_lock_flag = 0;
4018 mutex_unlock(&ctlr->bus_lock_mutex);
4022 EXPORT_SYMBOL_GPL(spi_bus_unlock);
4024 /* portable code must never pass more than 32 bytes */
4025 #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
4030 * spi_write_then_read - SPI synchronous write followed by read
4031 * @spi: device with which data will be exchanged
4032 * @txbuf: data to be written (need not be dma-safe)
4033 * @n_tx: size of txbuf, in bytes
4034 * @rxbuf: buffer into which data will be read (need not be dma-safe)
4035 * @n_rx: size of rxbuf, in bytes
4036 * Context: can sleep
4038 * This performs a half duplex MicroWire style transaction with the
4039 * device, sending txbuf and then reading rxbuf. The return value
4040 * is zero for success, else a negative errno status code.
4041 * This call may only be used from a context that may sleep.
4043 * Parameters to this routine are always copied using a small buffer.
4044 * Performance-sensitive or bulk transfer code should instead use
4045 * spi_{async,sync}() calls with dma-safe buffers.
4047 * Return: zero on success, else a negative error code.
4049 int spi_write_then_read(struct spi_device *spi,
4050 const void *txbuf, unsigned n_tx,
4051 void *rxbuf, unsigned n_rx)
4053 static DEFINE_MUTEX(lock);
4056 struct spi_message message;
4057 struct spi_transfer x[2];
4060 /* Use preallocated DMA-safe buffer if we can. We can't avoid
4061 * copying here, (as a pure convenience thing), but we can
4062 * keep heap costs out of the hot path unless someone else is
4063 * using the pre-allocated buffer or the transfer is too large.
4065 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
4066 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
4067 GFP_KERNEL | GFP_DMA);
4074 spi_message_init(&message);
4075 memset(x, 0, sizeof(x));
4078 spi_message_add_tail(&x[0], &message);
4082 spi_message_add_tail(&x[1], &message);
4085 memcpy(local_buf, txbuf, n_tx);
4086 x[0].tx_buf = local_buf;
4087 x[1].rx_buf = local_buf + n_tx;
4090 status = spi_sync(spi, &message);
4092 memcpy(rxbuf, x[1].rx_buf, n_rx);
4094 if (x[0].tx_buf == buf)
4095 mutex_unlock(&lock);
4101 EXPORT_SYMBOL_GPL(spi_write_then_read);
4103 /*-------------------------------------------------------------------------*/
4105 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
4106 /* must call put_device() when done with returned spi_device device */
4107 static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
4109 struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
4111 return dev ? to_spi_device(dev) : NULL;
4114 /* the spi controllers are not using spi_bus, so we find it with another way */
4115 static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
4119 dev = class_find_device_by_of_node(&spi_master_class, node);
4120 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4121 dev = class_find_device_by_of_node(&spi_slave_class, node);
4125 /* reference got in class_find_device */
4126 return container_of(dev, struct spi_controller, dev);
4129 static int of_spi_notify(struct notifier_block *nb, unsigned long action,
4132 struct of_reconfig_data *rd = arg;
4133 struct spi_controller *ctlr;
4134 struct spi_device *spi;
4136 switch (of_reconfig_get_state_change(action, arg)) {
4137 case OF_RECONFIG_CHANGE_ADD:
4138 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
4140 return NOTIFY_OK; /* not for us */
4142 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
4143 put_device(&ctlr->dev);
4147 spi = of_register_spi_device(ctlr, rd->dn);
4148 put_device(&ctlr->dev);
4151 pr_err("%s: failed to create for '%pOF'\n",
4153 of_node_clear_flag(rd->dn, OF_POPULATED);
4154 return notifier_from_errno(PTR_ERR(spi));
4158 case OF_RECONFIG_CHANGE_REMOVE:
4159 /* already depopulated? */
4160 if (!of_node_check_flag(rd->dn, OF_POPULATED))
4163 /* find our device by node */
4164 spi = of_find_spi_device_by_node(rd->dn);
4166 return NOTIFY_OK; /* no? not meant for us */
4168 /* unregister takes one ref away */
4169 spi_unregister_device(spi);
4171 /* and put the reference of the find */
4172 put_device(&spi->dev);
4179 static struct notifier_block spi_of_notifier = {
4180 .notifier_call = of_spi_notify,
4182 #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4183 extern struct notifier_block spi_of_notifier;
4184 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4186 #if IS_ENABLED(CONFIG_ACPI)
4187 static int spi_acpi_controller_match(struct device *dev, const void *data)
4189 return ACPI_COMPANION(dev->parent) == data;
4192 static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
4196 dev = class_find_device(&spi_master_class, NULL, adev,
4197 spi_acpi_controller_match);
4198 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4199 dev = class_find_device(&spi_slave_class, NULL, adev,
4200 spi_acpi_controller_match);
4204 return container_of(dev, struct spi_controller, dev);
4207 static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
4211 dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
4212 return to_spi_device(dev);
4215 static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
4218 struct acpi_device *adev = arg;
4219 struct spi_controller *ctlr;
4220 struct spi_device *spi;
4223 case ACPI_RECONFIG_DEVICE_ADD:
4224 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
4228 acpi_register_spi_device(ctlr, adev);
4229 put_device(&ctlr->dev);
4231 case ACPI_RECONFIG_DEVICE_REMOVE:
4232 if (!acpi_device_enumerated(adev))
4235 spi = acpi_spi_find_device_by_adev(adev);
4239 spi_unregister_device(spi);
4240 put_device(&spi->dev);
4247 static struct notifier_block spi_acpi_notifier = {
4248 .notifier_call = acpi_spi_notify,
4251 extern struct notifier_block spi_acpi_notifier;
4254 static int __init spi_init(void)
4258 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
4264 status = bus_register(&spi_bus_type);
4268 status = class_register(&spi_master_class);
4272 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
4273 status = class_register(&spi_slave_class);
4278 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
4279 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
4280 if (IS_ENABLED(CONFIG_ACPI))
4281 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
4286 class_unregister(&spi_master_class);
4288 bus_unregister(&spi_bus_type);
4296 /* board_info is normally registered in arch_initcall(),
4297 * but even essential drivers wait till later
4299 * REVISIT only boardinfo really needs static linking. the rest (device and
4300 * driver registration) _could_ be dynamically linked (modular) ... costs
4301 * include needing to have boardinfo data structures be much more public.
4303 postcore_initcall(spi_init);