Skip to contentSkip to conversation

staging: iio: adc: ad7816: code cleanups and modernization

PATCH In review Md. Mahmudul Hasan Mabud 3 participants 10 messages Mar 31 – Apr 12

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v4 cover staging: iio: adc: ad7816: code cleanups and modernization

This series addresses feedback from Andy Shevchenko to modernize and clean up the ad7816 driver.

Changes in v4:
- Added a blank line after the AD7816_FULL and AD7816_PD definitions to improve code readability, as requested by Andy Shevchenko.

Changes in v3:
- Added missing Signed-off-by tags and detailed patch descriptions to all commits as requested by the automated patch-bot.
- No functional changes since v2.

Changes in v2:
- Renamed sysfs callback functions (e.g., ad7816_show_mode to mode_show) to satisfy the requirements of modern IIO attribute macros.
- Fixed usage of sysfs_match_string() by removing the unnecessary ARRAY_SIZE parameter.
- Fixed vertical alignment of function parameters to match open parenthesis.
- Replaced sprintf() with sysfs_emit() across all show attributes.

Md. Mahmudul Hasan Mabud (4):
staging: iio: adc: ad7816: redefine mode constants to start from 0
staging: iio: adc: ad7816: use sysfs_match_string() for mode selection
staging: iio: adc: ad7816: replace sprintf() with sysfs_emit()
staging: iio: adc: ad7816: use modern IIO attribute macros

 drivers/staging/iio/adc/ad7816.c | 112 +++++++++++++++----------------
 1 file changed, 54 insertions(+), 58 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/4 staging: iio: adc: ad7816: redefine mode constants to start from 0

Redefine AD7816_FULL and AD7816_PD constants to start from 0 instead of 1. This change is necessary to use these constants as direct indices for the mode string array and to align with standard indexing in modern sysfs helpers.

---
 drivers/staging/iio/adc/ad7816.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/iio/adc/ad7816.c +3 -2
index 172acf135..9a2f60b97 100644
@@ -22,8 +22,9 @@
 /*
  * AD7816 config masks
  */
-#define AD7816_FULL			0x1
-#define AD7816_PD			0x2
+#define AD7816_FULL			0
+#define AD7816_PD			1
+
 #define AD7816_CS_MASK			0x7
 #define AD7816_CS_MAX			0x4
 
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v4 1/4] staging: iio: adc: ad7816: redefine mode constants to start from 0

Quoted 6 lines · On Tue, 31 Mar 2026 13:24:50 +0600 "Md. Mahmudul Hasan Mabud"…

Redefine AD7816_FULL and AD7816_PD constants to start from 0 instead of 1. This change is necessary to use these constants as direct indices for the mode string array and to align with standard indexing in modern sysfs helpers.

Hi.

This shows one of the perhaps less well documented bits of kernel process. Hold off before sending a new version for a while! Dan commented on this in v3 but after you'd already sent v4.

General rule is wait at least a week between versions unless it's very minor stuff after the patch set is very stable or you get an explicit request to repost sooner (I'll do that occasionally at the end of a kernel cycle).

Anyhow, see discussion on v3.

Jonathan

Quoted 20 lines
---
 drivers/staging/iio/adc/ad7816.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/iio/adc/ad7816.c +3 -2
index 172acf135..9a2f60b97 100644
@@ -22,8 +22,9 @@
 /*
  * AD7816 config masks
  */
-#define AD7816_FULL			0x1
-#define AD7816_PD			0x2
+#define AD7816_FULL			0
+#define AD7816_PD			1
+
 #define AD7816_CS_MASK			0x7
 #define AD7816_CS_MAX			0x4
 
maintainer
#3 lore raw

Re: [PATCH v4 1/4] staging: iio: adc: ad7816: redefine mode constants to start from 0

In reply to Md. Mahmudul Hasan Mabud
Quoted 15 lines · On Tue, 31 Mar 2026 13:24:50 +0600 "Md. Mahmudul Hasan Mabud"…

Redefine AD7816_FULL and AD7816_PD constants to start from 0 instead of 1. This change is necessary to use these constants as direct indices for the mode string array and to align with standard indexing in modern sysfs helpers.

