Devicetree
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Robert Ijtsma via B4 Relay <devnull+robertijtsma.gmail.com@kernel.org>
Cc: robertijtsma@gmail.com, "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver
Date: Thu, 26 Nov 2026 02:35:40 +0000	[thread overview]
Message-ID: <20261126023540.35016e71@jic23-hlaptop> (raw)
In-Reply-To: <20261124-iio-tcs3430-v1-2-0f2a38a6a4c1@gmail.com>

On Tue, 24 Nov 2026 23:46:58 +0100
Robert Ijtsma via B4 Relay <devnull+robertijtsma.gmail.com@kernel.org> wrote:

> From: Robert Ijtsma <robertijtsma@gmail.com>
> 
> 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@gmail.com>

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


      parent reply	other threads:[~2026-11-26  2:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 22:46 [PATCH 0/2] iio: light: add support for ams OSRAM TCS3430 Robert Ijtsma via B4 Relay
2026-11-24 22:46 ` [PATCH 1/2] dt-bindings: iio: light: add " Robert Ijtsma via B4 Relay
2026-11-25  7:43   ` Krzysztof Kozlowski
2026-11-25  8:02   ` Joshua Crofts
2026-11-24 22:46 ` [PATCH 2/2] iio: light: add ams OSRAM TCS3430 XYZ and ambient light sensor driver Robert Ijtsma via B4 Relay
2026-11-24 22:56   ` sashiko-bot
2026-11-25  6:28   ` kernel test robot
2026-11-25  8:31   ` Joshua Crofts
2026-11-26  2:35   ` Jonathan Cameron [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126023540.35016e71@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=devnull+robertijtsma.gmail.com@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=robertijtsma@gmail.com \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox