linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
@ 2011-09-22 13:48 Jonathan Cameron
       [not found] ` <1316699294-6936-1-git-send-email-jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Jonathan Cameron @ 2011-09-22 13:48 UTC (permalink / raw)
  To: khali-PUYAD+kWke1g9hUCZPvPmw, ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA
  Cc: Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA, Jonathan Cameron

Reimplemented at least 17 times discounting error mangling cases
where it could be used.

Signed-off-by: Jonathan Cameron <jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
---

Hi All,

Quite a number of devices rather unhelpfully handle smbus read/write word
commands but return the result byte swapped.  Hence drivers swap it back
again.

Examples based on quick grep or read users that byte swap(write is completely trivial)

drivers/hwmon/ad7418.c - no error handling so trivial
drivers/hwmon/ads1015.c - correct
drivers/hwmon/asb100.c - no error handling so trivial
drivers/hwmon/ds1621.c - correct
drivers/hwmon/ds620.c - no error handling so trivial
drivers/hwmon/gl518sm.c - no error handling so trivial
drivers/hwmon/gl520sm.c - no error handling so trivial
drivers/hwmon/jc42.c - correct
drivers/hwmon/lm73.c - no error handling
drivers/hwmon/lm75.c - correct
drivers/hwmon/lm92.c - some are byte swapped. Implementation doesn't handle errors
drivers/hwmon/tmp102.c - correct
drivers/hwmon/w83781.c - no error handling.
drivers/input/touchscreen/ad7879-i2c.c - no error handling
drivers/media/video/mt9m001.c - correct
drivers/media/video/mt9m111.c - no error handling
drivers/media/video/mt9t031.c - correct
drivers/media/video/mt9v022.c - correct
drivers/media/video/mt9v032.c - correct
drivers/media/video/vpx3220.c - correct
drivers/staging/iio/adc/ad7150.c - correct
drivers/staging/iio/adc/ad7152.c - correct
drivers/staging/iio/adc/ad7291.c - correct
drivers/staging/iio/adc/ad7746.c - correct
drivers/staging/iio/adc/ad799x_core.c - correct
drivers/staging/iio/adc/adt7410.c - correct
drivers/staging/iio/adc/adt75.c - correct

'correct' are those that need handle or at least pass on the error code without
mangling it.  The others typically just shove an error into some local
cache without taking any notice.

Just for the curious this is based on greping for i2c_smbus_write_word_data and
looking to see if the read does the swab16 as well.

Anyhow, so to the proposal.  Introduce a couple of inline static functions into
i2c.h.

My only use examples done so far are on top of unpublished iio
changes, so I'll leave the reader to take a look and decided
whether or not this is interesting enough to do.

Even if the driver uses equivalent functions we are saving about
6 lines per user.  I'm happy to do a series converting the easy
ones from the above if people don't mind the patch.

 include/linux/i2c.h |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index a6c652e..59ae02b 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -34,6 +34,7 @@
 #include <linux/sched.h>	/* for completion */
 #include <linux/mutex.h>
 #include <linux/of.h>		/* for struct device_node */
+#include <linux/swab.h>
 
 extern struct bus_type i2c_bus_type;
 extern struct device_type i2c_adapter_type;
@@ -88,6 +89,23 @@ extern s32 i2c_smbus_read_word_data(const struct i2c_client *client,
 				    u8 command);
 extern s32 i2c_smbus_write_word_data(const struct i2c_client *client,
 				     u8 command, u16 value);
+
+static inline s32
+i2c_smbus_read_word_data_swapped(const struct i2c_client *client,
+				 u8 command)
+{
+	s32 value = i2c_smbus_read_word_data(client, command);
+
+	return (value < 0) ? value : swab16(value);
+}
+
+static inline s32
+i2c_smbus_write_word_data_swapped(const struct i2c_client *client,
+				  u8 command, u16 value)
+{
+	return i2c_smbus_write_word_data(client, command, swab16(value));
+}
+
 /* Returns the number of read bytes */
 extern s32 i2c_smbus_read_block_data(const struct i2c_client *client,
 				     u8 command, u8 *values);
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found] ` <1316699294-6936-1-git-send-email-jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
@ 2011-10-04 16:37   ` Jonathan Cameron
       [not found]     ` <4E8B3637.1030704-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
  2011-10-08 21:10   ` Jean Delvare
  1 sibling, 1 reply; 11+ messages in thread
From: Jonathan Cameron @ 2011-10-04 16:37 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: khali-PUYAD+kWke1g9hUCZPvPmw, ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA

On 09/22/11 14:48, Jonathan Cameron wrote:
> Reimplemented at least 17 times discounting error mangling cases
> where it could be used.
> 
Anyone care to comment?
> Signed-off-by: Jonathan Cameron <jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
> ---
> 
> Hi All,
> 
> Quite a number of devices rather unhelpfully handle smbus read/write word
> commands but return the result byte swapped.  Hence drivers swap it back
> again.
> 
> Examples based on quick grep or read users that byte swap(write is completely trivial)
> 
> drivers/hwmon/ad7418.c - no error handling so trivial
> drivers/hwmon/ads1015.c - correct
> drivers/hwmon/asb100.c - no error handling so trivial
> drivers/hwmon/ds1621.c - correct
> drivers/hwmon/ds620.c - no error handling so trivial
> drivers/hwmon/gl518sm.c - no error handling so trivial
> drivers/hwmon/gl520sm.c - no error handling so trivial
> drivers/hwmon/jc42.c - correct
> drivers/hwmon/lm73.c - no error handling
> drivers/hwmon/lm75.c - correct
> drivers/hwmon/lm92.c - some are byte swapped. Implementation doesn't handle errors
> drivers/hwmon/tmp102.c - correct
> drivers/hwmon/w83781.c - no error handling.
> drivers/input/touchscreen/ad7879-i2c.c - no error handling
> drivers/media/video/mt9m001.c - correct
> drivers/media/video/mt9m111.c - no error handling
> drivers/media/video/mt9t031.c - correct
> drivers/media/video/mt9v022.c - correct
> drivers/media/video/mt9v032.c - correct
> drivers/media/video/vpx3220.c - correct
> drivers/staging/iio/adc/ad7150.c - correct
> drivers/staging/iio/adc/ad7152.c - correct
> drivers/staging/iio/adc/ad7291.c - correct
> drivers/staging/iio/adc/ad7746.c - correct
> drivers/staging/iio/adc/ad799x_core.c - correct
> drivers/staging/iio/adc/adt7410.c - correct
> drivers/staging/iio/adc/adt75.c - correct
> 
> 'correct' are those that need handle or at least pass on the error code without
> mangling it.  The others typically just shove an error into some local
> cache without taking any notice.
> 
> Just for the curious this is based on greping for i2c_smbus_write_word_data and
> looking to see if the read does the swab16 as well.
> 
> Anyhow, so to the proposal.  Introduce a couple of inline static functions into
> i2c.h.
> 
> My only use examples done so far are on top of unpublished iio
> changes, so I'll leave the reader to take a look and decided
> whether or not this is interesting enough to do.
> 
> Even if the driver uses equivalent functions we are saving about
> 6 lines per user.  I'm happy to do a series converting the easy
> ones from the above if people don't mind the patch.
> 
>  include/linux/i2c.h |   18 ++++++++++++++++++
>  1 files changed, 18 insertions(+), 0 deletions(-)
> 
> diff --git a/include/linux/i2c.h b/include/linux/i2c.h
> index a6c652e..59ae02b 100644
> --- a/include/linux/i2c.h
> +++ b/include/linux/i2c.h
> @@ -34,6 +34,7 @@
>  #include <linux/sched.h>	/* for completion */
>  #include <linux/mutex.h>
>  #include <linux/of.h>		/* for struct device_node */
> +#include <linux/swab.h>
>  
>  extern struct bus_type i2c_bus_type;
>  extern struct device_type i2c_adapter_type;
> @@ -88,6 +89,23 @@ extern s32 i2c_smbus_read_word_data(const struct i2c_client *client,
>  				    u8 command);
>  extern s32 i2c_smbus_write_word_data(const struct i2c_client *client,
>  				     u8 command, u16 value);
> +
> +static inline s32
> +i2c_smbus_read_word_data_swapped(const struct i2c_client *client,
> +				 u8 command)
> +{
> +	s32 value = i2c_smbus_read_word_data(client, command);
> +
> +	return (value < 0) ? value : swab16(value);
> +}
> +
> +static inline s32
> +i2c_smbus_write_word_data_swapped(const struct i2c_client *client,
> +				  u8 command, u16 value)
> +{
> +	return i2c_smbus_write_word_data(client, command, swab16(value));
> +}
> +
>  /* Returns the number of read bytes */
>  extern s32 i2c_smbus_read_block_data(const struct i2c_client *client,
>  				     u8 command, u8 *values);

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found]     ` <4E8B3637.1030704-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
@ 2011-10-04 17:05       ` Jean Delvare
  0 siblings, 0 replies; 11+ messages in thread
From: Jean Delvare @ 2011-10-04 17:05 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA

Hi Jonathan,

On Tue, 04 Oct 2011 17:37:11 +0100, Jonathan Cameron wrote:
> On 09/22/11 14:48, Jonathan Cameron wrote:
> > Reimplemented at least 17 times discounting error mangling cases
> > where it could be used.
>
> Anyone care to comment?

I will. I started reviewing your patch and pondering its inclusion, but
got sidetracked by more important issues. I will return to this
whenever I can.

-- 
Jean Delvare

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found] ` <1316699294-6936-1-git-send-email-jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
  2011-10-04 16:37   ` Jonathan Cameron
@ 2011-10-08 21:10   ` Jean Delvare
       [not found]     ` <20111008231005.41161836-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
  1 sibling, 1 reply; 11+ messages in thread
From: Jean Delvare @ 2011-10-08 21:10 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA

Hi Jonathan,

On Thu, 22 Sep 2011 14:48:14 +0100, Jonathan Cameron wrote:
> Reimplemented at least 17 times discounting error mangling cases
> where it could be used.
> 
> Signed-off-by: Jonathan Cameron <jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
> ---
> 
> Hi All,
> 
> Quite a number of devices rather unhelpfully handle smbus read/write word
> commands but return the result byte swapped.  Hence drivers swap it back
> again.
> 
> Examples based on quick grep or read users that byte swap(write is completely trivial)
> 
> drivers/hwmon/ad7418.c - no error handling so trivial
> drivers/hwmon/ads1015.c - correct
> drivers/hwmon/asb100.c - no error handling so trivial
> drivers/hwmon/ds1621.c - correct
> drivers/hwmon/ds620.c - no error handling so trivial
> drivers/hwmon/gl518sm.c - no error handling so trivial
> drivers/hwmon/gl520sm.c - no error handling so trivial
> drivers/hwmon/jc42.c - correct
> drivers/hwmon/lm73.c - no error handling
> drivers/hwmon/lm75.c - correct
> drivers/hwmon/lm92.c - some are byte swapped. Implementation doesn't handle errors
> drivers/hwmon/tmp102.c - correct
> drivers/hwmon/w83781.c - no error handling.
> drivers/input/touchscreen/ad7879-i2c.c - no error handling
> drivers/media/video/mt9m001.c - correct
> drivers/media/video/mt9m111.c - no error handling
> drivers/media/video/mt9t031.c - correct
> drivers/media/video/mt9v022.c - correct
> drivers/media/video/mt9v032.c - correct
> drivers/media/video/vpx3220.c - correct
> drivers/staging/iio/adc/ad7150.c - correct
> drivers/staging/iio/adc/ad7152.c - correct
> drivers/staging/iio/adc/ad7291.c - correct
> drivers/staging/iio/adc/ad7746.c - correct
> drivers/staging/iio/adc/ad799x_core.c - correct
> drivers/staging/iio/adc/adt7410.c - correct
> drivers/staging/iio/adc/adt75.c - correct
> 
> 'correct' are those that need handle or at least pass on the error code without
> mangling it.  The others typically just shove an error into some local
> cache without taking any notice.
> 
> Just for the curious this is based on greping for i2c_smbus_write_word_data and
> looking to see if the read does the swab16 as well.
> 
> Anyhow, so to the proposal.  Introduce a couple of inline static functions into
> i2c.h.
> 
> My only use examples done so far are on top of unpublished iio
> changes, so I'll leave the reader to take a look and decided
> whether or not this is interesting enough to do.
> 
> Even if the driver uses equivalent functions we are saving about
> 6 lines per user.  I'm happy to do a series converting the easy
> ones from the above if people don't mind the patch.
> 
>  include/linux/i2c.h |   18 ++++++++++++++++++
>  1 files changed, 18 insertions(+), 0 deletions(-)
> 
> diff --git a/include/linux/i2c.h b/include/linux/i2c.h
> index a6c652e..59ae02b 100644
> --- a/include/linux/i2c.h
> +++ b/include/linux/i2c.h
> @@ -34,6 +34,7 @@
>  #include <linux/sched.h>	/* for completion */
>  #include <linux/mutex.h>
>  #include <linux/of.h>		/* for struct device_node */
> +#include <linux/swab.h>

Maybe add /* for swab16 */ for consistency with other comments above.

>  
>  extern struct bus_type i2c_bus_type;
>  extern struct device_type i2c_adapter_type;
> @@ -88,6 +89,23 @@ extern s32 i2c_smbus_read_word_data(const struct i2c_client *client,
>  				    u8 command);
>  extern s32 i2c_smbus_write_word_data(const struct i2c_client *client,
>  				     u8 command, u16 value);
> +
> +static inline s32
> +i2c_smbus_read_word_data_swapped(const struct i2c_client *client,
> +				 u8 command)
> +{
> +	s32 value = i2c_smbus_read_word_data(client, command);
> +
> +	return (value < 0) ? value : swab16(value);
> +}
> +
> +static inline s32
> +i2c_smbus_write_word_data_swapped(const struct i2c_client *client,
> +				  u8 command, u16 value)
> +{
> +	return i2c_smbus_write_word_data(client, command, swab16(value));
> +}
> +
>  /* Returns the number of read bytes */
>  extern s32 i2c_smbus_read_block_data(const struct i2c_client *client,
>  				     u8 command, u8 *values);

It might make sense to change the _data_swapped suffix to just
_swapped, for the sake of avoiding overly long function names (_data
was never meaningful for the original functions anyway as there is no
ambiguity.)

Other than these minor details, I like the patch very much, I tested if
with 9 hwmon drivers and it indeed makes the drivers cleaner/smaller.
I'll be happy to commit it when I am able to do this again.

-- 
Jean Delvare

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found]     ` <20111008231005.41161836-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
@ 2011-10-09  7:41       ` Jean Delvare
       [not found]         ` <20111009094118.44b616bf-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Jean Delvare @ 2011-10-09  7:41 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA

On Sat, 8 Oct 2011 23:10:05 +0200, Jean Delvare wrote:
> On Thu, 22 Sep 2011 14:48:14 +0100, Jonathan Cameron wrote:
> > @@ -88,6 +89,23 @@ extern s32 i2c_smbus_read_word_data(const struct i2c_client *client,
> >  				    u8 command);
> >  extern s32 i2c_smbus_write_word_data(const struct i2c_client *client,
> >  				     u8 command, u16 value);
> > +
> > +static inline s32
> > +i2c_smbus_read_word_data_swapped(const struct i2c_client *client,
> > +				 u8 command)
> > +{
> > +	s32 value = i2c_smbus_read_word_data(client, command);
> > +
> > +	return (value < 0) ? value : swab16(value);
> > +}
> > +
> > +static inline s32
> > +i2c_smbus_write_word_data_swapped(const struct i2c_client *client,
> > +				  u8 command, u16 value)
> > +{
> > +	return i2c_smbus_write_word_data(client, command, swab16(value));
> > +}
> > +
> >  /* Returns the number of read bytes */
> >  extern s32 i2c_smbus_read_block_data(const struct i2c_client *client,
> >  				     u8 command, u8 *values);
> 
> It might make sense to change the _data_swapped suffix to just
> _swapped, for the sake of avoiding overly long function names (_data
> was never meaningful for the original functions anyway as there is no
> ambiguity.)
> 
> Other than these minor details, I like the patch very much, I tested if
> with 9 hwmon drivers and it indeed makes the drivers cleaner/smaller.
> I'll be happy to commit it when I am able to do this again.

Oh, and I think the new functions should be listed in
Documentation/i2c/smbus-protocol. Maybe not as separate entries, as
they are not new fundamental types, nor part of the SMBus protocol, but
at least as as notes in the SMBus Read Word and SMBus Write Word
sections.

-- 
Jean Delvare

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH V2] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found]         ` <20111009094118.44b616bf-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
@ 2011-10-10  9:07           ` Jonathan Cameron
  2011-10-10  9:07           ` [PATCH] " Jonathan Cameron
  1 sibling, 0 replies; 11+ messages in thread
From: Jonathan Cameron @ 2011-10-10  9:07 UTC (permalink / raw)
  To: linux-i2c-u79uwXL29TY76Z2rM5mHXA
  Cc: khali-PUYAD+kWke1g9hUCZPvPmw, ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA, Jonathan Cameron

V2: Jean's suggestions:

1) Documentation as comments on the smbus_write_word_data
and smbus_read_word_data entries in
Documetation/i2c/smbus-protocol
2) /* for swab 16 comment */
3) shorter names for the two functions (drop the _data part
as it doesn't tell us anything anyway).

V1:

Hi All,

Quite a number of devices rather unhelpfully handle smbus read/write word
commands but return the result byte swapped.  Hence drivers swap it back
again.

Examples based on quick grep or read users that byte swap(write is completely trivial)

drivers/hwmon/ad7418.c - no error handling so trivial
drivers/hwmon/ads1015.c - correct
drivers/hwmon/asb100.c - no error handling so trivial
drivers/hwmon/ds1621.c - correct
drivers/hwmon/ds620.c - no error handling so trivial
drivers/hwmon/gl518sm.c - no error handling so trivial
drivers/hwmon/gl520sm.c - no error handling so trivial
drivers/hwmon/jc42.c - correct
drivers/hwmon/lm73.c - no error handling
drivers/hwmon/lm75.c - correct
drivers/hwmon/lm92.c - some are byte swapped. Implementation doesn't handle errors
drivers/hwmon/tmp102.c - correct
drivers/hwmon/w83781.c - no error handling.
drivers/input/touchscreen/ad7879-i2c.c - no error handling
drivers/media/video/mt9m001.c - correct
drivers/media/video/mt9m111.c - no error handling
drivers/media/video/mt9t031.c - correct
drivers/media/video/mt9v022.c - correct
drivers/media/video/mt9v032.c - correct
drivers/media/video/vpx3220.c - correct
drivers/staging/iio/adc/ad7150.c - correct
drivers/staging/iio/adc/ad7152.c - correct
drivers/staging/iio/adc/ad7291.c - correct
drivers/staging/iio/adc/ad7746.c - correct
drivers/staging/iio/adc/ad799x_core.c - correct
drivers/staging/iio/adc/adt7410.c - correct
drivers/staging/iio/adc/adt75.c - correct

'correct' are those that need handle or at least pass on the error code without
mangling it.  The others typically just shove an error into some local
cache without taking any notice.

Just for the curious this is based on greping for i2c_smbus_write_word_data and
looking to see if the read does the swab16 as well.

Anyhow, so to the proposal.  Introduce a couple of inline static functions into
i2c.h.

My only use examples done so far are on top of unpublished iio
changes, so I'll leave the reader to take a look and decided
whether or not this is interesting enough to do.

Even if the driver uses equivalent functions we are saving about
6 lines per user.  I'm happy to do a series converting the easy
ones from the above if people don't mind the patch.

Jonathan Cameron (1):
  i2c: boilerplate function for byte swapped smbus_write/read_word_data

 Documentation/i2c/smbus-protocol |    8 ++++++++
 include/linux/i2c.h              |   17 +++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)

-- 
1.7.3.4

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found]         ` <20111009094118.44b616bf-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
  2011-10-10  9:07           ` [PATCH V2] " Jonathan Cameron
@ 2011-10-10  9:07           ` Jonathan Cameron
       [not found]             ` <1318237663-13937-2-git-send-email-jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
  1 sibling, 1 reply; 11+ messages in thread
From: Jonathan Cameron @ 2011-10-10  9:07 UTC (permalink / raw)
  To: linux-i2c-u79uwXL29TY76Z2rM5mHXA
  Cc: khali-PUYAD+kWke1g9hUCZPvPmw, ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA, Jonathan Cameron

Reimplemented at least 17 times discounting error mangling cases
where it could be used.

Signed-off-by: Jonathan Cameron <jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
---
 Documentation/i2c/smbus-protocol |    8 ++++++++
 include/linux/i2c.h              |   17 +++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/Documentation/i2c/smbus-protocol b/Documentation/i2c/smbus-protocol
index 7c19d1a..5003539 100644
--- a/Documentation/i2c/smbus-protocol
+++ b/Documentation/i2c/smbus-protocol
@@ -88,6 +88,10 @@ byte. But this time, the data is a complete word (16 bits).
 
 S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
 
+Note the convenience function i2c_smbus_read_word_swapped is
+available for reads where the two data bytes are the other way
+around. (not smbus compliant)
+
 
 SMBus Write Byte:  i2c_smbus_write_byte_data()
 ==============================================
@@ -108,6 +112,10 @@ specified through the Comm byte.
 
 S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A] P
 
+Note the convenience function i2c_smbus_write_word_swapped is
+available for writes where the two data bytes are the other way
+around. (not smbus compliant)
+
 
 SMBus Process Call:  i2c_smbus_process_call()
 =============================================
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index a6c652e..38a21c3 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -34,6 +34,7 @@
 #include <linux/sched.h>	/* for completion */
 #include <linux/mutex.h>
 #include <linux/of.h>		/* for struct device_node */
+#include <linux/swab.h>		/* for swab16 */
 
 extern struct bus_type i2c_bus_type;
 extern struct device_type i2c_adapter_type;
@@ -88,6 +89,22 @@ extern s32 i2c_smbus_read_word_data(const struct i2c_client *client,
 				    u8 command);
 extern s32 i2c_smbus_write_word_data(const struct i2c_client *client,
 				     u8 command, u16 value);
+
+static inline s32
+i2c_smbus_read_word_swapped(const struct i2c_client *client, u8 command)
+{
+	s32 value = i2c_smbus_read_word_data(client, command);
+
+	return (value < 0) ? value : swab16(value);
+}
+
+static inline s32
+i2c_smbus_write_word_swapped(const struct i2c_client *client,
+			     u8 command, u16 value)
+{
+	return i2c_smbus_write_word_data(client, command, swab16(value));
+}
+
 /* Returns the number of read bytes */
 extern s32 i2c_smbus_read_block_data(const struct i2c_client *client,
 				     u8 command, u8 *values);
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found]             ` <1318237663-13937-2-git-send-email-jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
@ 2011-10-10 11:50               ` Jean Delvare
       [not found]                 ` <20111010135014.72598737-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Jean Delvare @ 2011-10-10 11:50 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA

