All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@siemens.com>
To: Mark Brown <broonie@kernel.org>, Daniel Mack <daniel@zonque.org>,
	Haojian Zhuang <haojian.zhuang@gmail.com>,
	Robert Jarzmik <robert.jarzmik@free.fr>,
	Andy Shevchenko <andy.shevchenko@gmail.com>,
	Mika Westerberg <mika.westerberg@linux.intel.com>
Cc: linux-spi <linux-spi@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-arm-kernel <linux-arm-kernel@lists.infradead.org>
Subject: [PATCH RFC 2/2] spi: pxa2xx: Only claim CS GPIOs when the slave device is created
Date: Thu,  3 Aug 2017 13:40:33 +0200	[thread overview]
Message-ID: <5a691263ccf9d61134d6184861b37eaf37b13b77.1501760433.git.jan.kiszka@siemens.com> (raw)
In-Reply-To: <cover.1501760433.git.jan.kiszka@siemens.com>
In-Reply-To: <cover.1501760433.git.jan.kiszka@siemens.com>

From: Jan Kiszka <jan.kiszka@siemens.com>

Avoid hogging chip select GPIOs just because they are listed for the
master. They might be mulitplexed and, if no slave device is attached,
used for different purposes. Moreover, this strategy avoids having to
allocate a cs_gpiods structure.

Tested on the IOT2000 where the second SPI bus is connected to an
Arduino-compatible connector and multiplexed between SPI, GPIO and PWM
usage.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 drivers/spi/spi-pxa2xx.c | 68 +++++++++++++++++++++---------------------------
 drivers/spi/spi-pxa2xx.h |  3 +--
 2 files changed, 31 insertions(+), 40 deletions(-)

diff --git a/drivers/spi/spi-pxa2xx.c b/drivers/spi/spi-pxa2xx.c
index 7faba738110c..577a2f4ba3bf 100644
--- a/drivers/spi/spi-pxa2xx.c
+++ b/drivers/spi/spi-pxa2xx.c
@@ -1213,19 +1213,34 @@ static int setup_cs(struct spi_device *spi, struct chip_data *chip,
 		    struct pxa2xx_spi_chip *chip_info)
 {
 	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
+	struct device *pdev = &drv_data->pdev->dev;
 	struct gpio_desc *gpiod;
 	int err = 0;
 
 	if (chip == NULL)
 		return 0;
 
-	if (drv_data->cs_gpiods) {
-		gpiod = drv_data->cs_gpiods[spi->chip_select];
-		if (gpiod) {
-			chip->gpiod_cs = gpiod;
-			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
-			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
+	if (drv_data->cs_count > 0) {
+		/* setup() might be called multiple times. */
+		if (chip->gpiod_cs)
+			return 0;
+
+		if (spi->chip_select >= drv_data->cs_count)
+			return -EINVAL;
+
+		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
+
+		gpiod = gpiod_get_index(pdev, "cs", spi->chip_select,
+					chip->gpio_cs_inverted ?
+				        GPIOD_OUT_HIGH : GPIOD_OUT_LOW);
+		if (IS_ERR(gpiod)) {
+			/* Means use native chip select */
+			if (PTR_ERR(gpiod) == -ENOENT)
+				return 0;
+
+			return (int)PTR_ERR(gpiod);
 		}
+		chip->gpiod_cs = gpiod;
 
 		return 0;
 	}
@@ -1415,9 +1430,13 @@ static void cleanup(struct spi_device *spi)
 	if (!chip)
 		return;
 
-	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
-	    chip->gpiod_cs)
-		gpio_free(desc_to_gpio(chip->gpiod_cs));
+	if (drv_data->ssp_type != CE4100_SSP && chip->gpiod_cs) {
+		if (drv_data->cs_count > 0)
+			gpiod_put(chip->gpiod_cs);
+		else
+			gpio_free(desc_to_gpio(chip->gpiod_cs));
+		chip->gpiod_cs = NULL;
+	}
 
 	kfree(chip);
 }
@@ -1752,37 +1771,10 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 	master->num_chipselect = platform_info->num_chipselect;
 
 	count = gpiod_count(&pdev->dev, "cs");
-	if (count > 0) {
-		int i;
-
+	if (count > 0)
 		master->num_chipselect = max_t(int, count,
 			master->num_chipselect);
-
-		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
-			master->num_chipselect, sizeof(struct gpio_desc *),
-			GFP_KERNEL);
-		if (!drv_data->cs_gpiods) {
-			status = -ENOMEM;
-			goto out_error_clock_enabled;
-		}
-
-		for (i = 0; i < master->num_chipselect; i++) {
-			struct gpio_desc *gpiod;
-
-			gpiod = devm_gpiod_get_index(dev, "cs", i,
-						     GPIOD_OUT_HIGH);
-			if (IS_ERR(gpiod)) {
-				/* Means use native chip select */
-				if (PTR_ERR(gpiod) == -ENOENT)
-					continue;
-
-				status = (int)PTR_ERR(gpiod);
-				goto out_error_clock_enabled;
-			} else {
-				drv_data->cs_gpiods[i] = gpiod;
-			}
-		}
-	}
+	drv_data->cs_count = count;
 
 	tasklet_init(&drv_data->pump_transfers, pump_transfers,
 		     (unsigned long)drv_data);
