From: Joshua Crofts <crofts@kernel.org>
To: robertijtsma@gmail.com, "Jonathan Cameron" <jic23@kernel.org>,
"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>
Cc: 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: Wed, 25 Nov 2026 09:31:31 +0100 [thread overview]
Message-ID: <14111935-b991-457f-88d4-f5c06ae68df8@kernel.org> (raw)
In-Reply-To: <20261124-iio-tcs3430-v1-2-0f2a38a6a4c1@gmail.com>
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@gmail.com>
> + *
> + * 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
prev parent reply other threads:[~2026-11-25 8:31 UTC|newest]
Thread overview: 7+ 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-25 6:28 ` kernel test robot
2026-11-25 8:31 ` Joshua Crofts [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=14111935-b991-457f-88d4-f5c06ae68df8@kernel.org \
--to=crofts@kernel.org \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--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
all inboxes | Powered by JetHome®