Skip to contentSkip to conversation

Add at24 based EEPROMs to the eeprom_dev hardware class

PATCH In review Curt Brune 2 participants 2 messages Jan 23, 2014 – Jan 26, 2014

Subsystems (2)
lore
Share & tools
lore

Curt Brune

author
#0 lore raw

PATCH v2 2/2 Add at24 based EEPROMs to the eeprom_dev hardware class

During device instantiation have the at24 driver add the new device to the eeprom_dev hardware class. The functionality is enabled by CONFIG_EEPROM_CLASS.

---
 drivers/misc/eeprom/at24.c |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/misc/eeprom/at24.c +20 -0
index d87f77f..07782ea 100644
@@ -22,10 +22,14 @@
 #include <linux/jiffies.h>
 #include <linux/of.h>
 #include <linux/i2c.h>
 #include <linux/platform_data/at24.h>
 
+#ifdef CONFIG_EEPROM_CLASS
+#include <linux/eeprom_class.h>
+#endif
+
 /*
  * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
  * Differences between different vendor product lines (like Atmel AT24C or
  * MicroChip 24LC, etc) won't much matter for typical read/write access.
  * There are also I2C RAM chips, likewise interchangeable. One example
@@ -66,10 +70,13 @@ struct at24_data {
 
 	u8 *writebuf;
 	unsigned write_max;
 	unsigned num_addresses;
 
+#ifdef CONFIG_EEPROM_CLASS
+	struct device *eeprom_dev;
+#endif
 	/*
 	 * Some chips tie up multiple I2C addresses; dummy devices reserve
 	 * them for us, and we'll use them with SMBus calls.
 	 */
 	struct i2c_client *client[];
@@ -619,10 +626,19 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 
 	err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
 	if (err)
 		goto err_clients;
 
+#ifdef CONFIG_EEPROM_CLASS
+	at24->eeprom_dev = eeprom_device_register(&client->dev);
+	if (IS_ERR(at24->eeprom_dev)) {
+		dev_err(&client->dev, "error registering eeprom device.\n");
+		err = PTR_ERR(at24->eeprom_dev);
+		goto err_clients;
+	}
+#endif
+
 	i2c_set_clientdata(client, at24);
 
 	dev_info(&client->dev, "%zu byte %s EEPROM, %s, %u bytes/write\n",
 		at24->bin.size, client->name,
 		writable ? "writable" : "read-only", at24->write_max);
@@ -656,10 +672,14 @@ static int at24_remove(struct i2c_client *client)
 	sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);
 
 	for (i = 1; i < at24->num_addresses; i++)
 		i2c_unregister_device(at24->client[i]);
 
+#ifdef CONFIG_EEPROM_CLASS
+	eeprom_device_unregister(at24->eeprom_dev);
+#endif
+
 	return 0;
 }
 
 /*-------------------------------------------------------------------------*/
 
Signature · 1 line
1.7.10.4
#1 lore raw

Hi Curt,

Quoted 14 lines · Curt Brune

During device instantiation have the at24 driver add the new device to the eeprom_dev hardware class. The functionality is enabled by CONFIG_EEPROM_CLASS.

---
 drivers/misc/eeprom/at24.c |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/misc/eeprom/at24.c +0 -0
index d87f77f..07782ea 100644

drivers/misc/eeprom/at24.c line 29

Quoted 8 lines
drivers/misc/eeprom/at24.c +3 -0
@@ -22,10 +22,14 @@
 #include <linux/jiffies.h>
 #include <linux/of.h>
 #include <linux/i2c.h>
 #include <linux/platform_data/at24.h>
 
+#ifdef CONFIG_EEPROM_CLASS
+#include <linux/eeprom_class.h>
+#endif

I would recommend moving the "ifdeffery" into the eeprom_class.h and leave the users (in this case the at24.c) clean.

Quoted 7 lines
drivers/misc/eeprom/at24.c +1 -0
+
 /*
  * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
  * Differences between different vendor product lines (like Atmel AT24C or
  * MicroChip 24LC, etc) won't much matter for typical read/write access.
  * There are also I2C RAM chips, likewise interchangeable. One example

drivers/misc/eeprom/at24.c struct at24_data { · line 77

Quoted 8 lines
drivers/misc/eeprom/at24.c +3 -0
@@ -66,10 +70,13 @@ struct at24_data {
 
 	u8 *writebuf;
 	unsigned write_max;
 	unsigned num_addresses;
 
+#ifdef CONFIG_EEPROM_CLASS
+	struct device *eeprom_dev;
+#endif

Same here, this is just a pointer, so I don't think anyone has a problem having it even if eeprom class is not selected.

Quoted 9 lines
drivers/misc/eeprom/at24.c +0 -0
 	/*
 	 * Some chips tie up multiple I2C addresses; dummy devices reserve
 	 * them for us, and we'll use them with SMBus calls.
 	 */
 	struct i2c_client *client[];
@@ -619,10 +626,19 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 
 	err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
 	if (err)

drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 638

Quoted 10 lines
drivers/misc/eeprom/at24.c +8 -0
 		goto err_clients;
 
+#ifdef CONFIG_EEPROM_CLASS
+	at24->eeprom_dev = eeprom_device_register(&client->dev);
+	if (IS_ERR(at24->eeprom_dev)) {
+		dev_err(&client->dev, "error registering eeprom device.\n");
+		err = PTR_ERR(at24->eeprom_dev);
+		goto err_clients;
+	}
+#endif

stub the eeprom_device_register() out in eeprom_class.h for !CONFIG_EEPROM_CLASS case

Quoted 7 lines
drivers/misc/eeprom/at24.c +1 -0
+
 	i2c_set_clientdata(client, at24);
 
 	dev_info(&client->dev, "%zu byte %s EEPROM, %s, %u bytes/write\n",
 		at24->bin.size, client->name,
 		writable ? "writable" : "read-only", at24->write_max);

drivers/misc/eeprom/at24.c static int at24_remove(struct i2c_client *client) · line 679

Quoted 8 lines
drivers/misc/eeprom/at24.c +3 -0
@@ -656,10 +672,14 @@ static int at24_remove(struct i2c_client *client)
 	sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);
 
 	for (i = 1; i < at24->num_addresses; i++)
 		i2c_unregister_device(at24->client[i]);
 
+#ifdef CONFIG_EEPROM_CLASS
+	eeprom_device_unregister(at24->eeprom_dev);
+#endif

same here.

Quoted 7 lines
drivers/misc/eeprom/at24.c +1 -0
+
 	return 0;
 }
 
 /*-------------------------------------------------------------------------*/
 
Signature · 2 lines
Regards,
Igor.