From patchwork Fri Mar 3 09:14:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Caione X-Patchwork-Id: 9602349 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 8C4C660453 for ; Fri, 3 Mar 2017 09:11:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F2B2208C2 for ; Fri, 3 Mar 2017 09:11:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 73BD4285E3; Fri, 3 Mar 2017 09:11: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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1D513208C2 for ; Fri, 3 Mar 2017 09:11:18 +0000 (UTC) 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:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=2qFyqrUvnV9V59wnuEIjdQMIZi1LsKHTDFzUqUHkurw=; b=UqGZ7Z8AhBfGFwtnYQvXSjKZw2 D07r+r/L6nCQBkmnI43lTQQyfXntVnh0SZ8jHmZ3eCjNf+GIpuAAn2DU6paXW1RJGoZk5K4y5DZfz b+xzP5ajnweySkXSRZNVMyUJO1f+1VrHVrUyolT54DmfMjUxPIvyC5u6wszhGZp3g5q5PsMlvs3FB 7+hW+wtJg+VwpKFDPkeUrfkW+g2m0i++pXAvMWgX0gtHqZTs/NeFmrcextm7p8fQn19eOaIswU6pq 66JHJLYBM3COZm5s00vnybvC4kRqZsHECgo0TA8cYnN6l/6WDl4vv5s1JOJ0huMZbXxD6w9Ep78pZ aPmomKRA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cjjF4-00037L-1L; Fri, 03 Mar 2017 09:11:14 +0000 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cjj9V-0006IJ-54; Fri, 03 Mar 2017 09:05:31 +0000 Received: by mail-wm0-x243.google.com with SMTP id u63so2051533wmu.2; Fri, 03 Mar 2017 01:05:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=0H3EQhf4QFsS5XWFJiAAk8XeqT5lV0KYQUW+K+GTcO0=; b=RZzJRek0QGk62RwnJsvgW79WrMKxeqSFI+FDHp5aA9tFs5heg+AQJkboANZ9cljsJw Ceugt+oYxhnGTmlhoNsPBTjYefbg2aJcEZY6NTsBK+ZOrTvEqYJYF+qaXYjlxtmqjXRO rjLwQbBCUsOAFxsqximV1mVusbMwFzASdS+2xDndzP3l8puh/5jt6RQvHR49UUcQPBuy pQ7U1kmXIyoQ2RJSYmBbwuakRduMXwK0aRuYCDkayg+BRGFK/+CL/SqZrpkrPQaCecOh D7a2R9C666Et1T4ufouZH524GlPKkKwmNKwz1aZnX1IKEytne+ax3HNl9jUcsQM/Myai 57mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=0H3EQhf4QFsS5XWFJiAAk8XeqT5lV0KYQUW+K+GTcO0=; b=esYnD9IHPuoS1TXHrXlsvtgmyPffTFgDdwcEA4LX36NxiPHUlAhIuMrz9GoAbteBRs wvBhcA8B5kcfLzGOAdOBiYAr8ex0K1JxQUjrLRZi+H+G4hP4JciLaPGGnZngjnguaqY0 574glc1adCO2wZAOfmRSdk2MMk3dpdZ/duTUJ6BI8pVtIjBZkLK7nX8uQvhF+8C8hLv6 jslTHRuRE034qShzSH5JjBH2prXKEeJ6nmHFrF6PaWlm274+kzzFbVkfJBFDKSItPu8r rk9o6yVNTgSpOU300tsiM6CaR1kfnvFI+Vcx2Tf7MpuEADRZcrj/1LXw6Fv5ew5IsgfJ wcQA== X-Gm-Message-State: AMke39m22ngcCEOF0sGDwcpJFTMzhRlsp9CDS7wVbD8gAUGChk3MJAwfRTca6jmeOSXZSA== X-Received: by 10.28.111.136 with SMTP id c8mr1604186wmi.128.1488531907363; Fri, 03 Mar 2017 01:05:07 -0800 (PST) Received: from satan.homestation.setup ([151.45.79.45]) by smtp.gmail.com with ESMTPSA id z198sm2189747wmz.24.2017.03.03.01.05.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Mar 2017 01:05:06 -0800 (PST) From: Carlo Caione To: jdelvare@suse.com, linux@roeck-us.net, linux-hwmon@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-amlogic@lists.infradead.org, linux@endlessm.com, punit.agrawal@arm.com, sudeep.holla@arm.com, robh+dt@kernel.org Subject: [PATCH v2 2/2] hwmon: (scpi) Fix the scale of SCP sensor readings Date: Fri, 3 Mar 2017 10:14:49 +0100 Message-Id: <20170303091449.3512-3-carlo@caione.org> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170303091449.3512-1-carlo@caione.org> References: <20170303091449.3512-1-carlo@caione.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170303_010529_386192_3AC03B5A X-CRM114-Status: GOOD ( 15.50 ) 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: Carlo Caione MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Carlo Caione The implementation details for SCPI seems to suggest that the sensor readings must be reported by SCP using a well defined scale (millidegree Celsius for temperature, millivolts for voltage, milliamperes for current, microwatts for power and microjoules for energy). This is also important for the interaction with other subsystems: for example both the thermal sub-system and the hwmon sysfs interface expect the temperature expressed in millidegree Celsius. Unfortunately since this behaviour is dependent on the firmware implementation there are cases where the sensor readings are reported using a different scale. For example in the Amlogic SoCs the temperature is reported in degree and not millidegree Celsius. To take into account this discrepancy and fixup the values reported by SCP a new DT property `scpi,sensors-scale' is introduced and used in this patch by the scpi-hwmon driver to convert the sensor readings to the expected scale. Signed-off-by: Carlo Caione Acked-by: Punit Agrawal --- drivers/hwmon/scpi-hwmon.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/drivers/hwmon/scpi-hwmon.c b/drivers/hwmon/scpi-hwmon.c index 094f948f99ff..b64fe7d10742 100644 --- a/drivers/hwmon/scpi-hwmon.c +++ b/drivers/hwmon/scpi-hwmon.c @@ -23,6 +23,7 @@ #include struct sensor_data { + unsigned int scale; struct scpi_sensor_info info; struct device_attribute dev_attr_input; struct device_attribute dev_attr_label; @@ -44,6 +45,22 @@ struct scpi_sensors { const struct attribute_group *groups[2]; }; +static const u32 scpi_scale[] = { + [TEMPERATURE] = 1000, /* (millicelsius) */ + [VOLTAGE] = 1000, /* (millivolts) */ + [CURRENT] = 1000, /* (milliamperes) */ + [POWER] = 1000000, /* (microwatts) */ + [ENERGY] = 1000000, /* (microjoules) */ +}; + +void scpi_scale_reading(u64 *value, struct sensor_data *sensor) +{ + if (scpi_scale[sensor->info.class] != sensor->scale) { + *value *= scpi_scale[sensor->info.class]; + do_div(*value, sensor->scale); + } +} + static int scpi_read_temp(void *dev, int *temp) { struct scpi_thermal_zone *zone = dev; @@ -57,6 +74,8 @@ static int scpi_read_temp(void *dev, int *temp) if (ret) return ret; + scpi_scale_reading(&value, sensor); + *temp = value; return 0; } @@ -77,6 +96,8 @@ scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf) if (ret) return ret; + scpi_scale_reading(&value, sensor); + return sprintf(buf, "%llu\n", value); } @@ -97,6 +118,7 @@ static struct thermal_zone_of_device_ops scpi_sensor_ops = { static int scpi_hwmon_probe(struct platform_device *pdev) { u16 nr_sensors, i; + u32 scale[] = { 1000, 1000, 1000, 1000000, 1000000 }; int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0; int num_energy = 0; struct scpi_ops *scpi_ops; @@ -131,6 +153,16 @@ static int scpi_hwmon_probe(struct platform_device *pdev) scpi_sensors->scpi_ops = scpi_ops; + of_property_read_u32_array(dev->of_node, "scpi,sensors-scale", + scale, ARRAY_SIZE(scale)); + + for (i = 0; i < ARRAY_SIZE(scale); i++) { + if (!scale[i]) { + dev_err(dev, "%s: scale cannot be zero (%d)\n", __func__, i); + return -EINVAL; + } + } + for (i = 0, idx = 0; i < nr_sensors; i++) { struct sensor_data *sensor = &scpi_sensors->data[idx]; @@ -178,6 +210,8 @@ static int scpi_hwmon_probe(struct platform_device *pdev) continue; } + sensor->scale = scale[sensor->info.class]; + sensor->dev_attr_input.attr.mode = S_IRUGO; sensor->dev_attr_input.show = scpi_show_sensor; sensor->dev_attr_input.attr.name = sensor->input;