Message ID | 20210208135347.18494-3-o.rempel@pengutronix.de |
---|---|
State | New |
Headers | show |
Series | add support for GPIO or IRQ based evemt counter | expand |
On Mon, Feb 8, 2021 at 2:53 PM Oleksij Rempel <o.rempel@pengutronix.de> wrote: > Add simple IRQ or GPIO base event counter. This device is used to measure > rotation speed of some agricultural devices, so no high frequency on the > counter pin is expected. > > The maximal measurement frequency depends on the CPU and system load. On > the idle iMX6S I was able to measure up to 20kHz without count drops. > > Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> > Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de> From GPIO and interrupt point of view this driver looks good to me. I don't know about the userspace interface etc. Yours, Linus Walleij
On Mon, Feb 08, 2021 at 02:53:47PM +0100, Oleksij Rempel wrote: > Add simple IRQ or GPIO base event counter. This device is used to measure > rotation speed of some agricultural devices, so no high frequency on the > counter pin is expected. > > The maximal measurement frequency depends on the CPU and system load. On > the idle iMX6S I was able to measure up to 20kHz without count drops. > > Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> > Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Hi Oleksij, You're adding a new driver here so I'd like to see a new entry added to the MAINTAINERS file so users know who to contact when they have questions or want to submit patches. > --- > drivers/counter/Kconfig | 10 ++ > drivers/counter/Makefile | 1 + > drivers/counter/event-cnt.c | 250 ++++++++++++++++++++++++++++++++++++ > 3 files changed, 261 insertions(+) > create mode 100644 drivers/counter/event-cnt.c > > diff --git a/drivers/counter/Kconfig b/drivers/counter/Kconfig > index 2de53ab0dd25..3284987e070a 100644 > --- a/drivers/counter/Kconfig > +++ b/drivers/counter/Kconfig > @@ -29,6 +29,16 @@ config 104_QUAD_8 > The base port addresses for the devices may be configured via the base > array module parameter. > > +config EVENT_CNT > + tristate "Event counter driver" > + depends on GPIOLIB > + help > + Select this option to enable event counter driver. Any interrupt source > + can be used by this driver as the event source. > + > + To compile this driver as a module, choose M here: the > + module will be called gpio-pulse-cnt. > + > config STM32_TIMER_CNT > tristate "STM32 Timer encoder counter driver" > depends on MFD_STM32_TIMERS || COMPILE_TEST > diff --git a/drivers/counter/Makefile b/drivers/counter/Makefile > index 0a393f71e481..6626900468f6 100644 > --- a/drivers/counter/Makefile > +++ b/drivers/counter/Makefile > @@ -6,6 +6,7 @@ > obj-$(CONFIG_COUNTER) += counter.o > > obj-$(CONFIG_104_QUAD_8) += 104-quad-8.o > +obj-$(CONFIG_EVENT_CNT) += event-cnt.o > obj-$(CONFIG_STM32_TIMER_CNT) += stm32-timer-cnt.o > obj-$(CONFIG_STM32_LPTIMER_CNT) += stm32-lptimer-cnt.o > obj-$(CONFIG_TI_EQEP) += ti-eqep.o > diff --git a/drivers/counter/event-cnt.c b/drivers/counter/event-cnt.c > new file mode 100644 > index 000000000000..a394fe72c4e4 > --- /dev/null > +++ b/drivers/counter/event-cnt.c > @@ -0,0 +1,250 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (c) 2021 Pengutronix, Oleksij Rempel <kernel@pengutronix.de> > + */ > + > +#include <linux/counter.h> > +#include <linux/gpio/consumer.h> > +#include <linux/interrupt.h> > +#include <linux/irq.h> > +#include <linux/mod_devicetable.h> > +#include <linux/module.h> > +#include <linux/platform_device.h> > + > +#define EVENT_CNT_NAME "event-cnt" > + > +struct event_cnt_priv { > + struct counter_device counter; > + struct counter_ops ops; > + struct gpio_desc *gpio; > + int irq; > + bool enabled; > + atomic_t count; > +}; > + > +static irqreturn_t event_cnt_isr(int irq, void *dev_id) > +{ > + struct event_cnt_priv *priv = dev_id; > + > + atomic_inc(&priv->count); This is just used to count the number of interrupts right? I wonder if we can do this smarter. For example, the kernel already keeps track of number of interrupts that has occurred for any particular IRQ line on a CPU (see the 'kstat_irqs' member of struct irq_desc, and the show_interrupts() function in kernel/irq/proc.c). Would it make sense to simply store the initial interrupt count on driver load or enablement, and then return the difference during a count_read() callback? > + > + return IRQ_HANDLED; > +} > + > +static ssize_t event_cnt_enable_read(struct counter_device *counter, > + struct counter_count *count, void *private, > + char *buf) > +{ > + struct event_cnt_priv *priv = counter->priv; > + > + return sysfs_emit(buf, "%d\n", priv->enabled); > +} > + > +static ssize_t event_cnt_enable_write(struct counter_device *counter, > + struct counter_count *count, > + void *private, const char *buf, > + size_t len) > +{ > + struct event_cnt_priv *priv = counter->priv; > + bool enable; > + ssize_t ret; > + > + ret = kstrtobool(buf, &enable); > + if (ret) > + return ret; > + > + if (priv->enabled == enable) > + return len; > + > + if (enable) { > + priv->enabled = enable; > + enable_irq(priv->irq); > + } else { > + disable_irq(priv->irq); > + priv->enabled = enable; > + } Given the conditional path we know the value "enable" will hold here. it'll be good to set priv->enabled explicitly here so there's no confusion to a future reviewer: if (enable) { priv->enabled = 1; enable_irq(priv->irq); } else { disable_irq(priv->irq); priv->enabled = 0; } > + > + return len; > +} > + > +static const struct counter_count_ext event_cnt_ext[] = { > + { > + .name = "enable", > + .read = event_cnt_enable_read, > + .write = event_cnt_enable_write, > + }, > +}; > + > +static enum counter_synapse_action event_cnt_synapse_actionss[] = { > + COUNTER_SYNAPSE_ACTION_RISING_EDGE, > +}; > + > +static int event_cnt_action_get(struct counter_device *counter, > + struct counter_count *count, > + struct counter_synapse *synapse, > + size_t *action) > +{ > + *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE; Fix this as mentioned in my earlier reply: https://lore.kernel.org/linux-iio/YCFHRGbiVxpNgkQS@shinobu/ > + > + return 0; > +} > + > +static int event_cnt_read(struct counter_device *counter, > + struct counter_count *count, > + unsigned long *val) > +{ > + struct event_cnt_priv *priv = counter->priv; > + > + *val = atomic_read(&priv->count); > + > + return 0; > +} > + > +static int event_cnt_write(struct counter_device *counter, > + struct counter_count *count, > + const unsigned long val) > +{ > + struct event_cnt_priv *priv = counter->priv; > + > + atomic_set(&priv->count, val); > + > + return 0; > +} > + > +static int event_cnt_function_get(struct counter_device *counter, > + struct counter_count *count, size_t *function) > +{ > + *function = COUNTER_COUNT_FUNCTION_INCREASE; Fix this as mentioned in my earlier reply: https://lore.kernel.org/linux-iio/YCFHRGbiVxpNgkQS@shinobu/ > + > + return 0; > +} > + > +static int event_cnt_signal_read(struct counter_device *counter, > + struct counter_signal *signal, > + enum counter_signal_value *val) > +{ > + struct event_cnt_priv *priv = counter->priv; > + int ret; > + > + ret = gpiod_get_value(priv->gpio); > + if (ret < 0) > + return ret; > + > + *val = ret ? COUNTER_SIGNAL_HIGH : COUNTER_SIGNAL_LOW; > + > + return 0; > +} > + > +static struct counter_signal event_cnt_signals[] = { > + { > + .id = 0, > + .name = "Channel 0 signal", You should choose a more description name for this Signal; "Channel 0 signal" isn't very useful information for the user. Is this signal the respective GPIO line state? > + }, > +}; > + > +static struct counter_synapse event_cnt_synapses[] = { > + { > + .actions_list = event_cnt_synapse_actionss, > + .num_actions = ARRAY_SIZE(event_cnt_synapse_actionss), > + .signal = &event_cnt_signals[0] > + }, > +}; > + > +static enum counter_count_function event_cnt_functions[] = { > + COUNTER_COUNT_FUNCTION_INCREASE, > +}; > + > +static struct counter_count event_cnts[] = { > + { > + .id = 0, > + .name = "Channel 1 Count", > + .functions_list = event_cnt_functions, > + .num_functions = ARRAY_SIZE(event_cnt_functions), > + .synapses = event_cnt_synapses, > + .num_synapses = ARRAY_SIZE(event_cnt_synapses), > + .ext = event_cnt_ext, > + .num_ext = ARRAY_SIZE(event_cnt_ext), > + }, > +}; > + > +static int event_cnt_probe(struct platform_device *pdev) > +{ > + struct device *dev = &pdev->dev; > + struct event_cnt_priv *priv; > + int ret; > + > + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); > + if (!priv) > + return -ENOMEM; > + > + priv->irq = platform_get_irq_optional(pdev, 0); > + if (priv->irq == -ENXIO) > + priv->irq = 0; > + else if (priv->irq < 0) > + return dev_err_probe(dev, priv->irq, "failed to get IRQ\n"); > + > + priv->gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_IN); > + if (IS_ERR(priv->gpio)) > + return dev_err_probe(dev, PTR_ERR(priv->gpio), "failed to get GPIO\n"); > + > + if (!priv->irq && !priv->gpio) { > + dev_err(dev, "IRQ and GPIO are not found. At least one source should be provided\n"); > + return -ENODEV; > + } > + > + if (!priv->irq) { > + int irq = gpiod_to_irq(priv->gpio); > + > + if (irq < 0) > + return dev_err_probe(dev, irq, "failed to get IRQ from GPIO\n"); > + > + priv->irq = irq; > + } > + > + priv->ops.action_get = event_cnt_action_get; > + priv->ops.count_read = event_cnt_read; > + priv->ops.count_write = event_cnt_write; > + priv->ops.function_get = event_cnt_function_get; > + if (priv->gpio) > + priv->ops.signal_read = event_cnt_signal_read; Move ops out of priv and make it static const. You can get rid of this priv->gpio conditional here and instead perform it for num_signals as I explain inline below. > + > + priv->counter.name = dev_name(dev); > + priv->counter.parent = dev; > + priv->counter.ops = &priv->ops; > + priv->counter.counts = event_cnts; > + priv->counter.num_counts = ARRAY_SIZE(event_cnts); > + priv->counter.signals = event_cnt_signals; > + priv->counter.num_signals = ARRAY_SIZE(event_cnt_signals); If the Signal provided is only valid for GPIO sources, then you should add a conditional check here. Simply setting num_signals to 0 should be enough to disable the creation of the Signal attribute for non-GPIO sources: priv->counter.num_signals = priv->gpio ? ARRAY_SIZE(event_cnt_signals) : 0; > + priv->counter.priv = priv; > + > + irq_set_status_flags(priv->irq, IRQ_NOAUTOEN); > + ret = devm_request_irq(dev, priv->irq, event_cnt_isr, > + IRQF_TRIGGER_RISING | IRQF_NO_THREAD, > + EVENT_CNT_NAME, priv); > + if (ret) > + return ret; > + > + platform_set_drvdata(pdev, priv); This line isn't needed; you don't ever deal with struct device directly do you? William Breathitt Gray > + > + return devm_counter_register(dev, &priv->counter); > +} > + > +static const struct of_device_id event_cnt_of_match[] = { > + { .compatible = "event-counter", }, > + {} > +}; > +MODULE_DEVICE_TABLE(of, event_cnt_of_match); > + > +static struct platform_driver event_cnt_driver = { > + .probe = event_cnt_probe, > + .driver = { > + .name = EVENT_CNT_NAME, > + .of_match_table = event_cnt_of_match, > + }, > +}; > +module_platform_driver(event_cnt_driver); > + > +MODULE_ALIAS("platform:event-counter"); > +MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>"); > +MODULE_DESCRIPTION("Event counter driver"); > +MODULE_LICENSE("GPL v2"); > -- > 2.30.0 >
On Fri, Feb 12, 2021 at 10:26:39AM +0100, Linus Walleij wrote: > On Mon, Feb 8, 2021 at 2:53 PM Oleksij Rempel <o.rempel@pengutronix.de> wrote: > > > Add simple IRQ or GPIO base event counter. This device is used to measure > > rotation speed of some agricultural devices, so no high frequency on the > > counter pin is expected. > > > > The maximal measurement frequency depends on the CPU and system load. On > > the idle iMX6S I was able to measure up to 20kHz without count drops. > > > > Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> > > Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de> > > From GPIO and interrupt point of view this driver looks good to me. > I don't know about the userspace interface etc. May I have your Reviewed-by? -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Hi William, On Sun, Feb 14, 2021 at 05:54:22PM +0900, William Breathitt Gray wrote: > On Mon, Feb 08, 2021 at 02:53:47PM +0100, Oleksij Rempel wrote: > > Add simple IRQ or GPIO base event counter. This device is used to measure > > rotation speed of some agricultural devices, so no high frequency on the > > counter pin is expected. > > > > The maximal measurement frequency depends on the CPU and system load. On > > the idle iMX6S I was able to measure up to 20kHz without count drops. > > > > Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> > > Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de> > > Hi Oleksij, > > You're adding a new driver here so I'd like to see a new entry added to > the MAINTAINERS file so users know who to contact when they have > questions or want to submit patches. done > > --- > > drivers/counter/Kconfig | 10 ++ > > drivers/counter/Makefile | 1 + > > drivers/counter/event-cnt.c | 250 ++++++++++++++++++++++++++++++++++++ > > 3 files changed, 261 insertions(+) > > create mode 100644 drivers/counter/event-cnt.c > > > > diff --git a/drivers/counter/Kconfig b/drivers/counter/Kconfig > > index 2de53ab0dd25..3284987e070a 100644 > > --- a/drivers/counter/Kconfig > > +++ b/drivers/counter/Kconfig > > @@ -29,6 +29,16 @@ config 104_QUAD_8 > > The base port addresses for the devices may be configured via the base > > array module parameter. > > > > +config EVENT_CNT > > + tristate "Event counter driver" > > + depends on GPIOLIB > > + help > > + Select this option to enable event counter driver. Any interrupt source > > + can be used by this driver as the event source. > > + > > + To compile this driver as a module, choose M here: the > > + module will be called gpio-pulse-cnt. > > + > > config STM32_TIMER_CNT > > tristate "STM32 Timer encoder counter driver" > > depends on MFD_STM32_TIMERS || COMPILE_TEST > > diff --git a/drivers/counter/Makefile b/drivers/counter/Makefile > > index 0a393f71e481..6626900468f6 100644 > > --- a/drivers/counter/Makefile > > +++ b/drivers/counter/Makefile > > @@ -6,6 +6,7 @@ > > obj-$(CONFIG_COUNTER) += counter.o > > > > obj-$(CONFIG_104_QUAD_8) += 104-quad-8.o > > +obj-$(CONFIG_EVENT_CNT) += event-cnt.o > > obj-$(CONFIG_STM32_TIMER_CNT) += stm32-timer-cnt.o > > obj-$(CONFIG_STM32_LPTIMER_CNT) += stm32-lptimer-cnt.o > > obj-$(CONFIG_TI_EQEP) += ti-eqep.o > > diff --git a/drivers/counter/event-cnt.c b/drivers/counter/event-cnt.c > > new file mode 100644 > > index 000000000000..a394fe72c4e4 > > --- /dev/null > > +++ b/drivers/counter/event-cnt.c > > @@ -0,0 +1,250 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* > > + * Copyright (c) 2021 Pengutronix, Oleksij Rempel <kernel@pengutronix.de> > > + */ > > + > > +#include <linux/counter.h> > > +#include <linux/gpio/consumer.h> > > +#include <linux/interrupt.h> > > +#include <linux/irq.h> > > +#include <linux/mod_devicetable.h> > > +#include <linux/module.h> > > +#include <linux/platform_device.h> > > + > > +#define EVENT_CNT_NAME "event-cnt" > > + > > +struct event_cnt_priv { > > + struct counter_device counter; > > + struct counter_ops ops; > > + struct gpio_desc *gpio; > > + int irq; > > + bool enabled; > > + atomic_t count; > > +}; > > + > > +static irqreturn_t event_cnt_isr(int irq, void *dev_id) > > +{ > > + struct event_cnt_priv *priv = dev_id; > > + > > + atomic_inc(&priv->count); > > This is just used to count the number of interrupts right? I wonder if > we can do this smarter. For example, the kernel already keeps track of > number of interrupts that has occurred for any particular IRQ line on a > CPU (see the 'kstat_irqs' member of struct irq_desc, and the > show_interrupts() function in kernel/irq/proc.c). Would it make sense to > simply store the initial interrupt count on driver load or enablement, > and then return the difference during a count_read() callback? This driver do not makes a lot of sense without your chardev patches. As soon as this patches go mainline, this driver will be able to send event with a timestamp and counter state to the user space. With other words, we will need an irq handler anyway. In this case we can't save more RAM or CPU cycles by using system irq counters. > > + > > + return IRQ_HANDLED; > > +} > > + > > +static ssize_t event_cnt_enable_read(struct counter_device *counter, > > + struct counter_count *count, void *private, > > + char *buf) > > +{ > > + struct event_cnt_priv *priv = counter->priv; > > + > > + return sysfs_emit(buf, "%d\n", priv->enabled); > > +} > > + > > +static ssize_t event_cnt_enable_write(struct counter_device *counter, > > + struct counter_count *count, > > + void *private, const char *buf, > > + size_t len) > > +{ > > + struct event_cnt_priv *priv = counter->priv; > > + bool enable; > > + ssize_t ret; > > + > > + ret = kstrtobool(buf, &enable); > > + if (ret) > > + return ret; > > + > > + if (priv->enabled == enable) > > + return len; > > + > > + if (enable) { > > + priv->enabled = enable; > > + enable_irq(priv->irq); > > + } else { > > + disable_irq(priv->irq); > > + priv->enabled = enable; > > + } > > Given the conditional path we know the value "enable" will hold here. > it'll be good to set priv->enabled explicitly here so there's no > confusion to a future reviewer: > > if (enable) { > priv->enabled = 1; > enable_irq(priv->irq); > } else { > disable_irq(priv->irq); > priv->enabled = 0; > } Done > > + > > + return len; > > +} > > + > > +static const struct counter_count_ext event_cnt_ext[] = { > > + { > > + .name = "enable", > > + .read = event_cnt_enable_read, > > + .write = event_cnt_enable_write, > > + }, > > +}; > > + > > +static enum counter_synapse_action event_cnt_synapse_actionss[] = { > > + COUNTER_SYNAPSE_ACTION_RISING_EDGE, > > +}; > > + > > +static int event_cnt_action_get(struct counter_device *counter, > > + struct counter_count *count, > > + struct counter_synapse *synapse, > > + size_t *action) > > +{ > > + *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE; > > Fix this as mentioned in my earlier reply: > https://lore.kernel.org/linux-iio/YCFHRGbiVxpNgkQS@shinobu/ > > > + > > + return 0; > > +} > > + > > +static int event_cnt_read(struct counter_device *counter, > > + struct counter_count *count, > > + unsigned long *val) > > +{ > > + struct event_cnt_priv *priv = counter->priv; > > + > > + *val = atomic_read(&priv->count); > > + > > + return 0; > > +} > > + > > +static int event_cnt_write(struct counter_device *counter, > > + struct counter_count *count, > > + const unsigned long val) > > +{ > > + struct event_cnt_priv *priv = counter->priv; > > + > > + atomic_set(&priv->count, val); > > + > > + return 0; > > +} > > + > > +static int event_cnt_function_get(struct counter_device *counter, > > + struct counter_count *count, size_t *function) > > +{ > > + *function = COUNTER_COUNT_FUNCTION_INCREASE; > > Fix this as mentioned in my earlier reply: > https://lore.kernel.org/linux-iio/YCFHRGbiVxpNgkQS@shinobu/ done > > + > > + return 0; > > +} > > + > > +static int event_cnt_signal_read(struct counter_device *counter, > > + struct counter_signal *signal, > > + enum counter_signal_value *val) > > +{ > > + struct event_cnt_priv *priv = counter->priv; > > + int ret; > > + > > + ret = gpiod_get_value(priv->gpio); > > + if (ret < 0) > > + return ret; > > + > > + *val = ret ? COUNTER_SIGNAL_HIGH : COUNTER_SIGNAL_LOW; > > + > > + return 0; > > +} > > + > > +static struct counter_signal event_cnt_signals[] = { > > + { > > + .id = 0, > > + .name = "Channel 0 signal", > > You should choose a more description name for this Signal; > "Channel 0 signal" isn't very useful information for the user. Is this > signal the respective GPIO line state? Sounds plausible. How about "Channel 0, GPIO line state"? > > + }, > > +}; > > + > > +static struct counter_synapse event_cnt_synapses[] = { > > + { > > + .actions_list = event_cnt_synapse_actionss, > > + .num_actions = ARRAY_SIZE(event_cnt_synapse_actionss), > > + .signal = &event_cnt_signals[0] > > + }, > > +}; > > + > > +static enum counter_count_function event_cnt_functions[] = { > > + COUNTER_COUNT_FUNCTION_INCREASE, > > +}; > > + > > +static struct counter_count event_cnts[] = { > > + { > > + .id = 0, > > + .name = "Channel 1 Count", > > + .functions_list = event_cnt_functions, > > + .num_functions = ARRAY_SIZE(event_cnt_functions), > > + .synapses = event_cnt_synapses, > > + .num_synapses = ARRAY_SIZE(event_cnt_synapses), > > + .ext = event_cnt_ext, > > + .num_ext = ARRAY_SIZE(event_cnt_ext), > > + }, > > +}; > > + > > +static int event_cnt_probe(struct platform_device *pdev) > > +{ > > + struct device *dev = &pdev->dev; > > + struct event_cnt_priv *priv; > > + int ret; > > + > > + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); > > + if (!priv) > > + return -ENOMEM; > > + > > + priv->irq = platform_get_irq_optional(pdev, 0); > > + if (priv->irq == -ENXIO) > > + priv->irq = 0; > > + else if (priv->irq < 0) > > + return dev_err_probe(dev, priv->irq, "failed to get IRQ\n"); > > + > > + priv->gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_IN); > > + if (IS_ERR(priv->gpio)) > > + return dev_err_probe(dev, PTR_ERR(priv->gpio), "failed to get GPIO\n"); > > + > > + if (!priv->irq && !priv->gpio) { > > + dev_err(dev, "IRQ and GPIO are not found. At least one source should be provided\n"); > > + return -ENODEV; > > + } > > + > > + if (!priv->irq) { > > + int irq = gpiod_to_irq(priv->gpio); > > + > > + if (irq < 0) > > + return dev_err_probe(dev, irq, "failed to get IRQ from GPIO\n"); > > + > > + priv->irq = irq; > > + } > > + > > + priv->ops.action_get = event_cnt_action_get; > > + priv->ops.count_read = event_cnt_read; > > + priv->ops.count_write = event_cnt_write; > > + priv->ops.function_get = event_cnt_function_get; > > + if (priv->gpio) > > + priv->ops.signal_read = event_cnt_signal_read; > > Move ops out of priv and make it static const. You can get rid of this > priv->gpio conditional here and instead perform it for num_signals > as I explain inline below. done > > + > > + priv->counter.name = dev_name(dev); > > + priv->counter.parent = dev; > > + priv->counter.ops = &priv->ops; > > + priv->counter.counts = event_cnts; > > + priv->counter.num_counts = ARRAY_SIZE(event_cnts); > > + priv->counter.signals = event_cnt_signals; > > + priv->counter.num_signals = ARRAY_SIZE(event_cnt_signals); > > If the Signal provided is only valid for GPIO sources, then you should > add a conditional check here. Simply setting num_signals to 0 should be > enough to disable the creation of the Signal attribute for non-GPIO > sources: > > priv->counter.num_signals = priv->gpio ? > ARRAY_SIZE(event_cnt_signals) : 0; > done > > + priv->counter.priv = priv; > > + > > + irq_set_status_flags(priv->irq, IRQ_NOAUTOEN); > > + ret = devm_request_irq(dev, priv->irq, event_cnt_isr, > > + IRQF_TRIGGER_RISING | IRQF_NO_THREAD, > > + EVENT_CNT_NAME, priv); > > + if (ret) > > + return ret; > > + > > + platform_set_drvdata(pdev, priv); > > This line isn't needed; you don't ever deal with struct device directly > do you? no. removed. Regards, Oleksij -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
On Mon, Feb 15, 2021 at 10:17:37AM +0100, Oleksij Rempel wrote: > > > +static irqreturn_t event_cnt_isr(int irq, void *dev_id) > > > +{ > > > + struct event_cnt_priv *priv = dev_id; > > > + > > > + atomic_inc(&priv->count); > > > > This is just used to count the number of interrupts right? I wonder if > > we can do this smarter. For example, the kernel already keeps track of > > number of interrupts that has occurred for any particular IRQ line on a > > CPU (see the 'kstat_irqs' member of struct irq_desc, and the > > show_interrupts() function in kernel/irq/proc.c). Would it make sense to > > simply store the initial interrupt count on driver load or enablement, > > and then return the difference during a count_read() callback? > > This driver do not makes a lot of sense without your chardev patches. As > soon as this patches go mainline, this driver will be able to send > event with a timestamp and counter state to the user space. > > With other words, we will need an irq handler anyway. In this case we > can't save more RAM or CPU cycles by using system irq counters. It's true that this driver will need an IRQ handler when the timestamp functionality is added, but deriving the count value is different matter regardless. There's already code in the kernel to retrieve the number of interrupts, so it makes sense that we use that rather than rolling our own -- at the very least to ensure the value we provide to users is consistent with the ones already provided by other areas of the kernel. To that end, I'd like to see your cnt_isr() function removed for this patchset (you can bring it back once timestamp support is added). Reimplement your cnt_read/cnt_write() functions to instead use kstat_irqs_usr() from <linux/kernel_stat.h> to get the current number of interrupts the IRQ line and use it to derive your count value for this driver. > > > +static struct counter_signal event_cnt_signals[] = { > > > + { > > > + .id = 0, > > > + .name = "Channel 0 signal", > > > > You should choose a more description name for this Signal; > > "Channel 0 signal" isn't very useful information for the user. Is this > > signal the respective GPIO line state? > > Sounds plausible. How about "Channel 0, GPIO line state"? Ideally, this would match the GPIO name (or I suppose the IRQ number if not a GPIO line). So in your probe() function you can do something like this I believe: cnt_signals[0].name = priv->gpio->name; Of course, you should first check whether this is a GPIO line or IRQ line and set the name accordingly. William Breathitt Gray
On Mon, Feb 22, 2021 at 10:43:00AM +0900, William Breathitt Gray wrote: > On Mon, Feb 15, 2021 at 10:17:37AM +0100, Oleksij Rempel wrote: > > > > +static irqreturn_t event_cnt_isr(int irq, void *dev_id) > > > > +{ > > > > + struct event_cnt_priv *priv = dev_id; > > > > + > > > > + atomic_inc(&priv->count); > > > > > > This is just used to count the number of interrupts right? I wonder if > > > we can do this smarter. For example, the kernel already keeps track of > > > number of interrupts that has occurred for any particular IRQ line on a > > > CPU (see the 'kstat_irqs' member of struct irq_desc, and the > > > show_interrupts() function in kernel/irq/proc.c). Would it make sense to > > > simply store the initial interrupt count on driver load or enablement, > > > and then return the difference during a count_read() callback? > > > > This driver do not makes a lot of sense without your chardev patches. As > > soon as this patches go mainline, this driver will be able to send > > event with a timestamp and counter state to the user space. > > > > With other words, we will need an irq handler anyway. In this case we > > can't save more RAM or CPU cycles by using system irq counters. > > It's true that this driver will need an IRQ handler when the timestamp > functionality is added, but deriving the count value is different matter > regardless. There's already code in the kernel to retrieve the number of > interrupts, so it makes sense that we use that rather than rolling our > own -- at the very least to ensure the value we provide to users is > consistent with the ones already provided by other areas of the kernel. We are talking about one or two code lines. If we will take some duplication search engine, it will find that major part of the kernel is matching against it. Newer the less, this driver provides a way to reset the counter. Why should we drop this functionality no advantage? > To that end, I'd like to see your cnt_isr() function removed for this > patchset (you can bring it back once timestamp support is added). Are you suggesting to enable IRQ without interrupt handler? May be i'm missing some thing.. I do not understand it. > Reimplement your cnt_read/cnt_write() functions to instead use > kstat_irqs_usr() from <linux/kernel_stat.h> to get the current number of > interrupts the IRQ line and use it to derive your count value for this > driver. I can follow the counter read way, but overwriting system wide counter for local use is bad idea. > > > > +static struct counter_signal event_cnt_signals[] = { > > > > + { > > > > + .id = 0, > > > > + .name = "Channel 0 signal", > > > > > > You should choose a more description name for this Signal; > > > "Channel 0 signal" isn't very useful information for the user. Is this > > > signal the respective GPIO line state? > > > > Sounds plausible. How about "Channel 0, GPIO line state"? > > Ideally, this would match the GPIO name (or I suppose the IRQ number if > not a GPIO line). So in your probe() function you can do something like > this I believe: > > cnt_signals[0].name = priv->gpio->name; to make this possible, i would need hack gpiolib framework and add name/label exporter. But after endless rounds of pingponging me for renaming the driver and removing interrupt handler, i feel like we are not having serious discussion for mainlining this driver. Is it some expensive way to prepare me for 1. April joke? > Of course, you should first check whether this is a GPIO line or IRQ > line and set the name accordingly. Please, let's stop bike-shed for now. This driver has no limitless budget. If there are serious problem, I would love to fix it, but if we still discussing name of the driver or how to misuse kernel interrupt handling, then it makes no sense to continue. Regards, Oleksij -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Hello William, Here is cooled down technical answer. Excuse me for over reacting. On Tue, Feb 23, 2021 at 11:06:56AM +0100, Oleksij Rempel wrote: > On Mon, Feb 22, 2021 at 10:43:00AM +0900, William Breathitt Gray wrote: > > On Mon, Feb 15, 2021 at 10:17:37AM +0100, Oleksij Rempel wrote: > > > > > +static irqreturn_t event_cnt_isr(int irq, void *dev_id) > > > > > +{ > > > > > + struct event_cnt_priv *priv = dev_id; > > > > > + > > > > > + atomic_inc(&priv->count); > > > > > > > > This is just used to count the number of interrupts right? I wonder if > > > > we can do this smarter. For example, the kernel already keeps track of > > > > number of interrupts that has occurred for any particular IRQ line on a > > > > CPU (see the 'kstat_irqs' member of struct irq_desc, and the > > > > show_interrupts() function in kernel/irq/proc.c). Would it make sense to > > > > simply store the initial interrupt count on driver load or enablement, > > > > and then return the difference during a count_read() callback? > > > > > > This driver do not makes a lot of sense without your chardev patches. As > > > soon as this patches go mainline, this driver will be able to send > > > event with a timestamp and counter state to the user space. > > > > > > With other words, we will need an irq handler anyway. In this case we > > > can't save more RAM or CPU cycles by using system irq counters. > > > > It's true that this driver will need an IRQ handler when the timestamp > > functionality is added, but deriving the count value is different matter > > regardless. There's already code in the kernel to retrieve the number of > > interrupts, so it makes sense that we use that rather than rolling our > > own -- at the very least to ensure the value we provide to users is > > consistent with the ones already provided by other areas of the kernel. The value provided by the driver is consistent only if it is not overwritten by user. The driver provides an interface to reset/overwrite it. At least after this step the value is not consistent. > We are talking about one or two code lines. If we will take some > duplication search engine, it will find that major part of the kernel > is matching against it. > > Newer the less, this driver provides a way to reset the counter. Why > should we drop this functionality no advantage? > > > To that end, I'd like to see your cnt_isr() function removed for this > > patchset (you can bring it back once timestamp support is added). It make no sense to request an interrupt without interrupt service routine. https://elixir.bootlin.com/linux/latest/source/kernel/irq/manage.c#L2072if if (!handler) { if (!thread_fn) return -EINVAL; As you can see, requesting an irq need at least handler or thread_fn. enable_irq: this will explode at least here: https://elixir.bootlin.com/linux/latest/source/kernel/irq/manage.c#L778 If he have no IRQ handler and some how was able to enable it, at some point this IRQ will be disabled by this code: https://elixir.bootlin.com/linux/latest/source/kernel/irq/spurious.c#L410 if (unlikely(desc->irqs_unhandled > 99900)) { /* * The interrupt is stuck */ __report_bad_irq(desc, action_ret); /* * Now kill the IRQ */ printk(KERN_EMERG "Disabling IRQ #%d\n", irq); desc->istate |= IRQS_SPURIOUS_DISABLED; desc->depth++; irq_disable(desc); With current code, we can't request or enable IRQ without cnt_isr(). Not that it is not possible, but it make no sense to me. > Are you suggesting to enable IRQ without interrupt handler? May be i'm > missing some thing.. I do not understand it. > > > Reimplement your cnt_read/cnt_write() functions to instead use > > kstat_irqs_usr() from <linux/kernel_stat.h> to get the current number of > > interrupts the IRQ line and use it to derive your count value for this > > driver. irq descriptor has 3 counters: - irq_count: this value can be reset any time by the kernel at least by the note_interrupt() - irqs_unhandled: this value is increased in case of missing irq handler. Or if handler has returns IRQ_NONE. - tot_count: this value should not be reset. Non of this values is suitable for cnt_read() and cnt_write(). Only tot_count would be suitable if cnt_write() is removed. I do not see it as acceptable option. For this driver, we still need extra counter, where only this driver is responsible for writing to it. > I can follow the counter read way, but overwriting system wide counter > for local use is bad idea. > > > > > > +static struct counter_signal event_cnt_signals[] = { > > > > > + { > > > > > + .id = 0, > > > > > + .name = "Channel 0 signal", > > > > > > > > You should choose a more description name for this Signal; > > > > "Channel 0 signal" isn't very useful information for the user. Is this > > > > signal the respective GPIO line state? > > > > > > Sounds plausible. How about "Channel 0, GPIO line state"? > > > > Ideally, this would match the GPIO name (or I suppose the IRQ number if > > not a GPIO line). So in your probe() function you can do something like > > this I believe: > > > > cnt_signals[0].name = priv->gpio->name; > > to make this possible, i would need hack gpiolib framework and add > name/label exporter. But after endless rounds of pingponging me for > renaming the driver and removing interrupt handler, i feel like we are > not having serious discussion for mainlining this driver. Probably for good reason, struct gpio_desc was made local and is located in the drivers/gpio/gpiolib.h. It feels like additional hack to include it. I assume, it should be done properly so there is a function to provide gpio name or label. @Linus Walleij are there any good way to get the GPIO name? And which name will be actually used? A label provided over devicetree? If I see it correctly, it would need more work to make the kernel infrastructure suitable for this suggestions. Some of them are only needed before chardev support will go mainline and , in long term, not worth to spend time on it. Probably I do not understand you and i missing some thing? Regards, Oleksij -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
On Tue, Feb 23, 2021 at 06:45:16PM +0100, Oleksij Rempel wrote: > Hello William, > > Here is cooled down technical answer. Excuse me for over reacting. Hello Oleksij, Let me apologize too if I offended you in some way in with previous response, I assure you that was not my intention. I truly do believe this is a useful driver to have in the kernel and I want to make that happen; my concerns with your patch are purely technical in nature and I'm certain we can find a solution working together. > On Tue, Feb 23, 2021 at 11:06:56AM +0100, Oleksij Rempel wrote: > > On Mon, Feb 22, 2021 at 10:43:00AM +0900, William Breathitt Gray wrote: > > > On Mon, Feb 15, 2021 at 10:17:37AM +0100, Oleksij Rempel wrote: > > > > > > +static irqreturn_t event_cnt_isr(int irq, void *dev_id) > > > > > > +{ > > > > > > + struct event_cnt_priv *priv = dev_id; > > > > > > + > > > > > > + atomic_inc(&priv->count); > > > > > > > > > > This is just used to count the number of interrupts right? I wonder if > > > > > we can do this smarter. For example, the kernel already keeps track of > > > > > number of interrupts that has occurred for any particular IRQ line on a > > > > > CPU (see the 'kstat_irqs' member of struct irq_desc, and the > > > > > show_interrupts() function in kernel/irq/proc.c). Would it make sense to > > > > > simply store the initial interrupt count on driver load or enablement, > > > > > and then return the difference during a count_read() callback? > > > > > > > > This driver do not makes a lot of sense without your chardev patches. As > > > > soon as this patches go mainline, this driver will be able to send > > > > event with a timestamp and counter state to the user space. > > > > > > > > With other words, we will need an irq handler anyway. In this case we > > > > can't save more RAM or CPU cycles by using system irq counters. > > > > > > It's true that this driver will need an IRQ handler when the timestamp > > > functionality is added, but deriving the count value is different matter > > > regardless. There's already code in the kernel to retrieve the number of > > > interrupts, so it makes sense that we use that rather than rolling our > > > own -- at the very least to ensure the value we provide to users is > > > consistent with the ones already provided by other areas of the kernel. > > The value provided by the driver is consistent only if it is not > overwritten by user. The driver provides an interface to reset/overwrite it. > At least after this step the value is not consistent. I wasn't clear here so I apologize. What I would like is for this driver to maintain its own local count value derived from kstat_irqs_usr(). So for example, you can use the "count" member of your struct interrupt_cnt_priv to maintain this value (it can be unsigned int instead of atomic_t): static int interrupt_cnt_read(struct counter_device *counter, struct counter_count *count, unsigned long *val) { struct interrupt_cnt_priv *priv = counter->priv; *val = kstat_irqs_usr(priv->irq) - priv->count; return 0; } static int interrupt_cnt_write(struct counter_device *counter, struct counter_count *count, const unsigned long val) { struct interrupt_cnt_priv *priv = counter->priv; /* kstat_irqs_usr() returns unsigned int */ if (val != (unsigned int)val) return -ERANGE; priv->count = val; return 0; } > > We are talking about one or two code lines. If we will take some > > duplication search engine, it will find that major part of the kernel > > is matching against it. > > > > Newer the less, this driver provides a way to reset the counter. Why > > should we drop this functionality no advantage? > > > > > To that end, I'd like to see your cnt_isr() function removed for this > > > patchset (you can bring it back once timestamp support is added). > > It make no sense to request an interrupt without interrupt service > routine. > > https://elixir.bootlin.com/linux/latest/source/kernel/irq/manage.c#L2072if > if (!handler) { > if (!thread_fn) > return -EINVAL; > > As you can see, requesting an irq need at least handler or thread_fn. > > enable_irq: this will explode at least here: > https://elixir.bootlin.com/linux/latest/source/kernel/irq/manage.c#L778 > > If he have no IRQ handler and some how was able to enable it, at > some point this IRQ will be disabled by this code: > https://elixir.bootlin.com/linux/latest/source/kernel/irq/spurious.c#L410 > if (unlikely(desc->irqs_unhandled > 99900)) { > /* > * The interrupt is stuck > */ > __report_bad_irq(desc, action_ret); > /* > * Now kill the IRQ > */ > printk(KERN_EMERG "Disabling IRQ #%d\n", irq); > desc->istate |= IRQS_SPURIOUS_DISABLED; > desc->depth++; > irq_disable(desc); > > With current code, we can't request or enable IRQ without cnt_isr(). Not > that it is not possible, but it make no sense to me. What I'm requesting is to remove the interrupt code from this driver for now including the cnt_enable_write() callback. Yes, we will need it when timestamp functionality is added, but currently the Counter subsystem does not have that functionality yet. Once the Counter character device interface is merged, then it makes sense to add the interrupt service routine to push timestamps to the user. It is still useful to have this driver mainlined even without the interrupt code: getting the body of this driver merged means a much easier review of the timestamp code in the future, and users can start using current Counter sysfs interface to track their GPIO interrupts. > > Are you suggesting to enable IRQ without interrupt handler? May be i'm > > missing some thing.. I do not understand it. > > > > > Reimplement your cnt_read/cnt_write() functions to instead use > > > kstat_irqs_usr() from <linux/kernel_stat.h> to get the current number of > > > interrupts the IRQ line and use it to derive your count value for this > > > driver. > > irq descriptor has 3 counters: > - irq_count: this value can be reset any time by the kernel at least by > the note_interrupt() > - irqs_unhandled: this value is increased in case of missing irq > handler. Or if handler has returns IRQ_NONE. > - tot_count: this value should not be reset. > > Non of this values is suitable for cnt_read() and cnt_write(). Only > tot_count would be suitable if cnt_write() is removed. I do not see it > as acceptable option. > > For this driver, we still need extra counter, where only this driver is > responsible for writing to it. Yes, I'm sorry for not being clear before. Please use priv->count for this; there's no need to adjust directly the system irq count. > > I can follow the counter read way, but overwriting system wide counter > > for local use is bad idea. > > > > > > > > +static struct counter_signal event_cnt_signals[] = { > > > > > > + { > > > > > > + .id = 0, > > > > > > + .name = "Channel 0 signal", > > > > > > > > > > You should choose a more description name for this Signal; > > > > > "Channel 0 signal" isn't very useful information for the user. Is this > > > > > signal the respective GPIO line state? > > > > > > > > Sounds plausible. How about "Channel 0, GPIO line state"? > > > > > > Ideally, this would match the GPIO name (or I suppose the IRQ number if > > > not a GPIO line). So in your probe() function you can do something like > > > this I believe: > > > > > > cnt_signals[0].name = priv->gpio->name; > > > > > to make this possible, i would need hack gpiolib framework and add > > name/label exporter. But after endless rounds of pingponging me for > > renaming the driver and removing interrupt handler, i feel like we are > > not having serious discussion for mainlining this driver. > > Probably for good reason, struct gpio_desc was made local and is located > in the drivers/gpio/gpiolib.h. It feels like additional hack to include > it. I assume, it should be done properly so there is a function to > provide gpio name or label. > > @Linus Walleij are there any good way to get the GPIO name? And which > name will be actually used? A label provided over devicetree? Perhaps one of the GPIO subsystem maintainers can provide more guidance here, but I was under the impression that this name was provided statically by the respective GPIO driver via their struct gpio_chip. I think you can see the array of names via priv->gpio->gdev->chip->names. Alternatively, we can take a more generic approach: ignore the GPIO names and focus solely on the IRQ lines; because the GPIO lines will always be tied to respective IRQ lines here, using the IRQ as the basis of the name should always be valid. The "name" member of the struct irq_chip can work for this. I haven't tested this, but I think something like this would work: cnt_signals[0].name = irq_get_chip(priv->irq)->name; > If I see it correctly, it would need more work to make the kernel infrastructure > suitable for this suggestions. Some of them are only needed before > chardev support will go mainline and , in long term, not worth to > spend time on it. I disagree, I think there is benefit in getting this driver merged even if we don't have the interrupt service routine. Although I recommend we keep this initial patch simple to introduce the driver, later on you can for example add support for other Counter sysfs attributes such as "ceiling" and "floor" if users want to specify count limits, or perhaps alternative count functions (maybe a user wants to the count to decrease instead of increase with every interrupt). These other functionalities are tangental to the your timestamp interest for this driver, but I believe they will be useful to users at large as a convenient way to evaluate, track, and express the interrupt counts on their system. William Breathitt Gray > Probably I do not understand you and i missing some thing? > > Regards, > Oleksij > -- > Pengutronix e.K. | | > Steuerwalder Str. 21 | http://www.pengutronix.de/ | > 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | > Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Hello William, On Wed, Feb 24, 2021 at 11:34:06AM +0900, William Breathitt Gray wrote: > On Tue, Feb 23, 2021 at 06:45:16PM +0100, Oleksij Rempel wrote: > > Hello William, > > > > Here is cooled down technical answer. Excuse me for over reacting. > > Hello Oleksij, > > Let me apologize too if I offended you in some way in with previous > response, I assure you that was not my intention. I truly do believe > this is a useful driver to have in the kernel and I want to make that > happen; my concerns with your patch are purely technical in nature and > I'm certain we can find a solution working together. No problem :) > > On Tue, Feb 23, 2021 at 11:06:56AM +0100, Oleksij Rempel wrote: > > > On Mon, Feb 22, 2021 at 10:43:00AM +0900, William Breathitt Gray wrote: > > > > On Mon, Feb 15, 2021 at 10:17:37AM +0100, Oleksij Rempel wrote: > > > > > > > +static irqreturn_t event_cnt_isr(int irq, void *dev_id) > > > > > > > +{ > > > > > > > + struct event_cnt_priv *priv = dev_id; > > > > > > > + > > > > > > > + atomic_inc(&priv->count); > > > > > > > > > > > > This is just used to count the number of interrupts right? I wonder if > > > > > > we can do this smarter. For example, the kernel already keeps track of > > > > > > number of interrupts that has occurred for any particular IRQ line on a > > > > > > CPU (see the 'kstat_irqs' member of struct irq_desc, and the > > > > > > show_interrupts() function in kernel/irq/proc.c). Would it make sense to > > > > > > simply store the initial interrupt count on driver load or enablement, > > > > > > and then return the difference during a count_read() callback? > > > > > > > > > > This driver do not makes a lot of sense without your chardev patches. As > > > > > soon as this patches go mainline, this driver will be able to send > > > > > event with a timestamp and counter state to the user space. > > > > > > > > > > With other words, we will need an irq handler anyway. In this case we > > > > > can't save more RAM or CPU cycles by using system irq counters. > > > > > > > > It's true that this driver will need an IRQ handler when the timestamp > > > > functionality is added, but deriving the count value is different matter > > > > regardless. There's already code in the kernel to retrieve the number of > > > > interrupts, so it makes sense that we use that rather than rolling our > > > > own -- at the very least to ensure the value we provide to users is > > > > consistent with the ones already provided by other areas of the kernel. > > > > The value provided by the driver is consistent only if it is not > > overwritten by user. The driver provides an interface to reset/overwrite it. > > At least after this step the value is not consistent. > > I wasn't clear here so I apologize. What I would like is for this driver > to maintain its own local count value derived from kstat_irqs_usr(). So > for example, you can use the "count" member of your struct > interrupt_cnt_priv to maintain this value (it can be unsigned int > instead of atomic_t): > > static int interrupt_cnt_read(struct counter_device *counter, > struct counter_count *count, unsigned long *val) > { > struct interrupt_cnt_priv *priv = counter->priv; > > *val = kstat_irqs_usr(priv->irq) - priv->count; > > return 0; > } > > static int interrupt_cnt_write(struct counter_device *counter, > struct counter_count *count, > const unsigned long val) > { > struct interrupt_cnt_priv *priv = counter->priv; > > /* kstat_irqs_usr() returns unsigned int */ > if (val != (unsigned int)val) > return -ERANGE; > > priv->count = val; > > return 0; > } I understand this part. There is no need to spend extra CPU cycles if the interrupt was already counted. Just read it on user request and calculate the offset if needed. As soon as timestamp support is available, I will need to go back to local counter, because the kstat_irqs_usr() will take a lot more CPU cycles compared to private counter (it sums over all CPU local counters). So it's better to increment a single variable, then to call kstat_irqs_usr() from interrupt handler at IRQ rate several 10 thousands interrupts per second. > > > We are talking about one or two code lines. If we will take some > > > duplication search engine, it will find that major part of the kernel > > > is matching against it. > > > > > > Newer the less, this driver provides a way to reset the counter. Why > > > should we drop this functionality no advantage? > > > > > > > To that end, I'd like to see your cnt_isr() function removed for this > > > > patchset (you can bring it back once timestamp support is added). > > > > It make no sense to request an interrupt without interrupt service > > routine. > > > > https://elixir.bootlin.com/linux/latest/source/kernel/irq/manage.c#L2072if > > if (!handler) { > > if (!thread_fn) > > return -EINVAL; > > > > As you can see, requesting an irq need at least handler or thread_fn. > > > > enable_irq: this will explode at least here: > > https://elixir.bootlin.com/linux/latest/source/kernel/irq/manage.c#L778 > > > > If he have no IRQ handler and some how was able to enable it, at > > some point this IRQ will be disabled by this code: > > https://elixir.bootlin.com/linux/latest/source/kernel/irq/spurious.c#L410 > > if (unlikely(desc->irqs_unhandled > 99900)) { > > /* > > * The interrupt is stuck > > */ > > __report_bad_irq(desc, action_ret); > > /* > > * Now kill the IRQ > > */ > > printk(KERN_EMERG "Disabling IRQ #%d\n", irq); > > desc->istate |= IRQS_SPURIOUS_DISABLED; > > desc->depth++; > > irq_disable(desc); > > > > With current code, we can't request or enable IRQ without cnt_isr(). Not > > that it is not possible, but it make no sense to me. > > What I'm requesting is to remove the interrupt code from this driver for > now including the cnt_enable_write() callback. Yes, we will need it when > timestamp functionality is added, but currently the Counter subsystem > does not have that functionality yet. Once the Counter character device > interface is merged, then it makes sense to add the interrupt service > routine to push timestamps to the user. > > It is still useful to have this driver mainlined even without the > interrupt code: getting the body of this driver merged means a much > easier review of the timestamp code in the future, and users can start > using current Counter sysfs interface to track their GPIO interrupts. This driver, even without timestamping support, can't work without interrupt code. request_irq is the core functionality of it. The kernel interrupt infrastructure will not enable a IRQ and it will not provide stats without request_irq(). The minimal requirement to make it work is to call request_irq() with a handler which will return IRQ_HANDLED value. It makes no sense to mainline this driver without IRQ code, because it will not work. And if we need an IRQ handler anyway, and will need local count anyway, why should we go extra way around? :) > > > Are you suggesting to enable IRQ without interrupt handler? May be i'm > > > missing some thing.. I do not understand it. > > > > > > > Reimplement your cnt_read/cnt_write() functions to instead use > > > > kstat_irqs_usr() from <linux/kernel_stat.h> to get the current number of > > > > interrupts the IRQ line and use it to derive your count value for this > > > > driver. > > > > irq descriptor has 3 counters: > > - irq_count: this value can be reset any time by the kernel at least by > > the note_interrupt() > > - irqs_unhandled: this value is increased in case of missing irq > > handler. Or if handler has returns IRQ_NONE. > > - tot_count: this value should not be reset. > > > > Non of this values is suitable for cnt_read() and cnt_write(). Only > > tot_count would be suitable if cnt_write() is removed. I do not see it > > as acceptable option. > > > > For this driver, we still need extra counter, where only this driver is > > responsible for writing to it. > > Yes, I'm sorry for not being clear before. Please use priv->count for > this; there's no need to adjust directly the system irq count. > > > > I can follow the counter read way, but overwriting system wide counter > > > for local use is bad idea. > > > > > > > > > > +static struct counter_signal event_cnt_signals[] = { > > > > > > > + { > > > > > > > + .id = 0, > > > > > > > + .name = "Channel 0 signal", > > > > > > > > > > > > You should choose a more description name for this Signal; > > > > > > "Channel 0 signal" isn't very useful information for the user. Is this > > > > > > signal the respective GPIO line state? > > > > > > > > > > Sounds plausible. How about "Channel 0, GPIO line state"? > > > > > > > > Ideally, this would match the GPIO name (or I suppose the IRQ number if > > > > not a GPIO line). So in your probe() function you can do something like > > > > this I believe: > > > > > > > > cnt_signals[0].name = priv->gpio->name; > > > > > > > > to make this possible, i would need hack gpiolib framework and add > > > name/label exporter. But after endless rounds of pingponging me for > > > renaming the driver and removing interrupt handler, i feel like we are > > > not having serious discussion for mainlining this driver. > > > > Probably for good reason, struct gpio_desc was made local and is located > > in the drivers/gpio/gpiolib.h. It feels like additional hack to include > > it. I assume, it should be done properly so there is a function to > > provide gpio name or label. > > > > @Linus Walleij are there any good way to get the GPIO name? And which > > name will be actually used? A label provided over devicetree? > > Perhaps one of the GPIO subsystem maintainers can provide more guidance > here, but I was under the impression that this name was provided > statically by the respective GPIO driver via their struct gpio_chip. I > think you can see the array of names via priv->gpio->gdev->chip->names. > > Alternatively, we can take a more generic approach: ignore the GPIO > names and focus solely on the IRQ lines; because the GPIO lines will > always be tied to respective IRQ lines here, using the IRQ as the basis > of the name should always be valid. The "name" member of the struct > irq_chip can work for this. I haven't tested this, but I think something > like this would work: > > cnt_signals[0].name = irq_get_chip(priv->irq)->name; ok, i'll take a look at it. > > If I see it correctly, it would need more work to make the kernel infrastructure > > suitable for this suggestions. Some of them are only needed before > > chardev support will go mainline and , in long term, not worth to > > spend time on it. > > I disagree, I think there is benefit in getting this driver merged > even if we don't have the interrupt service routine. Although I > recommend we keep this initial patch simple to introduce the driver, > later on you can for example add support for other Counter sysfs > attributes such as "ceiling" and "floor" if users want to specify count > limits, or perhaps alternative count functions (maybe a user wants to > the count to decrease instead of increase with every interrupt). > > These other functionalities are tangental to the your timestamp interest > for this driver, but I believe they will be useful to users at large as > a convenient way to evaluate, track, and express the interrupt counts on > their system. > > William Breathitt Gray Regards, Oleksij -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
On Wed, Feb 24, 2021 at 08:35:06AM +0100, Oleksij Rempel wrote: > On Wed, Feb 24, 2021 at 11:34:06AM +0900, William Breathitt Gray wrote: > > On Tue, Feb 23, 2021 at 06:45:16PM +0100, Oleksij Rempel wrote: > > > On Tue, Feb 23, 2021 at 11:06:56AM +0100, Oleksij Rempel wrote: > > > > On Mon, Feb 22, 2021 at 10:43:00AM +0900, William Breathitt Gray wrote: > > > > > On Mon, Feb 15, 2021 at 10:17:37AM +0100, Oleksij Rempel wrote: > > > > > > > > +static irqreturn_t event_cnt_isr(int irq, void *dev_id) > > > > > > > > +{ > > > > > > > > + struct event_cnt_priv *priv = dev_id; > > > > > > > > + > > > > > > > > + atomic_inc(&priv->count); > > > > > > > > > > > > > > This is just used to count the number of interrupts right? I wonder if > > > > > > > we can do this smarter. For example, the kernel already keeps track of > > > > > > > number of interrupts that has occurred for any particular IRQ line on a > > > > > > > CPU (see the 'kstat_irqs' member of struct irq_desc, and the > > > > > > > show_interrupts() function in kernel/irq/proc.c). Would it make sense to > > > > > > > simply store the initial interrupt count on driver load or enablement, > > > > > > > and then return the difference during a count_read() callback? > > > > > > > > > > > > This driver do not makes a lot of sense without your chardev patches. As > > > > > > soon as this patches go mainline, this driver will be able to send > > > > > > event with a timestamp and counter state to the user space. > > > > > > > > > > > > With other words, we will need an irq handler anyway. In this case we > > > > > > can't save more RAM or CPU cycles by using system irq counters. > > > > > > > > > > It's true that this driver will need an IRQ handler when the timestamp > > > > > functionality is added, but deriving the count value is different matter > > > > > regardless. There's already code in the kernel to retrieve the number of > > > > > interrupts, so it makes sense that we use that rather than rolling our > > > > > own -- at the very least to ensure the value we provide to users is > > > > > consistent with the ones already provided by other areas of the kernel. > > > > > > The value provided by the driver is consistent only if it is not > > > overwritten by user. The driver provides an interface to reset/overwrite it. > > > At least after this step the value is not consistent. > > > > I wasn't clear here so I apologize. What I would like is for this driver > > to maintain its own local count value derived from kstat_irqs_usr(). So > > for example, you can use the "count" member of your struct > > interrupt_cnt_priv to maintain this value (it can be unsigned int > > instead of atomic_t): > > > > static int interrupt_cnt_read(struct counter_device *counter, > > struct counter_count *count, unsigned long *val) > > { > > struct interrupt_cnt_priv *priv = counter->priv; > > > > *val = kstat_irqs_usr(priv->irq) - priv->count; > > > > return 0; > > } > > > > static int interrupt_cnt_write(struct counter_device *counter, > > struct counter_count *count, > > const unsigned long val) > > { > > struct interrupt_cnt_priv *priv = counter->priv; > > > > /* kstat_irqs_usr() returns unsigned int */ > > if (val != (unsigned int)val) > > return -ERANGE; > > > > priv->count = val; > > > > return 0; > > } > > I understand this part. There is no need to spend extra CPU cycles if > the interrupt was already counted. Just read it on user request and > calculate the offset if needed. > > As soon as timestamp support is available, I will need to go back to > local counter, because the kstat_irqs_usr() will take a lot more CPU > cycles compared to private counter (it sums over all CPU local > counters). So it's better to increment a single variable, then to call > kstat_irqs_usr() from interrupt handler at IRQ rate several 10 thousands > interrupts per second. All right, I see what you mean. Let's keep your current implementation then. > > > > We are talking about one or two code lines. If we will take some > > > > duplication search engine, it will find that major part of the kernel > > > > is matching against it. > > > > > > > > Newer the less, this driver provides a way to reset the counter. Why > > > > should we drop this functionality no advantage? > > > > > > > > > To that end, I'd like to see your cnt_isr() function removed for this > > > > > patchset (you can bring it back once timestamp support is added). > > > > > > It make no sense to request an interrupt without interrupt service > > > routine. > > > > > > https://elixir.bootlin.com/linux/latest/source/kernel/irq/manage.c#L2072if > > > if (!handler) { > > > if (!thread_fn) > > > return -EINVAL; > > > > > > As you can see, requesting an irq need at least handler or thread_fn. > > > > > > enable_irq: this will explode at least here: > > > https://elixir.bootlin.com/linux/latest/source/kernel/irq/manage.c#L778 > > > > > > If he have no IRQ handler and some how was able to enable it, at > > > some point this IRQ will be disabled by this code: > > > https://elixir.bootlin.com/linux/latest/source/kernel/irq/spurious.c#L410 > > > if (unlikely(desc->irqs_unhandled > 99900)) { > > > /* > > > * The interrupt is stuck > > > */ > > > __report_bad_irq(desc, action_ret); > > > /* > > > * Now kill the IRQ > > > */ > > > printk(KERN_EMERG "Disabling IRQ #%d\n", irq); > > > desc->istate |= IRQS_SPURIOUS_DISABLED; > > > desc->depth++; > > > irq_disable(desc); > > > > > > With current code, we can't request or enable IRQ without cnt_isr(). Not > > > that it is not possible, but it make no sense to me. > > > > What I'm requesting is to remove the interrupt code from this driver for > > now including the cnt_enable_write() callback. Yes, we will need it when > > timestamp functionality is added, but currently the Counter subsystem > > does not have that functionality yet. Once the Counter character device > > interface is merged, then it makes sense to add the interrupt service > > routine to push timestamps to the user. > > > > It is still useful to have this driver mainlined even without the > > interrupt code: getting the body of this driver merged means a much > > easier review of the timestamp code in the future, and users can start > > using current Counter sysfs interface to track their GPIO interrupts. > > This driver, even without timestamping support, can't work without > interrupt code. request_irq is the core functionality of it. The kernel > interrupt infrastructure will not enable a IRQ and it will not provide > stats without request_irq(). > > The minimal requirement to make it work is to call request_irq() with > a handler which will return IRQ_HANDLED value. > > It makes no sense to mainline this driver without IRQ code, because it > will not work. And if we need an IRQ handler anyway, and will need local > count anyway, why should we go extra way around? :) Ack. > > > > Are you suggesting to enable IRQ without interrupt handler? May be i'm > > > > missing some thing.. I do not understand it. > > > > > > > > > Reimplement your cnt_read/cnt_write() functions to instead use > > > > > kstat_irqs_usr() from <linux/kernel_stat.h> to get the current number of > > > > > interrupts the IRQ line and use it to derive your count value for this > > > > > driver. > > > > > > irq descriptor has 3 counters: > > > - irq_count: this value can be reset any time by the kernel at least by > > > the note_interrupt() > > > - irqs_unhandled: this value is increased in case of missing irq > > > handler. Or if handler has returns IRQ_NONE. > > > - tot_count: this value should not be reset. > > > > > > Non of this values is suitable for cnt_read() and cnt_write(). Only > > > tot_count would be suitable if cnt_write() is removed. I do not see it > > > as acceptable option. > > > > > > For this driver, we still need extra counter, where only this driver is > > > responsible for writing to it. > > > > Yes, I'm sorry for not being clear before. Please use priv->count for > > this; there's no need to adjust directly the system irq count. > > > > > > I can follow the counter read way, but overwriting system wide counter > > > > for local use is bad idea. > > > > > > > > > > > > +static struct counter_signal event_cnt_signals[] = { > > > > > > > > + { > > > > > > > > + .id = 0, > > > > > > > > + .name = "Channel 0 signal", > > > > > > > > > > > > > > You should choose a more description name for this Signal; > > > > > > > "Channel 0 signal" isn't very useful information for the user. Is this > > > > > > > signal the respective GPIO line state? > > > > > > > > > > > > Sounds plausible. How about "Channel 0, GPIO line state"? > > > > > > > > > > Ideally, this would match the GPIO name (or I suppose the IRQ number if > > > > > not a GPIO line). So in your probe() function you can do something like > > > > > this I believe: > > > > > > > > > > cnt_signals[0].name = priv->gpio->name; > > > > > > > > > > > to make this possible, i would need hack gpiolib framework and add > > > > name/label exporter. But after endless rounds of pingponging me for > > > > renaming the driver and removing interrupt handler, i feel like we are > > > > not having serious discussion for mainlining this driver. > > > > > > Probably for good reason, struct gpio_desc was made local and is located > > > in the drivers/gpio/gpiolib.h. It feels like additional hack to include > > > it. I assume, it should be done properly so there is a function to > > > provide gpio name or label. > > > > > > @Linus Walleij are there any good way to get the GPIO name? And which > > > name will be actually used? A label provided over devicetree? > > > > Perhaps one of the GPIO subsystem maintainers can provide more guidance > > here, but I was under the impression that this name was provided > > statically by the respective GPIO driver via their struct gpio_chip. I > > think you can see the array of names via priv->gpio->gdev->chip->names. > > > > Alternatively, we can take a more generic approach: ignore the GPIO > > names and focus solely on the IRQ lines; because the GPIO lines will > > always be tied to respective IRQ lines here, using the IRQ as the basis > > of the name should always be valid. The "name" member of the struct > > irq_chip can work for this. I haven't tested this, but I think something > > like this would work: > > > > cnt_signals[0].name = irq_get_chip(priv->irq)->name; > > ok, i'll take a look at it. If that doesn't work, then use devm_kasprintf() to generate the name based on the IRQ line number. The idea here is that the user should be able to identify that the Signal component for this Count is the respective IRQ. William Breathitt Gray
On Wed, Feb 24, 2021 at 05:11:03PM +0900, William Breathitt Gray wrote: > On Wed, Feb 24, 2021 at 08:35:06AM +0100, Oleksij Rempel wrote: > > On Wed, Feb 24, 2021 at 11:34:06AM +0900, William Breathitt Gray wrote: > > > Alternatively, we can take a more generic approach: ignore the GPIO > > > names and focus solely on the IRQ lines; because the GPIO lines will > > > always be tied to respective IRQ lines here, using the IRQ as the basis > > > of the name should always be valid. The "name" member of the struct > > > irq_chip can work for this. I haven't tested this, but I think something > > > like this would work: > > > > > > cnt_signals[0].name = irq_get_chip(priv->irq)->name; > > > > ok, i'll take a look at it. > > If that doesn't work, then use devm_kasprintf() to generate the name > based on the IRQ line number. The idea here is that the user should be > able to identify that the Signal component for this Count is the > respective IRQ. > > William Breathitt Gray I realized that these irq_chip names are often just the device name which isn't very useful either. :-( In that case, I suppose we really are just left with generating the name based on the IRQ line number then. This should be fine then: cnt_signals[0].name = devm_kasprintf(dev, GFP_KERNEL, "IRQ %d", priv->irq); if (!cnt_signals[0].name) return -ENOMEM; I think this would make it clear to the user that this Signal is the respective IRQ (whether sourced from GPIO or not). William Breathitt Gray
On Wed, Feb 24, 2021 at 05:20:21PM +0900, William Breathitt Gray wrote: > On Wed, Feb 24, 2021 at 05:11:03PM +0900, William Breathitt Gray wrote: > > On Wed, Feb 24, 2021 at 08:35:06AM +0100, Oleksij Rempel wrote: > > > On Wed, Feb 24, 2021 at 11:34:06AM +0900, William Breathitt Gray wrote: > > > > Alternatively, we can take a more generic approach: ignore the GPIO > > > > names and focus solely on the IRQ lines; because the GPIO lines will > > > > always be tied to respective IRQ lines here, using the IRQ as the basis > > > > of the name should always be valid. The "name" member of the struct > > > > irq_chip can work for this. I haven't tested this, but I think something > > > > like this would work: > > > > > > > > cnt_signals[0].name = irq_get_chip(priv->irq)->name; > > > > > > ok, i'll take a look at it. > > > > If that doesn't work, then use devm_kasprintf() to generate the name > > based on the IRQ line number. The idea here is that the user should be > > able to identify that the Signal component for this Count is the > > respective IRQ. > > > > William Breathitt Gray > > I realized that these irq_chip names are often just the device name > which isn't very useful either. :-( > > In that case, I suppose we really are just left with generating the name > based on the IRQ line number then. This should be fine then: > > cnt_signals[0].name = devm_kasprintf(dev, GFP_KERNEL, "IRQ %d", > priv->irq); > if (!cnt_signals[0].name) > return -ENOMEM; > > I think this would make it clear to the user that this Signal is the > respective IRQ (whether sourced from GPIO or not). ack, with one correction. cnt_signals should be allocated, otherwise this value will be set per driver not per device. Regards, Oleksij -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
On Fri, Feb 26, 2021 at 07:46:01AM +0100, Oleksij Rempel wrote: > On Wed, Feb 24, 2021 at 05:20:21PM +0900, William Breathitt Gray wrote: > > On Wed, Feb 24, 2021 at 05:11:03PM +0900, William Breathitt Gray wrote: > > > On Wed, Feb 24, 2021 at 08:35:06AM +0100, Oleksij Rempel wrote: > > > > On Wed, Feb 24, 2021 at 11:34:06AM +0900, William Breathitt Gray wrote: > > > > > Alternatively, we can take a more generic approach: ignore the GPIO > > > > > names and focus solely on the IRQ lines; because the GPIO lines will > > > > > always be tied to respective IRQ lines here, using the IRQ as the basis > > > > > of the name should always be valid. The "name" member of the struct > > > > > irq_chip can work for this. I haven't tested this, but I think something > > > > > like this would work: > > > > > > > > > > cnt_signals[0].name = irq_get_chip(priv->irq)->name; > > > > > > > > ok, i'll take a look at it. > > > > > > If that doesn't work, then use devm_kasprintf() to generate the name > > > based on the IRQ line number. The idea here is that the user should be > > > able to identify that the Signal component for this Count is the > > > respective IRQ. > > > > > > William Breathitt Gray > > > > I realized that these irq_chip names are often just the device name > > which isn't very useful either. :-( > > > > In that case, I suppose we really are just left with generating the name > > based on the IRQ line number then. This should be fine then: > > > > cnt_signals[0].name = devm_kasprintf(dev, GFP_KERNEL, "IRQ %d", > > priv->irq); > > if (!cnt_signals[0].name) > > return -ENOMEM; > > > > I think this would make it clear to the user that this Signal is the > > respective IRQ (whether sourced from GPIO or not). > > ack, with one correction. cnt_signals should be allocated, otherwise > this value will be set per driver not per device. > > Regards, > Oleksij > -- > Pengutronix e.K. | | > Steuerwalder Str. 21 | http://www.pengutronix.de/ | > 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | > Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | Yes you're right, cnt_signals will need to be allocated then because these will be different per device. Thanks, William Breathitt Gray
On Wed, Feb 24, 2021 at 3:34 AM William Breathitt Gray <vilhelm.gray@gmail.com> wrote: > On Tue, Feb 23, 2021 at 06:45:16PM +0100, Oleksij Rempel wrote: > > > to make this possible, i would need hack gpiolib framework and add > > > name/label exporter. But after endless rounds of pingponging me for > > > renaming the driver and removing interrupt handler, i feel like we are > > > not having serious discussion for mainlining this driver. > > > > Probably for good reason, struct gpio_desc was made local and is located > > in the drivers/gpio/gpiolib.h. It feels like additional hack to include > > it. I assume, it should be done properly so there is a function to > > provide gpio name or label. > > > > @Linus Walleij are there any good way to get the GPIO name? And which > > name will be actually used? A label provided over devicetree? > > Perhaps one of the GPIO subsystem maintainers can provide more guidance > here, but I was under the impression that this name was provided > statically by the respective GPIO driver via their struct gpio_chip. I > think you can see the array of names via priv->gpio->gdev->chip->names. These names can be set either through device properties on the GPIO chip "line-names" such as through device tree, or as static names in the .names array on struct gpio_chip for chips that are e.g. hot-pluggable and does not have a hardware description associated. These names should be something like what the signal is called on the circuit board rail. gpiolib further has a function: gpiod_set_consumer_name() that can be used by consumers to set their use case for the line, which makes it appear in debugfs etc. The consumer name does not need to be unique. These names have no practical use other than debugging or userspace representation. I hope this helps. Yours, Linus Walleij
diff --git a/drivers/counter/Kconfig b/drivers/counter/Kconfig index 2de53ab0dd25..3284987e070a 100644 --- a/drivers/counter/Kconfig +++ b/drivers/counter/Kconfig @@ -29,6 +29,16 @@ config 104_QUAD_8 The base port addresses for the devices may be configured via the base array module parameter. +config EVENT_CNT + tristate "Event counter driver" + depends on GPIOLIB + help + Select this option to enable event counter driver. Any interrupt source + can be used by this driver as the event source. + + To compile this driver as a module, choose M here: the + module will be called gpio-pulse-cnt. + config STM32_TIMER_CNT tristate "STM32 Timer encoder counter driver" depends on MFD_STM32_TIMERS || COMPILE_TEST diff --git a/drivers/counter/Makefile b/drivers/counter/Makefile index 0a393f71e481..6626900468f6 100644 --- a/drivers/counter/Makefile +++ b/drivers/counter/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_COUNTER) += counter.o obj-$(CONFIG_104_QUAD_8) += 104-quad-8.o +obj-$(CONFIG_EVENT_CNT) += event-cnt.o obj-$(CONFIG_STM32_TIMER_CNT) += stm32-timer-cnt.o obj-$(CONFIG_STM32_LPTIMER_CNT) += stm32-lptimer-cnt.o obj-$(CONFIG_TI_EQEP) += ti-eqep.o diff --git a/drivers/counter/event-cnt.c b/drivers/counter/event-cnt.c new file mode 100644 index 000000000000..a394fe72c4e4 --- /dev/null +++ b/drivers/counter/event-cnt.c @@ -0,0 +1,250 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2021 Pengutronix, Oleksij Rempel <kernel@pengutronix.de> + */ + +#include <linux/counter.h> +#include <linux/gpio/consumer.h> +#include <linux/interrupt.h> +#include <linux/irq.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/platform_device.h> + +#define EVENT_CNT_NAME "event-cnt" + +struct event_cnt_priv { + struct counter_device counter; + struct counter_ops ops; + struct gpio_desc *gpio; + int irq; + bool enabled; + atomic_t count; +}; + +static irqreturn_t event_cnt_isr(int irq, void *dev_id) +{ + struct event_cnt_priv *priv = dev_id; + + atomic_inc(&priv->count); + + return IRQ_HANDLED; +} + +static ssize_t event_cnt_enable_read(struct counter_device *counter, + struct counter_count *count, void *private, + char *buf) +{ + struct event_cnt_priv *priv = counter->priv; + + return sysfs_emit(buf, "%d\n", priv->enabled); +} + +static ssize_t event_cnt_enable_write(struct counter_device *counter, + struct counter_count *count, + void *private, const char *buf, + size_t len) +{ + struct event_cnt_priv *priv = counter->priv; + bool enable; + ssize_t ret; + + ret = kstrtobool(buf, &enable); + if (ret) + return ret; + + if (priv->enabled == enable) + return len; + + if (enable) { + priv->enabled = enable; + enable_irq(priv->irq); + } else { + disable_irq(priv->irq); + priv->enabled = enable; + } + + return len; +} + +static const struct counter_count_ext event_cnt_ext[] = { + { + .name = "enable", + .read = event_cnt_enable_read, + .write = event_cnt_enable_write, + }, +}; + +static enum counter_synapse_action event_cnt_synapse_actionss[] = { + COUNTER_SYNAPSE_ACTION_RISING_EDGE, +}; + +static int event_cnt_action_get(struct counter_device *counter, + struct counter_count *count, + struct counter_synapse *synapse, + size_t *action) +{ + *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE; + + return 0; +} + +static int event_cnt_read(struct counter_device *counter, + struct counter_count *count, + unsigned long *val) +{ + struct event_cnt_priv *priv = counter->priv; + + *val = atomic_read(&priv->count); + + return 0; +} + +static int event_cnt_write(struct counter_device *counter, + struct counter_count *count, + const unsigned long val) +{ + struct event_cnt_priv *priv = counter->priv; + + atomic_set(&priv->count, val); + + return 0; +} + +static int event_cnt_function_get(struct counter_device *counter, + struct counter_count *count, size_t *function) +{ + *function = COUNTER_COUNT_FUNCTION_INCREASE; + + return 0; +} + +static int event_cnt_signal_read(struct counter_device *counter, + struct counter_signal *signal, + enum counter_signal_value *val) +{ + struct event_cnt_priv *priv = counter->priv; + int ret; + + ret = gpiod_get_value(priv->gpio); + if (ret < 0) + return ret; + + *val = ret ? COUNTER_SIGNAL_HIGH : COUNTER_SIGNAL_LOW; + + return 0; +} + +static struct counter_signal event_cnt_signals[] = { + { + .id = 0, + .name = "Channel 0 signal", + }, +}; + +static struct counter_synapse event_cnt_synapses[] = { + { + .actions_list = event_cnt_synapse_actionss, + .num_actions = ARRAY_SIZE(event_cnt_synapse_actionss), + .signal = &event_cnt_signals[0] + }, +}; + +static enum counter_count_function event_cnt_functions[] = { + COUNTER_COUNT_FUNCTION_INCREASE, +}; + +static struct counter_count event_cnts[] = { + { + .id = 0, + .name = "Channel 1 Count", + .functions_list = event_cnt_functions, + .num_functions = ARRAY_SIZE(event_cnt_functions), + .synapses = event_cnt_synapses, + .num_synapses = ARRAY_SIZE(event_cnt_synapses), + .ext = event_cnt_ext, + .num_ext = ARRAY_SIZE(event_cnt_ext), + }, +}; + +static int event_cnt_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct event_cnt_priv *priv; + int ret; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->irq = platform_get_irq_optional(pdev, 0); + if (priv->irq == -ENXIO) + priv->irq = 0; + else if (priv->irq < 0) + return dev_err_probe(dev, priv->irq, "failed to get IRQ\n"); + + priv->gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_IN); + if (IS_ERR(priv->gpio)) + return dev_err_probe(dev, PTR_ERR(priv->gpio), "failed to get GPIO\n"); + + if (!priv->irq && !priv->gpio) { + dev_err(dev, "IRQ and GPIO are not found. At least one source should be provided\n"); + return -ENODEV; + } + + if (!priv->irq) { + int irq = gpiod_to_irq(priv->gpio); + + if (irq < 0) + return dev_err_probe(dev, irq, "failed to get IRQ from GPIO\n"); + + priv->irq = irq; + } + + priv->ops.action_get = event_cnt_action_get; + priv->ops.count_read = event_cnt_read; + priv->ops.count_write = event_cnt_write; + priv->ops.function_get = event_cnt_function_get; + if (priv->gpio) + priv->ops.signal_read = event_cnt_signal_read; + + priv->counter.name = dev_name(dev); + priv->counter.parent = dev; + priv->counter.ops = &priv->ops; + priv->counter.counts = event_cnts; + priv->counter.num_counts = ARRAY_SIZE(event_cnts); + priv->counter.signals = event_cnt_signals; + priv->counter.num_signals = ARRAY_SIZE(event_cnt_signals); + priv->counter.priv = priv; + + irq_set_status_flags(priv->irq, IRQ_NOAUTOEN); + ret = devm_request_irq(dev, priv->irq, event_cnt_isr, + IRQF_TRIGGER_RISING | IRQF_NO_THREAD, + EVENT_CNT_NAME, priv); + if (ret) + return ret; + + platform_set_drvdata(pdev, priv); + + return devm_counter_register(dev, &priv->counter); +} + +static const struct of_device_id event_cnt_of_match[] = { + { .compatible = "event-counter", }, + {} +}; +MODULE_DEVICE_TABLE(of, event_cnt_of_match); + +static struct platform_driver event_cnt_driver = { + .probe = event_cnt_probe, + .driver = { + .name = EVENT_CNT_NAME, + .of_match_table = event_cnt_of_match, + }, +}; +module_platform_driver(event_cnt_driver); + +MODULE_ALIAS("platform:event-counter"); +MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>"); +MODULE_DESCRIPTION("Event counter driver"); +MODULE_LICENSE("GPL v2");