Skip to content

Commit 8744dcd

Browse files
Fabrice GasnierWilliam Breathitt Gray
authored andcommitted
counter: stm32-lptimer-cnt: fix error handling when enabling
In case the stm32_lptim_set_enable_state() fails to update CMP and ARR, a timeout error is raised, by regmap_read_poll_timeout. It may happen, when the lptimer runs on a slow clock, and the clock is gated only few times during the polling. Badly, when this happen, STM32_LPTIM_ENABLE in CR register has been set. So the 'enable' state in sysfs wrongly lies on the counter being correctly enabled, due to CR is read as one in stm32_lptim_is_enabled(). To fix both issues: - enable the clock before writing CMP, ARR and polling ISR bits. It will avoid the possible timeout error. - clear the ENABLE bit in CR and disable the clock in the error path. Fixes: d895882 ("iio: counter: Add support for STM32 LPTimer") Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com> Link: https://lore.kernel.org/r/20250224170657.3368236-1-fabrice.gasnier@foss.st.com Signed-off-by: William Breathitt Gray <wbg@kernel.org>
1 parent 2014c95 commit 8744dcd

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

drivers/counter/stm32-lptimer-cnt.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,43 @@ static int stm32_lptim_set_enable_state(struct stm32_lptim_cnt *priv,
5858
return 0;
5959
}
6060

61+
ret = clk_enable(priv->clk);
62+
if (ret)
63+
goto disable_cnt;
64+
6165
/* LP timer must be enabled before writing CMP & ARR */
6266
ret = regmap_write(priv->regmap, STM32_LPTIM_ARR, priv->ceiling);
6367
if (ret)
64-
return ret;
68+
goto disable_clk;
6569

6670
ret = regmap_write(priv->regmap, STM32_LPTIM_CMP, 0);
6771
if (ret)
68-
return ret;
72+
goto disable_clk;
6973

7074
/* ensure CMP & ARR registers are properly written */
7175
ret = regmap_read_poll_timeout(priv->regmap, STM32_LPTIM_ISR, val,
7276
(val & STM32_LPTIM_CMPOK_ARROK) == STM32_LPTIM_CMPOK_ARROK,
7377
100, 1000);
7478
if (ret)
75-
return ret;
79+
goto disable_clk;
7680

7781
ret = regmap_write(priv->regmap, STM32_LPTIM_ICR,
7882
STM32_LPTIM_CMPOKCF_ARROKCF);
7983
if (ret)
80-
return ret;
84+
goto disable_clk;
8185

82-
ret = clk_enable(priv->clk);
83-
if (ret) {
84-
regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
85-
return ret;
86-
}
8786
priv->enabled = true;
8887

8988
/* Start LP timer in continuous mode */
9089
return regmap_update_bits(priv->regmap, STM32_LPTIM_CR,
9190
STM32_LPTIM_CNTSTRT, STM32_LPTIM_CNTSTRT);
91+
92+
disable_clk:
93+
clk_disable(priv->clk);
94+
disable_cnt:
95+
regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
96+
97+
return ret;
9298
}
9399

94100
static int stm32_lptim_setup(struct stm32_lptim_cnt *priv, int enable)

0 commit comments

Comments
 (0)