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 module *owner, struct spi_driver *sdrv)
314 sdrv->driver.owner = owner;
315 sdrv->driver.bus = &spi_bus_type;
317 sdrv->driver.probe = spi_drv_probe;
319 sdrv->driver.remove = spi_drv_remove;
321 sdrv->driver.shutdown = spi_drv_shutdown;
322 return driver_register(&sdrv->driver);
324 EXPORT_SYMBOL_GPL(__spi_register_driver);
326 /*-------------------------------------------------------------------------*/
328 /* SPI devices should normally not be created by SPI device drivers; that
329 * would make them board-specific. Similarly with SPI master drivers.
330 * Device registration normally goes into like arch/.../mach.../board-YYY.c
331 * with other readonly (flashable) information about mainboard devices.
335 struct list_head list;
336 struct spi_board_info board_info;
339 static LIST_HEAD(board_list);
340 static LIST_HEAD(spi_master_list);
343 * Used to protect add/del opertion for board_info list and
344 * spi_master list, and their matching process
346 static DEFINE_MUTEX(board_lock);
349 * spi_alloc_device - Allocate a new SPI device
350 * @master: Controller to which device is connected
353 * Allows a driver to allocate and initialize a spi_device without
354 * registering it immediately. This allows a driver to directly
355 * fill the spi_device with device parameters before calling
356 * spi_add_device() on it.
358 * Caller is responsible to call spi_add_device() on the returned
359 * spi_device structure to add it to the SPI master. If the caller
360 * needs to discard the spi_device without adding it, then it should
361 * call spi_dev_put() on it.
363 * Returns a pointer to the new device, or NULL.
365 struct spi_device *spi_alloc_device(struct spi_master *master)
367 struct spi_device *spi;
369 if (!spi_master_get(master))
372 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
374 spi_master_put(master);
378 spi->master = master;
379 spi->dev.parent = &master->dev;
380 spi->dev.bus = &spi_bus_type;
381 spi->dev.release = spidev_release;
382 spi->cs_gpio = -ENOENT;
384 spin_lock_init(&spi->statistics.lock);
386 device_initialize(&spi->dev);
389 EXPORT_SYMBOL_GPL(spi_alloc_device);
391 static void spi_dev_set_name(struct spi_device *spi)
393 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
396 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
400 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
404 static int spi_dev_check(struct device *dev, void *data)
406 struct spi_device *spi = to_spi_device(dev);
407 struct spi_device *new_spi = data;
409 if (spi->master == new_spi->master &&
410 spi->chip_select == new_spi->chip_select)
416 * spi_add_device - Add spi_device allocated with spi_alloc_device
417 * @spi: spi_device to register
419 * Companion function to spi_alloc_device. Devices allocated with
420 * spi_alloc_device can be added onto the spi bus with this function.
422 * Returns 0 on success; negative errno on failure
424 int spi_add_device(struct spi_device *spi)
426 static DEFINE_MUTEX(spi_add_lock);
427 struct spi_master *master = spi->master;
428 struct device *dev = master->dev.parent;
431 /* Chipselects are numbered 0..max; validate. */
432 if (spi->chip_select >= master->num_chipselect) {
433 dev_err(dev, "cs%d >= max %d\n",
435 master->num_chipselect);
439 /* Set the bus ID string */
440 spi_dev_set_name(spi);
442 /* We need to make sure there's no other device with this
443 * chipselect **BEFORE** we call setup(), else we'll trash
444 * its configuration. Lock against concurrent add() calls.
446 mutex_lock(&spi_add_lock);
448 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
450 dev_err(dev, "chipselect %d already in use\n",
455 if (master->cs_gpios)
456 spi->cs_gpio = master->cs_gpios[spi->chip_select];
458 /* Drivers may modify this initial i/o setup, but will
459 * normally rely on the device being setup. Devices
460 * using SPI_CS_HIGH can't coexist well otherwise...
462 status = spi_setup(spi);
464 dev_err(dev, "can't setup %s, status %d\n",
465 dev_name(&spi->dev), status);
469 /* Device may be bound to an active driver when this returns */
470 status = device_add(&spi->dev);
472 dev_err(dev, "can't add %s, status %d\n",
473 dev_name(&spi->dev), status);
475 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
478 mutex_unlock(&spi_add_lock);
481 EXPORT_SYMBOL_GPL(spi_add_device);
484 * spi_new_device - instantiate one new SPI device
485 * @master: Controller to which device is connected
486 * @chip: Describes the SPI device
489 * On typical mainboards, this is purely internal; and it's not needed
490 * after board init creates the hard-wired devices. Some development
491 * platforms may not be able to use spi_register_board_info though, and
492 * this is exported so that for example a USB or parport based adapter
493 * driver could add devices (which it would learn about out-of-band).
495 * Returns the new device, or NULL.
497 struct spi_device *spi_new_device(struct spi_master *master,
498 struct spi_board_info *chip)
500 struct spi_device *proxy;
503 /* NOTE: caller did any chip->bus_num checks necessary.
505 * Also, unless we change the return value convention to use
506 * error-or-pointer (not NULL-or-pointer), troubleshootability
507 * suggests syslogged diagnostics are best here (ugh).
510 proxy = spi_alloc_device(master);
514 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
516 proxy->chip_select = chip->chip_select;
517 proxy->max_speed_hz = chip->max_speed_hz;
518 proxy->mode = chip->mode;
519 proxy->irq = chip->irq;
520 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
521 proxy->dev.platform_data = (void *) chip->platform_data;
522 proxy->controller_data = chip->controller_data;
523 proxy->controller_state = NULL;
525 status = spi_add_device(proxy);
533 EXPORT_SYMBOL_GPL(spi_new_device);
535 static void spi_match_master_to_boardinfo(struct spi_master *master,
536 struct spi_board_info *bi)
538 struct spi_device *dev;
540 if (master->bus_num != bi->bus_num)
543 dev = spi_new_device(master, bi);
545 dev_err(master->dev.parent, "can't create new device for %s\n",
550 * spi_register_board_info - register SPI devices for a given board
551 * @info: array of chip descriptors
552 * @n: how many descriptors are provided
555 * Board-specific early init code calls this (probably during arch_initcall)
556 * with segments of the SPI device table. Any device nodes are created later,
557 * after the relevant parent SPI controller (bus_num) is defined. We keep
558 * this table of devices forever, so that reloading a controller driver will
559 * not make Linux forget about these hard-wired devices.
561 * Other code can also call this, e.g. a particular add-on board might provide
562 * SPI devices through its expansion connector, so code initializing that board
563 * would naturally declare its SPI devices.
565 * The board info passed can safely be __initdata ... but be careful of
566 * any embedded pointers (platform_data, etc), they're copied as-is.
568 int spi_register_board_info(struct spi_board_info const *info, unsigned n)
570 struct boardinfo *bi;
576 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
580 for (i = 0; i < n; i++, bi++, info++) {
581 struct spi_master *master;
583 memcpy(&bi->board_info, info, sizeof(*info));
584 mutex_lock(&board_lock);
585 list_add_tail(&bi->list, &board_list);
586 list_for_each_entry(master, &spi_master_list, list)
587 spi_match_master_to_boardinfo(master, &bi->board_info);
588 mutex_unlock(&board_lock);
594 /*-------------------------------------------------------------------------*/
596 static void spi_set_cs(struct spi_device *spi, bool enable)
598 if (spi->mode & SPI_CS_HIGH)
601 if (spi->cs_gpio >= 0)
602 gpio_set_value(spi->cs_gpio, !enable);
603 else if (spi->master->set_cs)
604 spi->master->set_cs(spi, !enable);
607 #ifdef CONFIG_HAS_DMA
608 static int spi_map_buf(struct spi_master *master, struct device *dev,
609 struct sg_table *sgt, void *buf, size_t len,
610 enum dma_data_direction dir)
612 const bool vmalloced_buf = is_vmalloc_addr(buf);
615 struct page *vm_page;
621 desc_len = PAGE_SIZE;
622 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
624 desc_len = master->max_dma_len;
625 sgs = DIV_ROUND_UP(len, desc_len);
628 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
632 for (i = 0; i < sgs; i++) {
636 len, desc_len - offset_in_page(buf));
637 vm_page = vmalloc_to_page(buf);
642 sg_set_page(&sgt->sgl[i], vm_page,
643 min, offset_in_page(buf));
645 min = min_t(size_t, len, desc_len);
647 sg_set_buf(&sgt->sgl[i], sg_buf, min);
655 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
668 static void spi_unmap_buf(struct spi_master *master, struct device *dev,
669 struct sg_table *sgt, enum dma_data_direction dir)
671 if (sgt->orig_nents) {
672 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
677 static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
679 struct device *tx_dev, *rx_dev;
680 struct spi_transfer *xfer;
683 if (!master->can_dma)
687 tx_dev = master->dma_tx->device->dev;
689 tx_dev = &master->dev;
692 rx_dev = master->dma_rx->device->dev;
694 rx_dev = &master->dev;
696 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
697 if (!master->can_dma(master, msg->spi, xfer))
700 if (xfer->tx_buf != NULL) {
701 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
702 (void *)xfer->tx_buf, xfer->len,
708 if (xfer->rx_buf != NULL) {
709 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
710 xfer->rx_buf, xfer->len,
713 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
720 master->cur_msg_mapped = true;
725 static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
727 struct spi_transfer *xfer;
728 struct device *tx_dev, *rx_dev;
730 if (!master->cur_msg_mapped || !master->can_dma)
734 tx_dev = master->dma_tx->device->dev;
736 tx_dev = &master->dev;
739 rx_dev = master->dma_rx->device->dev;
741 rx_dev = &master->dev;
743 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
744 if (!master->can_dma(master, msg->spi, xfer))
747 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
748 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
753 #else /* !CONFIG_HAS_DMA */
754 static inline int __spi_map_msg(struct spi_master *master,
755 struct spi_message *msg)
760 static inline int __spi_unmap_msg(struct spi_master *master,
761 struct spi_message *msg)
765 #endif /* !CONFIG_HAS_DMA */
767 static inline int spi_unmap_msg(struct spi_master *master,
768 struct spi_message *msg)
770 struct spi_transfer *xfer;
772 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
774 * Restore the original value of tx_buf or rx_buf if they are
777 if (xfer->tx_buf == master->dummy_tx)
779 if (xfer->rx_buf == master->dummy_rx)
783 return __spi_unmap_msg(master, msg);
786 static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
788 struct spi_transfer *xfer;
790 unsigned int max_tx, max_rx;
792 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
796 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
797 if ((master->flags & SPI_MASTER_MUST_TX) &&
799 max_tx = max(xfer->len, max_tx);
800 if ((master->flags & SPI_MASTER_MUST_RX) &&
802 max_rx = max(xfer->len, max_rx);
806 tmp = krealloc(master->dummy_tx, max_tx,
807 GFP_KERNEL | GFP_DMA);
810 master->dummy_tx = tmp;
811 memset(tmp, 0, max_tx);
815 tmp = krealloc(master->dummy_rx, max_rx,
816 GFP_KERNEL | GFP_DMA);
819 master->dummy_rx = tmp;
822 if (max_tx || max_rx) {
823 list_for_each_entry(xfer, &msg->transfers,
826 xfer->tx_buf = master->dummy_tx;
828 xfer->rx_buf = master->dummy_rx;
833 return __spi_map_msg(master, msg);
837 * spi_transfer_one_message - Default implementation of transfer_one_message()
839 * This is a standard implementation of transfer_one_message() for
840 * drivers which impelment a transfer_one() operation. It provides
841 * standard handling of delays and chip select management.
843 static int spi_transfer_one_message(struct spi_master *master,
844 struct spi_message *msg)
846 struct spi_transfer *xfer;
847 bool keep_cs = false;
849 unsigned long ms = 1;
850 struct spi_statistics *statm = &master->statistics;
851 struct spi_statistics *stats = &msg->spi->statistics;
853 spi_set_cs(msg->spi, true);
855 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
856 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
858 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
859 trace_spi_transfer_start(msg, xfer);
861 spi_statistics_add_transfer_stats(statm, xfer, master);
862 spi_statistics_add_transfer_stats(stats, xfer, master);
864 if (xfer->tx_buf || xfer->rx_buf) {
865 reinit_completion(&master->xfer_completion);
867 ret = master->transfer_one(master, msg->spi, xfer);
869 SPI_STATISTICS_INCREMENT_FIELD(statm,
871 SPI_STATISTICS_INCREMENT_FIELD(stats,
873 dev_err(&msg->spi->dev,
874 "SPI transfer failed: %d\n", ret);
880 ms = xfer->len * 8 * 1000 / xfer->speed_hz;
881 ms += ms + 100; /* some tolerance */
883 ms = wait_for_completion_timeout(&master->xfer_completion,
884 msecs_to_jiffies(ms));
888 SPI_STATISTICS_INCREMENT_FIELD(statm,
890 SPI_STATISTICS_INCREMENT_FIELD(stats,
892 dev_err(&msg->spi->dev,
893 "SPI transfer timed out\n");
894 msg->status = -ETIMEDOUT;
898 dev_err(&msg->spi->dev,
899 "Bufferless transfer has length %u\n",
903 trace_spi_transfer_stop(msg, xfer);
905 if (msg->status != -EINPROGRESS)
908 if (xfer->delay_usecs)
909 udelay(xfer->delay_usecs);
911 if (xfer->cs_change) {
912 if (list_is_last(&xfer->transfer_list,
916 spi_set_cs(msg->spi, false);
918 spi_set_cs(msg->spi, true);
922 msg->actual_length += xfer->len;
926 if (ret != 0 || !keep_cs)
927 spi_set_cs(msg->spi, false);
929 if (msg->status == -EINPROGRESS)
932 if (msg->status && master->handle_err)
933 master->handle_err(master, msg);
935 spi_finalize_current_message(master);
941 * spi_finalize_current_transfer - report completion of a transfer
942 * @master: the master reporting completion
944 * Called by SPI drivers using the core transfer_one_message()
945 * implementation to notify it that the current interrupt driven
946 * transfer has finished and the next one may be scheduled.
948 void spi_finalize_current_transfer(struct spi_master *master)
950 complete(&master->xfer_completion);
952 EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
955 * __spi_pump_messages - function which processes spi message queue
956 * @master: master to process queue for
957 * @in_kthread: true if we are in the context of the message pump thread
959 * This function checks if there is any spi message in the queue that
960 * needs processing and if so call out to the driver to initialize hardware
961 * and transfer each message.
963 * Note that it is called both from the kthread itself and also from
964 * inside spi_sync(); the queue extraction handling at the top of the
965 * function should deal with this safely.
967 static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
970 bool was_busy = false;
974 spin_lock_irqsave(&master->queue_lock, flags);
976 /* Make sure we are not already running a message */
977 if (master->cur_msg) {
978 spin_unlock_irqrestore(&master->queue_lock, flags);
982 /* If another context is idling the device then defer */
983 if (master->idling) {
984 queue_kthread_work(&master->kworker, &master->pump_messages);
985 spin_unlock_irqrestore(&master->queue_lock, flags);
989 /* Check if the queue is idle */
990 if (list_empty(&master->queue) || !master->running) {
992 spin_unlock_irqrestore(&master->queue_lock, flags);
996 /* Only do teardown in the thread */
998 queue_kthread_work(&master->kworker,
999 &master->pump_messages);
1000 spin_unlock_irqrestore(&master->queue_lock, flags);
1004 master->busy = false;
1005 master->idling = true;
1006 spin_unlock_irqrestore(&master->queue_lock, flags);
1008 kfree(master->dummy_rx);
1009 master->dummy_rx = NULL;
1010 kfree(master->dummy_tx);
1011 master->dummy_tx = NULL;
1012 if (master->unprepare_transfer_hardware &&
1013 master->unprepare_transfer_hardware(master))
1014 dev_err(&master->dev,
1015 "failed to unprepare transfer hardware\n");
1016 if (master->auto_runtime_pm) {
1017 pm_runtime_mark_last_busy(master->dev.parent);
1018 pm_runtime_put_autosuspend(master->dev.parent);
1020 trace_spi_master_idle(master);
1022 spin_lock_irqsave(&master->queue_lock, flags);
1023 master->idling = false;
1024 spin_unlock_irqrestore(&master->queue_lock, flags);
1028 /* Extract head of queue */
1030 list_first_entry(&master->queue, struct spi_message, queue);
1032 list_del_init(&master->cur_msg->queue);
1036 master->busy = true;
1037 spin_unlock_irqrestore(&master->queue_lock, flags);
1039 if (!was_busy && master->auto_runtime_pm) {
1040 ret = pm_runtime_get_sync(master->dev.parent);
1042 dev_err(&master->dev, "Failed to power device: %d\n",
1049 trace_spi_master_busy(master);
1051 if (!was_busy && master->prepare_transfer_hardware) {
1052 ret = master->prepare_transfer_hardware(master);
1054 dev_err(&master->dev,
1055 "failed to prepare transfer hardware\n");
1057 if (master->auto_runtime_pm)
1058 pm_runtime_put(master->dev.parent);
1063 trace_spi_message_start(master->cur_msg);
1065 if (master->prepare_message) {
1066 ret = master->prepare_message(master, master->cur_msg);
1068 dev_err(&master->dev,
1069 "failed to prepare message: %d\n", ret);
1070 master->cur_msg->status = ret;
1071 spi_finalize_current_message(master);
1074 master->cur_msg_prepared = true;
1077 ret = spi_map_msg(master, master->cur_msg);
1079 master->cur_msg->status = ret;
1080 spi_finalize_current_message(master);
1084 ret = master->transfer_one_message(master, master->cur_msg);
1086 dev_err(&master->dev,
1087 "failed to transfer one message from queue\n");
1093 * spi_pump_messages - kthread work function which processes spi message queue
1094 * @work: pointer to kthread work struct contained in the master struct
1096 static void spi_pump_messages(struct kthread_work *work)
1098 struct spi_master *master =
1099 container_of(work, struct spi_master, pump_messages);
1101 __spi_pump_messages(master, true);
1104 static int spi_init_queue(struct spi_master *master)
1106 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1108 master->running = false;
1109 master->busy = false;
1111 init_kthread_worker(&master->kworker);
1112 master->kworker_task = kthread_run(kthread_worker_fn,
1113 &master->kworker, "%s",
1114 dev_name(&master->dev));
1115 if (IS_ERR(master->kworker_task)) {
1116 dev_err(&master->dev, "failed to create message pump task\n");
1117 return PTR_ERR(master->kworker_task);
1119 init_kthread_work(&master->pump_messages, spi_pump_messages);
1122 * Master config will indicate if this controller should run the
1123 * message pump with high (realtime) priority to reduce the transfer
1124 * latency on the bus by minimising the delay between a transfer
1125 * request and the scheduling of the message pump thread. Without this
1126 * setting the message pump thread will remain at default priority.
1129 dev_info(&master->dev,
1130 "will run message pump with realtime priority\n");
1131 sched_setscheduler(master->kworker_task, SCHED_FIFO, ¶m);
1138 * spi_get_next_queued_message() - called by driver to check for queued
1140 * @master: the master to check for queued messages
1142 * If there are more messages in the queue, the next message is returned from
1145 struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1147 struct spi_message *next;
1148 unsigned long flags;
1150 /* get a pointer to the next message, if any */
1151 spin_lock_irqsave(&master->queue_lock, flags);
1152 next = list_first_entry_or_null(&master->queue, struct spi_message,
1154 spin_unlock_irqrestore(&master->queue_lock, flags);
1158 EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1161 * spi_finalize_current_message() - the current message is complete
1162 * @master: the master to return the message to
1164 * Called by the driver to notify the core that the message in the front of the
1165 * queue is complete and can be removed from the queue.
1167 void spi_finalize_current_message(struct spi_master *master)
1169 struct spi_message *mesg;
1170 unsigned long flags;
1173 spin_lock_irqsave(&master->queue_lock, flags);
1174 mesg = master->cur_msg;
1175 spin_unlock_irqrestore(&master->queue_lock, flags);
1177 spi_unmap_msg(master, mesg);
1179 if (master->cur_msg_prepared && master->unprepare_message) {
1180 ret = master->unprepare_message(master, mesg);
1182 dev_err(&master->dev,
1183 "failed to unprepare message: %d\n", ret);
1187 spin_lock_irqsave(&master->queue_lock, flags);
1188 master->cur_msg = NULL;
1189 master->cur_msg_prepared = false;
1190 queue_kthread_work(&master->kworker, &master->pump_messages);
1191 spin_unlock_irqrestore(&master->queue_lock, flags);
1193 trace_spi_message_done(mesg);
1197 mesg->complete(mesg->context);
1199 EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1201 static int spi_start_queue(struct spi_master *master)
1203 unsigned long flags;
1205 spin_lock_irqsave(&master->queue_lock, flags);
1207 if (master->running || master->busy) {
1208 spin_unlock_irqrestore(&master->queue_lock, flags);
1212 master->running = true;
1213 master->cur_msg = NULL;
1214 spin_unlock_irqrestore(&master->queue_lock, flags);
1216 queue_kthread_work(&master->kworker, &master->pump_messages);
1221 static int spi_stop_queue(struct spi_master *master)
1223 unsigned long flags;
1224 unsigned limit = 500;
1227 spin_lock_irqsave(&master->queue_lock, flags);
1230 * This is a bit lame, but is optimized for the common execution path.
1231 * A wait_queue on the master->busy could be used, but then the common
1232 * execution path (pump_messages) would be required to call wake_up or
1233 * friends on every SPI message. Do this instead.
1235 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1236 spin_unlock_irqrestore(&master->queue_lock, flags);
1237 usleep_range(10000, 11000);
1238 spin_lock_irqsave(&master->queue_lock, flags);
1241 if (!list_empty(&master->queue) || master->busy)
1244 master->running = false;
1246 spin_unlock_irqrestore(&master->queue_lock, flags);
1249 dev_warn(&master->dev,
1250 "could not stop message queue\n");
1256 static int spi_destroy_queue(struct spi_master *master)
1260 ret = spi_stop_queue(master);
1263 * flush_kthread_worker will block until all work is done.
1264 * If the reason that stop_queue timed out is that the work will never
1265 * finish, then it does no good to call flush/stop thread, so
1269 dev_err(&master->dev, "problem destroying queue\n");
1273 flush_kthread_worker(&master->kworker);
1274 kthread_stop(master->kworker_task);
1279 static int __spi_queued_transfer(struct spi_device *spi,
1280 struct spi_message *msg,
1283 struct spi_master *master = spi->master;
1284 unsigned long flags;
1286 spin_lock_irqsave(&master->queue_lock, flags);
1288 if (!master->running) {
1289 spin_unlock_irqrestore(&master->queue_lock, flags);
1292 msg->actual_length = 0;
1293 msg->status = -EINPROGRESS;
1295 list_add_tail(&msg->queue, &master->queue);
1296 if (!master->busy && need_pump)
1297 queue_kthread_work(&master->kworker, &master->pump_messages);
1299 spin_unlock_irqrestore(&master->queue_lock, flags);
1304 * spi_queued_transfer - transfer function for queued transfers
1305 * @spi: spi device which is requesting transfer
1306 * @msg: spi message which is to handled is queued to driver queue
1308 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1310 return __spi_queued_transfer(spi, msg, true);
1313 static int spi_master_initialize_queue(struct spi_master *master)
1317 master->transfer = spi_queued_transfer;
1318 if (!master->transfer_one_message)
1319 master->transfer_one_message = spi_transfer_one_message;
1321 /* Initialize and start queue */
1322 ret = spi_init_queue(master);
1324 dev_err(&master->dev, "problem initializing queue\n");
1325 goto err_init_queue;
1327 master->queued = true;
1328 ret = spi_start_queue(master);
1330 dev_err(&master->dev, "problem starting queue\n");
1331 goto err_start_queue;
1337 spi_destroy_queue(master);
1342 /*-------------------------------------------------------------------------*/
1344 #if defined(CONFIG_OF)
1345 static struct spi_device *
1346 of_register_spi_device(struct spi_master *master, struct device_node *nc)
1348 struct spi_device *spi;
1352 /* Alloc an spi_device */
1353 spi = spi_alloc_device(master);
1355 dev_err(&master->dev, "spi_device alloc error for %s\n",
1361 /* Select device driver */
1362 rc = of_modalias_node(nc, spi->modalias,
1363 sizeof(spi->modalias));
1365 dev_err(&master->dev, "cannot find modalias for %s\n",
1370 /* Device address */
1371 rc = of_property_read_u32(nc, "reg", &value);
1373 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1377 spi->chip_select = value;
1379 /* Mode (clock phase/polarity/etc.) */
1380 if (of_find_property(nc, "spi-cpha", NULL))
1381 spi->mode |= SPI_CPHA;
1382 if (of_find_property(nc, "spi-cpol", NULL))
1383 spi->mode |= SPI_CPOL;
1384 if (of_find_property(nc, "spi-cs-high", NULL))
1385 spi->mode |= SPI_CS_HIGH;
1386 if (of_find_property(nc, "spi-3wire", NULL))
1387 spi->mode |= SPI_3WIRE;
1388 if (of_find_property(nc, "spi-lsb-first", NULL))
1389 spi->mode |= SPI_LSB_FIRST;
1391 /* Device DUAL/QUAD mode */
1392 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1397 spi->mode |= SPI_TX_DUAL;
1400 spi->mode |= SPI_TX_QUAD;
1403 dev_warn(&master->dev,
1404 "spi-tx-bus-width %d not supported\n",
1410 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1415 spi->mode |= SPI_RX_DUAL;
1418 spi->mode |= SPI_RX_QUAD;
1421 dev_warn(&master->dev,
1422 "spi-rx-bus-width %d not supported\n",
1429 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1431 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1435 spi->max_speed_hz = value;
1438 spi->irq = irq_of_parse_and_map(nc, 0);
1440 /* Store a pointer to the node in the device structure */
1442 spi->dev.of_node = nc;
1444 /* Register the new device */
1445 rc = spi_add_device(spi);
1447 dev_err(&master->dev, "spi_device register error %s\n",
1460 * of_register_spi_devices() - Register child devices onto the SPI bus
1461 * @master: Pointer to spi_master device
1463 * Registers an spi_device for each child node of master node which has a 'reg'
1466 static void of_register_spi_devices(struct spi_master *master)
1468 struct spi_device *spi;
1469 struct device_node *nc;
1471 if (!master->dev.of_node)
1474 for_each_available_child_of_node(master->dev.of_node, nc) {
1475 spi = of_register_spi_device(master, nc);
1477 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
1482 static void of_register_spi_devices(struct spi_master *master) { }
1486 static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1488 struct spi_device *spi = data;
1490 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1491 struct acpi_resource_spi_serialbus *sb;
1493 sb = &ares->data.spi_serial_bus;
1494 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1495 spi->chip_select = sb->device_selection;
1496 spi->max_speed_hz = sb->connection_speed;
1498 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1499 spi->mode |= SPI_CPHA;
1500 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1501 spi->mode |= SPI_CPOL;
1502 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1503 spi->mode |= SPI_CS_HIGH;
1505 } else if (spi->irq < 0) {
1508 if (acpi_dev_resource_interrupt(ares, 0, &r))
1512 /* Always tell the ACPI core to skip this resource */
1516 static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1517 void *data, void **return_value)
1519 struct spi_master *master = data;
1520 struct list_head resource_list;
1521 struct acpi_device *adev;
1522 struct spi_device *spi;
1525 if (acpi_bus_get_device(handle, &adev))
1527 if (acpi_bus_get_status(adev) || !adev->status.present)
1530 spi = spi_alloc_device(master);
1532 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1533 dev_name(&adev->dev));
1534 return AE_NO_MEMORY;
1537 ACPI_COMPANION_SET(&spi->dev, adev);
1540 INIT_LIST_HEAD(&resource_list);
1541 ret = acpi_dev_get_resources(adev, &resource_list,
1542 acpi_spi_add_resource, spi);
1543 acpi_dev_free_resource_list(&resource_list);
1545 if (ret < 0 || !spi->max_speed_hz) {
1550 adev->power.flags.ignore_parent = true;
1551 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
1552 if (spi_add_device(spi)) {
1553 adev->power.flags.ignore_parent = false;
1554 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1555 dev_name(&adev->dev));
1562 static void acpi_register_spi_devices(struct spi_master *master)
1567 handle = ACPI_HANDLE(master->dev.parent);
1571 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1572 acpi_spi_add_device, NULL,
1574 if (ACPI_FAILURE(status))
1575 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1578 static inline void acpi_register_spi_devices(struct spi_master *master) {}
1579 #endif /* CONFIG_ACPI */
1581 static void spi_master_release(struct device *dev)
1583 struct spi_master *master;
1585 master = container_of(dev, struct spi_master, dev);
1589 static struct class spi_master_class = {
1590 .name = "spi_master",
1591 .owner = THIS_MODULE,
1592 .dev_release = spi_master_release,
1593 .dev_groups = spi_master_groups,
1598 * spi_alloc_master - allocate SPI master controller
1599 * @dev: the controller, possibly using the platform_bus
1600 * @size: how much zeroed driver-private data to allocate; the pointer to this
1601 * memory is in the driver_data field of the returned device,
1602 * accessible with spi_master_get_devdata().
1603 * Context: can sleep
1605 * This call is used only by SPI master controller drivers, which are the
1606 * only ones directly touching chip registers. It's how they allocate
1607 * an spi_master structure, prior to calling spi_register_master().
1609 * This must be called from context that can sleep. It returns the SPI
1610 * master structure on success, else NULL.
1612 * The caller is responsible for assigning the bus number and initializing
1613 * the master's methods before calling spi_register_master(); and (after errors
1614 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1617 struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
1619 struct spi_master *master;
1624 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
1628 device_initialize(&master->dev);
1629 master->bus_num = -1;
1630 master->num_chipselect = 1;
1631 master->dev.class = &spi_master_class;
1632 master->dev.parent = get_device(dev);
1633 spi_master_set_devdata(master, &master[1]);
1637 EXPORT_SYMBOL_GPL(spi_alloc_master);
1640 static int of_spi_register_master(struct spi_master *master)
1643 struct device_node *np = master->dev.of_node;
1648 nb = of_gpio_named_count(np, "cs-gpios");
1649 master->num_chipselect = max_t(int, nb, master->num_chipselect);
1651 /* Return error only for an incorrectly formed cs-gpios property */
1652 if (nb == 0 || nb == -ENOENT)
1657 cs = devm_kzalloc(&master->dev,
1658 sizeof(int) * master->num_chipselect,
1660 master->cs_gpios = cs;
1662 if (!master->cs_gpios)
1665 for (i = 0; i < master->num_chipselect; i++)
1668 for (i = 0; i < nb; i++)
1669 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1674 static int of_spi_register_master(struct spi_master *master)
1681 * spi_register_master - register SPI master controller
1682 * @master: initialized master, originally from spi_alloc_master()
1683 * Context: can sleep
1685 * SPI master controllers connect to their drivers using some non-SPI bus,
1686 * such as the platform bus. The final stage of probe() in that code
1687 * includes calling spi_register_master() to hook up to this SPI bus glue.
1689 * SPI controllers use board specific (often SOC specific) bus numbers,
1690 * and board-specific addressing for SPI devices combines those numbers
1691 * with chip select numbers. Since SPI does not directly support dynamic
1692 * device identification, boards need configuration tables telling which
1693 * chip is at which address.
1695 * This must be called from context that can sleep. It returns zero on
1696 * success, else a negative error code (dropping the master's refcount).
1697 * After a successful return, the caller is responsible for calling
1698 * spi_unregister_master().
1700 int spi_register_master(struct spi_master *master)
1702 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
1703 struct device *dev = master->dev.parent;
1704 struct boardinfo *bi;
1705 int status = -ENODEV;
1711 status = of_spi_register_master(master);
1715 /* even if it's just one always-selected device, there must
1716 * be at least one chipselect
1718 if (master->num_chipselect == 0)
1721 if ((master->bus_num < 0) && master->dev.of_node)
1722 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1724 /* convention: dynamically assigned bus IDs count down from the max */
1725 if (master->bus_num < 0) {
1726 /* FIXME switch to an IDR based scheme, something like
1727 * I2C now uses, so we can't run out of "dynamic" IDs
1729 master->bus_num = atomic_dec_return(&dyn_bus_id);
1733 INIT_LIST_HEAD(&master->queue);
1734 spin_lock_init(&master->queue_lock);
1735 spin_lock_init(&master->bus_lock_spinlock);
1736 mutex_init(&master->bus_lock_mutex);
1737 master->bus_lock_flag = 0;
1738 init_completion(&master->xfer_completion);
1739 if (!master->max_dma_len)
1740 master->max_dma_len = INT_MAX;
1742 /* register the device, then userspace will see it.
1743 * registration fails if the bus ID is in use.
1745 dev_set_name(&master->dev, "spi%u", master->bus_num);
1746 status = device_add(&master->dev);
1749 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
1750 dynamic ? " (dynamic)" : "");
1752 /* If we're using a queued driver, start the queue */
1753 if (master->transfer)
1754 dev_info(dev, "master is unqueued, this is deprecated\n");
1756 status = spi_master_initialize_queue(master);
1758 device_del(&master->dev);
1762 /* add statistics */
1763 spin_lock_init(&master->statistics.lock);
1765 mutex_lock(&board_lock);
1766 list_add_tail(&master->list, &spi_master_list);
1767 list_for_each_entry(bi, &board_list, list)
1768 spi_match_master_to_boardinfo(master, &bi->board_info);
1769 mutex_unlock(&board_lock);
1771 /* Register devices from the device tree and ACPI */
1772 of_register_spi_devices(master);
1773 acpi_register_spi_devices(master);
1777 EXPORT_SYMBOL_GPL(spi_register_master);
1779 static void devm_spi_unregister(struct device *dev, void *res)
1781 spi_unregister_master(*(struct spi_master **)res);
1785 * dev_spi_register_master - register managed SPI master controller
1786 * @dev: device managing SPI master
1787 * @master: initialized master, originally from spi_alloc_master()
1788 * Context: can sleep
1790 * Register a SPI device as with spi_register_master() which will
1791 * automatically be unregister
1793 int devm_spi_register_master(struct device *dev, struct spi_master *master)
1795 struct spi_master **ptr;
1798 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1802 ret = spi_register_master(master);
1805 devres_add(dev, ptr);
1812 EXPORT_SYMBOL_GPL(devm_spi_register_master);
1814 static int __unregister(struct device *dev, void *null)
1816 spi_unregister_device(to_spi_device(dev));
1821 * spi_unregister_master - unregister SPI master controller
1822 * @master: the master being unregistered
1823 * Context: can sleep
1825 * This call is used only by SPI master controller drivers, which are the
1826 * only ones directly touching chip registers.
1828 * This must be called from context that can sleep.
1830 void spi_unregister_master(struct spi_master *master)
1834 if (master->queued) {
1835 if (spi_destroy_queue(master))
1836 dev_err(&master->dev, "queue remove failed\n");
1839 mutex_lock(&board_lock);
1840 list_del(&master->list);
1841 mutex_unlock(&board_lock);
1843 dummy = device_for_each_child(&master->dev, NULL, __unregister);
1844 device_unregister(&master->dev);
1846 EXPORT_SYMBOL_GPL(spi_unregister_master);
1848 int spi_master_suspend(struct spi_master *master)
1852 /* Basically no-ops for non-queued masters */
1853 if (!master->queued)
1856 ret = spi_stop_queue(master);
1858 dev_err(&master->dev, "queue stop failed\n");
1862 EXPORT_SYMBOL_GPL(spi_master_suspend);
1864 int spi_master_resume(struct spi_master *master)
1868 if (!master->queued)
1871 ret = spi_start_queue(master);
1873 dev_err(&master->dev, "queue restart failed\n");
1877 EXPORT_SYMBOL_GPL(spi_master_resume);
1879 static int __spi_master_match(struct device *dev, const void *data)
1881 struct spi_master *m;
1882 const u16 *bus_num = data;
1884 m = container_of(dev, struct spi_master, dev);
1885 return m->bus_num == *bus_num;
1889 * spi_busnum_to_master - look up master associated with bus_num
1890 * @bus_num: the master's bus number
1891 * Context: can sleep
1893 * This call may be used with devices that are registered after
1894 * arch init time. It returns a refcounted pointer to the relevant
1895 * spi_master (which the caller must release), or NULL if there is
1896 * no such master registered.
1898 struct spi_master *spi_busnum_to_master(u16 bus_num)
1901 struct spi_master *master = NULL;
1903 dev = class_find_device(&spi_master_class, NULL, &bus_num,
1904 __spi_master_match);
1906 master = container_of(dev, struct spi_master, dev);
1907 /* reference got in class_find_device */
1910 EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1913 /*-------------------------------------------------------------------------*/
1915 /* Core methods for SPI master protocol drivers. Some of the
1916 * other core methods are currently defined as inline functions.
1919 static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
1921 if (master->bits_per_word_mask) {
1922 /* Only 32 bits fit in the mask */
1923 if (bits_per_word > 32)
1925 if (!(master->bits_per_word_mask &
1926 SPI_BPW_MASK(bits_per_word)))
1934 * spi_setup - setup SPI mode and clock rate
1935 * @spi: the device whose settings are being modified
1936 * Context: can sleep, and no requests are queued to the device
1938 * SPI protocol drivers may need to update the transfer mode if the
1939 * device doesn't work with its default. They may likewise need
1940 * to update clock rates or word sizes from initial values. This function
1941 * changes those settings, and must be called from a context that can sleep.
1942 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1943 * effect the next time the device is selected and data is transferred to
1944 * or from it. When this function returns, the spi device is deselected.
1946 * Note that this call will fail if the protocol driver specifies an option
1947 * that the underlying controller or its driver does not support. For
1948 * example, not all hardware supports wire transfers using nine bit words,
1949 * LSB-first wire encoding, or active-high chipselects.
1951 int spi_setup(struct spi_device *spi)
1953 unsigned bad_bits, ugly_bits;
1956 /* check mode to prevent that DUAL and QUAD set at the same time
1958 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1959 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1961 "setup: can not select dual and quad at the same time\n");
1964 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1966 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1967 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1969 /* help drivers fail *cleanly* when they need options
1970 * that aren't supported with their current master
1972 bad_bits = spi->mode & ~spi->master->mode_bits;
1973 ugly_bits = bad_bits &
1974 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
1977 "setup: ignoring unsupported mode bits %x\n",
1979 spi->mode &= ~ugly_bits;
1980 bad_bits &= ~ugly_bits;
1983 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
1988 if (!spi->bits_per_word)
1989 spi->bits_per_word = 8;
1991 if (__spi_validate_bits_per_word(spi->master, spi->bits_per_word))
1994 if (!spi->max_speed_hz)
1995 spi->max_speed_hz = spi->master->max_speed_hz;
1997 spi_set_cs(spi, false);
1999 if (spi->master->setup)
2000 status = spi->master->setup(spi);
2002 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
2003 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2004 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2005 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2006 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2007 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2008 spi->bits_per_word, spi->max_speed_hz,
2013 EXPORT_SYMBOL_GPL(spi_setup);
2015 static int __spi_validate(struct spi_device *spi, struct spi_message *message)
2017 struct spi_master *master = spi->master;
2018 struct spi_transfer *xfer;
2021 if (list_empty(&message->transfers))
2024 /* Half-duplex links include original MicroWire, and ones with
2025 * only one data pin like SPI_3WIRE (switches direction) or where
2026 * either MOSI or MISO is missing. They can also be caused by
2027 * software limitations.
2029 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
2030 || (spi->mode & SPI_3WIRE)) {
2031 unsigned flags = master->flags;
2033 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2034 if (xfer->rx_buf && xfer->tx_buf)
2036 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
2038 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
2044 * Set transfer bits_per_word and max speed as spi device default if
2045 * it is not set for this transfer.
2046 * Set transfer tx_nbits and rx_nbits as single transfer default
2047 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
2049 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2050 message->frame_length += xfer->len;
2051 if (!xfer->bits_per_word)
2052 xfer->bits_per_word = spi->bits_per_word;
2054 if (!xfer->speed_hz)
2055 xfer->speed_hz = spi->max_speed_hz;
2056 if (!xfer->speed_hz)
2057 xfer->speed_hz = master->max_speed_hz;
2059 if (master->max_speed_hz &&
2060 xfer->speed_hz > master->max_speed_hz)
2061 xfer->speed_hz = master->max_speed_hz;
2063 if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
2067 * SPI transfer length should be multiple of SPI word size
2068 * where SPI word size should be power-of-two multiple
2070 if (xfer->bits_per_word <= 8)
2072 else if (xfer->bits_per_word <= 16)
2077 /* No partial transfers accepted */
2078 if (xfer->len % w_size)
2081 if (xfer->speed_hz && master->min_speed_hz &&
2082 xfer->speed_hz < master->min_speed_hz)
2085 if (xfer->tx_buf && !xfer->tx_nbits)
2086 xfer->tx_nbits = SPI_NBITS_SINGLE;
2087 if (xfer->rx_buf && !xfer->rx_nbits)
2088 xfer->rx_nbits = SPI_NBITS_SINGLE;
2089 /* check transfer tx/rx_nbits:
2090 * 1. check the value matches one of single, dual and quad
2091 * 2. check tx/rx_nbits match the mode in spi_device
2094 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2095 xfer->tx_nbits != SPI_NBITS_DUAL &&
2096 xfer->tx_nbits != SPI_NBITS_QUAD)
2098 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2099 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2101 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2102 !(spi->mode & SPI_TX_QUAD))
2105 /* check transfer rx_nbits */
2107 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2108 xfer->rx_nbits != SPI_NBITS_DUAL &&
2109 xfer->rx_nbits != SPI_NBITS_QUAD)
2111 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2112 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2114 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2115 !(spi->mode & SPI_RX_QUAD))
2120 message->status = -EINPROGRESS;
2125 static int __spi_async(struct spi_device *spi, struct spi_message *message)
2127 struct spi_master *master = spi->master;
2131 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
2132 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2134 trace_spi_message_submit(message);
2136 return master->transfer(spi, message);
2140 * spi_async - asynchronous SPI transfer
2141 * @spi: device with which data will be exchanged
2142 * @message: describes the data transfers, including completion callback
2143 * Context: any (irqs may be blocked, etc)
2145 * This call may be used in_irq and other contexts which can't sleep,
2146 * as well as from task contexts which can sleep.
2148 * The completion callback is invoked in a context which can't sleep.
2149 * Before that invocation, the value of message->status is undefined.
2150 * When the callback is issued, message->status holds either zero (to
2151 * indicate complete success) or a negative error code. After that
2152 * callback returns, the driver which issued the transfer request may
2153 * deallocate the associated memory; it's no longer in use by any SPI
2154 * core or controller driver code.
2156 * Note that although all messages to a spi_device are handled in
2157 * FIFO order, messages may go to different devices in other orders.
2158 * Some device might be higher priority, or have various "hard" access
2159 * time requirements, for example.
2161 * On detection of any fault during the transfer, processing of
2162 * the entire message is aborted, and the device is deselected.
2163 * Until returning from the associated message completion callback,
2164 * no other spi_message queued to that device will be processed.
2165 * (This rule applies equally to all the synchronous transfer calls,
2166 * which are wrappers around this core asynchronous primitive.)
2168 int spi_async(struct spi_device *spi, struct spi_message *message)
2170 struct spi_master *master = spi->master;
2172 unsigned long flags;
2174 ret = __spi_validate(spi, message);
2178 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2180 if (master->bus_lock_flag)
2183 ret = __spi_async(spi, message);
2185 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2189 EXPORT_SYMBOL_GPL(spi_async);
2192 * spi_async_locked - version of spi_async with exclusive bus usage
2193 * @spi: device with which data will be exchanged
2194 * @message: describes the data transfers, including completion callback
2195 * Context: any (irqs may be blocked, etc)
2197 * This call may be used in_irq and other contexts which can't sleep,
2198 * as well as from task contexts which can sleep.
2200 * The completion callback is invoked in a context which can't sleep.
2201 * Before that invocation, the value of message->status is undefined.
2202 * When the callback is issued, message->status holds either zero (to
2203 * indicate complete success) or a negative error code. After that
2204 * callback returns, the driver which issued the transfer request may
2205 * deallocate the associated memory; it's no longer in use by any SPI
2206 * core or controller driver code.
2208 * Note that although all messages to a spi_device are handled in
2209 * FIFO order, messages may go to different devices in other orders.
2210 * Some device might be higher priority, or have various "hard" access
2211 * time requirements, for example.
2213 * On detection of any fault during the transfer, processing of
2214 * the entire message is aborted, and the device is deselected.
2215 * Until returning from the associated message completion callback,
2216 * no other spi_message queued to that device will be processed.
2217 * (This rule applies equally to all the synchronous transfer calls,
2218 * which are wrappers around this core asynchronous primitive.)
2220 int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2222 struct spi_master *master = spi->master;
2224 unsigned long flags;
2226 ret = __spi_validate(spi, message);
2230 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2232 ret = __spi_async(spi, message);
2234 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2239 EXPORT_SYMBOL_GPL(spi_async_locked);
2242 /*-------------------------------------------------------------------------*/
2244 /* Utility methods for SPI master protocol drivers, layered on
2245 * top of the core. Some other utility methods are defined as
2249 static void spi_complete(void *arg)
2254 static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2257 DECLARE_COMPLETION_ONSTACK(done);
2259 struct spi_master *master = spi->master;
2260 unsigned long flags;
2262 status = __spi_validate(spi, message);
2266 message->complete = spi_complete;
2267 message->context = &done;
2270 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2271 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2274 mutex_lock(&master->bus_lock_mutex);
2276 /* If we're not using the legacy transfer method then we will
2277 * try to transfer in the calling context so special case.
2278 * This code would be less tricky if we could remove the
2279 * support for driver implemented message queues.
2281 if (master->transfer == spi_queued_transfer) {
2282 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2284 trace_spi_message_submit(message);
2286 status = __spi_queued_transfer(spi, message, false);
2288 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2290 status = spi_async_locked(spi, message);
2294 mutex_unlock(&master->bus_lock_mutex);
2297 /* Push out the messages in the calling context if we
2300 if (master->transfer == spi_queued_transfer) {
2301 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2302 spi_sync_immediate);
2303 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2304 spi_sync_immediate);
2305 __spi_pump_messages(master, false);
2308 wait_for_completion(&done);
2309 status = message->status;
2311 message->context = NULL;
2316 * spi_sync - blocking/synchronous SPI data transfers
2317 * @spi: device with which data will be exchanged
2318 * @message: describes the data transfers
2319 * Context: can sleep
2321 * This call may only be used from a context that may sleep. The sleep
2322 * is non-interruptible, and has no timeout. Low-overhead controller
2323 * drivers may DMA directly into and out of the message buffers.
2325 * Note that the SPI device's chip select is active during the message,
2326 * and then is normally disabled between messages. Drivers for some
2327 * frequently-used devices may want to minimize costs of selecting a chip,
2328 * by leaving it selected in anticipation that the next message will go
2329 * to the same chip. (That may increase power usage.)
2331 * Also, the caller is guaranteeing that the memory associated with the
2332 * message will not be freed before this call returns.
2334 * It returns zero on success, else a negative error code.
2336 int spi_sync(struct spi_device *spi, struct spi_message *message)
2338 return __spi_sync(spi, message, 0);
2340 EXPORT_SYMBOL_GPL(spi_sync);
2343 * spi_sync_locked - version of spi_sync with exclusive bus usage
2344 * @spi: device with which data will be exchanged
2345 * @message: describes the data transfers
2346 * Context: can sleep
2348 * This call may only be used from a context that may sleep. The sleep
2349 * is non-interruptible, and has no timeout. Low-overhead controller
2350 * drivers may DMA directly into and out of the message buffers.
2352 * This call should be used by drivers that require exclusive access to the
2353 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
2354 * be released by a spi_bus_unlock call when the exclusive access is over.
2356 * It returns zero on success, else a negative error code.
2358 int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2360 return __spi_sync(spi, message, 1);
2362 EXPORT_SYMBOL_GPL(spi_sync_locked);
2365 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2366 * @master: SPI bus master that should be locked for exclusive bus access
2367 * Context: can sleep
2369 * This call may only be used from a context that may sleep. The sleep
2370 * is non-interruptible, and has no timeout.
2372 * This call should be used by drivers that require exclusive access to the
2373 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2374 * exclusive access is over. Data transfer must be done by spi_sync_locked
2375 * and spi_async_locked calls when the SPI bus lock is held.
2377 * It returns zero on success, else a negative error code.
2379 int spi_bus_lock(struct spi_master *master)
2381 unsigned long flags;
2383 mutex_lock(&master->bus_lock_mutex);
2385 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2386 master->bus_lock_flag = 1;
2387 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2389 /* mutex remains locked until spi_bus_unlock is called */
2393 EXPORT_SYMBOL_GPL(spi_bus_lock);
2396 * spi_bus_unlock - release the lock for exclusive SPI bus usage
2397 * @master: SPI bus master that was locked for exclusive bus access
2398 * Context: can sleep
2400 * This call may only be used from a context that may sleep. The sleep
2401 * is non-interruptible, and has no timeout.
2403 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
2406 * It returns zero on success, else a negative error code.
2408 int spi_bus_unlock(struct spi_master *master)
2410 master->bus_lock_flag = 0;
2412 mutex_unlock(&master->bus_lock_mutex);
2416 EXPORT_SYMBOL_GPL(spi_bus_unlock);
2418 /* portable code must never pass more than 32 bytes */
2419 #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
2424 * spi_write_then_read - SPI synchronous write followed by read
2425 * @spi: device with which data will be exchanged
2426 * @txbuf: data to be written (need not be dma-safe)
2427 * @n_tx: size of txbuf, in bytes
2428 * @rxbuf: buffer into which data will be read (need not be dma-safe)
2429 * @n_rx: size of rxbuf, in bytes
2430 * Context: can sleep
2432 * This performs a half duplex MicroWire style transaction with the
2433 * device, sending txbuf and then reading rxbuf. The return value
2434 * is zero for success, else a negative errno status code.
2435 * This call may only be used from a context that may sleep.
2437 * Parameters to this routine are always copied using a small buffer;
2438 * portable code should never use this for more than 32 bytes.
2439 * Performance-sensitive or bulk transfer code should instead use
2440 * spi_{async,sync}() calls with dma-safe buffers.
2442 int spi_write_then_read(struct spi_device *spi,
2443 const void *txbuf, unsigned n_tx,
2444 void *rxbuf, unsigned n_rx)
2446 static DEFINE_MUTEX(lock);
2449 struct spi_message message;
2450 struct spi_transfer x[2];
2453 /* Use preallocated DMA-safe buffer if we can. We can't avoid
2454 * copying here, (as a pure convenience thing), but we can
2455 * keep heap costs out of the hot path unless someone else is
2456 * using the pre-allocated buffer or the transfer is too large.
2458 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
2459 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
2460 GFP_KERNEL | GFP_DMA);
2467 spi_message_init(&message);
2468 memset(x, 0, sizeof(x));
2471 spi_message_add_tail(&x[0], &message);
2475 spi_message_add_tail(&x[1], &message);
2478 memcpy(local_buf, txbuf, n_tx);
2479 x[0].tx_buf = local_buf;
2480 x[1].rx_buf = local_buf + n_tx;
2483 status = spi_sync(spi, &message);
2485 memcpy(rxbuf, x[1].rx_buf, n_rx);
2487 if (x[0].tx_buf == buf)
2488 mutex_unlock(&lock);
2494 EXPORT_SYMBOL_GPL(spi_write_then_read);
2496 /*-------------------------------------------------------------------------*/
2498 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
2499 static int __spi_of_device_match(struct device *dev, void *data)
2501 return dev->of_node == data;
2504 /* must call put_device() when done with returned spi_device device */
2505 static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
2507 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
2508 __spi_of_device_match);
2509 return dev ? to_spi_device(dev) : NULL;
2512 static int __spi_of_master_match(struct device *dev, const void *data)
2514 return dev->of_node == data;
2517 /* the spi masters are not using spi_bus, so we find it with another way */
2518 static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
2522 dev = class_find_device(&spi_master_class, NULL, node,
2523 __spi_of_master_match);
2527 /* reference got in class_find_device */
2528 return container_of(dev, struct spi_master, dev);
2531 static int of_spi_notify(struct notifier_block *nb, unsigned long action,
2534 struct of_reconfig_data *rd = arg;
2535 struct spi_master *master;
2536 struct spi_device *spi;
2538 switch (of_reconfig_get_state_change(action, arg)) {
2539 case OF_RECONFIG_CHANGE_ADD:
2540 master = of_find_spi_master_by_node(rd->dn->parent);
2542 return NOTIFY_OK; /* not for us */
2544 spi = of_register_spi_device(master, rd->dn);
2545 put_device(&master->dev);
2548 pr_err("%s: failed to create for '%s'\n",
2549 __func__, rd->dn->full_name);
2550 return notifier_from_errno(PTR_ERR(spi));
2554 case OF_RECONFIG_CHANGE_REMOVE:
2555 /* find our device by node */
2556 spi = of_find_spi_device_by_node(rd->dn);
2558 return NOTIFY_OK; /* no? not meant for us */
2560 /* unregister takes one ref away */
2561 spi_unregister_device(spi);
2563 /* and put the reference of the find */
2564 put_device(&spi->dev);
2571 static struct notifier_block spi_of_notifier = {
2572 .notifier_call = of_spi_notify,
2574 #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2575 extern struct notifier_block spi_of_notifier;
2576 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2578 static int __init spi_init(void)
2582 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
2588 status = bus_register(&spi_bus_type);
2592 status = class_register(&spi_master_class);
2596 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2597 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
2602 bus_unregister(&spi_bus_type);
2610 /* board_info is normally registered in arch_initcall(),
2611 * but even essential drivers wait till later
2613 * REVISIT only boardinfo really needs static linking. the rest (device and
2614 * driver registration) _could_ be dynamically linked (modular) ... costs
2615 * include needing to have boardinfo data structures be much more public.
2617 postcore_initcall(spi_init);