spi-gpio.c 13 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
David Brownell's avatar
David Brownell committed
2
/*
Grant Likely's avatar
Grant Likely committed
3
 * SPI master driver using generic bitbanged GPIO
David Brownell's avatar
David Brownell committed
4 5
 *
 * Copyright (C) 2006,2008 David Brownell
6
 * Copyright (C) 2017 Linus Walleij
David Brownell's avatar
David Brownell committed
7 8
 */
#include <linux/kernel.h>
9
#include <linux/module.h>
David Brownell's avatar
David Brownell committed
10
#include <linux/platform_device.h>
11
#include <linux/gpio/consumer.h>
12
#include <linux/of.h>
Daniel Mack's avatar
Daniel Mack committed
13
#include <linux/of_device.h>
David Brownell's avatar
David Brownell committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

#include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h>
#include <linux/spi/spi_gpio.h>


/*
 * This bitbanging SPI master driver should help make systems usable
 * when a native hardware SPI engine is not available, perhaps because
 * its driver isn't yet working or because the I/O pins it requires
 * are used for other purposes.
 *
 * platform_device->driver_data ... points to spi_gpio
 *
 * spi->controller_state ... reserved for bitbang framework code
 *
 * spi->master->dev.driver_data ... points to spi_gpio->bitbang
 */

struct spi_gpio {
	struct spi_bitbang		bitbang;
35 36 37 38
	struct gpio_desc		*sck;
	struct gpio_desc		*miso;
	struct gpio_desc		*mosi;
	struct gpio_desc		**cs_gpios;
David Brownell's avatar
David Brownell committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
};

/*----------------------------------------------------------------------*/

/*
 * Because the overhead of going through four GPIO procedure calls
 * per transferred bit can make performance a problem, this code
 * is set up so that you can use it in either of two ways:
 *
 *   - The slow generic way:  set up platform_data to hold the GPIO
 *     numbers used for MISO/MOSI/SCK, and issue procedure calls for
 *     each of them.  This driver can handle several such busses.
 *
 *   - The quicker inlined way:  only helps with platform GPIO code
 *     that inlines operations for constant GPIOs.  This can give
 *     you tight (fast!) inner loops, but each such bus needs a
 *     new driver.  You'll define a new C file, with Makefile and
 *     Kconfig support; the C code can be a total of six lines:
 *
 *		#define DRIVER_NAME	"myboard_spi2"
 *		#define	SPI_MISO_GPIO	119
 *		#define	SPI_MOSI_GPIO	120
 *		#define	SPI_SCK_GPIO	121
 *		#define	SPI_N_CHIPSEL	4
Grant Likely's avatar
Grant Likely committed
63
 *		#include "spi-gpio.c"
David Brownell's avatar
David Brownell committed
64 65 66 67 68 69 70 71 72 73 74
 */

#ifndef DRIVER_NAME
#define DRIVER_NAME	"spi_gpio"

#define GENERIC_BITBANG	/* vs tight inlines */

#endif

/*----------------------------------------------------------------------*/

75
static inline struct spi_gpio *__pure
76
spi_to_spi_gpio(const struct spi_device *spi)
David Brownell's avatar
David Brownell committed
77 78
{
	const struct spi_bitbang	*bang;
79
	struct spi_gpio			*spi_gpio;
David Brownell's avatar
David Brownell committed
80 81 82

	bang = spi_master_get_devdata(spi->master);
	spi_gpio = container_of(bang, struct spi_gpio, bitbang);
83 84 85
	return spi_gpio;
}

86
/* These helpers are in turn called by the bitbang inlines */
David Brownell's avatar
David Brownell committed
87 88
static inline void setsck(const struct spi_device *spi, int is_on)
{
89 90 91
	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);

	gpiod_set_value_cansleep(spi_gpio->sck, is_on);
David Brownell's avatar
David Brownell committed
92 93 94 95
}

static inline void setmosi(const struct spi_device *spi, int is_on)
{
96 97 98
	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);

	gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
David Brownell's avatar
David Brownell committed
99 100 101 102
}

static inline int getmiso(const struct spi_device *spi)
{
103
	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
David Brownell's avatar
David Brownell committed
104

105 106 107 108
	if (spi->mode & SPI_3WIRE)
		return !!gpiod_get_value_cansleep(spi_gpio->mosi);
	else
		return !!gpiod_get_value_cansleep(spi_gpio->miso);
109
}
David Brownell's avatar
David Brownell committed
110 111 112 113 114 115 116 117 118

/*
 * NOTE:  this clocks "as fast as we can".  It "should" be a function of the
 * requested device clock.  Software overhead means we usually have trouble
 * reaching even one Mbit/sec (except when we can inline bitops), so for now
 * we'll just assume we never need additional per-bit slowdowns.
 */
#define spidelay(nsecs)	do {} while (0)

Grant Likely's avatar
Grant Likely committed
119
#include "spi-bitbang-txrx.h"
David Brownell's avatar
David Brownell committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

/*
 * These functions can leverage inline expansion of GPIO calls to shrink
 * costs for a txrx bit, often by factors of around ten (by instruction
 * count).  That is particularly visible for larger word sizes, but helps
 * even with default 8-bit words.
 *
 * REVISIT overheads calling these functions for each word also have
 * significant performance costs.  Having txrx_bufs() calls that inline
 * the txrx_word() logic would help performance, e.g. on larger blocks
 * used with flash storage or MMC/SD.  There should also be ways to make
 * GCC be less stupid about reloading registers inside the I/O loops,
 * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
 */

static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
136
		unsigned nsecs, u32 word, u8 bits, unsigned flags)
David Brownell's avatar
David Brownell committed
137
{
138
	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
David Brownell's avatar
David Brownell committed
139 140 141
}

static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
142
		unsigned nsecs, u32 word, u8 bits, unsigned flags)
David Brownell's avatar
David Brownell committed
143
{
144
	return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
David Brownell's avatar
David Brownell committed
145 146 147
}

static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
148
		unsigned nsecs, u32 word, u8 bits, unsigned flags)
David Brownell's avatar
David Brownell committed
149
{
150
	return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
David Brownell's avatar
David Brownell committed
151 152 153
}

static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
154
		unsigned nsecs, u32 word, u8 bits, unsigned flags)
David Brownell's avatar
David Brownell committed
155
{
156
	return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
David Brownell's avatar
David Brownell committed
157 158
}

159 160 161 162 163 164 165 166 167 168 169
/*
 * These functions do not call setmosi or getmiso if respective flag
 * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
 * call when such pin is not present or defined in the controller.
 * A separate set of callbacks is defined to get highest possible
 * speed in the generic case (when both MISO and MOSI lines are
 * available), as optimiser will remove the checks when argument is
 * constant.
 */

static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
170
		unsigned nsecs, u32 word, u8 bits, unsigned flags)
171
{
172
	flags = spi->master->flags;
173 174 175 176
	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
}

static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
177
		unsigned nsecs, u32 word, u8 bits, unsigned flags)
178
{
179
	flags = spi->master->flags;
180 181 182 183
	return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
}

static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
184
		unsigned nsecs, u32 word, u8 bits, unsigned flags)
185
{
186
	flags = spi->master->flags;
187 188 189 190
	return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
}

static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
191
		unsigned nsecs, u32 word, u8 bits, unsigned flags)
192
{
193
	flags = spi->master->flags;
194 195 196
	return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
}

David Brownell's avatar
David Brownell committed
197 198 199 200
/*----------------------------------------------------------------------*/

static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
{
201
	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
David Brownell's avatar
David Brownell committed
202

203
	/* set initial clock line level */
David Brownell's avatar
David Brownell committed
204
	if (is_active)
205 206 207
		gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);

	/* Drive chip select line, if we have one */
208
	if (spi_gpio->cs_gpios) {
209
		struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
David Brownell's avatar
David Brownell committed
210

211 212
		/* SPI chip selects are normally active-low */
		gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
213
	}
David Brownell's avatar
David Brownell committed
214 215 216 217
}

static int spi_gpio_setup(struct spi_device *spi)
{
218
	struct gpio_desc	*cs;
219 220
	int			status = 0;
	struct spi_gpio		*spi_gpio = spi_to_spi_gpio(spi);
Daniel Mack's avatar
Daniel Mack committed
221

222 223 224 225
	/*
	 * The CS GPIOs have already been
	 * initialized from the descriptor lookup.
	 */
226 227 228 229 230 231
	if (spi_gpio->cs_gpios) {
		cs = spi_gpio->cs_gpios[spi->chip_select];
		if (!spi->controller_state && cs)
			status = gpiod_direction_output(cs,
						  !(spi->mode & SPI_CS_HIGH));
	}
232 233

	if (!status)
234
		status = spi_bitbang_setup(spi);
235

David Brownell's avatar
David Brownell committed
236 237 238
	return status;
}

239 240 241
static int spi_gpio_set_direction(struct spi_device *spi, bool output)
{
	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
242
	int ret;
243 244 245

	if (output)
		return gpiod_direction_output(spi_gpio->mosi, 1);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	ret = gpiod_direction_input(spi_gpio->mosi);
	if (ret)
		return ret;
	/*
	 * Send a turnaround high impedance cycle when switching
	 * from output to input. Theoretically there should be
	 * a clock delay here, but as has been noted above, the
	 * nsec delay function for bit-banged GPIO is simply
	 * {} because bit-banging just doesn't get fast enough
	 * anyway.
	 */
	if (spi->mode & SPI_3WIRE_HIZ) {
		gpiod_set_value_cansleep(spi_gpio->sck,
					 !(spi->mode & SPI_CPOL));
		gpiod_set_value_cansleep(spi_gpio->sck,
					 !!(spi->mode & SPI_CPOL));
	}
	return 0;
265 266
}

David Brownell's avatar
David Brownell committed
267 268 269 270 271
static void spi_gpio_cleanup(struct spi_device *spi)
{
	spi_bitbang_cleanup(spi);
}

272 273 274 275 276 277 278 279 280 281
/*
 * It can be convenient to use this driver with pins that have alternate
 * functions associated with a "native" SPI controller if a driver for that
 * controller is not available, or is missing important functionality.
 *
 * On platforms which can do so, configure MISO with a weak pullup unless
 * there's an external pullup on that signal.  That saves power by avoiding
 * floating signals.  (A weak pulldown would save power too, but many
 * drivers expect to see all-ones data as the no slave "response".)
 */
282
static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
David Brownell's avatar
David Brownell committed
283
{
284 285 286
	spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
	if (IS_ERR(spi_gpio->mosi))
		return PTR_ERR(spi_gpio->mosi);
David Brownell's avatar
David Brownell committed
287

288 289 290
	spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
	if (IS_ERR(spi_gpio->miso))
		return PTR_ERR(spi_gpio->miso);
David Brownell's avatar
David Brownell committed
291

292
	spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
293 294
	if (IS_ERR(spi_gpio->sck))
		return PTR_ERR(spi_gpio->sck);
David Brownell's avatar
David Brownell committed
295

296
	return 0;
David Brownell's avatar
David Brownell committed
297 298
}

Daniel Mack's avatar
Daniel Mack committed
299
#ifdef CONFIG_OF
300
static const struct of_device_id spi_gpio_dt_ids[] = {
Daniel Mack's avatar
Daniel Mack committed
301 302 303 304 305
	{ .compatible = "spi-gpio" },
	{}
};
MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);

306 307
static int spi_gpio_probe_dt(struct platform_device *pdev,
			     struct spi_master *master)
Daniel Mack's avatar
Daniel Mack committed
308
{
309 310
	master->dev.of_node = pdev->dev.of_node;
	master->use_gpio_descriptors = true;
Daniel Mack's avatar
Daniel Mack committed
311

312 313 314 315 316 317 318 319 320
	return 0;
}
#else
static inline int spi_gpio_probe_dt(struct platform_device *pdev,
				    struct spi_master *master)
{
	return 0;
}
#endif
Daniel Mack's avatar
Daniel Mack committed
321

322 323 324 325 326 327 328
static int spi_gpio_probe_pdata(struct platform_device *pdev,
				struct spi_master *master)
{
	struct device *dev = &pdev->dev;
	struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
	struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
	int i;
Daniel Mack's avatar
Daniel Mack committed
329

330 331 332 333 334 335 336 337 338
#ifdef GENERIC_BITBANG
	if (!pdata || !pdata->num_chipselect)
		return -ENODEV;
#endif
	/*
	 * The master needs to think there is a chipselect even if not
	 * connected
	 */
	master->num_chipselect = pdata->num_chipselect ?: 1;
Daniel Mack's avatar
Daniel Mack committed
339

340 341 342 343 344
	spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
					  sizeof(*spi_gpio->cs_gpios),
					  GFP_KERNEL);
	if (!spi_gpio->cs_gpios)
		return -ENOMEM;
