Skip to content

Commit

Permalink
hwmon: Use i2c_smbus_{read,write}_word_swapped
Browse files Browse the repository at this point in the history
Make use of the new i2c_smbus_{read,write}_word_swapped functions.
This makes the driver code more compact and readable. It also ensures
proper error handling.

Signed-off-by: Jean Delvare <[email protected]>
Acked-by: Jonathan Cameron <[email protected]>
Acked-by: Guenter Roeck <[email protected]>
Cc: Dirk Eibach <[email protected]>
Cc: "Mark M. Hoffman" <[email protected]>
Cc: Guillaume Ligneul <[email protected]>
  • Loading branch information
Jean Delvare authored and Jean Delvare committed Nov 4, 2011
1 parent 371f2e0 commit 90f4102
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 234 deletions.
7 changes: 3 additions & 4 deletions drivers/hwmon/ad7414.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ static inline int ad7414_temp_from_reg(s16 reg)

static inline int ad7414_read(struct i2c_client *client, u8 reg)
{
if (reg == AD7414_REG_TEMP) {
int value = i2c_smbus_read_word_data(client, reg);
return (value < 0) ? value : swab16(value);
} else
if (reg == AD7414_REG_TEMP)
return i2c_smbus_read_word_swapped(client, reg);
else
return i2c_smbus_read_byte_data(client, reg);
}

Expand Down
27 changes: 9 additions & 18 deletions drivers/hwmon/ad7418.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,6 @@ static struct i2c_driver ad7418_driver = {
.id_table = ad7418_id,
};

/* All registers are word-sized, except for the configuration registers.
* AD7418 uses a high-byte first convention. Do NOT use those functions to
* access the configuration registers CONF and CONF2, as they are byte-sized.
*/
static inline int ad7418_read(struct i2c_client *client, u8 reg)
{
return swab16(i2c_smbus_read_word_data(client, reg));
}

static inline int ad7418_write(struct i2c_client *client, u8 reg, u16 value)
{
return i2c_smbus_write_word_data(client, reg, swab16(value));
}

static void ad7418_init_client(struct i2c_client *client)
{
struct ad7418_data *data = i2c_get_clientdata(client);
Expand Down Expand Up @@ -128,7 +114,9 @@ static struct ad7418_data *ad7418_update_device(struct device *dev)
udelay(30);

for (i = 0; i < 3; i++) {
data->temp[i] = ad7418_read(client, AD7418_REG_TEMP[i]);
data->temp[i] =
i2c_smbus_read_word_swapped(client,
AD7418_REG_TEMP[i]);
}

for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
Expand All @@ -138,11 +126,12 @@ static struct ad7418_data *ad7418_update_device(struct device *dev)

udelay(15);
data->in[data->adc_max - 1 - i] =
ad7418_read(client, AD7418_REG_ADC);
i2c_smbus_read_word_swapped(client,
AD7418_REG_ADC);
}

/* restore old configuration value */
ad7418_write(client, AD7418_REG_CONF, cfg);
i2c_smbus_write_word_swapped(client, AD7418_REG_CONF, cfg);

data->last_updated = jiffies;
data->valid = 1;
Expand Down Expand Up @@ -182,7 +171,9 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,

mutex_lock(&data->lock);
data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
ad7418_write(client, AD7418_REG_TEMP[attr->index], data->temp[attr->index]);
i2c_smbus_write_word_swapped(client,
AD7418_REG_TEMP[attr->index],
data->temp[attr->index]);
mutex_unlock(&data->lock);
return count;
}
Expand Down
21 changes: 4 additions & 17 deletions drivers/hwmon/ads1015.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,6 @@ struct ads1015_data {
struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
};

static s32 ads1015_read_reg(struct i2c_client *client, unsigned int reg)
{
s32 data = i2c_smbus_read_word_data(client, reg);

return (data < 0) ? data : swab16(data);
}

static s32 ads1015_write_reg(struct i2c_client *client, unsigned int reg,
u16 val)
{
return i2c_smbus_write_word_data(client, reg, swab16(val));
}

static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
int *value)
{
Expand All @@ -87,7 +74,7 @@ static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
mutex_lock(&data->update_lock);

/* get channel parameters */
res = ads1015_read_reg(client, ADS1015_CONFIG);
res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
if (res < 0)
goto err_unlock;
config = res;
Expand All @@ -101,13 +88,13 @@ static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
config |= (pga & 0x0007) << 9;
config |= (data_rate & 0x0007) << 5;

res = ads1015_write_reg(client, ADS1015_CONFIG, config);
res = i2c_smbus_write_word_swapped(client, ADS1015_CONFIG, config);
if (res < 0)
goto err_unlock;

/* wait until conversion finished */
msleep(conversion_time_ms);
res = ads1015_read_reg(client, ADS1015_CONFIG);
res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
if (res < 0)
goto err_unlock;
config = res;
Expand All @@ -117,7 +104,7 @@ static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
goto err_unlock;
}

