Skip to contentSkip to conversation

hwmon: iio: Add labels

PATCH Superseded Sean Anderson 3 participants 8 messages Jun 20, 2024 – Jun 23, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hwmon: iio: Add labels

Add support for using IIO channel labels for HWMON labels.

Sean Anderson (2):
iio: Add iio_read_channel_label to inkern API
hwmon: iio: Add labels from IIO channels

 drivers/hwmon/iio_hwmon.c       | 33 ++++++++++++++++++++++++++++++---
 drivers/iio/iio_core.h          |  4 ++++
 drivers/iio/industrialio-core.c | 23 ++++++++++++++---------
 drivers/iio/inkern.c            |  6 ++++++
 include/linux/iio/consumer.h    | 10 ++++++++++
 5 files changed, 64 insertions(+), 12 deletions(-)
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#1 lore raw

PATCH 1/2 iio: Add iio_read_channel_label to inkern API

It can be convenient for other in-kernel drivers to reuse IIO channel labels. Export the iio_read_channel_label function to allow this. The signature is different depending on where we are calling it from, so the meat is moved to do_iio_read_channel_label.

---

 drivers/iio/iio_core.h          |  4 ++++
 drivers/iio/industrialio-core.c | 23 ++++++++++++++---------
 drivers/iio/inkern.c            |  6 ++++++
 include/linux/iio/consumer.h    | 10 ++++++++++
 4 files changed, 34 insertions(+), 9 deletions(-)
drivers/iio/iio_core.h +4 -0
index 1a38b1915e7a..b7d5f4f0fada 100644
@@ -34,6 +34,10 @@ void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
 				       struct iio_ioctl_handler *h);
 void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h);
 
+ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *c,
+				  char *buf);
+
 int __iio_add_chan_devattr(const char *postfix,
 			   struct iio_chan_spec const *chan,
 			   ssize_t (*func)(struct device *dev,
drivers/iio/industrialio-core.c +14 -9
index 2f185b386949..0f6cda7ffe45 100644
@@ -727,22 +727,27 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
 }
 EXPORT_SYMBOL_GPL(iio_format_value);
 
-static ssize_t iio_read_channel_label(struct device *dev,
-				      struct device_attribute *attr,
-				      char *buf)
+ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *c,
+				  char *buf)
 {
-	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-
 	if (indio_dev->info->read_label)
-		return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
+		return indio_dev->info->read_label(indio_dev, c, buf);
 
-	if (this_attr->c->extend_name)
-		return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
+	if (c->extend_name)
+		return sysfs_emit(buf, "%s\n", c->extend_name);
 
 	return -EINVAL;
 }
 
+static ssize_t iio_read_channel_label(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	return do_iio_read_channel_label(dev_to_iio_dev(dev),
+					 to_iio_dev_attr(attr)->c, buf);
+}
+
 static ssize_t iio_read_channel_info(struct device *dev,
 				     struct device_attribute *attr,
 				     char *buf)
drivers/iio/inkern.c +6 -0
index 39cf26d69d17..9f484c94bc6e 100644
@@ -1010,3 +1010,9 @@ ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
 			       chan->channel, buf, len);
 }
 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
+
+ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf)
+{
+	return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf);
+}
+EXPORT_SYMBOL_GPL(iio_read_channel_label);
include/linux/iio/consumer.h +10 -0
index e9910b41d48e..333d1d8ccb37 100644
@@ -441,4 +441,14 @@ ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
 				   const char *buf, size_t len);
 
+/**
+ * iio_read_channel_label() - read label for a given channel
+ * @chan:		The channel being queried.
+ * @buf:		Where to store the attribute value. Assumed to hold
+ *			at least PAGE_SIZE bytes.
+ *
+ * Returns the number of bytes written to buf, or an error code.
+ */
+ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf);
+
 #endif
Signature · 1 line
2.35.1.1320.gc452695387.dirty
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] iio: Add iio_read_channel_label to inkern API

Quoted 6 lines · On Thu, 20 Jun 2024 17:13:08 -0400 Sean Anderson <[email protected]>…

It can be convenient for other in-kernel drivers to reuse IIO channel labels. Export the iio_read_channel_label function to allow this. The signature is different depending on where we are calling it from, so the meat is moved to do_iio_read_channel_label.

Seems like a useful addition to me. I'm not 100% what path this will take so if it goes via HWMON.

It may be messy and need an immutable branch however. I haven't checked for churn in this code yet.

Quoted 97 lines

---

 drivers/iio/iio_core.h          |  4 ++++
 drivers/iio/industrialio-core.c | 23 ++++++++++++++---------
 drivers/iio/inkern.c            |  6 ++++++
 include/linux/iio/consumer.h    | 10 ++++++++++
 4 files changed, 34 insertions(+), 9 deletions(-)
drivers/iio/iio_core.h +4 -0
index 1a38b1915e7a..b7d5f4f0fada 100644
@@ -34,6 +34,10 @@ void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
 				       struct iio_ioctl_handler *h);
 void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h);
 
+ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *c,
+				  char *buf);
+
 int __iio_add_chan_devattr(const char *postfix,
 			   struct iio_chan_spec const *chan,
 			   ssize_t (*func)(struct device *dev,
drivers/iio/industrialio-core.c +14 -9
index 2f185b386949..0f6cda7ffe45 100644
@@ -727,22 +727,27 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
 }
 EXPORT_SYMBOL_GPL(iio_format_value);
 
-static ssize_t iio_read_channel_label(struct device *dev,
-				      struct device_attribute *attr,
-				      char *buf)
+ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *c,
+				  char *buf)
 {
-	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-
 	if (indio_dev->info->read_label)
-		return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
+		return indio_dev->info->read_label(indio_dev, c, buf);
 
-	if (this_attr->c->extend_name)
-		return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
+	if (c->extend_name)
+		return sysfs_emit(buf, "%s\n", c->extend_name);
 
 	return -EINVAL;
 }
 
+static ssize_t iio_read_channel_label(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	return do_iio_read_channel_label(dev_to_iio_dev(dev),
+					 to_iio_dev_attr(attr)->c, buf);
+}
+
 static ssize_t iio_read_channel_info(struct device *dev,
 				     struct device_attribute *attr,
 				     char *buf)
drivers/iio/inkern.c +6 -0
index 39cf26d69d17..9f484c94bc6e 100644
@@ -1010,3 +1010,9 @@ ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
 			       chan->channel, buf, len);
 }
 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
+
+ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf)
+{
+	return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf);
+}
+EXPORT_SYMBOL_GPL(iio_read_channel_label);
include/linux/iio/consumer.h +10 -0
index e9910b41d48e..333d1d8ccb37 100644
@@ -441,4 +441,14 @@ ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
 				   const char *buf, size_t len);
 
+/**
+ * iio_read_channel_label() - read label for a given channel
+ * @chan:		The channel being queried.
+ * @buf:		Where to store the attribute value. Assumed to hold
+ *			at least PAGE_SIZE bytes.
+ *
+ * Returns the number of bytes written to buf, or an error code.
+ */
+ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf);
+
 #endif
#3 lore raw

PATCH 2/2 hwmon: iio: Add labels from IIO channels

In reply to Sean Anderson

Add labels from IIO channels to our channels. This allows userspace to display more meaningful names instead of "in0" or "temp5".

---

 drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
drivers/hwmon/iio_hwmon.c +30 -3
index 4c8a80847891..588b64c18e63 100644
@@ -33,6 +33,17 @@ struct iio_hwmon_state {
 	struct attribute **attrs;
 };
 
