Skip to contentSkip to conversation

iio: resolver: move ad2s1210 out of staging

PATCH Applied David Lechner 2 participants 10 messages Oct 10, 2023 – Oct 12, 2023

Subsystems (4)
lore
Share & tools
lore

David Lechner

author reviewer
#0 lore raw

v5 cover iio: resolver: move ad2s1210 out of staging

This series is working towards moving the ad2s1210 resolver driver out of staging (after 13 years!). It involves a bunch of fixes and improvements to make proper device tree bindings and use standard IIO sysfs attributes.

This series has been tested on actual hardware using a EVAL-AD2S1210 evaluation board. (Note: not all device tree features have been implemented in the driver since the eval board doesn't support them out of the box. We plan to add them later if needed.)

---

v5 changes:
* Dropped applied patches:
* "staging: iio: resolver: ad2s1210: do not use fault register for dummy read"
* "staging: iio: resolver: ad2s1210: implement hysteresis as channel attr"
* "staging: iio: resolver: ad2s1210: convert fexcit to channel attribute"
* "staging: iio: resolver: ad2s1210: convert resolution to devicetree property"
* "staging: iio: resolver: ad2s1210: add phase lock range support"
* "staging: iio: resolver: ad2s1210: add triggered buffer support"
* "staging: iio: resolver: ad2s1210: convert LOT threshold attrs to event attrs"
* "staging: iio: resolver: ad2s1210: convert LOS threshold to event attr"
* "staging: iio: resolver: ad2s1210: convert DOS overrange threshold to event attr"
* "staging: iio: resolver: ad2s1210: convert DOS mismatch threshold to event attr"
* "staging: iio: resolver: ad2s1210: rename DOS reset min/max attrs"
* "iio: event: add optional event label support"
* "staging: iio: resolver: ad2s1210: implement fault events"
* "staging: iio: resolver: ad2s1210: add register/fault support summary"
* "staging: iio: resolver: ad2s1210: add label attribute support"
* "staging: iio: resolver: ad2s1210: remove fault attribute"
* Added new patches:
* "staging: iio: resolver: ad2s1210: refactor sample toggle"
* "staging: iio: resolver: ad2s1210: clear faults after soft reset"
* "iio: resolver: ad2s1210: move out of staging"

Link to v4: https://lore.kernel.org/r/[email protected]

v4 changes:
* Dropped applied patches:
* "dt-bindings: iio: resolver: add devicetree bindings for ad2s1210"
* "staging: iio: resolver: ad2s1210: read excitation frequency from control register"
* "staging: iio: resolver: ad2s1210: refactor setting excitation frequency"
* "staging: iio: resolver: ad2s1210: rework gpios"
* "staging: iio: resolver: ad2s1210: remove config attribute"
* "staging: iio: resolver: ad2s1210: add debugfs reg access"
* "staging: iio: resolver: ad2s1210: use regmap for config registers"
* "staging: iio: resolver: ad2s1210: use devicetree to get CLKIN rate"
* "staging: iio: resolver: ad2s1210: implement IIO_CHAN_INFO_SCALE"
* "staging: iio: resolver: ad2s1210: always use 16-bit value for raw read"
* "staging: iio: resolver: ad2s1210: sort imports"
* "staging: iio: resolver: ad2s1210: remove spi_set_drvdata()"
* "staging: iio: resolver: ad2s1210: check return of ad2s1210_initial()"
* "staging: iio: resolver: ad2s1210: remove call to spi_setup()"
* "staging: iio: resolver: ad2s1210: fix use before initialization"
* Added new patches:
* "staging: iio: resolver: ad2s1210: do not use fault register for dummy read"
* "iio: event: add optional event label support"
* "staging: iio: resolver: ad2s1210: add register/fault support summary"
* "staging: iio: resolver: ad2s1210: remove fault attribute"
* "staging: iio: resolver: ad2s1210: simplify code with guard(mutex)"
* Fixed DT property name in commit description of "staging: iio:
resolver: ad2s1210: convert resolution to devicetree property"
* Fixed compile error in "staging: iio: resolver: ad2s1210: implement fault events".
* Fixed angl0 hysteresis raw values when assigned-resolution-bits != 16.
* Fixed missing word in "staging: iio: resolver: ad2s1210: convert DOS overrange threshold to event attr" commit description.
* Fixed missing static qualifier on event attribute definitions.
* Dropped used of X/Y modifiers on sine/cosine channels.
* Changed type/direction on some events.
* Added event *_label attributes.

Link to v3: https://lore.kernel.org/r/[email protected]

v3 changes:
* Dropped applied patches:
* "staging: iio: resolver: ad2s1210: fix ad2s1210_show_fault"
* "staging: iio: resolver: ad2s1210: fix not restoring sample gpio in channel read"
* Dropped "staging: iio: Documentation: document IIO resolver AD2S1210 sysfs attributes". We will attempt to use existing ABI for faults/ thresholds in a future series.
* Added description of A0/A1 lines in DT bindings.
* Added power supply regulators to DT bindings.
* Moved sorting imports to separate patch.
* Renamed fclkin to clkin_hz.
* Added __be16 sample field to state struct for reading raw samples.
* Split out new function ad2s1210_single_conversion() from ad2s1210_read_raw().
* Split out new ad2s1210_get_hysteresis() and ad2s1210_set_hysteresis() functions.
* Fixed multi-line comment style.
* Added notes about soft reset not resetting config registers.
* Made use of FIELD_PREP() macro.
* Added more explanation to regmap commit message.
* Removed datasheet names from channel specs.
* Replaced "staging: iio: resolver: ad2s1210: rename fexcit attribute" with "staging: iio: resolver: ad2s1210: convert fexcit to channel attribute".
* Replaced "staging: iio: resolver: ad2s1210: add phase_lock_range attributes" with "staging: iio: resolver: ad2s1210: add phase lock range support"
* Added additional patches to convert custom device attributes to event attributes.
* Added patch for to add label attributes.

Link to v2: https://lore.kernel.org/r/[email protected]

v2 changes:
* Address initial device tree patch feedback
* Drop "iio: sysfs: add IIO_DEVICE_ATTR_NAMED_RW macro" (related cleanups also dropped for now, will address in a future series if needed)
* Apply improvements as a series of patches to the staging driver. It is not quite ready for the move out of staging patch yet.

---
David Lechner (4):
staging: iio: resolver: ad2s1210: refactor sample toggle
staging: iio: resolver: ad2s1210: clear faults after soft reset
staging: iio: resolver: ad2s1210: simplify code with guard(mutex)
iio: resolver: ad2s1210: move out of staging

 .../ABI/testing}/sysfs-bus-iio-resolver-ad2s1210   |   0
 drivers/iio/resolver/Kconfig                       |  13 ++
 drivers/iio/resolver/Makefile                      |   1 +
 drivers/{staging => }/iio/resolver/ad2s1210.c      | 208 ++++++++++-----------
 drivers/staging/iio/Kconfig                        |   1 -
 drivers/staging/iio/Makefile                       |   1 -
 drivers/staging/iio/resolver/Kconfig               |  19 --
 drivers/staging/iio/resolver/Makefile              |   6 -
 8 files changed, 109 insertions(+), 140 deletions(-)