---
 drivers/staging/iio/adc/ad7816.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/iio/adc/ad7816.c +0 -0
index 172acf135..9a2f60b97 100644

drivers/staging/iio/adc/ad7816.c line 23

Quoted 2 lines
drivers/staging/iio/adc/ad7816.c +0 -0
@@ -22,8 +22,9 @@
 /*
  * AD7816 config masks

Beyond the discussion on v3, in what way are these now config masks?

Quoted 9 lines
drivers/staging/iio/adc/ad7816.c +3 -2
  */
-#define AD7816_FULL			0x1
-#define AD7816_PD			0x2
+#define AD7816_FULL			0
+#define AD7816_PD			1
+
 #define AD7816_CS_MASK			0x7
 #define AD7816_CS_MAX			0x4
 
#4 lore raw

PATCH v4 2/4 staging: iio: adc: ad7816: use sysfs_match_string() for mode selection

In reply to Md. Mahmudul Hasan Mabud

Replace manual strcmp() logic with sysfs_match_string() to simplify the mode selection process. This reduces boilerplate code and leverages standard kernel helper functions for string matching.

---
 drivers/staging/iio/adc/ad7816.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/staging/iio/adc/ad7816.c +12 -7
index 9a2f60b97..253ef2262 100644
@@ -59,6 +59,11 @@ enum ad7816_type {
 	ID_AD7818,
 };
 
+static const char * const ad7816_modes[] = {
+	[AD7816_FULL] = "full",
+	[AD7816_PD] = "power-save",
+};
+
 /*
  * ad7816 data access by SPI
  */
@@ -136,14 +141,14 @@ static ssize_t ad7816_store_mode(struct device *dev,
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
+	int ret;
 
-	if (strcmp(buf, "full") == 0) {
-		gpiod_set_value(chip->rdwr_pin, 1);
-		chip->mode = AD7816_FULL;
-	} else {
-		gpiod_set_value(chip->rdwr_pin, 0);
-		chip->mode = AD7816_PD;
-	}
+	ret = sysfs_match_string(ad7816_modes, buf);
+	if (ret < 0)
+		return ret;
+
+	chip->mode = ret;
+	gpiod_set_value(chip->rdwr_pin, ret == AD7816_FULL);
 
 	return len;
 }
Signature · 1 line
2.34.1
#5 lore raw

PATCH v4 3/4 staging: iio: adc: ad7816: replace sprintf() with sysfs_emit()

In reply to Md. Mahmudul Hasan Mabud

Replace sprintf() with sysfs_emit() in all show attributes. sysfs_emit() is the preferred way to return values in sysfs callbacks as it is safer and handles page boundary checks automatically.

---
 drivers/staging/iio/adc/ad7816.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/staging/iio/adc/ad7816.c +7 -9
