Lines Matching +full:- +full:spi
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPI init/core code
9 #include <linux/clk/clk-conf.h>
13 #include <linux/dma-mapping.h>
34 #include <linux/spi/spi.h>
35 #include <linux/spi/spi-mem.h>
39 #include <trace/events/spi.h>
49 struct spi_device *spi = to_spi_device(dev); in spidev_release() local
51 spi_controller_put(spi->controller); in spidev_release()
52 kfree(spi->driver_override); in spidev_release()
53 free_percpu(spi->pcpu_statistics); in spidev_release()
54 kfree(spi); in spidev_release()
60 const struct spi_device *spi = to_spi_device(dev); in modalias_show() local
63 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); in modalias_show()
64 if (len != -ENODEV) in modalias_show()
67 return sysfs_emit(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); in modalias_show()
75 struct spi_device *spi = to_spi_device(dev); in driver_override_store() local
78 ret = driver_set_override(dev, &spi->driver_override, buf, count); in driver_override_store()
88 const struct spi_device *spi = to_spi_device(dev); in driver_override_show() local
92 len = sysfs_emit(buf, "%s\n", spi->driver_override ? : ""); in driver_override_show()
114 u64_stats_init(&stat->syncp); in spi_alloc_pcpu_stats()
135 start = u64_stats_fetch_begin(&pcpu_stats->syncp); in spi_emit_pcpu_stats()
137 } while (u64_stats_fetch_retry(&pcpu_stats->syncp, start)); in spi_emit_pcpu_stats()
150 return spi_statistics_##field##_show(ctlr->pcpu_statistics, buf); \
160 struct spi_device *spi = to_spi_device(dev); \
161 return spi_statistics_##field##_show(spi->pcpu_statistics, buf); \
198 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
199 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
200 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
201 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
202 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
203 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
204 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
205 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
206 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
207 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
208 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
209 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
210 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
211 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
212 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
213 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
317 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; in spi_statistics_add_transfer_stats()
325 u64_stats_update_begin(&stats->syncp); in spi_statistics_add_transfer_stats()
327 u64_stats_inc(&stats->transfers); in spi_statistics_add_transfer_stats()
328 u64_stats_inc(&stats->transfer_bytes_histo[l2len]); in spi_statistics_add_transfer_stats()
330 u64_stats_add(&stats->bytes, xfer->len); in spi_statistics_add_transfer_stats()
332 u64_stats_add(&stats->bytes_tx, xfer->len); in spi_statistics_add_transfer_stats()
334 u64_stats_add(&stats->bytes_rx, xfer->len); in spi_statistics_add_transfer_stats()
336 u64_stats_update_end(&stats->syncp); in spi_statistics_add_transfer_stats()
341 * modalias support makes "modprobe $MODALIAS" new-style hotplug work,
346 while (id->name[0]) { in spi_match_id()
347 if (!strcmp(name, id->name)) in spi_match_id()
356 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); in spi_get_device_id()
358 return spi_match_id(sdrv->id_table, sdev->modalias); in spi_get_device_id()
366 match = device_get_match_data(&sdev->dev); in spi_get_device_match_data()
370 return (const void *)spi_get_device_id(sdev)->driver_data; in spi_get_device_match_data()
376 const struct spi_device *spi = to_spi_device(dev); in spi_match_device() local
380 if (spi->driver_override) in spi_match_device()
381 return strcmp(spi->driver_override, drv->name) == 0; in spi_match_device()
391 if (sdrv->id_table) in spi_match_device()
392 return !!spi_match_id(sdrv->id_table, spi->modalias); in spi_match_device()
394 return strcmp(spi->modalias, drv->name) == 0; in spi_match_device()
399 const struct spi_device *spi = to_spi_device(dev); in spi_uevent() local
403 if (rc != -ENODEV) in spi_uevent()
406 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); in spi_uevent()
411 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_probe()
412 struct spi_device *spi = to_spi_device(dev); in spi_probe() local
415 ret = of_clk_set_defaults(dev->of_node, false); in spi_probe()
419 if (dev->of_node) { in spi_probe()
420 spi->irq = of_irq_get(dev->of_node, 0); in spi_probe()
421 if (spi->irq == -EPROBE_DEFER) in spi_probe()
422 return -EPROBE_DEFER; in spi_probe()
423 if (spi->irq < 0) in spi_probe()
424 spi->irq = 0; in spi_probe()
431 if (sdrv->probe) { in spi_probe()
432 ret = sdrv->probe(spi); in spi_probe()
442 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_remove()
444 if (sdrv->remove) in spi_remove()
445 sdrv->remove(to_spi_device(dev)); in spi_remove()
452 if (dev->driver) { in spi_shutdown()
453 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_shutdown()
455 if (sdrv->shutdown) in spi_shutdown()
456 sdrv->shutdown(to_spi_device(dev)); in spi_shutdown()
461 .name = "spi",
472 * __spi_register_driver - register a SPI driver
481 sdrv->driver.owner = owner; in __spi_register_driver()
482 sdrv->driver.bus = &spi_bus_type; in __spi_register_driver()
485 * For Really Good Reasons we use spi: modaliases not of: in __spi_register_driver()
489 if (sdrv->driver.of_match_table) { in __spi_register_driver()
492 for (of_id = sdrv->driver.of_match_table; of_id->compatible[0]; in __spi_register_driver()
497 of_name = strnchr(of_id->compatible, in __spi_register_driver()
498 sizeof(of_id->compatible), ','); in __spi_register_driver()
502 of_name = of_id->compatible; in __spi_register_driver()
504 if (sdrv->id_table) { in __spi_register_driver()
507 spi_id = spi_match_id(sdrv->id_table, of_name); in __spi_register_driver()
511 if (strcmp(sdrv->driver.name, of_name) == 0) in __spi_register_driver()
515 pr_warn("SPI driver %s has no spi_device_id for %s\n", in __spi_register_driver()
516 sdrv->driver.name, of_id->compatible); in __spi_register_driver()
520 return driver_register(&sdrv->driver); in __spi_register_driver()
524 /*-------------------------------------------------------------------------*/
527 * SPI devices should normally not be created by SPI device drivers; that
528 * would make them board-specific. Similarly with SPI controller drivers.
529 * Device registration normally goes into like arch/.../mach.../board-YYY.c
549 * spi_alloc_device - Allocate a new SPI device
559 * spi_device structure to add it to the SPI controller. If the caller
567 struct spi_device *spi; in spi_alloc_device() local
572 spi = kzalloc(sizeof(*spi), GFP_KERNEL); in spi_alloc_device()
573 if (!spi) { in spi_alloc_device()
578 spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL); in spi_alloc_device()
579 if (!spi->pcpu_statistics) { in spi_alloc_device()
580 kfree(spi); in spi_alloc_device()
585 spi->controller = ctlr; in spi_alloc_device()
586 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
587 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
588 spi->dev.release = spidev_release; in spi_alloc_device()
589 spi->mode = ctlr->buswidth_override_bits; in spi_alloc_device()
591 device_initialize(&spi->dev); in spi_alloc_device()
592 return spi; in spi_alloc_device()
596 static void spi_dev_set_name(struct spi_device *spi) in spi_dev_set_name() argument
598 struct device *dev = &spi->dev; in spi_dev_set_name()
602 dev_set_name(dev, "spi-%s", acpi_dev_name(to_acpi_device_node(fwnode))); in spi_dev_set_name()
607 dev_set_name(dev, "spi-%pfwP", fwnode); in spi_dev_set_name()
611 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
612 spi_get_chipselect(spi, 0)); in spi_dev_set_name()
617 * logical CS in the spi->chip_select[]. If all the physical CS
620 * CS can be 0. As a solution to this issue initialize all the CS to -1.
621 * Now all the unused logical CS will have -1 physical CS value & can be
624 #define SPI_INVALID_CS ((s8)-1)
632 struct spi_device *spi, u8 idx, in spi_dev_check_cs() argument
638 cs = spi_get_chipselect(spi, idx); in spi_dev_check_cs()
643 return -EBUSY; in spi_dev_check_cs()
651 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
655 if (spi->controller == new_spi->controller) { in spi_dev_check()
657 status = spi_dev_check_cs(dev, spi, idx, new_spi, 0); in spi_dev_check()
665 static void spi_cleanup(struct spi_device *spi) in spi_cleanup() argument
667 if (spi->controller->cleanup) in spi_cleanup()
668 spi->controller->cleanup(spi); in spi_cleanup()
671 static int __spi_add_device(struct spi_device *spi) in __spi_add_device() argument
673 struct spi_controller *ctlr = spi->controller; in __spi_add_device()
674 struct device *dev = ctlr->dev.parent; in __spi_add_device()
680 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
681 if (is_valid_cs(cs) && cs >= ctlr->num_chipselect) { in __spi_add_device()
682 dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx), in __spi_add_device()
683 ctlr->num_chipselect); in __spi_add_device()
684 return -EINVAL; in __spi_add_device()
690 * For example, spi->chip_select[0] != spi->chip_select[1] and so on. in __spi_add_device()
694 status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1); in __spi_add_device()
701 spi_dev_set_name(spi); in __spi_add_device()
708 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in __spi_add_device()
714 !device_is_registered(&ctlr->dev)) { in __spi_add_device()
715 return -ENODEV; in __spi_add_device()
718 if (ctlr->cs_gpiods) { in __spi_add_device()
722 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
724 spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]); in __spi_add_device()
733 status = spi_setup(spi); in __spi_add_device()
736 dev_name(&spi->dev), status); in __spi_add_device()
741 status = device_add(&spi->dev); in __spi_add_device()
744 dev_name(&spi->dev), status); in __spi_add_device()
745 spi_cleanup(spi); in __spi_add_device()
747 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in __spi_add_device()
754 * spi_add_device - Add spi_device allocated with spi_alloc_device
755 * @spi: spi_device to register
758 * spi_alloc_device can be added onto the SPI bus with this function.
762 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
764 struct spi_controller *ctlr = spi->controller; in spi_add_device()
768 spi_dev_set_name(spi); in spi_add_device()
770 mutex_lock(&ctlr->add_lock); in spi_add_device()
771 status = __spi_add_device(spi); in spi_add_device()
772 mutex_unlock(&ctlr->add_lock); in spi_add_device()
777 static void spi_set_all_cs_unused(struct spi_device *spi) in spi_set_all_cs_unused() argument
782 spi_set_chipselect(spi, idx, SPI_INVALID_CS); in spi_set_all_cs_unused()
786 * spi_new_device - instantiate one new SPI device
788 * @chip: Describes the SPI device
792 * after board init creates the hard-wired devices. Some development
795 * driver could add devices (which it would learn about out-of-band).
806 * NOTE: caller did any chip->bus_num checks necessary. in spi_new_device()
809 * error-or-pointer (not NULL-or-pointer), troubleshootability in spi_new_device()
817 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); in spi_new_device()
819 /* Use provided chip-select for proxy device */ in spi_new_device()
821 spi_set_chipselect(proxy, 0, chip->chip_select); in spi_new_device()
823 proxy->max_speed_hz = chip->max_speed_hz; in spi_new_device()
824 proxy->mode = chip->mode; in spi_new_device()
825 proxy->irq = chip->irq; in spi_new_device()
826 strscpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); in spi_new_device()
827 proxy->dev.platform_data = (void *) chip->platform_data; in spi_new_device()
828 proxy->controller_data = chip->controller_data; in spi_new_device()
829 proxy->controller_state = NULL; in spi_new_device()
831 * By default spi->chip_select[0] will hold the physical CS number, in spi_new_device()
832 * so set bit 0 in spi->cs_index_mask. in spi_new_device()
834 proxy->cs_index_mask = BIT(0); in spi_new_device()
836 if (chip->swnode) { in spi_new_device()
837 status = device_add_software_node(&proxy->dev, chip->swnode); in spi_new_device()
839 dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n", in spi_new_device()
840 chip->modalias, status); in spi_new_device()
852 device_remove_software_node(&proxy->dev); in spi_new_device()
859 * spi_unregister_device - unregister a single SPI device
860 * @spi: spi_device to unregister
862 * Start making the passed SPI device vanish. Normally this would be handled
865 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
867 if (!spi) in spi_unregister_device()
870 if (spi->dev.of_node) { in spi_unregister_device()
871 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
872 of_node_put(spi->dev.of_node); in spi_unregister_device()
874 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
875 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
876 device_remove_software_node(&spi->dev); in spi_unregister_device()
877 device_del(&spi->dev); in spi_unregister_device()
878 spi_cleanup(spi); in spi_unregister_device()
879 put_device(&spi->dev); in spi_unregister_device()
888 if (ctlr->bus_num != bi->bus_num) in spi_match_controller_to_boardinfo()
893 dev_err(ctlr->dev.parent, "can't create new device for %s\n", in spi_match_controller_to_boardinfo()
894 bi->modalias); in spi_match_controller_to_boardinfo()
898 * spi_register_board_info - register SPI devices for a given board
903 * Board-specific early init code calls this (probably during arch_initcall)
904 * with segments of the SPI device table. Any device nodes are created later,
905 * after the relevant parent SPI controller (bus_num) is defined. We keep
907 * not make Linux forget about these hard-wired devices.
909 * Other code can also call this, e.g. a particular add-on board might provide
910 * SPI devices through its expansion connector, so code initializing that board
911 * would naturally declare its SPI devices.
914 * any embedded pointers (platform_data, etc), they're copied as-is.
928 return -ENOMEM; in spi_register_board_info()
933 memcpy(&bi->board_info, info, sizeof(*info)); in spi_register_board_info()
936 list_add_tail(&bi->list, &board_list); in spi_register_board_info()
939 &bi->board_info); in spi_register_board_info()
946 /*-------------------------------------------------------------------------*/
948 /* Core methods for SPI resource management */
951 * spi_res_alloc - allocate a spi resource that is life-cycle managed
954 * @spi: the SPI device for which we allocate memory
964 static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release, in spi_res_alloc() argument
973 INIT_LIST_HEAD(&sres->entry); in spi_res_alloc()
974 sres->release = release; in spi_res_alloc()
976 return sres->data; in spi_res_alloc()
980 * spi_res_free - free an SPI resource
990 WARN_ON(!list_empty(&sres->entry)); in spi_res_free()
995 * spi_res_add - add a spi_res to the spi_message
996 * @message: the SPI message
1003 WARN_ON(!list_empty(&sres->entry)); in spi_res_add()
1004 list_add_tail(&sres->entry, &message->resources); in spi_res_add()
1008 * spi_res_release - release all SPI resources for this message
1016 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { in spi_res_release()
1017 if (res->release) in spi_res_release()
1018 res->release(ctlr, message, res->data); in spi_res_release()
1020 list_del(&res->entry); in spi_res_release()
1026 /*-------------------------------------------------------------------------*/
1027 #define spi_for_each_valid_cs(spi, idx) \ argument
1029 if (!(spi->cs_index_mask & BIT(idx))) {} else
1031 static inline bool spi_is_last_cs(struct spi_device *spi) in spi_is_last_cs() argument
1036 spi_for_each_valid_cs(spi, idx) { in spi_is_last_cs()
1037 if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx)) in spi_is_last_cs()
1043 static void spi_toggle_csgpiod(struct spi_device *spi, u8 idx, bool enable, bool activate) in spi_toggle_csgpiod() argument
1047 * thus the SPISerialBus() resource defines it on the per-chip in spi_toggle_csgpiod()
1055 if (has_acpi_companion(&spi->dev)) in spi_toggle_csgpiod()
1056 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), !enable); in spi_toggle_csgpiod()
1059 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), activate); in spi_toggle_csgpiod()
1062 spi_delay_exec(&spi->cs_setup, NULL); in spi_toggle_csgpiod()
1064 spi_delay_exec(&spi->cs_inactive, NULL); in spi_toggle_csgpiod()
1067 static void spi_set_cs(struct spi_device *spi, bool enable, bool force) in spi_set_cs() argument
1076 if (!force && ((enable && spi->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1077 spi_is_last_cs(spi)) || in spi_set_cs()
1078 (!enable && spi->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1079 !spi_is_last_cs(spi))) && in spi_set_cs()
1080 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
1083 trace_spi_set_cs(spi, activate); in spi_set_cs()
1085 spi->controller->last_cs_index_mask = spi->cs_index_mask; in spi_set_cs()
1087 spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : SPI_INVALID_CS; in spi_set_cs()
1088 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
1090 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
1097 if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate) in spi_set_cs()
1098 spi_delay_exec(&spi->cs_hold, NULL); in spi_set_cs()
1100 if (spi_is_csgpiod(spi)) { in spi_set_cs()
1101 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
1102 spi_for_each_valid_cs(spi, idx) { in spi_set_cs()
1103 if (spi_get_csgpiod(spi, idx)) in spi_set_cs()
1104 spi_toggle_csgpiod(spi, idx, enable, activate); in spi_set_cs()
1107 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
1108 if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) && in spi_set_cs()
1109 spi->controller->set_cs) in spi_set_cs()
1110 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1111 } else if (spi->controller->set_cs) { in spi_set_cs()
1112 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1115 if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) { in spi_set_cs()
1117 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
1119 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
1149 desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len); in spi_map_buf_attrs()
1152 return -EINVAL; in spi_map_buf_attrs()
1159 sg = &sgt->sgl[0]; in spi_map_buf_attrs()
1170 PAGE_SIZE - offset_in_page(buf))); in spi_map_buf_attrs()
1177 return -ENOMEM; in spi_map_buf_attrs()
1188 len -= min; in spi_map_buf_attrs()
1215 sgt->orig_nents = 0; in spi_unmap_buf_attrs()
1216 sgt->nents = 0; in spi_unmap_buf_attrs()
1231 if (!ctlr->can_dma) in __spi_map_msg()
1234 if (ctlr->dma_tx) in __spi_map_msg()
1235 tx_dev = ctlr->dma_tx->device->dev; in __spi_map_msg()
1236 else if (ctlr->dma_map_dev) in __spi_map_msg()
1237 tx_dev = ctlr->dma_map_dev; in __spi_map_msg()
1239 tx_dev = ctlr->dev.parent; in __spi_map_msg()
1241 if (ctlr->dma_rx) in __spi_map_msg()
1242 rx_dev = ctlr->dma_rx->device->dev; in __spi_map_msg()
1243 else if (ctlr->dma_map_dev) in __spi_map_msg()
1244 rx_dev = ctlr->dma_map_dev; in __spi_map_msg()
1246 rx_dev = ctlr->dev.parent; in __spi_map_msg()
1248 ret = -ENOMSG; in __spi_map_msg()
1249 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_map_msg()
1253 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
1256 if (xfer->tx_buf != NULL) { in __spi_map_msg()
1257 ret = spi_map_buf_attrs(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
1258 (void *)xfer->tx_buf, in __spi_map_msg()
1259 xfer->len, DMA_TO_DEVICE, in __spi_map_msg()
1264 xfer->tx_sg_mapped = true; in __spi_map_msg()
1267 if (xfer->rx_buf != NULL) { in __spi_map_msg()
1268 ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg, in __spi_map_msg()
1269 xfer->rx_buf, xfer->len, in __spi_map_msg()
1273 &xfer->tx_sg, DMA_TO_DEVICE, in __spi_map_msg()
1279 xfer->rx_sg_mapped = true; in __spi_map_msg()
1286 ctlr->cur_rx_dma_dev = rx_dev; in __spi_map_msg()
1287 ctlr->cur_tx_dma_dev = tx_dev; in __spi_map_msg()
1294 struct device *rx_dev = ctlr->cur_rx_dma_dev; in __spi_unmap_msg()
1295 struct device *tx_dev = ctlr->cur_tx_dma_dev; in __spi_unmap_msg()
1298 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_unmap_msg()
1302 if (xfer->rx_sg_mapped) in __spi_unmap_msg()
1303 spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg, in __spi_unmap_msg()
1305 xfer->rx_sg_mapped = false; in __spi_unmap_msg()
1307 if (xfer->tx_sg_mapped) in __spi_unmap_msg()
1308 spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg, in __spi_unmap_msg()
1310 xfer->tx_sg_mapped = false; in __spi_unmap_msg()
1319 struct device *rx_dev = ctlr->cur_rx_dma_dev; in spi_dma_sync_for_device()
1320 struct device *tx_dev = ctlr->cur_tx_dma_dev; in spi_dma_sync_for_device()
1322 if (xfer->tx_sg_mapped) in spi_dma_sync_for_device()
1323 dma_sync_sgtable_for_device(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in spi_dma_sync_for_device()
1324 if (xfer->rx_sg_mapped) in spi_dma_sync_for_device()
1325 dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in spi_dma_sync_for_device()
1331 struct device *rx_dev = ctlr->cur_rx_dma_dev; in spi_dma_sync_for_cpu()
1332 struct device *tx_dev = ctlr->cur_tx_dma_dev; in spi_dma_sync_for_cpu()
1334 if (xfer->rx_sg_mapped) in spi_dma_sync_for_cpu()
1335 dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in spi_dma_sync_for_cpu()
1336 if (xfer->tx_sg_mapped) in spi_dma_sync_for_cpu()
1337 dma_sync_sgtable_for_cpu(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in spi_dma_sync_for_cpu()
1368 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_unmap_msg()
1373 if (xfer->tx_buf == ctlr->dummy_tx) in spi_unmap_msg()
1374 xfer->tx_buf = NULL; in spi_unmap_msg()
1375 if (xfer->rx_buf == ctlr->dummy_rx) in spi_unmap_msg()
1376 xfer->rx_buf = NULL; in spi_unmap_msg()
1388 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) in spi_map_msg()
1389 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1393 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_map_msg()
1394 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && in spi_map_msg()
1395 !xfer->tx_buf) in spi_map_msg()
1396 max_tx = max(xfer->len, max_tx); in spi_map_msg()
1397 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && in spi_map_msg()
1398 !xfer->rx_buf) in spi_map_msg()
1399 max_rx = max(xfer->len, max_rx); in spi_map_msg()
1403 tmp = krealloc(ctlr->dummy_tx, max_tx, in spi_map_msg()
1406 return -ENOMEM; in spi_map_msg()
1407 ctlr->dummy_tx = tmp; in spi_map_msg()
1411 tmp = krealloc(ctlr->dummy_rx, max_rx, in spi_map_msg()
1414 return -ENOMEM; in spi_map_msg()
1415 ctlr->dummy_rx = tmp; in spi_map_msg()
1419 list_for_each_entry(xfer, &msg->transfers, in spi_map_msg()
1421 if (!xfer->len) in spi_map_msg()
1423 if (!xfer->tx_buf) in spi_map_msg()
1424 xfer->tx_buf = ctlr->dummy_tx; in spi_map_msg()
1425 if (!xfer->rx_buf) in spi_map_msg()
1426 xfer->rx_buf = ctlr->dummy_rx; in spi_map_msg()
1438 struct spi_statistics __percpu *statm = ctlr->pcpu_statistics; in spi_transfer_wait()
1439 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_wait()
1440 u32 speed_hz = xfer->speed_hz; in spi_transfer_wait()
1444 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { in spi_transfer_wait()
1445 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1446 return -EINTR; in spi_transfer_wait()
1453 * For each byte we wait for 8 cycles of the SPI clock. in spi_transfer_wait()
1458 ms = 8LL * MSEC_PER_SEC * xfer->len; in spi_transfer_wait()
1469 ms = wait_for_completion_timeout(&ctlr->xfer_completion, in spi_transfer_wait()
1475 dev_err(&msg->spi->dev, in spi_transfer_wait()
1476 "SPI transfer timed out\n"); in spi_transfer_wait()
1477 return -ETIMEDOUT; in spi_transfer_wait()
1480 if (xfer->error & SPI_TRANS_FAIL_IO) in spi_transfer_wait()
1481 return -EIO; in spi_transfer_wait()
1505 u32 delay = _delay->value; in spi_delay_to_ns()
1506 u32 unit = _delay->unit; in spi_delay_to_ns()
1522 return -EINVAL; in spi_delay_to_ns()
1527 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; in spi_delay_to_ns()
1529 return -EINVAL; in spi_delay_to_ns()
1535 return -EINVAL; in spi_delay_to_ns()
1549 return -EINVAL; in spi_delay_exec()
1565 u32 delay = xfer->cs_change_delay.value; in _spi_transfer_cs_change_delay()
1566 u32 unit = xfer->cs_change_delay.unit; in _spi_transfer_cs_change_delay()
1569 /* Return early on "fast" mode - for everything but USECS */ in _spi_transfer_cs_change_delay()
1576 ret = spi_delay_exec(&xfer->cs_change_delay, xfer); in _spi_transfer_cs_change_delay()
1578 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1593 * spi_transfer_one_message - Default implementation of transfer_one_message()
1605 struct spi_statistics __percpu *statm = ctlr->pcpu_statistics; in spi_transfer_one_message()
1606 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_one_message()
1608 xfer = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list); in spi_transfer_one_message()
1609 spi_set_cs(msg->spi, !xfer->cs_off, false); in spi_transfer_one_message()
1614 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_transfer_one_message()
1620 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1621 xfer->ptp_sts_word_pre = 0; in spi_transfer_one_message()
1622 ptp_read_system_prets(xfer->ptp_sts); in spi_transfer_one_message()
1625 if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { in spi_transfer_one_message()
1626 reinit_completion(&ctlr->xfer_completion); in spi_transfer_one_message()
1630 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1634 if ((xfer->tx_sg_mapped || xfer->rx_sg_mapped) && in spi_transfer_one_message()
1635 (xfer->error & SPI_TRANS_FAIL_NO_START)) { in spi_transfer_one_message()
1637 ctlr->fallback = true; in spi_transfer_one_message()
1638 xfer->error &= ~SPI_TRANS_FAIL_NO_START; in spi_transfer_one_message()
1646 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1647 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1654 msg->status = ret; in spi_transfer_one_message()
1659 if (xfer->len) in spi_transfer_one_message()
1660 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1662 xfer->len); in spi_transfer_one_message()
1665 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1666 ptp_read_system_postts(xfer->ptp_sts); in spi_transfer_one_message()
1667 xfer->ptp_sts_word_post = xfer->len; in spi_transfer_one_message()
1672 if (msg->status != -EINPROGRESS) in spi_transfer_one_message()
1677 if (xfer->cs_change) { in spi_transfer_one_message()
1678 if (list_is_last(&xfer->transfer_list, in spi_transfer_one_message()
1679 &msg->transfers)) { in spi_transfer_one_message()
1682 if (!xfer->cs_off) in spi_transfer_one_message()
1683 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1685 if (!list_next_entry(xfer, transfer_list)->cs_off) in spi_transfer_one_message()
1686 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1688 } else if (!list_is_last(&xfer->transfer_list, &msg->transfers) && in spi_transfer_one_message()
1689 xfer->cs_off != list_next_entry(xfer, transfer_list)->cs_off) { in spi_transfer_one_message()
1690 spi_set_cs(msg->spi, xfer->cs_off, false); in spi_transfer_one_message()
1693 msg->actual_length += xfer->len; in spi_transfer_one_message()
1698 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1700 if (msg->status == -EINPROGRESS) in spi_transfer_one_message()
1701 msg->status = ret; in spi_transfer_one_message()
1703 if (msg->status && ctlr->handle_err) in spi_transfer_one_message()
1704 ctlr->handle_err(ctlr, msg); in spi_transfer_one_message()
1712 * spi_finalize_current_transfer - report completion of a transfer
1715 * Called by SPI drivers using the core transfer_one_message()
1721 complete(&ctlr->xfer_completion); in spi_finalize_current_transfer()
1727 if (ctlr->auto_runtime_pm) { in spi_idle_runtime_pm()
1728 pm_runtime_mark_last_busy(ctlr->dev.parent); in spi_idle_runtime_pm()
1729 pm_runtime_put_autosuspend(ctlr->dev.parent); in spi_idle_runtime_pm()
1739 if (!was_busy && ctlr->auto_runtime_pm) { in __spi_pump_transfer_message()
1740 ret = pm_runtime_get_sync(ctlr->dev.parent); in __spi_pump_transfer_message()
1742 pm_runtime_put_noidle(ctlr->dev.parent); in __spi_pump_transfer_message()
1743 dev_err(&ctlr->dev, "Failed to power device: %d\n", in __spi_pump_transfer_message()
1746 msg->status = ret; in __spi_pump_transfer_message()
1756 if (!was_busy && ctlr->prepare_transfer_hardware) { in __spi_pump_transfer_message()
1757 ret = ctlr->prepare_transfer_hardware(ctlr); in __spi_pump_transfer_message()
1759 dev_err(&ctlr->dev, in __spi_pump_transfer_message()
1763 if (ctlr->auto_runtime_pm) in __spi_pump_transfer_message()
1764 pm_runtime_put(ctlr->dev.parent); in __spi_pump_transfer_message()
1766 msg->status = ret; in __spi_pump_transfer_message()
1775 if (ctlr->prepare_message) { in __spi_pump_transfer_message()
1776 ret = ctlr->prepare_message(ctlr, msg); in __spi_pump_transfer_message()
1778 dev_err(&ctlr->dev, "failed to prepare message: %d\n", in __spi_pump_transfer_message()
1780 msg->status = ret; in __spi_pump_transfer_message()
1784 msg->prepared = true; in __spi_pump_transfer_message()
1789 msg->status = ret; in __spi_pump_transfer_message()
1794 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in __spi_pump_transfer_message()
1795 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_pump_transfer_message()
1796 xfer->ptp_sts_word_pre = 0; in __spi_pump_transfer_message()
1797 ptp_read_system_prets(xfer->ptp_sts); in __spi_pump_transfer_message()
1807 * ctlr->cur_msg. in __spi_pump_transfer_message()
1814 WRITE_ONCE(ctlr->cur_msg_incomplete, true); in __spi_pump_transfer_message()
1815 WRITE_ONCE(ctlr->cur_msg_need_completion, false); in __spi_pump_transfer_message()
1816 reinit_completion(&ctlr->cur_msg_completion); in __spi_pump_transfer_message()
1819 ret = ctlr->transfer_one_message(ctlr, msg); in __spi_pump_transfer_message()
1821 dev_err(&ctlr->dev, in __spi_pump_transfer_message()
1826 WRITE_ONCE(ctlr->cur_msg_need_completion, true); in __spi_pump_transfer_message()
1828 if (READ_ONCE(ctlr->cur_msg_incomplete)) in __spi_pump_transfer_message()
1829 wait_for_completion(&ctlr->cur_msg_completion); in __spi_pump_transfer_message()
1835 * __spi_pump_messages - function which processes SPI message queue
1839 * This function checks if there is any SPI message in the queue that
1855 mutex_lock(&ctlr->io_mutex); in __spi_pump_messages()
1858 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1861 if (ctlr->cur_msg) in __spi_pump_messages()
1865 if (list_empty(&ctlr->queue) || !ctlr->running) { in __spi_pump_messages()
1866 if (!ctlr->busy) in __spi_pump_messages()
1869 /* Defer any non-atomic teardown to the thread */ in __spi_pump_messages()
1871 if (!ctlr->dummy_rx && !ctlr->dummy_tx && in __spi_pump_messages()
1872 !ctlr->unprepare_transfer_hardware) { in __spi_pump_messages()
1874 ctlr->busy = false; in __spi_pump_messages()
1875 ctlr->queue_empty = true; in __spi_pump_messages()
1878 kthread_queue_work(ctlr->kworker, in __spi_pump_messages()
1879 &ctlr->pump_messages); in __spi_pump_messages()
1884 ctlr->busy = false; in __spi_pump_messages()
1885 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1887 kfree(ctlr->dummy_rx); in __spi_pump_messages()
1888 ctlr->dummy_rx = NULL; in __spi_pump_messages()
1889 kfree(ctlr->dummy_tx); in __spi_pump_messages()
1890 ctlr->dummy_tx = NULL; in __spi_pump_messages()
1891 if (ctlr->unprepare_transfer_hardware && in __spi_pump_messages()
1892 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_pump_messages()
1893 dev_err(&ctlr->dev, in __spi_pump_messages()
1898 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1899 ctlr->queue_empty = true; in __spi_pump_messages()
1904 msg = list_first_entry(&ctlr->queue, struct spi_message, queue); in __spi_pump_messages()
1905 ctlr->cur_msg = msg; in __spi_pump_messages()
1907 list_del_init(&msg->queue); in __spi_pump_messages()
1908 if (ctlr->busy) in __spi_pump_messages()
1911 ctlr->busy = true; in __spi_pump_messages()
1912 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1915 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_pump_messages()
1917 ctlr->cur_msg = NULL; in __spi_pump_messages()
1918 ctlr->fallback = false; in __spi_pump_messages()
1920 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1928 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1929 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1933 * spi_pump_messages - kthread work function which processes spi message queue
1945 * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp
1957 * for the requested byte from the SPI transfer. The frequency with which this
1968 if (!xfer->ptp_sts) in spi_take_timestamp_pre()
1971 if (xfer->timestamped) in spi_take_timestamp_pre()
1974 if (progress > xfer->ptp_sts_word_pre) in spi_take_timestamp_pre()
1978 xfer->ptp_sts_word_pre = progress; in spi_take_timestamp_pre()
1981 local_irq_save(ctlr->irq_flags); in spi_take_timestamp_pre()
1985 ptp_read_system_prets(xfer->ptp_sts); in spi_take_timestamp_pre()
1990 * spi_take_timestamp_post - helper to collect the end of the TX timestamp
1994 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1997 * the requested byte from the SPI transfer. Can be called with an arbitrary
2005 if (!xfer->ptp_sts) in spi_take_timestamp_post()
2008 if (xfer->timestamped) in spi_take_timestamp_post()
2011 if (progress < xfer->ptp_sts_word_post) in spi_take_timestamp_post()
2014 ptp_read_system_postts(xfer->ptp_sts); in spi_take_timestamp_post()
2017 local_irq_restore(ctlr->irq_flags); in spi_take_timestamp_post()
2022 xfer->ptp_sts_word_post = progress; in spi_take_timestamp_post()
2024 xfer->timestamped = 1; in spi_take_timestamp_post()
2029 * spi_set_thread_rt - set the controller to pump at realtime priority
2033 * (by setting the ->rt value before calling spi_register_controller()) or
2045 dev_info(&ctlr->dev, in spi_set_thread_rt()
2047 sched_set_fifo(ctlr->kworker->task); in spi_set_thread_rt()
2052 ctlr->running = false; in spi_init_queue()
2053 ctlr->busy = false; in spi_init_queue()
2054 ctlr->queue_empty = true; in spi_init_queue()
2056 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); in spi_init_queue()
2057 if (IS_ERR(ctlr->kworker)) { in spi_init_queue()
2058 dev_err(&ctlr->dev, "failed to create message pump kworker\n"); in spi_init_queue()
2059 return PTR_ERR(ctlr->kworker); in spi_init_queue()
2062 kthread_init_work(&ctlr->pump_messages, spi_pump_messages); in spi_init_queue()
2071 if (ctlr->rt) in spi_init_queue()
2078 * spi_get_next_queued_message() - called by driver to check for queued
2093 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
2094 next = list_first_entry_or_null(&ctlr->queue, struct spi_message, in spi_get_next_queued_message()
2096 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
2103 * __spi_unoptimize_message - shared implementation of spi_unoptimize_message()
2115 struct spi_controller *ctlr = msg->spi->controller; in __spi_unoptimize_message()
2117 if (ctlr->unoptimize_message) in __spi_unoptimize_message()
2118 ctlr->unoptimize_message(msg); in __spi_unoptimize_message()
2122 msg->optimized = false; in __spi_unoptimize_message()
2123 msg->opt_state = NULL; in __spi_unoptimize_message()
2127 * spi_maybe_unoptimize_message - unoptimize msg not managed by a peripheral
2135 if (!msg->pre_optimized && msg->optimized && in spi_maybe_unoptimize_message()
2136 !msg->spi->controller->defer_optimize_message) in spi_maybe_unoptimize_message()
2141 * spi_finalize_current_message() - the current message is complete
2153 mesg = ctlr->cur_msg; in spi_finalize_current_message()
2155 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in spi_finalize_current_message()
2156 list_for_each_entry(xfer, &mesg->transfers, transfer_list) { in spi_finalize_current_message()
2157 ptp_read_system_postts(xfer->ptp_sts); in spi_finalize_current_message()
2158 xfer->ptp_sts_word_post = xfer->len; in spi_finalize_current_message()
2162 if (unlikely(ctlr->ptp_sts_supported)) in spi_finalize_current_message()
2163 list_for_each_entry(xfer, &mesg->transfers, transfer_list) in spi_finalize_current_message()
2164 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); in spi_finalize_current_message()
2168 if (mesg->prepared && ctlr->unprepare_message) { in spi_finalize_current_message()
2169 ret = ctlr->unprepare_message(ctlr, mesg); in spi_finalize_current_message()
2171 dev_err(&ctlr->dev, "failed to unprepare message: %d\n", in spi_finalize_current_message()
2176 mesg->prepared = false; in spi_finalize_current_message()
2180 WRITE_ONCE(ctlr->cur_msg_incomplete, false); in spi_finalize_current_message()
2182 if (READ_ONCE(ctlr->cur_msg_need_completion)) in spi_finalize_current_message()
2183 complete(&ctlr->cur_msg_completion); in spi_finalize_current_message()
2187 mesg->state = NULL; in spi_finalize_current_message()
2188 if (mesg->complete) in spi_finalize_current_message()
2189 mesg->complete(mesg->context); in spi_finalize_current_message()
2197 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_start_queue()
2199 if (ctlr->running || ctlr->busy) { in spi_start_queue()
2200 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
2201 return -EBUSY; in spi_start_queue()
2204 ctlr->running = true; in spi_start_queue()
2205 ctlr->cur_msg = NULL; in spi_start_queue()
2206 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
2208 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_start_queue()
2220 * A wait_queue on the ctlr->busy could be used, but then the common in spi_stop_queue()
2222 * friends on every SPI message. Do this instead. in spi_stop_queue()
2225 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
2226 if (list_empty(&ctlr->queue) && !ctlr->busy) { in spi_stop_queue()
2227 ctlr->running = false; in spi_stop_queue()
2228 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
2231 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
2233 } while (--limit); in spi_stop_queue()
2235 return -EBUSY; in spi_stop_queue()
2251 dev_err(&ctlr->dev, "problem destroying queue\n"); in spi_destroy_queue()
2255 kthread_destroy_worker(ctlr->kworker); in spi_destroy_queue()
2260 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
2264 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
2267 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_queued_transfer()
2269 if (!ctlr->running) { in __spi_queued_transfer()
2270 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
2271 return -ESHUTDOWN; in __spi_queued_transfer()
2273 msg->actual_length = 0; in __spi_queued_transfer()
2274 msg->status = -EINPROGRESS; in __spi_queued_transfer()
2276 list_add_tail(&msg->queue, &ctlr->queue); in __spi_queued_transfer()
2277 ctlr->queue_empty = false; in __spi_queued_transfer()
2278 if (!ctlr->busy && need_pump) in __spi_queued_transfer()
2279 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_queued_transfer()
2281 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
2286 * spi_queued_transfer - transfer function for queued transfers
2287 * @spi: SPI device which is requesting transfer
2288 * @msg: SPI message which is to handled is queued to driver queue
2292 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
2294 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
2301 ctlr->transfer = spi_queued_transfer; in spi_controller_initialize_queue()
2302 if (!ctlr->transfer_one_message) in spi_controller_initialize_queue()
2303 ctlr->transfer_one_message = spi_transfer_one_message; in spi_controller_initialize_queue()
2308 dev_err(&ctlr->dev, "problem initializing queue\n"); in spi_controller_initialize_queue()
2311 ctlr->queued = true; in spi_controller_initialize_queue()
2314 dev_err(&ctlr->dev, "problem starting queue\n"); in spi_controller_initialize_queue()
2327 * spi_flush_queue - Send all pending messages in the queue from the callers'
2332 * sent before doing something. Is used by the spi-mem code to make sure SPI
2333 * memory operations do not preempt regular SPI transfers that have been queued
2334 * before the spi-mem operation.
2338 if (ctlr->transfer == spi_queued_transfer) in spi_flush_queue()
2342 /*-------------------------------------------------------------------------*/
2352 delay->value = DIV_ROUND_UP(value, 1000); in of_spi_parse_dt_cs_delay()
2353 delay->unit = SPI_DELAY_UNIT_USECS; in of_spi_parse_dt_cs_delay()
2355 delay->value = value; in of_spi_parse_dt_cs_delay()
2356 delay->unit = SPI_DELAY_UNIT_NSECS; in of_spi_parse_dt_cs_delay()
2361 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
2368 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
2369 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
2370 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
2371 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
2372 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
2373 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
2374 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
2375 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
2376 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
2377 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
2380 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
2383 spi->mode |= SPI_NO_TX; in of_spi_parse_dt()
2388 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
2391 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
2394 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
2397 dev_warn(&ctlr->dev, in of_spi_parse_dt()
2398 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
2404 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
2407 spi->mode |= SPI_NO_RX; in of_spi_parse_dt()
2412 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
2415 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
2418 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
2421 dev_warn(&ctlr->dev, in of_spi_parse_dt()
2422 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
2430 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", in of_spi_parse_dt()
2432 return -EINVAL; in of_spi_parse_dt()
2437 if (ctlr->num_chipselect > SPI_CS_CNT_MAX) { in of_spi_parse_dt()
2438 dev_err(&ctlr->dev, "No. of CS is more than max. no. of supported CS\n"); in of_spi_parse_dt()
2439 return -EINVAL; in of_spi_parse_dt()
2442 spi_set_all_cs_unused(spi); in of_spi_parse_dt()
2448 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", in of_spi_parse_dt()
2452 if (rc > ctlr->num_chipselect) { in of_spi_parse_dt()
2453 dev_err(&ctlr->dev, "%pOF has number of CS > ctlr->num_chipselect (%d)\n", in of_spi_parse_dt()
2457 if ((of_property_read_bool(nc, "parallel-memories")) && in of_spi_parse_dt()
2458 (!(ctlr->flags & SPI_CONTROLLER_MULTI_CS))) { in of_spi_parse_dt()
2459 dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n"); in of_spi_parse_dt()
2460 return -EINVAL; in of_spi_parse_dt()
2463 spi_set_chipselect(spi, idx, cs[idx]); in of_spi_parse_dt()
2466 * By default spi->chip_select[0] will hold the physical CS number, in of_spi_parse_dt()
2467 * so set bit 0 in spi->cs_index_mask. in of_spi_parse_dt()
2469 spi->cs_index_mask = BIT(0); in of_spi_parse_dt()
2472 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2473 spi->max_speed_hz = value; in of_spi_parse_dt()
2476 of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns"); in of_spi_parse_dt()
2477 of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns"); in of_spi_parse_dt()
2478 of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns"); in of_spi_parse_dt()
2486 struct spi_device *spi; in of_register_spi_device() local
2490 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2491 if (!spi) { in of_register_spi_device()
2492 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); in of_register_spi_device()
2493 rc = -ENOMEM; in of_register_spi_device()
2498 rc = of_alias_from_compatible(nc, spi->modalias, in of_register_spi_device()
2499 sizeof(spi->modalias)); in of_register_spi_device()
2501 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); in of_register_spi_device()
2505 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2512 device_set_node(&spi->dev, of_fwnode_handle(nc)); in of_register_spi_device()
2515 rc = spi_add_device(spi); in of_register_spi_device()
2517 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); in of_register_spi_device()
2521 return spi; in of_register_spi_device()
2526 spi_dev_put(spi); in of_register_spi_device()
2531 * of_register_spi_devices() - Register child devices onto the SPI bus
2535 * represents a valid SPI slave.
2539 struct spi_device *spi; in of_register_spi_devices() local
2542 for_each_available_child_of_node(ctlr->dev.of_node, nc) { in of_register_spi_devices()
2545 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2546 if (IS_ERR(spi)) { in of_register_spi_devices()
2547 dev_warn(&ctlr->dev, in of_register_spi_devices()
2548 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2558 * spi_new_ancillary_device() - Register ancillary SPI device
2559 * @spi: Pointer to the main SPI device registering the ancillary device
2562 * Register an ancillary SPI device; for example some chips have a chip-select
2565 * This may only be called from main SPI device's probe routine.
2569 struct spi_device *spi_new_ancillary_device(struct spi_device *spi, in spi_new_ancillary_device() argument
2572 struct spi_controller *ctlr = spi->controller; in spi_new_ancillary_device()
2579 rc = -ENOMEM; in spi_new_ancillary_device()
2583 strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias)); in spi_new_ancillary_device()
2585 /* Use provided chip-select for ancillary device */ in spi_new_ancillary_device()
2589 /* Take over SPI mode/speed from SPI main device */ in spi_new_ancillary_device()
2590 ancillary->max_speed_hz = spi->max_speed_hz; in spi_new_ancillary_device()
2591 ancillary->mode = spi->mode; in spi_new_ancillary_device()
2593 * By default spi->chip_select[0] will hold the physical CS number, in spi_new_ancillary_device()
2594 * so set bit 0 in spi->cs_index_mask. in spi_new_ancillary_device()
2596 ancillary->cs_index_mask = BIT(0); in spi_new_ancillary_device()
2598 WARN_ON(!mutex_is_locked(&ctlr->add_lock)); in spi_new_ancillary_device()
2603 dev_err(&spi->dev, "failed to register ancillary device\n"); in spi_new_ancillary_device()
2632 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) in acpi_spi_count()
2635 sb = &ares->data.spi_serial_bus; in acpi_spi_count()
2636 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI) in acpi_spi_count()
2645 * acpi_spi_count_resources - Count the number of SpiSerialBus resources
2648 * Return: the number of SpiSerialBus resources in the ACPI-device's
2649 * resource-list; or a negative error code.
2676 && obj->buffer.length >= 4) in acpi_spi_parse_apple_properties()
2677 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2680 && obj->buffer.length == 8) in acpi_spi_parse_apple_properties()
2681 lookup->bits_per_word = *(u64 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2684 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2685 lookup->mode |= SPI_LSB_FIRST; in acpi_spi_parse_apple_properties()
2688 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2689 lookup->mode |= SPI_CPOL; in acpi_spi_parse_apple_properties()
2692 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2693 lookup->mode |= SPI_CPHA; in acpi_spi_parse_apple_properties()
2699 struct spi_controller *ctlr = lookup->ctlr; in acpi_spi_add_resource()
2701 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { in acpi_spi_add_resource()
2706 sb = &ares->data.spi_serial_bus; in acpi_spi_add_resource()
2707 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { in acpi_spi_add_resource()
2709 if (lookup->index != -1 && lookup->n++ != lookup->index) in acpi_spi_add_resource()
2713 sb->resource_source.string_ptr, in acpi_spi_add_resource()
2717 return -ENODEV; in acpi_spi_add_resource()
2720 if (!device_match_acpi_handle(ctlr->dev.parent, parent_handle)) in acpi_spi_add_resource()
2721 return -ENODEV; in acpi_spi_add_resource()
2727 return -ENODEV; in acpi_spi_add_resource()
2731 return -EPROBE_DEFER; in acpi_spi_add_resource()
2733 lookup->ctlr = ctlr; in acpi_spi_add_resource()
2740 * 0 .. max - 1 so we need to ask the driver to in acpi_spi_add_resource()
2743 if (ctlr->fw_translate_cs) { in acpi_spi_add_resource()
2744 int cs = ctlr->fw_translate_cs(ctlr, in acpi_spi_add_resource()
2745 sb->device_selection); in acpi_spi_add_resource()
2748 lookup->chip_select = cs; in acpi_spi_add_resource()
2750 lookup->chip_select = sb->device_selection; in acpi_spi_add_resource()
2753 lookup->max_speed_hz = sb->connection_speed; in acpi_spi_add_resource()
2754 lookup->bits_per_word = sb->data_bit_length; in acpi_spi_add_resource()
2756 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) in acpi_spi_add_resource()
2757 lookup->mode |= SPI_CPHA; in acpi_spi_add_resource()
2758 if (sb->clock_polarity == ACPI_SPI_START_HIGH) in acpi_spi_add_resource()
2759 lookup->mode |= SPI_CPOL; in acpi_spi_add_resource()
2760 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) in acpi_spi_add_resource()
2761 lookup->mode |= SPI_CS_HIGH; in acpi_spi_add_resource()
2763 } else if (lookup->irq < 0) { in acpi_spi_add_resource()
2767 lookup->irq = r.start; in acpi_spi_add_resource()
2775 * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2776 * @ctlr: controller to which the spi device belongs
2777 * @adev: ACPI Device for the spi device
2778 * @index: Index of the spi resource inside the ACPI Node
2780 * This should be used to allocate a new SPI device from and ACPI Device node.
2781 * The caller is responsible for calling spi_add_device to register the SPI device.
2783 * If ctlr is set to NULL, the Controller for the SPI device will be looked up
2785 * If index is set to -1, index is not used.
2786 * Note: If index is -1, ctlr must be set.
2797 struct spi_device *spi; in acpi_spi_device_alloc() local
2800 if (!ctlr && index == -1) in acpi_spi_device_alloc()
2801 return ERR_PTR(-EINVAL); in acpi_spi_device_alloc()
2804 lookup.irq = -1; in acpi_spi_device_alloc()
2814 /* Found SPI in _CRS but it points to another controller */ in acpi_spi_device_alloc()
2818 ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && in acpi_spi_device_alloc()
2819 device_match_acpi_handle(lookup.ctlr->dev.parent, parent_handle)) { in acpi_spi_device_alloc()
2820 /* Apple does not use _CRS but nested devices for SPI slaves */ in acpi_spi_device_alloc()
2825 return ERR_PTR(-ENODEV); in acpi_spi_device_alloc()
2827 spi = spi_alloc_device(lookup.ctlr); in acpi_spi_device_alloc()
2828 if (!spi) { in acpi_spi_device_alloc()
2829 dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_spi_device_alloc()
2830 dev_name(&adev->dev)); in acpi_spi_device_alloc()
2831 return ERR_PTR(-ENOMEM); in acpi_spi_device_alloc()
2834 spi_set_all_cs_unused(spi); in acpi_spi_device_alloc()
2835 spi_set_chipselect(spi, 0, lookup.chip_select); in acpi_spi_device_alloc()
2837 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_spi_device_alloc()
2838 spi->max_speed_hz = lookup.max_speed_hz; in acpi_spi_device_alloc()
2839 spi->mode |= lookup.mode; in acpi_spi_device_alloc()
2840 spi->irq = lookup.irq; in acpi_spi_device_alloc()
2841 spi->bits_per_word = lookup.bits_per_word; in acpi_spi_device_alloc()
2843 * By default spi->chip_select[0] will hold the physical CS number, in acpi_spi_device_alloc()
2844 * so set bit 0 in spi->cs_index_mask. in acpi_spi_device_alloc()
2846 spi->cs_index_mask = BIT(0); in acpi_spi_device_alloc()
2848 return spi; in acpi_spi_device_alloc()
2855 struct spi_device *spi; in acpi_register_spi_device() local
2857 if (acpi_bus_get_status(adev) || !adev->status.present || in acpi_register_spi_device()
2861 spi = acpi_spi_device_alloc(ctlr, adev, -1); in acpi_register_spi_device()
2862 if (IS_ERR(spi)) { in acpi_register_spi_device()
2863 if (PTR_ERR(spi) == -ENOMEM) in acpi_register_spi_device()
2869 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2870 sizeof(spi->modalias)); in acpi_register_spi_device()
2872 if (spi->irq < 0) in acpi_register_spi_device()
2873 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in acpi_register_spi_device()
2877 adev->power.flags.ignore_parent = true; in acpi_register_spi_device()
2878 if (spi_add_device(spi)) { in acpi_register_spi_device()
2879 adev->power.flags.ignore_parent = false; in acpi_register_spi_device()
2880 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2881 dev_name(&adev->dev)); in acpi_register_spi_device()
2882 spi_dev_put(spi); in acpi_register_spi_device()
2907 handle = ACPI_HANDLE(ctlr->dev.parent); in acpi_register_spi_devices()
2915 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2937 * spi_target_abort - abort the ongoing transfer request on an SPI slave
2939 * @spi: device used for the current transfer
2941 int spi_target_abort(struct spi_device *spi) in spi_target_abort() argument
2943 struct spi_controller *ctlr = spi->controller; in spi_target_abort()
2945 if (spi_controller_is_target(ctlr) && ctlr->target_abort) in spi_target_abort()
2946 return ctlr->target_abort(ctlr); in spi_target_abort()
2948 return -ENOTSUPP; in spi_target_abort()
2959 child = device_find_any_child(&ctlr->dev); in slave_show()
2960 return sysfs_emit(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL); in slave_show()
2968 struct spi_device *spi; in slave_store() local
2975 return -EINVAL; in slave_store()
2977 child = device_find_any_child(&ctlr->dev); in slave_store()
2986 spi = spi_alloc_device(ctlr); in slave_store()
2987 if (!spi) in slave_store()
2988 return -ENOMEM; in slave_store()
2990 strscpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2992 rc = spi_add_device(spi); in slave_store()
2994 spi_dev_put(spi); in slave_store()
3029 * __spi_alloc_controller - allocate an SPI master or slave controller
3031 * @size: how much zeroed driver-private data to allocate; the pointer to this
3036 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
3040 * This call is used only by SPI controller drivers, which are the
3051 * Return: the SPI controller structure on success, else NULL.
3066 device_initialize(&ctlr->dev); in __spi_alloc_controller()
3067 INIT_LIST_HEAD(&ctlr->queue); in __spi_alloc_controller()
3068 spin_lock_init(&ctlr->queue_lock); in __spi_alloc_controller()
3069 spin_lock_init(&ctlr->bus_lock_spinlock); in __spi_alloc_controller()
3070 mutex_init(&ctlr->bus_lock_mutex); in __spi_alloc_controller()
3071 mutex_init(&ctlr->io_mutex); in __spi_alloc_controller()
3072 mutex_init(&ctlr->add_lock); in __spi_alloc_controller()
3073 ctlr->bus_num = -1; in __spi_alloc_controller()
3074 ctlr->num_chipselect = 1; in __spi_alloc_controller()
3075 ctlr->slave = slave; in __spi_alloc_controller()
3077 ctlr->dev.class = &spi_slave_class; in __spi_alloc_controller()
3079 ctlr->dev.class = &spi_master_class; in __spi_alloc_controller()
3080 ctlr->dev.parent = dev; in __spi_alloc_controller()
3081 pm_suspend_ignore_children(&ctlr->dev, true); in __spi_alloc_controller()
3094 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
3095 * @dev: physical device of SPI controller
3096 * @size: how much zeroed driver-private data to allocate
3097 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
3100 * Allocate an SPI controller and automatically release a reference on it
3106 * Return: the SPI controller structure on success, else NULL.
3121 ctlr->devm_allocated = true; in __devm_spi_alloc_controller()
3133 * spi_get_gpio_descs() - grab chip select GPIOs for the master
3134 * @ctlr: The SPI master to grab GPIO descriptors for
3140 struct device *dev = &ctlr->dev; in spi_get_gpio_descs()
3147 if (nb == -ENOENT) in spi_get_gpio_descs()
3152 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in spi_get_gpio_descs()
3154 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), in spi_get_gpio_descs()
3157 return -ENOMEM; in spi_get_gpio_descs()
3158 ctlr->cs_gpiods = cs; in spi_get_gpio_descs()
3183 return -ENOMEM; in spi_get_gpio_descs()
3189 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { in spi_get_gpio_descs()
3191 return -EINVAL; in spi_get_gpio_descs()
3196 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; in spi_get_gpio_descs()
3198 if ((ctlr->flags & SPI_CONTROLLER_GPIO_SS) && num_cs_gpios && in spi_get_gpio_descs()
3199 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { in spi_get_gpio_descs()
3201 return -EINVAL; in spi_get_gpio_descs()
3210 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
3211 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
3213 * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least in spi_controller_check_ops()
3214 * one of the ->transfer_xxx() method be implemented. in spi_controller_check_ops()
3216 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { in spi_controller_check_ops()
3217 if (!ctlr->transfer && !ctlr->transfer_one && in spi_controller_check_ops()
3218 !ctlr->transfer_one_message) { in spi_controller_check_ops()
3219 return -EINVAL; in spi_controller_check_ops()
3235 return id == -ENOSPC ? -EBUSY : id; in spi_controller_id_alloc()
3236 ctlr->bus_num = id; in spi_controller_id_alloc()
3241 * spi_register_controller - register SPI master or slave controller
3246 * SPI controllers connect to their drivers using some non-SPI bus,
3248 * includes calling spi_register_controller() to hook up to this SPI bus glue.
3250 * SPI controllers use board specific (often SOC specific) bus numbers,
3251 * and board-specific addressing for SPI devices combines those numbers
3252 * with chip select numbers. Since SPI does not directly support dynamic
3265 struct device *dev = ctlr->dev.parent; in spi_register_controller()
3272 return -ENODEV; in spi_register_controller()
3276 * the SPI controller. in spi_register_controller()
3282 if (ctlr->bus_num < 0) in spi_register_controller()
3283 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
3284 if (ctlr->bus_num >= 0) { in spi_register_controller()
3285 /* Devices with a fixed bus num must check-in with the num */ in spi_register_controller()
3286 status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1); in spi_register_controller()
3290 if (ctlr->bus_num < 0) { in spi_register_controller()
3291 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
3301 ctlr->bus_lock_flag = 0; in spi_register_controller()
3302 init_completion(&ctlr->xfer_completion); in spi_register_controller()
3303 init_completion(&ctlr->cur_msg_completion); in spi_register_controller()
3304 if (!ctlr->max_dma_len) in spi_register_controller()
3305 ctlr->max_dma_len = INT_MAX; in spi_register_controller()
3311 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
3313 if (!spi_controller_is_target(ctlr) && ctlr->use_gpio_descriptors) { in spi_register_controller()
3321 ctlr->mode_bits |= SPI_CS_HIGH; in spi_register_controller()
3325 * Even if it's just one always-selected device, there must in spi_register_controller()
3328 if (!ctlr->num_chipselect) { in spi_register_controller()
3329 status = -EINVAL; in spi_register_controller()
3335 ctlr->last_cs[idx] = SPI_INVALID_CS; in spi_register_controller()
3337 status = device_add(&ctlr->dev); in spi_register_controller()
3342 dev_name(&ctlr->dev)); in spi_register_controller()
3346 * need the queueing logic if the driver is only supporting high-level in spi_register_controller()
3349 if (ctlr->transfer) { in spi_register_controller()
3351 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { in spi_register_controller()
3354 device_del(&ctlr->dev); in spi_register_controller()
3359 ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev); in spi_register_controller()
3360 if (!ctlr->pcpu_statistics) { in spi_register_controller()
3361 dev_err(dev, "Error allocating per-cpu statistics\n"); in spi_register_controller()
3362 status = -ENOMEM; in spi_register_controller()
3367 list_add_tail(&ctlr->list, &spi_controller_list); in spi_register_controller()
3369 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); in spi_register_controller()
3381 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
3393 * devm_spi_register_controller - register managed SPI master or slave
3395 * @dev: device managing SPI controller
3400 * Register a SPI device as with spi_register_controller() which will
3413 return -ENOMEM; in devm_spi_register_controller()
3434 * spi_unregister_controller - unregister SPI master or slave controller
3438 * This call is used only by SPI controller drivers, which are the
3448 int id = ctlr->bus_num; in spi_unregister_controller()
3452 mutex_lock(&ctlr->add_lock); in spi_unregister_controller()
3454 device_for_each_child(&ctlr->dev, NULL, __unregister); in spi_unregister_controller()
3460 if (ctlr->queued) { in spi_unregister_controller()
3462 dev_err(&ctlr->dev, "queue remove failed\n"); in spi_unregister_controller()
3465 list_del(&ctlr->list); in spi_unregister_controller()
3468 device_del(&ctlr->dev); in spi_unregister_controller()
3477 mutex_unlock(&ctlr->add_lock); in spi_unregister_controller()
3483 if (!ctlr->devm_allocated) in spi_unregister_controller()
3484 put_device(&ctlr->dev); in spi_unregister_controller()
3490 return ctlr->flags & SPI_CONTROLLER_SUSPENDED ? -ESHUTDOWN : 0; in __spi_check_suspended()
3495 mutex_lock(&ctlr->bus_lock_mutex); in __spi_mark_suspended()
3496 ctlr->flags |= SPI_CONTROLLER_SUSPENDED; in __spi_mark_suspended()
3497 mutex_unlock(&ctlr->bus_lock_mutex); in __spi_mark_suspended()
3502 mutex_lock(&ctlr->bus_lock_mutex); in __spi_mark_resumed()
3503 ctlr->flags &= ~SPI_CONTROLLER_SUSPENDED; in __spi_mark_resumed()
3504 mutex_unlock(&ctlr->bus_lock_mutex); in __spi_mark_resumed()
3511 /* Basically no-ops for non-queued controllers */ in spi_controller_suspend()
3512 if (ctlr->queued) { in spi_controller_suspend()
3515 dev_err(&ctlr->dev, "queue stop failed\n"); in spi_controller_suspend()
3529 if (ctlr->queued) { in spi_controller_resume()
3532 dev_err(&ctlr->dev, "queue restart failed\n"); in spi_controller_resume()
3538 /*-------------------------------------------------------------------------*/
3550 if (rxfer->release) in __spi_replace_transfers_release()
3551 rxfer->release(ctlr, msg, res); in __spi_replace_transfers_release()
3554 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); in __spi_replace_transfers_release()
3557 for (i = 0; i < rxfer->inserted; i++) in __spi_replace_transfers_release()
3558 list_del(&rxfer->inserted_transfers[i].transfer_list); in __spi_replace_transfers_release()
3562 * spi_replace_transfers - replace transfers with several transfers
3590 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3595 return ERR_PTR(-ENOMEM); in spi_replace_transfers()
3598 rxfer->release = release; in spi_replace_transfers()
3602 rxfer->extradata = in spi_replace_transfers()
3603 &rxfer->inserted_transfers[insert]; in spi_replace_transfers()
3606 INIT_LIST_HEAD(&rxfer->replaced_transfers); in spi_replace_transfers()
3610 * the @replaced_transfers - it may be spi_message.messages! in spi_replace_transfers()
3612 rxfer->replaced_after = xfer_first->transfer_list.prev; in spi_replace_transfers()
3617 * If the entry after replaced_after it is msg->transfers in spi_replace_transfers()
3621 if (rxfer->replaced_after->next == &msg->transfers) { in spi_replace_transfers()
3622 dev_err(&msg->spi->dev, in spi_replace_transfers()
3625 list_splice(&rxfer->replaced_transfers, in spi_replace_transfers()
3626 rxfer->replaced_after); in spi_replace_transfers()
3632 return ERR_PTR(-EINVAL); in spi_replace_transfers()
3639 list_move_tail(rxfer->replaced_after->next, in spi_replace_transfers()
3640 &rxfer->replaced_transfers); in spi_replace_transfers()
3649 xfer = &rxfer->inserted_transfers[insert - 1 - i]; in spi_replace_transfers()
3655 list_add(&xfer->transfer_list, rxfer->replaced_after); in spi_replace_transfers()
3659 xfer->cs_change = false; in spi_replace_transfers()
3660 xfer->delay.value = 0; in spi_replace_transfers()
3665 rxfer->inserted = insert; in spi_replace_transfers()
3684 count = DIV_ROUND_UP(xfer->len, maxsize); in __spi_split_transfer_maxsize()
3690 xfers = srt->inserted_transfers; in __spi_split_transfer_maxsize()
3715 xfers[i].len = min(maxsize, xfers[i].len - offset); in __spi_split_transfer_maxsize()
3722 *xferp = &xfers[count - 1]; in __spi_split_transfer_maxsize()
3725 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, in __spi_split_transfer_maxsize()
3727 SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, in __spi_split_transfer_maxsize()
3734 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3742 * spi message unoptimize phase so this function should only be called from
3761 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxsize()
3762 if (xfer->len > maxsize) { in spi_split_transfers_maxsize()
3776 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3778 * certain number of SPI words
3784 * spi message unoptimize phase so this function should only be called from
3802 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxwords()
3806 maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word)); in spi_split_transfers_maxwords()
3807 if (xfer->len > maxsize) { in spi_split_transfers_maxwords()
3819 /*-------------------------------------------------------------------------*/
3822 * Core methods for SPI controller protocol drivers. Some of the
3829 if (ctlr->bits_per_word_mask) { in __spi_validate_bits_per_word()
3832 return -EINVAL; in __spi_validate_bits_per_word()
3833 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) in __spi_validate_bits_per_word()
3834 return -EINVAL; in __spi_validate_bits_per_word()
3841 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3842 * @spi: the device that requires specific CS timing configuration
3846 static int spi_set_cs_timing(struct spi_device *spi) in spi_set_cs_timing() argument
3848 struct device *parent = spi->controller->dev.parent; in spi_set_cs_timing()
3851 if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) { in spi_set_cs_timing()
3852 if (spi->controller->auto_runtime_pm) { in spi_set_cs_timing()
3856 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_set_cs_timing()
3861 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3865 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3872 * spi_setup - setup SPI mode and clock rate
3873 * @spi: the device whose settings are being modified
3876 * SPI protocol drivers may need to update the transfer mode if the
3882 * or from it. When this function returns, the SPI device is deselected.
3887 * LSB-first wire encoding, or active-high chipselects.
3891 int spi_setup(struct spi_device *spi) in spi_setup() argument
3900 if ((hweight_long(spi->mode & in spi_setup()
3902 (hweight_long(spi->mode & in spi_setup()
3904 dev_err(&spi->dev, in spi_setup()
3905 "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); in spi_setup()
3906 return -EINVAL; in spi_setup()
3909 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3912 return -EINVAL; in spi_setup()
3914 if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) { in spi_setup()
3915 dev_err(&spi->dev, in spi_setup()
3917 return -EINVAL; in spi_setup()
3925 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | in spi_setup()
3931 dev_warn(&spi->dev, in spi_setup()
3934 spi->mode &= ~ugly_bits; in spi_setup()
3938 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3940 return -EINVAL; in spi_setup()
3943 if (!spi->bits_per_word) { in spi_setup()
3944 spi->bits_per_word = 8; in spi_setup()
3947 * Some controllers may not support the default 8 bits-per-word in spi_setup()
3950 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3951 spi->bits_per_word); in spi_setup()
3956 if (spi->controller->max_speed_hz && in spi_setup()
3957 (!spi->max_speed_hz || in spi_setup()
3958 spi->max_speed_hz > spi->controller->max_speed_hz)) in spi_setup()
3959 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3961 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3963 if (spi->controller->setup) { in spi_setup()
3964 status = spi->controller->setup(spi); in spi_setup()
3966 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3967 dev_err(&spi->controller->dev, "Failed to setup device: %d\n", in spi_setup()
3973 status = spi_set_cs_timing(spi); in spi_setup()
3975 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3979 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3980 status = pm_runtime_resume_and_get(spi->controller->dev.parent); in spi_setup()
3982 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3983 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3991 * checking for a non-zero return value instead of a negative in spi_setup()
3996 spi_set_cs(spi, false, true); in spi_setup()
3997 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3998 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
4000 spi_set_cs(spi, false, true); in spi_setup()
4003 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
4005 if (spi->rt && !spi->controller->rt) { in spi_setup()
4006 spi->controller->rt = true; in spi_setup()
4007 spi_set_thread_rt(spi->controller); in spi_setup()
4010 trace_spi_setup(spi, status); in spi_setup()
4012 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
4013 spi->mode & SPI_MODE_X_MASK, in spi_setup()
4014 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
4015 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
4016 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
4017 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
4018 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
4026 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
4030 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); in _spi_xfer_word_delay_update()
4034 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
4039 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
4040 sizeof(xfer->word_delay)); in _spi_xfer_word_delay_update()
4045 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
4047 struct spi_controller *ctlr = spi->controller; in __spi_validate()
4051 if (list_empty(&message->transfers)) in __spi_validate()
4052 return -EINVAL; in __spi_validate()
4054 message->spi = spi; in __spi_validate()
4057 * Half-duplex links include original MicroWire, and ones with in __spi_validate()
4062 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || in __spi_validate()
4063 (spi->mode & SPI_3WIRE)) { in __spi_validate()
4064 unsigned flags = ctlr->flags; in __spi_validate()
4066 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
4067 if (xfer->rx_buf && xfer->tx_buf) in __spi_validate()
4068 return -EINVAL; in __spi_validate()
4069 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) in __spi_validate()
4070 return -EINVAL; in __spi_validate()
4071 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) in __spi_validate()
4072 return -EINVAL; in __spi_validate()
4077 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
4084 message->frame_length = 0; in __spi_validate()
4085 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
4086 xfer->effective_speed_hz = 0; in __spi_validate()
4087 message->frame_length += xfer->len; in __spi_validate()
4088 if (!xfer->bits_per_word) in __spi_validate()
4089 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
4091 if (!xfer->speed_hz) in __spi_validate()
4092 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
4094 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) in __spi_validate()
4095 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
4097 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) in __spi_validate()
4098 return -EINVAL; in __spi_validate()
4101 * SPI transfer length should be multiple of SPI word size in __spi_validate()
4102 * where SPI word size should be power-of-two multiple. in __spi_validate()
4104 if (xfer->bits_per_word <= 8) in __spi_validate()
4106 else if (xfer->bits_per_word <= 16) in __spi_validate()
4112 if (xfer->len % w_size) in __spi_validate()
4113 return -EINVAL; in __spi_validate()
4115 if (xfer->speed_hz && ctlr->min_speed_hz && in __spi_validate()
4116 xfer->speed_hz < ctlr->min_speed_hz) in __spi_validate()
4117 return -EINVAL; in __spi_validate()
4119 if (xfer->tx_buf && !xfer->tx_nbits) in __spi_validate()
4120 xfer->tx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
4121 if (xfer->rx_buf && !xfer->rx_nbits) in __spi_validate()
4122 xfer->rx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
4128 if (xfer->tx_buf) { in __spi_validate()
4129 if (spi->mode & SPI_NO_TX) in __spi_validate()
4130 return -EINVAL; in __spi_validate()
4131 if (xfer->tx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
4132 xfer->tx_nbits != SPI_NBITS_DUAL && in __spi_validate()
4133 xfer->tx_nbits != SPI_NBITS_QUAD && in __spi_validate()
4134 xfer->tx_nbits != SPI_NBITS_OCTAL) in __spi_validate()
4135 return -EINVAL; in __spi_validate()
4136 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
4137 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
4138 return -EINVAL; in __spi_validate()
4139 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
4140 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
4141 return -EINVAL; in __spi_validate()
4144 if (xfer->rx_buf) { in __spi_validate()
4145 if (spi->mode & SPI_NO_RX) in __spi_validate()
4146 return -EINVAL; in __spi_validate()
4147 if (xfer->rx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
4148 xfer->rx_nbits != SPI_NBITS_DUAL && in __spi_validate()
4149 xfer->rx_nbits != SPI_NBITS_QUAD && in __spi_validate()
4150 xfer->rx_nbits != SPI_NBITS_OCTAL) in __spi_validate()
4151 return -EINVAL; in __spi_validate()
4152 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
4153 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
4154 return -EINVAL; in __spi_validate()
4155 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
4156 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
4157 return -EINVAL; in __spi_validate()
4160 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
4161 return -EINVAL; in __spi_validate()
4164 message->status = -EINPROGRESS; in __spi_validate()
4170 * spi_split_transfers - generic handling of transfer splitting
4173 * Under certain conditions, a SPI controller may not support arbitrary
4186 struct spi_controller *ctlr = msg->spi->controller; in spi_split_transfers()
4191 * If an SPI controller does not support toggling the CS line on each in spi_split_transfers()
4193 * for the CS line, we can emulate the CS-per-word hardware function by in spi_split_transfers()
4194 * splitting transfers into one-word transfers and ensuring that in spi_split_transfers()
4197 if ((msg->spi->mode & SPI_CS_WORD) && in spi_split_transfers()
4198 (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) { in spi_split_transfers()
4203 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers()
4205 if (list_is_last(&xfer->transfer_list, &msg->transfers)) in spi_split_transfers()
4208 xfer->cs_change = 1; in spi_split_transfers()
4212 spi_max_transfer_size(msg->spi)); in spi_split_transfers()
4221 * __spi_optimize_message - shared implementation for spi_optimize_message()
4223 * @spi: the device that will be used for the message
4226 * Peripheral drivers will call spi_optimize_message() and the spi core will
4233 static int __spi_optimize_message(struct spi_device *spi, in __spi_optimize_message() argument
4236 struct spi_controller *ctlr = spi->controller; in __spi_optimize_message()
4239 ret = __spi_validate(spi, msg); in __spi_optimize_message()
4247 if (ctlr->optimize_message) { in __spi_optimize_message()
4248 ret = ctlr->optimize_message(msg); in __spi_optimize_message()
4255 msg->optimized = true; in __spi_optimize_message()
4261 * spi_maybe_optimize_message - optimize message if it isn't already pre-optimized
4262 * @spi: the device that will be used for the message
4266 static int spi_maybe_optimize_message(struct spi_device *spi, in spi_maybe_optimize_message() argument
4269 if (spi->controller->defer_optimize_message) { in spi_maybe_optimize_message()
4270 msg->spi = spi; in spi_maybe_optimize_message()
4274 if (msg->pre_optimized) in spi_maybe_optimize_message()
4277 return __spi_optimize_message(spi, msg); in spi_maybe_optimize_message()
4281 * spi_optimize_message - do any one-time validation and setup for a SPI message
4282 * @spi: the device that will be used for the message
4291 * of updating the contents of any xfer->tx_buf (the pointer can't be changed,
4300 int spi_optimize_message(struct spi_device *spi, struct spi_message *msg) in spi_optimize_message() argument
4305 * Pre-optimization is not supported and optimization is deferred e.g. in spi_optimize_message()
4306 * when using spi-mux. in spi_optimize_message()
4308 if (spi->controller->defer_optimize_message) in spi_optimize_message()
4311 ret = __spi_optimize_message(spi, msg); in spi_optimize_message()
4321 msg->pre_optimized = true; in spi_optimize_message()
4328 * spi_unoptimize_message - releases any resources allocated by spi_optimize_message()
4337 if (msg->spi->controller->defer_optimize_message) in spi_unoptimize_message()
4341 msg->pre_optimized = false; in spi_unoptimize_message()
4345 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
4347 struct spi_controller *ctlr = spi->controller; in __spi_async()
4351 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
4354 if (!ctlr->transfer) in __spi_async()
4355 return -ENOTSUPP; in __spi_async()
4357 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async); in __spi_async()
4358 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); in __spi_async()
4362 if (!ctlr->ptp_sts_supported) { in __spi_async()
4363 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_async()
4364 xfer->ptp_sts_word_pre = 0; in __spi_async()
4365 ptp_read_system_prets(xfer->ptp_sts); in __spi_async()
4369 return ctlr->transfer(spi, message); in __spi_async()
4378 * devm_spi_optimize_message - managed version of spi_optimize_message()
4379 * @dev: the device that manages @msg (usually @spi->dev)
4380 * @spi: the device that will be used for the message
4387 int devm_spi_optimize_message(struct device *dev, struct spi_device *spi, in devm_spi_optimize_message() argument
4392 ret = spi_optimize_message(spi, msg); in devm_spi_optimize_message()
4401 * spi_async - asynchronous SPI transfer
4402 * @spi: device with which data will be exchanged
4410 * Before that invocation, the value of message->status is undefined.
4411 * When the callback is issued, message->status holds either zero (to
4414 * deallocate the associated memory; it's no longer in use by any SPI
4431 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
4433 struct spi_controller *ctlr = spi->controller; in spi_async()
4437 ret = spi_maybe_optimize_message(spi, message); in spi_async()
4441 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async()
4443 if (ctlr->bus_lock_flag) in spi_async()
4444 ret = -EBUSY; in spi_async()
4446 ret = __spi_async(spi, message); in spi_async()
4448 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async()
4459 mutex_lock(&ctlr->io_mutex); in __spi_transfer_message_noqueue()
4461 was_busy = ctlr->busy; in __spi_transfer_message_noqueue()
4463 ctlr->cur_msg = msg; in __spi_transfer_message_noqueue()
4466 dev_err(&ctlr->dev, "noqueue transfer failed\n"); in __spi_transfer_message_noqueue()
4467 ctlr->cur_msg = NULL; in __spi_transfer_message_noqueue()
4468 ctlr->fallback = false; in __spi_transfer_message_noqueue()
4471 kfree(ctlr->dummy_rx); in __spi_transfer_message_noqueue()
4472 ctlr->dummy_rx = NULL; in __spi_transfer_message_noqueue()
4473 kfree(ctlr->dummy_tx); in __spi_transfer_message_noqueue()
4474 ctlr->dummy_tx = NULL; in __spi_transfer_message_noqueue()
4475 if (ctlr->unprepare_transfer_hardware && in __spi_transfer_message_noqueue()
4476 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_transfer_message_noqueue()
4477 dev_err(&ctlr->dev, in __spi_transfer_message_noqueue()
4482 mutex_unlock(&ctlr->io_mutex); in __spi_transfer_message_noqueue()
4485 /*-------------------------------------------------------------------------*/
4488 * Utility methods for SPI protocol drivers, layered on
4498 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
4503 struct spi_controller *ctlr = spi->controller; in __spi_sync()
4506 dev_warn_once(&spi->dev, "Attempted to sync while suspend\n"); in __spi_sync()
4507 return -ESHUTDOWN; in __spi_sync()
4510 status = spi_maybe_optimize_message(spi, message); in __spi_sync()
4514 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync); in __spi_sync()
4515 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); in __spi_sync()
4523 if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) { in __spi_sync()
4524 message->actual_length = 0; in __spi_sync()
4525 message->status = -EINPROGRESS; in __spi_sync()
4529 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4530 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4534 return message->status; in __spi_sync()
4543 message->complete = spi_complete; in __spi_sync()
4544 message->context = &done; in __spi_sync()
4546 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
4547 status = __spi_async(spi, message); in __spi_sync()
4548 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
4552 status = message->status; in __spi_sync()
4554 message->complete = NULL; in __spi_sync()
4555 message->context = NULL; in __spi_sync()
4561 * spi_sync - blocking/synchronous SPI data transfers
4562 * @spi: device with which data will be exchanged
4567 * is non-interruptible, and has no timeout. Low-overhead controller
4570 * Note that the SPI device's chip select is active during the message,
4572 * frequently-used devices may want to minimize costs of selecting a chip,
4581 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
4585 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
4586 ret = __spi_sync(spi, message); in spi_sync()
4587 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
4594 * spi_sync_locked - version of spi_sync with exclusive bus usage
4595 * @spi: device with which data will be exchanged
4600 * is non-interruptible, and has no timeout. Low-overhead controller
4604 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4609 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
4611 return __spi_sync(spi, message); in spi_sync_locked()
4616 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4617 * @ctlr: SPI bus master that should be locked for exclusive bus access
4621 * is non-interruptible, and has no timeout.
4624 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4626 * and spi_async_locked calls when the SPI bus lock is held.
4634 mutex_lock(&ctlr->bus_lock_mutex); in spi_bus_lock()
4636 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
4637 ctlr->bus_lock_flag = 1; in spi_bus_lock()
4638 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
4647 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4648 * @ctlr: SPI bus master that was locked for exclusive bus access
4652 * is non-interruptible, and has no timeout.
4654 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4661 ctlr->bus_lock_flag = 0; in spi_bus_unlock()
4663 mutex_unlock(&ctlr->bus_lock_mutex); in spi_bus_unlock()
4675 * spi_write_then_read - SPI synchronous write followed by read
4676 * @spi: device with which data will be exchanged
4677 * @txbuf: data to be written (need not be DMA-safe)
4679 * @rxbuf: buffer into which data will be read (need not be DMA-safe)
4689 * Performance-sensitive or bulk transfer code should instead use
4690 * spi_{async,sync}() calls with DMA-safe buffers.
4694 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4706 * Use preallocated DMA-safe buffer if we can. We can't avoid in spi_write_then_read()
4709 * using the pre-allocated buffer or the transfer is too large. in spi_write_then_read()
4715 return -ENOMEM; in spi_write_then_read()
4736 status = spi_sync(spi, &message); in spi_write_then_read()
4749 /*-------------------------------------------------------------------------*/
4760 /* The spi controllers are not using spi_bus, so we find it with another way */
4780 struct spi_device *spi; in of_spi_notify() local
4784 ctlr = of_find_spi_controller_by_node(rd->dn->parent); in of_spi_notify()
4788 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { in of_spi_notify()
4789 put_device(&ctlr->dev); in of_spi_notify()
4797 rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE; in of_spi_notify()
4798 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4799 put_device(&ctlr->dev); in of_spi_notify()
4801 if (IS_ERR(spi)) { in of_spi_notify()
4803 __func__, rd->dn); in of_spi_notify()
4804 of_node_clear_flag(rd->dn, OF_POPULATED); in of_spi_notify()
4805 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4811 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_spi_notify()
4815 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4816 if (spi == NULL) in of_spi_notify()
4820 spi_unregister_device(spi); in of_spi_notify()
4823 put_device(&spi->dev); in of_spi_notify()
4840 return ACPI_COMPANION(dev->parent) == data; in spi_acpi_controller_match()
4872 struct spi_device *spi; in acpi_spi_notify() local
4881 put_device(&ctlr->dev); in acpi_spi_notify()
4887 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4888 if (!spi) in acpi_spi_notify()
4891 spi_unregister_device(spi); in acpi_spi_notify()
4892 put_device(&spi->dev); in acpi_spi_notify()
4912 status = -ENOMEM; in spi_init()