From: Jesper X Nilsson <jesper.x.nilsson@axis.com>
To: "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>,
"Stefan Windfeldt-Prytz" <stefan.windfeldt-prytz@axis.com>
Cc: <linux-iio@vger.kernel.org>, <devicetree@vger.kernel.org>,
<linux-kernel@vger.kernel.org>,
Jesper X Nilsson <jesper.x.nilsson@axis.com>, <kernel@axis.com>
Subject: [PATCH v2 3/4] iio: light: opt4001: refactor driver to prepare to support more devices
Date: Fri, 23 Oct 2026 14:30:30 +0200 [thread overview]
Message-ID: <20261023-opt4041-v2-3-ee320812804c@axis.com> (raw)
In-Reply-To: <20261023-opt4041-v2-0-ee320812804c@axis.com>
Prepare opt4001 driver to be able to support OPT4041. Extend struct
opt4001_chip_info with fields channels and device_id because OPT4041 has
two channels and another device id.
Datasheet: https://www.ti.com/lit/gpn/opt4041
Signed-off-by: Jesper X Nilsson <jesper.x.nilsson@axis.com>
---
drivers/iio/light/opt4001.c | 85 +++++++++++++++++++++++++++++----------------
1 file changed, 55 insertions(+), 30 deletions(-)
diff --git a/drivers/iio/light/opt4001.c b/drivers/iio/light/opt4001.c
index dcde10914a1e..dc2eac8ad4af 100644
--- a/drivers/iio/light/opt4001.c
+++ b/drivers/iio/light/opt4001.c
@@ -77,6 +77,9 @@
struct opt4001_chip_info {
struct s32_fract ratio;
const char *name;
+ const struct iio_chan_spec *channels;
+ int num_channels;
+ u16 device_id;
};
struct opt4001_chip {
@@ -86,14 +89,30 @@ struct opt4001_chip {
const struct opt4001_chip_info *chip_info;
};
+static const struct iio_chan_spec opt4001_channels[] = {
+ {
+ .type = IIO_LIGHT,
+ .address = OPT4001_LIGHT1_MSB,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+ .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME),
+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
+ },
+};
+
static const struct opt4001_chip_info opt4001_sot_5x3_info = {
.ratio = { .numerator = 4375, .denominator = 10000000 },
- .name = "opt4001-sot-5x3"
+ .name = "opt4001-sot-5x3",
+ .channels = opt4001_channels,
+ .num_channels = ARRAY_SIZE(opt4001_channels),
+ .device_id = OPT4001_DEVICE_ID_VAL,
};
static const struct opt4001_chip_info opt4001_picostar_info = {
.ratio = { .numerator = 3125, .denominator = 10000000 },
- .name = "opt4001-picostar"
+ .name = "opt4001-picostar",
+ .channels = opt4001_channels,
+ .num_channels = ARRAY_SIZE(opt4001_channels),
+ .device_id = OPT4001_DEVICE_ID_VAL,
};
static const int opt4001_int_time_available[][2] = {
@@ -156,30 +175,28 @@ static u8 opt4001_calculate_crc(u8 exp, u32 mantissa, u8 count)
return crc;
}
-static int opt4001_read_lux_value(struct iio_dev *indio_dev,
- int *val, int *val2)
+static int opt4001_read_raw_value(struct opt4001_chip *chip, unsigned int reg,
+ u64 *raw_value)
{
- struct opt4001_chip *chip = iio_priv(indio_dev);
struct device *dev = &chip->client->dev;
unsigned int light1;
unsigned int light2;
+ u32 mantissa;
+ u8 calc_crc;
+ u8 count;
u16 msb;
u16 lsb;
+ int ret;
u8 exp;
- u8 count;
u8 crc;
- u8 calc_crc;
- u64 lux_raw;
- u32 rem;
- int ret;
- ret = regmap_read(chip->regmap, OPT4001_LIGHT1_MSB, &light1);
+ ret = regmap_read(chip->regmap, reg, &light1);
if (ret < 0) {
dev_err(dev, "Failed to read data bytes");
return ret;
}
- ret = regmap_read(chip->regmap, OPT4001_LIGHT1_LSB, &light2);
+ ret = regmap_read(chip->regmap, reg + 1, &light2);
if (ret < 0) {
dev_err(dev, "Failed to read data bytes");
return ret;
@@ -190,12 +207,27 @@ static int opt4001_read_lux_value(struct iio_dev *indio_dev,
crc = FIELD_GET(OPT4001_CRC_MASK, light2);
msb = FIELD_GET(OPT4001_MSB_MASK, light1);
lsb = FIELD_GET(OPT4001_LSB_MASK, light2);
- lux_raw = (msb << 8) + lsb;
- calc_crc = opt4001_calculate_crc(exp, lux_raw, count);
+ mantissa = (msb << 8) + lsb;
+ calc_crc = opt4001_calculate_crc(exp, mantissa, count);
if (calc_crc != crc)
return -EIO;
- lux_raw = lux_raw << exp;
+ *raw_value = (u64)mantissa << exp;
+
+ return 0;
+}
+
+static int opt4001_read_lux_value(struct opt4001_chip *chip, unsigned int reg,
+ int *val, int *val2)
+{
+ u64 lux_raw;
+ u32 rem;
+ int ret;
+
+ ret = opt4001_read_raw_value(chip, reg, &lux_raw);
+ if (ret)
+ return ret;
+
lux_raw = lux_raw * chip->chip_info->ratio.numerator;
*val = div_u64_rem(lux_raw, chip->chip_info->ratio.denominator, &rem);
*val2 = rem * 100;
@@ -230,15 +262,6 @@ static void opt4001_chip_off_action(void *data)
dev_err(&chip->client->dev, "Failed to power down\n");
}
-static const struct iio_chan_spec opt4001_channels[] = {
- {
- .type = IIO_LIGHT,
- .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
- .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME),
- .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME)
- },
-};
-
static int opt4001_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
@@ -247,7 +270,7 @@ static int opt4001_read_raw(struct iio_dev *indio_dev,
switch (mask) {
case IIO_CHAN_INFO_PROCESSED:
- return opt4001_read_lux_value(indio_dev, val, val2);
+ return opt4001_read_lux_value(chip, chan->address, val, val2);
case IIO_CHAN_INFO_INT_TIME:
*val = 0;
*val2 = opt4001_int_time_reg[chip->int_time][0];
@@ -385,19 +408,21 @@ static int opt4001_probe(struct i2c_client *client)
return dev_err_probe(&client->dev, ret,
"failed to reinit regmap cache\n");
+ chip->chip_info = i2c_get_match_data(client);
+ if (!chip->chip_info)
+ return -ENODATA;
+
ret = regmap_read(chip->regmap, OPT4001_DEVICE_ID, &dev_id);
if (ret < 0)
return dev_err_probe(&client->dev, ret,
"Failed to read the device ID register\n");
dev_id = FIELD_GET(OPT4001_DEVICE_ID_MASK, dev_id);
- if (dev_id != OPT4001_DEVICE_ID_VAL)
+ if (dev_id != chip->chip_info->device_id)
dev_warn(&client->dev, "Device ID: %#04x unknown\n", dev_id);
- chip->chip_info = i2c_get_match_data(client);
-
- indio_dev->channels = opt4001_channels;
- indio_dev->num_channels = ARRAY_SIZE(opt4001_channels);
+ indio_dev->channels = chip->chip_info->channels;
+ indio_dev->num_channels = chip->chip_info->num_channels;
indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->name = chip->chip_info->name;
--
2.43.0
next prev parent reply other threads:[~2026-10-23 12:30 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-23 12:30 [PATCH v2 0/4] This series adds support for Texas Instruments OPT4041 Ambient light sensor Jesper X Nilsson
2026-10-23 12:30 ` [PATCH v2 1/4] dt-bindings: iio: light: opt4001: add opt4041 Jesper X Nilsson
2026-10-23 12:30 ` [PATCH v2 2/4] iio: light: opt4001: Re-use generic struct s32_fract Jesper X Nilsson
2026-10-23 12:30 ` Jesper X Nilsson [this message]
2026-10-23 13:51 ` [PATCH v2 3/4] iio: light: opt4001: refactor driver to prepare to support more devices Andy Shevchenko
2026-10-23 12:30 ` [PATCH v2 4/4] iio: light: opt4001: add support for opt4041 Jesper X Nilsson
2026-10-23 13:53 ` Andy Shevchenko
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=20261023-opt4041-v2-3-ee320812804c@axis.com \
--to=jesper.x.nilsson@axis.com \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=kernel@axis.com \
--cc=krzk+dt@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
--cc=stefan.windfeldt-prytz@axis.com \
/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®