2023-05-30 14:42:57

by Astrid Rost

[permalink] [raw]
Subject: [PATCH v5 0/7] iio: light: vcnl4000: Add features for vncl4040/4200

Add a more complete support for vncl4040 and vcnl4200, which allows to
change the distance of proximity detection and interrupt support for the
illuminance sensor.

Proximity functionality:
- Interrupt support (new on vcnl4200).

Proximity reduce the amount of interrupts:
- Adaptable integration time (new on vcnl4200) - the sampling rate
changes according to this value.
- Period - interrupt is asserted if the value is above or
below a certain threshold.

Proximity change the activity distance:
- Oversampling ratio - Amount of LED pulses per measured raw value.
- Calibration bias - LED current calibration of the sensor.

Illuminance functionality:
- Interrupt support.

Illuminance reduce the amount of interrupts:
- Adaptable integration time - the sampling rate and scale changes
according to this value.
- Period – interrupt is asserted if the value is above or
below a certain threshold.

changes v2:
- [PATCH v2 3/7] Fixed calculation of al_scale.
Fix the value of vcnl4040 according to the data-sheet.
Use div_u64 for the division.
scription for the branch

changes v3:
- [PATCH v3 1-3/7] Add differences between the chips as variables in
chip-spec.
- [PATCH v3 4/7] Changed commit message.
- [PATCH v3 5/7] Use period instead of debounce time. This causes some
calculations as the period is a time and the chip allows to set a certain
amount of measurements above/below the threshold, before throwing an
interrupt.
- [PATCH v3 6/7] Changed commit message.

changes v4:
- [PATCH v3 1-3/7] Fix setting correct als_it for vcnl4040.
- [PATCH v3 5/7] Use MICRO macro.
Fix values greater than 1 s for the proximity period.

changes v5:
[PATCH v5 2/7]:
- Calculate ps_it from ps_it_times by usinh NSEC_PER_USEC.
[PATCH v5 3/7]:
- Calculate als_it from ps_it_times by using NSEC_PER_USEC.
- Store scale step factor in chip_spec.
- Fixes sampling_rate to ns + 20 %.
[PATCH v5 3/7 - 7/7]
- Changed formatting.
- Changed some variable names.

Astrid Rost (7):
[PATCH v4 1/7] iio: light: vcnl4000: Add proximity irq for vcnl4200
[PATCH v4 2/7] iio: light: vcnl4000: Add proximity ps_it for vcnl4200
[PATCH v4 3/7] iio: light: vcnl4000: Add als_it for vcnl4040/4200
[PATCH v4 4/7] iio: light: vcnl4000: add illuminance irq vcnl4040/4200
[PATCH v4 5/7] iio: light: vcnl4000: Add period for vcnl4040/4200
[PATCH v4 6/7] iio: light: vcnl4000: Add oversampling_ratio for 4040/4200
[PATCH v4 7/7] iio: light: vcnl4000: Add calibration bias for 4040/4200

drivers/iio/light/vcnl4000.c | 727 +++++++++++++++++++++++++++++++----
1 file changed, 657 insertions(+), 70 deletions(-)

--
2.30.2



2023-05-30 14:44:11

by Astrid Rost

[permalink] [raw]
Subject: [PATCH v5 2/7] iio: light: vcnl4000: Add proximity ps_it for vcnl4200

Add ps_it attributes for vcnl4200 (similar to vcnl4040).
Add read/write attribute for proximity integration time.
Read attribute for available proximity integration times.
Change sampling rate depending on integration time.

Signed-off-by: Astrid Rost <[email protected]>
---
drivers/iio/light/vcnl4000.c | 33 ++++++++++++++++++++++++++-------
1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 565b9c9ea470..090e20229855 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -124,6 +124,15 @@ static const int vcnl4040_ps_it_times[][2] = {
{0, 800},
};

+static const int vcnl4200_ps_it_times[][2] = {
+ {0, 96},
+ {0, 144},
+ {0, 192},
+ {0, 384},
+ {0, 768},
+ {0, 864},
+};
+
#define VCNL4000_SLEEP_DELAY_MS 2000 /* before we enter pm_runtime_suspend */

enum vcnl4000_device_ids {
@@ -166,6 +175,8 @@ struct vcnl4000_chip_spec {
irqreturn_t (*irq_thread)(int irq, void *priv);
irqreturn_t (*trig_buffer_func)(int irq, void *priv);
u8 int_reg;
+ const int(*ps_it_times)[][2];
+ const int num_ps_it_times;
};

static const struct i2c_device_id vcnl4000_id[] = {
@@ -508,11 +519,11 @@ static int vcnl4040_read_ps_it(struct vcnl4000_data *data, int *val, int *val2)

ret = FIELD_GET(VCNL4040_PS_CONF2_PS_IT, ret);

- if (ret >= ARRAY_SIZE(vcnl4040_ps_it_times))
+ if (ret >= data->chip_spec->num_ps_it_times)
return -EINVAL;

- *val = vcnl4040_ps_it_times[ret][0];
- *val2 = vcnl4040_ps_it_times[ret][1];
+ *val = (*data->chip_spec->ps_it_times)[ret][0];
+ *val2 = (*data->chip_spec->ps_it_times)[ret][1];

return 0;
}
@@ -523,8 +534,8 @@ static ssize_t vcnl4040_write_ps_it(struct vcnl4000_data *data, int val)
int ret, index = -1;
u16 regval;

- for (i = 0; i < ARRAY_SIZE(vcnl4040_ps_it_times); i++) {
- if (val == vcnl4040_ps_it_times[i][1]) {
+ for (i = 0; i < data->chip_spec->num_ps_it_times; i++) {
+ if (val == (*data->chip_spec->ps_it_times)[i][1]) {
index = i;
break;
}
@@ -533,6 +544,8 @@ static ssize_t vcnl4040_write_ps_it(struct vcnl4000_data *data, int val)
if (index < 0)
return -EINVAL;

+ data->vcnl4200_ps.sampling_rate = ktime_set(0, val * 60 * NSEC_PER_USEC);
+
mutex_lock(&data->vcnl4000_lock);

ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
@@ -620,11 +633,13 @@ static int vcnl4040_read_avail(struct iio_dev *indio_dev,
const int **vals, int *type, int *length,
long mask)
{
+ struct vcnl4000_data *data = iio_priv(indio_dev);
+
switch (mask) {
case IIO_CHAN_INFO_INT_TIME:
- *vals = (int *)vcnl4040_ps_it_times;
+ *vals = (int *)(*data->chip_spec->ps_it_times);
*type = IIO_VAL_INT_PLUS_MICRO;
- *length = 2 * ARRAY_SIZE(vcnl4040_ps_it_times);
+ *length = 2 * data->chip_spec->num_ps_it_times;
return IIO_AVAIL_LIST;
default:
return -EINVAL;
@@ -1317,6 +1332,8 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
.info = &vcnl4040_info,
.irq_thread = vcnl4040_irq_thread,
.int_reg = VCNL4040_INT_FLAGS,
+ .ps_it_times = &vcnl4040_ps_it_times,
+ .num_ps_it_times = ARRAY_SIZE(vcnl4040_ps_it_times),
},
[VCNL4200] = {
.prod = "VCNL4200",
@@ -1329,6 +1346,8 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
.info = &vcnl4040_info,
.irq_thread = vcnl4040_irq_thread,
.int_reg = VCNL4200_INT_FLAGS,
+ .ps_it_times = &vcnl4200_ps_it_times,
+ .num_ps_it_times = ARRAY_SIZE(vcnl4200_ps_it_times),
},
};

--
2.30.2


2023-05-30 14:45:04

by Astrid Rost

[permalink] [raw]
Subject: [PATCH v5 6/7] iio: light: vcnl4000: Add oversampling_ratio for 4040/4200

Add the proximity multi pulse (PS_MPS) as oversampling_ratio.
Instead of one single pulse per every defined time frame, one can
program2, 4, or even 8 pulses. This leads to a longer IRED on-time
for each proximity measurement value, which also results in a higher
detection range.

Add read/write attribute for proximity oversampling-ratio and read
attribute for available oversampling-ratio.
This is supported for vcnl4040 and vcnl4200.

Signed-off-by: Astrid Rost <[email protected]>
---
drivers/iio/light/vcnl4000.c | 86 +++++++++++++++++++++++++++++++++++-
1 file changed, 84 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 3ae137bcd51a..7a340c6f518f 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -61,6 +61,7 @@

#define VCNL4200_AL_CONF 0x00 /* Ambient light configuration */
#define VCNL4200_PS_CONF1 0x03 /* Proximity configuration */
+#define VCNL4200_PS_CONF3 0x04 /* Proximity configuration */
#define VCNL4040_PS_THDL_LM 0x06 /* Proximity threshold low */
#define VCNL4040_PS_THDH_LM 0x07 /* Proximity threshold high */
#define VCNL4040_ALS_THDL_LM 0x02 /* Ambient light threshold low */
@@ -90,6 +91,7 @@
#define VCNL4040_PS_CONF2_PS_IT GENMASK(3, 1) /* Proximity integration time */
#define VCNL4040_CONF1_PS_PERS GENMASK(5, 4) /* Proximity interrupt persistence setting */
#define VCNL4040_PS_CONF2_PS_INT GENMASK(9, 8) /* Proximity interrupt mode */
+#define VCNL4040_PS_CONF3_MPS GENMASK(6, 5) /* Proximity multi pulse number */
#define VCNL4040_PS_IF_AWAY BIT(8) /* Proximity event cross low threshold */
#define VCNL4040_PS_IF_CLOSE BIT(9) /* Proximity event cross high threshold */
#define VCNL4040_ALS_RISING BIT(12) /* Ambient Light cross high threshold */
@@ -158,6 +160,7 @@ static const int vcnl4200_als_it_times[][2] = {

static const int vcnl4040_als_persistence[] = {1, 2, 4, 8};
static const int vcnl4040_ps_persistence[] = {1, 2, 3, 4};
+static const int vcnl4040_ps_oversampling_ratio[] = {1, 2, 4, 8};

#define VCNL4000_SLEEP_DELAY_MS 2000 /* before we enter pm_runtime_suspend */

@@ -777,6 +780,56 @@ static ssize_t vcnl4040_write_ps_period(struct vcnl4000_data *data, int val, int
return ret;
}

+static ssize_t vcnl4040_read_ps_oversampling_ratio(struct vcnl4000_data *data, int *val)
+{
+ int ret;
+
+ ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF3);
+ if (ret < 0)
+ return ret;
+
+ ret = FIELD_GET(VCNL4040_PS_CONF3_MPS, ret);
+
+ if (ret >= ARRAY_SIZE(vcnl4040_ps_oversampling_ratio))
+ return -EINVAL;
+
+ *val = vcnl4040_ps_oversampling_ratio[ret];
+
+ return ret;
+}
+
+static ssize_t vcnl4040_write_ps_oversampling_ratio(struct vcnl4000_data *data, int val)
+{
+ unsigned int i;
+ int ret, reg_val = -1;
+ u16 regval;
+
+ for (i = 0; i < ARRAY_SIZE(vcnl4040_ps_oversampling_ratio); i++) {
+ if (val == vcnl4040_ps_oversampling_ratio[i]) {
+ reg_val = i;
+ break;
+ }
+ }
+
+ if (reg_val < 0)
+ return -EINVAL;
+
+ mutex_lock(&data->vcnl4000_lock);
+
+ ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF3);
+ if (ret < 0)
+ goto out_unlock;
+
+ regval = (ret & ~VCNL4040_PS_CONF3_MPS) |
+ FIELD_PREP(VCNL4040_PS_CONF3_MPS, reg_val);
+ ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF3,
+ regval);
+
+out_unlock:
+ mutex_unlock(&data->vcnl4000_lock);
+ return ret;
+}
+
static int vcnl4000_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
@@ -827,6 +880,16 @@ static int vcnl4000_read_raw(struct iio_dev *indio_dev,
if (ret < 0)
return ret;
return IIO_VAL_INT_PLUS_MICRO;
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+ switch (chan->type) {
+ case IIO_PROXIMITY:
+ ret = vcnl4040_read_ps_oversampling_ratio(data, val);
+ if (ret < 0)
+ return ret;
+ return IIO_VAL_INT;
+ default:
+ return -EINVAL;
+ }
default:
return -EINVAL;
}
@@ -850,6 +913,13 @@ static int vcnl4040_write_raw(struct iio_dev *indio_dev,
default:
return -EINVAL;
}
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+ switch (chan->type) {
+ case IIO_PROXIMITY:
+ return vcnl4040_write_ps_oversampling_ratio(data, val);
+ default:
+ return -EINVAL;
+ }
default:
return -EINVAL;
}
@@ -878,6 +948,16 @@ static int vcnl4040_read_avail(struct iio_dev *indio_dev,
}
*type = IIO_VAL_INT_PLUS_MICRO;
return IIO_AVAIL_LIST;
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+ switch (chan->type) {
+ case IIO_PROXIMITY:
+ *vals = (int *)vcnl4040_ps_oversampling_ratio;
+ *length = ARRAY_SIZE(vcnl4040_ps_oversampling_ratio);
+ *type = IIO_VAL_INT;
+ return IIO_AVAIL_LIST;
+ default:
+ return -EINVAL;
+ }
default:
return -EINVAL;
}
@@ -1648,8 +1728,10 @@ static const struct iio_chan_spec vcnl4040_channels[] = {
}, {
.type = IIO_PROXIMITY,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
- BIT(IIO_CHAN_INFO_INT_TIME),
- .info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME),
+ BIT(IIO_CHAN_INFO_INT_TIME) |
+ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
+ .info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME) |
+ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
.ext_info = vcnl4000_ext_info,
.event_spec = vcnl4040_event_spec,
.num_event_specs = ARRAY_SIZE(vcnl4040_event_spec),
--
2.30.2


