hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/spi/spi.c
....@@ -1,10 +1,8 @@
11 // SPDX-License-Identifier: GPL-2.0-or-later
2
-/*
3
- * SPI init/core code
4
- *
5
- * Copyright (C) 2005 David Brownell
6
- * Copyright (C) 2008 Secret Lab Technologies Ltd.
7
- */
2
+// SPI init/core code
3
+//
4
+// Copyright (C) 2005 David Brownell
5
+// Copyright (C) 2008 Secret Lab Technologies Ltd.
86
97 #include <linux/kernel.h>
108 #include <linux/device.h>
....@@ -21,6 +19,7 @@
2119 #include <linux/spi/spi.h>
2220 #include <linux/spi/spi-mem.h>
2321 #include <linux/of_gpio.h>
22
+#include <linux/gpio/consumer.h>
2423 #include <linux/pm_runtime.h>
2524 #include <linux/pm_domain.h>
2625 #include <linux/property.h>
....@@ -37,6 +36,8 @@
3736
3837 #define CREATE_TRACE_POINTS
3938 #include <trace/events/spi.h>
39
+EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
40
+EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
4041
4142 #include "internals.h"
4243
....@@ -45,10 +46,6 @@
4546 static void spidev_release(struct device *dev)
4647 {
4748 struct spi_device *spi = to_spi_device(dev);
48
-
49
- /* spi controllers may cleanup for released devices */
50
- if (spi->controller->cleanup)
51
- spi->controller->cleanup(spi);
5249
5350 spi_controller_put(spi->controller);
5451 kfree(spi->driver_override);
....@@ -91,7 +88,7 @@
9188 if (len) {
9289 spi->driver_override = driver_override;
9390 } else {
94
- /* Emptry string, disable driver override */
91
+ /* Empty string, disable driver override */
9592 spi->driver_override = NULL;
9693 kfree(driver_override);
9794 }
....@@ -469,7 +466,7 @@
469466 static LIST_HEAD(spi_controller_list);
470467
471468 /*
472
- * Used to protect add/del opertion for board_info list and
469
+ * Used to protect add/del operation for board_info list and
473470 * spi_controller list, and their matching process
474471 * also used to protect object of type struct idr
475472 */
....@@ -516,6 +513,7 @@
516513 spi->dev.bus = &spi_bus_type;
517514 spi->dev.release = spidev_release;
518515 spi->cs_gpio = -ENOENT;
516
+ spi->mode = ctlr->buswidth_override_bits;
519517
520518 spin_lock_init(&spi->statistics.lock);
521519
....@@ -546,6 +544,12 @@
546544 spi->chip_select == new_spi->chip_select)
547545 return -EBUSY;
548546 return 0;
547
+}
548
+
549
+static void spi_cleanup(struct spi_device *spi)
550
+{
551
+ if (spi->controller->cleanup)
552
+ spi->controller->cleanup(spi);
549553 }
550554
551555 /**
....@@ -593,7 +597,10 @@
593597 goto done;
594598 }
595599
596
- if (ctlr->cs_gpios)
600
+ /* Descriptors take precedence */
601
+ if (ctlr->cs_gpiods)
602
+ spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
603
+ else if (ctlr->cs_gpios)
597604 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
598605
599606 /* Drivers may modify this initial i/o setup, but will
....@@ -609,11 +616,13 @@
609616
610617 /* Device may be bound to an active driver when this returns */
611618 status = device_add(&spi->dev);
612
- if (status < 0)
619
+ if (status < 0) {
613620 dev_err(dev, "can't add %s, status %d\n",
614621 dev_name(&spi->dev), status);
615
- else
622
+ spi_cleanup(spi);
623
+ } else {
616624 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
625
+ }
617626
618627 done:
619628 mutex_unlock(&spi_add_lock);
....@@ -706,7 +715,9 @@
706715 }
707716 if (ACPI_COMPANION(&spi->dev))
708717 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
709
- device_unregister(&spi->dev);
718
+ device_del(&spi->dev);
719
+ spi_cleanup(spi);
720
+ put_device(&spi->dev);
710721 }
711722 EXPORT_SYMBOL_GPL(spi_unregister_device);
712723
....@@ -782,21 +793,68 @@
782793
783794 /*-------------------------------------------------------------------------*/
784795
785
-static void spi_set_cs(struct spi_device *spi, bool enable)
796
+static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
786797 {
798
+ bool enable1 = enable;
799
+
800
+ /*
801
+ * Avoid calling into the driver (or doing delays) if the chip select
802
+ * isn't actually changing from the last time this was called.
803
+ */
804
+ if (!force && (spi->controller->last_cs_enable == enable) &&
805
+ (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
806
+ return;
807
+
808
+ spi->controller->last_cs_enable = enable;
809
+ spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
810
+
811
+ if (!spi->controller->set_cs_timing) {
812
+ if (enable1)
813
+ spi_delay_exec(&spi->controller->cs_setup, NULL);
814
+ else
815
+ spi_delay_exec(&spi->controller->cs_hold, NULL);
816
+ }
817
+
787818 if (spi->mode & SPI_CS_HIGH)
788819 enable = !enable;
789820
790
- if (gpio_is_valid(spi->cs_gpio)) {
791
- /* Honour the SPI_NO_CS flag */
792
- if (!(spi->mode & SPI_NO_CS))
793
- gpio_set_value(spi->cs_gpio, !enable);
821
+ if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
822
+ if (!(spi->mode & SPI_NO_CS)) {
823
+ if (spi->cs_gpiod) {
824
+ /*
825
+ * Historically ACPI has no means of the GPIO polarity and
826
+ * thus the SPISerialBus() resource defines it on the per-chip
827
+ * basis. In order to avoid a chain of negations, the GPIO
828
+ * polarity is considered being Active High. Even for the cases
829
+ * when _DSD() is involved (in the updated versions of ACPI)
830
+ * the GPIO CS polarity must be defined Active High to avoid
831
+ * ambiguity. That's why we use enable, that takes SPI_CS_HIGH
832
+ * into account.
833
+ */
834
+ if (has_acpi_companion(&spi->dev))
835
+ gpiod_set_value_cansleep(spi->cs_gpiod, !enable);
836
+ else
837
+ /* Polarity handled by GPIO library */
838
+ gpiod_set_value_cansleep(spi->cs_gpiod, enable1);
839
+ } else {
840
+ /*
841
+ * invert the enable line, as active low is
842
+ * default for SPI.
843
+ */
844
+ gpio_set_value_cansleep(spi->cs_gpio, !enable);
845
+ }
846
+ }
794847 /* Some SPI masters need both GPIO CS & slave_select */
795848 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
796849 spi->controller->set_cs)
797850 spi->controller->set_cs(spi, !enable);
798851 } else if (spi->controller->set_cs) {
799852 spi->controller->set_cs(spi, !enable);
853
+ }
854
+
855
+ if (!spi->controller->set_cs_timing) {
856
+ if (!enable1)
857
+ spi_delay_exec(&spi->controller->cs_inactive, NULL);
800858 }
801859 }
802860
....@@ -823,10 +881,10 @@
823881 int i, ret;
824882
825883 if (vmalloced_buf || kmap_buf) {
826
- desc_len = min_t(int, max_seg_size, PAGE_SIZE);
884
+ desc_len = min_t(unsigned long, max_seg_size, PAGE_SIZE);
827885 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
828886 } else if (virt_addr_valid(buf)) {
829
- desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
887
+ desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len);
830888 sgs = DIV_ROUND_UP(len, desc_len);
831889 } else {
832890 return -EINVAL;
....@@ -888,6 +946,8 @@
888946 if (sgt->orig_nents) {
889947 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
890948 sg_free_table(sgt);
949
+ sgt->orig_nents = 0;
950
+ sgt->nents = 0;
891951 }
892952 }
893953
....@@ -965,6 +1025,8 @@
9651025 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
9661026 }
9671027
1028
+ ctlr->cur_msg_mapped = false;
1029
+
9681030 return 0;
9691031 }
9701032 #else /* !CONFIG_HAS_DMA */
....@@ -1006,7 +1068,8 @@
10061068 void *tmp;
10071069 unsigned int max_tx, max_rx;
10081070
1009
- if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
1071
+ if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX))
1072
+ && !(msg->spi->mode & SPI_3WIRE)) {
10101073 max_tx = 0;
10111074 max_rx = 0;
10121075
....@@ -1058,7 +1121,8 @@
10581121 {
10591122 struct spi_statistics *statm = &ctlr->statistics;
10601123 struct spi_statistics *stats = &msg->spi->statistics;
1061
- unsigned long long ms = 1;
1124
+ u32 speed_hz = xfer->speed_hz;
1125
+ unsigned long long ms;
10621126
10631127 if (spi_controller_is_slave(ctlr)) {
10641128 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
....@@ -1066,8 +1130,11 @@
10661130 return -EINTR;
10671131 }
10681132 } else {
1133
+ if (!speed_hz)
1134
+ speed_hz = 100000;
1135
+
10691136 ms = 8LL * 1000LL * xfer->len;
1070
- do_div(ms, xfer->speed_hz);
1137
+ do_div(ms, speed_hz);
10711138 ms += ms + 200; /* some tolerance */
10721139
10731140 if (ms > UINT_MAX)
....@@ -1088,6 +1155,99 @@
10881155 return 0;
10891156 }
10901157
1158
+static void _spi_transfer_delay_ns(u32 ns)
1159
+{
1160
+ if (!ns)
1161
+ return;
1162
+ if (ns <= 1000) {
1163
+ ndelay(ns);
1164
+ } else {
1165
+ u32 us = DIV_ROUND_UP(ns, 1000);
1166
+
1167
+ if (us <= 10)
1168
+ udelay(us);
1169
+ else
1170
+ usleep_range(us, us + DIV_ROUND_UP(us, 10));
1171
+ }
1172
+}
1173
+
1174
+int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
1175
+{
1176
+ u32 delay = _delay->value;
1177
+ u32 unit = _delay->unit;
1178
+ u32 hz;
1179
+
1180
+ if (!delay)
1181
+ return 0;
1182
+
1183
+ switch (unit) {
1184
+ case SPI_DELAY_UNIT_USECS:
1185
+ delay *= 1000;
1186
+ break;
1187
+ case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
1188
+ break;
1189
+ case SPI_DELAY_UNIT_SCK:
1190
+ /* clock cycles need to be obtained from spi_transfer */
1191
+ if (!xfer)
1192
+ return -EINVAL;
1193
+ /* if there is no effective speed know, then approximate
1194
+ * by underestimating with half the requested hz
1195
+ */
1196
+ hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1197
+ if (!hz)
1198
+ return -EINVAL;
1199
+ delay *= DIV_ROUND_UP(1000000000, hz);
1200
+ break;
1201
+ default:
1202
+ return -EINVAL;
1203
+ }
1204
+
1205
+ return delay;
1206
+}
1207
+EXPORT_SYMBOL_GPL(spi_delay_to_ns);
1208
+
1209
+int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1210
+{
1211
+ int delay;
1212
+
1213
+ might_sleep();
1214
+
1215
+ if (!_delay)
1216
+ return -EINVAL;
1217
+
1218
+ delay = spi_delay_to_ns(_delay, xfer);
1219
+ if (delay < 0)
1220
+ return delay;
1221
+
1222
+ _spi_transfer_delay_ns(delay);
1223
+
1224
+ return 0;
1225
+}
1226
+EXPORT_SYMBOL_GPL(spi_delay_exec);
1227
+
1228
+static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1229
+ struct spi_transfer *xfer)
1230
+{
1231
+ u32 delay = xfer->cs_change_delay.value;
1232
+ u32 unit = xfer->cs_change_delay.unit;
1233
+ int ret;
1234
+
1235
+ /* return early on "fast" mode - for everything but USECS */
1236
+ if (!delay) {
1237
+ if (unit == SPI_DELAY_UNIT_USECS)
1238
+ _spi_transfer_delay_ns(10000);
1239
+ return;
1240
+ }
1241
+
1242
+ ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1243
+ if (ret) {
1244
+ dev_err_once(&msg->spi->dev,
1245
+ "Use of unsupported delay unit %i, using default of 10us\n",
1246
+ unit);
1247
+ _spi_transfer_delay_ns(10000);
1248
+ }
1249
+}
1250
+
10911251 /*
10921252 * spi_transfer_one_message - Default implementation of transfer_one_message()
10931253 *
....@@ -1104,7 +1264,7 @@
11041264 struct spi_statistics *statm = &ctlr->statistics;
11051265 struct spi_statistics *stats = &msg->spi->statistics;
11061266
1107
- spi_set_cs(msg->spi, true);
1267
+ spi_set_cs(msg->spi, true, false);
11081268
11091269 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
11101270 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
....@@ -1115,11 +1275,25 @@
11151275 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
11161276 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
11171277
1118
- if (xfer->tx_buf || xfer->rx_buf) {
1278
+ if (!ctlr->ptp_sts_supported) {
1279
+ xfer->ptp_sts_word_pre = 0;
1280
+ ptp_read_system_prets(xfer->ptp_sts);
1281
+ }
1282
+
1283
+ if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
11191284 reinit_completion(&ctlr->xfer_completion);
11201285
1286
+fallback_pio:
11211287 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
11221288 if (ret < 0) {
1289
+ if (ctlr->cur_msg_mapped &&
1290
+ (xfer->error & SPI_TRANS_FAIL_NO_START)) {
1291
+ __spi_unmap_msg(ctlr, msg);
1292
+ ctlr->fallback = true;
1293
+ xfer->error &= ~SPI_TRANS_FAIL_NO_START;
1294
+ goto fallback_pio;
1295
+ }
1296
+
11231297 SPI_STATISTICS_INCREMENT_FIELD(statm,
11241298 errors);
11251299 SPI_STATISTICS_INCREMENT_FIELD(stats,
....@@ -1141,28 +1315,26 @@
11411315 xfer->len);
11421316 }
11431317
1318
+ if (!ctlr->ptp_sts_supported) {
1319
+ ptp_read_system_postts(xfer->ptp_sts);
1320
+ xfer->ptp_sts_word_post = xfer->len;
1321
+ }
1322
+
11441323 trace_spi_transfer_stop(msg, xfer);
11451324
11461325 if (msg->status != -EINPROGRESS)
11471326 goto out;
11481327
1149
- if (xfer->delay_usecs) {
1150
- u16 us = xfer->delay_usecs;
1151
-
1152
- if (us <= 10)
1153
- udelay(us);
1154
- else
1155
- usleep_range(us, us + DIV_ROUND_UP(us, 10));
1156
- }
1328
+ spi_transfer_delay_exec(xfer);
11571329
11581330 if (xfer->cs_change) {
11591331 if (list_is_last(&xfer->transfer_list,
11601332 &msg->transfers)) {
11611333 keep_cs = true;
11621334 } else {
1163
- spi_set_cs(msg->spi, false);
1164
- udelay(10);
1165
- spi_set_cs(msg->spi, true);
1335
+ spi_set_cs(msg->spi, false, false);
1336
+ _spi_transfer_cs_change_delay(msg, xfer);
1337
+ spi_set_cs(msg->spi, true, false);
11661338 }
11671339 }
11681340
....@@ -1171,7 +1343,7 @@
11711343
11721344 out:
11731345 if (ret != 0 || !keep_cs)
1174
- spi_set_cs(msg->spi, false);
1346
+ spi_set_cs(msg->spi, false, false);
11751347
11761348 if (msg->status == -EINPROGRESS)
11771349 msg->status = ret;
....@@ -1198,6 +1370,14 @@
11981370 }
11991371 EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
12001372
1373
+static void spi_idle_runtime_pm(struct spi_controller *ctlr)
1374
+{
1375
+ if (ctlr->auto_runtime_pm) {
1376
+ pm_runtime_mark_last_busy(ctlr->dev.parent);
1377
+ pm_runtime_put_autosuspend(ctlr->dev.parent);
1378
+ }
1379
+}
1380
+
12011381 /**
12021382 * __spi_pump_messages - function which processes spi message queue
12031383 * @ctlr: controller to process queue for
....@@ -1213,8 +1393,10 @@
12131393 */
12141394 static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
12151395 {
1216
- unsigned long flags;
1396
+ struct spi_transfer *xfer;
1397
+ struct spi_message *msg;
12171398 bool was_busy = false;
1399
+ unsigned long flags;
12181400 int ret;
12191401
12201402 /* Lock queue */
....@@ -1228,7 +1410,7 @@
12281410
12291411 /* If another context is idling the device then defer */
12301412 if (ctlr->idling) {
1231
- kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1413
+ kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
12321414 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
12331415 return;
12341416 }
....@@ -1240,10 +1422,17 @@
12401422 return;
12411423 }
12421424
1243
- /* Only do teardown in the thread */
1425
+ /* Defer any non-atomic teardown to the thread */
12441426 if (!in_kthread) {
1245
- kthread_queue_work(&ctlr->kworker,
1246
- &ctlr->pump_messages);
1427
+ if (!ctlr->dummy_rx && !ctlr->dummy_tx &&
1428
+ !ctlr->unprepare_transfer_hardware) {
1429
+ spi_idle_runtime_pm(ctlr);
1430
+ ctlr->busy = false;
1431
+ trace_spi_controller_idle(ctlr);
1432
+ } else {
1433
+ kthread_queue_work(ctlr->kworker,
1434
+ &ctlr->pump_messages);
1435
+ }
12471436 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
12481437 return;
12491438 }
....@@ -1260,10 +1449,7 @@
12601449 ctlr->unprepare_transfer_hardware(ctlr))
12611450 dev_err(&ctlr->dev,
12621451 "failed to unprepare transfer hardware\n");
1263
- if (ctlr->auto_runtime_pm) {
1264
- pm_runtime_mark_last_busy(ctlr->dev.parent);
1265
- pm_runtime_put_autosuspend(ctlr->dev.parent);
1266
- }
1452
+ spi_idle_runtime_pm(ctlr);
12671453 trace_spi_controller_idle(ctlr);
12681454
12691455 spin_lock_irqsave(&ctlr->queue_lock, flags);
....@@ -1273,10 +1459,10 @@
12731459 }
12741460
12751461 /* Extract head of queue */
1276
- ctlr->cur_msg =
1277
- list_first_entry(&ctlr->queue, struct spi_message, queue);
1462
+ msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1463
+ ctlr->cur_msg = msg;
12781464
1279
- list_del_init(&ctlr->cur_msg->queue);
1465
+ list_del_init(&msg->queue);
12801466 if (ctlr->busy)
12811467 was_busy = true;
12821468 else
....@@ -1303,37 +1489,49 @@
13031489 ret = ctlr->prepare_transfer_hardware(ctlr);
13041490 if (ret) {
13051491 dev_err(&ctlr->dev,
1306
- "failed to prepare transfer hardware\n");
1492
+ "failed to prepare transfer hardware: %d\n",
1493
+ ret);
13071494
13081495 if (ctlr->auto_runtime_pm)
13091496 pm_runtime_put(ctlr->dev.parent);
1497
+
1498
+ msg->status = ret;
1499
+ spi_finalize_current_message(ctlr);
1500
+
13101501 mutex_unlock(&ctlr->io_mutex);
13111502 return;
13121503 }
13131504 }
13141505
1315
- trace_spi_message_start(ctlr->cur_msg);
1506
+ trace_spi_message_start(msg);
13161507
13171508 if (ctlr->prepare_message) {
1318
- ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);
1509
+ ret = ctlr->prepare_message(ctlr, msg);
13191510 if (ret) {
13201511 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
13211512 ret);
1322
- ctlr->cur_msg->status = ret;
1513
+ msg->status = ret;
13231514 spi_finalize_current_message(ctlr);
13241515 goto out;
13251516 }
13261517 ctlr->cur_msg_prepared = true;
13271518 }
13281519
1329
- ret = spi_map_msg(ctlr, ctlr->cur_msg);
1520
+ ret = spi_map_msg(ctlr, msg);
13301521 if (ret) {
1331
- ctlr->cur_msg->status = ret;
1522
+ msg->status = ret;
13321523 spi_finalize_current_message(ctlr);
13331524 goto out;
13341525 }
13351526
1336
- ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg);
1527
+ if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1528
+ list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1529
+ xfer->ptp_sts_word_pre = 0;
1530
+ ptp_read_system_prets(xfer->ptp_sts);
1531
+ }
1532
+ }
1533
+
1534
+ ret = ctlr->transfer_one_message(ctlr, msg);
13371535 if (ret) {
13381536 dev_err(&ctlr->dev,
13391537 "failed to transfer one message from queue\n");
....@@ -1360,20 +1558,124 @@
13601558 __spi_pump_messages(ctlr, true);
13611559 }
13621560
1561
+/**
1562
+ * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1563
+ * TX timestamp for the requested byte from the SPI
1564
+ * transfer. The frequency with which this function
1565
+ * must be called (once per word, once for the whole
1566
+ * transfer, once per batch of words etc) is arbitrary
1567
+ * as long as the @tx buffer offset is greater than or
1568
+ * equal to the requested byte at the time of the
1569
+ * call. The timestamp is only taken once, at the
1570
+ * first such call. It is assumed that the driver
1571
+ * advances its @tx buffer pointer monotonically.
1572
+ * @ctlr: Pointer to the spi_controller structure of the driver
1573
+ * @xfer: Pointer to the transfer being timestamped
1574
+ * @progress: How many words (not bytes) have been transferred so far
1575
+ * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1576
+ * transfer, for less jitter in time measurement. Only compatible
1577
+ * with PIO drivers. If true, must follow up with
1578
+ * spi_take_timestamp_post or otherwise system will crash.
1579
+ * WARNING: for fully predictable results, the CPU frequency must
1580
+ * also be under control (governor).
1581
+ */
1582
+void spi_take_timestamp_pre(struct spi_controller *ctlr,
1583
+ struct spi_transfer *xfer,
1584
+ size_t progress, bool irqs_off)
1585
+{
1586
+ if (!xfer->ptp_sts)
1587
+ return;
1588
+
1589
+ if (xfer->timestamped)
1590
+ return;
1591
+
1592
+ if (progress > xfer->ptp_sts_word_pre)
1593
+ return;
1594
+
1595
+ /* Capture the resolution of the timestamp */
1596
+ xfer->ptp_sts_word_pre = progress;
1597
+
1598
+ if (irqs_off) {
1599
+ local_irq_save(ctlr->irq_flags);
1600
+ preempt_disable();
1601
+ }
1602
+
1603
+ ptp_read_system_prets(xfer->ptp_sts);
1604
+}
1605
+EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1606
+
1607
+/**
1608
+ * spi_take_timestamp_post - helper for drivers to collect the end of the
1609
+ * TX timestamp for the requested byte from the SPI
1610
+ * transfer. Can be called with an arbitrary
1611
+ * frequency: only the first call where @tx exceeds
1612
+ * or is equal to the requested word will be
1613
+ * timestamped.
1614
+ * @ctlr: Pointer to the spi_controller structure of the driver
1615
+ * @xfer: Pointer to the transfer being timestamped
1616
+ * @progress: How many words (not bytes) have been transferred so far
1617
+ * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1618
+ */
1619
+void spi_take_timestamp_post(struct spi_controller *ctlr,
1620
+ struct spi_transfer *xfer,
1621
+ size_t progress, bool irqs_off)
1622
+{
1623
+ if (!xfer->ptp_sts)
1624
+ return;
1625
+
1626
+ if (xfer->timestamped)
1627
+ return;
1628
+
1629
+ if (progress < xfer->ptp_sts_word_post)
1630
+ return;
1631
+
1632
+ ptp_read_system_postts(xfer->ptp_sts);
1633
+
1634
+ if (irqs_off) {
1635
+ local_irq_restore(ctlr->irq_flags);
1636
+ preempt_enable();
1637
+ }
1638
+
1639
+ /* Capture the resolution of the timestamp */
1640
+ xfer->ptp_sts_word_post = progress;
1641
+
1642
+ xfer->timestamped = true;
1643
+}
1644
+EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
1645
+
1646
+/**
1647
+ * spi_set_thread_rt - set the controller to pump at realtime priority
1648
+ * @ctlr: controller to boost priority of
1649
+ *
1650
+ * This can be called because the controller requested realtime priority
1651
+ * (by setting the ->rt value before calling spi_register_controller()) or
1652
+ * because a device on the bus said that its transfers needed realtime
1653
+ * priority.
1654
+ *
1655
+ * NOTE: at the moment if any device on a bus says it needs realtime then
1656
+ * the thread will be at realtime priority for all transfers on that
1657
+ * controller. If this eventually becomes a problem we may see if we can
1658
+ * find a way to boost the priority only temporarily during relevant
1659
+ * transfers.
1660
+ */
1661
+static void spi_set_thread_rt(struct spi_controller *ctlr)
1662
+{
1663
+ dev_info(&ctlr->dev,
1664
+ "will run message pump with realtime priority\n");
1665
+ sched_set_fifo(ctlr->kworker->task);
1666
+}
1667
+
13631668 static int spi_init_queue(struct spi_controller *ctlr)
13641669 {
1365
- struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1366
-
13671670 ctlr->running = false;
13681671 ctlr->busy = false;
13691672
1370
- kthread_init_worker(&ctlr->kworker);
1371
- ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1372
- "%s", dev_name(&ctlr->dev));
1373
- if (IS_ERR(ctlr->kworker_task)) {
1374
- dev_err(&ctlr->dev, "failed to create message pump task\n");
1375
- return PTR_ERR(ctlr->kworker_task);
1673
+ ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev));
1674
+ if (IS_ERR(ctlr->kworker)) {
1675
+ dev_err(&ctlr->dev, "failed to create message pump kworker\n");
1676
+ return PTR_ERR(ctlr->kworker);
13761677 }
1678
+
13771679 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
13781680
13791681 /*
....@@ -1383,11 +1685,8 @@
13831685 * request and the scheduling of the message pump thread. Without this
13841686 * setting the message pump thread will remain at default priority.
13851687 */
1386
- if (ctlr->rt) {
1387
- dev_info(&ctlr->dev,
1388
- "will run message pump with realtime priority\n");
1389
- sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
1390
- }
1688
+ if (ctlr->rt)
1689
+ spi_set_thread_rt(ctlr);
13911690
13921691 return 0;
13931692 }
....@@ -1426,6 +1725,7 @@
14261725 */
14271726 void spi_finalize_current_message(struct spi_controller *ctlr)
14281727 {
1728
+ struct spi_transfer *xfer;
14291729 struct spi_message *mesg;
14301730 unsigned long flags;
14311731 int ret;
....@@ -1433,6 +1733,17 @@
14331733 spin_lock_irqsave(&ctlr->queue_lock, flags);
14341734 mesg = ctlr->cur_msg;
14351735 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1736
+
1737
+ if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1738
+ list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
1739
+ ptp_read_system_postts(xfer->ptp_sts);
1740
+ xfer->ptp_sts_word_post = xfer->len;
1741
+ }
1742
+ }
1743
+
1744
+ if (unlikely(ctlr->ptp_sts_supported))
1745
+ list_for_each_entry(xfer, &mesg->transfers, transfer_list)
1746
+ WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped);
14361747
14371748 spi_unmap_msg(ctlr, mesg);
14381749
....@@ -1454,7 +1765,8 @@
14541765 spin_lock_irqsave(&ctlr->queue_lock, flags);
14551766 ctlr->cur_msg = NULL;
14561767 ctlr->cur_msg_prepared = false;
1457
- kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1768
+ ctlr->fallback = false;
1769
+ kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
14581770 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
14591771
14601772 trace_spi_message_done(mesg);
....@@ -1480,7 +1792,7 @@
14801792 ctlr->cur_msg = NULL;
14811793 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
14821794
1483
- kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1795
+ kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
14841796
14851797 return 0;
14861798 }
....@@ -1536,8 +1848,7 @@
15361848 return ret;
15371849 }
15381850
1539
- kthread_flush_worker(&ctlr->kworker);
1540
- kthread_stop(ctlr->kworker_task);
1851
+ kthread_destroy_worker(ctlr->kworker);
15411852
15421853 return 0;
15431854 }
....@@ -1560,7 +1871,7 @@
15601871
15611872 list_add_tail(&msg->queue, &ctlr->queue);
15621873 if (!ctlr->busy && need_pump)
1563
- kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1874
+ kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
15641875
15651876 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
15661877 return 0;
....@@ -1637,12 +1948,12 @@
16371948 spi->mode |= SPI_CPHA;
16381949 if (of_property_read_bool(nc, "spi-cpol"))
16391950 spi->mode |= SPI_CPOL;
1640
- if (of_property_read_bool(nc, "spi-cs-high"))
1641
- spi->mode |= SPI_CS_HIGH;
16421951 if (of_property_read_bool(nc, "spi-3wire"))
16431952 spi->mode |= SPI_3WIRE;
16441953 if (of_property_read_bool(nc, "spi-lsb-first"))
16451954 spi->mode |= SPI_LSB_FIRST;
1955
+ if (of_property_read_bool(nc, "spi-cs-high"))
1956
+ spi->mode |= SPI_CS_HIGH;
16461957
16471958 /* Device DUAL/QUAD mode */
16481959 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
....@@ -1654,6 +1965,9 @@
16541965 break;
16551966 case 4:
16561967 spi->mode |= SPI_TX_QUAD;
1968
+ break;
1969
+ case 8:
1970
+ spi->mode |= SPI_TX_OCTAL;
16571971 break;
16581972 default:
16591973 dev_warn(&ctlr->dev,
....@@ -1673,6 +1987,9 @@
16731987 case 4:
16741988 spi->mode |= SPI_RX_QUAD;
16751989 break;
1990
+ case 8:
1991
+ spi->mode |= SPI_RX_OCTAL;
1992
+ break;
16761993 default:
16771994 dev_warn(&ctlr->dev,
16781995 "spi-rx-bus-width %d not supported\n",
....@@ -1682,7 +1999,7 @@
16821999 }
16832000
16842001 if (spi_controller_is_slave(ctlr)) {
1685
- if (strcmp(nc->name, "slave")) {
2002
+ if (!of_node_name_eq(nc, "slave")) {
16862003 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
16872004 nc);
16882005 return -EINVAL;
....@@ -1700,13 +2017,8 @@
17002017 spi->chip_select = value;
17012018
17022019 /* Device speed */
1703
- rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1704
- if (rc) {
1705
- dev_err(&ctlr->dev,
1706
- "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
1707
- return rc;
1708
- }
1709
- spi->max_speed_hz = value;
2020
+ if (!of_property_read_u32(nc, "spi-max-frequency", &value))
2021
+ spi->max_speed_hz = value;
17102022
17112023 return 0;
17122024 }
....@@ -1789,9 +2101,18 @@
17892101 #endif
17902102
17912103 #ifdef CONFIG_ACPI
1792
-static void acpi_spi_parse_apple_properties(struct spi_device *spi)
2104
+struct acpi_spi_lookup {
2105
+ struct spi_controller *ctlr;
2106
+ u32 max_speed_hz;
2107
+ u32 mode;
2108
+ int irq;
2109
+ u8 bits_per_word;
2110
+ u8 chip_select;
2111
+};
2112
+
2113
+static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
2114
+ struct acpi_spi_lookup *lookup)
17932115 {
1794
- struct acpi_device *dev = ACPI_COMPANION(&spi->dev);
17952116 const union acpi_object *obj;
17962117
17972118 if (!x86_apple_machine)
....@@ -1799,35 +2120,46 @@
17992120
18002121 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
18012122 && obj->buffer.length >= 4)
1802
- spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
2123
+ lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
18032124
18042125 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
18052126 && obj->buffer.length == 8)
1806
- spi->bits_per_word = *(u64 *)obj->buffer.pointer;
2127
+ lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
18072128
18082129 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
18092130 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
1810
- spi->mode |= SPI_LSB_FIRST;
2131
+ lookup->mode |= SPI_LSB_FIRST;
18112132
18122133 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
18132134 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1814
- spi->mode |= SPI_CPOL;
2135
+ lookup->mode |= SPI_CPOL;
18152136
18162137 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
18172138 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1818
- spi->mode |= SPI_CPHA;
2139
+ lookup->mode |= SPI_CPHA;
18192140 }
18202141
18212142 static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
18222143 {
1823
- struct spi_device *spi = data;
1824
- struct spi_controller *ctlr = spi->controller;
2144
+ struct acpi_spi_lookup *lookup = data;
2145
+ struct spi_controller *ctlr = lookup->ctlr;
18252146
18262147 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
18272148 struct acpi_resource_spi_serialbus *sb;
2149
+ acpi_handle parent_handle;
2150
+ acpi_status status;
18282151
18292152 sb = &ares->data.spi_serial_bus;
18302153 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
2154
+
2155
+ status = acpi_get_handle(NULL,
2156
+ sb->resource_source.string_ptr,
2157
+ &parent_handle);
2158
+
2159
+ if (ACPI_FAILURE(status) ||
2160
+ ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
2161
+ return -ENODEV;
2162
+
18312163 /*
18322164 * ACPI DeviceSelection numbering is handled by the
18332165 * host controller driver in Windows and can vary
....@@ -1840,25 +2172,26 @@
18402172 sb->device_selection);
18412173 if (cs < 0)
18422174 return cs;
1843
- spi->chip_select = cs;
2175
+ lookup->chip_select = cs;
18442176 } else {
1845
- spi->chip_select = sb->device_selection;
2177
+ lookup->chip_select = sb->device_selection;
18462178 }
18472179
1848
- spi->max_speed_hz = sb->connection_speed;
2180
+ lookup->max_speed_hz = sb->connection_speed;
2181
+ lookup->bits_per_word = sb->data_bit_length;
18492182
18502183 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1851
- spi->mode |= SPI_CPHA;
2184
+ lookup->mode |= SPI_CPHA;
18522185 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1853
- spi->mode |= SPI_CPOL;
2186
+ lookup->mode |= SPI_CPOL;
18542187 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1855
- spi->mode |= SPI_CS_HIGH;
2188
+ lookup->mode |= SPI_CS_HIGH;
18562189 }
1857
- } else if (spi->irq < 0) {
2190
+ } else if (lookup->irq < 0) {
18582191 struct resource r;
18592192
18602193 if (acpi_dev_resource_interrupt(ares, 0, &r))
1861
- spi->irq = r.start;
2194
+ lookup->irq = r.start;
18622195 }
18632196
18642197 /* Always tell the ACPI core to skip this resource */
....@@ -1868,12 +2201,36 @@
18682201 static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
18692202 struct acpi_device *adev)
18702203 {
2204
+ acpi_handle parent_handle = NULL;
18712205 struct list_head resource_list;
2206
+ struct acpi_spi_lookup lookup = {};
18722207 struct spi_device *spi;
18732208 int ret;
18742209
18752210 if (acpi_bus_get_status(adev) || !adev->status.present ||
18762211 acpi_device_enumerated(adev))
2212
+ return AE_OK;
2213
+
2214
+ lookup.ctlr = ctlr;
2215
+ lookup.irq = -1;
2216
+
2217
+ INIT_LIST_HEAD(&resource_list);
2218
+ ret = acpi_dev_get_resources(adev, &resource_list,
2219
+ acpi_spi_add_resource, &lookup);
2220
+ acpi_dev_free_resource_list(&resource_list);
2221
+
2222
+ if (ret < 0)
2223
+ /* found SPI in _CRS but it points to another controller */
2224
+ return AE_OK;
2225
+
2226
+ if (!lookup.max_speed_hz &&
2227
+ !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
2228
+ ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
2229
+ /* Apple does not use _CRS but nested devices for SPI slaves */
2230
+ acpi_spi_parse_apple_properties(adev, &lookup);
2231
+ }
2232
+
2233
+ if (!lookup.max_speed_hz)
18772234 return AE_OK;
18782235
18792236 spi = spi_alloc_device(ctlr);
....@@ -1883,20 +2240,13 @@
18832240 return AE_NO_MEMORY;
18842241 }
18852242
2243
+
18862244 ACPI_COMPANION_SET(&spi->dev, adev);
1887
- spi->irq = -1;
1888
-
1889
- INIT_LIST_HEAD(&resource_list);
1890
- ret = acpi_dev_get_resources(adev, &resource_list,
1891
- acpi_spi_add_resource, spi);
1892
- acpi_dev_free_resource_list(&resource_list);
1893
-
1894
- acpi_spi_parse_apple_properties(spi);
1895
-
1896
- if (ret < 0 || !spi->max_speed_hz) {
1897
- spi_dev_put(spi);
1898
- return AE_OK;
1899
- }
2245
+ spi->max_speed_hz = lookup.max_speed_hz;
2246
+ spi->mode |= lookup.mode;
2247
+ spi->irq = lookup.irq;
2248
+ spi->bits_per_word = lookup.bits_per_word;
2249
+ spi->chip_select = lookup.chip_select;
19002250
19012251 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
19022252 sizeof(spi->modalias));
....@@ -1929,6 +2279,8 @@
19292279 return acpi_register_spi_device(ctlr, adev);
19302280 }
19312281
2282
+#define SPI_ACPI_ENUMERATE_MAX_DEPTH 32
2283
+
19322284 static void acpi_register_spi_devices(struct spi_controller *ctlr)
19332285 {
19342286 acpi_status status;
....@@ -1938,7 +2290,8 @@
19382290 if (!handle)
19392291 return;
19402292
1941
- status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
2293
+ status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2294
+ SPI_ACPI_ENUMERATE_MAX_DEPTH,
19422295 acpi_spi_add_device, NULL, ctlr, NULL);
19432296 if (ACPI_FAILURE(status))
19442297 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
....@@ -1984,8 +2337,8 @@
19842337 return 1;
19852338 }
19862339
1987
-static ssize_t spi_slave_show(struct device *dev,
1988
- struct device_attribute *attr, char *buf)
2340
+static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2341
+ char *buf)
19892342 {
19902343 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
19912344 dev);
....@@ -1996,9 +2349,8 @@
19962349 child ? to_spi_device(child)->modalias : NULL);
19972350 }
19982351
1999
-static ssize_t spi_slave_store(struct device *dev,
2000
- struct device_attribute *attr, const char *buf,
2001
- size_t count)
2352
+static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2353
+ const char *buf, size_t count)
20022354 {
20032355 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
20042356 dev);
....@@ -2036,7 +2388,7 @@
20362388 return count;
20372389 }
20382390
2039
-static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store);
2391
+static DEVICE_ATTR_RW(slave);
20402392
20412393 static struct attribute *spi_slave_attrs[] = {
20422394 &dev_attr_slave.attr,
....@@ -2067,8 +2419,10 @@
20672419 * __spi_alloc_controller - allocate an SPI master or slave controller
20682420 * @dev: the controller, possibly using the platform_bus
20692421 * @size: how much zeroed driver-private data to allocate; the pointer to this
2070
- * memory is in the driver_data field of the returned device,
2071
- * accessible with spi_controller_get_devdata().
2422
+ * memory is in the driver_data field of the returned device, accessible
2423
+ * with spi_controller_get_devdata(); the memory is cacheline aligned;
2424
+ * drivers granting DMA access to portions of their private data need to
2425
+ * round up @size using ALIGN(size, dma_get_cache_alignment()).
20722426 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
20732427 * slave (true) controller
20742428 * Context: can sleep
....@@ -2090,11 +2444,12 @@
20902444 unsigned int size, bool slave)
20912445 {
20922446 struct spi_controller *ctlr;
2447
+ size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
20932448
20942449 if (!dev)
20952450 return NULL;
20962451
2097
- ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL);
2452
+ ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
20982453 if (!ctlr)
20992454 return NULL;
21002455
....@@ -2108,7 +2463,7 @@
21082463 ctlr->dev.class = &spi_master_class;
21092464 ctlr->dev.parent = dev;
21102465 pm_suspend_ignore_children(&ctlr->dev, true);
2111
- spi_controller_set_devdata(ctlr, &ctlr[1]);
2466
+ spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
21122467
21132468 return ctlr;
21142469 }
....@@ -2158,7 +2513,7 @@
21582513 EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
21592514
21602515 #ifdef CONFIG_OF
2161
-static int of_spi_register_master(struct spi_controller *ctlr)
2516
+static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
21622517 {
21632518 int nb, i, *cs;
21642519 struct device_node *np = ctlr->dev.of_node;
....@@ -2191,11 +2546,85 @@
21912546 return 0;
21922547 }
21932548 #else
2194
-static int of_spi_register_master(struct spi_controller *ctlr)
2549
+static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
21952550 {
21962551 return 0;
21972552 }
21982553 #endif
2554
+
2555
+/**
2556
+ * spi_get_gpio_descs() - grab chip select GPIOs for the master
2557
+ * @ctlr: The SPI master to grab GPIO descriptors for
2558
+ */
2559
+static int spi_get_gpio_descs(struct spi_controller *ctlr)
2560
+{
2561
+ int nb, i;
2562
+ struct gpio_desc **cs;
2563
+ struct device *dev = &ctlr->dev;
2564
+ unsigned long native_cs_mask = 0;
2565
+ unsigned int num_cs_gpios = 0;
2566
+
2567
+ nb = gpiod_count(dev, "cs");
2568
+ ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2569
+
2570
+ /* No GPIOs at all is fine, else return the error */
2571
+ if (nb == 0 || nb == -ENOENT)
2572
+ return 0;
2573
+ else if (nb < 0)
2574
+ return nb;
2575
+
2576
+ cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2577
+ GFP_KERNEL);
2578
+ if (!cs)
2579
+ return -ENOMEM;
2580
+ ctlr->cs_gpiods = cs;
2581
+
2582
+ for (i = 0; i < nb; i++) {
2583
+ /*
2584
+ * Most chipselects are active low, the inverted
2585
+ * semantics are handled by special quirks in gpiolib,
2586
+ * so initializing them GPIOD_OUT_LOW here means
2587
+ * "unasserted", in most cases this will drive the physical
2588
+ * line high.
2589
+ */
2590
+ cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2591
+ GPIOD_OUT_LOW);
2592
+ if (IS_ERR(cs[i]))
2593
+ return PTR_ERR(cs[i]);
2594
+
2595
+ if (cs[i]) {
2596
+ /*
2597
+ * If we find a CS GPIO, name it after the device and
2598
+ * chip select line.
2599
+ */
2600
+ char *gpioname;
2601
+
2602
+ gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2603
+ dev_name(dev), i);
2604
+ if (!gpioname)
2605
+ return -ENOMEM;
2606
+ gpiod_set_consumer_name(cs[i], gpioname);
2607
+ num_cs_gpios++;
2608
+ continue;
2609
+ }
2610
+
2611
+ if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
2612
+ dev_err(dev, "Invalid native chip select %d\n", i);
2613
+ return -EINVAL;
2614
+ }
2615
+ native_cs_mask |= BIT(i);
2616
+ }
2617
+
2618
+ ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
2619
+
2620
+ if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios &&
2621
+ ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
2622
+ dev_err(dev, "No unused native chip select available\n");
2623
+ return -EINVAL;
2624
+ }
2625
+
2626
+ return 0;
2627
+}
21992628
22002629 static int spi_controller_check_ops(struct spi_controller *ctlr)
22012630 {
....@@ -2244,7 +2673,7 @@
22442673 {
22452674 struct device *dev = ctlr->dev.parent;
22462675 struct boardinfo *bi;
2247
- int status = -ENODEV;
2676
+ int status;
22482677 int id, first_dynamic;
22492678
22502679 if (!dev)
....@@ -2258,17 +2687,6 @@
22582687 if (status)
22592688 return status;
22602689
2261
- if (!spi_controller_is_slave(ctlr)) {
2262
- status = of_spi_register_master(ctlr);
2263
- if (status)
2264
- return status;
2265
- }
2266
-
2267
- /* even if it's just one always-selected device, there must
2268
- * be at least one chipselect
2269
- */
2270
- if (ctlr->num_chipselect == 0)
2271
- return -EINVAL;
22722690 if (ctlr->bus_num >= 0) {
22732691 /* devices with a fixed bus num must check-in with the num */
22742692 mutex_lock(&board_lock);
....@@ -2320,14 +2738,37 @@
23202738 * registration fails if the bus ID is in use.
23212739 */
23222740 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2323
- status = device_add(&ctlr->dev);
2324
- if (status < 0) {
2325
- /* free bus id */
2326
- mutex_lock(&board_lock);
2327
- idr_remove(&spi_master_idr, ctlr->bus_num);
2328
- mutex_unlock(&board_lock);
2329
- goto done;
2741
+
2742
+ if (!spi_controller_is_slave(ctlr)) {
2743
+ if (ctlr->use_gpio_descriptors) {
2744
+ status = spi_get_gpio_descs(ctlr);
2745
+ if (status)
2746
+ goto free_bus_id;
2747
+ /*
2748
+ * A controller using GPIO descriptors always
2749
+ * supports SPI_CS_HIGH if need be.
2750
+ */
2751
+ ctlr->mode_bits |= SPI_CS_HIGH;
2752
+ } else {
2753
+ /* Legacy code path for GPIOs from DT */
2754
+ status = of_spi_get_gpio_numbers(ctlr);
2755
+ if (status)
2756
+ goto free_bus_id;
2757
+ }
23302758 }
2759
+
2760
+ /*
2761
+ * Even if it's just one always-selected device, there must
2762
+ * be at least one chipselect.
2763
+ */
2764
+ if (!ctlr->num_chipselect) {
2765
+ status = -EINVAL;
2766
+ goto free_bus_id;
2767
+ }
2768
+
2769
+ status = device_add(&ctlr->dev);
2770
+ if (status < 0)
2771
+ goto free_bus_id;
23312772 dev_dbg(dev, "registered %s %s\n",
23322773 spi_controller_is_slave(ctlr) ? "slave" : "master",
23332774 dev_name(&ctlr->dev));
....@@ -2343,11 +2784,7 @@
23432784 status = spi_controller_initialize_queue(ctlr);
23442785 if (status) {
23452786 device_del(&ctlr->dev);
2346
- /* free bus id */
2347
- mutex_lock(&board_lock);
2348
- idr_remove(&spi_master_idr, ctlr->bus_num);
2349
- mutex_unlock(&board_lock);
2350
- goto done;
2787
+ goto free_bus_id;
23512788 }
23522789 }
23532790 /* add statistics */
....@@ -2362,7 +2799,12 @@
23622799 /* Register devices from the device tree and ACPI */
23632800 of_register_spi_devices(ctlr);
23642801 acpi_register_spi_devices(ctlr);
2365
-done:
2802
+ return status;
2803
+
2804
+free_bus_id:
2805
+ mutex_lock(&board_lock);
2806
+ idr_remove(&spi_master_idr, ctlr->bus_num);
2807
+ mutex_unlock(&board_lock);
23662808 return status;
23672809 }
23682810 EXPORT_SYMBOL_GPL(spi_register_controller);
....@@ -2612,12 +3054,9 @@
26123054 */
26133055 void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
26143056 {
2615
- struct spi_res *res;
3057
+ struct spi_res *res, *tmp;
26163058
2617
- while (!list_empty(&message->resources)) {
2618
- res = list_last_entry(&message->resources,
2619
- struct spi_res, entry);
2620
-
3059
+ list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
26213060 if (res->release)
26223061 res->release(ctlr, message, res->data);
26233062
....@@ -2681,8 +3120,7 @@
26813120
26823121 /* allocate the structure using spi_res */
26833122 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2684
- insert * sizeof(struct spi_transfer)
2685
- + sizeof(struct spi_replaced_transfers)
3123
+ struct_size(rxfer, inserted_transfers, insert)
26863124 + extradatasize,
26873125 gfp);
26883126 if (!rxfer)
....@@ -2744,10 +3182,11 @@
27443182 /* add to list */
27453183 list_add(&xfer->transfer_list, rxfer->replaced_after);
27463184
2747
- /* clear cs_change and delay_usecs for all but the last */
3185
+ /* clear cs_change and delay for all but the last */
27483186 if (i) {
27493187 xfer->cs_change = false;
27503188 xfer->delay_usecs = 0;
3189
+ xfer->delay.value = 0;
27513190 }
27523191 }
27533192
....@@ -2771,11 +3210,6 @@
27713210 struct spi_replaced_transfers *srt;
27723211 size_t offset;
27733212 size_t count, i;
2774
-
2775
- /* warn once about this fact that we are splitting a transfer */
2776
- dev_warn_once(&msg->spi->dev,
2777
- "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
2778
- xfer->len, maxsize);
27793213
27803214 /* calculate how many we have to replace */
27813215 count = DIV_ROUND_UP(xfer->len, maxsize);
....@@ -2925,7 +3359,8 @@
29253359 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
29263360 */
29273361 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2928
- (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
3362
+ (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3363
+ SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
29293364 return -EINVAL;
29303365 /* help drivers fail *cleanly* when they need options
29313366 * that aren't supported with their current controller
....@@ -2933,8 +3368,14 @@
29333368 * so it is ignored here.
29343369 */
29353370 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
3371
+ /* nothing prevents from working with active-high CS in case if it
3372
+ * is driven by GPIO.
3373
+ */
3374
+ if (gpio_is_valid(spi->cs_gpio))
3375
+ bad_bits &= ~SPI_CS_HIGH;
29363376 ugly_bits = bad_bits &
2937
- (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
3377
+ (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3378
+ SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
29383379 if (ugly_bits) {
29393380 dev_warn(&spi->dev,
29403381 "setup: ignoring unsupported mode bits %x\n",
....@@ -2959,10 +3400,42 @@
29593400 if (!spi->max_speed_hz)
29603401 spi->max_speed_hz = spi->controller->max_speed_hz;
29613402
3403
+ mutex_lock(&spi->controller->io_mutex);
3404
+
29623405 if (spi->controller->setup)
29633406 status = spi->controller->setup(spi);
29643407
2965
- spi_set_cs(spi, false);
3408
+ if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3409
+ status = pm_runtime_get_sync(spi->controller->dev.parent);
3410
+ if (status < 0) {
3411
+ mutex_unlock(&spi->controller->io_mutex);
3412
+ pm_runtime_put_noidle(spi->controller->dev.parent);
3413
+ dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3414
+ status);
3415
+ return status;
3416
+ }
3417
+
3418
+ /*
3419
+ * We do not want to return positive value from pm_runtime_get,
3420
+ * there are many instances of devices calling spi_setup() and
3421
+ * checking for a non-zero return value instead of a negative
3422
+ * return value.
3423
+ */
3424
+ status = 0;
3425
+
3426
+ spi_set_cs(spi, false, true);
3427
+ pm_runtime_mark_last_busy(spi->controller->dev.parent);
3428
+ pm_runtime_put_autosuspend(spi->controller->dev.parent);
3429
+ } else {
3430
+ spi_set_cs(spi, false, true);
3431
+ }
3432
+
3433
+ mutex_unlock(&spi->controller->io_mutex);
3434
+
3435
+ if (spi->rt && !spi->controller->rt) {
3436
+ spi->controller->rt = true;
3437
+ spi_set_thread_rt(spi->controller);
3438
+ }
29663439
29673440 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
29683441 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
....@@ -2976,6 +3449,74 @@
29763449 return status;
29773450 }
29783451 EXPORT_SYMBOL_GPL(spi_setup);
3452
+
3453
+/**
3454
+ * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3455
+ * @spi: the device that requires specific CS timing configuration
3456
+ * @setup: CS setup time specified via @spi_delay
3457
+ * @hold: CS hold time specified via @spi_delay
3458
+ * @inactive: CS inactive delay between transfers specified via @spi_delay
3459
+ *
3460
+ * Return: zero on success, else a negative error code.
3461
+ */
3462
+int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup,
3463
+ struct spi_delay *hold, struct spi_delay *inactive)
3464
+{
3465
+ size_t len;
3466
+
3467
+ if (spi->controller->set_cs_timing)
3468
+ return spi->controller->set_cs_timing(spi, setup, hold,
3469
+ inactive);
3470
+
3471
+ if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) ||
3472
+ (hold && hold->unit == SPI_DELAY_UNIT_SCK) ||
3473
+ (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) {
3474
+ dev_err(&spi->dev,
3475
+ "Clock-cycle delays for CS not supported in SW mode\n");
3476
+ return -ENOTSUPP;
3477
+ }
3478
+
3479
+ len = sizeof(struct spi_delay);
3480
+
3481
+ /* copy delays to controller */
3482
+ if (setup)
3483
+ memcpy(&spi->controller->cs_setup, setup, len);
3484
+ else
3485
+ memset(&spi->controller->cs_setup, 0, len);
3486
+
3487
+ if (hold)
3488
+ memcpy(&spi->controller->cs_hold, hold, len);
3489
+ else
3490
+ memset(&spi->controller->cs_hold, 0, len);
3491
+
3492
+ if (inactive)
3493
+ memcpy(&spi->controller->cs_inactive, inactive, len);
3494
+ else
3495
+ memset(&spi->controller->cs_inactive, 0, len);
3496
+
3497
+ return 0;
3498
+}
3499
+EXPORT_SYMBOL_GPL(spi_set_cs_timing);
3500
+
3501
+static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
3502
+ struct spi_device *spi)
3503
+{
3504
+ int delay1, delay2;
3505
+
3506
+ delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
3507
+ if (delay1 < 0)
3508
+ return delay1;
3509
+
3510
+ delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
3511
+ if (delay2 < 0)
3512
+ return delay2;
3513
+
3514
+ if (delay1 < delay2)
3515
+ memcpy(&xfer->word_delay, &spi->word_delay,
3516
+ sizeof(xfer->word_delay));
3517
+
3518
+ return 0;
3519
+}
29793520
29803521 static int __spi_validate(struct spi_device *spi, struct spi_message *message)
29813522 {
....@@ -2993,6 +3534,7 @@
29933534 * cs_change is set for each transfer.
29943535 */
29953536 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
3537
+ spi->cs_gpiod ||
29963538 gpio_is_valid(spi->cs_gpio))) {
29973539 size_t maxsize;
29983540 int ret;
....@@ -3039,17 +3581,18 @@
30393581 * it is not set for this transfer.
30403582 * Set transfer tx_nbits and rx_nbits as single transfer default
30413583 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
3584
+ * Ensure transfer word_delay is at least as long as that required by
3585
+ * device itself.
30423586 */
30433587 message->frame_length = 0;
30443588 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3589
+ xfer->effective_speed_hz = 0;
30453590 message->frame_length += xfer->len;
30463591 if (!xfer->bits_per_word)
30473592 xfer->bits_per_word = spi->bits_per_word;
30483593
30493594 if (!xfer->speed_hz)
30503595 xfer->speed_hz = spi->max_speed_hz;
3051
- if (!xfer->speed_hz)
3052
- xfer->speed_hz = ctlr->max_speed_hz;
30533596
30543597 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
30553598 xfer->speed_hz = ctlr->max_speed_hz;
....@@ -3109,6 +3652,9 @@
31093652 !(spi->mode & SPI_RX_QUAD))
31103653 return -EINVAL;
31113654 }
3655
+
3656
+ if (_spi_xfer_word_delay_update(xfer, spi))
3657
+ return -EINVAL;
31123658 }
31133659
31143660 message->status = -EINPROGRESS;
....@@ -3119,6 +3665,7 @@
31193665 static int __spi_async(struct spi_device *spi, struct spi_message *message)
31203666 {
31213667 struct spi_controller *ctlr = spi->controller;
3668
+ struct spi_transfer *xfer;
31223669
31233670 /*
31243671 * Some controllers do not support doing regular SPI transfers. Return
....@@ -3133,6 +3680,13 @@
31333680 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
31343681
31353682 trace_spi_message_submit(message);
3683
+
3684
+ if (!ctlr->ptp_sts_supported) {
3685
+ list_for_each_entry(xfer, &message->transfers, transfer_list) {
3686
+ xfer->ptp_sts_word_pre = 0;
3687
+ ptp_read_system_prets(xfer->ptp_sts);
3688
+ }
3689
+ }
31363690
31373691 return ctlr->transfer(spi, message);
31383692 }
....@@ -3437,8 +3991,7 @@
34373991 * is zero for success, else a negative errno status code.
34383992 * This call may only be used from a context that may sleep.
34393993 *
3440
- * Parameters to this routine are always copied using a small buffer;
3441
- * portable code should never use this for more than 32 bytes.
3994
+ * Parameters to this routine are always copied using a small buffer.
34423995 * Performance-sensitive or bulk transfer code should instead use
34433996 * spi_{async,sync}() calls with dma-safe buffers.
34443997 *
....@@ -3501,37 +4054,25 @@
35014054 /*-------------------------------------------------------------------------*/
35024055
35034056 #if IS_ENABLED(CONFIG_OF)
3504
-static int __spi_of_device_match(struct device *dev, void *data)
3505
-{
3506
- return dev->of_node == data;
3507
-}
3508
-
35094057 /* must call put_device() when done with returned spi_device device */
35104058 struct spi_device *of_find_spi_device_by_node(struct device_node *node)
35114059 {
3512
- struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3513
- __spi_of_device_match);
4060
+ struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
4061
+
35144062 return dev ? to_spi_device(dev) : NULL;
35154063 }
35164064 EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
35174065 #endif /* IS_ENABLED(CONFIG_OF) */
35184066
35194067 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
3520
-static int __spi_of_controller_match(struct device *dev, const void *data)
3521
-{
3522
- return dev->of_node == data;
3523
-}
3524
-
35254068 /* the spi controllers are not using spi_bus, so we find it with another way */
35264069 static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
35274070 {
35284071 struct device *dev;
35294072
3530
- dev = class_find_device(&spi_master_class, NULL, node,
3531
- __spi_of_controller_match);
4073
+ dev = class_find_device_by_of_node(&spi_master_class, node);
35324074 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3533
- dev = class_find_device(&spi_slave_class, NULL, node,
3534
- __spi_of_controller_match);
4075
+ dev = class_find_device_by_of_node(&spi_slave_class, node);
35354076 if (!dev)
35364077 return NULL;
35374078
....@@ -3602,11 +4143,6 @@
36024143 return ACPI_COMPANION(dev->parent) == data;
36034144 }
36044145
3605
-static int spi_acpi_device_match(struct device *dev, void *data)
3606
-{
3607
- return ACPI_COMPANION(dev) == data;
3608
-}
3609
-
36104146 static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
36114147 {
36124148 struct device *dev;
....@@ -3626,9 +4162,8 @@
36264162 {
36274163 struct device *dev;
36284164
3629
- dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3630
-
3631
- return dev ? to_spi_device(dev) : NULL;
4165
+ dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
4166
+ return to_spi_device(dev);
36324167 }
36334168
36344169 static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
....@@ -3720,4 +4255,3 @@
37204255 * include needing to have boardinfo data structures be much more public.
37214256 */
37224257 postcore_initcall(spi_init);
3723
-