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

spi: spi-gpio: Rewrite to use GPIO descriptors

This converts the bit-banged GPIO SPI driver to looking up and
using GPIO descriptors to get a handle on GPIO lines for SCK,
MOSI, MISO and all CS lines.

All existing board files are converted in one go to keep it all
consistent. With these conversions I rarely find any interrim
steps that makes any sense.

Device tree probing and GPIO handling should work like before
also after this patch.

For board files, we stop using controller data to pass the GPIO
line for chip select, instead we pass this as a GPIO descriptor
lookup like everything else.

In some s3c24xx machines the names of the SPI devices were set to
"spi-gpio" rather than "spi_gpio" which can never have worked, I
fixed it working (I guess) as part of this patch set. Sometimes
I wonder how this code got upstream in the first place, it
obviously is not tested.

mach-s3c64xx/mach-smartq.c has the same problem and additionally
defines the *same* GPIO line for MOSI and MISO which is not going
to be accepted by gpiolib. As the lines were number 1,2,2 I assumed
it was a typo and use lines 1,2,3. A comment gives awat that line 0
is chip select though no actual SPI device is provided for the LCD
supposed to be on this bit-banged SPI bus. I left it intact instead
of just deleting the bus though.

Kill off board file code that try to initialize the SPI lines
to the same values that they will later be set by the spi_gpio
driver anyways. Given the huge number of weird things in these
board files I do not think this code is very tested or put in
with much afterthought anyways.

In order to assert that we do not get performance regressions on
this crucial bing-banged driver, a ran a script like this dumping the
Ilitek ILI9322 regmap 10000 times (it has no caching obviously) on
an otherwise idle system in two iterations before and after the
patches:

#!/bin/sh
for run in `seq 10000`
do
cat /debug/regmap/spi0.0/registers > /dev/null
done

Before the patch:

time test.sh
real 3m 41.03s
user 0m 29.41s
sys 3m 7.22s

time test.sh
real 3m 44.24s
user 0m 32.31s
sys 3m 7.60s

After the patch:

time test.sh
real 3m 41.32s
user 0m 28.92s
sys 3m 8.08s

time test.sh
real 3m 39.92s
user 0m 30.20s
sys 3m 5.56s

So any performance differences seems to be in the error margin.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Olof Johansson <olof@lixom.net>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Linus Walleij and committed by
Mark Brown
9b00bc7b 7928b2cb

