1ca632f55SGrant Likely /* 2ca632f55SGrant Likely * SPI master driver using generic bitbanged GPIO 3ca632f55SGrant Likely * 4ca632f55SGrant Likely * Copyright (C) 2006,2008 David Brownell 5ca632f55SGrant Likely * 6ca632f55SGrant Likely * This program is free software; you can redistribute it and/or modify 7ca632f55SGrant Likely * it under the terms of the GNU General Public License as published by 8ca632f55SGrant Likely * the Free Software Foundation; either version 2 of the License, or 9ca632f55SGrant Likely * (at your option) any later version. 10ca632f55SGrant Likely * 11ca632f55SGrant Likely * This program is distributed in the hope that it will be useful, 12ca632f55SGrant Likely * but WITHOUT ANY WARRANTY; without even the implied warranty of 13ca632f55SGrant Likely * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14ca632f55SGrant Likely * GNU General Public License for more details. 15ca632f55SGrant Likely * 16ca632f55SGrant Likely * You should have received a copy of the GNU General Public License 17ca632f55SGrant Likely * along with this program; if not, write to the Free Software 18ca632f55SGrant Likely * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19ca632f55SGrant Likely */ 20ca632f55SGrant Likely #include <linux/kernel.h> 21d7614de4SPaul Gortmaker #include <linux/module.h> 22ca632f55SGrant Likely #include <linux/platform_device.h> 23ca632f55SGrant Likely #include <linux/gpio.h> 24ecc77773SSachin Kamat #include <linux/of.h> 2538ab18caSDaniel Mack #include <linux/of_device.h> 2638ab18caSDaniel Mack #include <linux/of_gpio.h> 27ca632f55SGrant Likely 28ca632f55SGrant Likely #include <linux/spi/spi.h> 29ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h> 30ca632f55SGrant Likely #include <linux/spi/spi_gpio.h> 31ca632f55SGrant Likely 32ca632f55SGrant Likely 33ca632f55SGrant Likely /* 34ca632f55SGrant Likely * This bitbanging SPI master driver should help make systems usable 35ca632f55SGrant Likely * when a native hardware SPI engine is not available, perhaps because 36ca632f55SGrant Likely * its driver isn't yet working or because the I/O pins it requires 37ca632f55SGrant Likely * are used for other purposes. 38ca632f55SGrant Likely * 39ca632f55SGrant Likely * platform_device->driver_data ... points to spi_gpio 40ca632f55SGrant Likely * 41ca632f55SGrant Likely * spi->controller_state ... reserved for bitbang framework code 42ca632f55SGrant Likely * spi->controller_data ... holds chipselect GPIO 43ca632f55SGrant Likely * 44ca632f55SGrant Likely * spi->master->dev.driver_data ... points to spi_gpio->bitbang 45ca632f55SGrant Likely */ 46ca632f55SGrant Likely 47ca632f55SGrant Likely struct spi_gpio { 48ca632f55SGrant Likely struct spi_bitbang bitbang; 49ca632f55SGrant Likely struct spi_gpio_platform_data pdata; 50ca632f55SGrant Likely struct platform_device *pdev; 51161c2dd3SDaniel Mack int cs_gpios[0]; 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 /* all functions referencing these symbols must define pdata */ 85ca632f55SGrant Likely #define SPI_MISO_GPIO ((pdata)->miso) 86ca632f55SGrant Likely #define SPI_MOSI_GPIO ((pdata)->mosi) 87ca632f55SGrant Likely #define SPI_SCK_GPIO ((pdata)->sck) 88ca632f55SGrant Likely 89ca632f55SGrant Likely #define SPI_N_CHIPSEL ((pdata)->num_chipselect) 90ca632f55SGrant Likely 91ca632f55SGrant Likely #endif 92ca632f55SGrant Likely 93ca632f55SGrant Likely /*----------------------------------------------------------------------*/ 94ca632f55SGrant Likely 95161c2dd3SDaniel Mack static inline struct spi_gpio * __pure 96161c2dd3SDaniel Mack spi_to_spi_gpio(const struct spi_device *spi) 97ca632f55SGrant Likely { 98ca632f55SGrant Likely const struct spi_bitbang *bang; 99161c2dd3SDaniel Mack struct spi_gpio *spi_gpio; 100ca632f55SGrant Likely 101ca632f55SGrant Likely bang = spi_master_get_devdata(spi->master); 102ca632f55SGrant Likely spi_gpio = container_of(bang, struct spi_gpio, bitbang); 103161c2dd3SDaniel Mack return spi_gpio; 104161c2dd3SDaniel Mack } 105161c2dd3SDaniel Mack 106161c2dd3SDaniel Mack static inline struct spi_gpio_platform_data * __pure 107161c2dd3SDaniel Mack spi_to_pdata(const struct spi_device *spi) 108161c2dd3SDaniel Mack { 109161c2dd3SDaniel Mack return &spi_to_spi_gpio(spi)->pdata; 110ca632f55SGrant Likely } 111ca632f55SGrant Likely 112ca632f55SGrant Likely /* this is #defined to avoid unused-variable warnings when inlining */ 113ca632f55SGrant Likely #define pdata spi_to_pdata(spi) 114ca632f55SGrant Likely 115ca632f55SGrant Likely static inline void setsck(const struct spi_device *spi, int is_on) 116ca632f55SGrant Likely { 117d9dda5a1SEzequiel Garcia gpio_set_value_cansleep(SPI_SCK_GPIO, is_on); 118ca632f55SGrant Likely } 119ca632f55SGrant Likely 120ca632f55SGrant Likely static inline void setmosi(const struct spi_device *spi, int is_on) 121ca632f55SGrant Likely { 122d9dda5a1SEzequiel Garcia gpio_set_value_cansleep(SPI_MOSI_GPIO, is_on); 123ca632f55SGrant Likely } 124ca632f55SGrant Likely 125ca632f55SGrant Likely static inline int getmiso(const struct spi_device *spi) 126ca632f55SGrant Likely { 127d9dda5a1SEzequiel Garcia return !!gpio_get_value_cansleep(SPI_MISO_GPIO); 128ca632f55SGrant Likely } 129ca632f55SGrant Likely 130ca632f55SGrant Likely #undef pdata 131ca632f55SGrant Likely 132ca632f55SGrant Likely /* 133ca632f55SGrant Likely * NOTE: this clocks "as fast as we can". It "should" be a function of the 134ca632f55SGrant Likely * requested device clock. Software overhead means we usually have trouble 135ca632f55SGrant Likely * reaching even one Mbit/sec (except when we can inline bitops), so for now 136ca632f55SGrant Likely * we'll just assume we never need additional per-bit slowdowns. 137ca632f55SGrant Likely */ 138ca632f55SGrant Likely #define spidelay(nsecs) do {} while (0) 139ca632f55SGrant Likely 140ca632f55SGrant Likely #include "spi-bitbang-txrx.h" 141ca632f55SGrant Likely 142ca632f55SGrant Likely /* 143ca632f55SGrant Likely * These functions can leverage inline expansion of GPIO calls to shrink 144ca632f55SGrant Likely * costs for a txrx bit, often by factors of around ten (by instruction 145ca632f55SGrant Likely * count). That is particularly visible for larger word sizes, but helps 146ca632f55SGrant Likely * even with default 8-bit words. 147ca632f55SGrant Likely * 148ca632f55SGrant Likely * REVISIT overheads calling these functions for each word also have 149ca632f55SGrant Likely * significant performance costs. Having txrx_bufs() calls that inline 150ca632f55SGrant Likely * the txrx_word() logic would help performance, e.g. on larger blocks 151ca632f55SGrant Likely * used with flash storage or MMC/SD. There should also be ways to make 152ca632f55SGrant Likely * GCC be less stupid about reloading registers inside the I/O loops, 153ca632f55SGrant Likely * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3? 154ca632f55SGrant Likely */ 155ca632f55SGrant Likely 156ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, 157ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 158ca632f55SGrant Likely { 159ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits); 160ca632f55SGrant Likely } 161ca632f55SGrant Likely 162ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, 163ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 164ca632f55SGrant Likely { 165ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits); 166ca632f55SGrant Likely } 167ca632f55SGrant Likely 168ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, 169ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 170ca632f55SGrant Likely { 171ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits); 172ca632f55SGrant Likely } 173ca632f55SGrant Likely 174ca632f55SGrant Likely static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, 175ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 176ca632f55SGrant Likely { 177ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits); 178ca632f55SGrant Likely } 179ca632f55SGrant Likely 180ca632f55SGrant Likely /* 181ca632f55SGrant Likely * These functions do not call setmosi or getmiso if respective flag 182ca632f55SGrant Likely * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to 183ca632f55SGrant Likely * call when such pin is not present or defined in the controller. 184ca632f55SGrant Likely * A separate set of callbacks is defined to get highest possible 185ca632f55SGrant Likely * speed in the generic case (when both MISO and MOSI lines are 186ca632f55SGrant Likely * available), as optimiser will remove the checks when argument is 187ca632f55SGrant Likely * constant. 188ca632f55SGrant Likely */ 189ca632f55SGrant Likely 190ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, 191ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 192ca632f55SGrant Likely { 193ca632f55SGrant Likely unsigned flags = spi->master->flags; 194ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); 195ca632f55SGrant Likely } 196ca632f55SGrant Likely 197ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, 198ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 199ca632f55SGrant Likely { 200ca632f55SGrant Likely unsigned flags = spi->master->flags; 201ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); 202ca632f55SGrant Likely } 203ca632f55SGrant Likely 204ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, 205ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 206ca632f55SGrant Likely { 207ca632f55SGrant Likely unsigned flags = spi->master->flags; 208ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); 209ca632f55SGrant Likely } 210ca632f55SGrant Likely 211ca632f55SGrant Likely static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, 212ca632f55SGrant Likely unsigned nsecs, u32 word, u8 bits) 213ca632f55SGrant Likely { 214ca632f55SGrant Likely unsigned flags = spi->master->flags; 215ca632f55SGrant Likely return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); 216ca632f55SGrant Likely } 217ca632f55SGrant Likely 218ca632f55SGrant Likely /*----------------------------------------------------------------------*/ 219ca632f55SGrant Likely 220ca632f55SGrant Likely static void spi_gpio_chipselect(struct spi_device *spi, int is_active) 221ca632f55SGrant Likely { 222161c2dd3SDaniel Mack struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 223161c2dd3SDaniel Mack unsigned int cs = spi_gpio->cs_gpios[spi->chip_select]; 224ca632f55SGrant Likely 225ca632f55SGrant Likely /* set initial clock polarity */ 226ca632f55SGrant Likely if (is_active) 227ca632f55SGrant Likely setsck(spi, spi->mode & SPI_CPOL); 228ca632f55SGrant Likely 229ca632f55SGrant Likely if (cs != SPI_GPIO_NO_CHIPSELECT) { 230ca632f55SGrant Likely /* SPI is normally active-low */ 231d9dda5a1SEzequiel Garcia gpio_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); 232ca632f55SGrant Likely } 233ca632f55SGrant Likely } 234ca632f55SGrant Likely 235ca632f55SGrant Likely static int spi_gpio_setup(struct spi_device *spi) 236ca632f55SGrant Likely { 23738ab18caSDaniel Mack unsigned int cs; 238ca632f55SGrant Likely int status = 0; 239161c2dd3SDaniel Mack struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 24038ab18caSDaniel Mack struct device_node *np = spi->master->dev.of_node; 241ca632f55SGrant Likely 24238ab18caSDaniel Mack if (np) { 24338ab18caSDaniel Mack /* 24438ab18caSDaniel Mack * In DT environments, the CS GPIOs have already been 24538ab18caSDaniel Mack * initialized from the "cs-gpios" property of the node. 24638ab18caSDaniel Mack */ 24738ab18caSDaniel Mack cs = spi_gpio->cs_gpios[spi->chip_select]; 24838ab18caSDaniel Mack } else { 24938ab18caSDaniel Mack /* 25038ab18caSDaniel Mack * ... otherwise, take it from spi->controller_data 25138ab18caSDaniel Mack */ 252e1bde3b1SSeongJae Park cs = (unsigned int)(uintptr_t) spi->controller_data; 25338ab18caSDaniel Mack } 25438ab18caSDaniel Mack 255ca632f55SGrant Likely if (!spi->controller_state) { 256ca632f55SGrant Likely if (cs != SPI_GPIO_NO_CHIPSELECT) { 257ca632f55SGrant Likely status = gpio_request(cs, dev_name(&spi->dev)); 258ca632f55SGrant Likely if (status) 259ca632f55SGrant Likely return status; 26005644147SUwe Kleine-König status = gpio_direction_output(cs, 26105644147SUwe Kleine-König !(spi->mode & SPI_CS_HIGH)); 262ca632f55SGrant Likely } 263ca632f55SGrant Likely } 264161c2dd3SDaniel Mack if (!status) { 26538ab18caSDaniel Mack /* in case it was initialized from static board data */ 266161c2dd3SDaniel Mack spi_gpio->cs_gpios[spi->chip_select] = cs; 2676b8cc330SJosef Ahmad status = spi_bitbang_setup(spi); 268161c2dd3SDaniel Mack } 269161c2dd3SDaniel Mack 270ca632f55SGrant Likely if (status) { 271ca632f55SGrant Likely if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT) 272ca632f55SGrant Likely gpio_free(cs); 273ca632f55SGrant Likely } 274ca632f55SGrant Likely return status; 275ca632f55SGrant Likely } 276ca632f55SGrant Likely 277ca632f55SGrant Likely static void spi_gpio_cleanup(struct spi_device *spi) 278ca632f55SGrant Likely { 279161c2dd3SDaniel Mack struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 280161c2dd3SDaniel Mack unsigned int cs = spi_gpio->cs_gpios[spi->chip_select]; 281ca632f55SGrant Likely 282ca632f55SGrant Likely if (cs != SPI_GPIO_NO_CHIPSELECT) 283ca632f55SGrant Likely gpio_free(cs); 284ca632f55SGrant Likely spi_bitbang_cleanup(spi); 285ca632f55SGrant Likely } 286ca632f55SGrant Likely 287fd4a319bSGrant Likely static int spi_gpio_alloc(unsigned pin, const char *label, bool is_in) 288ca632f55SGrant Likely { 289ca632f55SGrant Likely int value; 290ca632f55SGrant Likely 291ca632f55SGrant Likely value = gpio_request(pin, label); 292ca632f55SGrant Likely if (value == 0) { 293ca632f55SGrant Likely if (is_in) 294ca632f55SGrant Likely value = gpio_direction_input(pin); 295ca632f55SGrant Likely else 296ca632f55SGrant Likely value = gpio_direction_output(pin, 0); 297ca632f55SGrant Likely } 298ca632f55SGrant Likely return value; 299ca632f55SGrant Likely } 300ca632f55SGrant Likely 301fd4a319bSGrant Likely static int spi_gpio_request(struct spi_gpio_platform_data *pdata, 302fd4a319bSGrant Likely const char *label, u16 *res_flags) 303ca632f55SGrant Likely { 304ca632f55SGrant Likely int value; 305ca632f55SGrant Likely 306ca632f55SGrant Likely /* NOTE: SPI_*_GPIO symbols may reference "pdata" */ 307ca632f55SGrant Likely 308ca632f55SGrant Likely if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) { 309ca632f55SGrant Likely value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false); 310ca632f55SGrant Likely if (value) 311ca632f55SGrant Likely goto done; 312ca632f55SGrant Likely } else { 313ca632f55SGrant Likely /* HW configuration without MOSI pin */ 314ca632f55SGrant Likely *res_flags |= SPI_MASTER_NO_TX; 315ca632f55SGrant Likely } 316ca632f55SGrant Likely 317ca632f55SGrant Likely if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) { 318ca632f55SGrant Likely value = spi_gpio_alloc(SPI_MISO_GPIO, label, true); 319ca632f55SGrant Likely if (value) 320ca632f55SGrant Likely goto free_mosi; 321ca632f55SGrant Likely } else { 322ca632f55SGrant Likely /* HW configuration without MISO pin */ 323ca632f55SGrant Likely *res_flags |= SPI_MASTER_NO_RX; 324ca632f55SGrant Likely } 325ca632f55SGrant Likely 326ca632f55SGrant Likely value = spi_gpio_alloc(SPI_SCK_GPIO, label, false); 327ca632f55SGrant Likely if (value) 328ca632f55SGrant Likely goto free_miso; 329ca632f55SGrant Likely 330ca632f55SGrant Likely goto done; 331ca632f55SGrant Likely 332ca632f55SGrant Likely free_miso: 333ca632f55SGrant Likely if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 334ca632f55SGrant Likely gpio_free(SPI_MISO_GPIO); 335ca632f55SGrant Likely free_mosi: 336ca632f55SGrant Likely if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 337ca632f55SGrant Likely gpio_free(SPI_MOSI_GPIO); 338ca632f55SGrant Likely done: 339ca632f55SGrant Likely return value; 340ca632f55SGrant Likely } 341ca632f55SGrant Likely 34238ab18caSDaniel Mack #ifdef CONFIG_OF 343*d9e15281SJingoo Han static const struct of_device_id spi_gpio_dt_ids[] = { 34438ab18caSDaniel Mack { .compatible = "spi-gpio" }, 34538ab18caSDaniel Mack {} 34638ab18caSDaniel Mack }; 34738ab18caSDaniel Mack MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids); 34838ab18caSDaniel Mack 34938ab18caSDaniel Mack static int spi_gpio_probe_dt(struct platform_device *pdev) 35038ab18caSDaniel Mack { 35138ab18caSDaniel Mack int ret; 35238ab18caSDaniel Mack u32 tmp; 35338ab18caSDaniel Mack struct spi_gpio_platform_data *pdata; 35438ab18caSDaniel Mack struct device_node *np = pdev->dev.of_node; 35538ab18caSDaniel Mack const struct of_device_id *of_id = 35638ab18caSDaniel Mack of_match_device(spi_gpio_dt_ids, &pdev->dev); 35738ab18caSDaniel Mack 35838ab18caSDaniel Mack if (!of_id) 35938ab18caSDaniel Mack return 0; 36038ab18caSDaniel Mack 36138ab18caSDaniel Mack pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 36238ab18caSDaniel Mack if (!pdata) 36338ab18caSDaniel Mack return -ENOMEM; 36438ab18caSDaniel Mack 3650202a32dSMaxime Ripard ret = of_get_named_gpio(np, "gpio-sck", 0); 3660202a32dSMaxime Ripard if (ret < 0) { 3670202a32dSMaxime Ripard dev_err(&pdev->dev, "gpio-sck property not found\n"); 3680202a32dSMaxime Ripard goto error_free; 3690202a32dSMaxime Ripard } 3700202a32dSMaxime Ripard pdata->sck = ret; 3710202a32dSMaxime Ripard 3720202a32dSMaxime Ripard ret = of_get_named_gpio(np, "gpio-miso", 0); 3730202a32dSMaxime Ripard if (ret < 0) { 3740202a32dSMaxime Ripard dev_info(&pdev->dev, "gpio-miso property not found, switching to no-rx mode\n"); 3750202a32dSMaxime Ripard pdata->miso = SPI_GPIO_NO_MISO; 3760202a32dSMaxime Ripard } else 3770202a32dSMaxime Ripard pdata->miso = ret; 3780202a32dSMaxime Ripard 3790202a32dSMaxime Ripard ret = of_get_named_gpio(np, "gpio-mosi", 0); 3800202a32dSMaxime Ripard if (ret < 0) { 3810202a32dSMaxime Ripard dev_info(&pdev->dev, "gpio-mosi property not found, switching to no-tx mode\n"); 3820202a32dSMaxime Ripard pdata->mosi = SPI_GPIO_NO_MOSI; 3830202a32dSMaxime Ripard } else 3840202a32dSMaxime Ripard pdata->mosi = ret; 38538ab18caSDaniel Mack 38638ab18caSDaniel Mack ret = of_property_read_u32(np, "num-chipselects", &tmp); 38738ab18caSDaniel Mack if (ret < 0) { 38838ab18caSDaniel Mack dev_err(&pdev->dev, "num-chipselects property not found\n"); 38938ab18caSDaniel Mack goto error_free; 39038ab18caSDaniel Mack } 39138ab18caSDaniel Mack 39238ab18caSDaniel Mack pdata->num_chipselect = tmp; 39338ab18caSDaniel Mack pdev->dev.platform_data = pdata; 39438ab18caSDaniel Mack 39538ab18caSDaniel Mack return 1; 39638ab18caSDaniel Mack 39738ab18caSDaniel Mack error_free: 39838ab18caSDaniel Mack devm_kfree(&pdev->dev, pdata); 39938ab18caSDaniel Mack return ret; 40038ab18caSDaniel Mack } 40138ab18caSDaniel Mack #else 402ac2cb30bSMark Brown static inline int spi_gpio_probe_dt(struct platform_device *pdev) 40338ab18caSDaniel Mack { 40438ab18caSDaniel Mack return 0; 40538ab18caSDaniel Mack } 40638ab18caSDaniel Mack #endif 40738ab18caSDaniel Mack 408fd4a319bSGrant Likely static int spi_gpio_probe(struct platform_device *pdev) 409ca632f55SGrant Likely { 410ca632f55SGrant Likely int status; 411ca632f55SGrant Likely struct spi_master *master; 412ca632f55SGrant Likely struct spi_gpio *spi_gpio; 413ca632f55SGrant Likely struct spi_gpio_platform_data *pdata; 414ca632f55SGrant Likely u16 master_flags = 0; 41538ab18caSDaniel Mack bool use_of = 0; 41638ab18caSDaniel Mack 41738ab18caSDaniel Mack status = spi_gpio_probe_dt(pdev); 41838ab18caSDaniel Mack if (status < 0) 41938ab18caSDaniel Mack return status; 42038ab18caSDaniel Mack if (status > 0) 42138ab18caSDaniel Mack use_of = 1; 422ca632f55SGrant Likely 4238074cf06SJingoo Han pdata = dev_get_platdata(&pdev->dev); 424ca632f55SGrant Likely #ifdef GENERIC_BITBANG 425ca632f55SGrant Likely if (!pdata || !pdata->num_chipselect) 426ca632f55SGrant Likely return -ENODEV; 427ca632f55SGrant Likely #endif 428ca632f55SGrant Likely 429ca632f55SGrant Likely status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags); 430ca632f55SGrant Likely if (status < 0) 431ca632f55SGrant Likely return status; 432ca632f55SGrant Likely 433161c2dd3SDaniel Mack master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) + 434161c2dd3SDaniel Mack (sizeof(int) * SPI_N_CHIPSEL)); 435ca632f55SGrant Likely if (!master) { 436ca632f55SGrant Likely status = -ENOMEM; 437ca632f55SGrant Likely goto gpio_free; 438ca632f55SGrant Likely } 439ca632f55SGrant Likely spi_gpio = spi_master_get_devdata(master); 440ca632f55SGrant Likely platform_set_drvdata(pdev, spi_gpio); 441ca632f55SGrant Likely 442ca632f55SGrant Likely spi_gpio->pdev = pdev; 443ca632f55SGrant Likely if (pdata) 444ca632f55SGrant Likely spi_gpio->pdata = *pdata; 445ca632f55SGrant Likely 44624778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); 447ca632f55SGrant Likely master->flags = master_flags; 448ca632f55SGrant Likely master->bus_num = pdev->id; 449ca632f55SGrant Likely master->num_chipselect = SPI_N_CHIPSEL; 450ca632f55SGrant Likely master->setup = spi_gpio_setup; 451ca632f55SGrant Likely master->cleanup = spi_gpio_cleanup; 45238ab18caSDaniel Mack #ifdef CONFIG_OF 45338ab18caSDaniel Mack master->dev.of_node = pdev->dev.of_node; 45438ab18caSDaniel Mack 45538ab18caSDaniel Mack if (use_of) { 45638ab18caSDaniel Mack int i; 45738ab18caSDaniel Mack struct device_node *np = pdev->dev.of_node; 45838ab18caSDaniel Mack 45938ab18caSDaniel Mack /* 46038ab18caSDaniel Mack * In DT environments, take the CS GPIO from the "cs-gpios" 46138ab18caSDaniel Mack * property of the node. 46238ab18caSDaniel Mack */ 46338ab18caSDaniel Mack 46438ab18caSDaniel Mack for (i = 0; i < SPI_N_CHIPSEL; i++) 46538ab18caSDaniel Mack spi_gpio->cs_gpios[i] = 46638ab18caSDaniel Mack of_get_named_gpio(np, "cs-gpios", i); 46738ab18caSDaniel Mack } 46838ab18caSDaniel Mack #endif 469ca632f55SGrant Likely 47094c69f76SAxel Lin spi_gpio->bitbang.master = master; 471ca632f55SGrant Likely spi_gpio->bitbang.chipselect = spi_gpio_chipselect; 472ca632f55SGrant Likely 473ca632f55SGrant Likely if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) { 474ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; 475ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; 476ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; 477ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; 478ca632f55SGrant Likely } else { 479ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; 480ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; 481ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; 482ca632f55SGrant Likely spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; 483ca632f55SGrant Likely } 484ca632f55SGrant Likely spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; 485ca632f55SGrant Likely spi_gpio->bitbang.flags = SPI_CS_HIGH; 486ca632f55SGrant Likely 487ca632f55SGrant Likely status = spi_bitbang_start(&spi_gpio->bitbang); 488ca632f55SGrant Likely if (status < 0) { 489ca632f55SGrant Likely gpio_free: 490ca632f55SGrant Likely if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 491ca632f55SGrant Likely gpio_free(SPI_MISO_GPIO); 492ca632f55SGrant Likely if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 493ca632f55SGrant Likely gpio_free(SPI_MOSI_GPIO); 494ca632f55SGrant Likely gpio_free(SPI_SCK_GPIO); 495ca632f55SGrant Likely spi_master_put(master); 496ca632f55SGrant Likely } 497ca632f55SGrant Likely 498ca632f55SGrant Likely return status; 499ca632f55SGrant Likely } 500ca632f55SGrant Likely 501fd4a319bSGrant Likely static int spi_gpio_remove(struct platform_device *pdev) 502ca632f55SGrant Likely { 503ca632f55SGrant Likely struct spi_gpio *spi_gpio; 504ca632f55SGrant Likely struct spi_gpio_platform_data *pdata; 505ca632f55SGrant Likely 506ca632f55SGrant Likely spi_gpio = platform_get_drvdata(pdev); 5078074cf06SJingoo Han pdata = dev_get_platdata(&pdev->dev); 508ca632f55SGrant Likely 509ca632f55SGrant Likely /* stop() unregisters child devices too */ 510d9721ae1SAxel Lin spi_bitbang_stop(&spi_gpio->bitbang); 511ca632f55SGrant Likely 512ca632f55SGrant Likely if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 513ca632f55SGrant Likely gpio_free(SPI_MISO_GPIO); 514ca632f55SGrant Likely if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 515ca632f55SGrant Likely gpio_free(SPI_MOSI_GPIO); 516ca632f55SGrant Likely gpio_free(SPI_SCK_GPIO); 51794c69f76SAxel Lin spi_master_put(spi_gpio->bitbang.master); 518ca632f55SGrant Likely 519d9721ae1SAxel Lin return 0; 520ca632f55SGrant Likely } 521ca632f55SGrant Likely 522ca632f55SGrant Likely MODULE_ALIAS("platform:" DRIVER_NAME); 523ca632f55SGrant Likely 524ca632f55SGrant Likely static struct platform_driver spi_gpio_driver = { 52538ab18caSDaniel Mack .driver = { 52638ab18caSDaniel Mack .name = DRIVER_NAME, 52738ab18caSDaniel Mack .owner = THIS_MODULE, 52838ab18caSDaniel Mack .of_match_table = of_match_ptr(spi_gpio_dt_ids), 52938ab18caSDaniel Mack }, 530940ab889SGrant Likely .probe = spi_gpio_probe, 531fd4a319bSGrant Likely .remove = spi_gpio_remove, 532ca632f55SGrant Likely }; 533940ab889SGrant Likely module_platform_driver(spi_gpio_driver); 534ca632f55SGrant Likely 535ca632f55SGrant Likely MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO "); 536ca632f55SGrant Likely MODULE_AUTHOR("David Brownell"); 537ca632f55SGrant Likely MODULE_LICENSE("GPL"); 538