Commit aacff892 authored by Lars-Peter Clausen's avatar Lars-Peter Clausen Committed by Jonathan Cameron

staging:iio:adis: Preallocate transfer message

Currently the driver reads out all sample registers of the device and throws
away those which it does not need. Furthermore the SPI message is constructed
each time the trigger handler is run, although it will be the same each time.
This patch preallocates and pre-constructs the SPI message in the
"update_scan_mode" callback. Only those register which are actually selected for
sampling are included in the message. The patch also gets rid of the conversion
of the sample data from big endian to the native endianness and instead marks
the channel as big endian in its scan type. This allows to directly push the
SPI transfer buffer to the IIO buffer without the need to post-process it.
Signed-off-by: default avatarLars-Peter Clausen <lars@metafoo.de>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent a458c55c
...@@ -148,6 +148,7 @@ static const struct iio_chan_spec adis16201_channels[] = { ...@@ -148,6 +148,7 @@ static const struct iio_chan_spec adis16201_channels[] = {
static const struct iio_info adis16201_info = { static const struct iio_info adis16201_info = {
.read_raw = &adis16201_read_raw, .read_raw = &adis16201_read_raw,
.write_raw = &adis16201_write_raw, .write_raw = &adis16201_write_raw,
.update_scan_mode = adis_update_scan_mode,
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
}; };
......
...@@ -112,6 +112,7 @@ static const struct iio_chan_spec adis16203_channels[] = { ...@@ -112,6 +112,7 @@ static const struct iio_chan_spec adis16203_channels[] = {
static const struct iio_info adis16203_info = { static const struct iio_info adis16203_info = {
.read_raw = &adis16203_read_raw, .read_raw = &adis16203_read_raw,
.write_raw = &adis16203_write_raw, .write_raw = &adis16203_write_raw,
.update_scan_mode = adis_update_scan_mode,
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
}; };
......
...@@ -153,6 +153,7 @@ static const struct iio_chan_spec adis16204_channels[] = { ...@@ -153,6 +153,7 @@ static const struct iio_chan_spec adis16204_channels[] = {
static const struct iio_info adis16204_info = { static const struct iio_info adis16204_info = {
.read_raw = &adis16204_read_raw, .read_raw = &adis16204_read_raw,
.write_raw = &adis16204_write_raw, .write_raw = &adis16204_write_raw,
.update_scan_mode = adis_update_scan_mode,
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
}; };
......
...@@ -146,6 +146,7 @@ static const struct iio_chan_spec adis16209_channels[] = { ...@@ -146,6 +146,7 @@ static const struct iio_chan_spec adis16209_channels[] = {
static const struct iio_info adis16209_info = { static const struct iio_info adis16209_info = {
.read_raw = &adis16209_read_raw, .read_raw = &adis16209_read_raw,
.write_raw = &adis16209_write_raw, .write_raw = &adis16209_write_raw,
.update_scan_mode = adis_update_scan_mode,
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
}; };
......
...@@ -202,6 +202,7 @@ static const struct iio_info adis16240_info = { ...@@ -202,6 +202,7 @@ static const struct iio_info adis16240_info = {
.attrs = &adis16240_attribute_group, .attrs = &adis16240_attribute_group,
.read_raw = &adis16240_read_raw, .read_raw = &adis16240_read_raw,
.write_raw = &adis16240_write_raw, .write_raw = &adis16240_write_raw,
.update_scan_mode = adis_update_scan_mode,
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
}; };
......
...@@ -267,6 +267,7 @@ static const struct iio_info adis16260_info = { ...@@ -267,6 +267,7 @@ static const struct iio_info adis16260_info = {
.attrs = &adis16260_attribute_group, .attrs = &adis16260_attribute_group,
.read_raw = &adis16260_read_raw, .read_raw = &adis16260_read_raw,
.write_raw = &adis16260_write_raw, .write_raw = &adis16260_write_raw,
.update_scan_mode = adis_update_scan_mode,
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
}; };
......
...@@ -87,6 +87,7 @@ int adis_single_conversion(struct iio_dev *indio_dev, ...@@ -87,6 +87,7 @@ int adis_single_conversion(struct iio_dev *indio_dev,
.sign = 'u', \ .sign = 'u', \
.realbits = (bits), \ .realbits = (bits), \
.storagebits = 16, \ .storagebits = 16, \
.endianness = IIO_BE, \
}, \ }, \
} }
...@@ -109,6 +110,7 @@ int adis_single_conversion(struct iio_dev *indio_dev, ...@@ -109,6 +110,7 @@ int adis_single_conversion(struct iio_dev *indio_dev,
.sign = 'u', \ .sign = 'u', \
.realbits = (bits), \ .realbits = (bits), \
.storagebits = 16, \ .storagebits = 16, \
.endianness = IIO_BE, \
}, \ }, \
} }
...@@ -125,6 +127,7 @@ int adis_single_conversion(struct iio_dev *indio_dev, ...@@ -125,6 +127,7 @@ int adis_single_conversion(struct iio_dev *indio_dev,
.sign = 's', \ .sign = 's', \
.realbits = (bits), \ .realbits = (bits), \
.storagebits = 16, \ .storagebits = 16, \
.endianness = IIO_BE, \
}, \ }, \
} }
...@@ -150,6 +153,9 @@ void adis_cleanup_buffer_and_trigger(struct adis *adis, ...@@ -150,6 +153,9 @@ void adis_cleanup_buffer_and_trigger(struct adis *adis,
int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev); int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
void adis_remove_trigger(struct adis *adis); void adis_remove_trigger(struct adis *adis);
int adis_update_scan_mode(struct iio_dev *indio_dev,
const unsigned long *scan_mask);
#else /* CONFIG_IIO_BUFFER */ #else /* CONFIG_IIO_BUFFER */
static inline int adis_setup_buffer_and_trigger(struct adis *adis, static inline int adis_setup_buffer_and_trigger(struct adis *adis,
...@@ -173,6 +179,8 @@ static inline void adis_remove_trigger(struct adis *adis) ...@@ -173,6 +179,8 @@ static inline void adis_remove_trigger(struct adis *adis)
{ {
} }
#define adis_update_scan_mode NULL
#endif /* CONFIG_IIO_BUFFER */ #endif /* CONFIG_IIO_BUFFER */
#endif #endif
/*
* Common library for ADIS16XXX devices
*
* Copyright 2012 Analog Devices Inc.
* Author: Lars-Peter Clausen <lars@metafoo.de>
*
* Licensed under the GPL-2 or later.
*/
#include <linux/export.h> #include <linux/export.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/mutex.h> #include <linux/mutex.h>
...@@ -12,73 +21,80 @@ ...@@ -12,73 +21,80 @@
#include "adis.h" #include "adis.h"
#define ADIS_MAX_OUTPUTS 12 int adis_update_scan_mode(struct iio_dev *indio_dev,
const unsigned long *scan_mask)
static int adis_read_buffer_data(struct adis *adis, struct iio_dev *indio_dev)
{ {
int n_outputs = indio_dev->num_channels; struct adis *adis = iio_device_get_drvdata(indio_dev);
struct spi_transfer xfers[ADIS_MAX_OUTPUTS + 1]; const struct iio_chan_spec *chan;
struct spi_message msg; unsigned int scan_count;
int ret; unsigned int i, j;
int i; __be16 *tx, *rx;
mutex_lock(&adis->txrx_lock);
spi_message_init(&msg);
memset(xfers, 0, sizeof(xfers));
for (i = 0; i <= n_outputs; i++) {
xfers[i].bits_per_word = 8;
xfers[i].cs_change = 1;
xfers[i].len = 2;
xfers[i].delay_usecs = adis->data->read_delay;
if (i < n_outputs) {
xfers[i].tx_buf = adis->tx + 2 * i;
adis->tx[2 * i] = indio_dev->channels[i].address;
adis->tx[2 * i + 1] = 0;
}
if (i >= 1)
xfers[i].rx_buf = adis->rx + 2 * (i - 1);
spi_message_add_tail(&xfers[i], &msg);
}
ret = spi_sync(adis->spi, &msg); kfree(adis->xfer);
if (ret) kfree(adis->buffer);
dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
mutex_unlock(&adis->txrx_lock); scan_count = indio_dev->scan_bytes / 2;
return ret; adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
if (!adis->xfer)
return -ENOMEM;
adis->buffer = kzalloc(indio_dev->scan_bytes * 2, GFP_KERNEL);
if (!adis->buffer)
return -ENOMEM;
rx = adis->buffer;
tx = rx + indio_dev->scan_bytes;
spi_message_init(&adis->msg);
for (j = 0; j <= scan_count; j++) {
adis->xfer[j].bits_per_word = 8;
if (j != scan_count)
adis->xfer[j].cs_change = 1;
adis->xfer[j].len = 2;
adis->xfer[j].delay_usecs = adis->data->read_delay;
if (j < scan_count)
adis->xfer[j].tx_buf = &tx[j];
if (j >= 1)
adis->xfer[j].rx_buf = &rx[j - 1];
spi_message_add_tail(&adis->xfer[j], &adis->msg);
}
chan = indio_dev->channels;
for (i = 0; i < indio_dev->num_channels; i++, chan++) {
if (!test_bit(chan->scan_index, scan_mask))
continue;
*tx++ = cpu_to_be16(chan->address << 8);
}
return 0;
} }
EXPORT_SYMBOL_GPL(adis_update_scan_mode);
static irqreturn_t adis_trigger_handler(int irq, void *p) static irqreturn_t adis_trigger_handler(int irq, void *p)
{ {
struct iio_poll_func *pf = p; struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev; struct iio_dev *indio_dev = pf->indio_dev;
struct adis *adis = iio_device_get_drvdata(indio_dev); struct adis *adis = iio_device_get_drvdata(indio_dev);
u16 *data; int ret;
int i = 0;
data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); if (!adis->buffer)
if (data == NULL) {
dev_err(&adis->spi->dev, "Failed to allocate memory.");
return -ENOMEM; return -ENOMEM;
}
if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength) ret = spi_sync(adis->spi, &adis->msg);
&& adis_read_buffer_data(adis, indio_dev) >= 0) if (ret)
for (; i < bitmap_weight(indio_dev->active_scan_mask, dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
indio_dev->masklength); i++)
data[i] = be16_to_cpup((__be16 *)&(adis->rx[i*2]));
/* Guaranteed to be aligned with 8 byte boundary */ /* Guaranteed to be aligned with 8 byte boundary */
if (indio_dev->scan_timestamp) if (indio_dev->scan_timestamp) {
*((s64 *)(PTR_ALIGN(data, sizeof(s64)))) = pf->timestamp; void *b = adis->buffer + indio_dev->scan_bytes - sizeof(s64);
*(s64 *)b = pf->timestamp;
}
iio_push_to_buffers(indio_dev, (u8 *)data); iio_push_to_buffers(indio_dev, adis->buffer);
iio_trigger_notify_done(indio_dev->trig); iio_trigger_notify_done(indio_dev->trig);
kfree(data);
return IRQ_HANDLED; return IRQ_HANDLED;
} }
...@@ -137,6 +153,8 @@ void adis_cleanup_buffer_and_trigger(struct adis *adis, ...@@ -137,6 +153,8 @@ void adis_cleanup_buffer_and_trigger(struct adis *adis,
{ {
if (adis->spi->irq) if (adis->spi->irq)
adis_remove_trigger(adis); adis_remove_trigger(adis);
kfree(adis->buffer);
kfree(adis->xfer);
iio_triggered_buffer_cleanup(indio_dev); iio_triggered_buffer_cleanup(indio_dev);
} }
EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger); EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment