public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] spi: Force the registration of the spidev devices
@ 2014-04-28 17:22 Maxime Ripard
  2014-04-29 18:37 ` Mark Brown
  0 siblings, 1 reply; 45+ messages in thread
From: Maxime Ripard @ 2014-04-28 17:22 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Alexandre Belloni, Maxime Ripard

spidev device registration has always been a controversial subject since the
move to DT.

Obviously, a spidev node has nothing to do in the DT, and the position so far
has been to add the compatible of the devices to drive through spidev to the
list of the compatibles spidev can handle.

While this is nicer than the DT solution because of its accurate hardware
representation, it's still not perfect because you might not have access to the
DT, or you might be driving a completely generic device (such as a
microcontroller) that might be used for something else in a different
context/board.

Solve this by registering automatically spidev devices for all the unused chip
selects when a master registers itself against the spi core.

This also adds an i2cdev-like feeling, where you get all the spidev devices all
the time, without any modification.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 drivers/spi/spi.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 4eb9bf02996c..e832a5e20e8d 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1402,6 +1402,52 @@ static void acpi_register_spi_devices(struct spi_master *master)
 static inline void acpi_register_spi_devices(struct spi_master *master) {}
 #endif /* CONFIG_ACPI */
 
+#ifdef CONFIG_SPI_SPIDEV
+static void spidev_register_devices(struct spi_master *master)
+{
+	struct spi_device *spi;
+	int i, status;
+
+	for (i = 0; i < master->num_chipselect; i++) {
+		spi = spi_alloc_device(master);
+		if (!spi) {
+			dev_err(&master->dev, "Couldn't allocate spidev device\n");
+			continue;
+		}
+
+		spi->chip_select = i;
+		strlcpy(spi->modalias, "spidev", sizeof(spi->modalias));
+
+		/*
+		 * This is far from perfect since an addition might be
+		 * done between here and the call to spi_add_device,
+		 * but we can't hold the lock and call spi_add_device
+		 * either, as it would trigger a deadlock.
+		 *
+		 * If such a race occurs, spi_add_device will still
+		 * catch it though, as it also checks for devices
+		 * being registered several times on the same chip
+		 * select.
+		*/
+		status = bus_for_each_dev(&spi_bus_type, NULL, spi,
+					  spi_dev_check);
+		if (status) {
+			dev_dbg(&master->dev, "Chipselect already in use.. Skipping.");
+			spi_dev_put(spi);
+			continue;
+		}
+
+		if (spi_add_device(spi)) {
+			dev_err(&master->dev, "Couldn't add spidev device\n");
+			spi_dev_put(spi);
+		}
+	}
+
+}
+#else
+static inline void spidev_register_devices(struct spi_master *master) {}
+#endif /* CONFIG_SPI_SPIDEV */
+
 static void spi_master_release(struct device *dev)
 {
 	struct spi_master *master;
@@ -1591,6 +1637,7 @@ int spi_register_master(struct spi_master *master)
 	/* Register devices from the device tree and ACPI */
 	of_register_spi_devices(master);
 	acpi_register_spi_devices(master);
+	spidev_register_devices(master);
 done:
 	return status;
 }
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread
* [PATCH] spi: Force the registration of the spidev devices
@ 2015-05-12 20:33 Maxime Ripard
  2015-05-13 11:26 ` Mark Brown
  0 siblings, 1 reply; 45+ messages in thread
From: Maxime Ripard @ 2015-05-12 20:33 UTC (permalink / raw)
  To: Mark Brown
  Cc: Greg Kroah-Hartman, linux-kernel, Hans de Goede, linux-spi,
	Martin Sperl, Michal Suchanek, Maxime Ripard

spidev device registration has always been a controversial subject since the
move to DT.

Obviously, a spidev node has nothing to do in the DT, and the position so far
has been to add the compatible of the devices to drive through spidev to the
list of the compatibles spidev can handle.

While this is nicer than the DT solution because of its accurate hardware
representation, it's still not perfect because you might not have access to the
DT, or you might be driving a completely generic device (such as a
microcontroller) that might be used for something else in a different
context/board.

Solve this by registering automatically spidev devices for all the unused chip
selects when a master registers itself against the spi core.

This also adds an i2cdev-like feeling, where you get all the spidev devices all
the time, without any modification.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 drivers/spi/spi.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index d5d7d2235163..e6ca46e1e0fc 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1384,6 +1384,52 @@ static void acpi_register_spi_devices(struct spi_master *master)
 static inline void acpi_register_spi_devices(struct spi_master *master) {}
 #endif /* CONFIG_ACPI */
 
