Skip to contentSkip to conversation

iio: light: add support for veml6031x00 ALS series

PATCH Superseded Javier Carrasco 6 participants 23 messages Aug 7 – Aug 11

Subsystems (4)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Javier Carrasco

author maintainer
#0 lore raw

v5 cover iio: light: add support for veml6031x00 ALS series

These ambient light sensors with I2C interface provide two light channels (ALS and IR), high/low threshold alarms with configurable persistence, and a data ready signal.

The devices covered by this driver have the same resolution, and they share most of their functionality. These are the differences between them (note that the x belongs to their names, and it is not a wildcard):

- Device ID: accessible via two 8-byte registers, different values for veml6031x00/veml6031x01 and veml60311x00/veml60311x01.
- I2C address: same grouping, 0x29 and 0x10 I2C addresses.
- AEC qualification: AEC-Q100 for veml6031x00/veml60311x00 and AEC-Q101 for veml6031x01/veml60311x01.

The alarms and the data ready signals share the interrupt pin, and an interrupt status register must be accessed to identify the source. Such multiplexing is not new in IIO, and I have followed existing examples for it. The persistence setting (own attribute) to trigger the alarms uses the pattern that has already been used for the veml6030.

The device configuration is in general documented in the datasheet and the application note. There is an exception, though: the activation of the "active force" mode that is required for the data ready signal must be carried out in two steps even though the affected bits are located in the same register: first ALS_AF (active force mode enable) must be set, and then ALS_TRIG (active force trigger setting) must be enabled. I have added a brief commentary in the code to explain this behavior, which has been confirmed by the manufacturer.

The datasheet specifies the scale and integration time for the ALS channel. Although both settings also affect the measured IR value, no transfer function, accuracy, or calibration is specified for converting the IR output into a physical quantity. The IR channel is therefore intended only as a qualitative indication of the infrared content of the incident light, rather than as a precision measurement channel. Consequently, the driver does not expose scale or integration time as IR-channel attributes.

The only functionality that has not been implemented yet is the x0.66 gain (and its x0.165 counterpart when PD_DIV=1), which makes the gts helpers less usable due to the conversions required. It is indeed an uncommon gain to use (there are x0.5 and x0.125 gains) with no known use-case at the moment that justifies making adjustments to the gts helpers or adding artificial conversions to make it work.

This driver has been tested with the four supported devices separately as well as in pairs where the I2C addresses don't overlap.

To: Jonathan Cameron <[email protected]>
To: Lars-Peter Clausen <[email protected]>
To: Rob Herring <[email protected]>
To: Krzysztof Kozlowski <[email protected]>
To: Conor Dooley <[email protected]>
To: David Lechner <[email protected]>
To: Nuno Sá <[email protected]>
To: Andy Shevchenko <[email protected]>

Changes in v5:
- [2/4] Explicit inclusion of some missing used headers.
- [2/4] Define CONF0 as volatile to account for AF_TRIG (reset by hardware).
- [2/4] Drop integration time and scale for IR channel.
- [2/4] Use unsigned int instead of int where no negative values are possible.
- [2/4] Write a helper to update the gain and use it where needed.
- [2/4] Move i2c_get_match() closer to the data allocation.
- [2/4] Move iio dev initialization out of the runtime pm region.
- [2/4] Lower log level for unknown ID part.
- [2/4] Redefine gains as _INT_MILLI.
- [2/4] Drop pointer to the device in the private structure.
- [2/4] Fix some coding-style issues.
- [4/4] Add second iio_chan_spec to avoid exposing dead sysfs attributes if no irq is available.
- [4/4] Refactor goto calls in functions with guard().
- [3/4] Use IIO_DECLARE_BUFFER_WITH_TS() instead of a custom structure.
- [4/4] Use format instead of deprecated sign for the scan type.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- [1/4] Entry in alphabetical order for MAINTAINERS.
- [2/4] Fix style for device ID tables.
- [2/4] Add missing header mod_devicetable.h and move iio/sysfs.h to
[4/4] for the persistence attribute.
- [2/4] Move IIO_DEV_ACQUIRE_DIRECT_MODE to [3/4] where buffers are
added.
- [3/4] Use test_bit() to check the active scan mask.
- [2/4] Add mutex for operations on scattered register fields.
- [2/4] Add shutdown action after turning the device on.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Move veml6030 fixes to a separate patch stack.
- Use C99 initializers for i2c_device_id.
- Split driver code into multiple patches to ease its review.
- Rework locking to get rid of atomic increment/decrement ops.
- Fix error paths in pm_runtime operations.
- Use IIO_DEV_ACQUIRE_DIRECT_MODE for single read/write ops.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add commit to fix bug in veml6030.c (channel type when pushing
events) and remove dead code.
- Use gts helpers to simplify operations.
- Drop unused gain_idx.
- Build INT_MASK as an OR operation of the involved bits.
- Format arrays to follow the desired standard for IIO.
- Directly return function result as the last operation within another function instead of 'ret = x; if (ret) return ret; return 0;'.
- Fix some spacing (double space, tab for alignemnt in info struct).
- Use sizeof() for __le16 reg instead of 2.
- Return an error if the part ID could not be read.
- Spell out sd -> shutdown.
- Use devm_mutex_init() instead of mutex_init().
- Avoid using conditional guard, use claim/release instead.
- Access integration times from the global array to get and set the integration time instead of using a switch.
- Simplify read of available periods (persistence).
- Drop IRQF_TRIGGER_FALLING in the threaded irq request.
- Add regmap ranges.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Javier Carrasco (4):
dt-bindings: iio: light: veml6030: add veml6031x00 ALS series
iio: light: add support for veml6031x00 ALS series
iio: light: veml6031x00: add support for triggered buffers
iio: light: veml6031x00: add support for events and trigger

 .../bindings/iio/light/vishay,veml6030.yaml        |   13 +-
 MAINTAINERS                                        |    6 +
 drivers/iio/light/Kconfig                          |   14 +
 drivers/iio/light/Makefile                         |    1 +
 drivers/iio/light/veml6031x00.c                    | 1321 ++++++++++++++++++++
 5 files changed, 1354 insertions(+), 1 deletion(-)

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20241109-veml6031x00-aa9463da064a

Best regards,

Signature · 1 line
Javier Carrasco <[email protected]>
author maintainer
#1 lore raw

PATCH v5 1/4 dt-bindings: iio: light: veml6030: add veml6031x00 ALS series

These ambient light sensors share their properties with the ones from the same manufacturer that are supported by this bindings.

Note that only two datasheets are provided as every one of them covers two devices (veml6031x00/veml60311x00 and veml6031x01/veml60311x01).

---
 .../devicetree/bindings/iio/light/vishay,veml6030.yaml      | 13 ++++++++++++-
 MAINTAINERS                                                 |  5 +++++
 2 files changed, 17 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +12 -1
index 0041e1db6838..6a387c76df35 100644
@@ -4,7 +4,8 @@
 $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: VEML3235, VEML3328, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
+title: VEML3235, VEML3328, VEML6030, VEML6031x00 series, VEML6035
+and VEML7700 Ambient Light Sensors (ALS)
 
 maintainers:
   - Rishi Gupta <[email protected]>
@@ -23,6 +24,8 @@ description: |
     https://www.vishay.com/docs/80131/veml3235.pdf
     https://www.vishay.com/docs/84968/veml3328.pdf
     https://www.vishay.com/docs/84366/veml6030.pdf
+    https://www.vishay.com/docs/80007/veml6031x00.pdf
+    https://www.vishay.com/docs/80008/veml6031x01.pdf
     https://www.vishay.com/docs/84889/veml6035.pdf
     https://www.vishay.com/docs/84286/veml7700.pdf
 
@@ -32,6 +35,10 @@ properties:
       - vishay,veml3235
       - vishay,veml3328
       - vishay,veml6030
+      - vishay,veml6031x00
+      - vishay,veml6031x01
+      - vishay,veml60311x00
+      - vishay,veml60311x01
       - vishay,veml6035
       - vishay,veml7700
 
@@ -69,6 +76,8 @@ allOf:
       properties:
         compatible:
           enum:
+            - vishay,veml6031x00
+            - vishay,veml6031x01
             - vishay,veml6035
     then:
       properties:
@@ -82,6 +91,8 @@ allOf:
           enum:
             - vishay,veml3235
             - vishay,veml3328
+            - vishay,veml60311x00
+            - vishay,veml60311x01
             - vishay,veml7700
     then:
       properties:
MAINTAINERS +5 -0
index 15011f5752a9..4369cbf8b5ca 100644
@@ -28761,6 +28761,11 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml
 F:	drivers/iio/light/veml6030.c
 
+VISHAY VEML6031X00 AMBIENT LIGHT SENSOR DRIVER
+M:	Javier Carrasco <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml
+
 VISHAY VEML6046X00 RGBIR COLOR SENSOR DRIVER
 M:	Andreas Klinger <[email protected]>
 S:	Maintained
Signature · 1 line
2.43.0
author maintainer
#7 lore raw

PATCH v5 2/4 iio: light: add support for veml6031x00 ALS series

In reply to Javier Carrasco

These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.

This first implementation provides basic functionality (measurement configuration, raw reads and ID validation) and defines the different register regions in preparation for extended features in the subsequent patches of the series.

---
 MAINTAINERS                     |   1 +
 drivers/iio/light/Kconfig       |  12 +
 drivers/iio/light/Makefile      |   1 +
 drivers/iio/light/veml6031x00.c | 695 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 709 insertions(+)
MAINTAINERS +1 -0
index 4369cbf8b5ca..85b35948da32 100644
@@ -28765,6 +28765,7 @@ VISHAY VEML6031X00 AMBIENT LIGHT SENSOR DRIVER
 M:	Javier Carrasco <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml
+F:	drivers/iio/light/veml6031x00.c
 
 VISHAY VEML6046X00 RGBIR COLOR SENSOR DRIVER
 M:	Andreas Klinger <[email protected]>
drivers/iio/light/Kconfig +12 -0
index ef36824f312f..985b776e71d6 100644
@@ -738,6 +738,18 @@ config VEML6030
 	  To compile this driver as a module, choose M here: the
 	  module will be called veml6030.
 
+config VEML6031X00
+	tristate "VEML6031X00 ambient light sensor series"
+	select REGMAP_I2C
+	select IIO_GTS_HELPER
+	depends on I2C
+	help
+	  Say Y here if you want to build a driver for the Vishay VEML6031X00
+	  ambient light sensor series.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called veml6031x00.
+
 config VEML6040
 	tristate "VEML6040 RGBW light sensor"
 	select REGMAP_I2C
drivers/iio/light/Makefile +1 -0
index 64e354c49ed8..6672ed01a819 100644
@@ -68,6 +68,7 @@ obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
 obj-$(CONFIG_VEML3235)		+= veml3235.o
 obj-$(CONFIG_VEML3328)		+= veml3328.o
 obj-$(CONFIG_VEML6030)		+= veml6030.o
+obj-$(CONFIG_VEML6031X00)	+= veml6031x00.o
 obj-$(CONFIG_VEML6040)		+= veml6040.o
 obj-$(CONFIG_VEML6046X00)	+= veml6046x00.o
 obj-$(CONFIG_VEML6070)		+= veml6070.o
drivers/iio/light/veml6031x00.c new file +695 -0

695 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#14 lore raw

PATCH v5 3/4 iio: light: veml6031x00: add support for triggered buffers

In reply to Javier Carrasco

Add triggered buffer functionality for the two channels the device provides (ALS and IR).

---
 drivers/iio/light/Kconfig       |   2 +
 drivers/iio/light/veml6031x00.c | 100 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)
drivers/iio/light/Kconfig +2 -0
index 985b776e71d6..397b3e0d4408 100644
@@ -742,6 +742,8 @@ config VEML6031X00
 	tristate "VEML6031X00 ambient light sensor series"
 	select REGMAP_I2C
 	select IIO_GTS_HELPER
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
 	depends on I2C
 	help
 	  Say Y here if you want to build a driver for the Vishay VEML6031X00
drivers/iio/light/veml6031x00.c +100 -0

142 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#17 lore raw

PATCH v5 4/4 iio: light: veml6031x00: add support for events and trigger

In reply to Javier Carrasco

The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.

---
 drivers/iio/light/veml6031x00.c | 538 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 532 insertions(+), 6 deletions(-)
drivers/iio/light/veml6031x00.c +532 -6
index 0cccd40d729e..e193f87e6740 100644
@@ -22,6 +22,9 @@
 #include <linux/regulator/consumer.h>
 
 #include <linux/iio/iio.h>
+#include <linux/iio/events.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger.h>
 #include <linux/iio/iio-gts-helper.h>
 #include <linux/iio/triggered_buffer.h>
 #include <linux/iio/trigger_consumer.h>
@@ -29,16 +32,29 @@
 /* Device registers */
 #define VEML6031X00_REG_CONF0       0x00
 #define VEML6031X00_REG_CONF1       0x01
+#define VEML6031X00_REG_WH_L        0x04
+#define VEML6031X00_REG_WH_H        0x05
+#define VEML6031X00_REG_WL_L        0x06
+#define VEML6031X00_REG_WL_H        0x07
 #define VEML6031X00_REG_ALS_L       0x10
 #define VEML6031X00_REG_ALS_H       0x11
 #define VEML6031X00_REG_IR_L        0x12
 #define VEML6031X00_REG_IR_H        0x13
 #define VEML6031X00_REG_ID_L        0x14
 #define VEML6031X00_REG_ID_H        0x15
