1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * polling/bitbanging SPI master 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>
15 #include <linux/spi/spi.h>
16 #include <linux/spi/spi_bitbang.h>
18 #define SPI_BITBANG_CS_DELAY 100
21 /*----------------------------------------------------------------------*/
24 * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
25 * Use this for GPIO or shift-register level hardware APIs.
27 * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
28 * to glue code. These bitbang setup() and cleanup() routines are always
29 * used, though maybe they're called from controller-aware code.
31 * chipselect() and friends may use spi_device->controller_data and
32 * controller registers as appropriate.
35 * NOTE: SPI controller pins can often be used as GPIO pins instead,
36 * which means you could use a bitbang driver either to get hardware
37 * working quickly, or testing for differences that aren't speed related.
40 struct spi_bitbang_cs {
41 unsigned nsecs; /* (clock cycle time)/2 */
42 u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
43 u32 word, u8 bits, unsigned flags);
44 unsigned (*txrx_bufs)(struct spi_device *,
46 struct spi_device *spi,
50 unsigned, struct spi_transfer *,
54 static unsigned bitbang_txrx_8(
55 struct spi_device *spi,
56 u32 (*txrx_word)(struct spi_device *spi,
61 struct spi_transfer *t,
64 unsigned bits = t->bits_per_word;
65 unsigned count = t->len;
66 const u8 *tx = t->tx_buf;
69 while (likely(count > 0)) {
74 word = txrx_word(spi, ns, word, bits, flags);
79 return t->len - count;
82 static unsigned bitbang_txrx_16(
83 struct spi_device *spi,
84 u32 (*txrx_word)(struct spi_device *spi,
89 struct spi_transfer *t,
92 unsigned bits = t->bits_per_word;
93 unsigned count = t->len;
94 const u16 *tx = t->tx_buf;
97 while (likely(count > 1)) {
102 word = txrx_word(spi, ns, word, bits, flags);
107 return t->len - count;
110 static unsigned bitbang_txrx_32(
111 struct spi_device *spi,
112 u32 (*txrx_word)(struct spi_device *spi,
117 struct spi_transfer *t,
120 unsigned bits = t->bits_per_word;
121 unsigned count = t->len;
122 const u32 *tx = t->tx_buf;
125 while (likely(count > 3)) {
130 word = txrx_word(spi, ns, word, bits, flags);
135 return t->len - count;
138 int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
140 struct spi_bitbang_cs *cs = spi->controller_state;
145 bits_per_word = t->bits_per_word;
152 /* spi_transfer level calls that work per-word */
154 bits_per_word = spi->bits_per_word;
155 if (bits_per_word <= 8)
156 cs->txrx_bufs = bitbang_txrx_8;
157 else if (bits_per_word <= 16)
158 cs->txrx_bufs = bitbang_txrx_16;
159 else if (bits_per_word <= 32)
160 cs->txrx_bufs = bitbang_txrx_32;
164 /* nsecs = (clock period)/2 */
166 hz = spi->max_speed_hz;
168 cs->nsecs = (1000000000/2) / hz;
169 if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
175 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
178 * spi_bitbang_setup - default setup for per-word I/O loops
180 int spi_bitbang_setup(struct spi_device *spi)
182 struct spi_bitbang_cs *cs = spi->controller_state;
183 struct spi_bitbang *bitbang;
184 bool initial_setup = false;
187 bitbang = spi_master_get_devdata(spi->master);
190 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
193 spi->controller_state = cs;
194 initial_setup = true;
197 /* per-word shift register access, in hardware or bitbanging */
198 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
199 if (!cs->txrx_word) {
204 if (bitbang->setup_transfer) {
205 retval = bitbang->setup_transfer(spi, NULL);
210 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
219 EXPORT_SYMBOL_GPL(spi_bitbang_setup);
222 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
224 void spi_bitbang_cleanup(struct spi_device *spi)
226 kfree(spi->controller_state);
228 EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
230 static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
232 struct spi_bitbang_cs *cs = spi->controller_state;
233 unsigned nsecs = cs->nsecs;
234 struct spi_bitbang *bitbang;
236 bitbang = spi_master_get_devdata(spi->master);
237 if (bitbang->set_line_direction) {
240 err = bitbang->set_line_direction(spi, !!(t->tx_buf));
245 if (spi->mode & SPI_3WIRE) {
248 flags = t->tx_buf ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX;
249 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
251 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
254 /*----------------------------------------------------------------------*/
257 * SECOND PART ... simple transfer queue runner.
259 * This costs a task context per controller, running the queue by
260 * performing each transfer in sequence. Smarter hardware can queue
261 * several DMA transfers at once, and process several controller queues
262 * in parallel; this driver doesn't match such hardware very well.
264 * Drivers can provide word-at-a-time i/o primitives, or provide
265 * transfer-at-a-time ones to leverage dma or fifo hardware.
268 static int spi_bitbang_prepare_hardware(struct spi_master *spi)
270 struct spi_bitbang *bitbang;
272 bitbang = spi_master_get_devdata(spi);
274 mutex_lock(&bitbang->lock);
276 mutex_unlock(&bitbang->lock);
281 static int spi_bitbang_transfer_one(struct spi_master *master,
282 struct spi_device *spi,
283 struct spi_transfer *transfer)
285 struct spi_bitbang *bitbang = spi_master_get_devdata(master);
288 if (bitbang->setup_transfer) {
289 status = bitbang->setup_transfer(spi, transfer);
295 status = bitbang->txrx_bufs(spi, transfer);
297 if (status == transfer->len)
299 else if (status >= 0)
303 spi_finalize_current_transfer(master);
308 static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
310 struct spi_bitbang *bitbang;
312 bitbang = spi_master_get_devdata(spi);
314 mutex_lock(&bitbang->lock);
316 mutex_unlock(&bitbang->lock);
321 static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
323 struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
325 /* SPI core provides CS high / low, but bitbang driver
327 * spi device driver takes care of handling SPI_CS_HIGH
329 enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
331 ndelay(SPI_BITBANG_CS_DELAY);
332 bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
333 BITBANG_CS_INACTIVE);
334 ndelay(SPI_BITBANG_CS_DELAY);
337 /*----------------------------------------------------------------------*/
339 int spi_bitbang_init(struct spi_bitbang *bitbang)
341 struct spi_master *master = bitbang->master;
347 * We only need the chipselect callback if we are actually using it.
348 * If we just use GPIO descriptors, it is surplus. If the
349 * SPI_MASTER_GPIO_SS flag is set, we always need to call the
350 * driver-specific chipselect routine.
352 custom_cs = (!master->use_gpio_descriptors ||
353 (master->flags & SPI_MASTER_GPIO_SS));
355 if (custom_cs && !bitbang->chipselect)
358 mutex_init(&bitbang->lock);
360 if (!master->mode_bits)
361 master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
363 if (master->transfer || master->transfer_one_message)
366 master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
367 master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
368 master->transfer_one = spi_bitbang_transfer_one;
370 * When using GPIO descriptors, the ->set_cs() callback doesn't even
371 * get called unless SPI_MASTER_GPIO_SS is set.
374 master->set_cs = spi_bitbang_set_cs;
376 if (!bitbang->txrx_bufs) {
377 bitbang->use_dma = 0;
378 bitbang->txrx_bufs = spi_bitbang_bufs;
379 if (!master->setup) {
380 if (!bitbang->setup_transfer)
381 bitbang->setup_transfer =
382 spi_bitbang_setup_transfer;
383 master->setup = spi_bitbang_setup;
384 master->cleanup = spi_bitbang_cleanup;
390 EXPORT_SYMBOL_GPL(spi_bitbang_init);
393 * spi_bitbang_start - start up a polled/bitbanging SPI master driver
394 * @bitbang: driver handle
396 * Caller should have zero-initialized all parts of the structure, and then
397 * provided callbacks for chip selection and I/O loops. If the master has
398 * a transfer method, its final step should call spi_bitbang_transfer; or,
399 * that's the default if the transfer routine is not initialized. It should
400 * also set up the bus number and number of chipselects.
402 * For i/o loops, provide callbacks either per-word (for bitbanging, or for
403 * hardware that basically exposes a shift register) or per-spi_transfer
404 * (which takes better advantage of hardware like fifos or DMA engines).
406 * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
407 * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
408 * master methods. Those methods are the defaults if the bitbang->txrx_bufs
409 * routine isn't initialized.
411 * This routine registers the spi_master, which will process requests in a
412 * dedicated task, keeping IRQs unblocked most of the time. To stop
413 * processing those requests, call spi_bitbang_stop().
415 * On success, this routine will take a reference to master. The caller is
416 * responsible for calling spi_bitbang_stop() to decrement the reference and
417 * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
420 int spi_bitbang_start(struct spi_bitbang *bitbang)
422 struct spi_master *master = bitbang->master;
425 ret = spi_bitbang_init(bitbang);
429 /* driver may get busy before register() returns, especially
430 * if someone registered boardinfo for devices
432 ret = spi_register_master(spi_master_get(master));
434 spi_master_put(master);
438 EXPORT_SYMBOL_GPL(spi_bitbang_start);
441 * spi_bitbang_stop - stops the task providing spi communication
443 void spi_bitbang_stop(struct spi_bitbang *bitbang)
445 spi_unregister_master(bitbang->master);
447 EXPORT_SYMBOL_GPL(spi_bitbang_stop);
449 MODULE_LICENSE("GPL");