Re: [PATCH] iio: frequency: ad9832: Use FIELD_PREP macro to set bit fields

From: Marcelo Schmitt
Date: Sat Mar 15 2025 - 11:02:56 EST


Hi Siddharth,

Adding some comments inline. If what Jonathan suggested + what I suggest bellow
is still insufficient to avoid those lots of shifting to make up a write
command, then probably not worth it to change those as Jonathan said.

On 03/14, Siddharth Menon wrote:
> Refactor code to use the FIELD_PREP macro for setting bit fields
> instead of manual bit manipulation.
>
> Suggested-by: Marcelo Schmitt <marcelo.schmitt1@xxxxxxxxx>
> Signed-off-by: Siddharth Menon <simeddon@xxxxxxxxx>
> ---
> drivers/staging/iio/frequency/ad9832.c | 39 ++++++++++++++------------
> 1 file changed, 21 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
> index 140ee4f9c137..bbde1f0e84ff 100644
> --- a/drivers/staging/iio/frequency/ad9832.c
> +++ b/drivers/staging/iio/frequency/ad9832.c
> @@ -70,6 +70,9 @@
> #define AD9832_FREQ_BITS 32
> #define AD9832_PHASE_BITS 12
> #define RES_MASK(bits) ((1 << (bits)) - 1)
> +#define DATA_MASK 0xFF
> +#define CMD_MASK (0xF << CMD_SHIFT)
> +#define ADD_MASK (0xF << ADD_SHIFT)

We could have masks for each part of the write command. For example,

#define CMD_MASK GENMASK(15, 12)
#define ADD_MASK GENMASK(11, 8)
#define DATA_MASK GENMASK(7, 0)

>
> /**
> * struct ad9832_state - driver instance specific data
> @@ -139,18 +142,18 @@ static int ad9832_write_frequency(struct ad9832_state *st,
>
> regval = ad9832_calc_freqreg(clk_freq, fout);
>
> - st->freq_data[0] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
> - (addr << ADD_SHIFT) |
> - ((regval >> 24) & 0xFF));
> - st->freq_data[1] = cpu_to_be16((AD9832_CMD_FRE16BITSW << CMD_SHIFT) |
> - ((addr - 1) << ADD_SHIFT) |
> - ((regval >> 16) & 0xFF));
> - st->freq_data[2] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
> - ((addr - 2) << ADD_SHIFT) |
> - ((regval >> 8) & 0xFF));
> - st->freq_data[3] = cpu_to_be16((AD9832_CMD_FRE16BITSW << CMD_SHIFT) |
> - ((addr - 3) << ADD_SHIFT) |
> - ((regval >> 0) & 0xFF));
> + st->freq_data[0] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_FRE8BITSW) |
> + FIELD_PREP(ADD_MASK, addr) |
> + FIELD_PREP(DATA_MASK, (regval >> 24) & 0xFF));

Then we can use FIELD_PREP() in combination with the masks. Example,

st->freq_data[0] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_FRE8BITSW) |
FIELD_PREP(ADD_MASK, addr) |
FIELD_PREP(DATA_MASK, (regval >> 24));

> + st->freq_data[1] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_FRE16BITSW) |
> + FIELD_PREP(ADD_MASK, (addr - 1)) |
> + FIELD_PREP(DATA_MASK, (regval >> 16) & 0xFF));
> + st->freq_data[2] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_FRE8BITSW) |
> + FIELD_PREP(ADD_MASK, (addr - 2)) |
> + FIELD_PREP(DATA_MASK, (regval >> 8) & 0xFF));
> + st->freq_data[3] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_FRE16BITSW) |
> + FIELD_PREP(ADD_MASK, (addr - 3)) |
> + FIELD_PREP(DATA_MASK, (regval >> 0) & 0xFF));

If able to make regval an array as Jonathan suggested, the above may become
something like

for (i = 0; i < ARRAY_SIZE(st->freq_data); i++)
st->freq_data[i] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_FRE8BITSW) |
FIELD_PREP(ADD_MASK, addr) |
FIELD_PREP(DATA_MASK, (regval[i])));

Note the command alternates between AD9832_CMD_FRE8BITSW and
AD9832_CMD_FRE16BITSW so maybe also put the command constants into an array or
use some ternary operation like i % 2 == 0 ? AD9832_CMD_FRE8BITSW : AD9832_CMD_FRE16BITS.