Re: [PATCH v3 2/2] iio: adc: Add TI ADS1100 and ADS1000

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Mar 06, 2023 at 02:16:24PM +0200, Andy Shevchenko wrote:
> On Mon, Mar 06, 2023 at 02:11:48PM +0200, Andy Shevchenko wrote:
> > On Sat, Mar 04, 2023 at 05:57:51PM +0000, Jonathan Cameron wrote:
> > > On Tue, 28 Feb 2023 07:31:51 +0100
> > > Mike Looijmans <mike.looijmans@xxxxxxxx> wrote:

...

> > > > +	for (i = 0; i < 4; i++) {
> > > > +		if (BIT(i) == gain) {
> > > > +			ads1100_set_config_bits(data, ADS1100_PGA_MASK, i);
> > > > +			return 0;
> > > > +		}
> > > > +	}
> > > Andy's suggestion of something like..
> > > 	if (!gain)
> > > 		return -EINVAL;
> > > 	i = ffs(gain);
> > > 	if (i >= 4 || BIT(i) != gain)
> > > 		return -EINVAL;
> > > 
> > > 	ads...
> > > 
> > > Is perhaps nicer than the loop.
> > 
> > Even better:
> > 
> > 	if (!gain || !is_power_of_2(gain))
> > 		return -EINVAL;
> 
> Or if you want to combine all checks:

> 	if (clamp_val(gain, BIT(0), BIT(3)) != gain || !is_power_of_2(gain))
> 		return -EINVAL;

Just a side note. I have been wanting to have is_in_range() for a long time.
Perhaps we can add a such to the kernel (math.h)

/* Check if in the range [start .. end] */
#define	is_in_range(value, start, end)	(value >= start && value <= end)

With it, the above will probably better look

	if (!is_in_range(gain, BIT(0), BIT(3) || !is_power_of_2(gain))

> 	ads1100_set_config_bits(data, ADS1100_PGA_MASK, ffs(gain));
> 	return 0;
> 
> (You can play with bloat-o-meter for the code generation and see which one is
>  better from that aspect)

-- 
With Best Regards,
Andy Shevchenko





[Index of Archives]     [Device Tree Compilter]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux PCI Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Yosemite Backpacking]


  Powered by Linux