Skip to contentSkip to conversation

regmap: Fix i2c word access when using SMBus access functions

PATCH In review Guenter Roeck 4 participants 9 messages Feb 1, 2015 – Feb 3, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regmap: Fix i2c word access when using SMBus access functions

SMBUs access functions assume that 16 bit values are formatted as little endian numbers. The direct i2c access functions in regmap, however, assume that 16 bit values are formatted as big endian numbers. As a result, the current code returns different values if an i2c chip's 16 bit registers are accessed through i2c access functions vs. SMBus access functions.

Use regmap_smbus_read_word_swapped and regmap_smbus_write_word_swapped for 16 bit SMBus accesses unless a chip is registered with val_format_endian set to REGMAP_ENDIAN_LITTLE. In the latter case, keep using regmap_smbus_write_word_data and regmap_smbus_read_word_data.

---
Might be a candidate for -stable, though as far as I can see there is currently no driver in the upstream kernel using regmap for 16-bit i2c accesses.

 drivers/base/regmap/regmap-i2c.c | 41 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)
drivers/base/regmap/regmap-i2c.c +40 -1
index 053150a..865f4f8 100644
@@ -87,6 +87,42 @@ static struct regmap_bus regmap_smbus_word = {
 	.reg_read = regmap_smbus_word_reg_read,
 };
 
+static int regmap_smbus_word_read_swapped(void *context, unsigned int reg,
+					  unsigned int *val)
+{
+	struct device *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
+	int ret;
+
+	if (reg > 0xff)
+		return -EINVAL;
+
+	ret = i2c_smbus_read_word_swapped(i2c, reg);
+	if (ret < 0)
+		return ret;
+
+	*val = ret;
+
+	return 0;
+}
+
+static int regmap_smbus_word_write_swapped(void *context, unsigned int reg,
+					   unsigned int val)
+{
+	struct device *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
+
+	if (val > 0xffff || reg > 0xff)
+		return -EINVAL;
+
+	return i2c_smbus_write_word_swapped(i2c, reg, val);
+}
+
+static struct regmap_bus regmap_smbus_word_swapped = {
+	.reg_write = regmap_smbus_word_write_swapped,
+	.reg_read = regmap_smbus_word_read_swapped,
+};
+
 static int regmap_i2c_write(void *context, const void *data, size_t count)
 {
 	struct device *dev = context;
@@ -180,7 +216,10 @@ static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
 	else if (config->val_bits == 16 && config->reg_bits == 8 &&
 		 i2c_check_functionality(i2c->adapter,
 					 I2C_FUNC_SMBUS_WORD_DATA))
-		return &regmap_smbus_word;
+			if (config->val_format_endian == REGMAP_ENDIAN_LITTLE)
+				return &regmap_smbus_word;
+			else
+				return &regmap_smbus_word_swapped;
 	else if (config->val_bits == 8 && config->reg_bits == 8 &&
 		 i2c_check_functionality(i2c->adapter,
 					 I2C_FUNC_SMBUS_BYTE_DATA))
Signature · 1 line
2.1.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Guenter,

Quoted 1 line · Guenter Roeck

SMBUs access functions assume that 16 bit values are formatted as

Correct spelling is SMBus (lowercase U.) Also I think "16 bit" is better spelled "16-bit" (more occurrences below.)

Quoted 68 lines

little endian numbers. The direct i2c access functions in regmap, however, assume that 16 bit values are formatted as big endian numbers. As a result, the current code returns different values if an i2c chip's 16 bit registers are accessed through i2c access functions vs. SMBus access functions.

Use regmap_smbus_read_word_swapped and regmap_smbus_write_word_swapped for 16 bit SMBus accesses unless a chip is registered with val_format_endian set to REGMAP_ENDIAN_LITTLE. In the latter case, keep using regmap_smbus_write_word_data and regmap_smbus_read_word_data.

---
Might be a candidate for -stable, though as far as I can see there is currently no driver in the upstream kernel using regmap for 16-bit i2c accesses.

 drivers/base/regmap/regmap-i2c.c | 41 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)
drivers/base/regmap/regmap-i2c.c +36 -0
index 053150a..865f4f8 100644
@@ -87,6 +87,42 @@ static struct regmap_bus regmap_smbus_word = {
 	.reg_read = regmap_smbus_word_reg_read,
 };
 