---
base-commit: 57fd97ead0e87ca528736f1945a3ba3096de2f3e
change-id: 20230925-ad2s1210-mainline-2791ef75e386

Best regards,

Signature · 1 line
David Lechner <[email protected]>
author reviewer
#1 lore raw

PATCH v5 1/4 staging: iio: resolver: ad2s1210: refactor sample toggle

This refactors the sample line toggle in the ad2s1210 resolver driver to a separate function. The sample has some timing requirements, so this ensures that it is always done the same way, both in the existing call sites and any future usage.

Previously, the sample line was kept on for the duration of the read, but this is not necessary. Data is latched in on the rising edge and after the specified delay the state of the sample line does not matter.

---

v5 changes: New patch in v5.

 drivers/staging/iio/resolver/ad2s1210.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
drivers/staging/iio/resolver/ad2s1210.c +22 -8
index e0bea3c68664..59c273a4b6a9 100644
@@ -286,6 +286,26 @@ static int ad2s1210_regmap_reg_read(void *context, unsigned int reg,
 	return 0;
 }
 
+/*
+ * Toggles the SAMPLE line on the AD2S1210 to latch in the current position,
+ * velocity, and faults.
+ *
+ * Must be called with lock held.
+ */
+static void ad2s1210_toggle_sample_line(struct ad2s1210_state *st)
+{
+	/*
+	 * Datasheet specifies minimum hold time t16 = 2 * tck + 20 ns. So the
+	 * longest time needed is when CLKIN is 6.144 MHz, in which case t16
+	 * ~= 350 ns. The same delay is also needed before re-asserting the
+	 * SAMPLE line.
+	 */
+	gpiod_set_value(st->sample_gpio, 1);
+	ndelay(350);
+	gpiod_set_value(st->sample_gpio, 0);
+	ndelay(350);
+}
+
 /*
  * Sets the excitation frequency and performs software reset.
  *
@@ -405,10 +425,8 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev,
 	int ret;
 
 	mutex_lock(&st->lock);
-	gpiod_set_value(st->sample_gpio, 1);
+	ad2s1210_toggle_sample_line(st);
 	timestamp = iio_get_time_ns(indio_dev);
-	/* delay (6 * tck + 20) nano seconds */
-	udelay(1);
 
 	switch (chan->type) {
 	case IIO_ANGL:
@@ -444,9 +462,6 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev,
 	ad2s1210_push_events(indio_dev, st->sample.fault, timestamp);
 
 error_ret:
-	gpiod_set_value(st->sample_gpio, 0);
-	/* delay (2 * tck + 20) nano seconds */
-	udelay(1);
 	mutex_unlock(&st->lock);
 	return ret;
 }
@@ -1268,7 +1283,7 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)
 	mutex_lock(&st->lock);
 
 	memset(&st->scan, 0, sizeof(st->scan));
