spi_gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * spi_gpio.c - 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/init.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/gpio.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/spi_bitbang.h>
  26. #include <linux/spi/spi_gpio.h>
  27. /*
  28. * This bitbanging SPI master driver should help make systems usable
  29. * when a native hardware SPI engine is not available, perhaps because
  30. * its driver isn't yet working or because the I/O pins it requires
  31. * are used for other purposes.
  32. *
  33. * platform_device->driver_data ... points to spi_gpio
  34. *
  35. * spi->controller_state ... reserved for bitbang framework code
  36. * spi->controller_data ... holds chipselect GPIO
  37. *
  38. * spi->master->dev.driver_data ... points to spi_gpio->bitbang
  39. */
  40. struct spi_gpio {
  41. struct spi_bitbang bitbang;
  42. struct spi_gpio_platform_data pdata;
  43. struct platform_device *pdev;
  44. };
  45. /*----------------------------------------------------------------------*/
  46. /*
  47. * Because the overhead of going through four GPIO procedure calls
  48. * per transferred bit can make performance a problem, this code
  49. * is set up so that you can use it in either of two ways:
  50. *
  51. * - The slow generic way: set up platform_data to hold the GPIO
  52. * numbers used for MISO/MOSI/SCK, and issue procedure calls for
  53. * each of them. This driver can handle several such busses.
  54. *
  55. * - The quicker inlined way: only helps with platform GPIO code
  56. * that inlines operations for constant GPIOs. This can give
  57. * you tight (fast!) inner loops, but each such bus needs a
  58. * new driver. You'll define a new C file, with Makefile and
  59. * Kconfig support; the C code can be a total of six lines:
  60. *
  61. * #define DRIVER_NAME "myboard_spi2"
  62. * #define SPI_MISO_GPIO 119
  63. * #define SPI_MOSI_GPIO 120
  64. * #define SPI_SCK_GPIO 121
  65. * #define SPI_N_CHIPSEL 4
  66. * #include "spi_gpio.c"
  67. */
  68. #ifndef DRIVER_NAME
  69. #define DRIVER_NAME "spi_gpio"
  70. #define GENERIC_BITBANG /* vs tight inlines */
  71. /* all functions referencing these symbols must define pdata */
  72. #define SPI_MISO_GPIO ((pdata)->miso)
  73. #define SPI_MOSI_GPIO ((pdata)->mosi)
  74. #define SPI_SCK_GPIO ((pdata)->sck)
  75. #define SPI_N_CHIPSEL ((pdata)->num_chipselect)
  76. #endif
  77. /*----------------------------------------------------------------------*/
  78. static inline const struct spi_gpio_platform_data * __pure
  79. spi_to_pdata(const struct spi_device *spi)
  80. {
  81. const struct spi_bitbang *bang;
  82. const struct spi_gpio *spi_gpio;
  83. bang = spi_master_get_devdata(spi->master);
  84. spi_gpio = container_of(bang, struct spi_gpio, bitbang);
  85. return &spi_gpio->pdata;
  86. }
  87. /* this is #defined to avoid unused-variable warnings when inlining */
  88. #define pdata spi_to_pdata(spi)
  89. static inline void setsck(const struct spi_device *spi, int is_on)
  90. {
  91. gpio_set_value(SPI_SCK_GPIO, is_on);
  92. }
  93. static inline void setmosi(const struct spi_device *spi, int is_on)
  94. {
  95. gpio_set_value(SPI_MOSI_GPIO, is_on);
  96. }
  97. static inline int getmiso(const struct spi_device *spi)
  98. {
  99. return !!gpio_get_value(SPI_MISO_GPIO);
  100. }
  101. #undef pdata
  102. /*
  103. * NOTE: this clocks "as fast as we can". It "should" be a function of the
  104. * requested device clock. Software overhead means we usually have trouble
  105. * reaching even one Mbit/sec (except when we can inline bitops), so for now
  106. * we'll just assume we never need additional per-bit slowdowns.
  107. */
  108. #define spidelay(nsecs) do {} while (0)
  109. #include "spi_bitbang_txrx.h"
  110. /*
  111. * These functions can leverage inline expansion of GPIO calls to shrink
  112. * costs for a txrx bit, often by factors of around ten (by instruction
  113. * count). That is particularly visible for larger word sizes, but helps
  114. * even with default 8-bit words.
  115. *
  116. * REVISIT overheads calling these functions for each word also have
  117. * significant performance costs. Having txrx_bufs() calls that inline
  118. * the txrx_word() logic would help performance, e.g. on larger blocks
  119. * used with flash storage or MMC/SD. There should also be ways to make
  120. * GCC be less stupid about reloading registers inside the I/O loops,
  121. * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
  122. */
  123. static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
  124. unsigned nsecs, u32 word, u8 bits)
  125. {
  126. return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
  127. }
  128. static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
  129. unsigned nsecs, u32 word, u8 bits)
  130. {
  131. return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
  132. }
  133. static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
  134. unsigned nsecs, u32 word, u8 bits)
  135. {
  136. return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
  137. }
  138. static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
  139. unsigned nsecs, u32 word, u8 bits)
  140. {
  141. return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
  142. }
  143. /*----------------------------------------------------------------------*/
  144. static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
  145. {
  146. unsigned long cs = (unsigned long) spi->controller_data;
  147. /* set initial clock polarity */
  148. if (is_active)
  149. setsck(spi, spi->mode & SPI_CPOL);
  150. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  151. /* SPI is normally active-low */
  152. gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  153. }
  154. }
  155. static int spi_gpio_setup(struct spi_device *spi)
  156. {
  157. unsigned long cs = (unsigned long) spi->controller_data;
  158. int status = 0;
  159. if (spi->bits_per_word > 32)
  160. return -EINVAL;
  161. if (!spi->controller_state) {
  162. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  163. status = gpio_request(cs, dev_name(&spi->dev));
  164. if (status)
  165. return status;
  166. status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
  167. }
  168. }
  169. if (!status)
  170. status = spi_bitbang_setup(spi);
  171. if (status) {
  172. if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
  173. gpio_free(cs);
  174. }
  175. return status;
  176. }
  177. static void spi_gpio_cleanup(struct spi_device *spi)
  178. {
  179. unsigned long cs = (unsigned long) spi->controller_data;
  180. if (cs != SPI_GPIO_NO_CHIPSELECT)
  181. gpio_free(cs);
  182. spi_bitbang_cleanup(spi);
  183. }
  184. static int __init spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
  185. {
  186. int value;
  187. value = gpio_request(pin, label);
  188. if (value == 0) {
  189. if (is_in)
  190. value = gpio_direction_input(pin);
  191. else
  192. value = gpio_direction_output(pin, 0);
  193. }
  194. return value;
  195. }
  196. static int __init
  197. spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label)
  198. {
  199. int value;
  200. /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
  201. value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
  202. if (value)
  203. goto done;
  204. value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
  205. if (value)
  206. goto free_mosi;
  207. value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
  208. if (value)
  209. goto free_miso;
  210. goto done;
  211. free_miso:
  212. gpio_free(SPI_MISO_GPIO);
  213. free_mosi:
  214. gpio_free(SPI_MOSI_GPIO);
  215. done:
  216. return value;
  217. }
  218. static int __init spi_gpio_probe(struct platform_device *pdev)
  219. {
  220. int status;
  221. struct spi_master *master;
  222. struct spi_gpio *spi_gpio;
  223. struct spi_gpio_platform_data *pdata;
  224. pdata = pdev->dev.platform_data;
  225. #ifdef GENERIC_BITBANG
  226. if (!pdata || !pdata->num_chipselect)
  227. return -ENODEV;
  228. #endif
  229. status = spi_gpio_request(pdata, dev_name(&pdev->dev));
  230. if (status < 0)
  231. return status;
  232. master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
  233. if (!master) {
  234. status = -ENOMEM;
  235. goto gpio_free;
  236. }
  237. spi_gpio = spi_master_get_devdata(master);
  238. platform_set_drvdata(pdev, spi_gpio);
  239. spi_gpio->pdev = pdev;
  240. if (pdata)
  241. spi_gpio->pdata = *pdata;
  242. master->bus_num = pdev->id;
  243. master->num_chipselect = SPI_N_CHIPSEL;
  244. master->setup = spi_gpio_setup;
  245. master->cleanup = spi_gpio_cleanup;
  246. spi_gpio->bitbang.master = spi_master_get(master);
  247. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  248. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  249. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  250. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  251. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  252. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  253. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  254. status = spi_bitbang_start(&spi_gpio->bitbang);
  255. if (status < 0) {
  256. spi_master_put(spi_gpio->bitbang.master);
  257. gpio_free:
  258. gpio_free(SPI_MISO_GPIO);
  259. gpio_free(SPI_MOSI_GPIO);
  260. gpio_free(SPI_SCK_GPIO);
  261. spi_master_put(master);
  262. }
  263. return status;
  264. }
  265. static int __exit spi_gpio_remove(struct platform_device *pdev)
  266. {
  267. struct spi_gpio *spi_gpio;
  268. struct spi_gpio_platform_data *pdata;
  269. int status;
  270. spi_gpio = platform_get_drvdata(pdev);
  271. pdata = pdev->dev.platform_data;
  272. /* stop() unregisters child devices too */
  273. status = spi_bitbang_stop(&spi_gpio->bitbang);
  274. spi_master_put(spi_gpio->bitbang.master);
  275. platform_set_drvdata(pdev, NULL);
  276. gpio_free(SPI_MISO_GPIO);
  277. gpio_free(SPI_MOSI_GPIO);
  278. gpio_free(SPI_SCK_GPIO);
  279. return status;
  280. }
  281. MODULE_ALIAS("platform:" DRIVER_NAME);
  282. static struct platform_driver spi_gpio_driver = {
  283. .driver.name = DRIVER_NAME,
  284. .driver.owner = THIS_MODULE,
  285. .remove = __exit_p(spi_gpio_remove),
  286. };
  287. static int __init spi_gpio_init(void)
  288. {
  289. return platform_driver_probe(&spi_gpio_driver, spi_gpio_probe);
  290. }
  291. module_init(spi_gpio_init);
  292. static void __exit spi_gpio_exit(void)
  293. {
  294. platform_driver_unregister(&spi_gpio_driver);
  295. }
  296. module_exit(spi_gpio_exit);
  297. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  298. MODULE_AUTHOR("David Brownell");
  299. MODULE_LICENSE("GPL");