Skip to content

Commit c633e48

Browse files
Nicolas Ferrealexandrebelloni
authored andcommitted
spi: atmel: use managed resource for gpio chip select
Use the managed gpio CS pin request so that we avoid having trouble in the cleanup code. In fact, if module was configured with DT, cleanup code released invalid pin. Since resource wasn't freed, module cannot be reinserted. This require to extract the gpio request call from the "setup" function and call it in the appropriate probe function. Reported-by: Alexander Morozov <linux@meltdown.ru> Signed-off-by: Nicolas Ferre <nicolas.ferre@atmel.com> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 69973b8 commit c633e48

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

drivers/spi/spi-atmel.c

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <linux/io.h>
2626
#include <linux/gpio.h>
27+
#include <linux/of_gpio.h>
2728
#include <linux/pinctrl/consumer.h>
2829
#include <linux/pm_runtime.h>
2930

@@ -1204,7 +1205,6 @@ static int atmel_spi_setup(struct spi_device *spi)
12041205
u32 csr;
12051206
unsigned int bits = spi->bits_per_word;
12061207
unsigned int npcs_pin;
1207-
int ret;
12081208

12091209
as = spi_master_get_devdata(spi->master);
12101210

@@ -1247,16 +1247,9 @@ static int atmel_spi_setup(struct spi_device *spi)
12471247
if (!asd)
12481248
return -ENOMEM;
12491249

1250-
if (as->use_cs_gpios) {
1251-
ret = gpio_request(npcs_pin, dev_name(&spi->dev));
1252-
if (ret) {
1253-
kfree(asd);
1254-
return ret;
1255-
}
1256-
1250+
if (as->use_cs_gpios)
12571251
gpio_direction_output(npcs_pin,
12581252
!(spi->mode & SPI_CS_HIGH));
1259-
}
12601253

12611254
asd->npcs_pin = npcs_pin;
12621255
spi->controller_state = asd;
@@ -1471,13 +1464,11 @@ static int atmel_spi_transfer_one_message(struct spi_master *master,
14711464
static void atmel_spi_cleanup(struct spi_device *spi)
14721465
{
14731466
struct atmel_spi_device *asd = spi->controller_state;
1474-
unsigned gpio = (unsigned long) spi->controller_data;
14751467

14761468
if (!asd)
14771469
return;
14781470

14791471
spi->controller_state = NULL;
1480-
gpio_free(gpio);
14811472
kfree(asd);
14821473
}
14831474

@@ -1499,6 +1490,39 @@ static void atmel_get_caps(struct atmel_spi *as)
14991490
}
15001491

15011492
/*-------------------------------------------------------------------------*/
1493+
static int atmel_spi_gpio_cs(struct platform_device *pdev)
1494+
{
1495+
struct spi_master *master = platform_get_drvdata(pdev);
1496+
struct atmel_spi *as = spi_master_get_devdata(master);
1497+
struct device_node *np = master->dev.of_node;
1498+
int i;
1499+
int ret = 0;
1500+
int nb = 0;
1501+
1502+
if (!as->use_cs_gpios)
1503+
return 0;
1504+
1505+
if (!np)
1506+
return 0;
1507+
1508+
nb = of_gpio_named_count(np, "cs-gpios");
1509+
for (i = 0; i < nb; i++) {
1510+
int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
1511+
"cs-gpios", i);
1512+
1513+
if (cs_gpio == -EPROBE_DEFER)
1514+
return cs_gpio;
1515+
1516+
if (gpio_is_valid(cs_gpio)) {
1517+
ret = devm_gpio_request(&pdev->dev, cs_gpio,
1518+
dev_name(&pdev->dev));
1519+
if (ret)
1520+
return ret;
1521+
}
1522+
}
1523+
1524+
return 0;
1525+
}
15021526

15031527
static int atmel_spi_probe(struct platform_device *pdev)
15041528
{
@@ -1577,6 +1601,10 @@ static int atmel_spi_probe(struct platform_device *pdev)
15771601
master->num_chipselect = 4;
15781602
}
15791603

1604+
ret = atmel_spi_gpio_cs(pdev);
1605+
if (ret)
1606+
goto out_unmap_regs;
1607+
15801608
as->use_dma = false;
15811609
as->use_pdc = false;
15821610
if (as->caps.has_dma_support) {

0 commit comments

Comments
 (0)