Skip to contentSkip to conversation

atlas-ph-sensor: add conductivity sensor support

PATCH Superseded Matt Ranostay 3 participants 7 messages May 20, 2016 – May 24, 2016

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover atlas-ph-sensor: add conductivity sensor support

Changes from v1:
* Add IIO_ELECTRICALCONDUCTIVITY type rather than do doing weird conversion to ppm
* move device tree mapping table above probe for of_match_device

Matt Ranostay (3):
iio: chemical: atlas-ph-sensor: reorg driver to allow multiple chips
iio: electricalconductivity: add IIO_ELECTRICALCONDUCTIVITY type
iio: chemical: atlas-ph-sensor: add EC feature

 Documentation/ABI/testing/sysfs-bus-iio            |   7 +
 .../bindings/iio/chemical/atlas,ec-sm.txt          |  22 ++
 drivers/iio/chemical/Kconfig                       |   8 +-
 drivers/iio/chemical/atlas-ph-sensor.c             | 257 ++++++++++++++++-----
 drivers/iio/industrialio-core.c                    |   1 +
 include/uapi/linux/iio/types.h                     |   1 +
 6 files changed, 236 insertions(+), 60 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/atlas,ec-sm.txt
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/3 iio: chemical: atlas-ph-sensor: reorg driver to allow multiple chips

---
 drivers/iio/chemical/atlas-ph-sensor.c | 145 ++++++++++++++++++++-------------
 1 file changed, 90 insertions(+), 55 deletions(-)
drivers/iio/chemical/atlas-ph-sensor.c +90 -55
index 62b37cd..e85477c 100644
@@ -24,6 +24,7 @@
 #include <linux/irq_work.h>
 #include <linux/gpio.h>
 #include <linux/i2c.h>
+#include <linux/of_device.h>
 #include <linux/regmap.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/buffer.h>
@@ -43,20 +44,25 @@
 
 #define ATLAS_REG_PWR_CONTROL		0x06
 
-#define ATLAS_REG_CALIB_STATUS		0x0d
-#define ATLAS_REG_CALIB_STATUS_MASK	0x07
-#define ATLAS_REG_CALIB_STATUS_LOW	BIT(0)
-#define ATLAS_REG_CALIB_STATUS_MID	BIT(1)
-#define ATLAS_REG_CALIB_STATUS_HIGH	BIT(2)
+#define ATLAS_REG_PH_CALIB_STATUS	0x0d
+#define ATLAS_REG_PH_CALIB_STATUS_MASK	0x07
+#define ATLAS_REG_PH_CALIB_STATUS_LOW	BIT(0)
+#define ATLAS_REG_PH_CALIB_STATUS_MID	BIT(1)
+#define ATLAS_REG_PH_CALIB_STATUS_HIGH	BIT(2)
 
-#define ATLAS_REG_TEMP_DATA		0x0e
+#define ATLAS_REG_PH_TEMP_DATA		0x0e
 #define ATLAS_REG_PH_DATA		0x16
 
 #define ATLAS_PH_INT_TIME_IN_US		450000
 
