Skip to contentSkip to conversation

staging:iio:ad7887: Preallocate sample buffer

PATCH In review Lars-Peter Clausen 2 participants 8 messages Nov 5, 2012 – Nov 10, 2012

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

PATCH 1/7 staging:iio:ad7887: Preallocate sample buffer

We know that the sample buffer will at most need to hold two 16 bit samples and the 64 bit aligned 64 bit timestamp. Preallocate a buffer large enough to hold this instead of allocating and freeing it each time a sample is read.

---
 drivers/staging/iio/adc/ad7887.h      |  6 ++++--
 drivers/staging/iio/adc/ad7887_ring.c | 15 ++-------------
 2 files changed, 6 insertions(+), 15 deletions(-)
drivers/staging/iio/adc/ad7887.h +4 -2
index 2e09e54..71e5092 100644
@@ -72,9 +72,11 @@ struct ad7887_state {
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
+	 * Buffer needs to be large enough to hold two 16 bit samples and a
+	 * 64 bit aligned 64 bit timestamp.
 	 */
-
-	unsigned char			data[4] ____cacheline_aligned;
+	unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)]
+		____cacheline_aligned;
 };
 
 enum ad7887_supported_device_ids {
drivers/staging/iio/adc/ad7887_ring.c +2 -13
index b39923b..f11925e 100644
@@ -73,31 +73,20 @@ static irqreturn_t ad7887_trigger_handler(int irq, void *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct ad7887_state *st = iio_priv(indio_dev);
 	s64 time_ns;
-	__u8 *buf;
 	int b_sent;
 
-	unsigned int bytes = bitmap_weight(indio_dev->active_scan_mask,
-					   indio_dev->masklength) *
-		st->chip_info->channel[0].scan_type.storagebits / 8;
-
-	buf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
-	if (buf == NULL)
-		goto done;
-
 	b_sent = spi_sync(st->spi, st->ring_msg);
 	if (b_sent)
 		goto done;
 
 	time_ns = iio_get_time_ns();
 
-	memcpy(buf, st->data, bytes);
 	if (indio_dev->scan_timestamp)
-		memcpy(buf + indio_dev->scan_bytes - sizeof(s64),
+		memcpy(st->data + indio_dev->scan_bytes - sizeof(s64),
 		       &time_ns, sizeof(time_ns));
 
-	iio_push_to_buffer(indio_dev->buffer, buf);
+	iio_push_to_buffer(indio_dev->buffer, st->data);
 done:
-	kfree(buf);
 	iio_trigger_notify_done(indio_dev->trig);
 
 	return IRQ_HANDLED;
Signature · 1 line
1.8.0
#1 lore raw

PATCH 2/7 staging:iio:ad7887: Rework regulator handling

Rework the regulator handling of the ad7887 driver to match more closely what we do for other drivers. Only request the regulator if a external reference is used, but treat it as an error if requesting the regulator fails. Also remove the possibility to specify the reference voltage via platform data and always use the regulator for this.

---
 drivers/staging/iio/adc/ad7887.h      |  3 --
 drivers/staging/iio/adc/ad7887_core.c | 52 ++++++++++++++++-------------------
 2 files changed, 24 insertions(+), 31 deletions(-)
drivers/staging/iio/adc/ad7887.h +0 -3
index 71e5092..25d5b40 100644
@@ -30,8 +30,6 @@ enum ad7887_channels {
  */
 
 struct ad7887_platform_data {
-	/* External Vref voltage applied */
-	u16				vref_mv;
 	/*
 	 * AD7887:
 	 * In single channel mode en_dual = flase, AIN1/Vref pins assumes its
@@ -63,7 +61,6 @@ struct ad7887_state {
 	struct spi_device		*spi;
 	const struct ad7887_chip_info	*chip_info;
 	struct regulator		*reg;
-	u16				int_vref_mv;
 	struct spi_transfer		xfer[4];
 	struct spi_message		msg[3];
 	struct spi_message		*ring_msg;
drivers/staging/iio/adc/ad7887_core.c +24 -28

104 lines not on this page · raw message

Signature · 1 line
1.8.0
#2 lore raw

PATCH 3/7 staging:iio:ad7887: Squash everything into one file

In reply to Lars-Peter Clausen

The recent cleanups have decimated the drivers code size by quite a bit. It is only a few hundred lines in total now and we also always build buffer support, so there really is no need to spread the driver out over multiple files. Putting everything into one file also allows to reduce the code size a bit more by removing a few lines of boilerplate code.

---
 drivers/staging/iio/adc/Makefile                   |   2 -
 .../staging/iio/adc/{ad7887_core.c => ad7887.c}    | 133 ++++++++++++++++++++-
 drivers/staging/iio/adc/ad7887.h                   |  66 ----------
 drivers/staging/iio/adc/ad7887_ring.c              | 111 -----------------
 4 files changed, 130 insertions(+), 182 deletions(-)
 rename drivers/staging/iio/adc/{ad7887_core.c => ad7887.c} (62%)
 delete mode 100644 drivers/staging/iio/adc/ad7887_ring.c
drivers/staging/iio/adc/Makefile +0 -2
index ff561c5..8036fd1 100644
@@ -17,8 +17,6 @@ ad799x-y := ad799x_core.o
 ad799x-$(CONFIG_AD799X_RING_BUFFER) += ad799x_ring.o
 obj-$(CONFIG_AD799X) += ad799x.o
 
-ad7887-y := ad7887_core.o
-ad7887-$(CONFIG_IIO_BUFFER) += ad7887_ring.o
 obj-$(CONFIG_AD7887) += ad7887.o
 
 ad7298-y := ad7298_core.o
drivers/staging/iio/adc/ad7887.c renamed from drivers/staging/iio/adc/ad7887_core.c +130 -3

165 lines not on this page · raw message

drivers/staging/iio/adc/ad7887.h +0 -66

76 lines not on this page · raw message

drivers/staging/iio/adc/ad7887_ring.c deleted +0 -111

111 lines not on this page · raw message

Signature · 1 line
1.8.0
#3 lore raw

PATCH 4/7 staging:iio:ad7887: Use proper kernel doc

In reply to Lars-Peter Clausen

Use proper kernel doc to document the platform data struct.

---
 drivers/staging/iio/adc/ad7887.h | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
drivers/staging/iio/adc/ad7887.h +13 -14
index b9078fc..78b06a3 100644
@@ -12,21 +12,20 @@
  * TODO: struct ad7887_platform_data needs to go into include/linux/iio
  */
 
+
+/**
+ * struct ad7887_platform_data - AD7887 ADC driver platform data
+ * @en_dual: Whether to use dual channel mode. If set to true AIN1 becomes the
+ *	second input channel, and Vref is internally connected to Vdd. If set to
+ *	false the device is used in single channel mode and AIN1/Vref is used as
+ *	VREF input.
+ * @use_onchip_ref: Whether to use the onchip reference. If set to true the
+ *	internal 2.5V reference is used. If set to false a external reference is
+ *	used.
+ */
 struct ad7887_platform_data {
-	/*
-	 * AD7887:
-	 * In single channel mode en_dual = flase, AIN1/Vref pins assumes its
-	 * Vref function. In dual channel mode en_dual = true, AIN1 becomes the
-	 * second input channel, and Vref is internally connected to Vdd.
-	 */
-	bool				en_dual;
-	/*
-	 * AD7887:
-	 * use_onchip_ref = true, the Vref is internally connected to the 2.500V
-	 * Voltage reference. If use_onchip_ref = false, the reference voltage
-	 * is supplied by AIN1/Vref
-	 */
-	bool				use_onchip_ref;
+	bool en_dual;
+	bool use_onchip_ref;
 };
 
 #endif /* IIO_ADC_AD7887_H_ */
Signature · 1 line
1.8.0
#4 lore raw

PATCH 5/7 staging:iio:ad7887: Allow to use internal ref in two channel mode

In reply to Lars-Peter Clausen

While it is not recommended to use the internal reference in two channel mode in order to obtain optimal performance it is still possible to use it.

While we are at it also get rid of the duplicate tx_cmd_buf entries. There are only two unique entries. One for channel 1 and one for channel 2.

---
 drivers/staging/iio/adc/ad7887.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/staging/iio/adc/ad7887.c +13 -15
index 88ffc46..3ac0c30 100644
@@ -59,7 +59,7 @@ struct ad7887_state {
 	struct spi_transfer		xfer[4];
 	struct spi_message		msg[3];
 	struct spi_message		*ring_msg;
-	unsigned char			tx_cmd_buf[8];
+	unsigned char			tx_cmd_buf[4];
 
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
@@ -238,6 +238,7 @@ static int __devinit ad7887_probe(struct spi_device *spi)
 	struct ad7887_platform_data *pdata = spi->dev.platform_data;
 	struct ad7887_state *st;
 	struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
+	uint8_t mode;
 	int ret;
 
 	if (indio_dev == NULL)
@@ -271,9 +272,13 @@ static int __devinit ad7887_probe(struct spi_device *spi)
 
 	/* Setup default message */
 
-	st->tx_cmd_buf[0] = AD7887_CH_AIN0 | AD7887_PM_MODE4 |
-			    ((pdata && pdata->use_onchip_ref) ?
-			    0 : AD7887_REF_DIS);
+	mode = AD7887_PM_MODE4;
+	if (!pdata || !pdata->use_onchip_ref)
+		mode |= AD7887_REF_DIS;
+	if (pdata && pdata->en_dual)
+		mode |= AD7887_DUAL;
+
+	st->tx_cmd_buf[0] = AD7887_CH_AIN0 | mode;
 
 	st->xfer[0].rx_buf = &st->data[0];
 	st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
@@ -283,29 +288,22 @@ static int __devinit ad7887_probe(struct spi_device *spi)
 	spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
 
 	if (pdata && pdata->en_dual) {
-		st->tx_cmd_buf[0] |= AD7887_DUAL | AD7887_REF_DIS;
-
-		st->tx_cmd_buf[2] = AD7887_CH_AIN1 | AD7887_DUAL |
-				    AD7887_REF_DIS | AD7887_PM_MODE4;
-		st->tx_cmd_buf[4] = AD7887_CH_AIN0 | AD7887_DUAL |
-				    AD7887_REF_DIS | AD7887_PM_MODE4;
-		st->tx_cmd_buf[6] = AD7887_CH_AIN1 | AD7887_DUAL |
-				    AD7887_REF_DIS | AD7887_PM_MODE4;
+		st->tx_cmd_buf[2] = AD7887_CH_AIN1 | mode;
 
 		st->xfer[1].rx_buf = &st->data[0];
 		st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
 		st->xfer[1].len = 2;
 
 		st->xfer[2].rx_buf = &st->data[2];
-		st->xfer[2].tx_buf = &st->tx_cmd_buf[4];
+		st->xfer[2].tx_buf = &st->tx_cmd_buf[0];
 		st->xfer[2].len = 2;
 
 		spi_message_init(&st->msg[AD7887_CH0_CH1]);
 		spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
 		spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
 
-		st->xfer[3].rx_buf = &st->data[0];
-		st->xfer[3].tx_buf = &st->tx_cmd_buf[6];
+		st->xfer[3].rx_buf = &st->data[2];
+		st->xfer[3].tx_buf = &st->tx_cmd_buf[2];
 		st->xfer[3].len = 2;
 
 		spi_message_init(&st->msg[AD7887_CH1]);
Signature · 1 line
1.8.0
#5 lore raw

PATCH 6/7 staging:iio:ad7887: Use passed in chan spec in ad7887_read_raw

In reply to Lars-Peter Clausen

Use the passed in chan spec in ad7887_read_raw instead of alawys using the first chan spec entry from the chip info data. Since all channels have the same shift and realbits from a functional point of view it does not matter which chan spec is used, but the patch makes the a bit more clear.

---
 drivers/staging/iio/adc/ad7887.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/iio/adc/ad7887.c +3 -3
index 3ac0c30..72cfe19 100644
@@ -177,8 +177,8 @@ static int ad7887_read_raw(struct iio_dev *indio_dev,
 
 		if (ret < 0)
 			return ret;
-		*val = (ret >> st->chip_info->channel[0].scan_type.shift) &
-			RES_MASK(st->chip_info->channel[0].scan_type.realbits);
+		*val = ret >> chan->scan_type.shift;
+		*val &= RES_MASK(chan->scan_type.realbits);
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		if (st->reg) {
@@ -190,7 +190,7 @@ static int ad7887_read_raw(struct iio_dev *indio_dev,
 			*val = st->chip_info->int_vref_mv;
 		}
 
-		*val2 = st->chip_info->channel[0].scan_type.realbits;
+		*val2 = chan->scan_type.realbits;
 
 		return IIO_VAL_FRACTIONAL_LOG2;
 	}
Signature · 1 line
1.8.0
#6 lore raw

PATCH 7/7 staging:iio: Move the ad7887 driver out of staging

In reply to Lars-Peter Clausen

The driver does not expose any custom API to userspace and none of the standard static code checker tools report any issues, so move it out of staging.

---
 drivers/iio/adc/Kconfig                                     | 13 +++++++++++++
 drivers/iio/adc/Makefile                                    |  1 +
 drivers/{staging => }/iio/adc/ad7887.c                      |  2 +-
 drivers/staging/iio/adc/Kconfig                             | 13 -------------
 drivers/staging/iio/adc/Makefile                            |  2 --
 .../iio/adc => include/linux/platform_data}/ad7887.h        |  5 -----
 6 files changed, 15 insertions(+), 21 deletions(-)
 rename drivers/{staging => }/iio/adc/ad7887.c (99%)
 rename {drivers/staging/iio/adc => include/linux/platform_data}/ad7887.h (90%)
drivers/iio/adc/Kconfig +13 -0
index 4927581..706386b 100644
@@ -45,6 +45,19 @@ config AD7476
 	  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
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say yes here to build support for Analog Devices
+	  AD7887 SPI analog to digital converter (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 ad7887.
+
 config AT91_ADC
 	tristate "Atmel AT91 ADC"
 	depends on ARCH_AT91
drivers/iio/adc/Makefile +1 -0
index 900995d..034eacb 100644
@@ -6,5 +6,6 @@ obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o
 obj-$(CONFIG_AD7266) += ad7266.o
 obj-$(CONFIG_AD7476) += ad7476.o
 obj-$(CONFIG_AD7791) += ad7791.o
+obj-$(CONFIG_AD7887) += ad7887.o
 obj-$(CONFIG_AT91_ADC) += at91_adc.o
 obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
drivers/iio/adc/ad7887.c renamed from drivers/staging/iio/adc/ad7887.c +1 -1
similarity index 99%
rename from drivers/staging/iio/adc/ad7887.c
rename to drivers/iio/adc/ad7887.c
index 72cfe19..fd62309 100644
@@ -23,7 +23,7 @@
 #include <linux/iio/trigger_consumer.h>
 #include <linux/iio/triggered_buffer.h>
 
-#include "ad7887.h"
+#include <linux/platform_data/ad7887.h>
 
 #define AD7887_REF_DIS		(1 << 5) /* on-chip reference disable */
 #define AD7887_DUAL		(1 << 4) /* dual-channel mode */
drivers/staging/iio/adc/Kconfig +0 -13
index 71a515d..eba64fb 100644
@@ -68,19 +68,6 @@ config AD799X_RING_BUFFER
 	  Say yes here to include ring buffer support in the AD799X
 	  ADC driver.
 
-config AD7887
-	tristate "Analog Devices AD7887 ADC driver"
-	depends on SPI
-	select IIO_BUFFER
-	select IIO_TRIGGERED_BUFFER
-	help
-	  Say yes here to build support for Analog Devices
-	  AD7887 SPI analog to digital converter (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 ad7887.
-
 config AD7780
 	tristate "Analog Devices AD7780 and similar ADCs driver"
 	depends on SPI
drivers/staging/iio/adc/Makefile +0 -2
index 8036fd1..c56b41e 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_AD7887) += ad7887.o
-
 ad7298-y := ad7298_core.o
 ad7298-$(CONFIG_IIO_BUFFER) += ad7298_ring.o
 obj-$(CONFIG_AD7298) += ad7298.o
include/linux/platform_data/ad7887.h renamed from drivers/staging/iio/adc/ad7887.h +0 -5
similarity index 90%
rename from drivers/staging/iio/adc/ad7887.h
rename to include/linux/platform_data/ad7887.h
index 78b06a3..dedb9735 100644
@@ -8,11 +8,6 @@
 #ifndef IIO_ADC_AD7887_H_
 #define IIO_ADC_AD7887_H_
 
-/*
- * TODO: struct ad7887_platform_data needs to go into include/linux/iio
- */
-
-
 /**
  * struct ad7887_platform_data - AD7887 ADC driver platform data
  * @en_dual: Whether to use dual channel mode. If set to true AIN1 becomes the
Signature · 1 line
1.8.0
maintainer
#7 lore raw
In reply to Lars-Peter Clausen
Quoted 5 lines · Lars-Peter Clausen

We know that the sample buffer will at most need to hold two 16 bit samples and the 64 bit aligned 64 bit timestamp. Preallocate a buffer large enough to hold this instead of allocating and freeing it each time a sample is read.

All 7 applied to togreg branch.

A nice series and good to get this one out of staging as it's been pretty much clean for an awful long time!

Jonathan

p.s. I thought I had sent this the other day but it appears not!

Quoted 62 lines
---
 drivers/staging/iio/adc/ad7887.h      |  6 ++++--
 drivers/staging/iio/adc/ad7887_ring.c | 15 ++-------------
 2 files changed, 6 insertions(+), 15 deletions(-)
drivers/staging/iio/adc/ad7887.h +4 -2
index 2e09e54..71e5092 100644
@@ -72,9 +72,11 @@ struct ad7887_state {
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
+	 * Buffer needs to be large enough to hold two 16 bit samples and a
+	 * 64 bit aligned 64 bit timestamp.
 	 */
-
-	unsigned char			data[4] ____cacheline_aligned;
+	unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)]
+		____cacheline_aligned;
 };
 
 enum ad7887_supported_device_ids {
drivers/staging/iio/adc/ad7887_ring.c +2 -13
index b39923b..f11925e 100644
@@ -73,31 +73,20 @@ static irqreturn_t ad7887_trigger_handler(int irq, void *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct ad7887_state *st = iio_priv(indio_dev);
 	s64 time_ns;
-	__u8 *buf;
 	int b_sent;
 
-	unsigned int bytes = bitmap_weight(indio_dev->active_scan_mask,
-					   indio_dev->masklength) *
-		st->chip_info->channel[0].scan_type.storagebits / 8;
-
-	buf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
-	if (buf == NULL)
-		goto done;
-
 	b_sent = spi_sync(st->spi, st->ring_msg);
 	if (b_sent)
 		goto done;
 
 	time_ns = iio_get_time_ns();
 
-	memcpy(buf, st->data, bytes);
 	if (indio_dev->scan_timestamp)
-		memcpy(buf + indio_dev->scan_bytes - sizeof(s64),
+		memcpy(st->data + indio_dev->scan_bytes - sizeof(s64),
 		       &time_ns, sizeof(time_ns));
 
-	iio_push_to_buffer(indio_dev->buffer, buf);
+	iio_push_to_buffer(indio_dev->buffer, st->data);
 done:
-	kfree(buf);
 	iio_trigger_notify_done(indio_dev->trig);
 
 	return IRQ_HANDLED;