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

From: Andy Shevchenko

Date: Thu Nov 26 2026 - 09:20:55 EST


On Thu, Nov 26, 2026 at 09:11:48AM +0100, Robert Ijtsma via B4 Relay wrote:

> 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 scale and integration time via the IIO GTS helper across
> 1x/4x/16x/64x/128x gain and 50.04/100.08/200.16/400.32 ms integration
> time settings
> - Regulator management and system sleep suspend/resume support
>
> Tested on Microsoft Surface Pro X SQ2 (SC8180X).

...

> +config TCS3430
> + tristate "ams OSRAM TCS3430 ambient light and XYZ color sensor"

Make this, top comment in the driver and MODULE_DESCRIPTION() text to be very
similar if not the same.

> + depends on I2C
> + select IIO_GTS_HELPER
> + select REGMAP_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.

...

> +#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/module.h>
> +#include <linux/mutex.h>
> +#include <linux/pm.h>
> +#include <linux/regmap.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/types.h>

...

> +static const struct regmap_config tcs3430_regmap_config = {
> + .name = "tcs3430",
> + .reg_bits = 8,
> + .val_bits = 8,
> + .max_register = TCS3430_REG_INTENAB,

No cache? Why?

> +};

...

With

struct regmap *map = data->regmap;

at the top of the function helps with this...

> + ret = regmap_write(data->regmap, TCS3430_REG_CFG3,
> + TCS3430_CFG3_DEFAULT);

...to become this.

ret = regmap_write(map, TCS3430_REG_CFG3, TCS3430_CFG3_DEFAULT);

> + if (ret)
> + return ret;

You may use this trick elsewhere to achieve better readability.

...

> + switch (mask) {
> + case IIO_CHAN_INFO_RAW: {
> + __le16 raw;
> +
> + guard(mutex)(&data->lock);

+ Blank line to make guard()() visible at a glance. Ditto for all such cases.

> + ret = regmap_bulk_read(data->regmap, chan->address, &raw,
> + sizeof(raw));
> + if (ret)
> + return ret;
> + *val = le16_to_cpu(raw);
> + return IIO_VAL_INT;
> + }

...

> +static int tcs3430_set_scale(struct tcs3430_data *data, int val, int val2)
> +{
> + int gain_sel, time_sel, ret;
> + bool keep_time = false;
> +
> + ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, data->atime,
> + val, val2, &gain_sel);
> + if (!ret) {

Why non-standard pattern?

> + keep_time = true;
> + time_sel = data->atime;
> + } else {
> + ret = iio_gts_find_gain_time_sel_for_scale(&data->gts, val, val2,
> + &gain_sel, &time_sel);
> + if (ret)
> + return ret;
> + }
> +
> + if (!keep_time) {
> + ret = tcs3430_apply_itime(data, time_sel);
> + if (ret)
> + return ret;
> + }
> +
> + return tcs3430_apply_gain(data, gain_sel);
> +}

...

> +static int tcs3430_set_itime(struct tcs3430_data *data, int val, int val2)
> +{
> + struct device *dev = regmap_get_device(data->regmap);
> + int prev_gain, prev_time, new_gain, gain_sel, time_sel, ret;
> + bool in_range;

Better to rename to avoid confusion with generic in_range() helper.

> +
> + if (val || !iio_gts_valid_time(&data->gts, val2))
> + return -EINVAL;
> +
> + prev_time = iio_gts_find_int_time_by_sel(&data->gts, data->atime);
> + if (prev_time < 0)
> + return prev_time;
> + if (prev_time == val2)
> + return 0;
> +
> + prev_gain = iio_gts_find_gain_by_sel(&data->gts, data->gain_sel);
> + if (prev_gain < 0)
> + return prev_gain;
> +
> + ret = iio_gts_find_new_gain_by_gain_time_min(&data->gts, prev_gain,
> + prev_time, val2,
> + &new_gain, &in_range);
> + if (ret)
> + return ret;
> + if (!in_range)

> + dev_dbg(dev, "optimal gain out of range for integration time %d\n",
> + val2);

I would make it a single line despite the length.

> +
> + time_sel = iio_gts_find_sel_by_int_time(&data->gts, val2);
> + if (time_sel < 0)
> + return time_sel;
> +
> + gain_sel = iio_gts_find_sel_by_gain(&data->gts, new_gain);
> + if (gain_sel < 0)
> + return gain_sel;
> +
> + ret = tcs3430_apply_itime(data, time_sel);
> + if (ret)
> + return ret;
> +
> + return tcs3430_apply_gain(data, gain_sel);
> +}

...

> +static DEFINE_SIMPLE_DEV_PM_OPS(tcs3430_pm_ops, tcs3430_suspend,
> + tcs3430_resume);

A single line

static DEFINE_SIMPLE_DEV_PM_OPS(tcs3430_pm_ops, tcs3430_suspend, tcs3430_resume);

or wrap on a logical borders

static DEFINE_SIMPLE_DEV_PM_OPS(tcs3430_pm_ops,
tcs3430_suspend, tcs3430_resume);

But the above is only 81 character long.

--
With Best Regards,
Andy Shevchenko