2015-12-08 03:20:23

by Peter Rosin

[permalink] [raw]
Subject: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

From: Peter Rosin <[email protected]>

Hi!

I have a signal connected to a gpio pin which is the output of
a comparator. By changing the level of one of the inputs to the
comparator, I can detect the envelope of the other input to
the comparator by using a series of measurements much in the
same maner a manual ADC works, but watching for changes on the
comparator over a period of time instead of only the immediate
output.

Now, the input signal to the comparator might have a high frequency,
which will cause the output from the comparator (and thus the GPIO
input) to change rapidly.

A common(?) idiom for this is to use the interrupt status register
to catch the glitches, but then not have any interrupt tied to
the pin as that could possibly generate pointless bursts of
(expensive) interrupts.

So, these two patches expose an interface to the PIO_ISR register
of the pio controllers on the platform I'm targetting. The first
patch adds some infrastructure to the gpio core and the second
patch hooks up "my" pin controller.

But hey, this seems like an old problem and I was surprised that
I had to touch the source to do it. Which makes me wonder what I'm
missing and what others needing to see short pulses on a pin but not
needing/wanting interrupts are doing?

Yes, there needs to be a way to select the interrupt edge w/o
actually arming the interrupt, that is missing. And probably
other things too, but I didn't want to do more work in case this
is a dead end for some reason...

Cheers,
Peter

Peter Rosin (2):
gpio: Add isr property of gpio pins
pinctrl: at91: expose the isr bit

Documentation/gpio/sysfs.txt | 12 ++++++++++
drivers/gpio/gpiolib-sysfs.c | 30 ++++++++++++++++++++++++
drivers/gpio/gpiolib.c | 15 ++++++++++++
drivers/pinctrl/pinctrl-at91.c | 50 ++++++++++++++++++++++++++++++++++++----
include/linux/gpio/consumer.h | 1 +
include/linux/gpio/driver.h | 2 ++
6 files changed, 106 insertions(+), 4 deletions(-)

--
1.7.10.4


2015-12-08 03:20:28

by Peter Rosin

[permalink] [raw]
Subject: [RESEND RFC PATCH 1/2] gpio: Add isr property of gpio pins

From: Peter Rosin <[email protected]>

Adds the possibility to read the interrupt status register bit for the
gpio pin. Expose the bit as an isr file in sysfs.

Signed-off-by: Peter Rosin <[email protected]>
---
Documentation/gpio/sysfs.txt | 12 ++++++++++++
drivers/gpio/gpiolib-sysfs.c | 30 ++++++++++++++++++++++++++++++
drivers/gpio/gpiolib.c | 15 +++++++++++++++
include/linux/gpio/consumer.h | 1 +
include/linux/gpio/driver.h | 2 ++
5 files changed, 60 insertions(+)

diff --git a/Documentation/gpio/sysfs.txt b/Documentation/gpio/sysfs.txt
index 535b6a8a7a7c..ded7ef9d01be 100644
--- a/Documentation/gpio/sysfs.txt
+++ b/Documentation/gpio/sysfs.txt
@@ -97,6 +97,18 @@ and have the following read/write attributes:
for "rising" and "falling" edges will follow this
setting.

+ "isr" ... reads as either 0 (false) or 1 (true). Reading the
+ file will clear the value, so that reading a 1 means
+ that there has been an interrupt-triggering action
+ on the pin since the file was last read.
+
+ This file exists only if the gpio chip supports reading
+ the interrupt status register bit for the pin.
+
+ Note that if reading the isr register for this pin
+ interferes with active interrupts, the read will fail
+ with an error.
+
GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
controller implementing GPIOs starting at #42) and have the following
read-only attributes:
diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
index b57ed8e55ab5..f6fe68fab191 100644
--- a/drivers/gpio/gpiolib-sysfs.c
+++ b/drivers/gpio/gpiolib-sysfs.c
@@ -139,6 +139,28 @@ static ssize_t value_store(struct device *dev,
}
static DEVICE_ATTR_RW(value);

+static ssize_t isr_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct gpiod_data *data = dev_get_drvdata(dev);
+ struct gpio_desc *desc = data->desc;
+ ssize_t status;
+ int isr;
+
+ mutex_lock(&data->mutex);
+
+ isr = gpiod_get_isr_cansleep(desc);
+ if (isr < 0)
+ status = isr;
+ else
+ status = sprintf(buf, "%d\n", isr);
+
+ mutex_unlock(&data->mutex);
+
+ return status;
+}
+static DEVICE_ATTR_RO(isr);
+
static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
{
struct gpiod_data *data = priv;
@@ -367,6 +389,13 @@ static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
mode = 0;
if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
mode = 0;
+ } else if (attr == &dev_attr_isr.attr) {
+ if (!desc->chip->get_isr)
+ mode = 0;
+ if (gpiod_to_irq(desc) < 0)
+ mode = 0;
+ if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
+ mode = 0;
}

return mode;
@@ -377,6 +406,7 @@ static struct attribute *gpio_attrs[] = {
&dev_attr_edge.attr,
&dev_attr_value.attr,
&dev_attr_active_low.attr,
+ &dev_attr_isr.attr,
NULL,
};

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index bf4bd1d120c3..b45e70b2713e 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1572,6 +1572,21 @@ int gpiod_get_value_cansleep(const struct gpio_desc *desc)
}
EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);

+int gpiod_get_isr_cansleep(const struct gpio_desc *desc)
+{
+ struct gpio_chip *chip;
+ int offset;
+
+ might_sleep_if(extra_checks);
+ if (!desc)
+ return -EINVAL;
+
+ chip = desc->chip;
+ offset = gpio_chip_hwgpio(desc);
+ return chip->get_isr ? chip->get_isr(chip, offset) : -ENXIO;
+}
+EXPORT_SYMBOL_GPL(gpiod_get_isr_cansleep);
+
/**
* gpiod_set_raw_value_cansleep() - assign a gpio's raw value
* @desc: gpio whose value will be assigned
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
index adac255aee86..d0290c14dc84 100644
--- a/include/linux/gpio/consumer.h
+++ b/include/linux/gpio/consumer.h
@@ -119,6 +119,7 @@ void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
struct gpio_desc **desc_array,
int *value_array);
+int gpiod_get_isr_cansleep(const struct gpio_desc *desc);

int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);

diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index c8393cd4d44f..dccfb12f9112 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -96,6 +96,8 @@ struct gpio_chip {
unsigned offset);
void (*set)(struct gpio_chip *chip,
unsigned offset, int value);
+ int (*get_isr)(struct gpio_chip *chip,
+ unsigned offset);
void (*set_multiple)(struct gpio_chip *chip,
unsigned long *mask,
unsigned long *bits);
--
1.7.10.4

2015-12-08 03:20:33

by Peter Rosin

[permalink] [raw]
Subject: [RESEND RFC PATCH 2/2] pinctrl: at91: expose the isr bit

From: Peter Rosin <[email protected]>

This is a bit horrible, as reading the isr register will interfere with
interrupts on other pins in the same pio. So, be careful...

Signed-off-by: Peter Rosin <[email protected]>
---
drivers/pinctrl/pinctrl-at91.c | 50 ++++++++++++++++++++++++++++++++++++----
1 file changed, 46 insertions(+), 4 deletions(-)

diff --git a/drivers/pinctrl/pinctrl-at91.c b/drivers/pinctrl/pinctrl-at91.c
index 2deb1309fcac..6ae615264e6a 100644
--- a/drivers/pinctrl/pinctrl-at91.c
+++ b/drivers/pinctrl/pinctrl-at91.c
@@ -16,6 +16,7 @@
#include <linux/of_irq.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
+#include <linux/spinlock.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/pinctrl/machine.h>
@@ -40,6 +41,8 @@ struct at91_gpio_chip {
int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
int pioc_virq; /* PIO bank Linux virtual interrupt */
int pioc_idx; /* PIO bank index */
+ spinlock_t isr_lock; /* PIO_ISR cache lock */
+ unsigned isr_cache; /* PIO_ISR cache */
void __iomem *regbase; /* PIO bank virtual address */
struct clk *clock; /* associated clock */
struct at91_pinctrl_mux_ops *ops; /* ops */
@@ -737,7 +740,9 @@ static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
continue;

