From patchwork Tue Oct 4 04:08:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Packham X-Patchwork-Id: 9361103 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 A266860752 for ; Tue, 4 Oct 2016 04:08:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 939B32879B for ; Tue, 4 Oct 2016 04:08:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 873F228AF3; Tue, 4 Oct 2016 04:08:29 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 4E4B72879B for ; Tue, 4 Oct 2016 04:08:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751241AbcJDEI2 (ORCPT ); Tue, 4 Oct 2016 00:08:28 -0400 Received: from gate2.alliedtelesis.co.nz ([202.36.163.20]:41919 "EHLO gate2.alliedtelesis.co.nz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750780AbcJDEI1 (ORCPT ); Tue, 4 Oct 2016 00:08:27 -0400 Received: from mmarshal3.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id 51A7D806AC; Tue, 4 Oct 2016 17:08:24 +1300 (NZDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail; t=1475554104; bh=ZbbSuCQAoEwMmqLUDEuZfHOLZqmzrHnAm7Vbj7VLwuY=; h=From:To:Cc:Subject:Date; b=MtdpGh0syAhyMYddFBzDu8rMk0/rwxywamDrYu3POBLlQ5WR1IXrRxjjOzwVqx0Sv psB/Ci6pXhmruw3kfvS1NR1xFtQSFKCDGxPBa+yRi1D101kQexTvNgZvyk+lyi4wTU KT8d52cACV2peoDoW0tlHn8bTwBtSHU6Fx+6q3Lw= Received: from smtp (Not Verified[10.32.16.33]) by mmarshal3.atlnz.lc with Trustwave SEG (v7, 5, 6, 8438) id ; Tue, 04 Oct 2016 17:08:24 +1300 Received: from monster-build1.atlnz.lc (monster-build1.atlnz.lc [10.32.18.140]) by smtp (Postfix) with ESMTP id 4DA7213EF11; Tue, 4 Oct 2016 17:08:23 +1300 (NZDT) Received: by monster-build1.atlnz.lc (Postfix, from userid 1030) id 99511500576; Tue, 4 Oct 2016 17:08:23 +1300 (NZDT) From: Chris Packham To: jdelvare@suse.com, linux@roeck-us.net, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Michael.Wang@alliedtelesis.co.nz, Chris Packham Subject: [PATCHv1] hwmon: adm9240: handle temperature readings below 0 Date: Tue, 4 Oct 2016 17:08:00 +1300 Message-Id: <20161004040800.9689-1-chris.packham@alliedtelesis.co.nz> X-Mailer: git-send-email 2.10.0 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 Unlike the temperature thresholds the temperature data is a 9-bit signed value. This allows and additional 0.5 degrees of precision on the reading but means we can't rely on sign-extension to handle negative values. Signed-off-by: Chris Packham --- drivers/hwmon/adm9240.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/adm9240.c b/drivers/hwmon/adm9240.c index 98114ce..6929b25 100644 --- a/drivers/hwmon/adm9240.c +++ b/drivers/hwmon/adm9240.c @@ -107,6 +107,21 @@ static inline s8 TEMP_TO_REG(long val) return clamp_val(SCALE(val, 1, 1000), -40, 127); } +/* 8-bit signed temperature (from Thigh, Tlow etc) */ +static inline int TEMP_FROM_REG(s8 val) +{ + return val * 1000; +} + +/* 9-bit signed temperature (from temperature data) */ +static inline int TEMP_FROM_DATA_REG(s16 val) +{ + if (val > 255) + val -= 512; + + return val * 500; +} + /* two fans, each with low fan speed limit */ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) { @@ -263,7 +278,7 @@ static ssize_t show_temp(struct device *dev, struct device_attribute *dummy, char *buf) { struct adm9240_data *data = adm9240_update_device(dev); - return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */ + return sprintf(buf, "%d\n", TEMP_FROM_DATA_REG(data->temp)); } static ssize_t show_max(struct device *dev, struct device_attribute *devattr, @@ -271,7 +286,7 @@ static ssize_t show_max(struct device *dev, struct device_attribute *devattr, { struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct adm9240_data *data = adm9240_update_device(dev); - return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000); + return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index])); } static ssize_t set_max(struct device *dev, struct device_attribute *devattr,