Skip to contentSkip to conversation

spi core: Provide means to instantiate devices through sysfs

PATCH RESEND In review Guenter Roeck 2 participants 3 messages Sep 17, 2012 – Sep 22, 2012 SPI

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 spi core: Provide means to instantiate devices through sysfs

The I2C core provides a means to instantiate devices from userspace using sysfs attributes. Provide the same mechanism for SPI devices.

---
This helped me tremendously for testing new SPI master and client drivers. Maybe it is useful for others as well.

ping: Any interest ?

 Documentation/spi/spi-summary |   48 +++++++++++++
 drivers/spi/spi.c             |  159 +++++++++++++++++++++++++++++++++++++++++
 include/linux/spi/spi.h       |    3 +
 3 files changed, 210 insertions(+)
Documentation/spi/spi-summary +48 -0
index 7312ec1..5b59992 100644
@@ -331,6 +331,54 @@ configurations will also be dynamic.  Fortunately, such devices all support
 basic device identification probes, so they should hotplug normally.
 
 
+DECLARE SLAVE DEVICES FROM USER-SPACE
+
+In general, the kernel should know which SPI devices are connected and
+what addresses they live at. However, in certain cases, it does not, so a
+sysfs interface was added to let the user provide the information. This
+interface is made of 2 attribute files which are created in every SPI master
+directory: new_device and delete_device. Both files are write only and you
+must write the right parameters to them in order to properly instantiate,
+respectively delete, a SPI device.
+
+File new_device takes several parameters:
+
+Primary parameters are the name of the SPI device (a string), the SPI chip
+select (a number, expressed in decimal or hexadecimal), the SPI bus speed
+(a number, expressed in decimal or hexadecimal), and the SPI mode (a number,
+expressed in decimal or hexadecimal). SPI device name, chip select, and speed
+are mandatory. The mode parameter is optional and will be initialized with 0 if
+not provided.
+
+For at25 type EEPROMs, additional parameters must be provided in < >. Those are
+the EEPROM name (a string), the EEPROM capacity in bytes (a number, expressed in
+decimal or hexadecimal), the EEPROM write page size (a number, expressed in
+decimal or hexadecimal), and the EEPROM flags (a number, expressed in decimal or
+hexadecimal).
+
+File delete_device takes a single parameter: the chip select of the SPI
+device. As no two devices can live at the same chip select on a given SPI
+master, the chip select is sufficient to uniquely identify the device to be
+deleted.
+
+Examples:
+	cd /sys/class/spi_master/spi0
+		Instantiate devices on SPI master 0
+
+	echo "lm70 0 400000" > new_device
+		Instantiate LM70 on CS0
+
+	echo "at25 1 1000000 0 < at25160b 2048 32 0x02 >" > new_device
+		Instantiate at25 driver with AT25160B on CS1
+
+	echo "sst25vf080b 2 1000000 3" > new_device
+		Instantiate SST25VF080B on CS2, using SPI mode 3
+
+While this interface should only be used when in-kernel device declaration
+can't be done, it can be helpful if you are developing a driver on a test board,
+where you soldered the SPI device yourself, or in hot-plug situations.
+
+
 How do I write an "SPI Protocol Driver"?
 ----------------------------------------
 Most SPI drivers are currently kernel drivers, but there's also support
drivers/spi/spi.c +159 -0

188 lines not on this page · raw message

include/linux/spi/spi.h +3 -0
index fa702ae..dac46bc 100644
@@ -91,6 +91,8 @@ struct spi_device {
 	void			*controller_data;
 	char			modalias[SPI_NAME_SIZE];
 
+	struct list_head	detected;
+
 	/*
 	 * likely need more hooks for more protocol options affecting how
 	 * the controller talks to each chip, like:
@@ -273,6 +275,7 @@ struct spi_master {
 	struct device	dev;
 
 	struct list_head list;
+	struct list_head userspace_devices;
 
 	/* other than negative (== assign one dynamically), bus_num is fully
 	 * board-specific.  usually that simplifies to being SOC-specific.
Signature · 1 line
1.7.9.7
#1 lore raw
Quoted 2 lines · Guenter Roeck

The I2C core provides a means to instantiate devices from userspace using sysfs attributes. Provide the same mechanism for SPI devices.

So, unlike I2C this is only going to work for a subset of controllers - anything that relies on GPIOs for chip select will need more data to add the chip selects for example. This means I'm having a hard time getting enthused about the idea, it seems like it might be a support burden.

------------------------------------------------------------------------------
How fast is your code?
3 out of 4 devs don\\\'t know how their code performs in production. Find out how slow your code is with AppDynamics Lite.
http://ad.doubleclick.net/clk;262219672;13503038;z?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html

#2 lore raw
Quoted 9 lines · Mark Brown
Quoted 2 lines · Guenter Roeck

The I2C core provides a means to instantiate devices from userspace using sysfs attributes. Provide the same mechanism for SPI devices.

So, unlike I2C this is only going to work for a subset of controllers - anything that relies on GPIOs for chip select will need more data to add the chip selects for example. This means I'm having a hard time getting enthused about the idea, it seems like it might be a support burden.

Yes, obviously that model can not be used if SPI chip select is handled outside the SPI master driver.

Anyway, no problem. That the code is useful for me doesn't mean it has to be useful for others. I'll make it available as branch on my linux tree on github.

Thanks,
Guenter