Skip to contentSkip to conversation

add support for PMS7003 PM sensor

PATCH Superseded Tomasz Duszynski 3 participants 12 messages Jan 27, 2019 – Feb 1, 2019

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tomasz Duszynski

author maintainer
#0 lore raw

v2 cover add support for PMS7003 PM sensor

This patch series adds support for Plantower PMS7003 PM matter sensor.

Unfortunately datasheet is not available for download from the Plantower website so one needs to find it elsewhere, for instance here:

https://download.kamami.com/p564008-p564008-PMS7003%20series%20data%20manua_English_V2.5.pdf

v2:
* check number of bytes returned from serdev_device_write()
(due to changes in linux 5.0 serdev api)
* add missing pins descriptions to the driver binding
* keep commands in the lookup table
* buffer data and process everything in one go instead of using state machine
* keep pm offset in channel address to simplify reading measurements

Tomasz Duszynski (3):
iio: chemical: add support for Plantower PMS7003 sensor
dt-bindings: add Plantower to the vendor prefixes
dt-bindings: iio: chemical: pms7003: add device tree support

 .../iio/chemical/plantower,pms7003.txt        |  19 +
 .../devicetree/bindings/vendor-prefixes.txt   |   1 +
 drivers/iio/chemical/Kconfig                  |  10 +
 drivers/iio/chemical/Makefile                 |   1 +
 drivers/iio/chemical/pms7003.c                | 343 ++++++++++++++++++
 5 files changed, 374 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt
 create mode 100644 drivers/iio/chemical/pms7003.c

--
2.20.1

author maintainer
#1 lore raw

PATCH v2 1/3 iio: chemical: add support for Plantower PMS7003 sensor

Add support for Plantower PMS7003 particulate matter sensor.

---
 drivers/iio/chemical/Kconfig   |  10 +
 drivers/iio/chemical/Makefile  |   1 +
 drivers/iio/chemical/pms7003.c | 343 +++++++++++++++++++++++++++++++++
 3 files changed, 354 insertions(+)
 create mode 100644 drivers/iio/chemical/pms7003.c
drivers/iio/chemical/Kconfig +10 -0
index 57832b4360e9..d5d146e9e372 100644
@@ -61,6 +61,16 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config PMS7003
+	tristate "Plantower PMS7003 particulate matter sensor"
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here to build support for the Plantower PMS7003 particulate
+	  matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called pms7003.
+
 config SPS30
 	tristate "SPS30 particulate matter sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 65bf0f89c0e4..f5d1365acb49 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_PMS7003) += pms7003.o
 obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/pms7003.c new file +343 -0

343 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH v2 1/3] iio: chemical: add support for Plantower PMS7003 sensor

On Sun, 27 Jan 2019, Tomasz Duszynski wrote:

some minor comments below, looks good to me

Quoted 49 lines

Add support for Plantower PMS7003 particulate matter sensor.

---
 drivers/iio/chemical/Kconfig   |  10 +
 drivers/iio/chemical/Makefile  |   1 +
 drivers/iio/chemical/pms7003.c | 343 +++++++++++++++++++++++++++++++++
 3 files changed, 354 insertions(+)
 create mode 100644 drivers/iio/chemical/pms7003.c
drivers/iio/chemical/Kconfig +10 -0
index 57832b4360e9..d5d146e9e372 100644
@@ -61,6 +61,16 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config PMS7003
+	tristate "Plantower PMS7003 particulate matter sensor"
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here to build support for the Plantower PMS7003 particulate
+	  matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called pms7003.
+
 config SPS30
 	tristate "SPS30 particulate matter sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 65bf0f89c0e4..f5d1365acb49 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_PMS7003) += pms7003.o
 obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/pms7003.c new file +0 -0
new file mode 100644
index 000000000000..b1ac4aeea989

drivers/iio/chemical/pms7003.c line 8

Quoted 8 lines
drivers/iio/chemical/pms7003.c new file +8 -0
@@ -0,0 +1,343 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Plantower PMS7003 particulate matter sensor driver
+ *
+ * Copyright (c) Tomasz Duszynski <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

pr_fmt() is not used

