Skip to content

Commit 300cd94

Browse files
committed
iio: adc: ad7768-1: use new spi-engine implementation
Add support for new spi-engine implementation on ad7768-1 driver, optimizing offload transfers by reducing CS delay and minimizing the number of commands in the offload FIFO. Without the otimization, there's not enough time between the end of a transfer and the start of the next one, reducing the actual sample rate. Adjust 'tx_data' buffer handling in SPI register read and write functions to ensure endianness consistency with the new spi-engine. Signed-off-by: Jonathan Santos <Jonathan.Santos@analog.com>
1 parent 5af3802 commit 300cd94

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

drivers/iio/adc/ad7768-1.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <linux/sysfs.h>
2222
#include "linux/util_macros.h"
2323
#include <linux/spi/spi.h>
24-
#include <linux/spi/spi-engine.h>
24+
#include <linux/spi/spi-engine-ex.h>
2525

2626
#include <linux/iio/buffer.h>
2727
#include <linux/iio/buffer-dma.h>
@@ -288,6 +288,8 @@ struct ad7768_state {
288288
struct mutex lock;
289289
struct clk *mclk;
290290
struct gpio_chip gpiochip;
291+
struct spi_transfer offload_xfer;
292+
struct spi_message offload_msg;
291293
unsigned int gpio_avail_map;
292294
unsigned int mclk_freq;
293295
unsigned int mclk_div;
@@ -329,11 +331,11 @@ static int ad7768_spi_reg_read(struct ad7768_state *st, unsigned int addr,
329331
.len = len + 1,
330332
.bits_per_word = (len == 3 ? 32 : 16),
331333
};
332-
unsigned char tx_data[4];
334+
u32 tx_data;
333335
int ret;
334336

335-
tx_data[0] = AD7768_RD_FLAG_MSK(addr);
336-
xfer.tx_buf = tx_data;
337+
tx_data = AD7768_RD_FLAG_MSK(addr) << (len * 8);
338+
xfer.tx_buf = &tx_data;
337339
ret = spi_sync_transfer(st->spi, &xfer, 1);
338340
if (ret < 0)
339341
return ret;
@@ -351,11 +353,11 @@ static int ad7768_spi_reg_write(struct ad7768_state *st,
351353
.len = 2,
352354
.bits_per_word = 16,
353355
};
354-
unsigned char tx_data[2];
356+
u16 tx_data;
355357

356-
tx_data[0] = AD7768_WR_FLAG_MSK(addr);
357-
tx_data[1] = val & 0xFF;
358-
xfer.tx_buf = tx_data;
358+
tx_data = AD7768_WR_FLAG_MSK(addr) << 8;
359+
tx_data |= val & 0xFF;
360+
xfer.tx_buf = &tx_data;
359361
return spi_sync_transfer(st->spi, &xfer, 1);
360362
}
361363

@@ -1145,18 +1147,15 @@ static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
11451147
{
11461148
struct ad7768_state *st = iio_priv(indio_dev);
11471149
const struct iio_scan_type *scan_type;
1148-
struct spi_transfer xfer = {
1149-
.len = 1,
1150-
};
11511150
unsigned int rx_data[2];
1152-
struct spi_message msg;
11531151
int ret;
11541152

11551153
scan_type = iio_get_current_scan_type(indio_dev, &indio_dev->channels[0]);
11561154
if (IS_ERR(scan_type))
11571155
return PTR_ERR(scan_type);
11581156

1159-
xfer.bits_per_word = scan_type->realbits;
1157+
st->offload_xfer.len = roundup_pow_of_two(BITS_TO_BYTES(scan_type->realbits));
1158+
st->offload_xfer.bits_per_word = scan_type->realbits;
11601159

11611160
/*
11621161
* Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
@@ -1168,14 +1167,19 @@ static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
11681167
return ret;
11691168

11701169
if (st->spi_is_dma_mapped) {
1170+
st->offload_xfer.rx_buf = rx_data;
1171+
spi_message_init_with_transfers(&st->offload_msg, &st->offload_xfer, 1);
1172+
1173+
ret = spi_optimize_message(st->spi, &st->offload_msg);
1174+
if (ret < 0)
1175+
return ret;
1176+
11711177
spi_bus_lock(st->spi->master);
11721178

1173-
xfer.rx_buf = rx_data;
1174-
spi_message_init_with_transfers(&msg, &xfer, 1);
1175-
ret = spi_engine_offload_load_msg(st->spi, &msg);
1179+
ret = spi_engine_ex_offload_load_msg(st->spi, &st->offload_msg);
11761180
if (ret < 0)
11771181
return ret;
1178-
spi_engine_offload_enable(st->spi, true);
1182+
spi_engine_ex_offload_enable(st->spi, true);
11791183
}
11801184

11811185
return ret;
@@ -1187,9 +1191,10 @@ static int ad7768_buffer_predisable(struct iio_dev *indio_dev)
11871191
unsigned int regval;
11881192

11891193
if (st->spi_is_dma_mapped) {
1190-
spi_engine_offload_enable(st->spi, false);
1194+
spi_engine_ex_offload_enable(st->spi, false);
11911195
spi_bus_unlock(st->spi->master);
11921196
}
1197+
spi_unoptimize_message(&st->offload_msg);
11931198

11941199
/*
11951200
* To exit continuous read mode, perform a single read of the ADC_DATA
@@ -1323,7 +1328,7 @@ static int ad7768_probe(struct spi_device *spi)
13231328
return PTR_ERR(st->mclk);
13241329

13251330
st->mclk_freq = clk_get_rate(st->mclk);
1326-
st->spi_is_dma_mapped = spi_engine_offload_supported(spi);
1331+
st->spi_is_dma_mapped = spi_engine_ex_offload_supported(spi);
13271332
st->irq = spi->irq;
13281333

13291334
mutex_init(&st->lock);

0 commit comments

Comments
 (0)