All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support
@ 2023-01-17 15:24 Mårten Lindahl
  2023-01-17 15:24 ` [PATCH v4 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Mårten Lindahl @ 2023-01-17 15:24 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, Andy Shevchenko, linux-iio, kernel,
	Mårten Lindahl

Hi!

I have made three patches to add support for proximity sensor
interrupts with the vcnl4040 sensor.

The first two patches are minor restructuring of the current setup for
interrupts since the probe function hardcodes it for vcnl4010 only.

The third patch adds support to configure proximity sensor interrupts
and threshold limits for vcnl4040.

Kind regards
Mårten Lindahl

Changes in v4:
 - Restore unnecessary line changes
 - Remove unnecessary typecast

Changes in v3:
 - Only register iio_trigger when there is an irq_thread function

Changes in v2:
 - Make restructure of functions for interrupts and triggered buffer
   in separate patch
 - Add check for buffer_setup_ops
 - Remove irq dependency for devm_iio_triggered_buffer_setup
 - Change size of register variable and document it
 - Use field definitions for read/write event_config

Mårten Lindahl (3):
  iio: light: vcnl4000: Prepare for more generic setup
  iio: light: vcnl4000: Make irq handling more generic
  iio: light: vcnl4000: Add interrupt support for vcnl4040

 drivers/iio/light/vcnl4000.c | 454 ++++++++++++++++++++++++-----------
 1 file changed, 315 insertions(+), 139 deletions(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4 1/3] iio: light: vcnl4000: Prepare for more generic setup
  2023-01-17 15:24 [PATCH v4 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
@ 2023-01-17 15:24 ` Mårten Lindahl
  2023-01-17 15:51   ` Andy Shevchenko
  2023-01-17 15:24 ` [PATCH v4 2/3] iio: light: vcnl4000: Make irq handling more generic Mårten Lindahl
  2023-01-17 15:24 ` [PATCH v4 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040 Mårten Lindahl
  2 siblings, 1 reply; 11+ messages in thread
From: Mårten Lindahl @ 2023-01-17 15:24 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, Andy Shevchenko, linux-iio, kernel,
	Mårten Lindahl

In order to allow the chip_spec array reference the function pointers
for interrupts, the code for these functions need to be moved above the
chip_spec array.

This is a prestep to support a more generic setup of interrupts.

Signed-off-by: Mårten Lindahl <marten.lindahl@axis.com>
---
 drivers/iio/light/vcnl4000.c | 256 +++++++++++++++++------------------
 1 file changed, 128 insertions(+), 128 deletions(-)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index cc1a2062e76d..11b54b57e592 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -887,6 +887,134 @@ static ssize_t vcnl4000_read_near_level(struct iio_dev *indio_dev,
 	return sprintf(buf, "%u\n", data->near_level);
 }
 
+static irqreturn_t vcnl4010_irq_thread(int irq, void *p)
+{
+	struct iio_dev *indio_dev = p;
+	struct vcnl4000_data *data = iio_priv(indio_dev);
+	unsigned long isr;
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR);
+	if (ret < 0)
+		goto end;
+
+	isr = ret;
+
+	if (isr & VCNL4010_INT_THR) {
+		if (test_bit(VCNL4010_INT_THR_LOW, &isr)) {
+			iio_push_event(indio_dev,
+				       IIO_UNMOD_EVENT_CODE(
+					       IIO_PROXIMITY,
+					       1,
+					       IIO_EV_TYPE_THRESH,
+					       IIO_EV_DIR_FALLING),
+				       iio_get_time_ns(indio_dev));
+		}
+
+		if (test_bit(VCNL4010_INT_THR_HIGH, &isr)) {
+			iio_push_event(indio_dev,
+				       IIO_UNMOD_EVENT_CODE(
+					       IIO_PROXIMITY,
+					       1,
+					       IIO_EV_TYPE_THRESH,
+					       IIO_EV_DIR_RISING),
+				       iio_get_time_ns(indio_dev));
+		}
+
+		i2c_smbus_write_byte_data(data->client, VCNL4010_ISR,
+					  isr & VCNL4010_INT_THR);
+	}
+
+	if (isr & VCNL4010_INT_DRDY && iio_buffer_enabled(indio_dev))
+		iio_trigger_poll_chained(indio_dev->trig);
+
+end:
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t vcnl4010_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct vcnl4000_data *data = iio_priv(indio_dev);
+	const unsigned long *active_scan_mask = indio_dev->active_scan_mask;
+	u16 buffer[8] __aligned(8) = {0}; /* 1x16-bit + naturally aligned ts */
+	bool data_read = false;
+	unsigned long isr;
+	int val = 0;
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR);
+	if (ret < 0)
+		goto end;
+
+	isr = ret;
+
+	if (test_bit(0, active_scan_mask)) {
+		if (test_bit(VCNL4010_INT_PROXIMITY, &isr)) {
+			ret = vcnl4000_read_data(data,
+						 VCNL4000_PS_RESULT_HI,
+						 &val);
+			if (ret < 0)
+				goto end;
+
+			buffer[0] = val;
+			data_read = true;
+		}
+	}
+
+	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_ISR,
+					isr & VCNL4010_INT_DRDY);
+	if (ret < 0)
+		goto end;
+
+	if (!data_read)
+		goto end;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
+					   iio_get_time_ns(indio_dev));
+
+end:
+	iio_trigger_notify_done(indio_dev->trig);
+	return IRQ_HANDLED;
+}
+
+static int vcnl4010_buffer_postenable(struct iio_dev *indio_dev)
+{
+	struct vcnl4000_data *data = iio_priv(indio_dev);
+	int ret;
+	int cmd;
+
+	/* Do not enable the buffer if we are already capturing events. */
+	if (vcnl4010_is_in_periodic_mode(data))
+		return -EBUSY;
+
+	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL,
+					VCNL4010_INT_PROX_EN);
+	if (ret < 0)
+		return ret;
+
+	cmd = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN;
+	return i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, cmd);
+}
+
+static int vcnl4010_buffer_predisable(struct iio_dev *indio_dev)
+{
+	struct vcnl4000_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, 0);
+	if (ret < 0)
+		return ret;
+
+	return i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 0);
+}
+
+static const struct iio_buffer_setup_ops vcnl4010_buffer_ops = {
+	.postenable = &vcnl4010_buffer_postenable,
+	.predisable = &vcnl4010_buffer_predisable,
+};
+
 static const struct iio_chan_spec_ext_info vcnl4000_ext_info[] = {
 	{
 		.name = "nearlevel",
@@ -1030,134 +1158,6 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
 	},
 };
 
-static irqreturn_t vcnl4010_irq_thread(int irq, void *p)
-{
-	struct iio_dev *indio_dev = p;
-	struct vcnl4000_data *data = iio_priv(indio_dev);
-	unsigned long isr;
-	int ret;
-
-	ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR);
-	if (ret < 0)
-		goto end;
-
-	isr = ret;
-
-	if (isr & VCNL4010_INT_THR) {
-		if (test_bit(VCNL4010_INT_THR_LOW, &isr)) {
-			iio_push_event(indio_dev,
-				       IIO_UNMOD_EVENT_CODE(
-					       IIO_PROXIMITY,
-					       1,
-					       IIO_EV_TYPE_THRESH,
-					       IIO_EV_DIR_FALLING),
-				       iio_get_time_ns(indio_dev));
-		}
-
-		if (test_bit(VCNL4010_INT_THR_HIGH, &isr)) {
-			iio_push_event(indio_dev,
-				       IIO_UNMOD_EVENT_CODE(
-					       IIO_PROXIMITY,
-					       1,
-					       IIO_EV_TYPE_THRESH,
-					       IIO_EV_DIR_RISING),
-				       iio_get_time_ns(indio_dev));
-		}
-
-		i2c_smbus_write_byte_data(data->client, VCNL4010_ISR,
-					  isr & VCNL4010_INT_THR);
-	}
-
-	if (isr & VCNL4010_INT_DRDY && iio_buffer_enabled(indio_dev))
-		iio_trigger_poll_chained(indio_dev->trig);
-
-end:
-	return IRQ_HANDLED;
-}
-
-static irqreturn_t vcnl4010_trigger_handler(int irq, void *p)
-{
-	struct iio_poll_func *pf = p;
-	struct iio_dev *indio_dev = pf->indio_dev;
-	struct vcnl4000_data *data = iio_priv(indio_dev);
-	const unsigned long *active_scan_mask = indio_dev->active_scan_mask;
-	u16 buffer[8] __aligned(8) = {0}; /* 1x16-bit + naturally aligned ts */
-	bool data_read = false;
-	unsigned long isr;
-	int val = 0;
-	int ret;
-
-	ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR);
-	if (ret < 0)
-		goto end;
-
-	isr = ret;
-
-	if (test_bit(0, active_scan_mask)) {
-		if (test_bit(VCNL4010_INT_PROXIMITY, &isr)) {
-			ret = vcnl4000_read_data(data,
-						 VCNL4000_PS_RESULT_HI,
-						 &val);
-			if (ret < 0)
-				goto end;
-
-			buffer[0] = val;
-			data_read = true;
-		}
-	}
-
-	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_ISR,
-					isr & VCNL4010_INT_DRDY);
-	if (ret < 0)
-		goto end;
-
-	if (!data_read)
-		goto end;
-
-	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
-					   iio_get_time_ns(indio_dev));
-
-end:
-	iio_trigger_notify_done(indio_dev->trig);
-	return IRQ_HANDLED;
-}
-
-static int vcnl4010_buffer_postenable(struct iio_dev *indio_dev)
-{
-	struct vcnl4000_data *data = iio_priv(indio_dev);
-	int ret;
-	int cmd;
-
-	/* Do not enable the buffer if we are already capturing events. */
-	if (vcnl4010_is_in_periodic_mode(data))
-		return -EBUSY;
-
-	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL,
-					VCNL4010_INT_PROX_EN);
-	if (ret < 0)
-		return ret;
-
-	cmd = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN;
-	return i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, cmd);
-}
-
-static int vcnl4010_buffer_predisable(struct iio_dev *indio_dev)
-{
-	struct vcnl4000_data *data = iio_priv(indio_dev);
-	int ret;
-
-	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, 0);
-	if (ret < 0)
-		return ret;
-
-	return i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 0);
-}
-
-static const struct iio_buffer_setup_ops vcnl4010_buffer_ops = {
-	.postenable = &vcnl4010_buffer_postenable,
-	.predisable = &vcnl4010_buffer_predisable,
-};
-
 static const struct iio_trigger_ops vcnl4010_trigger_ops = {
 	.validate_device = iio_trigger_validate_own_device,
 };
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v4 2/3] iio: light: vcnl4000: Make irq handling more generic
  2023-01-17 15:24 [PATCH v4 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
  2023-01-17 15:24 ` [PATCH v4 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
@ 2023-01-17 15:24 ` Mårten Lindahl
  2023-01-17 15:53   ` Andy Shevchenko
  2023-01-17 15:24 ` [PATCH v4 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040 Mårten Lindahl
  2 siblings, 1 reply; 11+ messages in thread
From: Mårten Lindahl @ 2023-01-17 15:24 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, Andy Shevchenko, linux-iio, kernel,
	Mårten Lindahl

This driver supports 4 chips, by which only one (vcnl4010) handles
interrupts and has support for triggered buffer. The setup of these
functions is hardcoded for vcnl4010 inside the generic vcnl4000_probe,
and thus ignores the chip specific configuration structure where all
other chip specific functions are specified.

This complicates adding interrupt handler and triggered buffer support
to chips which may have support for it.

Add members for irq threads and iio_buffer_setup_ops to the generic
vcnl4000_chip_spec struct, so that instead of checking a chip specific
boolean irq support, we check for a chip specific triggered buffer
handler, and/or a chip specific irq thread handler.

Signed-off-by: Mårten Lindahl <marten.lindahl@axis.com>
---
 drivers/iio/light/vcnl4000.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 11b54b57e592..7851a675290f 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -150,11 +150,13 @@ struct vcnl4000_chip_spec {
 	struct iio_chan_spec const *channels;
 	const int num_channels;
 	const struct iio_info *info;
-	bool irq_support;
+	const struct iio_buffer_setup_ops *buffer_setup_ops;
 	int (*init)(struct vcnl4000_data *data);
 	int (*measure_light)(struct vcnl4000_data *data, int *val);
 	int (*measure_proximity)(struct vcnl4000_data *data, int *val);
 	int (*set_power_state)(struct vcnl4000_data *data, bool on);
+	irqreturn_t (*irq_thread)(int irq, void *priv);
+	irqreturn_t (*trig_buffer_func)(int irq, void *priv);
 };
 
 static const struct i2c_device_id vcnl4000_id[] = {
@@ -1121,7 +1123,6 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
 		.channels = vcnl4000_channels,
 		.num_channels = ARRAY_SIZE(vcnl4000_channels),
 		.info = &vcnl4000_info,
-		.irq_support = false,
 	},
 	[VCNL4010] = {
 		.prod = "VCNL4010/4020",
@@ -1132,7 +1133,9 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
 		.channels = vcnl4010_channels,
 		.num_channels = ARRAY_SIZE(vcnl4010_channels),
 		.info = &vcnl4010_info,
-		.irq_support = true,
+		.irq_thread = vcnl4010_irq_thread,
+		.trig_buffer_func = vcnl4010_trigger_handler,
+		.buffer_setup_ops = &vcnl4010_buffer_ops,
 	},
 	[VCNL4040] = {
 		.prod = "VCNL4040",
@@ -1143,7 +1146,6 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
 		.channels = vcnl4040_channels,
 		.num_channels = ARRAY_SIZE(vcnl4040_channels),
 		.info = &vcnl4040_info,
-		.irq_support = false,
 	},
 	[VCNL4200] = {
 		.prod = "VCNL4200",
@@ -1154,7 +1156,6 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
 		.channels = vcnl4000_channels,
 		.num_channels = ARRAY_SIZE(vcnl4000_channels),
 		.info = &vcnl4000_info,
-		.irq_support = false,
 	},
 };
 
@@ -1214,22 +1215,26 @@ static int vcnl4000_probe(struct i2c_client *client)
 	indio_dev->name = VCNL4000_DRV_NAME;
 	indio_dev->modes = INDIO_DIRECT_MODE;
 
-	if (client->irq && data->chip_spec->irq_support) {
+	if (data->chip_spec->trig_buffer_func &&
+	    data->chip_spec->buffer_setup_ops) {
 		ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
 						      NULL,
-						      vcnl4010_trigger_handler,
-						      &vcnl4010_buffer_ops);
+						      data->chip_spec->trig_buffer_func,
+						      data->chip_spec->buffer_setup_ops);
 		if (ret < 0) {
 			dev_err(&client->dev,
 				"unable to setup iio triggered buffer\n");
 			return ret;
 		}
+	}
 
-		ret = devm_request_threaded_irq(&client->dev, client->irq,
-						NULL, vcnl4010_irq_thread,
+	if (client->irq && data->chip_spec->irq_thread) {
+		ret = devm_request_threaded_irq(&client->dev,
+						client->irq, NULL,
+						data->chip_spec->irq_thread,
 						IRQF_TRIGGER_FALLING |
 						IRQF_ONESHOT,
-						"vcnl4010_irq",
+						"vcnl4000_irq",
 						indio_dev);
 		if (ret < 0) {
 			dev_err(&client->dev, "irq request failed\n");
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v4 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-17 15:24 [PATCH v4 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
  2023-01-17 15:24 ` [PATCH v4 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
  2023-01-17 15:24 ` [PATCH v4 2/3] iio: light: vcnl4000: Make irq handling more generic Mårten Lindahl
@ 2023-01-17 15:24 ` Mårten Lindahl
  2023-01-17 15:58   ` Andy Shevchenko
  2 siblings, 1 reply; 11+ messages in thread
From: Mårten Lindahl @ 2023-01-17 15:24 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, Andy Shevchenko, linux-iio, kernel,
	Mårten Lindahl

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

Signed-off-by: Mårten Lindahl <marten.lindahl@axis.com>
---
 drivers/iio/light/vcnl4000.c | 171 +++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 7851a675290f..741a3ddb2c4f 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -60,8 +60,11 @@
 
 #define VCNL4200_AL_CONF	0x00 /* Ambient light configuration */
 #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 VCNL4200_PS_DATA	0x08 /* Proximity data */
 #define VCNL4200_AL_DATA	0x09 /* Ambient light data */
+#define VCNL4040_INT_FLAGS	0x0b /* Interrupt register */
 #define VCNL4200_DEV_ID		0x0e /* Device ID, slave address and version */
 
 #define VCNL4040_DEV_ID		0x0c /* Device ID and version */
@@ -78,6 +81,9 @@
 #define VCNL4040_ALS_CONF_ALS_SHUTDOWN	BIT(0)
 #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 */
 
 /* Bit masks for interrupt registers. */
 #define VCNL4010_INT_THR_SEL	BIT(0) /* Select threshold interrupt source */
@@ -138,6 +144,7 @@ struct vcnl4000_data {
 	enum vcnl4000_device_ids id;
 	int rev;
 	int al_scale;
+	u8 ps_int;		/* proximity interrupt mode */
 	const struct vcnl4000_chip_spec *chip_spec;
 	struct mutex vcnl4000_lock;
 	struct vcnl4200_channel vcnl4200_al;
@@ -256,6 +263,10 @@ 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)
+		return 0;
+
 	ret = vcnl4000_write_als_enable(data, on);
 	if (ret < 0)
 		return ret;
@@ -297,6 +308,7 @@ static int vcnl4200_init(struct vcnl4000_data *data)
 	dev_dbg(&data->client->dev, "device id 0x%x", id);
 
 	data->rev = (ret >> 8) & 0xf;
+	data->ps_int = 0;
 
 	data->vcnl4200_al.reg = VCNL4200_AL_DATA;
 	data->vcnl4200_ps.reg = VCNL4200_PS_DATA;
@@ -797,6 +809,64 @@ static int vcnl4010_write_event(struct iio_dev *indio_dev,
 	}
 }
 
+static int vcnl4040_read_event(struct iio_dev *indio_dev,
+			       const struct iio_chan_spec *chan,
+			       enum iio_event_type type,
+			       enum iio_event_direction dir,
+			       enum iio_event_info info,
+			       int *val, int *val2)
+{
+	int ret;
+	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;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vcnl4040_write_event(struct iio_dev *indio_dev,
+				const struct iio_chan_spec *chan,
+				enum iio_event_type type,
+				enum iio_event_direction dir,
+				enum iio_event_info info,
+				int val, int val2)
+{
+	int ret;
+	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;
+	default:
+		return -EINVAL;
+	}
+}
+
 static bool vcnl4010_is_thr_enabled(struct vcnl4000_data *data)
 {
 	int ret;
@@ -879,6 +949,88 @@ static int vcnl4010_write_event_config(struct iio_dev *indio_dev,
 	}
 }
 
+static int vcnl4040_read_event_config(struct iio_dev *indio_dev,
+				      const struct iio_chan_spec *chan,
+				      enum iio_event_type type,
+				      enum iio_event_direction dir)
+{
+	int ret;
+	struct vcnl4000_data *data = iio_priv(indio_dev);
+
+	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);
+}
+
+static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
+				       const struct iio_chan_spec *chan,
+				       enum iio_event_type type,
+				       enum iio_event_direction dir, int state)
+{
+	int ret;
+	u16 val;
+	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;
+
+	val = ret;
+
+	if (dir == IIO_EV_DIR_RISING)
+		val = state ? (val | VCNL4040_PS_IF_AWAY) :
+		    (val & ~VCNL4040_PS_IF_AWAY);
+	else
+		val = state ? (val | VCNL4040_PS_IF_CLOSE) :
+		    (val & ~VCNL4040_PS_IF_CLOSE);
+
+	data->ps_int = FIELD_GET(VCNL4040_PS_CONF2_PS_INT, val);
+	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, val);
+
+out:
+	mutex_unlock(&data->vcnl4000_lock);
+	data->chip_spec->set_power_state(data, data->ps_int != 0);
+
+	return ret;
+}
+
+static irqreturn_t vcnl4040_irq_thread(int irq, void *p)
+{
+	struct iio_dev *indio_dev = p;
+	struct vcnl4000_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = i2c_smbus_read_word_data(data->client, VCNL4040_INT_FLAGS);
+	if (ret < 0)
+		return IRQ_HANDLED;
+
+	if (ret & VCNL4040_PS_IF_CLOSE) {
+		iio_push_event(indio_dev,
+			       IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
+						    IIO_EV_TYPE_THRESH,
+						    IIO_EV_DIR_RISING),
+			       iio_get_time_ns(indio_dev));
+	}
+
+	if (ret & VCNL4040_PS_IF_AWAY) {
+		iio_push_event(indio_dev,
+			       IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
+						    IIO_EV_TYPE_THRESH,
+						    IIO_EV_DIR_FALLING),
+			       iio_get_time_ns(indio_dev));
+	}
+
+	return IRQ_HANDLED;
+}
+
 static ssize_t vcnl4000_read_near_level(struct iio_dev *indio_dev,
 					uintptr_t priv,
 					const struct iio_chan_spec *chan,
@@ -1042,6 +1194,18 @@ static const struct iio_event_spec vcnl4000_event_spec[] = {
 	}
 };
 
+static const struct iio_event_spec vcnl4040_event_spec[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_RISING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
+	},
+};
+
 static const struct iio_chan_spec vcnl4000_channels[] = {
 	{
 		.type = IIO_LIGHT,
@@ -1090,6 +1254,8 @@ static const struct iio_chan_spec vcnl4040_channels[] = {
 			BIT(IIO_CHAN_INFO_INT_TIME),
 		.info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME),
 		.ext_info = vcnl4000_ext_info,
+		.event_spec = vcnl4040_event_spec,
+		.num_event_specs = ARRAY_SIZE(vcnl4040_event_spec),
 	}
 };
 
@@ -1110,6 +1276,10 @@ static const struct iio_info vcnl4010_info = {
 static const struct iio_info vcnl4040_info = {
 	.read_raw = vcnl4000_read_raw,
 	.write_raw = vcnl4040_write_raw,
+	.read_event_value = vcnl4040_read_event,
+	.write_event_value = vcnl4040_write_event,
+	.read_event_config = vcnl4040_read_event_config,
+	.write_event_config = vcnl4040_write_event_config,
 	.read_avail = vcnl4040_read_avail,
 };
 
@@ -1146,6 +1316,7 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
 		.channels = vcnl4040_channels,
 		.num_channels = ARRAY_SIZE(vcnl4040_channels),
 		.info = &vcnl4040_info,
+		.irq_thread = vcnl4040_irq_thread,
 	},
 	[VCNL4200] = {
 		.prod = "VCNL4200",
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/3] iio: light: vcnl4000: Prepare for more generic setup
  2023-01-17 15:24 ` [PATCH v4 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
@ 2023-01-17 15:51   ` Andy Shevchenko
  2023-01-17 17:07     ` Marten Lindahl
  0 siblings, 1 reply; 11+ messages in thread
From: Andy Shevchenko @ 2023-01-17 15:51 UTC (permalink / raw)
  To: Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel

On Tue, Jan 17, 2023 at 04:24:33PM +0100, Mårten Lindahl wrote:
> In order to allow the chip_spec array reference the function pointers
> for interrupts, the code for these functions need to be moved above the
> chip_spec array.
> 
> This is a prestep to support a more generic setup of interrupts.

...

> +	u16 buffer[8] __aligned(8) = {0}; /* 1x16-bit + naturally aligned ts */

I understand that this just a code move without a single change, but
I have to ask. Don't we use the specific struct layout for this?

Also, 0 is redundant.

P.S. Maybe considered as a followup change(s).

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 2/3] iio: light: vcnl4000: Make irq handling more generic
  2023-01-17 15:24 ` [PATCH v4 2/3] iio: light: vcnl4000: Make irq handling more generic Mårten Lindahl
@ 2023-01-17 15:53   ` Andy Shevchenko
  2023-01-17 17:09     ` Marten Lindahl
  0 siblings, 1 reply; 11+ messages in thread
From: Andy Shevchenko @ 2023-01-17 15:53 UTC (permalink / raw)
  To: Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel

On Tue, Jan 17, 2023 at 04:24:34PM +0100, Mårten Lindahl wrote:
> This driver supports 4 chips, by which only one (vcnl4010) handles
> interrupts and has support for triggered buffer. The setup of these
> functions is hardcoded for vcnl4010 inside the generic vcnl4000_probe,
> and thus ignores the chip specific configuration structure where all
> other chip specific functions are specified.
> 
> This complicates adding interrupt handler and triggered buffer support
> to chips which may have support for it.
> 
> Add members for irq threads and iio_buffer_setup_ops to the generic
> vcnl4000_chip_spec struct, so that instead of checking a chip specific
> boolean irq support, we check for a chip specific triggered buffer
> handler, and/or a chip specific irq thread handler.

Thank for an update!

...

> -		ret = devm_request_threaded_irq(&client->dev, client->irq,
> -						NULL, vcnl4010_irq_thread,

> +		ret = devm_request_threaded_irq(&client->dev,
> +						client->irq, NULL,

But why first line has been changed now?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-17 15:24 ` [PATCH v4 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040 Mårten Lindahl
@ 2023-01-17 15:58   ` Andy Shevchenko
  2023-01-17 15:59     ` Andy Shevchenko
  0 siblings, 1 reply; 11+ messages in thread
From: Andy Shevchenko @ 2023-01-17 15:58 UTC (permalink / raw)
  To: Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel

On Tue, Jan 17, 2023 at 04:24:35PM +0100, Mårten Lindahl wrote:
> Add support to configure proximity sensor interrupts and threshold
> limits for vcnl4040. If an interrupt is detected an event will be
> pushed to the event interface.

...

> +	return (dir == IIO_EV_DIR_RISING) ?

> +	    FIELD_GET(VCNL4040_PS_IF_AWAY, ret) :
> +	    FIELD_GET(VCNL4040_PS_IF_CLOSE, ret);

Indentation issues?

...

> +	val = ret;
> +
> +	if (dir == IIO_EV_DIR_RISING)
> +		val = state ? (val | VCNL4040_PS_IF_AWAY) :
> +		    (val & ~VCNL4040_PS_IF_AWAY);
> +	else
> +		val = state ? (val | VCNL4040_PS_IF_CLOSE) :
> +		    (val & ~VCNL4040_PS_IF_CLOSE);


Wouldn't be better

	uXX val, mask;


	if (dir == IIO_EV_DIR_RISING)
		mask = VCNL4040_PS_IF_AWAY;
	else
		mask = VCNL4040_PS_IF_CLOSE;

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

?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-17 15:58   ` Andy Shevchenko
@ 2023-01-17 15:59     ` Andy Shevchenko
  2023-01-17 17:14       ` Marten Lindahl
  0 siblings, 1 reply; 11+ messages in thread
From: Andy Shevchenko @ 2023-01-17 15:59 UTC (permalink / raw)
  To: Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel

On Tue, Jan 17, 2023 at 05:58:54PM +0200, Andy Shevchenko wrote:
> On Tue, Jan 17, 2023 at 04:24:35PM +0100, Mårten Lindahl wrote:

...

> > +	val = ret;
> > +
> > +	if (dir == IIO_EV_DIR_RISING)
> > +		val = state ? (val | VCNL4040_PS_IF_AWAY) :
> > +		    (val & ~VCNL4040_PS_IF_AWAY);
> > +	else
> > +		val = state ? (val | VCNL4040_PS_IF_CLOSE) :
> > +		    (val & ~VCNL4040_PS_IF_CLOSE);
> 
> 
> Wouldn't be better
> 
> 	uXX val, mask;
> 
> 
> 	if (dir == IIO_EV_DIR_RISING)
> 		mask = VCNL4040_PS_IF_AWAY;
> 	else
> 		mask = VCNL4040_PS_IF_CLOSE;
> 
> 	val = state ? (val | mask) : (val & ~mask);

Of course I meant

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

> ?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/3] iio: light: vcnl4000: Prepare for more generic setup
  2023-01-17 15:51   ` Andy Shevchenko
@ 2023-01-17 17:07     ` Marten Lindahl
  0 siblings, 0 replies; 11+ messages in thread
From: Marten Lindahl @ 2023-01-17 17:07 UTC (permalink / raw)
  To: Andy Shevchenko, Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel


On 1/17/23 16:51, Andy Shevchenko wrote:
> On Tue, Jan 17, 2023 at 04:24:33PM +0100, Mårten Lindahl wrote:
>> In order to allow the chip_spec array reference the function pointers
>> for interrupts, the code for these functions need to be moved above the
>> chip_spec array.
>>
>> This is a prestep to support a more generic setup of interrupts.
> ...
>
>> +	u16 buffer[8] __aligned(8) = {0}; /* 1x16-bit + naturally aligned ts */
> I understand that this just a code move without a single change, but
> I have to ask. Don't we use the specific struct layout for this?
>
> Also, 0 is redundant.
>
> P.S. Maybe considered as a followup change(s).
>
Hi Andy! I see your point. But yes, as it's not directly part of my 
change I'd prefer to handle it separately from this patch series.

Kind regards

Mårten



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 2/3] iio: light: vcnl4000: Make irq handling more generic
  2023-01-17 15:53   ` Andy Shevchenko
@ 2023-01-17 17:09     ` Marten Lindahl
  0 siblings, 0 replies; 11+ messages in thread
From: Marten Lindahl @ 2023-01-17 17:09 UTC (permalink / raw)
  To: Andy Shevchenko, Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel


On 1/17/23 16:53, Andy Shevchenko wrote:
> On Tue, Jan 17, 2023 at 04:24:34PM +0100, Mårten Lindahl wrote:
>> This driver supports 4 chips, by which only one (vcnl4010) handles
>> interrupts and has support for triggered buffer. The setup of these
>> functions is hardcoded for vcnl4010 inside the generic vcnl4000_probe,
>> and thus ignores the chip specific configuration structure where all
>> other chip specific functions are specified.
>>
>> This complicates adding interrupt handler and triggered buffer support
>> to chips which may have support for it.
>>
>> Add members for irq threads and iio_buffer_setup_ops to the generic
>> vcnl4000_chip_spec struct, so that instead of checking a chip specific
>> boolean irq support, we check for a chip specific triggered buffer
>> handler, and/or a chip specific irq thread handler.
> Thank for an update!
>
> ...
>
>> -		ret = devm_request_threaded_irq(&client->dev, client->irq,
>> -						NULL, vcnl4010_irq_thread,
>> +		ret = devm_request_threaded_irq(&client->dev,
>> +						client->irq, NULL,
> But why first line has been changed now?
>
Sorry! I'll fix it!

Kind regards

Mårten


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-17 15:59     ` Andy Shevchenko
@ 2023-01-17 17:14       ` Marten Lindahl
  0 siblings, 0 replies; 11+ messages in thread
From: Marten Lindahl @ 2023-01-17 17:14 UTC (permalink / raw)
  To: Andy Shevchenko, Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel


On 1/17/23 16:59, Andy Shevchenko wrote:
> On Tue, Jan 17, 2023 at 05:58:54PM +0200, Andy Shevchenko wrote:
>> On Tue, Jan 17, 2023 at 04:24:35PM +0100, Mårten Lindahl wrote:
> ...
>
>>> +	val = ret;
>>> +
>>> +	if (dir == IIO_EV_DIR_RISING)
>>> +		val = state ? (val | VCNL4040_PS_IF_AWAY) :
>>> +		    (val & ~VCNL4040_PS_IF_AWAY);
>>> +	else
>>> +		val = state ? (val | VCNL4040_PS_IF_CLOSE) :
>>> +		    (val & ~VCNL4040_PS_IF_CLOSE);
>>
>> Wouldn't be better
>>
>> 	uXX val, mask;
>>
>>
>> 	if (dir == IIO_EV_DIR_RISING)
>> 		mask = VCNL4040_PS_IF_AWAY;
>> 	else
>> 		mask = VCNL4040_PS_IF_CLOSE;
>>
>> 	val = state ? (val | mask) : (val & ~mask);
> Of course I meant
>
> 	val = state ? (ret | mask) : (ret & ~mask);
>
>> ?

Sure. There are several ways to do this, but your suggestion may be more 
clear. I'll fix it. Thanks!

Kind regards

Mårten


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-01-17 17:15 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-17 15:24 [PATCH v4 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
2023-01-17 15:24 ` [PATCH v4 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
2023-01-17 15:51   ` Andy Shevchenko
2023-01-17 17:07     ` Marten Lindahl
2023-01-17 15:24 ` [PATCH v4 2/3] iio: light: vcnl4000: Make irq handling more generic Mårten Lindahl
2023-01-17 15:53   ` Andy Shevchenko
2023-01-17 17:09     ` Marten Lindahl
2023-01-17 15:24 ` [PATCH v4 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040 Mårten Lindahl
2023-01-17 15:58   ` Andy Shevchenko
2023-01-17 15:59     ` Andy Shevchenko
2023-01-17 17:14       ` Marten Lindahl

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.