iio: Add output buffer support
Subsystems (7)
v5 cover iio: Add output buffer support
Changelog v4 -> v5:
* Remove ad3552r example from series and replace with the update of an existing driver: ad5662. Will add ad3552r in other another series.
* Make normal comment from kernel-doc comment. (A bot was complaining about that)
* Add indio_dev->info check
* Rename iio_buffer_remove_sample -> iio_pop_from_buffer
* Fix comment of remove_from: sample -> scan
* Change iio_pop_from_buffer data type to void * to be consistent with iio_push_to_buffers
* Remove use watermark, in our kernel is not used and I can't think of an usecase for it.
* Reimplement write to increment buffer index and handle blocking and noblocking calls
* Move if (insert_buffer->direction == IIO_BUFFER_DIRECTION_OUT) outside lock
* Remove redundant checks of if (insert_buffer->direction == IIO_BUFFER_DIRECTION_OUT)
Alexandru Ardelean (1):
iio: triggered-buffer: extend support to configure output buffers
Lars-Peter Clausen (1):
iio: kfifo-buffer: Add output buffer support
Mihail Chindris (4):
iio: Add output buffer support
drivers: iio: dac: ad5766: Fix dt property name Documentation:devicetree:bindings:iio:dac: Fix val drivers:iio:dac:ad5766.c: Add trigger buffer
.../bindings/iio/dac/adi,ad5766.yaml | 2 +- drivers/iio/accel/adxl372.c | 1 + drivers/iio/accel/bmc150-accel-core.c | 1 + drivers/iio/adc/at91-sama5d2_adc.c | 4 +- .../buffer/industrialio-triggered-buffer.c | 8 +- drivers/iio/buffer/kfifo_buf.c | 50 ++++++++ .../cros_ec_sensors/cros_ec_sensors_core.c | 5 +- .../common/hid-sensors/hid-sensor-trigger.c | 5 +- drivers/iio/dac/ad5766.c | 42 +++++- drivers/iio/iio_core.h | 4 + drivers/iio/industrialio-buffer.c | 120 +++++++++++++++++- drivers/iio/industrialio-core.c | 1 + include/linux/iio/buffer.h | 7 + include/linux/iio/buffer_impl.h | 11 ++ include/linux/iio/triggered_buffer.h | 11 +- 15 files changed, 256 insertions(+), 16 deletions(-)
base-commit: 94a853eca720ac9e385e59f27e859b4a01123f58
Signature · 1 line
2.27.0
PATCH v5 1/6 iio: Add output buffer support
Currently IIO only supports buffer mode for capture devices like ADCs. Add support for buffered mode for output devices like DACs.
The output buffer implementation is analogous to the input buffer implementation. Instead of using read() to get data from the buffer write() is used to copy data into the buffer.
poll() with POLLOUT will wakeup if there is space available.
Drivers can remove data from a buffer using iio_pop_from_buffer(), the function can e.g. called from a trigger handler to write the data to hardware.
A buffer can only be either a output buffer or an input, but not both. So, for a device that has an ADC and DAC path, this will mean 2 IIO buffers (one for each direction).
The direction of the buffer is decided by the new direction field of the iio_buffer struct and should be set after allocating and before registering it.
--- drivers/iio/iio_core.h | 4 + drivers/iio/industrialio-buffer.c | 120 +++++++++++++++++++++++++++++- drivers/iio/industrialio-core.c | 1 + include/linux/iio/buffer.h | 7 ++ include/linux/iio/buffer_impl.h | 11 +++ 5 files changed, 141 insertions(+), 2 deletions(-)
drivers/iio/iio_core.h +4 -0
@@ -68,12 +68,15 @@ __poll_t iio_buffer_poll_wrapper(struct file *filp, struct poll_table_struct *wait);ssize_t iio_buffer_read_wrapper (struct file *filp,char __user *buf,size_t n,loff_t *f_ps); +ssize_t iio_buffer_write_wrapper (struct file *filp,const char __user *buf, +size_t n,loff_t *f_ps);int iio_buffers_alloc_sysfs_and_mask (struct iio_dev *indio_dev);void iio_buffers_free_sysfs_and_mask (struct iio_dev *indio_dev);#define iio_buffer_poll_addr (&iio_buffer_poll_wrapper)#define iio_buffer_read_outer_addr (&iio_buffer_read_wrapper) +#define iio_buffer_write_outer_addr (&iio_buffer_write_wrapper)void iio_disable_all_buffers (struct iio_dev *indio_dev);void iio_buffer_wakeup_poll (struct iio_dev *indio_dev);
@@ -83,6 +86,7 @@ void iio_device_detach_buffers(struct iio_dev *indio_dev);#define iio_buffer_poll_addr NULL#define iio_buffer_read_outer_addr NULL +#define iio_buffer_write_outer_addr NULLstatic inline int iio_buffers_alloc_sysfs_and_mask (struct iio_dev *indio_dev) {
drivers/iio/industrialio-buffer.c +118 -2
174 lines not on this page · raw message
drivers/iio/industrialio-core.c +1 -0
@@ -1822,6 +1822,7 @@ static const struct file_operations iio_buffer_fileops = { .owner = THIS_MODULE, .llseek = noop_llseek, .read = iio_buffer_read_outer_addr, + .write = iio_buffer_write_outer_addr, .poll = iio_buffer_poll_addr, .unlocked_ioctl = iio_ioctl, .compat_ioctl = compat_ptr_ioctl,
include/linux/iio/buffer.h +7 -0
@@ -11,8 +11,15 @@struct iio_buffer ; +enum iio_buffer_direction { +IIO_BUFFER_DIRECTION_IN , +IIO_BUFFER_DIRECTION_OUT , +}; +int iio_push_to_buffers (struct iio_dev *indio_dev,const void *data); +int iio_pop_from_buffer (struct iio_buffer *buffer,void *data); +/** * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers * @indio_dev: iio_dev structure for device.
include/linux/iio/buffer_impl.h +11 -0
@@ -7,6 +7,7 @@#ifdef CONFIG_IIO_BUFFER #include <uapi/linux/iio/buffer.h> +#include <linux/iio/buffer.h> struct iio_dev ;struct iio_buffer ;
@@ -23,6 +24,10 @@ struct iio_buffer;* @read: try to get a specified number of bytes (must exist) * @data_available: indicates how much data is available for reading from * the buffer. +* @remove_from: remove scan from buffer. Drivers should calls this to +* remove a scan from a buffer. +* @write: try to write a number of bytes +* @space_available: returns the amount of bytes available in a buffer * @request_update: if a parameter change has been marked, update underlying * storage. * @set_bytes_per_datum:set number of bytes per datum
@@ -49,6 +54,9 @@ struct iio_buffer_access_funcs {int (*store_to)(struct iio_buffer *buffer,const void *data);int (*read)(struct iio_buffer *buffer,size_t n,char __user *buf);size_t (*data_available)(struct iio_buffer *buffer); +int (*remove_from)(struct iio_buffer *buffer,void *data); +int (*write)(struct iio_buffer *buffer,size_t n,const char __user *buf); +size_t (*space_available)(struct iio_buffer *buffer);int (*request_update)(struct iio_buffer *buffer);
@@ -80,6 +88,9 @@ struct iio_buffer {/** @bytes_per_datum: Size of individual datum including timestamp. */ size_t bytes_per_datum; +/* @direction: Direction of the data stream (in/out). */ +enum iio_buffer_direction direction; +/** * @access: Buffer access functions associated with the * implementation.
Signature · 1 line
2.27.0
Jonathan Cameron
maintainerQuoted 17 lines · On Thu, 16 Sep 2021 18:29:09 +0000 Mihail Chindris…
Currently IIO only supports buffer mode for capture devices like ADCs. Add support for buffered mode for output devices like DACs.
The output buffer implementation is analogous to the input buffer implementation. Instead of using
read()to get data from the bufferwrite()is used to copy data into the buffer.
poll()with POLLOUT will wakeup if there is space available.Drivers can remove data from a buffer using
iio_pop_from_buffer(), the function can e.g. called from a trigger handler to write the data to hardware.A buffer can only be either a output buffer or an input, but not both. So, for a device that has an ADC and DAC path, this will mean 2 IIO buffers (one for each direction).
Quoted 7 lines
The direction of the buffer is decided by the new direction field of the
iio_bufferstruct and should be set after allocating and before registering it.Signed-off-by Lars-Peter Clausen <[email protected]>Signed-off-by Alexandru Ardelean <[email protected]>Signed-off-by Mihail Chindris <[email protected]>
A few minor things inline. I would have expected the missing check on insert_buffer to have resulted in a nasty deference of a null pointer though which does make me nervous about whether we have tested this series enough.
Jonathan
Quoted 161 lines
--- drivers/iio/iio_core.h | 4 + drivers/iio/industrialio-buffer.c | 120 +++++++++++++++++++++++++++++- drivers/iio/industrialio-core.c | 1 + include/linux/iio/buffer.h | 7 ++ include/linux/iio/buffer_impl.h | 11 +++ 5 files changed, 141 insertions(+), 2 deletions(-)drivers/iio/iio_core.h +4
-0@@ -68,12 +68,15 @@ __poll_t iio_buffer_poll_wrapper(struct file *filp, struct poll_table_struct *wait);ssize_t iio_buffer_read_wrapper (struct file *filp,char __user *buf,size_t n,loff_t *f_ps); +ssize_t iio_buffer_write_wrapper (struct file *filp,const char __user *buf, +size_t n,loff_t *f_ps);int iio_buffers_alloc_sysfs_and_mask (struct iio_dev *indio_dev);void iio_buffers_free_sysfs_and_mask (struct iio_dev *indio_dev);#define iio_buffer_poll_addr (&iio_buffer_poll_wrapper)#define iio_buffer_read_outer_addr (&iio_buffer_read_wrapper) +#define iio_buffer_write_outer_addr (&iio_buffer_write_wrapper)void iio_disable_all_buffers (struct iio_dev *indio_dev);void iio_buffer_wakeup_poll (struct iio_dev *indio_dev);@@ -83,6 +86,7 @@ void iio_device_detach_buffers(struct iio_dev *indio_dev);#define iio_buffer_poll_addr NULL#define iio_buffer_read_outer_addr NULL +#define iio_buffer_write_outer_addr NULLstatic inline int iio_buffers_alloc_sysfs_and_mask (struct iio_dev *indio_dev) {drivers/iio/industrialio-buffer.c +90
-2@@ -161,6 +161,62 @@ static ssize_t iio_buffer_read(struct file *filp, char __user *buf,return ret; } +static size_t iio_buffer_space_available (struct iio_buffer *buf) +{ +if (buf->access->space_available) +return buf->access->space_available (buf); + +return SIZE_MAX ; +} + +static ssize_t iio_buffer_write (struct file *filp,const char __user *buf, +size_t n,loff_t *f_ps) +{ +struct iio_dev_buffer_pair *ib = filp->private_data; +struct iio_buffer *rb = ib->buffer; +struct iio_dev *indio_dev = ib->indio_dev; +DEFINE_WAIT_FUNC (wait, woken_wake_function); +int ret; +size_t written; + +if (!indio_dev->info) +return -ENODEV ; + +if (!rb || !rb->access->write) +return -EINVAL ; + + written =0 ; +add_wait_queue (&rb->pollq, &wait); +do { +if (indio_dev->info ==NULL ) +return -ENODEV ; + +if (!iio_buffer_space_available (rb)) { +if (signal_pending (current)) { + ret = -ERESTARTSYS ; +break ; + } + +wait_woken (&wait,TASK_INTERRUPTIBLE , +MAX_SCHEDULE_TIMEOUT ); +continue ; + } + + ret = rb->access->write (rb, n - written, buf + written); +if (ret ==0 && (filp->f_flags &O_NONBLOCK )) + ret = -EAGAIN ; + +if (ret >0 ) { + written += ret; +if (written != n && !(filp->f_flags &O_NONBLOCK )) +continue ; + } + }while (ret ==0 ); +remove_wait_queue (&rb->pollq, &wait); + +return ret <0 ? ret : n; +} +/** * iio_buffer_poll() - poll the buffer to find out if it has data * @filp: File structure pointer for device access @@ -181,8 +237,18 @@ static __poll_t iio_buffer_poll(struct file *filp,return 0 ;poll_wait (filp, &rb->pollq, wait); -if (iio_buffer_ready (indio_dev, rb, rb->watermark,0 )) -return EPOLLIN |EPOLLRDNORM ; + +switch (rb->direction) { +case IIO_BUFFER_DIRECTION_IN : +if (iio_buffer_ready (indio_dev, rb, rb->watermark,0 )) +return EPOLLIN |EPOLLRDNORM ; +break ; +case IIO_BUFFER_DIRECTION_OUT : +if (iio_buffer_space_available (rb)) +return EPOLLOUT |EPOLLWRNORM ; +break ; + } +return 0 ; }@@ -199,6 +265,19 @@ ssize_t iio_buffer_read_wrapper(struct file *filp, char __user *buf,return iio_buffer_read (filp, buf, n, f_ps); } +ssize_t iio_buffer_write_wrapper (struct file *filp,const char __user *buf, +size_t n,loff_t *f_ps) +{ +struct iio_dev_buffer_pair *ib = filp->private_data; +struct iio_buffer *rb = ib->buffer; + +/* check if buffer was opened through new API */ +if (test_bit (IIO_BUSY_BIT_POS , &rb->flags)) +return -EBUSY ; + +return iio_buffer_write (filp, buf, n, f_ps); +} +__poll_t iio_buffer_poll_wrapper (struct file *filp,struct poll_table_struct *wait) {@@ -231,6 +310,15 @@ void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) } } +int iio_pop_from_buffer (struct iio_buffer *buffer,void *data) +{ +if (!buffer || !buffer->access || !buffer->access->remove_from) +return -EINVAL ; + +return buffer->access->remove_from (buffer, data); +} +EXPORT_SYMBOL_GPL (iio_pop_from_buffer); +void iio_buffer_init (struct iio_buffer *buffer) {INIT_LIST_HEAD (&buffer->demux_list);
drivers/iio/industrialio-buffer.c int iio_update_buffers(struct iio_dev *indio_dev, · line 1249
Quoted 6 lines
drivers/iio/industrialio-buffer.c +3
-0@@ -1156,6 +1244,9 @@ int iio_update_buffers(struct iio_dev *indio_dev,if (insert_buffer == remove_buffer)return 0 ; +if (insert_buffer->direction ==IIO_BUFFER_DIRECTION_OUT ) + ret = -EINVAL ; +
This block is unusual enough that it needs a comment to explain what the intent is. You are poking in an error code, but then continuing...
I'm fairly sure you need to check
if (insert_buffer && (insert_buffer->direction == IIO_BUFFER_DIRECTION_OUT))
The lack of that test makes me thing this will blow up when called to remove an output buffer.
Quoted 27 lines
drivers/iio/industrialio-buffer.c +16
-0mutex_lock (&iio_dev_opaque->info_exist_lock);mutex_lock (&indio_dev->mlock);@@ -1277,6 +1368,22 @@ static ssize_t iio_dma_show_data_available(struct device *dev,return sysfs_emit (buf,"%zu\n" ,iio_buffer_data_available (buffer)); } +static ssize_t direction_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_buffer *buffer =to_iio_dev_attr (attr)->buffer; + +switch (buffer->direction) { +case IIO_BUFFER_DIRECTION_IN : +return sprintf (buf,"in\n" ); +case IIO_BUFFER_DIRECTION_OUT : +return sprintf (buf,"out\n" ); +default : +return -EINVAL ; + } +} + static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, iio_buffer_write_length); static struct device_attribute dev_attr_length_ro = __ATTR(length,
drivers/iio/industrialio-buffer.c static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark, · line 1403
Quoted 8 lines
drivers/iio/industrialio-buffer.c +4
-0@@ -1289,12 +1396,20 @@ static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark, S_IRUGO, iio_buffer_show_watermark, NULL); static DEVICE_ATTR(data_available, S_IRUGO, iio_dma_show_data_available, NULL); +static DEVICE_ATTR_RO (direction); +/* +* When adding new attributes here, put the at the end, at least until +* the code that handles the lengh/length_ro & watermark/watermark_ro
length/length_ro
Quoted 95 lines
drivers/iio/industrialio-buffer.c +5
-0+* assignments gets cleaned up. Otherwise these can create some weird +* duplicate attributes errors under some setups. +*/ static struct attribute *iio_buffer_attrs[] = { &dev_attr_length.attr, &dev_attr_enable.attr, &dev_attr_watermark.attr, &dev_attr_data_available.attr, + &dev_attr_direction.attr, };#define to_dev_attr (_attr) container_of(_attr, struct device_attribute, attr)@@ -1397,6 +1512,7 @@ static const struct file_operations iio_buffer_chrdev_fileops = { .owner = THIS_MODULE, .llseek = noop_llseek, .read = iio_buffer_read, + .write = iio_buffer_write, .poll = iio_buffer_poll, .release = iio_buffer_chrdev_release, };drivers/iio/industrialio-core.c +1
-0@@ -1822,6 +1822,7 @@ static const struct file_operations iio_buffer_fileops = { .owner = THIS_MODULE, .llseek = noop_llseek, .read = iio_buffer_read_outer_addr, + .write = iio_buffer_write_outer_addr, .poll = iio_buffer_poll_addr, .unlocked_ioctl = iio_ioctl, .compat_ioctl = compat_ptr_ioctl,include/linux/iio/buffer.h +7
-0@@ -11,8 +11,15 @@struct iio_buffer ; +enum iio_buffer_direction { +IIO_BUFFER_DIRECTION_IN , +IIO_BUFFER_DIRECTION_OUT , +}; +int iio_push_to_buffers (struct iio_dev *indio_dev,const void *data); +int iio_pop_from_buffer (struct iio_buffer *buffer,void *data); +/** * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers * @indio_dev: iio_dev structure for device. include/linux/iio/buffer_impl.h +11
-0@@ -7,6 +7,7 @@#ifdef CONFIG_IIO_BUFFER #include <uapi/linux/iio/buffer.h> +#include <linux/iio/buffer.h> struct iio_dev ;struct iio_buffer ;@@ -23,6 +24,10 @@ struct iio_buffer;* @read: try to get a specified number of bytes (must exist) * @data_available: indicates how much data is available for reading from * the buffer. +* @remove_from: remove scan from buffer. Drivers should calls this to +* remove a scan from a buffer. +* @write: try to write a number of bytes +* @space_available: returns the amount of bytes available in a buffer * @request_update: if a parameter change has been marked, update underlying * storage. * @set_bytes_per_datum:set number of bytes per datum @@ -49,6 +54,9 @@ struct iio_buffer_access_funcs {int (*store_to)(struct iio_buffer *buffer,const void *data);int (*read)(struct iio_buffer *buffer,size_t n,char __user *buf);size_t (*data_available)(struct iio_buffer *buffer); +int (*remove_from)(struct iio_buffer *buffer,void *data); +int (*write)(struct iio_buffer *buffer,size_t n,const char __user *buf); +size_t (*space_available)(struct iio_buffer *buffer);int (*request_update)(struct iio_buffer *buffer);@@ -80,6 +88,9 @@ struct iio_buffer {/** @bytes_per_datum: Size of individual datum including timestamp. */ size_t bytes_per_datum; +/* @direction: Direction of the data stream (in/out). */ +enum iio_buffer_direction direction; +/** * @access: Buffer access functions associated with the * implementation.
Sa, Nuno
maintainerQuoted 116 lines
From: Jonathan Cameron <[email protected]>
Sent: Sunday, September 19, 2021 7:03 PM
To: Chindris, Mihail <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Hennerich, Michael <[email protected]>; Sa, Nuno <[email protected]>; Bogdan, Dragos <[email protected]>; [email protected]
Subject: Re: [PATCH v5 1/6] iio: Add output buffer supportQuoted 1 line · On Thu, 16 Sep 2021 18:29:09 +0000 Mihail Chindris…
Currently IIO only supports buffer mode for capture devices like
ADCs. Add
Quoted 4 lines
support for buffered mode for output devices like DACs.
The output buffer implementation is analogous to the input buffer implementation. Instead of using
read()to get data from the buffer
write()Quoted 5 lines
is used to copy data into the buffer.
poll()with POLLOUT will wakeup if there is space available.Drivers can remove data from a buffer using
iio_pop_from_buffer(),the
Quoted 4 lines
function can e.g. called from a trigger handler to write the data to hardware.
A buffer can only be either a output buffer or an input, but not both.
So,
Quoted 1 line
for a device that has an ADC and DAC path, this will mean 2 IIO
buffers
Quoted 3 lines
(one for each direction).
The direction of the buffer is decided by the new direction field of
the
Quoted 1 line
iio_bufferstruct and should be set after allocating and beforeregistering
Quoted 4 lines
it.
Signed-off-by Lars-Peter Clausen <[email protected]>Signed-off-by Alexandru ArdeleanQuoted 1 line
Signed-off-by Mihail Chindris <[email protected]>A few minor things inline. I would have expected the missing check on
insert_bufferto have resulted in a nasty deference of a null pointer though which does make me nervous about whether we have tested this
series enough.Jonathan
Quoted 3 lines
--- drivers/iio/iio_core.h | 4 + drivers/iio/industrialio-buffer.c | 120+++++++++++++++++++++++++++++-
drivers/iio/iio_core.h __poll_t iio_buffer_poll_wrapper(struct file
Quoted 10 lines
drivers/iio/industrialio-core.c | 1 + include/linux/iio/buffer.h | 7 ++ include/linux/iio/buffer_impl.h | 11 +++ 5 files changed, 141 insertions(+), 2 deletions(-)drivers/iio/iio_core.h +0
-0@@ -68,12 +68,15 @@ __poll_t iio_buffer_poll_wrapper(struct file*filp,
drivers/iio/iio_core.h __poll_t iio_buffer_poll_wrapper(struct file *filp,
Quoted 4 lines
drivers/iio/iio_core.h +1
-0struct poll_table_struct *wait);ssize_t iio_buffer_read_wrapper (struct file *filp,char __user *buf,size_t n,loff_t *f_ps); +ssize_t iio_buffer_write_wrapper(struct file *filp, const char __user*buf,
drivers/iio/iio_core.h __poll_t iio_buffer_poll_wrapper(struct file *filp,
Quoted 8 lines
drivers/iio/iio_core.h +2
-0+ size_t n, loff_t *f_ps);int iio_buffers_alloc_sysfs_and_mask (struct iio_dev *indio_dev);void iio_buffers_free_sysfs_and_mask (struct iio_dev *indio_dev);#define iio_buffer_poll_addr (&iio_buffer_poll_wrapper)#define iio_buffer_read_outer_addr (&iio_buffer_read_wrapper) +#define iio_buffer_write_outer_addr(
&iio_buffer_write_wrapper)drivers/iio/iio_core.h void iio_device_detach_buffers(struct iio_dev
Quoted 4 lines
drivers/iio/iio_core.h +0
-0void iio_disable_all_buffers (struct iio_dev *indio_dev);void iio_buffer_wakeup_poll (struct iio_dev *indio_dev);@@ -83,6 +86,7 @@ void iio_device_detach_buffers(struct iio_dev
*indio_dev);drivers/iio/iio_core.h void iio_device_detach_buffers(struct iio_dev *indio_dev);
Quoted 6 lines
drivers/iio/iio_core.h +1
-0#define iio_buffer_poll_addr NULL#define iio_buffer_read_outer_addr NULL +#define iio_buffer_write_outer_addr NULL static inline int iio_buffers_alloc_sysfs_and_mask(struct iio_dev
*indio_dev)drivers/iio/industrialio-
Quoted 2 lines
drivers/iio/iio_core.h +0
-0{drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0
-0
buffer.cdrivers/iio/industrialio-buffer.c static ssize_t iio_buffer_read(struct file *filp,
Quoted 4 lines
index
a95cc2da56be..a2a34c5652a7100644drivers/iio/industrialio-buffer.c +0
-0@@ -161,6 +161,62 @@ static ssize_t iio_buffer_read(struct file *filp,char
__user*buf,drivers/iio/industrialio-buffer.c static ssize_t iio_buffer_read(struct file *filp, char __user *buf,
Quoted 12 lines
drivers/iio/industrialio-buffer.c +9
-0return ret; } +static size_t iio_buffer_space_available (struct iio_buffer *buf) +{ +if (buf->access->space_available) +return buf->access->space_available (buf); + +return SIZE_MAX ; +} + +static ssize_t iio_buffer_write(struct file *filp, const char __user*buf,
Quoted 15 lines
Quoted 5 lines
drivers/iio/industrialio-buffer.c +5
-0+ size_t n, loff_t *f_ps) +{ + struct iio_dev_buffer_pair *ib = filp->private_data; +struct iio_buffer *rb = ib->buffer; +struct iio_dev *indio_dev = ib->indio_dev;drivers/iio/industrialio-buffer.c static ssize_t iio_buffer_read(struct file *filp, char __user *buf, · line 187
Quoted 10 lines
drivers/iio/industrialio-buffer.c +10
-0+DEFINE_WAIT_FUNC (wait, woken_wake_function); +int ret; +size_t written; + +if (!indio_dev->info) +return -ENODEV ; + +if (!rb || !rb->access->write) +return -EINVAL ; +
As the buffer implementation can support both 'read()' and 'write()', the following check might make sense:
if (rb->direction != IIO_BUFFER_DIRECTION_OUT)
return -EPERM;
If going with this, we should add an extra patch to do a similar thing on the read side...
- Nuno Sá
Jonathan Cameron
maintainerQuoted 135 lines · Nuno
Quoted 116 lines
From: Jonathan Cameron <[email protected]>
Sent: Sunday, September 19, 2021 7:03 PM
To: Chindris, Mihail <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Hennerich, Michael <[email protected]>; Sa, Nuno <[email protected]>; Bogdan, Dragos <[email protected]>; [email protected]
Subject: Re: [PATCH v5 1/6] iio: Add output buffer supportQuoted 1 line · On Thu, 16 Sep 2021 18:29:09 +0000 Mihail Chindris…
Currently IIO only supports buffer mode for capture devices like
ADCs. Add
Quoted 4 lines
support for buffered mode for output devices like DACs.
The output buffer implementation is analogous to the input buffer implementation. Instead of using
read()to get data from the buffer
write()Quoted 5 lines
is used to copy data into the buffer.
poll()with POLLOUT will wakeup if there is space available.Drivers can remove data from a buffer using
iio_pop_from_buffer(),the
Quoted 4 lines
function can e.g. called from a trigger handler to write the data to hardware.
A buffer can only be either a output buffer or an input, but not both.
So,
Quoted 1 line
for a device that has an ADC and DAC path, this will mean 2 IIO
buffers
Quoted 3 lines
(one for each direction).
The direction of the buffer is decided by the new direction field of
the
Quoted 1 line
iio_bufferstruct and should be set after allocating and beforeregistering
Quoted 4 lines
it.
Signed-off-by Lars-Peter Clausen <[email protected]>Signed-off-by Alexandru ArdeleanQuoted 1 line
Signed-off-by Mihail Chindris <[email protected]>A few minor things inline. I would have expected the missing check on
insert_bufferto have resulted in a nasty deference of a null pointer though which does make me nervous about whether we have tested this
series enough.Jonathan
Quoted 3 lines
--- drivers/iio/iio_core.h | 4 + drivers/iio/industrialio-buffer.c | 120+++++++++++++++++++++++++++++-
drivers/iio/iio_core.h __poll_t iio_buffer_poll_wrapper(struct file
Quoted 10 lines
drivers/iio/industrialio-core.c | 1 + include/linux/iio/buffer.h | 7 ++ include/linux/iio/buffer_impl.h | 11 +++ 5 files changed, 141 insertions(+), 2 deletions(-)drivers/iio/iio_core.h +0
-0@@ -68,12 +68,15 @@ __poll_t iio_buffer_poll_wrapper(struct file*filp,
drivers/iio/iio_core.h __poll_t iio_buffer_poll_wrapper(struct file *filp,
Quoted 4 lines
drivers/iio/iio_core.h +1
-0struct poll_table_struct *wait);ssize_t iio_buffer_read_wrapper (struct file *filp,char __user *buf,size_t n,loff_t *f_ps); +ssize_t iio_buffer_write_wrapper(struct file *filp, const char __user*buf,
drivers/iio/iio_core.h __poll_t iio_buffer_poll_wrapper(struct file *filp,
Quoted 8 lines
drivers/iio/iio_core.h +2
-0+ size_t n, loff_t *f_ps);int iio_buffers_alloc_sysfs_and_mask (struct iio_dev *indio_dev);void iio_buffers_free_sysfs_and_mask (struct iio_dev *indio_dev);#define iio_buffer_poll_addr (&iio_buffer_poll_wrapper)#define iio_buffer_read_outer_addr (&iio_buffer_read_wrapper) +#define iio_buffer_write_outer_addr(
&iio_buffer_write_wrapper)drivers/iio/iio_core.h void iio_device_detach_buffers(struct iio_dev
Quoted 4 lines
drivers/iio/iio_core.h +0
-0void iio_disable_all_buffers (struct iio_dev *indio_dev);void iio_buffer_wakeup_poll (struct iio_dev *indio_dev);@@ -83,6 +86,7 @@ void iio_device_detach_buffers(struct iio_dev
*indio_dev);drivers/iio/iio_core.h void iio_device_detach_buffers(struct iio_dev *indio_dev);
Quoted 6 lines
drivers/iio/iio_core.h +1
-0#define iio_buffer_poll_addr NULL#define iio_buffer_read_outer_addr NULL +#define iio_buffer_write_outer_addr NULL static inline int iio_buffers_alloc_sysfs_and_mask(struct iio_dev
*indio_dev)drivers/iio/industrialio-
Quoted 2 lines
drivers/iio/iio_core.h +0
-0{drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0
-0
buffer.cdrivers/iio/industrialio-buffer.c static ssize_t iio_buffer_read(struct file *filp,
Quoted 4 lines
index
a95cc2da56be..a2a34c5652a7100644drivers/iio/industrialio-buffer.c +0
-0@@ -161,6 +161,62 @@ static ssize_t iio_buffer_read(struct file *filp,char
__user*buf,drivers/iio/industrialio-buffer.c static ssize_t iio_buffer_read(struct file *filp, char __user *buf,
Quoted 12 lines
drivers/iio/industrialio-buffer.c +9
-0return ret; } +static size_t iio_buffer_space_available (struct iio_buffer *buf) +{ +if (buf->access->space_available) +return buf->access->space_available (buf); + +return SIZE_MAX ; +} + +static ssize_t iio_buffer_write(struct file *filp, const char __user*buf,
Quoted 15 lines
Quoted 5 lines
drivers/iio/industrialio-buffer.c +5
-0+ size_t n, loff_t *f_ps) +{ + struct iio_dev_buffer_pair *ib = filp->private_data; +struct iio_buffer *rb = ib->buffer; +struct iio_dev *indio_dev = ib->indio_dev;drivers/iio/industrialio-buffer.c static ssize_t iio_buffer_read(struct file *filp, char __user *buf, · line 187
Quoted 10 lines
drivers/iio/industrialio-buffer.c +10
-0+DEFINE_WAIT_FUNC (wait, woken_wake_function); +int ret; +size_t written; + +if (!indio_dev->info) +return -ENODEV ; + +if (!rb || !rb->access->write) +return -EINVAL ; +As the buffer implementation can support both '
read()' and 'write()', the following check might make sense:
Quoted 2 lines
if (
rb->direction!=IIO_BUFFER_DIRECTION_OUT)return -EPERM;
Makes sense. Whether EPERM is the right error code is a different question. Doesn't seem perfectly aligned with this case, but it's not too bad.
Jonathan
Quoted 5 lines
If going with this, we should add an extra patch to do a similar thing on the read side...
- Nuno Sá
PATCH v5 2/6 iio: kfifo-buffer: Add output buffer support
In reply to Mihail ChindrisFrom: Lars-Peter Clausen <[email protected]>
Add output buffer support to the kfifo buffer implementation.
The implementation is straight forward and mostly just wraps the kfifo API to provide the required operations.
--- drivers/iio/buffer/kfifo_buf.c | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+)
drivers/iio/buffer/kfifo_buf.c +50 -0
@@ -138,10 +138,60 @@ static void iio_kfifo_buffer_release(struct iio_buffer *buffer)kfree (kf); } +static size_t iio_kfifo_buf_space_available (struct iio_buffer *r) +{ +struct iio_kfifo *kf =iio_to_kfifo (r); +size_t avail; + +mutex_lock (&kf->user_lock); + avail =kfifo_avail (&kf->kf); +mutex_unlock (&kf->user_lock); + +return avail; +} + +static int iio_kfifo_remove_from (struct iio_buffer *r,void *data) +{ +int ret; +struct iio_kfifo *kf =iio_to_kfifo (r); + +if (kfifo_size (&kf->kf) <1 ) +return -EBUSY ; + + ret =kfifo_out (&kf->kf, data,1 ); +if (ret !=1 ) +return -EBUSY ; + +wake_up_interruptible_poll (&r->pollq,POLLOUT |POLLWRNORM ); + +return 0 ; +} + +static int iio_kfifo_write (struct iio_buffer *r,size_t n, +const char __user *buf) +{ +struct iio_kfifo *kf =iio_to_kfifo (r); +int ret, copied; + +mutex_lock (&kf->user_lock); +if (!kfifo_initialized (&kf->kf) || n <kfifo_esize (&kf->kf)) + ret = -EINVAL ; +else + ret =kfifo_from_user (&kf->kf, buf, n, &copied); +mutex_unlock (&kf->user_lock); +if (ret) +return ret; + +return copied; +} +static const struct iio_buffer_access_funcs kfifo_access_funcs = { .store_to = &iio_store_to_kfifo, .read = &iio_read_kfifo, .data_available = iio_kfifo_buf_data_available, + .remove_from = &iio_kfifo_remove_from, + .write = &iio_kfifo_write, + .space_available = &iio_kfifo_buf_space_available, .request_update = &iio_request_update_kfifo, .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, .set_length = &iio_set_length_kfifo,
Signature · 1 line
2.27.0
PATCH v5 3/6 iio: triggered-buffer: extend support to configure output buffers
In reply to Mihail ChindrisFrom: Alexandru Ardelean <[email protected]>
Now that output (kfifo) buffers are supported, we need to extend the {devm_}iio_triggered_buffer_setup_ext() parameter list to take a direction parameter.
This allows us to attach an output triggered buffer to a DAC device. Unfortunately it's a bit difficult to add another macro to avoid changing 5 drivers where {devm_}iio_triggered_buffer_setup_ext() is used. Well, it's doable, but may not be worth the trouble vs just updating all these 5 drivers.
--- drivers/iio/accel/adxl372.c | 1 + drivers/iio/accel/bmc150-accel-core.c | 1 + drivers/iio/adc/at91-sama5d2_adc.c | 4 ++-- drivers/iio/buffer/industrialio-triggered-buffer.c | 8 ++++++-- .../iio/common/cros_ec_sensors/cros_ec_sensors_core.c | 5 +++-- drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 5 +++-- include/linux/iio/triggered_buffer.h | 11 +++++++++-- 7 files changed, 25 insertions(+), 10 deletions(-)
drivers/iio/accel/adxl372.c +1 -0
@@ -1214,6 +1214,7 @@ int adxl372_probe(struct device *dev, struct regmap *regmap, ret =devm_iio_triggered_buffer_setup_ext (dev, indio_dev,NULL , adxl372_trigger_handler, +IIO_BUFFER_DIRECTION_IN , &adxl372_buffer_ops, adxl372_fifo_attributes);if (ret <0 )
drivers/iio/accel/bmc150-accel-core.c +1 -0
@@ -1734,6 +1734,7 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq, ret =iio_triggered_buffer_setup_ext (indio_dev, &iio_pollfunc_store_time, bmc150_accel_trigger_handler, +IIO_BUFFER_DIRECTION_IN , &bmc150_accel_buffer_ops, fifo_attrs);if (ret <0 ) {
drivers/iio/adc/at91-sama5d2_adc.c +2 -2
@@ -1681,8 +1681,8 @@ static int at91_adc_buffer_and_trigger_init(struct device *dev, fifo_attrs =NULL ; ret =devm_iio_triggered_buffer_setup_ext (&indio->dev, indio, - &iio_pollfunc_store_time, - &at91_adc_trigger_handler, &at91_buffer_setup_ops, fifo_attrs); + &iio_pollfunc_store_time, &at91_adc_trigger_handler, +IIO_BUFFER_DIRECTION_IN , &at91_buffer_setup_ops, fifo_attrs);if (ret <0 ) {dev_err (dev,"couldn't initialize the buffer.\n" );return ret;
drivers/iio/buffer/industrialio-triggered-buffer.c +6 -2
@@ -19,6 +19,7 @@* @indio_dev: IIO device structure * @h: Function which will be used as pollfunc top half * @thread: Function which will be used as pollfunc bottom half +* @direction: Direction of the data stream (in/out). * @setup_ops: Buffer setup functions to use for this device. * If NULL the default setup functions for triggered * buffers will be used.
@@ -38,6 +39,7 @@int iio_triggered_buffer_setup_ext (struct iio_dev *indio_dev,irqreturn_t (*h)(int irq,void *p),irqreturn_t (*thread)(int irq,void *p), +enum iio_buffer_direction direction,const struct iio_buffer_setup_ops *setup_ops,const struct attribute **buffer_attrs) {
@@ -68,6 +70,7 @@ int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev,/* Flag that polled ring buffering is possible */ indio_dev->modes |=INDIO_BUFFER_TRIGGERED ; + buffer->direction = direction; buffer->attrs = buffer_attrs; ret =iio_device_attach_buffer (indio_dev, buffer);
@@ -105,13 +108,14 @@ int devm_iio_triggered_buffer_setup_ext(struct device *dev,struct iio_dev *indio_dev, irqreturn_t (*h)(int irq, void *p), irqreturn_t (*thread)(int irq, void *p), + enum iio_buffer_direction direction, const struct iio_buffer_setup_ops *ops, const struct attribute **buffer_attrs) { int ret; - ret =iio_triggered_buffer_setup_ext (indio_dev, h, thread, ops, - buffer_attrs); + ret =iio_triggered_buffer_setup_ext (indio_dev, h, thread, direction, + ops, buffer_attrs);if (ret)return ret;
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -2
@@ -360,8 +360,9 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,* The only way to get samples in buffer is to set a * software trigger (systrig, hrtimer). */ - ret =devm_iio_triggered_buffer_setup (dev, indio_dev, -NULL , trigger_capture,NULL ); + ret =devm_iio_triggered_buffer_setup_ext (dev, + indio_dev,NULL , trigger_capture, +IIO_BUFFER_DIRECTION_IN ,NULL );if (ret)return ret; }
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +3 -2
@@ -241,8 +241,9 @@ int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, fifo_attrs =NULL ; ret =iio_triggered_buffer_setup_ext (indio_dev, - &iio_pollfunc_store_time, -NULL ,NULL , fifo_attrs); + &iio_pollfunc_store_time,NULL , +IIO_BUFFER_DIRECTION_IN , +NULL , fifo_attrs);if (ret) {dev_err (&indio_dev->dev,"Triggered Buffer Setup Failed\n" );return ret;
include/linux/iio/triggered_buffer.h +9 -2
@@ -2,6 +2,7 @@#ifndef _LINUX_IIO_TRIGGERED_BUFFER_H_#define _LINUX_IIO_TRIGGERED_BUFFER_H_ +#include <linux/iio/buffer.h> #include <linux/interrupt.h> struct attribute ;
@@ -11,21 +12,27 @@ struct iio_buffer_setup_ops;int iio_triggered_buffer_setup_ext (struct iio_dev *indio_dev,irqreturn_t (*h)(int irq,void *p),irqreturn_t (*thread)(int irq,void *p), +enum iio_buffer_direction direction,const struct iio_buffer_setup_ops *setup_ops,const struct attribute **buffer_attrs);void iio_triggered_buffer_cleanup (struct iio_dev *indio_dev);#define iio_triggered_buffer_setup (indio_dev, h, thread, setup_ops) \ - iio_triggered_buffer_setup_ext((indio_dev), (h), (thread), (setup_ops), NULL) + iio_triggered_buffer_setup_ext((indio_dev), (h), (thread), \ + IIO_BUFFER_DIRECTION_IN, (setup_ops), \ + NULL)int devm_iio_triggered_buffer_setup_ext (struct device *dev,struct iio_dev *indio_dev,irqreturn_t (*h)(int irq,void *p),irqreturn_t (*thread)(int irq,void *p), +enum iio_buffer_direction direction,const struct iio_buffer_setup_ops *ops,const struct attribute **buffer_attrs);#define devm_iio_triggered_buffer_setup (dev, indio_dev, h, thread, setup_ops) \ - devm_iio_triggered_buffer_setup_ext((dev), (indio_dev), (h), (thread), (setup_ops), NULL) + devm_iio_triggered_buffer_setup_ext((dev), (indio_dev), (h), (thread), \ + IIO_BUFFER_DIRECTION_IN, \ + (setup_ops), NULL)#endif
Signature · 1 line
2.27.0
Jonathan Cameron
maintainerRe: [PATCH v5 3/6] iio: triggered-buffer: extend support to configure output buffers
Quoted 6 lines · On Thu, 16 Sep 2021 18:29:11 +0000 Mihail Chindris…
From: Alexandru Ardelean <[email protected]>
Now that output (kfifo) buffers are supported, we need to extend the {devm_}iio_triggered_buffer_setup_ext() parameter list to take a direction parameter.
Quoted 7 lines
This allows us to attach an output triggered buffer to a DAC device. Unfortunately it's a bit difficult to add another macro to avoid changing 5 drivers where {devm_}iio_triggered_buffer_setup_ext() is used. Well, it's doable, but may not be worth the trouble vs just updating all these 5 drivers.
Signed-off-by Alexandru Ardelean <[email protected]>
Mihael,
This one is one of the categories where you must add your own
Signed-off-by: line to fulfill the kernel DCO requirements. You are posting this patch of Alex's so you need to sign off to indicate you 'handled' the patch on the way to mainline.
Patch looks good other than that.
Thanks,
Jonathan
Quoted 168 lines
--- drivers/iio/accel/adxl372.c | 1 + drivers/iio/accel/bmc150-accel-core.c | 1 + drivers/iio/adc/at91-sama5d2_adc.c | 4 ++-- drivers/iio/buffer/industrialio-triggered-buffer.c | 8 ++++++-- .../iio/common/cros_ec_sensors/cros_ec_sensors_core.c | 5 +++-- drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 5 +++-- include/linux/iio/triggered_buffer.h | 11 +++++++++-- 7 files changed, 25 insertions(+), 10 deletions(-)drivers/iio/accel/adxl372.c +1
-0@@ -1214,6 +1214,7 @@ int adxl372_probe(struct device *dev, struct regmap *regmap, ret =devm_iio_triggered_buffer_setup_ext (dev, indio_dev,NULL , adxl372_trigger_handler, +IIO_BUFFER_DIRECTION_IN , &adxl372_buffer_ops, adxl372_fifo_attributes);if (ret <0 )drivers/iio/accel/bmc150-accel-core.c +1
-0@@ -1734,6 +1734,7 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq, ret =iio_triggered_buffer_setup_ext (indio_dev, &iio_pollfunc_store_time, bmc150_accel_trigger_handler, +IIO_BUFFER_DIRECTION_IN , &bmc150_accel_buffer_ops, fifo_attrs);if (ret <0 ) {drivers/iio/adc/at91-sama5d2_adc.c +2
-2@@ -1681,8 +1681,8 @@ static int at91_adc_buffer_and_trigger_init(struct device *dev, fifo_attrs =NULL ; ret =devm_iio_triggered_buffer_setup_ext (&indio->dev, indio, - &iio_pollfunc_store_time, - &at91_adc_trigger_handler, &at91_buffer_setup_ops, fifo_attrs); + &iio_pollfunc_store_time, &at91_adc_trigger_handler, +IIO_BUFFER_DIRECTION_IN , &at91_buffer_setup_ops, fifo_attrs);if (ret <0 ) {dev_err (dev,"couldn't initialize the buffer.\n" );return ret;drivers/iio/buffer/industrialio-triggered-buffer.c +6
-2@@ -19,6 +19,7 @@* @indio_dev: IIO device structure * @h: Function which will be used as pollfunc top half * @thread: Function which will be used as pollfunc bottom half +* @direction: Direction of the data stream (in/out). * @setup_ops: Buffer setup functions to use for this device. * If NULL the default setup functions for triggered * buffers will be used. @@ -38,6 +39,7 @@int iio_triggered_buffer_setup_ext (struct iio_dev *indio_dev,irqreturn_t (*h)(int irq,void *p),irqreturn_t (*thread)(int irq,void *p), +enum iio_buffer_direction direction,const struct iio_buffer_setup_ops *setup_ops,const struct attribute **buffer_attrs) {@@ -68,6 +70,7 @@ int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev,/* Flag that polled ring buffering is possible */ indio_dev->modes |=INDIO_BUFFER_TRIGGERED ; + buffer->direction = direction; buffer->attrs = buffer_attrs; ret =iio_device_attach_buffer (indio_dev, buffer);@@ -105,13 +108,14 @@ int devm_iio_triggered_buffer_setup_ext(struct device *dev,struct iio_dev *indio_dev, irqreturn_t (*h)(int irq, void *p), irqreturn_t (*thread)(int irq, void *p), + enum iio_buffer_direction direction, const struct iio_buffer_setup_ops *ops, const struct attribute **buffer_attrs) { int ret; - ret =iio_triggered_buffer_setup_ext (indio_dev, h, thread, ops, - buffer_attrs); + ret =iio_triggered_buffer_setup_ext (indio_dev, h, thread, direction, + ops, buffer_attrs);if (ret)return ret;drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3
-2@@ -360,8 +360,9 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,* The only way to get samples in buffer is to set a * software trigger (systrig, hrtimer). */ - ret =devm_iio_triggered_buffer_setup (dev, indio_dev, -NULL , trigger_capture,NULL ); + ret =devm_iio_triggered_buffer_setup_ext (dev, + indio_dev,NULL , trigger_capture, +IIO_BUFFER_DIRECTION_IN ,NULL );if (ret)return ret; }drivers/iio/common/hid-sensors/hid-sensor-trigger.c +3
-2@@ -241,8 +241,9 @@ int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, fifo_attrs =NULL ; ret =iio_triggered_buffer_setup_ext (indio_dev, - &iio_pollfunc_store_time, -NULL ,NULL , fifo_attrs); + &iio_pollfunc_store_time,NULL , +IIO_BUFFER_DIRECTION_IN , +NULL , fifo_attrs);if (ret) {dev_err (&indio_dev->dev,"Triggered Buffer Setup Failed\n" );return ret;include/linux/iio/triggered_buffer.h +9
-2@@ -2,6 +2,7 @@#ifndef _LINUX_IIO_TRIGGERED_BUFFER_H_#define _LINUX_IIO_TRIGGERED_BUFFER_H_ +#include <linux/iio/buffer.h> #include <linux/interrupt.h> struct attribute ;@@ -11,21 +12,27 @@ struct iio_buffer_setup_ops;int iio_triggered_buffer_setup_ext (struct iio_dev *indio_dev,irqreturn_t (*h)(int irq,void *p),irqreturn_t (*thread)(int irq,void *p), +enum iio_buffer_direction direction,const struct iio_buffer_setup_ops *setup_ops,const struct attribute **buffer_attrs);void iio_triggered_buffer_cleanup (struct iio_dev *indio_dev);#define iio_triggered_buffer_setup (indio_dev, h, thread, setup_ops) \ - iio_triggered_buffer_setup_ext((indio_dev), (h), (thread), (setup_ops), NULL) + iio_triggered_buffer_setup_ext((indio_dev), (h), (thread), \ + IIO_BUFFER_DIRECTION_IN, (setup_ops), \ + NULL)int devm_iio_triggered_buffer_setup_ext (struct device *dev,struct iio_dev *indio_dev,irqreturn_t (*h)(int irq,void *p),irqreturn_t (*thread)(int irq,void *p), +enum iio_buffer_direction direction,const struct iio_buffer_setup_ops *ops,const struct attribute **buffer_attrs);#define devm_iio_triggered_buffer_setup (dev, indio_dev, h, thread, setup_ops) \ - devm_iio_triggered_buffer_setup_ext((dev), (indio_dev), (h), (thread), (setup_ops), NULL) + devm_iio_triggered_buffer_setup_ext((dev), (indio_dev), (h), (thread), \ + IIO_BUFFER_DIRECTION_IN, \ + (setup_ops), NULL)#endif
PATCH v5 4/6 drivers: iio: dac: ad5766: Fix dt property name
In reply to Mihail ChindrisIn the documentation the name for the property is output-range-microvolts which is a standard name, therefore this name must be used.
--- drivers/iio/dac/ad5766.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/dac/ad5766.c +3 -3
@@ -503,13 +503,13 @@ static int ad5766_get_output_range(struct ad5766_state *st)int i, ret, min, max, tmp[2 ]; ret =device_property_read_u32_array (&st->spi->dev, -"output-range-voltage" , +"output-range-microvolts" , tmp,2 );if (ret)return ret; - min = tmp[0 ] /1000 ; - max = tmp[1 ] /1000 ; + min = tmp[0 ] /1000000 ; + max = tmp[1 ] /1000000 ;for (i =0 ; i <ARRAY_SIZE (ad5766_span_tbl); i++) {if (ad5766_span_tbl[i].min != min || ad5766_span_tbl[i].max != max)
Signature · 1 line
2.27.0
Re: [PATCH v5 4/6] drivers: iio: dac: ad5766: Fix dt property name
Quoted 5 lines · Mihail Chindris
In the documentation the name for the property is output-range-microvolts which is a standard name, therefore this name must be used.
This requires a Fixes tag.
With that addressed:
Quoted 29 lines
Signed-off-by Mihail Chindris <[email protected]>--- drivers/iio/dac/ad5766.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/iio/dac/ad5766.c +3
-4@@ -503,13 +503,13 @@ static int ad5766_get_output_range(struct ad5766_state *st)int i, ret, min, max, tmp[2 ]; ret =device_property_read_u32_array (&st->spi->dev, -"output-range-voltage" , +"output-range-microvolts" , tmp,2 );if (ret)return ret; - min = tmp[0 ] /1000 ; - max = tmp[1 ] /1000 ; + min = tmp[0 ] /1000000 ; + max = tmp[1 ] /1000000 ;for (i =0 ; i <ARRAY_SIZE (ad5766_span_tbl); i++) {if (ad5766_span_tbl[i].min != min || ad5766_span_tbl[i].max != max) --2.27.0
Jonathan Cameron
maintainerRe: [PATCH v5 4/6] drivers: iio: dac: ad5766: Fix dt property name
Quoted 8 lines · On Fri, 17 Sep 2021 10:53:54 +0300 Alexandru Ardelean <[email protected]>…
Quoted 5 lines · Mihail Chindris
In the documentation the name for the property is output-range-microvolts which is a standard name, therefore this name must be used.
Quoted 4 lines
This requires a Fixes tag.
With that addressed:Reviewed-by Alexandru Ardelean <[email protected]>
Good catch. These cases are always a mess.
As the binding doc went in with the driver, there should have been no window in which anyone who was actually checking their DTS files against the yaml could have this wrong. Hopefully that means we don't have any broken ones out there in the wild
As this is binding related, sensible to cc Rob and the dt list. +CC
Quoted 30 lines
Quoted 30 lines
Signed-off-by Mihail Chindris <[email protected]>--- drivers/iio/dac/ad5766.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/iio/dac/ad5766.c +3
-4@@ -503,13 +503,13 @@ static int ad5766_get_output_range(struct ad5766_state *st)int i, ret, min, max, tmp[2 ]; ret =device_property_read_u32_array (&st->spi->dev, -"output-range-voltage" , +"output-range-microvolts" , tmp,2 );if (ret)return ret; - min = tmp[0 ] /1000 ; - max = tmp[1 ] /1000 ; + min = tmp[0 ] /1000000 ; + max = tmp[1 ] /1000000 ;for (i =0 ; i <ARRAY_SIZE (ad5766_span_tbl); i++) {if (ad5766_span_tbl[i].min != min || ad5766_span_tbl[i].max != max) --2.27.0
PATCH v5 5/6 Documentation:devicetree:bindings:iio:dac: Fix val
In reply to Mihail ChindrisA correct value for output-range-microvolts is -5 to 5 Volts not -5 to 5 milivolts
--- Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml +1 -1
@@ -54,7 +54,7 @@ examples: ad5766@0 { compatible = "adi,ad5766"; - output-range-microvolts = <(-5000) 5000>; + output-range-microvolts = <(-5000000) 5000000>; reg = <0>; spi-cpol; spi-max-frequency = <1000000>;
Signature · 1 line
2.27.0
Re: [PATCH v5 5/6] Documentation:devicetree:bindings:iio:dac: Fix val
Quoted 4 lines · Mihail Chindris
A correct value for output-range-microvolts is -5 to 5 Volts not -5 to 5 milivolts
Quoted 21 lines
Signed-off-by Mihail Chindris <[email protected]>--- Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml +1
-2@@ -54,7 +54,7 @@ examples: ad5766@0 { compatible = "adi,ad5766"; - output-range-microvolts = <(-5000) 5000>; + output-range-microvolts = <(-5000000) 5000000>; reg = <0>; spi-cpol; spi-max-frequency = <1000000>; --2.27.0
Jonathan Cameron
maintainerRe: [PATCH v5 5/6] Documentation:devicetree:bindings:iio:dac: Fix val
Quoted 8 lines · On Fri, 17 Sep 2021 10:53:02 +0300 Alexandru Ardelean <[email protected]>…
Quoted 4 lines · Mihail Chindris
A correct value for output-range-microvolts is -5 to 5 Volts not -5 to 5 milivolts
Reviewed-by Alexandru Ardelean <[email protected]>
+CC DT maintainer and list.
Please provide a suitable Fixes tag.
Thanks,
Jonathan
Quoted 22 lines
Quoted 22 lines
Signed-off-by Mihail Chindris <[email protected]>--- Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml +1
-2@@ -54,7 +54,7 @@ examples: ad5766@0 { compatible = "adi,ad5766"; - output-range-microvolts = <(-5000) 5000>; + output-range-microvolts = <(-5000000) 5000000>; reg = <0>; spi-cpol; spi-max-frequency = <1000000>; --2.27.0
PATCH v5 6/6 drivers:iio:dac:ad5766.c: Add trigger buffer
In reply to Mihail ChindrisThis chip is able to generate waveform and using an with the output trigger buffer will be easy to generate one.
--- drivers/iio/dac/ad5766.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
drivers/iio/dac/ad5766.c +36 -0
@@ -5,10 +5,13 @@* Copyright 2019-2020 Analog Devices Inc. */ #include <linux/bitfield.h> +#include <linux/bitops.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/gpio/consumer.h> #include <linux/iio/iio.h> +#include <linux/iio/triggered_buffer.h> +#include <linux/iio/trigger_consumer.h> #include <linux/module.h> #include <linux/spi/spi.h> #include <asm/unaligned.h>
@@ -41,6 +44,7 @@#define AD5766_CMD_DITHER_SCALE_2 0xD0#define AD5766_FULL_RESET_CODE 0x1234 +#define AD5766_NUM_CH 16enum ad5766_type {ID_AD5766 ,
@@ -455,6 +459,7 @@ static const struct iio_chan_spec_ext_info ad5766_ext_info[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type =BIT (IIO_CHAN_INFO_OFFSET ) | \ BIT(IIO_CHAN_INFO_SCALE), \ + .scan_index = (_chan), \ .scan_type = { \ .sign = 'u', \ .realbits = (_bits), \
@@ -576,6 +581,28 @@ static int ad5766_default_setup(struct ad5766_state *st)return __ad5766_spi_write (st,AD5766_CMD_SPAN_REG , st->crt_range); } +static irqreturn_t ad5766_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct iio_buffer *buf = indio_dev->buffer; +int ret, ch, i; +u16 data[AD5766_NUM_CH ]; + + ret =iio_pop_from_buffer (buf, (u8 *)data); +if (ret) + goto done; + + i =0 ; +for_each_set_bit (ch, indio_dev->active_scan_mask,AD5766_NUM_CH -1 ) +ad5766_write (indio_dev, ch,le16_to_cpu (data[i++])); + +done: +iio_trigger_notify_done (indio_dev->trig); + +return IRQ_HANDLED ; +} +static int ad5766_probe (struct spi_device *spi) {enum ad5766_type type;
@@ -609,6 +636,15 @@ static int ad5766_probe(struct spi_device *spi)if (ret)return ret; +/* Configure trigger buffer */ + ret =devm_iio_triggered_buffer_setup_ext (&spi->dev, indio_dev,NULL , + ad5766_trigger_handler, +IIO_BUFFER_DIRECTION_OUT , +NULL , +NULL ); +if (ret) +return ret; +return devm_iio_device_register (&spi->dev, indio_dev); }
Signature · 1 line
2.27.0
Re: [PATCH v5 6/6] drivers:iio:dac:ad5766.c: Add trigger buffer
Quoted 4 lines · Mihail Chindris
This chip is able to generate waveform and using an with the output trigger buffer will be easy to generate one.
This turned out to look quite neat.
Some minor notes inline.
Nothing major.
But other than that:
Quoted 40 lines
Signed-off-by Mihail Chindris <[email protected]>--- drivers/iio/dac/ad5766.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)drivers/iio/dac/ad5766.c +5
-0@@ -5,10 +5,13 @@* Copyright 2019-2020 Analog Devices Inc. */ #include <linux/bitfield.h> +#include <linux/bitops.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/gpio/consumer.h> #include <linux/iio/iio.h> +#include <linux/iio/triggered_buffer.h> +#include <linux/iio/trigger_consumer.h> #include <linux/module.h> #include <linux/spi/spi.h> #include <asm/unaligned.h> @@ -41,6 +44,7 @@#define AD5766_CMD_DITHER_SCALE_2 0xD0#define AD5766_FULL_RESET_CODE 0x1234 +#define AD5766_NUM_CH 16enum ad5766_type {ID_AD5766 ,@@ -455,6 +459,7 @@ static const struct iio_chan_spec_ext_info ad5766_ext_info[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type =BIT (IIO_CHAN_INFO_OFFSET ) | \ BIT(IIO_CHAN_INFO_SCALE), \ + .scan_index = (_chan), \ .scan_type = { \ .sign = 'u', \ .realbits = (_bits), \
drivers/iio/dac/ad5766.c static int ad5766_default_setup(struct ad5766_state *st) · line 588
Quoted 8 lines
drivers/iio/dac/ad5766.c +5
-0@@ -576,6 +581,28 @@ static int ad5766_default_setup(struct ad5766_state *st)return __ad5766_spi_write (st,AD5766_CMD_SPAN_REG , st->crt_range); } +static irqreturn_t ad5766_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct iio_buffer *buf = indio_dev->buffer;
Purely stylistic.
I would keep the variable name as "struct iio_buffer *bufffer". Reason is: when you start to grep the kernel-code, buf tends to refer to simple/small buffers (like buf[4], or buf[16]).
And when wanting to do some multiple-changes, grepping easier is useful.
drivers/iio/dac/ad5766.c static int ad5766_default_setup(struct ad5766_state *st) · line 592
Quoted 4 lines
drivers/iio/dac/ad5766.c +4
-0+int ret, ch, i; +u16 data[AD5766_NUM_CH ]; + + ret =iio_pop_from_buffer (buf, (u8 *)data);
Does the compiler complain if this (u8 *) cast is removed? Because it doesn't look like this is needed or that it would do anything as the argument of data is (void *).
Quoted 35 lines
drivers/iio/dac/ad5766.c +22
-1+if (ret) + goto done; + + i =0 ; +for_each_set_bit (ch, indio_dev->active_scan_mask,AD5766_NUM_CH -1 ) +ad5766_write (indio_dev, ch,le16_to_cpu (data[i++])); + +done: +iio_trigger_notify_done (indio_dev->trig); + +return IRQ_HANDLED ; +} +static int ad5766_probe (struct spi_device *spi) {enum ad5766_type type;@@ -609,6 +636,15 @@ static int ad5766_probe(struct spi_device *spi)if (ret)return ret; +/* Configure trigger buffer */ + ret =devm_iio_triggered_buffer_setup_ext (&spi->dev, indio_dev,NULL , + ad5766_trigger_handler, +IIO_BUFFER_DIRECTION_OUT , +NULL , +NULL ); +if (ret) +return ret; +return devm_iio_device_register (&spi->dev, indio_dev); } --2.27.0
Jonathan Cameron
maintainerRe: [PATCH v5 6/6] drivers:iio:dac:ad5766.c: Add trigger buffer
Quoted 8 lines · On Fri, 17 Sep 2021 11:08:24 +0300 Alexandru Ardelean <[email protected]>…
Quoted 4 lines · Mihail Chindris
This chip is able to generate waveform and using an with the output trigger buffer will be easy to generate one.
This turned out to look quite neat.
Indeed. Nice little example.
Quoted 32 lines
Some minor notes inline.
Nothing major.
But other than that:Reviewed-by Alexandru Ardelean <[email protected]>Quoted 26 lines
Signed-off-by Mihail Chindris <[email protected]>--- drivers/iio/dac/ad5766.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)drivers/iio/dac/ad5766.c +3
-0@@ -5,10 +5,13 @@* Copyright 2019-2020 Analog Devices Inc. */ #include <linux/bitfield.h> +#include <linux/bitops.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/gpio/consumer.h> #include <linux/iio/iio.h> +#include <linux/iio/triggered_buffer.h> +#include <linux/iio/trigger_consumer.h> #include <linux/module.h> #include <linux/spi/spi.h> #include <asm/unaligned.h> @@ -41,6 +44,7 @@#define AD5766_CMD_DITHER_SCALE_2 0xD0
Quoted 2 lines
drivers/iio/dac/ad5766.c line 47
Quoted 2 lines
drivers/iio/dac/ad5766.c +1
-0#define AD5766_FULL_RESET_CODE 0x1234 +#define AD5766_NUM_CH 16
Can we derive this from something already present in the driver, or perhaps enforce it having the right value in some fashion?
I'm thinking it should match ARRAY_SIZE(ad5766_channels) for example.
Quoted 15 lines
Quoted 15 lines
drivers/iio/dac/ad5766.c +1
-0enum ad5766_type {ID_AD5766 ,@@ -455,6 +459,7 @@ static const struct iio_chan_spec_ext_info ad5766_ext_info[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type =BIT (IIO_CHAN_INFO_OFFSET ) | \ BIT(IIO_CHAN_INFO_SCALE), \ + .scan_index = (_chan), \ .scan_type = { \ .sign = 'u', \ .realbits = (_bits), \@@ -576,6 +581,28 @@ static int ad5766_default_setup(struct ad5766_state *st)return __ad5766_spi_write (st,AD5766_CMD_SPAN_REG , st->crt_range); }
Quoted 8 lines
drivers/iio/dac/ad5766.c static int ad5766_default_setup(struct ad5766_state *st) · line 588
Quoted 5 lines
drivers/iio/dac/ad5766.c +5
-0+static irqreturn_t ad5766_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct iio_buffer *buf = indio_dev->buffer;Purely stylistic.
I would keep the variable name as "structiio_buffer*bufffer".
buffer. I guess 3 fs would make it very grep-able though :)
Quoted 4 lines
Reason is: when you start to grep the kernel-code,
buftends to refer to simple/small buffers (like buf[4], or buf[16]).
And when wanting to do some multiple-changes, grepping easier is useful.
Quoted 15 lines
drivers/iio/dac/ad5766.c static int ad5766_default_setup(struct ad5766_state *st) · line 592
Quoted 4 lines
drivers/iio/dac/ad5766.c +4
-0+int ret, ch, i; +u16 data[AD5766_NUM_CH ]; + + ret =iio_pop_from_buffer (buf, (u8 *)data);Does the compiler complain if this (u8 *) cast is removed? Because it doesn't look like this is needed or that it would do anything as the argument of data is (void *).
Quoted 6 lines
drivers/iio/dac/ad5766.c +6
-0+if (ret) + goto done; + + i =0 ; +for_each_set_bit (ch, indio_dev->active_scan_mask,AD5766_NUM_CH -1 ) +ad5766_write (indio_dev, ch,le16_to_cpu (data[i++]));
Looks like the device supports a mode where you write to input registers for all
channels and then trigger a simultaneous update. That feels like it would be
the mode most suitable to use for buffered mode as would make the whole 'scan' become active as close as possible to instantaneously.
Quoted 29 lines
Quoted 29 lines
drivers/iio/dac/ad5766.c +16
-1+ +done: +iio_trigger_notify_done (indio_dev->trig); + +return IRQ_HANDLED ; +} +static int ad5766_probe (struct spi_device *spi) {enum ad5766_type type;@@ -609,6 +636,15 @@ static int ad5766_probe(struct spi_device *spi)if (ret)return ret; +/* Configure trigger buffer */ + ret =devm_iio_triggered_buffer_setup_ext (&spi->dev, indio_dev,NULL , + ad5766_trigger_handler, +IIO_BUFFER_DIRECTION_OUT , +NULL , +NULL ); +if (ret) +return ret; +return devm_iio_device_register (&spi->dev, indio_dev); } --2.27.0