spi-bitbang.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. /*----------------------------------------------------------------------*/
  25. /*
  26. * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
  27. * Use this for GPIO or shift-register level hardware APIs.
  28. *
  29. * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
  30. * to glue code. These bitbang setup() and cleanup() routines are always
  31. * used, though maybe they're called from controller-aware code.
  32. *
  33. * chipselect() and friends may use spi_device->controller_data and
  34. * controller registers as appropriate.
  35. *
  36. *
  37. * NOTE: SPI controller pins can often be used as GPIO pins instead,
  38. * which means you could use a bitbang driver either to get hardware
  39. * working quickly, or testing for differences that aren't speed related.
  40. */
  41. struct spi_bitbang_cs {
  42. unsigned nsecs; /* (clock cycle time)/2 */
  43. u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
  44. u32 word, u8 bits);
  45. unsigned (*txrx_bufs)(struct spi_device *,
  46. u32 (*txrx_word)(
  47. struct spi_device *spi,
  48. unsigned nsecs,
  49. u32 word, u8 bits),
  50. unsigned, struct spi_transfer *);
  51. };
  52. static unsigned bitbang_txrx_8(
  53. struct spi_device *spi,
  54. u32 (*txrx_word)(struct spi_device *spi,
  55. unsigned nsecs,
  56. u32 word, u8 bits),
  57. unsigned ns,
  58. struct spi_transfer *t
  59. ) {
  60. unsigned bits = t->bits_per_word;
  61. unsigned count = t->len;
  62. const u8 *tx = t->tx_buf;
  63. u8 *rx = t->rx_buf;
  64. while (likely(count > 0)) {
  65. u8 word = 0;
  66. if (tx)
  67. word = *tx++;
  68. word = txrx_word(spi, ns, word, bits);
  69. if (rx)
  70. *rx++ = word;
  71. count -= 1;
  72. }
  73. return t->len - count;
  74. }
  75. static unsigned bitbang_txrx_16(
  76. struct spi_device *spi,
  77. u32 (*txrx_word)(struct spi_device *spi,
  78. unsigned nsecs,
  79. u32 word, u8 bits),
  80. unsigned ns,
  81. struct spi_transfer *t
  82. ) {
  83. unsigned bits = t->bits_per_word;
  84. unsigned count = t->len;
  85. const u16 *tx = t->tx_buf;
  86. u16 *rx = t->rx_buf;
  87. while (likely(count > 1)) {
  88. u16 word = 0;
  89. if (tx)
  90. word = *tx++;
  91. word = txrx_word(spi, ns, word, bits);
  92. if (rx)
  93. *rx++ = word;
  94. count -= 2;
  95. }
  96. return t->len - count;
  97. }
  98. static unsigned bitbang_txrx_32(
  99. struct spi_device *spi,
  100. u32 (*txrx_word)(struct spi_device *spi,
  101. unsigned nsecs,
  102. u32 word, u8 bits),
  103. unsigned ns,
  104. struct spi_transfer *t
  105. ) {
  106. unsigned bits = t->bits_per_word;
  107. unsigned count = t->len;
  108. const u32 *tx = t->tx_buf;
  109. u32 *rx = t->rx_buf;
  110. while (likely(count > 3)) {
  111. u32 word = 0;
  112. if (tx)
  113. word = *tx++;
  114. word = txrx_word(spi, ns, word, bits);
  115. if (rx)
  116. *rx++ = word;
  117. count -= 4;
  118. }
  119. return t->len - count;
  120. }
  121. int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  122. {
  123. struct spi_bitbang_cs *cs = spi->controller_state;
  124. u8 bits_per_word;
  125. u32 hz;
  126. if (t) {
  127. bits_per_word = t->bits_per_word;
  128. hz = t->speed_hz;
  129. } else {
  130. bits_per_word = 0;
  131. hz = 0;
  132. }
  133. /* spi_transfer level calls that work per-word */
  134. if (!bits_per_word)
  135. bits_per_word = spi->bits_per_word;
  136. if (bits_per_word <= 8)
  137. cs->txrx_bufs = bitbang_txrx_8;
  138. else if (bits_per_word <= 16)
  139. cs->txrx_bufs = bitbang_txrx_16;
  140. else if (bits_per_word <= 32)
  141. cs->txrx_bufs = bitbang_txrx_32;
  142. else
  143. return -EINVAL;
  144. /* nsecs = (clock period)/2 */
  145. if (!hz)
  146. hz = spi->max_speed_hz;
  147. if (hz) {
  148. cs->nsecs = (1000000000/2) / hz;
  149. if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
  155. /**
  156. * spi_bitbang_setup - default setup for per-word I/O loops
  157. */
  158. int spi_bitbang_setup(struct spi_device *spi)
  159. {
  160. struct spi_bitbang_cs *cs = spi->controller_state;
  161. struct spi_bitbang *bitbang;
  162. bitbang = spi_master_get_devdata(spi->master);
  163. if (!cs) {
  164. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  165. if (!cs)
  166. return -ENOMEM;
  167. spi->controller_state = cs;
  168. }
  169. /* per-word shift register access, in hardware or bitbanging */
  170. cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
  171. if (!cs->txrx_word)
  172. return -EINVAL;
  173. if (bitbang->setup_transfer) {
  174. int retval = bitbang->setup_transfer(spi, NULL);
  175. if (retval < 0)
  176. return retval;
  177. }
  178. dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
  179. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  180. * setup, unless the hardware defaults cooperate to avoid confusion
  181. * between normal (active low) and inverted chipselects.
  182. */
  183. /* deselect chip (low or high) */
  184. mutex_lock(&bitbang->lock);
  185. if (!bitbang->busy) {
  186. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  187. ndelay(cs->nsecs);
  188. }
  189. mutex_unlock(&bitbang->lock);
  190. return 0;
  191. }
  192. EXPORT_SYMBOL_GPL(spi_bitbang_setup);
  193. /**
  194. * spi_bitbang_cleanup - default cleanup for per-word I/O loops
  195. */
  196. void spi_bitbang_cleanup(struct spi_device *spi)
  197. {
  198. kfree(spi->controller_state);
  199. }
  200. EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
  201. static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
  202. {
  203. struct spi_bitbang_cs *cs = spi->controller_state;
  204. unsigned nsecs = cs->nsecs;
  205. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
  206. }
  207. /*----------------------------------------------------------------------*/
  208. /*
  209. * SECOND PART ... simple transfer queue runner.
  210. *
  211. * This costs a task context per controller, running the queue by
  212. * performing each transfer in sequence. Smarter hardware can queue
  213. * several DMA transfers at once, and process several controller queues
  214. * in parallel; this driver doesn't match such hardware very well.
  215. *
  216. * Drivers can provide word-at-a-time i/o primitives, or provide
  217. * transfer-at-a-time ones to leverage dma or fifo hardware.
  218. */
  219. static int spi_bitbang_prepare_hardware(struct spi_master *spi)
  220. {
  221. struct spi_bitbang *bitbang;
  222. bitbang = spi_master_get_devdata(spi);
  223. mutex_lock(&bitbang->lock);
  224. bitbang->busy = 1;
  225. mutex_unlock(&bitbang->lock);
  226. return 0;
  227. }
  228. static int spi_bitbang_transfer_one(struct spi_master *master,
  229. struct spi_message *m)
  230. {
  231. struct spi_bitbang *bitbang;
  232. unsigned nsecs;
  233. struct spi_transfer *t = NULL;
  234. unsigned cs_change;
  235. int status;
  236. struct spi_device *spi = m->spi;
  237. bitbang = spi_master_get_devdata(master);
  238. /* FIXME this is made-up ... the correct value is known to
  239. * word-at-a-time bitbang code, and presumably chipselect()
  240. * should enforce these requirements too?
  241. */
  242. nsecs = 100;
  243. cs_change = 1;
  244. status = 0;
  245. list_for_each_entry(t, &m->transfers, transfer_list) {
  246. if (bitbang->setup_transfer) {
  247. status = bitbang->setup_transfer(spi, t);
  248. if (status < 0)
  249. break;
  250. }
  251. /* set up default clock polarity, and activate chip;
  252. * this implicitly updates clock and spi modes as
  253. * previously recorded for this device via setup().
  254. * (and also deselects any other chip that might be
  255. * selected ...)
  256. */
  257. if (cs_change) {
  258. bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
  259. ndelay(nsecs);
  260. }
  261. cs_change = t->cs_change;
  262. if (!t->tx_buf && !t->rx_buf && t->len) {
  263. status = -EINVAL;
  264. break;
  265. }
  266. /* transfer data. the lower level code handles any
  267. * new dma mappings it needs. our caller always gave
  268. * us dma-safe buffers.
  269. */
  270. if (t->len) {
  271. /* REVISIT dma API still needs a designated
  272. * DMA_ADDR_INVALID; ~0 might be better.
  273. */
  274. if (!m->is_dma_mapped)
  275. t->rx_dma = t->tx_dma = 0;
  276. status = bitbang->txrx_bufs(spi, t);
  277. }
  278. if (status > 0)
  279. m->actual_length += status;
  280. if (status != t->len) {
  281. /* always report some kind of error */
  282. if (status >= 0)
  283. status = -EREMOTEIO;
  284. break;
  285. }
  286. status = 0;
  287. /* protocol tweaks before next transfer */
  288. if (t->delay_usecs)
  289. udelay(t->delay_usecs);
  290. if (cs_change &&
  291. !list_is_last(&t->transfer_list, &m->transfers)) {
  292. /* sometimes a short mid-message deselect of the chip
  293. * may be needed to terminate a mode or command
  294. */
  295. ndelay(nsecs);
  296. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  297. ndelay(nsecs);
  298. }
  299. }
  300. m->status = status;
  301. /* normally deactivate chipselect ... unless no error and
  302. * cs_change has hinted that the next message will probably
  303. * be for this chip too.
  304. */
  305. if (!(status == 0 && cs_change)) {
  306. ndelay(nsecs);
  307. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  308. ndelay(nsecs);
  309. }
  310. spi_finalize_current_message(master);
  311. return status;
  312. }
  313. static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
  314. {
  315. struct spi_bitbang *bitbang;
  316. bitbang = spi_master_get_devdata(spi);
  317. mutex_lock(&bitbang->lock);
  318. bitbang->busy = 0;
  319. mutex_unlock(&bitbang->lock);
  320. return 0;
  321. }
  322. /*----------------------------------------------------------------------*/
  323. /**
  324. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  325. * @bitbang: driver handle
  326. *
  327. * Caller should have zero-initialized all parts of the structure, and then
  328. * provided callbacks for chip selection and I/O loops. If the master has
  329. * a transfer method, its final step should call spi_bitbang_transfer; or,
  330. * that's the default if the transfer routine is not initialized. It should
  331. * also set up the bus number and number of chipselects.
  332. *
  333. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  334. * hardware that basically exposes a shift register) or per-spi_transfer
  335. * (which takes better advantage of hardware like fifos or DMA engines).
  336. *
  337. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
  338. * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
  339. * master methods. Those methods are the defaults if the bitbang->txrx_bufs
  340. * routine isn't initialized.
  341. *
  342. * This routine registers the spi_master, which will process requests in a
  343. * dedicated task, keeping IRQs unblocked most of the time. To stop
  344. * processing those requests, call spi_bitbang_stop().
  345. *
  346. * On success, this routine will take a reference to master. The caller is
  347. * responsible for calling spi_bitbang_stop() to decrement the reference and
  348. * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
  349. * leak.
  350. */
  351. int spi_bitbang_start(struct spi_bitbang *bitbang)
  352. {
  353. struct spi_master *master = bitbang->master;
  354. int ret;
  355. if (!master || !bitbang->chipselect)
  356. return -EINVAL;
  357. mutex_init(&bitbang->lock);
  358. if (!master->mode_bits)
  359. master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  360. if (master->transfer || master->transfer_one_message)
  361. return -EINVAL;
  362. master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
  363. master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
  364. master->transfer_one_message = spi_bitbang_transfer_one;
  365. if (!bitbang->txrx_bufs) {
  366. bitbang->use_dma = 0;
  367. bitbang->txrx_bufs = spi_bitbang_bufs;
  368. if (!master->setup) {
  369. if (!bitbang->setup_transfer)
  370. bitbang->setup_transfer =
  371. spi_bitbang_setup_transfer;
  372. master->setup = spi_bitbang_setup;
  373. master->cleanup = spi_bitbang_cleanup;
  374. }
  375. }
  376. /* driver may get busy before register() returns, especially
  377. * if someone registered boardinfo for devices
  378. */
  379. ret = spi_register_master(spi_master_get(master));
  380. if (ret)
  381. spi_master_put(master);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  385. /**
  386. * spi_bitbang_stop - stops the task providing spi communication
  387. */
  388. void spi_bitbang_stop(struct spi_bitbang *bitbang)
  389. {
  390. spi_unregister_master(bitbang->master);
  391. }
  392. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  393. MODULE_LICENSE("GPL");