From patchwork Thu Mar 9 10:17:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Caione X-Patchwork-Id: 9613111 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 493D5602B4 for ; Thu, 9 Mar 2017 11:25:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4350F285E8 for ; Thu, 9 Mar 2017 11:25:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 377CA285F5; Thu, 9 Mar 2017 11:25:57 +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=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=unavailable 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 51528285E8 for ; Thu, 9 Mar 2017 11:25:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754556AbdCILZ4 (ORCPT ); Thu, 9 Mar 2017 06:25:56 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:34869 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752930AbdCILZz (ORCPT ); Thu, 9 Mar 2017 06:25:55 -0500 Received: by mail-wm0-f65.google.com with SMTP id z63so10420073wmg.2; Thu, 09 Mar 2017 03:25:53 -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=voPhMxn11W+Gl9GnozgEYqEyOloNdx933CmXoXu7lTQ=; b=bKsyTHS6+ZdQ0hpl2WYew59nIfwk8vuKCDK0qY0kBpKI2B+yuksrfv8RLTYgeqXIOD dTYWhJ40AhaAqNnq0XE5KRMd+HELvQU+hpOGp99j+I9yz45OANnt2MsDnf8OmmsPs5uF jTj7QraCOn7OvPFqaHu6hMkuFBQn9NC00A4PAVJxlmPGtUc6YzLsFEcjUNKX15PlxvCi QMJWqzQ3RNYEaY5/ogbzHZE8mM7KwtobuFZ2TG38HLVVUj+1AeB56OMOHVulkDOmqw90 tXYw5kPIflininQ044ic3P3uv9IQ7PSbIqtMz38et6P6i2odBlo2lOuFGZr5vT7X+1os qblQ== 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=voPhMxn11W+Gl9GnozgEYqEyOloNdx933CmXoXu7lTQ=; b=jFIVyRQifDoiSyzRDbwaHCehm90NCUod8FSXfn3SLJDJWoxj6/S3dGVGCXfCo4tHXa bGoV4EO8/y/1dWi/BKOzOwQmIlY2n0aulLiYefATwK2HVb0FigIMBr8VwUfVQITCAQA9 opESL/r0+CYJL4BMcOiUKHO437dRAg57NfcDEe1DfW4hKEXqBO8NO8tMr8MHQIyAeIfD QMhMYmp0gCoYWCwY7M2uydN4L4op9x2cyGkIyGQ1XFpf382G98st3uiXPV8QrbZHImvi xFyPMixXez26oKaA0aTFcVDfyqJ2f4s1Ul4TDPlWebE8DWxgRSZExZX27Zkng4+pNo4S +kXQ== X-Gm-Message-State: AMke39nvzjyHIGz1Xq2nVzTos4otnZACKuo291y/kT9u6AAKaSAvnTLfhELExsiB6VK2Xw== X-Received: by 10.28.170.4 with SMTP id t4mr10069459wme.89.1489054709799; Thu, 09 Mar 2017 02:18:29 -0800 (PST) Received: from localhost.localdomain (host106-8-dynamic.15-87-r.retail.telecomitalia.it. [87.15.8.106]) by smtp.gmail.com with ESMTPSA id t30sm7660091wra.52.2017.03.09.02.18.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Mar 2017 02:18:29 -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, devicetree@vger.kernel.org Cc: Carlo Caione Subject: [PATCH v3 2/2] hwmon: (scpi) Fix the scale of SCP sensor readings Date: Thu, 9 Mar 2017 11:17:59 +0100 Message-Id: <20170309101759.18081-3-carlo@caione.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170309101759.18081-1-carlo@caione.org> References: <20170309101759.18081-1-carlo@caione.org> 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 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. Acked-by: Punit Agrawal Signed-off-by: Carlo Caione --- 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 094f948..4a4b8a4 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) */ +}; + +static 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;