Skip to contentSkip to conversation

iio:ad7923: Return error if we didn't get the expected result

PATCH In review Lars-Peter Clausen 2 participants 4 messages Mar 4, 2013 – Mar 17, 2013

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

PATCH 1/3 iio:ad7923: Return error if we didn't get the expected result

Instead of leaving 'val' uninitialized return an error if the result's address did not match that of the channel we were trying to read.

Cc Patrick Vasseur <[email protected]>
Cc Christophe Leroy <[email protected]>
---
 drivers/iio/adc/ad7923.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/ad7923.c +2 -0
index 766c740..36eee24 100644
@@ -199,6 +199,8 @@ static int ad7923_read_raw(struct iio_dev *indio_dev,
 
 		if (chan->address == EXTRACT(ret, 12, 4))
 			*val = EXTRACT(ret, 0, 12);
+		else
+			return -EIO;
 
 		return IIO_VAL_INT;
 	}
Signature · 1 line
1.8.0
#1 lore raw

PATCH 2/3 iio:ad7923: Implement scale reporting

The driver already claims to support scale reporting in its channel spec, but doesn't actually implement this yet. This patch uses the regulator API to get the reference voltage and calculates the scale based on that. The patch also moves the global configuration bits into a field in the ad7923_state struct, since depending on the RANGE bit, the range goes either from 0 to VREF or from 0 to 2 * VREF. So we need to know the setting of the RANGE bit when calculating the scale.

Cc Patrick Vasseur <[email protected]>
Cc Christophe Leroy <[email protected]>

---
Note, the patch assumes that the unit for voltages is millivolt.

---
 drivers/iio/adc/ad7923.c | 60 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 53 insertions(+), 7 deletions(-)
drivers/iio/adc/ad7923.c +53 -7
index 36eee24..11ccc42 100644
@@ -12,6 +12,7 @@
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 #include <linux/spi/spi.h>
+#include <linux/regulator/consumer.h>
 #include <linux/err.h>
 #include <linux/delay.h>
 #include <linux/module.h>
@@ -56,6 +57,11 @@ struct ad7923_state {
 	struct spi_transfer		scan_single_xfer[2];
 	struct spi_message		ring_msg;
 	struct spi_message		scan_single_msg;
+
+	struct regulator		*reg;
+
+	unsigned int			settings;
+
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
@@ -100,10 +106,9 @@ static int ad7923_update_scan_mode(struct iio_dev *indio_dev,
 
 	len = 0;
 	for_each_set_bit(i, active_scan_mask, AD7923_MAX_CHAN) {
-		cmd = AD7923_WRITE_CR | AD7923_CODING | AD7923_RANGE |
-			AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS) |
+		cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(i) |
 			AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
-			AD7923_CHANNEL_WRITE(i);
+			st->settings;
 		cmd <<= AD7923_SHIFT_REGISTER;
 		st->tx_buf[len++] = cpu_to_be16(cmd);
 	}
@@ -163,9 +168,9 @@ static int ad7923_scan_direct(struct ad7923_state *st, unsigned ch)
 {
 	int ret, cmd;
 
-	cmd = AD7923_WRITE_CR | AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS) |
-		AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) | AD7923_CODING |
-		AD7923_CHANNEL_WRITE(ch) | AD7923_RANGE;
+	cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(ch) |
+		AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
+		st->settings;
 	cmd <<= AD7923_SHIFT_REGISTER;
 	st->tx_buf[0] = cpu_to_be16(cmd);
 
@@ -176,6 +181,22 @@ static int ad7923_scan_direct(struct ad7923_state *st, unsigned ch)
 	return be16_to_cpu(st->rx_buf[0]);
 }
 
+static int ad7923_get_range(struct ad7923_state *st)
+{
+	int vref;
+
+	vref = regulator_get_voltage(st->reg);
+	if (vref < 0)
+		return vref;
+
+	vref /= 1000;
+
+	if (!(st->settings & AD7923_RANGE))
+		vref *= 2;
+
+	return vref;
+}
+
 static int ad7923_read_raw(struct iio_dev *indio_dev,
 			   struct iio_chan_spec const *chan,
 			   int *val,
@@ -203,6 +224,13 @@ static int ad7923_read_raw(struct iio_dev *indio_dev,
 			return -EIO;
 
 		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		ret = ad7923_get_range(st);
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		*val2 = chan->scan_type.realbits;
+		return IIO_VAL_FRACTIONAL_LOG2;
 	}
 	return -EINVAL;
 }
@@ -227,6 +255,8 @@ static int ad7923_probe(struct spi_device *spi)
 	spi_set_drvdata(spi, indio_dev);
 
 	st->spi = spi;
