hwmon: scmi: Scale values to target desired HWMON units
Subsystems (2)
cover hwmon: scmi: Scale values to target desired HWMON units
Hi Sudeep, Guenter,
This patch series adds support for scaling SCMI sensor values read from firmware. Sudeep, let me know if you think we should be treating scale == 0 as a special value to preserve some firmware compatibility (not that this would be desired).
Thanks!
Florian Fainelli (2):
firmware: arm_scmi: Fetch and store sensor scale
hwmon: scmi: Scale values to target desired HWMON units
drivers/firmware/arm_scmi/sensors.c | 7 +++- drivers/hwmon/scmi-hwmon.c | 55 ++++++++++++++++++++++++----- include/linux/scmi_protocol.h | 1 + 3 files changed, 53 insertions(+), 10 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/2 firmware: arm_scmi: Fetch and store sensor scale
In preparation for dealing with scales within the SCMI HWMON driver, fetch and store the sensor unit scale into the scmi_sensor_info structure.
--- drivers/firmware/arm_scmi/sensors.c | 7 ++++++- include/linux/scmi_protocol.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)
drivers/firmware/arm_scmi/sensors.c +6 -1
@@ -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 ];
@@ -140,6 +141,10 @@ 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); +/* Sign extend to a full u8 */ +if (s->scale & ((SENSOR_SCALE_MASK +1 ) >>1 )) + s->scale |=GENMASK (7 ,6 );strlcpy (s->name, buf->desc[cnt].name,SCMI_MAX_STR_SIZE ); }
include/linux/scmi_protocol.h +1 -0
@@ -144,6 +144,7 @@ struct scmi_power_ops {struct scmi_sensor_info {u32 id;u8 type; +u8 scale;char name[SCMI_MAX_STR_SIZE ]; };
Signature · 1 line
2.17.1
Guenter Roeck
maintainerRe: [PATCH 1/2] firmware: arm_scmi: Fetch and store sensor scale
Quoted 25 lines · Florian Fainelli
In preparation for dealing with scales within the SCMI HWMON driver, fetch and store the sensor unit scale into the
scmi_sensor_infostructure.Signed-off-by Florian Fainelli <[email protected]>--- drivers/firmware/arm_scmi/sensors.c | 7 ++++++- include/linux/scmi_protocol.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)drivers/firmware/arm_scmi/sensors.c +2
-1@@ -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 ];
drivers/firmware/arm_scmi/sensors.c static int scmi_sensor_description_get(const struct scmi_handle *handle, · line 146
Quoted 6 lines
drivers/firmware/arm_scmi/sensors.c +3
-0@@ -140,6 +141,10 @@ 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); +/* Sign extend to a full u8 */ +if (s->scale & ((SENSOR_SCALE_MASK +1 ) >>1 ))
The logic here is quite confusing. I think it would be better to define, say, SENSOR_SCALE_SIGN and use it.
include/linux/scmi_protocol.h struct scmi_power_ops { · line 147
Quoted 13 lines
drivers/firmware/arm_scmi/sensors.c +1
-0+ s->scale |=GENMASK (7 ,6 );strlcpy (s->name, buf->desc[cnt].name,SCMI_MAX_STR_SIZE ); }include/linux/scmi_protocol.h +1
-0@@ -144,6 +144,7 @@ struct scmi_power_ops {struct scmi_sensor_info {u32 id;u8 type; +u8 scale;
Why not s8 if this is a signed value ?
Thanks,
Guenter
Quoted 4 lines
include/linux/scmi_protocol.h +0
-0char name[SCMI_MAX_STR_SIZE ]; };
PATCH 2/2 hwmon: scmi: Scale values to target desired HWMON units
In reply to Florian FainelliIf the SCMI firmware implementation is reporting values in a scale that is different from the HWMON units, we need to scale up or down the value according to how far appart they are.
--- drivers/hwmon/scmi-hwmon.c | 55 +++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-)
drivers/hwmon/scmi-hwmon.c +46 -9
@@ -18,6 +18,51 @@ struct scmi_sensors {const struct scmi_sensor_info **info[hwmon_max]; }; +static enum hwmon_sensor_types scmi_types[] = { + [TEMPERATURE_C ] = hwmon_temp, + [VOLTAGE ] = hwmon_in, + [CURRENT ] = hwmon_curr, + [POWER ] = hwmon_power, + [ENERGY ] = hwmon_energy, +}; + +static u64 scmi_hwmon_scale (const struct scmi_sensor_info *sensor,u64 value) +{ +u64 scaled_value = value; +s8 desired_scale; +int n, p; + +switch (sensor->type) { +case TEMPERATURE_C : +case VOLTAGE : +case CURRENT : +/* fall through */ + desired_scale =-3 ; +break ; +case POWER : +case ENERGY : +/* fall through */ + desired_scale =-6 ; +break ; +default : +return scaled_value; + } + + n = (s8 )sensor->scale - desired_scale; +if (n ==0 ) +return scaled_value; + +for (p =0 ; p <abs (n); p++) { +/* Need to scale up from sensor to HWMON */ +if (n >0 ) + scaled_value *=10 ; +else +do_div (scaled_value,10 ); + } + +return scaled_value; +} +static int scmi_hwmon_read (struct device *dev,enum hwmon_sensor_types type,u32 attr,int channel,long *val) {
@@ -30,7 +75,7 @@ static int scmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type, sensor = *(scmi_sensors->info[type] + channel); ret = h->sensor_ops->reading_get (h, sensor->id, false, &value);if (!ret) - *val = value; + *val =scmi_hwmon_scale (sensor, value);return ret; }
@@ -91,14 +136,6 @@ static int scmi_hwmon_add_chan_info(struct hwmon_channel_info *scmi_hwmon_chan,return 0 ; } -static enum hwmon_sensor_types scmi_types[] = { - [TEMPERATURE_C ] = hwmon_temp, - [VOLTAGE ] = hwmon_in, - [CURRENT ] = hwmon_curr, - [POWER ] = hwmon_power, - [ENERGY ] = hwmon_energy, -}; -static u32 hwmon_attributes[] = { [hwmon_chip] =HWMON_C_REGISTER_TZ , [hwmon_temp] =HWMON_T_INPUT |HWMON_T_LABEL ,
Signature · 1 line
2.17.1
Guenter Roeck
maintainerHi Florian,
Quoted 18 lines · Florian Fainelli
If the SCMI firmware implementation is reporting values in a scale that is different from the HWMON units, we need to scale up or down the value according to how far appart they are.
Signed-off-by Florian Fainelli <[email protected]>--- drivers/hwmon/scmi-hwmon.c | 55 +++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-)drivers/hwmon/scmi-hwmon.c +1
-0@@ -18,6 +18,51 @@ struct scmi_sensors {const struct scmi_sensor_info **info[hwmon_max]; }; +static enum hwmon_sensor_types scmi_types[] = {
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 31
Quoted 10 lines
drivers/hwmon/scmi-hwmon.c +10
-0+ [TEMPERATURE_C] = hwmon_temp, + [VOLTAGE] = hwmon_in, + [CURRENT] = hwmon_curr, + [POWER] = hwmon_power, + [ENERGY] = hwmon_energy, +}; + +static u64 scmi_hwmon_scale (const struct scmi_sensor_info *sensor,u64 value) +{ +u64 scaled_value = value;
I don't think that variable is necessary.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 32
Quoted 1 line
drivers/hwmon/scmi-hwmon.c +1
-0+s8 desired_scale;
Just scale ? Also, you could assign scale here directly, and subtract the offset below. Then "n" would not be necessary.
Such as
s8 scale = sensor->scale; // assuming scale is s8 ... case CURRENT: scale += 3; ...
That would also be less confusing, since it would avoid the double negation.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 39
Quoted 8 lines
drivers/hwmon/scmi-hwmon.c +7
-0+int n, p; + +switch (sensor->type) { +case TEMPERATURE_C : +case VOLTAGE : +case CURRENT : +/* fall through */
Unnecessary comment
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 44
Quoted 5 lines
drivers/hwmon/scmi-hwmon.c +5
-0+ desired_scale =-3 ; +break ; +case POWER : +case ENERGY : +/* fall through */
Unnecessary comment.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 48
Quoted 4 lines
drivers/hwmon/scmi-hwmon.c +4
-0+ desired_scale =-6 ; +break ; +default : +return scaled_value;
Here we presumably want a scale of 0. However, if the scale passed from SCMI is, say, -5 or +5, we return the original (unadjusted) value. Seems to me we would still want to adjust the value to match hwmon expectations. Am I missing something ?
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 52
Quoted 4 lines
drivers/hwmon/scmi-hwmon.c +4
-0+ } + + n = (s8 )sensor->scale - desired_scale; +if (n ==0 )
Indentation seems off here.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 61
Quoted 9 lines
drivers/hwmon/scmi-hwmon.c +9
-0+return scaled_value; + +for (p =0 ; p <abs (n); p++) { +/* Need to scale up from sensor to HWMON */ +if (n >0 ) + scaled_value *=10 ; +else +do_div (scaled_value,10 ); + }
Something like
factor = pow10(abs(scale)); if (scale > 0) value *= factor; else do_div(value, factor);
would avoid the repeated abs() and do_div(). Unfortunately there is no pow10() helper, so you would have to write that. Still, I think that would be much more efficient.
Thanks,
Guenter
Quoted 32 lines
drivers/hwmon/scmi-hwmon.c +4
-0+ +return scaled_value; +} +static int scmi_hwmon_read (struct device *dev,enum hwmon_sensor_types type,u32 attr,int channel,long *val){drivers/hwmon/scmi-hwmon.c +1
-9@@ -30,7 +75,7 @@ static int scmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type, sensor = *(scmi_sensors->info[type] + channel); ret = h->sensor_ops->reading_get (h, sensor->id, false, &value);if (!ret) - *val = value; + *val =scmi_hwmon_scale (sensor, value);return ret; }@@ -91,14 +136,6 @@ static int scmi_hwmon_add_chan_info(struct hwmon_channel_info *scmi_hwmon_chan,return 0 ; } -static enum hwmon_sensor_types scmi_types[] = { - [TEMPERATURE_C ] = hwmon_temp, - [VOLTAGE ] = hwmon_in, - [CURRENT ] = hwmon_curr, - [POWER ] = hwmon_power, - [ENERGY ] = hwmon_energy, -}; -static u32 hwmon_attributes[] = { [hwmon_chip] =HWMON_C_REGISTER_TZ , [hwmon_temp] =HWMON_T_INPUT |HWMON_T_LABEL ,
Quoted 50 lines · Guenter Roeck
Hi Florian,
Quoted 18 lines · Florian Fainelli
If the SCMI firmware implementation is reporting values in a scale that is different from the HWMON units, we need to scale up or down the value according to how far appart they are.
Signed-off-by Florian Fainelli <[email protected]>---
drivers/hwmon/scmi-hwmon.c| 55 +++++++++++++++++++++++++++++++-------1 file changed, 46 insertions(+), 9 deletions(-)drivers/hwmon/scmi-hwmon.c +1
-0@@ -18,6 +18,51 @@ struct scmi_sensors { const struct scmi_sensor_info **info[hwmon_max]; }; +static enum hwmon_sensor_types scmi_types[] = { + [TEMPERATURE_C] = hwmon_temp,drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 31
Quoted 10 lines
drivers/hwmon/scmi-hwmon.c +9
-0+ [VOLTAGE] = hwmon_in, + [CURRENT] = hwmon_curr, + [POWER] = hwmon_power, + [ENERGY] = hwmon_energy, +}; + +static u64 scmi_hwmon_scale (const struct scmi_sensor_info *sensor,u64 value) +{ + u64 scaled_value = value;I don't think that variable is necessary.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 32
Quoted 1 line
drivers/hwmon/scmi-hwmon.c +1
-0+ s8 desired_scale;Just scale ? Also, you could assign scale here directly, and subtract the offset below. Then "n" would not be necessary.
Such as
s8 scale = sensor->scale; // assuming scale is s8
...
case CURRENT:
scale += 3;
...That would also be less confusing, since it would avoid the double negation.
Quoted 1 line
drivers/hwmon/scmi-hwmon.c +1
-0+ int n, p;
Quoted 7 lines
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 39
Quoted 6 lines
drivers/hwmon/scmi-hwmon.c +6
-0+ + switch (sensor->type) { + case TEMPERATURE_C: + case VOLTAGE: + case CURRENT: + /* fall through */Unnecessary comment
Is not removing the comment going to upset gcc when using -Wimplicit-fallthrough?
Quoted 13 lines
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 44
Quoted 6 lines
drivers/hwmon/scmi-hwmon.c +5
-0+ desired_scale = -3; + break; + case POWER: + case ENERGY: + /* fall through */Unnecessary comment.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 48
Quoted 4 lines
drivers/hwmon/scmi-hwmon.c +4
-0+ desired_scale = -6; + break; + default: + return scaled_value;
Quoted 4 lines
Here we presumably want a scale of 0. However, if the scale passed from SCMI is, say, -5 or +5, we return the original (unadjusted) value. Seems to me we would still want to adjust the value to match hwmon expectations. Am I missing something ?
You raise a valid point, not that could happen today because if the sensor type has a value we don't recognize, we have not registered it, so we would not even try to read rom it, but let's be future proof.
Quoted 26 lines
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 52
Quoted 5 lines
drivers/hwmon/scmi-hwmon.c +4
-0+ } + + n = (s8)sensor->scale - desired_scale; + if (n == 0)Indentation seems off here.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 61
Quoted 9 lines
drivers/hwmon/scmi-hwmon.c +9
-0+ return scaled_value; + + for (p = 0; p < abs(n); p++) { + /* Need to scale up from sensor to HWMON */ + if (n > 0) + scaled_value *= 10; + else + do_div(scaled_value, 10); + }Something like
factor = pow10(abs(scale));
if (scale > 0)
value *= factor;
else
do_div(value, factor);
Quoted 3 lines
would avoid the repeated
abs()anddo_div(). Unfortunately there is nopow10()helper, so you would have to write that. Still, I think that would be much more efficient.
Sounds reasonable. Thanks for your feedback!
Signature · 1 line
Florian
Guenter Roeck
maintainerHi Florian,
Quoted 57 lines · Florian Fainelli
Quoted 56 lines · Guenter Roeck
Hi Florian,
Quoted 18 lines · Florian Fainelli
If the SCMI firmware implementation is reporting values in a scale that is different from the HWMON units, we need to scale up or down the value according to how far appart they are.
Signed-off-by Florian Fainelli <[email protected]>---
drivers/hwmon/scmi-hwmon.c| 55 +++++++++++++++++++++++++++++++-------1 file changed, 46 insertions(+), 9 deletions(-)drivers/hwmon/scmi-hwmon.c +1
-0@@ -18,6 +18,51 @@ struct scmi_sensors { const struct scmi_sensor_info **info[hwmon_max]; }; +static enum hwmon_sensor_types scmi_types[] = { + [TEMPERATURE_C] = hwmon_temp,drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 31
Quoted 10 lines
drivers/hwmon/scmi-hwmon.c +9
-0+ [VOLTAGE] = hwmon_in, + [CURRENT] = hwmon_curr, + [POWER] = hwmon_power, + [ENERGY] = hwmon_energy, +}; + +static u64 scmi_hwmon_scale (const struct scmi_sensor_info *sensor,u64 value) +{ + u64 scaled_value = value;I don't think that variable is necessary.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 32
Quoted 1 line
drivers/hwmon/scmi-hwmon.c +1
-0+ s8 desired_scale;Just scale ? Also, you could assign scale here directly, and subtract the offset below. Then "n" would not be necessary.
Such as
s8 scale = sensor->scale; // assuming scale is s8
...
case CURRENT:
scale += 3;
...That would also be less confusing, since it would avoid the double negation.
Quoted 8 lines
drivers/hwmon/scmi-hwmon.c +7
-0+ int n, p; + + switch (sensor->type) { + case TEMPERATURE_C: + case VOLTAGE: + case CURRENT: + /* fall through */
Quoted 5 lines
Quoted 1 line
Unnecessary comment
Is not removing the comment going to upset gcc when using -Wimplicit-fallthrough?
There is no implicit fallthrough, and the comment would have to be ahead of the previous case statement. Such as:
case VOLTAGE: scale++; /* fall through */ case CURRENT: scale++; break; ...
Two case statements together don't count as fall through.
Guenter
Quoted 55 lines
Quoted 13 lines
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 44
Quoted 6 lines
drivers/hwmon/scmi-hwmon.c +5
-0+ desired_scale = -3; + break; + case POWER: + case ENERGY: + /* fall through */Unnecessary comment.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 48
Quoted 4 lines
drivers/hwmon/scmi-hwmon.c +4
-0+ desired_scale = -6; + break; + default: + return scaled_value;Quoted 4 lines
Here we presumably want a scale of 0. However, if the scale passed from SCMI is, say, -5 or +5, we return the original (unadjusted) value. Seems to me we would still want to adjust the value to match hwmon expectations. Am I missing something ?
You raise a valid point, not that could happen today because if the sensor type has a value we don't recognize, we have not registered it, so we would not even try to read rom it, but let's be future proof.
Quoted 26 lines
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 52
Quoted 5 lines
drivers/hwmon/scmi-hwmon.c +4
-0+ } + + n = (s8)sensor->scale - desired_scale; + if (n == 0)Indentation seems off here.
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 61
Quoted 9 lines
drivers/hwmon/scmi-hwmon.c +9
-0+ return scaled_value; + + for (p = 0; p < abs(n); p++) { + /* Need to scale up from sensor to HWMON */ + if (n > 0) + scaled_value *= 10; + else + do_div(scaled_value, 10); + }Something like
factor = pow10(abs(scale));
if (scale > 0)
value *= factor;
else
do_div(value, factor);Quoted 3 lines
would avoid the repeated
abs()anddo_div(). Unfortunately there is nopow10()helper, so you would have to write that. Still, I think that would be much more efficient.Sounds reasonable. Thanks for your feedback!
Signature · 1 line
Florian