2023-05-30 14:45:14

by Astrid Rost

[permalink] [raw]
Subject: [PATCH v5 4/7] iio: light: vcnl4000: add illuminance irq vcnl4040/4200

Add support to configure ambient light sensor interrupts and threshold
limits for vcnl4040 and vcnl4200. If an interrupt is detected an event
will be pushed to the event interface.

Signed-off-by: Astrid Rost <[email protected]>
---
drivers/iio/light/vcnl4000.c | 197 ++++++++++++++++++++++++++---------
1 file changed, 146 insertions(+), 51 deletions(-)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index b53f75b71309..8022805a6af4 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -62,6 +62,8 @@
#define VCNL4200_PS_CONF1 0x03 /* Proximity configuration */
#define VCNL4040_PS_THDL_LM 0x06 /* Proximity threshold low */
#define VCNL4040_PS_THDH_LM 0x07 /* Proximity threshold high */
+#define VCNL4040_ALS_THDL_LM 0x02 /* Ambient light threshold low */
+#define VCNL4040_ALS_THDH_LM 0x01 /* Ambient light threshold high */
#define VCNL4200_PS_DATA 0x08 /* Proximity data */
#define VCNL4200_AL_DATA 0x09 /* Ambient light data */
#define VCNL4040_INT_FLAGS 0x0b /* Interrupt register */
@@ -81,11 +83,14 @@

