xref: /openbmc/linux/drivers/iio/chemical/pms7003.c (revision 3381df09)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Plantower PMS7003 particulate matter sensor driver
4  *
5  * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
6  */
7 
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>
22 
23 #define PMS7003_DRIVER_NAME "pms7003"
24 
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
32 
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
37 
38 enum {
39 	PM1,
40 	PM2P5,
41 	PM10,
42 };
43 
44 enum pms7003_cmd {
45 	CMD_WAKEUP,
46 	CMD_ENTER_PASSIVE_MODE,
47 	CMD_READ_PASSIVE,
48 	CMD_SLEEP,
49 };
50 
51 /*
52  * commands have following format:
53  *
54  * +------+------+-----+------+-----+-----------+-----------+
55  * | 0x42 | 0x4d | cmd | 0x00 | arg | cksum msb | cksum lsb |
56  * +------+------+-----+------+-----+-----------+-----------+
57  */
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 },
63 };
64 
65 struct pms7003_frame {
66 	u8 data[PMS7003_MAX_DATA_LENGTH];
67 	u16 expected_length;
68 	u16 length;
69 };
70 
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 */
76 };
77 
78 static int pms7003_do_cmd(struct pms7003_state *state, enum pms7003_cmd cmd)
79 {
80 	int ret;
81 
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;
86 
87 	ret = wait_for_completion_interruptible_timeout(&state->frame_ready,
88 							PMS7003_TIMEOUT);
89 	if (!ret)
90 		ret = -ETIMEDOUT;
91 
92 	return ret < 0 ? ret : 0;
93 }
94 
95 static u16 pms7003_get_pm(const u8 *data)
96 {
97 	return clamp_val(get_unaligned_be16(data),
98 			 PMS7003_PM_MIN, PMS7003_PM_MAX);
99 }
100 
101 static irqreturn_t pms7003_trigger_handler(int irq, void *p)
102 {
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 */
108 	int ret;
109 
110 	mutex_lock(&state->lock);
111 	ret = pms7003_do_cmd(state, CMD_READ_PASSIVE);
112 	if (ret) {
113 		mutex_unlock(&state->lock);
114 		goto err;
115 	}
116 
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);
121 
122 	iio_push_to_buffers_with_timestamp(indio_dev, data,
123 					   iio_get_time_ns(indio_dev));
124 err:
125 	iio_trigger_notify_done(indio_dev->trig);
126 
127 	return IRQ_HANDLED;
128 }
129 
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)
133 {
134 	struct pms7003_state *state = iio_priv(indio_dev);
135 	struct pms7003_frame *frame = &state->frame;
136 	int ret;
137 
138 	switch (mask) {
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);
144 			if (ret) {
145 				mutex_unlock(&state->lock);
146 				return ret;
147 			}
148 
149 			*val = pms7003_get_pm(frame->data + chan->address);
150 			mutex_unlock(&state->lock);
151 
152 			return IIO_VAL_INT;
153 		default:
154 			return -EINVAL;
155 		}
156 	}
157 
158 	return -EINVAL;
159 }
160 
161 static const struct iio_info pms7003_info = {
162 	.read_raw = pms7003_read_raw,
163 };
164 
165 #define PMS7003_CHAN(_index, _mod, _addr) { \
166 	.type = IIO_MASSCONCENTRATION, \
167 	.modified = 1, \
168 	.channel2 = IIO_MOD_ ## _mod, \
169 	.address = _addr, \
170 	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
171 	.scan_index = _index, \
172 	.scan_type = { \
173 		.sign = 'u', \
174 		.realbits = 10, \
175 		.storagebits = 16, \
176 		.endianness = IIO_CPU, \
177 	}, \
178 }
179 
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),
185 };
186 
187 static u16 pms7003_calc_checksum(struct pms7003_frame *frame)
188 {
189 	u16 checksum = (PMS7003_MAGIC >> 8) + (u8)(PMS7003_MAGIC & 0xff) +
190 		       (frame->length >> 8) + (u8)frame->length;
191 	int i;
192 
193 	for (i = 0; i < frame->length - PMS7003_CHECKSUM_LENGTH; i++)
194 		checksum += frame->data[i];
195 
196 	return checksum;
197 }
198 
199 static bool pms7003_frame_is_okay(struct pms7003_frame *frame)
200 {
201 	int offset = frame->length - PMS7003_CHECKSUM_LENGTH;
202 	u16 checksum = get_unaligned_be16(frame->data + offset);
203 
204 	return checksum == pms7003_calc_checksum(frame);
205 }
206 
207 static int pms7003_receive_buf(struct serdev_device *serdev,
208 			       const unsigned char *buf, size_t size)
209 {
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;
213 	int num;
214 
215 	if (!frame->expected_length) {
216 		u16 magic;
217 
218 		/* wait for SOF and data length */
219 		if (size < 4)
220 			return 0;
221 
222 		magic = get_unaligned_be16(buf);
223 		if (magic != PMS7003_MAGIC)
224 			return 2;
225 
226 		num = get_unaligned_be16(buf + 2);
227 		if (num <= PMS7003_MAX_DATA_LENGTH) {
228 			frame->expected_length = num;
229 			frame->length = 0;
230 		}
231 
232 		return 4;
233 	}
234 
235 	num = min(size, (size_t)(frame->expected_length - frame->length));
236 	memcpy(frame->data + frame->length, buf, num);
237 	frame->length += num;
238 
239 	if (frame->length == frame->expected_length) {
240 		if (pms7003_frame_is_okay(frame))
241 			complete(&state->frame_ready);
242 
243 		frame->expected_length = 0;
244 	}
245 
246 	return num;
247 }
248 
249 static const struct serdev_device_ops pms7003_serdev_ops = {
250 	.receive_buf = pms7003_receive_buf,
251 	.write_wakeup = serdev_device_write_wakeup,
252 };
253 
254 static void pms7003_stop(void *data)
255 {
256 	struct pms7003_state *state = data;
257 
258 	pms7003_do_cmd(state, CMD_SLEEP);
259 }
260 
261 static const unsigned long pms7003_scan_masks[] = { 0x07, 0x00 };
262 
263 static int pms7003_probe(struct serdev_device *serdev)
264 {
265 	struct pms7003_state *state;
266 	struct iio_dev *indio_dev;
267 	int ret;
268 
269 	indio_dev = devm_iio_device_alloc(&serdev->dev, sizeof(*state));
270 	if (!indio_dev)
271 		return -ENOMEM;
272 
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;
283 
284 	mutex_init(&state->lock);
285 	init_completion(&state->frame_ready);
286 
287 	serdev_device_set_client_ops(serdev, &pms7003_serdev_ops);
288 	ret = devm_serdev_device_open(&serdev->dev, serdev);
289 	if (ret)
290 		return ret;
291 
292 	serdev_device_set_baudrate(serdev, 9600);
293 	serdev_device_set_flow_control(serdev, false);
294 
295 	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
296 	if (ret)
297 		return ret;
298 
299 	ret = pms7003_do_cmd(state, CMD_WAKEUP);
300 	if (ret) {
301 		dev_err(&serdev->dev, "failed to wakeup sensor\n");
302 		return ret;
303 	}
304 
305 	ret = pms7003_do_cmd(state, CMD_ENTER_PASSIVE_MODE);
306 	if (ret) {
307 		dev_err(&serdev->dev, "failed to enter passive mode\n");
308 		return ret;
309 	}
310 
311 	ret = devm_add_action_or_reset(&serdev->dev, pms7003_stop, state);
312 	if (ret)
313 		return ret;
314 
315 	ret = devm_iio_triggered_buffer_setup(&serdev->dev, indio_dev, NULL,
316 					      pms7003_trigger_handler, NULL);
317 	if (ret)
318 		return ret;
319 
320 	return devm_iio_device_register(&serdev->dev, indio_dev);
321 }
322 
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" },
330 	{ }
331 };
332 MODULE_DEVICE_TABLE(of, pms7003_of_match);
333 
334 static struct serdev_device_driver pms7003_driver = {
335 	.driver = {
336 		.name = PMS7003_DRIVER_NAME,
337 		.of_match_table = pms7003_of_match,
338 	},
339 	.probe = pms7003_probe,
340 };
341 module_serdev_device_driver(pms7003_driver);
342 
343 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
344 MODULE_DESCRIPTION("Plantower PMS7003 particulate matter sensor driver");
345 MODULE_LICENSE("GPL v2");
346