Skip to contentSkip to conversation

iio: debugfs register access through regmap in the core

PATCH Superseded Rupesh Majhi 3 participants 11 messages Dec 4

Subsystems (6)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover iio: debugfs register access through regmap in the core

More than 25 IIO drivers implement .debugfs_reg_access() with only a regmap_read() or regmap_write() on the regmap of the parent device. Patch 1 adds a core helper for that, patches 2 to 5 use it in four drivers and patch 6 adds register access to dps310 with it. The other drivers can follow in batches. Callbacks that take a lock or claim direct mode around the access are left alone.

Patch 6 is tested on a DPS310 on a BeagleBone Black. I don't have the other parts, so adxl313, adxl372, bma220 and ina226 were run on i2c-stub under QEMU: direct_reg_access gives the same results before and after.

Based on iio testing, which has Andy's split of the debugfs code [1].

v3:
- rename to iio_access_debugfs_regmap_reg() (Andy)
- 1: kernel-doc of iio_get_debugfs_dentry() moves inside the #if (Andy)
- Reviewed-by from Andy on 2 to 6

v2: https://lore.kernel.org/linux-iio/[email protected]/
- 1: the helper moves to industrialio-debugfs.c and is NULL without CONFIG_DEBUG_FS (Andy)
- Reviewed-by from Nuno on all, Tested-by from Petre on 4

v1: https://lore.kernel.org/linux-iio/[email protected]/

[1] https://lore.kernel.org/linux-iio/[email protected]/

Rupesh Majhi (6):
iio: core: add iio_access_debugfs_regmap_reg()
iio: accel: adxl313: use iio_access_debugfs_regmap_reg()
iio: accel: adxl372: use iio_access_debugfs_regmap_reg()
iio: accel: bma220: use iio_access_debugfs_regmap_reg()
iio: adc: ina2xx-adc: use iio_access_debugfs_regmap_reg()
iio: pressure: dps310: add debugfs register access

 drivers/iio/accel/adxl313_core.c   | 12 +-----------
 drivers/iio/accel/adxl372.c        | 15 +--------------
 drivers/iio/accel/bma220_core.c    | 12 +-----------
 drivers/iio/adc/ina2xx-adc.c       | 15 ++-------------
 drivers/iio/industrialio-debugfs.c | 28 ++++++++++++++++++++++++++++
 drivers/iio/pressure/dps310.c      |  1 +
 include/linux/iio/iio.h            |  7 ++++++-
 7 files changed, 40 insertions(+), 50 deletions(-)

base-commit: fba506008a5b548ad2708bc2765a039cb00a47ab

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/6 iio: core: add iio_access_debugfs_regmap_reg()

More than 25 drivers implement .debugfs_reg_access() with only a regmap_read() or regmap_write() on the regmap of the parent device. Add a helper that looks that regmap up with dev_get_regmap(), so these drivers can drop their own copy.

Without CONFIG_DEBUG_FS the helper is defined as NULL, like adis_debugfs_reg_access(), so drivers can use it without ifdefs.

---
v3:
- rename to iio_access_debugfs_regmap_reg() (Andy)
- kernel-doc of iio_get_debugfs_dentry() moves inside the #if, so the new prototype can share the block; kernel-doc output is unchanged (Andy)
- callback written as .debugfs_reg_access() (Andy)

v2:
- move the helper to industrialio-debugfs.c (Andy)
- NULL without CONFIG_DEBUG_FS, that file is not built then
- Nuno's tag kept, given "With Andy split"

 drivers/iio/industrialio-debugfs.c | 28 ++++++++++++++++++++++++++++
 include/linux/iio/iio.h            |  7 ++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)
drivers/iio/industrialio-debugfs.c +28 -0
index a8a3c7a3e85d..7b22308ed1a9 100644
@@ -10,6 +10,7 @@
 #include <linux/export.h>
 #include <linux/fs.h>
 #include <linux/init.h>
+#include <linux/regmap.h>
 #include <linux/sprintf.h>
 #include <linux/sysfs.h>
 #include <linux/types.h>
@@ -43,6 +44,33 @@ void __exit iio_debugfs_remove(void)
 	debugfs_remove(iio_debugfs_dentry);
 }
 
