Lines Matching +full:tx +full:- +full:clk +full:- +full:1000 +full:- +full:inverted
1 // SPDX-License-Identifier: GPL-2.0-or-later
9 #include <linux/clk/clk-conf.h>
13 #include <linux/dma-mapping.h>
35 #include <linux/spi/spi-mem.h>
51 spi_controller_put(spi->controller);
52 kfree(spi->driver_override);
53 free_percpu(spi->pcpu_statistics);
63 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
64 if (len != -ENODEV)
67 return sysfs_emit(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
78 ret = driver_set_override(dev, &spi->driver_override, buf, count);
92 len = sysfs_emit(buf, "%s\n", spi->driver_override ? : "");
114 u64_stats_init(&stat->syncp);
135 start = u64_stats_fetch_begin(&pcpu_stats->syncp);
137 } while (u64_stats_fetch_retry(&pcpu_stats->syncp, start));
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;
325 u64_stats_update_begin(&stats->syncp);
327 u64_stats_inc(&stats->transfers);
328 u64_stats_inc(&stats->transfer_bytes_histo[l2len]);
330 u64_stats_add(&stats->bytes, xfer->len);
332 u64_stats_add(&stats->bytes_tx, xfer->len);
334 u64_stats_add(&stats->bytes_rx, xfer->len);
336 u64_stats_update_end(&stats->syncp);
341 * modalias support makes "modprobe $MODALIAS" new-style hotplug work,
346 while (id->name[0]) {
347 if (!strcmp(name, id->name))
356 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
358 return spi_match_id(sdrv->id_table, sdev->modalias);
366 match = device_get_match_data(&sdev->dev);
370 return (const void *)spi_get_device_id(sdev)->driver_data;
380 if (spi->driver_override)
381 return strcmp(spi->driver_override, drv->name) == 0;
391 if (sdrv->id_table)
392 return !!spi_match_id(sdrv->id_table, spi->modalias);
394 return strcmp(spi->modalias, drv->name) == 0;
403 if (rc != -ENODEV)
406 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
411 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
415 ret = of_clk_set_defaults(dev->of_node, false);
419 if (dev->of_node) {
420 spi->irq = of_irq_get(dev->of_node, 0);
421 if (spi->irq == -EPROBE_DEFER)
422 return -EPROBE_DEFER;
423 if (spi->irq < 0)
424 spi->irq = 0;
431 if (sdrv->probe) {
432 ret = sdrv->probe(spi);
442 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
444 if (sdrv->remove)
445 sdrv->remove(to_spi_device(dev));
452 if (dev->driver) {
453 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
455 if (sdrv->shutdown)
456 sdrv->shutdown(to_spi_device(dev));
472 * __spi_register_driver - register a SPI driver
481 sdrv->driver.owner = owner;
482 sdrv->driver.bus = &spi_bus_type;
489 if (sdrv->driver.of_match_table) {
492 for (of_id = sdrv->driver.of_match_table; of_id->compatible[0];
497 of_name = strnchr(of_id->compatible,
498 sizeof(of_id->compatible), ',');
502 of_name = of_id->compatible;
504 if (sdrv->id_table) {
507 spi_id = spi_match_id(sdrv->id_table, of_name);
511 if (strcmp(sdrv->driver.name, of_name) == 0)
516 sdrv->driver.name, of_id->compatible);
520 return driver_register(&sdrv->driver);
524 /*-------------------------------------------------------------------------*/
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
578 spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL);
579 if (!spi->pcpu_statistics) {
585 spi->controller = ctlr;
586 spi->dev.parent = &ctlr->dev;
587 spi->dev.bus = &spi_bus_type;
588 spi->dev.release = spidev_release;
589 spi->mode = ctlr->buswidth_override_bits;
591 device_initialize(&spi->dev);
598 struct device *dev = &spi->dev;
602 dev_set_name(dev, "spi-%s", acpi_dev_name(to_acpi_device_node(fwnode)));
607 dev_set_name(dev, "spi-%pfwP", fwnode);
611 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
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)
643 return -EBUSY;
655 if (spi->controller == new_spi->controller) {
667 if (spi->controller->cleanup)
668 spi->controller->cleanup(spi);
673 struct spi_controller *ctlr = spi->controller;
674 struct device *dev = ctlr->dev.parent;
681 if (is_valid_cs(cs) && cs >= ctlr->num_chipselect) {
683 ctlr->num_chipselect);
684 return -EINVAL;
690 * For example, spi->chip_select[0] != spi->chip_select[1] and so on.
714 !device_is_registered(&ctlr->dev)) {
715 return -ENODEV;
718 if (ctlr->cs_gpiods) {
724 spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]);
736 dev_name(&spi->dev), status);
741 status = device_add(&spi->dev);
744 dev_name(&spi->dev), status);
747 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
754 * spi_add_device - Add spi_device allocated with spi_alloc_device
764 struct spi_controller *ctlr = spi->controller;
770 mutex_lock(&ctlr->add_lock);
772 mutex_unlock(&ctlr->add_lock);
786 * spi_new_device - instantiate one new 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.
809 * error-or-pointer (not NULL-or-pointer), troubleshootability
817 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
819 /* Use provided chip-select for proxy device */
821 spi_set_chipselect(proxy, 0, chip->chip_select);
823 proxy->max_speed_hz = chip->max_speed_hz;
824 proxy->mode = chip->mode;
825 proxy->irq = chip->irq;
826 strscpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
827 proxy->dev.platform_data = (void *) chip->platform_data;
828 proxy->controller_data = chip->controller_data;
829 proxy->controller_state = NULL;
831 * By default spi->chip_select[0] will hold the physical CS number,
832 * so set bit 0 in spi->cs_index_mask.
834 proxy->cs_index_mask = BIT(0);
836 if (chip->swnode) {
837 status = device_add_software_node(&proxy->dev, chip->swnode);
839 dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n",
840 chip->modalias, status);
852 device_remove_software_node(&proxy->dev);
859 * spi_unregister_device - unregister a single SPI device
870 if (spi->dev.of_node) {
871 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
872 of_node_put(spi->dev.of_node);
874 if (ACPI_COMPANION(&spi->dev))
875 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
876 device_remove_software_node(&spi->dev);
877 device_del(&spi->dev);
879 put_device(&spi->dev);
888 if (ctlr->bus_num != bi->bus_num)
893 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
894 bi->modalias);
898 * spi_register_board_info - register SPI devices for a given board
903 * Board-specific early init code calls this (probably during arch_initcall)
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
914 * any embedded pointers (platform_data, etc), they're copied as-is.
928 return -ENOMEM;
933 memcpy(&bi->board_info, info, sizeof(*info));
936 list_add_tail(&bi->list, &board_list);
939 &bi->board_info);
946 /*-------------------------------------------------------------------------*/
951 * spi_res_alloc - allocate a spi resource that is life-cycle managed
973 INIT_LIST_HEAD(&sres->entry);
974 sres->release = release;
976 return sres->data;
980 * spi_res_free - free an SPI resource
990 WARN_ON(!list_empty(&sres->entry));
995 * spi_res_add - add a spi_res to the spi_message
1003 WARN_ON(!list_empty(&sres->entry));
1004 list_add_tail(&sres->entry, &message->resources);
1008 * spi_res_release - release all SPI resources for this message
1016 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
1017 if (res->release)
1018 res->release(ctlr, message, res->data);
1020 list_del(&res->entry);
1026 /*-------------------------------------------------------------------------*/
1029 if (!(spi->cs_index_mask & BIT(idx))) {} else
1037 if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx))
1047 * thus the SPISerialBus() resource defines it on the per-chip
1055 if (has_acpi_companion(&spi->dev))
1062 spi_delay_exec(&spi->cs_setup, NULL);
1064 spi_delay_exec(&spi->cs_inactive, NULL);
1076 if (!force && ((enable && spi->controller->last_cs_index_mask == spi->cs_index_mask &&
1078 (!enable && spi->controller->last_cs_index_mask == spi->cs_index_mask &&
1080 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
1085 spi->controller->last_cs_index_mask = spi->cs_index_mask;
1087 spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : SPI_INVALID_CS;
1088 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
1090 if (spi->mode & SPI_CS_HIGH)
1097 if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate)
1098 spi_delay_exec(&spi->cs_hold, NULL);
1101 if (!(spi->mode & SPI_NO_CS)) {
1108 if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) &&
1109 spi->controller->set_cs)
1110 spi->controller->set_cs(spi, !enable);
1111 } else if (spi->controller->set_cs) {
1112 spi->controller->set_cs(spi, !enable);
1115 if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) {
1117 spi_delay_exec(&spi->cs_setup, NULL);
1119 spi_delay_exec(&spi->cs_inactive, NULL);
1149 desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len);
1152 return -EINVAL;
1159 sg = &sgt->sgl[0];
1170 PAGE_SIZE - offset_in_page(buf)));
1177 return -ENOMEM;
1188 len -= min;
1215 sgt->orig_nents = 0;
1216 sgt->nents = 0;
1231 if (!ctlr->can_dma)
1234 if (ctlr->dma_tx)
1235 tx_dev = ctlr->dma_tx->device->dev;
1236 else if (ctlr->dma_map_dev)
1237 tx_dev = ctlr->dma_map_dev;
1239 tx_dev = ctlr->dev.parent;
1241 if (ctlr->dma_rx)
1242 rx_dev = ctlr->dma_rx->device->dev;
1243 else if (ctlr->dma_map_dev)
1244 rx_dev = ctlr->dma_map_dev;
1246 rx_dev = ctlr->dev.parent;
1248 ret = -ENOMSG;
1249 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1253 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
1256 if (xfer->tx_buf != NULL) {
1257 ret = spi_map_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
1258 (void *)xfer->tx_buf,
1259 xfer->len, DMA_TO_DEVICE,
1264 xfer->tx_sg_mapped = true;
1267 if (xfer->rx_buf != NULL) {
1268 ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
1269 xfer->rx_buf, xfer->len,
1273 &xfer->tx_sg, DMA_TO_DEVICE,
1279 xfer->rx_sg_mapped = true;
1286 ctlr->cur_rx_dma_dev = rx_dev;
1287 ctlr->cur_tx_dma_dev = tx_dev;
1294 struct device *rx_dev = ctlr->cur_rx_dma_dev;
1295 struct device *tx_dev = ctlr->cur_tx_dma_dev;
1298 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1302 if (xfer->rx_sg_mapped)
1303 spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
1305 xfer->rx_sg_mapped = false;
1307 if (xfer->tx_sg_mapped)
1308 spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
1310 xfer->tx_sg_mapped = false;
1319 struct device *rx_dev = ctlr->cur_rx_dma_dev;
1320 struct device *tx_dev = ctlr->cur_tx_dma_dev;
1322 if (xfer->tx_sg_mapped)
1323 dma_sync_sgtable_for_device(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
1324 if (xfer->rx_sg_mapped)
1325 dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1331 struct device *rx_dev = ctlr->cur_rx_dma_dev;
1332 struct device *tx_dev = ctlr->cur_tx_dma_dev;
1334 if (xfer->rx_sg_mapped)
1335 dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1336 if (xfer->tx_sg_mapped)
1337 dma_sync_sgtable_for_cpu(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
1368 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1373 if (xfer->tx_buf == ctlr->dummy_tx)
1374 xfer->tx_buf = NULL;
1375 if (xfer->rx_buf == ctlr->dummy_rx)
1376 xfer->rx_buf = NULL;
1388 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX))
1389 && !(msg->spi->mode & SPI_3WIRE)) {
1393 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1394 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
1395 !xfer->tx_buf)
1396 max_tx = max(xfer->len, max_tx);
1397 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
1398 !xfer->rx_buf)
1399 max_rx = max(xfer->len, max_rx);
1403 tmp = krealloc(ctlr->dummy_tx, max_tx,
1406 return -ENOMEM;
1407 ctlr->dummy_tx = tmp;
1411 tmp = krealloc(ctlr->dummy_rx, max_rx,
1414 return -ENOMEM;
1415 ctlr->dummy_rx = tmp;
1419 list_for_each_entry(xfer, &msg->transfers,
1421 if (!xfer->len)
1423 if (!xfer->tx_buf)
1424 xfer->tx_buf = ctlr->dummy_tx;
1425 if (!xfer->rx_buf)
1426 xfer->rx_buf = ctlr->dummy_rx;
1438 struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1439 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
1440 u32 speed_hz = xfer->speed_hz;
1444 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1445 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1446 return -EINTR;
1458 ms = 8LL * MSEC_PER_SEC * xfer->len;
1469 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1475 dev_err(&msg->spi->dev,
1477 return -ETIMEDOUT;
1480 if (xfer->error & SPI_TRANS_FAIL_IO)
1481 return -EIO;
1505 u32 delay = _delay->value;
1506 u32 unit = _delay->unit;
1522 return -EINVAL;
1527 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1529 return -EINVAL;
1535 return -EINVAL;
1549 return -EINVAL;
1565 u32 delay = xfer->cs_change_delay.value;
1566 u32 unit = xfer->cs_change_delay.unit;
1569 /* Return early on "fast" mode - for everything but USECS */
1576 ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1578 dev_err_once(&msg->spi->dev,
1593 * spi_transfer_one_message - Default implementation of transfer_one_message()
1605 struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1606 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
1608 xfer = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list);
1609 spi_set_cs(msg->spi, !xfer->cs_off, false);
1614 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1620 if (!ctlr->ptp_sts_supported) {
1621 xfer->ptp_sts_word_pre = 0;
1622 ptp_read_system_prets(xfer->ptp_sts);
1625 if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
1626 reinit_completion(&ctlr->xfer_completion);
1630 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1634 if ((xfer->tx_sg_mapped || xfer->rx_sg_mapped) &&
1635 (xfer->error & SPI_TRANS_FAIL_NO_START)) {
1637 ctlr->fallback = true;
1638 xfer->error &= ~SPI_TRANS_FAIL_NO_START;
1646 dev_err(&msg->spi->dev,
1654 msg->status = ret;
1659 if (xfer->len)
1660 dev_err(&msg->spi->dev,
1662 xfer->len);
1665 if (!ctlr->ptp_sts_supported) {
1666 ptp_read_system_postts(xfer->ptp_sts);
1667 xfer->ptp_sts_word_post = xfer->len;
1672 if (msg->status != -EINPROGRESS)
1677 if (xfer->cs_change) {
1678 if (list_is_last(&xfer->transfer_list,
1679 &msg->transfers)) {
1682 if (!xfer->cs_off)
1683 spi_set_cs(msg->spi, false, false);
1685 if (!list_next_entry(xfer, transfer_list)->cs_off)
1686 spi_set_cs(msg->spi, true, false);
1688 } else if (!list_is_last(&xfer->transfer_list, &msg->transfers) &&
1689 xfer->cs_off != list_next_entry(xfer, transfer_list)->cs_off) {
1690 spi_set_cs(msg->spi, xfer->cs_off, false);
1693 msg->actual_length += xfer->len;
1698 spi_set_cs(msg->spi, false, false);
1700 if (msg->status == -EINPROGRESS)
1701 msg->status = ret;
1703 if (msg->status && ctlr->handle_err)
1704 ctlr->handle_err(ctlr, msg);
1712 * spi_finalize_current_transfer - report completion of a transfer
1721 complete(&ctlr->xfer_completion);
1727 if (ctlr->auto_runtime_pm) {
1728 pm_runtime_mark_last_busy(ctlr->dev.parent);
1729 pm_runtime_put_autosuspend(ctlr->dev.parent);
1739 if (!was_busy && ctlr->auto_runtime_pm) {
1740 ret = pm_runtime_get_sync(ctlr->dev.parent);
1742 pm_runtime_put_noidle(ctlr->dev.parent);
1743 dev_err(&ctlr->dev, "Failed to power device: %d\n",
1746 msg->status = ret;
1756 if (!was_busy && ctlr->prepare_transfer_hardware) {
1757 ret = ctlr->prepare_transfer_hardware(ctlr);
1759 dev_err(&ctlr->dev,
1763 if (ctlr->auto_runtime_pm)
1764 pm_runtime_put(ctlr->dev.parent);
1766 msg->status = ret;
1775 if (ctlr->prepare_message) {
1776 ret = ctlr->prepare_message(ctlr, msg);
1778 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1780 msg->status = ret;
1784 msg->prepared = true;
1789 msg->status = ret;
1794 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1795 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1796 xfer->ptp_sts_word_pre = 0;
1797 ptp_read_system_prets(xfer->ptp_sts);
1807 * ctlr->cur_msg.
1814 WRITE_ONCE(ctlr->cur_msg_incomplete, true);
1815 WRITE_ONCE(ctlr->cur_msg_need_completion, false);
1816 reinit_completion(&ctlr->cur_msg_completion);
1819 ret = ctlr->transfer_one_message(ctlr, msg);
1821 dev_err(&ctlr->dev,
1826 WRITE_ONCE(ctlr->cur_msg_need_completion, true);
1828 if (READ_ONCE(ctlr->cur_msg_incomplete))
1829 wait_for_completion(&ctlr->cur_msg_completion);
1835 * __spi_pump_messages - function which processes SPI message queue
1855 mutex_lock(&ctlr->io_mutex);
1858 spin_lock_irqsave(&ctlr->queue_lock, flags);
1861 if (ctlr->cur_msg)
1865 if (list_empty(&ctlr->queue) || !ctlr->running) {
1866 if (!ctlr->busy)
1869 /* Defer any non-atomic teardown to the thread */
1871 if (!ctlr->dummy_rx && !ctlr->dummy_tx &&
1872 !ctlr->unprepare_transfer_hardware) {
1874 ctlr->busy = false;
1875 ctlr->queue_empty = true;
1878 kthread_queue_work(ctlr->kworker,
1879 &ctlr->pump_messages);
1884 ctlr->busy = false;
1885 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1887 kfree(ctlr->dummy_rx);
1888 ctlr->dummy_rx = NULL;
1889 kfree(ctlr->dummy_tx);
1890 ctlr->dummy_tx = NULL;
1891 if (ctlr->unprepare_transfer_hardware &&
1892 ctlr->unprepare_transfer_hardware(ctlr))
1893 dev_err(&ctlr->dev,
1898 spin_lock_irqsave(&ctlr->queue_lock, flags);
1899 ctlr->queue_empty = true;
1904 msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1905 ctlr->cur_msg = msg;
1907 list_del_init(&msg->queue);
1908 if (ctlr->busy)
1911 ctlr->busy = true;
1912 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1915 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
1917 ctlr->cur_msg = NULL;
1918 ctlr->fallback = false;
1920 mutex_unlock(&ctlr->io_mutex);
1928 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1929 mutex_unlock(&ctlr->io_mutex);
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
1956 * This is a helper for drivers to collect the beginning of the TX timestamp
1959 * per batch of words etc) is arbitrary as long as the @tx buffer offset is
1962 * the driver advances its @tx buffer pointer monotonically.
1968 if (!xfer->ptp_sts)
1971 if (xfer->timestamped)
1974 if (progress > xfer->ptp_sts_word_pre)
1978 xfer->ptp_sts_word_pre = progress;
1981 local_irq_save(ctlr->irq_flags);
1985 ptp_read_system_prets(xfer->ptp_sts);
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.
1996 * This is a helper for drivers to collect the end of the TX timestamp for
1998 * frequency: only the first call where @tx exceeds or is equal to the
2005 if (!xfer->ptp_sts)
2008 if (xfer->timestamped)
2011 if (progress < xfer->ptp_sts_word_post)
2014 ptp_read_system_postts(xfer->ptp_sts);
2017 local_irq_restore(ctlr->irq_flags);
2022 xfer->ptp_sts_word_post = progress;
2024 xfer->timestamped = 1;
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,
2047 sched_set_fifo(ctlr->kworker->task);
2052 ctlr->running = false;
2053 ctlr->busy = false;
2054 ctlr->queue_empty = true;
2056 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev));
2057 if (IS_ERR(ctlr->kworker)) {
2058 dev_err(&ctlr->dev, "failed to create message pump kworker\n");
2059 return PTR_ERR(ctlr->kworker);
2062 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
2071 if (ctlr->rt)
2078 * spi_get_next_queued_message() - called by driver to check for queued
2093 spin_lock_irqsave(&ctlr->queue_lock, flags);
2094 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
2096 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2103 * __spi_unoptimize_message - shared implementation of spi_unoptimize_message()
2115 struct spi_controller *ctlr = msg->spi->controller;
2117 if (ctlr->unoptimize_message)
2118 ctlr->unoptimize_message(msg);
2122 msg->optimized = false;
2123 msg->opt_state = NULL;
2127 * spi_maybe_unoptimize_message - unoptimize msg not managed by a peripheral
2135 if (!msg->pre_optimized && msg->optimized &&
2136 !msg->spi->controller->defer_optimize_message)
2141 * spi_finalize_current_message() - the current message is complete
2153 mesg = ctlr->cur_msg;
2155 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
2156 list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
2157 ptp_read_system_postts(xfer->ptp_sts);
2158 xfer->ptp_sts_word_post = xfer->len;
2162 if (unlikely(ctlr->ptp_sts_supported))
2163 list_for_each_entry(xfer, &mesg->transfers, transfer_list)
2164 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped);
2168 if (mesg->prepared && ctlr->unprepare_message) {
2169 ret = ctlr->unprepare_message(ctlr, mesg);
2171 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
2176 mesg->prepared = false;
2180 WRITE_ONCE(ctlr->cur_msg_incomplete, false);
2182 if (READ_ONCE(ctlr->cur_msg_need_completion))
2183 complete(&ctlr->cur_msg_completion);
2187 mesg->state = NULL;
2188 if (mesg->complete)
2189 mesg->complete(mesg->context);
2197 spin_lock_irqsave(&ctlr->queue_lock, flags);
2199 if (ctlr->running || ctlr->busy) {
2200 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2201 return -EBUSY;
2204 ctlr->running = true;
2205 ctlr->cur_msg = NULL;
2206 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2208 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2220 * A wait_queue on the ctlr->busy could be used, but then the common
2225 spin_lock_irqsave(&ctlr->queue_lock, flags);
2226 if (list_empty(&ctlr->queue) && !ctlr->busy) {
2227 ctlr->running = false;
2228 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2231 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2233 } while (--limit);
2235 return -EBUSY;
2251 dev_err(&ctlr->dev, "problem destroying queue\n");
2255 kthread_destroy_worker(ctlr->kworker);
2264 struct spi_controller *ctlr = spi->controller;
2267 spin_lock_irqsave(&ctlr->queue_lock, flags);
2269 if (!ctlr->running) {
2270 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2271 return -ESHUTDOWN;
2273 msg->actual_length = 0;
2274 msg->status = -EINPROGRESS;
2276 list_add_tail(&msg->queue, &ctlr->queue);
2277 ctlr->queue_empty = false;
2278 if (!ctlr->busy && need_pump)
2279 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2281 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2286 * spi_queued_transfer - transfer function for queued transfers
2301 ctlr->transfer = spi_queued_transfer;
2302 if (!ctlr->transfer_one_message)
2303 ctlr->transfer_one_message = spi_transfer_one_message;
2308 dev_err(&ctlr->dev, "problem initializing queue\n");
2311 ctlr->queued = true;
2314 dev_err(&ctlr->dev, "problem starting queue\n");
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
2334 * before the spi-mem operation.
2338 if (ctlr->transfer == spi_queued_transfer)
2342 /*-------------------------------------------------------------------------*/
2352 delay->value = DIV_ROUND_UP(value, 1000);
2353 delay->unit = SPI_DELAY_UNIT_USECS;
2355 delay->value = value;
2356 delay->unit = SPI_DELAY_UNIT_NSECS;
2368 if (of_property_read_bool(nc, "spi-cpha"))
2369 spi->mode |= SPI_CPHA;
2370 if (of_property_read_bool(nc, "spi-cpol"))
2371 spi->mode |= SPI_CPOL;
2372 if (of_property_read_bool(nc, "spi-3wire"))
2373 spi->mode |= SPI_3WIRE;
2374 if (of_property_read_bool(nc, "spi-lsb-first"))
2375 spi->mode |= SPI_LSB_FIRST;
2376 if (of_property_read_bool(nc, "spi-cs-high"))
2377 spi->mode |= SPI_CS_HIGH;
2380 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
2383 spi->mode |= SPI_NO_TX;
2388 spi->mode |= SPI_TX_DUAL;
2391 spi->mode |= SPI_TX_QUAD;
2394 spi->mode |= SPI_TX_OCTAL;
2397 dev_warn(&ctlr->dev,
2398 "spi-tx-bus-width %d not supported\n",
2404 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
2407 spi->mode |= SPI_NO_RX;
2412 spi->mode |= SPI_RX_DUAL;
2415 spi->mode |= SPI_RX_QUAD;
2418 spi->mode |= SPI_RX_OCTAL;
2421 dev_warn(&ctlr->dev,
2422 "spi-rx-bus-width %d not supported\n",
2430 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
2432 return -EINVAL;
2437 if (ctlr->num_chipselect > SPI_CS_CNT_MAX) {
2438 dev_err(&ctlr->dev, "No. of CS is more than max. no. of supported CS\n");
2439 return -EINVAL;
2448 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
2452 if (rc > ctlr->num_chipselect) {
2453 dev_err(&ctlr->dev, "%pOF has number of CS > ctlr->num_chipselect (%d)\n",
2457 if ((of_property_read_bool(nc, "parallel-memories")) &&
2458 (!(ctlr->flags & SPI_CONTROLLER_MULTI_CS))) {
2459 dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n");
2460 return -EINVAL;
2466 * By default spi->chip_select[0] will hold the physical CS number,
2467 * so set bit 0 in spi->cs_index_mask.
2469 spi->cs_index_mask = BIT(0);
2472 if (!of_property_read_u32(nc, "spi-max-frequency", &value))
2473 spi->max_speed_hz = value;
2476 of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns");
2477 of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns");
2478 of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns");
2492 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
2493 rc = -ENOMEM;
2498 rc = of_alias_from_compatible(nc, spi->modalias,
2499 sizeof(spi->modalias));
2501 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
2512 device_set_node(&spi->dev, of_fwnode_handle(nc));
2517 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
2531 * of_register_spi_devices() - Register child devices onto the SPI bus
2542 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
2547 dev_warn(&ctlr->dev,
2558 * spi_new_ancillary_device() - Register ancillary SPI device
2562 * Register an ancillary SPI device; for example some chips have a chip-select
2572 struct spi_controller *ctlr = spi->controller;
2579 rc = -ENOMEM;
2583 strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias));
2585 /* Use provided chip-select for ancillary device */
2590 ancillary->max_speed_hz = spi->max_speed_hz;
2591 ancillary->mode = spi->mode;
2593 * By default spi->chip_select[0] will hold the physical CS number,
2594 * so set bit 0 in spi->cs_index_mask.
2596 ancillary->cs_index_mask = BIT(0);
2598 WARN_ON(!mutex_is_locked(&ctlr->add_lock));
2603 dev_err(&spi->dev, "failed to register ancillary device\n");
2632 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2635 sb = &ares->data.spi_serial_bus;
2636 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI)
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)
2677 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
2680 && obj->buffer.length == 8)
2681 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
2684 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
2685 lookup->mode |= SPI_LSB_FIRST;
2688 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
2689 lookup->mode |= SPI_CPOL;
2692 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
2693 lookup->mode |= SPI_CPHA;
2699 struct spi_controller *ctlr = lookup->ctlr;
2701 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
2706 sb = &ares->data.spi_serial_bus;
2707 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
2709 if (lookup->index != -1 && lookup->n++ != lookup->index)
2713 sb->resource_source.string_ptr,
2717 return -ENODEV;
2720 if (!device_match_acpi_handle(ctlr->dev.parent, parent_handle))
2721 return -ENODEV;
2727 return -ENODEV;
2731 return -EPROBE_DEFER;
2733 lookup->ctlr = ctlr;
2740 * 0 .. max - 1 so we need to ask the driver to
2743 if (ctlr->fw_translate_cs) {
2744 int cs = ctlr->fw_translate_cs(ctlr,
2745 sb->device_selection);
2748 lookup->chip_select = cs;
2750 lookup->chip_select = sb->device_selection;
2753 lookup->max_speed_hz = sb->connection_speed;
2754 lookup->bits_per_word = sb->data_bit_length;
2756 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
2757 lookup->mode |= SPI_CPHA;
2758 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
2759 lookup->mode |= SPI_CPOL;
2760 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
2761 lookup->mode |= SPI_CS_HIGH;
2763 } else if (lookup->irq < 0) {
2767 lookup->irq = r.start;
2775 * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2785 * If index is set to -1, index is not used.
2786 * Note: If index is -1, ctlr must be set.
2800 if (!ctlr && index == -1)
2801 return ERR_PTR(-EINVAL);
2804 lookup.irq = -1;
2818 ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) &&
2819 device_match_acpi_handle(lookup.ctlr->dev.parent, parent_handle)) {
2825 return ERR_PTR(-ENODEV);
2829 dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n",
2830 dev_name(&adev->dev));
2831 return ERR_PTR(-ENOMEM);
2837 ACPI_COMPANION_SET(&spi->dev, adev);
2838 spi->max_speed_hz = lookup.max_speed_hz;
2839 spi->mode |= lookup.mode;
2840 spi->irq = lookup.irq;
2841 spi->bits_per_word = lookup.bits_per_word;
2843 * By default spi->chip_select[0] will hold the physical CS number,
2844 * so set bit 0 in spi->cs_index_mask.
2846 spi->cs_index_mask = BIT(0);
2857 if (acpi_bus_get_status(adev) || !adev->status.present ||
2861 spi = acpi_spi_device_alloc(ctlr, adev, -1);
2863 if (PTR_ERR(spi) == -ENOMEM)
2869 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
2870 sizeof(spi->modalias));
2872 if (spi->irq < 0)
2873 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
2877 adev->power.flags.ignore_parent = true;
2879 adev->power.flags.ignore_parent = false;
2880 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
2881 dev_name(&adev->dev));
2907 handle = ACPI_HANDLE(ctlr->dev.parent);
2915 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
2937 * spi_target_abort - abort the ongoing transfer request on an SPI slave
2943 struct spi_controller *ctlr = spi->controller;
2945 if (spi_controller_is_target(ctlr) && ctlr->target_abort)
2946 return ctlr->target_abort(ctlr);
2948 return -ENOTSUPP;
2959 child = device_find_any_child(&ctlr->dev);
2960 return sysfs_emit(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL);
2975 return -EINVAL;
2977 child = device_find_any_child(&ctlr->dev);
2988 return -ENOMEM;
2990 strscpy(spi->modalias, name, sizeof(spi->modalias));
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
3066 device_initialize(&ctlr->dev);
3067 INIT_LIST_HEAD(&ctlr->queue);
3068 spin_lock_init(&ctlr->queue_lock);
3069 spin_lock_init(&ctlr->bus_lock_spinlock);
3070 mutex_init(&ctlr->bus_lock_mutex);
3071 mutex_init(&ctlr->io_mutex);
3072 mutex_init(&ctlr->add_lock);
3073 ctlr->bus_num = -1;
3074 ctlr->num_chipselect = 1;
3075 ctlr->slave = slave;
3077 ctlr->dev.class = &spi_slave_class;
3079 ctlr->dev.class = &spi_master_class;
3080 ctlr->dev.parent = dev;
3081 pm_suspend_ignore_children(&ctlr->dev, true);
3094 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
3096 * @size: how much zeroed driver-private data to allocate
3121 ctlr->devm_allocated = true;
3133 * spi_get_gpio_descs() - grab chip select GPIOs for the master
3140 struct device *dev = &ctlr->dev;
3147 if (nb == -ENOENT)
3152 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
3154 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
3157 return -ENOMEM;
3158 ctlr->cs_gpiods = cs;
3162 * Most chipselects are active low, the inverted
3183 return -ENOMEM;
3189 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
3191 return -EINVAL;
3196 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
3198 if ((ctlr->flags & SPI_CONTROLLER_GPIO_SS) && num_cs_gpios &&
3199 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
3201 return -EINVAL;
3210 * The controller may implement only the high-level SPI-memory like
3213 * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least
3214 * one of the ->transfer_xxx() method be implemented.
3216 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
3217 if (!ctlr->transfer && !ctlr->transfer_one &&
3218 !ctlr->transfer_one_message) {
3219 return -EINVAL;
3235 return id == -ENOSPC ? -EBUSY : id;
3236 ctlr->bus_num = id;
3241 * spi_register_controller - register SPI master or slave controller
3246 * SPI controllers connect to their drivers using some non-SPI bus,
3251 * and board-specific addressing for SPI devices combines those numbers
3265 struct device *dev = ctlr->dev.parent;
3272 return -ENODEV;
3282 if (ctlr->bus_num < 0)
3283 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi");
3284 if (ctlr->bus_num >= 0) {
3285 /* Devices with a fixed bus num must check-in with the num */
3286 status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1);
3290 if (ctlr->bus_num < 0) {
3301 ctlr->bus_lock_flag = 0;
3302 init_completion(&ctlr->xfer_completion);
3303 init_completion(&ctlr->cur_msg_completion);
3304 if (!ctlr->max_dma_len)
3305 ctlr->max_dma_len = INT_MAX;
3311 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
3313 if (!spi_controller_is_target(ctlr) && ctlr->use_gpio_descriptors) {
3321 ctlr->mode_bits |= SPI_CS_HIGH;
3325 * Even if it's just one always-selected device, there must
3328 if (!ctlr->num_chipselect) {
3329 status = -EINVAL;
3335 ctlr->last_cs[idx] = SPI_INVALID_CS;
3337 status = device_add(&ctlr->dev);
3342 dev_name(&ctlr->dev));
3346 * need the queueing logic if the driver is only supporting high-level
3349 if (ctlr->transfer) {
3351 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
3354 device_del(&ctlr->dev);
3359 ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev);
3360 if (!ctlr->pcpu_statistics) {
3361 dev_err(dev, "Error allocating per-cpu statistics\n");
3362 status = -ENOMEM;
3367 list_add_tail(&ctlr->list, &spi_controller_list);
3369 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
3381 idr_remove(&spi_master_idr, ctlr->bus_num);
3393 * devm_spi_register_controller - register managed SPI master or slave
3413 return -ENOMEM;
3434 * spi_unregister_controller - unregister SPI master or slave controller
3448 int id = ctlr->bus_num;
3452 mutex_lock(&ctlr->add_lock);
3454 device_for_each_child(&ctlr->dev, NULL, __unregister);
3460 if (ctlr->queued) {
3462 dev_err(&ctlr->dev, "queue remove failed\n");
3465 list_del(&ctlr->list);
3468 device_del(&ctlr->dev);
3477 mutex_unlock(&ctlr->add_lock);
3483 if (!ctlr->devm_allocated)
3484 put_device(&ctlr->dev);
3490 return ctlr->flags & SPI_CONTROLLER_SUSPENDED ? -ESHUTDOWN : 0;
3495 mutex_lock(&ctlr->bus_lock_mutex);
3496 ctlr->flags |= SPI_CONTROLLER_SUSPENDED;
3497 mutex_unlock(&ctlr->bus_lock_mutex);
3502 mutex_lock(&ctlr->bus_lock_mutex);
3503 ctlr->flags &= ~SPI_CONTROLLER_SUSPENDED;
3504 mutex_unlock(&ctlr->bus_lock_mutex);
3511 /* Basically no-ops for non-queued controllers */
3512 if (ctlr->queued) {
3515 dev_err(&ctlr->dev, "queue stop failed\n");
3529 if (ctlr->queued) {
3532 dev_err(&ctlr->dev, "queue restart failed\n");
3538 /*-------------------------------------------------------------------------*/
3550 if (rxfer->release)
3551 rxfer->release(ctlr, msg, res);
3554 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
3557 for (i = 0; i < rxfer->inserted; i++)
3558 list_del(&rxfer->inserted_transfers[i].transfer_list);
3562 * spi_replace_transfers - replace transfers with several transfers
3590 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
3595 return ERR_PTR(-ENOMEM);
3598 rxfer->release = release;
3602 rxfer->extradata =
3603 &rxfer->inserted_transfers[insert];
3606 INIT_LIST_HEAD(&rxfer->replaced_transfers);
3610 * the @replaced_transfers - it may be spi_message.messages!
3612 rxfer->replaced_after = xfer_first->transfer_list.prev;
3617 * If the entry after replaced_after it is msg->transfers
3621 if (rxfer->replaced_after->next == &msg->transfers) {
3622 dev_err(&msg->spi->dev,
3625 list_splice(&rxfer->replaced_transfers,
3626 rxfer->replaced_after);
3632 return ERR_PTR(-EINVAL);
3639 list_move_tail(rxfer->replaced_after->next,
3640 &rxfer->replaced_transfers);
3649 xfer = &rxfer->inserted_transfers[insert - 1 - i];
3655 list_add(&xfer->transfer_list, rxfer->replaced_after);
3659 xfer->cs_change = false;
3660 xfer->delay.value = 0;
3665 rxfer->inserted = insert;
3684 count = DIV_ROUND_UP(xfer->len, maxsize);
3690 xfers = srt->inserted_transfers;
3715 xfers[i].len = min(maxsize, xfers[i].len - offset);
3722 *xferp = &xfers[count - 1];
3725 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics,
3727 SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics,
3734 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3761 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3762 if (xfer->len > maxsize) {
3776 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3802 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3806 maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word));
3807 if (xfer->len > maxsize) {
3819 /*-------------------------------------------------------------------------*/
3829 if (ctlr->bits_per_word_mask) {
3832 return -EINVAL;
3833 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
3834 return -EINVAL;
3841 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3848 struct device *parent = spi->controller->dev.parent;
3851 if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) {
3852 if (spi->controller->auto_runtime_pm) {
3856 dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3861 status = spi->controller->set_cs_timing(spi);
3865 status = spi->controller->set_cs_timing(spi);
3872 * spi_setup - setup SPI mode and clock rate
3887 * LSB-first wire encoding, or active-high chipselects.
3900 if ((hweight_long(spi->mode &
3902 (hweight_long(spi->mode &
3904 dev_err(&spi->dev,
3905 "setup: can not select any two of dual, quad and no-rx/tx at the same time\n");
3906 return -EINVAL;
3909 if ((spi->mode & SPI_3WIRE) && (spi->mode &
3912 return -EINVAL;
3914 if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) {
3915 dev_err(&spi->dev,
3917 return -EINVAL;
3925 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
3931 dev_warn(&spi->dev,
3934 spi->mode &= ~ugly_bits;
3938 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
3940 return -EINVAL;
3943 if (!spi->bits_per_word) {
3944 spi->bits_per_word = 8;
3947 * Some controllers may not support the default 8 bits-per-word
3950 status = __spi_validate_bits_per_word(spi->controller,
3951 spi->bits_per_word);
3956 if (spi->controller->max_speed_hz &&
3957 (!spi->max_speed_hz ||
3958 spi->max_speed_hz > spi->controller->max_speed_hz))
3959 spi->max_speed_hz = spi->controller->max_speed_hz;
3961 mutex_lock(&spi->controller->io_mutex);
3963 if (spi->controller->setup) {
3964 status = spi->controller->setup(spi);
3966 mutex_unlock(&spi->controller->io_mutex);
3967 dev_err(&spi->controller->dev, "Failed to setup device: %d\n",
3975 mutex_unlock(&spi->controller->io_mutex);
3979 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3980 status = pm_runtime_resume_and_get(spi->controller->dev.parent);
3982 mutex_unlock(&spi->controller->io_mutex);
3983 dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3991 * checking for a non-zero return value instead of a negative
3997 pm_runtime_mark_last_busy(spi->controller->dev.parent);
3998 pm_runtime_put_autosuspend(spi->controller->dev.parent);
4003 mutex_unlock(&spi->controller->io_mutex);
4005 if (spi->rt && !spi->controller->rt) {
4006 spi->controller->rt = true;
4007 spi_set_thread_rt(spi->controller);
4012 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
4013 spi->mode & SPI_MODE_X_MASK,
4014 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
4015 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
4016 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
4017 (spi->mode & SPI_LOOP) ? "loopback, " : "",
4018 spi->bits_per_word, spi->max_speed_hz,
4030 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
4034 delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
4039 memcpy(&xfer->word_delay, &spi->word_delay,
4040 sizeof(xfer->word_delay));
4047 struct spi_controller *ctlr = spi->controller;
4051 if (list_empty(&message->transfers))
4052 return -EINVAL;
4054 message->spi = spi;
4057 * Half-duplex links include original MicroWire, and ones with
4062 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
4063 (spi->mode & SPI_3WIRE)) {
4064 unsigned flags = ctlr->flags;
4066 list_for_each_entry(xfer, &message->transfers, transfer_list) {
4067 if (xfer->rx_buf && xfer->tx_buf)
4068 return -EINVAL;
4069 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
4070 return -EINVAL;
4071 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
4072 return -EINVAL;
4084 message->frame_length = 0;
4085 list_for_each_entry(xfer, &message->transfers, transfer_list) {
4086 xfer->effective_speed_hz = 0;
4087 message->frame_length += xfer->len;
4088 if (!xfer->bits_per_word)
4089 xfer->bits_per_word = spi->bits_per_word;
4091 if (!xfer->speed_hz)
4092 xfer->speed_hz = spi->max_speed_hz;
4094 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
4095 xfer->speed_hz = ctlr->max_speed_hz;
4097 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
4098 return -EINVAL;
4102 * where SPI word size should be power-of-two multiple.
4104 if (xfer->bits_per_word <= 8)
4106 else if (xfer->bits_per_word <= 16)
4112 if (xfer->len % w_size)
4113 return -EINVAL;
4115 if (xfer->speed_hz && ctlr->min_speed_hz &&
4116 xfer->speed_hz < ctlr->min_speed_hz)
4117 return -EINVAL;
4119 if (xfer->tx_buf && !xfer->tx_nbits)
4120 xfer->tx_nbits = SPI_NBITS_SINGLE;
4121 if (xfer->rx_buf && !xfer->rx_nbits)
4122 xfer->rx_nbits = SPI_NBITS_SINGLE;
4124 * Check transfer tx/rx_nbits:
4126 * 2. check tx/rx_nbits match the mode in spi_device
4128 if (xfer->tx_buf) {
4129 if (spi->mode & SPI_NO_TX)
4130 return -EINVAL;
4131 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
4132 xfer->tx_nbits != SPI_NBITS_DUAL &&
4133 xfer->tx_nbits != SPI_NBITS_QUAD &&
4134 xfer->tx_nbits != SPI_NBITS_OCTAL)
4135 return -EINVAL;
4136 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
4137 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
4138 return -EINVAL;
4139 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
4140 !(spi->mode & SPI_TX_QUAD))
4141 return -EINVAL;
4144 if (xfer->rx_buf) {
4145 if (spi->mode & SPI_NO_RX)
4146 return -EINVAL;
4147 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
4148 xfer->rx_nbits != SPI_NBITS_DUAL &&
4149 xfer->rx_nbits != SPI_NBITS_QUAD &&
4150 xfer->rx_nbits != SPI_NBITS_OCTAL)
4151 return -EINVAL;
4152 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
4153 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
4154 return -EINVAL;
4155 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
4156 !(spi->mode & SPI_RX_QUAD))
4157 return -EINVAL;
4161 return -EINVAL;
4164 message->status = -EINPROGRESS;
4170 * spi_split_transfers - generic handling of transfer splitting
4186 struct spi_controller *ctlr = msg->spi->controller;
4193 * for the CS line, we can emulate the CS-per-word hardware function by
4194 * splitting transfers into one-word transfers and ensuring that
4197 if ((msg->spi->mode & SPI_CS_WORD) &&
4198 (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) {
4203 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
4205 if (list_is_last(&xfer->transfer_list, &msg->transfers))
4208 xfer->cs_change = 1;
4212 spi_max_transfer_size(msg->spi));
4221 * __spi_optimize_message - shared implementation for spi_optimize_message()
4236 struct spi_controller *ctlr = spi->controller;
4247 if (ctlr->optimize_message) {
4248 ret = ctlr->optimize_message(msg);
4255 msg->optimized = true;
4261 * spi_maybe_optimize_message - optimize message if it isn't already pre-optimized
4269 if (spi->controller->defer_optimize_message) {
4270 msg->spi = spi;
4274 if (msg->pre_optimized)
4281 * spi_optimize_message - do any one-time validation and setup for a SPI message
4291 * of updating the contents of any xfer->tx_buf (the pointer can't be changed,
4305 * Pre-optimization is not supported and optimization is deferred e.g.
4306 * when using spi-mux.
4308 if (spi->controller->defer_optimize_message)
4321 msg->pre_optimized = true;
4328 * spi_unoptimize_message - releases any resources allocated by spi_optimize_message()
4337 if (msg->spi->controller->defer_optimize_message)
4341 msg->pre_optimized = false;
4347 struct spi_controller *ctlr = spi->controller;
4354 if (!ctlr->transfer)
4355 return -ENOTSUPP;
4357 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async);
4358 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async);
4362 if (!ctlr->ptp_sts_supported) {
4363 list_for_each_entry(xfer, &message->transfers, transfer_list) {
4364 xfer->ptp_sts_word_pre = 0;
4365 ptp_read_system_prets(xfer->ptp_sts);
4369 return ctlr->transfer(spi, message);
4378 * devm_spi_optimize_message - managed version of spi_optimize_message()
4379 * @dev: the device that manages @msg (usually @spi->dev)
4401 * spi_async - asynchronous SPI transfer
4410 * Before that invocation, the value of message->status is undefined.
4411 * When the callback is issued, message->status holds either zero (to
4433 struct spi_controller *ctlr = spi->controller;
4441 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4443 if (ctlr->bus_lock_flag)
4444 ret = -EBUSY;
4448 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4459 mutex_lock(&ctlr->io_mutex);
4461 was_busy = ctlr->busy;
4463 ctlr->cur_msg = msg;
4466 dev_err(&ctlr->dev, "noqueue transfer failed\n");
4467 ctlr->cur_msg = NULL;
4468 ctlr->fallback = false;
4471 kfree(ctlr->dummy_rx);
4472 ctlr->dummy_rx = NULL;
4473 kfree(ctlr->dummy_tx);
4474 ctlr->dummy_tx = NULL;
4475 if (ctlr->unprepare_transfer_hardware &&
4476 ctlr->unprepare_transfer_hardware(ctlr))
4477 dev_err(&ctlr->dev,
4482 mutex_unlock(&ctlr->io_mutex);
4485 /*-------------------------------------------------------------------------*/
4503 struct spi_controller *ctlr = spi->controller;
4506 dev_warn_once(&spi->dev, "Attempted to sync while suspend\n");
4507 return -ESHUTDOWN;
4514 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync);
4515 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync);
4523 if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) {
4524 message->actual_length = 0;
4525 message->status = -EINPROGRESS;
4529 SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate);
4530 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate);
4534 return message->status;
4543 message->complete = spi_complete;
4544 message->context = &done;
4546 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4548 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4552 status = message->status;
4554 message->complete = NULL;
4555 message->context = NULL;
4561 * spi_sync - blocking/synchronous SPI data transfers
4567 * is non-interruptible, and has no timeout. Low-overhead controller
4572 * frequently-used devices may want to minimize costs of selecting a chip,
4585 mutex_lock(&spi->controller->bus_lock_mutex);
4587 mutex_unlock(&spi->controller->bus_lock_mutex);
4594 * spi_sync_locked - version of spi_sync with exclusive bus usage
4600 * is non-interruptible, and has no timeout. Low-overhead controller
4616 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4621 * is non-interruptible, and has no timeout.
4634 mutex_lock(&ctlr->bus_lock_mutex);
4636 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4637 ctlr->bus_lock_flag = 1;
4638 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4647 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4652 * is non-interruptible, and has no timeout.
4661 ctlr->bus_lock_flag = 0;
4663 mutex_unlock(&ctlr->bus_lock_mutex);
4675 * spi_write_then_read - SPI synchronous write followed by read
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.
4706 * Use preallocated DMA-safe buffer if we can. We can't avoid
4709 * using the pre-allocated buffer or the transfer is too large.
4715 return -ENOMEM;
4749 /*-------------------------------------------------------------------------*/
4784 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
4788 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
4789 put_device(&ctlr->dev);
4797 rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
4798 spi = of_register_spi_device(ctlr, rd->dn);
4799 put_device(&ctlr->dev);
4803 __func__, rd->dn);
4804 of_node_clear_flag(rd->dn, OF_POPULATED);
4811 if (!of_node_check_flag(rd->dn, OF_POPULATED))
4815 spi = of_find_spi_device_by_node(rd->dn);
4823 put_device(&spi->dev);
4840 return ACPI_COMPANION(dev->parent) == data;
4881 put_device(&ctlr->dev);
4892 put_device(&spi->dev);
4912 status = -ENOMEM;