Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90f4102c authored by Jean Delvare's avatar Jean Delvare Committed by Jean Delvare
Browse files

hwmon: Use i2c_smbus_{read,write}_word_swapped



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: default avatarJean Delvare <khali@linux-fr.org>
Acked-by: default avatarJonathan Cameron <jic23@cam.ac.uk>
Acked-by: default avatarGuenter Roeck <guenter.roeck@ericsson.com>
Cc: Dirk Eibach <eibach@gdsys.de>
Cc: "Mark M. Hoffman" <mhoffman@lightlink.com>
Cc: Guillaume Ligneul <guillaume.ligneul@gmail.com>
parent 371f2e08
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -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);
}

+9 −18
Original line number Diff line number Diff line
@@ -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);
@@ -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--) {
@@ -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;
@@ -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;
}
+4 −17
Original line number Diff line number Diff line
@@ -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)
{
@@ -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;
@@ -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;
@@ -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;
+3 −9
Original line number Diff line number Diff line
@@ -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 */
@@ -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;
@@ -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__);
+5 −5
Original line number Diff line number Diff line
@@ -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;
		}
	}
@@ -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;
		}
	}
Loading