spi-gpio.c 12.3 KB
Newer Older
D
David Brownell 已提交
1
/*
G
Grant Likely 已提交
2
 * SPI master driver using generic bitbanged GPIO
D
David Brownell 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Copyright (C) 2006,2008 David Brownell
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <linux/kernel.h>
21
#include <linux/module.h>
D
David Brownell 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>

#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->controller_data ... holds chipselect GPIO
 *
 * spi->master->dev.driver_data ... points to spi_gpio->bitbang
 */

struct spi_gpio {
	struct spi_bitbang		bitbang;
	struct spi_gpio_platform_data	pdata;
	struct platform_device		*pdev;
49
	int				cs_gpios[0];
D
David Brownell 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
};

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

/*
 * 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
G
Grant Likely 已提交
74
 *		#include "spi-gpio.c"
D
David Brownell 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
 */

#ifndef DRIVER_NAME
#define DRIVER_NAME	"spi_gpio"

#define GENERIC_BITBANG	/* vs tight inlines */

/* all functions referencing these symbols must define pdata */
#define SPI_MISO_GPIO	((pdata)->miso)
#define SPI_MOSI_GPIO	((pdata)->mosi)
#define SPI_SCK_GPIO	((pdata)->sck)

#define SPI_N_CHIPSEL	((pdata)->num_chipselect)

#endif

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

93 94
static inline struct spi_gpio * __pure
spi_to_spi_gpio(const struct spi_device *spi)
D
David Brownell 已提交
95 96
{
	const struct spi_bitbang	*bang;
97
	struct spi_gpio			*spi_gpio;
D
David Brownell 已提交
98 99 100

	bang = spi_master_get_devdata(spi->master);
	spi_gpio = container_of(bang, struct spi_gpio, bitbang);
101 102 103 104 105 106 107
	return spi_gpio;
}

static inline struct spi_gpio_platform_data * __pure
spi_to_pdata(const struct spi_device *spi)
{
	return &spi_to_spi_gpio(spi)->pdata;
D
David Brownell 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

/* this is #defined to avoid unused-variable warnings when inlining */
#define pdata		spi_to_pdata(spi)

static inline void setsck(const struct spi_device *spi, int is_on)
{
	gpio_set_value(SPI_SCK_GPIO, is_on);
}

static inline void setmosi(const struct spi_device *spi, int is_on)
{
	gpio_set_value(SPI_MOSI_GPIO, is_on);
}

static inline int getmiso(const struct spi_device *spi)
{
125
	return !!gpio_get_value(SPI_MISO_GPIO);
D
David Brownell 已提交
126 127 128 129 130 131 132 133 134 135 136 137
}

#undef pdata

/*
 * 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)

G
Grant Likely 已提交
138
#include "spi-bitbang-txrx.h"
D
David Brownell 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

/*
 * 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,
		unsigned nsecs, u32 word, u8 bits)
{
157
	return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
D
David Brownell 已提交
158 159 160 161 162
}

static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
		unsigned nsecs, u32 word, u8 bits)
{
163
	return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits);
D
David Brownell 已提交
164 165 166 167 168
}

static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
		unsigned nsecs, u32 word, u8 bits)
{
169
	return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits);
D
David Brownell 已提交
170 171 172 173 174
}

static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
		unsigned nsecs, u32 word, u8 bits)
{
175
	return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits);
D
David Brownell 已提交
176 177
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/*
 * 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,
		unsigned nsecs, u32 word, u8 bits)
{
	unsigned flags = spi->master->flags;
	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
}

static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
		unsigned nsecs, u32 word, u8 bits)
{
	unsigned flags = spi->master->flags;
	return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
}

static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
		unsigned nsecs, u32 word, u8 bits)
{
	unsigned flags = spi->master->flags;
	return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
}

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

D
David Brownell 已提交
216 217 218 219
/*----------------------------------------------------------------------*/

static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
{
220 221
	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
	unsigned int cs = spi_gpio->cs_gpios[spi->chip_select];
D
David Brownell 已提交
222 223 224 225 226

	/* set initial clock polarity */
	if (is_active)
		setsck(spi, spi->mode & SPI_CPOL);

227 228 229 230
	if (cs != SPI_GPIO_NO_CHIPSELECT) {
		/* SPI is normally active-low */
		gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
	}
D
David Brownell 已提交
231 232 233 234
}

static int spi_gpio_setup(struct spi_device *spi)
{
235 236 237
	unsigned int		cs = (unsigned int) spi->controller_data;
	int			status = 0;
	struct spi_gpio		*spi_gpio = spi_to_spi_gpio(spi);
D
David Brownell 已提交
238 239 240 241 242

	if (spi->bits_per_word > 32)
		return -EINVAL;

	if (!spi->controller_state) {
243 244 245 246
		if (cs != SPI_GPIO_NO_CHIPSELECT) {
			status = gpio_request(cs, dev_name(&spi->dev));
			if (status)
				return status;
247 248
			status = gpio_direction_output(cs,
					!(spi->mode & SPI_CS_HIGH));
249
		}
D
David Brownell 已提交
250
	}
251
	if (!status) {
D
David Brownell 已提交
252
		status = spi_bitbang_setup(spi);
253 254 255
		spi_gpio->cs_gpios[spi->chip_select] = cs;
	}

D
David Brownell 已提交
256
	if (status) {
257
		if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
D
David Brownell 已提交
258 259 260 261 262 263 264
			gpio_free(cs);
	}
	return status;
}

static void spi_gpio_cleanup(struct spi_device *spi)
{
265 266
	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
	unsigned int cs = spi_gpio->cs_gpios[spi->chip_select];
D
David Brownell 已提交
267

268 269
	if (cs != SPI_GPIO_NO_CHIPSELECT)
		gpio_free(cs);
D
David Brownell 已提交
270 271 272
	spi_bitbang_cleanup(spi);
}

273
static int __devinit spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
D
David Brownell 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286
{
	int value;

	value = gpio_request(pin, label);
	if (value == 0) {
		if (is_in)
			value = gpio_direction_input(pin);
		else
			value = gpio_direction_output(pin, 0);
	}
	return value;
}

287
static int __devinit
288 289
spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label,
	u16 *res_flags)
