[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230304172618.37f448d0@jic23-huawei>
Date: Sat, 4 Mar 2023 17:26:18 +0000
From: Jonathan Cameron <jic23@...nel.org>
To: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
Cc: Mike Looijmans <mike.looijmans@...ic.nl>,
devicetree@...r.kernel.org, linux-iio@...r.kernel.org,
Caleb Connolly <caleb.connolly@...aro.org>,
ChiYuan Huang <cy_huang@...htek.com>,
ChiaEn Wu <chiaen_wu@...htek.com>,
Cosmin Tanislav <demonsingur@...il.com>,
Ibrahim Tilki <Ibrahim.Tilki@...log.com>,
Lars-Peter Clausen <lars@...afoo.de>,
Ramona Bolboaca <ramona.bolboaca@...log.com>,
William Breathitt Gray <william.gray@...aro.org>,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v3 2/2] iio: adc: Add TI ADS1100 and ADS1000
On Thu, 2 Mar 2023 16:23:14 +0200
Andy Shevchenko <andriy.shevchenko@...ux.intel.com> 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!
>
> ...
>
> > > > + 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;
Powered by blists - more mailing lists