• Home
  • Raw
  • Download

Lines Matching +full:spi +full:- +full:controller

1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPI init/core code
11 #include <linux/dma-mapping.h>
16 #include <linux/clk/clk-conf.h>
19 #include <linux/spi/spi.h>
20 #include <linux/spi/spi-mem.h>
38 #include <trace/events/spi.h>
48 struct spi_device *spi = to_spi_device(dev); in spidev_release() local
50 spi_controller_put(spi->controller); in spidev_release()
51 kfree(spi->driver_override); in spidev_release()
52 kfree(spi); in spidev_release()
58 const struct spi_device *spi = to_spi_device(dev); in modalias_show() local
61 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); in modalias_show()
62 if (len != -ENODEV) in modalias_show()
65 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); in modalias_show()
73 struct spi_device *spi = to_spi_device(dev); in driver_override_store() local
75 const size_t len = end ? end - buf : count; in driver_override_store()
79 if (len >= (PAGE_SIZE - 1)) in driver_override_store()
80 return -EINVAL; in driver_override_store()
84 return -ENOMEM; in driver_override_store()
87 old = spi->driver_override; in driver_override_store()
89 spi->driver_override = driver_override; in driver_override_store()
92 spi->driver_override = NULL; in driver_override_store()
104 const struct spi_device *spi = to_spi_device(dev); in driver_override_show() local
108 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); in driver_override_show()
121 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
131 struct spi_device *spi = to_spi_device(dev); \
132 return spi_statistics_##field##_show(&spi->statistics, buf); \
145 spin_lock_irqsave(&stat->lock, flags); \
146 len = sprintf(buf, format_string, stat->field); \
147 spin_unlock_irqrestore(&stat->lock, flags); \
173 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
174 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
175 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
176 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
177 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
178 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
179 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
180 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
181 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
182 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
183 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
184 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
185 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
186 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
187 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
188 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
293 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; in spi_statistics_add_transfer_stats()
298 spin_lock_irqsave(&stats->lock, flags); in spi_statistics_add_transfer_stats()
300 stats->transfers++; in spi_statistics_add_transfer_stats()
301 stats->transfer_bytes_histo[l2len]++; in spi_statistics_add_transfer_stats()
303 stats->bytes += xfer->len; in spi_statistics_add_transfer_stats()
304 if ((xfer->tx_buf) && in spi_statistics_add_transfer_stats()
305 (xfer->tx_buf != ctlr->dummy_tx)) in spi_statistics_add_transfer_stats()
306 stats->bytes_tx += xfer->len; in spi_statistics_add_transfer_stats()
307 if ((xfer->rx_buf) && in spi_statistics_add_transfer_stats()
308 (xfer->rx_buf != ctlr->dummy_rx)) in spi_statistics_add_transfer_stats()
309 stats->bytes_rx += xfer->len; in spi_statistics_add_transfer_stats()
311 spin_unlock_irqrestore(&stats->lock, flags); in spi_statistics_add_transfer_stats()
315 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
322 while (id->name[0]) { in spi_match_id()
323 if (!strcmp(sdev->modalias, id->name)) in spi_match_id()
332 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); in spi_get_device_id()
334 return spi_match_id(sdrv->id_table, sdev); in spi_get_device_id()
340 const struct spi_device *spi = to_spi_device(dev); in spi_match_device() local
344 if (spi->driver_override) in spi_match_device()
345 return strcmp(spi->driver_override, drv->name) == 0; in spi_match_device()
355 if (sdrv->id_table) in spi_match_device()
356 return !!spi_match_id(sdrv->id_table, spi); in spi_match_device()
358 return strcmp(spi->modalias, drv->name) == 0; in spi_match_device()
363 const struct spi_device *spi = to_spi_device(dev); in spi_uevent() local
367 if (rc != -ENODEV) in spi_uevent()
370 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); in spi_uevent()
374 .name = "spi",
384 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_probe()
385 struct spi_device *spi = to_spi_device(dev); in spi_drv_probe() local
388 ret = of_clk_set_defaults(dev->of_node, false); in spi_drv_probe()
392 if (dev->of_node) { in spi_drv_probe()
393 spi->irq = of_irq_get(dev->of_node, 0); in spi_drv_probe()
394 if (spi->irq == -EPROBE_DEFER) in spi_drv_probe()
395 return -EPROBE_DEFER; in spi_drv_probe()
396 if (spi->irq < 0) in spi_drv_probe()
397 spi->irq = 0; in spi_drv_probe()
404 if (sdrv->probe) { in spi_drv_probe()
405 ret = sdrv->probe(spi); in spi_drv_probe()
415 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_remove()
418 if (sdrv->remove) in spi_drv_remove()
419 ret = sdrv->remove(to_spi_device(dev)); in spi_drv_remove()
427 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_shutdown()
429 sdrv->shutdown(to_spi_device(dev)); in spi_drv_shutdown()
433 * __spi_register_driver - register a SPI driver
442 sdrv->driver.owner = owner; in __spi_register_driver()
443 sdrv->driver.bus = &spi_bus_type; in __spi_register_driver()
444 sdrv->driver.probe = spi_drv_probe; in __spi_register_driver()
445 sdrv->driver.remove = spi_drv_remove; in __spi_register_driver()
446 if (sdrv->shutdown) in __spi_register_driver()
447 sdrv->driver.shutdown = spi_drv_shutdown; in __spi_register_driver()
448 return driver_register(&sdrv->driver); in __spi_register_driver()
452 /*-------------------------------------------------------------------------*/
454 /* SPI devices should normally not be created by SPI device drivers; that
455 * would make them board-specific. Similarly with SPI controller drivers.
456 * Device registration normally goes into like arch/.../mach.../board-YYY.c
476 * spi_alloc_device - Allocate a new SPI device
477 * @ctlr: Controller to which device is connected
486 * spi_device structure to add it to the SPI controller. If the caller
494 struct spi_device *spi; in spi_alloc_device() local
499 spi = kzalloc(sizeof(*spi), GFP_KERNEL); in spi_alloc_device()
500 if (!spi) { in spi_alloc_device()
505 spi->master = spi->controller = ctlr; in spi_alloc_device()
506 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
507 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
508 spi->dev.release = spidev_release; in spi_alloc_device()
509 spi->cs_gpio = -ENOENT; in spi_alloc_device()
510 spi->mode = ctlr->buswidth_override_bits; in spi_alloc_device()
512 spin_lock_init(&spi->statistics.lock); in spi_alloc_device()
514 device_initialize(&spi->dev); in spi_alloc_device()
515 return spi; in spi_alloc_device()
519 static void spi_dev_set_name(struct spi_device *spi) in spi_dev_set_name() argument
521 struct acpi_device *adev = ACPI_COMPANION(&spi->dev); in spi_dev_set_name()
524 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); in spi_dev_set_name()
528 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
529 spi->chip_select); in spi_dev_set_name()
534 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
537 if (spi->controller == new_spi->controller && in spi_dev_check()
538 spi->chip_select == new_spi->chip_select) in spi_dev_check()
539 return -EBUSY; in spi_dev_check()
543 static void spi_cleanup(struct spi_device *spi) in spi_cleanup() argument
545 if (spi->controller->cleanup) in spi_cleanup()
546 spi->controller->cleanup(spi); in spi_cleanup()
550 * spi_add_device - Add spi_device allocated with spi_alloc_device
551 * @spi: spi_device to register
554 * spi_alloc_device can be added onto the spi bus with this function.
558 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
560 struct spi_controller *ctlr = spi->controller; in spi_add_device()
561 struct device *dev = ctlr->dev.parent; in spi_add_device()
565 if (spi->chip_select >= ctlr->num_chipselect) { in spi_add_device()
566 dev_err(dev, "cs%d >= max %d\n", spi->chip_select, in spi_add_device()
567 ctlr->num_chipselect); in spi_add_device()
568 return -EINVAL; in spi_add_device()
572 spi_dev_set_name(spi); in spi_add_device()
578 mutex_lock(&ctlr->add_lock); in spi_add_device()
580 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in spi_add_device()
583 spi->chip_select); in spi_add_device()
587 /* Controller may unregister concurrently */ in spi_add_device()
589 !device_is_registered(&ctlr->dev)) { in spi_add_device()
590 status = -ENODEV; in spi_add_device()
595 if (ctlr->cs_gpiods) in spi_add_device()
596 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; in spi_add_device()
597 else if (ctlr->cs_gpios) in spi_add_device()
598 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; in spi_add_device()
604 status = spi_setup(spi); in spi_add_device()
607 dev_name(&spi->dev), status); in spi_add_device()
612 status = device_add(&spi->dev); in spi_add_device()
615 dev_name(&spi->dev), status); in spi_add_device()
616 spi_cleanup(spi); in spi_add_device()
618 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in spi_add_device()
622 mutex_unlock(&ctlr->add_lock); in spi_add_device()
628 * spi_new_device - instantiate one new SPI device
629 * @ctlr: Controller to which device is connected
630 * @chip: Describes the SPI device
634 * after board init creates the hard-wired devices. Some development
637 * driver could add devices (which it would learn about out-of-band).
647 /* NOTE: caller did any chip->bus_num checks necessary. in spi_new_device()
650 * error-or-pointer (not NULL-or-pointer), troubleshootability in spi_new_device()
658 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); in spi_new_device()
660 proxy->chip_select = chip->chip_select; in spi_new_device()
661 proxy->max_speed_hz = chip->max_speed_hz; in spi_new_device()
662 proxy->mode = chip->mode; in spi_new_device()
663 proxy->irq = chip->irq; in spi_new_device()
664 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); in spi_new_device()
665 proxy->dev.platform_data = (void *) chip->platform_data; in spi_new_device()
666 proxy->controller_data = chip->controller_data; in spi_new_device()
667 proxy->controller_state = NULL; in spi_new_device()
669 if (chip->properties) { in spi_new_device()
670 status = device_add_properties(&proxy->dev, chip->properties); in spi_new_device()
672 dev_err(&ctlr->dev, in spi_new_device()
674 chip->modalias, status); in spi_new_device()
686 if (chip->properties) in spi_new_device()
687 device_remove_properties(&proxy->dev); in spi_new_device()
695 * spi_unregister_device - unregister a single SPI device
696 * @spi: spi_device to unregister
698 * Start making the passed SPI device vanish. Normally this would be handled
701 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
703 if (!spi) in spi_unregister_device()
706 if (spi->dev.of_node) { in spi_unregister_device()
707 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
708 of_node_put(spi->dev.of_node); in spi_unregister_device()
710 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
711 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
712 device_del(&spi->dev); in spi_unregister_device()
713 spi_cleanup(spi); in spi_unregister_device()
714 put_device(&spi->dev); in spi_unregister_device()
723 if (ctlr->bus_num != bi->bus_num) in spi_match_controller_to_boardinfo()
728 dev_err(ctlr->dev.parent, "can't create new device for %s\n", in spi_match_controller_to_boardinfo()
729 bi->modalias); in spi_match_controller_to_boardinfo()
733 * spi_register_board_info - register SPI devices for a given board
738 * Board-specific early init code calls this (probably during arch_initcall)
739 * with segments of the SPI device table. Any device nodes are created later,
740 * after the relevant parent SPI controller (bus_num) is defined. We keep
741 * this table of devices forever, so that reloading a controller driver will
742 * not make Linux forget about these hard-wired devices.
744 * Other code can also call this, e.g. a particular add-on board might provide
745 * SPI devices through its expansion connector, so code initializing that board
746 * would naturally declare its SPI devices.
749 * any embedded pointers (platform_data, etc), they're copied as-is.
750 * Device properties are deep-copied though.
764 return -ENOMEM; in spi_register_board_info()
769 memcpy(&bi->board_info, info, sizeof(*info)); in spi_register_board_info()
770 if (info->properties) { in spi_register_board_info()
771 bi->board_info.properties = in spi_register_board_info()
772 property_entries_dup(info->properties); in spi_register_board_info()
773 if (IS_ERR(bi->board_info.properties)) in spi_register_board_info()
774 return PTR_ERR(bi->board_info.properties); in spi_register_board_info()
778 list_add_tail(&bi->list, &board_list); in spi_register_board_info()
781 &bi->board_info); in spi_register_board_info()
788 /*-------------------------------------------------------------------------*/
790 static void spi_set_cs(struct spi_device *spi, bool enable, bool force) in spi_set_cs() argument
798 if (!force && (spi->controller->last_cs_enable == enable) && in spi_set_cs()
799 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
802 spi->controller->last_cs_enable = enable; in spi_set_cs()
803 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
805 if (!spi->controller->set_cs_timing) { in spi_set_cs()
807 spi_delay_exec(&spi->controller->cs_setup, NULL); in spi_set_cs()
809 spi_delay_exec(&spi->controller->cs_hold, NULL); in spi_set_cs()
812 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
815 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { in spi_set_cs()
816 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
817 if (spi->cs_gpiod) { in spi_set_cs()
820 * thus the SPISerialBus() resource defines it on the per-chip in spi_set_cs()
828 if (has_acpi_companion(&spi->dev)) in spi_set_cs()
829 gpiod_set_value_cansleep(spi->cs_gpiod, !enable); in spi_set_cs()
832 gpiod_set_value_cansleep(spi->cs_gpiod, enable1); in spi_set_cs()
836 * default for SPI. in spi_set_cs()
838 gpio_set_value_cansleep(spi->cs_gpio, !enable); in spi_set_cs()
841 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
842 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && in spi_set_cs()
843 spi->controller->set_cs) in spi_set_cs()
844 spi->controller->set_cs(spi, !enable); in spi_set_cs()
845 } else if (spi->controller->set_cs) { in spi_set_cs()
846 spi->controller->set_cs(spi, !enable); in spi_set_cs()
849 if (!spi->controller->set_cs_timing) { in spi_set_cs()
851 spi_delay_exec(&spi->controller->cs_inactive, NULL); in spi_set_cs()
881 desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len); in spi_map_buf()
884 return -EINVAL; in spi_map_buf()
891 sg = &sgt->sgl[0]; in spi_map_buf()
902 PAGE_SIZE - offset_in_page(buf))); in spi_map_buf()
909 return -ENOMEM; in spi_map_buf()
920 len -= min; in spi_map_buf()
924 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); in spi_map_buf()
926 ret = -ENOMEM; in spi_map_buf()
932 sgt->nents = ret; in spi_map_buf()
940 if (sgt->orig_nents) { in spi_unmap_buf()
941 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); in spi_unmap_buf()
943 sgt->orig_nents = 0; in spi_unmap_buf()
944 sgt->nents = 0; in spi_unmap_buf()
954 if (!ctlr->can_dma) in __spi_map_msg()
957 if (ctlr->dma_tx) in __spi_map_msg()
958 tx_dev = ctlr->dma_tx->device->dev; in __spi_map_msg()
960 tx_dev = ctlr->dev.parent; in __spi_map_msg()
962 if (ctlr->dma_rx) in __spi_map_msg()
963 rx_dev = ctlr->dma_rx->device->dev; in __spi_map_msg()
965 rx_dev = ctlr->dev.parent; in __spi_map_msg()
967 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_map_msg()
968 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
971 if (xfer->tx_buf != NULL) { in __spi_map_msg()
972 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
973 (void *)xfer->tx_buf, xfer->len, in __spi_map_msg()
979 if (xfer->rx_buf != NULL) { in __spi_map_msg()
980 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, in __spi_map_msg()
981 xfer->rx_buf, xfer->len, in __spi_map_msg()
984 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
991 ctlr->cur_msg_mapped = true; in __spi_map_msg()
1001 if (!ctlr->cur_msg_mapped || !ctlr->can_dma) in __spi_unmap_msg()
1004 if (ctlr->dma_tx) in __spi_unmap_msg()
1005 tx_dev = ctlr->dma_tx->device->dev; in __spi_unmap_msg()
1007 tx_dev = ctlr->dev.parent; in __spi_unmap_msg()
1009 if (ctlr->dma_rx) in __spi_unmap_msg()
1010 rx_dev = ctlr->dma_rx->device->dev; in __spi_unmap_msg()
1012 rx_dev = ctlr->dev.parent; in __spi_unmap_msg()
1014 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_unmap_msg()
1015 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_unmap_msg()
1018 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in __spi_unmap_msg()
1019 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in __spi_unmap_msg()
1022 ctlr->cur_msg_mapped = false; in __spi_unmap_msg()
1045 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_unmap_msg()
1050 if (xfer->tx_buf == ctlr->dummy_tx) in spi_unmap_msg()
1051 xfer->tx_buf = NULL; in spi_unmap_msg()
1052 if (xfer->rx_buf == ctlr->dummy_rx) in spi_unmap_msg()
1053 xfer->rx_buf = NULL; in spi_unmap_msg()
1065 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) in spi_map_msg()
1066 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1070 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_map_msg()
1071 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && in spi_map_msg()
1072 !xfer->tx_buf) in spi_map_msg()
1073 max_tx = max(xfer->len, max_tx); in spi_map_msg()
1074 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && in spi_map_msg()
1075 !xfer->rx_buf) in spi_map_msg()
1076 max_rx = max(xfer->len, max_rx); in spi_map_msg()
1080 tmp = krealloc(ctlr->dummy_tx, max_tx, in spi_map_msg()
1083 return -ENOMEM; in spi_map_msg()
1084 ctlr->dummy_tx = tmp; in spi_map_msg()
1089 tmp = krealloc(ctlr->dummy_rx, max_rx, in spi_map_msg()
1092 return -ENOMEM; in spi_map_msg()
1093 ctlr->dummy_rx = tmp; in spi_map_msg()
1097 list_for_each_entry(xfer, &msg->transfers, in spi_map_msg()
1099 if (!xfer->len) in spi_map_msg()
1101 if (!xfer->tx_buf) in spi_map_msg()
1102 xfer->tx_buf = ctlr->dummy_tx; in spi_map_msg()
1103 if (!xfer->rx_buf) in spi_map_msg()
1104 xfer->rx_buf = ctlr->dummy_rx; in spi_map_msg()
1116 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_wait()
1117 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_wait()
1118 u32 speed_hz = xfer->speed_hz; in spi_transfer_wait()
1122 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { in spi_transfer_wait()
1123 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1124 return -EINTR; in spi_transfer_wait()
1130 ms = 8LL * 1000LL * xfer->len; in spi_transfer_wait()
1137 ms = wait_for_completion_timeout(&ctlr->xfer_completion, in spi_transfer_wait()
1143 dev_err(&msg->spi->dev, in spi_transfer_wait()
1144 "SPI transfer timed out\n"); in spi_transfer_wait()
1145 return -ETIMEDOUT; in spi_transfer_wait()
1170 u32 delay = _delay->value; in spi_delay_to_ns()
1171 u32 unit = _delay->unit; in spi_delay_to_ns()
1186 return -EINVAL; in spi_delay_to_ns()
1190 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; in spi_delay_to_ns()
1192 return -EINVAL; in spi_delay_to_ns()
1196 return -EINVAL; in spi_delay_to_ns()
1210 return -EINVAL; in spi_delay_exec()
1225 u32 delay = xfer->cs_change_delay.value; in _spi_transfer_cs_change_delay()
1226 u32 unit = xfer->cs_change_delay.unit; in _spi_transfer_cs_change_delay()
1229 /* return early on "fast" mode - for everything but USECS */ in _spi_transfer_cs_change_delay()
1236 ret = spi_delay_exec(&xfer->cs_change_delay, xfer); in _spi_transfer_cs_change_delay()
1238 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1246 * spi_transfer_one_message - Default implementation of transfer_one_message()
1258 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_one_message()
1259 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_one_message()
1261 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1266 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_transfer_one_message()
1272 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1273 xfer->ptp_sts_word_pre = 0; in spi_transfer_one_message()
1274 ptp_read_system_prets(xfer->ptp_sts); in spi_transfer_one_message()
1277 if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { in spi_transfer_one_message()
1278 reinit_completion(&ctlr->xfer_completion); in spi_transfer_one_message()
1281 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1283 if (ctlr->cur_msg_mapped && in spi_transfer_one_message()
1284 (xfer->error & SPI_TRANS_FAIL_NO_START)) { in spi_transfer_one_message()
1286 ctlr->fallback = true; in spi_transfer_one_message()
1287 xfer->error &= ~SPI_TRANS_FAIL_NO_START; in spi_transfer_one_message()
1295 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1296 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1303 msg->status = ret; in spi_transfer_one_message()
1306 if (xfer->len) in spi_transfer_one_message()
1307 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1309 xfer->len); in spi_transfer_one_message()
1312 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1313 ptp_read_system_postts(xfer->ptp_sts); in spi_transfer_one_message()
1314 xfer->ptp_sts_word_post = xfer->len; in spi_transfer_one_message()
1319 if (msg->status != -EINPROGRESS) in spi_transfer_one_message()
1324 if (xfer->cs_change) { in spi_transfer_one_message()
1325 if (list_is_last(&xfer->transfer_list, in spi_transfer_one_message()
1326 &msg->transfers)) { in spi_transfer_one_message()
1329 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1331 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1335 msg->actual_length += xfer->len; in spi_transfer_one_message()
1340 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1342 if (msg->status == -EINPROGRESS) in spi_transfer_one_message()
1343 msg->status = ret; in spi_transfer_one_message()
1345 if (msg->status && ctlr->handle_err) in spi_transfer_one_message()
1346 ctlr->handle_err(ctlr, msg); in spi_transfer_one_message()
1354 * spi_finalize_current_transfer - report completion of a transfer
1355 * @ctlr: the controller reporting completion
1357 * Called by SPI drivers using the core transfer_one_message()
1363 complete(&ctlr->xfer_completion); in spi_finalize_current_transfer()
1369 if (ctlr->auto_runtime_pm) { in spi_idle_runtime_pm()
1370 pm_runtime_mark_last_busy(ctlr->dev.parent); in spi_idle_runtime_pm()
1371 pm_runtime_put_autosuspend(ctlr->dev.parent); in spi_idle_runtime_pm()
1376 * __spi_pump_messages - function which processes spi message queue
1377 * @ctlr: controller to process queue for
1380 * This function checks if there is any spi message in the queue that
1397 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1400 if (ctlr->cur_msg) { in __spi_pump_messages()
1401 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1406 if (ctlr->idling) { in __spi_pump_messages()
1407 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_pump_messages()
1408 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1413 if (list_empty(&ctlr->queue) || !ctlr->running) { in __spi_pump_messages()
1414 if (!ctlr->busy) { in __spi_pump_messages()
1415 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1419 /* Defer any non-atomic teardown to the thread */ in __spi_pump_messages()
1421 if (!ctlr->dummy_rx && !ctlr->dummy_tx && in __spi_pump_messages()
1422 !ctlr->unprepare_transfer_hardware) { in __spi_pump_messages()
1424 ctlr->busy = false; in __spi_pump_messages()
1427 kthread_queue_work(ctlr->kworker, in __spi_pump_messages()
1428 &ctlr->pump_messages); in __spi_pump_messages()
1430 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1434 ctlr->busy = false; in __spi_pump_messages()
1435 ctlr->idling = true; in __spi_pump_messages()
1436 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1438 kfree(ctlr->dummy_rx); in __spi_pump_messages()
1439 ctlr->dummy_rx = NULL; in __spi_pump_messages()
1440 kfree(ctlr->dummy_tx); in __spi_pump_messages()
1441 ctlr->dummy_tx = NULL; in __spi_pump_messages()
1442 if (ctlr->unprepare_transfer_hardware && in __spi_pump_messages()
1443 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_pump_messages()
1444 dev_err(&ctlr->dev, in __spi_pump_messages()
1449 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1450 ctlr->idling = false; in __spi_pump_messages()
1451 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1456 msg = list_first_entry(&ctlr->queue, struct spi_message, queue); in __spi_pump_messages()
1457 ctlr->cur_msg = msg; in __spi_pump_messages()
1459 list_del_init(&msg->queue); in __spi_pump_messages()
1460 if (ctlr->busy) in __spi_pump_messages()
1463 ctlr->busy = true; in __spi_pump_messages()
1464 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1466 mutex_lock(&ctlr->io_mutex); in __spi_pump_messages()
1468 if (!was_busy && ctlr->auto_runtime_pm) { in __spi_pump_messages()
1469 ret = pm_runtime_get_sync(ctlr->dev.parent); in __spi_pump_messages()
1471 pm_runtime_put_noidle(ctlr->dev.parent); in __spi_pump_messages()
1472 dev_err(&ctlr->dev, "Failed to power device: %d\n", in __spi_pump_messages()
1474 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1482 if (!was_busy && ctlr->prepare_transfer_hardware) { in __spi_pump_messages()
1483 ret = ctlr->prepare_transfer_hardware(ctlr); in __spi_pump_messages()
1485 dev_err(&ctlr->dev, in __spi_pump_messages()
1489 if (ctlr->auto_runtime_pm) in __spi_pump_messages()
1490 pm_runtime_put(ctlr->dev.parent); in __spi_pump_messages()
1492 msg->status = ret; in __spi_pump_messages()
1495 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1502 if (ctlr->prepare_message) { in __spi_pump_messages()
1503 ret = ctlr->prepare_message(ctlr, msg); in __spi_pump_messages()
1505 dev_err(&ctlr->dev, "failed to prepare message: %d\n", in __spi_pump_messages()
1507 msg->status = ret; in __spi_pump_messages()
1511 ctlr->cur_msg_prepared = true; in __spi_pump_messages()
1516 msg->status = ret; in __spi_pump_messages()
1521 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in __spi_pump_messages()
1522 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_pump_messages()
1523 xfer->ptp_sts_word_pre = 0; in __spi_pump_messages()
1524 ptp_read_system_prets(xfer->ptp_sts); in __spi_pump_messages()
1528 ret = ctlr->transfer_one_message(ctlr, msg); in __spi_pump_messages()
1530 dev_err(&ctlr->dev, in __spi_pump_messages()
1536 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1544 * spi_pump_messages - kthread work function which processes spi message queue
1545 * @work: pointer to kthread work struct contained in the controller struct
1556 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1557 * TX timestamp for the requested byte from the SPI
1580 if (!xfer->ptp_sts) in spi_take_timestamp_pre()
1583 if (xfer->timestamped) in spi_take_timestamp_pre()
1586 if (progress > xfer->ptp_sts_word_pre) in spi_take_timestamp_pre()
1590 xfer->ptp_sts_word_pre = progress; in spi_take_timestamp_pre()
1593 local_irq_save(ctlr->irq_flags); in spi_take_timestamp_pre()
1597 ptp_read_system_prets(xfer->ptp_sts); in spi_take_timestamp_pre()
1602 * spi_take_timestamp_post - helper for drivers to collect the end of the
1603 * TX timestamp for the requested byte from the SPI
1611 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1617 if (!xfer->ptp_sts) in spi_take_timestamp_post()
1620 if (xfer->timestamped) in spi_take_timestamp_post()
1623 if (progress < xfer->ptp_sts_word_post) in spi_take_timestamp_post()
1626 ptp_read_system_postts(xfer->ptp_sts); in spi_take_timestamp_post()
1629 local_irq_restore(ctlr->irq_flags); in spi_take_timestamp_post()
1634 xfer->ptp_sts_word_post = progress; in spi_take_timestamp_post()
1636 xfer->timestamped = true; in spi_take_timestamp_post()
1641 * spi_set_thread_rt - set the controller to pump at realtime priority
1642 * @ctlr: controller to boost priority of
1644 * This can be called because the controller requested realtime priority
1645 * (by setting the ->rt value before calling spi_register_controller()) or
1651 * controller. If this eventually becomes a problem we may see if we can
1657 dev_info(&ctlr->dev, in spi_set_thread_rt()
1659 sched_set_fifo(ctlr->kworker->task); in spi_set_thread_rt()
1664 ctlr->running = false; in spi_init_queue()
1665 ctlr->busy = false; in spi_init_queue()
1667 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); in spi_init_queue()
1668 if (IS_ERR(ctlr->kworker)) { in spi_init_queue()
1669 dev_err(&ctlr->dev, "failed to create message pump kworker\n"); in spi_init_queue()
1670 return PTR_ERR(ctlr->kworker); in spi_init_queue()
1673 kthread_init_work(&ctlr->pump_messages, spi_pump_messages); in spi_init_queue()
1676 * Controller config will indicate if this controller should run the in spi_init_queue()
1682 if (ctlr->rt) in spi_init_queue()
1689 * spi_get_next_queued_message() - called by driver to check for queued
1691 * @ctlr: the controller to check for queued messages
1704 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1705 next = list_first_entry_or_null(&ctlr->queue, struct spi_message, in spi_get_next_queued_message()
1707 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1714 * spi_finalize_current_message() - the current message is complete
1715 * @ctlr: the controller to return the message to
1727 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1728 mesg = ctlr->cur_msg; in spi_finalize_current_message()
1729 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1731 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in spi_finalize_current_message()
1732 list_for_each_entry(xfer, &mesg->transfers, transfer_list) { in spi_finalize_current_message()
1733 ptp_read_system_postts(xfer->ptp_sts); in spi_finalize_current_message()
1734 xfer->ptp_sts_word_post = xfer->len; in spi_finalize_current_message()
1738 if (unlikely(ctlr->ptp_sts_supported)) in spi_finalize_current_message()
1739 list_for_each_entry(xfer, &mesg->transfers, transfer_list) in spi_finalize_current_message()
1740 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); in spi_finalize_current_message()
1744 /* In the prepare_messages callback the spi bus has the opportunity to in spi_finalize_current_message()
1751 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { in spi_finalize_current_message()
1752 ret = ctlr->unprepare_message(ctlr, mesg); in spi_finalize_current_message()
1754 dev_err(&ctlr->dev, "failed to unprepare message: %d\n", in spi_finalize_current_message()
1759 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1760 ctlr->cur_msg = NULL; in spi_finalize_current_message()
1761 ctlr->cur_msg_prepared = false; in spi_finalize_current_message()
1762 ctlr->fallback = false; in spi_finalize_current_message()
1763 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_finalize_current_message()
1764 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1768 mesg->state = NULL; in spi_finalize_current_message()
1769 if (mesg->complete) in spi_finalize_current_message()
1770 mesg->complete(mesg->context); in spi_finalize_current_message()
1778 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_start_queue()
1780 if (ctlr->running || ctlr->busy) { in spi_start_queue()
1781 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1782 return -EBUSY; in spi_start_queue()
1785 ctlr->running = true; in spi_start_queue()
1786 ctlr->cur_msg = NULL; in spi_start_queue()
1787 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1789 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_start_queue()
1800 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1804 * A wait_queue on the ctlr->busy could be used, but then the common in spi_stop_queue()
1806 * friends on every SPI message. Do this instead. in spi_stop_queue()
1808 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { in spi_stop_queue()
1809 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1811 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1814 if (!list_empty(&ctlr->queue) || ctlr->busy) in spi_stop_queue()
1815 ret = -EBUSY; in spi_stop_queue()
1817 ctlr->running = false; in spi_stop_queue()
1819 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1822 dev_warn(&ctlr->dev, "could not stop message queue\n"); in spi_stop_queue()
1841 dev_err(&ctlr->dev, "problem destroying queue\n"); in spi_destroy_queue()
1845 kthread_destroy_worker(ctlr->kworker); in spi_destroy_queue()
1850 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
1854 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
1857 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1859 if (!ctlr->running) { in __spi_queued_transfer()
1860 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1861 return -ESHUTDOWN; in __spi_queued_transfer()
1863 msg->actual_length = 0; in __spi_queued_transfer()
1864 msg->status = -EINPROGRESS; in __spi_queued_transfer()
1866 list_add_tail(&msg->queue, &ctlr->queue); in __spi_queued_transfer()
1867 if (!ctlr->busy && need_pump) in __spi_queued_transfer()
1868 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_queued_transfer()
1870 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1875 * spi_queued_transfer - transfer function for queued transfers
1876 * @spi: spi device which is requesting transfer
1877 * @msg: spi message which is to handled is queued to driver queue
1881 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
1883 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
1890 ctlr->transfer = spi_queued_transfer; in spi_controller_initialize_queue()
1891 if (!ctlr->transfer_one_message) in spi_controller_initialize_queue()
1892 ctlr->transfer_one_message = spi_transfer_one_message; in spi_controller_initialize_queue()
1897 dev_err(&ctlr->dev, "problem initializing queue\n"); in spi_controller_initialize_queue()
1900 ctlr->queued = true; in spi_controller_initialize_queue()
1903 dev_err(&ctlr->dev, "problem starting queue\n"); in spi_controller_initialize_queue()
1916 * spi_flush_queue - Send all pending messages in the queue from the callers'
1918 * @ctlr: controller to process queue for
1921 * sent before doing something. Is used by the spi-mem code to make sure SPI
1922 * memory operations do not preempt regular SPI transfers that have been queued
1923 * before the spi-mem operation.
1927 if (ctlr->transfer == spi_queued_transfer) in spi_flush_queue()
1931 /*-------------------------------------------------------------------------*/
1934 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
1941 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
1942 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
1943 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
1944 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
1945 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
1946 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
1947 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
1948 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
1949 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
1950 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
1953 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
1958 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
1961 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
1964 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
1967 dev_warn(&ctlr->dev, in of_spi_parse_dt()
1968 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
1974 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
1979 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
1982 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
1985 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
1988 dev_warn(&ctlr->dev, in of_spi_parse_dt()
1989 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
1997 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", in of_spi_parse_dt()
1999 return -EINVAL; in of_spi_parse_dt()
2007 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", in of_spi_parse_dt()
2011 spi->chip_select = value; in of_spi_parse_dt()
2014 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2015 spi->max_speed_hz = value; in of_spi_parse_dt()
2023 struct spi_device *spi; in of_register_spi_device() local
2027 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2028 if (!spi) { in of_register_spi_device()
2029 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); in of_register_spi_device()
2030 rc = -ENOMEM; in of_register_spi_device()
2035 rc = of_modalias_node(nc, spi->modalias, in of_register_spi_device()
2036 sizeof(spi->modalias)); in of_register_spi_device()
2038 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); in of_register_spi_device()
2042 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2048 spi->dev.of_node = nc; in of_register_spi_device()
2049 spi->dev.fwnode = of_fwnode_handle(nc); in of_register_spi_device()
2052 rc = spi_add_device(spi); in of_register_spi_device()
2054 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); in of_register_spi_device()
2058 return spi; in of_register_spi_device()
2063 spi_dev_put(spi); in of_register_spi_device()
2068 * of_register_spi_devices() - Register child devices onto the SPI bus
2071 * Registers an spi_device for each child node of controller node which
2072 * represents a valid SPI slave.
2076 struct spi_device *spi; in of_register_spi_devices() local
2079 if (!ctlr->dev.of_node) in of_register_spi_devices()
2082 for_each_available_child_of_node(ctlr->dev.of_node, nc) { in of_register_spi_devices()
2085 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2086 if (IS_ERR(spi)) { in of_register_spi_devices()
2087 dev_warn(&ctlr->dev, in of_register_spi_devices()
2088 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2116 && obj->buffer.length >= 4) in acpi_spi_parse_apple_properties()
2117 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2120 && obj->buffer.length == 8) in acpi_spi_parse_apple_properties()
2121 lookup->bits_per_word = *(u64 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2124 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2125 lookup->mode |= SPI_LSB_FIRST; in acpi_spi_parse_apple_properties()
2128 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2129 lookup->mode |= SPI_CPOL; in acpi_spi_parse_apple_properties()
2132 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2133 lookup->mode |= SPI_CPHA; in acpi_spi_parse_apple_properties()
2139 struct spi_controller *ctlr = lookup->ctlr; in acpi_spi_add_resource()
2141 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { in acpi_spi_add_resource()
2146 sb = &ares->data.spi_serial_bus; in acpi_spi_add_resource()
2147 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { in acpi_spi_add_resource()
2150 sb->resource_source.string_ptr, in acpi_spi_add_resource()
2154 ACPI_HANDLE(ctlr->dev.parent) != parent_handle) in acpi_spi_add_resource()
2155 return -ENODEV; in acpi_spi_add_resource()
2159 * host controller driver in Windows and can vary in acpi_spi_add_resource()
2161 * 0 .. max - 1 so we need to ask the driver to in acpi_spi_add_resource()
2164 if (ctlr->fw_translate_cs) { in acpi_spi_add_resource()
2165 int cs = ctlr->fw_translate_cs(ctlr, in acpi_spi_add_resource()
2166 sb->device_selection); in acpi_spi_add_resource()
2169 lookup->chip_select = cs; in acpi_spi_add_resource()
2171 lookup->chip_select = sb->device_selection; in acpi_spi_add_resource()
2174 lookup->max_speed_hz = sb->connection_speed; in acpi_spi_add_resource()
2175 lookup->bits_per_word = sb->data_bit_length; in acpi_spi_add_resource()
2177 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) in acpi_spi_add_resource()
2178 lookup->mode |= SPI_CPHA; in acpi_spi_add_resource()
2179 if (sb->clock_polarity == ACPI_SPI_START_HIGH) in acpi_spi_add_resource()
2180 lookup->mode |= SPI_CPOL; in acpi_spi_add_resource()
2181 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) in acpi_spi_add_resource()
2182 lookup->mode |= SPI_CS_HIGH; in acpi_spi_add_resource()
2184 } else if (lookup->irq < 0) { in acpi_spi_add_resource()
2188 lookup->irq = r.start; in acpi_spi_add_resource()
2201 struct spi_device *spi; in acpi_register_spi_device() local
2204 if (acpi_bus_get_status(adev) || !adev->status.present || in acpi_register_spi_device()
2209 lookup.irq = -1; in acpi_register_spi_device()
2217 /* found SPI in _CRS but it points to another controller */ in acpi_register_spi_device()
2221 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) && in acpi_register_spi_device()
2222 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { in acpi_register_spi_device()
2223 /* Apple does not use _CRS but nested devices for SPI slaves */ in acpi_register_spi_device()
2230 spi = spi_alloc_device(ctlr); in acpi_register_spi_device()
2231 if (!spi) { in acpi_register_spi_device()
2232 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_register_spi_device()
2233 dev_name(&adev->dev)); in acpi_register_spi_device()
2238 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_register_spi_device()
2239 spi->max_speed_hz = lookup.max_speed_hz; in acpi_register_spi_device()
2240 spi->mode |= lookup.mode; in acpi_register_spi_device()
2241 spi->irq = lookup.irq; in acpi_register_spi_device()
2242 spi->bits_per_word = lookup.bits_per_word; in acpi_register_spi_device()
2243 spi->chip_select = lookup.chip_select; in acpi_register_spi_device()
2245 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2246 sizeof(spi->modalias)); in acpi_register_spi_device()
2248 if (spi->irq < 0) in acpi_register_spi_device()
2249 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in acpi_register_spi_device()
2253 adev->power.flags.ignore_parent = true; in acpi_register_spi_device()
2254 if (spi_add_device(spi)) { in acpi_register_spi_device()
2255 adev->power.flags.ignore_parent = false; in acpi_register_spi_device()
2256 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2257 dev_name(&adev->dev)); in acpi_register_spi_device()
2258 spi_dev_put(spi); in acpi_register_spi_device()
2283 handle = ACPI_HANDLE(ctlr->dev.parent); in acpi_register_spi_devices()
2291 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2314 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2315 * controller
2316 * @spi: device used for the current transfer
2318 int spi_slave_abort(struct spi_device *spi) in spi_slave_abort() argument
2320 struct spi_controller *ctlr = spi->controller; in spi_slave_abort()
2322 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) in spi_slave_abort()
2323 return ctlr->slave_abort(ctlr); in spi_slave_abort()
2325 return -ENOTSUPP; in spi_slave_abort()
2341 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_show()
2343 child ? to_spi_device(child)->modalias : NULL); in slave_show()
2351 struct spi_device *spi; in slave_store() local
2358 return -EINVAL; in slave_store()
2360 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_store()
2369 spi = spi_alloc_device(ctlr); in slave_store()
2370 if (!spi) in slave_store()
2371 return -ENOMEM; in slave_store()
2373 strlcpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2375 rc = spi_add_device(spi); in slave_store()
2377 spi_dev_put(spi); in slave_store()
2413 * __spi_alloc_controller - allocate an SPI master or slave controller
2414 * @dev: the controller, possibly using the platform_bus
2415 * @size: how much zeroed driver-private data to allocate; the pointer to this
2420 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2421 * slave (true) controller
2424 * This call is used only by SPI controller drivers, which are the
2431 * controller's methods before calling spi_register_controller(); and (after
2435 * Return: the SPI controller structure on success, else NULL.
2450 device_initialize(&ctlr->dev); in __spi_alloc_controller()
2451 ctlr->bus_num = -1; in __spi_alloc_controller()
2452 ctlr->num_chipselect = 1; in __spi_alloc_controller()
2453 ctlr->slave = slave; in __spi_alloc_controller()
2455 ctlr->dev.class = &spi_slave_class; in __spi_alloc_controller()
2457 ctlr->dev.class = &spi_master_class; in __spi_alloc_controller()
2458 ctlr->dev.parent = dev; in __spi_alloc_controller()
2459 pm_suspend_ignore_children(&ctlr->dev, true); in __spi_alloc_controller()
2472 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
2473 * @dev: physical device of SPI controller
2474 * @size: how much zeroed driver-private data to allocate
2475 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
2478 * Allocate an SPI controller and automatically release a reference on it
2484 * Return: the SPI controller structure on success, else NULL.
2499 ctlr->devm_allocated = true; in __devm_spi_alloc_controller()
2514 struct device_node *np = ctlr->dev.of_node; in of_spi_get_gpio_numbers()
2519 nb = of_gpio_named_count(np, "cs-gpios"); in of_spi_get_gpio_numbers()
2520 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in of_spi_get_gpio_numbers()
2522 /* Return error only for an incorrectly formed cs-gpios property */ in of_spi_get_gpio_numbers()
2523 if (nb == 0 || nb == -ENOENT) in of_spi_get_gpio_numbers()
2528 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), in of_spi_get_gpio_numbers()
2530 ctlr->cs_gpios = cs; in of_spi_get_gpio_numbers()
2532 if (!ctlr->cs_gpios) in of_spi_get_gpio_numbers()
2533 return -ENOMEM; in of_spi_get_gpio_numbers()
2535 for (i = 0; i < ctlr->num_chipselect; i++) in of_spi_get_gpio_numbers()
2536 cs[i] = -ENOENT; in of_spi_get_gpio_numbers()
2539 cs[i] = of_get_named_gpio(np, "cs-gpios", i); in of_spi_get_gpio_numbers()
2551 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2552 * @ctlr: The SPI master to grab GPIO descriptors for
2558 struct device *dev = &ctlr->dev; in spi_get_gpio_descs()
2563 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in spi_get_gpio_descs()
2566 if (nb == 0 || nb == -ENOENT) in spi_get_gpio_descs()
2571 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), in spi_get_gpio_descs()
2574 return -ENOMEM; in spi_get_gpio_descs()
2575 ctlr->cs_gpiods = cs; in spi_get_gpio_descs()
2600 return -ENOMEM; in spi_get_gpio_descs()
2606 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2608 return -EINVAL; in spi_get_gpio_descs()
2613 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; in spi_get_gpio_descs()
2615 if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && in spi_get_gpio_descs()
2616 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2618 return -EINVAL; in spi_get_gpio_descs()
2627 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
2628 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
2630 * If ->mem_ops is NULL, we request that at least one of the in spi_controller_check_ops()
2631 * ->transfer_xxx() method be implemented. in spi_controller_check_ops()
2633 if (ctlr->mem_ops) { in spi_controller_check_ops()
2634 if (!ctlr->mem_ops->exec_op) in spi_controller_check_ops()
2635 return -EINVAL; in spi_controller_check_ops()
2636 } else if (!ctlr->transfer && !ctlr->transfer_one && in spi_controller_check_ops()
2637 !ctlr->transfer_one_message) { in spi_controller_check_ops()
2638 return -EINVAL; in spi_controller_check_ops()
2645 * spi_register_controller - register SPI master or slave controller
2650 * SPI controllers connect to their drivers using some non-SPI bus,
2652 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2654 * SPI controllers use board specific (often SOC specific) bus numbers,
2655 * and board-specific addressing for SPI devices combines those numbers
2656 * with chip select numbers. Since SPI does not directly support dynamic
2661 * success, else a negative error code (dropping the controller's refcount).
2669 struct device *dev = ctlr->dev.parent; in spi_register_controller()
2675 return -ENODEV; in spi_register_controller()
2679 * the SPI controller. in spi_register_controller()
2685 if (ctlr->bus_num >= 0) { in spi_register_controller()
2686 /* devices with a fixed bus num must check-in with the num */ in spi_register_controller()
2688 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2689 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2692 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2693 ctlr->bus_num = id; in spi_register_controller()
2694 } else if (ctlr->dev.of_node) { in spi_register_controller()
2696 id = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
2698 ctlr->bus_num = id; in spi_register_controller()
2700 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2701 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2704 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2707 if (ctlr->bus_num < 0) { in spi_register_controller()
2708 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
2720 ctlr->bus_num = id; in spi_register_controller()
2722 INIT_LIST_HEAD(&ctlr->queue); in spi_register_controller()
2723 spin_lock_init(&ctlr->queue_lock); in spi_register_controller()
2724 spin_lock_init(&ctlr->bus_lock_spinlock); in spi_register_controller()
2725 mutex_init(&ctlr->bus_lock_mutex); in spi_register_controller()
2726 mutex_init(&ctlr->io_mutex); in spi_register_controller()
2727 mutex_init(&ctlr->add_lock); in spi_register_controller()
2728 ctlr->bus_lock_flag = 0; in spi_register_controller()
2729 init_completion(&ctlr->xfer_completion); in spi_register_controller()
2730 if (!ctlr->max_dma_len) in spi_register_controller()
2731 ctlr->max_dma_len = INT_MAX; in spi_register_controller()
2736 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
2739 if (ctlr->use_gpio_descriptors) { in spi_register_controller()
2744 * A controller using GPIO descriptors always in spi_register_controller()
2747 ctlr->mode_bits |= SPI_CS_HIGH; in spi_register_controller()
2757 * Even if it's just one always-selected device, there must in spi_register_controller()
2760 if (!ctlr->num_chipselect) { in spi_register_controller()
2761 status = -EINVAL; in spi_register_controller()
2765 status = device_add(&ctlr->dev); in spi_register_controller()
2770 dev_name(&ctlr->dev)); in spi_register_controller()
2774 * need the queueing logic if the driver is only supporting high-level in spi_register_controller()
2777 if (ctlr->transfer) { in spi_register_controller()
2778 dev_info(dev, "controller is unqueued, this is deprecated\n"); in spi_register_controller()
2779 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { in spi_register_controller()
2782 device_del(&ctlr->dev); in spi_register_controller()
2787 spin_lock_init(&ctlr->statistics.lock); in spi_register_controller()
2790 list_add_tail(&ctlr->list, &spi_controller_list); in spi_register_controller()
2792 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); in spi_register_controller()
2802 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
2814 * devm_spi_register_controller - register managed SPI master or slave
2815 * controller
2816 * @dev: device managing SPI controller
2817 * @ctlr: initialized controller, originally from spi_alloc_master() or
2821 * Register a SPI device as with spi_register_controller() which will
2834 return -ENOMEM; in devm_spi_register_controller()
2855 * spi_unregister_controller - unregister SPI master or slave controller
2856 * @ctlr: the controller being unregistered
2859 * This call is used only by SPI controller drivers, which are the
2864 * Note that this function also drops a reference to the controller.
2869 int id = ctlr->bus_num; in spi_unregister_controller()
2873 mutex_lock(&ctlr->add_lock); in spi_unregister_controller()
2875 device_for_each_child(&ctlr->dev, NULL, __unregister); in spi_unregister_controller()
2877 /* First make sure that this controller was ever added */ in spi_unregister_controller()
2881 if (ctlr->queued) { in spi_unregister_controller()
2883 dev_err(&ctlr->dev, "queue remove failed\n"); in spi_unregister_controller()
2886 list_del(&ctlr->list); in spi_unregister_controller()
2889 device_del(&ctlr->dev); in spi_unregister_controller()
2891 /* Release the last reference on the controller if its driver in spi_unregister_controller()
2894 if (!ctlr->devm_allocated) in spi_unregister_controller()
2895 put_device(&ctlr->dev); in spi_unregister_controller()
2904 mutex_unlock(&ctlr->add_lock); in spi_unregister_controller()
2912 /* Basically no-ops for non-queued controllers */ in spi_controller_suspend()
2913 if (!ctlr->queued) in spi_controller_suspend()
2918 dev_err(&ctlr->dev, "queue stop failed\n"); in spi_controller_suspend()
2928 if (!ctlr->queued) in spi_controller_resume()
2933 dev_err(&ctlr->dev, "queue restart failed\n"); in spi_controller_resume()
2945 return ctlr->bus_num == *bus_num; in __spi_controller_match()
2949 * spi_busnum_to_master - look up master associated with bus_num
2958 * Return: the SPI master structure on success, else NULL.
2974 /*-------------------------------------------------------------------------*/
2976 /* Core methods for SPI resource management */
2979 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2982 * @spi: the spi device for which we allocate memory
2992 void *spi_res_alloc(struct spi_device *spi, in spi_res_alloc() argument
3002 INIT_LIST_HEAD(&sres->entry); in spi_res_alloc()
3003 sres->release = release; in spi_res_alloc()
3005 return sres->data; in spi_res_alloc()
3010 * spi_res_free - free an spi resource
3021 WARN_ON(!list_empty(&sres->entry)); in spi_res_free()
3027 * spi_res_add - add a spi_res to the spi_message
3028 * @message: the spi message
3035 WARN_ON(!list_empty(&sres->entry)); in spi_res_add()
3036 list_add_tail(&sres->entry, &message->resources); in spi_res_add()
3041 * spi_res_release - release all spi resources for this message
3049 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { in spi_res_release()
3050 if (res->release) in spi_res_release()
3051 res->release(ctlr, message, res->data); in spi_res_release()
3053 list_del(&res->entry); in spi_res_release()
3060 /*-------------------------------------------------------------------------*/
3072 if (rxfer->release) in __spi_replace_transfers_release()
3073 rxfer->release(ctlr, msg, res); in __spi_replace_transfers_release()
3076 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); in __spi_replace_transfers_release()
3079 for (i = 0; i < rxfer->inserted; i++) in __spi_replace_transfers_release()
3080 list_del(&rxfer->inserted_transfers[i].transfer_list); in __spi_replace_transfers_release()
3084 * spi_replace_transfers - replace transfers with several transfers
3112 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3117 return ERR_PTR(-ENOMEM); in spi_replace_transfers()
3120 rxfer->release = release; in spi_replace_transfers()
3124 rxfer->extradata = in spi_replace_transfers()
3125 &rxfer->inserted_transfers[insert]; in spi_replace_transfers()
3128 INIT_LIST_HEAD(&rxfer->replaced_transfers); in spi_replace_transfers()
3131 * the @replaced_transfers - it may be spi_message.messages! in spi_replace_transfers()
3133 rxfer->replaced_after = xfer_first->transfer_list.prev; in spi_replace_transfers()
3137 /* if the entry after replaced_after it is msg->transfers in spi_replace_transfers()
3141 if (rxfer->replaced_after->next == &msg->transfers) { in spi_replace_transfers()
3142 dev_err(&msg->spi->dev, in spi_replace_transfers()
3145 list_splice(&rxfer->replaced_transfers, in spi_replace_transfers()
3146 rxfer->replaced_after); in spi_replace_transfers()
3152 return ERR_PTR(-EINVAL); in spi_replace_transfers()
3158 list_move_tail(rxfer->replaced_after->next, in spi_replace_transfers()
3159 &rxfer->replaced_transfers); in spi_replace_transfers()
3167 xfer = &rxfer->inserted_transfers[insert - 1 - i]; in spi_replace_transfers()
3173 list_add(&xfer->transfer_list, rxfer->replaced_after); in spi_replace_transfers()
3177 xfer->cs_change = false; in spi_replace_transfers()
3178 xfer->delay_usecs = 0; in spi_replace_transfers()
3179 xfer->delay.value = 0; in spi_replace_transfers()
3184 rxfer->inserted = insert; in spi_replace_transfers()
3205 count = DIV_ROUND_UP(xfer->len, maxsize); in __spi_split_transfer_maxsize()
3211 xfers = srt->inserted_transfers; in __spi_split_transfer_maxsize()
3241 xfers[i].len = min(maxsize, xfers[i].len - offset); in __spi_split_transfer_maxsize()
3247 *xferp = &xfers[count - 1]; in __spi_split_transfer_maxsize()
3250 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_split_transfer_maxsize()
3252 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, in __spi_split_transfer_maxsize()
3259 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
3283 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxsize()
3284 if (xfer->len > maxsize) { in spi_split_transfers_maxsize()
3296 /*-------------------------------------------------------------------------*/
3298 /* Core methods for SPI controller protocol drivers. Some of the
3305 if (ctlr->bits_per_word_mask) { in __spi_validate_bits_per_word()
3308 return -EINVAL; in __spi_validate_bits_per_word()
3309 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) in __spi_validate_bits_per_word()
3310 return -EINVAL; in __spi_validate_bits_per_word()
3317 * spi_setup - setup SPI mode and clock rate
3318 * @spi: the device whose settings are being modified
3321 * SPI protocol drivers may need to update the transfer mode if the
3327 * or from it. When this function returns, the spi device is deselected.
3330 * that the underlying controller or its driver does not support. For
3332 * LSB-first wire encoding, or active-high chipselects.
3336 int spi_setup(struct spi_device *spi) in spi_setup() argument
3343 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || in spi_setup()
3344 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { in spi_setup()
3345 dev_err(&spi->dev, in spi_setup()
3347 return -EINVAL; in spi_setup()
3351 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3354 return -EINVAL; in spi_setup()
3356 * that aren't supported with their current controller in spi_setup()
3360 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); in spi_setup()
3361 /* nothing prevents from working with active-high CS in case if it in spi_setup()
3364 if (gpio_is_valid(spi->cs_gpio)) in spi_setup()
3370 dev_warn(&spi->dev, in spi_setup()
3373 spi->mode &= ~ugly_bits; in spi_setup()
3377 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3379 return -EINVAL; in spi_setup()
3382 if (!spi->bits_per_word) in spi_setup()
3383 spi->bits_per_word = 8; in spi_setup()
3385 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3386 spi->bits_per_word); in spi_setup()
3390 if (!spi->max_speed_hz) in spi_setup()
3391 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3393 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3395 if (spi->controller->setup) in spi_setup()
3396 status = spi->controller->setup(spi); in spi_setup()
3398 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3399 status = pm_runtime_get_sync(spi->controller->dev.parent); in spi_setup()
3401 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3402 pm_runtime_put_noidle(spi->controller->dev.parent); in spi_setup()
3403 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3411 * checking for a non-zero return value instead of a negative in spi_setup()
3416 spi_set_cs(spi, false, true); in spi_setup()
3417 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3418 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
3420 spi_set_cs(spi, false, true); in spi_setup()
3423 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3425 if (spi->rt && !spi->controller->rt) { in spi_setup()
3426 spi->controller->rt = true; in spi_setup()
3427 spi_set_thread_rt(spi->controller); in spi_setup()
3430 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
3431 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), in spi_setup()
3432 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
3433 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
3434 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
3435 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
3436 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
3444 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3445 * @spi: the device that requires specific CS timing configuration
3452 int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup, in spi_set_cs_timing() argument
3457 if (spi->controller->set_cs_timing) in spi_set_cs_timing()
3458 return spi->controller->set_cs_timing(spi, setup, hold, in spi_set_cs_timing()
3461 if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) || in spi_set_cs_timing()
3462 (hold && hold->unit == SPI_DELAY_UNIT_SCK) || in spi_set_cs_timing()
3463 (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) { in spi_set_cs_timing()
3464 dev_err(&spi->dev, in spi_set_cs_timing()
3465 "Clock-cycle delays for CS not supported in SW mode\n"); in spi_set_cs_timing()
3466 return -ENOTSUPP; in spi_set_cs_timing()
3471 /* copy delays to controller */ in spi_set_cs_timing()
3473 memcpy(&spi->controller->cs_setup, setup, len); in spi_set_cs_timing()
3475 memset(&spi->controller->cs_setup, 0, len); in spi_set_cs_timing()
3478 memcpy(&spi->controller->cs_hold, hold, len); in spi_set_cs_timing()
3480 memset(&spi->controller->cs_hold, 0, len); in spi_set_cs_timing()
3483 memcpy(&spi->controller->cs_inactive, inactive, len); in spi_set_cs_timing()
3485 memset(&spi->controller->cs_inactive, 0, len); in spi_set_cs_timing()
3492 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
3496 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); in _spi_xfer_word_delay_update()
3500 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
3505 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
3506 sizeof(xfer->word_delay)); in _spi_xfer_word_delay_update()
3511 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
3513 struct spi_controller *ctlr = spi->controller; in __spi_validate()
3517 if (list_empty(&message->transfers)) in __spi_validate()
3518 return -EINVAL; in __spi_validate()
3520 /* If an SPI controller does not support toggling the CS line on each in __spi_validate()
3522 * for the CS line, we can emulate the CS-per-word hardware function by in __spi_validate()
3523 * splitting transfers into one-word transfers and ensuring that in __spi_validate()
3526 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || in __spi_validate()
3527 spi->cs_gpiod || in __spi_validate()
3528 gpio_is_valid(spi->cs_gpio))) { in __spi_validate()
3532 maxsize = (spi->bits_per_word + 7) / 8; in __spi_validate()
3534 /* spi_split_transfers_maxsize() requires message->spi */ in __spi_validate()
3535 message->spi = spi; in __spi_validate()
3542 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3544 if (list_is_last(&xfer->transfer_list, &message->transfers)) in __spi_validate()
3546 xfer->cs_change = 1; in __spi_validate()
3550 /* Half-duplex links include original MicroWire, and ones with in __spi_validate()
3555 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || in __spi_validate()
3556 (spi->mode & SPI_3WIRE)) { in __spi_validate()
3557 unsigned flags = ctlr->flags; in __spi_validate()
3559 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3560 if (xfer->rx_buf && xfer->tx_buf) in __spi_validate()
3561 return -EINVAL; in __spi_validate()
3562 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) in __spi_validate()
3563 return -EINVAL; in __spi_validate()
3564 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) in __spi_validate()
3565 return -EINVAL; in __spi_validate()
3570 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
3577 message->frame_length = 0; in __spi_validate()
3578 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3579 xfer->effective_speed_hz = 0; in __spi_validate()
3580 message->frame_length += xfer->len; in __spi_validate()
3581 if (!xfer->bits_per_word) in __spi_validate()
3582 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
3584 if (!xfer->speed_hz) in __spi_validate()
3585 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
3587 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) in __spi_validate()
3588 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
3590 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) in __spi_validate()
3591 return -EINVAL; in __spi_validate()
3594 * SPI transfer length should be multiple of SPI word size in __spi_validate()
3595 * where SPI word size should be power-of-two multiple in __spi_validate()
3597 if (xfer->bits_per_word <= 8) in __spi_validate()
3599 else if (xfer->bits_per_word <= 16) in __spi_validate()
3605 if (xfer->len % w_size) in __spi_validate()
3606 return -EINVAL; in __spi_validate()
3608 if (xfer->speed_hz && ctlr->min_speed_hz && in __spi_validate()
3609 xfer->speed_hz < ctlr->min_speed_hz) in __spi_validate()
3610 return -EINVAL; in __spi_validate()
3612 if (xfer->tx_buf && !xfer->tx_nbits) in __spi_validate()
3613 xfer->tx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3614 if (xfer->rx_buf && !xfer->rx_nbits) in __spi_validate()
3615 xfer->rx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3620 if (xfer->tx_buf) { in __spi_validate()
3621 if (xfer->tx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3622 xfer->tx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3623 xfer->tx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3624 return -EINVAL; in __spi_validate()
3625 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3626 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
3627 return -EINVAL; in __spi_validate()
3628 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3629 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
3630 return -EINVAL; in __spi_validate()
3633 if (xfer->rx_buf) { in __spi_validate()
3634 if (xfer->rx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3635 xfer->rx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3636 xfer->rx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3637 return -EINVAL; in __spi_validate()
3638 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3639 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
3640 return -EINVAL; in __spi_validate()
3641 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3642 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
3643 return -EINVAL; in __spi_validate()
3646 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
3647 return -EINVAL; in __spi_validate()
3650 message->status = -EINPROGRESS; in __spi_validate()
3655 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
3657 struct spi_controller *ctlr = spi->controller; in __spi_async()
3661 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
3664 if (!ctlr->transfer) in __spi_async()
3665 return -ENOTSUPP; in __spi_async()
3667 message->spi = spi; in __spi_async()
3669 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); in __spi_async()
3670 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); in __spi_async()
3674 if (!ctlr->ptp_sts_supported) { in __spi_async()
3675 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_async()
3676 xfer->ptp_sts_word_pre = 0; in __spi_async()
3677 ptp_read_system_prets(xfer->ptp_sts); in __spi_async()
3681 return ctlr->transfer(spi, message); in __spi_async()
3685 * spi_async - asynchronous SPI transfer
3686 * @spi: device with which data will be exchanged
3694 * Before that invocation, the value of message->status is undefined.
3695 * When the callback is issued, message->status holds either zero (to
3698 * deallocate the associated memory; it's no longer in use by any SPI
3699 * core or controller driver code.
3715 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
3717 struct spi_controller *ctlr = spi->controller; in spi_async()
3721 ret = __spi_validate(spi, message); in spi_async()
3725 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async()
3727 if (ctlr->bus_lock_flag) in spi_async()
3728 ret = -EBUSY; in spi_async()
3730 ret = __spi_async(spi, message); in spi_async()
3732 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async()
3739 * spi_async_locked - version of spi_async with exclusive bus usage
3740 * @spi: device with which data will be exchanged
3748 * Before that invocation, the value of message->status is undefined.
3749 * When the callback is issued, message->status holds either zero (to
3752 * deallocate the associated memory; it's no longer in use by any SPI
3753 * core or controller driver code.
3769 int spi_async_locked(struct spi_device *spi, struct spi_message *message) in spi_async_locked() argument
3771 struct spi_controller *ctlr = spi->controller; in spi_async_locked()
3775 ret = __spi_validate(spi, message); in spi_async_locked()
3779 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3781 ret = __spi_async(spi, message); in spi_async_locked()
3783 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3790 /*-------------------------------------------------------------------------*/
3792 /* Utility methods for SPI protocol drivers, layered on
3802 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
3806 struct spi_controller *ctlr = spi->controller; in __spi_sync()
3809 status = __spi_validate(spi, message); in __spi_sync()
3813 message->complete = spi_complete; in __spi_sync()
3814 message->context = &done; in __spi_sync()
3815 message->spi = spi; in __spi_sync()
3817 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); in __spi_sync()
3818 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); in __spi_sync()
3825 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3826 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3830 status = __spi_queued_transfer(spi, message, false); in __spi_sync()
3832 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3834 status = spi_async_locked(spi, message); in __spi_sync()
3841 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3842 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_sync()
3844 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, in __spi_sync()
3850 status = message->status; in __spi_sync()
3852 message->context = NULL; in __spi_sync()
3857 * spi_sync - blocking/synchronous SPI data transfers
3858 * @spi: device with which data will be exchanged
3863 * is non-interruptible, and has no timeout. Low-overhead controller
3866 * Note that the SPI device's chip select is active during the message,
3868 * frequently-used devices may want to minimize costs of selecting a chip,
3877 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
3881 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
3882 ret = __spi_sync(spi, message); in spi_sync()
3883 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
3890 * spi_sync_locked - version of spi_sync with exclusive bus usage
3891 * @spi: device with which data will be exchanged
3896 * is non-interruptible, and has no timeout. Low-overhead controller
3900 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3905 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
3907 return __spi_sync(spi, message); in spi_sync_locked()
3912 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3913 * @ctlr: SPI bus master that should be locked for exclusive bus access
3917 * is non-interruptible, and has no timeout.
3920 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3922 * and spi_async_locked calls when the SPI bus lock is held.
3930 mutex_lock(&ctlr->bus_lock_mutex); in spi_bus_lock()
3932 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3933 ctlr->bus_lock_flag = 1; in spi_bus_lock()
3934 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3943 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3944 * @ctlr: SPI bus master that was locked for exclusive bus access
3948 * is non-interruptible, and has no timeout.
3950 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3957 ctlr->bus_lock_flag = 0; in spi_bus_unlock()
3959 mutex_unlock(&ctlr->bus_lock_mutex); in spi_bus_unlock()
3971 * spi_write_then_read - SPI synchronous write followed by read
3972 * @spi: device with which data will be exchanged
3973 * @txbuf: data to be written (need not be dma-safe)
3975 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3985 * Performance-sensitive or bulk transfer code should instead use
3986 * spi_{async,sync}() calls with dma-safe buffers.
3990 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4001 /* Use preallocated DMA-safe buffer if we can. We can't avoid in spi_write_then_read()
4004 * using the pre-allocated buffer or the transfer is too large. in spi_write_then_read()
4010 return -ENOMEM; in spi_write_then_read()
4031 status = spi_sync(spi, &message); in spi_write_then_read()
4044 /*-------------------------------------------------------------------------*/
4058 /* the spi controllers are not using spi_bus, so we find it with another way */
4078 struct spi_device *spi; in of_spi_notify() local
4082 ctlr = of_find_spi_controller_by_node(rd->dn->parent); in of_spi_notify()
4086 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { in of_spi_notify()
4087 put_device(&ctlr->dev); in of_spi_notify()
4091 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4092 put_device(&ctlr->dev); in of_spi_notify()
4094 if (IS_ERR(spi)) { in of_spi_notify()
4096 __func__, rd->dn); in of_spi_notify()
4097 of_node_clear_flag(rd->dn, OF_POPULATED); in of_spi_notify()
4098 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4104 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_spi_notify()
4108 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4109 if (spi == NULL) in of_spi_notify()
4113 spi_unregister_device(spi); in of_spi_notify()
4116 put_device(&spi->dev); in of_spi_notify()
4133 return ACPI_COMPANION(dev->parent) == data; in spi_acpi_controller_match()
4164 struct spi_device *spi; in acpi_spi_notify() local
4168 ctlr = acpi_spi_find_controller_by_adev(adev->parent); in acpi_spi_notify()
4173 put_device(&ctlr->dev); in acpi_spi_notify()
4179 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4180 if (!spi) in acpi_spi_notify()
4183 spi_unregister_device(spi); in acpi_spi_notify()
4184 put_device(&spi->dev); in acpi_spi_notify()
4204 status = -ENOMEM; in spi_init()