* [PATCH v2 1/2] regmap: Export regmap_get_val_endian @ 2015-02-03 18:01 Guenter Roeck 2015-02-03 18:01 ` [PATCH v2 2/2] regmap: Fix i2c word access when using SMBus access functions Guenter Roeck 2015-02-04 20:48 ` [PATCH v2 1/2] regmap: Export regmap_get_val_endian Mark Brown 0 siblings, 2 replies; 3+ messages in thread From: Guenter Roeck @ 2015-02-03 18:01 UTC (permalink / raw) To: Mark Brown; +Cc: linux-kernel, Guenter Roeck, Jean Delvare We'll need to call it from regmap-i2c.c, which can be built as module. Cc: Jean Delvare <jdelvare@suse.de> Signed-off-by: Guenter Roeck <linux@roeck-us.net> --- v2: New patch drivers/base/regmap/internal.h | 4 ++++ drivers/base/regmap/regmap.c | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h index 0da5865..c49a79e 100644 --- a/drivers/base/regmap/internal.h +++ b/drivers/base/regmap/internal.h @@ -233,6 +233,10 @@ int _regmap_raw_write(struct regmap *map, unsigned int reg, void regmap_async_complete_cb(struct regmap_async *async, int ret); +enum regmap_endian regmap_get_val_endian(struct device *dev, + const struct regmap_bus *bus, + const struct regmap_config *config); + extern struct regcache_ops regcache_rbtree_ops; extern struct regcache_ops regcache_lzo_ops; extern struct regcache_ops regcache_flat_ops; diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c index d2f8a81..f99b098 100644 --- a/drivers/base/regmap/regmap.c +++ b/drivers/base/regmap/regmap.c @@ -473,9 +473,9 @@ static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus, return REGMAP_ENDIAN_BIG; } -static enum regmap_endian regmap_get_val_endian(struct device *dev, - const struct regmap_bus *bus, - const struct regmap_config *config) +enum regmap_endian regmap_get_val_endian(struct device *dev, + const struct regmap_bus *bus, + const struct regmap_config *config) { struct device_node *np; enum regmap_endian endian; @@ -513,6 +513,7 @@ static enum regmap_endian regmap_get_val_endian(struct device *dev, /* Use this if no other value was found */ return REGMAP_ENDIAN_BIG; } +EXPORT_SYMBOL_GPL(regmap_get_val_endian); /** * regmap_init(): Initialise register map -- 2.1.0 ^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] regmap: Fix i2c word access when using SMBus access functions 2015-02-03 18:01 [PATCH v2 1/2] regmap: Export regmap_get_val_endian Guenter Roeck @ 2015-02-03 18:01 ` Guenter Roeck 2015-02-04 20:48 ` [PATCH v2 1/2] regmap: Export regmap_get_val_endian Mark Brown 1 sibling, 0 replies; 3+ messages in thread From: Guenter Roeck @ 2015-02-03 18:01 UTC (permalink / raw) To: Mark Brown; +Cc: linux-kernel, Guenter Roeck, Jean Delvare 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 if a chip is configured as REGMAP_ENDIAN_BIG. If the chip is configured as REGMAP_ENDIAN_LITTLE, keep using regmap_smbus_write_word_data and regmap_smbus_read_word_data. Otherwise reject registration if the controller does not support direct i2c accesses. Cc: Jean Delvare <jdelvare@suse.de> Signed-off-by: Guenter Roeck <linux@roeck-us.net> --- v2: SMBUs -> SMBus, 16 bit -> 16-bit in commit log Use regmap_get_val_endian to get configured chip endianness Only accept REGMAP_ENDIAN_LITTLE and REGMAP_ENDIAN_BIG for SMBus 16-bit accesses drivers/base/regmap/regmap-i2c.c | 46 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/drivers/base/regmap/regmap-i2c.c b/drivers/base/regmap/regmap-i2c.c index 053150a..4b76e33 100644 --- a/drivers/base/regmap/regmap-i2c.c +++ b/drivers/base/regmap/regmap-i2c.c @@ -14,6 +14,7 @@ #include <linux/i2c.h> #include <linux/module.h> +#include "internal.h" static int regmap_smbus_byte_reg_read(void *context, unsigned int reg, unsigned int *val) @@ -87,6 +88,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 +217,14 @@ 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 ®map_smbus_word; + switch (regmap_get_val_endian(&i2c->dev, NULL, config)) { + case REGMAP_ENDIAN_LITTLE: + return ®map_smbus_word; + case REGMAP_ENDIAN_BIG: + return ®map_smbus_word_swapped; + default: /* everything else is not supported */ + break; + } else if (config->val_bits == 8 && config->reg_bits == 8 && i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) -- 2.1.0 ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2 1/2] regmap: Export regmap_get_val_endian 2015-02-03 18:01 [PATCH v2 1/2] regmap: Export regmap_get_val_endian Guenter Roeck 2015-02-03 18:01 ` [PATCH v2 2/2] regmap: Fix i2c word access when using SMBus access functions Guenter Roeck @ 2015-02-04 20:48 ` Mark Brown 1 sibling, 0 replies; 3+ messages in thread From: Mark Brown @ 2015-02-04 20:48 UTC (permalink / raw) To: Guenter Roeck; +Cc: linux-kernel, Jean Delvare [-- Attachment #1: Type: text/plain, Size: 159 bytes --] On Tue, Feb 03, 2015 at 10:01:18AM -0800, Guenter Roeck wrote: > We'll need to call it from regmap-i2c.c, which can be built as module. Applied both, thanks. [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 473 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-02-04 20:48 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2015-02-03 18:01 [PATCH v2 1/2] regmap: Export regmap_get_val_endian Guenter Roeck 2015-02-03 18:01 ` [PATCH v2 2/2] regmap: Fix i2c word access when using SMBus access functions Guenter Roeck 2015-02-04 20:48 ` [PATCH v2 1/2] regmap: Export regmap_get_val_endian Mark Brown
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
Powered by JetHome