2 * polling/bitbanging SPI master controller driver utilities
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.
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.
15 #include <linux/spinlock.h>
16 #include <linux/workqueue.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/errno.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/spi_bitbang.h>
27 #define SPI_BITBANG_CS_DELAY 100
30 /*----------------------------------------------------------------------*/
33 * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
34 * Use this for GPIO or shift-register level hardware APIs.
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.
40 * chipselect() and friends may use spi_device->controller_data and
41 * controller registers as appropriate.
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.
49 struct spi_bitbang_cs
{
50 unsigned nsecs
; /* (clock cycle time)/2 */
51 u32 (*txrx_word
)(struct spi_device
*spi
, unsigned nsecs
,
52 u32 word
, u8 bits
, unsigned flags
);
53 unsigned (*txrx_bufs
)(struct spi_device
*,
55 struct spi_device
*spi
,
59 unsigned, struct spi_transfer
*,
63 static unsigned bitbang_txrx_8(
64 struct spi_device
*spi
,
65 u32 (*txrx_word
)(struct spi_device
*spi
,
70 struct spi_transfer
*t
,
73 unsigned bits
= t
->bits_per_word
;
74 unsigned count
= t
->len
;
75 const u8
*tx
= t
->tx_buf
;
78 while (likely(count
> 0)) {
83 word
= txrx_word(spi
, ns
, word
, bits
, flags
);
88 return t
->len
- count
;
91 static unsigned bitbang_txrx_16(
92 struct spi_device
*spi
,
93 u32 (*txrx_word
)(struct spi_device
*spi
,
98 struct spi_transfer
*t
,
101 unsigned bits
= t
->bits_per_word
;
102 unsigned count
= t
->len
;
103 const u16
*tx
= t
->tx_buf
;
106 while (likely(count
> 1)) {
111 word
= txrx_word(spi
, ns
, word
, bits
, flags
);
116 return t
->len
- count
;
119 static unsigned bitbang_txrx_32(
120 struct spi_device
*spi
,
121 u32 (*txrx_word
)(struct spi_device
*spi
,
126 struct spi_transfer
*t
,
129 unsigned bits
= t
->bits_per_word
;
130 unsigned count
= t
->len
;
131 const u32
*tx
= t
->tx_buf
;
134 while (likely(count
> 3)) {
139 word
= txrx_word(spi
, ns
, word
, bits
, flags
);
144 return t
->len
- count
;
147 int spi_bitbang_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
149 struct spi_bitbang_cs
*cs
= spi
->controller_state
;
154 bits_per_word
= t
->bits_per_word
;
161 /* spi_transfer level calls that work 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
;
173 /* nsecs = (clock period)/2 */
175 hz
= spi
->max_speed_hz
;
177 cs
->nsecs
= (1000000000/2) / hz
;
178 if (cs
->nsecs
> (MAX_UDELAY_MS
* 1000 * 1000))
184 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer
);
187 * spi_bitbang_setup - default setup for per-word I/O loops
189 int spi_bitbang_setup(struct spi_device
*spi
)
191 struct spi_bitbang_cs
*cs
= spi
->controller_state
;
192 struct spi_bitbang
*bitbang
;
194 bitbang
= spi_master_get_devdata(spi
->master
);
197 cs
= kzalloc(sizeof(*cs
), GFP_KERNEL
);
200 spi
->controller_state
= cs
;
203 /* per-word shift register access, in hardware or bitbanging */
204 cs
->txrx_word
= bitbang
->txrx_word
[spi
->mode
& (SPI_CPOL
|SPI_CPHA
)];
208 if (bitbang
->setup_transfer
) {
209 int retval
= bitbang
->setup_transfer(spi
, NULL
);
214 dev_dbg(&spi
->dev
, "%s, %u nsec/bit\n", __func__
, 2 * cs
->nsecs
);
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.
221 /* deselect chip (low or high) */
222 mutex_lock(&bitbang
->lock
);
223 if (!bitbang
->busy
) {
224 bitbang
->chipselect(spi
, BITBANG_CS_INACTIVE
);
227 mutex_unlock(&bitbang
->lock
);
231 EXPORT_SYMBOL_GPL(spi_bitbang_setup
);
234 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
236 void spi_bitbang_cleanup(struct spi_device
*spi
)
238 kfree(spi
->controller_state
);
240 EXPORT_SYMBOL_GPL(spi_bitbang_cleanup
);
242 static int spi_bitbang_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
244 struct spi_bitbang_cs
*cs
= spi
->controller_state
;
245 unsigned nsecs
= cs
->nsecs
;
246 struct spi_bitbang
*bitbang
;
248 bitbang
= spi_master_get_devdata(spi
->master
);
249 if (bitbang
->set_line_direction
) {
252 err
= bitbang
->set_line_direction(spi
, !!(t
->tx_buf
));
257 if (spi
->mode
& SPI_3WIRE
) {
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
);
263 return cs
->txrx_bufs(spi
, cs
->txrx_word
, nsecs
, t
, 0);
266 /*----------------------------------------------------------------------*/
269 * SECOND PART ... simple transfer queue runner.
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.
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.
280 static int spi_bitbang_prepare_hardware(struct spi_master
*spi
)
282 struct spi_bitbang
*bitbang
;
284 bitbang
= spi_master_get_devdata(spi
);
286 mutex_lock(&bitbang
->lock
);
288 mutex_unlock(&bitbang
->lock
);
293 static int spi_bitbang_transfer_one(struct spi_master
*master
,
294 struct spi_device
*spi
,
295 struct spi_transfer
*transfer
)
297 struct spi_bitbang
*bitbang
= spi_master_get_devdata(master
);
300 if (bitbang
->setup_transfer
) {
301 status
= bitbang
->setup_transfer(spi
, transfer
);
307 status
= bitbang
->txrx_bufs(spi
, transfer
);
309 if (status
== transfer
->len
)
311 else if (status
>= 0)
315 spi_finalize_current_transfer(master
);
320 static int spi_bitbang_unprepare_hardware(struct spi_master
*spi
)
322 struct spi_bitbang
*bitbang
;
324 bitbang
= spi_master_get_devdata(spi
);
326 mutex_lock(&bitbang
->lock
);
328 mutex_unlock(&bitbang
->lock
);
333 static void spi_bitbang_set_cs(struct spi_device
*spi
, bool enable
)
335 struct spi_bitbang
*bitbang
= spi_master_get_devdata(spi
->master
);
337 /* SPI core provides CS high / low, but bitbang driver
339 * spi device driver takes care of handling SPI_CS_HIGH
341 enable
= (!!(spi
->mode
& SPI_CS_HIGH
) == enable
);
343 ndelay(SPI_BITBANG_CS_DELAY
);
344 bitbang
->chipselect(spi
, enable
? BITBANG_CS_ACTIVE
:
345 BITBANG_CS_INACTIVE
);
346 ndelay(SPI_BITBANG_CS_DELAY
);
349 /*----------------------------------------------------------------------*/
352 * spi_bitbang_start - start up a polled/bitbanging SPI master driver
353 * @bitbang: driver handle
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.
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).
365 * 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.
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().
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
379 int spi_bitbang_start(struct spi_bitbang
*bitbang
)
381 struct spi_master
*master
= bitbang
->master
;
384 if (!master
|| !bitbang
->chipselect
)
387 mutex_init(&bitbang
->lock
);
389 if (!master
->mode_bits
)
390 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| bitbang
->flags
;
392 if (master
->transfer
|| master
->transfer_one_message
)
395 master
->prepare_transfer_hardware
= spi_bitbang_prepare_hardware
;
396 master
->unprepare_transfer_hardware
= spi_bitbang_unprepare_hardware
;
397 master
->transfer_one
= spi_bitbang_transfer_one
;
398 master
->set_cs
= spi_bitbang_set_cs
;
400 if (!bitbang
->txrx_bufs
) {
401 bitbang
->use_dma
= 0;
402 bitbang
->txrx_bufs
= spi_bitbang_bufs
;
403 if (!master
->setup
) {
404 if (!bitbang
->setup_transfer
)
405 bitbang
->setup_transfer
=
406 spi_bitbang_setup_transfer
;
407 master
->setup
= spi_bitbang_setup
;
408 master
->cleanup
= spi_bitbang_cleanup
;
412 /* driver may get busy before register() returns, especially
413 * if someone registered boardinfo for devices
415 ret
= spi_register_master(spi_master_get(master
));
417 spi_master_put(master
);
421 EXPORT_SYMBOL_GPL(spi_bitbang_start
);
424 * spi_bitbang_stop - stops the task providing spi communication
426 void spi_bitbang_stop(struct spi_bitbang
*bitbang
)
428 spi_unregister_master(bitbang
->master
);
430 EXPORT_SYMBOL_GPL(spi_bitbang_stop
);
432 MODULE_LICENSE("GPL");