spi_bitbang.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. static int
  126. bitbang_transfer_setup(struct spi_device *spi, struct spi_transfer *t)
  127. {
  128. struct spi_bitbang_cs *cs = spi->controller_state;
  129. u8 bits_per_word;
  130. u32 hz;
  131. if (t) {
  132. bits_per_word = t->bits_per_word;
  133. hz = t->speed_hz;
  134. } else {
  135. bits_per_word = 0;
  136. hz = 0;
  137. }
  138. /* spi_transfer level calls that work per-word */
  139. if (!bits_per_word)
  140. bits_per_word = spi->bits_per_word;
  141. if (bits_per_word <= 8)
  142. cs->txrx_bufs = bitbang_txrx_8;
  143. else if (bits_per_word <= 16)
  144. cs->txrx_bufs = bitbang_txrx_16;
  145. else if (bits_per_word <= 32)
  146. cs->txrx_bufs = bitbang_txrx_32;
  147. else
  148. return -EINVAL;
  149. /* nsecs = (clock period)/2 */
  150. if (!hz)
  151. hz = spi->max_speed_hz;
  152. cs->nsecs = (1000000000/2) / hz;
  153. if (cs->nsecs > MAX_UDELAY_MS * 1000)
  154. return -EINVAL;
  155. return 0;
  156. }
  157. /**
  158. * spi_bitbang_setup - default setup for per-word I/O loops
  159. */
  160. int spi_bitbang_setup(struct spi_device *spi)
  161. {
  162. struct spi_bitbang_cs *cs = spi->controller_state;
  163. struct spi_bitbang *bitbang;
  164. int retval;
  165. if (!spi->max_speed_hz)
  166. return -EINVAL;
  167. if (!cs) {
  168. cs = kzalloc(sizeof *cs, SLAB_KERNEL);
  169. if (!cs)
  170. return -ENOMEM;
  171. spi->controller_state = cs;
  172. }
  173. bitbang = spi_master_get_devdata(spi->master);
  174. if (!spi->bits_per_word)
  175. spi->bits_per_word = 8;
  176. /* per-word shift register access, in hardware or bitbanging */
  177. cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
  178. if (!cs->txrx_word)
  179. return -EINVAL;
  180. retval = bitbang_transfer_setup(spi, NULL);
  181. if (retval < 0)
  182. return retval;
  183. dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec\n",
  184. __FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA),
  185. spi->bits_per_word, 2 * cs->nsecs);
  186. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  187. * setup, unless the hardware defaults cooperate to avoid confusion
  188. * between normal (active low) and inverted chipselects.
  189. */
  190. /* deselect chip (low or high) */
  191. spin_lock(&bitbang->lock);
  192. if (!bitbang->busy) {
  193. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  194. ndelay(cs->nsecs);
  195. }
  196. spin_unlock(&bitbang->lock);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(spi_bitbang_setup);
  200. /**
  201. * spi_bitbang_cleanup - default cleanup for per-word I/O loops
  202. */
  203. void spi_bitbang_cleanup(const struct spi_device *spi)
  204. {
  205. kfree(spi->controller_state);
  206. }
  207. EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
  208. static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
  209. {
  210. struct spi_bitbang_cs *cs = spi->controller_state;
  211. unsigned nsecs = cs->nsecs;
  212. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
  213. }
  214. /*----------------------------------------------------------------------*/
  215. /*
  216. * SECOND PART ... simple transfer queue runner.
  217. *
  218. * This costs a task context per controller, running the queue by
  219. * performing each transfer in sequence. Smarter hardware can queue
  220. * several DMA transfers at once, and process several controller queues
  221. * in parallel; this driver doesn't match such hardware very well.
  222. *
  223. * Drivers can provide word-at-a-time i/o primitives, or provide
  224. * transfer-at-a-time ones to leverage dma or fifo hardware.
  225. */
  226. static void bitbang_work(void *_bitbang)
  227. {
  228. struct spi_bitbang *bitbang = _bitbang;
  229. unsigned long flags;
  230. spin_lock_irqsave(&bitbang->lock, flags);
  231. bitbang->busy = 1;
  232. while (!list_empty(&bitbang->queue)) {
  233. struct spi_message *m;
  234. struct spi_device *spi;
  235. unsigned nsecs;
  236. struct spi_transfer *t = NULL;
  237. unsigned tmp;
  238. unsigned cs_change;
  239. int status;
  240. int (*setup_transfer)(struct spi_device *,
  241. struct spi_transfer *);
  242. m = container_of(bitbang->queue.next, struct spi_message,
  243. queue);
  244. list_del_init(&m->queue);
  245. spin_unlock_irqrestore(&bitbang->lock, flags);
  246. /* FIXME this is made-up ... the correct value is known to
  247. * word-at-a-time bitbang code, and presumably chipselect()
  248. * should enforce these requirements too?
  249. */
  250. nsecs = 100;
  251. spi = m->spi;
  252. tmp = 0;
  253. cs_change = 1;
  254. status = 0;
  255. setup_transfer = NULL;
  256. list_for_each_entry (t, &m->transfers, transfer_list) {
  257. if (bitbang->shutdown) {
  258. status = -ESHUTDOWN;
  259. break;
  260. }
  261. /* override or restore speed and wordsize */
  262. if (t->speed_hz || t->bits_per_word) {
  263. setup_transfer = bitbang->setup_transfer;
  264. if (!setup_transfer) {
  265. status = -ENOPROTOOPT;
  266. break;
  267. }
  268. }
  269. if (setup_transfer) {
  270. status = setup_transfer(spi, t);
  271. if (status < 0)
  272. break;
  273. }
  274. /* set up default clock polarity, and activate chip;
  275. * this implicitly updates clock and spi modes as
  276. * previously recorded for this device via setup().
  277. * (and also deselects any other chip that might be
  278. * selected ...)
  279. */
  280. if (cs_change) {
  281. bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
  282. ndelay(nsecs);
  283. }
  284. cs_change = t->cs_change;
  285. if (!t->tx_buf && !t->rx_buf && t->len) {
  286. status = -EINVAL;
  287. break;
  288. }
  289. /* transfer data. the lower level code handles any
  290. * new dma mappings it needs. our caller always gave
  291. * us dma-safe buffers.
  292. */
  293. if (t->len) {
  294. /* REVISIT dma API still needs a designated
  295. * DMA_ADDR_INVALID; ~0 might be better.
  296. */
  297. if (!m->is_dma_mapped)
  298. t->rx_dma = t->tx_dma = 0;
  299. status = bitbang->txrx_bufs(spi, t);
  300. }
  301. if (status != t->len) {
  302. if (status > 0)
  303. status = -EMSGSIZE;
  304. break;
  305. }
  306. m->actual_length += status;
  307. status = 0;
  308. /* protocol tweaks before next transfer */
  309. if (t->delay_usecs)
  310. udelay(t->delay_usecs);
  311. if (!cs_change)
  312. continue;
  313. if (t->transfer_list.next == &m->transfers)
  314. break;
  315. /* sometimes a short mid-message deselect of the chip
  316. * may be needed to terminate a mode or command
  317. */
  318. ndelay(nsecs);
  319. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  320. ndelay(nsecs);
  321. }
  322. m->status = status;
  323. m->complete(m->context);
  324. /* restore speed and wordsize */
  325. if (setup_transfer)
  326. setup_transfer(spi, NULL);
  327. /* normally deactivate chipselect ... unless no error and
  328. * cs_change has hinted that the next message will probably
  329. * be for this chip too.
  330. */
  331. if (!(status == 0 && cs_change)) {
  332. ndelay(nsecs);
  333. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  334. ndelay(nsecs);
  335. }
  336. spin_lock_irqsave(&bitbang->lock, flags);
  337. }
  338. bitbang->busy = 0;
  339. spin_unlock_irqrestore(&bitbang->lock, flags);
  340. }
  341. /**
  342. * spi_bitbang_transfer - default submit to transfer queue
  343. */
  344. int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
  345. {
  346. struct spi_bitbang *bitbang;
  347. unsigned long flags;
  348. m->actual_length = 0;
  349. m->status = -EINPROGRESS;
  350. bitbang = spi_master_get_devdata(spi->master);
  351. if (bitbang->shutdown)
  352. return -ESHUTDOWN;
  353. spin_lock_irqsave(&bitbang->lock, flags);
  354. list_add_tail(&m->queue, &bitbang->queue);
  355. queue_work(bitbang->workqueue, &bitbang->work);
  356. spin_unlock_irqrestore(&bitbang->lock, flags);
  357. return 0;
  358. }
  359. EXPORT_SYMBOL_GPL(spi_bitbang_transfer);
  360. /*----------------------------------------------------------------------*/
  361. /**
  362. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  363. * @bitbang: driver handle
  364. *
  365. * Caller should have zero-initialized all parts of the structure, and then
  366. * provided callbacks for chip selection and I/O loops. If the master has
  367. * a transfer method, its final step should call spi_bitbang_transfer; or,
  368. * that's the default if the transfer routine is not initialized. It should
  369. * also set up the bus number and number of chipselects.
  370. *
  371. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  372. * hardware that basically exposes a shift register) or per-spi_transfer
  373. * (which takes better advantage of hardware like fifos or DMA engines).
  374. *
  375. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup and
  376. * spi_bitbang_cleanup to handle those spi master methods. Those methods are
  377. * the defaults if the bitbang->txrx_bufs routine isn't initialized.
  378. *
  379. * This routine registers the spi_master, which will process requests in a
  380. * dedicated task, keeping IRQs unblocked most of the time. To stop
  381. * processing those requests, call spi_bitbang_stop().
  382. */
  383. int spi_bitbang_start(struct spi_bitbang *bitbang)
  384. {
  385. int status;
  386. if (!bitbang->master || !bitbang->chipselect)
  387. return -EINVAL;
  388. INIT_WORK(&bitbang->work, bitbang_work, bitbang);
  389. spin_lock_init(&bitbang->lock);
  390. INIT_LIST_HEAD(&bitbang->queue);
  391. if (!bitbang->master->transfer)
  392. bitbang->master->transfer = spi_bitbang_transfer;
  393. if (!bitbang->txrx_bufs) {
  394. bitbang->use_dma = 0;
  395. bitbang->txrx_bufs = spi_bitbang_bufs;
  396. if (!bitbang->master->setup) {
  397. bitbang->setup_transfer = bitbang_transfer_setup;
  398. bitbang->master->setup = spi_bitbang_setup;
  399. bitbang->master->cleanup = spi_bitbang_cleanup;
  400. }
  401. } else if (!bitbang->master->setup)
  402. return -EINVAL;
  403. /* this task is the only thing to touch the SPI bits */
  404. bitbang->busy = 0;
  405. bitbang->workqueue = create_singlethread_workqueue(
  406. bitbang->master->cdev.dev->bus_id);
  407. if (bitbang->workqueue == NULL) {
  408. status = -EBUSY;
  409. goto err1;
  410. }
  411. /* driver may get busy before register() returns, especially
  412. * if someone registered boardinfo for devices
  413. */
  414. status = spi_register_master(bitbang->master);
  415. if (status < 0)
  416. goto err2;
  417. return status;
  418. err2:
  419. destroy_workqueue(bitbang->workqueue);
  420. err1:
  421. return status;
  422. }
  423. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  424. /**
  425. * spi_bitbang_stop - stops the task providing spi communication
  426. */
  427. int spi_bitbang_stop(struct spi_bitbang *bitbang)
  428. {
  429. unsigned limit = 500;
  430. spin_lock_irq(&bitbang->lock);
  431. bitbang->shutdown = 0;
  432. while (!list_empty(&bitbang->queue) && limit--) {
  433. spin_unlock_irq(&bitbang->lock);
  434. dev_dbg(bitbang->master->cdev.dev, "wait for queue\n");
  435. msleep(10);
  436. spin_lock_irq(&bitbang->lock);
  437. }
  438. spin_unlock_irq(&bitbang->lock);
  439. if (!list_empty(&bitbang->queue)) {
  440. dev_err(bitbang->master->cdev.dev, "queue didn't empty\n");
  441. return -EBUSY;
  442. }
  443. destroy_workqueue(bitbang->workqueue);
  444. spi_unregister_master(bitbang->master);
  445. return 0;
  446. }
  447. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  448. MODULE_LICENSE("GPL");