xref: /linux/drivers/spi/spi-gpio.c (revision 96cad6d78f7f8feac456002020971cba6073bff9)
1ca632f55SGrant Likely /*
2ca632f55SGrant Likely  * SPI master driver using generic bitbanged GPIO
3ca632f55SGrant Likely  *
4ca632f55SGrant Likely  * Copyright (C) 2006,2008 David Brownell
59b00bc7bSLinus Walleij  * Copyright (C) 2017 Linus Walleij
6ca632f55SGrant Likely  *
7ca632f55SGrant Likely  * This program is free software; you can redistribute it and/or modify
8ca632f55SGrant Likely  * it under the terms of the GNU General Public License as published by
9ca632f55SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
10ca632f55SGrant Likely  * (at your option) any later version.
11ca632f55SGrant Likely  *
12ca632f55SGrant Likely  * This program is distributed in the hope that it will be useful,
13ca632f55SGrant Likely  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14ca632f55SGrant Likely  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15ca632f55SGrant Likely  * GNU General Public License for more details.
16ca632f55SGrant Likely  */
17ca632f55SGrant Likely #include <linux/kernel.h>
18d7614de4SPaul Gortmaker #include <linux/module.h>
19ca632f55SGrant Likely #include <linux/platform_device.h>
209b00bc7bSLinus Walleij #include <linux/gpio/consumer.h>
21ecc77773SSachin Kamat #include <linux/of.h>
2238ab18caSDaniel Mack #include <linux/of_device.h>
23ca632f55SGrant Likely 
24ca632f55SGrant Likely #include <linux/spi/spi.h>
25ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h>
26ca632f55SGrant Likely #include <linux/spi/spi_gpio.h>
27ca632f55SGrant Likely 
28ca632f55SGrant Likely 
29ca632f55SGrant Likely /*
30ca632f55SGrant Likely  * This bitbanging SPI master driver should help make systems usable
31ca632f55SGrant Likely  * when a native hardware SPI engine is not available, perhaps because
32ca632f55SGrant Likely  * its driver isn't yet working or because the I/O pins it requires
33ca632f55SGrant Likely  * are used for other purposes.
34ca632f55SGrant Likely  *
35ca632f55SGrant Likely  * platform_device->driver_data ... points to spi_gpio
36ca632f55SGrant Likely  *
37ca632f55SGrant Likely  * spi->controller_state ... reserved for bitbang framework code
38ca632f55SGrant Likely  * spi->controller_data ... holds chipselect GPIO
39ca632f55SGrant Likely  *
40ca632f55SGrant Likely  * spi->master->dev.driver_data ... points to spi_gpio->bitbang
41ca632f55SGrant Likely  */
42ca632f55SGrant Likely 
43ca632f55SGrant Likely struct spi_gpio {
44ca632f55SGrant Likely 	struct spi_bitbang		bitbang;
45ca632f55SGrant Likely 	struct spi_gpio_platform_data	pdata;
46ca632f55SGrant Likely 	struct platform_device		*pdev;
479b00bc7bSLinus Walleij 	struct gpio_desc		*sck;
489b00bc7bSLinus Walleij 	struct gpio_desc		*miso;
499b00bc7bSLinus Walleij 	struct gpio_desc		*mosi;
509b00bc7bSLinus Walleij 	struct gpio_desc		**cs_gpios;
519b00bc7bSLinus Walleij 	bool				has_cs;
52ca632f55SGrant Likely };
53ca632f55SGrant Likely 
54ca632f55SGrant Likely /*----------------------------------------------------------------------*/
55ca632f55SGrant Likely 
56ca632f55SGrant Likely /*
57ca632f55SGrant Likely  * Because the overhead of going through four GPIO procedure calls
58ca632f55SGrant Likely  * per transferred bit can make performance a problem, this code
59ca632f55SGrant Likely  * is set up so that you can use it in either of two ways:
60ca632f55SGrant Likely  *
61ca632f55SGrant Likely  *   - The slow generic way:  set up platform_data to hold the GPIO
62ca632f55SGrant Likely  *     numbers used for MISO/MOSI/SCK, and issue procedure calls for
63ca632f55SGrant Likely  *     each of them.  This driver can handle several such busses.
64ca632f55SGrant Likely  *
65ca632f55SGrant Likely  *   - The quicker inlined way:  only helps with platform GPIO code
66ca632f55SGrant Likely  *     that inlines operations for constant GPIOs.  This can give
67ca632f55SGrant Likely  *     you tight (fast!) inner loops, but each such bus needs a
68ca632f55SGrant Likely  *     new driver.  You'll define a new C file, with Makefile and
69ca632f55SGrant Likely  *     Kconfig support; the C code can be a total of six lines:
70ca632f55SGrant Likely  *
71ca632f55SGrant Likely  *		#define DRIVER_NAME	"myboard_spi2"
72ca632f55SGrant Likely  *		#define	SPI_MISO_GPIO	119
73ca632f55SGrant Likely  *		#define	SPI_MOSI_GPIO	120
74ca632f55SGrant Likely  *		#define	SPI_SCK_GPIO	121
75ca632f55SGrant Likely  *		#define	SPI_N_CHIPSEL	4
76ca632f55SGrant Likely  *		#include "spi-gpio.c"
77ca632f55SGrant Likely  */
78ca632f55SGrant Likely 
79ca632f55SGrant Likely #ifndef DRIVER_NAME
80ca632f55SGrant Likely #define DRIVER_NAME	"spi_gpio"
81ca632f55SGrant Likely 
82ca632f55SGrant Likely #define GENERIC_BITBANG	/* vs tight inlines */
83ca632f55SGrant Likely 
84ca632f55SGrant Likely #endif
85ca632f55SGrant Likely 
86ca632f55SGrant Likely /*----------------------------------------------------------------------*/
87ca632f55SGrant Likely 
88161c2dd3SDaniel Mack static inline struct spi_gpio *__pure
89161c2dd3SDaniel Mack spi_to_spi_gpio(const struct spi_device *spi)
90ca632f55SGrant Likely {
91ca632f55SGrant Likely 	const struct spi_bitbang	*bang;
92161c2dd3SDaniel Mack 	struct spi_gpio			*spi_gpio;
93ca632f55SGrant Likely 
94ca632f55SGrant Likely 	bang = spi_master_get_devdata(spi->master);
95ca632f55SGrant Likely 	spi_gpio = container_of(bang, struct spi_gpio, bitbang);
96161c2dd3SDaniel Mack 	return spi_gpio;
97161c2dd3SDaniel Mack }
98161c2dd3SDaniel Mack 
999b00bc7bSLinus Walleij /* These helpers are in turn called by the bitbang inlines */
100ca632f55SGrant Likely static inline void setsck(const struct spi_device *spi, int is_on)
101ca632f55SGrant Likely {
1029b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
1039b00bc7bSLinus Walleij 
1049b00bc7bSLinus Walleij 	gpiod_set_value_cansleep(spi_gpio->sck, is_on);
105ca632f55SGrant Likely }
106ca632f55SGrant Likely 
107ca632f55SGrant Likely static inline void setmosi(const struct spi_device *spi, int is_on)
108ca632f55SGrant Likely {
1099b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
1109b00bc7bSLinus Walleij 
1119b00bc7bSLinus Walleij 	gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
112ca632f55SGrant Likely }
113ca632f55SGrant Likely 
114ca632f55SGrant Likely static inline int getmiso(const struct spi_device *spi)
115ca632f55SGrant Likely {
1169b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
117ca632f55SGrant Likely 
1184b859db2SLorenzo Bianconi 	if (spi->mode & SPI_3WIRE)
1194b859db2SLorenzo Bianconi 		return !!gpiod_get_value_cansleep(spi_gpio->mosi);
1204b859db2SLorenzo Bianconi 	else
1219b00bc7bSLinus Walleij 		return !!gpiod_get_value_cansleep(spi_gpio->miso);
1229b00bc7bSLinus Walleij }
123ca632f55SGrant Likely 
124ca632f55SGrant Likely /*
125ca632f55SGrant Likely  * NOTE:  this clocks "as fast as we can".  It "should" be a function of the
126ca632f55SGrant Likely  * requested device clock.  Software overhead means we usually have trouble
127ca632f55SGrant Likely  * reaching even one Mbit/sec (except when we can inline bitops), so for now
128ca632f55SGrant Likely  * we'll just assume we never need additional per-bit slowdowns.
129ca632f55SGrant Likely  */
130ca632f55SGrant Likely #define spidelay(nsecs)	do {} while (0)
131ca632f55SGrant Likely 
132ca632f55SGrant Likely #include "spi-bitbang-txrx.h"
133ca632f55SGrant Likely 
134ca632f55SGrant Likely /*
135ca632f55SGrant Likely  * These functions can leverage inline expansion of GPIO calls to shrink
136ca632f55SGrant Likely  * costs for a txrx bit, often by factors of around ten (by instruction
137ca632f55SGrant Likely  * count).  That is particularly visible for larger word sizes, but helps
138ca632f55SGrant Likely  * even with default 8-bit words.
139ca632f55SGrant Likely  *
140ca632f55SGrant Likely  * REVISIT overheads calling these functions for each word also have
141ca632f55SGrant Likely  * significant performance costs.  Having txrx_bufs() calls that inline
142ca632f55SGrant Likely  * the txrx_word() logic would help performance, e.g. on larger blocks
143ca632f55SGrant Likely  * used with flash storage or MMC/SD.  There should also be ways to make
144ca632f55SGrant Likely  * GCC be less stupid about reloading registers inside the I/O loops,
145ca632f55SGrant Likely  * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
146ca632f55SGrant Likely  */
147ca632f55SGrant Likely 
148ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
149304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
150ca632f55SGrant Likely {
151304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
152ca632f55SGrant Likely }
153ca632f55SGrant Likely 
154ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
155304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
156ca632f55SGrant Likely {
157304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
158ca632f55SGrant Likely }
159ca632f55SGrant Likely 
160ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
161304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
162ca632f55SGrant Likely {
163304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
164ca632f55SGrant Likely }
165ca632f55SGrant Likely 
166ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
167304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
168ca632f55SGrant Likely {
169304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
170ca632f55SGrant Likely }
171ca632f55SGrant Likely 
172ca632f55SGrant Likely /*
173ca632f55SGrant Likely  * These functions do not call setmosi or getmiso if respective flag
174ca632f55SGrant Likely  * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
175ca632f55SGrant Likely  * call when such pin is not present or defined in the controller.
176ca632f55SGrant Likely  * A separate set of callbacks is defined to get highest possible
177ca632f55SGrant Likely  * speed in the generic case (when both MISO and MOSI lines are
178ca632f55SGrant Likely  * available), as optimiser will remove the checks when argument is
179ca632f55SGrant Likely  * constant.
180ca632f55SGrant Likely  */
181ca632f55SGrant Likely 
182ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
183304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
184ca632f55SGrant Likely {
185304d3436SLorenzo Bianconi 	flags = spi->master->flags;
186ca632f55SGrant Likely 	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
187ca632f55SGrant Likely }
188ca632f55SGrant Likely 
189ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
190304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
191ca632f55SGrant Likely {
192304d3436SLorenzo Bianconi 	flags = spi->master->flags;
193ca632f55SGrant Likely 	return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
194ca632f55SGrant Likely }
195ca632f55SGrant Likely 
196ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
197304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
198ca632f55SGrant Likely {
199304d3436SLorenzo Bianconi 	flags = spi->master->flags;
200ca632f55SGrant Likely 	return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
201ca632f55SGrant Likely }
202ca632f55SGrant Likely 
203ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
204304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
205ca632f55SGrant Likely {
206304d3436SLorenzo Bianconi 	flags = spi->master->flags;
207ca632f55SGrant Likely 	return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
208ca632f55SGrant Likely }
209ca632f55SGrant Likely 
210ca632f55SGrant Likely /*----------------------------------------------------------------------*/
211ca632f55SGrant Likely 
212ca632f55SGrant Likely static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
213ca632f55SGrant Likely {
214161c2dd3SDaniel Mack 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
215ca632f55SGrant Likely 
2169b00bc7bSLinus Walleij 	/* set initial clock line level */
217ca632f55SGrant Likely 	if (is_active)
2189b00bc7bSLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
219ca632f55SGrant Likely 
2209b00bc7bSLinus Walleij 	/* Drive chip select line, if we have one */
2219b00bc7bSLinus Walleij 	if (spi_gpio->has_cs) {
2229b00bc7bSLinus Walleij 		struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
2239b00bc7bSLinus Walleij 
2249b00bc7bSLinus Walleij 		/* SPI chip selects are normally active-low */
2259b00bc7bSLinus Walleij 		gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
226ca632f55SGrant Likely 	}
227ca632f55SGrant Likely }
228ca632f55SGrant Likely 
229ca632f55SGrant Likely static int spi_gpio_setup(struct spi_device *spi)
230ca632f55SGrant Likely {
2319b00bc7bSLinus Walleij 	struct gpio_desc	*cs;
232ca632f55SGrant Likely 	int			status = 0;
233161c2dd3SDaniel Mack 	struct spi_gpio		*spi_gpio = spi_to_spi_gpio(spi);
234ca632f55SGrant Likely 
23538ab18caSDaniel Mack 	/*
2369b00bc7bSLinus Walleij 	 * The CS GPIOs have already been
2379b00bc7bSLinus Walleij 	 * initialized from the descriptor lookup.
23838ab18caSDaniel Mack 	 */
23938ab18caSDaniel Mack 	cs = spi_gpio->cs_gpios[spi->chip_select];
2409b00bc7bSLinus Walleij 	if (!spi->controller_state && cs)
2419b00bc7bSLinus Walleij 		status = gpiod_direction_output(cs,
24205644147SUwe Kleine-König 						!(spi->mode & SPI_CS_HIGH));
243161c2dd3SDaniel Mack 
2449b00bc7bSLinus Walleij 	if (!status)
2459b00bc7bSLinus Walleij 		status = spi_bitbang_setup(spi);
2469b00bc7bSLinus Walleij 
247ca632f55SGrant Likely 	return status;
248ca632f55SGrant Likely }
249ca632f55SGrant Likely 
2504b859db2SLorenzo Bianconi static int spi_gpio_set_direction(struct spi_device *spi, bool output)
2514b859db2SLorenzo Bianconi {
2524b859db2SLorenzo Bianconi 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
2535132b3d2SLinus Walleij 	int ret;
2544b859db2SLorenzo Bianconi 
2554b859db2SLorenzo Bianconi 	if (output)
2564b859db2SLorenzo Bianconi 		return gpiod_direction_output(spi_gpio->mosi, 1);
2575132b3d2SLinus Walleij 
2585132b3d2SLinus Walleij 	ret = gpiod_direction_input(spi_gpio->mosi);
2595132b3d2SLinus Walleij 	if (ret)
2605132b3d2SLinus Walleij 		return ret;
2615132b3d2SLinus Walleij 	/*
2625132b3d2SLinus Walleij 	 * Send a turnaround high impedance cycle when switching
2635132b3d2SLinus Walleij 	 * from output to input. Theoretically there should be
2645132b3d2SLinus Walleij 	 * a clock delay here, but as has been noted above, the
2655132b3d2SLinus Walleij 	 * nsec delay function for bit-banged GPIO is simply
2665132b3d2SLinus Walleij 	 * {} because bit-banging just doesn't get fast enough
2675132b3d2SLinus Walleij 	 * anyway.
2685132b3d2SLinus Walleij 	 */
2695132b3d2SLinus Walleij 	if (spi->mode & SPI_3WIRE_HIZ) {
2705132b3d2SLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck,
2715132b3d2SLinus Walleij 					 !(spi->mode & SPI_CPOL));
2725132b3d2SLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck,
2735132b3d2SLinus Walleij 					 !!(spi->mode & SPI_CPOL));
2745132b3d2SLinus Walleij 	}
2755132b3d2SLinus Walleij 	return 0;
2764b859db2SLorenzo Bianconi }
2774b859db2SLorenzo Bianconi 
278ca632f55SGrant Likely static void spi_gpio_cleanup(struct spi_device *spi)
279ca632f55SGrant Likely {
280ca632f55SGrant Likely 	spi_bitbang_cleanup(spi);
281ca632f55SGrant Likely }
282ca632f55SGrant Likely 
2839b00bc7bSLinus Walleij /*
2849b00bc7bSLinus Walleij  * It can be convenient to use this driver with pins that have alternate
2859b00bc7bSLinus Walleij  * functions associated with a "native" SPI controller if a driver for that
2869b00bc7bSLinus Walleij  * controller is not available, or is missing important functionality.
2879b00bc7bSLinus Walleij  *
2889b00bc7bSLinus Walleij  * On platforms which can do so, configure MISO with a weak pullup unless
2899b00bc7bSLinus Walleij  * there's an external pullup on that signal.  That saves power by avoiding
2909b00bc7bSLinus Walleij  * floating signals.  (A weak pulldown would save power too, but many
2919b00bc7bSLinus Walleij  * drivers expect to see all-ones data as the no slave "response".)
2929b00bc7bSLinus Walleij  */
2939b00bc7bSLinus Walleij static int spi_gpio_request(struct device *dev,
2949b00bc7bSLinus Walleij 			    struct spi_gpio *spi_gpio,
2959b00bc7bSLinus Walleij 			    unsigned int num_chipselects,
2969b00bc7bSLinus Walleij 			    u16 *mflags)
297ca632f55SGrant Likely {
2989b00bc7bSLinus Walleij 	int i;
299ca632f55SGrant Likely 
3009b00bc7bSLinus Walleij 	spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
3019b00bc7bSLinus Walleij 	if (IS_ERR(spi_gpio->mosi))
3029b00bc7bSLinus Walleij 		return PTR_ERR(spi_gpio->mosi);
3039b00bc7bSLinus Walleij 	if (!spi_gpio->mosi)
304ca632f55SGrant Likely 		/* HW configuration without MOSI pin */
3059b00bc7bSLinus Walleij 		*mflags |= SPI_MASTER_NO_TX;
306ca632f55SGrant Likely 
3079b00bc7bSLinus Walleij 	spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
3089b00bc7bSLinus Walleij 	if (IS_ERR(spi_gpio->miso))
3099b00bc7bSLinus Walleij 		return PTR_ERR(spi_gpio->miso);
310abf5feefSLinus Walleij 	/*
311abf5feefSLinus Walleij 	 * No setting SPI_MASTER_NO_RX here - if there is only a MOSI
312abf5feefSLinus Walleij 	 * pin connected the host can still do RX by changing the
313abf5feefSLinus Walleij 	 * direction of the line.
314abf5feefSLinus Walleij 	 */
3159b00bc7bSLinus Walleij 
3169b00bc7bSLinus Walleij 	spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
3171723c315SLinus Walleij 	if (IS_ERR(spi_gpio->sck))
3181723c315SLinus Walleij 		return PTR_ERR(spi_gpio->sck);
3199b00bc7bSLinus Walleij 
3209b00bc7bSLinus Walleij 	for (i = 0; i < num_chipselects; i++) {
3219b00bc7bSLinus Walleij 		spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs",
3229b00bc7bSLinus Walleij 							     i, GPIOD_OUT_HIGH);
3239b00bc7bSLinus Walleij 		if (IS_ERR(spi_gpio->cs_gpios[i]))
3249b00bc7bSLinus Walleij 			return PTR_ERR(spi_gpio->cs_gpios[i]);
325ca632f55SGrant Likely 	}
326ca632f55SGrant Likely 
3279b00bc7bSLinus Walleij 	return 0;
328ca632f55SGrant Likely }
329ca632f55SGrant Likely 
33038ab18caSDaniel Mack #ifdef CONFIG_OF
331d9e15281SJingoo Han static const struct of_device_id spi_gpio_dt_ids[] = {
33238ab18caSDaniel Mack 	{ .compatible = "spi-gpio" },
33338ab18caSDaniel Mack 	{}
33438ab18caSDaniel Mack };
33538ab18caSDaniel Mack MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
33638ab18caSDaniel Mack 
33738ab18caSDaniel Mack static int spi_gpio_probe_dt(struct platform_device *pdev)
33838ab18caSDaniel Mack {
33938ab18caSDaniel Mack 	int ret;
34038ab18caSDaniel Mack 	u32 tmp;
34138ab18caSDaniel Mack 	struct spi_gpio_platform_data	*pdata;
34238ab18caSDaniel Mack 	struct device_node *np = pdev->dev.of_node;
34338ab18caSDaniel Mack 	const struct of_device_id *of_id =
34438ab18caSDaniel Mack 			of_match_device(spi_gpio_dt_ids, &pdev->dev);
34538ab18caSDaniel Mack 
34638ab18caSDaniel Mack 	if (!of_id)
34738ab18caSDaniel Mack 		return 0;
34838ab18caSDaniel Mack 
34938ab18caSDaniel Mack 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
35038ab18caSDaniel Mack 	if (!pdata)
35138ab18caSDaniel Mack 		return -ENOMEM;
35238ab18caSDaniel Mack 
35338ab18caSDaniel Mack 
35438ab18caSDaniel Mack 	ret = of_property_read_u32(np, "num-chipselects", &tmp);
35538ab18caSDaniel Mack 	if (ret < 0) {
35638ab18caSDaniel Mack 		dev_err(&pdev->dev, "num-chipselects property not found\n");
35738ab18caSDaniel Mack 		goto error_free;
35838ab18caSDaniel Mack 	}
35938ab18caSDaniel Mack 
36038ab18caSDaniel Mack 	pdata->num_chipselect = tmp;
36138ab18caSDaniel Mack 	pdev->dev.platform_data = pdata;
36238ab18caSDaniel Mack 
36338ab18caSDaniel Mack 	return 1;
36438ab18caSDaniel Mack 
36538ab18caSDaniel Mack error_free:
36638ab18caSDaniel Mack 	devm_kfree(&pdev->dev, pdata);
36738ab18caSDaniel Mack 	return ret;
36838ab18caSDaniel Mack }
36938ab18caSDaniel Mack #else
370ac2cb30bSMark Brown static inline int spi_gpio_probe_dt(struct platform_device *pdev)
37138ab18caSDaniel Mack {
37238ab18caSDaniel Mack 	return 0;
37338ab18caSDaniel Mack }
37438ab18caSDaniel Mack #endif
37538ab18caSDaniel Mack 
376fd4a319bSGrant Likely static int spi_gpio_probe(struct platform_device *pdev)
377ca632f55SGrant Likely {
378ca632f55SGrant Likely 	int				status;
379ca632f55SGrant Likely 	struct spi_master		*master;
380ca632f55SGrant Likely 	struct spi_gpio			*spi_gpio;
381ca632f55SGrant Likely 	struct spi_gpio_platform_data	*pdata;
382*96cad6d7SAndrey Smirnov 	struct device			*dev = &pdev->dev;
383ca632f55SGrant Likely 	u16 master_flags = 0;
38438ab18caSDaniel Mack 	bool use_of = 0;
38538ab18caSDaniel Mack 
38638ab18caSDaniel Mack 	status = spi_gpio_probe_dt(pdev);
38738ab18caSDaniel Mack 	if (status < 0)
38838ab18caSDaniel Mack 		return status;
38938ab18caSDaniel Mack 	if (status > 0)
39038ab18caSDaniel Mack 		use_of = 1;
391ca632f55SGrant Likely 
392*96cad6d7SAndrey Smirnov 	pdata = dev_get_platdata(dev);
393ca632f55SGrant Likely #ifdef GENERIC_BITBANG
394d1d81802STorsten Fleischer 	if (!pdata || (!use_of && !pdata->num_chipselect))
395ca632f55SGrant Likely 		return -ENODEV;
396ca632f55SGrant Likely #endif
397ca632f55SGrant Likely 
398*96cad6d7SAndrey Smirnov 	master = spi_alloc_master(dev, sizeof(*spi_gpio));
3999b00bc7bSLinus Walleij 	if (!master)
4009b00bc7bSLinus Walleij 		return -ENOMEM;
401d1d81802STorsten Fleischer 
402ca632f55SGrant Likely 	spi_gpio = spi_master_get_devdata(master);
4039b00bc7bSLinus Walleij 
404*96cad6d7SAndrey Smirnov 	spi_gpio->cs_gpios = devm_kcalloc(dev,
405a86854d0SKees Cook 				pdata->num_chipselect,
406a86854d0SKees Cook 				sizeof(*spi_gpio->cs_gpios),
4079b00bc7bSLinus Walleij 				GFP_KERNEL);
4089b00bc7bSLinus Walleij 	if (!spi_gpio->cs_gpios)
4099b00bc7bSLinus Walleij 		return -ENOMEM;
4109b00bc7bSLinus Walleij 
411ca632f55SGrant Likely 	platform_set_drvdata(pdev, spi_gpio);
412ca632f55SGrant Likely 
4139b00bc7bSLinus Walleij 	/* Determine if we have chip selects connected */
4149b00bc7bSLinus Walleij 	spi_gpio->has_cs = !!pdata->num_chipselect;
4159b00bc7bSLinus Walleij 
416ca632f55SGrant Likely 	spi_gpio->pdev = pdev;
417ca632f55SGrant Likely 	if (pdata)
418ca632f55SGrant Likely 		spi_gpio->pdata = *pdata;
419ca632f55SGrant Likely 
420*96cad6d7SAndrey Smirnov 	status = spi_gpio_request(dev, spi_gpio,
4219b00bc7bSLinus Walleij 				  pdata->num_chipselect, &master_flags);
4229b00bc7bSLinus Walleij 	if (status)
4239b00bc7bSLinus Walleij 		return status;
4249b00bc7bSLinus Walleij 
42524778be2SStephen Warren 	master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
426b89fefdaSRussell King 	master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
427b89fefdaSRussell King 			    SPI_CS_HIGH;
428ca632f55SGrant Likely 	master->flags = master_flags;
429ca632f55SGrant Likely 	master->bus_num = pdev->id;
4309b00bc7bSLinus Walleij 	/* The master needs to think there is a chipselect even if not connected */
4319b00bc7bSLinus Walleij 	master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1;
432ca632f55SGrant Likely 	master->setup = spi_gpio_setup;
433ca632f55SGrant Likely 	master->cleanup = spi_gpio_cleanup;
43438ab18caSDaniel Mack #ifdef CONFIG_OF
435*96cad6d7SAndrey Smirnov 	master->dev.of_node = dev->of_node;
43638ab18caSDaniel Mack #endif
437ca632f55SGrant Likely 
43894c69f76SAxel Lin 	spi_gpio->bitbang.master = master;
439ca632f55SGrant Likely 	spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
4404b859db2SLorenzo Bianconi 	spi_gpio->bitbang.set_line_direction = spi_gpio_set_direction;
441ca632f55SGrant Likely 
442abf5feefSLinus Walleij 	if ((master_flags & SPI_MASTER_NO_TX) == 0) {
443ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
444ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
445ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
446ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
447ca632f55SGrant Likely 	} else {
448ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
449ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
450ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
451ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
452ca632f55SGrant Likely 	}
453ca632f55SGrant Likely 	spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
454ca632f55SGrant Likely 
455ca632f55SGrant Likely 	status = spi_bitbang_start(&spi_gpio->bitbang);
4569b00bc7bSLinus Walleij 	if (status)
457ca632f55SGrant Likely 		spi_master_put(master);
458ca632f55SGrant Likely 
459ca632f55SGrant Likely 	return status;
460ca632f55SGrant Likely }
461ca632f55SGrant Likely 
462fd4a319bSGrant Likely static int spi_gpio_remove(struct platform_device *pdev)
463ca632f55SGrant Likely {
464ca632f55SGrant Likely 	struct spi_gpio			*spi_gpio;
465ca632f55SGrant Likely 
466ca632f55SGrant Likely 	spi_gpio = platform_get_drvdata(pdev);
467ca632f55SGrant Likely 
468ca632f55SGrant Likely 	/* stop() unregisters child devices too */
469d9721ae1SAxel Lin 	spi_bitbang_stop(&spi_gpio->bitbang);
470ca632f55SGrant Likely 
47194c69f76SAxel Lin 	spi_master_put(spi_gpio->bitbang.master);
472ca632f55SGrant Likely 
473d9721ae1SAxel Lin 	return 0;
474ca632f55SGrant Likely }
475ca632f55SGrant Likely 
476ca632f55SGrant Likely MODULE_ALIAS("platform:" DRIVER_NAME);
477ca632f55SGrant Likely 
478ca632f55SGrant Likely static struct platform_driver spi_gpio_driver = {
47938ab18caSDaniel Mack 	.driver = {
48038ab18caSDaniel Mack 		.name	= DRIVER_NAME,
48138ab18caSDaniel Mack 		.of_match_table = of_match_ptr(spi_gpio_dt_ids),
48238ab18caSDaniel Mack 	},
483940ab889SGrant Likely 	.probe		= spi_gpio_probe,
484fd4a319bSGrant Likely 	.remove		= spi_gpio_remove,
485ca632f55SGrant Likely };
486940ab889SGrant Likely module_platform_driver(spi_gpio_driver);
487ca632f55SGrant Likely 
488ca632f55SGrant Likely MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
489ca632f55SGrant Likely MODULE_AUTHOR("David Brownell");
490ca632f55SGrant Likely MODULE_LICENSE("GPL");
491