Lines Matching +full:cs +full:- +full:setup

1 // SPDX-License-Identifier: GPL-2.0-or-later
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
65 unsigned bits = t->bits_per_word; in bitbang_txrx_8()
66 unsigned count = t->len; in bitbang_txrx_8()
67 const u8 *tx = t->tx_buf; in bitbang_txrx_8()
68 u8 *rx = t->rx_buf; in bitbang_txrx_8()
78 count -= 1; in bitbang_txrx_8()
80 return t->len - count; in bitbang_txrx_8()
94 unsigned bits = t->bits_per_word; in bitbang_txrx_16()
95 unsigned count = t->len; in bitbang_txrx_16()
96 const u16 *tx = t->tx_buf; in bitbang_txrx_16()
97 u16 *rx = t->rx_buf; in bitbang_txrx_16()
107 count -= 2; in bitbang_txrx_16()
109 return t->len - count; in bitbang_txrx_16()
123 unsigned bits = t->bits_per_word; in bitbang_txrx_32()
124 unsigned count = t->len; in bitbang_txrx_32()
125 const u32 *tx = t->tx_buf; in bitbang_txrx_32()
126 u32 *rx = t->rx_buf; in bitbang_txrx_32()
136 count -= 4; in bitbang_txrx_32()
138 return t->len - count; in bitbang_txrx_32()
143 struct spi_bitbang_cs *cs = spi->controller_state; in spi_bitbang_setup_transfer() local
148 bits_per_word = t->bits_per_word; in spi_bitbang_setup_transfer()
149 hz = t->speed_hz; in spi_bitbang_setup_transfer()
155 /* spi_transfer level calls that work per-word */ in spi_bitbang_setup_transfer()
157 bits_per_word = spi->bits_per_word; in spi_bitbang_setup_transfer()
159 cs->txrx_bufs = bitbang_txrx_8; in spi_bitbang_setup_transfer()
161 cs->txrx_bufs = bitbang_txrx_16; in spi_bitbang_setup_transfer()
163 cs->txrx_bufs = bitbang_txrx_32; in spi_bitbang_setup_transfer()
165 return -EINVAL; in spi_bitbang_setup_transfer()
169 hz = spi->max_speed_hz; in spi_bitbang_setup_transfer()
171 cs->nsecs = (1000000000/2) / hz; in spi_bitbang_setup_transfer()
172 if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000)) in spi_bitbang_setup_transfer()
173 return -EINVAL; in spi_bitbang_setup_transfer()
181 * spi_bitbang_setup - default setup for per-word I/O loops
185 struct spi_bitbang_cs *cs = spi->controller_state; in spi_bitbang_setup() local
190 bitbang = spi_master_get_devdata(spi->master); in spi_bitbang_setup()
192 if (!cs) { in spi_bitbang_setup()
193 cs = kzalloc(sizeof(*cs), GFP_KERNEL); in spi_bitbang_setup()
194 if (!cs) in spi_bitbang_setup()
195 return -ENOMEM; in spi_bitbang_setup()
196 spi->controller_state = cs; in spi_bitbang_setup()
200 /* per-word shift register access, in hardware or bitbanging */ in spi_bitbang_setup()
201 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)]; in spi_bitbang_setup()
202 if (!cs->txrx_word) { in spi_bitbang_setup()
203 retval = -EINVAL; in spi_bitbang_setup()
207 if (bitbang->setup_transfer) { in spi_bitbang_setup()
208 retval = bitbang->setup_transfer(spi, NULL); in spi_bitbang_setup()
213 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs); in spi_bitbang_setup()
219 kfree(cs); in spi_bitbang_setup()
225 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
229 kfree(spi->controller_state); in spi_bitbang_cleanup()
235 struct spi_bitbang_cs *cs = spi->controller_state; in spi_bitbang_bufs() local
236 unsigned nsecs = cs->nsecs; in spi_bitbang_bufs()
239 bitbang = spi_master_get_devdata(spi->master); in spi_bitbang_bufs()
240 if (bitbang->set_line_direction) { in spi_bitbang_bufs()
243 err = bitbang->set_line_direction(spi, !!(t->tx_buf)); in spi_bitbang_bufs()
248 if (spi->mode & SPI_3WIRE) { in spi_bitbang_bufs()
251 flags = t->tx_buf ? SPI_CONTROLLER_NO_RX : SPI_CONTROLLER_NO_TX; in spi_bitbang_bufs()
252 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags); in spi_bitbang_bufs()
254 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0); in spi_bitbang_bufs()
257 /*----------------------------------------------------------------------*/
267 * Drivers can provide word-at-a-time i/o primitives, or provide
268 * transfer-at-a-time ones to leverage dma or fifo hardware.
277 mutex_lock(&bitbang->lock); in spi_bitbang_prepare_hardware()
278 bitbang->busy = 1; in spi_bitbang_prepare_hardware()
279 mutex_unlock(&bitbang->lock); in spi_bitbang_prepare_hardware()
291 if (bitbang->setup_transfer) { in spi_bitbang_transfer_one()
292 status = bitbang->setup_transfer(spi, transfer); in spi_bitbang_transfer_one()
297 if (transfer->len) in spi_bitbang_transfer_one()
298 status = bitbang->txrx_bufs(spi, transfer); in spi_bitbang_transfer_one()
300 if (status == transfer->len) in spi_bitbang_transfer_one()
303 status = -EREMOTEIO; in spi_bitbang_transfer_one()
317 mutex_lock(&bitbang->lock); in spi_bitbang_unprepare_hardware()
318 bitbang->busy = 0; in spi_bitbang_unprepare_hardware()
319 mutex_unlock(&bitbang->lock); in spi_bitbang_unprepare_hardware()
326 struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master); in spi_bitbang_set_cs()
328 /* SPI core provides CS high / low, but bitbang driver in spi_bitbang_set_cs()
329 * expects CS active in spi_bitbang_set_cs()
332 enable = (!!(spi->mode & SPI_CS_HIGH) == enable); in spi_bitbang_set_cs()
335 bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE : in spi_bitbang_set_cs()
340 /*----------------------------------------------------------------------*/
344 struct spi_master *master = bitbang->master; in spi_bitbang_init()
348 return -EINVAL; in spi_bitbang_init()
353 * driver-specific chipselect routine. in spi_bitbang_init()
355 custom_cs = (!master->use_gpio_descriptors || in spi_bitbang_init()
356 (master->flags & SPI_CONTROLLER_GPIO_SS)); in spi_bitbang_init()
358 if (custom_cs && !bitbang->chipselect) in spi_bitbang_init()
359 return -EINVAL; in spi_bitbang_init()
361 mutex_init(&bitbang->lock); in spi_bitbang_init()
363 if (!master->mode_bits) in spi_bitbang_init()
364 master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags; in spi_bitbang_init()
366 if (master->transfer || master->transfer_one_message) in spi_bitbang_init()
367 return -EINVAL; in spi_bitbang_init()
369 master->prepare_transfer_hardware = spi_bitbang_prepare_hardware; in spi_bitbang_init()
370 master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware; in spi_bitbang_init()
371 master->transfer_one = spi_bitbang_transfer_one; in spi_bitbang_init()
373 * When using GPIO descriptors, the ->set_cs() callback doesn't even in spi_bitbang_init()
377 master->set_cs = spi_bitbang_set_cs; in spi_bitbang_init()
379 if (!bitbang->txrx_bufs) { in spi_bitbang_init()
380 bitbang->use_dma = 0; in spi_bitbang_init()
381 bitbang->txrx_bufs = spi_bitbang_bufs; in spi_bitbang_init()
382 if (!master->setup) { in spi_bitbang_init()
383 if (!bitbang->setup_transfer) in spi_bitbang_init()
384 bitbang->setup_transfer = in spi_bitbang_init()
386 master->setup = spi_bitbang_setup; in spi_bitbang_init()
387 master->cleanup = spi_bitbang_cleanup; in spi_bitbang_init()
396 * spi_bitbang_start - start up a polled/bitbanging SPI master driver
399 * Caller should have zero-initialized all parts of the structure, and then
405 * For i/o loops, provide callbacks either per-word (for bitbanging, or for
406 * hardware that basically exposes a shift register) or per-spi_transfer
409 * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
411 * master methods. Those methods are the defaults if the bitbang->txrx_bufs
425 struct spi_master *master = bitbang->master; in spi_bitbang_start()
444 * spi_bitbang_stop - stops the task providing spi communication
448 spi_unregister_master(bitbang->master); in spi_bitbang_stop()