Skip to contentSkip to conversation

staging: iio: dds: ad9834: Enable driver support for AD9833 and AD9834 DDS devices

PATCH In review [email protected] 2 participants 6 messages Nov 29, 2010 – Nov 30, 2010

Subsystems (4)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 1/1 staging: iio: dds: ad9834: Enable driver support for AD9833 and AD9834 DDS devices

From: Michael Hennerich <[email protected]>

staging: iio: dds: ad9834: add missing include file

staging: iio: dds: ad9834: fix typo

---
 drivers/staging/iio/dds/Kconfig  |   11 +-
 drivers/staging/iio/dds/Makefile |    1 +
 drivers/staging/iio/dds/ad9834.c |  348 ++++++++++++++++++++++++++++++++++++++
 drivers/staging/iio/dds/ad9834.h |  108 ++++++++++++
 drivers/staging/iio/dds/dds.h    |   99 +++++++++++
 5 files changed, 565 insertions(+), 2 deletions(-)
 create mode 100644 drivers/staging/iio/dds/ad9834.c
 create mode 100644 drivers/staging/iio/dds/ad9834.h
 create mode 100644 drivers/staging/iio/dds/dds.h
drivers/staging/iio/dds/Kconfig +9 -2
index d045ed6..4c9cce3 100644
@@ -11,11 +11,18 @@ config AD5930
 	  ad5930/ad5932, provides direct access via sysfs.
 
 config AD9832
-	tristate "Analog Devices ad9832/3/4/5 driver"
+	tristate "Analog Devices ad9832/5 driver"
 	depends on SPI
 	help
 	  Say yes here to build support for Analog Devices DDS chip
-	  ad9832/3/4/5, provides direct access via sysfs.
+	  ad9832 and ad9835, provides direct access via sysfs.
+
+config AD9834
+	tristate "Analog Devices ad9833/4/ driver"
+	depends on SPI
+	help
+	  Say yes here to build support for Analog Devices DDS chip
+	  AD9833 and AD9834, provides direct access via sysfs.
 
 config AD9850
 	tristate "Analog Devices ad9850/1 driver"
drivers/staging/iio/dds/Makefile +1 -0
index 6f274ac..1477461 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_AD5930) += ad5930.o
 obj-$(CONFIG_AD9832) += ad9832.o
+obj-$(CONFIG_AD9834) += ad9834.o
 obj-$(CONFIG_AD9850) += ad9850.o
 obj-$(CONFIG_AD9852) += ad9852.o
 obj-$(CONFIG_AD9910) += ad9910.o
drivers/staging/iio/dds/ad9834.c new file +348 -0

348 lines not on this page · raw message

drivers/staging/iio/dds/ad9834.h new file +108 -0

108 lines not on this page · raw message

drivers/staging/iio/dds/dds.h new file +99 -0

99 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#1 lore raw
Quoted 5 lines · On 11/29/10 15:51, [email protected] wrote:

From: Michael Hennerich <[email protected]>

staging: iio: dds: ad9834: add missing include file

staging: iio: dds: ad9834: fix typo

Hi Michael,

Thanks for this driver and in particular the first thoughts on a suitable abi design for these parts. There are a few nitpicks in the driver body that will be trivial to cleanup.

The abi needs more open discussion. I have inserted a few suggestions here, but I would also like to see it formally proposed as an RFC separate from the driver to linux-iio and lkml. It's a fairly substantial new abi that we want to try and get right. Given similar postings of IIO abi's in the past the lkml posting may fall on deaf ears, but then at least you can use it to bash latecomers over the head with when they disagree with your interface 6 months down the line... It won't help but it always makes me feel better and we might get a few more people who did read it at the time coming in on our side if we are lucky :)

Just wait... Someone will want to use a nice general DDS as a clock input for some other part and hence ask about in kernel interfaces...

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/staging/iio/dds/Kconfig  |   11 +-
 drivers/staging/iio/dds/Makefile |    1 +
 drivers/staging/iio/dds/ad9834.c |  348 ++++++++++++++++++++++++++++++++++++++
 drivers/staging/iio/dds/ad9834.h |  108 ++++++++++++
 drivers/staging/iio/dds/dds.h    |   99 +++++++++++
 5 files changed, 565 insertions(+), 2 deletions(-)
 create mode 100644 drivers/staging/iio/dds/ad9834.c
 create mode 100644 drivers/staging/iio/dds/ad9834.h
 create mode 100644 drivers/staging/iio/dds/dds.h
drivers/staging/iio/dds/Kconfig +0 -0
index d045ed6..4c9cce3 100644

drivers/staging/iio/dds/Kconfig config AD5930 · line 18

Quoted 9 lines
drivers/staging/iio/dds/Kconfig +1 -2
@@ -11,11 +11,18 @@ config AD5930
 	  ad5930/ad5932, provides direct access via sysfs.
 
 config AD9832
-	tristate "Analog Devices ad9832/3/4/5 driver"
+	tristate "Analog Devices ad9832/5 driver"
 	depends on SPI
 	help
 	  Say yes here to build support for Analog Devices DDS chip
-	  ad9832/3/4/5, provides direct access via sysfs.

This is somewhat 'interesting'. Please add to commit comment to say whether this change was due to only partial support existing in the 'old' driver or if it never worked in the first place for these parts.

Quoted 112 lines
drivers/staging/iio/dds/Kconfig +8 -0
+	  ad9832 and ad9835, provides direct access via sysfs.
+
+config AD9834
+	tristate "Analog Devices ad9833/4/ driver"
+	depends on SPI
+	help
+	  Say yes here to build support for Analog Devices DDS chip
+	  AD9833 and AD9834, provides direct access via sysfs.
 
 config AD9850
 	tristate "Analog Devices ad9850/1 driver"
drivers/staging/iio/dds/Makefile +1 -0
index 6f274ac..1477461 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_AD5930) += ad5930.o
 obj-$(CONFIG_AD9832) += ad9832.o
+obj-$(CONFIG_AD9834) += ad9834.o
 obj-$(CONFIG_AD9850) += ad9850.o
 obj-$(CONFIG_AD9852) += ad9852.o
 obj-$(CONFIG_AD9910) += ad9910.o
