Skip to content

Commit 600b6ac

Browse files
dlechgregkh
authored andcommitted
iio: temperature: maxim_thermocouple: use DMA-safe buffer for spi_read()
commit ae5bc07 upstream. Replace using stack-allocated buffers with a DMA-safe buffer for use with spi_read(). This allows the driver to be safely used with DMA-enabled SPI controllers. The buffer array is also converted to a struct with a union to make the usage of the memory in the buffer more clear and ensure proper alignment. Fixes: 1f25ca1 ("iio: temperature: add support for Maxim thermocouple chips") Signed-off-by: David Lechner <dlechner@baylibre.com> Reviewed-by: Nuno Sá <nuno.sa@analog.com> Link: https://patch.msgid.link/20250721-iio-use-more-iio_declare_buffer_with_ts-3-v2-1-0c68d41ccf6c@baylibre.com Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 64db338 commit 600b6ac

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

drivers/iio/temperature/maxim_thermocouple.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/module.h>
1212
#include <linux/err.h>
1313
#include <linux/spi/spi.h>
14+
#include <linux/types.h>
1415
#include <linux/iio/iio.h>
1516
#include <linux/iio/sysfs.h>
1617
#include <linux/iio/trigger.h>
@@ -121,27 +122,32 @@ struct maxim_thermocouple_data {
121122
struct spi_device *spi;
122123
const struct maxim_thermocouple_chip *chip;
123124
char tc_type;
124-
125-
u8 buffer[16] __aligned(IIO_DMA_MINALIGN);
125+
/* Buffer for reading up to 2 hardware channels. */
126+
struct {
127+
union {
128+
__be16 raw16;
129+
__be32 raw32;
130+
__be16 raw[2];
131+
};
132+
aligned_s64 timestamp;
133+
} buffer __aligned(IIO_DMA_MINALIGN);
126134
};
127135

128136
static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
129137
struct iio_chan_spec const *chan, int *val)
130138
{
131139
unsigned int storage_bytes = data->chip->read_size;
132140
unsigned int shift = chan->scan_type.shift + (chan->address * 8);
133-
__be16 buf16;
134-
__be32 buf32;
135141
int ret;
136142

137143
switch (storage_bytes) {
138144
case 2:
139-
ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
140-
*val = be16_to_cpu(buf16);
145+
ret = spi_read(data->spi, &data->buffer.raw16, storage_bytes);
146+
*val = be16_to_cpu(data->buffer.raw16);
141147
break;
142148
case 4:
143-
ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
144-
*val = be32_to_cpu(buf32);
149+
ret = spi_read(data->spi, &data->buffer.raw32, storage_bytes);
150+
*val = be32_to_cpu(data->buffer.raw32);
145151
break;
146152
default:
147153
ret = -EINVAL;
@@ -166,9 +172,9 @@ static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
166172
struct maxim_thermocouple_data *data = iio_priv(indio_dev);
167173
int ret;
168174

169-
ret = spi_read(data->spi, data->buffer, data->chip->read_size);
175+
ret = spi_read(data->spi, data->buffer.raw, data->chip->read_size);
170176
if (!ret) {
171-
iio_push_to_buffers_with_ts(indio_dev, data->buffer,
177+
iio_push_to_buffers_with_ts(indio_dev, &data->buffer,
172178
sizeof(data->buffer),
173179
iio_get_time_ns(indio_dev));
174180
}

0 commit comments

Comments
 (0)