Skip to contentSkip to conversation

hwmon: (lm63) Add support for writing the

PATCH In review Guenter Roeck 3 participants 4 messages Jan 10, 2012 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 4/5 hwmon: (lm63) Add support for writing the

From: Guenter Roeck <[email protected]>

On LM64, the external critical temperature limit is always writable. On LM96163, it is writable if the chip is configured for it. Add conditional support for writing the register dependent on chip type and configuration.

---
 drivers/hwmon/lm63.c |   57 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 52 insertions(+), 5 deletions(-)
drivers/hwmon/lm63.c +52 -5
index 24a96f8..6370e28 100644
@@ -117,6 +117,9 @@ static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
 				 (val) >= 127000 ? 127 : \
 				 (val) < 0 ? ((val) - 500) / 1000 : \
 				 ((val) + 500) / 1000)
+#define TEMP8U_TO_REG(val)	((val) <= 0 ? 0 : \
+				 (val) >= 255000 ? 255 : \
+				 ((val) + 500) / 1000)
 #define TEMP11_FROM_REG(reg)	((reg) / 32 * 125)
 #define TEMP11_TO_REG(val)	((val) <= -128000 ? 0x8000 : \
 				 (val) >= 127875 ? 0x7FE0 : \
@@ -333,6 +336,31 @@ static ssize_t set_local_temp8(struct device *dev,
 	return count;
 }
 
+static ssize_t set_remote_temp8(struct device *dev,
+				struct device_attribute *devattr,
+				const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct lm63_data *data = i2c_get_clientdata(client);
+	long val;
+	int err;
+
+	err = kstrtol(buf, 10, &val);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+	if (data->remote_unsigned)
+		data->temp8[2] = TEMP8U_TO_REG(val - data->temp2_offset);
+	else
+		data->temp8[2] = TEMP8_TO_REG(val - data->temp2_offset);
+
+	i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT,
+				  data->temp8[2]);
+	mutex_unlock(&data->update_lock);
+	return count;
+}
+
 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
 			   char *buf)
 {
@@ -470,12 +498,8 @@ static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
 	set_temp11, 2);
 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
 	set_temp11, 3);
-/*
- * On LM63, temp2_crit can be set only once, which should be job
- * of the bootloader.
- */
 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
-	NULL, 2);
+	set_remote_temp8, 2);
 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
 	set_temp2_crit_hyst);
 
@@ -510,7 +534,30 @@ static struct attribute *lm63_attributes[] = {
 	NULL
 };
 
+/*
+ * On LM63, temp2_crit can be set only once, which should be job
+ * of the bootloader.
+ * On LM64, temp2_crit can always be set.
+ * On LM96163, temp2_crit can be set if bit 1 of the configuration
+ * register is true.
+ */
+static mode_t lm63_attribute_mode(struct kobject *kobj,
+				  struct attribute *attr, int index)
+{
+	struct device *dev = container_of(kobj, struct device, kobj);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct lm63_data *data = i2c_get_clientdata(client);
+
+	if (attr = &sensor_dev_attr_temp2_crit.dev_attr.attr
+	    && (data->kind = lm64 || (data->kind = lm96163
+				       && (data->config & 0x02))))
+		return attr->mode | S_IWUSR;
+
+	return attr->mode;
+}
+
 static const struct attribute_group lm63_group = {
+	.is_visible = lm63_attribute_mode,
 	.attrs = lm63_attributes,
 };
 
Signature · 7 lines
1.7.5.4


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

Re: [lm-sensors] [PATCH 4/5] hwmon: (lm63) Add support for writing

Hi Guenter,

Quoted 11 lines · Guenter Roeck

From: Guenter Roeck <[email protected]>

On LM64, the external critical temperature limit is always writable. On LM96163, it is writable if the chip is configured for it. Add conditional support for writing the register dependent on chip type and configuration.

---
 drivers/hwmon/lm63.c |   57 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 52 insertions(+), 5 deletions(-)

Generates one build-time warning here:

drivers/hwmon/lm63.c:560:2: warning: initialization from incompatible pointer type

This is:

559 static const struct attribute_group lm63_group = {
560 .is_visible = lm63_attribute_mode,
561 .attrs = lm63_attributes,
562 };

Obviously this is caused by this recent commit:
http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commit;hX7a1f1659e8b330b8738ef4901832a2b63f0bed

Review:

Quoted 33 lines
drivers/hwmon/lm63.c +17 -0
index 24a96f8..6370e28 100644
@@ -117,6 +117,9 @@ static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
 				 (val) >= 127000 ? 127 : \
 				 (val) < 0 ? ((val) - 500) / 1000 : \
 				 ((val) + 500) / 1000)
+#define TEMP8U_TO_REG(val)	((val) <= 0 ? 0 : \
+				 (val) >= 255000 ? 255 : \
+				 ((val) + 500) / 1000)
 #define TEMP11_FROM_REG(reg)	((reg) / 32 * 125)
 #define TEMP11_TO_REG(val)	((val) <= -128000 ? 0x8000 : \
 				 (val) >= 127875 ? 0x7FE0 : \
@@ -333,6 +336,31 @@ static ssize_t set_local_temp8(struct device *dev,
 	return count;
 }
 
+static ssize_t set_remote_temp8(struct device *dev,
+				struct device_attribute *devattr,
+				const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct lm63_data *data = i2c_get_clientdata(client);
+	long val;
+	int err;
+
+	err = kstrtol(buf, 10, &val);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);

drivers/hwmon/lm63.c static ssize_t set_local_temp8(struct device *dev, · line 362

Quoted 10 lines
drivers/hwmon/lm63.c +10 -0
+	if (data->remote_unsigned)
+		data->temp8[2] = TEMP8U_TO_REG(val - data->temp2_offset);
+	else
+		data->temp8[2] = TEMP8_TO_REG(val - data->temp2_offset);
+
+	i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT,
+				  data->temp8[2]);
+	mutex_unlock(&data->update_lock);
+	return count;
+}

The code is correct, but wouldn't it make sense to merge set_local_temp8() and set_remote_temp8() into a single function? There's a lot of common code between these two functions.

Quoted 29 lines
drivers/hwmon/lm63.c +9 -5
+
 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
 			   char *buf)
 {
@@ -470,12 +498,8 @@ static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
 	set_temp11, 2);
 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
 	set_temp11, 3);
-/*
- * On LM63, temp2_crit can be set only once, which should be job
- * of the bootloader.
- */
 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
-	NULL, 2);
+	set_remote_temp8, 2);
 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
 	set_temp2_crit_hyst);
 
@@ -510,7 +534,30 @@ static struct attribute *lm63_attributes[] = {
 	NULL
 };
 
+/*
+ * On LM63, temp2_crit can be set only once, which should be job
+ * of the bootloader.
+ * On LM64, temp2_crit can always be set.
+ * On LM96163, temp2_crit can be set if bit 1 of the configuration
+ * register is true.
+ */