On Mon, 10 Oct 2011 10:07:43 +0100, Jonathan Cameron wrote:
> Reimplemented at least 17 times discounting error mangling cases
> where it could be used.
> 
> Signed-off-by: Jonathan Cameron <jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
> ---
>  Documentation/i2c/smbus-protocol |    8 ++++++++
>  include/linux/i2c.h              |   17 +++++++++++++++++
>  2 files changed, 25 insertions(+), 0 deletions(-)

Applied, thanks.

-- 
Jean Delvare

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found]                 ` <20111010135014.72598737-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
@ 2011-10-11 11:49                   ` Jean Delvare
       [not found]                     ` <20111011134906.60a8c284-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Jean Delvare @ 2011-10-11 11:49 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA

On Mon, 10 Oct 2011 13:50:14 +0200, Jean Delvare wrote:
> On Mon, 10 Oct 2011 10:07:43 +0100, Jonathan Cameron wrote:
> > Reimplemented at least 17 times discounting error mangling cases
> > where it could be used.
> > 
> > Signed-off-by: Jonathan Cameron <jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
> > ---
> >  Documentation/i2c/smbus-protocol |    8 ++++++++
> >  include/linux/i2c.h              |   17 +++++++++++++++++
> >  2 files changed, 25 insertions(+), 0 deletions(-)
> 
> Applied, thanks.

BTW... I have a patch ready converting all concerned hwmon drivers to
use the new functions. I would appreciate if you could take care of all
other drivers.

Thanks,
-- 
Jean Delvare

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found]                     ` <20111011134906.60a8c284-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
@ 2011-10-11 15:21                       ` Jonathan Cameron
       [not found]                         ` <4E945F06.6050308-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Jonathan Cameron @ 2011-10-11 15:21 UTC (permalink / raw)
  To: Jean Delvare
  Cc: linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA

On 10/11/11 12:49, Jean Delvare wrote:
> On Mon, 10 Oct 2011 13:50:14 +0200, Jean Delvare wrote:
>> On Mon, 10 Oct 2011 10:07:43 +0100, Jonathan Cameron wrote:
>>> Reimplemented at least 17 times discounting error mangling cases
>>> where it could be used.
>>>
>>> Signed-off-by: Jonathan Cameron <jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
>>> ---
>>>  Documentation/i2c/smbus-protocol |    8 ++++++++
>>>  include/linux/i2c.h              |   17 +++++++++++++++++
>>>  2 files changed, 25 insertions(+), 0 deletions(-)
>>
>> Applied, thanks.
> 
> BTW... I have a patch ready converting all concerned hwmon drivers to
> use the new functions. I would appreciate if you could take care of all
> other drivers.
> 
> Thanks,
Will do. Have IIO patch queued locally.  Will get the others covered shortly.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data
       [not found]                         ` <4E945F06.6050308-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
@ 2011-10-21 10:16                           ` Jonathan Cameron
  0 siblings, 0 replies; 11+ messages in thread
From: Jonathan Cameron @ 2011-10-21 10:16 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Jean Delvare,
	public-linux-i2c-u79uwXL29TY76Z2rM5mHXA-wOFGN7rlS/M9smdsby/KFg,
	public-ben-linux-elnMNo+KYs3YtjvyW6yDsg-wOFGN7rlS/M9smdsby/KFg,
	public-Michael.Hennerich-OyLXuOCK7orQT0dZR+AlfA-wOFGN7rlS/M9smdsby/KFg



