Re: [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver
From: Jonathan Cameron
Date: Wed Nov 25 2026 - 21:35:52 EST
On Tue, 24 Nov 2026 23:46:58 +0100
Robert Ijtsma via B4 Relay <devnull+robertijtsma.gmail.com@xxxxxxxxxx> wrote:
> From: Robert Ijtsma <robertijtsma@xxxxxxxxx>
>
> 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
So hardware gain has an interesting history. It isn't the interface that
should be used to control the gain of a sensor like this. For that
_scale should be used. Hardware gain is pretty much only for the
weird corner where the thing you are adjusting the gain on is not actually
what you are measuring. Time of flight sensors are the main user.
I have let in drivers that use it as a read only parameter + there
might be one or two others where I missed it being used as the control.
Scale well predates it in the ABI so is what userspace software
is more likely to expect.
I appreciate that it can feel a little tricky when we also have
integration time scaling in the mix. There is a helper
library that can make this easier in drivers/iio/industrialio-gts-helper.c
I haven't thought hard about whether that applies well in this
particular case.
Otherwise, just some quick comments below.
> - Regulator management and system sleep suspend/resume support
>
> Tested on Microsoft Surface Pro X SQ2 (SC8180X).
>
> Signed-off-by: Robert Ijtsma <robertijtsma@xxxxxxxxx>
> 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
> +
> +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);
I'll assume you fix the scope issues already point out.
> + 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)
Given the maths is quick, I'd just hold the lock for the remainder of the
function.
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;
> + }
> +
> +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++) {
for (unsigned int i = 0;
is fine and saves a line.
> + if (tcs3430_gains[i] == val) {
> + guard(mutex)(&data->lock);
> + return tcs3430_apply_gain(data, i);
> + }
> + }
> +
> + return -EINVAL;
> +}