On Mon, 25 Mar 2024 17:03:13 -0500 David Lechner <dlechner@xxxxxxxxxxxx> wrote: > This modifies the ad7944 driver to use spi_optimize_message() to reduce > CPU usage and increase the max sample rate by avoiding repeating > validation of the spi message on each transfer. > > Signed-off-by: David Lechner <dlechner@xxxxxxxxxxxx> One buglet inline. Otherwise looks fine to me. Jonathan > --- > drivers/iio/adc/ad7944.c | 177 +++++++++++++++++++++++++++-------------------- > 1 file changed, 103 insertions(+), 74 deletions(-) > > diff --git a/drivers/iio/adc/ad7944.c b/drivers/iio/adc/ad7944.c > index 261a3f645fd8..c767401712af 100644 > --- a/drivers/iio/adc/ad7944.c > +++ b/drivers/iio/adc/ad7944.c > @@ -51,6 +51,8 @@ static const char * const ad7944_spi_modes[] = { > struct ad7944_adc { > struct spi_device *spi; > enum ad7944_spi_mode spi_mode; > + struct spi_transfer xfers[3]; > + struct spi_message msg; > /* Chip-specific timing specifications. */ > const struct ad7944_timing_spec *timing_spec; > /* GPIO connected to CNV pin. */ > @@ -130,6 +132,88 @@ AD7944_DEFINE_CHIP_INFO(ad7985, ad7944, 16, 0); > /* fully differential */ > AD7944_DEFINE_CHIP_INFO(ad7986, ad7986, 18, 1); > > +static void ad7944_unoptimize_msg(void *msg) > +{ > + spi_unoptimize_message(msg); > +} > + > +static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *adc, > + const struct iio_chan_spec *chan) > +{ > + unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns > + : adc->timing_spec->conv_ns; > + struct spi_transfer *xfers = adc->xfers; > + int ret; > + > + /* > + * NB: can get better performance from some SPI controllers if we use > + * the same bits_per_word in every transfer. > + */ > + xfers[0].bits_per_word = chan->scan_type.realbits; > + /* > + * CS is tied to CNV and we need a low to high transition to start the > + * conversion, so place CNV low for t_QUIET to prepare for this. > + */ > + xfers[0].delay.value = T_QUIET_NS; > + xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS; > + > + /* > + * CS has to be high for full conversion time to avoid triggering the > + * busy indication. > + */ > + xfers[1].cs_off = 1; > + xfers[1].delay.value = t_conv_ns; > + xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS; > + xfers[0].bits_per_word = chan->scan_type.realbits; 0 ? > + > + /* Then we can read the data during the acquisition phase */ > + xfers[2].rx_buf = &adc->sample.raw; > + xfers[2].len = BITS_TO_BYTES(chan->scan_type.storagebits); > + xfers[2].bits_per_word = chan->scan_type.realbits; > + > + spi_message_init_with_transfers(&adc->msg, xfers, 3); > + > + ret = spi_optimize_message(adc->spi, &adc->msg); > + if (ret) > + return ret; > + > + return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg); > +}