Skip to content

Commit 6ee3961

Browse files
Phil Elwellpopcornmix
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 e3ee149 commit 6ee3961

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
@@ -359,6 +359,8 @@ static int bcm2835_spi_setup(struct spi_device *spi)
359359
{
360360
int err;
361361
struct gpio_chip *chip;
362+
struct device_node *pins;
363+
u32 pingroup_index;
362364
/*
363365
* sanity checking the native-chipselects
364366
*/
@@ -375,15 +377,42 @@ static int bcm2835_spi_setup(struct spi_device *spi)
375377
"setup: only two native chip-selects are supported\n");
376378
return -EINVAL;
377379
}
378-
/* now translate native cs to GPIO */
379-
380-
/* get the gpio chip for the base */
381-
chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
382-
if (!chip)
383-
return 0;
384380

385-
/* and calculate the real CS */
386-
spi->cs_gpio = chip->base + 8 - spi->chip_select;
381+
/* now translate native cs to GPIO */
382+
/* first look for chip select pins in the devices pin groups */
383+
for (pingroup_index = 0;
384+
(pins = of_parse_phandle(spi->master->dev.of_node,
385+
"pinctrl-0",
386+
pingroup_index)) != 0;
387+
pingroup_index++) {
388+
u32 pin;
389+
u32 pin_index;
390+
for (pin_index = 0;
391+
of_property_read_u32_index(pins,
392+
"brcm,pins",
393+
pin_index,
394+
&pin) == 0;
395+
pin_index++) {
396+
if (((spi->chip_select == 0) &&
397+
((pin == 8) || (pin == 36) || (pin == 46))) ||
398+
((spi->chip_select == 1) &&
399+
((pin == 7) || (pin == 35)))) {
400+
spi->cs_gpio = pin;
401+
break;
402+
}
403+
}
404+
of_node_put(pins);
405+
}
406+
/* if that fails, assume GPIOs 7-11 are used */
407+
if (!gpio_is_valid(spi->cs_gpio) ) {
408+
/* get the gpio chip for the base */
409+
chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
410+
if (!chip)
411+
return 0;
412+
413+
/* and calculate the real CS */
414+
spi->cs_gpio = chip->base + 8 - spi->chip_select;
415+
}
387416

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

0 commit comments

Comments
 (0)