[i2c] [PATCH] Alternate names for i2c devices

Jon Smirl jonsmirl at gmail.com
Tue Nov 20 20:54:47 CET 2007


PowerPC needs a mechanism to support alternative names for i2c
devices. This is a requirement for device tree support. Device trees
are standardized by a body outside of Linux and the naming convention
for i2c device is not the same as linux device drivers.

I've added this field in a way that is compatible with existing
drivers and requires no changes in them. The 'compatible' naming
scheme could be optimized more but it would require changing existing
drivers.

Extend i2c-core to support lists of device tree compatible names when
matching drivers

From: Jon Smirl <jonsmirl at gmail.com>

Patch creates a new field, compatible, in the i2c_driver structure.
i2c chip device drivers can use this field to add 'compatible' names.
For example in Open Firmware format: "ricoh,rs5c372a".
---

 drivers/i2c/i2c-core.c |   16 +++++++++++++++-
 include/linux/i2c.h    |   13 ++++++++++---
 2 files changed, 25 insertions(+), 4 deletions(-)


diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index b5e13e4..ac2ae0c 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -51,6 +51,7 @@ static int i2c_device_match(struct device *dev,
struct device_driver *drv)
 {
 	struct i2c_client	*client = to_i2c_client(dev);
 	struct i2c_driver	*driver = to_i2c_driver(drv);
+	char const **compatible;

 	/* make legacy i2c drivers bypass driver model probing entirely;
 	 * such drivers scan each i2c adapter/bus themselves.
@@ -61,7 +62,20 @@ static int i2c_device_match(struct device *dev,
struct device_driver *drv)
 	/* new style drivers use the same kind of driver matching policy
 	 * as platform devices or SPI:  compare device and driver IDs.
 	 */
-	return strcmp(client->driver_name, drv->name) == 0;
+	if (strcmp(client->driver_name, drv->name) == 0)
+		return true;
+	
+	/* Match against array of compatible device tree names. When a match
+	 * is found change the reference to point at the copy inside the
+	 * chip driver allowing the caller's string to be freed.
+ 	 */
+	compatible = driver->compatible;
+	while (compatible && *compatible) {
+		if (strnicmp(client->driver_name, *compatible, sizeof
client->driver_name) == 0)
+			return true;
+		compatible++;
+	}
+	return 0;	
 }

 #ifdef	CONFIG_HOTPLUG
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index a100c9f..7f09834 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -105,6 +105,13 @@ extern s32 i2c_smbus_write_i2c_block_data(struct
i2c_client * client,
 struct i2c_driver {
 	int id;
 	unsigned int class;
+	
+	/* Alias names for the driver. Used to support device trees on
+	 * the PowerPC architecture. Device tree names take the form of
+	 * vendor,chip. For example "epson,rtc8564". Alias is a list of
+	 * strings terminated by a zero entry.
+	 */
+	char const **compatible;	

 	/* Notifies the driver that a new bus has appeared. This routine
 	 * can be used by the driver to test if the bus meets its conditions
@@ -144,7 +151,7 @@ struct i2c_driver {
 };
 #define to_i2c_driver(d) container_of(d, struct i2c_driver, driver)

-#define I2C_NAME_SIZE	20
+#define I2C_NAME_SIZE	40

 /**
  * struct i2c_client - represent an I2C slave device
@@ -179,7 +186,7 @@ struct i2c_client {
 					/* to the client		*/
 	struct device dev;		/* the device structure		*/
 	int irq;			/* irq issued by device (or -1) */
-	char driver_name[KOBJ_NAME_LEN];
+	char driver_name[I2C_NAME_SIZE];
 	struct list_head list;
 	struct completion released;
 };
@@ -223,7 +230,7 @@ static inline void i2c_set_clientdata (struct
i2c_client *dev, void *data)
  * with the adapter already known.
  */
 struct i2c_board_info {
-	char		driver_name[KOBJ_NAME_LEN];
+	char		driver_name[I2C_NAME_SIZE];
 	char		type[I2C_NAME_SIZE];
 	unsigned short	flags;
 	unsigned short	addr;

-- 
Jon Smirl
jonsmirl at gmail.com



More information about the i2c mailing list