Skip to contentSkip to conversation

iio: light: si1133: driver cleanup

PATCH Superseded Joshua Crofts 2 participants 11 messages Apr 28 IIO

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover iio: light: si1133: driver cleanup

This series deals with the cleanup and modernization of the Silicon Labs SI1133 UV/Ambient light sensor.

What seemed like a simple checkpatch cleanup turned out to be more complex, therefore I've ended up doing a patch series instead.

Changes include:
- adding missing headers
- removing unused macros
- adding guard(mutex)() support
- code style fixes

---
Changes in v2:
- Change v2 to patch series
- PATCH 2: remove unused macros
- PATCH 3: add missing include headers
- PATCH 4: group generic <linux/*> headers
- PATCH 5: add guard(mutex)() support
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Joshua Crofts (5):
iio: light: si1133: prefer complex macros enclosed in parenthesis
iio: light: si1133: remove unused macros
iio: light: si1133: add missing include headers
iio: light: si1133: group generic <linux/*> headers
iio: light: si1133: use guard(mutex)() macro

 drivers/iio/light/si1133.c | 57 ++++++++++++++++++++++------------------------
 1 file changed, 27 insertions(+), 30 deletions(-)

---
base-commit: d86db1905add39f905cf9f04252804b359914ed6
change-id: 20260427-si1133-checkup-abcc5280adf3

Best regards,

Signature · 1 line
Joshua Crofts <[email protected]>
#1 lore raw

PATCH v2 1/5 iio: light: si1133: prefer complex macros enclosed in parenthesis

Enclose complex macros in parenthesis per checkpatch.pl error to improve code style.

---
 drivers/iio/light/si1133.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/iio/light/si1133.c +9 -9
index 44fa152dbd24c26c97cc778cbe641d92ecd56afa..52e4269dc35014b87b7c46a120e64723be716768 100644
@@ -50,23 +50,23 @@
 #define SI1133_MAX_CMD_CTR		0xF
 
 #define SI1133_PARAM_REG_CHAN_LIST	0x01
-#define SI1133_PARAM_REG_ADCCONFIG(x)	((x) * 4) + 2
-#define SI1133_PARAM_REG_ADCSENS(x)	((x) * 4) + 3
-#define SI1133_PARAM_REG_ADCPOST(x)	((x) * 4) + 4
+#define SI1133_PARAM_REG_ADCCONFIG(x)	(((x) * 4) + 2)
+#define SI1133_PARAM_REG_ADCSENS(x)	(((x) * 4) + 3)
+#define SI1133_PARAM_REG_ADCPOST(x)	(((x) * 4) + 4)
 
 #define SI1133_ADCMUX_MASK 0x1F
 
-#define SI1133_ADCCONFIG_DECIM_RATE(x)	(x) << 5
+#define SI1133_ADCCONFIG_DECIM_RATE(x)	((x) << 5)
 
 #define SI1133_ADCSENS_SCALE_MASK 0x70
 #define SI1133_ADCSENS_SCALE_SHIFT 4
 #define SI1133_ADCSENS_HSIG_MASK BIT(7)
 #define SI1133_ADCSENS_HSIG_SHIFT 7
 #define SI1133_ADCSENS_HW_GAIN_MASK 0xF
-#define SI1133_ADCSENS_NB_MEAS(x)	fls(x) << SI1133_ADCSENS_SCALE_SHIFT
+#define SI1133_ADCSENS_NB_MEAS(x)	(fls(x) << SI1133_ADCSENS_SCALE_SHIFT)
 
 #define SI1133_ADCPOST_24BIT_EN BIT(6)
-#define SI1133_ADCPOST_POSTSHIFT_BITQTY(x) (x & GENMASK(2, 0)) << 3
+#define SI1133_ADCPOST_POSTSHIFT_BITQTY(x) (((x) & GENMASK(2, 0)) << 3)
 
 #define SI1133_PARAM_ADCMUX_SMALL_IR	0x0
 #define SI1133_PARAM_ADCMUX_MED_IR	0x1
@@ -87,15 +87,15 @@
 #define SI1133_CMD_MINSLEEP_US_HIGH	7500
 #define SI1133_CMD_TIMEOUT_MS		25
 #define SI1133_CMD_LUX_TIMEOUT_MS	5000
-#define SI1133_CMD_TIMEOUT_US		SI1133_CMD_TIMEOUT_MS * 1000
+#define SI1133_CMD_TIMEOUT_US		(SI1133_CMD_TIMEOUT_MS * 1000)
 
-#define SI1133_REG_HOSTOUT(x)		(x) + 0x13
+#define SI1133_REG_HOSTOUT(x)		((x) + 0x13)
 
 #define SI1133_MEASUREMENT_FREQUENCY 1250
 
 #define SI1133_X_ORDER_MASK            0x0070
 #define SI1133_Y_ORDER_MASK            0x0007
-#define si1133_get_x_order(m)          ((m) & SI1133_X_ORDER_MASK) >> 4
+#define si1133_get_x_order(m)          (((m) & SI1133_X_ORDER_MASK) >> 4)
 #define si1133_get_y_order(m)          ((m) & SI1133_Y_ORDER_MASK)
 
 #define SI1133_LUX_ADC_MASK		0xE
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/5 iio: light: si1133: remove unused macros

In reply to Joshua Crofts

Remove unused macros to improve readability.

No functional change.

---
 drivers/iio/light/si1133.c | 7 -------
 1 file changed, 7 deletions(-)
drivers/iio/light/si1133.c +0 -7
index 52e4269dc35014b87b7c46a120e64723be716768..ada8da122b76c9a0a55751baa6522a145f0fc7e8 100644
@@ -22,8 +22,6 @@
 #define SI1133_REG_PART_ID		0x00
 #define SI1133_REG_REV_ID		0x01
 #define SI1133_REG_MFR_ID		0x02
-#define SI1133_REG_INFO0		0x03
-#define SI1133_REG_INFO1		0x04
 
 #define SI1133_PART_ID			0x33
 
@@ -40,7 +38,6 @@
 #define SI1133_CMD_RESET_CTR		0x00
 #define SI1133_CMD_RESET_SW		0x01
 #define SI1133_CMD_FORCE		0x11
-#define SI1133_CMD_START_AUTONOMOUS	0x13
 #define SI1133_CMD_PARAM_SET		0x80
 #define SI1133_CMD_PARAM_QUERY		0x40
 #define SI1133_CMD_PARAM_MASK		0x3F
@@ -86,13 +83,9 @@
 #define SI1133_CMD_MINSLEEP_US_LOW	5000
 #define SI1133_CMD_MINSLEEP_US_HIGH	7500
 #define SI1133_CMD_TIMEOUT_MS		25
-#define SI1133_CMD_LUX_TIMEOUT_MS	5000
-#define SI1133_CMD_TIMEOUT_US		(SI1133_CMD_TIMEOUT_MS * 1000)
 
 #define SI1133_REG_HOSTOUT(x)		((x) + 0x13)
 
-#define SI1133_MEASUREMENT_FREQUENCY 1250
-
 #define SI1133_X_ORDER_MASK            0x0070
 #define SI1133_Y_ORDER_MASK            0x0007
 #define si1133_get_x_order(m)          (((m) & SI1133_X_ORDER_MASK) >> 4)
Signature · 1 line
2.47.3
maintainer
#3 lore raw

Re: [PATCH v2 2/5] iio: light: si1133: remove unused macros

Quoted 3 lines · On Tue, 28 Apr 2026 15:18:34 +0200 Joshua Crofts via B4 Relay…

From: Joshua Crofts <[email protected]>

Remove unused macros to improve readability.

Hi Joshua,

Don't update macros then remove them - that's just increasing noise in the update patch. Reorder this patch to be before patch 1.

Otherwise, I think a few of these can stay. See below

Thanks,

Jonathan

Quoted 13 lines

No functional change.

---
 drivers/iio/light/si1133.c | 7 -------
 1 file changed, 7 deletions(-)
drivers/iio/light/si1133.c +0 -0
index 52e4269dc35014b87b7c46a120e64723be716768..ada8da122b76c9a0a55751baa6522a145f0fc7e8 100644

drivers/iio/light/si1133.c line 26

Quoted 5 lines
drivers/iio/light/si1133.c +0 -2
@@ -22,8 +22,6 @@
 #define SI1133_REG_PART_ID		0x00
 #define SI1133_REG_REV_ID		0x01
 #define SI1133_REG_MFR_ID		0x02
-#define SI1133_REG_INFO0		0x03
-#define SI1133_REG_INFO1		0x04

This one isn't in the category of things Andy was referring to.
It's a hardware register address definition. We don't have hard rules on whether a complete set of these should be defined, or just the ones used but it is definitely acceptable to just define them all up front as then easy to check them against a datasheet.

drivers/iio/light/si1133.c line 43

Quoted 8 lines
drivers/iio/light/si1133.c +0 -1
 #define SI1133_PART_ID			0x33
 
@@ -40,7 +38,6 @@
 #define SI1133_CMD_RESET_CTR		0x00
 #define SI1133_CMD_RESET_SW		0x01
 #define SI1133_CMD_FORCE		0x11
-#define SI1133_CMD_START_AUTONOMOUS	0x13

Similar for this.

drivers/iio/light/si1133.c line 90

Quoted 9 lines
drivers/iio/light/si1133.c +0 -2
 #define SI1133_CMD_PARAM_SET		0x80
 #define SI1133_CMD_PARAM_QUERY		0x40
 #define SI1133_CMD_PARAM_MASK		0x3F
@@ -86,13 +83,9 @@
 #define SI1133_CMD_MINSLEEP_US_LOW	5000
 #define SI1133_CMD_MINSLEEP_US_HIGH	7500
 #define SI1133_CMD_TIMEOUT_MS		25
-#define SI1133_CMD_LUX_TIMEOUT_MS	5000
-#define SI1133_CMD_TIMEOUT_US		(SI1133_CMD_TIMEOUT_MS * 1000)

Dropping these and the one below is good as those aren't register or command defines. This last one is entirely pointless as better to have the rescale inline and only one definition up here.

Quoted 9 lines
drivers/iio/light/si1133.c +0 -2
 #define SI1133_REG_HOSTOUT(x)		((x) + 0x13)
 
-#define SI1133_MEASUREMENT_FREQUENCY 1250
-
 #define SI1133_X_ORDER_MASK            0x0070
 #define SI1133_Y_ORDER_MASK            0x0007
 #define si1133_get_x_order(m)          (((m) & SI1133_X_ORDER_MASK) >> 4)
 
#4 lore raw

Re: [PATCH v2 2/5] iio: light: si1133: remove unused macros

Quoted 2 lines · Jonathan Cameron

Don't update macros then remove them - that's just increasing noise in the update patch. Reorder this patch to be before patch 1.

Yeah, my fault, I forgot to do a rebase.

Quoted 22 lines

drivers/iio/light/si1133.c line 26

Quoted 5 lines
 #define SI1133_REG_PART_ID           0x00
 #define SI1133_REG_REV_ID            0x01
 #define SI1133_REG_MFR_ID            0x02
drivers/iio/light/si1133.c +0 -2
-#define SI1133_REG_INFO0             0x03
-#define SI1133_REG_INFO1             0x04

This one isn't in the category of things Andy was referring to.
It's a hardware register address definition. We don't have hard rules on whether a complete set of these should be defined, or just the ones used but it is definitely acceptable to just define them all up front as then easy to check them against a datasheet.

drivers/iio/light/si1133.c line 43

Quoted 8 lines

#define SI1133_PART_ID 0x33

drivers/iio/light/si1133.c +0 -1
@@ -40,7 +38,6 @@
 #define SI1133_CMD_RESET_CTR         0x00
 #define SI1133_CMD_RESET_SW          0x01
 #define SI1133_CMD_FORCE             0x11
-#define SI1133_CMD_START_AUTONOMOUS  0x13

Similar for this.

Quoted 13 lines

drivers/iio/light/si1133.c line 90

Quoted 9 lines
drivers/iio/light/si1133.c +0 -2
 #define SI1133_CMD_PARAM_SET         0x80
 #define SI1133_CMD_PARAM_QUERY               0x40
 #define SI1133_CMD_PARAM_MASK                0x3F
@@ -86,13 +83,9 @@
 #define SI1133_CMD_MINSLEEP_US_LOW   5000
 #define SI1133_CMD_MINSLEEP_US_HIGH  7500
 #define SI1133_CMD_TIMEOUT_MS                25
-#define SI1133_CMD_LUX_TIMEOUT_MS    5000
-#define SI1133_CMD_TIMEOUT_US                (SI1133_CMD_TIMEOUT_MS * 1000)

Dropping these and the one below is good as those aren't register or command defines. This last one is entirely pointless as better to have the rescale inline and only one definition up here.

Sure, I'll send a v2 tomorrow.

Signature · 3 lines
Kind regards

CJD
#5 lore raw

PATCH v2 3/5 iio: light: si1133: add missing include headers

In reply to Joshua Crofts

Add missing include headers to prevent compilation relying on transient dependencies (array_size.h, bitops.h, completion.h, dev_printk.h, err.h, irqreturn.h, jiffies.h, math.h, mutex.h, types.h).

---
 drivers/iio/light/si1133.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/iio/light/si1133.c +10 -0
index ada8da122b76c9a0a55751baa6522a145f0fc7e8..5c5c5ae0535a9592522c0b21496f47068044c6ff 100644
@@ -6,11 +6,21 @@
  * Copyright 2018 Maxime Roussin-Belanger <[email protected]>
  */
 
