xref: /openbmc/linux/drivers/iio/chemical/sps30_serial.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2e171f5STomasz Duszynski // SPDX-License-Identifier: GPL-2.0
2b2e171f5STomasz Duszynski /*
3b2e171f5STomasz Duszynski  * Sensirion SPS30 particulate matter sensor serial driver
4b2e171f5STomasz Duszynski  *
5b2e171f5STomasz Duszynski  * Copyright (c) 2021 Tomasz Duszynski <tomasz.duszynski@octakon.com>
6b2e171f5STomasz Duszynski  */
7b2e171f5STomasz Duszynski #include <linux/completion.h>
8b2e171f5STomasz Duszynski #include <linux/device.h>
9b2e171f5STomasz Duszynski #include <linux/errno.h>
10b2e171f5STomasz Duszynski #include <linux/iio/iio.h>
11b2e171f5STomasz Duszynski #include <linux/minmax.h>
12b2e171f5STomasz Duszynski #include <linux/mod_devicetable.h>
13b2e171f5STomasz Duszynski #include <linux/module.h>
14b2e171f5STomasz Duszynski #include <linux/serdev.h>
15b2e171f5STomasz Duszynski #include <linux/types.h>
16b2e171f5STomasz Duszynski 
17b2e171f5STomasz Duszynski #include "sps30.h"
18b2e171f5STomasz Duszynski 
19b2e171f5STomasz Duszynski #define SPS30_SERIAL_DEV_NAME "sps30"
20b2e171f5STomasz Duszynski 
21b2e171f5STomasz Duszynski #define SPS30_SERIAL_SOF_EOF 0x7e
22b2e171f5STomasz Duszynski #define SPS30_SERIAL_TIMEOUT msecs_to_jiffies(20)
23b2e171f5STomasz Duszynski #define SPS30_SERIAL_MAX_BUF_SIZE 263
24b2e171f5STomasz Duszynski #define SPS30_SERIAL_ESCAPE_CHAR 0x7d
25b2e171f5STomasz Duszynski 
26b2e171f5STomasz Duszynski #define SPS30_SERIAL_FRAME_MIN_SIZE 7
27b2e171f5STomasz Duszynski #define SPS30_SERIAL_FRAME_ADR_OFFSET 1
28b2e171f5STomasz Duszynski #define SPS30_SERIAL_FRAME_CMD_OFFSET 2
29b2e171f5STomasz Duszynski #define SPS30_SERIAL_FRAME_MOSI_LEN_OFFSET 3
30b2e171f5STomasz Duszynski #define SPS30_SERIAL_FRAME_MISO_STATE_OFFSET 3
31b2e171f5STomasz Duszynski #define SPS30_SERIAL_FRAME_MISO_LEN_OFFSET 4
32b2e171f5STomasz Duszynski #define SPS30_SERIAL_FRAME_MISO_DATA_OFFSET 5
33b2e171f5STomasz Duszynski 
34b2e171f5STomasz Duszynski #define SPS30_SERIAL_START_MEAS 0x00
35b2e171f5STomasz Duszynski #define SPS30_SERIAL_STOP_MEAS 0x01
36b2e171f5STomasz Duszynski #define SPS30_SERIAL_READ_MEAS 0x03
37b2e171f5STomasz Duszynski #define SPS30_SERIAL_RESET 0xd3
38b2e171f5STomasz Duszynski #define SPS30_SERIAL_CLEAN_FAN 0x56
39b2e171f5STomasz Duszynski #define SPS30_SERIAL_PERIOD 0x80
40b2e171f5STomasz Duszynski #define SPS30_SERIAL_DEV_INFO 0xd0
41b2e171f5STomasz Duszynski #define SPS30_SERIAL_READ_VERSION 0xd1
42b2e171f5STomasz Duszynski 
43b2e171f5STomasz Duszynski struct sps30_serial_priv {
44b2e171f5STomasz Duszynski 	struct completion new_frame;
45b2e171f5STomasz Duszynski 	unsigned char buf[SPS30_SERIAL_MAX_BUF_SIZE];
46b2e171f5STomasz Duszynski 	size_t num;
47b2e171f5STomasz Duszynski 	bool escaped;
48b2e171f5STomasz Duszynski 	bool done;
49b2e171f5STomasz Duszynski };
50b2e171f5STomasz Duszynski 
sps30_serial_xfer(struct sps30_state * state,const unsigned char * buf,size_t size)51b2e171f5STomasz Duszynski static int sps30_serial_xfer(struct sps30_state *state, const unsigned char *buf, size_t size)
52b2e171f5STomasz Duszynski {
53b2e171f5STomasz Duszynski 	struct serdev_device *serdev = to_serdev_device(state->dev);
54b2e171f5STomasz Duszynski 	struct sps30_serial_priv *priv = state->priv;
55b2e171f5STomasz Duszynski 	int ret;
56b2e171f5STomasz Duszynski 
57b2e171f5STomasz Duszynski 	priv->num = 0;
58b2e171f5STomasz Duszynski 	priv->escaped = false;
59b2e171f5STomasz Duszynski 	priv->done = false;
60b2e171f5STomasz Duszynski 
61b2e171f5STomasz Duszynski 	ret = serdev_device_write(serdev, buf, size, SPS30_SERIAL_TIMEOUT);
62b2e171f5STomasz Duszynski 	if (ret < 0)
63b2e171f5STomasz Duszynski 		return ret;
64b2e171f5STomasz Duszynski 	if (ret != size)
65b2e171f5STomasz Duszynski 		return -EIO;
66b2e171f5STomasz Duszynski 
67b2e171f5STomasz Duszynski 	ret = wait_for_completion_interruptible_timeout(&priv->new_frame, SPS30_SERIAL_TIMEOUT);
68b2e171f5STomasz Duszynski 	if (ret < 0)
69b2e171f5STomasz Duszynski 		return ret;
70b2e171f5STomasz Duszynski 	if (!ret)
71b2e171f5STomasz Duszynski 		return -ETIMEDOUT;
72b2e171f5STomasz Duszynski 
73b2e171f5STomasz Duszynski 	return 0;
74b2e171f5STomasz Duszynski }
75b2e171f5STomasz Duszynski 
76b2e171f5STomasz Duszynski static const struct {
77b2e171f5STomasz Duszynski 	unsigned char byte;
78b2e171f5STomasz Duszynski 	unsigned char byte2;
79b2e171f5STomasz Duszynski } sps30_serial_bytes[] = {
80b2e171f5STomasz Duszynski 	{ 0x11, 0x31 },
81b2e171f5STomasz Duszynski 	{ 0x13, 0x33 },
82b2e171f5STomasz Duszynski 	{ 0x7e, 0x5e },
83b2e171f5STomasz Duszynski 	{ 0x7d, 0x5d },
84b2e171f5STomasz Duszynski };
85b2e171f5STomasz Duszynski 
sps30_serial_put_byte(unsigned char * buf,unsigned char byte)86b2e171f5STomasz Duszynski static int sps30_serial_put_byte(unsigned char *buf, unsigned char byte)
87b2e171f5STomasz Duszynski {
88b2e171f5STomasz Duszynski 	int i;
89b2e171f5STomasz Duszynski 
90b2e171f5STomasz Duszynski 	for (i = 0; i < ARRAY_SIZE(sps30_serial_bytes); i++) {
91b2e171f5STomasz Duszynski 		if (sps30_serial_bytes[i].byte != byte)
92b2e171f5STomasz Duszynski 			continue;
93b2e171f5STomasz Duszynski 
94b2e171f5STomasz Duszynski 		buf[0] = SPS30_SERIAL_ESCAPE_CHAR;
95b2e171f5STomasz Duszynski 		buf[1] = sps30_serial_bytes[i].byte2;
96b2e171f5STomasz Duszynski 
97b2e171f5STomasz Duszynski 		return 2;
98b2e171f5STomasz Duszynski 	}
99b2e171f5STomasz Duszynski 
100b2e171f5STomasz Duszynski 	buf[0] = byte;
101b2e171f5STomasz Duszynski 
102b2e171f5STomasz Duszynski 	return 1;
103b2e171f5STomasz Duszynski }
104b2e171f5STomasz Duszynski 
sps30_serial_get_byte(bool escaped,unsigned char byte2)105b2e171f5STomasz Duszynski static char sps30_serial_get_byte(bool escaped, unsigned char byte2)
106b2e171f5STomasz Duszynski {
107b2e171f5STomasz Duszynski 	int i;
108b2e171f5STomasz Duszynski 
109b2e171f5STomasz Duszynski 	if (!escaped)
110b2e171f5STomasz Duszynski 		return byte2;
111b2e171f5STomasz Duszynski 
112b2e171f5STomasz Duszynski 	for (i = 0; i < ARRAY_SIZE(sps30_serial_bytes); i++) {
113b2e171f5STomasz Duszynski 		if (sps30_serial_bytes[i].byte2 != byte2)
114b2e171f5STomasz Duszynski 			continue;
115b2e171f5STomasz Duszynski 
116b2e171f5STomasz Duszynski 		return sps30_serial_bytes[i].byte;
117b2e171f5STomasz Duszynski 	}
118b2e171f5STomasz Duszynski 
119b2e171f5STomasz Duszynski 	return 0;
120b2e171f5STomasz Duszynski }
121b2e171f5STomasz Duszynski 
sps30_serial_calc_chksum(const unsigned char * buf,size_t num)122b2e171f5STomasz Duszynski static unsigned char sps30_serial_calc_chksum(const unsigned char *buf, size_t num)
123b2e171f5STomasz Duszynski {
124b2e171f5STomasz Duszynski 	unsigned int chksum = 0;
125b2e171f5STomasz Duszynski 	size_t i;
126b2e171f5STomasz Duszynski 
127b2e171f5STomasz Duszynski 	for (i = 0; i < num; i++)
128b2e171f5STomasz Duszynski 		chksum += buf[i];
129b2e171f5STomasz Duszynski 
130b2e171f5STomasz Duszynski 	return ~chksum;
131b2e171f5STomasz Duszynski }
132b2e171f5STomasz Duszynski 
sps30_serial_prep_frame(unsigned char * buf,unsigned char cmd,const unsigned char * arg,size_t arg_size)133b2e171f5STomasz Duszynski static int sps30_serial_prep_frame(unsigned char *buf, unsigned char cmd,
134b2e171f5STomasz Duszynski 				   const unsigned char *arg, size_t arg_size)
135b2e171f5STomasz Duszynski {
136b2e171f5STomasz Duszynski 	unsigned char chksum;
137b2e171f5STomasz Duszynski 	int num = 0;
138b2e171f5STomasz Duszynski 	size_t i;
139b2e171f5STomasz Duszynski 
140b2e171f5STomasz Duszynski 	buf[num++] = SPS30_SERIAL_SOF_EOF;
141b2e171f5STomasz Duszynski 	buf[num++] = 0;
142b2e171f5STomasz Duszynski 	num += sps30_serial_put_byte(buf + num, cmd);
143b2e171f5STomasz Duszynski 	num += sps30_serial_put_byte(buf + num, arg_size);
144b2e171f5STomasz Duszynski 
145b2e171f5STomasz Duszynski 	for (i = 0; i < arg_size; i++)
146b2e171f5STomasz Duszynski 		num += sps30_serial_put_byte(buf + num, arg[i]);
147b2e171f5STomasz Duszynski 
148b2e171f5STomasz Duszynski 	/* SOF isn't checksummed */
149b2e171f5STomasz Duszynski 	chksum = sps30_serial_calc_chksum(buf + 1, num - 1);
150b2e171f5STomasz Duszynski 	num += sps30_serial_put_byte(buf + num, chksum);
151b2e171f5STomasz Duszynski 	buf[num++] = SPS30_SERIAL_SOF_EOF;
152b2e171f5STomasz Duszynski 
153b2e171f5STomasz Duszynski 	return num;
154b2e171f5STomasz Duszynski }
155b2e171f5STomasz Duszynski 
sps30_serial_frame_valid(struct sps30_state * state,const unsigned char * buf)156b2e171f5STomasz Duszynski static bool sps30_serial_frame_valid(struct sps30_state *state, const unsigned char *buf)
157b2e171f5STomasz Duszynski {
158b2e171f5STomasz Duszynski 	struct sps30_serial_priv *priv = state->priv;
159b2e171f5STomasz Duszynski 	unsigned char chksum;
160b2e171f5STomasz Duszynski 
161b2e171f5STomasz Duszynski 	if ((priv->num < SPS30_SERIAL_FRAME_MIN_SIZE) ||
162b2e171f5STomasz Duszynski 	    (priv->num != SPS30_SERIAL_FRAME_MIN_SIZE +
163b2e171f5STomasz Duszynski 	     priv->buf[SPS30_SERIAL_FRAME_MISO_LEN_OFFSET])) {
164b2e171f5STomasz Duszynski 		dev_err(state->dev, "frame has invalid number of bytes\n");
165b2e171f5STomasz Duszynski 		return false;
166b2e171f5STomasz Duszynski 	}
167b2e171f5STomasz Duszynski 
168b2e171f5STomasz Duszynski 	if ((priv->buf[SPS30_SERIAL_FRAME_ADR_OFFSET] != buf[SPS30_SERIAL_FRAME_ADR_OFFSET]) ||
169b2e171f5STomasz Duszynski 	    (priv->buf[SPS30_SERIAL_FRAME_CMD_OFFSET] != buf[SPS30_SERIAL_FRAME_CMD_OFFSET])) {
170b2e171f5STomasz Duszynski 		dev_err(state->dev, "frame has wrong ADR and CMD bytes\n");
171b2e171f5STomasz Duszynski 		return false;
172b2e171f5STomasz Duszynski 	}
173b2e171f5STomasz Duszynski 
174b2e171f5STomasz Duszynski 	if (priv->buf[SPS30_SERIAL_FRAME_MISO_STATE_OFFSET]) {
175b2e171f5STomasz Duszynski 		dev_err(state->dev, "frame with non-zero state received (0x%02x)\n",
176b2e171f5STomasz Duszynski 			priv->buf[SPS30_SERIAL_FRAME_MISO_STATE_OFFSET]);
177b2e171f5STomasz Duszynski 		return false;
178b2e171f5STomasz Duszynski 	}
179b2e171f5STomasz Duszynski 
180b2e171f5STomasz Duszynski 	/* SOF, checksum and EOF are not checksummed */
181b2e171f5STomasz Duszynski 	chksum = sps30_serial_calc_chksum(priv->buf + 1, priv->num - 3);
182b2e171f5STomasz Duszynski 	if (priv->buf[priv->num - 2] != chksum) {
183b2e171f5STomasz Duszynski 		dev_err(state->dev, "frame integrity check failed\n");
184b2e171f5STomasz Duszynski 		return false;
185b2e171f5STomasz Duszynski 	}
186b2e171f5STomasz Duszynski 
187b2e171f5STomasz Duszynski 	return true;
188b2e171f5STomasz Duszynski }
189b2e171f5STomasz Duszynski 
sps30_serial_command(struct sps30_state * state,unsigned char cmd,const void * arg,size_t arg_size,void * rsp,size_t rsp_size)190b2e171f5STomasz Duszynski static int sps30_serial_command(struct sps30_state *state, unsigned char cmd,
191b2e171f5STomasz Duszynski 				const void *arg, size_t arg_size, void *rsp, size_t rsp_size)
192b2e171f5STomasz Duszynski {
193b2e171f5STomasz Duszynski 	struct sps30_serial_priv *priv = state->priv;
194b2e171f5STomasz Duszynski 	unsigned char buf[SPS30_SERIAL_MAX_BUF_SIZE];
195b2e171f5STomasz Duszynski 	int ret, size;
196b2e171f5STomasz Duszynski 
197b2e171f5STomasz Duszynski 	size = sps30_serial_prep_frame(buf, cmd, arg, arg_size);
198b2e171f5STomasz Duszynski 	ret = sps30_serial_xfer(state, buf, size);
199b2e171f5STomasz Duszynski 	if (ret)
200b2e171f5STomasz Duszynski 		return ret;
201b2e171f5STomasz Duszynski 
202b2e171f5STomasz Duszynski 	if (!sps30_serial_frame_valid(state, buf))
203b2e171f5STomasz Duszynski 		return -EIO;
204b2e171f5STomasz Duszynski 
205b2e171f5STomasz Duszynski 	if (rsp) {
206b2e171f5STomasz Duszynski 		rsp_size = min_t(size_t, priv->buf[SPS30_SERIAL_FRAME_MISO_LEN_OFFSET], rsp_size);
207b2e171f5STomasz Duszynski 		memcpy(rsp, &priv->buf[SPS30_SERIAL_FRAME_MISO_DATA_OFFSET], rsp_size);
208b2e171f5STomasz Duszynski 	}
209b2e171f5STomasz Duszynski 
210b2e171f5STomasz Duszynski 	return rsp_size;
211b2e171f5STomasz Duszynski }
212b2e171f5STomasz Duszynski 
sps30_serial_receive_buf(struct serdev_device * serdev,const unsigned char * buf,size_t size)213b2e171f5STomasz Duszynski static int sps30_serial_receive_buf(struct serdev_device *serdev,
214b2e171f5STomasz Duszynski 				    const unsigned char *buf, size_t size)
215b2e171f5STomasz Duszynski {
216b2e171f5STomasz Duszynski 	struct iio_dev *indio_dev = dev_get_drvdata(&serdev->dev);
217b2e171f5STomasz Duszynski 	struct sps30_serial_priv *priv;
218b2e171f5STomasz Duszynski 	struct sps30_state *state;
219b2e171f5STomasz Duszynski 	unsigned char byte;
220b2e171f5STomasz Duszynski 	size_t i;
221b2e171f5STomasz Duszynski 
222b2e171f5STomasz Duszynski 	if (!indio_dev)
223b2e171f5STomasz Duszynski 		return 0;
224b2e171f5STomasz Duszynski 
225b2e171f5STomasz Duszynski 	state = iio_priv(indio_dev);
226b2e171f5STomasz Duszynski 	priv = state->priv;
227b2e171f5STomasz Duszynski 
228b2e171f5STomasz Duszynski 	/* just in case device put some unexpected data on the bus */
229b2e171f5STomasz Duszynski 	if (priv->done)
230b2e171f5STomasz Duszynski 		return size;
231b2e171f5STomasz Duszynski 
232b2e171f5STomasz Duszynski 	/* wait for the start of frame */
233b2e171f5STomasz Duszynski 	if (!priv->num && size && buf[0] != SPS30_SERIAL_SOF_EOF)
234b2e171f5STomasz Duszynski 		return 1;
235b2e171f5STomasz Duszynski 
236b2e171f5STomasz Duszynski 	if (priv->num + size >= ARRAY_SIZE(priv->buf))
237b2e171f5STomasz Duszynski 		size = ARRAY_SIZE(priv->buf) - priv->num;
238b2e171f5STomasz Duszynski 
239b2e171f5STomasz Duszynski 	for (i = 0; i < size; i++) {
240b2e171f5STomasz Duszynski 		byte = buf[i];
241b2e171f5STomasz Duszynski 		/* remove stuffed bytes on-the-fly */
242b2e171f5STomasz Duszynski 		if (byte == SPS30_SERIAL_ESCAPE_CHAR) {
243b2e171f5STomasz Duszynski 			priv->escaped = true;
244b2e171f5STomasz Duszynski 			continue;
245b2e171f5STomasz Duszynski 		}
246b2e171f5STomasz Duszynski 
247b2e171f5STomasz Duszynski 		byte = sps30_serial_get_byte(priv->escaped, byte);
248b2e171f5STomasz Duszynski 		if (priv->escaped && !byte)
249b2e171f5STomasz Duszynski 			dev_warn(state->dev, "unrecognized escaped char (0x%02x)\n", byte);
250b2e171f5STomasz Duszynski 
251b2e171f5STomasz Duszynski 		priv->buf[priv->num++] = byte;
252b2e171f5STomasz Duszynski 
253b2e171f5STomasz Duszynski 		/* EOF received */
254b2e171f5STomasz Duszynski 		if (!priv->escaped && byte == SPS30_SERIAL_SOF_EOF) {
255b2e171f5STomasz Duszynski 			if (priv->num < SPS30_SERIAL_FRAME_MIN_SIZE)
256b2e171f5STomasz Duszynski 				continue;
257b2e171f5STomasz Duszynski 
258b2e171f5STomasz Duszynski 			priv->done = true;
259b2e171f5STomasz Duszynski 			complete(&priv->new_frame);
260b2e171f5STomasz Duszynski 			i++;
261b2e171f5STomasz Duszynski 			break;
262b2e171f5STomasz Duszynski 		}
263b2e171f5STomasz Duszynski 
264b2e171f5STomasz Duszynski 		priv->escaped = false;
265b2e171f5STomasz Duszynski 	}
266b2e171f5STomasz Duszynski 
267b2e171f5STomasz Duszynski 	return i;
268b2e171f5STomasz Duszynski }
269b2e171f5STomasz Duszynski 
270b2e171f5STomasz Duszynski static const struct serdev_device_ops sps30_serial_device_ops = {
271b2e171f5STomasz Duszynski 	.receive_buf = sps30_serial_receive_buf,
272b2e171f5STomasz Duszynski 	.write_wakeup = serdev_device_write_wakeup,
273b2e171f5STomasz Duszynski };
274b2e171f5STomasz Duszynski 
sps30_serial_start_meas(struct sps30_state * state)275b2e171f5STomasz Duszynski static int sps30_serial_start_meas(struct sps30_state *state)
276b2e171f5STomasz Duszynski {
277b2e171f5STomasz Duszynski 	/* request BE IEEE754 formatted data */
278b2e171f5STomasz Duszynski 	unsigned char buf[] = { 0x01, 0x03 };
279b2e171f5STomasz Duszynski 
280b2e171f5STomasz Duszynski 	return sps30_serial_command(state, SPS30_SERIAL_START_MEAS, buf, sizeof(buf), NULL, 0);
281b2e171f5STomasz Duszynski }
282b2e171f5STomasz Duszynski 
sps30_serial_stop_meas(struct sps30_state * state)283b2e171f5STomasz Duszynski static int sps30_serial_stop_meas(struct sps30_state *state)
284b2e171f5STomasz Duszynski {
285b2e171f5STomasz Duszynski 	return sps30_serial_command(state, SPS30_SERIAL_STOP_MEAS, NULL, 0, NULL, 0);
286b2e171f5STomasz Duszynski }
287b2e171f5STomasz Duszynski 
sps30_serial_reset(struct sps30_state * state)288b2e171f5STomasz Duszynski static int sps30_serial_reset(struct sps30_state *state)
289b2e171f5STomasz Duszynski {
290b2e171f5STomasz Duszynski 	int ret;
291b2e171f5STomasz Duszynski 
292b2e171f5STomasz Duszynski 	ret = sps30_serial_command(state, SPS30_SERIAL_RESET, NULL, 0, NULL, 0);
293b2e171f5STomasz Duszynski 	msleep(500);
294b2e171f5STomasz Duszynski 
295b2e171f5STomasz Duszynski 	return ret;
296b2e171f5STomasz Duszynski }
297b2e171f5STomasz Duszynski 
sps30_serial_read_meas(struct sps30_state * state,__be32 * meas,size_t num)298b2e171f5STomasz Duszynski static int sps30_serial_read_meas(struct sps30_state *state, __be32 *meas, size_t num)
299b2e171f5STomasz Duszynski {
300b2e171f5STomasz Duszynski 	int ret;
301b2e171f5STomasz Duszynski 
302b2e171f5STomasz Duszynski 	/* measurements are ready within a second */
303b2e171f5STomasz Duszynski 	if (msleep_interruptible(1000))
304b2e171f5STomasz Duszynski 		return -EINTR;
305b2e171f5STomasz Duszynski 
306b2e171f5STomasz Duszynski 	ret = sps30_serial_command(state, SPS30_SERIAL_READ_MEAS, NULL, 0, meas, num * sizeof(num));
307b2e171f5STomasz Duszynski 	if (ret < 0)
308b2e171f5STomasz Duszynski 		return ret;
309b2e171f5STomasz Duszynski 	/* if measurements aren't ready sensor returns empty frame */
310b2e171f5STomasz Duszynski 	if (ret == SPS30_SERIAL_FRAME_MIN_SIZE)
311b2e171f5STomasz Duszynski 		return -ETIMEDOUT;
312b2e171f5STomasz Duszynski 	if (ret != num * sizeof(*meas))
313b2e171f5STomasz Duszynski 		return -EIO;
314b2e171f5STomasz Duszynski 
315b2e171f5STomasz Duszynski 	return 0;
316b2e171f5STomasz Duszynski }
317b2e171f5STomasz Duszynski 
sps30_serial_clean_fan(struct sps30_state * state)318b2e171f5STomasz Duszynski static int sps30_serial_clean_fan(struct sps30_state *state)
319b2e171f5STomasz Duszynski {
320b2e171f5STomasz Duszynski 	return sps30_serial_command(state, SPS30_SERIAL_CLEAN_FAN, NULL, 0, NULL, 0);
321b2e171f5STomasz Duszynski }
322b2e171f5STomasz Duszynski 
sps30_serial_read_cleaning_period(struct sps30_state * state,__be32 * period)323b2e171f5STomasz Duszynski static int sps30_serial_read_cleaning_period(struct sps30_state *state, __be32 *period)
324b2e171f5STomasz Duszynski {
325b2e171f5STomasz Duszynski 	unsigned char buf[] = { 0x00 };
326b2e171f5STomasz Duszynski 	int ret;
327b2e171f5STomasz Duszynski 
328b2e171f5STomasz Duszynski 	ret = sps30_serial_command(state, SPS30_SERIAL_PERIOD, buf, sizeof(buf),
329b2e171f5STomasz Duszynski 				   period, sizeof(*period));
330b2e171f5STomasz Duszynski 	if (ret < 0)
331b2e171f5STomasz Duszynski 		return ret;
332b2e171f5STomasz Duszynski 	if (ret != sizeof(*period))
333b2e171f5STomasz Duszynski 		return -EIO;
334b2e171f5STomasz Duszynski 
335b2e171f5STomasz Duszynski 	return 0;
336b2e171f5STomasz Duszynski }
337b2e171f5STomasz Duszynski 
sps30_serial_write_cleaning_period(struct sps30_state * state,__be32 period)338b2e171f5STomasz Duszynski static int sps30_serial_write_cleaning_period(struct sps30_state *state, __be32 period)
339b2e171f5STomasz Duszynski {
340b2e171f5STomasz Duszynski 	unsigned char buf[5] = { 0x00 };
341b2e171f5STomasz Duszynski 
342b2e171f5STomasz Duszynski 	memcpy(buf + 1, &period, sizeof(period));
343b2e171f5STomasz Duszynski 
344b2e171f5STomasz Duszynski 	return sps30_serial_command(state, SPS30_SERIAL_PERIOD, buf, sizeof(buf), NULL, 0);
345b2e171f5STomasz Duszynski }
346b2e171f5STomasz Duszynski 
sps30_serial_show_info(struct sps30_state * state)347b2e171f5STomasz Duszynski static int sps30_serial_show_info(struct sps30_state *state)
348b2e171f5STomasz Duszynski {
349b2e171f5STomasz Duszynski 	/*
350b2e171f5STomasz Duszynski 	 * tell device do return serial number and add extra nul byte just in case
351b2e171f5STomasz Duszynski 	 * serial number isn't a valid string
352b2e171f5STomasz Duszynski 	 */
353b2e171f5STomasz Duszynski 	unsigned char buf[32 + 1] = { 0x03 };
354b2e171f5STomasz Duszynski 	struct device *dev = state->dev;
355b2e171f5STomasz Duszynski 	int ret;
356b2e171f5STomasz Duszynski 
357b2e171f5STomasz Duszynski 	ret = sps30_serial_command(state, SPS30_SERIAL_DEV_INFO, buf, 1, buf, sizeof(buf) - 1);
358b2e171f5STomasz Duszynski 	if (ret < 0)
359b2e171f5STomasz Duszynski 		return ret;
360b2e171f5STomasz Duszynski 	if (ret != sizeof(buf) - 1)
361b2e171f5STomasz Duszynski 		return -EIO;
362b2e171f5STomasz Duszynski 
363b2e171f5STomasz Duszynski 	dev_info(dev, "serial number: %s\n", buf);
364b2e171f5STomasz Duszynski 
365b2e171f5STomasz Duszynski 	ret = sps30_serial_command(state, SPS30_SERIAL_READ_VERSION, NULL, 0, buf, sizeof(buf) - 1);
366b2e171f5STomasz Duszynski 	if (ret < 0)
367b2e171f5STomasz Duszynski 		return ret;
368b2e171f5STomasz Duszynski 	if (ret < 2)
369b2e171f5STomasz Duszynski 		return -EIO;
370b2e171f5STomasz Duszynski 
371b2e171f5STomasz Duszynski 	dev_info(dev, "fw version: %u.%u\n", buf[0], buf[1]);
372b2e171f5STomasz Duszynski 
373b2e171f5STomasz Duszynski 	return 0;
374b2e171f5STomasz Duszynski }
375b2e171f5STomasz Duszynski 
376b2e171f5STomasz Duszynski static const struct sps30_ops sps30_serial_ops = {
377b2e171f5STomasz Duszynski 	.start_meas = sps30_serial_start_meas,
378b2e171f5STomasz Duszynski 	.stop_meas = sps30_serial_stop_meas,
379b2e171f5STomasz Duszynski 	.read_meas = sps30_serial_read_meas,
380b2e171f5STomasz Duszynski 	.reset = sps30_serial_reset,
381b2e171f5STomasz Duszynski 	.clean_fan = sps30_serial_clean_fan,
382b2e171f5STomasz Duszynski 	.read_cleaning_period = sps30_serial_read_cleaning_period,
383b2e171f5STomasz Duszynski 	.write_cleaning_period = sps30_serial_write_cleaning_period,
384b2e171f5STomasz Duszynski 	.show_info = sps30_serial_show_info,
385b2e171f5STomasz Duszynski };
386b2e171f5STomasz Duszynski 
sps30_serial_probe(struct serdev_device * serdev)387b2e171f5STomasz Duszynski static int sps30_serial_probe(struct serdev_device *serdev)
388b2e171f5STomasz Duszynski {
389b2e171f5STomasz Duszynski 	struct device *dev = &serdev->dev;
390b2e171f5STomasz Duszynski 	struct sps30_serial_priv *priv;
391b2e171f5STomasz Duszynski 	int ret;
392b2e171f5STomasz Duszynski 
393b2e171f5STomasz Duszynski 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
394b2e171f5STomasz Duszynski 	if (!priv)
395b2e171f5STomasz Duszynski 		return -ENOMEM;
396b2e171f5STomasz Duszynski 
397b2e171f5STomasz Duszynski 	init_completion(&priv->new_frame);
398b2e171f5STomasz Duszynski 	serdev_device_set_client_ops(serdev, &sps30_serial_device_ops);
399b2e171f5STomasz Duszynski 
400b2e171f5STomasz Duszynski 	ret = devm_serdev_device_open(dev, serdev);
401b2e171f5STomasz Duszynski 	if (ret)
402b2e171f5STomasz Duszynski 		return ret;
403b2e171f5STomasz Duszynski 
404b2e171f5STomasz Duszynski 	serdev_device_set_baudrate(serdev, 115200);
405b2e171f5STomasz Duszynski 	serdev_device_set_flow_control(serdev, false);
406b2e171f5STomasz Duszynski 
407b2e171f5STomasz Duszynski 	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
408b2e171f5STomasz Duszynski 	if (ret)
409b2e171f5STomasz Duszynski 		return ret;
410b2e171f5STomasz Duszynski 
411b2e171f5STomasz Duszynski 	return sps30_probe(dev, SPS30_SERIAL_DEV_NAME, priv, &sps30_serial_ops);
412b2e171f5STomasz Duszynski }
413b2e171f5STomasz Duszynski 
414b2e171f5STomasz Duszynski static const struct of_device_id sps30_serial_of_match[] = {
415b2e171f5STomasz Duszynski 	{ .compatible = "sensirion,sps30" },
416b2e171f5STomasz Duszynski 	{ }
417b2e171f5STomasz Duszynski };
418b2e171f5STomasz Duszynski MODULE_DEVICE_TABLE(of, sps30_serial_of_match);
419b2e171f5STomasz Duszynski 
420b2e171f5STomasz Duszynski static struct serdev_device_driver sps30_serial_driver = {
421b2e171f5STomasz Duszynski 	.driver = {
422b2e171f5STomasz Duszynski 		.name = KBUILD_MODNAME,
423b2e171f5STomasz Duszynski 		.of_match_table = sps30_serial_of_match,
424b2e171f5STomasz Duszynski 	},
425b2e171f5STomasz Duszynski 	.probe = sps30_serial_probe,
426b2e171f5STomasz Duszynski };
427b2e171f5STomasz Duszynski module_serdev_device_driver(sps30_serial_driver);
428b2e171f5STomasz Duszynski 
429b2e171f5STomasz Duszynski MODULE_AUTHOR("Tomasz Duszynski <tomasz.duszynski@octakon.com>");
430b2e171f5STomasz Duszynski MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor serial driver");
431b2e171f5STomasz Duszynski MODULE_LICENSE("GPL v2");
432*53bee458SJonathan Cameron MODULE_IMPORT_NS(IIO_SPS30);
433