spi_gpio.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #define EXPAND_BITBANG_TXRX
  110. #include <linux/spi/spi_bitbang.h>
  111. /*
  112. * These functions can leverage inline expansion of GPIO calls to shrink
  113. * costs for a txrx bit, often by factors of around ten (by instruction
  114. * count). That is particularly visible for larger word sizes, but helps
  115. * even with default 8-bit words.
  116. *
  117. * REVISIT overheads calling these functions for each word also have
  118. * significant performance costs. Having txrx_bufs() calls that inline
  119. * the txrx_word() logic would help performance, e.g. on larger blocks
  120. * used with flash storage or MMC/SD. There should also be ways to make
  121. * GCC be less stupid about reloading registers inside the I/O loops,
  122. * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
  123. */
  124. static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
  125. unsigned nsecs, u32 word, u8 bits)
  126. {
  127. return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
  128. }
  129. static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
  130. unsigned nsecs, u32 word, u8 bits)
  131. {
  132. return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
  133. }
  134. static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
  135. unsigned nsecs, u32 word, u8 bits)
  136. {
  137. return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
  138. }
  139. static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
  140. unsigned nsecs, u32 word, u8 bits)
  141. {
  142. return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
  143. }
  144. /*----------------------------------------------------------------------*/
  145. static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
  146. {
  147. unsigned long cs = (unsigned long) spi->controller_data;
  148. /* set initial clock polarity */
  149. if (is_active)
  150. setsck(spi, spi->mode & SPI_CPOL);
  151. /* SPI is normally active-low */
  152. gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  153. }
  154. static int spi_gpio_setup(struct spi_device *spi)
  155. {
  156. unsigned long cs = (unsigned long) spi->controller_data;
  157. int status = 0;
  158. if (spi->bits_per_word > 32)
  159. return -EINVAL;
  160. if (!spi->controller_state) {
  161. status = gpio_request(cs, dev_name(&spi->dev));
  162. if (status)
  163. return status;
  164. status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
  165. }
  166. if (!status)
  167. status = spi_bitbang_setup(spi);
  168. if (status) {
  169. if (!spi->controller_state)
  170. gpio_free(cs);
  171. }
  172. return status;
  173. }
  174. static void spi_gpio_cleanup(struct spi_device *spi)
  175. {
  176. unsigned long cs = (unsigned long) spi->controller_data;
  177. gpio_free(cs);
  178. spi_bitbang_cleanup(spi);
  179. }
  180. static int __init spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
  181. {
  182. int value;
  183. value = gpio_request(pin, label);
  184. if (value == 0) {
  185. if (is_in)
  186. value = gpio_direction_input(pin);
  187. else
  188. value = gpio_direction_output(pin, 0);
  189. }
  190. return value;
  191. }
  192. static int __init
  193. spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label)
  194. {
  195. int value;
  196. /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
  197. value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
  198. if (value)
  199. goto done;
  200. value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
  201. if (value)
  202. goto free_mosi;
  203. value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
  204. if (value)
  205. goto free_miso;
  206. goto done;
  207. free_miso:
  208. gpio_free(SPI_MISO_GPIO);
  209. free_mosi:
  210. gpio_free(SPI_MOSI_GPIO);
  211. done:
  212. return value;
  213. }
  214. static int __init spi_gpio_probe(struct platform_device *pdev)
  215. {
  216. int status;
  217. struct spi_master *master;
  218. struct spi_gpio *spi_gpio;
  219. struct spi_gpio_platform_data *pdata;
  220. pdata = pdev->dev.platform_data;
  221. #ifdef GENERIC_BITBANG
  222. if (!pdata || !pdata->num_chipselect)
  223. return -ENODEV;
  224. #endif
  225. status = spi_gpio_request(pdata, dev_name(&pdev->dev));
  226. if (status < 0)
  227. return status;
  228. master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
  229. if (!master) {
  230. status = -ENOMEM;
  231. goto gpio_free;
  232. }
  233. spi_gpio = spi_master_get_devdata(master);
  234. platform_set_drvdata(pdev, spi_gpio);
  235. spi_gpio->pdev = pdev;
  236. if (pdata)
  237. spi_gpio->pdata = *pdata;
  238. master->bus_num = pdev->id;
  239. master->num_chipselect = SPI_N_CHIPSEL;
  240. master->setup = spi_gpio_setup;
  241. master->cleanup = spi_gpio_cleanup;
  242. spi_gpio->bitbang.master = spi_master_get(master);
  243. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  244. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  245. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  246. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  247. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  248. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  249. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  250. status = spi_bitbang_start(&spi_gpio->bitbang);
  251. if (status < 0) {
  252. spi_master_put(spi_gpio->bitbang.master);
  253. gpio_free:
  254. gpio_free(SPI_MISO_GPIO);
  255. gpio_free(SPI_MOSI_GPIO);
  256. gpio_free(SPI_SCK_GPIO);
  257. spi_master_put(master);
  258. }
  259. return status;
  260. }
  261. static int __exit spi_gpio_remove(struct platform_device *pdev)
  262. {
  263. struct spi_gpio *spi_gpio;
  264. struct spi_gpio_platform_data *pdata;
  265. int status;
  266. spi_gpio = platform_get_drvdata(pdev);
  267. pdata = pdev->dev.platform_data;
  268. /* stop() unregisters child devices too */
  269. status = spi_bitbang_stop(&spi_gpio->bitbang);
  270. spi_master_put(spi_gpio->bitbang.master);
  271. platform_set_drvdata(pdev, NULL);
  272. gpio_free(SPI_MISO_GPIO);
  273. gpio_free(SPI_MOSI_GPIO);
  274. gpio_free(SPI_SCK_GPIO);
  275. return status;
  276. }
  277. MODULE_ALIAS("platform:" DRIVER_NAME);
  278. static struct platform_driver spi_gpio_driver = {
  279. .driver.name = DRIVER_NAME,
  280. .driver.owner = THIS_MODULE,
  281. .remove = __exit_p(spi_gpio_remove),
  282. };
  283. static int __init spi_gpio_init(void)
  284. {
  285. return platform_driver_probe(&spi_gpio_driver, spi_gpio_probe);
  286. }
  287. module_init(spi_gpio_init);
  288. static void __exit spi_gpio_exit(void)
  289. {
  290. platform_driver_unregister(&spi_gpio_driver);
  291. }
  292. module_exit(spi_gpio_exit);
  293. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  294. MODULE_AUTHOR("David Brownell");
  295. MODULE_LICENSE("GPL");