+#ifdef CONFIG_SPI_SPIDEV
+static void spidev_register_devices(struct spi_master *master)
+{
+	struct spi_device *spi;
+	int i, status;
+
+	for (i = 0; i < master->num_chipselect; i++) {
+		spi = spi_alloc_device(master);
+		if (!spi) {
+			dev_err(&master->dev, "Couldn't allocate spidev device\n");
+			continue;
+		}
+
+		spi->chip_select = i;
+		strlcpy(spi->modalias, "spidev", sizeof(spi->modalias));
+
+		/*
+		 * This is far from perfect since an addition might be
+		 * done between here and the call to spi_add_device,
+		 * but we can't hold the lock and call spi_add_device
+		 * either, as it would trigger a deadlock.
+		 *
+		 * If such a race occurs, spi_add_device will still
+		 * catch it though, as it also checks for devices
+		 * being registered several times on the same chip
+		 * select.
+		*/
+		status = bus_for_each_dev(&spi_bus_type, NULL, spi,
+					  spi_dev_check);
+		if (status) {
+			dev_dbg(&master->dev, "Chipselect already in use.. Skipping.");
+			spi_dev_put(spi);
+			continue;
+		}
+
+		if (spi_add_device(spi)) {
+			dev_err(&master->dev, "Couldn't add spidev device\n");
+			spi_dev_put(spi);
+		}
+	}
+
+}
+#else
+static inline void spidev_register_devices(struct spi_master *master) {}
+#endif /* CONFIG_SPI_SPIDEV */
+
 static void spi_master_release(struct device *dev)
 {
 	struct spi_master *master;
@@ -1575,6 +1621,7 @@ int spi_register_master(struct spi_master *master)
 	/* Register devices from the device tree and ACPI */
 	of_register_spi_devices(master);
 	acpi_register_spi_devices(master);
+	spidev_register_devices(master);
 done:
 	return status;
 }
-- 
2.4.0


^ permalink raw reply related	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2015-07-15  6:27 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-28 17:22 [PATCH] spi: Force the registration of the spidev devices Maxime Ripard
2014-04-29 18:37 ` Mark Brown
2014-04-30 18:06   ` Maxime Ripard
2014-05-01  1:18     ` Mark Brown
2014-05-01 22:36       ` Maxime Ripard
2014-05-01 23:28         ` Geert Uytterhoeven
2014-05-02 16:55           ` Mark Brown
2014-05-05  4:17           ` Maxime Ripard
2014-05-05  7:10             ` Geert Uytterhoeven
2014-05-05 13:57               ` Alexandre Belloni
2014-05-05 14:22                 ` Geert Uytterhoeven
2014-05-05 19:16               ` Mark Brown
2014-05-02 17:40         ` Mark Brown
2014-05-05  4:21           ` Maxime Ripard
2014-05-05 19:17             ` Mark Brown
2014-05-08  2:22               ` Maxime Ripard
  -- strict thread matches above, loose matches on Subject: below --
2015-05-12 20:33 Maxime Ripard
2015-05-13 11:26 ` Mark Brown
2015-05-13 12:35   ` Michal Suchanek
2015-05-13 12:51   ` Maxime Ripard
2015-05-13 14:36     ` Mark Brown
2015-05-13 15:31       ` Michal Suchanek
2015-05-13 17:43         ` Mark Brown
2015-05-13 19:09       ` Maxime Ripard
2015-05-13 19:10     ` Geert Uytterhoeven
2015-05-13 19:41       ` Maxime Ripard
2015-05-13 15:37   ` Greg Kroah-Hartman
2015-05-13 15:52     ` Michal Suchanek
2015-05-13 17:13     ` Mark Brown
2015-05-13 17:20       ` Greg Kroah-Hartman
2015-05-13 17:39         ` Mark Brown
2015-05-13 18:16           ` Greg Kroah-Hartman
2015-05-13 18:32             ` Mark Brown
2015-05-13 18:36               ` Greg Kroah-Hartman
2015-05-13 18:51                 ` Mark Brown
2015-05-13 19:17                   ` Maxime Ripard
2015-05-13 17:50     ` Maxime Ripard
2015-05-13 18:12       ` Mark Brown
2015-05-13 18:17       ` Greg Kroah-Hartman
2015-05-13 19:23         ` Geert Uytterhoeven
2015-05-13 19:26         ` Maxime Ripard
2015-05-13 22:33           ` Greg Kroah-Hartman
2015-05-14 14:34             ` Mark Brown
2015-05-15  8:09             ` Maxime Ripard
2015-07-15  6:27             ` Lucas De Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox