Re: [PATCH v3 2/2] iio: adc: Add TI ADS1100 and ADS1000
From: Andy Shevchenko
Date: Mon Mar 06 2023 - 07:09:32 EST
On Sat, Mar 04, 2023 at 05:26:18PM +0000, Jonathan Cameron wrote:
> On Thu, 2 Mar 2023 16:23:14 +0200
> Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
> > On Thu, Mar 02, 2023 at 08:49:22AM +0100, Mike Looijmans wrote:
> > > On 01-03-2023 16:30, Andy Shevchenko wrote:
> > > > On Tue, Feb 28, 2023 at 07:31:51AM +0100, Mike Looijmans wrote:
...
> > > > > + /* Shift result to compensate for bit resolution vs. sample rate */
> > > > > + value <<= 16 - ads1100_data_bits(data);
> > > > > + *val = sign_extend32(value, 15);
> > > > Why not simply
> > > >
> > > > *val = sign_extend32(value, ads1100_data_bits(data) - 1);
> > > >
> > > > ?
> > >
> > > As discussed with Jonathan Cameron, the register is right-justified and the
> > > number of bits depend on the data rate. Rather than having the "scale"
> > > change when the sample rate changes, we chose to adjust the sample result so
> > > it's always left-justified.
> >
> > Hmm... OK, but it adds unneeded code I think.
>
> There isn't a way to do it in one go that I can think of.
> The first statement is multiplying the value by a power of 2, not just sign extending it.
> You could sign extend first then shift to do the multiply, but ends up same amount
> of code.
>
> It does look a bit like a weird open coded sign extension though so I can see where
> the confusion came from!
I see, for the negative value both approaches will work, for the positive
the original one will provide a multiplied value.
Yeah, doesn't seem to be a subject to the (micro-)optimizations.
...
> > > > > + for (i = 0; i < 4; i++) {
> > > > > + if (BIT(i) == gain) {
> > > > ffs()/__ffs() (look at the documentation for the difference and use proper one).
> > >
> > > Thought of it, but I'd rather have it return EINVAL for attempting to set
> > > the analog gain to "7" (0nly 1,2,4,8 allowed).
> >
> > I'm not sure what you are implying.
> >
> > You have open coded something that has already to be a function which on some
> > architectures become a single assembly instruction.
> >
> > That said, drop your for-loop if-cond and use one of the proposed directly.
> > Then you may compare the result to what ever you want to be a limit and return
> > whatever error code you want to
>
> Agreed, could do it with appropriate ffs() followed by if (BIT(i) != gain) return -EINVAL;
I meant something different.
i = ffs(gain); // or __ffs(gain)?
if (i >= 4)
return -EINVAL;
--
With Best Regards,
Andy Shevchenko