+static int regmap_smbus_word_read_swapped(void *context, unsigned int reg,
+					  unsigned int *val)
+{
+	struct device *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
+	int ret;
+
+	if (reg > 0xff)
+		return -EINVAL;
+
+	ret = i2c_smbus_read_word_swapped(i2c, reg);
+	if (ret < 0)
+		return ret;
+
+	*val = ret;
+
+	return 0;
+}
+
+static int regmap_smbus_word_write_swapped(void *context, unsigned int reg,
+					   unsigned int val)
+{
+	struct device *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
+
+	if (val > 0xffff || reg > 0xff)
+		return -EINVAL;
+
+	return i2c_smbus_write_word_swapped(i2c, reg, val);
+}
+
+static struct regmap_bus regmap_smbus_word_swapped = {
+	.reg_write = regmap_smbus_word_write_swapped,
+	.reg_read = regmap_smbus_word_read_swapped,
+};
+
 static int regmap_i2c_write(void *context, const void *data, size_t count)
 {
 	struct device *dev = context;

drivers/base/regmap/regmap-i2c.c static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c, · line 222

Quoted 8 lines
drivers/base/regmap/regmap-i2c.c +4 -1
@@ -180,7 +216,10 @@ static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
 	else if (config->val_bits == 16 && config->reg_bits == 8 &&
 		 i2c_check_functionality(i2c->adapter,
 					 I2C_FUNC_SMBUS_WORD_DATA))
-		return &regmap_smbus_word;
+			if (config->val_format_endian == REGMAP_ENDIAN_LITTLE)
+				return &regmap_smbus_word;
+			else
+				return &regmap_smbus_word_swapped;

Indentation looks wrong.

drivers/base/regmap/regmap-i2c.c static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c, · line 225

Quoted 3 lines
drivers/base/regmap/regmap-i2c.c +0 -0
 	else if (config->val_bits == 8 && config->reg_bits == 8 &&
 		 i2c_check_functionality(i2c->adapter,
 					 I2C_FUNC_SMBUS_BYTE_DATA))

The code itself looks sane, although I am not a regmap expert.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#2 lore raw
In reply to Guenter Roeck

On 02/02/2015 12:48 AM, Guenter Roeck wrote:
[...]

drivers/base/regmap/regmap-i2c.c static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c, · line 219

Quoted 9 lines
  static int regmap_i2c_write(void *context, const void *data, size_t count)
  {
  	struct device *dev = context;
drivers/base/regmap/regmap-i2c.c +1 -1
@@ -180,7 +216,10 @@ static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
  	else if (config->val_bits == 16 && config->reg_bits == 8 &&
  		 i2c_check_functionality(i2c->adapter,
  					 I2C_FUNC_SMBUS_WORD_DATA))
-		return &regmap_smbus_word;
+			if (config->val_format_endian == REGMAP_ENDIAN_LITTLE)

This should probably use regmap_get_val_endian() and maybe also handle REGMAP_ENDIAN_NATIVE.

Quoted 7 lines
drivers/base/regmap/regmap-i2c.c +3 -0
+				return &regmap_smbus_word;
+			else
+				return &regmap_smbus_word_swapped;
  	else if (config->val_bits == 8 && config->reg_bits == 8 &&
  		 i2c_check_functionality(i2c->adapter,
  					 I2C_FUNC_SMBUS_BYTE_DATA))
maintainer
#3 lore raw
Quoted 7 lines · Lars-Peter Clausen

drivers/base/regmap/regmap-i2c.c static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c, · line 219

Quoted 2 lines · Guenter Roeck
drivers/base/regmap/regmap-i2c.c +1 -1
-		return &regmap_smbus_word;
+			if (config->val_format_endian == REGMAP_ENDIAN_LITTLE)

This should probably use regmap_get_val_endian() and maybe also handle REGMAP_ENDIAN_NATIVE.

Yes, we really ought to handle _NATIVE too (though the chances of it being used with I2C are minimal, it's mostly for MMIO). This also feels like it's something that should be being handled further up the stack in the serialization code but given that there's direct functions for this in the smbus code perhaps it's better here. Or perhaps the smbus support ought to be transitioned to use the bus interface and set reg_write() and reg_read() operations now that we can do that, it seems like a better fit though it might break compatibility with wierd devices.

#4 lore raw
Quoted 9 lines · Mark Brown
Quoted 7 lines · Lars-Peter Clausen

drivers/base/regmap/regmap-i2c.c static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c, · line 219

Quoted 2 lines · Guenter Roeck
drivers/base/regmap/regmap-i2c.c +1 -1
-		return &regmap_smbus_word;
+			if (config->val_format_endian == REGMAP_ENDIAN_LITTLE)

This should probably use regmap_get_val_endian() and maybe also handle REGMAP_ENDIAN_NATIVE.

That means I'll have to make the function global and pass NULL as bus argument, which ultimately means that it is going to return either config->val_format_endian or REGMAP_ENDIAN_BIG. I didn't do that because I thought it has no value, but no problem.

Quoted 2 lines

Yes, we really ought to handle _NATIVE too (though the chances of it being used with I2C are minimal, it's mostly for MMIO). This also feels

Well, we do; it is handled similar to the big endian case with the current code. Do you think it should be handled differently ? If yes, how ?

Quoted 7 lines

like it's something that should be being handled further up the stack in the serialization code but given that there's direct functions for this in the smbus code perhaps it's better here. Or perhaps the smbus support ought to be transitioned to use the bus interface and set reg_write() and reg_read() operations now that we can do that, it seems like a better fit though it might break compatibility with wierd devices.

I thought about that, but since the smbus functions perform endianness conversion it would mean that I would have to undo that conversion just to have it done again.

Thanks,
Guenter

maintainer
#5 lore raw
Quoted 7 lines · Guenter Roeck
Quoted 2 lines · Mark Brown

Yes, we really ought to handle _NATIVE too (though the chances of it being used with I2C are minimal, it's mostly for MMIO). This also feels

Well, we do; it is handled similar to the big endian case with the current code. Do you think it should be handled differently ? If yes, how ?

Perhaps it just needs to be more explicit about how it's handling native endian? I didn't spot it.

Quoted 10 lines
Quoted 6 lines

like it's something that should be being handled further up the stack in the serialization code but given that there's direct functions for this in the smbus code perhaps it's better here. Or perhaps the smbus support ought to be transitioned to use the bus interface and set reg_write() and reg_read() operations now that we can do that, it seems like a better fit though it might break compatibility with wierd devices.

I thought about that, but since the smbus functions perform endianness conversion it would mean that I would have to undo that conversion just to have it done again.

No, the whole point is that by doing this you avoid any endianness conversions or formatting in the framework at all so you can just use the smbus functions to handle the formatting.

#6 lore raw
Quoted 12 lines · Mark Brown
Quoted 7 lines · Guenter Roeck
Quoted 2 lines · Mark Brown

Yes, we really ought to handle _NATIVE too (though the chances of it being used with I2C are minimal, it's mostly for MMIO). This also feels

Well, we do; it is handled similar to the big endian case with the current code. Do you think it should be handled differently ? If yes, how ?

Perhaps it just needs to be more explicit about how it's handling native endian? I didn't spot it.

Thinking about it, we should actually reject requests for _NATIVE. SMBus 16 bit accesses are either little endian or big endian.

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

like it's something that should be being handled further up the stack in the serialization code but given that there's direct functions for this in the smbus code perhaps it's better here. Or perhaps the smbus support ought to be transitioned to use the bus interface and set reg_write() and reg_read() operations now that we can do that, it seems like a better fit though it might break compatibility with wierd devices.

Quoted 8 lines
Quoted 3 lines

I thought about that, but since the smbus functions perform endianness conversion it would mean that I would have to undo that conversion just to have it done again.

No, the whole point is that by doing this you avoid any endianness conversions or formatting in the framework at all so you can just use the smbus functions to handle the formatting.

Ah, guess I got confused. The SMBus accesses are already using reg_read and reg_write.

Thanks,
Guenter

maintainer
#7 lore raw
Quoted 7 lines · Guenter Roeck
Quoted 2 lines · Mark Brown

Perhaps it just needs to be more explicit about how it's handling native endian? I didn't spot it.

Thinking about it, we should actually reject requests for _NATIVE. SMBus 16 bit accesses are either little endian or big endian.

Yes, that makes sense. It's also hard to see a use case for _NATIVE and smbus.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

I thought about that, but since the smbus functions perform endianness conversion it would mean that I would have to undo that conversion just to have it done again.

No, the whole point is that by doing this you avoid any endianness conversions or formatting in the framework at all so you can just use the smbus functions to handle the formatting.

Ah, guess I got confused. The SMBus accesses are already using reg_read and reg_write.

It's also possible that you've spotted some bug, wouldn't be the first time. Was this spotted by code inspection or by running into a problem?

#8 lore raw
Quoted 20 lines · Mark Brown
Quoted 7 lines · Guenter Roeck
Quoted 2 lines · Mark Brown

Perhaps it just needs to be more explicit about how it's handling native endian? I didn't spot it.

Thinking about it, we should actually reject requests for _NATIVE. SMBus 16 bit accesses are either little endian or big endian.

Yes, that makes sense. It's also hard to see a use case for _NATIVE and smbus.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

I thought about that, but since the smbus functions perform endianness conversion it would mean that I would have to undo that conversion just to have it done again.

No, the whole point is that by doing this you avoid any endianness conversions or formatting in the framework at all so you can just use the smbus functions to handle the formatting.

Quoted 5 lines
Quoted 2 lines

Ah, guess I got confused. The SMBus accesses are already using reg_read and reg_write.

It's also possible that you've spotted some bug, wouldn't be the first time. Was this spotted by code inspection or by running into a problem?

Oh, I did find a bug; that is the whole reason for this patch. I converted a driver (hwmon/ads7828) to use regmap. The result works fine with a real chip connected to an i2c controller supporting direct i2c accesses. However, my module test code using i2c-stub (which only supports smbus accesses) fails.

In the current code, the direct i2c access functions assume that the chip reports data MSB first. The SMBus word access functions, however, assume LSB first. The ADS7828 reports data MSB first, so it works with the i2c access functions but not with the currently used SMBus access functions.

A simpler fix would have been to use i2c_smbus_read_word_swapped and i2c_smbus_write_word_swapped instead of i2c_smbus_read_word_data and i2c_smbus_write_word_data, but then we would end up not supporting SMBus devices which _do_ report data LSB first.

Guenter