Re: [PATCH v4 4/4] iio: accel: adxl345: add sampling frequency support

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

 



On Mon, Jun 25, 2018 at 6:22 PM, Akinobu Mita <akinobu.mita@xxxxxxxxx> wrote:
> The ADXL345 provides selectable output data rate.  This adds the iio
> channel information for the sampling frequency with that feature.

> +#define ADXL345_BW_RATE                        GENMASK(3, 0)
> +#define ADXL345_BASE_RATE_NANO_HZ      97656250LL

> +               samp_freq_nhz = ADXL345_BASE_RATE_NANO_HZ <<
> +                               (regval & ADXL345_BW_RATE);
> +               *val = div_s64_rem(samp_freq_nhz, 1000000000, val2);

Yep, looks cool!

> +               for (i = 0; i <= ADXL345_BW_RATE; i++) {
> +                       int nhz;
> +                       int hz;
> +
> +                       hz = div_s64_rem(ADXL345_BASE_RATE_NANO_HZ << i,
> +                                        1000000000, &nhz);
> +                       if (hz == val && nhz == val2) {
> +                               return regmap_update_bits(data->regmap,
> +                                                       ADXL345_REG_BW_RATE,
> +                                                       ADXL345_BW_RATE, i);
> +                       }
> +               }

But this one can be just loop-less.

Something like

res = div_s64_rem(val * 1000000000 + val2, ADXL345_BASE_RATE_NANO_HZ, &rem);
if (rem ...) // here to decide what margin is allowed, or choose
closest, or choose lower / higher.
 return regmap...(..., res);

And I guess you might want to use NSEC_PER_SEC. (Is it really period
or frequency? If latter, perhaps add HZ_PER_GHZ macro, we have in one
driver HZ_PER_MHZ, maybe in the future they would be consolidated
somewhere).

-- 
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux