Re: [PATCH 2/2] iio: adc: ti-adc161s626: use DMA-safe memory for spi_read()

From: Jonathan Cameron

Date: Mon Mar 16 2026 - 14:32:17 EST


On Sat, 14 Mar 2026 18:13:32 -0500
David Lechner <dlechner@xxxxxxxxxxxx> wrote:

> Add a DMA-safe buffer and use it for spi_read() instead of a stack
> memory. All SPI buffers must be DMA-safe.
>
> Since we only need up to 3 bytes, we just use a u8[] instead of __be16
> and __be32 and change the conversion functions appropriately.
>
> Fixes: 4d671b71beef ("iio: adc: ti-adc161s626: add support for TI 1-channel differential ADCs")
> Signed-off-by: David Lechner <dlechner@xxxxxxxxxxxx>
> ---
> drivers/iio/adc/ti-adc161s626.c | 19 +++++++------------
> 1 file changed, 7 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/iio/adc/ti-adc161s626.c b/drivers/iio/adc/ti-adc161s626.c
> index 1d427548e0b3..6416d6a7ada0 100644
> --- a/drivers/iio/adc/ti-adc161s626.c
> +++ b/drivers/iio/adc/ti-adc161s626.c
> @@ -70,6 +70,7 @@ struct ti_adc_data {
>
> u8 read_size;
> u8 shift;
> + u8 buf[3] __aligned(IIO_DMA_MINALIGN);
On this. There is new generic infrastructure for marking these.
https://elixir.bootlin.com/linux/v7.0-rc3/source/include/linux/dma-mapping.h#L720
https://lore.kernel.org/all/01ea88055ded4d70cac70ba557680fd5fa7d9ff5.1767601130.git.mst@xxxxxxxxxx/

Would look like
__dma_from_device_group_begin();
u8 buf[3];
__dma_from_device_group_end();

Do you think we should adopt them rather than doing our own thing?
Slowly though I don't want the noise of a mass conversion.

As normal, advantage of standard infrastructure is cutting down
in subsystem specific magic.

I 'think' result is the same (though it also forces the trailing padding if anything
comes after this and needs it).


> };
>
> static int ti_adc_read_measurement(struct ti_adc_data *data,
> @@ -78,26 +79,20 @@ static int ti_adc_read_measurement(struct ti_adc_data *data,
> int ret;
>
> switch (data->read_size) {
> - case 2: {
> - __be16 buf;
> -
> - ret = spi_read(data->spi, (void *) &buf, 2);
> + case 2:
> + ret = spi_read(data->spi, data->buf, 2);
> if (ret)
> return ret;
>
> - *val = be16_to_cpu(buf);
> + *val = get_unaligned_be16(data->buf);
> break;
> - }
> - case 3: {
> - __be32 buf;
> -
> - ret = spi_read(data->spi, (void *) &buf, 3);
> + case 3:
> + ret = spi_read(data->spi, data->buf, 3);
> if (ret)
> return ret;
>
> - *val = be32_to_cpu(buf) >> 8;
> + *val = get_unaligned_be24(data->buf);
> break;
> - }
> default:
> return -EINVAL;
> }
>