Skip to contentSkip to conversation

spi: davinci: request cs_gpio's from probe

PATCH New Grygorii Strashko 2 participants 6 messages Sep 12, 2014 – Sep 16, 2014 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: davinci: request cs_gpio's from probe

Now CS GPIOs are requested from struct spi_master.setup() callback and that causes failures when Client SPI device is getting accessed through SPIDEV driver. The failure happens, because .setup() callback may be called many times from IOCTL handler and when it's called second time gpio_request() will fail and return -EBUSY.

Hence, fix it by moving CS GPIOs requesting code in .probe().

---
Tested using spidev_test utility.

 drivers/spi/spi-davinci.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)
drivers/spi/spi-davinci.c +25 -9
index 48f1d26..d493507 100644
@@ -397,24 +397,21 @@ static int davinci_spi_setup(struct spi_device *spi)
 	struct spi_master *master = spi->master;
 	struct device_node *np = spi->dev.of_node;
 	bool internal_cs = true;
-	unsigned long flags = GPIOF_DIR_OUT;
 
 	dspi = spi_master_get_devdata(spi->master);
 	pdata = &dspi->pdata;
 
-	flags |= (spi->mode & SPI_CS_HIGH) ? GPIOF_INIT_LOW : GPIOF_INIT_HIGH;
-
 	if (!(spi->mode & SPI_NO_CS)) {
 		if (np && (master->cs_gpios != NULL) && (spi->cs_gpio >= 0)) {
-			retval = gpio_request_one(spi->cs_gpio,
-						  flags, dev_name(&spi->dev));
+			retval = gpio_direction_output(
+				      spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
 			internal_cs = false;
 		} else if (pdata->chip_sel &&
 			   spi->chip_select < pdata->num_chipselect &&
 			   pdata->chip_sel[spi->chip_select] != SPI_INTERN_CS) {
 			spi->cs_gpio = pdata->chip_sel[spi->chip_select];
-			retval = gpio_request_one(spi->cs_gpio,
-						  flags, dev_name(&spi->dev));
+			retval = gpio_direction_output(
+				      spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
 			internal_cs = false;
 		}
 
@@ -441,8 +438,6 @@ static int davinci_spi_setup(struct spi_device *spi)
 
 static void davinci_spi_cleanup(struct spi_device *spi)
 {
-	if (spi->cs_gpio >= 0)
-		gpio_free(spi->cs_gpio);
 }
 
 static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
@@ -967,6 +962,27 @@ static int davinci_spi_probe(struct platform_device *pdev)
 	if (dspi->version == SPI_VERSION_2)
 		dspi->bitbang.flags |= SPI_READY;
 
+	if (pdev->dev.of_node) {
+		int i;
+
+		for (i = 0; i < pdata->num_chipselect; i++) {
+			int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
+							"cs-gpios", i);
+
+			if (cs_gpio == -EPROBE_DEFER) {
+				ret = cs_gpio;
+				goto free_clk;
+			}
+
+			if (gpio_is_valid(cs_gpio)) {
+				ret = devm_gpio_request(&pdev->dev, cs_gpio,
+							dev_name(&pdev->dev));
+				if (ret)
+					goto free_clk;
+			}
+		}
+	}
+
 	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
 	if (r)
 		dma_rx_chan = r->start;
Signature · 1 line
1.9.1
maintainer
#1 lore raw

PATCH 1/1 spi: davinci: request cs_gpio's from probe

drivers/spi/spi-davinci.c static int davinci_spi_setup(struct spi_device *spi) · line 441

Quoted 5 lines · Grygorii Strashko

static void davinci_spi_cleanup(struct spi_device *spi) {

drivers/spi/spi-davinci.c +0 -2
-	if (spi->cs_gpio >= 0)
-		gpio_free(spi->cs_gpio);
 }

This function is now empty so should be removed. I've applied for now
but please send a followup fixing this.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20140913/07d71215/attachment.sig>

#2 lore raw

PATCH 1/1 spi: davinci: request cs_gpio's from probe

Quoted 11 lines · Mark Brown

drivers/spi/spi-davinci.c static int davinci_spi_setup(struct spi_device *spi) · line 441

Quoted 5 lines · Grygorii Strashko
  static void davinci_spi_cleanup(struct spi_device *spi)
  {
drivers/spi/spi-davinci.c +0 -2
-	if (spi->cs_gpio >= 0)
-		gpio_free(spi->cs_gpio);
  }

This function is now empty so should be removed. I've applied for now but please send a followup fixing this.

The function davinci_spi_cleanup() will be reused by following patch "[PATCH v3] spi: davinci: add support for adding delay between word's transmissions"
http://www.spinics.net/lists/devicetree/msg49141.html

So, in broonie/spi.git/for-next it will not be empty.

Again, I've missed description of this dependency, sorry for that.

Regards,
-grygorii

maintainer
#3 lore raw

PATCH 1/1 spi: davinci: request cs_gpio's from probe

Quoted 7 lines · Grygorii Strashko

The function davinci_spi_cleanup() will be reused by following patch "[PATCH v3] spi: davinci: add support for adding delay between word's transmissions"
http://www.spinics.net/lists/devicetree/msg49141.html

So, in broonie/spi.git/for-next it will not be empty.

Again, I've missed description of this dependency, sorry for that.

Don't do things like this, delete the function and then re-add it - it
makes the review simpler and it means that if the second patch doesn't
get applied (as will happen here for v3.17) then the code still looks
good.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20140915/88c49bd2/attachment.sig>

#4 lore raw

PATCH 1/1 spi: davinci: remove empty function davinci_spi_cleanup

Remove empty function davinci_spi_cleanup().

---
 drivers/spi/spi-davinci.c | 5 -----
 1 file changed, 5 deletions(-)
drivers/spi/spi-davinci.c +0 -5
index d493507..134fb6e 100644
@@ -436,10 +436,6 @@ static int davinci_spi_setup(struct spi_device *spi)
 	return retval;
 }
 
-static void davinci_spi_cleanup(struct spi_device *spi)
-{
-}
-
 static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
 {
 	struct device *sdev = dspi->bitbang.master->dev.parent;
@@ -951,7 +947,6 @@ static int davinci_spi_probe(struct platform_device *pdev)
 	master->num_chipselect = pdata->num_chipselect;
 	master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
 	master->setup = davinci_spi_setup;
-	master->cleanup = davinci_spi_cleanup;
 
 	dspi->bitbang.chipselect = davinci_spi_chipselect;
 	dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
Signature · 1 line
1.9.1