mask = pin_to_mask(pin->pin);
+ spin_lock(&gpio_chips[pin->bank]->isr_lock);
at91_mux_disable_interrupt(pio, mask);
+ spin_unlock(&gpio_chips[pin->bank]->isr_lock);
switch (pin->mux) {
case AT91_MUX_GPIO:
at91_mux_gpio_enable(pio, mask, 1);
@@ -1331,6 +1336,29 @@ static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
return (pdsr & mask) != 0;
}

+static int at91_gpio_get_isr(struct gpio_chip *chip, unsigned offset)
+{
+ struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
+ void __iomem *pio = at91_gpio->regbase;
+ unsigned mask = 1 << offset;
+ int res;
+
+ spin_lock(&at91_gpio->isr_lock);
+ if (readl_relaxed(pio + PIO_IMR)) {
+ /* do not clobber PIO_ISR if any interrupts are enabled */
+ res = -EBUSY;
+ goto out;
+ }
+
+ at91_gpio->isr_cache |= readl_relaxed(pio + PIO_ISR);
+ res = (at91_gpio->isr_cache & mask) != 0;
+ at91_gpio->isr_cache &= ~mask;
+
+ out:
+ spin_unlock(&at91_gpio->isr_lock);
+ return res;
+}
+
static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
int val)
{
@@ -1425,8 +1453,12 @@ static void gpio_irq_mask(struct irq_data *d)
void __iomem *pio = at91_gpio->regbase;
unsigned mask = 1 << d->hwirq;

- if (pio)
- writel_relaxed(mask, pio + PIO_IDR);
+ if (!pio)
+ return;
+
+ spin_lock(&at91_gpio->isr_lock);
+ writel_relaxed(mask, pio + PIO_IDR);
+ spin_unlock(&at91_gpio->isr_lock);
}

static void gpio_irq_unmask(struct irq_data *d)
@@ -1435,8 +1467,12 @@ static void gpio_irq_unmask(struct irq_data *d)
void __iomem *pio = at91_gpio->regbase;
unsigned mask = 1 << d->hwirq;

- if (pio)
- writel_relaxed(mask, pio + PIO_IER);
+ if (!pio)
+ return;
+
+ spin_lock(&at91_gpio->isr_lock);
+ writel_relaxed(mask, pio + PIO_IER);
+ spin_unlock(&at91_gpio->isr_lock);
}

static int gpio_irq_type(struct irq_data *d, unsigned type)
@@ -1562,8 +1598,10 @@ void at91_pinctrl_gpio_suspend(void)
pio = gpio_chips[i]->regbase;

backups[i] = readl_relaxed(pio + PIO_IMR);
+ spin_lock(&gpio_chips[i]->isr_lock);
writel_relaxed(backups[i], pio + PIO_IDR);
writel_relaxed(wakeups[i], pio + PIO_IER);
+ spin_unlock(&gpio_chips[i]->isr_lock);

if (!wakeups[i])
clk_disable_unprepare(gpio_chips[i]->clock);
@@ -1588,8 +1626,10 @@ void at91_pinctrl_gpio_resume(void)
if (!wakeups[i])
clk_prepare_enable(gpio_chips[i]->clock);

+ spin_lock(&gpio_chips[i]->isr_lock);
writel_relaxed(wakeups[i], pio + PIO_IDR);
writel_relaxed(backups[i], pio + PIO_IER);
+ spin_unlock(&gpio_chips[i]->isr_lock);
}
}

@@ -1713,6 +1753,7 @@ static struct gpio_chip at91_gpio_template = {
.get_direction = at91_gpio_get_direction,
.direction_input = at91_gpio_direction_input,
.get = at91_gpio_get,
+ .get_isr = at91_gpio_get_isr,
.direction_output = at91_gpio_direction_output,
.set = at91_gpio_set,
.set_multiple = at91_gpio_set_multiple,
@@ -1789,6 +1830,7 @@ static int at91_gpio_probe(struct platform_device *pdev)
}

at91_chip->chip = at91_gpio_template;
+ spin_lock_init(&at91_chip->isr_lock);

chip = &at91_chip->chip;
chip->of_node = np;
--
1.7.10.4

2015-12-09 08:02:20

by Ludovic Desroches

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

Hi Peter,

On Tue, Dec 08, 2015 at 04:20:06AM +0100, Peter Rosin wrote:
> From: Peter Rosin <[email protected]>
>
> Hi!
>
> I have a signal connected to a gpio pin which is the output of
> a comparator. By changing the level of one of the inputs to the
> comparator, I can detect the envelope of the other input to
> the comparator by using a series of measurements much in the
> same maner a manual ADC works, but watching for changes on the
> comparator over a period of time instead of only the immediate
> output.
>
> Now, the input signal to the comparator might have a high frequency,
> which will cause the output from the comparator (and thus the GPIO
> input) to change rapidly.
>
> A common(?) idiom for this is to use the interrupt status register
> to catch the glitches, but then not have any interrupt tied to
> the pin as that could possibly generate pointless bursts of
> (expensive) interrupts.
>

Well I don't know if this use case as already been considered. I
understand you don't want to be overwhelmed by interrupts but why not
using the interrupt to start polling the PDSR (Pin Data Status
Register)?

I am really not confortable about exposing the ISR since there is a
clean on read. You have taken precautions by checking the IMR before but
if there is a single driver using a gpio as an irq, you will never get
the ISR.

Regards

Ludovic

> So, these two patches expose an interface to the PIO_ISR register
> of the pio controllers on the platform I'm targetting. The first
> patch adds some infrastructure to the gpio core and the second
> patch hooks up "my" pin controller.
>
> But hey, this seems like an old problem and I was surprised that
> I had to touch the source to do it. Which makes me wonder what I'm
> missing and what others needing to see short pulses on a pin but not
> needing/wanting interrupts are doing?
>
> Yes, there needs to be a way to select the interrupt edge w/o
> actually arming the interrupt, that is missing. And probably
> other things too, but I didn't want to do more work in case this
> is a dead end for some reason...
>
> Cheers,
> Peter
>
> Peter Rosin (2):
> gpio: Add isr property of gpio pins
> pinctrl: at91: expose the isr bit
>
> Documentation/gpio/sysfs.txt | 12 ++++++++++
> drivers/gpio/gpiolib-sysfs.c | 30 ++++++++++++++++++++++++
> drivers/gpio/gpiolib.c | 15 ++++++++++++
> drivers/pinctrl/pinctrl-at91.c | 50 ++++++++++++++++++++++++++++++++++++----
> include/linux/gpio/consumer.h | 1 +
> include/linux/gpio/driver.h | 2 ++
> 6 files changed, 106 insertions(+), 4 deletions(-)
>
> --
> 1.7.10.4
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html

2015-12-09 08:56:31

by Peter Rosin

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

Hi!

On 2015-12-09 09:01, Ludovic Desroches wrote:
> Hi Peter,
>
> On Tue, Dec 08, 2015 at 04:20:06AM +0100, Peter Rosin wrote:
>> From: Peter Rosin <[email protected]>
>>
>> Hi!
>>
>> I have a signal connected to a gpio pin which is the output of
>> a comparator. By changing the level of one of the inputs to the
>> comparator, I can detect the envelope of the other input to
>> the comparator by using a series of measurements much in the
>> same maner a manual ADC works, but watching for changes on the
>> comparator over a period of time instead of only the immediate
>> output.
>>
>> Now, the input signal to the comparator might have a high frequency,
>> which will cause the output from the comparator (and thus the GPIO
>> input) to change rapidly.
>>
>> A common(?) idiom for this is to use the interrupt status register
>> to catch the glitches, but then not have any interrupt tied to
>> the pin as that could possibly generate pointless bursts of
>> (expensive) interrupts.
>>
>
> Well I don't know if this use case as already been considered. I
> understand you don't want to be overwhelmed by interrupts but why not
> using the interrupt to start polling the PDSR (Pin Data Status
> Register)?