+static ssize_t iio_hwmon_read_label(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct iio_hwmon_state *state = dev_get_drvdata(dev);
+	struct iio_channel *chan = &state->channels[sattr->index];
+
+	return iio_read_channel_label(chan, buf);
+}
+
 /*
  * Assumes that IIO and hwmon operate in the same base units.
  * This is supposed to be true, but needs verification for
@@ -90,12 +101,12 @@ static int iio_hwmon_probe(struct platform_device *pdev)
 
 	st->channels = channels;
 
-	/* count how many attributes we have */
+	/* count how many channels we have */
 	while (st->channels[st->num_channels].indio_dev)
 		st->num_channels++;
 
 	st->attrs = devm_kcalloc(dev,
-				 st->num_channels + 1, sizeof(*st->attrs),
+				 2 * st->num_channels + 1, sizeof(*st->attrs),
 				 GFP_KERNEL);
 	if (st->attrs == NULL)
 		return -ENOMEM;
@@ -147,7 +158,23 @@ static int iio_hwmon_probe(struct platform_device *pdev)
 		a->dev_attr.show = iio_hwmon_read_val;
 		a->dev_attr.attr.mode = 0444;
 		a->index = i;
-		st->attrs[i] = &a->dev_attr.attr;
+		st->attrs[2 * i] = &a->dev_attr.attr;
+
+		a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
+		if (a == NULL)
+			return -ENOMEM;
+
+		sysfs_attr_init(&a->dev_attr.attr);
+		a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
+						       "%s%d_label",
+						       prefix, n);
+		if (!a->dev_attr.attr.name)
+			return -ENOMEM;
+
+		a->dev_attr.show = iio_hwmon_read_label;
+		a->dev_attr.attr.mode = 0444;
+		a->index = i;
+		st->attrs[2 * i + 1] = &a->dev_attr.attr;
 	}
 
 	st->attr_group.attrs = st->attrs;
Signature · 1 line
2.35.1.1320.gc452695387.dirty
maintainer
#4 lore raw

Re: [PATCH 2/2] hwmon: iio: Add labels from IIO channels

Quoted 16 lines · Sean Anderson

Add labels from IIO channels to our channels. This allows userspace to display more meaningful names instead of "in0" or "temp5".

---

  drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
  1 file changed, 30 insertions(+), 3 deletions(-)
drivers/hwmon/iio_hwmon.c +0 -0
index 4c8a80847891..588b64c18e63 100644
@@ -33,6 +33,17 @@ struct iio_hwmon_state {
  	struct attribute **attrs;
  };

drivers/hwmon/iio_hwmon.c struct iio_hwmon_state { · line 44

Quoted 10 lines
drivers/hwmon/iio_hwmon.c +9 -0
 
+static ssize_t iio_hwmon_read_label(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct iio_hwmon_state *state = dev_get_drvdata(dev);
+	struct iio_channel *chan = &state->channels[sattr->index];
+
+	return iio_read_channel_label(chan, buf);

This can return -EINVAL if there is no label. Since the label attribute is created unconditionally, every affected system would end up with lots of error messages when running the "sensors" command. This is not acceptable.

Guenter

Quoted 45 lines
drivers/hwmon/iio_hwmon.c +21 -3
+}
+
  /*
   * Assumes that IIO and hwmon operate in the same base units.
   * This is supposed to be true, but needs verification for
@@ -90,12 +101,12 @@ static int iio_hwmon_probe(struct platform_device *pdev)
 
  	st->channels = channels;
 
-	/* count how many attributes we have */
+	/* count how many channels we have */
  	while (st->channels[st->num_channels].indio_dev)
  		st->num_channels++;
 
  	st->attrs = devm_kcalloc(dev,
-				 st->num_channels + 1, sizeof(*st->attrs),
+				 2 * st->num_channels + 1, sizeof(*st->attrs),
  				 GFP_KERNEL);
  	if (st->attrs == NULL)
  		return -ENOMEM;
@@ -147,7 +158,23 @@ static int iio_hwmon_probe(struct platform_device *pdev)
  		a->dev_attr.show = iio_hwmon_read_val;
  		a->dev_attr.attr.mode = 0444;
  		a->index = i;
-		st->attrs[i] = &a->dev_attr.attr;
+		st->attrs[2 * i] = &a->dev_attr.attr;
+
+		a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
+		if (a == NULL)
+			return -ENOMEM;
+
+		sysfs_attr_init(&a->dev_attr.attr);
+		a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
+						       "%s%d_label",
+						       prefix, n);
+		if (!a->dev_attr.attr.name)
+			return -ENOMEM;
+
+		a->dev_attr.show = iio_hwmon_read_label;
+		a->dev_attr.attr.mode = 0444;
+		a->index = i;
+		st->attrs[2 * i + 1] = &a->dev_attr.attr;
  	}
 
  	st->attr_group.attrs = st->attrs;
