Skip to contentSkip to conversation

Fix and refactor output disable logic

PATCH Superseded Gabriel Shahrouzi 2 participants 7 messages Apr 20, 2025 – Apr 22, 2025

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fix and refactor output disable logic

Patch 1 includes the initial fix.

Patch 2 refactors the code to use the out_altvoltage_powerdown ABI.

Patch 3 adds small improvements by minimizing the size of types and doing a redundancy check.

Not sure whether to include a read function for powerdown as well since all the other attributes only had write permissions. I can also do this for the other attributes to help modernize the driver.

Gabriel Shahrouzi (3):
iio: frequency: Use SLEEP bit instead of RESET to disable output
staging: iio: ad9832: Refactor powerdown control
staging: iio: ad9832: Add minor improvements to ad9832_write_powerdown

 drivers/staging/iio/frequency/ad9832.c | 50 ++++++++++++++++++--------
 1 file changed, 36 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 iio: frequency: Use SLEEP bit instead of RESET to disable output

According to the AD9832 datasheet (Table 10, D12 description), setting the RESET bit forces the phase accumulator to zero, which corresponds to a full-scale DC output, rather than disabling the output signal.

The correct way to disable the output and enter a low-power state is to set the AD9832_SLEEP bit (Table 10, D13 description), which powers down the internal DAC current sources and disables internal clocks.

Fixes ea707584bac1 ("Staging: IIO: DDS: AD9832 / AD9835 driver")
---
 drivers/staging/iio/frequency/ad9832.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/frequency/ad9832.c +1 -1
index db42810c7664b..0872ff4ec4896 100644
@@ -232,7 +232,7 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 			st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP |
 					AD9832_CLR);
 		else
-			st->ctrl_src |= AD9832_RESET;
+			st->ctrl_src |= AD9832_SLEEP;
 
 		st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
 					st->ctrl_src);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/3 staging: iio: ad9832: Refactor powerdown control

In reply to Gabriel Shahrouzi

Replace custom implementation with out_altvoltage_powerdown ABI. The attribute's logic is inverted (1 now enables powerdown) to match the standard. Modernize driver by using the standard IIO interface.

---
 drivers/staging/iio/frequency/ad9832.c | 44 ++++++++++++++++++--------
 1 file changed, 30 insertions(+), 14 deletions(-)
drivers/staging/iio/frequency/ad9832.c +30 -14
index 0872ff4ec4896..a8fc20379efed 100644
@@ -167,6 +167,34 @@ static int ad9832_write_phase(struct ad9832_state *st,
 	return spi_sync(st->spi, &st->phase_msg);
 }
 
+static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribute *attr,
+				      const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct ad9832_state *st = iio_priv(indio_dev);
+	int ret;
+	unsigned long val;
+
+	ret = kstrtoul(buf, 10, &val);
+	if (ret)
+		goto error_ret;
+
+	mutex_lock(&st->lock);
+	if (val)
+		st->ctrl_src |= AD9832_SLEEP;
+	else
+		st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP |
+				 AD9832_CLR);
+
+	st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
+				st->ctrl_src);
+	ret = spi_sync(st->spi, &st->msg);
+	mutex_unlock(&st->lock);
+
+error_ret:
+	return ret ? ret : len;
+}
+
 static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 			    const char *buf, size_t len)
 {
@@ -227,17 +255,6 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 					st->ctrl_fp);
 		ret = spi_sync(st->spi, &st->msg);
 		break;
-	case AD9832_OUTPUT_EN:
-		if (val)
-			st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP |
-					AD9832_CLR);
-		else
-			st->ctrl_src |= AD9832_SLEEP;
-
-		st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
-					st->ctrl_src);
-		ret = spi_sync(st->spi, &st->msg);
-		break;
 	default:
 		ret = -ENODEV;
 	}
@@ -266,8 +283,7 @@ static IIO_CONST_ATTR_PHASE_SCALE(0, "0.0015339808"); /* 2PI/2^12 rad*/
 
 static IIO_DEV_ATTR_PINCONTROL_EN(0, 0200, NULL,
 				ad9832_write, AD9832_PINCTRL_EN);
