Linux IIO development
 help / color / mirror / Atom feed
* [PATCH v3 0/3] iio: light: ltr501: cleanups and modernizations
@ 2026-04-22 23:12 rafasales
  2026-04-22 23:12 ` [PATCH v3 1/3] iio: light: ltr501: update header inclusions rafasales
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: rafasales @ 2026-04-22 23:12 UTC (permalink / raw)
  To: andy, dlechner, jic23, nuno.sa; +Cc: Rafael B. Sales, linux-iio

From: "Rafael B. Sales" <rafasales@usp.br>

This series cleans up the iio/light/ltr501.c driver by sorting header
includes according to the IWYU principles and simplifying locking
using automatic cleanup helpers.

Also replaces instances of `scnprintf()` with `sysfs_emit_at()`, as
the latter is PAGE_SIZE-aware.

Changes in v3:
- Added patch to replace `scnprintf()` with `sysfs_emit_at()`
- PATCH 1: reformatted headers and minimized include redundancy,
removed cleanup.h header
- PATCH 2: replaced some `scoped_guard()` instances with
{} + `guard()`, included cleanup.h header

Changes in v2:
- Added patch to sort header includes
- Fixed naming scheme for functions in commit message
- Reverted original lock boundaries
- Simplified returns

Rafael B. Sales (3):
  iio: light: ltr501: update header inclusions
  iio: light: ltr501: use automatic cleanup of locks
  iio: light: ltr501: use `sysfs_emit_at()` for showing scales

 drivers/iio/light/ltr501.c | 163 +++++++++++++++++++------------------
 1 file changed, 83 insertions(+), 80 deletions(-)

-- 
2.53.0


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

* [PATCH v3 1/3] iio: light: ltr501: update header inclusions
  2026-04-22 23:12 [PATCH v3 0/3] iio: light: ltr501: cleanups and modernizations rafasales
@ 2026-04-22 23:12 ` rafasales
  2026-04-24  9:27   ` Andy Shevchenko
  2026-04-22 23:12 ` [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks rafasales
  2026-04-22 23:12 ` [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales rafasales
  2 siblings, 1 reply; 8+ messages in thread
From: rafasales @ 2026-04-22 23:12 UTC (permalink / raw)
  To: andy, dlechner, jic23, nuno.sa
  Cc: Rafael B. Sales, Gustavo C. Arakaki, linux-iio

From: "Rafael B. Sales" <rafasales@usp.br>

Update header inclusions to follow IWYU (Include What You Use)
principle

Signed-off-by: Rafael B. Sales <rafasales@usp.br>
Co-developed-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
Signed-off-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
---
Changes in v3:
- Separates asm/, linux/ and linux/iio/ headers in separate blocks
- Removes redundant headers
- Replaces some headers
- Removes cleanup.h
---
 drivers/iio/light/ltr501.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 4d99ae336f61..277c497bd6d8 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -9,20 +9,37 @@
  * TODO: IR LED characteristics
  */
 
-#include <linux/module.h>
-#include <linux/mod_devicetable.h>
-#include <linux/i2c.h>
-#include <linux/err.h>
+#include <asm/page.h>
+#include <asm/byteorder.h>
+
+#include <linux/array_size.h>
+#include <linux/bitops.h>
 #include <linux/delay.h>
+#include <linux/dev_printk.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/math.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/property.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/sprintf.h>
+#include <linux/sysfs.h>
+#include <linux/types.h>
 
-#include <linux/iio/iio.h>
+#include <linux/iio/buffer.h>
 #include <linux/iio/events.h>
+#include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 #include <linux/iio/trigger_consumer.h>
-#include <linux/iio/buffer.h>
 #include <linux/iio/triggered_buffer.h>
+#include <linux/iio/types.h>
 
 #define LTR501_ALS_CONTR 0x80 /* ALS operation mode, SW reset */
 #define LTR501_PS_CONTR 0x81 /* PS operation mode */
-- 
2.53.0


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

* [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks
  2026-04-22 23:12 [PATCH v3 0/3] iio: light: ltr501: cleanups and modernizations rafasales
  2026-04-22 23:12 ` [PATCH v3 1/3] iio: light: ltr501: update header inclusions rafasales
@ 2026-04-22 23:12 ` rafasales
  2026-04-24  9:33   ` Andy Shevchenko
  2026-04-24 11:32   ` Jonathan Cameron
  2026-04-22 23:12 ` [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales rafasales
  2 siblings, 2 replies; 8+ messages in thread
From: rafasales @ 2026-04-22 23:12 UTC (permalink / raw)
  To: andy, dlechner, jic23, nuno.sa
  Cc: Rafael B. Sales, Gustavo C. Arakaki, linux-iio

From: "Rafael B. Sales" <rafasales@usp.br>

Replace `mutex_lock()` and `mutex_unlock()` calls with guards
to reduce boilerplate and allow for simpler code blocks.

Signed-off-by: Rafael B. Sales <rafasales@usp.br>
Co-developed-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
Signed-off-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>

---
Changes in v3:
- Replaces `scoped_guard()` with {} + `guard()` in some cases
- Simplifies some return statements
- Includes cleanup.h header

Changes in v2:
- Maintains original locking boundaries
- Simplifies some return statements
---
 drivers/iio/light/ltr501.c | 131 +++++++++++++++++--------------------
 1 file changed, 59 insertions(+), 72 deletions(-)

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 277c497bd6d8..81fb35cbd56a 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -14,6 +14,7 @@
 
 #include <linux/array_size.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/dev_printk.h>
 #include <linux/device.h>
@@ -250,7 +251,7 @@ static int ltr501_ps_read_samp_freq(const struct ltr501_data *data,
 static int ltr501_als_write_samp_freq(struct ltr501_data *data,
 				      int val, int val2)
 {
-	int i, ret;
+	int i;
 
 	i = ltr501_match_samp_freq(ltr501_als_samp_table,
 				   ARRAY_SIZE(ltr501_als_samp_table),
@@ -259,17 +260,14 @@ static int ltr501_als_write_samp_freq(struct ltr501_data *data,
 	if (i < 0)
 		return i;
 
-	mutex_lock(&data->lock_als);
-	ret = regmap_field_write(data->reg_als_rate, i);
-	mutex_unlock(&data->lock_als);
-
-	return ret;
+	guard(mutex)(&data->lock_als);
+	return regmap_field_write(data->reg_als_rate, i);
 }
 
 static int ltr501_ps_write_samp_freq(struct ltr501_data *data,
 				     int val, int val2)
 {
-	int i, ret;
+	int i;
 
 	i = ltr501_match_samp_freq(ltr501_ps_samp_table,
 				   ARRAY_SIZE(ltr501_ps_samp_table),
@@ -278,11 +276,8 @@ static int ltr501_ps_write_samp_freq(struct ltr501_data *data,
 	if (i < 0)
 		return i;
 
-	mutex_lock(&data->lock_ps);
-	ret = regmap_field_write(data->reg_ps_rate, i);
-	mutex_unlock(&data->lock_ps);
-
-	return ret;
+	guard(mutex)(&data->lock_ps);
+	return regmap_field_write(data->reg_ps_rate, i);
 }
 
 static int ltr501_als_read_samp_period(const struct ltr501_data *data, int *val)
@@ -504,9 +499,10 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,
 		if (new_val < 0 || new_val > 0x0f)
 			return -EINVAL;
 
-		mutex_lock(&data->lock_als);
-		ret = regmap_field_write(data->reg_als_prst, new_val);
-		mutex_unlock(&data->lock_als);
+		scoped_guard(mutex, &data->lock_als) {
+			ret = regmap_field_write(data->reg_als_prst, new_val);
+		}
+
 		if (ret >= 0)
 			data->als_period = period;
 
@@ -524,9 +520,10 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,
 		if (new_val < 0 || new_val > 0x0f)
 			return -EINVAL;
 
-		mutex_lock(&data->lock_ps);
-		ret = regmap_field_write(data->reg_ps_prst, new_val);
-		mutex_unlock(&data->lock_ps);
+		scoped_guard(mutex, &data->lock_ps) {
+			ret = regmap_field_write(data->reg_ps_prst, new_val);
+		}
+
 		if (ret >= 0)
 			data->ps_period = period;
 
@@ -667,23 +664,23 @@ static int ltr501_read_info_raw(struct ltr501_data *data,
 	int ret;
 
 	switch (chan->type) {
-	case IIO_INTENSITY:
-		mutex_lock(&data->lock_als);
+	case IIO_INTENSITY: {
+		guard(mutex)(&data->lock_als);
 		ret = ltr501_read_als(data, buf);
-		mutex_unlock(&data->lock_als);
 		if (ret < 0)
 			return ret;
 		*val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ?
 				   buf[0] : buf[1]);
 		return IIO_VAL_INT;
-	case IIO_PROXIMITY:
-		mutex_lock(&data->lock_ps);
+        }
+	case IIO_PROXIMITY: {
+		guard(mutex)(&data->lock_ps);
 		ret = ltr501_read_ps(data);
-		mutex_unlock(&data->lock_ps);
 		if (ret < 0)
 			return ret;
 		*val = ret & LTR501_PS_DATA_MASK;
 		return IIO_VAL_INT;
+	}
 	default:
 		return -EINVAL;
 	}
@@ -704,9 +701,8 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 			if (!iio_device_claim_direct(indio_dev))
 				return -EBUSY;
 
-			mutex_lock(&data->lock_als);
-			ret = ltr501_read_als(data, buf);
-			mutex_unlock(&data->lock_als);
+			scoped_guard(mutex, &data->lock_als)
+				ret = ltr501_read_als(data, buf);
 			iio_device_release_direct(indio_dev);
 			if (ret < 0)
 				return ret;
@@ -815,14 +811,13 @@ static int __ltr501_write_raw(struct iio_dev *indio_dev,
 
 	case IIO_CHAN_INFO_INT_TIME:
 		switch (chan->type) {
-		case IIO_INTENSITY:
+		case IIO_INTENSITY: {
 			if (val != 0)
 				return -EINVAL;
 
-			mutex_lock(&data->lock_als);
-			ret = ltr501_set_it_time(data, val2);
-			mutex_unlock(&data->lock_als);
-			return ret;
+			guard(mutex)(&data->lock_als);
+			return ltr501_set_it_time(data, val2);
+		}
 		default:
 			return -EINVAL;
 		}
@@ -959,7 +954,6 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,
 			       int val, int val2)
 {
 	struct ltr501_data *data = iio_priv(indio_dev);
-	int ret;
 
 	if (val < 0)
 		return -EINVAL;
@@ -969,20 +963,18 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,
 		if (val > LTR501_ALS_THRESH_MASK)
 			return -EINVAL;
 		switch (dir) {
-		case IIO_EV_DIR_RISING:
-			mutex_lock(&data->lock_als);
-			ret = regmap_bulk_write(data->regmap,
-						LTR501_ALS_THRESH_UP,
-						&val, 2);
-			mutex_unlock(&data->lock_als);
-			return ret;
-		case IIO_EV_DIR_FALLING:
-			mutex_lock(&data->lock_als);
-			ret = regmap_bulk_write(data->regmap,
-						LTR501_ALS_THRESH_LOW,
-						&val, 2);
-			mutex_unlock(&data->lock_als);
-			return ret;
+		case IIO_EV_DIR_RISING: {
+			guard(mutex)(&data->lock_als);
+			return regmap_bulk_write(data->regmap,
+						 LTR501_ALS_THRESH_UP,
+						 &val, 2);
+			}
+		case IIO_EV_DIR_FALLING: {
+			guard(mutex)(&data->lock_als);
+			return regmap_bulk_write(data->regmap,
+						 LTR501_ALS_THRESH_LOW,
+						 &val, 2);
+			}
 		default:
 			return -EINVAL;
 		}
@@ -990,20 +982,18 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,
 		if (val > LTR501_PS_THRESH_MASK)
 			return -EINVAL;
 		switch (dir) {
-		case IIO_EV_DIR_RISING:
-			mutex_lock(&data->lock_ps);
-			ret = regmap_bulk_write(data->regmap,
-						LTR501_PS_THRESH_UP,
-						&val, 2);
-			mutex_unlock(&data->lock_ps);
-			return ret;
-		case IIO_EV_DIR_FALLING:
-			mutex_lock(&data->lock_ps);
-			ret = regmap_bulk_write(data->regmap,
-						LTR501_PS_THRESH_LOW,
-						&val, 2);
-			mutex_unlock(&data->lock_ps);
-			return ret;
+		case IIO_EV_DIR_RISING: {
+			guard(mutex)(&data->lock_ps);
+			return regmap_bulk_write(data->regmap,
+						 LTR501_PS_THRESH_UP,
+						 &val, 2);
+			}
+		case IIO_EV_DIR_FALLING: {
+			guard(mutex)(&data->lock_ps);
+			return regmap_bulk_write(data->regmap,
+						 LTR501_PS_THRESH_LOW,
+						 &val, 2);
+			}
 		default:
 			return -EINVAL;
 		}
@@ -1095,19 +1085,16 @@ static int ltr501_write_event_config(struct iio_dev *indio_dev,
 				     enum iio_event_direction dir, bool state)
 {
 	struct ltr501_data *data = iio_priv(indio_dev);
-	int ret;
 
 	switch (chan->type) {
-	case IIO_INTENSITY:
-		mutex_lock(&data->lock_als);
-		ret = regmap_field_write(data->reg_als_intr, state);
-		mutex_unlock(&data->lock_als);
-		return ret;
-	case IIO_PROXIMITY:
-		mutex_lock(&data->lock_ps);
-		ret = regmap_field_write(data->reg_ps_intr, state);
-		mutex_unlock(&data->lock_ps);
-		return ret;
+	case IIO_INTENSITY: {
+		guard(mutex)(&data->lock_als);
+		return regmap_field_write(data->reg_als_intr, state);
+		}
+	case IIO_PROXIMITY: {
+		guard(mutex)(&data->lock_ps);
+		return regmap_field_write(data->reg_ps_intr, state);
+		}
 	default:
 		return -EINVAL;
 	}
-- 
2.53.0


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

* [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales
  2026-04-22 23:12 [PATCH v3 0/3] iio: light: ltr501: cleanups and modernizations rafasales
  2026-04-22 23:12 ` [PATCH v3 1/3] iio: light: ltr501: update header inclusions rafasales
  2026-04-22 23:12 ` [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks rafasales
@ 2026-04-22 23:12 ` rafasales
  2026-04-24  9:34   ` Andy Shevchenko
  2 siblings, 1 reply; 8+ messages in thread
From: rafasales @ 2026-04-22 23:12 UTC (permalink / raw)
  To: andy, dlechner, jic23, nuno.sa
  Cc: Rafael B. Sales, Gustavo C. Arakaki, linux-iio

From: "Rafael B. Sales" <rafasales@usp.br>

Replace `scnprintf()` with `sysfs_emit_at()`, which is
PAGE_SIZE-aware and preferred for use in sysfs.

Signed-off-by: Rafael B. Sales <rafasales@usp.br>
Co-developed-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
Signed-off-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
---
 drivers/iio/light/ltr501.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 81fb35cbd56a..3866aa2c8abf 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -9,7 +9,6 @@
  * TODO: IR LED characteristics
  */
 
-#include <asm/page.h>
 #include <asm/byteorder.h>
 
 #include <linux/array_size.h>
@@ -1114,7 +1113,7 @@ static ssize_t ltr501_show_proximity_scale_avail(struct device *dev,
 	for (i = 0; i < info->ps_gain_tbl_size; i++) {
 		if (info->ps_gain[i].scale == LTR501_RESERVED_GAIN)
 			continue;
-		len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
+		len += sysfs_emit_at(buf, len, "%d.%06d ",
 				 info->ps_gain[i].scale,
 				 info->ps_gain[i].uscale);
 	}
@@ -1136,7 +1135,7 @@ static ssize_t ltr501_show_intensity_scale_avail(struct device *dev,
 	for (i = 0; i < info->als_gain_tbl_size; i++) {
 		if (info->als_gain[i].scale == LTR501_RESERVED_GAIN)
 			continue;
-		len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
+		len += sysfs_emit_at(buf, len, "%d.%06d ",
 				 info->als_gain[i].scale,
 				 info->als_gain[i].uscale);
 	}
-- 
2.53.0


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

* Re: [PATCH v3 1/3] iio: light: ltr501: update header inclusions
  2026-04-22 23:12 ` [PATCH v3 1/3] iio: light: ltr501: update header inclusions rafasales
@ 2026-04-24  9:27   ` Andy Shevchenko
  0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2026-04-24  9:27 UTC (permalink / raw)
  To: rafasales; +Cc: andy, dlechner, jic23, nuno.sa, Gustavo C. Arakaki, linux-iio

On Wed, Apr 22, 2026 at 08:12:55PM -0300, rafasales@usp.br wrote:

> Update header inclusions to follow IWYU (Include What You Use)
> principle

Respect English grammar and punctuation.

Also this patch does two things: it sorts headers AND moves to IWYU. Hence this
has to be TWO patches.

...

> -#include <linux/module.h>
> -#include <linux/mod_devicetable.h>
> -#include <linux/i2c.h>
> -#include <linux/err.h>
> +#include <asm/page.h>
> +#include <asm/byteorder.h>

The general rule (independently on the folder of the headers) is that the more
generic header is the earlier it's being included. asm/* are definitely _less_
generic than linux/* ones. But at the same time they are more generic than
linux/iio/* ones for _this_ driver.

> +#include <linux/array_size.h>
> +#include <linux/bitops.h>
>  #include <linux/delay.h>
> +#include <linux/dev_printk.h>
> +#include <linux/device.h>
> +#include <linux/err.h>
> +#include <linux/errno.h>
> +#include <linux/i2c.h>
> +#include <linux/interrupt.h>
> +#include <linux/math.h>
> +#include <linux/mod_devicetable.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/pm.h>
> +#include <linux/property.h>
>  #include <linux/regmap.h>
>  #include <linux/regulator/consumer.h>
> +#include <linux/sprintf.h>
> +#include <linux/sysfs.h>
> +#include <linux/types.h>
>  
> -#include <linux/iio/iio.h>
> +#include <linux/iio/buffer.h>
>  #include <linux/iio/events.h>
> +#include <linux/iio/iio.h>
>  #include <linux/iio/sysfs.h>
>  #include <linux/iio/trigger_consumer.h>
> -#include <linux/iio/buffer.h>
>  #include <linux/iio/triggered_buffer.h>
> +#include <linux/iio/types.h>

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks
  2026-04-22 23:12 ` [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks rafasales
@ 2026-04-24  9:33   ` Andy Shevchenko
  2026-04-24 11:32   ` Jonathan Cameron
  1 sibling, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2026-04-24  9:33 UTC (permalink / raw)
  To: rafasales; +Cc: andy, dlechner, jic23, nuno.sa, Gustavo C. Arakaki, linux-iio

On Wed, Apr 22, 2026 at 08:12:56PM -0300, rafasales@usp.br wrote:
> 
> Replace `mutex_lock()` and `mutex_unlock()` calls with guards
> to reduce boilerplate and allow for simpler code blocks.

...

> -	mutex_lock(&data->lock_als);
> -	ret = regmap_field_write(data->reg_als_rate, i);
> -	mutex_unlock(&data->lock_als);
> -
> -	return ret;
> +	guard(mutex)(&data->lock_als);

+ blank line.

We usually consider guard()() as not semantically linked to any code above
or below.

> +	return regmap_field_write(data->reg_als_rate, i);

Ditto for the same cases below.

...

> +		scoped_guard(mutex, &data->lock_als) {
> +			ret = regmap_field_write(data->reg_als_prst, new_val);
> +		}

Read Coding Style, please. The {} are not used for single statement bodies.

> +

This is an opposite to the above, the conditional is semantically coupled with
previous code, no blank line is needed.

>  		if (ret >= 0)
>  			data->als_period = period;

Ditto for the rest.

...

> +			scoped_guard(mutex, &data->lock_als)
> +				ret = ltr501_read_als(data, buf);

You see, even inside one patch there is an inconsistency. That's not good.
(Note, this piece is correct nevertheless by style).

>  			if (ret < 0)
>  				return ret;

...

>  		switch (dir) {
> +		case IIO_EV_DIR_RISING: {
> +			guard(mutex)(&data->lock_als);
> +			return regmap_bulk_write(data->regmap,
> +						 LTR501_ALS_THRESH_UP,
> +						 &val, 2);
> +			}
> +		case IIO_EV_DIR_FALLING: {
> +			guard(mutex)(&data->lock_als);
> +			return regmap_bulk_write(data->regmap,
> +						 LTR501_ALS_THRESH_LOW,
> +						 &val, 2);
> +			}
>  		default:
>  			return -EINVAL;
>  		}

Can you check if instead of doing like that, you can simply move guard()()
level up and make all this simpler? Same Q to the rest of similar code.

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales
  2026-04-22 23:12 ` [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales rafasales
@ 2026-04-24  9:34   ` Andy Shevchenko
  0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2026-04-24  9:34 UTC (permalink / raw)
  To: rafasales; +Cc: andy, dlechner, jic23, nuno.sa, Gustavo C. Arakaki, linux-iio

On Wed, Apr 22, 2026 at 08:12:57PM -0300, rafasales@usp.br wrote:

> Replace `scnprintf()` with `sysfs_emit_at()`, which is
> PAGE_SIZE-aware and preferred for use in sysfs.

...

>  	for (i = 0; i < info->ps_gain_tbl_size; i++) {
>  		if (info->ps_gain[i].scale == LTR501_RESERVED_GAIN)
>  			continue;
> -		len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
> +		len += sysfs_emit_at(buf, len, "%d.%06d ",
>  				 info->ps_gain[i].scale,
>  				 info->ps_gain[i].uscale);

It broke indentation now.

...

>  	for (i = 0; i < info->als_gain_tbl_size; i++) {
>  		if (info->als_gain[i].scale == LTR501_RESERVED_GAIN)
>  			continue;
> -		len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
> +		len += sysfs_emit_at(buf, len, "%d.%06d ",
>  				 info->als_gain[i].scale,
>  				 info->als_gain[i].uscale);

Ditto.

>  	}

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks
  2026-04-22 23:12 ` [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks rafasales
  2026-04-24  9:33   ` Andy Shevchenko
@ 2026-04-24 11:32   ` Jonathan Cameron
  1 sibling, 0 replies; 8+ messages in thread
From: Jonathan Cameron @ 2026-04-24 11:32 UTC (permalink / raw)
  To: rafasales; +Cc: andy, dlechner, nuno.sa, Gustavo C. Arakaki, linux-iio

On Wed, 22 Apr 2026 20:12:56 -0300
rafasales@usp.br wrote:

> From: "Rafael B. Sales" <rafasales@usp.br>
> 
> Replace `mutex_lock()` and `mutex_unlock()` calls with guards
> to reduce boilerplate and allow for simpler code blocks.
> 
> Signed-off-by: Rafael B. Sales <rafasales@usp.br>
> Co-developed-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
> Signed-off-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
> 
> ---
> Changes in v3:
> - Replaces `scoped_guard()` with {} + `guard()` in some cases
> - Simplifies some return statements
> - Includes cleanup.h header
> 
> Changes in v2:
> - Maintains original locking boundaries
> - Simplifies some return statements

>  static int ltr501_als_read_samp_period(const struct ltr501_data *data, int *val)
> @@ -504,9 +499,10 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,
>  		if (new_val < 0 || new_val > 0x0f)
>  			return -EINVAL;
>  
> -		mutex_lock(&data->lock_als);
> -		ret = regmap_field_write(data->reg_als_prst, new_val);
> -		mutex_unlock(&data->lock_als);
> +		scoped_guard(mutex, &data->lock_als) {
> +			ret = regmap_field_write(data->reg_als_prst, new_val);
> +		}
> +
>  		if (ret >= 0)
Whilst here I'd like these to take more standard form.  

		if (ret)
			return ret;

		data->als_period = period;

		return 0; 

It would be a separate patch though so I don't mind if we leave it for another day.

Also removes suggestion that the current code makes that ret > 0 is a possibility
when it isn't.
>  			data->als_period = period;
>  
> @@ -524,9 +520,10 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,
>  		if (new_val < 0 || new_val > 0x0f)
>  			return -EINVAL;
>  
> -		mutex_lock(&data->lock_ps);
> -		ret = regmap_field_write(data->reg_ps_prst, new_val);
> -		mutex_unlock(&data->lock_ps);
> +		scoped_guard(mutex, &data->lock_ps) {
> +			ret = regmap_field_write(data->reg_ps_prst, new_val);
> +		}
> +
>  		if (ret >= 0)
>  			data->ps_period = period;
Same on this one and any other similar cases.


> @@ -1095,19 +1085,16 @@ static int ltr501_write_event_config(struct iio_dev *indio_dev,
>  				     enum iio_event_direction dir, bool state)
>  {
>  	struct ltr501_data *data = iio_priv(indio_dev);
> -	int ret;
>  
>  	switch (chan->type) {
> -	case IIO_INTENSITY:
> -		mutex_lock(&data->lock_als);
> -		ret = regmap_field_write(data->reg_als_intr, state);
> -		mutex_unlock(&data->lock_als);
> -		return ret;
> -	case IIO_PROXIMITY:
> -		mutex_lock(&data->lock_ps);
> -		ret = regmap_field_write(data->reg_ps_intr, state);
> -		mutex_unlock(&data->lock_ps);
> -		return ret;
> +	case IIO_INTENSITY: {
> +		guard(mutex)(&data->lock_als);
> +		return regmap_field_write(data->reg_als_intr, state);
> +		}
If you end up keeping this stuff, I think conventional style would have
the bracket here:
	}
> +	case IIO_PROXIMITY: {
> +		guard(mutex)(&data->lock_ps);
> +		return regmap_field_write(data->reg_ps_intr, state);
> +		}
>  	default:
>  		return -EINVAL;
>  	}


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

end of thread, other threads:[~2026-04-24 11:32 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-22 23:12 [PATCH v3 0/3] iio: light: ltr501: cleanups and modernizations rafasales
2026-04-22 23:12 ` [PATCH v3 1/3] iio: light: ltr501: update header inclusions rafasales
2026-04-24  9:27   ` Andy Shevchenko
2026-04-22 23:12 ` [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks rafasales
2026-04-24  9:33   ` Andy Shevchenko
2026-04-24 11:32   ` Jonathan Cameron
2026-04-22 23:12 ` [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales rafasales
2026-04-24  9:34   ` Andy Shevchenko

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