+#include <linux/array_size.h>
+#include <linux/bitops.h>
+#include <linux/completion.h>
 #include <linux/delay.h>
+#include <linux/dev_printk.h>
+#include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
+#include <linux/irqreturn.h>
+#include <linux/jiffies.h>
+#include <linux/math.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/regmap.h>
+#include <linux/types.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
Signature · 1 line
2.47.3
#6 lore raw

PATCH v2 4/5 iio: light: si1133: group generic <linux/*> headers

In reply to Joshua Crofts

Group generic <linux/*> include headers to improve code style.

No functional change.

---
 drivers/iio/light/si1133.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/light/si1133.c +2 -4
index 5c5c5ae0535a9592522c0b21496f47068044c6ff..f2a79675c026c86bb56d85a65cd035d7540d0ef8 100644
@@ -21,14 +21,12 @@
 #include <linux/mutex.h>
 #include <linux/regmap.h>
 #include <linux/types.h>
+#include <linux/unaligned.h>
+#include <linux/util_macros.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 
-#include <linux/util_macros.h>
-
-#include <linux/unaligned.h>
-
 #define SI1133_REG_PART_ID		0x00
 #define SI1133_REG_REV_ID		0x01
 #define SI1133_REG_MFR_ID		0x02
Signature · 1 line
2.47.3
maintainer
#7 lore raw

Re: [PATCH v2 4/5] iio: light: si1133: group generic <linux/*> headers

Quoted 3 lines · On Tue, 28 Apr 2026 15:18:36 +0200 Joshua Crofts via B4 Relay…

From: Joshua Crofts <[email protected]>

Group generic <linux/*> include headers to improve code style.

Not particularly important but you could mention why these two are where they are. For the unaligned one that was a mass replacement of asm/unaligned with linux/unaligned that didn't reflect local ordering. No idea for util_macros.h!

Quoted 30 lines

No functional change.

---
 drivers/iio/light/si1133.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/light/si1133.c +2 -4
index 5c5c5ae0535a9592522c0b21496f47068044c6ff..f2a79675c026c86bb56d85a65cd035d7540d0ef8 100644
@@ -21,14 +21,12 @@
 #include <linux/mutex.h>
 #include <linux/regmap.h>
 #include <linux/types.h>
+#include <linux/unaligned.h>
+#include <linux/util_macros.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 
-#include <linux/util_macros.h>
-
-#include <linux/unaligned.h>
-
 #define SI1133_REG_PART_ID		0x00
 #define SI1133_REG_REV_ID		0x01
 #define SI1133_REG_MFR_ID		0x02
#8 lore raw

PATCH v2 5/5 iio: light: si1133: use guard(mutex)() macro

In reply to Joshua Crofts

Remove mutex_lock()/mutex_unlock() and goto instances and add guard(mutex)() macro to modernize driver and improve mutex handling.

---
 drivers/iio/light/si1133.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/iio/light/si1133.c +7 -11
index f2a79675c026c86bb56d85a65cd035d7540d0ef8..3b40defda383bcbe731dc0b413903e0267f03b01 100644
@@ -8,6 +8,7 @@
 
 #include <linux/array_size.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/dev_printk.h>
@@ -392,7 +393,7 @@ static int si1133_command(struct si1133_data *data, u8 cmd)
 	int err;
 	int expected_seq;
 
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
 
 	expected_seq = (data->rsp_seq + 1) & SI1133_MAX_CMD_CTR;
 
@@ -403,19 +404,17 @@ static int si1133_command(struct si1133_data *data, u8 cmd)
 	if (err) {
 		dev_warn(dev, "Failed to write command 0x%02x, ret=%d\n", cmd,
 			 err);
-		goto out;
+		return err;
 	}
 
 	if (cmd == SI1133_CMD_FORCE) {
 		/* wait for irq */
 		if (!wait_for_completion_timeout(&data->completion,
-			msecs_to_jiffies(SI1133_COMPLETION_TIMEOUT_MS))) {
-			err = -ETIMEDOUT;
-			goto out;
-		}
+			msecs_to_jiffies(SI1133_COMPLETION_TIMEOUT_MS)))
+			return -ETIMEDOUT;
 		err = regmap_read(data->regmap, SI1133_REG_RESPONSE0, &resp);
 		if (err)
-			goto out;
+			return err;
 	} else {
 		err = regmap_read_poll_timeout(data->regmap,
 					       SI1133_REG_RESPONSE0, resp,
@@ -428,7 +427,7 @@ static int si1133_command(struct si1133_data *data, u8 cmd)
 			dev_warn(dev,
 				 "Failed to read command 0x%02x, ret=%d\n",
 				 cmd, err);
-			goto out;
+			return err;
 		}
 	}
 
@@ -439,9 +438,6 @@ static int si1133_command(struct si1133_data *data, u8 cmd)
 		data->rsp_seq = expected_seq;
 	}
 
-out:
-	mutex_unlock(&data->mutex);
-
 	return err;
 }
 
Signature · 1 line
2.47.3
maintainer
#9 lore raw

Re: [PATCH v2 5/5] iio: light: si1133: use guard(mutex)() macro

Quoted 6 lines · On Tue, 28 Apr 2026 15:18:37 +0200 Joshua Crofts via B4 Relay…

From: Joshua Crofts <[email protected]>

Remove mutex_lock()/mutex_unlock() and goto instances and add guard(mutex)() macro to modernize driver and improve mutex handling.

One comment that is more about the existing code than the changes here, but as you are touching it nice to clean that bit up!

Nice series,

Jonathan