-static IIO_DEV_ATTR_OUT_ENABLE(0, 0200, NULL,
-				ad9832_write, AD9832_OUTPUT_EN);
+static IIO_DEVICE_ATTR(out_altvoltage_powerdown, 0200, NULL, ad9832_write_powerdown, 0);
 
 static struct attribute *ad9832_attributes[] = {
 	&iio_dev_attr_out_altvoltage0_frequency0.dev_attr.attr,
@@ -281,7 +297,7 @@ static struct attribute *ad9832_attributes[] = {
 	&iio_dev_attr_out_altvoltage0_pincontrol_en.dev_attr.attr,
 	&iio_dev_attr_out_altvoltage0_frequencysymbol.dev_attr.attr,
 	&iio_dev_attr_out_altvoltage0_phasesymbol.dev_attr.attr,
-	&iio_dev_attr_out_altvoltage0_out_enable.dev_attr.attr,
+	&iio_dev_attr_out_altvoltage_powerdown.dev_attr.attr,
 	NULL,
 };
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/3 staging: iio: ad9832: Add minor improvements to ad9832_write_powerdown

In reply to Gabriel Shahrouzi

Minimize size of type that needs to be used by replacing unsigned long with bool. Avoid redundancy by checking if cached power state is the same as the one requested.

---
 drivers/staging/iio/frequency/ad9832.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/staging/iio/frequency/ad9832.c +12 -6
index a8fc20379efed..2ab6026d56b5c 100644
@@ -173,13 +173,19 @@ static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad9832_state *st = iio_priv(indio_dev);
 	int ret;
-	unsigned long val;
+	bool val;
+	bool cur;
 
-	ret = kstrtoul(buf, 10, &val);
+	ret = kstrtobool(buf, &val);
 	if (ret)
-		goto error_ret;
+		return ret;
 
 	mutex_lock(&st->lock);
+
+	cur = !!(st->ctrl_src & AD9832_SLEEP);
+	if (val == cur)
+		goto unlock;
+
 	if (val)
 		st->ctrl_src |= AD9832_SLEEP;
 	else
@@ -189,10 +195,10 @@ static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut
 	st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
 				st->ctrl_src);
 	ret = spi_sync(st->spi, &st->msg);
-	mutex_unlock(&st->lock);
 
-error_ret:
-	return ret ? ret : len;
+unlock:
+	mutex_unlock(&st->lock);
+	return len;
 }
 
 static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2 3/3] staging: iio: ad9832: Add minor improvements to ad9832_write_powerdown

Quoted 16 lines · Gabriel Shahrouzi

Minimize size of type that needs to be used by replacing unsigned long with bool. Avoid redundancy by checking if cached power state is the same as the one requested.

---
 drivers/staging/iio/frequency/ad9832.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/staging/iio/frequency/ad9832.c +0 -0
index a8fc20379efed..2ab6026d56b5c 100644
@@ -173,13 +173,19 @@ static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct ad9832_state *st = iio_priv(indio_dev);

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut · line 181

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +4 -3
 	int ret;
-	unsigned long val;
+	bool val;
+	bool cur;
 
-	ret = kstrtoul(buf, 10, &val);
+	ret = kstrtobool(buf, &val);
 	if (ret)
-		goto error_ret;
+		return ret;

Fold this whole thing into patch 2. Don't write something and then fix it in the next patch.

Quoted 11 lines
drivers/staging/iio/frequency/ad9832.c +5 -0
 	mutex_lock(&st->lock);
+
+	cur = !!(st->ctrl_src & AD9832_SLEEP);
+	if (val == cur)
+		goto unlock;
+
 	if (val)
 		st->ctrl_src |= AD9832_SLEEP;
 	else

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut · line 201

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +3 -3
@@ -189,10 +195,10 @@ static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut
 	st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
 				st->ctrl_src);
 	ret = spi_sync(st->spi, &st->msg);
-	mutex_unlock(&st->lock);
 
-error_ret:
-	return ret ? ret : len;
+unlock:
+	mutex_unlock(&st->lock);
+	return len;

This should still be:

	return ret ? ret : len;

regards,
dan carpenter

#5 lore raw