Quoted 144 lines
drivers/iio/chemical/pms7003.c new file +144 -0
+
+#include <asm/unaligned.h>
+#include <linux/completion.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/serdev.h>
+
+#define PMS7003_DRIVER_NAME "pms7003"
+
+#define PMS7003_MAGIC 0x424d
+/* last 2 data bytes hold frame checksum */
+#define PMS7003_MAX_DATA_LENGTH 28
+#define PMS7003_CHECKSUM_LENGTH 2
+#define PMS7003_PM10_OFFSET 10
+#define PMS7003_PM2P5_OFFSET 8
+#define PMS7003_PM1_OFFSET 6
+
+#define PMS7003_TIMEOUT msecs_to_jiffies(6000)
+#define PMS7003_CMD_LENGTH 7
+#define PMS7003_PM_MAX 1000
+#define PMS7003_PM_MIN 0
+
+enum {
+	PM1,
+	PM2P5,
+	PM10,
+};
+
+enum pms7003_cmd {
+	CMD_WAKEUP,
+	CMD_ENTER_PASSIVE_MODE,
+	CMD_READ_PASSIVE,
+	CMD_SLEEP,
+};
+
+/*
+ * commands have following format:
+ *
+ * +------+------+-----+------+-----+-----------+-----------+
+ * | 0x42 | 0x4d | cmd | 0x00 | arg | cksum msb | cksum lsb |
+ * +------+------+-----+------+-----+-----------+-----------+
+ */
+static const u8 pms7003_cmd_tbl[][PMS7003_CMD_LENGTH] = {
+	[CMD_WAKEUP] = { 0x42, 0x4d, 0xe4, 0x00, 0x01, 0x01, 0x74 },
+	[CMD_ENTER_PASSIVE_MODE] = { 0x42, 0x4d, 0xe1, 0x00, 0x00, 0x01, 0x70 },
+	[CMD_READ_PASSIVE] = { 0x42, 0x4d, 0xe2, 0x00, 0x00, 0x01, 0x71 },
+	[CMD_SLEEP] = { 0x42, 0x4d, 0xe4, 0x00, 0x00, 0x01, 0x73 },
+};
+
+struct pms7003_frame {
+	u8 data[PMS7003_MAX_DATA_LENGTH];
+	u16 expected_length;
+	u16 length;
+};
+
+struct pms7003_state {
+	struct serdev_device *serdev;
+	struct pms7003_frame frame;
+	struct completion frame_ready;
+	struct mutex lock; /* must be held whenever state gets touched */
+};
+
+static int pms7003_do_cmd(struct pms7003_state *state, enum pms7003_cmd cmd)
+{
+	int ret;
+
+	ret = serdev_device_write(state->serdev, pms7003_cmd_tbl[cmd],
+				  PMS7003_CMD_LENGTH, PMS7003_TIMEOUT);
+	if (ret < PMS7003_CMD_LENGTH)
+		return ret < 0 ? ret : -EIO;
+
+	ret = wait_for_completion_interruptible_timeout(&state->frame_ready,
+							PMS7003_TIMEOUT);
+	if (!ret)
+		ret = -ETIMEDOUT;
+
+	return ret < 0 ? ret : 0;
+}
+
+static u16 pms7003_get_pm(const u8 *data)
+{
+	return clamp_val(get_unaligned_be16(data),
+			 PMS7003_PM_MIN, PMS7003_PM_MAX);
+}
+
+static irqreturn_t pms7003_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct pms7003_state *state = iio_priv(indio_dev);
+	struct pms7003_frame *frame = &state->frame;
+	u16 data[3 + 1 + 4]; /* PM1, PM2P5, PM10, padding, timestamp */
+	int ret;
+
+	mutex_lock(&state->lock);
+	ret = pms7003_do_cmd(state, CMD_READ_PASSIVE);
+	if (ret) {
+		mutex_unlock(&state->lock);
+		goto err;
+	}
+
+	data[PM1] = pms7003_get_pm(frame->data + PMS7003_PM1_OFFSET);
+	data[PM2P5] = pms7003_get_pm(frame->data + PMS7003_PM2P5_OFFSET);
+	data[PM10] = pms7003_get_pm(frame->data + PMS7003_PM10_OFFSET);
+	mutex_unlock(&state->lock);
+
+	iio_push_to_buffers_with_timestamp(indio_dev, data,
+					   iio_get_time_ns(indio_dev));
+err:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int pms7003_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long mask)
+{
+	struct pms7003_state *state = iio_priv(indio_dev);
+	struct pms7003_frame *frame = &state->frame;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			ret = pms7003_do_cmd(state, CMD_READ_PASSIVE);
+			if (ret) {
+				mutex_unlock(&state->lock);
+				return ret;
+			}
+
+			*val = pms7003_get_pm(frame->data + chan->address);
+			mutex_unlock(&state->lock);

drivers/iio/chemical/pms7003.c line 162

Quoted 10 lines
drivers/iio/chemical/pms7003.c new file +10 -0
+
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+	}
+
+	return -EINVAL;
+}
+

remove dup newline

Quoted 38 lines
drivers/iio/chemical/pms7003.c new file +38 -0
+
+static const struct iio_info pms7003_info = {
+	.read_raw = pms7003_read_raw,
+};
+
+#define PMS7003_CHAN(_index, _mod, _addr) { \
+	.type = IIO_MASSCONCENTRATION, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _mod, \
+	.address = _addr, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = _index, \
+	.scan_type = { \
+		.sign = 'u', \
+		.realbits = 10, \
+		.storagebits = 16, \
+		.endianness = IIO_CPU, \
+	}, \
+}
+
+static const struct iio_chan_spec pms7003_channels[] = {
+	PMS7003_CHAN(0, PM1, PMS7003_PM1_OFFSET),
+	PMS7003_CHAN(1, PM2P5, PMS7003_PM2P5_OFFSET),
+	PMS7003_CHAN(2, PM10, PMS7003_PM10_OFFSET),
+	IIO_CHAN_SOFT_TIMESTAMP(3),
+};
+
+static u16 pms7003_calc_checksum(struct pms7003_frame *frame)
+{
+	u16 checksum = (PMS7003_MAGIC >> 8) + (u8)PMS7003_MAGIC +
+		       (frame->length >> 8) + (u8)frame->length;
+	int i;
+
+	for (i = 0; i < frame->length - PMS7003_CHECKSUM_LENGTH; i++)
+		checksum += frame->data[i];
+
+	return checksum;
+}

drivers/iio/chemical/pms7003.c line 210