That scheme will not work for me. There might be only one short
glitch, and there might be a flood. I need to catch both. What could
be made to work is some kind of one-off interrupt thingy. I.e. an
interrupt that disabled itself when hit (if that is possibly without
lockup?). That could be a small generic driver not specific to gpio,
I suppose, but where should such a beast live and what user space
interface should it have?

And while that is generic and will probably work in more cases, it
seems complicated and quite a bit of a detour compared to simply
reading the same info from a register.

Are there really noone else using ISR type registers like this with
Linux? In my mind that was pretty standard practice...

> I am really not comfortable about exposing the ISR since there is a
> clean on read. You have taken precautions by checking the IMR before but
> if there is a single driver using a gpio as an irq, you will never get
> the ISR.

Yes, I'm aware of the limitation, but in my case that's not a problem,
obviously. I have no (other) interrupt sources on the gpios covered by
the ISR register in question.

I take it that your major concern is the non-generality, i.e. that it
is not possible to safely get at the ISR when there are interrupts
enabled, and not the complication/overhead of the new lock?

Cheers,
Peter

2015-12-11 12:43:34

by Linus Walleij

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 1/2] gpio: Add isr property of gpio pins

On Tue, Dec 8, 2015 at 4:20 AM, Peter Rosin <[email protected]> wrote:

> From: Peter Rosin <[email protected]>
>
> Adds the possibility to read the interrupt status register bit for the
> gpio pin. Expose the bit as an isr file in sysfs.
>
> Signed-off-by: Peter Rosin <[email protected]>

NACK. We have frozen the sysfs ABI and we are working on a
character device to replace it for userspace access, see:
http://marc.info/?l=linux-gpio&m=144550276512673&w=2

Second question is *WHY* you want this crazy thing? Userspace
should want *events* from a file descriptor, like IIO does it for
example (on top of a proper chardev), not polling a register to
figure out if an IRQ occurred.

We need to think about the real solution to what you want to do.

Yours,
Linus Walleij

2015-12-11 12:53:09

by Linus Walleij

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

Quoting extensively since I'm involving the linux-iio mailinglist.

The use case you describe is hand-in-glove with Industrial I/O.
I think you want a trigger interface from IIO and read events from
userspace using the IIO character device.

Look at the userspace examples in tools/iio for how it's used
in userspace, the subsystem is in drivers/iio. I suspect
drivers/iio/adc/polled-gpio.c or something is where you actually
want to go with this. The module should do all the fastpath
work and then expose what you actually want to know to
userspace using the IIO triggers or events.

I have used IIO myself, it is really neat for this kind of usecase,
and designed right from the ground up.

I think you whould think about how to write the right kind of
driver for IIO to do what you want.

Yours,
Linus Walleij

On Tue, Dec 8, 2015 at 4:20 AM, Peter Rosin <[email protected]> wrote:
> From: Peter Rosin <[email protected]>
>
> Hi!
>
> I have a signal connected to a gpio pin which is the output of
> a comparator. By changing the level of one of the inputs to the
> comparator, I can detect the envelope of the other input to
> the comparator by using a series of measurements much in the
> same maner a manual ADC works, but watching for changes on the
> comparator over a period of time instead of only the immediate
> output.
>
> Now, the input signal to the comparator might have a high frequency,
> which will cause the output from the comparator (and thus the GPIO
> input) to change rapidly.
>
> A common(?) idiom for this is to use the interrupt status register
> to catch the glitches, but then not have any interrupt tied to
> the pin as that could possibly generate pointless bursts of
> (expensive) interrupts.
>
> So, these two patches expose an interface to the PIO_ISR register
> of the pio controllers on the platform I'm targetting. The first
> patch adds some infrastructure to the gpio core and the second
> patch hooks up "my" pin controller.
>
> But hey, this seems like an old problem and I was surprised that
> I had to touch the source to do it. Which makes me wonder what I'm
> missing and what others needing to see short pulses on a pin but not
> needing/wanting interrupts are doing?
>
> Yes, there needs to be a way to select the interrupt edge w/o
> actually arming the interrupt, that is missing. And probably
> other things too, but I didn't want to do more work in case this
> is a dead end for some reason...
>
> Cheers,
> Peter
>
> Peter Rosin (2):
> gpio: Add isr property of gpio pins
> pinctrl: at91: expose the isr bit
>
> Documentation/gpio/sysfs.txt | 12 ++++++++++
> drivers/gpio/gpiolib-sysfs.c | 30 ++++++++++++++++++++++++
> drivers/gpio/gpiolib.c | 15 ++++++++++++
> drivers/pinctrl/pinctrl-at91.c | 50 ++++++++++++++++++++++++++++++++++++----
> include/linux/gpio/consumer.h | 1 +
> include/linux/gpio/driver.h | 2 ++
> 6 files changed, 106 insertions(+), 4 deletions(-)
>
> --
> 1.7.10.4
>

2015-12-12 18:02:19

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

On 11/12/15 12:53, Linus Walleij wrote:
> Quoting extensively since I'm involving the linux-iio mailinglist.
>
> The use case you describe is hand-in-glove with Industrial I/O.
> I think you want a trigger interface from IIO and read events from
> userspace using the IIO character device.
>
> Look at the userspace examples in tools/iio for how it's used
> in userspace, the subsystem is in drivers/iio. I suspect
> drivers/iio/adc/polled-gpio.c or something is where you actually
> want to go with this. The module should do all the fastpath
> work and then expose what you actually want to know to
> userspace using the IIO triggers or events.
>
> I have used IIO myself, it is really neat for this kind of usecase,
> and designed right from the ground up.
>
> I think you whould think about how to write the right kind of
> driver for IIO to do what you want.
Peter has a spot of IIO experience as well :)

I'm not sure I entirely understand what the data flows are here so I may
get this completely wrong!

Sounds like a quick, dirty and simple 'capture unit' like you'd find on a PLC to
me (be bit one that doesn't grab much data - I use these all the time at
work to catch the output from beam break sensor on automated systems and
stuff like that). Timers often support a copy to register on a gpio
signal but I'm not sure I've ever seen that supported in kernel either
(some discussion about doing this in IIO occurred a while ago but I don't
think anything ever came of it unfortunately). It was for the TI ECAP devices
by Matt Porter (cc'd) Not that closely related but perhaps Matt will
have some insight here.

So:

Are we looking to synchronised control of the DAC
feeding the comparator or is that entirely autonomous?
(for now I'll assume autonomous - it gets interesting if
not - we'd need the buffered output stuff Lars has for that)

How fast are we talking?

So I think we are basically looking for fast sampling of the gpio with latching.

I suspect the rates are high enough that an IIO trigger is going to be too expensive
(as it effectively runs as an irq). That's fine though as they are optional if
you have a good reason not to use them and a direct polling of the isr and filling a
buffer might work.

We don't currently have 1 bit channel support in IIO and in this particular case
our normal buffers are going to be very inefficient as they are kfifo based
and hence will burn 1 byte per sample if we do this the simple way.
The closest we have gotten to a 1 bit support was a comparator driver and
in the end the author decided to support that via events which have way higher
overhead than I think you want.

So if IIO is the sensible way to support this I think we need something like
the following:

1) 1 bit data type support in IIO - not too bad to add, though will need
to have some restrictions in the demux as arbitary bit channel recombining
would be horrible and costly. So in the first instance we'd probably burn 1 byte
per 1 bit channel each sample - address this later perhaps. If burning
a byte, just specify that you have a channel with realbits = 1, storagebits = 8
and it should all work. I'd like to add 1 bit support fully if you are
interested though!

2) A driver that can effectively check and clear the interrupt register and
push that to the kfifo. Probably running a kthread to keep the overhead
low - something like the recent INA2XX driver is doing (though for a rather
different reason). That would then shove data into the buffer at regular
intervals.

3) Normal userspace code would then read this - ideally with updates to
correctly interpret it as boolean data.

Doesn't sound too bad - just a question of whether it will be lightweight
enough for your use case.

Assuming I have understood even vaguely what you are doing ;)

Sounds fun.

Jonathan
>
> Yours,
> Linus Walleij
>
> On Tue, Dec 8, 2015 at 4:20 AM, Peter Rosin <[email protected]> wrote:
>> From: Peter Rosin <[email protected]>
>>
>> Hi!
>>
>> I have a signal connected to a gpio pin which is the output of
>> a comparator. By changing the level of one of the inputs to the
>> comparator, I can detect the envelope of the other input to
>> the comparator by using a series of measurements much in the
>> same maner a manual ADC works, but watching for changes on the
>> comparator over a period of time instead of only the immediate
>> output.
>>
>> Now, the input signal to the comparator might have a high frequency,
>> which will cause the output from the comparator (and thus the GPIO
>> input) to change rapidly.
>>
>> A common(?) idiom for this is to use the interrupt status register
>> to catch the glitches, but then not have any interrupt tied to
>> the pin as that could possibly generate pointless bursts of
>> (expensive) interrupts.
>>
>> So, these two patches expose an interface to the PIO_ISR register
>> of the pio controllers on the platform I'm targetting. The first
>> patch adds some infrastructure to the gpio core and the second
>> patch hooks up "my" pin controller.
>>
>> But hey, this seems like an old problem and I was surprised that
>> I had to touch the source to do it. Which makes me wonder what I'm
>> missing and what others needing to see short pulses on a pin but not
>> needing/wanting interrupts are doing?
Basically a capture unit... Be it one that doesn't grab anything else
at the moment.
>>
>> Yes, there needs to be a way to select the interrupt edge w/o
>> actually arming the interrupt, that is missing. And probably
>> other things too, but I didn't want to do more work in case this
>> is a dead end for some reason...
>>
>> Cheers,
>> Peter
>>
>> Peter Rosin (2):
>> gpio: Add isr property of gpio pins
>> pinctrl: at91: expose the isr bit
>>
>> Documentation/gpio/sysfs.txt | 12 ++++++++++
>> drivers/gpio/gpiolib-sysfs.c | 30 ++++++++++++++++++++++++
>> drivers/gpio/gpiolib.c | 15 ++++++++++++
>> drivers/pinctrl/pinctrl-at91.c | 50 ++++++++++++++++++++++++++++++++++++----
>> include/linux/gpio/consumer.h | 1 +
>> include/linux/gpio/driver.h | 2 ++
>> 6 files changed, 106 insertions(+), 4 deletions(-)
>>
>> --
>> 1.7.10.4
>>

2015-12-12 18:06:10

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

My address for Matt was out of date..
Here's hoping there is only one Matt Porter writing IIO drivers and
trying a more recent email address.

On 12/12/15 18:02, Jonathan Cameron wrote:
> On 11/12/15 12:53, Linus Walleij wrote:
>> Quoting extensively since I'm involving the linux-iio mailinglist.
>>
>> The use case you describe is hand-in-glove with Industrial I/O.
>> I think you want a trigger interface from IIO and read events from
>> userspace using the IIO character device.
>>
>> Look at the userspace examples in tools/iio for how it's used
>> in userspace, the subsystem is in drivers/iio. I suspect
>> drivers/iio/adc/polled-gpio.c or something is where you actually
>> want to go with this. The module should do all the fastpath
>> work and then expose what you actually want to know to
>> userspace using the IIO triggers or events.
>>
>> I have used IIO myself, it is really neat for this kind of usecase,
>> and designed right from the ground up.
>>
>> I think you whould think about how to write the right kind of
>> driver for IIO to do what you want.
> Peter has a spot of IIO experience as well :)
>
> I'm not sure I entirely understand what the data flows are here so I may
> get this completely wrong!
>
> Sounds like a quick, dirty and simple 'capture unit' like you'd find on a PLC to
> me (be bit one that doesn't grab much data - I use these all the time at
> work to catch the output from beam break sensor on automated systems and
> stuff like that). Timers often support a copy to register on a gpio
> signal but I'm not sure I've ever seen that supported in kernel either
> (some discussion about doing this in IIO occurred a while ago but I don't
> think anything ever came of it unfortunately). It was for the TI ECAP devices
> by Matt Porter (cc'd) Not that closely related but perhaps Matt will
> have some insight here.
>
> So:
>
> Are we looking to synchronised control of the DAC
> feeding the comparator or is that entirely autonomous?
> (for now I'll assume autonomous - it gets interesting if
> not - we'd need the buffered output stuff Lars has for that)
>
> How fast are we talking?
>
> So I think we are basically looking for fast sampling of the gpio with latching.
>
> I suspect the rates are high enough that an IIO trigger is going to be too expensive
> (as it effectively runs as an irq). That's fine though as they are optional if
> you have a good reason not to use them and a direct polling of the isr and filling a
> buffer might work.
>
> We don't currently have 1 bit channel support in IIO and in this particular case
> our normal buffers are going to be very inefficient as they are kfifo based
> and hence will burn 1 byte per sample if we do this the simple way.
> The closest we have gotten to a 1 bit support was a comparator driver and
> in the end the author decided to support that via events which have way higher
> overhead than I think you want.
>
> So if IIO is the sensible way to support this I think we need something like
> the following:
>
> 1) 1 bit data type support in IIO - not too bad to add, though will need
> to have some restrictions in the demux as arbitary bit channel recombining
> would be horrible and costly. So in the first instance we'd probably burn 1 byte
> per 1 bit channel each sample - address this later perhaps. If burning
> a byte, just specify that you have a channel with realbits = 1, storagebits = 8
> and it should all work. I'd like to add 1 bit support fully if you are
> interested though!
>
> 2) A driver that can effectively check and clear the interrupt register and
> push that to the kfifo. Probably running a kthread to keep the overhead
> low - something like the recent INA2XX driver is doing (though for a rather
> different reason). That would then shove data into the buffer at regular
> intervals.
>
> 3) Normal userspace code would then read this - ideally with updates to
> correctly interpret it as boolean data.
>
> Doesn't sound too bad - just a question of whether it will be lightweight
> enough for your use case.
>
> Assuming I have understood even vaguely what you are doing ;)
>
> Sounds fun.
>
> Jonathan
>>
>> Yours,
>> Linus Walleij
>>
>> On Tue, Dec 8, 2015 at 4:20 AM, Peter Rosin <[email protected]> wrote:
>>> From: Peter Rosin <[email protected]>
>>>
>>> Hi!
>>>
>>> I have a signal connected to a gpio pin which is the output of
>>> a comparator. By changing the level of one of the inputs to the
>>> comparator, I can detect the envelope of the other input to
>>> the comparator by using a series of measurements much in the
>>> same maner a manual ADC works, but watching for changes on the
>>> comparator over a period of time instead of only the immediate
>>> output.
>>>
>>> Now, the input signal to the comparator might have a high frequency,
>>> which will cause the output from the comparator (and thus the GPIO
>>> input) to change rapidly.
>>>
>>> A common(?) idiom for this is to use the interrupt status register
>>> to catch the glitches, but then not have any interrupt tied to
>>> the pin as that could possibly generate pointless bursts of
>>> (expensive) interrupts.
>>>
>>> So, these two patches expose an interface to the PIO_ISR register
>>> of the pio controllers on the platform I'm targetting. The first
>>> patch adds some infrastructure to the gpio core and the second
>>> patch hooks up "my" pin controller.
>>>
>>> But hey, this seems like an old problem and I was surprised that
>>> I had to touch the source to do it. Which makes me wonder what I'm
>>> missing and what others needing to see short pulses on a pin but not
>>> needing/wanting interrupts are doing?
> Basically a capture unit... Be it one that doesn't grab anything else
> at the moment.
>>>
>>> Yes, there needs to be a way to select the interrupt edge w/o
>>> actually arming the interrupt, that is missing. And probably
>>> other things too, but I didn't want to do more work in case this
>>> is a dead end for some reason...
>>>
>>> Cheers,
>>> Peter
>>>
>>> Peter Rosin (2):
>>> gpio: Add isr property of gpio pins
>>> pinctrl: at91: expose the isr bit
>>>
>>> Documentation/gpio/sysfs.txt | 12 ++++++++++
>>> drivers/gpio/gpiolib-sysfs.c | 30 ++++++++++++++++++++++++
>>> drivers/gpio/gpiolib.c | 15 ++++++++++++
>>> drivers/pinctrl/pinctrl-at91.c | 50 ++++++++++++++++++++++++++++++++++++----
>>> include/linux/gpio/consumer.h | 1 +
>>> include/linux/gpio/driver.h | 2 ++
>>> 6 files changed, 106 insertions(+), 4 deletions(-)
>>>
>>> --
>>> 1.7.10.4
>>>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

