xref: /linux/drivers/iio/chemical/sps30_i2c.c (revision 889600e21e3be388a6817c2a0dac0411df860751)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Sensirion SPS30 particulate matter sensor i2c driver
4  *
5  * Copyright (c) 2020 Tomasz Duszynski <tduszyns@gmail.com>
6  *
7  * I2C slave address: 0x69
8  */
9 #include <linux/unaligned.h>
10 #include <linux/crc8.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 
18 #include "sps30.h"
19 
20 #define SPS30_I2C_CRC8_POLYNOMIAL 0x31
21 /* max number of bytes needed to store PM measurements or serial string */
22 #define SPS30_I2C_MAX_BUF_SIZE 48
23 
24 DECLARE_CRC8_TABLE(sps30_i2c_crc8_table);
25 
26 #define SPS30_I2C_START_MEAS 0x0010
27 #define SPS30_I2C_STOP_MEAS 0x0104
28 #define SPS30_I2C_READ_MEAS 0x0300
29 #define SPS30_I2C_MEAS_READY 0x0202
30 #define SPS30_I2C_RESET 0xd304
31 #define SPS30_I2C_CLEAN_FAN 0x5607
32 #define SPS30_I2C_PERIOD 0x8004
33 #define SPS30_I2C_READ_SERIAL 0xd033
34 #define SPS30_I2C_READ_VERSION 0xd100
35 
sps30_i2c_xfer(struct sps30_state * state,unsigned char * txbuf,size_t txsize,unsigned char * rxbuf,size_t rxsize)36 static int sps30_i2c_xfer(struct sps30_state *state, unsigned char *txbuf, size_t txsize,
37 			  unsigned char *rxbuf, size_t rxsize)
38 {
39 	struct i2c_client *client = to_i2c_client(state->dev);
40 	int ret;
41 
42 	/*
43 	 * Sensor does not support repeated start so instead of
44 	 * sending two i2c messages in a row we just send one by one.
45 	 */
46 	ret = i2c_master_send(client, txbuf, txsize);
47 	if (ret < 0)
48 		return ret;
49 	if (ret != txsize)
50 		return -EIO;
51 
52 	if (!rxsize)
53 		return 0;
54 
55 	ret = i2c_master_recv(client, rxbuf, rxsize);
56 	if (ret < 0)
57 		return ret;
58 	if (ret != rxsize)
59 		return -EIO;
60 
61 	return 0;
62 }
63 
sps30_i2c_command(struct sps30_state * state,u16 cmd,void * arg,size_t arg_size,void * rsp,size_t rsp_size)64 static int sps30_i2c_command(struct sps30_state *state, u16 cmd, void *arg, size_t arg_size,
65 			     void *rsp, size_t rsp_size)
66 {
67 	/*
68 	 * Internally sensor stores measurements in a following manner:
69 	 *
70 	 * PM1:   upper two bytes, crc8, lower two bytes, crc8
71 	 * PM2P5: upper two bytes, crc8, lower two bytes, crc8
72 	 * PM4:   upper two bytes, crc8, lower two bytes, crc8
73 	 * PM10:  upper two bytes, crc8, lower two bytes, crc8
74 	 *
75 	 * What follows next are number concentration measurements and
76 	 * typical particle size measurement which we omit.
77 	 */
78 	unsigned char buf[SPS30_I2C_MAX_BUF_SIZE];
79 	unsigned char *tmp;
80 	unsigned char crc;
81 	size_t i;
82 	int ret;
83 
84 	put_unaligned_be16(cmd, buf);
85 	i = 2;
86 
87 	if (rsp) {
88 		/* each two bytes are followed by a crc8 */
89 		rsp_size += rsp_size / 2;
90 	} else {
91 		tmp = arg;
92 
93 		while (arg_size) {
94 			buf[i] = *tmp++;
95 			buf[i + 1] = *tmp++;
96 			buf[i + 2] = crc8(sps30_i2c_crc8_table, buf + i, 2, CRC8_INIT_VALUE);
97 			arg_size -= 2;
98 			i += 3;
99 		}
100 	}
101 
102 	ret = sps30_i2c_xfer(state, buf, i, buf, rsp_size);
103 	if (ret)
104 		return ret;
105 
106 	/* validate received data and strip off crc bytes */
107 	tmp = rsp;
108 	for (i = 0; i < rsp_size; i += 3) {
109 		crc = crc8(sps30_i2c_crc8_table, buf + i, 2, CRC8_INIT_VALUE);
110 		if (crc != buf[i + 2]) {
111 			dev_err(state->dev, "data integrity check failed\n");
112 			return -EIO;
113 		}
114 
115 		*tmp++ = buf[i];
116 		*tmp++ = buf[i + 1];
117 	}
118 
119 	return 0;
120 }
121 
sps30_i2c_start_meas(struct sps30_state * state)122 static int sps30_i2c_start_meas(struct sps30_state *state)
123 {
124 	/* request BE IEEE754 formatted data */
125 	unsigned char buf[] = { 0x03, 0x00 };
126 
127 	return sps30_i2c_command(state, SPS30_I2C_START_MEAS, buf, sizeof(buf), NULL, 0);
128 }
129 
sps30_i2c_stop_meas(struct sps30_state * state)130 static int sps30_i2c_stop_meas(struct sps30_state *state)
131 {
132 	return sps30_i2c_command(state, SPS30_I2C_STOP_MEAS, NULL, 0, NULL, 0);
133 }
134 
sps30_i2c_reset(struct sps30_state * state)135 static int sps30_i2c_reset(struct sps30_state *state)
136 {
137 	int ret;
138 
139 	ret = sps30_i2c_command(state, SPS30_I2C_RESET, NULL, 0, NULL, 0);
140 	msleep(500);
141 	/*
142 	 * Power-on-reset causes sensor to produce some glitch on i2c bus and
143 	 * some controllers end up in error state. Recover simply by placing
144 	 * some data on the bus, for example STOP_MEAS command, which
145 	 * is NOP in this case.
146 	 */
147 	sps30_i2c_stop_meas(state);
148 
149 	return ret;
150 }
151 
sps30_i2c_meas_ready(struct sps30_state * state)152 static bool sps30_i2c_meas_ready(struct sps30_state *state)
153 {
154 	unsigned char buf[2];
155 	int ret;
156 
157 	ret = sps30_i2c_command(state, SPS30_I2C_MEAS_READY, NULL, 0, buf, sizeof(buf));
158 	if (ret)
159 		return false;
160 
161 	return buf[1];
162 }
163 
sps30_i2c_read_meas(struct sps30_state * state,__be32 * meas,size_t num)164 static int sps30_i2c_read_meas(struct sps30_state *state, __be32 *meas, size_t num)
165 {
166 	/* measurements are ready within a second */
167 	if (msleep_interruptible(1000))
168 		return -EINTR;
169 
170 	if (!sps30_i2c_meas_ready(state))
171 		return -ETIMEDOUT;
172 
173 	return sps30_i2c_command(state, SPS30_I2C_READ_MEAS, NULL, 0, meas, sizeof(*meas) * num);
174 }
175 
sps30_i2c_clean_fan(struct sps30_state * state)176 static int sps30_i2c_clean_fan(struct sps30_state *state)
177 {
178 	return sps30_i2c_command(state, SPS30_I2C_CLEAN_FAN, NULL, 0, NULL, 0);
179 }
180 
sps30_i2c_read_cleaning_period(struct sps30_state * state,__be32 * period)181 static int sps30_i2c_read_cleaning_period(struct sps30_state *state, __be32 *period)
182 {
183 	return sps30_i2c_command(state, SPS30_I2C_PERIOD, NULL, 0, period, sizeof(*period));
184 }
185 
sps30_i2c_write_cleaning_period(struct sps30_state * state,__be32 period)186 static int sps30_i2c_write_cleaning_period(struct sps30_state *state, __be32 period)
187 {
188 	return sps30_i2c_command(state, SPS30_I2C_PERIOD, &period, sizeof(period), NULL, 0);
189 }
190 
sps30_i2c_show_info(struct sps30_state * state)191 static int sps30_i2c_show_info(struct sps30_state *state)
192 {
193 	/* extra nul just in case */
194 	unsigned char buf[32 + 1] = { 0x00 };
195 	int ret;
196 
197 	ret = sps30_i2c_command(state, SPS30_I2C_READ_SERIAL, NULL, 0, buf, sizeof(buf) - 1);
198 	if (ret)
199 		return ret;
200 
201 	dev_info(state->dev, "serial number: %s\n", buf);
202 
203 	ret = sps30_i2c_command(state, SPS30_I2C_READ_VERSION, NULL, 0, buf, 2);
204 	if (ret)
205 		return ret;
206 
207 	dev_info(state->dev, "fw version: %u.%u\n", buf[0], buf[1]);
208 
209 	return 0;
210 }
211 
212 static const struct sps30_ops sps30_i2c_ops = {
213 	.start_meas = sps30_i2c_start_meas,
214 	.stop_meas = sps30_i2c_stop_meas,
215 	.read_meas = sps30_i2c_read_meas,
216 	.reset = sps30_i2c_reset,
217 	.clean_fan = sps30_i2c_clean_fan,
218 	.read_cleaning_period = sps30_i2c_read_cleaning_period,
219 	.write_cleaning_period = sps30_i2c_write_cleaning_period,
220 	.show_info = sps30_i2c_show_info,
221 };
222 
sps30_i2c_probe(struct i2c_client * client)223 static int sps30_i2c_probe(struct i2c_client *client)
224 {
225 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
226 		return -EOPNOTSUPP;
227 
228 	crc8_populate_msb(sps30_i2c_crc8_table, SPS30_I2C_CRC8_POLYNOMIAL);
229 
230 	return sps30_probe(&client->dev, client->name, NULL, &sps30_i2c_ops);
231 }
232 
233 static const struct i2c_device_id sps30_i2c_id[] = {
234 	{ .name = "sps30" },
235 	{ }
236 };
237 MODULE_DEVICE_TABLE(i2c, sps30_i2c_id);
238 
239 static const struct of_device_id sps30_i2c_of_match[] = {
240 	{ .compatible = "sensirion,sps30" },
241 	{ }
242 };
243 MODULE_DEVICE_TABLE(of, sps30_i2c_of_match);
244 
245 static struct i2c_driver sps30_i2c_driver = {
246 	.driver = {
247 		.name = KBUILD_MODNAME,
248 		.of_match_table = sps30_i2c_of_match,
249 	},
250 	.id_table = sps30_i2c_id,
251 	.probe = sps30_i2c_probe,
252 };
253 module_i2c_driver(sps30_i2c_driver);
254 
255 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
256 MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor i2c driver");
257 MODULE_LICENSE("GPL v2");
258 MODULE_IMPORT_NS("IIO_SPS30");
259