]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
counter: microchip-tcb-capture: Add IRQ handling
authorBence Csókás <csokas.bence@prolan.hu>
Thu, 6 Mar 2025 13:44:36 +0000 (14:44 +0100)
committerWilliam Breathitt Gray <wbg@kernel.org>
Fri, 7 Mar 2025 23:57:02 +0000 (08:57 +0900)
Add interrupt servicing to allow userspace to wait for the following:
* Change-of-state caused by external trigger
* Capture of timer value into RA/RB
* Compare to RC register
* Overflow

Signed-off-by: Bence Csókás <csokas.bence@prolan.hu>
Link: https://lore.kernel.org/r/20250306134441.582819-2-csokas.bence@prolan.hu
Signed-off-by: William Breathitt Gray <wbg@kernel.org>
MAINTAINERS
drivers/counter/microchip-tcb-capture.c
include/uapi/linux/counter/microchip-tcb-capture.h [new file with mode: 0644]

index 896a307fa06545e2861abe46ea7029f9b4d3628e..c7aa35994e1485d002359c63e8b9967ce680d5da 100644 (file)
@@ -15642,6 +15642,7 @@ L:      linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
 L:     linux-iio@vger.kernel.org
 S:     Maintained
 F:     drivers/counter/microchip-tcb-capture.c
+F:     include/uapi/linux/counter/microchip-tcb-capture.h
 
 MICROCHIP USB251XB DRIVER
 M:     Richard Leitner <richard.leitner@skidata.com>
index 2f096a5b973d18edf5de5a2b33f2f72571deefb7..16707099bd69c7d6ada2bfa830ce4edc968b7b5b 100644 (file)
@@ -6,18 +6,24 @@
  */
 #include <linux/clk.h>
 #include <linux/counter.h>
+#include <linux/interrupt.h>
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/of.h>
+#include <linux/of_irq.h>
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
+#include <uapi/linux/counter/microchip-tcb-capture.h>
 #include <soc/at91/atmel_tcb.h>
 
 #define ATMEL_TC_CMR_MASK      (ATMEL_TC_LDRA_RISING | ATMEL_TC_LDRB_FALLING | \
                                 ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_LDBDIS | \
                                 ATMEL_TC_LDBSTOP)
 
+#define ATMEL_TC_DEF_IRQS      (ATMEL_TC_ETRGS | ATMEL_TC_COVFS | \
+                                ATMEL_TC_LDRAS | ATMEL_TC_LDRBS | ATMEL_TC_CPCS)
+
 #define ATMEL_TC_QDEN                  BIT(8)
 #define ATMEL_TC_POSEN                 BIT(9)
 
@@ -294,6 +300,65 @@ static const struct of_device_id atmel_tc_of_match[] = {
        { /* sentinel */ }
 };
 
+static irqreturn_t mchp_tc_isr(int irq, void *dev_id)
+{
+       struct counter_device *const counter = dev_id;
+       struct mchp_tc_data *const priv = counter_priv(counter);
+       u32 sr, mask;
+
+       regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
+       regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], IMR), &mask);
+
+       sr &= mask;
+       if (!(sr & ATMEL_TC_ALL_IRQ))
+               return IRQ_NONE;
+
+       if (sr & ATMEL_TC_ETRGS)
+               counter_push_event(counter, COUNTER_EVENT_CHANGE_OF_STATE,
+                                  COUNTER_MCHP_EVCHN_CV);
+       if (sr & ATMEL_TC_LDRAS)
+               counter_push_event(counter, COUNTER_EVENT_CAPTURE,
+                                  COUNTER_MCHP_EVCHN_RA);
+       if (sr & ATMEL_TC_LDRBS)
+               counter_push_event(counter, COUNTER_EVENT_CAPTURE,
+                                  COUNTER_MCHP_EVCHN_RB);
+       if (sr & ATMEL_TC_CPCS)
+               counter_push_event(counter, COUNTER_EVENT_THRESHOLD,
+                                  COUNTER_MCHP_EVCHN_RC);
+       if (sr & ATMEL_TC_COVFS)
+               counter_push_event(counter, COUNTER_EVENT_OVERFLOW,
+                                  COUNTER_MCHP_EVCHN_CV);
+
+       return IRQ_HANDLED;
+}
+
+static void mchp_tc_irq_remove(void *ptr)
+{
+       struct mchp_tc_data *priv = ptr;
+
+       regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], IDR), ATMEL_TC_DEF_IRQS);
+}
+
+static int mchp_tc_irq_enable(struct counter_device *const counter, int irq)
+{
+       struct mchp_tc_data *const priv = counter_priv(counter);
+       int ret = devm_request_irq(counter->parent, irq, mchp_tc_isr, 0,
+                                  dev_name(counter->parent), counter);
+
+       if (ret < 0)
+               return ret;
+
+       ret = regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], IER), ATMEL_TC_DEF_IRQS);
+       if (ret < 0)
+               return ret;
+
+       ret = devm_add_action_or_reset(counter->parent, mchp_tc_irq_remove, priv);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
 static void mchp_tc_clk_remove(void *ptr)
 {
        clk_disable_unprepare((struct clk *)ptr);
@@ -378,6 +443,15 @@ static int mchp_tc_probe(struct platform_device *pdev)
        counter->num_signals = ARRAY_SIZE(mchp_tc_count_signals);
        counter->signals = mchp_tc_count_signals;
 
+       i = of_irq_get(np->parent, 0);
+       if (i == -EPROBE_DEFER)
+               return -EPROBE_DEFER;
+       if (i > 0) {
+               ret = mchp_tc_irq_enable(counter, i);
+               if (ret < 0)
+                       return dev_err_probe(&pdev->dev, ret, "Failed to set up IRQ");
+       }
+
        ret = devm_counter_add(&pdev->dev, counter);
        if (ret < 0)
                return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
diff --git a/include/uapi/linux/counter/microchip-tcb-capture.h b/include/uapi/linux/counter/microchip-tcb-capture.h
new file mode 100644 (file)
index 0000000..f3ef315
--- /dev/null
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Channel numbers used by the microchip-tcb-capture driver
+ * Copyright (C) 2025 Bence Csókás
+ */
+#ifndef _UAPI_COUNTER_MCHP_TCB_H_
+#define _UAPI_COUNTER_MCHP_TCB_H_
+
+/*
+ * The driver defines the following components:
+ *
+ * Count 0
+ * \__  Synapse 0 -- Signal 0 (Channel A, i.e. TIOA)
+ * \__  Synapse 1 -- Signal 1 (Channel B, i.e. TIOB)
+ *
+ * It also supports the following events:
+ *
+ * Channel 0:
+ * - CV register changed
+ * - CV overflowed
+ * - RA captured
+ * Channel 1:
+ * - RB captured
+ * Channel 2:
+ * - RC compare triggered
+ */
+
+/* Event channels */
+#define COUNTER_MCHP_EVCHN_CV 0
+#define COUNTER_MCHP_EVCHN_RA 0
+#define COUNTER_MCHP_EVCHN_RB 1
+#define COUNTER_MCHP_EVCHN_RC 2
+
+#endif /* _UAPI_COUNTER_MCHP_TCB_H_ */