From: Jean Delvare <jdelvare@suse.de>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Mark Brown <broonie@kernel.org>,
Robert Rosengren <robert.rosengren@axis.com>,
linux-kernel@vger.kernel.org,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH] regmap: Fix i2c word access when using SMBus access functions
Date: Mon, 2 Feb 2015 11:09:31 +0100 [thread overview]
Message-ID: <20150202110931.7671a200@endymion.delvare> (raw)
In-Reply-To: <1422834481-6936-1-git-send-email-linux@roeck-us.net>
Hi Guenter,
On Sun, 1 Feb 2015 15:48:01 -0800, Guenter Roeck wrote:
> 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.)
> 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.
>
> Cc: Jean Delvare <jdelvare@suse.de>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
> 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(-)
>
> diff --git a/drivers/base/regmap/regmap-i2c.c b/drivers/base/regmap/regmap-i2c.c
> index 053150a..865f4f8 100644
> --- a/drivers/base/regmap/regmap-i2c.c
> +++ b/drivers/base/regmap/regmap-i2c.c
> @@ -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 ®map_smbus_word;
> + if (config->val_format_endian == REGMAP_ENDIAN_LITTLE)
> + return ®map_smbus_word;
> + else
> + return ®map_smbus_word_swapped;
Indentation looks wrong.
> 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.
Reviewed-by: Jean Delvare <jdelvare@suse.de>
--
Jean Delvare
SUSE L3 Support
next prev parent reply other threads:[~2015-02-02 10:09 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-01 23:48 Guenter Roeck
2015-02-02 10:09 ` Jean Delvare [this message]
2015-02-02 10:26 ` Lars-Peter Clausen
2015-02-02 11:56 ` Mark Brown
2015-02-02 14:30 ` Guenter Roeck
2015-02-03 11:42 ` Mark Brown
2015-02-03 14:21 ` Guenter Roeck
2015-02-03 16:09 ` Mark Brown
2015-02-03 16:58 ` Guenter Roeck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150202110931.7671a200@endymion.delvare \
--to=jdelvare@suse.de \
--cc=broonie@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=robert.rosengren@axis.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®