Skip to content

Commit a30a555

Browse files
msperlbroonie
authored andcommitted
spi: bcm2835: transform native-cs to gpio-cs on first spi_setup
Transforms the bcm-2835 native SPI-chip select to their gpio-cs equivalent. This allows for some support of some optimizations that are not possible due to HW-gliches on the CS line - especially filling the FIFO before enabling SPI interrupts (by writing to CS register) while the transfer is already in progress (See commit: e3a2be3) This patch also works arround some issues in bcm2835-pinctrl which does not set the value when setting the GPIO as output - it just sets up output and (typically) leaves the GPIO as low. When a fix for this is merged then this gpio_set_value can get removed from bcm2835_spi_setup. Signed-off-by: Martin Sperl <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent e3a2be3 commit a30a555

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

drivers/spi/spi-bcm2835.c

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,22 +291,61 @@ static void bcm2835_spi_set_cs(struct spi_device *spi, bool gpio_level)
291291
bcm2835_wr(bs, BCM2835_SPI_CS, cs);
292292
}
293293

294+
static int chip_match_name(struct gpio_chip *chip, void *data)
295+
{
296+
return !strcmp(chip->label, data);
297+
}
298+
294299
static int bcm2835_spi_setup(struct spi_device *spi)
295300
{
301+
int err;
302+
struct gpio_chip *chip;
296303
/*
297304
* sanity checking the native-chipselects
298305
*/
299306
if (spi->mode & SPI_NO_CS)
300307
return 0;
301308
if (gpio_is_valid(spi->cs_gpio))
302309
return 0;
303-
if (spi->chip_select < 3)
310+
if (spi->chip_select > 1) {
311+
/* error in the case of native CS requested with CS > 1
312+
* officially there is a CS2, but it is not documented
313+
* which GPIO is connected with that...
314+
*/
315+
dev_err(&spi->dev,
316+
"setup: only two native chip-selects are supported\n");
317+
return -EINVAL;
318+
}
319+
/* now translate native cs to GPIO */
320+
321+
/* get the gpio chip for the base */
322+
chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
323+
if (!chip)
304324
return 0;
305325

306-
/* error in the case of native CS requested with CS-id > 2 */
307-
dev_err(&spi->dev,
308-
"setup: only three native chip-selects are supported\n");
309-
return -EINVAL;
326+
/* and calculate the real CS */
327+
spi->cs_gpio = chip->base + 8 - spi->chip_select;
328+
329+
/* and set up the "mode" and level */
330+
dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n",
331+
spi->chip_select, spi->cs_gpio);
332+
333+
/* set up GPIO as output and pull to the correct level */
334+
err = gpio_direction_output(spi->cs_gpio,
335+
(spi->mode & SPI_CS_HIGH) ? 0 : 1);
336+
if (err) {
337+
dev_err(&spi->dev,
338+
"could not set CS%i gpio %i as output: %i",
339+
spi->chip_select, spi->cs_gpio, err);
340+
return err;
341+
}
342+
/* the implementation of pinctrl-bcm2835 currently does not
343+
* set the GPIO value when using gpio_direction_output
344+
* so we are setting it here explicitly
345+
*/
346+
gpio_set_value(spi->cs_gpio, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
347+
348+
return 0;
310349
}
311350

312351
static int bcm2835_spi_probe(struct platform_device *pdev)

0 commit comments

Comments
 (0)