+enum {
+	ATLAS_PH_SM,
+};
+
 struct atlas_data {
 	struct i2c_client *client;
 	struct iio_trigger *trig;
+	struct atlas_device *chip;
 	struct regmap *regmap;
 	struct irq_work work;
 
@@ -84,9 +90,10 @@ static const struct regmap_config atlas_regmap_config = {
 	.cache_type = REGCACHE_RBTREE,
 };
 
-static const struct iio_chan_spec atlas_channels[] = {
+static const struct iio_chan_spec atlas_ph_channels[] = {
 	{
 		.type = IIO_PH,
+		.address = ATLAS_REG_PH_DATA,
 		.info_mask_separate =
 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
 		.scan_index = 0,
@@ -100,7 +107,7 @@ static const struct iio_chan_spec atlas_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(1),
 	{
 		.type = IIO_TEMP,
-		.address = ATLAS_REG_TEMP_DATA,
+		.address = ATLAS_REG_PH_TEMP_DATA,
 		.info_mask_separate =
 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
 		.output = 1,
@@ -108,6 +115,52 @@ static const struct iio_chan_spec atlas_channels[] = {
 	},
 };
 
+static int atlas_check_ph_calibration(struct atlas_data *data)
+{
+	struct device *dev = &data->client->dev;
+	int ret;
+	unsigned int val;
+
+	ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val);
+	if (ret)
+		return ret;
+
+	if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) {
+		dev_warn(dev, "device has not been calibrated\n");
+		return 0;
+	}
+
+	if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW))
+		dev_warn(dev, "device missing low point calibration\n");
+
+	if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID))
+		dev_warn(dev, "device missing mid point calibration\n");
+
+	if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH))
+		dev_warn(dev, "device missing high point calibration\n");
+
+	return 0;
+}
+
+struct atlas_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+	int data_reg;
+
+	int (*calibration)(struct atlas_data *data);
+	int delay;
+};
+
+static struct atlas_device atlas_devices[] = {
+	[ATLAS_PH_SM] = {
+				.channels = atlas_ph_channels,
+				.num_channels = 3,
+				.data_reg = ATLAS_REG_PH_DATA,
+				.calibration = &atlas_check_ph_calibration,
+				.delay = ATLAS_PH_INT_TIME_IN_US,
+	},
+};
+
 static int atlas_set_powermode(struct atlas_data *data, int on)
 {
 	return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
@@ -178,8 +231,9 @@ static irqreturn_t atlas_trigger_handler(int irq, void *private)
 	struct atlas_data *data = iio_priv(indio_dev);
 	int ret;
 
-	ret = regmap_bulk_read(data->regmap, ATLAS_REG_PH_DATA,
-			      (u8 *) &data->buffer, sizeof(data->buffer[0]));
+	ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
+			      (u8 *) &data->buffer,
+			      sizeof(__be32) * (data->chip->num_channels - 2));
 
 	if (!ret)
 		iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
@@ -200,7 +254,7 @@ static irqreturn_t atlas_interrupt_handler(int irq, void *private)
 	return IRQ_HANDLED;
 }
 
-static int atlas_read_ph_measurement(struct atlas_data *data, __be32 *val)
+static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
 {
 	struct device *dev = &data->client->dev;
 	int suspended = pm_runtime_suspended(dev);
@@ -213,11 +267,9 @@ static int atlas_read_ph_measurement(struct atlas_data *data, __be32 *val)
 	}
 
 	if (suspended)
-		usleep_range(ATLAS_PH_INT_TIME_IN_US,
-			     ATLAS_PH_INT_TIME_IN_US + 100000);
+		usleep_range(data->chip->delay, data->chip->delay + 100000);
 
-	ret = regmap_bulk_read(data->regmap, ATLAS_REG_PH_DATA,
-			      (u8 *) val, sizeof(*val));
+	ret = regmap_bulk_read(data->regmap, reg, (u8 *) val, sizeof(*val));
 
 	pm_runtime_mark_last_busy(dev);
 	pm_runtime_put_autosuspend(dev);