-	gpiod_set_value(st->sample_gpio, 1);
+	ad2s1210_toggle_sample_line(st);
 
 	if (test_bit(0, indio_dev->active_scan_mask)) {
 		ret = ad2s1210_set_mode(st, MOD_POS);
@@ -1298,7 +1313,6 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)
 	iio_push_to_buffers_with_timestamp(indio_dev, &st->scan, pf->timestamp);
 
 error_ret:
-	gpiod_set_value(st->sample_gpio, 0);
 	mutex_unlock(&st->lock);
 	iio_trigger_notify_done(indio_dev->trig);
 
Signature · 1 line
2.42.0
maintainer Applied
#2 lore raw

Re: [PATCH v5 1/4] staging: iio: resolver: ad2s1210: refactor sample toggle

Quoted 10 lines · On Tue, 10 Oct 2023 16:12:33 -0500 David Lechner <[email protected]> wrote:

This refactors the sample line toggle in the ad2s1210 resolver driver to a separate function. The sample has some timing requirements, so this ensures that it is always done the same way, both in the existing call sites and any future usage.

Previously, the sample line was kept on for the duration of the read, but this is not necessary. Data is latched in on the rising edge and after the specified delay the state of the sample line does not matter.

Applied.

author reviewer
#3 lore raw

PATCH v5 2/4 staging: iio: resolver: ad2s1210: clear faults after soft reset

In reply to David Lechner

When a software reset is performed on the AD2S1210 to make the selected excitation frequency take effect, it always triggers faults on the input signals because the output signal is interrupted momentarily. So we need to clear the faults after the software reset to avoid triggering fault events the next time a sample is read.

The datasheet specifies a time t[track] in Table 27 that specifies the settle time in milliseconds after a reset depending on the selected resolution. This is used in the driver to add an appropriate delay.

---

v5 changes: New patch in v5.

 drivers/staging/iio/resolver/ad2s1210.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/staging/iio/resolver/ad2s1210.c +24 -1
