Re: [PATCH v3 2/2] iio: light: Add Capella CM3217 ambient light sensor driver

From: Andy Shevchenko

Date: Mon Nov 16 2026 - 04:43:44 EST


On Fri, Nov 13, 2026 at 08:59:00PM +0100, Patrick Fischer via B4 Relay wrote:

> The CM3217 has no registers and no ID. Writing a byte to address 0x10
> or 0x11 sets command byte 1 or 2, reading a byte from 0x10 or 0x11
> returns the high or low byte of the 16-bit light count. The driver
> takes the second address from the second reg entry and falls back to
> 0x11 when there is no firmware node, as with new_device.
>
> Command byte 1 holds the shutdown bit and the IT_T field (1/2 T, 1 T,
> 2 T or 4 T), command byte 2 the FD_IT field (800 ms down to 66 ms). The
> count scales linearly with both. The driver exposes FD_IT as the
> integration time and IT_T through the scale, so the scale list offers
> four ranges for each integration time.
>
> The datasheet is not public. The bit fields, the reserved bits that are
> written as 1 and the startup setting of 2 T and 100 ms come from NVIDIA's
> downstream drivers (drivers/staging/iio/light/cm3217.c in the 3.4 kernel
> rel-tegranote-r8 and the 3.10 kernel
> android-tegra-flounder-3.10-nougat-mr1.1). One value differs: the vendor
> header defines 4 T as 4 << 2, which is outside the two-bit field. On a
> Tegra Note 7 field value 3 gave four times the 1 T count, so the driver
> uses that. For the resolution, Capella
> gives a range of 0.01875 to 119156 lux. The driver takes 0.01875 lux
> per count at 4 T and 800 ms. This matches both the upper end (65535
> counts at 1/2 T and 66 ms) and the 0.3 lux per count that the NVIDIA
> 3.4 driver uses at 2 T and 100 ms.
>
> The sensor has no data-ready flag, and a new setting only takes effect
> when the conversion in progress ends. A conversion takes IT_T times
> FD_IT, up to 3.2 s. Leaving shutdown starts a new one at once, so the
> driver passes through shutdown on every change and waits one conversion
> period plus a margin before it reads the count. The timing was measured
> on a Tegra Note 7.

There are a few nit-picks below. But in general looks very good.

...

> +static int cm3217_read_count(struct cm3217_data *data, int *val)
> + __must_hold(&data->lock)
> +{
> + s64 wait_ms;
> + int lsb, msb;

Reversed xmas tree order.

> + wait_ms = ktime_ms_delta(data->ready, ktime_get());
> + if (wait_ms > 0)
> + msleep(wait_ms);
> +
> + lsb = i2c_smbus_read_byte(data->client_lsb);
> + if (lsb < 0)
> + return lsb;
> +
> + msb = i2c_smbus_read_byte(data->client);
> + if (msb < 0)
> + return msb;
> +
> + *val = msb << 8 | lsb;
> +
> + return 0;
> +}

...

> +static int cm3217_find_avail(const int (*avail)[2], unsigned int len,

This parameter is weird as we don't use at_least or so. It might be that
compiler can test it with the static const parameters supplied, but better
not to rely on that.

> + int val, int val2)
> +{
> + for (unsigned int i = 0; i < len; i++) {
> + if (avail[i][0] == val && avail[i][1] == val2)
> + return i;
> + }
> +
> + return -EINVAL;
> +}

With all complexity this gives, I would rather make one helper per each case.

static int cm3217_find_scale_avail(unsigned int fd_it, int val, int val2)
{
for (unsigned int i = 0; i < ARRAY_SIZE(cm3217_scale_avail[idx]); i++) {
if (cm3217_scale_avail[fd_it][i][0] == val &&
cm3217_scale_avail[fd_it][i][1] == val2)
return i;
}

return -EINVAL;
}

> +static int cm3217_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
> +{
> + struct cm3217_data *data = iio_priv(indio_dev);
> + unsigned int fd_it;
> + u8 cmd1, cmd2;
> + int i;
> +
> + guard(mutex)(&data->lock);
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_SCALE:

> + fd_it = FIELD_GET(CM3217_CMD2_FD_IT, data->cmd2);
> + i = cm3217_find_avail(cm3217_scale_avail[fd_it],
> + ARRAY_SIZE(cm3217_scale_avail[0]),
> + val, val2);
> + if (i < 0)
> + return i;

fd_it = FIELD_GET(CM3217_CMD2_FD_IT, data->cmd2);
i = cm3217_find_scale_avail(fd_it, val, val2);
if (i < 0)
return i;

> + cmd1 = data->cmd1;
> + FIELD_MODIFY(CM3217_CMD1_IT_T, &cmd1, i);
> +
> + return cm3217_write_cmd(data, cmd1, data->cmd2);
> + case IIO_CHAN_INFO_INT_TIME:
> + i = cm3217_find_avail(cm3217_int_time_avail,
> + ARRAY_SIZE(cm3217_int_time_avail),
> + val, val2);
> + if (i < 0)
> + return i;

In the similar way here. I think it it even grown in LoC, it will be not too
many,

> + cmd2 = FIELD_PREP(CM3217_CMD2_FD_IT, i);
> +
> + return cm3217_write_cmd(data, data->cmd1, cmd2);
> + default:
> + return -EINVAL;
> + }
> +}

...

> + data->client_lsb = devm_i2c_new_dummy_device(dev, client->adapter,
> + addr_lsb);

I would make it one line.

> + if (IS_ERR(data->client_lsb))
> + return dev_err_probe(dev, PTR_ERR(data->client_lsb),
> + "failed to register second address\n");

--
With Best Regards,
Andy Shevchenko