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/pm_runtime.h>
23 #include <linux/pm_domain.h>
24 #include <linux/property.h>
25 #include <linux/export.h>
26 #include <linux/sched/rt.h>
27 #include <uapi/linux/sched/types.h>
28 #include <linux/delay.h>
29 #include <linux/kthread.h>
30 #include <linux/ioport.h>
31 #include <linux/acpi.h>
32 #include <linux/highmem.h>
33 #include <linux/idr.h>
34 #include <linux/platform_data/x86/apple.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/spi.h>
39 #include "internals.h"
41 static DEFINE_IDR(spi_master_idr);
43 static void spidev_release(struct device *dev)
45 struct spi_device *spi = to_spi_device(dev);
47 /* spi controllers may cleanup for released devices */
48 if (spi->controller->cleanup)
49 spi->controller->cleanup(spi);
51 spi_controller_put(spi->controller);
52 kfree(spi->driver_override);
57 modalias_show(struct device *dev, struct device_attribute *a, char *buf)
59 const struct spi_device *spi = to_spi_device(dev);
62 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
66 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
68 static DEVICE_ATTR_RO(modalias);
70 static ssize_t driver_override_store(struct device *dev,
71 struct device_attribute *a,
72 const char *buf, size_t count)
74 struct spi_device *spi = to_spi_device(dev);
75 const char *end = memchr(buf, '\n', count);
76 const size_t len = end ? end - buf : count;
77 const char *driver_override, *old;
79 /* We need to keep extra room for a newline when displaying value */
80 if (len >= (PAGE_SIZE - 1))
83 driver_override = kstrndup(buf, len, GFP_KERNEL);
88 old = spi->driver_override;
90 spi->driver_override = driver_override;
92 /* Emptry string, disable driver override */
93 spi->driver_override = NULL;
94 kfree(driver_override);
102 static ssize_t driver_override_show(struct device *dev,
103 struct device_attribute *a, char *buf)
105 const struct spi_device *spi = to_spi_device(dev);
109 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
113 static DEVICE_ATTR_RW(driver_override);
115 #define SPI_STATISTICS_ATTRS(field, file) \
116 static ssize_t spi_controller_##field##_show(struct device *dev, \
117 struct device_attribute *attr, \
120 struct spi_controller *ctlr = container_of(dev, \
121 struct spi_controller, dev); \
122 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
124 static struct device_attribute dev_attr_spi_controller_##field = { \
125 .attr = { .name = file, .mode = 0444 }, \
126 .show = spi_controller_##field##_show, \
128 static ssize_t spi_device_##field##_show(struct device *dev, \
129 struct device_attribute *attr, \
132 struct spi_device *spi = to_spi_device(dev); \
133 return spi_statistics_##field##_show(&spi->statistics, buf); \
135 static struct device_attribute dev_attr_spi_device_##field = { \
136 .attr = { .name = file, .mode = 0444 }, \
137 .show = spi_device_##field##_show, \
140 #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
141 static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
144 unsigned long flags; \
146 spin_lock_irqsave(&stat->lock, flags); \
147 len = sprintf(buf, format_string, stat->field); \
148 spin_unlock_irqrestore(&stat->lock, flags); \
151 SPI_STATISTICS_ATTRS(name, file)
153 #define SPI_STATISTICS_SHOW(field, format_string) \
154 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
155 field, format_string)
157 SPI_STATISTICS_SHOW(messages, "%lu");
158 SPI_STATISTICS_SHOW(transfers, "%lu");
159 SPI_STATISTICS_SHOW(errors, "%lu");
160 SPI_STATISTICS_SHOW(timedout, "%lu");
162 SPI_STATISTICS_SHOW(spi_sync, "%lu");
163 SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
164 SPI_STATISTICS_SHOW(spi_async, "%lu");
166 SPI_STATISTICS_SHOW(bytes, "%llu");
167 SPI_STATISTICS_SHOW(bytes_rx, "%llu");
168 SPI_STATISTICS_SHOW(bytes_tx, "%llu");
170 #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
171 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
172 "transfer_bytes_histo_" number, \
173 transfer_bytes_histo[index], "%lu")
174 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
175 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
176 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
177 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
178 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
179 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
180 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
181 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
182 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
183 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
184 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
185 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
186 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
187 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
188 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
189 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
190 SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
192 SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
194 static struct attribute *spi_dev_attrs[] = {
195 &dev_attr_modalias.attr,
196 &dev_attr_driver_override.attr,
200 static const struct attribute_group spi_dev_group = {
201 .attrs = spi_dev_attrs,
204 static struct attribute *spi_device_statistics_attrs[] = {
205 &dev_attr_spi_device_messages.attr,
206 &dev_attr_spi_device_transfers.attr,
207 &dev_attr_spi_device_errors.attr,
208 &dev_attr_spi_device_timedout.attr,
209 &dev_attr_spi_device_spi_sync.attr,
210 &dev_attr_spi_device_spi_sync_immediate.attr,
211 &dev_attr_spi_device_spi_async.attr,
212 &dev_attr_spi_device_bytes.attr,
213 &dev_attr_spi_device_bytes_rx.attr,
214 &dev_attr_spi_device_bytes_tx.attr,
215 &dev_attr_spi_device_transfer_bytes_histo0.attr,
216 &dev_attr_spi_device_transfer_bytes_histo1.attr,
217 &dev_attr_spi_device_transfer_bytes_histo2.attr,
218 &dev_attr_spi_device_transfer_bytes_histo3.attr,
219 &dev_attr_spi_device_transfer_bytes_histo4.attr,
220 &dev_attr_spi_device_transfer_bytes_histo5.attr,
221 &dev_attr_spi_device_transfer_bytes_histo6.attr,
222 &dev_attr_spi_device_transfer_bytes_histo7.attr,
223 &dev_attr_spi_device_transfer_bytes_histo8.attr,
224 &dev_attr_spi_device_transfer_bytes_histo9.attr,
225 &dev_attr_spi_device_transfer_bytes_histo10.attr,
226 &dev_attr_spi_device_transfer_bytes_histo11.attr,
227 &dev_attr_spi_device_transfer_bytes_histo12.attr,
228 &dev_attr_spi_device_transfer_bytes_histo13.attr,
229 &dev_attr_spi_device_transfer_bytes_histo14.attr,
230 &dev_attr_spi_device_transfer_bytes_histo15.attr,
231 &dev_attr_spi_device_transfer_bytes_histo16.attr,
232 &dev_attr_spi_device_transfers_split_maxsize.attr,
236 static const struct attribute_group spi_device_statistics_group = {
237 .name = "statistics",
238 .attrs = spi_device_statistics_attrs,
241 static const struct attribute_group *spi_dev_groups[] = {
243 &spi_device_statistics_group,
247 static struct attribute *spi_controller_statistics_attrs[] = {
248 &dev_attr_spi_controller_messages.attr,
249 &dev_attr_spi_controller_transfers.attr,
250 &dev_attr_spi_controller_errors.attr,
251 &dev_attr_spi_controller_timedout.attr,
252 &dev_attr_spi_controller_spi_sync.attr,
253 &dev_attr_spi_controller_spi_sync_immediate.attr,
254 &dev_attr_spi_controller_spi_async.attr,
255 &dev_attr_spi_controller_bytes.attr,
256 &dev_attr_spi_controller_bytes_rx.attr,
257 &dev_attr_spi_controller_bytes_tx.attr,
258 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
259 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
260 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
261 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
262 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
263 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
264 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
265 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
266 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
267 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
268 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
269 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
270 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
271 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
272 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
273 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
274 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
275 &dev_attr_spi_controller_transfers_split_maxsize.attr,
279 static const struct attribute_group spi_controller_statistics_group = {
280 .name = "statistics",
281 .attrs = spi_controller_statistics_attrs,
284 static const struct attribute_group *spi_master_groups[] = {
285 &spi_controller_statistics_group,
289 void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
290 struct spi_transfer *xfer,
291 struct spi_controller *ctlr)
294 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
299 spin_lock_irqsave(&stats->lock, flags);
302 stats->transfer_bytes_histo[l2len]++;
304 stats->bytes += xfer->len;
305 if ((xfer->tx_buf) &&
306 (xfer->tx_buf != ctlr->dummy_tx))
307 stats->bytes_tx += xfer->len;
308 if ((xfer->rx_buf) &&
309 (xfer->rx_buf != ctlr->dummy_rx))
310 stats->bytes_rx += xfer->len;
312 spin_unlock_irqrestore(&stats->lock, flags);
314 EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
316 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
317 * and the sysfs version makes coldplug work too.
320 static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
321 const struct spi_device *sdev)
323 while (id->name[0]) {
324 if (!strcmp(sdev->modalias, id->name))
331 const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
333 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
335 return spi_match_id(sdrv->id_table, sdev);
337 EXPORT_SYMBOL_GPL(spi_get_device_id);
339 static int spi_match_device(struct device *dev, struct device_driver *drv)
341 const struct spi_device *spi = to_spi_device(dev);
342 const struct spi_driver *sdrv = to_spi_driver(drv);
344 /* Check override first, and if set, only use the named driver */
345 if (spi->driver_override)
346 return strcmp(spi->driver_override, drv->name) == 0;
348 /* Attempt an OF style match */
349 if (of_driver_match_device(dev, drv))
353 if (acpi_driver_match_device(dev, drv))
357 return !!spi_match_id(sdrv->id_table, spi);
359 return strcmp(spi->modalias, drv->name) == 0;
362 static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
364 const struct spi_device *spi = to_spi_device(dev);
367 rc = acpi_device_uevent_modalias(dev, env);
371 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
374 struct bus_type spi_bus_type = {
376 .dev_groups = spi_dev_groups,
377 .match = spi_match_device,
378 .uevent = spi_uevent,
380 EXPORT_SYMBOL_GPL(spi_bus_type);
383 static int spi_drv_probe(struct device *dev)
385 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
386 struct spi_device *spi = to_spi_device(dev);
389 ret = of_clk_set_defaults(dev->of_node, false);
394 spi->irq = of_irq_get(dev->of_node, 0);
395 if (spi->irq == -EPROBE_DEFER)
396 return -EPROBE_DEFER;
401 ret = dev_pm_domain_attach(dev, true);
405 ret = sdrv->probe(spi);
407 dev_pm_domain_detach(dev, true);
412 static int spi_drv_remove(struct device *dev)
414 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
417 ret = sdrv->remove(to_spi_device(dev));
418 dev_pm_domain_detach(dev, true);
423 static void spi_drv_shutdown(struct device *dev)
425 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
427 sdrv->shutdown(to_spi_device(dev));
431 * __spi_register_driver - register a SPI driver
432 * @owner: owner module of the driver to register
433 * @sdrv: the driver to register
436 * Return: zero on success, else a negative error code.
438 int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
440 sdrv->driver.owner = owner;
441 sdrv->driver.bus = &spi_bus_type;
443 sdrv->driver.probe = spi_drv_probe;
445 sdrv->driver.remove = spi_drv_remove;
447 sdrv->driver.shutdown = spi_drv_shutdown;
448 return driver_register(&sdrv->driver);
450 EXPORT_SYMBOL_GPL(__spi_register_driver);
452 /*-------------------------------------------------------------------------*/
454 /* SPI devices should normally not be created by SPI device drivers; that
455 * would make them board-specific. Similarly with SPI controller drivers.
456 * Device registration normally goes into like arch/.../mach.../board-YYY.c
457 * with other readonly (flashable) information about mainboard devices.
461 struct list_head list;
462 struct spi_board_info board_info;
465 static LIST_HEAD(board_list);
466 static LIST_HEAD(spi_controller_list);
469 * Used to protect add/del opertion for board_info list and
470 * spi_controller list, and their matching process
471 * also used to protect object of type struct idr
473 static DEFINE_MUTEX(board_lock);
476 * spi_alloc_device - Allocate a new SPI device
477 * @ctlr: Controller to which device is connected
480 * Allows a driver to allocate and initialize a spi_device without
481 * registering it immediately. This allows a driver to directly
482 * fill the spi_device with device parameters before calling
483 * spi_add_device() on it.
485 * Caller is responsible to call spi_add_device() on the returned
486 * spi_device structure to add it to the SPI controller. If the caller
487 * needs to discard the spi_device without adding it, then it should
488 * call spi_dev_put() on it.
490 * Return: a pointer to the new device, or NULL.
492 struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
494 struct spi_device *spi;
496 if (!spi_controller_get(ctlr))
499 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
501 spi_controller_put(ctlr);
505 spi->master = spi->controller = ctlr;
506 spi->dev.parent = &ctlr->dev;
507 spi->dev.bus = &spi_bus_type;
508 spi->dev.release = spidev_release;
509 spi->cs_gpio = -ENOENT;
511 spin_lock_init(&spi->statistics.lock);
513 device_initialize(&spi->dev);
516 EXPORT_SYMBOL_GPL(spi_alloc_device);
518 static void spi_dev_set_name(struct spi_device *spi)
520 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
523 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
527 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
531 static int spi_dev_check(struct device *dev, void *data)
533 struct spi_device *spi = to_spi_device(dev);
534 struct spi_device *new_spi = data;
536 if (spi->controller == new_spi->controller &&
537 spi->chip_select == new_spi->chip_select)
543 * spi_add_device - Add spi_device allocated with spi_alloc_device
544 * @spi: spi_device to register
546 * Companion function to spi_alloc_device. Devices allocated with
547 * spi_alloc_device can be added onto the spi bus with this function.
549 * Return: 0 on success; negative errno on failure
551 int spi_add_device(struct spi_device *spi)
553 static DEFINE_MUTEX(spi_add_lock);
554 struct spi_controller *ctlr = spi->controller;
555 struct device *dev = ctlr->dev.parent;
558 /* Chipselects are numbered 0..max; validate. */
559 if (spi->chip_select >= ctlr->num_chipselect) {
560 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
561 ctlr->num_chipselect);
565 /* Set the bus ID string */
566 spi_dev_set_name(spi);
568 /* We need to make sure there's no other device with this
569 * chipselect **BEFORE** we call setup(), else we'll trash
570 * its configuration. Lock against concurrent add() calls.
572 mutex_lock(&spi_add_lock);
574 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
576 dev_err(dev, "chipselect %d already in use\n",
582 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
584 /* Drivers may modify this initial i/o setup, but will
585 * normally rely on the device being setup. Devices
586 * using SPI_CS_HIGH can't coexist well otherwise...
588 status = spi_setup(spi);
590 dev_err(dev, "can't setup %s, status %d\n",
591 dev_name(&spi->dev), status);
595 /* Device may be bound to an active driver when this returns */
596 status = device_add(&spi->dev);
598 dev_err(dev, "can't add %s, status %d\n",
599 dev_name(&spi->dev), status);
601 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
604 mutex_unlock(&spi_add_lock);
607 EXPORT_SYMBOL_GPL(spi_add_device);
610 * spi_new_device - instantiate one new SPI device
611 * @ctlr: Controller to which device is connected
612 * @chip: Describes the SPI device
615 * On typical mainboards, this is purely internal; and it's not needed
616 * after board init creates the hard-wired devices. Some development
617 * platforms may not be able to use spi_register_board_info though, and
618 * this is exported so that for example a USB or parport based adapter
619 * driver could add devices (which it would learn about out-of-band).
621 * Return: the new device, or NULL.
623 struct spi_device *spi_new_device(struct spi_controller *ctlr,
624 struct spi_board_info *chip)
626 struct spi_device *proxy;
629 /* NOTE: caller did any chip->bus_num checks necessary.
631 * Also, unless we change the return value convention to use
632 * error-or-pointer (not NULL-or-pointer), troubleshootability
633 * suggests syslogged diagnostics are best here (ugh).
636 proxy = spi_alloc_device(ctlr);
640 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
642 proxy->chip_select = chip->chip_select;
643 proxy->max_speed_hz = chip->max_speed_hz;
644 proxy->mode = chip->mode;
645 proxy->irq = chip->irq;
646 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
647 proxy->dev.platform_data = (void *) chip->platform_data;
648 proxy->controller_data = chip->controller_data;
649 proxy->controller_state = NULL;
651 if (chip->properties) {
652 status = device_add_properties(&proxy->dev, chip->properties);
655 "failed to add properties to '%s': %d\n",
656 chip->modalias, status);
661 status = spi_add_device(proxy);
663 goto err_remove_props;
668 if (chip->properties)
669 device_remove_properties(&proxy->dev);
674 EXPORT_SYMBOL_GPL(spi_new_device);
677 * spi_unregister_device - unregister a single SPI device
678 * @spi: spi_device to unregister
680 * Start making the passed SPI device vanish. Normally this would be handled
681 * by spi_unregister_controller().
683 void spi_unregister_device(struct spi_device *spi)
688 if (spi->dev.of_node) {
689 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
690 of_node_put(spi->dev.of_node);
692 if (ACPI_COMPANION(&spi->dev))
693 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
694 device_unregister(&spi->dev);
696 EXPORT_SYMBOL_GPL(spi_unregister_device);
698 static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
699 struct spi_board_info *bi)
701 struct spi_device *dev;
703 if (ctlr->bus_num != bi->bus_num)
706 dev = spi_new_device(ctlr, bi);
708 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
713 * spi_register_board_info - register SPI devices for a given board
714 * @info: array of chip descriptors
715 * @n: how many descriptors are provided
718 * Board-specific early init code calls this (probably during arch_initcall)
719 * with segments of the SPI device table. Any device nodes are created later,
720 * after the relevant parent SPI controller (bus_num) is defined. We keep
721 * this table of devices forever, so that reloading a controller driver will
722 * not make Linux forget about these hard-wired devices.
724 * Other code can also call this, e.g. a particular add-on board might provide
725 * SPI devices through its expansion connector, so code initializing that board
726 * would naturally declare its SPI devices.
728 * The board info passed can safely be __initdata ... but be careful of
729 * any embedded pointers (platform_data, etc), they're copied as-is.
730 * Device properties are deep-copied though.
732 * Return: zero on success, else a negative error code.
734 int spi_register_board_info(struct spi_board_info const *info, unsigned n)
736 struct boardinfo *bi;
742 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
746 for (i = 0; i < n; i++, bi++, info++) {
747 struct spi_controller *ctlr;
749 memcpy(&bi->board_info, info, sizeof(*info));
750 if (info->properties) {
751 bi->board_info.properties =
752 property_entries_dup(info->properties);
753 if (IS_ERR(bi->board_info.properties))
754 return PTR_ERR(bi->board_info.properties);
757 mutex_lock(&board_lock);
758 list_add_tail(&bi->list, &board_list);
759 list_for_each_entry(ctlr, &spi_controller_list, list)
760 spi_match_controller_to_boardinfo(ctlr,
762 mutex_unlock(&board_lock);
768 /*-------------------------------------------------------------------------*/
770 static void spi_set_cs(struct spi_device *spi, bool enable)
772 if (spi->mode & SPI_CS_HIGH)
775 if (gpio_is_valid(spi->cs_gpio)) {
776 /* Honour the SPI_NO_CS flag */
777 if (!(spi->mode & SPI_NO_CS))
778 gpio_set_value(spi->cs_gpio, !enable);
779 /* Some SPI masters need both GPIO CS & slave_select */
780 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
781 spi->controller->set_cs)
782 spi->controller->set_cs(spi, !enable);
783 } else if (spi->controller->set_cs) {
784 spi->controller->set_cs(spi, !enable);
788 #ifdef CONFIG_HAS_DMA
789 int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
790 struct sg_table *sgt, void *buf, size_t len,
791 enum dma_data_direction dir)
793 const bool vmalloced_buf = is_vmalloc_addr(buf);
794 unsigned int max_seg_size = dma_get_max_seg_size(dev);
795 #ifdef CONFIG_HIGHMEM
796 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
797 (unsigned long)buf < (PKMAP_BASE +
798 (LAST_PKMAP * PAGE_SIZE)));
800 const bool kmap_buf = false;
804 struct page *vm_page;
805 struct scatterlist *sg;
810 if (vmalloced_buf || kmap_buf) {
811 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
812 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
813 } else if (virt_addr_valid(buf)) {
814 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
815 sgs = DIV_ROUND_UP(len, desc_len);
820 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
825 for (i = 0; i < sgs; i++) {
827 if (vmalloced_buf || kmap_buf) {
829 * Next scatterlist entry size is the minimum between
830 * the desc_len and the remaining buffer length that
833 min = min_t(size_t, desc_len,
835 PAGE_SIZE - offset_in_page(buf)));
837 vm_page = vmalloc_to_page(buf);
839 vm_page = kmap_to_page(buf);
844 sg_set_page(sg, vm_page,
845 min, offset_in_page(buf));
847 min = min_t(size_t, len, desc_len);
849 sg_set_buf(sg, sg_buf, min);
857 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
870 void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
871 struct sg_table *sgt, enum dma_data_direction dir)
873 if (sgt->orig_nents) {
874 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
879 static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
881 struct device *tx_dev, *rx_dev;
882 struct spi_transfer *xfer;
889 tx_dev = ctlr->dma_tx->device->dev;
891 tx_dev = ctlr->dev.parent;
894 rx_dev = ctlr->dma_rx->device->dev;
896 rx_dev = ctlr->dev.parent;
898 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
899 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
902 if (xfer->tx_buf != NULL) {
903 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
904 (void *)xfer->tx_buf, xfer->len,
910 if (xfer->rx_buf != NULL) {
911 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
912 xfer->rx_buf, xfer->len,
915 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
922 ctlr->cur_msg_mapped = true;
927 static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
929 struct spi_transfer *xfer;
930 struct device *tx_dev, *rx_dev;
932 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
936 tx_dev = ctlr->dma_tx->device->dev;
938 tx_dev = ctlr->dev.parent;
941 rx_dev = ctlr->dma_rx->device->dev;
943 rx_dev = ctlr->dev.parent;
945 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
946 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
949 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
950 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
955 #else /* !CONFIG_HAS_DMA */
956 static inline int __spi_map_msg(struct spi_controller *ctlr,
957 struct spi_message *msg)
962 static inline int __spi_unmap_msg(struct spi_controller *ctlr,
963 struct spi_message *msg)
967 #endif /* !CONFIG_HAS_DMA */
969 static inline int spi_unmap_msg(struct spi_controller *ctlr,
970 struct spi_message *msg)
972 struct spi_transfer *xfer;
974 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
976 * Restore the original value of tx_buf or rx_buf if they are
979 if (xfer->tx_buf == ctlr->dummy_tx)
981 if (xfer->rx_buf == ctlr->dummy_rx)
985 return __spi_unmap_msg(ctlr, msg);
988 static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
990 struct spi_transfer *xfer;
992 unsigned int max_tx, max_rx;
994 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
998 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
999 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
1001 max_tx = max(xfer->len, max_tx);
1002 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
1004 max_rx = max(xfer->len, max_rx);
1008 tmp = krealloc(ctlr->dummy_tx, max_tx,
1009 GFP_KERNEL | GFP_DMA);
1012 ctlr->dummy_tx = tmp;
1013 memset(tmp, 0, max_tx);
1017 tmp = krealloc(ctlr->dummy_rx, max_rx,
1018 GFP_KERNEL | GFP_DMA);
1021 ctlr->dummy_rx = tmp;
1024 if (max_tx || max_rx) {
1025 list_for_each_entry(xfer, &msg->transfers,
1028 xfer->tx_buf = ctlr->dummy_tx;
1030 xfer->rx_buf = ctlr->dummy_rx;
1035 return __spi_map_msg(ctlr, msg);
1038 static int spi_transfer_wait(struct spi_controller *ctlr,
1039 struct spi_message *msg,
1040 struct spi_transfer *xfer)
1042 struct spi_statistics *statm = &ctlr->statistics;
1043 struct spi_statistics *stats = &msg->spi->statistics;
1044 unsigned long long ms = 1;
1046 if (spi_controller_is_slave(ctlr)) {
1047 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1048 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1052 ms = 8LL * 1000LL * xfer->len;
1053 do_div(ms, xfer->speed_hz);
1054 ms += ms + 200; /* some tolerance */
1059 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1060 msecs_to_jiffies(ms));
1063 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1064 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1065 dev_err(&msg->spi->dev,
1066 "SPI transfer timed out\n");
1075 * spi_transfer_one_message - Default implementation of transfer_one_message()
1077 * This is a standard implementation of transfer_one_message() for
1078 * drivers which implement a transfer_one() operation. It provides
1079 * standard handling of delays and chip select management.
1081 static int spi_transfer_one_message(struct spi_controller *ctlr,
1082 struct spi_message *msg)
1084 struct spi_transfer *xfer;
1085 bool keep_cs = false;
1087 struct spi_statistics *statm = &ctlr->statistics;
1088 struct spi_statistics *stats = &msg->spi->statistics;
1090 spi_set_cs(msg->spi, true);
1092 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1093 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1095 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1096 trace_spi_transfer_start(msg, xfer);
1098 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1099 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
1101 if (xfer->tx_buf || xfer->rx_buf) {
1102 reinit_completion(&ctlr->xfer_completion);
1104 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1106 SPI_STATISTICS_INCREMENT_FIELD(statm,
1108 SPI_STATISTICS_INCREMENT_FIELD(stats,
1110 dev_err(&msg->spi->dev,
1111 "SPI transfer failed: %d\n", ret);
1116 ret = spi_transfer_wait(ctlr, msg, xfer);
1122 dev_err(&msg->spi->dev,
1123 "Bufferless transfer has length %u\n",
1127 trace_spi_transfer_stop(msg, xfer);
1129 if (msg->status != -EINPROGRESS)
1132 if (xfer->delay_usecs) {
1133 u16 us = xfer->delay_usecs;
1138 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1141 if (xfer->cs_change) {
1142 if (list_is_last(&xfer->transfer_list,
1146 spi_set_cs(msg->spi, false);
1148 spi_set_cs(msg->spi, true);
1152 msg->actual_length += xfer->len;
1156 if (ret != 0 || !keep_cs)
1157 spi_set_cs(msg->spi, false);
1159 if (msg->status == -EINPROGRESS)
1162 if (msg->status && ctlr->handle_err)
1163 ctlr->handle_err(ctlr, msg);
1165 spi_res_release(ctlr, msg);
1167 spi_finalize_current_message(ctlr);
1173 * spi_finalize_current_transfer - report completion of a transfer
1174 * @ctlr: the controller reporting completion
1176 * Called by SPI drivers using the core transfer_one_message()
1177 * implementation to notify it that the current interrupt driven
1178 * transfer has finished and the next one may be scheduled.
1180 void spi_finalize_current_transfer(struct spi_controller *ctlr)
1182 complete(&ctlr->xfer_completion);
1184 EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1187 * __spi_pump_messages - function which processes spi message queue
1188 * @ctlr: controller to process queue for
1189 * @in_kthread: true if we are in the context of the message pump thread
1191 * This function checks if there is any spi message in the queue that
1192 * needs processing and if so call out to the driver to initialize hardware
1193 * and transfer each message.
1195 * Note that it is called both from the kthread itself and also from
1196 * inside spi_sync(); the queue extraction handling at the top of the
1197 * function should deal with this safely.
1199 static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
1201 unsigned long flags;
1202 bool was_busy = false;
1206 spin_lock_irqsave(&ctlr->queue_lock, flags);
1208 /* Make sure we are not already running a message */
1209 if (ctlr->cur_msg) {
1210 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1214 /* If another context is idling the device then defer */
1216 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1217 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1221 /* Check if the queue is idle */
1222 if (list_empty(&ctlr->queue) || !ctlr->running) {
1224 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1228 /* Only do teardown in the thread */
1230 kthread_queue_work(&ctlr->kworker,
1231 &ctlr->pump_messages);
1232 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1237 ctlr->idling = true;
1238 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1240 kfree(ctlr->dummy_rx);
1241 ctlr->dummy_rx = NULL;
1242 kfree(ctlr->dummy_tx);
1243 ctlr->dummy_tx = NULL;
1244 if (ctlr->unprepare_transfer_hardware &&
1245 ctlr->unprepare_transfer_hardware(ctlr))
1247 "failed to unprepare transfer hardware\n");
1248 if (ctlr->auto_runtime_pm) {
1249 pm_runtime_mark_last_busy(ctlr->dev.parent);
1250 pm_runtime_put_autosuspend(ctlr->dev.parent);
1252 trace_spi_controller_idle(ctlr);
1254 spin_lock_irqsave(&ctlr->queue_lock, flags);
1255 ctlr->idling = false;
1256 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1260 /* Extract head of queue */
1262 list_first_entry(&ctlr->queue, struct spi_message, queue);
1264 list_del_init(&ctlr->cur_msg->queue);
1269 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1271 mutex_lock(&ctlr->io_mutex);
1273 if (!was_busy && ctlr->auto_runtime_pm) {
1274 ret = pm_runtime_get_sync(ctlr->dev.parent);
1276 pm_runtime_put_noidle(ctlr->dev.parent);
1277 dev_err(&ctlr->dev, "Failed to power device: %d\n",
1279 mutex_unlock(&ctlr->io_mutex);
1285 trace_spi_controller_busy(ctlr);
1287 if (!was_busy && ctlr->prepare_transfer_hardware) {
1288 ret = ctlr->prepare_transfer_hardware(ctlr);
1291 "failed to prepare transfer hardware\n");
1293 if (ctlr->auto_runtime_pm)
1294 pm_runtime_put(ctlr->dev.parent);
1295 mutex_unlock(&ctlr->io_mutex);
1300 trace_spi_message_start(ctlr->cur_msg);
1302 if (ctlr->prepare_message) {
1303 ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);
1305 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1307 ctlr->cur_msg->status = ret;
1308 spi_finalize_current_message(ctlr);
1311 ctlr->cur_msg_prepared = true;
1314 ret = spi_map_msg(ctlr, ctlr->cur_msg);
1316 ctlr->cur_msg->status = ret;
1317 spi_finalize_current_message(ctlr);
1321 ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg);
1324 "failed to transfer one message from queue\n");
1329 mutex_unlock(&ctlr->io_mutex);
1331 /* Prod the scheduler in case transfer_one() was busy waiting */
1337 * spi_pump_messages - kthread work function which processes spi message queue
1338 * @work: pointer to kthread work struct contained in the controller struct
1340 static void spi_pump_messages(struct kthread_work *work)
1342 struct spi_controller *ctlr =
1343 container_of(work, struct spi_controller, pump_messages);
1345 __spi_pump_messages(ctlr, true);
1348 static int spi_init_queue(struct spi_controller *ctlr)
1350 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1352 ctlr->running = false;
1355 kthread_init_worker(&ctlr->kworker);
1356 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1357 "%s", dev_name(&ctlr->dev));
1358 if (IS_ERR(ctlr->kworker_task)) {
1359 dev_err(&ctlr->dev, "failed to create message pump task\n");
1360 return PTR_ERR(ctlr->kworker_task);
1362 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
1365 * Controller config will indicate if this controller should run the
1366 * message pump with high (realtime) priority to reduce the transfer
1367 * latency on the bus by minimising the delay between a transfer
1368 * request and the scheduling of the message pump thread. Without this
1369 * setting the message pump thread will remain at default priority.
1372 dev_info(&ctlr->dev,
1373 "will run message pump with realtime priority\n");
1374 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, ¶m);
1381 * spi_get_next_queued_message() - called by driver to check for queued
1383 * @ctlr: the controller to check for queued messages
1385 * If there are more messages in the queue, the next message is returned from
1388 * Return: the next message in the queue, else NULL if the queue is empty.
1390 struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
1392 struct spi_message *next;
1393 unsigned long flags;
1395 /* get a pointer to the next message, if any */
1396 spin_lock_irqsave(&ctlr->queue_lock, flags);
1397 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
1399 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1403 EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1406 * spi_finalize_current_message() - the current message is complete
1407 * @ctlr: the controller to return the message to
1409 * Called by the driver to notify the core that the message in the front of the
1410 * queue is complete and can be removed from the queue.
1412 void spi_finalize_current_message(struct spi_controller *ctlr)
1414 struct spi_message *mesg;
1415 unsigned long flags;
1418 spin_lock_irqsave(&ctlr->queue_lock, flags);
1419 mesg = ctlr->cur_msg;
1420 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1422 spi_unmap_msg(ctlr, mesg);
1424 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1425 ret = ctlr->unprepare_message(ctlr, mesg);
1427 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1432 spin_lock_irqsave(&ctlr->queue_lock, flags);
1433 ctlr->cur_msg = NULL;
1434 ctlr->cur_msg_prepared = false;
1435 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1436 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1438 trace_spi_message_done(mesg);
1442 mesg->complete(mesg->context);
1444 EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1446 static int spi_start_queue(struct spi_controller *ctlr)
1448 unsigned long flags;
1450 spin_lock_irqsave(&ctlr->queue_lock, flags);
1452 if (ctlr->running || ctlr->busy) {
1453 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1457 ctlr->running = true;
1458 ctlr->cur_msg = NULL;
1459 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1461 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1466 static int spi_stop_queue(struct spi_controller *ctlr)
1468 unsigned long flags;
1469 unsigned limit = 500;
1472 spin_lock_irqsave(&ctlr->queue_lock, flags);
1475 * This is a bit lame, but is optimized for the common execution path.
1476 * A wait_queue on the ctlr->busy could be used, but then the common
1477 * execution path (pump_messages) would be required to call wake_up or
1478 * friends on every SPI message. Do this instead.
1480 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1481 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1482 usleep_range(10000, 11000);
1483 spin_lock_irqsave(&ctlr->queue_lock, flags);
1486 if (!list_empty(&ctlr->queue) || ctlr->busy)
1489 ctlr->running = false;
1491 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1494 dev_warn(&ctlr->dev, "could not stop message queue\n");
1500 static int spi_destroy_queue(struct spi_controller *ctlr)
1504 ret = spi_stop_queue(ctlr);
1507 * kthread_flush_worker will block until all work is done.
1508 * If the reason that stop_queue timed out is that the work will never
1509 * finish, then it does no good to call flush/stop thread, so
1513 dev_err(&ctlr->dev, "problem destroying queue\n");
1517 kthread_flush_worker(&ctlr->kworker);
1518 kthread_stop(ctlr->kworker_task);
1523 static int __spi_queued_transfer(struct spi_device *spi,
1524 struct spi_message *msg,
1527 struct spi_controller *ctlr = spi->controller;
1528 unsigned long flags;
1530 spin_lock_irqsave(&ctlr->queue_lock, flags);
1532 if (!ctlr->running) {
1533 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1536 msg->actual_length = 0;
1537 msg->status = -EINPROGRESS;
1539 list_add_tail(&msg->queue, &ctlr->queue);
1540 if (!ctlr->busy && need_pump)
1541 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1543 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1548 * spi_queued_transfer - transfer function for queued transfers
1549 * @spi: spi device which is requesting transfer
1550 * @msg: spi message which is to handled is queued to driver queue
1552 * Return: zero on success, else a negative error code.
1554 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1556 return __spi_queued_transfer(spi, msg, true);
1559 static int spi_controller_initialize_queue(struct spi_controller *ctlr)
1563 ctlr->transfer = spi_queued_transfer;
1564 if (!ctlr->transfer_one_message)
1565 ctlr->transfer_one_message = spi_transfer_one_message;
1567 /* Initialize and start queue */
1568 ret = spi_init_queue(ctlr);
1570 dev_err(&ctlr->dev, "problem initializing queue\n");
1571 goto err_init_queue;
1573 ctlr->queued = true;
1574 ret = spi_start_queue(ctlr);
1576 dev_err(&ctlr->dev, "problem starting queue\n");
1577 goto err_start_queue;
1583 spi_destroy_queue(ctlr);
1589 * spi_flush_queue - Send all pending messages in the queue from the callers'
1591 * @ctlr: controller to process queue for
1593 * This should be used when one wants to ensure all pending messages have been
1594 * sent before doing something. Is used by the spi-mem code to make sure SPI
1595 * memory operations do not preempt regular SPI transfers that have been queued
1596 * before the spi-mem operation.
1598 void spi_flush_queue(struct spi_controller *ctlr)
1600 if (ctlr->transfer == spi_queued_transfer)
1601 __spi_pump_messages(ctlr, false);
1604 /*-------------------------------------------------------------------------*/
1606 #if defined(CONFIG_OF)
1607 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
1608 struct device_node *nc)
1613 /* Mode (clock phase/polarity/etc.) */
1614 if (of_property_read_bool(nc, "spi-cpha"))
1615 spi->mode |= SPI_CPHA;
1616 if (of_property_read_bool(nc, "spi-cpol"))
1617 spi->mode |= SPI_CPOL;
1618 if (of_property_read_bool(nc, "spi-cs-high"))
1619 spi->mode |= SPI_CS_HIGH;
1620 if (of_property_read_bool(nc, "spi-3wire"))
1621 spi->mode |= SPI_3WIRE;
1622 if (of_property_read_bool(nc, "spi-lsb-first"))
1623 spi->mode |= SPI_LSB_FIRST;
1625 /* Device DUAL/QUAD mode */
1626 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1631 spi->mode |= SPI_TX_DUAL;
1634 spi->mode |= SPI_TX_QUAD;
1637 spi->mode |= SPI_TX_OCTAL;
1640 dev_warn(&ctlr->dev,
1641 "spi-tx-bus-width %d not supported\n",
1647 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1652 spi->mode |= SPI_RX_DUAL;
1655 spi->mode |= SPI_RX_QUAD;
1658 spi->mode |= SPI_RX_OCTAL;
1661 dev_warn(&ctlr->dev,
1662 "spi-rx-bus-width %d not supported\n",
1668 if (spi_controller_is_slave(ctlr)) {
1669 if (!of_node_name_eq(nc, "slave")) {
1670 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1677 /* Device address */
1678 rc = of_property_read_u32(nc, "reg", &value);
1680 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1684 spi->chip_select = value;
1687 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1690 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
1693 spi->max_speed_hz = value;
1698 static struct spi_device *
1699 of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
1701 struct spi_device *spi;
1704 /* Alloc an spi_device */
1705 spi = spi_alloc_device(ctlr);
1707 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
1712 /* Select device driver */
1713 rc = of_modalias_node(nc, spi->modalias,
1714 sizeof(spi->modalias));
1716 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
1720 rc = of_spi_parse_dt(ctlr, spi, nc);
1724 /* Store a pointer to the node in the device structure */
1726 spi->dev.of_node = nc;
1728 /* Register the new device */
1729 rc = spi_add_device(spi);
1731 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
1732 goto err_of_node_put;
1745 * of_register_spi_devices() - Register child devices onto the SPI bus
1746 * @ctlr: Pointer to spi_controller device
1748 * Registers an spi_device for each child node of controller node which
1749 * represents a valid SPI slave.
1751 static void of_register_spi_devices(struct spi_controller *ctlr)
1753 struct spi_device *spi;
1754 struct device_node *nc;
1756 if (!ctlr->dev.of_node)
1759 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
1760 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1762 spi = of_register_spi_device(ctlr, nc);
1764 dev_warn(&ctlr->dev,
1765 "Failed to create SPI device for %pOF\n", nc);
1766 of_node_clear_flag(nc, OF_POPULATED);
1771 static void of_register_spi_devices(struct spi_controller *ctlr) { }
1775 static void acpi_spi_parse_apple_properties(struct spi_device *spi)
1777 struct acpi_device *dev = ACPI_COMPANION(&spi->dev);
1778 const union acpi_object *obj;
1780 if (!x86_apple_machine)
1783 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
1784 && obj->buffer.length >= 4)
1785 spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
1787 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
1788 && obj->buffer.length == 8)
1789 spi->bits_per_word = *(u64 *)obj->buffer.pointer;
1791 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
1792 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
1793 spi->mode |= SPI_LSB_FIRST;
1795 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
1796 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1797 spi->mode |= SPI_CPOL;
1799 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
1800 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1801 spi->mode |= SPI_CPHA;
1804 static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1806 struct spi_device *spi = data;
1807 struct spi_controller *ctlr = spi->controller;
1809 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1810 struct acpi_resource_spi_serialbus *sb;
1812 sb = &ares->data.spi_serial_bus;
1813 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1815 * ACPI DeviceSelection numbering is handled by the
1816 * host controller driver in Windows and can vary
1817 * from driver to driver. In Linux we always expect
1818 * 0 .. max - 1 so we need to ask the driver to
1819 * translate between the two schemes.
1821 if (ctlr->fw_translate_cs) {
1822 int cs = ctlr->fw_translate_cs(ctlr,
1823 sb->device_selection);
1826 spi->chip_select = cs;
1828 spi->chip_select = sb->device_selection;
1831 spi->max_speed_hz = sb->connection_speed;
1833 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1834 spi->mode |= SPI_CPHA;
1835 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1836 spi->mode |= SPI_CPOL;
1837 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1838 spi->mode |= SPI_CS_HIGH;
1840 } else if (spi->irq < 0) {
1843 if (acpi_dev_resource_interrupt(ares, 0, &r))
1847 /* Always tell the ACPI core to skip this resource */
1851 static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
1852 struct acpi_device *adev)
1854 struct list_head resource_list;
1855 struct spi_device *spi;
1858 if (acpi_bus_get_status(adev) || !adev->status.present ||
1859 acpi_device_enumerated(adev))
1862 spi = spi_alloc_device(ctlr);
1864 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
1865 dev_name(&adev->dev));
1866 return AE_NO_MEMORY;
1869 ACPI_COMPANION_SET(&spi->dev, adev);
1872 INIT_LIST_HEAD(&resource_list);
1873 ret = acpi_dev_get_resources(adev, &resource_list,
1874 acpi_spi_add_resource, spi);
1875 acpi_dev_free_resource_list(&resource_list);
1877 acpi_spi_parse_apple_properties(spi);
1879 if (ret < 0 || !spi->max_speed_hz) {
1884 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1885 sizeof(spi->modalias));
1888 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1890 acpi_device_set_enumerated(adev);
1892 adev->power.flags.ignore_parent = true;
1893 if (spi_add_device(spi)) {
1894 adev->power.flags.ignore_parent = false;
1895 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
1896 dev_name(&adev->dev));
1903 static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1904 void *data, void **return_value)
1906 struct spi_controller *ctlr = data;
1907 struct acpi_device *adev;
1909 if (acpi_bus_get_device(handle, &adev))
1912 return acpi_register_spi_device(ctlr, adev);
1915 static void acpi_register_spi_devices(struct spi_controller *ctlr)
1920 handle = ACPI_HANDLE(ctlr->dev.parent);
1924 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1925 acpi_spi_add_device, NULL, ctlr, NULL);
1926 if (ACPI_FAILURE(status))
1927 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
1930 static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
1931 #endif /* CONFIG_ACPI */
1933 static void spi_controller_release(struct device *dev)
1935 struct spi_controller *ctlr;
1937 ctlr = container_of(dev, struct spi_controller, dev);
1941 static struct class spi_master_class = {
1942 .name = "spi_master",
1943 .owner = THIS_MODULE,
1944 .dev_release = spi_controller_release,
1945 .dev_groups = spi_master_groups,
1948 #ifdef CONFIG_SPI_SLAVE
1950 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
1952 * @spi: device used for the current transfer
1954 int spi_slave_abort(struct spi_device *spi)
1956 struct spi_controller *ctlr = spi->controller;
1958 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
1959 return ctlr->slave_abort(ctlr);
1963 EXPORT_SYMBOL_GPL(spi_slave_abort);
1965 static int match_true(struct device *dev, void *data)
1970 static ssize_t spi_slave_show(struct device *dev,
1971 struct device_attribute *attr, char *buf)
1973 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1975 struct device *child;
1977 child = device_find_child(&ctlr->dev, NULL, match_true);
1978 return sprintf(buf, "%s\n",
1979 child ? to_spi_device(child)->modalias : NULL);
1982 static ssize_t spi_slave_store(struct device *dev,
1983 struct device_attribute *attr, const char *buf,
1986 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1988 struct spi_device *spi;
1989 struct device *child;
1993 rc = sscanf(buf, "%31s", name);
1994 if (rc != 1 || !name[0])
1997 child = device_find_child(&ctlr->dev, NULL, match_true);
1999 /* Remove registered slave */
2000 device_unregister(child);
2004 if (strcmp(name, "(null)")) {
2005 /* Register new slave */
2006 spi = spi_alloc_device(ctlr);
2010 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2012 rc = spi_add_device(spi);
2022 static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store);
2024 static struct attribute *spi_slave_attrs[] = {
2025 &dev_attr_slave.attr,
2029 static const struct attribute_group spi_slave_group = {
2030 .attrs = spi_slave_attrs,
2033 static const struct attribute_group *spi_slave_groups[] = {
2034 &spi_controller_statistics_group,
2039 static struct class spi_slave_class = {
2040 .name = "spi_slave",
2041 .owner = THIS_MODULE,
2042 .dev_release = spi_controller_release,
2043 .dev_groups = spi_slave_groups,
2046 extern struct class spi_slave_class; /* dummy */
2050 * __spi_alloc_controller - allocate an SPI master or slave controller
2051 * @dev: the controller, possibly using the platform_bus
2052 * @size: how much zeroed driver-private data to allocate; the pointer to this
2053 * memory is in the driver_data field of the returned device,
2054 * accessible with spi_controller_get_devdata().
2055 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2056 * slave (true) controller
2057 * Context: can sleep
2059 * This call is used only by SPI controller drivers, which are the
2060 * only ones directly touching chip registers. It's how they allocate
2061 * an spi_controller structure, prior to calling spi_register_controller().
2063 * This must be called from context that can sleep.
2065 * The caller is responsible for assigning the bus number and initializing the
2066 * controller's methods before calling spi_register_controller(); and (after
2067 * errors adding the device) calling spi_controller_put() to prevent a memory
2070 * Return: the SPI controller structure on success, else NULL.
2072 struct spi_controller *__spi_alloc_controller(struct device *dev,
2073 unsigned int size, bool slave)
2075 struct spi_controller *ctlr;
2080 ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL);
2084 device_initialize(&ctlr->dev);
2086 ctlr->num_chipselect = 1;
2087 ctlr->slave = slave;
2088 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
2089 ctlr->dev.class = &spi_slave_class;
2091 ctlr->dev.class = &spi_master_class;
2092 ctlr->dev.parent = dev;
2093 pm_suspend_ignore_children(&ctlr->dev, true);
2094 spi_controller_set_devdata(ctlr, &ctlr[1]);
2098 EXPORT_SYMBOL_GPL(__spi_alloc_controller);
2101 static int of_spi_register_master(struct spi_controller *ctlr)
2104 struct device_node *np = ctlr->dev.of_node;
2109 nb = of_gpio_named_count(np, "cs-gpios");
2110 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2112 /* Return error only for an incorrectly formed cs-gpios property */
2113 if (nb == 0 || nb == -ENOENT)
2118 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
2120 ctlr->cs_gpios = cs;
2122 if (!ctlr->cs_gpios)
2125 for (i = 0; i < ctlr->num_chipselect; i++)
2128 for (i = 0; i < nb; i++)
2129 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2134 static int of_spi_register_master(struct spi_controller *ctlr)
2140 static int spi_controller_check_ops(struct spi_controller *ctlr)
2143 * The controller may implement only the high-level SPI-memory like
2144 * operations if it does not support regular SPI transfers, and this is
2146 * If ->mem_ops is NULL, we request that at least one of the
2147 * ->transfer_xxx() method be implemented.
2149 if (ctlr->mem_ops) {
2150 if (!ctlr->mem_ops->exec_op)
2152 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2153 !ctlr->transfer_one_message) {
2161 * spi_register_controller - register SPI master or slave controller
2162 * @ctlr: initialized master, originally from spi_alloc_master() or
2164 * Context: can sleep
2166 * SPI controllers connect to their drivers using some non-SPI bus,
2167 * such as the platform bus. The final stage of probe() in that code
2168 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2170 * SPI controllers use board specific (often SOC specific) bus numbers,
2171 * and board-specific addressing for SPI devices combines those numbers
2172 * with chip select numbers. Since SPI does not directly support dynamic
2173 * device identification, boards need configuration tables telling which
2174 * chip is at which address.
2176 * This must be called from context that can sleep. It returns zero on
2177 * success, else a negative error code (dropping the controller's refcount).
2178 * After a successful return, the caller is responsible for calling
2179 * spi_unregister_controller().
2181 * Return: zero on success, else a negative error code.
2183 int spi_register_controller(struct spi_controller *ctlr)
2185 struct device *dev = ctlr->dev.parent;
2186 struct boardinfo *bi;
2187 int status = -ENODEV;
2188 int id, first_dynamic;
2194 * Make sure all necessary hooks are implemented before registering
2195 * the SPI controller.
2197 status = spi_controller_check_ops(ctlr);
2201 if (!spi_controller_is_slave(ctlr)) {
2202 status = of_spi_register_master(ctlr);
2207 /* even if it's just one always-selected device, there must
2208 * be at least one chipselect
2210 if (ctlr->num_chipselect == 0)
2212 if (ctlr->bus_num >= 0) {
2213 /* devices with a fixed bus num must check-in with the num */
2214 mutex_lock(&board_lock);
2215 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2216 ctlr->bus_num + 1, GFP_KERNEL);
2217 mutex_unlock(&board_lock);
2218 if (WARN(id < 0, "couldn't get idr"))
2219 return id == -ENOSPC ? -EBUSY : id;
2221 } else if (ctlr->dev.of_node) {
2222 /* allocate dynamic bus number using Linux idr */
2223 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2226 mutex_lock(&board_lock);
2227 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2228 ctlr->bus_num + 1, GFP_KERNEL);
2229 mutex_unlock(&board_lock);
2230 if (WARN(id < 0, "couldn't get idr"))
2231 return id == -ENOSPC ? -EBUSY : id;
2234 if (ctlr->bus_num < 0) {
2235 first_dynamic = of_alias_get_highest_id("spi");
2236 if (first_dynamic < 0)
2241 mutex_lock(&board_lock);
2242 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2244 mutex_unlock(&board_lock);
2245 if (WARN(id < 0, "couldn't get idr"))
2249 INIT_LIST_HEAD(&ctlr->queue);
2250 spin_lock_init(&ctlr->queue_lock);
2251 spin_lock_init(&ctlr->bus_lock_spinlock);
2252 mutex_init(&ctlr->bus_lock_mutex);
2253 mutex_init(&ctlr->io_mutex);
2254 ctlr->bus_lock_flag = 0;
2255 init_completion(&ctlr->xfer_completion);
2256 if (!ctlr->max_dma_len)
2257 ctlr->max_dma_len = INT_MAX;
2259 /* register the device, then userspace will see it.
2260 * registration fails if the bus ID is in use.
2262 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2263 status = device_add(&ctlr->dev);
2266 mutex_lock(&board_lock);
2267 idr_remove(&spi_master_idr, ctlr->bus_num);
2268 mutex_unlock(&board_lock);
2271 dev_dbg(dev, "registered %s %s\n",
2272 spi_controller_is_slave(ctlr) ? "slave" : "master",
2273 dev_name(&ctlr->dev));
2276 * If we're using a queued driver, start the queue. Note that we don't
2277 * need the queueing logic if the driver is only supporting high-level
2278 * memory operations.
2280 if (ctlr->transfer) {
2281 dev_info(dev, "controller is unqueued, this is deprecated\n");
2282 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
2283 status = spi_controller_initialize_queue(ctlr);
2285 device_del(&ctlr->dev);
2287 mutex_lock(&board_lock);
2288 idr_remove(&spi_master_idr, ctlr->bus_num);
2289 mutex_unlock(&board_lock);
2293 /* add statistics */
2294 spin_lock_init(&ctlr->statistics.lock);
2296 mutex_lock(&board_lock);
2297 list_add_tail(&ctlr->list, &spi_controller_list);
2298 list_for_each_entry(bi, &board_list, list)
2299 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
2300 mutex_unlock(&board_lock);
2302 /* Register devices from the device tree and ACPI */
2303 of_register_spi_devices(ctlr);
2304 acpi_register_spi_devices(ctlr);
2308 EXPORT_SYMBOL_GPL(spi_register_controller);
2310 static void devm_spi_unregister(struct device *dev, void *res)
2312 spi_unregister_controller(*(struct spi_controller **)res);
2316 * devm_spi_register_controller - register managed SPI master or slave
2318 * @dev: device managing SPI controller
2319 * @ctlr: initialized controller, originally from spi_alloc_master() or
2321 * Context: can sleep
2323 * Register a SPI device as with spi_register_controller() which will
2324 * automatically be unregistered and freed.
2326 * Return: zero on success, else a negative error code.
2328 int devm_spi_register_controller(struct device *dev,
2329 struct spi_controller *ctlr)
2331 struct spi_controller **ptr;
2334 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2338 ret = spi_register_controller(ctlr);
2341 devres_add(dev, ptr);
2348 EXPORT_SYMBOL_GPL(devm_spi_register_controller);
2350 static int __unregister(struct device *dev, void *null)
2352 spi_unregister_device(to_spi_device(dev));
2357 * spi_unregister_controller - unregister SPI master or slave controller
2358 * @ctlr: the controller being unregistered
2359 * Context: can sleep
2361 * This call is used only by SPI controller drivers, which are the
2362 * only ones directly touching chip registers.
2364 * This must be called from context that can sleep.
2366 * Note that this function also drops a reference to the controller.
2368 void spi_unregister_controller(struct spi_controller *ctlr)
2370 struct spi_controller *found;
2371 int id = ctlr->bus_num;
2374 /* First make sure that this controller was ever added */
2375 mutex_lock(&board_lock);
2376 found = idr_find(&spi_master_idr, id);
2377 mutex_unlock(&board_lock);
2379 if (spi_destroy_queue(ctlr))
2380 dev_err(&ctlr->dev, "queue remove failed\n");
2382 mutex_lock(&board_lock);
2383 list_del(&ctlr->list);
2384 mutex_unlock(&board_lock);
2386 dummy = device_for_each_child(&ctlr->dev, NULL, __unregister);
2387 device_unregister(&ctlr->dev);
2389 mutex_lock(&board_lock);
2391 idr_remove(&spi_master_idr, id);
2392 mutex_unlock(&board_lock);
2394 EXPORT_SYMBOL_GPL(spi_unregister_controller);
2396 int spi_controller_suspend(struct spi_controller *ctlr)
2400 /* Basically no-ops for non-queued controllers */
2404 ret = spi_stop_queue(ctlr);
2406 dev_err(&ctlr->dev, "queue stop failed\n");
2410 EXPORT_SYMBOL_GPL(spi_controller_suspend);
2412 int spi_controller_resume(struct spi_controller *ctlr)
2419 ret = spi_start_queue(ctlr);
2421 dev_err(&ctlr->dev, "queue restart failed\n");
2425 EXPORT_SYMBOL_GPL(spi_controller_resume);
2427 static int __spi_controller_match(struct device *dev, const void *data)
2429 struct spi_controller *ctlr;
2430 const u16 *bus_num = data;
2432 ctlr = container_of(dev, struct spi_controller, dev);
2433 return ctlr->bus_num == *bus_num;
2437 * spi_busnum_to_master - look up master associated with bus_num
2438 * @bus_num: the master's bus number
2439 * Context: can sleep
2441 * This call may be used with devices that are registered after
2442 * arch init time. It returns a refcounted pointer to the relevant
2443 * spi_controller (which the caller must release), or NULL if there is
2444 * no such master registered.
2446 * Return: the SPI master structure on success, else NULL.
2448 struct spi_controller *spi_busnum_to_master(u16 bus_num)
2451 struct spi_controller *ctlr = NULL;
2453 dev = class_find_device(&spi_master_class, NULL, &bus_num,
2454 __spi_controller_match);
2456 ctlr = container_of(dev, struct spi_controller, dev);
2457 /* reference got in class_find_device */
2460 EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2462 /*-------------------------------------------------------------------------*/
2464 /* Core methods for SPI resource management */
2467 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2468 * during the processing of a spi_message while using
2470 * @spi: the spi device for which we allocate memory
2471 * @release: the release code to execute for this resource
2472 * @size: size to alloc and return
2473 * @gfp: GFP allocation flags
2475 * Return: the pointer to the allocated data
2477 * This may get enhanced in the future to allocate from a memory pool
2478 * of the @spi_device or @spi_controller to avoid repeated allocations.
2480 void *spi_res_alloc(struct spi_device *spi,
2481 spi_res_release_t release,
2482 size_t size, gfp_t gfp)
2484 struct spi_res *sres;
2486 sres = kzalloc(sizeof(*sres) + size, gfp);
2490 INIT_LIST_HEAD(&sres->entry);
2491 sres->release = release;
2495 EXPORT_SYMBOL_GPL(spi_res_alloc);
2498 * spi_res_free - free an spi resource
2499 * @res: pointer to the custom data of a resource
2502 void spi_res_free(void *res)
2504 struct spi_res *sres = container_of(res, struct spi_res, data);
2509 WARN_ON(!list_empty(&sres->entry));
2512 EXPORT_SYMBOL_GPL(spi_res_free);
2515 * spi_res_add - add a spi_res to the spi_message
2516 * @message: the spi message
2517 * @res: the spi_resource
2519 void spi_res_add(struct spi_message *message, void *res)
2521 struct spi_res *sres = container_of(res, struct spi_res, data);
2523 WARN_ON(!list_empty(&sres->entry));
2524 list_add_tail(&sres->entry, &message->resources);
2526 EXPORT_SYMBOL_GPL(spi_res_add);
2529 * spi_res_release - release all spi resources for this message
2530 * @ctlr: the @spi_controller
2531 * @message: the @spi_message
2533 void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
2535 struct spi_res *res;
2537 while (!list_empty(&message->resources)) {
2538 res = list_last_entry(&message->resources,
2539 struct spi_res, entry);
2542 res->release(ctlr, message, res->data);
2544 list_del(&res->entry);
2549 EXPORT_SYMBOL_GPL(spi_res_release);
2551 /*-------------------------------------------------------------------------*/
2553 /* Core methods for spi_message alterations */
2555 static void __spi_replace_transfers_release(struct spi_controller *ctlr,
2556 struct spi_message *msg,
2559 struct spi_replaced_transfers *rxfer = res;
2562 /* call extra callback if requested */
2564 rxfer->release(ctlr, msg, res);
2566 /* insert replaced transfers back into the message */
2567 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2569 /* remove the formerly inserted entries */
2570 for (i = 0; i < rxfer->inserted; i++)
2571 list_del(&rxfer->inserted_transfers[i].transfer_list);
2575 * spi_replace_transfers - replace transfers with several transfers
2576 * and register change with spi_message.resources
2577 * @msg: the spi_message we work upon
2578 * @xfer_first: the first spi_transfer we want to replace
2579 * @remove: number of transfers to remove
2580 * @insert: the number of transfers we want to insert instead
2581 * @release: extra release code necessary in some circumstances
2582 * @extradatasize: extra data to allocate (with alignment guarantees
2583 * of struct @spi_transfer)
2586 * Returns: pointer to @spi_replaced_transfers,
2587 * PTR_ERR(...) in case of errors.
2589 struct spi_replaced_transfers *spi_replace_transfers(
2590 struct spi_message *msg,
2591 struct spi_transfer *xfer_first,
2594 spi_replaced_release_t release,
2595 size_t extradatasize,
2598 struct spi_replaced_transfers *rxfer;
2599 struct spi_transfer *xfer;
2602 /* allocate the structure using spi_res */
2603 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2604 insert * sizeof(struct spi_transfer)
2605 + sizeof(struct spi_replaced_transfers)
2609 return ERR_PTR(-ENOMEM);
2611 /* the release code to invoke before running the generic release */
2612 rxfer->release = release;
2614 /* assign extradata */
2617 &rxfer->inserted_transfers[insert];
2619 /* init the replaced_transfers list */
2620 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2622 /* assign the list_entry after which we should reinsert
2623 * the @replaced_transfers - it may be spi_message.messages!
2625 rxfer->replaced_after = xfer_first->transfer_list.prev;
2627 /* remove the requested number of transfers */
2628 for (i = 0; i < remove; i++) {
2629 /* if the entry after replaced_after it is msg->transfers
2630 * then we have been requested to remove more transfers
2631 * than are in the list
2633 if (rxfer->replaced_after->next == &msg->transfers) {
2634 dev_err(&msg->spi->dev,
2635 "requested to remove more spi_transfers than are available\n");
2636 /* insert replaced transfers back into the message */
2637 list_splice(&rxfer->replaced_transfers,
2638 rxfer->replaced_after);
2640 /* free the spi_replace_transfer structure */
2641 spi_res_free(rxfer);
2643 /* and return with an error */
2644 return ERR_PTR(-EINVAL);
2647 /* remove the entry after replaced_after from list of
2648 * transfers and add it to list of replaced_transfers
2650 list_move_tail(rxfer->replaced_after->next,
2651 &rxfer->replaced_transfers);
2654 /* create copy of the given xfer with identical settings
2655 * based on the first transfer to get removed
2657 for (i = 0; i < insert; i++) {
2658 /* we need to run in reverse order */
2659 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2661 /* copy all spi_transfer data */
2662 memcpy(xfer, xfer_first, sizeof(*xfer));
2665 list_add(&xfer->transfer_list, rxfer->replaced_after);
2667 /* clear cs_change and delay_usecs for all but the last */
2669 xfer->cs_change = false;
2670 xfer->delay_usecs = 0;
2674 /* set up inserted */
2675 rxfer->inserted = insert;
2677 /* and register it with spi_res/spi_message */
2678 spi_res_add(msg, rxfer);
2682 EXPORT_SYMBOL_GPL(spi_replace_transfers);
2684 static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
2685 struct spi_message *msg,
2686 struct spi_transfer **xferp,
2690 struct spi_transfer *xfer = *xferp, *xfers;
2691 struct spi_replaced_transfers *srt;
2695 /* warn once about this fact that we are splitting a transfer */
2696 dev_warn_once(&msg->spi->dev,
2697 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
2698 xfer->len, maxsize);
2700 /* calculate how many we have to replace */
2701 count = DIV_ROUND_UP(xfer->len, maxsize);
2703 /* create replacement */
2704 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
2706 return PTR_ERR(srt);
2707 xfers = srt->inserted_transfers;
2709 /* now handle each of those newly inserted spi_transfers
2710 * note that the replacements spi_transfers all are preset
2711 * to the same values as *xferp, so tx_buf, rx_buf and len
2712 * are all identical (as well as most others)
2713 * so we just have to fix up len and the pointers.
2715 * this also includes support for the depreciated
2716 * spi_message.is_dma_mapped interface
2719 /* the first transfer just needs the length modified, so we
2720 * run it outside the loop
2722 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
2724 /* all the others need rx_buf/tx_buf also set */
2725 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2726 /* update rx_buf, tx_buf and dma */
2727 if (xfers[i].rx_buf)
2728 xfers[i].rx_buf += offset;
2729 if (xfers[i].rx_dma)
2730 xfers[i].rx_dma += offset;
2731 if (xfers[i].tx_buf)
2732 xfers[i].tx_buf += offset;
2733 if (xfers[i].tx_dma)
2734 xfers[i].tx_dma += offset;
2737 xfers[i].len = min(maxsize, xfers[i].len - offset);
2740 /* we set up xferp to the last entry we have inserted,
2741 * so that we skip those already split transfers
2743 *xferp = &xfers[count - 1];
2745 /* increment statistics counters */
2746 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
2747 transfers_split_maxsize);
2748 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2749 transfers_split_maxsize);
2755 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2756 * when an individual transfer exceeds a
2758 * @ctlr: the @spi_controller for this transfer
2759 * @msg: the @spi_message to transform
2760 * @maxsize: the maximum when to apply this
2761 * @gfp: GFP allocation flags
2763 * Return: status of transformation
2765 int spi_split_transfers_maxsize(struct spi_controller *ctlr,
2766 struct spi_message *msg,
2770 struct spi_transfer *xfer;
2773 /* iterate over the transfer_list,
2774 * but note that xfer is advanced to the last transfer inserted
2775 * to avoid checking sizes again unnecessarily (also xfer does
2776 * potentiall belong to a different list by the time the
2777 * replacement has happened
2779 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2780 if (xfer->len > maxsize) {
2781 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
2790 EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
2792 /*-------------------------------------------------------------------------*/
2794 /* Core methods for SPI controller protocol drivers. Some of the
2795 * other core methods are currently defined as inline functions.
2798 static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
2801 if (ctlr->bits_per_word_mask) {
2802 /* Only 32 bits fit in the mask */
2803 if (bits_per_word > 32)
2805 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
2813 * spi_setup - setup SPI mode and clock rate
2814 * @spi: the device whose settings are being modified
2815 * Context: can sleep, and no requests are queued to the device
2817 * SPI protocol drivers may need to update the transfer mode if the
2818 * device doesn't work with its default. They may likewise need
2819 * to update clock rates or word sizes from initial values. This function
2820 * changes those settings, and must be called from a context that can sleep.
2821 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2822 * effect the next time the device is selected and data is transferred to
2823 * or from it. When this function returns, the spi device is deselected.
2825 * Note that this call will fail if the protocol driver specifies an option
2826 * that the underlying controller or its driver does not support. For
2827 * example, not all hardware supports wire transfers using nine bit words,
2828 * LSB-first wire encoding, or active-high chipselects.
2830 * Return: zero on success, else a negative error code.
2832 int spi_setup(struct spi_device *spi)
2834 unsigned bad_bits, ugly_bits;
2837 /* check mode to prevent that DUAL and QUAD set at the same time
2839 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2840 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2842 "setup: can not select dual and quad at the same time\n");
2845 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2847 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2848 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
2849 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
2851 /* help drivers fail *cleanly* when they need options
2852 * that aren't supported with their current controller
2853 * SPI_CS_WORD has a fallback software implementation,
2854 * so it is ignored here.
2856 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
2857 ugly_bits = bad_bits &
2858 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
2859 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
2862 "setup: ignoring unsupported mode bits %x\n",
2864 spi->mode &= ~ugly_bits;
2865 bad_bits &= ~ugly_bits;
2868 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
2873 if (!spi->bits_per_word)
2874 spi->bits_per_word = 8;
2876 status = __spi_validate_bits_per_word(spi->controller,
2877 spi->bits_per_word);
2881 if (!spi->max_speed_hz)
2882 spi->max_speed_hz = spi->controller->max_speed_hz;
2884 if (spi->controller->setup)
2885 status = spi->controller->setup(spi);
2887 spi_set_cs(spi, false);
2889 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
2890 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2891 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2892 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2893 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2894 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2895 spi->bits_per_word, spi->max_speed_hz,
2900 EXPORT_SYMBOL_GPL(spi_setup);
2902 static int __spi_validate(struct spi_device *spi, struct spi_message *message)
2904 struct spi_controller *ctlr = spi->controller;
2905 struct spi_transfer *xfer;
2908 if (list_empty(&message->transfers))
2911 /* If an SPI controller does not support toggling the CS line on each
2912 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
2913 * for the CS line, we can emulate the CS-per-word hardware function by
2914 * splitting transfers into one-word transfers and ensuring that
2915 * cs_change is set for each transfer.
2917 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
2918 gpio_is_valid(spi->cs_gpio))) {
2922 maxsize = (spi->bits_per_word + 7) / 8;
2924 /* spi_split_transfers_maxsize() requires message->spi */
2927 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
2932 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2933 /* don't change cs_change on the last entry in the list */
2934 if (list_is_last(&xfer->transfer_list, &message->transfers))
2936 xfer->cs_change = 1;
2940 /* Half-duplex links include original MicroWire, and ones with
2941 * only one data pin like SPI_3WIRE (switches direction) or where
2942 * either MOSI or MISO is missing. They can also be caused by
2943 * software limitations.
2945 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
2946 (spi->mode & SPI_3WIRE)) {
2947 unsigned flags = ctlr->flags;
2949 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2950 if (xfer->rx_buf && xfer->tx_buf)
2952 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
2954 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
2960 * Set transfer bits_per_word and max speed as spi device default if
2961 * it is not set for this transfer.
2962 * Set transfer tx_nbits and rx_nbits as single transfer default
2963 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
2965 message->frame_length = 0;
2966 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2967 message->frame_length += xfer->len;
2968 if (!xfer->bits_per_word)
2969 xfer->bits_per_word = spi->bits_per_word;
2971 if (!xfer->speed_hz)
2972 xfer->speed_hz = spi->max_speed_hz;
2973 if (!xfer->speed_hz)
2974 xfer->speed_hz = ctlr->max_speed_hz;
2976 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
2977 xfer->speed_hz = ctlr->max_speed_hz;
2979 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
2983 * SPI transfer length should be multiple of SPI word size
2984 * where SPI word size should be power-of-two multiple
2986 if (xfer->bits_per_word <= 8)
2988 else if (xfer->bits_per_word <= 16)
2993 /* No partial transfers accepted */
2994 if (xfer->len % w_size)
2997 if (xfer->speed_hz && ctlr->min_speed_hz &&
2998 xfer->speed_hz < ctlr->min_speed_hz)
3001 if (xfer->tx_buf && !xfer->tx_nbits)
3002 xfer->tx_nbits = SPI_NBITS_SINGLE;
3003 if (xfer->rx_buf && !xfer->rx_nbits)
3004 xfer->rx_nbits = SPI_NBITS_SINGLE;
3005 /* check transfer tx/rx_nbits:
3006 * 1. check the value matches one of single, dual and quad
3007 * 2. check tx/rx_nbits match the mode in spi_device
3010 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3011 xfer->tx_nbits != SPI_NBITS_DUAL &&
3012 xfer->tx_nbits != SPI_NBITS_QUAD)
3014 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3015 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3017 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3018 !(spi->mode & SPI_TX_QUAD))
3021 /* check transfer rx_nbits */
3023 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3024 xfer->rx_nbits != SPI_NBITS_DUAL &&
3025 xfer->rx_nbits != SPI_NBITS_QUAD)
3027 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3028 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3030 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3031 !(spi->mode & SPI_RX_QUAD))
3036 message->status = -EINPROGRESS;
3041 static int __spi_async(struct spi_device *spi, struct spi_message *message)
3043 struct spi_controller *ctlr = spi->controller;
3046 * Some controllers do not support doing regular SPI transfers. Return
3047 * ENOTSUPP when this is the case.
3049 if (!ctlr->transfer)
3054 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
3055 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3057 trace_spi_message_submit(message);
3059 return ctlr->transfer(spi, message);
3063 * spi_async - asynchronous SPI transfer
3064 * @spi: device with which data will be exchanged
3065 * @message: describes the data transfers, including completion callback
3066 * Context: any (irqs may be blocked, etc)
3068 * This call may be used in_irq and other contexts which can't sleep,
3069 * as well as from task contexts which can sleep.
3071 * The completion callback is invoked in a context which can't sleep.
3072 * Before that invocation, the value of message->status is undefined.
3073 * When the callback is issued, message->status holds either zero (to
3074 * indicate complete success) or a negative error code. After that
3075 * callback returns, the driver which issued the transfer request may
3076 * deallocate the associated memory; it's no longer in use by any SPI
3077 * core or controller driver code.
3079 * Note that although all messages to a spi_device are handled in
3080 * FIFO order, messages may go to different devices in other orders.
3081 * Some device might be higher priority, or have various "hard" access
3082 * time requirements, for example.
3084 * On detection of any fault during the transfer, processing of
3085 * the entire message is aborted, and the device is deselected.
3086 * Until returning from the associated message completion callback,
3087 * no other spi_message queued to that device will be processed.
3088 * (This rule applies equally to all the synchronous transfer calls,
3089 * which are wrappers around this core asynchronous primitive.)
3091 * Return: zero on success, else a negative error code.
3093 int spi_async(struct spi_device *spi, struct spi_message *message)
3095 struct spi_controller *ctlr = spi->controller;
3097 unsigned long flags;
3099 ret = __spi_validate(spi, message);
3103 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3105 if (ctlr->bus_lock_flag)
3108 ret = __spi_async(spi, message);
3110 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3114 EXPORT_SYMBOL_GPL(spi_async);
3117 * spi_async_locked - version of spi_async with exclusive bus usage
3118 * @spi: device with which data will be exchanged
3119 * @message: describes the data transfers, including completion callback
3120 * Context: any (irqs may be blocked, etc)
3122 * This call may be used in_irq and other contexts which can't sleep,
3123 * as well as from task contexts which can sleep.
3125 * The completion callback is invoked in a context which can't sleep.
3126 * Before that invocation, the value of message->status is undefined.
3127 * When the callback is issued, message->status holds either zero (to
3128 * indicate complete success) or a negative error code. After that
3129 * callback returns, the driver which issued the transfer request may
3130 * deallocate the associated memory; it's no longer in use by any SPI
3131 * core or controller driver code.
3133 * Note that although all messages to a spi_device are handled in
3134 * FIFO order, messages may go to different devices in other orders.
3135 * Some device might be higher priority, or have various "hard" access
3136 * time requirements, for example.
3138 * On detection of any fault during the transfer, processing of
3139 * the entire message is aborted, and the device is deselected.
3140 * Until returning from the associated message completion callback,
3141 * no other spi_message queued to that device will be processed.
3142 * (This rule applies equally to all the synchronous transfer calls,
3143 * which are wrappers around this core asynchronous primitive.)
3145 * Return: zero on success, else a negative error code.
3147 int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3149 struct spi_controller *ctlr = spi->controller;
3151 unsigned long flags;
3153 ret = __spi_validate(spi, message);
3157 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3159 ret = __spi_async(spi, message);
3161 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3166 EXPORT_SYMBOL_GPL(spi_async_locked);
3168 /*-------------------------------------------------------------------------*/
3170 /* Utility methods for SPI protocol drivers, layered on
3171 * top of the core. Some other utility methods are defined as
3175 static void spi_complete(void *arg)
3180 static int __spi_sync(struct spi_device *spi, struct spi_message *message)
3182 DECLARE_COMPLETION_ONSTACK(done);
3184 struct spi_controller *ctlr = spi->controller;
3185 unsigned long flags;
3187 status = __spi_validate(spi, message);
3191 message->complete = spi_complete;
3192 message->context = &done;
3195 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
3196 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3198 /* If we're not using the legacy transfer method then we will
3199 * try to transfer in the calling context so special case.
3200 * This code would be less tricky if we could remove the
3201 * support for driver implemented message queues.
3203 if (ctlr->transfer == spi_queued_transfer) {
3204 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3206 trace_spi_message_submit(message);
3208 status = __spi_queued_transfer(spi, message, false);
3210 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3212 status = spi_async_locked(spi, message);
3216 /* Push out the messages in the calling context if we
3219 if (ctlr->transfer == spi_queued_transfer) {
3220 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
3221 spi_sync_immediate);
3222 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3223 spi_sync_immediate);
3224 __spi_pump_messages(ctlr, false);
3227 wait_for_completion(&done);
3228 status = message->status;
3230 message->context = NULL;
3235 * spi_sync - blocking/synchronous SPI data transfers
3236 * @spi: device with which data will be exchanged
3237 * @message: describes the data transfers
3238 * Context: can sleep
3240 * This call may only be used from a context that may sleep. The sleep
3241 * is non-interruptible, and has no timeout. Low-overhead controller
3242 * drivers may DMA directly into and out of the message buffers.
3244 * Note that the SPI device's chip select is active during the message,
3245 * and then is normally disabled between messages. Drivers for some
3246 * frequently-used devices may want to minimize costs of selecting a chip,
3247 * by leaving it selected in anticipation that the next message will go
3248 * to the same chip. (That may increase power usage.)
3250 * Also, the caller is guaranteeing that the memory associated with the
3251 * message will not be freed before this call returns.
3253 * Return: zero on success, else a negative error code.
3255 int spi_sync(struct spi_device *spi, struct spi_message *message)
3259 mutex_lock(&spi->controller->bus_lock_mutex);
3260 ret = __spi_sync(spi, message);
3261 mutex_unlock(&spi->controller->bus_lock_mutex);
3265 EXPORT_SYMBOL_GPL(spi_sync);
3268 * spi_sync_locked - version of spi_sync with exclusive bus usage
3269 * @spi: device with which data will be exchanged
3270 * @message: describes the data transfers
3271 * Context: can sleep
3273 * This call may only be used from a context that may sleep. The sleep
3274 * is non-interruptible, and has no timeout. Low-overhead controller
3275 * drivers may DMA directly into and out of the message buffers.
3277 * This call should be used by drivers that require exclusive access to the
3278 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3279 * be released by a spi_bus_unlock call when the exclusive access is over.
3281 * Return: zero on success, else a negative error code.
3283 int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3285 return __spi_sync(spi, message);
3287 EXPORT_SYMBOL_GPL(spi_sync_locked);
3290 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3291 * @ctlr: SPI bus master that should be locked for exclusive bus access
3292 * Context: can sleep
3294 * This call may only be used from a context that may sleep. The sleep
3295 * is non-interruptible, and has no timeout.
3297 * This call should be used by drivers that require exclusive access to the
3298 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3299 * exclusive access is over. Data transfer must be done by spi_sync_locked
3300 * and spi_async_locked calls when the SPI bus lock is held.
3302 * Return: always zero.
3304 int spi_bus_lock(struct spi_controller *ctlr)
3306 unsigned long flags;
3308 mutex_lock(&ctlr->bus_lock_mutex);
3310 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3311 ctlr->bus_lock_flag = 1;
3312 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3314 /* mutex remains locked until spi_bus_unlock is called */
3318 EXPORT_SYMBOL_GPL(spi_bus_lock);
3321 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3322 * @ctlr: SPI bus master that was locked for exclusive bus access
3323 * Context: can sleep
3325 * This call may only be used from a context that may sleep. The sleep
3326 * is non-interruptible, and has no timeout.
3328 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3331 * Return: always zero.
3333 int spi_bus_unlock(struct spi_controller *ctlr)
3335 ctlr->bus_lock_flag = 0;
3337 mutex_unlock(&ctlr->bus_lock_mutex);
3341 EXPORT_SYMBOL_GPL(spi_bus_unlock);
3343 /* portable code must never pass more than 32 bytes */
3344 #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
3349 * spi_write_then_read - SPI synchronous write followed by read
3350 * @spi: device with which data will be exchanged
3351 * @txbuf: data to be written (need not be dma-safe)
3352 * @n_tx: size of txbuf, in bytes
3353 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3354 * @n_rx: size of rxbuf, in bytes
3355 * Context: can sleep
3357 * This performs a half duplex MicroWire style transaction with the
3358 * device, sending txbuf and then reading rxbuf. The return value
3359 * is zero for success, else a negative errno status code.
3360 * This call may only be used from a context that may sleep.
3362 * Parameters to this routine are always copied using a small buffer;
3363 * portable code should never use this for more than 32 bytes.
3364 * Performance-sensitive or bulk transfer code should instead use
3365 * spi_{async,sync}() calls with dma-safe buffers.
3367 * Return: zero on success, else a negative error code.
3369 int spi_write_then_read(struct spi_device *spi,
3370 const void *txbuf, unsigned n_tx,
3371 void *rxbuf, unsigned n_rx)
3373 static DEFINE_MUTEX(lock);
3376 struct spi_message message;
3377 struct spi_transfer x[2];
3380 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3381 * copying here, (as a pure convenience thing), but we can
3382 * keep heap costs out of the hot path unless someone else is
3383 * using the pre-allocated buffer or the transfer is too large.
3385 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
3386 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3387 GFP_KERNEL | GFP_DMA);
3394 spi_message_init(&message);
3395 memset(x, 0, sizeof(x));
3398 spi_message_add_tail(&x[0], &message);
3402 spi_message_add_tail(&x[1], &message);
3405 memcpy(local_buf, txbuf, n_tx);
3406 x[0].tx_buf = local_buf;
3407 x[1].rx_buf = local_buf + n_tx;
3410 status = spi_sync(spi, &message);
3412 memcpy(rxbuf, x[1].rx_buf, n_rx);
3414 if (x[0].tx_buf == buf)
3415 mutex_unlock(&lock);
3421 EXPORT_SYMBOL_GPL(spi_write_then_read);
3423 /*-------------------------------------------------------------------------*/
3425 #if IS_ENABLED(CONFIG_OF)
3426 static int __spi_of_device_match(struct device *dev, void *data)
3428 return dev->of_node == data;
3431 /* must call put_device() when done with returned spi_device device */
3432 struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3434 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3435 __spi_of_device_match);
3436 return dev ? to_spi_device(dev) : NULL;
3438 EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
3439 #endif /* IS_ENABLED(CONFIG_OF) */
3441 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
3442 static int __spi_of_controller_match(struct device *dev, const void *data)
3444 return dev->of_node == data;
3447 /* the spi controllers are not using spi_bus, so we find it with another way */
3448 static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
3452 dev = class_find_device(&spi_master_class, NULL, node,
3453 __spi_of_controller_match);
3454 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3455 dev = class_find_device(&spi_slave_class, NULL, node,
3456 __spi_of_controller_match);
3460 /* reference got in class_find_device */
3461 return container_of(dev, struct spi_controller, dev);
3464 static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3467 struct of_reconfig_data *rd = arg;
3468 struct spi_controller *ctlr;
3469 struct spi_device *spi;
3471 switch (of_reconfig_get_state_change(action, arg)) {
3472 case OF_RECONFIG_CHANGE_ADD:
3473 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3475 return NOTIFY_OK; /* not for us */
3477 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
3478 put_device(&ctlr->dev);
3482 spi = of_register_spi_device(ctlr, rd->dn);
3483 put_device(&ctlr->dev);
3486 pr_err("%s: failed to create for '%pOF'\n",
3488 of_node_clear_flag(rd->dn, OF_POPULATED);
3489 return notifier_from_errno(PTR_ERR(spi));
3493 case OF_RECONFIG_CHANGE_REMOVE:
3494 /* already depopulated? */
3495 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3498 /* find our device by node */
3499 spi = of_find_spi_device_by_node(rd->dn);
3501 return NOTIFY_OK; /* no? not meant for us */
3503 /* unregister takes one ref away */
3504 spi_unregister_device(spi);
3506 /* and put the reference of the find */
3507 put_device(&spi->dev);
3514 static struct notifier_block spi_of_notifier = {
3515 .notifier_call = of_spi_notify,
3517 #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3518 extern struct notifier_block spi_of_notifier;
3519 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3521 #if IS_ENABLED(CONFIG_ACPI)
3522 static int spi_acpi_controller_match(struct device *dev, const void *data)
3524 return ACPI_COMPANION(dev->parent) == data;
3527 static int spi_acpi_device_match(struct device *dev, void *data)
3529 return ACPI_COMPANION(dev) == data;
3532 static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
3536 dev = class_find_device(&spi_master_class, NULL, adev,
3537 spi_acpi_controller_match);
3538 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3539 dev = class_find_device(&spi_slave_class, NULL, adev,
3540 spi_acpi_controller_match);
3544 return container_of(dev, struct spi_controller, dev);
3547 static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3551 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3553 return dev ? to_spi_device(dev) : NULL;
3556 static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3559 struct acpi_device *adev = arg;
3560 struct spi_controller *ctlr;
3561 struct spi_device *spi;
3564 case ACPI_RECONFIG_DEVICE_ADD:
3565 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3569 acpi_register_spi_device(ctlr, adev);
3570 put_device(&ctlr->dev);
3572 case ACPI_RECONFIG_DEVICE_REMOVE:
3573 if (!acpi_device_enumerated(adev))
3576 spi = acpi_spi_find_device_by_adev(adev);
3580 spi_unregister_device(spi);
3581 put_device(&spi->dev);
3588 static struct notifier_block spi_acpi_notifier = {
3589 .notifier_call = acpi_spi_notify,
3592 extern struct notifier_block spi_acpi_notifier;
3595 static int __init spi_init(void)
3599 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
3605 status = bus_register(&spi_bus_type);
3609 status = class_register(&spi_master_class);
3613 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
3614 status = class_register(&spi_slave_class);
3619 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
3620 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
3621 if (IS_ENABLED(CONFIG_ACPI))
3622 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
3627 class_unregister(&spi_master_class);
3629 bus_unregister(&spi_bus_type);
3637 /* board_info is normally registered in arch_initcall(),
3638 * but even essential drivers wait till later
3640 * REVISIT only boardinfo really needs static linking. the rest (device and
3641 * driver registration) _could_ be dynamically linked (modular) ... costs
3642 * include needing to have boardinfo data structures be much more public.
3644 postcore_initcall(spi_init);