Devicetree
 help / color / mirror / Atom feed
* [PATCH 0/2] iio: light: add support for ams OSRAM TCS3430
@ 2026-11-24 22:46 Robert Ijtsma via B4 Relay
  2026-11-24 22:46 ` [PATCH 1/2] dt-bindings: iio: light: add " Robert Ijtsma via B4 Relay
  2026-11-24 22:46 ` [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver Robert Ijtsma via B4 Relay
  0 siblings, 2 replies; 4+ messages in thread
From: Robert Ijtsma via B4 Relay @ 2026-11-24 22:46 UTC (permalink / raw)
  To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley
  Cc: linux-iio, devicetree, linux-kernel, Robert Ijtsma

Add Devicetree binding documentation and an IIO driver for the ams OSRAM
TCS3430 ambient light and CIE 1931 XYZ tristimulus color sensor.

Tested on Microsoft Surface Pro X SQ2 (SC8180X), where the sensor is
connected on I2C at 0x39 and used for ambient light sensing.

Signed-off-by: Robert Ijtsma <robertijtsma@gmail.com>
---
Robert Ijtsma (2):
      dt-bindings: iio: light: add ams OSRAM TCS3430
      iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver

 .../devicetree/bindings/iio/light/ams,tcs3430.yaml |  52 ++++
 MAINTAINERS                                        |   7 +
 drivers/iio/light/Kconfig                          |  10 +
 drivers/iio/light/Makefile                         |   1 +
 drivers/iio/light/tcs3430.c                        | 345 +++++++++++++++++++++
 5 files changed, 415 insertions(+)
---
base-commit: 4843f6873f7c4835e62f2c73ba0fd538e57a2f22
change-id: 20261124-iio-tcs3430-2f53879527ed

Best regards,
--  
Robert Ijtsma <robertijtsma@gmail.com>



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

* [PATCH 1/2] dt-bindings: iio: light: add ams OSRAM TCS3430
  2026-11-24 22:46 [PATCH 0/2] iio: light: add support for ams OSRAM TCS3430 Robert Ijtsma via B4 Relay
@ 2026-11-24 22:46 ` Robert Ijtsma via B4 Relay
  2026-11-24 22:46 ` [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver Robert Ijtsma via B4 Relay
  1 sibling, 0 replies; 4+ messages in thread
From: Robert Ijtsma via B4 Relay @ 2026-11-24 22:46 UTC (permalink / raw)
  To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley
  Cc: linux-iio, devicetree, linux-kernel, Robert Ijtsma

From: Robert Ijtsma <robertijtsma@gmail.com>

Add Devicetree binding schema for the ams OSRAM TCS3430 ambient light
and CIE 1931 XYZ tristimulus color sensor.

Signed-off-by: Robert Ijtsma <robertijtsma@gmail.com>
---
 .../devicetree/bindings/iio/light/ams,tcs3430.yaml | 52 ++++++++++++++++++++++
 MAINTAINERS                                        |  6 +++
 2 files changed, 58 insertions(+)

diff --git a/Documentation/devicetree/bindings/iio/light/ams,tcs3430.yaml b/Documentation/devicetree/bindings/iio/light/ams,tcs3430.yaml
new file mode 100644
index 0000000..085a70e
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/ams,tcs3430.yaml
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/light/ams,tcs3430.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: ams OSRAM TCS3430 XYZ Tristimulus Color and Ambient Light Sensor
+
+maintainers:
+  - Robert Ijtsma <robertijtsma@gmail.com>
+
+description: |
+  The ams OSRAM TCS3430 is an I2C ambient light and CIE 1931 XYZ tristimulus
+  color sensor with photopic Y, Z, X, and IR channels.
+
+  Datasheet: https://look.ams-osram.com/m/43d518b4cd81e3f2/original/TCS3430-DS000539.pdf
+
+properties:
+  compatible:
+    const: ams,tcs3430
+
+  reg:
+    maxItems: 1
+
+  vdd-supply: true
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - vdd-supply
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        light-sensor@39 {
+            compatible = "ams,tcs3430";
+            reg = <0x39>;
+            vdd-supply = <&vdd_reg>;
+            interrupts = <24 IRQ_TYPE_LEVEL_LOW>;
+        };
+    };
+...
diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1..b1cebc2 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1489,6 +1489,12 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
 F:	drivers/iio/light/as73211.c
 
+AMS TCS3430 DRIVER
+M:	Robert Ijtsma <robertijtsma@gmail.com>
+L:	linux-iio@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/light/ams,tcs3430.yaml
+
 AMT (Automatic Multicast Tunneling)
 M:	Taehee Yoo <ap420073@gmail.com>
 L:	netdev@vger.kernel.org

-- 
2.56.0



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

* [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver
  2026-11-24 22:46 [PATCH 0/2] iio: light: add support for ams OSRAM TCS3430 Robert Ijtsma via B4 Relay
  2026-11-24 22:46 ` [PATCH 1/2] dt-bindings: iio: light: add " Robert Ijtsma via B4 Relay
@ 2026-11-24 22:46 ` Robert Ijtsma via B4 Relay
  2026-11-24 22:56   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Robert Ijtsma via B4 Relay @ 2026-11-24 22:46 UTC (permalink / raw)
  To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley
  Cc: linux-iio, devicetree, linux-kernel, Robert Ijtsma

From: Robert Ijtsma <robertijtsma@gmail.com>

Add an IIO driver for the ams OSRAM TCS3430 ambient light and CIE 1931
XYZ tristimulus color sensor (found at I2C address 0x39 on devices such
as the Microsoft Surface Pro X).

The driver exposes:
- Illuminance (photopic Y channel) with raw counts and lux scale
- Modified intensity channels for X, Y, Z, and IR1
- Selectable hardware gain (1x, 4x, 16x, 64x, 128x) and integration time
- Regulator management and system sleep suspend/resume support

Tested on Microsoft Surface Pro X SQ2 (SC8180X).

Signed-off-by: Robert Ijtsma <robertijtsma@gmail.com>
---
 MAINTAINERS                 |   1 +
 drivers/iio/light/Kconfig   |  10 ++
 drivers/iio/light/Makefile  |   1 +
 drivers/iio/light/tcs3430.c | 345 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 357 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index b1cebc2..5194709 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1494,6 +1494,7 @@ M:	Robert Ijtsma <robertijtsma@gmail.com>
 L:	linux-iio@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/light/ams,tcs3430.yaml
+F:	drivers/iio/light/tcs3430.c
 
 AMT (Automatic Multicast Tunneling)
 M:	Taehee Yoo <ap420073@gmail.com>
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 462fff1..09894cd 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -609,6 +609,16 @@ config TCS3414
 	  This driver can also be built as a module.  If so, the module
 	  will be called tcs3414.
 
+config TCS3430
+	tristate "ams OSRAM TCS3430 ambient light and XYZ color sensor"
+	depends on I2C
+	help
+	  If you say yes here you get support for the ams OSRAM TCS3430
+	  ambient light and CIE 1931 XYZ tristimulus color sensor.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called tcs3430.
+
 config TCS3472
 	tristate "TAOS TCS3472 color light-to-digital converter"
 	depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 6672ed0..cd7b42e 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -56,6 +56,7 @@ obj-$(CONFIG_ST_UVIS25)		+= st_uvis25_core.o
 obj-$(CONFIG_ST_UVIS25_I2C)	+= st_uvis25_i2c.o
 obj-$(CONFIG_ST_UVIS25_SPI)	+= st_uvis25_spi.o
 obj-$(CONFIG_TCS3414)		+= tcs3414.o
+obj-$(CONFIG_TCS3430)		+= tcs3430.o
 obj-$(CONFIG_TCS3472)		+= tcs3472.o
 obj-$(CONFIG_SENSORS_TSL2563)	+= tsl2563.o
 obj-$(CONFIG_TSL2583)		+= tsl2583.o
diff --git a/drivers/iio/light/tcs3430.c b/drivers/iio/light/tcs3430.c
new file mode 100644
index 0000000..138c8ab
--- /dev/null
+++ b/drivers/iio/light/tcs3430.c
@@ -0,0 +1,345 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * IIO driver for ams OSRAM TCS3430 Ambient Light and CIE 1931 XYZ Sensor
+ *
+ * Copyright (C) 2026 Robert Ijtsma <robertijtsma@gmail.com>
+ *
+ * Datasheet:
+ *   https://look.ams-osram.com/m/43d518b4cd81e3f2/original/TCS3430-DS000539.pdf
+ */
+
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/math64.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/regulator/consumer.h>
+#include <linux/types.h>
+#include <linux/units.h>
+
+#include <linux/iio/iio.h>
+
+#define TCS3430_REG_ENABLE		0x80
+#define   TCS3430_ENABLE_PON		BIT(0)
+#define   TCS3430_ENABLE_AEN		BIT(1)
+#define TCS3430_REG_ATIME		0x81
+#define TCS3430_REG_CFG1		0x90
+#define   TCS3430_CFG1_AGAIN_MASK	GENMASK(1, 0)
+#define TCS3430_REG_ID			0x92
+#define   TCS3430_ID_MASK		GENMASK(7, 2)
+#define   TCS3430_ID_VAL		0xdc
+#define TCS3430_REG_CH0DATAL		0x94
+#define TCS3430_REG_CH1DATAL		0x96
+#define TCS3430_REG_CH2DATAL		0x98
+#define TCS3430_REG_CH3DATAL		0x9a
+#define TCS3430_REG_CFG2		0x9f
+#define   TCS3430_CFG2_DEFAULT		0x04
+#define   TCS3430_CFG2_HGAIN		BIT(4)
+#define TCS3430_REG_CFG3		0xab
+#define   TCS3430_CFG3_DEFAULT		0x0c
+#define TCS3430_REG_INTENAB		0xdd
+
+/* 72 integration cycles * 2.78 ms = 200.16 ms */
+#define TCS3430_DEFAULT_ATIME_CYCLES	72
+#define TCS3430_ATIME_STEP_US		2780
+
+/*
+ * Photopic Y channel typical sensitivity from datasheet optical characteristics:
+ * ~0.537 counts/lux at AGAIN = 16x and ATIME = 100 ms (36 cycles), giving:
+ *   lux/count = 2980 / (gain * integration_time_ms)
+ */
+#define TCS3430_LUX_FACTOR		2980ULL
+
+static const int tcs3430_gains[] = { 1, 4, 16, 64, 128 };
+
+struct tcs3430_data {
+	struct i2c_client *client;
+	/* Protects device configuration across multi-register updates */
+	struct mutex lock;
+	u8 gain_idx;
+};
+
+static int tcs3430_apply_gain(struct tcs3430_data *data, u8 gain_idx)
+{
+	u8 cfg1, cfg2;
+	int ret;
+
+	if (gain_idx == 4) {
+		cfg1 = FIELD_PREP(TCS3430_CFG1_AGAIN_MASK, 3);
+		cfg2 = TCS3430_CFG2_DEFAULT | TCS3430_CFG2_HGAIN;
+	} else {
+		cfg1 = FIELD_PREP(TCS3430_CFG1_AGAIN_MASK, gain_idx);
+		cfg2 = TCS3430_CFG2_DEFAULT;
+	}
+
+	ret = i2c_smbus_write_byte_data(data->client, TCS3430_REG_CFG1, cfg1);
+	if (ret)
+		return ret;
+
+	ret = i2c_smbus_write_byte_data(data->client, TCS3430_REG_CFG2, cfg2);
+	if (ret)
+		return ret;
+
+	data->gain_idx = gain_idx;
+	return 0;
+}
+
+static int tcs3430_init_hw(struct tcs3430_data *data)
+{
+	struct i2c_client *client = data->client;
+	int ret;
+
+	ret = i2c_smbus_write_byte_data(client, TCS3430_REG_ENABLE, 0x00);
+	if (ret)
+		return ret;
+
+	ret = i2c_smbus_write_byte_data(client, TCS3430_REG_ATIME,
+					TCS3430_DEFAULT_ATIME_CYCLES - 1);
+	if (ret)
+		return ret;
+
+	ret = tcs3430_apply_gain(data, data->gain_idx);
+	if (ret)
+		return ret;
+
+	ret = i2c_smbus_write_byte_data(client, TCS3430_REG_CFG3,
+					TCS3430_CFG3_DEFAULT);
+	if (ret)
+		return ret;
+
+	ret = i2c_smbus_write_byte_data(client, TCS3430_REG_INTENAB, 0x00);
+	if (ret)
+		return ret;
+
+	return i2c_smbus_write_byte_data(client, TCS3430_REG_ENABLE,
+					 TCS3430_ENABLE_PON |
+					 TCS3430_ENABLE_AEN);
+}
+
+static void tcs3430_power_off(void *priv)
+{
+	struct tcs3430_data *data = priv;
+
+	i2c_smbus_write_byte_data(data->client, TCS3430_REG_ENABLE, 0x00);
+}
+
+static int tcs3430_read_raw(struct iio_dev *indio_dev,
+			    const struct iio_chan_spec *chan,
+			    int *val, int *val2, long mask)
+{
+	struct tcs3430_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		guard(mutex)(&data->lock);
+		ret = i2c_smbus_read_word_data(data->client, chan->address);
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE: {
+		u32 itime_us = TCS3430_DEFAULT_ATIME_CYCLES * TCS3430_ATIME_STEP_US;
+		u64 scale_nano;
+		u32 rem;
+		int gain;
+
+		scoped_guard(mutex, &data->lock)
+			gain = tcs3430_gains[data->gain_idx];
+
+		scale_nano = div_u64(TCS3430_LUX_FACTOR * NSEC_PER_SEC * USEC_PER_MSEC,
+				     (u32)gain * itime_us);
+		*val = div_u64_rem(scale_nano, NSEC_PER_SEC, &rem);
+		*val2 = rem;
+		return IIO_VAL_INT_PLUS_NANO;
+	}
+
+	case IIO_CHAN_INFO_HARDWAREGAIN:
+		guard(mutex)(&data->lock);
+		*val = tcs3430_gains[data->gain_idx];
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_INT_TIME:
+		*val = 0;
+		*val2 = TCS3430_DEFAULT_ATIME_CYCLES * TCS3430_ATIME_STEP_US;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int tcs3430_write_raw(struct iio_dev *indio_dev,
+			     const struct iio_chan_spec *chan,
+			     int val, int val2, long mask)
+{
+	struct tcs3430_data *data = iio_priv(indio_dev);
+	unsigned int i;
+
+	if (mask != IIO_CHAN_INFO_HARDWAREGAIN || val2 != 0)
+		return -EINVAL;
+
+	for (i = 0; i < ARRAY_SIZE(tcs3430_gains); i++) {
+		if (tcs3430_gains[i] == val) {
+			guard(mutex)(&data->lock);
+			return tcs3430_apply_gain(data, i);
+		}
+	}
+
+	return -EINVAL;
+}
+
+static int tcs3430_read_avail(struct iio_dev *indio_dev,
+			      const struct iio_chan_spec *chan,
+			      const int **vals, int *type, int *length,
+			      long mask)
+{
+	if (mask != IIO_CHAN_INFO_HARDWAREGAIN)
+		return -EINVAL;
+
+	*vals = tcs3430_gains;
+	*type = IIO_VAL_INT;
+	*length = ARRAY_SIZE(tcs3430_gains);
+	return IIO_AVAIL_LIST;
+}
+
+#define TCS3430_INTENSITY_CHANNEL(_mod, _addr) {			\
+	.type = IIO_INTENSITY,						\
+	.modified = 1,							\
+	.channel2 = (_mod),						\
+	.address = (_addr),						\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
+	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_HARDWAREGAIN) |	\
+				   BIT(IIO_CHAN_INFO_INT_TIME),		\
+	.info_mask_shared_by_all_available =				\
+		BIT(IIO_CHAN_INFO_HARDWAREGAIN),			\
+}
+
+static const struct iio_chan_spec tcs3430_channels[] = {
+	{
+		.type = IIO_LIGHT,
+		.address = TCS3430_REG_CH1DATAL,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_HARDWAREGAIN) |
+					   BIT(IIO_CHAN_INFO_INT_TIME),
+		.info_mask_shared_by_all_available =
+			BIT(IIO_CHAN_INFO_HARDWAREGAIN),
+	},
+	TCS3430_INTENSITY_CHANNEL(IIO_MOD_Z, TCS3430_REG_CH0DATAL),
+	TCS3430_INTENSITY_CHANNEL(IIO_MOD_Y, TCS3430_REG_CH1DATAL),
+	TCS3430_INTENSITY_CHANNEL(IIO_MOD_LIGHT_IR, TCS3430_REG_CH2DATAL),
+	TCS3430_INTENSITY_CHANNEL(IIO_MOD_X, TCS3430_REG_CH3DATAL),
+};
+
+static const struct iio_info tcs3430_info = {
+	.read_raw = tcs3430_read_raw,
+	.write_raw = tcs3430_write_raw,
+	.read_avail = tcs3430_read_avail,
+};
+
+static int tcs3430_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct tcs3430_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter,
+				     I2C_FUNC_SMBUS_BYTE_DATA |
+				     I2C_FUNC_SMBUS_WORD_DATA))
+		return -EOPNOTSUPP;
+
+	ret = devm_regulator_get_enable(dev, "vdd");
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to enable vdd supply\n");
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	data->client = client;
+	data->gain_idx = 3; /* 64x default gain */
+	i2c_set_clientdata(client, data);
+
+	ret = devm_mutex_init(dev, &data->lock);
+	if (ret)
+		return ret;
+
+	ret = i2c_smbus_read_byte_data(client, TCS3430_REG_ID);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "failed to read device ID\n");
+	if ((ret & TCS3430_ID_MASK) != TCS3430_ID_VAL)
+		dev_info(dev, "unexpected part ID 0x%02x\n", ret);
+
+	ret = tcs3430_init_hw(data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to initialize device\n");
+
+	ret = devm_add_action_or_reset(dev, tcs3430_power_off, data);
+	if (ret)
+		return ret;
+
+	indio_dev->name = "tcs3430";
+	indio_dev->info = &tcs3430_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = tcs3430_channels;
+	indio_dev->num_channels = ARRAY_SIZE(tcs3430_channels);
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static int tcs3430_suspend(struct device *dev)
+{
+	struct tcs3430_data *data = dev_get_drvdata(dev);
+
+	guard(mutex)(&data->lock);
+	return i2c_smbus_write_byte_data(data->client, TCS3430_REG_ENABLE, 0x00);
+}
+
+static int tcs3430_resume(struct device *dev)
+{
+	struct tcs3430_data *data = dev_get_drvdata(dev);
+
+	guard(mutex)(&data->lock);
+	return tcs3430_init_hw(data);
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(tcs3430_pm_ops, tcs3430_suspend,
+				tcs3430_resume);
+
+static const struct of_device_id tcs3430_of_match[] = {
+	{ .compatible = "ams,tcs3430" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, tcs3430_of_match);
+
+static const struct i2c_device_id tcs3430_id[] = {
+	{ .name = "tcs3430" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, tcs3430_id);
+
+static struct i2c_driver tcs3430_driver = {
+	.driver = {
+		.name = "tcs3430",
+		.of_match_table = tcs3430_of_match,
+		.pm = pm_sleep_ptr(&tcs3430_pm_ops),
+	},
+	.probe = tcs3430_probe,
+	.id_table = tcs3430_id,
+};
+module_i2c_driver(tcs3430_driver);
+
+MODULE_AUTHOR("Robert Ijtsma <robertijtsma@gmail.com>");
+MODULE_DESCRIPTION("ams OSRAM TCS3430 Ambient Light and XYZ Color Sensor Driver");
+MODULE_LICENSE("GPL");

-- 
2.56.0



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

* Re: [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver
  2026-11-24 22:46 ` [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver Robert Ijtsma via B4 Relay
@ 2026-11-24 22:56   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-24 22:56 UTC (permalink / raw)
  To: Robert Ijtsma; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] iio: light: tcs3430: unscoped guard(mutex) in
  tcs3430_read_raw() switch cases
--

commit 9716c3f00abb68b36cb419ee8d2edca1a86defb1
Author: Robert Ijtsma <robertijtsma@gmail.com>

iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver

This commit adds an IIO driver for the ams OSRAM TCS3430 ambient light and
CIE 1931 XYZ tristimulus color sensor.

> diff --git a/drivers/iio/light/tcs3430.c b/drivers/iio/light/tcs3430.c
> new file mode 100644
> index 0000000000000..138c8ab649c4f
> --- /dev/null
> +++ b/drivers/iio/light/tcs3430.c

[ ... ]

> +static int tcs3430_read_raw(struct iio_dev *indio_dev,
> +			    const struct iio_chan_spec *chan,
> +			    int *val, int *val2, long mask)
> +{
> +	struct tcs3430_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		guard(mutex)(&data->lock);
> +		ret = i2c_smbus_read_word_data(data->client, chan->address);
> +		if (ret < 0)
> +			return ret;
> +		*val = ret;
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_SCALE: {
> +		u32 itime_us = TCS3430_DEFAULT_ATIME_CYCLES * TCS3430_ATIME_STEP_US;
> +		u64 scale_nano;
> +		u32 rem;
> +		int gain;
> +
> +		scoped_guard(mutex, &data->lock)
> +			gain = tcs3430_gains[data->gain_idx];
> +
> +		scale_nano = div_u64(TCS3430_LUX_FACTOR * NSEC_PER_SEC * USEC_PER_MSEC,
> +				     (u32)gain * itime_us);
> +		*val = div_u64_rem(scale_nano, NSEC_PER_SEC, &rem);
> +		*val2 = rem;
> +		return IIO_VAL_INT_PLUS_NANO;
> +	}
> +
> +	case IIO_CHAN_INFO_HARDWAREGAIN:
> +		guard(mutex)(&data->lock);
> +		*val = tcs3430_gains[data->gain_idx];
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_INT_TIME:
> +		*val = 0;
> +		*val2 = TCS3430_DEFAULT_ATIME_CYCLES * TCS3430_ATIME_STEP_US;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}

[Severity: High]
Do the guard(mutex)(&data->lock) declarations in case IIO_CHAN_INFO_RAW and
case IIO_CHAN_INFO_HARDWAREGAIN need enclosing braces or scoped_guard()?

Without braces, both __cleanup(class_mutex_destructor) guard variables are
scoped to the entire switch (mask) block.  This fails to build with Clang
because the following case labels jump into the scope of a cleanup variable
past its initialization, and on GCC builds reading sysfs attributes such as
in_illuminance_scale jumps past the guard initialization and runs
class_mutex_destructor() on an uninitialized lock pointer upon return.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124-iio-tcs3430-v1-0-0f2a38a6a4c1@gmail.com?part=2

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 22:46 [PATCH 0/2] iio: light: add support for ams OSRAM TCS3430 Robert Ijtsma via B4 Relay
2026-11-24 22:46 ` [PATCH 1/2] dt-bindings: iio: light: add " Robert Ijtsma via B4 Relay
2026-11-24 22:46 ` [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver Robert Ijtsma via B4 Relay
2026-11-24 22:56   ` sashiko-bot

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