* [PATCH v7 0/2] microchip-tcb-capture: Add Capture, Compare, Overflow etc. events
@ 2025-03-04 15:51 Bence Csókás
2025-03-04 15:51 ` [PATCH v7 1/2] counter: microchip-tcb-capture: Add IRQ handling Bence Csókás
2025-03-04 15:51 ` [PATCH v7 2/2] counter: microchip-tcb-capture: Add capture extensions for registers RA/RB Bence Csókás
0 siblings, 2 replies; 5+ messages in thread
From: Bence Csókás @ 2025-03-04 15:51 UTC (permalink / raw)
To: linux-arm-kernel, linux-iio, linux-kernel
Cc: Bence Csókás, Kamel Bouhara, William Breathitt Gray,
Dharma.B
The TCB has three R/W-able "general purpose" hardware registers:
RA, RB and RC. The hardware is capable of:
* sampling Counter Value Register (CV) to RA/RB on a trigger edge
* sending an interrupt of this change
* sending an interrupt on CV change due to trigger
* triggering an interrupt on CV compare to RC
* stop counting after sampling to RB
To enable using these features in user-space, an interrupt handler
was added, generating the necessary counter events. On top, RA/RB
registers are added as Count Extensions. To aid interoperation, a
uapi header was also added, containing the various numeral IDs of
the Extensions, Event channels etc.
Bence Csókás (2):
counter: microchip-tcb-capture: Add IRQ handling
counter: microchip-tcb-capture: Add capture extensions for registers
RA/RB
MAINTAINERS | 1 +
drivers/counter/microchip-tcb-capture.c | 133 ++++++++++++++++++
.../linux/counter/microchip-tcb-capture.h | 40 ++++++
3 files changed, 174 insertions(+)
create mode 100644 include/uapi/linux/counter/microchip-tcb-capture.h
--
2.48.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v7 1/2] counter: microchip-tcb-capture: Add IRQ handling
2025-03-04 15:51 [PATCH v7 0/2] microchip-tcb-capture: Add Capture, Compare, Overflow etc. events Bence Csókás
@ 2025-03-04 15:51 ` Bence Csókás
2025-03-06 5:49 ` William Breathitt Gray
2025-03-04 15:51 ` [PATCH v7 2/2] counter: microchip-tcb-capture: Add capture extensions for registers RA/RB Bence Csókás
1 sibling, 1 reply; 5+ messages in thread
From: Bence Csókás @ 2025-03-04 15:51 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, linux-iio
Cc: Bence Csókás, Kamel Bouhara, William Breathitt Gray
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>
---
Notes:
New in v2
Changes in v3:
* Add IRQs for Capture events (from next patch)
* Add IRQ for RC Compare
* Add events as bullet points to commit msg
Changes in v4:
* Add uapi header, names for the event channels
* Add check for -EPROBE_DEFER from `of_irq_get()`
Changes in v5:
* Split out UAPI header introduction
Changes in v7:
* Revert split-out
* Drop `enum counter_mchp_signals` from UAPI
* Change enums to #define's in UAPI header
MAINTAINERS | 1 +
drivers/counter/microchip-tcb-capture.c | 75 +++++++++++++++++++
.../linux/counter/microchip-tcb-capture.h | 34 +++++++++
3 files changed, 110 insertions(+)
create mode 100644 include/uapi/linux/counter/microchip-tcb-capture.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 8e047e20fbd8..d1d264210690 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15579,6 +15579,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>
diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
index 2f096a5b973d..cc12c2e2113a 100644
--- a/drivers/counter/microchip-tcb-capture.c
+++ b/drivers/counter/microchip-tcb-capture.c
@@ -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)
@@ -27,6 +33,7 @@ struct mchp_tc_data {
int qdec_mode;
int num_channels;
int channel[2];
+ int irq;
};
static const enum counter_function mchp_tc_count_functions[] = {
@@ -294,6 +301,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)
+{
+ struct mchp_tc_data *const priv = counter_priv(counter);
+ int ret = devm_request_irq(counter->parent, priv->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 +444,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;
+ priv->irq = of_irq_get(np->parent, 0);
+ if (priv->irq == -EPROBE_DEFER)
+ return -EPROBE_DEFER;
+ if (priv->irq > 0) {
+ ret = mchp_tc_irq_enable(counter);
+ 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
index 000000000000..f3ef315fe9f6
--- /dev/null
+++ b/include/uapi/linux/counter/microchip-tcb-capture.h
@@ -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_ */
--
2.48.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v7 2/2] counter: microchip-tcb-capture: Add capture extensions for registers RA/RB
2025-03-04 15:51 [PATCH v7 0/2] microchip-tcb-capture: Add Capture, Compare, Overflow etc. events Bence Csókás
2025-03-04 15:51 ` [PATCH v7 1/2] counter: microchip-tcb-capture: Add IRQ handling Bence Csókás
@ 2025-03-04 15:51 ` Bence Csókás
2025-03-06 5:51 ` William Breathitt Gray
1 sibling, 1 reply; 5+ messages in thread
From: Bence Csókás @ 2025-03-04 15:51 UTC (permalink / raw)
To: linux-arm-kernel, linux-iio, linux-kernel
Cc: Bence Csókás, Kamel Bouhara, William Breathitt Gray
TCB hardware is capable of capturing the timer value to registers RA and
RB. Add these registers as capture extensions.
Signed-off-by: Bence Csókás <csokas.bence@prolan.hu>
---
Notes:
Changes in v2:
* Add IRQs
Changes in v3:
* Move IRQs to previous patch
Changes in v4:
* Return the status of the regmap_*() operations
* Add names for the extension numbers
Changes in v6:
* Remove RC, as it is not a capture register
Changes in v7:
* Change enums to #define's in UAPI header
* Return early on error in `mchp_tc_count_cap_read()`
drivers/counter/microchip-tcb-capture.c | 58 +++++++++++++++++++
.../linux/counter/microchip-tcb-capture.h | 6 ++
2 files changed, 64 insertions(+)
diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
index cc12c2e2113a..52493f7e8b57 100644
--- a/drivers/counter/microchip-tcb-capture.c
+++ b/drivers/counter/microchip-tcb-capture.c
@@ -254,6 +254,62 @@ static int mchp_tc_count_read(struct counter_device *counter,
return 0;
}
+static int mchp_tc_count_cap_read(struct counter_device *counter,
+ struct counter_count *count, size_t idx, u64 *val)
+{
+ struct mchp_tc_data *const priv = counter_priv(counter);
+ u32 cnt;
+ int ret;
+
+ switch (idx) {
+ case COUNTER_MCHP_EXCAP_RA:
+ ret = regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], RA), &cnt);
+ break;
+ case COUNTER_MCHP_EXCAP_RB:
+ ret = regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], RB), &cnt);
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ if (ret < 0)
+ return ret;
+
+ *val = cnt;
+
+ return 0;
+}
+
+static int mchp_tc_count_cap_write(struct counter_device *counter,
+ struct counter_count *count, size_t idx, u64 val)
+{
+ struct mchp_tc_data *const priv = counter_priv(counter);
+ int ret;
+
+ if (val > U32_MAX)
+ return -ERANGE;
+
+ switch (idx) {
+ case COUNTER_MCHP_EXCAP_RA:
+ ret = regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], RA), val);
+ break;
+ case COUNTER_MCHP_EXCAP_RB:
+ ret = regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], RB), val);
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return ret;
+}
+
+static DEFINE_COUNTER_ARRAY_CAPTURE(mchp_tc_cnt_cap_array, 2);
+
+static struct counter_comp mchp_tc_count_ext[] = {
+ COUNTER_COMP_ARRAY_CAPTURE(mchp_tc_count_cap_read, mchp_tc_count_cap_write,
+ mchp_tc_cnt_cap_array),
+};
+
static struct counter_count mchp_tc_counts[] = {
{
.id = 0,
@@ -262,6 +318,8 @@ static struct counter_count mchp_tc_counts[] = {
.num_functions = ARRAY_SIZE(mchp_tc_count_functions),
.synapses = mchp_tc_count_synapses,
.num_synapses = ARRAY_SIZE(mchp_tc_count_synapses),
+ .ext = mchp_tc_count_ext,
+ .num_ext = ARRAY_SIZE(mchp_tc_count_ext),
},
};
diff --git a/include/uapi/linux/counter/microchip-tcb-capture.h b/include/uapi/linux/counter/microchip-tcb-capture.h
index f3ef315fe9f6..136e2faa7730 100644
--- a/include/uapi/linux/counter/microchip-tcb-capture.h
+++ b/include/uapi/linux/counter/microchip-tcb-capture.h
@@ -12,6 +12,8 @@
* Count 0
* \__ Synapse 0 -- Signal 0 (Channel A, i.e. TIOA)
* \__ Synapse 1 -- Signal 1 (Channel B, i.e. TIOB)
+ * \__ Extension capture0 (RA register)
+ * \__ Extension capture1 (RB register)
*
* It also supports the following events:
*
@@ -25,6 +27,10 @@
* - RC compare triggered
*/
+/* Capture extensions */
+#define COUNTER_MCHP_EXCAP_RA 0
+#define COUNTER_MCHP_EXCAP_RB 1
+
/* Event channels */
#define COUNTER_MCHP_EVCHN_CV 0
#define COUNTER_MCHP_EVCHN_RA 0
--
2.48.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v7 1/2] counter: microchip-tcb-capture: Add IRQ handling
2025-03-04 15:51 ` [PATCH v7 1/2] counter: microchip-tcb-capture: Add IRQ handling Bence Csókás
@ 2025-03-06 5:49 ` William Breathitt Gray
0 siblings, 0 replies; 5+ messages in thread
From: William Breathitt Gray @ 2025-03-06 5:49 UTC (permalink / raw)
To: Bence Csókás
Cc: linux-kernel, linux-arm-kernel, linux-iio, Kamel Bouhara
[-- Attachment #1: Type: text/plain, Size: 974 bytes --]
On Tue, Mar 04, 2025 at 04:51:51PM +0100, Bence Csókás wrote:
> 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>
Hi Bence,
I'm mostly statisfied with this patch now, but I have one minor
suggestion below.
> @@ -27,6 +33,7 @@ struct mchp_tc_data {
> int qdec_mode;
> int num_channels;
> int channel[2];
> + int irq;
We don't use the irq member during the driver operation, so I don't
think this value needs to be saved here with the driver data. It seems
to only be needed in mchp_tc_irq_enable() for devm_request_irq().
Perhaps you can pass the irq value as a parameter to
mchp_tc_irq_enable() instead.
With that change made, I think I'll be ready to pick up this patchset
and merge it into counter-next.
William Breathitt Gray
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v7 2/2] counter: microchip-tcb-capture: Add capture extensions for registers RA/RB
2025-03-04 15:51 ` [PATCH v7 2/2] counter: microchip-tcb-capture: Add capture extensions for registers RA/RB Bence Csókás
@ 2025-03-06 5:51 ` William Breathitt Gray
0 siblings, 0 replies; 5+ messages in thread
From: William Breathitt Gray @ 2025-03-06 5:51 UTC (permalink / raw)
To: Bence Csókás
Cc: linux-arm-kernel, linux-iio, linux-kernel, Kamel Bouhara
[-- Attachment #1: Type: text/plain, Size: 441 bytes --]
On Tue, Mar 04, 2025 at 04:51:52PM +0100, Bence Csókás wrote:
> TCB hardware is capable of capturing the timer value to registers RA and
> RB. Add these registers as capture extensions.
>
> Signed-off-by: Bence Csókás <csokas.bence@prolan.hu>
I'm putting my Acked-by line here to be a remainder for myself in the
next revision that this patch is fine and I already reviewed it.
Acked-by: William Breathitt Gray <wbg@kernel.org>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-03-06 5:52 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-04 15:51 [PATCH v7 0/2] microchip-tcb-capture: Add Capture, Compare, Overflow etc. events Bence Csókás
2025-03-04 15:51 ` [PATCH v7 1/2] counter: microchip-tcb-capture: Add IRQ handling Bence Csókás
2025-03-06 5:49 ` William Breathitt Gray
2025-03-04 15:51 ` [PATCH v7 2/2] counter: microchip-tcb-capture: Add capture extensions for registers RA/RB Bence Csókás
2025-03-06 5:51 ` William Breathitt Gray
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).