1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas RZ/G2L MTU3a Counter driver
5 * Copyright (C) 2022 Renesas Electronics Corporation
9 #include <linux/counter.h>
10 #include <linux/mfd/rz-mtu3.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/types.h>
17 * Register descriptions
18 * TSR: Timer Status Register
19 * TMDR1: Timer Mode Register 1
20 * TMDR3: Timer Mode Register 3
21 * TIOR: Timer I/O Control Register
22 * TCR: Timer Control Register
24 * TGRA: Timer general register A
25 * TCNTLW: Timer Longword Counter
26 * TGRALW: Timer longword general register A
29 #define RZ_MTU3_TSR_TCFD BIT(7) /* Count Direction Flag */
31 #define RZ_MTU3_TMDR1_PH_CNT_MODE_1 (4) /* Phase counting mode 1 */
32 #define RZ_MTU3_TMDR1_PH_CNT_MODE_2 (5) /* Phase counting mode 2 */
33 #define RZ_MTU3_TMDR1_PH_CNT_MODE_3 (6) /* Phase counting mode 3 */
34 #define RZ_MTU3_TMDR1_PH_CNT_MODE_4 (7) /* Phase counting mode 4 */
35 #define RZ_MTU3_TMDR1_PH_CNT_MODE_5 (9) /* Phase counting mode 5 */
36 #define RZ_MTU3_TMDR1_PH_CNT_MODE_MASK (0xf)
39 * LWA: MTU1/MTU2 Combination Longword Access Control
40 * 0: 16-bit, 1: 32-bit
42 #define RZ_MTU3_TMDR3_LWA (0)
45 * PHCKSEL: External Input Phase Clock Select
46 * 0: MTCLKA and MTCLKB, 1: MTCLKC and MTCLKD
48 #define RZ_MTU3_TMDR3_PHCKSEL (1)
50 #define RZ_MTU3_16_BIT_MTU1_CH (0)
51 #define RZ_MTU3_16_BIT_MTU2_CH (1)
52 #define RZ_MTU3_32_BIT_CH (2)
54 #define RZ_MTU3_TIOR_NO_OUTPUT (0) /* Output prohibited */
55 #define RZ_MTU3_TIOR_IC_BOTH (10) /* Input capture at both edges */
57 #define SIGNAL_A_ID (0)
58 #define SIGNAL_B_ID (1)
59 #define SIGNAL_C_ID (2)
60 #define SIGNAL_D_ID (3)
62 #define RZ_MTU3_MAX_HW_CNTR_CHANNELS (2)
63 #define RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS (3)
66 * struct rz_mtu3_cnt - MTU3 counter private data
68 * @clk: MTU3 module clock
69 * @lock: Lock to prevent concurrent access for ceiling and count
70 * @ch: HW channels for the counters
71 * @count_is_enabled: Enabled state of Counter value channel
72 * @mtu_16bit_max: Cache for 16-bit counters
73 * @mtu_32bit_max: Cache for 32-bit counters
78 struct rz_mtu3_channel
*ch
;
79 bool count_is_enabled
[RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS
];
81 u16 mtu_16bit_max
[RZ_MTU3_MAX_HW_CNTR_CHANNELS
];
86 static const enum counter_function rz_mtu3_count_functions
[] = {
87 COUNTER_FUNCTION_QUADRATURE_X4
,
88 COUNTER_FUNCTION_PULSE_DIRECTION
,
89 COUNTER_FUNCTION_QUADRATURE_X2_B
,
92 static inline size_t rz_mtu3_get_hw_ch(const size_t id
)
94 return (id
== RZ_MTU3_32_BIT_CH
) ? 0 : id
;
97 static inline struct rz_mtu3_channel
*rz_mtu3_get_ch(struct counter_device
*counter
, int id
)
99 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
100 const size_t ch_id
= rz_mtu3_get_hw_ch(id
);
102 return &priv
->ch
[ch_id
];
105 static bool rz_mtu3_is_counter_invalid(struct counter_device
*counter
, int id
)
107 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
110 pm_runtime_get_sync(priv
->ch
->dev
);
111 tmdr
= rz_mtu3_shared_reg_read(priv
->ch
, RZ_MTU3_TMDR3
);
112 pm_runtime_put(priv
->ch
->dev
);
114 if (id
== RZ_MTU3_32_BIT_CH
&& test_bit(RZ_MTU3_TMDR3_LWA
, &tmdr
))
117 if (id
!= RZ_MTU3_32_BIT_CH
&& !test_bit(RZ_MTU3_TMDR3_LWA
, &tmdr
))
123 static int rz_mtu3_lock_if_counter_is_valid(struct counter_device
*counter
,
124 struct rz_mtu3_channel
*const ch
,
125 struct rz_mtu3_cnt
*const priv
,
128 mutex_lock(&priv
->lock
);
130 if (ch
->is_busy
&& !priv
->count_is_enabled
[id
]) {
131 mutex_unlock(&priv
->lock
);
135 if (rz_mtu3_is_counter_invalid(counter
, id
)) {
136 mutex_unlock(&priv
->lock
);
143 static int rz_mtu3_lock_if_count_is_enabled(struct rz_mtu3_channel
*const ch
,
144 struct rz_mtu3_cnt
*const priv
,
147 mutex_lock(&priv
->lock
);
149 if (ch
->is_busy
&& !priv
->count_is_enabled
[id
]) {
150 mutex_unlock(&priv
->lock
);
157 static int rz_mtu3_count_read(struct counter_device
*counter
,
158 struct counter_count
*count
, u64
*val
)
160 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
161 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
164 ret
= rz_mtu3_lock_if_counter_is_valid(counter
, ch
, priv
, count
->id
);
168 pm_runtime_get_sync(ch
->dev
);
169 if (count
->id
== RZ_MTU3_32_BIT_CH
)
170 *val
= rz_mtu3_32bit_ch_read(ch
, RZ_MTU3_TCNTLW
);
172 *val
= rz_mtu3_16bit_ch_read(ch
, RZ_MTU3_TCNT
);
173 pm_runtime_put(ch
->dev
);
174 mutex_unlock(&priv
->lock
);
179 static int rz_mtu3_count_write(struct counter_device
*counter
,
180 struct counter_count
*count
, const u64 val
)
182 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
183 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
186 ret
= rz_mtu3_lock_if_counter_is_valid(counter
, ch
, priv
, count
->id
);
190 pm_runtime_get_sync(ch
->dev
);
191 if (count
->id
== RZ_MTU3_32_BIT_CH
)
192 rz_mtu3_32bit_ch_write(ch
, RZ_MTU3_TCNTLW
, val
);
194 rz_mtu3_16bit_ch_write(ch
, RZ_MTU3_TCNT
, val
);
195 pm_runtime_put(ch
->dev
);
196 mutex_unlock(&priv
->lock
);
201 static int rz_mtu3_count_function_read_helper(struct rz_mtu3_channel
*const ch
,
202 struct rz_mtu3_cnt
*const priv
,
203 enum counter_function
*function
)
207 pm_runtime_get_sync(ch
->dev
);
208 timer_mode
= rz_mtu3_8bit_ch_read(ch
, RZ_MTU3_TMDR1
);
209 pm_runtime_put(ch
->dev
);
211 switch (timer_mode
& RZ_MTU3_TMDR1_PH_CNT_MODE_MASK
) {
212 case RZ_MTU3_TMDR1_PH_CNT_MODE_1
:
213 *function
= COUNTER_FUNCTION_QUADRATURE_X4
;
215 case RZ_MTU3_TMDR1_PH_CNT_MODE_2
:
216 *function
= COUNTER_FUNCTION_PULSE_DIRECTION
;
218 case RZ_MTU3_TMDR1_PH_CNT_MODE_4
:
219 *function
= COUNTER_FUNCTION_QUADRATURE_X2_B
;
224 * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_3
225 * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_5
231 static int rz_mtu3_count_function_read(struct counter_device
*counter
,
232 struct counter_count
*count
,
233 enum counter_function
*function
)
235 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
236 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
239 ret
= rz_mtu3_lock_if_count_is_enabled(ch
, priv
, count
->id
);
243 ret
= rz_mtu3_count_function_read_helper(ch
, priv
, function
);
244 mutex_unlock(&priv
->lock
);
249 static int rz_mtu3_count_function_write(struct counter_device
*counter
,
250 struct counter_count
*count
,
251 enum counter_function function
)
253 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
254 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
258 ret
= rz_mtu3_lock_if_count_is_enabled(ch
, priv
, count
->id
);
263 case COUNTER_FUNCTION_QUADRATURE_X4
:
264 timer_mode
= RZ_MTU3_TMDR1_PH_CNT_MODE_1
;
266 case COUNTER_FUNCTION_PULSE_DIRECTION
:
267 timer_mode
= RZ_MTU3_TMDR1_PH_CNT_MODE_2
;
269 case COUNTER_FUNCTION_QUADRATURE_X2_B
:
270 timer_mode
= RZ_MTU3_TMDR1_PH_CNT_MODE_4
;
275 * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_3
276 * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_5
278 mutex_unlock(&priv
->lock
);
282 pm_runtime_get_sync(ch
->dev
);
283 rz_mtu3_8bit_ch_write(ch
, RZ_MTU3_TMDR1
, timer_mode
);
284 pm_runtime_put(ch
->dev
);
285 mutex_unlock(&priv
->lock
);
290 static int rz_mtu3_count_direction_read(struct counter_device
*counter
,
291 struct counter_count
*count
,
292 enum counter_count_direction
*direction
)
294 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
295 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
299 ret
= rz_mtu3_lock_if_count_is_enabled(ch
, priv
, count
->id
);
303 pm_runtime_get_sync(ch
->dev
);
304 tsr
= rz_mtu3_8bit_ch_read(ch
, RZ_MTU3_TSR
);
305 pm_runtime_put(ch
->dev
);
307 *direction
= (tsr
& RZ_MTU3_TSR_TCFD
) ?
308 COUNTER_COUNT_DIRECTION_FORWARD
: COUNTER_COUNT_DIRECTION_BACKWARD
;
309 mutex_unlock(&priv
->lock
);
314 static int rz_mtu3_count_ceiling_read(struct counter_device
*counter
,
315 struct counter_count
*count
,
318 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
319 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
320 const size_t ch_id
= rz_mtu3_get_hw_ch(count
->id
);
323 ret
= rz_mtu3_lock_if_counter_is_valid(counter
, ch
, priv
, count
->id
);
328 case RZ_MTU3_16_BIT_MTU1_CH
:
329 case RZ_MTU3_16_BIT_MTU2_CH
:
330 *ceiling
= priv
->mtu_16bit_max
[ch_id
];
332 case RZ_MTU3_32_BIT_CH
:
333 *ceiling
= priv
->mtu_32bit_max
;
336 /* should never reach this path */
337 mutex_unlock(&priv
->lock
);
341 mutex_unlock(&priv
->lock
);
345 static int rz_mtu3_count_ceiling_write(struct counter_device
*counter
,
346 struct counter_count
*count
,
349 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
350 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
351 const size_t ch_id
= rz_mtu3_get_hw_ch(count
->id
);
354 ret
= rz_mtu3_lock_if_counter_is_valid(counter
, ch
, priv
, count
->id
);
359 case RZ_MTU3_16_BIT_MTU1_CH
:
360 case RZ_MTU3_16_BIT_MTU2_CH
:
361 if (ceiling
> U16_MAX
) {
362 mutex_unlock(&priv
->lock
);
365 priv
->mtu_16bit_max
[ch_id
] = ceiling
;
367 case RZ_MTU3_32_BIT_CH
:
368 if (ceiling
> U32_MAX
) {
369 mutex_unlock(&priv
->lock
);
372 priv
->mtu_32bit_max
= ceiling
;
375 /* should never reach this path */
376 mutex_unlock(&priv
->lock
);
380 pm_runtime_get_sync(ch
->dev
);
381 if (count
->id
== RZ_MTU3_32_BIT_CH
)
382 rz_mtu3_32bit_ch_write(ch
, RZ_MTU3_TGRALW
, ceiling
);
384 rz_mtu3_16bit_ch_write(ch
, RZ_MTU3_TGRA
, ceiling
);
386 rz_mtu3_8bit_ch_write(ch
, RZ_MTU3_TCR
, RZ_MTU3_TCR_CCLR_TGRA
);
387 pm_runtime_put(ch
->dev
);
388 mutex_unlock(&priv
->lock
);
393 static void rz_mtu3_32bit_cnt_setting(struct counter_device
*counter
)
395 struct rz_mtu3_channel
*const ch1
= rz_mtu3_get_ch(counter
, 0);
396 struct rz_mtu3_channel
*const ch2
= rz_mtu3_get_ch(counter
, 1);
398 /* Phase counting mode 1 is used as default in initialization. */
399 rz_mtu3_8bit_ch_write(ch1
, RZ_MTU3_TMDR1
, RZ_MTU3_TMDR1_PH_CNT_MODE_1
);
401 rz_mtu3_8bit_ch_write(ch1
, RZ_MTU3_TCR
, RZ_MTU3_TCR_CCLR_TGRA
);
402 rz_mtu3_8bit_ch_write(ch1
, RZ_MTU3_TIOR
, RZ_MTU3_TIOR_IC_BOTH
);
408 static void rz_mtu3_16bit_cnt_setting(struct counter_device
*counter
, int id
)
410 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, id
);
412 /* Phase counting mode 1 is used as default in initialization. */
413 rz_mtu3_8bit_ch_write(ch
, RZ_MTU3_TMDR1
, RZ_MTU3_TMDR1_PH_CNT_MODE_1
);
415 rz_mtu3_8bit_ch_write(ch
, RZ_MTU3_TCR
, RZ_MTU3_TCR_CCLR_TGRA
);
416 rz_mtu3_8bit_ch_write(ch
, RZ_MTU3_TIOR
, RZ_MTU3_TIOR_NO_OUTPUT
);
420 static int rz_mtu3_initialize_counter(struct counter_device
*counter
, int id
)
422 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, id
);
423 struct rz_mtu3_channel
*const ch1
= rz_mtu3_get_ch(counter
, 0);
424 struct rz_mtu3_channel
*const ch2
= rz_mtu3_get_ch(counter
, 1);
427 case RZ_MTU3_16_BIT_MTU1_CH
:
428 case RZ_MTU3_16_BIT_MTU2_CH
:
429 if (!rz_mtu3_request_channel(ch
))
432 rz_mtu3_16bit_cnt_setting(counter
, id
);
434 case RZ_MTU3_32_BIT_CH
:
436 * 32-bit phase counting need MTU1 and MTU2 to create 32-bit
439 if (!rz_mtu3_request_channel(ch1
))
442 if (!rz_mtu3_request_channel(ch2
)) {
443 rz_mtu3_release_channel(ch1
);
447 rz_mtu3_32bit_cnt_setting(counter
);
450 /* should never reach this path */
455 static void rz_mtu3_terminate_counter(struct counter_device
*counter
, int id
)
457 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, id
);
458 struct rz_mtu3_channel
*const ch1
= rz_mtu3_get_ch(counter
, 0);
459 struct rz_mtu3_channel
*const ch2
= rz_mtu3_get_ch(counter
, 1);
461 if (id
== RZ_MTU3_32_BIT_CH
) {
462 rz_mtu3_release_channel(ch2
);
463 rz_mtu3_release_channel(ch1
);
464 rz_mtu3_disable(ch2
);
465 rz_mtu3_disable(ch1
);
467 rz_mtu3_release_channel(ch
);
472 static int rz_mtu3_count_enable_read(struct counter_device
*counter
,
473 struct counter_count
*count
, u8
*enable
)
475 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
476 struct rz_mtu3_channel
*const ch1
= rz_mtu3_get_ch(counter
, 0);
477 struct rz_mtu3_channel
*const ch2
= rz_mtu3_get_ch(counter
, 1);
478 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
481 ret
= rz_mtu3_lock_if_count_is_enabled(ch
, priv
, count
->id
);
485 if (count
->id
== RZ_MTU3_32_BIT_CH
)
486 *enable
= rz_mtu3_is_enabled(ch1
) && rz_mtu3_is_enabled(ch2
);
488 *enable
= rz_mtu3_is_enabled(ch
);
490 mutex_unlock(&priv
->lock
);
495 static int rz_mtu3_count_enable_write(struct counter_device
*counter
,
496 struct counter_count
*count
, u8 enable
)
498 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
499 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
503 mutex_lock(&priv
->lock
);
504 pm_runtime_get_sync(ch
->dev
);
505 ret
= rz_mtu3_initialize_counter(counter
, count
->id
);
507 priv
->count_is_enabled
[count
->id
] = true;
508 mutex_unlock(&priv
->lock
);
510 mutex_lock(&priv
->lock
);
511 rz_mtu3_terminate_counter(counter
, count
->id
);
512 priv
->count_is_enabled
[count
->id
] = false;
513 pm_runtime_put(ch
->dev
);
514 mutex_unlock(&priv
->lock
);
520 static int rz_mtu3_lock_if_ch0_is_enabled(struct rz_mtu3_cnt
*const priv
)
522 mutex_lock(&priv
->lock
);
523 if (priv
->ch
->is_busy
&& !(priv
->count_is_enabled
[RZ_MTU3_16_BIT_MTU1_CH
] ||
524 priv
->count_is_enabled
[RZ_MTU3_32_BIT_CH
])) {
525 mutex_unlock(&priv
->lock
);
532 static int rz_mtu3_cascade_counts_enable_get(struct counter_device
*counter
,
535 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
539 ret
= rz_mtu3_lock_if_ch0_is_enabled(priv
);
543 pm_runtime_get_sync(priv
->ch
->dev
);
544 tmdr
= rz_mtu3_shared_reg_read(priv
->ch
, RZ_MTU3_TMDR3
);
545 pm_runtime_put(priv
->ch
->dev
);
546 *cascade_enable
= test_bit(RZ_MTU3_TMDR3_LWA
, &tmdr
);
547 mutex_unlock(&priv
->lock
);
552 static int rz_mtu3_cascade_counts_enable_set(struct counter_device
*counter
,
555 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
558 ret
= rz_mtu3_lock_if_ch0_is_enabled(priv
);
562 pm_runtime_get_sync(priv
->ch
->dev
);
563 rz_mtu3_shared_reg_update_bit(priv
->ch
, RZ_MTU3_TMDR3
,
564 RZ_MTU3_TMDR3_LWA
, cascade_enable
);
565 pm_runtime_put(priv
->ch
->dev
);
566 mutex_unlock(&priv
->lock
);
571 static int rz_mtu3_ext_input_phase_clock_select_get(struct counter_device
*counter
,
572 u32
*ext_input_phase_clock_select
)
574 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
578 ret
= rz_mtu3_lock_if_ch0_is_enabled(priv
);
582 pm_runtime_get_sync(priv
->ch
->dev
);
583 tmdr
= rz_mtu3_shared_reg_read(priv
->ch
, RZ_MTU3_TMDR3
);
584 pm_runtime_put(priv
->ch
->dev
);
585 *ext_input_phase_clock_select
= test_bit(RZ_MTU3_TMDR3_PHCKSEL
, &tmdr
);
586 mutex_unlock(&priv
->lock
);
591 static int rz_mtu3_ext_input_phase_clock_select_set(struct counter_device
*counter
,
592 u32 ext_input_phase_clock_select
)
594 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
597 ret
= rz_mtu3_lock_if_ch0_is_enabled(priv
);
601 pm_runtime_get_sync(priv
->ch
->dev
);
602 rz_mtu3_shared_reg_update_bit(priv
->ch
, RZ_MTU3_TMDR3
,
603 RZ_MTU3_TMDR3_PHCKSEL
,
604 ext_input_phase_clock_select
);
605 pm_runtime_put(priv
->ch
->dev
);
606 mutex_unlock(&priv
->lock
);
611 static struct counter_comp rz_mtu3_count_ext
[] = {
612 COUNTER_COMP_DIRECTION(rz_mtu3_count_direction_read
),
613 COUNTER_COMP_ENABLE(rz_mtu3_count_enable_read
,
614 rz_mtu3_count_enable_write
),
615 COUNTER_COMP_CEILING(rz_mtu3_count_ceiling_read
,
616 rz_mtu3_count_ceiling_write
),
619 static const enum counter_synapse_action rz_mtu3_synapse_actions
[] = {
620 COUNTER_SYNAPSE_ACTION_BOTH_EDGES
,
621 COUNTER_SYNAPSE_ACTION_RISING_EDGE
,
622 COUNTER_SYNAPSE_ACTION_NONE
,
625 static int rz_mtu3_action_read(struct counter_device
*counter
,
626 struct counter_count
*count
,
627 struct counter_synapse
*synapse
,
628 enum counter_synapse_action
*action
)
630 const bool is_signal_ab
= (synapse
->signal
->id
== SIGNAL_A_ID
) ||
631 (synapse
->signal
->id
== SIGNAL_B_ID
);
632 struct rz_mtu3_channel
*const ch
= rz_mtu3_get_ch(counter
, count
->id
);
633 struct rz_mtu3_cnt
*const priv
= counter_priv(counter
);
634 enum counter_function function
;
639 ret
= rz_mtu3_lock_if_count_is_enabled(ch
, priv
, count
->id
);
643 ret
= rz_mtu3_count_function_read_helper(ch
, priv
, &function
);
645 mutex_unlock(&priv
->lock
);
649 /* Default action mode */
650 *action
= COUNTER_SYNAPSE_ACTION_NONE
;
652 if (count
->id
!= RZ_MTU3_16_BIT_MTU1_CH
) {
653 tmdr
= rz_mtu3_shared_reg_read(priv
->ch
, RZ_MTU3_TMDR3
);
654 mtclkc_mtclkd
= test_bit(RZ_MTU3_TMDR3_PHCKSEL
, &tmdr
);
655 if ((mtclkc_mtclkd
&& is_signal_ab
) ||
656 (!mtclkc_mtclkd
&& !is_signal_ab
)) {
657 mutex_unlock(&priv
->lock
);
663 case COUNTER_FUNCTION_PULSE_DIRECTION
:
665 * Rising edges on signal A (signal C) updates the respective
666 * count. The input level of signal B (signal D) determines
669 if (synapse
->signal
->id
== SIGNAL_A_ID
||
670 synapse
->signal
->id
== SIGNAL_C_ID
)
671 *action
= COUNTER_SYNAPSE_ACTION_RISING_EDGE
;
673 case COUNTER_FUNCTION_QUADRATURE_X2_B
:
675 * Any state transition on quadrature pair signal B (signal D)
676 * updates the respective count.
678 if (synapse
->signal
->id
== SIGNAL_B_ID
||
679 synapse
->signal
->id
== SIGNAL_D_ID
)
680 *action
= COUNTER_SYNAPSE_ACTION_BOTH_EDGES
;
682 case COUNTER_FUNCTION_QUADRATURE_X4
:
683 /* counts up/down on both edges of A (C) and B (D) signal */
684 *action
= COUNTER_SYNAPSE_ACTION_BOTH_EDGES
;
687 /* should never reach this path */
688 mutex_unlock(&priv
->lock
);
692 mutex_unlock(&priv
->lock
);
697 static const struct counter_ops rz_mtu3_cnt_ops
= {
698 .count_read
= rz_mtu3_count_read
,
699 .count_write
= rz_mtu3_count_write
,
700 .function_read
= rz_mtu3_count_function_read
,
701 .function_write
= rz_mtu3_count_function_write
,
702 .action_read
= rz_mtu3_action_read
,
705 #define RZ_MTU3_PHASE_SIGNAL(_id, _name) { \
710 static struct counter_signal rz_mtu3_signals
[] = {
711 RZ_MTU3_PHASE_SIGNAL(SIGNAL_A_ID
, "MTU1 MTCLKA"),
712 RZ_MTU3_PHASE_SIGNAL(SIGNAL_B_ID
, "MTU1 MTCLKB"),
713 RZ_MTU3_PHASE_SIGNAL(SIGNAL_C_ID
, "MTU2 MTCLKC"),
714 RZ_MTU3_PHASE_SIGNAL(SIGNAL_D_ID
, "MTU2 MTCLKD"),
717 static struct counter_synapse rz_mtu3_mtu1_count_synapses
[] = {
719 .actions_list
= rz_mtu3_synapse_actions
,
720 .num_actions
= ARRAY_SIZE(rz_mtu3_synapse_actions
),
721 .signal
= rz_mtu3_signals
,
724 .actions_list
= rz_mtu3_synapse_actions
,
725 .num_actions
= ARRAY_SIZE(rz_mtu3_synapse_actions
),
726 .signal
= rz_mtu3_signals
+ 1,
730 static struct counter_synapse rz_mtu3_mtu2_count_synapses
[] = {
732 .actions_list
= rz_mtu3_synapse_actions
,
733 .num_actions
= ARRAY_SIZE(rz_mtu3_synapse_actions
),
734 .signal
= rz_mtu3_signals
,
737 .actions_list
= rz_mtu3_synapse_actions
,
738 .num_actions
= ARRAY_SIZE(rz_mtu3_synapse_actions
),
739 .signal
= rz_mtu3_signals
+ 1,
742 .actions_list
= rz_mtu3_synapse_actions
,
743 .num_actions
= ARRAY_SIZE(rz_mtu3_synapse_actions
),
744 .signal
= rz_mtu3_signals
+ 2,
747 .actions_list
= rz_mtu3_synapse_actions
,
748 .num_actions
= ARRAY_SIZE(rz_mtu3_synapse_actions
),
749 .signal
= rz_mtu3_signals
+ 3,
753 static struct counter_count rz_mtu3_counts
[] = {
755 .id
= RZ_MTU3_16_BIT_MTU1_CH
,
756 .name
= "Channel 1 Count",
757 .functions_list
= rz_mtu3_count_functions
,
758 .num_functions
= ARRAY_SIZE(rz_mtu3_count_functions
),
759 .synapses
= rz_mtu3_mtu1_count_synapses
,
760 .num_synapses
= ARRAY_SIZE(rz_mtu3_mtu1_count_synapses
),
761 .ext
= rz_mtu3_count_ext
,
762 .num_ext
= ARRAY_SIZE(rz_mtu3_count_ext
),
765 .id
= RZ_MTU3_16_BIT_MTU2_CH
,
766 .name
= "Channel 2 Count",
767 .functions_list
= rz_mtu3_count_functions
,
768 .num_functions
= ARRAY_SIZE(rz_mtu3_count_functions
),
769 .synapses
= rz_mtu3_mtu2_count_synapses
,
770 .num_synapses
= ARRAY_SIZE(rz_mtu3_mtu2_count_synapses
),
771 .ext
= rz_mtu3_count_ext
,
772 .num_ext
= ARRAY_SIZE(rz_mtu3_count_ext
),
775 .id
= RZ_MTU3_32_BIT_CH
,
776 .name
= "Channel 1 and 2 (cascaded) Count",
777 .functions_list
= rz_mtu3_count_functions
,
778 .num_functions
= ARRAY_SIZE(rz_mtu3_count_functions
),
779 .synapses
= rz_mtu3_mtu2_count_synapses
,
780 .num_synapses
= ARRAY_SIZE(rz_mtu3_mtu2_count_synapses
),
781 .ext
= rz_mtu3_count_ext
,
782 .num_ext
= ARRAY_SIZE(rz_mtu3_count_ext
),
786 static const char *const rz_mtu3_ext_input_phase_clock_select
[] = {
791 static DEFINE_COUNTER_ENUM(rz_mtu3_ext_input_phase_clock_select_enum
,
792 rz_mtu3_ext_input_phase_clock_select
);
794 static struct counter_comp rz_mtu3_device_ext
[] = {
795 COUNTER_COMP_DEVICE_BOOL("cascade_counts_enable",
796 rz_mtu3_cascade_counts_enable_get
,
797 rz_mtu3_cascade_counts_enable_set
),
798 COUNTER_COMP_DEVICE_ENUM("external_input_phase_clock_select",
799 rz_mtu3_ext_input_phase_clock_select_get
,
800 rz_mtu3_ext_input_phase_clock_select_set
,
801 rz_mtu3_ext_input_phase_clock_select_enum
),
804 static int rz_mtu3_cnt_pm_runtime_suspend(struct device
*dev
)
806 struct clk
*const clk
= dev_get_drvdata(dev
);
808 clk_disable_unprepare(clk
);
813 static int rz_mtu3_cnt_pm_runtime_resume(struct device
*dev
)
815 struct clk
*const clk
= dev_get_drvdata(dev
);
817 clk_prepare_enable(clk
);
822 static DEFINE_RUNTIME_DEV_PM_OPS(rz_mtu3_cnt_pm_ops
,
823 rz_mtu3_cnt_pm_runtime_suspend
,
824 rz_mtu3_cnt_pm_runtime_resume
, NULL
);
826 static void rz_mtu3_cnt_pm_disable(void *data
)
828 struct device
*dev
= data
;
830 pm_runtime_disable(dev
);
831 pm_runtime_set_suspended(dev
);
834 static int rz_mtu3_cnt_probe(struct platform_device
*pdev
)
836 struct rz_mtu3
*ddata
= dev_get_drvdata(pdev
->dev
.parent
);
837 struct device
*dev
= &pdev
->dev
;
838 struct counter_device
*counter
;
839 struct rz_mtu3_channel
*ch
;
840 struct rz_mtu3_cnt
*priv
;
844 counter
= devm_counter_alloc(dev
, sizeof(*priv
));
848 priv
= counter_priv(counter
);
849 priv
->clk
= ddata
->clk
;
850 priv
->mtu_32bit_max
= U32_MAX
;
851 priv
->ch
= &ddata
->channels
[RZ_MTU3_CHAN_1
];
853 for (i
= 0; i
< RZ_MTU3_MAX_HW_CNTR_CHANNELS
; i
++) {
855 priv
->mtu_16bit_max
[i
] = U16_MAX
;
859 mutex_init(&priv
->lock
);
860 platform_set_drvdata(pdev
, priv
->clk
);
861 clk_prepare_enable(priv
->clk
);
862 pm_runtime_set_active(&pdev
->dev
);
863 pm_runtime_enable(&pdev
->dev
);
864 ret
= devm_add_action_or_reset(&pdev
->dev
, rz_mtu3_cnt_pm_disable
, dev
);
868 counter
->name
= dev_name(dev
);
869 counter
->parent
= dev
;
870 counter
->ops
= &rz_mtu3_cnt_ops
;
871 counter
->counts
= rz_mtu3_counts
;
872 counter
->num_counts
= ARRAY_SIZE(rz_mtu3_counts
);
873 counter
->signals
= rz_mtu3_signals
;
874 counter
->num_signals
= ARRAY_SIZE(rz_mtu3_signals
);
875 counter
->ext
= rz_mtu3_device_ext
;
876 counter
->num_ext
= ARRAY_SIZE(rz_mtu3_device_ext
);
878 /* Register Counter device */
879 ret
= devm_counter_add(dev
, counter
);
881 dev_err_probe(dev
, ret
, "Failed to add counter\n");
888 clk_disable_unprepare(priv
->clk
);
893 static struct platform_driver rz_mtu3_cnt_driver
= {
894 .probe
= rz_mtu3_cnt_probe
,
896 .name
= "rz-mtu3-counter",
897 .pm
= pm_ptr(&rz_mtu3_cnt_pm_ops
),
900 module_platform_driver(rz_mtu3_cnt_driver
);
902 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
903 MODULE_ALIAS("platform:rz-mtu3-counter");
904 MODULE_DESCRIPTION("Renesas RZ/G2L MTU3a counter driver");
905 MODULE_LICENSE("GPL");
906 MODULE_IMPORT_NS("COUNTER");