spi_bitbang.c 13 KB

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