Daniel Mack's avatar
Daniel Mack committed
345

346 347 348 349 350 351
	for (i = 0; i < master->num_chipselect; i++) {
		spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
							     GPIOD_OUT_HIGH);
		if (IS_ERR(spi_gpio->cs_gpios[i]))
			return PTR_ERR(spi_gpio->cs_gpios[i]);
	}
Daniel Mack's avatar
Daniel Mack committed
352 353 354 355

	return 0;
}

356 357 358 359 360
static void spi_gpio_put(void *data)
{
	spi_master_put(data);
}

361
static int spi_gpio_probe(struct platform_device *pdev)
David Brownell's avatar
David Brownell committed
362 363 364 365
{
	int				status;
	struct spi_master		*master;
	struct spi_gpio			*spi_gpio;
366
	struct device			*dev = &pdev->dev;
367
	struct spi_bitbang		*bb;
368
	const struct of_device_id	*of_id;
David Brownell's avatar
David Brownell committed
369

370
	of_id = of_match_device(spi_gpio_dt_ids, &pdev->dev);
David Brownell's avatar
David Brownell committed
371

372
	master = spi_alloc_master(dev, sizeof(*spi_gpio));
373 374
	if (!master)
		return -ENOMEM;
David Brownell's avatar
David Brownell committed
375

376 377 378 379
	status = devm_add_action_or_reset(&pdev->dev, spi_gpio_put, master);
	if (status)
		return status;

380 381 382 383
	if (of_id)
		status = spi_gpio_probe_dt(pdev, master);
	else
		status = spi_gpio_probe_pdata(pdev, master);
384

385 386
	if (status)
		return status;
387

388
	spi_gpio = spi_master_get_devdata(master);
David Brownell's avatar
David Brownell committed
389

390
	status = spi_gpio_request(dev, spi_gpio);
391 392 393
	if (status)
		return status;

394
	master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
395 396
	master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
			    SPI_CS_HIGH;
397 398 399 400 401 402 403 404 405 406
	if (!spi_gpio->mosi) {
		/* HW configuration without MOSI pin
		 *
		 * No setting SPI_MASTER_NO_RX here - if there is only
		 * a MOSI pin connected the host can still do RX by
		 * changing the direction of the line.
		 */
		master->flags = SPI_MASTER_NO_TX;
	}

David Brownell's avatar
David Brownell committed
407 408 409
	master->bus_num = pdev->id;
	master->setup = spi_gpio_setup;
	master->cleanup = spi_gpio_cleanup;
410

411 412
	bb = &spi_gpio->bitbang;
	bb->master = master;
413 414 415 416 417 418
	/*
	 * There is some additional business, apart from driving the CS GPIO
	 * line, that we need to do on selection. This makes the local
	 * callback for chipselect always get called.
	 */
	master->flags |= SPI_MASTER_GPIO_SS;
419 420
	bb->chipselect = spi_gpio_chipselect;
	bb->set_line_direction = spi_gpio_set_direction;
421

422
	if (master->flags & SPI_MASTER_NO_TX) {
423 424 425 426
		bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
		bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
		bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
		bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
427 428 429 430 431
	} else {
		bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
		bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
		bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
		bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
432
	}
433
	bb->setup_transfer = spi_bitbang_setup_transfer;
David Brownell's avatar
David Brownell committed
434

435 436 437
	status = spi_bitbang_init(&spi_gpio->bitbang);
	if (status)
		return status;
David Brownell's avatar
David Brownell committed
438

439
	return devm_spi_register_master(&pdev->dev, spi_master_get(master));
David Brownell's avatar
David Brownell committed
440 441 442 443 444
}

MODULE_ALIAS("platform:" DRIVER_NAME);

static struct platform_driver spi_gpio_driver = {
Daniel Mack's avatar
Daniel Mack committed
445 446 447 448
	.driver = {
		.name	= DRIVER_NAME,
		.of_match_table = of_match_ptr(spi_gpio_dt_ids),
	},
449
	.probe		= spi_gpio_probe,
David Brownell's avatar
David Brownell committed
450
};
451
module_platform_driver(spi_gpio_driver);
David Brownell's avatar
David Brownell committed
452 453 454 455

MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
MODULE_AUTHOR("David Brownell");
MODULE_LICENSE("GPL");