drivers/staging/iio/dds/ad9834.c new file +83 -0
new file mode 100644
index 0000000..d17f139
@@ -0,0 +1,348 @@
+/*
+ * AD9834 SPI DAC driver
+ *
+ * Copyright 2010 Analog Devices Inc.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+
+#include <linux/interrupt.h>
+#include <linux/workqueue.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+#include <linux/list.h>
+#include <linux/spi/spi.h>
+#include <linux/regulator/consumer.h>
+#include <linux/err.h>
+#include <asm/div64.h>
+
+#include "../iio.h"
+#include "../sysfs.h"
+#include "dds.h"
+
+#include "ad9834.h"
+
+static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
+{
+	unsigned long long freqreg = (u64) fout * (u64) (1 << AD9834_FREQ_BITS);
+	do_div(freqreg, mclk);
+	return freqreg;
+}
+
+static int ad9834_write_frequency(struct ad9834_state *st,
+				  unsigned long addr, unsigned long fout)
+{
+	unsigned long regval;
+
+	if (fout > (st->mclk / 2))
+		return -EINVAL;
+
+	regval = ad9834_calc_freqreg(st->mclk, fout);
+
+	st->freq_data[0] = cpu_to_be16(addr | (regval &
+				       RES_MASK(AD9834_FREQ_BITS / 2)));
+	st->freq_data[1] = cpu_to_be16(addr | ((regval >>
+				       (AD9834_FREQ_BITS / 2)) &
+				       RES_MASK(AD9834_FREQ_BITS / 2)));
+
+	return spi_sync(st->spi, &st->freq_msg);;
+}
+
+static int ad9834_write_phase(struct ad9834_state *st,
+				  unsigned long addr, unsigned long phase)
+{
+	if (phase > (1 << AD9834_PHASE_BITS))
+		return -EINVAL;
+	st->data = cpu_to_be16(addr | phase);
+
+	return spi_sync(st->spi, &st->msg);
+}
+
+static ssize_t ad9834_write(struct device *dev,
+		struct device_attribute *attr,
+		const char *buf,
+		size_t len)
+{
+	struct iio_dev *dev_info = dev_get_drvdata(dev);
+	struct ad9834_state *st = dev_info->dev_data;
+	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	int ret;
+	long val;
+
+	ret = strict_strtol(buf, 10, &val);
+	if (ret)
+		goto error_ret;
+
+	mutex_lock(&dev_info->mlock);
+	switch (this_attr->address) {
+	case AD9834_REG_FREQ0:
+	case AD9834_REG_FREQ1:
+		ret = ad9834_write_frequency(st, this_attr->address, val);
+		break;

drivers/staging/iio/dds/ad9834.c line 93

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+	case AD9834_REG_PHASE0:
+	case AD9834_REG_PHASE1:
+		ret = ad9834_write_phase(st, this_attr->address, val);
+		break;
+	case AD9834_PIN_SW:
+	case AD9834_OPBITEN:
+		if (val)
+			st->control |= this_attr->address;
+		else
+			st->control &= ~this_attr->address;

Odd tabbing

drivers/staging/iio/dds/ad9834.c line 104

Quoted 11 lines
drivers/staging/iio/dds/ad9834.c new file +11 -0
+			st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
+		ret = spi_sync(st->spi, &st->msg);
+		break;
+	case AD9834_FSEL:
+	case AD9834_PSEL:
+		if (val == 0)
+			st->control &= ~(this_attr->address | AD9834_PIN_SW);
+		else if (val == 1) {
+			st->control |= this_attr->address;
+			st->control &= ~AD9834_PIN_SW;
+		}

Do we want an error if val is anything else rather than carrying on with the last valid setting?

drivers/staging/iio/dds/ad9834.c line 112

Quoted 8 lines
drivers/staging/iio/dds/ad9834.c new file +8 -0
+		st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
+		ret = spi_sync(st->spi, &st->msg);
+		break;
+	case AD9834_RESET:
+		if (val)
+			st->control |= AD9834_RESET;
+		else
+			st->control &= ~AD9834_RESET;

Appears to be a tab issue on the next line.

Quoted 12 lines
drivers/staging/iio/dds/ad9834.c new file +12 -0
+			st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
+		ret = spi_sync(st->spi, &st->msg);
+		break;
+	default:
+		ret = -ENODEV;
+	}
+	mutex_unlock(&dev_info->mlock);
+
+error_ret:
+	return ret ? ret : len;
+}
+

drivers/staging/iio/dds/ad9834.c line 134

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+static ssize_t ad9834_store_mode(struct device *dev,
+				 struct device_attribute *attr,
+				 const char *buf,
+				 size_t len)
+{
+	struct iio_dev *dev_info = dev_get_drvdata(dev);
+	struct ad9834_state *st = dev_info->dev_data;
+	int ret;
+
+	mutex_lock(&dev_info->mlock);

Use the sysfs_streq instead for these? (avoids possible white space issues etc).

Quoted 12 lines
drivers/staging/iio/dds/ad9834.c new file +12 -0
+	if (strncmp(buf, "sine", 4) == 0)
+		st->control &= ~AD9834_MODE;
+	else if (strncmp(buf, "triangle", 8) == 0)
+		st->control |= AD9834_MODE;
+	else
+		ret = -EINVAL;
+	mutex_unlock(&dev_info->mlock);
+
+	return ret ? ret : len;
+}
+
+static ssize_t ad9834_show_name(struct device *dev,

drivers/staging/iio/dds/ad9834.c line 156

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+				 struct device_attribute *attr,
+				 char *buf)
+{
+	struct iio_dev *dev_info = dev_get_drvdata(dev);
+	struct ad9834_state *st = iio_dev_get_devdata(dev_info);
+
+	return sprintf(buf, "%s\n", spi_get_device_id(st->spi)->name);
+}
+static IIO_DEVICE_ATTR(name, S_IRUGO, ad9834_show_name, NULL, 0);
+

Comments on these alongside the header where the macros are defined.

Quoted 52 lines
drivers/staging/iio/dds/ad9834.c new file +52 -0
+static IIO_DEV_ATTR_FREQ(0, ad9834_write, AD9834_REG_FREQ0);
+static IIO_DEV_ATTR_FREQ(1, ad9834_write, AD9834_REG_FREQ1);
+static IIO_DEV_ATTR_FREQ_EN(ad9834_write, AD9834_FSEL);
+static IIO_CONST_ATTR_FREQ_SCALE("1"); /* 1Hz */
+
+static IIO_DEV_ATTR_PHASE(0, ad9834_write, AD9834_REG_PHASE0);
+static IIO_DEV_ATTR_PHASE(1, ad9834_write, AD9834_REG_PHASE1);
+static IIO_DEV_ATTR_PHASE_EN(ad9834_write, AD9834_PSEL);
+static IIO_CONST_ATTR_PHASE_SCALE("0.0015339808"); /* 2PI/2^12 rad*/
+
+static IIO_DEV_ATTR_PINCONTROL_EN(ad9834_write, AD9834_PIN_SW);
+static IIO_DEV_ATTR_DISABLE(ad9834_write, AD9834_RESET);
+static IIO_DEV_ATTR_SIGNBIT_OUTPUT_EN(ad9834_write, AD9834_OPBITEN);
+static IIO_DEV_ATTR_OUTPUT_MODE(ad9834_store_mode, 0);
+static IIO_CONST_ATTR_AVAILABLE_OUTPUT_MODES("sine triangle");
+
+static struct attribute *ad9834_attributes[] = {
+	&iio_dev_attr_freq0.dev_attr.attr,
+	&iio_dev_attr_freq1.dev_attr.attr,
+	&iio_const_attr_freq_scale.dev_attr.attr,
+	&iio_dev_attr_phase0.dev_attr.attr,
+	&iio_dev_attr_phase1.dev_attr.attr,
+	&iio_const_attr_phase_scale.dev_attr.attr,
+	&iio_dev_attr_pincontrol_en.dev_attr.attr,
+	&iio_dev_attr_freq_en.dev_attr.attr,
+	&iio_dev_attr_phase_en.dev_attr.attr,
+	&iio_dev_attr_disable.dev_attr.attr,
+	&iio_dev_attr_signbit_output_en.dev_attr.attr,
+	&iio_dev_attr_output_mode.dev_attr.attr,
+	&iio_const_attr_available_output_modes.dev_attr.attr,
+	&iio_dev_attr_name.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group ad9834_attribute_group = {
+	.attrs = ad9834_attributes,
+};
+
+static int __devinit ad9834_probe(struct spi_device *spi)
+{
+	struct ad9834_platform_data *pdata = spi->dev.platform_data;
+	struct ad9834_state *st;
+	int ret, voltage_uv = 0;
+
+	if (!pdata) {
+		dev_dbg(&spi->dev, "no platform data?\n");
+		return -ENODEV;
+	}
+
+	st = kzalloc(sizeof(*st), GFP_KERNEL);
+	if (st == NULL) {
+		ret = -ENOMEM;

drivers/staging/iio/dds/ad9834.c line 218

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+		goto error_ret;
+	}
+
+	st->reg = regulator_get(&spi->dev, "vcc");
+	if (!IS_ERR(st->reg)) {
+		ret = regulator_enable(st->reg);
+		if (ret)
+			goto error_put_reg;
+
+		voltage_uv = regulator_get_voltage(st->reg);

Guessing you wanted to do something with this but haven't implemented it yet?

Quoted 40 lines
drivers/staging/iio/dds/ad9834.c new file +40 -0
+	}
+
+	st->mclk = pdata->mclk;
+
+	spi_set_drvdata(spi, st);
+
+	st->spi = spi;
+
+	st->indio_dev = iio_allocate_device();
+	if (st->indio_dev == NULL) {
+		ret = -ENOMEM;
+		goto error_disable_reg;
+	}
+
+	st->indio_dev->dev.parent = &spi->dev;
+	st->indio_dev->attrs = &ad9834_attribute_group;
+	st->indio_dev->dev_data = (void *)(st);
+	st->indio_dev->driver_module = THIS_MODULE;
+	st->indio_dev->modes = INDIO_DIRECT_MODE;
+
+	/* Setup default messages */
+
+	st->xfer.tx_buf = &st->data;
+	st->xfer.len = 2;
+
+	spi_message_init(&st->msg);
+	spi_message_add_tail(&st->xfer, &st->msg);
+
+	st->freq_xfer[0].tx_buf = &st->freq_data[0];
+	st->freq_xfer[0].len = 2;
+	st->freq_xfer[0].cs_change = 1;
+	st->freq_xfer[1].tx_buf = &st->freq_data[1];
+	st->freq_xfer[1].len = 2;
+
+	spi_message_init(&st->freq_msg);
+	spi_message_add_tail(&st->freq_xfer[0], &st->freq_msg);
+	spi_message_add_tail(&st->freq_xfer[1], &st->freq_msg);
+
+	st->control = AD9834_B28 | AD9834_RESET;
+

drivers/staging/iio/dds/ad9834.c line 268

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+	if (!pdata->en_div2)
+		st->control |= AD9834_DIV2;
+
+	if (!pdata->en_signbit_msb_out)
+		st->control |= AD9834_SIGN_PIB;
+
+	st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
+	ret = spi_sync(st->spi, &st->msg);
+
+	ret = ad9834_write_frequency(st, AD9834_REG_FREQ0, pdata->freq0);

Annoying though it might be, I'd prefer to see the errors handled independently. Weird things might happen if two different errors are output. This is far from implausible as an initial error in transmission might give say -EAGAIN then continuing problems cause EIO (or similar).

Quoted 40 lines
drivers/staging/iio/dds/ad9834.c new file +40 -0
+	ret |= ad9834_write_frequency(st, AD9834_REG_FREQ1, pdata->freq1);
+	ret |= ad9834_write_phase(st, AD9834_REG_PHASE0, pdata->phase0);
+	ret |= ad9834_write_phase(st, AD9834_REG_PHASE1, pdata->phase1);
+
+	st->control &= ~AD9834_RESET;
+
+	st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
+	ret |= spi_sync(st->spi, &st->msg);
+
+	if (ret) {
+		dev_err(&spi->dev, "device init failed\n");
+		goto error_free_device;
+	}
+
+	ret = iio_device_register(st->indio_dev);
+	if (ret)
+		goto error_free_device;
+
+	return 0;
+
+error_free_device:
+	iio_free_device(st->indio_dev);
+error_disable_reg:
+	if (!IS_ERR(st->reg))
+		regulator_disable(st->reg);
+error_put_reg:
+	if (!IS_ERR(st->reg))
+		regulator_put(st->reg);
+	kfree(st);
+error_ret:
+	return ret;
+}
+
+static int ad9834_remove(struct spi_device *spi)
+{
+	struct ad9834_state *st = spi_get_drvdata(spi);
+	struct iio_dev *indio_dev = st->indio_dev;
+
+	iio_device_unregister(indio_dev);
+	if (!IS_ERR(st->reg)) {

drivers/staging/iio/dds/ad9834.c line 318

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+		regulator_disable(st->reg);
+		regulator_put(st->reg);
+	}
+	kfree(st);
+	return 0;
+}
+
+static const struct spi_device_id ad9834_id[] = {
+	{"ad9833", ID_AD9833},
+	{"ad9834", ID_AD9834},

I don't think you ever use the enum values. If you don't and there is not likely to be a need anytime soon, it would be a little cleaner not to specify them. That would make it clear the parts are identical as far as the driver is concerned.

Quoted 64 lines
drivers/staging/iio/dds/ad9834.c new file +30 -0
+	{}
+};
+
+static struct spi_driver ad9834_driver = {
+	.driver = {
+		.name	= "ad9834",
+		.bus	= &spi_bus_type,
+		.owner	= THIS_MODULE,
+	},
+	.probe		= ad9834_probe,
+	.remove		= __devexit_p(ad9834_remove),
+	.id_table	= ad9834_id,
+};
+
+static int __init ad9834_init(void)
+{
+	return spi_register_driver(&ad9834_driver);
+}
+module_init(ad9834_init);
+
+static void __exit ad9834_exit(void)
+{
+	spi_unregister_driver(&ad9834_driver);
+}
+module_exit(ad9834_exit);
+
+MODULE_AUTHOR("Michael Hennerich <[email protected]>");
+MODULE_DESCRIPTION("Analog Devices AD9833/AD9834 DAC");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("spi:ad9834");
drivers/staging/iio/dds/ad9834.h new file +28 -0
new file mode 100644
index 0000000..9b59061
@@ -0,0 +1,108 @@
+/*
+ * AD9834 SPI DDS driver
+ *
+ * Copyright 2010 Analog Devices Inc.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+#ifndef IIO_DDS_AD9834_H_
+#define IIO_DDS_AD9834_H_
+
+/* Registers */
+
+#define AD9834_REG_CMD		(0 << 14)
+#define AD9834_REG_FREQ0	(1 << 14)
+#define AD9834_REG_FREQ1	(2 << 14)
+#define AD9834_REG_PHASE0	(6 << 13)
+#define AD9834_REG_PHASE1	(7 << 13)
+
+/* Command Control Bits */
+
+#define AD9834_B28		(1 << 13)
+#define AD9834_HLB		(1 << 12)
+#define AD9834_FSEL		(1 << 11)
+#define AD9834_PSEL		(1 << 10)
+#define AD9834_PIN_SW		(1 << 9)
+#define AD9834_RESET		(1 << 8)
+#define AD9834_SLEEP1		(1 << 7)
+#define AD9834_SLEEP12		(1 << 6)

drivers/staging/iio/dds/ad9834.h line 38

Quoted 10 lines
drivers/staging/iio/dds/ad9834.h new file +10 -0
+#define AD9834_OPBITEN		(1 << 5)
+#define AD9834_SIGN_PIB		(1 << 4)
+#define AD9834_DIV2		(1 << 3)
+#define AD9834_MODE		(1 << 1)
+
+#define AD9834_FREQ_BITS	28
+#define AD9834_PHASE_BITS	12
+
+#define RES_MASK(bits)	((1 << (bits)) - 1)
+

Proper docs make me happy :) Makes it nice and easy to spot odd items...

drivers/staging/iio/dds/ad9834.h line 44

