From: Jonathan Cameron <jic23@kernel.org>
To: Svyatoslav Ryhel <clamor95@gmail.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>,
Thierry Reding <thierry.reding@gmail.com>,
Jonathan Hunter <jonathanh@nvidia.com>,
Javier Carrasco <javier.carrasco.cruz@gmail.com>,
Matti Vaittinen <mazziesaccount@gmail.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Emil Gedenryd <emil.gedenryd@axis.com>,
Arthur Becker <arthur.becker@sentec.com>,
Mudit Sharma <muditsharma.info@gmail.com>,
Per-Daniel Olsson <perdaniel.olsson@axis.com>,
Subhajit Ghosh <subhajit.ghosh@tweaklogic.com>,
Ivan Orlov <ivan.orlov0322@gmail.com>,
David Heidelberg <david@ixit.cz>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-tegra@vger.kernel.org
Subject: Re: [PATCH v2 2/3] iio: light: Add support for AL3000a illuminance sensor
Date: Sun, 16 Feb 2025 14:54:45 +0000 [thread overview]
Message-ID: <20250216145445.1278b6ae@jic23-huawei> (raw)
In-Reply-To: <20250215103159.106343-3-clamor95@gmail.com>
On Sat, 15 Feb 2025 12:31:58 +0200
Svyatoslav Ryhel <clamor95@gmail.com> wrote:
> AL3000a is a simple I2C-based ambient light sensor, which is
> closely related to AL3010 and AL3320a, but has significantly
> different way of processing data generated by the sensor.
>
> Tested-by: Robert Eckelmann <longnoserob@gmail.com>
> Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com>
Hi,
A few really minor comments inline. A nice small driver :)
Jonathan
> diff --git a/drivers/iio/light/al3000a.c b/drivers/iio/light/al3000a.c
> new file mode 100644
> index 000000000000..58d4336dd081
> --- /dev/null
> +++ b/drivers/iio/light/al3000a.c
> @@ -0,0 +1,221 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <linux/array_size.h>
> +#include <linux/bitfield.h>
> +#include <linux/device.h>
> +#include <linux/err.h>
> +#include <linux/i2c.h>
> +#include <linux/mod_devicetable.h>
> +#include <linux/module.h>
> +#include <linux/pm.h>
> +#include <linux/regmap.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/types.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
The iio/sysfs.h header is a bit of a legacy thing. Ideally we will
eventually get rid of it. In this driver, you are correctly not using
anything it provides so drop this include.
> +
> +static const struct iio_chan_spec al3000a_channels[] = {
> + {
> + .type = IIO_LIGHT,
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> + BIT(IIO_CHAN_INFO_SCALE),
As below. I'm a little confused on units, but this may want to just
be BIT(IIO_CHAN_INFO_PROCESSED)
> + },
> +};
> +
> +static int al3000a_set_pwr(struct al3000a_data *data, bool pwr)
> +{
> + struct device *dev = regmap_get_device(data->regmap);
> + u8 val = pwr ? AL3000A_CONFIG_ENABLE : AL3000A_CONFIG_DISABLE;
> + int ret;
> +
> + if (pwr) {
The two flows are different enough I'd split this into power on and power
off functions. Will be a few lines longer but slightly easier to read.
> + ret = regulator_enable(data->vdd_supply);
> + if (ret < 0) {
> + dev_err(dev, "failed to enable vdd power supply\n");
> + return ret;
> + }
> + }
> +
> + ret = regmap_write(data->regmap, AL3000A_REG_SYSTEM, val);
> + if (ret < 0) {
> + dev_err(dev, "failed to write system register\n");
> + return ret;
> + }
> +
> + if (!pwr) {
> + ret = regulator_disable(data->vdd_supply);
> + if (ret < 0) {
> + dev_err(dev, "failed to disable vdd power supply\n");
> + return ret;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static void al3000a_set_pwr_off(void *_data)
> +{
> + struct al3000a_data *data = _data;
> +
> + al3000a_set_pwr(data, false);
> +}
> +
> +static int al3000a_init(struct al3000a_data *data)
> +{
> + int ret;
> +
> + ret = al3000a_set_pwr(data, true);
> + if (ret < 0)
> + return ret;
> +
> + ret = regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_RESET);
> + if (ret < 0)
> + return ret;
> +
> + ret = regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_ENABLE);
regmap functions return <= 0 in all cases. So you can just do if (ret)
or in cases like this save a few lines with
return regmap_write();
> + if (ret < 0)
> + return ret;
> +
> + return 0;
> +}
> +
> +static int al3000a_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan, int *val,
> + int *val2, long mask)
> +{
> + struct al3000a_data *data = iio_priv(indio_dev);
> + int ret, gain;
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + ret = regmap_read(data->regmap, AL3000A_REG_DATA, &gain);
> + if (ret < 0)
> + return ret;
> +
> + *val = lux_table[gain & AL3000A_GAIN_MASK];
I may have misinterpreted the other thread. IS this value in lux?
If it is make this channel IIO_CHAN_INFO_PROCESSED instead.
> +
> + return IIO_VAL_INT;
> + case IIO_CHAN_INFO_SCALE:
> + *val = 1;
Either way, default scale is 1 so this should not be expressed at all.
> +
> + return IIO_VAL_INT;
> + default:
> + return -EINVAL;
> + }
> +}
> +
> +static const struct iio_info al3000a_info = {
> + .read_raw = al3000a_read_raw,
I'd not put a tab in the middle. Single space is fine.
This sort of alignment is a pain for long term maintenance anyway
as we get very noisy patches realigning things.
It makes even less sense with only one item!
> +};
> +
> +static int al3000a_probe(struct i2c_client *client)
> +{
> + struct al3000a_data *data;
> + struct device *dev = &client->dev;
> + struct iio_dev *indio_dev;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + data = iio_priv(indio_dev);
> + i2c_set_clientdata(client, indio_dev);
> +
> + data->regmap = devm_regmap_init_i2c(client, &al3000a_regmap_config);
> + if (IS_ERR(data->regmap))
> + return dev_err_probe(dev, PTR_ERR(data->regmap),
> + "cannot allocate regmap\n");
> +
> + data->vdd_supply = devm_regulator_get(dev, "vdd");
> + if (IS_ERR(data->vdd_supply))
> + return dev_err_probe(dev, PTR_ERR(data->vdd_supply),
> + "failed to get vdd regulator\n");
> +
> + indio_dev->info = &al3000a_info;
> + indio_dev->name = AL3000A_DRV_NAME;
As there is no actual reason why this name should necessarily match the
name of the driver I'd prefer to see the string expressed directly in both
places. That avoids giving the wrong impression as the driver gains support
for additional devices and generally makes things slightly easier to review.
> + indio_dev->channels = al3000a_channels;
> + indio_dev->num_channels = ARRAY_SIZE(al3000a_channels);
> + indio_dev->modes = INDIO_DIRECT_MODE;
> +
> + ret = al3000a_init(data);
> + if (ret < 0)
> + return dev_err_probe(dev, ret, "failed to init ALS\n");
> +
> + ret = devm_add_action_or_reset(dev, al3000a_set_pwr_off, data);
> + if (ret < 0)
> + return dev_err_probe(dev, ret, "failed to add action\n");
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static DEFINE_SIMPLE_DEV_PM_OPS(al3000a_pm_ops, al3000a_suspend, al3000a_resume);
> +
> +static const struct of_device_id al3000a_of_match[] = {
> + { .compatible = "dynaimage,al3000a" },
> + { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(of, al3000a_of_match);
Also provide the i2c_device_id table as it's used for autoprobing of the
driver (long story for why we still need that!)
> +
> +static struct i2c_driver al3000a_driver = {
> + .driver = {
> + .name = AL3000A_DRV_NAME,
> + .of_match_table = al3000a_of_match,
> + .pm = pm_sleep_ptr(&al3000a_pm_ops),
> + },
> + .probe = al3000a_probe,
> +};
> +module_i2c_driver(al3000a_driver);
> +
> +MODULE_AUTHOR("Svyatolsav Ryhel <clamor95@gmail.com>");
> +MODULE_DESCRIPTION("al3000a Ambient Light Sensor driver");
> +MODULE_LICENSE("GPL");
next prev parent reply other threads:[~2025-02-16 14:54 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-15 10:31 [PATCH v2 0/3] iio: light: add al3000a als support Svyatoslav Ryhel
2025-02-15 10:31 ` [PATCH v2 1/3] dt-bindings: iio: light: al3010: add al3000a support Svyatoslav Ryhel
2025-02-16 14:42 ` Jonathan Cameron
2025-02-16 14:44 ` Svyatoslav Ryhel
2025-02-17 14:18 ` Jonathan Cameron
2025-02-15 10:31 ` [PATCH v2 2/3] iio: light: Add support for AL3000a illuminance sensor Svyatoslav Ryhel
2025-02-15 14:12 ` David Heidelberg
2025-02-15 14:22 ` Svyatoslav Ryhel
2025-02-16 14:44 ` Jonathan Cameron
2025-02-16 14:47 ` Svyatoslav Ryhel
2025-02-17 14:20 ` Jonathan Cameron
2025-02-16 14:54 ` Jonathan Cameron [this message]
2025-02-16 15:00 ` Svyatoslav Ryhel
2025-02-17 14:24 ` Jonathan Cameron
2025-02-17 14:32 ` Svyatoslav Ryhel
2025-02-22 12:05 ` Jonathan Cameron
2025-02-22 12:11 ` Svyatoslav Ryhel
2025-02-15 10:31 ` [PATCH v2 3/3] ARM: tegra: tf101: Add al3000a illuminance sensor node Svyatoslav Ryhel
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=20250216145445.1278b6ae@jic23-huawei \
--to=jic23@kernel.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=arthur.becker@sentec.com \
--cc=clamor95@gmail.com \
--cc=conor+dt@kernel.org \
--cc=david@ixit.cz \
--cc=devicetree@vger.kernel.org \
--cc=emil.gedenryd@axis.com \
--cc=ivan.orlov0322@gmail.com \
--cc=javier.carrasco.cruz@gmail.com \
--cc=jonathanh@nvidia.com \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tegra@vger.kernel.org \
--cc=mazziesaccount@gmail.com \
--cc=muditsharma.info@gmail.com \
--cc=perdaniel.olsson@axis.com \
--cc=robh@kernel.org \
--cc=subhajit.ghosh@tweaklogic.com \
--cc=thierry.reding@gmail.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