xref: /openbmc/linux/drivers/spi/spi-gpio.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3*20becf43SYang 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  */
8ca632f55SGrant Likely #include <linux/kernel.h>
9d7614de4SPaul Gortmaker #include <linux/module.h>
10ca632f55SGrant Likely #include <linux/platform_device.h>
119b00bc7bSLinus Walleij #include <linux/gpio/consumer.h>
12ecc77773SSachin Kamat #include <linux/of.h>
13ca632f55SGrant Likely 
14ca632f55SGrant Likely #include <linux/spi/spi.h>
15ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h>
16ca632f55SGrant Likely #include <linux/spi/spi_gpio.h>
17ca632f55SGrant Likely 
18ca632f55SGrant Likely 
19ca632f55SGrant Likely /*
20*20becf43SYang 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  *
29*20becf43SYang 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
spi_to_spi_gpio(const struct spi_device * spi)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 
80*20becf43SYang 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 */
setsck(const struct spi_device * spi,int is_on)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 
setmosi(const struct spi_device * spi,int is_on)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 
getmiso(const struct spi_device * spi)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 
spi_gpio_txrx_word_mode0(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)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 
spi_gpio_txrx_word_mode1(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)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 
spi_gpio_txrx_word_mode2(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)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 
spi_gpio_txrx_word_mode3(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)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 
spi_gpio_spec_txrx_word_mode0(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)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 {
183*20becf43SYang 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 
spi_gpio_spec_txrx_word_mode1(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)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 {
193*20becf43SYang 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 
spi_gpio_spec_txrx_word_mode2(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)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 {
203*20becf43SYang 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 
spi_gpio_spec_txrx_word_mode3(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)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 {
213*20becf43SYang 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 
spi_gpio_chipselect(struct spi_device * spi,int is_active)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 
spi_gpio_setup(struct spi_device * spi)239ca632f55SGrant Likely static int spi_gpio_setup(struct spi_device *spi)
240ca632f55SGrant Likely {
2419b00bc7bSLinus Walleij 	struct gpio_desc	*cs;
242ca632f55SGrant Likely 	int			status = 0;
243161c2dd3SDaniel Mack 	struct spi_gpio		*spi_gpio = spi_to_spi_gpio(spi);
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)];
2519b00bc7bSLinus Walleij 		if (!spi->controller_state && cs)
2529b00bc7bSLinus Walleij 			status = gpiod_direction_output(cs,
25305644147SUwe Kleine-König 						  !(spi->mode & SPI_CS_HIGH));
254249e2632SAndrey Smirnov 	}
255161c2dd3SDaniel Mack 
2569b00bc7bSLinus Walleij 	if (!status)
2579b00bc7bSLinus Walleij 		status = spi_bitbang_setup(spi);
2589b00bc7bSLinus Walleij 
259ca632f55SGrant Likely 	return status;
260ca632f55SGrant Likely }
261ca632f55SGrant Likely 
spi_gpio_set_direction(struct spi_device * spi,bool output)2624b859db2SLorenzo Bianconi static int spi_gpio_set_direction(struct spi_device *spi, bool output)
2634b859db2SLorenzo Bianconi {
2644b859db2SLorenzo Bianconi 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
2655132b3d2SLinus Walleij 	int ret;
2664b859db2SLorenzo Bianconi 
2674b859db2SLorenzo Bianconi 	if (output)
2684b859db2SLorenzo Bianconi 		return gpiod_direction_output(spi_gpio->mosi, 1);
2695132b3d2SLinus Walleij 
2703a6f994fSKris Bahnsen 	/*
2713a6f994fSKris Bahnsen 	 * Only change MOSI to an input if using 3WIRE mode.
2723a6f994fSKris Bahnsen 	 * Otherwise, MOSI could be left floating if there is
2733a6f994fSKris Bahnsen 	 * no pull resistor connected to the I/O pin, or could
2743a6f994fSKris Bahnsen 	 * be left logic high if there is a pull-up. Transmitting
2753a6f994fSKris Bahnsen 	 * logic high when only clocking MISO data in can put some
2763a6f994fSKris Bahnsen 	 * SPI devices in to a bad state.
2773a6f994fSKris Bahnsen 	 */
2783a6f994fSKris Bahnsen 	if (spi->mode & SPI_3WIRE) {
2795132b3d2SLinus Walleij 		ret = gpiod_direction_input(spi_gpio->mosi);
2805132b3d2SLinus Walleij 		if (ret)
2815132b3d2SLinus Walleij 			return ret;
2823a6f994fSKris Bahnsen 	}
2835132b3d2SLinus Walleij 	/*
2845132b3d2SLinus Walleij 	 * Send a turnaround high impedance cycle when switching
2855132b3d2SLinus Walleij 	 * from output to input. Theoretically there should be
2865132b3d2SLinus Walleij 	 * a clock delay here, but as has been noted above, the
2875132b3d2SLinus Walleij 	 * nsec delay function for bit-banged GPIO is simply
2885132b3d2SLinus Walleij 	 * {} because bit-banging just doesn't get fast enough
2895132b3d2SLinus Walleij 	 * anyway.
2905132b3d2SLinus Walleij 	 */
2915132b3d2SLinus Walleij 	if (spi->mode & SPI_3WIRE_HIZ) {
2925132b3d2SLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck,
2935132b3d2SLinus Walleij 					 !(spi->mode & SPI_CPOL));
2945132b3d2SLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck,
2955132b3d2SLinus Walleij 					 !!(spi->mode & SPI_CPOL));
2965132b3d2SLinus Walleij 	}
2975132b3d2SLinus Walleij 	return 0;
2984b859db2SLorenzo Bianconi }
2994b859db2SLorenzo Bianconi 
spi_gpio_cleanup(struct spi_device * spi)300ca632f55SGrant Likely static void spi_gpio_cleanup(struct spi_device *spi)
301ca632f55SGrant Likely {
302ca632f55SGrant Likely 	spi_bitbang_cleanup(spi);
303ca632f55SGrant Likely }
304ca632f55SGrant Likely 
3059b00bc7bSLinus Walleij /*
3069b00bc7bSLinus Walleij  * It can be convenient to use this driver with pins that have alternate
3079b00bc7bSLinus Walleij  * functions associated with a "native" SPI controller if a driver for that
3089b00bc7bSLinus Walleij  * controller is not available, or is missing important functionality.
3099b00bc7bSLinus Walleij  *
3109b00bc7bSLinus Walleij  * On platforms which can do so, configure MISO with a weak pullup unless
3119b00bc7bSLinus Walleij  * there's an external pullup on that signal.  That saves power by avoiding
3129b00bc7bSLinus Walleij  * floating signals.  (A weak pulldown would save power too, but many
313*20becf43SYang Yingliang  * drivers expect to see all-ones data as the no target "response".)
3149b00bc7bSLinus Walleij  */
spi_gpio_request(struct device * dev,struct spi_gpio * spi_gpio)3155c8283c1SAndrey Smirnov static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
316ca632f55SGrant Likely {
3179b00bc7bSLinus Walleij 	spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
3189b00bc7bSLinus Walleij 	if (IS_ERR(spi_gpio->mosi))
3199b00bc7bSLinus Walleij 		return PTR_ERR(spi_gpio->mosi);
320ca632f55SGrant Likely 
3219b00bc7bSLinus Walleij 	spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
3229b00bc7bSLinus Walleij 	if (IS_ERR(spi_gpio->miso))
3239b00bc7bSLinus Walleij 		return PTR_ERR(spi_gpio->miso);
3249b00bc7bSLinus Walleij 
3259b00bc7bSLinus Walleij 	spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
3268995673eSMarkus Elfring 	return PTR_ERR_OR_ZERO(spi_gpio->sck);
327ca632f55SGrant Likely }
328ca632f55SGrant Likely 
32938ab18caSDaniel Mack #ifdef CONFIG_OF
330d9e15281SJingoo Han static const struct of_device_id spi_gpio_dt_ids[] = {
33138ab18caSDaniel Mack 	{ .compatible = "spi-gpio" },
33238ab18caSDaniel Mack 	{}
33338ab18caSDaniel Mack };
33438ab18caSDaniel Mack MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
33538ab18caSDaniel Mack 
spi_gpio_probe_dt(struct platform_device * pdev,struct spi_controller * host)336249e2632SAndrey Smirnov static int spi_gpio_probe_dt(struct platform_device *pdev,
337*20becf43SYang Yingliang 			     struct spi_controller *host)
33838ab18caSDaniel Mack {
339*20becf43SYang Yingliang 	host->dev.of_node = pdev->dev.of_node;
340*20becf43SYang Yingliang 	host->use_gpio_descriptors = true;
34138ab18caSDaniel Mack 
34238ab18caSDaniel Mack 	return 0;
34338ab18caSDaniel Mack }
34438ab18caSDaniel Mack #else
spi_gpio_probe_dt(struct platform_device * pdev,struct spi_controller * host)345249e2632SAndrey Smirnov static inline int spi_gpio_probe_dt(struct platform_device *pdev,
346*20becf43SYang Yingliang 				    struct spi_controller *host)
34738ab18caSDaniel Mack {
34838ab18caSDaniel Mack 	return 0;
34938ab18caSDaniel Mack }
35038ab18caSDaniel Mack #endif
35138ab18caSDaniel Mack 
spi_gpio_probe_pdata(struct platform_device * pdev,struct spi_controller * host)352249e2632SAndrey Smirnov static int spi_gpio_probe_pdata(struct platform_device *pdev,
353*20becf43SYang Yingliang 				struct spi_controller *host)
354249e2632SAndrey Smirnov {
355249e2632SAndrey Smirnov 	struct device *dev = &pdev->dev;
356249e2632SAndrey Smirnov 	struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
357*20becf43SYang Yingliang 	struct spi_gpio *spi_gpio = spi_controller_get_devdata(host);
358249e2632SAndrey Smirnov 	int i;
359249e2632SAndrey Smirnov 
360249e2632SAndrey Smirnov #ifdef GENERIC_BITBANG
361249e2632SAndrey Smirnov 	if (!pdata || !pdata->num_chipselect)
362249e2632SAndrey Smirnov 		return -ENODEV;
363249e2632SAndrey Smirnov #endif
364249e2632SAndrey Smirnov 	/*
365*20becf43SYang Yingliang 	 * The host needs to think there is a chipselect even if not
366249e2632SAndrey Smirnov 	 * connected
367249e2632SAndrey Smirnov 	 */
368*20becf43SYang Yingliang 	host->num_chipselect = pdata->num_chipselect ?: 1;
369249e2632SAndrey Smirnov 
370*20becf43SYang Yingliang 	spi_gpio->cs_gpios = devm_kcalloc(dev, host->num_chipselect,
371249e2632SAndrey Smirnov 					  sizeof(*spi_gpio->cs_gpios),
372249e2632SAndrey Smirnov 					  GFP_KERNEL);
373249e2632SAndrey Smirnov 	if (!spi_gpio->cs_gpios)
374249e2632SAndrey Smirnov 		return -ENOMEM;
375249e2632SAndrey Smirnov 
376*20becf43SYang Yingliang 	for (i = 0; i < host->num_chipselect; i++) {
377249e2632SAndrey Smirnov 		spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
378249e2632SAndrey Smirnov 							     GPIOD_OUT_HIGH);
379249e2632SAndrey Smirnov 		if (IS_ERR(spi_gpio->cs_gpios[i]))
380249e2632SAndrey Smirnov 			return PTR_ERR(spi_gpio->cs_gpios[i]);
381249e2632SAndrey Smirnov 	}
382249e2632SAndrey Smirnov 
383249e2632SAndrey Smirnov 	return 0;
384249e2632SAndrey Smirnov }
385249e2632SAndrey Smirnov 
spi_gpio_probe(struct platform_device * pdev)386fd4a319bSGrant Likely static int spi_gpio_probe(struct platform_device *pdev)
387ca632f55SGrant Likely {
388ca632f55SGrant Likely 	int				status;
389*20becf43SYang Yingliang 	struct spi_controller		*host;
390ca632f55SGrant Likely 	struct spi_gpio			*spi_gpio;
39196cad6d7SAndrey Smirnov 	struct device			*dev = &pdev->dev;
39215dd0e9eSAndrey Smirnov 	struct spi_bitbang		*bb;
393ca632f55SGrant Likely 
394*20becf43SYang Yingliang 	host = devm_spi_alloc_host(dev, sizeof(*spi_gpio));
395*20becf43SYang Yingliang 	if (!host)
3969b00bc7bSLinus Walleij 		return -ENOMEM;
397d1d81802STorsten Fleischer 
39862217f8bSStephen Boyd 	if (pdev->dev.of_node)
399*20becf43SYang Yingliang 		status = spi_gpio_probe_dt(pdev, host);
400249e2632SAndrey Smirnov 	else
401*20becf43SYang Yingliang 		status = spi_gpio_probe_pdata(pdev, host);
4029b00bc7bSLinus Walleij 
403249e2632SAndrey Smirnov 	if (status)
404249e2632SAndrey Smirnov 		return status;
405249e2632SAndrey Smirnov 
406*20becf43SYang Yingliang 	spi_gpio = spi_controller_get_devdata(host);
4079b00bc7bSLinus Walleij 
4085c8283c1SAndrey Smirnov 	status = spi_gpio_request(dev, spi_gpio);
4099b00bc7bSLinus Walleij 	if (status)
4109b00bc7bSLinus Walleij 		return status;
4119b00bc7bSLinus Walleij 
412*20becf43SYang Yingliang 	host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
413*20becf43SYang Yingliang 	host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
4141847e304SAndreas Färber 			    SPI_CS_HIGH | SPI_LSB_FIRST;
4155c8283c1SAndrey Smirnov 	if (!spi_gpio->mosi) {
4165c8283c1SAndrey Smirnov 		/* HW configuration without MOSI pin
4175c8283c1SAndrey Smirnov 		 *
418c397f09eSAndy Shevchenko 		 * No setting SPI_CONTROLLER_NO_RX here - if there is only
4195c8283c1SAndrey Smirnov 		 * a MOSI pin connected the host can still do RX by
4205c8283c1SAndrey Smirnov 		 * changing the direction of the line.
4215c8283c1SAndrey Smirnov 		 */
422*20becf43SYang Yingliang 		host->flags = SPI_CONTROLLER_NO_TX;
4235c8283c1SAndrey Smirnov 	}
4245c8283c1SAndrey Smirnov 
425*20becf43SYang Yingliang 	host->bus_num = pdev->id;
426*20becf43SYang Yingliang 	host->setup = spi_gpio_setup;
427*20becf43SYang Yingliang 	host->cleanup = spi_gpio_cleanup;
428249e2632SAndrey Smirnov 
42915dd0e9eSAndrey Smirnov 	bb = &spi_gpio->bitbang;
430*20becf43SYang Yingliang 	bb->master = host;
4312922d1ccSLinus Walleij 	/*
4322922d1ccSLinus Walleij 	 * There is some additional business, apart from driving the CS GPIO
4332922d1ccSLinus Walleij 	 * line, that we need to do on selection. This makes the local
4342922d1ccSLinus Walleij 	 * callback for chipselect always get called.
4352922d1ccSLinus Walleij 	 */
436*20becf43SYang Yingliang 	host->flags |= SPI_CONTROLLER_GPIO_SS;
43715dd0e9eSAndrey Smirnov 	bb->chipselect = spi_gpio_chipselect;
43815dd0e9eSAndrey Smirnov 	bb->set_line_direction = spi_gpio_set_direction;
439ca632f55SGrant Likely 
440*20becf43SYang Yingliang 	if (host->flags & SPI_CONTROLLER_NO_TX) {
44115dd0e9eSAndrey Smirnov 		bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
44215dd0e9eSAndrey Smirnov 		bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
44315dd0e9eSAndrey Smirnov 		bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
44415dd0e9eSAndrey Smirnov 		bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
44568cd9dc2SAndrey Smirnov 	} else {
44668cd9dc2SAndrey Smirnov 		bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
44768cd9dc2SAndrey Smirnov 		bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
44868cd9dc2SAndrey Smirnov 		bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
44968cd9dc2SAndrey Smirnov 		bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
450ca632f55SGrant Likely 	}
45115dd0e9eSAndrey Smirnov 	bb->setup_transfer = spi_bitbang_setup_transfer;
452ca632f55SGrant Likely 
45379567c1aSAndrey Smirnov 	status = spi_bitbang_init(&spi_gpio->bitbang);
45479567c1aSAndrey Smirnov 	if (status)
45579567c1aSAndrey Smirnov 		return status;
456ca632f55SGrant Likely 
457*20becf43SYang Yingliang 	return devm_spi_register_controller(&pdev->dev, host);
458ca632f55SGrant Likely }
459ca632f55SGrant Likely 
460ca632f55SGrant Likely MODULE_ALIAS("platform:" DRIVER_NAME);
461ca632f55SGrant Likely 
462ca632f55SGrant Likely static struct platform_driver spi_gpio_driver = {
46338ab18caSDaniel Mack 	.driver = {
46438ab18caSDaniel Mack 		.name	= DRIVER_NAME,
46538ab18caSDaniel Mack 		.of_match_table = of_match_ptr(spi_gpio_dt_ids),
46638ab18caSDaniel Mack 	},
467940ab889SGrant Likely 	.probe		= spi_gpio_probe,
468ca632f55SGrant Likely };
469940ab889SGrant Likely module_platform_driver(spi_gpio_driver);
470ca632f55SGrant Likely 
471*20becf43SYang Yingliang MODULE_DESCRIPTION("SPI host driver using generic bitbanged GPIO ");
472ca632f55SGrant Likely MODULE_AUTHOR("David Brownell");
473ca632f55SGrant Likely MODULE_LICENSE("GPL");
474