+#define VEML6031X00_REG_INT         0x17
 
 /* Bit masks for specific functionality */
 #define VEML6031X00_CONF0_SD        BIT(0)
+#define VEML6031X00_CONF0_AF_TRIG   BIT(2)
+#define VEML6031X00_CONF0_AF        BIT(3)
 #define VEML6031X00_CONF1_IR_SD     BIT(7)
+#define VEML6031X00_INT_TH_H        BIT(1)
+#define VEML6031X00_INT_TH_L        BIT(2)
+#define VEML6031X00_INT_DRDY        BIT(3)
+#define VEML6031X00_INT_MASK        (VEML6031X00_INT_TH_L | \
+				     VEML6031X00_INT_TH_H | \
+				     VEML6031X00_INT_DRDY)
 
 enum veml6031x00_scan {
 	VEML6031X00_SCAN_ALS,
@@ -48,8 +64,10 @@ enum veml6031x00_scan {
 
 struct veml6031x00_rf {
 	struct regmap_field *gain;
+	struct regmap_field *int_en;
 	struct regmap_field *it;
 	struct regmap_field *pd_div4;
+	struct regmap_field *pers;
 };
 
 struct veml6031x00_chip {
@@ -60,6 +78,7 @@ struct veml6031x00_chip {
 struct veml6031x00_data {
 	struct iio_gts gts;
 	struct regmap *regmap;
+	struct iio_trigger *trig;
 	struct veml6031x00_rf rf;
 	const struct veml6031x00_chip *chip;
 	/*
@@ -68,6 +87,14 @@ struct veml6031x00_data {
 	 * consistent set.
 	 */
 	struct mutex scale_lock;
+	/*
+	 * Serialize access to irq enable/disable by events and trigger
+	 * (shared line)
+	 */
+	struct mutex irq_lock;
+	int int_users;
+	bool ev_en;
+	bool trig_en;
 };
 
 static const struct iio_itime_sel_mul veml6031x00_it_sel[] = {
@@ -102,6 +129,17 @@ static const struct iio_gain_sel_pair veml6031x00_gain_sel[] = {
 	GAIN_SCALE_GAIN(16, VEML6031X00_SEL_GAIN_2_000),
 };
 
+static IIO_CONST_ATTR(in_illuminance_thresh_either_period_available, "1 2 4 8");
+
+static struct attribute *veml6031x00_event_attributes[] = {
+	&iio_const_attr_in_illuminance_thresh_either_period_available.dev_attr.attr,
+	NULL
+};
+
+static const struct attribute_group veml6031x00_event_attr_group = {
+	.attrs = veml6031x00_event_attributes,
+};
+
 /*
  * The shutdown bits (SD and ALS_IR_SD) are in different registers, which
  * must be written together when configuring the device.
@@ -139,6 +177,23 @@ static void veml6031x00_als_shutdown_action(void *data)
 			"Failed to shut down device: %d\n", ret);
 }
 
+static const struct iio_event_spec veml6031x00_event_spec[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_RISING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_PERIOD) |
+				 BIT(IIO_EV_INFO_ENABLE),
+	},
+};
+
 static const struct iio_chan_spec veml6031x00_channels[] = {
 	{
 		.type = IIO_LIGHT,
@@ -158,9 +213,47 @@ static const struct iio_chan_spec veml6031x00_channels[] = {
 	}
 };
 
+static const struct iio_chan_spec veml6031x00_channels_irq[] = {
+	{
+		.type = IIO_LIGHT,
+		.address = VEML6031X00_REG_ALS_L,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_INT_TIME) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME) |
+						BIT(IIO_CHAN_INFO_SCALE),
+		.event_spec = veml6031x00_event_spec,
+		.num_event_specs = ARRAY_SIZE(veml6031x00_event_spec),
+		.scan_index = VEML6031X00_SCAN_ALS,
+		.scan_type = {
+			.format = IIO_SCAN_FORMAT_UNSIGNED_INT,
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_INTENSITY,
+		.address = VEML6031X00_REG_IR_L,
+		.modified = 1,
+		.channel2 = IIO_MOD_LIGHT_IR,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_index = VEML6031X00_SCAN_IR,
+		.scan_type = {
+			.format = IIO_SCAN_FORMAT_UNSIGNED_INT,
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(VEML6031X00_SCAN_TIMESTAMP),
+};
+
 static const struct regmap_range veml6031x00_readable_ranges[] = {
 	regmap_reg_range(VEML6031X00_REG_CONF0, VEML6031X00_REG_CONF1),
+	regmap_reg_range(VEML6031X00_REG_WH_L, VEML6031X00_REG_WL_H),
 	regmap_reg_range(VEML6031X00_REG_ALS_L, VEML6031X00_REG_ID_H),
+	regmap_reg_range(VEML6031X00_REG_INT, VEML6031X00_REG_INT),
 };
 
 static const struct regmap_access_table veml6031x00_readable_table = {
@@ -169,7 +262,7 @@ static const struct regmap_access_table veml6031x00_readable_table = {
 };
 
 static const struct regmap_range veml6031x00_writable_ranges[] = {
-	regmap_reg_range(VEML6031X00_REG_CONF0, VEML6031X00_REG_CONF1),
+	regmap_reg_range(VEML6031X00_REG_CONF0, VEML6031X00_REG_WL_H),
 };
 
 static const struct regmap_access_table veml6031x00_writable_table = {
@@ -181,6 +274,7 @@ static const struct regmap_range veml6031x00_volatile_ranges[] = {
 	/* AF_TRIG in CONF0 is volatile */
 	regmap_reg_range(VEML6031X00_REG_CONF0, VEML6031X00_REG_CONF0),
 	regmap_reg_range(VEML6031X00_REG_ALS_L, VEML6031X00_REG_IR_H),
+	regmap_reg_range(VEML6031X00_REG_INT, VEML6031X00_REG_INT),
 };
 
 static const struct regmap_access_table veml6031x00_volatile_table = {
@@ -188,6 +282,15 @@ static const struct regmap_access_table veml6031x00_volatile_table = {
 	.n_yes_ranges = ARRAY_SIZE(veml6031x00_volatile_ranges),
 };
 
+static const struct regmap_range veml6031x00_precious_ranges[] = {
+	regmap_reg_range(VEML6031X00_REG_INT, VEML6031X00_REG_INT),
+};
+
+static const struct regmap_access_table veml6031x00_precious_table = {
+	.yes_ranges = veml6031x00_precious_ranges,
+	.n_yes_ranges = ARRAY_SIZE(veml6031x00_precious_ranges),
+};
+
 static const struct regmap_config veml6031x00_regmap_config = {
 	.name = "veml6031x00_regmap",
 	.reg_bits = 8,
@@ -195,13 +298,20 @@ static const struct regmap_config veml6031x00_regmap_config = {
 	.rd_table = &veml6031x00_readable_table,
 	.wr_table = &veml6031x00_writable_table,
 	.volatile_table = &veml6031x00_volatile_table,
-	.max_register = VEML6031X00_REG_ID_H,
+	.precious_table = &veml6031x00_precious_table,
+	.max_register = VEML6031X00_REG_INT,
 	.cache_type = REGCACHE_MAPLE,
 };
 
+static const struct reg_field veml6031x00_rf_int_en =
+	REG_FIELD(VEML6031X00_REG_CONF0, 1, 1);
+
 static const struct reg_field veml6031x00_rf_it =
 	REG_FIELD(VEML6031X00_REG_CONF0, 4, 6);
 
+static const struct reg_field veml6031x00_rf_pers =
+	REG_FIELD(VEML6031X00_REG_CONF1, 1, 2);
+
 static const struct reg_field veml6031x00_rf_gain =
 	REG_FIELD(VEML6031X00_REG_CONF1, 3, 4);
 
@@ -219,6 +329,12 @@ static int veml6031x00_regfield_init(struct veml6031x00_data *data)
 		return PTR_ERR(rm_field);
 	rf->gain = rm_field;
 
+	rm_field = devm_regmap_field_alloc(regmap_get_device(data->regmap),
+					   data->regmap, veml6031x00_rf_int_en);
+	if (IS_ERR(rm_field))
+		return PTR_ERR(rm_field);
+	rf->int_en = rm_field;
+
 	rm_field = devm_regmap_field_alloc(regmap_get_device(data->regmap),
 					   data->regmap, veml6031x00_rf_it);
 	if (IS_ERR(rm_field))
@@ -231,6 +347,12 @@ static int veml6031x00_regfield_init(struct veml6031x00_data *data)
 		return PTR_ERR(rm_field);
 	rf->pd_div4 = rm_field;
 
+	rm_field = devm_regmap_field_alloc(regmap_get_device(data->regmap),
+					   data->regmap, veml6031x00_rf_pers);
+	if (IS_ERR(rm_field))
+		return PTR_ERR(rm_field);
+	rf->pers = rm_field;
+
 	return 0;
 }
 
@@ -327,6 +449,30 @@ static int veml6031x00_set_it(struct iio_dev *iio, int val, int val2)
 	return veml6031x00_write_gain(data, gain_sel);
 }
 
+static int veml6031x00_read_period(struct iio_dev *iio, int *val)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret, regval;
+
+	ret = regmap_field_read(data->rf.pers, &regval);
+	if (ret)
+		return ret;
+
+	*val = 1 << regval;
+
+	return IIO_VAL_INT;
+}
+
+static int veml6031x00_write_period(struct iio_dev *iio, int val)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+
+	if (val < 0 || val > 8 || hweight8(val) != 1)
+		return -EINVAL;
+
+	return regmap_field_write(data->rf.pers, ffs(val) - 1);
+}
+
 static int veml6031x00_set_scale(struct iio_dev *iio, int val, int val2)
 {
 	struct veml6031x00_data *data = iio_priv(iio);
@@ -381,6 +527,52 @@ static int veml6031x00_get_scale(struct veml6031x00_data *data, int *val,
 	return IIO_VAL_INT_PLUS_NANO;
 }
 
+static int veml6031x00_read_th(struct iio_dev *iio, int *val, int *val2, int dir)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	__le16 regval;
+	int ret;
+
+	if (dir == IIO_EV_DIR_RISING)
+		ret = regmap_bulk_read(data->regmap, VEML6031X00_REG_WH_L,
+				       &regval, sizeof(regval));
+	else
+		ret = regmap_bulk_read(data->regmap, VEML6031X00_REG_WL_L,
+				       &regval, sizeof(regval));
+	if (ret)
+		return ret;
+
+	*val = le16_to_cpu(regval);
+
+	return IIO_VAL_INT;
+}
+
+static int veml6031x00_write_th(struct iio_dev *iio, int val, int val2, int dir)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	__le16 regval = cpu_to_le16(val);
+	int ret;
+
+	if (val < 0 || val > U16_MAX || val2)
+		return -EINVAL;
+
+	if (dir == IIO_EV_DIR_RISING) {
+		ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WH_L,
+					&regval, sizeof(regval));
+		if (ret)
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to set high threshold %d\n", ret);
+	} else {
+		ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WL_L,
+					&regval, sizeof(regval));
+		if (ret)
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to set low threshold %d\n", ret);
+	}
+
+	return ret;
+}
+
 static int veml6031x00_single_read(struct iio_dev *iio, enum iio_chan_type type,
 				   int *val)
 {
@@ -494,13 +686,205 @@ static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev,
 	}
 }
 
