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_SIGNAL_U64("frequency", ecap_cnt_clk_get_freq, NULL),
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_ARRAY_POLARITY(ecap_cnt_pol_array, ecap_cnt_pol_avail, ECAP_NB_CEVT);
382 static struct counter_comp ecap_cnt_signal_ext[] = {
383 COUNTER_COMP_ARRAY_POLARITY(ecap_cnt_pol_read, ecap_cnt_pol_write, ecap_cnt_pol_array),
386 static struct counter_signal ecap_cnt_signals[] = {
388 .id = ECAP_CLOCK_SIG,
389 .name = "Clock Signal",
390 .ext = ecap_cnt_clock_ext,
391 .num_ext = ARRAY_SIZE(ecap_cnt_clock_ext),
394 .id = ECAP_INPUT_SIG,
395 .name = "Input Signal",
396 .ext = ecap_cnt_signal_ext,
397 .num_ext = ARRAY_SIZE(ecap_cnt_signal_ext),
401 static struct counter_synapse ecap_cnt_synapses[] = {
403 .actions_list = ecap_cnt_clock_actions,
404 .num_actions = ARRAY_SIZE(ecap_cnt_clock_actions),
405 .signal = &ecap_cnt_signals[ECAP_CLOCK_SIG],
408 .actions_list = ecap_cnt_input_actions,
409 .num_actions = ARRAY_SIZE(ecap_cnt_input_actions),
410 .signal = &ecap_cnt_signals[ECAP_INPUT_SIG],
414 static DEFINE_COUNTER_ARRAY_CAPTURE(ecap_cnt_cap_array, ECAP_NB_CEVT);
416 static struct counter_comp ecap_cnt_count_ext[] = {
417 COUNTER_COMP_ARRAY_CAPTURE(ecap_cnt_cap_read, ecap_cnt_cap_write, ecap_cnt_cap_array),
418 COUNTER_COMP_COUNT_U64("num_overflows", ecap_cnt_nb_ovf_read, ecap_cnt_nb_ovf_write),
419 COUNTER_COMP_CEILING(ecap_cnt_ceiling_read, NULL),
420 COUNTER_COMP_ENABLE(ecap_cnt_enable_read, ecap_cnt_enable_write),
423 static struct counter_count ecap_cnt_counts[] = {
425 .name = "Timestamp Counter",
426 .functions_list = ecap_cnt_functions,
427 .num_functions = ARRAY_SIZE(ecap_cnt_functions),
428 .synapses = ecap_cnt_synapses,
429 .num_synapses = ARRAY_SIZE(ecap_cnt_synapses),
430 .ext = ecap_cnt_count_ext,
431 .num_ext = ARRAY_SIZE(ecap_cnt_count_ext),
435 static irqreturn_t ecap_cnt_isr(int irq, void *dev_id)
437 struct counter_device *counter_dev = dev_id;
438 struct ecap_cnt_dev *ecap_dev = counter_priv(counter_dev);
439 unsigned int clr = 0;
443 regmap_read(ecap_dev->regmap, ECAP_ECINT_EN_FLG_REG, &flg);
445 /* Check capture events */
446 for (i = 0 ; i < ECAP_NB_CEVT ; i++) {
447 if (flg & ECAP_EVT_FLG_BIT(i)) {
448 counter_push_event(counter_dev, COUNTER_EVENT_CAPTURE, i);
449 clr |= ECAP_EVT_CLR_BIT(i);
453 /* Check counter overflow */
454 if (flg & ECAP_EVT_FLG_BIT(ECAP_CNTOVF)) {
455 atomic_inc(&ecap_dev->nb_ovf);
456 for (i = 0 ; i < ECAP_NB_CEVT ; i++)
457 counter_push_event(counter_dev, COUNTER_EVENT_OVERFLOW, i);
458 clr |= ECAP_EVT_CLR_BIT(ECAP_CNTOVF);
461 clr |= ECAP_INT_CLR_BIT;
462 regmap_update_bits(ecap_dev->regmap, ECAP_ECINT_CLR_FRC_REG, ECAP_EVT_CLR_MASK, clr);
467 static void ecap_cnt_pm_disable(void *dev)
469 pm_runtime_disable(dev);
472 static int ecap_cnt_probe(struct platform_device *pdev)
474 struct device *dev = &pdev->dev;
475 struct ecap_cnt_dev *ecap_dev;
476 struct counter_device *counter_dev;
477 void __iomem *mmio_base;
478 unsigned long clk_rate;
481 counter_dev = devm_counter_alloc(dev, sizeof(*ecap_dev));
482 if (IS_ERR(counter_dev))
483 return PTR_ERR(counter_dev);
485 counter_dev->name = ECAP_DRV_NAME;
486 counter_dev->parent = dev;
487 counter_dev->ops = &ecap_cnt_ops;
488 counter_dev->signals = ecap_cnt_signals;
489 counter_dev->num_signals = ARRAY_SIZE(ecap_cnt_signals);
490 counter_dev->counts = ecap_cnt_counts;
491 counter_dev->num_counts = ARRAY_SIZE(ecap_cnt_counts);
493 ecap_dev = counter_priv(counter_dev);
495 mutex_init(&ecap_dev->lock);
497 ecap_dev->clk = devm_clk_get_enabled(dev, "fck");
498 if (IS_ERR(ecap_dev->clk))
499 return dev_err_probe(dev, PTR_ERR(ecap_dev->clk), "failed to get clock\n");
501 clk_rate = clk_get_rate(ecap_dev->clk);
503 dev_err(dev, "failed to get clock rate\n");
507 mmio_base = devm_platform_ioremap_resource(pdev, 0);
508 if (IS_ERR(mmio_base))
509 return PTR_ERR(mmio_base);
511 ecap_dev->regmap = devm_regmap_init_mmio(dev, mmio_base, &ecap_cnt_regmap_config);
512 if (IS_ERR(ecap_dev->regmap))
513 return dev_err_probe(dev, PTR_ERR(ecap_dev->regmap), "failed to init regmap\n");
515 ret = platform_get_irq(pdev, 0);
517 return dev_err_probe(dev, ret, "failed to get irq\n");
519 ret = devm_request_irq(dev, ret, ecap_cnt_isr, 0, pdev->name, counter_dev);
521 return dev_err_probe(dev, ret, "failed to request irq\n");
523 platform_set_drvdata(pdev, counter_dev);
525 pm_runtime_enable(dev);
527 /* Register a cleanup callback to care for disabling PM */
528 ret = devm_add_action_or_reset(dev, ecap_cnt_pm_disable, dev);
530 return dev_err_probe(dev, ret, "failed to add pm disable action\n");
532 ret = devm_counter_add(dev, counter_dev);
534 return dev_err_probe(dev, ret, "failed to add counter\n");
539 static int ecap_cnt_remove(struct platform_device *pdev)
541 struct counter_device *counter_dev = platform_get_drvdata(pdev);
542 struct ecap_cnt_dev *ecap_dev = counter_priv(counter_dev);
544 if (ecap_dev->enabled)
545 ecap_cnt_capture_disable(counter_dev);
550 static int ecap_cnt_suspend(struct device *dev)
552 struct counter_device *counter_dev = dev_get_drvdata(dev);
553 struct ecap_cnt_dev *ecap_dev = counter_priv(counter_dev);
555 /* If eCAP is running, stop capture then save timestamp counter */
556 if (ecap_dev->enabled) {
558 * Disabling capture has the following effects:
559 * - interrupts are disabled
560 * - loading of capture registers is disabled
561 * - timebase counter is stopped
563 ecap_cnt_capture_disable(counter_dev);
564 ecap_dev->pm_ctx.time_cntr = ecap_cnt_count_get_val(counter_dev, ECAP_TSCNT_REG);
567 ecap_dev->pm_ctx.ev_mode = ecap_cnt_capture_get_evmode(counter_dev);
569 clk_disable(ecap_dev->clk);
574 static int ecap_cnt_resume(struct device *dev)
576 struct counter_device *counter_dev = dev_get_drvdata(dev);
577 struct ecap_cnt_dev *ecap_dev = counter_priv(counter_dev);
579 clk_enable(ecap_dev->clk);
581 ecap_cnt_capture_set_evmode(counter_dev, ecap_dev->pm_ctx.ev_mode);
583 /* If eCAP was running, restore timestamp counter then run capture */
584 if (ecap_dev->enabled) {
585 ecap_cnt_count_set_val(counter_dev, ECAP_TSCNT_REG, ecap_dev->pm_ctx.time_cntr);
586 ecap_cnt_capture_enable(counter_dev);
592 static DEFINE_SIMPLE_DEV_PM_OPS(ecap_cnt_pm_ops, ecap_cnt_suspend, ecap_cnt_resume);
594 static const struct of_device_id ecap_cnt_of_match[] = {
595 { .compatible = "ti,am62-ecap-capture" },
598 MODULE_DEVICE_TABLE(of, ecap_cnt_of_match);
600 static struct platform_driver ecap_cnt_driver = {
601 .probe = ecap_cnt_probe,
602 .remove = ecap_cnt_remove,
604 .name = "ecap-capture",
605 .of_match_table = ecap_cnt_of_match,
606 .pm = pm_sleep_ptr(&ecap_cnt_pm_ops),
609 module_platform_driver(ecap_cnt_driver);
611 MODULE_DESCRIPTION("ECAP Capture driver");
612 MODULE_AUTHOR("Julien Panis <jpanis@baylibre.com>");
613 MODULE_LICENSE("GPL");
614 MODULE_IMPORT_NS(COUNTER);