#define VCNL4040_ALS_CONF_ALS_SHUTDOWN BIT(0)
#define VCNL4040_ALS_CONF_IT GENMASK(7, 6) /* Ambient integration time */
+#define VCNL4040_ALS_CONF_INT_EN BIT(1) /* Ambient light Interrupt enable */
#define VCNL4040_PS_CONF1_PS_SHUTDOWN BIT(0)
#define VCNL4040_PS_CONF2_PS_IT GENMASK(3, 1) /* Proximity integration time */
#define VCNL4040_PS_CONF2_PS_INT GENMASK(9, 8) /* Proximity interrupt mode */
#define VCNL4040_PS_IF_AWAY BIT(8) /* Proximity event cross low threshold */
#define VCNL4040_PS_IF_CLOSE BIT(9) /* Proximity event cross high threshold */
+#define VCNL4040_ALS_RISING BIT(12) /* Ambient Light cross high threshold */
+#define VCNL4040_ALS_FALLING BIT(13) /* Ambient Light cross low threshold */

/* Bit masks for interrupt registers. */
#define VCNL4010_INT_THR_SEL BIT(0) /* Select threshold interrupt source */
@@ -170,6 +175,7 @@ struct vcnl4000_data {
int rev;
int al_scale;
u8 ps_int; /* proximity interrupt mode */
+ u8 als_int; /* ambient light interrupt mode*/
const struct vcnl4000_chip_spec *chip_spec;
struct mutex vcnl4000_lock;
struct vcnl4200_channel vcnl4200_al;
@@ -294,7 +300,7 @@ static int vcnl4200_set_power_state(struct vcnl4000_data *data, bool on)
int ret;

/* Do not power down if interrupts are enabled */
- if (!on && data->ps_int)
+ if (!on && (data->ps_int || data->als_int))
return 0;

ret = vcnl4000_write_als_enable(data, on);
@@ -339,6 +345,7 @@ static int vcnl4200_init(struct vcnl4000_data *data)

data->rev = (ret >> 8) & 0xf;
data->ps_int = 0;
+ data->als_int = 0;

data->vcnl4200_al.reg = VCNL4200_AL_DATA;
data->vcnl4200_ps.reg = VCNL4200_PS_DATA;
@@ -928,27 +935,45 @@ static int vcnl4040_read_event(struct iio_dev *indio_dev,
enum iio_event_info info,
int *val, int *val2)
{
- int ret;
+ int ret = -EINVAL;
struct vcnl4000_data *data = iio_priv(indio_dev);

- switch (dir) {
- case IIO_EV_DIR_RISING:
- ret = i2c_smbus_read_word_data(data->client,
- VCNL4040_PS_THDH_LM);
- if (ret < 0)
- return ret;
- *val = ret;
- return IIO_VAL_INT;
- case IIO_EV_DIR_FALLING:
- ret = i2c_smbus_read_word_data(data->client,
- VCNL4040_PS_THDL_LM);
- if (ret < 0)
- return ret;
- *val = ret;
- return IIO_VAL_INT;
+ switch (chan->type) {
+ case IIO_LIGHT:
+ switch (dir) {
+ case IIO_EV_DIR_RISING:
+ ret = i2c_smbus_read_word_data(data->client,
+ VCNL4040_ALS_THDH_LM);
+ break;
+ case IIO_EV_DIR_FALLING:
+ ret = i2c_smbus_read_word_data(data->client,
+ VCNL4040_ALS_THDL_LM);
+ break;
+ default:
+ return -EINVAL;
+ }
+ break;
+ case IIO_PROXIMITY:
+ switch (dir) {
+ case IIO_EV_DIR_RISING:
+ ret = i2c_smbus_read_word_data(data->client,
+ VCNL4040_PS_THDH_LM);
+ break;
+ case IIO_EV_DIR_FALLING:
+ ret = i2c_smbus_read_word_data(data->client,
+ VCNL4040_PS_THDL_LM);
+ break;
+ default:
+ return -EINVAL;
+ }
+ break;
default:
return -EINVAL;
}
+ if (ret < 0)
+ return ret;
+ *val = ret;
+ return IIO_VAL_INT;
}

static int vcnl4040_write_event(struct iio_dev *indio_dev,
@@ -958,25 +983,43 @@ static int vcnl4040_write_event(struct iio_dev *indio_dev,
enum iio_event_info info,
int val, int val2)
{
- int ret;
+ int ret = -EINVAL;
struct vcnl4000_data *data = iio_priv(indio_dev);
-
- switch (dir) {
- case IIO_EV_DIR_RISING:
- ret = i2c_smbus_write_word_data(data->client,
- VCNL4040_PS_THDH_LM, val);
- if (ret < 0)
- return ret;
- return IIO_VAL_INT;
- case IIO_EV_DIR_FALLING:
- ret = i2c_smbus_write_word_data(data->client,
- VCNL4040_PS_THDL_LM, val);
- if (ret < 0)
- return ret;
- return IIO_VAL_INT;
+ switch (chan->type) {
+ case IIO_LIGHT:
+ switch (dir) {
+ case IIO_EV_DIR_RISING:
+ ret = i2c_smbus_write_word_data(data->client,
+ VCNL4040_ALS_THDH_LM, val);
+ break;
+ case IIO_EV_DIR_FALLING:
+ ret = i2c_smbus_write_word_data(data->client,
+ VCNL4040_ALS_THDL_LM, val);
+ break;
+ default:
+ return -EINVAL;
+ }
+ break;
+ case IIO_PROXIMITY:
+ switch (dir) {
+ case IIO_EV_DIR_RISING:
+ ret = i2c_smbus_write_word_data(data->client,
+ VCNL4040_PS_THDH_LM, val);
+ break;
+ case IIO_EV_DIR_FALLING:
+ ret = i2c_smbus_write_word_data(data->client,
+ VCNL4040_PS_THDL_LM, val);
+ break;
+ default:
+ return -EINVAL;
+ }
+ break;
default:
return -EINVAL;
}
+ if (ret < 0)
+ return ret;
+ return IIO_VAL_INT;
}

static bool vcnl4010_is_thr_enabled(struct vcnl4000_data *data)
@@ -1068,16 +1111,28 @@ static int vcnl4040_read_event_config(struct iio_dev *indio_dev,
{
int ret;
struct vcnl4000_data *data = iio_priv(indio_dev);
+ switch (chan->type) {
+ case IIO_LIGHT:
+ ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF);
+ if (ret < 0)
+ return ret;

- ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
- if (ret < 0)
- return ret;
+ data->als_int = FIELD_GET(VCNL4040_ALS_CONF_INT_EN, ret);

- data->ps_int = FIELD_GET(VCNL4040_PS_CONF2_PS_INT, ret);
+ return data->als_int;
+ case IIO_PROXIMITY:
+ ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
+ if (ret < 0)
+ return ret;
+
+ data->ps_int = FIELD_GET(VCNL4040_PS_CONF2_PS_INT, ret);

- return (dir == IIO_EV_DIR_RISING) ?
- FIELD_GET(VCNL4040_PS_IF_AWAY, ret) :
- FIELD_GET(VCNL4040_PS_IF_CLOSE, ret);
+ return (dir == IIO_EV_DIR_RISING) ?
+ FIELD_GET(VCNL4040_PS_IF_AWAY, ret) :
+ FIELD_GET(VCNL4040_PS_IF_CLOSE, ret);
+ default:
+ return -EINVAL;
+ }
}

static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
@@ -1085,29 +1140,51 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
enum iio_event_type type,
enum iio_event_direction dir, int state)
{
- int ret;
+ int ret = -EINVAL;
u16 val, mask;
struct vcnl4000_data *data = iio_priv(indio_dev);

mutex_lock(&data->vcnl4000_lock);

- ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
- if (ret < 0)
- goto out;
+ switch (chan->type) {
+ case IIO_LIGHT:
+ ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF);
+ if (ret < 0)
+ goto out;

- if (dir == IIO_EV_DIR_RISING)
- mask = VCNL4040_PS_IF_AWAY;
- else
- mask = VCNL4040_PS_IF_CLOSE;
+ mask = VCNL4040_ALS_CONF_INT_EN;

- val = state ? (ret | mask) : (ret & ~mask);
+ val = state ? (ret | mask) : (ret & ~mask);

- data->ps_int = FIELD_GET(VCNL4040_PS_CONF2_PS_INT, val);
- ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, val);
+ data->als_int = FIELD_GET(VCNL4040_ALS_CONF_INT_EN, val);
+ ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF,
+ val);
+ break;
+ case IIO_PROXIMITY:
+
+ ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
+ if (ret < 0)
+ goto out;
+
+ if (dir == IIO_EV_DIR_RISING)
+ mask = VCNL4040_PS_IF_AWAY;
+ else
+ mask = VCNL4040_PS_IF_CLOSE;
+
+ val = state ? (ret | mask) : (ret & ~mask);
+
+ data->ps_int = FIELD_GET(VCNL4040_PS_CONF2_PS_INT, val);
+ ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1,
+ val);
+ break;
+ default:
+ break;
+ }

