From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Liu Yufei <lyf98405@gmail.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] iio: proximity: Add driver for Vishay VCNL36829
Date: Sat, 3 Oct 2026 22:03:36 +0300 [thread overview]
Message-ID: <asFRiC-RniYbQAKH@ashevche-desk.local> (raw)
In-Reply-To: <20261003050453.413700-3-lyf98405@gmail.com>
On Sat, Oct 03, 2026 at 01:04:53PM +0800, Liu Yufei wrote:
> Add an IIO driver for the Vishay VCNL36829 proximity sensor with
> integrated VCSEL, connected over I2C.
>
> At probe the driver enables the vdd and vddio supplies, waits for the
> sensor to power up, checks the device ID, configures the VCSEL current
> and photodiodes, and turns the proximity engine on. The sensor is put
> back into shutdown when the device is removed.
>
> The following attributes are exposed:
> - in_proximity0_raw
> - in_proximity_integration_time
> - in_proximity_integration_time_available
> The datasheet is available at:
> https://www.vishay.com/docs/80580/vcnl36829um.pdf
Make it a Datasheet: tag.
> Assisted-by: Claude:claude-opus-5-5 checkpatch dtschema
Assisted-by: LLM
...
> +#include <linux/i2c.h>
> +#include <linux/mod_devicetable.h>
In the new code we rely on the bus header to provide the ID definitions.
> +#include <linux/iio/iio.h>
> +#include <linux/regmap.h>
> +#include <linux/bitfield.h>
> +#include <linux/module.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/delay.h>
Keep it sorted, also follow the IWYU principle.
...
> +struct vcnl36829_data {
> + struct i2c_client *client;
> + struct regmap *regmap;
One can be derived through the other, no need to keep client here.
> +};
> +
> +static const struct regmap_config vcnl36829_regmap_config = {
> + .name = "vcnl36829_regmap",
> + .reg_bits = 8,
> + .val_bits = 16,
> + .max_register = VCNL36829_DEV_ID,
> + .val_format_endian = REGMAP_ENDIAN_LITTLE,
No cache?
> +};
...
> +static int vcnl36829_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan, int val,
> + int val2, long mask)
> +{
> + struct vcnl36829_data *data = iio_priv(indio_dev);
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_INT_TIME:
> + if (val != 0)
> + return -EINVAL;
> + if (val2 >= 25 && val2 <= 375 && val2 % 25 == 0)
> + return regmap_update_bits(data->regmap,
> + VCNL36829_PS_CONF2,
> + VCNL36829_PS_IT_MSK,
> + FIELD_PREP(VCNL36829_PS_IT_MSK, val2 / 25));
Compiler might utilise some specific assembler instructions on some CPUs
to do % and / at once. To make it happen we can hint compiler with locating
these two operations next to each other. Also use standard pattern to check
for an error first and correct indentation.
int ival = val2 / 25;
int fval = val2 % 25;
...
if (val != 0)
return -EINVAL;
if (ival < 1 && ival > 15)
return -EINVAL;
if (fval != 0)
return -EINVAL;
return regmap_update_bits(data->regmap, VCNL36829_PS_CONF2,
VCNL36829_PS_IT_MSK,
FIELD_PREP(VCNL36829_PS_IT_MSK, ival));
> + return -EINVAL;
> + default:
> + return -EINVAL;
> + }
> +}
...
> +static int vcnl36829_probe(struct i2c_client *client)
> +{
> + struct device *dev = &client->dev;
> + struct vcnl36829_data *data;
> + struct iio_dev *indio_dev;
> + struct regmap *regmap;
> +
> + int ret;
> + unsigned int reg;
No blank lines in the definition block, also keep it in reversed xmas tree
order.
> + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
> + return dev_err_probe(dev, -EOPNOTSUPP,
> + "I2C adapter doesn't support plain I2C\n");
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + regmap = devm_regmap_init_i2c(client, &vcnl36829_regmap_config);
> + if (IS_ERR(regmap))
> + return dev_err_probe(dev, PTR_ERR(regmap),
> + "Regmap setup failed\n");
> +
> + data = iio_priv(indio_dev);
> + i2c_set_clientdata(client, indio_dev);
Is this being used?
> + data->client = client;
> + data->regmap = regmap;
> +
> + indio_dev->name = "vcnl36829";
> + indio_dev->info = &vcnl36829_info;
> + indio_dev->channels = vcnl36829_channels;
> + indio_dev->num_channels = ARRAY_SIZE(vcnl36829_channels);
> + indio_dev->modes = INDIO_DIRECT_MODE;
> +
> + ret = devm_regulator_get_enable(dev, "vdd");
> + if (ret)
> + return dev_err_probe(dev, ret, "failed to enable vdd\n");
> +
> + ret = devm_regulator_get_enable(dev, "vddio");
> + if (ret)
> + return dev_err_probe(dev, ret, "failed to enable vddio\n");
The following sleep needs a comment with a reference to the specific section
and/or table in the datasheet.
> + fsleep(3000);
3 * USEC_PER_MSEC
(will require time.h to be included)
> + ret = regmap_read(regmap, VCNL36829_DEV_ID, ®);
> + if (ret)
> + return dev_err_probe(dev, ret, "Failed to read device ID\n");
> + if ((reg & 0xFF) != VCNL36829_DEV_ID_VAL)
> + return dev_err_probe(dev, -ENODEV,
> + "Unknown device ID 0x%x\n", reg);
Don't we simply warn and continue in this case?
> + ret = regmap_update_bits(regmap, VCNL36829_PS_CONF2,
> + VCNL36829_PS_IT_MSK,
> + FIELD_PREP(VCNL36829_PS_IT_MSK, VCNL36829_PS_INT_TIME_25));
> + if (ret)
> + return dev_err_probe(dev, ret,
> + "Could not configure PS_IT\n");
It's perfectly a single line.
> +
> + ret = regmap_update_bits(regmap, VCNL36829_PS_CONF3,
> + VCNL36829_PS_CURRENT_EN_MSK |
> + VCNL36829_PS_CURRENT_MSK |
> + VCNL36829_PD1_EN_MSK |
> + VCNL36829_PD2_EN_MSK |
> + VCNL36829_PD3_EN_MSK,
> + FIELD_PREP(VCNL36829_PS_CURRENT_EN_MSK, 1) |
> + FIELD_PREP(VCNL36829_PS_CURRENT_MSK, VCNL36829_PS_CURRENT_18MA) |
> + FIELD_PREP(VCNL36829_PD1_EN_MSK, 1) |
> + FIELD_PREP(VCNL36829_PD2_EN_MSK, 1) |
> + FIELD_PREP(VCNL36829_PD3_EN_MSK, 1));
> + if (ret)
> + return dev_err_probe(dev, ret,
> + "Could not configure VCSEL/PD\n");
> +
> + ret = regmap_write(regmap, VCNL36829_PS_CONF5, VCNL36829_PS_CONF5_INIT);
> + if (ret)
> + return dev_err_probe(dev, ret, "Could not set PS_CONF5\n");
> +
> + ret = regmap_update_bits(regmap, VCNL36829_PS_CONF1,
> + VCNL36829_PS_ON_MSK | VCNL36829_PS_SD_MSK,
> + FIELD_PREP(VCNL36829_PS_ON_MSK, 1) |
> + FIELD_PREP(VCNL36829_PS_SD_MSK, 0));
> + if (ret)
> + return dev_err_probe(dev, ret,
> + "Could not set initial config\n");
> +
> + ret = devm_add_action_or_reset(dev, vcnl36829_shutdown_action, data);
> + if (ret)
> + return ret;
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
--
With Best Regards,
Andy Shevchenko
prev parent reply other threads:[~2026-10-03 19:03 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-03 5:04 [PATCH 0/2] iio: proximity: Add Vishay VCNL36829 driver Liu Yufei
2026-10-03 5:04 ` [PATCH 1/2] dt-bindings: iio: proximity: Add Vishay VCNL36829 Liu Yufei
2026-10-03 5:04 ` [PATCH 2/2] iio: proximity: Add driver for " Liu Yufei
2026-10-03 19:03 ` Andy Shevchenko [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=asFRiC-RniYbQAKH@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lyf98405@gmail.com \
--cc=nuno.sa@analog.com \
--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
all inboxes | Powered by JetHome®