Skip to contentSkip to conversation

staging: iio: frequency: ad9832: Fix and

PATCH In review Gabriel Shahrouzi 3 participants 7 messages Apr 24, 2025 – Apr 25, 2025

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

v4 cover staging: iio: frequency: ad9832: Fix and

Patch 1 includes the initial fix.

Patch 2 refactors the code to use the out_altvoltage_powerdown ABI via an extended attribute.

Gabriel Shahrouzi (2):
iio: frequency: Use SLEEP bit instead of RESET to disable output
staging: iio: ad9832: Refactor powerdown control

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

PATCH v4 1/2 staging: iio: frequency: ad9832: 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")

---
v3 -> v4:
- Rebase changes ontop of most recent changes.
v2 -> v3:
v1 -> v2:

---
 drivers/staging/iio/frequency/ad9832.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/frequency/ad9832.c +1 -1
index 49388da5a684a..2e555084ff98a 100644
@@ -236,7 +236,7 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 		if (val)
 			st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP | AD9832_CLR);
 		else
-			st->ctrl_src |= FIELD_PREP(AD9832_RESET, 1);
+			st->ctrl_src |= FIELD_PREP(AD9832_SLEEP, 1);
 
 		st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) |
 						  st->ctrl_src);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v4 1/2] staging: iio: frequency: ad9832: Use SLEEP bit instead of RESET to disable output

Quoted 8 lines · Gabriel Shahrouzi

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.

...

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 239

Quoted 5 lines
                if (val)
                        st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP | AD9832_CLR);
                else
drivers/staging/iio/frequency/ad9832.c +1 -1
-                       st->ctrl_src |= FIELD_PREP(AD9832_RESET, 1);
+                       st->ctrl_src |= FIELD_PREP(AD9832_SLEEP, 1);

From the code perspective this allows combinations of the bits to be set. So, what does the datasheet say about SLEEP+RESET, or RESET+CLR, or other combinations of these 3 bits?

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

PATCH v4 2/2 staging: iio: frequency: 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.

---
V3 -> v4:
- Use guard(mutex) for simplified locking.
- Use an extended attribute.
v2 -> v3:
v1 -> v2:
Refactor powerdown functionality.

---
 drivers/staging/iio/frequency/ad9832.c | 69 +++++++++++++++++++-------
 1 file changed, 51 insertions(+), 18 deletions(-)
drivers/staging/iio/frequency/ad9832.c +51 -18
index 2e555084ff98a..b8b52302abf36 100644
@@ -174,6 +174,32 @@ static int ad9832_write_phase(struct ad9832_state *st,
 	return spi_sync(st->spi, &st->phase_msg);
 }
 
+static ssize_t ad9832_write_powerdown(struct iio_dev *indio_dev, uintptr_t private,
+				      const struct iio_chan_spec *chan,
+				      const char *buf, size_t len)
+{
+	struct ad9832_state *st = iio_priv(indio_dev);
+	int ret;
+	bool val;
+
+	ret = kstrtobool(buf, &val);
+	if (ret)
+		return ret;
+
+	guard(mutex)(&st->lock);
+	if (val)
+		st->ctrl_src |= AD9832_SLEEP;
+	else
+		st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP |
+				 AD9832_CLR);
+
+	st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) |
+					  st->ctrl_src);
+	ret = spi_sync(st->spi, &st->msg);
+
+	return ret ? ret : len;
+}
+
 static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 			    const char *buf, size_t len)
 {
@@ -185,9 +211,9 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 
 	ret = kstrtoul(buf, 10, &val);
 	if (ret)
-		goto error_ret;
+		return ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	switch ((u32)this_attr->address) {
 	case AD9832_FREQ0HM:
 	case AD9832_FREQ1HM:
@@ -232,22 +258,9 @@ 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 |= FIELD_PREP(AD9832_SLEEP, 1);
-
-		st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) |
-						  st->ctrl_src);
-		ret = spi_sync(st->spi, &st->msg);
-		break;
 	default:
 		ret = -ENODEV;
 	}
-	mutex_unlock(&st->lock);
-
-error_ret:
 	return ret ? ret : len;
 }
 
@@ -270,8 +283,6 @@ 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 struct attribute *ad9832_attributes[] = {
 	&iio_dev_attr_out_altvoltage0_frequency0.dev_attr.attr,
@@ -285,7 +296,6 @@ 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,
 	NULL,
 };
 
@@ -293,6 +303,27 @@ static const struct attribute_group ad9832_attribute_group = {
 	.attrs = ad9832_attributes,
 };
 
