From mboxrd@z Thu Jan 1 00:00:00 1970 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=-8.2 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED,USER_AGENT_MUTT 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 4C0A9C04A6B for ; Wed, 8 May 2019 18:32:57 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 22DD42173B for ; Wed, 8 May 2019 18:32:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="cipqvnrV"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="csOjKCZW" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 22DD42173B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=VNPrIfhSgywdgohfiCYdXPNKRUQEafl1dYaapOh8QQA=; b=cipqvnrV0Xe6vg xdXUudsnJITI3hxiCObblE7lnBWMNkRfVuilxJAgM3sEaZBrG2AMiRbM/8Q/BiaffTJNo3BxJaJw4 tx6YdmMpc3uvAazsDVxBF5aYqYWi8DpuWP5T1XwVfefbTtS4akY4z+fUSw0Vhhf4ou7zirFX5A/in /N0pn+FPRO1Y4QDNdMmUzJolykqGOlQh5X4s8Wli/sWzzUJ4YB9wMNjm5aNNNrEslkeGlUjtoyWQx GfpHxV1i2ybvyE2WGygTxToxClXMGxbpj6UIyxW8wyp+laaAqRDxjFcPYBBoEPHc6UVmIhpwECNMx aVRrohtmTdmA7Xb5e3Cg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hORN5-00029W-Ty; Wed, 08 May 2019 18:32:51 +0000 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hORN2-00028S-MZ for linux-arm-kernel@lists.infradead.org; Wed, 08 May 2019 18:32:50 +0000 Received: by mail-pg1-x544.google.com with SMTP id j26so10514804pgl.5 for ; Wed, 08 May 2019 11:32:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=w8iERNhjq4OzlLmPnHYUtIOOz9hjBDMOTj3lfmGbD88=; b=csOjKCZWFc7h/tvZm5iqctBGtliiS0aJWn6m4UCGgErAj3BADJf1b/pOoDIi2xbnLk IUIQYP6o81N8xJmmjgjWTtJ54UqTLLEO+Q6vJdk0VLZ5+cz3urCOJzaz5bp2Cm9yprNt o+2/m/66V1r0iUNn0RS6rP8/24NeN8EPVNbB3egVuSaIuLcmYrtsVy+TxCq0auMO8fqj NFie/xcxbkhm05WJn2Imb3IxYvSpU3p5ZIhcCPHwn+6KncdSHWZ/4EPVgsRHetsuW3/S Xnuorw9H65DeKNFlp052rMx8UZJSl1ttTHO3LukVYiJhNYSDN7NU+MtEcF1Oqa7nS2mr Ednw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=w8iERNhjq4OzlLmPnHYUtIOOz9hjBDMOTj3lfmGbD88=; b=pE+PtiGCb3Rjm/aKY8WpvW37EDcibtTKJ1IQQYxPDkThgykq6BVqLwo3nVVENm/IdO qV7ej7wBS/h0YhYkZ8fgUFQiDQhNpJLr5QJHeDTP5Cu6BSTdVkIw5EMY0lFwac8Yrwoa 1v80DoFEQDhXv0HgdccPDtaDVXIsVdx3eO1mArCL+SEHK1ulNdmHS3YrmHvBN3wvKOX2 Zd0v7OssdVvg5FPkJf1uRJNwqe5jAOyMlB/fTOq9e5INsE1Prg9o/0x5j7VR0F8DwhWO wV39OEKkKvGzhvl1CKjsrmmlIjibfbcaGF3EYXYulItiGsIR0q/DgTLNlmd5aCBYMC2w fIlg== X-Gm-Message-State: APjAAAXBVb15EEfGNroVhe/1wDC0Z7+zVwc/CbsLsWvIFe7OJywYWZf9 zLAromJAtK/haYlaIt+FapQ= X-Google-Smtp-Source: APXvYqwLYUVaKYkkXyJSAw3vwXScZuxYcOcqIn/p6Q7XM2+c5JUQm0gApMQMJJweVC7xeA+dHN91Sw== X-Received: by 2002:aa7:8252:: with SMTP id e18mr50800633pfn.105.1557340367126; Wed, 08 May 2019 11:32:47 -0700 (PDT) Received: from localhost ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id a18sm22025285pff.6.2019.05.08.11.32.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 May 2019 11:32:45 -0700 (PDT) Date: Wed, 8 May 2019 11:32:44 -0700 From: Guenter Roeck To: Florian Fainelli Subject: Re: [PATCH v4 2/2] hwmon: scmi: Scale values to target desired HWMON units Message-ID: <20190508183244.GA25133@roeck-us.net> References: <20190508170035.19671-1-f.fainelli@gmail.com> <20190508170035.19671-3-f.fainelli@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20190508170035.19671-3-f.fainelli@gmail.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190508_113248_762412_30E7253E X-CRM114-Status: GOOD ( 18.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "open list:HARDWARE MONITORING" , Jean Delvare , linux-kernel@vger.kernel.org, bcm-kernel-feedback-list@broadcom.com, Sudeep Holla , linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Florian, On Wed, May 08, 2019 at 10:00:35AM -0700, Florian Fainelli wrote: > 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 | 46 ++++++++++++++++++++++++++++++++++++++ > 1 file changed, 46 insertions(+) > > diff --git a/drivers/hwmon/scmi-hwmon.c b/drivers/hwmon/scmi-hwmon.c > index a80183a488c5..4399372e2131 100644 > --- a/drivers/hwmon/scmi-hwmon.c > +++ b/drivers/hwmon/scmi-hwmon.c > @@ -7,6 +7,7 @@ > */ > > #include > +#include > #include > #include > #include > @@ -18,6 +19,47 @@ struct scmi_sensors { > const struct scmi_sensor_info **info[hwmon_max]; > }; > > +static inline u64 __pow10(u8 x) > +{ > + u64 r = 1; > + > + while (x--) > + r *= 10; > + > + return r; > +} > + > +static int 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 (f == U64_MAX) > + return -E2BIG; Unfortunately that is not how integer overflows work. A test program with increasing values of scale reports: 0: 1 ... 18: 1000000000000000000 19: 10000000000000000000 20: 7766279631452241920 21: 3875820019684212736 22: 1864712049423024128 23: 200376420520689664 24: 2003764205206896640 ... 61: 11529215046068469760 62: 4611686018427387904 63: 9223372036854775808 64: 0 ... You'll have to check for abs(scale) > 19 if you want to report overflows. Guenter > + > + if (scale > 0) > + *value *= f; > + else > + *value = div64_u64(*value, f); > + > + return 0; > +} > + > static int scmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type, > u32 attr, int channel, long *val) > { > @@ -29,6 +71,10 @@ 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) > + return ret; > + > + ret = scmi_hwmon_scale(sensor, value); > if (!ret) > *val = value; > > -- > 2.17.1 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel