spi-bitbang.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. unsigned long flags;
  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. spin_lock_irqsave(&bitbang->lock, flags);
  186. if (!bitbang->busy) {
  187. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  188. ndelay(cs->nsecs);
  189. }
  190. spin_unlock_irqrestore(&bitbang->lock, flags);
  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. unsigned long flags;
  224. bitbang = spi_master_get_devdata(spi);
  225. spin_lock_irqsave(&bitbang->lock, flags);
  226. bitbang->busy = 1;
  227. spin_unlock_irqrestore(&bitbang->lock, flags);
  228. return 0;
  229. }
  230. static int spi_bitbang_transfer_one(struct spi_master *master,
  231. struct spi_message *m)
  232. {
  233. struct spi_bitbang *bitbang;
  234. unsigned nsecs;
  235. struct spi_transfer *t = NULL;
  236. unsigned cs_change;
  237. int status;
  238. int do_setup = -1;
  239. struct spi_device *spi = m->spi;
  240. bitbang = spi_master_get_devdata(master);
  241. /* FIXME this is made-up ... the correct value is known to
  242. * word-at-a-time bitbang code, and presumably chipselect()
  243. * should enforce these requirements too?
  244. */
  245. nsecs = 100;
  246. cs_change = 1;
  247. status = 0;
  248. list_for_each_entry(t, &m->transfers, transfer_list) {
  249. /* override speed or wordsize? */
  250. if (t->speed_hz || t->bits_per_word)
  251. do_setup = 1;
  252. /* init (-1) or override (1) transfer params */
  253. if (do_setup != 0) {
  254. if (bitbang->setup_transfer) {
  255. status = bitbang->setup_transfer(spi, t);
  256. if (status < 0)
  257. break;
  258. }
  259. if (do_setup == -1)
  260. do_setup = 0;
  261. }
  262. /* set up default clock polarity, and activate chip;
  263. * this implicitly updates clock and spi modes as
  264. * previously recorded for this device via setup().
  265. * (and also deselects any other chip that might be
  266. * selected ...)
  267. */
  268. if (cs_change) {
  269. bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
  270. ndelay(nsecs);
  271. }
  272. cs_change = t->cs_change;
  273. if (!t->tx_buf && !t->rx_buf && t->len) {
  274. status = -EINVAL;
  275. break;
  276. }
  277. /* transfer data. the lower level code handles any
  278. * new dma mappings it needs. our caller always gave
  279. * us dma-safe buffers.
  280. */
  281. if (t->len) {
  282. /* REVISIT dma API still needs a designated
  283. * DMA_ADDR_INVALID; ~0 might be better.
  284. */
  285. if (!m->is_dma_mapped)
  286. t->rx_dma = t->tx_dma = 0;
  287. status = bitbang->txrx_bufs(spi, t);
  288. }
  289. if (status > 0)
  290. m->actual_length += status;
  291. if (status != t->len) {
  292. /* always report some kind of error */
  293. if (status >= 0)
  294. status = -EREMOTEIO;
  295. break;
  296. }
  297. status = 0;
  298. /* protocol tweaks before next transfer */
  299. if (t->delay_usecs)
  300. udelay(t->delay_usecs);
  301. if (cs_change &&
  302. !list_is_last(&t->transfer_list, &m->transfers)) {
  303. /* sometimes a short mid-message deselect of the chip
  304. * may be needed to terminate a mode or command
  305. */
  306. ndelay(nsecs);
  307. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  308. ndelay(nsecs);
  309. }
  310. }
  311. m->status = status;
  312. /* normally deactivate chipselect ... unless no error and
  313. * cs_change has hinted that the next message will probably
  314. * be for this chip too.
  315. */
  316. if (!(status == 0 && cs_change)) {
  317. ndelay(nsecs);
  318. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  319. ndelay(nsecs);
  320. }
  321. spi_finalize_current_message(master);
  322. return status;
  323. }
  324. static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
  325. {
  326. struct spi_bitbang *bitbang;
  327. unsigned long flags;
  328. bitbang = spi_master_get_devdata(spi);
  329. spin_lock_irqsave(&bitbang->lock, flags);
  330. bitbang->busy = 0;
  331. spin_unlock_irqrestore(&bitbang->lock, flags);
  332. return 0;
  333. }
  334. /*----------------------------------------------------------------------*/
  335. /**
  336. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  337. * @bitbang: driver handle
  338. *
  339. * Caller should have zero-initialized all parts of the structure, and then
  340. * provided callbacks for chip selection and I/O loops. If the master has
  341. * a transfer method, its final step should call spi_bitbang_transfer; or,
  342. * that's the default if the transfer routine is not initialized. It should
  343. * also set up the bus number and number of chipselects.
  344. *
  345. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  346. * hardware that basically exposes a shift register) or per-spi_transfer
  347. * (which takes better advantage of hardware like fifos or DMA engines).
  348. *
  349. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
  350. * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
  351. * master methods. Those methods are the defaults if the bitbang->txrx_bufs
  352. * routine isn't initialized.
  353. *
  354. * This routine registers the spi_master, which will process requests in a
  355. * dedicated task, keeping IRQs unblocked most of the time. To stop
  356. * processing those requests, call spi_bitbang_stop().
  357. *
  358. * On success, this routine will take a reference to master. The caller is
  359. * responsible for calling spi_bitbang_stop() to decrement the reference and
  360. * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
  361. * leak.
  362. */
  363. int spi_bitbang_start(struct spi_bitbang *bitbang)
  364. {
  365. struct spi_master *master = bitbang->master;
  366. int ret;
  367. if (!master || !bitbang->chipselect)
  368. return -EINVAL;
  369. spin_lock_init(&bitbang->lock);
  370. if (!master->mode_bits)
  371. master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  372. if (master->transfer || master->transfer_one_message)
  373. return -EINVAL;
  374. master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
  375. master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
  376. master->transfer_one_message = spi_bitbang_transfer_one;
  377. if (!bitbang->txrx_bufs) {
  378. bitbang->use_dma = 0;
  379. bitbang->txrx_bufs = spi_bitbang_bufs;
  380. if (!master->setup) {
  381. if (!bitbang->setup_transfer)
  382. bitbang->setup_transfer =
  383. spi_bitbang_setup_transfer;
  384. master->setup = spi_bitbang_setup;
  385. master->cleanup = spi_bitbang_cleanup;
  386. }
  387. }
  388. /* driver may get busy before register() returns, especially
  389. * if someone registered boardinfo for devices
  390. */
  391. ret = spi_register_master(spi_master_get(master));
  392. if (ret)
  393. spi_master_put(master);
  394. return 0;
  395. }
  396. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  397. /**
  398. * spi_bitbang_stop - stops the task providing spi communication
  399. */
  400. void spi_bitbang_stop(struct spi_bitbang *bitbang)
  401. {
  402. spi_unregister_master(bitbang->master);
  403. }
  404. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  405. MODULE_LICENSE("GPL");