Re: [PATCH 3/3] iio: light: vcnl4000: Add ps_it attributes for vcnl4040

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Mårten,

Le mar., sept. 20 2022 at 20:09:58 +0200, Mårten Lindahl <marten.lindahl@xxxxxxxx> a écrit :
Add read/write attribute for proximity integration time, and a read
attribute for available integration times for the vcnl4040 chip.

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

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 9838f0868372..7a207e48335d 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -76,6 +76,8 @@

#define VCNL4040_ALS_CONF_ALS_SD BIT(0) /* Enable ambient light sensor */
 #define VCNL4040_PS_CONF1_PS_SD	BIT(0) /* Enable proximity sensor */
+#define VCNL4040_PS_CONF2_PS_IT \
+	(BIT(3) | BIT(2) | BIT(1)) /* Proximity integration time */

Use the GENMASK() macro.


 /* Bit masks for interrupt registers. */
#define VCNL4010_INT_THR_SEL BIT(0) /* Select threshold interrupt source */ @@ -103,6 +105,16 @@ static const int vcnl4010_prox_sampling_frequency[][2] = {
 	{125, 0},
 	{250, 0},
 };
+static const int vcnl4040_ps_it_times[][2] = {
+	{0, 100},
+	{0, 150},
+	{0, 200},
+	{0, 250},
+	{0, 300},
+	{0, 350},
+	{0, 400},
+	{0, 800},
+};

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

@@ -486,6 +498,49 @@ static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on)
 	return ret;
 }

+static int vcnl4040_read_ps_it(struct vcnl4000_data *data, int *val, int *val2)
+{
+	int ret;
+
+	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
+	if (ret < 0)
+		return ret;
+
+	ret = (ret & VCNL4040_PS_CONF2_PS_IT) >> 1;

Use the FIELD_GET() macro.

+
+	if (ret >= ARRAY_SIZE(vcnl4040_ps_it_times))
+		return -EINVAL;
+
+	*val = vcnl4040_ps_it_times[ret][0];
+	*val2 = vcnl4040_ps_it_times[ret][1];
+
+	return 0;
+}
+
+static ssize_t vcnl4040_write_ps_it(struct vcnl4000_data *data, int val)
+{
+	unsigned int i;
+	int ret, index = -1;
+
+	for (i = 0; i < ARRAY_SIZE(vcnl4040_ps_it_times); i++) {
+		if (val == vcnl4040_ps_it_times[i][1]) {
+			index = i;
+			break;
+		}
+	}
+
+	if (index < 0)
+		return -EINVAL;
+
+	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
+	if (ret < 0)
+		return ret;
+
+	ret = (ret & ~VCNL4040_PS_CONF2_PS_IT) | (index << 1);

Use the FIELD_PREP() macro.

+
+ return i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, ret);
+}
+
 static int vcnl4000_read_raw(struct iio_dev *indio_dev,
 				struct iio_chan_spec const *chan,
 				int *val, int *val2, long mask)
@@ -533,6 +588,13 @@ static int vcnl4000_read_raw(struct iio_dev *indio_dev,
 		default:
 			return -EINVAL;
 		}
+	case IIO_CHAN_INFO_INT_TIME:
+		if (chan->type != IIO_PROXIMITY)
+			return -EINVAL;
+		ret = vcnl4040_read_ps_it(data, val, val2);
+		if (ret < 0)
+			return ret;
+		return IIO_VAL_INT_PLUS_MICRO;
 	default:
 		return -EINVAL;
 	}
@@ -554,6 +616,12 @@ static int vcnl4040_write_raw(struct iio_dev *indio_dev,
 		default:
 			return -EINVAL;
 		}
+	case IIO_CHAN_INFO_INT_TIME:
+		if (val != 0)
+			return -EINVAL;
+		if (chan->type != IIO_PROXIMITY)
+			return -EINVAL;
+		return vcnl4040_write_ps_it(data, val2);
 	default:
 		return -EINVAL;
 	}
@@ -900,11 +968,23 @@ static const struct iio_chan_spec vcnl4040_channels[] = {
 	}, {
 		.type = IIO_PROXIMITY,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
-			BIT(IIO_CHAN_INFO_ENABLE),
+			BIT(IIO_CHAN_INFO_ENABLE) | BIT(IIO_CHAN_INFO_INT_TIME),
 		.ext_info = vcnl4000_ext_info,
 	}
 };

+static IIO_CONST_ATTR(in_proximity_integration_time_available,
+ "0.000100 0.000150 0.000200 0.000250 0.000300 0.000350 0.000400 0.000800");

Hmm, this is not how you define a "_available" attribute.

You need to add a
.info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME)

to your iio_chan_spec, then implement the .read_avail callback in your iio_info structure.

Cheers,
-Paul

+
+static struct attribute *vcnl4040_attributes[] = {
+	&iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group vcnl4040_attribute_group = {
+	.attrs = vcnl4040_attributes,
+};
+
 static const struct iio_info vcnl4000_info = {
 	.read_raw = vcnl4000_read_raw,
 };
@@ -922,6 +1002,7 @@ static const struct iio_info vcnl4010_info = {
 static const struct iio_info vcnl4040_info = {
 	.read_raw = vcnl4000_read_raw,
 	.write_raw = vcnl4040_write_raw,
+	.attrs = &vcnl4040_attribute_group,
 };

 static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
--
2.30.2







[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux