From: Abhash Jha <abhashkumarjha123@gmail.com>
To: linux-iio@vger.kernel.org
Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org,
Abhash Jha <abhashkumarjha123@gmail.com>
Subject: [PATCH 1/3] iio: light: vl6180: Add configurable inter-measurement period support
Date: Fri, 4 Oct 2024 20:31:46 +0530 [thread overview]
Message-ID: <20241004150148.14033-2-abhashkumarjha123@gmail.com> (raw)
In-Reply-To: <20241004150148.14033-1-abhashkumarjha123@gmail.com>
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.
Signed-off-by: Abhash Jha <abhashkumarjha123@gmail.com>
---
drivers/iio/light/vl6180.c | 67 ++++++++++++++++++++++++++++++++++++--
1 file changed, 65 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c
index a1b2b3c0b..52a837644 100644
--- a/drivers/iio/light/vl6180.c
+++ b/drivers/iio/light/vl6180.c
@@ -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,11 +430,22 @@ 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;
switch (mask) {
case IIO_CHAN_INFO_INT_TIME:
@@ -427,6 +456,24 @@ 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:
+ guard(mutex)(&data->lock);
+ 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;
+ }
+
default:
return -EINVAL;
}
@@ -473,6 +520,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);
--
2.43.0
next prev parent reply other threads:[~2024-10-04 15:02 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-04 15:01 [PATCH 0/3] Interrupt and Continuous mode support for VL6180 Abhash Jha
2024-10-04 15:01 ` Abhash Jha [this message]
2024-10-05 12:25 ` [PATCH 1/3] iio: light: vl6180: Add configurable inter-measurement period support kernel test robot
2024-10-05 16:41 ` Jonathan Cameron
2024-10-05 16:56 ` Abhash jha
2024-10-05 17:03 ` Jonathan Cameron
2024-10-04 15:01 ` [PATCH 2/3] iio: light: vl6180: Added Interrupt support for single shot access Abhash Jha
2024-10-05 16:47 ` Jonathan Cameron
2024-10-05 17:35 ` Abhash jha
2024-10-05 18:08 ` Jonathan Cameron
2024-10-04 15:01 ` [PATCH 3/3] iio: light: vl6180: Add support for Continuous Mode Abhash Jha
2024-10-05 16:59 ` Jonathan Cameron
2024-10-05 17:12 ` Abhash jha
2024-10-05 18:15 ` Jonathan Cameron
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=20241004150148.14033-2-abhashkumarjha123@gmail.com \
--to=abhashkumarjha123@gmail.com \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.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