xref: /linux/drivers/spi/spi-gpio.c (revision ed7171ff9fabc49ae6ed42fbd082a576473836fc)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
320becf43SYang Yingliang  * SPI host driver using generic bitbanged GPIO
4ca632f55SGrant Likely  *
5ca632f55SGrant Likely  * Copyright (C) 2006,2008 David Brownell
69b00bc7bSLinus Walleij  * Copyright (C) 2017 Linus Walleij
7ca632f55SGrant Likely  */
804518cd8SAndy Shevchenko #include <linux/gpio/consumer.h>
9ca632f55SGrant Likely #include <linux/kernel.h>
1004518cd8SAndy Shevchenko #include <linux/mod_devicetable.h>
11d7614de4SPaul Gortmaker #include <linux/module.h>
12ca632f55SGrant Likely #include <linux/platform_device.h>
1304518cd8SAndy Shevchenko #include <linux/property.h>
14ca632f55SGrant Likely 
15ca632f55SGrant Likely #include <linux/spi/spi.h>
16ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h>
17ca632f55SGrant Likely #include <linux/spi/spi_gpio.h>
18ca632f55SGrant Likely 
19ca632f55SGrant Likely /*
2020becf43SYang Yingliang  * This bitbanging SPI host driver should help make systems usable
21ca632f55SGrant Likely  * when a native hardware SPI engine is not available, perhaps because
22ca632f55SGrant Likely  * its driver isn't yet working or because the I/O pins it requires
23ca632f55SGrant Likely  * are used for other purposes.
24ca632f55SGrant Likely  *
25ca632f55SGrant Likely  * platform_device->driver_data ... points to spi_gpio
26ca632f55SGrant Likely  *
27ca632f55SGrant Likely  * spi->controller_state ... reserved for bitbang framework code
28ca632f55SGrant Likely  *
2920becf43SYang Yingliang  * spi->controller->dev.driver_data ... points to spi_gpio->bitbang
30ca632f55SGrant Likely  */
31ca632f55SGrant Likely 
32ca632f55SGrant Likely struct spi_gpio {
33ca632f55SGrant Likely 	struct spi_bitbang		bitbang;
349b00bc7bSLinus Walleij 	struct gpio_desc		*sck;
359b00bc7bSLinus Walleij 	struct gpio_desc		*miso;
369b00bc7bSLinus Walleij 	struct gpio_desc		*mosi;
379b00bc7bSLinus Walleij 	struct gpio_desc		**cs_gpios;
38ca632f55SGrant Likely };
39ca632f55SGrant Likely 
40ca632f55SGrant Likely /*----------------------------------------------------------------------*/
41ca632f55SGrant Likely 
42ca632f55SGrant Likely /*
43ca632f55SGrant Likely  * Because the overhead of going through four GPIO procedure calls
44ca632f55SGrant Likely  * per transferred bit can make performance a problem, this code
45ca632f55SGrant Likely  * is set up so that you can use it in either of two ways:
46ca632f55SGrant Likely  *
47ca632f55SGrant Likely  *   - The slow generic way:  set up platform_data to hold the GPIO
48ca632f55SGrant Likely  *     numbers used for MISO/MOSI/SCK, and issue procedure calls for
49ca632f55SGrant Likely  *     each of them.  This driver can handle several such busses.
50ca632f55SGrant Likely  *
51ca632f55SGrant Likely  *   - The quicker inlined way:  only helps with platform GPIO code
52ca632f55SGrant Likely  *     that inlines operations for constant GPIOs.  This can give
53ca632f55SGrant Likely  *     you tight (fast!) inner loops, but each such bus needs a
54ca632f55SGrant Likely  *     new driver.  You'll define a new C file, with Makefile and
55ca632f55SGrant Likely  *     Kconfig support; the C code can be a total of six lines:
56ca632f55SGrant Likely  *
57ca632f55SGrant Likely  *		#define DRIVER_NAME	"myboard_spi2"
58ca632f55SGrant Likely  *		#define	SPI_MISO_GPIO	119
59ca632f55SGrant Likely  *		#define	SPI_MOSI_GPIO	120
60ca632f55SGrant Likely  *		#define	SPI_SCK_GPIO	121
61ca632f55SGrant Likely  *		#define	SPI_N_CHIPSEL	4
62ca632f55SGrant Likely  *		#include "spi-gpio.c"
63ca632f55SGrant Likely  */
64ca632f55SGrant Likely 
65ca632f55SGrant Likely #ifndef DRIVER_NAME
66ca632f55SGrant Likely #define DRIVER_NAME	"spi_gpio"
67ca632f55SGrant Likely 
68ca632f55SGrant Likely #define GENERIC_BITBANG	/* vs tight inlines */
69ca632f55SGrant Likely 
70ca632f55SGrant Likely #endif
71ca632f55SGrant Likely 
72ca632f55SGrant Likely /*----------------------------------------------------------------------*/
73ca632f55SGrant Likely 
74161c2dd3SDaniel Mack static inline struct spi_gpio *__pure
75161c2dd3SDaniel Mack spi_to_spi_gpio(const struct spi_device *spi)
76ca632f55SGrant Likely {
77ca632f55SGrant Likely 	const struct spi_bitbang	*bang;
78161c2dd3SDaniel Mack 	struct spi_gpio			*spi_gpio;
79ca632f55SGrant Likely 
8020becf43SYang Yingliang 	bang = spi_controller_get_devdata(spi->controller);
81ca632f55SGrant Likely 	spi_gpio = container_of(bang, struct spi_gpio, bitbang);
82161c2dd3SDaniel Mack 	return spi_gpio;
83161c2dd3SDaniel Mack }
84161c2dd3SDaniel Mack 
859b00bc7bSLinus Walleij /* These helpers are in turn called by the bitbang inlines */
86ca632f55SGrant Likely static inline void setsck(const struct spi_device *spi, int is_on)
87ca632f55SGrant Likely {
889b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
899b00bc7bSLinus Walleij 
909b00bc7bSLinus Walleij 	gpiod_set_value_cansleep(spi_gpio->sck, is_on);
91ca632f55SGrant Likely }
92ca632f55SGrant Likely 
93ca632f55SGrant Likely static inline void setmosi(const struct spi_device *spi, int is_on)
94ca632f55SGrant Likely {
959b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
969b00bc7bSLinus Walleij 
979b00bc7bSLinus Walleij 	gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
98ca632f55SGrant Likely }
99ca632f55SGrant Likely 
100ca632f55SGrant Likely static inline int getmiso(const struct spi_device *spi)
101ca632f55SGrant Likely {
1029b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
103ca632f55SGrant Likely 
1044b859db2SLorenzo Bianconi 	if (spi->mode & SPI_3WIRE)
1054b859db2SLorenzo Bianconi 		return !!gpiod_get_value_cansleep(spi_gpio->mosi);
1064b859db2SLorenzo Bianconi 	else
1079b00bc7bSLinus Walleij 		return !!gpiod_get_value_cansleep(spi_gpio->miso);
1089b00bc7bSLinus Walleij }
109ca632f55SGrant Likely 
110ca632f55SGrant Likely /*
111ca632f55SGrant Likely  * NOTE:  this clocks "as fast as we can".  It "should" be a function of the
112ca632f55SGrant Likely  * requested device clock.  Software overhead means we usually have trouble
113ca632f55SGrant Likely  * reaching even one Mbit/sec (except when we can inline bitops), so for now
114ca632f55SGrant Likely  * we'll just assume we never need additional per-bit slowdowns.
115ca632f55SGrant Likely  */
116ca632f55SGrant Likely #define spidelay(nsecs)	do {} while (0)
117ca632f55SGrant Likely 
118ca632f55SGrant Likely #include "spi-bitbang-txrx.h"
119ca632f55SGrant Likely 
120ca632f55SGrant Likely /*
121ca632f55SGrant Likely  * These functions can leverage inline expansion of GPIO calls to shrink
122ca632f55SGrant Likely  * costs for a txrx bit, often by factors of around ten (by instruction
123ca632f55SGrant Likely  * count).  That is particularly visible for larger word sizes, but helps
124ca632f55SGrant Likely  * even with default 8-bit words.
125ca632f55SGrant Likely  *
126ca632f55SGrant Likely  * REVISIT overheads calling these functions for each word also have
127ca632f55SGrant Likely  * significant performance costs.  Having txrx_bufs() calls that inline
128ca632f55SGrant Likely  * the txrx_word() logic would help performance, e.g. on larger blocks
129ca632f55SGrant Likely  * used with flash storage or MMC/SD.  There should also be ways to make
130ca632f55SGrant Likely  * GCC be less stupid about reloading registers inside the I/O loops,
131ca632f55SGrant Likely  * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
132ca632f55SGrant Likely  */
133ca632f55SGrant Likely 
134ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
135304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
136ca632f55SGrant Likely {
1371847e304SAndreas Färber 	if (unlikely(spi->mode & SPI_LSB_FIRST))
1381847e304SAndreas Färber 		return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
1391847e304SAndreas Färber 	else
140304d3436SLorenzo Bianconi 		return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
141ca632f55SGrant Likely }
142ca632f55SGrant Likely 
143ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
144304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
145ca632f55SGrant Likely {
1461847e304SAndreas Färber 	if (unlikely(spi->mode & SPI_LSB_FIRST))
1471847e304SAndreas Färber 		return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
1481847e304SAndreas Färber 	else
149304d3436SLorenzo Bianconi 		return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
150ca632f55SGrant Likely }
151ca632f55SGrant Likely 
152ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
153304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
154ca632f55SGrant Likely {
1551847e304SAndreas Färber 	if (unlikely(spi->mode & SPI_LSB_FIRST))
1561847e304SAndreas Färber 		return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
1571847e304SAndreas Färber 	else
158304d3436SLorenzo Bianconi 		return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
159ca632f55SGrant Likely }
160ca632f55SGrant Likely 
161ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
162304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
163ca632f55SGrant Likely {
1641847e304SAndreas Färber 	if (unlikely(spi->mode & SPI_LSB_FIRST))
1651847e304SAndreas Färber 		return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
1661847e304SAndreas Färber 	else
167304d3436SLorenzo Bianconi 		return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
168ca632f55SGrant Likely }
169ca632f55SGrant Likely 
170ca632f55SGrant Likely /*
171ca632f55SGrant Likely  * These functions do not call setmosi or getmiso if respective flag
172c397f09eSAndy Shevchenko  * (SPI_CONTROLLER_NO_RX or SPI_CONTROLLER_NO_TX) is set, so they are safe to
173ca632f55SGrant Likely  * call when such pin is not present or defined in the controller.
174ca632f55SGrant Likely  * A separate set of callbacks is defined to get highest possible
175ca632f55SGrant Likely  * speed in the generic case (when both MISO and MOSI lines are
176ca632f55SGrant Likely  * available), as optimiser will remove the checks when argument is
177ca632f55SGrant Likely  * constant.
178ca632f55SGrant Likely  */
179ca632f55SGrant Likely 
180ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
181304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
182ca632f55SGrant Likely {
18320becf43SYang Yingliang 	flags = spi->controller->flags;
1841847e304SAndreas Färber 	if (unlikely(spi->mode & SPI_LSB_FIRST))
1851847e304SAndreas Färber 		return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
1861847e304SAndreas Färber 	else
187ca632f55SGrant Likely 		return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
188ca632f55SGrant Likely }
189ca632f55SGrant Likely 
190ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
191304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
192ca632f55SGrant Likely {
19320becf43SYang Yingliang 	flags = spi->controller->flags;
1941847e304SAndreas Färber 	if (unlikely(spi->mode & SPI_LSB_FIRST))
1951847e304SAndreas Färber 		return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
1961847e304SAndreas Färber 	else
197ca632f55SGrant Likely 		return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
198ca632f55SGrant Likely }
199ca632f55SGrant Likely 
200ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
201304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
202ca632f55SGrant Likely {
20320becf43SYang Yingliang 	flags = spi->controller->flags;
2041847e304SAndreas Färber 	if (unlikely(spi->mode & SPI_LSB_FIRST))
2051847e304SAndreas Färber 		return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
2061847e304SAndreas Färber 	else
207ca632f55SGrant Likely 		return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
208ca632f55SGrant Likely }
209ca632f55SGrant Likely 
210ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
211304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
212ca632f55SGrant Likely {
21320becf43SYang Yingliang 	flags = spi->controller->flags;
2141847e304SAndreas Färber 	if (unlikely(spi->mode & SPI_LSB_FIRST))
2151847e304SAndreas Färber 		return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
2161847e304SAndreas Färber 	else
217ca632f55SGrant Likely 		return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
218ca632f55SGrant Likely }
219ca632f55SGrant Likely 
220ca632f55SGrant Likely /*----------------------------------------------------------------------*/
221ca632f55SGrant Likely 
222ca632f55SGrant Likely static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
223ca632f55SGrant Likely {
224161c2dd3SDaniel Mack 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
225ca632f55SGrant Likely 
2269b00bc7bSLinus Walleij 	/* set initial clock line level */
227ca632f55SGrant Likely 	if (is_active)
2289b00bc7bSLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
229ca632f55SGrant Likely 
2309b00bc7bSLinus Walleij 	/* Drive chip select line, if we have one */
231249e2632SAndrey Smirnov 	if (spi_gpio->cs_gpios) {
2329e264f3fSAmit Kumar Mahapatra via Alsa-devel 		struct gpio_desc *cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
2339b00bc7bSLinus Walleij 
2349b00bc7bSLinus Walleij 		/* SPI chip selects are normally active-low */
2359b00bc7bSLinus Walleij 		gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
236ca632f55SGrant Likely 	}
237ca632f55SGrant Likely }
238ca632f55SGrant Likely 
239ca632f55SGrant Likely static int spi_gpio_setup(struct spi_device *spi)
240ca632f55SGrant Likely {
2419b00bc7bSLinus Walleij 	struct gpio_desc	*cs;
242161c2dd3SDaniel Mack 	struct spi_gpio		*spi_gpio = spi_to_spi_gpio(spi);
243*196bf3e7SAndy Shevchenko 	int ret;
244ca632f55SGrant Likely 
24538ab18caSDaniel Mack 	/*
2469b00bc7bSLinus Walleij 	 * The CS GPIOs have already been
2479b00bc7bSLinus Walleij 	 * initialized from the descriptor lookup.
24838ab18caSDaniel Mack 	 */
249249e2632SAndrey Smirnov 	if (spi_gpio->cs_gpios) {
2509e264f3fSAmit Kumar Mahapatra via Alsa-devel 		cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
251*196bf3e7SAndy Shevchenko 		if (!spi->controller_state && cs) {
252*196bf3e7SAndy Shevchenko 			ret = gpiod_direction_output(cs, !(spi->mode & SPI_CS_HIGH));
253*196bf3e7SAndy Shevchenko 			if (ret)
254*196bf3e7SAndy Shevchenko 				return ret;
255*196bf3e7SAndy Shevchenko 		}
256249e2632SAndrey Smirnov 	}
257161c2dd3SDaniel Mack 
258*196bf3e7SAndy Shevchenko 	return spi_bitbang_setup(spi);
259ca632f55SGrant Likely }
260ca632f55SGrant Likely 
2614b859db2SLorenzo Bianconi static int spi_gpio_set_direction(struct spi_device *spi, bool output)
2624b859db2SLorenzo Bianconi {
2634b859db2SLorenzo Bianconi 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
2645132b3d2SLinus Walleij 	int ret;
2654b859db2SLorenzo Bianconi 
2664b859db2SLorenzo Bianconi 	if (output)
2674b859db2SLorenzo Bianconi 		return gpiod_direction_output(spi_gpio->mosi, 1);
2685132b3d2SLinus Walleij 
2693a6f994fSKris Bahnsen 	/*
2703a6f994fSKris Bahnsen 	 * Only change MOSI to an input if using 3WIRE mode.
2713a6f994fSKris Bahnsen 	 * Otherwise, MOSI could be left floating if there is
2723a6f994fSKris Bahnsen 	 * no pull resistor connected to the I/O pin, or could
2733a6f994fSKris Bahnsen 	 * be left logic high if there is a pull-up. Transmitting
2743a6f994fSKris Bahnsen 	 * logic high when only clocking MISO data in can put some
2753a6f994fSKris Bahnsen 	 * SPI devices in to a bad state.
2763a6f994fSKris Bahnsen 	 */
2773a6f994fSKris Bahnsen 	if (spi->mode & SPI_3WIRE) {
2785132b3d2SLinus Walleij 		ret = gpiod_direction_input(spi_gpio->mosi);
2795132b3d2SLinus Walleij 		if (ret)
2805132b3d2SLinus Walleij 			return ret;
2813a6f994fSKris Bahnsen 	}
2825132b3d2SLinus Walleij 	/*
2835132b3d2SLinus Walleij 	 * Send a turnaround high impedance cycle when switching
2845132b3d2SLinus Walleij 	 * from output to input. Theoretically there should be
2855132b3d2SLinus Walleij 	 * a clock delay here, but as has been noted above, the
2865132b3d2SLinus Walleij 	 * nsec delay function for bit-banged GPIO is simply
2875132b3d2SLinus Walleij 	 * {} because bit-banging just doesn't get fast enough
2885132b3d2SLinus Walleij 	 * anyway.
2895132b3d2SLinus Walleij 	 */
2905132b3d2SLinus Walleij 	if (spi->mode & SPI_3WIRE_HIZ) {
2915132b3d2SLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck,
2925132b3d2SLinus Walleij 					 !(spi->mode & SPI_CPOL));
2935132b3d2SLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck,
2945132b3d2SLinus Walleij 					 !!(spi->mode & SPI_CPOL));
2955132b3d2SLinus Walleij 	}
2965132b3d2SLinus Walleij 	return 0;
2974b859db2SLorenzo Bianconi }
2984b859db2SLorenzo Bianconi 
299ca632f55SGrant Likely static void spi_gpio_cleanup(struct spi_device *spi)
300ca632f55SGrant Likely {
301ca632f55SGrant Likely 	spi_bitbang_cleanup(spi);
302ca632f55SGrant Likely }
303ca632f55SGrant Likely 
3049b00bc7bSLinus Walleij /*
3059b00bc7bSLinus Walleij  * It can be convenient to use this driver with pins that have alternate
3069b00bc7bSLinus Walleij  * functions associated with a "native" SPI controller if a driver for that
3079b00bc7bSLinus Walleij  * controller is not available, or is missing important functionality.
3089b00bc7bSLinus Walleij  *
3099b00bc7bSLinus Walleij  * On platforms which can do so, configure MISO with a weak pullup unless
3109b00bc7bSLinus Walleij  * there's an external pullup on that signal.  That saves power by avoiding
3119b00bc7bSLinus Walleij  * floating signals.  (A weak pulldown would save power too, but many
31220becf43SYang Yingliang  * drivers expect to see all-ones data as the no target "response".)
3139b00bc7bSLinus Walleij  */
3145c8283c1SAndrey Smirnov static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
315ca632f55SGrant Likely {
3169b00bc7bSLinus Walleij 	spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
3179b00bc7bSLinus Walleij 	if (IS_ERR(spi_gpio->mosi))
3189b00bc7bSLinus Walleij 		return PTR_ERR(spi_gpio->mosi);
319ca632f55SGrant Likely 
3209b00bc7bSLinus Walleij 	spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
3219b00bc7bSLinus Walleij 	if (IS_ERR(spi_gpio->miso))
3229b00bc7bSLinus Walleij 		return PTR_ERR(spi_gpio->miso);
3239b00bc7bSLinus Walleij 
3249b00bc7bSLinus Walleij 	spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
3258995673eSMarkus Elfring 	return PTR_ERR_OR_ZERO(spi_gpio->sck);
326ca632f55SGrant Likely }
327ca632f55SGrant Likely 
328249e2632SAndrey Smirnov static int spi_gpio_probe_pdata(struct platform_device *pdev,
32920becf43SYang Yingliang 				struct spi_controller *host)
330249e2632SAndrey Smirnov {
331249e2632SAndrey Smirnov 	struct device *dev = &pdev->dev;
332249e2632SAndrey Smirnov 	struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
33320becf43SYang Yingliang 	struct spi_gpio *spi_gpio = spi_controller_get_devdata(host);
334249e2632SAndrey Smirnov 	int i;
335249e2632SAndrey Smirnov 
336249e2632SAndrey Smirnov #ifdef GENERIC_BITBANG
337249e2632SAndrey Smirnov 	if (!pdata || !pdata->num_chipselect)
338249e2632SAndrey Smirnov 		return -ENODEV;
339249e2632SAndrey Smirnov #endif
340249e2632SAndrey Smirnov 	/*
34120becf43SYang Yingliang 	 * The host needs to think there is a chipselect even if not
342249e2632SAndrey Smirnov 	 * connected
343249e2632SAndrey Smirnov 	 */
34420becf43SYang Yingliang 	host->num_chipselect = pdata->num_chipselect ?: 1;
345249e2632SAndrey Smirnov 
34620becf43SYang Yingliang 	spi_gpio->cs_gpios = devm_kcalloc(dev, host->num_chipselect,
347249e2632SAndrey Smirnov 					  sizeof(*spi_gpio->cs_gpios),
348249e2632SAndrey Smirnov 					  GFP_KERNEL);
349249e2632SAndrey Smirnov 	if (!spi_gpio->cs_gpios)
350249e2632SAndrey Smirnov 		return -ENOMEM;
351249e2632SAndrey Smirnov 
35220becf43SYang Yingliang 	for (i = 0; i < host->num_chipselect; i++) {
353249e2632SAndrey Smirnov 		spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
354249e2632SAndrey Smirnov 							     GPIOD_OUT_HIGH);
355249e2632SAndrey Smirnov 		if (IS_ERR(spi_gpio->cs_gpios[i]))
356249e2632SAndrey Smirnov 			return PTR_ERR(spi_gpio->cs_gpios[i]);
357249e2632SAndrey Smirnov 	}
358249e2632SAndrey Smirnov 
359249e2632SAndrey Smirnov 	return 0;
360249e2632SAndrey Smirnov }
361249e2632SAndrey Smirnov 
362fd4a319bSGrant Likely static int spi_gpio_probe(struct platform_device *pdev)
363ca632f55SGrant Likely {
364ca632f55SGrant Likely 	int				status;
36520becf43SYang Yingliang 	struct spi_controller		*host;
366ca632f55SGrant Likely 	struct spi_gpio			*spi_gpio;
36796cad6d7SAndrey Smirnov 	struct device			*dev = &pdev->dev;
36804518cd8SAndy Shevchenko 	struct fwnode_handle		*fwnode = dev_fwnode(dev);
36915dd0e9eSAndrey Smirnov 	struct spi_bitbang		*bb;
370ca632f55SGrant Likely 
37120becf43SYang Yingliang 	host = devm_spi_alloc_host(dev, sizeof(*spi_gpio));
37220becf43SYang Yingliang 	if (!host)
3739b00bc7bSLinus Walleij 		return -ENOMEM;
374d1d81802STorsten Fleischer 
37504518cd8SAndy Shevchenko 	if (fwnode) {
37604518cd8SAndy Shevchenko 		device_set_node(&host->dev, fwnode);
37704518cd8SAndy Shevchenko 		host->use_gpio_descriptors = true;
37804518cd8SAndy Shevchenko 	} else {
37920becf43SYang Yingliang 		status = spi_gpio_probe_pdata(pdev, host);
380249e2632SAndrey Smirnov 		if (status)
381249e2632SAndrey Smirnov 			return status;
38204518cd8SAndy Shevchenko 	}
383249e2632SAndrey Smirnov 
38420becf43SYang Yingliang 	spi_gpio = spi_controller_get_devdata(host);
3859b00bc7bSLinus Walleij 
3865c8283c1SAndrey Smirnov 	status = spi_gpio_request(dev, spi_gpio);
3879b00bc7bSLinus Walleij 	if (status)
3889b00bc7bSLinus Walleij 		return status;
3899b00bc7bSLinus Walleij 
39020becf43SYang Yingliang 	host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
39120becf43SYang Yingliang 	host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
3921847e304SAndreas Färber 			    SPI_CS_HIGH | SPI_LSB_FIRST;
3935c8283c1SAndrey Smirnov 	if (!spi_gpio->mosi) {
3945c8283c1SAndrey Smirnov 		/* HW configuration without MOSI pin
3955c8283c1SAndrey Smirnov 		 *
396c397f09eSAndy Shevchenko 		 * No setting SPI_CONTROLLER_NO_RX here - if there is only
3975c8283c1SAndrey Smirnov 		 * a MOSI pin connected the host can still do RX by
3985c8283c1SAndrey Smirnov 		 * changing the direction of the line.
3995c8283c1SAndrey Smirnov 		 */
40020becf43SYang Yingliang 		host->flags = SPI_CONTROLLER_NO_TX;
4015c8283c1SAndrey Smirnov 	}
4025c8283c1SAndrey Smirnov 
40320becf43SYang Yingliang 	host->bus_num = pdev->id;
40420becf43SYang Yingliang 	host->setup = spi_gpio_setup;
40520becf43SYang Yingliang 	host->cleanup = spi_gpio_cleanup;
406249e2632SAndrey Smirnov 
40715dd0e9eSAndrey Smirnov 	bb = &spi_gpio->bitbang;
40822592331SUwe Kleine-König 	bb->ctlr = host;
4092922d1ccSLinus Walleij 	/*
4102922d1ccSLinus Walleij 	 * There is some additional business, apart from driving the CS GPIO
4112922d1ccSLinus Walleij 	 * line, that we need to do on selection. This makes the local
4122922d1ccSLinus Walleij 	 * callback for chipselect always get called.
4132922d1ccSLinus Walleij 	 */
41420becf43SYang Yingliang 	host->flags |= SPI_CONTROLLER_GPIO_SS;
41515dd0e9eSAndrey Smirnov 	bb->chipselect = spi_gpio_chipselect;
41615dd0e9eSAndrey Smirnov 	bb->set_line_direction = spi_gpio_set_direction;
417ca632f55SGrant Likely 
41820becf43SYang Yingliang 	if (host->flags & SPI_CONTROLLER_NO_TX) {
41915dd0e9eSAndrey Smirnov 		bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
42015dd0e9eSAndrey Smirnov 		bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
42115dd0e9eSAndrey Smirnov 		bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
42215dd0e9eSAndrey Smirnov 		bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
42368cd9dc2SAndrey Smirnov 	} else {
42468cd9dc2SAndrey Smirnov 		bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
42568cd9dc2SAndrey Smirnov 		bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
42668cd9dc2SAndrey Smirnov 		bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
42768cd9dc2SAndrey Smirnov 		bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
428ca632f55SGrant Likely 	}
42915dd0e9eSAndrey Smirnov 	bb->setup_transfer = spi_bitbang_setup_transfer;
430ca632f55SGrant Likely 
43179567c1aSAndrey Smirnov 	status = spi_bitbang_init(&spi_gpio->bitbang);
43279567c1aSAndrey Smirnov 	if (status)
43379567c1aSAndrey Smirnov 		return status;
434ca632f55SGrant Likely 
43520becf43SYang Yingliang 	return devm_spi_register_controller(&pdev->dev, host);
436ca632f55SGrant Likely }
437ca632f55SGrant Likely 
438ca632f55SGrant Likely MODULE_ALIAS("platform:" DRIVER_NAME);
439ca632f55SGrant Likely 
44004518cd8SAndy Shevchenko static const struct of_device_id spi_gpio_dt_ids[] = {
44104518cd8SAndy Shevchenko 	{ .compatible = "spi-gpio" },
44204518cd8SAndy Shevchenko 	{}
44304518cd8SAndy Shevchenko };
44404518cd8SAndy Shevchenko MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
44504518cd8SAndy Shevchenko 
446ca632f55SGrant Likely static struct platform_driver spi_gpio_driver = {
44738ab18caSDaniel Mack 	.driver = {
44838ab18caSDaniel Mack 		.name	= DRIVER_NAME,
44904518cd8SAndy Shevchenko 		.of_match_table = spi_gpio_dt_ids,
45038ab18caSDaniel Mack 	},
451940ab889SGrant Likely 	.probe		= spi_gpio_probe,
452ca632f55SGrant Likely };
453940ab889SGrant Likely module_platform_driver(spi_gpio_driver);
454ca632f55SGrant Likely 
45520becf43SYang Yingliang MODULE_DESCRIPTION("SPI host driver using generic bitbanged GPIO ");
456ca632f55SGrant Likely MODULE_AUTHOR("David Brownell");
457ca632f55SGrant Likely MODULE_LICENSE("GPL");
458