out:
mutex_unlock(&data->vcnl4000_lock);
- data->chip_spec->set_power_state(data, data->ps_int != 0);
+ data->chip_spec->set_power_state(data, data->ps_int ||
+ data->als_int);

return ret;
}
@@ -1138,6 +1215,22 @@ static irqreturn_t vcnl4040_irq_thread(int irq, void *p)
iio_get_time_ns(indio_dev));
}

+ if (ret & VCNL4040_ALS_FALLING) {
+ iio_push_event(indio_dev,
+ IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
+ IIO_EV_TYPE_THRESH,
+ IIO_EV_DIR_FALLING),
+ iio_get_time_ns(indio_dev));
+ }
+
+ if (ret & VCNL4040_ALS_RISING) {
+ iio_push_event(indio_dev,
+ IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
+ IIO_EV_TYPE_THRESH,
+ IIO_EV_DIR_RISING),
+ iio_get_time_ns(indio_dev));
+ }
+
return IRQ_HANDLED;
}

@@ -1360,6 +1453,8 @@ static const struct iio_chan_spec vcnl4040_channels[] = {
BIT(IIO_CHAN_INFO_SCALE) |
BIT(IIO_CHAN_INFO_INT_TIME),
.info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME),
+ .event_spec = vcnl4000_event_spec,
+ .num_event_specs = ARRAY_SIZE(vcnl4000_event_spec),
}, {
.type = IIO_PROXIMITY,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
--
2.30.2


2023-06-03 13:52:39

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 2/7] iio: light: vcnl4000: Add proximity ps_it for vcnl4200

