On Fri, 17 Jan 2025 10:59:04 +0100 Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxx> wrote: > Hello, > > On Fri, Dec 20, 2024 at 02:01:34PM +0200, Antoniu Miclaus wrote: > > +static const int ad4851_oversampling_ratios[] = { > > + 1, 2, 4, 8, 16, 32, 64, 128, > > + 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, > > + 65536, > > +}; > > + > > +static int ad4851_osr_to_regval(unsigned int ratio) > > +{ > > + int i; > > + > > + for (i = 1; i < ARRAY_SIZE(ad4851_oversampling_ratios); i++) > > + if (ratio == ad4851_oversampling_ratios[i]) > > + return i - 1; > > + > > + return -EINVAL; > > +} > > This can be simplified (I guess) using something like: > > if (ratio >= 2 && ratio <= 65536 && is_power_of_2(ratio)) > return ilog2(ratio) - 1; > > return -EINVAL; Hi Uwe, Only at the cost of providing custom handling to compute the above array in order to provide it to userspace via the read_avail() callback. We could do what you have here and provide the array but that would be less clear than just looking it up. > > > +static void __ad4851_get_scale(struct iio_dev *indio_dev, int scale_tbl, > > + unsigned int *val, unsigned int *val2) > > +{ > > [...] > > +} > > + > > +static int ad4851_scale_fill(struct iio_dev *indio_dev) > > +{ > > [...] > > +} > > + > > +static int ad4851_set_oversampling_ratio(struct iio_dev *indio_dev, > > + const struct iio_chan_spec *chan, > > + unsigned int osr) > > +{ > > [...] > > +} > > + > > +static int ad4851_get_oversampling_ratio(struct ad4851_state *st, unsigned int *val) > > +{ > > + unsigned int osr; > > + int ret; > > + > > + guard(mutex)(&st->lock); > > + > > + ret = regmap_read(st->regmap, AD4851_REG_OVERSAMPLE, &osr); > > + if (ret) > > + return ret; > > + > > + if (!FIELD_GET(AD4851_OS_EN_MSK, osr)) > > + *val = 1; > > + else > > + *val = ad4851_oversampling_ratios[FIELD_GET(AD4851_OS_RATIO_MSK, osr) + 1]; > > With the suggestion above this gets: > > *val = 2 << FIELD_GET(AD4851_OS_RATIO_MSK, osr); > > (or > *val = 1 << (FIELD_GET(AD4851_OS_RATIO_MSK, osr) + 1); > > ). Then you can drop ad4851_oversampling_ratios[]. You missed the usage in as4851_read_avail() which is the reason it exists. These others are just convenient given that it already exists. Jonathan > > > + > > + st->osr = *val; > > + > > + return IIO_VAL_INT; > > +} > > Best regards > Uwe >