iio: light: ltr501: cleanups and modernizations
v3 cover iio: light: ltr501: cleanups and modernizations
From: "Rafael B. Sales" <[email protected]>
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(-)
Signature · 1 line
2.53.0
PATCH v3 1/3 iio: light: ltr501: update header inclusions
From: "Rafael B. Sales" <[email protected]>
Update header inclusions to follow IWYU (Include What You Use) principle
---
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(-)
drivers/iio/light/ltr501.c +23 -6
@@ -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 */
Signature · 1 line
2.53.0
Re: [PATCH v3 1/3] iio: light: ltr501: update header inclusions
Quoted 2 lines · On Wed, Apr 22, 2026 at 08:12:55PM -0300, [email protected] 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.
...
drivers/iio/light/ltr501.c line 13
Quoted 6 lines
drivers/iio/light/ltr501.c +2
-4-#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.
Quoted 30 lines
drivers/iio/light/ltr501.c +20
-2+#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>
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v3 2/3 iio: light: ltr501: use automatic cleanup of locks
In reply to [email protected]From: "Rafael B. Sales" <[email protected]>
Replace mutex_lock() and mutex_unlock() calls with guards to reduce boilerplate and allow for simpler code blocks.
---
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(-)
drivers/iio/light/ltr501.c +59 -72
@@ -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 <R501_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 ; }
Signature · 1 line
2.53.0
Re: [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks
Quoted 3 lines · On Wed, Apr 22, 2026 at 08:12:56PM -0300, [email protected] wrote:
Replace
mutex_lock()andmutex_unlock()calls with guards to reduce boilerplate and allow for simpler code blocks.
...
drivers/iio/light/ltr501.c static int ltr501_als_write_samp_freq(struct ltr501_data *data, · line 263
Quoted 6 lines
drivers/iio/light/ltr501.c +1
-5-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.
drivers/iio/light/ltr501.c static int ltr501_als_write_samp_freq(struct ltr501_data *data, · line 264
Quoted 1 line
drivers/iio/light/ltr501.c +1
-0+return regmap_field_write (data->reg_als_rate, i);
Ditto for the same cases below.
...
drivers/iio/light/ltr501.c static int ltr501_write_intr_prst(struct ltr501_data *data, · line 504
Quoted 3 lines
drivers/iio/light/ltr501.c +3
-0+ 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.
Quoted 1 line
+
This is an opposite to the above, the conditional is semantically coupled with previous code, no blank line is needed.
drivers/iio/light/ltr501.c static int ltr501_write_intr_prst(struct ltr501_data *data, · line 507
Quoted 2 lines
drivers/iio/light/ltr501.c +0
-0if (ret >=0 ) data->als_period = period;
Ditto for the rest.
...
drivers/iio/light/ltr501.c static int ltr501_read_raw(struct iio_dev *indio_dev, · line 705
Quoted 2 lines
drivers/iio/light/ltr501.c +2
-0+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).
Quoted 2 lines
(quoted diff) +0
-0if (ret < 0) return ret;
...
Quoted 6 lines
drivers/iio/light/ltr501.c +5
-0switch (dir) { +case IIO_EV_DIR_RISING : { +guard (mutex)(&data->lock_als); +return regmap_bulk_write (data->regmap, +LTR501_ALS_THRESH_UP , + &val,2 );
drivers/iio/light/ltr501.c static int ltr501_write_thresh(struct iio_dev *indio_dev, · line 980
Quoted 10 lines
drivers/iio/light/ltr501.c +7
-0+ } +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.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks
In reply to [email protected]Quoted 22 lines · On Wed, 22 Apr 2026 20:12:56 -0300 [email protected] wrote:
From: "Rafael B. Sales" <[email protected]>
Replace
mutex_lock()andmutex_unlock()calls with guards to reduce boilerplate and allow for simpler code blocks.Signed-off-by Rafael B. Sales <[email protected]>Co-developed-by Gustavo C. Arakaki <[email protected]>Signed-off-by Gustavo C. Arakaki <[email protected]>---
Changes in v3:
- Replacesscoped_guard()with {} +guard()in some cases
- Simplifies some return statements
- Includescleanup.hheaderChanges in v2:
- Maintains original locking boundaries
- Simplifies some return statementsstatic int
ltr501_als_read_samp_period(const struct ltr501_data *data, int *val)drivers/iio/light/ltr501.c +0
-0@@ -504,9 +499,10 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,if (new_val <0 || new_val >0x0f )
drivers/iio/light/ltr501.c static int ltr501_write_intr_prst(struct ltr501_data *data, · line 506
Quoted 10 lines
drivers/iio/light/ltr501.c +4
-3return -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.
Quoted 5 lines
drivers/iio/light/ltr501.c +0
-0data->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 ;
drivers/iio/light/ltr501.c static int ltr501_write_intr_prst(struct ltr501_data *data, · line 528
Quoted 10 lines
drivers/iio/light/ltr501.c +4
-3-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.
Quoted 11 lines
drivers/iio/light/ltr501.c +0
-5@@ -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);
drivers/iio/light/ltr501.c static int ltr501_write_event_config(struct iio_dev *indio_dev, · line 1093
Quoted 10 lines
drivers/iio/light/ltr501.c +4
-6-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:
}
Quoted 7 lines
drivers/iio/light/ltr501.c +4
-0+case IIO_PROXIMITY : { +guard (mutex)(&data->lock_ps); +return regmap_field_write (data->reg_ps_intr, state); + }default :return -EINVAL ; }
PATCH v3 3/3 iio: light: ltr501: use `sysfs_emit_at()` for showing scales
In reply to [email protected]From: "Rafael B. Sales" <[email protected]>
Replace scnprintf() with sysfs_emit_at(), which is PAGE_SIZE-aware and preferred for use in sysfs.
--- drivers/iio/light/ltr501.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/iio/light/ltr501.c +2 -3
@@ -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); }
Signature · 1 line
2.53.0
Re: [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales
Quoted 2 lines · On Wed, Apr 22, 2026 at 08:12:57PM -0300, [email protected] wrote:
Replace
scnprintf()withsysfs_emit_at(), which is PAGE_SIZE-aware and preferred for use in sysfs.
...
drivers/iio/light/ltr501.c static ssize_t ltr501_show_proximity_scale_avail(struct device *dev, · line 1118
Quoted 7 lines
drivers/iio/light/ltr501.c +1
-1for (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.
...
drivers/iio/light/ltr501.c static ssize_t ltr501_show_intensity_scale_avail(struct device *dev, · line 1140
Quoted 7 lines
drivers/iio/light/ltr501.c +1
-1for (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.
Quoted 1 line
}
Signature · 2 lines
With Best Regards, Andy Shevchenko