Skip to contentSkip to conversation

iio: light: noa1305: Simplify noa1305_read_raw()

PATCH In review Marek Vasut 2 participants 7 messages Jul 15, 2024 – Jul 20, 2024 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 iio: light: noa1305: Simplify noa1305_read_raw()

The only channel this hardware supports is IIO_LIGHT, if the channel is anything else, exit right away. The 'ret' variable is now always only assigned by noa1305_measure(), do not initialize it anymore. Update function parameter indent. No functional change.

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: [email protected]

---
 drivers/iio/light/noa1305.c | 37 +++++++++++++------------------------
 1 file changed, 13 insertions(+), 24 deletions(-)
drivers/iio/light/noa1305.c +13 -24
index 596cc48c4c341..a76f158bb50e5 100644
@@ -125,38 +125,27 @@ static const struct iio_chan_spec noa1305_channels[] = {
 };
 
 static int noa1305_read_raw(struct iio_dev *indio_dev,
-				struct iio_chan_spec const *chan,
-				int *val, int *val2, long mask)
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long mask)
 {
-	int ret = -EINVAL;
 	struct noa1305_priv *priv = iio_priv(indio_dev);
+	int ret;
+
+	if (chan->type != IIO_LIGHT)
+		return -EINVAL;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		switch (chan->type) {
-		case IIO_LIGHT:
-			ret = noa1305_measure(priv);
-			if (ret < 0)
-				return ret;
-			*val = ret;
-			return IIO_VAL_INT;
-		default:
-			break;
-		}
-		break;
+		ret = noa1305_measure(priv);
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		switch (chan->type) {
-		case IIO_LIGHT:
-			return noa1305_scale(priv, val, val2);
-		default:
-			break;
-		}
-		break;
+		return noa1305_scale(priv, val, val2);
 	default:
-		break;
+		return -EINVAL;
 	}
