devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Emil Gedenryd <emil.gedenryd@axis.com>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Andreas Dannenberg <dannenberg@ti.com>,
	<linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<devicetree@vger.kernel.org>, <kernel@axis.com>
Subject: Re: [PATCH v3 3/3] iio: light: opt3001: add support for TI's opt3002 light sensor
Date: Sat, 28 Sep 2024 17:05:20 +0100	[thread overview]
Message-ID: <20240928170520.02a27690@jic23-huawei> (raw)
In-Reply-To: <20240916-add_opt3002-v3-3-984b190cd68c@axis.com>

On Mon, 16 Sep 2024 16:56:39 +0200
Emil Gedenryd <emil.gedenryd@axis.com> 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
> Signed-off-by: Emil Gedenryd <emil.gedenryd@axis.com>
Hi Emil,

A few things inline,

Thanks,

Jonathan

> diff --git a/drivers/iio/light/opt3001.c b/drivers/iio/light/opt3001.c
> index 176e54bb48c3..5e3fe42c5b59 100644
> --- a/drivers/iio/light/opt3001.c
> +++ b/drivers/iio/light/opt3001.c


> @@ -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) {
> @@ -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.

>  		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.

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

>  	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);
> 


  reply	other threads:[~2024-09-28 16:05 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-16 14:56 [PATCH v3 0/3] iio: light: opt3001: add support for TI's opt3002 light sensor Emil Gedenryd
2024-09-16 14:56 ` [PATCH v3 1/3] iio: light: opt3001: add missing full-scale range value Emil Gedenryd
2024-09-28 15:57   ` Jonathan Cameron
2024-09-30  7:54     ` Emil Gedenryd
2024-09-16 14:56 ` [PATCH v3 2/3] dt-bindings: iio: light: opt3001: add compatible for opt3002 Emil Gedenryd
2024-09-16 15:24   ` Conor Dooley
2024-09-17  7:06     ` Emil Gedenryd
2024-09-16 14:56 ` [PATCH v3 3/3] iio: light: opt3001: add support for TI's opt3002 light sensor Emil Gedenryd
2024-09-28 16:05   ` Jonathan Cameron [this message]
2024-09-30  7:53     ` Emil Gedenryd

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=20240928170520.02a27690@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=dannenberg@ti.com \
    --cc=devicetree@vger.kernel.org \
    --cc=emil.gedenryd@axis.com \
    --cc=kernel@axis.com \
    --cc=krzk+dt@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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;
as well as URLs for NNTP newsgroup(s).