• Martin Blumenstingl's avatar
    pwm: meson: Simplify the calculation of the pre-divider and count · fb2081e8
    Martin Blumenstingl authored
    Replace the loop to calculate the pre-divider and count with two
    separate div64_u64() calculations. This makes the code easier to read
    and improves the precision.
    
    Three example cases:
    1) 32.768kHz LPO clock for the SDIO wifi chip on Khadas VIM
       clock input: 500MHz (FCLK_DIV4)
       period: 30518ns
       duty cycle: 15259ns
    old algorithm: pre_div=0, cnt=15259
    new algorithm: pre_div=0, cnt=15259
    (no difference in calculated values)
    
    2) PWM LED on Khadas VIM
       clock input: 24MHz (XTAL)
       period: 7812500ns
       duty cycle: 7812500ns
    old algorithm: pre_div=2, cnt=62004
    new algorithm: pre_div=2, cnt=62500
    Using a scope (24MHz sampling rate) shows the actual difference:
    - old: 7753000ns, off by -59500ns (0.7616%)
    - new: 7815000ns, off by +2500ns (0.032%)
    
    3) Theoretical case where pre_div is different
       clock input: 24MHz (XTAL)
       period: 2730624ns
       duty cycle: 1365312ns
    old algorithm: pre_div=1, cnt=32768
    new algorithm: pre_div=0, cnt=65534
    Using a scope (24MHz sampling rate) shows the actual difference:
    - old: 2731000ns
    - new: 2731000ns
    (my scope is not precise enough to measure the difference if there's
    any)
    Suggested-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
    Acked-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
    Reviewed-by: default avatarNeil Armstrong <narmstrong@baylibre.com>
    Signed-off-by: default avatarMartin Blumenstingl <martin.blumenstingl@googlemail.com>
    Signed-off-by: default avatarThierry Reding <thierry.reding@gmail.com>
    fb2081e8
pwm-meson.c 13 KB