1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Polling/bitbanging SPI host controller controller driver utilities
6 #include <linux/spinlock.h>
7 #include <linux/workqueue.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/time64.h>
16 #include <linux/spi/spi.h>
17 #include <linux/spi/spi_bitbang.h>
19 #define SPI_BITBANG_CS_DELAY 100
22 /*----------------------------------------------------------------------*/
25 * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
26 * Use this for GPIO or shift-register level hardware APIs.
28 * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
29 * to glue code. These bitbang setup() and cleanup() routines are always
30 * used, though maybe they're called from controller-aware code.
32 * chipselect() and friends may use spi_device->controller_data and
33 * controller registers as appropriate.
36 * NOTE: SPI controller pins can often be used as GPIO pins instead,
37 * which means you could use a bitbang driver either to get hardware
38 * working quickly, or testing for differences that aren't speed related.
41 struct spi_bitbang_cs {
42 unsigned nsecs; /* (clock cycle time)/2 */
43 u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
44 u32 word, u8 bits, unsigned flags);
45 unsigned (*txrx_bufs)(struct spi_device *,
47 struct spi_device *spi,
51 unsigned, struct spi_transfer *,
55 static unsigned bitbang_txrx_8(
56 struct spi_device *spi,
57 u32 (*txrx_word)(struct spi_device *spi,
62 struct spi_transfer *t,
66 unsigned bits = t->bits_per_word;
67 unsigned count = t->len;
68 const u8 *tx = t->tx_buf;
71 while (likely(count > 0)) {
76 word = txrx_word(spi, ns, word, bits, flags);
81 return t->len - count;
84 static unsigned bitbang_txrx_16(
85 struct spi_device *spi,
86 u32 (*txrx_word)(struct spi_device *spi,
91 struct spi_transfer *t,
95 unsigned bits = t->bits_per_word;
96 unsigned count = t->len;
97 const u16 *tx = t->tx_buf;
100 while (likely(count > 1)) {
105 word = txrx_word(spi, ns, word, bits, flags);
110 return t->len - count;
113 static unsigned bitbang_txrx_32(
114 struct spi_device *spi,
115 u32 (*txrx_word)(struct spi_device *spi,
120 struct spi_transfer *t,
124 unsigned bits = t->bits_per_word;
125 unsigned count = t->len;
126 const u32 *tx = t->tx_buf;
129 while (likely(count > 3)) {
134 word = txrx_word(spi, ns, word, bits, flags);
139 return t->len - count;
142 int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
144 struct spi_bitbang_cs *cs = spi->controller_state;
149 bits_per_word = t->bits_per_word;
156 /* spi_transfer level calls that work per-word */
158 bits_per_word = spi->bits_per_word;
159 if (bits_per_word <= 8)
160 cs->txrx_bufs = bitbang_txrx_8;
161 else if (bits_per_word <= 16)
162 cs->txrx_bufs = bitbang_txrx_16;
163 else if (bits_per_word <= 32)
164 cs->txrx_bufs = bitbang_txrx_32;
168 /* nsecs = (clock period)/2 */
170 hz = spi->max_speed_hz;
172 cs->nsecs = (NSEC_PER_SEC / 2) / hz;
173 if (cs->nsecs > (MAX_UDELAY_MS * NSEC_PER_MSEC))
179 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
182 * spi_bitbang_setup - default setup for per-word I/O loops
184 int spi_bitbang_setup(struct spi_device *spi)
186 struct spi_bitbang_cs *cs = spi->controller_state;
187 struct spi_bitbang *bitbang;
188 bool initial_setup = false;
191 bitbang = spi_controller_get_devdata(spi->controller);
194 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
197 spi->controller_state = cs;
198 initial_setup = true;
201 /* per-word shift register access, in hardware or bitbanging */
202 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
203 if (!cs->txrx_word) {
208 if (bitbang->setup_transfer) {
209 retval = bitbang->setup_transfer(spi, NULL);
214 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
223 EXPORT_SYMBOL_GPL(spi_bitbang_setup);
226 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
228 void spi_bitbang_cleanup(struct spi_device *spi)
230 kfree(spi->controller_state);
232 EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
234 static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
236 struct spi_bitbang_cs *cs = spi->controller_state;
237 unsigned nsecs = cs->nsecs;
238 struct spi_bitbang *bitbang;
240 bitbang = spi_controller_get_devdata(spi->controller);
241 if (bitbang->set_line_direction) {
244 err = bitbang->set_line_direction(spi, !!(t->tx_buf));
249 if (spi->mode & SPI_3WIRE) {
252 flags = t->tx_buf ? SPI_CONTROLLER_NO_RX : SPI_CONTROLLER_NO_TX;
253 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
255 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
258 /*----------------------------------------------------------------------*/
261 * SECOND PART ... simple transfer queue runner.
263 * This costs a task context per controller, running the queue by
264 * performing each transfer in sequence. Smarter hardware can queue
265 * several DMA transfers at once, and process several controller queues
266 * in parallel; this driver doesn't match such hardware very well.
268 * Drivers can provide word-at-a-time i/o primitives, or provide
269 * transfer-at-a-time ones to leverage dma or fifo hardware.
272 static int spi_bitbang_prepare_hardware(struct spi_controller *spi)
274 struct spi_bitbang *bitbang;
276 bitbang = spi_controller_get_devdata(spi);
278 mutex_lock(&bitbang->lock);
280 mutex_unlock(&bitbang->lock);
285 static int spi_bitbang_transfer_one(struct spi_controller *ctlr,
286 struct spi_device *spi,
287 struct spi_transfer *transfer)
289 struct spi_bitbang *bitbang = spi_controller_get_devdata(ctlr);
292 if (bitbang->setup_transfer) {
293 status = bitbang->setup_transfer(spi, transfer);
299 status = bitbang->txrx_bufs(spi, transfer);
301 if (status == transfer->len)
303 else if (status >= 0)
307 spi_finalize_current_transfer(ctlr);
312 static int spi_bitbang_unprepare_hardware(struct spi_controller *spi)
314 struct spi_bitbang *bitbang;
316 bitbang = spi_controller_get_devdata(spi);
318 mutex_lock(&bitbang->lock);
320 mutex_unlock(&bitbang->lock);
325 static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
327 struct spi_bitbang *bitbang = spi_controller_get_devdata(spi->controller);
329 /* SPI core provides CS high / low, but bitbang driver
331 * spi device driver takes care of handling SPI_CS_HIGH
333 enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
335 ndelay(SPI_BITBANG_CS_DELAY);
336 bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
337 BITBANG_CS_INACTIVE);
338 ndelay(SPI_BITBANG_CS_DELAY);
341 /*----------------------------------------------------------------------*/
343 int spi_bitbang_init(struct spi_bitbang *bitbang)
345 struct spi_controller *ctlr = bitbang->ctlr;
351 * We only need the chipselect callback if we are actually using it.
352 * If we just use GPIO descriptors, it is surplus. If the
353 * SPI_CONTROLLER_GPIO_SS flag is set, we always need to call the
354 * driver-specific chipselect routine.
356 custom_cs = (!ctlr->use_gpio_descriptors ||
357 (ctlr->flags & SPI_CONTROLLER_GPIO_SS));
359 if (custom_cs && !bitbang->chipselect)
362 mutex_init(&bitbang->lock);
364 if (!ctlr->mode_bits)
365 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
367 if (ctlr->transfer || ctlr->transfer_one_message)
370 ctlr->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
371 ctlr->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
372 ctlr->transfer_one = spi_bitbang_transfer_one;
374 * When using GPIO descriptors, the ->set_cs() callback doesn't even
375 * get called unless SPI_CONTROLLER_GPIO_SS is set.
378 ctlr->set_cs = spi_bitbang_set_cs;
380 if (!bitbang->txrx_bufs) {
381 bitbang->use_dma = 0;
382 bitbang->txrx_bufs = spi_bitbang_bufs;
384 if (!bitbang->setup_transfer)
385 bitbang->setup_transfer =
386 spi_bitbang_setup_transfer;
387 ctlr->setup = spi_bitbang_setup;
388 ctlr->cleanup = spi_bitbang_cleanup;
394 EXPORT_SYMBOL_GPL(spi_bitbang_init);
397 * spi_bitbang_start - start up a polled/bitbanging SPI host controller driver
398 * @bitbang: driver handle
400 * Caller should have zero-initialized all parts of the structure, and then
401 * provided callbacks for chip selection and I/O loops. If the host controller has
402 * a transfer method, its final step should call spi_bitbang_transfer(); or,
403 * that's the default if the transfer routine is not initialized. It should
404 * also set up the bus number and number of chipselects.
406 * For i/o loops, provide callbacks either per-word (for bitbanging, or for
407 * hardware that basically exposes a shift register) or per-spi_transfer
408 * (which takes better advantage of hardware like fifos or DMA engines).
410 * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup(),
411 * spi_bitbang_cleanup() and spi_bitbang_setup_transfer() to handle those SPI
412 * host controller methods. Those methods are the defaults if the bitbang->txrx_bufs
413 * routine isn't initialized.
415 * This routine registers the spi_controller, which will process requests in a
416 * dedicated task, keeping IRQs unblocked most of the time. To stop
417 * processing those requests, call spi_bitbang_stop().
419 * On success, this routine will take a reference to the controller. The caller
420 * is responsible for calling spi_bitbang_stop() to decrement the reference and
421 * spi_controller_put() as counterpart of spi_alloc_host() to prevent a memory
424 int spi_bitbang_start(struct spi_bitbang *bitbang)
426 struct spi_controller *ctlr = bitbang->ctlr;
429 ret = spi_bitbang_init(bitbang);
433 /* driver may get busy before register() returns, especially
434 * if someone registered boardinfo for devices
436 ret = spi_register_controller(spi_controller_get(ctlr));
438 spi_controller_put(ctlr);
442 EXPORT_SYMBOL_GPL(spi_bitbang_start);
445 * spi_bitbang_stop - stops the task providing spi communication
447 void spi_bitbang_stop(struct spi_bitbang *bitbang)
449 spi_unregister_controller(bitbang->ctlr);
451 EXPORT_SYMBOL_GPL(spi_bitbang_stop);
453 MODULE_LICENSE("GPL");
454 MODULE_DESCRIPTION("Utilities for Bitbanging SPI host controllers");