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 typedef unsigned int (*spi_bb_txrx_bufs_fn)(struct spi_device *, spi_bb_txrx_word_fn,
42 unsigned int, struct spi_transfer *,
45 struct spi_bitbang_cs {
46 unsigned int nsecs; /* (clock cycle time) / 2 */
47 spi_bb_txrx_word_fn txrx_word;
48 spi_bb_txrx_bufs_fn txrx_bufs;
51 static unsigned int bitbang_txrx_8(struct spi_device *spi,
52 spi_bb_txrx_word_fn txrx_word,
54 struct spi_transfer *t,
57 unsigned int bits = t->bits_per_word;
58 unsigned int count = t->len;
59 const u8 *tx = t->tx_buf;
62 while (likely(count > 0)) {
67 word = txrx_word(spi, ns, word, bits, flags);
72 return t->len - count;
75 static unsigned int bitbang_txrx_16(struct spi_device *spi,
76 spi_bb_txrx_word_fn txrx_word,
78 struct spi_transfer *t,
81 unsigned int bits = t->bits_per_word;
82 unsigned int count = t->len;
83 const u16 *tx = t->tx_buf;
86 while (likely(count > 1)) {
91 word = txrx_word(spi, ns, word, bits, flags);
96 return t->len - count;
99 static unsigned int bitbang_txrx_32(struct spi_device *spi,
100 spi_bb_txrx_word_fn txrx_word,
102 struct spi_transfer *t,
105 unsigned int bits = t->bits_per_word;
106 unsigned int count = t->len;
107 const u32 *tx = t->tx_buf;
110 while (likely(count > 3)) {
115 word = txrx_word(spi, ns, word, bits, flags);
120 return t->len - count;
123 int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
125 struct spi_bitbang_cs *cs = spi->controller_state;
130 bits_per_word = t->bits_per_word;
137 /* spi_transfer level calls that work 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;
149 /* nsecs = (clock period)/2 */
151 hz = spi->max_speed_hz;
153 cs->nsecs = (NSEC_PER_SEC / 2) / hz;
154 if (cs->nsecs > (MAX_UDELAY_MS * NSEC_PER_MSEC))
160 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
163 * spi_bitbang_setup - default setup for per-word I/O loops
165 int spi_bitbang_setup(struct spi_device *spi)
167 struct spi_bitbang_cs *cs = spi->controller_state;
168 struct spi_bitbang *bitbang;
169 bool initial_setup = false;
172 bitbang = spi_controller_get_devdata(spi->controller);
175 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
178 spi->controller_state = cs;
179 initial_setup = true;
182 /* per-word shift register access, in hardware or bitbanging */
183 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
184 if (!cs->txrx_word) {
189 if (bitbang->setup_transfer) {
190 retval = bitbang->setup_transfer(spi, NULL);
195 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
204 EXPORT_SYMBOL_GPL(spi_bitbang_setup);
207 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
209 void spi_bitbang_cleanup(struct spi_device *spi)
211 kfree(spi->controller_state);
213 EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
215 static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
217 struct spi_bitbang_cs *cs = spi->controller_state;
218 unsigned int nsecs = cs->nsecs;
219 struct spi_bitbang *bitbang;
221 bitbang = spi_controller_get_devdata(spi->controller);
222 if (bitbang->set_line_direction) {
225 err = bitbang->set_line_direction(spi, !!(t->tx_buf));
230 if (spi->mode & SPI_3WIRE) {
233 flags = t->tx_buf ? SPI_CONTROLLER_NO_RX : SPI_CONTROLLER_NO_TX;
234 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
236 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
239 /*----------------------------------------------------------------------*/
242 * SECOND PART ... simple transfer queue runner.
244 * This costs a task context per controller, running the queue by
245 * performing each transfer in sequence. Smarter hardware can queue
246 * several DMA transfers at once, and process several controller queues
247 * in parallel; this driver doesn't match such hardware very well.
249 * Drivers can provide word-at-a-time i/o primitives, or provide
250 * transfer-at-a-time ones to leverage dma or fifo hardware.
253 static int spi_bitbang_prepare_hardware(struct spi_controller *spi)
255 struct spi_bitbang *bitbang;
257 bitbang = spi_controller_get_devdata(spi);
259 mutex_lock(&bitbang->lock);
261 mutex_unlock(&bitbang->lock);
266 static int spi_bitbang_transfer_one(struct spi_controller *ctlr,
267 struct spi_device *spi,
268 struct spi_transfer *transfer)
270 struct spi_bitbang *bitbang = spi_controller_get_devdata(ctlr);
273 if (bitbang->setup_transfer) {
274 status = bitbang->setup_transfer(spi, transfer);
280 status = bitbang->txrx_bufs(spi, transfer);
282 if (status == transfer->len)
284 else if (status >= 0)
288 spi_finalize_current_transfer(ctlr);
293 static int spi_bitbang_unprepare_hardware(struct spi_controller *spi)
295 struct spi_bitbang *bitbang;
297 bitbang = spi_controller_get_devdata(spi);
299 mutex_lock(&bitbang->lock);
301 mutex_unlock(&bitbang->lock);
306 static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
308 struct spi_bitbang *bitbang = spi_controller_get_devdata(spi->controller);
310 /* SPI core provides CS high / low, but bitbang driver
312 * spi device driver takes care of handling SPI_CS_HIGH
314 enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
316 ndelay(SPI_BITBANG_CS_DELAY);
317 bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
318 BITBANG_CS_INACTIVE);
319 ndelay(SPI_BITBANG_CS_DELAY);
322 /*----------------------------------------------------------------------*/
324 int spi_bitbang_init(struct spi_bitbang *bitbang)
326 struct spi_controller *ctlr = bitbang->ctlr;
332 * We only need the chipselect callback if we are actually using it.
333 * If we just use GPIO descriptors, it is surplus. If the
334 * SPI_CONTROLLER_GPIO_SS flag is set, we always need to call the
335 * driver-specific chipselect routine.
337 custom_cs = (!ctlr->use_gpio_descriptors ||
338 (ctlr->flags & SPI_CONTROLLER_GPIO_SS));
340 if (custom_cs && !bitbang->chipselect)
343 mutex_init(&bitbang->lock);
345 if (!ctlr->mode_bits)
346 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
348 if (ctlr->transfer || ctlr->transfer_one_message)
351 ctlr->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
352 ctlr->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
353 ctlr->transfer_one = spi_bitbang_transfer_one;
355 * When using GPIO descriptors, the ->set_cs() callback doesn't even
356 * get called unless SPI_CONTROLLER_GPIO_SS is set.
359 ctlr->set_cs = spi_bitbang_set_cs;
361 if (!bitbang->txrx_bufs) {
362 bitbang->use_dma = 0;
363 bitbang->txrx_bufs = spi_bitbang_bufs;
365 if (!bitbang->setup_transfer)
366 bitbang->setup_transfer =
367 spi_bitbang_setup_transfer;
368 ctlr->setup = spi_bitbang_setup;
369 ctlr->cleanup = spi_bitbang_cleanup;
375 EXPORT_SYMBOL_GPL(spi_bitbang_init);
378 * spi_bitbang_start - start up a polled/bitbanging SPI host controller driver
379 * @bitbang: driver handle
381 * Caller should have zero-initialized all parts of the structure, and then
382 * provided callbacks for chip selection and I/O loops. If the host controller has
383 * a transfer method, its final step should call spi_bitbang_transfer(); or,
384 * that's the default if the transfer routine is not initialized. It should
385 * also set up the bus number and number of chipselects.
387 * For i/o loops, provide callbacks either per-word (for bitbanging, or for
388 * hardware that basically exposes a shift register) or per-spi_transfer
389 * (which takes better advantage of hardware like fifos or DMA engines).
391 * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup(),
392 * spi_bitbang_cleanup() and spi_bitbang_setup_transfer() to handle those SPI
393 * host controller methods. Those methods are the defaults if the bitbang->txrx_bufs
394 * routine isn't initialized.
396 * This routine registers the spi_controller, which will process requests in a
397 * dedicated task, keeping IRQs unblocked most of the time. To stop
398 * processing those requests, call spi_bitbang_stop().
400 * On success, this routine will take a reference to the controller. The caller
401 * is responsible for calling spi_bitbang_stop() to decrement the reference and
402 * spi_controller_put() as counterpart of spi_alloc_host() to prevent a memory
405 int spi_bitbang_start(struct spi_bitbang *bitbang)
407 struct spi_controller *ctlr = bitbang->ctlr;
410 ret = spi_bitbang_init(bitbang);
414 /* driver may get busy before register() returns, especially
415 * if someone registered boardinfo for devices
417 ret = spi_register_controller(spi_controller_get(ctlr));
419 spi_controller_put(ctlr);
423 EXPORT_SYMBOL_GPL(spi_bitbang_start);
426 * spi_bitbang_stop - stops the task providing spi communication
428 void spi_bitbang_stop(struct spi_bitbang *bitbang)
430 spi_unregister_controller(bitbang->ctlr);
432 EXPORT_SYMBOL_GPL(spi_bitbang_stop);
434 MODULE_LICENSE("GPL");
435 MODULE_DESCRIPTION("Utilities for Bitbanging SPI host controllers");