[PATCH 3/9] iio: adc: ad_sigma_delta: use BITS_TO_BYTES() macro

David Lechner posted 9 patches 3 months, 2 weeks ago
There is a newer version of this series
[PATCH 3/9] iio: adc: ad_sigma_delta: use BITS_TO_BYTES() macro
Posted by David Lechner 3 months, 2 weeks ago
Use the BITS_TO_BYTES() macro instead of dividing by 8 to convert bits
to bytes.

This makes it more obvious what unit conversion is taking place.

In once instance, we also avoid the temporary assignment to a variable
as it was confusing that reg_size was being used with two different
units (bits and bytes).

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 drivers/iio/adc/ad_sigma_delta.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
index 1657f64f1c0465b249adcc8a70dda8faf4a90565..fa792c800c80f960aca75b28a60cb2588e69fe7d 100644
--- a/drivers/iio/adc/ad_sigma_delta.c
+++ b/drivers/iio/adc/ad_sigma_delta.c
@@ -7,6 +7,7 @@
  */
 
 #include <linux/align.h>
+#include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/interrupt.h>
@@ -190,7 +191,7 @@ int ad_sd_reset(struct ad_sigma_delta *sigma_delta)
 	unsigned int size;
 	int ret;
 
-	size = DIV_ROUND_UP(reset_length, 8);
+	size = BITS_TO_BYTES(reset_length);
 	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
@@ -419,7 +420,7 @@ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
 		data_reg = AD_SD_REG_DATA;
 
 	ret = ad_sd_read_reg(sigma_delta, data_reg,
-		DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
+		BITS_TO_BYTES(chan->scan_type.realbits + chan->scan_type.shift),
 		&raw_sample);
 
 out:
@@ -552,9 +553,8 @@ static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
 	unsigned int reg_size;
 	unsigned int data_reg;
 
-	reg_size = indio_dev->channels[0].scan_type.realbits +
-			indio_dev->channels[0].scan_type.shift;
-	reg_size = DIV_ROUND_UP(reg_size, 8);
+	reg_size = BITS_TO_BYTES(indio_dev->channels[0].scan_type.realbits +
+				 indio_dev->channels[0].scan_type.shift);
 
 	if (sigma_delta->info->data_reg != 0)
 		data_reg = sigma_delta->info->data_reg;
@@ -616,7 +616,7 @@ static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
 		}
 	}
 
-	sample_size = indio_dev->channels[0].scan_type.storagebits / 8;
+	sample_size = BITS_TO_BYTES(indio_dev->channels[0].scan_type.storagebits);
 	sample_pos = sample_size * sigma_delta->current_slot;
 	memcpy(&sigma_delta->samples_buf[sample_pos], data, sample_size);
 	sigma_delta->current_slot++;

-- 
2.43.0
Re: [PATCH 3/9] iio: adc: ad_sigma_delta: use BITS_TO_BYTES() macro
Posted by Andy Shevchenko 3 months, 2 weeks ago
On Fri, Jun 20, 2025 at 05:20:09PM -0500, David Lechner wrote:
> Use the BITS_TO_BYTES() macro instead of dividing by 8 to convert bits
> to bytes.
> 
> This makes it more obvious what unit conversion is taking place.
> 
> In once instance, we also avoid the temporary assignment to a variable
> as it was confusing that reg_size was being used with two different
> units (bits and bytes).

...

> -	size = DIV_ROUND_UP(reset_length, 8);
> +	size = BITS_TO_BYTES(reset_length);
>  	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
>  	if (!buf)
>  		return -ENOMEM;

