1ca632f55SGrant Likely /* 2ca632f55SGrant Likely * SPI master driver using generic bitbanged GPIO 3ca632f55SGrant Likely * 4ca632f55SGrant Likely * Copyright (C) 2006,2008 David Brownell 59b00bc7bSLinus Walleij * Copyright (C) 2017 Linus Walleij 6ca632f55SGrant Likely * 7ca632f55SGrant Likely * This program is free software; you can redistribute it and/or modify 8ca632f55SGrant Likely * it under the terms of the GNU General Public License as published by 9ca632f55SGrant Likely * the Free Software Foundation; either version 2 of the License, or 10ca632f55SGrant Likely * (at your option) any later version. 11ca632f55SGrant Likely * 12ca632f55SGrant Likely * This program is distributed in the hope that it will be useful, 13ca632f55SGrant Likely * but WITHOUT ANY WARRANTY; without even the implied warranty of 14ca632f55SGrant Likely * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15ca632f55SGrant Likely * GNU General Public License for more details. 16ca632f55SGrant Likely */ 17ca632f55SGrant Likely #include <linux/kernel.h> 18d7614de4SPaul Gortmaker #include <linux/module.h> 19ca632f55SGrant Likely #include <linux/platform_device.h> 209b00bc7bSLinus Walleij #include <linux/gpio/consumer.h> 21ecc77773SSachin Kamat #include <linux/of.h> 2238ab18caSDaniel Mack #include <linux/of_device.h> 23ca632f55SGrant Likely 24ca632f55SGrant Likely #include <linux/spi/spi.h> 25ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h> 26ca632f55SGrant Likely #include <linux/spi/spi_gpio.h> 27ca632f55SGrant Likely 28ca632f55SGrant Likely 29ca632f55SGrant Likely /* 30ca632f55SGrant Likely * This bitbanging SPI master driver should help make systems usable 31ca632f55SGrant Likely * when a native hardware SPI engine is not available, perhaps because 32ca632f55SGrant Likely * its driver isn't yet working or because the I/O pins it requires 33ca632f55SGrant Likely * are used for other purposes. 34ca632f55SGrant Likely * 35ca632f55SGrant Likely * platform_device->driver_data ... points to spi_gpio 36ca632f55SGrant Likely * 37ca632f55SGrant Likely * spi->controller_state ... reserved for bitbang framework code 38ca632f55SGrant Likely * 39ca632f55SGrant Likely * spi->master->dev.driver_data ... points to spi_gpio->bitbang 40ca632f55SGrant Likely */ 41ca632f55SGrant Likely 42ca632f55SGrant Likely struct spi_gpio { 43ca632f55SGrant Likely struct spi_bitbang bitbang; 44ca632f55SGrant Likely struct platform_device *pdev; 459b00bc7bSLinus Walleij struct gpio_desc *sck; 469b00bc7bSLinus Walleij struct gpio_desc *miso; 479b00bc7bSLinus Walleij struct gpio_desc *mosi; 489b00bc7bSLinus Walleij struct gpio_desc **cs_gpios; 49ca632f55SGrant Likely }; 50ca632f55SGrant Likely 51ca632f55SGrant Likely /*----------------------------------------------------------------------*/ 52ca632f55SGrant Likely 53ca632f55SGrant Likely /* 54ca632f55SGrant Likely * Because the overhead of going through four GPIO procedure calls 55ca632f55SGrant Likely * per transferred bit can make performance a problem, this code 56ca632f55SGrant Likely * is set up so that you can use it in either of two ways: 57ca632f55SGrant Likely * 58ca632f55SGrant Likely * - The slow generic way: set up platform_data to hold the GPIO 59ca632f55SGrant Likely * numbers used for MISO/MOSI/SCK, and issue procedure calls for 60ca632f55SGrant Likely * each of them. This driver can handle several such busses. 61ca632f55SGrant Likely * 62ca632f55SGrant Likely * - The quicker inlined way: only helps with platform GPIO code 63ca632f55SGrant Likely * that inlines operations for constant GPIOs. This can give 64ca632f55SGrant Likely * you tight (fast!) inner loops, but each such bus needs a 65ca632f55SGrant Likely * new driver. You'll define a new C file, with Makefile and 66ca632f55SGrant Likely * Kconfig support; the C code can be a total of six lines: 67ca632f55SGrant Likely * 68ca632f55SGrant Likely * #define DRIVER_NAME "myboard_spi2" 69ca632f55SGrant Likely * #define SPI_MISO_GPIO 119 70ca632f55SGrant Likely * #define SPI_MOSI_GPIO 120 71ca632f55SGrant Likely * #define SPI_SCK_GPIO 121 72ca632f55SGrant Likely * #define SPI_N_CHIPSEL 4 73ca632f55SGrant Likely * #include "spi-gpio.c" 74ca632f55SGrant Likely */ 75ca632f55SGrant Likely 76ca632f55SGrant Likely #ifndef DRIVER_NAME 77ca632f55SGrant Likely #define DRIVER_NAME "spi_gpio" 78ca632f55SGrant Likely 79ca632f55SGrant Likely #define GENERIC_BITBANG /* vs tight inlines */ 80ca632f55SGrant Likely 81ca632f55SGrant Likely #endif 82ca632f55SGrant Likely 83ca632f55SGrant Likely /*----------------------------------------------------------------------*/ 84ca632f55SGrant Likely 85161c2dd3SDaniel Mack static inline struct spi_gpio *__pure 86161c2dd3SDaniel Mack spi_to_spi_gpio(const struct spi_device *spi) 87ca632f55SGrant Likely { 88ca632f55SGrant Likely const struct spi_bitbang *bang; 89161c2dd3SDaniel Mack struct spi_gpio *spi_gpio; 90ca632f55SGrant Likely 91ca632f55SGrant Likely bang = spi_master_get_devdata(spi->master); 92ca632f55SGrant Likely spi_gpio = container_of(bang, struct spi_gpio, bitbang); 93161c2dd3SDaniel Mack return spi_gpio; 94161c2dd3SDaniel Mack } 95161c2dd3SDaniel Mack 969b00bc7bSLinus Walleij /* These helpers are in turn called by the bitbang inlines */ 97ca632f55SGrant Likely static inline void setsck(const struct spi_device *spi, int is_on) 98ca632f55SGrant Likely { 999b00bc7bSLinus Walleij struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 1009b00bc7bSLinus Walleij 1019b00bc7bSLinus Walleij gpiod_set_value_cansleep(spi_gpio->sck, is_on); 102ca632f55SGrant Likely } 103ca632f55SGrant Likely 104ca632f55SGrant Likely static inline void setmosi(const struct spi_device *spi, int is_on) 105ca632f55SGrant Likely { 1069b00bc7bSLinus Walleij struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 1079b00bc7bSLinus Walleij 1089b00bc7bSLinus Walleij gpiod_set_value_cansleep(spi_gpio->mosi, is_on); 109ca632f55SGrant Likely } 110ca632f55SGrant Likely 111ca632f55SGrant Likely static inline int getmiso(const struct spi_device *spi) 112ca632f55SGrant Likely { 1139b00bc7bSLinus Walleij struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 114ca632f55SGrant Likely 1154b859db2SLorenzo Bianconi if (spi->mode & SPI_3WIRE) 1164b859db2SLorenzo Bianconi return !!gpiod_get_value_cansleep(spi_gpio->mosi); 1174b859db2SLorenzo Bianconi else 1189b00bc7bSLinus Walleij return !!gpiod_get_value_cansleep(spi_gpio->miso); 1199b00bc7bSLinus Walleij } 120ca632f55SGrant Likely 121ca632f55SGrant Likely /* 122ca632f55SGrant Likely * NOTE: this clocks "as fast as we can". It "should" be a function of the 123ca632f55SGrant Likely * requested device clock. Software overhead means we usually have trouble 124ca632f55SGrant Likely * reaching even one Mbit/sec (except when we can inline bitops), so for now 125ca632f55SGrant Likely * we'll just assume we never need additional per-bit slowdowns. 126ca632f55SGrant Likely */ 127ca632f55SGrant Likely #define spidelay(nsecs) do {} while (0) 128ca632f55SGrant Likely 129ca632f55SGrant Likely #include "spi-bitbang-txrx.h" 130ca632f55SGrant Likely 131ca632f55SGrant Likely /* 132ca632f55SGrant Likely * These functions can leverage inline expansion of GPIO calls to shrink 133ca632f55SGrant Likely * costs for a txrx bit, often by factors of around ten (by instruction 134ca632f55SGrant Likely * count). That is particularly visible for larger word sizes, but helps 135ca632f55SGrant Likely * even with default 8-bit words. 136ca632f55SGrant Likely * 137ca632f55SGrant Likely * REVISIT overheads calling these functions for each word also have 138ca632f55SGrant Likely * significant performance costs. Having txrx_bufs() calls that inline 139ca632f55SGrant Likely * the txrx_word() logic would help performance, e.g. on larger blocks 140ca632f55SGrant Likely * used with flash storage or MMC/SD. There should also be ways to make 141ca632f55SGrant Likely * GCC be less stupid about reloading registers inside the I/O loops, 142ca632f55SGrant Likely * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3? 143ca632f55SGrant Likely */ 144ca632f55SGrant Likely 145ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, 146304d3436SLorenzo Bianconi unsigned nsecs, u32 word, u8 bits, unsigned flags) 147ca632f55SGrant Likely { 148304d3436SLorenzo Bianconi return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); 149ca632f55SGrant Likely } 150ca632f55SGrant Likely 151ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, 152304d3436SLorenzo Bianconi unsigned nsecs, u32 word, u8 bits, unsigned flags) 153ca632f55SGrant Likely { 154304d3436SLorenzo Bianconi return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); 155ca632f55SGrant Likely } 156ca632f55SGrant Likely 157ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, 158304d3436SLorenzo Bianconi unsigned nsecs, u32 word, u8 bits, unsigned flags) 159ca632f55SGrant Likely { 160304d3436SLorenzo Bianconi return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); 161ca632f55SGrant Likely } 162ca632f55SGrant Likely 163ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, 164304d3436SLorenzo Bianconi unsigned nsecs, u32 word, u8 bits, unsigned flags) 165ca632f55SGrant Likely { 166304d3436SLorenzo Bianconi return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); 167ca632f55SGrant Likely } 168ca632f55SGrant Likely 169ca632f55SGrant Likely /* 170ca632f55SGrant Likely * These functions do not call setmosi or getmiso if respective flag 171ca632f55SGrant Likely * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to 172ca632f55SGrant Likely * call when such pin is not present or defined in the controller. 173ca632f55SGrant Likely * A separate set of callbacks is defined to get highest possible 174ca632f55SGrant Likely * speed in the generic case (when both MISO and MOSI lines are 175ca632f55SGrant Likely * available), as optimiser will remove the checks when argument is 176ca632f55SGrant Likely * constant. 177ca632f55SGrant Likely */ 178ca632f55SGrant Likely 179ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, 180304d3436SLorenzo Bianconi unsigned nsecs, u32 word, u8 bits, unsigned flags) 181ca632f55SGrant Likely { 182304d3436SLorenzo Bianconi flags = spi->master->flags; 183ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); 184ca632f55SGrant Likely } 185ca632f55SGrant Likely 186ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, 187304d3436SLorenzo Bianconi unsigned nsecs, u32 word, u8 bits, unsigned flags) 188ca632f55SGrant Likely { 189304d3436SLorenzo Bianconi flags = spi->master->flags; 190ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); 191ca632f55SGrant Likely } 192ca632f55SGrant Likely 193ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, 194304d3436SLorenzo Bianconi unsigned nsecs, u32 word, u8 bits, unsigned flags) 195ca632f55SGrant Likely { 196304d3436SLorenzo Bianconi flags = spi->master->flags; 197ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); 198ca632f55SGrant Likely } 199ca632f55SGrant Likely 200ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, 201304d3436SLorenzo Bianconi unsigned nsecs, u32 word, u8 bits, unsigned flags) 202ca632f55SGrant Likely { 203304d3436SLorenzo Bianconi flags = spi->master->flags; 204ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); 205ca632f55SGrant Likely } 206ca632f55SGrant Likely 207ca632f55SGrant Likely /*----------------------------------------------------------------------*/ 208ca632f55SGrant Likely 209ca632f55SGrant Likely static void spi_gpio_chipselect(struct spi_device *spi, int is_active) 210ca632f55SGrant Likely { 211161c2dd3SDaniel Mack struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 212ca632f55SGrant Likely 2139b00bc7bSLinus Walleij /* set initial clock line level */ 214ca632f55SGrant Likely if (is_active) 2159b00bc7bSLinus Walleij gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL); 216ca632f55SGrant Likely 2179b00bc7bSLinus Walleij /* Drive chip select line, if we have one */ 218*249e2632SAndrey Smirnov if (spi_gpio->cs_gpios) { 2199b00bc7bSLinus Walleij struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select]; 2209b00bc7bSLinus Walleij 2219b00bc7bSLinus Walleij /* SPI chip selects are normally active-low */ 2229b00bc7bSLinus Walleij gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); 223ca632f55SGrant Likely } 224ca632f55SGrant Likely } 225ca632f55SGrant Likely 226ca632f55SGrant Likely static int spi_gpio_setup(struct spi_device *spi) 227ca632f55SGrant Likely { 2289b00bc7bSLinus Walleij struct gpio_desc *cs; 229ca632f55SGrant Likely int status = 0; 230161c2dd3SDaniel Mack struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 231ca632f55SGrant Likely 23238ab18caSDaniel Mack /* 2339b00bc7bSLinus Walleij * The CS GPIOs have already been 2349b00bc7bSLinus Walleij * initialized from the descriptor lookup. 23538ab18caSDaniel Mack */ 236*249e2632SAndrey Smirnov if (spi_gpio->cs_gpios) { 23738ab18caSDaniel Mack cs = spi_gpio->cs_gpios[spi->chip_select]; 2389b00bc7bSLinus Walleij if (!spi->controller_state && cs) 2399b00bc7bSLinus Walleij status = gpiod_direction_output(cs, 24005644147SUwe Kleine-König !(spi->mode & SPI_CS_HIGH)); 241*249e2632SAndrey Smirnov } 242161c2dd3SDaniel Mack 2439b00bc7bSLinus Walleij if (!status) 2449b00bc7bSLinus Walleij status = spi_bitbang_setup(spi); 2459b00bc7bSLinus Walleij 246ca632f55SGrant Likely return status; 247ca632f55SGrant Likely } 248ca632f55SGrant Likely 2494b859db2SLorenzo Bianconi static int spi_gpio_set_direction(struct spi_device *spi, bool output) 2504b859db2SLorenzo Bianconi { 2514b859db2SLorenzo Bianconi struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 2525132b3d2SLinus Walleij int ret; 2534b859db2SLorenzo Bianconi 2544b859db2SLorenzo Bianconi if (output) 2554b859db2SLorenzo Bianconi return gpiod_direction_output(spi_gpio->mosi, 1); 2565132b3d2SLinus Walleij 2575132b3d2SLinus Walleij ret = gpiod_direction_input(spi_gpio->mosi); 2585132b3d2SLinus Walleij if (ret) 2595132b3d2SLinus Walleij return ret; 2605132b3d2SLinus Walleij /* 2615132b3d2SLinus Walleij * Send a turnaround high impedance cycle when switching 2625132b3d2SLinus Walleij * from output to input. Theoretically there should be 2635132b3d2SLinus Walleij * a clock delay here, but as has been noted above, the 2645132b3d2SLinus Walleij * nsec delay function for bit-banged GPIO is simply 2655132b3d2SLinus Walleij * {} because bit-banging just doesn't get fast enough 2665132b3d2SLinus Walleij * anyway. 2675132b3d2SLinus Walleij */ 2685132b3d2SLinus Walleij if (spi->mode & SPI_3WIRE_HIZ) { 2695132b3d2SLinus Walleij gpiod_set_value_cansleep(spi_gpio->sck, 2705132b3d2SLinus Walleij !(spi->mode & SPI_CPOL)); 2715132b3d2SLinus Walleij gpiod_set_value_cansleep(spi_gpio->sck, 2725132b3d2SLinus Walleij !!(spi->mode & SPI_CPOL)); 2735132b3d2SLinus Walleij } 2745132b3d2SLinus Walleij return 0; 2754b859db2SLorenzo Bianconi } 2764b859db2SLorenzo Bianconi 277ca632f55SGrant Likely static void spi_gpio_cleanup(struct spi_device *spi) 278ca632f55SGrant Likely { 279ca632f55SGrant Likely spi_bitbang_cleanup(spi); 280ca632f55SGrant Likely } 281ca632f55SGrant Likely 2829b00bc7bSLinus Walleij /* 2839b00bc7bSLinus Walleij * It can be convenient to use this driver with pins that have alternate 2849b00bc7bSLinus Walleij * functions associated with a "native" SPI controller if a driver for that 2859b00bc7bSLinus Walleij * controller is not available, or is missing important functionality. 2869b00bc7bSLinus Walleij * 2879b00bc7bSLinus Walleij * On platforms which can do so, configure MISO with a weak pullup unless 2889b00bc7bSLinus Walleij * there's an external pullup on that signal. That saves power by avoiding 2899b00bc7bSLinus Walleij * floating signals. (A weak pulldown would save power too, but many 2909b00bc7bSLinus Walleij * drivers expect to see all-ones data as the no slave "response".) 2919b00bc7bSLinus Walleij */ 2929b00bc7bSLinus Walleij static int spi_gpio_request(struct device *dev, 2939b00bc7bSLinus Walleij struct spi_gpio *spi_gpio, 2949b00bc7bSLinus Walleij u16 *mflags) 295ca632f55SGrant Likely { 2969b00bc7bSLinus Walleij spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); 2979b00bc7bSLinus Walleij if (IS_ERR(spi_gpio->mosi)) 2989b00bc7bSLinus Walleij return PTR_ERR(spi_gpio->mosi); 2999b00bc7bSLinus Walleij if (!spi_gpio->mosi) 300ca632f55SGrant Likely /* HW configuration without MOSI pin */ 3019b00bc7bSLinus Walleij *mflags |= SPI_MASTER_NO_TX; 302ca632f55SGrant Likely 3039b00bc7bSLinus Walleij spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); 3049b00bc7bSLinus Walleij if (IS_ERR(spi_gpio->miso)) 3059b00bc7bSLinus Walleij return PTR_ERR(spi_gpio->miso); 306abf5feefSLinus Walleij /* 307abf5feefSLinus Walleij * No setting SPI_MASTER_NO_RX here - if there is only a MOSI 308abf5feefSLinus Walleij * pin connected the host can still do RX by changing the 309abf5feefSLinus Walleij * direction of the line. 310abf5feefSLinus Walleij */ 3119b00bc7bSLinus Walleij 3129b00bc7bSLinus Walleij spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); 3131723c315SLinus Walleij if (IS_ERR(spi_gpio->sck)) 3141723c315SLinus Walleij return PTR_ERR(spi_gpio->sck); 3159b00bc7bSLinus Walleij 3169b00bc7bSLinus Walleij return 0; 317ca632f55SGrant Likely } 318ca632f55SGrant Likely 31938ab18caSDaniel Mack #ifdef CONFIG_OF 320d9e15281SJingoo Han static const struct of_device_id spi_gpio_dt_ids[] = { 32138ab18caSDaniel Mack { .compatible = "spi-gpio" }, 32238ab18caSDaniel Mack {} 32338ab18caSDaniel Mack }; 32438ab18caSDaniel Mack MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids); 32538ab18caSDaniel Mack 326*249e2632SAndrey Smirnov static int spi_gpio_probe_dt(struct platform_device *pdev, 327*249e2632SAndrey Smirnov struct spi_master *master) 32838ab18caSDaniel Mack { 329*249e2632SAndrey Smirnov master->dev.of_node = pdev->dev.of_node; 330*249e2632SAndrey Smirnov master->use_gpio_descriptors = true; 33138ab18caSDaniel Mack 33238ab18caSDaniel Mack return 0; 33338ab18caSDaniel Mack } 33438ab18caSDaniel Mack #else 335*249e2632SAndrey Smirnov static inline int spi_gpio_probe_dt(struct platform_device *pdev, 336*249e2632SAndrey Smirnov struct spi_master *master) 33738ab18caSDaniel Mack { 33838ab18caSDaniel Mack return 0; 33938ab18caSDaniel Mack } 34038ab18caSDaniel Mack #endif 34138ab18caSDaniel Mack 342*249e2632SAndrey Smirnov static int spi_gpio_probe_pdata(struct platform_device *pdev, 343*249e2632SAndrey Smirnov struct spi_master *master) 344*249e2632SAndrey Smirnov { 345*249e2632SAndrey Smirnov struct device *dev = &pdev->dev; 346*249e2632SAndrey Smirnov struct spi_gpio_platform_data *pdata = dev_get_platdata(dev); 347*249e2632SAndrey Smirnov struct spi_gpio *spi_gpio = spi_master_get_devdata(master); 348*249e2632SAndrey Smirnov int i; 349*249e2632SAndrey Smirnov 350*249e2632SAndrey Smirnov #ifdef GENERIC_BITBANG 351*249e2632SAndrey Smirnov if (!pdata || !pdata->num_chipselect) 352*249e2632SAndrey Smirnov return -ENODEV; 353*249e2632SAndrey Smirnov #endif 354*249e2632SAndrey Smirnov /* 355*249e2632SAndrey Smirnov * The master needs to think there is a chipselect even if not 356*249e2632SAndrey Smirnov * connected 357*249e2632SAndrey Smirnov */ 358*249e2632SAndrey Smirnov master->num_chipselect = pdata->num_chipselect ?: 1; 359*249e2632SAndrey Smirnov 360*249e2632SAndrey Smirnov spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect, 361*249e2632SAndrey Smirnov sizeof(*spi_gpio->cs_gpios), 362*249e2632SAndrey Smirnov GFP_KERNEL); 363*249e2632SAndrey Smirnov if (!spi_gpio->cs_gpios) 364*249e2632SAndrey Smirnov return -ENOMEM; 365*249e2632SAndrey Smirnov 366*249e2632SAndrey Smirnov for (i = 0; i < master->num_chipselect; i++) { 367*249e2632SAndrey Smirnov spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i, 368*249e2632SAndrey Smirnov GPIOD_OUT_HIGH); 369*249e2632SAndrey Smirnov if (IS_ERR(spi_gpio->cs_gpios[i])) 370*249e2632SAndrey Smirnov return PTR_ERR(spi_gpio->cs_gpios[i]); 371*249e2632SAndrey Smirnov } 372*249e2632SAndrey Smirnov 373*249e2632SAndrey Smirnov return 0; 374*249e2632SAndrey Smirnov } 375*249e2632SAndrey Smirnov 376fd4a319bSGrant Likely static int spi_gpio_probe(struct platform_device *pdev) 377ca632f55SGrant Likely { 378ca632f55SGrant Likely int status; 379ca632f55SGrant Likely struct spi_master *master; 380ca632f55SGrant Likely struct spi_gpio *spi_gpio; 38196cad6d7SAndrey Smirnov struct device *dev = &pdev->dev; 38215dd0e9eSAndrey Smirnov struct spi_bitbang *bb; 383*249e2632SAndrey Smirnov const struct of_device_id *of_id; 384ca632f55SGrant Likely u16 master_flags = 0; 38538ab18caSDaniel Mack 386*249e2632SAndrey Smirnov of_id = of_match_device(spi_gpio_dt_ids, &pdev->dev); 387ca632f55SGrant Likely 38896cad6d7SAndrey Smirnov master = spi_alloc_master(dev, sizeof(*spi_gpio)); 3899b00bc7bSLinus Walleij if (!master) 3909b00bc7bSLinus Walleij return -ENOMEM; 391d1d81802STorsten Fleischer 392*249e2632SAndrey Smirnov if (of_id) 393*249e2632SAndrey Smirnov status = spi_gpio_probe_dt(pdev, master); 394*249e2632SAndrey Smirnov else 395*249e2632SAndrey Smirnov status = spi_gpio_probe_pdata(pdev, master); 3969b00bc7bSLinus Walleij 397*249e2632SAndrey Smirnov if (status) 398*249e2632SAndrey Smirnov return status; 399*249e2632SAndrey Smirnov 400*249e2632SAndrey Smirnov spi_gpio = spi_master_get_devdata(master); 4019b00bc7bSLinus Walleij 402ca632f55SGrant Likely platform_set_drvdata(pdev, spi_gpio); 403ca632f55SGrant Likely 404ca632f55SGrant Likely spi_gpio->pdev = pdev; 405ca632f55SGrant Likely 406*249e2632SAndrey Smirnov status = spi_gpio_request(dev, spi_gpio, &master_flags); 4079b00bc7bSLinus Walleij if (status) 4089b00bc7bSLinus Walleij return status; 4099b00bc7bSLinus Walleij 41024778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); 411b89fefdaSRussell King master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL | 412b89fefdaSRussell King SPI_CS_HIGH; 413ca632f55SGrant Likely master->flags = master_flags; 414ca632f55SGrant Likely master->bus_num = pdev->id; 415ca632f55SGrant Likely master->setup = spi_gpio_setup; 416ca632f55SGrant Likely master->cleanup = spi_gpio_cleanup; 417*249e2632SAndrey Smirnov 41815dd0e9eSAndrey Smirnov bb = &spi_gpio->bitbang; 41915dd0e9eSAndrey Smirnov bb->master = master; 42015dd0e9eSAndrey Smirnov bb->chipselect = spi_gpio_chipselect; 42115dd0e9eSAndrey Smirnov bb->set_line_direction = spi_gpio_set_direction; 422ca632f55SGrant Likely 42368cd9dc2SAndrey Smirnov if (master_flags & SPI_MASTER_NO_TX) { 42415dd0e9eSAndrey Smirnov bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; 42515dd0e9eSAndrey Smirnov bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; 42615dd0e9eSAndrey Smirnov bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; 42715dd0e9eSAndrey Smirnov bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; 42868cd9dc2SAndrey Smirnov } else { 42968cd9dc2SAndrey Smirnov bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; 43068cd9dc2SAndrey Smirnov bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; 43168cd9dc2SAndrey Smirnov bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; 43268cd9dc2SAndrey Smirnov bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; 433ca632f55SGrant Likely } 43415dd0e9eSAndrey Smirnov bb->setup_transfer = spi_bitbang_setup_transfer; 435ca632f55SGrant Likely 436ca632f55SGrant Likely status = spi_bitbang_start(&spi_gpio->bitbang); 4379b00bc7bSLinus Walleij if (status) 438ca632f55SGrant Likely spi_master_put(master); 439ca632f55SGrant Likely 440ca632f55SGrant Likely return status; 441ca632f55SGrant Likely } 442ca632f55SGrant Likely 443fd4a319bSGrant Likely static int spi_gpio_remove(struct platform_device *pdev) 444ca632f55SGrant Likely { 445ca632f55SGrant Likely struct spi_gpio *spi_gpio; 446ca632f55SGrant Likely 447ca632f55SGrant Likely spi_gpio = platform_get_drvdata(pdev); 448ca632f55SGrant Likely 449ca632f55SGrant Likely /* stop() unregisters child devices too */ 450d9721ae1SAxel Lin spi_bitbang_stop(&spi_gpio->bitbang); 451ca632f55SGrant Likely 45294c69f76SAxel Lin spi_master_put(spi_gpio->bitbang.master); 453ca632f55SGrant Likely 454d9721ae1SAxel Lin return 0; 455ca632f55SGrant Likely } 456ca632f55SGrant Likely 457ca632f55SGrant Likely MODULE_ALIAS("platform:" DRIVER_NAME); 458ca632f55SGrant Likely 459ca632f55SGrant Likely static struct platform_driver spi_gpio_driver = { 46038ab18caSDaniel Mack .driver = { 46138ab18caSDaniel Mack .name = DRIVER_NAME, 46238ab18caSDaniel Mack .of_match_table = of_match_ptr(spi_gpio_dt_ids), 46338ab18caSDaniel Mack }, 464940ab889SGrant Likely .probe = spi_gpio_probe, 465fd4a319bSGrant Likely .remove = spi_gpio_remove, 466ca632f55SGrant Likely }; 467940ab889SGrant Likely module_platform_driver(spi_gpio_driver); 468ca632f55SGrant Likely 469ca632f55SGrant Likely MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO "); 470ca632f55SGrant Likely MODULE_AUTHOR("David Brownell"); 471ca632f55SGrant Likely MODULE_LICENSE("GPL"); 472