spi_gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  152. /* SPI is normally active-low */
  153. gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  154. }
  155. }
  156. static int spi_gpio_setup(struct spi_device *spi)
  157. {
  158. unsigned long cs = (unsigned long) spi->controller_data;
  159. int status = 0;
  160. if (spi->bits_per_word > 32)
  161. return -EINVAL;
  162. if (!spi->controller_state) {
  163. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  164. status = gpio_request(cs, dev_name(&spi->dev));
  165. if (status)
  166. return status;
  167. status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
  168. }
  169. }
  170. if (!status)
  171. status = spi_bitbang_setup(spi);
  172. if (status) {
  173. if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
  174. gpio_free(cs);
  175. }
  176. return status;
  177. }
  178. static void spi_gpio_cleanup(struct spi_device *spi)
  179. {
  180. unsigned long cs = (unsigned long) spi->controller_data;
  181. if (cs != SPI_GPIO_NO_CHIPSELECT)
  182. gpio_free(cs);
  183. spi_bitbang_cleanup(spi);
  184. }
  185. static int __init spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
  186. {
  187. int value;
  188. value = gpio_request(pin, label);
  189. if (value == 0) {
  190. if (is_in)
  191. value = gpio_direction_input(pin);
  192. else
  193. value = gpio_direction_output(pin, 0);
  194. }
  195. return value;
  196. }
  197. static int __init
  198. spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label)
  199. {
  200. int value;
  201. /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
  202. value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
  203. if (value)
  204. goto done;
  205. value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
  206. if (value)
  207. goto free_mosi;
  208. value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
  209. if (value)
  210. goto free_miso;
  211. goto done;
  212. free_miso:
  213. gpio_free(SPI_MISO_GPIO);
  214. free_mosi:
  215. gpio_free(SPI_MOSI_GPIO);
  216. done:
  217. return value;
  218. }
  219. static int __init spi_gpio_probe(struct platform_device *pdev)
  220. {
  221. int status;
  222. struct spi_master *master;
  223. struct spi_gpio *spi_gpio;
  224. struct spi_gpio_platform_data *pdata;
  225. pdata = pdev->dev.platform_data;
  226. #ifdef GENERIC_BITBANG
  227. if (!pdata || !pdata->num_chipselect)
  228. return -ENODEV;
  229. #endif
  230. status = spi_gpio_request(pdata, dev_name(&pdev->dev));
  231. if (status < 0)
  232. return status;
  233. master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
  234. if (!master) {
  235. status = -ENOMEM;
  236. goto gpio_free;
  237. }
  238. spi_gpio = spi_master_get_devdata(master);
  239. platform_set_drvdata(pdev, spi_gpio);
  240. spi_gpio->pdev = pdev;
  241. if (pdata)
  242. spi_gpio->pdata = *pdata;
  243. master->bus_num = pdev->id;
  244. master->num_chipselect = SPI_N_CHIPSEL;
  245. master->setup = spi_gpio_setup;
  246. master->cleanup = spi_gpio_cleanup;
  247. spi_gpio->bitbang.master = spi_master_get(master);
  248. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  249. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  250. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  251. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  252. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  253. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  254. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  255. status = spi_bitbang_start(&spi_gpio->bitbang);
  256. if (status < 0) {
  257. spi_master_put(spi_gpio->bitbang.master);
  258. gpio_free:
  259. gpio_free(SPI_MISO_GPIO);
  260. gpio_free(SPI_MOSI_GPIO);
  261. gpio_free(SPI_SCK_GPIO);
  262. spi_master_put(master);
  263. }
  264. return status;
  265. }
  266. static int __exit spi_gpio_remove(struct platform_device *pdev)
  267. {
  268. struct spi_gpio *spi_gpio;
  269. struct spi_gpio_platform_data *pdata;
  270. int status;
  271. spi_gpio = platform_get_drvdata(pdev);
  272. pdata = pdev->dev.platform_data;
  273. /* stop() unregisters child devices too */
  274. status = spi_bitbang_stop(&spi_gpio->bitbang);
  275. spi_master_put(spi_gpio->bitbang.master);
  276. platform_set_drvdata(pdev, NULL);
  277. gpio_free(SPI_MISO_GPIO);
  278. gpio_free(SPI_MOSI_GPIO);
  279. gpio_free(SPI_SCK_GPIO);
  280. return status;
  281. }
  282. MODULE_ALIAS("platform:" DRIVER_NAME);
  283. static struct platform_driver spi_gpio_driver = {
  284. .driver.name = DRIVER_NAME,
  285. .driver.owner = THIS_MODULE,
  286. .remove = __exit_p(spi_gpio_remove),
  287. };
  288. static int __init spi_gpio_init(void)
  289. {
  290. return platform_driver_probe(&spi_gpio_driver, spi_gpio_probe);
  291. }
  292. module_init(spi_gpio_init);
  293. static void __exit spi_gpio_exit(void)
  294. {
  295. platform_driver_unregister(&spi_gpio_driver);
  296. }
  297. module_exit(spi_gpio_exit);
  298. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  299. MODULE_AUTHOR("David Brownell");
  300. MODULE_LICENSE("GPL");