xref: /linux/drivers/spi/spi-gpio.c (revision b89fefda7d4e3a649129584d855be233c7465264)
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 
99161c2dd3SDaniel Mack static inline struct spi_gpio_platform_data *__pure
100161c2dd3SDaniel Mack spi_to_pdata(const struct spi_device *spi)
101161c2dd3SDaniel Mack {
102161c2dd3SDaniel Mack 	return &spi_to_spi_gpio(spi)->pdata;
103ca632f55SGrant Likely }
104ca632f55SGrant Likely 
1059b00bc7bSLinus Walleij /* These helpers are in turn called by the bitbang inlines */
106ca632f55SGrant Likely static inline void setsck(const struct spi_device *spi, int is_on)
107ca632f55SGrant Likely {
1089b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
1099b00bc7bSLinus Walleij 
1109b00bc7bSLinus Walleij 	gpiod_set_value_cansleep(spi_gpio->sck, is_on);
111ca632f55SGrant Likely }
112ca632f55SGrant Likely 
113ca632f55SGrant Likely static inline void setmosi(const struct spi_device *spi, int is_on)
114ca632f55SGrant Likely {
1159b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
1169b00bc7bSLinus Walleij 
1179b00bc7bSLinus Walleij 	gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
118ca632f55SGrant Likely }
119ca632f55SGrant Likely 
120ca632f55SGrant Likely static inline int getmiso(const struct spi_device *spi)
121ca632f55SGrant Likely {
1229b00bc7bSLinus Walleij 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
123ca632f55SGrant Likely 
1244b859db2SLorenzo Bianconi 	if (spi->mode & SPI_3WIRE)
1254b859db2SLorenzo Bianconi 		return !!gpiod_get_value_cansleep(spi_gpio->mosi);
1264b859db2SLorenzo Bianconi 	else
1279b00bc7bSLinus Walleij 		return !!gpiod_get_value_cansleep(spi_gpio->miso);
1289b00bc7bSLinus Walleij }
129ca632f55SGrant Likely 
130ca632f55SGrant Likely /*
131ca632f55SGrant Likely  * NOTE:  this clocks "as fast as we can".  It "should" be a function of the
132ca632f55SGrant Likely  * requested device clock.  Software overhead means we usually have trouble
133ca632f55SGrant Likely  * reaching even one Mbit/sec (except when we can inline bitops), so for now
134ca632f55SGrant Likely  * we'll just assume we never need additional per-bit slowdowns.
135ca632f55SGrant Likely  */
136ca632f55SGrant Likely #define spidelay(nsecs)	do {} while (0)
137ca632f55SGrant Likely 
138ca632f55SGrant Likely #include "spi-bitbang-txrx.h"
139ca632f55SGrant Likely 
140ca632f55SGrant Likely /*
141ca632f55SGrant Likely  * These functions can leverage inline expansion of GPIO calls to shrink
142ca632f55SGrant Likely  * costs for a txrx bit, often by factors of around ten (by instruction
143ca632f55SGrant Likely  * count).  That is particularly visible for larger word sizes, but helps
144ca632f55SGrant Likely  * even with default 8-bit words.
145ca632f55SGrant Likely  *
146ca632f55SGrant Likely  * REVISIT overheads calling these functions for each word also have
147ca632f55SGrant Likely  * significant performance costs.  Having txrx_bufs() calls that inline
148ca632f55SGrant Likely  * the txrx_word() logic would help performance, e.g. on larger blocks
149ca632f55SGrant Likely  * used with flash storage or MMC/SD.  There should also be ways to make
150ca632f55SGrant Likely  * GCC be less stupid about reloading registers inside the I/O loops,
151ca632f55SGrant Likely  * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
152ca632f55SGrant Likely  */
153ca632f55SGrant Likely 
154ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
155304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
156ca632f55SGrant Likely {
157304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
158ca632f55SGrant Likely }
159ca632f55SGrant Likely 
160ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
161304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
162ca632f55SGrant Likely {
163304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
164ca632f55SGrant Likely }
165ca632f55SGrant Likely 
166ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
167304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
168ca632f55SGrant Likely {
169304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
170ca632f55SGrant Likely }
171ca632f55SGrant Likely 
172ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
173304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
174ca632f55SGrant Likely {
175304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
176ca632f55SGrant Likely }
177ca632f55SGrant Likely 
178ca632f55SGrant Likely /*
179ca632f55SGrant Likely  * These functions do not call setmosi or getmiso if respective flag
180ca632f55SGrant Likely  * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
181ca632f55SGrant Likely  * call when such pin is not present or defined in the controller.
182ca632f55SGrant Likely  * A separate set of callbacks is defined to get highest possible
183ca632f55SGrant Likely  * speed in the generic case (when both MISO and MOSI lines are
184ca632f55SGrant Likely  * available), as optimiser will remove the checks when argument is
185ca632f55SGrant Likely  * constant.
186ca632f55SGrant Likely  */
187ca632f55SGrant Likely 
188ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
189304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
190ca632f55SGrant Likely {
191304d3436SLorenzo Bianconi 	flags = spi->master->flags;
192ca632f55SGrant Likely 	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
193ca632f55SGrant Likely }
194ca632f55SGrant Likely 
195ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
196304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
197ca632f55SGrant Likely {
198304d3436SLorenzo Bianconi 	flags = spi->master->flags;
199ca632f55SGrant Likely 	return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
200ca632f55SGrant Likely }
201ca632f55SGrant Likely 
202ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
203304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
204ca632f55SGrant Likely {
205304d3436SLorenzo Bianconi 	flags = spi->master->flags;
206ca632f55SGrant Likely 	return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
207ca632f55SGrant Likely }
208ca632f55SGrant Likely 
209ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
210304d3436SLorenzo Bianconi 		unsigned nsecs, u32 word, u8 bits, unsigned flags)
211ca632f55SGrant Likely {
212304d3436SLorenzo Bianconi 	flags = spi->master->flags;
213ca632f55SGrant Likely 	return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
214ca632f55SGrant Likely }
215ca632f55SGrant Likely 
216ca632f55SGrant Likely /*----------------------------------------------------------------------*/
217ca632f55SGrant Likely 
218ca632f55SGrant Likely static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
219ca632f55SGrant Likely {
220161c2dd3SDaniel Mack 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
221ca632f55SGrant Likely 
2229b00bc7bSLinus Walleij 	/* set initial clock line level */
223ca632f55SGrant Likely 	if (is_active)
2249b00bc7bSLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
225ca632f55SGrant Likely 
2269b00bc7bSLinus Walleij 	/* Drive chip select line, if we have one */
2279b00bc7bSLinus Walleij 	if (spi_gpio->has_cs) {
2289b00bc7bSLinus Walleij 		struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
2299b00bc7bSLinus Walleij 
2309b00bc7bSLinus Walleij 		/* SPI chip selects are normally active-low */
2319b00bc7bSLinus Walleij 		gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
232ca632f55SGrant Likely 	}
233ca632f55SGrant Likely }
234ca632f55SGrant Likely 
235ca632f55SGrant Likely static int spi_gpio_setup(struct spi_device *spi)
236ca632f55SGrant Likely {
2379b00bc7bSLinus Walleij 	struct gpio_desc	*cs;
238ca632f55SGrant Likely 	int			status = 0;
239161c2dd3SDaniel Mack 	struct spi_gpio		*spi_gpio = spi_to_spi_gpio(spi);
240ca632f55SGrant Likely 
24138ab18caSDaniel Mack 	/*
2429b00bc7bSLinus Walleij 	 * The CS GPIOs have already been
2439b00bc7bSLinus Walleij 	 * initialized from the descriptor lookup.
24438ab18caSDaniel Mack 	 */
24538ab18caSDaniel Mack 	cs = spi_gpio->cs_gpios[spi->chip_select];
2469b00bc7bSLinus Walleij 	if (!spi->controller_state && cs)
2479b00bc7bSLinus Walleij 		status = gpiod_direction_output(cs,
24805644147SUwe Kleine-König 						!(spi->mode & SPI_CS_HIGH));
249161c2dd3SDaniel Mack 
2509b00bc7bSLinus Walleij 	if (!status)
2519b00bc7bSLinus Walleij 		status = spi_bitbang_setup(spi);
2529b00bc7bSLinus Walleij 
253ca632f55SGrant Likely 	return status;
254ca632f55SGrant Likely }
255ca632f55SGrant Likely 
2564b859db2SLorenzo Bianconi static int spi_gpio_set_direction(struct spi_device *spi, bool output)
2574b859db2SLorenzo Bianconi {
2584b859db2SLorenzo Bianconi 	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
2595132b3d2SLinus Walleij 	int ret;
2604b859db2SLorenzo Bianconi 
2614b859db2SLorenzo Bianconi 	if (output)
2624b859db2SLorenzo Bianconi 		return gpiod_direction_output(spi_gpio->mosi, 1);
2635132b3d2SLinus Walleij 
2645132b3d2SLinus Walleij 	ret = gpiod_direction_input(spi_gpio->mosi);
2655132b3d2SLinus Walleij 	if (ret)
2665132b3d2SLinus Walleij 		return ret;
2675132b3d2SLinus Walleij 	/*
2685132b3d2SLinus Walleij 	 * Send a turnaround high impedance cycle when switching
2695132b3d2SLinus Walleij 	 * from output to input. Theoretically there should be
2705132b3d2SLinus Walleij 	 * a clock delay here, but as has been noted above, the
2715132b3d2SLinus Walleij 	 * nsec delay function for bit-banged GPIO is simply
2725132b3d2SLinus Walleij 	 * {} because bit-banging just doesn't get fast enough
2735132b3d2SLinus Walleij 	 * anyway.
2745132b3d2SLinus Walleij 	 */
2755132b3d2SLinus Walleij 	if (spi->mode & SPI_3WIRE_HIZ) {
2765132b3d2SLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck,
2775132b3d2SLinus Walleij 					 !(spi->mode & SPI_CPOL));
2785132b3d2SLinus Walleij 		gpiod_set_value_cansleep(spi_gpio->sck,
2795132b3d2SLinus Walleij 					 !!(spi->mode & SPI_CPOL));
2805132b3d2SLinus Walleij 	}
2815132b3d2SLinus Walleij 	return 0;
2824b859db2SLorenzo Bianconi }
2834b859db2SLorenzo Bianconi 
284ca632f55SGrant Likely static void spi_gpio_cleanup(struct spi_device *spi)
285ca632f55SGrant Likely {
286ca632f55SGrant Likely 	spi_bitbang_cleanup(spi);
287ca632f55SGrant Likely }
288ca632f55SGrant Likely 
2899b00bc7bSLinus Walleij /*
2909b00bc7bSLinus Walleij  * It can be convenient to use this driver with pins that have alternate
2919b00bc7bSLinus Walleij  * functions associated with a "native" SPI controller if a driver for that
2929b00bc7bSLinus Walleij  * controller is not available, or is missing important functionality.
2939b00bc7bSLinus Walleij  *
2949b00bc7bSLinus Walleij  * On platforms which can do so, configure MISO with a weak pullup unless
2959b00bc7bSLinus Walleij  * there's an external pullup on that signal.  That saves power by avoiding
2969b00bc7bSLinus Walleij  * floating signals.  (A weak pulldown would save power too, but many
2979b00bc7bSLinus Walleij  * drivers expect to see all-ones data as the no slave "response".)
2989b00bc7bSLinus Walleij  */
2999b00bc7bSLinus Walleij static int spi_gpio_request(struct device *dev,
3009b00bc7bSLinus Walleij 			    struct spi_gpio *spi_gpio,
3019b00bc7bSLinus Walleij 			    unsigned int num_chipselects,
3029b00bc7bSLinus Walleij 			    u16 *mflags)
303ca632f55SGrant Likely {
3049b00bc7bSLinus Walleij 	int i;
305ca632f55SGrant Likely 
3069b00bc7bSLinus Walleij 	spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
3079b00bc7bSLinus Walleij 	if (IS_ERR(spi_gpio->mosi))
3089b00bc7bSLinus Walleij 		return PTR_ERR(spi_gpio->mosi);
3099b00bc7bSLinus Walleij 	if (!spi_gpio->mosi)
310ca632f55SGrant Likely 		/* HW configuration without MOSI pin */
3119b00bc7bSLinus Walleij 		*mflags |= SPI_MASTER_NO_TX;
312ca632f55SGrant Likely 
3139b00bc7bSLinus Walleij 	spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
3149b00bc7bSLinus Walleij 	if (IS_ERR(spi_gpio->miso))
3159b00bc7bSLinus Walleij 		return PTR_ERR(spi_gpio->miso);
316abf5feefSLinus Walleij 	/*
317abf5feefSLinus Walleij 	 * No setting SPI_MASTER_NO_RX here - if there is only a MOSI
318abf5feefSLinus Walleij 	 * pin connected the host can still do RX by changing the
319abf5feefSLinus Walleij 	 * direction of the line.
320abf5feefSLinus Walleij 	 */
3219b00bc7bSLinus Walleij 
3229b00bc7bSLinus Walleij 	spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
3231723c315SLinus Walleij 	if (IS_ERR(spi_gpio->sck))
3241723c315SLinus Walleij 		return PTR_ERR(spi_gpio->sck);
3259b00bc7bSLinus Walleij 
3269b00bc7bSLinus Walleij 	for (i = 0; i < num_chipselects; i++) {
3279b00bc7bSLinus Walleij 		spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs",
3289b00bc7bSLinus Walleij 							     i, GPIOD_OUT_HIGH);
3299b00bc7bSLinus Walleij 		if (IS_ERR(spi_gpio->cs_gpios[i]))
3309b00bc7bSLinus Walleij 			return PTR_ERR(spi_gpio->cs_gpios[i]);
331ca632f55SGrant Likely 	}
332ca632f55SGrant Likely 
3339b00bc7bSLinus Walleij 	return 0;
334ca632f55SGrant Likely }
335ca632f55SGrant Likely 
33638ab18caSDaniel Mack #ifdef CONFIG_OF
337d9e15281SJingoo Han static const struct of_device_id spi_gpio_dt_ids[] = {
33838ab18caSDaniel Mack 	{ .compatible = "spi-gpio" },
33938ab18caSDaniel Mack 	{}
34038ab18caSDaniel Mack };
34138ab18caSDaniel Mack MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
34238ab18caSDaniel Mack 
34338ab18caSDaniel Mack static int spi_gpio_probe_dt(struct platform_device *pdev)
34438ab18caSDaniel Mack {
34538ab18caSDaniel Mack 	int ret;
34638ab18caSDaniel Mack 	u32 tmp;
34738ab18caSDaniel Mack 	struct spi_gpio_platform_data	*pdata;
34838ab18caSDaniel Mack 	struct device_node *np = pdev->dev.of_node;
34938ab18caSDaniel Mack 	const struct of_device_id *of_id =
35038ab18caSDaniel Mack 			of_match_device(spi_gpio_dt_ids, &pdev->dev);
35138ab18caSDaniel Mack 
35238ab18caSDaniel Mack 	if (!of_id)
35338ab18caSDaniel Mack 		return 0;
35438ab18caSDaniel Mack 
35538ab18caSDaniel Mack 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
35638ab18caSDaniel Mack 	if (!pdata)
35738ab18caSDaniel Mack 		return -ENOMEM;
35838ab18caSDaniel Mack 
35938ab18caSDaniel Mack 
36038ab18caSDaniel Mack 	ret = of_property_read_u32(np, "num-chipselects", &tmp);
36138ab18caSDaniel Mack 	if (ret < 0) {
36238ab18caSDaniel Mack 		dev_err(&pdev->dev, "num-chipselects property not found\n");
36338ab18caSDaniel Mack 		goto error_free;
36438ab18caSDaniel Mack 	}
36538ab18caSDaniel Mack 
36638ab18caSDaniel Mack 	pdata->num_chipselect = tmp;
36738ab18caSDaniel Mack 	pdev->dev.platform_data = pdata;
36838ab18caSDaniel Mack 
36938ab18caSDaniel Mack 	return 1;
37038ab18caSDaniel Mack 
37138ab18caSDaniel Mack error_free:
37238ab18caSDaniel Mack 	devm_kfree(&pdev->dev, pdata);
37338ab18caSDaniel Mack 	return ret;
37438ab18caSDaniel Mack }
37538ab18caSDaniel Mack #else
376ac2cb30bSMark Brown static inline int spi_gpio_probe_dt(struct platform_device *pdev)
37738ab18caSDaniel Mack {
37838ab18caSDaniel Mack 	return 0;
37938ab18caSDaniel Mack }
38038ab18caSDaniel Mack #endif
38138ab18caSDaniel Mack 
382fd4a319bSGrant Likely static int spi_gpio_probe(struct platform_device *pdev)
383ca632f55SGrant Likely {
384ca632f55SGrant Likely 	int				status;
385ca632f55SGrant Likely 	struct spi_master		*master;
386ca632f55SGrant Likely 	struct spi_gpio			*spi_gpio;
387ca632f55SGrant Likely 	struct spi_gpio_platform_data	*pdata;
388ca632f55SGrant Likely 	u16 master_flags = 0;
38938ab18caSDaniel Mack 	bool use_of = 0;
39038ab18caSDaniel Mack 
39138ab18caSDaniel Mack 	status = spi_gpio_probe_dt(pdev);
39238ab18caSDaniel Mack 	if (status < 0)
39338ab18caSDaniel Mack 		return status;
39438ab18caSDaniel Mack 	if (status > 0)
39538ab18caSDaniel Mack 		use_of = 1;
396ca632f55SGrant Likely 
3978074cf06SJingoo Han 	pdata = dev_get_platdata(&pdev->dev);
398ca632f55SGrant Likely #ifdef GENERIC_BITBANG
399d1d81802STorsten Fleischer 	if (!pdata || (!use_of && !pdata->num_chipselect))
400ca632f55SGrant Likely 		return -ENODEV;
401ca632f55SGrant Likely #endif
402ca632f55SGrant Likely 
4039b00bc7bSLinus Walleij 	master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio));
4049b00bc7bSLinus Walleij 	if (!master)
4059b00bc7bSLinus Walleij 		return -ENOMEM;
406d1d81802STorsten Fleischer 
407ca632f55SGrant Likely 	spi_gpio = spi_master_get_devdata(master);
4089b00bc7bSLinus Walleij 
409a86854d0SKees Cook 	spi_gpio->cs_gpios = devm_kcalloc(&pdev->dev,
410a86854d0SKees Cook 				pdata->num_chipselect,
411a86854d0SKees Cook 				sizeof(*spi_gpio->cs_gpios),
4129b00bc7bSLinus Walleij 				GFP_KERNEL);
4139b00bc7bSLinus Walleij 	if (!spi_gpio->cs_gpios)
4149b00bc7bSLinus Walleij 		return -ENOMEM;
4159b00bc7bSLinus Walleij 
416ca632f55SGrant Likely 	platform_set_drvdata(pdev, spi_gpio);
417ca632f55SGrant Likely 
4189b00bc7bSLinus Walleij 	/* Determine if we have chip selects connected */
4199b00bc7bSLinus Walleij 	spi_gpio->has_cs = !!pdata->num_chipselect;
4209b00bc7bSLinus Walleij 
421ca632f55SGrant Likely 	spi_gpio->pdev = pdev;
422ca632f55SGrant Likely 	if (pdata)
423ca632f55SGrant Likely 		spi_gpio->pdata = *pdata;
424ca632f55SGrant Likely 
4259b00bc7bSLinus Walleij 	status = spi_gpio_request(&pdev->dev, spi_gpio,
4269b00bc7bSLinus Walleij 				  pdata->num_chipselect, &master_flags);
4279b00bc7bSLinus Walleij 	if (status)
4289b00bc7bSLinus Walleij 		return status;
4299b00bc7bSLinus Walleij 
43024778be2SStephen Warren 	master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
431*b89fefdaSRussell King 	master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
432*b89fefdaSRussell King 			    SPI_CS_HIGH;
433ca632f55SGrant Likely 	master->flags = master_flags;
434ca632f55SGrant Likely 	master->bus_num = pdev->id;
4359b00bc7bSLinus Walleij 	/* The master needs to think there is a chipselect even if not connected */
4369b00bc7bSLinus Walleij 	master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1;
437ca632f55SGrant Likely 	master->setup = spi_gpio_setup;
438ca632f55SGrant Likely 	master->cleanup = spi_gpio_cleanup;
43938ab18caSDaniel Mack #ifdef CONFIG_OF
44038ab18caSDaniel Mack 	master->dev.of_node = pdev->dev.of_node;
44138ab18caSDaniel Mack #endif
442ca632f55SGrant Likely 
44394c69f76SAxel Lin 	spi_gpio->bitbang.master = master;
444ca632f55SGrant Likely 	spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
4454b859db2SLorenzo Bianconi 	spi_gpio->bitbang.set_line_direction = spi_gpio_set_direction;
446ca632f55SGrant Likely 
447abf5feefSLinus Walleij 	if ((master_flags & SPI_MASTER_NO_TX) == 0) {
448ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
449ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
450ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
451ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
452ca632f55SGrant Likely 	} else {
453ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
454ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
455ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
456ca632f55SGrant Likely 		spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
457ca632f55SGrant Likely 	}
458ca632f55SGrant Likely 	spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
459ca632f55SGrant Likely 
460ca632f55SGrant Likely 	status = spi_bitbang_start(&spi_gpio->bitbang);
4619b00bc7bSLinus Walleij 	if (status)
462ca632f55SGrant Likely 		spi_master_put(master);
463ca632f55SGrant Likely 
464ca632f55SGrant Likely 	return status;
465ca632f55SGrant Likely }
466ca632f55SGrant Likely 
467fd4a319bSGrant Likely static int spi_gpio_remove(struct platform_device *pdev)
468ca632f55SGrant Likely {
469ca632f55SGrant Likely 	struct spi_gpio			*spi_gpio;
470ca632f55SGrant Likely 
471ca632f55SGrant Likely 	spi_gpio = platform_get_drvdata(pdev);
472ca632f55SGrant Likely 
473ca632f55SGrant Likely 	/* stop() unregisters child devices too */
474d9721ae1SAxel Lin 	spi_bitbang_stop(&spi_gpio->bitbang);
475ca632f55SGrant Likely 
47694c69f76SAxel Lin 	spi_master_put(spi_gpio->bitbang.master);
477ca632f55SGrant Likely 
478d9721ae1SAxel Lin 	return 0;
479ca632f55SGrant Likely }
480ca632f55SGrant Likely 
481ca632f55SGrant Likely MODULE_ALIAS("platform:" DRIVER_NAME);
482ca632f55SGrant Likely 
483ca632f55SGrant Likely static struct platform_driver spi_gpio_driver = {
48438ab18caSDaniel Mack 	.driver = {
48538ab18caSDaniel Mack 		.name	= DRIVER_NAME,
48638ab18caSDaniel Mack 		.of_match_table = of_match_ptr(spi_gpio_dt_ids),
48738ab18caSDaniel Mack 	},
488940ab889SGrant Likely 	.probe		= spi_gpio_probe,
489fd4a319bSGrant Likely 	.remove		= spi_gpio_remove,
490ca632f55SGrant Likely };
491940ab889SGrant Likely module_platform_driver(spi_gpio_driver);
492ca632f55SGrant Likely 
493ca632f55SGrant Likely MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
494ca632f55SGrant Likely MODULE_AUTHOR("David Brownell");
495ca632f55SGrant Likely MODULE_LICENSE("GPL");
496