spi_bitbang.c 14 KB

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