-
-	return ret;
 }
 
 static const struct iio_info noa1305_info = {
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/5 iio: light: noa1305: Assign val in noa1305_measure()

Make noa1305_measure() behave similar to noa1305_scale(), make it assign the 'val' output variable on success and return IIO_VAL_INT. This further simplifies noa1305_read_raw() and allows removal of ret variable altogether.

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: [email protected]

---
 drivers/iio/light/noa1305.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/iio/light/noa1305.c +5 -8
index a76f158bb50e5..a7e4c68bea09e 100644
@@ -48,7 +48,7 @@ struct noa1305_priv {
 	struct regmap *regmap;
 };
 
-static int noa1305_measure(struct noa1305_priv *priv)
+static int noa1305_measure(struct noa1305_priv *priv, int *val)
 {
 	__le16 data;
 	int ret;
@@ -58,7 +58,9 @@ static int noa1305_measure(struct noa1305_priv *priv)
 	if (ret < 0)
 		return ret;
 
-	return le16_to_cpu(data);
+	*val = le16_to_cpu(data);
+
+	return IIO_VAL_INT;
 }
 
 static int noa1305_scale(struct noa1305_priv *priv, int *val, int *val2)
@@ -129,18 +131,13 @@ static int noa1305_read_raw(struct iio_dev *indio_dev,
 			    int *val, int *val2, long mask)
 {
 	struct noa1305_priv *priv = iio_priv(indio_dev);
-	int ret;
 
 	if (chan->type != IIO_LIGHT)
 		return -EINVAL;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = noa1305_measure(priv);
-		if (ret < 0)
-			return ret;
-		*val = ret;
-		return IIO_VAL_INT;
+		return noa1305_measure(priv, val);
 	case IIO_CHAN_INFO_SCALE:
 		return noa1305_scale(priv, val, val2);
 	default:
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/5 iio: light: noa1305: Use static table lookup of scale values

In reply to Marek Vasut

Move scale values into a static table, perform look up of those scale values in noa1305_scale() simply by using the integration time register content as an index, because the integration time register content directly maps to the table values.

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: [email protected]

---
 drivers/iio/light/noa1305.c | 51 +++++++++++--------------------------
 1 file changed, 15 insertions(+), 36 deletions(-)
drivers/iio/light/noa1305.c +15 -36
index a7e4c68bea09e..202a5c1bbf798 100644
@@ -29,6 +29,7 @@
 #define   NOA1305_INTEGR_TIME_25MS	0x05
 #define   NOA1305_INTEGR_TIME_12_5MS	0x06
 #define   NOA1305_INTEGR_TIME_6_25MS	0x07
+#define   NOA1305_INTEGR_TIME_MASK	0x07
 #define NOA1305_REG_INT_SELECT		0x3
 #define   NOA1305_INT_SEL_ACTIVE_HIGH	0x01
 #define   NOA1305_INT_SEL_ACTIVE_LOW	0x02
@@ -43,6 +44,17 @@
 #define NOA1305_DEVICE_ID	0x0519
 #define NOA1305_DRIVER_NAME	"noa1305"
 
+static int noa1305_scale_available[] = {
+	100, 8 * 77,		/* 800 ms */
+	100, 4 * 77,		/* 400 ms */
+	100, 2 * 77,		/* 200 ms */
+	100, 1 * 77,		/* 100 ms */
+	1000, 5 * 77,		/* 50 ms */
+	10000, 25 * 77,		/* 25 ms */
+	100000, 125 * 77,	/* 12.5 ms */
+	1000000, 625 * 77,	/* 6.25 ms */
+};
+
 struct noa1305_priv {
 	struct i2c_client *client;
 	struct regmap *regmap;
@@ -78,42 +90,9 @@ static int noa1305_scale(struct noa1305_priv *priv, int *val, int *val2)
 	 * Integration Constant = 7.7
 	 * Integration Time in Seconds
 	 */
-	switch (data) {
-	case NOA1305_INTEGR_TIME_800MS:
-		*val = 100;
-		*val2 = 77 * 8;
-		break;
-	case NOA1305_INTEGR_TIME_400MS:
-		*val = 100;
-		*val2 = 77 * 4;
-		break;
-	case NOA1305_INTEGR_TIME_200MS:
-		*val = 100;
-		*val2 = 77 * 2;
-		break;
-	case NOA1305_INTEGR_TIME_100MS:
-		*val = 100;
-		*val2 = 77;
-		break;
-	case NOA1305_INTEGR_TIME_50MS:
-		*val = 1000;
-		*val2 = 77 * 5;
-		break;
-	case NOA1305_INTEGR_TIME_25MS:
-		*val = 10000;
-		*val2 = 77 * 25;
-		break;
-	case NOA1305_INTEGR_TIME_12_5MS:
-		*val = 100000;
-		*val2 = 77 * 125;
-		break;
-	case NOA1305_INTEGR_TIME_6_25MS:
-		*val = 1000000;
-		*val2 = 77 * 625;
-		break;
-	default:
-		return -EINVAL;
-	}
+	data &= NOA1305_INTEGR_TIME_MASK;
+	*val = noa1305_scale_available[2 * data + 0];
+	*val2 = noa1305_scale_available[2 * data + 1];
 
 	return IIO_VAL_FRACTIONAL;
 }
Signature · 1 line
2.43.0
#3 lore raw

PATCH 4/5 iio: light: noa1305: Report available scale values

In reply to Marek Vasut

Make use of the new static table of scale values and expose available scale values via sysfs attribute of the IIO device.

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: [email protected]

---
 drivers/iio/light/noa1305.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/iio/light/noa1305.c +21 -0
index 202a5c1bbf798..e778714a942a0 100644
@@ -102,9 +102,29 @@ static const struct iio_chan_spec noa1305_channels[] = {
 		.type = IIO_LIGHT,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE),
 	}
 };
 