Tue, May 30, 2023 at 04:24:00PM +0200, Astrid Rost kirjoitti:
> Add ps_it attributes for vcnl4200 (similar to vcnl4040).
> Add read/write attribute for proximity integration time.
> Read attribute for available proximity integration times.
> Change sampling rate depending on integration time.

...

> irqreturn_t (*irq_thread)(int irq, void *priv);
> irqreturn_t (*trig_buffer_func)(int irq, void *priv);
> u8 int_reg;

+ Blank line?

> + const int(*ps_it_times)[][2];
> + const int num_ps_it_times;


--
With Best Regards,
Andy Shevchenko



2023-06-03 14:08:10

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 4/7] iio: light: vcnl4000: add illuminance irq vcnl4040/4200

Tue, May 30, 2023 at 04:24:02PM +0200, Astrid Rost kirjoitti:
> Add support to configure ambient light sensor interrupts and threshold
> limits for vcnl4040 and vcnl4200. If an interrupt is detected an event
> will be pushed to the event interface.

...

> - int ret;
> + int ret = -EINVAL;

What is the purpose of this change?

...

> - int ret;
> + int ret = -EINVAL;

Ditto.


...

> int ret;
> struct vcnl4000_data *data = iio_priv(indio_dev);
> + switch (chan->type) {

Had you run checkpatch.pl? A definition block and a code should be delimited
by a blank line.

...

> - int ret;
> + int ret = -EINVAL;

Purpose?

...

> - val = state ? (ret | mask) : (ret & ~mask);
> + val = state ? (ret | mask) : (ret & ~mask);

I'm wondering if you can prepere the code by introducing switch-case instead of
current implementation in the preparatory patch and then do what you want to do
here. It will show much better the change and make review easier.

--
With Best Regards,
Andy Shevchenko



2023-06-03 14:24:46

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 6/7] iio: light: vcnl4000: Add oversampling_ratio for 4040/4200

