From patchwork Tue May 7 23:09:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Fainelli X-Patchwork-Id: 10933859 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 1AC041390 for ; Tue, 7 May 2019 23:10:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0DB0027C0B for ; Tue, 7 May 2019 23:10:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 020152848D; Tue, 7 May 2019 23:10:18 +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 2257727C0B for ; Tue, 7 May 2019 23:10:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726091AbfEGXKQ (ORCPT ); Tue, 7 May 2019 19:10:16 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:40845 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726565AbfEGXKP (ORCPT ); Tue, 7 May 2019 19:10:15 -0400 Received: by mail-pl1-f196.google.com with SMTP id b3so8902795plr.7; Tue, 07 May 2019 16:10:15 -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=NxAUam5Dvt1HQoAAg/vTkC6mAACjVaJFnU6FSnpD4fQ=; b=W79MYHObhxKSz1yrBebZxY6fIgGFQNZrGIKFl/P07u3fvm0L1svG6Kge0eSRxVMp++ HozWeRJd9krBTS+xX5kgatnXuUjD9tj/TvnMEHLL2cO8kQbZUF/lNWw586NJu17NAMcv ailq0uRykUWp8uFLRxoqACxHQA2bO+U4QJFEiX7wlWIww54EXW5ykKohjZClEbzdG4V5 pQbIuKE1pEUnCXX9kQb6IU1GoFh54YlYmLWJfJR7yjlnLPnZ1cUP5EzLwc4P1HW5elL9 I2H+DGBEluRSfw6DRuLlopJp/7NEJQPNe2mdK/zjG7tkkusjqNjlfErgu5gtocC1UsO2 s10Q== 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=NxAUam5Dvt1HQoAAg/vTkC6mAACjVaJFnU6FSnpD4fQ=; b=My2efH/zBEQwXiTIXRGMc0hQque7qeCKMyHU7MWUoc7J0M2qY8cPdSiv6VtnAP/LlU rPY/dGt1i6SCZFpDtE9VCh85c83PKRKrG7R+CDdOiIJEOBD0nzZ88My2FbnHupo4hnjc 3ZfpXtgHx2UO/2R9NYLfjvZnvXwNA8vyNNuuw/xqaxxgZJlVZgSSC+9x9ag1Vcnav/oc MjW6HCse4ChZ/kN6cU9hPEZ5nA12sGx6G8RYei0xop4TkPhyEFLmnyocThlTX11gQcJ2 SHvNFarlqg5SUtO9igFbAQ43ljDvV0rndjbNJD9jGOBmeUpyif0GR2VoqZW3brZjDLMl EleQ== X-Gm-Message-State: APjAAAXajYvbUiVix9bs+E+XM079iro5GvshvoBiI1gtLZ5JVkl4vCAu bDYDWK7eOfSQuC0Jv46NOBkZz/+c X-Google-Smtp-Source: APXvYqwxsPy0o2lNDye/b4YPbzc/hYdJL1LqlWL1647cpG9aT7GB0XIvVQFfixDP9ZMIBfigSy0I8g== X-Received: by 2002:a17:902:9a9:: with SMTP id 38mr28196267pln.10.1557270614534; Tue, 07 May 2019 16:10:14 -0700 (PDT) Received: from fainelli-desktop.igp.broadcom.net ([192.19.223.250]) by smtp.gmail.com with ESMTPSA id w12sm7154742pfj.41.2019.05.07.16.10.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 May 2019 16:10:13 -0700 (PDT) From: Florian Fainelli To: linux-kernel@vger.kernel.org Cc: bcm-kernel-feedback-list@broadcom.com, Florian Fainelli , Sudeep Holla , Jean Delvare , Guenter Roeck , linux-arm-kernel@lists.infradead.org, linux-hwmon@vger.kernel.org (open list:HARDWARE MONITORING) Subject: [PATCH v3 1/2] firmware: arm_scmi: Fetch and store sensor scale Date: Tue, 7 May 2019 16:09:16 -0700 Message-Id: <20190507230917.21659-2-f.fainelli@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190507230917.21659-1-f.fainelli@gmail.com> References: <20190507230917.21659-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 In preparation for dealing with scales within the SCMI HWMON driver, fetch and store the sensor unit scale into the scmi_sensor_info structure. In order to simplify computations for upper layer, take care of sign extending the scale to a full 8-bit signed value. Signed-off-by: Florian Fainelli --- drivers/firmware/arm_scmi/sensors.c | 6 ++++++ include/linux/scmi_protocol.h | 1 + 2 files changed, 7 insertions(+) diff --git a/drivers/firmware/arm_scmi/sensors.c b/drivers/firmware/arm_scmi/sensors.c index b53d5cc9c9f6..21353470a740 100644 --- a/drivers/firmware/arm_scmi/sensors.c +++ b/drivers/firmware/arm_scmi/sensors.c @@ -34,6 +34,8 @@ struct scmi_msg_resp_sensor_description { __le32 attributes_high; #define SENSOR_TYPE(x) ((x) & 0xff) #define SENSOR_SCALE(x) (((x) >> 11) & 0x3f) +#define SENSOR_SCALE_SIGN BIT(5) +#define SENSOR_SCALE_EXTEND GENMASK(7, 6) #define SENSOR_UPDATE_SCALE(x) (((x) >> 22) & 0x1f) #define SENSOR_UPDATE_BASE(x) (((x) >> 27) & 0x1f) u8 name[SCMI_MAX_STR_SIZE]; @@ -140,6 +142,10 @@ static int scmi_sensor_description_get(const struct scmi_handle *handle, s = &si->sensors[desc_index + cnt]; s->id = le32_to_cpu(buf->desc[cnt].id); s->type = SENSOR_TYPE(attrh); + s->scale = SENSOR_SCALE(attrh); + /* Sign extend to a full s8 */ + if (s->scale & SENSOR_SCALE_SIGN) + s->scale |= SENSOR_SCALE_EXTEND; strlcpy(s->name, buf->desc[cnt].name, SCMI_MAX_STR_SIZE); } diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 3105055c00a7..9ff2e9357e9a 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -144,6 +144,7 @@ struct scmi_power_ops { struct scmi_sensor_info { u32 id; u8 type; + s8 scale; char name[SCMI_MAX_STR_SIZE]; }; From patchwork Tue May 7 23:09:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Fainelli X-Patchwork-Id: 10933863 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 C8BB3924 for ; Tue, 7 May 2019 23:10:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B8D2327C0B for ; Tue, 7 May 2019 23:10:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AC9F82848D; Tue, 7 May 2019 23:10:25 +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 9B80F27C0B for ; Tue, 7 May 2019 23:10:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726597AbfEGXKT (ORCPT ); Tue, 7 May 2019 19:10:19 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:38407 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726571AbfEGXKR (ORCPT ); Tue, 7 May 2019 19:10:17 -0400 Received: by mail-pg1-f195.google.com with SMTP id j26so9058169pgl.5; Tue, 07 May 2019 16:10:16 -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=jKMO64xZ/NLNKAqyiQNdSxQ5RK52UXZdK9wfMghWcTw=; b=S6HCWjxdMhJXtpwtM6/5wElmUx+WcK79V76XMFaSdug+PAu+yyFKLenuOkNhRbUxmS v3peC+1jROrGVnrXIO+nS9/OSVcDeZ0XUNanPKar19SXIlUGdEois0Y36nWjHYo55e+t Apt2ZkWGfyiHwqu8+2TeAWya9gA+W3IanulvL9jEBzQBrfuBZUuOnqV3OycdOW45RDgQ ietXGqmx6LdjfDE8N760ppa9EixmyUX/Mebgb4XYQjyT0pWlFOH+1RYHxeZ4g0fODrjV /CdGuaj/I+JvyJ8z51L4ubjESbAmmGTB6BZWI1e/dlj5qk6WT+YUJNX/csrzSFx0fbAb 7keg== 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=jKMO64xZ/NLNKAqyiQNdSxQ5RK52UXZdK9wfMghWcTw=; b=EQ3HQERgeH6ToT89zscMxgaa2p9/HpXUDI6GHaij0EHa25lO5hEhPR0YdW9vaNOdNN Q7jgvvUDbD7YywIWq4RPgCcZcpn/7l/KdwMi/Vryi+w31ofi/Ctt66rFQl7/EVWvSsxJ qJG4NqJPFJ5K5Svt/SY6zBAWqLn1bHoM/spFgooM58lH3IBt3/FcOr9qutX+59PXtUky 3LQlpWnzeXaWVMoqN307jckziC+yUgl0CeYLQiD1Ry04ZKvnWvMek/1auMedTppGB+S4 4Y08Mns5njL2OKSJLR5B2Ahz82YSbPU2vVJvbMMCSx/71gTxCnHURCNFAfAeWQdewus/ M17A== X-Gm-Message-State: APjAAAVNEqQLYTwO5tanCTJP1RnUWGxQD2cxuMW/YsrH5B9lp6E0hNBN Kn4czfxWDF2X99v0Znsohpf7PZoJ X-Google-Smtp-Source: APXvYqxwBYjE6jd3+l8NIjy4bYaiYCnr9KFnXMPOKo6jq8A3gHkYS7MaEekUk25FuV5XGUSfcQ+cjw== X-Received: by 2002:a63:e956:: with SMTP id q22mr43144036pgj.277.1557270616179; Tue, 07 May 2019 16:10:16 -0700 (PDT) Received: from fainelli-desktop.igp.broadcom.net ([192.19.223.250]) by smtp.gmail.com with ESMTPSA id w12sm7154742pfj.41.2019.05.07.16.10.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 May 2019 16:10:15 -0700 (PDT) From: Florian Fainelli To: linux-kernel@vger.kernel.org Cc: bcm-kernel-feedback-list@broadcom.com, Florian Fainelli , Sudeep Holla , Jean Delvare , Guenter Roeck , linux-arm-kernel@lists.infradead.org, linux-hwmon@vger.kernel.org (open list:HARDWARE MONITORING) Subject: [PATCH v3 2/2] hwmon: scmi: Scale values to target desired HWMON units Date: Tue, 7 May 2019 16:09:17 -0700 Message-Id: <20190507230917.21659-3-f.fainelli@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190507230917.21659-1-f.fainelli@gmail.com> References: <20190507230917.21659-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 | 43 +++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/drivers/hwmon/scmi-hwmon.c b/drivers/hwmon/scmi-hwmon.c index a80183a488c5..7820854e5954 100644 --- a/drivers/hwmon/scmi-hwmon.c +++ b/drivers/hwmon/scmi-hwmon.c @@ -18,6 +18,47 @@ struct scmi_sensors { const struct scmi_sensor_info **info[hwmon_max]; }; +static inline u64 __pow10(u8 x) +{ + u64 r = 1; + + if (unlikely(x > 18)) + return r; + + while (x--) + r *= 10; + + return r; +} + +static u64 scmi_hwmon_scale(const struct scmi_sensor_info *sensor, u64 value) +{ + s8 scale = sensor->scale; + u64 f; + + switch (sensor->type) { + case TEMPERATURE_C: + case VOLTAGE: + case CURRENT: + scale += 3; + break; + case POWER: + case ENERGY: + scale += 6; + break; + default: + break; + } + + f = __pow10(abs(scale)); + if (scale > 0) + value *= f; + else + value = div64_u64(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 +71,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; }