+static int veml6031x00_set_interrupt(struct veml6031x00_data *data, bool state)
+	__must_hold(&data->irq_lock)
+{
+	int ret;
+
+	if (state) {
+		data->int_users++;
+		if (data->int_users > 1)
+			return 0;
+	} else {
+		data->int_users--;
+		if (data->int_users > 0)
+			return 0;
+	}
+
+	ret = regmap_field_write(data->rf.int_en, state);
+	if (ret) {
+		if (state)
+			data->int_users--;
+		else
+			data->int_users++;
+	}
+
+	return ret;
+}
+
+static int veml6031x00_read_event_val(struct iio_dev *iio,
+				      const struct iio_chan_spec *chan,
+				      enum iio_event_type type,
+				      enum iio_event_direction dir,
+				      enum iio_event_info info,
+				      int *val, int *val2)
+{
+	switch (type) {
+	case IIO_EV_TYPE_THRESH:
+		if (dir == IIO_EV_DIR_EITHER && info == IIO_EV_INFO_PERIOD)
+			return veml6031x00_read_period(iio, val);
+
+		return veml6031x00_read_th(iio, val, val2, dir);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int veml6031x00_write_event_val(struct iio_dev *iio,
+				       const struct iio_chan_spec *chan,
+				       enum iio_event_type type,
+				       enum iio_event_direction dir,
+				       enum iio_event_info info,
+				       int val, int val2)
+{
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		return veml6031x00_write_th(iio, val, val2, dir);
+	case IIO_EV_INFO_PERIOD:
+		return veml6031x00_write_period(iio, val);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int veml6031x00_read_event_config(struct iio_dev *iio,
+					 const struct iio_chan_spec *chan,
+					 enum iio_event_type type,
+					 enum iio_event_direction dir)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+
+	guard(mutex)(&data->irq_lock);
+
+	return data->ev_en;
+}
+
+static int veml6031x00_write_event_config(struct iio_dev *iio,
+					  const struct iio_chan_spec *chan,
+					  enum iio_event_type type,
+					  enum iio_event_direction dir,
+					  bool state)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret;
+
+	guard(mutex)(&data->irq_lock);
+
+	/* avoid multiple increments/decrements from one source */
+	if (state == data->ev_en)
+		return 0;
+
+	if (state) {
+		ret = pm_runtime_resume_and_get(regmap_get_device(data->regmap));
+		if (ret)
+			return ret;
+	}
+
+	ret = veml6031x00_set_interrupt(data, state);
+	if (ret) {
+		if (state)
+			pm_runtime_put_autosuspend(regmap_get_device(data->regmap));
+		return ret;
+	}
+
+	data->ev_en = state;
+
+	if (!state)
+		pm_runtime_put_autosuspend(regmap_get_device(data->regmap));
+
+	return 0;
+}
+
+static void veml6031x00_disable_event_action(void *arg)
+{
+	struct veml6031x00_data *data = arg;
+	int ret;
+
+	guard(mutex)(&data->irq_lock);
+
+	if (!data->ev_en)
+		return;
+
+	ret = veml6031x00_set_interrupt(data, false);
+	if (ret)
+		dev_err(regmap_get_device(data->regmap),
+			"Failed to disable events: %d\n", ret);
+
+	data->ev_en = false;
+	pm_runtime_put_autosuspend(regmap_get_device(data->regmap));
+}
+
 static const struct iio_info veml6031x00_info = {
 	.read_raw = veml6031x00_read_raw,
 	.read_avail = veml6031x00_read_avail,
 	.write_raw = veml6031x00_write_raw,
 	.write_raw_get_fmt = veml6031x00_write_raw_get_fmt,
+	.read_event_value = veml6031x00_read_event_val,
+	.write_event_value = veml6031x00_write_event_val,
+	.read_event_config = veml6031x00_read_event_config,
+	.write_event_config = veml6031x00_write_event_config,
+	.event_attrs = &veml6031x00_event_attr_group,
+};
+
+static const struct iio_info veml6031x00_info_no_irq = {
+	.read_raw = veml6031x00_read_raw,
+	.read_avail = veml6031x00_read_avail,
+	.write_raw = veml6031x00_write_raw,
+	.write_raw_get_fmt = veml6031x00_write_raw_get_fmt,
 };
 
+static irqreturn_t veml6031x00_interrupt(int irq, void *private)
+{
+	struct iio_dev *iio = private;
+	struct veml6031x00_data *data = iio_priv(iio);
+	s64 timestamp;
+	int regval, ret;
+	bool trigger_poll;
+
+	scoped_guard(mutex, &data->irq_lock) {
+		ret = regmap_read(data->regmap, VEML6031X00_REG_INT, &regval);
+		if (ret) {
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to read interrupt register %d\n", ret);
+			return IRQ_NONE;
+		}
+
+		if (!(regval & VEML6031X00_INT_MASK))
+			return IRQ_NONE;
+
+		if ((regval & (VEML6031X00_INT_TH_H | VEML6031X00_INT_TH_L)) &&
+		    data->ev_en) {
+			timestamp = iio_get_time_ns(iio);
+
+			if (regval & VEML6031X00_INT_TH_H)
+				iio_push_event(iio,
+					       IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
+								    IIO_EV_TYPE_THRESH,
+								    IIO_EV_DIR_RISING),
+					       timestamp);
+			if (regval & VEML6031X00_INT_TH_L)
+				iio_push_event(iio,
+					       IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
+								    IIO_EV_TYPE_THRESH,
+								    IIO_EV_DIR_FALLING),
+					       timestamp);
+		}
+
+		trigger_poll = (regval & VEML6031X00_INT_DRDY) && data->trig_en;
+	}
+
+	/*
+	 * iio_trigger_poll_nested() must be called with irq_lock released:
+	 * iio_trigger_poll_nested() runs trig_handler() synchronously in this
+	 * thread, which calls reenable() on completion, and that callback also
+	 * takes irq_lock.
+	 */
+	if (trigger_poll)
+		iio_trigger_poll_nested(data->trig);
+
+	return IRQ_HANDLED;
+}
+
 static int veml6031x00_buffer_preenable(struct iio_dev *iio)
 {
 	struct veml6031x00_data *data = iio_priv(iio);
@@ -535,11 +919,78 @@ static int veml6031x00_buffer_postdisable(struct iio_dev *iio)
 	return 0;
 }
 
+static int veml6031x00_set_trigger_state(struct iio_trigger *trig, bool state)
+{
+	struct iio_dev *iio = iio_trigger_get_drvdata(trig);
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret;
+
+	guard(mutex)(&data->irq_lock);
+
+	if (state == data->trig_en)
+		return 0;
+
+	ret = veml6031x00_set_interrupt(data, state);
+	if (ret)
+		return ret;
+
+	/* The AF bit must be set before setting AF_TRIG */
+	ret = regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0,
+				 VEML6031X00_CONF0_AF,
+				 FIELD_PREP(VEML6031X00_CONF0_AF, state));
+	if (ret) {
+		veml6031x00_set_interrupt(data, !state);
+
+		return ret;
+	}
+
+	ret = regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0,
+				 VEML6031X00_CONF0_AF_TRIG,
+				 FIELD_PREP(VEML6031X00_CONF0_AF_TRIG, state));
+	if (ret) {
+		regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0,
+				   VEML6031X00_CONF0_AF,
+				   FIELD_PREP(VEML6031X00_CONF0_AF, !state));
+		veml6031x00_set_interrupt(data, !state);
+
+		return ret;
+	}
+
+	data->trig_en = state;
+
+	return 0;
+}
+
 static const struct iio_buffer_setup_ops veml6031x00_buffer_setup_ops = {
 	.preenable = veml6031x00_buffer_preenable,
 	.postdisable = veml6031x00_buffer_postdisable,
 };
 
+static void veml6031x00_trigger_reenable(struct iio_trigger *trig)
+{
+	struct iio_dev *iio = iio_trigger_get_drvdata(trig);
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret;
+
+	guard(mutex)(&data->irq_lock);
+
+	if (!data->trig_en)
+		return;
+
+	ret = regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0,
+				 VEML6031X00_CONF0_AF_TRIG,
+				 FIELD_PREP(VEML6031X00_CONF0_AF_TRIG, true));
+	if (ret)
+		dev_err(regmap_get_device(data->regmap),
+			"Failed to reenable trigger: %d\n", ret);
+}
+
+static const struct iio_trigger_ops veml6031x00_trigger_ops = {
+	.validate_device = iio_trigger_validate_own_device,
+	.set_trigger_state = veml6031x00_set_trigger_state,
+	.reenable = veml6031x00_trigger_reenable,
+};
+
 static irqreturn_t veml6031x00_trig_handler(int irq, void *p)
 {
 	struct iio_poll_func *pf = p;
@@ -597,7 +1048,8 @@ static int veml6031x00_validate_part_id(struct veml6031x00_data *data)
 static int veml6031x00_hw_init(struct iio_dev *iio)
 {
 	struct veml6031x00_data *data = iio_priv(iio);
-	int ret;
+	int ret, val;
+	__le16 regval;
 
 	/* Max resolution = 6.9632 lx/cnt for gain = 0.125 and IT = 3.125ms */
 	ret = devm_iio_init_iio_gts(regmap_get_device(data->regmap), 6, 963200000,
@@ -610,6 +1062,60 @@ static int veml6031x00_hw_init(struct iio_dev *iio)
 		return dev_err_probe(regmap_get_device(data->regmap), ret,
 				     "failed to init IIO GTS\n");
 
+	regval = 0;
+	ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WL_L, &regval,
+				sizeof(regval));
+	if (ret)
+		return dev_err_probe(regmap_get_device(data->regmap), ret,
+				     "Failed to set low threshold\n");
+
+	regval = cpu_to_le16(U16_MAX);
+	ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WH_L, &regval,
+				sizeof(regval));
+	if (ret)
+		return dev_err_probe(regmap_get_device(data->regmap), ret,
+				     "Failed to set high threshold\n");
+
+	ret = regmap_field_write(data->rf.int_en, 0);
+	if (ret)
+		return ret;
+
+	ret = regmap_read(data->regmap, VEML6031X00_REG_INT, &val);
+	if (ret)
+		return dev_err_probe(regmap_get_device(data->regmap), ret,
+				     "Failed to clear interrupts\n");
+
+	return 0;
+}
+
+static int veml6031x00_setup_irq(struct i2c_client *i2c, struct iio_dev *iio)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret;
+
+	data->trig = devm_iio_trigger_alloc(regmap_get_device(data->regmap),
+					    "%s-drdy%d", iio->name, iio_device_id(iio));
+	if (!data->trig)
+		return -ENOMEM;
+
+	data->trig->ops = &veml6031x00_trigger_ops;
+	iio_trigger_set_drvdata(data->trig, iio);
+
+	ret = devm_iio_trigger_register(regmap_get_device(data->regmap), data->trig);
+	if (ret)
+		return ret;
+
+	iio->trig = iio_trigger_get(data->trig);
+	ret = devm_request_threaded_irq(regmap_get_device(data->regmap),
+					i2c->irq, NULL,
+					veml6031x00_interrupt,
+					IRQF_ONESHOT,
+					iio->name, iio);
+	if (ret)
+		return dev_err_probe(regmap_get_device(data->regmap), ret,
+				     "Failed to request irq %d\n",
+				     i2c->irq);
+
 	return 0;
 }
 
@@ -636,15 +1142,29 @@ static int veml6031x00_probe(struct i2c_client *i2c)
 				     "Failed to set regmap\n");
 
 	iio->name = data->chip->name;
-	iio->channels = veml6031x00_channels;
-	iio->num_channels = ARRAY_SIZE(veml6031x00_channels);
 	iio->modes = INDIO_DIRECT_MODE;
-	iio->info = &veml6031x00_info;
+	if (i2c->irq) {
+		iio->channels = veml6031x00_channels_irq;
+		iio->num_channels = ARRAY_SIZE(veml6031x00_channels_irq);
+		ret = veml6031x00_setup_irq(i2c, iio);
+		if (ret)
+			return ret;
+
+		iio->info = &veml6031x00_info;
+	} else {
+		iio->channels = veml6031x00_channels;
+		iio->num_channels = ARRAY_SIZE(veml6031x00_channels);
+		iio->info = &veml6031x00_info_no_irq;
+	}
 
 	ret = devm_mutex_init(&i2c->dev, &data->scale_lock);
 	if (ret)
 		return ret;
 
+	ret = devm_mutex_init(&i2c->dev, &data->irq_lock);
+	if (ret)
+		return ret;
+
 	ret = veml6031x00_regfield_init(data);
 	if (ret)
 		return dev_err_probe(&i2c->dev, ret, "Failed to init regfield\n");
@@ -672,6 +1192,12 @@ static int veml6031x00_probe(struct i2c_client *i2c)
 	if (ret)
 		return dev_err_probe(&i2c->dev, ret, "Failed to get runtime PM\n");
 
+	ret = devm_add_action_or_reset(&i2c->dev,
+				       veml6031x00_disable_event_action, data);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret,
+				     "Failed to add event cleanup action\n");
+
 	ret = veml6031x00_validate_part_id(data);
 	if (ret)
 		return ret;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v5 1/4] dt-bindings: iio: light: veml6030: add veml6031x00 ALS series

In reply to Javier Carrasco
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The multiline `title` string in the YAML bindings lacks proper indentation on its continuation…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The multiline title string in the YAML bindings lacks proper indentation on its continuation line, resulting in a fatal YAML syntax error. --

--- Patch [1]: [PATCH v5 1/4] dt-bindings: iio: light: veml6030: add veml6031x00 ALS series --- commit 8d68b2e616d3ddf21759545fea44ab122ecc25e0
Author: Javier Carrasco <[email protected]>

dt-bindings: iio: light: veml6030: add veml6031x00 ALS series

This commit adds the veml6031x00 series of ambient light sensors to the vishay,veml6030 bindings, updating the required compatibility strings and datasheet references.

Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml line 8

Quoted 11 lines
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +2 -1
index 0041e1db68388..6a387c76df353 100644
@@ -4,7 +4,8 @@
 $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: VEML3235, VEML3328, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
+title: VEML3235, VEML3328, VEML6030, VEML6031x00 series, VEML6035
+and VEML7700 Ambient Light Sensors (ALS)

[Severity: Low]
Does this continuation line need to be indented?

Because it lacks indentation relative to the root mapping at column 0, the YAML parser treats it as a new implicit block mapping key rather than a continuation of the title scalar. This results in a syntax error during dt_binding_check.

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#8 lore raw
In reply to Javier Carrasco
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Mixing `devm_pm_runtime_get_noresume()` with a manual `pm_runtime_put_autosuspend()` in `probe`…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Mixing devm_pm_runtime_get_noresume() with a manual pm_runtime_put_autosuspend() in probe causes a PM usage counter underflow on device unbind or probe failure.
- [High] An unprotected read-modify-write in veml6031x00_set_power_state() races with sysfs configuration changes, causing lost user configuration, and a lock inversion prevents a simple fix. --

--- Patch [2]: [PATCH v5 2/4] iio: light: add support for veml6031x00 ALS series --- commit 2cb7c39d884f4873be61de5deba3865dbd24f394
Author: Javier Carrasco <[email protected]>

iio: light: add support for veml6031x00 ALS series

These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.

Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +0 -0
new file mode 100644
index 0000000000000..30cccda51ca08

[ ... ]

