Lines Matching full:spi

2 // SPI init/core code
34 #include <linux/spi/spi.h>
35 #include <linux/spi/spi-mem.h>
39 #include <trace/events/spi.h>
49 struct spi_device *spi = to_spi_device(dev); in spidev_release() local
51 spi_controller_put(spi->controller); in spidev_release()
52 kfree(spi->driver_override); in spidev_release()
53 free_percpu(spi->pcpu_statistics); in spidev_release()
54 kfree(spi); in spidev_release()
60 const struct spi_device *spi = to_spi_device(dev); in modalias_show() local
67 return sysfs_emit(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); in modalias_show()
75 struct spi_device *spi = to_spi_device(dev); in driver_override_store() local
78 ret = driver_set_override(dev, &spi->driver_override, buf, count); in driver_override_store()
88 const struct spi_device *spi = to_spi_device(dev); in driver_override_show() local
92 len = sysfs_emit(buf, "%s\n", spi->driver_override ? : ""); in driver_override_show()
160 struct spi_device *spi = to_spi_device(dev); \
161 return spi_statistics_##field##_show(spi->pcpu_statistics, buf); \
376 const struct spi_device *spi = to_spi_device(dev); in spi_match_device() local
380 if (spi->driver_override) in spi_match_device()
381 return strcmp(spi->driver_override, drv->name) == 0; in spi_match_device()
392 return !!spi_match_id(sdrv->id_table, spi->modalias); in spi_match_device()
394 return strcmp(spi->modalias, drv->name) == 0; in spi_match_device()
399 const struct spi_device *spi = to_spi_device(dev); in spi_uevent() local
406 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); in spi_uevent()
412 struct spi_device *spi = to_spi_device(dev); in spi_probe() local
420 spi->irq = of_irq_get(dev->of_node, 0); in spi_probe()
421 if (spi->irq == -EPROBE_DEFER) in spi_probe()
423 if (spi->irq < 0) in spi_probe()
424 spi->irq = 0; in spi_probe()
432 ret = sdrv->probe(spi); in spi_probe()
461 .name = "spi",
472 * __spi_register_driver - register a SPI driver
485 * For Really Good Reasons we use spi: modaliases not of: in __spi_register_driver()
515 pr_warn("SPI driver %s has no spi_device_id for %s\n", in __spi_register_driver()
527 * SPI devices should normally not be created by SPI device drivers; that
528 * would make them board-specific. Similarly with SPI controller drivers.
549 * spi_alloc_device - Allocate a new SPI device
559 * spi_device structure to add it to the SPI controller. If the caller
567 struct spi_device *spi; in spi_alloc_device() local
572 spi = kzalloc(sizeof(*spi), GFP_KERNEL); in spi_alloc_device()
573 if (!spi) { in spi_alloc_device()
578 spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL); in spi_alloc_device()
579 if (!spi->pcpu_statistics) { in spi_alloc_device()
580 kfree(spi); in spi_alloc_device()
585 spi->controller = ctlr; in spi_alloc_device()
586 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
587 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
588 spi->dev.release = spidev_release; in spi_alloc_device()
589 spi->mode = ctlr->buswidth_override_bits; in spi_alloc_device()
591 device_initialize(&spi->dev); in spi_alloc_device()
592 return spi; in spi_alloc_device()
596 static void spi_dev_set_name(struct spi_device *spi) in spi_dev_set_name() argument
598 struct device *dev = &spi->dev; in spi_dev_set_name()
602 dev_set_name(dev, "spi-%s", acpi_dev_name(to_acpi_device_node(fwnode))); in spi_dev_set_name()
607 dev_set_name(dev, "spi-%pfwP", fwnode); in spi_dev_set_name()
611 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
612 spi_get_chipselect(spi, 0)); in spi_dev_set_name()
617 * logical CS in the spi->chip_select[]. If all the physical CS
632 struct spi_device *spi, u8 idx, in spi_dev_check_cs() argument
638 cs = spi_get_chipselect(spi, idx); in spi_dev_check_cs()
651 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
655 if (spi->controller == new_spi->controller) { in spi_dev_check()
657 status = spi_dev_check_cs(dev, spi, idx, new_spi, 0); in spi_dev_check()
665 static void spi_cleanup(struct spi_device *spi) in spi_cleanup() argument
667 if (spi->controller->cleanup) in spi_cleanup()
668 spi->controller->cleanup(spi); in spi_cleanup()
671 static int __spi_add_device(struct spi_device *spi) in __spi_add_device() argument
673 struct spi_controller *ctlr = spi->controller; in __spi_add_device()
680 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
682 dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx), in __spi_add_device()
690 * For example, spi->chip_select[0] != spi->chip_select[1] and so on. in __spi_add_device()
694 status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1); in __spi_add_device()
701 spi_dev_set_name(spi); in __spi_add_device()
708 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in __spi_add_device()
722 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
724 spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]); in __spi_add_device()
733 status = spi_setup(spi); in __spi_add_device()
736 dev_name(&spi->dev), status); in __spi_add_device()
741 status = device_add(&spi->dev); in __spi_add_device()
744 dev_name(&spi->dev), status); in __spi_add_device()
745 spi_cleanup(spi); in __spi_add_device()
747 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in __spi_add_device()
755 * @spi: spi_device to register
758 * spi_alloc_device can be added onto the SPI bus with this function.
762 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
764 struct spi_controller *ctlr = spi->controller; in spi_add_device()
768 spi_dev_set_name(spi); in spi_add_device()
771 status = __spi_add_device(spi); in spi_add_device()
777 static void spi_set_all_cs_unused(struct spi_device *spi) in spi_set_all_cs_unused() argument
782 spi_set_chipselect(spi, idx, SPI_INVALID_CS); in spi_set_all_cs_unused()
786 * spi_new_device - instantiate one new SPI device
788 * @chip: Describes the SPI device
831 * By default spi->chip_select[0] will hold the physical CS number, in spi_new_device()
832 * so set bit 0 in spi->cs_index_mask. in spi_new_device()
859 * spi_unregister_device - unregister a single SPI device
860 * @spi: spi_device to unregister
862 * Start making the passed SPI device vanish. Normally this would be handled
865 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
867 if (!spi) in spi_unregister_device()
870 if (spi->dev.of_node) { in spi_unregister_device()
871 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
872 of_node_put(spi->dev.of_node); in spi_unregister_device()
874 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
875 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
876 device_remove_software_node(&spi->dev); in spi_unregister_device()
877 device_del(&spi->dev); in spi_unregister_device()
878 spi_cleanup(spi); in spi_unregister_device()
879 put_device(&spi->dev); in spi_unregister_device()
898 * spi_register_board_info - register SPI devices for a given board
904 * with segments of the SPI device table. Any device nodes are created later,
905 * after the relevant parent SPI controller (bus_num) is defined. We keep
910 * SPI devices through its expansion connector, so code initializing that board
911 * would naturally declare its SPI devices.
948 /* Core methods for SPI resource management */
951 * spi_res_alloc - allocate a spi resource that is life-cycle managed
954 * @spi: the SPI device for which we allocate memory
964 static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release, in spi_res_alloc() argument
980 * spi_res_free - free an SPI resource
996 * @message: the SPI message
1008 * spi_res_release - release all SPI resources for this message
1027 #define spi_for_each_valid_cs(spi, idx) \ argument
1029 if (!(spi->cs_index_mask & BIT(idx))) {} else
1031 static inline bool spi_is_last_cs(struct spi_device *spi) in spi_is_last_cs() argument
1036 spi_for_each_valid_cs(spi, idx) { in spi_is_last_cs()
1037 if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx)) in spi_is_last_cs()
1043 static void spi_toggle_csgpiod(struct spi_device *spi, u8 idx, bool enable, bool activate) in spi_toggle_csgpiod() argument
1055 if (has_acpi_companion(&spi->dev)) in spi_toggle_csgpiod()
1056 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), !enable); in spi_toggle_csgpiod()
1059 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), activate); in spi_toggle_csgpiod()
1062 spi_delay_exec(&spi->cs_setup, NULL); in spi_toggle_csgpiod()
1064 spi_delay_exec(&spi->cs_inactive, NULL); in spi_toggle_csgpiod()
1067 static void spi_set_cs(struct spi_device *spi, bool enable, bool force) in spi_set_cs() argument
1076 if (!force && ((enable && spi->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1077 spi_is_last_cs(spi)) || in spi_set_cs()
1078 (!enable && spi->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1079 !spi_is_last_cs(spi))) && in spi_set_cs()
1080 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
1083 trace_spi_set_cs(spi, activate); in spi_set_cs()
1085 spi->controller->last_cs_index_mask = spi->cs_index_mask; in spi_set_cs()
1087 spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : SPI_INVALID_CS; in spi_set_cs()
1088 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
1090 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
1097 if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate) in spi_set_cs()
1098 spi_delay_exec(&spi->cs_hold, NULL); in spi_set_cs()
1100 if (spi_is_csgpiod(spi)) { in spi_set_cs()
1101 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
1102 spi_for_each_valid_cs(spi, idx) { in spi_set_cs()
1103 if (spi_get_csgpiod(spi, idx)) in spi_set_cs()
1104 spi_toggle_csgpiod(spi, idx, enable, activate); in spi_set_cs()
1107 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
1108 if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) && in spi_set_cs()
1109 spi->controller->set_cs) in spi_set_cs()
1110 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1111 } else if (spi->controller->set_cs) { in spi_set_cs()
1112 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1115 if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) { in spi_set_cs()
1117 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
1119 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
1253 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
1389 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1439 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_wait()
1445 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1453 * For each byte we wait for 8 cycles of the SPI clock. in spi_transfer_wait()
1475 dev_err(&msg->spi->dev, in spi_transfer_wait()
1476 "SPI transfer timed out\n"); in spi_transfer_wait()
1578 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1606 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_one_message()
1609 spi_set_cs(msg->spi, !xfer->cs_off, false); in spi_transfer_one_message()
1630 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1646 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1647 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1660 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1683 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1686 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1690 spi_set_cs(msg->spi, xfer->cs_off, false); in spi_transfer_one_message()
1698 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1715 * Called by SPI drivers using the core transfer_one_message()
1835 * __spi_pump_messages - function which processes SPI message queue
1839 * This function checks if there is any SPI message in the queue that
1933 * spi_pump_messages - kthread work function which processes spi message queue
1957 * for the requested byte from the SPI transfer. The frequency with which this
1997 * the requested byte from the SPI transfer. Can be called with an arbitrary
2115 struct spi_controller *ctlr = msg->spi->controller; in __spi_unoptimize_message()
2136 !msg->spi->controller->defer_optimize_message) in spi_maybe_unoptimize_message()
2222 * friends on every SPI message. Do this instead. in spi_stop_queue()
2260 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
2264 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
2287 * @spi: SPI device which is requesting transfer
2288 * @msg: SPI message which is to handled is queued to driver queue
2292 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
2294 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
2332 * sent before doing something. Is used by the spi-mem code to make sure SPI
2333 * memory operations do not preempt regular SPI transfers that have been queued
2334 * before the spi-mem operation.
2361 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
2368 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
2369 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
2370 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
2371 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
2372 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
2373 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
2374 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
2375 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
2376 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
2377 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
2380 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
2383 spi->mode |= SPI_NO_TX; in of_spi_parse_dt()
2388 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
2391 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
2394 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
2398 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
2404 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
2407 spi->mode |= SPI_NO_RX; in of_spi_parse_dt()
2412 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
2415 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
2418 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
2422 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
2442 spi_set_all_cs_unused(spi); in of_spi_parse_dt()
2459 dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n"); in of_spi_parse_dt()
2463 spi_set_chipselect(spi, idx, cs[idx]); in of_spi_parse_dt()
2466 * By default spi->chip_select[0] will hold the physical CS number, in of_spi_parse_dt()
2467 * so set bit 0 in spi->cs_index_mask. in of_spi_parse_dt()
2469 spi->cs_index_mask = BIT(0); in of_spi_parse_dt()
2472 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2473 spi->max_speed_hz = value; in of_spi_parse_dt()
2476 of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns"); in of_spi_parse_dt()
2477 of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns"); in of_spi_parse_dt()
2478 of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns"); in of_spi_parse_dt()
2486 struct spi_device *spi; in of_register_spi_device() local
2490 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2491 if (!spi) { in of_register_spi_device()
2498 rc = of_alias_from_compatible(nc, spi->modalias, in of_register_spi_device()
2499 sizeof(spi->modalias)); in of_register_spi_device()
2505 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2512 device_set_node(&spi->dev, of_fwnode_handle(nc)); in of_register_spi_device()
2515 rc = spi_add_device(spi); in of_register_spi_device()
2521 return spi; in of_register_spi_device()
2526 spi_dev_put(spi); in of_register_spi_device()
2531 * of_register_spi_devices() - Register child devices onto the SPI bus
2535 * represents a valid SPI slave.
2539 struct spi_device *spi; in of_register_spi_devices() local
2545 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2546 if (IS_ERR(spi)) { in of_register_spi_devices()
2548 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2558 * spi_new_ancillary_device() - Register ancillary SPI device
2559 * @spi: Pointer to the main SPI device registering the ancillary device
2562 * Register an ancillary SPI device; for example some chips have a chip-select
2565 * This may only be called from main SPI device's probe routine.
2569 struct spi_device *spi_new_ancillary_device(struct spi_device *spi, in spi_new_ancillary_device() argument
2572 struct spi_controller *ctlr = spi->controller; in spi_new_ancillary_device()
2589 /* Take over SPI mode/speed from SPI main device */ in spi_new_ancillary_device()
2590 ancillary->max_speed_hz = spi->max_speed_hz; in spi_new_ancillary_device()
2591 ancillary->mode = spi->mode; in spi_new_ancillary_device()
2593 * By default spi->chip_select[0] will hold the physical CS number, in spi_new_ancillary_device()
2594 * so set bit 0 in spi->cs_index_mask. in spi_new_ancillary_device()
2603 dev_err(&spi->dev, "failed to register ancillary device\n"); in spi_new_ancillary_device()
2775 * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2776 * @ctlr: controller to which the spi device belongs
2777 * @adev: ACPI Device for the spi device
2778 * @index: Index of the spi resource inside the ACPI Node
2780 * This should be used to allocate a new SPI device from and ACPI Device node.
2781 * The caller is responsible for calling spi_add_device to register the SPI device.
2783 * If ctlr is set to NULL, the Controller for the SPI device will be looked up
2797 struct spi_device *spi; in acpi_spi_device_alloc() local
2814 /* Found SPI in _CRS but it points to another controller */ in acpi_spi_device_alloc()
2820 /* Apple does not use _CRS but nested devices for SPI slaves */ in acpi_spi_device_alloc()
2827 spi = spi_alloc_device(lookup.ctlr); in acpi_spi_device_alloc()
2828 if (!spi) { in acpi_spi_device_alloc()
2829 dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_spi_device_alloc()
2834 spi_set_all_cs_unused(spi); in acpi_spi_device_alloc()
2835 spi_set_chipselect(spi, 0, lookup.chip_select); in acpi_spi_device_alloc()
2837 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_spi_device_alloc()
2838 spi->max_speed_hz = lookup.max_speed_hz; in acpi_spi_device_alloc()
2839 spi->mode |= lookup.mode; in acpi_spi_device_alloc()
2840 spi->irq = lookup.irq; in acpi_spi_device_alloc()
2841 spi->bits_per_word = lookup.bits_per_word; in acpi_spi_device_alloc()
2843 * By default spi->chip_select[0] will hold the physical CS number, in acpi_spi_device_alloc()
2844 * so set bit 0 in spi->cs_index_mask. in acpi_spi_device_alloc()
2846 spi->cs_index_mask = BIT(0); in acpi_spi_device_alloc()
2848 return spi; in acpi_spi_device_alloc()
2855 struct spi_device *spi; in acpi_register_spi_device() local
2861 spi = acpi_spi_device_alloc(ctlr, adev, -1); in acpi_register_spi_device()
2862 if (IS_ERR(spi)) { in acpi_register_spi_device()
2863 if (PTR_ERR(spi) == -ENOMEM) in acpi_register_spi_device()
2869 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2870 sizeof(spi->modalias)); in acpi_register_spi_device()
2872 if (spi->irq < 0) in acpi_register_spi_device()
2873 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in acpi_register_spi_device()
2878 if (spi_add_device(spi)) { in acpi_register_spi_device()
2880 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2882 spi_dev_put(spi); in acpi_register_spi_device()
2915 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2937 * spi_target_abort - abort the ongoing transfer request on an SPI slave
2939 * @spi: device used for the current transfer
2941 int spi_target_abort(struct spi_device *spi) in spi_target_abort() argument
2943 struct spi_controller *ctlr = spi->controller; in spi_target_abort()
2968 struct spi_device *spi; in slave_store() local
2986 spi = spi_alloc_device(ctlr); in slave_store()
2987 if (!spi) in slave_store()
2990 strscpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2992 rc = spi_add_device(spi); in slave_store()
2994 spi_dev_put(spi); in slave_store()
3029 * __spi_alloc_controller - allocate an SPI master or slave controller
3036 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
3040 * This call is used only by SPI controller drivers, which are the
3051 * Return: the SPI controller structure on success, else NULL.
3095 * @dev: physical device of SPI controller
3097 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
3100 * Allocate an SPI controller and automatically release a reference on it
3106 * Return: the SPI controller structure on success, else NULL.
3134 * @ctlr: The SPI master to grab GPIO descriptors for
3210 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
3211 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
3241 * spi_register_controller - register SPI master or slave controller
3246 * SPI controllers connect to their drivers using some non-SPI bus,
3248 * includes calling spi_register_controller() to hook up to this SPI bus glue.
3250 * SPI controllers use board specific (often SOC specific) bus numbers,
3251 * and board-specific addressing for SPI devices combines those numbers
3252 * with chip select numbers. Since SPI does not directly support dynamic
3276 * the SPI controller. in spi_register_controller()
3283 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
3291 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
3311 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
3393 * devm_spi_register_controller - register managed SPI master or slave
3395 * @dev: device managing SPI controller
3400 * Register a SPI device as with spi_register_controller() which will
3434 * spi_unregister_controller - unregister SPI master or slave controller
3438 * This call is used only by SPI controller drivers, which are the
3590 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3622 dev_err(&msg->spi->dev, in spi_replace_transfers()
3727 SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, in __spi_split_transfer_maxsize()
3734 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3742 * spi message unoptimize phase so this function should only be called from
3776 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3778 * certain number of SPI words
3784 * spi message unoptimize phase so this function should only be called from
3822 * Core methods for SPI controller protocol drivers. Some of the
3842 * @spi: the device that requires specific CS timing configuration
3846 static int spi_set_cs_timing(struct spi_device *spi) in spi_set_cs_timing() argument
3848 struct device *parent = spi->controller->dev.parent; in spi_set_cs_timing()
3851 if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) { in spi_set_cs_timing()
3852 if (spi->controller->auto_runtime_pm) { in spi_set_cs_timing()
3856 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_set_cs_timing()
3861 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3865 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3872 * spi_setup - setup SPI mode and clock rate
3873 * @spi: the device whose settings are being modified
3876 * SPI protocol drivers may need to update the transfer mode if the
3882 * or from it. When this function returns, the SPI device is deselected.
3891 int spi_setup(struct spi_device *spi) in spi_setup() argument
3900 if ((hweight_long(spi->mode & in spi_setup()
3902 (hweight_long(spi->mode & in spi_setup()
3904 dev_err(&spi->dev, in spi_setup()
3909 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3914 if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) { in spi_setup()
3915 dev_err(&spi->dev, in spi_setup()
3925 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | in spi_setup()
3931 dev_warn(&spi->dev, in spi_setup()
3934 spi->mode &= ~ugly_bits; in spi_setup()
3938 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3943 if (!spi->bits_per_word) { in spi_setup()
3944 spi->bits_per_word = 8; in spi_setup()
3950 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3951 spi->bits_per_word); in spi_setup()
3956 if (spi->controller->max_speed_hz && in spi_setup()
3957 (!spi->max_speed_hz || in spi_setup()
3958 spi->max_speed_hz > spi->controller->max_speed_hz)) in spi_setup()
3959 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3961 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3963 if (spi->controller->setup) { in spi_setup()
3964 status = spi->controller->setup(spi); in spi_setup()
3966 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3967 dev_err(&spi->controller->dev, "Failed to setup device: %d\n", in spi_setup()
3973 status = spi_set_cs_timing(spi); in spi_setup()
3975 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3979 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3980 status = pm_runtime_resume_and_get(spi->controller->dev.parent); in spi_setup()
3982 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3983 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3996 spi_set_cs(spi, false, true); in spi_setup()
3997 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3998 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
4000 spi_set_cs(spi, false, true); in spi_setup()
4003 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
4005 if (spi->rt && !spi->controller->rt) { in spi_setup()
4006 spi->controller->rt = true; in spi_setup()
4007 spi_set_thread_rt(spi->controller); in spi_setup()
4010 trace_spi_setup(spi, status); in spi_setup()
4012 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
4013 spi->mode & SPI_MODE_X_MASK, in spi_setup()
4014 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
4015 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
4016 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
4017 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
4018 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
4026 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
4034 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
4039 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
4045 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
4047 struct spi_controller *ctlr = spi->controller; in __spi_validate()
4054 message->spi = spi; in __spi_validate()
4063 (spi->mode & SPI_3WIRE)) { in __spi_validate()
4077 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
4089 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
4092 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
4101 * SPI transfer length should be multiple of SPI word size in __spi_validate()
4102 * where SPI word size should be power-of-two multiple. in __spi_validate()
4129 if (spi->mode & SPI_NO_TX) in __spi_validate()
4137 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
4140 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
4145 if (spi->mode & SPI_NO_RX) in __spi_validate()
4153 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
4156 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
4160 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
4173 * Under certain conditions, a SPI controller may not support arbitrary
4186 struct spi_controller *ctlr = msg->spi->controller; in spi_split_transfers()
4191 * If an SPI controller does not support toggling the CS line on each in spi_split_transfers()
4197 if ((msg->spi->mode & SPI_CS_WORD) && in spi_split_transfers()
4198 (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) { in spi_split_transfers()
4212 spi_max_transfer_size(msg->spi)); in spi_split_transfers()
4223 * @spi: the device that will be used for the message
4226 * Peripheral drivers will call spi_optimize_message() and the spi core will
4233 static int __spi_optimize_message(struct spi_device *spi, in __spi_optimize_message() argument
4236 struct spi_controller *ctlr = spi->controller; in __spi_optimize_message()
4239 ret = __spi_validate(spi, msg); in __spi_optimize_message()
4262 * @spi: the device that will be used for the message
4266 static int spi_maybe_optimize_message(struct spi_device *spi, in spi_maybe_optimize_message() argument
4269 if (spi->controller->defer_optimize_message) { in spi_maybe_optimize_message()
4270 msg->spi = spi; in spi_maybe_optimize_message()
4277 return __spi_optimize_message(spi, msg); in spi_maybe_optimize_message()
4281 * spi_optimize_message - do any one-time validation and setup for a SPI message
4282 * @spi: the device that will be used for the message
4300 int spi_optimize_message(struct spi_device *spi, struct spi_message *msg) in spi_optimize_message() argument
4306 * when using spi-mux. in spi_optimize_message()
4308 if (spi->controller->defer_optimize_message) in spi_optimize_message()
4311 ret = __spi_optimize_message(spi, msg); in spi_optimize_message()
4337 if (msg->spi->controller->defer_optimize_message) in spi_unoptimize_message()
4345 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
4347 struct spi_controller *ctlr = spi->controller; in __spi_async()
4351 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
4358 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); in __spi_async()
4369 return ctlr->transfer(spi, message); in __spi_async()
4379 * @dev: the device that manages @msg (usually @spi->dev)
4380 * @spi: the device that will be used for the message
4387 int devm_spi_optimize_message(struct device *dev, struct spi_device *spi, in devm_spi_optimize_message() argument
4392 ret = spi_optimize_message(spi, msg); in devm_spi_optimize_message()
4401 * spi_async - asynchronous SPI transfer
4402 * @spi: device with which data will be exchanged
4414 * deallocate the associated memory; it's no longer in use by any SPI
4431 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
4433 struct spi_controller *ctlr = spi->controller; in spi_async()
4437 ret = spi_maybe_optimize_message(spi, message); in spi_async()
4446 ret = __spi_async(spi, message); in spi_async()
4488 * Utility methods for SPI protocol drivers, layered on
4498 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
4503 struct spi_controller *ctlr = spi->controller; in __spi_sync()
4506 dev_warn_once(&spi->dev, "Attempted to sync while suspend\n"); in __spi_sync()
4510 status = spi_maybe_optimize_message(spi, message); in __spi_sync()
4515 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); in __spi_sync()
4530 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4547 status = __spi_async(spi, message); in __spi_sync()
4561 * spi_sync - blocking/synchronous SPI data transfers
4562 * @spi: device with which data will be exchanged
4570 * Note that the SPI device's chip select is active during the message,
4581 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
4585 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
4586 ret = __spi_sync(spi, message); in spi_sync()
4587 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
4595 * @spi: device with which data will be exchanged
4604 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4609 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
4611 return __spi_sync(spi, message); in spi_sync_locked()
4616 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4617 * @ctlr: SPI bus master that should be locked for exclusive bus access
4624 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4626 * and spi_async_locked calls when the SPI bus lock is held.
4647 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4648 * @ctlr: SPI bus master that was locked for exclusive bus access
4654 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4675 * spi_write_then_read - SPI synchronous write followed by read
4676 * @spi: device with which data will be exchanged
4694 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4736 status = spi_sync(spi, &message); in spi_write_then_read()
4760 /* The spi controllers are not using spi_bus, so we find it with another way */
4780 struct spi_device *spi; in of_spi_notify() local
4798 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4801 if (IS_ERR(spi)) { in of_spi_notify()
4805 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4815 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4816 if (spi == NULL) in of_spi_notify()
4820 spi_unregister_device(spi); in of_spi_notify()
4823 put_device(&spi->dev); in of_spi_notify()
4872 struct spi_device *spi; in acpi_spi_notify() local
4887 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4888 if (!spi) in acpi_spi_notify()
4891 spi_unregister_device(spi); in acpi_spi_notify()
4892 put_device(&spi->dev); in acpi_spi_notify()