Re: [PATCH v3 3/5] proximity: vl53l0x: Handle the VDD regulator

From: Jonathan Cameron
Date: Thu May 12 2022 - 12:55:34 EST


On Thu, 12 May 2022 14:07:55 +0300
Markuss Broks <markuss.broks@xxxxxxxxx> wrote:

> Handle the regulator supplying the VDD pin of VL53L0X.
>
> Signed-off-by: Markuss Broks <markuss.broks@xxxxxxxxx>
Hi Markuss,

One ordering question inline.

> ---
> drivers/iio/proximity/vl53l0x-i2c.c | 37 +++++++++++++++++++++++++++++
> 1 file changed, 37 insertions(+)
>
> diff --git a/drivers/iio/proximity/vl53l0x-i2c.c b/drivers/iio/proximity/vl53l0x-i2c.c
> index 12a3e2eff464..d8523e3981e8 100644
> --- a/drivers/iio/proximity/vl53l0x-i2c.c
> +++ b/drivers/iio/proximity/vl53l0x-i2c.c
> @@ -43,6 +43,7 @@
> struct vl53l0x_data {
> struct i2c_client *client;
> struct completion completion;
> + struct regulator *vdd_supply;
> };
>
> static irqreturn_t vl53l0x_handle_irq(int irq, void *priv)
> @@ -192,10 +193,31 @@ static const struct iio_info vl53l0x_info = {
> .read_raw = vl53l0x_read_raw,
> };
>
> +static void vl53l0x_power_off(void *_data)
> +{
> + struct vl53l0x_data *data = _data;
> +
> + regulator_disable(data->vdd_supply);
> +}
> +
> +static int vl53l0x_power_on(struct vl53l0x_data *data)
> +{
> + int ret;
> +
> + ret = regulator_enable(data->vdd_supply);
> + if (ret)
> + return ret;
> +
> + usleep_range(3200, 5000);
> +
> + return 0;
> +}
> +
> static int vl53l0x_probe(struct i2c_client *client)
> {
> struct vl53l0x_data *data;
> struct iio_dev *indio_dev;
> + int error;
>
> indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> if (!indio_dev)
> @@ -210,6 +232,21 @@ static int vl53l0x_probe(struct i2c_client *client)
> I2C_FUNC_SMBUS_BYTE_DATA))
> return -EOPNOTSUPP;
>
> + data->vdd_supply = devm_regulator_get_optional(&client->dev, "vdd");
> + if (IS_ERR(data->vdd_supply))
> + return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply),
> + "Unable to get VDD regulator\n");
> +
> + error = devm_add_action_or_reset(&client->dev, vl53l0x_power_off, data);

I don't follow why you have this before the power_on. We haven't enabled the
regulator yet so shouldn't be turning it off if we get an error whilst trying
to enable it. The or_reset part is to ensure that even if this call
fails to register a devm action it will still call the callback thus allowing
you safely do this after turning the power on.

> + if (error)
> + return dev_err_probe(&client->dev, error,
> + "Failed to install poweroff action\n");
> +
> + error = vl53l0x_power_on(data);
> + if (error)
> + return dev_err_probe(&client->dev, error,
> + "Failed to power on the chip\n");
> +
> indio_dev->name = "vl53l0x";
> indio_dev->info = &vl53l0x_info;
> indio_dev->channels = vl53l0x_channels;