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
477 * addition of devices below an unregistering controller.
482 * spi_alloc_device - Allocate a new SPI device
483 * @ctlr: Controller to which device is connected
492 * spi_device structure to add it to the SPI controller. If the caller
500 struct spi_device *spi; in spi_alloc_device() local
505 spi = kzalloc(sizeof(*spi), GFP_KERNEL); in spi_alloc_device()
506 if (!spi) { in spi_alloc_device()
511 spi->master = spi->controller = ctlr; in spi_alloc_device()
512 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
513 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
514 spi->dev.release = spidev_release; in spi_alloc_device()
515 spi->cs_gpio = -ENOENT; in spi_alloc_device()
516 spi->mode = ctlr->buswidth_override_bits; in spi_alloc_device()
518 spin_lock_init(&spi->statistics.lock); in spi_alloc_device()
520 device_initialize(&spi->dev); in spi_alloc_device()
521 return spi; in spi_alloc_device()
525 static void spi_dev_set_name(struct spi_device *spi) in spi_dev_set_name() argument
527 struct acpi_device *adev = ACPI_COMPANION(&spi->dev); in spi_dev_set_name()
530 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); in spi_dev_set_name()
534 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
535 spi->chip_select); in spi_dev_set_name()
540 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
543 if (spi->controller == new_spi->controller && in spi_dev_check()
544 spi->chip_select == new_spi->chip_select) in spi_dev_check()
545 return -EBUSY; in spi_dev_check()
549 static void spi_cleanup(struct spi_device *spi) in spi_cleanup() argument
551 if (spi->controller->cleanup) in spi_cleanup()
552 spi->controller->cleanup(spi); in spi_cleanup()
556 * spi_add_device - Add spi_device allocated with spi_alloc_device
557 * @spi: spi_device to register
560 * spi_alloc_device can be added onto the spi bus with this function.
564 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
566 struct spi_controller *ctlr = spi->controller; in spi_add_device()
567 struct device *dev = ctlr->dev.parent; in spi_add_device()
571 if (spi->chip_select >= ctlr->num_chipselect) { in spi_add_device()
572 dev_err(dev, "cs%d >= max %d\n", spi->chip_select, in spi_add_device()
573 ctlr->num_chipselect); in spi_add_device()
574 return -EINVAL; in spi_add_device()
578 spi_dev_set_name(spi); in spi_add_device()
586 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in spi_add_device()
589 spi->chip_select); in spi_add_device()
593 /* Controller may unregister concurrently */ in spi_add_device()
595 !device_is_registered(&ctlr->dev)) { in spi_add_device()
596 status = -ENODEV; in spi_add_device()
601 if (ctlr->cs_gpiods) in spi_add_device()
602 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; in spi_add_device()
603 else if (ctlr->cs_gpios) in spi_add_device()
604 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; in spi_add_device()
610 status = spi_setup(spi); in spi_add_device()
613 dev_name(&spi->dev), status); in spi_add_device()
618 status = device_add(&spi->dev); in spi_add_device()
621 dev_name(&spi->dev), status); in spi_add_device()
622 spi_cleanup(spi); in spi_add_device()
624 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in spi_add_device()
634 * spi_new_device - instantiate one new SPI device
635 * @ctlr: Controller to which device is connected
636 * @chip: Describes the SPI device
640 * after board init creates the hard-wired devices. Some development
643 * driver could add devices (which it would learn about out-of-band).
653 /* NOTE: caller did any chip->bus_num checks necessary. in spi_new_device()
656 * error-or-pointer (not NULL-or-pointer), troubleshootability in spi_new_device()
664 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); in spi_new_device()
666 proxy->chip_select = chip->chip_select; in spi_new_device()
667 proxy->max_speed_hz = chip->max_speed_hz; in spi_new_device()
668 proxy->mode = chip->mode; in spi_new_device()
669 proxy->irq = chip->irq; in spi_new_device()
670 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); in spi_new_device()
671 proxy->dev.platform_data = (void *) chip->platform_data; in spi_new_device()
672 proxy->controller_data = chip->controller_data; in spi_new_device()
673 proxy->controller_state = NULL; in spi_new_device()
675 if (chip->properties) { in spi_new_device()
676 status = device_add_properties(&proxy->dev, chip->properties); in spi_new_device()
678 dev_err(&ctlr->dev, in spi_new_device()
680 chip->modalias, status); in spi_new_device()
692 if (chip->properties) in spi_new_device()
693 device_remove_properties(&proxy->dev); in spi_new_device()
701 * spi_unregister_device - unregister a single SPI device
702 * @spi: spi_device to unregister
704 * Start making the passed SPI device vanish. Normally this would be handled
707 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
709 if (!spi) in spi_unregister_device()
712 if (spi->dev.of_node) { in spi_unregister_device()
713 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
714 of_node_put(spi->dev.of_node); in spi_unregister_device()
716 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
717 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
718 device_del(&spi->dev); in spi_unregister_device()
719 spi_cleanup(spi); in spi_unregister_device()
720 put_device(&spi->dev); in spi_unregister_device()
729 if (ctlr->bus_num != bi->bus_num) in spi_match_controller_to_boardinfo()
734 dev_err(ctlr->dev.parent, "can't create new device for %s\n", in spi_match_controller_to_boardinfo()
735 bi->modalias); in spi_match_controller_to_boardinfo()
739 * spi_register_board_info - register SPI devices for a given board
744 * Board-specific early init code calls this (probably during arch_initcall)
745 * with segments of the SPI device table. Any device nodes are created later,
746 * after the relevant parent SPI controller (bus_num) is defined. We keep
747 * this table of devices forever, so that reloading a controller driver will
748 * not make Linux forget about these hard-wired devices.
750 * Other code can also call this, e.g. a particular add-on board might provide
751 * SPI devices through its expansion connector, so code initializing that board
752 * would naturally declare its SPI devices.
755 * any embedded pointers (platform_data, etc), they're copied as-is.
756 * Device properties are deep-copied though.
770 return -ENOMEM; in spi_register_board_info()
775 memcpy(&bi->board_info, info, sizeof(*info)); in spi_register_board_info()
776 if (info->properties) { in spi_register_board_info()
777 bi->board_info.properties = in spi_register_board_info()
778 property_entries_dup(info->properties); in spi_register_board_info()
779 if (IS_ERR(bi->board_info.properties)) in spi_register_board_info()
780 return PTR_ERR(bi->board_info.properties); in spi_register_board_info()
784 list_add_tail(&bi->list, &board_list); in spi_register_board_info()
787 &bi->board_info); in spi_register_board_info()
794 /*-------------------------------------------------------------------------*/
796 static void spi_set_cs(struct spi_device *spi, bool enable, bool force) in spi_set_cs() argument
804 if (!force && (spi->controller->last_cs_enable == enable) && in spi_set_cs()
805 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
808 spi->controller->last_cs_enable = enable; in spi_set_cs()
809 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
811 if (!spi->controller->set_cs_timing) { in spi_set_cs()
813 spi_delay_exec(&spi->controller->cs_setup, NULL); in spi_set_cs()
815 spi_delay_exec(&spi->controller->cs_hold, NULL); in spi_set_cs()
818 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
821 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { in spi_set_cs()
822 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
823 if (spi->cs_gpiod) { in spi_set_cs()
826 * thus the SPISerialBus() resource defines it on the per-chip in spi_set_cs()
834 if (has_acpi_companion(&spi->dev)) in spi_set_cs()
835 gpiod_set_value_cansleep(spi->cs_gpiod, !enable); in spi_set_cs()
838 gpiod_set_value_cansleep(spi->cs_gpiod, enable1); in spi_set_cs()
842 * default for SPI. in spi_set_cs()
844 gpio_set_value_cansleep(spi->cs_gpio, !enable); in spi_set_cs()
847 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
848 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && in spi_set_cs()
849 spi->controller->set_cs) in spi_set_cs()
850 spi->controller->set_cs(spi, !enable); in spi_set_cs()
851 } else if (spi->controller->set_cs) { in spi_set_cs()
852 spi->controller->set_cs(spi, !enable); in spi_set_cs()
855 if (!spi->controller->set_cs_timing) { in spi_set_cs()
857 spi_delay_exec(&spi->controller->cs_inactive, NULL); in spi_set_cs()
887 desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len); in spi_map_buf()
890 return -EINVAL; in spi_map_buf()
897 sg = &sgt->sgl[0]; in spi_map_buf()
908 PAGE_SIZE - offset_in_page(buf))); in spi_map_buf()
915 return -ENOMEM; in spi_map_buf()
926 len -= min; in spi_map_buf()
930 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); in spi_map_buf()
932 ret = -ENOMEM; in spi_map_buf()
938 sgt->nents = ret; in spi_map_buf()
946 if (sgt->orig_nents) { in spi_unmap_buf()
947 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); in spi_unmap_buf()
949 sgt->orig_nents = 0; in spi_unmap_buf()
950 sgt->nents = 0; in spi_unmap_buf()
960 if (!ctlr->can_dma) in __spi_map_msg()
963 if (ctlr->dma_tx) in __spi_map_msg()
964 tx_dev = ctlr->dma_tx->device->dev; in __spi_map_msg()
966 tx_dev = ctlr->dev.parent; in __spi_map_msg()
968 if (ctlr->dma_rx) in __spi_map_msg()
969 rx_dev = ctlr->dma_rx->device->dev; in __spi_map_msg()
971 rx_dev = ctlr->dev.parent; in __spi_map_msg()
973 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_map_msg()
974 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
977 if (xfer->tx_buf != NULL) { in __spi_map_msg()
978 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
979 (void *)xfer->tx_buf, xfer->len, in __spi_map_msg()
985 if (xfer->rx_buf != NULL) { in __spi_map_msg()
986 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, in __spi_map_msg()
987 xfer->rx_buf, xfer->len, in __spi_map_msg()
990 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
997 ctlr->cur_msg_mapped = true; in __spi_map_msg()
1007 if (!ctlr->cur_msg_mapped || !ctlr->can_dma) in __spi_unmap_msg()
1010 if (ctlr->dma_tx) in __spi_unmap_msg()
1011 tx_dev = ctlr->dma_tx->device->dev; in __spi_unmap_msg()
1013 tx_dev = ctlr->dev.parent; in __spi_unmap_msg()
1015 if (ctlr->dma_rx) in __spi_unmap_msg()
1016 rx_dev = ctlr->dma_rx->device->dev; in __spi_unmap_msg()
1018 rx_dev = ctlr->dev.parent; in __spi_unmap_msg()
1020 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_unmap_msg()
1021 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_unmap_msg()
1024 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in __spi_unmap_msg()
1025 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in __spi_unmap_msg()
1028 ctlr->cur_msg_mapped = false; in __spi_unmap_msg()
1051 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_unmap_msg()
1056 if (xfer->tx_buf == ctlr->dummy_tx) in spi_unmap_msg()
1057 xfer->tx_buf = NULL; in spi_unmap_msg()
1058 if (xfer->rx_buf == ctlr->dummy_rx) in spi_unmap_msg()
1059 xfer->rx_buf = NULL; in spi_unmap_msg()
1071 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) in spi_map_msg()
1072 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1076 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_map_msg()
1077 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && in spi_map_msg()
1078 !xfer->tx_buf) in spi_map_msg()
1079 max_tx = max(xfer->len, max_tx); in spi_map_msg()
1080 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && in spi_map_msg()
1081 !xfer->rx_buf) in spi_map_msg()
1082 max_rx = max(xfer->len, max_rx); in spi_map_msg()
1086 tmp = krealloc(ctlr->dummy_tx, max_tx, in spi_map_msg()
1089 return -ENOMEM; in spi_map_msg()
1090 ctlr->dummy_tx = tmp; in spi_map_msg()
1095 tmp = krealloc(ctlr->dummy_rx, max_rx, in spi_map_msg()
1098 return -ENOMEM; in spi_map_msg()
1099 ctlr->dummy_rx = tmp; in spi_map_msg()
1103 list_for_each_entry(xfer, &msg->transfers, in spi_map_msg()
1105 if (!xfer->len) in spi_map_msg()
1107 if (!xfer->tx_buf) in spi_map_msg()
1108 xfer->tx_buf = ctlr->dummy_tx; in spi_map_msg()
1109 if (!xfer->rx_buf) in spi_map_msg()
1110 xfer->rx_buf = ctlr->dummy_rx; in spi_map_msg()
1122 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_wait()
1123 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_wait()
1124 u32 speed_hz = xfer->speed_hz; in spi_transfer_wait()
1128 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { in spi_transfer_wait()
1129 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1130 return -EINTR; in spi_transfer_wait()
1136 ms = 8LL * 1000LL * xfer->len; in spi_transfer_wait()
1143 ms = wait_for_completion_timeout(&ctlr->xfer_completion, in spi_transfer_wait()
1149 dev_err(&msg->spi->dev, in spi_transfer_wait()
1150 "SPI transfer timed out\n"); in spi_transfer_wait()
1151 return -ETIMEDOUT; in spi_transfer_wait()
1176 u32 delay = _delay->value; in spi_delay_to_ns()
1177 u32 unit = _delay->unit; in spi_delay_to_ns()
1192 return -EINVAL; in spi_delay_to_ns()
1196 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; in spi_delay_to_ns()
1198 return -EINVAL; in spi_delay_to_ns()
1202 return -EINVAL; in spi_delay_to_ns()
1216 return -EINVAL; in spi_delay_exec()
1231 u32 delay = xfer->cs_change_delay.value; in _spi_transfer_cs_change_delay()
1232 u32 unit = xfer->cs_change_delay.unit; in _spi_transfer_cs_change_delay()
1235 /* return early on "fast" mode - for everything but USECS */ in _spi_transfer_cs_change_delay()
1242 ret = spi_delay_exec(&xfer->cs_change_delay, xfer); in _spi_transfer_cs_change_delay()
1244 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1252 * spi_transfer_one_message - Default implementation of transfer_one_message()
1264 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_one_message()
1265 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_one_message()
1267 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1272 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_transfer_one_message()
1278 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1279 xfer->ptp_sts_word_pre = 0; in spi_transfer_one_message()
1280 ptp_read_system_prets(xfer->ptp_sts); in spi_transfer_one_message()
1283 if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { in spi_transfer_one_message()
1284 reinit_completion(&ctlr->xfer_completion); in spi_transfer_one_message()
1287 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1289 if (ctlr->cur_msg_mapped && in spi_transfer_one_message()
1290 (xfer->error & SPI_TRANS_FAIL_NO_START)) { in spi_transfer_one_message()
1292 ctlr->fallback = true; in spi_transfer_one_message()
1293 xfer->error &= ~SPI_TRANS_FAIL_NO_START; in spi_transfer_one_message()
1301 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1302 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1309 msg->status = ret; in spi_transfer_one_message()
1312 if (xfer->len) in spi_transfer_one_message()
1313 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1315 xfer->len); in spi_transfer_one_message()
1318 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1319 ptp_read_system_postts(xfer->ptp_sts); in spi_transfer_one_message()
1320 xfer->ptp_sts_word_post = xfer->len; in spi_transfer_one_message()
1325 if (msg->status != -EINPROGRESS) in spi_transfer_one_message()
1330 if (xfer->cs_change) { in spi_transfer_one_message()
1331 if (list_is_last(&xfer->transfer_list, in spi_transfer_one_message()
1332 &msg->transfers)) { in spi_transfer_one_message()
1335 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1337 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1341 msg->actual_length += xfer->len; in spi_transfer_one_message()
1346 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1348 if (msg->status == -EINPROGRESS) in spi_transfer_one_message()
1349 msg->status = ret; in spi_transfer_one_message()
1351 if (msg->status && ctlr->handle_err) in spi_transfer_one_message()
1352 ctlr->handle_err(ctlr, msg); in spi_transfer_one_message()
1360 * spi_finalize_current_transfer - report completion of a transfer
1361 * @ctlr: the controller reporting completion
1363 * Called by SPI drivers using the core transfer_one_message()
1369 complete(&ctlr->xfer_completion); in spi_finalize_current_transfer()
1375 if (ctlr->auto_runtime_pm) { in spi_idle_runtime_pm()
1376 pm_runtime_mark_last_busy(ctlr->dev.parent); in spi_idle_runtime_pm()
1377 pm_runtime_put_autosuspend(ctlr->dev.parent); in spi_idle_runtime_pm()
1382 * __spi_pump_messages - function which processes spi message queue
1383 * @ctlr: controller to process queue for
1386 * This function checks if there is any spi message in the queue that
1403 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1406 if (ctlr->cur_msg) { in __spi_pump_messages()
1407 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1412 if (ctlr->idling) { in __spi_pump_messages()
1413 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_pump_messages()
1414 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1419 if (list_empty(&ctlr->queue) || !ctlr->running) { in __spi_pump_messages()
1420 if (!ctlr->busy) { in __spi_pump_messages()
1421 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1425 /* Defer any non-atomic teardown to the thread */ in __spi_pump_messages()
1427 if (!ctlr->dummy_rx && !ctlr->dummy_tx && in __spi_pump_messages()
1428 !ctlr->unprepare_transfer_hardware) { in __spi_pump_messages()
1430 ctlr->busy = false; in __spi_pump_messages()
1433 kthread_queue_work(ctlr->kworker, in __spi_pump_messages()
1434 &ctlr->pump_messages); in __spi_pump_messages()
1436 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1440 ctlr->busy = false; in __spi_pump_messages()
1441 ctlr->idling = true; in __spi_pump_messages()
1442 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1444 kfree(ctlr->dummy_rx); in __spi_pump_messages()
1445 ctlr->dummy_rx = NULL; in __spi_pump_messages()
1446 kfree(ctlr->dummy_tx); in __spi_pump_messages()
1447 ctlr->dummy_tx = NULL; in __spi_pump_messages()
1448 if (ctlr->unprepare_transfer_hardware && in __spi_pump_messages()
1449 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_pump_messages()
1450 dev_err(&ctlr->dev, in __spi_pump_messages()
1455 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1456 ctlr->idling = false; in __spi_pump_messages()
1457 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1462 msg = list_first_entry(&ctlr->queue, struct spi_message, queue); in __spi_pump_messages()
1463 ctlr->cur_msg = msg; in __spi_pump_messages()
1465 list_del_init(&msg->queue); in __spi_pump_messages()
1466 if (ctlr->busy) in __spi_pump_messages()
1469 ctlr->busy = true; in __spi_pump_messages()
1470 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1472 mutex_lock(&ctlr->io_mutex); in __spi_pump_messages()
1474 if (!was_busy && ctlr->auto_runtime_pm) { in __spi_pump_messages()
1475 ret = pm_runtime_get_sync(ctlr->dev.parent); in __spi_pump_messages()
1477 pm_runtime_put_noidle(ctlr->dev.parent); in __spi_pump_messages()
1478 dev_err(&ctlr->dev, "Failed to power device: %d\n", in __spi_pump_messages()
1480 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1488 if (!was_busy && ctlr->prepare_transfer_hardware) { in __spi_pump_messages()
1489 ret = ctlr->prepare_transfer_hardware(ctlr); in __spi_pump_messages()
1491 dev_err(&ctlr->dev, in __spi_pump_messages()
1495 if (ctlr->auto_runtime_pm) in __spi_pump_messages()
1496 pm_runtime_put(ctlr->dev.parent); in __spi_pump_messages()
1498 msg->status = ret; in __spi_pump_messages()
1501 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1508 if (ctlr->prepare_message) { in __spi_pump_messages()
1509 ret = ctlr->prepare_message(ctlr, msg); in __spi_pump_messages()
1511 dev_err(&ctlr->dev, "failed to prepare message: %d\n", in __spi_pump_messages()
1513 msg->status = ret; in __spi_pump_messages()
1517 ctlr->cur_msg_prepared = true; in __spi_pump_messages()
1522 msg->status = ret; in __spi_pump_messages()
1527 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in __spi_pump_messages()
1528 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_pump_messages()
1529 xfer->ptp_sts_word_pre = 0; in __spi_pump_messages()
1530 ptp_read_system_prets(xfer->ptp_sts); in __spi_pump_messages()
1534 ret = ctlr->transfer_one_message(ctlr, msg); in __spi_pump_messages()
1536 dev_err(&ctlr->dev, in __spi_pump_messages()
1542 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1550 * spi_pump_messages - kthread work function which processes spi message queue
1551 * @work: pointer to kthread work struct contained in the controller struct
1562 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1563 * TX timestamp for the requested byte from the SPI
1586 if (!xfer->ptp_sts) in spi_take_timestamp_pre()
1589 if (xfer->timestamped) in spi_take_timestamp_pre()
1592 if (progress > xfer->ptp_sts_word_pre) in spi_take_timestamp_pre()
1596 xfer->ptp_sts_word_pre = progress; in spi_take_timestamp_pre()
1599 local_irq_save(ctlr->irq_flags); in spi_take_timestamp_pre()
1603 ptp_read_system_prets(xfer->ptp_sts); in spi_take_timestamp_pre()
1608 * spi_take_timestamp_post - helper for drivers to collect the end of the
1609 * TX timestamp for the requested byte from the SPI
1617 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1623 if (!xfer->ptp_sts) in spi_take_timestamp_post()
1626 if (xfer->timestamped) in spi_take_timestamp_post()
1629 if (progress < xfer->ptp_sts_word_post) in spi_take_timestamp_post()
1632 ptp_read_system_postts(xfer->ptp_sts); in spi_take_timestamp_post()
1635 local_irq_restore(ctlr->irq_flags); in spi_take_timestamp_post()
1640 xfer->ptp_sts_word_post = progress; in spi_take_timestamp_post()
1642 xfer->timestamped = true; in spi_take_timestamp_post()
1647 * spi_set_thread_rt - set the controller to pump at realtime priority
1648 * @ctlr: controller to boost priority of
1650 * This can be called because the controller requested realtime priority
1651 * (by setting the ->rt value before calling spi_register_controller()) or
1657 * controller. If this eventually becomes a problem we may see if we can
1663 dev_info(&ctlr->dev, in spi_set_thread_rt()
1665 sched_set_fifo(ctlr->kworker->task); in spi_set_thread_rt()
1670 ctlr->running = false; in spi_init_queue()
1671 ctlr->busy = false; in spi_init_queue()
1673 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); in spi_init_queue()
1674 if (IS_ERR(ctlr->kworker)) { in spi_init_queue()
1675 dev_err(&ctlr->dev, "failed to create message pump kworker\n"); in spi_init_queue()
1676 return PTR_ERR(ctlr->kworker); in spi_init_queue()
1679 kthread_init_work(&ctlr->pump_messages, spi_pump_messages); in spi_init_queue()
1682 * Controller config will indicate if this controller should run the in spi_init_queue()
1688 if (ctlr->rt) in spi_init_queue()
1695 * spi_get_next_queued_message() - called by driver to check for queued
1697 * @ctlr: the controller to check for queued messages
1710 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1711 next = list_first_entry_or_null(&ctlr->queue, struct spi_message, in spi_get_next_queued_message()
1713 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1720 * spi_finalize_current_message() - the current message is complete
1721 * @ctlr: the controller to return the message to
1733 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1734 mesg = ctlr->cur_msg; in spi_finalize_current_message()
1735 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1737 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in spi_finalize_current_message()
1738 list_for_each_entry(xfer, &mesg->transfers, transfer_list) { in spi_finalize_current_message()
1739 ptp_read_system_postts(xfer->ptp_sts); in spi_finalize_current_message()
1740 xfer->ptp_sts_word_post = xfer->len; in spi_finalize_current_message()
1744 if (unlikely(ctlr->ptp_sts_supported)) in spi_finalize_current_message()
1745 list_for_each_entry(xfer, &mesg->transfers, transfer_list) in spi_finalize_current_message()
1746 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); in spi_finalize_current_message()
1750 /* In the prepare_messages callback the spi bus has the opportunity to in spi_finalize_current_message()
1757 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { in spi_finalize_current_message()
1758 ret = ctlr->unprepare_message(ctlr, mesg); in spi_finalize_current_message()
1760 dev_err(&ctlr->dev, "failed to unprepare message: %d\n", in spi_finalize_current_message()
1765 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1766 ctlr->cur_msg = NULL; in spi_finalize_current_message()
1767 ctlr->cur_msg_prepared = false; in spi_finalize_current_message()
1768 ctlr->fallback = false; in spi_finalize_current_message()
1769 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_finalize_current_message()
1770 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1774 mesg->state = NULL; in spi_finalize_current_message()
1775 if (mesg->complete) in spi_finalize_current_message()
1776 mesg->complete(mesg->context); in spi_finalize_current_message()
1784 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_start_queue()
1786 if (ctlr->running || ctlr->busy) { in spi_start_queue()
1787 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1788 return -EBUSY; in spi_start_queue()
1791 ctlr->running = true; in spi_start_queue()
1792 ctlr->cur_msg = NULL; in spi_start_queue()
1793 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1795 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_start_queue()
1806 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1810 * A wait_queue on the ctlr->busy could be used, but then the common in spi_stop_queue()
1812 * friends on every SPI message. Do this instead. in spi_stop_queue()
1814 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { in spi_stop_queue()
1815 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1817 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1820 if (!list_empty(&ctlr->queue) || ctlr->busy) in spi_stop_queue()
1821 ret = -EBUSY; in spi_stop_queue()
1823 ctlr->running = false; in spi_stop_queue()
1825 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1828 dev_warn(&ctlr->dev, "could not stop message queue\n"); in spi_stop_queue()
1847 dev_err(&ctlr->dev, "problem destroying queue\n"); in spi_destroy_queue()
1851 kthread_destroy_worker(ctlr->kworker); in spi_destroy_queue()
1856 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
1860 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
1863 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1865 if (!ctlr->running) { in __spi_queued_transfer()
1866 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1867 return -ESHUTDOWN; in __spi_queued_transfer()
1869 msg->actual_length = 0; in __spi_queued_transfer()
1870 msg->status = -EINPROGRESS; in __spi_queued_transfer()
1872 list_add_tail(&msg->queue, &ctlr->queue); in __spi_queued_transfer()
1873 if (!ctlr->busy && need_pump) in __spi_queued_transfer()
1874 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_queued_transfer()
1876 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1881 * spi_queued_transfer - transfer function for queued transfers
1882 * @spi: spi device which is requesting transfer
1883 * @msg: spi message which is to handled is queued to driver queue
1887 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
1889 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
1896 ctlr->transfer = spi_queued_transfer; in spi_controller_initialize_queue()
1897 if (!ctlr->transfer_one_message) in spi_controller_initialize_queue()
1898 ctlr->transfer_one_message = spi_transfer_one_message; in spi_controller_initialize_queue()
1903 dev_err(&ctlr->dev, "problem initializing queue\n"); in spi_controller_initialize_queue()
1906 ctlr->queued = true; in spi_controller_initialize_queue()
1909 dev_err(&ctlr->dev, "problem starting queue\n"); in spi_controller_initialize_queue()
1922 * spi_flush_queue - Send all pending messages in the queue from the callers'
1924 * @ctlr: controller to process queue for
1927 * sent before doing something. Is used by the spi-mem code to make sure SPI
1928 * memory operations do not preempt regular SPI transfers that have been queued
1929 * before the spi-mem operation.
1933 if (ctlr->transfer == spi_queued_transfer) in spi_flush_queue()
1937 /*-------------------------------------------------------------------------*/
1940 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
1947 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
1948 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
1949 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
1950 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
1951 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
1952 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
1953 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
1954 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
1955 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
1956 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
1959 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
1964 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
1967 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
1970 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
1973 dev_warn(&ctlr->dev, in of_spi_parse_dt()
1974 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
1980 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
1985 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
1988 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
1991 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
1994 dev_warn(&ctlr->dev, in of_spi_parse_dt()
1995 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
2003 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", in of_spi_parse_dt()
2005 return -EINVAL; in of_spi_parse_dt()
2013 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", in of_spi_parse_dt()
2017 spi->chip_select = value; in of_spi_parse_dt()
2020 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2021 spi->max_speed_hz = value; in of_spi_parse_dt()
2029 struct spi_device *spi; in of_register_spi_device() local
2033 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2034 if (!spi) { in of_register_spi_device()
2035 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); in of_register_spi_device()
2036 rc = -ENOMEM; in of_register_spi_device()
2041 rc = of_modalias_node(nc, spi->modalias, in of_register_spi_device()
2042 sizeof(spi->modalias)); in of_register_spi_device()
2044 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); in of_register_spi_device()
2048 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2054 spi->dev.of_node = nc; in of_register_spi_device()
2055 spi->dev.fwnode = of_fwnode_handle(nc); in of_register_spi_device()
2058 rc = spi_add_device(spi); in of_register_spi_device()
2060 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); in of_register_spi_device()
2064 return spi; in of_register_spi_device()
2069 spi_dev_put(spi); in of_register_spi_device()
2074 * of_register_spi_devices() - Register child devices onto the SPI bus
2077 * Registers an spi_device for each child node of controller node which
2078 * represents a valid SPI slave.
2082 struct spi_device *spi; in of_register_spi_devices() local
2085 if (!ctlr->dev.of_node) in of_register_spi_devices()
2088 for_each_available_child_of_node(ctlr->dev.of_node, nc) { in of_register_spi_devices()
2091 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2092 if (IS_ERR(spi)) { in of_register_spi_devices()
2093 dev_warn(&ctlr->dev, in of_register_spi_devices()
2094 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2122 && obj->buffer.length >= 4) in acpi_spi_parse_apple_properties()
2123 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2126 && obj->buffer.length == 8) in acpi_spi_parse_apple_properties()
2127 lookup->bits_per_word = *(u64 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2130 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2131 lookup->mode |= SPI_LSB_FIRST; in acpi_spi_parse_apple_properties()
2134 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2135 lookup->mode |= SPI_CPOL; in acpi_spi_parse_apple_properties()
2138 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2139 lookup->mode |= SPI_CPHA; in acpi_spi_parse_apple_properties()
2145 struct spi_controller *ctlr = lookup->ctlr; in acpi_spi_add_resource()
2147 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { in acpi_spi_add_resource()
2152 sb = &ares->data.spi_serial_bus; in acpi_spi_add_resource()
2153 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { in acpi_spi_add_resource()
2156 sb->resource_source.string_ptr, in acpi_spi_add_resource()
2160 ACPI_HANDLE(ctlr->dev.parent) != parent_handle) in acpi_spi_add_resource()
2161 return -ENODEV; in acpi_spi_add_resource()
2165 * host controller driver in Windows and can vary in acpi_spi_add_resource()
2167 * 0 .. max - 1 so we need to ask the driver to in acpi_spi_add_resource()
2170 if (ctlr->fw_translate_cs) { in acpi_spi_add_resource()
2171 int cs = ctlr->fw_translate_cs(ctlr, in acpi_spi_add_resource()
2172 sb->device_selection); in acpi_spi_add_resource()
2175 lookup->chip_select = cs; in acpi_spi_add_resource()
2177 lookup->chip_select = sb->device_selection; in acpi_spi_add_resource()
2180 lookup->max_speed_hz = sb->connection_speed; in acpi_spi_add_resource()
2181 lookup->bits_per_word = sb->data_bit_length; in acpi_spi_add_resource()
2183 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) in acpi_spi_add_resource()
2184 lookup->mode |= SPI_CPHA; in acpi_spi_add_resource()
2185 if (sb->clock_polarity == ACPI_SPI_START_HIGH) in acpi_spi_add_resource()
2186 lookup->mode |= SPI_CPOL; in acpi_spi_add_resource()
2187 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) in acpi_spi_add_resource()
2188 lookup->mode |= SPI_CS_HIGH; in acpi_spi_add_resource()
2190 } else if (lookup->irq < 0) { in acpi_spi_add_resource()
2194 lookup->irq = r.start; in acpi_spi_add_resource()
2207 struct spi_device *spi; in acpi_register_spi_device() local
2210 if (acpi_bus_get_status(adev) || !adev->status.present || in acpi_register_spi_device()
2215 lookup.irq = -1; in acpi_register_spi_device()
2223 /* found SPI in _CRS but it points to another controller */ in acpi_register_spi_device()
2227 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) && in acpi_register_spi_device()
2228 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { in acpi_register_spi_device()
2229 /* Apple does not use _CRS but nested devices for SPI slaves */ in acpi_register_spi_device()
2236 spi = spi_alloc_device(ctlr); in acpi_register_spi_device()
2237 if (!spi) { in acpi_register_spi_device()
2238 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_register_spi_device()
2239 dev_name(&adev->dev)); in acpi_register_spi_device()
2244 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_register_spi_device()
2245 spi->max_speed_hz = lookup.max_speed_hz; in acpi_register_spi_device()
2246 spi->mode |= lookup.mode; in acpi_register_spi_device()
2247 spi->irq = lookup.irq; in acpi_register_spi_device()
2248 spi->bits_per_word = lookup.bits_per_word; in acpi_register_spi_device()
2249 spi->chip_select = lookup.chip_select; in acpi_register_spi_device()
2251 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2252 sizeof(spi->modalias)); in acpi_register_spi_device()
2254 if (spi->irq < 0) in acpi_register_spi_device()
2255 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in acpi_register_spi_device()
2259 adev->power.flags.ignore_parent = true; in acpi_register_spi_device()
2260 if (spi_add_device(spi)) { in acpi_register_spi_device()
2261 adev->power.flags.ignore_parent = false; in acpi_register_spi_device()
2262 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2263 dev_name(&adev->dev)); in acpi_register_spi_device()
2264 spi_dev_put(spi); in acpi_register_spi_device()
2289 handle = ACPI_HANDLE(ctlr->dev.parent); in acpi_register_spi_devices()
2297 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2320 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2321 * controller
2322 * @spi: device used for the current transfer
2324 int spi_slave_abort(struct spi_device *spi) in spi_slave_abort() argument
2326 struct spi_controller *ctlr = spi->controller; in spi_slave_abort()
2328 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) in spi_slave_abort()
2329 return ctlr->slave_abort(ctlr); in spi_slave_abort()
2331 return -ENOTSUPP; in spi_slave_abort()
2347 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_show()
2349 child ? to_spi_device(child)->modalias : NULL); in slave_show()
2357 struct spi_device *spi; in slave_store() local
2364 return -EINVAL; in slave_store()
2366 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_store()
2375 spi = spi_alloc_device(ctlr); in slave_store()
2376 if (!spi) in slave_store()
2377 return -ENOMEM; in slave_store()
2379 strlcpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2381 rc = spi_add_device(spi); in slave_store()
2383 spi_dev_put(spi); in slave_store()
2419 * __spi_alloc_controller - allocate an SPI master or slave controller
2420 * @dev: the controller, possibly using the platform_bus
2421 * @size: how much zeroed driver-private data to allocate; the pointer to this
2426 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2427 * slave (true) controller
2430 * This call is used only by SPI controller drivers, which are the
2437 * controller's methods before calling spi_register_controller(); and (after
2441 * Return: the SPI controller structure on success, else NULL.
2456 device_initialize(&ctlr->dev); in __spi_alloc_controller()
2457 ctlr->bus_num = -1; in __spi_alloc_controller()
2458 ctlr->num_chipselect = 1; in __spi_alloc_controller()
2459 ctlr->slave = slave; in __spi_alloc_controller()
2461 ctlr->dev.class = &spi_slave_class; in __spi_alloc_controller()
2463 ctlr->dev.class = &spi_master_class; in __spi_alloc_controller()
2464 ctlr->dev.parent = dev; in __spi_alloc_controller()
2465 pm_suspend_ignore_children(&ctlr->dev, true); in __spi_alloc_controller()
2478 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
2479 * @dev: physical device of SPI controller
2480 * @size: how much zeroed driver-private data to allocate
2481 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
2484 * Allocate an SPI controller and automatically release a reference on it
2490 * Return: the SPI controller structure on success, else NULL.
2519 struct device_node *np = ctlr->dev.of_node; in of_spi_get_gpio_numbers()
2524 nb = of_gpio_named_count(np, "cs-gpios"); in of_spi_get_gpio_numbers()
2525 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in of_spi_get_gpio_numbers()
2527 /* Return error only for an incorrectly formed cs-gpios property */ in of_spi_get_gpio_numbers()
2528 if (nb == 0 || nb == -ENOENT) in of_spi_get_gpio_numbers()
2533 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), in of_spi_get_gpio_numbers()
2535 ctlr->cs_gpios = cs; in of_spi_get_gpio_numbers()
2537 if (!ctlr->cs_gpios) in of_spi_get_gpio_numbers()
2538 return -ENOMEM; in of_spi_get_gpio_numbers()
2540 for (i = 0; i < ctlr->num_chipselect; i++) in of_spi_get_gpio_numbers()
2541 cs[i] = -ENOENT; in of_spi_get_gpio_numbers()
2544 cs[i] = of_get_named_gpio(np, "cs-gpios", i); in of_spi_get_gpio_numbers()
2556 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2557 * @ctlr: The SPI master to grab GPIO descriptors for
2563 struct device *dev = &ctlr->dev; in spi_get_gpio_descs()
2568 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in spi_get_gpio_descs()
2571 if (nb == 0 || nb == -ENOENT) in spi_get_gpio_descs()
2576 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), in spi_get_gpio_descs()
2579 return -ENOMEM; in spi_get_gpio_descs()
2580 ctlr->cs_gpiods = cs; in spi_get_gpio_descs()
2605 return -ENOMEM; in spi_get_gpio_descs()
2611 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2613 return -EINVAL; in spi_get_gpio_descs()
2618 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; in spi_get_gpio_descs()
2620 if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && in spi_get_gpio_descs()
2621 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2623 return -EINVAL; in spi_get_gpio_descs()
2632 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
2633 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
2635 * If ->mem_ops is NULL, we request that at least one of the in spi_controller_check_ops()
2636 * ->transfer_xxx() method be implemented. in spi_controller_check_ops()
2638 if (ctlr->mem_ops) { in spi_controller_check_ops()
2639 if (!ctlr->mem_ops->exec_op) in spi_controller_check_ops()
2640 return -EINVAL; in spi_controller_check_ops()
2641 } else if (!ctlr->transfer && !ctlr->transfer_one && in spi_controller_check_ops()
2642 !ctlr->transfer_one_message) { in spi_controller_check_ops()
2643 return -EINVAL; in spi_controller_check_ops()
2650 * spi_register_controller - register SPI master or slave controller
2655 * SPI controllers connect to their drivers using some non-SPI bus,
2657 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2659 * SPI controllers use board specific (often SOC specific) bus numbers,
2660 * and board-specific addressing for SPI devices combines those numbers
2661 * with chip select numbers. Since SPI does not directly support dynamic
2666 * success, else a negative error code (dropping the controller's refcount).
2674 struct device *dev = ctlr->dev.parent; in spi_register_controller()
2680 return -ENODEV; in spi_register_controller()
2684 * the SPI controller. in spi_register_controller()
2690 if (ctlr->bus_num >= 0) { in spi_register_controller()
2691 /* devices with a fixed bus num must check-in with the num */ in spi_register_controller()
2693 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2694 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2697 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2698 ctlr->bus_num = id; in spi_register_controller()
2699 } else if (ctlr->dev.of_node) { in spi_register_controller()
2701 id = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
2703 ctlr->bus_num = id; in spi_register_controller()
2705 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2706 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2709 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2712 if (ctlr->bus_num < 0) { in spi_register_controller()
2713 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
2725 ctlr->bus_num = id; in spi_register_controller()
2727 INIT_LIST_HEAD(&ctlr->queue); in spi_register_controller()
2728 spin_lock_init(&ctlr->queue_lock); in spi_register_controller()
2729 spin_lock_init(&ctlr->bus_lock_spinlock); in spi_register_controller()
2730 mutex_init(&ctlr->bus_lock_mutex); in spi_register_controller()
2731 mutex_init(&ctlr->io_mutex); in spi_register_controller()
2732 ctlr->bus_lock_flag = 0; in spi_register_controller()
2733 init_completion(&ctlr->xfer_completion); in spi_register_controller()
2734 if (!ctlr->max_dma_len) in spi_register_controller()
2735 ctlr->max_dma_len = INT_MAX; in spi_register_controller()
2740 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
2743 if (ctlr->use_gpio_descriptors) { in spi_register_controller()
2748 * A controller using GPIO descriptors always in spi_register_controller()
2751 ctlr->mode_bits |= SPI_CS_HIGH; in spi_register_controller()
2761 * Even if it's just one always-selected device, there must in spi_register_controller()
2764 if (!ctlr->num_chipselect) { in spi_register_controller()
2765 status = -EINVAL; in spi_register_controller()
2769 status = device_add(&ctlr->dev); in spi_register_controller()
2774 dev_name(&ctlr->dev)); in spi_register_controller()
2778 * need the queueing logic if the driver is only supporting high-level in spi_register_controller()
2781 if (ctlr->transfer) { in spi_register_controller()
2782 dev_info(dev, "controller is unqueued, this is deprecated\n"); in spi_register_controller()
2783 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { in spi_register_controller()
2786 device_del(&ctlr->dev); in spi_register_controller()
2791 spin_lock_init(&ctlr->statistics.lock); in spi_register_controller()
2794 list_add_tail(&ctlr->list, &spi_controller_list); in spi_register_controller()
2796 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); in spi_register_controller()
2806 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
2818 * devm_spi_register_controller - register managed SPI master or slave
2819 * controller
2820 * @dev: device managing SPI controller
2821 * @ctlr: initialized controller, originally from spi_alloc_master() or
2825 * Register a SPI device as with spi_register_controller() which will
2838 return -ENOMEM; in devm_spi_register_controller()
2864 * spi_unregister_controller - unregister SPI master or slave controller
2865 * @ctlr: the controller being unregistered
2868 * This call is used only by SPI controller drivers, which are the
2873 * Note that this function also drops a reference to the controller.
2878 int id = ctlr->bus_num; in spi_unregister_controller()
2884 device_for_each_child(&ctlr->dev, NULL, __unregister); in spi_unregister_controller()
2886 /* First make sure that this controller was ever added */ in spi_unregister_controller()
2890 if (ctlr->queued) { in spi_unregister_controller()
2892 dev_err(&ctlr->dev, "queue remove failed\n"); in spi_unregister_controller()
2895 list_del(&ctlr->list); in spi_unregister_controller()
2898 device_del(&ctlr->dev); in spi_unregister_controller()
2900 /* Release the last reference on the controller if its driver in spi_unregister_controller()
2903 if (!devres_find(ctlr->dev.parent, devm_spi_release_controller, in spi_unregister_controller()
2905 put_device(&ctlr->dev); in spi_unregister_controller()
2922 /* Basically no-ops for non-queued controllers */ in spi_controller_suspend()
2923 if (!ctlr->queued) in spi_controller_suspend()
2928 dev_err(&ctlr->dev, "queue stop failed\n"); in spi_controller_suspend()
2938 if (!ctlr->queued) in spi_controller_resume()
2943 dev_err(&ctlr->dev, "queue restart failed\n"); in spi_controller_resume()
2955 return ctlr->bus_num == *bus_num; in __spi_controller_match()
2959 * spi_busnum_to_master - look up master associated with bus_num
2968 * Return: the SPI master structure on success, else NULL.
2984 /*-------------------------------------------------------------------------*/
2986 /* Core methods for SPI resource management */
2989 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2992 * @spi: the spi device for which we allocate memory
3002 void *spi_res_alloc(struct spi_device *spi, in spi_res_alloc() argument
3012 INIT_LIST_HEAD(&sres->entry); in spi_res_alloc()
3013 sres->release = release; in spi_res_alloc()
3015 return sres->data; in spi_res_alloc()
3020 * spi_res_free - free an spi resource
3031 WARN_ON(!list_empty(&sres->entry)); in spi_res_free()
3037 * spi_res_add - add a spi_res to the spi_message
3038 * @message: the spi message
3045 WARN_ON(!list_empty(&sres->entry)); in spi_res_add()
3046 list_add_tail(&sres->entry, &message->resources); in spi_res_add()
3051 * spi_res_release - release all spi resources for this message
3059 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { in spi_res_release()
3060 if (res->release) in spi_res_release()
3061 res->release(ctlr, message, res->data); in spi_res_release()
3063 list_del(&res->entry); in spi_res_release()
3070 /*-------------------------------------------------------------------------*/
3082 if (rxfer->release) in __spi_replace_transfers_release()
3083 rxfer->release(ctlr, msg, res); in __spi_replace_transfers_release()
3086 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); in __spi_replace_transfers_release()
3089 for (i = 0; i < rxfer->inserted; i++) in __spi_replace_transfers_release()
3090 list_del(&rxfer->inserted_transfers[i].transfer_list); in __spi_replace_transfers_release()
3094 * spi_replace_transfers - replace transfers with several transfers
3122 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3127 return ERR_PTR(-ENOMEM); in spi_replace_transfers()
3130 rxfer->release = release; in spi_replace_transfers()
3134 rxfer->extradata = in spi_replace_transfers()
3135 &rxfer->inserted_transfers[insert]; in spi_replace_transfers()
3138 INIT_LIST_HEAD(&rxfer->replaced_transfers); in spi_replace_transfers()
3141 * the @replaced_transfers - it may be spi_message.messages! in spi_replace_transfers()
3143 rxfer->replaced_after = xfer_first->transfer_list.prev; in spi_replace_transfers()
3147 /* if the entry after replaced_after it is msg->transfers in spi_replace_transfers()
3151 if (rxfer->replaced_after->next == &msg->transfers) { in spi_replace_transfers()
3152 dev_err(&msg->spi->dev, in spi_replace_transfers()
3155 list_splice(&rxfer->replaced_transfers, in spi_replace_transfers()
3156 rxfer->replaced_after); in spi_replace_transfers()
3162 return ERR_PTR(-EINVAL); in spi_replace_transfers()
3168 list_move_tail(rxfer->replaced_after->next, in spi_replace_transfers()
3169 &rxfer->replaced_transfers); in spi_replace_transfers()
3177 xfer = &rxfer->inserted_transfers[insert - 1 - i]; in spi_replace_transfers()
3183 list_add(&xfer->transfer_list, rxfer->replaced_after); in spi_replace_transfers()
3187 xfer->cs_change = false; in spi_replace_transfers()
3188 xfer->delay_usecs = 0; in spi_replace_transfers()
3189 xfer->delay.value = 0; in spi_replace_transfers()
3194 rxfer->inserted = insert; in spi_replace_transfers()
3215 count = DIV_ROUND_UP(xfer->len, maxsize); in __spi_split_transfer_maxsize()
3221 xfers = srt->inserted_transfers; in __spi_split_transfer_maxsize()
3251 xfers[i].len = min(maxsize, xfers[i].len - offset); in __spi_split_transfer_maxsize()
3257 *xferp = &xfers[count - 1]; in __spi_split_transfer_maxsize()
3260 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_split_transfer_maxsize()
3262 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, in __spi_split_transfer_maxsize()
3269 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
3293 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxsize()
3294 if (xfer->len > maxsize) { in spi_split_transfers_maxsize()
3306 /*-------------------------------------------------------------------------*/
3308 /* Core methods for SPI controller protocol drivers. Some of the
3315 if (ctlr->bits_per_word_mask) { in __spi_validate_bits_per_word()
3318 return -EINVAL; in __spi_validate_bits_per_word()
3319 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) in __spi_validate_bits_per_word()
3320 return -EINVAL; in __spi_validate_bits_per_word()
3327 * spi_setup - setup SPI mode and clock rate
3328 * @spi: the device whose settings are being modified
3331 * SPI protocol drivers may need to update the transfer mode if the
3337 * or from it. When this function returns, the spi device is deselected.
3340 * that the underlying controller or its driver does not support. For
3342 * LSB-first wire encoding, or active-high chipselects.
3346 int spi_setup(struct spi_device *spi) in spi_setup() argument
3353 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || in spi_setup()
3354 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { in spi_setup()
3355 dev_err(&spi->dev, in spi_setup()
3357 return -EINVAL; in spi_setup()
3361 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3364 return -EINVAL; in spi_setup()
3366 * that aren't supported with their current controller in spi_setup()
3370 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); in spi_setup()
3371 /* nothing prevents from working with active-high CS in case if it in spi_setup()
3374 if (gpio_is_valid(spi->cs_gpio)) in spi_setup()
3380 dev_warn(&spi->dev, in spi_setup()
3383 spi->mode &= ~ugly_bits; in spi_setup()
3387 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3389 return -EINVAL; in spi_setup()
3392 if (!spi->bits_per_word) in spi_setup()
3393 spi->bits_per_word = 8; in spi_setup()
3395 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3396 spi->bits_per_word); in spi_setup()
3400 if (!spi->max_speed_hz) in spi_setup()
3401 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3403 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3405 if (spi->controller->setup) in spi_setup()
3406 status = spi->controller->setup(spi); in spi_setup()
3408 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3409 status = pm_runtime_get_sync(spi->controller->dev.parent); in spi_setup()
3411 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3412 pm_runtime_put_noidle(spi->controller->dev.parent); in spi_setup()
3413 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3421 * checking for a non-zero return value instead of a negative in spi_setup()
3426 spi_set_cs(spi, false, true); in spi_setup()
3427 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3428 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
3430 spi_set_cs(spi, false, true); in spi_setup()
3433 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3435 if (spi->rt && !spi->controller->rt) { in spi_setup()
3436 spi->controller->rt = true; in spi_setup()
3437 spi_set_thread_rt(spi->controller); in spi_setup()
3440 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
3441 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), in spi_setup()
3442 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
3443 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
3444 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
3445 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
3446 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
3454 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3455 * @spi: the device that requires specific CS timing configuration
3462 int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup, in spi_set_cs_timing() argument
3467 if (spi->controller->set_cs_timing) in spi_set_cs_timing()
3468 return spi->controller->set_cs_timing(spi, setup, hold, in spi_set_cs_timing()
3471 if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) || in spi_set_cs_timing()
3472 (hold && hold->unit == SPI_DELAY_UNIT_SCK) || in spi_set_cs_timing()
3473 (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) { in spi_set_cs_timing()
3474 dev_err(&spi->dev, in spi_set_cs_timing()
3475 "Clock-cycle delays for CS not supported in SW mode\n"); in spi_set_cs_timing()
3476 return -ENOTSUPP; in spi_set_cs_timing()
3481 /* copy delays to controller */ in spi_set_cs_timing()
3483 memcpy(&spi->controller->cs_setup, setup, len); in spi_set_cs_timing()
3485 memset(&spi->controller->cs_setup, 0, len); in spi_set_cs_timing()
3488 memcpy(&spi->controller->cs_hold, hold, len); in spi_set_cs_timing()
3490 memset(&spi->controller->cs_hold, 0, len); in spi_set_cs_timing()
3493 memcpy(&spi->controller->cs_inactive, inactive, len); in spi_set_cs_timing()
3495 memset(&spi->controller->cs_inactive, 0, len); in spi_set_cs_timing()
3502 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
3506 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); in _spi_xfer_word_delay_update()
3510 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
3515 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
3516 sizeof(xfer->word_delay)); in _spi_xfer_word_delay_update()
3521 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
3523 struct spi_controller *ctlr = spi->controller; in __spi_validate()
3527 if (list_empty(&message->transfers)) in __spi_validate()
3528 return -EINVAL; in __spi_validate()
3530 /* If an SPI controller does not support toggling the CS line on each in __spi_validate()
3532 * for the CS line, we can emulate the CS-per-word hardware function by in __spi_validate()
3533 * splitting transfers into one-word transfers and ensuring that in __spi_validate()
3536 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || in __spi_validate()
3537 spi->cs_gpiod || in __spi_validate()
3538 gpio_is_valid(spi->cs_gpio))) { in __spi_validate()
3542 maxsize = (spi->bits_per_word + 7) / 8; in __spi_validate()
3544 /* spi_split_transfers_maxsize() requires message->spi */ in __spi_validate()
3545 message->spi = spi; in __spi_validate()
3552 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3554 if (list_is_last(&xfer->transfer_list, &message->transfers)) in __spi_validate()
3556 xfer->cs_change = 1; in __spi_validate()
3560 /* Half-duplex links include original MicroWire, and ones with in __spi_validate()
3565 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || in __spi_validate()
3566 (spi->mode & SPI_3WIRE)) { in __spi_validate()
3567 unsigned flags = ctlr->flags; in __spi_validate()
3569 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3570 if (xfer->rx_buf && xfer->tx_buf) in __spi_validate()
3571 return -EINVAL; in __spi_validate()
3572 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) in __spi_validate()
3573 return -EINVAL; in __spi_validate()
3574 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) in __spi_validate()
3575 return -EINVAL; in __spi_validate()
3580 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
3587 message->frame_length = 0; in __spi_validate()
3588 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3589 xfer->effective_speed_hz = 0; in __spi_validate()
3590 message->frame_length += xfer->len; in __spi_validate()
3591 if (!xfer->bits_per_word) in __spi_validate()
3592 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
3594 if (!xfer->speed_hz) in __spi_validate()
3595 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
3597 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) in __spi_validate()
3598 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
3600 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) in __spi_validate()
3601 return -EINVAL; in __spi_validate()
3604 * SPI transfer length should be multiple of SPI word size in __spi_validate()
3605 * where SPI word size should be power-of-two multiple in __spi_validate()
3607 if (xfer->bits_per_word <= 8) in __spi_validate()
3609 else if (xfer->bits_per_word <= 16) in __spi_validate()
3615 if (xfer->len % w_size) in __spi_validate()
3616 return -EINVAL; in __spi_validate()
3618 if (xfer->speed_hz && ctlr->min_speed_hz && in __spi_validate()
3619 xfer->speed_hz < ctlr->min_speed_hz) in __spi_validate()
3620 return -EINVAL; in __spi_validate()
3622 if (xfer->tx_buf && !xfer->tx_nbits) in __spi_validate()
3623 xfer->tx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3624 if (xfer->rx_buf && !xfer->rx_nbits) in __spi_validate()
3625 xfer->rx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3630 if (xfer->tx_buf) { in __spi_validate()
3631 if (xfer->tx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3632 xfer->tx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3633 xfer->tx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3634 return -EINVAL; in __spi_validate()
3635 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3636 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
3637 return -EINVAL; in __spi_validate()
3638 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3639 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
3640 return -EINVAL; in __spi_validate()
3643 if (xfer->rx_buf) { in __spi_validate()
3644 if (xfer->rx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3645 xfer->rx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3646 xfer->rx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3647 return -EINVAL; in __spi_validate()
3648 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3649 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
3650 return -EINVAL; in __spi_validate()
3651 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3652 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
3653 return -EINVAL; in __spi_validate()
3656 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
3657 return -EINVAL; in __spi_validate()
3660 message->status = -EINPROGRESS; in __spi_validate()
3665 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
3667 struct spi_controller *ctlr = spi->controller; in __spi_async()
3671 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
3674 if (!ctlr->transfer) in __spi_async()
3675 return -ENOTSUPP; in __spi_async()
3677 message->spi = spi; in __spi_async()
3679 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); in __spi_async()
3680 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); in __spi_async()
3684 if (!ctlr->ptp_sts_supported) { in __spi_async()
3685 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_async()
3686 xfer->ptp_sts_word_pre = 0; in __spi_async()
3687 ptp_read_system_prets(xfer->ptp_sts); in __spi_async()
3691 return ctlr->transfer(spi, message); in __spi_async()
3695 * spi_async - asynchronous SPI transfer
3696 * @spi: device with which data will be exchanged
3704 * Before that invocation, the value of message->status is undefined.
3705 * When the callback is issued, message->status holds either zero (to
3708 * deallocate the associated memory; it's no longer in use by any SPI
3709 * core or controller driver code.
3725 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
3727 struct spi_controller *ctlr = spi->controller; in spi_async()
3731 ret = __spi_validate(spi, message); in spi_async()
3735 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async()
3737 if (ctlr->bus_lock_flag) in spi_async()
3738 ret = -EBUSY; in spi_async()
3740 ret = __spi_async(spi, message); in spi_async()
3742 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async()
3749 * spi_async_locked - version of spi_async with exclusive bus usage
3750 * @spi: device with which data will be exchanged
3758 * Before that invocation, the value of message->status is undefined.
3759 * When the callback is issued, message->status holds either zero (to
3762 * deallocate the associated memory; it's no longer in use by any SPI
3763 * core or controller driver code.
3779 int spi_async_locked(struct spi_device *spi, struct spi_message *message) in spi_async_locked() argument
3781 struct spi_controller *ctlr = spi->controller; in spi_async_locked()
3785 ret = __spi_validate(spi, message); in spi_async_locked()
3789 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3791 ret = __spi_async(spi, message); in spi_async_locked()
3793 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3800 /*-------------------------------------------------------------------------*/
3802 /* Utility methods for SPI protocol drivers, layered on
3812 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
3816 struct spi_controller *ctlr = spi->controller; in __spi_sync()
3819 status = __spi_validate(spi, message); in __spi_sync()
3823 message->complete = spi_complete; in __spi_sync()
3824 message->context = &done; in __spi_sync()
3825 message->spi = spi; in __spi_sync()
3827 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); in __spi_sync()
3828 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); in __spi_sync()
3835 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3836 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3840 status = __spi_queued_transfer(spi, message, false); in __spi_sync()
3842 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3844 status = spi_async_locked(spi, message); in __spi_sync()
3851 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3852 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_sync()
3854 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, in __spi_sync()
3860 status = message->status; in __spi_sync()
3862 message->context = NULL; in __spi_sync()
3867 * spi_sync - blocking/synchronous SPI data transfers
3868 * @spi: device with which data will be exchanged
3873 * is non-interruptible, and has no timeout. Low-overhead controller
3876 * Note that the SPI device's chip select is active during the message,
3878 * frequently-used devices may want to minimize costs of selecting a chip,
3887 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
3891 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
3892 ret = __spi_sync(spi, message); in spi_sync()
3893 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
3900 * spi_sync_locked - version of spi_sync with exclusive bus usage
3901 * @spi: device with which data will be exchanged
3906 * is non-interruptible, and has no timeout. Low-overhead controller
3910 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3915 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
3917 return __spi_sync(spi, message); in spi_sync_locked()
3922 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3923 * @ctlr: SPI bus master that should be locked for exclusive bus access
3927 * is non-interruptible, and has no timeout.
3930 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3932 * and spi_async_locked calls when the SPI bus lock is held.
3940 mutex_lock(&ctlr->bus_lock_mutex); in spi_bus_lock()
3942 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3943 ctlr->bus_lock_flag = 1; in spi_bus_lock()
3944 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3953 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3954 * @ctlr: SPI bus master that was locked for exclusive bus access
3958 * is non-interruptible, and has no timeout.
3960 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3967 ctlr->bus_lock_flag = 0; in spi_bus_unlock()
3969 mutex_unlock(&ctlr->bus_lock_mutex); in spi_bus_unlock()
3981 * spi_write_then_read - SPI synchronous write followed by read
3982 * @spi: device with which data will be exchanged
3983 * @txbuf: data to be written (need not be dma-safe)
3985 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3995 * Performance-sensitive or bulk transfer code should instead use
3996 * spi_{async,sync}() calls with dma-safe buffers.
4000 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4011 /* Use preallocated DMA-safe buffer if we can. We can't avoid in spi_write_then_read()
4014 * using the pre-allocated buffer or the transfer is too large. in spi_write_then_read()
4020 return -ENOMEM; in spi_write_then_read()
4041 status = spi_sync(spi, &message); in spi_write_then_read()
4054 /*-------------------------------------------------------------------------*/
4068 /* the spi controllers are not using spi_bus, so we find it with another way */
4088 struct spi_device *spi; in of_spi_notify() local
4092 ctlr = of_find_spi_controller_by_node(rd->dn->parent); in of_spi_notify()
4096 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { in of_spi_notify()
4097 put_device(&ctlr->dev); in of_spi_notify()
4101 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4102 put_device(&ctlr->dev); in of_spi_notify()
4104 if (IS_ERR(spi)) { in of_spi_notify()
4106 __func__, rd->dn); in of_spi_notify()
4107 of_node_clear_flag(rd->dn, OF_POPULATED); in of_spi_notify()
4108 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4114 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_spi_notify()
4118 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4119 if (spi == NULL) in of_spi_notify()
4123 spi_unregister_device(spi); in of_spi_notify()
4126 put_device(&spi->dev); in of_spi_notify()
4143 return ACPI_COMPANION(dev->parent) == data; in spi_acpi_controller_match()
4174 struct spi_device *spi; in acpi_spi_notify() local
4178 ctlr = acpi_spi_find_controller_by_adev(adev->parent); in acpi_spi_notify()
4183 put_device(&ctlr->dev); in acpi_spi_notify()
4189 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4190 if (!spi) in acpi_spi_notify()
4193 spi_unregister_device(spi); in acpi_spi_notify()
4194 put_device(&spi->dev); in acpi_spi_notify()
4214 status = -ENOMEM; in spi_init()