Quoted 32 lines
---
 drivers/iio/light/si1133.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/iio/light/si1133.c +3 -2
index f2a79675c026c86bb56d85a65cd035d7540d0ef8..3b40defda383bcbe731dc0b413903e0267f03b01 100644
@@ -8,6 +8,7 @@
 
 #include <linux/array_size.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/dev_printk.h>
@@ -392,7 +393,7 @@ static int si1133_command(struct si1133_data *data, u8 cmd)
 	int err;
 	int expected_seq;
 
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
 
 	expected_seq = (data->rsp_seq + 1) & SI1133_MAX_CMD_CTR;
 
@@ -403,19 +404,17 @@ static int si1133_command(struct si1133_data *data, u8 cmd)
 	if (err) {
 		dev_warn(dev, "Failed to write command 0x%02x, ret=%d\n", cmd,
 			 err);
-		goto out;
+		return err;
 	}

drivers/iio/light/si1133.c static int si1133_command(struct si1133_data *data, u8 cmd) · line 414

Quoted 10 lines
drivers/iio/light/si1133.c +2 -4
 
 	if (cmd == SI1133_CMD_FORCE) {
 		/* wait for irq */
 		if (!wait_for_completion_timeout(&data->completion,
-			msecs_to_jiffies(SI1133_COMPLETION_TIMEOUT_MS))) {
-			err = -ETIMEDOUT;
-			goto out;
-		}
+			msecs_to_jiffies(SI1133_COMPLETION_TIMEOUT_MS)))
+			return -ETIMEDOUT;

