Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi/spi-gpio: add support for controllers without MISO or MOSI pin

There are some boards that do not strictly follow SPI standard and use
only 3 wires (SCLK, MOSI or MISO, SS) for connecting some simple auxiliary
chips and controls them with GPIO based 'spi controller'. In this
configuration the MISO or MOSI line is missing (it is not required if the
chip does not transfer any data back to host or host only reads data from
chip).

This patch adds support for such non-standard configuration in GPIO-based
SPI controller. It has been tested in configuration without MISO pin.

Reviewed-by: Kyungmin Park <kyungmin.park@samsung.com>
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Acked-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>

authored by

Marek Szyprowski and committed by
Grant Likely
3c8e1a84 04bb2a03

+88 -18
+83 -18
drivers/spi/spi_gpio.c
··· 167 167 return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits); 168 168 } 169 169 170 + /* 171 + * These functions do not call setmosi or getmiso if respective flag 172 + * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to 173 + * call when such pin is not present or defined in the controller. 174 + * A separate set of callbacks is defined to get highest possible 175 + * speed in the generic case (when both MISO and MOSI lines are 176 + * available), as optimiser will remove the checks when argument is 177 + * constant. 178 + */ 179 + 180 + static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, 181 + unsigned nsecs, u32 word, u8 bits) 182 + { 183 + unsigned flags = spi->master->flags; 184 + return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); 185 + } 186 + 187 + static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, 188 + unsigned nsecs, u32 word, u8 bits) 189 + { 190 + unsigned flags = spi->master->flags; 191 + return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); 192 + } 193 + 194 + static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, 195 + unsigned nsecs, u32 word, u8 bits) 196 + { 197 + unsigned flags = spi->master->flags; 198 + return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); 199 + } 200 + 201 + static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, 202 + unsigned nsecs, u32 word, u8 bits) 203 + { 204 + unsigned flags = spi->master->flags; 205 + return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); 206 + } 207 + 170 208 /*----------------------------------------------------------------------*/ 171 209 172 210 static void spi_gpio_chipselect(struct spi_device *spi, int is_active) ··· 270 232 } 271 233 272 234 static int __init 273 - spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label) 235 + spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label, 236 + u16 *res_flags) 274 237 { 275 238 int value; 276 239 277 240 /* NOTE: SPI_*_GPIO symbols may reference "pdata" */ 278 241 279 - value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false); 280 - if (value) 281 - goto done; 242 + if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) { 243 + value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false); 244 + if (value) 245 + goto done; 246 + } else { 247 + /* HW configuration without MOSI pin */ 248 + *res_flags |= SPI_MASTER_NO_TX; 249 + } 282 250 283 - value = spi_gpio_alloc(SPI_MISO_GPIO, label, true); 284 - if (value) 285 - goto free_mosi; 251 + if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) { 252 + value = spi_gpio_alloc(SPI_MISO_GPIO, label, true); 253 + if (value) 254 + goto free_mosi; 255 + } else { 256 + /* HW configuration without MISO pin */ 257 + *res_flags |= SPI_MASTER_NO_RX; 258 + } 286 259 287 260 value = spi_gpio_alloc(SPI_SCK_GPIO, label, false); 288 261 if (value) ··· 302 253 goto done; 303 254 304 255 free_miso: 305 - gpio_free(SPI_MISO_GPIO); 256 + if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 257 + gpio_free(SPI_MISO_GPIO); 306 258 free_mosi: 307 - gpio_free(SPI_MOSI_GPIO); 259 + if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 260 + gpio_free(SPI_MOSI_GPIO); 308 261 done: 309 262 return value; 310 263 } ··· 317 266 struct spi_master *master; 318 267 struct spi_gpio *spi_gpio; 319 268 struct spi_gpio_platform_data *pdata; 269 + u16 master_flags = 0; 320 270 321 271 pdata = pdev->dev.platform_data; 322 272 #ifdef GENERIC_BITBANG ··· 325 273 return -ENODEV; 326 274 #endif 327 275 328 - status = spi_gpio_request(pdata, dev_name(&pdev->dev)); 276 + status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags); 329 277 if (status < 0) 330 278 return status; 331 279 ··· 341 289 if (pdata) 342 290 spi_gpio->pdata = *pdata; 343 291 292 + master->flags = master_flags; 344 293 master->bus_num = pdev->id; 345 294 master->num_chipselect = SPI_N_CHIPSEL; 346 295 master->setup = spi_gpio_setup; ··· 349 296 350 297 spi_gpio->bitbang.master = spi_master_get(master); 351 298 spi_gpio->bitbang.chipselect = spi_gpio_chipselect; 352 - spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; 353 - spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; 354 - spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; 355 - spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; 299 + 300 + if ((master_flags & (SPI_MASTER_NO_RX | SPI_MASTER_NO_RX)) == 0) { 301 + spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; 302 + spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; 303 + spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; 304 + spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; 305 + } else { 306 + spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; 307 + spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; 308 + spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; 309 + spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; 310 + } 356 311 spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; 357 312 spi_gpio->bitbang.flags = SPI_CS_HIGH; 358 313 ··· 368 307 if (status < 0) { 369 308 spi_master_put(spi_gpio->bitbang.master); 370 309 gpio_free: 371 - gpio_free(SPI_MISO_GPIO); 372 - gpio_free(SPI_MOSI_GPIO); 310 + if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 311 + gpio_free(SPI_MISO_GPIO); 312 + if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 313 + gpio_free(SPI_MOSI_GPIO); 373 314 gpio_free(SPI_SCK_GPIO); 374 315 spi_master_put(master); 375 316 } ··· 394 331 395 332 platform_set_drvdata(pdev, NULL); 396 333 397 - gpio_free(SPI_MISO_GPIO); 398 - gpio_free(SPI_MOSI_GPIO); 334 + if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 335 + gpio_free(SPI_MISO_GPIO); 336 + if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 337 + gpio_free(SPI_MOSI_GPIO); 399 338 gpio_free(SPI_SCK_GPIO); 400 339 401 340 return status;
+5
include/linux/spi/spi_gpio.h
··· 29 29 * SPI_GPIO_NO_CHIPSELECT to the controller_data: 30 30 * .controller_data = (void *) SPI_GPIO_NO_CHIPSELECT; 31 31 * 32 + * If the MISO or MOSI pin is not available then it should be set to 33 + * SPI_GPIO_NO_MISO or SPI_GPIO_NO_MOSI. 34 + * 32 35 * If the bitbanged bus is later switched to a "native" controller, 33 36 * that platform_device and controller_data should be removed. 34 37 */ 35 38 36 39 #define SPI_GPIO_NO_CHIPSELECT ((unsigned long)-1l) 40 + #define SPI_GPIO_NO_MISO ((unsigned long)-1l) 41 + #define SPI_GPIO_NO_MOSI ((unsigned long)-1l) 37 42 38 43 /** 39 44 * struct spi_gpio_platform_data - parameter for bitbanged SPI master