Skip to contentSkip to conversation

iio: add ad5761 DAC driver

PATCH Superseded Ricardo Ribalda Delgado 2 participants 2 messages Jan 5, 2016

Subsystems (2)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: add ad5761 DAC driver

ad5761 is a 1-channel DAC with configurable output range. The driver uses the regulator interface for its voltage ref.

It shares its register layout with ad5761r, ad5721 and ad5721r.

Differences:
ad5761* are 16 bit, ad5721* are 12 bits.
ad57*1r have an internal reference.

---

v3: Move programmable voltage range to pdata
Exit on error instead of using internal voltage reference
Thanks to Jonathan Cameron <[email protected]>

v2: A lot of CodeStyle Fixes
Thanks to Peter Meerwald-Stadler <[email protected]> and Jonathan Cameron <[email protected]>

 CREDITS                        |   1 +
 drivers/iio/dac/Kconfig        |  10 +
 drivers/iio/dac/Makefile       |   1 +
 drivers/iio/dac/ad5761.c       | 413 +++++++++++++++++++++++++++++++++++++++++
 include/linux/iio/dac/ad5761.h |  36 ++++
 5 files changed, 461 insertions(+)
 create mode 100644 drivers/iio/dac/ad5761.c
 create mode 100644 include/linux/iio/dac/ad5761.h
CREDITS +1 -0
index 8207cc62ee9d..44ea433d70a1 100644
@@ -3035,6 +3035,7 @@ D: PLX USB338x driver
 D: PCA9634 driver
 D: Option GTM671WFS
 D: Fintek F81216A
+D: AD5761 iio driver
 D: Various kernel hacks
 S: Qtechnology A/S
 S: Valby Langgade 142
drivers/iio/dac/Kconfig +10 -0
index e701e28fb1cd..4caedd671360 100644
@@ -111,6 +111,16 @@ config AD5755
 	  To compile this driver as a module, choose M here: the
 	  module will be called ad5755.
 
+config AD5761
+	tristate "Analog Devices AD5761/61R/21/21R DAC driver"
+	depends on SPI_MASTER
+	help
+	  Say yes here to build support for Analog Devices AD5761, AD5761R, AD5721,
+	  AD5721R Digital to Analog Converter.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad5761.
+
 config AD5764
 	tristate "Analog Devices AD5764/64R/44/44R DAC driver"
 	depends on SPI_MASTER
drivers/iio/dac/Makefile +1 -0
index 63ae05633e0c..cb525b53fc7b 100644
@@ -12,6 +12,7 @@ obj-$(CONFIG_AD5504) += ad5504.o
 obj-$(CONFIG_AD5446) += ad5446.o
 obj-$(CONFIG_AD5449) += ad5449.o
 obj-$(CONFIG_AD5755) += ad5755.o
+obj-$(CONFIG_AD5761) += ad5761.o
 obj-$(CONFIG_AD5764) += ad5764.o
 obj-$(CONFIG_AD5791) += ad5791.o
 obj-$(CONFIG_AD5686) += ad5686.o
drivers/iio/dac/ad5761.c new file +413 -0

413 lines not on this page · raw message

include/linux/iio/dac/ad5761.h new file +36 -0
new file mode 100644
index 000000000000..3b95a8d2de5e
@@ -0,0 +1,36 @@
+#ifndef __IIO_DAC_AD5761_H__
+#define __IIO_DAC_AD5761_H__
+
+/**
+ * enum ad5761_voltage_range - Voltage range the AD5761 is configured for.
+ * @AD5761_VOLTAGE_RANGE_M10V_10V:  -10V to  10V
+ * @AD5761_VOLTAGE_RANGE_0V_10V:      0V to  10V
+ * @AD5761_VOLTAGE_RANGE_M5V_5V:     -5V to   5V
+ * @AD5761_VOLTAGE_RANGE_M2V5_7V5: -2.5V to 7.5V
+ * @AD5761_VOLTAGE_RANGE_M3V_3V:     -3V to   3V
+ * @AD5761_VOLTAGE_RANGE_0V_16V:      0V to  16V
+ * @AD5761_VOLTAGE_RANGE_0V_20V:      0V to  20V
+ */
+
+enum ad5761_voltage_range {
+	AD5761_VOLTAGE_RANGE_M10V_10V,
+	AD5761_VOLTAGE_RANGE_0V_10V,
+	AD5761_VOLTAGE_RANGE_M5V_5V,
+	AD5761_VOLTAGE_RANGE_0V_5V,
+	AD5761_VOLTAGE_RANGE_M2V5_7V5,
+	AD5761_VOLTAGE_RANGE_M3V_3V,
+	AD5761_VOLTAGE_RANGE_0V_16V,
+	AD5761_VOLTAGE_RANGE_0V_20V,
+};
+
+/**
+ * struct ad5761_platform_data - AD5761 DAC driver platform data
+ * @voltage_range: Voltage range the AD5761 is configured for
+ */
+
+struct ad5761_platform_data {
+	enum ad5761_voltage_range voltage_range;
+};
+
+
+#endif
Signature · 1 line
2.6.4
#1 lore raw
Quoted 10 lines · Ricardo Ribalda Delgado

