David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 1 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 2 | * SPI master driver using generic bitbanged GPIO |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2006,2008 David Brownell |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 5 | * Copyright (C) 2017 Linus Walleij |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 16 | */ |
| 17 | #include <linux/kernel.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 20 | #include <linux/gpio/consumer.h> |
Sachin Kamat | ecc7777 | 2013-10-11 17:21:53 +0530 | [diff] [blame] | 21 | #include <linux/of.h> |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 22 | #include <linux/of_device.h> |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 23 | |
| 24 | #include <linux/spi/spi.h> |
| 25 | #include <linux/spi/spi_bitbang.h> |
| 26 | #include <linux/spi/spi_gpio.h> |
| 27 | |
| 28 | |
| 29 | /* |
| 30 | * This bitbanging SPI master driver should help make systems usable |
| 31 | * when a native hardware SPI engine is not available, perhaps because |
| 32 | * its driver isn't yet working or because the I/O pins it requires |
| 33 | * are used for other purposes. |
| 34 | * |
| 35 | * platform_device->driver_data ... points to spi_gpio |
| 36 | * |
| 37 | * spi->controller_state ... reserved for bitbang framework code |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 38 | * |
| 39 | * spi->master->dev.driver_data ... points to spi_gpio->bitbang |
| 40 | */ |
| 41 | |
| 42 | struct spi_gpio { |
| 43 | struct spi_bitbang bitbang; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 44 | struct platform_device *pdev; |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 45 | struct gpio_desc *sck; |
| 46 | struct gpio_desc *miso; |
| 47 | struct gpio_desc *mosi; |
| 48 | struct gpio_desc **cs_gpios; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /*----------------------------------------------------------------------*/ |
| 52 | |
| 53 | /* |
| 54 | * Because the overhead of going through four GPIO procedure calls |
| 55 | * per transferred bit can make performance a problem, this code |
| 56 | * is set up so that you can use it in either of two ways: |
| 57 | * |
| 58 | * - The slow generic way: set up platform_data to hold the GPIO |
| 59 | * numbers used for MISO/MOSI/SCK, and issue procedure calls for |
| 60 | * each of them. This driver can handle several such busses. |
| 61 | * |
| 62 | * - The quicker inlined way: only helps with platform GPIO code |
| 63 | * that inlines operations for constant GPIOs. This can give |
| 64 | * you tight (fast!) inner loops, but each such bus needs a |
| 65 | * new driver. You'll define a new C file, with Makefile and |
| 66 | * Kconfig support; the C code can be a total of six lines: |
| 67 | * |
| 68 | * #define DRIVER_NAME "myboard_spi2" |
| 69 | * #define SPI_MISO_GPIO 119 |
| 70 | * #define SPI_MOSI_GPIO 120 |
| 71 | * #define SPI_SCK_GPIO 121 |
| 72 | * #define SPI_N_CHIPSEL 4 |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 73 | * #include "spi-gpio.c" |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | #ifndef DRIVER_NAME |
| 77 | #define DRIVER_NAME "spi_gpio" |
| 78 | |
| 79 | #define GENERIC_BITBANG /* vs tight inlines */ |
| 80 | |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | /*----------------------------------------------------------------------*/ |
| 84 | |
Nizam Haider | 650705c | 2015-01-05 17:31:03 +0530 | [diff] [blame] | 85 | static inline struct spi_gpio *__pure |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame] | 86 | spi_to_spi_gpio(const struct spi_device *spi) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 87 | { |
| 88 | const struct spi_bitbang *bang; |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame] | 89 | struct spi_gpio *spi_gpio; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 90 | |
| 91 | bang = spi_master_get_devdata(spi->master); |
| 92 | spi_gpio = container_of(bang, struct spi_gpio, bitbang); |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame] | 93 | return spi_gpio; |
| 94 | } |
| 95 | |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 96 | /* These helpers are in turn called by the bitbang inlines */ |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 97 | static inline void setsck(const struct spi_device *spi, int is_on) |
| 98 | { |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 99 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
| 100 | |
| 101 | gpiod_set_value_cansleep(spi_gpio->sck, is_on); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static inline void setmosi(const struct spi_device *spi, int is_on) |
| 105 | { |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 106 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
| 107 | |
| 108 | gpiod_set_value_cansleep(spi_gpio->mosi, is_on); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static inline int getmiso(const struct spi_device *spi) |
| 112 | { |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 113 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 114 | |
Lorenzo Bianconi | 4b859db | 2018-07-28 10:19:14 +0200 | [diff] [blame] | 115 | if (spi->mode & SPI_3WIRE) |
| 116 | return !!gpiod_get_value_cansleep(spi_gpio->mosi); |
| 117 | else |
| 118 | return !!gpiod_get_value_cansleep(spi_gpio->miso); |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 119 | } |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * NOTE: this clocks "as fast as we can". It "should" be a function of the |
| 123 | * requested device clock. Software overhead means we usually have trouble |
| 124 | * reaching even one Mbit/sec (except when we can inline bitops), so for now |
| 125 | * we'll just assume we never need additional per-bit slowdowns. |
| 126 | */ |
| 127 | #define spidelay(nsecs) do {} while (0) |
| 128 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 129 | #include "spi-bitbang-txrx.h" |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * These functions can leverage inline expansion of GPIO calls to shrink |
| 133 | * costs for a txrx bit, often by factors of around ten (by instruction |
| 134 | * count). That is particularly visible for larger word sizes, but helps |
| 135 | * even with default 8-bit words. |
| 136 | * |
| 137 | * REVISIT overheads calling these functions for each word also have |
| 138 | * significant performance costs. Having txrx_bufs() calls that inline |
| 139 | * the txrx_word() logic would help performance, e.g. on larger blocks |
| 140 | * used with flash storage or MMC/SD. There should also be ways to make |
| 141 | * GCC be less stupid about reloading registers inside the I/O loops, |
| 142 | * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3? |
| 143 | */ |
| 144 | |
| 145 | static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 146 | unsigned nsecs, u32 word, u8 bits, unsigned flags) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 147 | { |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 148 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 152 | unsigned nsecs, u32 word, u8 bits, unsigned flags) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 153 | { |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 154 | return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 158 | unsigned nsecs, u32 word, u8 bits, unsigned flags) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 159 | { |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 160 | return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 164 | unsigned nsecs, u32 word, u8 bits, unsigned flags) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 165 | { |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 166 | return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 169 | /* |
| 170 | * These functions do not call setmosi or getmiso if respective flag |
| 171 | * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to |
| 172 | * call when such pin is not present or defined in the controller. |
| 173 | * A separate set of callbacks is defined to get highest possible |
| 174 | * speed in the generic case (when both MISO and MOSI lines are |
| 175 | * available), as optimiser will remove the checks when argument is |
| 176 | * constant. |
| 177 | */ |
| 178 | |
| 179 | static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 180 | unsigned nsecs, u32 word, u8 bits, unsigned flags) |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 181 | { |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 182 | flags = spi->master->flags; |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 183 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); |
| 184 | } |
| 185 | |
| 186 | static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 187 | unsigned nsecs, u32 word, u8 bits, unsigned flags) |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 188 | { |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 189 | flags = spi->master->flags; |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 190 | return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); |
| 191 | } |
| 192 | |
| 193 | static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 194 | unsigned nsecs, u32 word, u8 bits, unsigned flags) |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 195 | { |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 196 | flags = spi->master->flags; |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 197 | return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); |
| 198 | } |
| 199 | |
| 200 | static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 201 | unsigned nsecs, u32 word, u8 bits, unsigned flags) |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 202 | { |
Lorenzo Bianconi | 304d343 | 2018-07-28 10:19:13 +0200 | [diff] [blame] | 203 | flags = spi->master->flags; |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 204 | return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); |
| 205 | } |
| 206 | |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 207 | /*----------------------------------------------------------------------*/ |
| 208 | |
| 209 | static void spi_gpio_chipselect(struct spi_device *spi, int is_active) |
| 210 | { |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame] | 211 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 212 | |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 213 | /* set initial clock line level */ |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 214 | if (is_active) |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 215 | gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 216 | |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 217 | /* Drive chip select line, if we have one */ |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 218 | if (spi_gpio->cs_gpios) { |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 219 | struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select]; |
| 220 | |
| 221 | /* SPI chip selects are normally active-low */ |
| 222 | gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); |
Michael Buesch | bfb9bcd | 2009-04-02 16:57:07 -0700 | [diff] [blame] | 223 | } |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static int spi_gpio_setup(struct spi_device *spi) |
| 227 | { |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 228 | struct gpio_desc *cs; |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame] | 229 | int status = 0; |
| 230 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 231 | |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 232 | /* |
| 233 | * The CS GPIOs have already been |
| 234 | * initialized from the descriptor lookup. |
| 235 | */ |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 236 | if (spi_gpio->cs_gpios) { |
| 237 | cs = spi_gpio->cs_gpios[spi->chip_select]; |
| 238 | if (!spi->controller_state && cs) |
| 239 | status = gpiod_direction_output(cs, |
| 240 | !(spi->mode & SPI_CS_HIGH)); |
| 241 | } |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 242 | |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 243 | if (!status) |
Josef Ahmad | 6b8cc33 | 2013-04-09 18:25:34 +0100 | [diff] [blame] | 244 | status = spi_bitbang_setup(spi); |
Daniel Mack | 161c2dd | 2012-09-05 11:04:35 +0200 | [diff] [blame] | 245 | |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 246 | return status; |
| 247 | } |
| 248 | |
Lorenzo Bianconi | 4b859db | 2018-07-28 10:19:14 +0200 | [diff] [blame] | 249 | static int spi_gpio_set_direction(struct spi_device *spi, bool output) |
| 250 | { |
| 251 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
Linus Walleij | 5132b3d | 2018-11-01 22:25:04 +0100 | [diff] [blame] | 252 | int ret; |
Lorenzo Bianconi | 4b859db | 2018-07-28 10:19:14 +0200 | [diff] [blame] | 253 | |
| 254 | if (output) |
| 255 | return gpiod_direction_output(spi_gpio->mosi, 1); |
Linus Walleij | 5132b3d | 2018-11-01 22:25:04 +0100 | [diff] [blame] | 256 | |
| 257 | ret = gpiod_direction_input(spi_gpio->mosi); |
| 258 | if (ret) |
| 259 | return ret; |
| 260 | /* |
| 261 | * Send a turnaround high impedance cycle when switching |
| 262 | * from output to input. Theoretically there should be |
| 263 | * a clock delay here, but as has been noted above, the |
| 264 | * nsec delay function for bit-banged GPIO is simply |
| 265 | * {} because bit-banging just doesn't get fast enough |
| 266 | * anyway. |
| 267 | */ |
| 268 | if (spi->mode & SPI_3WIRE_HIZ) { |
| 269 | gpiod_set_value_cansleep(spi_gpio->sck, |
| 270 | !(spi->mode & SPI_CPOL)); |
| 271 | gpiod_set_value_cansleep(spi_gpio->sck, |
| 272 | !!(spi->mode & SPI_CPOL)); |
| 273 | } |
| 274 | return 0; |
Lorenzo Bianconi | 4b859db | 2018-07-28 10:19:14 +0200 | [diff] [blame] | 275 | } |
| 276 | |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 277 | static void spi_gpio_cleanup(struct spi_device *spi) |
| 278 | { |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 279 | spi_bitbang_cleanup(spi); |
| 280 | } |
| 281 | |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 282 | /* |
| 283 | * It can be convenient to use this driver with pins that have alternate |
| 284 | * functions associated with a "native" SPI controller if a driver for that |
| 285 | * controller is not available, or is missing important functionality. |
| 286 | * |
| 287 | * On platforms which can do so, configure MISO with a weak pullup unless |
| 288 | * there's an external pullup on that signal. That saves power by avoiding |
| 289 | * floating signals. (A weak pulldown would save power too, but many |
| 290 | * drivers expect to see all-ones data as the no slave "response".) |
| 291 | */ |
Andrey Smirnov | 5c8283c | 2019-04-02 21:01:29 -0700 | [diff] [blame^] | 292 | static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 293 | { |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 294 | spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); |
| 295 | if (IS_ERR(spi_gpio->mosi)) |
| 296 | return PTR_ERR(spi_gpio->mosi); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 297 | |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 298 | spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); |
| 299 | if (IS_ERR(spi_gpio->miso)) |
| 300 | return PTR_ERR(spi_gpio->miso); |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 301 | |
| 302 | spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); |
Linus Walleij | 1723c31 | 2018-09-04 15:39:30 +0200 | [diff] [blame] | 303 | if (IS_ERR(spi_gpio->sck)) |
| 304 | return PTR_ERR(spi_gpio->sck); |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 305 | |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 306 | return 0; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 309 | #ifdef CONFIG_OF |
Jingoo Han | d9e1528 | 2014-05-07 16:48:59 +0900 | [diff] [blame] | 310 | static const struct of_device_id spi_gpio_dt_ids[] = { |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 311 | { .compatible = "spi-gpio" }, |
| 312 | {} |
| 313 | }; |
| 314 | MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids); |
| 315 | |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 316 | static int spi_gpio_probe_dt(struct platform_device *pdev, |
| 317 | struct spi_master *master) |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 318 | { |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 319 | master->dev.of_node = pdev->dev.of_node; |
| 320 | master->use_gpio_descriptors = true; |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 321 | |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 322 | return 0; |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 323 | } |
| 324 | #else |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 325 | static inline int spi_gpio_probe_dt(struct platform_device *pdev, |
| 326 | struct spi_master *master) |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 327 | { |
| 328 | return 0; |
| 329 | } |
| 330 | #endif |
| 331 | |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 332 | static int spi_gpio_probe_pdata(struct platform_device *pdev, |
| 333 | struct spi_master *master) |
| 334 | { |
| 335 | struct device *dev = &pdev->dev; |
| 336 | struct spi_gpio_platform_data *pdata = dev_get_platdata(dev); |
| 337 | struct spi_gpio *spi_gpio = spi_master_get_devdata(master); |
| 338 | int i; |
| 339 | |
| 340 | #ifdef GENERIC_BITBANG |
| 341 | if (!pdata || !pdata->num_chipselect) |
| 342 | return -ENODEV; |
| 343 | #endif |
| 344 | /* |
| 345 | * The master needs to think there is a chipselect even if not |
| 346 | * connected |
| 347 | */ |
| 348 | master->num_chipselect = pdata->num_chipselect ?: 1; |
| 349 | |
| 350 | spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect, |
| 351 | sizeof(*spi_gpio->cs_gpios), |
| 352 | GFP_KERNEL); |
| 353 | if (!spi_gpio->cs_gpios) |
| 354 | return -ENOMEM; |
| 355 | |
| 356 | for (i = 0; i < master->num_chipselect; i++) { |
| 357 | spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i, |
| 358 | GPIOD_OUT_HIGH); |
| 359 | if (IS_ERR(spi_gpio->cs_gpios[i])) |
| 360 | return PTR_ERR(spi_gpio->cs_gpios[i]); |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 366 | static int spi_gpio_probe(struct platform_device *pdev) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 367 | { |
| 368 | int status; |
| 369 | struct spi_master *master; |
| 370 | struct spi_gpio *spi_gpio; |
Andrey Smirnov | 96cad6d | 2019-04-02 21:01:23 -0700 | [diff] [blame] | 371 | struct device *dev = &pdev->dev; |
Andrey Smirnov | 15dd0e9 | 2019-04-02 21:01:24 -0700 | [diff] [blame] | 372 | struct spi_bitbang *bb; |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 373 | const struct of_device_id *of_id; |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 374 | |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 375 | of_id = of_match_device(spi_gpio_dt_ids, &pdev->dev); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 376 | |
Andrey Smirnov | 96cad6d | 2019-04-02 21:01:23 -0700 | [diff] [blame] | 377 | master = spi_alloc_master(dev, sizeof(*spi_gpio)); |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 378 | if (!master) |
| 379 | return -ENOMEM; |
Torsten Fleischer | d1d8180 | 2014-11-03 17:17:55 +0100 | [diff] [blame] | 380 | |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 381 | if (of_id) |
| 382 | status = spi_gpio_probe_dt(pdev, master); |
| 383 | else |
| 384 | status = spi_gpio_probe_pdata(pdev, master); |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 385 | |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 386 | if (status) |
| 387 | return status; |
| 388 | |
| 389 | spi_gpio = spi_master_get_devdata(master); |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 390 | |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 391 | platform_set_drvdata(pdev, spi_gpio); |
| 392 | |
| 393 | spi_gpio->pdev = pdev; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 394 | |
Andrey Smirnov | 5c8283c | 2019-04-02 21:01:29 -0700 | [diff] [blame^] | 395 | status = spi_gpio_request(dev, spi_gpio); |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 396 | if (status) |
| 397 | return status; |
| 398 | |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 399 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); |
Russell King | b89fefd | 2019-02-21 15:59:58 +0000 | [diff] [blame] | 400 | master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL | |
| 401 | SPI_CS_HIGH; |
Andrey Smirnov | 5c8283c | 2019-04-02 21:01:29 -0700 | [diff] [blame^] | 402 | if (!spi_gpio->mosi) { |
| 403 | /* HW configuration without MOSI pin |
| 404 | * |
| 405 | * No setting SPI_MASTER_NO_RX here - if there is only |
| 406 | * a MOSI pin connected the host can still do RX by |
| 407 | * changing the direction of the line. |
| 408 | */ |
| 409 | master->flags = SPI_MASTER_NO_TX; |
| 410 | } |
| 411 | |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 412 | master->bus_num = pdev->id; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 413 | master->setup = spi_gpio_setup; |
| 414 | master->cleanup = spi_gpio_cleanup; |
Andrey Smirnov | 249e263 | 2019-04-02 21:01:27 -0700 | [diff] [blame] | 415 | |
Andrey Smirnov | 15dd0e9 | 2019-04-02 21:01:24 -0700 | [diff] [blame] | 416 | bb = &spi_gpio->bitbang; |
| 417 | bb->master = master; |
| 418 | bb->chipselect = spi_gpio_chipselect; |
| 419 | bb->set_line_direction = spi_gpio_set_direction; |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 420 | |
Andrey Smirnov | 5c8283c | 2019-04-02 21:01:29 -0700 | [diff] [blame^] | 421 | if (master->flags & SPI_MASTER_NO_TX) { |
Andrey Smirnov | 15dd0e9 | 2019-04-02 21:01:24 -0700 | [diff] [blame] | 422 | bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; |
| 423 | bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; |
| 424 | bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; |
| 425 | bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; |
Andrey Smirnov | 68cd9dc | 2019-04-02 21:01:25 -0700 | [diff] [blame] | 426 | } else { |
| 427 | bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; |
| 428 | bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; |
| 429 | bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; |
| 430 | bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; |
Marek Szyprowski | 3c8e1a8 | 2010-06-30 14:27:37 -0600 | [diff] [blame] | 431 | } |
Andrey Smirnov | 15dd0e9 | 2019-04-02 21:01:24 -0700 | [diff] [blame] | 432 | bb->setup_transfer = spi_bitbang_setup_transfer; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 433 | |
| 434 | status = spi_bitbang_start(&spi_gpio->bitbang); |
Linus Walleij | 9b00bc7 | 2018-02-12 13:45:30 +0100 | [diff] [blame] | 435 | if (status) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 436 | spi_master_put(master); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 437 | |
| 438 | return status; |
| 439 | } |
| 440 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 441 | static int spi_gpio_remove(struct platform_device *pdev) |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 442 | { |
| 443 | struct spi_gpio *spi_gpio; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 444 | |
| 445 | spi_gpio = platform_get_drvdata(pdev); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 446 | |
| 447 | /* stop() unregisters child devices too */ |
Axel Lin | d9721ae | 2014-03-29 18:50:12 +0800 | [diff] [blame] | 448 | spi_bitbang_stop(&spi_gpio->bitbang); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 449 | |
Axel Lin | 94c69f7 | 2013-09-10 15:43:41 +0800 | [diff] [blame] | 450 | spi_master_put(spi_gpio->bitbang.master); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 451 | |
Axel Lin | d9721ae | 2014-03-29 18:50:12 +0800 | [diff] [blame] | 452 | return 0; |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 456 | |
| 457 | static struct platform_driver spi_gpio_driver = { |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 458 | .driver = { |
| 459 | .name = DRIVER_NAME, |
Daniel Mack | 38ab18c | 2012-09-05 11:04:36 +0200 | [diff] [blame] | 460 | .of_match_table = of_match_ptr(spi_gpio_dt_ids), |
| 461 | }, |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 462 | .probe = spi_gpio_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 463 | .remove = spi_gpio_remove, |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 464 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 465 | module_platform_driver(spi_gpio_driver); |
David Brownell | d29389d | 2009-01-06 14:41:41 -0800 | [diff] [blame] | 466 | |
| 467 | MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO "); |
| 468 | MODULE_AUTHOR("David Brownell"); |
| 469 | MODULE_LICENSE("GPL"); |