Quoted 6 lines
drivers/staging/iio/dds/ad9834.h new file +6 -0
+/**
+ * struct ad9834_state - driver instance specific data
+ * @indio_dev:		the industrial I/O device
+ * @spi:		spi_device
+ * @reg:		supply regulator
+ * @poll_work:		bottom half of polling interrupt handler

No sign of any poll work in this driver at the mo...

Quoted 12 lines
drivers/staging/iio/dds/ad9834.h new file +12 -0
+ * @mclk:		external master clock
+ * @control:		cached control word
+ * @xfer:		default spi transfer
+ * @msg:		default spi message
+ * @data:		spi transmit buffer
+ * @freq_xfer:		tuning word spi transfer
+ * @freq_msg:		tuning word spi message
+ * @freq_data:		tuning word spi transmit buffer
+ */
+
+struct ad9834_state {
+	struct iio_dev			*indio_dev;

drivers/staging/iio/dds/ad9834.h line 66

Quoted 10 lines
drivers/staging/iio/dds/ad9834.h new file +10 -0
+	struct spi_device		*spi;
+	struct regulator		*reg;
+	struct work_struct		poll_work;
+	unsigned int			mclk;
+	unsigned short			control;
+	struct spi_transfer		xfer;
+	struct spi_message		msg;
+	unsigned short			data;
+	struct spi_transfer		freq_xfer[2];
+	struct spi_message		freq_msg;

Going to need your usual alignment trick to avoid cache line problems. Same for data above I think.

Quoted 48 lines
drivers/staging/iio/dds/ad9834.h new file +42 -0
+	unsigned short			freq_data[2];
+};
+
+
+/*
+ * TODO: struct ad7887_platform_data needs to go into include/linux/iio
+ */
+
+/**
+ * struct ad9834_platform_data - platform specific information
+ * @mclk:		master clock in Hz
+ * @freq0:		power up freq0 tuning word in Hz
+ * @freq1:		power up freq1 tuning word in Hz
+ * @phase0:		power up phase0 value [0..4095] correlates with 0..2PI
+ * @phase1:		power up phase1 value [0..4095] correlates with 0..2PI
+ * @en_div2:		digital output/2 is passed to the SIGN BIT OUT pin
+ * @en_signbit_msb_out:	the MSB (or MSB/2) of the DAC data is connected to the
+ 			SIGN BIT OUT pin. en_div2 controls whether it is the MSB
+ 			or MSB/2 that is output. if en_signbit_msb_out=false,
+ 			the on-board comparator is connected to SIGN BIT OUT
+ */
+
+struct ad9834_platform_data {
+	unsigned int		mclk;
+	unsigned int		freq0;
+	unsigned int		freq1;
+	unsigned short		phase0;
+	unsigned short		phase1;
+	bool			en_div2;
+	bool			en_signbit_msb_out;
+};
+
+/**
+ * ad9834_supported_device_ids:
+ */
+
+enum ad9834_supported_device_ids {
+	ID_AD9833,
+	ID_AD9834,
+};
+
+#endif /* IIO_DDS_AD9834_H_ */
drivers/staging/iio/dds/dds.h new file +0 -0
new file mode 100644
index 0000000..6cbede6

drivers/staging/iio/dds/dds.h line 3

Quoted 3 lines
drivers/staging/iio/dds/dds.h new file +3 -0
@@ -0,0 +1,99 @@
+/*
+ * dds.h - sysfs attributes associated with DDS devices
+ */

Good to see so much documentation. I would ask that this also exists in the Documentation directory. Given there is so little overlap in attributes between this and our existing elements, it probably makes sense to add a new file called something like sysfs-bus-iio-dds. I also think this interface is worth posting (in that form) as an RFC to lkml. Note Greg just merged a big cleanup of the existing docs so that should be in linux-next etc tomorrow.

I am going to read this pretty much without referring to the datasheet so as to see how it might read to someone coming in with a different dds device.

drivers/staging/iio/dds/dds.h line 16

