All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support
@ 2023-01-10 13:43 Mårten Lindahl
  2023-01-10 13:43 ` [PATCH v3 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Mårten Lindahl @ 2023-01-10 13:43 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, 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 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 | 479 ++++++++++++++++++++++++-----------
 1 file changed, 328 insertions(+), 151 deletions(-)

-- 
2.30.2


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

* [PATCH v3 1/3] iio: light: vcnl4000: Prepare for more generic setup
  2023-01-10 13:43 [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
@ 2023-01-10 13:43 ` Mårten Lindahl
  2023-01-10 13:43 ` [PATCH v3 2/3] iio: light: vcnl4000: Make irq handling more generic Mårten Lindahl
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Mårten Lindahl @ 2023-01-10 13:43 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, 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 v3 2/3] iio: light: vcnl4000: Make irq handling more generic
  2023-01-10 13:43 [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
  2023-01-10 13:43 ` [PATCH v3 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
@ 2023-01-10 13:43 ` Mårten Lindahl
  2023-01-15 17:41   ` andriy.shechenko
  2023-01-10 13:43 ` [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040 Mårten Lindahl
  2023-01-14 16:47 ` [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Jonathan Cameron
  3 siblings, 1 reply; 11+ messages in thread
From: Mårten Lindahl @ 2023-01-10 13:43 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, 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 | 52 ++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 23 deletions(-)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 11b54b57e592..792fc7f9c36e 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,31 +1215,36 @@ 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) {
-		ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
-						      NULL,
-						      vcnl4010_trigger_handler,
-						      &vcnl4010_buffer_ops);
+	if (data->chip_spec->trig_buffer_func &&
+	    data->chip_spec->buffer_setup_ops) {
+		ret = devm_iio_triggered_buffer_setup(&client->dev,
+						      indio_dev, NULL,
+						      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,
-						IRQF_TRIGGER_FALLING |
-						IRQF_ONESHOT,
-						"vcnl4010_irq",
-						indio_dev);
-		if (ret < 0) {
-			dev_err(&client->dev, "irq request failed\n");
-			return ret;
-		}
+	if (client->irq) {
+		if (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,
+							"vcnl4000_irq",
+							indio_dev);
+			if (ret < 0) {
+				dev_err(&client->dev, "irq request failed\n");
+				return ret;
+			}
 
-		ret = vcnl4010_probe_trigger(indio_dev);
-		if (ret < 0)
-			return ret;
+			ret = vcnl4010_probe_trigger(indio_dev);
+			if (ret < 0)
+				return ret;
+		}
 	}
 
 	ret = pm_runtime_set_active(&client->dev);
-- 
2.30.2


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

* [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-10 13:43 [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
  2023-01-10 13:43 ` [PATCH v3 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
  2023-01-10 13:43 ` [PATCH v3 2/3] iio: light: vcnl4000: Make irq handling more generic Mårten Lindahl
@ 2023-01-10 13:43 ` Mårten Lindahl
  2023-01-15 17:38   ` andriy.shechenko
  2023-01-14 16:47 ` [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Jonathan Cameron
  3 siblings, 1 reply; 11+ messages in thread
From: Mårten Lindahl @ 2023-01-10 13:43 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, 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 792fc7f9c36e..4404974c3730 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 = (u16)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 v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support
  2023-01-10 13:43 [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
                   ` (2 preceding siblings ...)
  2023-01-10 13:43 ` [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040 Mårten Lindahl
@ 2023-01-14 16:47 ` Jonathan Cameron
  3 siblings, 0 replies; 11+ messages in thread
From: Jonathan Cameron @ 2023-01-14 16:47 UTC (permalink / raw)
  To: Mårten Lindahl; +Cc: Lars-Peter Clausen, linux-iio, kernel

On Tue, 10 Jan 2023 14:43:20 +0100
Mårten Lindahl <marten.lindahl@axis.com> wrote:

> 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

Series applied to the togreg branch of iio.git. Initially pushed out as
testing so that the bots can take a poke at it before I make a mess
of Linux next if we missed something.

Thanks,

Jonathan

> 
> 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 | 479 ++++++++++++++++++++++++-----------
>  1 file changed, 328 insertions(+), 151 deletions(-)
> 


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

* Re: [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-10 13:43 ` [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040 Mårten Lindahl
@ 2023-01-15 17:38   ` andriy.shechenko
  2023-01-16 17:32     ` Jonathan Cameron
  2023-01-16 23:19     ` Marten Lindahl
  0 siblings, 2 replies; 11+ messages in thread
From: andriy.shechenko @ 2023-01-15 17:38 UTC (permalink / raw)
  To: Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel

Tue, Jan 10, 2023 at 02:43:23PM +0100, Mårten Lindahl kirjoitti:
> 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.

...

> +	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
> +	if (ret < 0)
> +		goto out;
> +
> +	val = (u16)ret;

Why do you need casting?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v3 2/3] iio: light: vcnl4000: Make irq handling more generic
  2023-01-10 13:43 ` [PATCH v3 2/3] iio: light: vcnl4000: Make irq handling more generic Mårten Lindahl
@ 2023-01-15 17:41   ` andriy.shechenko
  2023-01-16 23:06     ` Marten Lindahl
  0 siblings, 1 reply; 11+ messages in thread
From: andriy.shechenko @ 2023-01-15 17:41 UTC (permalink / raw)
  To: Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel

Tue, Jan 10, 2023 at 02:43:22PM +0100, Mårten Lindahl kirjoitti:
> 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.

...

> -		ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
> -						      NULL,


> +		ret = devm_iio_triggered_buffer_setup(&client->dev,
> +						      indio_dev, NULL,

What the point of this part of the hunk?

...

> +	if (client->irq) {
> +		if (data->chip_spec->irq_thread) {

I have checked the rest of the series and found nothing that prevents this to
be written as

	if (client->irq && data->chip_spec->irq_thread) {

This will reduce the noise in the patch.

>  	}

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-15 17:38   ` andriy.shechenko
@ 2023-01-16 17:32     ` Jonathan Cameron
  2023-01-16 23:22       ` Marten Lindahl
  2023-01-16 23:19     ` Marten Lindahl
  1 sibling, 1 reply; 11+ messages in thread
From: Jonathan Cameron @ 2023-01-16 17:32 UTC (permalink / raw)
  To: andriy.shechenko
  Cc: Mårten Lindahl, Lars-Peter Clausen, linux-iio, kernel

On Sun, 15 Jan 2023 19:38:03 +0200
andriy.shechenko@saunalahti.fi wrote:

> Tue, Jan 10, 2023 at 02:43:23PM +0100, Mårten Lindahl kirjoitti:
> > 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.  
> 
> ...
> 
> > +	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
> > +	if (ret < 0)
> > +		goto out;
> > +
> > +	val = (u16)ret;  
> 
> Why do you need casting?
> 

Looks like these need a little more time.
Dropped from my tree for now.

Jonathan

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

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


On 1/15/23 18:41, andriy.shechenko@saunalahti.fi wrote:
> Tue, Jan 10, 2023 at 02:43:22PM +0100, Mårten Lindahl kirjoitti:
>> 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.
> ...
>
>> -		ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
>> -						      NULL,
>
>> +		ret = devm_iio_triggered_buffer_setup(&client->dev,
>> +						      indio_dev, NULL,
> What the point of this part of the hunk?

Hi Andy! If you mean why these two lines are changed, I blame Lindent. 
But yes, I will change it back to reduce the noise.

>
> ...
>
>> +	if (client->irq) {
>> +		if (data->chip_spec->irq_thread) {
> I have checked the rest of the series and found nothing that prevents this to
> be written as
>
> 	if (client->irq && data->chip_spec->irq_thread) {
>
> This will reduce the noise in the patch.

No problem. I see your point. I'll send a new version.

Thanks!

Kind regards

Mårten

>
>>   	}

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

* Re: [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-15 17:38   ` andriy.shechenko
  2023-01-16 17:32     ` Jonathan Cameron
@ 2023-01-16 23:19     ` Marten Lindahl
  1 sibling, 0 replies; 11+ messages in thread
From: Marten Lindahl @ 2023-01-16 23:19 UTC (permalink / raw)
  To: andriy.shechenko, Mårten Lindahl
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, kernel


On 1/15/23 18:38, andriy.shechenko@saunalahti.fi wrote:
> Tue, Jan 10, 2023 at 02:43:23PM +0100, Mårten Lindahl kirjoitti:
>> 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.
> ...
>
>> +	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
>> +	if (ret < 0)
>> +		goto out;
>> +
>> +	val = (u16)ret;
> Why do you need casting?

Hi Andy! I usually do this when assigning an int to a u16, since some 
compilers may complain about loosing precision.

In this case there is no such risk, so I guess it's fine to remove the 
cast. I can to that if you prefer so.

Kind regards

Mårten


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

* Re: [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040
  2023-01-16 17:32     ` Jonathan Cameron
@ 2023-01-16 23:22       ` Marten Lindahl
  0 siblings, 0 replies; 11+ messages in thread
From: Marten Lindahl @ 2023-01-16 23:22 UTC (permalink / raw)
  To: Jonathan Cameron, andriy.shechenko
  Cc: Mårten Lindahl, Lars-Peter Clausen, linux-iio, kernel


On 1/16/23 18:32, Jonathan Cameron wrote:
> On Sun, 15 Jan 2023 19:38:03 +0200
> andriy.shechenko@saunalahti.fi wrote:
>
>> Tue, Jan 10, 2023 at 02:43:23PM +0100, Mårten Lindahl kirjoitti:
>>> 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.
>> ...
>>
>>> +	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
>>> +	if (ret < 0)
>>> +		goto out;
>>> +
>>> +	val = (u16)ret;
>> Why do you need casting?
>>
> Looks like these need a little more time.
> Dropped from my tree for now.
>
> Jonathan

Hi Jonathan!

No problem. I'll send v4 shortly. Thanks!

Kind regards

Mårten


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

end of thread, other threads:[~2023-01-16 23:32 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-10 13:43 [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Mårten Lindahl
2023-01-10 13:43 ` [PATCH v3 1/3] iio: light: vcnl4000: Prepare for more generic setup Mårten Lindahl
2023-01-10 13:43 ` [PATCH v3 2/3] iio: light: vcnl4000: Make irq handling more generic Mårten Lindahl
2023-01-15 17:41   ` andriy.shechenko
2023-01-16 23:06     ` Marten Lindahl
2023-01-10 13:43 ` [PATCH v3 3/3] iio: light: vcnl4000: Add interrupt support for vcnl4040 Mårten Lindahl
2023-01-15 17:38   ` andriy.shechenko
2023-01-16 17:32     ` Jonathan Cameron
2023-01-16 23:22       ` Marten Lindahl
2023-01-16 23:19     ` Marten Lindahl
2023-01-14 16:47 ` [PATCH v3 0/3] iio: light: vcnl4000: Add vcnl4040 interrupt support Jonathan Cameron

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.