2021-09-17 06:07:59

by Chindris, Mihail

[permalink] [raw]
Subject: [PATCH v5 0/6] 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
--
2.27.0


2021-09-17 06:08:45

by Chindris, Mihail

[permalink] [raw]
Subject: [PATCH v5 2/6] iio: kfifo-buffer: Add output buffer support

From: 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.

Signed-off-by: Lars-Peter Clausen <[email protected]>
Signed-off-by: Alexandru Ardelean <[email protected]>
Signed-off-by: Mihail Chindris <[email protected]>
---
drivers/iio/buffer/kfifo_buf.c | 50 ++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)

diff --git a/drivers/iio/buffer/kfifo_buf.c b/drivers/iio/buffer/kfifo_buf.c
index 516eb3465de1..7368db2d5c32 100644
--- a/drivers/iio/buffer/kfifo_buf.c
+++ b/drivers/iio/buffer/kfifo_buf.c
@@ -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,
--
2.27.0

2021-09-17 06:10:52

by Chindris, Mihail

[permalink] [raw]
Subject: [PATCH v5 5/6] Documentation:devicetree:bindings:iio:dac: Fix val

A correct value for output-range-microvolts is -5 to 5 Volts
not -5 to 5 milivolts

Signed-off-by: Mihail Chindris <[email protected]>
---
Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml b/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
index d5c54813ce87..a8f7720d1e3e 100644
--- a/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
+++ b/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
@@ -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

2021-09-17 06:11:36

by Chindris, Mihail

[permalink] [raw]
Subject: [PATCH v5 6/6] drivers:iio:dac:ad5766.c: Add trigger buffer

This chip is able to generate waveform and using an
with the output trigger buffer will be easy to generate one.

Signed-off-by: Mihail Chindris <[email protected]>
---
drivers/iio/dac/ad5766.c | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)

diff --git a/drivers/iio/dac/ad5766.c b/drivers/iio/dac/ad5766.c
index dafda84fdea3..71491e6d466e 100644
--- a/drivers/iio/dac/ad5766.c
+++ b/drivers/iio/dac/ad5766.c
@@ -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 16

enum 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);
}

--
2.27.0

2021-09-17 07:40:59

by Chindris, Mihail

[permalink] [raw]
Subject: [PATCH v5 3/6] iio: triggered-buffer: extend support to configure output buffers

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.

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]>
---
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(-)

diff --git a/drivers/iio/accel/adxl372.c b/drivers/iio/accel/adxl372.c
index fc9592407717..758952584f8c 100644
--- a/drivers/iio/accel/adxl372.c
+++ b/drivers/iio/accel/adxl372.c
@@ -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)
diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c
index e8693a42ad46..63216321cdb5 100644
--- a/drivers/iio/accel/bmc150-accel-core.c
+++ b/drivers/iio/accel/bmc150-accel-core.c
@@ -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) {
diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
index ea5ca163d879..7093611e321e 100644
--- a/drivers/iio/adc/at91-sama5d2_adc.c
+++ b/drivers/iio/adc/at91-sama5d2_adc.c
@@ -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;
diff --git a/drivers/iio/buffer/industrialio-triggered-buffer.c b/drivers/iio/buffer/industrialio-triggered-buffer.c
index f77c4538141e..8d4fc97d1005 100644
--- a/drivers/iio/buffer/industrialio-triggered-buffer.c
+++ b/drivers/iio/buffer/industrialio-triggered-buffer.c
@@ -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;

diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
index 28bde13003b7..e9f64da06f89 100644
--- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
@@ -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;
}
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
index a4ec11a3b68a..1151434038d4 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
@@ -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;
diff --git a/include/linux/iio/triggered_buffer.h b/include/linux/iio/triggered_buffer.h
index 7f154d1f8739..7490b05fc5b2 100644
--- a/include/linux/iio/triggered_buffer.h
+++ b/include/linux/iio/triggered_buffer.h
@@ -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
--
2.27.0

2021-09-17 07:41:54

by Chindris, Mihail

[permalink] [raw]
Subject: [PATCH v5 4/6] drivers: iio: dac: ad5766: Fix dt property name

