diff mbox series

[v2,3/3] iio: chemical: atlas-ezo-sensor: add support for O2 sensor

Message ID 20200720070330.259954-4-matt.ranostay@konsulko.com (mailing list archive)
State New, archived
Headers show
Series iio: chemical: atlas-ezo-sensor: add support for O2 sensor | expand

Commit Message

Matt Ranostay July 20, 2020, 7:03 a.m. UTC
Add support for the Atlas EZO O2 chemical sensor which required
some refactoring of the driver and parsing of i2c transfer.

Sensor data is converted by the scaling value from percent to
IIO_CONCENTRATION.

Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
---
 drivers/iio/chemical/atlas-ezo-sensor.c | 74 ++++++++++++++++++-------
 1 file changed, 55 insertions(+), 19 deletions(-)

Comments

Andy Shevchenko July 21, 2020, 6:46 p.m. UTC | #1
On Mon, Jul 20, 2020 at 10:05 AM Matt Ranostay
<matt.ranostay@konsulko.com> wrote:
>
> Add support for the Atlas EZO O2 chemical sensor which required
> some refactoring of the driver and parsing of i2c transfer.
>
> Sensor data is converted by the scaling value from percent to
> IIO_CONCENTRATION.

...

> +static void atlas_ezo_sanitize(char *buf)
> +{
> +       char *ptr = strchr(buf, '.');
> +
> +       if (!ptr)
> +               return;
> +
> +       for (; *ptr; ptr++)
> +               *ptr = *(ptr + 1);
> +}

NIH of memmove()? Why?

And actually to avoid strlen() you may do it other way around, i.e
shift integer part one character right and return new buffer pointer.


if (!ptr)
  return buf;

memmove(buf + 1, buf, ptr - buf);
return buf + 1;
Andy Shevchenko July 21, 2020, 6:47 p.m. UTC | #2
On Tue, Jul 21, 2020 at 9:46 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> On Mon, Jul 20, 2020 at 10:05 AM Matt Ranostay
> <matt.ranostay@konsulko.com> wrote:

...

> memmove(buf + 1, buf, ptr - buf);

ptr - buf - 1 (off-by-one classic! But double check)
Matt Ranostay July 22, 2020, 4:38 a.m. UTC | #3
On Tue, Jul 21, 2020 at 11:46 AM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Mon, Jul 20, 2020 at 10:05 AM Matt Ranostay
> <matt.ranostay@konsulko.com> wrote:
> >
> > Add support for the Atlas EZO O2 chemical sensor which required
> > some refactoring of the driver and parsing of i2c transfer.
> >
> > Sensor data is converted by the scaling value from percent to
> > IIO_CONCENTRATION.
>
> ...
>
> > +static void atlas_ezo_sanitize(char *buf)
> > +{
> > +       char *ptr = strchr(buf, '.');
> > +
> > +       if (!ptr)
> > +               return;
> > +
> > +       for (; *ptr; ptr++)
> > +               *ptr = *(ptr + 1);
> > +}
>
> NIH of memmove()? Why?

Mainly since I forgot that POSIX function. I'll fix it up when possible

- Matt

>
> And actually to avoid strlen() you may do it other way around, i.e
> shift integer part one character right and return new buffer pointer.
>
>
> if (!ptr)
>   return buf;
>
> memmove(buf + 1, buf, ptr - buf);
> return buf + 1;
>
> --
> With Best Regards,
> Andy Shevchenko
Jonathan Cameron July 22, 2020, 1:39 p.m. UTC | #4
On Tue, 21 Jul 2020 21:38:11 -0700
Matt Ranostay <matt.ranostay@konsulko.com> wrote:

> On Tue, Jul 21, 2020 at 11:46 AM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> >
> > On Mon, Jul 20, 2020 at 10:05 AM Matt Ranostay
> > <matt.ranostay@konsulko.com> wrote:  
> > >
> > > Add support for the Atlas EZO O2 chemical sensor which required
> > > some refactoring of the driver and parsing of i2c transfer.
> > >
> > > Sensor data is converted by the scaling value from percent to
> > > IIO_CONCENTRATION.  
> >
> > ...
> >  
> > > +static void atlas_ezo_sanitize(char *buf)
> > > +{
> > > +       char *ptr = strchr(buf, '.');
> > > +
> > > +       if (!ptr)
> > > +               return;
> > > +
> > > +       for (; *ptr; ptr++)
> > > +               *ptr = *(ptr + 1);
> > > +}  
> >
> > NIH of memmove()? Why?  
> 
> Mainly since I forgot that POSIX function. I'll fix it up when possible
> 
> - Matt

I've dropped the series for now.  Lets just have a v3 with these
bits tidied up. It will have to wait for the next cycle however as I'll
be doing what is probably the last pull for this cycle in a few minutes.

Thanks,

Jonathan

> 
> >
> > And actually to avoid strlen() you may do it other way around, i.e
> > shift integer part one character right and return new buffer pointer.
> >
> >
> > if (!ptr)
> >   return buf;
> >
> > memmove(buf + 1, buf, ptr - buf);
> > return buf + 1;
> >
> > --
> > With Best Regards,
> > Andy Shevchenko
diff mbox series

