Lines Matching +full:spi +full:- +full:device
2 * SPI init/core code
19 #include <linux/device.h>
22 #include <linux/dma-mapping.h>
27 #include <linux/clk/clk-conf.h>
30 #include <linux/spi/spi.h>
31 #include <linux/spi/spi-mem.h>
48 #include <trace/events/spi.h>
54 static void spidev_release(struct device *dev) in spidev_release()
56 struct spi_device *spi = to_spi_device(dev); in spidev_release() local
58 /* spi controllers may cleanup for released devices */ in spidev_release()
59 if (spi->controller->cleanup) in spidev_release()
60 spi->controller->cleanup(spi); in spidev_release()
62 spi_controller_put(spi->controller); in spidev_release()
63 kfree(spi); in spidev_release()
67 modalias_show(struct device *dev, struct device_attribute *a, char *buf) in modalias_show()
69 const struct spi_device *spi = to_spi_device(dev); in modalias_show() local
72 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); in modalias_show()
73 if (len != -ENODEV) in modalias_show()
76 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); in modalias_show()
81 static ssize_t spi_controller_##field##_show(struct device *dev, \
87 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
93 static ssize_t spi_device_##field##_show(struct device *dev, \
97 struct spi_device *spi = to_spi_device(dev); \
98 return spi_statistics_##field##_show(&spi->statistics, buf); \
111 spin_lock_irqsave(&stat->lock, flags); \
112 len = sprintf(buf, format_string, stat->field); \
113 spin_unlock_irqrestore(&stat->lock, flags); \
139 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
140 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
141 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
142 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
143 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
144 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
145 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
146 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
147 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
148 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
149 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
150 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
151 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
152 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
153 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
154 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
258 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; in spi_statistics_add_transfer_stats()
263 spin_lock_irqsave(&stats->lock, flags); in spi_statistics_add_transfer_stats()
265 stats->transfers++; in spi_statistics_add_transfer_stats()
266 stats->transfer_bytes_histo[l2len]++; in spi_statistics_add_transfer_stats()
268 stats->bytes += xfer->len; in spi_statistics_add_transfer_stats()
269 if ((xfer->tx_buf) && in spi_statistics_add_transfer_stats()
270 (xfer->tx_buf != ctlr->dummy_tx)) in spi_statistics_add_transfer_stats()
271 stats->bytes_tx += xfer->len; in spi_statistics_add_transfer_stats()
272 if ((xfer->rx_buf) && in spi_statistics_add_transfer_stats()
273 (xfer->rx_buf != ctlr->dummy_rx)) in spi_statistics_add_transfer_stats()
274 stats->bytes_rx += xfer->len; in spi_statistics_add_transfer_stats()
276 spin_unlock_irqrestore(&stats->lock, flags); in spi_statistics_add_transfer_stats()
280 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
287 while (id->name[0]) { in spi_match_id()
288 if (!strcmp(sdev->modalias, id->name)) in spi_match_id()
297 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); in spi_get_device_id()
299 return spi_match_id(sdrv->id_table, sdev); in spi_get_device_id()
303 static int spi_match_device(struct device *dev, struct device_driver *drv) in spi_match_device()
305 const struct spi_device *spi = to_spi_device(dev); in spi_match_device() local
316 if (sdrv->id_table) in spi_match_device()
317 return !!spi_match_id(sdrv->id_table, spi); in spi_match_device()
319 return strcmp(spi->modalias, drv->name) == 0; in spi_match_device()
322 static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) in spi_uevent()
324 const struct spi_device *spi = to_spi_device(dev); in spi_uevent() local
328 if (rc != -ENODEV) in spi_uevent()
331 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); in spi_uevent()
335 .name = "spi",
343 static int spi_drv_probe(struct device *dev) in spi_drv_probe()
345 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_probe()
346 struct spi_device *spi = to_spi_device(dev); in spi_drv_probe() local
349 ret = of_clk_set_defaults(dev->of_node, false); in spi_drv_probe()
353 if (dev->of_node) { in spi_drv_probe()
354 spi->irq = of_irq_get(dev->of_node, 0); in spi_drv_probe()
355 if (spi->irq == -EPROBE_DEFER) in spi_drv_probe()
356 return -EPROBE_DEFER; in spi_drv_probe()
357 if (spi->irq < 0) in spi_drv_probe()
358 spi->irq = 0; in spi_drv_probe()
365 ret = sdrv->probe(spi); in spi_drv_probe()
372 static int spi_drv_remove(struct device *dev) in spi_drv_remove()
374 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_remove()
377 ret = sdrv->remove(to_spi_device(dev)); in spi_drv_remove()
383 static void spi_drv_shutdown(struct device *dev) in spi_drv_shutdown()
385 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_shutdown()
387 sdrv->shutdown(to_spi_device(dev)); in spi_drv_shutdown()
391 * __spi_register_driver - register a SPI driver
400 sdrv->driver.owner = owner; in __spi_register_driver()
401 sdrv->driver.bus = &spi_bus_type; in __spi_register_driver()
402 if (sdrv->probe) in __spi_register_driver()
403 sdrv->driver.probe = spi_drv_probe; in __spi_register_driver()
404 if (sdrv->remove) in __spi_register_driver()
405 sdrv->driver.remove = spi_drv_remove; in __spi_register_driver()
406 if (sdrv->shutdown) in __spi_register_driver()
407 sdrv->driver.shutdown = spi_drv_shutdown; in __spi_register_driver()
408 return driver_register(&sdrv->driver); in __spi_register_driver()
412 /*-------------------------------------------------------------------------*/
414 /* SPI devices should normally not be created by SPI device drivers; that
415 * would make them board-specific. Similarly with SPI controller drivers.
416 * Device registration normally goes into like arch/.../mach.../board-YYY.c
442 * spi_alloc_device - Allocate a new SPI device
443 * @ctlr: Controller to which device is connected
448 * fill the spi_device with device parameters before calling
452 * spi_device structure to add it to the SPI controller. If the caller
456 * Return: a pointer to the new device, or NULL.
460 struct spi_device *spi; in spi_alloc_device() local
465 spi = kzalloc(sizeof(*spi), GFP_KERNEL); in spi_alloc_device()
466 if (!spi) { in spi_alloc_device()
471 spi->master = spi->controller = ctlr; in spi_alloc_device()
472 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
473 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
474 spi->dev.release = spidev_release; in spi_alloc_device()
475 spi->cs_gpio = -ENOENT; in spi_alloc_device()
477 spin_lock_init(&spi->statistics.lock); in spi_alloc_device()
479 device_initialize(&spi->dev); in spi_alloc_device()
480 return spi; in spi_alloc_device()
484 static void spi_dev_set_name(struct spi_device *spi) in spi_dev_set_name() argument
486 struct acpi_device *adev = ACPI_COMPANION(&spi->dev); in spi_dev_set_name()
489 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); in spi_dev_set_name()
493 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
494 spi->chip_select); in spi_dev_set_name()
497 static int spi_dev_check(struct device *dev, void *data) in spi_dev_check()
499 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
502 if (spi->controller == new_spi->controller && in spi_dev_check()
503 spi->chip_select == new_spi->chip_select) in spi_dev_check()
504 return -EBUSY; in spi_dev_check()
509 * spi_add_device - Add spi_device allocated with spi_alloc_device
510 * @spi: spi_device to register
513 * spi_alloc_device can be added onto the spi bus with this function.
517 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
519 struct spi_controller *ctlr = spi->controller; in spi_add_device()
520 struct device *dev = ctlr->dev.parent; in spi_add_device()
524 if (spi->chip_select >= ctlr->num_chipselect) { in spi_add_device()
525 dev_err(dev, "cs%d >= max %d\n", spi->chip_select, in spi_add_device()
526 ctlr->num_chipselect); in spi_add_device()
527 return -EINVAL; in spi_add_device()
531 spi_dev_set_name(spi); in spi_add_device()
533 /* We need to make sure there's no other device with this in spi_add_device()
539 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in spi_add_device()
542 spi->chip_select); in spi_add_device()
548 !device_is_registered(&ctlr->dev)) { in spi_add_device()
549 status = -ENODEV; in spi_add_device()
553 if (ctlr->cs_gpios) in spi_add_device()
554 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; in spi_add_device()
557 * normally rely on the device being setup. Devices in spi_add_device()
560 status = spi_setup(spi); in spi_add_device()
563 dev_name(&spi->dev), status); in spi_add_device()
567 /* Device may be bound to an active driver when this returns */ in spi_add_device()
568 status = device_add(&spi->dev); in spi_add_device()
571 dev_name(&spi->dev), status); in spi_add_device()
573 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in spi_add_device()
582 * spi_new_device - instantiate one new SPI device
583 * @ctlr: Controller to which device is connected
584 * @chip: Describes the SPI device
588 * after board init creates the hard-wired devices. Some development
591 * driver could add devices (which it would learn about out-of-band).
593 * Return: the new device, or NULL.
601 /* NOTE: caller did any chip->bus_num checks necessary. in spi_new_device()
604 * error-or-pointer (not NULL-or-pointer), troubleshootability in spi_new_device()
612 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); in spi_new_device()
614 proxy->chip_select = chip->chip_select; in spi_new_device()
615 proxy->max_speed_hz = chip->max_speed_hz; in spi_new_device()
616 proxy->mode = chip->mode; in spi_new_device()
617 proxy->irq = chip->irq; in spi_new_device()
618 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); in spi_new_device()
619 proxy->dev.platform_data = (void *) chip->platform_data; in spi_new_device()
620 proxy->controller_data = chip->controller_data; in spi_new_device()
621 proxy->controller_state = NULL; in spi_new_device()
623 if (chip->properties) { in spi_new_device()
624 status = device_add_properties(&proxy->dev, chip->properties); in spi_new_device()
626 dev_err(&ctlr->dev, in spi_new_device()
628 chip->modalias, status); in spi_new_device()
640 if (chip->properties) in spi_new_device()
641 device_remove_properties(&proxy->dev); in spi_new_device()
649 * spi_unregister_device - unregister a single SPI device
650 * @spi: spi_device to unregister
652 * Start making the passed SPI device vanish. Normally this would be handled
655 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
657 if (!spi) in spi_unregister_device()
660 if (spi->dev.of_node) { in spi_unregister_device()
661 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
662 of_node_put(spi->dev.of_node); in spi_unregister_device()
664 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
665 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
666 device_unregister(&spi->dev); in spi_unregister_device()
675 if (ctlr->bus_num != bi->bus_num) in spi_match_controller_to_boardinfo()
680 dev_err(ctlr->dev.parent, "can't create new device for %s\n", in spi_match_controller_to_boardinfo()
681 bi->modalias); in spi_match_controller_to_boardinfo()
685 * spi_register_board_info - register SPI devices for a given board
690 * Board-specific early init code calls this (probably during arch_initcall)
691 * with segments of the SPI device table. Any device nodes are created later,
692 * after the relevant parent SPI controller (bus_num) is defined. We keep
694 * not make Linux forget about these hard-wired devices.
696 * Other code can also call this, e.g. a particular add-on board might provide
697 * SPI devices through its expansion connector, so code initializing that board
698 * would naturally declare its SPI devices.
701 * any embedded pointers (platform_data, etc), they're copied as-is.
702 * Device properties are deep-copied though.
716 return -ENOMEM; in spi_register_board_info()
721 memcpy(&bi->board_info, info, sizeof(*info)); in spi_register_board_info()
722 if (info->properties) { in spi_register_board_info()
723 bi->board_info.properties = in spi_register_board_info()
724 property_entries_dup(info->properties); in spi_register_board_info()
725 if (IS_ERR(bi->board_info.properties)) in spi_register_board_info()
726 return PTR_ERR(bi->board_info.properties); in spi_register_board_info()
730 list_add_tail(&bi->list, &board_list); in spi_register_board_info()
733 &bi->board_info); in spi_register_board_info()
740 /*-------------------------------------------------------------------------*/
742 static void spi_set_cs(struct spi_device *spi, bool enable) in spi_set_cs() argument
744 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
747 if (gpio_is_valid(spi->cs_gpio)) { in spi_set_cs()
748 gpio_set_value(spi->cs_gpio, !enable); in spi_set_cs()
749 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
750 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && in spi_set_cs()
751 spi->controller->set_cs) in spi_set_cs()
752 spi->controller->set_cs(spi, !enable); in spi_set_cs()
753 } else if (spi->controller->set_cs) { in spi_set_cs()
754 spi->controller->set_cs(spi, !enable); in spi_set_cs()
759 int spi_map_buf(struct spi_controller *ctlr, struct device *dev, in spi_map_buf()
784 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); in spi_map_buf()
787 return -EINVAL; in spi_map_buf()
794 sg = &sgt->sgl[0]; in spi_map_buf()
805 PAGE_SIZE - offset_in_page(buf))); in spi_map_buf()
812 return -ENOMEM; in spi_map_buf()
823 len -= min; in spi_map_buf()
827 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); in spi_map_buf()
829 ret = -ENOMEM; in spi_map_buf()
835 sgt->nents = ret; in spi_map_buf()
840 void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, in spi_unmap_buf()
843 if (sgt->orig_nents) { in spi_unmap_buf()
844 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); in spi_unmap_buf()
851 struct device *tx_dev, *rx_dev; in __spi_map_msg()
855 if (!ctlr->can_dma) in __spi_map_msg()
858 if (ctlr->dma_tx) in __spi_map_msg()
859 tx_dev = ctlr->dma_tx->device->dev; in __spi_map_msg()
861 tx_dev = ctlr->dev.parent; in __spi_map_msg()
863 if (ctlr->dma_rx) in __spi_map_msg()
864 rx_dev = ctlr->dma_rx->device->dev; in __spi_map_msg()
866 rx_dev = ctlr->dev.parent; in __spi_map_msg()
868 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_map_msg()
869 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
872 if (xfer->tx_buf != NULL) { in __spi_map_msg()
873 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
874 (void *)xfer->tx_buf, xfer->len, in __spi_map_msg()
880 if (xfer->rx_buf != NULL) { in __spi_map_msg()
881 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, in __spi_map_msg()
882 xfer->rx_buf, xfer->len, in __spi_map_msg()
885 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
892 ctlr->cur_msg_mapped = true; in __spi_map_msg()
900 struct device *tx_dev, *rx_dev; in __spi_unmap_msg()
902 if (!ctlr->cur_msg_mapped || !ctlr->can_dma) in __spi_unmap_msg()
905 if (ctlr->dma_tx) in __spi_unmap_msg()
906 tx_dev = ctlr->dma_tx->device->dev; in __spi_unmap_msg()
908 tx_dev = ctlr->dev.parent; in __spi_unmap_msg()
910 if (ctlr->dma_rx) in __spi_unmap_msg()
911 rx_dev = ctlr->dma_rx->device->dev; in __spi_unmap_msg()
913 rx_dev = ctlr->dev.parent; in __spi_unmap_msg()
915 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_unmap_msg()
916 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_unmap_msg()
919 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in __spi_unmap_msg()
920 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in __spi_unmap_msg()
944 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_unmap_msg()
949 if (xfer->tx_buf == ctlr->dummy_tx) in spi_unmap_msg()
950 xfer->tx_buf = NULL; in spi_unmap_msg()
951 if (xfer->rx_buf == ctlr->dummy_rx) in spi_unmap_msg()
952 xfer->rx_buf = NULL; in spi_unmap_msg()
964 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) { in spi_map_msg()
968 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_map_msg()
969 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && in spi_map_msg()
970 !xfer->tx_buf) in spi_map_msg()
971 max_tx = max(xfer->len, max_tx); in spi_map_msg()
972 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && in spi_map_msg()
973 !xfer->rx_buf) in spi_map_msg()
974 max_rx = max(xfer->len, max_rx); in spi_map_msg()
978 tmp = krealloc(ctlr->dummy_tx, max_tx, in spi_map_msg()
981 return -ENOMEM; in spi_map_msg()
982 ctlr->dummy_tx = tmp; in spi_map_msg()
987 tmp = krealloc(ctlr->dummy_rx, max_rx, in spi_map_msg()
990 return -ENOMEM; in spi_map_msg()
991 ctlr->dummy_rx = tmp; in spi_map_msg()
995 list_for_each_entry(xfer, &msg->transfers, in spi_map_msg()
997 if (!xfer->len) in spi_map_msg()
999 if (!xfer->tx_buf) in spi_map_msg()
1000 xfer->tx_buf = ctlr->dummy_tx; in spi_map_msg()
1001 if (!xfer->rx_buf) in spi_map_msg()
1002 xfer->rx_buf = ctlr->dummy_rx; in spi_map_msg()
1011 * spi_transfer_one_message - Default implementation of transfer_one_message()
1024 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_one_message()
1025 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_one_message()
1027 spi_set_cs(msg->spi, true); in spi_transfer_one_message()
1032 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_transfer_one_message()
1038 if (xfer->tx_buf || xfer->rx_buf) { in spi_transfer_one_message()
1039 reinit_completion(&ctlr->xfer_completion); in spi_transfer_one_message()
1041 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1047 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1048 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1054 ms = 8LL * 1000LL * xfer->len; in spi_transfer_one_message()
1055 do_div(ms, xfer->speed_hz); in spi_transfer_one_message()
1061 ms = wait_for_completion_timeout(&ctlr->xfer_completion, in spi_transfer_one_message()
1070 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1071 "SPI transfer timed out\n"); in spi_transfer_one_message()
1072 msg->status = -ETIMEDOUT; in spi_transfer_one_message()
1075 if (xfer->len) in spi_transfer_one_message()
1076 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1078 xfer->len); in spi_transfer_one_message()
1083 if (msg->status != -EINPROGRESS) in spi_transfer_one_message()
1086 if (xfer->delay_usecs) { in spi_transfer_one_message()
1087 u16 us = xfer->delay_usecs; in spi_transfer_one_message()
1095 if (xfer->cs_change) { in spi_transfer_one_message()
1096 if (list_is_last(&xfer->transfer_list, in spi_transfer_one_message()
1097 &msg->transfers)) { in spi_transfer_one_message()
1100 spi_set_cs(msg->spi, false); in spi_transfer_one_message()
1102 spi_set_cs(msg->spi, true); in spi_transfer_one_message()
1106 msg->actual_length += xfer->len; in spi_transfer_one_message()
1111 spi_set_cs(msg->spi, false); in spi_transfer_one_message()
1113 if (msg->status == -EINPROGRESS) in spi_transfer_one_message()
1114 msg->status = ret; in spi_transfer_one_message()
1116 if (msg->status && ctlr->handle_err) in spi_transfer_one_message()
1117 ctlr->handle_err(ctlr, msg); in spi_transfer_one_message()
1125 * spi_finalize_current_transfer - report completion of a transfer
1128 * Called by SPI drivers using the core transfer_one_message()
1134 complete(&ctlr->xfer_completion); in spi_finalize_current_transfer()
1139 * __spi_pump_messages - function which processes spi message queue
1143 * This function checks if there is any spi message in the queue that
1158 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1161 if (ctlr->cur_msg) { in __spi_pump_messages()
1162 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1166 /* If another context is idling the device then defer */ in __spi_pump_messages()
1167 if (ctlr->idling) { in __spi_pump_messages()
1168 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); in __spi_pump_messages()
1169 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1174 if (list_empty(&ctlr->queue) || !ctlr->running) { in __spi_pump_messages()
1175 if (!ctlr->busy) { in __spi_pump_messages()
1176 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1182 kthread_queue_work(&ctlr->kworker, in __spi_pump_messages()
1183 &ctlr->pump_messages); in __spi_pump_messages()
1184 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1188 ctlr->busy = false; in __spi_pump_messages()
1189 ctlr->idling = true; in __spi_pump_messages()
1190 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1192 kfree(ctlr->dummy_rx); in __spi_pump_messages()
1193 ctlr->dummy_rx = NULL; in __spi_pump_messages()
1194 kfree(ctlr->dummy_tx); in __spi_pump_messages()
1195 ctlr->dummy_tx = NULL; in __spi_pump_messages()
1196 if (ctlr->unprepare_transfer_hardware && in __spi_pump_messages()
1197 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_pump_messages()
1198 dev_err(&ctlr->dev, in __spi_pump_messages()
1200 if (ctlr->auto_runtime_pm) { in __spi_pump_messages()
1201 pm_runtime_mark_last_busy(ctlr->dev.parent); in __spi_pump_messages()
1202 pm_runtime_put_autosuspend(ctlr->dev.parent); in __spi_pump_messages()
1206 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1207 ctlr->idling = false; in __spi_pump_messages()
1208 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1213 ctlr->cur_msg = in __spi_pump_messages()
1214 list_first_entry(&ctlr->queue, struct spi_message, queue); in __spi_pump_messages()
1216 list_del_init(&ctlr->cur_msg->queue); in __spi_pump_messages()
1217 if (ctlr->busy) in __spi_pump_messages()
1220 ctlr->busy = true; in __spi_pump_messages()
1221 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1223 mutex_lock(&ctlr->io_mutex); in __spi_pump_messages()
1225 if (!was_busy && ctlr->auto_runtime_pm) { in __spi_pump_messages()
1226 ret = pm_runtime_get_sync(ctlr->dev.parent); in __spi_pump_messages()
1228 pm_runtime_put_noidle(ctlr->dev.parent); in __spi_pump_messages()
1229 dev_err(&ctlr->dev, "Failed to power device: %d\n", in __spi_pump_messages()
1231 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1239 if (!was_busy && ctlr->prepare_transfer_hardware) { in __spi_pump_messages()
1240 ret = ctlr->prepare_transfer_hardware(ctlr); in __spi_pump_messages()
1242 dev_err(&ctlr->dev, in __spi_pump_messages()
1245 if (ctlr->auto_runtime_pm) in __spi_pump_messages()
1246 pm_runtime_put(ctlr->dev.parent); in __spi_pump_messages()
1247 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1252 trace_spi_message_start(ctlr->cur_msg); in __spi_pump_messages()
1254 if (ctlr->prepare_message) { in __spi_pump_messages()
1255 ret = ctlr->prepare_message(ctlr, ctlr->cur_msg); in __spi_pump_messages()
1257 dev_err(&ctlr->dev, "failed to prepare message: %d\n", in __spi_pump_messages()
1259 ctlr->cur_msg->status = ret; in __spi_pump_messages()
1263 ctlr->cur_msg_prepared = true; in __spi_pump_messages()
1266 ret = spi_map_msg(ctlr, ctlr->cur_msg); in __spi_pump_messages()
1268 ctlr->cur_msg->status = ret; in __spi_pump_messages()
1273 ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg); in __spi_pump_messages()
1275 dev_err(&ctlr->dev, in __spi_pump_messages()
1281 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1289 * spi_pump_messages - kthread work function which processes spi message queue
1302 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; in spi_init_queue()
1304 ctlr->running = false; in spi_init_queue()
1305 ctlr->busy = false; in spi_init_queue()
1307 kthread_init_worker(&ctlr->kworker); in spi_init_queue()
1308 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker, in spi_init_queue()
1309 "%s", dev_name(&ctlr->dev)); in spi_init_queue()
1310 if (IS_ERR(ctlr->kworker_task)) { in spi_init_queue()
1311 dev_err(&ctlr->dev, "failed to create message pump task\n"); in spi_init_queue()
1312 return PTR_ERR(ctlr->kworker_task); in spi_init_queue()
1314 kthread_init_work(&ctlr->pump_messages, spi_pump_messages); in spi_init_queue()
1323 if (ctlr->rt) { in spi_init_queue()
1324 dev_info(&ctlr->dev, in spi_init_queue()
1326 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, ¶m); in spi_init_queue()
1333 * spi_get_next_queued_message() - called by driver to check for queued
1348 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1349 next = list_first_entry_or_null(&ctlr->queue, struct spi_message, in spi_get_next_queued_message()
1351 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1358 * spi_finalize_current_message() - the current message is complete
1370 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1371 mesg = ctlr->cur_msg; in spi_finalize_current_message()
1372 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1376 /* In the prepare_messages callback the spi bus has the opportunity to in spi_finalize_current_message()
1383 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { in spi_finalize_current_message()
1384 ret = ctlr->unprepare_message(ctlr, mesg); in spi_finalize_current_message()
1386 dev_err(&ctlr->dev, "failed to unprepare message: %d\n", in spi_finalize_current_message()
1391 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1392 ctlr->cur_msg = NULL; in spi_finalize_current_message()
1393 ctlr->cur_msg_prepared = false; in spi_finalize_current_message()
1394 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); in spi_finalize_current_message()
1395 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1399 mesg->state = NULL; in spi_finalize_current_message()
1400 if (mesg->complete) in spi_finalize_current_message()
1401 mesg->complete(mesg->context); in spi_finalize_current_message()
1409 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_start_queue()
1411 if (ctlr->running || ctlr->busy) { in spi_start_queue()
1412 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1413 return -EBUSY; in spi_start_queue()
1416 ctlr->running = true; in spi_start_queue()
1417 ctlr->cur_msg = NULL; in spi_start_queue()
1418 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1420 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); in spi_start_queue()
1431 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1435 * A wait_queue on the ctlr->busy could be used, but then the common in spi_stop_queue()
1437 * friends on every SPI message. Do this instead. in spi_stop_queue()
1439 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { in spi_stop_queue()
1440 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1442 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1445 if (!list_empty(&ctlr->queue) || ctlr->busy) in spi_stop_queue()
1446 ret = -EBUSY; in spi_stop_queue()
1448 ctlr->running = false; in spi_stop_queue()
1450 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1453 dev_warn(&ctlr->dev, "could not stop message queue\n"); in spi_stop_queue()
1472 dev_err(&ctlr->dev, "problem destroying queue\n"); in spi_destroy_queue()
1476 kthread_flush_worker(&ctlr->kworker); in spi_destroy_queue()
1477 kthread_stop(ctlr->kworker_task); in spi_destroy_queue()
1482 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
1486 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
1489 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1491 if (!ctlr->running) { in __spi_queued_transfer()
1492 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1493 return -ESHUTDOWN; in __spi_queued_transfer()
1495 msg->actual_length = 0; in __spi_queued_transfer()
1496 msg->status = -EINPROGRESS; in __spi_queued_transfer()
1498 list_add_tail(&msg->queue, &ctlr->queue); in __spi_queued_transfer()
1499 if (!ctlr->busy && need_pump) in __spi_queued_transfer()
1500 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); in __spi_queued_transfer()
1502 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1507 * spi_queued_transfer - transfer function for queued transfers
1508 * @spi: spi device which is requesting transfer
1509 * @msg: spi message which is to handled is queued to driver queue
1513 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
1515 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
1522 ctlr->transfer = spi_queued_transfer; in spi_controller_initialize_queue()
1523 if (!ctlr->transfer_one_message) in spi_controller_initialize_queue()
1524 ctlr->transfer_one_message = spi_transfer_one_message; in spi_controller_initialize_queue()
1529 dev_err(&ctlr->dev, "problem initializing queue\n"); in spi_controller_initialize_queue()
1532 ctlr->queued = true; in spi_controller_initialize_queue()
1535 dev_err(&ctlr->dev, "problem starting queue\n"); in spi_controller_initialize_queue()
1548 * spi_flush_queue - Send all pending messages in the queue from the callers'
1553 * sent before doing something. Is used by the spi-mem code to make sure SPI
1554 * memory operations do not preempt regular SPI transfers that have been queued
1555 * before the spi-mem operation.
1559 if (ctlr->transfer == spi_queued_transfer) in spi_flush_queue()
1563 /*-------------------------------------------------------------------------*/
1566 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
1573 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
1574 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
1575 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
1576 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
1577 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
1578 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
1579 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
1580 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
1581 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
1582 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
1584 /* Device DUAL/QUAD mode */ in of_spi_parse_dt()
1585 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
1590 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
1593 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
1596 dev_warn(&ctlr->dev, in of_spi_parse_dt()
1597 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
1603 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
1608 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
1611 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
1614 dev_warn(&ctlr->dev, in of_spi_parse_dt()
1615 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
1622 if (strcmp(nc->name, "slave")) { in of_spi_parse_dt()
1623 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", in of_spi_parse_dt()
1625 return -EINVAL; in of_spi_parse_dt()
1630 /* Device address */ in of_spi_parse_dt()
1633 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", in of_spi_parse_dt()
1637 spi->chip_select = value; in of_spi_parse_dt()
1639 /* Device speed */ in of_spi_parse_dt()
1640 rc = of_property_read_u32(nc, "spi-max-frequency", &value); in of_spi_parse_dt()
1642 dev_err(&ctlr->dev, in of_spi_parse_dt()
1643 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc); in of_spi_parse_dt()
1646 spi->max_speed_hz = value; in of_spi_parse_dt()
1654 struct spi_device *spi; in of_register_spi_device() local
1658 spi = spi_alloc_device(ctlr); in of_register_spi_device()
1659 if (!spi) { in of_register_spi_device()
1660 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); in of_register_spi_device()
1661 rc = -ENOMEM; in of_register_spi_device()
1665 /* Select device driver */ in of_register_spi_device()
1666 rc = of_modalias_node(nc, spi->modalias, in of_register_spi_device()
1667 sizeof(spi->modalias)); in of_register_spi_device()
1669 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); in of_register_spi_device()
1673 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
1677 /* Store a pointer to the node in the device structure */ in of_register_spi_device()
1679 spi->dev.of_node = nc; in of_register_spi_device()
1681 /* Register the new device */ in of_register_spi_device()
1682 rc = spi_add_device(spi); in of_register_spi_device()
1684 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); in of_register_spi_device()
1688 return spi; in of_register_spi_device()
1693 spi_dev_put(spi); in of_register_spi_device()
1698 * of_register_spi_devices() - Register child devices onto the SPI bus
1699 * @ctlr: Pointer to spi_controller device
1702 * represents a valid SPI slave.
1706 struct spi_device *spi; in of_register_spi_devices() local
1709 if (!ctlr->dev.of_node) in of_register_spi_devices()
1712 for_each_available_child_of_node(ctlr->dev.of_node, nc) { in of_register_spi_devices()
1715 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
1716 if (IS_ERR(spi)) { in of_register_spi_devices()
1717 dev_warn(&ctlr->dev, in of_register_spi_devices()
1718 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
1728 static void acpi_spi_parse_apple_properties(struct spi_device *spi) in acpi_spi_parse_apple_properties() argument
1730 struct acpi_device *dev = ACPI_COMPANION(&spi->dev); in acpi_spi_parse_apple_properties()
1737 && obj->buffer.length >= 4) in acpi_spi_parse_apple_properties()
1738 spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
1741 && obj->buffer.length == 8) in acpi_spi_parse_apple_properties()
1742 spi->bits_per_word = *(u64 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
1745 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
1746 spi->mode |= SPI_LSB_FIRST; in acpi_spi_parse_apple_properties()
1749 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
1750 spi->mode |= SPI_CPOL; in acpi_spi_parse_apple_properties()
1753 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
1754 spi->mode |= SPI_CPHA; in acpi_spi_parse_apple_properties()
1759 struct spi_device *spi = data; in acpi_spi_add_resource() local
1760 struct spi_controller *ctlr = spi->controller; in acpi_spi_add_resource()
1762 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { in acpi_spi_add_resource()
1765 sb = &ares->data.spi_serial_bus; in acpi_spi_add_resource()
1766 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { in acpi_spi_add_resource()
1771 * 0 .. max - 1 so we need to ask the driver to in acpi_spi_add_resource()
1774 if (ctlr->fw_translate_cs) { in acpi_spi_add_resource()
1775 int cs = ctlr->fw_translate_cs(ctlr, in acpi_spi_add_resource()
1776 sb->device_selection); in acpi_spi_add_resource()
1779 spi->chip_select = cs; in acpi_spi_add_resource()
1781 spi->chip_select = sb->device_selection; in acpi_spi_add_resource()
1784 spi->max_speed_hz = sb->connection_speed; in acpi_spi_add_resource()
1786 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) in acpi_spi_add_resource()
1787 spi->mode |= SPI_CPHA; in acpi_spi_add_resource()
1788 if (sb->clock_polarity == ACPI_SPI_START_HIGH) in acpi_spi_add_resource()
1789 spi->mode |= SPI_CPOL; in acpi_spi_add_resource()
1790 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) in acpi_spi_add_resource()
1791 spi->mode |= SPI_CS_HIGH; in acpi_spi_add_resource()
1793 } else if (spi->irq < 0) { in acpi_spi_add_resource()
1797 spi->irq = r.start; in acpi_spi_add_resource()
1808 struct spi_device *spi; in acpi_register_spi_device() local
1811 if (acpi_bus_get_status(adev) || !adev->status.present || in acpi_register_spi_device()
1815 spi = spi_alloc_device(ctlr); in acpi_register_spi_device()
1816 if (!spi) { in acpi_register_spi_device()
1817 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_register_spi_device()
1818 dev_name(&adev->dev)); in acpi_register_spi_device()
1822 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_register_spi_device()
1823 spi->irq = -1; in acpi_register_spi_device()
1827 acpi_spi_add_resource, spi); in acpi_register_spi_device()
1830 acpi_spi_parse_apple_properties(spi); in acpi_register_spi_device()
1832 if (ret < 0 || !spi->max_speed_hz) { in acpi_register_spi_device()
1833 spi_dev_put(spi); in acpi_register_spi_device()
1837 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
1838 sizeof(spi->modalias)); in acpi_register_spi_device()
1840 if (spi->irq < 0) in acpi_register_spi_device()
1841 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in acpi_register_spi_device()
1845 adev->power.flags.ignore_parent = true; in acpi_register_spi_device()
1846 if (spi_add_device(spi)) { in acpi_register_spi_device()
1847 adev->power.flags.ignore_parent = false; in acpi_register_spi_device()
1848 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
1849 dev_name(&adev->dev)); in acpi_register_spi_device()
1850 spi_dev_put(spi); in acpi_register_spi_device()
1873 handle = ACPI_HANDLE(ctlr->dev.parent); in acpi_register_spi_devices()
1880 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
1886 static void spi_controller_release(struct device *dev) in spi_controller_release()
1903 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
1905 * @spi: device used for the current transfer
1907 int spi_slave_abort(struct spi_device *spi) in spi_slave_abort() argument
1909 struct spi_controller *ctlr = spi->controller; in spi_slave_abort()
1911 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) in spi_slave_abort()
1912 return ctlr->slave_abort(ctlr); in spi_slave_abort()
1914 return -ENOTSUPP; in spi_slave_abort()
1918 static int match_true(struct device *dev, void *data) in match_true()
1923 static ssize_t spi_slave_show(struct device *dev, in spi_slave_show()
1928 struct device *child; in spi_slave_show()
1930 child = device_find_child(&ctlr->dev, NULL, match_true); in spi_slave_show()
1932 child ? to_spi_device(child)->modalias : NULL); in spi_slave_show()
1935 static ssize_t spi_slave_store(struct device *dev, in spi_slave_store()
1941 struct spi_device *spi; in spi_slave_store() local
1942 struct device *child; in spi_slave_store()
1948 return -EINVAL; in spi_slave_store()
1950 child = device_find_child(&ctlr->dev, NULL, match_true); in spi_slave_store()
1959 spi = spi_alloc_device(ctlr); in spi_slave_store()
1960 if (!spi) in spi_slave_store()
1961 return -ENOMEM; in spi_slave_store()
1963 strlcpy(spi->modalias, name, sizeof(spi->modalias)); in spi_slave_store()
1965 rc = spi_add_device(spi); in spi_slave_store()
1967 spi_dev_put(spi); in spi_slave_store()
2003 * __spi_alloc_controller - allocate an SPI master or slave controller
2005 * @size: how much zeroed driver-private data to allocate; the pointer to this
2006 * memory is in the driver_data field of the returned device,
2008 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2012 * This call is used only by SPI controller drivers, which are the
2020 * errors adding the device) calling spi_controller_put() to prevent a memory
2023 * Return: the SPI controller structure on success, else NULL.
2025 struct spi_controller *__spi_alloc_controller(struct device *dev, in __spi_alloc_controller()
2037 device_initialize(&ctlr->dev); in __spi_alloc_controller()
2038 ctlr->bus_num = -1; in __spi_alloc_controller()
2039 ctlr->num_chipselect = 1; in __spi_alloc_controller()
2040 ctlr->slave = slave; in __spi_alloc_controller()
2042 ctlr->dev.class = &spi_slave_class; in __spi_alloc_controller()
2044 ctlr->dev.class = &spi_master_class; in __spi_alloc_controller()
2045 ctlr->dev.parent = dev; in __spi_alloc_controller()
2046 pm_suspend_ignore_children(&ctlr->dev, true); in __spi_alloc_controller()
2057 struct device_node *np = ctlr->dev.of_node; in of_spi_register_master()
2062 nb = of_gpio_named_count(np, "cs-gpios"); in of_spi_register_master()
2063 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in of_spi_register_master()
2065 /* Return error only for an incorrectly formed cs-gpios property */ in of_spi_register_master()
2066 if (nb == 0 || nb == -ENOENT) in of_spi_register_master()
2071 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), in of_spi_register_master()
2073 ctlr->cs_gpios = cs; in of_spi_register_master()
2075 if (!ctlr->cs_gpios) in of_spi_register_master()
2076 return -ENOMEM; in of_spi_register_master()
2078 for (i = 0; i < ctlr->num_chipselect; i++) in of_spi_register_master()
2079 cs[i] = -ENOENT; in of_spi_register_master()
2082 cs[i] = of_get_named_gpio(np, "cs-gpios", i); in of_spi_register_master()
2096 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
2097 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
2099 * If ->mem_ops is NULL, we request that at least one of the in spi_controller_check_ops()
2100 * ->transfer_xxx() method be implemented. in spi_controller_check_ops()
2102 if (ctlr->mem_ops) { in spi_controller_check_ops()
2103 if (!ctlr->mem_ops->exec_op) in spi_controller_check_ops()
2104 return -EINVAL; in spi_controller_check_ops()
2105 } else if (!ctlr->transfer && !ctlr->transfer_one && in spi_controller_check_ops()
2106 !ctlr->transfer_one_message) { in spi_controller_check_ops()
2107 return -EINVAL; in spi_controller_check_ops()
2114 * spi_register_controller - register SPI master or slave controller
2119 * SPI controllers connect to their drivers using some non-SPI bus,
2121 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2123 * SPI controllers use board specific (often SOC specific) bus numbers,
2124 * and board-specific addressing for SPI devices combines those numbers
2125 * with chip select numbers. Since SPI does not directly support dynamic
2126 * device identification, boards need configuration tables telling which
2138 struct device *dev = ctlr->dev.parent; in spi_register_controller()
2140 int status = -ENODEV; in spi_register_controller()
2144 return -ENODEV; in spi_register_controller()
2148 * the SPI controller. in spi_register_controller()
2160 /* even if it's just one always-selected device, there must in spi_register_controller()
2163 if (ctlr->num_chipselect == 0) in spi_register_controller()
2164 return -EINVAL; in spi_register_controller()
2165 if (ctlr->bus_num >= 0) { in spi_register_controller()
2166 /* devices with a fixed bus num must check-in with the num */ in spi_register_controller()
2168 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2169 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2172 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2173 ctlr->bus_num = id; in spi_register_controller()
2174 } else if (ctlr->dev.of_node) { in spi_register_controller()
2176 id = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
2178 ctlr->bus_num = id; in spi_register_controller()
2180 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2181 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2184 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2187 if (ctlr->bus_num < 0) { in spi_register_controller()
2188 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
2200 ctlr->bus_num = id; in spi_register_controller()
2202 INIT_LIST_HEAD(&ctlr->queue); in spi_register_controller()
2203 spin_lock_init(&ctlr->queue_lock); in spi_register_controller()
2204 spin_lock_init(&ctlr->bus_lock_spinlock); in spi_register_controller()
2205 mutex_init(&ctlr->bus_lock_mutex); in spi_register_controller()
2206 mutex_init(&ctlr->io_mutex); in spi_register_controller()
2207 ctlr->bus_lock_flag = 0; in spi_register_controller()
2208 init_completion(&ctlr->xfer_completion); in spi_register_controller()
2209 if (!ctlr->max_dma_len) in spi_register_controller()
2210 ctlr->max_dma_len = INT_MAX; in spi_register_controller()
2212 /* register the device, then userspace will see it. in spi_register_controller()
2215 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
2216 status = device_add(&ctlr->dev); in spi_register_controller()
2220 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
2226 dev_name(&ctlr->dev)); in spi_register_controller()
2230 * need the queueing logic if the driver is only supporting high-level in spi_register_controller()
2233 if (ctlr->transfer) { in spi_register_controller()
2235 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { in spi_register_controller()
2238 device_del(&ctlr->dev); in spi_register_controller()
2241 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
2247 spin_lock_init(&ctlr->statistics.lock); in spi_register_controller()
2250 list_add_tail(&ctlr->list, &spi_controller_list); in spi_register_controller()
2252 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); in spi_register_controller()
2255 /* Register devices from the device tree and ACPI */ in spi_register_controller()
2263 static void devm_spi_unregister(struct device *dev, void *res) in devm_spi_unregister()
2269 * devm_spi_register_controller - register managed SPI master or slave
2271 * @dev: device managing SPI controller
2276 * Register a SPI device as with spi_register_controller() which will
2281 int devm_spi_register_controller(struct device *dev, in devm_spi_register_controller()
2289 return -ENOMEM; in devm_spi_register_controller()
2303 static int __unregister(struct device *dev, void *null) in __unregister()
2310 * spi_unregister_controller - unregister SPI master or slave controller
2314 * This call is used only by SPI controller drivers, which are the
2324 int id = ctlr->bus_num; in spi_unregister_controller()
2330 device_for_each_child(&ctlr->dev, NULL, __unregister); in spi_unregister_controller()
2336 if (ctlr->queued) { in spi_unregister_controller()
2338 dev_err(&ctlr->dev, "queue remove failed\n"); in spi_unregister_controller()
2341 list_del(&ctlr->list); in spi_unregister_controller()
2344 device_unregister(&ctlr->dev); in spi_unregister_controller()
2360 /* Basically no-ops for non-queued controllers */ in spi_controller_suspend()
2361 if (!ctlr->queued) in spi_controller_suspend()
2366 dev_err(&ctlr->dev, "queue stop failed\n"); in spi_controller_suspend()
2376 if (!ctlr->queued) in spi_controller_resume()
2381 dev_err(&ctlr->dev, "queue restart failed\n"); in spi_controller_resume()
2387 static int __spi_controller_match(struct device *dev, const void *data) in __spi_controller_match()
2393 return ctlr->bus_num == *bus_num; in __spi_controller_match()
2397 * spi_busnum_to_master - look up master associated with bus_num
2406 * Return: the SPI master structure on success, else NULL.
2410 struct device *dev; in spi_busnum_to_master()
2422 /*-------------------------------------------------------------------------*/
2424 /* Core methods for SPI resource management */
2427 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2430 * @spi: the spi device for which we allocate memory
2440 void *spi_res_alloc(struct spi_device *spi, in spi_res_alloc() argument
2450 INIT_LIST_HEAD(&sres->entry); in spi_res_alloc()
2451 sres->release = release; in spi_res_alloc()
2453 return sres->data; in spi_res_alloc()
2458 * spi_res_free - free an spi resource
2469 WARN_ON(!list_empty(&sres->entry)); in spi_res_free()
2475 * spi_res_add - add a spi_res to the spi_message
2476 * @message: the spi message
2483 WARN_ON(!list_empty(&sres->entry)); in spi_res_add()
2484 list_add_tail(&sres->entry, &message->resources); in spi_res_add()
2489 * spi_res_release - release all spi resources for this message
2497 while (!list_empty(&message->resources)) { in spi_res_release()
2498 res = list_last_entry(&message->resources, in spi_res_release()
2501 if (res->release) in spi_res_release()
2502 res->release(ctlr, message, res->data); in spi_res_release()
2504 list_del(&res->entry); in spi_res_release()
2511 /*-------------------------------------------------------------------------*/
2523 if (rxfer->release) in __spi_replace_transfers_release()
2524 rxfer->release(ctlr, msg, res); in __spi_replace_transfers_release()
2527 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); in __spi_replace_transfers_release()
2530 for (i = 0; i < rxfer->inserted; i++) in __spi_replace_transfers_release()
2531 list_del(&rxfer->inserted_transfers[i].transfer_list); in __spi_replace_transfers_release()
2535 * spi_replace_transfers - replace transfers with several transfers
2563 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
2569 return ERR_PTR(-ENOMEM); in spi_replace_transfers()
2572 rxfer->release = release; in spi_replace_transfers()
2576 rxfer->extradata = in spi_replace_transfers()
2577 &rxfer->inserted_transfers[insert]; in spi_replace_transfers()
2580 INIT_LIST_HEAD(&rxfer->replaced_transfers); in spi_replace_transfers()
2583 * the @replaced_transfers - it may be spi_message.messages! in spi_replace_transfers()
2585 rxfer->replaced_after = xfer_first->transfer_list.prev; in spi_replace_transfers()
2589 /* if the entry after replaced_after it is msg->transfers in spi_replace_transfers()
2593 if (rxfer->replaced_after->next == &msg->transfers) { in spi_replace_transfers()
2594 dev_err(&msg->spi->dev, in spi_replace_transfers()
2597 list_splice(&rxfer->replaced_transfers, in spi_replace_transfers()
2598 rxfer->replaced_after); in spi_replace_transfers()
2604 return ERR_PTR(-EINVAL); in spi_replace_transfers()
2610 list_move_tail(rxfer->replaced_after->next, in spi_replace_transfers()
2611 &rxfer->replaced_transfers); in spi_replace_transfers()
2619 xfer = &rxfer->inserted_transfers[insert - 1 - i]; in spi_replace_transfers()
2625 list_add(&xfer->transfer_list, rxfer->replaced_after); in spi_replace_transfers()
2629 xfer->cs_change = false; in spi_replace_transfers()
2630 xfer->delay_usecs = 0; in spi_replace_transfers()
2635 rxfer->inserted = insert; in spi_replace_transfers()
2656 dev_warn_once(&msg->spi->dev, in __spi_split_transfer_maxsize()
2657 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n", in __spi_split_transfer_maxsize()
2658 xfer->len, maxsize); in __spi_split_transfer_maxsize()
2661 count = DIV_ROUND_UP(xfer->len, maxsize); in __spi_split_transfer_maxsize()
2667 xfers = srt->inserted_transfers; in __spi_split_transfer_maxsize()
2697 xfers[i].len = min(maxsize, xfers[i].len - offset); in __spi_split_transfer_maxsize()
2703 *xferp = &xfers[count - 1]; in __spi_split_transfer_maxsize()
2706 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_split_transfer_maxsize()
2708 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, in __spi_split_transfer_maxsize()
2715 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2739 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxsize()
2740 if (xfer->len > maxsize) { in spi_split_transfers_maxsize()
2752 /*-------------------------------------------------------------------------*/
2754 /* Core methods for SPI controller protocol drivers. Some of the
2761 if (ctlr->bits_per_word_mask) { in __spi_validate_bits_per_word()
2764 return -EINVAL; in __spi_validate_bits_per_word()
2765 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) in __spi_validate_bits_per_word()
2766 return -EINVAL; in __spi_validate_bits_per_word()
2773 * spi_setup - setup SPI mode and clock rate
2774 * @spi: the device whose settings are being modified
2775 * Context: can sleep, and no requests are queued to the device
2777 * SPI protocol drivers may need to update the transfer mode if the
2778 * device doesn't work with its default. They may likewise need
2782 * effect the next time the device is selected and data is transferred to
2783 * or from it. When this function returns, the spi device is deselected.
2788 * LSB-first wire encoding, or active-high chipselects.
2792 int spi_setup(struct spi_device *spi) in spi_setup() argument
2799 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || in spi_setup()
2800 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { in spi_setup()
2801 dev_err(&spi->dev, in spi_setup()
2803 return -EINVAL; in spi_setup()
2807 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
2809 return -EINVAL; in spi_setup()
2813 bad_bits = spi->mode & ~spi->controller->mode_bits; in spi_setup()
2817 dev_warn(&spi->dev, in spi_setup()
2820 spi->mode &= ~ugly_bits; in spi_setup()
2824 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
2826 return -EINVAL; in spi_setup()
2829 if (!spi->bits_per_word) in spi_setup()
2830 spi->bits_per_word = 8; in spi_setup()
2832 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
2833 spi->bits_per_word); in spi_setup()
2837 if (!spi->max_speed_hz) in spi_setup()
2838 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
2840 if (spi->controller->setup) in spi_setup()
2841 status = spi->controller->setup(spi); in spi_setup()
2843 spi_set_cs(spi, false); in spi_setup()
2845 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
2846 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), in spi_setup()
2847 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
2848 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
2849 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
2850 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
2851 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
2858 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
2860 struct spi_controller *ctlr = spi->controller; in __spi_validate()
2864 if (list_empty(&message->transfers)) in __spi_validate()
2865 return -EINVAL; in __spi_validate()
2867 /* Half-duplex links include original MicroWire, and ones with in __spi_validate()
2872 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || in __spi_validate()
2873 (spi->mode & SPI_3WIRE)) { in __spi_validate()
2874 unsigned flags = ctlr->flags; in __spi_validate()
2876 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
2877 if (xfer->rx_buf && xfer->tx_buf) in __spi_validate()
2878 return -EINVAL; in __spi_validate()
2879 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) in __spi_validate()
2880 return -EINVAL; in __spi_validate()
2881 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) in __spi_validate()
2882 return -EINVAL; in __spi_validate()
2887 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
2892 message->frame_length = 0; in __spi_validate()
2893 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
2894 message->frame_length += xfer->len; in __spi_validate()
2895 if (!xfer->bits_per_word) in __spi_validate()
2896 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
2898 if (!xfer->speed_hz) in __spi_validate()
2899 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
2900 if (!xfer->speed_hz) in __spi_validate()
2901 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
2903 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) in __spi_validate()
2904 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
2906 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) in __spi_validate()
2907 return -EINVAL; in __spi_validate()
2910 * SPI transfer length should be multiple of SPI word size in __spi_validate()
2911 * where SPI word size should be power-of-two multiple in __spi_validate()
2913 if (xfer->bits_per_word <= 8) in __spi_validate()
2915 else if (xfer->bits_per_word <= 16) in __spi_validate()
2921 if (xfer->len % w_size) in __spi_validate()
2922 return -EINVAL; in __spi_validate()
2924 if (xfer->speed_hz && ctlr->min_speed_hz && in __spi_validate()
2925 xfer->speed_hz < ctlr->min_speed_hz) in __spi_validate()
2926 return -EINVAL; in __spi_validate()
2928 if (xfer->tx_buf && !xfer->tx_nbits) in __spi_validate()
2929 xfer->tx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
2930 if (xfer->rx_buf && !xfer->rx_nbits) in __spi_validate()
2931 xfer->rx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
2936 if (xfer->tx_buf) { in __spi_validate()
2937 if (xfer->tx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
2938 xfer->tx_nbits != SPI_NBITS_DUAL && in __spi_validate()
2939 xfer->tx_nbits != SPI_NBITS_QUAD) in __spi_validate()
2940 return -EINVAL; in __spi_validate()
2941 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
2942 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
2943 return -EINVAL; in __spi_validate()
2944 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
2945 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
2946 return -EINVAL; in __spi_validate()
2949 if (xfer->rx_buf) { in __spi_validate()
2950 if (xfer->rx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
2951 xfer->rx_nbits != SPI_NBITS_DUAL && in __spi_validate()
2952 xfer->rx_nbits != SPI_NBITS_QUAD) in __spi_validate()
2953 return -EINVAL; in __spi_validate()
2954 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
2955 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
2956 return -EINVAL; in __spi_validate()
2957 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
2958 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
2959 return -EINVAL; in __spi_validate()
2963 message->status = -EINPROGRESS; in __spi_validate()
2968 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
2970 struct spi_controller *ctlr = spi->controller; in __spi_async()
2973 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
2976 if (!ctlr->transfer) in __spi_async()
2977 return -ENOTSUPP; in __spi_async()
2979 message->spi = spi; in __spi_async()
2981 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); in __spi_async()
2982 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); in __spi_async()
2986 return ctlr->transfer(spi, message); in __spi_async()
2990 * spi_async - asynchronous SPI transfer
2991 * @spi: device with which data will be exchanged
2999 * Before that invocation, the value of message->status is undefined.
3000 * When the callback is issued, message->status holds either zero (to
3003 * deallocate the associated memory; it's no longer in use by any SPI
3008 * Some device might be higher priority, or have various "hard" access
3012 * the entire message is aborted, and the device is deselected.
3014 * no other spi_message queued to that device will be processed.
3020 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
3022 struct spi_controller *ctlr = spi->controller; in spi_async()
3026 ret = __spi_validate(spi, message); in spi_async()
3030 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async()
3032 if (ctlr->bus_lock_flag) in spi_async()
3033 ret = -EBUSY; in spi_async()
3035 ret = __spi_async(spi, message); in spi_async()
3037 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async()
3044 * spi_async_locked - version of spi_async with exclusive bus usage
3045 * @spi: device with which data will be exchanged
3053 * Before that invocation, the value of message->status is undefined.
3054 * When the callback is issued, message->status holds either zero (to
3057 * deallocate the associated memory; it's no longer in use by any SPI
3062 * Some device might be higher priority, or have various "hard" access
3066 * the entire message is aborted, and the device is deselected.
3068 * no other spi_message queued to that device will be processed.
3074 int spi_async_locked(struct spi_device *spi, struct spi_message *message) in spi_async_locked() argument
3076 struct spi_controller *ctlr = spi->controller; in spi_async_locked()
3080 ret = __spi_validate(spi, message); in spi_async_locked()
3084 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3086 ret = __spi_async(spi, message); in spi_async_locked()
3088 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3095 /*-------------------------------------------------------------------------*/
3097 /* Utility methods for SPI protocol drivers, layered on
3107 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
3111 struct spi_controller *ctlr = spi->controller; in __spi_sync()
3114 status = __spi_validate(spi, message); in __spi_sync()
3118 message->complete = spi_complete; in __spi_sync()
3119 message->context = &done; in __spi_sync()
3120 message->spi = spi; in __spi_sync()
3122 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); in __spi_sync()
3123 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); in __spi_sync()
3130 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3131 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3135 status = __spi_queued_transfer(spi, message, false); in __spi_sync()
3137 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3139 status = spi_async_locked(spi, message); in __spi_sync()
3146 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3147 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_sync()
3149 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, in __spi_sync()
3155 status = message->status; in __spi_sync()
3157 message->context = NULL; in __spi_sync()
3162 * spi_sync - blocking/synchronous SPI data transfers
3163 * @spi: device with which data will be exchanged
3168 * is non-interruptible, and has no timeout. Low-overhead controller
3171 * Note that the SPI device's chip select is active during the message,
3173 * frequently-used devices may want to minimize costs of selecting a chip,
3182 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
3186 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
3187 ret = __spi_sync(spi, message); in spi_sync()
3188 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
3195 * spi_sync_locked - version of spi_sync with exclusive bus usage
3196 * @spi: device with which data will be exchanged
3201 * is non-interruptible, and has no timeout. Low-overhead controller
3205 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3210 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
3212 return __spi_sync(spi, message); in spi_sync_locked()
3217 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3218 * @ctlr: SPI bus master that should be locked for exclusive bus access
3222 * is non-interruptible, and has no timeout.
3225 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3227 * and spi_async_locked calls when the SPI bus lock is held.
3235 mutex_lock(&ctlr->bus_lock_mutex); in spi_bus_lock()
3237 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3238 ctlr->bus_lock_flag = 1; in spi_bus_lock()
3239 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3248 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3249 * @ctlr: SPI bus master that was locked for exclusive bus access
3253 * is non-interruptible, and has no timeout.
3255 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3262 ctlr->bus_lock_flag = 0; in spi_bus_unlock()
3264 mutex_unlock(&ctlr->bus_lock_mutex); in spi_bus_unlock()
3276 * spi_write_then_read - SPI synchronous write followed by read
3277 * @spi: device with which data will be exchanged
3278 * @txbuf: data to be written (need not be dma-safe)
3280 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3285 * device, sending txbuf and then reading rxbuf. The return value
3291 * Performance-sensitive or bulk transfer code should instead use
3292 * spi_{async,sync}() calls with dma-safe buffers.
3296 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
3307 /* Use preallocated DMA-safe buffer if we can. We can't avoid in spi_write_then_read()
3310 * using the pre-allocated buffer or the transfer is too large. in spi_write_then_read()
3316 return -ENOMEM; in spi_write_then_read()
3337 status = spi_sync(spi, &message); in spi_write_then_read()
3350 /*-------------------------------------------------------------------------*/
3353 static int __spi_of_device_match(struct device *dev, void *data) in __spi_of_device_match()
3355 return dev->of_node == data; in __spi_of_device_match()
3358 /* must call put_device() when done with returned spi_device device */
3361 struct device *dev = bus_find_device(&spi_bus_type, NULL, node, in of_find_spi_device_by_node()
3366 static int __spi_of_controller_match(struct device *dev, const void *data) in __spi_of_controller_match()
3368 return dev->of_node == data; in __spi_of_controller_match()
3371 /* the spi controllers are not using spi_bus, so we find it with another way */
3374 struct device *dev; in of_find_spi_controller_by_node()
3393 struct spi_device *spi; in of_spi_notify() local
3397 ctlr = of_find_spi_controller_by_node(rd->dn->parent); in of_spi_notify()
3401 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { in of_spi_notify()
3402 put_device(&ctlr->dev); in of_spi_notify()
3406 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
3407 put_device(&ctlr->dev); in of_spi_notify()
3409 if (IS_ERR(spi)) { in of_spi_notify()
3411 __func__, rd->dn); in of_spi_notify()
3412 of_node_clear_flag(rd->dn, OF_POPULATED); in of_spi_notify()
3413 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
3419 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_spi_notify()
3422 /* find our device by node */ in of_spi_notify()
3423 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
3424 if (spi == NULL) in of_spi_notify()
3428 spi_unregister_device(spi); in of_spi_notify()
3431 put_device(&spi->dev); in of_spi_notify()
3446 static int spi_acpi_controller_match(struct device *dev, const void *data) in spi_acpi_controller_match()
3448 return ACPI_COMPANION(dev->parent) == data; in spi_acpi_controller_match()
3451 static int spi_acpi_device_match(struct device *dev, void *data) in spi_acpi_device_match()
3458 struct device *dev; in acpi_spi_find_controller_by_adev()
3473 struct device *dev; in acpi_spi_find_device_by_adev()
3485 struct spi_device *spi; in acpi_spi_notify() local
3489 ctlr = acpi_spi_find_controller_by_adev(adev->parent); in acpi_spi_notify()
3494 put_device(&ctlr->dev); in acpi_spi_notify()
3500 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
3501 if (!spi) in acpi_spi_notify()
3504 spi_unregister_device(spi); in acpi_spi_notify()
3505 put_device(&spi->dev); in acpi_spi_notify()
3525 status = -ENOMEM; in spi_init()
3564 * REVISIT only boardinfo really needs static linking. the rest (device and