Quoted 11 lines
drivers/iio/light/veml6031x00.c new file +11 -0
+static int veml6031x00_set_power_state(struct veml6031x00_data *data, bool on)
+{
+	u8 conf[2];
+	int ret;
+
+	ret = regmap_bulk_read(data->regmap, VEML6031X00_REG_CONF0,
+			       conf, sizeof(conf));
+	if (ret)
+		return ret;
+
+	if (on) {

drivers/iio/light/veml6031x00.c line 120

Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10 -0
+		conf[0] &= ~VEML6031X00_CONF0_SD;
+		conf[1] &= ~VEML6031X00_CONF1_IR_SD;
+	} else {
+		conf[0] |= VEML6031X00_CONF0_SD;
+		conf[1] |= VEML6031X00_CONF1_IR_SD;
+	}
+
+	return regmap_bulk_write(data->regmap, VEML6031X00_REG_CONF0,
+				 conf, sizeof(conf));
+}

[Severity: High]
Does this unprotected read-modify-write race with concurrent sysfs configuration updates?

Because CONF0 and CONF1 are in the volatile ranges, regmap does not protect them with its cache lock. Concurrent regmap_field_write() calls triggered by veml6031x00_write_raw() could change the gain or integration time between the bulk_read and bulk_write here, causing the user configuration to be silently overwritten and lost.

Additionally, could acquiring the scale_lock here to fix the race cause a deadlock?

Looking at veml6031x00_single_read(), it holds the scale_lock before acquiring the PM runtime reference:

veml6031x00_single_read()

    guard(mutex)(&data->scale_lock);
    ...
    PM_RUNTIME_ACQUIRE_AUTOSUSPEND(...)

If the autosuspend acquisition triggers veml6031x00_runtime_resume() which then calls veml6031x00_set_power_state(), attempting to take scale_lock again would deadlock.

[ ... ]

Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2 -0
+static int veml6031x00_probe(struct i2c_client *i2c)
+{

[ ... ]

Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +7 -0
+	ret = devm_pm_runtime_set_active_enabled(&i2c->dev);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret, "Failed to enable runtime PM\n");
+
+	ret = devm_pm_runtime_get_noresume(&i2c->dev);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret, "Failed to get runtime PM\n");

drivers/iio/light/veml6031x00.c line 591

Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10 -0
+
+	ret = veml6031x00_validate_part_id(data);
+	if (ret)
+		return ret;
+
+	ret = veml6031x00_hw_init(iio);
+	if (ret)
+		return ret;
+
+	pm_runtime_put_autosuspend(&i2c->dev);

[Severity: High]
Will mixing devm_pm_runtime_get_noresume() with a manual pm_runtime_put_autosuspend() here cause a PM usage counter underflow?

The devm_pm_runtime_get_noresume() call increments the usage counter and registers a devres action to decrement it on unbind or probe failure.

However, the manual pm_runtime_put_autosuspend() here drops the counter back to zero.

When the driver is subsequently unbound, the devres action will execute and decrement the counter again to -1, which could corrupt the runtime PM state machine and prevent the device or its parent from suspending.

Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +7 -0
+
+	ret = devm_iio_device_register(&i2c->dev, iio);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret, "Failed to register iio device\n");
+
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#3 lore raw

Re: [PATCH v5 1/4] dt-bindings: iio: light: veml6030: add veml6031x00 ALS series

In reply to [email protected]
Quoted 29 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The multiline title string in the YAML bindings lacks proper indentation on its continuation line, resulting in a fatal YAML syntax error. --

--- Patch [1]: [PATCH v5 1/4] dt-bindings: iio: light: veml6030: add veml6031x00 ALS series --- commit 8d68b2e616d3ddf21759545fea44ab122ecc25e0
Author: Javier Carrasco <[email protected]>

dt-bindings: iio: light: veml6030: add veml6031x00 ALS series

This commit adds the veml6031x00 series of ambient light sensors to the vishay,veml6030 bindings, updating the required compatibility strings and datasheet references.

Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml line 8

Quoted 11 lines
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +2 -1
index 0041e1db68388..6a387c76df353 100644
@@ -4,7 +4,8 @@
 $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: VEML3235, VEML3328, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
+title: VEML3235, VEML3328, VEML6030, VEML6031x00 series, VEML6035
+and VEML7700 Ambient Light Sensors (ALS)

[Severity: Low]
Does this continuation line need to be indented?

Quoted 6 lines

Because it lacks indentation relative to the root mapping at column 0, the YAML parser treats it as a new implicit block mapping key rather than a continuation of the title scalar. This results in a syntax error during dt_binding_check.

[ ... ]

This is right, another device was added to the title between versions and this 2-line title need two white spaces at the beginning of the second line. I just validated it with dt_binding_check, and it will be added to V6.

Best regards,
Javier

#15 lore raw

Re: [PATCH v5 3/4] iio: light: veml6031x00: add support for triggered buffers

In reply to Javier Carrasco
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] AB-BA deadlock between scale_lock and IIO direct mode lock - [High] Missing IIO buffer channel…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] AB-BA deadlock between scale_lock and IIO direct mode lock
- [High] Missing IIO buffer channel configuration causes probe failure
- [High] Uninitialized kernel stack data leaked to userspace in IIO triggered buffer
- [High] Unbalanced pm_runtime put leads to negative usage count
--

--- Patch [3]: [PATCH v5 3/4] iio: light: veml6031x00: add support for triggered buffers --- commit a5266734ac0d8d96ec4ee20e5c8d6088863dee05
Author: Javier Carrasco <[email protected]>

iio: light: veml6031x00: add support for triggered buffers

Add triggered buffer functionality for the two channels the device provides (ambient light sensor and IR).