+static const struct iio_chan_spec_ext_info ad9832_ext_info[] = {
+	{
+		.name = "powerdown",
+		.shared = IIO_SEPARATE,
+		.write = ad9832_write_powerdown,
+	},
+	{ },
+};
+
+#define AD9832_CHANNEL(chan) {					\
+	.type = IIO_ALTVOLTAGE,					\
+	.indexed = 1,						\
+	.output = 1,						\
+	.channel = (chan),					\
+	.ext_info = ad9832_ext_info,				\
+}
+
+static const struct iio_chan_spec ad9832_channels[] = {
+	AD9832_CHANNEL(0),
+};
+
 static const struct iio_info ad9832_info = {
 	.attrs = &ad9832_attribute_group,
 };
@@ -333,6 +364,8 @@ static int ad9832_probe(struct spi_device *spi)
 	indio_dev->name = spi_get_device_id(spi)->name;
 	indio_dev->info = &ad9832_info;
 	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = ad9832_channels;
+	indio_dev->num_channels = ARRAY_SIZE(ad9832_channels);
 
 	/* Setup default messages */
 
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v4 2/2] staging: iio: frequency: ad9832: Refactor powerdown control

Quoted 15 lines · Gabriel Shahrouzi

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

drivers/staging/iio/frequency/ad9832.c +9 -0
+static ssize_t ad9832_write_powerdown(struct iio_dev *indio_dev, uintptr_t private,
+                                     const struct iio_chan_spec *chan,
+                                     const char *buf, size_t len)
+{
+       struct ad9832_state *st = iio_priv(indio_dev);
+       int ret;
+       bool val;
+
+       ret = kstrtobool(buf, &val);

drivers/staging/iio/frequency/ad9832.c static int ad9832_write_phase(struct ad9832_state *st, · line 194

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +9 -0
+       if (ret)
+               return ret;
+
+       guard(mutex)(&st->lock);
 
+       if (val)
+               st->ctrl_src |= AD9832_SLEEP;
+       else
+               st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP |
+                                AD9832_CLR);

Why out of a sudden this went backwards in style and format (no FIELD_PREP(), two lines, ...)?

drivers/staging/iio/frequency/ad9832.c static int ad9832_write_phase(struct ad9832_state *st, · line 201

Quoted 7 lines
drivers/staging/iio/frequency/ad9832.c +7 -0
+
+       st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) |
+                                         st->ctrl_src);
+       ret = spi_sync(st->spi, &st->msg);
+
+       return ret ? ret : len;
+}

Use Elvis?

...

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 216

Quoted 2 lines
drivers/staging/iio/frequency/ad9832.c +1 -1
-       mutex_lock(&st->lock);
+       guard(mutex)(&st->lock);

This should be done in a separate patch for all locking.

...

Quoted 1 line

- st->ctrl_src |= FIELD_PREP(AD9832_SLEEP, 1);

Btw, I do not see this in v6.15-tcX, is this part of Linux Next?

...

Quoted 6 lines

&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,

NULL,

Also drop the trailing comma at some point.

...

