Skip to contentSkip to conversation

iio: frequency: ad9832: Use FIELD_PREP macro to set bit fields

PATCH Superseded Siddharth Menon 3 participants 3 messages Mar 13, 2025 – Mar 15, 2025

Subsystems (4)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: frequency: ad9832: Use FIELD_PREP macro to set bit fields

Refactor code to use the FIELD_PREP macro for setting bit fields instead of manual bit manipulation.

---
 drivers/staging/iio/frequency/ad9832.c | 39 ++++++++++++++------------
 1 file changed, 21 insertions(+), 18 deletions(-)
drivers/staging/iio/frequency/ad9832.c +21 -18
index 140ee4f9c137..bbde1f0e84ff 100644
@@ -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)
 
 /**
  * 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));
+	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));
 
 	return spi_sync(st->spi, &st->freq_msg);
 }
@@ -161,12 +164,12 @@ static int ad9832_write_phase(struct ad9832_state *st,
 	if (phase >= BIT(AD9832_PHASE_BITS))
 		return -EINVAL;
 
-	st->phase_data[0] = cpu_to_be16((AD9832_CMD_PHA8BITSW << CMD_SHIFT) |
-					(addr << ADD_SHIFT) |
-					((phase >> 8) & 0xFF));
-	st->phase_data[1] = cpu_to_be16((AD9832_CMD_PHA16BITSW << CMD_SHIFT) |
-					((addr - 1) << ADD_SHIFT) |
-					(phase & 0xFF));
+	st->phase_data[0] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_PHA8BITSW) |
+					FIELD_PREP(ADD_MASK, addr) |
+					FIELD_PREP(DATA_MASK, (phase >> 8) & 0xFF));
+	st->phase_data[1] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_PHA16BITSW) |
+					FIELD_PREP(ADD_MASK, (addr - 1)) |
+					FIELD_PREP(DATA_MASK, phase & 0xFF));
 
 	return spi_sync(st->spi, &st->phase_msg);
 }
Signature · 1 line
2.48.1
#1 lore raw
Quoted 14 lines · On Fri, 14 Mar 2025 01:24:17 +0530 Siddharth Menon <[email protected]> wrote:

Refactor code to use the FIELD_PREP macro for setting bit fields instead of manual bit manipulation.

---
 drivers/staging/iio/frequency/ad9832.c | 39 ++++++++++++++------------
 1 file changed, 21 insertions(+), 18 deletions(-)
drivers/staging/iio/frequency/ad9832.c +0 -0
index 140ee4f9c137..bbde1f0e84ff 100644

drivers/staging/iio/frequency/ad9832.c line 75

Quoted 6 lines
drivers/staging/iio/frequency/ad9832.c +3 -0
@@ -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)

Don't mix and match. If we can't get rid of CMD_SHIFT then this is not worth ti.

Ideally should end up with
#define AD9832_CMD_MSK 0xF000
#define AD9832_ADD_MSK 0x0F00

Quoted 21 lines
drivers/staging/iio/frequency/ad9832.c +2 -12
 /**
  * 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) |

drivers/staging/iio/frequency/ad9832.c static int ad9832_write_frequency(struct ad9832_state *st, · line 156

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +10 -0
+					FIELD_PREP(DATA_MASK, (regval >> 24) & 0xFF));
+	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));

These are doing a byte wise write. Instead of this regval shifting madness look at converting to array with appropriate unaligned_put_be32
then a loop to do this.

Quoted 22 lines
drivers/staging/iio/frequency/ad9832.c +6 -6
 	return spi_sync(st->spi, &st->freq_msg);
 }
@@ -161,12 +164,12 @@ static int ad9832_write_phase(struct ad9832_state *st,
 	if (phase >= BIT(AD9832_PHASE_BITS))
 		return -EINVAL;
 
-	st->phase_data[0] = cpu_to_be16((AD9832_CMD_PHA8BITSW << CMD_SHIFT) |
-					(addr << ADD_SHIFT) |
-					((phase >> 8) & 0xFF));
-	st->phase_data[1] = cpu_to_be16((AD9832_CMD_PHA16BITSW << CMD_SHIFT) |
-					((addr - 1) << ADD_SHIFT) |
-					(phase & 0xFF));
+	st->phase_data[0] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_PHA8BITSW) |
+					FIELD_PREP(ADD_MASK, addr) |
+					FIELD_PREP(DATA_MASK, (phase >> 8) & 0xFF));
+	st->phase_data[1] = cpu_to_be16(FIELD_PREP(CMD_MASK, AD9832_CMD_PHA16BITSW) |
+					FIELD_PREP(ADD_MASK, (addr - 1)) |
+					FIELD_PREP(DATA_MASK, phase & 0xFF));
 
 	return spi_sync(st->spi, &st->phase_msg);
 }
#2 lore raw
In reply to Siddharth Menon

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.

Quoted 14 lines · Siddharth Menon

Refactor code to use the FIELD_PREP macro for setting bit fields instead of manual bit manipulation.

---
 drivers/staging/iio/frequency/ad9832.c | 39 ++++++++++++++------------
 1 file changed, 21 insertions(+), 18 deletions(-)
drivers/staging/iio/frequency/ad9832.c +0 -0
index 140ee4f9c137..bbde1f0e84ff 100644

drivers/staging/iio/frequency/ad9832.c line 75

Quoted 6 lines
drivers/staging/iio/frequency/ad9832.c +3 -0
@@ -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)

Quoted 12 lines
drivers/staging/iio/frequency/ad9832.c +0 -5
 /**
  * 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) |

drivers/staging/iio/frequency/ad9832.c static int ad9832_write_frequency(struct ad9832_state *st, · line 147

Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +3 -7
-					((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));

drivers/staging/iio/frequency/ad9832.c static int ad9832_write_frequency(struct ad9832_state *st, · line 156

Quoted 9 lines
drivers/staging/iio/frequency/ad9832.c +9 -0
+	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.