Skip to contentSkip to conversation

iio: chemical: add atlas ph sensor support

PATCH Superseded Matt Ranostay 3 participants 8 messages Jan 20, 2016 – Jan 24, 2016

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover iio: chemical: add atlas ph sensor support

Changes from v3:
* Allow temp output channel to be read during buffer enable
* Add volatile table for regmap reading/writing
* Make sizeof() parameters consistent

Matt Ranostay (3):
devicetree: add Atlas Scientific LLC vendor prefix
iio: ph: add IIO_PH channel type
iio: chemical: add Atlas pH-SM sensor support

 Documentation/ABI/testing/sysfs-bus-iio            |   7 +
 .../bindings/iio/chemical/atlas,ph-sm.txt          |  22 +
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 drivers/iio/chemical/Kconfig                       |  13 +
 drivers/iio/chemical/Makefile                      |   1 +
 drivers/iio/chemical/atlas-ph-sensor.c             | 497 +++++++++++++++++++++
 drivers/iio/industrialio-core.c                    |   1 +
 include/uapi/linux/iio/types.h                     |   1 +
 8 files changed, 543 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt
 create mode 100644 drivers/iio/chemical/atlas-ph-sensor.c
Signature · 1 line
2.7.0.rc3
#1 lore raw

PATCH v4 1/3 devicetree: add Atlas Scientific LLC vendor prefix

Add the "atlas" vendor prefix for Atlas Scientific LLC

---
 Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
 1 file changed, 1 insertion(+)
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 72e2c5a..44ddc98 100644
@@ -28,6 +28,7 @@ arm	ARM Ltd.
 armadeus	ARMadeus Systems SARL
 artesyn	Artesyn Embedded Technologies Inc.
 asahi-kasei	Asahi Kasei Corp.
+atlas	Atlas Scientific LLC
 atmel	Atmel Corporation
 auo	AU Optronics Corporation
 avago	Avago Technologies
Signature · 1 line
2.7.0.rc3
#2 lore raw

PATCH v4 2/3 iio: ph: add IIO_PH channel 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 0439c2a..86ba8d6 100644
@@ -1491,3 +1491,10 @@ Description:
 		This ABI is especially applicable for humidity sensors
 		to heatup the device and get rid of any condensation
 		in some humidity environment
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_ph_raw
+KernelVersion:	4.5
+Contact:	[email protected]
+Description:
+		Raw (unscaled no offset etc.) pH reading of a substance as a negative
+		base-10 logarithm of hydrodium ions in a litre of water.
drivers/iio/industrialio-core.c +1 -0
index af7cc1e..70cb7eb 100644
@@ -77,6 +77,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_VELOCITY] = "velocity",
 	[IIO_CONCENTRATION] = "concentration",
 	[IIO_RESISTANCE] = "resistance",
+	[IIO_PH] = "ph",
 };
 
 static const char * const iio_modifier_names[] = {
include/uapi/linux/iio/types.h +1 -0
index 7c63bd6..c077617 100644
@@ -37,6 +37,7 @@ enum iio_chan_type {
 	IIO_VELOCITY,
 	IIO_CONCENTRATION,
 	IIO_RESISTANCE,
+	IIO_PH,
 };
 
 enum iio_modifier {
Signature · 1 line
2.7.0.rc3
maintainer
#3 lore raw

Re: [PATCH v4 2/3] iio: ph: add IIO_PH channel type

Quoted 1 line · Matt Ranostay

Maybe it's just the British Chemistry GCSE syllabus when I was a kid, but I was tempted to add a note to say. 'Also, it's that magic value that you measured in school chemistry lessons using funny coloured paper.'

:)

Quoted 46 lines
---
 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 0439c2a..86ba8d6 100644
@@ -1491,3 +1491,10 @@ Description:
 		This ABI is especially applicable for humidity sensors
 		to heatup the device and get rid of any condensation
 		in some humidity environment
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_ph_raw
+KernelVersion:	4.5
+Contact:	[email protected]
+Description:
+		Raw (unscaled no offset etc.) pH reading of a substance as a negative
+		base-10 logarithm of hydrodium ions in a litre of water.
drivers/iio/industrialio-core.c +1 -0
index af7cc1e..70cb7eb 100644
@@ -77,6 +77,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_VELOCITY] = "velocity",
 	[IIO_CONCENTRATION] = "concentration",
 	[IIO_RESISTANCE] = "resistance",
+	[IIO_PH] = "ph",
 };
 
 static const char * const iio_modifier_names[] = {
include/uapi/linux/iio/types.h +1 -0
index 7c63bd6..c077617 100644
@@ -37,6 +37,7 @@ enum iio_chan_type {
 	IIO_VELOCITY,
 	IIO_CONCENTRATION,
 	IIO_RESISTANCE,
+	IIO_PH,
 };
 
 enum iio_modifier {
#4 lore raw

PATCH v4 3/3 iio: chemical: add Atlas pH-SM sensor support

In reply to Matt Ranostay

Add support for the Atlas Scientific pH-SM chemical sensor that can detect pH levels of solutions in the range of 0-14.

---
 .../bindings/iio/chemical/atlas,ph-sm.txt          |  22 +
 drivers/iio/chemical/Kconfig                       |  13 +
 drivers/iio/chemical/Makefile                      |   1 +
 drivers/iio/chemical/atlas-ph-sensor.c             | 497 +++++++++++++++++++++
 4 files changed, 533 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt
 create mode 100644 drivers/iio/chemical/atlas-ph-sensor.c
Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt new file +22 -0
new file mode 100644
index 0000000..cffa190
@@ -0,0 +1,22 @@
+* Atlas Scientific pH-SM OEM sensor
+
+http://www.atlas-scientific.com/_files/_datasheets/_oem/pH_oem_datasheet.pdf
+
+Required properties:
+
+  - compatible: must be "atlas,ph-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@65 {
+	compatible = "atlas,ph-sm";
+	reg = <0x65>;
+	interrupt-parent = <&gpio1>;
+	interrupts = <16 2>;
+};
drivers/iio/chemical/Kconfig +13 -0
index f16de61..ce7cd1370 100644
@@ -4,6 +4,19 @@
 
 menu "Chemical Sensors"
 
+config ATLAS_PH_SENSOR
+	tristate "Atlas Scientific OEM pH-SM sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	 Say Y here to build I2C interface support for the Atlas
+	 Scientific OEM pH-SM sensor.
+
+	 To compile this driver as module, choose M here: the
+	 module will be called atlas-ph-sensor.
+
 config IAQCORE
 	tristate "AMS iAQ-Core VOC sensors"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 167861f..b02202b 100644
@@ -3,5 +3,6 @@
 #
 
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_ATLAS_PH_SENSOR)	+= atlas-ph-sensor.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/atlas-ph-sensor.c new file +497 -0

497 lines not on this page · raw message

Signature · 1 line
2.7.0.rc3
maintainer
#5 lore raw

Re: [PATCH v4 3/3] iio: chemical: add Atlas pH-SM sensor support

Quoted 4 lines · Matt Ranostay

Add support for the Atlas Scientific pH-SM chemical sensor that can detect pH levels of solutions in the range of 0-14.

There's a small amount of missbalanced stuff between the probe error path and the remove function that probably wants tidying up. Few trivial bits and bobs otherwise.

I'm a little confused by the rising or falling interrupt you are registering a hander for as well. Is it really toggling? If so that's unusual enough to merit a comment.

Jonathan

Quoted 83 lines
---
 .../bindings/iio/chemical/atlas,ph-sm.txt          |  22 +
 drivers/iio/chemical/Kconfig                       |  13 +
 drivers/iio/chemical/Makefile                      |   1 +
 drivers/iio/chemical/atlas-ph-sensor.c             | 497 +++++++++++++++++++++
 4 files changed, 533 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt
 create mode 100644 drivers/iio/chemical/atlas-ph-sensor.c
Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt new file +22 -0
new file mode 100644
index 0000000..cffa190
@@ -0,0 +1,22 @@
+* Atlas Scientific pH-SM OEM sensor
+
+http://www.atlas-scientific.com/_files/_datasheets/_oem/pH_oem_datasheet.pdf
+
+Required properties:
+
+  - compatible: must be "atlas,ph-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@65 {
+	compatible = "atlas,ph-sm";
+	reg = <0x65>;
+	interrupt-parent = <&gpio1>;
+	interrupts = <16 2>;
+};
drivers/iio/chemical/Kconfig +13 -0
index f16de61..ce7cd1370 100644
@@ -4,6 +4,19 @@
 
 menu "Chemical Sensors"
 
+config ATLAS_PH_SENSOR
+	tristate "Atlas Scientific OEM pH-SM sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	 Say Y here to build I2C interface support for the Atlas
+	 Scientific OEM pH-SM sensor.
+
+	 To compile this driver as module, choose M here: the
+	 module will be called atlas-ph-sensor.
+
 config IAQCORE
 	tristate "AMS iAQ-Core VOC sensors"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 167861f..b02202b 100644
@@ -3,5 +3,6 @@
 #
 
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_ATLAS_PH_SENSOR)	+= atlas-ph-sensor.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/atlas-ph-sensor.c new file +5 -0
new file mode 100644
index 0000000..4c65688
@@ -0,0 +1,497 @@
+/*
+ * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor
+ *
+ * Copyright (C) 2015 Matt Ranostay <[email protected]>
+ *

drivers/iio/chemical/atlas-ph-sensor.c line 15

Quoted 10 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +10 -0
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *

Bonus blank line! (my pet hate of the day!)

Quoted 372 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +372 -0
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/err.h>
+#include <linux/irq.h>
+#include <linux/irq_work.h>
+#include <linux/gpio.h>
+#include <linux/i2c.h>
+#include <linux/regmap.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/pm_runtime.h>
+
+#define ATLAS_REGMAP_NAME	"atlas_ph_regmap"
+#define ATLAS_DRV_NAME		"atlas_ph"
+
+#define ATLAS_REG_DEV_TYPE		0x00
+#define ATLAS_REG_DEV_VERSION		0x01
+
+#define ATLAS_REG_INT_CONTROL		0x04
+#define ATLAS_REG_INT_CONTROL_EN	BIT(3)
+
+#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_TEMP_DATA		0x0e
+#define ATLAS_REG_PH_DATA		0x16
+
+#define ATLAS_PH_INT_TIME_IN_US		450000
+
+struct atlas_data {
+	struct i2c_client *client;
+	struct iio_trigger *trig;
+	struct regmap *regmap;
+	struct irq_work work;
+
+	__be32 buffer[4]; /* 32-bit pH data + 32-bit pad + 64-bit timestamp */
+};
+
+static const struct regmap_range atlas_volatile_ranges[] = {
+	regmap_reg_range(ATLAS_REG_INT_CONTROL, ATLAS_REG_INT_CONTROL),
+	regmap_reg_range(ATLAS_REG_CALIB_STATUS, ATLAS_REG_CALIB_STATUS),
+	regmap_reg_range(ATLAS_REG_TEMP_DATA, ATLAS_REG_TEMP_DATA + 4),
+	regmap_reg_range(ATLAS_REG_PH_DATA, ATLAS_REG_PH_DATA + 4),
+};
+
+static const struct regmap_access_table atlas_volatile_table = {
+	.yes_ranges	= atlas_volatile_ranges,
+	.n_yes_ranges	= ARRAY_SIZE(atlas_volatile_ranges),
+};
+
+static const struct regmap_config atlas_regmap_config = {
+	.name = ATLAS_REGMAP_NAME,
+
+	.reg_bits = 8,
+	.val_bits = 8,
+
+	.volatile_table = &atlas_volatile_table,
+	.max_register = ATLAS_REG_PH_DATA + 4,
+	.cache_type = REGCACHE_FLAT,
+};
+
+static const struct iio_chan_spec atlas_channels[] = {
+	{
+		.type = IIO_PH,
+		.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_BE,
+		},
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1),
+	{
+		.type = IIO_TEMP,
+		.address = ATLAS_REG_TEMP_DATA,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
+		.output = 1,
+		.scan_index = -1
+	},
+};
+
+static int atlas_set_powermode(struct atlas_data *data, int on)
+{
+	return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
+}
+
+static int atlas_set_interrupt(struct atlas_data *data, bool state)
+{
+	return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL,
+				  ATLAS_REG_INT_CONTROL_EN,
+				  state ? ATLAS_REG_INT_CONTROL_EN : 0);
+}
+
+static int atlas_buffer_postenable(struct iio_dev *indio_dev)
+{
+	struct atlas_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = iio_triggered_buffer_postenable(indio_dev);
+	if (ret)
+		return ret;
+
+	ret = pm_runtime_get_sync(&data->client->dev);
+	if (ret < 0) {
+		pm_runtime_put_noidle(&data->client->dev);
+		return ret;
+	}
+
+	return atlas_set_interrupt(data, true);
+}
+
+static int atlas_buffer_predisable(struct iio_dev *indio_dev)
+{
+	struct atlas_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = iio_triggered_buffer_predisable(indio_dev);
+	if (ret)
+		return ret;
+
+	ret = atlas_set_interrupt(data, false);
+	if (ret)
+		return ret;
+
+	pm_runtime_mark_last_busy(&data->client->dev);
+	return pm_runtime_put_autosuspend(&data->client->dev);
+}
+
+static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
+	.owner = THIS_MODULE,
+};
+
+static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
+	.postenable = atlas_buffer_postenable,
+	.predisable = atlas_buffer_predisable,
+};
+
+static void atlas_work_handler(struct irq_work *work)
+{
+	struct atlas_data *data = container_of(work, struct atlas_data, work);
+
+	iio_trigger_poll(data->trig);
+}
+
+static irqreturn_t atlas_trigger_handler(int irq, void *private)
+{
+	struct iio_poll_func *pf = private;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct atlas_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = i2c_smbus_read_i2c_block_data(data->client, ATLAS_REG_PH_DATA,
+				sizeof(data->buffer[0]), (u8 *) &data->buffer);
+
+	if (ret > 0)
+		iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+				iio_get_time_ns());
+
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t atlas_interrupt_handler(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct atlas_data *data = iio_priv(indio_dev);
+
+	irq_work_queue(&data->work);
+
+	return IRQ_HANDLED;
+}
+
+static int atlas_read_ph_measurement(struct atlas_data *data, __be32 *val)
+{
+	struct device *dev = &data->client->dev;
+	int suspended = pm_runtime_suspended(dev);
+	int ret;
+
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0) {
+		pm_runtime_put_noidle(dev);
+		return ret;
+	}
+
+	if (suspended)
+		usleep_range(ATLAS_PH_INT_TIME_IN_US,
+			     ATLAS_PH_INT_TIME_IN_US + 100000);
+
+	ret = regmap_bulk_read(data->regmap, ATLAS_REG_PH_DATA,
+			      (u8 *) val, sizeof(*val));
+
+	pm_runtime_mark_last_busy(dev);
+	pm_runtime_put_autosuspend(dev);
+
+	return ret;
+}
+
+static int atlas_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct atlas_data *data = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		int ret;
+		__be32 reg;
+
+		switch (chan->type) {
+		case IIO_TEMP:
+			ret = regmap_bulk_read(data->regmap, chan->address,
+					      (u8 *) &reg, sizeof(reg));
+			break;
+		case IIO_PH:
+			mutex_lock(&indio_dev->mlock);
+
+			if (iio_buffer_enabled(indio_dev))
+				ret = -EBUSY;
+			else
+				ret = atlas_read_ph_measurement(data, &reg);
+
+			mutex_unlock(&indio_dev->mlock);
+			break;
+		default:
+			ret = -EINVAL;
+		}
+
+		if (!ret) {
+			*val = be32_to_cpu(reg);
+			ret = IIO_VAL_INT;
+		}
+		return ret;
+	}
+	case IIO_CHAN_INFO_SCALE:
+		*val = 0; /* 0.001 */
+		*val2 = 1000;
+		return IIO_VAL_INT_PLUS_MICRO;
+	}
+
+	return -EINVAL;
+}
+
+static int atlas_write_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int val, int val2, long mask)
+{
+	struct atlas_data *data = iio_priv(indio_dev);
+	__be32 reg = cpu_to_be32(val);
+
+	if (val2 != 0 || val < 0 || val > 20000)
+		return -EINVAL;
+
+	if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
+		return -EINVAL;
+
+	return regmap_bulk_write(data->regmap, chan->address,
+				 &reg, sizeof(reg));
+}
+
+static const struct iio_info atlas_info = {
+	.driver_module = THIS_MODULE,
+	.read_raw = atlas_read_raw,
+	.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");
+
+	return 0;
+};
+
+static int atlas_probe(struct i2c_client *client,
+		       const struct i2c_device_id *id)
+{
+	struct atlas_data *data;
+	struct iio_trigger *trig;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	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->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
+	indio_dev->dev.parent = &client->dev;
+
+	trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
+				      indio_dev->name, indio_dev->id);
+
+	if (!trig)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	data->client = client;
+	data->trig = trig;
+	trig->dev.parent = indio_dev->dev.parent;
+	trig->ops = &atlas_interrupt_trigger_ops;
+	iio_trigger_set_drvdata(trig, indio_dev);
+
+	i2c_set_clientdata(client, indio_dev);
+
+	data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config);
+	if (IS_ERR(data->regmap)) {
+		dev_err(&client->dev, "regmap initialization failed\n");
+		return PTR_ERR(data->regmap);
+	}
+
+	ret = pm_runtime_set_active(&client->dev);
+	if (ret)
+		return ret;
+
+	if (client->irq <= 0) {
+		dev_err(&client->dev, "no valid irq defined\n");
+		return -EINVAL;
+	}
+
+	ret = atlas_check_calibration(data);
+	if (ret)
+		return ret;
+
+	ret = iio_trigger_register(trig);
+	if (ret) {
+		dev_err(&client->dev, "failed to register trigger\n");
+		return ret;
+	}
+
+	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
+		&atlas_trigger_handler, &atlas_buffer_setup_ops);
+	if (ret) {

drivers/iio/chemical/atlas-ph-sensor.c line 397

Quoted 10 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +10 -0
+		dev_err(&client->dev, "cannot setup iio trigger\n");
+		goto unregister_trigger;
+	}
+
+	init_irq_work(&data->work, atlas_work_handler);
+
+	ret = devm_request_threaded_irq(&client->dev, client->irq,
+					NULL, atlas_interrupt_handler,
+					IRQF_TRIGGER_RISING |
+					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,

Both rising and falling? Seems odd for the usecase seen here (end of conversion type interrupt).

Quoted 15 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +15 -0
+					"atlas_irq",
+					indio_dev);
+	if (ret) {
+		dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
+		goto unregister_buffer;
+	}
+
+	ret = atlas_set_powermode(data, 1);
+	if (ret) {
+		dev_err(&client->dev, "cannot power device on");
+		goto unregister_buffer;
+	}
+
+	pm_runtime_enable(&client->dev);
+	pm_runtime_set_autosuspend_delay(&client->dev, 2500);