This is some unfortunate indentation from readability point of view.
I'd reindent and not worry about the long line - it's very long but still under 100 so tools won't complain and I think it is worth the pain here!

drivers/iio/light/si1133.c static int si1133_command(struct si1133_data *data, u8 cmd) · line 412

Quoted 1 line
drivers/iio/light/si1133.c +0 -0
 		if (!wait_for_completion_timeout(&data->completion,
						 msecs_to_jiffies(SI1133_COMPLETION_TIMEOUT_MS)))
			return -ETIMEDOUT;

or perhaps better yet, use a local variable for that timeout.

Quoted 27 lines
drivers/iio/light/si1133.c +1 -1
 		err = regmap_read(data->regmap, SI1133_REG_RESPONSE0, &resp);
 		if (err)
-			goto out;
+			return err;
 	} else {
 		err = regmap_read_poll_timeout(data->regmap,
drivers/iio/light/si1133.c +1 -4
 					       SI1133_REG_RESPONSE0, resp,
@@ -428,7 +427,7 @@ static int si1133_command(struct si1133_data *data, u8 cmd)
 			dev_warn(dev,
 				 "Failed to read command 0x%02x, ret=%d\n",
 				 cmd, err);
-			goto out;
+			return err;
 		}
 	}
 
@@ -439,9 +438,6 @@ static int si1133_command(struct si1133_data *data, u8 cmd)
 		data->rsp_seq = expected_seq;
 	}
 