On 10/11/11 16:21, Jonathan Cameron wrote:
> On 10/11/11 12:49, Jean Delvare wrote:
>> On Mon, 10 Oct 2011 13:50:14 +0200, Jean Delvare wrote:
>>> On Mon, 10 Oct 2011 10:07:43 +0100, Jonathan Cameron wrote:
>>>> Reimplemented at least 17 times discounting error mangling cases
>>>> where it could be used.
>>>>
>>>> Signed-off-by: Jonathan Cameron <jic23-KWPb1pKIrIJaa/9Udqfwiw-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org>
>>>> ---
>>>>  Documentation/i2c/smbus-protocol |    8 ++++++++
>>>>  include/linux/i2c.h              |   17 +++++++++++++++++
>>>>  2 files changed, 25 insertions(+), 0 deletions(-)
>>>
>>> Applied, thanks.
>>
>> BTW... I have a patch ready converting all concerned hwmon drivers to
>> use the new functions. I would appreciate if you could take care of all
>> other drivers.
>>
>> Thanks,
> Will do. Have IIO patch queued locally.  Will get the others covered shortly.
Jean,

Sorry forgot to cc you on the first few patches I sent out using this.
So for reference (and anyone else who cares).

IIO I'll deal with when it merges (patch has been posted for a couple of weeks).
v4l sent out (5 drivers)
input sent out patch (only one user)
power sent out patch (only one user)

All pretty trivial cases.

Jonathan

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2011-10-21 10:16 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-22 13:48 [PATCH] i2c: boilerplate function for byte swapped smbus_write/read_word_data Jonathan Cameron
     [not found] ` <1316699294-6936-1-git-send-email-jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
2011-10-04 16:37   ` Jonathan Cameron
     [not found]     ` <4E8B3637.1030704-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
2011-10-04 17:05       ` Jean Delvare
2011-10-08 21:10   ` Jean Delvare
     [not found]     ` <20111008231005.41161836-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2011-10-09  7:41       ` Jean Delvare
     [not found]         ` <20111009094118.44b616bf-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2011-10-10  9:07           ` [PATCH V2] " Jonathan Cameron
2011-10-10  9:07           ` [PATCH] " Jonathan Cameron
     [not found]             ` <1318237663-13937-2-git-send-email-jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
2011-10-10 11:50               ` Jean Delvare
     [not found]                 ` <20111010135014.72598737-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2011-10-11 11:49                   ` Jean Delvare
     [not found]                     ` <20111011134906.60a8c284-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2011-10-11 15:21                       ` Jonathan Cameron
     [not found]                         ` <4E945F06.6050308-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org>
2011-10-21 10:16                           ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).