hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/spi/spi-txx9.c
....@@ -26,7 +26,8 @@
2626 #include <linux/clk.h>
2727 #include <linux/io.h>
2828 #include <linux/module.h>
29
-#include <linux/gpio.h>
29
+#include <linux/gpio/machine.h>
30
+#include <linux/gpio/consumer.h>
3031
3132
3233 #define SPI_FIFO_SIZE 4
....@@ -79,7 +80,7 @@
7980 void __iomem *membase;
8081 int baseclk;
8182 struct clk *clk;
82
- int last_chipselect;
83
+ struct gpio_desc *last_chipselect;
8384 int last_chipselect_val;
8485 };
8586
....@@ -95,20 +96,22 @@
9596 static void txx9spi_cs_func(struct spi_device *spi, struct txx9spi *c,
9697 int on, unsigned int cs_delay)
9798 {
98
- int val = (spi->mode & SPI_CS_HIGH) ? on : !on;
99
-
99
+ /*
100
+ * The GPIO descriptor will track polarity inversion inside
101
+ * gpiolib.
102
+ */
100103 if (on) {
101104 /* deselect the chip with cs_change hint in last transfer */
102
- if (c->last_chipselect >= 0)
103
- gpio_set_value(c->last_chipselect,
105
+ if (c->last_chipselect)
106
+ gpiod_set_value(c->last_chipselect,
104107 !c->last_chipselect_val);
105
- c->last_chipselect = spi->chip_select;
106
- c->last_chipselect_val = val;
108
+ c->last_chipselect = spi->cs_gpiod;
109
+ c->last_chipselect_val = on;
107110 } else {
108
- c->last_chipselect = -1;
111
+ c->last_chipselect = NULL;
109112 ndelay(cs_delay); /* CS Hold Time */
110113 }
111
- gpio_set_value(spi->chip_select, val);
114
+ gpiod_set_value(spi->cs_gpiod, on);
112115 ndelay(cs_delay); /* CS Setup Time / CS Recovery Time */
113116 }
114117
....@@ -118,12 +121,6 @@
118121
119122 if (!spi->max_speed_hz)
120123 return -EINVAL;
121
-
122
- if (gpio_direction_output(spi->chip_select,
123
- !(spi->mode & SPI_CS_HIGH))) {
124
- dev_err(&spi->dev, "Cannot setup GPIO for chipselect.\n");
125
- return -EINVAL;
126
- }
127124
128125 /* deselect chip */
129126 spin_lock(&c->lock);
....@@ -248,8 +245,7 @@
248245 len -= count * wsize;
249246 }
250247 m->actual_length += t->len;
251
- if (t->delay_usecs)
252
- udelay(t->delay_usecs);
248
+ spi_transfer_delay_exec(t);
253249
254250 if (!cs_change)
255251 continue;
....@@ -320,6 +316,47 @@
320316 return 0;
321317 }
322318
319
+/*
320
+ * Chip select uses GPIO only, further the driver is using the chip select
321
+ * numer (from the device tree "reg" property, and this can only come from
322
+ * device tree since this i MIPS and there is no way to pass platform data) as
323
+ * the GPIO number. As the platform has only one GPIO controller (the txx9 GPIO
324
+ * chip) it is thus using the chip select number as an offset into that chip.
325
+ * This chip has a maximum of 16 GPIOs 0..15 and this is what all platforms
326
+ * register.
327
+ *
328
+ * We modernized this behaviour by explicitly converting that offset to an
329
+ * offset on the GPIO chip using a GPIO descriptor machine table of the same
330
+ * size as the txx9 GPIO chip with a 1-to-1 mapping of chip select to GPIO
331
+ * offset.
332
+ *
333
+ * This is admittedly a hack, but it is countering the hack of using "reg" to
334
+ * contain a GPIO offset when it should be using "cs-gpios" as the SPI bindings
335
+ * state.
336
+ */
337
+static struct gpiod_lookup_table txx9spi_cs_gpio_table = {
338
+ .dev_id = "spi0",
339
+ .table = {
340
+ GPIO_LOOKUP_IDX("TXx9", 0, "cs", 0, GPIO_ACTIVE_LOW),
341
+ GPIO_LOOKUP_IDX("TXx9", 1, "cs", 1, GPIO_ACTIVE_LOW),
342
+ GPIO_LOOKUP_IDX("TXx9", 2, "cs", 2, GPIO_ACTIVE_LOW),
343
+ GPIO_LOOKUP_IDX("TXx9", 3, "cs", 3, GPIO_ACTIVE_LOW),
344
+ GPIO_LOOKUP_IDX("TXx9", 4, "cs", 4, GPIO_ACTIVE_LOW),
345
+ GPIO_LOOKUP_IDX("TXx9", 5, "cs", 5, GPIO_ACTIVE_LOW),
346
+ GPIO_LOOKUP_IDX("TXx9", 6, "cs", 6, GPIO_ACTIVE_LOW),
347
+ GPIO_LOOKUP_IDX("TXx9", 7, "cs", 7, GPIO_ACTIVE_LOW),
348
+ GPIO_LOOKUP_IDX("TXx9", 8, "cs", 8, GPIO_ACTIVE_LOW),
349
+ GPIO_LOOKUP_IDX("TXx9", 9, "cs", 9, GPIO_ACTIVE_LOW),
350
+ GPIO_LOOKUP_IDX("TXx9", 10, "cs", 10, GPIO_ACTIVE_LOW),
351
+ GPIO_LOOKUP_IDX("TXx9", 11, "cs", 11, GPIO_ACTIVE_LOW),
352
+ GPIO_LOOKUP_IDX("TXx9", 12, "cs", 12, GPIO_ACTIVE_LOW),
353
+ GPIO_LOOKUP_IDX("TXx9", 13, "cs", 13, GPIO_ACTIVE_LOW),
354
+ GPIO_LOOKUP_IDX("TXx9", 14, "cs", 14, GPIO_ACTIVE_LOW),
355
+ GPIO_LOOKUP_IDX("TXx9", 15, "cs", 15, GPIO_ACTIVE_LOW),
356
+ { },
357
+ },
358
+};
359
+
323360 static int txx9spi_probe(struct platform_device *dev)
324361 {
325362 struct spi_master *master;
....@@ -373,11 +410,13 @@
373410 if (ret)
374411 goto exit;
375412
376
- c->last_chipselect = -1;
413
+ c->last_chipselect = NULL;
377414
378415 dev_info(&dev->dev, "at %#llx, irq %d, %dMHz\n",
379416 (unsigned long long)res->start, irq,
380417 (c->baseclk + 500000) / 1000000);
418
+
419
+ gpiod_add_lookup_table(&txx9spi_cs_gpio_table);
381420
382421 /* the spi->mode bits understood by this driver: */
383422 master->mode_bits = SPI_CS_HIGH | SPI_CPOL | SPI_CPHA;
....@@ -387,6 +426,7 @@
387426 master->transfer = txx9spi_transfer;
388427 master->num_chipselect = (u16)UINT_MAX; /* any GPIO numbers */
389428 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
429
+ master->use_gpio_descriptors = true;
390430
391431 ret = devm_spi_register_master(&dev->dev, master);
392432 if (ret)