Skip to contentSkip to conversation

hwmon: (tmp401) Add support for update_interval attribute

PATCH Superseded Guenter Roeck 2 participants 5 messages Apr 6, 2013 – Apr 14, 2013 Hardware monitoring

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 4/5 hwmon: (tmp401) Add support for update_interval attribute

---
 drivers/hwmon/tmp401.c |   54 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)
drivers/hwmon/tmp401.c +53 -1
index c0cf87d..e16544a 100644
@@ -126,6 +126,8 @@ struct tmp401_data {
 	unsigned long last_updated; /* in jiffies */
 	enum chips kind;
 
+	int update_interval;	/* in milliseconds */
+
 	/* register values */
 	u8 status;
 	u8 config;
@@ -193,10 +195,13 @@ static struct tmp401_data *tmp401_update_device(struct device *dev)
 	struct tmp401_data *data = i2c_get_clientdata(client);
 	struct tmp401_data *ret = data;
 	int val;
+	unsigned long next_update;
 
 	mutex_lock(&data->update_lock);
 
-	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+	next_update = data->last_updated +
+	  msecs_to_jiffies(data->update_interval) + 1;
+	if (time_after(jiffies, next_update) || !data->valid) {
 		val = i2c_smbus_read_byte_data(client, TMP401_STATUS);
 		if (val < 0) {
 			ret = ERR_PTR(val);
@@ -369,6 +374,46 @@ static ssize_t reset_temp_history(struct device *dev,
 	return count;
 }
 
+static ssize_t show_update_interval(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct tmp401_data *data = i2c_get_clientdata(client);
+
+	return sprintf(buf, "%u\n", data->update_interval);
+}
+
+static ssize_t set_update_interval(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct tmp401_data *data = i2c_get_clientdata(client);
+	unsigned long val;
+	int err, rate;
+
+	err = kstrtoul(buf, 10, &val);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+	/*
+	 * For valid rates, interval can be calculated as
+	 *	interval = (1 << (7 - rate)) * 125;
+	 * Rounded rate is therefore
+	 *	rate = 8 - fls(interval * 4 / (125 * 3));
+	 * Use clamp_val() to avoid overflows, and to clamp the result
+	 * to its valid range.
+	 */
+	val = clamp_val(val, 0, 100000);
+	rate = clamp_val(8 - fls(val * 4 / (125 * 3)), 0, 7);
+	i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, rate);
+	data->update_interval = (1 << (7 - rate)) * 125;
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IWUSR | S_IRUGO, show_temp,
 			    store_temp, 1, 0);
@@ -402,6 +447,9 @@ static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_status, NULL,
 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_status, NULL,
 			  TMP401_STATUS_REMOTE_CRIT);
 
+static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
+		   set_update_interval);
+
 static struct attribute *tmp401_attributes[] = {
 	&sensor_dev_attr_temp1_input.dev_attr.attr,
 	&sensor_dev_attr_temp1_min.dev_attr.attr,
@@ -422,6 +470,8 @@ static struct attribute *tmp401_attributes[] = {
 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
 
+	&dev_attr_update_interval.attr,
+
 	NULL
 };
 
@@ -463,9 +513,11 @@ static const struct attribute_group tmp411_group = {
 static void tmp401_init_client(struct i2c_client *client)
 {
 	int config, config_orig;
+	struct tmp401_data *data = i2c_get_clientdata(client);
 
 	/* Set the conversion rate to 2 Hz */
 	i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
+	data->update_interval = 500;
 
 	/* Start conversions (disable shutdown if necessary) */
 	config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
Signature · 7 lines
1.7.9.7


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw

Hi Guenter,

Quoted 4 lines · Guenter Roeck
---
 drivers/hwmon/tmp401.c |   54 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)

Looks good overall, with minor comments though:

drivers/hwmon/tmp401.c struct tmp401_data { · line 129

Quoted 9 lines
drivers/hwmon/tmp401.c +1 -0
index c0cf87d..e16544a 100644
@@ -126,6 +126,8 @@ struct tmp401_data {
 	unsigned long last_updated; /* in jiffies */
 	enum chips kind;
 
+	int update_interval;	/* in milliseconds */

Should be unsigned. As a matter of fact you print it with %u not %d.

Quoted 5 lines
drivers/hwmon/tmp401.c +1 -0
+
 	/* register values */
 	u8 status;
 	u8 config;

drivers/hwmon/tmp401.c static struct tmp401_data *tmp401_update_device(struct device *dev) · line 203

Quoted 10 lines
drivers/hwmon/tmp401.c +3 -1
@@ -193,10 +195,13 @@ static struct tmp401_data *tmp401_update_device(struct device *dev)
 	struct tmp401_data *data = i2c_get_clientdata(client);
 	struct tmp401_data *ret = data;
 	int val;
+	unsigned long next_update;
 
 	mutex_lock(&data->update_lock);
 
-	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+	next_update = data->last_updated +
+	  msecs_to_jiffies(data->update_interval) + 1;

More indentation would make this more readable IMHO (for example align on "=".)

Quoted 19 lines
drivers/hwmon/tmp401.c +14 -0
+	if (time_after(jiffies, next_update) || !data->valid) {
 		val = i2c_smbus_read_byte_data(client, TMP401_STATUS);
 		if (val < 0) {
 			ret = ERR_PTR(val);
 
(...)
+static ssize_t set_update_interval(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct tmp401_data *data = i2c_get_clientdata(client);
+	unsigned long val;
+	int err, rate;
+
+	err = kstrtoul(buf, 10, &val);
+	if (err)
+		return err;
+

drivers/hwmon/tmp401.c static struct tmp401_data *tmp401_update_device(struct device *dev)

Quoted 10 lines
drivers/hwmon/tmp401.c +10 -0
+	mutex_lock(&data->update_lock);
+	/*
+	 * For valid rates, interval can be calculated as
+	 *	interval = (1 << (7 - rate)) * 125;
+	 * Rounded rate is therefore
+	 *	rate = 8 - fls(interval * 4 / (125 * 3));
+	 * Use clamp_val() to avoid overflows, and to clamp the result
+	 * to its valid range.
+	 */
+	val = clamp_val(val, 0, 100000);

Use clamp_val(val, 125, 16000), then you no longer need the second call to clamp_val(). And then you could also use __fls(), which is faster than fls() and starts counting at 0 so no s/7/8/ magic needed. Lastly, note that the computation can be done outside of the locked section,

Quoted 7 lines
drivers/hwmon/tmp401.c +7 -0
+	rate = clamp_val(8 - fls(val * 4 / (125 * 3)), 0, 7);
+	i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, rate);
+	data->update_interval = (1 << (7 - rate)) * 125;
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
author maintainer
#2 lore raw
In reply to Guenter Roeck
Quoted 67 lines · Jean Delvare

Hi Guenter,

Quoted 4 lines · Guenter Roeck
---
 drivers/hwmon/tmp401.c |   54 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)

Looks good overall, with minor comments though:

drivers/hwmon/tmp401.c struct tmp401_data { · line 129

Quoted 9 lines
drivers/hwmon/tmp401.c +1 -0
index c0cf87d..e16544a 100644
@@ -126,6 +126,8 @@ struct tmp401_data {
 	unsigned long last_updated; /* in jiffies */
 	enum chips kind;
 
+	int update_interval;	/* in milliseconds */

Should be unsigned. As a matter of fact you print it with %u not %d.

Quoted 5 lines
drivers/hwmon/tmp401.c +1 -0
+
 	/* register values */
 	u8 status;
 	u8 config;

drivers/hwmon/tmp401.c static struct tmp401_data *tmp401_update_device(struct device *dev) · line 203

Quoted 10 lines
drivers/hwmon/tmp401.c +3 -1
@@ -193,10 +195,13 @@ static struct tmp401_data *tmp401_update_device(struct device *dev)
 	struct tmp401_data *data = i2c_get_clientdata(client);
 	struct tmp401_data *ret = data;
 	int val;
+	unsigned long next_update;
 
 	mutex_lock(&data->update_lock);
 
-	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+	next_update = data->last_updated +
+	  msecs_to_jiffies(data->update_interval) + 1;

More indentation would make this more readable IMHO (for example align on "=".)

Quoted 26 lines
drivers/hwmon/tmp401.c +21 -0
+	if (time_after(jiffies, next_update) || !data->valid) {
 		val = i2c_smbus_read_byte_data(client, TMP401_STATUS);
 		if (val < 0) {
 			ret = ERR_PTR(val);
 
(...)
+static ssize_t set_update_interval(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct tmp401_data *data = i2c_get_clientdata(client);
+	unsigned long val;
+	int err, rate;
+
+	err = kstrtoul(buf, 10, &val);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+	/*
+	 * For valid rates, interval can be calculated as
+	 *	interval = (1 << (7 - rate)) * 125;
+	 * Rounded rate is therefore
+	 *	rate = 8 - fls(interval * 4 / (125 * 3));
+	 * Use clamp_val() to avoid overflows, and to clamp the result
Quoted 9 lines

drivers/hwmon/tmp401.c static ssize_t reset_temp_history(struct device *dev, · line 408

Quoted 3 lines
drivers/hwmon/tmp401.c +3 -0
+	 * to its valid range.
+	 */
+	val = clamp_val(val, 0, 100000);

Use clamp_val(val, 125, 16000), then you no longer need the second call to clamp_val(). And then you could also use __fls(), which is faster than fls() and starts counting at 0 so no s/7/8/ magic needed. Lastly, note that the computation can be done outside of the locked section,

Hi Jean,

Excellent idea.

Something else: The TMP4xx chips have a separate resolution register. Wonder if it is time to add a "resolution" attribute to the ABI, especially since this has come up a couple of times already. Thoughts ?

Thanks,
Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#3 lore raw
In reply to Guenter Roeck
Quoted 3 lines · Guenter Roeck

Something else: The TMP4xx chips have a separate resolution register. Wonder if it is time to add a "resolution" attribute to the ABI, especially since this has come up a couple of times already. Thoughts ?

As you wish. For the lm75 driver I decided to enforce what I thought were sane per-chip defaults. My idea was that anyone not happy with these would add and provide platform data to change it. I don't really expect people to change the resolution of the sensor on-the-fly, but maybe I'm wrong... It might make sense to do so to save power on some platforms, I don't know. If you have any need for it, just make a proposal for a new attribute and I'll review it.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
author maintainer
#4 lore raw
In reply to Guenter Roeck
Quoted 5 lines · Jean Delvare
Quoted 3 lines · Guenter Roeck

Something else: The TMP4xx chips have a separate resolution register. Wonder if it is time to add a "resolution" attribute to the ABI, especially since this has come up a couple of times already. Thoughts ?

Quoted 8 lines

As you wish. For the lm75 driver I decided to enforce what I thought were sane per-chip defaults. My idea was that anyone not happy with these would add and provide platform data to change it. I don't really expect people to change the resolution of the sensor on-the-fly, but maybe I'm wrong... It might make sense to do so to save power on some platforms, I don't know. If you have any need for it, just make a proposal for a new attribute and I'll review it.

Same logic as with limits. One would not expect to set them on the fly either, but it may be necessary or at least desirable to be able to do it once. "sensors -s" is one approach, another might be platform or devicetree data.

As for this one, I don't know yet if there is a requirement not to use the default. I'll see.

Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors