Skip to contentSkip to conversation

iio: light: opt3001: add support for TI's opt3002 light sensor

PATCH Superseded Emil Gedenryd 3 participants 10 messages Sep 16, 2024 – Sep 30, 2024

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover iio: light: opt3001: add support for TI's opt3002 light sensor

TI's opt3002 light-to-digital sensor provides the functionality of an optical power meter within a single device. It shares a lot of similarities with their opt3001 model but has a wide spectral bandwidth, ranging from 300 nm to 1000 nm.

This patch set adds support for the TI opt3002 by extending the opt3001 driver. In addition, a missing full-scale range value for the opt3001 is added, resulting in higher precision when setting event trigger values.

Datasheet: http://www.ti.com/product/OPT3002

---
Changes in v3:
- Include difference between opt3001 and opt3002 in commit message for dt-binding patch.
- Remove whitespace between tags in commit message for opt3002 patch.
- Rename opt300x_chip_info to opt3001_chip_info.
- Add documentation for mathematical constants in opt3001_chip_info.
- Only check return value after opt3001_read_id if chip has id register in opt3001_probe.
- Change opt3002 channel mask to IIO_CHAN_INFO_RAW.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Move dt-binding patch to before implementation.
- Fix dt-binding compatible definition.
- Clarify bug description for missing full-scale range value.
- Remove model enum from chip info and all in-function switch-case statements.
- Move model-specific channels and mathematic constants to chip info.
- Add valid match data to opt3001_id array
- Skip call to function opt3001_read_id() if model doesn't have a device id register.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Emil Gedenryd (3):
iio: light: opt3001: add missing full-scale range value
dt-bindings: iio: light: opt3001: add compatible for opt3002
iio: light: opt3001: add support for TI's opt3002 light sensor

 .../devicetree/bindings/iio/light/ti,opt3001.yaml  |   4 +-
 drivers/iio/light/Kconfig                          |   2 +-
 drivers/iio/light/opt3001.c                        | 190 +++++++++++++++++----
 3 files changed, 161 insertions(+), 35 deletions(-)

---
base-commit: 5be63fc19fcaa4c236b307420483578a56986a37
change-id: 20240828-add_opt3002-40552c1a2f77

Best regards,

Signature · 1 line
Emil Gedenryd <[email protected]>
#1 lore raw

PATCH v3 1/3 iio: light: opt3001: add missing full-scale range value

The opt3001 driver uses predetermined full-scale range values to determine what exponent to use for event trigger threshold values. The problem is that one of the values specified in the datasheet is missing from the implementation. This causes larger values to be scaled down to an incorrect exponent, effectively reducing the maximum settable threshold value by a factor of 2.

Add missing full-scale range array value.

Fixes 94a9b7b1809f ("iio: light: add support for TI's opt3001 light sensor")
---
 drivers/iio/light/opt3001.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iio/light/opt3001.c +4 -0
index 887c4b776a86..176e54bb48c3 100644
@@ -138,6 +138,10 @@ static const struct opt3001_scale opt3001_scales[] = {
 		.val = 20966,
 		.val2 = 400000,
 	},
+	{
+		.val = 41932,
+		.val2 = 800000,
+	},
 	{
 		.val = 83865,
 		.val2 = 600000,
Signature · 1 line
2.39.2
maintainer
#2 lore raw

Re: [PATCH v3 1/3] iio: light: opt3001: add missing full-scale range value

Quoted 11 lines · On Mon, 16 Sep 2024 16:56:37 +0200 Emil Gedenryd <[email protected]> wrote:

The opt3001 driver uses predetermined full-scale range values to determine what exponent to use for event trigger threshold values. The problem is that one of the values specified in the datasheet is missing from the implementation. This causes larger values to be scaled down to an incorrect exponent, effectively reducing the maximum settable threshold value by a factor of 2.

Add missing full-scale range array value.

Fixes 94a9b7b1809f ("iio: light: add support for TI's opt3001 light sensor")

This one is already queued up, so you can drop it from future versions of this series posted for review.

Thanks,

Jonathan

Quoted 20 lines
---
 drivers/iio/light/opt3001.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iio/light/opt3001.c +4 -0
index 887c4b776a86..176e54bb48c3 100644
@@ -138,6 +138,10 @@ static const struct opt3001_scale opt3001_scales[] = {
 		.val = 20966,
 		.val2 = 400000,
 	},
+	{
+		.val = 41932,
+		.val2 = 800000,
+	},
 	{
 		.val = 83865,
 		.val2 = 600000,
#3 lore raw

Re: [PATCH v3 1/3] iio: light: opt3001: add missing full-scale range value

Quoted 12 lines · Jonathan Cameron
Quoted 9 lines · On Mon, 16 Sep 2024 16:56:37 +0200 Emil Gedenryd <[email protected]> wrote:

The opt3001 driver uses predetermined full-scale range values to determine what exponent to use for event trigger threshold values. The problem is that one of the values specified in the datasheet is missing from the implementation. This causes larger values to be scaled down to an incorrect exponent, effectively reducing the maximum settable threshold value by a factor of 2.

Add missing full-scale range array value.

Quoted 8 lines
Quoted 2 lines
Fixes 94a9b7b1809f ("iio: light: add support for TI's opt3001 light sensor")

This one is already queued up, so you can drop it from future versions of this series posted for review.

Thanks,

Jonathan

Okay, I'll leave it out in the future.

Thanks,
Emil

#4 lore raw

PATCH v3 2/3 dt-bindings: iio: light: opt3001: add compatible for opt3002

In reply to Emil Gedenryd

The opt3002 is a Light-to-Digital Sensor by TI with support for wide-range spectrum light. It shares most properties with their opt3001 model with the exception of having a wide spectral bandwidth, ranging from 300 nm to 1000 nm.

Add the compatible string of opt3002.

---
 Documentation/devicetree/bindings/iio/light/ti,opt3001.yaml | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/ti,opt3001.yaml +3 -1
index 441e9343fc97..67ca8d08256a 100644
@@ -15,7 +15,9 @@ description: |
 
 properties:
   compatible:
-    const: ti,opt3001
+    enum:
+      - ti,opt3001
+      - ti,opt3002
 
   reg:
     maxItems: 1
Signature · 1 line
2.39.2
Acked-by on v3 2/3
#5 lore raw

Re: [PATCH v3 2/3] dt-bindings: iio: light: opt3001: add compatible for opt3002

Quoted 8 lines · Emil Gedenryd

The opt3002 is a Light-to-Digital Sensor by TI with support for wide-range spectrum light. It shares most properties with their opt3001 model with the exception of having a wide spectral bandwidth, ranging from 300 nm to 1000 nm.

Add the compatible string of opt3002.

Pretty sure I gave you an ack for this already, provided you added the a description of the differences, which you have.

Cheers,
Conor.

#6 lore raw

Re: [PATCH v3 2/3] dt-bindings: iio: light: opt3001: add compatible for opt3002

Quoted 10 lines · Conor Dooley
Quoted 8 lines · Emil Gedenryd

The opt3002 is a Light-to-Digital Sensor by TI with support for wide-range spectrum light. It shares most properties with their opt3001 model with the exception of having a wide spectral bandwidth, ranging from 300 nm to 1000 nm.

Add the compatible string of opt3002.

Quoted 7 lines

Pretty sure I gave you an ack for this already, provided you added the a description of the differences, which you have.

Cheers,
Conor.

Thank you!

Regards,
Emil

#7 lore raw

PATCH v3 3/3 iio: light: opt3001: add support for TI's opt3002 light sensor

In reply to Emil Gedenryd

TI's opt3002 light sensor shares most properties with the opt3001 model, with the exception of supporting a wider spectrum range.

Add support for TI's opt3002 by extending the TI opt3001 driver.

Datasheet: https://www.ti.com/product/OPT3002

---
 drivers/iio/light/Kconfig   |   2 +-
 drivers/iio/light/opt3001.c | 186 ++++++++++++++++++++++++++++++++++++--------
 2 files changed, 154 insertions(+), 34 deletions(-)
drivers/iio/light/Kconfig +1 -1
index b68dcc1fbaca..c35bf962dae6 100644
@@ -461,7 +461,7 @@ config OPT3001
 	depends on I2C
 	help
 	  If you say Y or M here, you get support for Texas Instruments
-	  OPT3001 Ambient Light Sensor.
+	  OPT3001 Ambient Light Sensor, OPT3002 Light-to-Digital Sensor.
 
 	  If built as a dynamically linked module, it will be called
 	  opt3001.
drivers/iio/light/opt3001.c +153 -33

311 lines not on this page · raw message

Signature · 1 line
2.39.2
maintainer
#8 lore raw
Quoted 7 lines · On Mon, 16 Sep 2024 16:56:39 +0200 Emil Gedenryd <[email protected]> wrote:

TI's opt3002 light sensor shares most properties with the opt3001 model, with the exception of supporting a wider spectrum range.

Add support for TI's opt3002 by extending the TI opt3001 driver.

Datasheet: https://www.ti.com/product/OPT3002

Hi Emil,

A few things inline,

Thanks,

Jonathan

Quoted 31 lines
drivers/iio/light/opt3001.c +9 -1
index 176e54bb48c3..5e3fe42c5b59 100644
@@ -479,6 +565,9 @@ static int opt3001_write_event_value(struct iio_dev *iio,
 {
 	struct opt3001 *opt = iio_priv(iio);
 	int ret;
+	int whole;
+	int integer;
+	int decimal;
 
 	u16 mantissa;
 	u16 value;
@@ -497,7 +586,12 @@ static int opt3001_write_event_value(struct iio_dev *iio,
 		goto err;
 	}
 
-	mantissa = (((val * 1000) + (val2 / 1000)) / 10) >> exponent;
+	whole = opt->chip_info->factor_whole;
+	integer = opt->chip_info->factor_integer;
+	decimal = opt->chip_info->factor_decimal;
+
+	mantissa = (((val * integer) + (val2 / decimal)) / whole) >> exponent;
+
 	value = (exponent << 12) | mantissa;
 
 	switch (dir) {

drivers/iio/light/opt3001.c static int opt3001_read_id(struct opt3001 *opt) · line 707

Quoted 5 lines
drivers/iio/light/opt3001.c +1 -1
@@ -610,7 +704,7 @@ static int opt3001_read_id(struct opt3001 *opt)
 	ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID);
 	if (ret < 0) {
 		dev_err(opt->dev, "failed to read register %02x\n",
-				OPT3001_DEVICE_ID);
+			OPT3001_DEVICE_ID);

Unrelated change so in theory should be in a separate patch but meh, it's trivial so leave it here if you like.

drivers/iio/light/opt3001.c static int opt3001_probe(struct i2c_client *client) · line 853

Quoted 8 lines
drivers/iio/light/opt3001.c +1 -0
 		return ret;
 	}
 
@@ -755,22 +850,25 @@ static int opt3001_probe(struct i2c_client *client)
 	opt = iio_priv(iio);
 	opt->client = client;
 	opt->dev = dev;
+	opt->chip_info = device_get_match_data(&client->dev);

Use the i2c specific way to to do this.
https://elixir.bootlin.com/linux/v6.11/source/drivers/i2c/i2c-core-base.c#L120
i2c_get_match_data() because it will also handle falling back to matching via the i2c_match_id() path against the old style match tables in a few cases.

Quoted 13 lines
drivers/iio/light/opt3001.c +5 -3
 	mutex_init(&opt->lock);
 	init_waitqueue_head(&opt->result_ready_queue);
 	i2c_set_clientdata(client, iio);
 
-	ret = opt3001_read_id(opt);
-	if (ret)
-		return ret;
+	if (opt->chip_info->has_id) {
+		ret = opt3001_read_id(opt);
+		if (ret)
+			return ret;
+	}

drivers/iio/light/opt3001.c static int opt3001_probe(struct i2c_client *client) · line 871

Quoted 10 lines
drivers/iio/light/opt3001.c +2 -2
 
 	ret = opt3001_configure(opt);
 	if (ret)
 		return ret;
 
 	iio->name = client->name;
-	iio->channels = opt3001_channels;
-	iio->num_channels = ARRAY_SIZE(opt3001_channels);
+	iio->channels = *opt->chip_info->channels;
+	iio->num_channels = ARRAY_SIZE(*opt->chip_info->channels);

This won't work as it has no way to perform a sizeof through a pointer.

Add a num_channels filed to your opt3001_chip_info structure as then it can be ARRAY_SIZE(&opt3001_channels) which can work.

Quoted 43 lines
drivers/iio/light/opt3001.c +24 -2
 	iio->modes = INDIO_DIRECT_MODE;
 	iio->info = &opt3001_info;
 
@@ -825,14 +923,36 @@ static void opt3001_remove(struct i2c_client *client)
 	}
 }
 
+static const struct opt3001_chip_info opt3001_chip_information = {
+	.channels = &opt3001_channels,
+	.chan_type = IIO_LIGHT,
+	.scales = &opt3001_scales,
+	.factor_whole = 10,
+	.factor_integer = 1000,
+	.factor_decimal = 1000,
+	.has_id = true,
+};
+
+static const struct opt3001_chip_info opt3002_chip_information = {
+	.channels = &opt3002_channels,
+	.chan_type = IIO_INTENSITY,
+	.scales = &opt3002_scales,
+	.factor_whole = 12,
+	.factor_integer = 10,
+	.factor_decimal = 100000,
+	.has_id = false,
+};
+
 static const struct i2c_device_id opt3001_id[] = {
-	{ "opt3001" },
+	{ "opt3001", (kernel_ulong_t)&opt3001_chip_information },
+	{ "opt3002", (kernel_ulong_t)&opt3002_chip_information },
 	{ } /* Terminating Entry */
 };
 MODULE_DEVICE_TABLE(i2c, opt3001_id);
 
 static const struct of_device_id opt3001_of_match[] = {
-	{ .compatible = "ti,opt3001" },
+	{ .compatible = "ti,opt3001", .data = &opt3001_chip_information },
+	{ .compatible = "ti,opt3002", .data = &opt3002_chip_information },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, opt3001_of_match);
#9 lore raw
Quoted 10 lines · Jonathan Cameron
Quoted 7 lines · On Mon, 16 Sep 2024 16:56:39 +0200 Emil Gedenryd <[email protected]> wrote:

TI's opt3002 light sensor shares most properties with the opt3001 model, with the exception of supporting a wider spectrum range.

Add support for TI's opt3002 by extending the TI opt3001 driver.

Datasheet: https://www.ti.com/product/OPT3002

Quoted 7 lines

Hi Emil,

A few things inline,

Thanks,

Jonathan

Hi Jonathan,

Thank you for the comments, I'll start working on fixing them during the week.

Best Regards,
Emil

Quoted 11 lines

drivers/iio/light/opt3001.c static int opt3001_read_id(struct opt3001 *opt) · line 707

Quoted 8 lines
drivers/iio/light/opt3001.c +1 -1
@@ -610,7 +704,7 @@ static int opt3001_read_id(struct opt3001 *opt)
 	ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID);
 	if (ret < 0) {
 		dev_err(opt->dev, "failed to read register %02x\n",
-				OPT3001_DEVICE_ID);
+			OPT3001_DEVICE_ID);

