Skip to content

Commit 34ff799

Browse files
committed
Merge tag 'counter-updates-for-6.15' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/wbg/counter into char-misc-next
William writes: Counter updates for 6.15 counter: - Introduce the COUNTER_EVENT_DIRECTION_CHANGE event - Introduce the COUNTER_COMP_COMPARE helper macro microchip-tcb-cpature: - Add IRQ handling - Add support for capture extensions - Add support for compare extension ti-eqep: - Add support for reading and detecting changes in direction tools/counter: - Add counter_watch_events executable to .gitignore - Support COUNTER_EVENT_DIRECTION_CHANGE in counter_watch_events tool * tag 'counter-updates-for-6.15' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/wbg/counter: counter: microchip-tcb-capture: Add support for RC Compare counter: Introduce the compare component counter: microchip-tcb-capture: Add capture extensions for registers RA/RB counter: microchip-tcb-capture: Add IRQ handling counter: ti-eqep: add direction support tools/counter: add direction change event to watcher counter: add direction change event tools/counter: gitignore counter_watch_events
2 parents 046cc01 + ba27a02 commit 34ff799

File tree

9 files changed

+253
-0
lines changed

9 files changed

+253
-0
lines changed

Documentation/ABI/testing/sysfs-bus-counter

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ Contact: linux-iio@vger.kernel.org
3434
Description:
3535
Count data of Count Y represented as a string.
3636

37+
What: /sys/bus/counter/devices/counterX/countY/compare
38+
KernelVersion: 6.15
39+
Contact: linux-iio@vger.kernel.org
40+
Description:
41+
If the counter device supports compare registers -- registers
42+
used to compare counter channels against a particular count --
43+
the compare count for channel Y is provided by this attribute.
44+
3745
What: /sys/bus/counter/devices/counterX/countY/capture
3846
KernelVersion: 6.1
3947
Contact: linux-iio@vger.kernel.org
@@ -301,6 +309,7 @@ Description:
301309

302310
What: /sys/bus/counter/devices/counterX/cascade_counts_enable_component_id
303311
What: /sys/bus/counter/devices/counterX/external_input_phase_clock_select_component_id
312+
What: /sys/bus/counter/devices/counterX/countY/compare_component_id
304313
What: /sys/bus/counter/devices/counterX/countY/capture_component_id
305314
What: /sys/bus/counter/devices/counterX/countY/ceiling_component_id
306315
What: /sys/bus/counter/devices/counterX/countY/floor_component_id

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15644,6 +15644,7 @@ L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
1564415644
L: linux-iio@vger.kernel.org
1564515645
S: Maintained
1564615646
F: drivers/counter/microchip-tcb-capture.c
15647+
F: include/uapi/linux/counter/microchip-tcb-capture.h
1564715648

1564815649
MICROCHIP USB251XB DRIVER
1564915650
M: Richard Leitner <richard.leitner@skidata.com>

drivers/counter/microchip-tcb-capture.c

Lines changed: 160 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

@@ -247,6 +253,90 @@ static int mchp_tc_count_read(struct counter_device *counter,
247253
return 0;
248254
}
249255

