From patchwork Mon Jan 9 16:47:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: jeroen.de_wachter.ext@nokia.com X-Patchwork-Id: 9505555 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2CD0A60757 for ; Mon, 9 Jan 2017 17:00:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 27DC328485 for ; Mon, 9 Jan 2017 17:00:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1A3A32849D; Mon, 9 Jan 2017 17:00:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A37FC28485 for ; Mon, 9 Jan 2017 17:00:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1762778AbdAIRAD (ORCPT ); Mon, 9 Jan 2017 12:00:03 -0500 Received: from fr-hpida-esg-02.alcatel-lucent.com ([135.245.210.21]:39524 "EHLO smtp-fr.alcatel-lucent.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1762662AbdAIQ7m (ORCPT ); Mon, 9 Jan 2017 11:59:42 -0500 X-Greylist: delayed 652 seconds by postgrey-1.27 at vger.kernel.org; Mon, 09 Jan 2017 11:59:41 EST Received: from fr712umx4.dmz.alcatel-lucent.com (unknown [135.245.210.45]) by Websense Email Security Gateway with ESMTPS id C95654359703D; Mon, 9 Jan 2017 16:48:41 +0000 (GMT) Received: from fr712usmtp2.zeu.alcatel-lucent.com (fr712usmtp2.zeu.alcatel-lucent.com [135.239.2.42]) by fr712umx4.dmz.alcatel-lucent.com (GMO-o) with ESMTP id v09GmfQD018143 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 9 Jan 2017 16:48:44 GMT Received: from FR712WXCHHUB03.zeu.alcatel-lucent.com (fr712wxchhub03.zeu.alcatel-lucent.com [135.239.2.74]) by fr712usmtp2.zeu.alcatel-lucent.com (GMO) with ESMTP id v09GmPj8015331 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL); Mon, 9 Jan 2017 16:48:39 GMT Received: from devws062.be.alcatel-lucent.com (135.239.27.41) by FR712WXCHHUB03.zeu.alcatel-lucent.com (135.239.2.74) with Microsoft SMTP Server (TLS) id 14.3.301.0; Mon, 9 Jan 2017 17:48:19 +0100 From: To: , CC: , , Jeroen De Wachter 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 Message-ID: <1483980458-28202-1-git-send-email-jeroen.de_wachter.ext@nokia.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [135.239.27.41] Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jeroen De Wachter 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 --- 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;