Skip to content

Commit 06a6784

Browse files
Jonathan CameronJean Delvare
Jonathan Cameron
authored and
Jean Delvare
committed
i2c: Functions for byte-swapped smbus_write/read_word_data
Reimplemented at least 17 times discounting error mangling cases where it could be used. Signed-off-by: Jonathan Cameron <[email protected]> Signed-off-by: Jean Delvare <[email protected]>
1 parent 4403988 commit 06a6784

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Documentation/i2c/smbus-protocol

+8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ byte. But this time, the data is a complete word (16 bits).
8888

8989
S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
9090

91+
Note the convenience function i2c_smbus_read_word_swapped is
92+
available for reads where the two data bytes are the other way
93+
around (not SMBus compliant, but very popular.)
94+
9195

9296
SMBus Write Byte: i2c_smbus_write_byte_data()
9397
==============================================
@@ -108,6 +112,10 @@ specified through the Comm byte.
108112

109113
S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A] P
110114

115+
Note the convenience function i2c_smbus_write_word_swapped is
116+
available for writes where the two data bytes are the other way
117+
around (not SMBus compliant, but very popular.)
118+
111119

112120
SMBus Process Call: i2c_smbus_process_call()
113121
=============================================

include/linux/i2c.h

+17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/sched.h> /* for completion */
3535
#include <linux/mutex.h>
3636
#include <linux/of.h> /* for struct device_node */
37+
#include <linux/swab.h> /* for swab16 */
3738

3839
extern struct bus_type i2c_bus_type;
3940
extern struct device_type i2c_adapter_type;
@@ -88,6 +89,22 @@ extern s32 i2c_smbus_read_word_data(const struct i2c_client *client,
8889
u8 command);
8990
extern s32 i2c_smbus_write_word_data(const struct i2c_client *client,
9091
u8 command, u16 value);
92+
93+
static inline s32
94+
i2c_smbus_read_word_swapped(const struct i2c_client *client, u8 command)
95+
{
96+
s32 value = i2c_smbus_read_word_data(client, command);
97+
98+
return (value < 0) ? value : swab16(value);
99+
}
100+
101+
static inline s32
102+
i2c_smbus_write_word_swapped(const struct i2c_client *client,
103+
u8 command, u16 value)
104+
{
105+
return i2c_smbus_write_word_data(client, command, swab16(value));
106+
}
107+
91108
/* Returns the number of read bytes */
92109
extern s32 i2c_smbus_read_block_data(const struct i2c_client *client,
93110
u8 command, u8 *values);

0 commit comments

Comments
 (0)