4 * Copyright (C) 2005 David Brownell
5 * Copyright (C) 2008 Secret Lab Technologies Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/cache.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/dmaengine.h>
24 #include <linux/mutex.h>
25 #include <linux/of_device.h>
26 #include <linux/of_irq.h>
27 #include <linux/clk/clk-conf.h>
28 #include <linux/slab.h>
29 #include <linux/mod_devicetable.h>
30 #include <linux/spi/spi.h>
31 #include <linux/of_gpio.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/pm_domain.h>
34 #include <linux/export.h>
35 #include <linux/sched/rt.h>
36 #include <linux/delay.h>
37 #include <linux/kthread.h>
38 #include <linux/ioport.h>
39 #include <linux/acpi.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/spi.h>
44 static void spidev_release(struct device *dev)
46 struct spi_device *spi = to_spi_device(dev);
48 /* spi masters may cleanup for released devices */
49 if (spi->master->cleanup)
50 spi->master->cleanup(spi);
52 spi_master_put(spi->master);
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 #define SPI_STATISTICS_ATTRS(field, file) \
71 static ssize_t spi_master_##field##_show(struct device *dev, \
72 struct device_attribute *attr, \
75 struct spi_master *master = container_of(dev, \
76 struct spi_master, dev); \
77 return spi_statistics_##field##_show(&master->statistics, buf); \
79 static struct device_attribute dev_attr_spi_master_##field = { \
80 .attr = { .name = file, .mode = S_IRUGO }, \
81 .show = spi_master_##field##_show, \
83 static ssize_t spi_device_##field##_show(struct device *dev, \
84 struct device_attribute *attr, \
87 struct spi_device *spi = container_of(dev, \
88 struct spi_device, dev); \
89 return spi_statistics_##field##_show(&spi->statistics, buf); \
91 static struct device_attribute dev_attr_spi_device_##field = { \
92 .attr = { .name = file, .mode = S_IRUGO }, \
93 .show = spi_device_##field##_show, \
96 #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
97 static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
100 unsigned long flags; \
102 spin_lock_irqsave(&stat->lock, flags); \
103 len = sprintf(buf, format_string, stat->field); \
104 spin_unlock_irqrestore(&stat->lock, flags); \
107 SPI_STATISTICS_ATTRS(name, file)
109 #define SPI_STATISTICS_SHOW(field, format_string) \
110 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
111 field, format_string)
113 SPI_STATISTICS_SHOW(messages, "%lu");
114 SPI_STATISTICS_SHOW(transfers, "%lu");
115 SPI_STATISTICS_SHOW(errors, "%lu");
116 SPI_STATISTICS_SHOW(timedout, "%lu");
118 SPI_STATISTICS_SHOW(spi_sync, "%lu");
119 SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
120 SPI_STATISTICS_SHOW(spi_async, "%lu");
122 SPI_STATISTICS_SHOW(bytes, "%llu");
123 SPI_STATISTICS_SHOW(bytes_rx, "%llu");
124 SPI_STATISTICS_SHOW(bytes_tx, "%llu");
126 static struct attribute *spi_dev_attrs[] = {
127 &dev_attr_modalias.attr,
131 static const struct attribute_group spi_dev_group = {
132 .attrs = spi_dev_attrs,
135 static struct attribute *spi_device_statistics_attrs[] = {
136 &dev_attr_spi_device_messages.attr,
137 &dev_attr_spi_device_transfers.attr,
138 &dev_attr_spi_device_errors.attr,
139 &dev_attr_spi_device_timedout.attr,
140 &dev_attr_spi_device_spi_sync.attr,
141 &dev_attr_spi_device_spi_sync_immediate.attr,
142 &dev_attr_spi_device_spi_async.attr,
143 &dev_attr_spi_device_bytes.attr,
144 &dev_attr_spi_device_bytes_rx.attr,
145 &dev_attr_spi_device_bytes_tx.attr,
149 static const struct attribute_group spi_device_statistics_group = {
150 .name = "statistics",
151 .attrs = spi_device_statistics_attrs,
154 static const struct attribute_group *spi_dev_groups[] = {
156 &spi_device_statistics_group,
160 static struct attribute *spi_master_statistics_attrs[] = {
161 &dev_attr_spi_master_messages.attr,
162 &dev_attr_spi_master_transfers.attr,
163 &dev_attr_spi_master_errors.attr,
164 &dev_attr_spi_master_timedout.attr,
165 &dev_attr_spi_master_spi_sync.attr,
166 &dev_attr_spi_master_spi_sync_immediate.attr,
167 &dev_attr_spi_master_spi_async.attr,
168 &dev_attr_spi_master_bytes.attr,
169 &dev_attr_spi_master_bytes_rx.attr,
170 &dev_attr_spi_master_bytes_tx.attr,
174 static const struct attribute_group spi_master_statistics_group = {
175 .name = "statistics",
176 .attrs = spi_master_statistics_attrs,
179 static const struct attribute_group *spi_master_groups[] = {
180 &spi_master_statistics_group,
184 void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
185 struct spi_transfer *xfer,
186 struct spi_master *master)
190 spin_lock_irqsave(&stats->lock, flags);
194 stats->bytes += xfer->len;
195 if ((xfer->tx_buf) &&
196 (xfer->tx_buf != master->dummy_tx))
197 stats->bytes_tx += xfer->len;
198 if ((xfer->rx_buf) &&
199 (xfer->rx_buf != master->dummy_rx))
200 stats->bytes_rx += xfer->len;
202 spin_unlock_irqrestore(&stats->lock, flags);
204 EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
206 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
207 * and the sysfs version makes coldplug work too.
210 static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
211 const struct spi_device *sdev)
213 while (id->name[0]) {
214 if (!strcmp(sdev->modalias, id->name))
221 const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
223 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
225 return spi_match_id(sdrv->id_table, sdev);
227 EXPORT_SYMBOL_GPL(spi_get_device_id);
229 static int spi_match_device(struct device *dev, struct device_driver *drv)
231 const struct spi_device *spi = to_spi_device(dev);
232 const struct spi_driver *sdrv = to_spi_driver(drv);
234 /* Attempt an OF style match */
235 if (of_driver_match_device(dev, drv))
239 if (acpi_driver_match_device(dev, drv))
243 return !!spi_match_id(sdrv->id_table, spi);
245 return strcmp(spi->modalias, drv->name) == 0;
248 static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
250 const struct spi_device *spi = to_spi_device(dev);
253 rc = acpi_device_uevent_modalias(dev, env);
257 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
261 struct bus_type spi_bus_type = {
263 .dev_groups = spi_dev_groups,
264 .match = spi_match_device,
265 .uevent = spi_uevent,
267 EXPORT_SYMBOL_GPL(spi_bus_type);
270 static int spi_drv_probe(struct device *dev)
272 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
275 ret = of_clk_set_defaults(dev->of_node, false);
279 ret = dev_pm_domain_attach(dev, true);
280 if (ret != -EPROBE_DEFER) {
281 ret = sdrv->probe(to_spi_device(dev));
283 dev_pm_domain_detach(dev, true);
289 static int spi_drv_remove(struct device *dev)
291 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
294 ret = sdrv->remove(to_spi_device(dev));
295 dev_pm_domain_detach(dev, true);
300 static void spi_drv_shutdown(struct device *dev)
302 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
304 sdrv->shutdown(to_spi_device(dev));
308 * spi_register_driver - register a SPI driver
309 * @sdrv: the driver to register
312 int spi_register_driver(struct spi_driver *sdrv)
314 sdrv->driver.bus = &spi_bus_type;
316 sdrv->driver.probe = spi_drv_probe;
318 sdrv->driver.remove = spi_drv_remove;
320 sdrv->driver.shutdown = spi_drv_shutdown;
321 return driver_register(&sdrv->driver);
323 EXPORT_SYMBOL_GPL(spi_register_driver);
325 /*-------------------------------------------------------------------------*/
327 /* SPI devices should normally not be created by SPI device drivers; that
328 * would make them board-specific. Similarly with SPI master drivers.
329 * Device registration normally goes into like arch/.../mach.../board-YYY.c
330 * with other readonly (flashable) information about mainboard devices.
334 struct list_head list;
335 struct spi_board_info board_info;
338 static LIST_HEAD(board_list);
339 static LIST_HEAD(spi_master_list);
342 * Used to protect add/del opertion for board_info list and
343 * spi_master list, and their matching process
345 static DEFINE_MUTEX(board_lock);
348 * spi_alloc_device - Allocate a new SPI device
349 * @master: Controller to which device is connected
352 * Allows a driver to allocate and initialize a spi_device without
353 * registering it immediately. This allows a driver to directly
354 * fill the spi_device with device parameters before calling
355 * spi_add_device() on it.
357 * Caller is responsible to call spi_add_device() on the returned
358 * spi_device structure to add it to the SPI master. If the caller
359 * needs to discard the spi_device without adding it, then it should
360 * call spi_dev_put() on it.
362 * Returns a pointer to the new device, or NULL.
364 struct spi_device *spi_alloc_device(struct spi_master *master)
366 struct spi_device *spi;
368 if (!spi_master_get(master))
371 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
373 spi_master_put(master);
377 spi->master = master;
378 spi->dev.parent = &master->dev;
379 spi->dev.bus = &spi_bus_type;
380 spi->dev.release = spidev_release;
381 spi->cs_gpio = -ENOENT;
383 spin_lock_init(&spi->statistics.lock);
385 device_initialize(&spi->dev);
388 EXPORT_SYMBOL_GPL(spi_alloc_device);
390 static void spi_dev_set_name(struct spi_device *spi)
392 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
395 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
399 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
403 static int spi_dev_check(struct device *dev, void *data)
405 struct spi_device *spi = to_spi_device(dev);
406 struct spi_device *new_spi = data;
408 if (spi->master == new_spi->master &&
409 spi->chip_select == new_spi->chip_select)
415 * spi_add_device - Add spi_device allocated with spi_alloc_device
416 * @spi: spi_device to register
418 * Companion function to spi_alloc_device. Devices allocated with
419 * spi_alloc_device can be added onto the spi bus with this function.
421 * Returns 0 on success; negative errno on failure
423 int spi_add_device(struct spi_device *spi)
425 static DEFINE_MUTEX(spi_add_lock);
426 struct spi_master *master = spi->master;
427 struct device *dev = master->dev.parent;
430 /* Chipselects are numbered 0..max; validate. */
431 if (spi->chip_select >= master->num_chipselect) {
432 dev_err(dev, "cs%d >= max %d\n",
434 master->num_chipselect);
438 /* Set the bus ID string */
439 spi_dev_set_name(spi);
441 /* We need to make sure there's no other device with this
442 * chipselect **BEFORE** we call setup(), else we'll trash
443 * its configuration. Lock against concurrent add() calls.
445 mutex_lock(&spi_add_lock);
447 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
449 dev_err(dev, "chipselect %d already in use\n",
454 if (master->cs_gpios)
455 spi->cs_gpio = master->cs_gpios[spi->chip_select];
457 /* Drivers may modify this initial i/o setup, but will
458 * normally rely on the device being setup. Devices
459 * using SPI_CS_HIGH can't coexist well otherwise...
461 status = spi_setup(spi);
463 dev_err(dev, "can't setup %s, status %d\n",
464 dev_name(&spi->dev), status);
468 /* Device may be bound to an active driver when this returns */
469 status = device_add(&spi->dev);
471 dev_err(dev, "can't add %s, status %d\n",
472 dev_name(&spi->dev), status);
474 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
477 mutex_unlock(&spi_add_lock);
480 EXPORT_SYMBOL_GPL(spi_add_device);
483 * spi_new_device - instantiate one new SPI device
484 * @master: Controller to which device is connected
485 * @chip: Describes the SPI device
488 * On typical mainboards, this is purely internal; and it's not needed
489 * after board init creates the hard-wired devices. Some development
490 * platforms may not be able to use spi_register_board_info though, and
491 * this is exported so that for example a USB or parport based adapter
492 * driver could add devices (which it would learn about out-of-band).
494 * Returns the new device, or NULL.
496 struct spi_device *spi_new_device(struct spi_master *master,
497 struct spi_board_info *chip)
499 struct spi_device *proxy;
502 /* NOTE: caller did any chip->bus_num checks necessary.
504 * Also, unless we change the return value convention to use
505 * error-or-pointer (not NULL-or-pointer), troubleshootability
506 * suggests syslogged diagnostics are best here (ugh).
509 proxy = spi_alloc_device(master);
513 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
515 proxy->chip_select = chip->chip_select;
516 proxy->max_speed_hz = chip->max_speed_hz;
517 proxy->mode = chip->mode;
518 proxy->irq = chip->irq;
519 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
520 proxy->dev.platform_data = (void *) chip->platform_data;
521 proxy->controller_data = chip->controller_data;
522 proxy->controller_state = NULL;
524 status = spi_add_device(proxy);
532 EXPORT_SYMBOL_GPL(spi_new_device);
534 static void spi_match_master_to_boardinfo(struct spi_master *master,
535 struct spi_board_info *bi)
537 struct spi_device *dev;
539 if (master->bus_num != bi->bus_num)
542 dev = spi_new_device(master, bi);
544 dev_err(master->dev.parent, "can't create new device for %s\n",
549 * spi_register_board_info - register SPI devices for a given board
550 * @info: array of chip descriptors
551 * @n: how many descriptors are provided
554 * Board-specific early init code calls this (probably during arch_initcall)
555 * with segments of the SPI device table. Any device nodes are created later,
556 * after the relevant parent SPI controller (bus_num) is defined. We keep
557 * this table of devices forever, so that reloading a controller driver will
558 * not make Linux forget about these hard-wired devices.
560 * Other code can also call this, e.g. a particular add-on board might provide
561 * SPI devices through its expansion connector, so code initializing that board
562 * would naturally declare its SPI devices.
564 * The board info passed can safely be __initdata ... but be careful of
565 * any embedded pointers (platform_data, etc), they're copied as-is.
567 int spi_register_board_info(struct spi_board_info const *info, unsigned n)
569 struct boardinfo *bi;
575 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
579 for (i = 0; i < n; i++, bi++, info++) {
580 struct spi_master *master;
582 memcpy(&bi->board_info, info, sizeof(*info));
583 mutex_lock(&board_lock);
584 list_add_tail(&bi->list, &board_list);
585 list_for_each_entry(master, &spi_master_list, list)
586 spi_match_master_to_boardinfo(master, &bi->board_info);
587 mutex_unlock(&board_lock);
593 /*-------------------------------------------------------------------------*/
595 static void spi_set_cs(struct spi_device *spi, bool enable)
597 if (spi->mode & SPI_CS_HIGH)
600 if (spi->cs_gpio >= 0)
601 gpio_set_value(spi->cs_gpio, !enable);
602 else if (spi->master->set_cs)
603 spi->master->set_cs(spi, !enable);
606 #ifdef CONFIG_HAS_DMA
607 static int spi_map_buf(struct spi_master *master, struct device *dev,
608 struct sg_table *sgt, void *buf, size_t len,
609 enum dma_data_direction dir)
611 const bool vmalloced_buf = is_vmalloc_addr(buf);
614 struct page *vm_page;
620 desc_len = PAGE_SIZE;
621 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
623 desc_len = master->max_dma_len;
624 sgs = DIV_ROUND_UP(len, desc_len);
627 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
631 for (i = 0; i < sgs; i++) {
635 len, desc_len - offset_in_page(buf));
636 vm_page = vmalloc_to_page(buf);
641 sg_set_page(&sgt->sgl[i], vm_page,
642 min, offset_in_page(buf));
644 min = min_t(size_t, len, desc_len);
646 sg_set_buf(&sgt->sgl[i], sg_buf, min);
654 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
667 static void spi_unmap_buf(struct spi_master *master, struct device *dev,
668 struct sg_table *sgt, enum dma_data_direction dir)
670 if (sgt->orig_nents) {
671 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
676 static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
678 struct device *tx_dev, *rx_dev;
679 struct spi_transfer *xfer;
682 if (!master->can_dma)
686 tx_dev = master->dma_tx->device->dev;
688 tx_dev = &master->dev;
691 rx_dev = master->dma_rx->device->dev;
693 rx_dev = &master->dev;
695 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
696 if (!master->can_dma(master, msg->spi, xfer))
699 if (xfer->tx_buf != NULL) {
700 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
701 (void *)xfer->tx_buf, xfer->len,
707 if (xfer->rx_buf != NULL) {
708 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
709 xfer->rx_buf, xfer->len,
712 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
719 master->cur_msg_mapped = true;
724 static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
726 struct spi_transfer *xfer;
727 struct device *tx_dev, *rx_dev;
729 if (!master->cur_msg_mapped || !master->can_dma)
733 tx_dev = master->dma_tx->device->dev;
735 tx_dev = &master->dev;
738 rx_dev = master->dma_rx->device->dev;
740 rx_dev = &master->dev;
742 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
743 if (!master->can_dma(master, msg->spi, xfer))
746 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
747 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
752 #else /* !CONFIG_HAS_DMA */
753 static inline int __spi_map_msg(struct spi_master *master,
754 struct spi_message *msg)
759 static inline int __spi_unmap_msg(struct spi_master *master,
760 struct spi_message *msg)
764 #endif /* !CONFIG_HAS_DMA */
766 static inline int spi_unmap_msg(struct spi_master *master,
767 struct spi_message *msg)
769 struct spi_transfer *xfer;
771 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
773 * Restore the original value of tx_buf or rx_buf if they are
776 if (xfer->tx_buf == master->dummy_tx)
778 if (xfer->rx_buf == master->dummy_rx)
782 return __spi_unmap_msg(master, msg);
785 static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
787 struct spi_transfer *xfer;
789 unsigned int max_tx, max_rx;
791 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
795 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
796 if ((master->flags & SPI_MASTER_MUST_TX) &&
798 max_tx = max(xfer->len, max_tx);
799 if ((master->flags & SPI_MASTER_MUST_RX) &&
801 max_rx = max(xfer->len, max_rx);
805 tmp = krealloc(master->dummy_tx, max_tx,
806 GFP_KERNEL | GFP_DMA);
809 master->dummy_tx = tmp;
810 memset(tmp, 0, max_tx);
814 tmp = krealloc(master->dummy_rx, max_rx,
815 GFP_KERNEL | GFP_DMA);
818 master->dummy_rx = tmp;
821 if (max_tx || max_rx) {
822 list_for_each_entry(xfer, &msg->transfers,
825 xfer->tx_buf = master->dummy_tx;
827 xfer->rx_buf = master->dummy_rx;
832 return __spi_map_msg(master, msg);
836 * spi_transfer_one_message - Default implementation of transfer_one_message()
838 * This is a standard implementation of transfer_one_message() for
839 * drivers which impelment a transfer_one() operation. It provides
840 * standard handling of delays and chip select management.
842 static int spi_transfer_one_message(struct spi_master *master,
843 struct spi_message *msg)
845 struct spi_transfer *xfer;
846 bool keep_cs = false;
848 unsigned long ms = 1;
849 struct spi_statistics *statm = &master->statistics;
850 struct spi_statistics *stats = &msg->spi->statistics;
852 spi_set_cs(msg->spi, true);
854 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
855 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
857 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
858 trace_spi_transfer_start(msg, xfer);
860 spi_statistics_add_transfer_stats(statm, xfer, master);
861 spi_statistics_add_transfer_stats(stats, xfer, master);
863 if (xfer->tx_buf || xfer->rx_buf) {
864 reinit_completion(&master->xfer_completion);
866 ret = master->transfer_one(master, msg->spi, xfer);
868 SPI_STATISTICS_INCREMENT_FIELD(statm,
870 SPI_STATISTICS_INCREMENT_FIELD(stats,
872 dev_err(&msg->spi->dev,
873 "SPI transfer failed: %d\n", ret);
879 ms = xfer->len * 8 * 1000 / xfer->speed_hz;
880 ms += ms + 100; /* some tolerance */
882 ms = wait_for_completion_timeout(&master->xfer_completion,
883 msecs_to_jiffies(ms));
887 SPI_STATISTICS_INCREMENT_FIELD(statm,
889 SPI_STATISTICS_INCREMENT_FIELD(stats,
891 dev_err(&msg->spi->dev,
892 "SPI transfer timed out\n");
893 msg->status = -ETIMEDOUT;
897 dev_err(&msg->spi->dev,
898 "Bufferless transfer has length %u\n",
902 trace_spi_transfer_stop(msg, xfer);
904 if (msg->status != -EINPROGRESS)
907 if (xfer->delay_usecs)
908 udelay(xfer->delay_usecs);
910 if (xfer->cs_change) {
911 if (list_is_last(&xfer->transfer_list,
915 spi_set_cs(msg->spi, false);
917 spi_set_cs(msg->spi, true);
921 msg->actual_length += xfer->len;
925 if (ret != 0 || !keep_cs)
926 spi_set_cs(msg->spi, false);
928 if (msg->status == -EINPROGRESS)
931 if (msg->status && master->handle_err)
932 master->handle_err(master, msg);
934 spi_finalize_current_message(master);
940 * spi_finalize_current_transfer - report completion of a transfer
941 * @master: the master reporting completion
943 * Called by SPI drivers using the core transfer_one_message()
944 * implementation to notify it that the current interrupt driven
945 * transfer has finished and the next one may be scheduled.
947 void spi_finalize_current_transfer(struct spi_master *master)
949 complete(&master->xfer_completion);
951 EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
954 * __spi_pump_messages - function which processes spi message queue
955 * @master: master to process queue for
956 * @in_kthread: true if we are in the context of the message pump thread
958 * This function checks if there is any spi message in the queue that
959 * needs processing and if so call out to the driver to initialize hardware
960 * and transfer each message.
962 * Note that it is called both from the kthread itself and also from
963 * inside spi_sync(); the queue extraction handling at the top of the
964 * function should deal with this safely.
966 static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
969 bool was_busy = false;
973 spin_lock_irqsave(&master->queue_lock, flags);
975 /* Make sure we are not already running a message */
976 if (master->cur_msg) {
977 spin_unlock_irqrestore(&master->queue_lock, flags);
981 /* If another context is idling the device then defer */
982 if (master->idling) {
983 queue_kthread_work(&master->kworker, &master->pump_messages);
984 spin_unlock_irqrestore(&master->queue_lock, flags);
988 /* Check if the queue is idle */
989 if (list_empty(&master->queue) || !master->running) {
991 spin_unlock_irqrestore(&master->queue_lock, flags);
995 /* Only do teardown in the thread */
997 queue_kthread_work(&master->kworker,
998 &master->pump_messages);
999 spin_unlock_irqrestore(&master->queue_lock, flags);
1003 master->busy = false;
1004 master->idling = true;
1005 spin_unlock_irqrestore(&master->queue_lock, flags);
1007 kfree(master->dummy_rx);
1008 master->dummy_rx = NULL;
1009 kfree(master->dummy_tx);
1010 master->dummy_tx = NULL;
1011 if (master->unprepare_transfer_hardware &&
1012 master->unprepare_transfer_hardware(master))
1013 dev_err(&master->dev,
1014 "failed to unprepare transfer hardware\n");
1015 if (master->auto_runtime_pm) {
1016 pm_runtime_mark_last_busy(master->dev.parent);
1017 pm_runtime_put_autosuspend(master->dev.parent);
1019 trace_spi_master_idle(master);
1021 spin_lock_irqsave(&master->queue_lock, flags);
1022 master->idling = false;
1023 spin_unlock_irqrestore(&master->queue_lock, flags);
1027 /* Extract head of queue */
1029 list_first_entry(&master->queue, struct spi_message, queue);
1031 list_del_init(&master->cur_msg->queue);
1035 master->busy = true;
1036 spin_unlock_irqrestore(&master->queue_lock, flags);
1038 if (!was_busy && master->auto_runtime_pm) {
1039 ret = pm_runtime_get_sync(master->dev.parent);
1041 dev_err(&master->dev, "Failed to power device: %d\n",
1048 trace_spi_master_busy(master);
1050 if (!was_busy && master->prepare_transfer_hardware) {
1051 ret = master->prepare_transfer_hardware(master);
1053 dev_err(&master->dev,
1054 "failed to prepare transfer hardware\n");
1056 if (master->auto_runtime_pm)
1057 pm_runtime_put(master->dev.parent);
1062 trace_spi_message_start(master->cur_msg);
1064 if (master->prepare_message) {
1065 ret = master->prepare_message(master, master->cur_msg);
1067 dev_err(&master->dev,
1068 "failed to prepare message: %d\n", ret);
1069 master->cur_msg->status = ret;
1070 spi_finalize_current_message(master);
1073 master->cur_msg_prepared = true;
1076 ret = spi_map_msg(master, master->cur_msg);
1078 master->cur_msg->status = ret;
1079 spi_finalize_current_message(master);
1083 ret = master->transfer_one_message(master, master->cur_msg);
1085 dev_err(&master->dev,
1086 "failed to transfer one message from queue\n");
1092 * spi_pump_messages - kthread work function which processes spi message queue
1093 * @work: pointer to kthread work struct contained in the master struct
1095 static void spi_pump_messages(struct kthread_work *work)
1097 struct spi_master *master =
1098 container_of(work, struct spi_master, pump_messages);
1100 __spi_pump_messages(master, true);
1103 static int spi_init_queue(struct spi_master *master)
1105 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1107 master->running = false;
1108 master->busy = false;
1110 init_kthread_worker(&master->kworker);
1111 master->kworker_task = kthread_run(kthread_worker_fn,
1112 &master->kworker, "%s",
1113 dev_name(&master->dev));
1114 if (IS_ERR(master->kworker_task)) {
1115 dev_err(&master->dev, "failed to create message pump task\n");
1116 return PTR_ERR(master->kworker_task);
1118 init_kthread_work(&master->pump_messages, spi_pump_messages);
1121 * Master config will indicate if this controller should run the
1122 * message pump with high (realtime) priority to reduce the transfer
1123 * latency on the bus by minimising the delay between a transfer
1124 * request and the scheduling of the message pump thread. Without this
1125 * setting the message pump thread will remain at default priority.
1128 dev_info(&master->dev,
1129 "will run message pump with realtime priority\n");
1130 sched_setscheduler(master->kworker_task, SCHED_FIFO, ¶m);
1137 * spi_get_next_queued_message() - called by driver to check for queued
1139 * @master: the master to check for queued messages
1141 * If there are more messages in the queue, the next message is returned from
1144 struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1146 struct spi_message *next;
1147 unsigned long flags;
1149 /* get a pointer to the next message, if any */
1150 spin_lock_irqsave(&master->queue_lock, flags);
1151 next = list_first_entry_or_null(&master->queue, struct spi_message,
1153 spin_unlock_irqrestore(&master->queue_lock, flags);
1157 EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1160 * spi_finalize_current_message() - the current message is complete
1161 * @master: the master to return the message to
1163 * Called by the driver to notify the core that the message in the front of the
1164 * queue is complete and can be removed from the queue.
1166 void spi_finalize_current_message(struct spi_master *master)
1168 struct spi_message *mesg;
1169 unsigned long flags;
1172 spin_lock_irqsave(&master->queue_lock, flags);
1173 mesg = master->cur_msg;
1174 spin_unlock_irqrestore(&master->queue_lock, flags);
1176 spi_unmap_msg(master, mesg);
1178 if (master->cur_msg_prepared && master->unprepare_message) {
1179 ret = master->unprepare_message(master, mesg);
1181 dev_err(&master->dev,
1182 "failed to unprepare message: %d\n", ret);
1186 spin_lock_irqsave(&master->queue_lock, flags);
1187 master->cur_msg = NULL;
1188 master->cur_msg_prepared = false;
1189 queue_kthread_work(&master->kworker, &master->pump_messages);
1190 spin_unlock_irqrestore(&master->queue_lock, flags);
1192 trace_spi_message_done(mesg);
1196 mesg->complete(mesg->context);
1198 EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1200 static int spi_start_queue(struct spi_master *master)
1202 unsigned long flags;
1204 spin_lock_irqsave(&master->queue_lock, flags);
1206 if (master->running || master->busy) {
1207 spin_unlock_irqrestore(&master->queue_lock, flags);
1211 master->running = true;
1212 master->cur_msg = NULL;
1213 spin_unlock_irqrestore(&master->queue_lock, flags);
1215 queue_kthread_work(&master->kworker, &master->pump_messages);
1220 static int spi_stop_queue(struct spi_master *master)
1222 unsigned long flags;
1223 unsigned limit = 500;
1226 spin_lock_irqsave(&master->queue_lock, flags);
1229 * This is a bit lame, but is optimized for the common execution path.
1230 * A wait_queue on the master->busy could be used, but then the common
1231 * execution path (pump_messages) would be required to call wake_up or
1232 * friends on every SPI message. Do this instead.
1234 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1235 spin_unlock_irqrestore(&master->queue_lock, flags);
1236 usleep_range(10000, 11000);
1237 spin_lock_irqsave(&master->queue_lock, flags);
1240 if (!list_empty(&master->queue) || master->busy)
1243 master->running = false;
1245 spin_unlock_irqrestore(&master->queue_lock, flags);
1248 dev_warn(&master->dev,
1249 "could not stop message queue\n");
1255 static int spi_destroy_queue(struct spi_master *master)
1259 ret = spi_stop_queue(master);
1262 * flush_kthread_worker will block until all work is done.
1263 * If the reason that stop_queue timed out is that the work will never
1264 * finish, then it does no good to call flush/stop thread, so
1268 dev_err(&master->dev, "problem destroying queue\n");
1272 flush_kthread_worker(&master->kworker);
1273 kthread_stop(master->kworker_task);
1278 static int __spi_queued_transfer(struct spi_device *spi,
1279 struct spi_message *msg,
1282 struct spi_master *master = spi->master;
1283 unsigned long flags;
1285 spin_lock_irqsave(&master->queue_lock, flags);
1287 if (!master->running) {
1288 spin_unlock_irqrestore(&master->queue_lock, flags);
1291 msg->actual_length = 0;
1292 msg->status = -EINPROGRESS;
1294 list_add_tail(&msg->queue, &master->queue);
1295 if (!master->busy && need_pump)
1296 queue_kthread_work(&master->kworker, &master->pump_messages);
1298 spin_unlock_irqrestore(&master->queue_lock, flags);
1303 * spi_queued_transfer - transfer function for queued transfers
1304 * @spi: spi device which is requesting transfer
1305 * @msg: spi message which is to handled is queued to driver queue
1307 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1309 return __spi_queued_transfer(spi, msg, true);
1312 static int spi_master_initialize_queue(struct spi_master *master)
1316 master->transfer = spi_queued_transfer;
1317 if (!master->transfer_one_message)
1318 master->transfer_one_message = spi_transfer_one_message;
1320 /* Initialize and start queue */
1321 ret = spi_init_queue(master);
1323 dev_err(&master->dev, "problem initializing queue\n");
1324 goto err_init_queue;
1326 master->queued = true;
1327 ret = spi_start_queue(master);
1329 dev_err(&master->dev, "problem starting queue\n");
1330 goto err_start_queue;
1336 spi_destroy_queue(master);
1341 /*-------------------------------------------------------------------------*/
1343 #if defined(CONFIG_OF)
1344 static struct spi_device *
1345 of_register_spi_device(struct spi_master *master, struct device_node *nc)
1347 struct spi_device *spi;
1351 /* Alloc an spi_device */
1352 spi = spi_alloc_device(master);
1354 dev_err(&master->dev, "spi_device alloc error for %s\n",
1360 /* Select device driver */
1361 rc = of_modalias_node(nc, spi->modalias,
1362 sizeof(spi->modalias));
1364 dev_err(&master->dev, "cannot find modalias for %s\n",
1369 /* Device address */
1370 rc = of_property_read_u32(nc, "reg", &value);
1372 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1376 spi->chip_select = value;
1378 /* Mode (clock phase/polarity/etc.) */
1379 if (of_find_property(nc, "spi-cpha", NULL))
1380 spi->mode |= SPI_CPHA;
1381 if (of_find_property(nc, "spi-cpol", NULL))
1382 spi->mode |= SPI_CPOL;
1383 if (of_find_property(nc, "spi-cs-high", NULL))
1384 spi->mode |= SPI_CS_HIGH;
1385 if (of_find_property(nc, "spi-3wire", NULL))
1386 spi->mode |= SPI_3WIRE;
1387 if (of_find_property(nc, "spi-lsb-first", NULL))
1388 spi->mode |= SPI_LSB_FIRST;
1390 /* Device DUAL/QUAD mode */
1391 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1396 spi->mode |= SPI_TX_DUAL;
1399 spi->mode |= SPI_TX_QUAD;
1402 dev_warn(&master->dev,
1403 "spi-tx-bus-width %d not supported\n",
1409 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1414 spi->mode |= SPI_RX_DUAL;
1417 spi->mode |= SPI_RX_QUAD;
1420 dev_warn(&master->dev,
1421 "spi-rx-bus-width %d not supported\n",
1428 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1430 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1434 spi->max_speed_hz = value;
1437 spi->irq = irq_of_parse_and_map(nc, 0);
1439 /* Store a pointer to the node in the device structure */
1441 spi->dev.of_node = nc;
1443 /* Register the new device */
1444 rc = spi_add_device(spi);
1446 dev_err(&master->dev, "spi_device register error %s\n",
1459 * of_register_spi_devices() - Register child devices onto the SPI bus
1460 * @master: Pointer to spi_master device
1462 * Registers an spi_device for each child node of master node which has a 'reg'
1465 static void of_register_spi_devices(struct spi_master *master)
1467 struct spi_device *spi;
1468 struct device_node *nc;
1470 if (!master->dev.of_node)
1473 for_each_available_child_of_node(master->dev.of_node, nc) {
1474 spi = of_register_spi_device(master, nc);
1476 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
1481 static void of_register_spi_devices(struct spi_master *master) { }
1485 static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1487 struct spi_device *spi = data;
1489 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1490 struct acpi_resource_spi_serialbus *sb;
1492 sb = &ares->data.spi_serial_bus;
1493 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1494 spi->chip_select = sb->device_selection;
1495 spi->max_speed_hz = sb->connection_speed;
1497 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1498 spi->mode |= SPI_CPHA;
1499 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1500 spi->mode |= SPI_CPOL;
1501 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1502 spi->mode |= SPI_CS_HIGH;
1504 } else if (spi->irq < 0) {
1507 if (acpi_dev_resource_interrupt(ares, 0, &r))
1511 /* Always tell the ACPI core to skip this resource */
1515 static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1516 void *data, void **return_value)
1518 struct spi_master *master = data;
1519 struct list_head resource_list;
1520 struct acpi_device *adev;
1521 struct spi_device *spi;
1524 if (acpi_bus_get_device(handle, &adev))
1526 if (acpi_bus_get_status(adev) || !adev->status.present)
1529 spi = spi_alloc_device(master);
1531 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1532 dev_name(&adev->dev));
1533 return AE_NO_MEMORY;
1536 ACPI_COMPANION_SET(&spi->dev, adev);
1539 INIT_LIST_HEAD(&resource_list);
1540 ret = acpi_dev_get_resources(adev, &resource_list,
1541 acpi_spi_add_resource, spi);
1542 acpi_dev_free_resource_list(&resource_list);
1544 if (ret < 0 || !spi->max_speed_hz) {
1549 adev->power.flags.ignore_parent = true;
1550 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
1551 if (spi_add_device(spi)) {
1552 adev->power.flags.ignore_parent = false;
1553 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1554 dev_name(&adev->dev));
1561 static void acpi_register_spi_devices(struct spi_master *master)
1566 handle = ACPI_HANDLE(master->dev.parent);
1570 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1571 acpi_spi_add_device, NULL,
1573 if (ACPI_FAILURE(status))
1574 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1577 static inline void acpi_register_spi_devices(struct spi_master *master) {}
1578 #endif /* CONFIG_ACPI */
1580 static void spi_master_release(struct device *dev)
1582 struct spi_master *master;
1584 master = container_of(dev, struct spi_master, dev);
1588 static struct class spi_master_class = {
1589 .name = "spi_master",
1590 .owner = THIS_MODULE,
1591 .dev_release = spi_master_release,
1592 .dev_groups = spi_master_groups,
1597 * spi_alloc_master - allocate SPI master controller
1598 * @dev: the controller, possibly using the platform_bus
1599 * @size: how much zeroed driver-private data to allocate; the pointer to this
1600 * memory is in the driver_data field of the returned device,
1601 * accessible with spi_master_get_devdata().
1602 * Context: can sleep
1604 * This call is used only by SPI master controller drivers, which are the
1605 * only ones directly touching chip registers. It's how they allocate
1606 * an spi_master structure, prior to calling spi_register_master().
1608 * This must be called from context that can sleep. It returns the SPI
1609 * master structure on success, else NULL.
1611 * The caller is responsible for assigning the bus number and initializing
1612 * the master's methods before calling spi_register_master(); and (after errors
1613 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1616 struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
1618 struct spi_master *master;
1623 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
1627 device_initialize(&master->dev);
1628 master->bus_num = -1;
1629 master->num_chipselect = 1;
1630 master->dev.class = &spi_master_class;
1631 master->dev.parent = get_device(dev);
1632 spi_master_set_devdata(master, &master[1]);
1636 EXPORT_SYMBOL_GPL(spi_alloc_master);
1639 static int of_spi_register_master(struct spi_master *master)
1642 struct device_node *np = master->dev.of_node;
1647 nb = of_gpio_named_count(np, "cs-gpios");
1648 master->num_chipselect = max_t(int, nb, master->num_chipselect);
1650 /* Return error only for an incorrectly formed cs-gpios property */
1651 if (nb == 0 || nb == -ENOENT)
1656 cs = devm_kzalloc(&master->dev,
1657 sizeof(int) * master->num_chipselect,
1659 master->cs_gpios = cs;
1661 if (!master->cs_gpios)
1664 for (i = 0; i < master->num_chipselect; i++)
1667 for (i = 0; i < nb; i++)
1668 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1673 static int of_spi_register_master(struct spi_master *master)
1680 * spi_register_master - register SPI master controller
1681 * @master: initialized master, originally from spi_alloc_master()
1682 * Context: can sleep
1684 * SPI master controllers connect to their drivers using some non-SPI bus,
1685 * such as the platform bus. The final stage of probe() in that code
1686 * includes calling spi_register_master() to hook up to this SPI bus glue.
1688 * SPI controllers use board specific (often SOC specific) bus numbers,
1689 * and board-specific addressing for SPI devices combines those numbers
1690 * with chip select numbers. Since SPI does not directly support dynamic
1691 * device identification, boards need configuration tables telling which
1692 * chip is at which address.
1694 * This must be called from context that can sleep. It returns zero on
1695 * success, else a negative error code (dropping the master's refcount).
1696 * After a successful return, the caller is responsible for calling
1697 * spi_unregister_master().
1699 int spi_register_master(struct spi_master *master)
1701 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
1702 struct device *dev = master->dev.parent;
1703 struct boardinfo *bi;
1704 int status = -ENODEV;
1710 status = of_spi_register_master(master);
1714 /* even if it's just one always-selected device, there must
1715 * be at least one chipselect
1717 if (master->num_chipselect == 0)
1720 if ((master->bus_num < 0) && master->dev.of_node)
1721 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1723 /* convention: dynamically assigned bus IDs count down from the max */
1724 if (master->bus_num < 0) {
1725 /* FIXME switch to an IDR based scheme, something like
1726 * I2C now uses, so we can't run out of "dynamic" IDs
1728 master->bus_num = atomic_dec_return(&dyn_bus_id);
1732 INIT_LIST_HEAD(&master->queue);
1733 spin_lock_init(&master->queue_lock);
1734 spin_lock_init(&master->bus_lock_spinlock);
1735 mutex_init(&master->bus_lock_mutex);
1736 master->bus_lock_flag = 0;
1737 init_completion(&master->xfer_completion);
1738 if (!master->max_dma_len)
1739 master->max_dma_len = INT_MAX;
1741 /* register the device, then userspace will see it.
1742 * registration fails if the bus ID is in use.
1744 dev_set_name(&master->dev, "spi%u", master->bus_num);
1745 status = device_add(&master->dev);
1748 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
1749 dynamic ? " (dynamic)" : "");
1751 /* If we're using a queued driver, start the queue */
1752 if (master->transfer)
1753 dev_info(dev, "master is unqueued, this is deprecated\n");
1755 status = spi_master_initialize_queue(master);
1757 device_del(&master->dev);
1761 /* add statistics */
1762 spin_lock_init(&master->statistics.lock);
1764 mutex_lock(&board_lock);
1765 list_add_tail(&master->list, &spi_master_list);
1766 list_for_each_entry(bi, &board_list, list)
1767 spi_match_master_to_boardinfo(master, &bi->board_info);
1768 mutex_unlock(&board_lock);
1770 /* Register devices from the device tree and ACPI */
1771 of_register_spi_devices(master);
1772 acpi_register_spi_devices(master);
1776 EXPORT_SYMBOL_GPL(spi_register_master);
1778 static void devm_spi_unregister(struct device *dev, void *res)
1780 spi_unregister_master(*(struct spi_master **)res);
1784 * dev_spi_register_master - register managed SPI master controller
1785 * @dev: device managing SPI master
1786 * @master: initialized master, originally from spi_alloc_master()
1787 * Context: can sleep
1789 * Register a SPI device as with spi_register_master() which will
1790 * automatically be unregister
1792 int devm_spi_register_master(struct device *dev, struct spi_master *master)
1794 struct spi_master **ptr;
1797 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1801 ret = spi_register_master(master);
1804 devres_add(dev, ptr);
1811 EXPORT_SYMBOL_GPL(devm_spi_register_master);
1813 static int __unregister(struct device *dev, void *null)
1815 spi_unregister_device(to_spi_device(dev));
1820 * spi_unregister_master - unregister SPI master controller
1821 * @master: the master being unregistered
1822 * Context: can sleep
1824 * This call is used only by SPI master controller drivers, which are the
1825 * only ones directly touching chip registers.
1827 * This must be called from context that can sleep.
1829 void spi_unregister_master(struct spi_master *master)
1833 if (master->queued) {
1834 if (spi_destroy_queue(master))
1835 dev_err(&master->dev, "queue remove failed\n");
1838 mutex_lock(&board_lock);
1839 list_del(&master->list);
1840 mutex_unlock(&board_lock);
1842 dummy = device_for_each_child(&master->dev, NULL, __unregister);
1843 device_unregister(&master->dev);
1845 EXPORT_SYMBOL_GPL(spi_unregister_master);
1847 int spi_master_suspend(struct spi_master *master)
1851 /* Basically no-ops for non-queued masters */
1852 if (!master->queued)
1855 ret = spi_stop_queue(master);
1857 dev_err(&master->dev, "queue stop failed\n");
1861 EXPORT_SYMBOL_GPL(spi_master_suspend);
1863 int spi_master_resume(struct spi_master *master)
1867 if (!master->queued)
1870 ret = spi_start_queue(master);
1872 dev_err(&master->dev, "queue restart failed\n");
1876 EXPORT_SYMBOL_GPL(spi_master_resume);
1878 static int __spi_master_match(struct device *dev, const void *data)
1880 struct spi_master *m;
1881 const u16 *bus_num = data;
1883 m = container_of(dev, struct spi_master, dev);
1884 return m->bus_num == *bus_num;
1888 * spi_busnum_to_master - look up master associated with bus_num
1889 * @bus_num: the master's bus number
1890 * Context: can sleep
1892 * This call may be used with devices that are registered after
1893 * arch init time. It returns a refcounted pointer to the relevant
1894 * spi_master (which the caller must release), or NULL if there is
1895 * no such master registered.
1897 struct spi_master *spi_busnum_to_master(u16 bus_num)
1900 struct spi_master *master = NULL;
1902 dev = class_find_device(&spi_master_class, NULL, &bus_num,
1903 __spi_master_match);
1905 master = container_of(dev, struct spi_master, dev);
1906 /* reference got in class_find_device */
1909 EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1912 /*-------------------------------------------------------------------------*/
1914 /* Core methods for SPI master protocol drivers. Some of the
1915 * other core methods are currently defined as inline functions.
1918 static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
1920 if (master->bits_per_word_mask) {
1921 /* Only 32 bits fit in the mask */
1922 if (bits_per_word > 32)
1924 if (!(master->bits_per_word_mask &
1925 SPI_BPW_MASK(bits_per_word)))
1933 * spi_setup - setup SPI mode and clock rate
1934 * @spi: the device whose settings are being modified
1935 * Context: can sleep, and no requests are queued to the device
1937 * SPI protocol drivers may need to update the transfer mode if the
1938 * device doesn't work with its default. They may likewise need
1939 * to update clock rates or word sizes from initial values. This function
1940 * changes those settings, and must be called from a context that can sleep.
1941 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1942 * effect the next time the device is selected and data is transferred to
1943 * or from it. When this function returns, the spi device is deselected.
1945 * Note that this call will fail if the protocol driver specifies an option
1946 * that the underlying controller or its driver does not support. For
1947 * example, not all hardware supports wire transfers using nine bit words,
1948 * LSB-first wire encoding, or active-high chipselects.
1950 int spi_setup(struct spi_device *spi)
1952 unsigned bad_bits, ugly_bits;
1955 /* check mode to prevent that DUAL and QUAD set at the same time
1957 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1958 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1960 "setup: can not select dual and quad at the same time\n");
1963 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1965 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1966 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1968 /* help drivers fail *cleanly* when they need options
1969 * that aren't supported with their current master
1971 bad_bits = spi->mode & ~spi->master->mode_bits;
1972 ugly_bits = bad_bits &
1973 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
1976 "setup: ignoring unsupported mode bits %x\n",
1978 spi->mode &= ~ugly_bits;
1979 bad_bits &= ~ugly_bits;
1982 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
1987 if (!spi->bits_per_word)
1988 spi->bits_per_word = 8;
1990 if (__spi_validate_bits_per_word(spi->master, spi->bits_per_word))
1993 if (!spi->max_speed_hz)
1994 spi->max_speed_hz = spi->master->max_speed_hz;
1996 spi_set_cs(spi, false);
1998 if (spi->master->setup)
1999 status = spi->master->setup(spi);
2001 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
2002 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2003 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2004 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2005 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2006 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2007 spi->bits_per_word, spi->max_speed_hz,
2012 EXPORT_SYMBOL_GPL(spi_setup);
2014 static int __spi_validate(struct spi_device *spi, struct spi_message *message)
2016 struct spi_master *master = spi->master;
2017 struct spi_transfer *xfer;
2020 if (list_empty(&message->transfers))
2023 /* Half-duplex links include original MicroWire, and ones with
2024 * only one data pin like SPI_3WIRE (switches direction) or where
2025 * either MOSI or MISO is missing. They can also be caused by
2026 * software limitations.
2028 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
2029 || (spi->mode & SPI_3WIRE)) {
2030 unsigned flags = master->flags;
2032 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2033 if (xfer->rx_buf && xfer->tx_buf)
2035 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
2037 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
2043 * Set transfer bits_per_word and max speed as spi device default if
2044 * it is not set for this transfer.
2045 * Set transfer tx_nbits and rx_nbits as single transfer default
2046 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
2048 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2049 message->frame_length += xfer->len;
2050 if (!xfer->bits_per_word)
2051 xfer->bits_per_word = spi->bits_per_word;
2053 if (!xfer->speed_hz)
2054 xfer->speed_hz = spi->max_speed_hz;
2055 if (!xfer->speed_hz)
2056 xfer->speed_hz = master->max_speed_hz;
2058 if (master->max_speed_hz &&
2059 xfer->speed_hz > master->max_speed_hz)
2060 xfer->speed_hz = master->max_speed_hz;
2062 if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
2066 * SPI transfer length should be multiple of SPI word size
2067 * where SPI word size should be power-of-two multiple
2069 if (xfer->bits_per_word <= 8)
2071 else if (xfer->bits_per_word <= 16)
2076 /* No partial transfers accepted */
2077 if (xfer->len % w_size)
2080 if (xfer->speed_hz && master->min_speed_hz &&
2081 xfer->speed_hz < master->min_speed_hz)
2084 if (xfer->tx_buf && !xfer->tx_nbits)
2085 xfer->tx_nbits = SPI_NBITS_SINGLE;
2086 if (xfer->rx_buf && !xfer->rx_nbits)
2087 xfer->rx_nbits = SPI_NBITS_SINGLE;
2088 /* check transfer tx/rx_nbits:
2089 * 1. check the value matches one of single, dual and quad
2090 * 2. check tx/rx_nbits match the mode in spi_device
2093 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2094 xfer->tx_nbits != SPI_NBITS_DUAL &&
2095 xfer->tx_nbits != SPI_NBITS_QUAD)
2097 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2098 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2100 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2101 !(spi->mode & SPI_TX_QUAD))
2104 /* check transfer rx_nbits */
2106 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2107 xfer->rx_nbits != SPI_NBITS_DUAL &&
2108 xfer->rx_nbits != SPI_NBITS_QUAD)
2110 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2111 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2113 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2114 !(spi->mode & SPI_RX_QUAD))
2119 message->status = -EINPROGRESS;
2124 static int __spi_async(struct spi_device *spi, struct spi_message *message)
2126 struct spi_master *master = spi->master;
2130 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
2131 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2133 trace_spi_message_submit(message);
2135 return master->transfer(spi, message);
2139 * spi_async - asynchronous SPI transfer
2140 * @spi: device with which data will be exchanged
2141 * @message: describes the data transfers, including completion callback
2142 * Context: any (irqs may be blocked, etc)
2144 * This call may be used in_irq and other contexts which can't sleep,
2145 * as well as from task contexts which can sleep.
2147 * The completion callback is invoked in a context which can't sleep.
2148 * Before that invocation, the value of message->status is undefined.
2149 * When the callback is issued, message->status holds either zero (to
2150 * indicate complete success) or a negative error code. After that
2151 * callback returns, the driver which issued the transfer request may
2152 * deallocate the associated memory; it's no longer in use by any SPI
2153 * core or controller driver code.
2155 * Note that although all messages to a spi_device are handled in
2156 * FIFO order, messages may go to different devices in other orders.
2157 * Some device might be higher priority, or have various "hard" access
2158 * time requirements, for example.
2160 * On detection of any fault during the transfer, processing of
2161 * the entire message is aborted, and the device is deselected.
2162 * Until returning from the associated message completion callback,
2163 * no other spi_message queued to that device will be processed.
2164 * (This rule applies equally to all the synchronous transfer calls,
2165 * which are wrappers around this core asynchronous primitive.)
2167 int spi_async(struct spi_device *spi, struct spi_message *message)
2169 struct spi_master *master = spi->master;
2171 unsigned long flags;
2173 ret = __spi_validate(spi, message);
2177 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2179 if (master->bus_lock_flag)
2182 ret = __spi_async(spi, message);
2184 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2188 EXPORT_SYMBOL_GPL(spi_async);
2191 * spi_async_locked - version of spi_async with exclusive bus usage
2192 * @spi: device with which data will be exchanged
2193 * @message: describes the data transfers, including completion callback
2194 * Context: any (irqs may be blocked, etc)
2196 * This call may be used in_irq and other contexts which can't sleep,
2197 * as well as from task contexts which can sleep.
2199 * The completion callback is invoked in a context which can't sleep.
2200 * Before that invocation, the value of message->status is undefined.
2201 * When the callback is issued, message->status holds either zero (to
2202 * indicate complete success) or a negative error code. After that
2203 * callback returns, the driver which issued the transfer request may
2204 * deallocate the associated memory; it's no longer in use by any SPI
2205 * core or controller driver code.
2207 * Note that although all messages to a spi_device are handled in
2208 * FIFO order, messages may go to different devices in other orders.
2209 * Some device might be higher priority, or have various "hard" access
2210 * time requirements, for example.
2212 * On detection of any fault during the transfer, processing of
2213 * the entire message is aborted, and the device is deselected.
2214 * Until returning from the associated message completion callback,
2215 * no other spi_message queued to that device will be processed.
2216 * (This rule applies equally to all the synchronous transfer calls,
2217 * which are wrappers around this core asynchronous primitive.)
2219 int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2221 struct spi_master *master = spi->master;
2223 unsigned long flags;
2225 ret = __spi_validate(spi, message);
2229 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2231 ret = __spi_async(spi, message);
2233 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2238 EXPORT_SYMBOL_GPL(spi_async_locked);
2241 /*-------------------------------------------------------------------------*/
2243 /* Utility methods for SPI master protocol drivers, layered on
2244 * top of the core. Some other utility methods are defined as
2248 static void spi_complete(void *arg)
2253 static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2256 DECLARE_COMPLETION_ONSTACK(done);
2258 struct spi_master *master = spi->master;
2259 unsigned long flags;
2261 status = __spi_validate(spi, message);
2265 message->complete = spi_complete;
2266 message->context = &done;
2269 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2270 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2273 mutex_lock(&master->bus_lock_mutex);
2275 /* If we're not using the legacy transfer method then we will
2276 * try to transfer in the calling context so special case.
2277 * This code would be less tricky if we could remove the
2278 * support for driver implemented message queues.
2280 if (master->transfer == spi_queued_transfer) {
2281 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2283 trace_spi_message_submit(message);
2285 status = __spi_queued_transfer(spi, message, false);
2287 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2289 status = spi_async_locked(spi, message);
2293 mutex_unlock(&master->bus_lock_mutex);
2296 /* Push out the messages in the calling context if we
2299 if (master->transfer == spi_queued_transfer) {
2300 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2301 spi_sync_immediate);
2302 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2303 spi_sync_immediate);
2304 __spi_pump_messages(master, false);
2307 wait_for_completion(&done);
2308 status = message->status;
2310 message->context = NULL;
2315 * spi_sync - blocking/synchronous SPI data transfers
2316 * @spi: device with which data will be exchanged
2317 * @message: describes the data transfers
2318 * Context: can sleep
2320 * This call may only be used from a context that may sleep. The sleep
2321 * is non-interruptible, and has no timeout. Low-overhead controller
2322 * drivers may DMA directly into and out of the message buffers.
2324 * Note that the SPI device's chip select is active during the message,
2325 * and then is normally disabled between messages. Drivers for some
2326 * frequently-used devices may want to minimize costs of selecting a chip,
2327 * by leaving it selected in anticipation that the next message will go
2328 * to the same chip. (That may increase power usage.)
2330 * Also, the caller is guaranteeing that the memory associated with the
2331 * message will not be freed before this call returns.
2333 * It returns zero on success, else a negative error code.
2335 int spi_sync(struct spi_device *spi, struct spi_message *message)
2337 return __spi_sync(spi, message, 0);
2339 EXPORT_SYMBOL_GPL(spi_sync);
2342 * spi_sync_locked - version of spi_sync with exclusive bus usage
2343 * @spi: device with which data will be exchanged
2344 * @message: describes the data transfers
2345 * Context: can sleep
2347 * This call may only be used from a context that may sleep. The sleep
2348 * is non-interruptible, and has no timeout. Low-overhead controller
2349 * drivers may DMA directly into and out of the message buffers.
2351 * This call should be used by drivers that require exclusive access to the
2352 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
2353 * be released by a spi_bus_unlock call when the exclusive access is over.
2355 * It returns zero on success, else a negative error code.
2357 int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2359 return __spi_sync(spi, message, 1);
2361 EXPORT_SYMBOL_GPL(spi_sync_locked);
2364 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2365 * @master: SPI bus master that should be locked for exclusive bus access
2366 * Context: can sleep
2368 * This call may only be used from a context that may sleep. The sleep
2369 * is non-interruptible, and has no timeout.
2371 * This call should be used by drivers that require exclusive access to the
2372 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2373 * exclusive access is over. Data transfer must be done by spi_sync_locked
2374 * and spi_async_locked calls when the SPI bus lock is held.
2376 * It returns zero on success, else a negative error code.
2378 int spi_bus_lock(struct spi_master *master)
2380 unsigned long flags;
2382 mutex_lock(&master->bus_lock_mutex);
2384 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2385 master->bus_lock_flag = 1;
2386 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2388 /* mutex remains locked until spi_bus_unlock is called */
2392 EXPORT_SYMBOL_GPL(spi_bus_lock);
2395 * spi_bus_unlock - release the lock for exclusive SPI bus usage
2396 * @master: SPI bus master that was locked for exclusive bus access
2397 * Context: can sleep
2399 * This call may only be used from a context that may sleep. The sleep
2400 * is non-interruptible, and has no timeout.
2402 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
2405 * It returns zero on success, else a negative error code.
2407 int spi_bus_unlock(struct spi_master *master)
2409 master->bus_lock_flag = 0;
2411 mutex_unlock(&master->bus_lock_mutex);
2415 EXPORT_SYMBOL_GPL(spi_bus_unlock);
2417 /* portable code must never pass more than 32 bytes */
2418 #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
2423 * spi_write_then_read - SPI synchronous write followed by read
2424 * @spi: device with which data will be exchanged
2425 * @txbuf: data to be written (need not be dma-safe)
2426 * @n_tx: size of txbuf, in bytes
2427 * @rxbuf: buffer into which data will be read (need not be dma-safe)
2428 * @n_rx: size of rxbuf, in bytes
2429 * Context: can sleep
2431 * This performs a half duplex MicroWire style transaction with the
2432 * device, sending txbuf and then reading rxbuf. The return value
2433 * is zero for success, else a negative errno status code.
2434 * This call may only be used from a context that may sleep.
2436 * Parameters to this routine are always copied using a small buffer;
2437 * portable code should never use this for more than 32 bytes.
2438 * Performance-sensitive or bulk transfer code should instead use
2439 * spi_{async,sync}() calls with dma-safe buffers.
2441 int spi_write_then_read(struct spi_device *spi,
2442 const void *txbuf, unsigned n_tx,
2443 void *rxbuf, unsigned n_rx)
2445 static DEFINE_MUTEX(lock);
2448 struct spi_message message;
2449 struct spi_transfer x[2];
2452 /* Use preallocated DMA-safe buffer if we can. We can't avoid
2453 * copying here, (as a pure convenience thing), but we can
2454 * keep heap costs out of the hot path unless someone else is
2455 * using the pre-allocated buffer or the transfer is too large.
2457 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
2458 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
2459 GFP_KERNEL | GFP_DMA);
2466 spi_message_init(&message);
2467 memset(x, 0, sizeof(x));
2470 spi_message_add_tail(&x[0], &message);
2474 spi_message_add_tail(&x[1], &message);
2477 memcpy(local_buf, txbuf, n_tx);
2478 x[0].tx_buf = local_buf;
2479 x[1].rx_buf = local_buf + n_tx;
2482 status = spi_sync(spi, &message);
2484 memcpy(rxbuf, x[1].rx_buf, n_rx);
2486 if (x[0].tx_buf == buf)
2487 mutex_unlock(&lock);
2493 EXPORT_SYMBOL_GPL(spi_write_then_read);
2495 /*-------------------------------------------------------------------------*/
2497 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
2498 static int __spi_of_device_match(struct device *dev, void *data)
2500 return dev->of_node == data;
2503 /* must call put_device() when done with returned spi_device device */
2504 static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
2506 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
2507 __spi_of_device_match);
2508 return dev ? to_spi_device(dev) : NULL;
2511 static int __spi_of_master_match(struct device *dev, const void *data)
2513 return dev->of_node == data;
2516 /* the spi masters are not using spi_bus, so we find it with another way */
2517 static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
2521 dev = class_find_device(&spi_master_class, NULL, node,
2522 __spi_of_master_match);
2526 /* reference got in class_find_device */
2527 return container_of(dev, struct spi_master, dev);
2530 static int of_spi_notify(struct notifier_block *nb, unsigned long action,
2533 struct of_reconfig_data *rd = arg;
2534 struct spi_master *master;
2535 struct spi_device *spi;
2537 switch (of_reconfig_get_state_change(action, arg)) {
2538 case OF_RECONFIG_CHANGE_ADD:
2539 master = of_find_spi_master_by_node(rd->dn->parent);
2541 return NOTIFY_OK; /* not for us */
2543 spi = of_register_spi_device(master, rd->dn);
2544 put_device(&master->dev);
2547 pr_err("%s: failed to create for '%s'\n",
2548 __func__, rd->dn->full_name);
2549 return notifier_from_errno(PTR_ERR(spi));
2553 case OF_RECONFIG_CHANGE_REMOVE:
2554 /* find our device by node */
2555 spi = of_find_spi_device_by_node(rd->dn);
2557 return NOTIFY_OK; /* no? not meant for us */
2559 /* unregister takes one ref away */
2560 spi_unregister_device(spi);
2562 /* and put the reference of the find */
2563 put_device(&spi->dev);
2570 static struct notifier_block spi_of_notifier = {
2571 .notifier_call = of_spi_notify,
2573 #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2574 extern struct notifier_block spi_of_notifier;
2575 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2577 static int __init spi_init(void)
2581 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
2587 status = bus_register(&spi_bus_type);
2591 status = class_register(&spi_master_class);
2595 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2596 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
2601 bus_unregister(&spi_bus_type);
2609 /* board_info is normally registered in arch_initcall(),
2610 * but even essential drivers wait till later
2612 * REVISIT only boardinfo really needs static linking. the rest (device and
2613 * driver registration) _could_ be dynamically linked (modular) ... costs
2614 * include needing to have boardinfo data structures be much more public.
2616 postcore_initcall(spi_init);