256+
static int mchp_tc_count_cap_read(struct counter_device *counter,
257+
struct counter_count *count, size_t idx, u64 *val)
258+
{
259+
struct mchp_tc_data *const priv = counter_priv(counter);
260+
u32 cnt;
261+
int ret;
262+
263+
switch (idx) {
264+
case COUNTER_MCHP_EXCAP_RA:
265+
ret = regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], RA), &cnt);
266+
break;
267+
case COUNTER_MCHP_EXCAP_RB:
268+
ret = regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], RB), &cnt);
269+
break;
270+
default:
271+
return -EINVAL;
272+
}
273+
274+
if (ret < 0)
275+
return ret;
276+
277+
*val = cnt;
278+
279+
return 0;
280+
}
281+
282+
static int mchp_tc_count_cap_write(struct counter_device *counter,
283+
struct counter_count *count, size_t idx, u64 val)
284+
{
285+
struct mchp_tc_data *const priv = counter_priv(counter);
286+
int ret;
287+
288+
if (val > U32_MAX)
289+
return -ERANGE;
290+
291+
switch (idx) {
292+
case COUNTER_MCHP_EXCAP_RA:
293+
ret = regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], RA), val);
294+
break;
295+
case COUNTER_MCHP_EXCAP_RB:
296+
ret = regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], RB), val);
297+
break;
298+
default:
299+
return -EINVAL;
300+
}
301+
302+
return ret;
303+
}
304+
305+
static int mchp_tc_count_compare_read(struct counter_device *counter, struct counter_count *count,
306+
u64 *val)
307+
{
308+
struct mchp_tc_data *const priv = counter_priv(counter);
309+
u32 cnt;
310+
int ret;
311+
312+
ret = regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], RC), &cnt);
313+
if (ret < 0)
314+
return ret;
315+
316+
*val = cnt;
317+
318+
return 0;
319+
}
320+
321+
static int mchp_tc_count_compare_write(struct counter_device *counter, struct counter_count *count,
322+
u64 val)
323+
{
324+
struct mchp_tc_data *const priv = counter_priv(counter);
325+
326+
if (val > U32_MAX)
327+
return -ERANGE;
328+
329+
return regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], RC), val);
330+
}
331+
332+
static DEFINE_COUNTER_ARRAY_CAPTURE(mchp_tc_cnt_cap_array, 2);
333+
334+
static struct counter_comp mchp_tc_count_ext[] = {
335+
COUNTER_COMP_ARRAY_CAPTURE(mchp_tc_count_cap_read, mchp_tc_count_cap_write,
336+
mchp_tc_cnt_cap_array),
337+
COUNTER_COMP_COMPARE(mchp_tc_count_compare_read, mchp_tc_count_compare_write),
338+
};
339+
250340
static struct counter_count mchp_tc_counts[] = {
251341
{
252342
.id = 0,
@@ -255,6 +345,8 @@ static struct counter_count mchp_tc_counts[] = {
255345
.num_functions = ARRAY_SIZE(mchp_tc_count_functions),
256346
.synapses = mchp_tc_count_synapses,
257347
.num_synapses = ARRAY_SIZE(mchp_tc_count_synapses),
348+
.ext = mchp_tc_count_ext,
349+
.num_ext = ARRAY_SIZE(mchp_tc_count_ext),
258350
},
259351
};
260352

@@ -294,6 +386,65 @@ static const struct of_device_id atmel_tc_of_match[] = {
294386
{ /* sentinel */ }
295387
};
296388

389+
static irqreturn_t mchp_tc_isr(int irq, void *dev_id)
390+
{
391+
struct counter_device *const counter = dev_id;
392+
struct mchp_tc_data *const priv = counter_priv(counter);
393+
u32 sr, mask;
394+
395+
regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
396+
regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], IMR), &mask);
397+
398+
sr &= mask;
399+
if (!(sr & ATMEL_TC_ALL_IRQ))
400+
return IRQ_NONE;
401+
402+
if (sr & ATMEL_TC_ETRGS)
403+
counter_push_event(counter, COUNTER_EVENT_CHANGE_OF_STATE,
404+
COUNTER_MCHP_EVCHN_CV);
405+
if (sr & ATMEL_TC_LDRAS)
406+
counter_push_event(counter, COUNTER_EVENT_CAPTURE,
407+
COUNTER_MCHP_EVCHN_RA);
408+
if (sr & ATMEL_TC_LDRBS)
409+
counter_push_event(counter, COUNTER_EVENT_CAPTURE,
410+
COUNTER_MCHP_EVCHN_RB);
411+
if (sr & ATMEL_TC_CPCS)
412+
counter_push_event(counter, COUNTER_EVENT_THRESHOLD,
413+
COUNTER_MCHP_EVCHN_RC);
414+
if (sr & ATMEL_TC_COVFS)
415+
counter_push_event(counter, COUNTER_EVENT_OVERFLOW,
416+
COUNTER_MCHP_EVCHN_CV);
417+
418+
return IRQ_HANDLED;
419+
}
420+
421+
static void mchp_tc_irq_remove(void *ptr)
422+
{
423+
struct mchp_tc_data *priv = ptr;
424+
425+
regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], IDR), ATMEL_TC_DEF_IRQS);
426+
}
427+
428+
static int mchp_tc_irq_enable(struct counter_device *const counter, int irq)
429+
{
430+
struct mchp_tc_data *const priv = counter_priv(counter);
431+
int ret = devm_request_irq(counter->parent, irq, mchp_tc_isr, 0,
432+
dev_name(counter->parent), counter);
433+
434+
if (ret < 0)
435+
return ret;
436+
437+
ret = regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], IER), ATMEL_TC_DEF_IRQS);
438+
if (ret < 0)
439+
return ret;
440+
441+
ret = devm_add_action_or_reset(counter->parent, mchp_tc_irq_remove, priv);
442+
if (ret < 0)
443+
return ret;
444+
445+
return 0;
446+
}
447+
297448
static void mchp_tc_clk_remove(void *ptr)
298449
{
299450
clk_disable_unprepare((struct clk *)ptr);
@@ -378,6 +529,15 @@ static int mchp_tc_probe(struct platform_device *pdev)
378529
counter->num_signals = ARRAY_SIZE(mchp_tc_count_signals);
379530
counter->signals = mchp_tc_count_signals;
380531

532+
i = of_irq_get(np->parent, 0);
533+
if (i == -EPROBE_DEFER)
534+
return -EPROBE_DEFER;
535+
if (i > 0) {
536+
ret = mchp_tc_irq_enable(counter, i);
537+
if (ret < 0)
538+
return dev_err_probe(&pdev->dev, ret, "Failed to set up IRQ");
539+
}
540+
381541
ret = devm_counter_add(&pdev->dev, counter);
382542
if (ret < 0)
383543
return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");

drivers/counter/ti-eqep.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@
107107
#define QCLR_PCE BIT(1)
108108
#define QCLR_INT BIT(0)
109109

110+
#define QEPSTS_UPEVNT BIT(7)
111+
#define QEPSTS_FDF BIT(6)
112+
#define QEPSTS_QDF BIT(5)
113+
#define QEPSTS_QDLF BIT(4)
114+
#define QEPSTS_COEF BIT(3)
115+
#define QEPSTS_CDEF BIT(2)
116+
#define QEPSTS_FIMF BIT(1)
117+
#define QEPSTS_PCEF BIT(0)
118+
110119
/* EQEP Inputs */
111120
enum {
112121
TI_EQEP_SIGNAL_QEPA, /* QEPA/XCLK */
@@ -286,6 +295,9 @@ static int ti_eqep_events_configure(struct counter_device *counter)
286295
case COUNTER_EVENT_UNDERFLOW:
287296
qeint |= QEINT_PCU;
288297
break;
298+
case COUNTER_EVENT_DIRECTION_CHANGE:
299+
qeint |= QEINT_QDC;
300+
break;
289301
}
290302
}
291303

