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
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.
10 Signed-off-by: Tomasz Duszynski <tduszyns@gmail.com>
11 Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
13 Documentation/ABI/testing/sysfs-bus-iio-sps30 | 20 +++
14 drivers/iio/chemical/sps30.c | 143 +++++++++++++++---
15 2 files changed, 145 insertions(+), 18 deletions(-)
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.
24 +What: /sys/bus/iio/devices/iio:deviceX/cleaning_period
27 +Contact: linux-iio@vger.kernel.org
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.
33 + Writing 0 disables periodical self cleaning entirely.
35 +What: /sys/bus/iio/devices/iio:deviceX/cleaning_period_available
38 +Contact: linux-iio@vger.kernel.org
40 + The range of available values in seconds represented as the
41 + minimum value, the step and the maximum value, all enclosed in
43 --- a/drivers/iio/chemical/sps30.c
44 +++ b/drivers/iio/chemical/sps30.c
46 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
48 * I2C slave address: 0x69
51 - * - support for reading/setting auto cleaning interval
54 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
62 #define SPS30_CRC8_POLYNOMIAL 0x31
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
72 #define SPS30_START_MEAS 0x0010
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
83 @@ -45,6 +49,11 @@ enum {
93 struct i2c_client *client;
95 @@ -52,6 +61,7 @@ struct sps30_state {
96 * Must be held whenever sequence of commands is to be executed.
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);
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
115 ret = sps30_write_then_read(state, buf, 2, buf, size);
117 + case SPS30_AUTO_CLEANING_PERIOD:
120 + buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE);
123 + buf[7] = crc8(sps30_crc8_table, &buf[5], 2, CRC8_INIT_VALUE);
124 + ret = sps30_write_then_read(state, buf, 8, NULL, 0);
129 @@ -170,6 +192,14 @@ static int sps30_do_meas(struct sps30_st
130 int i, ret, tries = 5;
133 + if (state->state == RESET) {
134 + ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
138 + state->state = MEASURING;
142 ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2);
144 @@ -276,6 +306,24 @@ static int sps30_read_raw(struct iio_dev
148 +static int sps30_do_cmd_reset(struct sps30_state *state)
152 + ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
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.
160 + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
161 + state->state = RESET;
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
173 +static ssize_t cleaning_period_show(struct device *dev,
174 + struct device_attribute *attr,
177 + struct iio_dev *indio_dev = dev_to_iio_dev(dev);
178 + struct sps30_state *state = iio_priv(indio_dev);
182 + mutex_lock(&state->lock);
183 + ret = sps30_do_cmd(state, SPS30_READ_AUTO_CLEANING_PERIOD, tmp, 4);
184 + mutex_unlock(&state->lock);
188 + return sprintf(buf, "%d\n", get_unaligned_be32(tmp));
191 +static ssize_t cleaning_period_store(struct device *dev,
192 + struct device_attribute *attr,
193 + const char *buf, size_t len)
195 + struct iio_dev *indio_dev = dev_to_iio_dev(dev);
196 + struct sps30_state *state = iio_priv(indio_dev);
200 + if (kstrtoint(buf, 0, &val))
203 + if ((val < SPS30_AUTO_CLEANING_PERIOD_MIN) ||
204 + (val > SPS30_AUTO_CLEANING_PERIOD_MAX))
207 + put_unaligned_be32(val, tmp);
209 + mutex_lock(&state->lock);
210 + ret = sps30_do_cmd(state, SPS30_AUTO_CLEANING_PERIOD, tmp, 0);
212 + mutex_unlock(&state->lock);
219 + * sensor requires reset in order to return up to date self cleaning
222 + ret = sps30_do_cmd_reset(state);
225 + "period changed but reads will return the old value\n");
227 + mutex_unlock(&state->lock);
232 +static ssize_t cleaning_period_available_show(struct device *dev,
233 + struct device_attribute *attr,
236 + return snprintf(buf, PAGE_SIZE, "[%d %d %d]\n",
237 + SPS30_AUTO_CLEANING_PERIOD_MIN, 1,
238 + SPS30_AUTO_CLEANING_PERIOD_MAX);
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);
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,
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);
264 - ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
265 + ret = sps30_do_cmd_reset(state);
267 dev_err(&client->dev, "failed to reset device\n");
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.
277 - sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
279 ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
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);
285 - ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
287 - dev_err(&client->dev, "failed to start measurement\n");
291 ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state);