#5 lore raw

Re: [PATCH 2/2] hwmon: iio: Add labels from IIO channels

Quoted 23 lines · Guenter Roeck
Quoted 22 lines · Sean Anderson

Add labels from IIO channels to our channels. This allows userspace to display more meaningful names instead of "in0" or "temp5".

---

  drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---

  1 file changed, 30 insertions(+), 3 deletions(-)
drivers/hwmon/iio_hwmon.c +5 -0
index 4c8a80847891..588b64c18e63 100644
@@ -33,6 +33,17 @@ struct iio_hwmon_state {
       struct attribute **attrs;
   };
   +static ssize_t iio_hwmon_read_label(struct device *dev,
+                  struct device_attribute *attr,
+                  char *buf)
+{
+    struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+    struct iio_hwmon_state *state = dev_get_drvdata(dev);
Quoted 8 lines

drivers/hwmon/iio_hwmon.c struct iio_hwmon_state { · line 44

Quoted 3 lines
drivers/hwmon/iio_hwmon.c +3 -0
+    struct iio_channel *chan = &state->channels[sattr->index];
+
+    return iio_read_channel_label(chan, buf);

This can return -EINVAL if there is no label. Since the label attribute is created unconditionally, every affected system would end up with lots of error messages when running the "sensors" command. This is not acceptable.

The sensors command gracefully handles this. There are no errors, and the label is unused.

--Sean

#6 lore raw

Re: [PATCH 2/2] hwmon: iio: Add labels from IIO channels

Quoted 33 lines · Sean Anderson
Quoted 23 lines · Guenter Roeck
Quoted 22 lines · Sean Anderson

Add labels from IIO channels to our channels. This allows userspace to display more meaningful names instead of "in0" or "temp5".

---

  drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
  1 file changed, 30 insertions(+), 3 deletions(-)
drivers/hwmon/iio_hwmon.c +5 -0
index 4c8a80847891..588b64c18e63 100644
@@ -33,6 +33,17 @@ struct iio_hwmon_state {
      struct attribute **attrs;
  };
  +static ssize_t iio_hwmon_read_label(struct device *dev,
+                  struct device_attribute *attr,
+                  char *buf)
+{
+    struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+    struct iio_hwmon_state *state = dev_get_drvdata(dev);
Quoted 8 lines

drivers/hwmon/iio_hwmon.c struct iio_hwmon_state { · line 44

Quoted 3 lines
drivers/hwmon/iio_hwmon.c +3 -0
+    struct iio_channel *chan = &state->channels[sattr->index];
+
+    return iio_read_channel_label(chan, buf);

This can return -EINVAL if there is no label. Since the label attribute is created unconditionally, every affected system would end up with lots of error messages when running the "sensors" command. This is not acceptable.

Quoted 1 line

The sensors command gracefully handles this. There are no errors, and the label is unused.

For example, without IIO labels I get:

$ sensors hwmon_ams_ps-isa-0000
hwmon_ams_ps-isa-0000
Adapter: ISA adapter
in1:         825.00 mV 
in2:         826.00 mV 
in3:           1.81 V  
in4:           1.18 V  
in5:           1.80 V  
in6:           1.80 V  
in7:           3.27 V  
in8:           1.81 V  
in9:         825.00 mV 
in10:          1.81 V  
in11:          1.80 V  
temp1:        +79.8 C  
temp2:        +80.9 C  

and with labels I get

$ sensors hwmon_ams_ps-isa-0000
hwmon_ams_ps-isa-0000
Adapter: ISA adapter
VCC_PSINTLP: 824.00 mV 
VCC_PSINTFP: 822.00 mV 
VCC_PSAUX:     1.81 V  
VCC_PSDDR:     1.18 V  
VCC_PSIO3:     1.80 V  
VCC_PSIO0:     1.80 V  
VCC_PSIO1:     3.27 V  
VCC_PSIO2:     1.80 V  
PS_MGTRAVCC: 822.00 mV 
PS_MGTRAVTT:   1.81 V  
VCC_PSADC:     1.80 V  
Temp_LPD:     +79.5 C  
Temp_FPD:     +80.2 C  

I also was concerned about the same thing, but lm-sensors handles things gracefully, allowing us to simplify the implementation.

--Sean

maintainer
#7 lore raw

Re: [PATCH 2/2] hwmon: iio: Add labels from IIO channels

Quoted 57 lines · Sean Anderson
Quoted 33 lines · Sean Anderson
Quoted 23 lines · Guenter Roeck
Quoted 22 lines · Sean Anderson

Add labels from IIO channels to our channels. This allows userspace to display more meaningful names instead of "in0" or "temp5".

---

   drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
   1 file changed, 30 insertions(+), 3 deletions(-)
drivers/hwmon/iio_hwmon.c +5 -0
index 4c8a80847891..588b64c18e63 100644
@@ -33,6 +33,17 @@ struct iio_hwmon_state {
       struct attribute **attrs;
   };
   +static ssize_t iio_hwmon_read_label(struct device *dev,
+                  struct device_attribute *attr,
+                  char *buf)
+{
+    struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+    struct iio_hwmon_state *state = dev_get_drvdata(dev);
Quoted 8 lines

drivers/hwmon/iio_hwmon.c struct iio_hwmon_state { · line 44

Quoted 3 lines
drivers/hwmon/iio_hwmon.c +3 -0
+    struct iio_channel *chan = &state->channels[sattr->index];
+
+    return iio_read_channel_label(chan, buf);

This can return -EINVAL if there is no label. Since the label attribute is created unconditionally, every affected system would end up with lots of error messages when running the "sensors" command. This is not acceptable.

Quoted 1 line

The sensors command gracefully handles this. There are no errors, and the label is unused.

For example, without IIO labels I get:

$ sensors hwmon_ams_ps-isa-0000
hwmon_ams_ps-isa-0000
Adapter: ISA adapter
in1:         825.00 mV
in2:         826.00 mV
in3:           1.81 V
in4:           1.18 V
in5:           1.80 V
in6:           1.80 V
in7:           3.27 V
in8:           1.81 V
in9:         825.00 mV
in10:          1.81 V
in11:          1.80 V
temp1:        +79.8 C
temp2:        +80.9 C

and with labels I get

Quoted 17 lines
$ sensors hwmon_ams_ps-isa-0000
hwmon_ams_ps-isa-0000
Adapter: ISA adapter
VCC_PSINTLP: 824.00 mV
VCC_PSINTFP: 822.00 mV
VCC_PSAUX:     1.81 V
VCC_PSDDR:     1.18 V
VCC_PSIO3:     1.80 V
VCC_PSIO0:     1.80 V
VCC_PSIO1:     3.27 V
VCC_PSIO2:     1.80 V
PS_MGTRAVCC: 822.00 mV
PS_MGTRAVTT:   1.81 V
VCC_PSADC:     1.80 V
Temp_LPD:     +79.5 C
Temp_FPD:     +80.2 C

You are correct, the sensors code ignores access errors on label attributes. Still, I am not even going there. If there is no label, don't create the attribute, period. The ABI says

Should only be created if the driver has hints about what this voltage channel is being used for, and user-space doesn't. In all other cases, the label is provided by user-space.

It should be obvious that this doesn't mean that you can create random attributes and then just return an error if it isn't actually supported.

Guenter