2015-12-14 10:40:35

by Peter Rosin

[permalink] [raw]
Subject: RE: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

Jonathan Cameron [mailto:[email protected]] wrote:
> On 11/12/15 12:53, Linus Walleij wrote:
> > Quoting extensively since I'm involving the linux-iio mailinglist.
> >
> > The use case you describe is hand-in-glove with Industrial I/O.
> > I think you want a trigger interface from IIO and read events from
> > userspace using the IIO character device.
> >
> > Look at the userspace examples in tools/iio for how it's used
> > in userspace, the subsystem is in drivers/iio. I suspect
> > drivers/iio/adc/polled-gpio.c or something is where you actually
> > want to go with this. The module should do all the fastpath
> > work and then expose what you actually want to know to
> > userspace using the IIO triggers or events.
> >
> > I have used IIO myself, it is really neat for this kind of usecase,
> > and designed right from the ground up.
> >
> > I think you whould think about how to write the right kind of
> > driver for IIO to do what you want.
> Peter has a spot of IIO experience as well :)

Right, the "DAC" I'm using to control the input level on the comparator
is actually my IIO mcp4531 potentiometer driver. But I have only
rudimentary IIO knowledge; that driver is trivial.

> I'm not sure I entirely understand what the data flows are here so I may
> get this completely wrong!
>
> Sounds like a quick, dirty and simple 'capture unit' like you'd find on a PLC to
> me (be bit one that doesn't grab much data - I use these all the time at
> work to catch the output from beam break sensor on automated systems and
> stuff like that). Timers often support a copy to register on a gpio
> signal but I'm not sure I've ever seen that supported in kernel either
> (some discussion about doing this in IIO occurred a while ago but I don't
> think anything ever came of it unfortunately). It was for the TI ECAP devices
> by Matt Porter (cc'd) Not that closely related but perhaps Matt will
> have some insight here.
>
> So:
>
> Are we looking to synchronised control of the DAC
> feeding the comparator or is that entirely autonomous?
> (for now I'll assume autonomous - it gets interesting if
> not - we'd need the buffered output stuff Lars has for that)
>
> How fast are we talking?
>
> So I think we are basically looking for fast sampling of the gpio with latching.
>
> I suspect the rates are high enough that an IIO trigger is going to be too expensive
> (as it effectively runs as an irq). That's fine though as they are optional if
> you have a good reason not to use them and a direct polling of the isr and filling a
> buffer might work.
>
> We don't currently have 1 bit channel support in IIO and in this particular case
> our normal buffers are going to be very inefficient as they are kfifo based
> and hence will burn 1 byte per sample if we do this the simple way.
> The closest we have gotten to a 1 bit support was a comparator driver and
> in the end the author decided to support that via events which have way higher
> overhead than I think you want.
>
> So if IIO is the sensible way to support this I think we need something like
> the following:
>
> 1) 1 bit data type support in IIO - not too bad to add, though will need
> to have some restrictions in the demux as arbitary bit channel recombining
> would be horrible and costly. So in the first instance we'd probably burn 1 byte
> per 1 bit channel each sample - address this later perhaps. If burning
> a byte, just specify that you have a channel with realbits = 1, storagebits = 8
> and it should all work. I'd like to add 1 bit support fully if you are
> interested though!
>
> 2) A driver that can effectively check and clear the interrupt register and
> push that to the kfifo. Probably running a kthread to keep the overhead
> low - something like the recent INA2XX driver is doing (though for a rather
> different reason). That would then shove data into the buffer at regular
> intervals.
>
> 3) Normal userspace code would then read this - ideally with updates to
> correctly interpret it as boolean data.
>
> Doesn't sound too bad - just a question of whether it will be lightweight
> enough for your use case.
>
> Assuming I have understood even vaguely what you are doing ;)
>
> Sounds fun.

Hmm, I've been reading the responses from you and Linus a couple of
times, and I think you have misunderstood? You talk about triggers,
fastpath, high rates and whatnot. That is not what I need and not my
itch at all! I'm not looking at getting a continuous stream of envelope
values, I only need to check the envelope value every 5 seconds or
so. Also, the whole thing is complicated by the envelope detector
being multiplexed, so that the one envelope detector can be used
for a handful of signals.

The simplified schematics are:

-------
-> | I1 | ------- -------
-> | I2 O | -> INPUT -> | A | | |
-> | I3 | | C | -> | gpio |
-> | I4 | DAC -> | B | | |
-> | I5 | ------- -------
------- CMP MCU
MUX

Userspace does the following when doing this w/o the isr patches:

1. select signal using the MUX
2. set the DAC so high that INPUT is never reaching that level.
C (and thus gpio) is now stable
3. start waiting for interrupts on gpio
4. adjust the DAC level to the level of interest
5. abort on interrupt or timeout

If the measurement timed out, I know that the signal is weaker than the
given DAC threshold, and can go back to 4 with a lower DAC level. If the
measurement was interrupted, I need to go back to 2 in order to set a
higher DAC level when point 4 is reached.

The actual INPUT envelope is found out by repeating this until we
run out of bits in the DAC (i.e. using a binary search pattern).

In my use case, I don't pretend to detect signals lower than 20Hz, so
my timeout is 50ms.

With the isr patches, the above transforms into:

1. select signal using the MUX
2. set the DAC so high that INPUT is never reaching that level.
C (and thus gpio) is now stable
3. read the isr bit to clear it
4. adjust the DAC level to the level of interest
5. read the isr bit again after 50ms

The result is available directly in the isr bit, no interrupts needed.
If I happen to wait longer than 50ms, that's not a problem either. With
the isr register version, there is simply no need to do any of this
with any critical urgency.

The actual INPUT envelope is found out in the same way as in the
interrupt case, by looping until we run out of DAC bits.

So, my problem is that doing this with the interrupt version
introduces a risk that you get a never-ending flood of interrupts if
INPUT has a frequency that's high enough. User space may never get
a chance to say that more interrupts are not interesting. Or,
at least, the device may be tied up with handling totally pointless
interrupts for an unacceptable amount of time before user space
gets to run.

INPUT may be an external signal to the device, and while I could add
specs that state a max frequency and then blame the end user in case of
trouble, I would very much like it if it was not possible the kill the
device by applying the "right" signal.



I have realized that I could work with one-shot-interrupts. Then the
urgency to disable interrupts go away, as only one interrupt would
be served. That was not my immediate solution though, as I have been
using isr type registers in this way several times before.

One gain with the interrupt approach is that you may not need to wait
the full 50ms for each measurement, but I can't say that I care much
about that.

If this is to be done in IIO, I imagine that the sanest thing would
be to integrate the whole DAC-loop and present a finished envelope
value to user space? This envelope detector would have to be pretty
configurable, or it will be next to useless to anybody but me.

I could imaging that this new IIO driver should be able to work
with any DAC type device, which in my case would be the mcp4531
dpot. Which is not a DAC, maybe that could be solved with a new
dac-dpot driver, applicable to cases where a dpot is wired as a
simple voltage divider? The new IIO driver also needs to know how
to get a reading from the comparator. I think the driver should
support having a latch between the comparator and the gpio, so it
need to know how to optionally "reset the comparator". That
would have solved the whole problem, you would never have seen
any of this if I had such a latch on my board. But the isr
register is a latch, so...