Tue, May 30, 2023 at 04:24:04PM +0200, Astrid Rost kirjoitti:
> Add the proximity multi pulse (PS_MPS) as oversampling_ratio.
> Instead of one single pulse per every defined time frame, one can
> program2, 4, or even 8 pulses. This leads to a longer IRED on-time
> for each proximity measurement value, which also results in a higher
> detection range.
>
> Add read/write attribute for proximity oversampling-ratio and read
> attribute for available oversampling-ratio.
> This is supported for vcnl4040 and vcnl4200.

As per other patch comments.

--
With Best Regards,
Andy Shevchenko



2023-06-03 14:43:24

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 0/7] iio: light: vcnl4000: Add features for vncl4040/4200

Tue, May 30, 2023 at 04:23:58PM +0200, Astrid Rost kirjoitti:
> Add a more complete support for vncl4040 and vcnl4200, which allows to
> change the distance of proximity detection and interrupt support for the
> illuminance sensor.
>
> Proximity functionality:
> - Interrupt support (new on vcnl4200).
>
> Proximity reduce the amount of interrupts:
> - Adaptable integration time (new on vcnl4200) - the sampling rate
> changes according to this value.
> - Period - interrupt is asserted if the value is above or
> below a certain threshold.
>
> Proximity change the activity distance:
> - Oversampling ratio - Amount of LED pulses per measured raw value.
> - Calibration bias - LED current calibration of the sensor.
>
> Illuminance functionality:
> - Interrupt support.
>
> Illuminance reduce the amount of interrupts:
> - Adaptable integration time - the sampling rate and scale changes
> according to this value.
> - Period – interrupt is asserted if the value is above or
> below a certain threshold.

