From patchwork Wed Sep 22 13:41:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Fertser X-Patchwork-Id: 12510707 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39EB3C433F5 for ; Wed, 22 Sep 2021 13:42:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F1BE60F24 for ; Wed, 22 Sep 2021 13:42:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232209AbhIVNoP (ORCPT ); Wed, 22 Sep 2021 09:44:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231758AbhIVNoO (ORCPT ); Wed, 22 Sep 2021 09:44:14 -0400 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C97B2C061756; Wed, 22 Sep 2021 06:42:44 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id z24so12054329lfu.13; Wed, 22 Sep 2021 06:42:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=2pVmVEzBfv3j4RRyLFyDyz2q0UkyXOkX4Ca5NndLWGQ=; b=hRCSeWYyuAGIwYgMIShMmegsmZ6IGA/T4u0loyzF5RBeeQxNnbodpEbSiZs77byGkZ Emd0wCzIYVLJiH7UZnSm+vmsx1LJgrFfOr83UACJevZ3Ru/cm//GcKWTKUQAEsPk4TxD kXIT0L1BEqK7JeCfnnDFINm7IZ9Mof90OCt1l5rv2r+bvKzDKXPF9kfv3LOZjtZ4WFRz u6qJSX7cECehU9KY4wb6mf1oEg+hCOIhuR0Jgx7XIBb8KDnNsVD4xKnuTAbL5BssdXyg LpD3bhT7OnX3jdxJxfcVZ//POCSG/vtby5INd7X5PKjw/jL8RbYotwfruHO1yb3k+G9R xgVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=2pVmVEzBfv3j4RRyLFyDyz2q0UkyXOkX4Ca5NndLWGQ=; b=UOZGv4BXtJmeGBOmTOD9hisd/iw8hNUHyGMUG/rvWFS9VLHG/6Vdh5JMdnIhGF+yte go/sG1tem7H/jBuJ4e6VoPsnrMKQTmbQYsZ9Evck7k/jS9yMQy6Cv4VBFVNXAzs7yF+N mwZBJF68kwaiVK4sHi5WWwGkw3xAf696dn3FUgLiYtKaKoUeOpuDvyKWOtHFRzeaCzlN oHwQFFOvCDWmQN5/+Wu2u4yNjCn4+bGEl6p2RS4TMc95FQbUse/Bexv7j+j8UT1TsXT6 +rVtmzyFp0FnEQIxU8+rSFy5iIYBMGxG7OoM0C1mZD0AghjqUjjtqAzQ1c56OYqEak8R Mamg== X-Gm-Message-State: AOAM530LOo4DWkvXeZnDn65e66ATMoZmmtJEovx8PpeF78Fu4l5OH2GK AsGgPaY0PLSyj54F2vco7bS4yZSXTPc= X-Google-Smtp-Source: ABdhPJxvjCRhlbV6Pf95jFGFqk5kPiAWKflglCSbRdgN7KCHc+pN5MfJUXPiaO7jBWhL+vcnEC8XrQ== X-Received: by 2002:a05:6512:3d94:: with SMTP id k20mr18072118lfv.359.1632318140015; Wed, 22 Sep 2021 06:42:20 -0700 (PDT) Received: from home.paul.comp (paulfertser.info. [2001:470:26:54b:226:9eff:fe70:80c2]) by smtp.gmail.com with ESMTPSA id e5sm256943ljj.129.2021.09.22.06.42.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Sep 2021 06:42:19 -0700 (PDT) Received: from home.paul.comp (home.paul.comp [IPv6:0:0:0:0:0:0:0:1]) by home.paul.comp (8.15.2/8.15.2/Debian-14~deb10u1) with ESMTP id 18MDgGtj020807; Wed, 22 Sep 2021 16:42:17 +0300 Received: (from paul@localhost) by home.paul.comp (8.15.2/8.15.2/Submit) id 18MDgF5Q020806; Wed, 22 Sep 2021 16:42:15 +0300 From: Paul Fertser To: linux-hwmon@vger.kernel.org Cc: Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, Paul Fertser , stable@vger.kernel.org Subject: [PATCH 1/3] hwmon: tmp421: handle I2C errors Date: Wed, 22 Sep 2021 16:41:52 +0300 Message-Id: <20210922134154.20766-1-fercerpav@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org Function i2c_smbus_read_byte_data() can return a negative error number instead of the data read if I2C transaction failed for whatever reason. I consider this fix to be stable material as lack of error checking here leads to serious issues on production hardware. Errors treated as temperatures produce spurious critical temperature-crossed-threshold errors in BMC logs for OCP server hardware. The patch was tested with Mellanox OCP Mezzanine card emulating TMP421 protocol for temperature sensing which sometimes leads to I2C protocol error during early boot up stage. Cc: stable@vger.kernel.org Signed-off-by: Paul Fertser --- drivers/hwmon/tmp421.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/drivers/hwmon/tmp421.c b/drivers/hwmon/tmp421.c index ede66ea6a730..6175ed4b10bd 100644 --- a/drivers/hwmon/tmp421.c +++ b/drivers/hwmon/tmp421.c @@ -93,7 +93,7 @@ struct tmp421_data { struct hwmon_channel_info temp_info; const struct hwmon_channel_info *info[2]; struct hwmon_chip_info chip; - char valid; + int last_errno; unsigned long last_updated; unsigned long channels; u8 config; @@ -128,20 +128,30 @@ static struct tmp421_data *tmp421_update_device(struct device *dev) mutex_lock(&data->update_lock); if (time_after(jiffies, data->last_updated + (HZ / 2)) || - !data->valid) { - data->config = i2c_smbus_read_byte_data(client, - TMP421_CONFIG_REG_1); + data->last_errno) { + data->last_errno = i2c_smbus_read_byte_data(client, + TMP421_CONFIG_REG_1); + if (data->last_errno < 0) + goto exit; + data->config = data->last_errno; for (i = 0; i < data->channels; i++) { - data->temp[i] = i2c_smbus_read_byte_data(client, - TMP421_TEMP_MSB[i]) << 8; - data->temp[i] |= i2c_smbus_read_byte_data(client, - TMP421_TEMP_LSB[i]); + data->last_errno = i2c_smbus_read_byte_data(client, + TMP421_TEMP_MSB[i]); + if (data->last_errno < 0) + goto exit; + data->temp[i] = data->last_errno << 8; + data->last_errno = i2c_smbus_read_byte_data(client, + TMP421_TEMP_LSB[i]); + if (data->last_errno < 0) + goto exit; + data->temp[i] |= data->last_errno; } data->last_updated = jiffies; - data->valid = 1; + data->last_errno = 0; } +exit: mutex_unlock(&data->update_lock); return data; @@ -152,6 +162,9 @@ static int tmp421_read(struct device *dev, enum hwmon_sensor_types type, { struct tmp421_data *tmp421 = tmp421_update_device(dev); + if (tmp421->last_errno) + return tmp421->last_errno; + switch (attr) { case hwmon_temp_input: if (tmp421->config & TMP421_CONFIG_RANGE) From patchwork Wed Sep 22 13:41:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Fertser X-Patchwork-Id: 12510709 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29ED1C4332F for ; Wed, 22 Sep 2021 13:42:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1263A610A1 for ; Wed, 22 Sep 2021 13:42:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231758AbhIVNoQ (ORCPT ); Wed, 22 Sep 2021 09:44:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232385AbhIVNoP (ORCPT ); Wed, 22 Sep 2021 09:44:15 -0400 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AF32C061574; Wed, 22 Sep 2021 06:42:45 -0700 (PDT) Received: by mail-lf1-x132.google.com with SMTP id z24so12054528lfu.13; Wed, 22 Sep 2021 06:42:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZR/F/tAdeYtP0BuXmGIaoBGFpWAWS9oXF4q6CXrSt+8=; b=RlPXqdhg5O+9K6Aooi4Oe0pSO4FXKyo7eQXFdGOeyCaIRwTNCkYw+5UJt8SmHfuWdh ZHkRuNjdLPLSsjy7a3EACTCu1Bt1vox1KGcq8ZUneSHLI9zVXMcMhnuFRDV5b/9aQCv1 yUpag+uUZV5n/AJbHNv3xBhezw3zfn/M7xnn4pal71SnxyRz1VAz5eamCVH4h4EJ5tA+ T+NX/MeyooGp3b5z5C0PmgoqUvQWrLS2JL3VoJCTJ6GOyW90BHNueOJtA1dILe6rxzJ+ xObUlsUwUBzvZ7Kca7yXySkBuazF5inyXxi/A0S6RVEzcnwK0s+G9v7vq4Nxv7KZIkgB PqAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZR/F/tAdeYtP0BuXmGIaoBGFpWAWS9oXF4q6CXrSt+8=; b=5SFhxIzXSO3hZ0ACXq3QBuX7ujVGmz1XCxh7ygRwynq982XS2HsA+x0tf1GGBDjPtx Ka62tj2IoolRYX8LlRBslmbsgPWlQAQMruunWPQIfPTaFmkZO1zeciLn8daGO3CS91Fd AQofKMiYDRNkCbNikRKW8Ybz0qF7+wEyMyvLWwM4i0xDQ5hfg9W8IWEbpKKhsl7xY/qx se37pEZCtcMv66uEAp2USckwx7N2g+zifTNPyW2SFPqgUXMBosIR5MpwNYkbIAmwvgLd VI/z306qtPOts/LylYj1AL3YdGTethGfICdrSEZ0sNcWn0cCOYU3lDaat0ITa1xqVQOh ldxg== X-Gm-Message-State: AOAM531Pr4q5VlDACjec2wPs5XfLLDDVQaz4CyV/V83j53Kax+4IO9yb qFkCzp3VgYkUjnU5DX3d0zg5kQdI0m0= X-Google-Smtp-Source: ABdhPJxDBDAte4lqxgQllzAVk7Y5eMZPUdHA9gjrGvoqnd9w+WwBMU0yva+Wdm0W0K3KiOLTtPAK6A== X-Received: by 2002:a05:6512:3091:: with SMTP id z17mr27336263lfd.246.1632318140763; Wed, 22 Sep 2021 06:42:20 -0700 (PDT) Received: from home.paul.comp (paulfertser.info. [2001:470:26:54b:226:9eff:fe70:80c2]) by smtp.gmail.com with ESMTPSA id r12sm168495ljc.40.2021.09.22.06.42.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Sep 2021 06:42:20 -0700 (PDT) Received: from home.paul.comp (home.paul.comp [IPv6:0:0:0:0:0:0:0:1]) by home.paul.comp (8.15.2/8.15.2/Debian-14~deb10u1) with ESMTP id 18MDgHGf020811; Wed, 22 Sep 2021 16:42:18 +0300 Received: (from paul@localhost) by home.paul.comp (8.15.2/8.15.2/Submit) id 18MDgHYD020810; Wed, 22 Sep 2021 16:42:17 +0300 From: Paul Fertser To: linux-hwmon@vger.kernel.org Cc: Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, Paul Fertser Subject: [PATCH 2/3] hwmon: tmp421: report /PVLD condition Date: Wed, 22 Sep 2021 16:41:53 +0300 Message-Id: <20210922134154.20766-2-fercerpav@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210922134154.20766-1-fercerpav@gmail.com> References: <20210922134154.20766-1-fercerpav@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org For both local and remote sensors all the supported ICs can report an "undervoltage lockout" condition which means the conversion wasn't properly performed due to insufficient power supply voltage and so the measurement results can't be trusted. Signed-off-by: Paul Fertser --- drivers/hwmon/tmp421.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/tmp421.c b/drivers/hwmon/tmp421.c index 6175ed4b10bd..9ee4c61531f3 100644 --- a/drivers/hwmon/tmp421.c +++ b/drivers/hwmon/tmp421.c @@ -174,10 +174,10 @@ static int tmp421_read(struct device *dev, enum hwmon_sensor_types type, return 0; case hwmon_temp_fault: /* - * The OPEN bit signals a fault. This is bit 0 of the temperature - * register (low byte). + * Any of OPEN or /PVLD bits indicate a hardware mulfunction + * and the conversion result may be incorrect */ - *val = tmp421->temp[channel] & 0x01; + *val = !!(tmp421->temp[channel] & 0x03); return 0; default: return -EOPNOTSUPP; @@ -190,9 +190,6 @@ static umode_t tmp421_is_visible(const void *data, enum hwmon_sensor_types type, { switch (attr) { case hwmon_temp_fault: - if (channel == 0) - return 0; - return 0444; case hwmon_temp_input: return 0444; default: From patchwork Wed Sep 22 13:41:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Fertser X-Patchwork-Id: 12510711 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2976C433F5 for ; Wed, 22 Sep 2021 13:42:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C76F1610A1 for ; Wed, 22 Sep 2021 13:42:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232385AbhIVNoY (ORCPT ); Wed, 22 Sep 2021 09:44:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233003AbhIVNoX (ORCPT ); Wed, 22 Sep 2021 09:44:23 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82A31C061756; Wed, 22 Sep 2021 06:42:53 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id e15so11999536lfr.10; Wed, 22 Sep 2021 06:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lOF37ErIrt4QCm9hfD32diBPJ9qYvrZDIm2Bmis27/E=; b=REMfwssb9gkyNN1zQ063pdfB/iO5RBC6SxWxl5yWT8m2NGso+nVADmTJFQkvl1BB26 dyTNEApex+x7KZZ6JzOENT1d+mkRVTcDqC6oCZonKNdtKUKotQxW7Te3C4q3Wm9VrZXa u3OKY9+uAe5Y88jqUrgLkDKaMS+nacUhuV68glw0LaUOQyxrgCD0df5Sv6QUEYNn8D9a z7P5Qkjx4MVIJBreEj6zTauE/JerFvKR6/efBPccLImCJqQRmR+9oia4maiyGsl8s0sA RMUqNf+EmuNe1G/ch5akAp2oaD0wlczxYxRZ62jp+VTdRVwbBrcqs0Uw7bNMW6J25fVZ enVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lOF37ErIrt4QCm9hfD32diBPJ9qYvrZDIm2Bmis27/E=; b=S+AXnZfEQBiyIE5qlryuiur9tgFe90e7hWR1sFEscc84zbTlzVuehCOVsbHkOstGzt 8OAgKHxYWPmiJ+gf3UQe7eAphZFeMYQWpHh8T2oqIZw595lbZeGezetqBPfzJaaIQwhn dfr6HyoaHvq4okMZazN+SowYzMqetUYiBdgU5KFluVVkXIOEQSzgInXZxdzo6yQhQ4qs y3If/QrwwM3qguNmnqSiJmXHCtp1wP9tR72Z56RsTBF9ujokzpAzOf9e4R+zuB6e1G3m efGk6ckpIfCqeG/sFN/pXyZlXcKzoyr+BdrXzZLcuoM7qcEZU5ZW9WijKxEFSpuAKe3A NF0A== X-Gm-Message-State: AOAM532ew8+FG/XtfECnj7gHJsQ5GbJvybtJ1eXzf8W+DC77kCXnTZRi cTl6L8Fm+eSlbx1QIpF/lnuDxuOwX7Q= X-Google-Smtp-Source: ABdhPJzwC1AGAhl37Phjms+cmHYc8VuojgykapdK5JlDg6Linc2JCp91P7QHFEqG5djxTCsQkB3qqw== X-Received: by 2002:a05:651c:3c3:: with SMTP id f3mr27966863ljp.222.1632318142221; Wed, 22 Sep 2021 06:42:22 -0700 (PDT) Received: from home.paul.comp (paulfertser.info. [2001:470:26:54b:226:9eff:fe70:80c2]) by smtp.gmail.com with ESMTPSA id y13sm182538lfs.17.2021.09.22.06.42.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Sep 2021 06:42:21 -0700 (PDT) Received: from home.paul.comp (home.paul.comp [IPv6:0:0:0:0:0:0:0:1]) by home.paul.comp (8.15.2/8.15.2/Debian-14~deb10u1) with ESMTP id 18MDgJ6j020815; Wed, 22 Sep 2021 16:42:20 +0300 Received: (from paul@localhost) by home.paul.comp (8.15.2/8.15.2/Submit) id 18MDgJSW020814; Wed, 22 Sep 2021 16:42:19 +0300 From: Paul Fertser To: linux-hwmon@vger.kernel.org Cc: Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, Paul Fertser Subject: [PATCH 3/3] hwmon: tmp421: fix rounding for negative values Date: Wed, 22 Sep 2021 16:41:54 +0300 Message-Id: <20210922134154.20766-3-fercerpav@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210922134154.20766-1-fercerpav@gmail.com> References: <20210922134154.20766-1-fercerpav@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org Current code produces -24999 for 0b1110011100000000 input in standard format due to always rounding up rather than "away from zero". Use the common macro for division, unify and simplify the conversion code along the way. Signed-off-by: Paul Fertser --- drivers/hwmon/tmp421.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/drivers/hwmon/tmp421.c b/drivers/hwmon/tmp421.c index 9ee4c61531f3..dec3aced3d8b 100644 --- a/drivers/hwmon/tmp421.c +++ b/drivers/hwmon/tmp421.c @@ -100,23 +100,17 @@ struct tmp421_data { s16 temp[4]; }; -static int temp_from_s16(s16 reg) +static int temp_from_raw(u16 reg, bool extended) { /* Mask out status bits */ int temp = reg & ~0xf; - return (temp * 1000 + 128) / 256; -} - -static int temp_from_u16(u16 reg) -{ - /* Mask out status bits */ - int temp = reg & ~0xf; - - /* Add offset for extended temperature range. */ - temp -= 64 * 256; + if (extended) + temp = temp - 64 * 256; + else + temp = (s16)temp; - return (temp * 1000 + 128) / 256; + return DIV_ROUND_CLOSEST(temp * 1000, 256); } static struct tmp421_data *tmp421_update_device(struct device *dev) @@ -167,10 +161,8 @@ static int tmp421_read(struct device *dev, enum hwmon_sensor_types type, switch (attr) { case hwmon_temp_input: - if (tmp421->config & TMP421_CONFIG_RANGE) - *val = temp_from_u16(tmp421->temp[channel]); - else - *val = temp_from_s16(tmp421->temp[channel]); + *val = temp_from_raw(tmp421->temp[channel], + tmp421->config & TMP421_CONFIG_RANGE); return 0; case hwmon_temp_fault: /*