Regardless, I think such a driver still needs support from gpio
and/or pinctrl. Either exposing the isr register or supporting
one-shot-interrupts that disarm themselves before restoring the
processor interrupt flag (maybe that exists?). Otherwise the
core problem remains unsolved. Also, this imaginary IIO driver
probably have to be totally oblivious of the MUX, or the number
of possibilities explode.

Cheers,
Peter

> > Yours,
> > Linus Walleij
> >
> > On Tue, Dec 8, 2015 at 4:20 AM, Peter Rosin <[email protected]> wrote:
> >> From: Peter Rosin <[email protected]>
> >>
> >> Hi!
> >>
> >> I have a signal connected to a gpio pin which is the output of
> >> a comparator. By changing the level of one of the inputs to the
> >> comparator, I can detect the envelope of the other input to
> >> the comparator by using a series of measurements much in the
> >> same maner a manual ADC works, but watching for changes on the
> >> comparator over a period of time instead of only the immediate
> >> output.
> >>
> >> Now, the input signal to the comparator might have a high frequency,
> >> which will cause the output from the comparator (and thus the GPIO
> >> input) to change rapidly.
> >>
> >> A common(?) idiom for this is to use the interrupt status register
> >> to catch the glitches, but then not have any interrupt tied to
> >> the pin as that could possibly generate pointless bursts of
> >> (expensive) interrupts.
> >>
> >> So, these two patches expose an interface to the PIO_ISR register
> >> of the pio controllers on the platform I'm targetting. The first
> >> patch adds some infrastructure to the gpio core and the second
> >> patch hooks up "my" pin controller.
> >>
> >> But hey, this seems like an old problem and I was surprised that
> >> I had to touch the source to do it. Which makes me wonder what I'm
> >> missing and what others needing to see short pulses on a pin but not
> >> needing/wanting interrupts are doing?
> Basically a capture unit... Be it one that doesn't grab anything else
> at the moment.
> >>
> >> Yes, there needs to be a way to select the interrupt edge w/o
> >> actually arming the interrupt, that is missing. And probably
> >> other things too, but I didn't want to do more work in case this
> >> is a dead end for some reason...
> >>
> >> Cheers,
> >> Peter
> >>
> >> Peter Rosin (2):
> >> gpio: Add isr property of gpio pins
> >> pinctrl: at91: expose the isr bit
> >>
> >> Documentation/gpio/sysfs.txt | 12 ++++++++++
> >> drivers/gpio/gpiolib-sysfs.c | 30 ++++++++++++++++++++++++
> >> drivers/gpio/gpiolib.c | 15 ++++++++++++
> >> drivers/pinctrl/pinctrl-at91.c | 50 ++++++++++++++++++++++++++++++++++++----
> >> include/linux/gpio/consumer.h | 1 +
> >> include/linux/gpio/driver.h | 2 ++
> >> 6 files changed, 106 insertions(+), 4 deletions(-)
> >>
> >> --
> >> 1.7.10.4
> >>
????{.n?+???????+%?????ݶ??w??{.n?+????{??G?????{ay?ʇڙ?,j??f???h?????????z_??(?階?ݢj"???m??????G????????????&???~???iO???z??v?^?m???? ????????I?

2015-12-15 14:20:49

by Linus Walleij

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

Hi Peter,

On Mon, Dec 14, 2015 at 11:38 AM, Peter Rosin <[email protected]> wrote:

I think I atleast half-understand what you're trying to do.

> Userspace does the following when doing this w/o the isr patches:
>
> 1. select signal using the MUX
> 2. set the DAC so high that INPUT is never reaching that level.
> C (and thus gpio) is now stable
> 3. start waiting for interrupts on gpio
> 4. adjust the DAC level to the level of interest
> 5. abort on interrupt or timeout
(...)
> With the isr patches, the above transforms into:
>
> 1. select signal using the MUX
> 2. set the DAC so high that INPUT is never reaching that level.
> C (and thus gpio) is now stable
> 3. read the isr bit to clear it
> 4. adjust the DAC level to the level of interest
> 5. read the isr bit again after 50ms
>
> The result is available directly in the isr bit, no interrupts needed.

The problem I see here as kernel developer is that there is a fuzzy
and implicit separation of concerns between userspace and
kernelspace.

IMO reading hardware registers is the domain of the kernel, and
then the result thereof is presented to userspace. The kernel
handles hardware, simply.

I think we need to reverse out of this solution and instead ask
the question what the kernel should provide your userspace.
Maybe parts of what you have in userspace needs to actually
go into the kernel?

The goal of IIO seems to be to present raw and calibrated
(SI unit) data to userspace. So what data is it you want, and
why can't you just get that directly from the kernel without
tricksing around with reading registers bits in userspace?

If a kernel module needs to read an interrupt bit directly from the
GPIO controller is another thing. That is akin to how polling
network drivers start polling registers for incoming packages
instead of waiting for them to fire interrupts. Just that these are
dedicated IRQ lines, not GPIOs.

As struct irq_chip has irq_get_irqchip_state() I think this
is actually possible to achieve this by implementing that
and calling irq_get_irqchip_state().

> I have realized that I could work with one-shot-interrupts. Then the
> urgency to disable interrupts go away, as only one interrupt would
> be served. That was not my immediate solution though, as I have been
> using isr type registers in this way several times before.

That sounds like the right solution. With ONESHOT a threaded IRQ
will have its line masked until you return from the ISR thread. Would this
work for your usecase?

I suspect this require you to move the logic into the kernel? Into IIO?

> If this is to be done in IIO, I imagine that the sanest thing would
> be to integrate the whole DAC-loop and present a finished envelope
> value to user space? This envelope detector would have to be pretty
> configurable, or it will be next to useless to anybody but me.

Makes sense to me, but must be ACKed by Jonathan. But it
sounds pretty cool does it not?

> I could imaging that this new IIO driver should be able to work
> with any DAC type device, which in my case would be the mcp4531
> dpot. Which is not a DAC, maybe that could be solved with a new
> dac-dpot driver, applicable to cases where a dpot is wired as a
> simple voltage divider? The new IIO driver also needs to know how
> to get a reading from the comparator. I think the driver should
> support having a latch between the comparator and the gpio, so it
> need to know how to optionally "reset the comparator". That
> would have solved the whole problem, you would never have seen
> any of this if I had such a latch on my board. But the isr
> register is a latch, so...
>
> Regardless, I think such a driver still needs support from gpio
> and/or pinctrl. Either exposing the isr register or supporting
> one-shot-interrupts that disarm themselves before restoring the
> processor interrupt flag (maybe that exists?).

All irqchips support one-shot interrupts as long as you request a
threaded IRQ I think.

Your GPIO driver must support IRQs though but AT91 surely does.
Maybe you will need to implement irq_get_irqchip_state() on it
if you insist on polling the interrupt.

> Otherwise the
> core problem remains unsolved. Also, this imaginary IIO driver
> probably have to be totally oblivious of the MUX, or the number
> of possibilities explode.

Usually we try to follow the UNIX philisophy to do one thing
and do it good.

You haven't said much about how that MUX works, if it's another
userspace ThingOfABob or what it is. There is no generic
kernel framework for muxes so I see why people would want to
drive that using userspace GPIO lines for example.

If it is pinmuxing in a standard chip of some kind, pinctrl
handles it.

Yours,
Linus Walleij

2015-12-17 23:21:15

by Peter Rosin

[permalink] [raw]
Subject: RE: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

Hi Linus,

> On Mon, Dec 14, 2015 at 11:38 AM, Peter Rosin <[email protected]> wrote:
>
> I think I atleast half-understand what you're trying to do.

Good. It's really not that complicated, but I'm perhaps not describing
it very clearly...

> > Userspace does the following when doing this w/o the isr patches:
> >
> > 1. select signal using the MUX
> > 2. set the DAC so high that INPUT is never reaching that level.
> > C (and thus gpio) is now stable
> > 3. start waiting for interrupts on gpio
> > 4. adjust the DAC level to the level of interest
> > 5. abort on interrupt or timeout
> (...)
> > With the isr patches, the above transforms into:
> >
> > 1. select signal using the MUX
> > 2. set the DAC so high that INPUT is never reaching that level.
> > C (and thus gpio) is now stable
> > 3. read the isr bit to clear it
> > 4. adjust the DAC level to the level of interest
> > 5. read the isr bit again after 50ms
> >
> > The result is available directly in the isr bit, no interrupts needed.
>
> The problem I see here as kernel developer is that there is a fuzzy
> and implicit separation of concerns between userspace and
> kernelspace.
>
> IMO reading hardware registers is the domain of the kernel, and
> then the result thereof is presented to userspace. The kernel
> handles hardware, simply.
>
> I think we need to reverse out of this solution and instead ask
> the question what the kernel should provide your userspace.
> Maybe parts of what you have in userspace needs to actually
> go into the kernel?
>
> The goal of IIO seems to be to present raw and calibrated
> (SI unit) data to userspace. So what data is it you want, and
> why can't you just get that directly from the kernel without
> tricksing around with reading registers bits in userspace?

This all makes sense. The reason is that I'm not familiar with
the kernel APIs. I have to wrap my head around how to set up
work to be performed later, etc etc. All of that is in all
likelihood pretty straightforward, but I feel that I am
flundering around every step of the way. End result; I find
myself trying to do as little as possible inside the kernel.

I.e. I have a pretty clear picture of what needs to be done, but
the devil is in the details...

> If a kernel module needs to read an interrupt bit directly from the
> GPIO controller is another thing. That is akin to how polling
> network drivers start polling registers for incoming packages
> instead of waiting for them to fire interrupts. Just that these are
> dedicated IRQ lines, not GPIOs.

Yes, there was also the NACK to adding new gpio sysfs files which
emphasizes this.

> As struct irq_chip has irq_get_irqchip_state() I think this
> is actually possible to achieve this by implementing that
> and calling irq_get_irqchip_state().

I'll have a peek into that, but see below.

> > I have realized that I could work with one-shot-interrupts. Then the
> > urgency to disable interrupts go away, as only one interrupt would
> > be served. That was not my immediate solution though, as I have been
> > using isr type registers in this way several times before.
>
> That sounds like the right solution. With ONESHOT a threaded IRQ
> will have its line masked until you return from the ISR thread. Would this
> work for your usecase?

The ISR thread would need to be able to disable further interrupts
before it returned, is that possible without deadlock? If so, it's
a good fit.

> I suspect this require you to move the logic into the kernel? Into IIO?

Yes, and yes IIO seems about right to me too.

> > If this is to be done in IIO, I imagine that the sanest thing would
> > be to integrate the whole DAC-loop and present a finished envelope
> > value to user space? This envelope detector would have to be pretty
> > configurable, or it will be next to useless to anybody but me.
>
> Makes sense to me, but must be ACKed by Jonathan. But it
> sounds pretty cool does it not?

Right, but I don't see why it should be a problem? An envelope detector
surely fits IIO.

> > I could imaging that this new IIO driver should be able to work
> > with any DAC type device, which in my case would be the mcp4531
> > dpot. Which is not a DAC, maybe that could be solved with a new
> > dac-dpot driver, applicable to cases where a dpot is wired as a
> > simple voltage divider? The new IIO driver also needs to know how
> > to get a reading from the comparator. I think the driver should
> > support having a latch between the comparator and the gpio, so it
> > need to know how to optionally "reset the comparator". That
> > would have solved the whole problem, you would never have seen
> > any of this if I had such a latch on my board. But the isr
> > register is a latch, so...
> >
> > Regardless, I think such a driver still needs support from gpio
> > and/or pinctrl. Either exposing the isr register or supporting
> > one-shot-interrupts that disarm themselves before restoring the
> > processor interrupt flag (maybe that exists?).
>
> All irqchips support one-shot interrupts as long as you request a
> threaded IRQ I think.
>
> Your GPIO driver must support IRQs though but AT91 surely does.
> Maybe you will need to implement irq_get_irqchip_state() on it
> if you insist on polling the interrupt.

If I get the oneshot irqs to work, that indeed seems like the better
and more general solution.

> > Otherwise the
> > core problem remains unsolved. Also, this imaginary IIO driver
> > probably have to be totally oblivious of the MUX, or the number
> > of possibilities explode.
>
> Usually we try to follow the UNIX philisophy to do one thing
> and do it good.
>
> You haven't said much about how that MUX works, if it's another
> userspace ThingOfABob or what it is. There is no generic
> kernel framework for muxes so I see why people would want to
> drive that using userspace GPIO lines for example.
>
> If it is pinmuxing in a standard chip of some kind, pinctrl
> handles it.

I'm afraid it's currently done from userspace with gpio-sysfs. If
that's not changed, I need userspace to control *when* the kernel
performs the envelope detector logic.

Thanks for your feedback! I think I have enough info to get
started. Now I just need to find the time...

Cheers,
Peter
????{.n?+???????+%?????ݶ??w??{.n?+????{??G?????{ay?ʇڙ?,j??f???h?????????z_??(?階?ݢj"???m??????G????????????&???~???iO???z??v?^?m???? ????????I?

2015-12-19 16:06:36

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

On 17/12/15 23:19, Peter Rosin wrote:
> Hi Linus,
>
>> On Mon, Dec 14, 2015 at 11:38 AM, Peter Rosin <[email protected]> wrote:
>>
>> I think I atleast half-understand what you're trying to do.
>
> Good. It's really not that complicated, but I'm perhaps not describing
> it very clearly...
>
>>> Userspace does the following when doing this w/o the isr patches:
>>>
>>> 1. select signal using the MUX
>>> 2. set the DAC so high that INPUT is never reaching that level.
>>> C (and thus gpio) is now stable
>>> 3. start waiting for interrupts on gpio
>>> 4. adjust the DAC level to the level of interest
>>> 5. abort on interrupt or timeout
>> (...)
>>> With the isr patches, the above transforms into:
>>>
>>> 1. select signal using the MUX
>>> 2. set the DAC so high that INPUT is never reaching that level.
>>> C (and thus gpio) is now stable
>>> 3. read the isr bit to clear it
>>> 4. adjust the DAC level to the level of interest
>>> 5. read the isr bit again after 50ms
>>>
>>> The result is available directly in the isr bit, no interrupts needed.
>>
>> The problem I see here as kernel developer is that there is a fuzzy
>> and implicit separation of concerns between userspace and
>> kernelspace.
>>
>> IMO reading hardware registers is the domain of the kernel, and
>> then the result thereof is presented to userspace. The kernel
>> handles hardware, simply.
>>
>> I think we need to reverse out of this solution and instead ask
>> the question what the kernel should provide your userspace.
>> Maybe parts of what you have in userspace needs to actually
>> go into the kernel?
>>
>> The goal of IIO seems to be to present raw and calibrated
>> (SI unit) data to userspace. So what data is it you want, and
>> why can't you just get that directly from the kernel without
>> tricksing around with reading registers bits in userspace?
>
> This all makes sense. The reason is that I'm not familiar with
> the kernel APIs. I have to wrap my head around how to set up
> work to be performed later, etc etc. All of that is in all
> likelihood pretty straightforward, but I feel that I am
> flundering around every step of the way. End result; I find
> myself trying to do as little as possible inside the kernel.
>
> I.e. I have a pretty clear picture of what needs to be done, but
> the devil is in the details...
>
>> If a kernel module needs to read an interrupt bit directly from the
>> GPIO controller is another thing. That is akin to how polling
>> network drivers start polling registers for incoming packages
>> instead of waiting for them to fire interrupts. Just that these are
>> dedicated IRQ lines, not GPIOs.
>
> Yes, there was also the NACK to adding new gpio sysfs files which
> emphasizes this.
>
>> As struct irq_chip has irq_get_irqchip_state() I think this
>> is actually possible to achieve this by implementing that
>> and calling irq_get_irqchip_state().
>
> I'll have a peek into that, but see below.
>
>>> I have realized that I could work with one-shot-interrupts. Then the
>>> urgency to disable interrupts go away, as only one interrupt would
>>> be served. That was not my immediate solution though, as I have been
>>> using isr type registers in this way several times before.
>>
>> That sounds like the right solution. With ONESHOT a threaded IRQ
>> will have its line masked until you return from the ISR thread. Would this
>> work for your usecase?
>
> The ISR thread would need to be able to disable further interrupts
> before it returned, is that possible without deadlock? If so, it's
> a good fit.
>
>> I suspect this require you to move the logic into the kernel? Into IIO?
> bc
> Yes, and yes IIO seems about right to me too.
Whilst the full approach could well be done in IIO I can also see
some possible demand for a simple latching gpio which is I think all you
were doing with the ISR stuff.

Seems a sensible interface to support in some fashion even aside from this
discussion. Devices like PLCs do this stuff all the time though usually
in conjunction with a capture unit that will stash a copy of some
associated counter...


>
>>> If this is to be done in IIO, I imagine that the sanest thing would
>>> be to integrate the whole DAC-loop and present a finished envelope
>>> value to user space? This envelope detector would have to be pretty
>>> configurable, or it will be next to useless to anybody but me.
>>
>> Makes sense to me, but must be ACKed by Jonathan. But it
>> sounds pretty cool does it not?
>
> Right, but I don't see why it should be a problem? An envelope detector
> surely fits IIO.
Sure - it's within scope I think, but there is probably a fair bit of
new interface needed to control it..
>
>>> I could imaging that this new IIO driver should be able to work
>>> with any DAC type device, which in my case would be the mcp4531
>>> dpot. Which is not a DAC, maybe that could be solved with a new
>>> dac-dpot driver, applicable to cases where a dpot is wired as a
>>> simple voltage divider? The new IIO driver also needs to know how
>>> to get a reading from the comparator. I think the driver should
>>> support having a latch between the comparator and the gpio, so it
>>> need to know how to optionally "reset the comparator". That
>>> would have solved the whole problem, you would never have seen
>>> any of this if I had such a latch on my board. But the isr
>>> register is a latch, so...
>>>
>>> Regardless, I think such a driver still needs support from gpio
>>> and/or pinctrl. Either exposing the isr register or supporting
>>> one-shot-interrupts that disarm themselves before restoring the
>>> processor interrupt flag (maybe that exists?).
>>
>> All irqchips support one-shot interrupts as long as you request a
>> threaded IRQ I think.
>>
>> Your GPIO driver must support IRQs though but AT91 surely does.
>> Maybe you will need to implement irq_get_irqchip_state() on it
>> if you insist on polling the interrupt.
>
> If I get the oneshot irqs to work, that indeed seems like the better
> and more general solution.
>
>>> Otherwise the
>>> core problem remains unsolved. Also, this imaginary IIO driver
>>> probably have to be totally oblivious of the MUX, or the number
>>> of possibilities explode.
>>
>> Usually we try to follow the UNIX philisophy to do one thing
>> and do it good.
>>
>> You haven't said much about how that MUX works, if it's another
>> userspace ThingOfABob or what it is. There is no generic
>> kernel framework for muxes so I see why people would want to
>> drive that using userspace GPIO lines for example.
>>
>> If it is pinmuxing in a standard chip of some kind, pinctrl
>> handles it.
>
> I'm afraid it's currently done from userspace with gpio-sysfs. If
> that's not changed, I need userspace to control *when* the kernel
> performs the envelope detector logic.
Definitely a case of doing this in stages. Obviously you can
do some of the following in a different order!

1) Add a simple general purpose IIO driver to read gpios.
2) Add latching support to that - so rather than getting the current
value allow it to (if possible) support setting up a 'pseudo'
interrupt using the stuff Linus pointed you to above.
You may need an explicit 'start now' signal - not sure.
3) Get your application running from userspace as:
a) Configure your IIO 'latching gpio driver' and probably
read it to force a reset.
b) Set DAC value
c) Wait a bit
d) read from sysfs iio interface (can move to buffered stuff later)
e) Set new Dac value based on result
f) read here to burn any value set in between
g) wait a bit
h) read a gain
etc.

