1 // SPDX-License-Identifier: GPL-2.0
3 * Plantower PMS7003 particulate matter sensor driver
5 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
8 #include <asm/unaligned.h>
9 #include <linux/completion.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/iio/buffer.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/trigger_consumer.h>
15 #include <linux/iio/triggered_buffer.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/serdev.h>
23 #define PMS7003_DRIVER_NAME "pms7003"
25 #define PMS7003_MAGIC 0x424d
26 /* last 2 data bytes hold frame checksum */
27 #define PMS7003_MAX_DATA_LENGTH 28
28 #define PMS7003_CHECKSUM_LENGTH 2
29 #define PMS7003_PM10_OFFSET 10
30 #define PMS7003_PM2P5_OFFSET 8
31 #define PMS7003_PM1_OFFSET 6
33 #define PMS7003_TIMEOUT msecs_to_jiffies(6000)
34 #define PMS7003_CMD_LENGTH 7
35 #define PMS7003_PM_MAX 1000
36 #define PMS7003_PM_MIN 0
46 CMD_ENTER_PASSIVE_MODE,
52 * commands have following format:
54 * +------+------+-----+------+-----+-----------+-----------+
55 * | 0x42 | 0x4d | cmd | 0x00 | arg | cksum msb | cksum lsb |
56 * +------+------+-----+------+-----+-----------+-----------+
58 static const u8 pms7003_cmd_tbl[][PMS7003_CMD_LENGTH] = {
59 [CMD_WAKEUP] = { 0x42, 0x4d, 0xe4, 0x00, 0x01, 0x01, 0x74 },
60 [CMD_ENTER_PASSIVE_MODE] = { 0x42, 0x4d, 0xe1, 0x00, 0x00, 0x01, 0x70 },
61 [CMD_READ_PASSIVE] = { 0x42, 0x4d, 0xe2, 0x00, 0x00, 0x01, 0x71 },
62 [CMD_SLEEP] = { 0x42, 0x4d, 0xe4, 0x00, 0x00, 0x01, 0x73 },
65 struct pms7003_frame {
66 u8 data[PMS7003_MAX_DATA_LENGTH];
71 struct pms7003_state {
72 struct serdev_device *serdev;
73 struct pms7003_frame frame;
74 struct completion frame_ready;
75 struct mutex lock; /* must be held whenever state gets touched */
78 static int pms7003_do_cmd(struct pms7003_state *state, enum pms7003_cmd cmd)
82 ret = serdev_device_write(state->serdev, pms7003_cmd_tbl[cmd],
83 PMS7003_CMD_LENGTH, PMS7003_TIMEOUT);
84 if (ret < PMS7003_CMD_LENGTH)
85 return ret < 0 ? ret : -EIO;
87 ret = wait_for_completion_interruptible_timeout(&state->frame_ready,
92 return ret < 0 ? ret : 0;
95 static u16 pms7003_get_pm(const u8 *data)
97 return clamp_val(get_unaligned_be16(data),
98 PMS7003_PM_MIN, PMS7003_PM_MAX);
101 static irqreturn_t pms7003_trigger_handler(int irq, void *p)
103 struct iio_poll_func *pf = p;
104 struct iio_dev *indio_dev = pf->indio_dev;
105 struct pms7003_state *state = iio_priv(indio_dev);
106 struct pms7003_frame *frame = &state->frame;
107 u16 data[3 + 1 + 4]; /* PM1, PM2P5, PM10, padding, timestamp */
110 mutex_lock(&state->lock);
111 ret = pms7003_do_cmd(state, CMD_READ_PASSIVE);
113 mutex_unlock(&state->lock);
117 data[PM1] = pms7003_get_pm(frame->data + PMS7003_PM1_OFFSET);
118 data[PM2P5] = pms7003_get_pm(frame->data + PMS7003_PM2P5_OFFSET);
119 data[PM10] = pms7003_get_pm(frame->data + PMS7003_PM10_OFFSET);
120 mutex_unlock(&state->lock);
122 iio_push_to_buffers_with_timestamp(indio_dev, data,
123 iio_get_time_ns(indio_dev));
125 iio_trigger_notify_done(indio_dev->trig);
130 static int pms7003_read_raw(struct iio_dev *indio_dev,
131 struct iio_chan_spec const *chan,
132 int *val, int *val2, long mask)
134 struct pms7003_state *state = iio_priv(indio_dev);
135 struct pms7003_frame *frame = &state->frame;
139 case IIO_CHAN_INFO_PROCESSED:
140 switch (chan->type) {
141 case IIO_MASSCONCENTRATION:
142 mutex_lock(&state->lock);
143 ret = pms7003_do_cmd(state, CMD_READ_PASSIVE);
145 mutex_unlock(&state->lock);
149 *val = pms7003_get_pm(frame->data + chan->address);
150 mutex_unlock(&state->lock);
161 static const struct iio_info pms7003_info = {
162 .read_raw = pms7003_read_raw,
165 #define PMS7003_CHAN(_index, _mod, _addr) { \
166 .type = IIO_MASSCONCENTRATION, \
168 .channel2 = IIO_MOD_ ## _mod, \
170 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
171 .scan_index = _index, \
176 .endianness = IIO_CPU, \
180 static const struct iio_chan_spec pms7003_channels[] = {
181 PMS7003_CHAN(0, PM1, PMS7003_PM1_OFFSET),
182 PMS7003_CHAN(1, PM2P5, PMS7003_PM2P5_OFFSET),
183 PMS7003_CHAN(2, PM10, PMS7003_PM10_OFFSET),
184 IIO_CHAN_SOFT_TIMESTAMP(3),
187 static u16 pms7003_calc_checksum(struct pms7003_frame *frame)
189 u16 checksum = (PMS7003_MAGIC >> 8) + (u8)(PMS7003_MAGIC & 0xff) +
190 (frame->length >> 8) + (u8)frame->length;
193 for (i = 0; i < frame->length - PMS7003_CHECKSUM_LENGTH; i++)
194 checksum += frame->data[i];
199 static bool pms7003_frame_is_okay(struct pms7003_frame *frame)
201 int offset = frame->length - PMS7003_CHECKSUM_LENGTH;
202 u16 checksum = get_unaligned_be16(frame->data + offset);
204 return checksum == pms7003_calc_checksum(frame);
207 static int pms7003_receive_buf(struct serdev_device *serdev,
208 const unsigned char *buf, size_t size)
210 struct iio_dev *indio_dev = serdev_device_get_drvdata(serdev);
211 struct pms7003_state *state = iio_priv(indio_dev);
212 struct pms7003_frame *frame = &state->frame;
215 if (!frame->expected_length) {
218 /* wait for SOF and data length */
222 magic = get_unaligned_be16(buf);
223 if (magic != PMS7003_MAGIC)
226 num = get_unaligned_be16(buf + 2);
227 if (num <= PMS7003_MAX_DATA_LENGTH) {
228 frame->expected_length = num;
235 num = min(size, (size_t)(frame->expected_length - frame->length));
236 memcpy(frame->data + frame->length, buf, num);
237 frame->length += num;
239 if (frame->length == frame->expected_length) {
240 if (pms7003_frame_is_okay(frame))
241 complete(&state->frame_ready);
243 frame->expected_length = 0;
249 static const struct serdev_device_ops pms7003_serdev_ops = {
250 .receive_buf = pms7003_receive_buf,
251 .write_wakeup = serdev_device_write_wakeup,
254 static void pms7003_stop(void *data)
256 struct pms7003_state *state = data;
258 pms7003_do_cmd(state, CMD_SLEEP);
261 static const unsigned long pms7003_scan_masks[] = { 0x07, 0x00 };
263 static int pms7003_probe(struct serdev_device *serdev)
265 struct pms7003_state *state;
266 struct iio_dev *indio_dev;
269 indio_dev = devm_iio_device_alloc(&serdev->dev, sizeof(*state));
273 state = iio_priv(indio_dev);
274 serdev_device_set_drvdata(serdev, indio_dev);
275 state->serdev = serdev;
276 indio_dev->dev.parent = &serdev->dev;
277 indio_dev->info = &pms7003_info;
278 indio_dev->name = PMS7003_DRIVER_NAME;
279 indio_dev->channels = pms7003_channels,
280 indio_dev->num_channels = ARRAY_SIZE(pms7003_channels);
281 indio_dev->modes = INDIO_DIRECT_MODE;
282 indio_dev->available_scan_masks = pms7003_scan_masks;
284 mutex_init(&state->lock);
285 init_completion(&state->frame_ready);
287 serdev_device_set_client_ops(serdev, &pms7003_serdev_ops);
288 ret = devm_serdev_device_open(&serdev->dev, serdev);
292 serdev_device_set_baudrate(serdev, 9600);
293 serdev_device_set_flow_control(serdev, false);
295 ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
299 ret = pms7003_do_cmd(state, CMD_WAKEUP);
301 dev_err(&serdev->dev, "failed to wakeup sensor\n");
305 ret = pms7003_do_cmd(state, CMD_ENTER_PASSIVE_MODE);
307 dev_err(&serdev->dev, "failed to enter passive mode\n");
311 ret = devm_add_action_or_reset(&serdev->dev, pms7003_stop, state);
315 ret = devm_iio_triggered_buffer_setup(&serdev->dev, indio_dev, NULL,
316 pms7003_trigger_handler, NULL);
320 return devm_iio_device_register(&serdev->dev, indio_dev);
323 static const struct of_device_id pms7003_of_match[] = {
324 { .compatible = "plantower,pms1003" },
325 { .compatible = "plantower,pms3003" },
326 { .compatible = "plantower,pms5003" },
327 { .compatible = "plantower,pms6003" },
328 { .compatible = "plantower,pms7003" },
329 { .compatible = "plantower,pmsa003" },
332 MODULE_DEVICE_TABLE(of, pms7003_of_match);
334 static struct serdev_device_driver pms7003_driver = {
336 .name = PMS7003_DRIVER_NAME,
337 .of_match_table = pms7003_of_match,
339 .probe = pms7003_probe,
341 module_serdev_device_driver(pms7003_driver);
343 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
344 MODULE_DESCRIPTION("Plantower PMS7003 particulate matter sensor driver");
345 MODULE_LICENSE("GPL v2");