Re: [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver

From: Joshua Crofts

Date: Wed Nov 25 2026 - 03:31:37 EST


On 24-Nov-26 23:46, Robert Ijtsma via B4 Relay wrote:

...

> 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@xxxxxxxxx>
> + *
> + * 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>

No need for this, the device tables required will be pulled in
via the respective bus headers.

> +#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>
> +

..

> +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);

Why not use regmap for read/write operations?

> + 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);

cleanup.h magic works thanks to local variables and a scope. When using
guard() in a switch case it has to be surrounded by brackets to define
the scope (this is what the email from lkp is warning about).

> + 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);

Same here.

> + *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_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;

For IIO macros we usually use a switch, even if we're only checking
for one case.

--
Kind regards,
Joshua Crofts