drivers/iio/chemical/atlas-ph-sensor.c line 422

Quoted 10 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +10 -0
+	pm_runtime_use_autosuspend(&client->dev);
+
+	ret = iio_device_register(indio_dev);
+	if (ret) {
+		dev_err(&client->dev, "unable to register device\n");
+		goto unregister_buffer;
+	}
+
+	return 0;
+

I guess it's not overly important, but seems to me that you ought to also be calling
pm_runtime_disable if you've called the enable before hitting an error.

+ powering down the device would be nice I guess.

I always expect to see most of the remove function matching the probe error paths, so wonder what is going on if bits are missing...

Quoted 76 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +75 -0
+unregister_buffer:
+	iio_triggered_buffer_cleanup(indio_dev);
+
+unregister_trigger:
+	iio_trigger_unregister(data->trig);
+
+	return ret;
+}
+
+static int atlas_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct atlas_data *data = iio_priv(indio_dev);
+
+	iio_device_unregister(indio_dev);
+	iio_triggered_buffer_cleanup(indio_dev);
+	iio_trigger_unregister(data->trig);
+
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+
+	return atlas_set_powermode(data, 0);
+}
+
+#ifdef CONFIG_PM
+static int atlas_runtime_suspend(struct device *dev)
+{
+	struct atlas_data *data =
+		     iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
+
+	return atlas_set_powermode(data, 0);
+}
+
+static int atlas_runtime_resume(struct device *dev)
+{
+	struct atlas_data *data =
+		     iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
+
+	return atlas_set_powermode(data, 1);
+}
+#endif
+
+static const struct dev_pm_ops atlas_pm_ops = {
+	SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
+			   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,
+		.of_match_table	= of_match_ptr(atlas_dt_ids),
+		.pm	= &atlas_pm_ops,
+	},
+	.probe		= atlas_probe,
+	.remove		= atlas_remove,
+	.id_table	= atlas_id,
+};
+module_i2c_driver(atlas_driver);
+
+MODULE_AUTHOR("Matt Ranostay <[email protected]>");
+MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
+MODULE_LICENSE("GPL");
#6 lore raw

Re: [PATCH v4 3/3] iio: chemical: add Atlas pH-SM sensor support

Quoted 4 lines · Jonathan Cameron
Quoted 3 lines · Matt Ranostay

Add support for the Atlas Scientific pH-SM chemical sensor that can detect pH levels of solutions in the range of 0-14.

Quoted 8 lines
Quoted 1 line

There's a small amount of missbalanced stuff between the probe error path and the remove function that probably wants tidying up. Few trivial bits and bobs otherwise.

I'm a little confused by the rising or falling interrupt you are registering a hander for as well. Is it really toggling? If so that's unusual enough to merit a comment.

Yeah it is little odd but it toggles the interrupt to the inverse state when a new conversion happens.
I'll document better in v5.

Quoted 510 lines

Jonathan