drivers/staging/iio/frequency/ad9832.c static const struct attribute_group ad9832_attribute_group = { · line 312

Quoted 8 lines
drivers/staging/iio/frequency/ad9832.c +7 -0
+static const struct iio_chan_spec_ext_info ad9832_ext_info[] = {
+       {
+               .name = "powerdown",
+               .shared = IIO_SEPARATE,
+               .write = ad9832_write_powerdown,
+       },
 
+       { },

Ditto.

Quoted 1 line

+};

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

Re: [PATCH v4 2/2] staging: iio: frequency: ad9832: Refactor powerdown control

In reply to Gabriel Shahrouzi
Quoted 21 lines · Gabriel Shahrouzi

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

---
V3 -> v4:
- Use guard(mutex) for simplified locking.
- Use an extended attribute.
v2 -> v3:
v1 -> v2:
Refactor powerdown functionality.

---
 drivers/staging/iio/frequency/ad9832.c | 69 +++++++++++++++++++-------
 1 file changed, 51 insertions(+), 18 deletions(-)
drivers/staging/iio/frequency/ad9832.c +0 -0
index 2e555084ff98a..b8b52302abf36 100644

drivers/staging/iio/frequency/ad9832.c static int ad9832_write_phase(struct ad9832_state *st, · line 183

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +7 -0
@@ -174,6 +174,32 @@ static int ad9832_write_phase(struct ad9832_state *st,
 	return spi_sync(st->spi, &st->phase_msg);
 }
 
+static ssize_t ad9832_write_powerdown(struct iio_dev *indio_dev, uintptr_t private,
+				      const struct iio_chan_spec *chan,
+				      const char *buf, size_t len)
+{
+	struct ad9832_state *st = iio_priv(indio_dev);
+	int ret;
+	bool val;

Declare val before ret. Use reverse Christmas tree order.

	long long long_name;
	medium name;
	short name;
Quoted 23 lines
drivers/staging/iio/frequency/ad9832.c +19 -0
+
+	ret = kstrtobool(buf, &val);
+	if (ret)
+		return ret;
+
+	guard(mutex)(&st->lock);
+	if (val)
+		st->ctrl_src |= AD9832_SLEEP;
+	else
+		st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP |
+				 AD9832_CLR);
+
+	st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) |
+					  st->ctrl_src);
+	ret = spi_sync(st->spi, &st->msg);
+
+	return ret ? ret : len;
+}
+
 static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 			    const char *buf, size_t len)
 {

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 214

Quoted 5 lines
drivers/staging/iio/frequency/ad9832.c +1 -1
@@ -185,9 +211,9 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 
 	ret = kstrtoul(buf, 10, &val);
 	if (ret)
-		goto error_ret;
+		return ret;

This is unrelated. Do this in a separate patch.

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 216

Quoted 3 lines
drivers/staging/iio/frequency/ad9832.c +1 -1
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);

same.

Quoted 7 lines
drivers/staging/iio/frequency/ad9832.c +0 -0
 	switch ((u32)this_attr->address) {
 	case AD9832_FREQ0HM:
 	case AD9832_FREQ1HM:
@@ -232,22 +258,9 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
 						  st->ctrl_fp);
 		ret = spi_sync(st->spi, &st->msg);
 		break;

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 244

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +0 -10
-	case AD9832_OUTPUT_EN:
-		if (val)
-			st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP | AD9832_CLR);
-		else
-			st->ctrl_src |= FIELD_PREP(AD9832_SLEEP, 1);
-
-		st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) |
-						  st->ctrl_src);
-		ret = spi_sync(st->spi, &st->msg);
-		break;

This is related. Keep this.

drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 264

Quoted 7 lines
drivers/staging/iio/frequency/ad9832.c +0 -3
 	default:
 		ret = -ENODEV;
 	}
-	mutex_unlock(&st->lock);
-
-error_ret:
 	return ret ? ret : len;

Unrelated.

Quoted 20 lines
drivers/staging/iio/frequency/ad9832.c +0 -3
 }
 
@@ -270,8 +283,6 @@ 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 struct attribute *ad9832_attributes[] = {
 	&iio_dev_attr_out_altvoltage0_frequency0.dev_attr.attr,
@@ -285,7 +296,6 @@ 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,
 	NULL,
 };
 

drivers/staging/iio/frequency/ad9832.c static const struct attribute_group ad9832_attribute_group = { · line 312

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +7 -0
@@ -293,6 +303,27 @@ static const struct attribute_group ad9832_attribute_group = {
 	.attrs = ad9832_attributes,
 };
 
+static const struct iio_chan_spec_ext_info ad9832_ext_info[] = {
+	{
+		.name = "powerdown",
+		.shared = IIO_SEPARATE,
+		.write = ad9832_write_powerdown,
+	},
+	{ },
           ^

No comma after a sentinal.

drivers/staging/iio/frequency/ad9832.c static const struct attribute_group ad9832_attribute_group = { · line 314

Quoted 2 lines
drivers/staging/iio/frequency/ad9832.c +2 -0
+};
+

regards,
dan carpenter

#6 lore raw
In reply to Gabriel Shahrouzi
Quoted 10 lines · Gabriel Shahrouzi

Patch 1 includes the initial fix.

Patch 2 refactors the code to use the out_altvoltage_powerdown ABI via an extended attribute.

Gabriel Shahrouzi (2):
iio: frequency: Use SLEEP bit instead of RESET to disable output
staging: iio: ad9832: Refactor powerdown control

Quoted 6 lines
 drivers/staging/iio/frequency/ad9832.c | 69 +++++++++++++++++++-------
 1 file changed, 51 insertions(+), 18 deletions(-)

--
2.43.0

Whoops. Didn't realize reformatting the title on the cover letter so it fits within the width requirement would cause part of title to be omitted. Title should be: Subject: [PATCH v4 0/2] staging: iio:
frequency: ad9832: Fix and refactor output disable logic.