iio: frequency: ad9832: Use FIELD_PREP macro to set bit fields
Subsystems (4)
PATCH v5 1/1 iio: frequency: ad9832: Use FIELD_PREP macro to set bit fields
Use bitfield and bitmask macros to clearly specify AD9832 SPI command fields to make register write code more readable.
---
v1->v2:
- remove CMD_SHIFT and ADD_SHIFT
- use GENMASK
- store regval in an array and iterate through it v2->v3:
- add missing header
- refactor code in the previously introduced loops v3->v4:
- update commit message with a better one
- convert AD9832_PHASE and RES_MASK to masks
- cleanup a few if else blocks
v4->v5
- remove unnecessary inversion (val ? 0 : 1) used with AD9832_PHASE_MASK introduced in v4
- use ARRAY_SIZE instead of fixed integers
- use reverse xmas tree order
- align mask macros
drivers/staging/iio/frequency/ad9832.c | 85 +++++++++++++------------- 1 file changed, 44 insertions(+), 41 deletions(-)
drivers/staging/iio/frequency/ad9832.c +44 -41
@@ -16,6 +16,9 @@#include <linux/slab.h> #include <linux/spi/spi.h> #include <linux/sysfs.h> +#include <linux/bitfield.h> +#include <linux/bits.h> +#include <linux/unaligned.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h>
@@ -59,17 +62,18 @@#define AD9832_CMD_SLEEPRESCLR 0xC#define AD9832_FREQ BIT(11) -#define AD9832_PHASE (x) (((x) & 3) << 9) +#define AD9832_PHASE_MASK GENMASK(10, 9)#define AD9832_SYNC BIT(13)#define AD9832_SELSRC BIT(12)#define AD9832_SLEEP BIT(13)#define AD9832_RESET BIT(12)#define AD9832_CLR BIT(11) -#define CMD_SHIFT 12 -#define ADD_SHIFT 8#define AD9832_FREQ_BITS 32#define AD9832_PHASE_BITS 12 -#define RES_MASK (bits) ((1 << (bits)) - 1) +#define RES_MASK (bits) GENMASK((bits) - 1, 0) +#define AD9832_CMD_MSK GENMASK(15, 12) +#define AD9832_ADD_MSK GENMASK(11, 8) +#define AD9832_DAT_MSK GENMASK(7, 0)/** * struct ad9832_state - driver instance specific data
@@ -131,6 +135,8 @@ static int ad9832_write_frequency(struct ad9832_state *st, {unsigned long clk_freq;unsigned long regval; +u8 regval_bytes[4 ]; +u16 freq_cmd; clk_freq =clk_get_rate (st->mclk);
@@ -138,19 +144,15 @@ static int ad9832_write_frequency(struct ad9832_state *st,return -EINVAL ; regval =ad9832_calc_freqreg (clk_freq, fout); +put_unaligned_be32 (regval, regval_bytes); - 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 )); +for (int i =0 ; i <ARRAY_SIZE (regval_bytes); i++) { + freq_cmd = (i %2 ==0 ) ?AD9832_CMD_FRE8BITSW :AD9832_CMD_FRE16BITSW ; + + st->freq_data[i] =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK , freq_cmd) | +FIELD_PREP (AD9832_ADD_MSK , addr - i) | +FIELD_PREP (AD9832_DAT_MSK , regval_bytes[i])); + }return spi_sync (st->spi, &st->freq_msg); }
@@ -158,15 +160,21 @@ static int ad9832_write_frequency(struct ad9832_state *st,static int ad9832_write_phase (struct ad9832_state *st,unsigned long addr,unsigned long phase) { +u8 phase_bytes[2 ]; +u16 phase_cmd; +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 )); +put_unaligned_be16 (phase, phase_bytes); + +for (int i =0 ; i <ARRAY_SIZE (phase_bytes); i++) { + phase_cmd = (i %2 ==0 ) ?AD9832_CMD_PHA8BITSW :AD9832_CMD_PHA16BITSW ; + + st->phase_data[i] =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK , phase_cmd) | +FIELD_PREP (AD9832_ADD_MSK , addr - i) | +FIELD_PREP (AD9832_DAT_MSK , phase_bytes[i])); + }return spi_sync (st->spi, &st->phase_msg); }
@@ -197,24 +205,22 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, ret =ad9832_write_phase (st, this_attr->address, val);break ;case AD9832_PINCTRL_EN : -if (val) - st->ctrl_ss &= ~AD9832_SELSRC ; -else - st->ctrl_ss |=AD9832_SELSRC ; - st->data =cpu_to_be16 ((AD9832_CMD_SYNCSELSRC <<CMD_SHIFT ) | + st->ctrl_ss &= ~AD9832_SELSRC ; + st->ctrl_ss |=FIELD_PREP (AD9832_SELSRC , val ?0 :1 ); + + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SYNCSELSRC ) | st->ctrl_ss); ret =spi_sync (st->spi, &st->msg);break ;case AD9832_FREQ_SYM : -if (val ==1 ) { - st->ctrl_fp |=AD9832_FREQ ; - }else if (val ==0 ) { +if (val ==1 || val ==0 ) { st->ctrl_fp &= ~AD9832_FREQ ; + st->ctrl_fp |=FIELD_PREP (AD9832_FREQ , val ?0 :1 ); }else { ret = -EINVAL ;break ; } - st->data =cpu_to_be16 ((AD9832_CMD_FPSELECT <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_FPSELECT ) | st->ctrl_fp); ret =spi_sync (st->spi, &st->msg);break ;
@@ -224,21 +230,18 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,break ; } - st->ctrl_fp &= ~AD9832_PHASE (3 ); - st->ctrl_fp |=AD9832_PHASE (val); + st->ctrl_fp &= ~FIELD_PREP (AD9832_PHASE_MASK ,3 ); + st->ctrl_fp |=FIELD_PREP (AD9832_PHASE_MASK , val); - st->data =cpu_to_be16 ((AD9832_CMD_FPSELECT <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_FPSELECT ) | st->ctrl_fp); ret =spi_sync (st->spi, &st->msg);break ;case AD9832_OUTPUT_EN : -if (val) - st->ctrl_src &= ~(AD9832_RESET |AD9832_SLEEP | -AD9832_CLR ); -else - st->ctrl_src |=AD9832_RESET ; + st->ctrl_src &= ~(AD9832_RESET |AD9832_SLEEP |AD9832_CLR ); + st->ctrl_src |=FIELD_PREP (AD9832_RESET , val ?0 :1 ); - st->data =cpu_to_be16 ((AD9832_CMD_SLEEPRESCLR <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SLEEPRESCLR ) | st->ctrl_src); ret =spi_sync (st->spi, &st->msg);break ;
@@ -396,7 +399,7 @@ static int ad9832_probe(struct spi_device *spi)spi_message_add_tail (&st->phase_xfer[1 ], &st->phase_msg); st->ctrl_src =AD9832_SLEEP |AD9832_RESET |AD9832_CLR ; - st->data =cpu_to_be16 ((AD9832_CMD_SLEEPRESCLR <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SLEEPRESCLR ) | st->ctrl_src); ret =spi_sync (st->spi, &st->msg);if (ret) {
Signature · 1 line
2.49.0
Jonathan Cameron
maintainerQuoted 5 lines · On Sun, 30 Mar 2025 19:19:51 +0530 Siddharth Menon <[email protected]> wrote:
Use bitfield and bitmask macros to clearly specify AD9832 SPI command fields to make register write code more readable.
Suggested-by Marcelo Schmitt <[email protected]>Signed-off-by Siddharth Menon <[email protected]>
Hi Siddharth,
I took a closer look at the original data layout handling in here. It doesn't make much sense to me, so perhaps we should fix that up whilst making these other changes.
Jonathan
Quoted 26 lines
---
v1->v2:
- removeCMD_SHIFTandADD_SHIFT
- use GENMASK
- store regval in an array and iterate through itv2->v3:
- add missing header
- refactor code in the previously introduced loopsv3->v4:
- update commit message with a better one
- convertAD9832_PHASEandRES_MASKto masks
- cleanup a few if else blocks
v4->v5
- remove unnecessary inversion (val ? 0 : 1) used withAD9832_PHASE_MASKintroduced in v4
- useARRAY_SIZEinstead of fixed integers
- use reverse xmas tree order
- align mask macrosdrivers/staging/iio/frequency/ad9832.c | 85 +++++++++++++------------- 1 file changed, 44 insertions(+), 41 deletions(-)drivers/staging/iio/frequency/ad9832.c +0
-0
drivers/staging/iio/frequency/ad9832.c line 21
Quoted 6 lines
drivers/staging/iio/frequency/ad9832.c +3
-0@@ -16,6 +16,9 @@#include <linux/slab.h> #include <linux/spi/spi.h> #include <linux/sysfs.h> +#include <linux/bitfield.h> +#include <linux/bits.h> +#include <linux/unaligned.h>
Before this patch the includes were in alphabetical order. That's preferred for IIO patches in general, but in this case following local style makes that more important still.
Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +1
-1#include <linux/iio/iio.h> #include <linux/iio/sysfs.h> @@ -59,17 +62,18 @@#define AD9832_CMD_SLEEPRESCLR 0xC#define AD9832_FREQ BIT(11) -#define AD9832_PHASE (x) (((x) & 3) << 9) +#define AD9832_PHASE_MASK GENMASK(10, 9)#define AD9832_SYNC BIT(13)
drivers/staging/iio/frequency/ad9832.c line 73
Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +1
-3#define AD9832_SELSRC BIT(12)#define AD9832_SLEEP BIT(13)#define AD9832_RESET BIT(12)#define AD9832_CLR BIT(11) -#define CMD_SHIFT 12 -#define ADD_SHIFT 8#define AD9832_FREQ_BITS 32#define AD9832_PHASE_BITS 12 -#define RES_MASK (bits) ((1 << (bits)) - 1) +#define RES_MASK (bits) GENMASK((bits) - 1, 0)
Silly question. Is this used anywhere? Given FIELD_PREP etc tend not to work with non constant values I went looking to check this was constant and couldn't find any where it is used. As such just drop it instead!
Quoted 29 lines
drivers/staging/iio/frequency/ad9832.c +6
-8+#define AD9832_CMD_MSK GENMASK(15, 12) +#define AD9832_ADD_MSK GENMASK(11, 8) +#define AD9832_DAT_MSK GENMASK(7, 0)/** * struct ad9832_state - driver instance specific data @@ -131,6 +135,8 @@ static int ad9832_write_frequency(struct ad9832_state *st, {unsigned long clk_freq;unsigned long regval; +u8 regval_bytes[4 ]; +u16 freq_cmd; clk_freq =clk_get_rate (st->mclk);@@ -138,19 +144,15 @@ static int ad9832_write_frequency(struct ad9832_state *st,return -EINVAL ; regval =ad9832_calc_freqreg (clk_freq, fout); +put_unaligned_be32 (regval, regval_bytes); - 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) |
drivers/staging/iio/frequency/ad9832.c static int ad9832_write_frequency(struct ad9832_state *st, · line 154
Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +6
-4- ((regval >> 8) & 0xFF)); - st->freq_data[3 ] =cpu_to_be16 ((AD9832_CMD_FRE16BITSW <<CMD_SHIFT ) | - ((addr -3 ) <<ADD_SHIFT ) | - ((regval >>0 ) &0xFF )); +for (int i =0 ; i <ARRAY_SIZE (regval_bytes); i++) { + freq_cmd = (i %2 ==0 ) ?AD9832_CMD_FRE8BITSW :AD9832_CMD_FRE16BITSW ; + + st->freq_data[i] =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK , freq_cmd) | +FIELD_PREP (AD9832_ADD_MSK , addr - i) | +FIELD_PREP (AD9832_DAT_MSK , regval_bytes[i]));
Looking at the data layout here, this seems like an interesting dance to fill two unrelated u8 values - it's not a be16 at all.
I'd be tempted to split the freq_data into u8s and then you will just have
st->freq_data[i][0] = FIELD_PREP(AD9832_CMD_MSK, freq_cmd) | FIELD_PREP(AD9832_ADD_SMK, addr - i); //with masks adjusted appropriately. st->freq_data[i][1] = regval_bytes[i];
Quoted 18 lines
drivers/staging/iio/frequency/ad9832.c +4
-4+ }return spi_sync (st->spi, &st->freq_msg); }@@ -158,15 +160,21 @@ static int ad9832_write_frequency(struct ad9832_state *st,static int ad9832_write_phase (struct ad9832_state *st,unsigned long addr,unsigned long phase) { +u8 phase_bytes[2 ]; +u16 phase_cmd; +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) |
drivers/staging/iio/frequency/ad9832.c static int ad9832_write_frequency(struct ad9832_state *st, · line 176
Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +8
-2- ((addr -1 ) <<ADD_SHIFT ) | - (phase & 0xFF)); +put_unaligned_be16 (phase, phase_bytes); + +for (int i =0 ; i <ARRAY_SIZE (phase_bytes); i++) { + phase_cmd = (i %2 ==0 ) ?AD9832_CMD_PHA8BITSW :AD9832_CMD_PHA16BITSW ; + + st->phase_data[i] =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK , phase_cmd) | +FIELD_PREP (AD9832_ADD_MSK , addr - i) | +FIELD_PREP (AD9832_DAT_MSK , phase_bytes[i]));
Similarly.
Quoted 73 lines
drivers/staging/iio/frequency/ad9832.c +15
-19+ }return spi_sync (st->spi, &st->phase_msg); }@@ -197,24 +205,22 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, ret =ad9832_write_phase (st, this_attr->address, val);break ;case AD9832_PINCTRL_EN : -if (val) - st->ctrl_ss &= ~AD9832_SELSRC ; -else - st->ctrl_ss |=AD9832_SELSRC ; - st->data =cpu_to_be16 ((AD9832_CMD_SYNCSELSRC <<CMD_SHIFT ) | + st->ctrl_ss &= ~AD9832_SELSRC ; + st->ctrl_ss |=FIELD_PREP (AD9832_SELSRC , val ?0 :1 ); + + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SYNCSELSRC ) | st->ctrl_ss); ret =spi_sync (st->spi, &st->msg);break ;case AD9832_FREQ_SYM : -if (val ==1 ) { - st->ctrl_fp |=AD9832_FREQ ; - }else if (val ==0 ) { +if (val ==1 || val ==0 ) { st->ctrl_fp &= ~AD9832_FREQ ; + st->ctrl_fp |=FIELD_PREP (AD9832_FREQ , val ?0 :1 ); }else { ret = -EINVAL ;break ; } - st->data =cpu_to_be16 ((AD9832_CMD_FPSELECT <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_FPSELECT ) | st->ctrl_fp); ret =spi_sync (st->spi, &st->msg);break ;@@ -224,21 +230,18 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,break ; } - st->ctrl_fp &= ~AD9832_PHASE (3 ); - st->ctrl_fp |=AD9832_PHASE (val); + st->ctrl_fp &= ~FIELD_PREP (AD9832_PHASE_MASK ,3 ); + st->ctrl_fp |=FIELD_PREP (AD9832_PHASE_MASK , val); - st->data =cpu_to_be16 ((AD9832_CMD_FPSELECT <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_FPSELECT ) | st->ctrl_fp); ret =spi_sync (st->spi, &st->msg);break ;case AD9832_OUTPUT_EN : -if (val) - st->ctrl_src &= ~(AD9832_RESET |AD9832_SLEEP | -AD9832_CLR ); -else - st->ctrl_src |=AD9832_RESET ; + st->ctrl_src &= ~(AD9832_RESET |AD9832_SLEEP |AD9832_CLR ); + st->ctrl_src |=FIELD_PREP (AD9832_RESET , val ?0 :1 ); - st->data =cpu_to_be16 ((AD9832_CMD_SLEEPRESCLR <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SLEEPRESCLR ) | st->ctrl_src); ret =spi_sync (st->spi, &st->msg);break ;@@ -396,7 +399,7 @@ static int ad9832_probe(struct spi_device *spi)spi_message_add_tail (&st->phase_xfer[1 ], &st->phase_msg); st->ctrl_src =AD9832_SLEEP |AD9832_RESET |AD9832_CLR ; - st->data =cpu_to_be16 ((AD9832_CMD_SLEEPRESCLR <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SLEEPRESCLR ) | st->ctrl_src); ret =spi_sync (st->spi, &st->msg);if (ret) {
Quoted 6 lines · Jonathan Cameron
drivers/staging/iio/frequency/ad9832.c static int ad9832_write_frequency(struct ad9832_state *st, · line 154
Quoted 6 lines
drivers/staging/iio/frequency/ad9832.c +6
-0+for (int i =0 ; i <ARRAY_SIZE (regval_bytes); i++) { + freq_cmd = (i %2 ==0 ) ?AD9832_CMD_FRE8BITSW :AD9832_CMD_FRE16BITSW ; + + st->freq_data[i] =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK , freq_cmd) | +FIELD_PREP (AD9832_ADD_MSK , addr - i) | +FIELD_PREP (AD9832_DAT_MSK , regval_bytes[i]));
Quoted 9 lines
Looking at the data layout here, this seems like an interesting dance to fill two unrelated u8 values - it's not a be16 at all.
I'd be tempted to split the
freq_datainto u8s and then you will just havest->freq_data[i][0] = FIELD_PREP(AD9832_CMD_MSK, freq_cmd) | FIELD_PREP(AD9832_ADD_SMK, addr - i); //with masks adjusted appropriately. st->freq_data[i][1] = regval_bytes[i];
Hello Jonathan,
I briefly went through the datasheet for the device.
From what I understand, the device is expecting 16 bit write operations where:
- First 4 bits: Operation type (frequency/phase)
- Next 4 bits: Destination register address
- Last 8 bits: Data
so these fields would need to be combined into a single 16-bit value regardless.
As I am unable to procure a testing unit at this time, I’m hesitant to make changes that could unintentionally break the existing driver.
Would it be acceptable to limit the scope of this patch to introducing bitfield macros and addressing the remaining feedback?
Regards,
Siddharth Menon
Jonathan Cameron
maintainerQuoted 17 lines · On Wed, 9 Apr 2025 01:25:52 +0530 Siddharth Menon <[email protected]> wrote:
Quoted 6 lines · Jonathan Cameron
drivers/staging/iio/frequency/ad9832.c static int ad9832_write_frequency(struct ad9832_state *st, · line 154
Quoted 6 lines
drivers/staging/iio/frequency/ad9832.c +6
-0+for (int i =0 ; i <ARRAY_SIZE (regval_bytes); i++) { + freq_cmd = (i %2 ==0 ) ?AD9832_CMD_FRE8BITSW :AD9832_CMD_FRE16BITSW ; + + st->freq_data[i] =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK , freq_cmd) | +FIELD_PREP (AD9832_ADD_MSK , addr - i) | +FIELD_PREP (AD9832_DAT_MSK , regval_bytes[i]));Quoted 9 lines
Looking at the data layout here, this seems like an interesting dance to fill two unrelated u8 values - it's not a be16 at all.
I'd be tempted to split the
freq_datainto u8s and then you will just havest->freq_data[i][0] = FIELD_PREP(AD9832_CMD_MSK, freq_cmd) | FIELD_PREP(AD9832_ADD_SMK, addr - i); //with masks adjusted appropriately. st->freq_data[i][1] = regval_bytes[i];
Quoted 8 lines
Hello Jonathan,
I briefly went through the datasheet for the device.
From what I understand, the device is expecting 16 bit write operations where:
- First 4 bits: Operation type (frequency/phase)
- Next 4 bits: Destination register address
- Last 8 bits: Data
so these fields would need to be combined into a single 16-bit value regardless.
Hmm. That is really a documentation thing rather than anything real. If they had been documented it as a control value of 8 bits and a data value of 8 bits then it would naturally map to an array.
Quoted 3 lines
As I am unable to procure a testing unit at this time, I’m hesitant to make changes that could unintentionally break the existing driver.
Sure. It is always a bit of a risk assessment for changes like this. I'm less nervous about breaking staging drivers than others, but we should still do our best to not do so. Probably not worth spinning up some emulation for this change!
Quoted 3 lines
Would it be acceptable to limit the scope of this patch to introducing bitfield macros and addressing the remaining feedback?
Sure. We can perhaps revisit this suggestion in a future series.
Jonathan
Quoted 4 lines
Regards,
Siddharth Menon
Hi Siddharth,
Some suggestions in addition to what Jonathan has provided in his review.
Quoted 47 lines · Siddharth Menon
Use bitfield and bitmask macros to clearly specify AD9832 SPI command fields to make register write code more readable.
Suggested-by Marcelo Schmitt <[email protected]>Signed-off-by Siddharth Menon <[email protected]>---
v1->v2:
- removeCMD_SHIFTandADD_SHIFT
- use GENMASK
- store regval in an array and iterate through itv2->v3:
- add missing header
- refactor code in the previously introduced loopsv3->v4:
- update commit message with a better one
- convertAD9832_PHASEandRES_MASKto masks
- cleanup a few if else blocks
v4->v5
- remove unnecessary inversion (val ? 0 : 1) used withAD9832_PHASE_MASKintroduced in v4
- useARRAY_SIZEinstead of fixed integers
- use reverse xmas tree order
- align mask macrosdrivers/staging/iio/frequency/ad9832.c | 85 +++++++++++++------------- 1 file changed, 44 insertions(+), 41 deletions(-)drivers/staging/iio/frequency/ad9832.c +4
-1@@ -16,6 +16,9 @@#include <linux/slab.h> #include <linux/spi/spi.h> #include <linux/sysfs.h> +#include <linux/bitfield.h> +#include <linux/bits.h> +#include <linux/unaligned.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h> @@ -59,17 +62,18 @@#define AD9832_CMD_SLEEPRESCLR 0xC#define AD9832_FREQ BIT(11) -#define AD9832_PHASE (x) (((x) & 3) << 9) +#define AD9832_PHASE_MASK GENMASK(10, 9)#define AD9832_SYNC BIT(13)
drivers/staging/iio/frequency/ad9832.c line 73
Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +1
-3#define AD9832_SELSRC BIT(12)#define AD9832_SLEEP BIT(13)#define AD9832_RESET BIT(12)#define AD9832_CLR BIT(11) -#define CMD_SHIFT 12 -#define ADD_SHIFT 8#define AD9832_FREQ_BITS 32#define AD9832_PHASE_BITS 12 -#define RES_MASK (bits) ((1 << (bits)) - 1) +#define RES_MASK (bits) GENMASK((bits) - 1, 0)
I also don't see RES_MASK being used by ad9832 so just drop it.
Quoted 4 lines
drivers/staging/iio/frequency/ad9832.c +3
-0+#define AD9832_CMD_MSK GENMASK(15, 12) +#define AD9832_ADD_MSK GENMASK(11, 8) +#define AD9832_DAT_MSK GENMASK(7, 0)
...
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 218
Quoted 7 lines
drivers/staging/iio/frequency/ad9832.c +2
-3case AD9832_FREQ_SYM : -if (val ==1 ) { - st->ctrl_fp |=AD9832_FREQ ; - }else if (val ==0 ) { +if (val ==1 || val ==0 ) { st->ctrl_fp &= ~AD9832_FREQ ; + st->ctrl_fp |=FIELD_PREP (AD9832_FREQ , val ?0 :1 );
The previous implementation would set ctrl_fp if val == 1 and unset it if val == 0. This patch seems to be doing the reverse (setting ctrl_fp if val == 0, and unsetting it if val != 0). Was the previous implementation potentially buggy? If not, I think we can just do
st->ctrl_fp &= ~AD9832_FREQ; st->ctrl_fp |= FIELD_PREP(AD9832_FREQ, !!val);
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 222
Quoted 4 lines
drivers/staging/iio/frequency/ad9832.c +0
-0}else { ret = -EINVAL ;break ; }
and drop the error path. Slight change in interface but guess no problem with that.
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 234
Quoted 13 lines
drivers/staging/iio/frequency/ad9832.c +3
-3- st->data =cpu_to_be16 ((AD9832_CMD_FPSELECT <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_FPSELECT ) | st->ctrl_fp); ret =spi_sync (st->spi, &st->msg);break ;@@ -224,21 +230,18 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,break ; } - st->ctrl_fp &= ~AD9832_PHASE (3 ); - st->ctrl_fp |=AD9832_PHASE (val); + st->ctrl_fp &= ~FIELD_PREP (AD9832_PHASE_MASK ,3 ); + st->ctrl_fp |=FIELD_PREP (AD9832_PHASE_MASK , val);
This seems to be a typical modify use case as exemplified in include/linux/bitfield.h. So,
st->ctrl_fp &= ~AD9832_PHASE_MASK; st->ctrl_fp |= FIELD_PREP(AD9832_PHASE_MASK, val);
Quoted 4 lines
drivers/staging/iio/frequency/ad9832.c +1
-1- st->data =cpu_to_be16 ((AD9832_CMD_FPSELECT <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_FPSELECT ) | st->ctrl_fp);
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 242
Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +2
-5ret =spi_sync (st->spi, &st->msg);break ;case AD9832_OUTPUT_EN : -if (val) - st->ctrl_src &= ~(AD9832_RESET |AD9832_SLEEP | -AD9832_CLR ); -else - st->ctrl_src |=AD9832_RESET ; + st->ctrl_src &= ~(AD9832_RESET |AD9832_SLEEP |AD9832_CLR ); + st->ctrl_src |=FIELD_PREP (AD9832_RESET , val ?0 :1 );
Hmm, this is modifying behavior. AD9832_SLEEP and AD9832_CLR were only being modified if something other than 0 was written to output enable sysfs file. Is the patch code mode appropriate than how the driver was before?
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 247
Quoted 6 lines
drivers/staging/iio/frequency/ad9832.c +1
-1- st->data =cpu_to_be16 ((AD9832_CMD_SLEEPRESCLR <<CMD_SHIFT ) | + st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SLEEPRESCLR ) | st->ctrl_src); ret =spi_sync (st->spi, &st->msg);break ;
Regards,
Marcelo
Quoted 3 lines · Marcelo Schmitt
The previous implementation would set
ctrl_fpif val == 1 and unset it if val == 0. This patch seems to be doing the reverse (settingctrl_fpif val == 0, and unsetting it if val != 0). Was the previous implementation potentially buggy?
My apologies, I seem to have made a mistake here.
Quoted 3 lines
Hmm, this is modifying behavior.
AD9832_SLEEPandAD9832_CLRwere only being modified if something other than 0 was written to output enable sysfs file. Is the patch code mode appropriate than how the driver was before?
I shall address the aforementioned issues along with the ones pointed out by Jonathan in my next patch.
Thank you for taking the time to review my patches.
Regards,
Siddharth Menon
Quoted 7 lines · Siddharth Menon
Quoted 3 lines · Marcelo Schmitt
The previous implementation would set
ctrl_fpif val == 1 and unset it if val == 0. This patch seems to be doing the reverse (settingctrl_fpif val == 0, and unsetting it if val != 0). Was the previous implementation potentially buggy?My apologies, I seem to have made a mistake here.
No worries. It's okay to change/update device drivers to make them better (e.g. implement new features, fix bugs). If the proposed change was fixing something, then it could be put into a separate patch with a Fixes tag. Though, I had another look at the datasheet and my understanding is that the freq bit (bit 11) should be set if the user writes 1 to out_altvoltageX_frequencysymbol (ABI documented in drivers/staging/iio/Documentation/sysfs-bus-iio-dds). By the way, I now think my suggestion of doingst->ctrl_fp |= FIELD_PREP(AD9832_FREQ, !!val) was not a good idea since that wouldn't follow the proposed ABI.
Anyway, it's okay to propose more changes/patches if you want. Just separate the patches according to their logical change if you make more changes to the driver.
Regards,
Marcelo