mt76-6e-usb: mt7921u: Add support Netgear AXE3000(A8000) device
[platform/kernel/linux-rpi.git] / drivers / spi / spi-gpio.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SPI master driver using generic bitbanged GPIO
4  *
5  * Copyright (C) 2006,2008 David Brownell
6  * Copyright (C) 2017 Linus Walleij
7  */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14
15 #include <linux/spi/spi.h>
16 #include <linux/spi/spi_bitbang.h>
17 #include <linux/spi/spi_gpio.h>
18
19
20 /*
21  * This bitbanging SPI master driver should help make systems usable
22  * when a native hardware SPI engine is not available, perhaps because
23  * its driver isn't yet working or because the I/O pins it requires
24  * are used for other purposes.
25  *
26  * platform_device->driver_data ... points to spi_gpio
27  *
28  * spi->controller_state ... reserved for bitbang framework code
29  *
30  * spi->master->dev.driver_data ... points to spi_gpio->bitbang
31  */
32
33 struct spi_gpio {
34         struct spi_bitbang              bitbang;
35         struct gpio_desc                *sck;
36         struct gpio_desc                *miso;
37         struct gpio_desc                *mosi;
38         bool                            sck_idle_input;
39         struct gpio_desc                **cs_gpios;
40 };
41
42 /*----------------------------------------------------------------------*/
43
44 /*
45  * Because the overhead of going through four GPIO procedure calls
46  * per transferred bit can make performance a problem, this code
47  * is set up so that you can use it in either of two ways:
48  *
49  *   - The slow generic way:  set up platform_data to hold the GPIO
50  *     numbers used for MISO/MOSI/SCK, and issue procedure calls for
51  *     each of them.  This driver can handle several such busses.
52  *
53  *   - The quicker inlined way:  only helps with platform GPIO code
54  *     that inlines operations for constant GPIOs.  This can give
55  *     you tight (fast!) inner loops, but each such bus needs a
56  *     new driver.  You'll define a new C file, with Makefile and
57  *     Kconfig support; the C code can be a total of six lines:
58  *
59  *              #define DRIVER_NAME     "myboard_spi2"
60  *              #define SPI_MISO_GPIO   119
61  *              #define SPI_MOSI_GPIO   120
62  *              #define SPI_SCK_GPIO    121
63  *              #define SPI_N_CHIPSEL   4
64  *              #include "spi-gpio.c"
65  */
66
67 #ifndef DRIVER_NAME
68 #define DRIVER_NAME     "spi_gpio"
69
70 #define GENERIC_BITBANG /* vs tight inlines */
71
72 #endif
73
74 /*----------------------------------------------------------------------*/
75
76 static inline struct spi_gpio *__pure
77 spi_to_spi_gpio(const struct spi_device *spi)
78 {
79         const struct spi_bitbang        *bang;
80         struct spi_gpio                 *spi_gpio;
81
82         bang = spi_master_get_devdata(spi->master);
83         spi_gpio = container_of(bang, struct spi_gpio, bitbang);
84         return spi_gpio;
85 }
86
87 /* These helpers are in turn called by the bitbang inlines */
88 static inline void setsck(const struct spi_device *spi, int is_on)
89 {
90         struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
91
92         gpiod_set_value_cansleep(spi_gpio->sck, is_on);
93 }
94
95 static inline void setmosi(const struct spi_device *spi, int is_on)
96 {
97         struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
98
99         gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
100 }
101
102 static inline int getmiso(const struct spi_device *spi)
103 {
104         struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
105
106         if (spi->mode & SPI_3WIRE)
107                 return !!gpiod_get_value_cansleep(spi_gpio->mosi);
108         else
109                 return !!gpiod_get_value_cansleep(spi_gpio->miso);
110 }
111
112 /*
113  * NOTE:  this clocks "as fast as we can".  It "should" be a function of the
114  * requested device clock.  Software overhead means we usually have trouble
115  * reaching even one Mbit/sec (except when we can inline bitops), so for now
116  * we'll just assume we never need additional per-bit slowdowns.
117  */
118 #define spidelay(nsecs) do {} while (0)
119
120 #include "spi-bitbang-txrx.h"
121
122 /*
123  * These functions can leverage inline expansion of GPIO calls to shrink
124  * costs for a txrx bit, often by factors of around ten (by instruction
125  * count).  That is particularly visible for larger word sizes, but helps
126  * even with default 8-bit words.
127  *
128  * REVISIT overheads calling these functions for each word also have
129  * significant performance costs.  Having txrx_bufs() calls that inline
130  * the txrx_word() logic would help performance, e.g. on larger blocks
131  * used with flash storage or MMC/SD.  There should also be ways to make
132  * GCC be less stupid about reloading registers inside the I/O loops,
133  * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
134  */
135
136 static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
137                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
138 {
139         return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
140 }
141
142 static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
143                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
144 {
145         return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
146 }
147
148 static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
149                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
150 {
151         return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
152 }
153
154 static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
155                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
156 {
157         return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
158 }
159
160 /*
161  * These functions do not call setmosi or getmiso if respective flag
162  * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
163  * call when such pin is not present or defined in the controller.
164  * A separate set of callbacks is defined to get highest possible
165  * speed in the generic case (when both MISO and MOSI lines are
166  * available), as optimiser will remove the checks when argument is
167  * constant.
168  */
169
170 static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
171                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
172 {
173         flags = spi->master->flags;
174         return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
175 }
176
177 static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
178                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
179 {
180         flags = spi->master->flags;
181         return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
182 }
183
184 static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
185                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
186 {
187         flags = spi->master->flags;
188         return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
189 }
190
191 static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
192                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
193 {
194         flags = spi->master->flags;
195         return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
196 }
197
198 /*----------------------------------------------------------------------*/
199
200 static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
201 {
202         struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
203
204         /* set initial clock line level */
205         if (is_active) {
206                 if (spi_gpio->sck_idle_input)
207                         gpiod_direction_output(spi_gpio->sck, spi->mode & SPI_CPOL);
208                 else
209                         gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
210         }
211
212         /* Drive chip select line, if we have one */
213         if (spi_gpio->cs_gpios) {
214                 struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
215
216                 /* SPI chip selects are normally active-low */
217                 gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
218         }
219
220         if (spi_gpio->sck_idle_input && !is_active)
221                 gpiod_direction_input(spi_gpio->sck);
222 }
223
224 static int spi_gpio_setup(struct spi_device *spi)
225 {
226         struct gpio_desc        *cs;
227         int                     status = 0;
228         struct spi_gpio         *spi_gpio = spi_to_spi_gpio(spi);
229
230         /*
231          * The CS GPIOs have already been
232          * initialized from the descriptor lookup.
233          */
234         if (spi_gpio->cs_gpios) {
235                 cs = spi_gpio->cs_gpios[spi->chip_select];
236                 if (!spi->controller_state && cs)
237                         status = gpiod_direction_output(cs,
238                                                   !(spi->mode & SPI_CS_HIGH));
239         }
240
241         if (!status)
242                 status = spi_bitbang_setup(spi);
243
244         return status;
245 }
246
247 static int spi_gpio_set_direction(struct spi_device *spi, bool output)
248 {
249         struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
250         int ret;
251
252         if (output)
253                 return gpiod_direction_output(spi_gpio->mosi, 1);
254
255         /*
256          * Only change MOSI to an input if using 3WIRE mode.
257          * Otherwise, MOSI could be left floating if there is
258          * no pull resistor connected to the I/O pin, or could
259          * be left logic high if there is a pull-up. Transmitting
260          * logic high when only clocking MISO data in can put some
261          * SPI devices in to a bad state.
262          */
263         if (spi->mode & SPI_3WIRE) {
264                 ret = gpiod_direction_input(spi_gpio->mosi);
265                 if (ret)
266                         return ret;
267         }
268         /*
269          * Send a turnaround high impedance cycle when switching
270          * from output to input. Theoretically there should be
271          * a clock delay here, but as has been noted above, the
272          * nsec delay function for bit-banged GPIO is simply
273          * {} because bit-banging just doesn't get fast enough
274          * anyway.
275          */
276         if (spi->mode & SPI_3WIRE_HIZ) {
277                 gpiod_set_value_cansleep(spi_gpio->sck,
278                                          !(spi->mode & SPI_CPOL));
279                 gpiod_set_value_cansleep(spi_gpio->sck,
280                                          !!(spi->mode & SPI_CPOL));
281         }
282         return 0;
283 }
284
285 static void spi_gpio_cleanup(struct spi_device *spi)
286 {
287         spi_bitbang_cleanup(spi);
288 }
289
290 /*
291  * It can be convenient to use this driver with pins that have alternate
292  * functions associated with a "native" SPI controller if a driver for that
293  * controller is not available, or is missing important functionality.
294  *
295  * On platforms which can do so, configure MISO with a weak pullup unless
296  * there's an external pullup on that signal.  That saves power by avoiding
297  * floating signals.  (A weak pulldown would save power too, but many
298  * drivers expect to see all-ones data as the no slave "response".)
299  */
300 static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
301 {
302         spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
303         if (IS_ERR(spi_gpio->mosi))
304                 return PTR_ERR(spi_gpio->mosi);
305
306         spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
307         if (IS_ERR(spi_gpio->miso))
308                 return PTR_ERR(spi_gpio->miso);
309
310         spi_gpio->sck_idle_input = device_property_read_bool(dev, "sck-idle-input");
311         spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
312         return PTR_ERR_OR_ZERO(spi_gpio->sck);
313 }
314
315 #ifdef CONFIG_OF
316 static const struct of_device_id spi_gpio_dt_ids[] = {
317         { .compatible = "spi-gpio" },
318         {}
319 };
320 MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
321
322 static int spi_gpio_probe_dt(struct platform_device *pdev,
323                              struct spi_master *master)
324 {
325         master->dev.of_node = pdev->dev.of_node;
326         master->use_gpio_descriptors = true;
327
328         return 0;
329 }
330 #else
331 static inline int spi_gpio_probe_dt(struct platform_device *pdev,
332                                     struct spi_master *master)
333 {
334         return 0;
335 }
336 #endif
337
338 static int spi_gpio_probe_pdata(struct platform_device *pdev,
339                                 struct spi_master *master)
340 {
341         struct device *dev = &pdev->dev;
342         struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
343         struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
344         int i;
345
346 #ifdef GENERIC_BITBANG
347         if (!pdata || !pdata->num_chipselect)
348                 return -ENODEV;
349 #endif
350         /*
351          * The master needs to think there is a chipselect even if not
352          * connected
353          */
354         master->num_chipselect = pdata->num_chipselect ?: 1;
355
356         spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
357                                           sizeof(*spi_gpio->cs_gpios),
358                                           GFP_KERNEL);
359         if (!spi_gpio->cs_gpios)
360                 return -ENOMEM;
361
362         for (i = 0; i < master->num_chipselect; i++) {
363                 spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
364                                                              GPIOD_OUT_HIGH);
365                 if (IS_ERR(spi_gpio->cs_gpios[i]))
366                         return PTR_ERR(spi_gpio->cs_gpios[i]);
367         }
368
369         return 0;
370 }
371
372 static int spi_gpio_probe(struct platform_device *pdev)
373 {
374         int                             status;
375         struct spi_master               *master;
376         struct spi_gpio                 *spi_gpio;
377         struct device                   *dev = &pdev->dev;
378         struct spi_bitbang              *bb;
379
380         master = devm_spi_alloc_master(dev, sizeof(*spi_gpio));
381         if (!master)
382                 return -ENOMEM;
383
384         if (pdev->dev.of_node)
385                 status = spi_gpio_probe_dt(pdev, master);
386         else
387                 status = spi_gpio_probe_pdata(pdev, master);
388
389         if (status)
390                 return status;
391
392         spi_gpio = spi_master_get_devdata(master);
393
394         status = spi_gpio_request(dev, spi_gpio);
395         if (status)
396                 return status;
397
398         master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
399         master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
400                             SPI_CS_HIGH;
401         if (!spi_gpio->mosi) {
402                 /* HW configuration without MOSI pin
403                  *
404                  * No setting SPI_MASTER_NO_RX here - if there is only
405                  * a MOSI pin connected the host can still do RX by
406                  * changing the direction of the line.
407                  */
408                 master->flags = SPI_MASTER_NO_TX;
409         }
410
411         master->bus_num = pdev->id;
412         master->setup = spi_gpio_setup;
413         master->cleanup = spi_gpio_cleanup;
414
415         bb = &spi_gpio->bitbang;
416         bb->master = master;
417         /*
418          * There is some additional business, apart from driving the CS GPIO
419          * line, that we need to do on selection. This makes the local
420          * callback for chipselect always get called.
421          */
422         master->flags |= SPI_MASTER_GPIO_SS;
423         bb->chipselect = spi_gpio_chipselect;
424         bb->set_line_direction = spi_gpio_set_direction;
425
426         if (master->flags & SPI_MASTER_NO_TX) {
427                 bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
428                 bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
429                 bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
430                 bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
431         } else {
432                 bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
433                 bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
434                 bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
435                 bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
436         }
437         bb->setup_transfer = spi_bitbang_setup_transfer;
438
439         status = spi_bitbang_init(&spi_gpio->bitbang);
440         if (status)
441                 return status;
442
443         return devm_spi_register_master(&pdev->dev, master);
444 }
445
446 MODULE_ALIAS("platform:" DRIVER_NAME);
447
448 static struct platform_driver spi_gpio_driver = {
449         .driver = {
450                 .name   = DRIVER_NAME,
451                 .of_match_table = of_match_ptr(spi_gpio_dt_ids),
452         },
453         .probe          = spi_gpio_probe,
454 };
455 module_platform_driver(spi_gpio_driver);
456
457 MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
458 MODULE_AUTHOR("David Brownell");
459 MODULE_LICENSE("GPL");