3a1335ba44c63bb2c7c42987f8d56b035cefa0c0
[openwrt/staging/adrian.git] /
1 From 62129a0849d27cc94ced832bcf9dcde283dcbe08 Mon Sep 17 00:00:00 2001
2 From: Tomasz Duszynski <tduszyns@gmail.com>
3 Date: Tue, 15 Jan 2019 20:00:06 +0100
4 Subject: [PATCH] iio: chemical: sps30: allow changing self cleaning period
5
6 Sensor can periodically trigger self cleaning. Period can be changed by
7 writing a new value to a dedicated attribute. Upon attribute read
8 current period gets returned.
9
10 Signed-off-by: Tomasz Duszynski <tduszyns@gmail.com>
11 Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
12 ---
13 Documentation/ABI/testing/sysfs-bus-iio-sps30 | 20 +++
14 drivers/iio/chemical/sps30.c | 143 +++++++++++++++---
15 2 files changed, 145 insertions(+), 18 deletions(-)
16
17 --- a/Documentation/ABI/testing/sysfs-bus-iio-sps30
18 +++ b/Documentation/ABI/testing/sysfs-bus-iio-sps30
19 @@ -6,3 +6,23 @@ Description:
20 Writing 1 starts sensor self cleaning. Internal fan accelerates
21 to its maximum speed and keeps spinning for about 10 seconds in
22 order to blow out accumulated dust.
23 +
24 +What: /sys/bus/iio/devices/iio:deviceX/cleaning_period
25 +Date: January 2019
26 +KernelVersion: 5.1
27 +Contact: linux-iio@vger.kernel.org
28 +Description:
29 + Sensor is capable of triggering self cleaning periodically.
30 + Period can be changed by writing a new value here. Upon reading
31 + the current one is returned. Units are seconds.
32 +
33 + Writing 0 disables periodical self cleaning entirely.
34 +
35 +What: /sys/bus/iio/devices/iio:deviceX/cleaning_period_available
36 +Date: January 2019
37 +KernelVersion: 5.1
38 +Contact: linux-iio@vger.kernel.org
39 +Description:
40 + The range of available values in seconds represented as the
41 + minimum value, the step and the maximum value, all enclosed in
42 + square brackets.
43 --- a/drivers/iio/chemical/sps30.c
44 +++ b/drivers/iio/chemical/sps30.c
45 @@ -5,9 +5,6 @@
46 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
47 *
48 * I2C slave address: 0x69
49 - *
50 - * TODO:
51 - * - support for reading/setting auto cleaning interval
52 */
53
54 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55 @@ -21,6 +18,7 @@
56 #include <linux/iio/sysfs.h>
57 #include <linux/iio/trigger_consumer.h>
58 #include <linux/iio/triggered_buffer.h>
59 +#include <linux/kernel.h>
60 #include <linux/module.h>
61
62 #define SPS30_CRC8_POLYNOMIAL 0x31
63 @@ -28,6 +26,9 @@
64 #define SPS30_MAX_READ_SIZE 48
65 /* sensor measures reliably up to 3000 ug / m3 */
66 #define SPS30_MAX_PM 3000
67 +/* minimum and maximum self cleaning periods in seconds */
68 +#define SPS30_AUTO_CLEANING_PERIOD_MIN 0
69 +#define SPS30_AUTO_CLEANING_PERIOD_MAX 604800
70
71 /* SPS30 commands */
72 #define SPS30_START_MEAS 0x0010
73 @@ -37,6 +38,9 @@
74 #define SPS30_READ_DATA 0x0300
75 #define SPS30_READ_SERIAL 0xd033
76 #define SPS30_START_FAN_CLEANING 0x5607
77 +#define SPS30_AUTO_CLEANING_PERIOD 0x8004
78 +/* not a sensor command per se, used only to distinguish write from read */
79 +#define SPS30_READ_AUTO_CLEANING_PERIOD 0x8005
80
81 enum {
82 PM1,
83 @@ -45,6 +49,11 @@ enum {
84 PM10,
85 };
86
87 +enum {
88 + RESET,
89 + MEASURING,
90 +};
91 +
92 struct sps30_state {
93 struct i2c_client *client;
94 /*
95 @@ -52,6 +61,7 @@ struct sps30_state {
96 * Must be held whenever sequence of commands is to be executed.
97 */
98 struct mutex lock;
99 + int state;
100 };
101
102 DECLARE_CRC8_TABLE(sps30_crc8_table);
103 @@ -107,6 +117,9 @@ static int sps30_do_cmd(struct sps30_sta
104 case SPS30_START_FAN_CLEANING:
105 ret = sps30_write_then_read(state, buf, 2, NULL, 0);
106 break;
107 + case SPS30_READ_AUTO_CLEANING_PERIOD:
108 + buf[0] = SPS30_AUTO_CLEANING_PERIOD >> 8;
109 + buf[1] = (u8)SPS30_AUTO_CLEANING_PERIOD;
110 case SPS30_READ_DATA_READY_FLAG:
111 case SPS30_READ_DATA:
112 case SPS30_READ_SERIAL:
113 @@ -114,6 +127,15 @@ static int sps30_do_cmd(struct sps30_sta
114 size += size / 2;
115 ret = sps30_write_then_read(state, buf, 2, buf, size);
116 break;
117 + case SPS30_AUTO_CLEANING_PERIOD:
118 + buf[2] = data[0];
119 + buf[3] = data[1];
120 + buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE);
121 + buf[5] = data[2];
122 + buf[6] = data[3];
123 + buf[7] = crc8(sps30_crc8_table, &buf[5], 2, CRC8_INIT_VALUE);
124 + ret = sps30_write_then_read(state, buf, 8, NULL, 0);
125 + break;
126 }
127
128 if (ret)
129 @@ -170,6 +192,14 @@ static int sps30_do_meas(struct sps30_st
130 int i, ret, tries = 5;
131 u8 tmp[16];
132
133 + if (state->state == RESET) {
134 + ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
135 + if (ret)
136 + return ret;
137 +
138 + state->state = MEASURING;
139 + }
140 +
141 while (tries--) {
142 ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2);
143 if (ret)
144 @@ -276,6 +306,24 @@ static int sps30_read_raw(struct iio_dev
145 return -EINVAL;
146 }
147
148 +static int sps30_do_cmd_reset(struct sps30_state *state)
149 +{
150 + int ret;
151 +
152 + ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
153 + msleep(300);
154 + /*
155 + * Power-on-reset causes sensor to produce some glitch on i2c bus and
156 + * some controllers end up in error state. Recover simply by placing
157 + * some data on the bus, for example STOP_MEAS command, which
158 + * is NOP in this case.
159 + */
160 + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
161 + state->state = RESET;
162 +
163 + return ret;
164 +}
165 +
166 static ssize_t start_cleaning_store(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t len)
169 @@ -296,10 +344,82 @@ static ssize_t start_cleaning_store(stru
170 return len;
171 }
172
173 +static ssize_t cleaning_period_show(struct device *dev,
174 + struct device_attribute *attr,
175 + char *buf)
176 +{
177 + struct iio_dev *indio_dev = dev_to_iio_dev(dev);
178 + struct sps30_state *state = iio_priv(indio_dev);
179 + u8 tmp[4];
180 + int ret;
181 +
182 + mutex_lock(&state->lock);
183 + ret = sps30_do_cmd(state, SPS30_READ_AUTO_CLEANING_PERIOD, tmp, 4);
184 + mutex_unlock(&state->lock);
185 + if (ret)
186 + return ret;
187 +
188 + return sprintf(buf, "%d\n", get_unaligned_be32(tmp));
189 +}
190 +
191 +static ssize_t cleaning_period_store(struct device *dev,
192 + struct device_attribute *attr,
193 + const char *buf, size_t len)
194 +{
195 + struct iio_dev *indio_dev = dev_to_iio_dev(dev);
196 + struct sps30_state *state = iio_priv(indio_dev);
197 + int val, ret;
198 + u8 tmp[4];
199 +
200 + if (kstrtoint(buf, 0, &val))
201 + return -EINVAL;
202 +
203 + if ((val < SPS30_AUTO_CLEANING_PERIOD_MIN) ||
204 + (val > SPS30_AUTO_CLEANING_PERIOD_MAX))
205 + return -EINVAL;
206 +
207 + put_unaligned_be32(val, tmp);
208 +
209 + mutex_lock(&state->lock);
210 + ret = sps30_do_cmd(state, SPS30_AUTO_CLEANING_PERIOD, tmp, 0);
211 + if (ret) {
212 + mutex_unlock(&state->lock);
213 + return ret;
214 + }
215 +
216 + msleep(20);
217 +
218 + /*
219 + * sensor requires reset in order to return up to date self cleaning
220 + * period
221 + */
222 + ret = sps30_do_cmd_reset(state);
223 + if (ret)
224 + dev_warn(dev,
225 + "period changed but reads will return the old value\n");
226 +
227 + mutex_unlock(&state->lock);
228 +
229 + return len;
230 +}
231 +
232 +static ssize_t cleaning_period_available_show(struct device *dev,
233 + struct device_attribute *attr,
234 + char *buf)
235 +{
236 + return snprintf(buf, PAGE_SIZE, "[%d %d %d]\n",
237 + SPS30_AUTO_CLEANING_PERIOD_MIN, 1,
238 + SPS30_AUTO_CLEANING_PERIOD_MAX);
239 +}
240 +
241 static IIO_DEVICE_ATTR_WO(start_cleaning, 0);
242 +static IIO_DEVICE_ATTR_RW(cleaning_period, 0);
243 +static IIO_DEVICE_ATTR_RO(cleaning_period_available, 0);
244
245 static struct attribute *sps30_attrs[] = {
246 &iio_dev_attr_start_cleaning.dev_attr.attr,
247 + &iio_dev_attr_cleaning_period.dev_attr.attr,
248 + &iio_dev_attr_cleaning_period_available.dev_attr.attr,
249 NULL
250 };
251
252 @@ -362,6 +482,7 @@ static int sps30_probe(struct i2c_client
253 state = iio_priv(indio_dev);
254 i2c_set_clientdata(client, indio_dev);
255 state->client = client;
256 + state->state = RESET;
257 indio_dev->dev.parent = &client->dev;
258 indio_dev->info = &sps30_info;
259 indio_dev->name = client->name;
260 @@ -373,19 +494,11 @@ static int sps30_probe(struct i2c_client
261 mutex_init(&state->lock);
262 crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
263
264 - ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
265 + ret = sps30_do_cmd_reset(state);
266 if (ret) {
267 dev_err(&client->dev, "failed to reset device\n");
268 return ret;
269 }
270 - msleep(300);
271 - /*
272 - * Power-on-reset causes sensor to produce some glitch on i2c bus and
273 - * some controllers end up in error state. Recover simply by placing
274 - * some data on the bus, for example STOP_MEAS command, which
275 - * is NOP in this case.
276 - */
277 - sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
278
279 ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
280 if (ret) {
281 @@ -395,12 +508,6 @@ static int sps30_probe(struct i2c_client
282 /* returned serial number is already NUL terminated */
283 dev_info(&client->dev, "serial number: %s\n", buf);
284
285 - ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
286 - if (ret) {
287 - dev_err(&client->dev, "failed to start measurement\n");
288 - return ret;
289 - }
290 -
291 ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state);
292 if (ret)
293 return ret;