diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c index e68f8ec867b9..28e90ea1189e 100644 --- a/drivers/pwm/pwm-stm32.c +++ b/drivers/pwm/pwm-stm32.c @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPL-2.0 -/* + // SPDX-License-Identifier: GPL-2.0 + /* * Copyright (C) STMicroelectronics 2016 * * Author: Gerald Baeza @@ -321,19 +321,23 @@ static int stm32_pwm_config(struct stm32_pwm *priv, unsigned int ch, * First we need to find the minimal value for prescaler such that * * period_ns * clkrate - * ------------------------------ + * ------------------------------ < max_arr + 1 * NSEC_PER_SEC * (prescaler + 1) * - * isn't bigger than max_arr. + * This equation is equivalent to + * + * period_ns * clkrate + * ---------------------------- < prescaler + 1 + * NSEC_PER_SEC * (max_arr + 1) + * + * Using integer division and knowing that the right hand side is + * integer, this is further equivalent to + * + * (period_ns * clkrate) // (NSEC_PER_SEC * (max_arr + 1)) ≤ prescaler */ prescaler = mul_u64_u64_div_u64(period_ns, clk_get_rate(priv->clk), - (u64)NSEC_PER_SEC * priv->max_arr); - if (prescaler > 0) - prescaler -= 1; - - if (prescaler > MAX_TIM_PSC) - return -EINVAL; + (u64)NSEC_PER_SEC * ((u64)priv->max_arr + 1)); prd = mul_u64_u64_div_u64(period_ns, clk_get_rate(priv->clk), (u64)NSEC_PER_SEC * (prescaler + 1));