spi-bitbang.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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, unsigned flags);
  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 flags),
  52. unsigned, struct spi_transfer *,
  53. unsigned);
  54. };
  55. static unsigned bitbang_txrx_8(
  56. struct spi_device *spi,
  57. u32 (*txrx_word)(struct spi_device *spi,
  58. unsigned nsecs,
  59. u32 word, u8 bits,
  60. unsigned flags),
  61. unsigned ns,
  62. struct spi_transfer *t,
  63. unsigned flags
  64. ) {
  65. unsigned bits = t->bits_per_word;
  66. unsigned count = t->len;
  67. const u8 *tx = t->tx_buf;
  68. u8 *rx = t->rx_buf;
  69. while (likely(count > 0)) {
  70. u8 word = 0;
  71. if (tx)
  72. word = *tx++;
  73. word = txrx_word(spi, ns, word, bits, flags);
  74. if (rx)
  75. *rx++ = word;
  76. count -= 1;
  77. }
  78. return t->len - count;
  79. }
  80. static unsigned bitbang_txrx_16(
  81. struct spi_device *spi,
  82. u32 (*txrx_word)(struct spi_device *spi,
  83. unsigned nsecs,
  84. u32 word, u8 bits,
  85. unsigned flags),
  86. unsigned ns,
  87. struct spi_transfer *t,
  88. unsigned flags
  89. ) {
  90. unsigned bits = t->bits_per_word;
  91. unsigned count = t->len;
  92. const u16 *tx = t->tx_buf;
  93. u16 *rx = t->rx_buf;
  94. while (likely(count > 1)) {
  95. u16 word = 0;
  96. if (tx)
  97. word = *tx++;
  98. word = txrx_word(spi, ns, word, bits, flags);
  99. if (rx)
  100. *rx++ = word;
  101. count -= 2;
  102. }
  103. return t->len - count;
  104. }
  105. static unsigned bitbang_txrx_32(
  106. struct spi_device *spi,
  107. u32 (*txrx_word)(struct spi_device *spi,
  108. unsigned nsecs,
  109. u32 word, u8 bits,
  110. unsigned flags),
  111. unsigned ns,
  112. struct spi_transfer *t,
  113. unsigned flags
  114. ) {
  115. unsigned bits = t->bits_per_word;
  116. unsigned count = t->len;
  117. const u32 *tx = t->tx_buf;
  118. u32 *rx = t->rx_buf;
  119. while (likely(count > 3)) {
  120. u32 word = 0;
  121. if (tx)
  122. word = *tx++;
  123. word = txrx_word(spi, ns, word, bits, flags);
  124. if (rx)
  125. *rx++ = word;
  126. count -= 4;
  127. }
  128. return t->len - count;
  129. }
  130. int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  131. {
  132. struct spi_bitbang_cs *cs = spi->controller_state;
  133. u8 bits_per_word;
  134. u32 hz;
  135. if (t) {
  136. bits_per_word = t->bits_per_word;
  137. hz = t->speed_hz;
  138. } else {
  139. bits_per_word = 0;
  140. hz = 0;
  141. }
  142. /* spi_transfer level calls that work per-word */
  143. if (!bits_per_word)
  144. bits_per_word = spi->bits_per_word;
  145. if (bits_per_word <= 8)
  146. cs->txrx_bufs = bitbang_txrx_8;
  147. else if (bits_per_word <= 16)
  148. cs->txrx_bufs = bitbang_txrx_16;
  149. else if (bits_per_word <= 32)
  150. cs->txrx_bufs = bitbang_txrx_32;
  151. else
  152. return -EINVAL;
  153. /* nsecs = (clock period)/2 */
  154. if (!hz)
  155. hz = spi->max_speed_hz;
  156. if (hz) {
  157. cs->nsecs = (1000000000/2) / hz;
  158. if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
  159. return -EINVAL;
  160. }
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
  164. /**
  165. * spi_bitbang_setup - default setup for per-word I/O loops
  166. */
  167. int spi_bitbang_setup(struct spi_device *spi)
  168. {
  169. struct spi_bitbang_cs *cs = spi->controller_state;
  170. struct spi_bitbang *bitbang;
  171. bitbang = spi_master_get_devdata(spi->master);
  172. if (!cs) {
  173. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  174. if (!cs)
  175. return -ENOMEM;
  176. spi->controller_state = cs;
  177. }
  178. /* per-word shift register access, in hardware or bitbanging */
  179. cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
  180. if (!cs->txrx_word)
  181. return -EINVAL;
  182. if (bitbang->setup_transfer) {
  183. int retval = bitbang->setup_transfer(spi, NULL);
  184. if (retval < 0)
  185. return retval;
  186. }
  187. dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
  188. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  189. * setup, unless the hardware defaults cooperate to avoid confusion
  190. * between normal (active low) and inverted chipselects.
  191. */
  192. /* deselect chip (low or high) */
  193. mutex_lock(&bitbang->lock);
  194. if (!bitbang->busy) {
  195. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  196. ndelay(cs->nsecs);
  197. }
  198. mutex_unlock(&bitbang->lock);
  199. return 0;
  200. }
  201. EXPORT_SYMBOL_GPL(spi_bitbang_setup);
  202. /**
  203. * spi_bitbang_cleanup - default cleanup for per-word I/O loops
  204. */
  205. void spi_bitbang_cleanup(struct spi_device *spi)
  206. {
  207. kfree(spi->controller_state);
  208. }
  209. EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
  210. static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
  211. {
  212. struct spi_bitbang_cs *cs = spi->controller_state;
  213. unsigned nsecs = cs->nsecs;
  214. struct spi_bitbang *bitbang;
  215. bitbang = spi_master_get_devdata(spi->master);
  216. if (bitbang->set_line_direction) {
  217. int err;
  218. err = bitbang->set_line_direction(spi, !!(t->tx_buf));
  219. if (err < 0)
  220. return err;
  221. }
  222. if (spi->mode & SPI_3WIRE) {
  223. unsigned flags;
  224. flags = t->tx_buf ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX;
  225. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
  226. }
  227. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
  228. }
  229. /*----------------------------------------------------------------------*/
  230. /*
  231. * SECOND PART ... simple transfer queue runner.
  232. *
  233. * This costs a task context per controller, running the queue by
  234. * performing each transfer in sequence. Smarter hardware can queue
  235. * several DMA transfers at once, and process several controller queues
  236. * in parallel; this driver doesn't match such hardware very well.
  237. *
  238. * Drivers can provide word-at-a-time i/o primitives, or provide
  239. * transfer-at-a-time ones to leverage dma or fifo hardware.
  240. */
  241. static int spi_bitbang_prepare_hardware(struct spi_master *spi)
  242. {
  243. struct spi_bitbang *bitbang;
  244. bitbang = spi_master_get_devdata(spi);
  245. mutex_lock(&bitbang->lock);
  246. bitbang->busy = 1;
  247. mutex_unlock(&bitbang->lock);
  248. return 0;
  249. }
  250. static int spi_bitbang_transfer_one(struct spi_master *master,
  251. struct spi_device *spi,
  252. struct spi_transfer *transfer)
  253. {
  254. struct spi_bitbang *bitbang = spi_master_get_devdata(master);
  255. int status = 0;
  256. if (bitbang->setup_transfer) {
  257. status = bitbang->setup_transfer(spi, transfer);
  258. if (status < 0)
  259. goto out;
  260. }
  261. if (transfer->len)
  262. status = bitbang->txrx_bufs(spi, transfer);
  263. if (status == transfer->len)
  264. status = 0;
  265. else if (status >= 0)
  266. status = -EREMOTEIO;
  267. out:
  268. spi_finalize_current_transfer(master);
  269. return status;
  270. }
  271. static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
  272. {
  273. struct spi_bitbang *bitbang;
  274. bitbang = spi_master_get_devdata(spi);
  275. mutex_lock(&bitbang->lock);
  276. bitbang->busy = 0;
  277. mutex_unlock(&bitbang->lock);
  278. return 0;
  279. }
  280. static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
  281. {
  282. struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
  283. /* SPI core provides CS high / low, but bitbang driver
  284. * expects CS active
  285. * spi device driver takes care of handling SPI_CS_HIGH
  286. */
  287. enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
  288. ndelay(SPI_BITBANG_CS_DELAY);
  289. bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
  290. BITBANG_CS_INACTIVE);
  291. ndelay(SPI_BITBANG_CS_DELAY);
  292. }
  293. /*----------------------------------------------------------------------*/
  294. /**
  295. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  296. * @bitbang: driver handle
  297. *
  298. * Caller should have zero-initialized all parts of the structure, and then
  299. * provided callbacks for chip selection and I/O loops. If the master has
  300. * a transfer method, its final step should call spi_bitbang_transfer; or,
  301. * that's the default if the transfer routine is not initialized. It should
  302. * also set up the bus number and number of chipselects.
  303. *
  304. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  305. * hardware that basically exposes a shift register) or per-spi_transfer
  306. * (which takes better advantage of hardware like fifos or DMA engines).
  307. *
  308. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
  309. * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
  310. * master methods. Those methods are the defaults if the bitbang->txrx_bufs
  311. * routine isn't initialized.
  312. *
  313. * This routine registers the spi_master, which will process requests in a
  314. * dedicated task, keeping IRQs unblocked most of the time. To stop
  315. * processing those requests, call spi_bitbang_stop().
  316. *
  317. * On success, this routine will take a reference to master. The caller is
  318. * responsible for calling spi_bitbang_stop() to decrement the reference and
  319. * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
  320. * leak.
  321. */
  322. int spi_bitbang_start(struct spi_bitbang *bitbang)
  323. {
  324. struct spi_master *master = bitbang->master;
  325. int ret;
  326. if (!master || !bitbang->chipselect)
  327. return -EINVAL;
  328. mutex_init(&bitbang->lock);
  329. if (!master->mode_bits)
  330. master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  331. if (master->transfer || master->transfer_one_message)
  332. return -EINVAL;
  333. master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
  334. master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
  335. master->transfer_one = spi_bitbang_transfer_one;
  336. master->set_cs = spi_bitbang_set_cs;
  337. if (!bitbang->txrx_bufs) {
  338. bitbang->use_dma = 0;
  339. bitbang->txrx_bufs = spi_bitbang_bufs;
  340. if (!master->setup) {
  341. if (!bitbang->setup_transfer)
  342. bitbang->setup_transfer =
  343. spi_bitbang_setup_transfer;
  344. master->setup = spi_bitbang_setup;
  345. master->cleanup = spi_bitbang_cleanup;
  346. }
  347. }
  348. /* driver may get busy before register() returns, especially
  349. * if someone registered boardinfo for devices
  350. */
  351. ret = spi_register_master(spi_master_get(master));
  352. if (ret)
  353. spi_master_put(master);
  354. return ret;
  355. }
  356. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  357. /**
  358. * spi_bitbang_stop - stops the task providing spi communication
  359. */
  360. void spi_bitbang_stop(struct spi_bitbang *bitbang)
  361. {
  362. spi_unregister_master(bitbang->master);
  363. }
  364. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  365. MODULE_LICENSE("GPL");