+static int noa1305_read_avail(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      const int **vals, int *type,
+			      int *length, long mask)
+{
+	if (chan->type != IIO_LIGHT)
+		return -EINVAL;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		*vals = noa1305_scale_available;
+		*length = ARRAY_SIZE(noa1305_scale_available);
+		*type = IIO_VAL_FRACTIONAL;
+		return IIO_AVAIL_LIST;
+	default:
+		return -EINVAL;
+	}
+}
+
 static int noa1305_read_raw(struct iio_dev *indio_dev,
 			    struct iio_chan_spec const *chan,
 			    int *val, int *val2, long mask)
@@ -125,6 +145,7 @@ static int noa1305_read_raw(struct iio_dev *indio_dev,
 }
 
 static const struct iio_info noa1305_info = {
+	.read_avail = noa1305_read_avail,
 	.read_raw = noa1305_read_raw,
 };
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH 5/5 iio: light: noa1305: Make integration time configurable

In reply to Marek Vasut

Add another attribute which is the integration time. Report available integration time settings, support read of integration time currently configured in hardware, and support configuration of integration time into hardware.

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: [email protected]

---
 drivers/iio/light/noa1305.c | 61 +++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
drivers/iio/light/noa1305.c +61 -0
index e778714a942a0..da83425fd9100 100644
@@ -55,6 +55,17 @@ static int noa1305_scale_available[] = {
 	1000000, 625 * 77,	/* 6.25 ms */
 };
 
+static int noa1305_int_time_available[] = {
+	0, 800000,		/* 800 ms */
+	0, 400000,		/* 400 ms */
+	0, 200000,		/* 200 ms */
+	0, 100000,		/* 100 ms */
+	0, 50000,		/* 50 ms */
+	0, 25000,		/* 25 ms */
+	0, 12500,		/* 12.5 ms */
+	0, 6250,		/* 6.25 ms */
+};
+
 struct noa1305_priv {
 	struct i2c_client *client;
 	struct regmap *regmap;
@@ -97,12 +108,30 @@ static int noa1305_scale(struct noa1305_priv *priv, int *val, int *val2)
 	return IIO_VAL_FRACTIONAL;
 }
 
+static int noa1305_int_time(struct noa1305_priv *priv, int *val, int *val2)
+{
+	int data;
+	int ret;
+
+	ret = regmap_read(priv->regmap, NOA1305_REG_INTEGRATION_TIME, &data);
+	if (ret < 0)
+		return ret;
+
+	data &= NOA1305_INTEGR_TIME_MASK;
+	*val = noa1305_int_time_available[2 * data + 0];
+	*val2 = noa1305_int_time_available[2 * data + 1];
+
+	return IIO_VAL_INT_PLUS_MICRO;
+}
+
 static const struct iio_chan_spec noa1305_channels[] = {
 	{
 		.type = IIO_LIGHT,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
 		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
+		.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME),
 	}
 };
 
@@ -120,6 +149,11 @@ static int noa1305_read_avail(struct iio_dev *indio_dev,
 		*length = ARRAY_SIZE(noa1305_scale_available);
 		*type = IIO_VAL_FRACTIONAL;
 		return IIO_AVAIL_LIST;
+	case IIO_CHAN_INFO_INT_TIME:
+		*vals = noa1305_int_time_available;
+		*length = ARRAY_SIZE(noa1305_int_time_available);
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
 	default:
 		return -EINVAL;
 	}
@@ -139,14 +173,41 @@ static int noa1305_read_raw(struct iio_dev *indio_dev,
 		return noa1305_measure(priv, val);
 	case IIO_CHAN_INFO_SCALE:
 		return noa1305_scale(priv, val, val2);
+	case IIO_CHAN_INFO_INT_TIME:
+		return noa1305_int_time(priv, val, val2);
 	default:
 		return -EINVAL;
 	}
 }
 
