Skip to content

Commit 7626c25

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 8d4c28f commit 7626c25

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

@@ -1137,18 +1139,15 @@ static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
11371139
{
11381140
struct ad7768_state *st = iio_priv(indio_dev);
11391141
const struct iio_scan_type *scan_type;
1140-
struct spi_transfer xfer = {
1141-
.len = 1,
1142-
};
11431142
unsigned int rx_data[2];
1144-
struct spi_message msg;
11451143
int ret;
11461144

11471145
scan_type = iio_get_current_scan_type(indio_dev, &indio_dev->channels[0]);
11481146
if (IS_ERR(scan_type))
11491147
return PTR_ERR(scan_type);
11501148

1151-
xfer.bits_per_word = scan_type->realbits;
1149+
st->offload_xfer.len = roundup_pow_of_two(BITS_TO_BYTES(scan_type->realbits));
1150+
st->offload_xfer.bits_per_word = scan_type->realbits;
11521151

11531152
/*
11541153
* Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
@@ -1160,14 +1159,19 @@ static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
11601159
return ret;
11611160

11621161
if (st->spi_is_dma_mapped) {
1162+
st->offload_xfer.rx_buf = rx_data;
1163+
spi_message_init_with_transfers(&st->offload_msg, &st->offload_xfer, 1);
1164+
1165+
ret = spi_optimize_message(st->spi, &st->offload_msg);
1166+
if (ret < 0)
1167+
return ret;
1168+
11631169
spi_bus_lock(st->spi->master);
11641170

1165-
xfer.rx_buf = rx_data;
1166-
spi_message_init_with_transfers(&msg, &xfer, 1);
1167-
ret = spi_engine_offload_load_msg(st->spi, &msg);
1171+
ret = spi_engine_ex_offload_load_msg(st->spi, &st->offload_msg);
11681172
if (ret < 0)
11691173
return ret;
1170-
spi_engine_offload_enable(st->spi, true);
1174+
spi_engine_ex_offload_enable(st->spi, true);
11711175
}
11721176

11731177
return ret;
@@ -1179,9 +1183,10 @@ static int ad7768_buffer_predisable(struct iio_dev *indio_dev)
11791183
unsigned int regval;
11801184

11811185
if (st->spi_is_dma_mapped) {
1182-
spi_engine_offload_enable(st->spi, false);
1186+
spi_engine_ex_offload_enable(st->spi, false);
11831187
spi_bus_unlock(st->spi->master);
11841188
}
1189+
spi_unoptimize_message(&st->offload_msg);
11851190

11861191
/*
11871192
* To exit continuous read mode, perform a single read of the ADC_DATA
@@ -1315,7 +1320,7 @@ static int ad7768_probe(struct spi_device *spi)
13151320
return PTR_ERR(st->mclk);
13161321

13171322
st->mclk_freq = clk_get_rate(st->mclk);
1318-
st->spi_is_dma_mapped = spi_engine_offload_supported(spi);
1323+
st->spi_is_dma_mapped = spi_engine_ex_offload_supported(spi);
13191324
st->irq = spi->irq;
13201325

13211326
mutex_init(&st->lock);

0 commit comments

Comments
 (0)