res = ads1015_read_reg(client, ADS1015_CONVERSION);
res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION);
if (res < 0)
goto err_unlock;
conversion = res;
Expand Down
12 changes: 3 additions & 9 deletions drivers/hwmon/ads7828.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ static int ads7828_detect(struct i2c_client *client,
static int ads7828_probe(struct i2c_client *client,
const struct i2c_device_id *id);

/* The ADS7828 returns the 12-bit sample in two bytes,
these are read as a word then byte-swapped */
static u16 ads7828_read_value(struct i2c_client *client, u8 reg)
{
return swab16(i2c_smbus_read_word_data(client, reg));
}

static inline u8 channel_cmd_byte(int ch)
{
/* cmd byte C2,C1,C0 - see datasheet */
Expand All @@ -104,7 +97,8 @@ static struct ads7828_data *ads7828_update_device(struct device *dev)

for (ch = 0; ch < ADS7828_NCH; ch++) {
u8 cmd = channel_cmd_byte(ch);
data->adc_input[ch] = ads7828_read_value(client, cmd);
data->adc_input[ch] =
i2c_smbus_read_word_swapped(client, cmd);
}
data->last_updated = jiffies;
data->valid = 1;
Expand Down Expand Up @@ -203,7 +197,7 @@ static int ads7828_detect(struct i2c_client *client,
for (ch = 0; ch < ADS7828_NCH; ch++) {
u16 in_data;
u8 cmd = channel_cmd_byte(ch);
in_data = ads7828_read_value(client, cmd);
in_data = i2c_smbus_read_word_swapped(client, cmd);
if (in_data & 0xF000) {
pr_debug("%s : Doesn't look like an ads7828 device\n",
__func__);
Expand Down
10 changes: 5 additions & 5 deletions drivers/hwmon/asb100.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,17 +829,17 @@ static int asb100_read_value(struct i2c_client *client, u16 reg)
/* convert from ISA to LM75 I2C addresses */
switch (reg & 0xff) {
case 0x50: /* TEMP */
res = swab16(i2c_smbus_read_word_data(cl, 0));
res = i2c_smbus_read_word_swapped(cl, 0);
break;
case 0x52: /* CONFIG */
res = i2c_smbus_read_byte_data(cl, 1);
break;
case 0x53: /* HYST */
res = swab16(i2c_smbus_read_word_data(cl, 2));
res = i2c_smbus_read_word_swapped(cl, 2);
break;
case 0x55: /* MAX */
default:
res = swab16(i2c_smbus_read_word_data(cl, 3));
res = i2c_smbus_read_word_swapped(cl, 3);
break;
}
}
Expand Down Expand Up @@ -877,10 +877,10 @@ static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
i2c_smbus_write_byte_data(cl, 1, value & 0xff);
break;
case 0x53: /* HYST */
i2c_smbus_write_word_data(cl, 2, swab16(value));
i2c_smbus_write_word_swapped(cl, 2, value);
break;
case 0x55: /* MAX */
i2c_smbus_write_word_data(cl, 3, swab16(value));
i2c_smbus_write_word_swapped(cl, 3, value);
break;
}
}
Expand Down
24 changes: 3 additions & 21 deletions drivers/hwmon/ds1621.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@ struct ds1621_data {
u8 conf; /* Register encoding, combined */
};

/* Temperature registers are word-sized.
DS1621 uses a high-byte first convention, which is exactly opposite to
the SMBus standard. */
static int ds1621_read_temp(struct i2c_client *client, u8 reg)
{
int ret;

ret = i2c_smbus_read_word_data(client, reg);
if (ret < 0)
return ret;
return swab16(ret);
}

static int ds1621_write_temp(struct i2c_client *client, u8 reg, u16 value)
{
return i2c_smbus_write_word_data(client, reg, swab16(value));
}

static void ds1621_init_client(struct i2c_client *client)
{
u8 conf, new_conf;
Expand Down Expand Up @@ -136,7 +118,7 @@ static struct ds1621_data *ds1621_update_client(struct device *dev)
data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);

for (i = 0; i < ARRAY_SIZE(data->temp); i++)
data->temp[i] = ds1621_read_temp(client,
data->temp[i] = i2c_smbus_read_word_swapped(client,
DS1621_REG_TEMP[i]);

/* reset alarms if necessary */
Expand Down Expand Up @@ -177,8 +159,8 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da,

mutex_lock(&data->update_lock);
data->temp[attr->index] = val;
ds1621_write_temp(client, DS1621_REG_TEMP[attr->index],
data->temp[attr->index]);
i2c_smbus_write_word_swapped(client, DS1621_REG_TEMP[attr->index],
data->temp[attr->index]);
mutex_unlock(&data->update_lock);
return count;
}
Expand Down
42 changes: 10 additions & 32 deletions drivers/hwmon/ds620.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,13 @@ struct ds620_data {
s16 temp[3]; /* Register values, word */
};

/*
* Temperature registers are word-sized.
* DS620 uses a high-byte first convention, which is exactly opposite to
* the SMBus standard.
*/
static int ds620_read_temp(struct i2c_client *client, u8 reg)
{
int ret;

ret = i2c_smbus_read_word_data(client, reg);
if (ret < 0)
return ret;
return swab16(ret);
}

static int ds620_write_temp(struct i2c_client *client, u8 reg, u16 value)
{
return i2c_smbus_write_word_data(client, reg, swab16(value));
}

static void ds620_init_client(struct i2c_client *client)
{
struct ds620_platform_data *ds620_info = client->dev.platform_data;
u16 conf, new_conf;

new_conf = conf =
swab16(i2c_smbus_read_word_data(client, DS620_REG_CONF));
i2c_smbus_read_word_swapped(client, DS620_REG_CONF);

/* switch to continuous conversion mode */
new_conf &= ~DS620_REG_CONFIG_1SHOT;
Expand All @@ -118,8 +98,7 @@ static void ds620_init_client(struct i2c_client *client)
new_conf |= DS620_REG_CONFIG_R1 | DS620_REG_CONFIG_R0;

if (conf != new_conf)
i2c_smbus_write_word_data(client, DS620_REG_CONF,
swab16(new_conf));
i2c_smbus_write_word_swapped(client, DS620_REG_CONF, new_conf);

/* start conversion */
i2c_smbus_write_byte(client, DS620_COM_START);
Expand All @@ -141,8 +120,8 @@ static struct ds620_data *ds620_update_client(struct device *dev)
dev_dbg(&client->dev, "Starting ds620 update\n");

for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
res = ds620_read_temp(client,
DS620_REG_TEMP[i]);
res = i2c_smbus_read_word_swapped(client,
DS620_REG_TEMP[i]);
if (res < 0) {
ret = ERR_PTR(res);
goto abort;
Expand Down Expand Up @@ -191,8 +170,8 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da,

mutex_lock(&data->update_lock);
data->temp[attr->index] = val;
ds620_write_temp(client, DS620_REG_TEMP[attr->index],
data->temp[attr->index]);
i2c_smbus_write_word_swapped(client, DS620_REG_TEMP[attr->index],
data->temp[attr->index]);
mutex_unlock(&data->update_lock);
return count;
}
Expand All @@ -210,16 +189,15 @@ static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
return PTR_ERR(data);

/* reset alarms if necessary */
res = i2c_smbus_read_word_data(client, DS620_REG_CONF);
res = i2c_smbus_read_word_swapped(client, DS620_REG_CONF);
if (res < 0)
return res;

conf = swab16(res);
new_conf = conf;
new_conf = conf = res;
new_conf &= ~attr->index;
if (conf != new_conf) {
res = i2c_smbus_write_word_data(client, DS620_REG_CONF,
swab16(new_conf));
res = i2c_smbus_write_word_swapped(client, DS620_REG_CONF,
new_conf);
if (res < 0)
return res;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/hwmon/gl518sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,15 @@ static int gl518_remove(struct i2c_client *client)
static int gl518_read_value(struct i2c_client *client, u8 reg)
{
if ((reg >= 0x07) && (reg <= 0x0c))
return swab16(i2c_smbus_read_word_data(client, reg));
return i2c_smbus_read_word_swapped(client, reg);
else
return i2c_smbus_read_byte_data(client, reg);
}

static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
{
if ((reg >= 0x07) && (reg <= 0x0c))
return i2c_smbus_write_word_data(client, reg, swab16(value));
return i2c_smbus_write_word_swapped(client, reg, value);
else
return i2c_smbus_write_byte_data(client, reg, value);
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/hwmon/gl520sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,15 +821,15 @@ static int gl520_remove(struct i2c_client *client)
static int gl520_read_value(struct i2c_client *client, u8 reg)
{
if ((reg >= 0x07) && (reg <= 0x0c))
return swab16(i2c_smbus_read_word_data(client, reg));
return i2c_smbus_read_word_swapped(client, reg);
else
return i2c_smbus_read_byte_data(client, reg);
}

static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
{
if ((reg >= 0x07) && (reg <= 0x0c))
return i2c_smbus_write_word_data(client, reg, swab16(value));
return i2c_smbus_write_word_swapped(client, reg, value);
else
return i2c_smbus_write_byte_data(client, reg, value);
}
Expand Down
Loading

0 comments on commit 90f4102

Please sign in to comment.