From: Jonathan Cameron <jic23@kernel.org>
To: Marek Vasut <marex@denx.de>
Cc: linux-iio@vger.kernel.org,
Alexander Stein <alexander.stein@ew.tq-group.com>,
Andre Werner <andre.werner@systec-electronic.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Bjorn Helgaas <bhelgaas@google.com>,
Conor Dooley <conor+dt@kernel.org>,
Fabio Estevam <festevam@denx.de>,
Guenter Roeck <linux@roeck-us.net>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
Lars-Peter Clausen <lars@metafoo.de>,
Luca Ceresoli <luca.ceresoli@bootlin.com>,
Mark Brown <broonie@kernel.org>,
Matti Vaittinen <mazziesaccount@gmail.com>,
Naresh Solanki <naresh.solanki@9elements.com>,
Patrick Rudolph <patrick.rudolph@9elements.com>,
Rob Herring <robh+dt@kernel.org>,
Stefan Windfeldt-Prytz <stefan.windfeldt-prytz@axis.com>,
Vincent Tremblay <vincent@vtremblay.dev>,
devicetree@vger.kernel.org
Subject: Re: [PATCH v4 2/2] iio: light: isl76682: Add ISL76682 driver
Date: Sat, 25 Nov 2023 18:06:31 +0000 [thread overview]
Message-ID: <20231125180631.0dadc926@jic23-huawei> (raw)
In-Reply-To: <20231121031043.327614-2-marex@denx.de>
On Tue, 21 Nov 2023 04:10:40 +0100
Marek Vasut <marex@denx.de> wrote:
> The ISL76682 is very basic ALS which only supports ALS or IR mode
> in four ranges, 1k/4k/16k/64k LUX. There is no IRQ support or any
> other fancy functionality.
>
> Signed-off-by: Marek Vasut <marex@denx.de>
Nice little driver.
A few comments inline.
Thanks,
Jonathan
> diff --git a/drivers/iio/light/isl76682.c b/drivers/iio/light/isl76682.c
> new file mode 100644
> index 0000000000000..7f0ccd0d37539
> --- /dev/null
> +++ b/drivers/iio/light/isl76682.c
> @@ -0,0 +1,364 @@
> +
> +static int isl76682_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
> + struct isl76682_chip *chip = iio_priv(indio_dev);
> + int ret;
> + int i;
> +
> + if (chan->type != IIO_LIGHT && chan->type != IIO_INTENSITY)
> + return -EINVAL;
> +
> + guard(mutex)(&chip->lock);
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + switch (chan->type) {
> + case IIO_LIGHT:
> + ret = isl76682_get(chip, false, val);
> + return (ret < 0) ? ret : IIO_VAL_INT;
> + case IIO_INTENSITY:
> + ret = isl76682_get(chip, true, val);
> + return (ret < 0) ? ret : IIO_VAL_INT;
> + default:
> + break;
return here and drop the one below.
> + }
> +
> + return -EINVAL;
> + case IIO_CHAN_INFO_SCALE:
> + for (i = 0; i < ARRAY_SIZE(isl76682_range_table); i++) {
> + if (chip->range != isl76682_range_table[i].range)
> + continue;
> +
> + *val = 0;
> + switch (chan->type) {
> + case IIO_LIGHT:
> + *val2 = isl76682_range_table[i].als;
> + return IIO_VAL_INT_PLUS_MICRO;
> + case IIO_INTENSITY:
> + *val2 = isl76682_range_table[i].ir;
> + return IIO_VAL_INT_PLUS_MICRO;
> + default:
> + return -EINVAL;
> + }
> + }
> + return -EINVAL;
> + case IIO_CHAN_INFO_INT_TIME:
> + *val = 0;
> + *val2 = ISL76682_INT_TIME_US;
> + return IIO_VAL_INT_PLUS_MICRO;
> + default:
> + return -EINVAL;
> + }
> +}
> +static int isl76682_probe(struct i2c_client *client)
> +{
> + const struct i2c_device_id *id = i2c_client_get_device_id(client);
For reason below, we should avoid accessing this directly.
If you need the associated data later, then use the access functions that
try the property.h accessors first and fallback to this only if this fails.
That ends up much less fragile.
> + struct device *dev = &client->dev;
> + struct isl76682_chip *chip;
> + struct iio_dev *indio_dev;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + chip = iio_priv(indio_dev);
> +
> + mutex_init(&chip->lock);
> +
> + chip->regmap = devm_regmap_init_i2c(client, &isl76682_regmap_config);
> + ret = PTR_ERR_OR_ZERO(chip->regmap);
> + if (ret)
> + return dev_err_probe(dev, ret, "Error initializing regmap\n");
> +
> + chip->range = ISL76682_COMMAND_RANGE_LUX_1K;
> +
> + ret = isl76682_clear_configure_reg(chip);
> + if (ret < 0)
> + return ret;
> +
> + ret = devm_add_action_or_reset(dev, isl76682_reset_action, chip);
> + if (ret)
> + return ret;
> +
> + indio_dev->info = &isl76682_info;
> + indio_dev->channels = isl76682_channels;
> + indio_dev->num_channels = ARRAY_SIZE(isl76682_channels);
> + indio_dev->name = id->name;
This tends to be fragile as drivers gain more entrees in the id and of table.
So the name should always be retrieved from a chip_info structure.
Given you don't need one of those yet, I'd prefer just having the string here
directly. We've been bitten by this before so I get paranoid about it :)
> + indio_dev->modes = INDIO_DIRECT_MODE;
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
prev parent reply other threads:[~2023-11-25 18:06 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-21 3:10 [PATCH v4 1/2] dt-bindings: iio: light: isl76682: Document ISL76682 Marek Vasut
2023-11-21 3:10 ` [PATCH v4 2/2] iio: light: isl76682: Add ISL76682 driver Marek Vasut
2023-11-21 18:09 ` Andy Shevchenko
2023-11-22 0:06 ` Marek Vasut
2023-11-22 12:17 ` Matti Vaittinen
2023-11-23 0:26 ` Marek Vasut
2023-11-23 7:24 ` Matti Vaittinen
2023-12-15 12:06 ` Matti Vaittinen
2023-12-17 13:06 ` Jonathan Cameron
2023-12-18 9:36 ` Matti Vaittinen
2023-11-25 18:06 ` 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=20231125180631.0dadc926@jic23-huawei \
--to=jic23@kernel.org \
--cc=alexander.stein@ew.tq-group.com \
--cc=andre.werner@systec-electronic.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=bhelgaas@google.com \
--cc=broonie@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=festevam@denx.de \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=luca.ceresoli@bootlin.com \
--cc=marex@denx.de \
--cc=mazziesaccount@gmail.com \
--cc=naresh.solanki@9elements.com \
--cc=patrick.rudolph@9elements.com \
--cc=robh+dt@kernel.org \
--cc=stefan.windfeldt-prytz@axis.com \
--cc=vincent@vtremblay.dev \
/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