Commit 6a8cdd14 authored by Chris Packham's avatar Chris Packham Committed by Guenter Roeck

hwmon: (adm9240) Create functions for updating measure and config

Split the body of adm9240_update_device() into two helper functions
adm9240_update_measure() and adm9240_update_config(). Although neither
of the new helpers returns an error yet lay the groundwork for
propagating failures through to the sysfs readers.
Signed-off-by: default avatarChris Packham <chris.packham@alliedtelesis.co.nz>
Link: https://lore.kernel.org/r/20200924085102.15219-3-chris.packham@alliedtelesis.co.nzSigned-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent 10d09773
...@@ -158,18 +158,11 @@ static void adm9240_write_fan_div(struct i2c_client *client, int nr, ...@@ -158,18 +158,11 @@ static void adm9240_write_fan_div(struct i2c_client *client, int nr,
nr + 1, 1 << old, 1 << fan_div); nr + 1, 1 << old, 1 << fan_div);
} }
static struct adm9240_data *adm9240_update_device(struct device *dev) static int adm9240_update_measure(struct adm9240_data *data)
{ {
struct adm9240_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client; struct i2c_client *client = data->client;
int i; int i;
mutex_lock(&data->update_lock);
/* minimum measurement cycle: 1.75 seconds */
if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
|| !data->valid) {
for (i = 0; i < 6; i++) { /* read voltages */ for (i = 0; i < 6; i++) { /* read voltages */
data->in[i] = i2c_smbus_read_byte_data(client, data->in[i] = i2c_smbus_read_byte_data(client,
ADM9240_REG_IN(i)); ADM9240_REG_IN(i));
...@@ -206,12 +199,14 @@ static struct adm9240_data *adm9240_update_device(struct device *dev) ...@@ -206,12 +199,14 @@ static struct adm9240_data *adm9240_update_device(struct device *dev)
data->fan_min[i] /= 2; data->fan_min[i] /= 2;
} }
} }
data->last_updated_measure = jiffies;
}
/* minimum config reading cycle: 300 seconds */ return 0;
if (time_after(jiffies, data->last_updated_config + (HZ * 300)) }
|| !data->valid) {
static int adm9240_update_config(struct adm9240_data *data)
{
struct i2c_client *client = data->client;
int i;
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
data->in_min[i] = i2c_smbus_read_byte_data(client, data->in_min[i] = i2c_smbus_read_byte_data(client,
...@@ -239,6 +234,37 @@ static struct adm9240_data *adm9240_update_device(struct device *dev) ...@@ -239,6 +234,37 @@ static struct adm9240_data *adm9240_update_device(struct device *dev)
data->aout = i2c_smbus_read_byte_data(client, data->aout = i2c_smbus_read_byte_data(client,
ADM9240_REG_ANALOG_OUT); ADM9240_REG_ANALOG_OUT);
return 0;
}
static struct adm9240_data *adm9240_update_device(struct device *dev)
{
struct adm9240_data *data = dev_get_drvdata(dev);
int err;
mutex_lock(&data->update_lock);
/* minimum measurement cycle: 1.75 seconds */
if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
|| !data->valid) {
err = adm9240_update_measure(data);
if (err < 0) {
data->valid = 0;
mutex_unlock(&data->update_lock);
return ERR_PTR(err);
}
data->last_updated_measure = jiffies;
}
/* minimum config reading cycle: 300 seconds */
if (time_after(jiffies, data->last_updated_config + (HZ * 300))
|| !data->valid) {
err = adm9240_update_config(data);
if (err < 0) {
data->valid = 0;
mutex_unlock(&data->update_lock);
return ERR_PTR(err);
}
data->last_updated_config = jiffies; data->last_updated_config = jiffies;
data->valid = 1; data->valid = 1;
} }
...@@ -253,6 +279,10 @@ static ssize_t temp1_input_show(struct device *dev, ...@@ -253,6 +279,10 @@ static ssize_t temp1_input_show(struct device *dev,
struct device_attribute *dummy, char *buf) struct device_attribute *dummy, char *buf)
{ {
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", data->temp / 128 * 500); /* 9-bit value */ return sprintf(buf, "%d\n", data->temp / 128 * 500); /* 9-bit value */
} }
...@@ -261,6 +291,10 @@ static ssize_t max_show(struct device *dev, struct device_attribute *devattr, ...@@ -261,6 +291,10 @@ static ssize_t max_show(struct device *dev, struct device_attribute *devattr,
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000); return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
} }
...@@ -295,6 +329,10 @@ static ssize_t in_show(struct device *dev, struct device_attribute *devattr, ...@@ -295,6 +329,10 @@ static ssize_t in_show(struct device *dev, struct device_attribute *devattr,
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index], return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
attr->index)); attr->index));
} }
...@@ -304,6 +342,10 @@ static ssize_t in_min_show(struct device *dev, ...@@ -304,6 +342,10 @@ static ssize_t in_min_show(struct device *dev,
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index], return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
attr->index)); attr->index));
} }
...@@ -313,6 +355,10 @@ static ssize_t in_max_show(struct device *dev, ...@@ -313,6 +355,10 @@ static ssize_t in_max_show(struct device *dev,
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index], return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
attr->index)); attr->index));
} }
...@@ -386,6 +432,10 @@ static ssize_t fan_show(struct device *dev, struct device_attribute *devattr, ...@@ -386,6 +432,10 @@ static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
1 << data->fan_div[attr->index])); 1 << data->fan_div[attr->index]));
} }
...@@ -395,6 +445,10 @@ static ssize_t fan_min_show(struct device *dev, ...@@ -395,6 +445,10 @@ static ssize_t fan_min_show(struct device *dev,
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index], return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
1 << data->fan_div[attr->index])); 1 << data->fan_div[attr->index]));
} }
...@@ -404,6 +458,10 @@ static ssize_t fan_div_show(struct device *dev, ...@@ -404,6 +458,10 @@ static ssize_t fan_div_show(struct device *dev,
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]); return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
} }
...@@ -490,6 +548,10 @@ static ssize_t alarms_show(struct device *dev, ...@@ -490,6 +548,10 @@ static ssize_t alarms_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%u\n", data->alarms); return sprintf(buf, "%u\n", data->alarms);
} }
static DEVICE_ATTR_RO(alarms); static DEVICE_ATTR_RO(alarms);
...@@ -499,6 +561,10 @@ static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, ...@@ -499,6 +561,10 @@ static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
{ {
int bitnr = to_sensor_dev_attr(attr)->index; int bitnr = to_sensor_dev_attr(attr)->index;
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
} }
static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0); static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
...@@ -516,6 +582,10 @@ static ssize_t cpu0_vid_show(struct device *dev, ...@@ -516,6 +582,10 @@ static ssize_t cpu0_vid_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
} }
static DEVICE_ATTR_RO(cpu0_vid); static DEVICE_ATTR_RO(cpu0_vid);
...@@ -525,6 +595,10 @@ static ssize_t aout_output_show(struct device *dev, ...@@ -525,6 +595,10 @@ static ssize_t aout_output_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct adm9240_data *data = adm9240_update_device(dev); struct adm9240_data *data = adm9240_update_device(dev);
if (IS_ERR(data))
return PTR_ERR(data);
return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment