Lines Matching +full:spi +full:- +full:device
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPI init/core code
8 #include <linux/device.h>
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>
46 static void spidev_release(struct device *dev) in spidev_release()
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()
56 modalias_show(struct device *dev, struct device_attribute *a, char *buf) in modalias_show()
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()
69 static ssize_t driver_override_store(struct device *dev, in driver_override_store()
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()
101 static ssize_t driver_override_show(struct device *dev, in driver_override_show()
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()
115 static ssize_t spi_controller_##field##_show(struct device *dev, \
121 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
127 static ssize_t spi_device_##field##_show(struct device *dev, \
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()
338 static int spi_match_device(struct device *dev, struct device_driver *drv) in spi_match_device()
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()
361 static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) in spi_uevent()
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",
382 static int spi_drv_probe(struct device *dev) in spi_drv_probe()
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()
413 static int spi_drv_remove(struct device *dev) in spi_drv_remove()
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()
425 static void spi_drv_shutdown(struct device *dev) in spi_drv_shutdown()
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
482 * spi_alloc_device - Allocate a new SPI device
483 * @ctlr: Controller to which device is connected
488 * fill the spi_device with device parameters before calling
492 * spi_device structure to add it to the SPI controller. If the caller
496 * Return: a pointer to the new device, or NULL.
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()
538 static int spi_dev_check(struct device *dev, void *data) in spi_dev_check()
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()
580 /* We need to make sure there's no other device with this 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()
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()
607 * normally rely on the device being setup. Devices in spi_add_device()
610 status = spi_setup(spi); in spi_add_device()
613 dev_name(&spi->dev), status); in spi_add_device()
617 /* Device may be bound to an active driver when this returns */ 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).
645 * Return: the new device, or NULL.
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
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()
862 int spi_map_buf(struct spi_controller *ctlr, struct device *dev, in spi_map_buf()
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()
943 void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, in spi_unmap_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()
956 struct device *tx_dev, *rx_dev; in __spi_map_msg()
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()
1005 struct device *tx_dev, *rx_dev; in __spi_unmap_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
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
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()
1411 /* If another context is idling the device then defer */ 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
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
1651 * (by setting the ->rt value before calling spi_register_controller()) or
1652 * because a device on the bus said that its transfers needed realtime
1655 * NOTE: at the moment if any device on a bus says it needs realtime then
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()
1688 if (ctlr->rt) in spi_init_queue()
1695 * spi_get_next_queued_message() - called by driver to check for queued
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
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'
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()
1958 /* Device DUAL/QUAD mode */ 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()
2010 /* Device address */ 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()
2019 /* Device speed */ 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()
2040 /* Select device driver */ 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()
2052 /* Store a pointer to the node in the device structure */ 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()
2057 /* Register the new device */ 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
2075 * @ctlr: Pointer to spi_controller device
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()
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()
2303 static void spi_controller_release(struct device *dev) in spi_controller_release()
2320 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
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()
2335 static int match_true(struct device *dev, void *data) in match_true()
2340 static ssize_t slave_show(struct device *dev, struct device_attribute *attr, in slave_show()
2345 struct device *child; in slave_show()
2347 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_show()
2349 child ? to_spi_device(child)->modalias : NULL); in slave_show()
2352 static ssize_t slave_store(struct device *dev, struct device_attribute *attr, in slave_store()
2357 struct spi_device *spi; in slave_store() local
2358 struct device *child; in slave_store()
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
2421 * @size: how much zeroed driver-private data to allocate; the pointer to this
2422 * memory is in the driver_data field of the returned device, accessible
2426 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2430 * This call is used only by SPI controller drivers, which are the
2438 * errors adding the device) calling spi_controller_put() to prevent a memory
2441 * Return: the SPI controller structure on success, else NULL.
2443 struct spi_controller *__spi_alloc_controller(struct device *dev, in __spi_alloc_controller()
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()
2472 static void devm_spi_release_controller(struct device *dev, void *ctlr) in devm_spi_release_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.
2492 struct spi_controller *__devm_spi_alloc_controller(struct device *dev, in __devm_spi_alloc_controller()
2505 ctlr->devm_allocated = true; in __devm_spi_alloc_controller()
2520 struct device_node *np = ctlr->dev.of_node; in of_spi_get_gpio_numbers()
2525 nb = of_gpio_named_count(np, "cs-gpios"); in of_spi_get_gpio_numbers()
2526 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in of_spi_get_gpio_numbers()
2528 /* Return error only for an incorrectly formed cs-gpios property */ in of_spi_get_gpio_numbers()
2529 if (nb == 0 || nb == -ENOENT) in of_spi_get_gpio_numbers()
2534 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), in of_spi_get_gpio_numbers()
2536 ctlr->cs_gpios = cs; in of_spi_get_gpio_numbers()
2538 if (!ctlr->cs_gpios) in of_spi_get_gpio_numbers()
2539 return -ENOMEM; in of_spi_get_gpio_numbers()
2541 for (i = 0; i < ctlr->num_chipselect; i++) in of_spi_get_gpio_numbers()
2542 cs[i] = -ENOENT; in of_spi_get_gpio_numbers()
2545 cs[i] = of_get_named_gpio(np, "cs-gpios", i); in of_spi_get_gpio_numbers()
2557 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2558 * @ctlr: The SPI master to grab GPIO descriptors for
2564 struct device *dev = &ctlr->dev; in spi_get_gpio_descs()
2569 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in spi_get_gpio_descs()
2572 if (nb == 0 || nb == -ENOENT) in spi_get_gpio_descs()
2577 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), in spi_get_gpio_descs()
2580 return -ENOMEM; in spi_get_gpio_descs()
2581 ctlr->cs_gpiods = cs; in spi_get_gpio_descs()
2598 * If we find a CS GPIO, name it after the device and in spi_get_gpio_descs()
2606 return -ENOMEM; in spi_get_gpio_descs()
2612 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2614 return -EINVAL; in spi_get_gpio_descs()
2619 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; in spi_get_gpio_descs()
2621 if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && in spi_get_gpio_descs()
2622 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2624 return -EINVAL; in spi_get_gpio_descs()
2633 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
2634 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
2636 * If ->mem_ops is NULL, we request that at least one of the in spi_controller_check_ops()
2637 * ->transfer_xxx() method be implemented. in spi_controller_check_ops()
2639 if (ctlr->mem_ops) { in spi_controller_check_ops()
2640 if (!ctlr->mem_ops->exec_op) in spi_controller_check_ops()
2641 return -EINVAL; in spi_controller_check_ops()
2642 } else if (!ctlr->transfer && !ctlr->transfer_one && in spi_controller_check_ops()
2643 !ctlr->transfer_one_message) { in spi_controller_check_ops()
2644 return -EINVAL; in spi_controller_check_ops()
2651 * spi_register_controller - register SPI master or slave controller
2656 * SPI controllers connect to their drivers using some non-SPI bus,
2658 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2660 * SPI controllers use board specific (often SOC specific) bus numbers,
2661 * and board-specific addressing for SPI devices combines those numbers
2662 * with chip select numbers. Since SPI does not directly support dynamic
2663 * device identification, boards need configuration tables telling which
2675 struct device *dev = ctlr->dev.parent; in spi_register_controller()
2681 return -ENODEV; in spi_register_controller()
2685 * the SPI controller. in spi_register_controller()
2691 if (ctlr->bus_num >= 0) { in spi_register_controller()
2692 /* devices with a fixed bus num must check-in with the num */ in spi_register_controller()
2694 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2695 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2698 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2699 ctlr->bus_num = id; in spi_register_controller()
2700 } else if (ctlr->dev.of_node) { in spi_register_controller()
2702 id = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
2704 ctlr->bus_num = id; in spi_register_controller()
2706 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2707 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2710 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2713 if (ctlr->bus_num < 0) { in spi_register_controller()
2714 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
2726 ctlr->bus_num = id; in spi_register_controller()
2728 INIT_LIST_HEAD(&ctlr->queue); in spi_register_controller()
2729 spin_lock_init(&ctlr->queue_lock); in spi_register_controller()
2730 spin_lock_init(&ctlr->bus_lock_spinlock); in spi_register_controller()
2731 mutex_init(&ctlr->bus_lock_mutex); in spi_register_controller()
2732 mutex_init(&ctlr->io_mutex); in spi_register_controller()
2733 ctlr->bus_lock_flag = 0; in spi_register_controller()
2734 init_completion(&ctlr->xfer_completion); in spi_register_controller()
2735 if (!ctlr->max_dma_len) in spi_register_controller()
2736 ctlr->max_dma_len = INT_MAX; in spi_register_controller()
2738 /* register the device, then userspace will see it. in spi_register_controller()
2741 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
2744 if (ctlr->use_gpio_descriptors) { in spi_register_controller()
2752 ctlr->mode_bits |= SPI_CS_HIGH; in spi_register_controller()
2762 * Even if it's just one always-selected device, there must in spi_register_controller()
2765 if (!ctlr->num_chipselect) { in spi_register_controller()
2766 status = -EINVAL; in spi_register_controller()
2770 status = device_add(&ctlr->dev); in spi_register_controller()
2775 dev_name(&ctlr->dev)); in spi_register_controller()
2779 * need the queueing logic if the driver is only supporting high-level in spi_register_controller()
2782 if (ctlr->transfer) { in spi_register_controller()
2784 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { in spi_register_controller()
2787 device_del(&ctlr->dev); in spi_register_controller()
2792 spin_lock_init(&ctlr->statistics.lock); in spi_register_controller()
2795 list_add_tail(&ctlr->list, &spi_controller_list); in spi_register_controller()
2797 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); in spi_register_controller()
2800 /* Register devices from the device tree and ACPI */ in spi_register_controller()
2807 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
2813 static void devm_spi_unregister(struct device *dev, void *res) in devm_spi_unregister()
2819 * devm_spi_register_controller - register managed SPI master or slave
2821 * @dev: device managing SPI controller
2826 * Register a SPI device as with spi_register_controller() which will
2831 int devm_spi_register_controller(struct device *dev, in devm_spi_register_controller()
2839 return -ENOMEM; in devm_spi_register_controller()
2853 static int __unregister(struct device *dev, void *null) in __unregister()
2860 * spi_unregister_controller - unregister SPI master or slave controller
2864 * This call is used only by SPI controller drivers, which are the
2874 int id = ctlr->bus_num; in spi_unregister_controller()
2880 device_for_each_child(&ctlr->dev, NULL, __unregister); in spi_unregister_controller()
2886 if (ctlr->queued) { in spi_unregister_controller()
2888 dev_err(&ctlr->dev, "queue remove failed\n"); in spi_unregister_controller()
2891 list_del(&ctlr->list); in spi_unregister_controller()
2894 device_del(&ctlr->dev); in spi_unregister_controller()
2899 if (!ctlr->devm_allocated) in spi_unregister_controller()
2900 put_device(&ctlr->dev); in spi_unregister_controller()
2917 /* Basically no-ops for non-queued controllers */ in spi_controller_suspend()
2918 if (!ctlr->queued) in spi_controller_suspend()
2923 dev_err(&ctlr->dev, "queue stop failed\n"); in spi_controller_suspend()
2933 if (!ctlr->queued) in spi_controller_resume()
2938 dev_err(&ctlr->dev, "queue restart failed\n"); in spi_controller_resume()
2944 static int __spi_controller_match(struct device *dev, const void *data) in __spi_controller_match()
2950 return ctlr->bus_num == *bus_num; in __spi_controller_match()
2954 * spi_busnum_to_master - look up master associated with bus_num
2963 * Return: the SPI master structure on success, else NULL.
2967 struct device *dev; in spi_busnum_to_master()
2979 /*-------------------------------------------------------------------------*/
2981 /* Core methods for SPI resource management */
2984 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2987 * @spi: the spi device for which we allocate memory
2997 void *spi_res_alloc(struct spi_device *spi, in spi_res_alloc() argument
3007 INIT_LIST_HEAD(&sres->entry); in spi_res_alloc()
3008 sres->release = release; in spi_res_alloc()
3010 return sres->data; in spi_res_alloc()
3015 * spi_res_free - free an spi resource
3026 WARN_ON(!list_empty(&sres->entry)); in spi_res_free()
3032 * spi_res_add - add a spi_res to the spi_message
3033 * @message: the spi message
3040 WARN_ON(!list_empty(&sres->entry)); in spi_res_add()
3041 list_add_tail(&sres->entry, &message->resources); in spi_res_add()
3046 * spi_res_release - release all spi resources for this message
3054 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { in spi_res_release()
3055 if (res->release) in spi_res_release()
3056 res->release(ctlr, message, res->data); in spi_res_release()
3058 list_del(&res->entry); in spi_res_release()
3065 /*-------------------------------------------------------------------------*/
3077 if (rxfer->release) in __spi_replace_transfers_release()
3078 rxfer->release(ctlr, msg, res); in __spi_replace_transfers_release()
3081 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); in __spi_replace_transfers_release()
3084 for (i = 0; i < rxfer->inserted; i++) in __spi_replace_transfers_release()
3085 list_del(&rxfer->inserted_transfers[i].transfer_list); in __spi_replace_transfers_release()
3089 * spi_replace_transfers - replace transfers with several transfers
3117 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3122 return ERR_PTR(-ENOMEM); in spi_replace_transfers()
3125 rxfer->release = release; in spi_replace_transfers()
3129 rxfer->extradata = in spi_replace_transfers()
3130 &rxfer->inserted_transfers[insert]; in spi_replace_transfers()
3133 INIT_LIST_HEAD(&rxfer->replaced_transfers); in spi_replace_transfers()
3136 * the @replaced_transfers - it may be spi_message.messages! in spi_replace_transfers()
3138 rxfer->replaced_after = xfer_first->transfer_list.prev; in spi_replace_transfers()
3142 /* if the entry after replaced_after it is msg->transfers in spi_replace_transfers()
3146 if (rxfer->replaced_after->next == &msg->transfers) { in spi_replace_transfers()
3147 dev_err(&msg->spi->dev, in spi_replace_transfers()
3150 list_splice(&rxfer->replaced_transfers, in spi_replace_transfers()
3151 rxfer->replaced_after); in spi_replace_transfers()
3157 return ERR_PTR(-EINVAL); in spi_replace_transfers()
3163 list_move_tail(rxfer->replaced_after->next, in spi_replace_transfers()
3164 &rxfer->replaced_transfers); in spi_replace_transfers()
3172 xfer = &rxfer->inserted_transfers[insert - 1 - i]; in spi_replace_transfers()
3178 list_add(&xfer->transfer_list, rxfer->replaced_after); in spi_replace_transfers()
3182 xfer->cs_change = false; in spi_replace_transfers()
3183 xfer->delay_usecs = 0; in spi_replace_transfers()
3184 xfer->delay.value = 0; in spi_replace_transfers()
3189 rxfer->inserted = insert; in spi_replace_transfers()
3210 count = DIV_ROUND_UP(xfer->len, maxsize); in __spi_split_transfer_maxsize()
3216 xfers = srt->inserted_transfers; in __spi_split_transfer_maxsize()
3246 xfers[i].len = min(maxsize, xfers[i].len - offset); in __spi_split_transfer_maxsize()
3252 *xferp = &xfers[count - 1]; in __spi_split_transfer_maxsize()
3255 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_split_transfer_maxsize()
3257 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, in __spi_split_transfer_maxsize()
3264 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
3288 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxsize()
3289 if (xfer->len > maxsize) { in spi_split_transfers_maxsize()
3301 /*-------------------------------------------------------------------------*/
3303 /* Core methods for SPI controller protocol drivers. Some of the
3310 if (ctlr->bits_per_word_mask) { in __spi_validate_bits_per_word()
3313 return -EINVAL; in __spi_validate_bits_per_word()
3314 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) in __spi_validate_bits_per_word()
3315 return -EINVAL; in __spi_validate_bits_per_word()
3322 * spi_setup - setup SPI mode and clock rate
3323 * @spi: the device whose settings are being modified
3324 * Context: can sleep, and no requests are queued to the device
3326 * SPI protocol drivers may need to update the transfer mode if the
3327 * device doesn't work with its default. They may likewise need
3331 * effect the next time the device is selected and data is transferred to
3332 * or from it. When this function returns, the spi device is deselected.
3337 * LSB-first wire encoding, or active-high chipselects.
3341 int spi_setup(struct spi_device *spi) in spi_setup() argument
3348 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || in spi_setup()
3349 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { in spi_setup()
3350 dev_err(&spi->dev, in spi_setup()
3352 return -EINVAL; in spi_setup()
3356 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3359 return -EINVAL; in spi_setup()
3365 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); in spi_setup()
3366 /* nothing prevents from working with active-high CS in case if it in spi_setup()
3369 if (gpio_is_valid(spi->cs_gpio)) in spi_setup()
3375 dev_warn(&spi->dev, in spi_setup()
3378 spi->mode &= ~ugly_bits; in spi_setup()
3382 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3384 return -EINVAL; in spi_setup()
3387 if (!spi->bits_per_word) in spi_setup()
3388 spi->bits_per_word = 8; in spi_setup()
3390 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3391 spi->bits_per_word); in spi_setup()
3395 if (!spi->max_speed_hz) in spi_setup()
3396 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3398 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3400 if (spi->controller->setup) in spi_setup()
3401 status = spi->controller->setup(spi); in spi_setup()
3403 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3404 status = pm_runtime_get_sync(spi->controller->dev.parent); in spi_setup()
3406 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3407 pm_runtime_put_noidle(spi->controller->dev.parent); in spi_setup()
3408 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3416 * checking for a non-zero return value instead of a negative in spi_setup()
3421 spi_set_cs(spi, false, true); in spi_setup()
3422 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3423 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
3425 spi_set_cs(spi, false, true); in spi_setup()
3428 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3430 if (spi->rt && !spi->controller->rt) { in spi_setup()
3431 spi->controller->rt = true; in spi_setup()
3432 spi_set_thread_rt(spi->controller); in spi_setup()
3435 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
3436 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), in spi_setup()
3437 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
3438 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
3439 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
3440 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
3441 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
3449 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3450 * @spi: the device that requires specific CS timing configuration
3457 int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup, in spi_set_cs_timing() argument
3462 if (spi->controller->set_cs_timing) in spi_set_cs_timing()
3463 return spi->controller->set_cs_timing(spi, setup, hold, in spi_set_cs_timing()
3466 if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) || in spi_set_cs_timing()
3467 (hold && hold->unit == SPI_DELAY_UNIT_SCK) || in spi_set_cs_timing()
3468 (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) { in spi_set_cs_timing()
3469 dev_err(&spi->dev, in spi_set_cs_timing()
3470 "Clock-cycle delays for CS not supported in SW mode\n"); in spi_set_cs_timing()
3471 return -ENOTSUPP; in spi_set_cs_timing()
3478 memcpy(&spi->controller->cs_setup, setup, len); in spi_set_cs_timing()
3480 memset(&spi->controller->cs_setup, 0, len); in spi_set_cs_timing()
3483 memcpy(&spi->controller->cs_hold, hold, len); in spi_set_cs_timing()
3485 memset(&spi->controller->cs_hold, 0, len); in spi_set_cs_timing()
3488 memcpy(&spi->controller->cs_inactive, inactive, len); in spi_set_cs_timing()
3490 memset(&spi->controller->cs_inactive, 0, len); in spi_set_cs_timing()
3497 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
3501 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); in _spi_xfer_word_delay_update()
3505 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
3510 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
3511 sizeof(xfer->word_delay)); in _spi_xfer_word_delay_update()
3516 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
3518 struct spi_controller *ctlr = spi->controller; in __spi_validate()
3522 if (list_empty(&message->transfers)) in __spi_validate()
3523 return -EINVAL; in __spi_validate()
3525 /* If an SPI controller does not support toggling the CS line on each in __spi_validate()
3527 * for the CS line, we can emulate the CS-per-word hardware function by in __spi_validate()
3528 * splitting transfers into one-word transfers and ensuring that in __spi_validate()
3531 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || in __spi_validate()
3532 spi->cs_gpiod || in __spi_validate()
3533 gpio_is_valid(spi->cs_gpio))) { in __spi_validate()
3537 maxsize = (spi->bits_per_word + 7) / 8; in __spi_validate()
3539 /* spi_split_transfers_maxsize() requires message->spi */ in __spi_validate()
3540 message->spi = spi; in __spi_validate()
3547 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3549 if (list_is_last(&xfer->transfer_list, &message->transfers)) in __spi_validate()
3551 xfer->cs_change = 1; in __spi_validate()
3555 /* Half-duplex links include original MicroWire, and ones with in __spi_validate()
3560 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || in __spi_validate()
3561 (spi->mode & SPI_3WIRE)) { in __spi_validate()
3562 unsigned flags = ctlr->flags; in __spi_validate()
3564 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3565 if (xfer->rx_buf && xfer->tx_buf) in __spi_validate()
3566 return -EINVAL; in __spi_validate()
3567 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) in __spi_validate()
3568 return -EINVAL; in __spi_validate()
3569 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) in __spi_validate()
3570 return -EINVAL; in __spi_validate()
3575 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
3580 * device itself. in __spi_validate()
3582 message->frame_length = 0; in __spi_validate()
3583 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3584 xfer->effective_speed_hz = 0; in __spi_validate()
3585 message->frame_length += xfer->len; in __spi_validate()
3586 if (!xfer->bits_per_word) in __spi_validate()
3587 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
3589 if (!xfer->speed_hz) in __spi_validate()
3590 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
3592 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) in __spi_validate()
3593 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
3595 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) in __spi_validate()
3596 return -EINVAL; in __spi_validate()
3599 * SPI transfer length should be multiple of SPI word size in __spi_validate()
3600 * where SPI word size should be power-of-two multiple in __spi_validate()
3602 if (xfer->bits_per_word <= 8) in __spi_validate()
3604 else if (xfer->bits_per_word <= 16) in __spi_validate()
3610 if (xfer->len % w_size) in __spi_validate()
3611 return -EINVAL; in __spi_validate()
3613 if (xfer->speed_hz && ctlr->min_speed_hz && in __spi_validate()
3614 xfer->speed_hz < ctlr->min_speed_hz) in __spi_validate()
3615 return -EINVAL; in __spi_validate()
3617 if (xfer->tx_buf && !xfer->tx_nbits) in __spi_validate()
3618 xfer->tx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3619 if (xfer->rx_buf && !xfer->rx_nbits) in __spi_validate()
3620 xfer->rx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3625 if (xfer->tx_buf) { in __spi_validate()
3626 if (xfer->tx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3627 xfer->tx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3628 xfer->tx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3629 return -EINVAL; in __spi_validate()
3630 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3631 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
3632 return -EINVAL; in __spi_validate()
3633 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3634 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
3635 return -EINVAL; in __spi_validate()
3638 if (xfer->rx_buf) { in __spi_validate()
3639 if (xfer->rx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3640 xfer->rx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3641 xfer->rx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3642 return -EINVAL; in __spi_validate()
3643 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3644 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
3645 return -EINVAL; in __spi_validate()
3646 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3647 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
3648 return -EINVAL; in __spi_validate()
3651 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
3652 return -EINVAL; in __spi_validate()
3655 message->status = -EINPROGRESS; in __spi_validate()
3660 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
3662 struct spi_controller *ctlr = spi->controller; in __spi_async()
3666 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
3669 if (!ctlr->transfer) in __spi_async()
3670 return -ENOTSUPP; in __spi_async()
3672 message->spi = spi; in __spi_async()
3674 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); in __spi_async()
3675 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); in __spi_async()
3679 if (!ctlr->ptp_sts_supported) { in __spi_async()
3680 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_async()
3681 xfer->ptp_sts_word_pre = 0; in __spi_async()
3682 ptp_read_system_prets(xfer->ptp_sts); in __spi_async()
3686 return ctlr->transfer(spi, message); in __spi_async()
3690 * spi_async - asynchronous SPI transfer
3691 * @spi: device with which data will be exchanged
3699 * Before that invocation, the value of message->status is undefined.
3700 * When the callback is issued, message->status holds either zero (to
3703 * deallocate the associated memory; it's no longer in use by any SPI
3708 * Some device might be higher priority, or have various "hard" access
3712 * the entire message is aborted, and the device is deselected.
3714 * no other spi_message queued to that device will be processed.
3720 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
3722 struct spi_controller *ctlr = spi->controller; in spi_async()
3726 ret = __spi_validate(spi, message); in spi_async()
3730 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async()
3732 if (ctlr->bus_lock_flag) in spi_async()
3733 ret = -EBUSY; in spi_async()
3735 ret = __spi_async(spi, message); in spi_async()
3737 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async()
3744 * spi_async_locked - version of spi_async with exclusive bus usage
3745 * @spi: device with which data will be exchanged
3753 * Before that invocation, the value of message->status is undefined.
3754 * When the callback is issued, message->status holds either zero (to
3757 * deallocate the associated memory; it's no longer in use by any SPI
3762 * Some device might be higher priority, or have various "hard" access
3766 * the entire message is aborted, and the device is deselected.
3768 * no other spi_message queued to that device will be processed.
3774 int spi_async_locked(struct spi_device *spi, struct spi_message *message) in spi_async_locked() argument
3776 struct spi_controller *ctlr = spi->controller; in spi_async_locked()
3780 ret = __spi_validate(spi, message); in spi_async_locked()
3784 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3786 ret = __spi_async(spi, message); in spi_async_locked()
3788 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3795 /*-------------------------------------------------------------------------*/
3797 /* Utility methods for SPI protocol drivers, layered on
3807 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
3811 struct spi_controller *ctlr = spi->controller; in __spi_sync()
3814 status = __spi_validate(spi, message); in __spi_sync()
3818 message->complete = spi_complete; in __spi_sync()
3819 message->context = &done; in __spi_sync()
3820 message->spi = spi; in __spi_sync()
3822 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); in __spi_sync()
3823 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); in __spi_sync()
3830 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3831 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3835 status = __spi_queued_transfer(spi, message, false); in __spi_sync()
3837 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3839 status = spi_async_locked(spi, message); in __spi_sync()
3846 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3847 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_sync()
3849 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, in __spi_sync()
3855 status = message->status; in __spi_sync()
3857 message->context = NULL; in __spi_sync()
3862 * spi_sync - blocking/synchronous SPI data transfers
3863 * @spi: device with which data will be exchanged
3868 * is non-interruptible, and has no timeout. Low-overhead controller
3871 * Note that the SPI device's chip select is active during the message,
3873 * frequently-used devices may want to minimize costs of selecting a chip,
3882 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
3886 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
3887 ret = __spi_sync(spi, message); in spi_sync()
3888 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
3895 * spi_sync_locked - version of spi_sync with exclusive bus usage
3896 * @spi: device with which data will be exchanged
3901 * is non-interruptible, and has no timeout. Low-overhead controller
3905 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3910 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
3912 return __spi_sync(spi, message); in spi_sync_locked()
3917 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3918 * @ctlr: SPI bus master that should be locked for exclusive bus access
3922 * is non-interruptible, and has no timeout.
3925 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3927 * and spi_async_locked calls when the SPI bus lock is held.
3935 mutex_lock(&ctlr->bus_lock_mutex); in spi_bus_lock()
3937 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3938 ctlr->bus_lock_flag = 1; in spi_bus_lock()
3939 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3948 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3949 * @ctlr: SPI bus master that was locked for exclusive bus access
3953 * is non-interruptible, and has no timeout.
3955 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3962 ctlr->bus_lock_flag = 0; in spi_bus_unlock()
3964 mutex_unlock(&ctlr->bus_lock_mutex); in spi_bus_unlock()
3976 * spi_write_then_read - SPI synchronous write followed by read
3977 * @spi: device with which data will be exchanged
3978 * @txbuf: data to be written (need not be dma-safe)
3980 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3985 * device, sending txbuf and then reading rxbuf. The return value
3990 * Performance-sensitive or bulk transfer code should instead use
3991 * spi_{async,sync}() calls with dma-safe buffers.
3995 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4006 /* Use preallocated DMA-safe buffer if we can. We can't avoid in spi_write_then_read()
4009 * using the pre-allocated buffer or the transfer is too large. in spi_write_then_read()
4015 return -ENOMEM; in spi_write_then_read()
4036 status = spi_sync(spi, &message); in spi_write_then_read()
4049 /*-------------------------------------------------------------------------*/
4052 /* must call put_device() when done with returned spi_device device */
4055 struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); in of_find_spi_device_by_node()
4063 /* the spi controllers are not using spi_bus, so we find it with another way */
4066 struct device *dev; in of_find_spi_controller_by_node()
4083 struct spi_device *spi; in of_spi_notify() local
4087 ctlr = of_find_spi_controller_by_node(rd->dn->parent); in of_spi_notify()
4091 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { in of_spi_notify()
4092 put_device(&ctlr->dev); in of_spi_notify()
4096 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4097 put_device(&ctlr->dev); in of_spi_notify()
4099 if (IS_ERR(spi)) { in of_spi_notify()
4101 __func__, rd->dn); in of_spi_notify()
4102 of_node_clear_flag(rd->dn, OF_POPULATED); in of_spi_notify()
4103 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4109 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_spi_notify()
4112 /* find our device by node */ in of_spi_notify()
4113 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4114 if (spi == NULL) in of_spi_notify()
4118 spi_unregister_device(spi); in of_spi_notify()
4121 put_device(&spi->dev); in of_spi_notify()
4136 static int spi_acpi_controller_match(struct device *dev, const void *data) in spi_acpi_controller_match()
4138 return ACPI_COMPANION(dev->parent) == data; in spi_acpi_controller_match()
4143 struct device *dev; in acpi_spi_find_controller_by_adev()
4158 struct device *dev; in acpi_spi_find_device_by_adev()
4169 struct spi_device *spi; in acpi_spi_notify() local
4173 ctlr = acpi_spi_find_controller_by_adev(adev->parent); in acpi_spi_notify()
4178 put_device(&ctlr->dev); in acpi_spi_notify()
4184 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4185 if (!spi) in acpi_spi_notify()
4188 spi_unregister_device(spi); in acpi_spi_notify()
4189 put_device(&spi->dev); in acpi_spi_notify()
4209 status = -ENOMEM; in spi_init()
4248 * REVISIT only boardinfo really needs static linking. the rest (device and