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
421 spi->irq = of_irq_get(dev->of_node, 0); in spi_probe()
422 else if (is_acpi_device_node(fwnode) && spi->irq < 0) in spi_probe()
423 spi->irq = acpi_dev_gpio_irq_get(to_acpi_device_node(fwnode), 0); in spi_probe()
424 if (spi->irq == -EPROBE_DEFER) in spi_probe()
425 return dev_err_probe(dev, spi->irq, "Failed to get irq\n"); in spi_probe()
426 if (spi->irq < 0) in spi_probe()
427 spi->irq = 0; in spi_probe()
434 ret = sdrv->probe(spi); in spi_probe()
463 .name = "spi",
474 * __spi_register_driver - register a SPI driver
487 * For Really Good Reasons we use spi: modaliases not of: in __spi_register_driver()
517 pr_warn("SPI driver %s has no spi_device_id for %s\n", in __spi_register_driver()
529 * SPI devices should normally not be created by SPI device drivers; that
530 * would make them board-specific. Similarly with SPI controller drivers.
551 * spi_alloc_device - Allocate a new SPI device
561 * spi_device structure to add it to the SPI controller. If the caller
569 struct spi_device *spi; in spi_alloc_device() local
574 spi = kzalloc(sizeof(*spi), GFP_KERNEL); in spi_alloc_device()
575 if (!spi) { in spi_alloc_device()
580 spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL); in spi_alloc_device()
581 if (!spi->pcpu_statistics) { in spi_alloc_device()
582 kfree(spi); in spi_alloc_device()
587 spi->controller = ctlr; in spi_alloc_device()
588 spi->dev.parent = &ctlr->dev; in spi_alloc_device()
589 spi->dev.bus = &spi_bus_type; in spi_alloc_device()
590 spi->dev.release = spidev_release; in spi_alloc_device()
591 spi->mode = ctlr->buswidth_override_bits; in spi_alloc_device()
593 device_initialize(&spi->dev); in spi_alloc_device()
594 return spi; in spi_alloc_device()
598 static void spi_dev_set_name(struct spi_device *spi) in spi_dev_set_name() argument
600 struct device *dev = &spi->dev; in spi_dev_set_name()
604 dev_set_name(dev, "spi-%s", acpi_dev_name(to_acpi_device_node(fwnode))); in spi_dev_set_name()
609 dev_set_name(dev, "spi-%pfwP", fwnode); in spi_dev_set_name()
613 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
614 spi_get_chipselect(spi, 0)); in spi_dev_set_name()
619 * logical CS in the spi->chip_select[]. If all the physical CS
634 struct spi_device *spi, u8 idx, in spi_dev_check_cs() argument
640 cs = spi_get_chipselect(spi, idx); in spi_dev_check_cs()
653 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
657 if (spi->controller == new_spi->controller) { in spi_dev_check()
659 status = spi_dev_check_cs(dev, spi, idx, new_spi, 0); in spi_dev_check()
667 static void spi_cleanup(struct spi_device *spi) in spi_cleanup() argument
669 if (spi->controller->cleanup) in spi_cleanup()
670 spi->controller->cleanup(spi); in spi_cleanup()
673 static int __spi_add_device(struct spi_device *spi) in __spi_add_device() argument
675 struct spi_controller *ctlr = spi->controller; in __spi_add_device()
682 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
684 dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx), in __spi_add_device()
692 * For example, spi->chip_select[0] != spi->chip_select[1] and so on. in __spi_add_device()
696 status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1); in __spi_add_device()
703 spi_dev_set_name(spi); in __spi_add_device()
710 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in __spi_add_device()
724 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
726 spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]); in __spi_add_device()
735 status = spi_setup(spi); in __spi_add_device()
738 dev_name(&spi->dev), status); in __spi_add_device()
743 status = device_add(&spi->dev); in __spi_add_device()
746 dev_name(&spi->dev), status); in __spi_add_device()
747 spi_cleanup(spi); in __spi_add_device()
749 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in __spi_add_device()
757 * @spi: spi_device to register
760 * spi_alloc_device can be added onto the SPI bus with this function.
764 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
766 struct spi_controller *ctlr = spi->controller; in spi_add_device()
770 spi_dev_set_name(spi); in spi_add_device()
773 status = __spi_add_device(spi); in spi_add_device()
779 static void spi_set_all_cs_unused(struct spi_device *spi) in spi_set_all_cs_unused() argument
784 spi_set_chipselect(spi, idx, SPI_INVALID_CS); in spi_set_all_cs_unused()
788 * spi_new_device - instantiate one new SPI device
790 * @chip: Describes the SPI device
833 * By default spi->chip_select[0] will hold the physical CS number, in spi_new_device()
834 * so set bit 0 in spi->cs_index_mask. in spi_new_device()
861 * spi_unregister_device - unregister a single SPI device
862 * @spi: spi_device to unregister
864 * Start making the passed SPI device vanish. Normally this would be handled
867 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
871 if (!spi) in spi_unregister_device()
874 fwnode = dev_fwnode(&spi->dev); in spi_unregister_device()
881 device_remove_software_node(&spi->dev); in spi_unregister_device()
882 device_del(&spi->dev); in spi_unregister_device()
883 spi_cleanup(spi); in spi_unregister_device()
884 put_device(&spi->dev); in spi_unregister_device()
903 * spi_register_board_info - register SPI devices for a given board
909 * with segments of the SPI device table. Any device nodes are created later,
910 * after the relevant parent SPI controller (bus_num) is defined. We keep
915 * SPI devices through its expansion connector, so code initializing that board
916 * would naturally declare its SPI devices.
953 /* Core methods for SPI resource management */
956 * spi_res_alloc - allocate a spi resource that is life-cycle managed
959 * @spi: the SPI device for which we allocate memory
969 static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release, in spi_res_alloc() argument
985 * spi_res_free - free an SPI resource
998 * @message: the SPI message
1010 * spi_res_release - release all SPI resources for this message
1029 #define spi_for_each_valid_cs(spi, idx) \ argument
1031 if (!(spi->cs_index_mask & BIT(idx))) {} else
1033 static inline bool spi_is_last_cs(struct spi_device *spi) in spi_is_last_cs() argument
1038 spi_for_each_valid_cs(spi, idx) { in spi_is_last_cs()
1039 if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx)) in spi_is_last_cs()
1045 static void spi_toggle_csgpiod(struct spi_device *spi, u8 idx, bool enable, bool activate) in spi_toggle_csgpiod() argument
1057 if (is_acpi_device_node(dev_fwnode(&spi->dev))) in spi_toggle_csgpiod()
1058 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), !enable); in spi_toggle_csgpiod()
1061 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), activate); in spi_toggle_csgpiod()
1064 spi_delay_exec(&spi->cs_setup, NULL); in spi_toggle_csgpiod()
1066 spi_delay_exec(&spi->cs_inactive, NULL); in spi_toggle_csgpiod()
1069 static void spi_set_cs(struct spi_device *spi, bool enable, bool force) in spi_set_cs() argument
1078 if (!force && ((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 (!enable && spi->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1081 !spi_is_last_cs(spi))) && in spi_set_cs()
1082 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
1085 trace_spi_set_cs(spi, activate); in spi_set_cs()
1087 spi->controller->last_cs_index_mask = spi->cs_index_mask; in spi_set_cs()
1089 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()
1092 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
1099 if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate) in spi_set_cs()
1100 spi_delay_exec(&spi->cs_hold, NULL); in spi_set_cs()
1102 if (spi_is_csgpiod(spi)) { in spi_set_cs()
1103 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
1104 spi_for_each_valid_cs(spi, idx) { in spi_set_cs()
1105 if (spi_get_csgpiod(spi, idx)) in spi_set_cs()
1106 spi_toggle_csgpiod(spi, idx, enable, activate); in spi_set_cs()
1109 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
1110 if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) && in spi_set_cs()
1111 spi->controller->set_cs) in spi_set_cs()
1112 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1113 } else if (spi->controller->set_cs) { in spi_set_cs()
1114 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1117 if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) { in spi_set_cs()
1119 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
1121 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
1255 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
1391 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1441 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_wait()
1447 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1455 * For each byte we wait for 8 cycles of the SPI clock. in spi_transfer_wait()
1477 dev_err(&msg->spi->dev, in spi_transfer_wait()
1478 "SPI transfer timed out\n"); in spi_transfer_wait()
1580 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1608 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_one_message()
1611 spi_set_cs(msg->spi, !xfer->cs_off, false); in spi_transfer_one_message()
1632 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1648 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1649 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1662 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1685 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1688 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1692 spi_set_cs(msg->spi, xfer->cs_off, false); in spi_transfer_one_message()
1700 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1717 * Called by SPI drivers using the core transfer_one_message()
1837 * __spi_pump_messages - function which processes SPI message queue
1841 * This function checks if there is any SPI message in the queue that
1935 * spi_pump_messages - kthread work function which processes spi message queue
1959 * for the requested byte from the SPI transfer. The frequency with which this
1999 * the requested byte from the SPI transfer. Can be called with an arbitrary
2117 struct spi_controller *ctlr = msg->spi->controller; in __spi_unoptimize_message()
2138 !msg->spi->controller->defer_optimize_message) in spi_maybe_unoptimize_message()
2224 * friends on every SPI message. Do this instead. in spi_stop_queue()
2262 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
2266 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
2289 * @spi: SPI device which is requesting transfer
2290 * @msg: SPI message which is to handled is queued to driver queue
2294 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
2296 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
2334 * sent before doing something. Is used by the spi-mem code to make sure SPI
2335 * memory operations do not preempt regular SPI transfers that have been queued
2336 * before the spi-mem operation.
2363 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
2370 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
2371 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
2372 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
2373 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
2374 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
2375 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
2376 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
2377 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
2378 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
2379 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
2382 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
2385 spi->mode |= SPI_NO_TX; in of_spi_parse_dt()
2390 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
2393 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
2396 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
2400 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
2406 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
2409 spi->mode |= SPI_NO_RX; in of_spi_parse_dt()
2414 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
2417 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
2420 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
2424 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
2444 spi_set_all_cs_unused(spi); in of_spi_parse_dt()
2461 dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n"); in of_spi_parse_dt()
2465 spi_set_chipselect(spi, idx, cs[idx]); in of_spi_parse_dt()
2468 * By default spi->chip_select[0] will hold the physical CS number, in of_spi_parse_dt()
2469 * so set bit 0 in spi->cs_index_mask. in of_spi_parse_dt()
2471 spi->cs_index_mask = BIT(0); in of_spi_parse_dt()
2474 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2475 spi->max_speed_hz = value; in of_spi_parse_dt()
2478 of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns"); in of_spi_parse_dt()
2479 of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns"); in of_spi_parse_dt()
2480 of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns"); in of_spi_parse_dt()
2488 struct spi_device *spi; in of_register_spi_device() local
2492 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2493 if (!spi) { in of_register_spi_device()
2500 rc = of_alias_from_compatible(nc, spi->modalias, in of_register_spi_device()
2501 sizeof(spi->modalias)); in of_register_spi_device()
2507 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2514 device_set_node(&spi->dev, of_fwnode_handle(nc)); in of_register_spi_device()
2517 rc = spi_add_device(spi); in of_register_spi_device()
2523 return spi; in of_register_spi_device()
2528 spi_dev_put(spi); in of_register_spi_device()
2533 * of_register_spi_devices() - Register child devices onto the SPI bus
2537 * represents a valid SPI slave.
2541 struct spi_device *spi; in of_register_spi_devices() local
2547 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2548 if (IS_ERR(spi)) { in of_register_spi_devices()
2550 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2560 * spi_new_ancillary_device() - Register ancillary SPI device
2561 * @spi: Pointer to the main SPI device registering the ancillary device
2564 * Register an ancillary SPI device; for example some chips have a chip-select
2567 * This may only be called from main SPI device's probe routine.
2571 struct spi_device *spi_new_ancillary_device(struct spi_device *spi, in spi_new_ancillary_device() argument
2574 struct spi_controller *ctlr = spi->controller; in spi_new_ancillary_device()
2591 /* Take over SPI mode/speed from SPI main device */ in spi_new_ancillary_device()
2592 ancillary->max_speed_hz = spi->max_speed_hz; in spi_new_ancillary_device()
2593 ancillary->mode = spi->mode; in spi_new_ancillary_device()
2595 * By default spi->chip_select[0] will hold the physical CS number, in spi_new_ancillary_device()
2596 * so set bit 0 in spi->cs_index_mask. in spi_new_ancillary_device()
2605 dev_err(&spi->dev, "failed to register ancillary device\n"); in spi_new_ancillary_device()
2777 * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2778 * @ctlr: controller to which the spi device belongs
2779 * @adev: ACPI Device for the spi device
2780 * @index: Index of the spi resource inside the ACPI Node
2782 * This should be used to allocate a new SPI device from and ACPI Device node.
2783 * The caller is responsible for calling spi_add_device to register the SPI device.
2785 * If ctlr is set to NULL, the Controller for the SPI device will be looked up
2799 struct spi_device *spi; in acpi_spi_device_alloc() local
2816 /* Found SPI in _CRS but it points to another controller */ in acpi_spi_device_alloc()
2822 /* Apple does not use _CRS but nested devices for SPI slaves */ in acpi_spi_device_alloc()
2829 spi = spi_alloc_device(lookup.ctlr); in acpi_spi_device_alloc()
2830 if (!spi) { in acpi_spi_device_alloc()
2831 dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_spi_device_alloc()
2836 spi_set_all_cs_unused(spi); in acpi_spi_device_alloc()
2837 spi_set_chipselect(spi, 0, lookup.chip_select); in acpi_spi_device_alloc()
2839 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_spi_device_alloc()
2840 spi->max_speed_hz = lookup.max_speed_hz; in acpi_spi_device_alloc()
2841 spi->mode |= lookup.mode; in acpi_spi_device_alloc()
2842 spi->irq = lookup.irq; in acpi_spi_device_alloc()
2843 spi->bits_per_word = lookup.bits_per_word; in acpi_spi_device_alloc()
2845 * By default spi->chip_select[0] will hold the physical CS number, in acpi_spi_device_alloc()
2846 * so set bit 0 in spi->cs_index_mask. in acpi_spi_device_alloc()
2848 spi->cs_index_mask = BIT(0); in acpi_spi_device_alloc()
2850 return spi; in acpi_spi_device_alloc()
2857 struct spi_device *spi; in acpi_register_spi_device() local
2863 spi = acpi_spi_device_alloc(ctlr, adev, -1); in acpi_register_spi_device()
2864 if (IS_ERR(spi)) { in acpi_register_spi_device()
2865 if (PTR_ERR(spi) == -ENOMEM) in acpi_register_spi_device()
2871 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2872 sizeof(spi->modalias)); in acpi_register_spi_device()
2877 if (spi_add_device(spi)) { in acpi_register_spi_device()
2879 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2881 spi_dev_put(spi); in acpi_register_spi_device()
2914 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2936 * spi_target_abort - abort the ongoing transfer request on an SPI slave
2938 * @spi: device used for the current transfer
2940 int spi_target_abort(struct spi_device *spi) in spi_target_abort() argument
2942 struct spi_controller *ctlr = spi->controller; in spi_target_abort()
2971 struct spi_device *spi; in slave_store() local
2989 spi = spi_alloc_device(ctlr); in slave_store()
2990 if (!spi) in slave_store()
2993 strscpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
2995 rc = spi_add_device(spi); in slave_store()
2997 spi_dev_put(spi); in slave_store()
3032 * __spi_alloc_controller - allocate an SPI master or slave controller
3039 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
3043 * This call is used only by SPI controller drivers, which are the
3054 * Return: the SPI controller structure on success, else NULL.
3098 * @dev: physical device of SPI controller
3100 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
3103 * Allocate an SPI controller and automatically release a reference on it
3109 * Return: the SPI controller structure on success, else NULL.
3137 * @ctlr: The SPI master to grab GPIO descriptors for
3213 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
3214 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
3244 * spi_register_controller - register SPI host or target controller
3249 * SPI controllers connect to their drivers using some non-SPI bus,
3251 * includes calling spi_register_controller() to hook up to this SPI bus glue.
3253 * SPI controllers use board specific (often SOC specific) bus numbers,
3254 * and board-specific addressing for SPI devices combines those numbers
3255 * with chip select numbers. Since SPI does not directly support dynamic
3279 * the SPI controller. in spi_register_controller()
3286 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
3294 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
3314 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
3396 * devm_spi_register_controller - register managed SPI host or target
3398 * @dev: device managing SPI controller
3403 * Register a SPI device as with spi_register_controller() which will
3437 * spi_unregister_controller - unregister SPI master or slave controller
3441 * This call is used only by SPI controller drivers, which are the
3593 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3625 dev_err(&msg->spi->dev, in spi_replace_transfers()
3730 SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, in __spi_split_transfer_maxsize()
3737 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3745 * spi message unoptimize phase so this function should only be called from
3779 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3781 * certain number of SPI words
3787 * spi message unoptimize phase so this function should only be called from
3825 * Core methods for SPI controller protocol drivers. Some of the
3845 * @spi: the device that requires specific CS timing configuration
3849 static int spi_set_cs_timing(struct spi_device *spi) in spi_set_cs_timing() argument
3851 struct device *parent = spi->controller->dev.parent; in spi_set_cs_timing()
3854 if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) { in spi_set_cs_timing()
3855 if (spi->controller->auto_runtime_pm) { in spi_set_cs_timing()
3859 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_set_cs_timing()
3864 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3868 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3875 * spi_setup - setup SPI mode and clock rate
3876 * @spi: the device whose settings are being modified
3879 * SPI protocol drivers may need to update the transfer mode if the
3885 * or from it. When this function returns, the SPI device is deselected.
3894 int spi_setup(struct spi_device *spi) in spi_setup() argument
3903 if ((hweight_long(spi->mode & in spi_setup()
3905 (hweight_long(spi->mode & in spi_setup()
3907 dev_err(&spi->dev, in spi_setup()
3912 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3917 if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) { in spi_setup()
3918 dev_err(&spi->dev, in spi_setup()
3928 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | in spi_setup()
3934 dev_warn(&spi->dev, in spi_setup()
3937 spi->mode &= ~ugly_bits; in spi_setup()
3941 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3946 if (!spi->bits_per_word) { in spi_setup()
3947 spi->bits_per_word = 8; in spi_setup()
3953 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3954 spi->bits_per_word); in spi_setup()
3959 if (spi->controller->max_speed_hz && in spi_setup()
3960 (!spi->max_speed_hz || in spi_setup()
3961 spi->max_speed_hz > spi->controller->max_speed_hz)) in spi_setup()
3962 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3964 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3966 if (spi->controller->setup) { in spi_setup()
3967 status = spi->controller->setup(spi); in spi_setup()
3969 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3970 dev_err(&spi->controller->dev, "Failed to setup device: %d\n", in spi_setup()
3976 status = spi_set_cs_timing(spi); in spi_setup()
3978 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3982 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3983 status = pm_runtime_resume_and_get(spi->controller->dev.parent); in spi_setup()
3985 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3986 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
3999 spi_set_cs(spi, false, true); in spi_setup()
4000 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
4001 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
4003 spi_set_cs(spi, false, true); in spi_setup()
4006 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
4008 if (spi->rt && !spi->controller->rt) { in spi_setup()
4009 spi->controller->rt = true; in spi_setup()
4010 spi_set_thread_rt(spi->controller); in spi_setup()
4013 trace_spi_setup(spi, status); in spi_setup()
4015 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
4016 spi->mode & SPI_MODE_X_MASK, in spi_setup()
4017 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
4018 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
4019 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
4020 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
4021 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
4029 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
4037 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
4042 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
4048 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
4050 struct spi_controller *ctlr = spi->controller; in __spi_validate()
4057 message->spi = spi; in __spi_validate()
4066 (spi->mode & SPI_3WIRE)) { in __spi_validate()
4080 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
4092 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
4095 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
4104 * SPI transfer length should be multiple of SPI word size in __spi_validate()
4105 * where SPI word size should be power-of-two multiple. in __spi_validate()
4132 if (spi->mode & SPI_NO_TX) in __spi_validate()
4140 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
4143 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
4148 if (spi->mode & SPI_NO_RX) in __spi_validate()
4156 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
4159 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
4163 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
4176 * Under certain conditions, a SPI controller may not support arbitrary
4189 struct spi_controller *ctlr = msg->spi->controller; in spi_split_transfers()
4194 * If an SPI controller does not support toggling the CS line on each in spi_split_transfers()
4200 if ((msg->spi->mode & SPI_CS_WORD) && in spi_split_transfers()
4201 (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) { in spi_split_transfers()
4215 spi_max_transfer_size(msg->spi)); in spi_split_transfers()
4226 * @spi: the device that will be used for the message
4229 * Peripheral drivers will call spi_optimize_message() and the spi core will
4236 static int __spi_optimize_message(struct spi_device *spi, in __spi_optimize_message() argument
4239 struct spi_controller *ctlr = spi->controller; in __spi_optimize_message()
4242 ret = __spi_validate(spi, msg); in __spi_optimize_message()
4265 * @spi: the device that will be used for the message
4269 static int spi_maybe_optimize_message(struct spi_device *spi, in spi_maybe_optimize_message() argument
4272 if (spi->controller->defer_optimize_message) { in spi_maybe_optimize_message()
4273 msg->spi = spi; in spi_maybe_optimize_message()
4280 return __spi_optimize_message(spi, msg); in spi_maybe_optimize_message()
4284 * spi_optimize_message - do any one-time validation and setup for a SPI message
4285 * @spi: the device that will be used for the message
4303 int spi_optimize_message(struct spi_device *spi, struct spi_message *msg) in spi_optimize_message() argument
4309 * when using spi-mux. in spi_optimize_message()
4311 if (spi->controller->defer_optimize_message) in spi_optimize_message()
4314 ret = __spi_optimize_message(spi, msg); in spi_optimize_message()
4340 if (msg->spi->controller->defer_optimize_message) in spi_unoptimize_message()
4348 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
4350 struct spi_controller *ctlr = spi->controller; in __spi_async()
4354 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
4361 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); in __spi_async()
4372 return ctlr->transfer(spi, message); in __spi_async()
4382 * @dev: the device that manages @msg (usually @spi->dev)
4383 * @spi: the device that will be used for the message
4390 int devm_spi_optimize_message(struct device *dev, struct spi_device *spi, in devm_spi_optimize_message() argument
4395 ret = spi_optimize_message(spi, msg); in devm_spi_optimize_message()
4404 * spi_async - asynchronous SPI transfer
4405 * @spi: device with which data will be exchanged
4417 * deallocate the associated memory; it's no longer in use by any SPI
4434 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
4436 struct spi_controller *ctlr = spi->controller; in spi_async()
4440 ret = spi_maybe_optimize_message(spi, message); in spi_async()
4449 ret = __spi_async(spi, message); in spi_async()
4491 * Utility methods for SPI protocol drivers, layered on
4501 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
4506 struct spi_controller *ctlr = spi->controller; in __spi_sync()
4509 dev_warn_once(&spi->dev, "Attempted to sync while suspend\n"); in __spi_sync()
4513 status = spi_maybe_optimize_message(spi, message); in __spi_sync()
4518 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); in __spi_sync()
4533 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4550 status = __spi_async(spi, message); in __spi_sync()
4564 * spi_sync - blocking/synchronous SPI data transfers
4565 * @spi: device with which data will be exchanged
4573 * Note that the SPI device's chip select is active during the message,
4584 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
4588 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
4589 ret = __spi_sync(spi, message); in spi_sync()
4590 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
4598 * @spi: device with which data will be exchanged
4607 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4612 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
4614 return __spi_sync(spi, message); in spi_sync_locked()
4619 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4620 * @ctlr: SPI bus master that should be locked for exclusive bus access
4627 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4629 * and spi_async_locked calls when the SPI bus lock is held.
4650 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4651 * @ctlr: SPI bus master that was locked for exclusive bus access
4657 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4678 * spi_write_then_read - SPI synchronous write followed by read
4679 * @spi: device with which data will be exchanged
4697 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4739 status = spi_sync(spi, &message); in spi_write_then_read()
4763 /* The spi controllers are not using spi_bus, so we find it with another way */
4783 struct spi_device *spi; in of_spi_notify() local
4801 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4804 if (IS_ERR(spi)) { in of_spi_notify()
4808 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4818 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4819 if (spi == NULL) in of_spi_notify()
4823 spi_unregister_device(spi); in of_spi_notify()
4826 put_device(&spi->dev); in of_spi_notify()
4875 struct spi_device *spi; in acpi_spi_notify() local
4890 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4891 if (!spi) in acpi_spi_notify()
4894 spi_unregister_device(spi); in acpi_spi_notify()
4895 put_device(&spi->dev); in acpi_spi_notify()