Quoted 4 lines
drivers/iio/light/veml6031x00.c +0 -0
index 30cccda51ca08..0cccd40d729eb 100644

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_single_read(struct iio_dev *iio, enum iio_chan_type type, · line 407

Quoted 8 lines
drivers/iio/light/veml6031x00.c +4 -0
@@ -392,6 +401,10 @@ static int veml6031x00_single_read(struct iio_dev *iio, enum iio_chan_type type,
 
 	guard(mutex)(&data->scale_lock);
 
+	IIO_DEV_ACQUIRE_DIRECT_MODE(iio, claim);
+	if (IIO_DEV_ACQUIRE_FAILED(claim))
+		return -EBUSY;
+

[Severity: High]
Does this establish a lock order of scale_lock followed by the IIO direct mode lock?

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw(struct iio_dev *iio, · line 472

Quoted 8 lines
drivers/iio/light/veml6031x00.c +4 -0
@@ -453,6 +466,10 @@ static int veml6031x00_write_raw(struct iio_dev *iio,
 				 struct iio_chan_spec const *chan,
 				 int val, int val2, long mask)
 {
+	IIO_DEV_ACQUIRE_DIRECT_MODE(iio, claim);
+	if (IIO_DEV_ACQUIRE_FAILED(claim))
+		return -EBUSY;
+

[Severity: High]
Can this cause a deadlock?

In veml6031x00_single_read(), data->scale_lock is acquired before the IIO direct mode lock.

However, here the IIO direct mode lock is acquired first. If this then calls veml6031x00_set_it(), which acquires data->scale_lock:

veml6031x00_write_raw()

    IIO_DEV_ACQUIRE_DIRECT_MODE(iio, claim)
    veml6031x00_set_it()
        guard(mutex)(&data->scale_lock)

Could concurrent sysfs access to raw data read and integration time write lead to a deadlock?

Quoted 3 lines
drivers/iio/light/veml6031x00.c +0 -0
 	switch (mask) {
 	case IIO_CHAN_INFO_INT_TIME:
 		return veml6031x00_set_it(iio, val, val2);

[ ... ]

Quoted 1 line
drivers/iio/light/veml6031x00.c +0 -0
@@ -484,6 +501,81 @@ static const struct iio_info veml6031x00_info = {

[ ... ]

drivers/iio/light/veml6031x00.c static const struct iio_info veml6031x00_info = { · line 548

Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+static irqreturn_t veml6031x00_trig_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6031x00_data *data = iio_priv(iio);
+	IIO_DECLARE_BUFFER_WITH_TS(__le16, scan, 2);

[Severity: High]
Does this leak uninitialized stack memory to userspace?

The scan buffer is declared without zero-initialization.

When reading only a single active channel (e.g., just the ambient light sensor), the driver only writes 2 bytes into the array, leaving the unpopulated channel slots and structure padding uninitialized.

Will iio_push_to_buffers_with_ts() push this uninitialized kernel stack memory to the ring buffer? Should the scan structure be zero-initialized?

Quoted 2 lines
drivers/iio/light/veml6031x00.c +2 -0
+	unsigned int i = 0;
+	int ch, ret;

[ ... ]

Quoted 1 line
drivers/iio/light/veml6031x00.c +0 -0
@@ -588,6 +680,14 @@ static int veml6031x00_probe(struct i2c_client *i2c)

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 690

Quoted 8 lines
drivers/iio/light/veml6031x00.c +8 -0
+	ret = devm_iio_triggered_buffer_setup(regmap_get_device(data->regmap), iio,
+					      iio_pollfunc_store_time,
+					      veml6031x00_trig_handler,
+					      &veml6031x00_buffer_setup_ops);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret,
+				     "Failed to register triggered buffer\n");
+

[Severity: High]
Will this probe fail with -EINVAL?

The triggered buffer is registered here, but it appears the driver's channels array (veml6031x00_channels) was not updated with .scan_index, .scan_type, or a timestamp channel.

Without .scan_index, the IIO core defaults both channels to index 0.

Does iio_device_register() abort with -EINVAL during probe when iio_check_unique_scan_index() detects the duplicate scan index 0?

Quoted 1 line

pm_runtime_put_autosuspend(&i2c->dev);

[Severity: High]
Is this pm_runtime_put_autosuspend() call unbalanced?

The probe function appears to use devm_pm_runtime_get_noresume() which automatically queues a put on unbind.

If we explicitly decrement the usage counter here, will the devres action firing on unbind drop the counter below zero and trigger a warning?

Should this use the non-devm pm_runtime_get_noresume() instead if the driver intends to manually drop the reference later?

Quoted 2 lines

ret = devm_iio_device_register(&i2c->dev, iio);

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#9 lore raw
In reply to [email protected]
Quoted 66 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Mixing devm_pm_runtime_get_noresume() with a manual pm_runtime_put_autosuspend() in probe causes a PM usage counter underflow on device unbind or probe failure.
- [High] An unprotected read-modify-write in veml6031x00_set_power_state() races with sysfs configuration changes, causing lost user configuration, and a lock inversion prevents a simple fix. --

--- Patch [2]: [PATCH v5 2/4] iio: light: add support for veml6031x00 ALS series --- commit 2cb7c39d884f4873be61de5deba3865dbd24f394
Author: Javier Carrasco <[email protected]>

iio: light: add support for veml6031x00 ALS series

These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.

Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +0 -0
new file mode 100644
index 0000000000000..30cccda51ca08

[ ... ]

Quoted 11 lines
drivers/iio/light/veml6031x00.c new file +11 -0
+static int veml6031x00_set_power_state(struct veml6031x00_data *data, bool on)
+{
+	u8 conf[2];
+	int ret;
+
+	ret = regmap_bulk_read(data->regmap, VEML6031X00_REG_CONF0,
+			       conf, sizeof(conf));
+	if (ret)
+		return ret;
+
+	if (on) {

drivers/iio/light/veml6031x00.c line 120

Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10 -0
+		conf[0] &= ~VEML6031X00_CONF0_SD;
+		conf[1] &= ~VEML6031X00_CONF1_IR_SD;
+	} else {
+		conf[0] |= VEML6031X00_CONF0_SD;
+		conf[1] |= VEML6031X00_CONF1_IR_SD;
+	}
+
+	return regmap_bulk_write(data->regmap, VEML6031X00_REG_CONF0,
+				 conf, sizeof(conf));
+}

[Severity: High]
Does this unprotected read-modify-write race with concurrent sysfs configuration updates?

Because CONF0 and CONF1 are in the volatile ranges, regmap does not protect them with its cache lock. Concurrent regmap_field_write() calls triggered by veml6031x00_write_raw() could change the gain or integration time between the bulk_read and bulk_write here, causing the user configuration to be silently overwritten and lost.

Additionally, could acquiring the scale_lock here to fix the race cause a deadlock?

Looking at veml6031x00_single_read(), it holds the scale_lock before acquiring the PM runtime reference:

veml6031x00_single_read()

    guard(mutex)(&data->scale_lock);
    ...
    PM_RUNTIME_ACQUIRE_AUTOSUSPEND(...)
Quoted 6 lines

If the autosuspend acquisition triggers veml6031x00_runtime_resume() which then calls veml6031x00_set_power_state(), attempting to take scale_lock again would deadlock.

[ ... ]

This issue can be avoided by using regmap_update_bits() instead, which actually was used in previous versions. I modified it to test the difference after I get confirmation from the manufacturer that the registers must be updated one after the other, not at the same time. I will get back to regmap_update_bits().

Quoted 30 lines
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2 -0
+static int veml6031x00_probe(struct i2c_client *i2c)
+{

[ ... ]

Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +7 -0
+	ret = devm_pm_runtime_set_active_enabled(&i2c->dev);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret, "Failed to enable runtime PM\n");
+
+	ret = devm_pm_runtime_get_noresume(&i2c->dev);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret, "Failed to get runtime PM\n");

drivers/iio/light/veml6031x00.c line 591

Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10 -0
+
+	ret = veml6031x00_validate_part_id(data);
+	if (ret)
+		return ret;
+
+	ret = veml6031x00_hw_init(iio);
+	if (ret)
+		return ret;
+
+	pm_runtime_put_autosuspend(&i2c->dev);

[Severity: High]
Will mixing devm_pm_runtime_get_noresume() with a manual pm_runtime_put_autosuspend() here cause a PM usage counter underflow?

The devm_pm_runtime_get_noresume() call increments the usage counter and registers a devres action to decrement it on unbind or probe failure.

Quoted 7 lines

However, the manual pm_runtime_put_autosuspend() here drops the counter back to zero.

When the driver is subsequently unbound, the devres action will execute and decrement the counter again to -1, which could corrupt the runtime PM state machine and prevent the device or its parent from suspending.

This was discussed in V4, and it is not a real issue because this configuration can never reach to decrement the counter to -1 (long story
short: atomic_add_unless(&dev->power.usage_count, -1, 0)). It was also validated by loading and unloading the driver under different circumstances.

Best regards,
Javier

#18 lore raw

Re: [PATCH v5 4/4] iio: light: veml6031x00: add support for events and trigger

In reply to Javier Carrasco
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] Unbalanced PM runtime usage counter causes an underflow on device unbind or probe error. - [High]…

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] Unbalanced PM runtime usage counter causes an underflow on device unbind or probe error.
- [High] Uninitialized padding in the local scan buffer leaks kernel stack memory to userspace.
- [High] Spurious interrupts cause I2C reads while the device is runtime suspended.
- [High] Initialization order and use-after-destroy race due to early IRQ/trigger registration.
- [High] Triggered buffer is unconditionally registered on the no-IRQ path with a malformed channels array.
- [Medium] Missing timestamp acquisition for nested trigger handler.
- [Medium] Trigger disable sequence violates hardware constraints by clearing AF before AF_TRIG. --

--- Patch [4]: [PATCH v5 4/4] iio: light: veml6031x00: add support for events and trigger --- commit aeccd415dcc79cfa9463cf8b5eec2292ee46dfab
Author: Javier Carrasco <[email protected]>

iio: light: veml6031x00: add support for events and trigger

The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.

Quoted 4 lines
drivers/iio/light/veml6031x00.c +0 -0
index 0cccd40d729eb..e193f87e6740f 100644

[ ... ]

Quoted 1 line
drivers/iio/light/veml6031x00.c +0 -0
@@ -494,13 +686,205 @@ static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev,

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 845

Quoted 10 lines
drivers/iio/light/veml6031x00.c +10 -0
+static irqreturn_t veml6031x00_interrupt(int irq, void *private)
+{
+	struct iio_dev *iio = private;
+	struct veml6031x00_data *data = iio_priv(iio);
+	s64 timestamp;
+	int regval, ret;
+	bool trigger_poll;
+
+	scoped_guard(mutex, &data->irq_lock) {
+		ret = regmap_read(data->regmap, VEML6031X00_REG_INT, &regval);

[Severity: High]
If a spurious interrupt fires while both ev_en and trig_en are false, could this unconditionally read from the I2C bus while the device is runtime suspended?

It looks like this might hit a powered-off device since it doesn't use pm_runtime_get_if_active() or check software state before the register read.

Quoted 5 lines
drivers/iio/light/veml6031x00.c +5 -0
+		if (ret) {
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to read interrupt register %d\n", ret);
+			return IRQ_NONE;
+		}

[ ... ]

Quoted 13 lines
drivers/iio/light/veml6031x00.c +9 -0
@@ -535,11 +919,78 @@ static int veml6031x00_buffer_postdisable(struct iio_dev *iio)
 	return 0;
 }
 
+static int veml6031x00_set_trigger_state(struct iio_trigger *trig, bool state)
+{
+	struct iio_dev *iio = iio_trigger_get_drvdata(trig);
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret;
+
+	guard(mutex)(&data->irq_lock);
+
+	if (state == data->trig_en)

drivers/iio/light/veml6031x00.c static int veml6031x00_buffer_postdisable(struct iio_dev *iio) · line 940

Quoted 10 lines
drivers/iio/light/veml6031x00.c +10 -0
+		return 0;
+
+	ret = veml6031x00_set_interrupt(data, state);
+	if (ret)
+		return ret;
+
+	/* The AF bit must be set before setting AF_TRIG */
+	ret = regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0,
+				 VEML6031X00_CONF0_AF,
+				 FIELD_PREP(VEML6031X00_CONF0_AF, state));

[Severity: Medium]
Since the AF bit must be set before setting AF_TRIG, does the disable sequence need to happen in reverse order?

When state is false, this clears the AF bit first, creating a window where AF_TRIG is still active but AF is disabled.

Quoted 9 lines
drivers/iio/light/veml6031x00.c +9 -0
+	if (ret) {
+		veml6031x00_set_interrupt(data, !state);
+
+		return ret;
+	}
+
+	ret = regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0,
+				 VEML6031X00_CONF0_AF_TRIG,
+				 FIELD_PREP(VEML6031X00_CONF0_AF_TRIG, state));

[ ... ]

drivers/iio/light/veml6031x00.c

Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+static irqreturn_t veml6031x00_trig_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6031x00_data *data = iio_priv(iio);
+	IIO_DECLARE_BUFFER_WITH_TS(__le16, scan, 2);

[Severity: High]
Could this leak uninitialized kernel stack memory to userspace?

The IIO_DECLARE_BUFFER_WITH_TS macro allocates the buffer on the stack without zeroing the padding bytes between the channel data and the timestamp.

Quoted 13 lines
drivers/iio/light/veml6031x00.c +13 -0
+	unsigned int i = 0;
+	int ch, ret;
+
+	if (test_bit(VEML6031X00_SCAN_ALS, iio->active_scan_mask) &&
+	    test_bit(VEML6031X00_SCAN_IR, iio->active_scan_mask)) {
+		ret = regmap_bulk_read(data->regmap,
+				       VEML6031X00_REG_ALS_L,
+				       scan,
+				       2 * sizeof(*scan));
+		if (ret)
+			goto done;
+	} else {
+		iio_for_each_active_channel(iio, ch) {

drivers/iio/light/veml6031x00.c

Quoted 10 lines
drivers/iio/light/veml6031x00.c +10 -0
+			ret = regmap_bulk_read(data->regmap,
+					       iio->channels[ch].address,
+					       &scan[i++],
+					       sizeof(*scan));
+			if (ret)
+				goto done;
+		}
+	}
+
+	iio_push_to_buffers_with_ts(iio, scan, sizeof(scan), pf->timestamp);

[Severity: Medium]
Since this trigger handler is executed synchronously from the interrupt handler via iio_trigger_poll_nested() bypassing the top-half iio_pollfunc_store_time(), will pf->timestamp always be zero here?

Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+
+done:
+	iio_trigger_notify_done(iio->trig);
+
+	return IRQ_HANDLED;
+}

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1149

Quoted 12 lines
drivers/iio/light/veml6031x00.c +4 -3
@@ -636,15 +1142,29 @@ static int veml6031x00_probe(struct i2c_client *i2c)
 				     "Failed to set regmap\n");
 
 	iio->name = data->chip->name;
-	iio->channels = veml6031x00_channels;
-	iio->num_channels = ARRAY_SIZE(veml6031x00_channels);
 	iio->modes = INDIO_DIRECT_MODE;
-	iio->info = &veml6031x00_info;
+	if (i2c->irq) {
+		iio->channels = veml6031x00_channels_irq;
+		iio->num_channels = ARRAY_SIZE(veml6031x00_channels_irq);
+		ret = veml6031x00_setup_irq(i2c, iio);

[Severity: High]
Does registering the IRQ and trigger here expose a race condition?

veml6031x00_setup_irq() requests the threaded IRQ before devm_mutex_init() allocates data->irq_lock and veml6031x00_regfield_init() allocates data->rf.int_en. If an interrupt fires immediately, the handler will try to lock an uninitialized mutex.

Additionally, since devres cleans up in reverse order, won't the mutex be destroyed before the IRQ is unregistered during unbind?

drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1155

Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+		if (ret)
+			return ret;
+
+		iio->info = &veml6031x00_info;
+	} else {
+		iio->channels = veml6031x00_channels;

[Severity: High]
If the device does not provide an interrupt, it falls back to veml6031x00_channels which lacks scan_index and scan_type definitions.

Will setting up a triggered buffer unconditionally at the end of probe cause IIO core undefined behavior if a user attaches an external sysfs/hrtimer trigger, since both channels would default to a scan_index of 0 and 0 storage bits?

Quoted 16 lines
drivers/iio/light/veml6031x00.c +7 -0
+		iio->num_channels = ARRAY_SIZE(veml6031x00_channels);
+		iio->info = &veml6031x00_info_no_irq;
+	}
 
 	ret = devm_mutex_init(&i2c->dev, &data->scale_lock);
 	if (ret)
 		return ret;
 
+	ret = devm_mutex_init(&i2c->dev, &data->irq_lock);
+	if (ret)
+		return ret;
+
 	ret = veml6031x00_regfield_init(data);
 	if (ret)
 		return dev_err_probe(&i2c->dev, ret, "Failed to init regfield\n");

drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1194

Quoted 3 lines
drivers/iio/light/veml6031x00.c +0 -0
@@ -672,6 +1192,12 @@ static int veml6031x00_probe(struct i2c_client *i2c)
 	if (ret)
 		return dev_err_probe(&i2c->dev, ret, "Failed to get runtime PM\n");
 

[Severity: High]
Could the devm_pm_runtime_get_noresume() call just above this hunk cause a usage counter underflow?

The devres action will eventually call pm_runtime_put_noidle() during driver unbind. However, the driver also manually calls pm_runtime_put_autosuspend() at the end of veml6031x00_probe() to drop the initial reference.

This double-put could underflow the usage counter to -1 and corrupt device power management. If the driver manually manages the initial reference, should it use the non-devm pm_runtime_get_noresume() variant?

Quoted 7 lines
drivers/iio/light/veml6031x00.c +6 -0
+	ret = devm_add_action_or_reset(&i2c->dev,
+				       veml6031x00_disable_event_action, data);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret,
+				     "Failed to add event cleanup action\n");
+
 	ret = veml6031x00_validate_part_id(data);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#4 lore raw

Re: [PATCH v5 1/4] dt-bindings: iio: light: veml6030: add veml6031x00 ALS series

In reply to Javier Carrasco
Quoted 6 lines · Javier Carrasco

These ambient light sensors share their properties with the ones from the same manufacturer that are supported by this bindings.

Note that only two datasheets are provided as every one of them covers two devices (veml6031x00/veml60311x00 and veml6031x01/veml60311x01).

Quoted 7 lines
---
 .../devicetree/bindings/iio/light/vishay,veml6030.yaml      | 13 ++++++++++++-
 MAINTAINERS                                                 |  5 +++++
 2 files changed, 17 insertions(+), 1 deletion(-)

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:
./Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml:10:1: [error] syntax error: could not find expected ':' (syntax)

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml: ignoring, error parsing file ./Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml: error loading YAML: while scanning a simple key
in "./Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml", line 8, column 1
could not find expected ':'
in "./Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml", line 10, column 1
./Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml:10:1: could not find expected ':'
make[2]: *** Deleting file 'Documentation/devicetree/bindings/iio/light/vishay,veml6030.example.dts' Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml:10:1: could not find expected ':'
make[2]: *** [Documentation/devicetree/bindings/Makefile:26: Documentation/devicetree/bindings/iio/light/vishay,veml6030.example.dts] Error 1 make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/builds/robherring/dt-review-ci/linux/Makefile:1669: dt_binding_check] Error 2
make: *** [Makefile:248: __sub-make] Error 2

doc reference errors (make refcheckdocs):

See https://patchwork.kernel.org/project/devicetree/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

author maintainer
#16 lore raw

Re: [PATCH v5 3/4] iio: light: veml6031x00: add support for triggered buffers

In reply to [email protected]
Quoted 53 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] AB-BA deadlock between scale_lock and IIO direct mode lock
- [High] Missing IIO buffer channel configuration causes probe failure
- [High] Uninitialized kernel stack data leaked to userspace in IIO triggered buffer
- [High] Unbalanced pm_runtime put leads to negative usage count
--

--- Patch [3]: [PATCH v5 3/4] iio: light: veml6031x00: add support for triggered buffers --- commit a5266734ac0d8d96ec4ee20e5c8d6088863dee05
Author: Javier Carrasco <[email protected]>

iio: light: veml6031x00: add support for triggered buffers

Add triggered buffer functionality for the two channels the device provides (ambient light sensor and IR).