ad5761 is a 1-channel DAC with configurable output range. The driver uses the regulator interface for its voltage ref.

It shares its register layout with ad5761r, ad5721 and ad5721r.

Differences:
ad5761* are 16 bit, ad5721* are 12 bits.
ad57*1r have an internal reference.

Hi,

Thanks for the driver. Looks pretty good, a few comments inline.

[...]

drivers/iio/dac/ad5761.c line 62

Quoted 9 lines
drivers/iio/dac/ad5761.c new file +9 -0
+enum ad5761_range_ids {
+	MODE_M10V_10V,
+	MODE_0V_10V,
+	MODE_M5V_5V,
+	MODE_0V_5V,
+	MODE_M2V5_7V5,
+	MODE_M3V_3V,
+	MODE_0V_16V,
+	MODE_0V_20V,

Those should have the AD5761 prefix to avoid name conflicts.

Quoted 10 lines
drivers/iio/dac/ad5761.c new file +10 -0
+};
+
+static int ad5761_spi_set_range(struct ad5761_state *st,
+				enum ad5761_voltage_range range)
+{
+	u16 aux;
+	int ret;
+
+	aux = range | AD5761_CTRL_ETS;
+

drivers/iio/dac/ad5761.c line 179

Quoted 10 lines
drivers/iio/dac/ad5761.c new file +10 -0
+	if (st->use_intref)
+		aux |= AD5761_CTRL_USE_INTVREF;
+
+	ret = ad5761_spi_write(st, AD5761_ADDR_CTRL_WRITE_REG, aux);
+	if (ret)
+		return ret;
+
+	ret = ad5761_spi_write(st, AD5761_ADDR_SW_DATA_RESET, 0);
+	if (ret)
+		return ret;

The datasheet says:

When the DAC output range is reconfigured during operation, a software full reset command (see Table 26) must be written to the device before writing to the control register.

This is just the data reset and it should be moved before the range change.

Quoted 5 lines
drivers/iio/dac/ad5761.c new file +5 -0
+
+	st->range = range;
+
+	return 0;
+}

drivers/iio/dac/ad5761.c line 194

Quoted 10 lines
drivers/iio/dac/ad5761.c new file +10 -0
+
+static int ad5761_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int *val,
+			   int *val2,
+			   long mask)
+{
+	struct ad5761_state *st = iio_priv(indio_dev);
+	int ret;
+	u16 aux;

You'll need locking here to avoid concurrent access to the data field in the state struct.

Quoted 27 lines
drivers/iio/dac/ad5761.c new file +27 -0
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = ad5761_spi_read(st, AD5761_ADDR_DAC_READ, &aux);
+		if (ret)
+			return ret;
+		*val = aux >> chan->scan_type.shift;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		*val = st->vref * ad5761_range_params[st->range].m;
+		*val /= 10;
+		*val2 = chan->scan_type.realbits;
+		return IIO_VAL_FRACTIONAL_LOG2;
+	case IIO_CHAN_INFO_OFFSET:
+		*val = -(1 << chan->scan_type.realbits);
+		*val *=	ad5761_range_params[st->range].c;
+		*val /=	ad5761_range_params[st->range].m;
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int ad5761_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val,
+			    int val2,

drivers/iio/dac/ad5761.c line 231

Quoted 10 lines
drivers/iio/dac/ad5761.c new file +10 -0
+			    long mask)
+{
+	struct ad5761_state *st = iio_priv(indio_dev);
+	u16 aux;
+
+	if (mask != IIO_CHAN_INFO_RAW)
+		return -EINVAL;
+
+	aux = val << chan->scan_type.shift;
+

Same here.

Quoted 3 lines
drivers/iio/dac/ad5761.c new file +3 -0
+	return ad5761_spi_write(st, AD5761_ADDR_DAC_WRITE, aux);
+}
+

[...]

drivers/iio/dac/ad5761.c line 296

Quoted 4 lines
drivers/iio/dac/ad5761.c new file +4 -0
+	if (IS_ERR(st->vref_reg)) {
+		dev_err(&st->spi->dev,
+			"Error getting voltage reference regulator\n");
+		return -EIO;

This needs to propagate the original error, otherwise things like probed deferring wont work.

drivers/iio/dac/ad5761.c line 303

Quoted 7 lines
drivers/iio/dac/ad5761.c new file +7 -0
+	}
+
+	ret = regulator_enable(st->vref_reg);
+	if (ret) {
+		dev_err(&st->spi->dev,
+			 "Failed to enable voltage reference\n");
+		return -EIO;

Return the original error code here as well.

Quoted 14 lines
drivers/iio/dac/ad5761.c new file +14 -0
+	}
+
+	ret = regulator_get_voltage(st->vref_reg);
+	if (ret < 0) {
+		dev_err(&st->spi->dev,
+			 "Failed to get voltage reference value\n");
+		goto disable_regulator_vref;
+	}
+
+	if (ret < 2000000 || ret > 3000000) {
+		dev_warn(&st->spi->dev,
+			 "Invalid external voltage ref. value %d uV\n", ret);
+		goto disable_regulator_vref;
+	}

drivers/iio/dac/ad5761.c line 327

Quoted 10 lines
drivers/iio/dac/ad5761.c new file +10 -0
+
+	st->vref = ret / 1000;
+	st->use_intref = false;
+
+	return 0;
+
+disable_regulator_vref:
+	regulator_disable(st->vref_reg);
+	st->vref_reg = NULL;
+	return -EIO;

and here.

Quoted 17 lines
drivers/iio/dac/ad5761.c new file +17 -0
+}
+
+static int ad5761_probe(struct spi_device *spi)
+{
+	struct iio_dev *iio_dev;
+	struct ad5761_state *st;
+	int ret;
+	const struct ad5761_chip_info *chip_info =
+		&ad5761_chip_infos[spi_get_device_id(spi)->driver_data];
+	enum ad5761_voltage_range voltage_range = MODE_0V_5V;
+	struct ad5761_platform_data *pdata = dev_get_platdata(&spi->dev);
+
+	iio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
+	if (!iio_dev)
+		return -ENOMEM;
+
+	st = iio_priv(iio_dev);

drivers/iio/dac/ad5761.c line 354

Quoted 10 lines
drivers/iio/dac/ad5761.c new file +10 -0
+
+	st->spi = spi;
+	spi_set_drvdata(spi, iio_dev);
+
+	ret = ad5761_get_vref(st, chip_info);
+	if (ret)
+		return ret;
+
+	if (pdata)
+		voltage_range = pdata->voltage_range & 0x7;

What is encoded in the upper bits of pdata->voltage_range?

Quoted 22 lines
drivers/iio/dac/ad5761.c new file +22 -0
+
+	ret = ad5761_spi_set_range(st, voltage_range);
+	if (ret)
+		goto disable_regulator_err;
+
+	iio_dev->dev.parent = &spi->dev;
+	iio_dev->info = &ad5761_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = &chip_info->channel;
+	iio_dev->num_channels = 1;
+	iio_dev->name = spi_get_device_id(st->spi)->name;
+	ret = iio_device_register(iio_dev);
+	if (ret)
+		goto disable_regulator_err;
+
+	return ret;
+
+disable_regulator_err:
+	if (!IS_ERR_OR_NULL(st->vref_reg))
+		regulator_disable(st->vref_reg);
+
+	return ret;

drivers/iio/dac/ad5761.c line 386

Quoted 10 lines
drivers/iio/dac/ad5761.c new file +10 -0
+}
+
+static int ad5761_remove(struct spi_device *spi)
+{
+	struct iio_dev *iio_dev = spi_get_drvdata(spi);
+	struct ad5761_state *st = iio_priv(iio_dev);
+
+	if (!IS_ERR_OR_NULL(st->vref_reg))
+		regulator_disable(st->vref_reg);
+	iio_device_unregister(iio_dev);

First unregister, then regulator disable.

Quoted 3 lines
drivers/iio/dac/ad5761.c new file +3 -0
+
+	return 0;
+}

[...]

drivers/iio/dac/ad5761.c line 403

Quoted 4 lines
drivers/iio/dac/ad5761.c new file +4 -0
+static struct spi_driver ad5761_driver = {
+	.driver = {
+		   .name = "ad5761",
+		   .owner = THIS_MODULE,

This is no longer necessary. spi_register_driver() takes care of initializing the owner field.

Quoted 6 lines
drivers/iio/dac/ad5761.c new file +6 -0
+		   },
+	.probe = ad5761_probe,
+	.remove = ad5761_remove,
+	.id_table = ad5761_id,
+};
+module_spi_driver(ad5761_driver);

[...]