spi-bitbang.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * polling/bitbanging SPI master controller driver utilities
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/spinlock.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/errno.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/spi/spi_bitbang.h>
  24. #define SPI_BITBANG_CS_DELAY 100
  25. /*----------------------------------------------------------------------*/
  26. /*
  27. * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
  28. * Use this for GPIO or shift-register level hardware APIs.
  29. *
  30. * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
  31. * to glue code. These bitbang setup() and cleanup() routines are always
  32. * used, though maybe they're called from controller-aware code.
  33. *
  34. * chipselect() and friends may use spi_device->controller_data and
  35. * controller registers as appropriate.
  36. *
  37. *
  38. * NOTE: SPI controller pins can often be used as GPIO pins instead,
  39. * which means you could use a bitbang driver either to get hardware
  40. * working quickly, or testing for differences that aren't speed related.
  41. */
  42. struct spi_bitbang_cs {
  43. unsigned nsecs; /* (clock cycle time)/2 */
  44. u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
  45. u32 word, u8 bits);
  46. unsigned (*txrx_bufs)(struct spi_device *,
  47. u32 (*txrx_word)(
  48. struct spi_device *spi,
  49. unsigned nsecs,
  50. u32 word, u8 bits),
  51. unsigned, struct spi_transfer *);
  52. };
  53. static unsigned bitbang_txrx_8(
  54. struct spi_device *spi,
  55. u32 (*txrx_word)(struct spi_device *spi,
  56. unsigned nsecs,
  57. u32 word, u8 bits),
  58. unsigned ns,
  59. struct spi_transfer *t
  60. ) {
  61. unsigned bits = t->bits_per_word;
  62. unsigned count = t->len;
  63. const u8 *tx = t->tx_buf;
  64. u8 *rx = t->rx_buf;
  65. while (likely(count > 0)) {
  66. u8 word = 0;
  67. if (tx)
  68. word = *tx++;
  69. word = txrx_word(spi, ns, word, bits);
  70. if (rx)
  71. *rx++ = word;
  72. count -= 1;
  73. }
  74. return t->len - count;
  75. }
  76. static unsigned bitbang_txrx_16(
  77. struct spi_device *spi,
  78. u32 (*txrx_word)(struct spi_device *spi,
  79. unsigned nsecs,
  80. u32 word, u8 bits),
  81. unsigned ns,
  82. struct spi_transfer *t
  83. ) {
  84. unsigned bits = t->bits_per_word;
  85. unsigned count = t->len;
  86. const u16 *tx = t->tx_buf;
  87. u16 *rx = t->rx_buf;
  88. while (likely(count > 1)) {
  89. u16 word = 0;
  90. if (tx)
  91. word = *tx++;
  92. word = txrx_word(spi, ns, word, bits);
  93. if (rx)
  94. *rx++ = word;
  95. count -= 2;
  96. }
  97. return t->len - count;
  98. }
  99. static unsigned bitbang_txrx_32(
  100. struct spi_device *spi,
  101. u32 (*txrx_word)(struct spi_device *spi,
  102. unsigned nsecs,
  103. u32 word, u8 bits),
  104. unsigned ns,
  105. struct spi_transfer *t
  106. ) {
  107. unsigned bits = t->bits_per_word;
  108. unsigned count = t->len;
  109. const u32 *tx = t->tx_buf;
  110. u32 *rx = t->rx_buf;
  111. while (likely(count > 3)) {
  112. u32 word = 0;
  113. if (tx)
  114. word = *tx++;
  115. word = txrx_word(spi, ns, word, bits);
  116. if (rx)
  117. *rx++ = word;
  118. count -= 4;
  119. }
  120. return t->len - count;
  121. }
  122. int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  123. {
  124. struct spi_bitbang_cs *cs = spi->controller_state;
  125. u8 bits_per_word;
  126. u32 hz;
  127. if (t) {
  128. bits_per_word = t->bits_per_word;
  129. hz = t->speed_hz;
  130. } else {
  131. bits_per_word = 0;
  132. hz = 0;
  133. }
  134. /* spi_transfer level calls that work per-word */
  135. if (!bits_per_word)
  136. bits_per_word = spi->bits_per_word;
  137. if (bits_per_word <= 8)
  138. cs->txrx_bufs = bitbang_txrx_8;
  139. else if (bits_per_word <= 16)
  140. cs->txrx_bufs = bitbang_txrx_16;
  141. else if (bits_per_word <= 32)
  142. cs->txrx_bufs = bitbang_txrx_32;
  143. else
  144. return -EINVAL;
  145. /* nsecs = (clock period)/2 */
  146. if (!hz)
  147. hz = spi->max_speed_hz;
  148. if (hz) {
  149. cs->nsecs = (1000000000/2) / hz;
  150. if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
  151. return -EINVAL;
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
  156. /**
  157. * spi_bitbang_setup - default setup for per-word I/O loops
  158. */
  159. int spi_bitbang_setup(struct spi_device *spi)
  160. {
  161. struct spi_bitbang_cs *cs = spi->controller_state;
  162. struct spi_bitbang *bitbang;
  163. bitbang = spi_master_get_devdata(spi->master);
  164. if (!cs) {
  165. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  166. if (!cs)
  167. return -ENOMEM;
  168. spi->controller_state = cs;
  169. }
  170. /* per-word shift register access, in hardware or bitbanging */
  171. cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
  172. if (!cs->txrx_word)
  173. return -EINVAL;
  174. if (bitbang->setup_transfer) {
  175. int retval = bitbang->setup_transfer(spi, NULL);
  176. if (retval < 0)
  177. return retval;
  178. }
  179. dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
  180. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  181. * setup, unless the hardware defaults cooperate to avoid confusion
  182. * between normal (active low) and inverted chipselects.
  183. */
  184. /* deselect chip (low or high) */
  185. mutex_lock(&bitbang->lock);
  186. if (!bitbang->busy) {
  187. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  188. ndelay(cs->nsecs);
  189. }
  190. mutex_unlock(&bitbang->lock);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(spi_bitbang_setup);
  194. /**
  195. * spi_bitbang_cleanup - default cleanup for per-word I/O loops
  196. */
  197. void spi_bitbang_cleanup(struct spi_device *spi)
  198. {
  199. kfree(spi->controller_state);
  200. }
  201. EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
  202. static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
  203. {
  204. struct spi_bitbang_cs *cs = spi->controller_state;
  205. unsigned nsecs = cs->nsecs;
  206. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
  207. }
  208. /*----------------------------------------------------------------------*/
  209. /*
  210. * SECOND PART ... simple transfer queue runner.
  211. *
  212. * This costs a task context per controller, running the queue by
  213. * performing each transfer in sequence. Smarter hardware can queue
  214. * several DMA transfers at once, and process several controller queues
  215. * in parallel; this driver doesn't match such hardware very well.
  216. *
  217. * Drivers can provide word-at-a-time i/o primitives, or provide
  218. * transfer-at-a-time ones to leverage dma or fifo hardware.
  219. */
  220. static int spi_bitbang_prepare_hardware(struct spi_master *spi)
  221. {
  222. struct spi_bitbang *bitbang;
  223. bitbang = spi_master_get_devdata(spi);
  224. mutex_lock(&bitbang->lock);
  225. bitbang->busy = 1;
  226. mutex_unlock(&bitbang->lock);
  227. return 0;
  228. }
  229. static int spi_bitbang_transfer_one(struct spi_master *master,
  230. struct spi_device *spi,
  231. struct spi_transfer *transfer)
  232. {
  233. struct spi_bitbang *bitbang = spi_master_get_devdata(master);
  234. int status = 0;
  235. if (bitbang->setup_transfer) {
  236. status = bitbang->setup_transfer(spi, transfer);
  237. if (status < 0)
  238. goto out;
  239. }
  240. if (transfer->len)
  241. status = bitbang->txrx_bufs(spi, transfer);
  242. if (status == transfer->len)
  243. status = 0;
  244. else if (status >= 0)
  245. status = -EREMOTEIO;
  246. out:
  247. spi_finalize_current_transfer(master);
  248. return status;
  249. }
  250. static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
  251. {
  252. struct spi_bitbang *bitbang;
  253. bitbang = spi_master_get_devdata(spi);
  254. mutex_lock(&bitbang->lock);
  255. bitbang->busy = 0;
  256. mutex_unlock(&bitbang->lock);
  257. return 0;
  258. }
  259. static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
  260. {
  261. struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
  262. /* SPI core provides CS high / low, but bitbang driver
  263. * expects CS active
  264. * spi device driver takes care of handling SPI_CS_HIGH
  265. */
  266. enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
  267. ndelay(SPI_BITBANG_CS_DELAY);
  268. bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
  269. BITBANG_CS_INACTIVE);
  270. ndelay(SPI_BITBANG_CS_DELAY);
  271. }
  272. /*----------------------------------------------------------------------*/
  273. /**
  274. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  275. * @bitbang: driver handle
  276. *
  277. * Caller should have zero-initialized all parts of the structure, and then
  278. * provided callbacks for chip selection and I/O loops. If the master has
  279. * a transfer method, its final step should call spi_bitbang_transfer; or,
  280. * that's the default if the transfer routine is not initialized. It should
  281. * also set up the bus number and number of chipselects.
  282. *
  283. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  284. * hardware that basically exposes a shift register) or per-spi_transfer
  285. * (which takes better advantage of hardware like fifos or DMA engines).
  286. *
  287. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
  288. * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
  289. * master methods. Those methods are the defaults if the bitbang->txrx_bufs
  290. * routine isn't initialized.
  291. *
  292. * This routine registers the spi_master, which will process requests in a
  293. * dedicated task, keeping IRQs unblocked most of the time. To stop
  294. * processing those requests, call spi_bitbang_stop().
  295. *
  296. * On success, this routine will take a reference to master. The caller is
  297. * responsible for calling spi_bitbang_stop() to decrement the reference and
  298. * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
  299. * leak.
  300. */
  301. int spi_bitbang_start(struct spi_bitbang *bitbang)
  302. {
  303. struct spi_master *master = bitbang->master;
  304. int ret;
  305. if (!master || !bitbang->chipselect)
  306. return -EINVAL;
  307. mutex_init(&bitbang->lock);
  308. if (!master->mode_bits)
  309. master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  310. if (master->transfer || master->transfer_one_message)
  311. return -EINVAL;
  312. master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
  313. master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
  314. master->transfer_one = spi_bitbang_transfer_one;
  315. master->set_cs = spi_bitbang_set_cs;
  316. if (!bitbang->txrx_bufs) {
  317. bitbang->use_dma = 0;
  318. bitbang->txrx_bufs = spi_bitbang_bufs;
  319. if (!master->setup) {
  320. if (!bitbang->setup_transfer)
  321. bitbang->setup_transfer =
  322. spi_bitbang_setup_transfer;
  323. master->setup = spi_bitbang_setup;
  324. master->cleanup = spi_bitbang_cleanup;
  325. }
  326. }
  327. /* driver may get busy before register() returns, especially
  328. * if someone registered boardinfo for devices
  329. */
  330. ret = spi_register_master(spi_master_get(master));
  331. if (ret)
  332. spi_master_put(master);
  333. return 0;
  334. }
  335. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  336. /**
  337. * spi_bitbang_stop - stops the task providing spi communication
  338. */
  339. void spi_bitbang_stop(struct spi_bitbang *bitbang)
  340. {
  341. spi_unregister_master(bitbang->master);
  342. }
  343. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  344. MODULE_LICENSE("GPL");