It's a good work, thank you for doing it!

But it has a lot of small style and inconsistent issues. They are not major
per se, but since there is more than 3, it makes sense to address. Also check
if you can split your patches to two or three where it makes sense.

--
With Best Regards,
Andy Shevchenko



2023-06-04 11:36:51

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 4/7] iio: light: vcnl4000: add illuminance irq vcnl4040/4200


> ...
>
> > - val = state ? (ret | mask) : (ret & ~mask);
> > + val = state ? (ret | mask) : (ret & ~mask);
>
> I'm wondering if you can prepere the code by introducing switch-case instead of
> current implementation in the preparatory patch and then do what you want to do
> here. It will show much better the change and make review easier.
>
Agreed. Diff wasn't being particularly helpful in how it handled the changes here
so helping it along with a precursor patch would be good.

Jonathan



2023-06-04 12:00:27

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 0/7] iio: light: vcnl4000: Add features for vncl4040/4200

On Sat, 3 Jun 2023 16:54:10 +0300
[email protected] wrote:

> Tue, May 30, 2023 at 04:23:58PM +0200, Astrid Rost kirjoitti:
> > Add a more complete support for vncl4040 and vcnl4200, which allows to
> > change the distance of proximity detection and interrupt support for the
> > illuminance sensor.
> >
> > Proximity functionality:
> > - Interrupt support (new on vcnl4200).
> >
> > Proximity reduce the amount of interrupts:
> > - Adaptable integration time (new on vcnl4200) - the sampling rate
> > changes according to this value.
> > - Period - interrupt is asserted if the value is above or
> > below a certain threshold.
> >
> > Proximity change the activity distance:
> > - Oversampling ratio - Amount of LED pulses per measured raw value.
> > - Calibration bias - LED current calibration of the sensor.
> >
> > Illuminance functionality:
> > - Interrupt support.
> >
> > Illuminance reduce the amount of interrupts:
> > - Adaptable integration time - the sampling rate and scale changes
> > according to this value.
> > - Period – interrupt is asserted if the value is above or
> > below a certain threshold.
>
> It's a good work, thank you for doing it!
>
> But it has a lot of small style and inconsistent issues. They are not major
> per se, but since there is more than 3, it makes sense to address. Also check
> if you can split your patches to two or three where it makes sense.
>

FWIW nothing to add from me. Agree with Andy that we are down to style
things that would be good to tidy up before applying these.

Adding levels to switch nests always makes for ugly diffs so I fully agree
with Andy that, though trivial, it is probably better to do those in two steps
for ease of review. If there is just one in a patch then meh, we can probably
cope with the extra thinking required to review them , but where it happens
several times it's worth making reviewer's lives that little bit easier!

Jonathan