Quoted 10 lines
drivers/iio/chemical/pms7003.c new file +10 -0
+
+static bool pms7003_frame_is_okay(struct pms7003_frame *frame)
+{
+	int offset = frame->length - PMS7003_CHECKSUM_LENGTH;
+	u16 checksum = get_unaligned_be16(frame->data + offset);
+
+	return checksum == pms7003_calc_checksum(frame);
+}
+
+static int pms7003_receive_buf(struct serdev_device *serdev,

maybe add a comment what the return value of this function is

drivers/iio/chemical/pms7003.c line 221

Quoted 11 lines
drivers/iio/chemical/pms7003.c new file +11 -0
+			       const unsigned char *buf, size_t size)
+{
+	struct iio_dev *indio_dev = serdev_device_get_drvdata(serdev);
+	struct pms7003_state *state = iio_priv(indio_dev);
+	struct pms7003_frame *frame = &state->frame;
+	int num;
+
+	if (!frame->expected_length) {
+		u16 magic;
+
+		/* wait for SOF  and data length */

remove extra space after SOF

drivers/iio/chemical/pms7003.c line 229

Quoted 8 lines
drivers/iio/chemical/pms7003.c new file +8 -0
+		if (size < 4)
+			return 0;
+
+		magic = get_unaligned_be16(buf);
+		if (magic != PMS7003_MAGIC)
+			return 2;
+
+		num = get_unaligned_be16(buf + 2);

2 is sizeof(MAGIC)

Quoted 115 lines
drivers/iio/chemical/pms7003.c new file +114 -0
+		if (num <= PMS7003_MAX_DATA_LENGTH) {
+			frame->expected_length = num;
+			frame->length = 0;
+		}
+
+		return 4;
+	}
+
+	num = min(size, (size_t)(frame->expected_length - frame->length));
+	memcpy(frame->data + frame->length, buf, num);
+	frame->length += num;
+
+	if (frame->length == frame->expected_length) {
+		if (pms7003_frame_is_okay(frame))
+			complete(&state->frame_ready);
+
+		frame->expected_length = 0;
+	}
+
+	return num;
+}
+
+static const struct serdev_device_ops pms7003_serdev_ops = {
+	.receive_buf = pms7003_receive_buf,
+	.write_wakeup = serdev_device_write_wakeup,
+};
+
+static void pms7003_stop(void *data)
+{
+	struct pms7003_state *state = data;
+
+	pms7003_do_cmd(state, CMD_SLEEP);
+}
+
+static const unsigned long pms7003_scan_masks[] = { 0x07, 0x00 };
+
+static int pms7003_probe(struct serdev_device *serdev)
+{
+	struct pms7003_state *state;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&serdev->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	serdev_device_set_drvdata(serdev, indio_dev);
+	state->serdev = serdev;
+	indio_dev->dev.parent = &serdev->dev;
+	indio_dev->info = &pms7003_info;
+	indio_dev->name = PMS7003_DRIVER_NAME;
+	indio_dev->channels = pms7003_channels,
+	indio_dev->num_channels = ARRAY_SIZE(pms7003_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = pms7003_scan_masks;
+
+	mutex_init(&state->lock);
+	init_completion(&state->frame_ready);
+
+	serdev_device_set_client_ops(serdev, &pms7003_serdev_ops);
+	ret = devm_serdev_device_open(&serdev->dev, serdev);
+	if (ret)
+		return ret;
+
+	serdev_device_set_baudrate(serdev, 9600);
+	serdev_device_set_flow_control(serdev, false);
+
+	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
+	if (ret)
+		return ret;
+
+	ret = pms7003_do_cmd(state, CMD_WAKEUP);
+	if (ret) {
+		dev_err(&serdev->dev, "failed to wakeup sensor\n");
+		return ret;
+	}
+
+	ret = pms7003_do_cmd(state, CMD_ENTER_PASSIVE_MODE);
+	if (ret) {
+		dev_err(&serdev->dev, "failed to enter passive mode\n");
+		return ret;
+	}
+
+	ret = devm_add_action_or_reset(&serdev->dev, pms7003_stop, state);
+	if (ret)
+		return ret;
+
+	ret = devm_iio_triggered_buffer_setup(&serdev->dev, indio_dev, NULL,
+					      pms7003_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&serdev->dev, indio_dev);
+}
+
+static const struct of_device_id pms7003_of_match[] = {
+	{ .compatible = "plantower,pms7003" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, pms7003_of_match);
+
+static struct serdev_device_driver pms7003_driver = {
+	.driver = {
+		.name = PMS7003_DRIVER_NAME,
+		.of_match_table = pms7003_of_match,
+	},
+	.probe = pms7003_probe,
+};
+module_serdev_device_driver(pms7003_driver);
+
+MODULE_AUTHOR("Tomasz Duszynski <[email protected]>");
+MODULE_DESCRIPTION("Plantower PMS7003 particulate matter sensor driver");
+MODULE_LICENSE("GPL v2");
Signature · 2 lines
Peter Meerwald-Stadler
Mobile: +43 664 24 44 418
author maintainer
#3 lore raw

Re: [PATCH v2 1/3] iio: chemical: add support for Plantower PMS7003 sensor

Quoted 4 lines · Peter Meerwald-Stadler

On Sun, 27 Jan 2019, Tomasz Duszynski wrote:

some minor comments below, looks good to me

Thanks for comments.

Quoted 51 lines
Quoted 51 lines

Add support for Plantower PMS7003 particulate matter sensor.

---
 drivers/iio/chemical/Kconfig   |  10 +
 drivers/iio/chemical/Makefile  |   1 +
 drivers/iio/chemical/pms7003.c | 343 +++++++++++++++++++++++++++++++++
 3 files changed, 354 insertions(+)
 create mode 100644 drivers/iio/chemical/pms7003.c
drivers/iio/chemical/Kconfig +10 -0
index 57832b4360e9..d5d146e9e372 100644
@@ -61,6 +61,16 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config PMS7003
+	tristate "Plantower PMS7003 particulate matter sensor"
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here to build support for the Plantower PMS7003 particulate
+	  matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called pms7003.
+
 config SPS30
 	tristate "SPS30 particulate matter sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 65bf0f89c0e4..f5d1365acb49 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_PMS7003) += pms7003.o
 obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/pms7003.c new file +2 -0
new file mode 100644
index 000000000000..b1ac4aeea989
@@ -0,0 +1,343 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
Quoted 9 lines

drivers/iio/chemical/pms7003.c line 8

Quoted 6 lines
drivers/iio/chemical/pms7003.c new file +6 -0
+ * Plantower PMS7003 particulate matter sensor driver
+ *
+ * Copyright (c) Tomasz Duszynski <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

pr_fmt() is not used

Good catch, indeed no calls to prink() in the driver code.

Quoted 148 lines
Quoted 148 lines
drivers/iio/chemical/pms7003.c new file +148 -0
+
+#include <asm/unaligned.h>
+#include <linux/completion.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/serdev.h>
+
+#define PMS7003_DRIVER_NAME "pms7003"
+
+#define PMS7003_MAGIC 0x424d
+/* last 2 data bytes hold frame checksum */
+#define PMS7003_MAX_DATA_LENGTH 28
+#define PMS7003_CHECKSUM_LENGTH 2
+#define PMS7003_PM10_OFFSET 10
+#define PMS7003_PM2P5_OFFSET 8
+#define PMS7003_PM1_OFFSET 6
+
+#define PMS7003_TIMEOUT msecs_to_jiffies(6000)
+#define PMS7003_CMD_LENGTH 7
+#define PMS7003_PM_MAX 1000
+#define PMS7003_PM_MIN 0
+
+enum {
+	PM1,
+	PM2P5,
+	PM10,
+};
+
+enum pms7003_cmd {
+	CMD_WAKEUP,
+	CMD_ENTER_PASSIVE_MODE,
+	CMD_READ_PASSIVE,
+	CMD_SLEEP,
+};
+
+/*
+ * commands have following format:
+ *
+ * +------+------+-----+------+-----+-----------+-----------+
+ * | 0x42 | 0x4d | cmd | 0x00 | arg | cksum msb | cksum lsb |
+ * +------+------+-----+------+-----+-----------+-----------+
+ */
+static const u8 pms7003_cmd_tbl[][PMS7003_CMD_LENGTH] = {
+	[CMD_WAKEUP] = { 0x42, 0x4d, 0xe4, 0x00, 0x01, 0x01, 0x74 },
+	[CMD_ENTER_PASSIVE_MODE] = { 0x42, 0x4d, 0xe1, 0x00, 0x00, 0x01, 0x70 },
+	[CMD_READ_PASSIVE] = { 0x42, 0x4d, 0xe2, 0x00, 0x00, 0x01, 0x71 },
+	[CMD_SLEEP] = { 0x42, 0x4d, 0xe4, 0x00, 0x00, 0x01, 0x73 },
+};
+
+struct pms7003_frame {
+	u8 data[PMS7003_MAX_DATA_LENGTH];
+	u16 expected_length;
+	u16 length;
+};
+
+struct pms7003_state {
+	struct serdev_device *serdev;
+	struct pms7003_frame frame;
+	struct completion frame_ready;
+	struct mutex lock; /* must be held whenever state gets touched */
+};
+
+static int pms7003_do_cmd(struct pms7003_state *state, enum pms7003_cmd cmd)
+{
+	int ret;
+
+	ret = serdev_device_write(state->serdev, pms7003_cmd_tbl[cmd],
+				  PMS7003_CMD_LENGTH, PMS7003_TIMEOUT);
+	if (ret < PMS7003_CMD_LENGTH)
+		return ret < 0 ? ret : -EIO;
+
+	ret = wait_for_completion_interruptible_timeout(&state->frame_ready,
+							PMS7003_TIMEOUT);
+	if (!ret)
+		ret = -ETIMEDOUT;
+
+	return ret < 0 ? ret : 0;
+}
+
+static u16 pms7003_get_pm(const u8 *data)
+{
+	return clamp_val(get_unaligned_be16(data),
+			 PMS7003_PM_MIN, PMS7003_PM_MAX);
+}
+
+static irqreturn_t pms7003_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct pms7003_state *state = iio_priv(indio_dev);
+	struct pms7003_frame *frame = &state->frame;
+	u16 data[3 + 1 + 4]; /* PM1, PM2P5, PM10, padding, timestamp */
+	int ret;
+
+	mutex_lock(&state->lock);
+	ret = pms7003_do_cmd(state, CMD_READ_PASSIVE);
+	if (ret) {
+		mutex_unlock(&state->lock);
+		goto err;
+	}
+
+	data[PM1] = pms7003_get_pm(frame->data + PMS7003_PM1_OFFSET);
+	data[PM2P5] = pms7003_get_pm(frame->data + PMS7003_PM2P5_OFFSET);
+	data[PM10] = pms7003_get_pm(frame->data + PMS7003_PM10_OFFSET);
+	mutex_unlock(&state->lock);
+
+	iio_push_to_buffers_with_timestamp(indio_dev, data,
+					   iio_get_time_ns(indio_dev));
+err:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int pms7003_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long mask)
+{
+	struct pms7003_state *state = iio_priv(indio_dev);
+	struct pms7003_frame *frame = &state->frame;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			ret = pms7003_do_cmd(state, CMD_READ_PASSIVE);
+			if (ret) {
+				mutex_unlock(&state->lock);
+				return ret;
+			}
+
+			*val = pms7003_get_pm(frame->data + chan->address);
+			mutex_unlock(&state->lock);
+
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
Quoted 9 lines

drivers/iio/chemical/pms7003.c line 159

Quoted 6 lines
drivers/iio/chemical/pms7003.c new file +6 -0
+		}
+	}
+
+	return -EINVAL;
+}
+

