lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SPI master driver using generic bitbanged GPIO |
| 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> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/gpio.h> |
| 25 | #include <mach/gpio.h> |
| 26 | |
| 27 | #include <linux/spi/spi.h> |
| 28 | #include <linux/spi/spi_bitbang.h> |
| 29 | #include <linux/spi/spi_gpio.h> |
| 30 | |
| 31 | |
| 32 | /* |
| 33 | * This bitbanging SPI master driver should help make systems usable |
| 34 | * when a native hardware SPI engine is not available, perhaps because |
| 35 | * its driver isn't yet working or because the I/O pins it requires |
| 36 | * are used for other purposes. |
| 37 | * |
| 38 | * platform_device->driver_data ... points to spi_gpio |
| 39 | * |
| 40 | * spi->controller_state ... reserved for bitbang framework code |
| 41 | * spi->controller_data ... holds chipselect GPIO |
| 42 | * |
| 43 | * spi->master->dev.driver_data ... points to spi_gpio->bitbang |
| 44 | */ |
| 45 | |
| 46 | struct spi_gpio { |
| 47 | struct spi_bitbang bitbang; |
| 48 | struct spi_gpio_platform_data pdata; |
| 49 | struct platform_device *pdev; |
| 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 |
| 74 | * #include "spi-gpio.c" |
| 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 | |
| 93 | static inline const struct spi_gpio_platform_data * __pure |
| 94 | spi_to_pdata(const struct spi_device *spi) |
| 95 | { |
| 96 | const struct spi_bitbang *bang; |
| 97 | const struct spi_gpio *spi_gpio; |
| 98 | |
| 99 | bang = spi_master_get_devdata(spi->master); |
| 100 | spi_gpio = container_of(bang, struct spi_gpio, bitbang); |
| 101 | return &spi_gpio->pdata; |
| 102 | } |
| 103 | |
| 104 | /* this is #defined to avoid unused-variable warnings when inlining */ |
| 105 | #define pdata spi_to_pdata(spi) |
| 106 | |
| 107 | static inline void setsck(const struct spi_device *spi, int is_on) |
| 108 | { |
| 109 | gpio_set_value(SPI_SCK_GPIO, is_on); |
| 110 | } |
| 111 | |
| 112 | static inline void setmosi(const struct spi_device *spi, int is_on) |
| 113 | { |
| 114 | gpio_set_value(SPI_MOSI_GPIO, !!is_on); |
| 115 | } |
| 116 | |
| 117 | static inline int getmiso(const struct spi_device *spi) |
| 118 | { |
| 119 | return !!gpio_get_value(SPI_MISO_GPIO); |
| 120 | } |
| 121 | |
| 122 | #undef pdata |
| 123 | |
| 124 | /* |
| 125 | * NOTE: this clocks "as fast as we can". It "should" be a function of the |
| 126 | * requested device clock. Software overhead means we usually have trouble |
| 127 | * reaching even one Mbit/sec (except when we can inline bitops), so for now |
| 128 | * we'll just assume we never need additional per-bit slowdowns. |
| 129 | */ |
| 130 | #define spidelay(nsecs) do {} while (0) |
| 131 | |
| 132 | #include "spi-bitbang-txrx.h" |
| 133 | |
| 134 | /* |
| 135 | * These functions can leverage inline expansion of GPIO calls to shrink |
| 136 | * costs for a txrx bit, often by factors of around ten (by instruction |
| 137 | * count). That is particularly visible for larger word sizes, but helps |
| 138 | * even with default 8-bit words. |
| 139 | * |
| 140 | * REVISIT overheads calling these functions for each word also have |
| 141 | * significant performance costs. Having txrx_bufs() calls that inline |
| 142 | * the txrx_word() logic would help performance, e.g. on larger blocks |
| 143 | * used with flash storage or MMC/SD. There should also be ways to make |
| 144 | * GCC be less stupid about reloading registers inside the I/O loops, |
| 145 | * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3? |
| 146 | */ |
| 147 | |
| 148 | static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, |
| 149 | unsigned nsecs, u32 word, u8 bits) |
| 150 | { |
| 151 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits); |
| 152 | } |
| 153 | |
| 154 | static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, |
| 155 | unsigned nsecs, u32 word, u8 bits) |
| 156 | { |
| 157 | return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits); |
| 158 | } |
| 159 | |
| 160 | static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, |
| 161 | unsigned nsecs, u32 word, u8 bits) |
| 162 | { |
| 163 | return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits); |
| 164 | } |
| 165 | |
| 166 | static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, |
| 167 | unsigned nsecs, u32 word, u8 bits) |
| 168 | { |
| 169 | return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * These functions do not call setmosi or getmiso if respective flag |
| 174 | * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to |
| 175 | * call when such pin is not present or defined in the controller. |
| 176 | * A separate set of callbacks is defined to get highest possible |
| 177 | * speed in the generic case (when both MISO and MOSI lines are |
| 178 | * available), as optimiser will remove the checks when argument is |
| 179 | * constant. |
| 180 | */ |
| 181 | |
| 182 | static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, |
| 183 | unsigned nsecs, u32 word, u8 bits) |
| 184 | { |
| 185 | unsigned flags = spi->master->flags; |
| 186 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); |
| 187 | } |
| 188 | |
| 189 | static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, |
| 190 | unsigned nsecs, u32 word, u8 bits) |
| 191 | { |
| 192 | unsigned flags = spi->master->flags; |
| 193 | return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); |
| 194 | } |
| 195 | |
| 196 | static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, |
| 197 | unsigned nsecs, u32 word, u8 bits) |
| 198 | { |
| 199 | unsigned flags = spi->master->flags; |
| 200 | return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); |
| 201 | } |
| 202 | |
| 203 | static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, |
| 204 | unsigned nsecs, u32 word, u8 bits) |
| 205 | { |
| 206 | unsigned flags = spi->master->flags; |
| 207 | return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); |
| 208 | } |
| 209 | |
| 210 | /*----------------------------------------------------------------------*/ |
| 211 | |
| 212 | static void spi_gpio_chipselect(struct spi_device *spi, int is_active) |
| 213 | { |
| 214 | unsigned long cs = (unsigned long) spi->controller_data; |
| 215 | |
| 216 | /* set initial clock polarity */ |
| 217 | if (is_active) |
| 218 | setsck(spi, spi->mode & SPI_CPOL); |
| 219 | |
| 220 | if (cs != SPI_GPIO_NO_CHIPSELECT) { |
| 221 | /* SPI is normally active-low */ |
| 222 | gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static int spi_gpio_setup(struct spi_device *spi) |
| 227 | { |
| 228 | unsigned long cs = (unsigned long) spi->controller_data; |
| 229 | int status = 0; |
| 230 | |
| 231 | if (spi->bits_per_word > 32) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | if (!spi->controller_state) { |
| 235 | if (cs != SPI_GPIO_NO_CHIPSELECT) { |
| 236 | status = gpio_request(cs, dev_name(&spi->dev)); |
| 237 | if (status) |
| 238 | return status; |
| 239 | status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH); |
| 240 | } |
| 241 | } |
| 242 | if (!status) |
| 243 | status = spi_bitbang_setup(spi); |
| 244 | if (status) { |
| 245 | if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT) |
| 246 | gpio_free(cs); |
| 247 | } |
| 248 | return status; |
| 249 | } |
| 250 | |
| 251 | static void spi_gpio_cleanup(struct spi_device *spi) |
| 252 | { |
| 253 | unsigned long cs = (unsigned long) spi->controller_data; |
| 254 | |
| 255 | if (cs != SPI_GPIO_NO_CHIPSELECT) |
| 256 | gpio_free(cs); |
| 257 | spi_bitbang_cleanup(spi); |
| 258 | } |
| 259 | |
| 260 | static int __devinit spi_gpio_alloc(unsigned pin, const char *label, bool is_in) |
| 261 | { |
| 262 | int value; |
| 263 | |
| 264 | value = gpio_request(pin, label); |
| 265 | if (value == 0) { |
| 266 | if (is_in) |
| 267 | value = gpio_direction_input(pin); |
| 268 | else |
| 269 | value = gpio_direction_output(pin, 0); |
| 270 | } |
| 271 | return value; |
| 272 | } |
| 273 | |
| 274 | static int __devinit zx29_spi_gpio_alloc(unsigned pin, unsigned func, const char *label, bool is_in) |
| 275 | { |
| 276 | int value; |
| 277 | |
| 278 | value = gpio_request(pin, label); |
| 279 | if (value == 0) { |
| 280 | zx29_gpio_config(pin, func); |
| 281 | if (is_in) { |
| 282 | value = gpio_direction_input(pin); |
| 283 | zx29_gpio_pd_pu_set(pin, IO_CFG_PULL_UP); |
| 284 | } else { |
| 285 | value = gpio_direction_output(pin, 0); |
| 286 | zx29_gpio_pd_pu_set(pin, IO_CFG_PULL_DISABLE); |
| 287 | } |
| 288 | } |
| 289 | return value; |
| 290 | } |
| 291 | |
| 292 | static int __devinit |
| 293 | spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label, |
| 294 | u16 *res_flags) |
| 295 | { |
| 296 | int value; |
| 297 | |
| 298 | /* NOTE: SPI_*_GPIO symbols may reference "pdata" */ |
| 299 | |
| 300 | if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) { |
| 301 | value = zx29_spi_gpio_alloc(SPI_MOSI_GPIO, pdata->mosi_func, label, false); |
| 302 | if (value) |
| 303 | goto done; |
| 304 | } else { |
| 305 | /* HW configuration without MOSI pin */ |
| 306 | *res_flags |= SPI_MASTER_NO_TX; |
| 307 | } |
| 308 | |
| 309 | if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) { |
| 310 | value = zx29_spi_gpio_alloc(SPI_MISO_GPIO, pdata->miso_func, label, true); |
| 311 | if (value) |
| 312 | goto free_mosi; |
| 313 | } else { |
| 314 | /* HW configuration without MISO pin */ |
| 315 | *res_flags |= SPI_MASTER_NO_RX; |
| 316 | } |
| 317 | |
| 318 | value = zx29_spi_gpio_alloc(SPI_SCK_GPIO, pdata->sck_func, label, false); |
| 319 | if (value) |
| 320 | goto free_miso; |
| 321 | |
| 322 | goto done; |
| 323 | |
| 324 | free_miso: |
| 325 | if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) |
| 326 | gpio_free(SPI_MISO_GPIO); |
| 327 | free_mosi: |
| 328 | if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) |
| 329 | gpio_free(SPI_MOSI_GPIO); |
| 330 | done: |
| 331 | return value; |
| 332 | } |
| 333 | |
| 334 | static int __devinit spi_gpio_probe(struct platform_device *pdev) |
| 335 | { |
| 336 | int status; |
| 337 | struct spi_master *master; |
| 338 | struct spi_gpio *spi_gpio; |
| 339 | struct spi_gpio_platform_data *pdata; |
| 340 | u16 master_flags = 0; |
| 341 | |
| 342 | pdata = pdev->dev.platform_data; |
| 343 | #ifdef GENERIC_BITBANG |
| 344 | if (!pdata || !pdata->num_chipselect) |
| 345 | return -ENODEV; |
| 346 | #endif |
| 347 | |
| 348 | status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags); |
| 349 | if (status < 0) |
| 350 | return status; |
| 351 | |
| 352 | master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio); |
| 353 | if (!master) { |
| 354 | status = -ENOMEM; |
| 355 | goto gpio_free; |
| 356 | } |
| 357 | spi_gpio = spi_master_get_devdata(master); |
| 358 | platform_set_drvdata(pdev, spi_gpio); |
| 359 | |
| 360 | spi_gpio->pdev = pdev; |
| 361 | if (pdata) |
| 362 | spi_gpio->pdata = *pdata; |
| 363 | |
| 364 | master->flags = master_flags; |
| 365 | master->bus_num = pdev->id; |
| 366 | master->num_chipselect = SPI_N_CHIPSEL; |
| 367 | master->setup = spi_gpio_setup; |
| 368 | master->cleanup = spi_gpio_cleanup; |
| 369 | |
| 370 | spi_gpio->bitbang.master = spi_master_get(master); |
| 371 | spi_gpio->bitbang.chipselect = spi_gpio_chipselect; |
| 372 | |
| 373 | if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) { |
| 374 | spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; |
| 375 | spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; |
| 376 | spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; |
| 377 | spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; |
| 378 | } else { |
| 379 | spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; |
| 380 | spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; |
| 381 | spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; |
| 382 | spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; |
| 383 | } |
| 384 | spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; |
| 385 | spi_gpio->bitbang.flags = SPI_CS_HIGH; |
| 386 | |
| 387 | status = spi_bitbang_start(&spi_gpio->bitbang); |
| 388 | if (status < 0) { |
| 389 | spi_master_put(spi_gpio->bitbang.master); |
| 390 | gpio_free: |
| 391 | if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) |
| 392 | gpio_free(SPI_MISO_GPIO); |
| 393 | if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) |
| 394 | gpio_free(SPI_MOSI_GPIO); |
| 395 | gpio_free(SPI_SCK_GPIO); |
| 396 | spi_master_put(master); |
| 397 | } |
| 398 | |
| 399 | return status; |
| 400 | } |
| 401 | |
| 402 | static int __devexit spi_gpio_remove(struct platform_device *pdev) |
| 403 | { |
| 404 | struct spi_gpio *spi_gpio; |
| 405 | struct spi_gpio_platform_data *pdata; |
| 406 | int status; |
| 407 | |
| 408 | spi_gpio = platform_get_drvdata(pdev); |
| 409 | pdata = pdev->dev.platform_data; |
| 410 | |
| 411 | /* stop() unregisters child devices too */ |
| 412 | status = spi_bitbang_stop(&spi_gpio->bitbang); |
| 413 | spi_master_put(spi_gpio->bitbang.master); |
| 414 | |
| 415 | platform_set_drvdata(pdev, NULL); |
| 416 | |
| 417 | if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) |
| 418 | gpio_free(SPI_MISO_GPIO); |
| 419 | if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) |
| 420 | gpio_free(SPI_MOSI_GPIO); |
| 421 | gpio_free(SPI_SCK_GPIO); |
| 422 | |
| 423 | return status; |
| 424 | } |
| 425 | |
| 426 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 427 | |
| 428 | static struct platform_driver spi_gpio_driver = { |
| 429 | .driver.name = DRIVER_NAME, |
| 430 | .driver.owner = THIS_MODULE, |
| 431 | .probe = spi_gpio_probe, |
| 432 | .remove = __devexit_p(spi_gpio_remove), |
| 433 | }; |
| 434 | module_platform_driver(spi_gpio_driver); |
| 435 | |
| 436 | MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO "); |
| 437 | MODULE_AUTHOR("David Brownell"); |
| 438 | MODULE_LICENSE("GPL"); |