Linux IIO development
 help / color / mirror / Atom feed
* [PATCH v2] iio: light: veml3328: reshape scale array for readability
@ 2026-07-30  8:29 Giorgi Tchankvetadze
  2026-07-30  9:06 ` Joshua Crofts
  0 siblings, 1 reply; 2+ messages in thread
From: Giorgi Tchankvetadze @ 2026-07-30  8:29 UTC (permalink / raw)
  To: joshua.crofts1, jic23
  Cc: dlechner, nuno.sa, andy, linux-iio, linux-kernel,
	Giorgi Tchankvetadze

From: Giorgi Tchankvetadze <giorgi@tchankvetadze.com>

The veml3328_scale_vals array is declared as a flattened [4][8] array,
so accessing a scale value requires calculating the offset of its
(val, val2) pair using gain_inx * 2.

Reshape the array as [4][4][2], with separate dimensions for integration
time, gain and the scale value pair. This removes the manual stride
calculation and makes the relationship between the indexes and values
explicit.

Suggested-by: David Lechner <dlechner@baylibre.com>
Signed-off-by: Giorgi Tchankvetadze <giorgi@tchankvetadze.com>
---
Changes in v2:
- Reshape veml3328_scale_vals as [4][4][2], as suggested by
  David Lechner.
- Update read and write paths to access the scale value pair directly.

Link to v1:
https://lore.kernel.org/r/20260727112726.211344-1-giorgitchankvetadze1997@gmail.com

 drivers/iio/light/veml3328.c | 39 +++++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/light/veml3328.c b/drivers/iio/light/veml3328.c
index 7ff1753925c4..ef0bb82e54e0 100644
--- a/drivers/iio/light/veml3328.c
+++ b/drivers/iio/light/veml3328.c
@@ -91,11 +91,31 @@ static const struct iio_chan_spec veml3328_channels[] = {
  * Gain indexes: 0 (x0.5), 1 (x1), 2 (x2), 3 (x4)
  * IT indexes: 0 (50ms), 1 (100ms), 2 (200ms), 3 (400ms)
  */
-static const int veml3328_scale_vals[4][8] = {
-	{ 0, 768000, 0, 384000, 0, 192000, 0, 96000 },
-	{ 0, 384000, 0, 192000, 0, 96000,  0, 48000 },
-	{ 0, 192000, 0, 96000,  0, 48000,  0, 24000 },
-	{ 0, 96000,  0, 48000,  0, 24000,  0, 12000 },
+static const int veml3328_scale_vals[4][4][2] = {
+	{ /* 50 ms */
+		{ 0, 768000 },
+		{ 0, 384000 },
+		{ 0, 192000 },
+		{ 0, 96000 },
+	},
+	{ /* 100 ms */
+		{ 0, 384000 },
+		{ 0, 192000 },
+		{ 0, 96000 },
+		{ 0, 48000 },
+	},
+	{ /* 200 ms */
+		{ 0, 192000 },
+		{ 0, 96000 },
+		{ 0, 48000 },
+		{ 0, 24000 },
+	},
+	{ /* 400 ms */
+		{ 0, 96000 },
+		{ 0, 48000 },
+		{ 0, 24000 },
+		{ 0, 12000 },
+	},
 };
 
 /* integration times in microseconds */
@@ -184,9 +204,8 @@ static int veml3328_read_raw(struct iio_dev *indio_dev,
 		if (it_inx >= ARRAY_SIZE(veml3328_it_times) || gain_inx >= 4)
 			return -EINVAL;
 
-		/* Stride by 2 through the flattened array to match (val, val2) */
-		*val = veml3328_scale_vals[it_inx][gain_inx * 2];
-		*val2 = veml3328_scale_vals[it_inx][gain_inx * 2 + 1];
+		*val = veml3328_scale_vals[it_inx][gain_inx][0];
+		*val2 = veml3328_scale_vals[it_inx][gain_inx][1];
 
 		return IIO_VAL_INT_PLUS_MICRO;
 
@@ -282,8 +301,8 @@ static int veml3328_write_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 
 		for (i = 0; i < 4; i++) {
-			if (val == veml3328_scale_vals[it_inx][i * 2] &&
-			    val2 == veml3328_scale_vals[it_inx][i * 2 + 1])
+			if (val == veml3328_scale_vals[it_inx][i][0] &&
+			    val2 == veml3328_scale_vals[it_inx][i][1])
 				break;
 		}
 
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] iio: light: veml3328: reshape scale array for readability
  2026-07-30  8:29 [PATCH v2] iio: light: veml3328: reshape scale array for readability Giorgi Tchankvetadze
@ 2026-07-30  9:06 ` Joshua Crofts
  0 siblings, 0 replies; 2+ messages in thread
From: Joshua Crofts @ 2026-07-30  9:06 UTC (permalink / raw)
  To: Giorgi Tchankvetadze
  Cc: jic23, dlechner, nuno.sa, andy, linux-iio, linux-kernel,
	Giorgi Tchankvetadze

On Thu, 30 Jul 2026 12:29:34 +0400
Giorgi Tchankvetadze <giorgitchankvetadze1997@gmail.com> wrote:

> From: Giorgi Tchankvetadze <giorgi@tchankvetadze.com>
> 
> The veml3328_scale_vals array is declared as a flattened [4][8] array,
> so accessing a scale value requires calculating the offset of its
> (val, val2) pair using gain_inx * 2.
> 
> Reshape the array as [4][4][2], with separate dimensions for integration
> time, gain and the scale value pair. This removes the manual stride
> calculation and makes the relationship between the indexes and values
> explicit.
> 
> Suggested-by: David Lechner <dlechner@baylibre.com>
> Signed-off-by: Giorgi Tchankvetadze <giorgi@tchankvetadze.com>
> ---

Okay, this is better than my original implementation. No idea what I was
doing...

Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>

Thanks for improving my driver!

>  /* integration times in microseconds */
> @@ -184,9 +204,8 @@ static int veml3328_read_raw(struct iio_dev *indio_dev,
>  		if (it_inx >= ARRAY_SIZE(veml3328_it_times) || gain_inx >= 4)
>  			return -EINVAL;
>  
> -		/* Stride by 2 through the flattened array to match (val, val2) */
> -		*val = veml3328_scale_vals[it_inx][gain_inx * 2];
> -		*val2 = veml3328_scale_vals[it_inx][gain_inx * 2 + 1];
> +		*val = veml3328_scale_vals[it_inx][gain_inx][0];
> +		*val2 = veml3328_scale_vals[it_inx][gain_inx][1];
>  
>  		return IIO_VAL_INT_PLUS_MICRO;
>  
> @@ -282,8 +301,8 @@ static int veml3328_write_raw(struct iio_dev *indio_dev,
>  			return -EINVAL;
>  
>  		for (i = 0; i < 4; i++) {

Given how annoying hard-coded values are, could you change the `i < 4` to
`i < ARRAY_SIZE(veml3328_scale_vals[it_inx])`? This should also be done for 
the subsequent -EINVAL check as well. Feel free to do a separate patch.

> -			if (val == veml3328_scale_vals[it_inx][i * 2] &&
> -			    val2 == veml3328_scale_vals[it_inx][i * 2 + 1])
> +			if (val == veml3328_scale_vals[it_inx][i][0] &&
> +			    val2 == veml3328_scale_vals[it_inx][i][1])
>  				break;
>  		}
>  



-- 
Kind regards,
Joshua Crofts

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-30  9:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-30  8:29 [PATCH v2] iio: light: veml3328: reshape scale array for readability Giorgi Tchankvetadze
2026-07-30  9:06 ` Joshua Crofts

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox