From: W_Armin@gmx.de
To: hdegoede@redhat.com
Cc: linux-hwmon@vger.kernel.org, Armin Wolf <W_Armin@gmx.de>
Subject: [PATCH 2/2] hwmon: (sch5627) Split sch5627_update_device()
Date: Sun, 11 Apr 2021 18:42:25 +0200 [thread overview]
Message-ID: <20210411164225.11967-3-W_Armin@gmx.de> (raw)
In-Reply-To: <20210411164225.11967-1-W_Armin@gmx.de>
From: Armin Wolf <W_Armin@gmx.de>
An error in sch5627_update_device() could cause sch5627_read()
to fail even if the error did not affect the target sensor type.
Split sch5627_update_device() to prevent that.
Tested on a Fujitsu Esprimo P720.
Signed-off-by: Armin Wolf <W_Armin@gmx.de>
---
drivers/hwmon/sch5627.c | 102 +++++++++++++++++++++++++++-------------
1 file changed, 69 insertions(+), 33 deletions(-)
diff --git a/drivers/hwmon/sch5627.c b/drivers/hwmon/sch5627.c
index 023376082ee6..9735f3a81cbb 100644
--- a/drivers/hwmon/sch5627.c
+++ b/drivers/hwmon/sch5627.c
@@ -73,66 +73,96 @@ struct sch5627_data {
struct mutex update_lock;
unsigned long last_battery; /* In jiffies */
- char valid; /* !=0 if following fields are valid */
- unsigned long last_updated; /* In jiffies */
+ char temp_valid; /* !=0 if following fields are valid */
+ char fan_valid;
+ char in_valid;
+ unsigned long temp_last_updated; /* In jiffies */
+ unsigned long fan_last_updated;
+ unsigned long in_last_updated;
u16 temp[SCH5627_NO_TEMPS];
u16 fan[SCH5627_NO_FANS];
u16 in[SCH5627_NO_IN];
};
-static struct sch5627_data *sch5627_update_device(struct device *dev)
+static int sch5627_update_temp(struct sch5627_data *data)
{
- struct sch5627_data *data = dev_get_drvdata(dev);
- struct sch5627_data *ret = data;
+ int ret = 0;
int i, val;
mutex_lock(&data->update_lock);
- /* Trigger a Vbat voltage measurement every 5 minutes */
- if (time_after(jiffies, data->last_battery + 300 * HZ)) {
- sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL,
- data->control | 0x10);
- data->last_battery = jiffies;
- }
-
/* Cache the values for 1 second */
- if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+ if (time_after(jiffies, data->temp_last_updated + HZ) || !data->temp_valid) {
for (i = 0; i < SCH5627_NO_TEMPS; i++) {
- val = sch56xx_read_virtual_reg12(data->addr,
- SCH5627_REG_TEMP_MSB[i],
- SCH5627_REG_TEMP_LSN[i],
- SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
+ val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_TEMP_MSB[i],
+ SCH5627_REG_TEMP_LSN[i],
+ SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
if (unlikely(val < 0)) {
- ret = ERR_PTR(val);
+ ret = val;
goto abort;
}
data->temp[i] = val;
}
+ data->temp_last_updated = jiffies;
+ data->temp_valid = 1;
+ }
+abort:
+ mutex_unlock(&data->update_lock);
+ return ret;
+}
+
+static int sch5627_update_fan(struct sch5627_data *data)
+{
+ int ret = 0;
+ int i, val;
+ mutex_lock(&data->update_lock);
+
+ /* Cache the values for 1 second */
+ if (time_after(jiffies, data->fan_last_updated + HZ) || !data->fan_valid) {
for (i = 0; i < SCH5627_NO_FANS; i++) {
- val = sch56xx_read_virtual_reg16(data->addr,
- SCH5627_REG_FAN[i]);
+ val = sch56xx_read_virtual_reg16(data->addr, SCH5627_REG_FAN[i]);
if (unlikely(val < 0)) {
- ret = ERR_PTR(val);
+ ret = val;
goto abort;
}
data->fan[i] = val;
}
+ data->fan_last_updated = jiffies;
+ data->fan_valid = 1;
+ }
+abort:
+ mutex_unlock(&data->update_lock);
+ return ret;
+}
+
+static int sch5627_update_in(struct sch5627_data *data)
+{
+ int ret = 0;
+ int i, val;
+
+ mutex_lock(&data->update_lock);
+ /* Trigger a Vbat voltage measurement every 5 minutes */
+ if (time_after(jiffies, data->last_battery + 300 * HZ)) {
+ sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL, data->control | 0x10);
+ data->last_battery = jiffies;
+ }
+
+ /* Cache the values for 1 second */
+ if (time_after(jiffies, data->in_last_updated + HZ) || !data->in_valid) {
for (i = 0; i < SCH5627_NO_IN; i++) {
- val = sch56xx_read_virtual_reg12(data->addr,
- SCH5627_REG_IN_MSB[i],
- SCH5627_REG_IN_LSN[i],
- SCH5627_REG_IN_HIGH_NIBBLE[i]);
+ val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_IN_MSB[i],
+ SCH5627_REG_IN_LSN[i],
+ SCH5627_REG_IN_HIGH_NIBBLE[i]);
if (unlikely(val < 0)) {
- ret = ERR_PTR(val);
+ ret = val;
goto abort;
}
data->in[i] = val;
}
-
- data->last_updated = jiffies;
- data->valid = 1;
+ data->in_last_updated = jiffies;
+ data->in_valid = 1;
}
abort:
mutex_unlock(&data->update_lock);
@@ -200,14 +230,14 @@ static umode_t sch5627_is_visible(const void *drvdata, enum hwmon_sensor_types t
static int sch5627_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
long *val)
{
- struct sch5627_data *data = sch5627_update_device(dev);
+ struct sch5627_data *data = dev_get_drvdata(dev);
int ret;
- if (IS_ERR(data))
- return PTR_ERR(data);
-
switch (type) {
case hwmon_temp:
+ ret = sch5627_update_temp(data);
+ if (ret < 0)
+ return ret;
switch (attr) {
case hwmon_temp_input:
*val = reg_to_temp(data->temp[channel]);
@@ -226,6 +256,9 @@ static int sch5627_read(struct device *dev, enum hwmon_sensor_types type, u32 at
}
break;
case hwmon_fan:
+ ret = sch5627_update_fan(data);
+ if (ret < 0)
+ return ret;
switch (attr) {
case hwmon_fan_input:
ret = reg_to_rpm(data->fan[channel]);
@@ -247,6 +280,9 @@ static int sch5627_read(struct device *dev, enum hwmon_sensor_types type, u32 at
}
break;
case hwmon_in:
+ ret = sch5627_update_in(data);
+ if (ret < 0)
+ return ret;
switch (attr) {
case hwmon_in_input:
*val = DIV_ROUND_CLOSEST(data->in[channel] * SCH5627_REG_IN_FACTOR[channel],
--
2.20.1
next prev parent reply other threads:[~2021-04-11 16:42 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-11 16:42 [PATCH 0/2] hwmon: (sch5627) Replace hwmon_device_register() W_Armin
2021-04-11 16:42 ` [PATCH 1/2] hwmon: (sch5627) Convert to hwmon_device_register_with_info() W_Armin
2021-04-11 16:42 ` W_Armin [this message]
2021-04-13 16:39 ` [PATCH 2/2] hwmon: (sch5627) Split sch5627_update_device() Guenter Roeck
2021-04-13 8:11 ` [PATCH 0/2] hwmon: (sch5627) Replace hwmon_device_register() Hans de Goede
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210411164225.11967-3-W_Armin@gmx.de \
--to=w_armin@gmx.de \
--cc=hdegoede@redhat.com \
--cc=linux-hwmon@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox