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()
375 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_probe()
376 struct spi_device *spi = to_spi_device(dev); in spi_probe() local
379 ret = of_clk_set_defaults(dev->of_node, false); in spi_probe()
383 if (dev->of_node) { in spi_probe()
384 spi->irq = of_irq_get(dev->of_node, 0); in spi_probe()
385 if (spi->irq == -EPROBE_DEFER) in spi_probe()
386 return -EPROBE_DEFER; in spi_probe()
387 if (spi->irq < 0) in spi_probe()
388 spi->irq = 0; in spi_probe()
395 if (sdrv->probe) { in spi_probe()
396 ret = sdrv->probe(spi); in spi_probe()
406 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_remove()
408 if (sdrv->remove) { in spi_remove()
411 ret = sdrv->remove(to_spi_device(dev)); in spi_remove()
423 if (dev->driver) { in spi_shutdown()
424 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_shutdown()
426 if (sdrv->shutdown) in spi_shutdown()
427 sdrv->shutdown(to_spi_device(dev)); in spi_shutdown()
432 .name = "spi",
443 * __spi_register_driver - register a SPI driver
452 sdrv->driver.owner = owner; in __spi_register_driver()
453 sdrv->driver.bus = &spi_bus_type; in __spi_register_driver()
454 return driver_register(&sdrv->driver); in __spi_register_driver()
458 /*-------------------------------------------------------------------------*/
460 /* SPI devices should normally not be created by SPI device drivers; that
461 * would make them board-specific. Similarly with SPI controller drivers.
462 * Device registration normally goes into like arch/.../mach.../board-YYY.c
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()
555 static int __spi_add_device(struct spi_device *spi) in __spi_add_device() argument
557 struct spi_controller *ctlr = spi->controller; in __spi_add_device()
558 struct device *dev = ctlr->dev.parent; in __spi_add_device()
561 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in __spi_add_device()
564 spi->chip_select); in __spi_add_device()
568 /* Controller may unregister concurrently */ in __spi_add_device()
570 !device_is_registered(&ctlr->dev)) { in __spi_add_device()
571 return -ENODEV; in __spi_add_device()
575 if (ctlr->cs_gpiods) in __spi_add_device()
576 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; in __spi_add_device()
577 else if (ctlr->cs_gpios) in __spi_add_device()
578 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; in __spi_add_device()
584 status = spi_setup(spi); in __spi_add_device()
587 dev_name(&spi->dev), status); in __spi_add_device()
592 status = device_add(&spi->dev); in __spi_add_device()
595 dev_name(&spi->dev), status); in __spi_add_device()
596 spi_cleanup(spi); in __spi_add_device()
598 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in __spi_add_device()
605 * spi_add_device - Add spi_device allocated with spi_alloc_device
606 * @spi: spi_device to register
609 * spi_alloc_device can be added onto the spi bus with this function.
613 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
615 struct spi_controller *ctlr = spi->controller; in spi_add_device()
616 struct device *dev = ctlr->dev.parent; in spi_add_device()
620 if (spi->chip_select >= ctlr->num_chipselect) { in spi_add_device()
621 dev_err(dev, "cs%d >= max %d\n", spi->chip_select, in spi_add_device()
622 ctlr->num_chipselect); in spi_add_device()
623 return -EINVAL; in spi_add_device()
627 spi_dev_set_name(spi); in spi_add_device()
633 mutex_lock(&ctlr->add_lock); in spi_add_device()
634 status = __spi_add_device(spi); in spi_add_device()
635 mutex_unlock(&ctlr->add_lock); in spi_add_device()
640 static int spi_add_device_locked(struct spi_device *spi) in spi_add_device_locked() argument
642 struct spi_controller *ctlr = spi->controller; in spi_add_device_locked()
643 struct device *dev = ctlr->dev.parent; in spi_add_device_locked()
646 if (spi->chip_select >= ctlr->num_chipselect) { in spi_add_device_locked()
647 dev_err(dev, "cs%d >= max %d\n", spi->chip_select, in spi_add_device_locked()
648 ctlr->num_chipselect); in spi_add_device_locked()
649 return -EINVAL; in spi_add_device_locked()
653 spi_dev_set_name(spi); in spi_add_device_locked()
655 WARN_ON(!mutex_is_locked(&ctlr->add_lock)); in spi_add_device_locked()
656 return __spi_add_device(spi); in spi_add_device_locked()
660 * spi_new_device - instantiate one new SPI device
661 * @ctlr: Controller to which device is connected
662 * @chip: Describes the SPI device
666 * after board init creates the hard-wired devices. Some development
669 * driver could add devices (which it would learn about out-of-band).
679 /* NOTE: caller did any chip->bus_num checks necessary. in spi_new_device()
682 * error-or-pointer (not NULL-or-pointer), troubleshootability in spi_new_device()
690 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); in spi_new_device()
692 proxy->chip_select = chip->chip_select; in spi_new_device()
693 proxy->max_speed_hz = chip->max_speed_hz; in spi_new_device()
694 proxy->mode = chip->mode; in spi_new_device()
695 proxy->irq = chip->irq; in spi_new_device()
696 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); in spi_new_device()
697 proxy->dev.platform_data = (void *) chip->platform_data; in spi_new_device()
698 proxy->controller_data = chip->controller_data; in spi_new_device()
699 proxy->controller_state = NULL; in spi_new_device()
701 if (chip->swnode) { in spi_new_device()
702 status = device_add_software_node(&proxy->dev, chip->swnode); in spi_new_device()
704 dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n", in spi_new_device()
705 chip->modalias, status); in spi_new_device()
717 device_remove_software_node(&proxy->dev); in spi_new_device()
724 * spi_unregister_device - unregister a single SPI device
725 * @spi: spi_device to unregister
727 * Start making the passed SPI device vanish. Normally this would be handled
730 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
732 if (!spi) in spi_unregister_device()
735 if (spi->dev.of_node) { in spi_unregister_device()
736 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
737 of_node_put(spi->dev.of_node); in spi_unregister_device()
739 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
740 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
741 device_remove_software_node(&spi->dev); in spi_unregister_device()
742 device_del(&spi->dev); in spi_unregister_device()
743 spi_cleanup(spi); in spi_unregister_device()
744 put_device(&spi->dev); in spi_unregister_device()
753 if (ctlr->bus_num != bi->bus_num) in spi_match_controller_to_boardinfo()
758 dev_err(ctlr->dev.parent, "can't create new device for %s\n", in spi_match_controller_to_boardinfo()
759 bi->modalias); in spi_match_controller_to_boardinfo()
763 * spi_register_board_info - register SPI devices for a given board
768 * Board-specific early init code calls this (probably during arch_initcall)
769 * with segments of the SPI device table. Any device nodes are created later,
770 * after the relevant parent SPI controller (bus_num) is defined. We keep
771 * this table of devices forever, so that reloading a controller driver will
772 * not make Linux forget about these hard-wired devices.
774 * Other code can also call this, e.g. a particular add-on board might provide
775 * SPI devices through its expansion connector, so code initializing that board
776 * would naturally declare its SPI devices.
779 * any embedded pointers (platform_data, etc), they're copied as-is.
793 return -ENOMEM; in spi_register_board_info()
798 memcpy(&bi->board_info, info, sizeof(*info)); in spi_register_board_info()
801 list_add_tail(&bi->list, &board_list); in spi_register_board_info()
804 &bi->board_info); in spi_register_board_info()
811 /*-------------------------------------------------------------------------*/
813 static void spi_set_cs(struct spi_device *spi, bool enable, bool force) in spi_set_cs() argument
821 if (!force && (spi->controller->last_cs_enable == enable) && in spi_set_cs()
822 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
825 trace_spi_set_cs(spi, activate); in spi_set_cs()
827 spi->controller->last_cs_enable = enable; in spi_set_cs()
828 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
830 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio) || in spi_set_cs()
831 !spi->controller->set_cs_timing) { in spi_set_cs()
833 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
835 spi_delay_exec(&spi->cs_hold, NULL); in spi_set_cs()
838 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
841 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { in spi_set_cs()
842 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
843 if (spi->cs_gpiod) { in spi_set_cs()
846 * thus the SPISerialBus() resource defines it on the per-chip in spi_set_cs()
854 if (has_acpi_companion(&spi->dev)) in spi_set_cs()
855 gpiod_set_value_cansleep(spi->cs_gpiod, !enable); in spi_set_cs()
858 gpiod_set_value_cansleep(spi->cs_gpiod, activate); in spi_set_cs()
862 * default for SPI. in spi_set_cs()
864 gpio_set_value_cansleep(spi->cs_gpio, !enable); in spi_set_cs()
867 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
868 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && in spi_set_cs()
869 spi->controller->set_cs) in spi_set_cs()
870 spi->controller->set_cs(spi, !enable); in spi_set_cs()
871 } else if (spi->controller->set_cs) { in spi_set_cs()
872 spi->controller->set_cs(spi, !enable); in spi_set_cs()
875 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio) || in spi_set_cs()
876 !spi->controller->set_cs_timing) { in spi_set_cs()
878 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
908 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); in spi_map_buf()
911 return -EINVAL; in spi_map_buf()
918 sg = &sgt->sgl[0]; in spi_map_buf()
929 PAGE_SIZE - offset_in_page(buf))); in spi_map_buf()
936 return -ENOMEM; in spi_map_buf()
947 len -= min; in spi_map_buf()
951 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); in spi_map_buf()
953 ret = -ENOMEM; in spi_map_buf()
959 sgt->nents = ret; in spi_map_buf()
967 if (sgt->orig_nents) { in spi_unmap_buf()
968 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); in spi_unmap_buf()
979 if (!ctlr->can_dma) in __spi_map_msg()
982 if (ctlr->dma_tx) in __spi_map_msg()
983 tx_dev = ctlr->dma_tx->device->dev; in __spi_map_msg()
984 else if (ctlr->dma_map_dev) in __spi_map_msg()
985 tx_dev = ctlr->dma_map_dev; in __spi_map_msg()
987 tx_dev = ctlr->dev.parent; in __spi_map_msg()
989 if (ctlr->dma_rx) in __spi_map_msg()
990 rx_dev = ctlr->dma_rx->device->dev; in __spi_map_msg()
991 else if (ctlr->dma_map_dev) in __spi_map_msg()
992 rx_dev = ctlr->dma_map_dev; in __spi_map_msg()
994 rx_dev = ctlr->dev.parent; in __spi_map_msg()
996 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_map_msg()
997 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
1000 if (xfer->tx_buf != NULL) { in __spi_map_msg()
1001 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
1002 (void *)xfer->tx_buf, xfer->len, in __spi_map_msg()
1008 if (xfer->rx_buf != NULL) { in __spi_map_msg()
1009 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, in __spi_map_msg()
1010 xfer->rx_buf, xfer->len, in __spi_map_msg()
1013 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
1020 ctlr->cur_msg_mapped = true; in __spi_map_msg()
1030 if (!ctlr->cur_msg_mapped || !ctlr->can_dma) in __spi_unmap_msg()
1033 if (ctlr->dma_tx) in __spi_unmap_msg()
1034 tx_dev = ctlr->dma_tx->device->dev; in __spi_unmap_msg()
1036 tx_dev = ctlr->dev.parent; in __spi_unmap_msg()
1038 if (ctlr->dma_rx) in __spi_unmap_msg()
1039 rx_dev = ctlr->dma_rx->device->dev; in __spi_unmap_msg()
1041 rx_dev = ctlr->dev.parent; in __spi_unmap_msg()
1043 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_unmap_msg()
1044 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_unmap_msg()
1047 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in __spi_unmap_msg()
1048 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in __spi_unmap_msg()
1051 ctlr->cur_msg_mapped = false; in __spi_unmap_msg()
1074 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_unmap_msg()
1079 if (xfer->tx_buf == ctlr->dummy_tx) in spi_unmap_msg()
1080 xfer->tx_buf = NULL; in spi_unmap_msg()
1081 if (xfer->rx_buf == ctlr->dummy_rx) in spi_unmap_msg()
1082 xfer->rx_buf = NULL; in spi_unmap_msg()
1094 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) in spi_map_msg()
1095 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1099 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_map_msg()
1100 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && in spi_map_msg()
1101 !xfer->tx_buf) in spi_map_msg()
1102 max_tx = max(xfer->len, max_tx); in spi_map_msg()
1103 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && in spi_map_msg()
1104 !xfer->rx_buf) in spi_map_msg()
1105 max_rx = max(xfer->len, max_rx); in spi_map_msg()
1109 tmp = krealloc(ctlr->dummy_tx, max_tx, in spi_map_msg()
1112 return -ENOMEM; in spi_map_msg()
1113 ctlr->dummy_tx = tmp; in spi_map_msg()
1118 tmp = krealloc(ctlr->dummy_rx, max_rx, in spi_map_msg()
1121 return -ENOMEM; in spi_map_msg()
1122 ctlr->dummy_rx = tmp; in spi_map_msg()
1126 list_for_each_entry(xfer, &msg->transfers, in spi_map_msg()
1128 if (!xfer->len) in spi_map_msg()
1130 if (!xfer->tx_buf) in spi_map_msg()
1131 xfer->tx_buf = ctlr->dummy_tx; in spi_map_msg()
1132 if (!xfer->rx_buf) in spi_map_msg()
1133 xfer->rx_buf = ctlr->dummy_rx; in spi_map_msg()
1145 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_wait()
1146 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_wait()
1147 u32 speed_hz = xfer->speed_hz; in spi_transfer_wait()
1151 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { in spi_transfer_wait()
1152 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1153 return -EINTR; in spi_transfer_wait()
1160 * For each byte we wait for 8 cycles of the SPI clock. in spi_transfer_wait()
1165 ms = 8LL * MSEC_PER_SEC * xfer->len; in spi_transfer_wait()
1176 ms = wait_for_completion_timeout(&ctlr->xfer_completion, in spi_transfer_wait()
1182 dev_err(&msg->spi->dev, in spi_transfer_wait()
1183 "SPI transfer timed out\n"); in spi_transfer_wait()
1184 return -ETIMEDOUT; in spi_transfer_wait()
1209 u32 delay = _delay->value; in spi_delay_to_ns()
1210 u32 unit = _delay->unit; in spi_delay_to_ns()
1226 return -EINVAL; in spi_delay_to_ns()
1231 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; in spi_delay_to_ns()
1233 return -EINVAL; in spi_delay_to_ns()
1239 return -EINVAL; in spi_delay_to_ns()
1253 return -EINVAL; in spi_delay_exec()
1269 u32 delay = xfer->cs_change_delay.value; in _spi_transfer_cs_change_delay()
1270 u32 unit = xfer->cs_change_delay.unit; in _spi_transfer_cs_change_delay()
1273 /* return early on "fast" mode - for everything but USECS */ in _spi_transfer_cs_change_delay()
1280 ret = spi_delay_exec(&xfer->cs_change_delay, xfer); in _spi_transfer_cs_change_delay()
1282 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1290 * spi_transfer_one_message - Default implementation of transfer_one_message()
1302 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_one_message()
1303 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_one_message()
1305 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1310 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_transfer_one_message()
1316 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1317 xfer->ptp_sts_word_pre = 0; in spi_transfer_one_message()
1318 ptp_read_system_prets(xfer->ptp_sts); in spi_transfer_one_message()
1321 if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { in spi_transfer_one_message()
1322 reinit_completion(&ctlr->xfer_completion); in spi_transfer_one_message()
1325 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1327 if (ctlr->cur_msg_mapped && in spi_transfer_one_message()
1328 (xfer->error & SPI_TRANS_FAIL_NO_START)) { in spi_transfer_one_message()
1330 ctlr->fallback = true; in spi_transfer_one_message()
1331 xfer->error &= ~SPI_TRANS_FAIL_NO_START; in spi_transfer_one_message()
1339 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1340 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1347 msg->status = ret; in spi_transfer_one_message()
1350 if (xfer->len) in spi_transfer_one_message()
1351 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1353 xfer->len); in spi_transfer_one_message()
1356 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1357 ptp_read_system_postts(xfer->ptp_sts); in spi_transfer_one_message()
1358 xfer->ptp_sts_word_post = xfer->len; in spi_transfer_one_message()
1363 if (msg->status != -EINPROGRESS) in spi_transfer_one_message()
1368 if (xfer->cs_change) { in spi_transfer_one_message()
1369 if (list_is_last(&xfer->transfer_list, in spi_transfer_one_message()
1370 &msg->transfers)) { in spi_transfer_one_message()
1373 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1375 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1379 msg->actual_length += xfer->len; in spi_transfer_one_message()
1384 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1386 if (msg->status == -EINPROGRESS) in spi_transfer_one_message()
1387 msg->status = ret; in spi_transfer_one_message()
1389 if (msg->status && ctlr->handle_err) in spi_transfer_one_message()
1390 ctlr->handle_err(ctlr, msg); in spi_transfer_one_message()
1398 * spi_finalize_current_transfer - report completion of a transfer
1399 * @ctlr: the controller reporting completion
1401 * Called by SPI drivers using the core transfer_one_message()
1407 complete(&ctlr->xfer_completion); in spi_finalize_current_transfer()
1413 if (ctlr->auto_runtime_pm) { in spi_idle_runtime_pm()
1414 pm_runtime_mark_last_busy(ctlr->dev.parent); in spi_idle_runtime_pm()
1415 pm_runtime_put_autosuspend(ctlr->dev.parent); in spi_idle_runtime_pm()
1420 * __spi_pump_messages - function which processes spi message queue
1421 * @ctlr: controller to process queue for
1424 * This function checks if there is any spi message in the queue that
1441 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1444 if (ctlr->cur_msg) { in __spi_pump_messages()
1445 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1450 if (ctlr->idling) { in __spi_pump_messages()
1451 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_pump_messages()
1452 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1457 if (list_empty(&ctlr->queue) || !ctlr->running) { in __spi_pump_messages()
1458 if (!ctlr->busy) { in __spi_pump_messages()
1459 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1463 /* Defer any non-atomic teardown to the thread */ in __spi_pump_messages()
1465 if (!ctlr->dummy_rx && !ctlr->dummy_tx && in __spi_pump_messages()
1466 !ctlr->unprepare_transfer_hardware) { in __spi_pump_messages()
1468 ctlr->busy = false; in __spi_pump_messages()
1471 kthread_queue_work(ctlr->kworker, in __spi_pump_messages()
1472 &ctlr->pump_messages); in __spi_pump_messages()
1474 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1478 ctlr->busy = false; in __spi_pump_messages()
1479 ctlr->idling = true; in __spi_pump_messages()
1480 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1482 kfree(ctlr->dummy_rx); in __spi_pump_messages()
1483 ctlr->dummy_rx = NULL; in __spi_pump_messages()
1484 kfree(ctlr->dummy_tx); in __spi_pump_messages()
1485 ctlr->dummy_tx = NULL; in __spi_pump_messages()
1486 if (ctlr->unprepare_transfer_hardware && in __spi_pump_messages()
1487 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_pump_messages()
1488 dev_err(&ctlr->dev, in __spi_pump_messages()
1493 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1494 ctlr->idling = false; in __spi_pump_messages()
1495 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1500 msg = list_first_entry(&ctlr->queue, struct spi_message, queue); in __spi_pump_messages()
1501 ctlr->cur_msg = msg; in __spi_pump_messages()
1503 list_del_init(&msg->queue); in __spi_pump_messages()
1504 if (ctlr->busy) in __spi_pump_messages()
1507 ctlr->busy = true; in __spi_pump_messages()
1508 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1510 mutex_lock(&ctlr->io_mutex); in __spi_pump_messages()
1512 if (!was_busy && ctlr->auto_runtime_pm) { in __spi_pump_messages()
1513 ret = pm_runtime_get_sync(ctlr->dev.parent); in __spi_pump_messages()
1515 pm_runtime_put_noidle(ctlr->dev.parent); in __spi_pump_messages()
1516 dev_err(&ctlr->dev, "Failed to power device: %d\n", in __spi_pump_messages()
1518 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1526 if (!was_busy && ctlr->prepare_transfer_hardware) { in __spi_pump_messages()
1527 ret = ctlr->prepare_transfer_hardware(ctlr); in __spi_pump_messages()
1529 dev_err(&ctlr->dev, in __spi_pump_messages()
1533 if (ctlr->auto_runtime_pm) in __spi_pump_messages()
1534 pm_runtime_put(ctlr->dev.parent); in __spi_pump_messages()
1536 msg->status = ret; in __spi_pump_messages()
1539 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1546 if (ctlr->prepare_message) { in __spi_pump_messages()
1547 ret = ctlr->prepare_message(ctlr, msg); in __spi_pump_messages()
1549 dev_err(&ctlr->dev, "failed to prepare message: %d\n", in __spi_pump_messages()
1551 msg->status = ret; in __spi_pump_messages()
1555 ctlr->cur_msg_prepared = true; in __spi_pump_messages()
1560 msg->status = ret; in __spi_pump_messages()
1565 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in __spi_pump_messages()
1566 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_pump_messages()
1567 xfer->ptp_sts_word_pre = 0; in __spi_pump_messages()
1568 ptp_read_system_prets(xfer->ptp_sts); in __spi_pump_messages()
1572 ret = ctlr->transfer_one_message(ctlr, msg); in __spi_pump_messages()
1574 dev_err(&ctlr->dev, in __spi_pump_messages()
1580 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1588 * spi_pump_messages - kthread work function which processes spi message queue
1589 * @work: pointer to kthread work struct contained in the controller struct
1600 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1601 * TX timestamp for the requested byte from the SPI
1624 if (!xfer->ptp_sts) in spi_take_timestamp_pre()
1627 if (xfer->timestamped) in spi_take_timestamp_pre()
1630 if (progress > xfer->ptp_sts_word_pre) in spi_take_timestamp_pre()
1634 xfer->ptp_sts_word_pre = progress; in spi_take_timestamp_pre()
1637 local_irq_save(ctlr->irq_flags); in spi_take_timestamp_pre()
1641 ptp_read_system_prets(xfer->ptp_sts); in spi_take_timestamp_pre()
1646 * spi_take_timestamp_post - helper for drivers to collect the end of the
1647 * TX timestamp for the requested byte from the SPI
1655 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1661 if (!xfer->ptp_sts) in spi_take_timestamp_post()
1664 if (xfer->timestamped) in spi_take_timestamp_post()
1667 if (progress < xfer->ptp_sts_word_post) in spi_take_timestamp_post()
1670 ptp_read_system_postts(xfer->ptp_sts); in spi_take_timestamp_post()
1673 local_irq_restore(ctlr->irq_flags); in spi_take_timestamp_post()
1678 xfer->ptp_sts_word_post = progress; in spi_take_timestamp_post()
1680 xfer->timestamped = true; in spi_take_timestamp_post()
1685 * spi_set_thread_rt - set the controller to pump at realtime priority
1686 * @ctlr: controller to boost priority of
1688 * This can be called because the controller requested realtime priority
1689 * (by setting the ->rt value before calling spi_register_controller()) or
1695 * controller. If this eventually becomes a problem we may see if we can
1701 dev_info(&ctlr->dev, in spi_set_thread_rt()
1703 sched_set_fifo(ctlr->kworker->task); in spi_set_thread_rt()
1708 ctlr->running = false; in spi_init_queue()
1709 ctlr->busy = false; in spi_init_queue()
1711 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); in spi_init_queue()
1712 if (IS_ERR(ctlr->kworker)) { in spi_init_queue()
1713 dev_err(&ctlr->dev, "failed to create message pump kworker\n"); in spi_init_queue()
1714 return PTR_ERR(ctlr->kworker); in spi_init_queue()
1717 kthread_init_work(&ctlr->pump_messages, spi_pump_messages); in spi_init_queue()
1720 * Controller config will indicate if this controller should run the in spi_init_queue()
1726 if (ctlr->rt) in spi_init_queue()
1733 * spi_get_next_queued_message() - called by driver to check for queued
1735 * @ctlr: the controller to check for queued messages
1748 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1749 next = list_first_entry_or_null(&ctlr->queue, struct spi_message, in spi_get_next_queued_message()
1751 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1758 * spi_finalize_current_message() - the current message is complete
1759 * @ctlr: the controller to return the message to
1771 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1772 mesg = ctlr->cur_msg; in spi_finalize_current_message()
1773 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1775 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in spi_finalize_current_message()
1776 list_for_each_entry(xfer, &mesg->transfers, transfer_list) { in spi_finalize_current_message()
1777 ptp_read_system_postts(xfer->ptp_sts); in spi_finalize_current_message()
1778 xfer->ptp_sts_word_post = xfer->len; in spi_finalize_current_message()
1782 if (unlikely(ctlr->ptp_sts_supported)) in spi_finalize_current_message()
1783 list_for_each_entry(xfer, &mesg->transfers, transfer_list) in spi_finalize_current_message()
1784 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); in spi_finalize_current_message()
1788 /* In the prepare_messages callback the spi bus has the opportunity to in spi_finalize_current_message()
1795 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { in spi_finalize_current_message()
1796 ret = ctlr->unprepare_message(ctlr, mesg); in spi_finalize_current_message()
1798 dev_err(&ctlr->dev, "failed to unprepare message: %d\n", in spi_finalize_current_message()
1803 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1804 ctlr->cur_msg = NULL; in spi_finalize_current_message()
1805 ctlr->cur_msg_prepared = false; in spi_finalize_current_message()
1806 ctlr->fallback = false; in spi_finalize_current_message()
1807 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_finalize_current_message()
1808 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1812 mesg->state = NULL; in spi_finalize_current_message()
1813 if (mesg->complete) in spi_finalize_current_message()
1814 mesg->complete(mesg->context); in spi_finalize_current_message()
1822 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_start_queue()
1824 if (ctlr->running || ctlr->busy) { in spi_start_queue()
1825 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1826 return -EBUSY; in spi_start_queue()
1829 ctlr->running = true; in spi_start_queue()
1830 ctlr->cur_msg = NULL; in spi_start_queue()
1831 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1833 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_start_queue()
1844 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1848 * A wait_queue on the ctlr->busy could be used, but then the common in spi_stop_queue()
1850 * friends on every SPI message. Do this instead. in spi_stop_queue()
1852 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { in spi_stop_queue()
1853 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1855 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1858 if (!list_empty(&ctlr->queue) || ctlr->busy) in spi_stop_queue()
1859 ret = -EBUSY; in spi_stop_queue()
1861 ctlr->running = false; in spi_stop_queue()
1863 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1866 dev_warn(&ctlr->dev, "could not stop message queue\n"); in spi_stop_queue()
1885 dev_err(&ctlr->dev, "problem destroying queue\n"); in spi_destroy_queue()
1889 kthread_destroy_worker(ctlr->kworker); in spi_destroy_queue()
1894 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
1898 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
1901 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1903 if (!ctlr->running) { in __spi_queued_transfer()
1904 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1905 return -ESHUTDOWN; in __spi_queued_transfer()
1907 msg->actual_length = 0; in __spi_queued_transfer()
1908 msg->status = -EINPROGRESS; in __spi_queued_transfer()
1910 list_add_tail(&msg->queue, &ctlr->queue); in __spi_queued_transfer()
1911 if (!ctlr->busy && need_pump) in __spi_queued_transfer()
1912 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_queued_transfer()
1914 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1919 * spi_queued_transfer - transfer function for queued transfers
1920 * @spi: spi device which is requesting transfer
1921 * @msg: spi message which is to handled is queued to driver queue
1925 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
1927 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
1934 ctlr->transfer = spi_queued_transfer; in spi_controller_initialize_queue()
1935 if (!ctlr->transfer_one_message) in spi_controller_initialize_queue()
1936 ctlr->transfer_one_message = spi_transfer_one_message; in spi_controller_initialize_queue()
1941 dev_err(&ctlr->dev, "problem initializing queue\n"); in spi_controller_initialize_queue()
1944 ctlr->queued = true; in spi_controller_initialize_queue()
1947 dev_err(&ctlr->dev, "problem starting queue\n"); in spi_controller_initialize_queue()
1960 * spi_flush_queue - Send all pending messages in the queue from the callers'
1962 * @ctlr: controller to process queue for
1965 * sent before doing something. Is used by the spi-mem code to make sure SPI
1966 * memory operations do not preempt regular SPI transfers that have been queued
1967 * before the spi-mem operation.
1971 if (ctlr->transfer == spi_queued_transfer) in spi_flush_queue()
1975 /*-------------------------------------------------------------------------*/
1978 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
1985 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
1986 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
1987 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
1988 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
1989 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
1990 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
1991 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
1992 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
1993 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
1994 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
1997 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
2000 spi->mode |= SPI_NO_TX; in of_spi_parse_dt()
2005 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
2008 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
2011 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
2014 dev_warn(&ctlr->dev, in of_spi_parse_dt()
2015 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
2021 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
2024 spi->mode |= SPI_NO_RX; in of_spi_parse_dt()
2029 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
2032 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
2035 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
2038 dev_warn(&ctlr->dev, in of_spi_parse_dt()
2039 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
2047 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", in of_spi_parse_dt()
2049 return -EINVAL; in of_spi_parse_dt()
2057 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", in of_spi_parse_dt()
2061 spi->chip_select = value; in of_spi_parse_dt()
2064 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2065 spi->max_speed_hz = value; in of_spi_parse_dt()
2073 struct spi_device *spi; in of_register_spi_device() local
2077 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2078 if (!spi) { in of_register_spi_device()
2079 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); in of_register_spi_device()
2080 rc = -ENOMEM; in of_register_spi_device()
2085 rc = of_modalias_node(nc, spi->modalias, in of_register_spi_device()
2086 sizeof(spi->modalias)); in of_register_spi_device()
2088 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); in of_register_spi_device()
2092 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2098 spi->dev.of_node = nc; in of_register_spi_device()
2099 spi->dev.fwnode = of_fwnode_handle(nc); in of_register_spi_device()
2102 rc = spi_add_device(spi); in of_register_spi_device()
2104 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); in of_register_spi_device()
2108 return spi; in of_register_spi_device()
2113 spi_dev_put(spi); in of_register_spi_device()
2118 * of_register_spi_devices() - Register child devices onto the SPI bus
2121 * Registers an spi_device for each child node of controller node which
2122 * represents a valid SPI slave.
2126 struct spi_device *spi; in of_register_spi_devices() local
2129 if (!ctlr->dev.of_node) in of_register_spi_devices()
2132 for_each_available_child_of_node(ctlr->dev.of_node, nc) { in of_register_spi_devices()
2135 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2136 if (IS_ERR(spi)) { in of_register_spi_devices()
2137 dev_warn(&ctlr->dev, in of_register_spi_devices()
2138 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2148 * spi_new_ancillary_device() - Register ancillary SPI device
2149 * @spi: Pointer to the main SPI device registering the ancillary device
2152 * Register an ancillary SPI device; for example some chips have a chip-select
2155 * This may only be called from main SPI device's probe routine.
2159 struct spi_device *spi_new_ancillary_device(struct spi_device *spi, in spi_new_ancillary_device() argument
2166 ancillary = spi_alloc_device(spi->controller); in spi_new_ancillary_device()
2168 rc = -ENOMEM; in spi_new_ancillary_device()
2172 strlcpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias)); in spi_new_ancillary_device()
2174 /* Use provided chip-select for ancillary device */ in spi_new_ancillary_device()
2175 ancillary->chip_select = chip_select; in spi_new_ancillary_device()
2177 /* Take over SPI mode/speed from SPI main device */ in spi_new_ancillary_device()
2178 ancillary->max_speed_hz = spi->max_speed_hz; in spi_new_ancillary_device()
2179 ancillary->mode = spi->mode; in spi_new_ancillary_device()
2184 dev_err(&spi->dev, "failed to register ancillary device\n"); in spi_new_ancillary_device()
2215 && obj->buffer.length >= 4) in acpi_spi_parse_apple_properties()
2216 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2219 && obj->buffer.length == 8) in acpi_spi_parse_apple_properties()
2220 lookup->bits_per_word = *(u64 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2223 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2224 lookup->mode |= SPI_LSB_FIRST; in acpi_spi_parse_apple_properties()
2227 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2228 lookup->mode |= SPI_CPOL; in acpi_spi_parse_apple_properties()
2231 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2232 lookup->mode |= SPI_CPHA; in acpi_spi_parse_apple_properties()
2238 struct spi_controller *ctlr = lookup->ctlr; in acpi_spi_add_resource()
2240 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { in acpi_spi_add_resource()
2245 sb = &ares->data.spi_serial_bus; in acpi_spi_add_resource()
2246 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { in acpi_spi_add_resource()
2249 sb->resource_source.string_ptr, in acpi_spi_add_resource()
2253 ACPI_HANDLE(ctlr->dev.parent) != parent_handle) in acpi_spi_add_resource()
2254 return -ENODEV; in acpi_spi_add_resource()
2258 * host controller driver in Windows and can vary in acpi_spi_add_resource()
2260 * 0 .. max - 1 so we need to ask the driver to in acpi_spi_add_resource()
2263 if (ctlr->fw_translate_cs) { in acpi_spi_add_resource()
2264 int cs = ctlr->fw_translate_cs(ctlr, in acpi_spi_add_resource()
2265 sb->device_selection); in acpi_spi_add_resource()
2268 lookup->chip_select = cs; in acpi_spi_add_resource()
2270 lookup->chip_select = sb->device_selection; in acpi_spi_add_resource()
2273 lookup->max_speed_hz = sb->connection_speed; in acpi_spi_add_resource()
2274 lookup->bits_per_word = sb->data_bit_length; in acpi_spi_add_resource()
2276 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) in acpi_spi_add_resource()
2277 lookup->mode |= SPI_CPHA; in acpi_spi_add_resource()
2278 if (sb->clock_polarity == ACPI_SPI_START_HIGH) in acpi_spi_add_resource()
2279 lookup->mode |= SPI_CPOL; in acpi_spi_add_resource()
2280 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) in acpi_spi_add_resource()
2281 lookup->mode |= SPI_CS_HIGH; in acpi_spi_add_resource()
2283 } else if (lookup->irq < 0) { in acpi_spi_add_resource()
2287 lookup->irq = r.start; in acpi_spi_add_resource()
2300 struct spi_device *spi; in acpi_register_spi_device() local
2303 if (acpi_bus_get_status(adev) || !adev->status.present || in acpi_register_spi_device()
2308 lookup.irq = -1; in acpi_register_spi_device()
2316 /* found SPI in _CRS but it points to another controller */ in acpi_register_spi_device()
2320 ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && in acpi_register_spi_device()
2321 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { in acpi_register_spi_device()
2322 /* Apple does not use _CRS but nested devices for SPI slaves */ in acpi_register_spi_device()
2329 spi = spi_alloc_device(ctlr); in acpi_register_spi_device()
2330 if (!spi) { in acpi_register_spi_device()
2331 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_register_spi_device()
2332 dev_name(&adev->dev)); in acpi_register_spi_device()
2337 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_register_spi_device()
2338 spi->max_speed_hz = lookup.max_speed_hz; in acpi_register_spi_device()
2339 spi->mode |= lookup.mode; in acpi_register_spi_device()
2340 spi->irq = lookup.irq; in acpi_register_spi_device()
2341 spi->bits_per_word = lookup.bits_per_word; in acpi_register_spi_device()
2342 spi->chip_select = lookup.chip_select; in acpi_register_spi_device()
2344 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2345 sizeof(spi->modalias)); in acpi_register_spi_device()
2347 if (spi->irq < 0) in acpi_register_spi_device()
2348 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in acpi_register_spi_device()
2352 adev->power.flags.ignore_parent = true; in acpi_register_spi_device()
2353 if (spi_add_device(spi)) { in acpi_register_spi_device()
2354 adev->power.flags.ignore_parent = false; in acpi_register_spi_device()
2355 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2356 dev_name(&adev->dev)); in acpi_register_spi_device()
2357 spi_dev_put(spi); in acpi_register_spi_device()
2382 handle = ACPI_HANDLE(ctlr->dev.parent); in acpi_register_spi_devices()
2390 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2413 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2414 * controller
2415 * @spi: device used for the current transfer
2417 int spi_slave_abort(struct spi_device *spi) in spi_slave_abort() argument
2419 struct spi_controller *ctlr = spi->controller; in spi_slave_abort()
2421 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) in spi_slave_abort()
2422 return ctlr->slave_abort(ctlr); in spi_slave_abort()
2424 return -ENOTSUPP; in spi_slave_abort()
2440 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_show()
2442 child ? to_spi_device(child)->modalias : NULL); in slave_show()
2450 struct spi_device *spi; in slave_store() local
2457 return -EINVAL; in slave_store()
2459 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_store()
2468 spi = spi_alloc_device(ctlr); in slave_store()
2469 if (!spi) in slave_store()
2470 return -ENOMEM; in slave_store()
2472 strlcpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2474 rc = spi_add_device(spi); in slave_store()
2476 spi_dev_put(spi); in slave_store()
2512 * __spi_alloc_controller - allocate an SPI master or slave controller
2513 * @dev: the controller, possibly using the platform_bus
2514 * @size: how much zeroed driver-private data to allocate; the pointer to this
2519 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2520 * slave (true) controller
2523 * This call is used only by SPI controller drivers, which are the
2530 * controller's methods before calling spi_register_controller(); and (after
2534 * Return: the SPI controller structure on success, else NULL.
2549 device_initialize(&ctlr->dev); in __spi_alloc_controller()
2550 INIT_LIST_HEAD(&ctlr->queue); in __spi_alloc_controller()
2551 spin_lock_init(&ctlr->queue_lock); in __spi_alloc_controller()
2552 spin_lock_init(&ctlr->bus_lock_spinlock); in __spi_alloc_controller()
2553 mutex_init(&ctlr->bus_lock_mutex); in __spi_alloc_controller()
2554 mutex_init(&ctlr->io_mutex); in __spi_alloc_controller()
2555 mutex_init(&ctlr->add_lock); in __spi_alloc_controller()
2556 ctlr->bus_num = -1; in __spi_alloc_controller()
2557 ctlr->num_chipselect = 1; in __spi_alloc_controller()
2558 ctlr->slave = slave; in __spi_alloc_controller()
2560 ctlr->dev.class = &spi_slave_class; in __spi_alloc_controller()
2562 ctlr->dev.class = &spi_master_class; in __spi_alloc_controller()
2563 ctlr->dev.parent = dev; in __spi_alloc_controller()
2564 pm_suspend_ignore_children(&ctlr->dev, true); in __spi_alloc_controller()
2577 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
2578 * @dev: physical device of SPI controller
2579 * @size: how much zeroed driver-private data to allocate
2580 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
2583 * Allocate an SPI controller and automatically release a reference on it
2589 * Return: the SPI controller structure on success, else NULL.
2604 ctlr->devm_allocated = true; in __devm_spi_alloc_controller()
2619 struct device_node *np = ctlr->dev.of_node; in of_spi_get_gpio_numbers()
2624 nb = of_gpio_named_count(np, "cs-gpios"); in of_spi_get_gpio_numbers()
2625 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in of_spi_get_gpio_numbers()
2627 /* Return error only for an incorrectly formed cs-gpios property */ in of_spi_get_gpio_numbers()
2628 if (nb == 0 || nb == -ENOENT) in of_spi_get_gpio_numbers()
2633 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), in of_spi_get_gpio_numbers()
2635 ctlr->cs_gpios = cs; in of_spi_get_gpio_numbers()
2637 if (!ctlr->cs_gpios) in of_spi_get_gpio_numbers()
2638 return -ENOMEM; in of_spi_get_gpio_numbers()
2640 for (i = 0; i < ctlr->num_chipselect; i++) in of_spi_get_gpio_numbers()
2641 cs[i] = -ENOENT; in of_spi_get_gpio_numbers()
2644 cs[i] = of_get_named_gpio(np, "cs-gpios", i); in of_spi_get_gpio_numbers()
2656 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2657 * @ctlr: The SPI master to grab GPIO descriptors for
2663 struct device *dev = &ctlr->dev; in spi_get_gpio_descs()
2670 if (nb == -ENOENT) in spi_get_gpio_descs()
2675 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in spi_get_gpio_descs()
2677 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), in spi_get_gpio_descs()
2680 return -ENOMEM; in spi_get_gpio_descs()
2681 ctlr->cs_gpiods = cs; in spi_get_gpio_descs()
2706 return -ENOMEM; in spi_get_gpio_descs()
2712 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2714 return -EINVAL; in spi_get_gpio_descs()
2719 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; in spi_get_gpio_descs()
2721 if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && in spi_get_gpio_descs()
2722 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2724 return -EINVAL; in spi_get_gpio_descs()
2733 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
2734 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
2736 * If ->mem_ops is NULL, we request that at least one of the in spi_controller_check_ops()
2737 * ->transfer_xxx() method be implemented. in spi_controller_check_ops()
2739 if (ctlr->mem_ops) { in spi_controller_check_ops()
2740 if (!ctlr->mem_ops->exec_op) in spi_controller_check_ops()
2741 return -EINVAL; in spi_controller_check_ops()
2742 } else if (!ctlr->transfer && !ctlr->transfer_one && in spi_controller_check_ops()
2743 !ctlr->transfer_one_message) { in spi_controller_check_ops()
2744 return -EINVAL; in spi_controller_check_ops()
2751 * spi_register_controller - register SPI master or slave controller
2756 * SPI controllers connect to their drivers using some non-SPI bus,
2758 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2760 * SPI controllers use board specific (often SOC specific) bus numbers,
2761 * and board-specific addressing for SPI devices combines those numbers
2762 * with chip select numbers. Since SPI does not directly support dynamic
2767 * success, else a negative error code (dropping the controller's refcount).
2775 struct device *dev = ctlr->dev.parent; in spi_register_controller()
2781 return -ENODEV; in spi_register_controller()
2785 * the SPI controller. in spi_register_controller()
2791 if (ctlr->bus_num >= 0) { in spi_register_controller()
2792 /* devices with a fixed bus num must check-in with the num */ in spi_register_controller()
2794 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2795 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2798 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2799 ctlr->bus_num = id; in spi_register_controller()
2800 } else if (ctlr->dev.of_node) { in spi_register_controller()
2802 id = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
2804 ctlr->bus_num = id; in spi_register_controller()
2806 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2807 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2810 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2813 if (ctlr->bus_num < 0) { in spi_register_controller()
2814 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
2826 ctlr->bus_num = id; in spi_register_controller()
2828 ctlr->bus_lock_flag = 0; in spi_register_controller()
2829 init_completion(&ctlr->xfer_completion); in spi_register_controller()
2830 if (!ctlr->max_dma_len) in spi_register_controller()
2831 ctlr->max_dma_len = INT_MAX; in spi_register_controller()
2836 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
2839 if (ctlr->use_gpio_descriptors) { in spi_register_controller()
2844 * A controller using GPIO descriptors always in spi_register_controller()
2847 ctlr->mode_bits |= SPI_CS_HIGH; in spi_register_controller()
2857 * Even if it's just one always-selected device, there must in spi_register_controller()
2860 if (!ctlr->num_chipselect) { in spi_register_controller()
2861 status = -EINVAL; in spi_register_controller()
2865 status = device_add(&ctlr->dev); in spi_register_controller()
2870 dev_name(&ctlr->dev)); in spi_register_controller()
2874 * need the queueing logic if the driver is only supporting high-level in spi_register_controller()
2877 if (ctlr->transfer) { in spi_register_controller()
2878 dev_info(dev, "controller is unqueued, this is deprecated\n"); in spi_register_controller()
2879 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { in spi_register_controller()
2882 device_del(&ctlr->dev); in spi_register_controller()
2887 spin_lock_init(&ctlr->statistics.lock); in spi_register_controller()
2890 list_add_tail(&ctlr->list, &spi_controller_list); in spi_register_controller()
2892 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); in spi_register_controller()
2902 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
2914 * devm_spi_register_controller - register managed SPI master or slave
2915 * controller
2916 * @dev: device managing SPI controller
2917 * @ctlr: initialized controller, originally from spi_alloc_master() or
2921 * Register a SPI device as with spi_register_controller() which will
2946 * spi_unregister_controller - unregister SPI master or slave controller
2947 * @ctlr: the controller being unregistered
2950 * This call is used only by SPI controller drivers, which are the
2955 * Note that this function also drops a reference to the controller.
2960 int id = ctlr->bus_num; in spi_unregister_controller()
2964 mutex_lock(&ctlr->add_lock); in spi_unregister_controller()
2966 device_for_each_child(&ctlr->dev, NULL, __unregister); in spi_unregister_controller()
2968 /* First make sure that this controller was ever added */ in spi_unregister_controller()
2972 if (ctlr->queued) { in spi_unregister_controller()
2974 dev_err(&ctlr->dev, "queue remove failed\n"); in spi_unregister_controller()
2977 list_del(&ctlr->list); in spi_unregister_controller()
2980 device_del(&ctlr->dev); in spi_unregister_controller()
2982 /* Release the last reference on the controller if its driver in spi_unregister_controller()
2985 if (!ctlr->devm_allocated) in spi_unregister_controller()
2986 put_device(&ctlr->dev); in spi_unregister_controller()
2995 mutex_unlock(&ctlr->add_lock); in spi_unregister_controller()
3003 /* Basically no-ops for non-queued controllers */ in spi_controller_suspend()
3004 if (!ctlr->queued) in spi_controller_suspend()
3009 dev_err(&ctlr->dev, "queue stop failed\n"); in spi_controller_suspend()
3019 if (!ctlr->queued) in spi_controller_resume()
3024 dev_err(&ctlr->dev, "queue restart failed\n"); in spi_controller_resume()
3036 return ctlr->bus_num == *bus_num; in __spi_controller_match()
3040 * spi_busnum_to_master - look up master associated with bus_num
3049 * Return: the SPI master structure on success, else NULL.
3065 /*-------------------------------------------------------------------------*/
3067 /* Core methods for SPI resource management */
3070 * spi_res_alloc - allocate a spi resource that is life-cycle managed
3073 * @spi: the spi device for which we allocate memory
3083 void *spi_res_alloc(struct spi_device *spi, in spi_res_alloc() argument
3093 INIT_LIST_HEAD(&sres->entry); in spi_res_alloc()
3094 sres->release = release; in spi_res_alloc()
3096 return sres->data; in spi_res_alloc()
3101 * spi_res_free - free an spi resource
3112 WARN_ON(!list_empty(&sres->entry)); in spi_res_free()
3118 * spi_res_add - add a spi_res to the spi_message
3119 * @message: the spi message
3126 WARN_ON(!list_empty(&sres->entry)); in spi_res_add()
3127 list_add_tail(&sres->entry, &message->resources); in spi_res_add()
3132 * spi_res_release - release all spi resources for this message
3140 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { in spi_res_release()
3141 if (res->release) in spi_res_release()
3142 res->release(ctlr, message, res->data); in spi_res_release()
3144 list_del(&res->entry); in spi_res_release()
3151 /*-------------------------------------------------------------------------*/
3163 if (rxfer->release) in __spi_replace_transfers_release()
3164 rxfer->release(ctlr, msg, res); in __spi_replace_transfers_release()
3167 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); in __spi_replace_transfers_release()
3170 for (i = 0; i < rxfer->inserted; i++) in __spi_replace_transfers_release()
3171 list_del(&rxfer->inserted_transfers[i].transfer_list); in __spi_replace_transfers_release()
3175 * spi_replace_transfers - replace transfers with several transfers
3203 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3208 return ERR_PTR(-ENOMEM); in spi_replace_transfers()
3211 rxfer->release = release; in spi_replace_transfers()
3215 rxfer->extradata = in spi_replace_transfers()
3216 &rxfer->inserted_transfers[insert]; in spi_replace_transfers()
3219 INIT_LIST_HEAD(&rxfer->replaced_transfers); in spi_replace_transfers()
3222 * the @replaced_transfers - it may be spi_message.messages! in spi_replace_transfers()
3224 rxfer->replaced_after = xfer_first->transfer_list.prev; in spi_replace_transfers()
3228 /* if the entry after replaced_after it is msg->transfers in spi_replace_transfers()
3232 if (rxfer->replaced_after->next == &msg->transfers) { in spi_replace_transfers()
3233 dev_err(&msg->spi->dev, in spi_replace_transfers()
3236 list_splice(&rxfer->replaced_transfers, in spi_replace_transfers()
3237 rxfer->replaced_after); in spi_replace_transfers()
3243 return ERR_PTR(-EINVAL); in spi_replace_transfers()
3249 list_move_tail(rxfer->replaced_after->next, in spi_replace_transfers()
3250 &rxfer->replaced_transfers); in spi_replace_transfers()
3258 xfer = &rxfer->inserted_transfers[insert - 1 - i]; in spi_replace_transfers()
3264 list_add(&xfer->transfer_list, rxfer->replaced_after); in spi_replace_transfers()
3268 xfer->cs_change = false; in spi_replace_transfers()
3269 xfer->delay.value = 0; in spi_replace_transfers()
3274 rxfer->inserted = insert; in spi_replace_transfers()
3295 count = DIV_ROUND_UP(xfer->len, maxsize); in __spi_split_transfer_maxsize()
3301 xfers = srt->inserted_transfers; in __spi_split_transfer_maxsize()
3331 xfers[i].len = min(maxsize, xfers[i].len - offset); in __spi_split_transfer_maxsize()
3337 *xferp = &xfers[count - 1]; in __spi_split_transfer_maxsize()
3340 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_split_transfer_maxsize()
3342 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, in __spi_split_transfer_maxsize()
3349 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3373 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxsize()
3374 if (xfer->len > maxsize) { in spi_split_transfers_maxsize()
3386 /*-------------------------------------------------------------------------*/
3388 /* Core methods for SPI controller protocol drivers. Some of the
3395 if (ctlr->bits_per_word_mask) { in __spi_validate_bits_per_word()
3398 return -EINVAL; in __spi_validate_bits_per_word()
3399 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) in __spi_validate_bits_per_word()
3400 return -EINVAL; in __spi_validate_bits_per_word()
3407 * spi_setup - setup SPI mode and clock rate
3408 * @spi: the device whose settings are being modified
3411 * SPI protocol drivers may need to update the transfer mode if the
3417 * or from it. When this function returns, the spi device is deselected.
3420 * that the underlying controller or its driver does not support. For
3422 * LSB-first wire encoding, or active-high chipselects.
3426 int spi_setup(struct spi_device *spi) in spi_setup() argument
3435 if ((hweight_long(spi->mode & in spi_setup()
3437 (hweight_long(spi->mode & in spi_setup()
3439 dev_err(&spi->dev, in spi_setup()
3440 "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); in spi_setup()
3441 return -EINVAL; in spi_setup()
3445 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3448 return -EINVAL; in spi_setup()
3450 * that aren't supported with their current controller in spi_setup()
3454 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | in spi_setup()
3456 /* nothing prevents from working with active-high CS in case if it in spi_setup()
3459 if (gpio_is_valid(spi->cs_gpio)) in spi_setup()
3465 dev_warn(&spi->dev, in spi_setup()
3468 spi->mode &= ~ugly_bits; in spi_setup()
3472 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3474 return -EINVAL; in spi_setup()
3477 if (!spi->bits_per_word) in spi_setup()
3478 spi->bits_per_word = 8; in spi_setup()
3480 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3481 spi->bits_per_word); in spi_setup()
3485 if (spi->controller->max_speed_hz && in spi_setup()
3486 (!spi->max_speed_hz || in spi_setup()
3487 spi->max_speed_hz > spi->controller->max_speed_hz)) in spi_setup()
3488 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3490 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3492 if (spi->controller->setup) { in spi_setup()
3493 status = spi->controller->setup(spi); in spi_setup()
3495 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3496 dev_err(&spi->controller->dev, "Failed to setup device: %d\n", in spi_setup()
3502 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3503 status = pm_runtime_get_sync(spi->controller->dev.parent); in spi_setup()
3505 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3506 pm_runtime_put_noidle(spi->controller->dev.parent); in spi_setup()
3507 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3515 * checking for a non-zero return value instead of a negative in spi_setup()
3520 spi_set_cs(spi, false, true); in spi_setup()
3521 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3522 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
3524 spi_set_cs(spi, false, true); in spi_setup()
3527 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3529 if (spi->rt && !spi->controller->rt) { in spi_setup()
3530 spi->controller->rt = true; in spi_setup()
3531 spi_set_thread_rt(spi->controller); in spi_setup()
3534 trace_spi_setup(spi, status); in spi_setup()
3536 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
3537 spi->mode & SPI_MODE_X_MASK, in spi_setup()
3538 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
3539 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
3540 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
3541 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
3542 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
3550 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
3554 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); in _spi_xfer_word_delay_update()
3558 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
3563 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
3564 sizeof(xfer->word_delay)); in _spi_xfer_word_delay_update()
3569 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
3571 struct spi_controller *ctlr = spi->controller; in __spi_validate()
3575 if (list_empty(&message->transfers)) in __spi_validate()
3576 return -EINVAL; in __spi_validate()
3578 /* If an SPI controller does not support toggling the CS line on each in __spi_validate()
3580 * for the CS line, we can emulate the CS-per-word hardware function by in __spi_validate()
3581 * splitting transfers into one-word transfers and ensuring that in __spi_validate()
3584 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || in __spi_validate()
3585 spi->cs_gpiod || in __spi_validate()
3586 gpio_is_valid(spi->cs_gpio))) { in __spi_validate()
3590 maxsize = (spi->bits_per_word + 7) / 8; in __spi_validate()
3592 /* spi_split_transfers_maxsize() requires message->spi */ in __spi_validate()
3593 message->spi = spi; in __spi_validate()
3600 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3602 if (list_is_last(&xfer->transfer_list, &message->transfers)) in __spi_validate()
3604 xfer->cs_change = 1; in __spi_validate()
3608 /* Half-duplex links include original MicroWire, and ones with in __spi_validate()
3613 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || in __spi_validate()
3614 (spi->mode & SPI_3WIRE)) { in __spi_validate()
3615 unsigned flags = ctlr->flags; in __spi_validate()
3617 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3618 if (xfer->rx_buf && xfer->tx_buf) in __spi_validate()
3619 return -EINVAL; in __spi_validate()
3620 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) in __spi_validate()
3621 return -EINVAL; in __spi_validate()
3622 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) in __spi_validate()
3623 return -EINVAL; in __spi_validate()
3628 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
3635 message->frame_length = 0; in __spi_validate()
3636 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3637 xfer->effective_speed_hz = 0; in __spi_validate()
3638 message->frame_length += xfer->len; in __spi_validate()
3639 if (!xfer->bits_per_word) in __spi_validate()
3640 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
3642 if (!xfer->speed_hz) in __spi_validate()
3643 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
3645 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) in __spi_validate()
3646 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
3648 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) in __spi_validate()
3649 return -EINVAL; in __spi_validate()
3652 * SPI transfer length should be multiple of SPI word size in __spi_validate()
3653 * where SPI word size should be power-of-two multiple in __spi_validate()
3655 if (xfer->bits_per_word <= 8) in __spi_validate()
3657 else if (xfer->bits_per_word <= 16) in __spi_validate()
3663 if (xfer->len % w_size) in __spi_validate()
3664 return -EINVAL; in __spi_validate()
3666 if (xfer->speed_hz && ctlr->min_speed_hz && in __spi_validate()
3667 xfer->speed_hz < ctlr->min_speed_hz) in __spi_validate()
3668 return -EINVAL; in __spi_validate()
3670 if (xfer->tx_buf && !xfer->tx_nbits) in __spi_validate()
3671 xfer->tx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3672 if (xfer->rx_buf && !xfer->rx_nbits) in __spi_validate()
3673 xfer->rx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3678 if (xfer->tx_buf) { in __spi_validate()
3679 if (spi->mode & SPI_NO_TX) in __spi_validate()
3680 return -EINVAL; in __spi_validate()
3681 if (xfer->tx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3682 xfer->tx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3683 xfer->tx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3684 return -EINVAL; in __spi_validate()
3685 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3686 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
3687 return -EINVAL; in __spi_validate()
3688 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3689 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
3690 return -EINVAL; in __spi_validate()
3693 if (xfer->rx_buf) { in __spi_validate()
3694 if (spi->mode & SPI_NO_RX) in __spi_validate()
3695 return -EINVAL; in __spi_validate()
3696 if (xfer->rx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3697 xfer->rx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3698 xfer->rx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3699 return -EINVAL; in __spi_validate()
3700 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3701 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
3702 return -EINVAL; in __spi_validate()
3703 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3704 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
3705 return -EINVAL; in __spi_validate()
3708 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
3709 return -EINVAL; in __spi_validate()
3712 message->status = -EINPROGRESS; in __spi_validate()
3717 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
3719 struct spi_controller *ctlr = spi->controller; in __spi_async()
3723 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
3726 if (!ctlr->transfer) in __spi_async()
3727 return -ENOTSUPP; in __spi_async()
3729 message->spi = spi; in __spi_async()
3731 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); in __spi_async()
3732 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); in __spi_async()
3736 if (!ctlr->ptp_sts_supported) { in __spi_async()
3737 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_async()
3738 xfer->ptp_sts_word_pre = 0; in __spi_async()
3739 ptp_read_system_prets(xfer->ptp_sts); in __spi_async()
3743 return ctlr->transfer(spi, message); in __spi_async()
3747 * spi_async - asynchronous SPI transfer
3748 * @spi: device with which data will be exchanged
3756 * Before that invocation, the value of message->status is undefined.
3757 * When the callback is issued, message->status holds either zero (to
3760 * deallocate the associated memory; it's no longer in use by any SPI
3761 * core or controller driver code.
3777 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
3779 struct spi_controller *ctlr = spi->controller; in spi_async()
3783 ret = __spi_validate(spi, message); in spi_async()
3787 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async()
3789 if (ctlr->bus_lock_flag) in spi_async()
3790 ret = -EBUSY; in spi_async()
3792 ret = __spi_async(spi, message); in spi_async()
3794 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async()
3801 * spi_async_locked - version of spi_async with exclusive bus usage
3802 * @spi: device with which data will be exchanged
3810 * Before that invocation, the value of message->status is undefined.
3811 * When the callback is issued, message->status holds either zero (to
3814 * deallocate the associated memory; it's no longer in use by any SPI
3815 * core or controller driver code.
3831 int spi_async_locked(struct spi_device *spi, struct spi_message *message) in spi_async_locked() argument
3833 struct spi_controller *ctlr = spi->controller; in spi_async_locked()
3837 ret = __spi_validate(spi, message); in spi_async_locked()
3841 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3843 ret = __spi_async(spi, message); in spi_async_locked()
3845 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3852 /*-------------------------------------------------------------------------*/
3854 /* Utility methods for SPI protocol drivers, layered on
3864 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
3868 struct spi_controller *ctlr = spi->controller; in __spi_sync()
3871 status = __spi_validate(spi, message); in __spi_sync()
3875 message->complete = spi_complete; in __spi_sync()
3876 message->context = &done; in __spi_sync()
3877 message->spi = spi; in __spi_sync()
3879 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); in __spi_sync()
3880 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); in __spi_sync()
3887 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3888 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3892 status = __spi_queued_transfer(spi, message, false); in __spi_sync()
3894 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3896 status = spi_async_locked(spi, message); in __spi_sync()
3903 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3904 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_sync()
3906 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, in __spi_sync()
3912 status = message->status; in __spi_sync()
3914 message->context = NULL; in __spi_sync()
3919 * spi_sync - blocking/synchronous SPI data transfers
3920 * @spi: device with which data will be exchanged
3925 * is non-interruptible, and has no timeout. Low-overhead controller
3928 * Note that the SPI device's chip select is active during the message,
3930 * frequently-used devices may want to minimize costs of selecting a chip,
3939 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
3943 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
3944 ret = __spi_sync(spi, message); in spi_sync()
3945 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
3952 * spi_sync_locked - version of spi_sync with exclusive bus usage
3953 * @spi: device with which data will be exchanged
3958 * is non-interruptible, and has no timeout. Low-overhead controller
3962 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3967 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
3969 return __spi_sync(spi, message); in spi_sync_locked()
3974 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3975 * @ctlr: SPI bus master that should be locked for exclusive bus access
3979 * is non-interruptible, and has no timeout.
3982 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3984 * and spi_async_locked calls when the SPI bus lock is held.
3992 mutex_lock(&ctlr->bus_lock_mutex); in spi_bus_lock()
3994 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3995 ctlr->bus_lock_flag = 1; in spi_bus_lock()
3996 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
4005 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4006 * @ctlr: SPI bus master that was locked for exclusive bus access
4010 * is non-interruptible, and has no timeout.
4012 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4019 ctlr->bus_lock_flag = 0; in spi_bus_unlock()
4021 mutex_unlock(&ctlr->bus_lock_mutex); in spi_bus_unlock()
4033 * spi_write_then_read - SPI synchronous write followed by read
4034 * @spi: device with which data will be exchanged
4035 * @txbuf: data to be written (need not be dma-safe)
4037 * @rxbuf: buffer into which data will be read (need not be dma-safe)
4047 * Performance-sensitive or bulk transfer code should instead use
4048 * spi_{async,sync}() calls with dma-safe buffers.
4052 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4063 /* Use preallocated DMA-safe buffer if we can. We can't avoid in spi_write_then_read()
4066 * using the pre-allocated buffer or the transfer is too large. in spi_write_then_read()
4072 return -ENOMEM; in spi_write_then_read()
4093 status = spi_sync(spi, &message); in spi_write_then_read()
4106 /*-------------------------------------------------------------------------*/
4120 /* the spi controllers are not using spi_bus, so we find it with another way */
4140 struct spi_device *spi; in of_spi_notify() local
4144 ctlr = of_find_spi_controller_by_node(rd->dn->parent); in of_spi_notify()
4148 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { in of_spi_notify()
4149 put_device(&ctlr->dev); in of_spi_notify()
4153 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4154 put_device(&ctlr->dev); in of_spi_notify()
4156 if (IS_ERR(spi)) { in of_spi_notify()
4158 __func__, rd->dn); in of_spi_notify()
4159 of_node_clear_flag(rd->dn, OF_POPULATED); in of_spi_notify()
4160 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4166 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_spi_notify()
4170 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4171 if (spi == NULL) in of_spi_notify()
4175 spi_unregister_device(spi); in of_spi_notify()
4178 put_device(&spi->dev); in of_spi_notify()
4195 return ACPI_COMPANION(dev->parent) == data; in spi_acpi_controller_match()
4226 struct spi_device *spi; in acpi_spi_notify() local
4230 ctlr = acpi_spi_find_controller_by_adev(adev->parent); in acpi_spi_notify()
4235 put_device(&ctlr->dev); in acpi_spi_notify()
4241 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4242 if (!spi) in acpi_spi_notify()
4245 spi_unregister_device(spi); in acpi_spi_notify()
4246 put_device(&spi->dev); in acpi_spi_notify()
4266 status = -ENOMEM; in spi_init()