-out:
-	mutex_unlock(&data->mutex);
-
 	return err;
 }
 
#10 lore raw

Re: [PATCH v2 5/5] iio: light: si1133: use guard(mutex)() macro

Quoted 13 lines · Jonathan Cameron

drivers/iio/light/si1133.c static int si1133_command(struct si1133_data *data, u8 cmd) · line 414

Quoted 9 lines
      if (cmd == SI1133_CMD_FORCE) {
              /* wait for irq */
              if (!wait_for_completion_timeout(&data->completion,
drivers/iio/light/si1133.c +2 -4
-                     msecs_to_jiffies(SI1133_COMPLETION_TIMEOUT_MS))) {
-                     err = -ETIMEDOUT;
-                     goto out;
-             }
+                     msecs_to_jiffies(SI1133_COMPLETION_TIMEOUT_MS)))
+                     return -ETIMEDOUT;

This is some unfortunate indentation from readability point of view.
I'd reindent and not worry about the long line - it's very long but still under 100 so tools won't complain and I think it is worth the pain here!

Quoted 5 lines
Quoted 1 line
              if (!wait_for_completion_timeout(&data->completion,
                                                 msecs_to_jiffies(SI1133_COMPLETION_TIMEOUT_MS)))
                        return -ETIMEDOUT;

or perhaps better yet, use a local variable for that timeout.

Okay, I'll probably do a separate patch for that though.

Signature · 3 lines
Kind regards

CJD