On Thu, Dec 19, 2019 at 01:02:28PM +0200, Andy Shevchenko wrote: > On Thu, Dec 19, 2019 at 6:27 AM Dan Robertson <dan@xxxxxxxxxxxxxxx> wrote: > > Add a IIO driver for the Bosch BMA400 3-axes ultra-low power accelerometer. > > The driver supports reading from the acceleration and temperature > > registers. The driver also supports reading and configuring the output data > > rate, oversampling ratio, and scale. > > ... > > > +static int bma400_set_accel_output_data_rate(struct bma400_data *data, > > + int hz, int uhz) > > +{ > > + unsigned int idx; > > + unsigned int odr; > > + unsigned int val; > > + int ret; > > + > > + if (hz >= BMA400_ACC_ODR_MIN_WHOLE_HZ) { > > + if (uhz || hz % BMA400_ACC_ODR_MIN_WHOLE_HZ) > > + return -EINVAL; > > + > > + val = hz / BMA400_ACC_ODR_MIN_WHOLE_HZ; > > Again, AFAICS division may be avoided in both cases (% and / above) > because of is_power_of_2() check below. > Can you revisit this? Yeah I can update this in the next patchset, but I don't know if it is much more readable this way. > > + if (!is_power_of_2(val)) > > + return -EINVAL; > > + > > + idx = __ffs(val) + BMA400_ACC_ODR_MIN_RAW + 1; > > + } else if (hz == BMA400_ACC_ODR_MIN_HZ && uhz == 500000) { > > + idx = BMA400_ACC_ODR_MIN_RAW; > > + } else { > > + return -EINVAL; > > + } > > + > > + ret = regmap_read(data->regmap, BMA400_ACC_CONFIG1_REG, &val); > > + if (ret) > > + return ret; > > + > > + /* preserve the range and normal mode osr */ > > > + odr = idx | (~BMA400_ACC_ODR_MASK & val); > > Yoda style? Fixed in v8. > > + > > + ret = regmap_write(data->regmap, BMA400_ACC_CONFIG1_REG, odr); > > + if (ret) > > + return ret; > > + > > + bma400_output_data_rate_from_raw(idx, &data->sample_freq.hz, > > + &data->sample_freq.uhz); > > + return 0; > > +} > ... > > > +int bma400_accel_scale_to_raw(struct bma400_data *data, unsigned int val) > > +{ > > + int scale = val / BMA400_SCALE_MIN; > > + int raw; > > + > > + if (scale == 0) > > + return -EINVAL; > > + > > + raw = __ffs(scale); > > + > > + if (val % BMA400_SCALE_MIN || !is_power_of_2(scale)) > > + return -EINVAL; > > Ditto. > > > + > > + return raw; > > +} > > ... > > > +out: > > Make a little sense. Why not return directly? Mostly setup for the next patch in this patchset. > > + return ret; > > ... > > > + ret = bma400_init(data); > > + if (ret < 0) > > May it be positive value returned? Fixed in v8. Cheers, - Dan