Quoted 83 lines
---
 .../bindings/iio/chemical/atlas,ph-sm.txt          |  22 +
 drivers/iio/chemical/Kconfig                       |  13 +
 drivers/iio/chemical/Makefile                      |   1 +
 drivers/iio/chemical/atlas-ph-sensor.c             | 497 +++++++++++++++++++++
 4 files changed, 533 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt
 create mode 100644 drivers/iio/chemical/atlas-ph-sensor.c
Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt new file +22 -0
new file mode 100644
index 0000000..cffa190
@@ -0,0 +1,22 @@
+* Atlas Scientific pH-SM OEM sensor
+
+http://www.atlas-scientific.com/_files/_datasheets/_oem/pH_oem_datasheet.pdf
+
+Required properties:
+
+  - compatible: must be "atlas,ph-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@65 {
+     compatible = "atlas,ph-sm";
+     reg = <0x65>;
+     interrupt-parent = <&gpio1>;
+     interrupts = <16 2>;
+};
drivers/iio/chemical/Kconfig +13 -0
index f16de61..ce7cd1370 100644
@@ -4,6 +4,19 @@
 
 menu "Chemical Sensors"
 
+config ATLAS_PH_SENSOR
+     tristate "Atlas Scientific OEM pH-SM sensor"
+     depends on I2C
+     select REGMAP_I2C
+     select IIO_BUFFER
+     select IIO_TRIGGERED_BUFFER
+     help
+      Say Y here to build I2C interface support for the Atlas
+      Scientific OEM pH-SM sensor.
+
+      To compile this driver as module, choose M here: the
+      module will be called atlas-ph-sensor.
+
 config IAQCORE
      tristate "AMS iAQ-Core VOC sensors"
      depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 167861f..b02202b 100644
@@ -3,5 +3,6 @@
 #
 
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_ATLAS_PH_SENSOR)        += atlas-ph-sensor.o
 obj-$(CONFIG_IAQCORE)                += ams-iaq-core.o
 obj-$(CONFIG_VZ89X)          += vz89x.o
drivers/iio/chemical/atlas-ph-sensor.c new file +5 -0
new file mode 100644
index 0000000..4c65688
@@ -0,0 +1,497 @@
+/*
+ * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor
+ *
+ * Copyright (C) 2015 Matt Ranostay <[email protected]>
+ *

drivers/iio/chemical/atlas-ph-sensor.c line 15

Quoted 10 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +10 -0
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *

Bonus blank line! (my pet hate of the day!)

Quoted 372 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +372 -0
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/err.h>
+#include <linux/irq.h>
+#include <linux/irq_work.h>
+#include <linux/gpio.h>
+#include <linux/i2c.h>
+#include <linux/regmap.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/pm_runtime.h>
+
+#define ATLAS_REGMAP_NAME    "atlas_ph_regmap"
+#define ATLAS_DRV_NAME               "atlas_ph"
+
+#define ATLAS_REG_DEV_TYPE           0x00
+#define ATLAS_REG_DEV_VERSION                0x01
+
+#define ATLAS_REG_INT_CONTROL                0x04
+#define ATLAS_REG_INT_CONTROL_EN     BIT(3)
+
+#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_TEMP_DATA          0x0e
+#define ATLAS_REG_PH_DATA            0x16
+
+#define ATLAS_PH_INT_TIME_IN_US              450000
+
+struct atlas_data {
+     struct i2c_client *client;
+     struct iio_trigger *trig;
+     struct regmap *regmap;
+     struct irq_work work;
+
+     __be32 buffer[4]; /* 32-bit pH data + 32-bit pad + 64-bit timestamp */
+};
+
+static const struct regmap_range atlas_volatile_ranges[] = {
+     regmap_reg_range(ATLAS_REG_INT_CONTROL, ATLAS_REG_INT_CONTROL),
+     regmap_reg_range(ATLAS_REG_CALIB_STATUS, ATLAS_REG_CALIB_STATUS),
+     regmap_reg_range(ATLAS_REG_TEMP_DATA, ATLAS_REG_TEMP_DATA + 4),
+     regmap_reg_range(ATLAS_REG_PH_DATA, ATLAS_REG_PH_DATA + 4),
+};
+
+static const struct regmap_access_table atlas_volatile_table = {
+     .yes_ranges     = atlas_volatile_ranges,
+     .n_yes_ranges   = ARRAY_SIZE(atlas_volatile_ranges),
+};
+
+static const struct regmap_config atlas_regmap_config = {
+     .name = ATLAS_REGMAP_NAME,
+
+     .reg_bits = 8,
+     .val_bits = 8,
+
+     .volatile_table = &atlas_volatile_table,
+     .max_register = ATLAS_REG_PH_DATA + 4,
+     .cache_type = REGCACHE_FLAT,
+};
+
+static const struct iio_chan_spec atlas_channels[] = {
+     {
+             .type = IIO_PH,
+             .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_BE,
+             },
+     },
+     IIO_CHAN_SOFT_TIMESTAMP(1),
+     {
+             .type = IIO_TEMP,
+             .address = ATLAS_REG_TEMP_DATA,
+             .info_mask_separate =
+                     BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
+             .output = 1,
+             .scan_index = -1
+     },
+};
+
+static int atlas_set_powermode(struct atlas_data *data, int on)
+{
+     return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
+}
+
+static int atlas_set_interrupt(struct atlas_data *data, bool state)
+{
+     return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL,
+                               ATLAS_REG_INT_CONTROL_EN,
+                               state ? ATLAS_REG_INT_CONTROL_EN : 0);
+}
+
+static int atlas_buffer_postenable(struct iio_dev *indio_dev)
+{
+     struct atlas_data *data = iio_priv(indio_dev);
+     int ret;
+
+     ret = iio_triggered_buffer_postenable(indio_dev);
+     if (ret)
+             return ret;
+
+     ret = pm_runtime_get_sync(&data->client->dev);
+     if (ret < 0) {
+             pm_runtime_put_noidle(&data->client->dev);
+             return ret;
+     }
+
+     return atlas_set_interrupt(data, true);
+}
+
+static int atlas_buffer_predisable(struct iio_dev *indio_dev)
+{
+     struct atlas_data *data = iio_priv(indio_dev);
+     int ret;
+
+     ret = iio_triggered_buffer_predisable(indio_dev);
+     if (ret)
+             return ret;
+
+     ret = atlas_set_interrupt(data, false);
+     if (ret)
+             return ret;
+
+     pm_runtime_mark_last_busy(&data->client->dev);
+     return pm_runtime_put_autosuspend(&data->client->dev);
+}
+
+static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
+     .owner = THIS_MODULE,
+};
+
+static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
+     .postenable = atlas_buffer_postenable,
+     .predisable = atlas_buffer_predisable,
+};
+
+static void atlas_work_handler(struct irq_work *work)
+{
+     struct atlas_data *data = container_of(work, struct atlas_data, work);
+
+     iio_trigger_poll(data->trig);
+}
+
+static irqreturn_t atlas_trigger_handler(int irq, void *private)
+{
+     struct iio_poll_func *pf = private;
+     struct iio_dev *indio_dev = pf->indio_dev;
+     struct atlas_data *data = iio_priv(indio_dev);
+     int ret;
+
+     ret = i2c_smbus_read_i2c_block_data(data->client, ATLAS_REG_PH_DATA,
+                             sizeof(data->buffer[0]), (u8 *) &data->buffer);
+
+     if (ret > 0)
+             iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+                             iio_get_time_ns());
+
+     iio_trigger_notify_done(indio_dev->trig);
+
+     return IRQ_HANDLED;
+}
+
+static irqreturn_t atlas_interrupt_handler(int irq, void *private)
+{
+     struct iio_dev *indio_dev = private;
+     struct atlas_data *data = iio_priv(indio_dev);
+
+     irq_work_queue(&data->work);
+
+     return IRQ_HANDLED;
+}
+
+static int atlas_read_ph_measurement(struct atlas_data *data, __be32 *val)
+{
+     struct device *dev = &data->client->dev;
+     int suspended = pm_runtime_suspended(dev);
+     int ret;
+
+     ret = pm_runtime_get_sync(dev);
+     if (ret < 0) {
+             pm_runtime_put_noidle(dev);
+             return ret;
+     }
+
+     if (suspended)
+             usleep_range(ATLAS_PH_INT_TIME_IN_US,
+                          ATLAS_PH_INT_TIME_IN_US + 100000);
+
+     ret = regmap_bulk_read(data->regmap, ATLAS_REG_PH_DATA,
+                           (u8 *) val, sizeof(*val));
+
+     pm_runtime_mark_last_busy(dev);
+     pm_runtime_put_autosuspend(dev);
+
+     return ret;
+}
+
+static int atlas_read_raw(struct iio_dev *indio_dev,
+                       struct iio_chan_spec const *chan,
+                       int *val, int *val2, long mask)
+{
+     struct atlas_data *data = iio_priv(indio_dev);
+
+     switch (mask) {
+     case IIO_CHAN_INFO_RAW: {
+             int ret;
+             __be32 reg;
+
+             switch (chan->type) {
+             case IIO_TEMP:
+                     ret = regmap_bulk_read(data->regmap, chan->address,
+                                           (u8 *) &reg, sizeof(reg));
+                     break;
+             case IIO_PH:
+                     mutex_lock(&indio_dev->mlock);
+
+                     if (iio_buffer_enabled(indio_dev))
+                             ret = -EBUSY;
+                     else
+                             ret = atlas_read_ph_measurement(data, &reg);
+
+                     mutex_unlock(&indio_dev->mlock);
+                     break;
+             default:
+                     ret = -EINVAL;
+             }
+
+             if (!ret) {
+                     *val = be32_to_cpu(reg);
+                     ret = IIO_VAL_INT;
+             }
+             return ret;
+     }
+     case IIO_CHAN_INFO_SCALE:
+             *val = 0; /* 0.001 */
+             *val2 = 1000;
+             return IIO_VAL_INT_PLUS_MICRO;
+     }
+
+     return -EINVAL;
+}
+
+static int atlas_write_raw(struct iio_dev *indio_dev,
+                        struct iio_chan_spec const *chan,
+                        int val, int val2, long mask)
+{
+     struct atlas_data *data = iio_priv(indio_dev);
+     __be32 reg = cpu_to_be32(val);
+
+     if (val2 != 0 || val < 0 || val > 20000)
+             return -EINVAL;
+
+     if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
+             return -EINVAL;
+
+     return regmap_bulk_write(data->regmap, chan->address,
+                              &reg, sizeof(reg));
+}
+
+static const struct iio_info atlas_info = {
+     .driver_module = THIS_MODULE,
+     .read_raw = atlas_read_raw,
+     .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");
+
+     return 0;
+};
+
+static int atlas_probe(struct i2c_client *client,
+                    const struct i2c_device_id *id)
+{
+     struct atlas_data *data;
+     struct iio_trigger *trig;
+     struct iio_dev *indio_dev;
+     int ret;
+
+     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+     if (!indio_dev)
+             return -ENOMEM;
+
+     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->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
+     indio_dev->dev.parent = &client->dev;
+
+     trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
+                                   indio_dev->name, indio_dev->id);
+
+     if (!trig)
+             return -ENOMEM;
+
+     data = iio_priv(indio_dev);
+     data->client = client;
+     data->trig = trig;
+     trig->dev.parent = indio_dev->dev.parent;
+     trig->ops = &atlas_interrupt_trigger_ops;
+     iio_trigger_set_drvdata(trig, indio_dev);
+
+     i2c_set_clientdata(client, indio_dev);
+
+     data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config);
+     if (IS_ERR(data->regmap)) {
+             dev_err(&client->dev, "regmap initialization failed\n");
+             return PTR_ERR(data->regmap);
+     }
+
+     ret = pm_runtime_set_active(&client->dev);
+     if (ret)
+             return ret;
+
+     if (client->irq <= 0) {
+             dev_err(&client->dev, "no valid irq defined\n");
+             return -EINVAL;
+     }
+
+     ret = atlas_check_calibration(data);
+     if (ret)
+             return ret;
+
+     ret = iio_trigger_register(trig);
+     if (ret) {
+             dev_err(&client->dev, "failed to register trigger\n");
+             return ret;
+     }
+
+     ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
+             &atlas_trigger_handler, &atlas_buffer_setup_ops);
+     if (ret) {

drivers/iio/chemical/atlas-ph-sensor.c line 397

Quoted 10 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +10 -0
+             dev_err(&client->dev, "cannot setup iio trigger\n");
+             goto unregister_trigger;
+     }
+
+     init_irq_work(&data->work, atlas_work_handler);
+
+     ret = devm_request_threaded_irq(&client->dev, client->irq,
+                                     NULL, atlas_interrupt_handler,
+                                     IRQF_TRIGGER_RISING |
+                                     IRQF_TRIGGER_FALLING | IRQF_ONESHOT,

Both rising and falling? Seems odd for the usecase seen here (end of conversion type interrupt).

Quoted 15 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +15 -0
+                                     "atlas_irq",
+                                     indio_dev);
+     if (ret) {
+             dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
+             goto unregister_buffer;
+     }
+
+     ret = atlas_set_powermode(data, 1);
+     if (ret) {
+             dev_err(&client->dev, "cannot power device on");
+             goto unregister_buffer;
+     }
+
+     pm_runtime_enable(&client->dev);
+     pm_runtime_set_autosuspend_delay(&client->dev, 2500);