remove dup newline

Okay.

Quoted 42 lines
Quoted 42 lines
drivers/iio/chemical/pms7003.c new file +42 -0
+
+static const struct iio_info pms7003_info = {
+	.read_raw = pms7003_read_raw,
+};
+
+#define PMS7003_CHAN(_index, _mod, _addr) { \
+	.type = IIO_MASSCONCENTRATION, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _mod, \
+	.address = _addr, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = _index, \
+	.scan_type = { \
+		.sign = 'u', \
+		.realbits = 10, \
+		.storagebits = 16, \
+		.endianness = IIO_CPU, \
+	}, \
+}
+
+static const struct iio_chan_spec pms7003_channels[] = {
+	PMS7003_CHAN(0, PM1, PMS7003_PM1_OFFSET),
+	PMS7003_CHAN(1, PM2P5, PMS7003_PM2P5_OFFSET),
+	PMS7003_CHAN(2, PM10, PMS7003_PM10_OFFSET),
+	IIO_CHAN_SOFT_TIMESTAMP(3),
+};
+
+static u16 pms7003_calc_checksum(struct pms7003_frame *frame)
+{
+	u16 checksum = (PMS7003_MAGIC >> 8) + (u8)PMS7003_MAGIC +
+		       (frame->length >> 8) + (u8)frame->length;
+	int i;
+
+	for (i = 0; i < frame->length - PMS7003_CHECKSUM_LENGTH; i++)
+		checksum += frame->data[i];
+
+	return checksum;
+}
+
+static bool pms7003_frame_is_okay(struct pms7003_frame *frame)
+{
+	int offset = frame->length - PMS7003_CHECKSUM_LENGTH;
Quoted 9 lines

drivers/iio/chemical/pms7003.c line 210

Quoted 6 lines
drivers/iio/chemical/pms7003.c new file +6 -0
+	u16 checksum = get_unaligned_be16(frame->data + offset);
+
+	return checksum == pms7003_calc_checksum(frame);
+}
+
+static int pms7003_receive_buf(struct serdev_device *serdev,

maybe add a comment what the return value of this function is

From what I see serdev header file already has some documentation regarding this. Perhaps there's no real value in repeating it here again.

Quoted 5 lines
Quoted 5 lines
drivers/iio/chemical/pms7003.c new file +5 -0
+			       const unsigned char *buf, size_t size)
+{
+	struct iio_dev *indio_dev = serdev_device_get_drvdata(serdev);
+	struct pms7003_state *state = iio_priv(indio_dev);
+	struct pms7003_frame *frame = &state->frame;
Quoted 9 lines

drivers/iio/chemical/pms7003.c line 221

Quoted 6 lines
drivers/iio/chemical/pms7003.c new file +6 -0
+	int num;
+
+	if (!frame->expected_length) {
+		u16 magic;
+
+		/* wait for SOF  and data length */

remove extra space after SOF

Okay.

Quoted 11 lines

drivers/iio/chemical/pms7003.c line 229

Quoted 8 lines
drivers/iio/chemical/pms7003.c new file +8 -0
+		if (size < 4)
+			return 0;
+
+		magic = get_unaligned_be16(buf);
+		if (magic != PMS7003_MAGIC)
+			return 2;
+
+		num = get_unaligned_be16(buf + 2);

2 is sizeof(MAGIC)

If you do not have particularly strong opinion on this one I would like to leave it as is. Otherwise I would likely need to replace other numbers in this branch with sizeofs due to consistency.

Quoted 120 lines
Quoted 105 lines
drivers/iio/chemical/pms7003.c new file +104 -0
+		if (num <= PMS7003_MAX_DATA_LENGTH) {
+			frame->expected_length = num;
+			frame->length = 0;
+		}
+
+		return 4;
+	}
+
+	num = min(size, (size_t)(frame->expected_length - frame->length));
+	memcpy(frame->data + frame->length, buf, num);
+	frame->length += num;
+
+	if (frame->length == frame->expected_length) {
+		if (pms7003_frame_is_okay(frame))
+			complete(&state->frame_ready);
+
+		frame->expected_length = 0;
+	}
+
+	return num;
+}
+
+static const struct serdev_device_ops pms7003_serdev_ops = {
+	.receive_buf = pms7003_receive_buf,
+	.write_wakeup = serdev_device_write_wakeup,
+};
+
+static void pms7003_stop(void *data)
+{
+	struct pms7003_state *state = data;
+
+	pms7003_do_cmd(state, CMD_SLEEP);
+}
+
+static const unsigned long pms7003_scan_masks[] = { 0x07, 0x00 };
+
+static int pms7003_probe(struct serdev_device *serdev)
+{
+	struct pms7003_state *state;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&serdev->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	serdev_device_set_drvdata(serdev, indio_dev);
+	state->serdev = serdev;
+	indio_dev->dev.parent = &serdev->dev;
+	indio_dev->info = &pms7003_info;
+	indio_dev->name = PMS7003_DRIVER_NAME;
+	indio_dev->channels = pms7003_channels,
+	indio_dev->num_channels = ARRAY_SIZE(pms7003_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = pms7003_scan_masks;
+
+	mutex_init(&state->lock);
+	init_completion(&state->frame_ready);
+
+	serdev_device_set_client_ops(serdev, &pms7003_serdev_ops);
+	ret = devm_serdev_device_open(&serdev->dev, serdev);
+	if (ret)
+		return ret;
+
+	serdev_device_set_baudrate(serdev, 9600);
+	serdev_device_set_flow_control(serdev, false);
+
+	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
+	if (ret)
+		return ret;
+
+	ret = pms7003_do_cmd(state, CMD_WAKEUP);
+	if (ret) {
+		dev_err(&serdev->dev, "failed to wakeup sensor\n");
+		return ret;
+	}
+
+	ret = pms7003_do_cmd(state, CMD_ENTER_PASSIVE_MODE);
+	if (ret) {
+		dev_err(&serdev->dev, "failed to enter passive mode\n");
+		return ret;
+	}
+
+	ret = devm_add_action_or_reset(&serdev->dev, pms7003_stop, state);
+	if (ret)
+		return ret;
+
+	ret = devm_iio_triggered_buffer_setup(&serdev->dev, indio_dev, NULL,
+					      pms7003_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&serdev->dev, indio_dev);
+}
+
+static const struct of_device_id pms7003_of_match[] = {
+	{ .compatible = "plantower,pms7003" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, pms7003_of_match);
+
+static struct serdev_device_driver pms7003_driver = {
+	.driver = {

drivers/iio/chemical/pms7003.c line 343

Quoted 10 lines
drivers/iio/chemical/pms7003.c new file +10 -0
+		.name = PMS7003_DRIVER_NAME,
+		.of_match_table = pms7003_of_match,
+	},
+	.probe = pms7003_probe,
+};
+module_serdev_device_driver(pms7003_driver);
+
+MODULE_AUTHOR("Tomasz Duszynski <[email protected]>");
+MODULE_DESCRIPTION("Plantower PMS7003 particulate matter sensor driver");
+MODULE_LICENSE("GPL v2");

--

Peter Meerwald-Stadler
Mobile: +43 664 24 44 418

author maintainer
#4 lore raw

PATCH v2 2/3 dt-bindings: add Plantower to the vendor prefixes

In reply to Tomasz Duszynski

Add Plantower to the vendor prefixes.

---
 Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
 1 file changed, 1 insertion(+)
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 3cfe0c8b8250..fa391a650f3a 100644
@@ -300,6 +300,7 @@ phytec	PHYTEC Messtechnik GmbH
 picochip	Picochip Ltd
 pine64	Pine64
 pixcir  PIXCIR MICROELECTRONICS Co., Ltd
+plantower Plantower Co., Ltd
 plathome	Plat'Home Co., Ltd.
 plda	PLDA
 plx	Broadcom Corporation (formerly PLX Technology)

--
2.20.1

author maintainer
#5 lore raw

PATCH v2 3/3 dt-bindings: iio: chemical: pms7003: add device tree support

In reply to Tomasz Duszynski

Add device tree support for Plantower PMS7003 particulate matter sensor.

---
 .../iio/chemical/plantower,pms7003.txt        | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +19 -0
new file mode 100644
index 000000000000..e4c7f2fb1e30
@@ -0,0 +1,19 @@
+* Plantower PMS7003 particulate matter sensor
+
+Required properties:
+- compatible: must be "plantower,pms7003"
+
+Optional properties:
+- vcc-supply: phandle to the regulator that provides power to the sensor
+- set-gpios: phandle to the GPIO connected to the SET line
+- reset-gpios: phandle to the GPIO connected to the RESET line
+
+Refer to serial/slave-device.txt for generic serial attached device bindings.
+
+Example:
+
+&uart0 {
+	pms7003 {
+		compatible = "plantower,pms7003";
+	};
+};
Signature · 1 line
2.20.1
#6 lore raw

Re: [PATCH v2 3/3] dt-bindings: iio: chemical: pms7003: add device tree support

Quoted 14 lines · Tomasz Duszynski

Add device tree support for Plantower PMS7003 particulate matter sensor.

---
 .../iio/chemical/plantower,pms7003.txt        | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +0 -0
new file mode 100644
index 000000000000..e4c7f2fb1e30

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 7

Quoted 7 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +7 -0
@@ -0,0 +1,19 @@
+* Plantower PMS7003 particulate matter sensor
+
+Required properties:
+- compatible: must be "plantower,pms7003"
+
+Optional properties:
+- vcc-supply: phandle to the regulator that provides power to the sensor

Shouldn't this one be a required property?

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 16

Quoted 9 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +9 -0
+- set-gpios: phandle to the GPIO connected to the SET line
+- reset-gpios: phandle to the GPIO connected to the RESET line
+
+Refer to serial/slave-device.txt for generic serial attached device bindings.
+
+Example:
+
+&uart0 {
+	pms7003 {

The node name should be generic and reflect the functionality rather than model. Perhaps "pms" will do here.

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 19

Quoted 3 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +3 -0
+		compatible = "plantower,pms7003";
+	};
+};

Johan

author maintainer
#7 lore raw

Re: [PATCH v2 3/3] dt-bindings: iio: chemical: pms7003: add device tree support

Quoted 9 lines · Johan Hovold
Quoted 8 lines · Tomasz Duszynski

Add device tree support for Plantower PMS7003 particulate matter sensor.

---
 .../iio/chemical/plantower,pms7003.txt        | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt
Quoted 16 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 7

Quoted 13 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +7 -0
new file mode 100644
index 000000000000..e4c7f2fb1e30
@@ -0,0 +1,19 @@
+* Plantower PMS7003 particulate matter sensor
+
+Required properties:
+- compatible: must be "plantower,pms7003"
+
+Optional properties:
+- vcc-supply: phandle to the regulator that provides power to the sensor

Shouldn't this one be a required property?

Driver does not use regulator framework hence to me this property fits here better.

Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +4 -0
+- set-gpios: phandle to the GPIO connected to the SET line
+- reset-gpios: phandle to the GPIO connected to the RESET line
+
+Refer to serial/slave-device.txt for generic serial attached device bindings.
Quoted 9 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 16

Quoted 5 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +5 -0
+
+Example:
+
+&uart0 {
+	pms7003 {

The node name should be generic and reflect the functionality rather than model. Perhaps "pms" will do here.

Agree, ideally we should have a generic dt name for this kind of sensors (something like air-pollution-sensor perhaps?). But unfortunately there isn't anything available now so I guess compatible part name should be okay (besides this is the type of naming commonly used in other iio bindings).

Quoted 5 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 19

Quoted 3 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +3 -0
+		compatible = "plantower,pms7003";
+	};
+};

Johan

#8 lore raw

Re: [PATCH v2 3/3] dt-bindings: iio: chemical: pms7003: add device tree support

Quoted 27 lines · Tomasz Duszynski
Quoted 9 lines · Johan Hovold
Quoted 8 lines · Tomasz Duszynski

Add device tree support for Plantower PMS7003 particulate matter sensor.

---
 .../iio/chemical/plantower,pms7003.txt        | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt
Quoted 16 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 7

Quoted 13 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +7 -0
new file mode 100644
index 000000000000..e4c7f2fb1e30
@@ -0,0 +1,19 @@
+* Plantower PMS7003 particulate matter sensor
+
+Required properties:
+- compatible: must be "plantower,pms7003"
+
+Optional properties:
+- vcc-supply: phandle to the regulator that provides power to the sensor

Shouldn't this one be a required property?

Quoted 2 lines

Driver does not use regulator framework hence to me this property fits here better.

The device tree describes hardware, not any particular driver.

That said, there is a bit of an on-going debate on whether mandatory supplies (from a hardware perspective) should always be represented in device tree or not.

	https://lore.kernel.org/lkml/[email protected]/T/#u
        https://lore.kernel.org/lkml/[email protected]/T/#u
	https://lore.kernel.org/lkml/20180425171123.xhyoay3nu463btoq@rob-hp-laptop/T/#u
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +4 -0
+- set-gpios: phandle to the GPIO connected to the SET line
+- reset-gpios: phandle to the GPIO connected to the RESET line
+
+Refer to serial/slave-device.txt for generic serial attached device bindings.
Quoted 8 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 16

Quoted 5 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +5 -0
+
+Example:
+
+&uart0 {
+	pms7003 {

The node name should be generic and reflect the functionality rather than model. Perhaps "pms" will do here.

Quoted 4 lines

Agree, ideally we should have a generic dt name for this kind of sensors (something like air-pollution-sensor perhaps?). But unfortunately there isn't anything available now so I guess compatible part name should be okay (besides this is the type of naming commonly used in other iio bindings).

What's wrong with particulate matter sensor ("pms")?

Seems like a better fix than any particular model name to me at least.

Quoted 3 lines
Quoted 3 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 19

Quoted 3 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +3 -0
+		compatible = "plantower,pms7003";
+	};
+};

Johan

author maintainer
#9 lore raw

Re: [PATCH v2 3/3] dt-bindings: iio: chemical: pms7003: add device tree support

Quoted 33 lines · Johan Hovold
Quoted 27 lines · Tomasz Duszynski
Quoted 9 lines · Johan Hovold
Quoted 8 lines · Tomasz Duszynski

Add device tree support for Plantower PMS7003 particulate matter sensor.

---
 .../iio/chemical/plantower,pms7003.txt        | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt
Quoted 16 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 7

Quoted 13 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +7 -0
new file mode 100644
index 000000000000..e4c7f2fb1e30
@@ -0,0 +1,19 @@
+* Plantower PMS7003 particulate matter sensor
+
+Required properties:
+- compatible: must be "plantower,pms7003"
+
+Optional properties:
+- vcc-supply: phandle to the regulator that provides power to the sensor

Shouldn't this one be a required property?

Quoted 2 lines

Driver does not use regulator framework hence to me this property fits here better.

The device tree describes hardware, not any particular driver.

Quoted 8 lines

That said, there is a bit of an on-going debate on whether mandatory supplies (from a hardware perspective) should always be represented in device tree or not.

	https://lore.kernel.org/lkml/[email protected]/T/#u
        https://lore.kernel.org/lkml/[email protected]/T/#u
	https://lore.kernel.org/lkml/20180425171123.xhyoay3nu463btoq@rob-hp-laptop/T/#u

Even after going through this threads I am still unconvinced that vcc-supply should be a requirement. Making this a mandatory property would automatically imply using *_regulator_get() in a driver code (on condition one is adding regulator support). In case there isn't any physically connected we end up with a dummy one and a warning.

Is it how this should work?

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 16

Quoted 9 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +9 -0
+- set-gpios: phandle to the GPIO connected to the SET line
+- reset-gpios: phandle to the GPIO connected to the RESET line
+
+Refer to serial/slave-device.txt for generic serial attached device bindings.
+
+Example:
+
+&uart0 {
+	pms7003 {

The node name should be generic and reflect the functionality rather than model. Perhaps "pms" will do here.

Quoted 9 lines
Quoted 4 lines

Agree, ideally we should have a generic dt name for this kind of sensors (something like air-pollution-sensor perhaps?). But unfortunately there isn't anything available now so I guess compatible part name should be okay (besides this is the type of naming commonly used in other iio bindings).

What's wrong with particulate matter sensor ("pms")?

Seems like a better fix than any particular model name to me at least.

Personally I would vote for a more descriptive node name, for example:
particulate-matter-sensor or air-pollution-sensor or other name which clearly shows what device we are dealing with.

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 19

Quoted 3 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +3 -0
+		compatible = "plantower,pms7003";
+	};
+};

Johan

#10 lore raw

Re: [PATCH v2 3/3] dt-bindings: iio: chemical: pms7003: add device tree support

Quoted 43 lines · Tomasz Duszynski
Quoted 33 lines · Johan Hovold
Quoted 27 lines · Tomasz Duszynski
Quoted 9 lines · Johan Hovold
Quoted 8 lines · Tomasz Duszynski

Add device tree support for Plantower PMS7003 particulate matter sensor.

---
 .../iio/chemical/plantower,pms7003.txt        | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt
Quoted 16 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 7

Quoted 13 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +7 -0
new file mode 100644
index 000000000000..e4c7f2fb1e30
@@ -0,0 +1,19 @@
+* Plantower PMS7003 particulate matter sensor
+
+Required properties:
+- compatible: must be "plantower,pms7003"
+
+Optional properties:
+- vcc-supply: phandle to the regulator that provides power to the sensor

Shouldn't this one be a required property?

Quoted 2 lines

Driver does not use regulator framework hence to me this property fits here better.

The device tree describes hardware, not any particular driver.

Quoted 8 lines

That said, there is a bit of an on-going debate on whether mandatory supplies (from a hardware perspective) should always be represented in device tree or not.

	https://lore.kernel.org/lkml/[email protected]/T/#u
        https://lore.kernel.org/lkml/[email protected]/T/#u
	https://lore.kernel.org/lkml/20180425171123.xhyoay3nu463btoq@rob-hp-laptop/T/#u
Quoted 5 lines

Even after going through this threads I am still unconvinced that vcc-supply should be a requirement. Making this a mandatory property would automatically imply using *_regulator_get() in a driver code (on condition one is adding regulator support). In case there isn't any physically connected we end up with a dummy one and a warning.

How would the device work at all without a physical vcc supply?

Quoted 1 line

Is it how this should work?

As Mark mentioned in the threads above, you could add a fixed, always-on regulator for cases where the device is always powered.

Boards failing to describe this supply would still work on Linux, but would end up with a dummy regulator and a warning.

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +4 -0
+- set-gpios: phandle to the GPIO connected to the SET line
+- reset-gpios: phandle to the GPIO connected to the RESET line
+
+Refer to serial/slave-device.txt for generic serial attached device bindings.
Quoted 8 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 16

Quoted 5 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +5 -0
+
+Example:
+
+&uart0 {
+	pms7003 {

The node name should be generic and reflect the functionality rather than model. Perhaps "pms" will do here.

Agree, ideally we should have a generic dt name for this kind of sensors (something like air-pollution-sensor perhaps?). But unfortunately there isn't anything available now so I guess compatible part name should be okay (besides this is the type of naming commonly used in other iio bindings).

Quoted 8 lines
Quoted 4 lines

What's wrong with particulate matter sensor ("pms")?

Seems like a better fix than any particular model name to me at least.

Personally I would vote for a more descriptive node name, for example:
particulate-matter-sensor or air-pollution-sensor or other name which clearly shows what device we are dealing with.

Spelling it out seems preferable, yes. You know the domain better than I do; I only suggest you come up with a generic node name.

Johan

author maintainer
#11 lore raw

Re: [PATCH v2 3/3] dt-bindings: iio: chemical: pms7003: add device tree support

Quoted 44 lines · Johan Hovold
Quoted 43 lines · Tomasz Duszynski
Quoted 41 lines · Johan Hovold
Quoted 27 lines · Tomasz Duszynski
Quoted 9 lines · Johan Hovold
Quoted 8 lines · Tomasz Duszynski

Add device tree support for Plantower PMS7003 particulate matter sensor.

---
 .../iio/chemical/plantower,pms7003.txt        | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt
Quoted 16 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 7

Quoted 13 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +7 -0
new file mode 100644
index 000000000000..e4c7f2fb1e30
@@ -0,0 +1,19 @@
+* Plantower PMS7003 particulate matter sensor
+
+Required properties:
+- compatible: must be "plantower,pms7003"
+
+Optional properties:
+- vcc-supply: phandle to the regulator that provides power to the sensor

Shouldn't this one be a required property?

Quoted 2 lines

Driver does not use regulator framework hence to me this property fits here better.

The device tree describes hardware, not any particular driver.

That said, there is a bit of an on-going debate on whether mandatory supplies (from a hardware perspective) should always be represented in device tree or not.

	https://lore.kernel.org/lkml/[email protected]/T/#u
        https://lore.kernel.org/lkml/[email protected]/T/#u
	https://lore.kernel.org/lkml/20180425171123.xhyoay3nu463btoq@rob-hp-laptop/T/#u
Quoted 8 lines
Quoted 5 lines

Even after going through this threads I am still unconvinced that vcc-supply should be a requirement. Making this a mandatory property would automatically imply using *_regulator_get() in a driver code (on condition one is adding regulator support). In case there isn't any physically connected we end up with a dummy one and a warning.

How would the device work at all without a physical vcc supply?

I meant the discrete IC regulator.

Quoted 8 lines
Quoted 1 line

Is it how this should work?

As Mark mentioned in the threads above, you could add a fixed, always-on regulator for cases where the device is always powered.

Boards failing to describe this supply would still work on Linux, but would end up with a dummy regulator and a warning.

Fair enough.

Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +4 -0
+- set-gpios: phandle to the GPIO connected to the SET line
+- reset-gpios: phandle to the GPIO connected to the RESET line
+
+Refer to serial/slave-device.txt for generic serial attached device bindings.
Quoted 8 lines

Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt line 16

Quoted 5 lines
Documentation/devicetree/bindings/iio/chemical/plantower,pms7003.txt new file +5 -0
+
+Example:
+
+&uart0 {
+	pms7003 {

The node name should be generic and reflect the functionality rather than model. Perhaps "pms" will do here.

Agree, ideally we should have a generic dt name for this kind of sensors (something like air-pollution-sensor perhaps?). But unfortunately there isn't anything available now so I guess compatible part name should be okay (besides this is the type of naming commonly used in other iio bindings).

Quoted 8 lines
Quoted 4 lines

What's wrong with particulate matter sensor ("pms")?

Seems like a better fix than any particular model name to me at least.

Personally I would vote for a more descriptive node name, for example:
particulate-matter-sensor or air-pollution-sensor or other name which clearly shows what device we are dealing with.

Spelling it out seems preferable, yes. You know the domain better than I do; I only suggest you come up with a generic node name.

Johan