History

hwmon: (ina2xx) Fix word size register read and write operations

The driver uses be16_to_cpu and cpu_to_be16 to convert data in SMBus word operations from chip to host byte order. However, the data passed from and to the SMBus word API functions is in host byte order, not in chip byte order. Conversion should therefore use swab16 instead of be16 to change the byte order. Replace driver internal word conversion functions with SMBus API functions to solve the problem.

Guenter Roeck committed 14 years ago

Commit 080b98e1 parent 73d7c111 file · +9 −21Browse files

Unified · Split

drivers/hwmon/ina2xx.c+9−21
@@ -69,22 +69,6 @@ struct ina2xx_data { · 68 unmodified lines
69 u16 regs[INA2XX_MAX_REGISTERS];69 u16 regs[INA2XX_MAX_REGISTERS];
70};70};
71 71
72int ina2xx_read_word(struct i2c_client *client, int reg)
73{
74 int val = i2c_smbus_read_word_data(client, reg);
75 if (unlikely(val < 0)) {
76 dev_dbg(&client->dev,
77 "Failed to read register: %d\n", reg);
78 return val;
79 }
80 return be16_to_cpu(val);
81}
82
83void ina2xx_write_word(struct i2c_client *client, int reg, int data)
84{
85 i2c_smbus_write_word_data(client, reg, cpu_to_be16(data));
86}
87
88static struct ina2xx_data *ina2xx_update_device(struct device *dev)72static struct ina2xx_data *ina2xx_update_device(struct device *dev)
89{73{
90 struct i2c_client *client = to_i2c_client(dev);74 struct i2c_client *client = to_i2c_client(dev);
@@ -102,7 +86,7 @@ static struct ina2xx_data *ina2xx_update_device(struct device *dev) · 11 unmodified lines
102 86
103 /* Read all registers */87 /* Read all registers */
104 for (i = 0; i < data->registers; i++) {88 for (i = 0; i < data->registers; i++) {
105 int rv = ina2xx_read_word(client, i);89 int rv = i2c_smbus_read_word_swapped(client, i);
106 if (rv < 0) {90 if (rv < 0) {
107 ret = ERR_PTR(rv);91 ret = ERR_PTR(rv);
108 goto abort;92 goto abort;
@@ -279,22 +263,26 @@ static int ina2xx_probe(struct i2c_client *client, · 170 unmodified lines
279 switch (data->kind) {263 switch (data->kind) {
280 case ina219:264 case ina219:
281 /* device configuration */265 /* device configuration */
282 ina2xx_write_word(client, INA2XX_CONFIG, INA219_CONFIG_DEFAULT);266 i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
267 INA219_CONFIG_DEFAULT);
283 268
284 /* set current LSB to 1mA, shunt is in uOhms */269 /* set current LSB to 1mA, shunt is in uOhms */
285 /* (equation 13 in datasheet) */270 /* (equation 13 in datasheet) */
286 ina2xx_write_word(client, INA2XX_CALIBRATION, 40960000 / shunt);271 i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
272 40960000 / shunt);
287 dev_info(&client->dev,273 dev_info(&client->dev,
288 "power monitor INA219 (Rshunt = %li uOhm)\n", shunt);274 "power monitor INA219 (Rshunt = %li uOhm)\n", shunt);
289 data->registers = INA219_REGISTERS;275 data->registers = INA219_REGISTERS;
290 break;276 break;
291 case ina226:277 case ina226:
292 /* device configuration */278 /* device configuration */
293 ina2xx_write_word(client, INA2XX_CONFIG, INA226_CONFIG_DEFAULT);279 i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
280 INA226_CONFIG_DEFAULT);
294 281
295 /* set current LSB to 1mA, shunt is in uOhms */282 /* set current LSB to 1mA, shunt is in uOhms */
296 /* (equation 1 in datasheet)*/283 /* (equation 1 in datasheet)*/
297 ina2xx_write_word(client, INA2XX_CALIBRATION, 5120000 / shunt);284 i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
285 5120000 / shunt);
298 dev_info(&client->dev,286 dev_info(&client->dev,
299 "power monitor INA226 (Rshunt = %li uOhm)\n", shunt);287 "power monitor INA226 (Rshunt = %li uOhm)\n", shunt);
300 data->registers = INA226_REGISTERS;288 data->registers = INA226_REGISTERS;