Re: [PATCH v2 2/3] iio: dac: ad5706r: Add support for AD5706R DAC

From: Andy Shevchenko

Date: Thu Mar 12 2026 - 09:05:13 EST


On Thu, Mar 12, 2026 at 02:32:21AM +0000, Torreno, Alexis Czezar wrote:

...

> > > + st->tx_buf = cpu_to_be32((((u32)reg) << 16) |
> > > + ((u32)val << (16 - (num_bytes * 8))));
> >
> > What the heck is this?
>
> I'll make this more readable.

The problems with the above are:
- macro style (it's not a macro, no need so many parentheses)
- unneeded castings (C promotes types to int)
- "clever" compression of the standard patterns (see below)

> but the format goal is:
>
> if num_bytes = 1
> tx_buf [31:0] = [reg 31:16] [val 15:8] [ XXXX]
> if num_bytes = 2
> tx_buf [31:0] = [reg 31:16] [ val 15:0 ]

Assuming tx->buf is of u8:

put_unaligned_be16(reg, &st->tx_buf[0]);
if ( == 1)
st->tx_buf[2] = val;
else if ( == 2)
put_unaligned_be16(val, &st->tx_buf[2]);
else
ERR!

(can also be switch-case, but it will be more verbose).

--
With Best Regards,
Andy Shevchenko