David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 1 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 2 | * SPI master driver using generic bitbanged GPIO |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2006,2008 David Brownell |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | #include <linux/kernel.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/gpio.h> |
| 25 | |
| 26 | #include <linux/spi/spi.h> |
| 27 | #include <linux/spi/spi_bitbang.h> |
| 28 | #include <linux/spi/spi_gpio.h> |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * This bitbanging SPI master driver should help make systems usable |
| 33 | * when a native hardware SPI engine is not available, perhaps because |
| 34 | * its driver isn't yet working or because the I/O pins it requires |
| 35 | * are used for other purposes. |
| 36 | * |
| 37 | * platform_device->driver_data ... points to spi_gpio |
| 38 | * |
| 39 | * spi->controller_state ... reserved for bitbang framework code |
| 40 | * spi->controller_data ... holds chipselect GPIO |
| 41 | * |
| 42 | * spi->master->dev.driver_data ... points to spi_gpio->bitbang |
| 43 | */ |
| 44 | |
| 45 | struct spi_gpio { |
| 46 | struct spi_bitbang bitbang; |
| 47 | struct spi_gpio_platform_data pdata; |
| 48 | struct platform_device *pdev; |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 49 | int cs_gpios[0]; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /*----------------------------------------------------------------------*/ |
| 53 | |
| 54 | /* |
| 55 | * Because the overhead of going through four GPIO procedure calls |
| 56 | * per transferred bit can make performance a problem, this code |
| 57 | * is set up so that you can use it in either of two ways: |
| 58 | * |
| 59 | * - The slow generic way: set up platform_data to hold the GPIO |
| 60 | * numbers used for MISO/MOSI/SCK, and issue procedure calls for |
| 61 | * each of them. This driver can handle several such busses. |
| 62 | * |
| 63 | * - The quicker inlined way: only helps with platform GPIO code |
| 64 | * that inlines operations for constant GPIOs. This can give |
| 65 | * you tight (fast!) inner loops, but each such bus needs a |
| 66 | * new driver. You'll define a new C file, with Makefile and |
| 67 | * Kconfig support; the C code can be a total of six lines: |
| 68 | * |
| 69 | * #define DRIVER_NAME "myboard_spi2" |
| 70 | * #define SPI_MISO_GPIO 119 |
| 71 | * #define SPI_MOSI_GPIO 120 |
| 72 | * #define SPI_SCK_GPIO 121 |
| 73 | * #define SPI_N_CHIPSEL 4 |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 74 | * #include "spi-gpio.c" |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 75 | */ |
| 76 | |
| 77 | #ifndef DRIVER_NAME |
| 78 | #define DRIVER_NAME "spi_gpio" |
| 79 | |
| 80 | #define GENERIC_BITBANG /* vs tight inlines */ |
| 81 | |
| 82 | /* all functions referencing these symbols must define pdata */ |
| 83 | #define SPI_MISO_GPIO ((pdata)->miso) |
| 84 | #define SPI_MOSI_GPIO ((pdata)->mosi) |
| 85 | #define SPI_SCK_GPIO ((pdata)->sck) |
| 86 | |
| 87 | #define SPI_N_CHIPSEL ((pdata)->num_chipselect) |
| 88 | |
| 89 | #endif |
| 90 | |
| 91 | /*----------------------------------------------------------------------*/ |
| 92 | |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 93 | static inline struct spi_gpio * __pure |
| 94 | spi_to_spi_gpio(const struct spi_device *spi) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 95 | { |
| 96 | const struct spi_bitbang *bang; |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 97 | struct spi_gpio *spi_gpio; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 98 | |
| 99 | bang = spi_master_get_devdata(spi->master); |
| 100 | spi_gpio = container_of(bang, struct spi_gpio, bitbang); |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 101 | return spi_gpio; |
| 102 | } |
| 103 | |
| 104 | static inline struct spi_gpio_platform_data * __pure |
| 105 | spi_to_pdata(const struct spi_device *spi) |
| 106 | { |
| 107 | return &spi_to_spi_gpio(spi)->pdata; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* this is #defined to avoid unused-variable warnings when inlining */ |
| 111 | #define pdata spi_to_pdata(spi) |
| 112 | |
| 113 | static inline void setsck(const struct spi_device *spi, int is_on) |
| 114 | { |
| 115 | gpio_set_value(SPI_SCK_GPIO, is_on); |
| 116 | } |
| 117 | |
| 118 | static inline void setmosi(const struct spi_device *spi, int is_on) |
| 119 | { |
| 120 | gpio_set_value(SPI_MOSI_GPIO, is_on); |
| 121 | } |
| 122 | |
| 123 | static inline int getmiso(const struct spi_device *spi) |
| 124 | { |
Michael Buesch | be50344 | 2009-02-18 14:48:41 -0800 | [diff] [blame] | 125 | return !!gpio_get_value(SPI_MISO_GPIO); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | #undef pdata |
| 129 | |
| 130 | /* |
| 131 | * NOTE: this clocks "as fast as we can". It "should" be a function of the |
| 132 | * requested device clock. Software overhead means we usually have trouble |
| 133 | * reaching even one Mbit/sec (except when we can inline bitops), so for now |
| 134 | * we'll just assume we never need additional per-bit slowdowns. |
| 135 | */ |
| 136 | #define spidelay(nsecs) do {} while (0) |
| 137 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 138 | #include "spi-bitbang-txrx.h" |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * These functions can leverage inline expansion of GPIO calls to shrink |
| 142 | * costs for a txrx bit, often by factors of around ten (by instruction |
| 143 | * count). That is particularly visible for larger word sizes, but helps |
| 144 | * even with default 8-bit words. |
| 145 | * |
| 146 | * REVISIT overheads calling these functions for each word also have |
| 147 | * significant performance costs. Having txrx_bufs() calls that inline |
| 148 | * the txrx_word() logic would help performance, e.g. on larger blocks |
| 149 | * used with flash storage or MMC/SD. There should also be ways to make |
| 150 | * GCC be less stupid about reloading registers inside the I/O loops, |
| 151 | * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3? |
| 152 | */ |
| 153 | |
| 154 | static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, |
| 155 | unsigned nsecs, u32 word, u8 bits) |
| 156 | { |
Marek Szyprowski | 04bb2a0 | 2010-06-30 14:27:32 -0600 | [diff] [blame] | 157 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, |
| 161 | unsigned nsecs, u32 word, u8 bits) |
| 162 | { |
Marek Szyprowski | 04bb2a0 | 2010-06-30 14:27:32 -0600 | [diff] [blame] | 163 | return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, |
| 167 | unsigned nsecs, u32 word, u8 bits) |
| 168 | { |
Marek Szyprowski | 04bb2a0 | 2010-06-30 14:27:32 -0600 | [diff] [blame] | 169 | return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, |
| 173 | unsigned nsecs, u32 word, u8 bits) |
| 174 | { |
Marek Szyprowski | 04bb2a0 | 2010-06-30 14:27:32 -0600 | [diff] [blame] | 175 | return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 178 | /* |
| 179 | * These functions do not call setmosi or getmiso if respective flag |
| 180 | * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to |
| 181 | * call when such pin is not present or defined in the controller. |
| 182 | * A separate set of callbacks is defined to get highest possible |
| 183 | * speed in the generic case (when both MISO and MOSI lines are |
| 184 | * available), as optimiser will remove the checks when argument is |
| 185 | * constant. |
| 186 | */ |
| 187 | |
| 188 | static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, |
| 189 | unsigned nsecs, u32 word, u8 bits) |
| 190 | { |
| 191 | unsigned flags = spi->master->flags; |
| 192 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); |
| 193 | } |
| 194 | |
| 195 | static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, |
| 196 | unsigned nsecs, u32 word, u8 bits) |
| 197 | { |
| 198 | unsigned flags = spi->master->flags; |
| 199 | return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); |
| 200 | } |
| 201 | |
| 202 | static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, |
| 203 | unsigned nsecs, u32 word, u8 bits) |
| 204 | { |
| 205 | unsigned flags = spi->master->flags; |
| 206 | return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); |
| 207 | } |
| 208 | |
| 209 | static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, |
| 210 | unsigned nsecs, u32 word, u8 bits) |
| 211 | { |
| 212 | unsigned flags = spi->master->flags; |
| 213 | return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); |
| 214 | } |
| 215 | |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 216 | /*----------------------------------------------------------------------*/ |
| 217 | |
| 218 | static void spi_gpio_chipselect(struct spi_device *spi, int is_active) |
| 219 | { |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 220 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
| 221 | unsigned int cs = spi_gpio->cs_gpios[spi->chip_select]; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 222 | |
| 223 | /* set initial clock polarity */ |
| 224 | if (is_active) |
| 225 | setsck(spi, spi->mode & SPI_CPOL); |
| 226 | |
Michael Buesch | bfb9bcd | 2009-04-02 16:57:07 -0700 | [diff] [blame] | 227 | if (cs != SPI_GPIO_NO_CHIPSELECT) { |
| 228 | /* SPI is normally active-low */ |
| 229 | gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); |
| 230 | } |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int spi_gpio_setup(struct spi_device *spi) |
| 234 | { |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 235 | unsigned int cs = (unsigned int) spi->controller_data; |
| 236 | int status = 0; |
| 237 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 238 | |
| 239 | if (spi->bits_per_word > 32) |
| 240 | return -EINVAL; |
| 241 | |
| 242 | if (!spi->controller_state) { |
Michael Buesch | bfb9bcd | 2009-04-02 16:57:07 -0700 | [diff] [blame] | 243 | if (cs != SPI_GPIO_NO_CHIPSELECT) { |
| 244 | status = gpio_request(cs, dev_name(&spi->dev)); |
| 245 | if (status) |
| 246 | return status; |
Uwe Kleine-König | 0564414 | 2012-02-09 22:21:45 +0100 | [diff] [blame] | 247 | status = gpio_direction_output(cs, |
| 248 | !(spi->mode & SPI_CS_HIGH)); |
Michael Buesch | bfb9bcd | 2009-04-02 16:57:07 -0700 | [diff] [blame] | 249 | } |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 250 | } |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 251 | if (!status) { |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 252 | status = spi_bitbang_setup(spi); |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 253 | spi_gpio->cs_gpios[spi->chip_select] = cs; |
| 254 | } |
| 255 | |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 256 | if (status) { |
Michael Buesch | bfb9bcd | 2009-04-02 16:57:07 -0700 | [diff] [blame] | 257 | if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 258 | gpio_free(cs); |
| 259 | } |
| 260 | return status; |
| 261 | } |
| 262 | |
| 263 | static void spi_gpio_cleanup(struct spi_device *spi) |
| 264 | { |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 265 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
| 266 | unsigned int cs = spi_gpio->cs_gpios[spi->chip_select]; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 267 | |
Michael Buesch | bfb9bcd | 2009-04-02 16:57:07 -0700 | [diff] [blame] | 268 | if (cs != SPI_GPIO_NO_CHIPSELECT) |
| 269 | gpio_free(cs); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 270 | spi_bitbang_cleanup(spi); |
| 271 | } |
| 272 | |
Manuel Lauss | c65b53b | 2011-11-19 13:08:10 +0100 | [diff] [blame] | 273 | static int __devinit spi_gpio_alloc(unsigned pin, const char *label, bool is_in) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 274 | { |
| 275 | int value; |
| 276 | |
| 277 | value = gpio_request(pin, label); |
| 278 | if (value == 0) { |
| 279 | if (is_in) |
| 280 | value = gpio_direction_input(pin); |
| 281 | else |
| 282 | value = gpio_direction_output(pin, 0); |
| 283 | } |
| 284 | return value; |
| 285 | } |
| 286 | |
Manuel Lauss | c65b53b | 2011-11-19 13:08:10 +0100 | [diff] [blame] | 287 | static int __devinit |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 288 | spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label, |
| 289 | u16 *res_flags) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 290 | { |
| 291 | int value; |
| 292 | |
| 293 | /* NOTE: SPI_*_GPIO symbols may reference "pdata" */ |
| 294 | |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 295 | if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) { |
| 296 | value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false); |
| 297 | if (value) |
| 298 | goto done; |
| 299 | } else { |
| 300 | /* HW configuration without MOSI pin */ |
| 301 | *res_flags |= SPI_MASTER_NO_TX; |
| 302 | } |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 303 | |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 304 | if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) { |
| 305 | value = spi_gpio_alloc(SPI_MISO_GPIO, label, true); |
| 306 | if (value) |
| 307 | goto free_mosi; |
| 308 | } else { |
| 309 | /* HW configuration without MISO pin */ |
| 310 | *res_flags |= SPI_MASTER_NO_RX; |
| 311 | } |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 312 | |
| 313 | value = spi_gpio_alloc(SPI_SCK_GPIO, label, false); |
| 314 | if (value) |
| 315 | goto free_miso; |
| 316 | |
| 317 | goto done; |
| 318 | |
| 319 | free_miso: |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 320 | if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) |
| 321 | gpio_free(SPI_MISO_GPIO); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 322 | free_mosi: |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 323 | if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) |
| 324 | gpio_free(SPI_MOSI_GPIO); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 325 | done: |
| 326 | return value; |
| 327 | } |
| 328 | |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 329 | static int __devinit spi_gpio_probe(struct platform_device *pdev) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 330 | { |
| 331 | int status; |
| 332 | struct spi_master *master; |
| 333 | struct spi_gpio *spi_gpio; |
| 334 | struct spi_gpio_platform_data *pdata; |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 335 | u16 master_flags = 0; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 336 | |
| 337 | pdata = pdev->dev.platform_data; |
| 338 | #ifdef GENERIC_BITBANG |
| 339 | if (!pdata || !pdata->num_chipselect) |
| 340 | return -ENODEV; |
| 341 | #endif |
| 342 | |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 343 | status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 344 | if (status < 0) |
| 345 | return status; |
| 346 | |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame^] | 347 | master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) + |
| 348 | (sizeof(int) * SPI_N_CHIPSEL)); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 349 | if (!master) { |
| 350 | status = -ENOMEM; |
| 351 | goto gpio_free; |
| 352 | } |
| 353 | spi_gpio = spi_master_get_devdata(master); |
| 354 | platform_set_drvdata(pdev, spi_gpio); |
| 355 | |
| 356 | spi_gpio->pdev = pdev; |
| 357 | if (pdata) |
| 358 | spi_gpio->pdata = *pdata; |
| 359 | |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 360 | master->flags = master_flags; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 361 | master->bus_num = pdev->id; |
| 362 | master->num_chipselect = SPI_N_CHIPSEL; |
| 363 | master->setup = spi_gpio_setup; |
| 364 | master->cleanup = spi_gpio_cleanup; |
| 365 | |
| 366 | spi_gpio->bitbang.master = spi_master_get(master); |
| 367 | spi_gpio->bitbang.chipselect = spi_gpio_chipselect; |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 368 | |
Roel Kluin | 23699f9 | 2010-10-02 14:03:32 +0200 | [diff] [blame] | 369 | if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) { |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 370 | spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; |
| 371 | spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; |
| 372 | spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; |
| 373 | spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; |
| 374 | } else { |
| 375 | spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; |
| 376 | spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; |
| 377 | spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; |
| 378 | spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; |
| 379 | } |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 380 | spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; |
| 381 | spi_gpio->bitbang.flags = SPI_CS_HIGH; |
| 382 | |
| 383 | status = spi_bitbang_start(&spi_gpio->bitbang); |
| 384 | if (status < 0) { |
| 385 | spi_master_put(spi_gpio->bitbang.master); |
| 386 | gpio_free: |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 387 | if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) |
| 388 | gpio_free(SPI_MISO_GPIO); |
| 389 | if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) |
| 390 | gpio_free(SPI_MOSI_GPIO); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 391 | gpio_free(SPI_SCK_GPIO); |
| 392 | spi_master_put(master); |
| 393 | } |
| 394 | |
| 395 | return status; |
| 396 | } |
| 397 | |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 398 | static int __devexit spi_gpio_remove(struct platform_device *pdev) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 399 | { |
| 400 | struct spi_gpio *spi_gpio; |
| 401 | struct spi_gpio_platform_data *pdata; |
| 402 | int status; |
| 403 | |
| 404 | spi_gpio = platform_get_drvdata(pdev); |
| 405 | pdata = pdev->dev.platform_data; |
| 406 | |
| 407 | /* stop() unregisters child devices too */ |
| 408 | status = spi_bitbang_stop(&spi_gpio->bitbang); |
| 409 | spi_master_put(spi_gpio->bitbang.master); |
| 410 | |
| 411 | platform_set_drvdata(pdev, NULL); |
| 412 | |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 413 | if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) |
| 414 | gpio_free(SPI_MISO_GPIO); |
| 415 | if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) |
| 416 | gpio_free(SPI_MOSI_GPIO); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 417 | gpio_free(SPI_SCK_GPIO); |
| 418 | |
| 419 | return status; |
| 420 | } |
| 421 | |
| 422 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 423 | |
| 424 | static struct platform_driver spi_gpio_driver = { |
| 425 | .driver.name = DRIVER_NAME, |
| 426 | .driver.owner = THIS_MODULE, |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 427 | .probe = spi_gpio_probe, |
| 428 | .remove = __devexit_p(spi_gpio_remove), |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 429 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 430 | module_platform_driver(spi_gpio_driver); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 431 | |
| 432 | MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO "); |
| 433 | MODULE_AUTHOR("David Brownell"); |
| 434 | MODULE_LICENSE("GPL"); |