Skip to contentSkip to conversation

iio: debugfs register access through regmap in the core

PATCH In review Rupesh Majhi 4 participants 11 messages Nov 24

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

cover iio: debugfs register access through regmap in the core

More than 25 IIO drivers have a debugfs_reg_access callback that is 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 if the helper is wanted.

The helper finds the regmap with dev_get_regmap() on the parent, so it only fits drivers with one regmap there. Existing 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. For those, adxl313, adxl372, bma220 and ina226 (ina219 uses the same callback) were instantiated on i2c-stub under QEMU:
reads, writes and refused registers through direct_reg_access give the same results before and after.

Based on iio testing.

Rupesh Majhi (6):
iio: core: add iio_debugfs_reg_access_regmap()
iio: accel: adxl313: use iio_debugfs_reg_access_regmap()
iio: accel: adxl372: use iio_debugfs_reg_access_regmap()
iio: accel: bma220: use iio_debugfs_reg_access_regmap()
iio: adc: ina2xx-adc: use iio_debugfs_reg_access_regmap()
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-core.c  | 28 ++++++++++++++++++++++++++++
 drivers/iio/pressure/dps310.c    |  1 +
 include/linux/iio/iio.h          |  2 ++
 7 files changed, 36 insertions(+), 49 deletions(-)

base-commit: f2cbef9101b9fc979a324bb1242b8b802a2484b6

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/6 iio: core: add iio_debugfs_reg_access_regmap()

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.

---
 drivers/iio/industrialio-core.c | 28 ++++++++++++++++++++++++++++
 include/linux/iio/iio.h         |  2 ++
 2 files changed, 30 insertions(+)
drivers/iio/industrialio-core.c +28 -0
index dabd57a0994e..e658687d978f 100644
@@ -28,6 +28,7 @@
 #include <linux/overflow.h>
 #include <linux/poll.h>
 #include <linux/property.h>
+#include <linux/regmap.h>
 #include <linux/sched.h>
 #include <linux/seq_buf.h>
 #include <linux/slab.h>
@@ -446,6 +447,33 @@ static void __exit iio_exit(void)
 	debugfs_remove(iio_debugfs_dentry);
 }
 
+/**
+ * iio_debugfs_reg_access_regmap() - 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_debugfs_reg_access_regmap(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_debugfs_reg_access_regmap);
+
 #if defined(CONFIG_DEBUG_FS)
 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
 			      size_t count, loff_t *ppos)
include/linux/iio/iio.h +2 -0
index 8b3fd7673293..8407e6e4620a 100644
@@ -677,6 +677,8 @@ struct iio_dev {
 
 int iio_device_id(struct iio_dev *indio_dev);
 int iio_device_get_current_mode(struct iio_dev *indio_dev);
+int iio_debugfs_reg_access_regmap(struct iio_dev *indio_dev, unsigned int reg,
+				  unsigned int writeval, unsigned int *readval);
 bool iio_buffer_enabled(struct iio_dev *indio_dev);
 
 const struct iio_chan_spec
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/6 iio: accel: adxl313: use iio_debugfs_reg_access_regmap()

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..c37bf11d77d5 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_debugfs_reg_access_regmap,
 };
 
 static int adxl313_setup(struct device *dev, struct adxl313_data *data,
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/6 iio: accel: adxl372: use iio_debugfs_reg_access_regmap()

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..994c5cd58bed 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_debugfs_reg_access_regmap,
 	.hwfifo_set_watermark = adxl372_set_watermark,
 };
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/6 iio: accel: bma220: use iio_debugfs_reg_access_regmap()

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..d2f21c0ab491 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_debugfs_reg_access_regmap,
 };
 
 static int bma220_reset(struct bma220_data *data, bool up)
Signature · 1 line
2.43.0
Tested-by on v1 4/6
#5 lore raw

Re: [PATCH 4/6] iio: accel: bma220: use iio_debugfs_reg_access_regmap()

Hello!

drivers/iio/accel/bma220_core.c

Quoted 10 lines · 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 +0 -0
index 23eff578158c..d2f21c0ab491 100644

passes all my unit tests (which do use debugfs regmap access) on both i2c and spi sensors, thanks for the patch.

best regards,
peter

#6 lore raw

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

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..587e82c33454 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_debugfs_reg_access_regmap,
 };
 
 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_debugfs_reg_access_regmap,
 };
 
 /* Initialize the configuration and calibration registers. */
Signature · 1 line
2.43.0
#7 lore raw

PATCH 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 9ffef150cc4c..257fe25c5ce1 100644
@@ -1563,6 +1563,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_debugfs_reg_access_regmap,
 };
 
 static int dps310_probe(struct i2c_client *client)
Signature · 1 line
2.43.0
#8 lore raw
In reply to Rupesh Majhi
Quoted 9 lines · Rupesh Majhi

More than 25 IIO drivers have a debugfs_reg_access callback that is 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 if the helper is wanted.

The helper finds the regmap with dev_get_regmap() on the parent, so it only fits drivers with one regmap there. Existing callbacks that take a lock or claim direct mode around the access are left alone.

Nice! But I think we want a bit more to do to prepare for this change. I was triggered by regmap.h include in IIO core.

I can see the first patch in the series to split debugfs bits into industrialio-debugfs.c first, and then your first patch builds on top of that.

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

More than 25 IIO drivers have a debugfs_reg_access callback that is 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 if the helper is wanted.

The helper finds the regmap with dev_get_regmap() on the parent, so it only fits drivers with one regmap there. Existing callbacks that take a lock or claim direct mode around the access are left alone.

Quoted 6 lines

Nice! But I think we want a bit more to do to prepare for this change. I was triggered by regmap.h include in IIO core.

I can see the first patch in the series to split debugfs bits into industrialio-debugfs.c first, and then your first patch builds on top of that.

I just sent one separately, feel free to include into your v2. [email protected]

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Reviewed-by on the v1 series
#10 lore raw
In reply to Rupesh Majhi
Quoted 10 lines · Rupesh Majhi

More than 25 IIO drivers have a debugfs_reg_access callback that is 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 if the helper is wanted.

The helper finds the regmap with dev_get_regmap() on the parent, so it only fits drivers with one regmap there. Existing callbacks that take a lock or claim direct mode around the access are left alone.

Quoted 7 lines

Patch 6 is tested on a DPS310 on a BeagleBone Black. I don't have the other parts. For those, adxl313, adxl372, bma220 and ina226 (ina219 uses the same callback) were instantiated on i2c-stub under QEMU:
reads, writes and refused registers through direct_reg_access give the same results before and after.

Based on iio testing.

LGTM! With Andy spilt:

Quoted 23 lines

Rupesh Majhi (6):
iio: core: add iio_debugfs_reg_access_regmap()
iio: accel: adxl313: use iio_debugfs_reg_access_regmap()
iio: accel: adxl372: use iio_debugfs_reg_access_regmap()
iio: accel: bma220: use iio_debugfs_reg_access_regmap()
iio: adc: ina2xx-adc: use iio_debugfs_reg_access_regmap()
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-core.c  | 28 ++++++++++++++++++++++++++++
 drivers/iio/pressure/dps310.c    |  1 +
 include/linux/iio/iio.h          |  2 ++
 7 files changed, 36 insertions(+), 49 deletions(-)

base-commit: f2cbef9101b9fc979a324bb1242b8b802a2484b6

Signature · 1 line
2.43.0