Skip to content

Commit 6337de2

Browse files
mhennerichdtor
authored andcommitted
Input: ad714x - fix endianness issues
Allow driver to be used on Big Endian boxes. Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 5b9063b commit 6337de2

2 files changed

Lines changed: 25 additions & 33 deletions

File tree

‎drivers/input/misc/ad714x-i2c.c‎

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,12 @@ static int ad714x_i2c_write(struct device *dev, unsigned short reg,
3232
{
3333
struct i2c_client *client = to_i2c_client(dev);
3434
int ret = 0;
35-
u8 *_reg = (u8 *)&reg;
36-
u8 *_data = (u8 *)&data;
37-
38-
u8 tx[4] = {
39-
_reg[1],
40-
_reg[0],
41-
_data[1],
42-
_data[0]
35+
unsigned short tx[2] = {
36+
cpu_to_be16(reg),
37+
cpu_to_be16(data)
4338
};
4439

45-
ret = i2c_master_send(client, tx, 4);
40+
ret = i2c_master_send(client, (u8 *)tx, 4);
4641
if (ret < 0)
4742
dev_err(&client->dev, "I2C write error\n");
4843

@@ -54,25 +49,16 @@ static int ad714x_i2c_read(struct device *dev, unsigned short reg,
5449
{
5550
struct i2c_client *client = to_i2c_client(dev);
5651
int ret = 0;
57-
u8 *_reg = (u8 *)&reg;
58-
u8 *_data = (u8 *)data;
52+
unsigned short tx = cpu_to_be16(reg);
5953

60-
u8 tx[2] = {
61-
_reg[1],
62-
_reg[0]
63-
};
64-
u8 rx[2];
65-
66-
ret = i2c_master_send(client, tx, 2);
54+
ret = i2c_master_send(client, (u8 *)&tx, 2);
6755
if (ret >= 0)
68-
ret = i2c_master_recv(client, rx, 2);
56+
ret = i2c_master_recv(client, (u8 *)data, 2);
6957

70-
if (unlikely(ret < 0)) {
58+
if (unlikely(ret < 0))
7159
dev_err(&client->dev, "I2C read error\n");
72-
} else {
73-
_data[0] = rx[1];
74-
_data[1] = rx[0];
75-
}
60+
else
61+
*data = be16_to_cpu(*data);
7662

7763
return ret;
7864
}

‎drivers/input/misc/ad714x-spi.c‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Licensed under the GPL-2 or later.
77
*/
88

9-
#include <linux/input.h> /* BUS_I2C */
9+
#include <linux/input.h> /* BUS_SPI */
1010
#include <linux/module.h>
1111
#include <linux/spi/spi.h>
1212
#include <linux/pm.h>
@@ -30,22 +30,28 @@ static int ad714x_spi_resume(struct device *dev)
3030

3131
static SIMPLE_DEV_PM_OPS(ad714x_spi_pm, ad714x_spi_suspend, ad714x_spi_resume);
3232

33-
static int ad714x_spi_read(struct device *dev, unsigned short reg,
34-
unsigned short *data)
33+
static int ad714x_spi_read(struct device *dev,
34+
unsigned short reg, unsigned short *data)
3535
{
3636
struct spi_device *spi = to_spi_device(dev);
37-
unsigned short tx = AD714x_SPI_CMD_PREFIX | AD714x_SPI_READ | reg;
37+
unsigned short tx = cpu_to_be16(AD714x_SPI_CMD_PREFIX |
38+
AD714x_SPI_READ | reg);
39+
int ret;
3840

39-
return spi_write_then_read(spi, (u8 *)&tx, 2, (u8 *)data, 2);
41+
ret = spi_write_then_read(spi, &tx, 2, data, 2);
42+
43+
*data = be16_to_cpup(data);
44+
45+
return ret;
4046
}
4147

42-
static int ad714x_spi_write(struct device *dev, unsigned short reg,
43-
unsigned short data)
48+
static int ad714x_spi_write(struct device *dev,
49+
unsigned short reg, unsigned short data)
4450
{
4551
struct spi_device *spi = to_spi_device(dev);
4652
unsigned short tx[2] = {
47-
AD714x_SPI_CMD_PREFIX | reg,
48-
data
53+
cpu_to_be16(AD714x_SPI_CMD_PREFIX | reg),
54+
cpu_to_be16(data)
4955
};
5056

5157
return spi_write(spi, (u8 *)tx, 4);

0 commit comments

Comments
 (0)