spi-bitbang.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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/spinlock.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.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 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 = t->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 = t->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 = t->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. unsigned long flags;
  167. bitbang = spi_master_get_devdata(spi->master);
  168. if (!cs) {
  169. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  170. if (!cs)
  171. return -ENOMEM;
  172. spi->controller_state = cs;
  173. }
  174. /* per-word shift register access, in hardware or bitbanging */
  175. cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
  176. if (!cs->txrx_word)
  177. return -EINVAL;
  178. if (bitbang->setup_transfer) {
  179. int retval = bitbang->setup_transfer(spi, NULL);
  180. if (retval < 0)
  181. return retval;
  182. }
  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 int spi_bitbang_prepare_hardware(struct spi_master *spi)
  225. {
  226. struct spi_bitbang *bitbang;
  227. unsigned long flags;
  228. bitbang = spi_master_get_devdata(spi);
  229. spin_lock_irqsave(&bitbang->lock, flags);
  230. bitbang->busy = 1;
  231. spin_unlock_irqrestore(&bitbang->lock, flags);
  232. return 0;
  233. }
  234. static int spi_bitbang_transfer_one(struct spi_master *master,
  235. struct spi_message *m)
  236. {
  237. struct spi_bitbang *bitbang;
  238. unsigned nsecs;
  239. struct spi_transfer *t = NULL;
  240. unsigned cs_change;
  241. int status;
  242. int do_setup = -1;
  243. struct spi_device *spi = m->spi;
  244. bitbang = spi_master_get_devdata(master);
  245. /* FIXME this is made-up ... the correct value is known to
  246. * word-at-a-time bitbang code, and presumably chipselect()
  247. * should enforce these requirements too?
  248. */
  249. nsecs = 100;
  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. if (bitbang->setup_transfer) {
  259. status = bitbang->setup_transfer(spi, t);
  260. if (status < 0)
  261. break;
  262. }
  263. if (do_setup == -1)
  264. do_setup = 0;
  265. }
  266. /* set up default clock polarity, and activate chip;
  267. * this implicitly updates clock and spi modes as
  268. * previously recorded for this device via setup().
  269. * (and also deselects any other chip that might be
  270. * selected ...)
  271. */
  272. if (cs_change) {
  273. bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
  274. ndelay(nsecs);
  275. }
  276. cs_change = t->cs_change;
  277. if (!t->tx_buf && !t->rx_buf && t->len) {
  278. status = -EINVAL;
  279. break;
  280. }
  281. /* transfer data. the lower level code handles any
  282. * new dma mappings it needs. our caller always gave
  283. * us dma-safe buffers.
  284. */
  285. if (t->len) {
  286. /* REVISIT dma API still needs a designated
  287. * DMA_ADDR_INVALID; ~0 might be better.
  288. */
  289. if (!m->is_dma_mapped)
  290. t->rx_dma = t->tx_dma = 0;
  291. status = bitbang->txrx_bufs(spi, t);
  292. }
  293. if (status > 0)
  294. m->actual_length += status;
  295. if (status != t->len) {
  296. /* always report some kind of error */
  297. if (status >= 0)
  298. status = -EREMOTEIO;
  299. break;
  300. }
  301. status = 0;
  302. /* protocol tweaks before next transfer */
  303. if (t->delay_usecs)
  304. udelay(t->delay_usecs);
  305. if (cs_change &&
  306. !list_is_last(&t->transfer_list, &m->transfers)) {
  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. }
  315. m->status = status;
  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. spi_finalize_current_message(master);
  326. return status;
  327. }
  328. static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
  329. {
  330. struct spi_bitbang *bitbang;
  331. unsigned long flags;
  332. bitbang = spi_master_get_devdata(spi);
  333. spin_lock_irqsave(&bitbang->lock, flags);
  334. bitbang->busy = 0;
  335. spin_unlock_irqrestore(&bitbang->lock, flags);
  336. return 0;
  337. }
  338. /*----------------------------------------------------------------------*/
  339. /**
  340. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  341. * @bitbang: driver handle
  342. *
  343. * Caller should have zero-initialized all parts of the structure, and then
  344. * provided callbacks for chip selection and I/O loops. If the master has
  345. * a transfer method, its final step should call spi_bitbang_transfer; or,
  346. * that's the default if the transfer routine is not initialized. It should
  347. * also set up the bus number and number of chipselects.
  348. *
  349. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  350. * hardware that basically exposes a shift register) or per-spi_transfer
  351. * (which takes better advantage of hardware like fifos or DMA engines).
  352. *
  353. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
  354. * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
  355. * master methods. Those methods are the defaults if the bitbang->txrx_bufs
  356. * routine isn't initialized.
  357. *
  358. * This routine registers the spi_master, which will process requests in a
  359. * dedicated task, keeping IRQs unblocked most of the time. To stop
  360. * processing those requests, call spi_bitbang_stop().
  361. *
  362. * On success, this routine will take a reference to master. The caller is
  363. * responsible for calling spi_bitbang_stop() to decrement the reference and
  364. * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
  365. * leak.
  366. */
  367. int spi_bitbang_start(struct spi_bitbang *bitbang)
  368. {
  369. struct spi_master *master = bitbang->master;
  370. int ret;
  371. if (!master || !bitbang->chipselect)
  372. return -EINVAL;
  373. spin_lock_init(&bitbang->lock);
  374. if (!master->mode_bits)
  375. master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  376. if (master->transfer || master->transfer_one_message)
  377. return -EINVAL;
  378. master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
  379. master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
  380. master->transfer_one_message = spi_bitbang_transfer_one;
  381. if (!bitbang->txrx_bufs) {
  382. bitbang->use_dma = 0;
  383. bitbang->txrx_bufs = spi_bitbang_bufs;
  384. if (!master->setup) {
  385. if (!bitbang->setup_transfer)
  386. bitbang->setup_transfer =
  387. spi_bitbang_setup_transfer;
  388. master->setup = spi_bitbang_setup;
  389. master->cleanup = spi_bitbang_cleanup;
  390. }
  391. }
  392. /* driver may get busy before register() returns, especially
  393. * if someone registered boardinfo for devices
  394. */
  395. ret = spi_register_master(spi_master_get(master));
  396. if (ret)
  397. spi_master_put(master);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  401. /**
  402. * spi_bitbang_stop - stops the task providing spi communication
  403. */
  404. void spi_bitbang_stop(struct spi_bitbang *bitbang)
  405. {
  406. spi_unregister_master(bitbang->master);
  407. }
  408. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  409. MODULE_LICENSE("GPL");