Skip to content

Commit fae6f62

Browse files
William Breathitt Grayjic23
authored andcommitted
counter: stm32-timer-cnt: Report count function when SLAVE_MODE_DISABLED
When in SLAVE_MODE_DISABLED mode, the count still increases if the counter is enabled because an internal clock is used. This patch fixes the stm32_count_function_get() and stm32_count_function_set() functions to properly handle this behavior. Fixes: ad29937 ("counter: Add STM32 Timer quadrature encoder") Cc: Fabrice Gasnier <[email protected]> Cc: Maxime Coquelin <[email protected]> Cc: Alexandre Torgue <[email protected]> Signed-off-by: William Breathitt Gray <[email protected]> Reviewed-by: Fabrice Gasnier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 4f54340 commit fae6f62

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

drivers/counter/stm32-timer-cnt.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ struct stm32_timer_cnt {
4444
* @STM32_COUNT_ENCODER_MODE_3: counts on both TI1FP1 and TI2FP2 edges
4545
*/
4646
enum stm32_count_function {
47-
STM32_COUNT_SLAVE_MODE_DISABLED = -1,
47+
STM32_COUNT_SLAVE_MODE_DISABLED,
4848
STM32_COUNT_ENCODER_MODE_1,
4949
STM32_COUNT_ENCODER_MODE_2,
5050
STM32_COUNT_ENCODER_MODE_3,
5151
};
5252

5353
static enum counter_count_function stm32_count_functions[] = {
54+
[STM32_COUNT_SLAVE_MODE_DISABLED] = COUNTER_COUNT_FUNCTION_INCREASE,
5455
[STM32_COUNT_ENCODER_MODE_1] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_A,
5556
[STM32_COUNT_ENCODER_MODE_2] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_B,
5657
[STM32_COUNT_ENCODER_MODE_3] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4,
@@ -90,6 +91,9 @@ static int stm32_count_function_get(struct counter_device *counter,
9091
regmap_read(priv->regmap, TIM_SMCR, &smcr);
9192

9293
switch (smcr & TIM_SMCR_SMS) {
94+
case 0:
95+
*function = STM32_COUNT_SLAVE_MODE_DISABLED;
96+
return 0;
9397
case 1:
9498
*function = STM32_COUNT_ENCODER_MODE_1;
9599
return 0;
@@ -99,9 +103,9 @@ static int stm32_count_function_get(struct counter_device *counter,
99103
case 3:
100104
*function = STM32_COUNT_ENCODER_MODE_3;
101105
return 0;
106+
default:
107+
return -EINVAL;
102108
}
103-
104-
return -EINVAL;
105109
}
106110

107111
static int stm32_count_function_set(struct counter_device *counter,
@@ -112,6 +116,9 @@ static int stm32_count_function_set(struct counter_device *counter,
112116
u32 cr1, sms;
113117

114118
switch (function) {
119+
case STM32_COUNT_SLAVE_MODE_DISABLED:
120+
sms = 0;
121+
break;
115122
case STM32_COUNT_ENCODER_MODE_1:
116123
sms = 1;
117124
break;
@@ -122,8 +129,7 @@ static int stm32_count_function_set(struct counter_device *counter,
122129
sms = 3;
123130
break;
124131
default:
125-
sms = 0;
126-
break;
132+
return -EINVAL;
127133
}
128134

129135
/* Store enable status */
@@ -274,31 +280,36 @@ static int stm32_action_get(struct counter_device *counter,
274280
size_t function;
275281
int err;
276282

277-
/* Default action mode (e.g. STM32_COUNT_SLAVE_MODE_DISABLED) */
278-
*action = STM32_SYNAPSE_ACTION_NONE;
279-
280283
err = stm32_count_function_get(counter, count, &function);
281284
if (err)
282-
return 0;
285+
return err;
283286

284287
switch (function) {
288+
case STM32_COUNT_SLAVE_MODE_DISABLED:
289+
/* counts on internal clock when CEN=1 */
290+
*action = STM32_SYNAPSE_ACTION_NONE;
291+
return 0;
285292
case STM32_COUNT_ENCODER_MODE_1:
286293
/* counts up/down on TI1FP1 edge depending on TI2FP2 level */
287294
if (synapse->signal->id == count->synapses[0].signal->id)
288295
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
289-
break;
296+
else
297+
*action = STM32_SYNAPSE_ACTION_NONE;
298+
return 0;
290299
case STM32_COUNT_ENCODER_MODE_2:
291300
/* counts up/down on TI2FP2 edge depending on TI1FP1 level */
292301
if (synapse->signal->id == count->synapses[1].signal->id)
293302
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
294-
break;
303+
else
304+
*action = STM32_SYNAPSE_ACTION_NONE;
305+
return 0;
295306
case STM32_COUNT_ENCODER_MODE_3:
296307
/* counts up/down on both TI1FP1 and TI2FP2 edges */
297308
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
298-
break;
309+
return 0;
310+
default:
311+
return -EINVAL;
299312
}
300-
301-
return 0;
302313
}
303314

304315
static const struct counter_ops stm32_timer_cnt_ops = {

0 commit comments

Comments
 (0)