Re: [PATCH v15 1/2] pwm: add microchip soft ip corePWM driver

From: Conor Dooley
Date: Thu Apr 06 2023 - 09:45:37 EST


On Thu, Apr 06, 2023 at 03:17:45PM +0200, Thierry Reding wrote:
> On Sat, Apr 01, 2023 at 09:50:47PM +0100, Conor Dooley wrote:
> > On Thu, Mar 30, 2023 at 08:12:03AM +0100, Conor Dooley wrote:
> >
> > > + /*
> > > + * Because 0xff is not a permitted value some error will seep into the
> > > + * calculation of prescale as prescale grows. Specifically, this error
> > > + * occurs where the remainder of the prescale calculation is less than
> > > + * prescale.
> > > + * For small values of prescale, only a handful of values will need
> > > + * correction, but overall this applies to almost half of the valid
> > > + * values for tmp.
> > > + *
> > > + * To keep the algorithm's decision making consistent, this case is
> > > + * checked for and the simple solution is to, in these cases,
> > > + * decrement prescale and check that the resulting value of period_steps
> > > + * is valid.
> > > + *
> > > + * period_steps can be computed from prescale:
> > > + * period * clk_rate
> > > + * period_steps = ----------------------------- - 1
> > > + * NSEC_PER_SEC * (prescale + 1)
> > > + *
> > > + */
> > > + if (tmp % (MCHPCOREPWM_PERIOD_STEPS_MAX + 1) < *prescale) {
> >
> > Hmm, looks like 32-bit doesn't like this modulus.
> > I pushed things out for LKP to test before sending as I felt I'd not be
> > allowed to do that operation, but got a build success email from it.
> > I'm not sure why the mail wasn't sent as a reply to this, but
> > <202304020410.A86IBNES-lkp@xxxxxxxxx> complains:
> > pwm-microchip-core.c:(.text+0x20a): undefined reference to `__aeabi_uldivmod'
> >
> > I know that tmp < 65536 at this point, so if the general approach is
> > fine, I can always cast it to a non 64-bit type without losing any
> > information.
>
> Since you already use some of the helpers from linux/math64.h, perhaps
> you can use something like div_u64_rem() here?

I had actually initially tried to do that, before I added changed the
calculation of prescale to use DIV64_U64_ROUND_UP():
| *prescale = DIV64_U64_ROUND_UP(tmp, MCHPCOREPWM_PERIOD_STEPS_MAX + 1) - 1;

I could I suppose add an additional:
| div_u64_rem(tmp, MCHPCOREPWM_PERIOD_STEPS_MAX + 1, &remainder);

and then just use the value of remainder in the if statement.
Not the prettiest thing in the world I suppose, but should be 32-bit
safe...

Thanks for the suggestion, I'm just surprised that LKP didn't complain
when it built my tree...

Attachment: signature.asc
Description: PGP signature