drivers/iio/chemical/atlas-ph-sensor.c line 422

Quoted 10 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +10 -0
+     pm_runtime_use_autosuspend(&client->dev);
+
+     ret = iio_device_register(indio_dev);
+     if (ret) {
+             dev_err(&client->dev, "unable to register device\n");
+             goto unregister_buffer;
+     }
+
+     return 0;
+

I guess it's not overly important, but seems to me that you ought to also be calling
pm_runtime_disable if you've called the enable before hitting an error.

Quoted 5 lines

+ powering down the device would be nice I guess.

I always expect to see most of the remove function matching the probe error paths, so wonder what is going on if bits are missing...

Ok will look into this.

Quoted 78 lines
Quoted 77 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +75 -0
+unregister_buffer:
+     iio_triggered_buffer_cleanup(indio_dev);
+
+unregister_trigger:
+     iio_trigger_unregister(data->trig);
+
+     return ret;
+}
+
+static int atlas_remove(struct i2c_client *client)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(client);
+     struct atlas_data *data = iio_priv(indio_dev);
+
+     iio_device_unregister(indio_dev);
+     iio_triggered_buffer_cleanup(indio_dev);
+     iio_trigger_unregister(data->trig);
+
+     pm_runtime_disable(&client->dev);
+     pm_runtime_set_suspended(&client->dev);
+     pm_runtime_put_noidle(&client->dev);
+
+     return atlas_set_powermode(data, 0);
+}
+
+#ifdef CONFIG_PM
+static int atlas_runtime_suspend(struct device *dev)
+{
+     struct atlas_data *data =
+                  iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
+
+     return atlas_set_powermode(data, 0);
+}
+
+static int atlas_runtime_resume(struct device *dev)
+{
+     struct atlas_data *data =
+                  iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
+
+     return atlas_set_powermode(data, 1);
+}
+#endif
+
+static const struct dev_pm_ops atlas_pm_ops = {
+     SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
+                        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,
+             .of_match_table = of_match_ptr(atlas_dt_ids),
+             .pm     = &atlas_pm_ops,
+     },
+     .probe          = atlas_probe,
+     .remove         = atlas_remove,
+     .id_table       = atlas_id,
+};
+module_i2c_driver(atlas_driver);
+
+MODULE_AUTHOR("Matt Ranostay <[email protected]>");
+MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
+MODULE_LICENSE("GPL");
#7 lore raw

