Re: [PATCH 3/3] iio: light: Add support for ltrf216a sensor

From: Jonathan Cameron
Date: Tue Apr 12 2022 - 10:54:00 EST


On Tue, 12 Apr 2022 10:06:18 -0400
Gabriel Krisman Bertazi <krisman@xxxxxxxxxxxxx> wrote:

> Shreeya Patel <shreeya.patel@xxxxxxxxxxxxx> writes:
>
> >>> + val_1 = i2c_smbus_read_byte_data(data->client, addr + 1);
> >>> + val_2 = i2c_smbus_read_byte_data(data->client, addr + 2);
> >>> + ret = (val_2 << 16) + (val_1 << 8) + val_0;
> >> This is a le24_to_cpu() conversion.
> >> Preferred choice would be to use something like
> >> u8 buf[3];
> >> int i;
> >>
> >> for (i = 0; i < 3; i++) {
> >> ret = i2c_smbus_read_byte_data(data->client, addr);
> >> if (ret < 0)
> >> return ret;
> >> buf[i] = ret;
> >> }
> >> return le24_to_cpu(buf);
> >>
> >
> > We do not have any le24_to_cpu() function in current kernel source code.
> > I was thinking to use le32_to_cpu() instead but it requires an argument of
> > type __le32 and our case we storing the values in u8 buf[3] so I'm not
> > really sure if it's possible to use le32_to_cpu() or any other function.
>
> I guess you could make it a 32-bit buffer, keep the most
> significant byte zeroed and return le32_to_cpu:
>
> u8 buf[4];
>
> memset(buf, 0x0, sizeof(buf));
>
> for (i = 0; i < 3; i++) {
> ret = i2c_smbus_read_byte_data(data->client, addr);
> if (ret < 0)
> return ret;
> buf[i] = ret;
> }
> return le32_to_cpu(buf);

>
I was being silly. It's not aligned for obvious reasons that we don't do
24bit alignment, so you need
get_unaligned_le24()

Jonathan