spi-bitbang.c 11 KB

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