In the documentation the name for the property is
output-range-microvolts which is a standard name, therefore this name
must be used.

Signed-off-by: Mihail Chindris <[email protected]>
---
drivers/iio/dac/ad5766.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/dac/ad5766.c b/drivers/iio/dac/ad5766.c
index 3104ec32dfac..dafda84fdea3 100644
--- a/drivers/iio/dac/ad5766.c
+++ b/drivers/iio/dac/ad5766.c
@@ -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

2021-09-17 08:08:44

by Chindris, Mihail

[permalink] [raw]
Subject: [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.

Signed-off-by: Lars-Peter Clausen <[email protected]>
Signed-off-by: Alexandru Ardelean <[email protected]>
Signed-off-by: Mihail Chindris <[email protected]>
---
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(-)

diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
index 8f4a9b264962..61e318431de9 100644
--- a/drivers/iio/iio_core.h
+++ b/drivers/iio/iio_core.h
@@ -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 NULL

static inline int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
{
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index a95cc2da56be..a2a34c5652a7 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -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);
@@ -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;
+
mutex_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,
@@ -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
+ * 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,
};
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 2dbb37e09b8c..537a08549a69 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -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,
diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h
index b6928ac5c63d..fe2e680d9b5e 100644
--- a/include/linux/iio/buffer.h
+++ b/include/linux/iio/buffer.h
@@ -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.
diff --git a/include/linux/iio/buffer_impl.h b/include/linux/iio/buffer_impl.h
index 245b32918ae1..e2ca8ea23e19 100644
--- a/include/linux/iio/buffer_impl.h
+++ b/include/linux/iio/buffer_impl.h
@@ -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.
--
2.27.0

2021-09-17 13:06:03

by Alexandru Ardelean

[permalink] [raw]
Subject: Re: [PATCH v5 4/6] drivers: iio: dac: ad5766: Fix dt property name

On Fri, Sep 17, 2021 at 9:11 AM Mihail Chindris
<[email protected]> wrote:
>
> 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:

Reviewed-by: Alexandru Ardelean <[email protected]>

> Signed-off-by: Mihail Chindris <[email protected]>
> ---
> drivers/iio/dac/ad5766.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/iio/dac/ad5766.c b/drivers/iio/dac/ad5766.c
> index 3104ec32dfac..dafda84fdea3 100644
> --- a/drivers/iio/dac/ad5766.c
> +++ b/drivers/iio/dac/ad5766.c
> @@ -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
>

2021-09-17 13:11:14

by Alexandru Ardelean

[permalink] [raw]
Subject: Re: [PATCH v5 6/6] drivers:iio:dac:ad5766.c: Add trigger buffer

On Fri, Sep 17, 2021 at 9:11 AM Mihail Chindris
<[email protected]> wrote:
>
> 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:

Reviewed-by: Alexandru Ardelean <[email protected]>

> Signed-off-by: Mihail Chindris <[email protected]>
> ---
> drivers/iio/dac/ad5766.c | 36 ++++++++++++++++++++++++++++++++++++
> 1 file changed, 36 insertions(+)
>
> diff --git a/drivers/iio/dac/ad5766.c b/drivers/iio/dac/ad5766.c
> index dafda84fdea3..71491e6d466e 100644
> --- a/drivers/iio/dac/ad5766.c
> +++ b/drivers/iio/dac/ad5766.c
> @@ -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 16
>
> enum 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;

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.

> + 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 *).

> + 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
>

2021-09-17 17:34:19

by Alexandru Ardelean

[permalink] [raw]
Subject: Re: [PATCH v5 5/6] Documentation:devicetree:bindings:iio:dac: Fix val

On Fri, Sep 17, 2021 at 9:08 AM Mihail Chindris
<[email protected]> wrote:
>
> A correct value for output-range-microvolts is -5 to 5 Volts
> not -5 to 5 milivolts
>

Reviewed-by: Alexandru Ardelean <[email protected]>

> Signed-off-by: Mihail Chindris <[email protected]>
> ---
> Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml b/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
> index d5c54813ce87..a8f7720d1e3e 100644
> --- a/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
> +++ b/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
> @@ -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
>