+/**
+ * iio_access_debugfs_regmap_reg() - debugfs register access through regmap
+ * @indio_dev: IIO device
+ * @reg: register address
+ * @writeval: value to write when @readval is NULL
+ * @readval: where to store the value read, or NULL for a write
+ *
+ * For &iio_info.debugfs_reg_access when the parent device has one regmap.
+ *
+ * Returns: 0 on success, -ENODEV if the parent has no regmap, or the error
+ * from regmap.
+ */
+int iio_access_debugfs_regmap_reg(struct iio_dev *indio_dev, unsigned int reg,
+				  unsigned int writeval, unsigned int *readval)
+{
+	struct regmap *map = dev_get_regmap(indio_dev->dev.parent, NULL);
+
+	if (!map)
+		return -ENODEV;
+
+	if (readval)
+		return regmap_read(map, reg, readval);
+
+	return regmap_write(map, reg, writeval);
+}
+EXPORT_SYMBOL_GPL(iio_access_debugfs_regmap_reg);
+
 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
 			      size_t count, loff_t *ppos)
 {
include/linux/iio/iio.h +6 -1
index 8b3fd7673293..ca44394ab115 100644
@@ -1000,17 +1000,22 @@ __printf(3, 4)
 struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent,
 					     struct module *this_mod,
 					     const char *fmt, ...);
+#if defined(CONFIG_DEBUG_FS)
 /**
  * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
  * @indio_dev:		IIO device structure for device
  **/
-#if defined(CONFIG_DEBUG_FS)
 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev);
+
+int iio_access_debugfs_regmap_reg(struct iio_dev *indio_dev, unsigned int reg,
+				  unsigned int writeval, unsigned int *readval);
 #else
 static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
 {
 	return NULL;
 }
