PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/spi/spi_bitbang.c

https://github.com/ssvb/linux-n810
C | 510 lines | 317 code | 71 blank | 122 comment | 65 complexity | b2da469cba9ac52797d63111c5dfee76 MD5 | raw file
  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/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 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 = spi->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 = spi->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 = spi->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. int retval;
  167. unsigned long flags;
  168. bitbang = spi_master_get_devdata(spi->master);
  169. if (!cs) {
  170. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  171. if (!cs)
  172. return -ENOMEM;
  173. spi->controller_state = cs;
  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. return -EINVAL;
  179. retval = bitbang->setup_transfer(spi, NULL);
  180. if (retval < 0)
  181. return retval;
  182. dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
  183. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  184. * setup, unless the hardware defaults cooperate to avoid confusion
  185. * between normal (active low) and inverted chipselects.
  186. */
  187. /* deselect chip (low or high) */
  188. spin_lock_irqsave(&bitbang->lock, flags);
  189. if (!bitbang->busy) {
  190. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  191. ndelay(cs->nsecs);
  192. }
  193. spin_unlock_irqrestore(&bitbang->lock, flags);
  194. return 0;
  195. }
  196. EXPORT_SYMBOL_GPL(spi_bitbang_setup);
  197. /**
  198. * spi_bitbang_cleanup - default cleanup for per-word I/O loops
  199. */
  200. void spi_bitbang_cleanup(struct spi_device *spi)
  201. {
  202. kfree(spi->controller_state);
  203. }
  204. EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
  205. static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
  206. {
  207. struct spi_bitbang_cs *cs = spi->controller_state;
  208. unsigned nsecs = cs->nsecs;
  209. return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
  210. }
  211. /*----------------------------------------------------------------------*/
  212. /*
  213. * SECOND PART ... simple transfer queue runner.
  214. *
  215. * This costs a task context per controller, running the queue by
  216. * performing each transfer in sequence. Smarter hardware can queue
  217. * several DMA transfers at once, and process several controller queues
  218. * in parallel; this driver doesn't match such hardware very well.
  219. *
  220. * Drivers can provide word-at-a-time i/o primitives, or provide
  221. * transfer-at-a-time ones to leverage dma or fifo hardware.
  222. */
  223. static void bitbang_work(struct work_struct *work)
  224. {
  225. struct spi_bitbang *bitbang =
  226. container_of(work, struct spi_bitbang, work);
  227. unsigned long flags;
  228. int (*setup_transfer)(struct spi_device *,
  229. struct spi_transfer *);
  230. setup_transfer = bitbang->setup_transfer;
  231. spin_lock_irqsave(&bitbang->lock, flags);
  232. bitbang->busy = 1;
  233. while (!list_empty(&bitbang->queue)) {
  234. struct spi_message *m;
  235. struct spi_device *spi;
  236. unsigned nsecs;
  237. struct spi_transfer *t = NULL;
  238. unsigned tmp;
  239. unsigned cs_change;
  240. int status;
  241. int do_setup = -1;
  242. m = container_of(bitbang->queue.next, struct spi_message,
  243. queue);
  244. list_del_init(&m->queue);
  245. spin_unlock_irqrestore(&bitbang->lock, flags);
  246. /* FIXME this is made-up ... the correct value is known to
  247. * word-at-a-time bitbang code, and presumably chipselect()
  248. * should enforce these requirements too?
  249. */
  250. nsecs = 100;
  251. spi = m->spi;
  252. tmp = 0;
  253. cs_change = 1;
  254. status = 0;
  255. list_for_each_entry (t, &m->transfers, transfer_list) {
  256. /* override speed or wordsize? */
  257. if (t->speed_hz || t->bits_per_word)
  258. do_setup = 1;
  259. /* init (-1) or override (1) transfer params */
  260. if (do_setup != 0) {
  261. if (!setup_transfer) {
  262. status = -ENOPROTOOPT;
  263. break;
  264. }
  265. status = setup_transfer(spi, t);
  266. if (status < 0)
  267. break;
  268. if (do_setup == -1)
  269. do_setup = 0;
  270. }
  271. /* set up default clock polarity, and activate chip;
  272. * this implicitly updates clock and spi modes as
  273. * previously recorded for this device via setup().
  274. * (and also deselects any other chip that might be
  275. * selected ...)
  276. */
  277. if (cs_change) {
  278. bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
  279. ndelay(nsecs);
  280. }
  281. cs_change = t->cs_change;
  282. if (!t->tx_buf && !t->rx_buf && t->len) {
  283. status = -EINVAL;
  284. break;
  285. }
  286. /* transfer data. the lower level code handles any
  287. * new dma mappings it needs. our caller always gave
  288. * us dma-safe buffers.
  289. */
  290. if (t->len) {
  291. /* REVISIT dma API still needs a designated
  292. * DMA_ADDR_INVALID; ~0 might be better.
  293. */
  294. if (!m->is_dma_mapped)
  295. t->rx_dma = t->tx_dma = 0;
  296. status = bitbang->txrx_bufs(spi, t);
  297. }
  298. if (status > 0)
  299. m->actual_length += status;
  300. if (status != t->len) {
  301. /* always report some kind of error */
  302. if (status >= 0)
  303. status = -EREMOTEIO;
  304. break;
  305. }
  306. status = 0;
  307. /* protocol tweaks before next transfer */
  308. if (t->delay_usecs)
  309. udelay(t->delay_usecs);
  310. if (!cs_change)
  311. continue;
  312. if (t->transfer_list.next == &m->transfers)
  313. break;
  314. /* sometimes a short mid-message deselect of the chip
  315. * may be needed to terminate a mode or command
  316. */
  317. ndelay(nsecs);
  318. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  319. ndelay(nsecs);
  320. }
  321. m->status = status;
  322. m->complete(m->context);
  323. /* normally deactivate chipselect ... unless no error and
  324. * cs_change has hinted that the next message will probably
  325. * be for this chip too.
  326. */
  327. if (!(status == 0 && cs_change)) {
  328. ndelay(nsecs);
  329. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  330. ndelay(nsecs);
  331. }
  332. spin_lock_irqsave(&bitbang->lock, flags);
  333. }
  334. bitbang->busy = 0;
  335. spin_unlock_irqrestore(&bitbang->lock, flags);
  336. }
  337. /**
  338. * spi_bitbang_transfer - default submit to transfer queue
  339. */
  340. int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
  341. {
  342. struct spi_bitbang *bitbang;
  343. unsigned long flags;
  344. int status = 0;
  345. m->actual_length = 0;
  346. m->status = -EINPROGRESS;
  347. bitbang = spi_master_get_devdata(spi->master);
  348. spin_lock_irqsave(&bitbang->lock, flags);
  349. if (!spi->max_speed_hz)
  350. status = -ENETDOWN;
  351. else {
  352. list_add_tail(&m->queue, &bitbang->queue);
  353. queue_work(bitbang->workqueue, &bitbang->work);
  354. }
  355. spin_unlock_irqrestore(&bitbang->lock, flags);
  356. return status;
  357. }
  358. EXPORT_SYMBOL_GPL(spi_bitbang_transfer);
  359. /*----------------------------------------------------------------------*/
  360. /**
  361. * spi_bitbang_start - start up a polled/bitbanging SPI master driver
  362. * @bitbang: driver handle
  363. *
  364. * Caller should have zero-initialized all parts of the structure, and then
  365. * provided callbacks for chip selection and I/O loops. If the master has
  366. * a transfer method, its final step should call spi_bitbang_transfer; or,
  367. * that's the default if the transfer routine is not initialized. It should
  368. * also set up the bus number and number of chipselects.
  369. *
  370. * For i/o loops, provide callbacks either per-word (for bitbanging, or for
  371. * hardware that basically exposes a shift register) or per-spi_transfer
  372. * (which takes better advantage of hardware like fifos or DMA engines).
  373. *
  374. * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
  375. * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
  376. * master methods. Those methods are the defaults if the bitbang->txrx_bufs
  377. * routine isn't initialized.
  378. *
  379. * This routine registers the spi_master, which will process requests in a
  380. * dedicated task, keeping IRQs unblocked most of the time. To stop
  381. * processing those requests, call spi_bitbang_stop().
  382. */
  383. int spi_bitbang_start(struct spi_bitbang *bitbang)
  384. {
  385. int status;
  386. if (!bitbang->master || !bitbang->chipselect)
  387. return -EINVAL;
  388. INIT_WORK(&bitbang->work, bitbang_work);
  389. spin_lock_init(&bitbang->lock);
  390. INIT_LIST_HEAD(&bitbang->queue);
  391. if (!bitbang->master->mode_bits)
  392. bitbang->master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
  393. if (!bitbang->master->transfer)
  394. bitbang->master->transfer = spi_bitbang_transfer;
  395. if (!bitbang->txrx_bufs) {
  396. bitbang->use_dma = 0;
  397. bitbang->txrx_bufs = spi_bitbang_bufs;
  398. if (!bitbang->master->setup) {
  399. if (!bitbang->setup_transfer)
  400. bitbang->setup_transfer =
  401. spi_bitbang_setup_transfer;
  402. bitbang->master->setup = spi_bitbang_setup;
  403. bitbang->master->cleanup = spi_bitbang_cleanup;
  404. }
  405. } else if (!bitbang->master->setup)
  406. return -EINVAL;
  407. /* this task is the only thing to touch the SPI bits */
  408. bitbang->busy = 0;
  409. bitbang->workqueue = create_singlethread_workqueue(
  410. dev_name(bitbang->master->dev.parent));
  411. if (bitbang->workqueue == NULL) {
  412. status = -EBUSY;
  413. goto err1;
  414. }
  415. /* driver may get busy before register() returns, especially
  416. * if someone registered boardinfo for devices
  417. */
  418. status = spi_register_master(bitbang->master);
  419. if (status < 0)
  420. goto err2;
  421. return status;
  422. err2:
  423. destroy_workqueue(bitbang->workqueue);
  424. err1:
  425. return status;
  426. }
  427. EXPORT_SYMBOL_GPL(spi_bitbang_start);
  428. /**
  429. * spi_bitbang_stop - stops the task providing spi communication
  430. */
  431. int spi_bitbang_stop(struct spi_bitbang *bitbang)
  432. {
  433. spi_unregister_master(bitbang->master);
  434. WARN_ON(!list_empty(&bitbang->queue));
  435. destroy_workqueue(bitbang->workqueue);
  436. return 0;
  437. }
  438. EXPORT_SYMBOL_GPL(spi_bitbang_stop);
  439. MODULE_LICENSE("GPL");