Quoted 4 lines
drivers/iio/light/veml6031x00.c +0 -0
index 30cccda51ca08..0cccd40d729eb 100644

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_single_read(struct iio_dev *iio, enum iio_chan_type type, · line 407

Quoted 8 lines
drivers/iio/light/veml6031x00.c +4 -0
@@ -392,6 +401,10 @@ static int veml6031x00_single_read(struct iio_dev *iio, enum iio_chan_type type,
 
 	guard(mutex)(&data->scale_lock);
 
+	IIO_DEV_ACQUIRE_DIRECT_MODE(iio, claim);
+	if (IIO_DEV_ACQUIRE_FAILED(claim))
+		return -EBUSY;
+

[Severity: High]
Does this establish a lock order of scale_lock followed by the IIO direct mode lock?

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw(struct iio_dev *iio, · line 472

Quoted 8 lines
drivers/iio/light/veml6031x00.c +4 -0
@@ -453,6 +466,10 @@ static int veml6031x00_write_raw(struct iio_dev *iio,
 				 struct iio_chan_spec const *chan,
 				 int val, int val2, long mask)
 {
+	IIO_DEV_ACQUIRE_DIRECT_MODE(iio, claim);
+	if (IIO_DEV_ACQUIRE_FAILED(claim))
+		return -EBUSY;
+

[Severity: High]
Can this cause a deadlock?

In veml6031x00_single_read(), data->scale_lock is acquired before the IIO direct mode lock.

However, here the IIO direct mode lock is acquired first. If this then calls veml6031x00_set_it(), which acquires data->scale_lock:

Quoted 8 lines

veml6031x00_write_raw()

    IIO_DEV_ACQUIRE_DIRECT_MODE(iio, claim)
    veml6031x00_set_it()
        guard(mutex)(&data->scale_lock)

Could concurrent sysfs access to raw data read and integration time write lead to a deadlock?

I will reorder the lock acquisition order, always acquiring direct mode first and then scale_lock.

Quoted 16 lines
Quoted 3 lines
drivers/iio/light/veml6031x00.c +0 -0
 	switch (mask) {
 	case IIO_CHAN_INFO_INT_TIME:
 		return veml6031x00_set_it(iio, val, val2);

[ ... ]

Quoted 1 line
drivers/iio/light/veml6031x00.c +0 -0
@@ -484,6 +501,81 @@ static const struct iio_info veml6031x00_info = {

[ ... ]

drivers/iio/light/veml6031x00.c static const struct iio_info veml6031x00_info = { · line 548

Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+static irqreturn_t veml6031x00_trig_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6031x00_data *data = iio_priv(iio);
+	IIO_DECLARE_BUFFER_WITH_TS(__le16, scan, 2);

[Severity: High]
Does this leak uninitialized stack memory to userspace?

Quoted 9 lines

The scan buffer is declared without zero-initialization.

When reading only a single active channel (e.g., just the ambient light sensor), the driver only writes 2 bytes into the array, leaving the unpopulated channel slots and structure padding uninitialized.

Will iio_push_to_buffers_with_ts() push this uninitialized kernel stack memory to the ring buffer? Should the scan structure be zero-initialized?

I will fix it in V6 with = { } on the declaration.

Quoted 17 lines
Quoted 2 lines
drivers/iio/light/veml6031x00.c +2 -0
+	unsigned int i = 0;
+	int ch, ret;

[ ... ]

Quoted 1 line
drivers/iio/light/veml6031x00.c +0 -0
@@ -588,6 +680,14 @@ static int veml6031x00_probe(struct i2c_client *i2c)

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 690

Quoted 8 lines
drivers/iio/light/veml6031x00.c +8 -0
+	ret = devm_iio_triggered_buffer_setup(regmap_get_device(data->regmap), iio,
+					      iio_pollfunc_store_time,
+					      veml6031x00_trig_handler,
+					      &veml6031x00_buffer_setup_ops);
+	if (ret)
+		return dev_err_probe(&i2c->dev, ret,
+				     "Failed to register triggered buffer\n");
+

[Severity: High]
Will this probe fail with -EINVAL?

Quoted 9 lines

The triggered buffer is registered here, but it appears the driver's channels array (veml6031x00_channels) was not updated with .scan_index, .scan_type, or a timestamp channel.

Without .scan_index, the IIO core defaults both channels to index 0.

Does iio_device_register() abort with -EINVAL during probe when iio_check_unique_scan_index() detects the duplicate scan index 0?

The problem is that the no_irq channel definition is missing the scan attributes. I will add them for V6.

Quoted 5 lines
Quoted 1 line

pm_runtime_put_autosuspend(&i2c->dev);

[Severity: High]
Is this pm_runtime_put_autosuspend() call unbalanced?

Quoted 11 lines

The probe function appears to use devm_pm_runtime_get_noresume() which automatically queues a put on unbind.

If we explicitly decrement the usage counter here, will the devres action firing on unbind drop the counter below zero and trigger a warning?

Should this use the non-devm pm_runtime_get_noresume() instead if the driver intends to manually drop the reference later?

Quoted 2 lines

ret = devm_iio_device_register(&i2c->dev, iio);

I yield. This warning is going to show up a million times during the review, and probably every time a change is applied to this driver. I will use the simple pm_runtime_get_noresume() and add the pm_runtime_put_noidle() in the error paths after it in the probe.

Best regards,
Javier

#10 lore raw
In reply to Javier Carrasco
Quoted 4 lines · Javier Carrasco
drivers/iio/light/veml6031x00.c new file +4 -0
+#include <linux/bitfield.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/device.h>

drivers/iio/light/veml6031x00.c line 21

Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10 -0
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/limits.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>

Please rely on <linux/i2c.h> to provice i2c_device_id and of_device_id and drop <linux/mod_devicetable.h>. The latter header will go away soon.

Best regards
Uwe

author maintainer
#19 lore raw

Re: [PATCH v5 4/4] iio: light: veml6031x00: add support for events and trigger

In reply to [email protected]
Quoted 44 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] Unbalanced PM runtime usage counter causes an underflow on device unbind or probe error.
- [High] Uninitialized padding in the local scan buffer leaks kernel stack memory to userspace.
- [High] Spurious interrupts cause I2C reads while the device is runtime suspended.
- [High] Initialization order and use-after-destroy race due to early IRQ/trigger registration.
- [High] Triggered buffer is unconditionally registered on the no-IRQ path with a malformed channels array.
- [Medium] Missing timestamp acquisition for nested trigger handler.
- [Medium] Trigger disable sequence violates hardware constraints by clearing AF before AF_TRIG. --

--- Patch [4]: [PATCH v5 4/4] iio: light: veml6031x00: add support for events and trigger --- commit aeccd415dcc79cfa9463cf8b5eec2292ee46dfab
Author: Javier Carrasco <[email protected]>

iio: light: veml6031x00: add support for events and trigger

The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.

Quoted 4 lines
drivers/iio/light/veml6031x00.c +0 -0
index 0cccd40d729eb..e193f87e6740f 100644

[ ... ]

Quoted 1 line
drivers/iio/light/veml6031x00.c +0 -0
@@ -494,13 +686,205 @@ static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev,

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 845

Quoted 10 lines
drivers/iio/light/veml6031x00.c +10 -0
+static irqreturn_t veml6031x00_interrupt(int irq, void *private)
+{
+	struct iio_dev *iio = private;
+	struct veml6031x00_data *data = iio_priv(iio);
+	s64 timestamp;
+	int regval, ret;
+	bool trigger_poll;
+
+	scoped_guard(mutex, &data->irq_lock) {
+		ret = regmap_read(data->regmap, VEML6031X00_REG_INT, &regval);

[Severity: High]
If a spurious interrupt fires while both ev_en and trig_en are false, could this unconditionally read from the I2C bus while the device is runtime suspended?

Quoted 4 lines

It looks like this might hit a powered-off device since it doesn't use pm_runtime_get_if_active() or check software state before the register read.

The I2C interface stays accessible even while the device is powered down, so this read doesn't fail or have side effects in case of a spurious interrupt.

Quoted 34 lines
Quoted 5 lines
drivers/iio/light/veml6031x00.c +5 -0
+		if (ret) {
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to read interrupt register %d\n", ret);
+			return IRQ_NONE;
+		}

[ ... ]

Quoted 13 lines
drivers/iio/light/veml6031x00.c +9 -0
@@ -535,11 +919,78 @@ static int veml6031x00_buffer_postdisable(struct iio_dev *iio)
 	return 0;
 }
 
+static int veml6031x00_set_trigger_state(struct iio_trigger *trig, bool state)
+{
+	struct iio_dev *iio = iio_trigger_get_drvdata(trig);
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret;
+
+	guard(mutex)(&data->irq_lock);
+
+	if (state == data->trig_en)

drivers/iio/light/veml6031x00.c static int veml6031x00_buffer_postdisable(struct iio_dev *iio) · line 940

Quoted 10 lines
drivers/iio/light/veml6031x00.c +10 -0
+		return 0;
+
+	ret = veml6031x00_set_interrupt(data, state);
+	if (ret)
+		return ret;
+
+	/* The AF bit must be set before setting AF_TRIG */
+	ret = regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0,
+				 VEML6031X00_CONF0_AF,
+				 FIELD_PREP(VEML6031X00_CONF0_AF, state));

[Severity: Medium]
Since the AF bit must be set before setting AF_TRIG, does the disable sequence need to happen in reverse order?

Quoted 3 lines

When state is false, this clears the AF bit first, creating a window where AF_TRIG is still active but AF is disabled.

Disabling AF is what actually turns off auto-force mode, so it should be done in the current order.

Quoted 20 lines
Quoted 9 lines
drivers/iio/light/veml6031x00.c +9 -0
+	if (ret) {
+		veml6031x00_set_interrupt(data, !state);
+
+		return ret;
+	}
+
+	ret = regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0,
+				 VEML6031X00_CONF0_AF_TRIG,
+				 FIELD_PREP(VEML6031X00_CONF0_AF_TRIG, state));

[ ... ]

drivers/iio/light/veml6031x00.c

Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+static irqreturn_t veml6031x00_trig_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6031x00_data *data = iio_priv(iio);
+	IIO_DECLARE_BUFFER_WITH_TS(__le16, scan, 2);

[Severity: High]
Could this leak uninitialized kernel stack memory to userspace?

Quoted 4 lines

The IIO_DECLARE_BUFFER_WITH_TS macro allocates the buffer on the stack without zeroing the padding bytes between the channel data and the timestamp.

Already addressed in the reply to Sashiko's feedback about [3/4].

Quoted 20 lines
Quoted 20 lines
drivers/iio/light/veml6031x00.c +20 -0
+	unsigned int i = 0;
+	int ch, ret;
+
+	if (test_bit(VEML6031X00_SCAN_ALS, iio->active_scan_mask) &&
+	    test_bit(VEML6031X00_SCAN_IR, iio->active_scan_mask)) {
+		ret = regmap_bulk_read(data->regmap,
+				       VEML6031X00_REG_ALS_L,
+				       scan,
+				       2 * sizeof(*scan));
+		if (ret)
+			goto done;
+	} else {
+		iio_for_each_active_channel(iio, ch) {
+			ret = regmap_bulk_read(data->regmap,
+					       iio->channels[ch].address,
+					       &scan[i++],
+					       sizeof(*scan));
+			if (ret)
+				goto done;
+		}
Quoted 9 lines

drivers/iio/light/veml6031x00.c static int veml6031x00_set_it(struct iio_dev *iio, int val, int val2)

Quoted 3 lines
drivers/iio/light/veml6031x00.c +3 -0
+	}
+
+	iio_push_to_buffers_with_ts(iio, scan, sizeof(scan), pf->timestamp);

[Severity: Medium]
Since this trigger handler is executed synchronously from the interrupt handler via iio_trigger_poll_nested() bypassing the top-half iio_pollfunc_store_time(), will pf->timestamp always be zero here?

The timestamp is already captured in the interrupt handler. I will assign it to pollfunc->timestamp before calling iio_trigger_poll_nested().

Quoted 23 lines
Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+
+done:
+	iio_trigger_notify_done(iio->trig);
+
+	return IRQ_HANDLED;
+}

[ ... ]

drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1149

Quoted 12 lines
drivers/iio/light/veml6031x00.c +4 -3
@@ -636,15 +1142,29 @@ static int veml6031x00_probe(struct i2c_client *i2c)
 				     "Failed to set regmap\n");
 
 	iio->name = data->chip->name;
-	iio->channels = veml6031x00_channels;
-	iio->num_channels = ARRAY_SIZE(veml6031x00_channels);
 	iio->modes = INDIO_DIRECT_MODE;
-	iio->info = &veml6031x00_info;
+	if (i2c->irq) {
+		iio->channels = veml6031x00_channels_irq;
+		iio->num_channels = ARRAY_SIZE(veml6031x00_channels_irq);
+		ret = veml6031x00_setup_irq(i2c, iio);

[Severity: High]
Does registering the IRQ and trigger here expose a race condition?

Quoted 8 lines

veml6031x00_setup_irq() requests the threaded IRQ before devm_mutex_init() allocates data->irq_lock and veml6031x00_regfield_init() allocates data->rf.int_en. If an interrupt fires immediately, the handler will try to lock an uninitialized mutex.

Additionally, since devres cleans up in reverse order, won't the mutex be destroyed before the IRQ is unregistered during unbind?

For V6: mutex_init and regfield_init moved before setup_irq().

Quoted 11 lines

drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1155

Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+		if (ret)
+			return ret;
+
+		iio->info = &veml6031x00_info;
+	} else {
+		iio->channels = veml6031x00_channels;

[Severity: High]
If the device does not provide an interrupt, it falls back to veml6031x00_channels which lacks scan_index and scan_type definitions.

Quoted 5 lines

Will setting up a triggered buffer unconditionally at the end of probe cause IIO core undefined behavior if a user attaches an external sysfs/hrtimer trigger, since both channels would default to a scan_index of 0 and 0 storage bits?

Already addressed in the reply to Sashiko's feedback about [3/4].

Quoted 24 lines
Quoted 16 lines
drivers/iio/light/veml6031x00.c +7 -0
+		iio->num_channels = ARRAY_SIZE(veml6031x00_channels);
+		iio->info = &veml6031x00_info_no_irq;
+	}
 
 	ret = devm_mutex_init(&i2c->dev, &data->scale_lock);
 	if (ret)
 		return ret;
 
+	ret = devm_mutex_init(&i2c->dev, &data->irq_lock);
+	if (ret)
+		return ret;
+
 	ret = veml6031x00_regfield_init(data);
 	if (ret)
 		return dev_err_probe(&i2c->dev, ret, "Failed to init regfield\n");

drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1194

Quoted 3 lines
drivers/iio/light/veml6031x00.c +0 -0
@@ -672,6 +1192,12 @@ static int veml6031x00_probe(struct i2c_client *i2c)
 	if (ret)
 		return dev_err_probe(&i2c->dev, ret, "Failed to get runtime PM\n");
 

[Severity: High]
Could the devm_pm_runtime_get_noresume() call just above this hunk cause a usage counter underflow?

Quoted 9 lines

The devres action will eventually call pm_runtime_put_noidle() during driver unbind. However, the driver also manually calls pm_runtime_put_autosuspend() at the end of veml6031x00_probe() to drop the initial reference.

This double-put could underflow the usage counter to -1 and corrupt device power management. If the driver manually manages the initial reference, should it use the non-devm pm_runtime_get_noresume() variant?

Already addressed in the reply to Sashiko's feedback about [3/4].

Best regards,
Javier

#5 lore raw

Re: [PATCH v5 1/4] dt-bindings: iio: light: veml6030: add veml6031x00 ALS series

In reply to Javier Carrasco
Quoted 18 lines · Javier Carrasco

These ambient light sensors share their properties with the ones from the same manufacturer that are supported by this bindings.

Note that only two datasheets are provided as every one of them covers two devices (veml6031x00/veml60311x00 and veml6031x01/veml60311x01).

---
 .../devicetree/bindings/iio/light/vishay,veml6030.yaml      | 13 ++++++++++++-
 MAINTAINERS                                                 |  5 +++++
 2 files changed, 17 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +0 -0
index 0041e1db6838..6a387c76df35 100644

Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml line 8

Quoted 6 lines
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +2 -1
@@ -4,7 +4,8 @@
 $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: VEML3235, VEML3328, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
+title: VEML3235, VEML3328, VEML6030, VEML6031x00 series, VEML6035
+and VEML7700 Ambient Light Sensors (ALS)

This was correct, now is broken. And was not even tested before sending...

Best regards,
Krzysztof

author maintainer
#6 lore raw

Re: [PATCH v5 1/4] dt-bindings: iio: light: veml6030: add veml6031x00 ALS series

Quoted 22 lines · Krzysztof Kozlowski
Quoted 21 lines · Javier Carrasco

These ambient light sensors share their properties with the ones from the same manufacturer that are supported by this bindings.

Note that only two datasheets are provided as every one of them covers two devices (veml6031x00/veml60311x00 and veml6031x01/veml60311x01).

---
 .../devicetree/bindings/iio/light/vishay,veml6030.yaml      | 13 ++++++++++++-
 MAINTAINERS                                                 |  5 +++++
 2 files changed, 17 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +0 -0
index 0041e1db6838..6a387c76df35 100644
@@ -4,7 +4,8 @@
 $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
Quoted 8 lines

Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml line 8

Quoted 3 lines
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +2 -1
-title: VEML3235, VEML3328, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
+title: VEML3235, VEML3328, VEML6030, VEML6031x00 series, VEML6035
+and VEML7700 Ambient Light Sensors (ALS)

This was correct, now is broken. And was not even tested before sending...

Best regards,
Krzysztof

Yes, it is. I replied to the Sashiko email before this one arrived that a new device was added to the binding between versions and I just extended the title without further thinking. My bad, I have added the missing white spaces at the beginning of the second line for V6, validated the result, and included dt_binding_check in my CI pipeline to keep this from happening again.

Thanks and best regards,
Javier

#11 lore raw
In reply to Javier Carrasco
Quoted 8 lines · Javier Carrasco

These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.

This first implementation provides basic functionality (measurement configuration, raw reads and ID validation) and defines the different register regions in preparation for extended features in the subsequent patches of the series.

Since it's going to be a new version, my comments below.

...

+ array_size.h // ARRAY_SIZE()

Quoted 1 line

+#include <linux/bitfield.h>

Is this in use?

+ bits.h // BIT()

drivers/iio/light/veml6031x00.c line 11

Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+#include <linux/cleanup.h>
+#include <linux/delay.h>
 
+#include <linux/device.h>

Oh, the whole headers hell is loaded just due to dev_get_drvdata() it seems...

drivers/iio/light/veml6031x00.c line 14

Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/limits.h>

I missed probably it, but is it used?

drivers/iio/light/veml6031x00.c line 18

Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +4 -0
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
 
+#include <linux/pm.h>

Not really required as pm_runtime.h takes care of.

drivers/iio/light/veml6031x00.c line 21

Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>

+ types.h // bool, __le16, et cetera.

+ asm/byteorder.h // le16_to_cpu() et alia.

...

drivers/iio/light/veml6031x00.c line 130

Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +9 -0
+static void veml6031x00_als_shutdown_action(void *data)
+{
+	struct veml6031x00_data *veml = data;
+	int ret;
+
+	ret = veml6031x00_set_power_state(veml, false);
+	if (ret)
 
+		dev_err(regmap_get_device(veml->regmap),
+			"Failed to shut down device: %d\n", ret);

It's only a warning, as there is neither error returning, nor a fallback.

Quoted 1 line

+}

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int veml6031x00_regfield_init(struct veml6031x00_data *data)
+{
	struct device *dev = regmap_get_device(...);

drivers/iio/light/veml6031x00.c line 208

Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +5 -0
+	struct regmap_field *rm_field;
+	struct veml6031x00_rf *rf = &data->rf;
+
+	rm_field = devm_regmap_field_alloc(regmap_get_device(data->regmap),
+					   data->regmap, veml6031x00_rf_gain);
	rm_field = devm_regmap_field_alloc(dev, data->regmap, veml6031x00_rf_gain);

(yes, slightly longer than 80, but I think it's okay, even in the last case
below).

Quoted 18 lines
drivers/iio/light/veml6031x00.c new file +18 -0
+	if (IS_ERR(rm_field))
+		return PTR_ERR(rm_field);
+	rf->gain = rm_field;
+
+	rm_field = devm_regmap_field_alloc(regmap_get_device(data->regmap),
+					   data->regmap, veml6031x00_rf_it);
+	if (IS_ERR(rm_field))
+		return PTR_ERR(rm_field);
+	rf->it = rm_field;
+
+	rm_field = devm_regmap_field_alloc(regmap_get_device(data->regmap),
+					   data->regmap, veml6031x00_rf_pd_div4);
+	if (IS_ERR(rm_field))
+		return PTR_ERR(rm_field);
+	rf->pd_div4 = rm_field;
+
+	return 0;
+}

...

drivers/iio/light/veml6031x00.c line 248

Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+static int veml6031x00_get_it(struct veml6031x00_data *data, int *val2)
+{
+	scoped_guard(mutex, &data->scale_lock)

guard()() will suffice in this case. Yes, will need a blank line, but overall it's a better choice I think.

Quoted 2 lines
(quoted diff) +2 -0
+		return __veml6031x00_get_it(data, val2);
+}

...

drivers/iio/light/veml6031x00.c line 268

Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +6 -0
+static int veml6031x00_set_it(struct iio_dev *iio, int val, int val2)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret, gain_sel, new_gain, prev_gain, prev_it;
+	unsigned int gain_reg, it_idx, pd_div4;
 
+	bool in_range;

Can we name it differently? When grepping over the whole tree this will give a lot of the common in_range() calls...

Quoted 51 lines
drivers/iio/light/veml6031x00.c new file +51 -0
+
+	if (val || !iio_gts_valid_time(&data->gts, val2))
+		return -EINVAL;
+
+	guard(mutex)(&data->scale_lock);
+
+	ret = regmap_field_read(data->rf.it, &it_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(data->rf.gain, &gain_reg);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(data->rf.pd_div4, &pd_div4);
+	if (ret)
+		return ret;
+
+	prev_it = iio_gts_find_int_time_by_sel(&data->gts, it_idx);
+	if (prev_it < 0)
+		return prev_it;
+
+	if (prev_it == val2)
+		return 0;
+
+	prev_gain = iio_gts_find_gain_by_sel(&data->gts, (pd_div4 << 2) | gain_reg);
+	if (prev_gain < 0)
+		return prev_gain;
+
+	ret = iio_gts_find_new_gain_by_gain_time_min(&data->gts, prev_gain, prev_it,
+						     val2, &new_gain, &in_range);
+	if (ret)
+		return ret;
+
+	if (!in_range)
+		dev_dbg(regmap_get_device(data->regmap), "Optimal gain out of range\n");
+
+	ret = iio_gts_find_sel_by_int_time(&data->gts, val2);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_field_write(data->rf.it, ret);
+	if (ret)
+		return ret;
+
+	gain_sel = iio_gts_find_sel_by_gain(&data->gts, new_gain);
+	if (gain_sel < 0)
+		return gain_sel;
+
+	return veml6031x00_write_gain(data, gain_sel);
+}

...

Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +5 -0
+static int veml6031x00_get_scale(struct veml6031x00_data *data, int *val,
+				 int *val2)
+{
+	int gain, it, gain_reg, pd_div4, it_reg, ret, sel;
+

drivers/iio/light/veml6031x00.c line 354

Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +9 -0
+	scoped_guard(mutex, &data->scale_lock) {
+		ret = regmap_field_read(data->rf.gain, &gain_reg);
+		if (ret)
+			return ret;
+
+		ret = regmap_field_read(data->rf.pd_div4, &pd_div4);
+		if (ret)
+			return ret;
 
+		sel = (pd_div4 << 2) | gain_reg;

This magic happens a few times, perhaps some macros to define?

#define DIV4_GAIN_TO_SEL(div4, gain)
#define DIV4_FROM_SEL(sel)
#define GAIN_FROM_SEL(sel)

?

Quoted 56 lines
drivers/iio/light/veml6031x00.c new file +55 -0
+		gain = iio_gts_find_gain_by_sel(&data->gts, sel);
+		if (gain < 0)
+			return gain;
+
+		ret = regmap_field_read(data->rf.it, &it_reg);
+		if (ret)
+			return ret;
+	}
+
+	it = iio_gts_find_int_time_by_sel(&data->gts, it_reg);
+	if (it < 0)
+		return it;
+
+	ret = iio_gts_get_scale(&data->gts, gain, it, val, val2);
+	if (ret)
+		return ret;
+
+	return IIO_VAL_INT_PLUS_NANO;
+}
 
+	int addr, it_usec, ret;
+	__le16 regval;
+
+	switch (type) {
+	case IIO_LIGHT:
+		addr = VEML6031X00_REG_ALS_L;
+		break;
+	case IIO_INTENSITY:
+		addr = VEML6031X00_REG_IR_L;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	guard(mutex)(&data->scale_lock);
+
+	PM_RUNTIME_ACQUIRE_AUTOSUSPEND(regmap_get_device(data->regmap), pm);
+	ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+	if (ret)
+		return ret;
+
+	ret = __veml6031x00_get_it(data, &it_usec);
+	if (ret < 0)
+		return ret;
+
+	/* integration time + 10% to ensure completion */
+	fsleep(it_usec + (it_usec / 10));
+
+	ret = regmap_bulk_read(data->regmap, addr, &regval, sizeof(regval));
+	if (ret)
+		return ret;
+
+	*val = le16_to_cpu(regval);
+
+	return IIO_VAL_INT;
+}

...

drivers/iio/light/veml6031x00.c line 418

Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+static int veml6031x00_read_raw(struct iio_dev *iio,
+				struct iio_chan_spec const *chan, int *val,
+				int *val2, long mask)

Split logically

static int veml6031x00_read_raw(struct iio_dev *iio,
struct iio_chan_spec const *chan, int *val, int *val2, long mask)

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int veml6031x00_validate_part_id(struct veml6031x00_data *data)
+{
	struct device *dev = ...;
Quoted 15 lines
drivers/iio/light/veml6031x00.c new file +15 -0
+	int part_id, ret;
+	__le16 regval;
+
+	ret = regmap_bulk_read(data->regmap, VEML6031X00_REG_ID_L, &regval,
+			       sizeof(regval));
+	if (ret)
+		return dev_err_probe(regmap_get_device(data->regmap), ret,
+				     "Failed to read ID\n");
+
+	part_id = le16_to_cpu(regval);
+	if (part_id != data->chip->part_id)
+		dev_info(regmap_get_device(data->regmap), "Unknown ID %04x\n", part_id);
+
+	return 0;
+}

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int veml6031x00_hw_init(struct iio_dev *iio)
+{

As per above, and check the rest of the code for the same opportunity.

Quoted 16 lines
drivers/iio/light/veml6031x00.c new file +16 -0
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret;
+
+	/* Max resolution = 6.9632 lx/cnt for gain = 0.125 and IT = 3.125ms */
+	ret = devm_iio_init_iio_gts(regmap_get_device(data->regmap), 6, 963200000,
+				    veml6031x00_gain_sel,
+				    ARRAY_SIZE(veml6031x00_gain_sel),
+				    veml6031x00_it_sel,
+				    ARRAY_SIZE(veml6031x00_it_sel),
+				    &data->gts);
+	if (ret)
+		return dev_err_probe(regmap_get_device(data->regmap), ret,
+				     "failed to init IIO GTS\n");
+
+	return 0;
+}

...

Quoted 1 line

+static int veml6031x00_probe(struct i2c_client *i2c)

Ditto.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#20 lore raw

Re: [PATCH v5 4/4] iio: light: veml6031x00: add support for events and trigger

In reply to Javier Carrasco
Quoted 4 lines · Javier Carrasco

The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.

...

drivers/iio/light/veml6031x00.c line 57

Quoted 3 lines
drivers/iio/light/veml6031x00.c +3 -0
+#define VEML6031X00_INT_MASK        (VEML6031X00_INT_TH_L | \
+				     VEML6031X00_INT_TH_H | \
+				     VEML6031X00_INT_DRDY)

Make it better style with

#define VEML6031X00_INT_MASK						     \
	(VEML6031X00_INT_TH_L | VEML6031X00_INT_TH_H | VEML6031X00_INT_DRDY)

(looking further in the code I kinda have a déjà vu that I said that already in
the past).

...

drivers/iio/light/veml6031x00.c struct veml6031x00_data { · line 92

Quoted 3 lines
drivers/iio/light/veml6031x00.c +3 -0
+	/*
+	 * Serialize access to irq enable/disable by events and trigger
+	 * (shared line)

Missing period.

Quoted 1 line
(quoted diff) +1 -0
+	 */

...

drivers/iio/light/veml6031x00.c static int veml6031x00_set_it(struct iio_dev *iio, int val, int val2) · line 455

Quoted 4 lines
drivers/iio/light/veml6031x00.c +4 -0
+static int veml6031x00_read_period(struct iio_dev *iio, int *val)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret, regval;

Why is regval signed?

drivers/iio/light/veml6031x00.c static int veml6031x00_set_it(struct iio_dev *iio, int val, int val2) · line 461

Quoted 5 lines
drivers/iio/light/veml6031x00.c +5 -0
+	ret = regmap_field_read(data->rf.pers, &regval);
+	if (ret)
+		return ret;
+
+	*val = 1 << regval;

BIT() ?

Quoted 2 lines
(quoted diff) +2 -0
+	return IIO_VAL_INT;
+}

...

drivers/iio/light/veml6031x00.c static int veml6031x00_get_scale(struct veml6031x00_data *data, int *val, · line 553

Quoted 4 lines
drivers/iio/light/veml6031x00.c +4 -0
+static int veml6031x00_write_th(struct iio_dev *iio, int val, int val2, int dir)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	__le16 regval = cpu_to_le16(val);

There is no technical need to assign it here, especially if the below validation won't pass, but it doesn't have any side effects, so I guess it's fine.

Quoted 21 lines
drivers/iio/light/veml6031x00.c +21 -0
+	int ret;
+
+	if (val < 0 || val > U16_MAX || val2)
+		return -EINVAL;
+
+	if (dir == IIO_EV_DIR_RISING) {
+		ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WH_L,
+					&regval, sizeof(regval));
+		if (ret)
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to set high threshold %d\n", ret);
+	} else {
+		ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WL_L,
+					&regval, sizeof(regval));
+		if (ret)
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to set low threshold %d\n", ret);
+	}
+
+	return ret;
+}

...

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 690

Quoted 2 lines
drivers/iio/light/veml6031x00.c +2 -0
+static int veml6031x00_set_interrupt(struct veml6031x00_data *data, bool state)
+	__must_hold(&data->irq_lock)

The sparse annotations is fine, but lockdep one is even better.

Quoted 23 lines
drivers/iio/light/veml6031x00.c +23 -0
+{
+	int ret;
+
+	if (state) {
+		data->int_users++;
+		if (data->int_users > 1)
+			return 0;
+	} else {
+		data->int_users--;
+		if (data->int_users > 0)
+			return 0;
+	}
+
+	ret = regmap_field_write(data->rf.int_en, state);
+	if (ret) {
+		if (state)
+			data->int_users--;
+		else
+			data->int_users++;
+	}
+
+	return ret;
+}

...

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 841

Quoted 6 lines
drivers/iio/light/veml6031x00.c +6 -0
+static irqreturn_t veml6031x00_interrupt(int irq, void *private)
+{
+	struct iio_dev *iio = private;
+	struct veml6031x00_data *data = iio_priv(iio);
+	s64 timestamp;
+	int regval, ret;

Why is regval signed?

Quoted 45 lines
drivers/iio/light/veml6031x00.c +45 -0
+	bool trigger_poll;
+
+	scoped_guard(mutex, &data->irq_lock) {
+		ret = regmap_read(data->regmap, VEML6031X00_REG_INT, &regval);
+		if (ret) {
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to read interrupt register %d\n", ret);
+			return IRQ_NONE;
+		}
+
+		if (!(regval & VEML6031X00_INT_MASK))
+			return IRQ_NONE;
+
+		if ((regval & (VEML6031X00_INT_TH_H | VEML6031X00_INT_TH_L)) &&
+		    data->ev_en) {
+			timestamp = iio_get_time_ns(iio);
+
+			if (regval & VEML6031X00_INT_TH_H)
+				iio_push_event(iio,
+					       IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
+								    IIO_EV_TYPE_THRESH,
+								    IIO_EV_DIR_RISING),
+					       timestamp);
+			if (regval & VEML6031X00_INT_TH_L)
+				iio_push_event(iio,
+					       IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
+								    IIO_EV_TYPE_THRESH,
+								    IIO_EV_DIR_FALLING),
+					       timestamp);
+		}
+
+		trigger_poll = (regval & VEML6031X00_INT_DRDY) && data->trig_en;
+	}
+
+	/*
+	 * iio_trigger_poll_nested() must be called with irq_lock released:
+	 * iio_trigger_poll_nested() runs trig_handler() synchronously in this
+	 * thread, which calls reenable() on completion, and that callback also
+	 * takes irq_lock.
+	 */
+	if (trigger_poll)
+		iio_trigger_poll_nested(data->trig);
+
+	return IRQ_HANDLED;
+}