diff --git a/drivers/spi/spi-pxa2xx.h b/drivers/spi/spi-pxa2xx.h
index 94f7b0713281..8551c881f636 100644
--- a/drivers/spi/spi-pxa2xx.h
+++ b/drivers/spi/spi-pxa2xx.h
@@ -67,8 +67,7 @@ struct driver_data {
 
 	void __iomem *lpss_base;
 
-	/* GPIOs for chip selects */
-	struct gpio_desc **cs_gpiods;
+	int cs_count;
 };
 
 struct chip_data {
-- 
2.12.3

WARNING: multiple messages have this Message-ID (diff)
From: Jan Kiszka <jan.kiszka@siemens.com>
To: Mark Brown <broonie@kernel.org>, Daniel Mack <daniel@zonque.org>,
	Haojian Zhuang <haojian.zhuang@gmail.com>,
	Robert Jarzmik <robert.jarzmik@free.fr>,
	Andy Shevchenko <andy.shevchenko@gmail.com>,
	Mika Westerberg <mika.westerberg@linux.intel.com>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-arm-kernel <linux-arm-kernel@lists.infradead.org>,
	linux-spi <linux-spi@vger.kernel.org>
Subject: [PATCH RFC 2/2] spi: pxa2xx: Only claim CS GPIOs when the slave device is created
Date: Thu,  3 Aug 2017 13:40:33 +0200	[thread overview]
Message-ID: <5a691263ccf9d61134d6184861b37eaf37b13b77.1501760433.git.jan.kiszka@siemens.com> (raw)
In-Reply-To: <cover.1501760433.git.jan.kiszka@siemens.com>
In-Reply-To: <cover.1501760433.git.jan.kiszka@siemens.com>

From: Jan Kiszka <jan.kiszka@siemens.com>

Avoid hogging chip select GPIOs just because they are listed for the
master. They might be mulitplexed and, if no slave device is attached,
used for different purposes. Moreover, this strategy avoids having to
allocate a cs_gpiods structure.

Tested on the IOT2000 where the second SPI bus is connected to an
Arduino-compatible connector and multiplexed between SPI, GPIO and PWM
usage.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 drivers/spi/spi-pxa2xx.c | 68 +++++++++++++++++++++---------------------------
 drivers/spi/spi-pxa2xx.h |  3 +--
 2 files changed, 31 insertions(+), 40 deletions(-)

diff --git a/drivers/spi/spi-pxa2xx.c b/drivers/spi/spi-pxa2xx.c
index 7faba738110c..577a2f4ba3bf 100644
--- a/drivers/spi/spi-pxa2xx.c
+++ b/drivers/spi/spi-pxa2xx.c
@@ -1213,19 +1213,34 @@ static int setup_cs(struct spi_device *spi, struct chip_data *chip,
 		    struct pxa2xx_spi_chip *chip_info)
 {
 	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
+	struct device *pdev = &drv_data->pdev->dev;
 	struct gpio_desc *gpiod;
 	int err = 0;
 
 	if (chip == NULL)
 		return 0;
 
-	if (drv_data->cs_gpiods) {
-		gpiod = drv_data->cs_gpiods[spi->chip_select];
-		if (gpiod) {
-			chip->gpiod_cs = gpiod;
-			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
-			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
+	if (drv_data->cs_count > 0) {
+		/* setup() might be called multiple times. */
+		if (chip->gpiod_cs)
+			return 0;
+
+		if (spi->chip_select >= drv_data->cs_count)
+			return -EINVAL;
+
+		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
+
+		gpiod = gpiod_get_index(pdev, "cs", spi->chip_select,
+					chip->gpio_cs_inverted ?
+				        GPIOD_OUT_HIGH : GPIOD_OUT_LOW);
+		if (IS_ERR(gpiod)) {
+			/* Means use native chip select */
+			if (PTR_ERR(gpiod) == -ENOENT)
+				return 0;
+
+			return (int)PTR_ERR(gpiod);
 		}
+		chip->gpiod_cs = gpiod;
 
 		return 0;
 	}
@@ -1415,9 +1430,13 @@ static void cleanup(struct spi_device *spi)
 	if (!chip)
 		return;
 
-	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
-	    chip->gpiod_cs)
-		gpio_free(desc_to_gpio(chip->gpiod_cs));
+	if (drv_data->ssp_type != CE4100_SSP && chip->gpiod_cs) {
+		if (drv_data->cs_count > 0)
+			gpiod_put(chip->gpiod_cs);
+		else
+			gpio_free(desc_to_gpio(chip->gpiod_cs));
+		chip->gpiod_cs = NULL;
+	}
 
 	kfree(chip);
 }
@@ -1752,37 +1771,10 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 	master->num_chipselect = platform_info->num_chipselect;
 
 	count = gpiod_count(&pdev->dev, "cs");
-	if (count > 0) {
-		int i;
-
+	if (count > 0)
 		master->num_chipselect = max_t(int, count,
 			master->num_chipselect);
-
-		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
-			master->num_chipselect, sizeof(struct gpio_desc *),
-			GFP_KERNEL);
-		if (!drv_data->cs_gpiods) {
-			status = -ENOMEM;
-			goto out_error_clock_enabled;
-		}
-
-		for (i = 0; i < master->num_chipselect; i++) {
-			struct gpio_desc *gpiod;
-
-			gpiod = devm_gpiod_get_index(dev, "cs", i,
-						     GPIOD_OUT_HIGH);
-			if (IS_ERR(gpiod)) {
-				/* Means use native chip select */
-				if (PTR_ERR(gpiod) == -ENOENT)
-					continue;
-
-				status = (int)PTR_ERR(gpiod);
-				goto out_error_clock_enabled;
-			} else {
-				drv_data->cs_gpiods[i] = gpiod;
-			}
-		}
-	}
+	drv_data->cs_count = count;
 
 	tasklet_init(&drv_data->pump_transfers, pump_transfers,
 		     (unsigned long)drv_data);
diff --git a/drivers/spi/spi-pxa2xx.h b/drivers/spi/spi-pxa2xx.h
index 94f7b0713281..8551c881f636 100644
--- a/drivers/spi/spi-pxa2xx.h
+++ b/drivers/spi/spi-pxa2xx.h
@@ -67,8 +67,7 @@ struct driver_data {
 
 	void __iomem *lpss_base;
 
-	/* GPIOs for chip selects */
-	struct gpio_desc **cs_gpiods;
+	int cs_count;
 };
 
 struct chip_data {
-- 
2.12.3

WARNING: multiple messages have this Message-ID (diff)
From: jan.kiszka@siemens.com (Jan Kiszka)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH RFC 2/2] spi: pxa2xx: Only claim CS GPIOs when the slave device is created
Date: Thu,  3 Aug 2017 13:40:33 +0200	[thread overview]
Message-ID: <5a691263ccf9d61134d6184861b37eaf37b13b77.1501760433.git.jan.kiszka@siemens.com> (raw)
In-Reply-To: <cover.1501760433.git.jan.kiszka@siemens.com>

From: Jan Kiszka <jan.kiszka@siemens.com>

Avoid hogging chip select GPIOs just because they are listed for the
master. They might be mulitplexed and, if no slave device is attached,
used for different purposes. Moreover, this strategy avoids having to
allocate a cs_gpiods structure.

Tested on the IOT2000 where the second SPI bus is connected to an
Arduino-compatible connector and multiplexed between SPI, GPIO and PWM
usage.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 drivers/spi/spi-pxa2xx.c | 68 +++++++++++++++++++++---------------------------
 drivers/spi/spi-pxa2xx.h |  3 +--
 2 files changed, 31 insertions(+), 40 deletions(-)

diff --git a/drivers/spi/spi-pxa2xx.c b/drivers/spi/spi-pxa2xx.c
index 7faba738110c..577a2f4ba3bf 100644
--- a/drivers/spi/spi-pxa2xx.c
+++ b/drivers/spi/spi-pxa2xx.c
@@ -1213,19 +1213,34 @@ static int setup_cs(struct spi_device *spi, struct chip_data *chip,
 		    struct pxa2xx_spi_chip *chip_info)
 {
 	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
+	struct device *pdev = &drv_data->pdev->dev;
 	struct gpio_desc *gpiod;
 	int err = 0;
 
 	if (chip == NULL)
 		return 0;
 
-	if (drv_data->cs_gpiods) {
-		gpiod = drv_data->cs_gpiods[spi->chip_select];
-		if (gpiod) {
-			chip->gpiod_cs = gpiod;
-			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
-			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
+	if (drv_data->cs_count > 0) {
+		/* setup() might be called multiple times. */
+		if (chip->gpiod_cs)
+			return 0;
+
+		if (spi->chip_select >= drv_data->cs_count)
+			return -EINVAL;
+
+		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
+
+		gpiod = gpiod_get_index(pdev, "cs", spi->chip_select,
+					chip->gpio_cs_inverted ?
+				        GPIOD_OUT_HIGH : GPIOD_OUT_LOW);
+		if (IS_ERR(gpiod)) {
+			/* Means use native chip select */
+			if (PTR_ERR(gpiod) == -ENOENT)
+				return 0;
+
+			return (int)PTR_ERR(gpiod);
 		}
+		chip->gpiod_cs = gpiod;
 
 		return 0;
 	}
@@ -1415,9 +1430,13 @@ static void cleanup(struct spi_device *spi)
 	if (!chip)
 		return;
 
-	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
-	    chip->gpiod_cs)
-		gpio_free(desc_to_gpio(chip->gpiod_cs));
+	if (drv_data->ssp_type != CE4100_SSP && chip->gpiod_cs) {
+		if (drv_data->cs_count > 0)
+			gpiod_put(chip->gpiod_cs);
+		else
+			gpio_free(desc_to_gpio(chip->gpiod_cs));
+		chip->gpiod_cs = NULL;
+	}
 
 	kfree(chip);
 }
@@ -1752,37 +1771,10 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 	master->num_chipselect = platform_info->num_chipselect;
 
 	count = gpiod_count(&pdev->dev, "cs");
-	if (count > 0) {
-		int i;
-
+	if (count > 0)
 		master->num_chipselect = max_t(int, count,
 			master->num_chipselect);
-
-		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
-			master->num_chipselect, sizeof(struct gpio_desc *),
-			GFP_KERNEL);
-		if (!drv_data->cs_gpiods) {
-			status = -ENOMEM;
-			goto out_error_clock_enabled;
-		}
-
-		for (i = 0; i < master->num_chipselect; i++) {
-			struct gpio_desc *gpiod;
-
-			gpiod = devm_gpiod_get_index(dev, "cs", i,
-						     GPIOD_OUT_HIGH);
-			if (IS_ERR(gpiod)) {
-				/* Means use native chip select */
-				if (PTR_ERR(gpiod) == -ENOENT)
-					continue;
-
-				status = (int)PTR_ERR(gpiod);
-				goto out_error_clock_enabled;
-			} else {
-				drv_data->cs_gpiods[i] = gpiod;
-			}
-		}
-	}
+	drv_data->cs_count = count;
 
 	tasklet_init(&drv_data->pump_transfers, pump_transfers,
 		     (unsigned long)drv_data);
diff --git a/drivers/spi/spi-pxa2xx.h b/drivers/spi/spi-pxa2xx.h
index 94f7b0713281..8551c881f636 100644
--- a/drivers/spi/spi-pxa2xx.h
+++ b/drivers/spi/spi-pxa2xx.h
@@ -67,8 +67,7 @@ struct driver_data {
 
 	void __iomem *lpss_base;
 
-	/* GPIOs for chip selects */
-	struct gpio_desc **cs_gpiods;
+	int cs_count;
 };
 
 struct chip_data {
-- 
2.12.3

  parent reply	other threads:[~2017-08-03 11:41 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-03 11:40 [PATCH RFC 0/2] spi: pxa2xx: gpiod cleanups and late CS GPIO allocation Jan Kiszka
2017-08-03 11:40 ` Jan Kiszka
2017-08-03 11:40 ` [PATCH RFC 1/2] spi: pxa2xx: Convert to GPIO descriptor API where possible Jan Kiszka
2017-08-03 11:40   ` Jan Kiszka
2017-08-03 11:40   ` Jan Kiszka
2017-08-04 10:10   ` Mika Westerberg
2017-08-04 10:10     ` Mika Westerberg
2017-08-04 10:10     ` Mika Westerberg
2017-08-04 10:18     ` Jan Kiszka
2017-08-04 10:18       ` Jan Kiszka
2017-08-04 10:18       ` Jan Kiszka
2017-08-04 10:24       ` Mika Westerberg
2017-08-04 10:24         ` Mika Westerberg
2017-08-04 10:24         ` Mika Westerberg
2017-08-04 11:43         ` Mark Brown
2017-08-04 11:43           ` Mark Brown
2017-08-04 11:43           ` Mark Brown
2017-08-04 11:44   ` Applied "spi: pxa2xx: Convert to GPIO descriptor API where possible" to the spi tree Mark Brown
2017-08-04 11:44     ` Mark Brown
2017-08-04 11:44     ` Mark Brown
2017-08-03 11:40 ` Jan Kiszka [this message]
2017-08-03 11:40   ` [PATCH RFC 2/2] spi: pxa2xx: Only claim CS GPIOs when the slave device is created Jan Kiszka
2017-08-03 11:40   ` Jan Kiszka
2017-08-04 10:29   ` Mika Westerberg
2017-08-04 10:29     ` Mika Westerberg
2017-08-04 10:29     ` Mika Westerberg
2017-08-04 11:43   ` Mark Brown
2017-08-04 11:43     ` Mark Brown
2017-08-04 11:43     ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5a691263ccf9d61134d6184861b37eaf37b13b77.1501760433.git.jan.kiszka@siemens.com \
    --to=jan.kiszka@siemens.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=broonie@kernel.org \
    --cc=daniel@zonque.org \
    --cc=haojian.zhuang@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=mika.westerberg@linux.intel.com \
    --cc=robert.jarzmik@free.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.