Re: [PATCH 2/2] iio: proximity: Add driver for Vishay VCNL36829

From: Andy Shevchenko

Date: Sat Oct 03 2026 - 15:03:47 EST


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, &reg);
> + 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