Hi Uwe, Jones <lee.jones@xxxxxxxxxx>; Philipp > Zabel <p.zabel@xxxxxxxxxxxxxx> > Subject: Re: [PATCH v7 2/2] pwm: Add support for RZ/G2L GPT > > 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. Thanks. Tested on target and results are ok now. + for (i =0 ; i < 256; i++) { + pr_info("####### %s ####%u/%u\n",__func__,i,(fls(i) + 1) / 2); + } [ 34.740902] ####### rzg2l_gpt_calculate_prescale ####0/0 [ 34.759151] ####### rzg2l_gpt_calculate_prescale ####3/1 [ 34.825619] ####### rzg2l_gpt_calculate_prescale ####15/2 [ 35.094655] ####### rzg2l_gpt_calculate_prescale ####63/3 [ 36.184126] ####### rzg2l_gpt_calculate_prescale ####255/4 Cheers, Biju