Unrelated change so in theory should be in a separate patch but meh, it's trivial so leave it here if you like.

Good to know! I'll keep it since it's minor but I'll remember this for the future.

Quoted 4 lines
Quoted 3 lines
drivers/iio/light/opt3001.c +0 -0
 		return ret;
 	}
Quoted 11 lines

drivers/iio/light/opt3001.c static int opt3001_probe(struct i2c_client *client) · line 853

Quoted 5 lines
drivers/iio/light/opt3001.c +1 -0
@@ -755,22 +850,25 @@ static int opt3001_probe(struct i2c_client *client)
 	opt = iio_priv(iio);
 	opt->client = client;
 	opt->dev = dev;
+	opt->chip_info = device_get_match_data(&client->dev);

Use the i2c specific way to to do this.
https://elixir.bootlin.com/linux/v6.11/source/drivers/i2c/i2c-core-base.c#L120
i2c_get_match_data() because it will also handle falling back to matching via the i2c_match_id() path against the old style match tables in a few cases.

Okay!

Quoted 26 lines
Quoted 13 lines
drivers/iio/light/opt3001.c +5 -3
 	mutex_init(&opt->lock);
 	init_waitqueue_head(&opt->result_ready_queue);
 	i2c_set_clientdata(client, iio);
 
-	ret = opt3001_read_id(opt);
-	if (ret)
-		return ret;
+	if (opt->chip_info->has_id) {
+		ret = opt3001_read_id(opt);
+		if (ret)
+			return ret;
+	}

