spi-gpio.c 12 KB

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