Skip to content

Commit 4e546f7

Browse files
guillerodriguezgregkh
authored andcommitted
i2c: stm32f7: truncate clock period instead of rounding it
commit 111bb7f upstream. stm32f7_i2c_compute_timing() derives the I2C clock source period (i2cclk) with DIV_ROUND_CLOSEST, which may round it up. When the period is overestimated, all timings computed from it (SCLDEL, SDADEL, SCLL, SCLH) come out shorter on the wire than calculated, and the resulting bus rate can exceed the requested speed, violating the I2C specification minimums for tLOW and tHIGH. For example, with a 104.45 MHz clock source (e.g. PCLK1, the reset-default I2C clock source on STM32MP1), i2cclk is rounded from 9.574 ns up to 10 ns. Requesting a 400 kHz fast mode bus with 72/27 ns rise/fall times and no analog/digital filters then produces an actual bus rate of 415.6 kHz with tLOW = 1254 ns, violating both the 400 kHz maximum rate and the 1300 ns tLOW minimum of the specification. Truncate the period instead, so that it can only be underestimated. The error then falls on the safe side: the programmed timings come out slightly longer than computed and the bus runs marginally below the target rate (375.3 kHz in the example above) while meeting the specification. i2cbus is left rounded-to-closest: it is only used as the target of the clk_error comparison and is never multiplied into the programmed timings, so nearest rounding remains accurate there. Fixes: aeb068c ("i2c: i2c-stm32f7: add driver") Signed-off-by: Guillermo Rodríguez <guille.rodriguez@gmail.com> Cc: <stable@vger.kernel.org> # v4.14+ Acked-by: Alain Volmat <alain.volmat@foss.st.com> Reviewed-by: Pierre-Yves MORDRET <pierre-yves.mordret@foss.st.com> Signed-off-by: Andi Shyti <andi.shyti@kernel.org> Link: https://lore.kernel.org/r/20260611104857.242153-1-guille.rodriguez@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 11dfa37 commit 4e546f7

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

drivers/i2c/busses/i2c-stm32f7.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,13 @@ static int stm32f7_i2c_compute_timing(struct stm32f7_i2c_dev *i2c_dev,
449449
{
450450
struct stm32f7_i2c_spec *specs;
451451
u32 p_prev = STM32F7_PRESC_MAX;
452-
u32 i2cclk = DIV_ROUND_CLOSEST(NSEC_PER_SEC,
453-
setup->clock_src);
452+
/*
453+
* Truncate instead of rounding to closest: if the clock period is
454+
* overestimated, the computed SCL timings will come out shorter on
455+
* the wire, which can push the bus above the target rate and below
456+
* the spec's tLOW/tHIGH minimums.
457+
*/
458+
u32 i2cclk = NSEC_PER_SEC / setup->clock_src;
454459
u32 i2cbus = DIV_ROUND_CLOSEST(NSEC_PER_SEC,
455460
setup->speed_freq);
456461
u32 clk_error_prev = i2cbus;

0 commit comments

Comments
 (0)