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); \
378 const struct spi_device *spi = to_spi_device(dev); in spi_match_device() local
382 if (spi->driver_override) in spi_match_device()
383 return strcmp(spi->driver_override, drv->name) == 0; in spi_match_device()
394 return !!spi_match_id(sdrv->id_table, spi->modalias); in spi_match_device()
396 return strcmp(spi->modalias, drv->name) == 0; in spi_match_device()
401 const struct spi_device *spi = to_spi_device(dev); in spi_uevent() local
408 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); in spi_uevent()
414 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 if (spi->irq == -EPROBE_DEFER) in spi_probe()
425 if (spi->irq < 0) in spi_probe()
426 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->master = 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 acpi_device *adev = ACPI_COMPANION(&spi->dev); in spi_dev_set_name()
603 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); in spi_dev_set_name()
607 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), in spi_dev_set_name()
608 spi_get_chipselect(spi, 0)); in spi_dev_set_name()
613 struct spi_device *spi = to_spi_device(dev); in spi_dev_check() local
618 if (spi->controller == new_spi->controller) { in spi_dev_check()
620 cs = spi_get_chipselect(spi, idx); in spi_dev_check()
633 static void spi_cleanup(struct spi_device *spi) in spi_cleanup() argument
635 if (spi->controller->cleanup) in spi_cleanup()
636 spi->controller->cleanup(spi); in spi_cleanup()
639 static int __spi_add_device(struct spi_device *spi) in __spi_add_device() argument
641 struct spi_controller *ctlr = spi->controller; in __spi_add_device()
648 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
650 dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx), in __spi_add_device()
658 * For example, spi->chip_select[0] != spi->chip_select[1] and so on. in __spi_add_device()
661 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
663 nw_cs = spi_get_chipselect(spi, nw_idx); in __spi_add_device()
672 spi_dev_set_name(spi); in __spi_add_device()
679 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); in __spi_add_device()
693 cs = spi_get_chipselect(spi, idx); in __spi_add_device()
695 spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]); in __spi_add_device()
704 status = spi_setup(spi); in __spi_add_device()
707 dev_name(&spi->dev), status); in __spi_add_device()
712 status = device_add(&spi->dev); in __spi_add_device()
715 dev_name(&spi->dev), status); in __spi_add_device()
716 spi_cleanup(spi); in __spi_add_device()
718 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); in __spi_add_device()
726 * @spi: spi_device to register
729 * spi_alloc_device can be added onto the SPI bus with this function.
733 int spi_add_device(struct spi_device *spi) in spi_add_device() argument
735 struct spi_controller *ctlr = spi->controller; in spi_add_device()
739 spi_dev_set_name(spi); in spi_add_device()
742 status = __spi_add_device(spi); in spi_add_device()
749 * spi_new_device - instantiate one new SPI device
751 * @chip: Describes the SPI device
785 * logical CS in the spi->chip_select[]. If all the physical CS in spi_new_device()
804 * spi->chip_select[i] gives the corresponding physical CS for logical CS i in spi_new_device()
805 * logical CS number is represented by setting the ith bit in spi->cs_index_mask in spi_new_device()
806 * So, for example, if spi->cs_index_mask = 0x01 then logical CS number is 0 and in spi_new_device()
807 * spi->chip_select[0] will give the physical CS. in spi_new_device()
808 * By default spi->chip_select[0] will hold the physical CS number so, set in spi_new_device()
809 * spi->cs_index_mask as 0x01. in spi_new_device()
836 * spi_unregister_device - unregister a single SPI device
837 * @spi: spi_device to unregister
839 * Start making the passed SPI device vanish. Normally this would be handled
842 void spi_unregister_device(struct spi_device *spi) in spi_unregister_device() argument
844 if (!spi) in spi_unregister_device()
847 if (spi->dev.of_node) { in spi_unregister_device()
848 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); in spi_unregister_device()
849 of_node_put(spi->dev.of_node); in spi_unregister_device()
851 if (ACPI_COMPANION(&spi->dev)) in spi_unregister_device()
852 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); in spi_unregister_device()
853 device_remove_software_node(&spi->dev); in spi_unregister_device()
854 device_del(&spi->dev); in spi_unregister_device()
855 spi_cleanup(spi); in spi_unregister_device()
856 put_device(&spi->dev); in spi_unregister_device()
875 * spi_register_board_info - register SPI devices for a given board
881 * with segments of the SPI device table. Any device nodes are created later,
882 * after the relevant parent SPI controller (bus_num) is defined. We keep
887 * SPI devices through its expansion connector, so code initializing that board
888 * would naturally declare its SPI devices.
925 /* Core methods for SPI resource management */
928 * spi_res_alloc - allocate a spi resource that is life-cycle managed
931 * @spi: the SPI device for which we allocate memory
941 static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release, in spi_res_alloc() argument
957 * spi_res_free - free an SPI resource
973 * @message: the SPI message
985 * spi_res_release - release all SPI resources for this message
1004 static inline bool spi_is_last_cs(struct spi_device *spi) in spi_is_last_cs() argument
1010 if ((spi->cs_index_mask >> idx) & 0x01) { in spi_is_last_cs()
1011 if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx)) in spi_is_last_cs()
1019 static void spi_set_cs(struct spi_device *spi, bool enable, bool force) in spi_set_cs() argument
1028 if (!force && ((enable && spi->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1029 spi_is_last_cs(spi)) || in spi_set_cs()
1030 (!enable && spi->controller->last_cs_index_mask == spi->cs_index_mask && in spi_set_cs()
1031 !spi_is_last_cs(spi))) && in spi_set_cs()
1032 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) in spi_set_cs()
1035 trace_spi_set_cs(spi, activate); in spi_set_cs()
1037 spi->controller->last_cs_index_mask = spi->cs_index_mask; in spi_set_cs()
1039 spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : -1; in spi_set_cs()
1040 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; in spi_set_cs()
1042 if (spi->mode & SPI_CS_HIGH) in spi_set_cs()
1045 if (spi_is_csgpiod(spi)) { in spi_set_cs()
1046 if (!spi->controller->set_cs_timing && !activate) in spi_set_cs()
1047 spi_delay_exec(&spi->cs_hold, NULL); in spi_set_cs()
1049 if (!(spi->mode & SPI_NO_CS)) { in spi_set_cs()
1061 if (((spi->cs_index_mask >> idx) & 0x01) && in spi_set_cs()
1062 spi_get_csgpiod(spi, idx)) { in spi_set_cs()
1063 if (has_acpi_companion(&spi->dev)) in spi_set_cs()
1064 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), in spi_set_cs()
1068 gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), in spi_set_cs()
1072 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
1074 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
1078 /* Some SPI masters need both GPIO CS & slave_select */ in spi_set_cs()
1079 if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) && in spi_set_cs()
1080 spi->controller->set_cs) in spi_set_cs()
1081 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1083 if (!spi->controller->set_cs_timing) { in spi_set_cs()
1085 spi_delay_exec(&spi->cs_setup, NULL); in spi_set_cs()
1087 spi_delay_exec(&spi->cs_inactive, NULL); in spi_set_cs()
1089 } else if (spi->controller->set_cs) { in spi_set_cs()
1090 spi->controller->set_cs(spi, !enable); in spi_set_cs()
1225 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_map_msg()
1271 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) in __spi_unmap_msg()
1364 && !(msg->spi->mode & SPI_3WIRE)) { in spi_map_msg()
1414 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_wait()
1420 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); in spi_transfer_wait()
1428 * For each byte we wait for 8 cycles of the SPI clock. in spi_transfer_wait()
1450 dev_err(&msg->spi->dev, in spi_transfer_wait()
1451 "SPI transfer timed out\n"); in spi_transfer_wait()
1553 dev_err_once(&msg->spi->dev, in _spi_transfer_cs_change_delay()
1581 struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; in spi_transfer_one_message()
1584 spi_set_cs(msg->spi, !xfer->cs_off, false); in spi_transfer_one_message()
1605 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); in spi_transfer_one_message()
1621 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1622 "SPI transfer failed: %d\n", ret); in spi_transfer_one_message()
1635 dev_err(&msg->spi->dev, in spi_transfer_one_message()
1658 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1661 spi_set_cs(msg->spi, true, false); in spi_transfer_one_message()
1665 spi_set_cs(msg->spi, xfer->cs_off, false); in spi_transfer_one_message()
1673 spi_set_cs(msg->spi, false, false); in spi_transfer_one_message()
1690 * Called by SPI drivers using the core transfer_one_message()
1751 spi_max_transfer_size(msg->spi), in __spi_pump_transfer_message()
1819 * __spi_pump_messages - function which processes SPI message queue
1823 * This function checks if there is any SPI message in the queue that
1917 * spi_pump_messages - kthread work function which processes spi message queue
1941 * for the requested byte from the SPI transfer. The frequency with which this
1981 * the requested byte from the SPI transfer. Can be called with an arbitrary
2115 * In the prepare_messages callback the SPI bus has the opportunity in spi_finalize_current_message()
2178 * friends on every SPI message. Do this instead. in spi_stop_queue()
2218 static int __spi_queued_transfer(struct spi_device *spi, in __spi_queued_transfer() argument
2222 struct spi_controller *ctlr = spi->controller; in __spi_queued_transfer()
2245 * @spi: SPI device which is requesting transfer
2246 * @msg: SPI message which is to handled is queued to driver queue
2250 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) in spi_queued_transfer() argument
2252 return __spi_queued_transfer(spi, msg, true); in spi_queued_transfer()
2290 * sent before doing something. Is used by the spi-mem code to make sure SPI
2291 * memory operations do not preempt regular SPI transfers that have been queued
2292 * before the spi-mem operation.
2319 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, in of_spi_parse_dt() argument
2326 if (of_property_read_bool(nc, "spi-cpha")) in of_spi_parse_dt()
2327 spi->mode |= SPI_CPHA; in of_spi_parse_dt()
2328 if (of_property_read_bool(nc, "spi-cpol")) in of_spi_parse_dt()
2329 spi->mode |= SPI_CPOL; in of_spi_parse_dt()
2330 if (of_property_read_bool(nc, "spi-3wire")) in of_spi_parse_dt()
2331 spi->mode |= SPI_3WIRE; in of_spi_parse_dt()
2332 if (of_property_read_bool(nc, "spi-lsb-first")) in of_spi_parse_dt()
2333 spi->mode |= SPI_LSB_FIRST; in of_spi_parse_dt()
2334 if (of_property_read_bool(nc, "spi-cs-high")) in of_spi_parse_dt()
2335 spi->mode |= SPI_CS_HIGH; in of_spi_parse_dt()
2338 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { in of_spi_parse_dt()
2341 spi->mode |= SPI_NO_TX; in of_spi_parse_dt()
2346 spi->mode |= SPI_TX_DUAL; in of_spi_parse_dt()
2349 spi->mode |= SPI_TX_QUAD; in of_spi_parse_dt()
2352 spi->mode |= SPI_TX_OCTAL; in of_spi_parse_dt()
2356 "spi-tx-bus-width %d not supported\n", in of_spi_parse_dt()
2362 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { in of_spi_parse_dt()
2365 spi->mode |= SPI_NO_RX; in of_spi_parse_dt()
2370 spi->mode |= SPI_RX_DUAL; in of_spi_parse_dt()
2373 spi->mode |= SPI_RX_QUAD; in of_spi_parse_dt()
2376 spi->mode |= SPI_RX_OCTAL; in of_spi_parse_dt()
2380 "spi-rx-bus-width %d not supported\n", in of_spi_parse_dt()
2402 * logical CS in the spi->chip_select[]. If all the physical CS in of_spi_parse_dt()
2410 spi_set_chipselect(spi, idx, 0xFF); in of_spi_parse_dt()
2427 dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n"); in of_spi_parse_dt()
2431 spi_set_chipselect(spi, idx, cs[idx]); in of_spi_parse_dt()
2434 * spi->chip_select[i] gives the corresponding physical CS for logical CS i in of_spi_parse_dt()
2435 * logical CS number is represented by setting the ith bit in spi->cs_index_mask in of_spi_parse_dt()
2436 * So, for example, if spi->cs_index_mask = 0x01 then logical CS number is 0 and in of_spi_parse_dt()
2437 * spi->chip_select[0] will give the physical CS. in of_spi_parse_dt()
2438 * By default spi->chip_select[0] will hold the physical CS number so, set in of_spi_parse_dt()
2439 * spi->cs_index_mask as 0x01. in of_spi_parse_dt()
2441 spi->cs_index_mask = 0x01; in of_spi_parse_dt()
2444 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) in of_spi_parse_dt()
2445 spi->max_speed_hz = value; in of_spi_parse_dt()
2448 of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns"); in of_spi_parse_dt()
2449 of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns"); in of_spi_parse_dt()
2450 of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns"); in of_spi_parse_dt()
2458 struct spi_device *spi; in of_register_spi_device() local
2462 spi = spi_alloc_device(ctlr); in of_register_spi_device()
2463 if (!spi) { in of_register_spi_device()
2470 rc = of_alias_from_compatible(nc, spi->modalias, in of_register_spi_device()
2471 sizeof(spi->modalias)); in of_register_spi_device()
2477 rc = of_spi_parse_dt(ctlr, spi, nc); in of_register_spi_device()
2484 device_set_node(&spi->dev, of_fwnode_handle(nc)); in of_register_spi_device()
2487 rc = spi_add_device(spi); in of_register_spi_device()
2493 return spi; in of_register_spi_device()
2498 spi_dev_put(spi); in of_register_spi_device()
2503 * of_register_spi_devices() - Register child devices onto the SPI bus
2507 * represents a valid SPI slave.
2511 struct spi_device *spi; in of_register_spi_devices() local
2517 spi = of_register_spi_device(ctlr, nc); in of_register_spi_devices()
2518 if (IS_ERR(spi)) { in of_register_spi_devices()
2520 "Failed to create SPI device for %pOF\n", nc); in of_register_spi_devices()
2530 * spi_new_ancillary_device() - Register ancillary SPI device
2531 * @spi: Pointer to the main SPI device registering the ancillary device
2534 * Register an ancillary SPI device; for example some chips have a chip-select
2537 * This may only be called from main SPI device's probe routine.
2541 struct spi_device *spi_new_ancillary_device(struct spi_device *spi, in spi_new_ancillary_device() argument
2544 struct spi_controller *ctlr = spi->controller; in spi_new_ancillary_device()
2560 * logical CS in the spi->chip_select[]. If all the physical CS in spi_new_ancillary_device()
2573 /* Take over SPI mode/speed from SPI main device */ in spi_new_ancillary_device()
2574 ancillary->max_speed_hz = spi->max_speed_hz; in spi_new_ancillary_device()
2575 ancillary->mode = spi->mode; in spi_new_ancillary_device()
2577 * spi->chip_select[i] gives the corresponding physical CS for logical CS i in spi_new_ancillary_device()
2578 * logical CS number is represented by setting the ith bit in spi->cs_index_mask in spi_new_ancillary_device()
2579 * So, for example, if spi->cs_index_mask = 0x01 then logical CS number is 0 and in spi_new_ancillary_device()
2580 * spi->chip_select[0] will give the physical CS. in spi_new_ancillary_device()
2581 * By default spi->chip_select[0] will hold the physical CS number so, set in spi_new_ancillary_device()
2582 * spi->cs_index_mask as 0x01. in spi_new_ancillary_device()
2591 dev_err(&spi->dev, "failed to register ancillary device\n"); in spi_new_ancillary_device()
2763 * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2764 * @ctlr: controller to which the spi device belongs
2765 * @adev: ACPI Device for the spi device
2766 * @index: Index of the spi resource inside the ACPI Node
2768 * This should be used to allocate a new SPI device from and ACPI Device node.
2769 * The caller is responsible for calling spi_add_device to register the SPI device.
2771 * If ctlr is set to NULL, the Controller for the SPI device will be looked up
2785 struct spi_device *spi; in acpi_spi_device_alloc() local
2803 /* Found SPI in _CRS but it points to another controller */ in acpi_spi_device_alloc()
2809 /* Apple does not use _CRS but nested devices for SPI slaves */ in acpi_spi_device_alloc()
2816 spi = spi_alloc_device(lookup.ctlr); in acpi_spi_device_alloc()
2817 if (!spi) { in acpi_spi_device_alloc()
2818 dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", in acpi_spi_device_alloc()
2825 * logical CS in the spi->chip_select[]. If all the physical CS in acpi_spi_device_alloc()
2833 spi_set_chipselect(spi, idx, 0xFF); in acpi_spi_device_alloc()
2835 ACPI_COMPANION_SET(&spi->dev, adev); in acpi_spi_device_alloc()
2836 spi->max_speed_hz = lookup.max_speed_hz; in acpi_spi_device_alloc()
2837 spi->mode |= lookup.mode; in acpi_spi_device_alloc()
2838 spi->irq = lookup.irq; in acpi_spi_device_alloc()
2839 spi->bits_per_word = lookup.bits_per_word; in acpi_spi_device_alloc()
2840 spi_set_chipselect(spi, 0, lookup.chip_select); in acpi_spi_device_alloc()
2842 * spi->chip_select[i] gives the corresponding physical CS for logical CS i in acpi_spi_device_alloc()
2843 * logical CS number is represented by setting the ith bit in spi->cs_index_mask in acpi_spi_device_alloc()
2844 * So, for example, if spi->cs_index_mask = 0x01 then logical CS number is 0 and in acpi_spi_device_alloc()
2845 * spi->chip_select[0] will give the physical CS. in acpi_spi_device_alloc()
2846 * By default spi->chip_select[0] will hold the physical CS number so, set in acpi_spi_device_alloc()
2847 * spi->cs_index_mask as 0x01. in acpi_spi_device_alloc()
2849 spi->cs_index_mask = 0x01; in acpi_spi_device_alloc()
2851 return spi; in acpi_spi_device_alloc()
2858 struct spi_device *spi; in acpi_register_spi_device() local
2864 spi = acpi_spi_device_alloc(ctlr, adev, -1); in acpi_register_spi_device()
2865 if (IS_ERR(spi)) { in acpi_register_spi_device()
2866 if (PTR_ERR(spi) == -ENOMEM) in acpi_register_spi_device()
2872 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, in acpi_register_spi_device()
2873 sizeof(spi->modalias)); in acpi_register_spi_device()
2875 if (spi->irq < 0) in acpi_register_spi_device()
2876 spi->irq = acpi_dev_gpio_irq_get(adev, 0); in acpi_register_spi_device()
2881 if (spi_add_device(spi)) { in acpi_register_spi_device()
2883 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", in acpi_register_spi_device()
2885 spi_dev_put(spi); in acpi_register_spi_device()
2918 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); in acpi_register_spi_devices()
2940 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2942 * @spi: device used for the current transfer
2944 int spi_slave_abort(struct spi_device *spi) in spi_slave_abort() argument
2946 struct spi_controller *ctlr = spi->controller; in spi_slave_abort()
2955 int spi_target_abort(struct spi_device *spi) in spi_target_abort() argument
2957 struct spi_controller *ctlr = spi->controller; in spi_target_abort()
2982 struct spi_device *spi; in slave_store() local
3000 spi = spi_alloc_device(ctlr); in slave_store()
3001 if (!spi) in slave_store()
3004 strscpy(spi->modalias, name, sizeof(spi->modalias)); in slave_store()
3006 rc = spi_add_device(spi); in slave_store()
3008 spi_dev_put(spi); in slave_store()
3043 * __spi_alloc_controller - allocate an SPI master or slave controller
3050 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
3054 * This call is used only by SPI controller drivers, which are the
3065 * Return: the SPI controller structure on success, else NULL.
3109 * @dev: physical device of SPI controller
3111 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
3114 * Allocate an SPI controller and automatically release a reference on it
3120 * Return: the SPI controller structure on success, else NULL.
3148 * @ctlr: The SPI master to grab GPIO descriptors for
3224 * The controller may implement only the high-level SPI-memory like in spi_controller_check_ops()
3225 * operations if it does not support regular SPI transfers, and this is in spi_controller_check_ops()
3255 * spi_register_controller - register SPI master or slave controller
3260 * SPI controllers connect to their drivers using some non-SPI bus,
3262 * includes calling spi_register_controller() to hook up to this SPI bus glue.
3264 * SPI controllers use board specific (often SOC specific) bus numbers,
3265 * and board-specific addressing for SPI devices combines those numbers
3266 * with chip select numbers. Since SPI does not directly support dynamic
3290 * the SPI controller. in spi_register_controller()
3297 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi"); in spi_register_controller()
3305 first_dynamic = of_alias_get_highest_id("spi"); in spi_register_controller()
3325 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); in spi_register_controller()
3407 * devm_spi_register_controller - register managed SPI master or slave
3409 * @dev: device managing SPI controller
3414 * Register a SPI device as with spi_register_controller() which will
3448 * spi_unregister_controller - unregister SPI master or slave controller
3452 * This call is used only by SPI controller drivers, which are the
3604 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, in spi_replace_transfers()
3636 dev_err(&msg->spi->dev, in spi_replace_transfers()
3749 SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, in __spi_split_transfer_maxsize()
3756 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3796 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3798 * certain number of SPI words
3840 * Core methods for SPI controller protocol drivers. Some of the
3860 * @spi: the device that requires specific CS timing configuration
3864 static int spi_set_cs_timing(struct spi_device *spi) in spi_set_cs_timing() argument
3866 struct device *parent = spi->controller->dev.parent; in spi_set_cs_timing()
3869 if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) { in spi_set_cs_timing()
3870 if (spi->controller->auto_runtime_pm) { in spi_set_cs_timing()
3874 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_set_cs_timing()
3879 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3883 status = spi->controller->set_cs_timing(spi); in spi_set_cs_timing()
3890 * spi_setup - setup SPI mode and clock rate
3891 * @spi: the device whose settings are being modified
3894 * SPI protocol drivers may need to update the transfer mode if the
3900 * or from it. When this function returns, the SPI device is deselected.
3909 int spi_setup(struct spi_device *spi) in spi_setup() argument
3918 if ((hweight_long(spi->mode & in spi_setup()
3920 (hweight_long(spi->mode & in spi_setup()
3922 dev_err(&spi->dev, in spi_setup()
3927 if ((spi->mode & SPI_3WIRE) && (spi->mode & in spi_setup()
3937 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | in spi_setup()
3943 dev_warn(&spi->dev, in spi_setup()
3946 spi->mode &= ~ugly_bits; in spi_setup()
3950 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", in spi_setup()
3955 if (!spi->bits_per_word) { in spi_setup()
3956 spi->bits_per_word = 8; in spi_setup()
3962 status = __spi_validate_bits_per_word(spi->controller, in spi_setup()
3963 spi->bits_per_word); in spi_setup()
3968 if (spi->controller->max_speed_hz && in spi_setup()
3969 (!spi->max_speed_hz || in spi_setup()
3970 spi->max_speed_hz > spi->controller->max_speed_hz)) in spi_setup()
3971 spi->max_speed_hz = spi->controller->max_speed_hz; in spi_setup()
3973 mutex_lock(&spi->controller->io_mutex); in spi_setup()
3975 if (spi->controller->setup) { in spi_setup()
3976 status = spi->controller->setup(spi); in spi_setup()
3978 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3979 dev_err(&spi->controller->dev, "Failed to setup device: %d\n", in spi_setup()
3985 status = spi_set_cs_timing(spi); in spi_setup()
3987 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3991 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { in spi_setup()
3992 status = pm_runtime_resume_and_get(spi->controller->dev.parent); in spi_setup()
3994 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
3995 dev_err(&spi->controller->dev, "Failed to power device: %d\n", in spi_setup()
4008 spi_set_cs(spi, false, true); in spi_setup()
4009 pm_runtime_mark_last_busy(spi->controller->dev.parent); in spi_setup()
4010 pm_runtime_put_autosuspend(spi->controller->dev.parent); in spi_setup()
4012 spi_set_cs(spi, false, true); in spi_setup()
4015 mutex_unlock(&spi->controller->io_mutex); in spi_setup()
4017 if (spi->rt && !spi->controller->rt) { in spi_setup()
4018 spi->controller->rt = true; in spi_setup()
4019 spi_set_thread_rt(spi->controller); in spi_setup()
4022 trace_spi_setup(spi, status); in spi_setup()
4024 dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", in spi_setup()
4025 spi->mode & SPI_MODE_X_MASK, in spi_setup()
4026 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", in spi_setup()
4027 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", in spi_setup()
4028 (spi->mode & SPI_3WIRE) ? "3wire, " : "", in spi_setup()
4029 (spi->mode & SPI_LOOP) ? "loopback, " : "", in spi_setup()
4030 spi->bits_per_word, spi->max_speed_hz, in spi_setup()
4038 struct spi_device *spi) in _spi_xfer_word_delay_update() argument
4046 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); in _spi_xfer_word_delay_update()
4051 memcpy(&xfer->word_delay, &spi->word_delay, in _spi_xfer_word_delay_update()
4057 static int __spi_validate(struct spi_device *spi, struct spi_message *message) in __spi_validate() argument
4059 struct spi_controller *ctlr = spi->controller; in __spi_validate()
4067 * If an SPI controller does not support toggling the CS line on each in __spi_validate()
4073 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || in __spi_validate()
4074 spi_is_csgpiod(spi))) { in __spi_validate()
4075 size_t maxsize = BITS_TO_BYTES(spi->bits_per_word); in __spi_validate()
4078 /* spi_split_transfers_maxsize() requires message->spi */ in __spi_validate()
4079 message->spi = spi; in __spi_validate()
4101 (spi->mode & SPI_3WIRE)) { in __spi_validate()
4115 * Set transfer bits_per_word and max speed as spi device default if in __spi_validate()
4127 xfer->bits_per_word = spi->bits_per_word; in __spi_validate()
4130 xfer->speed_hz = spi->max_speed_hz; in __spi_validate()
4139 * SPI transfer length should be multiple of SPI word size in __spi_validate()
4140 * where SPI word size should be power-of-two multiple. in __spi_validate()
4167 if (spi->mode & SPI_NO_TX) in __spi_validate()
4174 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) in __spi_validate()
4177 !(spi->mode & SPI_TX_QUAD)) in __spi_validate()
4182 if (spi->mode & SPI_NO_RX) in __spi_validate()
4189 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) in __spi_validate()
4192 !(spi->mode & SPI_RX_QUAD)) in __spi_validate()
4196 if (_spi_xfer_word_delay_update(xfer, spi)) in __spi_validate()
4205 static int __spi_async(struct spi_device *spi, struct spi_message *message) in __spi_async() argument
4207 struct spi_controller *ctlr = spi->controller; in __spi_async()
4211 * Some controllers do not support doing regular SPI transfers. Return in __spi_async()
4217 message->spi = spi; in __spi_async()
4220 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); in __spi_async()
4231 return ctlr->transfer(spi, message); in __spi_async()
4235 * spi_async - asynchronous SPI transfer
4236 * @spi: device with which data will be exchanged
4248 * deallocate the associated memory; it's no longer in use by any SPI
4265 int spi_async(struct spi_device *spi, struct spi_message *message) in spi_async() argument
4267 struct spi_controller *ctlr = spi->controller; in spi_async()
4271 ret = __spi_validate(spi, message); in spi_async()
4280 ret = __spi_async(spi, message); in spi_async()
4290 * @spi: device with which data will be exchanged
4302 * deallocate the associated memory; it's no longer in use by any SPI
4319 static int spi_async_locked(struct spi_device *spi, struct spi_message *message) in spi_async_locked() argument
4321 struct spi_controller *ctlr = spi->controller; in spi_async_locked()
4325 ret = __spi_validate(spi, message); in spi_async_locked()
4331 ret = __spi_async(spi, message); in spi_async_locked()
4373 * Utility methods for SPI protocol drivers, layered on
4383 static int __spi_sync(struct spi_device *spi, struct spi_message *message) in __spi_sync() argument
4387 struct spi_controller *ctlr = spi->controller; in __spi_sync()
4390 dev_warn_once(&spi->dev, "Attempted to sync while suspend\n"); in __spi_sync()
4394 status = __spi_validate(spi, message); in __spi_sync()
4398 message->spi = spi; in __spi_sync()
4401 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); in __spi_sync()
4416 SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); in __spi_sync()
4431 status = spi_async_locked(spi, message); in __spi_sync()
4442 * spi_sync - blocking/synchronous SPI data transfers
4443 * @spi: device with which data will be exchanged
4451 * Note that the SPI device's chip select is active during the message,
4462 int spi_sync(struct spi_device *spi, struct spi_message *message) in spi_sync() argument
4466 mutex_lock(&spi->controller->bus_lock_mutex); in spi_sync()
4467 ret = __spi_sync(spi, message); in spi_sync()
4468 mutex_unlock(&spi->controller->bus_lock_mutex); in spi_sync()
4476 * @spi: device with which data will be exchanged
4485 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4490 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) in spi_sync_locked() argument
4492 return __spi_sync(spi, message); in spi_sync_locked()
4497 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4498 * @ctlr: SPI bus master that should be locked for exclusive bus access
4505 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4507 * and spi_async_locked calls when the SPI bus lock is held.
4528 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4529 * @ctlr: SPI bus master that was locked for exclusive bus access
4535 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4556 * spi_write_then_read - SPI synchronous write followed by read
4557 * @spi: device with which data will be exchanged
4575 int spi_write_then_read(struct spi_device *spi, in spi_write_then_read() argument
4617 status = spi_sync(spi, &message); in spi_write_then_read()
4641 /* The spi controllers are not using spi_bus, so we find it with another way */
4661 struct spi_device *spi; in of_spi_notify() local
4679 spi = of_register_spi_device(ctlr, rd->dn); in of_spi_notify()
4682 if (IS_ERR(spi)) { in of_spi_notify()
4686 return notifier_from_errno(PTR_ERR(spi)); in of_spi_notify()
4696 spi = of_find_spi_device_by_node(rd->dn); in of_spi_notify()
4697 if (spi == NULL) in of_spi_notify()
4701 spi_unregister_device(spi); in of_spi_notify()
4704 put_device(&spi->dev); in of_spi_notify()
4753 struct spi_device *spi; in acpi_spi_notify() local
4768 spi = acpi_spi_find_device_by_adev(adev); in acpi_spi_notify()
4769 if (!spi) in acpi_spi_notify()
4772 spi_unregister_device(spi); in acpi_spi_notify()
4773 put_device(&spi->dev); in acpi_spi_notify()