Commit e6a18823 authored by Jean Delvare's avatar Jean Delvare Committed by Patrick Mochel

[PATCH] I2C: Refactor swap_bytes in i2c chip drivers

> Ick, no, we should be using the proper kernel call for this, swab16(),
> right?  Care to redo this patch to just fix the drivers and get rid of
> our duplicating of this function.

Oh, I didn't know such a function existed, sorry.

Here's a new patch, hope you like it. Tested to work on my as99127f, btw
(w83781d driver).

Documentation update follows (well, tomorrow it does).
parent 11eff1d6
...@@ -862,11 +862,6 @@ static int asb100_detach_client(struct i2c_client *client) ...@@ -862,11 +862,6 @@ static int asb100_detach_client(struct i2c_client *client)
return 0; return 0;
} }
static u16 swap_bytes(u16 val)
{
return (val >> 8) | (val << 8);
}
/* The SMBus locks itself, usually, but nothing may access the chip between /* The SMBus locks itself, usually, but nothing may access the chip between
bank switches. */ bank switches. */
static int asb100_read_value(struct i2c_client *client, u16 reg) static int asb100_read_value(struct i2c_client *client, u16 reg)
...@@ -891,17 +886,17 @@ static int asb100_read_value(struct i2c_client *client, u16 reg) ...@@ -891,17 +886,17 @@ static int asb100_read_value(struct i2c_client *client, u16 reg)
/* convert from ISA to LM75 I2C addresses */ /* convert from ISA to LM75 I2C addresses */
switch (reg & 0xff) { switch (reg & 0xff) {
case 0x50: /* TEMP */ case 0x50: /* TEMP */
res = swap_bytes(i2c_smbus_read_word_data (cl, 0)); res = swab16(i2c_smbus_read_word_data (cl, 0));
break; break;
case 0x52: /* CONFIG */ case 0x52: /* CONFIG */
res = i2c_smbus_read_byte_data(cl, 1); res = i2c_smbus_read_byte_data(cl, 1);
break; break;
case 0x53: /* HYST */ case 0x53: /* HYST */
res = swap_bytes(i2c_smbus_read_word_data (cl, 2)); res = swab16(i2c_smbus_read_word_data (cl, 2));
break; break;
case 0x55: /* MAX */ case 0x55: /* MAX */
default: default:
res = swap_bytes(i2c_smbus_read_word_data (cl, 3)); res = swab16(i2c_smbus_read_word_data (cl, 3));
break; break;
} }
} }
...@@ -939,10 +934,10 @@ static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value) ...@@ -939,10 +934,10 @@ static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
i2c_smbus_write_byte_data(cl, 1, value & 0xff); i2c_smbus_write_byte_data(cl, 1, value & 0xff);
break; break;
case 0x53: /* HYST */ case 0x53: /* HYST */
i2c_smbus_write_word_data(cl, 2, swap_bytes(value)); i2c_smbus_write_word_data(cl, 2, swab16(value));
break; break;
case 0x55: /* MAX */ case 0x55: /* MAX */
i2c_smbus_write_word_data(cl, 3, swap_bytes(value)); i2c_smbus_write_word_data(cl, 3, swab16(value));
break; break;
} }
} }
......
...@@ -97,11 +97,6 @@ static struct i2c_driver ds1621_driver = { ...@@ -97,11 +97,6 @@ static struct i2c_driver ds1621_driver = {
static int ds1621_id = 0; static int ds1621_id = 0;
static u16 swap_bytes(u16 val)
{
return (val >> 8) | (val << 8);
}
/* All registers are word-sized, except for the configuration register. /* All registers are word-sized, except for the configuration register.
DS1621 uses a high-byte first convention, which is exactly opposite to DS1621 uses a high-byte first convention, which is exactly opposite to
the usual practice. */ the usual practice. */
...@@ -110,7 +105,7 @@ static int ds1621_read_value(struct i2c_client *client, u8 reg) ...@@ -110,7 +105,7 @@ static int ds1621_read_value(struct i2c_client *client, u8 reg)
if (reg == DS1621_REG_CONF) if (reg == DS1621_REG_CONF)
return i2c_smbus_read_byte_data(client, reg); return i2c_smbus_read_byte_data(client, reg);
else else
return swap_bytes(i2c_smbus_read_word_data(client, reg)); return swab16(i2c_smbus_read_word_data(client, reg));
} }
/* All registers are word-sized, except for the configuration register. /* All registers are word-sized, except for the configuration register.
...@@ -121,8 +116,7 @@ static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value) ...@@ -121,8 +116,7 @@ static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
if (reg == DS1621_REG_CONF) if (reg == DS1621_REG_CONF)
return i2c_smbus_write_byte_data(client, reg, value); return i2c_smbus_write_byte_data(client, reg, value);
else else
return i2c_smbus_write_word_data(client, reg, return i2c_smbus_write_word_data(client, reg, swab16(value));
swap_bytes(value));
} }
static void ds1621_init_client(struct i2c_client *client) static void ds1621_init_client(struct i2c_client *client)
......
...@@ -484,18 +484,13 @@ static int gl518_detach_client(struct i2c_client *client) ...@@ -484,18 +484,13 @@ static int gl518_detach_client(struct i2c_client *client)
return 0; return 0;
} }
static inline u16 swap_bytes(u16 val)
{
return (val >> 8) | (val << 8);
}
/* Registers 0x07 to 0x0c are word-sized, others are byte-sized /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
GL518 uses a high-byte first convention, which is exactly opposite to GL518 uses a high-byte first convention, which is exactly opposite to
the usual practice. */ the usual practice. */
static int gl518_read_value(struct i2c_client *client, u8 reg) static int gl518_read_value(struct i2c_client *client, u8 reg)
{ {
if ((reg >= 0x07) && (reg <= 0x0c)) if ((reg >= 0x07) && (reg <= 0x0c))
return swap_bytes(i2c_smbus_read_word_data(client, reg)); return swab16(i2c_smbus_read_word_data(client, reg));
else else
return i2c_smbus_read_byte_data(client, reg); return i2c_smbus_read_byte_data(client, reg);
} }
...@@ -506,8 +501,7 @@ static int gl518_read_value(struct i2c_client *client, u8 reg) ...@@ -506,8 +501,7 @@ static int gl518_read_value(struct i2c_client *client, u8 reg)
static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value) static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
{ {
if ((reg >= 0x07) && (reg <= 0x0c)) if ((reg >= 0x07) && (reg <= 0x0c))
return i2c_smbus_write_word_data(client, reg, return i2c_smbus_write_word_data(client, reg, swab16(value));
swap_bytes(value));
else else
return i2c_smbus_write_byte_data(client, reg, value); return i2c_smbus_write_byte_data(client, reg, value);
} }
......
...@@ -206,11 +206,6 @@ static int lm75_detach_client(struct i2c_client *client) ...@@ -206,11 +206,6 @@ static int lm75_detach_client(struct i2c_client *client)
return 0; return 0;
} }
static u16 swap_bytes(u16 val)
{
return (val >> 8) | (val << 8);
}
/* All registers are word-sized, except for the configuration register. /* All registers are word-sized, except for the configuration register.
LM75 uses a high-byte first convention, which is exactly opposite to LM75 uses a high-byte first convention, which is exactly opposite to
the usual practice. */ the usual practice. */
...@@ -219,7 +214,7 @@ static int lm75_read_value(struct i2c_client *client, u8 reg) ...@@ -219,7 +214,7 @@ static int lm75_read_value(struct i2c_client *client, u8 reg)
if (reg == LM75_REG_CONF) if (reg == LM75_REG_CONF)
return i2c_smbus_read_byte_data(client, reg); return i2c_smbus_read_byte_data(client, reg);
else else
return swap_bytes(i2c_smbus_read_word_data(client, reg)); return swab16(i2c_smbus_read_word_data(client, reg));
} }
/* All registers are word-sized, except for the configuration register. /* All registers are word-sized, except for the configuration register.
...@@ -230,8 +225,7 @@ static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value) ...@@ -230,8 +225,7 @@ static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
if (reg == LM75_REG_CONF) if (reg == LM75_REG_CONF)
return i2c_smbus_write_byte_data(client, reg, value); return i2c_smbus_write_byte_data(client, reg, value);
else else
return i2c_smbus_write_word_data(client, reg, return i2c_smbus_write_word_data(client, reg, swab16(value));
swap_bytes(value));
} }
static void lm75_init_client(struct i2c_client *client) static void lm75_init_client(struct i2c_client *client)
......
...@@ -275,11 +275,6 @@ static int w83781d_write_value(struct i2c_client *client, u16 register, ...@@ -275,11 +275,6 @@ static int w83781d_write_value(struct i2c_client *client, u16 register,
static struct w83781d_data *w83781d_update_device(struct device *dev); static struct w83781d_data *w83781d_update_device(struct device *dev);
static void w83781d_init_client(struct i2c_client *client); static void w83781d_init_client(struct i2c_client *client);
static inline u16 swap_bytes(u16 val)
{
return (val >> 8) | (val << 8);
}
static struct i2c_driver w83781d_driver = { static struct i2c_driver w83781d_driver = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.name = "w83781d", .name = "w83781d",
...@@ -1407,20 +1402,17 @@ w83781d_read_value(struct i2c_client *client, u16 reg) ...@@ -1407,20 +1402,17 @@ w83781d_read_value(struct i2c_client *client, u16 reg)
/* convert from ISA to LM75 I2C addresses */ /* convert from ISA to LM75 I2C addresses */
switch (reg & 0xff) { switch (reg & 0xff) {
case 0x50: /* TEMP */ case 0x50: /* TEMP */
res = res = swab16(i2c_smbus_read_word_data(cl, 0));
swap_bytes(i2c_smbus_read_word_data(cl, 0));
break; break;
case 0x52: /* CONFIG */ case 0x52: /* CONFIG */
res = i2c_smbus_read_byte_data(cl, 1); res = i2c_smbus_read_byte_data(cl, 1);
break; break;
case 0x53: /* HYST */ case 0x53: /* HYST */
res = res = swab16(i2c_smbus_read_word_data(cl, 2));
swap_bytes(i2c_smbus_read_word_data(cl, 2));
break; break;
case 0x55: /* OVER */ case 0x55: /* OVER */
default: default:
res = res = swab16(i2c_smbus_read_word_data(cl, 3));
swap_bytes(i2c_smbus_read_word_data(cl, 3));
break; break;
} }
} }
...@@ -1481,12 +1473,10 @@ w83781d_write_value(struct i2c_client *client, u16 reg, u16 value) ...@@ -1481,12 +1473,10 @@ w83781d_write_value(struct i2c_client *client, u16 reg, u16 value)
i2c_smbus_write_byte_data(cl, 1, value & 0xff); i2c_smbus_write_byte_data(cl, 1, value & 0xff);
break; break;
case 0x53: /* HYST */ case 0x53: /* HYST */
i2c_smbus_write_word_data(cl, 2, i2c_smbus_write_word_data(cl, 2, swab16(value));
swap_bytes(value));
break; break;
case 0x55: /* OVER */ case 0x55: /* OVER */
i2c_smbus_write_word_data(cl, 3, i2c_smbus_write_word_data(cl, 3, swab16(value));
swap_bytes(value));
break; break;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment