// SPDX-License-Identifier: GPL-2.0-or-later /* * SPI host driver using generic bitbanged GPIO * * Copyright (C) 2006,2008 David Brownell * Copyright (C) 2017 Linus Walleij */ #include <linux/gpio/consumer.h> #include <linux/kernel.h> #include <linux/mod_devicetable.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/property.h> #include <linux/spi/spi.h> #include <linux/spi/spi_bitbang.h> #include <linux/spi/spi_gpio.h> /* * This bitbanging SPI host driver should help make systems usable * when a native hardware SPI engine is not available, perhaps because * its driver isn't yet working or because the I/O pins it requires * are used for other purposes. * * platform_device->driver_data ... points to spi_gpio * * spi->controller_state ... reserved for bitbang framework code * * spi->controller->dev.driver_data ... points to spi_gpio->bitbang */ struct spi_gpio { … }; /*----------------------------------------------------------------------*/ /* * Because the overhead of going through four GPIO procedure calls * per transferred bit can make performance a problem, this code * is set up so that you can use it in either of two ways: * * - The slow generic way: set up platform_data to hold the GPIO * numbers used for MISO/MOSI/SCK, and issue procedure calls for * each of them. This driver can handle several such busses. * * - The quicker inlined way: only helps with platform GPIO code * that inlines operations for constant GPIOs. This can give * you tight (fast!) inner loops, but each such bus needs a * new driver. You'll define a new C file, with Makefile and * Kconfig support; the C code can be a total of six lines: * * #define DRIVER_NAME "myboard_spi2" * #define SPI_MISO_GPIO 119 * #define SPI_MOSI_GPIO 120 * #define SPI_SCK_GPIO 121 * #define SPI_N_CHIPSEL 4 * #include "spi-gpio.c" */ #ifndef DRIVER_NAME #define DRIVER_NAME … #define GENERIC_BITBANG … #endif /*----------------------------------------------------------------------*/ static inline struct spi_gpio *__pure spi_to_spi_gpio(const struct spi_device *spi) { … } /* These helpers are in turn called by the bitbang inlines */ static inline void setsck(const struct spi_device *spi, int is_on) { … } static inline void setmosi(const struct spi_device *spi, int is_on) { … } static inline int getmiso(const struct spi_device *spi) { … } /* * NOTE: this clocks "as fast as we can". It "should" be a function of the * requested device clock. Software overhead means we usually have trouble * reaching even one Mbit/sec (except when we can inline bitops), so for now * we'll just assume we never need additional per-bit slowdowns. */ #define spidelay(nsecs) … #include "spi-bitbang-txrx.h" /* * These functions can leverage inline expansion of GPIO calls to shrink * costs for a txrx bit, often by factors of around ten (by instruction * count). That is particularly visible for larger word sizes, but helps * even with default 8-bit words. * * REVISIT overheads calling these functions for each word also have * significant performance costs. Having txrx_bufs() calls that inline * the txrx_word() logic would help performance, e.g. on larger blocks * used with flash storage or MMC/SD. There should also be ways to make * GCC be less stupid about reloading registers inside the I/O loops, * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3? */ static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags) { … } static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags) { … } static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags) { … } static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags) { … } /* * These functions do not call setmosi or getmiso if respective flag * (SPI_CONTROLLER_NO_RX or SPI_CONTROLLER_NO_TX) is set, so they are safe to * call when such pin is not present or defined in the controller. * A separate set of callbacks is defined to get highest possible * speed in the generic case (when both MISO and MOSI lines are * available), as optimiser will remove the checks when argument is * constant. */ static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags) { … } static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags) { … } static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags) { … } static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags) { … } /*----------------------------------------------------------------------*/ static void spi_gpio_chipselect(struct spi_device *spi, int is_active) { … } static int spi_gpio_setup(struct spi_device *spi) { … } static int spi_gpio_set_direction(struct spi_device *spi, bool output) { … } static void spi_gpio_cleanup(struct spi_device *spi) { … } /* * It can be convenient to use this driver with pins that have alternate * functions associated with a "native" SPI controller if a driver for that * controller is not available, or is missing important functionality. * * On platforms which can do so, configure MISO with a weak pullup unless * there's an external pullup on that signal. That saves power by avoiding * floating signals. (A weak pulldown would save power too, but many * drivers expect to see all-ones data as the no target "response".) */ static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio) { … } static int spi_gpio_probe_pdata(struct platform_device *pdev, struct spi_controller *host) { … } static int spi_gpio_probe(struct platform_device *pdev) { … } MODULE_ALIAS(…) …; static const struct of_device_id spi_gpio_dt_ids[] = …; MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids); static struct platform_driver spi_gpio_driver = …; module_platform_driver(…) …; MODULE_DESCRIPTION(…) …; MODULE_AUTHOR(…) …; MODULE_LICENSE(…) …;