Re: [PATCH v4 3/3] iio: chemical: add Atlas pH-SM sensor support

Quoted 11 lines · Matt Ranostay
Quoted 9 lines · Jonathan Cameron
Quoted 4 lines · Matt Ranostay

Add support for the Atlas Scientific pH-SM chemical sensor that can detect pH levels of solutions in the range of 0-14.

There's a small amount of missbalanced stuff between the probe error path and the remove function that probably wants tidying up. Few trivial bits and bobs otherwise.

Quoted 7 lines
Quoted 3 lines

I'm a little confused by the rising or falling interrupt you are registering a hander for as well. Is it really toggling? If so that's unusual enough to merit a comment.

Yeah it is little odd but it toggles the interrupt to the inverse state when a new conversion happens.
I'll document better in v5.

Fair enough, I have pulled the same trick with rubbish servo drives in the past,
avoids explicit hand shakes without ever getting stuck. Just unusual for a sensor!

Quoted 634 lines
Quoted 8 lines

Jonathan

Quoted 5 lines
---
 .../bindings/iio/chemical/atlas,ph-sm.txt          |  22 +
 drivers/iio/chemical/Kconfig                       |  13 +
 drivers/iio/chemical/Makefile                      |   1 +
 drivers/iio/chemical/atlas-ph-sensor.c             | 497

+++++++++++++++++++++

Quoted 2 lines
Quoted 2 lines
 4 files changed, 533 insertions(+)
 create mode 100644

Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt

Quoted 3 lines
Quoted 3 lines
 create mode 100644 drivers/iio/chemical/atlas-ph-sensor.c

diff --git

a/Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt b/Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt

Quoted 8 lines

Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt line 2

Quoted 8 lines

new file mode 100644
index 0000000..cffa190

Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt new file +2 -0
@@ -0,0 +1,22 @@
+* Atlas Scientific pH-SM OEM sensor
+

+http://www.atlas-scientific.com/_files/_datasheets/_oem/pH_oem_datasheet.pdf

Quoted 6 lines

Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt

Quoted 6 lines
Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt new file +6 -0
+
+Required properties:
+
+  - compatible: must be "atlas,ph-sm"
+  - reg: the I2C address of the sensor
+  - interrupt-parent: should be the phandle for the interrupt

controller

Quoted 3 lines

Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt

Quoted 3 lines
Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt new file +3 -0
+  - interrupts: the sole interrupt generated by the device
+
+  Refer to interrupt-controller/interrupts.txt for generic

interrupt client

Quoted 11 lines

Review

Quoted 11 lines
Documentation/devicetree/bindings/iio/chemical/atlas,ph-sm.txt new file +10 -0
+  node bindings.
+
+Example:
+
+atlas@65 {
+     compatible = "atlas,ph-sm";
+     reg = <0x65>;
+     interrupt-parent = <&gpio1>;
+     interrupts = <16 2>;
+};
(quoted diff) +0 -0

b/drivers/iio/chemical/Kconfig

Quoted 16 lines
Quoted 16 lines

index f16de61..ce7cd1370 100644

drivers/iio/chemical/Kconfig +9 -0
@@ -4,6 +4,19 @@
 
 menu "Chemical Sensors"
 
+config ATLAS_PH_SENSOR
+     tristate "Atlas Scientific OEM pH-SM sensor"
+     depends on I2C
+     select REGMAP_I2C
+     select IIO_BUFFER
+     select IIO_TRIGGERED_BUFFER
+     help
+      Say Y here to build I2C interface support for the Atlas
+      Scientific OEM pH-SM sensor.
Quoted 8 lines

Review

Quoted 8 lines
drivers/iio/chemical/Kconfig +4 -0
+
+      To compile this driver as module, choose M here: the
+      module will be called atlas-ph-sensor.
+
 config IAQCORE
      tristate "AMS iAQ-Core VOC sensors"
      depends on I2C
(quoted diff) +0 -0

b/drivers/iio/chemical/Makefile

Quoted 11 lines

Review

Quoted 11 lines

index 167861f..b02202b 100644

drivers/iio/chemical/Makefile +1 -0
@@ -3,5 +3,6 @@
 #
 
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_ATLAS_PH_SENSOR)        += atlas-ph-sensor.o
 obj-$(CONFIG_IAQCORE)                += ams-iaq-core.o
 obj-$(CONFIG_VZ89X)          += vz89x.o
(quoted diff) +0 -0

b/drivers/iio/chemical/atlas-ph-sensor.c

Quoted 7 lines

drivers/iio/chemical/atlas-ph-sensor.c line 2

Quoted 7 lines

new file mode 100644
index 0000000..4c65688

drivers/iio/chemical/atlas-ph-sensor.c new file +2 -0
@@ -0,0 +1,497 @@
+/*
+ * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM

sensor

Quoted 4 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 4 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +4 -0
+ *
+ * Copyright (C) 2015 Matt Ranostay <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/iio/chemical/atlas-ph-sensor.c line 7

Quoted 1 line
drivers/iio/chemical/atlas-ph-sensor.c new file +1 -0
+ * it under the terms of the GNU General Public License as

published by

Quoted 1 line

drivers/iio/chemical/atlas-ph-sensor.c line 8

Quoted 1 line
drivers/iio/chemical/atlas-ph-sensor.c new file +1 -0
+ * the Free Software Foundation; either version 2 of the License,

or

Quoted 49 lines

drivers/iio/chemical/atlas-ph-sensor.c line 15

Quoted 7 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +7 -0
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *

Bonus blank line! (my pet hate of the day!)

Quoted 41 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +41 -0
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/err.h>
+#include <linux/irq.h>
+#include <linux/irq_work.h>
+#include <linux/gpio.h>
+#include <linux/i2c.h>
+#include <linux/regmap.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/pm_runtime.h>
+
+#define ATLAS_REGMAP_NAME    "atlas_ph_regmap"
+#define ATLAS_DRV_NAME               "atlas_ph"
+
+#define ATLAS_REG_DEV_TYPE           0x00
+#define ATLAS_REG_DEV_VERSION                0x01
+
+#define ATLAS_REG_INT_CONTROL                0x04
+#define ATLAS_REG_INT_CONTROL_EN     BIT(3)
+
+#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_TEMP_DATA          0x0e
+#define ATLAS_REG_PH_DATA            0x16
+
+#define ATLAS_PH_INT_TIME_IN_US              450000
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+
+struct atlas_data {
+     struct i2c_client *client;
+     struct iio_trigger *trig;
+     struct regmap *regmap;
+     struct irq_work work;
+
+     __be32 buffer[4]; /* 32-bit pH data + 32-bit pad + 64-bit

timestamp */