@@ -247,7 +299,8 @@ static int atlas_read_raw(struct iio_dev *indio_dev,
 			if (iio_buffer_enabled(indio_dev))
 				ret = -EBUSY;
 			else
-				ret = atlas_read_ph_measurement(data, &reg);
+				ret = atlas_read_measurement(data,
+							chan->address, &reg);
 
 			mutex_unlock(&indio_dev->mlock);
 			break;
@@ -303,37 +356,24 @@ static const struct iio_info atlas_info = {
 	.write_raw = atlas_write_raw,
 };
 
-static int atlas_check_calibration(struct atlas_data *data)
-{
-	struct device *dev = &data->client->dev;
-	int ret;
-	unsigned int val;
-
-	ret = regmap_read(data->regmap, ATLAS_REG_CALIB_STATUS, &val);
-	if (ret)
-		return ret;
-
-	if (!(val & ATLAS_REG_CALIB_STATUS_MASK)) {
-		dev_warn(dev, "device has not been calibrated\n");
-		return 0;
-	}
-
-	if (!(val & ATLAS_REG_CALIB_STATUS_LOW))
-		dev_warn(dev, "device missing low point calibration\n");
-
-	if (!(val & ATLAS_REG_CALIB_STATUS_MID))
-		dev_warn(dev, "device missing mid point calibration\n");
-
-	if (!(val & ATLAS_REG_CALIB_STATUS_HIGH))
-		dev_warn(dev, "device missing high point calibration\n");
+static const struct i2c_device_id atlas_id[] = {
+	{ "atlas-ph-sm", ATLAS_PH_SM},
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, atlas_id);
 
-	return 0;
+static const struct of_device_id atlas_dt_ids[] = {
+	{ .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, },
+	{ }
 };
+MODULE_DEVICE_TABLE(of, atlas_dt_ids);
 
 static int atlas_probe(struct i2c_client *client,
 		       const struct i2c_device_id *id)
 {
 	struct atlas_data *data;
+	struct atlas_device *chip;
+	const struct of_device_id *of_id;
 	struct iio_trigger *trig;
 	struct iio_dev *indio_dev;
 	int ret;
@@ -342,10 +382,16 @@ static int atlas_probe(struct i2c_client *client,
 	if (!indio_dev)
 		return -ENOMEM;
 
+	of_id = of_match_device(atlas_dt_ids, &client->dev);
+	if (!of_id)
+		chip = &atlas_devices[id->driver_data];
+	else
+		chip = &atlas_devices[(unsigned long)of_id->data];
+
 	indio_dev->info = &atlas_info;
 	indio_dev->name = ATLAS_DRV_NAME;
-	indio_dev->channels = atlas_channels;
-	indio_dev->num_channels = ARRAY_SIZE(atlas_channels);
+	indio_dev->channels = chip->channels;
+	indio_dev->num_channels = chip->num_channels;
 	indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
 	indio_dev->dev.parent = &client->dev;
 
@@ -358,6 +404,7 @@ static int atlas_probe(struct i2c_client *client,
 	data = iio_priv(indio_dev);
 	data->client = client;
 	data->trig = trig;
+	data->chip = chip;
 	trig->dev.parent = indio_dev->dev.parent;
 	trig->ops = &atlas_interrupt_trigger_ops;
 	iio_trigger_set_drvdata(trig, indio_dev);
@@ -379,7 +426,7 @@ static int atlas_probe(struct i2c_client *client,
 		return -EINVAL;
 	}
 
-	ret = atlas_check_calibration(data);
+	ret = chip->calibration(data);
 	if (ret)
 		return ret;
 
@@ -480,18 +527,6 @@ static const struct dev_pm_ops atlas_pm_ops = {
 			   atlas_runtime_resume, NULL)
 };
 
-static const struct i2c_device_id atlas_id[] = {
-	{ "atlas-ph-sm", 0 },
-	{}
-};
-MODULE_DEVICE_TABLE(i2c, atlas_id);
-
-static const struct of_device_id atlas_dt_ids[] = {
-	{ .compatible = "atlas,ph-sm" },
-	{ }
-};
-MODULE_DEVICE_TABLE(of, atlas_dt_ids);
-
 static struct i2c_driver atlas_driver = {
 	.driver = {
 		.name	= ATLAS_DRV_NAME,
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/3 iio: electricalconductivity: add IIO_ELECTRICALCONDUCTIVITY type

In reply to Matt Ranostay
---
 Documentation/ABI/testing/sysfs-bus-iio | 7 +++++++
 drivers/iio/industrialio-core.c         | 1 +
 include/uapi/linux/iio/types.h          | 1 +
 3 files changed, 9 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +7 -0
index df44998..adfdf95 100644
@@ -1565,3 +1565,10 @@ Description:
 		* X is in the plane of the propellers, perpendicular to Y axis,
 		  and positive towards the starboard side of the UAV ;
 		* Z is perpendicular to propellers plane and positive upwards.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_electricalconductivity_raw
+KernelVersion:	4.8
+Contact:	[email protected]
+Description:
+		Raw (unscaled no offset etc.) electric conductivity reading that
+		maps to microsiemens per centimeter.
drivers/iio/industrialio-core.c +1 -0
index e6319a9..2a85bd8 100644
@@ -80,6 +80,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_RESISTANCE] = "resistance",
 	[IIO_PH] = "ph",
 	[IIO_UVINDEX] = "uvindex",
+	[IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
 };
 
 static const char * const iio_modifier_names[] = {
include/uapi/linux/iio/types.h +1 -0
index b0916fc..22e5e58 100644
@@ -39,6 +39,7 @@ enum iio_chan_type {
 	IIO_RESISTANCE,
 	IIO_PH,
 	IIO_UVINDEX,
+	IIO_ELECTRICALCONDUCTIVITY,
 };
 
 enum iio_modifier {
Signature · 1 line
2.7.4
#6 lore raw

PATCH v2 3/3 iio: chemical: atlas-ph-sensor: add EC feature

In reply to Matt Ranostay
---
 .../bindings/iio/chemical/atlas,ec-sm.txt          |  22 ++++
 drivers/iio/chemical/Kconfig                       |   8 +-
 drivers/iio/chemical/atlas-ph-sensor.c             | 117 ++++++++++++++++++++-
 3 files changed, 142 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/atlas,ec-sm.txt
Documentation/devicetree/bindings/iio/chemical/atlas,ec-sm.txt new file +22 -0
new file mode 100644
index 0000000..2962bd9
@@ -0,0 +1,22 @@
+* Atlas Scientific EC-SM OEM sensor
+
+http://www.atlas-scientific.com/_files/_datasheets/_oem/EC_oem_datasheet.pdf
+
+Required properties:
+
+  - compatible: must be "atlas,ec-sm"
+  - reg: the I2C address of the sensor
+  - interrupt-parent: should be the phandle for the interrupt controller
+  - interrupts: the sole interrupt generated by the device
+
+  Refer to interrupt-controller/interrupts.txt for generic interrupt client
+  node bindings.
+
+Example:
+
+atlas@64 {
+	compatible = "atlas,ec-sm";
+	reg = <0x64>;
+	interrupt-parent = <&gpio1>;
+	interrupts = <16 2>;
+};
drivers/iio/chemical/Kconfig +5 -3
index f73290f..4bcc025 100644
@@ -5,15 +5,17 @@
 menu "Chemical Sensors"
 
 config ATLAS_PH_SENSOR
-	tristate "Atlas Scientific OEM pH-SM sensor"
+	tristate "Atlas Scientific OEM SM sensors"
 	depends on I2C
 	select REGMAP_I2C
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 	select IRQ_WORK
 	help
-	 Say Y here to build I2C interface support for the Atlas
-	 Scientific OEM pH-SM sensor.
+	 Say Y here to build I2C interface support for the following
+	 Atlas Scientific OEM SM sensors:
+	    * pH SM sensor
+	    * EC SM sensor
 
 	 To compile this driver as module, choose M here: the
 	 module will be called atlas-ph-sensor.
drivers/iio/chemical/atlas-ph-sensor.c +115 -2

189 lines not on this page · raw message

Signature · 1 line
2.7.4
maintainer
#3 lore raw

Re: [PATCH v2 2/3] iio: electricalconductivity: add IIO_ELECTRICALCONDUCTIVITY type

In reply to Matt Ranostay
Quoted 12 lines · Matt Ranostay
---
 Documentation/ABI/testing/sysfs-bus-iio | 7 +++++++
 drivers/iio/industrialio-core.c         | 1 +
 include/uapi/linux/iio/types.h          | 1 +
 3 files changed, 9 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +0 -0
index df44998..adfdf95 100644

Documentation/ABI/testing/sysfs-bus-iio Description: · line 1574

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-iio +7 -0
@@ -1565,3 +1565,10 @@ Description:
 		* X is in the plane of the propellers, perpendicular to Y axis,
 		  and positive towards the starboard side of the UAV ;
 		* Z is perpendicular to propellers plane and positive upwards.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_electricalconductivity_raw
+KernelVersion:	4.8
+Contact:	[email protected]
+Description:
+		Raw (unscaled no offset etc.) electric conductivity reading that
+		maps to microsiemens per centimeter.

Rather than seimens per m? Seems like the units are a mess when measuring this and we do in theory aim for the basic SI units where possible...

Otherwise, this looks fine to me.

Quoted 25 lines
drivers/iio/industrialio-core.c +1 -0
index e6319a9..2a85bd8 100644
@@ -80,6 +80,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_RESISTANCE] = "resistance",
 	[IIO_PH] = "ph",
 	[IIO_UVINDEX] = "uvindex",
+	[IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
 };
 
 static const char * const iio_modifier_names[] = {
include/uapi/linux/iio/types.h +1 -0
index b0916fc..22e5e58 100644
@@ -39,6 +39,7 @@ enum iio_chan_type {
 	IIO_RESISTANCE,
 	IIO_PH,
 	IIO_UVINDEX,
+	IIO_ELECTRICALCONDUCTIVITY,
 };
 
 enum iio_modifier {
#4 lore raw

Re: [PATCH v2 2/3] iio: electricalconductivity: add IIO_ELECTRICALCONDUCTIVITY type

Quoted 17 lines · Jonathan Cameron
Quoted 16 lines · Matt Ranostay
---
 Documentation/ABI/testing/sysfs-bus-iio | 7 +++++++
 drivers/iio/industrialio-core.c         | 1 +
 include/uapi/linux/iio/types.h          | 1 +
 3 files changed, 9 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +1 -0
index df44998..adfdf95 100644
@@ -1565,3 +1565,10 @@ Description:
              * X is in the plane of the propellers, perpendicular to Y axis,
                and positive towards the starboard side of the UAV ;
              * Z is perpendicular to propellers plane and positive upwards.
+
Quoted 9 lines

Documentation/ABI/testing/sysfs-bus-iio Description: · line 1574

Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-iio +6 -0
+What:                /sys/bus/iio/devices/iio:deviceX/in_electricalconductivity_raw
+KernelVersion:       4.8
+Contact:     [email protected]
+Description:
+             Raw (unscaled no offset etc.) electric conductivity reading that
+             maps to microsiemens per centimeter.

Rather than seimens per m? Seems like the units are a mess when measuring this and we do in theory aim for the basic SI units where possible...

Yeah seems most meters output this when they mean EC to be microsiemens per cm....
I have no issue making this a siemens per meter and update the scaling in the other patchset.

Thoughts?

Quoted 28 lines

Otherwise, this looks fine to me.

Quoted 25 lines
drivers/iio/industrialio-core.c +1 -0
index e6319a9..2a85bd8 100644
@@ -80,6 +80,7 @@ static const char * const iio_chan_type_name_spec[] = {
      [IIO_RESISTANCE] = "resistance",
      [IIO_PH] = "ph",
      [IIO_UVINDEX] = "uvindex",
+     [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
 };
 
 static const char * const iio_modifier_names[] = {
include/uapi/linux/iio/types.h +1 -0
index b0916fc..22e5e58 100644
@@ -39,6 +39,7 @@ enum iio_chan_type {
      IIO_RESISTANCE,
      IIO_PH,
      IIO_UVINDEX,
+     IIO_ELECTRICALCONDUCTIVITY,
 };
 
 enum iio_modifier {
#5 lore raw

Re: [PATCH v2 2/3] iio: electricalconductivity: add IIO_ELECTRICALCONDUCTIVITY type

Quoted 32 lines · Matt Ranostay
Quoted 21 lines · Jonathan Cameron
Quoted 20 lines · Matt Ranostay
---
 Documentation/ABI/testing/sysfs-bus-iio | 7 +++++++
 drivers/iio/industrialio-core.c         | 1 +
 include/uapi/linux/iio/types.h          | 1 +
 3 files changed, 9 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +0 -0
index df44998..adfdf95 100644
@@ -1565,3 +1565,10 @@ Description:
              * X is in the plane of the propellers, perpendicular to 
 Y axis,
                and positive towards the starboard side of the UAV ;
              * Z is perpendicular to propellers plane and positive 

upwards.

Documentation/ABI/testing/sysfs-bus-iio +2 -0
+
+What:                

/sys/bus/iio/devices/iio:deviceX/in_electricalconductivity_raw

Quoted 9 lines
Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-iio +4 -0
+KernelVersion:       4.8
+Contact:     [email protected]
+Description:
+             Raw (unscaled no offset etc.) electric conductivity 

reading that
+ maps to microsiemens per centimeter.

Rather than seimens per m? Seems like the units are a mess when measuring this and we do in theory aim for the basic SI units where possible...

Quoted 5 lines

Yeah seems most meters output this when they mean EC to be microsiemens per cm....
I have no issue making this a siemens per meter and update the scaling in the other patchset.

Update it. Just because the world is bonkers isn't a reason to follow along :)
+ conversion is trivial anyway so I doubt anyone will moan about it.

J

Quoted 32 lines

Thoughts?

Quoted 29 lines

Otherwise, this looks fine to me.

Quoted 26 lines
drivers/iio/industrialio-core.c +1 -0
index e6319a9..2a85bd8 100644
@@ -80,6 +80,7 @@ static const char * const iio_chan_type_name_spec[] 
 = {
      [IIO_RESISTANCE] = "resistance",
      [IIO_PH] = "ph",
      [IIO_UVINDEX] = "uvindex",
+     [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
 };
 

static const char * const iio_modifier_names[] = {

include/uapi/linux/iio/types.h +1 -0
index b0916fc..22e5e58 100644
@@ -39,6 +39,7 @@ enum iio_chan_type {
      IIO_RESISTANCE,
      IIO_PH,
      IIO_UVINDEX,
+     IIO_ELECTRICALCONDUCTIVITY,
 };
 
 enum iio_modifier {