D
David Brownell 已提交
290 291 292 293 294
{
	int value;

	/* NOTE:  SPI_*_GPIO symbols may reference "pdata" */

295 296 297 298 299 300 301 302
	if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) {
		value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
		if (value)
			goto done;
	} else {
		/* HW configuration without MOSI pin */
		*res_flags |= SPI_MASTER_NO_TX;
	}
D
David Brownell 已提交
303

304 305 306 307 308 309 310 311
	if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) {
		value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
		if (value)
			goto free_mosi;
	} else {
		/* HW configuration without MISO pin */
		*res_flags |= SPI_MASTER_NO_RX;
	}
D
David Brownell 已提交
312 313 314 315 316 317 318 319

	value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
	if (value)
		goto free_miso;

	goto done;

free_miso:
320 321
	if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
		gpio_free(SPI_MISO_GPIO);
D
David Brownell 已提交
322
free_mosi:
323 324
	if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
		gpio_free(SPI_MOSI_GPIO);
D
David Brownell 已提交
325 326 327 328
done:
	return value;
}

329
static int __devinit spi_gpio_probe(struct platform_device *pdev)
D
David Brownell 已提交
330 331 332 333 334
{
	int				status;
	struct spi_master		*master;
	struct spi_gpio			*spi_gpio;
	struct spi_gpio_platform_data	*pdata;
335
	u16 master_flags = 0;
D
David Brownell 已提交
336 337 338 339 340 341 342

	pdata = pdev->dev.platform_data;
#ifdef GENERIC_BITBANG
	if (!pdata || !pdata->num_chipselect)
		return -ENODEV;
#endif

343
	status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags);
D
David Brownell 已提交
344 345 346
	if (status < 0)
		return status;

347 348
	master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) +
					(sizeof(int) * SPI_N_CHIPSEL));
D
David Brownell 已提交
349 350 351 352 353 354 355 356 357 358 359
	if (!master) {
		status = -ENOMEM;
		goto gpio_free;
	}
	spi_gpio = spi_master_get_devdata(master);
	platform_set_drvdata(pdev, spi_gpio);

	spi_gpio->pdev = pdev;
	if (pdata)
		spi_gpio->pdata = *pdata;

360
	master->flags = master_flags;
D
David Brownell 已提交
361 362 363 364 365 366 367
	master->bus_num = pdev->id;
	master->num_chipselect = SPI_N_CHIPSEL;
	master->setup = spi_gpio_setup;
	master->cleanup = spi_gpio_cleanup;

	spi_gpio->bitbang.master = spi_master_get(master);
	spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
368

369
	if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
370 371 372 373 374 375 376 377 378 379
		spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
		spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
		spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
		spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
	} else {
		spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
		spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
		spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
		spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
	}
D
David Brownell 已提交
380 381 382 383 384 385 386
	spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
	spi_gpio->bitbang.flags = SPI_CS_HIGH;

	status = spi_bitbang_start(&spi_gpio->bitbang);
	if (status < 0) {
		spi_master_put(spi_gpio->bitbang.master);
gpio_free:
387 388 389 390
		if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
			gpio_free(SPI_MISO_GPIO);
		if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
			gpio_free(SPI_MOSI_GPIO);
D
David Brownell 已提交
391 392 393 394 395 396 397
		gpio_free(SPI_SCK_GPIO);
		spi_master_put(master);
	}

	return status;
}

398
static int __devexit spi_gpio_remove(struct platform_device *pdev)
D
David Brownell 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412
{
	struct spi_gpio			*spi_gpio;
	struct spi_gpio_platform_data	*pdata;
	int				status;

	spi_gpio = platform_get_drvdata(pdev);
	pdata = pdev->dev.platform_data;

	/* stop() unregisters child devices too */
	status = spi_bitbang_stop(&spi_gpio->bitbang);
	spi_master_put(spi_gpio->bitbang.master);

	platform_set_drvdata(pdev, NULL);

413 414 415 416
	if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
		gpio_free(SPI_MISO_GPIO);
	if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
		gpio_free(SPI_MOSI_GPIO);
D
David Brownell 已提交
417 418 419 420 421 422 423 424 425 426
	gpio_free(SPI_SCK_GPIO);

	return status;
}

MODULE_ALIAS("platform:" DRIVER_NAME);

static struct platform_driver spi_gpio_driver = {
	.driver.name	= DRIVER_NAME,
	.driver.owner	= THIS_MODULE,
427 428
	.probe		= spi_gpio_probe,
	.remove		= __devexit_p(spi_gpio_remove),
D
David Brownell 已提交
429
};
430
module_platform_driver(spi_gpio_driver);
D
David Brownell 已提交
431 432 433 434

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