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 * spi->controller_data ... holds chipselect GPIO 39ca632f55SGrant Likely * 40ca632f55SGrant Likely * spi->master->dev.driver_data ... points to spi_gpio->bitbang 41ca632f55SGrant Likely */ 42ca632f55SGrant Likely 43ca632f55SGrant Likely struct spi_gpio { 44ca632f55SGrant Likely struct spi_bitbang bitbang; 45ca632f55SGrant Likely struct spi_gpio_platform_data pdata; 46ca632f55SGrant Likely struct platform_device *pdev; 479b00bc7bSLinus Walleij struct gpio_desc *sck; 489b00bc7bSLinus Walleij struct gpio_desc *miso; 499b00bc7bSLinus Walleij struct gpio_desc *mosi; 509b00bc7bSLinus Walleij struct gpio_desc **cs_gpios; 519b00bc7bSLinus Walleij bool has_cs; 52ca632f55SGrant Likely }; 53ca632f55SGrant Likely 54ca632f55SGrant Likely /*----------------------------------------------------------------------*/ 55ca632f55SGrant Likely 56ca632f55SGrant Likely /* 57ca632f55SGrant Likely * Because the overhead of going through four GPIO procedure calls 58ca632f55SGrant Likely * per transferred bit can make performance a problem, this code 59ca632f55SGrant Likely * is set up so that you can use it in either of two ways: 60ca632f55SGrant Likely * 61ca632f55SGrant Likely * - The slow generic way: set up platform_data to hold the GPIO 62ca632f55SGrant Likely * numbers used for MISO/MOSI/SCK, and issue procedure calls for 63ca632f55SGrant Likely * each of them. This driver can handle several such busses. 64ca632f55SGrant Likely * 65ca632f55SGrant Likely * - The quicker inlined way: only helps with platform GPIO code 66ca632f55SGrant Likely * that inlines operations for constant GPIOs. This can give 67ca632f55SGrant Likely * you tight (fast!) inner loops, but each such bus needs a 68ca632f55SGrant Likely * new driver. You'll define a new C file, with Makefile and 69ca632f55SGrant Likely * Kconfig support; the C code can be a total of six lines: 70ca632f55SGrant Likely * 71ca632f55SGrant Likely * #define DRIVER_NAME "myboard_spi2" 72ca632f55SGrant Likely * #define SPI_MISO_GPIO 119 73ca632f55SGrant Likely * #define SPI_MOSI_GPIO 120 74ca632f55SGrant Likely * #define SPI_SCK_GPIO 121 75ca632f55SGrant Likely * #define SPI_N_CHIPSEL 4 76ca632f55SGrant Likely * #include "spi-gpio.c" 77ca632f55SGrant Likely */ 78ca632f55SGrant Likely 79ca632f55SGrant Likely #ifndef DRIVER_NAME 80ca632f55SGrant Likely #define DRIVER_NAME "spi_gpio" 81ca632f55SGrant Likely 82ca632f55SGrant Likely #define GENERIC_BITBANG /* vs tight inlines */ 83ca632f55SGrant Likely 84ca632f55SGrant Likely #endif 85ca632f55SGrant Likely 86ca632f55SGrant Likely /*----------------------------------------------------------------------*/ 87ca632f55SGrant Likely 88161c2dd3SDaniel Mack static inline struct spi_gpio *__pure 89161c2dd3SDaniel Mack spi_to_spi_gpio(const struct spi_device *spi) 90ca632f55SGrant Likely { 91ca632f55SGrant Likely const struct spi_bitbang *bang; 92161c2dd3SDaniel Mack struct spi_gpio *spi_gpio; 93ca632f55SGrant Likely 94ca632f55SGrant Likely bang = spi_master_get_devdata(spi->master); 95ca632f55SGrant Likely spi_gpio = container_of(bang, struct spi_gpio, bitbang); 96161c2dd3SDaniel Mack return spi_gpio; 97161c2dd3SDaniel Mack } 98161c2dd3SDaniel Mack 99161c2dd3SDaniel Mack static inline struct spi_gpio_platform_data *__pure 100161c2dd3SDaniel Mack spi_to_pdata(const struct spi_device *spi) 101161c2dd3SDaniel Mack { 102161c2dd3SDaniel Mack return &spi_to_spi_gpio(spi)->pdata; 103ca632f55SGrant Likely } 104ca632f55SGrant Likely 1059b00bc7bSLinus Walleij /* These helpers are in turn called by the bitbang inlines */ 106ca632f55SGrant Likely static inline void setsck(const struct spi_device *spi, int is_on) 107ca632f55SGrant Likely { 1089b00bc7bSLinus Walleij struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 1099b00bc7bSLinus Walleij 1109b00bc7bSLinus Walleij gpiod_set_value_cansleep(spi_gpio->sck, is_on); 111ca632f55SGrant Likely } 112ca632f55SGrant Likely 113ca632f55SGrant Likely static inline void setmosi(const struct spi_device *spi, int is_on) 114ca632f55SGrant Likely { 1159b00bc7bSLinus Walleij struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 1169b00bc7bSLinus Walleij 1179b00bc7bSLinus Walleij gpiod_set_value_cansleep(spi_gpio->mosi, is_on); 118ca632f55SGrant Likely } 119ca632f55SGrant Likely 120ca632f55SGrant Likely static inline int getmiso(const struct spi_device *spi) 121ca632f55SGrant Likely { 1229b00bc7bSLinus Walleij struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 123ca632f55SGrant Likely 1249b00bc7bSLinus Walleij return !!gpiod_get_value_cansleep(spi_gpio->miso); 1259b00bc7bSLinus Walleij } 126ca632f55SGrant Likely 127ca632f55SGrant Likely /* 128ca632f55SGrant Likely * NOTE: this clocks "as fast as we can". It "should" be a function of the 129ca632f55SGrant Likely * requested device clock. Software overhead means we usually have trouble 130ca632f55SGrant Likely * reaching even one Mbit/sec (except when we can inline bitops), so for now 131ca632f55SGrant Likely * we'll just assume we never need additional per-bit slowdowns. 132ca632f55SGrant Likely */ 133ca632f55SGrant Likely #define spidelay(nsecs) do {} while (0) 134ca632f55SGrant Likely 135ca632f55SGrant Likely #include "spi-bitbang-txrx.h" 136ca632f55SGrant Likely 137ca632f55SGrant Likely /* 138ca632f55SGrant Likely * These functions can leverage inline expansion of GPIO calls to shrink 139ca632f55SGrant Likely * costs for a txrx bit, often by factors of around ten (by instruction 140ca632f55SGrant Likely * count). That is particularly visible for larger word sizes, but helps 141ca632f55SGrant Likely * even with default 8-bit words. 142ca632f55SGrant Likely * 143ca632f55SGrant Likely * REVISIT overheads calling these functions for each word also have 144ca632f55SGrant Likely * significant performance costs. Having txrx_bufs() calls that inline 145ca632f55SGrant Likely * the txrx_word() logic would help performance, e.g. on larger blocks 146ca632f55SGrant Likely * used with flash storage or MMC/SD. There should also be ways to make 147ca632f55SGrant Likely * GCC be less stupid about reloading registers inside the I/O loops, 148ca632f55SGrant Likely * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3? 149ca632f55SGrant Likely */ 150ca632f55SGrant Likely 151ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, 152ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 153ca632f55SGrant Likely { 154ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits); 155ca632f55SGrant Likely } 156ca632f55SGrant Likely 157ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, 158ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 159ca632f55SGrant Likely { 160ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits); 161ca632f55SGrant Likely } 162ca632f55SGrant Likely 163ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, 164ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 165ca632f55SGrant Likely { 166ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits); 167ca632f55SGrant Likely } 168ca632f55SGrant Likely 169ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, 170ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 171ca632f55SGrant Likely { 172ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits); 173ca632f55SGrant Likely } 174ca632f55SGrant Likely 175ca632f55SGrant Likely /* 176ca632f55SGrant Likely * These functions do not call setmosi or getmiso if respective flag 177ca632f55SGrant Likely * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to 178ca632f55SGrant Likely * call when such pin is not present or defined in the controller. 179ca632f55SGrant Likely * A separate set of callbacks is defined to get highest possible 180ca632f55SGrant Likely * speed in the generic case (when both MISO and MOSI lines are 181ca632f55SGrant Likely * available), as optimiser will remove the checks when argument is 182ca632f55SGrant Likely * constant. 183ca632f55SGrant Likely */ 184ca632f55SGrant Likely 185ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, 186ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 187ca632f55SGrant Likely { 188ca632f55SGrant Likely unsigned flags = spi->master->flags; 189ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); 190ca632f55SGrant Likely } 191ca632f55SGrant Likely 192ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, 193ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 194ca632f55SGrant Likely { 195ca632f55SGrant Likely unsigned flags = spi->master->flags; 196ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); 197ca632f55SGrant Likely } 198ca632f55SGrant Likely 199ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, 200ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 201ca632f55SGrant Likely { 202ca632f55SGrant Likely unsigned flags = spi->master->flags; 203ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); 204ca632f55SGrant Likely } 205ca632f55SGrant Likely 206ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, 207ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 208ca632f55SGrant Likely { 209ca632f55SGrant Likely unsigned flags = spi->master->flags; 210ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); 211ca632f55SGrant Likely } 212ca632f55SGrant Likely 213ca632f55SGrant Likely /*----------------------------------------------------------------------*/ 214ca632f55SGrant Likely 215ca632f55SGrant Likely static void spi_gpio_chipselect(struct spi_device *spi, int is_active) 216ca632f55SGrant Likely { 217161c2dd3SDaniel Mack struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 218ca632f55SGrant Likely 2199b00bc7bSLinus Walleij /* set initial clock line level */ 220ca632f55SGrant Likely if (is_active) 2219b00bc7bSLinus Walleij gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL); 222ca632f55SGrant Likely 2239b00bc7bSLinus Walleij /* Drive chip select line, if we have one */ 2249b00bc7bSLinus Walleij if (spi_gpio->has_cs) { 2259b00bc7bSLinus Walleij struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select]; 2269b00bc7bSLinus Walleij 2279b00bc7bSLinus Walleij /* SPI chip selects are normally active-low */ 2289b00bc7bSLinus Walleij gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); 229ca632f55SGrant Likely } 230ca632f55SGrant Likely } 231ca632f55SGrant Likely 232ca632f55SGrant Likely static int spi_gpio_setup(struct spi_device *spi) 233ca632f55SGrant Likely { 2349b00bc7bSLinus Walleij struct gpio_desc *cs; 235ca632f55SGrant Likely int status = 0; 236161c2dd3SDaniel Mack struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 237ca632f55SGrant Likely 23838ab18caSDaniel Mack /* 2399b00bc7bSLinus Walleij * The CS GPIOs have already been 2409b00bc7bSLinus Walleij * initialized from the descriptor lookup. 24138ab18caSDaniel Mack */ 24238ab18caSDaniel Mack cs = spi_gpio->cs_gpios[spi->chip_select]; 2439b00bc7bSLinus Walleij if (!spi->controller_state && cs) 2449b00bc7bSLinus Walleij status = gpiod_direction_output(cs, 24505644147SUwe Kleine-König !(spi->mode & SPI_CS_HIGH)); 246161c2dd3SDaniel Mack 2479b00bc7bSLinus Walleij if (!status) 2489b00bc7bSLinus Walleij status = spi_bitbang_setup(spi); 2499b00bc7bSLinus Walleij 250ca632f55SGrant Likely return status; 251ca632f55SGrant Likely } 252ca632f55SGrant Likely 253ca632f55SGrant Likely static void spi_gpio_cleanup(struct spi_device *spi) 254ca632f55SGrant Likely { 255ca632f55SGrant Likely spi_bitbang_cleanup(spi); 256ca632f55SGrant Likely } 257ca632f55SGrant Likely 2589b00bc7bSLinus Walleij /* 2599b00bc7bSLinus Walleij * It can be convenient to use this driver with pins that have alternate 2609b00bc7bSLinus Walleij * functions associated with a "native" SPI controller if a driver for that 2619b00bc7bSLinus Walleij * controller is not available, or is missing important functionality. 2629b00bc7bSLinus Walleij * 2639b00bc7bSLinus Walleij * On platforms which can do so, configure MISO with a weak pullup unless 2649b00bc7bSLinus Walleij * there's an external pullup on that signal. That saves power by avoiding 2659b00bc7bSLinus Walleij * floating signals. (A weak pulldown would save power too, but many 2669b00bc7bSLinus Walleij * drivers expect to see all-ones data as the no slave "response".) 2679b00bc7bSLinus Walleij */ 2689b00bc7bSLinus Walleij static int spi_gpio_request(struct device *dev, 2699b00bc7bSLinus Walleij struct spi_gpio *spi_gpio, 2709b00bc7bSLinus Walleij unsigned int num_chipselects, 2719b00bc7bSLinus Walleij u16 *mflags) 272ca632f55SGrant Likely { 2739b00bc7bSLinus Walleij int i; 274ca632f55SGrant Likely 2759b00bc7bSLinus Walleij spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); 2769b00bc7bSLinus Walleij if (IS_ERR(spi_gpio->mosi)) 2779b00bc7bSLinus Walleij return PTR_ERR(spi_gpio->mosi); 2789b00bc7bSLinus Walleij if (!spi_gpio->mosi) 279ca632f55SGrant Likely /* HW configuration without MOSI pin */ 2809b00bc7bSLinus Walleij *mflags |= SPI_MASTER_NO_TX; 281ca632f55SGrant Likely 2829b00bc7bSLinus Walleij spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); 2839b00bc7bSLinus Walleij if (IS_ERR(spi_gpio->miso)) 2849b00bc7bSLinus Walleij return PTR_ERR(spi_gpio->miso); 2859b00bc7bSLinus Walleij if (!spi_gpio->miso) 286ca632f55SGrant Likely /* HW configuration without MISO pin */ 2879b00bc7bSLinus Walleij *mflags |= SPI_MASTER_NO_RX; 2889b00bc7bSLinus Walleij 2899b00bc7bSLinus Walleij spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); 2909b00bc7bSLinus Walleij if (IS_ERR(spi_gpio->mosi)) 2919b00bc7bSLinus Walleij return PTR_ERR(spi_gpio->mosi); 2929b00bc7bSLinus Walleij 2939b00bc7bSLinus Walleij for (i = 0; i < num_chipselects; i++) { 2949b00bc7bSLinus Walleij spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", 2959b00bc7bSLinus Walleij i, GPIOD_OUT_HIGH); 2969b00bc7bSLinus Walleij if (IS_ERR(spi_gpio->cs_gpios[i])) 2979b00bc7bSLinus Walleij return PTR_ERR(spi_gpio->cs_gpios[i]); 298ca632f55SGrant Likely } 299ca632f55SGrant Likely 3009b00bc7bSLinus Walleij return 0; 301ca632f55SGrant Likely } 302ca632f55SGrant Likely 30338ab18caSDaniel Mack #ifdef CONFIG_OF 304d9e15281SJingoo Han static const struct of_device_id spi_gpio_dt_ids[] = { 30538ab18caSDaniel Mack { .compatible = "spi-gpio" }, 30638ab18caSDaniel Mack {} 30738ab18caSDaniel Mack }; 30838ab18caSDaniel Mack MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids); 30938ab18caSDaniel Mack 31038ab18caSDaniel Mack static int spi_gpio_probe_dt(struct platform_device *pdev) 31138ab18caSDaniel Mack { 31238ab18caSDaniel Mack int ret; 31338ab18caSDaniel Mack u32 tmp; 31438ab18caSDaniel Mack struct spi_gpio_platform_data *pdata; 31538ab18caSDaniel Mack struct device_node *np = pdev->dev.of_node; 31638ab18caSDaniel Mack const struct of_device_id *of_id = 31738ab18caSDaniel Mack of_match_device(spi_gpio_dt_ids, &pdev->dev); 31838ab18caSDaniel Mack 31938ab18caSDaniel Mack if (!of_id) 32038ab18caSDaniel Mack return 0; 32138ab18caSDaniel Mack 32238ab18caSDaniel Mack pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 32338ab18caSDaniel Mack if (!pdata) 32438ab18caSDaniel Mack return -ENOMEM; 32538ab18caSDaniel Mack 32638ab18caSDaniel Mack 32738ab18caSDaniel Mack ret = of_property_read_u32(np, "num-chipselects", &tmp); 32838ab18caSDaniel Mack if (ret < 0) { 32938ab18caSDaniel Mack dev_err(&pdev->dev, "num-chipselects property not found\n"); 33038ab18caSDaniel Mack goto error_free; 33138ab18caSDaniel Mack } 33238ab18caSDaniel Mack 33338ab18caSDaniel Mack pdata->num_chipselect = tmp; 33438ab18caSDaniel Mack pdev->dev.platform_data = pdata; 33538ab18caSDaniel Mack 33638ab18caSDaniel Mack return 1; 33738ab18caSDaniel Mack 33838ab18caSDaniel Mack error_free: 33938ab18caSDaniel Mack devm_kfree(&pdev->dev, pdata); 34038ab18caSDaniel Mack return ret; 34138ab18caSDaniel Mack } 34238ab18caSDaniel Mack #else 343ac2cb30bSMark Brown static inline int spi_gpio_probe_dt(struct platform_device *pdev) 34438ab18caSDaniel Mack { 34538ab18caSDaniel Mack return 0; 34638ab18caSDaniel Mack } 34738ab18caSDaniel Mack #endif 34838ab18caSDaniel Mack 349fd4a319bSGrant Likely static int spi_gpio_probe(struct platform_device *pdev) 350ca632f55SGrant Likely { 351ca632f55SGrant Likely int status; 352ca632f55SGrant Likely struct spi_master *master; 353ca632f55SGrant Likely struct spi_gpio *spi_gpio; 354ca632f55SGrant Likely struct spi_gpio_platform_data *pdata; 355ca632f55SGrant Likely u16 master_flags = 0; 35638ab18caSDaniel Mack bool use_of = 0; 35738ab18caSDaniel Mack 35838ab18caSDaniel Mack status = spi_gpio_probe_dt(pdev); 35938ab18caSDaniel Mack if (status < 0) 36038ab18caSDaniel Mack return status; 36138ab18caSDaniel Mack if (status > 0) 36238ab18caSDaniel Mack use_of = 1; 363ca632f55SGrant Likely 3648074cf06SJingoo Han pdata = dev_get_platdata(&pdev->dev); 365ca632f55SGrant Likely #ifdef GENERIC_BITBANG 366d1d81802STorsten Fleischer if (!pdata || (!use_of && !pdata->num_chipselect)) 367ca632f55SGrant Likely return -ENODEV; 368ca632f55SGrant Likely #endif 369ca632f55SGrant Likely 3709b00bc7bSLinus Walleij master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio)); 3719b00bc7bSLinus Walleij if (!master) 3729b00bc7bSLinus Walleij return -ENOMEM; 373d1d81802STorsten Fleischer 374ca632f55SGrant Likely spi_gpio = spi_master_get_devdata(master); 3759b00bc7bSLinus Walleij 376*a86854d0SKees Cook spi_gpio->cs_gpios = devm_kcalloc(&pdev->dev, 377*a86854d0SKees Cook pdata->num_chipselect, 378*a86854d0SKees Cook sizeof(*spi_gpio->cs_gpios), 3799b00bc7bSLinus Walleij GFP_KERNEL); 3809b00bc7bSLinus Walleij if (!spi_gpio->cs_gpios) 3819b00bc7bSLinus Walleij return -ENOMEM; 3829b00bc7bSLinus Walleij 383ca632f55SGrant Likely platform_set_drvdata(pdev, spi_gpio); 384ca632f55SGrant Likely 3859b00bc7bSLinus Walleij /* Determine if we have chip selects connected */ 3869b00bc7bSLinus Walleij spi_gpio->has_cs = !!pdata->num_chipselect; 3879b00bc7bSLinus Walleij 388ca632f55SGrant Likely spi_gpio->pdev = pdev; 389ca632f55SGrant Likely if (pdata) 390ca632f55SGrant Likely spi_gpio->pdata = *pdata; 391ca632f55SGrant Likely 3929b00bc7bSLinus Walleij status = spi_gpio_request(&pdev->dev, spi_gpio, 3939b00bc7bSLinus Walleij pdata->num_chipselect, &master_flags); 3949b00bc7bSLinus Walleij if (status) 3959b00bc7bSLinus Walleij return status; 3969b00bc7bSLinus Walleij 39724778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); 398ca632f55SGrant Likely master->flags = master_flags; 399ca632f55SGrant Likely master->bus_num = pdev->id; 4009b00bc7bSLinus Walleij /* The master needs to think there is a chipselect even if not connected */ 4019b00bc7bSLinus Walleij master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1; 402ca632f55SGrant Likely master->setup = spi_gpio_setup; 403ca632f55SGrant Likely master->cleanup = spi_gpio_cleanup; 40438ab18caSDaniel Mack #ifdef CONFIG_OF 40538ab18caSDaniel Mack master->dev.of_node = pdev->dev.of_node; 40638ab18caSDaniel Mack #endif 407ca632f55SGrant Likely 40894c69f76SAxel Lin spi_gpio->bitbang.master = master; 409ca632f55SGrant Likely spi_gpio->bitbang.chipselect = spi_gpio_chipselect; 410ca632f55SGrant Likely 411ca632f55SGrant Likely if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) { 412ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; 413ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; 414ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; 415ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; 416ca632f55SGrant Likely } else { 417ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; 418ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; 419ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; 420ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; 421ca632f55SGrant Likely } 422ca632f55SGrant Likely spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; 423ca632f55SGrant Likely spi_gpio->bitbang.flags = SPI_CS_HIGH; 424ca632f55SGrant Likely 425ca632f55SGrant Likely status = spi_bitbang_start(&spi_gpio->bitbang); 4269b00bc7bSLinus Walleij if (status) 427ca632f55SGrant Likely spi_master_put(master); 428ca632f55SGrant Likely 429ca632f55SGrant Likely return status; 430ca632f55SGrant Likely } 431ca632f55SGrant Likely 432fd4a319bSGrant Likely static int spi_gpio_remove(struct platform_device *pdev) 433ca632f55SGrant Likely { 434ca632f55SGrant Likely struct spi_gpio *spi_gpio; 435ca632f55SGrant Likely struct spi_gpio_platform_data *pdata; 436ca632f55SGrant Likely 437ca632f55SGrant Likely spi_gpio = platform_get_drvdata(pdev); 4388074cf06SJingoo Han pdata = dev_get_platdata(&pdev->dev); 439ca632f55SGrant Likely 440ca632f55SGrant Likely /* stop() unregisters child devices too */ 441d9721ae1SAxel Lin spi_bitbang_stop(&spi_gpio->bitbang); 442ca632f55SGrant Likely 44394c69f76SAxel Lin spi_master_put(spi_gpio->bitbang.master); 444ca632f55SGrant Likely 445d9721ae1SAxel Lin return 0; 446ca632f55SGrant Likely } 447ca632f55SGrant Likely 448ca632f55SGrant Likely MODULE_ALIAS("platform:" DRIVER_NAME); 449ca632f55SGrant Likely 450ca632f55SGrant Likely static struct platform_driver spi_gpio_driver = { 45138ab18caSDaniel Mack .driver = { 45238ab18caSDaniel Mack .name = DRIVER_NAME, 45338ab18caSDaniel Mack .of_match_table = of_match_ptr(spi_gpio_dt_ids), 45438ab18caSDaniel Mack }, 455940ab889SGrant Likely .probe = spi_gpio_probe, 456fd4a319bSGrant Likely .remove = spi_gpio_remove, 457ca632f55SGrant Likely }; 458940ab889SGrant Likely module_platform_driver(spi_gpio_driver); 459ca632f55SGrant Likely 460ca632f55SGrant Likely MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO "); 461ca632f55SGrant Likely MODULE_AUTHOR("David Brownell"); 462ca632f55SGrant Likely MODULE_LICENSE("GPL"); 463