Quoted 13 lines
drivers/staging/iio/dds/dds.h new file +13 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/freqX
+ * Description:
+ * Stores frequency into tuning word register X.
+ * There can be more than one freqX file, which allows for pin controlled FSK
+ * Frequency Shift Keying (en_pincontrol is active) or the user can control
+ * the desired active tuning word by writing X to the en_freq file.
+ */
+
+#define IIO_DEV_ATTR_FREQ(_num, _store, _addr)				\
+	IIO_DEVICE_ATTR(freq##_num, S_IWUSR, NULL, _store, _addr)
+

Do we need a base name for these? If we have a device that combines a dds element with other IIO elements things could get rather confusing. This is also true if we have a single device with multiple dds channels (perhaps a more common situation).

Perhaps somthing like...

ddsX_freqY
ddsX_freq_en
ddsX_phaseY
ddsX_phase_en
ddsX_phase_scale
ddsX_phaseY_scale
ddsX_phase_en

drivers/staging/iio/dds/dds.h line 22

Quoted 6 lines
drivers/staging/iio/dds/dds.h new file +6 -0
+/**
+ * /sys/bus/iio/devices/deviceX/freq_en
+ * Description:
+ * Specifies the active output frequency tuning word.
+ * To exit this mode the user can write pincontrol_en or disable file.
+ */

Based on the description I didn't really follow this one.
Looking at what the code does, it's a simple input for of the current FSK symbol? _en is not a good name for it. I'd go with ddsX_freqsymbol or something like that. The value then corresponds to the Y in ddsX_freqY.

drivers/staging/iio/dds/dds.h line 28

Quoted 6 lines
drivers/staging/iio/dds/dds.h new file +6 -0
+
+#define IIO_DEV_ATTR_FREQ_EN(_store, _addr)				\
+	IIO_DEVICE_ATTR(freq_en, S_IWUSR, NULL, _store, _addr);
+
+#define IIO_CONST_ATTR_FREQ_SCALE(_string)		\
+	IIO_CONST_ATTR(freq_scale, _string)

This should probably be under the freqX element as that is what it applies to. I'd like to see some explanation.

Quoted 11 lines
drivers/staging/iio/dds/dds.h new file +11 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/phaseX
+ * Description:
+ * Stores phase into phase register X.
+ * There can be more than one phaseX file, which allows for pin controlled PSK
+ * Phase Shift Keying (en_pincontrol is active) or the user can control
+ * the desired phase X which is added to the phase accumulator output
+ * by writing X to the en_phase file.
+ */
+

drivers/staging/iio/dds/dds.h line 49

Quoted 10 lines
drivers/staging/iio/dds/dds.h new file +10 -0
+#define IIO_DEV_ATTR_PHASE(_num, _store, _addr)				\
+	IIO_DEVICE_ATTR(phase##_num, S_IWUSR, NULL, _store, _addr)
+
+/**
+ * /sys/bus/iio/devices/deviceX/phase_en
+ * Description:
+ * Specifies the active phase which is added to the phase accumulator output.
+ * To exit this mode the user can write pincontrol_en or disable file.
+ */
+

These have the same questions as above for frequency.

Quoted 6 lines
drivers/staging/iio/dds/dds.h new file +6 -0
+#define IIO_DEV_ATTR_PHASE_EN(_store, _addr)				\
+	IIO_DEVICE_ATTR(phase_en, S_IWUSR, NULL, _store, _addr);
+
+#define IIO_CONST_ATTR_PHASE_SCALE(_string)		\
+	IIO_CONST_ATTR(phase_scale, _string)
+

drivers/staging/iio/dds/dds.h line 65

Quoted 10 lines
drivers/staging/iio/dds/dds.h new file +10 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/pincontrol_en
+ * Description:
+ * The active frequency and phase is controlled by the respective
+ * phase and frequency control inputs.
+ */
+
+#define IIO_DEV_ATTR_PINCONTROL_EN(_store, _addr)			\
+	IIO_DEVICE_ATTR(pincontrol_en, S_IWUSR, NULL, _store, _addr);

Is it just about plausible we will get a device with separate pin control for phase and frequency? (I really don't know much about these devices ;) If so that naming should allow for it. Perhaps you are allowing for it here and the specific ones would be ddsX_pincontrol_phase_en
ddsX_pincontrol_freq_en
or
dds_pincontrol_en for one that covers all channels and both phase and frequency?

drivers/staging/iio/dds/dds.h line 70

Quoted 5 lines
drivers/staging/iio/dds/dds.h new file +5 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/disable
+ * Description:
+ * Disables any signal generation, the output is set to midscale

Is the midscale a feature of this part of absolutely general? i.e. do we want to enforce this by documentation or allow for some flexibility (and a means to specify if it is something else?).

drivers/staging/iio/dds/dds.h line 82

Quoted 12 lines
drivers/staging/iio/dds/dds.h new file +12 -0
+ */
+
+#define IIO_DEV_ATTR_DISABLE(_store, _addr)				\
+	IIO_DEVICE_ATTR(disable, S_IWUSR, NULL, _store, _addr);
+
+/**
+ * /sys/bus/iio/devices/deviceX/signbit_output_en
+ * Description:
+ * Enables an auxiliary square wave output
+ */
+#define IIO_DEV_ATTR_SIGNBIT_OUTPUT_EN(_store, _addr)				\
+	IIO_DEVICE_ATTR(signbit_output_en, S_IWUSR, NULL, _store, _addr);

How standard is this? Can we make it look more general. See next comment.

drivers/staging/iio/dds/dds.h line 91

Quoted 9 lines
drivers/staging/iio/dds/dds.h new file +9 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/output_mode
+ * Description:
+ * Specifies the output waveform.
+ * For a list of available output waveform modes read available_output_modes.
+ */
+#define IIO_DEV_ATTR_OUTPUT_MODE(_store, _addr)				\
+	IIO_DEVICE_ATTR(output_mode, S_IWUSR, NULL, _store, _addr);

ddsX_waveform (or perhaps wavetype) seems more descriptive to me. Output mode could cover a whole host of things!

dds0_wavetype (sine, triangle, square)

Quoted 1 line
Quoted 1 line

From a generalization point of view we don't care that the square wave is a

random bonus option the part happens to be able to kick out. Ideally we want a naming scheme that makes it's relationship to the other waveforms apparent though and it's not entirely obvious how to handle that (e.g. how to handle the double frequency square wave this part can produce). Things will get more fun if parts have multiple outputs tied to one dds channel.

I'd also like to see the docs including the current options and a description of each. It's fairly obvious for those we have here, but there are going to be less simple cases in future!

Quoted 8 lines
drivers/staging/iio/dds/dds.h new file +8 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/available_output_modes
+ * Description:
+ * Lists all available output waveform modes
+ */
+#define IIO_CONST_ATTR_AVAILABLE_OUTPUT_MODES(_modes)			\
+	IIO_CONST_ATTR(available_output_modes, _modes);
author maintainer
#2 lore raw
Quoted 26 lines

From: Jonathan Cameron [mailto:[email protected]]

Quoted 6 lines · On 11/29/10 15:51, [email protected] wrote:

From: Michael Hennerich <[email protected]>

staging: iio: dds: ad9834: add missing include file

staging: iio: dds: ad9834: fix typo

Hi Michael,

Thanks for this driver and in particular the first thoughts on a suitable abi design for these parts. There are a few nitpicks in the driver body that will be trivial to cleanup.

The abi needs more open discussion. I have inserted a few suggestions here, but I would also like to see it formally proposed as an RFC separate from the driver to linux-iio and lkml. It's a fairly substantial new abi that we want to try and get right. Given similar postings of IIO abi's in the past the lkml posting may fall on deaf ears, but then at least you can use it to bash latecomers over the head with when they disagree with your interface 6 months down the line... It won't help but it always makes me feel better and we might get a few more people who did read it at the time coming in on our side if we are lucky :)

Quoted 3 lines

Just wait... Someone will want to use a nice general DDS as a clock input for some other part and hence ask about in kernel interfaces...

Hi Jonathan,

That's something we can add later.

Quoted 26 lines

Thanks,

Jonathan

Quoted 6 lines
---
 drivers/staging/iio/dds/Kconfig  |   11 +-
 drivers/staging/iio/dds/Makefile |    1 +
 drivers/staging/iio/dds/ad9834.c |  348

++++++++++++++++++++++++++++++++++++++

Review

Quoted 8 lines
 drivers/staging/iio/dds/ad9834.h |  108 ++++++++++++
 drivers/staging/iio/dds/dds.h    |   99 +++++++++++
 5 files changed, 565 insertions(+), 2 deletions(-)
 create mode 100644 drivers/staging/iio/dds/ad9834.c
 create mode 100644 drivers/staging/iio/dds/ad9834.h
 create mode 100644 drivers/staging/iio/dds/dds.h
(quoted diff) +0 -0

b/drivers/staging/iio/dds/Kconfig

Quoted 6 lines

index d045ed6..4c9cce3 100644

drivers/staging/iio/dds/Kconfig +0 -0
@@ -11,11 +11,18 @@ config AD5930
      ad5930/ad5932, provides direct access via sysfs.
 
Quoted 11 lines

drivers/staging/iio/dds/Kconfig config AD5930 · line 18

Quoted 7 lines
drivers/staging/iio/dds/Kconfig +1 -2
 config AD9832
-   tristate "Analog Devices ad9832/3/4/5 driver"
+   tristate "Analog Devices ad9832/5 driver"
    depends on SPI
    help
      Say yes here to build support for Analog Devices DDS chip
-     ad9832/3/4/5, provides direct access via sysfs.

This is somewhat 'interesting'. Please add to commit comment to say whether this change was due to only partial support existing in the 'old' driver or if it never worked in the first place for these parts.

Actually support for the ADP9833/34 never existed in the AD9832 driver.

Quoted 107 lines

Review

Quoted 12 lines
drivers/staging/iio/dds/Kconfig +8 -0
+     ad9832 and ad9835, provides direct access via sysfs.
+
+config AD9834
+   tristate "Analog Devices ad9833/4/ driver"
+   depends on SPI
+   help
+     Say yes here to build support for Analog Devices DDS chip
+     AD9833 and AD9834, provides direct access via sysfs.
 
 config AD9850
    tristate "Analog Devices ad9850/1 driver"
(quoted diff) +0 -0

b/drivers/staging/iio/dds/Makefile

Review

Quoted 12 lines

index 6f274ac..1477461 100644

drivers/staging/iio/dds/Makefile +1 -0
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_AD5930) +=3D ad5930.o
 obj-$(CONFIG_AD9832) +=3D ad9832.o
+obj-$(CONFIG_AD9834) +=3D ad9834.o
 obj-$(CONFIG_AD9850) +=3D ad9850.o
 obj-$(CONFIG_AD9852) +=3D ad9852.o
 obj-$(CONFIG_AD9910) +=3D ad9910.o
(quoted diff) +0 -0

b/drivers/staging/iio/dds/ad9834.c

Quoted 22 lines

new file mode 100644
index 0000000..d17f139

drivers/staging/iio/dds/ad9834.c new file +17 -0
@@ -0,0 +1,348 @@
+/*
+ * AD9834 SPI DAC driver
+ *
+ * Copyright 2010 Analog Devices Inc.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+
+#include <linux/interrupt.h>
+#include <linux/workqueue.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+#include <linux/list.h>
+#include <linux/spi/spi.h>
+#include <linux/regulator/consumer.h>

drivers/staging/iio/dds/ad9834.c line 27

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+#include <linux/err.h>
+#include <asm/div64.h>
+
+#include "../iio.h"
+#include "../sysfs.h"
+#include "dds.h"
+
+#include "ad9834.h"
+
+static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned

long fout)

drivers/staging/iio/dds/ad9834.c line 29

Quoted 2 lines
drivers/staging/iio/dds/ad9834.c new file +2 -0
+{
+   unsigned long long freqreg =3D (u64) fout * (u64) (1 <<

AD9834_FREQ_BITS);

Quoted 45 lines
drivers/staging/iio/dds/ad9834.c new file +45 -0
+   do_div(freqreg, mclk);
+   return freqreg;
+}
+
+static int ad9834_write_frequency(struct ad9834_state *st,
+                             unsigned long addr, unsigned long fout)
+{
+   unsigned long regval;
+
+   if (fout > (st->mclk / 2))
+           return -EINVAL;
+
+   regval =3D ad9834_calc_freqreg(st->mclk, fout);
+
+   st->freq_data[0] =3D cpu_to_be16(addr | (regval &
+                                  RES_MASK(AD9834_FREQ_BITS / 2)));
+   st->freq_data[1] =3D cpu_to_be16(addr | ((regval >>
+                                  (AD9834_FREQ_BITS / 2)) &
+                                  RES_MASK(AD9834_FREQ_BITS / 2)));
+
+   return spi_sync(st->spi, &st->freq_msg);;
+}
+
+static int ad9834_write_phase(struct ad9834_state *st,
+                             unsigned long addr, unsigned long phase)
+{
+   if (phase > (1 << AD9834_PHASE_BITS))
+           return -EINVAL;
+   st->data =3D cpu_to_be16(addr | phase);
+
+   return spi_sync(st->spi, &st->msg);
+}
+
+static ssize_t ad9834_write(struct device *dev,
+           struct device_attribute *attr,
+           const char *buf,
+           size_t len)
+{
+   struct iio_dev *dev_info =3D dev_get_drvdata(dev);
+   struct ad9834_state *st =3D dev_info->dev_data;
+   struct iio_dev_attr *this_attr =3D to_iio_dev_attr(attr);
+   int ret;
+   long val;
+
+   ret =3D strict_strtol(buf, 10, &val);
Quoted 8 lines

drivers/staging/iio/dds/ad9834.c

Quoted 8 lines
drivers/staging/iio/dds/ad9834.c new file +8 -0
+   if (ret)
+           goto error_ret;
+
+   mutex_lock(&dev_info->mlock);
+   switch (this_attr->address) {
+   case AD9834_REG_FREQ0:
+   case AD9834_REG_FREQ1:
+           ret =3D ad9834_write_frequency(st, this_attr->address, val)=

;

Quoted 11 lines

drivers/staging/iio/dds/ad9834.c

Quoted 11 lines
drivers/staging/iio/dds/ad9834.c new file +11 -0
+           break;
+   case AD9834_REG_PHASE0:
+   case AD9834_REG_PHASE1:
+           ret =3D ad9834_write_phase(st, this_attr->address, val);
+           break;
+   case AD9834_PIN_SW:
+   case AD9834_OPBITEN:
+           if (val)
+                   st->control |=3D this_attr->address;
+           else
+                   st->control &=3D ~this_attr->address;
Quoted 2 lines

Odd tabbing

Quoted 1 line
drivers/staging/iio/dds/ad9834.c new file +1 -0
+                   st->data =3D cpu_to_be16(AD9834_REG_CMD | st->contr=

ol);

Quoted 6 lines

drivers/staging/iio/dds/ad9834.c

Quoted 6 lines
drivers/staging/iio/dds/ad9834.c new file +6 -0
+           ret =3D spi_sync(st->spi, &st->msg);
+           break;
+   case AD9834_FSEL:
+   case AD9834_PSEL:
+           if (val =3D=3D 0)
+                   st->control &=3D ~(this_attr->address | AD9834_PIN_=

SW);

Quoted 6 lines

drivers/staging/iio/dds/ad9834.c line 104

Quoted 4 lines
drivers/staging/iio/dds/ad9834.c new file +4 -0
+           else if (val =3D=3D 1) {
+                   st->control |=3D this_attr->address;
+                   st->control &=3D ~AD9834_PIN_SW;
+           }

Do we want an error if val is anything else rather than carrying on with the last valid setting?

I made this case error returning -EINVAL

Quoted 10 lines

drivers/staging/iio/dds/ad9834.c

Quoted 8 lines
drivers/staging/iio/dds/ad9834.c new file +8 -0
+           st->data =3D cpu_to_be16(AD9834_REG_CMD | st->control);
+           ret =3D spi_sync(st->spi, &st->msg);
+           break;
+   case AD9834_RESET:
+           if (val)
+                   st->control |=3D AD9834_RESET;
+           else
+                   st->control &=3D ~AD9834_RESET;

Appears to be a tab issue on the next line.

Quoted 1 line
drivers/staging/iio/dds/ad9834.c new file +1 -0
+                   st->data =3D cpu_to_be16(AD9834_REG_CMD | st->contr=

ol);

Quoted 15 lines
Quoted 15 lines
drivers/staging/iio/dds/ad9834.c new file +15 -0
+           ret =3D spi_sync(st->spi, &st->msg);
+           break;
+   default:
+           ret =3D -ENODEV;
+   }
+   mutex_unlock(&dev_info->mlock);
+
+error_ret:
+   return ret ? ret : len;
+}
+
+static ssize_t ad9834_store_mode(struct device *dev,
+                            struct device_attribute *attr,
+                            const char *buf,
+                            size_t len)
Quoted 8 lines

drivers/staging/iio/dds/ad9834.c line 78

Quoted 6 lines
drivers/staging/iio/dds/ad9834.c new file +6 -0
+{
+   struct iio_dev *dev_info =3D dev_get_drvdata(dev);
+   struct ad9834_state *st =3D dev_info->dev_data;
+   int ret;
+
+   mutex_lock(&dev_info->mlock);

Use the sysfs_streq instead for these? (avoids possible white space issues etc).

Thanks - sysfs_streq is much better.

Quoted 81 lines
Quoted 12 lines
drivers/staging/iio/dds/ad9834.c new file +12 -0
+   if (strncmp(buf, "sine", 4) =3D=3D 0)
+           st->control &=3D ~AD9834_MODE;
+   else if (strncmp(buf, "triangle", 8) =3D=3D 0)
+           st->control |=3D AD9834_MODE;
+   else
+           ret =3D -EINVAL;
+   mutex_unlock(&dev_info->mlock);
+
+   return ret ? ret : len;
+}
+
+static ssize_t ad9834_show_name(struct device *dev,

drivers/staging/iio/dds/ad9834.c line 156

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+                            struct device_attribute *attr,
+                            char *buf)
+{
+   struct iio_dev *dev_info =3D dev_get_drvdata(dev);
+   struct ad9834_state *st =3D iio_dev_get_devdata(dev_info);
+
+   return sprintf(buf, "%s\n", spi_get_device_id(st->spi)->name);
+}
+static IIO_DEVICE_ATTR(name, S_IRUGO, ad9834_show_name, NULL, 0);
+

Comments on these alongside the header where the macros are defined.

Quoted 56 lines
drivers/staging/iio/dds/ad9834.c new file +56 -0
+static IIO_DEV_ATTR_FREQ(0, ad9834_write, AD9834_REG_FREQ0);
+static IIO_DEV_ATTR_FREQ(1, ad9834_write, AD9834_REG_FREQ1);
+static IIO_DEV_ATTR_FREQ_EN(ad9834_write, AD9834_FSEL);
+static IIO_CONST_ATTR_FREQ_SCALE("1"); /* 1Hz */
+
+static IIO_DEV_ATTR_PHASE(0, ad9834_write, AD9834_REG_PHASE0);
+static IIO_DEV_ATTR_PHASE(1, ad9834_write, AD9834_REG_PHASE1);
+static IIO_DEV_ATTR_PHASE_EN(ad9834_write, AD9834_PSEL);
+static IIO_CONST_ATTR_PHASE_SCALE("0.0015339808"); /* 2PI/2^12 rad*/
+
+static IIO_DEV_ATTR_PINCONTROL_EN(ad9834_write, AD9834_PIN_SW);
+static IIO_DEV_ATTR_DISABLE(ad9834_write, AD9834_RESET);
+static IIO_DEV_ATTR_SIGNBIT_OUTPUT_EN(ad9834_write, AD9834_OPBITEN);
+static IIO_DEV_ATTR_OUTPUT_MODE(ad9834_store_mode, 0);
+static IIO_CONST_ATTR_AVAILABLE_OUTPUT_MODES("sine triangle");
+
+static struct attribute *ad9834_attributes[] =3D {
+   &iio_dev_attr_freq0.dev_attr.attr,
+   &iio_dev_attr_freq1.dev_attr.attr,
+   &iio_const_attr_freq_scale.dev_attr.attr,
+   &iio_dev_attr_phase0.dev_attr.attr,
+   &iio_dev_attr_phase1.dev_attr.attr,
+   &iio_const_attr_phase_scale.dev_attr.attr,
+   &iio_dev_attr_pincontrol_en.dev_attr.attr,
+   &iio_dev_attr_freq_en.dev_attr.attr,
+   &iio_dev_attr_phase_en.dev_attr.attr,
+   &iio_dev_attr_disable.dev_attr.attr,
+   &iio_dev_attr_signbit_output_en.dev_attr.attr,
+   &iio_dev_attr_output_mode.dev_attr.attr,
+   &iio_const_attr_available_output_modes.dev_attr.attr,
+   &iio_dev_attr_name.dev_attr.attr,
+   NULL,
+};
+
+static const struct attribute_group ad9834_attribute_group =3D {
+   .attrs =3D ad9834_attributes,
+};
+
+static int __devinit ad9834_probe(struct spi_device *spi)
+{
+   struct ad9834_platform_data *pdata =3D spi->dev.platform_data;
+   struct ad9834_state *st;
+   int ret, voltage_uv =3D 0;
+
+   if (!pdata) {
+           dev_dbg(&spi->dev, "no platform data?\n");
+           return -ENODEV;
+   }
+
+   st =3D kzalloc(sizeof(*st), GFP_KERNEL);
+   if (st =3D=3D NULL) {
+           ret =3D -ENOMEM;
+           goto error_ret;
+   }
+
+   st->reg =3D regulator_get(&spi->dev, "vcc");
Quoted 8 lines

drivers/staging/iio/dds/ad9834.c

Quoted 6 lines
drivers/staging/iio/dds/ad9834.c new file +6 -0
+   if (!IS_ERR(st->reg)) {
+           ret =3D regulator_enable(st->reg);
+           if (ret)
+                   goto error_put_reg;
+
+           voltage_uv =3D regulator_get_voltage(st->reg);

Guessing you wanted to do something with this but haven't implemented it yet?

Exactly - I removed it for now.

Quoted 103 lines
Quoted 40 lines
drivers/staging/iio/dds/ad9834.c new file +40 -0
+   }
+
+   st->mclk =3D pdata->mclk;
+
+   spi_set_drvdata(spi, st);
+
+   st->spi =3D spi;
+
+   st->indio_dev =3D iio_allocate_device();
+   if (st->indio_dev =3D=3D NULL) {
+           ret =3D -ENOMEM;
+           goto error_disable_reg;
+   }
+
+   st->indio_dev->dev.parent =3D &spi->dev;
+   st->indio_dev->attrs =3D &ad9834_attribute_group;
+   st->indio_dev->dev_data =3D (void *)(st);
+   st->indio_dev->driver_module =3D THIS_MODULE;
+   st->indio_dev->modes =3D INDIO_DIRECT_MODE;
+
+   /* Setup default messages */
+
+   st->xfer.tx_buf =3D &st->data;
+   st->xfer.len =3D 2;
+
+   spi_message_init(&st->msg);
+   spi_message_add_tail(&st->xfer, &st->msg);
+
+   st->freq_xfer[0].tx_buf =3D &st->freq_data[0];
+   st->freq_xfer[0].len =3D 2;
+   st->freq_xfer[0].cs_change =3D 1;
+   st->freq_xfer[1].tx_buf =3D &st->freq_data[1];
+   st->freq_xfer[1].len =3D 2;
+
+   spi_message_init(&st->freq_msg);
+   spi_message_add_tail(&st->freq_xfer[0], &st->freq_msg);
+   spi_message_add_tail(&st->freq_xfer[1], &st->freq_msg);
+
+   st->control =3D AD9834_B28 | AD9834_RESET;
+

drivers/staging/iio/dds/ad9834.c

Quoted 10 lines
drivers/staging/iio/dds/ad9834.c new file +10 -0
+   if (!pdata->en_div2)
+           st->control |=3D AD9834_DIV2;
+
+   if (!pdata->en_signbit_msb_out)
+           st->control |=3D AD9834_SIGN_PIB;
+
+   st->data =3D cpu_to_be16(AD9834_REG_CMD | st->control);
+   ret =3D spi_sync(st->spi, &st->msg);
+
+   ret =3D ad9834_write_frequency(st, AD9834_REG_FREQ0, pdata->freq0);

Annoying though it might be, I'd prefer to see the errors handled independently. Weird things might happen if two different errors are output. This is far from implausible as an initial error in transmission
might give say -EAGAIN then continuing problems cause EIO (or similar).

Quoted 48 lines
drivers/staging/iio/dds/ad9834.c new file +1 -0
+   ret |=3D ad9834_write_frequency(st, AD9834_REG_FREQ1, pdata-

freq1);

drivers/staging/iio/dds/ad9834.c new file +46 -0
+   ret |=3D ad9834_write_phase(st, AD9834_REG_PHASE0, pdata->phase0);
+   ret |=3D ad9834_write_phase(st, AD9834_REG_PHASE1, pdata->phase1);
+
+   st->control &=3D ~AD9834_RESET;
+
+   st->data =3D cpu_to_be16(AD9834_REG_CMD | st->control);
+   ret |=3D spi_sync(st->spi, &st->msg);
+
+   if (ret) {
+           dev_err(&spi->dev, "device init failed\n");
+           goto error_free_device;
+   }
+
+   ret =3D iio_device_register(st->indio_dev);
+   if (ret)
+           goto error_free_device;
+
+   return 0;
+
+error_free_device:
+   iio_free_device(st->indio_dev);
+error_disable_reg:
+   if (!IS_ERR(st->reg))
+           regulator_disable(st->reg);
+error_put_reg:
+   if (!IS_ERR(st->reg))
+           regulator_put(st->reg);
+   kfree(st);
+error_ret:
+   return ret;
+}
+
+static int ad9834_remove(struct spi_device *spi)
+{
+   struct ad9834_state *st =3D spi_get_drvdata(spi);
+   struct iio_dev *indio_dev =3D st->indio_dev;
+
+   iio_device_unregister(indio_dev);
+   if (!IS_ERR(st->reg)) {
+           regulator_disable(st->reg);
+           regulator_put(st->reg);
+   }
+   kfree(st);
+   return 0;
+}
+
Quoted 8 lines

drivers/staging/iio/dds/ad9834.c line 318

Quoted 3 lines
drivers/staging/iio/dds/ad9834.c new file +3 -0
+static const struct spi_device_id ad9834_id[] =3D {
+   {"ad9833", ID_AD9833},
+   {"ad9834", ID_AD9834},

I don't think you ever use the enum values. If you don't and there is not likely to be a need anytime soon, it would be a little cleaner not to specify them. That would make it clear the parts are identical as far as the driver is concerned.

The AD9833 is a bit different from the AD9834.
In the patch I'm going to send out shortly device_id is used in a few places.

Quoted 116 lines

Review

Quoted 31 lines
drivers/staging/iio/dds/ad9834.c new file +30 -0
+   {}
+};
+
+static struct spi_driver ad9834_driver =3D {
+   .driver =3D {
+           .name   =3D "ad9834",
+           .bus    =3D &spi_bus_type,
+           .owner  =3D THIS_MODULE,
+   },
+   .probe          =3D ad9834_probe,
+   .remove         =3D __devexit_p(ad9834_remove),
+   .id_table       =3D ad9834_id,
+};
+
+static int __init ad9834_init(void)
+{
+   return spi_register_driver(&ad9834_driver);
+}
+module_init(ad9834_init);
+
+static void __exit ad9834_exit(void)
+{
+   spi_unregister_driver(&ad9834_driver);
+}
+module_exit(ad9834_exit);
+
+MODULE_AUTHOR("Michael Hennerich <[email protected]>");
+MODULE_DESCRIPTION("Analog Devices AD9833/AD9834 DAC");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("spi:ad9834");
(quoted diff) +0 -0

b/drivers/staging/iio/dds/ad9834.h

Quoted 33 lines

new file mode 100644
index 0000000..9b59061

drivers/staging/iio/dds/ad9834.h new file +28 -0
@@ -0,0 +1,108 @@
+/*
+ * AD9834 SPI DDS driver
+ *
+ * Copyright 2010 Analog Devices Inc.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+#ifndef IIO_DDS_AD9834_H_
+#define IIO_DDS_AD9834_H_
+
+/* Registers */
+
+#define AD9834_REG_CMD             (0 << 14)
+#define AD9834_REG_FREQ0   (1 << 14)
+#define AD9834_REG_FREQ1   (2 << 14)
+#define AD9834_REG_PHASE0  (6 << 13)
+#define AD9834_REG_PHASE1  (7 << 13)
+
+/* Command Control Bits */
+
+#define AD9834_B28         (1 << 13)
+#define AD9834_HLB         (1 << 12)
+#define AD9834_FSEL                (1 << 11)
+#define AD9834_PSEL                (1 << 10)
+#define AD9834_PIN_SW              (1 << 9)
+#define AD9834_RESET               (1 << 8)
+#define AD9834_SLEEP1              (1 << 7)
+#define AD9834_SLEEP12             (1 << 6)

drivers/staging/iio/dds/ad9834.h line 38

Quoted 10 lines
drivers/staging/iio/dds/ad9834.h new file +10 -0
+#define AD9834_OPBITEN             (1 << 5)
+#define AD9834_SIGN_PIB            (1 << 4)
+#define AD9834_DIV2                (1 << 3)
+#define AD9834_MODE                (1 << 1)
+
+#define AD9834_FREQ_BITS   28
+#define AD9834_PHASE_BITS  12
+
+#define RES_MASK(bits)     ((1 << (bits)) - 1)
+

Proper docs make me happy :) Makes it nice and easy to spot odd items...

drivers/staging/iio/dds/ad9834.h line 44

Quoted 6 lines
drivers/staging/iio/dds/ad9834.h new file +6 -0
+/**
+ * struct ad9834_state - driver instance specific data
+ * @indio_dev:             the industrial I/O device
+ * @spi:           spi_device
+ * @reg:           supply regulator
+ * @poll_work:             bottom half of polling interrupt handler

No sign of any poll work in this driver at the mo...

Quoted 12 lines
drivers/staging/iio/dds/ad9834.h new file +12 -0
+ * @mclk:          external master clock
+ * @control:               cached control word
+ * @xfer:          default spi transfer
+ * @msg:           default spi message
+ * @data:          spi transmit buffer
+ * @freq_xfer:             tuning word spi transfer
+ * @freq_msg:              tuning word spi message
+ * @freq_data:             tuning word spi transmit buffer
+ */
+
+struct ad9834_state {
+   struct iio_dev                  *indio_dev;

drivers/staging/iio/dds/ad9834.h line 66

Quoted 10 lines
drivers/staging/iio/dds/ad9834.h new file +10 -0
+   struct spi_device               *spi;
+   struct regulator                *reg;
+   struct work_struct              poll_work;
+   unsigned int                    mclk;
+   unsigned short                  control;
+   struct spi_transfer             xfer;
+   struct spi_message              msg;
+   unsigned short                  data;
+   struct spi_transfer             freq_xfer[2];
+   struct spi_message              freq_msg;

Going to need your usual alignment trick to avoid cache line problems. Same for data above I think.

drivers/staging/iio/dds/ad9834.h

Quoted 6 lines
drivers/staging/iio/dds/ad9834.h new file +6 -0
+   unsigned short                  freq_data[2];
+};
+
+
+/*
+ * TODO: struct ad7887_platform_data needs to go into

include/linux/iio

Quoted 8 lines

drivers/staging/iio/dds/ad9834.h

Quoted 8 lines
drivers/staging/iio/dds/ad9834.h new file +8 -0
+ */
+
+/**
+ * struct ad9834_platform_data - platform specific information
+ * @mclk:          master clock in Hz
+ * @freq0:         power up freq0 tuning word in Hz
+ * @freq1:         power up freq1 tuning word in Hz
+ * @phase0:                power up phase0 value [0..4095] correlates =

with

Quoted 2 lines

0..2PI

Quoted 1 line
drivers/staging/iio/dds/ad9834.h new file +1 -0
+ * @phase1:                power up phase1 value [0..4095] correlates =

with

Quoted 2 lines

0..2PI

Quoted 1 line
drivers/staging/iio/dds/ad9834.h new file +1 -0
+ * @en_div2:               digital output/2 is passed to the SIGN BIT =

OUT

Quoted 4 lines

pin

drivers/staging/iio/dds/ad9834.h line 83

Quoted 1 line
drivers/staging/iio/dds/ad9834.h new file +1 -0
+ * @en_signbit_msb_out:    the MSB (or MSB/2) of the DAC data is

connected to the

Quoted 1 line
drivers/staging/iio/dds/ad9834.h new file +1 -0
+                   SIGN BIT OUT pin. en_div2 controls whether it is th=

e

Quoted 2 lines

MSB

Quoted 1 line
drivers/staging/iio/dds/ad9834.h new file +1 -0
+                   or MSB/2 that is output. if en_signbit_msb_out=3Dfa=

lse,

Quoted 1 line

drivers/staging/iio/dds/ad9834.h line 86

Quoted 1 line
drivers/staging/iio/dds/ad9834.h new file +1 -0
+                   the on-board comparator is connected to SIGN BIT OU=

T

Quoted 24 lines

Review

Quoted 23 lines
drivers/staging/iio/dds/ad9834.h new file +22 -0
+ */
+
+struct ad9834_platform_data {
+   unsigned int            mclk;
+   unsigned int            freq0;
+   unsigned int            freq1;
+   unsigned short          phase0;
+   unsigned short          phase1;
+   bool                    en_div2;
+   bool                    en_signbit_msb_out;
+};
+
+/**
+ * ad9834_supported_device_ids:
+ */
+
+enum ad9834_supported_device_ids {
+   ID_AD9833,
+   ID_AD9834,
+};
+
+#endif /* IIO_DDS_AD9834_H_ */
(quoted diff) +0 -0

b/drivers/staging/iio/dds/dds.h

Quoted 13 lines

drivers/staging/iio/dds/dds.h line 3

Quoted 8 lines

new file mode 100644
index 0000000..6cbede6

drivers/staging/iio/dds/dds.h new file +3 -0
@@ -0,0 +1,99 @@
+/*
+ * dds.h - sysfs attributes associated with DDS devices
+ */

Good to see so much documentation. I would ask that this also exists in the Documentation directory. Given there is so little overlap in attributes between this and our existing elements, it probably makes sense to add a new file called something like sysfs-bus-iio-dds.

I'll create this file after posting the RFC.
I don't want to update more or less the same documentation, in multiple places.

Quoted 4 lines

I also think this interface is worth posting (in that form) as an RFC to
lkml. Note Greg just merged a big cleanup of the existing docs so that should be in linux-next etc tomorrow.

Shall I just post the dds.h file as patch?

Quoted 19 lines

I am going to read this pretty much without referring to the datasheet so as to see how it might read to someone coming in with a different dds device.

drivers/staging/iio/dds/dds.h

Quoted 6 lines
drivers/staging/iio/dds/dds.h new file +6 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/freqX
+ * Description:
+ * Stores frequency into tuning word register X.
+ * There can be more than one freqX file, which allows for pin

controlled FSK

drivers/staging/iio/dds/dds.h line 10

Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+ * Frequency Shift Keying (en_pincontrol is active) or the user can

control

drivers/staging/iio/dds/dds.h

Quoted 4 lines
drivers/staging/iio/dds/dds.h new file +4 -0
+ * the desired active tuning word by writing X to the en_freq file.
+ */
+
+#define IIO_DEV_ATTR_FREQ(_num, _store, _addr)
      \

drivers/staging/iio/dds/dds.h line 16

Quoted 2 lines
drivers/staging/iio/dds/dds.h new file +2 -0
+   IIO_DEVICE_ATTR(freq##_num, S_IWUSR, NULL, _store, _addr)
+
Quoted 7 lines

Do we need a base name for these? If we have a device that combines a dds
element with other IIO elements things could get rather confusing. This is
also true if we have a single device with multiple dds channels (perhaps
a more common situation).

Good catch, in fact there is a quad channel device, featuring 4 independent=
dds outputs.

Quoted 9 lines

Perhaps somthing like...

ddsX_freqY
ddsX_freq_en
ddsX_phaseY
ddsX_phase_en
ddsX_phase_scale
ddsX_phaseY_scale
ddsX_phase_en

Looks good!

Quoted 10 lines

drivers/staging/iio/dds/dds.h

Quoted 5 lines
drivers/staging/iio/dds/dds.h new file +5 -0
+/**
+ * /sys/bus/iio/devices/deviceX/freq_en
+ * Description:
+ * Specifies the active output frequency tuning word.
+ * To exit this mode the user can write pincontrol_en or disable

file.

drivers/staging/iio/dds/dds.h line 22

Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+ */

Based on the description I didn't really follow this one. Looking at what the code does, it's a simple input for of the current FSK symbol?

Yes - writing this file selects the frequency tuning word.
It basically overwrites the pin control mechanism.
This can be useful when pin control is not used, and you alternatingly upda= te
dds0_freq0 and dds0_freq1 with new values. The chip then ensures that the n= ew
frequency value get's updates during the zero cross. Which avoids a lot of unwanted distortions.

Quoted 3 lines

_en is not a good name for it. I'd go with ddsX_freqsymbol or something like
that. The value then corresponds to the Y in ddsX_freqY.

ok

Quoted 2 lines

drivers/staging/iio/dds/dds.h line 24

Quoted 2 lines
drivers/staging/iio/dds/dds.h new file +2 -0
+
+#define IIO_DEV_ATTR_FREQ_EN(_store, _addr)                           =
     \
Quoted 7 lines

drivers/staging/iio/dds/dds.h line 28

Quoted 4 lines
drivers/staging/iio/dds/dds.h new file +4 -0
+   IIO_DEVICE_ATTR(freq_en, S_IWUSR, NULL, _store, _addr);
+
+#define IIO_CONST_ATTR_FREQ_SCALE(_string)         \
+   IIO_CONST_ATTR(freq_scale, _string)

This should probably be under the freqX element as that is what it applies
to. I'd like to see some explanation.

Added

Quoted 23 lines

drivers/staging/iio/dds/dds.h

Quoted 6 lines
drivers/staging/iio/dds/dds.h new file +6 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/phaseX
+ * Description:
+ * Stores phase into phase register X.
+ * There can be more than one phaseX file, which allows for pin

controlled PSK

drivers/staging/iio/dds/dds.h line 35

Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+ * Phase Shift Keying (en_pincontrol is active) or the user can

control

drivers/staging/iio/dds/dds.h line 36

Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+ * the desired phase X which is added to the phase accumulator

output

drivers/staging/iio/dds/dds.h

Quoted 4 lines
drivers/staging/iio/dds/dds.h new file +4 -0
+ * by writing X to the en_phase file.
+ */
+
+#define IIO_DEV_ATTR_PHASE(_num, _store, _addr)
      \

drivers/staging/iio/dds/dds.h

Quoted 6 lines
drivers/staging/iio/dds/dds.h new file +6 -0
+   IIO_DEVICE_ATTR(phase##_num, S_IWUSR, NULL, _store, _addr)
+
+/**
+ * /sys/bus/iio/devices/deviceX/phase_en
+ * Description:
+ * Specifies the active phase which is added to the phase

accumulator output.

Quoted 6 lines

drivers/staging/iio/dds/dds.h line 47

Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+ * To exit this mode the user can write pincontrol_en or disable

file.

drivers/staging/iio/dds/dds.h line 49

Quoted 2 lines
drivers/staging/iio/dds/dds.h new file +2 -0
+ */
+

These have the same questions as above for frequency.

Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+#define IIO_DEV_ATTR_PHASE_EN(_store, _addr)                          =
     \
Quoted 11 lines
Quoted 11 lines
drivers/staging/iio/dds/dds.h new file +11 -0
+   IIO_DEVICE_ATTR(phase_en, S_IWUSR, NULL, _store, _addr);
+
+#define IIO_CONST_ATTR_PHASE_SCALE(_string)                \
+   IIO_CONST_ATTR(phase_scale, _string)
+
+
+/**
+ * /sys/bus/iio/devices/deviceX/pincontrol_en
+ * Description:
+ * The active frequency and phase is controlled by the respective
+ * phase and frequency control inputs.
Quoted 8 lines

drivers/staging/iio/dds/dds.h line 65

Quoted 4 lines
drivers/staging/iio/dds/dds.h new file +4 -0
+ */
+
+#define IIO_DEV_ATTR_PINCONTROL_EN(_store, _addr)                  \
+   IIO_DEVICE_ATTR(pincontrol_en, S_IWUSR, NULL, _store, _addr);

Is it just about plausible we will get a device with separate pin control
for phase and frequency? (I really don't know much about these devices ;)

Yes that's the fact. There are even parts without pin control.

Quoted 7 lines

If so that naming should allow for it. Perhaps you are allowing for it here and the specific ones would be ddsX_pincontrol_phase_en
ddsX_pincontrol_freq_en
or
dds_pincontrol_en for one that covers all channels and both phase and frequency?

That's it.

Quoted 9 lines

drivers/staging/iio/dds/dds.h line 70

Quoted 6 lines
drivers/staging/iio/dds/dds.h new file +5 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/disable
+ * Description:
+ * Disables any signal generation, the output is set to midscale

Is the midscale a feature of this part of absolutely general? i.e. do we want to enforce this by documentation or allow for some flexibility (and a means to specify if it is something else?).

AFAIK the midscale thing is pretty common.
But there might be parts that shut down the output completely. I'll better remove this comment.

Quoted 3 lines

drivers/staging/iio/dds/dds.h line 73

Quoted 3 lines
drivers/staging/iio/dds/dds.h new file +3 -0
+ */
+
+#define IIO_DEV_ATTR_DISABLE(_store, _addr)                           =
     \
Quoted 11 lines

drivers/staging/iio/dds/dds.h

Quoted 8 lines
drivers/staging/iio/dds/dds.h new file +8 -0
+   IIO_DEVICE_ATTR(disable, S_IWUSR, NULL, _store, _addr);
+
+/**
+ * /sys/bus/iio/devices/deviceX/signbit_output_en
+ * Description:
+ * Enables an auxiliary square wave output
+ */
+#define IIO_DEV_ATTR_SIGNBIT_OUTPUT_EN(_store, _addr)
              \

drivers/staging/iio/dds/dds.h line 82

Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+   IIO_DEVICE_ATTR(signbit_output_en, S_IWUSR, NULL, _store, _addr);

How standard is this?

Only a few devices feature this, maybe remove from dds.h and just have it i= n the driver?

Quoted 11 lines

Can we make it look more general. See next
comment.

drivers/staging/iio/dds/dds.h

Quoted 6 lines
drivers/staging/iio/dds/dds.h new file +6 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/output_mode
+ * Description:
+ * Specifies the output waveform.
+ * For a list of available output waveform modes read

available_output_modes.

drivers/staging/iio/dds/dds.h line 90

Quoted 2 lines
drivers/staging/iio/dds/dds.h new file +2 -0
+ */
+#define IIO_DEV_ATTR_OUTPUT_MODE(_store, _addr)
Quoted 7 lines
      \

drivers/staging/iio/dds/dds.h line 91

Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+   IIO_DEVICE_ATTR(output_mode, S_IWUSR, NULL, _store, _addr);

ddsX_waveform (or perhaps wavetype) seems more descriptive to me. Output mode
could cover a whole host of things!

dds0_wavetype (sine, triangle, square)

Makes sense.

Quoted 10 lines

From a generalization point of view we don't care that the square wave is a
random bonus option the part happens to be able to kick out. Ideally we
want a naming scheme that makes it's relationship to the other waveforms
apparent though and it's not entirely obvious how to handle that (e.g. how to handle the double frequency square wave this part can produce). Things will get more fun if parts have multiple outputs tied to one dds channel.

Actually that's the case here on the AD9834. The signbit is an independent output. It therefore can't be handled with the dds0_wavetype file. It can be enabled/disabled while the DDS generates a sine wave on the main = output.

On the AD9833 the signbit square wave feature is routed to the DDS main out= put.

Quoted 5 lines

I'd also like to see the docs including the current options and a description
of each. It's fairly obvious for those we have here, but there are going
to be less simple cases in future!

Will add docs once we all agree on the userspace abi.

Quoted 9 lines

drivers/staging/iio/dds/dds.h

Quoted 7 lines
drivers/staging/iio/dds/dds.h new file +7 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/available_output_modes
+ * Description:
+ * Lists all available output waveform modes
+ */
+#define IIO_CONST_ATTR_AVAILABLE_OUTPUT_MODES(_modes)
      \
Quoted 1 line
drivers/staging/iio/dds/dds.h new file +1 -0
+   IIO_CONST_ATTR(available_output_modes, _modes);
#3 lore raw
Quoted 26 lines · Michael
Quoted 26 lines

From: Jonathan Cameron [mailto:[email protected]]

Quoted 6 lines · On 11/29/10 15:51, [email protected] wrote:

From: Michael Hennerich <[email protected]>

staging: iio: dds: ad9834: add missing include file

staging: iio: dds: ad9834: fix typo

Hi Michael,

Thanks for this driver and in particular the first thoughts on a suitable abi design for these parts. There are a few nitpicks in the driver body that will be trivial to cleanup.

The abi needs more open discussion. I have inserted a few suggestions here, but I would also like to see it formally proposed as an RFC separate from the driver to linux-iio and lkml. It's a fairly substantial new abi that we want to try and get right. Given similar postings of IIO abi's in the past the lkml posting may fall on deaf ears, but then at least you can use it to bash latecomers over the head with when they disagree with your interface 6 months down the line... It won't help but it always makes me feel better and we might get a few more people who did read it at the time coming in on our side if we are lucky :)

Quoted 7 lines
Quoted 3 lines

Just wait... Someone will want to use a nice general DDS as a clock input for some other part and hence ask about in kernel interfaces...

Hi Jonathan,

That's something we can add later.

:)

Quoted 40 lines
Quoted 27 lines

Thanks,

Jonathan

Quoted 6 lines
---
 drivers/staging/iio/dds/Kconfig  |   11 +-
 drivers/staging/iio/dds/Makefile |    1 +
 drivers/staging/iio/dds/ad9834.c |  348

++++++++++++++++++++++++++++++++++++++

Review

Quoted 8 lines
 drivers/staging/iio/dds/ad9834.h |  108 ++++++++++++
 drivers/staging/iio/dds/dds.h    |   99 +++++++++++
 5 files changed, 565 insertions(+), 2 deletions(-)
 create mode 100644 drivers/staging/iio/dds/ad9834.c
 create mode 100644 drivers/staging/iio/dds/ad9834.h
 create mode 100644 drivers/staging/iio/dds/dds.h
(quoted diff) +0 -0

b/drivers/staging/iio/dds/Kconfig

Quoted 6 lines

index d045ed6..4c9cce3 100644

drivers/staging/iio/dds/Kconfig +0 -0
@@ -11,11 +11,18 @@ config AD5930
      ad5930/ad5932, provides direct access via sysfs.
 
Quoted 11 lines

drivers/staging/iio/dds/Kconfig config AD5930 · line 18

Quoted 7 lines
drivers/staging/iio/dds/Kconfig +1 -2
 config AD9832
-   tristate "Analog Devices ad9832/3/4/5 driver"
+   tristate "Analog Devices ad9832/5 driver"
    depends on SPI
    help
      Say yes here to build support for Analog Devices DDS chip
-     ad9832/3/4/5, provides direct access via sysfs.

This is somewhat 'interesting'. Please add to commit comment to say whether this change was due to only partial support existing in the 'old' driver or if it never worked in the first place for these parts.

Actually support for the ADP9833/34 never existed in the AD9832 driver.

...

Quoted 9 lines
Quoted 7 lines

drivers/staging/iio/dds/Kconfig

Quoted 5 lines
drivers/staging/iio/dds/Kconfig +5 -0
+   struct iio_dev *dev_info = dev_get_drvdata(dev);
+   struct ad9834_state *st = dev_info->dev_data;
+   int ret;
+
+   mutex_lock(&dev_info->mlock);

Use the sysfs_streq instead for these? (avoids possible white space issues etc).

Thanks - sysfs_streq is much better.

I only came across that one whilst reviewing someone elses code the other day. Very handy little function.
...

Quoted 9 lines
Quoted 8 lines

drivers/staging/iio/dds/Kconfig

Quoted 3 lines
drivers/staging/iio/dds/Kconfig +3 -0
+static const struct spi_device_id ad9834_id[] = {
+   {"ad9833", ID_AD9833},
+   {"ad9834", ID_AD9834},

I don't think you ever use the enum values. If you don't and there is not likely to be a need anytime soon, it would be a little cleaner not to specify them. That would make it clear the parts are identical as far as the driver is concerned.

Quoted 3 lines

The AD9833 is a bit different from the AD9834.
In the patch I'm going to send out shortly device_id is used in a few places.

That's fine then.

...

Quoted 8 lines
Quoted 4 lines

Good to see so much documentation. I would ask that this also exists in the Documentation directory. Given there is so little overlap in attributes between this and our existing elements, it probably makes sense to add a new file called something like sysfs-bus-iio-dds.

I'll create this file after posting the RFC.
I don't want to update more or less the same documentation, in multiple places.

Sure. I'd personally separate out the docs from here into the file and base the discussion on that. We don't currently have anywhere near this good a set of docs in the headers for the other types whereas we do have fairly thorough documentation files and they may come up in the discussion.

Quoted 7 lines
Quoted 5 lines

I also think this interface is worth posting (in that form) as an RFC to
lkml. Note Greg just merged a big cleanup of the existing docs so that should be in linux-next etc tomorrow.

Shall I just post the dds.h file as patch?

Could do, though then I suspect most of the discussion will be about the implementation rather than the actual abi. It would probably be easier to do it with a straight documentation file perhaps with the dds.h and the driver provided as later elements of the series (basically as an example of use),

Quoted 149 lines
Quoted 20 lines

I am going to read this pretty much without referring to the datasheet so as to see how it might read to someone coming in with a different dds device.

drivers/staging/iio/dds/Kconfig

Quoted 6 lines
drivers/staging/iio/dds/Kconfig +6 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/freqX
+ * Description:
+ * Stores frequency into tuning word register X.
+ * There can be more than one freqX file, which allows for pin

controlled FSK

Quoted 1 line

+ * Frequency Shift Keying (en_pincontrol is active) or the user can

control

drivers/staging/iio/dds/Kconfig

Quoted 4 lines

+ * the desired active tuning word by writing X to the en_freq file.
+ */

drivers/staging/iio/dds/Kconfig +2 -0
+
+#define IIO_DEV_ATTR_FREQ(_num, _store, _addr)
      \

drivers/staging/iio/dds/Kconfig

Quoted 2 lines
drivers/staging/iio/dds/Kconfig +2 -0
+   IIO_DEVICE_ATTR(freq##_num, S_IWUSR, NULL, _store, _addr)
+
Quoted 7 lines

Do we need a base name for these? If we have a device that combines a dds
element with other IIO elements things could get rather confusing. This is
also true if we have a single device with multiple dds channels (perhaps
a more common situation).

Good catch, in fact there is a quad channel device, featuring 4 independent dds outputs.

Quoted 9 lines

Perhaps somthing like...

ddsX_freqY
ddsX_freq_en
ddsX_phaseY
ddsX_phase_en
ddsX_phase_scale
ddsX_phaseY_scale
ddsX_phase_en

Looks good!

Quoted 10 lines
Quoted 5 lines

+/**
+ * /sys/bus/iio/devices/deviceX/freq_en
+ * Description:
+ * Specifies the active output frequency tuning word.
+ * To exit this mode the user can write pincontrol_en or disable

file.

Quoted 1 line

+ */

Based on the description I didn't really follow this one. Looking at what the code does, it's a simple input for of the current FSK symbol?

Yes - writing this file selects the frequency tuning word.
It basically overwrites the pin control mechanism.
This can be useful when pin control is not used, and you alternatingly update dds0_freq0 and dds0_freq1 with new values. The chip then ensures that the new frequency value get's updates during the zero cross. Which avoids a lot of unwanted distortions.

Quoted 3 lines

_en is not a good name for it. I'd go with ddsX_freqsymbol or something like
that. The value then corresponds to the Y in ddsX_freqY.

ok

Quoted 9 lines

drivers/staging/iio/dds/Kconfig

Quoted 6 lines
drivers/staging/iio/dds/Kconfig +6 -0
+
+#define IIO_DEV_ATTR_FREQ_EN(_store, _addr)                                \
+   IIO_DEVICE_ATTR(freq_en, S_IWUSR, NULL, _store, _addr);
+
+#define IIO_CONST_ATTR_FREQ_SCALE(_string)         \
+   IIO_CONST_ATTR(freq_scale, _string)

This should probably be under the freqX element as that is what it applies
to. I'd like to see some explanation.

Added

Quoted 40 lines

drivers/staging/iio/dds/Kconfig

Quoted 6 lines
drivers/staging/iio/dds/Kconfig +6 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/phaseX
+ * Description:
+ * Stores phase into phase register X.
+ * There can be more than one phaseX file, which allows for pin

controlled PSK

Quoted 1 line

+ * Phase Shift Keying (en_pincontrol is active) or the user can

control

Quoted 1 line

+ * the desired phase X which is added to the phase accumulator

output

drivers/staging/iio/dds/Kconfig

Quoted 4 lines

+ * by writing X to the en_phase file.
+ */

drivers/staging/iio/dds/Kconfig +2 -0
+
+#define IIO_DEV_ATTR_PHASE(_num, _store, _addr)
      \

drivers/staging/iio/dds/Kconfig

Quoted 6 lines
drivers/staging/iio/dds/Kconfig +6 -0
+   IIO_DEVICE_ATTR(phase##_num, S_IWUSR, NULL, _store, _addr)
+
+/**
+ * /sys/bus/iio/devices/deviceX/phase_en
+ * Description:
+ * Specifies the active phase which is added to the phase

accumulator output.

Quoted 1 line

+ * To exit this mode the user can write pincontrol_en or disable

file.

Quoted 2 lines

+ */
+

These have the same questions as above for frequency.

Quoted 12 lines
drivers/staging/iio/dds/Kconfig +12 -0
+#define IIO_DEV_ATTR_PHASE_EN(_store, _addr)                               \
+   IIO_DEVICE_ATTR(phase_en, S_IWUSR, NULL, _store, _addr);
+
+#define IIO_CONST_ATTR_PHASE_SCALE(_string)                \
+   IIO_CONST_ATTR(phase_scale, _string)
+
+
+/**
+ * /sys/bus/iio/devices/deviceX/pincontrol_en
+ * Description:
+ * The active frequency and phase is controlled by the respective
+ * phase and frequency control inputs.
Quoted 8 lines

drivers/staging/iio/dds/Kconfig

Quoted 4 lines

+ */

drivers/staging/iio/dds/Kconfig +3 -0
+
+#define IIO_DEV_ATTR_PINCONTROL_EN(_store, _addr)                  \
+   IIO_DEVICE_ATTR(pincontrol_en, S_IWUSR, NULL, _store, _addr);

Is it just about plausible we will get a device with separate pin control
for phase and frequency? (I really don't know much about these devices ;)

Yes that's the fact. There are even parts without pin control.

Quoted 7 lines

If so that naming should allow for it. Perhaps you are allowing for it here and the specific ones would be ddsX_pincontrol_phase_en
ddsX_pincontrol_freq_en
or
dds_pincontrol_en for one that covers all channels and both phase and frequency?

That's it.

Quoted 9 lines

drivers/staging/iio/dds/Kconfig

Quoted 6 lines
drivers/staging/iio/dds/Kconfig +5 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/disable
+ * Description:
+ * Disables any signal generation, the output is set to midscale

Is the midscale a feature of this part of absolutely general? i.e. do we want to enforce this by documentation or allow for some flexibility (and a means to specify if it is something else?).

Quoted 3 lines

AFAIK the midscale thing is pretty common.
But there might be parts that shut down the output completely. I'll better remove this comment.

Either that or add another attribute that tells you what value is when shut down.

Quoted 14 lines
Quoted 14 lines

drivers/staging/iio/dds/Kconfig

Quoted 13 lines

+ */

drivers/staging/iio/dds/Kconfig +10 -0
+
+#define IIO_DEV_ATTR_DISABLE(_store, _addr)                                \
+   IIO_DEVICE_ATTR(disable, S_IWUSR, NULL, _store, _addr);
+
+/**
+ * /sys/bus/iio/devices/deviceX/signbit_output_en
+ * Description:
+ * Enables an auxiliary square wave output
+ */
+#define IIO_DEV_ATTR_SIGNBIT_OUTPUT_EN(_store, _addr)
              \
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

+ IIO_DEVICE_ATTR(signbit_output_en, S_IWUSR, NULL, _store, _addr);

How standard is this?

Only a few devices feature this, maybe remove from dds.h and just have it in the driver?

I'd prefer to see it handled as described below as I think that will generalize reasonably well to similar situations.

Quoted 33 lines
Quoted 12 lines

Can we make it look more general. See next
comment.

drivers/staging/iio/dds/Kconfig

Quoted 6 lines
drivers/staging/iio/dds/Kconfig +6 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/output_mode
+ * Description:
+ * Specifies the output waveform.
+ * For a list of available output waveform modes read

available_output_modes.

Quoted 2 lines

+ */
+#define IIO_DEV_ATTR_OUTPUT_MODE(_store, _addr)

Quoted 7 lines
      \
Quoted 1 line

+ IIO_DEVICE_ATTR(output_mode, S_IWUSR, NULL, _store, _addr);

ddsX_waveform (or perhaps wavetype) seems more descriptive to me. Output mode
could cover a whole host of things!

dds0_wavetype (sine, triangle, square)

Makes sense.

Quoted 10 lines

From a generalization point of view we don't care that the square wave is a
random bonus option the part happens to be able to kick out. Ideally we
want a naming scheme that makes it's relationship to the other waveforms
apparent though and it's not entirely obvious how to handle that (e.g. how to handle the double frequency square wave this part can produce). Things will get more fun if parts have multiple outputs tied to one dds channel.

Quoted 2 lines

Actually that's the case here on the AD9834. The signbit is an independent output. It therefore can't be handled with the dds0_wavetype file.

Then have dds0_wavetype0 (sine triangle) and dds0_wavetype1 (square). Document that the second index is only required if there are multiple physical outputs from a given channel.

Quoted 3 lines

It can be enabled/disabled while the DDS generates a sine wave on the main output.

On the AD9833 the signbit square wave feature is routed to the DDS main output.

Then that part just has the dds0_wavetype (triangle sine square) as suggested above.

Quoted 8 lines
Quoted 6 lines

I'd also like to see the docs including the current options and a description
of each. It's fairly obvious for those we have here, but there are going
to be less simple cases in future!

Will add docs once we all agree on the userspace abi.

Sadly the docs are what is needed for people to understand the proposed abi. However we also need to see code for the more complex stuff to understand how it actually works. Hence I think the ideal here is a series to LKML with

[RFC 1/3] IIO: Direct digital synthesis abi documentation [RFC 2/3] IIO:dds.h convenience macros
[RFC 3/3] IIO:DDS:ad9833 / ad9834 driver

Obviously that adds some work, so you could probably get away with merging the first two as long as absolutely everything in the header is documented.

Quoted 12 lines
Quoted 10 lines

drivers/staging/iio/dds/Kconfig

Quoted 8 lines
drivers/staging/iio/dds/Kconfig +7 -0
+
+/**
+ * /sys/bus/iio/devices/deviceX/available_output_modes
+ * Description:
+ * Lists all available output waveform modes
+ */
+#define IIO_CONST_ATTR_AVAILABLE_OUTPUT_MODES(_modes)
      \
Quoted 1 line

+ IIO_CONST_ATTR(available_output_modes, _modes);

author maintainer
#4 lore raw
Quoted 9 lines

Jonathan Cameron wrote on 2010-11-30:

Quoted 8 lines
Quoted 7 lines

From a generalization point of view we don't care that the square wave is a random bonus option the part happens to be able to kick out. Ideally we want a naming scheme that makes it's relationship to the other waveforms apparent though and it's not entirely obvious how to handle that (e.g. how to handle the double frequency square wave this part can produce). Things will get more fun if parts have multiple outputs tied to one dds channel.

Quoted 6 lines
Quoted 2 lines

Actually that's the case here on the AD9834. The signbit is an independent output. It therefore can't be handled with the

dds0_wavetype file.
Then have dds0_wavetype0 (sine triangle) and dds0_wavetype1 (square). Document that the second index is only required if there are multiple physical outputs from a given channel.

How to disable dds0_wavetype1 (square)?

(square off)?

Not all combinations of dds0_wavetype0 (sine triangle) are allowed with the=
signbit output.

Quoted 7 lines
Quoted 4 lines

It can be enabled/disabled while the DDS generates a sine wave on the main output.

On the AD9833 the signbit square wave feature is routed to the DDS

main output.
Then that part just has the dds0_wavetype (triangle sine square) as suggested above.

That's how I implemented it for the AD9833

Greetings,
Michael

--
Analog Devices GmbH Wilhelm-Wagenfeld-Str. 6 80807 Muenchen
Sitz der Gesellschaft Muenchen, Registergericht Muenchen HRB 4036 Geschaeftsfuehrer Thomas Wessel, William A. Martin, Margaret Seif

#5 lore raw
Quoted 11 lines · Michael
Quoted 11 lines

Jonathan Cameron wrote on 2010-11-30:

Quoted 10 lines
Quoted 7 lines

From a generalization point of view we don't care that the square wave is a random bonus option the part happens to be able to kick out. Ideally we want a naming scheme that makes it's relationship to the other waveforms apparent though and it's not entirely obvious how to handle that (e.g. how to handle the double frequency square wave this part can produce). Things will get more fun if parts have multiple outputs tied to one dds channel.

Actually that's the case here on the AD9834. The signbit is an independent output. It therefore can't be handled with the

Quoted 8 lines
Quoted 4 lines

dds0_wavetype file.
Then have dds0_wavetype0 (sine triangle) and dds0_wavetype1 (square). Document that the second index is only required if there are multiple physical outputs from a given channel.

How to disable dds0_wavetype1 (square)?

(square off)?

Nope, it's just like the other output in that it can be disabled so give it it's own disable attribute. The naming currently proposed looks a little strange for this... hmm. Perhaps change to dds0_out1_wavetype etc for the other elements.
dds0_out_disable (for both) and dds0_out1_disable allows this channel to be separately disabled.

Quoted 2 lines

Not all combinations of dds0_wavetype0 (sine triangle) are allowed with the signbit output.

That's fine. You have dds0_out1_wavtype_available and dds0_out0_wavetype_available to say what is possible for each of the two outputs of dds0.

Quoted 11 lines
Quoted 9 lines
Quoted 6 lines

It can be enabled/disabled while the DDS generates a sine wave on the main output.

On the AD9833 the signbit square wave feature is routed to the DDS

main output.
Then that part just has the dds0_wavetype (triangle sine square) as suggested above.

That's how I implemented it for the AD9833

Excellent, I had missed that.

Quoted 11 lines

Greetings,
Michael

--
Analog Devices GmbH Wilhelm-Wagenfeld-Str. 6 80807 Muenchen
Sitz der Gesellschaft Muenchen, Registergericht Muenchen HRB 4036 Geschaeftsfuehrer Thomas Wessel, William A. Martin, Margaret Seif