Lines Matching +full:spi +full:- +full:lsb +full:- +full:first
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPI init/core code
11 #include <linux/dma-mapping.h>
16 #include <linux/clk/clk-conf.h>
19 #include <linux/spi/spi.h>
20 #include <linux/spi/spi-mem.h>
38 #include <trace/events/spi.h>
48 struct spi_device *spi = to_spi_device(dev);
50 /* spi controllers may cleanup for released devices */
51 if (spi->controller->cleanup)
52 spi->controller->cleanup(spi);
54 spi_controller_put(spi->controller);
55 kfree(spi->driver_override);
56 kfree(spi);
62 const struct spi_device *spi = to_spi_device(dev);
65 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
66 if (len != -ENODEV)
69 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
77 struct spi_device *spi = to_spi_device(dev);
79 const size_t len = end ? end - buf : count;
83 if (len >= (PAGE_SIZE - 1))
84 return -EINVAL;
88 return -ENOMEM;
91 old = spi->driver_override;
93 spi->driver_override = driver_override;
96 spi->driver_override = NULL;
108 const struct spi_device *spi = to_spi_device(dev);
112 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
125 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
135 struct spi_device *spi = to_spi_device(dev); \
136 return spi_statistics_##field##_show(&spi->statistics, buf); \
149 spin_lock_irqsave(&stat->lock, flags); \
150 len = sprintf(buf, format_string, stat->field); \
151 spin_unlock_irqrestore(&stat->lock, flags); \
177 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
178 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
179 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
180 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
181 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
182 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
183 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
184 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
185 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
186 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
187 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
188 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
189 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
190 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
191 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
192 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
297 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
302 spin_lock_irqsave(&stats->lock, flags);
304 stats->transfers++;
305 stats->transfer_bytes_histo[l2len]++;
307 stats->bytes += xfer->len;
308 if ((xfer->tx_buf) &&
309 (xfer->tx_buf != ctlr->dummy_tx))
310 stats->bytes_tx += xfer->len;
311 if ((xfer->rx_buf) &&
312 (xfer->rx_buf != ctlr->dummy_rx))
313 stats->bytes_rx += xfer->len;
315 spin_unlock_irqrestore(&stats->lock, flags);
319 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
326 while (id->name[0]) {
327 if (!strcmp(sdev->modalias, id->name))
336 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
338 return spi_match_id(sdrv->id_table, sdev);
344 const struct spi_device *spi = to_spi_device(dev);
347 /* Check override first, and if set, only use the named driver */
348 if (spi->driver_override)
349 return strcmp(spi->driver_override, drv->name) == 0;
359 if (sdrv->id_table)
360 return !!spi_match_id(sdrv->id_table, spi);
362 return strcmp(spi->modalias, drv->name) == 0;
367 const struct spi_device *spi = to_spi_device(dev);
371 if (rc != -ENODEV)
374 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
378 .name = "spi",
388 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
389 struct spi_device *spi = to_spi_device(dev);
392 ret = of_clk_set_defaults(dev->of_node, false);
396 if (dev->of_node) {
397 spi->irq = of_irq_get(dev->of_node, 0);
398 if (spi->irq == -EPROBE_DEFER)
399 return -EPROBE_DEFER;
400 if (spi->irq < 0)
401 spi->irq = 0;
408 ret = sdrv->probe(spi);
417 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
420 ret = sdrv->remove(to_spi_device(dev));
428 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
430 sdrv->shutdown(to_spi_device(dev));
434 * __spi_register_driver - register a SPI driver
443 sdrv->driver.owner = owner;
444 sdrv->driver.bus = &spi_bus_type;
445 if (sdrv->probe)
446 sdrv->driver.probe = spi_drv_probe;
447 if (sdrv->remove)
448 sdrv->driver.remove = spi_drv_remove;
449 if (sdrv->shutdown)
450 sdrv->driver.shutdown = spi_drv_shutdown;
451 return driver_register(&sdrv->driver);
455 /*-------------------------------------------------------------------------*/
457 /* SPI devices should normally not be created by SPI device drivers; that
458 * would make them board-specific. Similarly with SPI controller drivers.
459 * Device registration normally goes into like arch/.../mach.../board-YYY.c
479 * spi_alloc_device - Allocate a new SPI device
489 * spi_device structure to add it to the SPI controller. If the caller
497 struct spi_device *spi;
502 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
503 if (!spi) {
508 spi->master = spi->controller = ctlr;
509 spi->dev.parent = &ctlr->dev;
510 spi->dev.bus = &spi_bus_type;
511 spi->dev.release = spidev_release;
512 spi->cs_gpio = -ENOENT;
514 spin_lock_init(&spi->statistics.lock);
516 device_initialize(&spi->dev);
517 return spi;
521 static void spi_dev_set_name(struct spi_device *spi)
523 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
526 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
530 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
531 spi->chip_select);
536 struct spi_device *spi = to_spi_device(dev);
539 if (spi->controller == new_spi->controller &&
540 spi->chip_select == new_spi->chip_select)
541 return -EBUSY;
546 * spi_add_device - Add spi_device allocated with spi_alloc_device
547 * @spi: spi_device to register
550 * spi_alloc_device can be added onto the spi bus with this function.
554 int spi_add_device(struct spi_device *spi)
557 struct spi_controller *ctlr = spi->controller;
558 struct device *dev = ctlr->dev.parent;
562 if (spi->chip_select >= ctlr->num_chipselect) {
563 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
564 ctlr->num_chipselect);
565 return -EINVAL;
569 spi_dev_set_name(spi);
577 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
580 spi->chip_select);
585 if (ctlr->cs_gpiods)
586 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
587 else if (ctlr->cs_gpios)
588 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
594 status = spi_setup(spi);
597 dev_name(&spi->dev), status);
602 status = device_add(&spi->dev);
605 dev_name(&spi->dev), status);
607 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
616 * spi_new_device - instantiate one new SPI device
618 * @chip: Describes the SPI device
622 * after board init creates the hard-wired devices. Some development
625 * driver could add devices (which it would learn about out-of-band).
635 /* NOTE: caller did any chip->bus_num checks necessary.
638 * error-or-pointer (not NULL-or-pointer), troubleshootability
646 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
648 proxy->chip_select = chip->chip_select;
649 proxy->max_speed_hz = chip->max_speed_hz;
650 proxy->mode = chip->mode;
651 proxy->irq = chip->irq;
652 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
653 proxy->dev.platform_data = (void *) chip->platform_data;
654 proxy->controller_data = chip->controller_data;
655 proxy->controller_state = NULL;
657 if (chip->properties) {
658 status = device_add_properties(&proxy->dev, chip->properties);
660 dev_err(&ctlr->dev,
662 chip->modalias, status);
674 if (chip->properties)
675 device_remove_properties(&proxy->dev);
683 * spi_unregister_device - unregister a single SPI device
684 * @spi: spi_device to unregister
686 * Start making the passed SPI device vanish. Normally this would be handled
689 void spi_unregister_device(struct spi_device *spi)
691 if (!spi)
694 if (spi->dev.of_node) {
695 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
696 of_node_put(spi->dev.of_node);
698 if (ACPI_COMPANION(&spi->dev))
699 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
700 device_unregister(&spi->dev);
709 if (ctlr->bus_num != bi->bus_num)
714 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
715 bi->modalias);
719 * spi_register_board_info - register SPI devices for a given board
724 * Board-specific early init code calls this (probably during arch_initcall)
725 * with segments of the SPI device table. Any device nodes are created later,
726 * after the relevant parent SPI controller (bus_num) is defined. We keep
728 * not make Linux forget about these hard-wired devices.
730 * Other code can also call this, e.g. a particular add-on board might provide
731 * SPI devices through its expansion connector, so code initializing that board
732 * would naturally declare its SPI devices.
735 * any embedded pointers (platform_data, etc), they're copied as-is.
736 * Device properties are deep-copied though.
750 return -ENOMEM;
755 memcpy(&bi->board_info, info, sizeof(*info));
756 if (info->properties) {
757 bi->board_info.properties =
758 property_entries_dup(info->properties);
759 if (IS_ERR(bi->board_info.properties))
760 return PTR_ERR(bi->board_info.properties);
764 list_add_tail(&bi->list, &board_list);
767 &bi->board_info);
774 /*-------------------------------------------------------------------------*/
776 static void spi_set_cs(struct spi_device *spi, bool enable)
778 if (spi->mode & SPI_CS_HIGH)
781 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
784 * active low is default for SPI. Execution paths that handle
789 if (!(spi->mode & SPI_NO_CS)) {
790 if (spi->cs_gpiod)
791 gpiod_set_value_cansleep(spi->cs_gpiod,
794 gpio_set_value_cansleep(spi->cs_gpio, !enable);
796 /* Some SPI masters need both GPIO CS & slave_select */
797 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
798 spi->controller->set_cs)
799 spi->controller->set_cs(spi, !enable);
800 } else if (spi->controller->set_cs) {
801 spi->controller->set_cs(spi, !enable);
831 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
834 return -EINVAL;
841 sg = &sgt->sgl[0];
852 PAGE_SIZE - offset_in_page(buf)));
859 return -ENOMEM;
870 len -= min;
874 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
876 ret = -ENOMEM;
882 sgt->nents = ret;
890 if (sgt->orig_nents) {
891 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
902 if (!ctlr->can_dma)
905 if (ctlr->dma_tx)
906 tx_dev = ctlr->dma_tx->device->dev;
908 tx_dev = ctlr->dev.parent;
910 if (ctlr->dma_rx)
911 rx_dev = ctlr->dma_rx->device->dev;
913 rx_dev = ctlr->dev.parent;
915 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
916 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
919 if (xfer->tx_buf != NULL) {
920 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
921 (void *)xfer->tx_buf, xfer->len,
927 if (xfer->rx_buf != NULL) {
928 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
929 xfer->rx_buf, xfer->len,
932 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
939 ctlr->cur_msg_mapped = true;
949 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
952 if (ctlr->dma_tx)
953 tx_dev = ctlr->dma_tx->device->dev;
955 tx_dev = ctlr->dev.parent;
957 if (ctlr->dma_rx)
958 rx_dev = ctlr->dma_rx->device->dev;
960 rx_dev = ctlr->dev.parent;
962 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
963 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
966 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
967 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
991 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
996 if (xfer->tx_buf == ctlr->dummy_tx)
997 xfer->tx_buf = NULL;
998 if (xfer->rx_buf == ctlr->dummy_rx)
999 xfer->rx_buf = NULL;
1011 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
1015 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1016 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
1017 !xfer->tx_buf)
1018 max_tx = max(xfer->len, max_tx);
1019 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
1020 !xfer->rx_buf)
1021 max_rx = max(xfer->len, max_rx);
1025 tmp = krealloc(ctlr->dummy_tx, max_tx,
1028 return -ENOMEM;
1029 ctlr->dummy_tx = tmp;
1034 tmp = krealloc(ctlr->dummy_rx, max_rx,
1037 return -ENOMEM;
1038 ctlr->dummy_rx = tmp;
1042 list_for_each_entry(xfer, &msg->transfers,
1044 if (!xfer->len)
1046 if (!xfer->tx_buf)
1047 xfer->tx_buf = ctlr->dummy_tx;
1048 if (!xfer->rx_buf)
1049 xfer->rx_buf = ctlr->dummy_rx;
1061 struct spi_statistics *statm = &ctlr->statistics;
1062 struct spi_statistics *stats = &msg->spi->statistics;
1066 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1067 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1068 return -EINTR;
1071 ms = 8LL * 1000LL * xfer->len;
1072 do_div(ms, xfer->speed_hz);
1078 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1084 dev_err(&msg->spi->dev,
1085 "SPI transfer timed out\n");
1086 return -ETIMEDOUT;
1112 u32 delay = xfer->cs_change_delay;
1113 u32 unit = xfer->cs_change_delay_unit;
1116 /* return early on "fast" mode - for everything but USECS */
1134 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1138 dev_err_once(&msg->spi->dev,
1140 xfer->cs_change_delay_unit);
1148 * spi_transfer_one_message - Default implementation of transfer_one_message()
1160 struct spi_statistics *statm = &ctlr->statistics;
1161 struct spi_statistics *stats = &msg->spi->statistics;
1163 spi_set_cs(msg->spi, true);
1168 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1174 if (xfer->tx_buf || xfer->rx_buf) {
1175 reinit_completion(&ctlr->xfer_completion);
1177 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1183 dev_err(&msg->spi->dev,
1184 "SPI transfer failed: %d\n", ret);
1191 msg->status = ret;
1194 if (xfer->len)
1195 dev_err(&msg->spi->dev,
1197 xfer->len);
1202 if (msg->status != -EINPROGRESS)
1205 if (xfer->delay_usecs)
1206 _spi_transfer_delay_ns(xfer->delay_usecs * 1000);
1208 if (xfer->cs_change) {
1209 if (list_is_last(&xfer->transfer_list,
1210 &msg->transfers)) {
1213 spi_set_cs(msg->spi, false);
1215 spi_set_cs(msg->spi, true);
1219 msg->actual_length += xfer->len;
1224 spi_set_cs(msg->spi, false);
1226 if (msg->status == -EINPROGRESS)
1227 msg->status = ret;
1229 if (msg->status && ctlr->handle_err)
1230 ctlr->handle_err(ctlr, msg);
1240 * spi_finalize_current_transfer - report completion of a transfer
1243 * Called by SPI drivers using the core transfer_one_message()
1249 complete(&ctlr->xfer_completion);
1254 * __spi_pump_messages - function which processes spi message queue
1258 * This function checks if there is any spi message in the queue that
1274 spin_lock_irqsave(&ctlr->queue_lock, flags);
1277 if (ctlr->cur_msg) {
1278 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1283 if (ctlr->idling) {
1284 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1285 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1290 if (list_empty(&ctlr->queue) || !ctlr->running) {
1291 if (!ctlr->busy) {
1292 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1298 kthread_queue_work(&ctlr->kworker,
1299 &ctlr->pump_messages);
1300 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1304 ctlr->busy = false;
1305 ctlr->idling = true;
1306 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1308 kfree(ctlr->dummy_rx);
1309 ctlr->dummy_rx = NULL;
1310 kfree(ctlr->dummy_tx);
1311 ctlr->dummy_tx = NULL;
1312 if (ctlr->unprepare_transfer_hardware &&
1313 ctlr->unprepare_transfer_hardware(ctlr))
1314 dev_err(&ctlr->dev,
1316 if (ctlr->auto_runtime_pm) {
1317 pm_runtime_mark_last_busy(ctlr->dev.parent);
1318 pm_runtime_put_autosuspend(ctlr->dev.parent);
1322 spin_lock_irqsave(&ctlr->queue_lock, flags);
1323 ctlr->idling = false;
1324 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1329 msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1330 ctlr->cur_msg = msg;
1332 list_del_init(&msg->queue);
1333 if (ctlr->busy)
1336 ctlr->busy = true;
1337 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1339 mutex_lock(&ctlr->io_mutex);
1341 if (!was_busy && ctlr->auto_runtime_pm) {
1342 ret = pm_runtime_get_sync(ctlr->dev.parent);
1344 pm_runtime_put_noidle(ctlr->dev.parent);
1345 dev_err(&ctlr->dev, "Failed to power device: %d\n",
1347 mutex_unlock(&ctlr->io_mutex);
1355 if (!was_busy && ctlr->prepare_transfer_hardware) {
1356 ret = ctlr->prepare_transfer_hardware(ctlr);
1358 dev_err(&ctlr->dev,
1362 if (ctlr->auto_runtime_pm)
1363 pm_runtime_put(ctlr->dev.parent);
1365 msg->status = ret;
1368 mutex_unlock(&ctlr->io_mutex);
1375 if (ctlr->prepare_message) {
1376 ret = ctlr->prepare_message(ctlr, msg);
1378 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1380 msg->status = ret;
1384 ctlr->cur_msg_prepared = true;
1389 msg->status = ret;
1394 ret = ctlr->transfer_one_message(ctlr, msg);
1396 dev_err(&ctlr->dev,
1402 mutex_unlock(&ctlr->io_mutex);
1410 * spi_pump_messages - kthread work function which processes spi message queue
1422 * spi_set_thread_rt - set the controller to pump at realtime priority
1426 * (by setting the ->rt value before calling spi_register_controller()) or
1440 dev_info(&ctlr->dev,
1442 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, ¶m);
1447 ctlr->running = false;
1448 ctlr->busy = false;
1450 kthread_init_worker(&ctlr->kworker);
1451 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1452 "%s", dev_name(&ctlr->dev));
1453 if (IS_ERR(ctlr->kworker_task)) {
1454 dev_err(&ctlr->dev, "failed to create message pump task\n");
1455 return PTR_ERR(ctlr->kworker_task);
1457 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
1466 if (ctlr->rt)
1473 * spi_get_next_queued_message() - called by driver to check for queued
1488 spin_lock_irqsave(&ctlr->queue_lock, flags);
1489 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
1491 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1498 * spi_finalize_current_message() - the current message is complete
1510 spin_lock_irqsave(&ctlr->queue_lock, flags);
1511 mesg = ctlr->cur_msg;
1512 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1516 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1517 ret = ctlr->unprepare_message(ctlr, mesg);
1519 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1524 spin_lock_irqsave(&ctlr->queue_lock, flags);
1525 ctlr->cur_msg = NULL;
1526 ctlr->cur_msg_prepared = false;
1527 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1528 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1532 mesg->state = NULL;
1533 if (mesg->complete)
1534 mesg->complete(mesg->context);
1542 spin_lock_irqsave(&ctlr->queue_lock, flags);
1544 if (ctlr->running || ctlr->busy) {
1545 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1546 return -EBUSY;
1549 ctlr->running = true;
1550 ctlr->cur_msg = NULL;
1551 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1553 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1564 spin_lock_irqsave(&ctlr->queue_lock, flags);
1568 * A wait_queue on the ctlr->busy could be used, but then the common
1570 * friends on every SPI message. Do this instead.
1572 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1573 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1575 spin_lock_irqsave(&ctlr->queue_lock, flags);
1578 if (!list_empty(&ctlr->queue) || ctlr->busy)
1579 ret = -EBUSY;
1581 ctlr->running = false;
1583 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1586 dev_warn(&ctlr->dev, "could not stop message queue\n");
1605 dev_err(&ctlr->dev, "problem destroying queue\n");
1609 kthread_flush_worker(&ctlr->kworker);
1610 kthread_stop(ctlr->kworker_task);
1615 static int __spi_queued_transfer(struct spi_device *spi,
1619 struct spi_controller *ctlr = spi->controller;
1622 spin_lock_irqsave(&ctlr->queue_lock, flags);
1624 if (!ctlr->running) {
1625 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1626 return -ESHUTDOWN;
1628 msg->actual_length = 0;
1629 msg->status = -EINPROGRESS;
1631 list_add_tail(&msg->queue, &ctlr->queue);
1632 if (!ctlr->busy && need_pump)
1633 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1635 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1640 * spi_queued_transfer - transfer function for queued transfers
1641 * @spi: spi device which is requesting transfer
1642 * @msg: spi message which is to handled is queued to driver queue
1646 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1648 return __spi_queued_transfer(spi, msg, true);
1655 ctlr->transfer = spi_queued_transfer;
1656 if (!ctlr->transfer_one_message)
1657 ctlr->transfer_one_message = spi_transfer_one_message;
1662 dev_err(&ctlr->dev, "problem initializing queue\n");
1665 ctlr->queued = true;
1668 dev_err(&ctlr->dev, "problem starting queue\n");
1681 * spi_flush_queue - Send all pending messages in the queue from the callers'
1686 * sent before doing something. Is used by the spi-mem code to make sure SPI
1687 * memory operations do not preempt regular SPI transfers that have been queued
1688 * before the spi-mem operation.
1692 if (ctlr->transfer == spi_queued_transfer)
1696 /*-------------------------------------------------------------------------*/
1699 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
1706 if (of_property_read_bool(nc, "spi-cpha"))
1707 spi->mode |= SPI_CPHA;
1708 if (of_property_read_bool(nc, "spi-cpol"))
1709 spi->mode |= SPI_CPOL;
1710 if (of_property_read_bool(nc, "spi-3wire"))
1711 spi->mode |= SPI_3WIRE;
1712 if (of_property_read_bool(nc, "spi-lsb-first"))
1713 spi->mode |= SPI_LSB_FIRST;
1720 if (ctlr->use_gpio_descriptors)
1721 spi->mode |= SPI_CS_HIGH;
1722 else if (of_property_read_bool(nc, "spi-cs-high"))
1723 spi->mode |= SPI_CS_HIGH;
1726 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1731 spi->mode |= SPI_TX_DUAL;
1734 spi->mode |= SPI_TX_QUAD;
1737 spi->mode |= SPI_TX_OCTAL;
1740 dev_warn(&ctlr->dev,
1741 "spi-tx-bus-width %d not supported\n",
1747 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1752 spi->mode |= SPI_RX_DUAL;
1755 spi->mode |= SPI_RX_QUAD;
1758 spi->mode |= SPI_RX_OCTAL;
1761 dev_warn(&ctlr->dev,
1762 "spi-rx-bus-width %d not supported\n",
1770 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1772 return -EINVAL;
1780 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1784 spi->chip_select = value;
1787 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1789 dev_err(&ctlr->dev,
1790 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
1793 spi->max_speed_hz = value;
1801 struct spi_device *spi;
1805 spi = spi_alloc_device(ctlr);
1806 if (!spi) {
1807 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
1808 rc = -ENOMEM;
1813 rc = of_modalias_node(nc, spi->modalias,
1814 sizeof(spi->modalias));
1816 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
1820 rc = of_spi_parse_dt(ctlr, spi, nc);
1826 spi->dev.of_node = nc;
1829 rc = spi_add_device(spi);
1831 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
1835 return spi;
1840 spi_dev_put(spi);
1845 * of_register_spi_devices() - Register child devices onto the SPI bus
1849 * represents a valid SPI slave.
1853 struct spi_device *spi;
1856 if (!ctlr->dev.of_node)
1859 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
1862 spi = of_register_spi_device(ctlr, nc);
1863 if (IS_ERR(spi)) {
1864 dev_warn(&ctlr->dev,
1865 "Failed to create SPI device for %pOF\n", nc);
1893 && obj->buffer.length >= 4)
1894 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
1897 && obj->buffer.length == 8)
1898 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
1901 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
1902 lookup->mode |= SPI_LSB_FIRST;
1905 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1906 lookup->mode |= SPI_CPOL;
1909 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1910 lookup->mode |= SPI_CPHA;
1916 struct spi_controller *ctlr = lookup->ctlr;
1918 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1923 sb = &ares->data.spi_serial_bus;
1924 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1927 sb->resource_source.string_ptr,
1931 ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
1932 return -ENODEV;
1938 * 0 .. max - 1 so we need to ask the driver to
1941 if (ctlr->fw_translate_cs) {
1942 int cs = ctlr->fw_translate_cs(ctlr,
1943 sb->device_selection);
1946 lookup->chip_select = cs;
1948 lookup->chip_select = sb->device_selection;
1951 lookup->max_speed_hz = sb->connection_speed;
1953 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1954 lookup->mode |= SPI_CPHA;
1955 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1956 lookup->mode |= SPI_CPOL;
1957 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1958 lookup->mode |= SPI_CS_HIGH;
1960 } else if (lookup->irq < 0) {
1964 lookup->irq = r.start;
1977 struct spi_device *spi;
1980 if (acpi_bus_get_status(adev) || !adev->status.present ||
1985 lookup.irq = -1;
1993 /* found SPI in _CRS but it points to another controller */
1997 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
1998 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
1999 /* Apple does not use _CRS but nested devices for SPI slaves */
2006 spi = spi_alloc_device(ctlr);
2007 if (!spi) {
2008 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
2009 dev_name(&adev->dev));
2013 ACPI_COMPANION_SET(&spi->dev, adev);
2014 spi->max_speed_hz = lookup.max_speed_hz;
2015 spi->mode = lookup.mode;
2016 spi->irq = lookup.irq;
2017 spi->bits_per_word = lookup.bits_per_word;
2018 spi->chip_select = lookup.chip_select;
2020 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
2021 sizeof(spi->modalias));
2023 if (spi->irq < 0)
2024 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
2028 adev->power.flags.ignore_parent = true;
2029 if (spi_add_device(spi)) {
2030 adev->power.flags.ignore_parent = false;
2031 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
2032 dev_name(&adev->dev));
2033 spi_dev_put(spi);
2058 handle = ACPI_HANDLE(ctlr->dev.parent);
2066 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
2089 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2091 * @spi: device used for the current transfer
2093 int spi_slave_abort(struct spi_device *spi)
2095 struct spi_controller *ctlr = spi->controller;
2097 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
2098 return ctlr->slave_abort(ctlr);
2100 return -ENOTSUPP;
2116 child = device_find_child(&ctlr->dev, NULL, match_true);
2118 child ? to_spi_device(child)->modalias : NULL);
2126 struct spi_device *spi;
2133 return -EINVAL;
2135 child = device_find_child(&ctlr->dev, NULL, match_true);
2144 spi = spi_alloc_device(ctlr);
2145 if (!spi)
2146 return -ENOMEM;
2148 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2150 rc = spi_add_device(spi);
2152 spi_dev_put(spi);
2188 * __spi_alloc_controller - allocate an SPI master or slave controller
2190 * @size: how much zeroed driver-private data to allocate; the pointer to this
2195 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2199 * This call is used only by SPI controller drivers, which are the
2210 * Return: the SPI controller structure on success, else NULL.
2225 device_initialize(&ctlr->dev);
2226 ctlr->bus_num = -1;
2227 ctlr->num_chipselect = 1;
2228 ctlr->slave = slave;
2230 ctlr->dev.class = &spi_slave_class;
2232 ctlr->dev.class = &spi_master_class;
2233 ctlr->dev.parent = dev;
2234 pm_suspend_ignore_children(&ctlr->dev, true);
2245 struct device_node *np = ctlr->dev.of_node;
2250 nb = of_gpio_named_count(np, "cs-gpios");
2251 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2253 /* Return error only for an incorrectly formed cs-gpios property */
2254 if (nb == 0 || nb == -ENOENT)
2259 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
2261 ctlr->cs_gpios = cs;
2263 if (!ctlr->cs_gpios)
2264 return -ENOMEM;
2266 for (i = 0; i < ctlr->num_chipselect; i++)
2267 cs[i] = -ENOENT;
2270 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2282 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2283 * @ctlr: The SPI master to grab GPIO descriptors for
2289 struct device *dev = &ctlr->dev;
2292 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2295 if (nb == 0 || nb == -ENOENT)
2300 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2303 return -ENOMEM;
2304 ctlr->cs_gpiods = cs;
2329 return -ENOMEM;
2340 * The controller may implement only the high-level SPI-memory like
2341 * operations if it does not support regular SPI transfers, and this is
2343 * If ->mem_ops is NULL, we request that at least one of the
2344 * ->transfer_xxx() method be implemented.
2346 if (ctlr->mem_ops) {
2347 if (!ctlr->mem_ops->exec_op)
2348 return -EINVAL;
2349 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2350 !ctlr->transfer_one_message) {
2351 return -EINVAL;
2358 * spi_register_controller - register SPI master or slave controller
2363 * SPI controllers connect to their drivers using some non-SPI bus,
2365 * includes calling spi_register_controller() to hook up to this SPI bus glue.
2367 * SPI controllers use board specific (often SOC specific) bus numbers,
2368 * and board-specific addressing for SPI devices combines those numbers
2369 * with chip select numbers. Since SPI does not directly support dynamic
2382 struct device *dev = ctlr->dev.parent;
2388 return -ENODEV;
2392 * the SPI controller.
2398 if (ctlr->bus_num >= 0) {
2399 /* devices with a fixed bus num must check-in with the num */
2401 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2402 ctlr->bus_num + 1, GFP_KERNEL);
2405 return id == -ENOSPC ? -EBUSY : id;
2406 ctlr->bus_num = id;
2407 } else if (ctlr->dev.of_node) {
2409 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2411 ctlr->bus_num = id;
2413 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2414 ctlr->bus_num + 1, GFP_KERNEL);
2417 return id == -ENOSPC ? -EBUSY : id;
2420 if (ctlr->bus_num < 0) {
2421 first_dynamic = of_alias_get_highest_id("spi");
2433 ctlr->bus_num = id;
2435 INIT_LIST_HEAD(&ctlr->queue);
2436 spin_lock_init(&ctlr->queue_lock);
2437 spin_lock_init(&ctlr->bus_lock_spinlock);
2438 mutex_init(&ctlr->bus_lock_mutex);
2439 mutex_init(&ctlr->io_mutex);
2440 ctlr->bus_lock_flag = 0;
2441 init_completion(&ctlr->xfer_completion);
2442 if (!ctlr->max_dma_len)
2443 ctlr->max_dma_len = INT_MAX;
2448 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2451 if (ctlr->use_gpio_descriptors) {
2459 ctlr->mode_bits |= SPI_CS_HIGH;
2469 * Even if it's just one always-selected device, there must
2472 if (!ctlr->num_chipselect)
2473 return -EINVAL;
2475 status = device_add(&ctlr->dev);
2479 idr_remove(&spi_master_idr, ctlr->bus_num);
2485 dev_name(&ctlr->dev));
2489 * need the queueing logic if the driver is only supporting high-level
2492 if (ctlr->transfer) {
2494 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
2497 device_del(&ctlr->dev);
2500 idr_remove(&spi_master_idr, ctlr->bus_num);
2506 spin_lock_init(&ctlr->statistics.lock);
2509 list_add_tail(&ctlr->list, &spi_controller_list);
2511 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
2528 * devm_spi_register_controller - register managed SPI master or slave
2530 * @dev: device managing SPI controller
2535 * Register a SPI device as with spi_register_controller() which will
2548 return -ENOMEM;
2569 * spi_unregister_controller - unregister SPI master or slave controller
2573 * This call is used only by SPI controller drivers, which are the
2583 int id = ctlr->bus_num;
2585 /* First make sure that this controller was ever added */
2589 if (ctlr->queued) {
2591 dev_err(&ctlr->dev, "queue remove failed\n");
2594 list_del(&ctlr->list);
2597 device_for_each_child(&ctlr->dev, NULL, __unregister);
2598 device_unregister(&ctlr->dev);
2611 /* Basically no-ops for non-queued controllers */
2612 if (!ctlr->queued)
2617 dev_err(&ctlr->dev, "queue stop failed\n");
2627 if (!ctlr->queued)
2632 dev_err(&ctlr->dev, "queue restart failed\n");
2644 return ctlr->bus_num == *bus_num;
2648 * spi_busnum_to_master - look up master associated with bus_num
2657 * Return: the SPI master structure on success, else NULL.
2673 /*-------------------------------------------------------------------------*/
2675 /* Core methods for SPI resource management */
2678 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2681 * @spi: the spi device for which we allocate memory
2691 void *spi_res_alloc(struct spi_device *spi,
2701 INIT_LIST_HEAD(&sres->entry);
2702 sres->release = release;
2704 return sres->data;
2709 * spi_res_free - free an spi resource
2720 WARN_ON(!list_empty(&sres->entry));
2726 * spi_res_add - add a spi_res to the spi_message
2727 * @message: the spi message
2734 WARN_ON(!list_empty(&sres->entry));
2735 list_add_tail(&sres->entry, &message->resources);
2740 * spi_res_release - release all spi resources for this message
2748 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
2749 if (res->release)
2750 res->release(ctlr, message, res->data);
2752 list_del(&res->entry);
2759 /*-------------------------------------------------------------------------*/
2771 if (rxfer->release)
2772 rxfer->release(ctlr, msg, res);
2775 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2778 for (i = 0; i < rxfer->inserted; i++)
2779 list_del(&rxfer->inserted_transfers[i].transfer_list);
2783 * spi_replace_transfers - replace transfers with several transfers
2786 * @xfer_first: the first spi_transfer we want to replace
2811 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2816 return ERR_PTR(-ENOMEM);
2819 rxfer->release = release;
2823 rxfer->extradata =
2824 &rxfer->inserted_transfers[insert];
2827 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2830 * the @replaced_transfers - it may be spi_message.messages!
2832 rxfer->replaced_after = xfer_first->transfer_list.prev;
2836 /* if the entry after replaced_after it is msg->transfers
2840 if (rxfer->replaced_after->next == &msg->transfers) {
2841 dev_err(&msg->spi->dev,
2844 list_splice(&rxfer->replaced_transfers,
2845 rxfer->replaced_after);
2851 return ERR_PTR(-EINVAL);
2857 list_move_tail(rxfer->replaced_after->next,
2858 &rxfer->replaced_transfers);
2862 * based on the first transfer to get removed
2866 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2872 list_add(&xfer->transfer_list, rxfer->replaced_after);
2876 xfer->cs_change = false;
2877 xfer->delay_usecs = 0;
2882 rxfer->inserted = insert;
2903 count = DIV_ROUND_UP(xfer->len, maxsize);
2909 xfers = srt->inserted_transfers;
2921 /* the first transfer just needs the length modified, so we
2939 xfers[i].len = min(maxsize, xfers[i].len - offset);
2945 *xferp = &xfers[count - 1];
2948 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
2950 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2957 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2981 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2982 if (xfer->len > maxsize) {
2994 /*-------------------------------------------------------------------------*/
2996 /* Core methods for SPI controller protocol drivers. Some of the
3003 if (ctlr->bits_per_word_mask) {
3006 return -EINVAL;
3007 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
3008 return -EINVAL;
3015 * spi_setup - setup SPI mode and clock rate
3016 * @spi: the device whose settings are being modified
3019 * SPI protocol drivers may need to update the transfer mode if the
3025 * or from it. When this function returns, the spi device is deselected.
3030 * LSB-first wire encoding, or active-high chipselects.
3034 int spi_setup(struct spi_device *spi)
3041 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
3042 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
3043 dev_err(&spi->dev,
3045 return -EINVAL;
3049 if ((spi->mode & SPI_3WIRE) && (spi->mode &
3052 return -EINVAL;
3058 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
3059 /* nothing prevents from working with active-high CS in case if it
3062 if (gpio_is_valid(spi->cs_gpio))
3068 dev_warn(&spi->dev,
3071 spi->mode &= ~ugly_bits;
3075 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
3077 return -EINVAL;
3080 if (!spi->bits_per_word)
3081 spi->bits_per_word = 8;
3083 status = __spi_validate_bits_per_word(spi->controller,
3084 spi->bits_per_word);
3088 if (!spi->max_speed_hz)
3089 spi->max_speed_hz = spi->controller->max_speed_hz;
3091 if (spi->controller->setup)
3092 status = spi->controller->setup(spi);
3094 spi_set_cs(spi, false);
3096 if (spi->rt && !spi->controller->rt) {
3097 spi->controller->rt = true;
3098 spi_set_thread_rt(spi->controller);
3101 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
3102 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
3103 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
3104 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
3105 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
3106 (spi->mode & SPI_LOOP) ? "loopback, " : "",
3107 spi->bits_per_word, spi->max_speed_hz,
3115 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3116 * @spi: the device that requires specific CS timing configuration
3121 void spi_set_cs_timing(struct spi_device *spi, u8 setup, u8 hold,
3124 if (spi->controller->set_cs_timing)
3125 spi->controller->set_cs_timing(spi, setup, hold, inactive_dly);
3129 static int __spi_validate(struct spi_device *spi, struct spi_message *message)
3131 struct spi_controller *ctlr = spi->controller;
3135 if (list_empty(&message->transfers))
3136 return -EINVAL;
3138 /* If an SPI controller does not support toggling the CS line on each
3140 * for the CS line, we can emulate the CS-per-word hardware function by
3141 * splitting transfers into one-word transfers and ensuring that
3144 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
3145 spi->cs_gpiod ||
3146 gpio_is_valid(spi->cs_gpio))) {
3150 maxsize = (spi->bits_per_word + 7) / 8;
3152 /* spi_split_transfers_maxsize() requires message->spi */
3153 message->spi = spi;
3160 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3162 if (list_is_last(&xfer->transfer_list, &message->transfers))
3164 xfer->cs_change = 1;
3168 /* Half-duplex links include original MicroWire, and ones with
3173 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3174 (spi->mode & SPI_3WIRE)) {
3175 unsigned flags = ctlr->flags;
3177 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3178 if (xfer->rx_buf && xfer->tx_buf)
3179 return -EINVAL;
3180 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
3181 return -EINVAL;
3182 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
3183 return -EINVAL;
3188 * Set transfer bits_per_word and max speed as spi device default if
3195 message->frame_length = 0;
3196 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3197 xfer->effective_speed_hz = 0;
3198 message->frame_length += xfer->len;
3199 if (!xfer->bits_per_word)
3200 xfer->bits_per_word = spi->bits_per_word;
3202 if (!xfer->speed_hz)
3203 xfer->speed_hz = spi->max_speed_hz;
3205 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3206 xfer->speed_hz = ctlr->max_speed_hz;
3208 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
3209 return -EINVAL;
3212 * SPI transfer length should be multiple of SPI word size
3213 * where SPI word size should be power-of-two multiple
3215 if (xfer->bits_per_word <= 8)
3217 else if (xfer->bits_per_word <= 16)
3223 if (xfer->len % w_size)
3224 return -EINVAL;
3226 if (xfer->speed_hz && ctlr->min_speed_hz &&
3227 xfer->speed_hz < ctlr->min_speed_hz)
3228 return -EINVAL;
3230 if (xfer->tx_buf && !xfer->tx_nbits)
3231 xfer->tx_nbits = SPI_NBITS_SINGLE;
3232 if (xfer->rx_buf && !xfer->rx_nbits)
3233 xfer->rx_nbits = SPI_NBITS_SINGLE;
3238 if (xfer->tx_buf) {
3239 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3240 xfer->tx_nbits != SPI_NBITS_DUAL &&
3241 xfer->tx_nbits != SPI_NBITS_QUAD)
3242 return -EINVAL;
3243 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3244 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3245 return -EINVAL;
3246 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3247 !(spi->mode & SPI_TX_QUAD))
3248 return -EINVAL;
3251 if (xfer->rx_buf) {
3252 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3253 xfer->rx_nbits != SPI_NBITS_DUAL &&
3254 xfer->rx_nbits != SPI_NBITS_QUAD)
3255 return -EINVAL;
3256 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3257 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3258 return -EINVAL;
3259 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3260 !(spi->mode & SPI_RX_QUAD))
3261 return -EINVAL;
3264 if (xfer->word_delay_usecs < spi->word_delay_usecs)
3265 xfer->word_delay_usecs = spi->word_delay_usecs;
3268 message->status = -EINPROGRESS;
3273 static int __spi_async(struct spi_device *spi, struct spi_message *message)
3275 struct spi_controller *ctlr = spi->controller;
3278 * Some controllers do not support doing regular SPI transfers. Return
3281 if (!ctlr->transfer)
3282 return -ENOTSUPP;
3284 message->spi = spi;
3286 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
3287 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3291 return ctlr->transfer(spi, message);
3295 * spi_async - asynchronous SPI transfer
3296 * @spi: device with which data will be exchanged
3304 * Before that invocation, the value of message->status is undefined.
3305 * When the callback is issued, message->status holds either zero (to
3308 * deallocate the associated memory; it's no longer in use by any SPI
3325 int spi_async(struct spi_device *spi, struct spi_message *message)
3327 struct spi_controller *ctlr = spi->controller;
3331 ret = __spi_validate(spi, message);
3335 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3337 if (ctlr->bus_lock_flag)
3338 ret = -EBUSY;
3340 ret = __spi_async(spi, message);
3342 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3349 * spi_async_locked - version of spi_async with exclusive bus usage
3350 * @spi: device with which data will be exchanged
3358 * Before that invocation, the value of message->status is undefined.
3359 * When the callback is issued, message->status holds either zero (to
3362 * deallocate the associated memory; it's no longer in use by any SPI
3379 int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3381 struct spi_controller *ctlr = spi->controller;
3385 ret = __spi_validate(spi, message);
3389 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3391 ret = __spi_async(spi, message);
3393 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3400 /*-------------------------------------------------------------------------*/
3402 /* Utility methods for SPI protocol drivers, layered on
3412 static int __spi_sync(struct spi_device *spi, struct spi_message *message)
3416 struct spi_controller *ctlr = spi->controller;
3419 status = __spi_validate(spi, message);
3423 message->complete = spi_complete;
3424 message->context = &done;
3425 message->spi = spi;
3427 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
3428 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3435 if (ctlr->transfer == spi_queued_transfer) {
3436 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3440 status = __spi_queued_transfer(spi, message, false);
3442 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3444 status = spi_async_locked(spi, message);
3451 if (ctlr->transfer == spi_queued_transfer) {
3452 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
3454 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3460 status = message->status;
3462 message->context = NULL;
3467 * spi_sync - blocking/synchronous SPI data transfers
3468 * @spi: device with which data will be exchanged
3473 * is non-interruptible, and has no timeout. Low-overhead controller
3476 * Note that the SPI device's chip select is active during the message,
3478 * frequently-used devices may want to minimize costs of selecting a chip,
3487 int spi_sync(struct spi_device *spi, struct spi_message *message)
3491 mutex_lock(&spi->controller->bus_lock_mutex);
3492 ret = __spi_sync(spi, message);
3493 mutex_unlock(&spi->controller->bus_lock_mutex);
3500 * spi_sync_locked - version of spi_sync with exclusive bus usage
3501 * @spi: device with which data will be exchanged
3506 * is non-interruptible, and has no timeout. Low-overhead controller
3510 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
3515 int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3517 return __spi_sync(spi, message);
3522 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
3523 * @ctlr: SPI bus master that should be locked for exclusive bus access
3527 * is non-interruptible, and has no timeout.
3530 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3532 * and spi_async_locked calls when the SPI bus lock is held.
3540 mutex_lock(&ctlr->bus_lock_mutex);
3542 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3543 ctlr->bus_lock_flag = 1;
3544 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
3553 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3554 * @ctlr: SPI bus master that was locked for exclusive bus access
3558 * is non-interruptible, and has no timeout.
3560 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3567 ctlr->bus_lock_flag = 0;
3569 mutex_unlock(&ctlr->bus_lock_mutex);
3581 * spi_write_then_read - SPI synchronous write followed by read
3582 * @spi: device with which data will be exchanged
3583 * @txbuf: data to be written (need not be dma-safe)
3585 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3596 * Performance-sensitive or bulk transfer code should instead use
3597 * spi_{async,sync}() calls with dma-safe buffers.
3601 int spi_write_then_read(struct spi_device *spi,
3612 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3615 * using the pre-allocated buffer or the transfer is too large.
3621 return -ENOMEM;
3642 status = spi_sync(spi, &message);
3655 /*-------------------------------------------------------------------------*/
3669 /* the spi controllers are not using spi_bus, so we find it with another way */
3689 struct spi_device *spi;
3693 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3697 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
3698 put_device(&ctlr->dev);
3702 spi = of_register_spi_device(ctlr, rd->dn);
3703 put_device(&ctlr->dev);
3705 if (IS_ERR(spi)) {
3707 __func__, rd->dn);
3708 of_node_clear_flag(rd->dn, OF_POPULATED);
3709 return notifier_from_errno(PTR_ERR(spi));
3715 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3719 spi = of_find_spi_device_by_node(rd->dn);
3720 if (spi == NULL)
3724 spi_unregister_device(spi);
3727 put_device(&spi->dev);
3744 return ACPI_COMPANION(dev->parent) == data;
3775 struct spi_device *spi;
3779 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3784 put_device(&ctlr->dev);
3790 spi = acpi_spi_find_device_by_adev(adev);
3791 if (!spi)
3794 spi_unregister_device(spi);
3795 put_device(&spi->dev);
3815 status = -ENOMEM;