// SPDX-License-Identifier: GPL-2.0-or-later /* * Polling/bitbanging SPI host controller controller driver utilities */ #include <linux/spinlock.h> #include <linux/workqueue.h> #include <linux/interrupt.h> #include <linux/module.h> #include <linux/delay.h> #include <linux/errno.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/time64.h> #include <linux/spi/spi.h> #include <linux/spi/spi_bitbang.h> #define SPI_BITBANG_CS_DELAY … /*----------------------------------------------------------------------*/ /* * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support. * Use this for GPIO or shift-register level hardware APIs. * * spi_bitbang_cs is in spi_device->controller_state, which is unavailable * to glue code. These bitbang setup() and cleanup() routines are always * used, though maybe they're called from controller-aware code. * * chipselect() and friends may use spi_device->controller_data and * controller registers as appropriate. * * * NOTE: SPI controller pins can often be used as GPIO pins instead, * which means you could use a bitbang driver either to get hardware * working quickly, or testing for differences that aren't speed related. */ spi_bb_txrx_bufs_fn; struct spi_bitbang_cs { … }; static unsigned int bitbang_txrx_8(struct spi_device *spi, spi_bb_txrx_word_fn txrx_word, unsigned int ns, struct spi_transfer *t, unsigned int flags) { … } static unsigned int bitbang_txrx_16(struct spi_device *spi, spi_bb_txrx_word_fn txrx_word, unsigned int ns, struct spi_transfer *t, unsigned int flags) { … } static unsigned int bitbang_txrx_32(struct spi_device *spi, spi_bb_txrx_word_fn txrx_word, unsigned int ns, struct spi_transfer *t, unsigned int flags) { … } int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t) { … } EXPORT_SYMBOL_GPL(…); /* * spi_bitbang_setup - default setup for per-word I/O loops */ int spi_bitbang_setup(struct spi_device *spi) { … } EXPORT_SYMBOL_GPL(…); /* * spi_bitbang_cleanup - default cleanup for per-word I/O loops */ void spi_bitbang_cleanup(struct spi_device *spi) { … } EXPORT_SYMBOL_GPL(…); static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t) { … } /*----------------------------------------------------------------------*/ /* * SECOND PART ... simple transfer queue runner. * * This costs a task context per controller, running the queue by * performing each transfer in sequence. Smarter hardware can queue * several DMA transfers at once, and process several controller queues * in parallel; this driver doesn't match such hardware very well. * * Drivers can provide word-at-a-time i/o primitives, or provide * transfer-at-a-time ones to leverage dma or fifo hardware. */ static int spi_bitbang_prepare_hardware(struct spi_controller *spi) { … } static int spi_bitbang_transfer_one(struct spi_controller *ctlr, struct spi_device *spi, struct spi_transfer *transfer) { … } static int spi_bitbang_unprepare_hardware(struct spi_controller *spi) { … } static void spi_bitbang_set_cs(struct spi_device *spi, bool enable) { … } /*----------------------------------------------------------------------*/ int spi_bitbang_init(struct spi_bitbang *bitbang) { … } EXPORT_SYMBOL_GPL(…); /** * spi_bitbang_start - start up a polled/bitbanging SPI host controller driver * @bitbang: driver handle * * Caller should have zero-initialized all parts of the structure, and then * provided callbacks for chip selection and I/O loops. If the host controller has * a transfer method, its final step should call spi_bitbang_transfer(); or, * that's the default if the transfer routine is not initialized. It should * also set up the bus number and number of chipselects. * * For i/o loops, provide callbacks either per-word (for bitbanging, or for * hardware that basically exposes a shift register) or per-spi_transfer * (which takes better advantage of hardware like fifos or DMA engines). * * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup(), * spi_bitbang_cleanup() and spi_bitbang_setup_transfer() to handle those SPI * host controller methods. Those methods are the defaults if the bitbang->txrx_bufs * routine isn't initialized. * * This routine registers the spi_controller, which will process requests in a * dedicated task, keeping IRQs unblocked most of the time. To stop * processing those requests, call spi_bitbang_stop(). * * On success, this routine will take a reference to the controller. The caller * is responsible for calling spi_bitbang_stop() to decrement the reference and * spi_controller_put() as counterpart of spi_alloc_host() to prevent a memory * leak. */ int spi_bitbang_start(struct spi_bitbang *bitbang) { … } EXPORT_SYMBOL_GPL(…); /* * spi_bitbang_stop - stops the task providing spi communication */ void spi_bitbang_stop(struct spi_bitbang *bitbang) { … } EXPORT_SYMBOL_GPL(…); MODULE_LICENSE(…) …; MODULE_DESCRIPTION(…) …;