4) Look at tying stuff together in kernel - driving towards your full
setup. Ignoring mux for now this might look like.

A) An overarching driver that is a 'consumer' of both the dac and the
latching gpio drivers.
Latches onto the consumer interfaces of both and drives them in sync,
in first instance probably using the in kernel equivalent of the sysfs
interfaces. (can work out the nice fast version later using buffered
interfaces ;)

B) Associated gpio capture driver as described above
C) Associated DAC driver.

Actually now I think about it, to get this first version up should be
pretty straight forward. The most irritating bit will be working out
how to tie the various drivers together in a generic way. Probably
something for the new configfs interfaces where you would create an instance
of the overarching driver, associate the dac and gpio interfaces then
set some 'instantiate' attribute to true to bring it all up.

Anyhow, sounds fun. I've been mulling over a gpio based DIO driver
for IIO for a while (be it without the latching stuff). The interface
stuff will also be handy for devices with general purpose inputs
alongside their ADC type ones (e.g. ADIS IMUs tend to have a couple
that can be read in the main data burst modes I think).


>
> Thanks for your feedback! I think I have enough info to get
> started. Now I just need to find the time...
Know the feeling but it's always slightly easier when it is fun
and I think this sounds like it should be!

Good luck

Jonathan
>
> Cheers,
> Peter
> N�����r��y���b�X��ǧv�^�)޺{.n�+����{��*"��^n�r���z���h����&���G���h�(�階�ݢj"���m�����z�ޖ���f���h���~�mml==
>

2015-12-22 08:44:33

by Linus Walleij

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 0/2] Expose the PIO_ISR register on SAMA5D3

On Fri, Dec 18, 2015 at 12:19 AM, Peter Rosin <[email protected]> wrote:

> This all makes sense. The reason is that I'm not familiar with
> the kernel APIs. I have to wrap my head around how to set up
> work to be performed later, etc etc.

FWIW a random work to be performed later is a delayed work.
It can be queued on a global workqueue or you can create your
own workqueue. This is the latter way, it's even simpler if you
just use NULL as workqueue, it will then end up on the big
system workqueue. The workqueue in turn uses deferrable timers,
and these can also be used directly.

#include <linux/workqueue.h>

struct foo {
struct workqueue_struct *foo_wq;
struct delayed_work delayed_foo_work;
....
};

static void foo_work_cb(struct work_struct *work)
{
struct foo *foo = container_of(work, struct foo, delayed_foo_work.work);

... do the work ...
};

main {
INIT_DEFERRABLE_WORK(&foo->delayed_foo_work, foo_work_cb);
queue_delayed_work(foo->foo_wq, &foo->delayed_foo_work, 6*HZ);
}

6*HZ means wait for 6 seconds as the delay is given in ticks (jiffies)
and the system does HZ ticks per second.

>> > I have realized that I could work with one-shot-interrupts. Then the
>> > urgency to disable interrupts go away, as only one interrupt would
>> > be served. That was not my immediate solution though, as I have been
>> > using isr type registers in this way several times before.
>>
>> That sounds like the right solution. With ONESHOT a threaded IRQ
>> will have its line masked until you return from the ISR thread. Would this
>> work for your usecase?
>
> The ISR thread would need to be able to disable further interrupts
> before it returned, is that possible without deadlock? If so, it's
> a good fit.

Yes that is what the ONESHOT flag means. So like this:

ret = request_threaded_irq(irqnum, NULL, foo_irq_handler,
IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
"foo", foo);

To register a threaded oneshot IRQ that will run on a falling edge,
as a thread, until completed, masking its own interrupt line, but
no others.

The threaded handler can msleep(), mdelay()/udelay() etc since it
is a thread. usleep_range(a, b) is the best as the system can idle
while that happens, and can plan for a good wakeup point.

Yours,
Linus Walleij