1 // SPDX-License-Identifier: GPL-2.0
3 * STM32 Timer Encoder and Counter driver
5 * Copyright (C) STMicroelectronics 2018
7 * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
10 #include <linux/counter.h>
11 #include <linux/interrupt.h>
12 #include <linux/mfd/stm32-timers.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/types.h>
20 #define TIM_CCMR_CCXS (BIT(8) | BIT(0))
21 #define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
22 TIM_CCMR_IC1F | TIM_CCMR_IC2F)
23 #define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
24 TIM_CCER_CC2P | TIM_CCER_CC2NP)
26 #define STM32_CH1_SIG 0
27 #define STM32_CH2_SIG 1
28 #define STM32_CLOCK_SIG 2
29 #define STM32_CH3_SIG 3
30 #define STM32_CH4_SIG 4
32 struct stm32_timer_regs
{
39 struct stm32_timer_cnt
{
40 struct regmap
*regmap
;
44 struct stm32_timer_regs bak
;
46 unsigned int nchannels
;
48 spinlock_t lock
; /* protects nb_ovf */
52 static const enum counter_function stm32_count_functions
[] = {
53 COUNTER_FUNCTION_INCREASE
,
54 COUNTER_FUNCTION_QUADRATURE_X2_A
,
55 COUNTER_FUNCTION_QUADRATURE_X2_B
,
56 COUNTER_FUNCTION_QUADRATURE_X4
,
59 static int stm32_count_read(struct counter_device
*counter
,
60 struct counter_count
*count
, u64
*val
)
62 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
65 regmap_read(priv
->regmap
, TIM_CNT
, &cnt
);
71 static int stm32_count_write(struct counter_device
*counter
,
72 struct counter_count
*count
, const u64 val
)
74 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
77 regmap_read(priv
->regmap
, TIM_ARR
, &ceiling
);
81 return regmap_write(priv
->regmap
, TIM_CNT
, val
);
84 static int stm32_count_function_read(struct counter_device
*counter
,
85 struct counter_count
*count
,
86 enum counter_function
*function
)
88 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
91 regmap_read(priv
->regmap
, TIM_SMCR
, &smcr
);
93 switch (smcr
& TIM_SMCR_SMS
) {
94 case TIM_SMCR_SMS_SLAVE_MODE_DISABLED
:
95 *function
= COUNTER_FUNCTION_INCREASE
;
97 case TIM_SMCR_SMS_ENCODER_MODE_1
:
98 *function
= COUNTER_FUNCTION_QUADRATURE_X2_A
;
100 case TIM_SMCR_SMS_ENCODER_MODE_2
:
101 *function
= COUNTER_FUNCTION_QUADRATURE_X2_B
;
103 case TIM_SMCR_SMS_ENCODER_MODE_3
:
104 *function
= COUNTER_FUNCTION_QUADRATURE_X4
;
111 static int stm32_count_function_write(struct counter_device
*counter
,
112 struct counter_count
*count
,
113 enum counter_function function
)
115 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
119 case COUNTER_FUNCTION_INCREASE
:
120 sms
= TIM_SMCR_SMS_SLAVE_MODE_DISABLED
;
122 case COUNTER_FUNCTION_QUADRATURE_X2_A
:
123 if (!priv
->has_encoder
)
125 sms
= TIM_SMCR_SMS_ENCODER_MODE_1
;
127 case COUNTER_FUNCTION_QUADRATURE_X2_B
:
128 if (!priv
->has_encoder
)
130 sms
= TIM_SMCR_SMS_ENCODER_MODE_2
;
132 case COUNTER_FUNCTION_QUADRATURE_X4
:
133 if (!priv
->has_encoder
)
135 sms
= TIM_SMCR_SMS_ENCODER_MODE_3
;
141 /* Store enable status */
142 regmap_read(priv
->regmap
, TIM_CR1
, &cr1
);
144 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
, 0);
146 regmap_update_bits(priv
->regmap
, TIM_SMCR
, TIM_SMCR_SMS
, sms
);
148 /* Make sure that registers are updated */
149 regmap_update_bits(priv
->regmap
, TIM_EGR
, TIM_EGR_UG
, TIM_EGR_UG
);
151 /* Restore the enable status */
152 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
, cr1
);
157 static int stm32_count_direction_read(struct counter_device
*counter
,
158 struct counter_count
*count
,
159 enum counter_count_direction
*direction
)
161 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
164 regmap_read(priv
->regmap
, TIM_CR1
, &cr1
);
165 *direction
= (cr1
& TIM_CR1_DIR
) ? COUNTER_COUNT_DIRECTION_BACKWARD
:
166 COUNTER_COUNT_DIRECTION_FORWARD
;
171 static int stm32_count_ceiling_read(struct counter_device
*counter
,
172 struct counter_count
*count
, u64
*ceiling
)
174 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
177 regmap_read(priv
->regmap
, TIM_ARR
, &arr
);
184 static int stm32_count_ceiling_write(struct counter_device
*counter
,
185 struct counter_count
*count
, u64 ceiling
)
187 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
189 if (ceiling
> priv
->max_arr
)
192 /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
193 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_ARPE
, 0);
194 regmap_write(priv
->regmap
, TIM_ARR
, ceiling
);
199 static int stm32_count_enable_read(struct counter_device
*counter
,
200 struct counter_count
*count
, u8
*enable
)
202 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
205 regmap_read(priv
->regmap
, TIM_CR1
, &cr1
);
207 *enable
= cr1
& TIM_CR1_CEN
;
212 static int stm32_count_enable_write(struct counter_device
*counter
,
213 struct counter_count
*count
, u8 enable
)
215 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
219 regmap_read(priv
->regmap
, TIM_CR1
, &cr1
);
220 if (!(cr1
& TIM_CR1_CEN
))
221 clk_enable(priv
->clk
);
223 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
,
226 regmap_read(priv
->regmap
, TIM_CR1
, &cr1
);
227 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
, 0);
228 if (cr1
& TIM_CR1_CEN
)
229 clk_disable(priv
->clk
);
232 /* Keep enabled state to properly handle low power states */
233 priv
->enabled
= enable
;
238 static int stm32_count_prescaler_read(struct counter_device
*counter
,
239 struct counter_count
*count
, u64
*prescaler
)
241 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
244 regmap_read(priv
->regmap
, TIM_PSC
, &psc
);
246 *prescaler
= psc
+ 1;
251 static int stm32_count_prescaler_write(struct counter_device
*counter
,
252 struct counter_count
*count
, u64 prescaler
)
254 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
257 if (!prescaler
|| prescaler
> MAX_TIM_PSC
+ 1)
262 return regmap_write(priv
->regmap
, TIM_PSC
, psc
);
265 static int stm32_count_cap_read(struct counter_device
*counter
,
266 struct counter_count
*count
,
269 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
272 if (ch
>= priv
->nchannels
)
277 regmap_read(priv
->regmap
, TIM_CCR1
, &ccrx
);
280 regmap_read(priv
->regmap
, TIM_CCR2
, &ccrx
);
283 regmap_read(priv
->regmap
, TIM_CCR3
, &ccrx
);
286 regmap_read(priv
->regmap
, TIM_CCR4
, &ccrx
);
292 dev_dbg(counter
->parent
, "CCR%zu: 0x%08x\n", ch
+ 1, ccrx
);
299 static int stm32_count_nb_ovf_read(struct counter_device
*counter
,
300 struct counter_count
*count
, u64
*val
)
302 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
303 unsigned long irqflags
;
305 spin_lock_irqsave(&priv
->lock
, irqflags
);
307 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
312 static int stm32_count_nb_ovf_write(struct counter_device
*counter
,
313 struct counter_count
*count
, u64 val
)
315 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
316 unsigned long irqflags
;
318 spin_lock_irqsave(&priv
->lock
, irqflags
);
320 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
325 static DEFINE_COUNTER_ARRAY_CAPTURE(stm32_count_cap_array
, 4);
327 static struct counter_comp stm32_count_ext
[] = {
328 COUNTER_COMP_DIRECTION(stm32_count_direction_read
),
329 COUNTER_COMP_ENABLE(stm32_count_enable_read
, stm32_count_enable_write
),
330 COUNTER_COMP_CEILING(stm32_count_ceiling_read
,
331 stm32_count_ceiling_write
),
332 COUNTER_COMP_COUNT_U64("prescaler", stm32_count_prescaler_read
,
333 stm32_count_prescaler_write
),
334 COUNTER_COMP_ARRAY_CAPTURE(stm32_count_cap_read
, NULL
, stm32_count_cap_array
),
335 COUNTER_COMP_COUNT_U64("num_overflows", stm32_count_nb_ovf_read
, stm32_count_nb_ovf_write
),
338 static const enum counter_synapse_action stm32_clock_synapse_actions
[] = {
339 COUNTER_SYNAPSE_ACTION_RISING_EDGE
,
342 static const enum counter_synapse_action stm32_synapse_actions
[] = {
343 COUNTER_SYNAPSE_ACTION_NONE
,
344 COUNTER_SYNAPSE_ACTION_BOTH_EDGES
347 static int stm32_action_read(struct counter_device
*counter
,
348 struct counter_count
*count
,
349 struct counter_synapse
*synapse
,
350 enum counter_synapse_action
*action
)
352 enum counter_function function
;
355 err
= stm32_count_function_read(counter
, count
, &function
);
360 case COUNTER_FUNCTION_INCREASE
:
361 /* counts on internal clock when CEN=1 */
362 if (synapse
->signal
->id
== STM32_CLOCK_SIG
)
363 *action
= COUNTER_SYNAPSE_ACTION_RISING_EDGE
;
365 *action
= COUNTER_SYNAPSE_ACTION_NONE
;
367 case COUNTER_FUNCTION_QUADRATURE_X2_A
:
368 /* counts up/down on TI1FP1 edge depending on TI2FP2 level */
369 if (synapse
->signal
->id
== STM32_CH1_SIG
)
370 *action
= COUNTER_SYNAPSE_ACTION_BOTH_EDGES
;
372 *action
= COUNTER_SYNAPSE_ACTION_NONE
;
374 case COUNTER_FUNCTION_QUADRATURE_X2_B
:
375 /* counts up/down on TI2FP2 edge depending on TI1FP1 level */
376 if (synapse
->signal
->id
== STM32_CH2_SIG
)
377 *action
= COUNTER_SYNAPSE_ACTION_BOTH_EDGES
;
379 *action
= COUNTER_SYNAPSE_ACTION_NONE
;
381 case COUNTER_FUNCTION_QUADRATURE_X4
:
382 /* counts up/down on both TI1FP1 and TI2FP2 edges */
383 if (synapse
->signal
->id
== STM32_CH1_SIG
|| synapse
->signal
->id
== STM32_CH2_SIG
)
384 *action
= COUNTER_SYNAPSE_ACTION_BOTH_EDGES
;
386 *action
= COUNTER_SYNAPSE_ACTION_NONE
;
393 struct stm32_count_cc_regs
{
400 static const struct stm32_count_cc_regs stm32_cc
[] = {
401 { TIM_CCMR1
, TIM_CCMR_CC1S
, TIM_CCMR_CC1S_TI1
,
402 TIM_CCER_CC1E
| TIM_CCER_CC1P
| TIM_CCER_CC1NP
},
403 { TIM_CCMR1
, TIM_CCMR_CC2S
, TIM_CCMR_CC2S_TI2
,
404 TIM_CCER_CC2E
| TIM_CCER_CC2P
| TIM_CCER_CC2NP
},
405 { TIM_CCMR2
, TIM_CCMR_CC3S
, TIM_CCMR_CC3S_TI3
,
406 TIM_CCER_CC3E
| TIM_CCER_CC3P
| TIM_CCER_CC3NP
},
407 { TIM_CCMR2
, TIM_CCMR_CC4S
, TIM_CCMR_CC4S_TI4
,
408 TIM_CCER_CC4E
| TIM_CCER_CC4P
| TIM_CCER_CC4NP
},
411 static int stm32_count_capture_configure(struct counter_device
*counter
, unsigned int ch
,
414 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
415 const struct stm32_count_cc_regs
*cc
;
418 if (ch
>= ARRAY_SIZE(stm32_cc
) || ch
>= priv
->nchannels
) {
419 dev_err(counter
->parent
, "invalid ch: %d\n", ch
);
426 * configure channel in input capture mode, map channel 1 on TI1, channel2 on TI2...
427 * Select both edges / non-inverted to trigger a capture.
430 /* first clear possibly latched capture flag upon enabling */
431 if (!regmap_test_bits(priv
->regmap
, TIM_CCER
, cc
->ccer_bits
))
432 regmap_write(priv
->regmap
, TIM_SR
, ~TIM_SR_CC_IF(ch
));
433 regmap_update_bits(priv
->regmap
, cc
->ccmr_reg
, cc
->ccmr_mask
,
435 regmap_set_bits(priv
->regmap
, TIM_CCER
, cc
->ccer_bits
);
437 regmap_clear_bits(priv
->regmap
, TIM_CCER
, cc
->ccer_bits
);
438 regmap_clear_bits(priv
->regmap
, cc
->ccmr_reg
, cc
->ccmr_mask
);
441 regmap_read(priv
->regmap
, cc
->ccmr_reg
, &ccmr
);
442 regmap_read(priv
->regmap
, TIM_CCER
, &ccer
);
443 dev_dbg(counter
->parent
, "%s(%s) ch%d 0x%08x 0x%08x\n", __func__
, enable
? "ena" : "dis",
449 static int stm32_count_events_configure(struct counter_device
*counter
)
451 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
452 struct counter_event_node
*event_node
;
456 list_for_each_entry(event_node
, &counter
->events_list
, l
) {
457 switch (event_node
->event
) {
458 case COUNTER_EVENT_OVERFLOW_UNDERFLOW
:
459 /* first clear possibly latched UIF before enabling */
460 if (!regmap_test_bits(priv
->regmap
, TIM_DIER
, TIM_DIER_UIE
))
461 regmap_write(priv
->regmap
, TIM_SR
, (u32
)~TIM_SR_UIF
);
462 dier
|= TIM_DIER_UIE
;
464 case COUNTER_EVENT_CAPTURE
:
465 ret
= stm32_count_capture_configure(counter
, event_node
->channel
, true);
468 dier
|= TIM_DIER_CCxIE(event_node
->channel
+ 1);
471 /* should never reach this path */
476 /* Enable / disable all events at once, from events_list, so write all DIER bits */
477 regmap_write(priv
->regmap
, TIM_DIER
, dier
);
479 /* check for disabled capture events */
480 for (i
= 0 ; i
< priv
->nchannels
; i
++) {
481 if (!(dier
& TIM_DIER_CCxIE(i
+ 1))) {
482 ret
= stm32_count_capture_configure(counter
, i
, false);
491 static int stm32_count_watch_validate(struct counter_device
*counter
,
492 const struct counter_watch
*watch
)
494 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
496 /* Interrupts are optional */
500 switch (watch
->event
) {
501 case COUNTER_EVENT_CAPTURE
:
502 if (watch
->channel
>= priv
->nchannels
) {
503 dev_err(counter
->parent
, "Invalid channel %d\n", watch
->channel
);
507 case COUNTER_EVENT_OVERFLOW_UNDERFLOW
:
514 static const struct counter_ops stm32_timer_cnt_ops
= {
515 .count_read
= stm32_count_read
,
516 .count_write
= stm32_count_write
,
517 .function_read
= stm32_count_function_read
,
518 .function_write
= stm32_count_function_write
,
519 .action_read
= stm32_action_read
,
520 .events_configure
= stm32_count_events_configure
,
521 .watch_validate
= stm32_count_watch_validate
,
524 static int stm32_count_clk_get_freq(struct counter_device
*counter
,
525 struct counter_signal
*signal
, u64
*freq
)
527 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
529 *freq
= clk_get_rate(priv
->clk
);
534 static struct counter_comp stm32_count_clock_ext
[] = {
535 COUNTER_COMP_FREQUENCY(stm32_count_clk_get_freq
),
538 static struct counter_signal stm32_signals
[] = {
540 * Need to declare all the signals as a static array, and keep the signals order here,
541 * even if they're unused or unexisting on some timer instances. It's an abstraction,
542 * e.g. high level view of the counter features.
544 * Userspace programs may rely on signal0 to be "Channel 1", signal1 to be "Channel 2",
545 * and so on. When a signal is unexisting, the COUNTER_SYNAPSE_ACTION_NONE can be used,
546 * to indicate that a signal doesn't affect the counter.
557 .id
= STM32_CLOCK_SIG
,
559 .ext
= stm32_count_clock_ext
,
560 .num_ext
= ARRAY_SIZE(stm32_count_clock_ext
),
572 static struct counter_synapse stm32_count_synapses
[] = {
574 .actions_list
= stm32_synapse_actions
,
575 .num_actions
= ARRAY_SIZE(stm32_synapse_actions
),
576 .signal
= &stm32_signals
[STM32_CH1_SIG
]
579 .actions_list
= stm32_synapse_actions
,
580 .num_actions
= ARRAY_SIZE(stm32_synapse_actions
),
581 .signal
= &stm32_signals
[STM32_CH2_SIG
]
584 .actions_list
= stm32_clock_synapse_actions
,
585 .num_actions
= ARRAY_SIZE(stm32_clock_synapse_actions
),
586 .signal
= &stm32_signals
[STM32_CLOCK_SIG
]
589 .actions_list
= stm32_synapse_actions
,
590 .num_actions
= ARRAY_SIZE(stm32_synapse_actions
),
591 .signal
= &stm32_signals
[STM32_CH3_SIG
]
594 .actions_list
= stm32_synapse_actions
,
595 .num_actions
= ARRAY_SIZE(stm32_synapse_actions
),
596 .signal
= &stm32_signals
[STM32_CH4_SIG
]
600 static struct counter_count stm32_counts
= {
602 .name
= "STM32 Timer Counter",
603 .functions_list
= stm32_count_functions
,
604 .num_functions
= ARRAY_SIZE(stm32_count_functions
),
605 .synapses
= stm32_count_synapses
,
606 .num_synapses
= ARRAY_SIZE(stm32_count_synapses
),
607 .ext
= stm32_count_ext
,
608 .num_ext
= ARRAY_SIZE(stm32_count_ext
)
611 static irqreturn_t
stm32_timer_cnt_isr(int irq
, void *ptr
)
613 struct counter_device
*counter
= ptr
;
614 struct stm32_timer_cnt
*const priv
= counter_priv(counter
);
615 u32 clr
= GENMASK(31, 0); /* SR flags can be cleared by writing 0 (wr 1 has no effect) */
619 regmap_read(priv
->regmap
, TIM_SR
, &sr
);
620 regmap_read(priv
->regmap
, TIM_DIER
, &dier
);
622 * Some status bits in SR don't match with the enable bits in DIER. Only take care of
623 * the possibly enabled bits in DIER (that matches in between SR and DIER).
625 dier
&= (TIM_DIER_UIE
| TIM_DIER_CC1IE
| TIM_DIER_CC2IE
| TIM_DIER_CC3IE
| TIM_DIER_CC4IE
);
628 if (sr
& TIM_SR_UIF
) {
629 spin_lock(&priv
->lock
);
631 spin_unlock(&priv
->lock
);
632 counter_push_event(counter
, COUNTER_EVENT_OVERFLOW_UNDERFLOW
, 0);
633 dev_dbg(counter
->parent
, "COUNTER_EVENT_OVERFLOW_UNDERFLOW\n");
634 /* SR flags can be cleared by writing 0, only clear relevant flag */
638 /* Check capture events */
639 for (i
= 0 ; i
< priv
->nchannels
; i
++) {
640 if (sr
& TIM_SR_CC_IF(i
)) {
641 counter_push_event(counter
, COUNTER_EVENT_CAPTURE
, i
);
642 clr
&= ~TIM_SR_CC_IF(i
);
643 dev_dbg(counter
->parent
, "COUNTER_EVENT_CAPTURE, %d\n", i
);
647 regmap_write(priv
->regmap
, TIM_SR
, clr
);
652 static void stm32_timer_cnt_detect_channels(struct device
*dev
,
653 struct stm32_timer_cnt
*priv
)
655 u32 ccer
, ccer_backup
;
657 regmap_read(priv
->regmap
, TIM_CCER
, &ccer_backup
);
658 regmap_set_bits(priv
->regmap
, TIM_CCER
, TIM_CCER_CCXE
);
659 regmap_read(priv
->regmap
, TIM_CCER
, &ccer
);
660 regmap_write(priv
->regmap
, TIM_CCER
, ccer_backup
);
661 priv
->nchannels
= hweight32(ccer
& TIM_CCER_CCXE
);
663 dev_dbg(dev
, "has %d cc channels\n", priv
->nchannels
);
666 /* encoder supported on TIM1 TIM2 TIM3 TIM4 TIM5 TIM8 */
667 #define STM32_TIM_ENCODER_SUPPORTED (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(7))
669 static const char * const stm32_timer_trigger_compat
[] = {
670 "st,stm32-timer-trigger",
671 "st,stm32h7-timer-trigger",
674 static int stm32_timer_cnt_probe_encoder(struct device
*dev
,
675 struct stm32_timer_cnt
*priv
)
677 struct device
*parent
= dev
->parent
;
678 struct device_node
*tnode
= NULL
, *pnode
= parent
->of_node
;
683 * Need to retrieve the trigger node index from DT, to be able
684 * to determine if the counter supports encoder mode. It also
685 * enforce backward compatibility, and allow to support other
686 * counter modes in this driver (when the timer doesn't support
689 for (i
= 0; i
< ARRAY_SIZE(stm32_timer_trigger_compat
) && !tnode
; i
++)
690 tnode
= of_get_compatible_child(pnode
, stm32_timer_trigger_compat
[i
]);
692 dev_err(dev
, "Can't find trigger node\n");
696 ret
= of_property_read_u32(tnode
, "reg", &idx
);
698 dev_err(dev
, "Can't get index (%d)\n", ret
);
702 priv
->has_encoder
= !!(STM32_TIM_ENCODER_SUPPORTED
& BIT(idx
));
704 dev_dbg(dev
, "encoder support: %s\n", priv
->has_encoder
? "yes" : "no");
709 static int stm32_timer_cnt_probe(struct platform_device
*pdev
)
711 struct stm32_timers
*ddata
= dev_get_drvdata(pdev
->dev
.parent
);
712 struct device
*dev
= &pdev
->dev
;
713 struct stm32_timer_cnt
*priv
;
714 struct counter_device
*counter
;
717 if (IS_ERR_OR_NULL(ddata
))
720 counter
= devm_counter_alloc(dev
, sizeof(*priv
));
724 priv
= counter_priv(counter
);
726 priv
->regmap
= ddata
->regmap
;
727 priv
->clk
= ddata
->clk
;
728 priv
->max_arr
= ddata
->max_arr
;
729 priv
->nr_irqs
= ddata
->nr_irqs
;
731 ret
= stm32_timer_cnt_probe_encoder(dev
, priv
);
735 stm32_timer_cnt_detect_channels(dev
, priv
);
737 counter
->name
= dev_name(dev
);
738 counter
->parent
= dev
;
739 counter
->ops
= &stm32_timer_cnt_ops
;
740 counter
->counts
= &stm32_counts
;
741 counter
->num_counts
= 1;
742 counter
->signals
= stm32_signals
;
743 counter
->num_signals
= ARRAY_SIZE(stm32_signals
);
745 spin_lock_init(&priv
->lock
);
747 platform_set_drvdata(pdev
, priv
);
749 /* STM32 Timers can have either 1 global, or 4 dedicated interrupts (optional) */
750 if (priv
->nr_irqs
== 1) {
751 /* All events reported through the global interrupt */
752 ret
= devm_request_irq(&pdev
->dev
, ddata
->irq
[0], stm32_timer_cnt_isr
,
753 0, dev_name(dev
), counter
);
755 dev_err(dev
, "Failed to request irq %d (err %d)\n",
760 for (i
= 0; i
< priv
->nr_irqs
; i
++) {
762 * Only take care of update IRQ for overflow events, and cc for
765 if (i
!= STM32_TIMERS_IRQ_UP
&& i
!= STM32_TIMERS_IRQ_CC
)
768 ret
= devm_request_irq(&pdev
->dev
, ddata
->irq
[i
], stm32_timer_cnt_isr
,
769 0, dev_name(dev
), counter
);
771 dev_err(dev
, "Failed to request irq %d (err %d)\n",
778 /* Reset input selector to its default input */
779 regmap_write(priv
->regmap
, TIM_TISEL
, 0x0);
781 /* Register Counter device */
782 ret
= devm_counter_add(dev
, counter
);
784 dev_err_probe(dev
, ret
, "Failed to add counter\n");
789 static int __maybe_unused
stm32_timer_cnt_suspend(struct device
*dev
)
791 struct stm32_timer_cnt
*priv
= dev_get_drvdata(dev
);
793 /* Only take care of enabled counter: don't disturb other MFD child */
795 /* Backup registers that may get lost in low power mode */
796 regmap_read(priv
->regmap
, TIM_SMCR
, &priv
->bak
.smcr
);
797 regmap_read(priv
->regmap
, TIM_ARR
, &priv
->bak
.arr
);
798 regmap_read(priv
->regmap
, TIM_CNT
, &priv
->bak
.cnt
);
799 regmap_read(priv
->regmap
, TIM_CR1
, &priv
->bak
.cr1
);
801 /* Disable the counter */
802 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
, 0);
803 clk_disable(priv
->clk
);
806 return pinctrl_pm_select_sleep_state(dev
);
809 static int __maybe_unused
stm32_timer_cnt_resume(struct device
*dev
)
811 struct stm32_timer_cnt
*priv
= dev_get_drvdata(dev
);
814 ret
= pinctrl_pm_select_default_state(dev
);
819 clk_enable(priv
->clk
);
821 /* Restore registers that may have been lost */
822 regmap_write(priv
->regmap
, TIM_SMCR
, priv
->bak
.smcr
);
823 regmap_write(priv
->regmap
, TIM_ARR
, priv
->bak
.arr
);
824 regmap_write(priv
->regmap
, TIM_CNT
, priv
->bak
.cnt
);
826 /* Also re-enables the counter */
827 regmap_write(priv
->regmap
, TIM_CR1
, priv
->bak
.cr1
);
833 static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops
, stm32_timer_cnt_suspend
,
834 stm32_timer_cnt_resume
);
836 static const struct of_device_id stm32_timer_cnt_of_match
[] = {
837 { .compatible
= "st,stm32-timer-counter", },
840 MODULE_DEVICE_TABLE(of
, stm32_timer_cnt_of_match
);
842 static struct platform_driver stm32_timer_cnt_driver
= {
843 .probe
= stm32_timer_cnt_probe
,
845 .name
= "stm32-timer-counter",
846 .of_match_table
= stm32_timer_cnt_of_match
,
847 .pm
= &stm32_timer_cnt_pm_ops
,
850 module_platform_driver(stm32_timer_cnt_driver
);
852 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
853 MODULE_ALIAS("platform:stm32-timer-counter");
854 MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
855 MODULE_LICENSE("GPL v2");
856 MODULE_IMPORT_NS(COUNTER
);