From patchwork Thu Mar 2 12:15:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Caione X-Patchwork-Id: 9600123 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 7B8B160453 for ; Thu, 2 Mar 2017 12:07:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 696F728599 for ; Thu, 2 Mar 2017 12:07:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D8C4285A2; Thu, 2 Mar 2017 12:07:17 +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 D7C8028599 for ; Thu, 2 Mar 2017 12:07:16 +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=KKppZBuKlubfY7z/zVbtzyXWkx1HRw6RJFFfQtnQCKc=; b=lBzII+yXxNRp6QU9k1xkYAiORy Xb0+BakqlYSY5MLKuIOvpavCV0tA+eLV6k90q+kRdXentb+n3emy9Y7pVX5jLfpDuSdsERqfbSb4i 0OK8e99B/AYZxeXKdfqpX/ShXWlHQanFf+XF4xTWBuodkTHIkj6IATxG/WHulFnUWohPBxwVq+2Iz 4TiM3BW5ngwV6Tg5ZKjl9Xw5hoxQhM+lr4EYX3fEQo4doMG3B3j2r/4KhI9d9I+VrSBWxdw2W5hBY 7slcFRzXbNV6o12/vh/FMACna3Q2gZrEPDXUUnHYTR/1ngxabjREisEZbtsmJwCUWnIDOP+bFTnf5 E+rP+rCA==; 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 1cjPVj-0003TP-G9; Thu, 02 Mar 2017 12:07:07 +0000 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cjPUK-0002Um-Iu; Thu, 02 Mar 2017 12:05:53 +0000 Received: by mail-wr0-x242.google.com with SMTP id g10so9199272wrg.0; Thu, 02 Mar 2017 04:05:20 -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=OVNP1uLazeVAYhx1eG37GVvuDB/PxLMlxnIdJkDmZOw=; b=R4l/auU6AE+VA0Me1CcLxWhwDdmonU++rMTYp81J4JUacIucWw6/QrBx7wFd+nhw/c 21i/+6gbh4W0MxPN53GpdAQEaGDkxuXlTP/bmM9l41JCtWNm8owkH0q9GcjxlHegjTik 8EmvA2QszC3RyRR7TrvmTNemN99vZnOP2cjbVm75gZ8TMH67y0CfR4GmhT1NGNHWciBr 215AJ30yzw5+Er9rKbHTYNhYgODXlwFX+iNV2pBnr52aZ21c0PKtnGovRNQ+jf+CDWUc yhVsdu9XN/ubJaUeNBMtJtB0MQLSOJMjsmcL5I+zQzKQd5OF+mRPnMZPid7oqfsAGk6E tyNA== 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=OVNP1uLazeVAYhx1eG37GVvuDB/PxLMlxnIdJkDmZOw=; b=UoXZW64y0WStiufm5O+EXmeeitklSe53re+fCNtgqrOf/iYbJEkzVaByjoXlWdMiRG ZcHgnTQ0EvdpBctZUdGKElCwQxwfgoOlHiAuLftIgJpHeM9p9mtzczsRtox8bokuJlJQ P3QJnJYJquBYsDOJEo9IjQELRTr5US0MaKnjhaZ0SKsPacXSPYMIAgt7nNWoLqylk2pt S4+ol7ybrMct0K7W52joQRZh+CMFndBNr1SWTZPSw0DuoNx75MUBkGG0Sfxv5OWM64Dt GL3dduH5H5bQbST+99w6nHEACDoJpLIq/UtxamTm5cVUyAxXzBfbfuqnEKRWkllPjqHJ YqTg== X-Gm-Message-State: AMke39m6LnjFPqR7hjiDf8WsT2uv0bvI/kF2+ehK/d8dGC1WNBAg4oYBjskFKTECS5Yoaw== X-Received: by 10.223.150.15 with SMTP id b15mr11772894wra.191.1488456318749; Thu, 02 Mar 2017 04:05:18 -0800 (PST) Received: from satan.homestation.setup ([151.45.79.45]) by smtp.gmail.com with ESMTPSA id 191sm10956689wmo.21.2017.03.02.04.05.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Mar 2017 04:05:18 -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 2/2] hwmon: (scpi) Fix the scale of SCP sensor readings Date: Thu, 2 Mar 2017 13:15:00 +0100 Message-Id: <20170302121500.8121-3-carlo@caione.org> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170302121500.8121-1-carlo@caione.org> References: <20170302121500.8121-1-carlo@caione.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170302_040541_402775_3301F73F X-CRM114-Status: GOOD ( 15.85 ) X-BeenThere: linux-amlogic@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-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=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 --- drivers/hwmon/scpi-hwmon.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/drivers/hwmon/scpi-hwmon.c b/drivers/hwmon/scpi-hwmon.c index 094f948f99ff..64a51e06c8df 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,14 @@ struct scpi_sensors { const struct attribute_group *groups[2]; }; +static const unsigned int scpi_scale[] = { + 1000, /* Temperature (millidegrees Celsius) */ + 1000, /* Voltage (millivolts) */ + 1000, /* Current (milliamperes) */ + 1000000, /* Power (microwatts) */ + 1000000, /* Energy (microjoules) */ +}; + static int scpi_read_temp(void *dev, int *temp) { struct scpi_thermal_zone *zone = dev; @@ -57,6 +66,11 @@ static int scpi_read_temp(void *dev, int *temp) if (ret) return ret; + if (scpi_scale[sensor->info.class] != sensor->scale) { + value *= scpi_scale[sensor->info.class]; + do_div(value, sensor->scale); + } + *temp = value; return 0; } @@ -77,6 +91,11 @@ scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf) if (ret) return ret; + if (scpi_scale[sensor->info.class] != sensor->scale) { + value *= scpi_scale[sensor->info.class]; + do_div(value, sensor->scale); + } + return sprintf(buf, "%llu\n", value); } @@ -97,6 +116,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 +151,9 @@ 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, idx = 0; i < nr_sensors; i++) { struct sensor_data *sensor = &scpi_sensors->data[idx]; @@ -178,6 +201,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;