Skip to contentSkip to conversation

staging:iio:ad7476: Fix off by one error for channel shift

PATCH Superseded Lars-Peter Clausen 2 participants 20 messages Sep 7, 2012 – Sep 8, 2012

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/10 staging:iio:ad7476: Fix off by one error for channel shift

The datasheet is a bit confusing about this. It says that a dataword has 4 leading zeros, but the first zero is already put on the bus when CS is pulled low and the second zero is put on the bus on the first leading edge of SCLK, so when the first bit is sampled on the first trailing edge it will sample what the datasheet refers to as the second leading zero. Subsequently we only see 3 leading zeros in the 16 bit dataword and the result we get is shifted to the left by one bit. Fix this by adjusting the channel shift by 1.

---
 drivers/staging/iio/adc/ad7476_core.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/adc/ad7476_core.c +1 -1
index 4f6d59e..33435ed 100644
@@ -76,7 +76,7 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
 		.sign = 'u',					\
 		.realbits = bits,				\
 		.storagebits = 16,				\
-		.shift = 12 - bits,				\
+		.shift = 13 - bits,				\
 	},							\
 }
 
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 2/10 staging:iio:ad7476: Remove duplicated chip info entries

Some of the parts supported by this driver are software compatible. The difference between them is only in the operating voltage range. So we do not need extra chip info entries for them.

---
 drivers/staging/iio/adc/ad7476.h      |    4 ----
 drivers/staging/iio/adc/ad7476_core.c |   30 +++++++-----------------------
 2 files changed, 7 insertions(+), 27 deletions(-)
drivers/staging/iio/adc/ad7476.h +0 -4
index b1dd931..6085fad 100644
@@ -41,10 +41,6 @@ enum ad7476_supported_device_ids {
 	ID_AD7466,
 	ID_AD7467,
 	ID_AD7468,
-	ID_AD7475,
-	ID_AD7476,
-	ID_AD7477,
-	ID_AD7478,
 	ID_AD7495
 };
 
drivers/staging/iio/adc/ad7476_core.c +7 -23
index 33435ed..c97300b 100644
@@ -93,22 +93,6 @@ static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
 		.channel[0] = AD7476_CHAN(8),
 		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
 	},
