From: <jeroen.de_wachter.ext@nokia.com>
To: <linux@roeck-us.net>, <jdelvare@suse.com>
Cc: <linux-hwmon@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
Jeroen De Wachter <jeroen.de_wachter.ext@nokia.com>
Subject: [PATCH 1/2] hwmon/tmp401: use smb word operations instead of 2 smb byte operations
Date: Mon, 9 Jan 2017 17:47:37 +0100 [thread overview]
Message-ID: <1483980458-28202-1-git-send-email-jeroen.de_wachter.ext@nokia.com> (raw)
From: Jeroen De Wachter <jeroen.de_wachter.ext@nokia.com>
tmp401 separately read/wrote high and low bytes of temperature values while
the hardware supports reading/writing those values in one operation. Driver
has been modified to use word operations where possible.
Tested with a tmp432 sensor on a mips64 platform.
Signed-off-by: Jeroen De Wachter <jeroen.de_wachter.ext@nokia.com>
---
drivers/hwmon/tmp401.c | 48 +++++++++++++++++++++++++++++-------------------
1 file changed, 29 insertions(+), 19 deletions(-)
diff --git a/drivers/hwmon/tmp401.c b/drivers/hwmon/tmp401.c
index eeeed2c..88b17e4 100644
--- a/drivers/hwmon/tmp401.c
+++ b/drivers/hwmon/tmp401.c
@@ -213,25 +213,29 @@ static int tmp401_update_device_reg16(struct i2c_client *client,
for (i = 0; i < num_sensors; i++) { /* local / r1 / r2 */
for (j = 0; j < num_regs; j++) { /* temp / low / ... */
u8 regaddr;
- /*
- * High byte must be read first immediately followed
- * by the low byte
- */
+
regaddr = data->kind == tmp432 ?
TMP432_TEMP_MSB_READ[j][i] :
TMP401_TEMP_MSB_READ[j][i];
- val = i2c_smbus_read_byte_data(client, regaddr);
- if (val < 0)
- return val;
- data->temp[j][i] = val << 8;
- if (j == 3) /* crit is msb only */
- continue;
- regaddr = data->kind == tmp432 ? TMP432_TEMP_LSB[j][i]
- : TMP401_TEMP_LSB[j][i];
- val = i2c_smbus_read_byte_data(client, regaddr);
+ if (j == 3) { /* crit is msb only */
+ val = i2c_smbus_read_byte_data(client, regaddr);
+ } else {
+ /*
+ * Hardware provides big endian data. However,
+ * the smbus protocol expects the LSB first in
+ * I2C_SMBUS_WORD_DATA operations.
+ * i2c_smbus_read_word_swapped swaps the bytes
+ * in the value it receives from
+ * i2c_smbus_read_word_data before returning the
+ * data, so we get the correct value.
+ */
+ val = i2c_smbus_read_word_swapped(client,
+ regaddr);
+ }
if (val < 0)
return val;
- data->temp[j][i] |= val;
+
+ data->temp[j][i] = j == 3 ? val << 8 : val;
}
}
return 0;
@@ -373,11 +377,17 @@ static ssize_t store_temp(struct device *dev, struct device_attribute *devattr,
regaddr = data->kind == tmp432 ? TMP432_TEMP_MSB_WRITE[nr][index]
: TMP401_TEMP_MSB_WRITE[nr][index];
- i2c_smbus_write_byte_data(client, regaddr, reg >> 8);
- if (nr != 3) {
- regaddr = data->kind == tmp432 ? TMP432_TEMP_LSB[nr][index]
- : TMP401_TEMP_LSB[nr][index];
- i2c_smbus_write_byte_data(client, regaddr, reg & 0xFF);
+ if (nr == 3) { /* crit is msb only */
+ i2c_smbus_write_byte_data(client, regaddr, reg >> 8);
+ } else {
+ /*
+ * Hardware expects big endian data. However, the smbus protocol
+ * puts the LSB first in I2C_SMBUS_WORD_DATA operations.
+ * i2c_smbus_write_word_data_swapped will swap the bytes before
+ * passing them to i2c_smbus_write_word_data, so the hardware
+ * will receive the MSB first, as expected.
+ */
+ i2c_smbus_write_word_swapped(client, regaddr, reg);
}
data->temp[nr][index] = reg;
--
1.8.3.1
next reply other threads:[~2017-01-09 17:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-09 16:47 jeroen.de_wachter.ext [this message]
2017-01-09 16:47 ` [PATCH 2/2] hwmon/tmp401: Fix some checkstyle warnings jeroen.de_wachter.ext
2017-01-09 17:11 ` Guenter Roeck
2017-01-09 17:09 ` [PATCH 1/2] hwmon/tmp401: use smb word operations instead of 2 smb byte operations Guenter Roeck
2017-01-09 17:55 ` [PATCH v2] " jeroen.de_wachter.ext
2017-01-10 17:03 ` [v2] " Guenter Roeck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1483980458-28202-1-git-send-email-jeroen.de_wachter.ext@nokia.com \
--to=jeroen.de_wachter.ext@nokia.com \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).