Linux IIO development
 help / color / mirror / Atom feed
From: "Mårten Lindahl" <marten.lindahl@axis.com>
To: Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>
Cc: "Paul Cercueil" <paul@crapouillou.net>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	linux-iio@vger.kernel.org, kernel@axis.com,
	"Mårten Lindahl" <marten.lindahl@axis.com>
Subject: [PATCH 2/3] iio: light: vcnl4000: Add enable attributes for vcnl4040
Date: Tue, 20 Sep 2022 20:09:57 +0200	[thread overview]
Message-ID: <20220920180958.2308229-3-marten.lindahl@axis.com> (raw)
In-Reply-To: <20220920180958.2308229-1-marten.lindahl@axis.com>

Add channel attribute in_illuminance_en and in_proximity_en with
read/write access for vcnl4040. If automatic runtime power management is
turned off (power/control = on), both sensors can be kept on or off by
userspace.

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

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 0b226c684957..9838f0868372 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -125,6 +125,9 @@ struct vcnl4000_data {
 	enum vcnl4000_device_ids id;
 	int rev;
 	int al_scale;
+	bool als_enable;
+	bool ps_enable;
+
 	const struct vcnl4000_chip_spec *chip_spec;
 	struct mutex vcnl4000_lock;
 	struct vcnl4200_channel vcnl4200_al;
@@ -202,10 +205,13 @@ static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, int val)
 		if (ret < 0)
 			return ret;
 
-		if (val)
+		if (val) {
 			ret &= ~VCNL4040_ALS_CONF_ALS_SD;
-		else
+			data->als_enable = true;
+		} else {
 			ret |= VCNL4040_ALS_CONF_ALS_SD;
+			data->als_enable = false;
+		}
 
 		return i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF,
 						 ret);
@@ -225,10 +231,13 @@ static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, int val)
 		if (ret < 0)
 			return ret;
 
-		if (val)
+		if (val) {
 			ret &= ~VCNL4040_PS_CONF1_PS_SD;
-		else
+			data->ps_enable = true;
+		} else {
 			ret |= VCNL4040_PS_CONF1_PS_SD;
+			data->ps_enable = false;
+		}
 
 		return i2c_smbus_write_word_data(data->client,
 						 VCNL4200_PS_CONF1, ret);
@@ -283,6 +292,8 @@ static int vcnl4200_init(struct vcnl4000_data *data)
 	dev_dbg(&data->client->dev, "device id 0x%x", id);
 
 	data->rev = (ret >> 8) & 0xf;
+	data->als_enable = false;
+	data->ps_enable = false;
 
 	data->vcnl4200_al.reg = VCNL4200_AL_DATA;
 	data->vcnl4200_ps.reg = VCNL4200_PS_DATA;
@@ -459,8 +470,12 @@ static bool vcnl4010_is_in_periodic_mode(struct vcnl4000_data *data)
 static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on)
 {
 	struct device *dev = &data->client->dev;
+	struct iio_dev *indio_dev = i2c_get_clientdata(data->client);
 	int ret;
 
+	if (!indio_dev->dev.power.runtime_auto)
+		return 0;
+
 	if (on) {
 		ret = pm_runtime_resume_and_get(dev);
 	} else {
@@ -507,6 +522,38 @@ static int vcnl4000_read_raw(struct iio_dev *indio_dev,
 		*val = 0;
 		*val2 = data->al_scale;
 		return IIO_VAL_INT_PLUS_MICRO;
+	case IIO_CHAN_INFO_ENABLE:
+		switch (chan->type) {
+		case IIO_LIGHT:
+			*val = data->als_enable;
+			return IIO_VAL_INT;
+		case IIO_PROXIMITY:
+			*val = data->ps_enable;
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vcnl4040_write_raw(struct iio_dev *indio_dev,
+				struct iio_chan_spec const *chan,
+				int val, int val2, long mask)
+{
+	struct vcnl4000_data *data = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_ENABLE:
+		switch (chan->type) {
+		case IIO_LIGHT:
+			return vcnl4000_write_als_enable(data, val);
+		case IIO_PROXIMITY:
+			return vcnl4000_write_ps_enable(data, val);
+		default:
+			return -EINVAL;
+		}
 	default:
 		return -EINVAL;
 	}
@@ -845,6 +892,19 @@ static const struct iio_chan_spec vcnl4010_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(1),
 };
 
+static const struct iio_chan_spec vcnl4040_channels[] = {
+	{
+		.type = IIO_LIGHT,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_ENABLE),
+	}, {
+		.type = IIO_PROXIMITY,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+			BIT(IIO_CHAN_INFO_ENABLE),
+		.ext_info = vcnl4000_ext_info,
+	}
+};
+
 static const struct iio_info vcnl4000_info = {
 	.read_raw = vcnl4000_read_raw,
 };
@@ -859,6 +919,11 @@ static const struct iio_info vcnl4010_info = {
 	.write_event_config = vcnl4010_write_event_config,
 };
 
+static const struct iio_info vcnl4040_info = {
+	.read_raw = vcnl4000_read_raw,
+	.write_raw = vcnl4040_write_raw,
+};
+
 static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
 	[VCNL4000] = {
 		.prod = "VCNL4000",
@@ -888,9 +953,9 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
 		.measure_light = vcnl4200_measure_light,
 		.measure_proximity = vcnl4200_measure_proximity,
 		.set_power_state = vcnl4200_set_power_state,
-		.channels = vcnl4000_channels,
-		.num_channels = ARRAY_SIZE(vcnl4000_channels),
-		.info = &vcnl4000_info,
+		.channels = vcnl4040_channels,
+		.num_channels = ARRAY_SIZE(vcnl4040_channels),
+		.info = &vcnl4040_info,
 		.irq_support = false,
 	},
 	[VCNL4200] = {
-- 
2.30.2


  parent reply	other threads:[~2022-09-20 18:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-20 18:09 [PATCH 0/3] Add basic attributes for vcnl4040 Mårten Lindahl
2022-09-20 18:09 ` [PATCH 1/3] iio: light: vcnl4000: Preserve conf bits when toggle power Mårten Lindahl
2022-09-20 22:23   ` Paul Cercueil
2022-09-22 12:18     ` Marten Lindahl
2022-09-20 18:09 ` Mårten Lindahl [this message]
2022-09-20 22:01   ` [PATCH 2/3] iio: light: vcnl4000: Add enable attributes for vcnl4040 Paul Cercueil
2022-09-22 13:04     ` Marten Lindahl
2022-09-22 14:10       ` Paul Cercueil
2022-09-22 18:39         ` Marten Lindahl
2022-09-20 18:09 ` [PATCH 3/3] iio: light: vcnl4000: Add ps_it " Mårten Lindahl
2022-09-20 22:12   ` Paul Cercueil
2022-09-22 13:31     ` Marten Lindahl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220920180958.2308229-3-marten.lindahl@axis.com \
    --to=marten.lindahl@axis.com \
    --cc=jic23@kernel.org \
    --cc=kernel@axis.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=paul@crapouillou.net \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox