Re: [PATCH v3 7/7] iio: accel: Add support for Kionix/ROHM KX132-1211 accelerometer

Jonathan Cameron
2023-05-01T14:48:59Z

> > +static int kx132_get_fifo_bytes(struct kx022a_data *data)
> > +{
> > + struct device *dev = regmap_get_device(data->regmap);
> > + __le16 buf_status;
> > + int ret, fifo_bytes;
> > +
> > + ret = regmap_bulk_read(data->regmap, data->chip_info->buf_status1,
> > + &buf_status, sizeof(buf_status));
> > + if (ret) {
> > + dev_err(dev, "Error reading buffer status\n");
> > + return ret;
> > + }
> > +
> > + fifo_bytes = le16_to_cpu(buf_status);
> > + fifo_bytes &= data->chip_info->buf_smp_lvl_mask;
>
> This is probably just my limitation but I've hard time thinking how this
> works out on BE machines. It'd be much easier for me to understand this
> if the data was handled as two u8 values and mask was applied before
> endianes conversion. (Eg - untested pseudo code follows;
>
> __le16 buf_status;
> u8 *reg_data;
>
> ...
>
> ret = regmap_bulk_read(data->regmap, data->chip_info->buf_status1,
> &buf_status, sizeof(buf_status));
> ...
>
> reg_data = (u8 *)&buf_status;
>
> /* Clear the unused bits form 2.nd reg */
> reg_data[1] = reg_data[i] & MASK_SMP_LVL_REG_HIGH_BITS;
>
> /* Convert to CPU endianess */
> fifo_bytes = le16_to_cpu(buf_status);
>
> Well, others may have different view on this :)

:)

I go the other way. It's less obvious to me that it is appropriate
to apply le16_to_cpu(buf_status) after applying a mask to some
bits. The moment that is appropriate, then we certainly hope a single
mask application is as well.

I think treating it as a 16 bit register is appropriate, in particular
as the field is described as SMP_LEV[9:0] on the datasheet
(of course there are datasheets that do that for unconnected sets of
bits so this doesn't always work ;)

Jonathan