Quoted 4 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 4 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +4 -0
+};
+
+static const struct regmap_range atlas_volatile_ranges[] = {
+     regmap_reg_range(ATLAS_REG_INT_CONTROL,

ATLAS_REG_INT_CONTROL),

Quoted 1 line

drivers/iio/chemical/atlas-ph-sensor.c line 69

Quoted 1 line
drivers/iio/chemical/atlas-ph-sensor.c new file +1 -0
+     regmap_reg_range(ATLAS_REG_CALIB_STATUS,

ATLAS_REG_CALIB_STATUS),

Quoted 1 line

drivers/iio/chemical/atlas-ph-sensor.c line 70

Quoted 1 line
drivers/iio/chemical/atlas-ph-sensor.c new file +1 -0
+     regmap_reg_range(ATLAS_REG_TEMP_DATA, ATLAS_REG_TEMP_DATA +

4),

Quoted 16 lines
Quoted 16 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +16 -0
+     regmap_reg_range(ATLAS_REG_PH_DATA, ATLAS_REG_PH_DATA + 4),
+};
+
+static const struct regmap_access_table atlas_volatile_table = {
+     .yes_ranges     = atlas_volatile_ranges,
+     .n_yes_ranges   = ARRAY_SIZE(atlas_volatile_ranges),
+};
+
+static const struct regmap_config atlas_regmap_config = {
+     .name = ATLAS_REGMAP_NAME,
+
+     .reg_bits = 8,
+     .val_bits = 8,
+
+     .volatile_table = &atlas_volatile_table,
+     .max_register = ATLAS_REG_PH_DATA + 4,
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+     .cache_type = REGCACHE_FLAT,
+};
+
+static const struct iio_chan_spec atlas_channels[] = {
+     {
+             .type = IIO_PH,
+             .info_mask_separate =
+                     BIT(IIO_CHAN_INFO_RAW) |

BIT(IIO_CHAN_INFO_SCALE),

Quoted 6 lines
Quoted 6 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +6 -0
+             .scan_index = 0,
+             .scan_type = {
+                     .sign = 'u',
+                     .realbits = 32,
+                     .storagebits = 32,
+                     .endianness = IIO_BE,
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+             },
+     },
+     IIO_CHAN_SOFT_TIMESTAMP(1),
+     {
+             .type = IIO_TEMP,
+             .address = ATLAS_REG_TEMP_DATA,
+             .info_mask_separate =
+                     BIT(IIO_CHAN_INFO_RAW) |

BIT(IIO_CHAN_INFO_SCALE),

Quoted 7 lines
Quoted 7 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +7 -0
+             .output = 1,
+             .scan_index = -1
+     },
+};
+
+static int atlas_set_powermode(struct atlas_data *data, int on)
+{
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+     return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
+}
+
+static int atlas_set_interrupt(struct atlas_data *data, bool state)
+{
+     return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL,
+                               ATLAS_REG_INT_CONTROL_EN,
+                               state ? ATLAS_REG_INT_CONTROL_EN :

0);

Quoted 41 lines
Quoted 41 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +41 -0
+}
+
+static int atlas_buffer_postenable(struct iio_dev *indio_dev)
+{
+     struct atlas_data *data = iio_priv(indio_dev);
+     int ret;
+
+     ret = iio_triggered_buffer_postenable(indio_dev);
+     if (ret)
+             return ret;
+
+     ret = pm_runtime_get_sync(&data->client->dev);
+     if (ret < 0) {
+             pm_runtime_put_noidle(&data->client->dev);
+             return ret;
+     }
+
+     return atlas_set_interrupt(data, true);
+}
+
+static int atlas_buffer_predisable(struct iio_dev *indio_dev)
+{
+     struct atlas_data *data = iio_priv(indio_dev);
+     int ret;
+
+     ret = iio_triggered_buffer_predisable(indio_dev);
+     if (ret)
+             return ret;
+
+     ret = atlas_set_interrupt(data, false);
+     if (ret)
+             return ret;
+
+     pm_runtime_mark_last_busy(&data->client->dev);
+     return pm_runtime_put_autosuspend(&data->client->dev);
+}
+
+static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
+     .owner = THIS_MODULE,
+};
+
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
+     .postenable = atlas_buffer_postenable,
+     .predisable = atlas_buffer_predisable,
+};
+
+static void atlas_work_handler(struct irq_work *work)
+{
+     struct atlas_data *data = container_of(work, struct

atlas_data, work);

Quoted 4 lines
Quoted 4 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +4 -0
+
+     iio_trigger_poll(data->trig);
+}
+
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+static irqreturn_t atlas_trigger_handler(int irq, void *private)
+{
+     struct iio_poll_func *pf = private;
+     struct iio_dev *indio_dev = pf->indio_dev;
+     struct atlas_data *data = iio_priv(indio_dev);
+     int ret;
+
+     ret = i2c_smbus_read_i2c_block_data(data->client,

ATLAS_REG_PH_DATA,

Quoted 1 line

drivers/iio/chemical/atlas-ph-sensor.c line 185

Quoted 1 line
drivers/iio/chemical/atlas-ph-sensor.c new file +1 -0
+                             sizeof(data->buffer[0]), (u8 *)

&data->buffer);

Quoted 3 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 3 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +3 -0
+
+     if (ret > 0)
+             iio_push_to_buffers_with_timestamp(indio_dev,

data->buffer,

Quoted 10 lines
Quoted 10 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +10 -0
+                             iio_get_time_ns());
+
+     iio_trigger_notify_done(indio_dev->trig);
+
+     return IRQ_HANDLED;
+}
+
+static irqreturn_t atlas_interrupt_handler(int irq, void *private)
+{
+     struct iio_dev *indio_dev = private;
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+     struct atlas_data *data = iio_priv(indio_dev);
+
+     irq_work_queue(&data->work);
+
+     return IRQ_HANDLED;
+}
+
+static int atlas_read_ph_measurement(struct atlas_data *data,

__be32 *val)

