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->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1080 spi_is_last_cs(spi)) || in spi_set_cs()
1081 (!enable && spi->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1082 !spi_is_last_cs(spi))) && in spi_set_cs()
1083 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
1086 trace_spi_set_cs(spi, activate); in spi_set_cs()
1088 spi->controller->last_cs_index_mask = spi->cs_index_mask; in spi_set_cs()
1090 spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : SPI_INVALID_CS; in spi_set_cs()
1091 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
1093 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
1100 if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate) in spi_set_cs()
1101 spi_delay_exec(&spi->cs_hold, NULL); in spi_set_cs()
1103 if (spi_is_csgpiod(spi)) { in spi_set_cs()
1104 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
1105 spi_for_each_valid_cs(spi, idx) { in spi_set_cs()
1106 if (spi_get_csgpiod(spi, idx)) in spi_set_cs()
1107 spi_toggle_csgpiod(spi, idx, enable, activate); in spi_set_cs()
1110 /* Some SPI controllers need both GPIO CS & ->set_cs() */ in spi_set_cs()
1111 if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) && in spi_set_cs()
1112 spi->controller->set_cs) in spi_set_cs()
1113 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1114 } else if (spi->controller->set_cs) { in spi_set_cs()
1115 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1118 if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) { in spi_set_cs()
1120 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
1122 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
1256 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
1392 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1442 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_wait()
1448 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1456 * For each byte we wait for 8 cycles of the SPI clock. in spi_transfer_wait()
1478 dev_err(&msg->spi->dev, in spi_transfer_wait()
1479 "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 target device.
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 target devices */ 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()
2875 if (spi_add_device(spi)) { in acpi_register_spi_device()
2877 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2879 spi_dev_put(spi); in acpi_register_spi_device()
2912 dev_warn(&ctlr->dev, "failed to enumerate SPI target devices\n"); in acpi_register_spi_devices()
2934 * spi_target_abort - abort the ongoing transfer request on an SPI target controller
2935 * @spi: device used for the current transfer
2937 int spi_target_abort(struct spi_device *spi) in spi_target_abort() argument
2939 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 host or target controller
3036 * @target: flag indicating whether to allocate an SPI host (false) or SPI target (true)
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 * @target: whether to allocate an SPI host (false) or SPI target (true) controller
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 controller 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 host or target 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 host or target controller
3394 * @dev: device managing SPI controller
3399 * Register a SPI device as with spi_register_controller() which will
3433 * spi_unregister_controller - unregister SPI host or target controller
3437 * This call is used only by SPI controller drivers, which are the
3589 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3621 dev_err(&msg->spi->dev, in spi_replace_transfers()
3726 SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, in __spi_split_transfer_maxsize()
3733 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3741 * spi message unoptimize phase so this function should only be called from
3775 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3777 * certain number of SPI words
3783 * spi message unoptimize phase so this function should only be called from
3821 * Core methods for SPI controller protocol drivers. Some of the
3841 * @spi: the device that requires specific CS timing configuration
3845 static int spi_set_cs_timing(struct spi_device *spi) in spi_set_cs_timing() argument
3847 struct device *parent = spi->controller->dev.parent; in spi_set_cs_timing()
3850 if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) { in spi_set_cs_timing()
3851 if (spi->controller->auto_runtime_pm) { in spi_set_cs_timing()
3855 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_set_cs_timing()
3860 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3864 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3871 * spi_setup - setup SPI mode and clock rate
3872 * @spi: the device whose settings are being modified
3875 * SPI protocol drivers may need to update the transfer mode if the
3881 * or from it. When this function returns, the SPI device is deselected.
3890 int spi_setup(struct spi_device *spi) in spi_setup() argument
3899 if ((hweight_long(spi->mode & in spi_setup()
3901 (hweight_long(spi->mode & in spi_setup()
3903 dev_err(&spi->dev, in spi_setup()
3908 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3913 if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) { in spi_setup()
3914 dev_err(&spi->dev, in spi_setup()
3924 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | in spi_setup()
3930 dev_warn(&spi->dev, in spi_setup()
3933 spi->mode &= ~ugly_bits; in spi_setup()
3937 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3942 if (!spi->bits_per_word) { in spi_setup()
3943 spi->bits_per_word = 8; in spi_setup()
3949 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3950 spi->bits_per_word); in spi_setup()
3955 if (spi->controller->max_speed_hz && in spi_setup()
3956 (!spi->max_speed_hz || in spi_setup()
3957 spi->max_speed_hz > spi->controller->max_speed_hz)) in spi_setup()
3958 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3960 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3962 if (spi->controller->setup) { in spi_setup()
3963 status = spi->controller->setup(spi); in spi_setup()
3965 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3966 dev_err(&spi->controller->dev, "Failed to setup device: %d\n", in spi_setup()
3972 status = spi_set_cs_timing(spi); in spi_setup()
3974 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3978 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3979 status = pm_runtime_resume_and_get(spi->controller->dev.parent); in spi_setup()
3981 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3982 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3995 spi_set_cs(spi, false, true); in spi_setup()
3996 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
3997 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
3999 spi_set_cs(spi, false, true); in spi_setup()
4002 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
4004 if (spi->rt && !spi->controller->rt) { in spi_setup()
4005 spi->controller->rt = true; in spi_setup()
4006 spi_set_thread_rt(spi->controller); in spi_setup()
4009 trace_spi_setup(spi, status); in spi_setup()
4011 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
4012 spi->mode & SPI_MODE_X_MASK, in spi_setup()
4013 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
4014 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
4015 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
4016 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
4017 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
4025 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
4033 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
4038 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
4044 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
4046 struct spi_controller *ctlr = spi->controller; in __spi_validate()
4053 message->spi = spi; in __spi_validate()
4062 (spi->mode & SPI_3WIRE)) { in __spi_validate()
4076 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
4088 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
4091 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
4100 * SPI transfer length should be multiple of SPI word size in __spi_validate()
4101 * where SPI word size should be power-of-two multiple. in __spi_validate()
4128 if (spi->mode & SPI_NO_TX) in __spi_validate()
4136 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
4139 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
4144 if (spi->mode & SPI_NO_RX) in __spi_validate()
4152 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
4155 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
4159 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
4181 * Under certain conditions, a SPI controller may not support arbitrary
4194 struct spi_controller *ctlr = msg->spi->controller; in spi_split_transfers()
4199 * If an SPI controller does not support toggling the CS line on each in spi_split_transfers()
4205 if ((msg->spi->mode & SPI_CS_WORD) && in spi_split_transfers()
4206 (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) { in spi_split_transfers()
4220 spi_max_transfer_size(msg->spi)); in spi_split_transfers()
4231 * @spi: the device that will be used for the message
4234 * Peripheral drivers will call spi_optimize_message() and the spi core will
4241 static int __spi_optimize_message(struct spi_device *spi, in __spi_optimize_message() argument
4244 struct spi_controller *ctlr = spi->controller; in __spi_optimize_message()
4247 ret = __spi_validate(spi, msg); in __spi_optimize_message()
4270 * @spi: the device that will be used for the message
4274 static int spi_maybe_optimize_message(struct spi_device *spi, in spi_maybe_optimize_message() argument
4277 if (spi->controller->defer_optimize_message) { in spi_maybe_optimize_message()
4278 msg->spi = spi; in spi_maybe_optimize_message()
4285 return __spi_optimize_message(spi, msg); in spi_maybe_optimize_message()
4289 * spi_optimize_message - do any one-time validation and setup for a SPI message
4290 * @spi: the device that will be used for the message
4308 int spi_optimize_message(struct spi_device *spi, struct spi_message *msg) in spi_optimize_message() argument
4314 * when using spi-mux. in spi_optimize_message()
4316 if (spi->controller->defer_optimize_message) in spi_optimize_message()
4319 ret = __spi_optimize_message(spi, msg); in spi_optimize_message()
4345 if (msg->spi->controller->defer_optimize_message) in spi_unoptimize_message()
4353 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
4355 struct spi_controller *ctlr = spi->controller; in __spi_async()
4359 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
4366 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); in __spi_async()
4377 return ctlr->transfer(spi, message); in __spi_async()
4387 * @dev: the device that manages @msg (usually @spi->dev)
4388 * @spi: the device that will be used for the message
4395 int devm_spi_optimize_message(struct device *dev, struct spi_device *spi, in devm_spi_optimize_message() argument
4400 ret = spi_optimize_message(spi, msg); in devm_spi_optimize_message()
4409 * spi_async - asynchronous SPI transfer
4410 * @spi: device with which data will be exchanged
4422 * deallocate the associated memory; it's no longer in use by any SPI
4439 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
4441 struct spi_controller *ctlr = spi->controller; in spi_async()
4445 ret = spi_maybe_optimize_message(spi, message); in spi_async()
4454 ret = __spi_async(spi, message); in spi_async()
4496 * Utility methods for SPI protocol drivers, layered on
4506 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
4511 struct spi_controller *ctlr = spi->controller; in __spi_sync()
4514 dev_warn_once(&spi->dev, "Attempted to sync while suspend\n"); in __spi_sync()
4518 status = spi_maybe_optimize_message(spi, message); in __spi_sync()
4523 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); in __spi_sync()
4538 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4555 status = __spi_async(spi, message); in __spi_sync()
4569 * spi_sync - blocking/synchronous SPI data transfers
4570 * @spi: device with which data will be exchanged
4578 * Note that the SPI device's chip select is active during the message,
4589 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
4593 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
4594 ret = __spi_sync(spi, message); in spi_sync()
4595 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
4603 * @spi: device with which data will be exchanged
4612 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4617 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
4619 return __spi_sync(spi, message); in spi_sync_locked()
4624 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4625 * @ctlr: SPI bus controller that should be locked for exclusive bus access
4632 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4634 * and spi_async_locked calls when the SPI bus lock is held.
4655 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4656 * @ctlr: SPI bus controller that was locked for exclusive bus access
4662 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4683 * spi_write_then_read - SPI synchronous write followed by read
4684 * @spi: device with which data will be exchanged
4702 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4744 status = spi_sync(spi, &message); in spi_write_then_read()
4768 /* The spi controllers are not using spi_bus, so we find it with another way */
4788 struct spi_device *spi; in of_spi_notify() local
4806 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4809 if (IS_ERR(spi)) { in of_spi_notify()
4813 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4823 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4824 if (spi == NULL) in of_spi_notify()
4828 spi_unregister_device(spi); in of_spi_notify()
4831 put_device(&spi->dev); in of_spi_notify()
4880 struct spi_device *spi; in acpi_spi_notify() local
4895 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4896 if (!spi) in acpi_spi_notify()
4899 spi_unregister_device(spi); in acpi_spi_notify()
4900 put_device(&spi->dev); in acpi_spi_notify()