+	st->settings = AD7923_CODING | AD7923_RANGE |
+			AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS);
 
 	indio_dev->name = spi_get_device_id(spi)->name;
 	indio_dev->dev.parent = &spi->dev;
@@ -247,10 +277,19 @@ static int ad7923_probe(struct spi_device *spi)
 	spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
 	spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
 
+	st->reg = regulator_get(&spi->dev, "refin");
+	if (IS_ERR(st->reg)) {
+		ret = PTR_ERR(st->reg);
+		goto error_free;
+	}
+	ret = regulator_enable(st->reg);
+	if (ret)
+		goto error_put_reg;
+
 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
 			&ad7923_trigger_handler, NULL);
 	if (ret)
-		goto error_free;
+		goto error_disable_reg;
 
 	ret = iio_device_register(indio_dev);
 	if (ret)
@@ -260,6 +299,10 @@ static int ad7923_probe(struct spi_device *spi)
 
 error_cleanup_ring:
 	iio_triggered_buffer_cleanup(indio_dev);
+error_disable_reg:
+	regulator_disable(st->reg);
+error_put_reg:
+	regulator_put(st->reg);
 error_free:
 	iio_device_free(indio_dev);
 
@@ -269,9 +312,12 @@ error_free:
 static int ad7923_remove(struct spi_device *spi)
 {
 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
+	struct ad7923_state *st = iio_priv(indio_dev);
 
 	iio_device_unregister(indio_dev);
 	iio_triggered_buffer_cleanup(indio_dev);
+	regulator_disable(st->reg);
+	regulator_put(st->reg);
 	iio_device_free(indio_dev);
 
 	return 0;
Signature · 1 line
1.8.0
#2 lore raw

PATCH 3/3 iio:adc:ad7923: Add support for the ad7904/ad7914/ad7924

In reply to Lars-Peter Clausen

The ad7924 is software compatible with the ad7923. The ad7904 and ad7914 are the 8 and 10 bit version of the ad7924.

While we are at it also drop the "with temperature sensor" from the Kconfig entry, since the chips do not have a temperature sensor.

Cc Patrick Vasseur <[email protected]>
Cc Christophe Leroy <[email protected]>
---
 drivers/iio/adc/Kconfig  |  6 ++---
 drivers/iio/adc/ad7923.c | 63 ++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 53 insertions(+), 16 deletions(-)
drivers/iio/adc/Kconfig +3 -3
index a40d3c2..3f05834 100644
@@ -31,13 +31,13 @@ config AD7298
 	  module will be called ad7298.
 
 config AD7923
-	tristate "Analog Devices AD7923 ADC driver"
+	tristate "Analog Devices AD7923 and similar ADCs driver"
 	depends on SPI
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 	help
-	  Say yes here to build support for Analog Devices AD7923
-	  4 Channel ADC with temperature sensor.
+	  Say yes here to build support for Analog Devices
+	  AD7904, AD7914, AD7923, AD7924 4 Channel ADCs.
 
 	  To compile this driver as a module, choose M here: the
 	  module will be called ad7923.
drivers/iio/adc/ad7923.c +50 -13

109 lines not on this page · raw message

Signature · 1 line
1.8.0
maintainer
#3 lore raw

Re: [PATCH 3/3] iio:adc:ad7923: Add support for the ad7904/ad7914/ad7924

Quoted 9 lines · Lars-Peter Clausen

The ad7924 is software compatible with the ad7923. The ad7904 and ad7914 are the 8 and 10 bit version of the ad7924.

While we are at it also drop the "with temperature sensor" from the Kconfig entry, since the chips do not have a temperature sensor.

Cc Patrick Vasseur <[email protected]>
Cc Christophe Leroy <[email protected]>

All three applied to togreg branch of iio.git

Thanks.

Quoted 147 lines
---
 drivers/iio/adc/Kconfig  |  6 ++---
 drivers/iio/adc/ad7923.c | 63 ++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 53 insertions(+), 16 deletions(-)
drivers/iio/adc/Kconfig +3 -3
index a40d3c2..3f05834 100644
@@ -31,13 +31,13 @@ config AD7298
 	  module will be called ad7298.
 
 config AD7923
-	tristate "Analog Devices AD7923 ADC driver"
+	tristate "Analog Devices AD7923 and similar ADCs driver"
 	depends on SPI
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 	help
-	  Say yes here to build support for Analog Devices AD7923
-	  4 Channel ADC with temperature sensor.
+	  Say yes here to build support for Analog Devices
+	  AD7904, AD7914, AD7923, AD7924 4 Channel ADCs.
 
 	  To compile this driver as a module, choose M here: the
 	  module will be called ad7923.
drivers/iio/adc/ad7923.c +50 -13
index 11ccc42..97fa0d3 100644
@@ -1,5 +1,5 @@
 /*
- * AD7923 SPI ADC driver
+ * AD7904/AD7914/AD7923/AD7924 SPI ADC driver
  *
  * Copyright 2011 Analog Devices Inc (from AD7923 Driver)
  * Copyright 2012 CS Systemes d'Information
@@ -70,7 +70,18 @@ struct ad7923_state {
 	__be16				tx_buf[4];
 };
 
-#define AD7923_V_CHAN(index)						\
+struct ad7923_chip_info {
+	const struct iio_chan_spec *channels;
+	unsigned int num_channels;
+};
+
+enum ad7923_id {
+	AD7904,
+	AD7914,
+	AD7924,
+};
+
+#define AD7923_V_CHAN(index, bits)					\
 	{								\
 		.type = IIO_VOLTAGE,					\
 		.indexed = 1,						\
@@ -81,18 +92,38 @@ struct ad7923_state {
 		.scan_index = index,					\
 		.scan_type = {						\
 			.sign = 'u',					\
-			.realbits = 12,					\
+			.realbits = (bits),				\
 			.storagebits = 16,				\
 			.endianness = IIO_BE,				\
 		},							\
 	}
 
-static const struct iio_chan_spec ad7923_channels[] = {
-	AD7923_V_CHAN(0),
-	AD7923_V_CHAN(1),
-	AD7923_V_CHAN(2),
-	AD7923_V_CHAN(3),
-	IIO_CHAN_SOFT_TIMESTAMP(4),
+#define DECLARE_AD7923_CHANNELS(name, bits) \
+const struct iio_chan_spec name ## _channels[] = { \
+	AD7923_V_CHAN(0, bits), \
+	AD7923_V_CHAN(1, bits), \
+	AD7923_V_CHAN(2, bits), \
+	AD7923_V_CHAN(3, bits), \
+	IIO_CHAN_SOFT_TIMESTAMP(4), \
+}
+
+static DECLARE_AD7923_CHANNELS(ad7904, 8);
+static DECLARE_AD7923_CHANNELS(ad7914, 10);
+static DECLARE_AD7923_CHANNELS(ad7924, 12);
+
+static const struct ad7923_chip_info ad7923_chip_info[] = {
+	[AD7904] = {
+		.channels = ad7904_channels,
+		.num_channels = ARRAY_SIZE(ad7904_channels),
+	},
+	[AD7914] = {
+		.channels = ad7914_channels,
+		.num_channels = ARRAY_SIZE(ad7914_channels),
+	},
+	[AD7924] = {
+		.channels = ad7924_channels,
+		.num_channels = ARRAY_SIZE(ad7924_channels),
+	},
 };
 
 /**
@@ -245,6 +276,7 @@ static int ad7923_probe(struct spi_device *spi)
 {
 	struct ad7923_state *st;
 	struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
+	const struct ad7923_chip_info *info;
 	int ret;
 
 	if (indio_dev == NULL)
@@ -258,11 +290,13 @@ static int ad7923_probe(struct spi_device *spi)
 	st->settings = AD7923_CODING | AD7923_RANGE |
 			AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS);
 
+	info = &ad7923_chip_info[spi_get_device_id(spi)->driver_data];
+
 	indio_dev->name = spi_get_device_id(spi)->name;
 	indio_dev->dev.parent = &spi->dev;
 	indio_dev->modes = INDIO_DIRECT_MODE;
-	indio_dev->channels = ad7923_channels;
-	indio_dev->num_channels = ARRAY_SIZE(ad7923_channels);
+	indio_dev->channels = info->channels;
+	indio_dev->num_channels = info->num_channels;
 	indio_dev->info = &ad7923_info;
 
 	/* Setup default message */
@@ -324,7 +358,10 @@ static int ad7923_remove(struct spi_device *spi)
 }
 
 static const struct spi_device_id ad7923_id[] = {
-	{"ad7923", 0},
+	{"ad7904", AD7904},
+	{"ad7914", AD7914},
+	{"ad7923", AD7924},
+	{"ad7924", AD7924},
 	{}
 };
 MODULE_DEVICE_TABLE(spi, ad7923_id);
@@ -342,5 +379,5 @@ module_spi_driver(ad7923_driver);
 
 MODULE_AUTHOR("Michael Hennerich <[email protected]>");
 MODULE_AUTHOR("Patrick Vasseur <[email protected]>");
-MODULE_DESCRIPTION("Analog Devices AD7923 ADC");
+MODULE_DESCRIPTION("Analog Devices AD7904/AD7914/AD7923/AD7924 ADC");
 MODULE_LICENSE("GPL v2");