Skip to content

Commit e5d5813

Browse files
Bence CsókásWilliam Breathitt Gray
authored andcommitted
counter: microchip-tcb-capture: Add IRQ handling
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: William Breathitt Gray <[email protected]>
1 parent c2a7566 commit e5d5813

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15642,6 +15642,7 @@ L: [email protected] (moderated for non-subscribers)
1564215642
1564315643
S: Maintained
1564415644
F: drivers/counter/microchip-tcb-capture.c
15645+
F: include/uapi/linux/counter/microchip-tcb-capture.h
1564515646

1564615647
MICROCHIP USB251XB DRIVER
1564715648
M: Richard Leitner <[email protected]>

drivers/counter/microchip-tcb-capture.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
*/
77
#include <linux/clk.h>
88
#include <linux/counter.h>
9+
#include <linux/interrupt.h>
910
#include <linux/mfd/syscon.h>
1011
#include <linux/module.h>
1112
#include <linux/mutex.h>
1213
#include <linux/of.h>
14+
#include <linux/of_irq.h>
1315
#include <linux/platform_device.h>
1416
#include <linux/regmap.h>
17+
#include <uapi/linux/counter/microchip-tcb-capture.h>
1518
#include <soc/at91/atmel_tcb.h>
1619

1720
#define ATMEL_TC_CMR_MASK (ATMEL_TC_LDRA_RISING | ATMEL_TC_LDRB_FALLING | \
1821
ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_LDBDIS | \
1922
ATMEL_TC_LDBSTOP)
2023

24+
#define ATMEL_TC_DEF_IRQS (ATMEL_TC_ETRGS | ATMEL_TC_COVFS | \
25+
ATMEL_TC_LDRAS | ATMEL_TC_LDRBS | ATMEL_TC_CPCS)
26+
2127
#define ATMEL_TC_QDEN BIT(8)
2228
#define ATMEL_TC_POSEN BIT(9)
2329

@@ -294,6 +300,65 @@ static const struct of_device_id atmel_tc_of_match[] = {
294300
{ /* sentinel */ }
295301
};
296302

303+
static irqreturn_t mchp_tc_isr(int irq, void *dev_id)
304+
{
305+
struct counter_device *const counter = dev_id;
306+
struct mchp_tc_data *const priv = counter_priv(counter);
307+
u32 sr, mask;
308+
309+
regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
310+
regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], IMR), &mask);
311+
312+
sr &= mask;
313+
if (!(sr & ATMEL_TC_ALL_IRQ))
314+
return IRQ_NONE;
315+
316+
if (sr & ATMEL_TC_ETRGS)
317+
counter_push_event(counter, COUNTER_EVENT_CHANGE_OF_STATE,
318+
COUNTER_MCHP_EVCHN_CV);
319+
if (sr & ATMEL_TC_LDRAS)
320+
counter_push_event(counter, COUNTER_EVENT_CAPTURE,
321+
COUNTER_MCHP_EVCHN_RA);
322+
if (sr & ATMEL_TC_LDRBS)
323+
counter_push_event(counter, COUNTER_EVENT_CAPTURE,
324+
COUNTER_MCHP_EVCHN_RB);
325+
if (sr & ATMEL_TC_CPCS)
326+
counter_push_event(counter, COUNTER_EVENT_THRESHOLD,
327+
COUNTER_MCHP_EVCHN_RC);
328+
if (sr & ATMEL_TC_COVFS)
329+
counter_push_event(counter, COUNTER_EVENT_OVERFLOW,
330+
COUNTER_MCHP_EVCHN_CV);
331+
332+
return IRQ_HANDLED;
333+
}
334+
335+
static void mchp_tc_irq_remove(void *ptr)
336+
{
337+
struct mchp_tc_data *priv = ptr;
338+
339+
regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], IDR), ATMEL_TC_DEF_IRQS);
340+
}
341+
342+
static int mchp_tc_irq_enable(struct counter_device *const counter, int irq)
343+
{
344+
struct mchp_tc_data *const priv = counter_priv(counter);
345+
int ret = devm_request_irq(counter->parent, irq, mchp_tc_isr, 0,
346+
dev_name(counter->parent), counter);
347+
348+
if (ret < 0)
349+
return ret;
350+
351+
ret = regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], IER), ATMEL_TC_DEF_IRQS);
352+
if (ret < 0)
353+
return ret;
354+
355+
ret = devm_add_action_or_reset(counter->parent, mchp_tc_irq_remove, priv);
356+
if (ret < 0)
357+
return ret;
358+
359+
return 0;
360+
}
361+
297362
static void mchp_tc_clk_remove(void *ptr)
298363
{
299364
clk_disable_unprepare((struct clk *)ptr);
@@ -378,6 +443,15 @@ static int mchp_tc_probe(struct platform_device *pdev)
378443
counter->num_signals = ARRAY_SIZE(mchp_tc_count_signals);
379444
counter->signals = mchp_tc_count_signals;
380445

446+
i = of_irq_get(np->parent, 0);
447+
if (i == -EPROBE_DEFER)
448+
return -EPROBE_DEFER;
449+
if (i > 0) {
450+
ret = mchp_tc_irq_enable(counter, i);
451+
if (ret < 0)
452+
return dev_err_probe(&pdev->dev, ret, "Failed to set up IRQ");
453+
}
454+
381455
ret = devm_counter_add(&pdev->dev, counter);
382456
if (ret < 0)
383457
return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
/*
3+
* Channel numbers used by the microchip-tcb-capture driver
4+
* Copyright (C) 2025 Bence Csókás
5+
*/
6+
#ifndef _UAPI_COUNTER_MCHP_TCB_H_
7+
#define _UAPI_COUNTER_MCHP_TCB_H_
8+
9+
/*
10+
* The driver defines the following components:
11+
*
12+
* Count 0
13+
* \__ Synapse 0 -- Signal 0 (Channel A, i.e. TIOA)
14+
* \__ Synapse 1 -- Signal 1 (Channel B, i.e. TIOB)
15+
*
16+
* It also supports the following events:
17+
*
18+
* Channel 0:
19+
* - CV register changed
20+
* - CV overflowed
21+
* - RA captured
22+
* Channel 1:
23+
* - RB captured
24+
* Channel 2:
25+
* - RC compare triggered
26+
*/
27+
28+
/* Event channels */
29+
#define COUNTER_MCHP_EVCHN_CV 0
30+
#define COUNTER_MCHP_EVCHN_RA 0
31+
#define COUNTER_MCHP_EVCHN_RB 1
32+
#define COUNTER_MCHP_EVCHN_RC 2
33+
34+
#endif /* _UAPI_COUNTER_MCHP_TCB_H_ */

0 commit comments

Comments
 (0)