+262 -286
+17 -4
arch/arm/mach-pxa/cm-x300.c
··· 23 23 #include <linux/clk.h> 24 24 25 25 #include <linux/gpio.h> 26 + #include <linux/gpio/machine.h> 26 27 #include <linux/dm9000.h> 27 28 #include <linux/leds.h> 28 29 #include <linux/platform_data/rtc-v3020.h> ··· 344 343 #define LCD_SPI_BUS_NUM (1) 345 344 346 345 static struct spi_gpio_platform_data cm_x300_spi_gpio_pdata = { 347 - .sck = GPIO_LCD_SCL, 348 - .mosi = GPIO_LCD_DIN, 349 - .miso = GPIO_LCD_DOUT, 350 346 .num_chipselect = 1, 351 347 }; 352 348 ··· 352 354 .id = LCD_SPI_BUS_NUM, 353 355 .dev = { 354 356 .platform_data = &cm_x300_spi_gpio_pdata, 357 + }, 358 + }; 359 + 360 + static struct gpiod_lookup_table cm_x300_spi_gpiod_table = { 361 + .dev_id = "spi_gpio", 362 + .table = { 363 + GPIO_LOOKUP("gpio-pxa", GPIO_LCD_SCL, 364 + "sck", GPIO_ACTIVE_HIGH), 365 + GPIO_LOOKUP("gpio-pxa", GPIO_LCD_DIN, 366 + "mosi", GPIO_ACTIVE_HIGH), 367 + GPIO_LOOKUP("gpio-pxa", GPIO_LCD_DOUT, 368 + "miso", GPIO_ACTIVE_HIGH), 369 + GPIO_LOOKUP("gpio-pxa", GPIO_LCD_CS, 370 + "cs", GPIO_ACTIVE_HIGH), 371 + { }, 355 372 }, 356 373 }; 357 374 ··· 380 367 .max_speed_hz = 1000000, 381 368 .bus_num = LCD_SPI_BUS_NUM, 382 369 .chip_select = 0, 383 - .controller_data = (void *) GPIO_LCD_CS, 384 370 .platform_data = &cm_x300_tdo24m_pdata, 385 371 }, 386 372 }; ··· 388 376 { 389 377 spi_register_board_info(cm_x300_spi_devices, 390 378 ARRAY_SIZE(cm_x300_spi_devices)); 379 + gpiod_add_lookup_table(&cm_x300_spi_gpiod_table); 391 380 platform_device_register(&cm_x300_spi_gpio); 392 381 } 393 382 #else
+20 -6
arch/arm/mach-pxa/raumfeld.c
··· 646 646 */ 647 647 648 648 static struct spi_gpio_platform_data raumfeld_spi_platform_data = { 649 - .sck = GPIO_SPI_CLK, 650 - .mosi = GPIO_SPI_MOSI, 651 - .miso = GPIO_SPI_MISO, 652 649 .num_chipselect = 3, 653 650 }; 654 651 ··· 655 658 .dev = { 656 659 .platform_data = &raumfeld_spi_platform_data, 657 660 } 661 + }; 662 + 663 + static struct gpiod_lookup_table raumfeld_spi_gpiod_table = { 664 + .dev_id = "spi_gpio", 665 + .table = { 666 + GPIO_LOOKUP("gpio-0", GPIO_SPI_CLK, 667 + "sck", GPIO_ACTIVE_HIGH), 668 + GPIO_LOOKUP("gpio-0", GPIO_SPI_MOSI, 669 + "mosi", GPIO_ACTIVE_HIGH), 670 + GPIO_LOOKUP("gpio-0", GPIO_SPI_MISO, 671 + "miso", GPIO_ACTIVE_HIGH), 672 + GPIO_LOOKUP_IDX("gpio-0", GPIO_SPDIF_CS, 673 + "cs", 0, GPIO_ACTIVE_HIGH), 674 + GPIO_LOOKUP_IDX("gpio-0", GPIO_ACCEL_CS, 675 + "cs", 1, GPIO_ACTIVE_HIGH), 676 + GPIO_LOOKUP_IDX("gpio-0", GPIO_MCLK_DAC_CS, 677 + "cs", 2, GPIO_ACTIVE_HIGH), 678 + { }, 679 + }, 658 680 }; 659 681 660 682 static struct lis3lv02d_platform_data lis3_pdata = { ··· 696 680 .max_speed_hz = 10000, \ 697 681 .bus_num = 0, \ 698 682 .chip_select = 0, \ 699 - .controller_data = (void *) GPIO_SPDIF_CS, \ 700 683 } 701 684 702 685 #define SPI_LIS3 \ ··· 704 689 .max_speed_hz = 1000000, \ 705 690 .bus_num = 0, \ 706 691 .chip_select = 1, \ 707 - .controller_data = (void *) GPIO_ACCEL_CS, \ 708 692 .platform_data = &lis3_pdata, \ 709 693 .irq = PXA_GPIO_TO_IRQ(GPIO_ACCEL_IRQ), \ 710 694 } ··· 714 700 .max_speed_hz = 1000000, \ 715 701 .bus_num = 0, \ 716 702 .chip_select = 2, \ 717 - .controller_data = (void *) GPIO_MCLK_DAC_CS, \ 718 703 } 719 704 720 705 static struct spi_board_info connector_spi_devices[] __initdata = { ··· 1079 1066 else 1080 1067 gpio_direction_output(GPIO_SHUTDOWN_SUPPLY, 0); 1081 1068 1069 + gpiod_add_lookup_table(&raumfeld_spi_gpiod_table); 1082 1070 platform_add_devices(ARRAY_AND_SIZE(raumfeld_common_devices)); 1083 1071 i2c_register_board_info(1, &raumfeld_pwri2c_board_info, 1); 1084 1072 }
+32 -23
arch/arm/mach-s3c24xx/mach-jive.c
··· 12 12 #include <linux/timer.h> 13 13 #include <linux/init.h> 14 14 #include <linux/gpio.h> 15 + #include <linux/gpio/machine.h> 15 16 #include <linux/syscore_ops.h> 16 17 #include <linux/serial_core.h> 17 18 #include <linux/serial_s3c.h> ··· 389 388 /* LCD SPI support */ 390 389 391 390 static struct spi_gpio_platform_data jive_lcd_spi = { 392 - .sck = S3C2410_GPG(8), 393 - .mosi = S3C2410_GPB(8), 394 - .miso = SPI_GPIO_NO_MISO, 391 + .num_chipselect = 1, 395 392 }; 396 393 397 394 static struct platform_device jive_device_lcdspi = { 398 - .name = "spi-gpio", 395 + .name = "spi_gpio", 399 396 .id = 1, 400 397 .dev.platform_data = &jive_lcd_spi, 401 398 }; 402 399 400 + static struct gpiod_lookup_table jive_lcdspi_gpiod_table = { 401 + .dev_id = "spi_gpio", 402 + .table = { 403 + GPIO_LOOKUP("GPIOG", 8, 404 + "sck", GPIO_ACTIVE_HIGH), 405 + GPIO_LOOKUP("GPIOB", 8, 406 + "mosi", GPIO_ACTIVE_HIGH), 407 + GPIO_LOOKUP("GPIOB", 7, 408 + "cs", GPIO_ACTIVE_HIGH), 409 + { }, 410 + }, 411 + }; 403 412 404 413 /* WM8750 audio code SPI definition */ 405 414 406 415 static struct spi_gpio_platform_data jive_wm8750_spi = { 407 - .sck = S3C2410_GPB(4), 408 - .mosi = S3C2410_GPB(9), 409 - .miso = SPI_GPIO_NO_MISO, 416 + .num_chipselect = 1, 410 417 }; 411 418 412 419 static struct platform_device jive_device_wm8750 = { 413 - .name = "spi-gpio", 420 + .name = "spi_gpio", 414 421 .id = 2, 415 422 .dev.platform_data = &jive_wm8750_spi, 423 + }; 424 + 425 + static struct gpiod_lookup_table jive_wm8750_gpiod_table = { 426 + .dev_id = "spi_gpio", 427 + .table = { 428 + GPIO_LOOKUP("GPIOB", 4, 429 + "gpio-sck", GPIO_ACTIVE_HIGH), 430 + GPIO_LOOKUP("GPIOB", 9, 431 + "gpio-mosi", GPIO_ACTIVE_HIGH), 432 + GPIO_LOOKUP("GPIOH", 10, 433 + "cs", GPIO_ACTIVE_HIGH), 434 + { }, 435 + }, 416 436 }; 417 437 418 438 /* JIVE SPI devices. */ ··· 446 424 .mode = SPI_MODE_3, /* CPOL=1, CPHA=1 */ 447 425 .max_speed_hz = 100000, 448 426 .platform_data = &jive_lcm_config, 449 - .controller_data = (void *)S3C2410_GPB(7), 450 427 }, { 451 428 .modalias = "WM8750", 452 429 .bus_num = 2, 453 430 .chip_select = 0, 454 431 .mode = SPI_MODE_0, /* CPOL=0, CPHA=0 */ 455 432 .max_speed_hz = 100000, 456 - .controller_data = (void *)S3C2410_GPH(10), 457 433 }, 458 434 }; 459 435 ··· 639 619 /** TODO - check that this is after the cmdline option! */ 640 620 s3c_nand_set_platdata(&jive_nand_info); 641 621 642 - /* initialise the spi */ 643 - 644 622 gpio_request(S3C2410_GPG(13), "lcm reset"); 645 623 gpio_direction_output(S3C2410_GPG(13), 0); 646 624 647 - gpio_request(S3C2410_GPB(7), "jive spi"); 648 - gpio_direction_output(S3C2410_GPB(7), 1); 649 - 650 625 gpio_request_one(S3C2410_GPB(6), GPIOF_OUT_INIT_LOW, NULL); 651 626 gpio_free(S3C2410_GPB(6)); 652 - 653 - gpio_request_one(S3C2410_GPG(8), GPIOF_OUT_INIT_HIGH, NULL); 654 - gpio_free(S3C2410_GPG(8)); 655 - 656 - /* initialise the WM8750 spi */ 657 - 658 - gpio_request(S3C2410_GPH(10), "jive wm8750 spi"); 659 - gpio_direction_output(S3C2410_GPH(10), 1); 660 627 661 628 /* Turn off suspend on both USB ports, and switch the 662 629 * selectable USB port to USB device mode. */ ··· 662 655 663 656 pm_power_off = jive_power_off; 664 657 658 + gpiod_add_lookup_table(&jive_lcdspi_gpiod_table); 659 + gpiod_add_lookup_table(&jive_wm8750_gpiod_table); 665 660 platform_add_devices(jive_devices, ARRAY_SIZE(jive_devices)); 666 661 } 667 662
+19 -7
arch/arm/mach-s3c24xx/mach-qt2410.c
··· 11 11 #include <linux/timer.h> 12 12 #include <linux/init.h> 13 13 #include <linux/gpio.h> 14 + #include <linux/gpio/machine.h> 14 15 #include <linux/device.h> 15 16 #include <linux/platform_device.h> 16 17 #include <linux/serial_core.h> ··· 195 194 /* SPI */ 196 195 197 196 static struct spi_gpio_platform_data spi_gpio_cfg = { 198 - .sck = S3C2410_GPG(7), 199 - .mosi = S3C2410_GPG(6), 200 - .miso = S3C2410_GPG(5), 197 + .num_chipselect = 1, 201 198 }; 202 199 203 200 static struct platform_device qt2410_spi = { 204 - .name = "spi-gpio", 201 + .name = "spi_gpio", 205 202 .id = 1, 206 203 .dev.platform_data = &spi_gpio_cfg, 204 + }; 205 + 206 + static struct gpiod_lookup_table qt2410_spi_gpiod_table = { 207 + .dev_id = "spi_gpio", 208 + .table = { 209 + GPIO_LOOKUP("GPIOG", 7, 210 + "sck", GPIO_ACTIVE_HIGH), 211 + GPIO_LOOKUP("GPIOG", 6, 212 + "mosi", GPIO_ACTIVE_HIGH), 213 + GPIO_LOOKUP("GPIOG", 5, 214 + "miso", GPIO_ACTIVE_HIGH), 215 + GPIO_LOOKUP("GPIOB", 5, 216 + "cs", GPIO_ACTIVE_HIGH), 217 + { }, 218 + }, 207 219 }; 208 220 209 221 /* Board devices */ ··· 337 323 s3c24xx_udc_set_platdata(&qt2410_udc_cfg); 338 324 s3c_i2c0_set_platdata(NULL); 339 325 340 - WARN_ON(gpio_request(S3C2410_GPB(5), "spi cs")); 341 - gpio_direction_output(S3C2410_GPB(5), 1); 342 - 326 + gpiod_add_lookup_table(&qt2410_spi_gpiod_table); 343 327 platform_add_devices(qt2410_devices, ARRAY_SIZE(qt2410_devices)); 344 328 s3c_pm_init(); 345 329 }
+18 -4
arch/arm/mach-s3c64xx/mach-smartq.c
··· 206 206 207 207 /* GPM0 -> CS */ 208 208 static struct spi_gpio_platform_data smartq_lcd_control = { 209 - .sck = S3C64XX_GPM(1), 210 - .mosi = S3C64XX_GPM(2), 211 - .miso = S3C64XX_GPM(2), 209 + .num_chipselect = 1, 212 210 }; 213 211 214 212 static struct platform_device smartq_lcd_control_device = { 215 - .name = "spi-gpio", 213 + .name = "spi_gpio", 216 214 .id = 1, 217 215 .dev.platform_data = &smartq_lcd_control, 216 + }; 217 + 218 + static struct gpiod_lookup_table smartq_lcd_control_gpiod_table = { 219 + .dev_id = "spi_gpio", 220 + .table = { 221 + GPIO_LOOKUP("GPIOM", 1, 222 + "sck", GPIO_ACTIVE_HIGH), 223 + GPIO_LOOKUP("GPIOM", 2, 224 + "mosi", GPIO_ACTIVE_HIGH), 225 + GPIO_LOOKUP("GPIOM", 3, 226 + "miso", GPIO_ACTIVE_HIGH), 227 + GPIO_LOOKUP("GPIOM", 0, 228 + "cs", GPIO_ACTIVE_HIGH), 229 + { }, 230 + }, 218 231 }; 219 232 220 233 static void smartq_lcd_power_set(struct plat_lcd_data *pd, unsigned int power) ··· 417 404 WARN_ON(smartq_wifi_init()); 418 405 419 406 pwm_add_table(smartq_pwm_lookup, ARRAY_SIZE(smartq_pwm_lookup)); 407 + gpiod_add_lookup_table(&smartq_lcd_control_gpiod_table); 420 408 platform_add_devices(smartq_devices, ARRAY_SIZE(smartq_devices)); 421 409 422 410 gpiod_add_lookup_table(&smartq_audio_gpios);
+20 -4
arch/mips/alchemy/devboards/db1000.c
··· 22 22 #include <linux/clk.h> 23 23 #include <linux/dma-mapping.h> 24 24 #include <linux/gpio.h> 25 + #include <linux/gpio/machine.h> 25 26 #include <linux/init.h> 26 27 #include <linux/interrupt.h> 27 28 #include <linux/leds.h> ··· 448 447 }; 449 448 450 449 static struct spi_gpio_platform_data db1100_spictl_pd = { 451 - .sck = 209, 452 - .mosi = 208, 453 - .miso = 207, 454 450 .num_chipselect = 1, 455 451 }; 456 452 ··· 460 462 .mode = 0, 461 463 .irq = AU1100_GPIO21_INT, 462 464 .platform_data = &db1100_touch_pd, 463 - .controller_data = (void *)210, /* for spi_gpio: CS# GPIO210 */ 464 465 }, 465 466 }; 466 467 ··· 471 474 }, 472 475 }; 473 476 477 + /* 478 + * Alchemy GPIO 2 has its base at 200 so the GPIO lines 479 + * 207 thru 210 are GPIOs at offset 7 thru 10 at this chip. 480 + */ 481 + static struct gpiod_lookup_table db1100_spi_gpiod_table = { 482 + .dev_id = "spi_gpio", 483 + .table = { 484 + GPIO_LOOKUP("alchemy-gpio2", 9, 485 + "sck", GPIO_ACTIVE_HIGH), 486 + GPIO_LOOKUP("alchemy-gpio2", 8, 487 + "mosi", GPIO_ACTIVE_HIGH), 488 + GPIO_LOOKUP("alchemy-gpio2", 7, 489 + "miso", GPIO_ACTIVE_HIGH), 490 + GPIO_LOOKUP("alchemy-gpio2", 10, 491 + "cs", GPIO_ACTIVE_HIGH), 492 + { }, 493 + }, 494 + }; 474 495 475 496 static struct platform_device *db1x00_devs[] = { 476 497 &db1x00_codec_dev, ··· 556 541 clk_put(p); 557 542 558 543 platform_add_devices(db1100_devs, ARRAY_SIZE(db1100_devs)); 544 + gpiod_add_lookup_table(&db1100_spi_gpiod_table); 559 545 platform_device_register(&db1100_spi_dev); 560 546 } else if (board == BCSR_WHOAMI_DB1000) { 561 547 c0 = AU1000_GPIO2_INT;
+18 -8
arch/mips/jz4740/board-qi_lb60.c
··· 313 313 .pixclk_falling_edge = 1, 314 314 }; 315 315 316 - struct spi_gpio_platform_data spigpio_platform_data = { 317 - .sck = JZ_GPIO_PORTC(23), 318 - .mosi = JZ_GPIO_PORTC(22), 319 - .miso = -1, 316 + struct spi_gpio_platform_data qi_lb60_spigpio_platform_data = { 320 317 .num_chipselect = 1, 321 318 }; 322 319 323 - static struct platform_device spigpio_device = { 320 + static struct platform_device qi_lb60_spigpio_device = { 324 321 .name = "spi_gpio", 325 322 .id = 1, 326 323 .dev = { 327 - .platform_data = &spigpio_platform_data, 324 + .platform_data = &qi_lb60_spigpio_platform_data, 325 + }, 326 + }; 327 + 328 + static struct gpiod_lookup_table qi_lb60_spigpio_gpio_table = { 329 + .dev_id = "spi_gpio", 330 + .table = { 331 + GPIO_LOOKUP("GPIOC", 23, 332 + "sck", GPIO_ACTIVE_HIGH), 333 + GPIO_LOOKUP("GPIOC", 22, 334 + "mosi", GPIO_ACTIVE_HIGH), 335 + GPIO_LOOKUP("GPIOC", 21, 336 + "cs", GPIO_ACTIVE_HIGH), 337 + { }, 328 338 }, 329 339 }; 330 340 331 341 static struct spi_board_info qi_lb60_spi_board_info[] = { 332 342 { 333 343 .modalias = "ili8960", 334 - .controller_data = (void *)JZ_GPIO_PORTC(21), 335 344 .chip_select = 0, 336 345 .bus_num = 1, 337 346 .max_speed_hz = 30 * 1000, ··· 444 435 &jz4740_mmc_device, 445 436 &jz4740_nand_device, 446 437 &qi_lb60_keypad, 447 - &spigpio_device, 438 + &qi_lb60_spigpio_device, 448 439 &jz4740_framebuffer_device, 449 440 &jz4740_pcm_device, 450 441 &jz4740_i2s_device, ··· 498 489 499 490 gpiod_add_lookup_table(&qi_lb60_audio_gpio_table); 500 491 gpiod_add_lookup_table(&qi_lb60_nand_gpio_table); 492 + gpiod_add_lookup_table(&qi_lb60_spigpio_gpio_table); 501 493 502 494 spi_register_board_info(qi_lb60_spi_board_info, 503 495 ARRAY_SIZE(qi_lb60_spi_board_info));
+25 -4
drivers/misc/eeprom/digsy_mtc_eeprom.c
··· 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License version 2 as 9 9 * published by the Free Software Foundation. 10 + * 11 + * FIXME: this driver is used on a device-tree probed platform: it 12 + * should be defined as a bit-banged SPI device and probed from the device 13 + * tree and not like this with static grabbing of a few numbered GPIO 14 + * lines at random. 15 + * 16 + * Add proper SPI and EEPROM in arch/powerpc/boot/dts/digsy_mtc.dts 17 + * and delete this driver. 10 18 */ 11 19 12 20 #include <linux/gpio.h> 21 + #include <linux/gpio/machine.h> 13 22 #include <linux/init.h> 14 23 #include <linux/platform_device.h> 15 24 #include <linux/spi/spi.h> ··· 51 42 }; 52 43 53 44 static struct spi_gpio_platform_data eeprom_spi_gpio_data = { 54 - .sck = GPIO_EEPROM_CLK, 55 - .mosi = GPIO_EEPROM_DI, 56 - .miso = GPIO_EEPROM_DO, 57 45 .num_chipselect = 1, 58 46 }; 59 47 ··· 62 56 }, 63 57 }; 64 58 59 + static struct gpiod_lookup_table eeprom_spi_gpiod_table = { 60 + .dev_id = "spi_gpio", 61 + .table = { 62 + GPIO_LOOKUP("gpio@b00", GPIO_EEPROM_CLK, 63 + "sck", GPIO_ACTIVE_HIGH), 64 + GPIO_LOOKUP("gpio@b00", GPIO_EEPROM_DI, 65 + "mosi", GPIO_ACTIVE_HIGH), 66 + GPIO_LOOKUP("gpio@b00", GPIO_EEPROM_DO, 67 + "miso", GPIO_ACTIVE_HIGH), 68 + GPIO_LOOKUP("gpio@b00", GPIO_EEPROM_CS, 69 + "cs", GPIO_ACTIVE_HIGH), 70 + { }, 71 + }, 72 + }; 73 + 65 74 static struct spi_board_info digsy_mtc_eeprom_info[] __initdata = { 66 75 { 67 76 .modalias = "93xx46", ··· 84 63 .bus_num = EE_SPI_BUS_NUM, 85 64 .chip_select = 0, 86 65 .mode = SPI_MODE_0, 87 - .controller_data = (void *)GPIO_EEPROM_CS, 88 66 .platform_data = &digsy_mtc_eeprom_data, 89 67 }, 90 68 }; ··· 98 78 pr_err("can't request gpio %d\n", GPIO_EEPROM_OE); 99 79 return ret; 100 80 } 81 + gpiod_add_lookup_table(&eeprom_spi_gpiod_table); 101 82 spi_register_board_info(digsy_mtc_eeprom_info, 102 83 ARRAY_SIZE(digsy_mtc_eeprom_info)); 103 84 return platform_device_register(&digsy_mtc_eeprom);
+92 -178
drivers/spi/spi-gpio.c
··· 2 2 * SPI master driver using generic bitbanged GPIO 3 3 * 4 4 * Copyright (C) 2006,2008 David Brownell 5 + * Copyright (C) 2017 Linus Walleij 5 6 * 6 7 * This program is free software; you can redistribute it and/or modify 7 8 * it under the terms of the GNU General Public License as published by ··· 17 16 #include <linux/kernel.h> 18 17 #include <linux/module.h> 19 18 #include <linux/platform_device.h> 20 - #include <linux/gpio.h> 19 + #include <linux/gpio/consumer.h> 21 20 #include <linux/of.h> 22 21 #include <linux/of_device.h> 23 - #include <linux/of_gpio.h> 24 22 25 23 #include <linux/spi/spi.h> 26 24 #include <linux/spi/spi_bitbang.h> ··· 44 44 struct spi_bitbang bitbang; 45 45 struct spi_gpio_platform_data pdata; 46 46 struct platform_device *pdev; 47 - unsigned long cs_gpios[0]; 47 + struct gpio_desc *sck; 48 + struct gpio_desc *miso; 49 + struct gpio_desc *mosi; 50 + struct gpio_desc **cs_gpios; 51 + bool has_cs; 48 52 }; 49 53 50 54 /*----------------------------------------------------------------------*/ ··· 81 77 82 78 #define GENERIC_BITBANG /* vs tight inlines */ 83 79 84 - /* all functions referencing these symbols must define pdata */ 85 - #define SPI_MISO_GPIO ((pdata)->miso) 86 - #define SPI_MOSI_GPIO ((pdata)->mosi) 87 - #define SPI_SCK_GPIO ((pdata)->sck) 88 - 89 - #define SPI_N_CHIPSEL ((pdata)->num_chipselect) 90 - 91 80 #endif 92 81 93 82 /*----------------------------------------------------------------------*/ ··· 102 105 return &spi_to_spi_gpio(spi)->pdata; 103 106 } 104 107 105 - /* this is #defined to avoid unused-variable warnings when inlining */ 106 - #define pdata spi_to_pdata(spi) 107 - 108 + /* These helpers are in turn called by the bitbang inlines */ 108 109 static inline void setsck(const struct spi_device *spi, int is_on) 109 110 { 110 - gpio_set_value_cansleep(SPI_SCK_GPIO, is_on); 111 + struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 112 + 113 + gpiod_set_value_cansleep(spi_gpio->sck, is_on); 111 114 } 112 115 113 116 static inline void setmosi(const struct spi_device *spi, int is_on) 114 117 { 115 - gpio_set_value_cansleep(SPI_MOSI_GPIO, is_on); 118 + struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 119 + 120 + gpiod_set_value_cansleep(spi_gpio->mosi, is_on); 116 121 } 117 122 118 123 static inline int getmiso(const struct spi_device *spi) 119 124 { 120 - return !!gpio_get_value_cansleep(SPI_MISO_GPIO); 121 - } 125 + struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 122 126 123 - #undef pdata 127 + return !!gpiod_get_value_cansleep(spi_gpio->miso); 128 + } 124 129 125 130 /* 126 131 * NOTE: this clocks "as fast as we can". It "should" be a function of the ··· 215 216 static void spi_gpio_chipselect(struct spi_device *spi, int is_active) 216 217 { 217 218 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 218 - unsigned long cs = spi_gpio->cs_gpios[spi->chip_select]; 219 219 220 - /* set initial clock polarity */ 220 + /* set initial clock line level */ 221 221 if (is_active) 222 - setsck(spi, spi->mode & SPI_CPOL); 222 + gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL); 223 223 224 - if (cs != SPI_GPIO_NO_CHIPSELECT) { 225 - /* SPI is normally active-low */ 226 - gpio_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); 224 + /* Drive chip select line, if we have one */ 225 + if (spi_gpio->has_cs) { 226 + struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select]; 227 + 228 + /* SPI chip selects are normally active-low */ 229 + gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); 227 230 } 228 231 } 229 232 230 233 static int spi_gpio_setup(struct spi_device *spi) 231 234 { 232 - unsigned long cs; 235 + struct gpio_desc *cs; 233 236 int status = 0; 234 237 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 235 - struct device_node *np = spi->master->dev.of_node; 236 238 237 - if (np) { 238 - /* 239 - * In DT environments, the CS GPIOs have already been 240 - * initialized from the "cs-gpios" property of the node. 241 - */ 242 - cs = spi_gpio->cs_gpios[spi->chip_select]; 243 - } else { 244 - /* 245 - * ... otherwise, take it from spi->controller_data 246 - */ 247 - cs = (uintptr_t) spi->controller_data; 248 - } 239 + /* 240 + * The CS GPIOs have already been 241 + * initialized from the descriptor lookup. 242 + */ 243 + cs = spi_gpio->cs_gpios[spi->chip_select]; 244 + if (!spi->controller_state && cs) 245 + status = gpiod_direction_output(cs, 246 + !(spi->mode & SPI_CS_HIGH)); 249 247 250 - if (!spi->controller_state) { 251 - if (cs != SPI_GPIO_NO_CHIPSELECT) { 252 - status = gpio_request(cs, dev_name(&spi->dev)); 253 - if (status) 254 - return status; 255 - status = gpio_direction_output(cs, 256 - !(spi->mode & SPI_CS_HIGH)); 257 - } 258 - } 259 - if (!status) { 260 - /* in case it was initialized from static board data */ 261 - spi_gpio->cs_gpios[spi->chip_select] = cs; 248 + if (!status) 262 249 status = spi_bitbang_setup(spi); 263 - } 264 250 265 - if (status) { 266 - if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT) 267 - gpio_free(cs); 268 - } 269 251 return status; 270 252 } 271 253 272 254 static void spi_gpio_cleanup(struct spi_device *spi) 273 255 { 274 - struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 275 - unsigned long cs = spi_gpio->cs_gpios[spi->chip_select]; 276 - 277 - if (cs != SPI_GPIO_NO_CHIPSELECT) 278 - gpio_free(cs); 279 256 spi_bitbang_cleanup(spi); 280 257 } 281 258 282 - static int spi_gpio_alloc(unsigned pin, const char *label, bool is_in) 259 + /* 260 + * It can be convenient to use this driver with pins that have alternate 261 + * functions associated with a "native" SPI controller if a driver for that 262 + * controller is not available, or is missing important functionality. 263 + * 264 + * On platforms which can do so, configure MISO with a weak pullup unless 265 + * there's an external pullup on that signal. That saves power by avoiding 266 + * floating signals. (A weak pulldown would save power too, but many 267 + * drivers expect to see all-ones data as the no slave "response".) 268 + */ 269 + static int spi_gpio_request(struct device *dev, 270 + struct spi_gpio *spi_gpio, 271 + unsigned int num_chipselects, 272 + u16 *mflags) 283 273 { 284 - int value; 274 + int i; 285 275 286 - value = gpio_request(pin, label); 287 - if (value == 0) { 288 - if (is_in) 289 - value = gpio_direction_input(pin); 290 - else 291 - value = gpio_direction_output(pin, 0); 292 - } 293 - return value; 294 - } 295 - 296 - static int spi_gpio_request(struct spi_gpio_platform_data *pdata, 297 - const char *label, u16 *res_flags) 298 - { 299 - int value; 300 - 301 - /* NOTE: SPI_*_GPIO symbols may reference "pdata" */ 302 - 303 - if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) { 304 - value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false); 305 - if (value) 306 - goto done; 307 - } else { 276 + spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); 277 + if (IS_ERR(spi_gpio->mosi)) 278 + return PTR_ERR(spi_gpio->mosi); 279 + if (!spi_gpio->mosi) 308 280 /* HW configuration without MOSI pin */ 309 - *res_flags |= SPI_MASTER_NO_TX; 310 - } 281 + *mflags |= SPI_MASTER_NO_TX; 311 282 312 - if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) { 313 - value = spi_gpio_alloc(SPI_MISO_GPIO, label, true); 314 - if (value) 315 - goto free_mosi; 316 - } else { 283 + spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); 284 + if (IS_ERR(spi_gpio->miso)) 285 + return PTR_ERR(spi_gpio->miso); 286 + if (!spi_gpio->miso) 317 287 /* HW configuration without MISO pin */ 318 - *res_flags |= SPI_MASTER_NO_RX; 288 + *mflags |= SPI_MASTER_NO_RX; 289 + 290 + spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); 291 + if (IS_ERR(spi_gpio->mosi)) 292 + return PTR_ERR(spi_gpio->mosi); 293 + 294 + for (i = 0; i < num_chipselects; i++) { 295 + spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", 296 + i, GPIOD_OUT_HIGH); 297 + if (IS_ERR(spi_gpio->cs_gpios[i])) 298 + return PTR_ERR(spi_gpio->cs_gpios[i]); 319 299 } 320 300 321 - value = spi_gpio_alloc(SPI_SCK_GPIO, label, false); 322 - if (value) 323 - goto free_miso; 324 - 325 - goto done; 326 - 327 - free_miso: 328 - if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 329 - gpio_free(SPI_MISO_GPIO); 330 - free_mosi: 331 - if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 332 - gpio_free(SPI_MOSI_GPIO); 333 - done: 334 - return value; 301 + return 0; 335 302 } 336 303 337 304 #ifdef CONFIG_OF ··· 323 358 if (!pdata) 324 359 return -ENOMEM; 325 360 326 - ret = of_get_named_gpio(np, "gpio-sck", 0); 327 - if (ret < 0) { 328 - dev_err(&pdev->dev, "gpio-sck property not found\n"); 329 - goto error_free; 330 - } 331 - pdata->sck = ret; 332 - 333 - ret = of_get_named_gpio(np, "gpio-miso", 0); 334 - if (ret < 0) { 335 - dev_info(&pdev->dev, "gpio-miso property not found, switching to no-rx mode\n"); 336 - pdata->miso = SPI_GPIO_NO_MISO; 337 - } else 338 - pdata->miso = ret; 339 - 340 - ret = of_get_named_gpio(np, "gpio-mosi", 0); 341 - if (ret < 0) { 342 - dev_info(&pdev->dev, "gpio-mosi property not found, switching to no-tx mode\n"); 343 - pdata->mosi = SPI_GPIO_NO_MOSI; 344 - } else 345 - pdata->mosi = ret; 346 361 347 362 ret = of_property_read_u32(np, "num-chipselects", &tmp); 348 363 if (ret < 0) { ··· 354 409 struct spi_gpio_platform_data *pdata; 355 410 u16 master_flags = 0; 356 411 bool use_of = 0; 357 - int num_devices; 358 412 359 413 status = spi_gpio_probe_dt(pdev); 360 414 if (status < 0) ··· 367 423 return -ENODEV; 368 424 #endif 369 425 370 - if (use_of && !SPI_N_CHIPSEL) 371 - num_devices = 1; 372 - else 373 - num_devices = SPI_N_CHIPSEL; 426 + master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio)); 427 + if (!master) 428 + return -ENOMEM; 374 429 375 - status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags); 376 - if (status < 0) 377 - return status; 378 - 379 - master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) + 380 - (sizeof(unsigned long) * num_devices)); 381 - if (!master) { 382 - status = -ENOMEM; 383 - goto gpio_free; 384 - } 385 430 spi_gpio = spi_master_get_devdata(master); 431 + 432 + spi_gpio->cs_gpios = devm_kzalloc(&pdev->dev, 433 + pdata->num_chipselect * sizeof(*spi_gpio->cs_gpios), 434 + GFP_KERNEL); 435 + if (!spi_gpio->cs_gpios) 436 + return -ENOMEM; 437 + 386 438 platform_set_drvdata(pdev, spi_gpio); 439 + 440 + /* Determine if we have chip selects connected */ 441 + spi_gpio->has_cs = !!pdata->num_chipselect; 387 442 388 443 spi_gpio->pdev = pdev; 389 444 if (pdata) 390 445 spi_gpio->pdata = *pdata; 391 446 447 + status = spi_gpio_request(&pdev->dev, spi_gpio, 448 + pdata->num_chipselect, &master_flags); 449 + if (status) 450 + return status; 451 + 392 452 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); 393 453 master->flags = master_flags; 394 454 master->bus_num = pdev->id; 395 - master->num_chipselect = num_devices; 455 + /* The master needs to think there is a chipselect even if not connected */ 456 + master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1; 396 457 master->setup = spi_gpio_setup; 397 458 master->cleanup = spi_gpio_cleanup; 398 459 #ifdef CONFIG_OF 399 460 master->dev.of_node = pdev->dev.of_node; 400 - 401 - if (use_of) { 402 - int i; 403 - struct device_node *np = pdev->dev.of_node; 404 - 405 - /* 406 - * In DT environments, take the CS GPIO from the "cs-gpios" 407 - * property of the node. 408 - */ 409 - 410 - if (!SPI_N_CHIPSEL) 411 - spi_gpio->cs_gpios[0] = SPI_GPIO_NO_CHIPSELECT; 412 - else 413 - for (i = 0; i < SPI_N_CHIPSEL; i++) { 414 - status = of_get_named_gpio(np, "cs-gpios", i); 415 - if (status < 0) { 416 - dev_err(&pdev->dev, 417 - "invalid cs-gpios property\n"); 418 - goto gpio_free; 419 - } 420 - spi_gpio->cs_gpios[i] = status; 421 - } 422 - } 423 461 #endif 424 462 425 463 spi_gpio->bitbang.master = master; ··· 422 496 spi_gpio->bitbang.flags = SPI_CS_HIGH; 423 497 424 498 status = spi_bitbang_start(&spi_gpio->bitbang); 425 - if (status < 0) { 426 - gpio_free: 427 - if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 428 - gpio_free(SPI_MISO_GPIO); 429 - if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 430 - gpio_free(SPI_MOSI_GPIO); 431 - gpio_free(SPI_SCK_GPIO); 499 + if (status) 432 500 spi_master_put(master); 433 - } 434 501 435 502 return status; 436 503 } ··· 439 520 /* stop() unregisters child devices too */ 440 521 spi_bitbang_stop(&spi_gpio->bitbang); 441 522 442 - if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) 443 - gpio_free(SPI_MISO_GPIO); 444 - if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) 445 - gpio_free(SPI_MOSI_GPIO); 446 - gpio_free(SPI_SCK_GPIO); 447 523 spi_master_put(spi_gpio->bitbang.master); 448 524 449 525 return 0;
+1 -48
include/linux/spi/spi_gpio.h
··· 8 8 * - id the same as the SPI bus number it implements 9 9 * - dev.platform data pointing to a struct spi_gpio_platform_data 10 10 * 11 - * Or, see the driver code for information about speedups that are 12 - * possible on platforms that support inlined access for GPIOs (no 13 - * spi_gpio_platform_data is used). 14 - * 15 - * Use spi_board_info with these busses in the usual way, being sure 16 - * that the controller_data being the GPIO used for each device's 17 - * chipselect: 18 - * 19 - * static struct spi_board_info ... [] = { 20 - * ... 21 - * // this slave uses GPIO 42 for its chipselect 22 - * .controller_data = (void *) 42, 23 - * ... 24 - * // this one uses GPIO 86 for its chipselect 25 - * .controller_data = (void *) 86, 26 - * ... 27 - * }; 28 - * 29 - * If chipselect is not used (there's only one device on the bus), assign 30 - * SPI_GPIO_NO_CHIPSELECT to the controller_data: 31 - * .controller_data = (void *) SPI_GPIO_NO_CHIPSELECT; 32 - * 33 - * If the MISO or MOSI pin is not available then it should be set to 34 - * SPI_GPIO_NO_MISO or SPI_GPIO_NO_MOSI. 11 + * Use spi_board_info with these busses in the usual way. 35 12 * 36 13 * If the bitbanged bus is later switched to a "native" controller, 37 14 * that platform_device and controller_data should be removed. 38 15 */ 39 16 40 - #define SPI_GPIO_NO_CHIPSELECT ((unsigned long)-1l) 41 - #define SPI_GPIO_NO_MISO ((unsigned long)-1l) 42 - #define SPI_GPIO_NO_MOSI ((unsigned long)-1l) 43 - 44 17 /** 45 18 * struct spi_gpio_platform_data - parameter for bitbanged SPI master 46 - * @sck: number of the GPIO used for clock output 47 - * @mosi: number of the GPIO used for Master Output, Slave In (MOSI) data 48 - * @miso: number of the GPIO used for Master Input, Slave Output (MISO) data 49 19 * @num_chipselect: how many slaves to allow 50 - * 51 - * All GPIO signals used with the SPI bus managed through this driver 52 - * (chipselects, MOSI, MISO, SCK) must be configured as GPIOs, instead 53 - * of some alternate function. 54 - * 55 - * It can be convenient to use this driver with pins that have alternate 56 - * functions associated with a "native" SPI controller if a driver for that 57 - * controller is not available, or is missing important functionality. 58 - * 59 - * On platforms which can do so, configure MISO with a weak pullup unless 60 - * there's an external pullup on that signal. That saves power by avoiding 61 - * floating signals. (A weak pulldown would save power too, but many 62 - * drivers expect to see all-ones data as the no slave "response".) 63 20 */ 64 21 struct spi_gpio_platform_data { 65 - unsigned sck; 66 - unsigned long mosi; 67 - unsigned long miso; 68 - 69 22 u16 num_chipselect; 70 23 }; 71 24