spi-bitbang.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Polling/bitbanging SPI host controller controller driver utilities
  4. */
  5. #include <linux/spinlock.h>
  6. #include <linux/workqueue.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/module.h>
  9. #include <linux/delay.h>
  10. #include <linux/errno.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/time64.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/spi/spi_bitbang.h>
  16. #define SPI_BITBANG_CS_DELAY 100
  17. /*----------------------------------------------------------------------*/
  18. /*
  19. * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
  20. * Use this for GPIO or shift-register level hardware APIs.
  21. *
  22. * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
  23. * to glue code. These bitbang setup() and cleanup() routines are always
  24. * used, though maybe they're called from controller-aware code.
  25. *
  26. * chipselect() and friends may use spi_device->controller_data and
  27. * controller registers as appropriate.
  28. *
  29. *
  30. * NOTE: SPI controller pins can often be used as GPIO pins instead,
  31. * which means you could use a bitbang driver either to get hardware
  32. * working quickly, or testing for differences that aren't speed related.
  33. */
  34. typedef unsigned int (*spi_bb_txrx_bufs_fn)(struct spi_device *, spi_bb_txrx_word_fn,
  35. unsigned int, struct spi_transfer *,
  36. unsigned int);
  37. struct spi_bitbang_cs {
  38. unsigned int nsecs; /* (clock cycle time) / 2 */
  39. spi_bb_txrx_word_fn txrx_word;
  40. spi_bb_txrx_bufs_fn txrx_bufs;
  41. };
  42. static unsigned int bitbang_txrx_8(struct spi_device *spi,
  43. spi_bb_txrx_word_fn txrx_word,
  44. unsigned int ns,
  45. struct spi_transfer *t,
  46. unsigned int flags)
  47. {
  48. struct spi_bitbang *bitbang;
  49. unsigned int bits = t->bits_per_word;
  50. unsigned int count = t->len;
  51. const u8 *tx = t->tx_buf;
  52. u8 *rx = t->rx_buf;
  53. bitbang = spi_controller_get_devdata(spi->controller);
  54. while (likely(count > 0)) {
  55. u8 word = 0;
  56. if (tx)
  57. word = *tx++;
  58. else
  59. word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFF : 0;
  60. word = txrx_word(spi, ns, word, bits, flags);
  61. if (rx)
  62. *rx++ = word;
  63. count -= 1;
  64. }
  65. if (bitbang->set_mosi_idle)
  66. bitbang->set_mosi_idle(spi);
  67. return t->len - count;
  68. }
  69. static unsigned int bitbang_txrx_16(struct spi_device *spi,
  70. spi_bb_txrx_word_fn txrx_word,
  71. unsigned int ns,
  72. struct spi_transfer *t,
  73. unsigned int flags)
  74. {
  75. struct spi_bitbang *bitbang;
  76. unsigned int bits = t->bits_per_word;
  77. unsigned int count = t->len;
  78. const u16 *tx = t->tx_buf;
  79. u16 *rx = t->rx_buf;
  80. bitbang = spi_controller_get_devdata(spi->controller);
  81. while (likely(count > 1)) {
  82. u16 word = 0;
  83. if (tx)
  84. word = *tx++;
  85. else
  86. word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFFFF : 0;
  87. word = txrx_word(spi, ns, word, bits, flags);
  88. if (rx)
  89. *rx++ = word;
  90. count -= 2;
  91. }
  92. if (bitbang->set_mosi_idle)
  93. bitbang->set_mosi_idle(spi);
  94. return t->len - count;
  95. }
  96. static unsigned int bitbang_txrx_32(struct spi_device *spi,
  97. spi_bb_txrx_word_fn txrx_word,
  98. unsigned int ns,
  99. struct spi_transfer *t,
  100. unsigned int flags)
  101. {
  102. struct spi_bitbang *bitbang;
  103. unsigned int bits = t->bits_per_word;
  104. unsigned int count = t->len;
  105. const u32 *tx = t->tx_buf;
  106. u32 *rx = t->rx_buf;
  107. bitbang = spi_controller_get_devdata(spi->controller);
  108. while (likely(count > 3)) {
  109. u32 word = 0;
  110. if (tx)
  111. word = *tx++;
  112. else
  113. word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFFFFFFFF : 0;
  114. word = txrx_word(spi, ns, word, bits, flags);
  115. if (rx)
  116. *rx++ = word;
  117. count -= 4;
  118. }
  119. if (bitbang->set_mosi_idle)
  120. bitbang->set_mosi_idle(spi);
  121. return t->len - count;
  122. }
  123. int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  124. {
  125. struct spi_bitbang_cs *cs = spi->controller_state;
  126. u8 bits_per_word;
  127. u32 hz;
  128. if (t) {
  129. bits_per_word = t->bits_per_word;
  130. hz = t->speed_hz;
  131. } else {
  132. bits_per_word = 0;
  133. hz = 0;
  134. }
  135. /* spi_transfer level calls that work per-word */
  136. if (!bits_per_word)
  137. bits_per_word = spi->bits_per_word;
  138. if (bits_per_word <= 8)
  139. cs->txrx_bufs = bitbang_txrx_8;
  140. else if (bits_per_word <= 16)
  141. cs->txrx_bufs = bitbang_txrx_16;
  142. else if (bits_per_word <= 32)
  143. cs->txrx_bufs = bitbang_txrx_32;
  144. else
  145. return -EINVAL;
  146. /* nsecs = (clock period)/2 */
  147. if (!hz)
  148. hz = spi->max_speed_hz;
  149. if (hz) {
  150. cs->nsecs = (NSEC_PER_SEC / 2) / hz;
  151. if (cs->nsecs > (MAX_UDELAY_MS * NSEC_PER_MSEC))
  152. return -EINVAL;
  153. }
  154. return 0;
  155. }
  156. EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
  157. /*
  158. * spi_bitbang_setup - default setup for per-word I/O loops
  159. */
  160. int spi_bitbang_setup(struct spi_device *spi)
  161. {
  162. struct spi_bitbang_cs *cs = spi->controller_state;
  163. struct spi_bitbang *bitbang;
  164. bool initial_setup = false;
  165. int retval;
  166. bitbang = spi_controller_get_devdata(spi->controller);
  167. if (!cs) {
  168. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  169. if (!cs)
  170. return -ENOMEM;
  171. spi->controller_state = cs;
  172. initial_setup = true;
  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. retval = -EINVAL;
  178. goto err_free;
  179. }
  180. if (bitbang->setup_transfer) {
  181. retval = bitbang->setup_transfer(spi, NULL);
  182. if (retval < 0)
  183. goto err_free;
  184. }
  185. if (bitbang->set_mosi_idle)
  186. bitbang->set_mosi_idle(spi);
  187. dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
  188. return 0;
  189. err_free:
  190. if (initial_setup)
  191. kfree(cs);
  192. return retval;
  193. }
  194. EXPORT_SYMBOL_GPL(spi_bitbang_setup);
  195. /*
  196. * spi_bitbang_cleanup - default cleanup for per-word I/O loops
  197. */
  198. void spi_bitbang_cleanup(struct spi_device *spi)
  199. {
  200. kfree(spi->controller_state);
  201. }
  202. EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
  203. static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
  204. {
  205. struct spi_bitbang_cs *cs = spi->controller_state;
  206. unsigned int nsecs = cs->nsecs;
  207. struct spi_bitbang *bitbang;
  208. bitbang = spi_controller_get_devdata(spi->controller);
  209. if (bitbang->set_line_direction) {
  210. int err;
  211. err = bitbang->set_line_direction(spi, !!(t->tx_buf));
  212. if (err < 0)
  213. return err;
  214. }
  215. if (spi->mode & SPI_3WIRE) {
  216. unsigned int flags;
  217. flags = t->tx_buf ? SPI_CONTROLLER_NO_RX : SPI_CONTROLLER_NO_TX;
  218. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
  219. }
  220. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
  221. }
  222. /*----------------------------------------------------------------------*/
  223. /*
  224. * SECOND PART ... simple transfer queue runner.
  225. *
  226. * This costs a task context per controller, running the queue by
  227. * performing each transfer in sequence. Smarter hardware can queue
  228. * several DMA transfers at once, and process several controller queues
  229. * in parallel; this driver doesn't match such hardware very well.
  230. *
  231. * Drivers can provide word-at-a-time i/o primitives, or provide
  232. * transfer-at-a-time ones to leverage dma or fifo hardware.
  233. */
  234. static int spi_bitbang_prepare_hardware(struct spi_controller *spi)
  235. {
  236. struct spi_bitbang *bitbang;
  237. bitbang = spi_controller_get_devdata(spi);
  238. mutex_lock(&bitbang->lock);
  239. bitbang->busy = 1;
  240. mutex_unlock(&bitbang->lock);
  241. return 0;
  242. }
  243. static int spi_bitbang_transfer_one(struct spi_controller *ctlr,
  244. struct spi_device *spi,
  245. struct spi_transfer *transfer)
  246. {
  247. struct spi_bitbang *bitbang = spi_controller_get_devdata(ctlr);
  248. int status = 0;
  249. if (bitbang->setup_transfer) {
  250. status = bitbang->setup_transfer(spi, transfer);
  251. if (status < 0)
  252. goto out;
  253. }
  254. if (transfer->len)
  255. status = bitbang->txrx_bufs(spi, transfer);
  256. if (status == transfer->len)
  257. status = 0;
  258. else if (status >= 0)
  259. status = -EREMOTEIO;
  260. out:
  261. spi_finalize_current_transfer(ctlr);
  262. return status;
  263. }
  264. static int spi_bitbang_unprepare_hardware(struct spi_controller *spi)
  265. {
  266. struct spi_bitbang *bitbang;
  267. bitbang = spi_controller_get_devdata(spi);
  268. mutex_lock(&bitbang->lock);
  269. bitbang->busy = 0;
  270. mutex_unlock(&bitbang->lock);
  271. return 0;
  272. }
  273. static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
  274. {
  275. struct spi_bitbang *bitbang = spi_controller_get_devdata(spi->controller);
  276. /* SPI core provides CS high / low, but bitbang driver
  277. * expects CS active
  278. * spi device driver takes care of handling SPI_CS_HIGH
  279. */
  280. enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
  281. ndelay(SPI_BITBANG_CS_DELAY);
  282. bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
  283. BITBANG_CS_INACTIVE);
  284. ndelay(SPI_BITBANG_CS_DELAY);
  285. }
  286. /*----------------------------------------------------------------------*/
  287. int spi_bitbang_init(struct spi_bitbang *bitbang)
  288. {
  289. struct spi_controller *ctlr = bitbang->ctlr;
  290. bool custom_cs;
  291. if (!ctlr)
  292. return -EINVAL;
  293. /*
  294. * We only need the chipselect callback if we are actually using it.
  295. * If we just use GPIO descriptors, it is surplus. If the
  296. * SPI_CONTROLLER_GPIO_SS flag is set, we always need to call the
  297. * driver-specific chipselect routine.
  298. */
  299. custom_cs = (!ctlr->use_gpio_descriptors ||
  300. (ctlr->flags & SPI_CONTROLLER_GPIO_SS));
  301. if (custom_cs && !bitbang->chipselect)
  302. return -EINVAL;
  303. mutex_init(&bitbang->lock);
  304. if (!ctlr->mode_bits)
  305. ctlr->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  306. if (ctlr->transfer || ctlr->transfer_one_message)
  307. return -EINVAL;
  308. ctlr->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
  309. ctlr->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
  310. ctlr->transfer_one = spi_bitbang_transfer_one;
  311. /*
  312. * When using GPIO descriptors, the ->set_cs() callback doesn't even
  313. * get called unless SPI_CONTROLLER_GPIO_SS is set.
  314. */
  315. if (custom_cs)
  316. ctlr->set_cs = spi_bitbang_set_cs;
  317. if (!bitbang->txrx_bufs) {
  318. bitbang->use_dma = 0;
  319. bitbang->txrx_bufs = spi_bitbang_bufs;
  320. if (!ctlr->setup) {
  321. if (!bitbang->setup_transfer)
  322. bitbang->setup_transfer =
  323. spi_bitbang_setup_transfer;
  324. ctlr->setup = spi_bitbang_setup;
  325. ctlr->cleanup = spi_bitbang_cleanup;
  326. }
  327. }
  328. return 0;
  329. }
  330. EXPORT_SYMBOL_GPL(spi_bitbang_init);
  331. /**
  332. * spi_bitbang_start - start up a polled/bitbanging SPI host controller driver
  333. * @bitbang: driver handle
  334. *
  335. * Caller should have zero-initialized all parts of the structure, and then
  336. * provided callbacks for chip selection and I/O loops. If the host controller has
  337. * a transfer method, its final step should call spi_bitbang_transfer(); or,
  338. * that's the default if the transfer routine is not initialized. It should
  339. * also set up the bus number and number of chipselects.
  340. *
  341. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  342. * hardware that basically exposes a shift register) or per-spi_transfer
  343. * (which takes better advantage of hardware like fifos or DMA engines).
  344. *
  345. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup(),
  346. * spi_bitbang_cleanup() and spi_bitbang_setup_transfer() to handle those SPI
  347. * host controller methods. Those methods are the defaults if the bitbang->txrx_bufs
  348. * routine isn't initialized.
  349. *
  350. * This routine registers the spi_controller, which will process requests in a
  351. * dedicated task, keeping IRQs unblocked most of the time. To stop
  352. * processing those requests, call spi_bitbang_stop().
  353. *
  354. * On success, this routine will take a reference to the controller. The caller
  355. * is responsible for calling spi_bitbang_stop() to decrement the reference and
  356. * spi_controller_put() as counterpart of spi_alloc_host() to prevent a memory
  357. * leak.
  358. */
  359. int spi_bitbang_start(struct spi_bitbang *bitbang)
  360. {
  361. struct spi_controller *ctlr = bitbang->ctlr;
  362. int ret;
  363. ret = spi_bitbang_init(bitbang);
  364. if (ret)
  365. return ret;
  366. /* driver may get busy before register() returns, especially
  367. * if someone registered boardinfo for devices
  368. */
  369. ret = spi_register_controller(spi_controller_get(ctlr));
  370. if (ret)
  371. spi_controller_put(ctlr);
  372. return ret;
  373. }
  374. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  375. /*
  376. * spi_bitbang_stop - stops the task providing spi communication
  377. */
  378. void spi_bitbang_stop(struct spi_bitbang *bitbang)
  379. {
  380. spi_unregister_controller(bitbang->ctlr);
  381. }
  382. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  383. MODULE_LICENSE("GPL");
  384. MODULE_DESCRIPTION("Utilities for Bitbanging SPI host controllers");