2021-09-19 20:44:20

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 4/6] drivers: iio: dac: ad5766: Fix dt property name

On Fri, 17 Sep 2021 10:53:54 +0300
Alexandru Ardelean <[email protected]> wrote:

> On Fri, Sep 17, 2021 at 9:11 AM Mihail Chindris
> <[email protected]> wrote:
> >
> > 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:
>
> 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


>
> > Signed-off-by: Mihail Chindris <[email protected]>
> > ---
> > drivers/iio/dac/ad5766.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/iio/dac/ad5766.c b/drivers/iio/dac/ad5766.c
> > index 3104ec32dfac..dafda84fdea3 100644
> > --- a/drivers/iio/dac/ad5766.c
> > +++ b/drivers/iio/dac/ad5766.c
> > @@ -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
> >

2021-09-19 20:44:54

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 5/6] Documentation:devicetree:bindings:iio:dac: Fix val

On Fri, 17 Sep 2021 10:53:02 +0300
Alexandru Ardelean <[email protected]> wrote:

> On Fri, Sep 17, 2021 at 9:08 AM Mihail Chindris
> <[email protected]> wrote:
> >
> > 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


>
> > Signed-off-by: Mihail Chindris <[email protected]>
> > ---
> > Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml b/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
> > index d5c54813ce87..a8f7720d1e3e 100644
> > --- a/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
> > +++ b/Documentation/devicetree/bindings/iio/dac/adi,ad5766.yaml
> > @@ -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
> >

2021-09-19 22:08:29

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 1/6] iio: Add output buffer support

On Thu, 16 Sep 2021 18:29:09 +0000
Mihail Chindris <[email protected]> wrote:

> 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.
>
> 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

> ---
> 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(-)
>
> diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
> index 8f4a9b264962..61e318431de9 100644
> --- a/drivers/iio/iio_core.h
> +++ b/drivers/iio/iio_core.h
> @@ -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 NULL
>
> static inline int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
> {
> diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
> index a95cc2da56be..a2a34c5652a7 100644
> --- a/drivers/iio/industrialio-buffer.c
> +++ b/drivers/iio/industrialio-buffer.c
> @@ -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);
> @@ -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.

> mutex_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,
> @@ -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

> + * 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,
> };
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 2dbb37e09b8c..537a08549a69 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -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,
> diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h
> index b6928ac5c63d..fe2e680d9b5e 100644
> --- a/include/linux/iio/buffer.h
> +++ b/include/linux/iio/buffer.h
> @@ -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.
> diff --git a/include/linux/iio/buffer_impl.h b/include/linux/iio/buffer_impl.h
> index 245b32918ae1..e2ca8ea23e19 100644
> --- a/include/linux/iio/buffer_impl.h
> +++ b/include/linux/iio/buffer_impl.h
> @@ -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.

2021-09-19 22:08:29

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 3/6] iio: triggered-buffer: extend support to configure output buffers

On Thu, 16 Sep 2021 18:29:11 +0000
Mihail Chindris <[email protected]> wrote:

> 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.
>
> 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

> ---
> 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(-)
>
> diff --git a/drivers/iio/accel/adxl372.c b/drivers/iio/accel/adxl372.c
> index fc9592407717..758952584f8c 100644
> --- a/drivers/iio/accel/adxl372.c
> +++ b/drivers/iio/accel/adxl372.c
> @@ -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)
> diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c
> index e8693a42ad46..63216321cdb5 100644
> --- a/drivers/iio/accel/bmc150-accel-core.c
> +++ b/drivers/iio/accel/bmc150-accel-core.c
> @@ -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) {
> diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
> index ea5ca163d879..7093611e321e 100644
> --- a/drivers/iio/adc/at91-sama5d2_adc.c
> +++ b/drivers/iio/adc/at91-sama5d2_adc.c
> @@ -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;
> diff --git a/drivers/iio/buffer/industrialio-triggered-buffer.c b/drivers/iio/buffer/industrialio-triggered-buffer.c
> index f77c4538141e..8d4fc97d1005 100644
> --- a/drivers/iio/buffer/industrialio-triggered-buffer.c
> +++ b/drivers/iio/buffer/industrialio-triggered-buffer.c
> @@ -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;
>
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> index 28bde13003b7..e9f64da06f89 100644
> --- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> @@ -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;
> }
> diff --git a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
> index a4ec11a3b68a..1151434038d4 100644
> --- a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
> +++ b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
> @@ -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;
> diff --git a/include/linux/iio/triggered_buffer.h b/include/linux/iio/triggered_buffer.h
> index 7f154d1f8739..7490b05fc5b2 100644
> --- a/include/linux/iio/triggered_buffer.h
> +++ b/include/linux/iio/triggered_buffer.h
> @@ -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

