Skip to contentSkip to conversation

i2c: slave-eeprom: Make it possible to pre-load eeprom data

PATCH Superseded Björn Ardö 3 participants 8 messages Apr 24, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 i2c: slave-eeprom: Make it possible to pre-load eeprom data

If the slave eeprom has a "firmware-name" in devicetree, then pre-load the data in the eeprom with this file. Otherwise we init the eeprom with 0xFF.

---
 drivers/i2c/i2c-slave-eeprom.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/i2c/i2c-slave-eeprom.c +25 -0
index cb415b10642f..c846c96c25c9 100644
@@ -18,6 +18,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/firmware.h>
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/module.h>
@@ -120,6 +121,26 @@ static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kob
 	return count;
 }
 
+static int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client,
+					 unsigned int size)
+{
+	const struct firmware *fw;
+	const char *eeprom_data;
+	int error = device_property_read_string(&client->dev, "firmware-name", &eeprom_data);
+
+	if (!error) {
+		int ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
+						    eeprom->buffer, size);
+		if (ret)
+			return ret;
+		release_firmware(fw);
+	} else {
+		/* An empty eeprom typically has all bits set to 1 */
+		memset(eeprom->buffer, 0xFF, size);
+	}
+	return 0;
+}
+
 static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
 	struct eeprom_data *eeprom;
@@ -138,6 +159,10 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
 	spin_lock_init(&eeprom->buffer_lock);
 	i2c_set_clientdata(client, eeprom);
 
+	ret = i2c_slave_init_eeprom_data(eeprom, client, size);
+	if (ret)
+		return ret;
+
 	sysfs_bin_attr_init(&eeprom->bin);
 	eeprom->bin.attr.name = "slave-eeprom";
 	eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
Signature · 1 line
2.11.0
#1 lore raw
Quoted 5 lines · Björn Ardö

If the slave eeprom has a "firmware-name" in devicetree, then pre-load the data in the eeprom with this file. Otherwise we init the eeprom with 0xFF.

I like it a lot, thanks! Maybe we could add a SoB from Patrick for his 0xff-suggestion (but keeping you as the patch author).

Is this okay for everyone?

Quoted 57 lines
---
 drivers/i2c/i2c-slave-eeprom.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/i2c/i2c-slave-eeprom.c +25 -0
index cb415b10642f..c846c96c25c9 100644
@@ -18,6 +18,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/firmware.h>
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/module.h>
@@ -120,6 +121,26 @@ static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kob
 	return count;
 }
 
+static int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client,
+					 unsigned int size)
+{
+	const struct firmware *fw;
+	const char *eeprom_data;
+	int error = device_property_read_string(&client->dev, "firmware-name", &eeprom_data);
+
+	if (!error) {
+		int ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
+						    eeprom->buffer, size);
+		if (ret)
+			return ret;
+		release_firmware(fw);
+	} else {
+		/* An empty eeprom typically has all bits set to 1 */
+		memset(eeprom->buffer, 0xFF, size);
+	}
+	return 0;
+}
+
 static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
 	struct eeprom_data *eeprom;
@@ -138,6 +159,10 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
 	spin_lock_init(&eeprom->buffer_lock);
 	i2c_set_clientdata(client, eeprom);
 
+	ret = i2c_slave_init_eeprom_data(eeprom, client, size);
+	if (ret)
+		return ret;
+
 	sysfs_bin_attr_init(&eeprom->bin);
 	eeprom->bin.attr.name = "slave-eeprom";
 	eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
Signature · 1 line
2.11.0
author
#2 lore raw
Quoted 10 lines · Wolfram Sang
Quoted 5 lines · Björn Ardö

If the slave eeprom has a "firmware-name" in devicetree, then pre-load the data in the eeprom with this file. Otherwise we init the eeprom with 0xFF.

I like it a lot, thanks! Maybe we could add a SoB from Patrick for his 0xff-suggestion (but keeping you as the patch author).

Is this okay for everyone?

OK for me!

#3 lore raw
Quoted 7 lines · Bjorn Ardo
Quoted 5 lines · Wolfram Sang
Quoted 4 lines · Björn Ardö

If the slave eeprom has a "firmware-name" in devicetree, then pre-load the data in the eeprom with this file. Otherwise we init the eeprom with 0xFF.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

I like it a lot, thanks! Maybe we could add a SoB from Patrick for his 0xff-suggestion (but keeping you as the patch author).

Is this okay for everyone?

OK for me!

Fine by me, also.

Signature · 1 line
Patrick Williams
#4 lore raw
In reply to Björn Ardö

On second look, two questions:

drivers/i2c/i2c-slave-eeprom.c static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kob · line 135

Quoted 5 lines
drivers/i2c/i2c-slave-eeprom.c +5 -0
+	if (!error) {
+		int ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
+						    eeprom->buffer, size);
+		if (ret)
+			return ret;

Aren't we leaking 'fw' here?

Also, do we need 'error' and 'ret'? Can't we reuse one of them?

Quoted 1 line
drivers/i2c/i2c-slave-eeprom.c +1 -0
+		release_firmware(fw);
author
#5 lore raw
Quoted 8 lines · Wolfram Sang

On second look, two questions:

drivers/i2c/i2c-slave-eeprom.c static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kob · line 135

Quoted 5 lines
drivers/i2c/i2c-slave-eeprom.c +5 -0
+	if (!error) {
+		int ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
+						    eeprom->buffer, size);
+		if (ret)
+			return ret;

Aren't we leaking 'fw' here?

As I can see in drivers/base/firmware_loader/main.c in function _request_firmware, then the fw will be released internally if it returns an error value.

Quoted 1 line

Also, do we need 'error' and 'ret'? Can't we reuse one of them?

Yes, I can fix that.

/BA

#6 lore raw
Quoted 4 lines

As I can see in drivers/base/firmware_loader/main.c in function _request_firmware, then the fw will be released internally if it returns an error value.

Ouch, of course!

Quoted 3 lines
Quoted 1 line

Also, do we need 'error' and 'ret'? Can't we reuse one of them?

Yes, I can fix that.

Great, thanks!

author
#7 lore raw

You got a new patch (ignor the first one, I forgot to update the version number).

Quoted 9 lines · Wolfram Sang
Quoted 3 lines

As I can see in drivers/base/firmware_loader/main.c in function _request_firmware, then the fw will be released internally if it returns an error value.

Ouch, of course!

Quoted 2 lines
Quoted 1 line

Also, do we need 'error' and 'ret'? Can't we reuse one of them?

Yes, I can fix that.

Great, thanks!