drivers/iio/light/opt3001.c static int opt3001_probe(struct i2c_client *client) · line 871

Quoted 10 lines
drivers/iio/light/opt3001.c +2 -2
 
 	ret = opt3001_configure(opt);
 	if (ret)
 		return ret;
 
 	iio->name = client->name;
-	iio->channels = opt3001_channels;
-	iio->num_channels = ARRAY_SIZE(opt3001_channels);
+	iio->channels = *opt->chip_info->channels;
+	iio->num_channels = ARRAY_SIZE(*opt->chip_info->channels);

This won't work as it has no way to perform a sizeof through a pointer.

Quoted 2 lines

Add a num_channels filed to your opt3001_chip_info structure as then it can be ARRAY_SIZE(&opt3001_channels) which can work.

Good catch and thanks for the suggestion, I'll do it that way!

Quoted 45 lines
Quoted 44 lines
drivers/iio/light/opt3001.c +24 -2
 	iio->modes = INDIO_DIRECT_MODE;
 	iio->info = &opt3001_info;
 
@@ -825,14 +923,36 @@ static void opt3001_remove(struct i2c_client *client)
 	}
 }
 
+static const struct opt3001_chip_info opt3001_chip_information = {
+	.channels = &opt3001_channels,
+	.chan_type = IIO_LIGHT,
+	.scales = &opt3001_scales,
+	.factor_whole = 10,
+	.factor_integer = 1000,
+	.factor_decimal = 1000,
+	.has_id = true,
+};
+
+static const struct opt3001_chip_info opt3002_chip_information = {
+	.channels = &opt3002_channels,
+	.chan_type = IIO_INTENSITY,
+	.scales = &opt3002_scales,
+	.factor_whole = 12,
+	.factor_integer = 10,
+	.factor_decimal = 100000,
+	.has_id = false,
+};
+
 static const struct i2c_device_id opt3001_id[] = {
-	{ "opt3001" },
+	{ "opt3001", (kernel_ulong_t)&opt3001_chip_information },
+	{ "opt3002", (kernel_ulong_t)&opt3002_chip_information },
 	{ } /* Terminating Entry */
 };
 MODULE_DEVICE_TABLE(i2c, opt3001_id);
 
 static const struct of_device_id opt3001_of_match[] = {
-	{ .compatible = "ti,opt3001" },
+	{ .compatible = "ti,opt3001", .data = &opt3001_chip_information },
+	{ .compatible = "ti,opt3002", .data = &opt3002_chip_information },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, opt3001_of_match);