Hmm... On the rough glance this sounds like some kind of bitmap
and hence bitmap_zalloc() (and other bitmap APIs), but I am not
sure.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH 3/9] iio: adc: ad_sigma_delta: use BITS_TO_BYTES() macro
Posted by David Lechner 3 months, 1 week ago
On 6/23/25 1:54 AM, Andy Shevchenko wrote:
> On Fri, Jun 20, 2025 at 05:20:09PM -0500, David Lechner wrote:
>> Use the BITS_TO_BYTES() macro instead of dividing by 8 to convert bits
>> to bytes.
>>
>> This makes it more obvious what unit conversion is taking place.
>>
>> In once instance, we also avoid the temporary assignment to a variable
>> as it was confusing that reg_size was being used with two different
>> units (bits and bytes).
> 
> ...
> 
>> -	size = DIV_ROUND_UP(reset_length, 8);
>> +	size = BITS_TO_BYTES(reset_length);
>>  	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
>>  	if (!buf)
>>  		return -ENOMEM;
> 
> Hmm... On the rough glance this sounds like some kind of bitmap
> and hence bitmap_zalloc() (and other bitmap APIs), but I am not
> sure.
> 

I looked at it, but I don't think it is worth changing. It is
really counting clock cycles, not bits.
Re: [PATCH 3/9] iio: adc: ad_sigma_delta: use BITS_TO_BYTES() macro
Posted by Jonathan Cameron 3 months, 2 weeks ago
On Fri, 20 Jun 2025 17:20:09 -0500
David Lechner <dlechner@baylibre.com> wrote:

> Use the BITS_TO_BYTES() macro instead of dividing by 8 to convert bits
> to bytes.
> 
> This makes it more obvious what unit conversion is taking place.
> 
> In once instance, we also avoid the temporary assignment to a variable

In one instance

(don't bother fixing that unless doing a v2 for some other reason)

> as it was confusing that reg_size was being used with two different
> units (bits and bytes).
> 
> Signed-off-by: David Lechner <dlechner@baylibre.com>
> ---
>  drivers/iio/adc/ad_sigma_delta.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
> index 1657f64f1c0465b249adcc8a70dda8faf4a90565..fa792c800c80f960aca75b28a60cb2588e69fe7d 100644
> --- a/drivers/iio/adc/ad_sigma_delta.c
> +++ b/drivers/iio/adc/ad_sigma_delta.c
> @@ -7,6 +7,7 @@
>   */
>  
>  #include <linux/align.h>
> +#include <linux/bitops.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
>  #include <linux/interrupt.h>
> @@ -190,7 +191,7 @@ int ad_sd_reset(struct ad_sigma_delta *sigma_delta)
>  	unsigned int size;
>  	int ret;
>  
> -	size = DIV_ROUND_UP(reset_length, 8);
> +	size = BITS_TO_BYTES(reset_length);
>  	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
>  	if (!buf)
>  		return -ENOMEM;
> @@ -419,7 +420,7 @@ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
>  		data_reg = AD_SD_REG_DATA;
>  
>  	ret = ad_sd_read_reg(sigma_delta, data_reg,
> -		DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
> +		BITS_TO_BYTES(chan->scan_type.realbits + chan->scan_type.shift),
>  		&raw_sample);
>  
>  out:
> @@ -552,9 +553,8 @@ static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
>  	unsigned int reg_size;
>  	unsigned int data_reg;
>  
> -	reg_size = indio_dev->channels[0].scan_type.realbits +
> -			indio_dev->channels[0].scan_type.shift;
> -	reg_size = DIV_ROUND_UP(reg_size, 8);
> +	reg_size = BITS_TO_BYTES(indio_dev->channels[0].scan_type.realbits +
> +				 indio_dev->channels[0].scan_type.shift);
>  
>  	if (sigma_delta->info->data_reg != 0)
>  		data_reg = sigma_delta->info->data_reg;
> @@ -616,7 +616,7 @@ static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
>  		}
>  	}
>  
> -	sample_size = indio_dev->channels[0].scan_type.storagebits / 8;
> +	sample_size = BITS_TO_BYTES(indio_dev->channels[0].scan_type.storagebits);
>  	sample_pos = sample_size * sigma_delta->current_slot;
>  	memcpy(&sigma_delta->samples_buf[sample_pos], data, sample_size);
>  	sigma_delta->current_slot++;
>