Skip to content

Commit 2eb6f8d

Browse files
Phil ElwellTiejunChina
Phil Elwell
authored andcommitted
spi-bcm2835: Support pin groups other than 7-11
The spi-bcm2835 driver automatically uses GPIO chip-selects due to some unreliability of the native ones. In doing so it chooses the same pins as the native chip-selects would use, but the existing code always uses pins 7 and 8, wherever the SPI function is mapped. Search the pinctrl group assigned to the driver for pins that correspond to native chip-selects, and use those for GPIO chip- selects. Signed-off-by: Phil Elwell <[email protected]>
1 parent d836d69 commit 2eb6f8d

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

drivers/spi/spi-bcm2835.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,8 @@ static int bcm2835_spi_setup(struct spi_device *spi)
686686
{
687687
int err;
688688
struct gpio_chip *chip;
689+
struct device_node *pins;
690+
u32 pingroup_index;
689691
/*
690692
* sanity checking the native-chipselects
691693
*/
@@ -702,15 +704,42 @@ static int bcm2835_spi_setup(struct spi_device *spi)
702704
"setup: only two native chip-selects are supported\n");
703705
return -EINVAL;
704706
}
705-
/* now translate native cs to GPIO */
706-
707-
/* get the gpio chip for the base */
708-
chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
709-
if (!chip)
710-
return 0;
711707

712-
/* and calculate the real CS */
713-
spi->cs_gpio = chip->base + 8 - spi->chip_select;
708+
/* now translate native cs to GPIO */
709+
/* first look for chip select pins in the devices pin groups */
710+
for (pingroup_index = 0;
711+
(pins = of_parse_phandle(spi->master->dev.of_node,
712+
"pinctrl-0",
713+
pingroup_index)) != 0;
714+
pingroup_index++) {
715+
u32 pin;
716+
u32 pin_index;
717+
for (pin_index = 0;
718+
of_property_read_u32_index(pins,
719+
"brcm,pins",
720+
pin_index,
721+
&pin) == 0;
722+
pin_index++) {
723+
if (((spi->chip_select == 0) &&
724+
((pin == 8) || (pin == 36) || (pin == 46))) ||
725+
((spi->chip_select == 1) &&
726+
((pin == 7) || (pin == 35)))) {
727+
spi->cs_gpio = pin;
728+
break;
729+
}
730+
}
731+
of_node_put(pins);
732+
}
733+
/* if that fails, assume GPIOs 7-11 are used */
734+
if (!gpio_is_valid(spi->cs_gpio) ) {
735+
/* get the gpio chip for the base */
736+
chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
737+
if (!chip)
738+
return 0;
739+
740+
/* and calculate the real CS */
741+
spi->cs_gpio = chip->base + 8 - spi->chip_select;
742+
}
714743

715744
/* and set up the "mode" and level */
716745
dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n",

0 commit comments

Comments
 (0)