Lines Matching +full:coexist +full:- +full:gpio +full:- +full:pin
1 // SPDX-License-Identifier: GPL-2.0-or-later
9 #include <linux/clk/clk-conf.h>
13 #include <linux/dma-mapping.h>
15 #include <linux/gpio/consumer.h>
35 #include <linux/spi/spi-mem.h>
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()
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()
78 ret = driver_set_override(dev, &spi->driver_override, buf, count); in driver_override_store()
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); \
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()
331 if ((xfer->tx_buf) && in spi_statistics_add_transfer_stats()
332 (xfer->tx_buf != ctlr->dummy_tx)) in spi_statistics_add_transfer_stats()
333 u64_stats_add(&stats->bytes_tx, xfer->len); in spi_statistics_add_transfer_stats()
334 if ((xfer->rx_buf) && in spi_statistics_add_transfer_stats()
335 (xfer->rx_buf != ctlr->dummy_rx)) in spi_statistics_add_transfer_stats()
336 u64_stats_add(&stats->bytes_rx, xfer->len); in spi_statistics_add_transfer_stats()
338 u64_stats_update_end(&stats->syncp); in spi_statistics_add_transfer_stats()
343 * modalias support makes "modprobe $MODALIAS" new-style hotplug work,
348 while (id->name[0]) { in spi_match_id()
349 if (!strcmp(name, id->name)) in spi_match_id()
358 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); in spi_get_device_id()
360 return spi_match_id(sdrv->id_table, sdev->modalias); in spi_get_device_id()
368 match = device_get_match_data(&sdev->dev); in spi_get_device_match_data()
372 return (const void *)spi_get_device_id(sdev)->driver_data; in spi_get_device_match_data()
382 if (spi->driver_override) in spi_match_device()
383 return strcmp(spi->driver_override, drv->name) == 0; in spi_match_device()
393 if (sdrv->id_table) in spi_match_device()
394 return !!spi_match_id(sdrv->id_table, spi->modalias); in spi_match_device()
396 return strcmp(spi->modalias, drv->name) == 0; in spi_match_device()
405 if (rc != -ENODEV) in spi_uevent()
408 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); in spi_uevent()
413 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_probe()
417 ret = of_clk_set_defaults(dev->of_node, false); in spi_probe()
421 if (dev->of_node) { in spi_probe()
422 spi->irq = of_irq_get(dev->of_node, 0); in spi_probe()
423 if (spi->irq == -EPROBE_DEFER) in spi_probe()
424 return -EPROBE_DEFER; in spi_probe()
425 if (spi->irq < 0) in spi_probe()
426 spi->irq = 0; in spi_probe()
429 if (has_acpi_companion(dev) && spi->irq < 0) { in spi_probe()
430 struct acpi_device *adev = to_acpi_device_node(dev->fwnode); in spi_probe()
432 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in spi_probe()
433 if (spi->irq == -EPROBE_DEFER) in spi_probe()
434 return -EPROBE_DEFER; in spi_probe()
435 if (spi->irq < 0) in spi_probe()
436 spi->irq = 0; in spi_probe()
443 if (sdrv->probe) { in spi_probe()
444 ret = sdrv->probe(spi); in spi_probe()
454 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_remove()
456 if (sdrv->remove) in spi_remove()
457 sdrv->remove(to_spi_device(dev)); in spi_remove()
464 if (dev->driver) { in spi_shutdown()
465 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_shutdown()
467 if (sdrv->shutdown) in spi_shutdown()
468 sdrv->shutdown(to_spi_device(dev)); in spi_shutdown()
484 * __spi_register_driver - register a SPI driver
493 sdrv->driver.owner = owner; in __spi_register_driver()
494 sdrv->driver.bus = &spi_bus_type; in __spi_register_driver()
501 if (sdrv->driver.of_match_table) { in __spi_register_driver()
504 for (of_id = sdrv->driver.of_match_table; of_id->compatible[0]; in __spi_register_driver()
509 of_name = strnchr(of_id->compatible, in __spi_register_driver()
510 sizeof(of_id->compatible), ','); in __spi_register_driver()
514 of_name = of_id->compatible; in __spi_register_driver()
516 if (sdrv->id_table) { in __spi_register_driver()
519 spi_id = spi_match_id(sdrv->id_table, of_name); in __spi_register_driver()
523 if (strcmp(sdrv->driver.name, of_name) == 0) in __spi_register_driver()
528 sdrv->driver.name, of_id->compatible); in __spi_register_driver()
532 return driver_register(&sdrv->driver); in __spi_register_driver()
536 /*-------------------------------------------------------------------------*/
540 * would make them board-specific. Similarly with SPI controller drivers.
541 * Device registration normally goes into like arch/.../mach.../board-YYY.c
561 * spi_alloc_device - Allocate a new SPI device
590 spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL); in spi_alloc_device()
591 if (!spi->pcpu_statistics) { in spi_alloc_device()
597 spi->master = spi->controller = ctlr; in spi_alloc_device()
598 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
599 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
600 spi->dev.release = spidev_release; in spi_alloc_device()
601 spi->mode = ctlr->buswidth_override_bits; in spi_alloc_device()
603 device_initialize(&spi->dev); in spi_alloc_device()
610 struct acpi_device *adev = ACPI_COMPANION(&spi->dev); in spi_dev_set_name()
613 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); in spi_dev_set_name()
617 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
626 if (spi->controller == new_spi->controller && in spi_dev_check()
628 return -EBUSY; in spi_dev_check()
634 if (spi->controller->cleanup) in spi_cleanup()
635 spi->controller->cleanup(spi); in spi_cleanup()
640 struct spi_controller *ctlr = spi->controller; in __spi_add_device()
641 struct device *dev = ctlr->dev.parent; in __spi_add_device()
645 if (spi_get_chipselect(spi, 0) >= ctlr->num_chipselect) { in __spi_add_device()
647 ctlr->num_chipselect); in __spi_add_device()
648 return -EINVAL; in __spi_add_device()
668 !device_is_registered(&ctlr->dev)) { in __spi_add_device()
669 return -ENODEV; in __spi_add_device()
672 if (ctlr->cs_gpiods) in __spi_add_device()
673 spi_set_csgpiod(spi, 0, ctlr->cs_gpiods[spi_get_chipselect(spi, 0)]); in __spi_add_device()
678 * using SPI_CS_HIGH can't coexist well otherwise... in __spi_add_device()
683 dev_name(&spi->dev), status); in __spi_add_device()
688 status = device_add(&spi->dev); in __spi_add_device()
691 dev_name(&spi->dev), status); in __spi_add_device()
694 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in __spi_add_device()
701 * spi_add_device - Add spi_device allocated with spi_alloc_device
711 struct spi_controller *ctlr = spi->controller; in spi_add_device()
714 mutex_lock(&ctlr->add_lock); in spi_add_device()
716 mutex_unlock(&ctlr->add_lock); in spi_add_device()
722 * spi_new_device - instantiate one new SPI device
728 * after board init creates the hard-wired devices. Some development
731 * driver could add devices (which it would learn about out-of-band).
742 * NOTE: caller did any chip->bus_num checks necessary. in spi_new_device()
745 * error-or-pointer (not NULL-or-pointer), troubleshootability in spi_new_device()
753 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); in spi_new_device()
755 spi_set_chipselect(proxy, 0, chip->chip_select); in spi_new_device()
756 proxy->max_speed_hz = chip->max_speed_hz; in spi_new_device()
757 proxy->mode = chip->mode; in spi_new_device()
758 proxy->irq = chip->irq; in spi_new_device()
759 strscpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); in spi_new_device()
760 proxy->dev.platform_data = (void *) chip->platform_data; in spi_new_device()
761 proxy->controller_data = chip->controller_data; in spi_new_device()
762 proxy->controller_state = NULL; in spi_new_device()
764 if (chip->swnode) { in spi_new_device()
765 status = device_add_software_node(&proxy->dev, chip->swnode); in spi_new_device()
767 dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n", in spi_new_device()
768 chip->modalias, status); in spi_new_device()
780 device_remove_software_node(&proxy->dev); in spi_new_device()
787 * spi_unregister_device - unregister a single SPI device
798 if (spi->dev.of_node) { in spi_unregister_device()
799 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
800 of_node_put(spi->dev.of_node); in spi_unregister_device()
802 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
803 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
804 device_remove_software_node(&spi->dev); in spi_unregister_device()
805 device_del(&spi->dev); in spi_unregister_device()
807 put_device(&spi->dev); in spi_unregister_device()
816 if (ctlr->bus_num != bi->bus_num) in spi_match_controller_to_boardinfo()
821 dev_err(ctlr->dev.parent, "can't create new device for %s\n", in spi_match_controller_to_boardinfo()
822 bi->modalias); in spi_match_controller_to_boardinfo()
826 * spi_register_board_info - register SPI devices for a given board
831 * Board-specific early init code calls this (probably during arch_initcall)
835 * not make Linux forget about these hard-wired devices.
837 * Other code can also call this, e.g. a particular add-on board might provide
842 * any embedded pointers (platform_data, etc), they're copied as-is.
856 return -ENOMEM; in spi_register_board_info()
861 memcpy(&bi->board_info, info, sizeof(*info)); in spi_register_board_info()
864 list_add_tail(&bi->list, &board_list); in spi_register_board_info()
867 &bi->board_info); in spi_register_board_info()
874 /*-------------------------------------------------------------------------*/
879 * spi_res_alloc - allocate a spi resource that is life-cycle managed
901 INIT_LIST_HEAD(&sres->entry); in spi_res_alloc()
902 sres->release = release; in spi_res_alloc()
904 return sres->data; in spi_res_alloc()
908 * spi_res_free - free an SPI resource
918 WARN_ON(!list_empty(&sres->entry)); in spi_res_free()
923 * spi_res_add - add a spi_res to the spi_message
931 WARN_ON(!list_empty(&sres->entry)); in spi_res_add()
932 list_add_tail(&sres->entry, &message->resources); in spi_res_add()
936 * spi_res_release - release all SPI resources for this message
944 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { in spi_res_release()
945 if (res->release) in spi_res_release()
946 res->release(ctlr, message, res->data); in spi_res_release()
948 list_del(&res->entry); in spi_res_release()
954 /*-------------------------------------------------------------------------*/
964 if (!force && ((enable && spi->controller->last_cs == spi_get_chipselect(spi, 0)) || in spi_set_cs()
965 (!enable && spi->controller->last_cs != spi_get_chipselect(spi, 0))) && in spi_set_cs()
966 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
971 spi->controller->last_cs = enable ? spi_get_chipselect(spi, 0) : -1; in spi_set_cs()
972 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
974 if ((spi_get_csgpiod(spi, 0) || !spi->controller->set_cs_timing) && !activate) in spi_set_cs()
975 spi_delay_exec(&spi->cs_hold, NULL); in spi_set_cs()
977 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
981 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
983 * Historically ACPI has no means of the GPIO polarity and in spi_set_cs()
984 * thus the SPISerialBus() resource defines it on the per-chip in spi_set_cs()
985 * basis. In order to avoid a chain of negations, the GPIO in spi_set_cs()
988 * the GPIO CS polarity must be defined Active High to avoid in spi_set_cs()
992 if (has_acpi_companion(&spi->dev)) in spi_set_cs()
995 /* Polarity handled by GPIO library */ in spi_set_cs()
998 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
999 if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) && in spi_set_cs()
1000 spi->controller->set_cs) in spi_set_cs()
1001 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1002 } else if (spi->controller->set_cs) { in spi_set_cs()
1003 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1006 if (spi_get_csgpiod(spi, 0) || !spi->controller->set_cs_timing) { in spi_set_cs()
1008 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
1010 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
1040 desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len); in spi_map_buf_attrs()
1043 return -EINVAL; in spi_map_buf_attrs()
1050 sg = &sgt->sgl[0]; in spi_map_buf_attrs()
1061 PAGE_SIZE - offset_in_page(buf))); in spi_map_buf_attrs()
1068 return -ENOMEM; in spi_map_buf_attrs()
1079 len -= min; in spi_map_buf_attrs()
1104 if (sgt->orig_nents) { in spi_unmap_buf_attrs()
1107 sgt->orig_nents = 0; in spi_unmap_buf_attrs()
1108 sgt->nents = 0; in spi_unmap_buf_attrs()
1124 if (!ctlr->can_dma) in __spi_map_msg()
1127 if (ctlr->dma_tx) in __spi_map_msg()
1128 tx_dev = ctlr->dma_tx->device->dev; in __spi_map_msg()
1129 else if (ctlr->dma_map_dev) in __spi_map_msg()
1130 tx_dev = ctlr->dma_map_dev; in __spi_map_msg()
1132 tx_dev = ctlr->dev.parent; in __spi_map_msg()
1134 if (ctlr->dma_rx) in __spi_map_msg()
1135 rx_dev = ctlr->dma_rx->device->dev; in __spi_map_msg()
1136 else if (ctlr->dma_map_dev) in __spi_map_msg()
1137 rx_dev = ctlr->dma_map_dev; in __spi_map_msg()
1139 rx_dev = ctlr->dev.parent; in __spi_map_msg()
1141 ret = -ENOMSG; in __spi_map_msg()
1142 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_map_msg()
1146 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
1149 if (xfer->tx_buf != NULL) { in __spi_map_msg()
1150 ret = spi_map_buf_attrs(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
1151 (void *)xfer->tx_buf, in __spi_map_msg()
1152 xfer->len, DMA_TO_DEVICE, in __spi_map_msg()
1158 if (xfer->rx_buf != NULL) { in __spi_map_msg()
1159 ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg, in __spi_map_msg()
1160 xfer->rx_buf, xfer->len, in __spi_map_msg()
1164 &xfer->tx_sg, DMA_TO_DEVICE, in __spi_map_msg()
1175 ctlr->cur_rx_dma_dev = rx_dev; in __spi_map_msg()
1176 ctlr->cur_tx_dma_dev = tx_dev; in __spi_map_msg()
1177 ctlr->cur_msg_mapped = true; in __spi_map_msg()
1184 struct device *rx_dev = ctlr->cur_rx_dma_dev; in __spi_unmap_msg()
1185 struct device *tx_dev = ctlr->cur_tx_dma_dev; in __spi_unmap_msg()
1188 if (!ctlr->cur_msg_mapped || !ctlr->can_dma) in __spi_unmap_msg()
1191 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_unmap_msg()
1195 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_unmap_msg()
1198 spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg, in __spi_unmap_msg()
1200 spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg, in __spi_unmap_msg()
1204 ctlr->cur_msg_mapped = false; in __spi_unmap_msg()
1212 struct device *rx_dev = ctlr->cur_rx_dma_dev; in spi_dma_sync_for_device()
1213 struct device *tx_dev = ctlr->cur_tx_dma_dev; in spi_dma_sync_for_device()
1215 if (!ctlr->cur_msg_mapped) in spi_dma_sync_for_device()
1218 if (xfer->tx_sg.orig_nents) in spi_dma_sync_for_device()
1219 dma_sync_sgtable_for_device(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in spi_dma_sync_for_device()
1220 if (xfer->rx_sg.orig_nents) in spi_dma_sync_for_device()
1221 dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in spi_dma_sync_for_device()
1227 struct device *rx_dev = ctlr->cur_rx_dma_dev; in spi_dma_sync_for_cpu()
1228 struct device *tx_dev = ctlr->cur_tx_dma_dev; in spi_dma_sync_for_cpu()
1230 if (!ctlr->cur_msg_mapped) in spi_dma_sync_for_cpu()
1233 if (xfer->rx_sg.orig_nents) in spi_dma_sync_for_cpu()
1234 dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in spi_dma_sync_for_cpu()
1235 if (xfer->tx_sg.orig_nents) in spi_dma_sync_for_cpu()
1236 dma_sync_sgtable_for_cpu(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in spi_dma_sync_for_cpu()
1267 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_unmap_msg()
1272 if (xfer->tx_buf == ctlr->dummy_tx) in spi_unmap_msg()
1273 xfer->tx_buf = NULL; in spi_unmap_msg()
1274 if (xfer->rx_buf == ctlr->dummy_rx) in spi_unmap_msg()
1275 xfer->rx_buf = NULL; in spi_unmap_msg()
1287 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) in spi_map_msg()
1288 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1292 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_map_msg()
1293 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && in spi_map_msg()
1294 !xfer->tx_buf) in spi_map_msg()
1295 max_tx = max(xfer->len, max_tx); in spi_map_msg()
1296 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && in spi_map_msg()
1297 !xfer->rx_buf) in spi_map_msg()
1298 max_rx = max(xfer->len, max_rx); in spi_map_msg()
1302 tmp = krealloc(ctlr->dummy_tx, max_tx, in spi_map_msg()
1305 return -ENOMEM; in spi_map_msg()
1306 ctlr->dummy_tx = tmp; in spi_map_msg()
1310 tmp = krealloc(ctlr->dummy_rx, max_rx, in spi_map_msg()
1313 return -ENOMEM; in spi_map_msg()
1314 ctlr->dummy_rx = tmp; in spi_map_msg()
1318 list_for_each_entry(xfer, &msg->transfers, in spi_map_msg()
1320 if (!xfer->len) in spi_map_msg()
1322 if (!xfer->tx_buf) in spi_map_msg()
1323 xfer->tx_buf = ctlr->dummy_tx; in spi_map_msg()
1324 if (!xfer->rx_buf) in spi_map_msg()
1325 xfer->rx_buf = ctlr->dummy_rx; in spi_map_msg()
1337 struct spi_statistics __percpu *statm = ctlr->pcpu_statistics; in spi_transfer_wait()
1338 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_wait()
1339 u32 speed_hz = xfer->speed_hz; in spi_transfer_wait()
1343 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { in spi_transfer_wait()
1344 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1345 return -EINTR; in spi_transfer_wait()
1357 ms = 8LL * MSEC_PER_SEC * xfer->len; in spi_transfer_wait()
1368 ms = wait_for_completion_timeout(&ctlr->xfer_completion, in spi_transfer_wait()
1374 dev_err(&msg->spi->dev, in spi_transfer_wait()
1376 return -ETIMEDOUT; in spi_transfer_wait()
1401 u32 delay = _delay->value; in spi_delay_to_ns()
1402 u32 unit = _delay->unit; in spi_delay_to_ns()
1418 return -EINVAL; in spi_delay_to_ns()
1423 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; in spi_delay_to_ns()
1425 return -EINVAL; in spi_delay_to_ns()
1431 return -EINVAL; in spi_delay_to_ns()
1445 return -EINVAL; in spi_delay_exec()
1461 u32 delay = xfer->cs_change_delay.value; in _spi_transfer_cs_change_delay()
1462 u32 unit = xfer->cs_change_delay.unit; in _spi_transfer_cs_change_delay()
1465 /* Return early on "fast" mode - for everything but USECS */ in _spi_transfer_cs_change_delay()
1472 ret = spi_delay_exec(&xfer->cs_change_delay, xfer); in _spi_transfer_cs_change_delay()
1474 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1489 * spi_transfer_one_message - Default implementation of transfer_one_message()
1501 struct spi_statistics __percpu *statm = ctlr->pcpu_statistics; in spi_transfer_one_message()
1502 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_one_message()
1504 xfer = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list); in spi_transfer_one_message()
1505 spi_set_cs(msg->spi, !xfer->cs_off, false); in spi_transfer_one_message()
1510 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_transfer_one_message()
1516 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1517 xfer->ptp_sts_word_pre = 0; in spi_transfer_one_message()
1518 ptp_read_system_prets(xfer->ptp_sts); in spi_transfer_one_message()
1521 if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { in spi_transfer_one_message()
1522 reinit_completion(&ctlr->xfer_completion); in spi_transfer_one_message()
1526 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1530 if (ctlr->cur_msg_mapped && in spi_transfer_one_message()
1531 (xfer->error & SPI_TRANS_FAIL_NO_START)) { in spi_transfer_one_message()
1533 ctlr->fallback = true; in spi_transfer_one_message()
1534 xfer->error &= ~SPI_TRANS_FAIL_NO_START; in spi_transfer_one_message()
1542 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1550 msg->status = ret; in spi_transfer_one_message()
1555 if (xfer->len) in spi_transfer_one_message()
1556 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1558 xfer->len); in spi_transfer_one_message()
1561 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1562 ptp_read_system_postts(xfer->ptp_sts); in spi_transfer_one_message()
1563 xfer->ptp_sts_word_post = xfer->len; in spi_transfer_one_message()
1568 if (msg->status != -EINPROGRESS) in spi_transfer_one_message()
1573 if (xfer->cs_change) { in spi_transfer_one_message()
1574 if (list_is_last(&xfer->transfer_list, in spi_transfer_one_message()
1575 &msg->transfers)) { in spi_transfer_one_message()
1578 if (!xfer->cs_off) in spi_transfer_one_message()
1579 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1581 if (!list_next_entry(xfer, transfer_list)->cs_off) in spi_transfer_one_message()
1582 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1584 } else if (!list_is_last(&xfer->transfer_list, &msg->transfers) && in spi_transfer_one_message()
1585 xfer->cs_off != list_next_entry(xfer, transfer_list)->cs_off) { in spi_transfer_one_message()
1586 spi_set_cs(msg->spi, xfer->cs_off, false); in spi_transfer_one_message()
1589 msg->actual_length += xfer->len; in spi_transfer_one_message()
1594 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1596 if (msg->status == -EINPROGRESS) in spi_transfer_one_message()
1597 msg->status = ret; in spi_transfer_one_message()
1599 if (msg->status && ctlr->handle_err) in spi_transfer_one_message()
1600 ctlr->handle_err(ctlr, msg); in spi_transfer_one_message()
1608 * spi_finalize_current_transfer - report completion of a transfer
1617 complete(&ctlr->xfer_completion); in spi_finalize_current_transfer()
1623 if (ctlr->auto_runtime_pm) { in spi_idle_runtime_pm()
1624 pm_runtime_mark_last_busy(ctlr->dev.parent); in spi_idle_runtime_pm()
1625 pm_runtime_put_autosuspend(ctlr->dev.parent); in spi_idle_runtime_pm()
1635 if (!was_busy && ctlr->auto_runtime_pm) { in __spi_pump_transfer_message()
1636 ret = pm_runtime_get_sync(ctlr->dev.parent); in __spi_pump_transfer_message()
1638 pm_runtime_put_noidle(ctlr->dev.parent); in __spi_pump_transfer_message()
1639 dev_err(&ctlr->dev, "Failed to power device: %d\n", in __spi_pump_transfer_message()
1642 msg->status = ret; in __spi_pump_transfer_message()
1652 if (!was_busy && ctlr->prepare_transfer_hardware) { in __spi_pump_transfer_message()
1653 ret = ctlr->prepare_transfer_hardware(ctlr); in __spi_pump_transfer_message()
1655 dev_err(&ctlr->dev, in __spi_pump_transfer_message()
1659 if (ctlr->auto_runtime_pm) in __spi_pump_transfer_message()
1660 pm_runtime_put(ctlr->dev.parent); in __spi_pump_transfer_message()
1662 msg->status = ret; in __spi_pump_transfer_message()
1672 spi_max_transfer_size(msg->spi), in __spi_pump_transfer_message()
1675 msg->status = ret; in __spi_pump_transfer_message()
1680 if (ctlr->prepare_message) { in __spi_pump_transfer_message()
1681 ret = ctlr->prepare_message(ctlr, msg); in __spi_pump_transfer_message()
1683 dev_err(&ctlr->dev, "failed to prepare message: %d\n", in __spi_pump_transfer_message()
1685 msg->status = ret; in __spi_pump_transfer_message()
1689 msg->prepared = true; in __spi_pump_transfer_message()
1694 msg->status = ret; in __spi_pump_transfer_message()
1699 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in __spi_pump_transfer_message()
1700 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_pump_transfer_message()
1701 xfer->ptp_sts_word_pre = 0; in __spi_pump_transfer_message()
1702 ptp_read_system_prets(xfer->ptp_sts); in __spi_pump_transfer_message()
1712 * ctlr->cur_msg. in __spi_pump_transfer_message()
1719 WRITE_ONCE(ctlr->cur_msg_incomplete, true); in __spi_pump_transfer_message()
1720 WRITE_ONCE(ctlr->cur_msg_need_completion, false); in __spi_pump_transfer_message()
1721 reinit_completion(&ctlr->cur_msg_completion); in __spi_pump_transfer_message()
1724 ret = ctlr->transfer_one_message(ctlr, msg); in __spi_pump_transfer_message()
1726 dev_err(&ctlr->dev, in __spi_pump_transfer_message()
1731 WRITE_ONCE(ctlr->cur_msg_need_completion, true); in __spi_pump_transfer_message()
1733 if (READ_ONCE(ctlr->cur_msg_incomplete)) in __spi_pump_transfer_message()
1734 wait_for_completion(&ctlr->cur_msg_completion); in __spi_pump_transfer_message()
1740 * __spi_pump_messages - function which processes SPI message queue
1760 mutex_lock(&ctlr->io_mutex); in __spi_pump_messages()
1763 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1766 if (ctlr->cur_msg) in __spi_pump_messages()
1770 if (list_empty(&ctlr->queue) || !ctlr->running) { in __spi_pump_messages()
1771 if (!ctlr->busy) in __spi_pump_messages()
1774 /* Defer any non-atomic teardown to the thread */ in __spi_pump_messages()
1776 if (!ctlr->dummy_rx && !ctlr->dummy_tx && in __spi_pump_messages()
1777 !ctlr->unprepare_transfer_hardware) { in __spi_pump_messages()
1779 ctlr->busy = false; in __spi_pump_messages()
1780 ctlr->queue_empty = true; in __spi_pump_messages()
1783 kthread_queue_work(ctlr->kworker, in __spi_pump_messages()
1784 &ctlr->pump_messages); in __spi_pump_messages()
1789 ctlr->busy = false; in __spi_pump_messages()
1790 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1792 kfree(ctlr->dummy_rx); in __spi_pump_messages()
1793 ctlr->dummy_rx = NULL; in __spi_pump_messages()
1794 kfree(ctlr->dummy_tx); in __spi_pump_messages()
1795 ctlr->dummy_tx = NULL; in __spi_pump_messages()
1796 if (ctlr->unprepare_transfer_hardware && in __spi_pump_messages()
1797 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_pump_messages()
1798 dev_err(&ctlr->dev, in __spi_pump_messages()
1803 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1804 ctlr->queue_empty = true; in __spi_pump_messages()
1809 msg = list_first_entry(&ctlr->queue, struct spi_message, queue); in __spi_pump_messages()
1810 ctlr->cur_msg = msg; in __spi_pump_messages()
1812 list_del_init(&msg->queue); in __spi_pump_messages()
1813 if (ctlr->busy) in __spi_pump_messages()
1816 ctlr->busy = true; in __spi_pump_messages()
1817 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1820 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_pump_messages()
1822 ctlr->cur_msg = NULL; in __spi_pump_messages()
1823 ctlr->fallback = false; in __spi_pump_messages()
1825 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1833 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1834 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1838 * spi_pump_messages - kthread work function which processes spi message queue
1850 * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp
1873 if (!xfer->ptp_sts) in spi_take_timestamp_pre()
1876 if (xfer->timestamped) in spi_take_timestamp_pre()
1879 if (progress > xfer->ptp_sts_word_pre) in spi_take_timestamp_pre()
1883 xfer->ptp_sts_word_pre = progress; in spi_take_timestamp_pre()
1886 local_irq_save(ctlr->irq_flags); in spi_take_timestamp_pre()
1890 ptp_read_system_prets(xfer->ptp_sts); in spi_take_timestamp_pre()
1895 * spi_take_timestamp_post - helper to collect the end of the TX timestamp
1899 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1910 if (!xfer->ptp_sts) in spi_take_timestamp_post()
1913 if (xfer->timestamped) in spi_take_timestamp_post()
1916 if (progress < xfer->ptp_sts_word_post) in spi_take_timestamp_post()
1919 ptp_read_system_postts(xfer->ptp_sts); in spi_take_timestamp_post()
1922 local_irq_restore(ctlr->irq_flags); in spi_take_timestamp_post()
1927 xfer->ptp_sts_word_post = progress; in spi_take_timestamp_post()
1929 xfer->timestamped = 1; in spi_take_timestamp_post()
1934 * spi_set_thread_rt - set the controller to pump at realtime priority
1938 * (by setting the ->rt value before calling spi_register_controller()) or
1950 dev_info(&ctlr->dev, in spi_set_thread_rt()
1952 sched_set_fifo(ctlr->kworker->task); in spi_set_thread_rt()
1957 ctlr->running = false; in spi_init_queue()
1958 ctlr->busy = false; in spi_init_queue()
1959 ctlr->queue_empty = true; in spi_init_queue()
1961 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); in spi_init_queue()
1962 if (IS_ERR(ctlr->kworker)) { in spi_init_queue()
1963 dev_err(&ctlr->dev, "failed to create message pump kworker\n"); in spi_init_queue()
1964 return PTR_ERR(ctlr->kworker); in spi_init_queue()
1967 kthread_init_work(&ctlr->pump_messages, spi_pump_messages); in spi_init_queue()
1976 if (ctlr->rt) in spi_init_queue()
1983 * spi_get_next_queued_message() - called by driver to check for queued
1998 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1999 next = list_first_entry_or_null(&ctlr->queue, struct spi_message, in spi_get_next_queued_message()
2001 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
2008 * spi_finalize_current_message() - the current message is complete
2020 mesg = ctlr->cur_msg; in spi_finalize_current_message()
2022 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in spi_finalize_current_message()
2023 list_for_each_entry(xfer, &mesg->transfers, transfer_list) { in spi_finalize_current_message()
2024 ptp_read_system_postts(xfer->ptp_sts); in spi_finalize_current_message()
2025 xfer->ptp_sts_word_post = xfer->len; in spi_finalize_current_message()
2029 if (unlikely(ctlr->ptp_sts_supported)) in spi_finalize_current_message()
2030 list_for_each_entry(xfer, &mesg->transfers, transfer_list) in spi_finalize_current_message()
2031 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); in spi_finalize_current_message()
2044 if (mesg->prepared && ctlr->unprepare_message) { in spi_finalize_current_message()
2045 ret = ctlr->unprepare_message(ctlr, mesg); in spi_finalize_current_message()
2047 dev_err(&ctlr->dev, "failed to unprepare message: %d\n", in spi_finalize_current_message()
2052 mesg->prepared = false; in spi_finalize_current_message()
2054 WRITE_ONCE(ctlr->cur_msg_incomplete, false); in spi_finalize_current_message()
2056 if (READ_ONCE(ctlr->cur_msg_need_completion)) in spi_finalize_current_message()
2057 complete(&ctlr->cur_msg_completion); in spi_finalize_current_message()
2061 mesg->state = NULL; in spi_finalize_current_message()
2062 if (mesg->complete) in spi_finalize_current_message()
2063 mesg->complete(mesg->context); in spi_finalize_current_message()
2071 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_start_queue()
2073 if (ctlr->running || ctlr->busy) { in spi_start_queue()
2074 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
2075 return -EBUSY; in spi_start_queue()
2078 ctlr->running = true; in spi_start_queue()
2079 ctlr->cur_msg = NULL; in spi_start_queue()
2080 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
2082 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_start_queue()
2093 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
2097 * A wait_queue on the ctlr->busy could be used, but then the common in spi_stop_queue()
2101 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { in spi_stop_queue()
2102 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
2104 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
2107 if (!list_empty(&ctlr->queue) || ctlr->busy) in spi_stop_queue()
2108 ret = -EBUSY; in spi_stop_queue()
2110 ctlr->running = false; in spi_stop_queue()
2112 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
2115 dev_warn(&ctlr->dev, "could not stop message queue\n"); in spi_stop_queue()
2134 dev_err(&ctlr->dev, "problem destroying queue\n"); in spi_destroy_queue()
2138 kthread_destroy_worker(ctlr->kworker); in spi_destroy_queue()
2147 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
2150 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_queued_transfer()
2152 if (!ctlr->running) { in __spi_queued_transfer()
2153 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
2154 return -ESHUTDOWN; in __spi_queued_transfer()
2156 msg->actual_length = 0; in __spi_queued_transfer()
2157 msg->status = -EINPROGRESS; in __spi_queued_transfer()
2159 list_add_tail(&msg->queue, &ctlr->queue); in __spi_queued_transfer()
2160 ctlr->queue_empty = false; in __spi_queued_transfer()
2161 if (!ctlr->busy && need_pump) in __spi_queued_transfer()
2162 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_queued_transfer()
2164 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
2169 * spi_queued_transfer - transfer function for queued transfers
2184 ctlr->transfer = spi_queued_transfer; in spi_controller_initialize_queue()
2185 if (!ctlr->transfer_one_message) in spi_controller_initialize_queue()
2186 ctlr->transfer_one_message = spi_transfer_one_message; in spi_controller_initialize_queue()
2191 dev_err(&ctlr->dev, "problem initializing queue\n"); in spi_controller_initialize_queue()
2194 ctlr->queued = true; in spi_controller_initialize_queue()
2197 dev_err(&ctlr->dev, "problem starting queue\n"); in spi_controller_initialize_queue()
2210 * spi_flush_queue - Send all pending messages in the queue from the callers'
2215 * sent before doing something. Is used by the spi-mem code to make sure SPI
2217 * before the spi-mem operation.
2221 if (ctlr->transfer == spi_queued_transfer) in spi_flush_queue()
2225 /*-------------------------------------------------------------------------*/
2235 delay->value = DIV_ROUND_UP(value, 1000); in of_spi_parse_dt_cs_delay()
2236 delay->unit = SPI_DELAY_UNIT_USECS; in of_spi_parse_dt_cs_delay()
2238 delay->value = value; in of_spi_parse_dt_cs_delay()
2239 delay->unit = SPI_DELAY_UNIT_NSECS; in of_spi_parse_dt_cs_delay()
2251 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
2252 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
2253 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
2254 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
2255 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
2256 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
2257 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
2258 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
2259 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
2260 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
2263 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
2266 spi->mode |= SPI_NO_TX; in of_spi_parse_dt()
2271 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
2274 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
2277 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
2280 dev_warn(&ctlr->dev, in of_spi_parse_dt()
2281 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
2287 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
2290 spi->mode |= SPI_NO_RX; in of_spi_parse_dt()
2295 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
2298 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
2301 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
2304 dev_warn(&ctlr->dev, in of_spi_parse_dt()
2305 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
2313 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", in of_spi_parse_dt()
2315 return -EINVAL; in of_spi_parse_dt()
2323 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", in of_spi_parse_dt()
2330 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2331 spi->max_speed_hz = value; in of_spi_parse_dt()
2334 of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns"); in of_spi_parse_dt()
2335 of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns"); in of_spi_parse_dt()
2336 of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns"); in of_spi_parse_dt()
2350 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); in of_register_spi_device()
2351 rc = -ENOMEM; in of_register_spi_device()
2356 rc = of_alias_from_compatible(nc, spi->modalias, in of_register_spi_device()
2357 sizeof(spi->modalias)); in of_register_spi_device()
2359 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); in of_register_spi_device()
2370 device_set_node(&spi->dev, of_fwnode_handle(nc)); in of_register_spi_device()
2375 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); in of_register_spi_device()
2389 * of_register_spi_devices() - Register child devices onto the SPI bus
2400 for_each_available_child_of_node(ctlr->dev.of_node, nc) { in of_register_spi_devices()
2405 dev_warn(&ctlr->dev, in of_register_spi_devices()
2416 * spi_new_ancillary_device() - Register ancillary SPI device
2420 * Register an ancillary SPI device; for example some chips have a chip-select
2430 struct spi_controller *ctlr = spi->controller; in spi_new_ancillary_device()
2437 rc = -ENOMEM; in spi_new_ancillary_device()
2441 strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias)); in spi_new_ancillary_device()
2443 /* Use provided chip-select for ancillary device */ in spi_new_ancillary_device()
2447 ancillary->max_speed_hz = spi->max_speed_hz; in spi_new_ancillary_device()
2448 ancillary->mode = spi->mode; in spi_new_ancillary_device()
2450 WARN_ON(!mutex_is_locked(&ctlr->add_lock)); in spi_new_ancillary_device()
2455 dev_err(&spi->dev, "failed to register ancillary device\n"); in spi_new_ancillary_device()
2484 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) in acpi_spi_count()
2487 sb = &ares->data.spi_serial_bus; in acpi_spi_count()
2488 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI) in acpi_spi_count()
2497 * acpi_spi_count_resources - Count the number of SpiSerialBus resources
2500 * Return: the number of SpiSerialBus resources in the ACPI-device's
2501 * resource-list; or a negative error code.
2528 && obj->buffer.length >= 4) in acpi_spi_parse_apple_properties()
2529 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2532 && obj->buffer.length == 8) in acpi_spi_parse_apple_properties()
2533 lookup->bits_per_word = *(u64 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2536 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2537 lookup->mode |= SPI_LSB_FIRST; in acpi_spi_parse_apple_properties()
2540 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2541 lookup->mode |= SPI_CPOL; in acpi_spi_parse_apple_properties()
2544 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2545 lookup->mode |= SPI_CPHA; in acpi_spi_parse_apple_properties()
2553 struct spi_controller *ctlr = lookup->ctlr; in acpi_spi_add_resource()
2555 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { in acpi_spi_add_resource()
2560 sb = &ares->data.spi_serial_bus; in acpi_spi_add_resource()
2561 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { in acpi_spi_add_resource()
2563 if (lookup->index != -1 && lookup->n++ != lookup->index) in acpi_spi_add_resource()
2567 sb->resource_source.string_ptr, in acpi_spi_add_resource()
2571 return -ENODEV; in acpi_spi_add_resource()
2574 if (ACPI_HANDLE(ctlr->dev.parent) != parent_handle) in acpi_spi_add_resource()
2575 return -ENODEV; in acpi_spi_add_resource()
2581 return -ENODEV; in acpi_spi_add_resource()
2585 return -EPROBE_DEFER; in acpi_spi_add_resource()
2587 lookup->ctlr = ctlr; in acpi_spi_add_resource()
2594 * 0 .. max - 1 so we need to ask the driver to in acpi_spi_add_resource()
2597 if (ctlr->fw_translate_cs) { in acpi_spi_add_resource()
2598 int cs = ctlr->fw_translate_cs(ctlr, in acpi_spi_add_resource()
2599 sb->device_selection); in acpi_spi_add_resource()
2602 lookup->chip_select = cs; in acpi_spi_add_resource()
2604 lookup->chip_select = sb->device_selection; in acpi_spi_add_resource()
2607 lookup->max_speed_hz = sb->connection_speed; in acpi_spi_add_resource()
2608 lookup->bits_per_word = sb->data_bit_length; in acpi_spi_add_resource()
2610 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) in acpi_spi_add_resource()
2611 lookup->mode |= SPI_CPHA; in acpi_spi_add_resource()
2612 if (sb->clock_polarity == ACPI_SPI_START_HIGH) in acpi_spi_add_resource()
2613 lookup->mode |= SPI_CPOL; in acpi_spi_add_resource()
2614 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) in acpi_spi_add_resource()
2615 lookup->mode |= SPI_CS_HIGH; in acpi_spi_add_resource()
2617 } else if (lookup->irq < 0) { in acpi_spi_add_resource()
2621 lookup->irq = r.start; in acpi_spi_add_resource()
2629 * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2639 * If index is set to -1, index is not used.
2640 * Note: If index is -1, ctlr must be set.
2654 if (!ctlr && index == -1) in acpi_spi_device_alloc()
2655 return ERR_PTR(-EINVAL); in acpi_spi_device_alloc()
2658 lookup.irq = -1; in acpi_spi_device_alloc()
2672 ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && in acpi_spi_device_alloc()
2673 ACPI_HANDLE(lookup.ctlr->dev.parent) == parent_handle) { in acpi_spi_device_alloc()
2679 return ERR_PTR(-ENODEV); in acpi_spi_device_alloc()
2683 dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_spi_device_alloc()
2684 dev_name(&adev->dev)); in acpi_spi_device_alloc()
2685 return ERR_PTR(-ENOMEM); in acpi_spi_device_alloc()
2688 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_spi_device_alloc()
2689 spi->max_speed_hz = lookup.max_speed_hz; in acpi_spi_device_alloc()
2690 spi->mode |= lookup.mode; in acpi_spi_device_alloc()
2691 spi->irq = lookup.irq; in acpi_spi_device_alloc()
2692 spi->bits_per_word = lookup.bits_per_word; in acpi_spi_device_alloc()
2704 if (acpi_bus_get_status(adev) || !adev->status.present || in acpi_register_spi_device()
2708 spi = acpi_spi_device_alloc(ctlr, adev, -1); in acpi_register_spi_device()
2710 if (PTR_ERR(spi) == -ENOMEM) in acpi_register_spi_device()
2716 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2717 sizeof(spi->modalias)); in acpi_register_spi_device()
2721 adev->power.flags.ignore_parent = true; in acpi_register_spi_device()
2723 adev->power.flags.ignore_parent = false; in acpi_register_spi_device()
2724 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2725 dev_name(&adev->dev)); in acpi_register_spi_device()
2751 handle = ACPI_HANDLE(ctlr->dev.parent); in acpi_register_spi_devices()
2759 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2781 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2787 struct spi_controller *ctlr = spi->controller; in spi_slave_abort()
2789 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) in spi_slave_abort()
2790 return ctlr->slave_abort(ctlr); in spi_slave_abort()
2792 return -ENOTSUPP; in spi_slave_abort()
2798 struct spi_controller *ctlr = spi->controller; in spi_target_abort()
2800 if (spi_controller_is_target(ctlr) && ctlr->target_abort) in spi_target_abort()
2801 return ctlr->target_abort(ctlr); in spi_target_abort()
2803 return -ENOTSUPP; in spi_target_abort()
2814 child = device_find_any_child(&ctlr->dev); in slave_show()
2815 return sysfs_emit(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL); in slave_show()
2830 return -EINVAL; in slave_store()
2832 child = device_find_any_child(&ctlr->dev); in slave_store()
2843 return -ENOMEM; in slave_store()
2845 strscpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2884 * __spi_alloc_controller - allocate an SPI master or slave controller
2886 * @size: how much zeroed driver-private data to allocate; the pointer to this
2921 device_initialize(&ctlr->dev); in __spi_alloc_controller()
2922 INIT_LIST_HEAD(&ctlr->queue); in __spi_alloc_controller()
2923 spin_lock_init(&ctlr->queue_lock); in __spi_alloc_controller()
2924 spin_lock_init(&ctlr->bus_lock_spinlock); in __spi_alloc_controller()
2925 mutex_init(&ctlr->bus_lock_mutex); in __spi_alloc_controller()
2926 mutex_init(&ctlr->io_mutex); in __spi_alloc_controller()
2927 mutex_init(&ctlr->add_lock); in __spi_alloc_controller()
2928 ctlr->bus_num = -1; in __spi_alloc_controller()
2929 ctlr->num_chipselect = 1; in __spi_alloc_controller()
2930 ctlr->slave = slave; in __spi_alloc_controller()
2932 ctlr->dev.class = &spi_slave_class; in __spi_alloc_controller()
2934 ctlr->dev.class = &spi_master_class; in __spi_alloc_controller()
2935 ctlr->dev.parent = dev; in __spi_alloc_controller()
2936 pm_suspend_ignore_children(&ctlr->dev, true); in __spi_alloc_controller()
2949 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
2951 * @size: how much zeroed driver-private data to allocate
2976 ctlr->devm_allocated = true; in __devm_spi_alloc_controller()
2988 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2989 * @ctlr: The SPI master to grab GPIO descriptors for
2995 struct device *dev = &ctlr->dev; in spi_get_gpio_descs()
3002 if (nb == -ENOENT) in spi_get_gpio_descs()
3007 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in spi_get_gpio_descs()
3009 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), in spi_get_gpio_descs()
3012 return -ENOMEM; in spi_get_gpio_descs()
3013 ctlr->cs_gpiods = cs; in spi_get_gpio_descs()
3030 * If we find a CS GPIO, name it after the device and in spi_get_gpio_descs()
3038 return -ENOMEM; in spi_get_gpio_descs()
3044 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { in spi_get_gpio_descs()
3046 return -EINVAL; in spi_get_gpio_descs()
3051 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; in spi_get_gpio_descs()
3053 if ((ctlr->flags & SPI_CONTROLLER_GPIO_SS) && num_cs_gpios && in spi_get_gpio_descs()
3054 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { in spi_get_gpio_descs()
3056 return -EINVAL; in spi_get_gpio_descs()
3065 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
3068 * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least in spi_controller_check_ops()
3069 * one of the ->transfer_xxx() method be implemented. in spi_controller_check_ops()
3071 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { in spi_controller_check_ops()
3072 if (!ctlr->transfer && !ctlr->transfer_one && in spi_controller_check_ops()
3073 !ctlr->transfer_one_message) { in spi_controller_check_ops()
3074 return -EINVAL; in spi_controller_check_ops()
3090 return id == -ENOSPC ? -EBUSY : id; in spi_controller_id_alloc()
3091 ctlr->bus_num = id; in spi_controller_id_alloc()
3096 * spi_register_controller - register SPI master or slave controller
3101 * SPI controllers connect to their drivers using some non-SPI bus,
3106 * and board-specific addressing for SPI devices combines those numbers
3120 struct device *dev = ctlr->dev.parent; in spi_register_controller()
3126 return -ENODEV; in spi_register_controller()
3136 if (ctlr->bus_num < 0) in spi_register_controller()
3137 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
3138 if (ctlr->bus_num >= 0) { in spi_register_controller()
3139 /* Devices with a fixed bus num must check-in with the num */ in spi_register_controller()
3140 status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1); in spi_register_controller()
3144 if (ctlr->bus_num < 0) { in spi_register_controller()
3155 ctlr->bus_lock_flag = 0; in spi_register_controller()
3156 init_completion(&ctlr->xfer_completion); in spi_register_controller()
3157 init_completion(&ctlr->cur_msg_completion); in spi_register_controller()
3158 if (!ctlr->max_dma_len) in spi_register_controller()
3159 ctlr->max_dma_len = INT_MAX; in spi_register_controller()
3165 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
3167 if (!spi_controller_is_slave(ctlr) && ctlr->use_gpio_descriptors) { in spi_register_controller()
3172 * A controller using GPIO descriptors always in spi_register_controller()
3175 ctlr->mode_bits |= SPI_CS_HIGH; in spi_register_controller()
3179 * Even if it's just one always-selected device, there must in spi_register_controller()
3182 if (!ctlr->num_chipselect) { in spi_register_controller()
3183 status = -EINVAL; in spi_register_controller()
3187 /* Setting last_cs to -1 means no chip selected */ in spi_register_controller()
3188 ctlr->last_cs = -1; in spi_register_controller()
3190 status = device_add(&ctlr->dev); in spi_register_controller()
3195 dev_name(&ctlr->dev)); in spi_register_controller()
3199 * need the queueing logic if the driver is only supporting high-level in spi_register_controller()
3202 if (ctlr->transfer) { in spi_register_controller()
3204 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { in spi_register_controller()
3207 device_del(&ctlr->dev); in spi_register_controller()
3212 ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev); in spi_register_controller()
3213 if (!ctlr->pcpu_statistics) { in spi_register_controller()
3214 dev_err(dev, "Error allocating per-cpu statistics\n"); in spi_register_controller()
3215 status = -ENOMEM; in spi_register_controller()
3220 list_add_tail(&ctlr->list, &spi_controller_list); in spi_register_controller()
3222 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); in spi_register_controller()
3234 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
3246 * devm_spi_register_controller - register managed SPI master or slave
3266 return -ENOMEM; in devm_spi_register_controller()
3287 * spi_unregister_controller - unregister SPI master or slave controller
3301 int id = ctlr->bus_num; in spi_unregister_controller()
3305 mutex_lock(&ctlr->add_lock); in spi_unregister_controller()
3307 device_for_each_child(&ctlr->dev, NULL, __unregister); in spi_unregister_controller()
3313 if (ctlr->queued) { in spi_unregister_controller()
3315 dev_err(&ctlr->dev, "queue remove failed\n"); in spi_unregister_controller()
3318 list_del(&ctlr->list); in spi_unregister_controller()
3321 device_del(&ctlr->dev); in spi_unregister_controller()
3330 mutex_unlock(&ctlr->add_lock); in spi_unregister_controller()
3336 if (!ctlr->devm_allocated) in spi_unregister_controller()
3337 put_device(&ctlr->dev); in spi_unregister_controller()
3343 return ctlr->flags & SPI_CONTROLLER_SUSPENDED ? -ESHUTDOWN : 0; in __spi_check_suspended()
3348 mutex_lock(&ctlr->bus_lock_mutex); in __spi_mark_suspended()
3349 ctlr->flags |= SPI_CONTROLLER_SUSPENDED; in __spi_mark_suspended()
3350 mutex_unlock(&ctlr->bus_lock_mutex); in __spi_mark_suspended()
3355 mutex_lock(&ctlr->bus_lock_mutex); in __spi_mark_resumed()
3356 ctlr->flags &= ~SPI_CONTROLLER_SUSPENDED; in __spi_mark_resumed()
3357 mutex_unlock(&ctlr->bus_lock_mutex); in __spi_mark_resumed()
3364 /* Basically no-ops for non-queued controllers */ in spi_controller_suspend()
3365 if (ctlr->queued) { in spi_controller_suspend()
3368 dev_err(&ctlr->dev, "queue stop failed\n"); in spi_controller_suspend()
3382 if (ctlr->queued) { in spi_controller_resume()
3385 dev_err(&ctlr->dev, "queue restart failed\n"); in spi_controller_resume()
3391 /*-------------------------------------------------------------------------*/
3403 if (rxfer->release) in __spi_replace_transfers_release()
3404 rxfer->release(ctlr, msg, res); in __spi_replace_transfers_release()
3407 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); in __spi_replace_transfers_release()
3410 for (i = 0; i < rxfer->inserted; i++) in __spi_replace_transfers_release()
3411 list_del(&rxfer->inserted_transfers[i].transfer_list); in __spi_replace_transfers_release()
3415 * spi_replace_transfers - replace transfers with several transfers
3443 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3448 return ERR_PTR(-ENOMEM); in spi_replace_transfers()
3451 rxfer->release = release; in spi_replace_transfers()
3455 rxfer->extradata = in spi_replace_transfers()
3456 &rxfer->inserted_transfers[insert]; in spi_replace_transfers()
3459 INIT_LIST_HEAD(&rxfer->replaced_transfers); in spi_replace_transfers()
3463 * the @replaced_transfers - it may be spi_message.messages! in spi_replace_transfers()
3465 rxfer->replaced_after = xfer_first->transfer_list.prev; in spi_replace_transfers()
3470 * If the entry after replaced_after it is msg->transfers in spi_replace_transfers()
3474 if (rxfer->replaced_after->next == &msg->transfers) { in spi_replace_transfers()
3475 dev_err(&msg->spi->dev, in spi_replace_transfers()
3478 list_splice(&rxfer->replaced_transfers, in spi_replace_transfers()
3479 rxfer->replaced_after); in spi_replace_transfers()
3485 return ERR_PTR(-EINVAL); in spi_replace_transfers()
3492 list_move_tail(rxfer->replaced_after->next, in spi_replace_transfers()
3493 &rxfer->replaced_transfers); in spi_replace_transfers()
3502 xfer = &rxfer->inserted_transfers[insert - 1 - i]; in spi_replace_transfers()
3508 list_add(&xfer->transfer_list, rxfer->replaced_after); in spi_replace_transfers()
3512 xfer->cs_change = false; in spi_replace_transfers()
3513 xfer->delay.value = 0; in spi_replace_transfers()
3518 rxfer->inserted = insert; in spi_replace_transfers()
3538 count = DIV_ROUND_UP(xfer->len, maxsize); in __spi_split_transfer_maxsize()
3544 xfers = srt->inserted_transfers; in __spi_split_transfer_maxsize()
3576 xfers[i].len = min(maxsize, xfers[i].len - offset); in __spi_split_transfer_maxsize()
3583 *xferp = &xfers[count - 1]; in __spi_split_transfer_maxsize()
3586 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, in __spi_split_transfer_maxsize()
3588 SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, in __spi_split_transfer_maxsize()
3595 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3620 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxsize()
3621 if (xfer->len > maxsize) { in spi_split_transfers_maxsize()
3635 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3659 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxwords()
3663 maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word)); in spi_split_transfers_maxwords()
3664 if (xfer->len > maxsize) { in spi_split_transfers_maxwords()
3676 /*-------------------------------------------------------------------------*/
3686 if (ctlr->bits_per_word_mask) { in __spi_validate_bits_per_word()
3689 return -EINVAL; in __spi_validate_bits_per_word()
3690 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) in __spi_validate_bits_per_word()
3691 return -EINVAL; in __spi_validate_bits_per_word()
3698 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3705 struct device *parent = spi->controller->dev.parent; in spi_set_cs_timing()
3708 if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) { in spi_set_cs_timing()
3709 if (spi->controller->auto_runtime_pm) { in spi_set_cs_timing()
3713 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_set_cs_timing()
3718 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3722 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3729 * spi_setup - setup SPI mode and clock rate
3744 * LSB-first wire encoding, or active-high chipselects.
3757 if ((hweight_long(spi->mode & in spi_setup()
3759 (hweight_long(spi->mode & in spi_setup()
3761 dev_err(&spi->dev, in spi_setup()
3762 "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); in spi_setup()
3763 return -EINVAL; in spi_setup()
3766 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3769 return -EINVAL; in spi_setup()
3776 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | in spi_setup()
3782 dev_warn(&spi->dev, in spi_setup()
3785 spi->mode &= ~ugly_bits; in spi_setup()
3789 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3791 return -EINVAL; in spi_setup()
3794 if (!spi->bits_per_word) { in spi_setup()
3795 spi->bits_per_word = 8; in spi_setup()
3798 * Some controllers may not support the default 8 bits-per-word in spi_setup()
3801 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3802 spi->bits_per_word); in spi_setup()
3807 if (spi->controller->max_speed_hz && in spi_setup()
3808 (!spi->max_speed_hz || in spi_setup()
3809 spi->max_speed_hz > spi->controller->max_speed_hz)) in spi_setup()
3810 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3812 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3814 if (spi->controller->setup) { in spi_setup()
3815 status = spi->controller->setup(spi); in spi_setup()
3817 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3818 dev_err(&spi->controller->dev, "Failed to setup device: %d\n", in spi_setup()
3826 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3830 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3831 status = pm_runtime_resume_and_get(spi->controller->dev.parent); in spi_setup()
3833 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3834 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3842 * checking for a non-zero return value instead of a negative in spi_setup()
3848 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3849 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
3854 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3856 if (spi->rt && !spi->controller->rt) { in spi_setup()
3857 spi->controller->rt = true; in spi_setup()
3858 spi_set_thread_rt(spi->controller); in spi_setup()
3863 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
3864 spi->mode & SPI_MODE_X_MASK, in spi_setup()
3865 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
3866 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
3867 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
3868 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
3869 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
3881 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); in _spi_xfer_word_delay_update()
3885 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
3890 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
3891 sizeof(xfer->word_delay)); in _spi_xfer_word_delay_update()
3898 struct spi_controller *ctlr = spi->controller; in __spi_validate()
3902 if (list_empty(&message->transfers)) in __spi_validate()
3903 return -EINVAL; in __spi_validate()
3907 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO in __spi_validate()
3908 * for the CS line, we can emulate the CS-per-word hardware function by in __spi_validate()
3909 * splitting transfers into one-word transfers and ensuring that in __spi_validate()
3912 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || in __spi_validate()
3914 size_t maxsize = BITS_TO_BYTES(spi->bits_per_word); in __spi_validate()
3917 /* spi_split_transfers_maxsize() requires message->spi */ in __spi_validate()
3918 message->spi = spi; in __spi_validate()
3925 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3927 if (list_is_last(&xfer->transfer_list, &message->transfers)) in __spi_validate()
3929 xfer->cs_change = 1; in __spi_validate()
3934 * Half-duplex links include original MicroWire, and ones with in __spi_validate()
3935 * only one data pin like SPI_3WIRE (switches direction) or where in __spi_validate()
3939 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || in __spi_validate()
3940 (spi->mode & SPI_3WIRE)) { in __spi_validate()
3941 unsigned flags = ctlr->flags; in __spi_validate()
3943 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3944 if (xfer->rx_buf && xfer->tx_buf) in __spi_validate()
3945 return -EINVAL; in __spi_validate()
3946 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) in __spi_validate()
3947 return -EINVAL; in __spi_validate()
3948 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) in __spi_validate()
3949 return -EINVAL; in __spi_validate()
3961 message->frame_length = 0; in __spi_validate()
3962 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3963 xfer->effective_speed_hz = 0; in __spi_validate()
3964 message->frame_length += xfer->len; in __spi_validate()
3965 if (!xfer->bits_per_word) in __spi_validate()
3966 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
3968 if (!xfer->speed_hz) in __spi_validate()
3969 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
3971 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) in __spi_validate()
3972 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
3974 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) in __spi_validate()
3975 return -EINVAL; in __spi_validate()
3979 * where SPI word size should be power-of-two multiple. in __spi_validate()
3981 if (xfer->bits_per_word <= 8) in __spi_validate()
3983 else if (xfer->bits_per_word <= 16) in __spi_validate()
3989 if (xfer->len % w_size) in __spi_validate()
3990 return -EINVAL; in __spi_validate()
3992 if (xfer->speed_hz && ctlr->min_speed_hz && in __spi_validate()
3993 xfer->speed_hz < ctlr->min_speed_hz) in __spi_validate()
3994 return -EINVAL; in __spi_validate()
3996 if (xfer->tx_buf && !xfer->tx_nbits) in __spi_validate()
3997 xfer->tx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3998 if (xfer->rx_buf && !xfer->rx_nbits) in __spi_validate()
3999 xfer->rx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
4005 if (xfer->tx_buf) { in __spi_validate()
4006 if (spi->mode & SPI_NO_TX) in __spi_validate()
4007 return -EINVAL; in __spi_validate()
4008 if (xfer->tx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
4009 xfer->tx_nbits != SPI_NBITS_DUAL && in __spi_validate()
4010 xfer->tx_nbits != SPI_NBITS_QUAD && in __spi_validate()
4011 xfer->tx_nbits != SPI_NBITS_OCTAL) in __spi_validate()
4012 return -EINVAL; in __spi_validate()
4013 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
4014 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
4015 return -EINVAL; in __spi_validate()
4016 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
4017 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
4018 return -EINVAL; in __spi_validate()
4021 if (xfer->rx_buf) { in __spi_validate()
4022 if (spi->mode & SPI_NO_RX) in __spi_validate()
4023 return -EINVAL; in __spi_validate()
4024 if (xfer->rx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
4025 xfer->rx_nbits != SPI_NBITS_DUAL && in __spi_validate()
4026 xfer->rx_nbits != SPI_NBITS_QUAD && in __spi_validate()
4027 xfer->rx_nbits != SPI_NBITS_OCTAL) in __spi_validate()
4028 return -EINVAL; in __spi_validate()
4029 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
4030 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
4031 return -EINVAL; in __spi_validate()
4032 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
4033 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
4034 return -EINVAL; in __spi_validate()
4038 return -EINVAL; in __spi_validate()
4041 message->status = -EINPROGRESS; in __spi_validate()
4048 struct spi_controller *ctlr = spi->controller; in __spi_async()
4055 if (!ctlr->transfer) in __spi_async()
4056 return -ENOTSUPP; in __spi_async()
4058 message->spi = spi; in __spi_async()
4060 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async); in __spi_async()
4061 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); in __spi_async()
4065 if (!ctlr->ptp_sts_supported) { in __spi_async()
4066 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_async()
4067 xfer->ptp_sts_word_pre = 0; in __spi_async()
4068 ptp_read_system_prets(xfer->ptp_sts); in __spi_async()
4072 return ctlr->transfer(spi, message); in __spi_async()
4076 * spi_async - asynchronous SPI transfer
4085 * Before that invocation, the value of message->status is undefined.
4086 * When the callback is issued, message->status holds either zero (to
4108 struct spi_controller *ctlr = spi->controller; in spi_async()
4116 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async()
4118 if (ctlr->bus_lock_flag) in spi_async()
4119 ret = -EBUSY; in spi_async()
4123 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async()
4130 * spi_async_locked - version of spi_async with exclusive bus usage
4139 * Before that invocation, the value of message->status is undefined.
4140 * When the callback is issued, message->status holds either zero (to
4162 struct spi_controller *ctlr = spi->controller; in spi_async_locked()
4170 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
4174 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
4185 mutex_lock(&ctlr->io_mutex); in __spi_transfer_message_noqueue()
4187 was_busy = ctlr->busy; in __spi_transfer_message_noqueue()
4189 ctlr->cur_msg = msg; in __spi_transfer_message_noqueue()
4192 dev_err(&ctlr->dev, "noqueue transfer failed\n"); in __spi_transfer_message_noqueue()
4193 ctlr->cur_msg = NULL; in __spi_transfer_message_noqueue()
4194 ctlr->fallback = false; in __spi_transfer_message_noqueue()
4197 kfree(ctlr->dummy_rx); in __spi_transfer_message_noqueue()
4198 ctlr->dummy_rx = NULL; in __spi_transfer_message_noqueue()
4199 kfree(ctlr->dummy_tx); in __spi_transfer_message_noqueue()
4200 ctlr->dummy_tx = NULL; in __spi_transfer_message_noqueue()
4201 if (ctlr->unprepare_transfer_hardware && in __spi_transfer_message_noqueue()
4202 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_transfer_message_noqueue()
4203 dev_err(&ctlr->dev, in __spi_transfer_message_noqueue()
4208 mutex_unlock(&ctlr->io_mutex); in __spi_transfer_message_noqueue()
4211 /*-------------------------------------------------------------------------*/
4228 struct spi_controller *ctlr = spi->controller; in __spi_sync()
4231 dev_warn_once(&spi->dev, "Attempted to sync while suspend\n"); in __spi_sync()
4232 return -ESHUTDOWN; in __spi_sync()
4239 message->spi = spi; in __spi_sync()
4241 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync); in __spi_sync()
4242 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); in __spi_sync()
4250 if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) { in __spi_sync()
4251 message->actual_length = 0; in __spi_sync()
4252 message->status = -EINPROGRESS; in __spi_sync()
4256 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4257 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4261 return message->status; in __spi_sync()
4270 message->complete = spi_complete; in __spi_sync()
4271 message->context = &done; in __spi_sync()
4275 status = message->status; in __spi_sync()
4277 message->complete = NULL; in __spi_sync()
4278 message->context = NULL; in __spi_sync()
4284 * spi_sync - blocking/synchronous SPI data transfers
4290 * is non-interruptible, and has no timeout. Low-overhead controller
4295 * frequently-used devices may want to minimize costs of selecting a chip,
4308 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
4310 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
4317 * spi_sync_locked - version of spi_sync with exclusive bus usage
4323 * is non-interruptible, and has no timeout. Low-overhead controller
4339 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4344 * is non-interruptible, and has no timeout.
4357 mutex_lock(&ctlr->bus_lock_mutex); in spi_bus_lock()
4359 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
4360 ctlr->bus_lock_flag = 1; in spi_bus_lock()
4361 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
4370 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4375 * is non-interruptible, and has no timeout.
4384 ctlr->bus_lock_flag = 0; in spi_bus_unlock()
4386 mutex_unlock(&ctlr->bus_lock_mutex); in spi_bus_unlock()
4398 * spi_write_then_read - SPI synchronous write followed by read
4400 * @txbuf: data to be written (need not be DMA-safe)
4402 * @rxbuf: buffer into which data will be read (need not be DMA-safe)
4412 * Performance-sensitive or bulk transfer code should instead use
4413 * spi_{async,sync}() calls with DMA-safe buffers.
4429 * Use preallocated DMA-safe buffer if we can. We can't avoid in spi_write_then_read()
4432 * using the pre-allocated buffer or the transfer is too large. in spi_write_then_read()
4438 return -ENOMEM; in spi_write_then_read()
4472 /*-------------------------------------------------------------------------*/
4507 ctlr = of_find_spi_controller_by_node(rd->dn->parent); in of_spi_notify()
4511 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { in of_spi_notify()
4512 put_device(&ctlr->dev); in of_spi_notify()
4520 rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE; in of_spi_notify()
4521 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4522 put_device(&ctlr->dev); in of_spi_notify()
4526 __func__, rd->dn); in of_spi_notify()
4527 of_node_clear_flag(rd->dn, OF_POPULATED); in of_spi_notify()
4534 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_spi_notify()
4538 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4546 put_device(&spi->dev); in of_spi_notify()
4563 return ACPI_COMPANION(dev->parent) == data; in spi_acpi_controller_match()
4603 put_device(&ctlr->dev); in acpi_spi_notify()
4614 put_device(&spi->dev); in acpi_spi_notify()
4634 status = -ENOMEM; in spi_init()