Re: [PATCH v2 3/3] staging: iio: ad9832: Add minor improvements to ad9832_write_powerdown

Quoted 56 lines · Dan Carpenter
Quoted 16 lines · Gabriel Shahrouzi

Minimize size of type that needs to be used by replacing unsigned long with bool. Avoid redundancy by checking if cached power state is the same as the one requested.

---
 drivers/staging/iio/frequency/ad9832.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/staging/iio/frequency/ad9832.c +0 -0
index a8fc20379efed..2ab6026d56b5c 100644
@@ -173,13 +173,19 @@ static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut
      struct iio_dev *indio_dev = dev_to_iio_dev(dev);
      struct ad9832_state *st = iio_priv(indio_dev);

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut · line 181

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +4 -3
      int ret;
-     unsigned long val;
+     bool val;
+     bool cur;
 
-     ret = kstrtoul(buf, 10, &val);
+     ret = kstrtobool(buf, &val);
      if (ret)
-             goto error_ret;
+             return ret;

Fold this whole thing into patch 2. Don't write something and then fix it in the next patch.

Quoted 11 lines
drivers/staging/iio/frequency/ad9832.c +5 -0
      mutex_lock(&st->lock);
+
+     cur = !!(st->ctrl_src & AD9832_SLEEP);
+     if (val == cur)
+             goto unlock;
+
      if (val)
              st->ctrl_src |= AD9832_SLEEP;
      else

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut · line 201

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +3 -3
@@ -189,10 +195,10 @@ static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut
      st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
                              st->ctrl_src);
      ret = spi_sync(st->spi, &st->msg);
-     mutex_unlock(&st->lock);
 
-error_ret:
-     return ret ? ret : len;
+unlock:
+     mutex_unlock(&st->lock);
+     return len;

This should still be:

Quoted 1 line
        return ret ? ret : len;

Ah right, the return from the spi_sync. Thanks.

Quoted 4 lines

regards,
dan carpenter

#6 lore raw

Re: [PATCH v2 3/3] staging: iio: ad9832: Add minor improvements to ad9832_write_powerdown

In reply to Dan Carpenter
Quoted 23 lines · Dan Carpenter
Quoted 21 lines · Gabriel Shahrouzi

Minimize size of type that needs to be used by replacing unsigned long with bool. Avoid redundancy by checking if cached power state is the same as the one requested.

---
 drivers/staging/iio/frequency/ad9832.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/staging/iio/frequency/ad9832.c +2 -1
index a8fc20379efed..2ab6026d56b5c 100644
@@ -173,13 +173,19 @@ static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut
      struct iio_dev *indio_dev = dev_to_iio_dev(dev);
      struct ad9832_state *st = iio_priv(indio_dev);
      int ret;
-     unsigned long val;
+     bool val;
+     bool cur;
 
Quoted 8 lines

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut · line 181

Quoted 5 lines
drivers/staging/iio/frequency/ad9832.c +2 -2
-     ret = kstrtoul(buf, 10, &val);
+     ret = kstrtobool(buf, &val);
      if (ret)
-             goto error_ret;
+             return ret;

Fold this whole thing into patch 2. Don't write something and then fix it in the next patch.

Ah, I forgot to respond to this part — makes sense. I was initially trying to mirror the original author’s style and build on top of it, but I see now that all the changes should be folded into a single patch.

Quoted 30 lines
Quoted 12 lines
drivers/staging/iio/frequency/ad9832.c +5 -0
      mutex_lock(&st->lock);
+
+     cur = !!(st->ctrl_src & AD9832_SLEEP);
+     if (val == cur)
+             goto unlock;
+
      if (val)
              st->ctrl_src |= AD9832_SLEEP;
      else

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut · line 201

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +3 -3
@@ -189,10 +195,10 @@ static ssize_t ad9832_write_powerdown(struct device *dev, struct device_attribut
      st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
                              st->ctrl_src);
      ret = spi_sync(st->spi, &st->msg);
-     mutex_unlock(&st->lock);
 
-error_ret:
-     return ret ? ret : len;
+unlock:
+     mutex_unlock(&st->lock);
+     return len;

This should still be:

        return ret ? ret : len;

regards,
dan carpenter