@@ -298,6 +310,7 @@ static int ti_eqep_watch_validate(struct counter_device *counter,
298310
switch (watch->event) {
299311
case COUNTER_EVENT_OVERFLOW:
300312
case COUNTER_EVENT_UNDERFLOW:
313+
case COUNTER_EVENT_DIRECTION_CHANGE:
301314
if (watch->channel != 0)
302315
return -EINVAL;
303316

@@ -368,11 +381,27 @@ static int ti_eqep_position_enable_write(struct counter_device *counter,
368381
return 0;
369382
}
370383

384+
static int ti_eqep_direction_read(struct counter_device *counter,
385+
struct counter_count *count,
386+
enum counter_count_direction *direction)
387+
{
388+
struct ti_eqep_cnt *priv = counter_priv(counter);
389+
u32 qepsts;
390+
391+
regmap_read(priv->regmap16, QEPSTS, &qepsts);
392+
393+
*direction = (qepsts & QEPSTS_QDF) ? COUNTER_COUNT_DIRECTION_FORWARD
394+
: COUNTER_COUNT_DIRECTION_BACKWARD;
395+
396+
return 0;
397+
}
398+
371399
static struct counter_comp ti_eqep_position_ext[] = {
372400
COUNTER_COMP_CEILING(ti_eqep_position_ceiling_read,
373401
ti_eqep_position_ceiling_write),
374402
COUNTER_COMP_ENABLE(ti_eqep_position_enable_read,
375403
ti_eqep_position_enable_write),
404+
COUNTER_COMP_DIRECTION(ti_eqep_direction_read),
376405
};
377406

378407
static struct counter_signal ti_eqep_signals[] = {
@@ -439,6 +468,9 @@ static irqreturn_t ti_eqep_irq_handler(int irq, void *dev_id)
439468
if (qflg & QFLG_PCU)
440469
counter_push_event(counter, COUNTER_EVENT_UNDERFLOW, 0);
441470

471+
if (qflg & QFLG_QDC)
472+
counter_push_event(counter, COUNTER_EVENT_DIRECTION_CHANGE, 0);
473+
442474
regmap_write(priv->regmap16, QCLR, qflg);
443475

444476
return IRQ_HANDLED;

include/linux/counter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ struct counter_array {
580580
#define COUNTER_COMP_CEILING(_read, _write) \
581581
COUNTER_COMP_COUNT_U64("ceiling", _read, _write)
582582

583+
#define COUNTER_COMP_COMPARE(_read, _write) \
584+
COUNTER_COMP_COUNT_U64("compare", _read, _write)
585+
583586
#define COUNTER_COMP_COUNT_MODE(_read, _write, _available) \
584587
{ \
585588
.type = COUNTER_COMP_COUNT_MODE, \

include/uapi/linux/counter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ enum counter_event_type {
6565
COUNTER_EVENT_CHANGE_OF_STATE,
6666
/* Count value captured */
6767
COUNTER_EVENT_CAPTURE,
68+
/* Direction change detected */
69+
COUNTER_EVENT_DIRECTION_CHANGE,
6870
};
6971

7072
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
* \__ Extension capture0 (RA register)
16+
* \__ Extension capture1 (RB register)
17+
*
18+
* It also supports the following events:
19+
*
20+
* Channel 0:
21+
* - CV register changed
22+
* - CV overflowed
23+
* - RA captured
24+
* Channel 1:
25+
* - RB captured
26+
* Channel 2:
27+
* - RC compare triggered
28+
*/
29+
30+
/* Capture extensions */
31+
#define COUNTER_MCHP_EXCAP_RA 0
32+
#define COUNTER_MCHP_EXCAP_RB 1
33+
34+
/* Event channels */
35+
#define COUNTER_MCHP_EVCHN_CV 0
36+
#define COUNTER_MCHP_EVCHN_RA 0
37+
#define COUNTER_MCHP_EVCHN_RB 1
38+
#define COUNTER_MCHP_EVCHN_RC 2
39+
40+
#endif /* _UAPI_COUNTER_MCHP_TCB_H_ */

tools/counter/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/counter_example
2+
/counter_watch_events
23
/include/linux/counter.h

0 commit comments

Comments
 (0)