2021-09-19 22:11:31

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 6/6] drivers:iio:dac:ad5766.c: Add trigger buffer

On Fri, 17 Sep 2021 11:08:24 +0300
Alexandru Ardelean <[email protected]> wrote:

> On Fri, Sep 17, 2021 at 9:11 AM Mihail Chindris
> <[email protected]> wrote:
> >
> > 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.

> Some minor notes inline.
> Nothing major.
> But other than that:
>
> Reviewed-by: Alexandru Ardelean <[email protected]>
>
> > Signed-off-by: Mihail Chindris <[email protected]>
> > ---
> > drivers/iio/dac/ad5766.c | 36 ++++++++++++++++++++++++++++++++++++
> > 1 file changed, 36 insertions(+)
> >
> > diff --git a/drivers/iio/dac/ad5766.c b/drivers/iio/dac/ad5766.c
> > index dafda84fdea3..71491e6d466e 100644
> > --- a/drivers/iio/dac/ad5766.c
> > +++ b/drivers/iio/dac/ad5766.c
> > @@ -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 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.

> >
> > enum 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;
>
> Purely stylistic.
> I would keep the variable name as "struct iio_buffer *bufffer".

buffer. I guess 3 fs would make it very grep-able though :)

> 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.
>
> > + 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 *).
>
> > + 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.

> > +
> > +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
> >

2021-09-20 09:53:48

by Nuno Sa

[permalink] [raw]
Subject: RE: [PATCH v5 1/6] iio: Add output buffer support

> 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 support
>
> On Thu, 16 Sep 2021 18:29:09 +0000
> Mihail Chindris <[email protected]> wrote:
>
> > 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.
> >
> > 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
>
> > ---
> > 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(-)
> >
> > diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
> > index 8f4a9b264962..61e318431de9 100644
> > --- a/drivers/iio/iio_core.h
> > +++ b/drivers/iio/iio_core.h
> > @@ -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 NULL
> >
> > static inline int iio_buffers_alloc_sysfs_and_mask(struct iio_dev
> *indio_dev)
> > {
> > diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-
> buffer.c
> > index a95cc2da56be..a2a34c5652a7 100644
> > --- a/drivers/iio/industrialio-buffer.c
> > +++ b/drivers/iio/industrialio-buffer.c
> > @@ -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;
> > +

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?

2021-09-21 02:52:58

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 1/6] iio: Add output buffer support

On Mon, 20 Sep 2021 08:02:29 +0000
"Sa, Nuno" <[email protected]> wrote:

> > 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 support
> >
> > On Thu, 16 Sep 2021 18:29:09 +0000
> > Mihail Chindris <[email protected]> wrote:
> >
> > > 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.
> > >
> > > 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
> >
> > > ---
> > > 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(-)
> > >
> > > diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
> > > index 8f4a9b264962..61e318431de9 100644
> > > --- a/drivers/iio/iio_core.h
> > > +++ b/drivers/iio/iio_core.h
> > > @@ -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 NULL
> > >
> > > static inline int iio_buffers_alloc_sysfs_and_mask(struct iio_dev
> > *indio_dev)
> > > {
> > > diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-
> > buffer.c
> > > index a95cc2da56be..a2a34c5652a7 100644
> > > --- a/drivers/iio/industrialio-buffer.c
> > > +++ b/drivers/iio/industrialio-buffer.c
> > > @@ -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;
> > > +
>
> 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;

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

>
> If going with this, we should add an extra patch to do a similar thing on the read side...
>
> - Nuno Sá
>