Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp387296imu; Wed, 16 Jan 2019 00:19:51 -0800 (PST) X-Google-Smtp-Source: ALg8bN56/QJIaXFOUDHESrKWjmKD9at5vQy+f9yil72CePybrbg4a3O0fRLVz7ckmRcQB1uzMe3c X-Received: by 2002:a17:902:ac1:: with SMTP id 59mr8580538plp.36.1547626790983; Wed, 16 Jan 2019 00:19:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547626790; cv=none; d=google.com; s=arc-20160816; b=AJPskDFQoxusp/Iorv6QTSG1bhRYwMYlX6Ee/vLA4cU8FqcYZVTp5wBlYJb7eyHUP4 8XYRZemWQPhAiclH9lpAaV9SXrF6MEe0Hd7TRz74hk+iQ1qIeRWTAXaZjgv2+7AmhDN4 HEu3/sAhpdbWqMLw/1BVISUcz20AyJ1eIKut+Uvf4ZVIpbD6OnP/xOZVt1nwxJEFkEN7 /xTpMsaZdPQ3bOu70vvjqclIgs1yuk/VHTVP1yVwx6VGNNbVxIMFrfx/OsK1lA9O4Y9m O5CsArT0SD+LWcdXUcb1ecZH6yKV8tJCrA9I2o0K7bRn0ivNyVtQpDyKxrKdhGKfZMAR Caew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=25yZ+tmli1MPXVX4ha840NozHxbCjWITEmRfreBlfCE=; b=TXTE86O4WpoLfwRfeOPj3X5H6zNZ2BWScGXNA08leLCB+2xYHzM1KmeJ/B2Atn0Pa9 rzYxzDVqtg++LnUZou2TjiFRNI1GfJFMrT1QlxnpILDDkZXtEEHYgPH/8jboYX2ZjM8G kMuI+5XbNS+U+JjPYtV7Lxl0RygkCj08ViAv72YoIrf9TLD7eG3uQly6850MQb+6jX7q WXE/aYCEmOqTx4h5lzi59Y1MC0b4gwHT6a7yYpuzMOgDh7kw++KFFwQPxPuCAYQqLJym pFR1A5q27jH4S1PoQD6WVjIxqTD6rpa3/swucOoKnoi88QNrLNTWEzyEetRLh29PtDft dPvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=O57pWJE7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ce19si5994418plb.13.2019.01.16.00.19.32; Wed, 16 Jan 2019 00:19:50 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=O57pWJE7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388747AbfAOScE (ORCPT + 99 others); Tue, 15 Jan 2019 13:32:04 -0500 Received: from mail-lf1-f65.google.com ([209.85.167.65]:35018 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727088AbfAOScD (ORCPT ); Tue, 15 Jan 2019 13:32:03 -0500 Received: by mail-lf1-f65.google.com with SMTP id e26so2838245lfc.2; Tue, 15 Jan 2019 10:32:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=25yZ+tmli1MPXVX4ha840NozHxbCjWITEmRfreBlfCE=; b=O57pWJE72Mpsc6RbiPWF1C5In/wbipu3ODXqdvyWkxNkq5CplEh1/jYjPp5P4xe/gJ 5CSdE7JXvsKpDimur+lvSCZT/UYL9mjQzMy2fnM6G87gfFELvy9ThD2WBzlfYuCqebWN KzDxaKM6tiK/RqjQR0AEpkku1oCFBPgkzrlxaly/M6nilLG3x/pVTnKQUk97Xosn0JPI Ckfq5JRS0j7Y6xFFLJo6dxo8Pt68pow3utCIEraZxoWkMwhbZxCW1dbhHnyGv63b91lm CMCl2/uX/xMIPcEghxVWESnC8COP4b4alodJCyOlhVwAdA8xM7AI8OX2CAX10tx1ptlg n5Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=25yZ+tmli1MPXVX4ha840NozHxbCjWITEmRfreBlfCE=; b=mS4RfvBF3P9haSSB3uJOd5Ea/695iE/Qm3iiqWiuHgbywIOF8Yyfhu54ecz2qVMl9z vuYT2ZYM2QSfNQIBP5B9skEFv5VKdRu3ikUAZs4B/8t4Vv6dm5RxHTf6v3resiaDRPoU JAlzJvTSaSkLSKqGK4tRuu7qXB8K2wk6UUdRaagiH2c2rhpZmHqXBIVNm22/V9eqMntK c4V9NuYBMBykcl5fJu87MSEmKbxKnguq2RfdD9mRhn4CiCw+UczJJMUknIBBKJGqkPGP atUTF9Xcw2qalTnVqTxiv3mJ/cFE80v5S/5MwQVbYw3LJP38C0lDjgp+mvMv8naZ7NYn hxew== X-Gm-Message-State: AJcUukfFFRIQOwpLviDmee30FscG+zAtUMykhJO2zo+SkSTaGlTJjCiM 2I/VIgWvo6KhqwU1KfhyofE= X-Received: by 2002:a19:ef15:: with SMTP id n21mr3985037lfh.21.1547577119998; Tue, 15 Jan 2019 10:31:59 -0800 (PST) Received: from localhost (89-70-37-207.dynamic.chello.pl. [89.70.37.207]) by smtp.gmail.com with ESMTPSA id x11sm842039lfd.81.2019.01.15.10.31.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 15 Jan 2019 10:31:59 -0800 (PST) Date: Tue, 15 Jan 2019 19:31:34 +0100 From: Tomasz Duszynski To: Jeremy Fertic Cc: Tomasz Duszynski , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] iio: chemical: sps30: allow changing self cleaning period Message-ID: <20190115183133.GA9202@arch> References: <20190113100912.29665-1-tduszyns@gmail.com> <20190115001539.GA20418@r2700x.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190115001539.GA20418@r2700x.localdomain> User-Agent: Mutt/1.11.2 (2019-01-07) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 14, 2019 at 05:15:39PM -0700, Jeremy Fertic wrote: > On Sun, Jan 13, 2019 at 11:09:12AM +0100, Tomasz Duszynski wrote: > > Sensor can periodically trigger self cleaning. Period can be changed by > > writing a new value to a dedicated attribute. Upon attribute read > > current period gets returned. > > > > Signed-off-by: Tomasz Duszynski > > --- > > v3: > > * add info about used units to attribute docs > > > > v2: > > * return available values formated as a range > > * tweak naming a little (interval -> period) > > > > Documentation/ABI/testing/sysfs-bus-iio-sps30 | 20 +++ > > drivers/iio/chemical/sps30.c | 143 +++++++++++++++--- > > 2 files changed, 145 insertions(+), 18 deletions(-) > > > > diff --git a/Documentation/ABI/testing/sysfs-bus-iio-sps30 b/Documentation/ABI/testing/sysfs-bus-iio-sps30 > > index e7ce2c57635e..143df8e89d08 100644 > > --- a/Documentation/ABI/testing/sysfs-bus-iio-sps30 > > +++ b/Documentation/ABI/testing/sysfs-bus-iio-sps30 > > @@ -6,3 +6,23 @@ Description: > > Writing 1 starts sensor self cleaning. Internal fan accelerates > > to its maximum speed and keeps spinning for about 10 seconds in > > order to blow out accumulated dust. > > + > > +What: /sys/bus/iio/devices/iio:deviceX/cleaning_period > > +Date: January 2019 > > +KernelVersion: 5.1 > > +Contact: linux-iio@vger.kernel.org > > +Description: > > + Sensor is capable of triggering self cleaning periodically. > > + Period can be changed by writing a new value here. Upon reading > > + the current one is returned. Units are seconds. > > + > > + Writing 0 disables periodical self cleaning entirely. > > + > > +What: /sys/bus/iio/devices/iio:deviceX/cleaning_period_available > > +Date: January 2019 > > +KernelVersion: 5.1 > > +Contact: linux-iio@vger.kernel.org > > +Description: > > + The range of available values in seconds represented as the > > + minimum value, the step and the maximum value, all enclosed in > > + square brackets. > > diff --git a/drivers/iio/chemical/sps30.c b/drivers/iio/chemical/sps30.c > > index f3b4390c8f5c..fe5f6309191b 100644 > > --- a/drivers/iio/chemical/sps30.c > > +++ b/drivers/iio/chemical/sps30.c > > @@ -5,9 +5,6 @@ > > * Copyright (c) Tomasz Duszynski > > * > > * I2C slave address: 0x69 > > - * > > - * TODO: > > - * - support for reading/setting auto cleaning interval > > */ > > > > #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt > > @@ -21,6 +18,7 @@ > > #include > > #include > > #include > > +#include > > #include > > > > #define SPS30_CRC8_POLYNOMIAL 0x31 > > @@ -28,6 +26,9 @@ > > #define SPS30_MAX_READ_SIZE 48 > > /* sensor measures reliably up to 3000 ug / m3 */ > > #define SPS30_MAX_PM 3000 > > +/* minimum and maximum self cleaning periods in seconds */ > > +#define SPS30_AUTO_CLEANING_PERIOD_MIN 0 > > +#define SPS30_AUTO_CLEANING_PERIOD_MAX 604800 > > > > /* SPS30 commands */ > > #define SPS30_START_MEAS 0x0010 > > @@ -37,6 +38,9 @@ > > #define SPS30_READ_DATA 0x0300 > > #define SPS30_READ_SERIAL 0xd033 > > #define SPS30_START_FAN_CLEANING 0x5607 > > +#define SPS30_AUTO_CLEANING_PERIOD 0x8004 > > +/* not a sensor command per se, used only to distinguish write from read */ > > +#define SPS30_READ_AUTO_CLEANING_PERIOD 0x8005 > > > > enum { > > PM1, > > @@ -45,6 +49,11 @@ enum { > > PM10, > > }; > > > > +enum { > > + RESET, > > + MEASURING, > > +}; > > + > > struct sps30_state { > > struct i2c_client *client; > > /* > > @@ -52,6 +61,7 @@ struct sps30_state { > > * Must be held whenever sequence of commands is to be executed. > > */ > > struct mutex lock; > > + int state; > > }; > > > > DECLARE_CRC8_TABLE(sps30_crc8_table); > > @@ -107,6 +117,9 @@ static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size) > > case SPS30_START_FAN_CLEANING: > > ret = sps30_write_then_read(state, buf, 2, NULL, 0); > > break; > > + case SPS30_READ_AUTO_CLEANING_PERIOD: > > + buf[0] = SPS30_AUTO_CLEANING_PERIOD >> 8; > > + buf[1] = (u8)SPS30_AUTO_CLEANING_PERIOD; > > case SPS30_READ_DATA_READY_FLAG: > > case SPS30_READ_DATA: > > case SPS30_READ_SERIAL: > > @@ -114,6 +127,15 @@ static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size) > > size += size / 2; > > ret = sps30_write_then_read(state, buf, 2, buf, size); > > break; > > + case SPS30_AUTO_CLEANING_PERIOD: > > + buf[2] = data[0]; > > + buf[3] = data[1]; > > + buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE); > > + buf[5] = data[2]; > > + buf[6] = data[3]; > > + buf[7] = crc8(sps30_crc8_table, &buf[5], 2, CRC8_INIT_VALUE); > > + ret = sps30_write_then_read(state, buf, 8, NULL, 0); > > + break; > > } > > > > if (ret) > > @@ -170,6 +192,14 @@ static int sps30_do_meas(struct sps30_state *state, s32 *data, int size) > > int i, ret, tries = 5; > > u8 tmp[16]; > > > > + if (state->state == RESET) { > > + ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0); > > + if (ret) > > + return ret; > > + > > + state->state = MEASURING; > > + } > > + > > while (tries--) { > > ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2); > > if (ret) > > @@ -276,6 +306,24 @@ static int sps30_read_raw(struct iio_dev *indio_dev, > > return -EINVAL; > > } > > > > +static int sps30_do_cmd_reset(struct sps30_state *state) > > +{ > > + int ret; > > + > > + ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0); > > + msleep(300); > > + /* > > + * Power-on-reset causes sensor to produce some glitch on i2c bus and > > + * some controllers end up in error state. Recover simply by placing > > + * some data on the bus, for example STOP_MEAS command, which > > + * is NOP in this case. > > + */ > > + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0); > > + state->state = RESET; > > + > > + return ret; > > +} > > + > > static ssize_t start_cleaning_store(struct device *dev, > > struct device_attribute *attr, > > const char *buf, size_t len) > > @@ -296,10 +344,82 @@ static ssize_t start_cleaning_store(struct device *dev, > > return len; > > } > > > > +static ssize_t cleaning_period_show(struct device *dev, > > + struct device_attribute *attr, > > + char *buf) > > +{ > > + struct iio_dev *indio_dev = dev_to_iio_dev(dev); > > + struct sps30_state *state = iio_priv(indio_dev); > > + u8 tmp[4]; > > + int ret; > > + > > + mutex_lock(&state->lock); > > + ret = sps30_do_cmd(state, SPS30_READ_AUTO_CLEANING_PERIOD, tmp, 4); > > + mutex_unlock(&state->lock); > > + if (ret) > > + return ret; > > + > > + return sprintf(buf, "%d\n", get_unaligned_be32(tmp)); > > +} > > + > > +static ssize_t cleaning_period_store(struct device *dev, > > + struct device_attribute *attr, > > + const char *buf, size_t len) > > +{ > > + struct iio_dev *indio_dev = dev_to_iio_dev(dev); > > + struct sps30_state *state = iio_priv(indio_dev); > > + int val, ret; > > + u8 tmp[4]; > > + > > + if (kstrtoint(buf, 0, &val)) > > + return -EINVAL; > > + > > + if ((val < SPS30_AUTO_CLEANING_PERIOD_MIN) && > > + (val > SPS30_AUTO_CLEANING_PERIOD_MAX)) > > + return -EINVAL; > > Should this be || instead of &&? > Definitely. Thanks for catching this. > Jeremy > > > + > > + put_unaligned_be32(val, tmp); > > + > > + mutex_lock(&state->lock); > > + ret = sps30_do_cmd(state, SPS30_AUTO_CLEANING_PERIOD, tmp, 0); > > + if (ret) { > > + mutex_unlock(&state->lock); > > + return ret; > > + } > > + > > + msleep(20); > > + > > + /* > > + * sensor requires reset in order to return up to date self cleaning > > + * period > > + */ > > + ret = sps30_do_cmd_reset(state); > > + if (ret) > > + dev_warn(dev, > > + "period changed but reads will return the old value\n"); > > + > > + mutex_unlock(&state->lock); > > + > > + return len; > > +} > > + > > +static ssize_t cleaning_period_available_show(struct device *dev, > > + struct device_attribute *attr, > > + char *buf) > > +{ > > + return snprintf(buf, PAGE_SIZE, "[%d %d %d]\n", > > + SPS30_AUTO_CLEANING_PERIOD_MIN, 1, > > + SPS30_AUTO_CLEANING_PERIOD_MAX); > > +} > > + > > static IIO_DEVICE_ATTR_WO(start_cleaning, 0); > > +static IIO_DEVICE_ATTR_RW(cleaning_period, 0); > > +static IIO_DEVICE_ATTR_RO(cleaning_period_available, 0); > > > > static struct attribute *sps30_attrs[] = { > > &iio_dev_attr_start_cleaning.dev_attr.attr, > > + &iio_dev_attr_cleaning_period.dev_attr.attr, > > + &iio_dev_attr_cleaning_period_available.dev_attr.attr, > > NULL > > }; > > > > @@ -362,6 +482,7 @@ static int sps30_probe(struct i2c_client *client) > > state = iio_priv(indio_dev); > > i2c_set_clientdata(client, indio_dev); > > state->client = client; > > + state->state = RESET; > > indio_dev->dev.parent = &client->dev; > > indio_dev->info = &sps30_info; > > indio_dev->name = client->name; > > @@ -373,19 +494,11 @@ static int sps30_probe(struct i2c_client *client) > > mutex_init(&state->lock); > > crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL); > > > > - ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0); > > + ret = sps30_do_cmd_reset(state); > > if (ret) { > > dev_err(&client->dev, "failed to reset device\n"); > > return ret; > > } > > - msleep(300); > > - /* > > - * Power-on-reset causes sensor to produce some glitch on i2c bus and > > - * some controllers end up in error state. Recover simply by placing > > - * some data on the bus, for example STOP_MEAS command, which > > - * is NOP in this case. > > - */ > > - sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0); > > > > ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf)); > > if (ret) { > > @@ -395,12 +508,6 @@ static int sps30_probe(struct i2c_client *client) > > /* returned serial number is already NUL terminated */ > > dev_info(&client->dev, "serial number: %s\n", buf); > > > > - ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0); > > - if (ret) { > > - dev_err(&client->dev, "failed to start measurement\n"); > > - return ret; > > - } > > - > > ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state); > > if (ret) > > return ret; > > -- > > 2.20.1 > >