Interrupt and Continuous mode support for VL6180
v2 cover Interrupt and Continuous mode support for VL6180
Hello,
The first patch adds support for configuring the Sampling frequency (inter-measurement period) of the sensor. The values must be provided in miliseconds. The default value for the inter-measurement period for ALS is 10ms and for Range is 50ms.
The second patch adds support for interrupt based single shot reading. We registered an irq_handler that fires everytime the data is ready. And then we read the appropriate value in the vl6180_measure routine.
The third patch adds support for continuous mode in the sensor by using buffers. We enable the sensor's continuous mode in the buffer_postenable function depending on the active_scan_mask.
The continuous mode can be disabled by disabling the buffer. Added a trigger to the device for the continuous mode. Also validating that the device uses the internal trigger provided by us.
Changes in v2:
- Fixed label followed by a declaration is a C23 extension [-Wc23-extensions] by moving the guard(mutex)(&data->lock) above the switch statement.
- The above error was pointed out during testing by kernel-test-robot
Thanks,
Abhash Jha
Abhash Jha (3):
iio: light: vl6180: Add configurable inter-measurement period support
iio: light: vl6180: Added Interrupt support for single shot access
iio: light: vl6180: Add support for Continuous Mode
drivers/iio/light/vl6180.c | 266 ++++++++++++++++++++++++++++++++++--- 1 file changed, 249 insertions(+), 17 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 iio: light: vl6180: Add configurable inter-measurement period support
Expose the IIO_CHAN_INFO_SAMP_FREQ attribute as a way to configure the inter-measurement period for both the IIO_DISTANCE and IIO_LIGHT channels. The inter-measurement period must be given in miliseconds.
--- drivers/iio/light/vl6180.c | 66 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-)
drivers/iio/light/vl6180.c +64 -2
@@ -38,7 +38,9 @@#define VL6180_OUT_OF_RESET 0x016#define VL6180_HOLD 0x017#define VL6180_RANGE_START 0x018 +#define VL6180_RANGE_INTER_MEAS_TIME 0x01b#define VL6180_ALS_START 0x038 +#define VL6180_ALS_INTER_MEAS_TIME 0x03e#define VL6180_ALS_GAIN 0x03f#define VL6180_ALS_IT 0x040
@@ -86,6 +88,8 @@ struct vl6180_data {struct mutex lock;unsigned int als_gain_milli;unsigned int als_it_ms; +unsigned int als_meas_rate; +unsigned int range_meas_rate; };enum {VL6180_ALS ,VL6180_RANGE ,VL6180_PROX };
@@ -261,12 +265,14 @@ static const struct iio_chan_spec vl6180_channels[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) | - BIT(IIO_CHAN_INFO_HARDWAREGAIN), + BIT(IIO_CHAN_INFO_HARDWAREGAIN) | + BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type = IIO_DISTANCE, .address = VL6180_RANGE, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE), + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type = IIO_PROXIMITY, .address = VL6180_PROX,
@@ -333,6 +339,18 @@ static int vl6180_read_raw(struct iio_dev *indio_dev,return IIO_VAL_FRACTIONAL ; +case IIO_CHAN_INFO_SAMP_FREQ : +switch (chan->type) { +case IIO_DISTANCE : + *val = data->range_meas_rate; +return IIO_VAL_INT ; +case IIO_LIGHT : + *val = data->als_meas_rate; +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +default :return -EINVAL ; }
@@ -412,12 +430,24 @@ static int vl6180_set_it(struct vl6180_data *data, int val, int val2)return ret; } +static int vl6180_meas_reg_val_from_ms (unsigned int period) +{ +unsigned int reg_val =0 ; + +if (period >10 ) + reg_val = period <2550 ? (DIV_ROUND_CLOSEST (period,10 ) -1 ) :254 ; + +return reg_val; +} +static int vl6180_write_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan,int val,int val2,long mask) {struct vl6180_data *data =iio_priv (indio_dev); +unsigned int reg_val; +guard (mutex)(&data->lock);switch (mask) {case IIO_CHAN_INFO_INT_TIME :return vl6180_set_it (data, val, val2);
@@ -427,6 +457,22 @@ static int vl6180_write_raw(struct iio_dev *indio_dev,return -EINVAL ;return vl6180_set_als_gain (data, val, val2); + +case IIO_CHAN_INFO_SAMP_FREQ : +switch (chan->type) { +case IIO_DISTANCE : + data->range_meas_rate = val; + reg_val =vl6180_meas_reg_val_from_ms (val); +return vl6180_write_byte (data->client, +VL6180_RANGE_INTER_MEAS_TIME , reg_val); + +case IIO_LIGHT : + data->als_meas_rate = val; + reg_val =vl6180_meas_reg_val_from_ms (val); +return vl6180_write_byte (data->client, +VL6180_ALS_INTER_MEAS_TIME , reg_val); + } +default :return -EINVAL ; }
@@ -473,6 +519,22 @@ static int vl6180_init(struct vl6180_data *data)if (ret <0 )return ret; +/* Default Range inter-measurement time: 50ms +* reg_val = (50 / 10 - 1) = 4 +*/ + ret =vl6180_write_byte (client,VL6180_RANGE_INTER_MEAS_TIME ,4 ); +if (ret <0 ) +return ret; + data->range_meas_rate =50 ; + +/* Default ALS inter-measurement time: 10ms +* reg_val = (10 / 10 - 1) = 0 +*/ + ret =vl6180_write_byte (client,VL6180_ALS_INTER_MEAS_TIME ,0 ); +if (ret <0 ) +return ret; + data->als_meas_rate =10 ; +/* ALS integration time: 100ms */ data->als_it_ms =100 ; ret =vl6180_write_word (client,VL6180_ALS_IT ,VL6180_ALS_IT_100 );
Signature · 1 line
2.43.0
PATCH v2 2/3 iio: light: vl6180: Added Interrupt support for single shot access
In reply to Abhash JhaConfigured the GPIO1 pin to provide output interrupts. And then the interrupts are serviced in the vl6180_measure function when the irq_handler signals that the reading is complete.
--- drivers/iio/light/vl6180.c | 64 +++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 12 deletions(-)
drivers/iio/light/vl6180.c +52 -12
@@ -33,6 +33,7 @@#define VL6180_MODEL_ID_VAL 0xb4/* Configuration registers */ +#define VL6180_MODE_GPIO1 0x011#define VL6180_INTR_CONFIG 0x014#define VL6180_INTR_CLEAR 0x015#define VL6180_OUT_OF_RESET 0x016
@@ -70,6 +71,9 @@/* bits of the HOLD register */ #define VL6180_HOLD_ON BIT(0) +/* bit for enabling GPIO1 Interrupt Output */ +#define VL6180_GPIO1_INTR_OUT BIT(4) +/* default value for the ALS_IT register */ #define VL6180_ALS_IT_100 0x63/* 100 ms */
@@ -86,6 +90,7 @@struct vl6180_data {struct i2c_client *client;struct mutex lock; +struct completion completion;unsigned int als_gain_milli;unsigned int als_it_ms;unsigned int als_meas_rate;
@@ -211,6 +216,7 @@ static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val)static int vl6180_measure (struct vl6180_data *data,int addr) {struct i2c_client *client = data->client; +unsigned long time_left;int tries =20 , ret;u16 value;
@@ -221,19 +227,26 @@ static int vl6180_measure(struct vl6180_data *data, int addr)if (ret <0 ) goto fail; -while (tries--) { - ret =vl6180_read_byte (client,VL6180_INTR_STATUS ); -if (ret <0 ) - goto fail; - -if (ret & vl6180_chan_regs_table[addr].drdy_mask) -break ; -msleep (20 ); - } +if (client->irq) { +reinit_completion (&data->completion); + time_left =wait_for_completion_timeout (&data->completion,HZ /10 ); +if (time_left ==0 ) +return -ETIMEDOUT ; + }else { +while (tries--) { + ret =vl6180_read_byte (client,VL6180_INTR_STATUS ); +if (ret <0 ) + goto fail; + +if (ret & vl6180_chan_regs_table[addr].drdy_mask) +break ; +msleep (20 ); + } -if (tries <0 ) { - ret = -EIO ; - goto fail; +if (tries <0 ) { + ret = -EIO ; + goto fail; + } }/* Read result value from appropriate registers */
@@ -478,6 +491,15 @@ static int vl6180_write_raw(struct iio_dev *indio_dev, } } +static irqreturn_t vl6180_threaded_irq (int irq,void *priv) +{ +struct iio_dev *indio_dev = priv; +struct vl6180_data *data =iio_priv (indio_dev); + +complete (&data->completion); +return IRQ_HANDLED ; +} +static const struct iio_info vl6180_info = { .read_raw = vl6180_read_raw, .write_raw = vl6180_write_raw,
@@ -513,6 +535,11 @@ static int vl6180_init(struct vl6180_data *data)if (ret !=0x01 )dev_info (&client->dev,"device is not fresh out of reset\n" ); + ret =vl6180_write_byte (client,VL6180_MODE_GPIO1 , +VL6180_GPIO1_INTR_OUT ); +if (ret <0 ) +return ret; +/* Enable ALS and Range ready interrupts */ ret =vl6180_write_byte (client,VL6180_INTR_CONFIG ,VL6180_ALS_READY |VL6180_RANGE_READY );
@@ -579,6 +606,19 @@ static int vl6180_probe(struct i2c_client *client)if (ret <0 )return ret; +if (client->irq) { + ret =devm_request_threaded_irq (&client->dev, client->irq, +NULL , vl6180_threaded_irq, +IRQF_ONESHOT , + indio_dev->name, indio_dev); +if (ret) { +dev_err (&client->dev,"devm_request_irq error: %d\n" , ret); +return ret; + } + +init_completion (&data->completion); + } +return devm_iio_device_register (&client->dev, indio_dev); }
Signature · 1 line
2.43.0
PATCH v2 3/3 iio: light: vl6180: Add support for Continuous Mode
In reply to Abhash JhaAdded support for getting continuous readings from vl6180 using triggered buffer approach. The continuous mode can be enabled by enabling the buffer.
Also added a trigger and appropriate checks to see that it is used with this device.
--- drivers/iio/light/vl6180.c | 138 +++++++++++++++++++++++++++++++++++-- 1 file changed, 134 insertions(+), 4 deletions(-)
drivers/iio/light/vl6180.c +134 -4
@@ -25,6 +25,10 @@#include <linux/iio/iio.h> #include <linux/iio/sysfs.h> +#include <linux/iio/buffer.h> +#include <linux/iio/trigger.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> #define VL6180_DRV_NAME "vl6180"
@@ -91,10 +95,16 @@ struct vl6180_data {struct i2c_client *client;struct mutex lock;struct completion completion; +struct iio_trigger *trig;unsigned int als_gain_milli;unsigned int als_it_ms;unsigned int als_meas_rate;unsigned int range_meas_rate; + +struct { +u16 chan; +aligned_u64 timestamp; + } scan; };enum {VL6180_ALS ,VL6180_RANGE ,VL6180_PROX };
@@ -275,6 +285,12 @@ static const struct iio_chan_spec vl6180_channels[] = { { .type = IIO_LIGHT, .address = VL6180_ALS, + .scan_index = VL6180_ALS, + .scan_type = { + .sign = 'u', + .realbits = 16, + .storagebits = 16, + }, .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) |BIT (IIO_CHAN_INFO_INT_TIME ) | BIT(IIO_CHAN_INFO_SCALE) |
@@ -283,14 +299,27 @@ static const struct iio_chan_spec vl6180_channels[] = { }, { .type = IIO_DISTANCE, .address = VL6180_RANGE, + .scan_index = VL6180_RANGE, + .scan_type = { + .sign = 'u', + .realbits = 8, + .storagebits = 8, + }, .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) |BIT (IIO_CHAN_INFO_SCALE ) | BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type = IIO_PROXIMITY, .address = VL6180_PROX, + .scan_index = VL6180_PROX, + .scan_type = { + .sign = 'u', + .realbits = 16, + .storagebits = 16, + }, .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), - } + }, + IIO_CHAN_SOFT_TIMESTAMP(3), };/*
@@ -496,17 +525,99 @@ static irqreturn_t vl6180_threaded_irq(int irq, void *priv)struct iio_dev *indio_dev = priv;struct vl6180_data *data =iio_priv (indio_dev); -complete (&data->completion); +if (iio_buffer_enabled (indio_dev)) +iio_trigger_poll_nested (indio_dev->trig); +else +complete (&data->completion); +return IRQ_HANDLED ; } +static irqreturn_t vl6180_trigger_handler (int irq,void *priv) +{ +struct iio_poll_func *pf = priv; +struct iio_dev *indio_dev = pf->indio_dev; +struct vl6180_data *data =iio_priv (indio_dev); +int ret; + +for (int i =0 ; i < indio_dev->masklength; i++) { +if (test_bit (i, indio_dev->active_scan_mask)) { + + ret = vl6180_chan_regs_table[i].word ? +vl6180_read_word (data->client, vl6180_chan_regs_table[i].value_reg) : +vl6180_read_byte (data->client, vl6180_chan_regs_table[i].value_reg); +if (ret <0 ) +dev_err (&data->client->dev,"failed to read from value regs: %d\n" , ret); + + data->scan.chan = ret; +iio_push_to_buffers_with_timestamp (indio_dev, &data->scan, +iio_get_time_ns (indio_dev)); + } + } + +iio_trigger_notify_done (indio_dev->trig); + +/* Clear the interrupt flag after data read */ + ret =vl6180_write_byte (data->client,VL6180_INTR_CLEAR , +VL6180_CLEAR_ERROR |VL6180_CLEAR_ALS |VL6180_CLEAR_RANGE ); +if (ret <0 ) +dev_err (&data->client->dev,"failed to clear irq: %d\n" , ret); + +return IRQ_HANDLED ; +} + +static int vl6180_validate_trigger (struct iio_dev *indio_dev,struct iio_trigger *trig) +{ +struct vl6180_data *data =iio_priv (indio_dev); + +return data->trig == trig ?0 : -EINVAL ; +} +static const struct iio_info vl6180_info = { .read_raw = vl6180_read_raw, .write_raw = vl6180_write_raw, .attrs = &vl6180_attribute_group, + .validate_trigger = vl6180_validate_trigger, }; -static int vl6180_init (struct vl6180_data *data) +static int vl6180_buffer_postenable (struct iio_dev *indio_dev) +{ +struct vl6180_data *data =iio_priv (indio_dev); + +for (int i =0 ; i < indio_dev->masklength; i++) { +if (test_bit (i, indio_dev->active_scan_mask)) +return vl6180_write_byte (data->client, + vl6180_chan_regs_table[i].start_reg, +VL6180_MODE_CONT |VL6180_STARTSTOP ); + } + +return -EINVAL ; +} + +static int vl6180_buffer_postdisable (struct iio_dev *indio_dev) +{ +struct vl6180_data *data =iio_priv (indio_dev); + +for (int i =0 ; i < indio_dev->masklength; i++) { +if (test_bit (i, indio_dev->active_scan_mask)) +return vl6180_write_byte (data->client, + vl6180_chan_regs_table[i].start_reg, +VL6180_STARTSTOP ); + } + +return -EINVAL ; +} + +static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { + .postenable = &vl6180_buffer_postenable, + .postdisable = &vl6180_buffer_postdisable, +}; + +static const struct iio_trigger_ops vl6180_trigger_ops = { + .validate_device = iio_trigger_validate_own_device, +}; + +static int vl6180_init (struct vl6180_data *data,struct iio_dev *indio_dev) {struct i2c_client *client = data->client;int ret;
@@ -546,6 +657,12 @@ static int vl6180_init(struct vl6180_data *data)if (ret <0 )return ret; + ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev,NULL , + &vl6180_trigger_handler, + &iio_triggered_buffer_setup_ops); +if (ret) +return ret; +/* Default Range inter-measurement time: 50ms * reg_val = (50 / 10 - 1) = 4 */
@@ -602,7 +719,7 @@ static int vl6180_probe(struct i2c_client *client) indio_dev->name =VL6180_DRV_NAME ; indio_dev->modes =INDIO_DIRECT_MODE ; - ret =vl6180_init (data); + ret =vl6180_init (data, indio_dev);if (ret <0 )return ret;
@@ -617,6 +734,19 @@ static int vl6180_probe(struct i2c_client *client) }init_completion (&data->completion); + + data->trig =devm_iio_trigger_alloc (&client->dev,"%s-dev%d" , + indio_dev->name,iio_device_id (indio_dev)); +if (!data->trig) +return -ENOMEM ; + + data->trig->ops = &vl6180_trigger_ops; +iio_trigger_set_drvdata (data->trig, indio_dev); + ret =devm_iio_trigger_register (&client->dev, data->trig); +if (ret) +return ret; + + indio_dev->trig =iio_trigger_get (data->trig); }return devm_iio_device_register (&client->dev, indio_dev);
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerQuoted 18 lines · On Sat, 5 Oct 2024 21:22:22 +0530 Abhash Jha <[email protected]>…
Hello,
The first patch adds support for configuring the Sampling frequency (inter-measurement period) of the sensor. The values must be provided in miliseconds. The default value for the inter-measurement period for ALS is 10ms and for Range is 50ms.
The second patch adds support for interrupt based single shot reading. We registered an
irq_handlerthat fires everytime the data is ready. And then we read the appropriate value in thevl6180_measureroutine.The third patch adds support for continuous mode in the sensor by using buffers. We enable the sensor's continuous mode in the
buffer_postenablefunction depending on theactive_scan_mask.
The continuous mode can be disabled by disabling the buffer. Added a trigger to the device for the continuous mode. Also validating that the device uses the internal trigger provided by us.
Quoted 8 lines
Changes in v2:
- Fixedlabel followed by a declaration is a C23 extension [-Wc23-extensions]by moving the guard(mutex)(&data->lock) above the switch statement.- The above error was pointed out during testing by kernel-test-robot
Thanks,
Abhash Jha
Hi Abhash,
I left comments on v1. Even for a compile issue like this one, maybe just a quick reply to the earlier posting saying what you will change is more appropriate than sending a new version just one day after the first.
Not a bit thing, so just check those reviews on v1.
Thanks,
Jonathan
Quoted 10 lines
Abhash Jha (3):
iio: light: vl6180: Add configurable inter-measurement period support
iio: light: vl6180: Added Interrupt support for single shot access
iio: light: vl6180: Add support for Continuous Modedrivers/iio/light/vl6180.c | 266 ++++++++++++++++++++++++++++++++++--- 1 file changed, 249 insertions(+), 17 deletions(-)