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