1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2022 Julien Panis <jpanis@baylibre.com>
8 #include <linux/atomic.h>
10 #include <linux/counter.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
21 #define ECAP_DRV_NAME "ecap"
30 #define ECAP_CEVT_LAST ECAP_CEVT4
31 #define ECAP_NB_CEVT (ECAP_CEVT_LAST + 1)
33 #define ECAP_EVT_LAST ECAP_CNTOVF
34 #define ECAP_NB_EVT (ECAP_EVT_LAST + 1)
37 #define ECAP_TSCNT_REG 0x00
39 #define ECAP_CAP_REG(i) (((i) << 2) + 0x08)
41 #define ECAP_ECCTL_REG 0x28
42 #define ECAP_CAPPOL_BIT(i) BIT((i) << 1)
43 #define ECAP_EV_MODE_MASK GENMASK(7, 0)
44 #define ECAP_CAPLDEN_BIT BIT(8)
45 #define ECAP_CONT_ONESHT_BIT BIT(16)
46 #define ECAP_STOPVALUE_MASK GENMASK(18, 17)
47 #define ECAP_TSCNTSTP_BIT BIT(20)
48 #define ECAP_SYNCO_DIS_MASK GENMASK(23, 22)
49 #define ECAP_CAP_APWM_BIT BIT(25)
50 #define ECAP_ECCTL_EN_MASK (ECAP_CAPLDEN_BIT | ECAP_TSCNTSTP_BIT)
51 #define ECAP_ECCTL_CFG_MASK (ECAP_SYNCO_DIS_MASK | ECAP_STOPVALUE_MASK \
52 | ECAP_ECCTL_EN_MASK | ECAP_CAP_APWM_BIT \
53 | ECAP_CONT_ONESHT_BIT)
55 #define ECAP_ECINT_EN_FLG_REG 0x2c
56 #define ECAP_EVT_EN_MASK GENMASK(ECAP_NB_EVT, ECAP_NB_CEVT)
57 #define ECAP_EVT_FLG_BIT(i) BIT((i) + 17)
59 #define ECAP_ECINT_CLR_FRC_REG 0x30
60 #define ECAP_INT_CLR_BIT BIT(0)
61 #define ECAP_EVT_CLR_BIT(i) BIT((i) + 1)
62 #define ECAP_EVT_CLR_MASK GENMASK(ECAP_NB_EVT, 0)
64 #define ECAP_PID_REG 0x5c
67 #define ECAP_CLOCK_SIG 0
68 #define ECAP_INPUT_SIG 1
70 static const struct regmap_config ecap_cnt_regmap_config
= {
74 .max_register
= ECAP_PID_REG
,
78 * struct ecap_cnt_dev - device private data structure
79 * @enabled: device state
80 * @lock: synchronization lock to prevent I/O race conditions
82 * @regmap: device register map
83 * @nb_ovf: number of overflows since capture start
84 * @pm_ctx: device context for PM operations
85 * @pm_ctx.ev_mode: event mode bits
86 * @pm_ctx.time_cntr: timestamp counter value
92 struct regmap
*regmap
;
100 static u8
ecap_cnt_capture_get_evmode(struct counter_device
*counter
)
102 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
105 pm_runtime_get_sync(counter
->parent
);
106 regmap_read(ecap_dev
->regmap
, ECAP_ECCTL_REG
, ®val
);
107 pm_runtime_put_sync(counter
->parent
);
112 static void ecap_cnt_capture_set_evmode(struct counter_device
*counter
, u8 ev_mode
)
114 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
116 pm_runtime_get_sync(counter
->parent
);
117 regmap_update_bits(ecap_dev
->regmap
, ECAP_ECCTL_REG
, ECAP_EV_MODE_MASK
, ev_mode
);
118 pm_runtime_put_sync(counter
->parent
);
121 static void ecap_cnt_capture_enable(struct counter_device
*counter
)
123 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
125 pm_runtime_get_sync(counter
->parent
);
127 /* Enable interrupts on events */
128 regmap_update_bits(ecap_dev
->regmap
, ECAP_ECINT_EN_FLG_REG
,
129 ECAP_EVT_EN_MASK
, ECAP_EVT_EN_MASK
);
132 regmap_update_bits(ecap_dev
->regmap
, ECAP_ECCTL_REG
, ECAP_ECCTL_CFG_MASK
,
133 ECAP_SYNCO_DIS_MASK
| ECAP_STOPVALUE_MASK
| ECAP_ECCTL_EN_MASK
);
136 static void ecap_cnt_capture_disable(struct counter_device
*counter
)
138 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
141 regmap_update_bits(ecap_dev
->regmap
, ECAP_ECCTL_REG
, ECAP_ECCTL_EN_MASK
, 0);
143 /* Disable interrupts on events */
144 regmap_update_bits(ecap_dev
->regmap
, ECAP_ECINT_EN_FLG_REG
, ECAP_EVT_EN_MASK
, 0);
146 pm_runtime_put_sync(counter
->parent
);
149 static u32
ecap_cnt_count_get_val(struct counter_device
*counter
, unsigned int reg
)
151 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
154 pm_runtime_get_sync(counter
->parent
);
155 regmap_read(ecap_dev
->regmap
, reg
, ®val
);
156 pm_runtime_put_sync(counter
->parent
);
161 static void ecap_cnt_count_set_val(struct counter_device
*counter
, unsigned int reg
, u32 val
)
163 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
165 pm_runtime_get_sync(counter
->parent
);
166 regmap_write(ecap_dev
->regmap
, reg
, val
);
167 pm_runtime_put_sync(counter
->parent
);
170 static int ecap_cnt_count_read(struct counter_device
*counter
,
171 struct counter_count
*count
, u64
*val
)
173 *val
= ecap_cnt_count_get_val(counter
, ECAP_TSCNT_REG
);
178 static int ecap_cnt_count_write(struct counter_device
*counter
,
179 struct counter_count
*count
, u64 val
)
184 ecap_cnt_count_set_val(counter
, ECAP_TSCNT_REG
, val
);
189 static int ecap_cnt_function_read(struct counter_device
*counter
,
190 struct counter_count
*count
,
191 enum counter_function
*function
)
193 *function
= COUNTER_FUNCTION_INCREASE
;
198 static int ecap_cnt_action_read(struct counter_device
*counter
,
199 struct counter_count
*count
,
200 struct counter_synapse
*synapse
,
201 enum counter_synapse_action
*action
)
203 *action
= (synapse
->signal
->id
== ECAP_CLOCK_SIG
) ?
204 COUNTER_SYNAPSE_ACTION_RISING_EDGE
:
205 COUNTER_SYNAPSE_ACTION_NONE
;
210 static int ecap_cnt_watch_validate(struct counter_device
*counter
,
211 const struct counter_watch
*watch
)
213 if (watch
->channel
> ECAP_CEVT_LAST
)
216 switch (watch
->event
) {
217 case COUNTER_EVENT_CAPTURE
:
218 case COUNTER_EVENT_OVERFLOW
:
225 static int ecap_cnt_clk_get_freq(struct counter_device
*counter
,
226 struct counter_signal
*signal
, u64
*freq
)
228 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
230 *freq
= clk_get_rate(ecap_dev
->clk
);
235 static int ecap_cnt_pol_read(struct counter_device
*counter
,
236 struct counter_signal
*signal
,
237 size_t idx
, enum counter_signal_polarity
*pol
)
239 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
242 pm_runtime_get_sync(counter
->parent
);
243 bitval
= regmap_test_bits(ecap_dev
->regmap
, ECAP_ECCTL_REG
, ECAP_CAPPOL_BIT(idx
));
244 pm_runtime_put_sync(counter
->parent
);
246 *pol
= bitval
? COUNTER_SIGNAL_POLARITY_NEGATIVE
: COUNTER_SIGNAL_POLARITY_POSITIVE
;
251 static int ecap_cnt_pol_write(struct counter_device
*counter
,
252 struct counter_signal
*signal
,
253 size_t idx
, enum counter_signal_polarity pol
)
255 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
257 pm_runtime_get_sync(counter
->parent
);
258 if (pol
== COUNTER_SIGNAL_POLARITY_NEGATIVE
)
259 regmap_set_bits(ecap_dev
->regmap
, ECAP_ECCTL_REG
, ECAP_CAPPOL_BIT(idx
));
261 regmap_clear_bits(ecap_dev
->regmap
, ECAP_ECCTL_REG
, ECAP_CAPPOL_BIT(idx
));
262 pm_runtime_put_sync(counter
->parent
);
267 static int ecap_cnt_cap_read(struct counter_device
*counter
,
268 struct counter_count
*count
,
269 size_t idx
, u64
*cap
)
271 *cap
= ecap_cnt_count_get_val(counter
, ECAP_CAP_REG(idx
));
276 static int ecap_cnt_cap_write(struct counter_device
*counter
,
277 struct counter_count
*count
,
283 ecap_cnt_count_set_val(counter
, ECAP_CAP_REG(idx
), cap
);
288 static int ecap_cnt_nb_ovf_read(struct counter_device
*counter
,
289 struct counter_count
*count
, u64
*val
)
291 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
293 *val
= atomic_read(&ecap_dev
->nb_ovf
);
298 static int ecap_cnt_nb_ovf_write(struct counter_device
*counter
,
299 struct counter_count
*count
, u64 val
)
301 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
306 atomic_set(&ecap_dev
->nb_ovf
, val
);
311 static int ecap_cnt_ceiling_read(struct counter_device
*counter
,
312 struct counter_count
*count
, u64
*val
)
319 static int ecap_cnt_enable_read(struct counter_device
*counter
,
320 struct counter_count
*count
, u8
*enable
)
322 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
324 *enable
= ecap_dev
->enabled
;
329 static int ecap_cnt_enable_write(struct counter_device
*counter
,
330 struct counter_count
*count
, u8 enable
)
332 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter
);
334 mutex_lock(&ecap_dev
->lock
);
336 if (enable
== ecap_dev
->enabled
)
340 ecap_cnt_capture_enable(counter
);
342 ecap_cnt_capture_disable(counter
);
343 ecap_dev
->enabled
= enable
;
346 mutex_unlock(&ecap_dev
->lock
);
351 static const struct counter_ops ecap_cnt_ops
= {
352 .count_read
= ecap_cnt_count_read
,
353 .count_write
= ecap_cnt_count_write
,
354 .function_read
= ecap_cnt_function_read
,
355 .action_read
= ecap_cnt_action_read
,
356 .watch_validate
= ecap_cnt_watch_validate
,
359 static const enum counter_function ecap_cnt_functions
[] = {
360 COUNTER_FUNCTION_INCREASE
,
363 static const enum counter_synapse_action ecap_cnt_clock_actions
[] = {
364 COUNTER_SYNAPSE_ACTION_RISING_EDGE
,
367 static const enum counter_synapse_action ecap_cnt_input_actions
[] = {
368 COUNTER_SYNAPSE_ACTION_NONE
,
371 static struct counter_comp ecap_cnt_clock_ext
[] = {
372 COUNTER_COMP_FREQUENCY(ecap_cnt_clk_get_freq
),
375 static const enum counter_signal_polarity ecap_cnt_pol_avail
[] = {
376 COUNTER_SIGNAL_POLARITY_POSITIVE
,
377 COUNTER_SIGNAL_POLARITY_NEGATIVE
,
380 static DEFINE_COUNTER_AVAILABLE(ecap_cnt_pol_available
, ecap_cnt_pol_avail
);
381 static DEFINE_COUNTER_ARRAY_POLARITY(ecap_cnt_pol_array
, ecap_cnt_pol_available
, ECAP_NB_CEVT
);
383 static struct counter_comp ecap_cnt_signal_ext
[] = {
384 COUNTER_COMP_ARRAY_POLARITY(ecap_cnt_pol_read
, ecap_cnt_pol_write
, ecap_cnt_pol_array
),
387 static struct counter_signal ecap_cnt_signals
[] = {
389 .id
= ECAP_CLOCK_SIG
,
390 .name
= "Clock Signal",
391 .ext
= ecap_cnt_clock_ext
,
392 .num_ext
= ARRAY_SIZE(ecap_cnt_clock_ext
),
395 .id
= ECAP_INPUT_SIG
,
396 .name
= "Input Signal",
397 .ext
= ecap_cnt_signal_ext
,
398 .num_ext
= ARRAY_SIZE(ecap_cnt_signal_ext
),
402 static struct counter_synapse ecap_cnt_synapses
[] = {
404 .actions_list
= ecap_cnt_clock_actions
,
405 .num_actions
= ARRAY_SIZE(ecap_cnt_clock_actions
),
406 .signal
= &ecap_cnt_signals
[ECAP_CLOCK_SIG
],
409 .actions_list
= ecap_cnt_input_actions
,
410 .num_actions
= ARRAY_SIZE(ecap_cnt_input_actions
),
411 .signal
= &ecap_cnt_signals
[ECAP_INPUT_SIG
],
415 static DEFINE_COUNTER_ARRAY_CAPTURE(ecap_cnt_cap_array
, ECAP_NB_CEVT
);
417 static struct counter_comp ecap_cnt_count_ext
[] = {
418 COUNTER_COMP_ARRAY_CAPTURE(ecap_cnt_cap_read
, ecap_cnt_cap_write
, ecap_cnt_cap_array
),
419 COUNTER_COMP_COUNT_U64("num_overflows", ecap_cnt_nb_ovf_read
, ecap_cnt_nb_ovf_write
),
420 COUNTER_COMP_CEILING(ecap_cnt_ceiling_read
, NULL
),
421 COUNTER_COMP_ENABLE(ecap_cnt_enable_read
, ecap_cnt_enable_write
),
424 static struct counter_count ecap_cnt_counts
[] = {
426 .name
= "Timestamp Counter",
427 .functions_list
= ecap_cnt_functions
,
428 .num_functions
= ARRAY_SIZE(ecap_cnt_functions
),
429 .synapses
= ecap_cnt_synapses
,
430 .num_synapses
= ARRAY_SIZE(ecap_cnt_synapses
),
431 .ext
= ecap_cnt_count_ext
,
432 .num_ext
= ARRAY_SIZE(ecap_cnt_count_ext
),
436 static irqreturn_t
ecap_cnt_isr(int irq
, void *dev_id
)
438 struct counter_device
*counter_dev
= dev_id
;
439 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter_dev
);
440 unsigned int clr
= 0;
444 regmap_read(ecap_dev
->regmap
, ECAP_ECINT_EN_FLG_REG
, &flg
);
446 /* Check capture events */
447 for (i
= 0 ; i
< ECAP_NB_CEVT
; i
++) {
448 if (flg
& ECAP_EVT_FLG_BIT(i
)) {
449 counter_push_event(counter_dev
, COUNTER_EVENT_CAPTURE
, i
);
450 clr
|= ECAP_EVT_CLR_BIT(i
);
454 /* Check counter overflow */
455 if (flg
& ECAP_EVT_FLG_BIT(ECAP_CNTOVF
)) {
456 atomic_inc(&ecap_dev
->nb_ovf
);
457 for (i
= 0 ; i
< ECAP_NB_CEVT
; i
++)
458 counter_push_event(counter_dev
, COUNTER_EVENT_OVERFLOW
, i
);
459 clr
|= ECAP_EVT_CLR_BIT(ECAP_CNTOVF
);
462 clr
|= ECAP_INT_CLR_BIT
;
463 regmap_update_bits(ecap_dev
->regmap
, ECAP_ECINT_CLR_FRC_REG
, ECAP_EVT_CLR_MASK
, clr
);
468 static void ecap_cnt_pm_disable(void *dev
)
470 pm_runtime_disable(dev
);
473 static int ecap_cnt_probe(struct platform_device
*pdev
)
475 struct device
*dev
= &pdev
->dev
;
476 struct ecap_cnt_dev
*ecap_dev
;
477 struct counter_device
*counter_dev
;
478 void __iomem
*mmio_base
;
479 unsigned long clk_rate
;
482 counter_dev
= devm_counter_alloc(dev
, sizeof(*ecap_dev
));
486 counter_dev
->name
= ECAP_DRV_NAME
;
487 counter_dev
->parent
= dev
;
488 counter_dev
->ops
= &ecap_cnt_ops
;
489 counter_dev
->signals
= ecap_cnt_signals
;
490 counter_dev
->num_signals
= ARRAY_SIZE(ecap_cnt_signals
);
491 counter_dev
->counts
= ecap_cnt_counts
;
492 counter_dev
->num_counts
= ARRAY_SIZE(ecap_cnt_counts
);
494 ecap_dev
= counter_priv(counter_dev
);
496 mutex_init(&ecap_dev
->lock
);
498 ecap_dev
->clk
= devm_clk_get_enabled(dev
, "fck");
499 if (IS_ERR(ecap_dev
->clk
))
500 return dev_err_probe(dev
, PTR_ERR(ecap_dev
->clk
), "failed to get clock\n");
502 clk_rate
= clk_get_rate(ecap_dev
->clk
);
504 dev_err(dev
, "failed to get clock rate\n");
508 mmio_base
= devm_platform_ioremap_resource(pdev
, 0);
509 if (IS_ERR(mmio_base
))
510 return PTR_ERR(mmio_base
);
512 ecap_dev
->regmap
= devm_regmap_init_mmio(dev
, mmio_base
, &ecap_cnt_regmap_config
);
513 if (IS_ERR(ecap_dev
->regmap
))
514 return dev_err_probe(dev
, PTR_ERR(ecap_dev
->regmap
), "failed to init regmap\n");
516 ret
= platform_get_irq(pdev
, 0);
518 return dev_err_probe(dev
, ret
, "failed to get irq\n");
520 ret
= devm_request_irq(dev
, ret
, ecap_cnt_isr
, 0, pdev
->name
, counter_dev
);
522 return dev_err_probe(dev
, ret
, "failed to request irq\n");
524 platform_set_drvdata(pdev
, counter_dev
);
526 pm_runtime_enable(dev
);
528 /* Register a cleanup callback to care for disabling PM */
529 ret
= devm_add_action_or_reset(dev
, ecap_cnt_pm_disable
, dev
);
531 return dev_err_probe(dev
, ret
, "failed to add pm disable action\n");
533 ret
= devm_counter_add(dev
, counter_dev
);
535 return dev_err_probe(dev
, ret
, "failed to add counter\n");
540 static void ecap_cnt_remove(struct platform_device
*pdev
)
542 struct counter_device
*counter_dev
= platform_get_drvdata(pdev
);
543 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter_dev
);
545 if (ecap_dev
->enabled
)
546 ecap_cnt_capture_disable(counter_dev
);
549 static int ecap_cnt_suspend(struct device
*dev
)
551 struct counter_device
*counter_dev
= dev_get_drvdata(dev
);
552 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter_dev
);
554 /* If eCAP is running, stop capture then save timestamp counter */
555 if (ecap_dev
->enabled
) {
557 * Disabling capture has the following effects:
558 * - interrupts are disabled
559 * - loading of capture registers is disabled
560 * - timebase counter is stopped
562 ecap_cnt_capture_disable(counter_dev
);
563 ecap_dev
->pm_ctx
.time_cntr
= ecap_cnt_count_get_val(counter_dev
, ECAP_TSCNT_REG
);
566 ecap_dev
->pm_ctx
.ev_mode
= ecap_cnt_capture_get_evmode(counter_dev
);
568 clk_disable(ecap_dev
->clk
);
573 static int ecap_cnt_resume(struct device
*dev
)
575 struct counter_device
*counter_dev
= dev_get_drvdata(dev
);
576 struct ecap_cnt_dev
*ecap_dev
= counter_priv(counter_dev
);
579 ret
= clk_enable(ecap_dev
->clk
);
581 dev_err(dev
, "Cannot enable clock %d\n", ret
);
585 ecap_cnt_capture_set_evmode(counter_dev
, ecap_dev
->pm_ctx
.ev_mode
);
587 /* If eCAP was running, restore timestamp counter then run capture */
588 if (ecap_dev
->enabled
) {
589 ecap_cnt_count_set_val(counter_dev
, ECAP_TSCNT_REG
, ecap_dev
->pm_ctx
.time_cntr
);
590 ecap_cnt_capture_enable(counter_dev
);
596 static DEFINE_SIMPLE_DEV_PM_OPS(ecap_cnt_pm_ops
, ecap_cnt_suspend
, ecap_cnt_resume
);
598 static const struct of_device_id ecap_cnt_of_match
[] = {
599 { .compatible
= "ti,am62-ecap-capture" },
602 MODULE_DEVICE_TABLE(of
, ecap_cnt_of_match
);
604 static struct platform_driver ecap_cnt_driver
= {
605 .probe
= ecap_cnt_probe
,
606 .remove
= ecap_cnt_remove
,
608 .name
= "ecap-capture",
609 .of_match_table
= ecap_cnt_of_match
,
610 .pm
= pm_sleep_ptr(&ecap_cnt_pm_ops
),
613 module_platform_driver(ecap_cnt_driver
);
615 MODULE_DESCRIPTION("ECAP Capture driver");
616 MODULE_AUTHOR("Julien Panis <jpanis@baylibre.com>");
617 MODULE_LICENSE("GPL");
618 MODULE_IMPORT_NS(COUNTER
);