PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/spi/spi-bitbang.c

https://gitlab.com/jhalayashraj/nkernel
C | 506 lines | 314 code | 70 blank | 122 comment | 66 complexity | b011a98b816efae0bc44744c07a4f72e MD5 | raw file
  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/init.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/spi_bitbang.h>
  29. /*----------------------------------------------------------------------*/
  30. /*
  31. * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
  32. * Use this for GPIO or shift-register level hardware APIs.
  33. *
  34. * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
  35. * to glue code. These bitbang setup() and cleanup() routines are always
  36. * used, though maybe they're called from controller-aware code.
  37. *
  38. * chipselect() and friends may use use spi_device->controller_data and
  39. * controller registers as appropriate.
  40. *
  41. *
  42. * NOTE: SPI controller pins can often be used as GPIO pins instead,
  43. * which means you could use a bitbang driver either to get hardware
  44. * working quickly, or testing for differences that aren't speed related.
  45. */
  46. struct spi_bitbang_cs {
  47. unsigned nsecs; /* (clock cycle time)/2 */
  48. u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
  49. u32 word, u8 bits);
  50. unsigned (*txrx_bufs)(struct spi_device *,
  51. u32 (*txrx_word)(
  52. struct spi_device *spi,
  53. unsigned nsecs,
  54. u32 word, u8 bits),
  55. unsigned, struct spi_transfer *);
  56. };
  57. static unsigned bitbang_txrx_8(
  58. struct spi_device *spi,
  59. u32 (*txrx_word)(struct spi_device *spi,
  60. unsigned nsecs,
  61. u32 word, u8 bits),
  62. unsigned ns,
  63. struct spi_transfer *t
  64. ) {
  65. unsigned bits = t->bits_per_word ? : spi->bits_per_word;
  66. unsigned count = t->len;
  67. const u8 *tx = t->tx_buf;
  68. u8 *rx = t->rx_buf;
  69. while (likely(count > 0)) {
  70. u8 word = 0;
  71. if (tx)
  72. word = *tx++;
  73. word = txrx_word(spi, ns, word, bits);
  74. if (rx)
  75. *rx++ = word;
  76. count -= 1;
  77. }
  78. return t->len - count;
  79. }
  80. static unsigned bitbang_txrx_16(
  81. struct spi_device *spi,
  82. u32 (*txrx_word)(struct spi_device *spi,
  83. unsigned nsecs,
  84. u32 word, u8 bits),
  85. unsigned ns,
  86. struct spi_transfer *t
  87. ) {
  88. unsigned bits = t->bits_per_word ? : spi->bits_per_word;
  89. unsigned count = t->len;
  90. const u16 *tx = t->tx_buf;
  91. u16 *rx = t->rx_buf;
  92. while (likely(count > 1)) {
  93. u16 word = 0;
  94. if (tx)
  95. word = *tx++;
  96. word = txrx_word(spi, ns, word, bits);
  97. if (rx)
  98. *rx++ = word;
  99. count -= 2;
  100. }
  101. return t->len - count;
  102. }
  103. static unsigned bitbang_txrx_32(
  104. struct spi_device *spi,
  105. u32 (*txrx_word)(struct spi_device *spi,
  106. unsigned nsecs,
  107. u32 word, u8 bits),
  108. unsigned ns,
  109. struct spi_transfer *t
  110. ) {
  111. unsigned bits = t->bits_per_word ? : spi->bits_per_word;
  112. unsigned count = t->len;
  113. const u32 *tx = t->tx_buf;
  114. u32 *rx = t->rx_buf;
  115. while (likely(count > 3)) {
  116. u32 word = 0;
  117. if (tx)
  118. word = *tx++;
  119. word = txrx_word(spi, ns, word, bits);
  120. if (rx)
  121. *rx++ = word;
  122. count -= 4;
  123. }
  124. return t->len - count;
  125. }
  126. int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  127. {
  128. struct spi_bitbang_cs *cs = spi->controller_state;
  129. u8 bits_per_word;
  130. u32 hz;
  131. if (t) {
  132. bits_per_word = t->bits_per_word;
  133. hz = t->speed_hz;
  134. } else {
  135. bits_per_word = 0;
  136. hz = 0;
  137. }
  138. /* spi_transfer level calls that work per-word */
  139. if (!bits_per_word)
  140. bits_per_word = spi->bits_per_word;
  141. if (bits_per_word <= 8)
  142. cs->txrx_bufs = bitbang_txrx_8;
  143. else if (bits_per_word <= 16)
  144. cs->txrx_bufs = bitbang_txrx_16;
  145. else if (bits_per_word <= 32)
  146. cs->txrx_bufs = bitbang_txrx_32;
  147. else
  148. return -EINVAL;
  149. /* nsecs = (clock period)/2 */
  150. if (!hz)
  151. hz = spi->max_speed_hz;
  152. if (hz) {
  153. cs->nsecs = (1000000000/2) / hz;
  154. if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
  155. return -EINVAL;
  156. }
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
  160. /**
  161. * spi_bitbang_setup - default setup for per-word I/O loops
  162. */
  163. int spi_bitbang_setup(struct spi_device *spi)
  164. {
  165. struct spi_bitbang_cs *cs = spi->controller_state;
  166. struct spi_bitbang *bitbang;
  167. int retval;
  168. unsigned long flags;
  169. bitbang = spi_master_get_devdata(spi->master);
  170. if (!cs) {
  171. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  172. if (!cs)
  173. return -ENOMEM;
  174. spi->controller_state = cs;
  175. }
  176. /* per-word shift register access, in hardware or bitbanging */
  177. cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
  178. if (!cs->txrx_word)
  179. return -EINVAL;
  180. retval = bitbang->setup_transfer(spi, NULL);
  181. if (retval < 0)
  182. return retval;
  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 void bitbang_work(struct work_struct *work)
  225. {
  226. struct spi_bitbang *bitbang =
  227. container_of(work, struct spi_bitbang, work);
  228. unsigned long flags;
  229. spin_lock_irqsave(&bitbang->lock, flags);
  230. bitbang->busy = 1;
  231. while (!list_empty(&bitbang->queue)) {
  232. struct spi_message *m;
  233. struct spi_device *spi;
  234. unsigned nsecs;
  235. struct spi_transfer *t = NULL;
  236. unsigned tmp;
  237. unsigned cs_change;
  238. int status;
  239. int do_setup = -1;
  240. m = container_of(bitbang->queue.next, struct spi_message,
  241. queue);
  242. list_del_init(&m->queue);
  243. spin_unlock_irqrestore(&bitbang->lock, flags);
  244. /* FIXME this is made-up ... the correct value is known to
  245. * word-at-a-time bitbang code, and presumably chipselect()
  246. * should enforce these requirements too?
  247. */
  248. nsecs = 100;
  249. spi = m->spi;
  250. tmp = 0;
  251. cs_change = 1;
  252. status = 0;
  253. list_for_each_entry (t, &m->transfers, transfer_list) {
  254. /* override speed or wordsize? */
  255. if (t->speed_hz || t->bits_per_word)
  256. do_setup = 1;
  257. /* init (-1) or override (1) transfer params */
  258. if (do_setup != 0) {
  259. status = bitbang->setup_transfer(spi, t);
  260. if (status < 0)
  261. break;
  262. if (do_setup == -1)
  263. do_setup = 0;
  264. }
  265. /* set up default clock polarity, and activate chip;
  266. * this implicitly updates clock and spi modes as
  267. * previously recorded for this device via setup().
  268. * (and also deselects any other chip that might be
  269. * selected ...)
  270. */
  271. if (cs_change) {
  272. bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
  273. ndelay(nsecs);
  274. }
  275. cs_change = t->cs_change;
  276. if (!t->tx_buf && !t->rx_buf && t->len) {
  277. status = -EINVAL;
  278. break;
  279. }
  280. /* transfer data. the lower level code handles any
  281. * new dma mappings it needs. our caller always gave
  282. * us dma-safe buffers.
  283. */
  284. if (t->len) {
  285. /* REVISIT dma API still needs a designated
  286. * DMA_ADDR_INVALID; ~0 might be better.
  287. */
  288. if (!m->is_dma_mapped)
  289. t->rx_dma = t->tx_dma = 0;
  290. status = bitbang->txrx_bufs(spi, t);
  291. }
  292. if (status > 0)
  293. m->actual_length += status;
  294. if (status != t->len) {
  295. /* always report some kind of error */
  296. if (status >= 0)
  297. status = -EREMOTEIO;
  298. break;
  299. }
  300. status = 0;
  301. /* protocol tweaks before next transfer */
  302. if (t->delay_usecs)
  303. udelay(t->delay_usecs);
  304. if (!cs_change)
  305. continue;
  306. if (t->transfer_list.next == &m->transfers)
  307. break;
  308. /* sometimes a short mid-message deselect of the chip
  309. * may be needed to terminate a mode or command
  310. */
  311. ndelay(nsecs);
  312. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  313. ndelay(nsecs);
  314. }
  315. m->status = status;
  316. m->complete(m->context);
  317. /* normally deactivate chipselect ... unless no error and
  318. * cs_change has hinted that the next message will probably
  319. * be for this chip too.
  320. */
  321. if (!(status == 0 && cs_change)) {
  322. ndelay(nsecs);
  323. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  324. ndelay(nsecs);
  325. }
  326. spin_lock_irqsave(&bitbang->lock, flags);
  327. }
  328. bitbang->busy = 0;
  329. spin_unlock_irqrestore(&bitbang->lock, flags);
  330. }
  331. /**
  332. * spi_bitbang_transfer - default submit to transfer queue
  333. */
  334. int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
  335. {
  336. struct spi_bitbang *bitbang;
  337. unsigned long flags;
  338. int status = 0;
  339. m->actual_length = 0;
  340. m->status = -EINPROGRESS;
  341. bitbang = spi_master_get_devdata(spi->master);
  342. spin_lock_irqsave(&bitbang->lock, flags);
  343. if (!spi->max_speed_hz)
  344. status = -ENETDOWN;
  345. else {
  346. list_add_tail(&m->queue, &bitbang->queue);
  347. queue_work(bitbang->workqueue, &bitbang->work);
  348. }
  349. spin_unlock_irqrestore(&bitbang->lock, flags);
  350. return status;
  351. }
  352. EXPORT_SYMBOL_GPL(spi_bitbang_transfer);
  353. /*----------------------------------------------------------------------*/
  354. /**
  355. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  356. * @bitbang: driver handle
  357. *
  358. * Caller should have zero-initialized all parts of the structure, and then
  359. * provided callbacks for chip selection and I/O loops. If the master has
  360. * a transfer method, its final step should call spi_bitbang_transfer; or,
  361. * that's the default if the transfer routine is not initialized. It should
  362. * also set up the bus number and number of chipselects.
  363. *
  364. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  365. * hardware that basically exposes a shift register) or per-spi_transfer
  366. * (which takes better advantage of hardware like fifos or DMA engines).
  367. *
  368. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
  369. * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
  370. * master methods. Those methods are the defaults if the bitbang->txrx_bufs
  371. * routine isn't initialized.
  372. *
  373. * This routine registers the spi_master, which will process requests in a
  374. * dedicated task, keeping IRQs unblocked most of the time. To stop
  375. * processing those requests, call spi_bitbang_stop().
  376. */
  377. int spi_bitbang_start(struct spi_bitbang *bitbang)
  378. {
  379. int status;
  380. if (!bitbang->master || !bitbang->chipselect)
  381. return -EINVAL;
  382. INIT_WORK(&bitbang->work, bitbang_work);
  383. spin_lock_init(&bitbang->lock);
  384. INIT_LIST_HEAD(&bitbang->queue);
  385. if (!bitbang->master->mode_bits)
  386. bitbang->master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  387. if (!bitbang->master->transfer)
  388. bitbang->master->transfer = spi_bitbang_transfer;
  389. if (!bitbang->txrx_bufs) {
  390. bitbang->use_dma = 0;
  391. bitbang->txrx_bufs = spi_bitbang_bufs;
  392. if (!bitbang->master->setup) {
  393. if (!bitbang->setup_transfer)
  394. bitbang->setup_transfer =
  395. spi_bitbang_setup_transfer;
  396. bitbang->master->setup = spi_bitbang_setup;
  397. bitbang->master->cleanup = spi_bitbang_cleanup;
  398. }
  399. } else if (!bitbang->master->setup)
  400. return -EINVAL;
  401. if (bitbang->master->transfer == spi_bitbang_transfer &&
  402. !bitbang->setup_transfer)
  403. return -EINVAL;
  404. /* this task is the only thing to touch the SPI bits */
  405. bitbang->busy = 0;
  406. bitbang->workqueue = create_singlethread_workqueue(
  407. dev_name(bitbang->master->dev.parent));
  408. if (bitbang->workqueue == NULL) {
  409. status = -EBUSY;
  410. goto err1;
  411. }
  412. /* driver may get busy before register() returns, especially
  413. * if someone registered boardinfo for devices
  414. */
  415. status = spi_register_master(bitbang->master);
  416. if (status < 0)
  417. goto err2;
  418. return status;
  419. err2:
  420. destroy_workqueue(bitbang->workqueue);
  421. err1:
  422. return status;
  423. }
  424. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  425. /**
  426. * spi_bitbang_stop - stops the task providing spi communication
  427. */
  428. int spi_bitbang_stop(struct spi_bitbang *bitbang)
  429. {
  430. spi_unregister_master(bitbang->master);
  431. WARN_ON(!list_empty(&bitbang->queue));
  432. destroy_workqueue(bitbang->workqueue);
  433. return 0;
  434. }
  435. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  436. MODULE_LICENSE("GPL");