...

Quoted 18 lines
drivers/iio/light/veml6031x00.c +18 -0
+static int veml6031x00_setup_irq(struct i2c_client *i2c, struct iio_dev *iio)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	int ret;
+
+	data->trig = devm_iio_trigger_alloc(regmap_get_device(data->regmap),
+					    "%s-drdy%d", iio->name, iio_device_id(iio));
+	if (!data->trig)
+		return -ENOMEM;
+
+	data->trig->ops = &veml6031x00_trigger_ops;
+	iio_trigger_set_drvdata(data->trig, iio);
+
+	ret = devm_iio_trigger_register(regmap_get_device(data->regmap), data->trig);
+	if (ret)
+		return ret;
+
+	iio->trig = iio_trigger_get(data->trig);

drivers/iio/light/veml6031x00.c static int veml6031x00_hw_init(struct iio_dev *iio)

Quoted 10 lines
drivers/iio/light/veml6031x00.c +9 -0
+	ret = devm_request_threaded_irq(regmap_get_device(data->regmap),
+					i2c->irq, NULL,
+					veml6031x00_interrupt,
+					IRQF_ONESHOT,
+					iio->name, iio);
+	if (ret)
 
+		return dev_err_probe(regmap_get_device(data->regmap), ret,
+				     "Failed to request irq %d\n",
+				     i2c->irq);

This is a dup message. Remove it.

Review

Quoted 2 lines
(quoted diff) +1 -0
+
 	return 0;
	return devm_request_threaded_irq(...);
Quoted 1 line

}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#12 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Javier Carrasco

These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.

Quoted 7 lines
Quoted 4 lines

This first implementation provides basic functionality (measurement configuration, raw reads and ID validation) and defines the different register regions in preparation for extended features in the subsequent patches of the series.

Since it's going to be a new version, my comments below.

Hi Andy, thank you for your review. I agree with most of your comments and I will apply your suggestions for V6.

Quoted 8 lines

...

+ array_size.h // ARRAY_SIZE()

Quoted 1 line

+#include <linux/bitfield.h>

Is this in use?

This one is in use for FIELD_PREP()

Quoted 9 lines

+ bits.h // BIT()

drivers/iio/light/veml6031x00.c line 11

Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+#include <linux/cleanup.h>
+#include <linux/delay.h>
 
+#include <linux/device.h>

Oh, the whole headers hell is loaded just due to dev_get_drvdata() it seems...

I will rely on <linux/i2c.h> instead, which is also what I am going to do to get rid of <linux/mod_devicetable.h> as Uwe suggested.

Quoted 6 lines

drivers/iio/light/veml6031x00.c line 14

Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/limits.h>

I missed probably it, but is it used?

This one is used for U16_MAX.

Quoted 6 lines

drivers/iio/light/veml6031x00.c line 18

Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +4 -0
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
 
+#include <linux/pm.h>

The rest of your comments need no reply from my side.

Best regards,
Javier

author maintainer
#21 lore raw

Re: [PATCH v5 4/4] iio: light: veml6031x00: add support for events and trigger

In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 4 lines · Javier Carrasco

The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.

...

...

Quoted 9 lines

drivers/iio/light/veml6031x00.c static int veml6031x00_get_scale(struct veml6031x00_data *data, int *val, · line 553

Quoted 4 lines
drivers/iio/light/veml6031x00.c +4 -0
+static int veml6031x00_write_th(struct iio_dev *iio, int val, int val2, int dir)
+{
+	struct veml6031x00_data *data = iio_priv(iio);
+	__le16 regval = cpu_to_le16(val);

There is no technical need to assign it here, especially if the below validation won't pass, but it doesn't have any side effects, so I guess it's fine.

I will assign it after the val check instead.

Quoted 24 lines
Quoted 21 lines
drivers/iio/light/veml6031x00.c +21 -0
+	int ret;
+
+	if (val < 0 || val > U16_MAX || val2)
+		return -EINVAL;
+
+	if (dir == IIO_EV_DIR_RISING) {
+		ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WH_L,
+					&regval, sizeof(regval));
+		if (ret)
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to set high threshold %d\n", ret);
+	} else {
+		ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WL_L,
+					&regval, sizeof(regval));
+		if (ret)
+			dev_dbg(regmap_get_device(data->regmap),
+				"Failed to set low threshold %d\n", ret);
+	}
+
+	return ret;
+}

...

Quoted 5 lines

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 690

Quoted 2 lines
drivers/iio/light/veml6031x00.c +2 -0
+static int veml6031x00_set_interrupt(struct veml6031x00_data *data, bool state)
+	__must_hold(&data->irq_lock)

The sparse annotations is fine, but lockdep one is even better.

I have seen a few instances in a number of subsystems where both are used at the same time. What is the preferred alternative here?

Quoted 15 lines
Quoted 15 lines
drivers/iio/light/veml6031x00.c +15 -0
+{
+	int ret;
+
+	if (state) {
+		data->int_users++;
+		if (data->int_users > 1)
+			return 0;
+	} else {
+		data->int_users--;
+		if (data->int_users > 0)
+			return 0;
+	}
+
+	ret = regmap_field_write(data->rf.int_en, state);
+	if (ret) {
Quoted 9 lines

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 713

Quoted 8 lines
drivers/iio/light/veml6031x00.c +8 -0
+		if (state)
+			data->int_users--;
+		else
+			data->int_users++;
+	}
+
+	return ret;
+}

Best regards,
Javier

#13 lore raw
In reply to Javier Carrasco
Quoted 2 lines · Javier Carrasco
Quoted 1 line · Andy Shevchenko

On Fri, Aug 07, 2026 at 03:51:53PM +0200, Javier Carrasco wrote:

...

Quoted 7 lines
Quoted 5 lines

+ array_size.h // ARRAY_SIZE()

Quoted 1 line

+#include <linux/bitfield.h>

Is this in use?

This one is in use for FIELD_PREP()

Ah, I missed that.

Quoted 11 lines
Quoted 8 lines

+ bits.h // BIT()

drivers/iio/light/veml6031x00.c line 11

Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+#include <linux/cleanup.h>
+#include <linux/delay.h>
 
+#include <linux/device.h>

Oh, the whole headers hell is loaded just due to dev_get_drvdata() it seems...

I will rely on <linux/i2c.h> instead, which is also what I am going to do to get rid of <linux/mod_devicetable.h> as Uwe suggested.

Sure, but device.h is still required as you use it.

Quoted 7 lines
Quoted 5 lines

drivers/iio/light/veml6031x00.c line 14

Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3 -0
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/limits.h>

I missed probably it, but is it used?

This one is used for U16_MAX.

Good!

Quoted 7 lines
Quoted 5 lines

drivers/iio/light/veml6031x00.c line 18

Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +4 -0
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
 
+#include <linux/pm.h>

The rest of your comments need no reply from my side.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#22 lore raw

Re: [PATCH v5 4/4] iio: light: veml6031x00: add support for events and trigger

In reply to Javier Carrasco
Quoted 2 lines · Javier Carrasco
Quoted 1 line · Andy Shevchenko

On Fri, Aug 07, 2026 at 03:51:55PM +0200, Javier Carrasco wrote:

...

Quoted 7 lines
Quoted 4 lines

drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 690

Quoted 2 lines
drivers/iio/light/veml6031x00.c +2 -0
+static int veml6031x00_set_interrupt(struct veml6031x00_data *data, bool state)
+	__must_hold(&data->irq_lock)

The sparse annotations is fine, but lockdep one is even better.

I have seen a few instances in a number of subsystems where both are used at the same time. What is the preferred alternative here?

Both. lockdep is a runtime check, sparse one is a build-time.

Signature · 2 lines
With Best Regards,
Andy Shevchenko