Quoted 30 lines
Quoted 30 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +30 -0
+{
+     struct device *dev = &data->client->dev;
+     int suspended = pm_runtime_suspended(dev);
+     int ret;
+
+     ret = pm_runtime_get_sync(dev);
+     if (ret < 0) {
+             pm_runtime_put_noidle(dev);
+             return ret;
+     }
+
+     if (suspended)
+             usleep_range(ATLAS_PH_INT_TIME_IN_US,
+                          ATLAS_PH_INT_TIME_IN_US + 100000);
+
+     ret = regmap_bulk_read(data->regmap, ATLAS_REG_PH_DATA,
+                           (u8 *) val, sizeof(*val));
+
+     pm_runtime_mark_last_busy(dev);
+     pm_runtime_put_autosuspend(dev);
+
+     return ret;
+}
+
+static int atlas_read_raw(struct iio_dev *indio_dev,
+                       struct iio_chan_spec const *chan,
+                       int *val, int *val2, long mask)
+{
+     struct atlas_data *data = iio_priv(indio_dev);
+
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+     switch (mask) {
+     case IIO_CHAN_INFO_RAW: {
+             int ret;
+             __be32 reg;
+
+             switch (chan->type) {
+             case IIO_TEMP:
+                     ret = regmap_bulk_read(data->regmap,

chan->address,

Quoted 1 line

drivers/iio/chemical/atlas-ph-sensor.c line 245

Quoted 1 line
drivers/iio/chemical/atlas-ph-sensor.c new file +1 -0
+                                           (u8 *) &reg,

sizeof(reg));

Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+                     break;
+             case IIO_PH:
+                     mutex_lock(&indio_dev->mlock);
+
+                     if (iio_buffer_enabled(indio_dev))
+                             ret = -EBUSY;
+                     else
+                             ret = atlas_read_ph_measurement(data,

&reg);

Quoted 54 lines
Quoted 54 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +54 -0
+
+                     mutex_unlock(&indio_dev->mlock);
+                     break;
+             default:
+                     ret = -EINVAL;
+             }
+
+             if (!ret) {
+                     *val = be32_to_cpu(reg);
+                     ret = IIO_VAL_INT;
+             }
+             return ret;
+     }
+     case IIO_CHAN_INFO_SCALE:
+             *val = 0; /* 0.001 */
+             *val2 = 1000;
+             return IIO_VAL_INT_PLUS_MICRO;
+     }
+
+     return -EINVAL;
+}
+
+static int atlas_write_raw(struct iio_dev *indio_dev,
+                        struct iio_chan_spec const *chan,
+                        int val, int val2, long mask)
+{
+     struct atlas_data *data = iio_priv(indio_dev);
+     __be32 reg = cpu_to_be32(val);
+
+     if (val2 != 0 || val < 0 || val > 20000)
+             return -EINVAL;
+
+     if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
+             return -EINVAL;
+
+     return regmap_bulk_write(data->regmap, chan->address,
+                              &reg, sizeof(reg));
+}
+
+static const struct iio_info atlas_info = {
+     .driver_module = THIS_MODULE,
+     .read_raw = atlas_read_raw,
+     .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;
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+
+     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");

Quoted 3 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 3 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +3 -0
+
+     if (!(val & ATLAS_REG_CALIB_STATUS_MID))
+             dev_warn(dev, "device missing mid point

calibration\n");

Quoted 3 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 3 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +3 -0
+
+     if (!(val & ATLAS_REG_CALIB_STATUS_HIGH))
+             dev_warn(dev, "device missing high point

calibration\n");

Quoted 5 lines
Quoted 5 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +5 -0
+
+     return 0;
+};
+
+static int atlas_probe(struct i2c_client *client,
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+                    const struct i2c_device_id *id)
+{
+     struct atlas_data *data;
+     struct iio_trigger *trig;
+     struct iio_dev *indio_dev;
+     int ret;
+
+     indio_dev = devm_iio_device_alloc(&client->dev,

sizeof(*data));

Quoted 18 lines
Quoted 18 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +18 -0
+     if (!indio_dev)
+             return -ENOMEM;
+
+     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->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
+     indio_dev->dev.parent = &client->dev;
+
+     trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
+                                   indio_dev->name, indio_dev->id);
+
+     if (!trig)
+             return -ENOMEM;
+
+     data = iio_priv(indio_dev);
+     data->client = client;
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+     data->trig = trig;
+     trig->dev.parent = indio_dev->dev.parent;
+     trig->ops = &atlas_interrupt_trigger_ops;
+     iio_trigger_set_drvdata(trig, indio_dev);
+
+     i2c_set_clientdata(client, indio_dev);
+
+     data->regmap = devm_regmap_init_i2c(client,

&atlas_regmap_config);

Quoted 2 lines

drivers/iio/chemical/atlas-ph-sensor.c line 362

Quoted 2 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +2 -0
+     if (IS_ERR(data->regmap)) {
+             dev_err(&client->dev, "regmap initialization

failed\n");

Quoted 15 lines
Quoted 15 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +15 -0
+             return PTR_ERR(data->regmap);
+     }
+
+     ret = pm_runtime_set_active(&client->dev);
+     if (ret)
+             return ret;
+
+     if (client->irq <= 0) {
+             dev_err(&client->dev, "no valid irq defined\n");
+             return -EINVAL;
+     }
+
+     ret = atlas_check_calibration(data);
+     if (ret)
+             return ret;
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+
+     ret = iio_trigger_register(trig);
+     if (ret) {
+             dev_err(&client->dev, "failed to register trigger\n");
+             return ret;
+     }
+
+     ret = iio_triggered_buffer_setup(indio_dev,

&iio_pollfunc_store_time,

Quoted 4 lines
Quoted 4 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +4 -0
+             &atlas_trigger_handler, &atlas_buffer_setup_ops);
+     if (ret) {
+             dev_err(&client->dev, "cannot setup iio trigger\n");
+             goto unregister_trigger;
Quoted 8 lines

drivers/iio/chemical/atlas-ph-sensor.c

Quoted 8 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +8 -0
+     }
+
+     init_irq_work(&data->work, atlas_work_handler);
+
+     ret = devm_request_threaded_irq(&client->dev, client->irq,
+                                     NULL, atlas_interrupt_handler,
+                                     IRQF_TRIGGER_RISING |
+                                     IRQF_TRIGGER_FALLING |

IRQF_ONESHOT,

Quoted 6 lines

Both rising and falling? Seems odd for the usecase seen here (end of conversion type interrupt).

Quoted 4 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +4 -0
+                                     "atlas_irq",
+                                     indio_dev);
+     if (ret) {
+             dev_err(&client->dev, "request irq (%d) failed\n",

client->irq);

Quoted 26 lines
Quoted 11 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +11 -0
+             goto unregister_buffer;
+     }
+
+     ret = atlas_set_powermode(data, 1);
+     if (ret) {
+             dev_err(&client->dev, "cannot power device on");
+             goto unregister_buffer;
+     }
+
+     pm_runtime_enable(&client->dev);
+     pm_runtime_set_autosuspend_delay(&client->dev, 2500);

drivers/iio/chemical/atlas-ph-sensor.c line 422

Quoted 10 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +10 -0
+     pm_runtime_use_autosuspend(&client->dev);
+
+     ret = iio_device_register(indio_dev);
+     if (ret) {
+             dev_err(&client->dev, "unable to register device\n");
+             goto unregister_buffer;
+     }
+
+     return 0;
+

I guess it's not overly important, but seems to me that you ought to also be calling
pm_runtime_disable if you've called the enable before hitting an error.

Quoted 5 lines

+ powering down the device would be nice I guess.

I always expect to see most of the remove function matching the probe error paths, so wonder what is going on if bits are missing...

Ok will look into this.

Quoted 78 lines
Quoted 77 lines
drivers/iio/chemical/atlas-ph-sensor.c new file +75 -0
+unregister_buffer:
+     iio_triggered_buffer_cleanup(indio_dev);
+
+unregister_trigger:
+     iio_trigger_unregister(data->trig);
+
+     return ret;
+}
+
+static int atlas_remove(struct i2c_client *client)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(client);
+     struct atlas_data *data = iio_priv(indio_dev);
+
+     iio_device_unregister(indio_dev);
+     iio_triggered_buffer_cleanup(indio_dev);
+     iio_trigger_unregister(data->trig);
+
+     pm_runtime_disable(&client->dev);
+     pm_runtime_set_suspended(&client->dev);
+     pm_runtime_put_noidle(&client->dev);
+
+     return atlas_set_powermode(data, 0);
+}
+
+#ifdef CONFIG_PM
+static int atlas_runtime_suspend(struct device *dev)
+{
+     struct atlas_data *data =
+                  iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
+
+     return atlas_set_powermode(data, 0);
+}
+
+static int atlas_runtime_resume(struct device *dev)
+{
+     struct atlas_data *data =
+                  iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
+
+     return atlas_set_powermode(data, 1);
+}
+#endif
+
+static const struct dev_pm_ops atlas_pm_ops = {
+     SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
+                        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,
+             .of_match_table = of_match_ptr(atlas_dt_ids),
+             .pm     = &atlas_pm_ops,
+     },
+     .probe          = atlas_probe,
+     .remove         = atlas_remove,
+     .id_table       = atlas_id,
+};
+module_i2c_driver(atlas_driver);
+
+MODULE_AUTHOR("Matt Ranostay <[email protected]>");
+MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
+MODULE_LICENSE("GPL");
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.