From patchwork Tue May 30 09:05:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Caione X-Patchwork-Id: 9754129 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 A732C602F0 for ; Tue, 30 May 2017 09:05:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9AA4827F10 for ; Tue, 30 May 2017 09:05:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8F323283C0; Tue, 30 May 2017 09:05:22 +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=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 14D6F27F10 for ; Tue, 30 May 2017 09:05:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750985AbdE3JFV (ORCPT ); Tue, 30 May 2017 05:05:21 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:33228 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750929AbdE3JFU (ORCPT ); Tue, 30 May 2017 05:05:20 -0400 Received: by mail-wm0-f67.google.com with SMTP id b84so23621497wmh.0; Tue, 30 May 2017 02:05:19 -0700 (PDT) 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=dCzfwp+1IVypSXmO0rwF1gGgkpjy3pCY7fU2WE5ft4I=; b=Gw4U3ILpxWMpmx67deIEJ5DbPKA7VQvw7aS4jNJTzumD+Dwklmgm4zWuZhD0LE9SSN o3pUlDnTrNgue2wOB7dwjB3DlyBNX4sos/wDwenSxLnsp6zndWEz0HZ/4Zk68On+Ms4X Qe/hnYwLaH6hScl1TesYTEIRjpeYKQkN2kt/RCGsz83gNCbKn3SJEuuuT9aKmoVWdwhX f1Wn3cCshVi0/hQu/EZdg5+Eme6D2JgnPUFkfnBWbH6Nz1MX0rpXER8sQIHKYQ6GpVO2 ZpbcpwZ4UTQtTOqkeulNtaJ9htF8a4G9LqjRZzYHWCEgfyP0O/42Z5N7r8XXKQ14pVF+ q0RQ== 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=dCzfwp+1IVypSXmO0rwF1gGgkpjy3pCY7fU2WE5ft4I=; b=DPkedMlK/gmY9kvpySXVWnpdVd5i43HDdm0FVSDLWaINDyIxWGLiQ7XxV9Bpe/J3qz lMgdsQbsmOSCfuvq/eyK7V2haomO4+G7EC/RI/ZKrx3EEBy4jtqdZ8t0GhLhF+6y2uPl mgLZm2GijZa9EQEW9U2UpdAJnhR3hfgBkAf/hTl6jJo/xVuGG5t1VQpCb6zhWrDlo/nv Nqid57tJ/9nhikXhR2SmXsyD/7pKhlARff02h4WRuujnH5YkKQUlBcmYxMm3c0lYWat7 h6wfEgNeNdGRiuKYFBXUyDAMeeh8G2q/CMLff6DXxz/muBa07sx6Ee0PNZiMsM1Ucv1K FhTA== X-Gm-Message-State: AODbwcBvIvIacm7cLnE9aAyPVR7mhLTkaycJWu3fZsg7QKg5/rPInvOw wWNqOPHeIjjT5g== X-Received: by 10.223.138.139 with SMTP id y11mr15202650wry.22.1496135119011; Tue, 30 May 2017 02:05:19 -0700 (PDT) Received: from localhost.localdomain (host171-33-dynamic.248-95-r.retail.telecomitalia.it. [95.248.33.171]) by smtp.gmail.com with ESMTPSA id n49sm11181117wrn.30.2017.05.30.02.05.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2017 02:05:18 -0700 (PDT) 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 v4 2/2] hwmon: (scpi) Fix the scale of SCP sensor readings Date: Tue, 30 May 2017 11:05:07 +0200 Message-Id: <20170530090507.16611-3-carlo@caione.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170530090507.16611-1-carlo@caione.org> References: <20170530090507.16611-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 compatible 'amlogic,meson-gxbb-scpi-sensors' 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: Sudeep Holla --- drivers/hwmon/scpi-hwmon.c | 54 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/scpi-hwmon.c b/drivers/hwmon/scpi-hwmon.c index 094f948..a586480 100644 --- a/drivers/hwmon/scpi-hwmon.c +++ b/drivers/hwmon/scpi-hwmon.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -23,6 +24,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 +46,30 @@ struct scpi_sensors { const struct attribute_group *groups[2]; }; +static const u32 gxbb_scpi_scale[] = { + [TEMPERATURE] = 1, /* (celsius) */ + [VOLTAGE] = 1000, /* (millivolts) */ + [CURRENT] = 1000, /* (milliamperes) */ + [POWER] = 1000000, /* (microwatts) */ + [ENERGY] = 1000000, /* (microjoules) */ +}; + +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 +83,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 +105,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); } @@ -94,14 +124,23 @@ static struct thermal_zone_of_device_ops scpi_sensor_ops = { .get_temp = scpi_read_temp, }; +static const struct of_device_id scpi_of_match[] = { + {.compatible = "arm,scpi-sensors", .data = &scpi_scale}, + {.compatible = "amlogic,meson-gxbb-scpi-sensors", .data = &gxbb_scpi_scale}, + {}, +}; +MODULE_DEVICE_TABLE(of, scpi_of_match); + static int scpi_hwmon_probe(struct platform_device *pdev) { u16 nr_sensors, i; + const u32 *scale; int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0; int num_energy = 0; struct scpi_ops *scpi_ops; struct device *hwdev, *dev = &pdev->dev; struct scpi_sensors *scpi_sensors; + const struct of_device_id *of_id; int idx, ret; scpi_ops = get_scpi_ops(); @@ -131,6 +170,13 @@ static int scpi_hwmon_probe(struct platform_device *pdev) scpi_sensors->scpi_ops = scpi_ops; + of_id = of_match_device(scpi_of_match, &pdev->dev); + if (!of_id) { + dev_err(&pdev->dev, "Unable to initialize scpi-hwmon data\n"); + return -ENODEV; + } + scale = of_id->data; + for (i = 0, idx = 0; i < nr_sensors; i++) { struct sensor_data *sensor = &scpi_sensors->data[idx]; @@ -178,6 +224,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; @@ -247,12 +295,6 @@ static int scpi_hwmon_probe(struct platform_device *pdev) return 0; } -static const struct of_device_id scpi_of_match[] = { - {.compatible = "arm,scpi-sensors"}, - {}, -}; -MODULE_DEVICE_TABLE(of, scpi_of_match); - static struct platform_driver scpi_hwmon_platdrv = { .driver = { .name = "scpi-hwmon",