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 controllers may cleanup for released devices */ in spidev_release()
51 if (spi->controller->cleanup) in spidev_release()
52 spi->controller->cleanup(spi); in spidev_release()
54 spi_controller_put(spi->controller); in spidev_release()
55 kfree(spi->driver_override); in spidev_release()
56 kfree(spi); in spidev_release()
60 modalias_show(struct device *dev, struct device_attribute *a, char *buf) in modalias_show()
62 const struct spi_device *spi = to_spi_device(dev); in modalias_show() local
65 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); in modalias_show()
66 if (len != -ENODEV) in modalias_show()
69 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); in modalias_show()
73 static ssize_t driver_override_store(struct device *dev, in driver_override_store()
77 struct spi_device *spi = to_spi_device(dev); in driver_override_store() local
79 const size_t len = end ? end - buf : count; in driver_override_store()
83 if (len >= (PAGE_SIZE - 1)) in driver_override_store()
84 return -EINVAL; in driver_override_store()
88 return -ENOMEM; in driver_override_store()
91 old = spi->driver_override; in driver_override_store()
93 spi->driver_override = driver_override; in driver_override_store()
96 spi->driver_override = NULL; in driver_override_store()
105 static ssize_t driver_override_show(struct device *dev, in driver_override_show()
108 const struct spi_device *spi = to_spi_device(dev); in driver_override_show() local
112 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); in driver_override_show()
119 static ssize_t spi_controller_##field##_show(struct device *dev, \
125 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
131 static ssize_t spi_device_##field##_show(struct device *dev, \
135 struct spi_device *spi = to_spi_device(dev); \
136 return spi_statistics_##field##_show(&spi->statistics, buf); \
149 spin_lock_irqsave(&stat->lock, flags); \
150 len = sprintf(buf, format_string, stat->field); \
151 spin_unlock_irqrestore(&stat->lock, flags); \
177 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
178 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
179 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
180 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
181 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
182 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
183 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
184 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
185 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
186 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
187 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
188 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
189 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
190 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
191 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
192 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
297 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; in spi_statistics_add_transfer_stats()
302 spin_lock_irqsave(&stats->lock, flags); in spi_statistics_add_transfer_stats()
304 stats->transfers++; in spi_statistics_add_transfer_stats()
305 stats->transfer_bytes_histo[l2len]++; in spi_statistics_add_transfer_stats()
307 stats->bytes += xfer->len; in spi_statistics_add_transfer_stats()
308 if ((xfer->tx_buf) && in spi_statistics_add_transfer_stats()
309 (xfer->tx_buf != ctlr->dummy_tx)) in spi_statistics_add_transfer_stats()
310 stats->bytes_tx += xfer->len; in spi_statistics_add_transfer_stats()
311 if ((xfer->rx_buf) && in spi_statistics_add_transfer_stats()
312 (xfer->rx_buf != ctlr->dummy_rx)) in spi_statistics_add_transfer_stats()
313 stats->bytes_rx += xfer->len; in spi_statistics_add_transfer_stats()
315 spin_unlock_irqrestore(&stats->lock, flags); in spi_statistics_add_transfer_stats()
319 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
326 while (id->name[0]) { in spi_match_id()
327 if (!strcmp(sdev->modalias, id->name)) in spi_match_id()
336 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); in spi_get_device_id()
338 return spi_match_id(sdrv->id_table, sdev); in spi_get_device_id()
342 static int spi_match_device(struct device *dev, struct device_driver *drv) in spi_match_device()
344 const struct spi_device *spi = to_spi_device(dev); in spi_match_device() local
348 if (spi->driver_override) in spi_match_device()
349 return strcmp(spi->driver_override, drv->name) == 0; in spi_match_device()
359 if (sdrv->id_table) in spi_match_device()
360 return !!spi_match_id(sdrv->id_table, spi); in spi_match_device()
362 return strcmp(spi->modalias, drv->name) == 0; in spi_match_device()
365 static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) in spi_uevent()
367 const struct spi_device *spi = to_spi_device(dev); in spi_uevent() local
371 if (rc != -ENODEV) in spi_uevent()
374 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); in spi_uevent()
378 .name = "spi",
386 static int spi_drv_probe(struct device *dev) in spi_drv_probe()
388 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_probe()
389 struct spi_device *spi = to_spi_device(dev); in spi_drv_probe() local
392 ret = of_clk_set_defaults(dev->of_node, false); in spi_drv_probe()
396 if (dev->of_node) { in spi_drv_probe()
397 spi->irq = of_irq_get(dev->of_node, 0); in spi_drv_probe()
398 if (spi->irq == -EPROBE_DEFER) in spi_drv_probe()
399 return -EPROBE_DEFER; in spi_drv_probe()
400 if (spi->irq < 0) in spi_drv_probe()
401 spi->irq = 0; in spi_drv_probe()
408 ret = sdrv->probe(spi); in spi_drv_probe()
415 static int spi_drv_remove(struct device *dev) in spi_drv_remove()
417 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_remove()
420 ret = sdrv->remove(to_spi_device(dev)); in spi_drv_remove()
426 static void spi_drv_shutdown(struct device *dev) in spi_drv_shutdown()
428 const struct spi_driver *sdrv = to_spi_driver(dev->driver); in spi_drv_shutdown()
430 sdrv->shutdown(to_spi_device(dev)); in spi_drv_shutdown()
434 * __spi_register_driver - register a SPI driver
443 sdrv->driver.owner = owner; in __spi_register_driver()
444 sdrv->driver.bus = &spi_bus_type; in __spi_register_driver()
445 if (sdrv->probe) in __spi_register_driver()
446 sdrv->driver.probe = spi_drv_probe; in __spi_register_driver()
447 if (sdrv->remove) in __spi_register_driver()
448 sdrv->driver.remove = spi_drv_remove; in __spi_register_driver()
449 if (sdrv->shutdown) in __spi_register_driver()
450 sdrv->driver.shutdown = spi_drv_shutdown; in __spi_register_driver()
451 return driver_register(&sdrv->driver); in __spi_register_driver()
455 /*-------------------------------------------------------------------------*/
457 /* SPI devices should normally not be created by SPI device drivers; that
458 * would make them board-specific. Similarly with SPI controller drivers.
459 * Device registration normally goes into like arch/.../mach.../board-YYY.c
485 * spi_alloc_device - Allocate a new SPI device
486 * @ctlr: Controller to which device is connected
491 * fill the spi_device with device parameters before calling
495 * spi_device structure to add it to the SPI controller. If the caller
499 * Return: a pointer to the new device, or NULL.
503 struct spi_device *spi; in spi_alloc_device() local
508 spi = kzalloc(sizeof(*spi), GFP_KERNEL); in spi_alloc_device()
509 if (!spi) { in spi_alloc_device()
514 spi->master = spi->controller = ctlr; in spi_alloc_device()
515 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
516 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
517 spi->dev.release = spidev_release; in spi_alloc_device()
518 spi->cs_gpio = -ENOENT; in spi_alloc_device()
519 spi->mode = ctlr->buswidth_override_bits; in spi_alloc_device()
521 spin_lock_init(&spi->statistics.lock); in spi_alloc_device()
523 device_initialize(&spi->dev); in spi_alloc_device()
524 return spi; in spi_alloc_device()
528 static void spi_dev_set_name(struct spi_device *spi) in spi_dev_set_name() argument
530 struct acpi_device *adev = ACPI_COMPANION(&spi->dev); in spi_dev_set_name()
533 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); in spi_dev_set_name()
537 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
538 spi->chip_select); in spi_dev_set_name()
541 static int spi_dev_check(struct device *dev, void *data) in spi_dev_check()
543 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
546 if (spi->controller == new_spi->controller && in spi_dev_check()
547 spi->chip_select == new_spi->chip_select) in spi_dev_check()
548 return -EBUSY; in spi_dev_check()
553 * spi_add_device - Add spi_device allocated with spi_alloc_device
554 * @spi: spi_device to register
557 * spi_alloc_device can be added onto the spi bus with this function.
561 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
563 struct spi_controller *ctlr = spi->controller; in spi_add_device()
564 struct device *dev = ctlr->dev.parent; in spi_add_device()
568 if (spi->chip_select >= ctlr->num_chipselect) { in spi_add_device()
569 dev_err(dev, "cs%d >= max %d\n", spi->chip_select, in spi_add_device()
570 ctlr->num_chipselect); in spi_add_device()
571 return -EINVAL; in spi_add_device()
575 spi_dev_set_name(spi); in spi_add_device()
577 /* We need to make sure there's no other device with this in spi_add_device()
583 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in spi_add_device()
586 spi->chip_select); in spi_add_device()
592 !device_is_registered(&ctlr->dev)) { in spi_add_device()
593 status = -ENODEV; in spi_add_device()
598 if (ctlr->cs_gpiods) in spi_add_device()
599 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; in spi_add_device()
600 else if (ctlr->cs_gpios) in spi_add_device()
601 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; in spi_add_device()
604 * normally rely on the device being setup. Devices in spi_add_device()
607 status = spi_setup(spi); in spi_add_device()
610 dev_name(&spi->dev), status); in spi_add_device()
614 /* Device may be bound to an active driver when this returns */ in spi_add_device()
615 status = device_add(&spi->dev); in spi_add_device()
618 dev_name(&spi->dev), status); in spi_add_device()
620 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in spi_add_device()
629 * spi_new_device - instantiate one new SPI device
630 * @ctlr: Controller to which device is connected
631 * @chip: Describes the SPI device
635 * after board init creates the hard-wired devices. Some development
638 * driver could add devices (which it would learn about out-of-band).
640 * Return: the new device, or NULL.
648 /* NOTE: caller did any chip->bus_num checks necessary. in spi_new_device()
651 * error-or-pointer (not NULL-or-pointer), troubleshootability in spi_new_device()
659 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); in spi_new_device()
661 proxy->chip_select = chip->chip_select; in spi_new_device()
662 proxy->max_speed_hz = chip->max_speed_hz; in spi_new_device()
663 proxy->mode = chip->mode; in spi_new_device()
664 proxy->irq = chip->irq; in spi_new_device()
665 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); in spi_new_device()
666 proxy->dev.platform_data = (void *) chip->platform_data; in spi_new_device()
667 proxy->controller_data = chip->controller_data; in spi_new_device()
668 proxy->controller_state = NULL; in spi_new_device()
670 if (chip->properties) { in spi_new_device()
671 status = device_add_properties(&proxy->dev, chip->properties); in spi_new_device()
673 dev_err(&ctlr->dev, in spi_new_device()
675 chip->modalias, status); in spi_new_device()
687 if (chip->properties) in spi_new_device()
688 device_remove_properties(&proxy->dev); in spi_new_device()
696 * spi_unregister_device - unregister a single SPI device
697 * @spi: spi_device to unregister
699 * Start making the passed SPI device vanish. Normally this would be handled
702 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
704 if (!spi) in spi_unregister_device()
707 if (spi->dev.of_node) { in spi_unregister_device()
708 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
709 of_node_put(spi->dev.of_node); in spi_unregister_device()
711 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
712 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
713 device_unregister(&spi->dev); in spi_unregister_device()
722 if (ctlr->bus_num != bi->bus_num) in spi_match_controller_to_boardinfo()
727 dev_err(ctlr->dev.parent, "can't create new device for %s\n", in spi_match_controller_to_boardinfo()
728 bi->modalias); in spi_match_controller_to_boardinfo()
732 * spi_register_board_info - register SPI devices for a given board
737 * Board-specific early init code calls this (probably during arch_initcall)
738 * with segments of the SPI device table. Any device nodes are created later,
739 * after the relevant parent SPI controller (bus_num) is defined. We keep
741 * not make Linux forget about these hard-wired devices.
743 * Other code can also call this, e.g. a particular add-on board might provide
744 * SPI devices through its expansion connector, so code initializing that board
745 * would naturally declare its SPI devices.
748 * any embedded pointers (platform_data, etc), they're copied as-is.
749 * Device properties are deep-copied though.
763 return -ENOMEM; in spi_register_board_info()
768 memcpy(&bi->board_info, info, sizeof(*info)); in spi_register_board_info()
769 if (info->properties) { in spi_register_board_info()
770 bi->board_info.properties = in spi_register_board_info()
771 property_entries_dup(info->properties); in spi_register_board_info()
772 if (IS_ERR(bi->board_info.properties)) in spi_register_board_info()
773 return PTR_ERR(bi->board_info.properties); in spi_register_board_info()
777 list_add_tail(&bi->list, &board_list); in spi_register_board_info()
780 &bi->board_info); in spi_register_board_info()
787 /*-------------------------------------------------------------------------*/
789 static void spi_set_cs(struct spi_device *spi, bool enable) in spi_set_cs() argument
797 if ((spi->controller->last_cs_enable == enable) && in spi_set_cs()
798 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
801 spi->controller->last_cs_enable = enable; in spi_set_cs()
802 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
804 if (!spi->controller->set_cs_timing) { in spi_set_cs()
806 spi_delay_exec(&spi->controller->cs_setup, NULL); in spi_set_cs()
808 spi_delay_exec(&spi->controller->cs_hold, NULL); in spi_set_cs()
811 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
814 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { in spi_set_cs()
815 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
816 if (spi->cs_gpiod) in spi_set_cs()
818 gpiod_set_value_cansleep(spi->cs_gpiod, in spi_set_cs()
823 * default for SPI. in spi_set_cs()
825 gpio_set_value_cansleep(spi->cs_gpio, !enable); in spi_set_cs()
827 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
828 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && in spi_set_cs()
829 spi->controller->set_cs) in spi_set_cs()
830 spi->controller->set_cs(spi, !enable); in spi_set_cs()
831 } else if (spi->controller->set_cs) { in spi_set_cs()
832 spi->controller->set_cs(spi, !enable); in spi_set_cs()
835 if (!spi->controller->set_cs_timing) { in spi_set_cs()
837 spi_delay_exec(&spi->controller->cs_inactive, NULL); in spi_set_cs()
842 int spi_map_buf(struct spi_controller *ctlr, struct device *dev, in spi_map_buf()
867 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); in spi_map_buf()
870 return -EINVAL; in spi_map_buf()
877 sg = &sgt->sgl[0]; in spi_map_buf()
888 PAGE_SIZE - offset_in_page(buf))); in spi_map_buf()
895 return -ENOMEM; in spi_map_buf()
906 len -= min; in spi_map_buf()
910 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); in spi_map_buf()
912 ret = -ENOMEM; in spi_map_buf()
918 sgt->nents = ret; in spi_map_buf()
923 void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, in spi_unmap_buf()
926 if (sgt->orig_nents) { in spi_unmap_buf()
927 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); in spi_unmap_buf()
934 struct device *tx_dev, *rx_dev; in __spi_map_msg()
938 if (!ctlr->can_dma) in __spi_map_msg()
941 if (ctlr->dma_tx) in __spi_map_msg()
942 tx_dev = ctlr->dma_tx->device->dev; in __spi_map_msg()
944 tx_dev = ctlr->dev.parent; in __spi_map_msg()
946 if (ctlr->dma_rx) in __spi_map_msg()
947 rx_dev = ctlr->dma_rx->device->dev; in __spi_map_msg()
949 rx_dev = ctlr->dev.parent; in __spi_map_msg()
951 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_map_msg()
952 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
955 if (xfer->tx_buf != NULL) { in __spi_map_msg()
956 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
957 (void *)xfer->tx_buf, xfer->len, in __spi_map_msg()
963 if (xfer->rx_buf != NULL) { in __spi_map_msg()
964 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, in __spi_map_msg()
965 xfer->rx_buf, xfer->len, in __spi_map_msg()
968 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, in __spi_map_msg()
975 ctlr->cur_msg_mapped = true; in __spi_map_msg()
983 struct device *tx_dev, *rx_dev; in __spi_unmap_msg()
985 if (!ctlr->cur_msg_mapped || !ctlr->can_dma) in __spi_unmap_msg()
988 if (ctlr->dma_tx) in __spi_unmap_msg()
989 tx_dev = ctlr->dma_tx->device->dev; in __spi_unmap_msg()
991 tx_dev = ctlr->dev.parent; in __spi_unmap_msg()
993 if (ctlr->dma_rx) in __spi_unmap_msg()
994 rx_dev = ctlr->dma_rx->device->dev; in __spi_unmap_msg()
996 rx_dev = ctlr->dev.parent; in __spi_unmap_msg()
998 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_unmap_msg()
999 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_unmap_msg()
1002 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); in __spi_unmap_msg()
1003 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); in __spi_unmap_msg()
1006 ctlr->cur_msg_mapped = false; in __spi_unmap_msg()
1029 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_unmap_msg()
1034 if (xfer->tx_buf == ctlr->dummy_tx) in spi_unmap_msg()
1035 xfer->tx_buf = NULL; in spi_unmap_msg()
1036 if (xfer->rx_buf == ctlr->dummy_rx) in spi_unmap_msg()
1037 xfer->rx_buf = NULL; in spi_unmap_msg()
1049 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) in spi_map_msg()
1050 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1054 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_map_msg()
1055 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && in spi_map_msg()
1056 !xfer->tx_buf) in spi_map_msg()
1057 max_tx = max(xfer->len, max_tx); in spi_map_msg()
1058 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && in spi_map_msg()
1059 !xfer->rx_buf) in spi_map_msg()
1060 max_rx = max(xfer->len, max_rx); in spi_map_msg()
1064 tmp = krealloc(ctlr->dummy_tx, max_tx, in spi_map_msg()
1067 return -ENOMEM; in spi_map_msg()
1068 ctlr->dummy_tx = tmp; in spi_map_msg()
1073 tmp = krealloc(ctlr->dummy_rx, max_rx, in spi_map_msg()
1076 return -ENOMEM; in spi_map_msg()
1077 ctlr->dummy_rx = tmp; in spi_map_msg()
1081 list_for_each_entry(xfer, &msg->transfers, in spi_map_msg()
1083 if (!xfer->len) in spi_map_msg()
1085 if (!xfer->tx_buf) in spi_map_msg()
1086 xfer->tx_buf = ctlr->dummy_tx; in spi_map_msg()
1087 if (!xfer->rx_buf) in spi_map_msg()
1088 xfer->rx_buf = ctlr->dummy_rx; in spi_map_msg()
1100 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_wait()
1101 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_wait()
1105 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { in spi_transfer_wait()
1106 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1107 return -EINTR; in spi_transfer_wait()
1110 ms = 8LL * 1000LL * xfer->len; in spi_transfer_wait()
1111 do_div(ms, xfer->speed_hz); in spi_transfer_wait()
1117 ms = wait_for_completion_timeout(&ctlr->xfer_completion, in spi_transfer_wait()
1123 dev_err(&msg->spi->dev, in spi_transfer_wait()
1124 "SPI transfer timed out\n"); in spi_transfer_wait()
1125 return -ETIMEDOUT; in spi_transfer_wait()
1150 u32 delay = _delay->value; in spi_delay_to_ns()
1151 u32 unit = _delay->unit; in spi_delay_to_ns()
1166 return -EINVAL; in spi_delay_to_ns()
1170 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; in spi_delay_to_ns()
1172 return -EINVAL; in spi_delay_to_ns()
1176 return -EINVAL; in spi_delay_to_ns()
1190 return -EINVAL; in spi_delay_exec()
1205 u32 delay = xfer->cs_change_delay.value; in _spi_transfer_cs_change_delay()
1206 u32 unit = xfer->cs_change_delay.unit; in _spi_transfer_cs_change_delay()
1209 /* return early on "fast" mode - for everything but USECS */ in _spi_transfer_cs_change_delay()
1216 ret = spi_delay_exec(&xfer->cs_change_delay, xfer); in _spi_transfer_cs_change_delay()
1218 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1226 * spi_transfer_one_message - Default implementation of transfer_one_message()
1238 struct spi_statistics *statm = &ctlr->statistics; in spi_transfer_one_message()
1239 struct spi_statistics *stats = &msg->spi->statistics; in spi_transfer_one_message()
1241 spi_set_cs(msg->spi, true); in spi_transfer_one_message()
1246 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_transfer_one_message()
1252 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1253 xfer->ptp_sts_word_pre = 0; in spi_transfer_one_message()
1254 ptp_read_system_prets(xfer->ptp_sts); in spi_transfer_one_message()
1257 if (xfer->tx_buf || xfer->rx_buf) { in spi_transfer_one_message()
1258 reinit_completion(&ctlr->xfer_completion); in spi_transfer_one_message()
1261 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1263 if (ctlr->cur_msg_mapped && in spi_transfer_one_message()
1264 (xfer->error & SPI_TRANS_FAIL_NO_START)) { in spi_transfer_one_message()
1266 ctlr->fallback = true; in spi_transfer_one_message()
1267 xfer->error &= ~SPI_TRANS_FAIL_NO_START; in spi_transfer_one_message()
1275 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1276 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1283 msg->status = ret; in spi_transfer_one_message()
1286 if (xfer->len) in spi_transfer_one_message()
1287 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1289 xfer->len); in spi_transfer_one_message()
1292 if (!ctlr->ptp_sts_supported) { in spi_transfer_one_message()
1293 ptp_read_system_postts(xfer->ptp_sts); in spi_transfer_one_message()
1294 xfer->ptp_sts_word_post = xfer->len; in spi_transfer_one_message()
1299 if (msg->status != -EINPROGRESS) in spi_transfer_one_message()
1304 if (xfer->cs_change) { in spi_transfer_one_message()
1305 if (list_is_last(&xfer->transfer_list, in spi_transfer_one_message()
1306 &msg->transfers)) { in spi_transfer_one_message()
1309 spi_set_cs(msg->spi, false); in spi_transfer_one_message()
1311 spi_set_cs(msg->spi, true); in spi_transfer_one_message()
1315 msg->actual_length += xfer->len; in spi_transfer_one_message()
1320 spi_set_cs(msg->spi, false); in spi_transfer_one_message()
1322 if (msg->status == -EINPROGRESS) in spi_transfer_one_message()
1323 msg->status = ret; in spi_transfer_one_message()
1325 if (msg->status && ctlr->handle_err) in spi_transfer_one_message()
1326 ctlr->handle_err(ctlr, msg); in spi_transfer_one_message()
1334 * spi_finalize_current_transfer - report completion of a transfer
1337 * Called by SPI drivers using the core transfer_one_message()
1343 complete(&ctlr->xfer_completion); in spi_finalize_current_transfer()
1349 if (ctlr->auto_runtime_pm) { in spi_idle_runtime_pm()
1350 pm_runtime_mark_last_busy(ctlr->dev.parent); in spi_idle_runtime_pm()
1351 pm_runtime_put_autosuspend(ctlr->dev.parent); in spi_idle_runtime_pm()
1356 * __spi_pump_messages - function which processes spi message queue
1360 * This function checks if there is any spi message in the queue that
1377 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1380 if (ctlr->cur_msg) { in __spi_pump_messages()
1381 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1385 /* If another context is idling the device then defer */ in __spi_pump_messages()
1386 if (ctlr->idling) { in __spi_pump_messages()
1387 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_pump_messages()
1388 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1393 if (list_empty(&ctlr->queue) || !ctlr->running) { in __spi_pump_messages()
1394 if (!ctlr->busy) { in __spi_pump_messages()
1395 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1399 /* Defer any non-atomic teardown to the thread */ in __spi_pump_messages()
1401 if (!ctlr->dummy_rx && !ctlr->dummy_tx && in __spi_pump_messages()
1402 !ctlr->unprepare_transfer_hardware) { in __spi_pump_messages()
1404 ctlr->busy = false; in __spi_pump_messages()
1407 kthread_queue_work(ctlr->kworker, in __spi_pump_messages()
1408 &ctlr->pump_messages); in __spi_pump_messages()
1410 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1414 ctlr->busy = false; in __spi_pump_messages()
1415 ctlr->idling = true; in __spi_pump_messages()
1416 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1418 kfree(ctlr->dummy_rx); in __spi_pump_messages()
1419 ctlr->dummy_rx = NULL; in __spi_pump_messages()
1420 kfree(ctlr->dummy_tx); in __spi_pump_messages()
1421 ctlr->dummy_tx = NULL; in __spi_pump_messages()
1422 if (ctlr->unprepare_transfer_hardware && in __spi_pump_messages()
1423 ctlr->unprepare_transfer_hardware(ctlr)) in __spi_pump_messages()
1424 dev_err(&ctlr->dev, in __spi_pump_messages()
1429 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_pump_messages()
1430 ctlr->idling = false; in __spi_pump_messages()
1431 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1436 msg = list_first_entry(&ctlr->queue, struct spi_message, queue); in __spi_pump_messages()
1437 ctlr->cur_msg = msg; in __spi_pump_messages()
1439 list_del_init(&msg->queue); in __spi_pump_messages()
1440 if (ctlr->busy) in __spi_pump_messages()
1443 ctlr->busy = true; in __spi_pump_messages()
1444 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_pump_messages()
1446 mutex_lock(&ctlr->io_mutex); in __spi_pump_messages()
1448 if (!was_busy && ctlr->auto_runtime_pm) { in __spi_pump_messages()
1449 ret = pm_runtime_get_sync(ctlr->dev.parent); in __spi_pump_messages()
1451 pm_runtime_put_noidle(ctlr->dev.parent); in __spi_pump_messages()
1452 dev_err(&ctlr->dev, "Failed to power device: %d\n", in __spi_pump_messages()
1454 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1462 if (!was_busy && ctlr->prepare_transfer_hardware) { in __spi_pump_messages()
1463 ret = ctlr->prepare_transfer_hardware(ctlr); in __spi_pump_messages()
1465 dev_err(&ctlr->dev, in __spi_pump_messages()
1469 if (ctlr->auto_runtime_pm) in __spi_pump_messages()
1470 pm_runtime_put(ctlr->dev.parent); in __spi_pump_messages()
1472 msg->status = ret; in __spi_pump_messages()
1475 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1482 if (ctlr->prepare_message) { in __spi_pump_messages()
1483 ret = ctlr->prepare_message(ctlr, msg); in __spi_pump_messages()
1485 dev_err(&ctlr->dev, "failed to prepare message: %d\n", in __spi_pump_messages()
1487 msg->status = ret; in __spi_pump_messages()
1491 ctlr->cur_msg_prepared = true; in __spi_pump_messages()
1496 msg->status = ret; in __spi_pump_messages()
1501 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in __spi_pump_messages()
1502 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in __spi_pump_messages()
1503 xfer->ptp_sts_word_pre = 0; in __spi_pump_messages()
1504 ptp_read_system_prets(xfer->ptp_sts); in __spi_pump_messages()
1508 ret = ctlr->transfer_one_message(ctlr, msg); in __spi_pump_messages()
1510 dev_err(&ctlr->dev, in __spi_pump_messages()
1516 mutex_unlock(&ctlr->io_mutex); in __spi_pump_messages()
1524 * spi_pump_messages - kthread work function which processes spi message queue
1536 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1537 * TX timestamp for the requested byte from the SPI
1560 if (!xfer->ptp_sts) in spi_take_timestamp_pre()
1563 if (xfer->timestamped) in spi_take_timestamp_pre()
1566 if (progress > xfer->ptp_sts_word_pre) in spi_take_timestamp_pre()
1570 xfer->ptp_sts_word_pre = progress; in spi_take_timestamp_pre()
1573 local_irq_save(ctlr->irq_flags); in spi_take_timestamp_pre()
1577 ptp_read_system_prets(xfer->ptp_sts); in spi_take_timestamp_pre()
1582 * spi_take_timestamp_post - helper for drivers to collect the end of the
1583 * TX timestamp for the requested byte from the SPI
1591 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1597 if (!xfer->ptp_sts) in spi_take_timestamp_post()
1600 if (xfer->timestamped) in spi_take_timestamp_post()
1603 if (progress < xfer->ptp_sts_word_post) in spi_take_timestamp_post()
1606 ptp_read_system_postts(xfer->ptp_sts); in spi_take_timestamp_post()
1609 local_irq_restore(ctlr->irq_flags); in spi_take_timestamp_post()
1614 xfer->ptp_sts_word_post = progress; in spi_take_timestamp_post()
1616 xfer->timestamped = true; in spi_take_timestamp_post()
1621 * spi_set_thread_rt - set the controller to pump at realtime priority
1625 * (by setting the ->rt value before calling spi_register_controller()) or
1626 * because a device on the bus said that its transfers needed realtime
1629 * NOTE: at the moment if any device on a bus says it needs realtime then
1637 dev_info(&ctlr->dev, in spi_set_thread_rt()
1639 sched_set_fifo(ctlr->kworker->task); in spi_set_thread_rt()
1644 ctlr->running = false; in spi_init_queue()
1645 ctlr->busy = false; in spi_init_queue()
1647 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); in spi_init_queue()
1648 if (IS_ERR(ctlr->kworker)) { in spi_init_queue()
1649 dev_err(&ctlr->dev, "failed to create message pump kworker\n"); in spi_init_queue()
1650 return PTR_ERR(ctlr->kworker); in spi_init_queue()
1653 kthread_init_work(&ctlr->pump_messages, spi_pump_messages); in spi_init_queue()
1662 if (ctlr->rt) in spi_init_queue()
1669 * spi_get_next_queued_message() - called by driver to check for queued
1684 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1685 next = list_first_entry_or_null(&ctlr->queue, struct spi_message, in spi_get_next_queued_message()
1687 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_get_next_queued_message()
1694 * spi_finalize_current_message() - the current message is complete
1707 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1708 mesg = ctlr->cur_msg; in spi_finalize_current_message()
1709 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1711 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { in spi_finalize_current_message()
1712 list_for_each_entry(xfer, &mesg->transfers, transfer_list) { in spi_finalize_current_message()
1713 ptp_read_system_postts(xfer->ptp_sts); in spi_finalize_current_message()
1714 xfer->ptp_sts_word_post = xfer->len; in spi_finalize_current_message()
1718 if (unlikely(ctlr->ptp_sts_supported)) in spi_finalize_current_message()
1719 list_for_each_entry(xfer, &mesg->transfers, transfer_list) in spi_finalize_current_message()
1720 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); in spi_finalize_current_message()
1724 /* In the prepare_messages callback the spi bus has the opportunity to in spi_finalize_current_message()
1731 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { in spi_finalize_current_message()
1732 ret = ctlr->unprepare_message(ctlr, mesg); in spi_finalize_current_message()
1734 dev_err(&ctlr->dev, "failed to unprepare message: %d\n", in spi_finalize_current_message()
1739 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1740 ctlr->cur_msg = NULL; in spi_finalize_current_message()
1741 ctlr->cur_msg_prepared = false; in spi_finalize_current_message()
1742 ctlr->fallback = false; in spi_finalize_current_message()
1743 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_finalize_current_message()
1744 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_finalize_current_message()
1748 mesg->state = NULL; in spi_finalize_current_message()
1749 if (mesg->complete) in spi_finalize_current_message()
1750 mesg->complete(mesg->context); in spi_finalize_current_message()
1758 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_start_queue()
1760 if (ctlr->running || ctlr->busy) { in spi_start_queue()
1761 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1762 return -EBUSY; in spi_start_queue()
1765 ctlr->running = true; in spi_start_queue()
1766 ctlr->cur_msg = NULL; in spi_start_queue()
1767 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_start_queue()
1769 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in spi_start_queue()
1780 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1784 * A wait_queue on the ctlr->busy could be used, but then the common in spi_stop_queue()
1786 * friends on every SPI message. Do this instead. in spi_stop_queue()
1788 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { in spi_stop_queue()
1789 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1791 spin_lock_irqsave(&ctlr->queue_lock, flags); in spi_stop_queue()
1794 if (!list_empty(&ctlr->queue) || ctlr->busy) in spi_stop_queue()
1795 ret = -EBUSY; in spi_stop_queue()
1797 ctlr->running = false; in spi_stop_queue()
1799 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in spi_stop_queue()
1802 dev_warn(&ctlr->dev, "could not stop message queue\n"); in spi_stop_queue()
1821 dev_err(&ctlr->dev, "problem destroying queue\n"); in spi_destroy_queue()
1825 kthread_destroy_worker(ctlr->kworker); in spi_destroy_queue()
1830 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
1834 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
1837 spin_lock_irqsave(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1839 if (!ctlr->running) { in __spi_queued_transfer()
1840 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1841 return -ESHUTDOWN; in __spi_queued_transfer()
1843 msg->actual_length = 0; in __spi_queued_transfer()
1844 msg->status = -EINPROGRESS; in __spi_queued_transfer()
1846 list_add_tail(&msg->queue, &ctlr->queue); in __spi_queued_transfer()
1847 if (!ctlr->busy && need_pump) in __spi_queued_transfer()
1848 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); in __spi_queued_transfer()
1850 spin_unlock_irqrestore(&ctlr->queue_lock, flags); in __spi_queued_transfer()
1855 * spi_queued_transfer - transfer function for queued transfers
1856 * @spi: spi device which is requesting transfer
1857 * @msg: spi message which is to handled is queued to driver queue
1861 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
1863 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
1870 ctlr->transfer = spi_queued_transfer; in spi_controller_initialize_queue()
1871 if (!ctlr->transfer_one_message) in spi_controller_initialize_queue()
1872 ctlr->transfer_one_message = spi_transfer_one_message; in spi_controller_initialize_queue()
1877 dev_err(&ctlr->dev, "problem initializing queue\n"); in spi_controller_initialize_queue()
1880 ctlr->queued = true; in spi_controller_initialize_queue()
1883 dev_err(&ctlr->dev, "problem starting queue\n"); in spi_controller_initialize_queue()
1896 * spi_flush_queue - Send all pending messages in the queue from the callers'
1901 * sent before doing something. Is used by the spi-mem code to make sure SPI
1902 * memory operations do not preempt regular SPI transfers that have been queued
1903 * before the spi-mem operation.
1907 if (ctlr->transfer == spi_queued_transfer) in spi_flush_queue()
1911 /*-------------------------------------------------------------------------*/
1914 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
1921 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
1922 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
1923 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
1924 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
1925 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
1926 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
1927 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
1928 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
1929 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
1930 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
1932 /* Device DUAL/QUAD mode */ in of_spi_parse_dt()
1933 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
1938 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
1941 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
1944 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
1947 dev_warn(&ctlr->dev, in of_spi_parse_dt()
1948 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
1954 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
1959 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
1962 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
1965 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
1968 dev_warn(&ctlr->dev, in of_spi_parse_dt()
1969 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
1977 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", in of_spi_parse_dt()
1979 return -EINVAL; in of_spi_parse_dt()
1984 /* Device address */ in of_spi_parse_dt()
1987 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", in of_spi_parse_dt()
1991 spi->chip_select = value; in of_spi_parse_dt()
1993 /* Device speed */ in of_spi_parse_dt()
1994 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
1995 spi->max_speed_hz = value; in of_spi_parse_dt()
2003 struct spi_device *spi; in of_register_spi_device() local
2007 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2008 if (!spi) { in of_register_spi_device()
2009 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); in of_register_spi_device()
2010 rc = -ENOMEM; in of_register_spi_device()
2014 /* Select device driver */ in of_register_spi_device()
2015 rc = of_modalias_node(nc, spi->modalias, in of_register_spi_device()
2016 sizeof(spi->modalias)); in of_register_spi_device()
2018 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); in of_register_spi_device()
2022 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2026 /* Store a pointer to the node in the device structure */ in of_register_spi_device()
2028 spi->dev.of_node = nc; in of_register_spi_device()
2030 /* Register the new device */ in of_register_spi_device()
2031 rc = spi_add_device(spi); in of_register_spi_device()
2033 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); in of_register_spi_device()
2037 return spi; in of_register_spi_device()
2042 spi_dev_put(spi); in of_register_spi_device()
2047 * of_register_spi_devices() - Register child devices onto the SPI bus
2048 * @ctlr: Pointer to spi_controller device
2051 * represents a valid SPI slave.
2055 struct spi_device *spi; in of_register_spi_devices() local
2058 if (!ctlr->dev.of_node) in of_register_spi_devices()
2061 for_each_available_child_of_node(ctlr->dev.of_node, nc) { in of_register_spi_devices()
2064 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2065 if (IS_ERR(spi)) { in of_register_spi_devices()
2066 dev_warn(&ctlr->dev, in of_register_spi_devices()
2067 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2095 && obj->buffer.length >= 4) in acpi_spi_parse_apple_properties()
2096 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2099 && obj->buffer.length == 8) in acpi_spi_parse_apple_properties()
2100 lookup->bits_per_word = *(u64 *)obj->buffer.pointer; in acpi_spi_parse_apple_properties()
2103 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2104 lookup->mode |= SPI_LSB_FIRST; in acpi_spi_parse_apple_properties()
2107 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2108 lookup->mode |= SPI_CPOL; in acpi_spi_parse_apple_properties()
2111 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) in acpi_spi_parse_apple_properties()
2112 lookup->mode |= SPI_CPHA; in acpi_spi_parse_apple_properties()
2118 struct spi_controller *ctlr = lookup->ctlr; in acpi_spi_add_resource()
2120 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { in acpi_spi_add_resource()
2125 sb = &ares->data.spi_serial_bus; in acpi_spi_add_resource()
2126 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { in acpi_spi_add_resource()
2129 sb->resource_source.string_ptr, in acpi_spi_add_resource()
2133 ACPI_HANDLE(ctlr->dev.parent) != parent_handle) in acpi_spi_add_resource()
2134 return -ENODEV; in acpi_spi_add_resource()
2140 * 0 .. max - 1 so we need to ask the driver to in acpi_spi_add_resource()
2143 if (ctlr->fw_translate_cs) { in acpi_spi_add_resource()
2144 int cs = ctlr->fw_translate_cs(ctlr, in acpi_spi_add_resource()
2145 sb->device_selection); in acpi_spi_add_resource()
2148 lookup->chip_select = cs; in acpi_spi_add_resource()
2150 lookup->chip_select = sb->device_selection; in acpi_spi_add_resource()
2153 lookup->max_speed_hz = sb->connection_speed; in acpi_spi_add_resource()
2154 lookup->bits_per_word = sb->data_bit_length; in acpi_spi_add_resource()
2156 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) in acpi_spi_add_resource()
2157 lookup->mode |= SPI_CPHA; in acpi_spi_add_resource()
2158 if (sb->clock_polarity == ACPI_SPI_START_HIGH) in acpi_spi_add_resource()
2159 lookup->mode |= SPI_CPOL; in acpi_spi_add_resource()
2160 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) in acpi_spi_add_resource()
2161 lookup->mode |= SPI_CS_HIGH; in acpi_spi_add_resource()
2163 } else if (lookup->irq < 0) { in acpi_spi_add_resource()
2167 lookup->irq = r.start; in acpi_spi_add_resource()
2180 struct spi_device *spi; in acpi_register_spi_device() local
2183 if (acpi_bus_get_status(adev) || !adev->status.present || in acpi_register_spi_device()
2188 lookup.irq = -1; in acpi_register_spi_device()
2196 /* found SPI in _CRS but it points to another controller */ in acpi_register_spi_device()
2200 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) && in acpi_register_spi_device()
2201 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { in acpi_register_spi_device()
2202 /* Apple does not use _CRS but nested devices for SPI slaves */ in acpi_register_spi_device()
2209 spi = spi_alloc_device(ctlr); in acpi_register_spi_device()
2210 if (!spi) { in acpi_register_spi_device()
2211 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_register_spi_device()
2212 dev_name(&adev->dev)); in acpi_register_spi_device()
2217 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_register_spi_device()
2218 spi->max_speed_hz = lookup.max_speed_hz; in acpi_register_spi_device()
2219 spi->mode |= lookup.mode; in acpi_register_spi_device()
2220 spi->irq = lookup.irq; in acpi_register_spi_device()
2221 spi->bits_per_word = lookup.bits_per_word; in acpi_register_spi_device()
2222 spi->chip_select = lookup.chip_select; in acpi_register_spi_device()
2224 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2225 sizeof(spi->modalias)); in acpi_register_spi_device()
2227 if (spi->irq < 0) in acpi_register_spi_device()
2228 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in acpi_register_spi_device()
2232 adev->power.flags.ignore_parent = true; in acpi_register_spi_device()
2233 if (spi_add_device(spi)) { in acpi_register_spi_device()
2234 adev->power.flags.ignore_parent = false; in acpi_register_spi_device()
2235 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2236 dev_name(&adev->dev)); in acpi_register_spi_device()
2237 spi_dev_put(spi); in acpi_register_spi_device()
2262 handle = ACPI_HANDLE(ctlr->dev.parent); in acpi_register_spi_devices()
2270 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2276 static void spi_controller_release(struct device *dev) in spi_controller_release()
2293 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2295 * @spi: device used for the current transfer
2297 int spi_slave_abort(struct spi_device *spi) in spi_slave_abort() argument
2299 struct spi_controller *ctlr = spi->controller; in spi_slave_abort()
2301 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) in spi_slave_abort()
2302 return ctlr->slave_abort(ctlr); in spi_slave_abort()
2304 return -ENOTSUPP; in spi_slave_abort()
2308 static int match_true(struct device *dev, void *data) in match_true()
2313 static ssize_t slave_show(struct device *dev, struct device_attribute *attr, in slave_show()
2318 struct device *child; in slave_show()
2320 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_show()
2322 child ? to_spi_device(child)->modalias : NULL); in slave_show()
2325 static ssize_t slave_store(struct device *dev, struct device_attribute *attr, in slave_store()
2330 struct spi_device *spi; in slave_store() local
2331 struct device *child; in slave_store()
2337 return -EINVAL; in slave_store()
2339 child = device_find_child(&ctlr->dev, NULL, match_true); in slave_store()
2348 spi = spi_alloc_device(ctlr); in slave_store()
2349 if (!spi) in slave_store()
2350 return -ENOMEM; in slave_store()
2352 strlcpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2354 rc = spi_add_device(spi); in slave_store()
2356 spi_dev_put(spi); in slave_store()
2392 * __spi_alloc_controller - allocate an SPI master or slave controller
2394 * @size: how much zeroed driver-private data to allocate; the pointer to this
2395 * memory is in the driver_data field of the returned device, accessible
2399 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2403 * This call is used only by SPI controller drivers, which are the
2411 * errors adding the device) calling spi_controller_put() to prevent a memory
2414 * Return: the SPI controller structure on success, else NULL.
2416 struct spi_controller *__spi_alloc_controller(struct device *dev, in __spi_alloc_controller()
2429 device_initialize(&ctlr->dev); in __spi_alloc_controller()
2430 ctlr->bus_num = -1; in __spi_alloc_controller()
2431 ctlr->num_chipselect = 1; in __spi_alloc_controller()
2432 ctlr->slave = slave; in __spi_alloc_controller()
2434 ctlr->dev.class = &spi_slave_class; in __spi_alloc_controller()
2436 ctlr->dev.class = &spi_master_class; in __spi_alloc_controller()
2437 ctlr->dev.parent = dev; in __spi_alloc_controller()
2438 pm_suspend_ignore_children(&ctlr->dev, true); in __spi_alloc_controller()
2445 static void devm_spi_release_controller(struct device *dev, void *ctlr) in devm_spi_release_controller()
2451 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
2452 * @dev: physical device of SPI controller
2453 * @size: how much zeroed driver-private data to allocate
2454 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
2457 * Allocate an SPI controller and automatically release a reference on it
2463 * Return: the SPI controller structure on success, else NULL.
2465 struct spi_controller *__devm_spi_alloc_controller(struct device *dev, in __devm_spi_alloc_controller()
2492 struct device_node *np = ctlr->dev.of_node; in of_spi_get_gpio_numbers()
2497 nb = of_gpio_named_count(np, "cs-gpios"); in of_spi_get_gpio_numbers()
2498 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in of_spi_get_gpio_numbers()
2500 /* Return error only for an incorrectly formed cs-gpios property */ in of_spi_get_gpio_numbers()
2501 if (nb == 0 || nb == -ENOENT) in of_spi_get_gpio_numbers()
2506 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), in of_spi_get_gpio_numbers()
2508 ctlr->cs_gpios = cs; in of_spi_get_gpio_numbers()
2510 if (!ctlr->cs_gpios) in of_spi_get_gpio_numbers()
2511 return -ENOMEM; in of_spi_get_gpio_numbers()
2513 for (i = 0; i < ctlr->num_chipselect; i++) in of_spi_get_gpio_numbers()
2514 cs[i] = -ENOENT; in of_spi_get_gpio_numbers()
2517 cs[i] = of_get_named_gpio(np, "cs-gpios", i); in of_spi_get_gpio_numbers()
2529 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2530 * @ctlr: The SPI master to grab GPIO descriptors for
2536 struct device *dev = &ctlr->dev; in spi_get_gpio_descs()
2541 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); in spi_get_gpio_descs()
2544 if (nb == 0 || nb == -ENOENT) in spi_get_gpio_descs()
2549 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), in spi_get_gpio_descs()
2552 return -ENOMEM; in spi_get_gpio_descs()
2553 ctlr->cs_gpiods = cs; in spi_get_gpio_descs()
2570 * If we find a CS GPIO, name it after the device and in spi_get_gpio_descs()
2578 return -ENOMEM; in spi_get_gpio_descs()
2584 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2586 return -EINVAL; in spi_get_gpio_descs()
2591 ctlr->unused_native_cs = ffz(native_cs_mask); in spi_get_gpio_descs()
2592 if (num_cs_gpios && ctlr->max_native_cs && in spi_get_gpio_descs()
2593 ctlr->unused_native_cs >= ctlr->max_native_cs) { in spi_get_gpio_descs()
2595 return -EINVAL; in spi_get_gpio_descs()
2604 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
2605 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
2607 * If ->mem_ops is NULL, we request that at least one of the in spi_controller_check_ops()
2608 * ->transfer_xxx() method be implemented. in spi_controller_check_ops()
2610 if (ctlr->mem_ops) { in spi_controller_check_ops()
2611 if (!ctlr->mem_ops->exec_op) in spi_controller_check_ops()
2612 return -EINVAL; in spi_controller_check_ops()
2613 } else if (!ctlr->transfer && !ctlr->transfer_one && in spi_controller_check_ops()
2614 !ctlr->transfer_one_message) { in spi_controller_check_ops()
2615 return -EINVAL; in spi_controller_check_ops()
2622 * spi_register_controller - register SPI master or slave controller
2627 * SPI controllers connect to their drivers using some non-SPI bus,
2629 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2631 * SPI controllers use board specific (often SOC specific) bus numbers,
2632 * and board-specific addressing for SPI devices combines those numbers
2633 * with chip select numbers. Since SPI does not directly support dynamic
2634 * device identification, boards need configuration tables telling which
2646 struct device *dev = ctlr->dev.parent; in spi_register_controller()
2652 return -ENODEV; in spi_register_controller()
2656 * the SPI controller. in spi_register_controller()
2662 if (ctlr->bus_num >= 0) { in spi_register_controller()
2663 /* devices with a fixed bus num must check-in with the num */ in spi_register_controller()
2665 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2666 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2669 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2670 ctlr->bus_num = id; in spi_register_controller()
2671 } else if (ctlr->dev.of_node) { in spi_register_controller()
2673 id = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
2675 ctlr->bus_num = id; in spi_register_controller()
2677 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, in spi_register_controller()
2678 ctlr->bus_num + 1, GFP_KERNEL); in spi_register_controller()
2681 return id == -ENOSPC ? -EBUSY : id; in spi_register_controller()
2684 if (ctlr->bus_num < 0) { in spi_register_controller()
2685 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
2697 ctlr->bus_num = id; in spi_register_controller()
2699 INIT_LIST_HEAD(&ctlr->queue); in spi_register_controller()
2700 spin_lock_init(&ctlr->queue_lock); in spi_register_controller()
2701 spin_lock_init(&ctlr->bus_lock_spinlock); in spi_register_controller()
2702 mutex_init(&ctlr->bus_lock_mutex); in spi_register_controller()
2703 mutex_init(&ctlr->io_mutex); in spi_register_controller()
2704 ctlr->bus_lock_flag = 0; in spi_register_controller()
2705 init_completion(&ctlr->xfer_completion); in spi_register_controller()
2706 if (!ctlr->max_dma_len) in spi_register_controller()
2707 ctlr->max_dma_len = INT_MAX; in spi_register_controller()
2709 /* register the device, then userspace will see it. in spi_register_controller()
2712 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
2715 if (ctlr->use_gpio_descriptors) { in spi_register_controller()
2723 ctlr->mode_bits |= SPI_CS_HIGH; in spi_register_controller()
2733 * Even if it's just one always-selected device, there must in spi_register_controller()
2736 if (!ctlr->num_chipselect) { in spi_register_controller()
2737 status = -EINVAL; in spi_register_controller()
2741 status = device_add(&ctlr->dev); in spi_register_controller()
2746 dev_name(&ctlr->dev)); in spi_register_controller()
2750 * need the queueing logic if the driver is only supporting high-level in spi_register_controller()
2753 if (ctlr->transfer) { in spi_register_controller()
2755 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { in spi_register_controller()
2758 device_del(&ctlr->dev); in spi_register_controller()
2763 spin_lock_init(&ctlr->statistics.lock); in spi_register_controller()
2766 list_add_tail(&ctlr->list, &spi_controller_list); in spi_register_controller()
2768 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); in spi_register_controller()
2771 /* Register devices from the device tree and ACPI */ in spi_register_controller()
2778 idr_remove(&spi_master_idr, ctlr->bus_num); in spi_register_controller()
2784 static void devm_spi_unregister(struct device *dev, void *res) in devm_spi_unregister()
2790 * devm_spi_register_controller - register managed SPI master or slave
2792 * @dev: device managing SPI controller
2797 * Register a SPI device as with spi_register_controller() which will
2802 int devm_spi_register_controller(struct device *dev, in devm_spi_register_controller()
2810 return -ENOMEM; in devm_spi_register_controller()
2824 static int devm_spi_match_controller(struct device *dev, void *res, void *ctlr) in devm_spi_match_controller()
2829 static int __unregister(struct device *dev, void *null) in __unregister()
2836 * spi_unregister_controller - unregister SPI master or slave controller
2840 * This call is used only by SPI controller drivers, which are the
2850 int id = ctlr->bus_num; in spi_unregister_controller()
2856 device_for_each_child(&ctlr->dev, NULL, __unregister); in spi_unregister_controller()
2862 if (ctlr->queued) { in spi_unregister_controller()
2864 dev_err(&ctlr->dev, "queue remove failed\n"); in spi_unregister_controller()
2867 list_del(&ctlr->list); in spi_unregister_controller()
2870 device_del(&ctlr->dev); in spi_unregister_controller()
2875 if (!devres_find(ctlr->dev.parent, devm_spi_release_controller, in spi_unregister_controller()
2877 put_device(&ctlr->dev); in spi_unregister_controller()
2894 /* Basically no-ops for non-queued controllers */ in spi_controller_suspend()
2895 if (!ctlr->queued) in spi_controller_suspend()
2900 dev_err(&ctlr->dev, "queue stop failed\n"); in spi_controller_suspend()
2910 if (!ctlr->queued) in spi_controller_resume()
2915 dev_err(&ctlr->dev, "queue restart failed\n"); in spi_controller_resume()
2921 static int __spi_controller_match(struct device *dev, const void *data) in __spi_controller_match()
2927 return ctlr->bus_num == *bus_num; in __spi_controller_match()
2931 * spi_busnum_to_master - look up master associated with bus_num
2940 * Return: the SPI master structure on success, else NULL.
2944 struct device *dev; in spi_busnum_to_master()
2956 /*-------------------------------------------------------------------------*/
2958 /* Core methods for SPI resource management */
2961 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2964 * @spi: the spi device for which we allocate memory
2974 void *spi_res_alloc(struct spi_device *spi, in spi_res_alloc() argument
2984 INIT_LIST_HEAD(&sres->entry); in spi_res_alloc()
2985 sres->release = release; in spi_res_alloc()
2987 return sres->data; in spi_res_alloc()
2992 * spi_res_free - free an spi resource
3003 WARN_ON(!list_empty(&sres->entry)); in spi_res_free()
3009 * spi_res_add - add a spi_res to the spi_message
3010 * @message: the spi message
3017 WARN_ON(!list_empty(&sres->entry)); in spi_res_add()
3018 list_add_tail(&sres->entry, &message->resources); in spi_res_add()
3023 * spi_res_release - release all spi resources for this message
3031 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { in spi_res_release()
3032 if (res->release) in spi_res_release()
3033 res->release(ctlr, message, res->data); in spi_res_release()
3035 list_del(&res->entry); in spi_res_release()
3042 /*-------------------------------------------------------------------------*/
3054 if (rxfer->release) in __spi_replace_transfers_release()
3055 rxfer->release(ctlr, msg, res); in __spi_replace_transfers_release()
3058 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); in __spi_replace_transfers_release()
3061 for (i = 0; i < rxfer->inserted; i++) in __spi_replace_transfers_release()
3062 list_del(&rxfer->inserted_transfers[i].transfer_list); in __spi_replace_transfers_release()
3066 * spi_replace_transfers - replace transfers with several transfers
3094 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3099 return ERR_PTR(-ENOMEM); in spi_replace_transfers()
3102 rxfer->release = release; in spi_replace_transfers()
3106 rxfer->extradata = in spi_replace_transfers()
3107 &rxfer->inserted_transfers[insert]; in spi_replace_transfers()
3110 INIT_LIST_HEAD(&rxfer->replaced_transfers); in spi_replace_transfers()
3113 * the @replaced_transfers - it may be spi_message.messages! in spi_replace_transfers()
3115 rxfer->replaced_after = xfer_first->transfer_list.prev; in spi_replace_transfers()
3119 /* if the entry after replaced_after it is msg->transfers in spi_replace_transfers()
3123 if (rxfer->replaced_after->next == &msg->transfers) { in spi_replace_transfers()
3124 dev_err(&msg->spi->dev, in spi_replace_transfers()
3127 list_splice(&rxfer->replaced_transfers, in spi_replace_transfers()
3128 rxfer->replaced_after); in spi_replace_transfers()
3134 return ERR_PTR(-EINVAL); in spi_replace_transfers()
3140 list_move_tail(rxfer->replaced_after->next, in spi_replace_transfers()
3141 &rxfer->replaced_transfers); in spi_replace_transfers()
3149 xfer = &rxfer->inserted_transfers[insert - 1 - i]; in spi_replace_transfers()
3155 list_add(&xfer->transfer_list, rxfer->replaced_after); in spi_replace_transfers()
3159 xfer->cs_change = false; in spi_replace_transfers()
3160 xfer->delay_usecs = 0; in spi_replace_transfers()
3161 xfer->delay.value = 0; in spi_replace_transfers()
3166 rxfer->inserted = insert; in spi_replace_transfers()
3187 count = DIV_ROUND_UP(xfer->len, maxsize); in __spi_split_transfer_maxsize()
3193 xfers = srt->inserted_transfers; in __spi_split_transfer_maxsize()
3223 xfers[i].len = min(maxsize, xfers[i].len - offset); in __spi_split_transfer_maxsize()
3229 *xferp = &xfers[count - 1]; in __spi_split_transfer_maxsize()
3232 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_split_transfer_maxsize()
3234 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, in __spi_split_transfer_maxsize()
3241 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
3265 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_split_transfers_maxsize()
3266 if (xfer->len > maxsize) { in spi_split_transfers_maxsize()
3278 /*-------------------------------------------------------------------------*/
3280 /* Core methods for SPI controller protocol drivers. Some of the
3287 if (ctlr->bits_per_word_mask) { in __spi_validate_bits_per_word()
3290 return -EINVAL; in __spi_validate_bits_per_word()
3291 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) in __spi_validate_bits_per_word()
3292 return -EINVAL; in __spi_validate_bits_per_word()
3299 * spi_setup - setup SPI mode and clock rate
3300 * @spi: the device whose settings are being modified
3301 * Context: can sleep, and no requests are queued to the device
3303 * SPI protocol drivers may need to update the transfer mode if the
3304 * device doesn't work with its default. They may likewise need
3308 * effect the next time the device is selected and data is transferred to
3309 * or from it. When this function returns, the spi device is deselected.
3314 * LSB-first wire encoding, or active-high chipselects.
3318 int spi_setup(struct spi_device *spi) in spi_setup() argument
3325 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || in spi_setup()
3326 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { in spi_setup()
3327 dev_err(&spi->dev, in spi_setup()
3329 return -EINVAL; in spi_setup()
3333 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3336 return -EINVAL; in spi_setup()
3342 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); in spi_setup()
3343 /* nothing prevents from working with active-high CS in case if it in spi_setup()
3346 if (gpio_is_valid(spi->cs_gpio)) in spi_setup()
3352 dev_warn(&spi->dev, in spi_setup()
3355 spi->mode &= ~ugly_bits; in spi_setup()
3359 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3361 return -EINVAL; in spi_setup()
3364 if (!spi->bits_per_word) in spi_setup()
3365 spi->bits_per_word = 8; in spi_setup()
3367 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3368 spi->bits_per_word); in spi_setup()
3372 if (!spi->max_speed_hz) in spi_setup()
3373 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3375 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3377 if (spi->controller->setup) in spi_setup()
3378 status = spi->controller->setup(spi); in spi_setup()
3380 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3381 status = pm_runtime_get_sync(spi->controller->dev.parent); in spi_setup()
3383 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3384 pm_runtime_put_noidle(spi->controller->dev.parent); in spi_setup()
3385 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3393 * checking for a non-zero return value instead of a negative in spi_setup()
3398 spi_set_cs(spi, false); in spi_setup()
3399 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3400 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
3402 spi_set_cs(spi, false); in spi_setup()
3405 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3407 if (spi->rt && !spi->controller->rt) { in spi_setup()
3408 spi->controller->rt = true; in spi_setup()
3409 spi_set_thread_rt(spi->controller); in spi_setup()
3412 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
3413 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), in spi_setup()
3414 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
3415 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
3416 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
3417 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
3418 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
3426 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3427 * @spi: the device that requires specific CS timing configuration
3434 int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup, in spi_set_cs_timing() argument
3439 if (spi->controller->set_cs_timing) in spi_set_cs_timing()
3440 return spi->controller->set_cs_timing(spi, setup, hold, in spi_set_cs_timing()
3443 if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) || in spi_set_cs_timing()
3444 (hold && hold->unit == SPI_DELAY_UNIT_SCK) || in spi_set_cs_timing()
3445 (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) { in spi_set_cs_timing()
3446 dev_err(&spi->dev, in spi_set_cs_timing()
3447 "Clock-cycle delays for CS not supported in SW mode\n"); in spi_set_cs_timing()
3448 return -ENOTSUPP; in spi_set_cs_timing()
3455 memcpy(&spi->controller->cs_setup, setup, len); in spi_set_cs_timing()
3457 memset(&spi->controller->cs_setup, 0, len); in spi_set_cs_timing()
3460 memcpy(&spi->controller->cs_hold, hold, len); in spi_set_cs_timing()
3462 memset(&spi->controller->cs_hold, 0, len); in spi_set_cs_timing()
3465 memcpy(&spi->controller->cs_inactive, inactive, len); in spi_set_cs_timing()
3467 memset(&spi->controller->cs_inactive, 0, len); in spi_set_cs_timing()
3474 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
3478 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); in _spi_xfer_word_delay_update()
3482 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
3487 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
3488 sizeof(xfer->word_delay)); in _spi_xfer_word_delay_update()
3493 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
3495 struct spi_controller *ctlr = spi->controller; in __spi_validate()
3499 if (list_empty(&message->transfers)) in __spi_validate()
3500 return -EINVAL; in __spi_validate()
3502 /* If an SPI controller does not support toggling the CS line on each in __spi_validate()
3504 * for the CS line, we can emulate the CS-per-word hardware function by in __spi_validate()
3505 * splitting transfers into one-word transfers and ensuring that in __spi_validate()
3508 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || in __spi_validate()
3509 spi->cs_gpiod || in __spi_validate()
3510 gpio_is_valid(spi->cs_gpio))) { in __spi_validate()
3514 maxsize = (spi->bits_per_word + 7) / 8; in __spi_validate()
3516 /* spi_split_transfers_maxsize() requires message->spi */ in __spi_validate()
3517 message->spi = spi; in __spi_validate()
3524 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3526 if (list_is_last(&xfer->transfer_list, &message->transfers)) in __spi_validate()
3528 xfer->cs_change = 1; in __spi_validate()
3532 /* Half-duplex links include original MicroWire, and ones with in __spi_validate()
3537 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || in __spi_validate()
3538 (spi->mode & SPI_3WIRE)) { in __spi_validate()
3539 unsigned flags = ctlr->flags; in __spi_validate()
3541 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3542 if (xfer->rx_buf && xfer->tx_buf) in __spi_validate()
3543 return -EINVAL; in __spi_validate()
3544 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) in __spi_validate()
3545 return -EINVAL; in __spi_validate()
3546 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) in __spi_validate()
3547 return -EINVAL; in __spi_validate()
3552 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
3557 * device itself. in __spi_validate()
3559 message->frame_length = 0; in __spi_validate()
3560 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_validate()
3561 xfer->effective_speed_hz = 0; in __spi_validate()
3562 message->frame_length += xfer->len; in __spi_validate()
3563 if (!xfer->bits_per_word) in __spi_validate()
3564 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
3566 if (!xfer->speed_hz) in __spi_validate()
3567 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
3569 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) in __spi_validate()
3570 xfer->speed_hz = ctlr->max_speed_hz; in __spi_validate()
3572 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) in __spi_validate()
3573 return -EINVAL; in __spi_validate()
3576 * SPI transfer length should be multiple of SPI word size in __spi_validate()
3577 * where SPI word size should be power-of-two multiple in __spi_validate()
3579 if (xfer->bits_per_word <= 8) in __spi_validate()
3581 else if (xfer->bits_per_word <= 16) in __spi_validate()
3587 if (xfer->len % w_size) in __spi_validate()
3588 return -EINVAL; in __spi_validate()
3590 if (xfer->speed_hz && ctlr->min_speed_hz && in __spi_validate()
3591 xfer->speed_hz < ctlr->min_speed_hz) in __spi_validate()
3592 return -EINVAL; in __spi_validate()
3594 if (xfer->tx_buf && !xfer->tx_nbits) in __spi_validate()
3595 xfer->tx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3596 if (xfer->rx_buf && !xfer->rx_nbits) in __spi_validate()
3597 xfer->rx_nbits = SPI_NBITS_SINGLE; in __spi_validate()
3602 if (xfer->tx_buf) { in __spi_validate()
3603 if (xfer->tx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3604 xfer->tx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3605 xfer->tx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3606 return -EINVAL; in __spi_validate()
3607 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3608 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
3609 return -EINVAL; in __spi_validate()
3610 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3611 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
3612 return -EINVAL; in __spi_validate()
3615 if (xfer->rx_buf) { in __spi_validate()
3616 if (xfer->rx_nbits != SPI_NBITS_SINGLE && in __spi_validate()
3617 xfer->rx_nbits != SPI_NBITS_DUAL && in __spi_validate()
3618 xfer->rx_nbits != SPI_NBITS_QUAD) in __spi_validate()
3619 return -EINVAL; in __spi_validate()
3620 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && in __spi_validate()
3621 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
3622 return -EINVAL; in __spi_validate()
3623 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && in __spi_validate()
3624 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
3625 return -EINVAL; in __spi_validate()
3628 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
3629 return -EINVAL; in __spi_validate()
3632 message->status = -EINPROGRESS; in __spi_validate()
3637 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
3639 struct spi_controller *ctlr = spi->controller; in __spi_async()
3643 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
3646 if (!ctlr->transfer) in __spi_async()
3647 return -ENOTSUPP; in __spi_async()
3649 message->spi = spi; in __spi_async()
3651 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); in __spi_async()
3652 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); in __spi_async()
3656 if (!ctlr->ptp_sts_supported) { in __spi_async()
3657 list_for_each_entry(xfer, &message->transfers, transfer_list) { in __spi_async()
3658 xfer->ptp_sts_word_pre = 0; in __spi_async()
3659 ptp_read_system_prets(xfer->ptp_sts); in __spi_async()
3663 return ctlr->transfer(spi, message); in __spi_async()
3667 * spi_async - asynchronous SPI transfer
3668 * @spi: device with which data will be exchanged
3676 * Before that invocation, the value of message->status is undefined.
3677 * When the callback is issued, message->status holds either zero (to
3680 * deallocate the associated memory; it's no longer in use by any SPI
3685 * Some device might be higher priority, or have various "hard" access
3689 * the entire message is aborted, and the device is deselected.
3691 * no other spi_message queued to that device will be processed.
3697 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
3699 struct spi_controller *ctlr = spi->controller; in spi_async()
3703 ret = __spi_validate(spi, message); in spi_async()
3707 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async()
3709 if (ctlr->bus_lock_flag) in spi_async()
3710 ret = -EBUSY; in spi_async()
3712 ret = __spi_async(spi, message); in spi_async()
3714 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async()
3721 * spi_async_locked - version of spi_async with exclusive bus usage
3722 * @spi: device with which data will be exchanged
3730 * Before that invocation, the value of message->status is undefined.
3731 * When the callback is issued, message->status holds either zero (to
3734 * deallocate the associated memory; it's no longer in use by any SPI
3739 * Some device might be higher priority, or have various "hard" access
3743 * the entire message is aborted, and the device is deselected.
3745 * no other spi_message queued to that device will be processed.
3751 int spi_async_locked(struct spi_device *spi, struct spi_message *message) in spi_async_locked() argument
3753 struct spi_controller *ctlr = spi->controller; in spi_async_locked()
3757 ret = __spi_validate(spi, message); in spi_async_locked()
3761 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3763 ret = __spi_async(spi, message); in spi_async_locked()
3765 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_async_locked()
3772 /*-------------------------------------------------------------------------*/
3774 /* Utility methods for SPI protocol drivers, layered on
3784 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
3788 struct spi_controller *ctlr = spi->controller; in __spi_sync()
3791 status = __spi_validate(spi, message); in __spi_sync()
3795 message->complete = spi_complete; in __spi_sync()
3796 message->context = &done; in __spi_sync()
3797 message->spi = spi; in __spi_sync()
3799 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); in __spi_sync()
3800 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); in __spi_sync()
3807 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3808 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3812 status = __spi_queued_transfer(spi, message, false); in __spi_sync()
3814 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in __spi_sync()
3816 status = spi_async_locked(spi, message); in __spi_sync()
3823 if (ctlr->transfer == spi_queued_transfer) { in __spi_sync()
3824 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, in __spi_sync()
3826 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, in __spi_sync()
3832 status = message->status; in __spi_sync()
3834 message->context = NULL; in __spi_sync()
3839 * spi_sync - blocking/synchronous SPI data transfers
3840 * @spi: device with which data will be exchanged
3845 * is non-interruptible, and has no timeout. Low-overhead controller
3848 * Note that the SPI device's chip select is active during the message,
3850 * frequently-used devices may want to minimize costs of selecting a chip,
3859 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
3863 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
3864 ret = __spi_sync(spi, message); in spi_sync()
3865 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
3872 * spi_sync_locked - version of spi_sync with exclusive bus usage
3873 * @spi: device with which data will be exchanged
3878 * is non-interruptible, and has no timeout. Low-overhead controller
3882 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3887 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
3889 return __spi_sync(spi, message); in spi_sync_locked()
3894 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3895 * @ctlr: SPI bus master that should be locked for exclusive bus access
3899 * is non-interruptible, and has no timeout.
3902 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3904 * and spi_async_locked calls when the SPI bus lock is held.
3912 mutex_lock(&ctlr->bus_lock_mutex); in spi_bus_lock()
3914 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3915 ctlr->bus_lock_flag = 1; in spi_bus_lock()
3916 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); in spi_bus_lock()
3925 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3926 * @ctlr: SPI bus master that was locked for exclusive bus access
3930 * is non-interruptible, and has no timeout.
3932 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3939 ctlr->bus_lock_flag = 0; in spi_bus_unlock()
3941 mutex_unlock(&ctlr->bus_lock_mutex); in spi_bus_unlock()
3953 * spi_write_then_read - SPI synchronous write followed by read
3954 * @spi: device with which data will be exchanged
3955 * @txbuf: data to be written (need not be dma-safe)
3957 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3962 * device, sending txbuf and then reading rxbuf. The return value
3967 * Performance-sensitive or bulk transfer code should instead use
3968 * spi_{async,sync}() calls with dma-safe buffers.
3972 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
3983 /* Use preallocated DMA-safe buffer if we can. We can't avoid in spi_write_then_read()
3986 * using the pre-allocated buffer or the transfer is too large. in spi_write_then_read()
3992 return -ENOMEM; in spi_write_then_read()
4013 status = spi_sync(spi, &message); in spi_write_then_read()
4026 /*-------------------------------------------------------------------------*/
4029 /* must call put_device() when done with returned spi_device device */
4032 struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); in of_find_spi_device_by_node()
4040 /* the spi controllers are not using spi_bus, so we find it with another way */
4043 struct device *dev; in of_find_spi_controller_by_node()
4060 struct spi_device *spi; in of_spi_notify() local
4064 ctlr = of_find_spi_controller_by_node(rd->dn->parent); in of_spi_notify()
4068 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { in of_spi_notify()
4069 put_device(&ctlr->dev); in of_spi_notify()
4073 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4074 put_device(&ctlr->dev); in of_spi_notify()
4076 if (IS_ERR(spi)) { in of_spi_notify()
4078 __func__, rd->dn); in of_spi_notify()
4079 of_node_clear_flag(rd->dn, OF_POPULATED); in of_spi_notify()
4080 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4086 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_spi_notify()
4089 /* find our device by node */ in of_spi_notify()
4090 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4091 if (spi == NULL) in of_spi_notify()
4095 spi_unregister_device(spi); in of_spi_notify()
4098 put_device(&spi->dev); in of_spi_notify()
4113 static int spi_acpi_controller_match(struct device *dev, const void *data) in spi_acpi_controller_match()
4115 return ACPI_COMPANION(dev->parent) == data; in spi_acpi_controller_match()
4120 struct device *dev; in acpi_spi_find_controller_by_adev()
4135 struct device *dev; in acpi_spi_find_device_by_adev()
4146 struct spi_device *spi; in acpi_spi_notify() local
4150 ctlr = acpi_spi_find_controller_by_adev(adev->parent); in acpi_spi_notify()
4155 put_device(&ctlr->dev); in acpi_spi_notify()
4161 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4162 if (!spi) in acpi_spi_notify()
4165 spi_unregister_device(spi); in acpi_spi_notify()
4166 put_device(&spi->dev); in acpi_spi_notify()
4186 status = -ENOMEM; in spi_init()
4225 * REVISIT only boardinfo really needs static linking. the rest (device and