hwmon: (jc42) regmap conversion and resume fix
v3 cover hwmon: (jc42) regmap conversion and resume fix
Hello,
this is a follow-up to the comments I got from Guenter on v1 of my patch from [0] titled:
"hwmon: (jc42) Restore the min/max/critical temperatures on resume"
There Guenter suggested: "The best solution would probably be to convert the driver to use regmap and let regmap handle the caching". That's the goal of this series - in addition to fixing the original resume issue (see patch #3 - which was the reason for v1 of this series).
Guenter suggested:
Quoted 3 lines
Make sure that the alarm bits are not dropped after reading the temperature (running the 'sensors' command with alarms active should do)
I configured the limits to be below the case temperature on my system (as the jc42 sensor - a ST Microelectronics STTS2004 - is part of the DIMMs) and ran sensors three times in a row. The output is the same for all runs:
temp1: +35.0°C (low = +0.0°C) ALARM (HIGH, CRIT)
(high = +25.0°C, hyst = +25.0°C)
(crit = +30.0°C, hyst = +30.0°C)My conclusion is that the alarm bit is not dropped after reading the temperature.
Changes sinc v2 at [1]:
- squashed patches #1, #2 and #4 into the new patch #1 (without any other changes to content in jc42.c)
- patch #3 has no changes other than it's numbering (see previous change)
- dropped RFC prefix
Changes since v1 at [0]:
- marked as RFC
- added patches for regmap (patch #1) and regcache (patch #2) conversion
- patch #3 has been updated to use regcache for restoring the register values during system resume (this was originally patch 1/1)
- added another patch to remove caching of the temperature register
[0] https://lore.kernel.org/linux-hwmon/[email protected]/
[1] https://lore.kernel.org/linux-hwmon/[email protected]/
Martin Blumenstingl (2):
hwmon: (jc42) Convert register access and caching to regmap/regcache
hwmon: (jc42) Restore the min/max/critical temperatures on resume
drivers/hwmon/Kconfig | 1 + drivers/hwmon/jc42.c | 224 +++++++++++++++++++++++------------------- 2 files changed, 125 insertions(+), 100 deletions(-)
Signature · 1 line
2.38.1
PATCH v3 1/2 hwmon: (jc42) Convert register access and caching to regmap/regcache
Switch the jc42 driver to use an I2C regmap to access the registers. Also move over to regmap's built-in caching instead of adding a custom caching implementation. This works for JC42_REG_TEMP_UPPER, JC42_REG_TEMP_LOWER and JC42_REG_TEMP_CRITICAL as these values never change except when explicitly written. The cache For JC42_REG_TEMP is dropped (regmap can't cache it because it's volatile, meaning it can change at any time) as well for simplicity and consistency with other drivers.
--- drivers/hwmon/Kconfig | 1 + drivers/hwmon/jc42.c | 214 +++++++++++++++++++++++------------------- 2 files changed, 116 insertions(+), 99 deletions(-)
drivers/hwmon/Kconfig +1 -0
@@ -799,6 +799,7 @@ config SENSORS_IT87 config SENSORS_JC42 tristate "JEDEC JC42.4 compliant memory module temperature sensors" depends on I2C + select REGMAP_I2C help If you say yes here, you get support for JEDEC JC42.4 compliant temperature sensors, which are used on many DDR3 memory modules for
drivers/hwmon/jc42.c +115 -99
353 lines not on this page · raw message
Signature · 1 line
2.38.1
Guenter Roeck
maintainerRe: [PATCH v3 1/2] hwmon: (jc42) Convert register access and caching to regmap/regcache
Quoted 11 lines · Martin Blumenstingl
Switch the jc42 driver to use an I2C regmap to access the registers. Also move over to regmap's built-in caching instead of adding a custom caching implementation. This works for
JC42_REG_TEMP_UPPER,JC42_REG_TEMP_LOWERandJC42_REG_TEMP_CRITICALas these values never change except when explicitly written. The cache ForJC42_REG_TEMPis dropped (regmap can't cache it because it's volatile, meaning it can change at any time) as well for simplicity and consistency with other drivers.Signed-off-by Martin Blumenstingl <[email protected]>---
...
Quoted 19 lines
drivers/hwmon/jc42.c +6
-1case hwmon_temp_crit_hyst: + ret =regmap_read (data->regmap,JC42_REG_TEMP_CRITICAL , + ®val); +if (ret) +return ret; +/* * JC42.4 compliant chips only support four hysteresis values. * Pick best choice and go from there. @@ -356,7 +349,7 @@ static int jc42_write(struct device *dev, enum hwmon_sensor_types type, val =clamp_val (val, (data->extended ?JC42_TEMP_MIN_EXTENDED :JC42_TEMP_MIN ) -6000 ,JC42_TEMP_MAX ); - diff =jc42_temp_from_reg (data->temp[t_crit]) - val; + diff =jc42_temp_from_reg (regval) - val; hyst =0 ;if (diff >0 ) {if (diff <2250 )
drivers/hwmon/jc42.c static int jc42_write(struct device *dev, enum hwmon_sensor_types type, · line 366
Quoted 9 lines
drivers/hwmon/jc42.c +2
-3@@ -368,17 +361,14 @@ static int jc42_write(struct device *dev, enum hwmon_sensor_types type, } data->config = (data->config & ~JC42_CFG_HYST_MASK ) | (hyst <<JC42_CFG_HYST_SHIFT ); - ret =i2c_smbus_write_word_swapped (data->client, -JC42_REG_CONFIG , - data->config); + ret =regmap_write (data->regmap,JC42_REG_CONFIG , + data->config);break ;
This code sequence still requires a mutex since another thread could modify the upper limit (and/or the hysteresis) while the hysteresis is in the process of being written. Worst case there could be a mismatch between the value in data->config and the value actually written into the chip. Granted, that is unlikely to happen, but the race still exists.
Thanks,
Guenter
Re: [PATCH v3 1/2] hwmon: (jc42) Convert register access and caching to regmap/regcache
Hi Guenter,
On Fri, Oct 21, 2022 at 7:11 PM Guenter Roeck <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/jc42.c +1
-3@@ -368,17 +361,14 @@ static int jc42_write(struct device *dev, enum hwmon_sensor_types type, } data->config = (data->config & ~JC42_CFG_HYST_MASK ) | (hyst <<JC42_CFG_HYST_SHIFT ); - ret =i2c_smbus_write_word_swapped (data->client, -JC42_REG_CONFIG , - data->config); + ret = regmap_write(data->regmap, JC42_REG_CONFIG,
Quoted 8 lines
drivers/hwmon/jc42.c static int jc42_write(struct device *dev, enum hwmon_sensor_types type, · line 366
Quoted 2 lines
drivers/hwmon/jc42.c +1
-0+ data->config);break ;This code sequence still requires a mutex since another thread could modify the upper limit (and/or the hysteresis) while the hysteresis is in the process of being written. Worst case there could be a mismatch between the value in
data->configand the value actually written into the chip. Granted, that is unlikely to happen, but the race still exists.
Thanks for spotting this - this is indeed a potential issue. Do you also want me to add locking for the data->config access (read) in jc42_read()? Without a lock there in theory jc42_write() may have already updated data->config with a new value while hardware still has the old value. So in the end the read output may show a hysteresis which was not programmed to the registers at that time.
Best regards,
Martin
Guenter Roeck
maintainerRe: [PATCH v3 1/2] hwmon: (jc42) Convert register access and caching to regmap/regcache
Quoted 20 lines · Martin Blumenstingl
Hi Guenter,
On Fri, Oct 21, 2022 at 7:11 PM Guenter Roeck <[email protected]> wrote:
[...]Quoted 8 lines
Quoted 8 lines
drivers/hwmon/jc42.c +1
-3@@ -368,17 +361,14 @@ static int jc42_write(struct device *dev, enum hwmon_sensor_types type, } data->config = (data->config & ~JC42_CFG_HYST_MASK ) | (hyst <<JC42_CFG_HYST_SHIFT ); - ret =i2c_smbus_write_word_swapped (data->client, -JC42_REG_CONFIG , - data->config); + ret = regmap_write(data->regmap, JC42_REG_CONFIG,Quoted 8 lines
drivers/hwmon/jc42.c static int jc42_write(struct device *dev, enum hwmon_sensor_types type, · line 366
Quoted 2 lines
drivers/hwmon/jc42.c +1
-0+ data->config);break ;This code sequence still requires a mutex since another thread could modify the upper limit (and/or the hysteresis) while the hysteresis is in the process of being written. Worst case there could be a mismatch between the value in
data->configand the value actually written into the chip. Granted, that is unlikely to happen, but the race still exists.
Quoted 7 lines
Thanks for spotting this - this is indeed a potential issue. Do you also want me to add locking for the
data->configaccess (read) injc42_read()? Without a lock there in theoryjc42_write()may have already updateddata->configwith a new value while hardware still has the old value. So in the end the read output may show a hysteresis which was not programmed to the registers at that time.
Thanks for noticing. I had missed that one. Yes, please.
Thanks,
Guenter
PATCH v3 2/2 hwmon: (jc42) Restore the min/max/critical temperatures on resume
In reply to Martin BlumenstinglThe JC42 compatible thermal sensor on Kingston KSM32ES8/16ME DIMMs (using Micron E-Die) is an ST Microelectronics STTS2004 (manufacturer 0x104a, device 0x2201). It does not keep the previously programmed minimum, maximum and critical temperatures after system suspend and resume (which is a shutdown / startup cycle for the JC42 temperature sensor). This results in an alarm on system resume because the hardware default for these values is 0°C (so any environment temperature greater than 0°C will trigger the alarm).
Example before system suspend:
jc42-i2c-0-1a
Adapter: SMBus PIIX4 adapter port 0 at 0b00
temp1: +34.8°C (low = +0.0°C)
(high = +85.0°C, hyst = +85.0°C)
(crit = +95.0°C, hyst = +95.0°C)Example after system resume (without this change):
jc42-i2c-0-1a
Adapter: SMBus PIIX4 adapter port 0 at 0b00
temp1: +34.8°C (low = +0.0°C) ALARM (HIGH, CRIT)
(high = +0.0°C, hyst = +0.0°C)
(crit = +0.0°C, hyst = +0.0°C)Apply the cached values from the JC42_REG_TEMP_UPPER, JC42_REG_TEMP_LOWER, JC42_REG_TEMP_CRITICAL and JC42_REG_SMBUS (where the SMBUS register is not related to this issue but a side-effect of using regcache_sync() during system resume with the previously cached/programmed values. This fixes the alarm due to the hardware defaults of 0°C because the previously applied limits (set by userspace) are re-applied on system resume.
--- drivers/hwmon/jc42.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/hwmon/jc42.c +9 -1
@@ -565,6 +565,10 @@ static int jc42_suspend(struct device *dev) data->config |=JC42_CFG_SHUTDOWN ;regmap_write (data->regmap,JC42_REG_CONFIG , data->config); + +regcache_cache_only (data->regmap, true); +regcache_mark_dirty (data->regmap); +return 0 ; }
@@ -572,9 +576,13 @@ static int jc42_resume(struct device *dev) {struct jc42_data *data =dev_get_drvdata (dev); +regcache_cache_only (data->regmap, false); + data->config &= ~JC42_CFG_SHUTDOWN ;regmap_write (data->regmap,JC42_REG_CONFIG , data->config); -return 0 ; + +/* Restore cached register values to hardware */ +return regcache_sync (data->regmap); }static const struct dev_pm_ops jc42_dev_pm_ops = {
Signature · 1 line
2.38.1
Re: [PATCH v3 2/2] hwmon: (jc42) Restore the min/max/critical temperatures on resume
Quoted 31 lines · Martin Blumenstingl
The JC42 compatible thermal sensor on Kingston KSM32ES8/16ME DIMMs (using Micron E-Die) is an ST Microelectronics STTS2004 (manufacturer
0x104a, device0x2201). It does not keep the previously programmed minimum, maximum and critical temperatures after system suspend and resume (which is a shutdown / startup cycle for the JC42 temperature sensor). This results in an alarm on system resume because the hardware default for these values is 0°C (so any environment temperature greater than 0°C will trigger the alarm).Example before system suspend:
jc42-i2c-0-1a
Adapter: SMBus PIIX4 adapter port 0 at 0b00
temp1: +34.8°C (low = +0.0°C)(high = +85.0°C, hyst = +85.0°C) (crit = +95.0°C, hyst = +95.0°C)Example after system resume (without this change):
jc42-i2c-0-1a
Adapter: SMBus PIIX4 adapter port 0 at 0b00
temp1: +34.8°C (low = +0.0°C) ALARM (HIGH, CRIT)(high = +0.0°C, hyst = +0.0°C) (crit = +0.0°C, hyst = +0.0°C)Apply the cached values from the
JC42_REG_TEMP_UPPER,JC42_REG_TEMP_LOWER,JC42_REG_TEMP_CRITICALandJC42_REG_SMBUS(where the SMBUS register is not related to this issue but a side-effect of usingregcache_sync()during system resume with the previously cached/programmed values. This fixes the alarm due to the hardware defaults of 0°C because the previously applied limits (set by userspace) are re-applied on system resume.
Quoted 2 lines
Fixes 175c490c9e7f ("hwmon: (jc42) Add support for STTS2004 and AT30TSE004")Signed-off-by Martin Blumenstingl <[email protected]>
Nice use of regmap.
For my reference:
Thanks,
Guenter
Quoted 37 lines
--- drivers/hwmon/jc42.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)drivers/hwmon/jc42.c +9
-1@@ -565,6 +565,10 @@ static int jc42_suspend(struct device *dev) data->config |=JC42_CFG_SHUTDOWN ;regmap_write (data->regmap,JC42_REG_CONFIG , data->config); + +regcache_cache_only (data->regmap, true); +regcache_mark_dirty (data->regmap); +return 0 ; }@@ -572,9 +576,13 @@ static int jc42_resume(struct device *dev) {struct jc42_data *data =dev_get_drvdata (dev); +regcache_cache_only (data->regmap, false); + data->config &= ~JC42_CFG_SHUTDOWN ;regmap_write (data->regmap,JC42_REG_CONFIG , data->config); -return 0 ; + +/* Restore cached register values to hardware */ +return regcache_sync (data->regmap); }static const struct dev_pm_ops jc42_dev_pm_ops = {Signature · 1 line
2.38.1