Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1524373lqb; Sun, 26 May 2024 05:21:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVv7JmJ+NmldALxSIvtlFxcz5CPobEm04YDouylaYHh7qJNLTfxx0cMjy53HtcjW1W+k1tfWZXShRPp7rVAQHjUkAC/eEQOzIEWTS8F/A== X-Google-Smtp-Source: AGHT+IEu1tu/j/WVhapH43lLeGxtuq3p3vayaUnvjQFe/qbC5YTVwGHo3f913+9qO/Q+4vTKDMqC X-Received: by 2002:a17:902:d490:b0:1f4:8363:a6ed with SMTP id d9443c01a7336-1f48363b687mr24687095ad.37.1716726084090; Sun, 26 May 2024 05:21:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716726084; cv=pass; d=google.com; s=arc-20160816; b=x0LcTtYSHsOsk9nte2FBVQzKxyjdbeWR3H/F6QTNviBxi74atVd2qWBlIr4xkdjWaZ htaLhgHPWj88hzYUSD1SymlcRl1LwlDiIbHaE8qIQ0XvC6rUralE6Rpz9LS8OdiEjTy4 Os9Nl/lMSfMsb3n0hExUXPKIvTj590Z9ubtYTqdzXKehOz68qLPuwcNCheUzhmajnpBd WfzWhS2+VMibkED0ZKqWhzUuePnxclxfkR3GpV0MmZdKVoEkmwypl5z6XJKOoOOkD6n3 G8+9I6y5ijQNxmHY9Kv9BaXumgiOzohaIM5asx7XodL0YORpHxeiAd0Wq7BNrssBgOTU mW/g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=c4w2NHjbNkq3uOfLC/5eJwTTe1SmqndE+xJbaHE4Td0=; fh=/pysYmf+ZjFXNFjitXmK3BJq8kPJjL2ze6dAsukb92g=; b=SKyw4cH9D4j+5bBKw8ZCJCLy7lIW/kgqBa8OvSYWmAU7eZWrvkJBw2cZcHL7Lj/dI5 uRY2brBsYHuWJ2tW+YBOpf+YvREY9nY6hkhWHEPfwqQtT9emNNMk2ON6gYnufMpZ1VZt 1/Y2Ep39ezfljEivxhrZvn7ks5ZZd7I3GCmFKv30lNl7780rWi62ZPDA1r/w+WUvo3bi TTsPjF7HCN50oigh6IEGVNA/zhTpFla7wqpvdaLEeHVUONpS/vG006ocMLwJg6msBMUy p1AFMbERmYn6uqmMGTHR1U6hYU09IoPa+iBetyNXD3k578b1AUPaVaR5p91p3xf7tBAW Tgxg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cVmtEots; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-189733-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189733-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d9443c01a7336-1f44c9700d3si44721895ad.294.2024.05.26.05.21.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 05:21:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189733-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cVmtEots; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-189733-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189733-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 02AC2B21245 for ; Sun, 26 May 2024 12:21:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 19EADDF43; Sun, 26 May 2024 12:21:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cVmtEots" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A7D1B676; Sun, 26 May 2024 12:21:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716726072; cv=none; b=C+qUbz1bXW8uieSk6IKULsHYonhcppWHBweMmOb21rJ0eK0F7jbtH+zQnjJYfIssEDn1iniX6IRT8gJEn+J+fapNKd4UGQBHA2dQRiw03AQOm5Z9z++/XYpOiw1lYfTLf6XsL3SHi4OnEEd9pOx/aWOLdkQggyyLl9ASgeE8y6Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716726072; c=relaxed/simple; bh=f1kHWS542LSTMfdmWktNiOaMXzRkAj1bsw6jPECvGyg=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gnfzQt+lRwHR2WW9r7pZO7evC0xzpPIC6h1FD9Hesxf7WJy91vQVmig9naH/ouAhIPJqHTvhk4oisRwTAfEFwqge6l1iX9bOgRv2u1TWiveTQ5Y4zBJc0AcnPKGCGHgl/PaQII6MoEe9EGQc6u6exKcK8Z8LVG0RbLjY+6A7a/k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cVmtEots; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91547C2BD10; Sun, 26 May 2024 12:20:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716726071; bh=f1kHWS542LSTMfdmWktNiOaMXzRkAj1bsw6jPECvGyg=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=cVmtEotsGjCe7Knku67qpVLkurVHkdp0+UAV1/yAg1a3bt+8qw/CwKEFEFqfD/wX2 qRt4e7M+tu/Kd97zmNp1iZiCU86Z02895CsbAFlm4qLvI+6pLGYTfcgaz0lPaOUYTx EAmaNKrdlQi0H/JLJDLxz19BFzhxCjST1QY9zVH0qQ4Cre++oI5ULhHErkTQg3yDYK 2n+gTJFxut/c/Z1Tjk4x23eSvCS51zZOgTIQmpIi36qFT0Cz2cVKf/acACOMloJgGg kknxXzUQPxFnaxUmEvNAbL0rYq26bukoAYX2bUrPgE3T4FbvxYvhmWdJsCJWYhPxwl 3MhZp6lORJIVw== Date: Sun, 26 May 2024 13:20:25 +0100 From: Jonathan Cameron To: Gustavo Silva Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, lars@metafoo.de, christophe.jaillet@wanadoo.fr, gerald.loacker@wolfvision.net, devicetree@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/6] iio: chemical: add driver for ENS160 sensor Message-ID: <20240526131927.1ab92e13@jic23-huawei> In-Reply-To: References: <20240512210444.30824-1-gustavograzs@gmail.com> <20240512210444.30824-4-gustavograzs@gmail.com> <20240519150151.291a21dc@jic23-huawei> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.42; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sat, 25 May 2024 21:29:42 -0300 Gustavo Silva wrote: > Hi Jonathan, > > Thank you for your review. I've got a few questions inline. > > On Sun, May 19, 2024 at 03:01:51PM GMT, Jonathan Cameron wrote: > > On Sun, 12 May 2024 18:04:39 -0300 > > Gustavo Silva wrote: > > > > > ScioSense ENS160 is a digital metal oxide multi-gas sensor, designed > > > for indoor air quality monitoring. The driver supports readings of > > > CO2 and VOC, and can be accessed via both SPI and I2C. > > > > > > Signed-off-by: Gustavo Silva > > > > > + > > > +static int ens160_read_raw(struct iio_dev *indio_dev, > > > + struct iio_chan_spec const *chan, > > > + int *val, int *val2, long mask) > > > +{ > > > + struct ens160_data *data = iio_priv(indio_dev); > > > + __le16 buf; > > > + int ret; > > > + > > > + switch (mask) { > > > + case IIO_CHAN_INFO_RAW: > > > + ret = regmap_bulk_read(data->regmap, chan->address, > > > + &buf, sizeof(buf)); > > > > As below, should use a DMA safe buffer. > > > > > +static int ens160_chip_init(struct ens160_data *data) > > > +{ > > > + struct device *dev = regmap_get_device(data->regmap); > > > + u8 fw_version[3]; > > > + __le16 part_id; > > > + unsigned int status; > > > + int ret; > > > + > > > + ret = ens160_set_mode(data, ENS160_REG_MODE_RESET); > > > + if (ret) > > > + return ret; > > > > No docs that I can see on what this means for access to registers etc. > > Good to add a comment if you have info on this. > > > Performing a reset at this point isn't strictly necessary. When we reach > this point the chip should be in idle state because: > > a) it was just powered on Maybe but we have no way of telling that > > b) the driver had been previously removed Maybe or maybe not - the device may have just done a soft reboot and switched operating system. We have no idea what the hardware state is. As such the reset is a good idea. > > This is documented in the state diagram on page 24 of the datasheet. > > I'll remove this reset. Better to keep the reset and provide info on what it means wrt to accessing registers etc if possible. If there is no information then obviously not much you can add in the way of documentation! > > > > + > > > + ret = regmap_bulk_read(data->regmap, ENS160_REG_PART_ID, &part_id, > > > + sizeof(part_id)); > > > > Ah. So this is a fun corner case. Currently regmap makes not guarantees > > to always bounce buffer things (though last time I checked it actually did > > do so - there are optimisations that may make sense where it will again > > not do so). So given we have an SPI bus involved, we should ensure that > > only DMA safe buffers are used. These need to ensure that no other data > > that might be changed concurrently with DMA is in the same IIO_DMA_MINALIGN > > of aligned data (traditionally a cacheline but it gets more complex in some > > systems and is less in others). Upshot is that if you are are doing > > bulk accesses you need to use a buffer that is either on the heap (kzalloc etc) > > or carefully placed at the end of the iio_priv() structure marked > > __align(IIO_DMA_MINALIGN). Lots of examples of that in tree. > > If you are curious, wolfram did a good talk on the i2c equivalent of this > > a few years back. > > https://www.youtube.com/watch?v=JDwaMClvV-s I think. > > > Interesting. Thank you for the detailed info. > > > > + if (ret) > > > + return ret; > > > + > > > + if (le16_to_cpu(part_id) != ENS160_PART_ID) > > > + return -ENODEV; > > > + > > > + ret = ens160_set_mode(data, ENS160_REG_MODE_IDLE); > > > + if (ret) > > > + return ret; > > > + > > > + ret = regmap_write(data->regmap, ENS160_REG_COMMAND, > > > + ENS160_REG_COMMAND_CLRGPR); > > > + if (ret) > > > + return ret; > > > + > > > + ret = regmap_write(data->regmap, ENS160_REG_COMMAND, > > > + ENS160_REG_COMMAND_GET_APPVER); > > > + if (ret) > > > + return ret; > > > + > > > + msleep(ENS160_BOOTING_TIME_MS); > > > > Why here? Not obviously associated with a boot command? > > A comment might make this easier to follow. I 'think' it is > > because this next read is the first time it matters. If so that > > isn't obvious. Also, there is an existing sleep in the mode set, > > so I'm not sure why we need another one. > > > The usage of booting time is not documented in the datasheet. From > ScioSense's arduino driver the booting time is necessary after setting > the operation mode. I performed some tests that confirm this. > > In this case in particular it is not necessary. Maybe I forgot to remove > it after some testing. > > > + > > > + ret = regmap_bulk_read(data->regmap, ENS160_REG_GPR_READ4, > > > + fw_version, sizeof(fw_version)); > > > Does this bulk read also need to be made DMA safe? I'm guessing in this > case it would be best to devm_kzalloc() a buffer of three bytes? All bulk accesses need dma safe buffers. I would take the approach many IIO drivers do of not doing another allocation (which has overheads etc) but instead just add a suitable __aligned(IIO_DMA_MINALIGN) buffer to the iio_priv structure. Note you normally only need to do mark the first one like that as we don't care if various different DMA buffers are in the same cacheline as the SPI controller should not cause DMA safety issues with itself. > > > The first datasheet that google provided me has this > > GPR_READ0/GPR_READ1 and only 2 bytes. I hope they have maintained backwards > > compatibility with that earlier doc! > > > > When you do a separate DT binding in v2, make sure to include a link > > to the datasheet you are using. Also use a Datasheet: tag > > in this patch to make it easy to find that. > > I dug a little deeper and found the one on sciosense own website > > - ah, you do have it in the comments. Add to the commit message > > and DT binding as well. > > > > > > > + if (ret) > > > + return ret; > > > + > > > + msleep(ENS160_BOOTING_TIME_MS); > > Why again? > Again, not needed. I'll remove it. > > > > + data = iio_priv(indio_dev); > > > + dev_set_drvdata(dev, indio_dev); > > > > After you've moved to devm_add_action_or_reset() for the unwind of > > ens160_chip_init() you probably don't need to set the drvdata. > > > I don't get it. Could you please elaborate on why it isn't needed to > set drvdata after the change? No other users. Only the remove() callback calls the matching get_drvdata() and that function won't exist once you've added device managed callbacks to handle everything it does. Jonathan