Re: [PATCH v4 2/2] iio: proximity: vl53l0x-i2c: Added continuous mode support

From: Jonathan Cameron
Date: Sat Sep 14 2024 - 09:35:15 EST


On Mon, 9 Sep 2024 15:45:07 +0530
Abhash Jha <abhashkumarjha123@xxxxxxxxx> wrote:

> The continuous mode of the sensor is enabled in the buffer_postenable.
> Replaced the original irq handler with a threaded irq handler to perform
> i2c reads during continuous mode.
> The continuous mode is disabled by disabling the buffer.
> Added a trigger for this device to be used for continuous mode.
>
> Signed-off-by: Abhash Jha <abhashkumarjha123@xxxxxxxxx>
Hi Abhash,

Applied this with a couple of minor tweaks (see below) to the
testing branch of iio.git. I'll be rebasing that tree on rc1 once
available and pushing out as togreg which gets picked up by linux-next.

Thanks,

Jonathan

> ---
> drivers/iio/proximity/vl53l0x-i2c.c | 161 +++++++++++++++++++++++-----
> 1 file changed, 135 insertions(+), 26 deletions(-)
>
> diff --git a/drivers/iio/proximity/vl53l0x-i2c.c b/drivers/iio/proximity/vl53l0x-i2c.c
> index 3f416d3db..cbf030869 100644
> --- a/drivers/iio/proximity/vl53l0x-i2c.c
> +++ b/drivers/iio/proximity/vl53l0x-i2c.c
> @@ -22,6 +22,12 @@
> #include <linux/module.h>
>
> #include <linux/iio/iio.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/triggered_buffer.h>
> +
> +#include <asm/unaligned.h>
>
> #define VL_REG_SYSRANGE_START 0x00
>
> @@ -43,20 +49,70 @@
> #define VL_REG_RESULT_RANGE_STATUS_COMPLETE BIT(0)
>
> #define VL53L0X_MODEL_ID_VAL 0xEE
> +#define VL53L0X_CONTINUOUS_MODE 0x02
> +#define VL53L0X_SINGLE_MODE 0x01
>
> struct vl53l0x_data {
> struct i2c_client *client;
> struct completion completion;
> struct regulator *vdd_supply;
> struct gpio_desc *reset_gpio;
> + struct iio_trigger *trig;
> +
> + struct {
> + u16 chan;
> + s64 timestamp __aligned(8);
I tweak this whilst applying to use the new aligned_s64
(the patch crossed with yours)

> + } scan;
> };
>
>
> @@ -153,7 +192,7 @@ static int vl53l0x_read_proximity(struct vl53l0x_data *data,
> return -EREMOTEIO;
>
> /* Values should be between 30~1200 in millimeters. */
> - *val = (buffer[10] << 8) + buffer[11];
> + *val = get_unaligned_be16(&buffer[10]);

In theory this should have been a different patch, but meh it's tiny so
I'll just take it in here.

>
> return 0;
> }
> @@ -163,7 +202,14 @@ static const struct iio_chan_spec vl53l0x_channels[] = {
> .type = IIO_DISTANCE,
> .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> BIT(IIO_CHAN_INFO_SCALE),
> + .scan_index = 0,
> + .scan_type = {
> + .sign = 'u',
> + .realbits = 12,
> + .storagebits = 16,
> + },
> },
> + IIO_CHAN_SOFT_TIMESTAMP(1),
> };
>
> static int vl53l0x_read_raw(struct iio_dev *indio_dev,
> @@ -193,8 +239,16 @@ static int vl53l0x_read_raw(struct iio_dev *indio_dev,
> }
> }
>
> +static int vl53l0x_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
> +{
> + struct vl53l0x_data *data = iio_priv(indio_dev);
> +
> + return data->trig == trig ? 0 : -EINVAL;
> +}
> +
> static const struct iio_info vl53l0x_info = {
> .read_raw = vl53l0x_read_raw,
> + .validate_trigger = vl53l0x_validate_trigger,
> };
>
> static void vl53l0x_power_off(void *_data)
> @@ -221,6 +275,39 @@ static int vl53l0x_power_on(struct vl53l0x_data *data)
> return 0;
> }
>
> +static int vl53l0x_buffer_postenable(struct iio_dev *indio_dev)
> +{
> + struct vl53l0x_data *data = iio_priv(indio_dev);
> +
> + return i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START,
> + VL53L0X_CONTINUOUS_MODE);
> +}
> +
> +static int vl53l0x_buffer_postdisable(struct iio_dev *indio_dev)
> +{
> + struct vl53l0x_data *data = iio_priv(indio_dev);
> + int ret;
> +
> + ret = i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START,
> + VL53L0X_SINGLE_MODE);
> + if (ret < 0)
> + return ret;
> +
> + /* Let the ongoing reading finish */
> + reinit_completion(&data->completion);
> + wait_for_completion_timeout(&data->completion, HZ / 10);
Trivial but I'll add a blank line here to separate the completion related
bits from the clear irq as they are more or less unrelated.

> + return vl53l0x_clear_irq(data);
> +}