+
+#define iio_access_debugfs_regmap_reg NULL
 #endif
 
 /**
Signature · 1 line
2.43.0
#4 lore raw

PATCH v3 2/6 iio: accel: adxl313: use iio_access_debugfs_regmap_reg()

In reply to Rupesh Majhi

The debugfs callback only reads or writes the regmap of the parent device, which is what the core helper does.

---
 drivers/iio/accel/adxl313_core.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/iio/accel/adxl313_core.c +1 -11
index 6dc918c4ae17..4f4336a9dc11 100644
@@ -1131,16 +1131,6 @@ static irqreturn_t adxl313_irq_handler(int irq, void *p)
 	return IRQ_HANDLED;
 }
 
-static int adxl313_reg_access(struct iio_dev *indio_dev, unsigned int reg,
-			      unsigned int writeval, unsigned int *readval)
-{
-	struct adxl313_data *data = iio_priv(indio_dev);
-
-	if (readval)
-		return regmap_read(data->regmap, reg, readval);
-	return regmap_write(data->regmap, reg, writeval);
-}
-
 static const struct iio_info adxl313_info = {
 	.read_raw	= adxl313_read_raw,
 	.write_raw	= adxl313_write_raw,
@@ -1150,7 +1140,7 @@ static const struct iio_info adxl313_info = {
 	.write_event_value = adxl313_write_event_value,
 	.read_avail	= adxl313_read_freq_avail,
 	.hwfifo_set_watermark = adxl313_set_watermark,
-	.debugfs_reg_access = &adxl313_reg_access,
+	.debugfs_reg_access = iio_access_debugfs_regmap_reg,
 };
 
 static int adxl313_setup(struct device *dev, struct adxl313_data *data,
Signature · 1 line
2.43.0
#5 lore raw

PATCH v3 3/6 iio: accel: adxl372: use iio_access_debugfs_regmap_reg()

In reply to Rupesh Majhi

The debugfs callback only reads or writes the regmap of the parent device, which is what the core helper does.

---
 drivers/iio/accel/adxl372.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/iio/accel/adxl372.c +1 -14
index a880ff4bfce1..cb9c424d3298 100644
@@ -806,19 +806,6 @@ static int adxl372_setup(struct adxl372_state *st)
 	return adxl372_set_op_mode(st, ADXL372_FULL_BW_MEASUREMENT);
 }
 
-static int adxl372_reg_access(struct iio_dev *indio_dev,
-			      unsigned int reg,
-			      unsigned int writeval,
-			      unsigned int *readval)
-{
-	struct adxl372_state *st = iio_priv(indio_dev);
-
-	if (readval)
-		return regmap_read(st->regmap, reg, readval);
-	else
-		return regmap_write(st->regmap, reg, writeval);
-}
-
 static int adxl372_read_raw(struct iio_dev *indio_dev,
 			    struct iio_chan_spec const *chan,
 			    int *val, int *val2, long info)
@@ -1221,7 +1208,7 @@ static const struct iio_info adxl372_info = {
 	.write_event_config = adxl372_write_event_config,
 	.read_event_value = adxl372_read_event_value,
 	.write_event_value = adxl372_write_event_value,
-	.debugfs_reg_access = &adxl372_reg_access,
+	.debugfs_reg_access = iio_access_debugfs_regmap_reg,
 	.hwfifo_set_watermark = adxl372_set_watermark,
 };
 
Signature · 1 line
2.43.0
#6 lore raw

PATCH v3 4/6 iio: accel: bma220: use iio_access_debugfs_regmap_reg()

In reply to Rupesh Majhi

The debugfs callback only reads or writes the regmap of the parent device, which is what the core helper does.

---
 drivers/iio/accel/bma220_core.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/iio/accel/bma220_core.c +1 -11
index 23eff578158c..4c595177688a 100644
@@ -369,21 +369,11 @@ static int bma220_read_avail(struct iio_dev *indio_dev,
 	}
 }
 
-static int bma220_reg_access(struct iio_dev *indio_dev, unsigned int reg,
-			     unsigned int writeval, unsigned int *readval)
-{
-	struct bma220_data *data = iio_priv(indio_dev);
-
-	if (readval)
-		return regmap_read(data->regmap, reg, readval);
-	return regmap_write(data->regmap, reg, writeval);
-}
-
 static const struct iio_info bma220_info = {
 	.read_raw		= bma220_read_raw,
 	.write_raw		= bma220_write_raw,
 	.read_avail		= bma220_read_avail,
-	.debugfs_reg_access	= &bma220_reg_access,
+	.debugfs_reg_access	= iio_access_debugfs_regmap_reg,
 };
 
 static int bma220_reset(struct bma220_data *data, bool up)
Signature · 1 line
2.43.0
#7 lore raw

PATCH v3 5/6 iio: adc: ina2xx-adc: use iio_access_debugfs_regmap_reg()

In reply to Rupesh Majhi

The debugfs callback only reads or writes the regmap of the parent device, which is what the core helper does.

---
 drivers/iio/adc/ina2xx-adc.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/iio/adc/ina2xx-adc.c +2 -13
index c412f5ed46a1..abaa85da9acc 100644
@@ -895,17 +895,6 @@ static const struct iio_buffer_setup_ops ina2xx_setup_ops = {
 	.predisable = &ina2xx_buffer_disable,
 };
 
-static int ina2xx_debug_reg(struct iio_dev *indio_dev,
-			    unsigned reg, unsigned writeval, unsigned *readval)
-{
-	struct ina2xx_chip_info *chip = iio_priv(indio_dev);
-
-	if (!readval)
-		return regmap_write(chip->regmap, reg, writeval);
-
-	return regmap_read(chip->regmap, reg, readval);
-}
-
 /* Possible integration times for vshunt and vbus */
 static IIO_CONST_ATTR_NAMED(ina219_integration_time_available,
 			    integration_time_available,
@@ -950,14 +939,14 @@ static const struct iio_info ina219_info = {
 	.read_raw = ina2xx_read_raw,
 	.read_avail = ina2xx_read_avail,
 	.write_raw = ina2xx_write_raw,
-	.debugfs_reg_access = ina2xx_debug_reg,
+	.debugfs_reg_access = iio_access_debugfs_regmap_reg,
 };
 
 static const struct iio_info ina226_info = {
 	.attrs = &ina226_attribute_group,
 	.read_raw = ina2xx_read_raw,
 	.write_raw = ina2xx_write_raw,
-	.debugfs_reg_access = ina2xx_debug_reg,
+	.debugfs_reg_access = iio_access_debugfs_regmap_reg,
 };
 
 /* Initialize the configuration and calibration registers. */
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 6/6 iio: pressure: dps310: add debugfs register access

In reply to Rupesh Majhi

Expose the registers through direct_reg_access with the core regmap helper, so the configuration and status registers can be looked at through the driver's regmap instead of around it on the bus.

The access is not serialised with the driver beyond the regmap lock. Reading INT_STS or a result register clears what the driver waits for, as it would on the bus.

---
 drivers/iio/pressure/dps310.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iio/pressure/dps310.c +1 -0
index 74d4111b5e29..ae132c188c9c 100644
@@ -1557,6 +1557,7 @@ static const struct iio_info dps310_info = {
 	.write_raw = dps310_write_raw,
 	.hwfifo_set_watermark = dps310_hwfifo_set_watermark,
 	.hwfifo_flush_to_buffer = dps310_hwfifo_flush,
+	.debugfs_reg_access = iio_access_debugfs_regmap_reg,
 };
 
 static int dps310_probe(struct i2c_client *client)
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/6] iio: core: add iio_access_debugfs_regmap_reg()

In reply to Rupesh Majhi
Quoted 10 lines · Rupesh Majhi

More than 25 drivers implement .debugfs_reg_access() with only a regmap_read() or regmap_write() on the regmap of the parent device. Add a helper that looks that regmap up with dev_get_regmap(), so these drivers can drop their own copy.

Without CONFIG_DEBUG_FS the helper is defined as NULL, like adis_debugfs_reg_access(), so drivers can use it without ifdefs.

- kernel-doc of iio_get_debugfs_dentry() moves inside the #if, so the new prototype can share the block; kernel-doc output is unchanged (Andy)

Just to be sure, you checked kernel-doc render of the file, but would it be possible to check the sphynx generated documentation? I believe it should not be dependent on the CONFIG_DEBUG_FS=n.

...

drivers/iio/industrialio-debugfs.c void __exit iio_debugfs_remove(void) · line 63

Quoted 6 lines
drivers/iio/industrialio-debugfs.c +5 -0
+int iio_access_debugfs_regmap_reg(struct iio_dev *indio_dev, unsigned int reg,
+				  unsigned int writeval, unsigned int *readval)
+{
 
+	struct regmap *map = dev_get_regmap(indio_dev->dev.parent, NULL);
+

Split the assignment, it would be easier to maintain.

drivers/iio/industrialio-debugfs.c void __exit iio_debugfs_remove(void) · line 65

Quoted 2 lines
drivers/iio/industrialio-debugfs.c +2 -0
+	if (!map)
+		return -ENODEV;
	struct regmap *map;

	map = dev_get_regmap(indio_dev->dev.parent, NULL);
	if (!map)
		return -ENODEV;
Quoted 5 lines
drivers/iio/industrialio-debugfs.c +5 -0
+	if (readval)
+		return regmap_read(map, reg, readval);
+
+	return regmap_write(map, reg, writeval);
+}

...

Quoted 5 lines

+#if defined(CONFIG_DEBUG_FS)
/**
* iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
* @indio_dev: IIO device structure for device
**/

TBH, I don't understand why kernel-doc is even here. More I think about it more I believe it should be simply moved to the respective C-file.

Quoted 2 lines

-#if defined(CONFIG_DEBUG_FS)
struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev);

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw
In reply to Rupesh Majhi
Quoted 11 lines · Rupesh Majhi

More than 25 IIO drivers implement .debugfs_reg_access() with only a regmap_read() or regmap_write() on the regmap of the parent device. Patch 1 adds a core helper for that, patches 2 to 5 use it in four drivers and patch 6 adds register access to dps310 with it. The other drivers can follow in batches. Callbacks that take a lock or claim direct mode around the access are left alone.

Patch 6 is tested on a DPS310 on a BeagleBone Black. I don't have the other parts, so adxl313, adxl372, bma220 and ina226 were run on i2c-stub under QEMU: direct_reg_access gives the same results before and after.

...

Quoted 1 line

- 1: kernel-doc of iio_get_debugfs_dentry() moves inside the #if (Andy)

This part should be done in a separate patch. Also see a comment I gave against patch 1.

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

Re: [PATCH v3 1/6] iio: core: add iio_access_debugfs_regmap_reg()

In reply to Andy Shevchenko
Quoted 10 lines
Quoted 7 lines

+#if defined(CONFIG_DEBUG_FS)
/**
* iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
* @indio_dev: IIO device structure for device
**/

TBH, I don't understand why kernel-doc is even here. More I think about it more I believe it should be simply moved to the respective C-file.

My foolishness years ago. :) I've always considered it too churn heavy to fix that across the tree, but perhaps now is the time.

J

Quoted 4 lines
Quoted 3 lines

-#if defined(CONFIG_DEBUG_FS)
struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev);

maintainer
#10 lore raw
In reply to Andy Shevchenko
Quoted 13 lines · On Fri, 4 Dec 2026 20:36:14 +0200 Andy Shevchenko…
Quoted 11 lines · Rupesh Majhi

More than 25 IIO drivers implement .debugfs_reg_access() with only a regmap_read() or regmap_write() on the regmap of the parent device. Patch 1 adds a core helper for that, patches 2 to 5 use it in four drivers and patch 6 adds register access to dps310 with it. The other drivers can follow in batches. Callbacks that take a lock or claim direct mode around the access are left alone.

Patch 6 is tested on a DPS310 on a BeagleBone Black. I don't have the other parts, so adxl313, adxl372, bma220 and ina226 were run on i2c-stub under QEMU: direct_reg_access gives the same results before and after.

Quoted 6 lines

...

Quoted 1 line

- 1: kernel-doc of iio_get_debugfs_dentry() moves inside the #if (Andy)

This part should be done in a separate patch. Also see a comment I gave against patch 1.

As Andy suggests, it should really be in the c file anyway. Better to take this opportunity to add an extra patch that just moves it there.

Thanks

Jonathan