Lines Matching +full:spi +full:- +full:controller

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Polling/bitbanging SPI host controller controller driver utilities
16 #include <linux/spi/spi.h>
17 #include <linux/spi/spi_bitbang.h>
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
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,
51 static unsigned int bitbang_txrx_8(struct spi_device *spi, in bitbang_txrx_8() argument
58 unsigned int bits = t->bits_per_word; in bitbang_txrx_8()
59 unsigned int count = t->len; in bitbang_txrx_8()
60 const u8 *tx = t->tx_buf; in bitbang_txrx_8()
61 u8 *rx = t->rx_buf; in bitbang_txrx_8()
63 bitbang = spi_controller_get_devdata(spi->controller); in bitbang_txrx_8()
70 word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFF : 0; in bitbang_txrx_8()
71 word = txrx_word(spi, ns, word, bits, flags); in bitbang_txrx_8()
74 count -= 1; in bitbang_txrx_8()
76 if (bitbang->set_mosi_idle) in bitbang_txrx_8()
77 bitbang->set_mosi_idle(spi); in bitbang_txrx_8()
79 return t->len - count; in bitbang_txrx_8()
82 static unsigned int bitbang_txrx_16(struct spi_device *spi, in bitbang_txrx_16() argument
89 unsigned int bits = t->bits_per_word; in bitbang_txrx_16()
90 unsigned int count = t->len; in bitbang_txrx_16()
91 const u16 *tx = t->tx_buf; in bitbang_txrx_16()
92 u16 *rx = t->rx_buf; in bitbang_txrx_16()
94 bitbang = spi_controller_get_devdata(spi->controller); in bitbang_txrx_16()
101 word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFFFF : 0; in bitbang_txrx_16()
102 word = txrx_word(spi, ns, word, bits, flags); in bitbang_txrx_16()
105 count -= 2; in bitbang_txrx_16()
107 if (bitbang->set_mosi_idle) in bitbang_txrx_16()
108 bitbang->set_mosi_idle(spi); in bitbang_txrx_16()
110 return t->len - count; in bitbang_txrx_16()
113 static unsigned int bitbang_txrx_32(struct spi_device *spi, in bitbang_txrx_32() argument
120 unsigned int bits = t->bits_per_word; in bitbang_txrx_32()
121 unsigned int count = t->len; in bitbang_txrx_32()
122 const u32 *tx = t->tx_buf; in bitbang_txrx_32()
123 u32 *rx = t->rx_buf; in bitbang_txrx_32()
125 bitbang = spi_controller_get_devdata(spi->controller); in bitbang_txrx_32()
132 word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFFFFFFFF : 0; in bitbang_txrx_32()
133 word = txrx_word(spi, ns, word, bits, flags); in bitbang_txrx_32()
136 count -= 4; in bitbang_txrx_32()
138 if (bitbang->set_mosi_idle) in bitbang_txrx_32()
139 bitbang->set_mosi_idle(spi); in bitbang_txrx_32()
141 return t->len - count; in bitbang_txrx_32()
144 int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t) in spi_bitbang_setup_transfer() argument
146 struct spi_bitbang_cs *cs = spi->controller_state; in spi_bitbang_setup_transfer()
151 bits_per_word = t->bits_per_word; in spi_bitbang_setup_transfer()
152 hz = t->speed_hz; in spi_bitbang_setup_transfer()
158 /* spi_transfer level calls that work per-word */ in spi_bitbang_setup_transfer()
160 bits_per_word = spi->bits_per_word; in spi_bitbang_setup_transfer()
162 cs->txrx_bufs = bitbang_txrx_8; in spi_bitbang_setup_transfer()
164 cs->txrx_bufs = bitbang_txrx_16; in spi_bitbang_setup_transfer()
166 cs->txrx_bufs = bitbang_txrx_32; in spi_bitbang_setup_transfer()
168 return -EINVAL; in spi_bitbang_setup_transfer()
172 hz = spi->max_speed_hz; in spi_bitbang_setup_transfer()
174 cs->nsecs = (NSEC_PER_SEC / 2) / hz; in spi_bitbang_setup_transfer()
175 if (cs->nsecs > (MAX_UDELAY_MS * NSEC_PER_MSEC)) in spi_bitbang_setup_transfer()
176 return -EINVAL; in spi_bitbang_setup_transfer()
184 * spi_bitbang_setup - default setup for per-word I/O loops
186 int spi_bitbang_setup(struct spi_device *spi) in spi_bitbang_setup() argument
188 struct spi_bitbang_cs *cs = spi->controller_state; in spi_bitbang_setup()
193 bitbang = spi_controller_get_devdata(spi->controller); in spi_bitbang_setup()
198 return -ENOMEM; in spi_bitbang_setup()
199 spi->controller_state = cs; in spi_bitbang_setup()
203 /* per-word shift register access, in hardware or bitbanging */ in spi_bitbang_setup()
204 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)]; in spi_bitbang_setup()
205 if (!cs->txrx_word) { in spi_bitbang_setup()
206 retval = -EINVAL; in spi_bitbang_setup()
210 if (bitbang->setup_transfer) { in spi_bitbang_setup()
211 retval = bitbang->setup_transfer(spi, NULL); in spi_bitbang_setup()
216 if (bitbang->set_mosi_idle) in spi_bitbang_setup()
217 bitbang->set_mosi_idle(spi); in spi_bitbang_setup()
219 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs); in spi_bitbang_setup()
231 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
233 void spi_bitbang_cleanup(struct spi_device *spi) in spi_bitbang_cleanup() argument
235 kfree(spi->controller_state); in spi_bitbang_cleanup()
239 static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t) in spi_bitbang_bufs() argument
241 struct spi_bitbang_cs *cs = spi->controller_state; in spi_bitbang_bufs()
242 unsigned int nsecs = cs->nsecs; in spi_bitbang_bufs()
245 bitbang = spi_controller_get_devdata(spi->controller); in spi_bitbang_bufs()
246 if (bitbang->set_line_direction) { in spi_bitbang_bufs()
249 err = bitbang->set_line_direction(spi, !!(t->tx_buf)); in spi_bitbang_bufs()
254 if (spi->mode & SPI_3WIRE) { in spi_bitbang_bufs()
257 flags = t->tx_buf ? SPI_CONTROLLER_NO_RX : SPI_CONTROLLER_NO_TX; in spi_bitbang_bufs()
258 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags); in spi_bitbang_bufs()
260 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0); in spi_bitbang_bufs()
263 /*----------------------------------------------------------------------*/
268 * This costs a task context per controller, running the queue by
270 * several DMA transfers at once, and process several controller queues
273 * Drivers can provide word-at-a-time i/o primitives, or provide
274 * transfer-at-a-time ones to leverage dma or fifo hardware.
277 static int spi_bitbang_prepare_hardware(struct spi_controller *spi) in spi_bitbang_prepare_hardware() argument
281 bitbang = spi_controller_get_devdata(spi); in spi_bitbang_prepare_hardware()
283 mutex_lock(&bitbang->lock); in spi_bitbang_prepare_hardware()
284 bitbang->busy = 1; in spi_bitbang_prepare_hardware()
285 mutex_unlock(&bitbang->lock); in spi_bitbang_prepare_hardware()
291 struct spi_device *spi, in spi_bitbang_transfer_one() argument
297 if (bitbang->setup_transfer) { in spi_bitbang_transfer_one()
298 status = bitbang->setup_transfer(spi, transfer); in spi_bitbang_transfer_one()
303 if (transfer->len) in spi_bitbang_transfer_one()
304 status = bitbang->txrx_bufs(spi, transfer); in spi_bitbang_transfer_one()
306 if (status == transfer->len) in spi_bitbang_transfer_one()
309 status = -EREMOTEIO; in spi_bitbang_transfer_one()
317 static int spi_bitbang_unprepare_hardware(struct spi_controller *spi) in spi_bitbang_unprepare_hardware() argument
321 bitbang = spi_controller_get_devdata(spi); in spi_bitbang_unprepare_hardware()
323 mutex_lock(&bitbang->lock); in spi_bitbang_unprepare_hardware()
324 bitbang->busy = 0; in spi_bitbang_unprepare_hardware()
325 mutex_unlock(&bitbang->lock); in spi_bitbang_unprepare_hardware()
330 static void spi_bitbang_set_cs(struct spi_device *spi, bool enable) in spi_bitbang_set_cs() argument
332 struct spi_bitbang *bitbang = spi_controller_get_devdata(spi->controller); in spi_bitbang_set_cs()
334 /* SPI core provides CS high / low, but bitbang driver in spi_bitbang_set_cs()
336 * spi device driver takes care of handling SPI_CS_HIGH in spi_bitbang_set_cs()
338 enable = (!!(spi->mode & SPI_CS_HIGH) == enable); in spi_bitbang_set_cs()
341 bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE : in spi_bitbang_set_cs()
346 /*----------------------------------------------------------------------*/
350 struct spi_controller *ctlr = bitbang->ctlr; in spi_bitbang_init()
354 return -EINVAL; in spi_bitbang_init()
359 * driver-specific chipselect routine. in spi_bitbang_init()
361 custom_cs = (!ctlr->use_gpio_descriptors || in spi_bitbang_init()
362 (ctlr->flags & SPI_CONTROLLER_GPIO_SS)); in spi_bitbang_init()
364 if (custom_cs && !bitbang->chipselect) in spi_bitbang_init()
365 return -EINVAL; in spi_bitbang_init()
367 mutex_init(&bitbang->lock); in spi_bitbang_init()
369 if (!ctlr->mode_bits) in spi_bitbang_init()
370 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags; in spi_bitbang_init()
372 if (ctlr->transfer || ctlr->transfer_one_message) in spi_bitbang_init()
373 return -EINVAL; in spi_bitbang_init()
375 ctlr->prepare_transfer_hardware = spi_bitbang_prepare_hardware; in spi_bitbang_init()
376 ctlr->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware; in spi_bitbang_init()
377 ctlr->transfer_one = spi_bitbang_transfer_one; in spi_bitbang_init()
379 * When using GPIO descriptors, the ->set_cs() callback doesn't even in spi_bitbang_init()
383 ctlr->set_cs = spi_bitbang_set_cs; in spi_bitbang_init()
385 if (!bitbang->txrx_bufs) { in spi_bitbang_init()
386 bitbang->use_dma = 0; in spi_bitbang_init()
387 bitbang->txrx_bufs = spi_bitbang_bufs; in spi_bitbang_init()
388 if (!ctlr->setup) { in spi_bitbang_init()
389 if (!bitbang->setup_transfer) in spi_bitbang_init()
390 bitbang->setup_transfer = in spi_bitbang_init()
392 ctlr->setup = spi_bitbang_setup; in spi_bitbang_init()
393 ctlr->cleanup = spi_bitbang_cleanup; in spi_bitbang_init()
402 * spi_bitbang_start - start up a polled/bitbanging SPI host controller driver
405 * Caller should have zero-initialized all parts of the structure, and then
406 * provided callbacks for chip selection and I/O loops. If the host controller has
411 * For i/o loops, provide callbacks either per-word (for bitbanging, or for
412 * hardware that basically exposes a shift register) or per-spi_transfer
415 * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup(),
416 * spi_bitbang_cleanup() and spi_bitbang_setup_transfer() to handle those SPI
417 * host controller methods. Those methods are the defaults if the bitbang->txrx_bufs
424 * On success, this routine will take a reference to the controller. The caller
431 struct spi_controller *ctlr = bitbang->ctlr; in spi_bitbang_start()
450 * spi_bitbang_stop - stops the task providing spi communication
454 spi_unregister_controller(bitbang->ctlr); in spi_bitbang_stop()
459 MODULE_DESCRIPTION("Utilities for Bitbanging SPI host controllers");