[PATCH] iio: light: ltr501: return proper error code from ltr501_get_gain_index()

Antoniu Miclaus posted 1 patch 1 week, 1 day ago
There is a newer version of this series
drivers/iio/light/ltr501.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] iio: light: ltr501: return proper error code from ltr501_get_gain_index()
Posted by Antoniu Miclaus 1 week, 1 day ago
Return -EINVAL instead of -1 when no matching gain value is found
in the gain table. Using standard kernel error codes ensures
consistency and proper error handling.

Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
---
 drivers/iio/light/ltr501.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 022e0693983b..29e93986dea6 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -754,7 +754,7 @@ static int ltr501_get_gain_index(const struct ltr501_gain *gain, int size,
 		if (val == gain[i].scale && val2 == gain[i].uscale)
 			return i;
 
-	return -1;
+	return -EINVAL;
 }
 
 static int __ltr501_write_raw(struct iio_dev *indio_dev,
-- 
2.43.0
Re: [PATCH] iio: light: ltr501: return proper error code from ltr501_get_gain_index()
Posted by Jonathan Cameron 1 week ago
On Fri, 30 Jan 2026 15:52:38 +0200
Antoniu Miclaus <antoniu.miclaus@analog.com> wrote:

> Return -EINVAL instead of -1 when no matching gain value is found
> in the gain table. Using standard kernel error codes ensures
> consistency and proper error handling.
> 
> Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
> ---
>  drivers/iio/light/ltr501.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
> index 022e0693983b..29e93986dea6 100644
> --- a/drivers/iio/light/ltr501.c
> +++ b/drivers/iio/light/ltr501.c
> @@ -754,7 +754,7 @@ static int ltr501_get_gain_index(const struct ltr501_gain *gain, int size,
>  		if (val == gain[i].scale && val2 == gain[i].uscale)
>  			return i;
>  
> -	return -1;
> +	return -EINVAL;

Look at how this is used. What you have here isn't wrong but I'd rather
we returned the error this function provides than overwrite -EINVAL
with -EINVAL at the caller.

>  }
>  
>  static int __ltr501_write_raw(struct iio_dev *indio_dev,