Skip to contentSkip to conversation

i2c: core: Add support for best effort block read emulation

RFC PATCH Superseded Irina Tirdea 2 participants 3 messages Mar 25, 2015 – Apr 17, 2015

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 i2c: core: Add support for best effort block read emulation

There are devices that need to handle block transactions regardless of the capabilities exported by the adapter. For performance reasons, they need to use i2c read blocks if available, otherwise emulate the block transaction with word or byte transactions.

Add support for a helper function that would read a data block using the best transfer available: I2C_FUNC_SMBUS_READ_I2C_BLOCK, I2C_FUNC_SMBUS_READ_WORD_DATA or I2C_FUNC_SMBUS_READ_BYTE_DATA.

---

Hi,

This is a new API proposal to handle i2c block emulation in the core instead of the driver code.

This is needed for a set of iio sensor changes ([1], [2], [3]) that would otherwise duplicate this code. There are also some usages of this functionality in the kernel (e.g. eeprom driver at24).

Please let me know what you think.

Thanks,
Irina

[1] https://lkml.org/lkml/2015/2/16/408
[2] https://lkml.org/lkml/2015/2/16/413
[3] https://lkml.org/lkml/2015/2/16/402

 drivers/i2c/i2c-core.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c.h    |  3 +++
 2 files changed, 65 insertions(+)
drivers/i2c/i2c-core.c +62 -0
index fe80f85..2579f7d 100644
@@ -2907,6 +2907,68 @@ trace:
 }
 EXPORT_SYMBOL(i2c_smbus_xfer);
 
+/**
+ * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
+ * @client: Handle to slave device
+ * @command: Byte interpreted by slave
+ * @length: Size of data block; SMBus allows at most 32 bytes
+ * @values: Byte array into which data will be read; big enough to hold
+ *	the data returned by the slave.  SMBus allows at most 32 bytes.
+ *
+ * This executes the SMBus "block read" protocol if supported by the adapter.
+ * If block read is not supported, it emulates it using either word or byte
+ * read protocols depending on availability.
+ */
+s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
+					      u8 command, u8 length, u8 *values)
+{
+	u8 i;
+	int status;
+
+	if (length > I2C_SMBUS_BLOCK_MAX)
+		length = I2C_SMBUS_BLOCK_MAX;
+
+	if (i2c_check_functionality(client->adapter,
+				    I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
+		return i2c_smbus_read_i2c_block_data(client, command,
+						     length, values);
+	} else if (i2c_check_functionality(client->adapter,
+					   I2C_FUNC_SMBUS_READ_WORD_DATA |
+					   I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
+		for (i = 0; (i + 2) <= length; i += 2) {
+			status = i2c_smbus_read_word_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status & 0xff;
+			values[i+1] = status >> 8;
+		}
+		if (i < length) {
+			status = i2c_smbus_read_byte_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status;
+			i++;
+		}
+		return i;
+	} else if (i2c_check_functionality(client->adapter,
+					   I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
+		for (i = 0; i < length; i++) {
+			status = i2c_smbus_read_byte_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status;
+		}
+		return i;
+	}
+
+	dev_err(&client->adapter->dev, "Unsupported transactions: %d,%d,%d\n",
+		I2C_SMBUS_I2C_BLOCK_DATA, I2C_SMBUS_WORD_DATA,
+		I2C_SMBUS_BYTE_DATA);
+
+	return -EOPNOTSUPP;
+}
+EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
+
 #if IS_ENABLED(CONFIG_I2C_SLAVE)
 int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
 {
include/linux/i2c.h +3 -0
index 243d1a1..f3ede76 100644
@@ -121,6 +121,9 @@ extern s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client,
 extern s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client,
 					  u8 command, u8 length,
 					  const u8 *values);
+extern s32
+i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
+					  u8 command, u8 length, u8 *values);
 #endif /* I2C */
 
 /**
Signature · 1 line
1.9.1
#1 lore raw
Quoted 15 lines · Irina Tirdea

There are devices that need to handle block transactions regardless of the capabilities exported by the adapter. For performance reasons, they need to use i2c read blocks if available, otherwise emulate the block transaction with word or byte transactions.

Add support for a helper function that would read a data block using the best transfer available: I2C_FUNC_SMBUS_READ_I2C_BLOCK, I2C_FUNC_SMBUS_READ_WORD_DATA or I2C_FUNC_SMBUS_READ_BYTE_DATA.

---

Hi,

Quoted 8 lines

This is a new API proposal to handle i2c block emulation in the core instead of the driver code.

This is needed for a set of iio sensor changes ([1], [2], [3]) that would otherwise duplicate this code. There are also some usages of this functionality in the kernel (e.g. eeprom driver at24).

Please let me know what you think.

I am open to add something like this. One change I'd like to request is to introduce a user, e.g. convert at24.

Quoted 17 lines

Thanks,
Irina

[1] https://lkml.org/lkml/2015/2/16/408
[2] https://lkml.org/lkml/2015/2/16/413
[3] https://lkml.org/lkml/2015/2/16/402

 drivers/i2c/i2c-core.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c.h    |  3 +++
 2 files changed, 65 insertions(+)
drivers/i2c/i2c-core.c +0 -0
index fe80f85..2579f7d 100644

drivers/i2c/i2c-core.c trace: · line 2916

Quoted 10 lines
drivers/i2c/i2c-core.c +7 -0
@@ -2907,6 +2907,68 @@ trace:
 }
 EXPORT_SYMBOL(i2c_smbus_xfer);
 
+/**
+ * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
+ * @client: Handle to slave device
+ * @command: Byte interpreted by slave
+ * @length: Size of data block; SMBus allows at most 32 bytes
+ * @values: Byte array into which data will be read; big enough to hold
+ *	the data returned by the slave.  SMBus allows at most 32 bytes.

Sidenote: SMBus3 allows 255 byte, but we don't support that (yet), so this is okay for now.

drivers/i2c/i2c-core.c trace: · line 2920

Quoted 4 lines
drivers/i2c/i2c-core.c +4 -0
+ *
+ * This executes the SMBus "block read" protocol if supported by the adapter.
+ * If block read is not supported, it emulates it using either word or byte
+ * read protocols depending on availability.

Here I'd like to see a warning that people should double-check if their I2C slave does support that. Sometimes one can't exchange a block transfer with a byte transfer.

Quoted 7 lines
drivers/i2c/i2c-core.c +7 -0
+ */
+s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
+					      u8 command, u8 length, u8 *values)
+{
+	u8 i;
+	int status;
+

drivers/i2c/i2c-core.c trace: · line 2937

Quoted 10 lines
drivers/i2c/i2c-core.c +10 -0
+	if (length > I2C_SMBUS_BLOCK_MAX)
+		length = I2C_SMBUS_BLOCK_MAX;
+
+	if (i2c_check_functionality(client->adapter,
+				    I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
+		return i2c_smbus_read_i2c_block_data(client, command,
+						     length, values);
+	} else if (i2c_check_functionality(client->adapter,
+					   I2C_FUNC_SMBUS_READ_WORD_DATA |
+					   I2C_FUNC_SMBUS_READ_BYTE_DATA)) {

What about skipping the need for READ_BYTE_DATA and dump the byte which was maybe read too much?

Quoted 23 lines
drivers/i2c/i2c-core.c +23 -0
+		for (i = 0; (i + 2) <= length; i += 2) {
+			status = i2c_smbus_read_word_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status & 0xff;
+			values[i+1] = status >> 8;
+		}
+		if (i < length) {
+			status = i2c_smbus_read_byte_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status;
+			i++;
+		}
+		return i;
+	} else if (i2c_check_functionality(client->adapter,
+					   I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
+		for (i = 0; i < length; i++) {
+			status = i2c_smbus_read_byte_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status;
+		}

drivers/i2c/i2c-core.c trace: · line 2970

Quoted 10 lines
drivers/i2c/i2c-core.c +10 -0
+		return i;
+	}
+
+	dev_err(&client->adapter->dev, "Unsupported transactions: %d,%d,%d\n",
+		I2C_SMBUS_I2C_BLOCK_DATA, I2C_SMBUS_WORD_DATA,
+		I2C_SMBUS_BYTE_DATA);
+
+	return -EOPNOTSUPP;
+}
+EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);

Thanks,

Wolfram

#2 lore raw
Quoted 28 lines

-----Original Message-----
From: Wolfram Sang [mailto:wsa-z923LK4zBo2bacvFa/[email protected]]
Sent: 15 April, 2015 18:55
To: Tirdea, Irina
Cc: [email protected]; [email protected]
Subject: Re: [RFC PATCH 1/1] i2c: core: Add support for best effort block read emulation

Quoted 18 lines · Irina Tirdea

There are devices that need to handle block transactions regardless of the capabilities exported by the adapter. For performance reasons, they need to use i2c read blocks if available, otherwise emulate the block transaction with word or byte transactions.

Add support for a helper function that would read a data block using the best transfer available: I2C_FUNC_SMBUS_READ_I2C_BLOCK, I2C_FUNC_SMBUS_READ_WORD_DATA or I2C_FUNC_SMBUS_READ_BYTE_DATA.

---

Hi,

This is a new API proposal to handle i2c block emulation in the core instead of the driver code.

Quoted 9 lines
Quoted 5 lines

This is needed for a set of iio sensor changes ([1], [2], [3]) that would otherwise duplicate this code. There are also some usages of this functionality in the kernel (e.g. eeprom driver at24).

Please let me know what you think.

I am open to add something like this. One change I'd like to request is to introduce a user, e.g. convert at24.

Thanks for the review!

Sure, I'll send a new version with the fixes you suggested and include a user as well.

Quoted 31 lines
Quoted 17 lines

Thanks,
Irina

[1] https://lkml.org/lkml/2015/2/16/408
[2] https://lkml.org/lkml/2015/2/16/413
[3] https://lkml.org/lkml/2015/2/16/402

 drivers/i2c/i2c-core.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c.h    |  3 +++
 2 files changed, 65 insertions(+)
drivers/i2c/i2c-core.c +0 -0
index fe80f85..2579f7d 100644

drivers/i2c/i2c-core.c trace: · line 2916

Quoted 10 lines
drivers/i2c/i2c-core.c +7 -0
@@ -2907,6 +2907,68 @@ trace:
 }
 EXPORT_SYMBOL(i2c_smbus_xfer);
 
+/**
+ * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
+ * @client: Handle to slave device
+ * @command: Byte interpreted by slave
+ * @length: Size of data block; SMBus allows at most 32 bytes
+ * @values: Byte array into which data will be read; big enough to hold
+ *	the data returned by the slave.  SMBus allows at most 32 bytes.

Sidenote: SMBus3 allows 255 byte, but we don't support that (yet), so this is okay for now.

Quoted 9 lines

drivers/i2c/i2c-core.c trace: · line 2920

Quoted 4 lines
drivers/i2c/i2c-core.c +4 -0
+ *
+ * This executes the SMBus "block read" protocol if supported by the adapter.
+ * If block read is not supported, it emulates it using either word or byte
+ * read protocols depending on availability.

Here I'd like to see a warning that people should double-check if their I2C slave does support that. Sometimes one can't exchange a block transfer with a byte transfer.

Ok.

Quoted 12 lines
Quoted 12 lines
drivers/i2c/i2c-core.c +12 -0
+ */
+s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
+					      u8 command, u8 length, u8 *values)
+{
+	u8 i;
+	int status;
+
+	if (length > I2C_SMBUS_BLOCK_MAX)
+		length = I2C_SMBUS_BLOCK_MAX;
+
+	if (i2c_check_functionality(client->adapter,
+				    I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
Quoted 9 lines

drivers/i2c/i2c-core.c trace: · line 2937

Quoted 5 lines
drivers/i2c/i2c-core.c +5 -0
+		return i2c_smbus_read_i2c_block_data(client, command,
+						     length, values);
+	} else if (i2c_check_functionality(client->adapter,
+					   I2C_FUNC_SMBUS_READ_WORD_DATA |
+					   I2C_FUNC_SMBUS_READ_BYTE_DATA)) {

What about skipping the need for READ_BYTE_DATA and dump the byte which was maybe read too much?

Yes, that will simply the code.

Quoted 37 lines
Quoted 23 lines
drivers/i2c/i2c-core.c +23 -0
+		for (i = 0; (i + 2) <= length; i += 2) {
+			status = i2c_smbus_read_word_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status & 0xff;
+			values[i+1] = status >> 8;
+		}
+		if (i < length) {
+			status = i2c_smbus_read_byte_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status;
+			i++;
+		}
+		return i;
+	} else if (i2c_check_functionality(client->adapter,
+					   I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
+		for (i = 0; i < length; i++) {
+			status = i2c_smbus_read_byte_data(client, command + i);
+			if (status < 0)
+				return status;
+			values[i] = status;
+		}

drivers/i2c/i2c-core.c trace: · line 2970

Quoted 10 lines
drivers/i2c/i2c-core.c +10 -0
+		return i;
+	}
+
+	dev_err(&client->adapter->dev, "Unsupported transactions: %d,%d,%d\n",
+		I2C_SMBUS_I2C_BLOCK_DATA, I2C_SMBUS_WORD_DATA,
+		I2C_SMBUS_BYTE_DATA);
+
+	return -EOPNOTSUPP;
+}
+EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);

Thanks,

Wolfram