+static int noa1305_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2, long mask)
+{
+	struct noa1305_priv *priv = iio_priv(indio_dev);
+	int i;
+
+	if (chan->type != IIO_LIGHT)
+		return -EINVAL;
+
+	if (mask != IIO_CHAN_INFO_INT_TIME)
+		return -EINVAL;
+
+	if (val)	/* >= 1s integration time not supported */
+		return -EINVAL;
+
+	/* Look up integration time register settings and write it if found. */
+	for (i = 0; i < ARRAY_SIZE(noa1305_int_time_available); i++)
+		if (noa1305_int_time_available[2 * i + 1] == val2)
+			return regmap_write(priv->regmap, NOA1305_REG_INTEGRATION_TIME, i);
+
+	return -EINVAL;
+}
+
 static const struct iio_info noa1305_info = {
 	.read_avail = noa1305_read_avail,
 	.read_raw = noa1305_read_raw,
+	.write_raw = noa1305_write_raw,
 };
 
 static bool noa1305_writable_reg(struct device *dev, unsigned int reg)
Signature · 1 line
2.43.0
maintainer
#5 lore raw
In reply to Marek Vasut
Quoted 6 lines · On Mon, 15 Jul 2024 20:28:55 +0200 Marek Vasut <[email protected]> wrote:

The only channel this hardware supports is IIO_LIGHT, if the channel is anything else, exit right away. The 'ret' variable is now always only assigned by noa1305_measure(), do not initialize it anymore. Update function parameter indent. No functional change.

Hi Marek,

One minor thing - please use a cover letter.
Provides an obvious place for me to reply when picking things up and gives the whole series a meaningful title in patchwork.

Anyhow, looks straight forward to me so I'll apply it to my current testing tree. Not that will get rebased on rc1 once available.
If anyone has additional feedback in the meantime I'm happy to rebase until this gets pushed out as togreg so can add tags or make other changes.

Thanks,

Jonathan

Quoted 65 lines

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: [email protected]

---
 drivers/iio/light/noa1305.c | 37 +++++++++++++------------------------
 1 file changed, 13 insertions(+), 24 deletions(-)
drivers/iio/light/noa1305.c +13 -24
index 596cc48c4c341..a76f158bb50e5 100644
@@ -125,38 +125,27 @@ static const struct iio_chan_spec noa1305_channels[] = {
 };
 
 static int noa1305_read_raw(struct iio_dev *indio_dev,
-				struct iio_chan_spec const *chan,
-				int *val, int *val2, long mask)
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long mask)
 {
-	int ret = -EINVAL;
 	struct noa1305_priv *priv = iio_priv(indio_dev);
+	int ret;
+
+	if (chan->type != IIO_LIGHT)
+		return -EINVAL;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		switch (chan->type) {
-		case IIO_LIGHT:
-			ret = noa1305_measure(priv);
-			if (ret < 0)
-				return ret;
-			*val = ret;
-			return IIO_VAL_INT;
-		default:
-			break;
-		}
-		break;
+		ret = noa1305_measure(priv);
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		switch (chan->type) {
-		case IIO_LIGHT:
-			return noa1305_scale(priv, val, val2);
-		default:
-			break;
-		}
-		break;
+		return noa1305_scale(priv, val, val2);
 	default:
-		break;
+		return -EINVAL;
 	}
-
-	return ret;
 }
 
 static const struct iio_info noa1305_info = {
#6 lore raw
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 15 Jul 2024 20:28:55 +0200 Marek Vasut <[email protected]> wrote:

The only channel this hardware supports is IIO_LIGHT, if the channel is anything else, exit right away. The 'ret' variable is now always only assigned by noa1305_measure(), do not initialize it anymore. Update function parameter indent. No functional change.

Quoted 6 lines
Quoted 1 line

Hi Marek,

One minor thing - please use a cover letter.
Provides an obvious place for me to reply when picking things up and gives the whole series a meaningful title in patchwork.

I will keep that in mind, thanks for the reminder.