hwmon: add a driver for the temp/voltage sensor on PolarFire SoC
Subsystems (4)
RFC 1/1 hwmon: add a driver for the temp/voltage sensor on PolarFire SoC
From: Lars Randers <[email protected]>
Add a driver for the temperature and voltage sensors on PolarFire SoC. The temperature reports how hot the die is, and the voltages are the SoC's 1.05, 1.8 and 2.5 volt rails respectively.
The hardware supports alarms in theory, but there is an unconfirmed erratum that prevents clearing them once triggered, so no support is added.
The hardware measures voltage with 16 bits, of which 1 is a sign bit and the remainder holds the voltage as a fixed point integer value. It's improbable that the hardware will work if the voltages are negative, so the driver ignores the sign bits.
There's no dt support etc here because this is the child of a simple-mfd syscon.
---
Guenter, there's one question here about the unit that update_interval is in, I didn't see anyone else using us, but I assume that's okay since the resolution that ms would give would be 8 steps only?
RFC cos the question is also in the driver as a comment.
--- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/tvs-mpfs.rst | 53 +++++ MAINTAINERS | 1 + drivers/hwmon/Kconfig | 12 + drivers/hwmon/Makefile | 1 + drivers/hwmon/tvs-mpfs.c | 396 +++++++++++++++++++++++++++++++ 6 files changed, 464 insertions(+) create mode 100644 Documentation/hwmon/tvs-mpfs.rst create mode 100644 drivers/hwmon/tvs-mpfs.c
Documentation/hwmon/index.rst +1 -0
@@ -262,6 +262,7 @@ Hardware Monitoring Kernel Drivers tps53679 tps546d24 tsc1641 + tvs-mpfs twl4030-madc-hwmon ucd9000 ucd9200
Documentation/hwmon/tvs-mpfs.rst new file +53 -0
53 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -22938,6 +22938,7 @@ F: drivers/char/hw_random/mpfs-rng.c F: drivers/clk/microchip/clk-mpfs*.c F: drivers/firmware/microchip/mpfs-auto-update.c F: drivers/gpio/gpio-mpfs.c +F: drivers/hwmon/tvs-mpfs.c F: drivers/i2c/busses/i2c-microchip-corei2c.c F: drivers/mailbox/mailbox-mpfs.c F: drivers/pci/controller/plda/pcie-microchip-host.c
drivers/hwmon/Kconfig +12 -0
@@ -930,6 +930,18 @@ config SENSORS_JC42 This driver can also be built as a module. If so, the module will be called jc42. +config SENSORS_POLARFIRE_SOC_TVS + tristate "PolarFire SoC (MPFS) temperature and voltage sensor" + depends on POLARFIRE_SOC_MAILBOX + help + This driver adds support for the PolarFire SoC (MPFS) Temperature and + Voltage Sensor. + + To compile this driver as a module, choose M here. the + module will be called tvs-mpfs. + + If unsure, say N. + config SENSORS_POWERZ tristate "ChargerLAB POWER-Z USB-C tester" depends on USB
drivers/hwmon/Makefile +1 -0
@@ -194,6 +194,7 @@ obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o obj-$(CONFIG_SENSORS_PC87360) += pc87360.o obj-$(CONFIG_SENSORS_PC87427) += pc87427.o obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o +obj-$(CONFIG_SENSORS_POLARFIRE_SOC_TVS) += tvs-mpfs.o obj-$(CONFIG_SENSORS_POWERZ) += powerz.o obj-$(CONFIG_SENSORS_POWR1220) += powr1220.o obj-$(CONFIG_SENSORS_PT5161L) += pt5161l.o
drivers/hwmon/tvs-mpfs.c new file +396 -0
396 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider: - [High] Masking out the sign bit of a two's complement value incorrectly reports small negative voltages…
Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider:
- [High] Masking out the sign bit of a two's complement value incorrectly reports small negative voltages as massive positive spikes.
- [High] Hardware initialization occurs after device registration and uses regmap_write, creating a race condition and clobbering existing register state.
- [High] Missing return value checks for regmap_read expose uninitialized kernel stack memory to userspace.
- [High] Blind dereference of pdev->dev.parent in the probe function.
- [Medium] The update_interval attribute is exposed and expected in microseconds, violating the hwmon sysfs ABI standard.
- [Medium] Boolean sysfs enable attributes fail to properly reject negative values.
- [Medium] The driver uses syscon APIs but its Kconfig entry does not depend on or select MFD_SYSCON.
- [Medium] The driver ignores return values from hardware write operations, masking potential hardware failures.
- [Low] Using a bitwise right shift (>> 4) on potentially negative temperatures relies on implementation-defined behavior and rounds incorrectly.
- [Low] The comment explaining the absolute zero constant is mathematically incorrect.
--
commit 0cc52b7af906d819cbf1265ac544bb7d896a59da
Author: Lars Randers <[email protected]>
hwmon: add a driver for the temp/voltage sensor on PolarFire SoC
Add a driver for the temperature and voltage sensors on PolarFire SoC. The temperature reports how hot the die is, and the voltages are the SoC's 1.05, 1.8 and 2.5 volt rails respectively.
Quoted 4 lines
drivers/hwmon/Kconfig +0
-0
[ ... ]
drivers/hwmon/Kconfig config SENSORS_JC42 · line 935
Quoted 7 lines
drivers/hwmon/Kconfig +3
-0@@ -930,6 +930,18 @@ config SENSORS_JC42 This driver can also be built as a module. If so, the module will be called jc42. +config SENSORS_POLARFIRE_SOC_TVS + tristate "PolarFire SoC (MPFS) temperature and voltage sensor" + depends on POLARFIRE_SOC_MAILBOX
[Severity: Medium]
Should this Kconfig entry depend on or select MFD_SYSCON?
The driver explicitly calls device_node_to_regmap() which is provided by the syscon framework. If MFD_SYSCON is not enabled, the fallback stub unconditionally returns -ENOTSUPP, which would cause the driver to silently fail to probe at runtime without producing any build-time warnings.
Quoted 2 lines
drivers/hwmon/Kconfig +2
-0+ help + This driver adds support for the PolarFire SoC (MPFS) Temperature and
[ ... ]
Quoted 5 lines
drivers/hwmon/tvs-mpfs.c new file +0
-0
[ ... ]
drivers/hwmon/tvs-mpfs.c line 41
Quoted 9 lines
drivers/hwmon/tvs-mpfs.c new file +9
-0+/* +* For all of these the value in millivolts is stored in 16 bits, with an upper +* sign bit and a lower 2 bits of decimal. These masks discard the sign bit and +* decimal places, because if Linux is running these voltages cannot be negative +* and so avoid having to convert to two's complement. +*/ +#define MPFS_OUTPUT0_V1P8_MASK GENMASK(30, 19) +#define MPFS_OUTPUT0_V1P05_MASK GENMASK(14, 3) +#define MPFS_OUTPUT1_V2P5_MASK GENMASK(14, 3)
[Severity: High]
Does discarding the sign bit cause issues with slight negative readings?
If the hardware measures a slightly negative voltage due to sensor noise (for example, -1 mV), wouldn't the two's complement representation have the integer bits set to all 1s?
Since the driver discards the sign bit and extracts the remaining bits via FIELD_GET, it seems this could result in reporting the maximum positive integer (4095 mV) instead of clamping to 0.
Quoted 6 lines
drivers/hwmon/tvs-mpfs.c new file +6
-0+ +/* +* The register map claims that the temperature is stored in bits 31:16, but +* application note "AN4682: PolarFire FPGA Temperature and Voltage Sensor" +* says that 31 is reserved. Temperature is in kelvin, so what's probably a +* sign bit has no value anyway.
drivers/hwmon/tvs-mpfs.c line 57
Quoted 10 lines
drivers/hwmon/tvs-mpfs.c new file +10
-0+*/ +#define MPFS_OUTPUT1_TEMP_MASK GENMASK(30, 16) + +#define MPFS_TVS_INTERVAL_MASK GENMASK(15, 8) +#define MPFS_TVS_INTERVAL_OFFSET 8 +/* The interval register is in increments of 32 us */ +#define MPFS_TVS_INTERVAL_SCALE 32 + +/* 273.5 in 11.4 fixed-point notation */ +#define MPFS_TVS_K_TO_C 0x1112
[Severity: Low]
Is this comment mathematically accurate?
The value 0x1112 represents 4370 in decimal, which is 273.125 when divided by 16. Absolute zero is 273.15, making 4370 the nearest integer, but the comment mentions 273.5.
Quoted 2 lines
drivers/hwmon/tvs-mpfs.c new file +2
-0+ +enum mpfs_tvs_sensors {
[ ... ]
drivers/hwmon/tvs-mpfs.c line 79
Quoted 9 lines
drivers/hwmon/tvs-mpfs.c new file +9
-0+static int mpfs_tvs_voltage_read (struct mpfs_tvs *data,u32 attr, +int channel,long *val) +{ +u32 tmp, control; + +if (attr != hwmon_in_input && attr != hwmon_in_enable) +return -EOPNOTSUPP ; + +regmap_read (data->regmap,MPFS_TVS_CTRL , &control);
[Severity: High]
Can a failed regmap_read() leave the control variable uninitialized?
If the underlying bus read fails, the control variable remains uninitialized, and this function could extract bits from uninitialized kernel stack memory and return it to userspace via sysfs. This also appears to happen with the tmp variable in the other read functions below.
Quoted 3 lines
drivers/hwmon/tvs-mpfs.c new file +3
-0+ +switch (channel) { +case SENSOR_V2P5 :
[ ... ]
drivers/hwmon/tvs-mpfs.c line 136
Quoted 12 lines
drivers/hwmon/tvs-mpfs.c new file +12
-0+static int mpfs_tvs_voltage_write (struct mpfs_tvs *data,u32 attr, +int channel,long val) +{ +u32 tmp; + +if (attr != hwmon_in_enable) +return -EOPNOTSUPP ; + +switch (channel) { +case SENSOR_V2P5 : +if (val >1 ) +return -EINVAL ;
[Severity: Medium]
Does this check properly reject negative inputs?
Because val is a signed long, negative values like -1 will bypass this validation. When a negative value is passed to FIELD_PREP, it could inadvertently set the enable bit to 1 due to the all-1s binary representation.
drivers/hwmon/tvs-mpfs.c line 140
Quoted 4 lines
drivers/hwmon/tvs-mpfs.c new file +4
-0+ + tmp =FIELD_PREP (MPFS_TVS_CTRL_V2P5_ENABLE , val); +regmap_update_bits (data->regmap,MPFS_TVS_CTRL , +MPFS_TVS_CTRL_V2P5_ENABLE , tmp);
[Severity: Medium]
Are the return values from regmap_update_bits() intentionally ignored here?
If a hardware write fails, it looks like this unconditionally returns 0, which would incorrectly inform userspace that the configuration was successfully applied. This pattern seems to occur for other write handlers as well.
Quoted 2 lines
drivers/hwmon/tvs-mpfs.c new file +2
-0+break ; +case SENSOR_V1P8 :
[ ... ]
Quoted 11 lines
drivers/hwmon/tvs-mpfs.c new file +11
-0+static int mpfs_tvs_temp_read (struct mpfs_tvs *data,u32 attr,long *val) +{ +u32 tmp, control; + +if (attr != hwmon_temp_input && attr != hwmon_temp_enable) +return -EOPNOTSUPP ; + +regmap_read (data->regmap,MPFS_TVS_CTRL , &control); + +if (attr == hwmon_temp_enable) { + *val =FIELD_GET (MPFS_TVS_CTRL_TEMP_ENABLE , control);
drivers/hwmon/tvs-mpfs.c line 185
Quoted 10 lines
drivers/hwmon/tvs-mpfs.c new file +10
-0+return 0 ; + } + +if (!(control &MPFS_TVS_CTRL_TEMP_VALID )) +return -EINVAL ; + +regmap_read (data->regmap,MPFS_TVS_OUTPUT1 , &tmp); + *val =FIELD_GET (MPFS_OUTPUT1_TEMP_MASK , tmp); + *val -=MPFS_TVS_K_TO_C ; + *val = (1000 * *val) >>4 ;/* fixed point (11.4) to millidegrees */
[Severity: Low]
Does using a bitwise right shift on a potentially negative temperature value rely on implementation-defined behavior?
Subtracting MPFS_TVS_K_TO_C can yield a negative number. An arithmetic right shift rounds towards negative infinity rather than towards zero like standard division, which might introduce minor inaccuracies for sub-zero temperatures.
Quoted 3 lines
drivers/hwmon/tvs-mpfs.c new file +3
-0+ +return 0 ; +}
[ ... ]
drivers/hwmon/tvs-mpfs.c line 219
Quoted 13 lines
drivers/hwmon/tvs-mpfs.c new file +13
-0+static int mpfs_tvs_interval_read (struct mpfs_tvs *data,u32 attr,long *val) +{ +u32 tmp; + +if (attr != hwmon_chip_update_interval) +return -EOPNOTSUPP ; + +regmap_read (data->regmap,MPFS_TVS_CTRL , &tmp); +//TODO: Guenter, update_interval, as implemented here, expects values +//in microseconds. Is that okay? Most drivers (all that I checked tbh) +//use milliseconds, but this is an 8-bit value so using ms will have +//almost no granularity at all. + *val =FIELD_GET (MPFS_TVS_INTERVAL_MASK , tmp) *MPFS_TVS_INTERVAL_SCALE ;
[Severity: Medium]
Is this exposing the interval in microseconds?
The hwmon sysfs ABI expects update_interval to be expressed in milliseconds. Returning microseconds directly could break standard userspace monitoring tools and the thermal subsystem that rely on standardized units.
Quoted 3 lines
drivers/hwmon/tvs-mpfs.c new file +3
-0+ +return 0 ; +}
[ ... ]
drivers/hwmon/tvs-mpfs.c line 368
Quoted 10 lines
drivers/hwmon/tvs-mpfs.c new file +10
-0+static int mpfs_tvs_probe (struct platform_device *pdev) +{ +struct device *hwmon_dev; +struct mpfs_tvs *data; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->regmap =device_node_to_regmap (pdev->dev.parent->of_node);
[Severity: High]
Could pdev->dev.parent be NULL if the platform device is instantiated manually?
If a privileged user writes to sysfs to instantiate the device or it is bound such that pdev->dev.parent is NULL, would this unconditional dereference trigger a NULL pointer dereference?
drivers/hwmon/tvs-mpfs.c line 381
Quoted 13 lines
drivers/hwmon/tvs-mpfs.c new file +13
-0+if (IS_ERR (data->regmap)) +return dev_err_probe (&pdev->dev,PTR_ERR (data->regmap), +"Failed to find syscon regmap\n" ); + + hwmon_dev =devm_hwmon_device_register_with_info (&pdev->dev,"mpfs_tvs" , + data, + &mpfs_tvs_chip_info, +NULL ); +if (IS_ERR (hwmon_dev)) +return dev_err_probe (&pdev->dev,PTR_ERR (hwmon_dev), +"hwmon device registration failed.\n" ); + +regmap_write (data->regmap,MPFS_TVS_CTRL ,MPFS_TVS_CTRL_ENABLE_ALL );
[Severity: High]
Does registering the device before enabling the sensors create a race window?
The thermal core or userspace might attempt to read sensors after devm_hwmon_device_register_with_info() completes but before they are enabled.
Also, does this regmap_write() clobber the update_interval field? Overwriting the entire control register with MPFS_TVS_CTRL_ENABLE_ALL appears to zero out bits 15:8, destroying any interval settings made during the race window or pre-configured by the bootloader.
Quoted 4 lines
drivers/hwmon/tvs-mpfs.c new file +4
-0+ +return 0 ; +} +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260527-earring-bully-eb4a268c2e68@spud?part=1
Quoted 40 lines · On Wed, May 27, 2026 at 09:34:27AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider:
- [High] Masking out the sign bit of a two's complement value incorrectly reports small negative voltages as massive positive spikes.
- [High] Hardware initialization occurs after device registration and usesregmap_write, creating a race condition and clobbering existing register state.
- [High] Missing return value checks forregmap_readexpose uninitialized kernel stack memory to userspace.
- [High] Blind dereference ofpdev->dev.parentin the probe function.
- [Medium] Theupdate_intervalattribute is exposed and expected in microseconds, violating the hwmon sysfs ABI standard.
- [Medium] Boolean sysfs enable attributes fail to properly reject negative values.
- [Medium] The driver uses syscon APIs but itsKconfigentry does not depend on or selectMFD_SYSCON.
- [Medium] The driver ignores return values from hardware write operations, masking potential hardware failures.
- [Low] Using a bitwise right shift (>> 4) on potentially negative temperatures relies on implementation-defined behavior and rounds incorrectly.
- [Low] The comment explaining the absolute zero constant is mathematically incorrect.
--commit
0cc52b7af906d819cbf1265ac544bb7d896a59da
Author: Lars Randers <[email protected]>hwmon: add a driver for the temp/voltage sensor on PolarFire SoC
Add a driver for the temperature and voltage sensors on PolarFire SoC. The temperature reports how hot the die is, and the voltages are the SoC's 1.05, 1.8 and 2.5 volt rails respectively.
Quoted 4 lines
drivers/hwmon/Kconfig +0
-0[ ... ]
drivers/hwmon/Kconfig config SENSORS_JC42 · line 935
Quoted 7 lines
drivers/hwmon/Kconfig +3
-0@@ -930,6 +930,18 @@ config SENSORS_JC42 This driver can also be built as a module. If so, the module will be called jc42. +config SENSORS_POLARFIRE_SOC_TVS + tristate "PolarFire SoC (MPFS) temperature and voltage sensor" + depends on POLARFIRE_SOC_MAILBOX[Severity: Medium]
Should thisKconfigentry depend on or selectMFD_SYSCON?
Quoted 4 lines
The driver explicitly calls
device_node_to_regmap()which is provided by the syscon framework. IfMFD_SYSCONis not enabled, the fallback stub unconditionally returns-ENOTSUPP, which would cause the driver to silently fail to probe at runtime without producing any build-time warnings.
I'm not sure why this uses the mailbox as a dep, but it depends on MFD_SYSCON so that satisfies the condition. The deps here need rework.
Quoted 13 lines
drivers/hwmon/Kconfig config SENSORS_JC42 · line 944
Quoted 9 lines
drivers/hwmon/Kconfig +9
-0+/* + * For all of these the value in millivolts is stored in 16 bits, with an upper + * sign bit and a lower 2 bits of decimal. These masks discard the sign bit and + * decimal places, because if Linux is running these voltages cannot be negative + * and so avoid having to convert to two's complement. + */ +#define MPFS_OUTPUT0_V1P8_MASK GENMASK(30, 19) +#define MPFS_OUTPUT0_V1P05_MASK GENMASK(14, 3) +#define MPFS_OUTPUT1_V2P5_MASK GENMASK(14, 3)[Severity: High]
Does discarding the sign bit cause issues with slight negative readings?
Quoted 7 lines
If the hardware measures a slightly negative voltage due to sensor noise (for example, -1 mV), wouldn't the two's complement representation have the integer bits set to all 1s?
Since the driver discards the sign bit and extracts the remaining bits via
FIELD_GET, it seems this could result in reporting the maximum positive integer (4095 mV) instead of clamping to 0.
The comment covers this.
Quoted 13 lines
drivers/hwmon/Kconfig config SENSORS_JC42
Quoted 9 lines
drivers/hwmon/Kconfig +9
-0+static int mpfs_tvs_voltage_read(struct mpfs_tvs *data, u32 attr, + int channel, long *val) +{ + u32 tmp, control; + + if (attr != hwmon_in_input && attr != hwmon_in_enable) + return -EOPNOTSUPP; + + regmap_read(data->regmap, MPFS_TVS_CTRL, &control);[Severity: High]
Can a failedregmap_read()leave the control variable uninitialized?
Quoted 4 lines
If the underlying bus read fails, the control variable remains uninitialized, and this function could extract bits from uninitialized kernel stack memory and return it to userspace via sysfs. This also appears to happen with the tmp variable in the other read functions below.
It's an MMIO bus, I've never bothered checking the results of regmap_read() etc cos I can't check the result of readl().
Quoted 26 lines
Quoted 11 lines
drivers/hwmon/Kconfig +11
-0+static int mpfs_tvs_temp_read(struct mpfs_tvs *data, u32 attr, long *val) +{ + u32 tmp, control; + + if (attr != hwmon_temp_input && attr != hwmon_temp_enable) + return -EOPNOTSUPP; + + regmap_read(data->regmap, MPFS_TVS_CTRL, &control); + + if (attr == hwmon_temp_enable) { + *val = FIELD_GET(MPFS_TVS_CTRL_TEMP_ENABLE, control);drivers/hwmon/Kconfig
Quoted 10 lines
drivers/hwmon/Kconfig +10
-0+ return 0; + } + + if (!(control & MPFS_TVS_CTRL_TEMP_VALID)) + return -EINVAL; + + regmap_read(data->regmap, MPFS_TVS_OUTPUT1, &tmp); + *val = FIELD_GET(MPFS_OUTPUT1_TEMP_MASK, tmp); + *val -= MPFS_TVS_K_TO_C; + *val = (1000 * *val) >> 4; /* fixed point (11.4) to millidegrees */[Severity: Low]
Does using a bitwise right shift on a potentially negative temperature value rely on implementation-defined behavior?
Quoted 4 lines
Subtracting
MPFS_TVS_K_TO_Ccan yield a negative number. An arithmetic right shift rounds towards negative infinity rather than towards zero like standard division, which might introduce minor inaccuracies for sub-zero temperatures.
Does this matter?
Quoted 15 lines
drivers/hwmon/Kconfig
Quoted 10 lines
drivers/hwmon/Kconfig +10
-0+static int mpfs_tvs_probe(struct platform_device *pdev) +{ + struct device *hwmon_dev; + struct mpfs_tvs *data; + + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->regmap = device_node_to_regmap(pdev->dev.parent->of_node);[Severity: High]
Couldpdev->dev.parentbe NULL if the platform device is instantiated manually?
Quoted 3 lines
If a privileged user writes to sysfs to instantiate the device or it is bound such that
pdev->dev.parentis NULL, would this unconditional dereference trigger a NULL pointer dereference?
I don't think I have ever seen code check this.
Quoted 17 lines
drivers/hwmon/Kconfig
Quoted 13 lines
drivers/hwmon/Kconfig +13
-0+ if (IS_ERR(data->regmap)) + return dev_err_probe(&pdev->dev, PTR_ERR(data->regmap), + "Failed to find syscon regmap\n"); + + hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, "mpfs_tvs", + data, + &mpfs_tvs_chip_info, + NULL); + if (IS_ERR(hwmon_dev)) + return dev_err_probe(&pdev->dev, PTR_ERR(hwmon_dev), + "hwmon device registration failed.\n"); + + regmap_write(data->regmap, MPFS_TVS_CTRL, MPFS_TVS_CTRL_ENABLE_ALL);[Severity: High]
Does registering the device before enabling the sensors create a race window?
Quoted 3 lines
The thermal core or userspace might attempt to read sensors after
devm_hwmon_device_register_with_info()completes but before they are enabled.
Is this a realistic concern? Moving it is effectively free I think anyway, not really even worth turning them off in a remove callback.
Quoted 4 lines
Also, does this
regmap_write()clobber theupdate_intervalfield? Overwriting the entire control register withMPFS_TVS_CTRL_ENABLE_ALLappears to zero out bits 15:8, destroying any interval settings made during the race window or pre-configured by the bootloader.
Intentionally, to get a known-good state.
Guenter Roeck
maintainerQuoted 18 lines · Conor Dooley
From: Lars Randers <[email protected]>
Add a driver for the temperature and voltage sensors on PolarFire SoC. The temperature reports how hot the die is, and the voltages are the SoC's 1.05, 1.8 and 2.5 volt rails respectively.
The hardware supports alarms in theory, but there is an unconfirmed erratum that prevents clearing them once triggered, so no support is added.
The hardware measures voltage with 16 bits, of which 1 is a sign bit and the remainder holds the voltage as a fixed point integer value. It's improbable that the hardware will work if the voltages are negative, so the driver ignores the sign bits.
There's no dt support etc here because this is the child of a simple-mfd syscon.
Quoted 9 lines
Signed-off-by Lars Randers <[email protected]>Co-developed-by Conor Dooley <[email protected]>Signed-off-by Conor Dooley <[email protected]>---
Guenter, there's one question here about the unit thatupdate_intervalis in, I didn't see anyone else using us, but I assume that's okay since the resolution that ms would give would be 8 steps only?
RFC cos the question is also in the driver as a comment.
That just came up in a different context. We'll add a new standard attribute update_interval_us. The existing attribute MUST use ms. Everything else would be an ABI violation.
Guenter
Quoted 545 lines
CC Guenter Roeck <[email protected]>CC Jonathan Corbet <[email protected]>CC Shuah Khan <[email protected]>CC Conor Dooley <[email protected]>CC Daire McNamara <[email protected]>--- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/tvs-mpfs.rst | 53 +++++ MAINTAINERS | 1 + drivers/hwmon/Kconfig | 12 + drivers/hwmon/Makefile | 1 + drivers/hwmon/tvs-mpfs.c | 396 +++++++++++++++++++++++++++++++ 6 files changed, 464 insertions(+) create mode 100644 Documentation/hwmon/tvs-mpfs.rst create mode 100644 drivers/hwmon/tvs-mpfs.cDocumentation/hwmon/index.rst +1
-0@@ -262,6 +262,7 @@ Hardware Monitoring Kernel Drivers tps53679 tps546d24 tsc1641 + tvs-mpfs twl4030-madc-hwmon ucd9000 ucd9200Documentation/hwmon/tvs-mpfs.rst new file +53
-0@@ -0,0 +1,53 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Kernel driver tvs-mpfs +====================== + +Supported chips: + + * PolarFire SoC + +Authors: + + - Conor Dooley <[email protected]> + - Lars Randers <[email protected]> + +Description +----------- + +This driver implements support for the temperature and voltage sensors on +PolarFire SoC. The temperature reports how hot the die is, and the voltages are +the SoC's 1.05, 1.8 and 2.5 volt rails respectively. + + +Usage Notes +----------- + +update_interval is in microseconds, with a permitted range of 0 to 8128. + +Temperatures are read in millidegrees Celsius, but the hardware measures in +degrees Kelvin, storing the result as 11.4 fixed point data, for a maximum +value of 2047.9375 degrees Kelvin. + +Voltages are read in millivolts. The hardware measures in millivolts, storing +the value as 12.3 fixed point data, for a maximum of 4095.875 millivolts. +The minimum value reportable by the driver is 0 volts, although the hardware +is capable of measuring negative values. + +Sysfs entries +------------- + +The following attributes are supported. update_interval is read-write, as are +the enables. All other attributes are read only. + +======================= ==================================================== +temp1_label Fixed name for channel. +temp1_input Measured temperature for channel. +temp1_enable Enable/disable for channel. + +in[0-2]_label Fixed name for channel. +in[0-2]_input Measured voltage for channel. +in[0-2]_enable Enable/disable for channel. + +update_interval The interval at which the chip will update readings. +======================= ====================================================MAINTAINERS +1
-0@@ -22938,6 +22938,7 @@ F: drivers/char/hw_random/mpfs-rng.c F: drivers/clk/microchip/clk-mpfs*.c F: drivers/firmware/microchip/mpfs-auto-update.c F: drivers/gpio/gpio-mpfs.c +F: drivers/hwmon/tvs-mpfs.c F: drivers/i2c/busses/i2c-microchip-corei2c.c F: drivers/mailbox/mailbox-mpfs.c F: drivers/pci/controller/plda/pcie-microchip-host.cdrivers/hwmon/Kconfig +12
-0@@ -930,6 +930,18 @@ config SENSORS_JC42 This driver can also be built as a module. If so, the module will be called jc42. +config SENSORS_POLARFIRE_SOC_TVS + tristate "PolarFire SoC (MPFS) temperature and voltage sensor" + depends on POLARFIRE_SOC_MAILBOX + help + This driver adds support for the PolarFire SoC (MPFS) Temperature and + Voltage Sensor. + + To compile this driver as a module, choose M here. the + module will be called tvs-mpfs. + + If unsure, say N. + config SENSORS_POWERZ tristate "ChargerLAB POWER-Z USB-C tester" depends on USBdrivers/hwmon/Makefile +1
-0@@ -194,6 +194,7 @@ obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o obj-$(CONFIG_SENSORS_PC87360) += pc87360.o obj-$(CONFIG_SENSORS_PC87427) += pc87427.o obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o +obj-$(CONFIG_SENSORS_POLARFIRE_SOC_TVS) += tvs-mpfs.o obj-$(CONFIG_SENSORS_POWERZ) += powerz.o obj-$(CONFIG_SENSORS_POWR1220) += powr1220.o obj-$(CONFIG_SENSORS_PT5161L) += pt5161l.odrivers/hwmon/tvs-mpfs.c new file +396
-0@@ -0,0 +1,396 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Author: Lars Randers <[email protected]> +*/ + +#include <linux/bitfield.h> +#include <linux/err.h> +#include <linux/freezer.h> +#include <linux/hwmon.h> +#include <linux/io.h> +#include <linux/kthread.h> +#include <linux/mfd/syscon.h> +#include <linux/module.h> +#include <linux/of_address.h> +#include <linux/platform_device.h> +#include <linux/regmap.h> + +#define MPFS_TVS_CTRL 0x08 +#define MPFS_TVS_OUTPUT0 0x24 +#define MPFS_TVS_OUTPUT1 0x28 + +#define MPFS_TVS_CTRL_TEMP_VALID BIT(19) +#define MPFS_TVS_CTRL_V2P5_VALID BIT(18) +#define MPFS_TVS_CTRL_V1P8_VALID BIT(17) +#define MPFS_TVS_CTRL_V1P05_VALID BIT(16) + +#define MPFS_TVS_CTRL_TEMP_ENABLE BIT(3) +#define MPFS_TVS_CTRL_V2P5_ENABLE BIT(2) +#define MPFS_TVS_CTRL_V1P8_ENABLE BIT(1) +#define MPFS_TVS_CTRL_V1P05_ENABLE BIT(0) +#define MPFS_TVS_CTRL_ENABLE_ALL GENMASK(3, 0) + +/* +* For all of these the value in millivolts is stored in 16 bits, with an upper +* sign bit and a lower 2 bits of decimal. These masks discard the sign bit and +* decimal places, because if Linux is running these voltages cannot be negative +* and so avoid having to convert to two's complement. +*/ +#define MPFS_OUTPUT0_V1P8_MASK GENMASK(30, 19) +#define MPFS_OUTPUT0_V1P05_MASK GENMASK(14, 3) +#define MPFS_OUTPUT1_V2P5_MASK GENMASK(14, 3) + +/* +* The register map claims that the temperature is stored in bits 31:16, but +* application note "AN4682: PolarFire FPGA Temperature and Voltage Sensor" +* says that 31 is reserved. Temperature is in kelvin, so what's probably a +* sign bit has no value anyway. +*/ +#define MPFS_OUTPUT1_TEMP_MASK GENMASK(30, 16) + +#define MPFS_TVS_INTERVAL_MASK GENMASK(15, 8) +#define MPFS_TVS_INTERVAL_OFFSET 8 +/* The interval register is in increments of 32 us */ +#define MPFS_TVS_INTERVAL_SCALE 32 + +/* 273.5 in 11.4 fixed-point notation */ +#define MPFS_TVS_K_TO_C 0x1112 + +enum mpfs_tvs_sensors { +SENSOR_V1P05 =0 , +SENSOR_V1P8 , +SENSOR_V2P5 , +}; + +static const char *const mpfs_tvs_voltage_labels[] = {"1P05" ,"1P8" ,"2P5" }; + +struct mpfs_tvs { +struct regmap *regmap; +}; + +static int mpfs_tvs_voltage_read (struct mpfs_tvs *data,u32 attr, +int channel,long *val) +{ +u32 tmp, control; + +if (attr != hwmon_in_input && attr != hwmon_in_enable) +return -EOPNOTSUPP ; + +regmap_read (data->regmap,MPFS_TVS_CTRL , &control); + +switch (channel) { +case SENSOR_V2P5 : +if (attr == hwmon_in_enable) { + *val =FIELD_GET (MPFS_TVS_CTRL_V2P5_ENABLE , control); +break ; + } + +if (!(control &MPFS_TVS_CTRL_V2P5_VALID )) +return -EINVAL ; + +regmap_read (data->regmap,MPFS_TVS_OUTPUT1 , &tmp); + *val =FIELD_GET (MPFS_OUTPUT1_V2P5_MASK , tmp); +break ; +case SENSOR_V1P8 : +if (attr == hwmon_in_enable) { + *val =FIELD_GET (MPFS_TVS_CTRL_V1P8_ENABLE , control); +break ; + } + +if (!(control &MPFS_TVS_CTRL_V1P8_VALID )) +return -EINVAL ; + +regmap_read (data->regmap,MPFS_TVS_OUTPUT0 , &tmp); + *val =FIELD_GET (MPFS_OUTPUT0_V1P8_MASK , tmp); +break ; +case SENSOR_V1P05 : +if (attr == hwmon_in_enable) { + *val =FIELD_GET (MPFS_TVS_CTRL_V1P05_ENABLE , control); +break ; + } + +if (!(control &MPFS_TVS_CTRL_V1P05_VALID )) +return -EINVAL ; + +regmap_read (data->regmap,MPFS_TVS_OUTPUT0 , &tmp); + *val =FIELD_GET (MPFS_OUTPUT0_V1P05_MASK , tmp); +break ; +default : +return -EOPNOTSUPP ; + } + +return 0 ; +} + +static int mpfs_tvs_voltage_write (struct mpfs_tvs *data,u32 attr, +int channel,long val) +{ +u32 tmp; + +if (attr != hwmon_in_enable) +return -EOPNOTSUPP ; + +switch (channel) { +case SENSOR_V2P5 : +if (val >1 ) +return -EINVAL ; + + tmp =FIELD_PREP (MPFS_TVS_CTRL_V2P5_ENABLE , val); +regmap_update_bits (data->regmap,MPFS_TVS_CTRL , +MPFS_TVS_CTRL_V2P5_ENABLE , tmp); +break ; +case SENSOR_V1P8 : +if (val >1 ) +return -EINVAL ; + + tmp =FIELD_PREP (MPFS_TVS_CTRL_V1P8_ENABLE , val); +regmap_update_bits (data->regmap,MPFS_TVS_CTRL , +MPFS_TVS_CTRL_V1P8_ENABLE , tmp); +break ; +case SENSOR_V1P05 : +if (val >1 ) +return -EINVAL ; + + tmp =FIELD_PREP (MPFS_TVS_CTRL_V1P05_ENABLE , val); +regmap_update_bits (data->regmap,MPFS_TVS_CTRL , +MPFS_TVS_CTRL_V1P05_ENABLE , tmp); +break ; +default : +return -EOPNOTSUPP ; + } + +return 0 ; +} + +static int mpfs_tvs_temp_read (struct mpfs_tvs *data,u32 attr,long *val) +{ +u32 tmp, control; + +if (attr != hwmon_temp_input && attr != hwmon_temp_enable) +return -EOPNOTSUPP ; + +regmap_read (data->regmap,MPFS_TVS_CTRL , &control); + +if (attr == hwmon_temp_enable) { + *val =FIELD_GET (MPFS_TVS_CTRL_TEMP_ENABLE , control); +return 0 ; + } + +if (!(control &MPFS_TVS_CTRL_TEMP_VALID )) +return -EINVAL ; + +regmap_read (data->regmap,MPFS_TVS_OUTPUT1 , &tmp); + *val =FIELD_GET (MPFS_OUTPUT1_TEMP_MASK , tmp); + *val -=MPFS_TVS_K_TO_C ; + *val = (1000 * *val) >>4 ;/* fixed point (11.4) to millidegrees */ + +return 0 ; +} + +static int mpfs_tvs_temp_write (struct mpfs_tvs *data,u32 attr,long val) +{ +u32 tmp; + +if (attr != hwmon_temp_enable) +return -EOPNOTSUPP ; + +if (val >1 ) +return -EINVAL ; + + tmp =FIELD_PREP (MPFS_TVS_CTRL_TEMP_ENABLE , val); +regmap_update_bits (data->regmap,MPFS_TVS_CTRL , +MPFS_TVS_CTRL_TEMP_ENABLE , tmp); + +return 0 ; +} + +static int mpfs_tvs_interval_read (struct mpfs_tvs *data,u32 attr,long *val) +{ +u32 tmp; + +if (attr != hwmon_chip_update_interval) +return -EOPNOTSUPP ; + +regmap_read (data->regmap,MPFS_TVS_CTRL , &tmp); +//TODO: Guenter, update_interval, as implemented here, expects values +//in microseconds. Is that okay? Most drivers (all that I checked tbh) +//use milliseconds, but this is an 8-bit value so using ms will have +//almost no granularity at all. + *val =FIELD_GET (MPFS_TVS_INTERVAL_MASK , tmp) *MPFS_TVS_INTERVAL_SCALE ; + +return 0 ; +} + +static int mpfs_tvs_interval_write (struct mpfs_tvs *data,u32 attr,long val) +{ +unsigned long temp = val; + +if (attr != hwmon_chip_update_interval) +return -EOPNOTSUPP ; + + temp /=MPFS_TVS_INTERVAL_SCALE ; + +/* +* The value is 8 bits wide, but 255 is described as +* "255= Do single set of transfers when scoverride set" +* but there's no scoverride bit in the tvs register region. +* Ban using 255 since its behaviour is suspect. +*/ +if (temp >254 ) +return -EINVAL ; + + temp <<=MPFS_TVS_INTERVAL_OFFSET ; +regmap_update_bits (data->regmap,MPFS_TVS_CTRL , +MPFS_TVS_INTERVAL_MASK , temp); + +return 0 ; +} + + +static umode_t mpfs_tvs_is_visible (const void *data, +enum hwmon_sensor_types type, +u32 attr,int channel) +{ +if (type == hwmon_chip && attr == hwmon_chip_update_interval) +return 0644 ; + +if (type == hwmon_temp) { +switch (attr) { +case hwmon_temp_enable: +return 0644 ; +case hwmon_temp_input: +case hwmon_temp_label: +return 0444 ; +default : +return 0 ; + } + } + +if (type == hwmon_in) { +switch (attr) { +case hwmon_in_enable: +return 0644 ; +case hwmon_in_input: +case hwmon_in_label: +return 0444 ; +default : +return 0 ; + } + } + +return 0 ; +} + +static int mpfs_tvs_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +struct mpfs_tvs *data =dev_get_drvdata (dev); + +switch (type) { +case hwmon_temp: +return mpfs_tvs_temp_read (data, attr, val); +case hwmon_in: +return mpfs_tvs_voltage_read (data, attr, channel, val); +case hwmon_chip: +return mpfs_tvs_interval_read (data, attr, val); +default : +return -EOPNOTSUPP ; + } +} + +static int mpfs_tvs_write (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long val) +{ +struct mpfs_tvs *data =dev_get_drvdata (dev); + +switch (type) { +case hwmon_temp: +return mpfs_tvs_temp_write (data, attr, val); +case hwmon_in: +return mpfs_tvs_voltage_write (data, attr, channel, val); +case hwmon_chip: +return mpfs_tvs_interval_write (data, attr, val); +default : +return -EOPNOTSUPP ; + } +} + +static int mpfs_tvs_read_labels (struct device *dev, +enum hwmon_sensor_types type, +u32 attr,int channel, +const char **str) +{ +switch (type) { +case hwmon_temp: + *str ="Die Temp" ; +return 0 ; +case hwmon_in: + *str = mpfs_tvs_voltage_labels[channel]; +return 0 ; +default : +return -EOPNOTSUPP ; + } +} + +static const struct hwmon_ops mpfs_tvs_ops = { + .is_visible = mpfs_tvs_is_visible, + .read_string = mpfs_tvs_read_labels, + .read = mpfs_tvs_read, + .write = mpfs_tvs_write, +}; + +static const struct hwmon_channel_info *mpfs_tvs_info[] = { +HWMON_CHANNEL_INFO (chip, +HWMON_C_REGISTER_TZ |HWMON_C_UPDATE_INTERVAL ), +HWMON_CHANNEL_INFO (temp, +HWMON_T_INPUT |HWMON_T_LABEL |HWMON_T_ENABLE ), +HWMON_CHANNEL_INFO (in, +HWMON_I_INPUT |HWMON_I_LABEL |HWMON_I_ENABLE , +HWMON_I_INPUT |HWMON_I_LABEL |HWMON_I_ENABLE , +HWMON_I_INPUT |HWMON_I_LABEL |HWMON_I_ENABLE ), +NULL +}; + +static const struct hwmon_chip_info mpfs_tvs_chip_info = { + .ops = &mpfs_tvs_ops, + .info = mpfs_tvs_info, +}; + +static int mpfs_tvs_probe (struct platform_device *pdev) +{ +struct device *hwmon_dev; +struct mpfs_tvs *data; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->regmap =device_node_to_regmap (pdev->dev.parent->of_node); +if (IS_ERR (data->regmap)) +return dev_err_probe (&pdev->dev,PTR_ERR (data->regmap), +"Failed to find syscon regmap\n" ); + + hwmon_dev =devm_hwmon_device_register_with_info (&pdev->dev,"mpfs_tvs" , + data, + &mpfs_tvs_chip_info, +NULL ); +if (IS_ERR (hwmon_dev)) +return dev_err_probe (&pdev->dev,PTR_ERR (hwmon_dev), +"hwmon device registration failed.\n" ); + +regmap_write (data->regmap,MPFS_TVS_CTRL ,MPFS_TVS_CTRL_ENABLE_ALL ); + +return 0 ; +} + +static struct platform_driver mpfs_tvs_driver = { + .probe = mpfs_tvs_probe, + .driver = { + .name ="mpfs-tvs" , + }, +}; +module_platform_driver (mpfs_tvs_driver); + +MODULE_AUTHOR ("Lars Randers <[email protected]>" ); +MODULE_DESCRIPTION ("PolarFire SoC temperature & voltage sensor driver" ); +MODULE_LICENSE ("GPL" );Signature · 1 line
2.53.0
Quoted 29 lines · Guenter Roeck
Quoted 18 lines · Conor Dooley
From: Lars Randers <[email protected]>
Add a driver for the temperature and voltage sensors on PolarFire SoC. The temperature reports how hot the die is, and the voltages are the SoC's 1.05, 1.8 and 2.5 volt rails respectively.
The hardware supports alarms in theory, but there is an unconfirmed erratum that prevents clearing them once triggered, so no support is added.
The hardware measures voltage with 16 bits, of which 1 is a sign bit and the remainder holds the voltage as a fixed point integer value. It's improbable that the hardware will work if the voltages are negative, so the driver ignores the sign bits.
There's no dt support etc here because this is the child of a simple-mfd syscon.
Quoted 9 lines
Signed-off-by Lars Randers <[email protected]>Co-developed-by Conor Dooley <[email protected]>Signed-off-by Conor Dooley <[email protected]>---
Guenter, there's one question here about the unit thatupdate_intervalis in, I didn't see anyone else using us, but I assume that's okay since the resolution that ms would give would be 8 steps only?
RFC cos the question is also in the driver as a comment.
Quoted 3 lines
That just came up in a different context. We'll add a new standard attribute
update_interval_us. The existing attribute MUST use ms. Everything else would be an ABI violation.
Cool. Sounds like Ferdinand is working on that based on the other thread.
Do you think I should support both update_interval and update_interval_us in this driver?
If yes, should I do the ms version now and add the us version later once Ferdinand's work is complete?
Cheers,
Conor.
Guenter Roeck
maintainerQuoted 34 lines · Conor Dooley
Quoted 29 lines · Guenter Roeck
Quoted 18 lines · Conor Dooley
From: Lars Randers <[email protected]>
Add a driver for the temperature and voltage sensors on PolarFire SoC. The temperature reports how hot the die is, and the voltages are the SoC's 1.05, 1.8 and 2.5 volt rails respectively.
The hardware supports alarms in theory, but there is an unconfirmed erratum that prevents clearing them once triggered, so no support is added.
The hardware measures voltage with 16 bits, of which 1 is a sign bit and the remainder holds the voltage as a fixed point integer value. It's improbable that the hardware will work if the voltages are negative, so the driver ignores the sign bits.
There's no dt support etc here because this is the child of a simple-mfd syscon.
Quoted 9 lines
Signed-off-by Lars Randers <[email protected]>Co-developed-by Conor Dooley <[email protected]>Signed-off-by Conor Dooley <[email protected]>---
Guenter, there's one question here about the unit thatupdate_intervalis in, I didn't see anyone else using us, but I assume that's okay since the resolution that ms would give would be 8 steps only?
RFC cos the question is also in the driver as a comment.Quoted 3 lines
That just came up in a different context. We'll add a new standard attribute
update_interval_us. The existing attribute MUST use ms. Everything else would be an ABI violation.
Quoted 6 lines
Cool. Sounds like Ferdinand is working on that based on the other thread.
Do you think I should support both
update_intervalandupdate_interval_usin this driver?
I have been thinking about only supporting one for a given driver, but that doesn't work because there are existing drivers which would benefit from supporting microsecond-based intervals, so both it is (unless you want to wait for _us support to be available and then just support that).
Quoted 3 lines
If yes, should I do the ms version now and add the us version later once Ferdinand's work is complete?
Yes, that is what I would recommend.
Thanks,
Guenter