Skip to content

Commit 0a1f277

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 59695d4 commit 0a1f277

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>
@@ -294,6 +294,8 @@ struct ad7768_state {
294294
struct mutex lock;
295295
struct clk *mclk;
296296
struct gpio_chip gpiochip;
297+
struct spi_transfer offload_xfer;
298+
struct spi_message offload_msg;
297299
unsigned int gpio_avail_map;
298300
unsigned int mclk_freq;
299301
unsigned int mclk_div;
@@ -335,11 +337,11 @@ static int ad7768_spi_reg_read(struct ad7768_state *st, unsigned int addr,
335337
.len = len + 1,
336338
.bits_per_word = (len == 3 ? 32 : 16),
337339
};
338-
unsigned char tx_data[4];
340+
u32 tx_data;
339341
int ret;
340342

341-
tx_data[0] = AD7768_RD_FLAG_MSK(addr);
342-
xfer.tx_buf = tx_data;
343+
tx_data = AD7768_RD_FLAG_MSK(addr) << (len * 8);
344+
xfer.tx_buf = &tx_data;
343345
ret = spi_sync_transfer(st->spi, &xfer, 1);
344346
if (ret < 0)
345347
return ret;
@@ -357,11 +359,11 @@ static int ad7768_spi_reg_write(struct ad7768_state *st,
357359
.len = 2,
358360
.bits_per_word = 16,
359361
};
360-
unsigned char tx_data[2];
362+
u16 tx_data;
361363

362-
tx_data[0] = AD7768_WR_FLAG_MSK(addr);
363-
tx_data[1] = val & 0xFF;
364-
xfer.tx_buf = tx_data;
364+
tx_data = AD7768_WR_FLAG_MSK(addr) << 8;
365+
tx_data |= val & 0xFF;
366+
xfer.tx_buf = &tx_data;
365367
return spi_sync_transfer(st->spi, &xfer, 1);
366368
}
367369

@@ -1155,18 +1157,15 @@ static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
11551157
{
11561158
struct ad7768_state *st = iio_priv(indio_dev);
11571159
const struct iio_scan_type *scan_type;
1158-
struct spi_transfer xfer = {
1159-
.len = 1,
1160-
};
11611160
unsigned int rx_data[2];
1162-
struct spi_message msg;
11631161
int ret;
11641162

11651163
scan_type = iio_get_current_scan_type(indio_dev, &indio_dev->channels[0]);
11661164
if (IS_ERR(scan_type))
11671165
return PTR_ERR(scan_type);
11681166

1169-
xfer.bits_per_word = scan_type->realbits;
1167+
st->offload_xfer.len = roundup_pow_of_two(BITS_TO_BYTES(scan_type->realbits));
1168+
st->offload_xfer.bits_per_word = scan_type->realbits;
11701169

11711170
/*
11721171
* Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
@@ -1178,14 +1177,19 @@ static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
11781177
return ret;
11791178

11801179
if (st->spi_is_dma_mapped) {
1180+
st->offload_xfer.rx_buf = rx_data;
1181+
spi_message_init_with_transfers(&st->offload_msg, &st->offload_xfer, 1);
1182+
1183+
ret = spi_optimize_message(st->spi, &st->offload_msg);
1184+
if (ret < 0)
1185+
return ret;
1186+
11811187
spi_bus_lock(st->spi->master);
11821188

1183-
xfer.rx_buf = rx_data;
1184-
spi_message_init_with_transfers(&msg, &xfer, 1);
1185-
ret = spi_engine_offload_load_msg(st->spi, &msg);
1189+
ret = spi_engine_ex_offload_load_msg(st->spi, &st->offload_msg);
11861190
if (ret < 0)
11871191
return ret;
1188-
spi_engine_offload_enable(st->spi, true);
1192+
spi_engine_ex_offload_enable(st->spi, true);
11891193
}
11901194

11911195
return ret;
@@ -1197,9 +1201,10 @@ static int ad7768_buffer_predisable(struct iio_dev *indio_dev)
11971201
unsigned int regval;
11981202

11991203
if (st->spi_is_dma_mapped) {
1200-
spi_engine_offload_enable(st->spi, false);
1204+
spi_engine_ex_offload_enable(st->spi, false);
12011205
spi_bus_unlock(st->spi->master);
12021206
}
1207+
spi_unoptimize_message(&st->offload_msg);
12031208

12041209
/*
12051210
* To exit continuous read mode, perform a single read of the ADC_DATA
@@ -1333,7 +1338,7 @@ static int ad7768_probe(struct spi_device *spi)
13331338
return PTR_ERR(st->mclk);
13341339

13351340
st->mclk_freq = clk_get_rate(st->mclk);
1336-
st->spi_is_dma_mapped = spi_engine_offload_supported(spi);
1341+
st->spi_is_dma_mapped = spi_engine_ex_offload_supported(spi);
13371342
st->irq = spi->irq;
13381343

13391344
mutex_init(&st->lock);

0 commit comments

Comments
 (0)