Lines Matching full:spi

2 // SPI init/core code
34 #include <linux/spi/offload/types.h>
35 #include <linux/spi/spi.h>
36 #include <linux/spi/spi-mem.h>
40 #include <trace/events/spi.h>
50 struct spi_device *spi = to_spi_device(dev); in spidev_release() local
52 spi_controller_put(spi->controller); in spidev_release()
53 kfree(spi->driver_override); in spidev_release()
54 free_percpu(spi->pcpu_statistics); in spidev_release()
55 kfree(spi); in spidev_release()
61 const struct spi_device *spi = to_spi_device(dev); in modalias_show() local
68 return sysfs_emit(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); in modalias_show()
76 struct spi_device *spi = to_spi_device(dev); in driver_override_store() local
79 ret = driver_set_override(dev, &spi->driver_override, buf, count); in driver_override_store()
89 const struct spi_device *spi = to_spi_device(dev); in driver_override_show() local
93 len = sysfs_emit(buf, "%s\n", spi->driver_override ? : ""); in driver_override_show()
161 struct spi_device *spi = to_spi_device(dev); \
162 return spi_statistics_##field##_show(spi->pcpu_statistics, buf); \
377 const struct spi_device *spi = to_spi_device(dev); in spi_match_device() local
381 if (spi->driver_override) in spi_match_device()
382 return strcmp(spi->driver_override, drv->name) == 0; in spi_match_device()
393 return !!spi_match_id(sdrv->id_table, spi->modalias); in spi_match_device()
395 return strcmp(spi->modalias, drv->name) == 0; in spi_match_device()
400 const struct spi_device *spi = to_spi_device(dev); in spi_uevent() local
407 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); in spi_uevent()
413 struct spi_device *spi = to_spi_device(dev); in spi_probe() local
422 spi->irq = of_irq_get(dev->of_node, 0); in spi_probe()
423 else if (is_acpi_device_node(fwnode) && spi->irq < 0) in spi_probe()
424 spi->irq = acpi_dev_gpio_irq_get(to_acpi_device_node(fwnode), 0); in spi_probe()
425 if (spi->irq == -EPROBE_DEFER) in spi_probe()
426 return dev_err_probe(dev, spi->irq, "Failed to get irq\n"); in spi_probe()
427 if (spi->irq < 0) in spi_probe()
428 spi->irq = 0; in spi_probe()
435 ret = sdrv->probe(spi); in spi_probe()
464 .name = "spi",
475 * __spi_register_driver - register a SPI driver
488 * For Really Good Reasons we use spi: modaliases not of: in __spi_register_driver()
518 pr_warn("SPI driver %s has no spi_device_id for %s\n", in __spi_register_driver()
530 * SPI devices should normally not be created by SPI device drivers; that
531 * would make them board-specific. Similarly with SPI controller drivers.
552 * spi_alloc_device - Allocate a new SPI device
562 * spi_device structure to add it to the SPI controller. If the caller
570 struct spi_device *spi; in spi_alloc_device() local
575 spi = kzalloc(sizeof(*spi), GFP_KERNEL); in spi_alloc_device()
576 if (!spi) { in spi_alloc_device()
581 spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL); in spi_alloc_device()
582 if (!spi->pcpu_statistics) { in spi_alloc_device()
583 kfree(spi); in spi_alloc_device()
588 spi->controller = ctlr; in spi_alloc_device()
589 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
590 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
591 spi->dev.release = spidev_release; in spi_alloc_device()
592 spi->mode = ctlr->buswidth_override_bits; in spi_alloc_device()
594 device_initialize(&spi->dev); in spi_alloc_device()
595 return spi; in spi_alloc_device()
599 static void spi_dev_set_name(struct spi_device *spi) in spi_dev_set_name() argument
601 struct device *dev = &spi->dev; in spi_dev_set_name()
605 dev_set_name(dev, "spi-%s", acpi_dev_name(to_acpi_device_node(fwnode))); in spi_dev_set_name()
610 dev_set_name(dev, "spi-%pfwP", fwnode); in spi_dev_set_name()
614 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
615 spi_get_chipselect(spi, 0)); in spi_dev_set_name()
620 * logical CS in the spi->chip_select[]. If all the physical CS
635 struct spi_device *spi, u8 idx, in spi_dev_check_cs() argument
641 cs = spi_get_chipselect(spi, idx); in spi_dev_check_cs()
654 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
658 if (spi->controller == new_spi->controller) { in spi_dev_check()
660 status = spi_dev_check_cs(dev, spi, idx, new_spi, 0); in spi_dev_check()
668 static void spi_cleanup(struct spi_device *spi) in spi_cleanup() argument
670 if (spi->controller->cleanup) in spi_cleanup()
671 spi->controller->cleanup(spi); in spi_cleanup()
674 static int __spi_add_device(struct spi_device *spi) in __spi_add_device() argument
676 struct spi_controller *ctlr = spi->controller; in __spi_add_device()
683 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
685 dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx), in __spi_add_device()
693 * For example, spi->chip_select[0] != spi->chip_select[1] and so on. in __spi_add_device()
697 status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1); in __spi_add_device()
704 spi_dev_set_name(spi); in __spi_add_device()
711 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in __spi_add_device()
725 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
727 spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]); in __spi_add_device()
736 status = spi_setup(spi); in __spi_add_device()
739 dev_name(&spi->dev), status); in __spi_add_device()
744 status = device_add(&spi->dev); in __spi_add_device()
747 dev_name(&spi->dev), status); in __spi_add_device()
748 spi_cleanup(spi); in __spi_add_device()
750 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in __spi_add_device()
758 * @spi: spi_device to register
761 * spi_alloc_device can be added onto the SPI bus with this function.
765 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
767 struct spi_controller *ctlr = spi->controller; in spi_add_device()
771 spi_dev_set_name(spi); in spi_add_device()
774 status = __spi_add_device(spi); in spi_add_device()
780 static void spi_set_all_cs_unused(struct spi_device *spi) in spi_set_all_cs_unused() argument
785 spi_set_chipselect(spi, idx, SPI_INVALID_CS); in spi_set_all_cs_unused()
789 * spi_new_device - instantiate one new SPI device
791 * @chip: Describes the SPI device
834 * By default spi->chip_select[0] will hold the physical CS number, in spi_new_device()
835 * so set bit 0 in spi->cs_index_mask. in spi_new_device()
862 * spi_unregister_device - unregister a single SPI device
863 * @spi: spi_device to unregister
865 * Start making the passed SPI device vanish. Normally this would be handled
868 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
872 if (!spi) in spi_unregister_device()
875 fwnode = dev_fwnode(&spi->dev); in spi_unregister_device()
882 device_remove_software_node(&spi->dev); in spi_unregister_device()
883 device_del(&spi->dev); in spi_unregister_device()
884 spi_cleanup(spi); in spi_unregister_device()
885 put_device(&spi->dev); in spi_unregister_device()
904 * spi_register_board_info - register SPI devices for a given board
910 * with segments of the SPI device table. Any device nodes are created later,
911 * after the relevant parent SPI controller (bus_num) is defined. We keep
916 * SPI devices through its expansion connector, so code initializing that board
917 * would naturally declare its SPI devices.
954 /* Core methods for SPI resource management */
957 * spi_res_alloc - allocate a spi resource that is life-cycle managed
960 * @spi: the SPI device for which we allocate memory
970 static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release, in spi_res_alloc() argument
986 * spi_res_free - free an SPI resource
999 * @message: the SPI message
1011 * spi_res_release - release all SPI resources for this message
1030 #define spi_for_each_valid_cs(spi, idx) \ argument
1032 if (!(spi->cs_index_mask & BIT(idx))) {} else
1034 static inline bool spi_is_last_cs(struct spi_device *spi) in spi_is_last_cs() argument
1039 spi_for_each_valid_cs(spi, idx) { in spi_is_last_cs()
1040 if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx)) in spi_is_last_cs()
1046 static void spi_toggle_csgpiod(struct spi_device *spi, u8 idx, bool enable, bool activate) in spi_toggle_csgpiod() argument
1058 if (is_acpi_device_node(dev_fwnode(&spi->dev))) in spi_toggle_csgpiod()
1059 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), !enable); in spi_toggle_csgpiod()
1062 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), activate); in spi_toggle_csgpiod()
1065 spi_delay_exec(&spi->cs_setup, NULL); in spi_toggle_csgpiod()
1067 spi_delay_exec(&spi->cs_inactive, NULL); in spi_toggle_csgpiod()
1070 static void spi_set_cs(struct spi_device *spi, bool enable, bool force) in spi_set_cs() argument
1079 if (!force && (enable == spi_is_last_cs(spi)) && in spi_set_cs()
1080 (spi->controller->last_cs_index_mask == spi->cs_index_mask) && in spi_set_cs()
1081 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
1084 trace_spi_set_cs(spi, activate); in spi_set_cs()
1086 spi->controller->last_cs_index_mask = spi->cs_index_mask; in spi_set_cs()
1088 spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : SPI_INVALID_CS; in spi_set_cs()
1090 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
1091 if (spi->controller->last_cs_mode_high) in spi_set_cs()
1098 if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate) in spi_set_cs()
1099 spi_delay_exec(&spi->cs_hold, NULL); in spi_set_cs()
1101 if (spi_is_csgpiod(spi)) { in spi_set_cs()
1102 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
1103 spi_for_each_valid_cs(spi, idx) { in spi_set_cs()
1104 if (spi_get_csgpiod(spi, idx)) in spi_set_cs()
1105 spi_toggle_csgpiod(spi, idx, enable, activate); in spi_set_cs()
1108 /* Some SPI controllers need both GPIO CS & ->set_cs() */ in spi_set_cs()
1109 if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) && in spi_set_cs()
1110 spi->controller->set_cs) in spi_set_cs()
1111 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1112 } else if (spi->controller->set_cs) { in spi_set_cs()
1113 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1116 if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) { in spi_set_cs()
1118 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
1120 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
1254 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
1390 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1440 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_wait()
1446 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1454 * For each byte we wait for 8 cycles of the SPI clock. in spi_transfer_wait()
1476 dev_err(&msg->spi->dev, in spi_transfer_wait()
1477 "SPI transfer timed out\n"); in spi_transfer_wait()
1576 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1604 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_one_message()
1607 spi_set_cs(msg->spi, !xfer->cs_off, false); in spi_transfer_one_message()
1628 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1644 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1645 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1658 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1681 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1684 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1688 spi_set_cs(msg->spi, xfer->cs_off, false); in spi_transfer_one_message()
1696 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1713 * Called by SPI drivers using the core transfer_one_message()
1832 * __spi_pump_messages - function which processes SPI message queue
1836 * This function checks if there is any SPI message in the queue that
1930 * spi_pump_messages - kthread work function which processes spi message queue
1954 * for the requested byte from the SPI transfer. The frequency with which this
1994 * the requested byte from the SPI transfer. Can be called with an arbitrary
2112 struct spi_controller *ctlr = msg->spi->controller; in __spi_unoptimize_message()
2133 !msg->spi->controller->defer_optimize_message) in spi_maybe_unoptimize_message()
2219 * friends on every SPI message. Do this instead. in spi_stop_queue()
2257 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
2261 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
2284 * @spi: SPI device which is requesting transfer
2285 * @msg: SPI message which is to handled is queued to driver queue
2289 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
2291 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
2329 * sent before doing something. Is used by the spi-mem code to make sure SPI
2330 * memory operations do not preempt regular SPI transfers that have been queued
2331 * before the spi-mem operation.
2358 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
2365 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
2366 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
2367 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
2368 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
2369 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
2370 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
2371 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
2372 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
2373 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
2374 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
2377 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
2380 spi->mode |= SPI_NO_TX; in of_spi_parse_dt()
2385 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
2388 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
2391 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
2395 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
2401 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
2404 spi->mode |= SPI_NO_RX; in of_spi_parse_dt()
2409 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
2412 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
2415 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
2419 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
2439 spi_set_all_cs_unused(spi); in of_spi_parse_dt()
2456 dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n"); in of_spi_parse_dt()
2460 spi_set_chipselect(spi, idx, cs[idx]); in of_spi_parse_dt()
2463 * By default spi->chip_select[0] will hold the physical CS number, in of_spi_parse_dt()
2464 * so set bit 0 in spi->cs_index_mask. in of_spi_parse_dt()
2466 spi->cs_index_mask = BIT(0); in of_spi_parse_dt()
2469 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2470 spi->max_speed_hz = value; in of_spi_parse_dt()
2473 of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns"); in of_spi_parse_dt()
2474 of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns"); in of_spi_parse_dt()
2475 of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns"); in of_spi_parse_dt()
2483 struct spi_device *spi; in of_register_spi_device() local
2487 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2488 if (!spi) { in of_register_spi_device()
2495 rc = of_alias_from_compatible(nc, spi->modalias, in of_register_spi_device()
2496 sizeof(spi->modalias)); in of_register_spi_device()
2502 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2509 device_set_node(&spi->dev, of_fwnode_handle(nc)); in of_register_spi_device()
2512 rc = spi_add_device(spi); in of_register_spi_device()
2518 return spi; in of_register_spi_device()
2523 spi_dev_put(spi); in of_register_spi_device()
2528 * of_register_spi_devices() - Register child devices onto the SPI bus
2532 * represents a valid SPI target device.
2536 struct spi_device *spi; in of_register_spi_devices() local
2542 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2543 if (IS_ERR(spi)) { in of_register_spi_devices()
2545 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2555 * spi_new_ancillary_device() - Register ancillary SPI device
2556 * @spi: Pointer to the main SPI device registering the ancillary device
2559 * Register an ancillary SPI device; for example some chips have a chip-select
2562 * This may only be called from main SPI device's probe routine.
2566 struct spi_device *spi_new_ancillary_device(struct spi_device *spi, in spi_new_ancillary_device() argument
2569 struct spi_controller *ctlr = spi->controller; in spi_new_ancillary_device()
2586 /* Take over SPI mode/speed from SPI main device */ in spi_new_ancillary_device()
2587 ancillary->max_speed_hz = spi->max_speed_hz; in spi_new_ancillary_device()
2588 ancillary->mode = spi->mode; in spi_new_ancillary_device()
2590 * By default spi->chip_select[0] will hold the physical CS number, in spi_new_ancillary_device()
2591 * so set bit 0 in spi->cs_index_mask. in spi_new_ancillary_device()
2600 dev_err(&spi->dev, "failed to register ancillary device\n"); in spi_new_ancillary_device()
2772 * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2773 * @ctlr: controller to which the spi device belongs
2774 * @adev: ACPI Device for the spi device
2775 * @index: Index of the spi resource inside the ACPI Node
2777 * This should be used to allocate a new SPI device from and ACPI Device node.
2778 * The caller is responsible for calling spi_add_device to register the SPI device.
2780 * If ctlr is set to NULL, the Controller for the SPI device will be looked up
2794 struct spi_device *spi; in acpi_spi_device_alloc() local
2811 /* Found SPI in _CRS but it points to another controller */ in acpi_spi_device_alloc()
2817 /* Apple does not use _CRS but nested devices for SPI target devices */ in acpi_spi_device_alloc()
2824 spi = spi_alloc_device(lookup.ctlr); in acpi_spi_device_alloc()
2825 if (!spi) { in acpi_spi_device_alloc()
2826 dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_spi_device_alloc()
2831 spi_set_all_cs_unused(spi); in acpi_spi_device_alloc()
2832 spi_set_chipselect(spi, 0, lookup.chip_select); in acpi_spi_device_alloc()
2834 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_spi_device_alloc()
2835 spi->max_speed_hz = lookup.max_speed_hz; in acpi_spi_device_alloc()
2836 spi->mode |= lookup.mode; in acpi_spi_device_alloc()
2837 spi->irq = lookup.irq; in acpi_spi_device_alloc()
2838 spi->bits_per_word = lookup.bits_per_word; in acpi_spi_device_alloc()
2840 * By default spi->chip_select[0] will hold the physical CS number, in acpi_spi_device_alloc()
2841 * so set bit 0 in spi->cs_index_mask. in acpi_spi_device_alloc()
2843 spi->cs_index_mask = BIT(0); in acpi_spi_device_alloc()
2845 return spi; in acpi_spi_device_alloc()
2852 struct spi_device *spi; in acpi_register_spi_device() local
2858 spi = acpi_spi_device_alloc(ctlr, adev, -1); in acpi_register_spi_device()
2859 if (IS_ERR(spi)) { in acpi_register_spi_device()
2860 if (PTR_ERR(spi) == -ENOMEM) in acpi_register_spi_device()
2866 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2867 sizeof(spi->modalias)); in acpi_register_spi_device()
2872 if (spi_add_device(spi)) { in acpi_register_spi_device()
2874 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2876 spi_dev_put(spi); in acpi_register_spi_device()
2909 dev_warn(&ctlr->dev, "failed to enumerate SPI target devices\n"); in acpi_register_spi_devices()
2931 * spi_target_abort - abort the ongoing transfer request on an SPI target controller
2932 * @spi: device used for the current transfer
2934 int spi_target_abort(struct spi_device *spi) in spi_target_abort() argument
2936 struct spi_controller *ctlr = spi->controller; in spi_target_abort()
2965 struct spi_device *spi; in slave_store() local
2983 spi = spi_alloc_device(ctlr); in slave_store()
2984 if (!spi) in slave_store()
2987 strscpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2989 rc = spi_add_device(spi); in slave_store()
2991 spi_dev_put(spi); in slave_store()
3026 * __spi_alloc_controller - allocate an SPI host or target controller
3033 * @target: flag indicating whether to allocate an SPI host (false) or SPI target (true)
3037 * This call is used only by SPI controller drivers, which are the
3048 * Return: the SPI controller structure on success, else NULL.
3092 * @dev: physical device of SPI controller
3094 * @target: whether to allocate an SPI host (false) or SPI target (true) controller
3097 * Allocate an SPI controller and automatically release a reference on it
3103 * Return: the SPI controller structure on success, else NULL.
3131 * @ctlr: The SPI controller to grab GPIO descriptors for
3207 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
3208 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
3238 * spi_register_controller - register SPI host or target controller
3243 * SPI controllers connect to their drivers using some non-SPI bus,
3245 * includes calling spi_register_controller() to hook up to this SPI bus glue.
3247 * SPI controllers use board specific (often SOC specific) bus numbers,
3248 * and board-specific addressing for SPI devices combines those numbers
3249 * with chip select numbers. Since SPI does not directly support dynamic
3273 * the SPI controller. in spi_register_controller()
3280 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
3288 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
3308 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
3390 * devm_spi_register_controller - register managed SPI host or target controller
3391 * @dev: device managing SPI controller
3396 * Register a SPI device as with spi_register_controller() which will
3430 * spi_unregister_controller - unregister SPI host or target controller
3434 * This call is used only by SPI controller drivers, which are the
3586 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3618 dev_err(&msg->spi->dev, in spi_replace_transfers()
3723 SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, in __spi_split_transfer_maxsize()
3730 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3738 * spi message unoptimize phase so this function should only be called from
3772 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3774 * certain number of SPI words
3780 * spi message unoptimize phase so this function should only be called from
3818 * Core methods for SPI controller protocol drivers. Some of the
3838 * @spi: the device that requires specific CS timing configuration
3842 static int spi_set_cs_timing(struct spi_device *spi) in spi_set_cs_timing() argument
3844 struct device *parent = spi->controller->dev.parent; in spi_set_cs_timing()
3847 if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) { in spi_set_cs_timing()
3848 if (spi->controller->auto_runtime_pm) { in spi_set_cs_timing()
3852 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_set_cs_timing()
3857 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3860 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3867 * spi_setup - setup SPI mode and clock rate
3868 * @spi: the device whose settings are being modified
3871 * SPI protocol drivers may need to update the transfer mode if the
3877 * or from it. When this function returns, the SPI device is deselected.
3886 int spi_setup(struct spi_device *spi) in spi_setup() argument
3895 if ((hweight_long(spi->mode & in spi_setup()
3897 (hweight_long(spi->mode & in spi_setup()
3899 dev_err(&spi->dev, in spi_setup()
3904 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3909 if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) { in spi_setup()
3910 dev_err(&spi->dev, in spi_setup()
3920 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | in spi_setup()
3926 dev_warn(&spi->dev, in spi_setup()
3929 spi->mode &= ~ugly_bits; in spi_setup()
3933 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3938 if (!spi->bits_per_word) { in spi_setup()
3939 spi->bits_per_word = 8; in spi_setup()
3945 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3946 spi->bits_per_word); in spi_setup()
3951 if (spi->controller->max_speed_hz && in spi_setup()
3952 (!spi->max_speed_hz || in spi_setup()
3953 spi->max_speed_hz > spi->controller->max_speed_hz)) in spi_setup()
3954 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3956 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3958 if (spi->controller->setup) { in spi_setup()
3959 status = spi->controller->setup(spi); in spi_setup()
3961 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3962 dev_err(&spi->controller->dev, "Failed to setup device: %d\n", in spi_setup()
3968 status = spi_set_cs_timing(spi); in spi_setup()
3970 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3974 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3975 status = pm_runtime_resume_and_get(spi->controller->dev.parent); in spi_setup()
3977 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3978 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3991 spi_set_cs(spi, false, true); in spi_setup()
3992 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
3994 spi_set_cs(spi, false, true); in spi_setup()
3997 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3999 if (spi->rt && !spi->controller->rt) { in spi_setup()
4000 spi->controller->rt = true; in spi_setup()
4001 spi_set_thread_rt(spi->controller); in spi_setup()
4004 trace_spi_setup(spi, status); in spi_setup()
4006 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
4007 spi->mode & SPI_MODE_X_MASK, in spi_setup()
4008 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
4009 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
4010 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
4011 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
4012 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
4020 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
4028 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
4033 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
4039 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
4041 struct spi_controller *ctlr = spi->controller; in __spi_validate()
4048 message->spi = spi; in __spi_validate()
4057 (spi->mode & SPI_3WIRE)) { in __spi_validate()
4071 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
4083 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
4086 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
4095 * default considered as SDR mode for SPI and QSPI controller. in __spi_validate()
4102 * SPI transfer length should be multiple of SPI word size in __spi_validate()
4103 * where SPI word size should be power-of-two multiple. in __spi_validate()
4130 if (spi->mode & SPI_NO_TX) in __spi_validate()
4138 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL))) in __spi_validate()
4141 !(spi->mode & (SPI_TX_QUAD | SPI_TX_OCTAL))) in __spi_validate()
4144 !(spi->mode & SPI_TX_OCTAL)) in __spi_validate()
4149 if (spi->mode & SPI_NO_RX) in __spi_validate()
4157 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) in __spi_validate()
4160 !(spi->mode & (SPI_RX_QUAD | SPI_RX_OCTAL))) in __spi_validate()
4163 !(spi->mode & SPI_RX_OCTAL)) in __spi_validate()
4167 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
4189 * Under certain conditions, a SPI controller may not support arbitrary
4202 struct spi_controller *ctlr = msg->spi->controller; in spi_split_transfers()
4207 * If an SPI controller does not support toggling the CS line on each in spi_split_transfers()
4213 if ((msg->spi->mode & SPI_CS_WORD) && in spi_split_transfers()
4214 (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) { in spi_split_transfers()
4228 spi_max_transfer_size(msg->spi)); in spi_split_transfers()
4239 * @spi: the device that will be used for the message
4242 * Peripheral drivers will call spi_optimize_message() and the spi core will
4249 static int __spi_optimize_message(struct spi_device *spi, in __spi_optimize_message() argument
4252 struct spi_controller *ctlr = spi->controller; in __spi_optimize_message()
4255 ret = __spi_validate(spi, msg); in __spi_optimize_message()
4278 * @spi: the device that will be used for the message
4282 static int spi_maybe_optimize_message(struct spi_device *spi, in spi_maybe_optimize_message() argument
4285 if (spi->controller->defer_optimize_message) { in spi_maybe_optimize_message()
4286 msg->spi = spi; in spi_maybe_optimize_message()
4293 return __spi_optimize_message(spi, msg); in spi_maybe_optimize_message()
4297 * spi_optimize_message - do any one-time validation and setup for a SPI message
4298 * @spi: the device that will be used for the message
4316 int spi_optimize_message(struct spi_device *spi, struct spi_message *msg) in spi_optimize_message() argument
4322 * when using spi-mux. in spi_optimize_message()
4324 if (spi->controller->defer_optimize_message) in spi_optimize_message()
4327 ret = __spi_optimize_message(spi, msg); in spi_optimize_message()
4353 if (msg->spi->controller->defer_optimize_message) in spi_unoptimize_message()
4361 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
4363 struct spi_controller *ctlr = spi->controller; in __spi_async()
4367 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
4374 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); in __spi_async()
4385 return ctlr->transfer(spi, message); in __spi_async()
4395 * @dev: the device that manages @msg (usually @spi->dev)
4396 * @spi: the device that will be used for the message
4403 int devm_spi_optimize_message(struct device *dev, struct spi_device *spi, in devm_spi_optimize_message() argument
4408 ret = spi_optimize_message(spi, msg); in devm_spi_optimize_message()
4417 * spi_async - asynchronous SPI transfer
4418 * @spi: device with which data will be exchanged
4430 * deallocate the associated memory; it's no longer in use by any SPI
4447 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
4449 struct spi_controller *ctlr = spi->controller; in spi_async()
4453 ret = spi_maybe_optimize_message(spi, message); in spi_async()
4462 ret = __spi_async(spi, message); in spi_async()
4504 * Utility methods for SPI protocol drivers, layered on
4514 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
4519 struct spi_controller *ctlr = spi->controller; in __spi_sync()
4522 dev_warn_once(&spi->dev, "Attempted to sync while suspend\n"); in __spi_sync()
4526 status = spi_maybe_optimize_message(spi, message); in __spi_sync()
4531 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); in __spi_sync()
4546 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4563 status = __spi_async(spi, message); in __spi_sync()
4577 * spi_sync - blocking/synchronous SPI data transfers
4578 * @spi: device with which data will be exchanged
4586 * Note that the SPI device's chip select is active during the message,
4597 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
4601 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
4602 ret = __spi_sync(spi, message); in spi_sync()
4603 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
4611 * @spi: device with which data will be exchanged
4620 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4625 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
4627 return __spi_sync(spi, message); in spi_sync_locked()
4632 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4633 * @ctlr: SPI bus controller that should be locked for exclusive bus access
4640 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4642 * and spi_async_locked calls when the SPI bus lock is held.
4663 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4664 * @ctlr: SPI bus controller that was locked for exclusive bus access
4670 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4691 * spi_write_then_read - SPI synchronous write followed by read
4692 * @spi: device with which data will be exchanged
4710 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4752 status = spi_sync(spi, &message); in spi_write_then_read()
4776 /* The spi controllers are not using spi_bus, so we find it with another way */
4796 struct spi_device *spi; in of_spi_notify() local
4814 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4817 if (IS_ERR(spi)) { in of_spi_notify()
4821 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4831 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4832 if (spi == NULL) in of_spi_notify()
4836 spi_unregister_device(spi); in of_spi_notify()
4839 put_device(&spi->dev); in of_spi_notify()
4888 struct spi_device *spi; in acpi_spi_notify() local
4903 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4904 if (!spi) in acpi_spi_notify()
4907 spi_unregister_device(spi); in acpi_spi_notify()
4908 put_device(&spi->dev); in acpi_spi_notify()