Re: [PATCH v4] iio: tcs3472: implement wait time and sampling frequency

From: Andy Shevchenko

Date: Tue Jun 09 2026 - 11:15:49 EST


On Tue, Jun 09, 2026 at 03:36:03PM +0200, Aldo Conte wrote:
> On 09/06/26 14:15, Andy Shevchenko wrote:
> > On Tue, Jun 09, 2026 at 12:27:52PM +0200, Aldo Conte wrote:

...

> > > case IIO_EV_INFO_PERIOD:
> > > period = tcs3472_cycle_time_us(data) *
> > > tcs3472_intr_pers[data->apers];
> > > *val = period / USEC_PER_SEC;
> > > *val2 = period % USEC_PER_SEC;
> >
> > Again, use HZ-based multiplier, see above.
>
> Here i do not understand why MICROHZ_PER_HZ is better.
> In the previous case, we were indeed referring to the denominator of the
> calculation, which was actually a frequency, so MICROHZ_PER_HZ is indeed
> correct.
> But in this case, we are calculating the integer and fractional parts of a
> period, so I think this would be fine.
> Tell me where I’m going wrong.

Hmm... I think you are right and I stand corrected.

> > > return IIO_VAL_INT_PLUS_MICRO;
> > >
> > > write becomes:
> > >
> > > case IIO_EV_INFO_PERIOD:{
> > > unsigned int cycle_us;
> > >
> > > period = val * USEC_PER_SEC + val2;
> >
> > And again, use HZ-based multiplier, see above.
>
> as above...

...

> > > for (i = 1; i < ARRAY_SIZE(tcs3472_intr_pers) - 1; i++) {
> > > if (period <= cycle_us * tcs3472_intr_pers[i])
> > > break;
> > > }
> >
> > do {} while () seems better choice here (and do it in reverse order?).
>
> Could you suggest the do {} while {} form, which seems better? Because I
> think it is already comprehensible.

OK, it's not do {} while

i = ARRAY_SIZE(tcs3472_intr_pers);
while (i--) {
if (period > cycle_us * tcs3472_intr_pers[i])
break;
}

if I am not mistaken... OTOH, the for-loop is in the original code, perhaps
better to leave this approach and change separately if needed.

--
With Best Regards,
Andy Shevchenko