From patchwork Tue May 7 19:35:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Fainelli X-Patchwork-Id: 10933701 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4C03616C1 for ; Tue, 7 May 2019 19:38:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3C11024B44 for ; Tue, 7 May 2019 19:38:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 30684274D1; Tue, 7 May 2019 19:38:22 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,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 D451724B44 for ; Tue, 7 May 2019 19:38:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726608AbfEGTiV (ORCPT ); Tue, 7 May 2019 15:38:21 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:46729 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726336AbfEGTiU (ORCPT ); Tue, 7 May 2019 15:38:20 -0400 Received: by mail-pl1-f194.google.com with SMTP id bi2so8668667plb.13; Tue, 07 May 2019 12:38:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0un4Zq1Ylcl4MILUCeu+0vxFmB6uI/BzyrLOsfSvsMM=; b=RFOUUtGXP8OJR5bQmeIdNY3FuggAcfnTfuyUoXqn9lbFOD3gfZnBbA63LWUpgklwQ0 7MI/Qs3Ae2RpZm4tI4XSW4RxXp7Y3xj7Z1uwkilzE6NDrPqc3uvJwx8/vr2giw9wKfB0 zkDlRQXeb/bF6fflJkc0DBDNJNtkVoQIdoeimGxZJUPGfJGRLaCpbx2IIkj3foDMYjOO PWaPmr/M3yhPf7N681uWwQDkxLfH2/8qL9wUalhnAnG5ihkkEaJK+G+QhLmANO7vRiTn cgcZ8jdoAM7U3czBbWeKKh7ObtbqV5/X4mA9beUOefb+nLMwGUReTc2+PVbgv41TMLfO gXdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0un4Zq1Ylcl4MILUCeu+0vxFmB6uI/BzyrLOsfSvsMM=; b=DaY1NFiMQNmL4I1nseJxl2l74ew+xt6RG9GeJuw01Fk5I9jPQ4Oc0U7OHuZYNtpLyw PzZvzf+MOMsmg8/3cp3CIsTILLdXRZM7vf2SUCXWG+HoCIHT7RrqPwpUkXo8whfr9FVP 15amTxVu0XWxlupH0Z489fWjAKqs7GR2DFfy80lv5WkxGufmRZBjQBfTIZblAUQyBUAL oKIksqUtbh6ClaHhwaYUNqlAt3YJW7R724jUE/UY8aV1bWaSCRDryJUPUM+gKqrJchwj SNS/KxjPHajfxTbsTHD3yFK4maqQgoNHKnUIe+sakT0p+EFUqmIEXkdNQ5Appk3+8e3r SAXg== X-Gm-Message-State: APjAAAXQwMyI2qnz368APL+8Pgk7npums5ksQgoMqpFaqhOZc+Vw6GEA RodoiyjD/nCNFemcVFxR0dPEYhKA X-Google-Smtp-Source: APXvYqy9JrXGy3NKwIYDouYAWcrRPVQJ8hxuwN5Y/em8coxBLkR2WaURI4/cr8v4DaOdBargyNfPXA== X-Received: by 2002:a17:902:e188:: with SMTP id cd8mr20263009plb.110.1557257899147; Tue, 07 May 2019 12:38:19 -0700 (PDT) Received: from fainelli-desktop.igp.broadcom.net ([192.19.223.250]) by smtp.gmail.com with ESMTPSA id l21sm5964658pff.40.2019.05.07.12.38.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 May 2019 12:38:18 -0700 (PDT) From: Florian Fainelli To: linux-kernel@vger.kernel.org Cc: Florian Fainelli , Sudeep Holla , Jean Delvare , Guenter Roeck , linux-arm-kernel@lists.infradead.org, linux-hwmon@vger.kernel.org (open list:HARDWARE MONITORING), bcm-kernel-feedback-list@broadcom.com Subject: [PATCH v2 3/3] hwmon: scmi: Scale values to target desired HWMON units Date: Tue, 7 May 2019 12:35:04 -0700 Message-Id: <20190507193504.28248-4-f.fainelli@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190507193504.28248-1-f.fainelli@gmail.com> References: <20190507193504.28248-1-f.fainelli@gmail.com> 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 If the SCMI firmware implementation is reporting values in a scale that is different from the HWMON units, we need to scale up or down the value according to how far appart they are. Signed-off-by: Florian Fainelli --- drivers/hwmon/scmi-hwmon.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/drivers/hwmon/scmi-hwmon.c b/drivers/hwmon/scmi-hwmon.c index a80183a488c5..5c244053efc8 100644 --- a/drivers/hwmon/scmi-hwmon.c +++ b/drivers/hwmon/scmi-hwmon.c @@ -18,6 +18,34 @@ struct scmi_sensors { const struct scmi_sensor_info **info[hwmon_max]; }; +static u64 scmi_hwmon_scale(const struct scmi_sensor_info *sensor, u64 value) +{ + s8 scale = sensor->scale; + unsigned long long f; + + switch (sensor->type) { + case TEMPERATURE_C: + case VOLTAGE: + case CURRENT: + scale += 3; + break; + case POWER: + case ENERGY: + scale += 6; + break; + default: + break; + } + + __pow10(abs(scale), f); + if (scale > 0) + value *= f; + else + do_div(value, f); + + return value; +} + static int scmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { @@ -30,7 +58,7 @@ static int scmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type, sensor = *(scmi_sensors->info[type] + channel); ret = h->sensor_ops->reading_get(h, sensor->id, false, &value); if (!ret) - *val = value; + *val = scmi_hwmon_scale(sensor, value); return ret; }