drivers/hwmon/lm63.c static struct attribute *lm63_attributes[] = { · line 553

Quoted 10 lines
drivers/hwmon/lm63.c +10 -0
+static mode_t lm63_attribute_mode(struct kobject *kobj,
+				  struct attribute *attr, int index)
+{
+	struct device *dev = container_of(kobj, struct device, kobj);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct lm63_data *data = i2c_get_clientdata(client);
+
+	if (attr = &sensor_dev_attr_temp2_crit.dev_attr.attr
+	    && (data->kind = lm64 || (data->kind = lm96163
+				       && (data->config & 0x02))))

I think this would be more readable as:

	    && (data->kind = lm64 ||
		(data->kind = lm96163 && (data->config & 0x02))))

drivers/hwmon/lm63.c static struct attribute *lm63_attributes[] = { · line 554

Quoted 1 line
drivers/hwmon/lm63.c +1 -0
+		return attr->mode | S_IWUSR;

FWIW I think you can save a few binary bytes by using instead:

		attr->mode |= S_IWUSR;

Thanks to the single exit point.

Quoted 9 lines
drivers/hwmon/lm63.c +5 -0
+
+	return attr->mode;
+}
+
 static const struct attribute_group lm63_group = {
+	.is_visible = lm63_attribute_mode,
 	.attrs = lm63_attributes,
 };
 
Signature · 6 lines
Jean Delvare

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

Re: [lm-sensors] [PATCH 4/5] hwmon: (lm63) Add support for writing

In reply to Guenter Roeck
Quoted 21 lines · Jean Delvare

Hi Guenter,

Quoted 11 lines · Guenter Roeck

From: Guenter Roeck <[email protected]>

On LM64, the external critical temperature limit is always writable. On LM96163, it is writable if the chip is configured for it. Add conditional support for writing the register dependent on chip type and configuration.

---
 drivers/hwmon/lm63.c |   57 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 52 insertions(+), 5 deletions(-)

Generates one build-time warning here:

drivers/hwmon/lm63.c:560:2: warning: initialization from incompatible pointer type

This is:

Quoted 8 lines

559 static const struct attribute_group lm63_group = {
560 .is_visible = lm63_attribute_mode,
561 .attrs = lm63_attributes,
562 };

Obviously this is caused by this recent commit:
http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commit;hX7a1f1659e8b330b8738ef4901832a2b63f0bed

I'll fix and resubmit.

Quoted 41 lines

Review:

Quoted 39 lines
drivers/hwmon/lm63.c +23 -0
index 24a96f8..6370e28 100644
@@ -117,6 +117,9 @@ static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
 				 (val) >= 127000 ? 127 : \
 				 (val) < 0 ? ((val) - 500) / 1000 : \
 				 ((val) + 500) / 1000)
+#define TEMP8U_TO_REG(val)	((val) <= 0 ? 0 : \
+				 (val) >= 255000 ? 255 : \
+				 ((val) + 500) / 1000)
 #define TEMP11_FROM_REG(reg)	((reg) / 32 * 125)
 #define TEMP11_TO_REG(val)	((val) <= -128000 ? 0x8000 : \
 				 (val) >= 127875 ? 0x7FE0 : \
@@ -333,6 +336,31 @@ static ssize_t set_local_temp8(struct device *dev,
 	return count;
 }
 
+static ssize_t set_remote_temp8(struct device *dev,
+				struct device_attribute *devattr,
+				const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct lm63_data *data = i2c_get_clientdata(client);
+	long val;
+	int err;
+
+	err = kstrtol(buf, 10, &val);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+	if (data->remote_unsigned)
+		data->temp8[2] = TEMP8U_TO_REG(val - data->temp2_offset);
+	else
+		data->temp8[2] = TEMP8_TO_REG(val - data->temp2_offset);
+
+	i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT,
Quoted 9 lines

drivers/hwmon/lm63.c static ssize_t set_local_temp8(struct device *dev, · line 362

Quoted 4 lines
drivers/hwmon/lm63.c +4 -0
+				  data->temp8[2]);
+	mutex_unlock(&data->update_lock);
+	return count;
+}

The code is correct, but wouldn't it make sense to merge set_local_temp8() and set_remote_temp8() into a single function? There's a lot of common code between these two functions.

I'll give it a shot.

Quoted 42 lines
Quoted 29 lines
drivers/hwmon/lm63.c +9 -5
+
 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
 			   char *buf)
 {
@@ -470,12 +498,8 @@ static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
 	set_temp11, 2);
 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
 	set_temp11, 3);
-/*
- * On LM63, temp2_crit can be set only once, which should be job
- * of the bootloader.
- */
 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
