From db77e5646c688d7206206524291232188125ea47 Mon Sep 17 00:00:00 2001 From: robertijtsma <37256770+robertijtsma@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:05:44 +0100 Subject: [PATCH 1/3] dt-bindings: iio: light: add ams TCS3430 Add Devicetree binding schema for the ams OSRAM TCS3430 ambient light and CIE 1931 XYZ tristimulus color sensor. Signed-off-by: robertijtsma <37256770+robertijtsma@users.noreply.github.com> --- .../bindings/iio/light/ams,tcs3430.yaml | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/ams,tcs3430.yaml 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 0000000000000..a846925290e41 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/light/ams,tcs3430.yaml @@ -0,0 +1,46 @@ +# 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: + - robertijtsma <37256770+robertijtsma@users.noreply.github.com> + +description: | + The ams OSRAM TCS3430 is an I2C ambient light and CIE 1931 XYZ tristimulus + color sensor with photopic Y, Z, X, and IR1 channels. + +properties: + compatible: + const: ams,tcs3430 + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + #include + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + light-sensor@39 { + compatible = "ams,tcs3430"; + reg = <0x39>; + interrupts = <24 IRQ_TYPE_LEVEL_LOW>; + }; + }; +... From 471ecdc725ff05c21dd601c866c3eadf5e437079 Mon Sep 17 00:00:00 2001 From: robertijtsma <37256770+robertijtsma@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:05:45 +0100 Subject: [PATCH 2/3] iio: light: add ams TCS3430 ambient light and XYZ color sensor driver Add an IIO driver for the ams OSRAM TCS3430 ambient light and XYZ tristimulus color sensor (present at I2C address 0x39 on the Microsoft Surface Pro X). Exposes processed photopic light intensity alongside raw Z, Y, IR1, and X intensity channels, automatic 1x..64x gain stepping, integration time reporting, and system suspend/resume power management. Tested on Microsoft Surface Pro X SQ2 (SC8180X, Linux 7.3.1-1-surface). Signed-off-by: robertijtsma <37256770+robertijtsma@users.noreply.github.com> --- drivers/iio/light/Kconfig | 10 ++ drivers/iio/light/Makefile | 1 + drivers/iio/light/tcs3430.c | 283 ++++++++++++++++++++++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 drivers/iio/light/tcs3430.c diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig index f23bbce12c72c..b34efd0c22a9d 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 64e354c49ed8d..ae8ac22537f05 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 0000000000000..1511821360c9e --- /dev/null +++ b/drivers/iio/light/tcs3430.c @@ -0,0 +1,283 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * ams OSRAM TCS3430 Ambient Light & XYZ Tristimulus IIO Driver + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TCS3430_ENABLE 0x80 +#define TCS3430_ATIME 0x81 +#define TCS3430_CFG1 0x90 +#define TCS3430_ID 0x92 +#define TCS3430_STATUS 0x93 +#define TCS3430_DATA 0x94 +#define TCS3430_CFG2 0x9f +#define TCS3430_CFG3 0xab +#define TCS3430_INTENAB 0xdd + +#define TCS3430_ENABLE_PON BIT(0) +#define TCS3430_ENABLE_AEN BIT(1) +#define TCS3430_ID_MASK GENMASK(7, 2) +#define TCS3430_ID_VAL 0xdc + +/* 72 integration cycles * 2.78 ms = ~200 ms continuous integration */ +#define TCS3430_INTEGRATION_CYCLES 72 +#define TCS3430_CYCLE_US 2780 + +static const int tcs3430_gains[] = { 1, 4, 16, 64 }; + +struct tcs3430_data { + struct i2c_client *client; + struct mutex lock; + u8 gain_code; + int frame[4]; /* Z, Y (photopic), IR1, X */ +}; + +static int tcs3430_write(struct tcs3430_data *data, u8 reg, u8 value) +{ + return i2c_smbus_write_byte_data(data->client, reg, value); +} + +static int tcs3430_init_hw(struct tcs3430_data *data) +{ + int ret; + + ret = tcs3430_write(data, TCS3430_ENABLE, 0x00); + if (ret < 0) + return ret; + + ret = tcs3430_write(data, TCS3430_ATIME, + TCS3430_INTEGRATION_CYCLES - 1); + if (ret < 0) + return ret; + + ret = tcs3430_write(data, TCS3430_CFG1, data->gain_code); + if (ret < 0) + return ret; + + ret = tcs3430_write(data, TCS3430_CFG2, 0x04); + if (ret < 0) + return ret; + + ret = tcs3430_write(data, TCS3430_CFG3, 0x0c); + if (ret < 0) + return ret; + + ret = tcs3430_write(data, TCS3430_INTENAB, 0x00); + if (ret < 0) + return ret; + + return tcs3430_write(data, TCS3430_ENABLE, + TCS3430_ENABLE_PON | TCS3430_ENABLE_AEN); +} + +static int tcs3430_read_frame(struct tcs3430_data *data) +{ + u8 raw[8]; + int ret, i; + int y; + + ret = i2c_smbus_read_i2c_block_data(data->client, TCS3430_DATA, + sizeof(raw), raw); + if (ret != sizeof(raw)) + return ret < 0 ? ret : -EIO; + + for (i = 0; i < 4; i++) + data->frame[i] = raw[i * 2] | (raw[i * 2 + 1] << 8); + + y = data->frame[1]; + if (y > 52000 && data->gain_code > 0) { + data->gain_code--; + tcs3430_write(data, TCS3430_CFG1, data->gain_code); + } else if (y < 200 && data->gain_code < ARRAY_SIZE(tcs3430_gains) - 1) { + data->gain_code++; + tcs3430_write(data, TCS3430_CFG1, data->gain_code); + } + + return 0; +} + +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, gain; + + switch (mask) { + case IIO_CHAN_INFO_PROCESSED: + mutex_lock(&data->lock); + ret = tcs3430_read_frame(data); + if (!ret) { + gain = tcs3430_gains[data->gain_code & 3]; + *val = (data->frame[1] * 64) / gain; + ret = IIO_VAL_INT; + } + mutex_unlock(&data->lock); + return ret; + case IIO_CHAN_INFO_RAW: + mutex_lock(&data->lock); + ret = tcs3430_read_frame(data); + if (!ret) { + *val = data->frame[chan->address]; + ret = IIO_VAL_INT; + } + mutex_unlock(&data->lock); + return ret; + case IIO_CHAN_INFO_HARDWAREGAIN: + mutex_lock(&data->lock); + *val = tcs3430_gains[data->gain_code & 3]; + mutex_unlock(&data->lock); + return IIO_VAL_INT; + case IIO_CHAN_INFO_INT_TIME: + *val = 0; + *val2 = TCS3430_INTEGRATION_CYCLES * TCS3430_CYCLE_US; + return IIO_VAL_INT_PLUS_MICRO; + default: + return -EINVAL; + } +} + +#define TCS3430_INTENSITY_CHANNEL(label, idx) { \ + .type = IIO_INTENSITY, \ + .indexed = 1, \ + .channel = (idx), \ + .extend_name = (label), \ + .address = (idx), \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \ + BIT(IIO_CHAN_INFO_INT_TIME), \ +} + +static const struct iio_chan_spec tcs3430_channels[] = { + { + .type = IIO_LIGHT, + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_HARDWAREGAIN) | + BIT(IIO_CHAN_INFO_INT_TIME), + }, + TCS3430_INTENSITY_CHANNEL("z", 0), + TCS3430_INTENSITY_CHANNEL("y", 1), + TCS3430_INTENSITY_CHANNEL("ir", 2), + TCS3430_INTENSITY_CHANNEL("x", 3), +}; + +static const struct iio_info tcs3430_info = { + .read_raw = tcs3430_read_raw, +}; + +static void tcs3430_power_off(void *priv) +{ + struct tcs3430_data *data = priv; + + tcs3430_write(data, TCS3430_ENABLE, 0x00); +} + +static int tcs3430_probe(struct i2c_client *client) +{ + 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_READ_I2C_BLOCK)) + return -EOPNOTSUPP; + + ret = i2c_smbus_read_byte_data(client, TCS3430_ID); + if (ret < 0) + return ret; + if ((ret & TCS3430_ID_MASK) != TCS3430_ID_VAL) + return dev_err_probe(&client->dev, -ENODEV, + "unexpected TCS3430 ID: %#02x\n", ret); + + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); + if (!indio_dev) + return -ENOMEM; + + data = iio_priv(indio_dev); + data->client = client; + data->gain_code = 3; /* 64x default gain */ + + ret = devm_mutex_init(&client->dev, &data->lock); + if (ret) + return ret; + + ret = tcs3430_init_hw(data); + if (ret < 0) + return ret; + + ret = devm_add_action_or_reset(&client->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); + i2c_set_clientdata(client, data); + + return devm_iio_device_register(&client->dev, indio_dev); +} + +static int tcs3430_suspend(struct device *dev) +{ + struct tcs3430_data *data = dev_get_drvdata(dev); + int ret; + + mutex_lock(&data->lock); + ret = tcs3430_write(data, TCS3430_ENABLE, 0x00); + mutex_unlock(&data->lock); + + return ret < 0 ? ret : 0; +} + +static int tcs3430_resume(struct device *dev) +{ + struct tcs3430_data *data = dev_get_drvdata(dev); + int ret; + + mutex_lock(&data->lock); + ret = tcs3430_init_hw(data); + mutex_unlock(&data->lock); + + return ret < 0 ? ret : 0; +} + +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[] = { + { "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_DESCRIPTION("ams OSRAM TCS3430 Ambient Light and XYZ Color Sensor Driver"); +MODULE_LICENSE("GPL"); From bbcc520244c01edef5c62733d56209607ee51f5c Mon Sep 17 00:00:00 2001 From: robertijtsma <37256770+robertijtsma@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:05:45 +0100 Subject: [PATCH 3/3] arm64: dts: qcom: sc8180x-surface-pro-x: switch ALS compatible to ams,tcs3430 The ambient light sensor at 0x39 on i2c2 is an ams OSRAM TCS3430 (chip ID 0xdc), not an Avago APDS9960. Update the compatible string to ams,tcs3430. Signed-off-by: robertijtsma <37256770+robertijtsma@users.noreply.github.com> --- arch/arm64/boot/dts/qcom/sc8180x-surface-pro-x.dts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/arm64/boot/dts/qcom/sc8180x-surface-pro-x.dts b/arch/arm64/boot/dts/qcom/sc8180x-surface-pro-x.dts index 91bf6480f0026..fc5ade360b3a0 100644 --- a/arch/arm64/boot/dts/qcom/sc8180x-surface-pro-x.dts +++ b/arch/arm64/boot/dts/qcom/sc8180x-surface-pro-x.dts @@ -1173,8 +1173,7 @@ pinctrl-0 = <&i2c2_active>, <&i2c2_als_active>; als@39 { - // TODO: this should actually be a TCS3430 - compatible = "avago,apds9960"; + compatible = "ams,tcs3430"; reg = <0x39>; interrupts-extended = <&tlmm 24 IRQ_TYPE_LEVEL_LOW>;