-	[ID_AD7475] = {
-		.channel[0] = AD7476_CHAN(12),
-		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
-	},
-	[ID_AD7476] = {
-		.channel[0] = AD7476_CHAN(12),
-		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
-	},
-	[ID_AD7477] = {
-		.channel[0] = AD7476_CHAN(10),
-		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
-	},
-	[ID_AD7478] = {
-		.channel[0] = AD7476_CHAN(8),
-		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
-	},
 	[ID_AD7495] = {
 		.channel[0] = AD7476_CHAN(12),
 		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
@@ -216,13 +200,13 @@ static const struct spi_device_id ad7476_id[] = {
 	{"ad7466", ID_AD7466},
 	{"ad7467", ID_AD7467},
 	{"ad7468", ID_AD7468},
-	{"ad7475", ID_AD7475},
-	{"ad7476", ID_AD7476},
-	{"ad7476a", ID_AD7476},
-	{"ad7477", ID_AD7477},
-	{"ad7477a", ID_AD7477},
-	{"ad7478", ID_AD7478},
-	{"ad7478a", ID_AD7478},
+	{"ad7475", ID_AD7466},
+	{"ad7476", ID_AD7466},
+	{"ad7476a", ID_AD7466},
+	{"ad7477", ID_AD7467},
+	{"ad7477a", ID_AD7467},
+	{"ad7478", ID_AD7468},
+	{"ad7478a", ID_AD7468},
 	{"ad7495", ID_AD7495},
 	{}
 };
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 3/10 staging:iio:ad7476: Avoid alloc/free for each sample in buffered mode

In reply to Lars-Peter Clausen

The ad7476 driver has only support for 1 channel ADCs. So the upper limit for the buffer size is the size of one sample plus the size of the timestamp. Preallocate a buffer large enough to hold this to avoid having to allocate and free a new buffer for each sample being captured.

---
 drivers/staging/iio/adc/ad7476.h      |    5 ++++-
 drivers/staging/iio/adc/ad7476_ring.c |   13 +++----------
 2 files changed, 7 insertions(+), 11 deletions(-)
drivers/staging/iio/adc/ad7476.h +4 -1
index 6085fad..c4f1150 100644
@@ -33,8 +33,11 @@ struct ad7476_state {
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
+	 * Make the buffer large enough for one 16 bit sample and one 64 bit
+	 * aligned 64 bit timestamp.
 	 */
-	unsigned char			data[2] ____cacheline_aligned;
+	unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
+			____cacheline_aligned;
 };
 
 enum ad7476_supported_device_ids {
drivers/staging/iio/adc/ad7476_ring.c +3 -10
index 940681f..185cfde 100644
@@ -26,14 +26,9 @@ static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct ad7476_state *st = iio_priv(indio_dev);
 	s64 time_ns;
-	__u8 *rxbuf;
 	int b_sent;
 
-	rxbuf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
-	if (rxbuf == NULL)
-		goto done;
-
-	b_sent = spi_read(st->spi, rxbuf,
+	b_sent = spi_read(st->spi, st->data,
 			  st->chip_info->channel[0].scan_type.storagebits / 8);
 	if (b_sent < 0)
 		goto done;
@@ -41,13 +36,11 @@ static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
 	time_ns = iio_get_time_ns();
 
 	if (indio_dev->scan_timestamp)
-		memcpy(rxbuf + indio_dev->scan_bytes - sizeof(s64),
-			&time_ns, sizeof(time_ns));
+		((s64 *)st->data)[1] = time_ns;
 
-	iio_push_to_buffer(indio_dev->buffer, rxbuf);
+	iio_push_to_buffer(indio_dev->buffer, st->data);
 done:
 	iio_trigger_notify_done(indio_dev->trig);
-	kfree(rxbuf);
 
 	return IRQ_HANDLED;
 }
Signature · 1 line
1.7.10.4
#7 lore raw

PATCH 4/10 staging:iio:ad7476: Rework reference voltage handling

In reply to Lars-Peter Clausen

Slightly rework the reference voltage handling for the ad7476 driver. Now the only way to specify a external reference voltage is to use the regulator API, previously it was possible to use either platform_data or the regulator API. The new way is more consistent with what other drivers do.

Also do not ignore errors when requesting the regulator, since this will cope very poorly with e.g. deferred probing.

---
 drivers/staging/iio/adc/ad7476.h      |   11 +------
 drivers/staging/iio/adc/ad7476_core.c |   56 +++++++++++++++------------------
 2 files changed, 26 insertions(+), 41 deletions(-)
drivers/staging/iio/adc/ad7476.h +1 -10
index c4f1150..4ed5494 100644
@@ -10,16 +10,8 @@
 
 #define RES_MASK(bits)	((1 << (bits)) - 1)
 
-/*
- * TODO: struct ad7476_platform_data needs to go into include/linux/iio
- */
-
-struct ad7476_platform_data {
-	u16				vref_mv;
-};
-
 struct ad7476_chip_info {
-	u16				int_vref_mv;
+	unsigned int			int_vref_uv;
 	struct iio_chan_spec		channel[2];
 };
 
@@ -27,7 +19,6 @@ struct ad7476_state {
 	struct spi_device		*spi;
 	const struct ad7476_chip_info	*chip_info;
 	struct regulator		*reg;
-	u16				int_vref_mv;
 	struct spi_transfer		xfer;
 	struct spi_message		msg;
 	/*
drivers/staging/iio/adc/ad7476_core.c +25 -31

105 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#10 lore raw

PATCH 5/10 staging:iio:ad7476: Squash driver into a single file.

In reply to Lars-Peter Clausen

After the recent cleanups the buffer support is just a single 23 line function. This does not really justify a file on its own, so move it to the main driver file. And with only one source file left the header file containing the device state struct becomes superflousious so move the content of the header file to the main driver source file as well.

---
 drivers/staging/iio/adc/Makefile                   |    2 -
 .../staging/iio/adc/{ad7476_core.c => ad7476.c}    |   82 +++++++++++++++++++-
 drivers/staging/iio/adc/ad7476.h                   |   56 -------------
 drivers/staging/iio/adc/ad7476_ring.c              |   57 --------------
 4 files changed, 81 insertions(+), 116 deletions(-)
 rename drivers/staging/iio/adc/{ad7476_core.c => ad7476.c} (73%)
 delete mode 100644 drivers/staging/iio/adc/ad7476.h
 delete mode 100644 drivers/staging/iio/adc/ad7476_ring.c
drivers/staging/iio/adc/Makefile +0 -2
index ecac9a0..6b60af0 100644
@@ -17,8 +17,6 @@ ad799x-y := ad799x_core.o
 ad799x-$(CONFIG_AD799X_RING_BUFFER) += ad799x_ring.o
 obj-$(CONFIG_AD799X) += ad799x.o
 
-ad7476-y := ad7476_core.o
-ad7476-$(CONFIG_IIO_BUFFER) += ad7476_ring.o
 obj-$(CONFIG_AD7476) += ad7476.o
 
 ad7887-y := ad7887_core.o
drivers/staging/iio/adc/ad7476.c renamed from drivers/staging/iio/adc/ad7476_core.c +81 -1

89 lines not on this page · raw message

drivers/staging/iio/adc/ad7476.h deleted +0 -56

56 lines not on this page · raw message

drivers/staging/iio/adc/ad7476_ring.c deleted +0 -57

57 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#13 lore raw

PATCH 6/10 staging:iio:ad7476: Use be16_to_cpup instead of open-coding it

In reply to Lars-Peter Clausen
---
 drivers/staging/iio/adc/ad7476.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/adc/ad7476.c +1 -1
index 13aed0e..845bd28 100644
@@ -109,7 +109,7 @@ static int ad7476_scan_direct(struct ad7476_state *st)
 	if (ret)
 		return ret;
 
-	return (st->data[0] << 8) | st->data[1];
+	return be16_to_cpup((__be16 *)st->data);
 }
 
 static int ad7476_read_raw(struct iio_dev *indio_dev,
Signature · 1 line
1.7.10.4
#14 lore raw

PATCH 7/10 iio: Move ad7476 driver out of staging

In reply to Lars-Peter Clausen

The ad7476 driver is a driver for simple single channel ADCs. The driver does not export any experimental or custom ABI files nor do the static code check tools report any issues, so move the driver out of staging.

---
 drivers/iio/adc/Kconfig                |   14 ++++++++++++++
 drivers/iio/adc/Makefile               |    1 +
 drivers/{staging => }/iio/adc/ad7476.c |    0
 drivers/staging/iio/adc/Kconfig        |   14 --------------
 drivers/staging/iio/adc/Makefile       |    2 --
 5 files changed, 15 insertions(+), 16 deletions(-)
 rename drivers/{staging => }/iio/adc/ad7476.c (100%)
drivers/iio/adc/Kconfig +14 -0
index a2c5071..260e20b 100644
@@ -18,6 +18,20 @@ config AD7266
 	  Say yes here to build support for Analog Devices AD7265 and AD7266
 	  ADCs.
 
+config AD7476
+	tristate "Analog Devices AD7475/6/7/8 AD7466/7/8 and AD7495 ADC driver"
+	depends on SPI
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say yes here to build support for Analog Devices
+	  AD7475, AD7476, AD7477, AD7478, AD7466, AD7467, AD7468, AD7495
+	  SPI analog to digital converters (ADC).
+	  If unsure, say N (but it's safe to say "Y").
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad7476.
+
 config AT91_ADC
 	tristate "Atmel AT91 ADC"
 	depends on ARCH_AT91
drivers/iio/adc/Makefile +1 -0
index 5989356..17a60a8 100644
@@ -4,4 +4,5 @@
 
 obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o
 obj-$(CONFIG_AD7266) += ad7266.o
+obj-$(CONFIG_AD7476) += ad7476.o
 obj-$(CONFIG_AT91_ADC) += at91_adc.o
drivers/iio/adc/ad7476.c renamed from drivers/staging/iio/adc/ad7476.c +0 -0
similarity index 100%
rename from drivers/staging/iio/adc/ad7476.c
rename to drivers/iio/adc/ad7476.c
drivers/staging/iio/adc/Kconfig +0 -14
index a1fa172..1b4a356 100644
@@ -68,20 +68,6 @@ config AD799X_RING_BUFFER
 	  Say yes here to include ring buffer support in the AD799X
 	  ADC driver.
 
-config AD7476
-	tristate "Analog Devices AD7475/6/7/8 AD7466/7/8 and AD7495 ADC driver"
-	depends on SPI
-	select IIO_BUFFER
-	select IIO_TRIGGERED_BUFFER
-	help
-	  Say yes here to build support for Analog Devices
-	  AD7475, AD7476, AD7477, AD7478, AD7466, AD7467, AD7468, AD7495
-	  SPI analog to digital converters (ADC).
-	  If unsure, say N (but it's safe to say "Y").
-
-	  To compile this driver as a module, choose M here: the
-	  module will be called ad7476.
-
 config AD7887
 	tristate "Analog Devices AD7887 ADC driver"
 	depends on SPI
drivers/staging/iio/adc/Makefile +0 -2
index 6b60af0..62ee02e 100644
@@ -17,8 +17,6 @@ ad799x-y := ad799x_core.o
 ad799x-$(CONFIG_AD799X_RING_BUFFER) += ad799x_ring.o
 obj-$(CONFIG_AD799X) += ad799x.o
 
-obj-$(CONFIG_AD7476) += ad7476.o
-
 ad7887-y := ad7887_core.o
 ad7887-$(CONFIG_IIO_BUFFER) += ad7887_ring.o
 obj-$(CONFIG_AD7887) += ad7887.o
Signature · 1 line
1.7.10.4
#15 lore raw

PATCH 8/10 iio:ad7476: Add ad7910/ad7920 device table entries

In reply to Lars-Peter Clausen

The ad7910/ad7920 are software compatible to the ad7467/ad7466.

---
 drivers/iio/adc/Kconfig  |    9 +++++----
 drivers/iio/adc/ad7476.c |    4 +++-
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/iio/adc/Kconfig +5 -4
index 260e20b..364d2d7 100644
@@ -19,14 +19,15 @@ config AD7266
 	  ADCs.
 
 config AD7476
-	tristate "Analog Devices AD7475/6/7/8 AD7466/7/8 and AD7495 ADC driver"
+	tristate "Analog Devices AD7476 and similar 1-channel ADCs driver"
 	depends on SPI
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 	help
-	  Say yes here to build support for Analog Devices
-	  AD7475, AD7476, AD7477, AD7478, AD7466, AD7467, AD7468, AD7495
-	  SPI analog to digital converters (ADC).
+	  Say yes here to build support for Analog Devices AD7475, AD7476, AD7477,
+	  AD7478, AD7466, AD7467, AD7468, AD7495, AD7910, AD7920 SPI analog to
+	  digital converters (ADC).
+
 	  If unsure, say N (but it's safe to say "Y").
 
 	  To compile this driver as a module, choose M here: the
drivers/iio/adc/ad7476.c +3 -1
index 845bd28..1c7ae6f 100644
@@ -282,6 +282,8 @@ static const struct spi_device_id ad7476_id[] = {
 	{"ad7478", ID_AD7468},
 	{"ad7478a", ID_AD7468},
 	{"ad7495", ID_AD7495},
+	{"ad7910", ID_AD7467},
+	{"ad7920", ID_AD7466},
 	{}
 };
 MODULE_DEVICE_TABLE(spi, ad7476_id);
@@ -298,5 +300,5 @@ static struct spi_driver ad7476_driver = {
 module_spi_driver(ad7476_driver);
 
 MODULE_AUTHOR("Michael Hennerich <[email protected]>");
-MODULE_DESCRIPTION("Analog Devices AD7475/6/7/8(A) AD7466/7/8 ADC");
+MODULE_DESCRIPTION("Analog Devices AD7476 and similar 1-channel ADCs");
 MODULE_LICENSE("GPL v2");
Signature · 1 line
1.7.10.4
#16 lore raw

PATCH 9/10 iio:ad7476: Add ad7940 support

In reply to Lars-Peter Clausen

The AD7940 is a single channel 14 bit ADC similar to the ADCs already supported by the ad7476 driver, but it does have a different shift factor.

---
 drivers/iio/adc/Kconfig  |    4 ++--
 drivers/iio/adc/ad7476.c |   18 ++++++++++++++----
 2 files changed, 16 insertions(+), 6 deletions(-)
drivers/iio/adc/Kconfig +2 -2
index 364d2d7..7152a49 100644
@@ -25,8 +25,8 @@ config AD7476
 	select IIO_TRIGGERED_BUFFER
 	help
 	  Say yes here to build support for Analog Devices AD7475, AD7476, AD7477,
-	  AD7478, AD7466, AD7467, AD7468, AD7495, AD7910, AD7920 SPI analog to
-	  digital converters (ADC).
+	  AD7478, AD7466, AD7467, AD7468, AD7495, AD7910, AD7920, AD7920 SPI analog
+	  to digital converters (ADC).
 
 	  If unsure, say N (but it's safe to say "Y").
 
drivers/iio/adc/ad7476.c +14 -4
index 1c7ae6f..646df45 100644
@@ -48,7 +48,8 @@ enum ad7476_supported_device_ids {
 	ID_AD7466,
 	ID_AD7467,
 	ID_AD7468,
-	ID_AD7495
+	ID_AD7495,
+	ID_AD7940,
 };
 
 #if IS_ENABLED(CONFIG_IIO_BUFFER)
@@ -152,7 +153,7 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
 	return -EINVAL;
 }
 
-#define AD7476_CHAN(bits)					\
+#define _AD7476_CHAN(bits, _shift)				\
 	{							\
 	.type = IIO_VOLTAGE,					\
 	.indexed = 1,						\
@@ -160,12 +161,16 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
 	IIO_CHAN_INFO_SCALE_SHARED_BIT,				\
 	.scan_type = {						\
 		.sign = 'u',					\
-		.realbits = bits,				\
+		.realbits = (bits),				\
 		.storagebits = 16,				\
-		.shift = 13 - bits,				\
+		.shift = (_shift),				\
+		.endianness = IIO_BE,				\
 	},							\
 }
 
+#define AD7476_CHAN(bits) _AD7476_CHAN((bits), 13 - (bits))
+#define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits))
+
 static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
 	[ID_AD7466] = {
 		.channel[0] = AD7476_CHAN(12),
@@ -184,6 +189,10 @@ static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
 		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
 		.int_vref_uv = 2500000,
 	},
+	[ID_AD7940] = {
+		.channel[0] = AD7940_CHAN(14),
+		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
+	},
 };
 
 static const struct iio_info ad7476_info = {
@@ -284,6 +293,7 @@ static const struct spi_device_id ad7476_id[] = {
 	{"ad7495", ID_AD7495},
 	{"ad7910", ID_AD7467},
 	{"ad7920", ID_AD7466},
+	{"ad7940", ID_AD7940},
 	{}
 };
 MODULE_DEVICE_TABLE(spi, ad7476_id);
Signature · 1 line
1.7.10.4
#17 lore raw

PATCH 10/10 iio:ad7476: Add support for ad7274/ad7275/ad7276/ad7277/ad7278

In reply to Lars-Peter Clausen

The ad7276/ad7277/ad7278 are similar to the ad7476/ad7477/ad7478 but have the same number of leading zeros as the ad7940. The ad7274/ad7275 have a extra pin for VREF where as for the ad7276/ad7277/ad7278 VREF is taken from VDD, but otherwise they are compatible to the ad7276/ad7277.

---
 drivers/iio/adc/Kconfig  |    6 +++---
 drivers/iio/adc/ad7476.c |   20 ++++++++++++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)
drivers/iio/adc/Kconfig +3 -3
index 7152a49..645c052 100644
@@ -24,9 +24,9 @@ config AD7476
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 	help
-	  Say yes here to build support for Analog Devices AD7475, AD7476, AD7477,
-	  AD7478, AD7466, AD7467, AD7468, AD7495, AD7910, AD7920, AD7920 SPI analog
-	  to digital converters (ADC).
+	  Say yes here to build support for Analog Devices AD7274, AD7275, AD7276,
+	  AD7277, AD7278, AD7475, AD7476, AD7477, AD7478, AD7466, AD7467, AD7468,
+	  AD7495, AD7910, AD7920, AD7920 SPI analog to digital converters (ADC).
 
 	  If unsure, say N (but it's safe to say "Y").
 
drivers/iio/adc/ad7476.c +20 -0
index 646df45..8c7e206 100644
@@ -45,6 +45,9 @@ struct ad7476_state {
 };
 
 enum ad7476_supported_device_ids {
+	ID_AD7276,
+	ID_AD7277,
+	ID_AD7278,
 	ID_AD7466,
 	ID_AD7467,
 	ID_AD7468,
@@ -172,6 +175,18 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
 #define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits))
 
 static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
+	[ID_AD7276] = {
+		.channel[0] = AD7940_CHAN(12),
+		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
+	},
+	[ID_AD7277] = {
+		.channel[0] = AD7940_CHAN(10),
+		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
+	},
+	[ID_AD7278] = {
+		.channel[0] = AD7940_CHAN(8),
+		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
+	},
 	[ID_AD7466] = {
 		.channel[0] = AD7476_CHAN(12),
 		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
@@ -280,6 +295,11 @@ static int __devexit ad7476_remove(struct spi_device *spi)
 }
 
 static const struct spi_device_id ad7476_id[] = {
+	{"ad7274", ID_AD7276},
+	{"ad7275", ID_AD7277},
+	{"ad7276", ID_AD7276},
+	{"ad7277", ID_AD7277},
+	{"ad7278", ID_AD7278},
 	{"ad7466", ID_AD7466},
 	{"ad7467", ID_AD7467},
 	{"ad7468", ID_AD7468},
Signature · 1 line
1.7.10.4
#8 lore raw

PATCH v2 4/10 staging:iio:ad7476: Rework reference voltage handling

In reply to Lars-Peter Clausen

Slightly rework the reference voltage handling for the ad7476 driver. Now the only way to specify a external reference voltage is to use the regulator API, previously it was possible to use either platform_data or the regulator API. The new way is more consistent with what other drivers do.

Also do not ignore errors when requesting the regulator, since this will cope very poorly with e.g. deferred probing.

---
Had a uncommited change... :/

Changes since v1:
* Fix regulator error handling

---
 drivers/staging/iio/adc/ad7476.h      |   11 +------
 drivers/staging/iio/adc/ad7476_core.c |   58 +++++++++++++++------------------
 2 files changed, 27 insertions(+), 42 deletions(-)
drivers/staging/iio/adc/ad7476.h +1 -10
index c4f1150..4ed5494 100644
@@ -10,16 +10,8 @@
 
 #define RES_MASK(bits)	((1 << (bits)) - 1)
 
-/*
- * TODO: struct ad7476_platform_data needs to go into include/linux/iio
- */
-
-struct ad7476_platform_data {
-	u16				vref_mv;
-};
-
 struct ad7476_chip_info {
-	u16				int_vref_mv;
+	unsigned int			int_vref_uv;
 	struct iio_chan_spec		channel[2];
 };
 
@@ -27,7 +19,6 @@ struct ad7476_state {
 	struct spi_device		*spi;
 	const struct ad7476_chip_info	*chip_info;
 	struct regulator		*reg;
-	u16				int_vref_mv;
 	struct spi_transfer		xfer;
 	struct spi_message		msg;
 	/*
drivers/staging/iio/adc/ad7476_core.c +26 -32

106 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#3 lore raw

Re: [PATCH 03/10] staging:iio:ad7476: Avoid alloc/free for each sample in buffered mode

In reply to Lars-Peter Clausen
Quoted 36 lines · Lars-Peter Clausen

The ad7476 driver has only support for 1 channel ADCs. So the upper limit for the buffer size is the size of one sample plus the size of the timestamp. Preallocate a buffer large enough to hold this to avoid having to allocate and free a new buffer for each sample being captured.

---
 drivers/staging/iio/adc/ad7476.h      |    5 ++++-
 drivers/staging/iio/adc/ad7476_ring.c |   13 +++----------
 2 files changed, 7 insertions(+), 11 deletions(-)
drivers/staging/iio/adc/ad7476.h +4 -1
index 6085fad..c4f1150 100644
@@ -33,8 +33,11 @@ struct ad7476_state {
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
+	 * Make the buffer large enough for one 16 bit sample and one 64 bit
+	 * aligned 64 bit timestamp.
 	 */
-	unsigned char			data[2] ____cacheline_aligned;
+	unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
+			____cacheline_aligned;
 };
 
 enum ad7476_supported_device_ids {
drivers/staging/iio/adc/ad7476_ring.c +0 -0
index 940681f..185cfde 100644
@@ -26,14 +26,9 @@ static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct ad7476_state *st = iio_priv(indio_dev);
 	s64 time_ns;

drivers/staging/iio/adc/ad7476_ring.c static irqreturn_t ad7476_trigger_handler(int irq, void *p) · line 32

Quoted 10 lines
drivers/staging/iio/adc/ad7476_ring.c +1 -6
-	__u8 *rxbuf;
 	int b_sent;
 
-	rxbuf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
-	if (rxbuf == NULL)
-		goto done;
-
-	b_sent = spi_read(st->spi, rxbuf,
+	b_sent = spi_read(st->spi, st->data,
 			  st->chip_info->channel[0].scan_type.storagebits / 8);

I just noticed this can even more be simplified by using the prepared SPI message. Unfortunately this will require regeneration of a few more patches. I'll resend the whole series next week, after it had some exposure to review.

Quoted 18 lines
drivers/staging/iio/adc/ad7476_ring.c +2 -4
 	if (b_sent < 0)
 		goto done;
@@ -41,13 +36,11 @@ static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
 	time_ns = iio_get_time_ns();
 
 	if (indio_dev->scan_timestamp)
-		memcpy(rxbuf + indio_dev->scan_bytes - sizeof(s64),
-			&time_ns, sizeof(time_ns));
+		((s64 *)st->data)[1] = time_ns;
 
-	iio_push_to_buffer(indio_dev->buffer, rxbuf);
+	iio_push_to_buffer(indio_dev->buffer, st->data);
 done:
 	iio_trigger_notify_done(indio_dev->trig);
-	kfree(rxbuf);
 
 	return IRQ_HANDLED;
 }
maintainer
#19 lore raw
In reply to Lars-Peter Clausen
Quoted 8 lines · Lars-Peter Clausen

The datasheet is a bit confusing about this. It says that a dataword has 4 leading zeros, but the first zero is already put on the bus when CS is pulled low and the second zero is put on the bus on the first leading edge of SCLK, so when the first bit is sampled on the first trailing edge it will sample what the datasheet refers to as the second leading zero. Subsequently we only see 3 leading zeros in the 16 bit dataword and the result we get is shifted to the left by one bit. Fix this by adjusting the channel shift by 1.

I'll take your word for it :)

Quoted 19 lines
---
 drivers/staging/iio/adc/ad7476_core.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/adc/ad7476_core.c +1 -1
index 4f6d59e..33435ed 100644
@@ -76,7 +76,7 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
 		.sign = 'u',					\
 		.realbits = bits,				\
 		.storagebits = 16,				\
-		.shift = 12 - bits,				\
+		.shift = 13 - bits,				\
 	},							\
 }
 
maintainer
#4 lore raw

Re: [PATCH 03/10] staging:iio:ad7476: Avoid alloc/free for each sample in buffered mode

In reply to Lars-Peter Clausen
Quoted 5 lines · Lars-Peter Clausen
Quoted 4 lines · Lars-Peter Clausen

The ad7476 driver has only support for 1 channel ADCs. So the upper limit for the buffer size is the size of one sample plus the size of the timestamp. Preallocate a buffer large enough to hold this to avoid having to allocate and free a new buffer for each sample being captured.

Sensible move...

Quoted 35 lines
Quoted 35 lines
---
 drivers/staging/iio/adc/ad7476.h      |    5 ++++-
 drivers/staging/iio/adc/ad7476_ring.c |   13 +++----------
 2 files changed, 7 insertions(+), 11 deletions(-)
drivers/staging/iio/adc/ad7476.h +4 -1
index 6085fad..c4f1150 100644
@@ -33,8 +33,11 @@ struct ad7476_state {
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
+	 * Make the buffer large enough for one 16 bit sample and one 64 bit
+	 * aligned 64 bit timestamp.
 	 */
-	unsigned char			data[2] ____cacheline_aligned;
+	unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
+			____cacheline_aligned;
 };
 
 enum ad7476_supported_device_ids {
drivers/staging/iio/adc/ad7476_ring.c +0 -1
index 940681f..185cfde 100644
@@ -26,14 +26,9 @@ static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct ad7476_state *st = iio_priv(indio_dev);
 	s64 time_ns;
-	__u8 *rxbuf;
 	int b_sent;
 
Quoted 11 lines

drivers/staging/iio/adc/ad7476_ring.c static irqreturn_t ad7476_trigger_handler(int irq, void *p) · line 32

Quoted 7 lines
drivers/staging/iio/adc/ad7476_ring.c +1 -5
-	rxbuf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
-	if (rxbuf == NULL)
-		goto done;
-
-	b_sent = spi_read(st->spi, rxbuf,
+	b_sent = spi_read(st->spi, st->data,
 			  st->chip_info->channel[0].scan_type.storagebits / 8);

I just noticed this can even more be simplified by using the prepared SPI message. Unfortunately this will require regeneration of a few more patches. I'll resend the whole series next week, after it had some exposure to review.

Hmm. Guess that would be a marginal improvement. Actually from code readability I'd be tempted to drop the prepared SPI message and just do the spi_read as you have it here where that message is currently used.

Obviously it'll have some overhead though so up to you which way you go.

Quoted 25 lines
Quoted 9 lines
drivers/staging/iio/adc/ad7476_ring.c +0 -2
 	if (b_sent < 0)
 		goto done;
@@ -41,13 +36,11 @@ static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
 	time_ns = iio_get_time_ns();
 
 	if (indio_dev->scan_timestamp)
-		memcpy(rxbuf + indio_dev->scan_bytes - sizeof(s64),
-			&time_ns, sizeof(time_ns));

drivers/staging/iio/adc/ad7476_ring.c static irqreturn_t ad7476_trigger_handler(int irq, void *p) · line 46

Quoted 10 lines
drivers/staging/iio/adc/ad7476_ring.c +2 -2
+		((s64 *)st->data)[1] = time_ns;
 
-	iio_push_to_buffer(indio_dev->buffer, rxbuf);
+	iio_push_to_buffer(indio_dev->buffer, st->data);
 done:
 	iio_trigger_notify_done(indio_dev->trig);
-	kfree(rxbuf);
 
 	return IRQ_HANDLED;
 }

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#9 lore raw

Re: [PATCH 04/10] staging:iio:ad7476: Rework reference voltage handling

In reply to Lars-Peter Clausen
Quoted 8 lines · Lars-Peter Clausen

Slightly rework the reference voltage handling for the ad7476 driver. Now the only way to specify a external reference voltage is to use the regulator API, previously it was possible to use either platform_data or the regulator API. The new way is more consistent with what other drivers do.

Also do not ignore errors when requesting the regulator, since this will cope very poorly with e.g. deferred probing.

The approach of using platform data is an old one from the days when the regulator framework was relatively new and little used. Now days it probably makes sense to kill that off in the drivers that use it in favour of insisting people use a fixed regulator to do the same job...

If anyone wants to pick up the job of clearing this out of the other drivers that still have it then feel free!

Quoted 153 lines
---
 drivers/staging/iio/adc/ad7476.h      |   11 +------
 drivers/staging/iio/adc/ad7476_core.c |   56 +++++++++++++++------------------
 2 files changed, 26 insertions(+), 41 deletions(-)
drivers/staging/iio/adc/ad7476.h +1 -10
index c4f1150..4ed5494 100644
@@ -10,16 +10,8 @@
 
 #define RES_MASK(bits)	((1 << (bits)) - 1)
 
-/*
- * TODO: struct ad7476_platform_data needs to go into include/linux/iio
- */
-
-struct ad7476_platform_data {
-	u16				vref_mv;
-};
-
 struct ad7476_chip_info {
-	u16				int_vref_mv;
+	unsigned int			int_vref_uv;
 	struct iio_chan_spec		channel[2];
 };
 
@@ -27,7 +19,6 @@ struct ad7476_state {
 	struct spi_device		*spi;
 	const struct ad7476_chip_info	*chip_info;
 	struct regulator		*reg;
-	u16				int_vref_mv;
 	struct spi_transfer		xfer;
 	struct spi_message		msg;
 	/*
drivers/staging/iio/adc/ad7476_core.c +25 -31
index c97300b..42523e6 100644
@@ -40,7 +40,7 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
 {
 	int ret;
 	struct ad7476_state *st = iio_priv(indio_dev);
-	unsigned int scale_uv;
+	int scale_uv;
 
 	switch (m) {
 	case IIO_CHAN_INFO_RAW:
@@ -57,10 +57,16 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
 			RES_MASK(st->chip_info->channel[0].scan_type.realbits);
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		scale_uv = (st->int_vref_mv * 1000)
-			>> st->chip_info->channel[0].scan_type.realbits;
-		*val =  scale_uv/1000;
-		*val2 = (scale_uv%1000)*1000;
+		if (!st->chip_info->int_vref_uv) {
+			scale_uv = regulator_get_voltage(st->reg);
+			if (scale_uv < 0)
+				return scale_uv;
+		} else {
+			scale_uv = st->chip_info->int_vref_uv;
+		}
+		scale_uv >>= chan->scan_type.realbits;
+		*val =  scale_uv / 1000;
+		*val2 = (scale_uv % 1000) * 1000;
 		return IIO_VAL_INT_PLUS_MICRO;
 	}
 	return -EINVAL;
@@ -96,7 +102,7 @@ static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
 	[ID_AD7495] = {
 		.channel[0] = AD7476_CHAN(12),
 		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
-		.int_vref_mv = 2500,
+		.int_vref_uv = 2500000,
 	},
 };
 
@@ -107,10 +113,9 @@ static const struct iio_info ad7476_info = {
 
 static int __devinit ad7476_probe(struct spi_device *spi)
 {
-	struct ad7476_platform_data *pdata = spi->dev.platform_data;
 	struct ad7476_state *st;
 	struct iio_dev *indio_dev;
-	int ret, voltage_uv = 0;
+	int ret;
 
 	indio_dev = iio_device_alloc(sizeof(*st));
 	if (indio_dev == NULL) {
@@ -118,25 +123,18 @@ static int __devinit ad7476_probe(struct spi_device *spi)
 		goto error_ret;
 	}
 	st = iio_priv(indio_dev);
+	st->chip_info =
+		&ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
+
 	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);
+		ret = PTR_ERR(st->reg);
+		goto error_ret;
 	}
-	st->chip_info =
-		&ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
 
-	if (st->chip_info->int_vref_mv)
-		st->int_vref_mv = st->chip_info->int_vref_mv;
-	else if (pdata && pdata->vref_mv)
-		st->int_vref_mv = pdata->vref_mv;
-	else if (voltage_uv)
-		st->int_vref_mv = voltage_uv / 1000;
-	else
-		dev_warn(&spi->dev, "reference voltage unspecified\n");
+	ret = regulator_enable(st->reg);
+	if (ret)
+		goto error_put_reg;
 
 	spi_set_drvdata(spi, indio_dev);
 
@@ -169,11 +167,9 @@ static int __devinit ad7476_probe(struct spi_device *spi)
 error_ring_unregister:
 	ad7476_ring_cleanup(indio_dev);
 error_disable_reg:
-	if (!IS_ERR(st->reg))
-		regulator_disable(st->reg);
+	regulator_disable(st->reg);
 error_put_reg:
-	if (!IS_ERR(st->reg))
-		regulator_put(st->reg);
+	regulator_put(st->reg);
 	iio_device_free(indio_dev);
 
 error_ret:
@@ -187,10 +183,8 @@ static int __devexit ad7476_remove(struct spi_device *spi)
 
 	iio_device_unregister(indio_dev);
 	ad7476_ring_cleanup(indio_dev);
-	if (!IS_ERR(st->reg)) {
-		regulator_disable(st->reg);
-		regulator_put(st->reg);
-	}
+	regulator_disable(st->reg);
+	regulator_put(st->reg);
 	iio_device_free(indio_dev);
 
 	return 0;
maintainer
#11 lore raw

Re: [PATCH 05/10] staging:iio:ad7476: Squash driver into a single file.

In reply to Lars-Peter Clausen
Quoted 6 lines · Lars-Peter Clausen

After the recent cleanups the buffer support is just a single 23 line function. This does not really justify a file on its own, so move it to the main driver file. And with only one source file left the header file containing the device state struct becomes superflousious so move the content of the header file to the main driver source file as well.

A sensible change I think. Normally we do our best to keep all ifdefs in the header, (and hence need the two files) but here the distinction is well defined so I agree merging them all makes sense.

Quoted 247 lines
---
 drivers/staging/iio/adc/Makefile                   |    2 -
 .../staging/iio/adc/{ad7476_core.c => ad7476.c}    |   82 +++++++++++++++++++-
 drivers/staging/iio/adc/ad7476.h                   |   56 -------------
 drivers/staging/iio/adc/ad7476_ring.c              |   57 --------------
 4 files changed, 81 insertions(+), 116 deletions(-)
 rename drivers/staging/iio/adc/{ad7476_core.c => ad7476.c} (73%)
 delete mode 100644 drivers/staging/iio/adc/ad7476.h
 delete mode 100644 drivers/staging/iio/adc/ad7476_ring.c
drivers/staging/iio/adc/Makefile +0 -2
index ecac9a0..6b60af0 100644
@@ -17,8 +17,6 @@ ad799x-y := ad799x_core.o
 ad799x-$(CONFIG_AD799X_RING_BUFFER) += ad799x_ring.o
 obj-$(CONFIG_AD799X) += ad799x.o
 
-ad7476-y := ad7476_core.o
-ad7476-$(CONFIG_IIO_BUFFER) += ad7476_ring.o
 obj-$(CONFIG_AD7476) += ad7476.o
 
 ad7887-y := ad7887_core.o
drivers/staging/iio/adc/ad7476.c renamed from drivers/staging/iio/adc/ad7476_core.c +81 -1
similarity index 73%
rename from drivers/staging/iio/adc/ad7476_core.c
rename to drivers/staging/iio/adc/ad7476.c
index 42523e6..13aed0e 100644
@@ -18,8 +18,88 @@
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 #include <linux/iio/buffer.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+#define RES_MASK(bits)	((1 << (bits)) - 1)
+
+struct ad7476_chip_info {
+	unsigned int			int_vref_uv;
+	struct iio_chan_spec		channel[2];
+};
+
+struct ad7476_state {
+	struct spi_device		*spi;
+	const struct ad7476_chip_info	*chip_info;
+	struct regulator		*reg;
+	struct spi_transfer		xfer;
+	struct spi_message		msg;
+	/*
+	 * DMA (thus cache coherency maintenance) requires the
+	 * transfer buffers to live in their own cache lines.
+	 * Make the buffer large enough for one 16 bit sample and one 64 bit
+	 * aligned 64 bit timestamp.
+	 */
+	unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
+			____cacheline_aligned;
+};
+
+enum ad7476_supported_device_ids {
+	ID_AD7466,
+	ID_AD7467,
+	ID_AD7468,
+	ID_AD7495
+};
+
+#if IS_ENABLED(CONFIG_IIO_BUFFER)
+
+static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct ad7476_state *st = iio_priv(indio_dev);
+	s64 time_ns;
+	int b_sent;
+
+	b_sent = spi_read(st->spi, st->data,
+			  st->chip_info->channel[0].scan_type.storagebits / 8);
+	if (b_sent < 0)
+		goto done;
+
+	time_ns = iio_get_time_ns();
+
+	if (indio_dev->scan_timestamp)
+		((s64 *)st->data)[1] = time_ns;
+
+	iio_push_to_buffer(indio_dev->buffer, st->data);
+done:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
+{
+	return iio_triggered_buffer_setup(indio_dev, NULL,
+			&ad7476_trigger_handler, NULL);
+}
+
+static void ad7476_ring_cleanup(struct iio_dev *indio_dev)
+{
+	iio_triggered_buffer_cleanup(indio_dev);
+}
+
+#else
+
+static int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
+{
+	return 0;
+}
+
+static void ad7476_ring_cleanup(struct iio_dev *indio_dev) {}
+
+#endif
 
-#include "ad7476.h"
 
 static int ad7476_scan_direct(struct ad7476_state *st)
 {
drivers/staging/iio/adc/ad7476.h deleted +0 -56
deleted file mode 100644
index 4ed5494..0000000
@@ -1,56 +0,0 @@
-/*
- * AD7476/5/7/8 (A) SPI ADC driver
- *
- * Copyright 2010 Analog Devices Inc.
- *
- * Licensed under the GPL-2 or later.
- */
-#ifndef IIO_ADC_AD7476_H_
-#define IIO_ADC_AD7476_H_
-
-#define RES_MASK(bits)	((1 << (bits)) - 1)
-
-struct ad7476_chip_info {
-	unsigned int			int_vref_uv;
-	struct iio_chan_spec		channel[2];
-};
-
-struct ad7476_state {
-	struct spi_device		*spi;
-	const struct ad7476_chip_info	*chip_info;
-	struct regulator		*reg;
-	struct spi_transfer		xfer;
-	struct spi_message		msg;
-	/*
-	 * DMA (thus cache coherency maintenance) requires the
-	 * transfer buffers to live in their own cache lines.
-	 * Make the buffer large enough for one 16 bit sample and one 64 bit
-	 * aligned 64 bit timestamp.
-	 */
-	unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
-			____cacheline_aligned;
-};
-
-enum ad7476_supported_device_ids {
-	ID_AD7466,
-	ID_AD7467,
-	ID_AD7468,
-	ID_AD7495
-};
-
-#ifdef CONFIG_IIO_BUFFER
-int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev);
-void ad7476_ring_cleanup(struct iio_dev *indio_dev);
-#else /* CONFIG_IIO_BUFFER */
-
-static inline int
-ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
-{
-	return 0;
-}
-
-static inline void ad7476_ring_cleanup(struct iio_dev *indio_dev)
-{
-}
-#endif /* CONFIG_IIO_BUFFER */
-#endif /* IIO_ADC_AD7476_H_ */
drivers/staging/iio/adc/ad7476_ring.c deleted +0 -57
deleted file mode 100644
index 185cfde..0000000
@@ -1,57 +0,0 @@
-/*
- * Copyright 2010-2012 Analog Devices Inc.
- * Copyright (C) 2008 Jonathan Cameron
- *
- * Licensed under the GPL-2 or later.
- *
- * ad7476_ring.c
- */
-
-#include <linux/interrupt.h>
-#include <linux/device.h>
-#include <linux/kernel.h>
-#include <linux/slab.h>
-#include <linux/spi/spi.h>
-
-#include <linux/iio/iio.h>
-#include <linux/iio/buffer.h>
-#include <linux/iio/trigger_consumer.h>
-#include <linux/iio/triggered_buffer.h>
-
-#include "ad7476.h"
-
-static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
-{
-	struct iio_poll_func *pf = p;
-	struct iio_dev *indio_dev = pf->indio_dev;
-	struct ad7476_state *st = iio_priv(indio_dev);
-	s64 time_ns;
-	int b_sent;
-
-	b_sent = spi_read(st->spi, st->data,
-			  st->chip_info->channel[0].scan_type.storagebits / 8);
-	if (b_sent < 0)
-		goto done;
-
-	time_ns = iio_get_time_ns();
-
-	if (indio_dev->scan_timestamp)
-		((s64 *)st->data)[1] = time_ns;
-
-	iio_push_to_buffer(indio_dev->buffer, st->data);
-done:
-	iio_trigger_notify_done(indio_dev->trig);
-
-	return IRQ_HANDLED;
-}
-
-int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
-{
-	return iio_triggered_buffer_setup(indio_dev, NULL,
-			&ad7476_trigger_handler, NULL);
-}
-
-void ad7476_ring_cleanup(struct iio_dev *indio_dev)
-{
-	iio_triggered_buffer_cleanup(indio_dev);
-}
maintainer
#12 lore raw

Re: [PATCH 05/10] staging:iio:ad7476: Squash driver into a single file.

In reply to Lars-Peter Clausen
Quoted 6 lines · Lars-Peter Clausen

After the recent cleanups the buffer support is just a single 23 line function. This does not really justify a file on its own, so move it to the main driver file. And with only one source file left the header file containing the device state struct becomes superflousious so move the content of the header file to the main driver source file as well.

The move out of staging patch meant I looked at the kconfig entry.

This driver selects buffering anyway, so you don't need the ifdef magic.. Hence drop that in your next version making this patch obviously the correct thing to do!

Quoted 247 lines
---
 drivers/staging/iio/adc/Makefile                   |    2 -
 .../staging/iio/adc/{ad7476_core.c => ad7476.c}    |   82 +++++++++++++++++++-
 drivers/staging/iio/adc/ad7476.h                   |   56 -------------
 drivers/staging/iio/adc/ad7476_ring.c              |   57 --------------
 4 files changed, 81 insertions(+), 116 deletions(-)
 rename drivers/staging/iio/adc/{ad7476_core.c => ad7476.c} (73%)
 delete mode 100644 drivers/staging/iio/adc/ad7476.h
 delete mode 100644 drivers/staging/iio/adc/ad7476_ring.c
drivers/staging/iio/adc/Makefile +0 -2
index ecac9a0..6b60af0 100644
@@ -17,8 +17,6 @@ ad799x-y := ad799x_core.o
 ad799x-$(CONFIG_AD799X_RING_BUFFER) += ad799x_ring.o
 obj-$(CONFIG_AD799X) += ad799x.o
 
-ad7476-y := ad7476_core.o
-ad7476-$(CONFIG_IIO_BUFFER) += ad7476_ring.o
 obj-$(CONFIG_AD7476) += ad7476.o
 
 ad7887-y := ad7887_core.o
drivers/staging/iio/adc/ad7476.c renamed from drivers/staging/iio/adc/ad7476_core.c +81 -1
similarity index 73%
rename from drivers/staging/iio/adc/ad7476_core.c
rename to drivers/staging/iio/adc/ad7476.c
index 42523e6..13aed0e 100644
@@ -18,8 +18,88 @@
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 #include <linux/iio/buffer.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+#define RES_MASK(bits)	((1 << (bits)) - 1)
+
+struct ad7476_chip_info {
+	unsigned int			int_vref_uv;
+	struct iio_chan_spec		channel[2];
+};
+
+struct ad7476_state {
+	struct spi_device		*spi;
+	const struct ad7476_chip_info	*chip_info;
+	struct regulator		*reg;
+	struct spi_transfer		xfer;
+	struct spi_message		msg;
+	/*
+	 * DMA (thus cache coherency maintenance) requires the
+	 * transfer buffers to live in their own cache lines.
+	 * Make the buffer large enough for one 16 bit sample and one 64 bit
+	 * aligned 64 bit timestamp.
+	 */
+	unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
+			____cacheline_aligned;
+};
+
+enum ad7476_supported_device_ids {
+	ID_AD7466,
+	ID_AD7467,
+	ID_AD7468,
+	ID_AD7495
+};
+
+#if IS_ENABLED(CONFIG_IIO_BUFFER)
+
+static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct ad7476_state *st = iio_priv(indio_dev);
+	s64 time_ns;
+	int b_sent;
+
+	b_sent = spi_read(st->spi, st->data,
+			  st->chip_info->channel[0].scan_type.storagebits / 8);
+	if (b_sent < 0)
+		goto done;
+
+	time_ns = iio_get_time_ns();
+
+	if (indio_dev->scan_timestamp)
+		((s64 *)st->data)[1] = time_ns;
+
+	iio_push_to_buffer(indio_dev->buffer, st->data);
+done:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
+{
+	return iio_triggered_buffer_setup(indio_dev, NULL,
+			&ad7476_trigger_handler, NULL);
+}
+
+static void ad7476_ring_cleanup(struct iio_dev *indio_dev)
+{
+	iio_triggered_buffer_cleanup(indio_dev);
+}
+
+#else
+
+static int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
+{
+	return 0;
+}
+
+static void ad7476_ring_cleanup(struct iio_dev *indio_dev) {}
+
+#endif
 
-#include "ad7476.h"
 
 static int ad7476_scan_direct(struct ad7476_state *st)
 {
drivers/staging/iio/adc/ad7476.h deleted +0 -56
deleted file mode 100644
index 4ed5494..0000000
@@ -1,56 +0,0 @@
-/*
- * AD7476/5/7/8 (A) SPI ADC driver
- *
- * Copyright 2010 Analog Devices Inc.
- *
- * Licensed under the GPL-2 or later.
- */
-#ifndef IIO_ADC_AD7476_H_
-#define IIO_ADC_AD7476_H_
-
-#define RES_MASK(bits)	((1 << (bits)) - 1)
-
-struct ad7476_chip_info {
-	unsigned int			int_vref_uv;
-	struct iio_chan_spec		channel[2];
-};
-
-struct ad7476_state {
-	struct spi_device		*spi;
-	const struct ad7476_chip_info	*chip_info;
-	struct regulator		*reg;
-	struct spi_transfer		xfer;
-	struct spi_message		msg;
-	/*
-	 * DMA (thus cache coherency maintenance) requires the
-	 * transfer buffers to live in their own cache lines.
-	 * Make the buffer large enough for one 16 bit sample and one 64 bit
-	 * aligned 64 bit timestamp.
-	 */
-	unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
-			____cacheline_aligned;
-};
-
-enum ad7476_supported_device_ids {
-	ID_AD7466,
-	ID_AD7467,
-	ID_AD7468,
-	ID_AD7495
-};
-
-#ifdef CONFIG_IIO_BUFFER
-int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev);
-void ad7476_ring_cleanup(struct iio_dev *indio_dev);
-#else /* CONFIG_IIO_BUFFER */
-
-static inline int
-ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
-{
-	return 0;
-}
-
-static inline void ad7476_ring_cleanup(struct iio_dev *indio_dev)
-{
-}
-#endif /* CONFIG_IIO_BUFFER */
-#endif /* IIO_ADC_AD7476_H_ */
drivers/staging/iio/adc/ad7476_ring.c deleted +0 -57
deleted file mode 100644
index 185cfde..0000000
@@ -1,57 +0,0 @@
-/*
- * Copyright 2010-2012 Analog Devices Inc.
- * Copyright (C) 2008 Jonathan Cameron
- *
- * Licensed under the GPL-2 or later.
- *
- * ad7476_ring.c
- */
-
-#include <linux/interrupt.h>
-#include <linux/device.h>
-#include <linux/kernel.h>
-#include <linux/slab.h>
-#include <linux/spi/spi.h>
-
-#include <linux/iio/iio.h>
-#include <linux/iio/buffer.h>
-#include <linux/iio/trigger_consumer.h>
-#include <linux/iio/triggered_buffer.h>
-
-#include "ad7476.h"
-
-static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
-{
-	struct iio_poll_func *pf = p;
-	struct iio_dev *indio_dev = pf->indio_dev;
-	struct ad7476_state *st = iio_priv(indio_dev);
-	s64 time_ns;
-	int b_sent;
-
-	b_sent = spi_read(st->spi, st->data,
-			  st->chip_info->channel[0].scan_type.storagebits / 8);
-	if (b_sent < 0)
-		goto done;
-
-	time_ns = iio_get_time_ns();
-
-	if (indio_dev->scan_timestamp)
-		((s64 *)st->data)[1] = time_ns;
-
-	iio_push_to_buffer(indio_dev->buffer, st->data);
-done:
-	iio_trigger_notify_done(indio_dev->trig);
-
-	return IRQ_HANDLED;
-}
-
-int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
-{
-	return iio_triggered_buffer_setup(indio_dev, NULL,
-			&ad7476_trigger_handler, NULL);
-}
-
-void ad7476_ring_cleanup(struct iio_dev *indio_dev)
-{
-	iio_triggered_buffer_cleanup(indio_dev);
-}
maintainer
#18 lore raw

Re: [PATCH 10/10] iio:ad7476: Add support for ad7274/ad7275/ad7276/ad7277/ad7278

In reply to Lars-Peter Clausen
Quoted 5 lines · Lars-Peter Clausen

The ad7276/ad7277/ad7278 are similar to the ad7476/ad7477/ad7478 but have the same number of leading zeros as the ad7940. The ad7274/ad7275 have a extra pin for VREF where as for the ad7276/ad7277/ad7278 VREF is taken from VDD, but otherwise they are compatible to the ad7276/ad7277.

This and all the other patches I haven't commented on look fine to me. Looking forward to the updated series.

Hehe. You did a much better job of reviewing your own patches than I did ;)

Quoted 69 lines
---
 drivers/iio/adc/Kconfig  |    6 +++---
 drivers/iio/adc/ad7476.c |   20 ++++++++++++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)
drivers/iio/adc/Kconfig +3 -3
index 7152a49..645c052 100644
@@ -24,9 +24,9 @@ config AD7476
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 	help
-	  Say yes here to build support for Analog Devices AD7475, AD7476, AD7477,
-	  AD7478, AD7466, AD7467, AD7468, AD7495, AD7910, AD7920, AD7920 SPI analog
-	  to digital converters (ADC).
+	  Say yes here to build support for Analog Devices AD7274, AD7275, AD7276,
+	  AD7277, AD7278, AD7475, AD7476, AD7477, AD7478, AD7466, AD7467, AD7468,
+	  AD7495, AD7910, AD7920, AD7920 SPI analog to digital converters (ADC).
 
 	  If unsure, say N (but it's safe to say "Y").
 
drivers/iio/adc/ad7476.c +20 -0
index 646df45..8c7e206 100644
@@ -45,6 +45,9 @@ struct ad7476_state {
 };
 
 enum ad7476_supported_device_ids {
+	ID_AD7276,
+	ID_AD7277,
+	ID_AD7278,
 	ID_AD7466,
 	ID_AD7467,
 	ID_AD7468,
@@ -172,6 +175,18 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
 #define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits))
 
 static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
+	[ID_AD7276] = {
+		.channel[0] = AD7940_CHAN(12),
+		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
+	},
+	[ID_AD7277] = {
+		.channel[0] = AD7940_CHAN(10),
+		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
+	},
+	[ID_AD7278] = {
+		.channel[0] = AD7940_CHAN(8),
+		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
+	},
 	[ID_AD7466] = {
 		.channel[0] = AD7476_CHAN(12),
 		.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
@@ -280,6 +295,11 @@ static int __devexit ad7476_remove(struct spi_device *spi)
 }
 
 static const struct spi_device_id ad7476_id[] = {
+	{"ad7274", ID_AD7276},
+	{"ad7275", ID_AD7277},
+	{"ad7276", ID_AD7276},
+	{"ad7277", ID_AD7277},
+	{"ad7278", ID_AD7278},
 	{"ad7466", ID_AD7466},
 	{"ad7467", ID_AD7467},
 	{"ad7468", ID_AD7468},
#5 lore raw

Re: [PATCH 03/10] staging:iio:ad7476: Avoid alloc/free for each sample in buffered mode

In reply to Jonathan Cameron
Quoted 14 lines · Jonathan Cameron
Quoted 5 lines · Lars-Peter Clausen
Quoted 4 lines · Lars-Peter Clausen

The ad7476 driver has only support for 1 channel ADCs. So the upper limit for the buffer size is the size of one sample plus the size of the timestamp. Preallocate a buffer large enough to hold this to avoid having to allocate and free a new buffer for each sample being captured.

Sensible move...

Quoted 7 lines
Quoted 7 lines
---
 drivers/staging/iio/adc/ad7476.h      |    5 ++++-
 drivers/staging/iio/adc/ad7476_ring.c |   13 +++----------
 2 files changed, 7 insertions(+), 11 deletions(-)

[...]

Quoted 24 lines
Quoted 12 lines
Quoted 12 lines

enum ad7476_supported_device_ids {

drivers/staging/iio/adc/ad7476_ring.c +0 -1
index 940681f..185cfde 100644
@@ -26,14 +26,9 @@ static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct ad7476_state *st = iio_priv(indio_dev);
 	s64 time_ns;
-	__u8 *rxbuf;
 	int b_sent;
 
Quoted 11 lines

drivers/staging/iio/adc/ad7476_ring.c static irqreturn_t ad7476_trigger_handler(int irq, void *p) · line 32

Quoted 7 lines
drivers/staging/iio/adc/ad7476_ring.c +1 -5
-	rxbuf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
-	if (rxbuf == NULL)
-		goto done;
-
-	b_sent = spi_read(st->spi, rxbuf,
+	b_sent = spi_read(st->spi, st->data,
 			  st->chip_info->channel[0].scan_type.storagebits / 8);

I just noticed this can even more be simplified by using the prepared SPI message. Unfortunately this will require regeneration of a few more patches. I'll resend the whole series next week, after it had some exposure to review.

Quoted 5 lines

Hmm. Guess that would be a marginal improvement. Actually from code readability I'd be tempted to drop the prepared SPI message and just do the spi_read as you have it here where that message is currently used.

Obviously it'll have some overhead though so up to you which way you go.

I think the overhead is actually negligible. But there is a pattern that emerges. For SPI based devices we usually first send the spi message which fills the transfer buffer, then store the timestamp in the last element of the transfer buffer and then send the transfer buffer to the iio buffer using iio_push_to_buffer. What differs for different devices is how exactly the message looks, but the rest of the pattern is identical. So if we define the the message outside of the trigger handler, e.g. at device init or the update_scan_mode callback we get the opportunity to have a common trigger function between a wide range of different SPI devices. Obviously it wont work for all, because some need special handling, but it will provide a good default implementation. So that's one reason why I'd like to use spi_sync here, since it will make the migration later on more obvious.

- Lars

maintainer
#6 lore raw

Re: [PATCH 03/10] staging:iio:ad7476: Avoid alloc/free for each sample in buffered mode

Quoted 46 lines · Lars-Peter Clausen
Quoted 14 lines · Jonathan Cameron
Quoted 5 lines · Lars-Peter Clausen
Quoted 4 lines · Lars-Peter Clausen

The ad7476 driver has only support for 1 channel ADCs. So the upper limit for the buffer size is the size of one sample plus the size of the timestamp. Preallocate a buffer large enough to hold this to avoid having to allocate and free a new buffer for each sample being captured.

Sensible move...

Quoted 7 lines
Quoted 7 lines
---
 drivers/staging/iio/adc/ad7476.h      |    5 ++++-
 drivers/staging/iio/adc/ad7476_ring.c |   13 +++----------
 2 files changed, 7 insertions(+), 11 deletions(-)

[...]

Quoted 24 lines
Quoted 12 lines
Quoted 12 lines

enum ad7476_supported_device_ids {

drivers/staging/iio/adc/ad7476_ring.c +0 -1
index 940681f..185cfde 100644
@@ -26,14 +26,9 @@ static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct ad7476_state *st = iio_priv(indio_dev);
 	s64 time_ns;
-	__u8 *rxbuf;
 	int b_sent;
 
Quoted 11 lines

drivers/staging/iio/adc/ad7476_ring.c static irqreturn_t ad7476_trigger_handler(int irq, void *p) · line 32

Quoted 7 lines
drivers/staging/iio/adc/ad7476_ring.c +1 -5
-	rxbuf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
-	if (rxbuf == NULL)
-		goto done;
-
-	b_sent = spi_read(st->spi, rxbuf,
+	b_sent = spi_read(st->spi, st->data,
 			  st->chip_info->channel[0].scan_type.storagebits / 8);

I just noticed this can even more be simplified by using the prepared SPI message. Unfortunately this will require regeneration of a few more patches. I'll resend the whole series next week, after it had some exposure to review.

Quoted 5 lines

Hmm. Guess that would be a marginal improvement. Actually from code readability I'd be tempted to drop the prepared SPI message and just do the spi_read as you have it here where that message is currently used.

Obviously it'll have some overhead though so up to you which way you go.

Quoted 12 lines

I think the overhead is actually negligible. But there is a pattern that emerges. For SPI based devices we usually first send the spi message which fills the transfer buffer, then store the timestamp in the last element of the transfer buffer and then send the transfer buffer to the iio buffer using iio_push_to_buffer. What differs for different devices is how exactly the message looks, but the rest of the pattern is identical. So if we define the the message outside of the trigger handler, e.g. at device init or the update_scan_mode callback we get the opportunity to have a common trigger function between a wide range of different SPI devices. Obviously it wont work for all, because some need special handling, but it will provide a good default implementation. So that's one reason why I'd like to use spi_sync here, since it will make the migration later on more obvious.

Fair enough.

Quoted 3 lines

- Lars