index 253ef2262..790b04544 100644
@@ -129,9 +129,7 @@ static ssize_t ad7816_show_mode(struct device *dev,
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
 
-	if (chip->mode)
-		return sprintf(buf, "power-save\n");
-	return sprintf(buf, "full\n");
+	return sysfs_emit(buf, "%s\n", ad7816_modes[chip->mode]);
 }
 
 static ssize_t ad7816_store_mode(struct device *dev,
@@ -162,7 +160,7 @@ static ssize_t ad7816_show_available_modes(struct device *dev,
 					   struct device_attribute *attr,
 					   char *buf)
 {
-	return sprintf(buf, "full\npower-save\n");
+	return sysfs_emit(buf, "full\npower-save\n");
 }
 
 static IIO_DEVICE_ATTR(available_modes, 0444, ad7816_show_available_modes,
@@ -175,7 +173,7 @@ static ssize_t ad7816_show_channel(struct device *dev,
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
 
-	return sprintf(buf, "%d\n", chip->channel_id);
+	return sysfs_emit(buf, "%d\n", chip->channel_id);
 }
 
 static ssize_t ad7816_store_channel(struct device *dev,
@@ -237,9 +235,9 @@ static ssize_t ad7816_show_value(struct device *dev,
 		data &= AD7816_TEMP_FLOAT_MASK;
 		if (value < 0)
 			data = BIT(AD7816_TEMP_FLOAT_OFFSET) - data;
-		return sprintf(buf, "%d.%.2d\n", value, data * 25);
+		return sysfs_emit(buf, "%d.%.2d\n", value, data * 25);
 	}
-	return sprintf(buf, "%u\n", data);
+	return sysfs_emit(buf, "%u\n", data);
 }
 
 static IIO_DEVICE_ATTR(value, 0444, ad7816_show_value, NULL, 0);
@@ -287,9 +285,9 @@ static ssize_t ad7816_show_oti(struct device *dev,
 		value = AD7816_BOUND_VALUE_MIN +
 			(chip->oti_data[chip->channel_id] -
 			AD7816_BOUND_VALUE_BASE);
-		return sprintf(buf, "%d\n", value);
+		return sysfs_emit(buf, "%d\n", value);
 	}
-	return sprintf(buf, "%u\n", chip->oti_data[chip->channel_id]);
+	return sysfs_emit(buf, "%u\n", chip->oti_data[chip->channel_id]);
 }
 
 static inline ssize_t ad7816_set_oti(struct device *dev,
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v4 3/4] staging: iio: adc: ad7816: replace sprintf() with sysfs_emit()

Quoted 4 lines · Md. Mahmudul Hasan Mabud

Replace sprintf() with sysfs_emit() in all show attributes. sysfs_emit() is the preferred way to return values in sysfs callbacks as it is safer and handles page boundary checks automatically.

...

drivers/staging/iio/adc/ad7816.c static ssize_t ad7816_show_available_modes(struct device *dev, · line 163

Quoted 6 lines

static ssize_t ad7816_show_available_modes(struct device *dev,

drivers/staging/iio/adc/ad7816.c +1 -1
 					   struct device_attribute *attr,
 					   char *buf)
 {
-	return sprintf(buf, "full\npower-save\n");
+	return sysfs_emit(buf, "full\npower-save\n");

Missed a comment. This needs to be converted to a for-loop with use of sysfs_emit_at().

Quoted 1 line

}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

PATCH v4 4/4 staging: iio: adc: ad7816: use modern IIO attribute macros

In reply to Md. Mahmudul Hasan Mabud

Modernize the driver by using IIO_DEVICE_ATTR_RW and IIO_DEVICE_ATTR_RO macros. This involves renaming the callback functions to standard <name>_show and <name>_store formats, making the code cleaner and more maintainable.

---
 drivers/staging/iio/adc/ad7816.c | 72 ++++++++++++++------------------
 1 file changed, 32 insertions(+), 40 deletions(-)
drivers/staging/iio/adc/ad7816.c +32 -40
index 790b04544..c83f7f70d 100644
@@ -122,9 +122,9 @@ static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data)
 	return ret;
 }
 
