linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* SCMI sensor reads unit scaling
@ 2019-04-20 16:43 Florian Fainelli
  2019-04-20 17:31 ` Guenter Roeck
  2019-04-23 13:06 ` Sudeep Holla
  0 siblings, 2 replies; 5+ messages in thread
From: Florian Fainelli @ 2019-04-20 16:43 UTC (permalink / raw)
  To: Sudeep Holla, Guenter Roeck, linux-hwmon,
	moderated list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE

Hi Sudeep, Guenter,

The current SCMI hwmon support does not seem to make use of the sensor
scale/unit as defined in the sensor replies (or the update scale for
that matter).

I came up with the patch below which gets the job done, but I am worried
about possibly breaking people's SCMI deployments and sensors reading
because they may have intentionally or not already decided to return a
value which is scaled the way Linux's hwmon expect it, and may, or may
not have populated a valid unit number in the sensor reply.

Ideally we should probably do two conversions:

- from within scmi_sensor_reading_get(), scale the value as indicated by
the reply
- from within scmi_hwmon_read(), take that scaled value and apply the
necessary conversion expected by Linux's HWMON conventions (e.g.:
reporting voltage as mV values)

What do you think?

---
From: Florian Fainelli <f.fainelli@gmail.com>
Subject: firmware: arm_scmi: Support sensor scaling

The SCMI sensor management protocol includes the following provision:

The power-of-10 multiplier in two’s-complement format that is applied to
the sensor unit specified by the SensorType field.

Add support for scaling the value returned based on what is provided by
the firmware. This requires us to be able to look up a sensor identifier
to its backing scmi_sensor_info structure and apply the necessary scale.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 drivers/firmware/arm_scmi/sensors.c | 37
++++++++++++++++++++++++++++++++++++-
 include/linux/scmi_protocol.h       |  1 +
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/arm_scmi/sensors.c
b/drivers/firmware/arm_scmi/sensors.c
index bbb469f..2eccc6a 100644
--- a/drivers/firmware/arm_scmi/sensors.c
+++ b/drivers/firmware/arm_scmi/sensors.c
@@ -33,7 +33,8 @@ struct scmi_msg_resp_sensor_description {
 #define NUM_TRIP_POINTS(x)	(((x) >> 4) & 0xff)
 		__le32 attributes_high;
 #define SENSOR_TYPE(x)		((x) & 0xff)
-#define SENSOR_SCALE(x)		(((x) >> 11) & 0x3f)
+#define SENSOR_SCALE_MASK	0x3f
+#define SENSOR_SCALE(x)		(((x) >> 11) & SENSOR_SCALE_MASK)
 #define SENSOR_UPDATE_SCALE(x)	(((x) >> 22) & 0x1f)
 #define SENSOR_UPDATE_BASE(x)	(((x) >> 27) & 0x1f)
 		    u8 name[SCMI_MAX_STR_SIZE];
@@ -72,6 +73,35 @@ struct sensors_info {
 	struct scmi_sensor_info *sensors;
 };

+static
+struct scmi_sensor_info *scmi_sensor_info_from_id(struct sensors_info *si,
+						  u32 sensor_id)
+{
+	struct scmi_sensor_info *s = NULL;
+	int i;
+
+	for (i = 0; i < si->num_sensors; i++) {
+		s = &si->sensors[i];
+		if (s->id == sensor_id)
+			return s;
+	}
+
+	return s;
+}
+
+static void inline scmi_scale_value(u8 scale, u64 *value)
+{
+	unsigned int i;
+
+	if (scale & ((SENSOR_SCALE_MASK + 1) >> 1)) {
+		for (i = 0; i < (~scale) + 1; i++)
+			*value /= 10;
+	} else {
+		for (i = 0; i < scale; i++)
+			*value *= 10;
+	}
+}
+
 static int scmi_sensor_attributes_get(const struct scmi_handle *handle,
 				      struct sensors_info *si)
 {
@@ -140,6 +170,7 @@ static int scmi_sensor_description_get(const struct
scmi_handle *handle,
 			s = &si->sensors[desc_index + cnt];
 			s->id = le32_to_cpu(buf->desc[cnt].id);
 			s->type = SENSOR_TYPE(attrh);
+			s->scale = SENSOR_SCALE(attrh);
 			memcpy(s->name, buf->desc[cnt].name, SCMI_MAX_STR_SIZE);
 		}

@@ -205,6 +236,8 @@ static int scmi_sensor_trip_point_set(const struct
scmi_handle *handle,
 static int scmi_sensor_reading_get(const struct scmi_handle *handle,
 				   u32 sensor_id, bool async, u64 *value)
 {
+	struct sensors_info *si = handle->sensor_priv;
+	struct scmi_sensor_info *s = scmi_sensor_info_from_id(si, sensor_id);
 	int ret;
 	struct scmi_xfer *t;
 	struct scmi_msg_sensor_reading_get *sensor;
@@ -225,6 +258,8 @@ static int scmi_sensor_reading_get(const struct
scmi_handle *handle,

 		*value = le32_to_cpu(*pval);
 		*value |= (u64)le32_to_cpu(*(pval + 1)) << 32;
+		if (s && s->scale)
+			scmi_scale_value(s->scale, value);
 	}

 	scmi_one_xfer_put(handle, t);
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index b458c87..782b53e 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -140,6 +140,7 @@ struct scmi_power_ops {
 struct scmi_sensor_info {
 	u32 id;
 	u8 type;
+	u8 scale;
 	char name[SCMI_MAX_STR_SIZE];
 };

-- 
-- 
Florian

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-04-23 13:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-20 16:43 SCMI sensor reads unit scaling Florian Fainelli
2019-04-20 17:31 ` Guenter Roeck
2019-04-22 18:31   ` Florian Fainelli
2019-04-22 19:29     ` Guenter Roeck
2019-04-23 13:06 ` Sudeep Holla

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).