On Fri, Sep 30, 2022 at 06:51:12AM +0000, Biju Das wrote: > Hi Uwe, > > > Subject: Re: [PATCH v7 2/2] pwm: Add support for RZ/G2L GPT > > > > Hello Biju, > > > > On Thu, Sep 29, 2022 at 05:36:38PM +0000, Biju Das wrote: > > > > +static u8 rzg2l_calculate_prescale(struct rzg2l_gpt_chip > > *rzg2l_gpt, > > > > + u64 period_cycles) > > > > +{ > > > > + u32 prescaled_period_cycles; > > > > + u8 prescale; > > > > + > > > > + prescaled_period_cycles = period_cycles >> 32; > > > > + > > > > + if (prescaled_period_cycles >= 256) > > > > + prescale = 5; > > > > + else > > > > + prescale = (roundup_pow_of_two(prescaled_period_cycles + 1) > > > > + 1) / 2; > > > > > > > > > This algorithm won't give desired result. > > > > > > prescaled_period_cycles Expected result > > > 0 ->0 > > > 1..3 ->1 > > > 4..15 ->2 > > > 16..63 ->3 > > > 64..255 ->4 > > > 256 > ->5 > > > > Oh, indeed, it fails for prescaled_period_cycles ∈ { 0, 3, 15, 63, 255 > > }. > > > > The correct formula is: > > > > if (prescaled_period_cycles >= 256) > > prescale = 5; > > else > > prescale = (roundup_pow_of_two(prescaled_period_cycles) + > > 1) / 2; > > > > Round_pow_of_two(n) --> n=0 is not acceptable > > Round_pow_of_two(58)--> 64 as per the above formula, it becomes 64 + 1 /2 = 32 > Which is very high value. Oh, I translated my (Python) prototype wrongly to Kernel-C, please make this: if (prescaled_period_cycles >= 256) prescale = 5; else prescale = (fls(prescaled_period_cycles) + 1) / 2; With fls(58) = 6 the result is 3 as intended. Best regards Uwe -- Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Attachment:
signature.asc
Description: PGP signature