media: rp1: csi2: Fix csi2_pad_set_fmt()
[platform/kernel/linux-rpi.git] / drivers / spi / spi-gpio.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SPI host 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/delay.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  * This bitbanging SPI host driver should help make systems usable
21  * when a native hardware SPI engine is not available, perhaps because
22  * its driver isn't yet working or because the I/O pins it requires
23  * are used for other purposes.
24  *
25  * platform_device->driver_data ... points to spi_gpio
26  *
27  * spi->controller_state ... reserved for bitbang framework code
28  *
29  * spi->controller->dev.driver_data ... points to spi_gpio->bitbang
30  */
31
32 struct spi_gpio {
33         struct spi_bitbang              bitbang;
34         struct gpio_desc                *sck;
35         struct gpio_desc                *miso;
36         struct gpio_desc                *mosi;
37         struct gpio_desc                **cs_gpios;
38         bool                            sck_idle_input;
39         bool                            cs_dont_invert;
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_controller_get_devdata(spi->controller);
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  * Generic bit-banged GPIO SPI might free-run at something in the range
114  * 1Mbps ~ 10Mbps (depending on the platform), and some SPI devices may
115  * need to be clocked at a lower rate. ndelay() is often implemented by
116  * udelay() with rounding up, so do the delay only for nsecs >= 500
117  * (<= 1Mbps). The conditional test adds a small overhead.
118  */
119
120 static inline void spidelay(unsigned long nsecs)
121 {
122         if (nsecs >= 500)
123                 ndelay(nsecs);
124 }
125
126 #include "spi-bitbang-txrx.h"
127
128 /*
129  * These functions can leverage inline expansion of GPIO calls to shrink
130  * costs for a txrx bit, often by factors of around ten (by instruction
131  * count).  That is particularly visible for larger word sizes, but helps
132  * even with default 8-bit words.
133  *
134  * REVISIT overheads calling these functions for each word also have
135  * significant performance costs.  Having txrx_bufs() calls that inline
136  * the txrx_word() logic would help performance, e.g. on larger blocks
137  * used with flash storage or MMC/SD.  There should also be ways to make
138  * GCC be less stupid about reloading registers inside the I/O loops,
139  * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
140  */
141
142 static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
143                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
144 {
145         if (unlikely(spi->mode & SPI_LSB_FIRST))
146                 return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
147         else
148                 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
149 }
150
151 static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
152                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
153 {
154         if (unlikely(spi->mode & SPI_LSB_FIRST))
155                 return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
156         else
157                 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
158 }
159
160 static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
161                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
162 {
163         if (unlikely(spi->mode & SPI_LSB_FIRST))
164                 return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
165         else
166                 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
167 }
168
169 static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
170                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
171 {
172         if (unlikely(spi->mode & SPI_LSB_FIRST))
173                 return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
174         else
175                 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
176 }
177
178 /*
179  * These functions do not call setmosi or getmiso if respective flag
180  * (SPI_CONTROLLER_NO_RX or SPI_CONTROLLER_NO_TX) is set, so they are safe to
181  * call when such pin is not present or defined in the controller.
182  * A separate set of callbacks is defined to get highest possible
183  * speed in the generic case (when both MISO and MOSI lines are
184  * available), as optimiser will remove the checks when argument is
185  * constant.
186  */
187
188 static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
189                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
190 {
191         flags = spi->controller->flags;
192         if (unlikely(spi->mode & SPI_LSB_FIRST))
193                 return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
194         else
195                 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
196 }
197
198 static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
199                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
200 {
201         flags = spi->controller->flags;
202         if (unlikely(spi->mode & SPI_LSB_FIRST))
203                 return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
204         else
205                 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
206 }
207
208 static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
209                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
210 {
211         flags = spi->controller->flags;
212         if (unlikely(spi->mode & SPI_LSB_FIRST))
213                 return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
214         else
215                 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
216 }
217
218 static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
219                 unsigned nsecs, u32 word, u8 bits, unsigned flags)
220 {
221         flags = spi->controller->flags;
222         if (unlikely(spi->mode & SPI_LSB_FIRST))
223                 return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
224         else
225                 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
226 }
227
228 /*----------------------------------------------------------------------*/
229
230 static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
231 {
232         struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
233
234         /* set initial clock line level */
235         if (is_active) {
236                 if (spi_gpio->sck_idle_input)
237                         gpiod_direction_output(spi_gpio->sck, spi->mode & SPI_CPOL);
238                 else
239                         gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
240         }
241
242         /*
243          * Drive chip select line, if we have one.
244          * SPI chip selects are normally active-low, but when
245          * cs_dont_invert is set, we assume their polarity is
246          * controlled by the GPIO, and write '1' to assert.
247          */
248         if (spi_gpio->cs_gpios) {
249                 struct gpio_desc *cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
250                 int val = ((spi->mode & SPI_CS_HIGH) || spi_gpio->cs_dont_invert) ?
251                         is_active : !is_active;
252
253                 gpiod_set_value_cansleep(cs, val);
254         }
255
256         if (spi_gpio->sck_idle_input && !is_active)
257                 gpiod_direction_input(spi_gpio->sck);
258 }
259
260 static int spi_gpio_setup(struct spi_device *spi)
261 {
262         struct gpio_desc        *cs;
263         int                     status = 0;
264         struct spi_gpio         *spi_gpio = spi_to_spi_gpio(spi);
265
266         /*
267          * The CS GPIOs have already been
268          * initialized from the descriptor lookup.
269          * Here we set them to the non-asserted state.
270          */
271         if (spi_gpio->cs_gpios) {
272                 cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
273                 if (!spi->controller_state && cs)
274                         status = gpiod_direction_output(cs,
275                                                         !((spi->mode & SPI_CS_HIGH) ||
276                                                            spi_gpio->cs_dont_invert));
277         }
278
279         if (!status)
280                 status = spi_bitbang_setup(spi);
281
282         return status;
283 }
284
285 static int spi_gpio_set_direction(struct spi_device *spi, bool output)
286 {
287         struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
288         int ret;
289
290         if (output)
291                 return gpiod_direction_output(spi_gpio->mosi, 1);
292
293         /*
294          * Only change MOSI to an input if using 3WIRE mode.
295          * Otherwise, MOSI could be left floating if there is
296          * no pull resistor connected to the I/O pin, or could
297          * be left logic high if there is a pull-up. Transmitting
298          * logic high when only clocking MISO data in can put some
299          * SPI devices in to a bad state.
300          */
301         if (spi->mode & SPI_3WIRE) {
302                 ret = gpiod_direction_input(spi_gpio->mosi);
303                 if (ret)
304                         return ret;
305         }
306         /*
307          * Send a turnaround high impedance cycle when switching
308          * from output to input. Theoretically there should be
309          * a clock delay here, but as has been noted above, the
310          * nsec delay function for bit-banged GPIO is simply
311          * {} because bit-banging just doesn't get fast enough
312          * anyway.
313          */
314         if (spi->mode & SPI_3WIRE_HIZ) {
315                 gpiod_set_value_cansleep(spi_gpio->sck,
316                                          !(spi->mode & SPI_CPOL));
317                 gpiod_set_value_cansleep(spi_gpio->sck,
318                                          !!(spi->mode & SPI_CPOL));
319         }
320         return 0;
321 }
322
323 static void spi_gpio_cleanup(struct spi_device *spi)
324 {
325         spi_bitbang_cleanup(spi);
326 }
327
328 /*
329  * It can be convenient to use this driver with pins that have alternate
330  * functions associated with a "native" SPI controller if a driver for that
331  * controller is not available, or is missing important functionality.
332  *
333  * On platforms which can do so, configure MISO with a weak pullup unless
334  * there's an external pullup on that signal.  That saves power by avoiding
335  * floating signals.  (A weak pulldown would save power too, but many
336  * drivers expect to see all-ones data as the no target "response".)
337  */
338 static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
339 {
340         spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
341         if (IS_ERR(spi_gpio->mosi))
342                 return PTR_ERR(spi_gpio->mosi);
343
344         spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
345         if (IS_ERR(spi_gpio->miso))
346                 return PTR_ERR(spi_gpio->miso);
347
348         spi_gpio->sck_idle_input = device_property_read_bool(dev, "sck-idle-input");
349         spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
350         return PTR_ERR_OR_ZERO(spi_gpio->sck);
351 }
352
353 /*
354  * In order to implement "sck-idle-input" (which requires SCK
355  * direction and CS level to be switched in a particular order),
356  * we need to control GPIO chip selects from within this driver.
357  */
358
359 static int spi_gpio_probe_get_cs_gpios(struct device *dev,
360                                        struct spi_master *master,
361                                        bool gpio_defines_polarity)
362 {
363         int i;
364         struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
365
366         spi_gpio->cs_dont_invert = gpio_defines_polarity;
367         spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
368                                           sizeof(*spi_gpio->cs_gpios),
369                                           GFP_KERNEL);
370         if (!spi_gpio->cs_gpios)
371                 return -ENOMEM;
372
373         for (i = 0; i < master->num_chipselect; i++) {
374                 spi_gpio->cs_gpios[i] =
375                         devm_gpiod_get_index(dev, "cs", i,
376                                              gpio_defines_polarity ?
377                                                 GPIOD_OUT_LOW : GPIOD_OUT_HIGH);
378                 if (IS_ERR(spi_gpio->cs_gpios[i]))
379                         return PTR_ERR(spi_gpio->cs_gpios[i]);
380         }
381
382         return 0;
383 }
384
385 #ifdef CONFIG_OF
386 static const struct of_device_id spi_gpio_dt_ids[] = {
387         { .compatible = "spi-gpio" },
388         {}
389 };
390 MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
391
392 static int spi_gpio_probe_dt(struct platform_device *pdev,
393                              struct spi_controller *host)
394 {
395         struct device *dev = &pdev->dev;
396
397         host->dev.of_node = dev->of_node;
398         host->num_chipselect = gpiod_count(dev, "cs");
399
400         return spi_gpio_probe_get_cs_gpios(dev, host, true);
401 }
402 #else
403 static inline int spi_gpio_probe_dt(struct platform_device *pdev,
404                                     struct spi_controller *host)
405 {
406         return 0;
407 }
408 #endif
409
410 static int spi_gpio_probe_pdata(struct platform_device *pdev,
411                                 struct spi_controller *host)
412 {
413         struct device *dev = &pdev->dev;
414         struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
415
416 #ifdef GENERIC_BITBANG
417         if (!pdata || !pdata->num_chipselect)
418                 return -ENODEV;
419 #endif
420         /*
421          * The host needs to think there is a chipselect even if not
422          * connected
423          */
424         host->num_chipselect = pdata->num_chipselect ?: 1;
425
426         return spi_gpio_probe_get_cs_gpios(dev, host, false);
427 }
428
429 static int spi_gpio_probe(struct platform_device *pdev)
430 {
431         int                             status;
432         struct spi_controller           *host;
433         struct spi_gpio                 *spi_gpio;
434         struct device                   *dev = &pdev->dev;
435         struct spi_bitbang              *bb;
436
437         host = devm_spi_alloc_host(dev, sizeof(*spi_gpio));
438         if (!host)
439                 return -ENOMEM;
440
441         if (pdev->dev.of_node)
442                 status = spi_gpio_probe_dt(pdev, host);
443         else
444                 status = spi_gpio_probe_pdata(pdev, host);
445
446         if (status)
447                 return status;
448
449         spi_gpio = spi_controller_get_devdata(host);
450
451         status = spi_gpio_request(dev, spi_gpio);
452         if (status)
453                 return status;
454
455         host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
456         host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
457                             SPI_CS_HIGH | SPI_LSB_FIRST;
458         if (!spi_gpio->mosi) {
459                 /* HW configuration without MOSI pin
460                  *
461                  * No setting SPI_CONTROLLER_NO_RX here - if there is only
462                  * a MOSI pin connected the host can still do RX by
463                  * changing the direction of the line.
464                  */
465                 host->flags = SPI_CONTROLLER_NO_TX;
466         }
467
468         host->bus_num = pdev->id;
469         host->setup = spi_gpio_setup;
470         host->cleanup = spi_gpio_cleanup;
471
472         bb = &spi_gpio->bitbang;
473         bb->master = host;
474         /*
475          * There is some additional business, apart from driving the CS GPIO
476          * line, that we need to do on selection. This makes the local
477          * callback for chipselect always get called.
478          */
479         host->flags |= SPI_CONTROLLER_GPIO_SS;
480         bb->chipselect = spi_gpio_chipselect;
481         bb->set_line_direction = spi_gpio_set_direction;
482
483         if (host->flags & SPI_CONTROLLER_NO_TX) {
484                 bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
485                 bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
486                 bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
487                 bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
488         } else {
489                 bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
490                 bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
491                 bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
492                 bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
493         }
494         bb->setup_transfer = spi_bitbang_setup_transfer;
495
496         status = spi_bitbang_init(&spi_gpio->bitbang);
497         if (status)
498                 return status;
499
500         return devm_spi_register_controller(&pdev->dev, host);
501 }
502
503 MODULE_ALIAS("platform:" DRIVER_NAME);
504
505 static struct platform_driver spi_gpio_driver = {
506         .driver = {
507                 .name   = DRIVER_NAME,
508                 .of_match_table = of_match_ptr(spi_gpio_dt_ids),
509         },
510         .probe          = spi_gpio_probe,
511 };
512 module_platform_driver(spi_gpio_driver);
513
514 MODULE_DESCRIPTION("SPI host driver using generic bitbanged GPIO ");
515 MODULE_AUTHOR("David Brownell");
516 MODULE_LICENSE("GPL");