blob: 06cf9388e74f96b7e6251197b92a3f3b4897be8d [file] [log] [blame]
David Brownell9904f222006-01-08 13:34:26 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * polling/bitbanging SPI master controller driver utilities
David Brownell9904f222006-01-08 13:34:26 -08003 *
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.
David Brownell9904f222006-01-08 13:34:26 -080013 */
14
David Brownell9904f222006-01-08 13:34:26 -080015#include <linux/spinlock.h>
16#include <linux/workqueue.h>
17#include <linux/interrupt.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040018#include <linux/module.h>
David Brownell9904f222006-01-08 13:34:26 -080019#include <linux/delay.h>
20#include <linux/errno.h>
21#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Brownell9904f222006-01-08 13:34:26 -080023
24#include <linux/spi/spi.h>
25#include <linux/spi/spi_bitbang.h>
26
Heiner Kallweit00376862015-09-29 23:15:53 +020027#define SPI_BITBANG_CS_DELAY 100
28
David Brownell9904f222006-01-08 13:34:26 -080029
30/*----------------------------------------------------------------------*/
31
32/*
33 * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
34 * Use this for GPIO or shift-register level hardware APIs.
35 *
36 * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
37 * to glue code. These bitbang setup() and cleanup() routines are always
38 * used, though maybe they're called from controller-aware code.
39 *
Uwe Kleine-König03ddcbc2013-08-07 21:22:20 +020040 * chipselect() and friends may use spi_device->controller_data and
David Brownell9904f222006-01-08 13:34:26 -080041 * controller registers as appropriate.
42 *
43 *
44 * NOTE: SPI controller pins can often be used as GPIO pins instead,
45 * which means you could use a bitbang driver either to get hardware
46 * working quickly, or testing for differences that aren't speed related.
47 */
48
49struct spi_bitbang_cs {
50 unsigned nsecs; /* (clock cycle time)/2 */
51 u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020052 u32 word, u8 bits, unsigned flags);
David Brownell9904f222006-01-08 13:34:26 -080053 unsigned (*txrx_bufs)(struct spi_device *,
54 u32 (*txrx_word)(
55 struct spi_device *spi,
56 unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020057 u32 word, u8 bits,
58 unsigned flags),
59 unsigned, struct spi_transfer *,
60 unsigned);
David Brownell9904f222006-01-08 13:34:26 -080061};
62
63static unsigned bitbang_txrx_8(
64 struct spi_device *spi,
65 u32 (*txrx_word)(struct spi_device *spi,
66 unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020067 u32 word, u8 bits,
68 unsigned flags),
David Brownell9904f222006-01-08 13:34:26 -080069 unsigned ns,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020070 struct spi_transfer *t,
71 unsigned flags
David Brownell9904f222006-01-08 13:34:26 -080072) {
Laxman Dewangan766ed702012-12-18 14:25:43 +053073 unsigned bits = t->bits_per_word;
David Brownell9904f222006-01-08 13:34:26 -080074 unsigned count = t->len;
75 const u8 *tx = t->tx_buf;
76 u8 *rx = t->rx_buf;
77
78 while (likely(count > 0)) {
79 u8 word = 0;
80
81 if (tx)
82 word = *tx++;
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020083 word = txrx_word(spi, ns, word, bits, flags);
David Brownell9904f222006-01-08 13:34:26 -080084 if (rx)
85 *rx++ = word;
86 count -= 1;
87 }
88 return t->len - count;
89}
90
91static unsigned bitbang_txrx_16(
92 struct spi_device *spi,
93 u32 (*txrx_word)(struct spi_device *spi,
94 unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020095 u32 word, u8 bits,
96 unsigned flags),
David Brownell9904f222006-01-08 13:34:26 -080097 unsigned ns,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020098 struct spi_transfer *t,
99 unsigned flags
David Brownell9904f222006-01-08 13:34:26 -0800100) {
Laxman Dewangan766ed702012-12-18 14:25:43 +0530101 unsigned bits = t->bits_per_word;
David Brownell9904f222006-01-08 13:34:26 -0800102 unsigned count = t->len;
103 const u16 *tx = t->tx_buf;
104 u16 *rx = t->rx_buf;
105
106 while (likely(count > 1)) {
107 u16 word = 0;
108
109 if (tx)
110 word = *tx++;
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200111 word = txrx_word(spi, ns, word, bits, flags);
David Brownell9904f222006-01-08 13:34:26 -0800112 if (rx)
113 *rx++ = word;
114 count -= 2;
115 }
116 return t->len - count;
117}
118
119static unsigned bitbang_txrx_32(
120 struct spi_device *spi,
121 u32 (*txrx_word)(struct spi_device *spi,
122 unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200123 u32 word, u8 bits,
124 unsigned flags),
David Brownell9904f222006-01-08 13:34:26 -0800125 unsigned ns,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200126 struct spi_transfer *t,
127 unsigned flags
David Brownell9904f222006-01-08 13:34:26 -0800128) {
Laxman Dewangan766ed702012-12-18 14:25:43 +0530129 unsigned bits = t->bits_per_word;
David Brownell9904f222006-01-08 13:34:26 -0800130 unsigned count = t->len;
131 const u32 *tx = t->tx_buf;
132 u32 *rx = t->rx_buf;
133
134 while (likely(count > 3)) {
135 u32 word = 0;
136
137 if (tx)
138 word = *tx++;
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200139 word = txrx_word(spi, ns, word, bits, flags);
David Brownell9904f222006-01-08 13:34:26 -0800140 if (rx)
141 *rx++ = word;
142 count -= 4;
143 }
144 return t->len - count;
145}
146
Kumar Galaff9f4772006-04-02 16:06:35 -0500147int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
Imre Deak4cff33f2006-02-17 10:02:18 -0800148{
149 struct spi_bitbang_cs *cs = spi->controller_state;
150 u8 bits_per_word;
151 u32 hz;
152
153 if (t) {
154 bits_per_word = t->bits_per_word;
155 hz = t->speed_hz;
156 } else {
157 bits_per_word = 0;
158 hz = 0;
159 }
160
161 /* spi_transfer level calls that work per-word */
162 if (!bits_per_word)
163 bits_per_word = spi->bits_per_word;
164 if (bits_per_word <= 8)
165 cs->txrx_bufs = bitbang_txrx_8;
166 else if (bits_per_word <= 16)
167 cs->txrx_bufs = bitbang_txrx_16;
168 else if (bits_per_word <= 32)
169 cs->txrx_bufs = bitbang_txrx_32;
170 else
171 return -EINVAL;
172
173 /* nsecs = (clock period)/2 */
174 if (!hz)
175 hz = spi->max_speed_hz;
David Brownell1e316d72006-04-06 22:25:56 -0700176 if (hz) {
177 cs->nsecs = (1000000000/2) / hz;
178 if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
179 return -EINVAL;
180 }
Imre Deak4cff33f2006-02-17 10:02:18 -0800181
182 return 0;
183}
Kumar Galaff9f4772006-04-02 16:06:35 -0500184EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
Imre Deak4cff33f2006-02-17 10:02:18 -0800185
David Brownell9904f222006-01-08 13:34:26 -0800186/**
187 * spi_bitbang_setup - default setup for per-word I/O loops
188 */
189int spi_bitbang_setup(struct spi_device *spi)
190{
191 struct spi_bitbang_cs *cs = spi->controller_state;
192 struct spi_bitbang *bitbang;
193
David Brownellccf77cc2006-04-03 15:46:22 -0700194 bitbang = spi_master_get_devdata(spi->master);
195
David Brownell9904f222006-01-08 13:34:26 -0800196 if (!cs) {
Jingoo Hancff93c52013-10-14 10:33:38 +0900197 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
David Brownell9904f222006-01-08 13:34:26 -0800198 if (!cs)
199 return -ENOMEM;
200 spi->controller_state = cs;
201 }
David Brownell9904f222006-01-08 13:34:26 -0800202
David Brownell9904f222006-01-08 13:34:26 -0800203 /* per-word shift register access, in hardware or bitbanging */
204 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
205 if (!cs->txrx_word)
206 return -EINVAL;
207
Pelle Nilsson7d0ec8b2015-04-14 15:40:17 +0200208 if (bitbang->setup_transfer) {
209 int retval = bitbang->setup_transfer(spi, NULL);
210 if (retval < 0)
211 return retval;
212 }
David Brownell9904f222006-01-08 13:34:26 -0800213
David Brownell7d077192009-06-17 16:26:03 -0700214 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
David Brownell9904f222006-01-08 13:34:26 -0800215
216 /* NOTE we _need_ to call chipselect() early, ideally with adapter
217 * setup, unless the hardware defaults cooperate to avoid confusion
218 * between normal (active low) and inverted chipselects.
219 */
220
221 /* deselect chip (low or high) */
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800222 mutex_lock(&bitbang->lock);
David Brownell9904f222006-01-08 13:34:26 -0800223 if (!bitbang->busy) {
Vitaly Wool8275c642006-01-08 13:34:28 -0800224 bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
David Brownell9904f222006-01-08 13:34:26 -0800225 ndelay(cs->nsecs);
226 }
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800227 mutex_unlock(&bitbang->lock);
David Brownell9904f222006-01-08 13:34:26 -0800228
229 return 0;
230}
231EXPORT_SYMBOL_GPL(spi_bitbang_setup);
232
233/**
234 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
235 */
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -0800236void spi_bitbang_cleanup(struct spi_device *spi)
David Brownell9904f222006-01-08 13:34:26 -0800237{
238 kfree(spi->controller_state);
239}
240EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
241
242static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
243{
244 struct spi_bitbang_cs *cs = spi->controller_state;
245 unsigned nsecs = cs->nsecs;
Lorenzo Bianconi4b859db2018-07-28 10:19:14 +0200246 struct spi_bitbang *bitbang;
David Brownell9904f222006-01-08 13:34:26 -0800247
Lorenzo Bianconi4b859db2018-07-28 10:19:14 +0200248 bitbang = spi_master_get_devdata(spi->master);
249 if (bitbang->set_line_direction) {
250 int err;
251
252 err = bitbang->set_line_direction(spi, !!(t->tx_buf));
253 if (err < 0)
254 return err;
255 }
256
257 if (spi->mode & SPI_3WIRE) {
258 unsigned flags;
259
260 flags = t->tx_buf ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX;
261 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
262 }
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200263 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
David Brownell9904f222006-01-08 13:34:26 -0800264}
265
266/*----------------------------------------------------------------------*/
267
268/*
269 * SECOND PART ... simple transfer queue runner.
270 *
271 * This costs a task context per controller, running the queue by
272 * performing each transfer in sequence. Smarter hardware can queue
273 * several DMA transfers at once, and process several controller queues
274 * in parallel; this driver doesn't match such hardware very well.
275 *
276 * Drivers can provide word-at-a-time i/o primitives, or provide
277 * transfer-at-a-time ones to leverage dma or fifo hardware.
278 */
Mark Brown20251722013-07-05 20:07:27 +0100279
280static int spi_bitbang_prepare_hardware(struct spi_master *spi)
281{
Jingoo Hancff93c52013-10-14 10:33:38 +0900282 struct spi_bitbang *bitbang;
Mark Brown20251722013-07-05 20:07:27 +0100283
284 bitbang = spi_master_get_devdata(spi);
285
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800286 mutex_lock(&bitbang->lock);
Mark Brown20251722013-07-05 20:07:27 +0100287 bitbang->busy = 1;
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800288 mutex_unlock(&bitbang->lock);
Mark Brown20251722013-07-05 20:07:27 +0100289
290 return 0;
291}
292
Fabio Estevamd60990d52013-07-17 15:49:54 -0300293static int spi_bitbang_transfer_one(struct spi_master *master,
Heiner Kallweit00376862015-09-29 23:15:53 +0200294 struct spi_device *spi,
295 struct spi_transfer *transfer)
Mark Brown91b30852013-07-05 12:06:44 +0100296{
Heiner Kallweit00376862015-09-29 23:15:53 +0200297 struct spi_bitbang *bitbang = spi_master_get_devdata(master);
298 int status = 0;
Mark Brown91b30852013-07-05 12:06:44 +0100299
Heiner Kallweit00376862015-09-29 23:15:53 +0200300 if (bitbang->setup_transfer) {
301 status = bitbang->setup_transfer(spi, transfer);
302 if (status < 0)
303 goto out;
304 }
Mark Brown91b30852013-07-05 12:06:44 +0100305
Heiner Kallweit00376862015-09-29 23:15:53 +0200306 if (transfer->len)
307 status = bitbang->txrx_bufs(spi, transfer);
Mark Brown91b30852013-07-05 12:06:44 +0100308
Heiner Kallweit00376862015-09-29 23:15:53 +0200309 if (status == transfer->len)
Mark Brown91b30852013-07-05 12:06:44 +0100310 status = 0;
Heiner Kallweit00376862015-09-29 23:15:53 +0200311 else if (status >= 0)
312 status = -EREMOTEIO;
Mark Brown91b30852013-07-05 12:06:44 +0100313
Heiner Kallweit00376862015-09-29 23:15:53 +0200314out:
315 spi_finalize_current_transfer(master);
Mark Brown20251722013-07-05 20:07:27 +0100316
Mark Brown91b30852013-07-05 12:06:44 +0100317 return status;
318}
319
Mark Brown20251722013-07-05 20:07:27 +0100320static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
David Brownell9904f222006-01-08 13:34:26 -0800321{
Jingoo Hancff93c52013-10-14 10:33:38 +0900322 struct spi_bitbang *bitbang;
Mark Brown20251722013-07-05 20:07:27 +0100323
324 bitbang = spi_master_get_devdata(spi);
David Brownell9904f222006-01-08 13:34:26 -0800325
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800326 mutex_lock(&bitbang->lock);
David Brownell9904f222006-01-08 13:34:26 -0800327 bitbang->busy = 0;
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800328 mutex_unlock(&bitbang->lock);
David Brownell9904f222006-01-08 13:34:26 -0800329
Mark Brown20251722013-07-05 20:07:27 +0100330 return 0;
David Brownell9904f222006-01-08 13:34:26 -0800331}
David Brownell9904f222006-01-08 13:34:26 -0800332
Heiner Kallweit00376862015-09-29 23:15:53 +0200333static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
334{
335 struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
336
337 /* SPI core provides CS high / low, but bitbang driver
338 * expects CS active
339 * spi device driver takes care of handling SPI_CS_HIGH
340 */
341 enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
342
343 ndelay(SPI_BITBANG_CS_DELAY);
344 bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
345 BITBANG_CS_INACTIVE);
346 ndelay(SPI_BITBANG_CS_DELAY);
347}
348
David Brownell9904f222006-01-08 13:34:26 -0800349/*----------------------------------------------------------------------*/
350
351/**
352 * spi_bitbang_start - start up a polled/bitbanging SPI master driver
353 * @bitbang: driver handle
354 *
355 * Caller should have zero-initialized all parts of the structure, and then
356 * provided callbacks for chip selection and I/O loops. If the master has
357 * a transfer method, its final step should call spi_bitbang_transfer; or,
358 * that's the default if the transfer routine is not initialized. It should
359 * also set up the bus number and number of chipselects.
360 *
361 * For i/o loops, provide callbacks either per-word (for bitbanging, or for
362 * hardware that basically exposes a shift register) or per-spi_transfer
363 * (which takes better advantage of hardware like fifos or DMA engines).
364 *
Hans-Peter Nilsson7f8c7612007-02-12 00:52:44 -0800365 * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
366 * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
367 * master methods. Those methods are the defaults if the bitbang->txrx_bufs
368 * routine isn't initialized.
David Brownell9904f222006-01-08 13:34:26 -0800369 *
370 * This routine registers the spi_master, which will process requests in a
371 * dedicated task, keeping IRQs unblocked most of the time. To stop
372 * processing those requests, call spi_bitbang_stop().
Axel Lin702a4872013-09-10 15:43:41 +0800373 *
374 * On success, this routine will take a reference to master. The caller is
375 * responsible for calling spi_bitbang_stop() to decrement the reference and
376 * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
377 * leak.
David Brownell9904f222006-01-08 13:34:26 -0800378 */
379int spi_bitbang_start(struct spi_bitbang *bitbang)
380{
Guennadi Liakhovetski7a5d8ca2013-01-10 13:13:56 +0100381 struct spi_master *master = bitbang->master;
Axel Lin702a4872013-09-10 15:43:41 +0800382 int ret;
David Brownell9904f222006-01-08 13:34:26 -0800383
Guennadi Liakhovetski7a5d8ca2013-01-10 13:13:56 +0100384 if (!master || !bitbang->chipselect)
David Brownell9904f222006-01-08 13:34:26 -0800385 return -EINVAL;
386
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800387 mutex_init(&bitbang->lock);
David Brownell9904f222006-01-08 13:34:26 -0800388
Guennadi Liakhovetski7a5d8ca2013-01-10 13:13:56 +0100389 if (!master->mode_bits)
390 master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
David Brownelle7db06b2009-06-17 16:26:04 -0700391
Mark Brown20251722013-07-05 20:07:27 +0100392 if (master->transfer || master->transfer_one_message)
393 return -EINVAL;
394
395 master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
396 master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
Heiner Kallweit00376862015-09-29 23:15:53 +0200397 master->transfer_one = spi_bitbang_transfer_one;
398 master->set_cs = spi_bitbang_set_cs;
Mark Brown20251722013-07-05 20:07:27 +0100399
David Brownell9904f222006-01-08 13:34:26 -0800400 if (!bitbang->txrx_bufs) {
401 bitbang->use_dma = 0;
402 bitbang->txrx_bufs = spi_bitbang_bufs;
Guennadi Liakhovetski7a5d8ca2013-01-10 13:13:56 +0100403 if (!master->setup) {
Kumar Galaff9f4772006-04-02 16:06:35 -0500404 if (!bitbang->setup_transfer)
405 bitbang->setup_transfer =
406 spi_bitbang_setup_transfer;
Guennadi Liakhovetski7a5d8ca2013-01-10 13:13:56 +0100407 master->setup = spi_bitbang_setup;
408 master->cleanup = spi_bitbang_cleanup;
David Brownell9904f222006-01-08 13:34:26 -0800409 }
Uwe Kleine-König52ade732013-08-08 16:09:49 +0200410 }
David Brownell9904f222006-01-08 13:34:26 -0800411
412 /* driver may get busy before register() returns, especially
413 * if someone registered boardinfo for devices
414 */
Axel Lin702a4872013-09-10 15:43:41 +0800415 ret = spi_register_master(spi_master_get(master));
416 if (ret)
417 spi_master_put(master);
418
YueHaibingdbd94f42019-05-16 15:56:56 +0800419 return ret;
David Brownell9904f222006-01-08 13:34:26 -0800420}
421EXPORT_SYMBOL_GPL(spi_bitbang_start);
422
423/**
424 * spi_bitbang_stop - stops the task providing spi communication
425 */
Axel Lind9721ae2014-03-29 18:50:12 +0800426void spi_bitbang_stop(struct spi_bitbang *bitbang)
David Brownell9904f222006-01-08 13:34:26 -0800427{
Chris Lesiaka836f582007-03-16 13:38:13 -0800428 spi_unregister_master(bitbang->master);
David Brownell9904f222006-01-08 13:34:26 -0800429}
430EXPORT_SYMBOL_GPL(spi_bitbang_stop);
431
432MODULE_LICENSE("GPL");
433