spi_bitbang.c 13 KB

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