spi_gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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, 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, 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, 0, 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, 0, word, bits);
  142. }
  143. /*
  144. * These functions do not call setmosi or getmiso if respective flag
  145. * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
  146. * call when such pin is not present or defined in the controller.
  147. * A separate set of callbacks is defined to get highest possible
  148. * speed in the generic case (when both MISO and MOSI lines are
  149. * available), as optimiser will remove the checks when argument is
  150. * constant.
  151. */
  152. static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
  153. unsigned nsecs, u32 word, u8 bits)
  154. {
  155. unsigned flags = spi->master->flags;
  156. return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
  157. }
  158. static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
  159. unsigned nsecs, u32 word, u8 bits)
  160. {
  161. unsigned flags = spi->master->flags;
  162. return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
  163. }
  164. static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
  165. unsigned nsecs, u32 word, u8 bits)
  166. {
  167. unsigned flags = spi->master->flags;
  168. return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
  169. }
  170. static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
  171. unsigned nsecs, u32 word, u8 bits)
  172. {
  173. unsigned flags = spi->master->flags;
  174. return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
  175. }
  176. /*----------------------------------------------------------------------*/
  177. static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
  178. {
  179. unsigned long cs = (unsigned long) spi->controller_data;
  180. /* set initial clock polarity */
  181. if (is_active)
  182. setsck(spi, spi->mode & SPI_CPOL);
  183. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  184. /* SPI is normally active-low */
  185. gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  186. }
  187. }
  188. static int spi_gpio_setup(struct spi_device *spi)
  189. {
  190. unsigned long cs = (unsigned long) spi->controller_data;
  191. int status = 0;
  192. if (spi->bits_per_word > 32)
  193. return -EINVAL;
  194. if (!spi->controller_state) {
  195. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  196. status = gpio_request(cs, dev_name(&spi->dev));
  197. if (status)
  198. return status;
  199. status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
  200. }
  201. }
  202. if (!status)
  203. status = spi_bitbang_setup(spi);
  204. if (status) {
  205. if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
  206. gpio_free(cs);
  207. }
  208. return status;
  209. }
  210. static void spi_gpio_cleanup(struct spi_device *spi)
  211. {
  212. unsigned long cs = (unsigned long) spi->controller_data;
  213. if (cs != SPI_GPIO_NO_CHIPSELECT)
  214. gpio_free(cs);
  215. spi_bitbang_cleanup(spi);
  216. }
  217. static int __init spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
  218. {
  219. int value;
  220. value = gpio_request(pin, label);
  221. if (value == 0) {
  222. if (is_in)
  223. value = gpio_direction_input(pin);
  224. else
  225. value = gpio_direction_output(pin, 0);
  226. }
  227. return value;
  228. }
  229. static int __init
  230. spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label,
  231. u16 *res_flags)
  232. {
  233. int value;
  234. /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
  235. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) {
  236. value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
  237. if (value)
  238. goto done;
  239. } else {
  240. /* HW configuration without MOSI pin */
  241. *res_flags |= SPI_MASTER_NO_TX;
  242. }
  243. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) {
  244. value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
  245. if (value)
  246. goto free_mosi;
  247. } else {
  248. /* HW configuration without MISO pin */
  249. *res_flags |= SPI_MASTER_NO_RX;
  250. }
  251. value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
  252. if (value)
  253. goto free_miso;
  254. goto done;
  255. free_miso:
  256. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  257. gpio_free(SPI_MISO_GPIO);
  258. free_mosi:
  259. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  260. gpio_free(SPI_MOSI_GPIO);
  261. done:
  262. return value;
  263. }
  264. static int __init spi_gpio_probe(struct platform_device *pdev)
  265. {
  266. int status;
  267. struct spi_master *master;
  268. struct spi_gpio *spi_gpio;
  269. struct spi_gpio_platform_data *pdata;
  270. u16 master_flags = 0;
  271. pdata = pdev->dev.platform_data;
  272. #ifdef GENERIC_BITBANG
  273. if (!pdata || !pdata->num_chipselect)
  274. return -ENODEV;
  275. #endif
  276. status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags);
  277. if (status < 0)
  278. return status;
  279. master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
  280. if (!master) {
  281. status = -ENOMEM;
  282. goto gpio_free;
  283. }
  284. spi_gpio = spi_master_get_devdata(master);
  285. platform_set_drvdata(pdev, spi_gpio);
  286. spi_gpio->pdev = pdev;
  287. if (pdata)
  288. spi_gpio->pdata = *pdata;
  289. master->flags = master_flags;
  290. master->bus_num = pdev->id;
  291. master->num_chipselect = SPI_N_CHIPSEL;
  292. master->setup = spi_gpio_setup;
  293. master->cleanup = spi_gpio_cleanup;
  294. spi_gpio->bitbang.master = spi_master_get(master);
  295. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  296. if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
  297. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  298. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  299. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  300. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  301. } else {
  302. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
  303. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
  304. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
  305. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
  306. }
  307. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  308. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  309. status = spi_bitbang_start(&spi_gpio->bitbang);
  310. if (status < 0) {
  311. spi_master_put(spi_gpio->bitbang.master);
  312. gpio_free:
  313. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  314. gpio_free(SPI_MISO_GPIO);
  315. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  316. gpio_free(SPI_MOSI_GPIO);
  317. gpio_free(SPI_SCK_GPIO);
  318. spi_master_put(master);
  319. }
  320. return status;
  321. }
  322. static int __exit spi_gpio_remove(struct platform_device *pdev)
  323. {
  324. struct spi_gpio *spi_gpio;
  325. struct spi_gpio_platform_data *pdata;
  326. int status;
  327. spi_gpio = platform_get_drvdata(pdev);
  328. pdata = pdev->dev.platform_data;
  329. /* stop() unregisters child devices too */
  330. status = spi_bitbang_stop(&spi_gpio->bitbang);
  331. spi_master_put(spi_gpio->bitbang.master);
  332. platform_set_drvdata(pdev, NULL);
  333. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  334. gpio_free(SPI_MISO_GPIO);
  335. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  336. gpio_free(SPI_MOSI_GPIO);
  337. gpio_free(SPI_SCK_GPIO);
  338. return status;
  339. }
  340. MODULE_ALIAS("platform:" DRIVER_NAME);
  341. static struct platform_driver spi_gpio_driver = {
  342. .driver.name = DRIVER_NAME,
  343. .driver.owner = THIS_MODULE,
  344. .remove = __exit_p(spi_gpio_remove),
  345. };
  346. static int __init spi_gpio_init(void)
  347. {
  348. return platform_driver_probe(&spi_gpio_driver, spi_gpio_probe);
  349. }
  350. module_init(spi_gpio_init);
  351. static void __exit spi_gpio_exit(void)
  352. {
  353. platform_driver_unregister(&spi_gpio_driver);
  354. }
  355. module_exit(spi_gpio_exit);
  356. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  357. MODULE_AUTHOR("David Brownell");
  358. MODULE_LICENSE("GPL");