Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp1927544ybm; Sun, 31 May 2020 03:04:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzFriL9MwRotsm3jyTJ8VRgVziVojh9oIAHYIwn8OJXR/A0qBEAS1FLvLqF/flpBROiTuG X-Received: by 2002:a05:6402:144e:: with SMTP id d14mr15190632edx.14.1590919493651; Sun, 31 May 2020 03:04:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590919493; cv=none; d=google.com; s=arc-20160816; b=tiI82CbkGW+PHC2CfxVrUFNY7goWPWcC/wnZkf6xdi9zcIPCTcTA8J9K/re2WySfBH GPV6OOk8vRLwRlIWnNusZJsfHq3pZz9sYn6SDVApZ1qbaszdyG2dNOI7jKlAhhnRAWSA aThLH+AhDIGLrY+F7Kkl65NwKQ1QMXEaBrJl2QudLhPxudCKoJvldI0Z+fEEU+UGWiVD HczRTAceeBeg6c7Sigg33nVRN8SE+FRj+JzfhsYP+VEATU/LkuoL4nQHHCEhPmX5C6DF oDUZoONePf7bWhCOp5xywFtJCgVCYQE2De96jRqpBNIE6FLQMIjnyisbXvKNnv+FDStT Gj+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=289WLOzQHTlVnE9kCItSDUtlmTT78yP8UJVw1iBBORo=; b=j7VAODhu9jBWtBiAqjicSAyM0VeWM+FgPFYOfNKiJYuPoKWZ2i/aNnUMaCAvDp4Yfi KlS3GCvA98BaGHbfwDry10IFL2Y97tDw+pjFlboxJortDDcv+KlfwBHs+OCOEgUTOWz6 fena4+GHlM0wlvNSMtXsMYvp+5hXTWXyZImK0WJMhc/nim4RkKngOumzCpd/p0Ok0pW8 uts6DTdHRNPRoIESBbCqopZX55PfZMpX7EOr7WrbMxAfKX0iMvb45oVpSevPpgxdKUql zj8sWaeZxX4K9/fNaj3gQIZMeFFYkAo6qDDhIF3fyRCb0jDW07QRu+1S2CN52B45vyx6 99hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cehJJbaA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gg19si9153560ejb.563.2020.05.31.03.04.30; Sun, 31 May 2020 03:04:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cehJJbaA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728039AbgEaKCt (ORCPT + 99 others); Sun, 31 May 2020 06:02:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:45132 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727119AbgEaKCt (ORCPT ); Sun, 31 May 2020 06:02:49 -0400 Received: from archlinux (cpc149474-cmbg20-2-0-cust94.5-4.cable.virginm.net [82.4.196.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 467A12074A; Sun, 31 May 2020 10:02:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590919368; bh=UT14kuMHzEg0IT0ew3DA1M9E1z1S3rJyfWwsDqEZrdk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=cehJJbaA+Bkm+TxyvEkKNqkNTzs3HNHa9boHB6hikzvvA1YQo3kjiZlMeReN6u5ZN /+UIZFLLiFxRihHOEQfoq4oGBe+qitk+wh8serINLaPlIcQ8ZJkZwZPgAhYHeGZTMz Mf9rPlbhZXGy6hRzB0wlNTclfbV2uVbEcMJ7QN2U= Date: Sun, 31 May 2020 11:02:44 +0100 From: Jonathan Cameron To: Tomasz Duszynski Cc: , , , , , Subject: Re: [PATCH v2 2/4] iio: chemical: scd30: add I2C interface driver Message-ID: <20200531110244.5773908d@archlinux> In-Reply-To: <20200530213630.87159-3-tomasz.duszynski@octakon.com> References: <20200530213630.87159-1-tomasz.duszynski@octakon.com> <20200530213630.87159-3-tomasz.duszynski@octakon.com> X-Mailer: Claws Mail 3.17.5 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 30 May 2020 23:36:28 +0200 Tomasz Duszynski wrote: > Add I2C interface driver for the SCD30 sensor. > > Signed-off-by: Tomasz Duszynski Looks good to me. J > --- > MAINTAINERS | 1 + > drivers/iio/chemical/Kconfig | 11 +++ > drivers/iio/chemical/Makefile | 1 + > drivers/iio/chemical/scd30_i2c.c | 134 +++++++++++++++++++++++++++++++ > 4 files changed, 147 insertions(+) > create mode 100644 drivers/iio/chemical/scd30_i2c.c > > diff --git a/MAINTAINERS b/MAINTAINERS > index 41a509cca6f1..13aed3473b7e 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -15142,6 +15142,7 @@ M: Tomasz Duszynski > S: Maintained > F: drivers/iio/chemical/scd30.h > F: drivers/iio/chemical/scd30_core.c > +F: drivers/iio/chemical/scd30_i2c.c > > SENSIRION SPS30 AIR POLLUTION SENSOR DRIVER > M: Tomasz Duszynski > diff --git a/drivers/iio/chemical/Kconfig b/drivers/iio/chemical/Kconfig > index 99e852b67e55..970d34888c2e 100644 > --- a/drivers/iio/chemical/Kconfig > +++ b/drivers/iio/chemical/Kconfig > @@ -96,6 +96,17 @@ config SCD30_CORE > To compile this driver as a module, choose M here: the module will > be called scd30_core. > > +config SCD30_I2C > + tristate "SCD30 carbon dioxide sensor I2C driver" > + depends on SCD30_CORE && I2C > + select CRC8 > + help > + Say Y here to build support for the Sensirion SCD30 I2C interface > + driver. > + > + To compile this driver as a module, choose M here: the module will > + be called scd30_i2c. > + > config SENSIRION_SGP30 > tristate "Sensirion SGPxx gas sensors" > depends on I2C > diff --git a/drivers/iio/chemical/Makefile b/drivers/iio/chemical/Makefile > index c9804b041ecd..0966ca34e34b 100644 > --- a/drivers/iio/chemical/Makefile > +++ b/drivers/iio/chemical/Makefile > @@ -13,6 +13,7 @@ obj-$(CONFIG_CCS811) += ccs811.o > obj-$(CONFIG_IAQCORE) += ams-iaq-core.o > obj-$(CONFIG_PMS7003) += pms7003.o > obj-$(CONFIG_SCD30_CORE) += scd30_core.o > +obj-$(CONFIG_SCD30_I2C) += scd30_i2c.o > obj-$(CONFIG_SENSIRION_SGP30) += sgp30.o > obj-$(CONFIG_SPS30) += sps30.o > obj-$(CONFIG_VZ89X) += vz89x.o > diff --git a/drivers/iio/chemical/scd30_i2c.c b/drivers/iio/chemical/scd30_i2c.c > new file mode 100644 > index 000000000000..a6b532b83669 > --- /dev/null > +++ b/drivers/iio/chemical/scd30_i2c.c > @@ -0,0 +1,134 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Sensirion SCD30 carbon dioxide sensor i2c driver > + * > + * Copyright (c) 2020 Tomasz Duszynski > + * > + * I2C slave address: 0x61 > + */ > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "scd30.h" > + > +#define SCD30_I2C_MAX_BUF_SIZE 18 > +#define SCD30_I2C_CRC8_POLYNOMIAL 0x31 > + > +static u16 scd30_i2c_cmd_lookup_tbl[] = { > + [CMD_START_MEAS] = 0x0010, > + [CMD_STOP_MEAS] = 0x0104, > + [CMD_MEAS_INTERVAL] = 0x4600, > + [CMD_MEAS_READY] = 0x0202, > + [CMD_READ_MEAS] = 0x0300, > + [CMD_ASC] = 0x5306, > + [CMD_FRC] = 0x5204, > + [CMD_TEMP_OFFSET] = 0x5403, > + [CMD_FW_VERSION] = 0xd100, > + [CMD_RESET] = 0xd304, > +}; > + > +DECLARE_CRC8_TABLE(scd30_i2c_crc8_tbl); > + > +static int scd30_i2c_xfer(struct scd30_state *state, char *txbuf, int txsize, > + char *rxbuf, int rxsize) > +{ > + struct i2c_client *client = to_i2c_client(state->dev); > + int ret; > + > + /* > + * repeated start is not supported hence instead of sending two i2c > + * messages in a row we send one by one > + */ > + ret = i2c_master_send(client, txbuf, txsize); > + if (ret != txsize) > + return ret < 0 ? ret : -EIO; > + > + if (!rxbuf) > + return 0; > + > + ret = i2c_master_recv(client, rxbuf, rxsize); > + if (ret != rxsize) > + return ret < 0 ? ret : -EIO; > + > + return 0; > +} > + > +static int scd30_i2c_command(struct scd30_state *state, enum scd30_cmd cmd, > + u16 arg, void *response, int size) > +{ > + char crc, buf[SCD30_I2C_MAX_BUF_SIZE], *rsp = response; > + int i, ret; > + > + put_unaligned_be16(scd30_i2c_cmd_lookup_tbl[cmd], buf); > + i = 2; > + > + if (rsp) { > + /* each two bytes are followed by a crc8 */ > + size += size / 2; > + } else { > + put_unaligned_be16(arg, buf + i); > + crc = crc8(scd30_i2c_crc8_tbl, buf + i, 2, CRC8_INIT_VALUE); > + i += 2; > + buf[i] = crc; > + i += 1; > + > + /* commands below don't take an argument */ > + if ((cmd == CMD_STOP_MEAS) || (cmd == CMD_RESET)) > + i -= 3; > + } > + > + ret = scd30_i2c_xfer(state, buf, i, buf, size); > + if (ret) > + return ret; > + > + /* validate received data and strip off crc bytes */ > + for (i = 0; i < size; i += 3) { > + crc = crc8(scd30_i2c_crc8_tbl, buf + i, 2, CRC8_INIT_VALUE); > + if (crc != buf[i + 2]) { > + dev_err(state->dev, "data integrity check failed\n"); > + return -EIO; > + } > + > + *rsp++ = buf[i]; > + *rsp++ = buf[i + 1]; > + } > + > + return 0; > +} > + > +static int scd30_i2c_probe(struct i2c_client *client) > +{ > + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) > + return -EOPNOTSUPP; > + > + crc8_populate_msb(scd30_i2c_crc8_tbl, SCD30_I2C_CRC8_POLYNOMIAL); > + > + return scd30_probe(&client->dev, client->irq, client->name, NULL, > + scd30_i2c_command); > +} > + > +static const struct of_device_id scd30_i2c_of_match[] = { > + { .compatible = "sensirion,scd30" }, > + { } > +}; > +MODULE_DEVICE_TABLE(of, scd30_i2c_of_match); > + > +static struct i2c_driver scd30_i2c_driver = { > + .driver = { > + .name = KBUILD_MODNAME, > + .of_match_table = scd30_i2c_of_match, > + .pm = &scd30_pm_ops, > + }, > + .probe_new = scd30_i2c_probe, > +}; > +module_i2c_driver(scd30_i2c_driver); > + > +MODULE_AUTHOR("Tomasz Duszynski "); > +MODULE_DESCRIPTION("Sensirion SCD30 carbon dioxide sensor i2c driver"); > +MODULE_LICENSE("GPL v2");