hwmon: scmi: Scale values to target desired HWMON units
Subsystems (2)
v2 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).
Changes in v2:
- added a helper function in kernel.h: __pow10()
- made the scale in scmi_sensor_info an s8 type, added defines for checking the sign bit and sign extending with a mask
- simplify computations in hwmon driver
Florian Fainelli (3):
kernel: Provide a __pow10() function
firmware: arm_scmi: Fetch and store sensor scale
hwmon: scmi: Scale values to target desired HWMON units
drivers/firmware/arm_scmi/sensors.c | 6 ++++++ drivers/hwmon/scmi-hwmon.c | 30 ++++++++++++++++++++++++++++- include/linux/kernel.h | 11 +++++++++++ include/linux/scmi_protocol.h | 1 + 4 files changed, 47 insertions(+), 1 deletion(-)
Signature · 1 line
2.17.1
PATCH v2 1/3 kernel: Provide a __pow10() function
Provide a simple macro that can return the value of 10 raised to a positive integer. We are going to use this in order to scale units from firmware to HWMON.
--- include/linux/kernel.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/kernel.h +11 -0
@@ -294,6 +294,17 @@ static inline u32 reciprocal_scale(u32 val, u32 ep_ro)return (u32)(((u64 ) val * ep_ro) >>32 ); } +/* Return in f the value of 10 raise to the power x */ +#define __pow10 (x, f)( \ +{ \ + typeof(x) __x = abs(x); \ + f = 1; \ + while (__x--) \ + f *= 10; \ + f; \ +} \ +) +#if defined(CONFIG_MMU ) && \ (defined(CONFIG_PROVE_LOCKING ) || defined(CONFIG_DEBUG_ATOMIC_SLEEP ))#define might_fault () __might_fault(__FILE__, __LINE__)
Signature · 1 line
2.17.1
Guenter Roeck
maintainerRe: [PATCH v2 1/3] kernel: Provide a __pow10() function
Quoted 17 lines · Florian Fainelli
Provide a simple macro that can return the value of 10 raised to a positive integer. We are going to use this in order to scale units from firmware to HWMON.
Signed-off-by Florian Fainelli <[email protected]>--- include/linux/kernel.h | 11 +++++++++++ 1 file changed, 11 insertions(+)include/linux/kernel.h +0
-0@@ -294,6 +294,17 @@ static inline u32 reciprocal_scale(u32 val, u32 ep_ro)return (u32)(((u64 ) val * ep_ro) >>32 ); }
include/linux/kernel.h static inline u32 reciprocal_scale(u32 val, u32 ep_ro) · line 306
Quoted 10 lines
include/linux/kernel.h +10
-0+/* Return in f the value of 10 raise to the power x */ +#define __pow10 (x, f)( \ +{ \ + typeof(x) __x = abs(x); \ + f = 1; \ + while (__x--) \ + f *= 10; \ + f; \ +} \ +)
Kind of unusual. I would have expected to use this like
f = __pow10(x);
ie without having to provide f as parameter. That would be much less confusing. I assume this is to make the result type independent, but I am not sure if that is worth the trouble.
Are there users outside the hwmon code ? If not, it might be simpler to keep it there for now.
Thanks,
Guenter
Quoted 7 lines
include/linux/kernel.h +1
-0+#if defined(CONFIG_MMU ) && \ (defined(CONFIG_PROVE_LOCKING ) || defined(CONFIG_DEBUG_ATOMIC_SLEEP ))#define might_fault () __might_fault(__FILE__, __LINE__)Signature · 1 line
2.17.1
Re: [PATCH v2 1/3] kernel: Provide a __pow10() function
Quoted 30 lines · Guenter Roeck
Quoted 17 lines · Florian Fainelli
Provide a simple macro that can return the value of 10 raised to a positive integer. We are going to use this in order to scale units from firmware to HWMON.
Signed-off-by Florian Fainelli <[email protected]>--- include/linux/kernel.h | 11 +++++++++++ 1 file changed, 11 insertions(+)include/linux/kernel.h +0
-0@@ -294,6 +294,17 @@ static inline u32 reciprocal_scale(u32 val, u32 ep_ro)return (u32)(((u64 ) val * ep_ro) >>32 ); }include/linux/kernel.h static inline u32 reciprocal_scale(u32 val, u32 ep_ro) · line 306
Quoted 10 lines
include/linux/kernel.h +10
-0+/* Return in f the value of 10 raise to the power x */ +#define __pow10 (x, f)( \ +{ \ + typeof(x) __x = abs(x); \ + f = 1; \ + while (__x--) \ + f *= 10; \ + f; \ +} \ +)Kind of unusual. I would have expected to use this like
Quoted 4 lines
f = __pow10(x);ie without having to provide f as parameter. That would be much less confusing. I assume this is to make the result type independent, but I am not sure if that is worth the trouble.
Correct, that was the intent here.
Quoted 3 lines
Are there users outside the hwmon code ? If not, it might be simpler to keep it there for now.
There appears to be a few outside actually:
drivers/acpi/sbs.c::battery_scale drivers/iio/common/hid-sensors/hid-sensor-attributes.c::pow_10
There could be others but those two came out as obvious candidates.
Would you be okay with a local pow10 function within scmi-hwmon.c and a subsequent patch series providing a common function?
Signature · 1 line
Florian
Guenter Roeck
maintainerRe: [PATCH v2 1/3] kernel: Provide a __pow10() function
Quoted 44 lines · Florian Fainelli
Quoted 30 lines · Guenter Roeck
Quoted 17 lines · Florian Fainelli
Provide a simple macro that can return the value of 10 raised to a positive integer. We are going to use this in order to scale units from firmware to HWMON.
Signed-off-by Florian Fainelli <[email protected]>--- include/linux/kernel.h | 11 +++++++++++ 1 file changed, 11 insertions(+)include/linux/kernel.h +0
-0@@ -294,6 +294,17 @@ static inline u32 reciprocal_scale(u32 val, u32 ep_ro)return (u32)(((u64 ) val * ep_ro) >>32 ); }include/linux/kernel.h static inline u32 reciprocal_scale(u32 val, u32 ep_ro) · line 306
Quoted 10 lines
include/linux/kernel.h +10
-0+/* Return in f the value of 10 raise to the power x */ +#define __pow10 (x, f)( \ +{ \ + typeof(x) __x = abs(x); \ + f = 1; \ + while (__x--) \ + f *= 10; \ + f; \ +} \ +)Kind of unusual. I would have expected to use this like
Quoted 4 lines
f = __pow10(x);ie without having to provide f as parameter. That would be much less confusing. I assume this is to make the result type independent, but I am not sure if that is worth the trouble.
Correct, that was the intent here.
Quoted 3 lines
Are there users outside the hwmon code ? If not, it might be simpler to keep it there for now.
There appears to be a few outside actually:
Quoted 8 lines
drivers/acpi/sbs.c::battery_scale drivers/iio/common/hid-sensors/hid-sensor-attributes.c::pow_10
There could be others but those two came out as obvious candidates.
Would you be okay with a local pow10 function within
scmi-hwmon.cand a subsequent patch series providing a common function?
I would prefer that, actually, to reduce dependencies.
Thanks,
Guenter
PATCH v2 2/3 firmware: arm_scmi: Fetch and store sensor scale
In reply to Florian FainelliIn preparation for dealing with scales within the SCMI HWMON driver, fetch and store the sensor unit scale into the scmi_sensor_info structure. In order to simplify computations for upper layer, take care of sign extending the scale to a full 8-bit signed value.
--- drivers/firmware/arm_scmi/sensors.c | 6 ++++++ include/linux/scmi_protocol.h | 1 + 2 files changed, 7 insertions(+)
drivers/firmware/arm_scmi/sensors.c +6 -0
@@ -34,6 +34,8 @@ struct scmi_msg_resp_sensor_description {__le32 attributes_high;#define SENSOR_TYPE (x) ((x) & 0xff)#define SENSOR_SCALE (x) (((x) >> 11) & 0x3f) +#define SENSOR_SCALE_SIGN BIT(5) +#define SENSOR_SCALE_EXTEND GENMASK(7, 6)#define SENSOR_UPDATE_SCALE (x) (((x) >> 22) & 0x1f)#define SENSOR_UPDATE_BASE (x) (((x) >> 27) & 0x1f)u8 name[SCMI_MAX_STR_SIZE ];
@@ -140,6 +142,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 s8 */ +if (s->scale &SENSOR_SCALE_SIGN ) + s->scale |=SENSOR_SCALE_EXTEND ;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; +s8 scale;char name[SCMI_MAX_STR_SIZE ]; };
Signature · 1 line
2.17.1
PATCH v2 3/3 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 | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/hwmon/scmi-hwmon.c +29 -1
@@ -18,6 +18,34 @@ struct scmi_sensors {const struct scmi_sensor_info **info[hwmon_max]; }; +static u64 scmi_hwmon_scale (const struct scmi_sensor_info *sensor,u64 value) +{ +s8 scale = sensor->scale; +unsigned long long f; + +switch (sensor->type) { +case TEMPERATURE_C : +case VOLTAGE : +case CURRENT : + scale +=3 ; +break ; +case POWER : +case ENERGY : + scale +=6 ; +break ; +default : +break ; + } + +__pow10 (abs (scale), f); +if (scale >0 ) + value *= f; +else +do_div (value, f); + +return value; +} +static int scmi_hwmon_read (struct device *dev,enum hwmon_sensor_types type,u32 attr,int channel,long *val) {
@@ -30,7 +58,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; }
Signature · 1 line
2.17.1
Guenter Roeck
maintainerQuoted 14 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 | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)drivers/hwmon/scmi-hwmon.c +0
-0
drivers/hwmon/scmi-hwmon.c struct scmi_sensors { · line 24
Quoted 7 lines
drivers/hwmon/scmi-hwmon.c +4
-0@@ -18,6 +18,34 @@ struct scmi_sensors {const struct scmi_sensor_info **info[hwmon_max]; }; +static u64 scmi_hwmon_scale (const struct scmi_sensor_info *sensor,u64 value) +{ +s8 scale = sensor->scale; +unsigned long long f;
do_div() takes either an uint32 or an unsigned long as second parameter, so this doesn't really help. If you want to be able to handle scales outside the 32-bit range, you would have to use u64 and div64_u64().
Guenter
Quoted 39 lines
drivers/hwmon/scmi-hwmon.c +25
-1+ +switch (sensor->type) { +case TEMPERATURE_C : +case VOLTAGE : +case CURRENT : + scale +=3 ; +break ; +case POWER : +case ENERGY : + scale +=6 ; +break ; +default : +break ; + } + +__pow10 (abs (scale), f); +if (scale >0 ) + value *= f; +else +do_div (value, f); + +return value; +} +static int scmi_hwmon_read (struct device *dev,enum hwmon_sensor_types type,u32 attr,int channel,long *val) {@@ -30,7 +58,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; }Signature · 1 line
2.17.1