index 59c273a4b6a9..cf4018434447 100644
@@ -314,6 +314,9 @@ static void ad2s1210_toggle_sample_line(struct ad2s1210_state *st)
 static int ad2s1210_reinit_excitation_frequency(struct ad2s1210_state *st,
 						u16 fexcit)
 {
+	/* Map resolution to settle time in milliseconds. */
+	static const int track_time_ms[] = { 10, 20, 25, 60 };
+	unsigned int ignored;
 	int ret;
 	u8 fcw;
 
@@ -329,7 +332,27 @@ static int ad2s1210_reinit_excitation_frequency(struct ad2s1210_state *st,
 	 * Software reset reinitializes the excitation frequency output.
 	 * It does not reset any of the configuration registers.
 	 */
-	return regmap_write(st->regmap, AD2S1210_REG_SOFT_RESET, 0);
+	ret = regmap_write(st->regmap, AD2S1210_REG_SOFT_RESET, 0);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Soft reset always triggers some faults due the change in the output
+	 * signal so clear the faults too. We need to delay for some time
+	 * (what datasheet calls t[track]) to allow things to settle before
+	 * clearing the faults.
+	 */
+	msleep(track_time_ms[st->resolution] * 8192000 / st->clkin_hz);
+
+	/* Reading the fault register clears the faults. */
+	ret = regmap_read(st->regmap, AD2S1210_REG_FAULT, &ignored);
+	if (ret < 0)
+		return ret;
+
+	/* Have to toggle sample line to get fault output pins to reset. */
+	ad2s1210_toggle_sample_line(st);
+
+	return 0;
 }
 
 static void ad2s1210_push_events(struct iio_dev *indio_dev,
Signature · 1 line
2.42.0
maintainer Applied
#4 lore raw

Re: [PATCH v5 2/4] staging: iio: resolver: ad2s1210: clear faults after soft reset

Quoted 11 lines · On Tue, 10 Oct 2023 16:12:34 -0500 David Lechner <[email protected]> wrote:

When a software reset is performed on the AD2S1210 to make the selected excitation frequency take effect, it always triggers faults on the input signals because the output signal is interrupted momentarily. So we need to clear the faults after the software reset to avoid triggering fault events the next time a sample is read.

The datasheet specifies a time t[track] in Table 27 that specifies the settle time in milliseconds after a reset depending on the selected resolution. This is used in the driver to add an appropriate delay.

Applied

author reviewer
#5 lore raw

PATCH v5 3/4 staging: iio: resolver: ad2s1210: simplify code with guard(mutex)

In reply to David Lechner

We can simplify the code and get rid of most of the gotos by using guard(mutex) from cleanup.h.

---

v5 changes: None (rebased).

Review comment from v4 is no longer applicable as it was addressed in "staging: iio: resolver: ad2s1210: refactor sample toggle"

 drivers/staging/iio/resolver/ad2s1210.c | 153 ++++++++++----------------------
 1 file changed, 49 insertions(+), 104 deletions(-)
drivers/staging/iio/resolver/ad2s1210.c +49 -104
index cf4018434447..bd4a90c222b5 100644
@@ -47,6 +47,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/bits.h>
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/device.h>
@@ -447,7 +448,8 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev,
 	s64 timestamp;
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
+
 	ad2s1210_toggle_sample_line(st);
 	timestamp = iio_get_time_ns(indio_dev);
 
@@ -459,14 +461,13 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev,
 		ret = ad2s1210_set_mode(st, MOD_VEL);
 		break;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
 	if (ret < 0)
-		goto error_ret;
+		return ret;
 	ret = spi_read(st->sdev, &st->sample, 3);
 	if (ret < 0)
-		goto error_ret;
+		return ret;
 
 	switch (chan->type) {
 	case IIO_ANGL:
@@ -478,14 +479,11 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev,
 		ret = IIO_VAL_INT;
 		break;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
 
 	ad2s1210_push_events(indio_dev, st->sample.fault, timestamp);
 
-error_ret:
-	mutex_unlock(&st->lock);
 	return ret;
 }
 
@@ -493,11 +491,9 @@ static int ad2s1210_get_hysteresis(struct ad2s1210_state *st, int *val)
 {
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	ret = regmap_test_bits(st->regmap, AD2S1210_REG_CONTROL,
 			       AD2S1210_ENABLE_HYSTERESIS);
-	mutex_unlock(&st->lock);
-
 	if (ret < 0)
 		return ret;
 
@@ -507,15 +503,10 @@ static int ad2s1210_get_hysteresis(struct ad2s1210_state *st, int *val)
 
 static int ad2s1210_set_hysteresis(struct ad2s1210_state *st, int val)
 {
-	int ret;
-
-	mutex_lock(&st->lock);
-	ret = regmap_update_bits(st->regmap, AD2S1210_REG_CONTROL,
-				 AD2S1210_ENABLE_HYSTERESIS,
-				 val ? AD2S1210_ENABLE_HYSTERESIS : 0);
-	mutex_unlock(&st->lock);
-
-	return ret;
+	guard(mutex)(&st->lock);
+	return regmap_update_bits(st->regmap, AD2S1210_REG_CONTROL,
+				  AD2S1210_ENABLE_HYSTERESIS,
+				  val ? AD2S1210_ENABLE_HYSTERESIS : 0);
 }
 
 static int ad2s1210_get_phase_lock_range(struct ad2s1210_state *st,
@@ -523,11 +514,9 @@ static int ad2s1210_get_phase_lock_range(struct ad2s1210_state *st,
 {
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	ret = regmap_test_bits(st->regmap, AD2S1210_REG_CONTROL,
 			       AD2S1210_PHASE_LOCK_RANGE_44);
-	mutex_unlock(&st->lock);
-
 	if (ret < 0)
 		return ret;
 
@@ -547,7 +536,7 @@ static int ad2s1210_get_phase_lock_range(struct ad2s1210_state *st,
 static int ad2s1210_set_phase_lock_range(struct ad2s1210_state *st,
 					 int val, int val2)
 {
-	int deg, ret;
+	int deg;
 
 	/* convert radians to degrees - only two allowable values */
 	if (val == PHASE_44_DEG_TO_RAD_INT && val2 == PHASE_44_DEG_TO_RAD_MICRO)
@@ -558,12 +547,10 @@ static int ad2s1210_set_phase_lock_range(struct ad2s1210_state *st,
 	else
 		return -EINVAL;
 
-	mutex_lock(&st->lock);
-	ret = regmap_update_bits(st->regmap, AD2S1210_REG_CONTROL,
-				 AD2S1210_PHASE_LOCK_RANGE_44,
-				 deg == 44 ? AD2S1210_PHASE_LOCK_RANGE_44 : 0);
-	mutex_unlock(&st->lock);
-	return ret;
+	guard(mutex)(&st->lock);
+	return regmap_update_bits(st->regmap, AD2S1210_REG_CONTROL,
+				  AD2S1210_PHASE_LOCK_RANGE_44,
+				  deg == 44 ? AD2S1210_PHASE_LOCK_RANGE_44 : 0);
 }
 
 /* map resolution to microradians/LSB for LOT registers */
@@ -580,10 +567,8 @@ static int ad2s1210_get_voltage_threshold(struct ad2s1210_state *st,
 	unsigned int reg_val;
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	ret = regmap_read(st->regmap, reg, &reg_val);
-	mutex_unlock(&st->lock);
-
 	if (ret < 0)
 		return ret;
 
@@ -595,15 +580,11 @@ static int ad2s1210_set_voltage_threshold(struct ad2s1210_state *st,
 					  unsigned int reg, int val)
 {
 	unsigned int reg_val;
-	int ret;
 
 	reg_val = val / THRESHOLD_MILLIVOLT_PER_LSB;
 
-	mutex_lock(&st->lock);
-	ret = regmap_write(st->regmap, reg, reg_val);
-	mutex_unlock(&st->lock);
-
-	return ret;
+	guard(mutex)(&st->lock);
+	return regmap_write(st->regmap, reg, reg_val);
 }
 
 static int ad2s1210_get_lot_high_threshold(struct ad2s1210_state *st,
@@ -612,10 +593,8 @@ static int ad2s1210_get_lot_high_threshold(struct ad2s1210_state *st,
 	unsigned int reg_val;
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	ret = regmap_read(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, &reg_val);
-	mutex_unlock(&st->lock);
-
 	if (ret < 0)
 		return ret;
 
@@ -634,18 +613,18 @@ static int ad2s1210_set_lot_high_threshold(struct ad2s1210_state *st,
 	if (val != 0)
 		return -EINVAL;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	/*
 	 * We need to read both high and low registers first so we can preserve
 	 * the hysteresis.
 	 */
 	ret = regmap_read(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, &high_reg_val);
 	if (ret < 0)
-		goto error_ret;
+		return ret;
 
 	ret = regmap_read(st->regmap, AD2S1210_REG_LOT_LOW_THRD, &low_reg_val);
 	if (ret < 0)
-		goto error_ret;
+		return ret;
 
 	hysteresis = high_reg_val - low_reg_val;
 	high_reg_val = val2 / ad2s1210_lot_threshold_urad_per_lsb[st->resolution];
@@ -653,14 +632,9 @@ static int ad2s1210_set_lot_high_threshold(struct ad2s1210_state *st,
 
 	ret = regmap_write(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, high_reg_val);
 	if (ret < 0)
-		goto error_ret;
-
-	ret = regmap_write(st->regmap, AD2S1210_REG_LOT_LOW_THRD, low_reg_val);
-
-error_ret:
-	mutex_unlock(&st->lock);
+		return ret;
 
-	return ret;
+	return regmap_write(st->regmap, AD2S1210_REG_LOT_LOW_THRD, low_reg_val);
 }
 
 static int ad2s1210_get_lot_low_threshold(struct ad2s1210_state *st,
@@ -669,16 +643,13 @@ static int ad2s1210_get_lot_low_threshold(struct ad2s1210_state *st,
 	unsigned int high_reg_val, low_reg_val;
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
+
 	ret = regmap_read(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, &high_reg_val);
 	if (ret < 0)
-		goto error_ret;
+		return ret;
 
 	ret = regmap_read(st->regmap, AD2S1210_REG_LOT_LOW_THRD, &low_reg_val);
-
-error_ret:
-	mutex_unlock(&st->lock);
-
 	if (ret < 0)
 		return ret;
 
@@ -701,18 +672,14 @@ static int ad2s1210_set_lot_low_threshold(struct ad2s1210_state *st,
 
 	hysteresis = val2 / ad2s1210_lot_threshold_urad_per_lsb[st->resolution];
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
+
 	ret = regmap_read(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, &reg_val);
 	if (ret < 0)
-		goto error_ret;
+		return ret;
 
-	ret = regmap_write(st->regmap, AD2S1210_REG_LOT_LOW_THRD,
+	return regmap_write(st->regmap, AD2S1210_REG_LOT_LOW_THRD,
 			   reg_val - hysteresis);
-
-error_ret:
-	mutex_unlock(&st->lock);
-
-	return ret;
 }
 
 static int ad2s1210_get_excitation_frequency(struct ad2s1210_state *st, int *val)
@@ -720,31 +687,23 @@ static int ad2s1210_get_excitation_frequency(struct ad2s1210_state *st, int *val
 	unsigned int reg_val;
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
+
 	ret = regmap_read(st->regmap, AD2S1210_REG_EXCIT_FREQ, &reg_val);
 	if (ret < 0)
-		goto error_ret;
+		return ret;
 
 	*val = reg_val * st->clkin_hz / (1 << 15);
-	ret = IIO_VAL_INT;
-
-error_ret:
-	mutex_unlock(&st->lock);
-	return ret;
+	return IIO_VAL_INT;
 }
 
 static int ad2s1210_set_excitation_frequency(struct ad2s1210_state *st, int val)
 {
-	int ret;
-
 	if (val < AD2S1210_MIN_EXCIT || val > AD2S1210_MAX_EXCIT)
 		return -EINVAL;
 
-	mutex_lock(&st->lock);
-	ret = ad2s1210_reinit_excitation_frequency(st, val);
-	mutex_unlock(&st->lock);
-
-	return ret;
+	guard(mutex)(&st->lock);
+	return ad2s1210_reinit_excitation_frequency(st, val);
 }
 
 static const int ad2s1210_velocity_scale[] = {
@@ -1020,10 +979,8 @@ static ssize_t event_attr_voltage_reg_show(struct device *dev,
 	unsigned int value;
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	ret = regmap_read(st->regmap, iattr->address, &value);
-	mutex_unlock(&st->lock);
-
 	if (ret < 0)
 		return ret;
 
@@ -1043,11 +1000,9 @@ static ssize_t event_attr_voltage_reg_store(struct device *dev,
 	if (ret)
 		return -EINVAL;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	ret = regmap_write(st->regmap, iattr->address,
 			   data / THRESHOLD_MILLIVOLT_PER_LSB);
-	mutex_unlock(&st->lock);
-
 	if (ret < 0)
 		return ret;
 
@@ -1121,7 +1076,7 @@ static int ad2s1210_initial(struct ad2s1210_state *st)
 	unsigned int data;
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	/* Use default config register value plus resolution from devicetree. */
 	data = FIELD_PREP(AD2S1210_PHASE_LOCK_RANGE_44, 1);
@@ -1131,13 +1086,9 @@ static int ad2s1210_initial(struct ad2s1210_state *st)
 
 	ret = regmap_write(st->regmap, AD2S1210_REG_CONTROL, data);
 	if (ret < 0)
-		goto error_ret;
-
-	ret = ad2s1210_reinit_excitation_frequency(st, AD2S1210_DEF_EXCIT);
+		return ret;
 
-error_ret:
-	mutex_unlock(&st->lock);
-	return ret;
+	return ad2s1210_reinit_excitation_frequency(st, AD2S1210_DEF_EXCIT);
 }
 
 static int ad2s1210_read_label(struct iio_dev *indio_dev,
@@ -1281,18 +1232,13 @@ static int ad2s1210_debugfs_reg_access(struct iio_dev *indio_dev,
 				       unsigned int *readval)
 {
 	struct ad2s1210_state *st = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	if (readval)
-		ret = regmap_read(st->regmap, reg, readval);
-	else
-		ret = regmap_write(st->regmap, reg, writeval);
-
-	mutex_unlock(&st->lock);
+		return regmap_read(st->regmap, reg, readval);
 
-	return ret;
+	return regmap_write(st->regmap, reg, writeval);
 }
 
 static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)
@@ -1303,7 +1249,7 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)
 	size_t chan = 0;
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	memset(&st->scan, 0, sizeof(st->scan));
 	ad2s1210_toggle_sample_line(st);
@@ -1336,7 +1282,6 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)
 	iio_push_to_buffers_with_timestamp(indio_dev, &st->scan, pf->timestamp);
 
 error_ret:
-	mutex_unlock(&st->lock);
 	iio_trigger_notify_done(indio_dev->trig);
 
 	return IRQ_HANDLED;
Signature · 1 line
2.42.0
maintainer Applied
#6 lore raw

Re: [PATCH v5 3/4] staging: iio: resolver: ad2s1210: simplify code with guard(mutex)

Quoted 4 lines · On Tue, 10 Oct 2023 16:12:35 -0500 David Lechner <[email protected]> wrote:

We can simplify the code and get rid of most of the gotos by using guard(mutex) from cleanup.h.

Applied.

author reviewer
#7 lore raw

PATCH v5 4/4 iio: resolver: ad2s1210: move out of staging

In reply to David Lechner

This moves the ad2s1210 resolver driver out of staging. The driver has been fixed up and is ready to graduate.

---

v5 changes: New patch in v5.

Diff was made with file rename detection turned off so we can see the full driver code for one last check through. sysfs-bus-iio-resolver-ad2s1210 and ad2s1210.c are just moved (no changes).

 .../testing/sysfs-bus-iio-resolver-ad2s1210   |   27 +
 drivers/iio/resolver/Kconfig                  |   13 +
 drivers/iio/resolver/Makefile                 |    1 +
 drivers/iio/resolver/ad2s1210.c               | 1522 +++++++++++++++++
 .../sysfs-bus-iio-resolver-ad2s1210           |   27 -
 drivers/staging/iio/Kconfig                   |    1 -
 drivers/staging/iio/Makefile                  |    1 -
 drivers/staging/iio/resolver/Kconfig          |   19 -
 drivers/staging/iio/resolver/Makefile         |    6 -
 drivers/staging/iio/resolver/ad2s1210.c       | 1522 -----------------
 10 files changed, 1563 insertions(+), 1576 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-resolver-ad2s1210
 create mode 100644 drivers/iio/resolver/ad2s1210.c
 delete mode 100644 drivers/staging/iio/Documentation/sysfs-bus-iio-resolver-ad2s1210
 delete mode 100644 drivers/staging/iio/resolver/Kconfig
 delete mode 100644 drivers/staging/iio/resolver/Makefile
 delete mode 100644 drivers/staging/iio/resolver/ad2s1210.c
Documentation/ABI/testing/sysfs-bus-iio-resolver-ad2s1210 new file +27 -0
new file mode 100644
index 000000000000..f92c79342b93
@@ -0,0 +1,27 @@
+What:		/sys/bus/iio/devices/iio:deviceX/events/in_altvoltage0_mag_rising_reset_max
+KernelVersion:  6.7
+Contact:	[email protected]
+Description:
+		Reading returns the current Degradation of Signal Reset Maximum
+		Threshold value in millivolts. Writing sets the value.
+
+What:		/sys/bus/iio/devices/iio:deviceX/events/in_altvoltage0_mag_rising_reset_max_available
+KernelVersion:  6.7
+Contact:	[email protected]
+Description:
+		Reading returns the allowable voltage range for
+		in_altvoltage0_mag_rising_reset_max.
+
+What:		/sys/bus/iio/devices/iio:deviceX/events/in_altvoltage0_mag_rising_reset_min
+KernelVersion:  6.7
+Contact:	[email protected]
+Description:
+		Reading returns the current Degradation of Signal Reset Minimum
+		Threshold value in millivolts. Writing sets the value.
+
+What:		/sys/bus/iio/devices/iio:deviceX/events/in_altvoltage0_mag_rising_reset_min_available
+KernelVersion:  6.7
+Contact:	[email protected]
+Description:
+		Reading returns the allowable voltage range for
+		in_altvoltage0_mag_rising_reset_min.
drivers/iio/resolver/Kconfig +13 -0
index 47dbfead9b31..424529d36080 100644
@@ -25,4 +25,17 @@ config AD2S1200
 
 	  To compile this driver as a module, choose M here: the
 	  module will be called ad2s1200.
+
+config AD2S1210
+	tristate "Analog Devices ad2s1210 driver"
+	depends on SPI
+	depends on COMMON_CLK
+	depends on GPIOLIB || COMPILE_TEST
+	help
+	  Say yes here to build support for Analog Devices spi resolver
+	  to digital converters, ad2s1210, provides direct access via sysfs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad2s1210.
+
 endmenu
drivers/iio/resolver/Makefile +1 -0
index fa558138ce45..7f6c876c35ae 100644
@@ -5,3 +5,4 @@
 
 obj-$(CONFIG_AD2S90) += ad2s90.o
 obj-$(CONFIG_AD2S1200) += ad2s1200.o
+obj-$(CONFIG_AD2S1210) += ad2s1210.o
drivers/iio/resolver/ad2s1210.c new file +1522 -0

1,522 lines not on this page · raw message

drivers/staging/iio/Documentation/sysfs-bus-iio-resolver-ad2s1210 deleted +0 -27
deleted file mode 100644
index f92c79342b93..000000000000
@@ -1,27 +0,0 @@
-What:		/sys/bus/iio/devices/iio:deviceX/events/in_altvoltage0_mag_rising_reset_max
-KernelVersion:  6.7
-Contact:	[email protected]
-Description:
-		Reading returns the current Degradation of Signal Reset Maximum
-		Threshold value in millivolts. Writing sets the value.
-
-What:		/sys/bus/iio/devices/iio:deviceX/events/in_altvoltage0_mag_rising_reset_max_available
-KernelVersion:  6.7
-Contact:	[email protected]
-Description:
-		Reading returns the allowable voltage range for
-		in_altvoltage0_mag_rising_reset_max.
-
-What:		/sys/bus/iio/devices/iio:deviceX/events/in_altvoltage0_mag_rising_reset_min
-KernelVersion:  6.7
-Contact:	[email protected]
-Description:
-		Reading returns the current Degradation of Signal Reset Minimum
-		Threshold value in millivolts. Writing sets the value.
-
-What:		/sys/bus/iio/devices/iio:deviceX/events/in_altvoltage0_mag_rising_reset_min_available
-KernelVersion:  6.7
-Contact:	[email protected]
-Description:
-		Reading returns the allowable voltage range for
-		in_altvoltage0_mag_rising_reset_min.
drivers/staging/iio/Kconfig +0 -1
index d3968fe2ebb8..a60631c1f449 100644
@@ -10,6 +10,5 @@ source "drivers/staging/iio/adc/Kconfig"
 source "drivers/staging/iio/addac/Kconfig"
 source "drivers/staging/iio/frequency/Kconfig"
 source "drivers/staging/iio/impedance-analyzer/Kconfig"
-source "drivers/staging/iio/resolver/Kconfig"
 
 endmenu
drivers/staging/iio/Makefile +0 -1
index c50f1019f829..628583535393 100644
@@ -8,4 +8,3 @@ obj-y += adc/
 obj-y += addac/
 obj-y += frequency/
 obj-y += impedance-analyzer/
-obj-y += resolver/
drivers/staging/iio/resolver/Kconfig deleted +0 -19
deleted file mode 100644
index bebb35822c9e..000000000000
@@ -1,19 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0
-#
-# Resolver/Synchro drivers
-#
-menu "Resolver to digital converters"
-
-config AD2S1210
-	tristate "Analog Devices ad2s1210 driver"
-	depends on SPI
-	depends on COMMON_CLK
-	depends on GPIOLIB || COMPILE_TEST
-	help
-	  Say yes here to build support for Analog Devices spi resolver
-	  to digital converters, ad2s1210, provides direct access via sysfs.
-
-	  To compile this driver as a module, choose M here: the
-	  module will be called ad2s1210.
-
-endmenu
drivers/staging/iio/resolver/Makefile deleted +0 -6
deleted file mode 100644
index 398631f7e79b..000000000000
@@ -1,6 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0
-#
-# Makefile for Resolver/Synchro drivers
-#
-
-obj-$(CONFIG_AD2S1210) += ad2s1210.o
drivers/staging/iio/resolver/ad2s1210.c deleted +0 -1522

1,368 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.42.0

Part of the diff is not shown here. View the raw message.

maintainer
#8 lore raw

Re: [PATCH v5 4/4] iio: resolver: ad2s1210: move out of staging

Quoted 11 lines · On Tue, 10 Oct 2023 16:12:36 -0500 David Lechner <[email protected]> wrote:

This moves the ad2s1210 resolver driver out of staging. The driver has been fixed up and is ready to graduate.

---

v5 changes: New patch in v5.

Diff was made with file rename detection turned off so we can see the full driver code for one last check through. sysfs-bus-iio-resolver-ad2s1210 and ad2s1210.c are just moved (no changes).

Excellent. Great work btw - this looks really nice now.

I did a final scan through (most of the code was familiar for some reason :) I've called out a few things that I'd like to see tidied up in follow up patches but definitely nothing that prevents us making the move out of staging.

If you are happy to do a patch dropping of_match_ptr() then great, if not I'll send one out at somepoint soon. I didn't want to just sneak it in here as an edit whilst applying because I like the clean copy of identical code in these move patches.

If anyone else wants to review, unless they see anything critical / ABI changing I'd like any follow on work that comes up to be done as patches on a normal / non staging driver.

Removing another directory in staging/iio is great as well :)

Applied to the togreg branch of iio.git, but pushed out initially as testing to let 0-day take a quick look.

Thanks

Jonathan

Quoted 26 lines
 .../testing/sysfs-bus-iio-resolver-ad2s1210   |   27 +
 drivers/iio/resolver/Kconfig                  |   13 +
 drivers/iio/resolver/Makefile                 |    1 +
 drivers/iio/resolver/ad2s1210.c               | 1522 +++++++++++++++++
 .../sysfs-bus-iio-resolver-ad2s1210           |   27 -
 drivers/staging/iio/Kconfig                   |    1 -
 drivers/staging/iio/Makefile                  |    1 -
 drivers/staging/iio/resolver/Kconfig          |   19 -
 drivers/staging/iio/resolver/Makefile         |    6 -
 drivers/staging/iio/resolver/ad2s1210.c       | 1522 -----------------
 10 files changed, 1563 insertions(+), 1576 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-resolver-ad2s1210
 create mode 100644 drivers/iio/resolver/ad2s1210.c
 delete mode 100644 drivers/staging/iio/Documentation/sysfs-bus-iio-resolver-ad2s1210
 delete mode 100644 drivers/staging/iio/resolver/Kconfig
 delete mode 100644 drivers/staging/iio/resolver/Makefile
 delete mode 100644 drivers/staging/iio/resolver/ad2s1210.c
drivers/iio/resolver/ad2s1210.c new file +0 -0
new file mode 100644
index 000000000000..bd4a90c222b5

drivers/iio/resolver/ad2s1210.c line 72

Quoted 4 lines
drivers/iio/resolver/ad2s1210.c new file +3 -0
@@ -0,0 +1,1522 @@
 
+
+#define DRV_NAME "ad2s1210"
+

Nice to tidy up:
This is a pet hate of mine. Why have a define for DRV_NAME that is only
used in one or two places? That's where we will go to find out what it is called, so define just makes that take one more step!

drivers/iio/resolver/ad2s1210.c line 301

Quoted 12 lines
drivers/iio/resolver/ad2s1210.c new file +12 -0
+/*
+ * Toggles the SAMPLE line on the AD2S1210 to latch in the current position,
+ * velocity, and faults.
+ *
+ * Must be called with lock held.
+ */
+static void ad2s1210_toggle_sample_line(struct ad2s1210_state *st)
+{
+	/*
+	 * Datasheet specifies minimum hold time t16 = 2 * tck + 20 ns. So the
+	 * longest time needed is when CLKIN is 6.144 MHz, in which case t16
+	 * ~= 350 ns. The same delay is also needed before re-asserting the

Potential issue in long term but wait and see:
Hmm. The about equal makes me a little nervous long term. Far too much history of parts being produced that don't quite meet the documented timing. Still can fix it if we see any problems.

Quoted 4 lines
drivers/iio/resolver/ad2s1210.c new file +4 -0
+	 * SAMPLE line.
+	 */
+	gpiod_set_value(st->sample_gpio, 1);
+	ndelay(350);

drivers/iio/resolver/ad2s1210.c

Quoted 10 lines
drivers/iio/resolver/ad2s1210.c new file +8 -0
+	gpiod_set_value(st->sample_gpio, 0);
+	ndelay(350);
+}
 
 
+
+static struct spi_driver ad2s1210_driver = {
+	.driver = {
+		.name = DRV_NAME,
+		.of_match_table = of_match_ptr(ad2s1210_of_match),

Fix this in a follow up patch:

Don't guard a of_match_table with of_match_ptr(). All that does is prevent any chance of using another firmware (ACPI PRP0001 for example) that uses this table to match.

Quoted 5 lines
drivers/iio/resolver/ad2s1210.c new file +5 -0
+	},
+	.probe = ad2s1210_probe,
+	.id_table = ad2s1210_id,
+};
+module_spi_driver(ad2s1210_driver);
author reviewer
#9 lore raw

Re: [PATCH v5 4/4] iio: resolver: ad2s1210: move out of staging

Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Tue, 10 Oct 2023 16:12:36 -0500 David Lechner <[email protected]> wrote:

This moves the ad2s1210 resolver driver out of staging. The driver has been fixed up and is ready to graduate.

---

v5 changes: New patch in v5.

Quoted 7 lines
Quoted 3 lines

Diff was made with file rename detection turned off so we can see the full driver code for one last check through. sysfs-bus-iio-resolver-ad2s1210 and ad2s1210.c are just moved (no changes).

Excellent. Great work btw - this looks really nice now.

I did a final scan through (most of the code was familiar for some reason :)

Thanks for all of the reviews. There were less than 200 lines left (mostly blank lines and boilerplate) that didn't get touched!