spi_bitbang.c 12 KB

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