-	NULL, 2);
+	set_remote_temp8, 2);
 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
 	set_temp2_crit_hyst);
 
@@ -510,7 +534,30 @@ static struct attribute *lm63_attributes[] = {
 	NULL
 };
 
+/*
+ * On LM63, temp2_crit can be set only once, which should be job
+ * of the bootloader.
+ * On LM64, temp2_crit can always be set.
+ * On LM96163, temp2_crit can be set if bit 1 of the configuration
+ * register is true.
+ */

drivers/hwmon/lm63.c static struct attribute *lm63_attributes[] = { · line 553

Quoted 10 lines
drivers/hwmon/lm63.c +10 -0
+static mode_t lm63_attribute_mode(struct kobject *kobj,
+				  struct attribute *attr, int index)
+{
+	struct device *dev = container_of(kobj, struct device, kobj);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct lm63_data *data = i2c_get_clientdata(client);
+
+	if (attr = &sensor_dev_attr_temp2_crit.dev_attr.attr
+	    && (data->kind = lm64 || (data->kind = lm96163
+				       && (data->config & 0x02))))

I think this would be more readable as:

Quoted 4 lines
	    && (data->kind = lm64 ||
		(data->kind = lm96163 && (data->config & 0x02))))

Ok.

Quoted 8 lines

drivers/hwmon/lm63.c static struct attribute *lm63_attributes[] = { · line 554

Quoted 1 line
drivers/hwmon/lm63.c +1 -0
+		return attr->mode | S_IWUSR;

FWIW I think you can save a few binary bytes by using instead:

		attr->mode |= S_IWUSR;

Thanks to the single exit point.

Unless I am missing something, that would set S_IWUSR for all instances of the driver. Not sure if that is a good idea. I can use a local variable for mode, though.

Guenter

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

#3 lore raw

Re: [lm-sensors] [PATCH 4/5] hwmon: (lm63) Add support for writing

In reply to Guenter Roeck
Quoted 36 lines · Guenter Roeck
Quoted 25 lines · Jean Delvare
Quoted 11 lines · Guenter Roeck
drivers/hwmon/lm63.c +7 -0
@@ -510,7 +534,30 @@ static struct attribute *lm63_attributes[] = {
 	NULL
 };
 
+/*
+ * On LM63, temp2_crit can be set only once, which should be job
+ * of the bootloader.
+ * On LM64, temp2_crit can always be set.
+ * On LM96163, temp2_crit can be set if bit 1 of the configuration
+ * register is true.
+ */

drivers/hwmon/lm63.c static struct attribute *lm63_attributes[] = { · line 553

Quoted 10 lines
drivers/hwmon/lm63.c +10 -0
+static mode_t lm63_attribute_mode(struct kobject *kobj,
+				  struct attribute *attr, int index)
+{
+	struct device *dev = container_of(kobj, struct device, kobj);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct lm63_data *data = i2c_get_clientdata(client);
+
+	if (attr = &sensor_dev_attr_temp2_crit.dev_attr.attr
+	    && (data->kind = lm64 || (data->kind = lm96163
+				       && (data->config & 0x02))))

I think this would be more readable as:

Quoted 4 lines
	    && (data->kind = lm64 ||
		(data->kind = lm96163 && (data->config & 0x02))))

Ok.

Quoted 4 lines

drivers/hwmon/lm63.c static struct attribute *lm63_attributes[] = { · line 554

Quoted 1 line
drivers/hwmon/lm63.c +1 -0
+		return attr->mode | S_IWUSR;

FWIW I think you can save a few binary bytes by using instead:

Quoted 7 lines
Quoted 4 lines
		attr->mode |= S_IWUSR;

Thanks to the single exit point.

Unless I am missing something, that would set S_IWUSR for all instances of the driver. Not sure if that is a good idea. I can use a local variable for mode, though.

Oops, you're totally right, my bad. Just ignore my comment on this.

Signature · 6 lines
Jean Delvare

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