Skip to contentSkip to conversation

iio: debugfs register access through regmap in the core

PATCH New Rupesh Majhi 1 participant 7 messages Nov 28

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

v2 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. 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].

v2:
- 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_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-debugfs.c | 28 ++++++++++++++++++++++++++++
 drivers/iio/pressure/dps310.c      |  1 +
 include/linux/iio/iio.h            |  4 ++++
 7 files changed, 38 insertions(+), 49 deletions(-)

base-commit: 5ebd0bae9fbf9929c08280cf88aebaa41247c61a

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 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.

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

---
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            |  4 ++++
 2 files changed, 32 insertions(+)
drivers/iio/industrialio-debugfs.c +28 -0
index a8a3c7a3e85d..22a96f286f4b 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_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);
+
 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
 			      size_t count, loff_t *ppos)
 {
include/linux/iio/iio.h +4 -0
index 8b3fd7673293..8ecf8ee879cf 100644
@@ -1006,11 +1006,15 @@ struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent,
  **/
 #if defined(CONFIG_DEBUG_FS)
 struct dentry *iio_get_debugfs_dentry(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);
 #else
 static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
 {
 	return NULL;
 }
+
+#define iio_debugfs_reg_access_regmap NULL
 #endif
 
 /**
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 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 v2 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 v2 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
#5 lore raw

PATCH v2 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
#6 lore raw

PATCH v2 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..e23cb8b5194f 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_debugfs_reg_access_regmap,
 };
 
 static int dps310_probe(struct i2c_client *client)
Signature · 1 line
2.43.0