counter: stm32-timer-cnt: Report count function when SLAVE_MODE_DISABLED
[ Upstream commit fae6f62e6a580b663ecf42c2120a0898deae9137 ]
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: ad29937e20 ("counter: Add STM32 Timer quadrature encoder")
Cc: Fabrice Gasnier <fabrice.gasnier@st.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Alexandre Torgue <alexandre.torgue@st.com>
Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Reviewed-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
Link: https://lore.kernel.org/r/20210226012931.161429-1-vilhelm.gray@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
f854abe46b
commit
c5fe922eaf
@@ -44,13 +44,14 @@ struct stm32_timer_cnt {
|
|||||||
* @STM32_COUNT_ENCODER_MODE_3: counts on both TI1FP1 and TI2FP2 edges
|
* @STM32_COUNT_ENCODER_MODE_3: counts on both TI1FP1 and TI2FP2 edges
|
||||||
*/
|
*/
|
||||||
enum stm32_count_function {
|
enum stm32_count_function {
|
||||||
STM32_COUNT_SLAVE_MODE_DISABLED = -1,
|
STM32_COUNT_SLAVE_MODE_DISABLED,
|
||||||
STM32_COUNT_ENCODER_MODE_1,
|
STM32_COUNT_ENCODER_MODE_1,
|
||||||
STM32_COUNT_ENCODER_MODE_2,
|
STM32_COUNT_ENCODER_MODE_2,
|
||||||
STM32_COUNT_ENCODER_MODE_3,
|
STM32_COUNT_ENCODER_MODE_3,
|
||||||
};
|
};
|
||||||
|
|
||||||
static enum counter_count_function stm32_count_functions[] = {
|
static enum counter_count_function stm32_count_functions[] = {
|
||||||
|
[STM32_COUNT_SLAVE_MODE_DISABLED] = COUNTER_COUNT_FUNCTION_INCREASE,
|
||||||
[STM32_COUNT_ENCODER_MODE_1] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_A,
|
[STM32_COUNT_ENCODER_MODE_1] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_A,
|
||||||
[STM32_COUNT_ENCODER_MODE_2] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_B,
|
[STM32_COUNT_ENCODER_MODE_2] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_B,
|
||||||
[STM32_COUNT_ENCODER_MODE_3] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4,
|
[STM32_COUNT_ENCODER_MODE_3] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4,
|
||||||
@@ -90,6 +91,9 @@ static int stm32_count_function_get(struct counter_device *counter,
|
|||||||
regmap_read(priv->regmap, TIM_SMCR, &smcr);
|
regmap_read(priv->regmap, TIM_SMCR, &smcr);
|
||||||
|
|
||||||
switch (smcr & TIM_SMCR_SMS) {
|
switch (smcr & TIM_SMCR_SMS) {
|
||||||
|
case 0:
|
||||||
|
*function = STM32_COUNT_SLAVE_MODE_DISABLED;
|
||||||
|
return 0;
|
||||||
case 1:
|
case 1:
|
||||||
*function = STM32_COUNT_ENCODER_MODE_1;
|
*function = STM32_COUNT_ENCODER_MODE_1;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -99,10 +103,10 @@ static int stm32_count_function_get(struct counter_device *counter,
|
|||||||
case 3:
|
case 3:
|
||||||
*function = STM32_COUNT_ENCODER_MODE_3;
|
*function = STM32_COUNT_ENCODER_MODE_3;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
default:
|
||||||
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int stm32_count_function_set(struct counter_device *counter,
|
static int stm32_count_function_set(struct counter_device *counter,
|
||||||
struct counter_count *count,
|
struct counter_count *count,
|
||||||
@@ -112,6 +116,9 @@ static int stm32_count_function_set(struct counter_device *counter,
|
|||||||
u32 cr1, sms;
|
u32 cr1, sms;
|
||||||
|
|
||||||
switch (function) {
|
switch (function) {
|
||||||
|
case STM32_COUNT_SLAVE_MODE_DISABLED:
|
||||||
|
sms = 0;
|
||||||
|
break;
|
||||||
case STM32_COUNT_ENCODER_MODE_1:
|
case STM32_COUNT_ENCODER_MODE_1:
|
||||||
sms = 1;
|
sms = 1;
|
||||||
break;
|
break;
|
||||||
@@ -122,8 +129,7 @@ static int stm32_count_function_set(struct counter_device *counter,
|
|||||||
sms = 3;
|
sms = 3;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
sms = 0;
|
return -EINVAL;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store enable status */
|
/* Store enable status */
|
||||||
@@ -274,31 +280,36 @@ static int stm32_action_get(struct counter_device *counter,
|
|||||||
size_t function;
|
size_t function;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
/* Default action mode (e.g. STM32_COUNT_SLAVE_MODE_DISABLED) */
|
|
||||||
*action = STM32_SYNAPSE_ACTION_NONE;
|
|
||||||
|
|
||||||
err = stm32_count_function_get(counter, count, &function);
|
err = stm32_count_function_get(counter, count, &function);
|
||||||
if (err)
|
if (err)
|
||||||
return 0;
|
return err;
|
||||||
|
|
||||||
switch (function) {
|
switch (function) {
|
||||||
|
case STM32_COUNT_SLAVE_MODE_DISABLED:
|
||||||
|
/* counts on internal clock when CEN=1 */
|
||||||
|
*action = STM32_SYNAPSE_ACTION_NONE;
|
||||||
|
return 0;
|
||||||
case STM32_COUNT_ENCODER_MODE_1:
|
case STM32_COUNT_ENCODER_MODE_1:
|
||||||
/* counts up/down on TI1FP1 edge depending on TI2FP2 level */
|
/* counts up/down on TI1FP1 edge depending on TI2FP2 level */
|
||||||
if (synapse->signal->id == count->synapses[0].signal->id)
|
if (synapse->signal->id == count->synapses[0].signal->id)
|
||||||
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
|
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
|
||||||
break;
|
else
|
||||||
|
*action = STM32_SYNAPSE_ACTION_NONE;
|
||||||
|
return 0;
|
||||||
case STM32_COUNT_ENCODER_MODE_2:
|
case STM32_COUNT_ENCODER_MODE_2:
|
||||||
/* counts up/down on TI2FP2 edge depending on TI1FP1 level */
|
/* counts up/down on TI2FP2 edge depending on TI1FP1 level */
|
||||||
if (synapse->signal->id == count->synapses[1].signal->id)
|
if (synapse->signal->id == count->synapses[1].signal->id)
|
||||||
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
|
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
|
||||||
break;
|
else
|
||||||
|
*action = STM32_SYNAPSE_ACTION_NONE;
|
||||||
|
return 0;
|
||||||
case STM32_COUNT_ENCODER_MODE_3:
|
case STM32_COUNT_ENCODER_MODE_3:
|
||||||
/* counts up/down on both TI1FP1 and TI2FP2 edges */
|
/* counts up/down on both TI1FP1 and TI2FP2 edges */
|
||||||
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
|
*action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
default:
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct counter_ops stm32_timer_cnt_ops = {
|
static const struct counter_ops stm32_timer_cnt_ops = {
|
||||||
|
|||||||
Reference in New Issue
Block a user