Patch

diff --git a/drivers/iio/chemical/atlas-ezo-sensor.c b/drivers/iio/chemical/atlas-ezo-sensor.c
index a94eb8a6bf32..94188c84e65f 100644
--- a/drivers/iio/chemical/atlas-ezo-sensor.c
+++ b/drivers/iio/chemical/atlas-ezo-sensor.c
@@ -16,10 +16,11 @@ 
 #include <linux/iio/iio.h>
 
 #define ATLAS_EZO_DRV_NAME		"atlas-ezo-sensor"
-#define ATLAS_CO2_INT_TIME_IN_MS	950
+#define ATLAS_INT_TIME_IN_MS		950
 
 enum {
 	ATLAS_CO2_EZO,
+	ATLAS_O2_EZO,
 };
 
 struct atlas_ezo_device {
@@ -38,31 +39,54 @@  struct atlas_ezo_data {
 	u8 buffer[8];
 };
 
+#define ATLAS_CONCENTRATION_CHANNEL(_modifier) \
+	{ \
+		.type = IIO_CONCENTRATION, \
+		.modified = 1,\
+		.channel2 = _modifier, \
+		.info_mask_separate = \
+			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
+		.scan_index = 0, \
+		.scan_type =  { \
+			.sign = 'u', \
+			.realbits = 32, \
+			.storagebits = 32, \
+			.endianness = IIO_CPU, \
+		}, \
+	}
+
 static const struct iio_chan_spec atlas_co2_ezo_channels[] = {
-	{
-		.type = IIO_CONCENTRATION,
-		.modified = 1,
-		.channel2 = IIO_MOD_CO2,
-		.info_mask_separate =
-			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
-		.scan_index = 0,
-		.scan_type = {
-			.sign = 'u',
-			.realbits = 32,
-			.storagebits = 32,
-			.endianness = IIO_CPU,
-		},
-	},
+	ATLAS_CONCENTRATION_CHANNEL(IIO_MOD_CO2),
+};
+
+static const struct iio_chan_spec atlas_o2_ezo_channels[] = {
+	ATLAS_CONCENTRATION_CHANNEL(IIO_MOD_O2),
 };
 
 static struct atlas_ezo_device atlas_ezo_devices[] = {
 	[ATLAS_CO2_EZO] = {
 		.channels = atlas_co2_ezo_channels,
 		.num_channels = 1,
-		.delay = ATLAS_CO2_INT_TIME_IN_MS,
+		.delay = ATLAS_INT_TIME_IN_MS,
 	},
+	[ATLAS_O2_EZO] = {
+		.channels = atlas_o2_ezo_channels,
+		.num_channels = 1,
+		.delay = ATLAS_INT_TIME_IN_MS,
+	}
 };
 
+static void atlas_ezo_sanitize(char *buf)
+{
+	char *ptr = strchr(buf, '.');
+
+	if (!ptr)
+		return;
+
+	for (; *ptr; ptr++)
+		*ptr = *(ptr + 1);
+}
+
 static int atlas_ezo_read_raw(struct iio_dev *indio_dev,
 			  struct iio_chan_spec const *chan,
 			  int *val, int *val2, long mask)
@@ -96,6 +120,9 @@  static int atlas_ezo_read_raw(struct iio_dev *indio_dev,
 			return -EBUSY;
 		}
 
+		/* removing floating point for fixed number representation */
+		atlas_ezo_sanitize(data->buffer + 2);
+
 		ret = kstrtol(data->buffer + 1, 10, &tmp);
 
 		*val = tmp;
@@ -105,9 +132,16 @@  static int atlas_ezo_read_raw(struct iio_dev *indio_dev,
 		return ret ? ret : IIO_VAL_INT;
 	}
 	case IIO_CHAN_INFO_SCALE:
-		*val = 0;
-		*val2 = 100; /* 0.0001 */
-		return IIO_VAL_INT_PLUS_MICRO;
+		switch (chan->channel2) {
+		case IIO_MOD_CO2:
+			*val = 0;
+			*val2 = 100; /* 0.0001 */
+			return IIO_VAL_INT_PLUS_MICRO;
+		case IIO_MOD_O2:
+			*val = 100;
+			return IIO_VAL_INT;
+		}
+		return -EINVAL;
 	}
 
 	return 0;
@@ -119,12 +153,14 @@  static const struct iio_info atlas_info = {
 
 static const struct i2c_device_id atlas_ezo_id[] = {
 	{ "atlas-co2-ezo", ATLAS_CO2_EZO },
+	{ "atlas-o2-ezo", ATLAS_O2_EZO },
 	{}
 };
 MODULE_DEVICE_TABLE(i2c, atlas_ezo_id);
 
 static const struct of_device_id atlas_ezo_dt_ids[] = {
 	{ .compatible = "atlas,co2-ezo", .data = (void *)ATLAS_CO2_EZO, },
+	{ .compatible = "atlas,o2-ezo", .data = (void *)ATLAS_O2_EZO, },
 	{}
 };
 MODULE_DEVICE_TABLE(of, atlas_ezo_dt_ids);