spi-gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <linux/spi/spi.h>
  26. #include <linux/spi/spi_bitbang.h>
  27. #include <linux/spi/spi_gpio.h>
  28. /*
  29. * This bitbanging SPI master driver should help make systems usable
  30. * when a native hardware SPI engine is not available, perhaps because
  31. * its driver isn't yet working or because the I/O pins it requires
  32. * are used for other purposes.
  33. *
  34. * platform_device->driver_data ... points to spi_gpio
  35. *
  36. * spi->controller_state ... reserved for bitbang framework code
  37. * spi->controller_data ... holds chipselect GPIO
  38. *
  39. * spi->master->dev.driver_data ... points to spi_gpio->bitbang
  40. */
  41. struct spi_gpio {
  42. struct spi_bitbang bitbang;
  43. struct spi_gpio_platform_data pdata;
  44. struct platform_device *pdev;
  45. };
  46. /*----------------------------------------------------------------------*/
  47. /*
  48. * Because the overhead of going through four GPIO procedure calls
  49. * per transferred bit can make performance a problem, this code
  50. * is set up so that you can use it in either of two ways:
  51. *
  52. * - The slow generic way: set up platform_data to hold the GPIO
  53. * numbers used for MISO/MOSI/SCK, and issue procedure calls for
  54. * each of them. This driver can handle several such busses.
  55. *
  56. * - The quicker inlined way: only helps with platform GPIO code
  57. * that inlines operations for constant GPIOs. This can give
  58. * you tight (fast!) inner loops, but each such bus needs a
  59. * new driver. You'll define a new C file, with Makefile and
  60. * Kconfig support; the C code can be a total of six lines:
  61. *
  62. * #define DRIVER_NAME "myboard_spi2"
  63. * #define SPI_MISO_GPIO 119
  64. * #define SPI_MOSI_GPIO 120
  65. * #define SPI_SCK_GPIO 121
  66. * #define SPI_N_CHIPSEL 4
  67. * #include "spi-gpio.c"
  68. */
  69. #ifndef DRIVER_NAME
  70. #define DRIVER_NAME "spi_gpio"
  71. #define GENERIC_BITBANG /* vs tight inlines */
  72. /* all functions referencing these symbols must define pdata */
  73. #define SPI_MISO_GPIO ((pdata)->miso)
  74. #define SPI_MOSI_GPIO ((pdata)->mosi)
  75. #define SPI_SCK_GPIO ((pdata)->sck)
  76. #define SPI_N_CHIPSEL ((pdata)->num_chipselect)
  77. #endif
  78. /*----------------------------------------------------------------------*/
  79. static inline const struct spi_gpio_platform_data * __pure
  80. spi_to_pdata(const struct spi_device *spi)
  81. {
  82. const struct spi_bitbang *bang;
  83. const struct spi_gpio *spi_gpio;
  84. bang = spi_master_get_devdata(spi->master);
  85. spi_gpio = container_of(bang, struct spi_gpio, bitbang);
  86. return &spi_gpio->pdata;
  87. }
  88. /* this is #defined to avoid unused-variable warnings when inlining */
  89. #define pdata spi_to_pdata(spi)
  90. static inline void setsck(const struct spi_device *spi, int is_on)
  91. {
  92. gpio_set_value(SPI_SCK_GPIO, is_on);
  93. }
  94. static inline void setmosi(const struct spi_device *spi, int is_on)
  95. {
  96. gpio_set_value(SPI_MOSI_GPIO, is_on);
  97. }
  98. static inline int getmiso(const struct spi_device *spi)
  99. {
  100. return !!gpio_get_value(SPI_MISO_GPIO);
  101. }
  102. #undef pdata
  103. /*
  104. * NOTE: this clocks "as fast as we can". It "should" be a function of the
  105. * requested device clock. Software overhead means we usually have trouble
  106. * reaching even one Mbit/sec (except when we can inline bitops), so for now
  107. * we'll just assume we never need additional per-bit slowdowns.
  108. */
  109. #define spidelay(nsecs) do {} while (0)
  110. #include "spi-bitbang-txrx.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, 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, 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, 0, 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, 0, word, bits);
  143. }
  144. /*
  145. * These functions do not call setmosi or getmiso if respective flag
  146. * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
  147. * call when such pin is not present or defined in the controller.
  148. * A separate set of callbacks is defined to get highest possible
  149. * speed in the generic case (when both MISO and MOSI lines are
  150. * available), as optimiser will remove the checks when argument is
  151. * constant.
  152. */
  153. static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
  154. unsigned nsecs, u32 word, u8 bits)
  155. {
  156. unsigned flags = spi->master->flags;
  157. return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
  158. }
  159. static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
  160. unsigned nsecs, u32 word, u8 bits)
  161. {
  162. unsigned flags = spi->master->flags;
  163. return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
  164. }
  165. static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
  166. unsigned nsecs, u32 word, u8 bits)
  167. {
  168. unsigned flags = spi->master->flags;
  169. return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
  170. }
  171. static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
  172. unsigned nsecs, u32 word, u8 bits)
  173. {
  174. unsigned flags = spi->master->flags;
  175. return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
  176. }
  177. /*----------------------------------------------------------------------*/
  178. static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
  179. {
  180. unsigned long cs = (unsigned long) spi->controller_data;
  181. /* set initial clock polarity */
  182. if (is_active)
  183. setsck(spi, spi->mode & SPI_CPOL);
  184. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  185. /* SPI is normally active-low */
  186. gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  187. }
  188. }
  189. static int spi_gpio_setup(struct spi_device *spi)
  190. {
  191. unsigned long cs = (unsigned long) spi->controller_data;
  192. int status = 0;
  193. if (spi->bits_per_word > 32)
  194. return -EINVAL;
  195. if (!spi->controller_state) {
  196. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  197. status = gpio_request(cs, dev_name(&spi->dev));
  198. if (status)
  199. return status;
  200. status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
  201. }
  202. }
  203. if (!status)
  204. status = spi_bitbang_setup(spi);
  205. if (status) {
  206. if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
  207. gpio_free(cs);
  208. }
  209. return status;
  210. }
  211. static void spi_gpio_cleanup(struct spi_device *spi)
  212. {
  213. unsigned long cs = (unsigned long) spi->controller_data;
  214. if (cs != SPI_GPIO_NO_CHIPSELECT)
  215. gpio_free(cs);
  216. spi_bitbang_cleanup(spi);
  217. }
  218. static int __devinit spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
  219. {
  220. int value;
  221. value = gpio_request(pin, label);
  222. if (value == 0) {
  223. if (is_in)
  224. value = gpio_direction_input(pin);
  225. else
  226. value = gpio_direction_output(pin, 0);
  227. }
  228. return value;
  229. }
  230. static int __devinit
  231. spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label,
  232. u16 *res_flags)
  233. {
  234. int value;
  235. /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
  236. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) {
  237. value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
  238. if (value)
  239. goto done;
  240. } else {
  241. /* HW configuration without MOSI pin */
  242. *res_flags |= SPI_MASTER_NO_TX;
  243. }
  244. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) {
  245. value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
  246. if (value)
  247. goto free_mosi;
  248. } else {
  249. /* HW configuration without MISO pin */
  250. *res_flags |= SPI_MASTER_NO_RX;
  251. }
  252. value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
  253. if (value)
  254. goto free_miso;
  255. goto done;
  256. free_miso:
  257. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  258. gpio_free(SPI_MISO_GPIO);
  259. free_mosi:
  260. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  261. gpio_free(SPI_MOSI_GPIO);
  262. done:
  263. return value;
  264. }
  265. static int __devinit spi_gpio_probe(struct platform_device *pdev)
  266. {
  267. int status;
  268. struct spi_master *master;
  269. struct spi_gpio *spi_gpio;
  270. struct spi_gpio_platform_data *pdata;
  271. u16 master_flags = 0;
  272. pdata = pdev->dev.platform_data;
  273. #ifdef GENERIC_BITBANG
  274. if (!pdata || !pdata->num_chipselect)
  275. return -ENODEV;
  276. #endif
  277. status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags);
  278. if (status < 0)
  279. return status;
  280. master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
  281. if (!master) {
  282. status = -ENOMEM;
  283. goto gpio_free;
  284. }
  285. spi_gpio = spi_master_get_devdata(master);
  286. platform_set_drvdata(pdev, spi_gpio);
  287. spi_gpio->pdev = pdev;
  288. if (pdata)
  289. spi_gpio->pdata = *pdata;
  290. master->flags = master_flags;
  291. master->bus_num = pdev->id;
  292. master->num_chipselect = SPI_N_CHIPSEL;
  293. master->setup = spi_gpio_setup;
  294. master->cleanup = spi_gpio_cleanup;
  295. spi_gpio->bitbang.master = spi_master_get(master);
  296. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  297. if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
  298. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  299. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  300. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  301. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  302. } else {
  303. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
  304. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
  305. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
  306. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
  307. }
  308. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  309. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  310. status = spi_bitbang_start(&spi_gpio->bitbang);
  311. if (status < 0) {
  312. spi_master_put(spi_gpio->bitbang.master);
  313. gpio_free:
  314. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  315. gpio_free(SPI_MISO_GPIO);
  316. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  317. gpio_free(SPI_MOSI_GPIO);
  318. gpio_free(SPI_SCK_GPIO);
  319. spi_master_put(master);
  320. }
  321. return status;
  322. }
  323. static int __devexit spi_gpio_remove(struct platform_device *pdev)
  324. {
  325. struct spi_gpio *spi_gpio;
  326. struct spi_gpio_platform_data *pdata;
  327. int status;
  328. spi_gpio = platform_get_drvdata(pdev);
  329. pdata = pdev->dev.platform_data;
  330. /* stop() unregisters child devices too */
  331. status = spi_bitbang_stop(&spi_gpio->bitbang);
  332. spi_master_put(spi_gpio->bitbang.master);
  333. platform_set_drvdata(pdev, NULL);
  334. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  335. gpio_free(SPI_MISO_GPIO);
  336. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  337. gpio_free(SPI_MOSI_GPIO);
  338. gpio_free(SPI_SCK_GPIO);
  339. return status;
  340. }
  341. MODULE_ALIAS("platform:" DRIVER_NAME);
  342. static struct platform_driver spi_gpio_driver = {
  343. .driver.name = DRIVER_NAME,
  344. .driver.owner = THIS_MODULE,
  345. .probe = spi_gpio_probe,
  346. .remove = __devexit_p(spi_gpio_remove),
  347. };
  348. module_platform_driver(spi_gpio_driver);
  349. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  350. MODULE_AUTHOR("David Brownell");
  351. MODULE_LICENSE("GPL");