spi-bitbang.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. * 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/init.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/spi_bitbang.h>
  29. /*----------------------------------------------------------------------*/
  30. /*
  31. * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
  32. * Use this for GPIO or shift-register level hardware APIs.
  33. *
  34. * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
  35. * to glue code. These bitbang setup() and cleanup() routines are always
  36. * used, though maybe they're called from controller-aware code.
  37. *
  38. * chipselect() and friends may use use spi_device->controller_data and
  39. * controller registers as appropriate.
  40. *
  41. *
  42. * NOTE: SPI controller pins can often be used as GPIO pins instead,
  43. * which means you could use a bitbang driver either to get hardware
  44. * working quickly, or testing for differences that aren't speed related.
  45. */
  46. struct spi_bitbang_cs {
  47. unsigned nsecs; /* (clock cycle time)/2 */
  48. u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
  49. u32 word, u8 bits);
  50. unsigned (*txrx_bufs)(struct spi_device *,
  51. u32 (*txrx_word)(
  52. struct spi_device *spi,
  53. unsigned nsecs,
  54. u32 word, u8 bits),
  55. unsigned, struct spi_transfer *);
  56. };
  57. static unsigned bitbang_txrx_8(
  58. struct spi_device *spi,
  59. u32 (*txrx_word)(struct spi_device *spi,
  60. unsigned nsecs,
  61. u32 word, u8 bits),
  62. unsigned ns,
  63. struct spi_transfer *t
  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);
  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 ns,
  86. struct spi_transfer *t
  87. ) {
  88. unsigned bits = t->bits_per_word;
  89. unsigned count = t->len;
  90. const u16 *tx = t->tx_buf;
  91. u16 *rx = t->rx_buf;
  92. while (likely(count > 1)) {
  93. u16 word = 0;
  94. if (tx)
  95. word = *tx++;
  96. word = txrx_word(spi, ns, word, bits);
  97. if (rx)
  98. *rx++ = word;
  99. count -= 2;
  100. }
  101. return t->len - count;
  102. }
  103. static unsigned bitbang_txrx_32(
  104. struct spi_device *spi,
  105. u32 (*txrx_word)(struct spi_device *spi,
  106. unsigned nsecs,
  107. u32 word, u8 bits),
  108. unsigned ns,
  109. struct spi_transfer *t
  110. ) {
  111. unsigned bits = t->bits_per_word;
  112. unsigned count = t->len;
  113. const u32 *tx = t->tx_buf;
  114. u32 *rx = t->rx_buf;
  115. while (likely(count > 3)) {
  116. u32 word = 0;
  117. if (tx)
  118. word = *tx++;
  119. word = txrx_word(spi, ns, word, bits);
  120. if (rx)
  121. *rx++ = word;
  122. count -= 4;
  123. }
  124. return t->len - count;
  125. }
  126. int spi_bitbang_setup_transfer(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. if (hz) {
  153. cs->nsecs = (1000000000/2) / hz;
  154. if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
  155. return -EINVAL;
  156. }
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
  160. /**
  161. * spi_bitbang_setup - default setup for per-word I/O loops
  162. */
  163. int spi_bitbang_setup(struct spi_device *spi)
  164. {
  165. struct spi_bitbang_cs *cs = spi->controller_state;
  166. struct spi_bitbang *bitbang;
  167. int retval;
  168. unsigned long flags;
  169. bitbang = spi_master_get_devdata(spi->master);
  170. if (!cs) {
  171. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  172. if (!cs)
  173. return -ENOMEM;
  174. spi->controller_state = cs;
  175. }
  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->setup_transfer(spi, NULL);
  181. if (retval < 0)
  182. return retval;
  183. dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
  184. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  185. * setup, unless the hardware defaults cooperate to avoid confusion
  186. * between normal (active low) and inverted chipselects.
  187. */
  188. /* deselect chip (low or high) */
  189. spin_lock_irqsave(&bitbang->lock, flags);
  190. if (!bitbang->busy) {
  191. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  192. ndelay(cs->nsecs);
  193. }
  194. spin_unlock_irqrestore(&bitbang->lock, flags);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(spi_bitbang_setup);
  198. /**
  199. * spi_bitbang_cleanup - default cleanup for per-word I/O loops
  200. */
  201. void spi_bitbang_cleanup(struct spi_device *spi)
  202. {
  203. kfree(spi->controller_state);
  204. }
  205. EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
  206. static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
  207. {
  208. struct spi_bitbang_cs *cs = spi->controller_state;
  209. unsigned nsecs = cs->nsecs;
  210. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
  211. }
  212. /*----------------------------------------------------------------------*/
  213. /*
  214. * SECOND PART ... simple transfer queue runner.
  215. *
  216. * This costs a task context per controller, running the queue by
  217. * performing each transfer in sequence. Smarter hardware can queue
  218. * several DMA transfers at once, and process several controller queues
  219. * in parallel; this driver doesn't match such hardware very well.
  220. *
  221. * Drivers can provide word-at-a-time i/o primitives, or provide
  222. * transfer-at-a-time ones to leverage dma or fifo hardware.
  223. */
  224. static void bitbang_work(struct work_struct *work)
  225. {
  226. struct spi_bitbang *bitbang =
  227. container_of(work, struct spi_bitbang, work);
  228. unsigned long flags;
  229. struct spi_message *m, *_m;
  230. spin_lock_irqsave(&bitbang->lock, flags);
  231. bitbang->busy = 1;
  232. list_for_each_entry_safe(m, _m, &bitbang->queue, queue) {
  233. struct spi_device *spi;
  234. unsigned nsecs;
  235. struct spi_transfer *t = NULL;
  236. unsigned tmp;
  237. unsigned cs_change;
  238. int status;
  239. int do_setup = -1;
  240. list_del(&m->queue);
  241. spin_unlock_irqrestore(&bitbang->lock, flags);
  242. /* FIXME this is made-up ... the correct value is known to
  243. * word-at-a-time bitbang code, and presumably chipselect()
  244. * should enforce these requirements too?
  245. */
  246. nsecs = 100;
  247. spi = m->spi;
  248. tmp = 0;
  249. cs_change = 1;
  250. status = 0;
  251. list_for_each_entry (t, &m->transfers, transfer_list) {
  252. /* override speed or wordsize? */
  253. if (t->speed_hz || t->bits_per_word)
  254. do_setup = 1;
  255. /* init (-1) or override (1) transfer params */
  256. if (do_setup != 0) {
  257. status = bitbang->setup_transfer(spi, t);
  258. if (status < 0)
  259. break;
  260. if (do_setup == -1)
  261. do_setup = 0;
  262. }
  263. /* set up default clock polarity, and activate chip;
  264. * this implicitly updates clock and spi modes as
  265. * previously recorded for this device via setup().
  266. * (and also deselects any other chip that might be
  267. * selected ...)
  268. */
  269. if (cs_change) {
  270. bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
  271. ndelay(nsecs);
  272. }
  273. cs_change = t->cs_change;
  274. if (!t->tx_buf && !t->rx_buf && t->len) {
  275. status = -EINVAL;
  276. break;
  277. }
  278. /* transfer data. the lower level code handles any
  279. * new dma mappings it needs. our caller always gave
  280. * us dma-safe buffers.
  281. */
  282. if (t->len) {
  283. /* REVISIT dma API still needs a designated
  284. * DMA_ADDR_INVALID; ~0 might be better.
  285. */
  286. if (!m->is_dma_mapped)
  287. t->rx_dma = t->tx_dma = 0;
  288. status = bitbang->txrx_bufs(spi, t);
  289. }
  290. if (status > 0)
  291. m->actual_length += status;
  292. if (status != t->len) {
  293. /* always report some kind of error */
  294. if (status >= 0)
  295. status = -EREMOTEIO;
  296. break;
  297. }
  298. status = 0;
  299. /* protocol tweaks before next transfer */
  300. if (t->delay_usecs)
  301. udelay(t->delay_usecs);
  302. if (cs_change && !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. m->complete(m->context);
  313. /* normally deactivate chipselect ... unless no error and
  314. * cs_change has hinted that the next message will probably
  315. * be for this chip too.
  316. */
  317. if (!(status == 0 && cs_change)) {
  318. ndelay(nsecs);
  319. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  320. ndelay(nsecs);
  321. }
  322. spin_lock_irqsave(&bitbang->lock, flags);
  323. }
  324. bitbang->busy = 0;
  325. spin_unlock_irqrestore(&bitbang->lock, flags);
  326. }
  327. /**
  328. * spi_bitbang_transfer - default submit to transfer queue
  329. */
  330. int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
  331. {
  332. struct spi_bitbang *bitbang;
  333. unsigned long flags;
  334. int status = 0;
  335. m->actual_length = 0;
  336. m->status = -EINPROGRESS;
  337. bitbang = spi_master_get_devdata(spi->master);
  338. spin_lock_irqsave(&bitbang->lock, flags);
  339. if (!spi->max_speed_hz)
  340. status = -ENETDOWN;
  341. else {
  342. list_add_tail(&m->queue, &bitbang->queue);
  343. queue_work(bitbang->workqueue, &bitbang->work);
  344. }
  345. spin_unlock_irqrestore(&bitbang->lock, flags);
  346. return status;
  347. }
  348. EXPORT_SYMBOL_GPL(spi_bitbang_transfer);
  349. /*----------------------------------------------------------------------*/
  350. /**
  351. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  352. * @bitbang: driver handle
  353. *
  354. * Caller should have zero-initialized all parts of the structure, and then
  355. * provided callbacks for chip selection and I/O loops. If the master has
  356. * a transfer method, its final step should call spi_bitbang_transfer; or,
  357. * that's the default if the transfer routine is not initialized. It should
  358. * also set up the bus number and number of chipselects.
  359. *
  360. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  361. * hardware that basically exposes a shift register) or per-spi_transfer
  362. * (which takes better advantage of hardware like fifos or DMA engines).
  363. *
  364. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
  365. * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
  366. * master methods. Those methods are the defaults if the bitbang->txrx_bufs
  367. * routine isn't initialized.
  368. *
  369. * This routine registers the spi_master, which will process requests in a
  370. * dedicated task, keeping IRQs unblocked most of the time. To stop
  371. * processing those requests, call spi_bitbang_stop().
  372. */
  373. int spi_bitbang_start(struct spi_bitbang *bitbang)
  374. {
  375. struct spi_master *master = bitbang->master;
  376. int status;
  377. if (!master || !bitbang->chipselect)
  378. return -EINVAL;
  379. INIT_WORK(&bitbang->work, bitbang_work);
  380. spin_lock_init(&bitbang->lock);
  381. INIT_LIST_HEAD(&bitbang->queue);
  382. if (!master->mode_bits)
  383. master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  384. if (!master->transfer)
  385. master->transfer = spi_bitbang_transfer;
  386. if (!bitbang->txrx_bufs) {
  387. bitbang->use_dma = 0;
  388. bitbang->txrx_bufs = spi_bitbang_bufs;
  389. if (!master->setup) {
  390. if (!bitbang->setup_transfer)
  391. bitbang->setup_transfer =
  392. spi_bitbang_setup_transfer;
  393. master->setup = spi_bitbang_setup;
  394. master->cleanup = spi_bitbang_cleanup;
  395. }
  396. } else if (!master->setup)
  397. return -EINVAL;
  398. if (master->transfer == spi_bitbang_transfer &&
  399. !bitbang->setup_transfer)
  400. return -EINVAL;
  401. /* this task is the only thing to touch the SPI bits */
  402. bitbang->busy = 0;
  403. bitbang->workqueue = create_singlethread_workqueue(
  404. dev_name(master->dev.parent));
  405. if (bitbang->workqueue == NULL) {
  406. status = -EBUSY;
  407. goto err1;
  408. }
  409. /* driver may get busy before register() returns, especially
  410. * if someone registered boardinfo for devices
  411. */
  412. status = spi_register_master(master);
  413. if (status < 0)
  414. goto err2;
  415. return status;
  416. err2:
  417. destroy_workqueue(bitbang->workqueue);
  418. err1:
  419. return status;
  420. }
  421. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  422. /**
  423. * spi_bitbang_stop - stops the task providing spi communication
  424. */
  425. int spi_bitbang_stop(struct spi_bitbang *bitbang)
  426. {
  427. spi_unregister_master(bitbang->master);
  428. WARN_ON(!list_empty(&bitbang->queue));
  429. destroy_workqueue(bitbang->workqueue);
  430. return 0;
  431. }
  432. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  433. MODULE_LICENSE("GPL");