-static ssize_t ad7816_show_mode(struct device *dev,
-				struct device_attribute *attr,
-				char *buf)
+static ssize_t mode_show(struct device *dev,
+			 struct device_attribute *attr,
+			 char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
@@ -132,10 +132,10 @@ static ssize_t ad7816_show_mode(struct device *dev,
 	return sysfs_emit(buf, "%s\n", ad7816_modes[chip->mode]);
 }
 
-static ssize_t ad7816_store_mode(struct device *dev,
-				 struct device_attribute *attr,
-				 const char *buf,
-				 size_t len)
+static ssize_t mode_store(struct device *dev,
+			  struct device_attribute *attr,
+			  const char *buf,
+			  size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
@@ -151,24 +151,20 @@ static ssize_t ad7816_store_mode(struct device *dev,
 	return len;
 }
 
-static IIO_DEVICE_ATTR(mode, 0644,
-		ad7816_show_mode,
-		ad7816_store_mode,
-		0);
+static IIO_DEVICE_ATTR_RW(mode, 0);
 
-static ssize_t ad7816_show_available_modes(struct device *dev,
-					   struct device_attribute *attr,
-					   char *buf)
+static ssize_t available_modes_show(struct device *dev,
+				    struct device_attribute *attr,
+				    char *buf)
 {
 	return sysfs_emit(buf, "full\npower-save\n");
 }
 
-static IIO_DEVICE_ATTR(available_modes, 0444, ad7816_show_available_modes,
-			NULL, 0);
+static IIO_DEVICE_ATTR_RO(available_modes, 0);
 
-static ssize_t ad7816_show_channel(struct device *dev,
-				   struct device_attribute *attr,
-				   char *buf)
+static ssize_t channel_show(struct device *dev,
+			    struct device_attribute *attr,
+			    char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
@@ -176,10 +172,10 @@ static ssize_t ad7816_show_channel(struct device *dev,
 	return sysfs_emit(buf, "%d\n", chip->channel_id);
 }
 
-static ssize_t ad7816_store_channel(struct device *dev,
-				    struct device_attribute *attr,
-				    const char *buf,
-				    size_t len)
+static ssize_t channel_store(struct device *dev,
+			     struct device_attribute *attr,
+			     const char *buf,
+			     size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
@@ -209,14 +205,11 @@ static ssize_t ad7816_store_channel(struct device *dev,
 	return len;
 }
 
-static IIO_DEVICE_ATTR(channel, 0644,
-		ad7816_show_channel,
-		ad7816_store_channel,
-		0);
+static IIO_DEVICE_ATTR_RW(channel, 0);
 
-static ssize_t ad7816_show_value(struct device *dev,
-				 struct device_attribute *attr,
-				 char *buf)
+static ssize_t value_show(struct device *dev,
+			  struct device_attribute *attr,
+			  char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
@@ -240,7 +233,7 @@ static ssize_t ad7816_show_value(struct device *dev,
 	return sysfs_emit(buf, "%u\n", data);
 }
 
-static IIO_DEVICE_ATTR(value, 0444, ad7816_show_value, NULL, 0);
+static IIO_DEVICE_ATTR_RO(value, 0);
 
 static struct attribute *ad7816_attributes[] = {
 	&iio_dev_attr_available_modes.dev_attr.attr,
@@ -270,9 +263,9 @@ static irqreturn_t ad7816_event_handler(int irq, void *private)
 	return IRQ_HANDLED;
 }
 
-static ssize_t ad7816_show_oti(struct device *dev,
-			       struct device_attribute *attr,
-			       char *buf)
+static ssize_t oti_show(struct device *dev,
+			struct device_attribute *attr,
+			char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
@@ -290,10 +283,10 @@ static ssize_t ad7816_show_oti(struct device *dev,
 	return sysfs_emit(buf, "%u\n", chip->oti_data[chip->channel_id]);
 }
 
-static inline ssize_t ad7816_set_oti(struct device *dev,
-				     struct device_attribute *attr,
-				     const char *buf,
-				     size_t len)
+static inline ssize_t oti_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf,
+				size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
@@ -331,8 +324,7 @@ static inline ssize_t ad7816_set_oti(struct device *dev,
 	return len;
 }
 
-static IIO_DEVICE_ATTR(oti, 0644,
-		       ad7816_show_oti, ad7816_set_oti, 0);
+static IIO_DEVICE_ATTR_RW(oti, 0);
 
 static struct attribute *ad7816_event_attributes[] = {
 	&iio_dev_attr_oti.dev_attr.attr,
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v4 4/4] staging: iio: adc: ad7816: use modern IIO attribute macros

Quoted 4 lines · Md. Mahmudul Hasan Mabud

Modernize the driver by using IIO_DEVICE_ATTR_RW and IIO_DEVICE_ATTR_RO macros. This involves renaming the callback functions to standard <name>_show and <name>_store formats, making the code cleaner and more maintainable.

Missed my comments about commit message changes and below about line lengths.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#9 lore raw

Re: [PATCH v4 4/4] staging: iio: adc: ad7816: use modern IIO attribute macros

Quoted 9 lines · On Tue, 31 Mar 2026 12:10:44 +0300 Andy Shevchenko…
Quoted 4 lines · Md. Mahmudul Hasan Mabud

Modernize the driver by using IIO_DEVICE_ATTR_RW and IIO_DEVICE_ATTR_RO macros. This involves renaming the callback functions to standard <name>_show and <name>_store formats, making the code cleaner and more maintainable.

Missed my comments about commit message changes and below about line lengths.

Another example of why you should go slow in sending new versions! Andy's email came in after you sent this version I think.

J