Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp433436lqh; Thu, 28 Mar 2024 06:34:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUwIrDi2oFEmAW5iEFZ3ljNRoLQ43uIGjuPWGrwJ7Hc8RS4pi1A8BZWDeef6s4YrhWqxxfzlsSIwz+vA8l1HD0meOh9TJp2Br+dN37w3g== X-Google-Smtp-Source: AGHT+IG5ul/yDXZT0lbyrm7JhSvkBDacQLpi1mb9i0On3XlC/KQBVz74wsfB2xG6luARUGOUFZvx X-Received: by 2002:a05:6808:16a8:b0:3c3:7e7a:8236 with SMTP id bb40-20020a05680816a800b003c37e7a8236mr3524446oib.18.1711632885732; Thu, 28 Mar 2024 06:34:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711632885; cv=pass; d=google.com; s=arc-20160816; b=nUxvK/l/ccgwdV/wGtrF2yMycE7/JpAIy0DWDYXrtlYLhsuaUc8TiUB5k/R9mTvP+V EEsVLzO/UeJDwNUdyQ3Plr8yHY0xu3WZ8q4MXZ8gkp70YkTdVlVKECWcOLcELkECgP9W TWVWkDcVe20FSpCoFWuIdN61sI/J6xK3A70GnRxlUKWgbRucjhZT7O8Jrj2uoVnkQtXj wcA7DqSfDbKYQPHBp13ST60wXzOP37f2gcRFPchU6V1oMjecWICzKkE7Duv7uvRjQLxg zD5IOvDJ6VGjsNSaVFBjUuMtp7SjjSEUMCna/1ahR/Xp1yWoU+6ewjT1H4kw5Ma+rzjy UX6w== 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=tbx+K6klE/8d1KnherxItZP4jMSt0JIV99OSLyV5TpU=; fh=1e6Cal33cWE5JW5ba8MF9Y9+aH2IllmjHRaK99oBic4=; b=CHonAXnV6NpRvPAAUtIwd2suI7qgFXbeEYr7eOCaMlJ0Qd7m79IhnWgfkLNNnvVN6s RGv4W+Udhdqfn1RGMMvrKJfjFUYY4RUpM+aYjssgAZZAZD63VtD7aMpLrYg2ADacvgwh yY5vXjM97xc4WeK6XDBHed4P+H1zYKrgQiyYiPbDuzWCOgGHjoz6Qe7Qm+NlXbCCTk48 KGcxk4ov6FnNQq2UaYhAHkC359srhHGu1B8vuftIcmV3g1DU1tJxwsXRyYHZ/7MEYKPt PEkCriqmN9c/vCaUZ76yG+wDsGlTBtNAD+wxoxUCr6x0JH0CAUjj9lN8HIpVf2YPAAiI kghw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PtrmNpjL; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-122958-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122958-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d17-20020a05622a05d100b00432b4580465si1277011qtb.531.2024.03.28.06.34.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 06:34:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122958-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PtrmNpjL; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-122958-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122958-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 5EFD01C262B1 for ; Thu, 28 Mar 2024 13:34:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E16A80BFC; Thu, 28 Mar 2024 13:34:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PtrmNpjL" 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 685D27E0E5; Thu, 28 Mar 2024 13:34:37 +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=1711632877; cv=none; b=SZ4+nstyCBy02zJGrDF0zsKk2LZiAmPvQt3ERAcSlY+1URdFW7/LnDZYiw9FjeVWm+LOTv7RDj6okXyWR5OwLLHk5sqBvx8tc3P42mJvlKPSHTtnNhosjbbnJR9IVs9hFwte3ajz5ohGq+7pjgUyvCm2SwTkaX9/C+tvPjs0nao= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711632877; c=relaxed/simple; bh=1NmwgZQEt4WxSUfU0jQNuBtFGQG8gZZKskF/G6lPMV8=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BYIR9v+pfop7hHsVm34GlI/E99wo9dEq6P/9vYNFNLkY3r3UwjnaDNR5QCuXxXjqq6vld/0ZZXeGR074Io34FasLSANxI49ZwT82eU+mJwaiN30Gnh7Q85llYBKO1Y/w7099ES7gI0gWdiSSpPVA+MHSLPpnDnUf7LPZtRN0K90= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PtrmNpjL; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id DA01BC433F1; Thu, 28 Mar 2024 13:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711632876; bh=1NmwgZQEt4WxSUfU0jQNuBtFGQG8gZZKskF/G6lPMV8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=PtrmNpjLmARH0mFur4fcmsRnS+NkMjCh69rTibYgJryMeRha1sXpiAoxC4WswSJPv +HcrXDcP6Fj7Ah9P9A4OaiSz1hbu6BMXADveOR7xZUji+z0md1bF/UR5rZ/V1B9yy0 fn44UE/aNMgswFifQkvzp4TpeuBhQwDZ71E+dR8CzKvls8eFlpNtTKS55hXdA666Me iZg8a+Xsli7d4iyTKK2HsC21VDunS+QLJXQ4KTKi8fk+zhkZBEBoogxCQECA8U/MIW ++SGCWTgkZ2yl+rxEYIJikv845H+/xJLGp4oynCIxeo5eVI4OrGsWaxdY13YQHNthp NLYgkfIjRyRnw== Date: Thu, 28 Mar 2024 13:34:21 +0000 From: Jonathan Cameron To: Thomas Haemmerle Cc: joel@jms.id.au, bsp-development.geo@leica-geosystems.com, Eddie James , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] iio: pressure: dps310: support negative pressure and temperature values Message-ID: <20240328133421.1844a83c@jic23-huawei> In-Reply-To: <20240327084937.3801125-1-thomas.haemmerle@leica-geosystems.com> References: <20240327084937.3801125-1-thomas.haemmerle@leica-geosystems.com> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; 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 Wed, 27 Mar 2024 09:49:36 +0100 Thomas Haemmerle wrote: > The current implementation interprets negative values returned from > function invocation as error codes, even those that report actual data. > This has a side effect that when temperature values are calculated - > they also converted by error code, which leads to false interpretation > of results. > > Fix this by using the return values only for error handling and passing > a pointer for the values. > > Signed-off-by: Thomas Haemmerle Hi Thomas, This needs a fixes tag so we know where to backport it to. A few other comments inline. Note that one aim in a fix is to keep things minimal to make it easy to backport. If you want to the follow the fix with a cleanup patch that makes the driver more consistent that is great, just don't combine that with the bug fix. Jonathan > --- > drivers/iio/pressure/dps310.c | 122 +++++++++++++++++++--------------- > 1 file changed, 69 insertions(+), 53 deletions(-) > > diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c > index 1ff091b2f764..373d1c063b05 100644 > --- a/drivers/iio/pressure/dps310.c > +++ b/drivers/iio/pressure/dps310.c > @@ -171,7 +171,7 @@ static int dps310_temp_workaround(struct dps310_data *data) > int reg; > > rc = regmap_read(data->regmap, 0x32, ®); > - if (rc) > + if (rc < 0) > return rc; Why this change? It seems unrelated to the issue you are fixing. > > /* > @@ -256,24 +256,24 @@ static int dps310_startup(struct dps310_data *data) > return dps310_temp_workaround(data); > } > > -static int dps310_get_pres_precision(struct dps310_data *data) > +static int dps310_get_pres_precision(struct dps310_data *data, int *val) > { > int rc; > - int val; > > - rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val); > + rc = regmap_read(data->regmap, DPS310_PRS_CFG, val); > if (rc < 0) > return rc; I'd prefer a local variable here for the intermediate result. > > - return BIT(val & GENMASK(2, 0)); > + *val = BIT(*val & GENMASK(2, 0)); For these precision values, it's positive anyway, so why change it to report this way? Consistency only or am I missing something else? > + > + return 0; > } > > -static int dps310_get_temp_precision(struct dps310_data *data) > +static int dps310_get_temp_precision(struct dps310_data *data, int *val) > { > int rc; > - int val; > > - rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val); > + rc = regmap_read(data->regmap, DPS310_TMP_CFG, val); As above, local variable for intermediate result would be clearer. > if (rc < 0) > return rc; > > @@ -281,7 +281,9 @@ static int dps310_get_temp_precision(struct dps310_data *data) > * Scale factor is bottom 4 bits of the register, but 1111 is > * reserved so just grab bottom three > */ > - return BIT(val & GENMASK(2, 0)); > + *val = BIT(*val & GENMASK(2, 0)); > + > + return 0; > } > > /* Called with lock held */ > @@ -350,48 +352,56 @@ static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq) > DPS310_TMP_RATE_BITS, val); > } > > -static int dps310_get_pres_samp_freq(struct dps310_data *data) > +static int dps310_get_pres_samp_freq(struct dps310_data *data, int *val) > { > int rc; > - int val; > > - rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val); > + rc = regmap_read(data->regmap, DPS310_PRS_CFG, val); Same again. > if (rc < 0) > return rc; > > - return BIT((val & DPS310_PRS_RATE_BITS) >> 4); > + *val = BIT((*val & DPS310_PRS_RATE_BITS) >> 4); Whilst here nice to use BIT(FIELD_GET(regval, DPS310_PRS_RATE_BITS)); > + > + return 0; > } > > -static int dps310_get_temp_samp_freq(struct dps310_data *data) > +static int dps310_get_temp_samp_freq(struct dps310_data *data, int *val) > { > int rc; > - int val; > > - rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val); > + rc = regmap_read(data->regmap, DPS310_TMP_CFG, val); > if (rc < 0) > return rc; > > - return BIT((val & DPS310_TMP_RATE_BITS) >> 4); > + *val = BIT((*val & DPS310_TMP_RATE_BITS) >> 4); As above. > + > + return 0; > } > > -static int dps310_get_pres_k(struct dps310_data *data) > +static int dps310_get_pres_k(struct dps310_data *data, int *val) > { > - int rc = dps310_get_pres_precision(data); > + int rc; > > - if (rc < 0) > + rc = dps310_get_pres_precision(data, val); > + if (rc) > return rc; > > - return scale_factors[ilog2(rc)]; > + *val = scale_factors[ilog2(*val)]; This only just went to the effort of 2^val, so why not skip that step and pull the BIT() section out to read_pressure() where we do want that form. You will need an extra local variable at that call site I think, but in general it is a useful additional simplification of the code. > + > + return 0; > } > > -static int dps310_get_temp_k(struct dps310_data *data) > +static int dps310_get_temp_k(struct dps310_data *data, int *val) > { > - int rc = dps310_get_temp_precision(data); > + int rc; > > - if (rc < 0) > + rc = dps310_get_temp_precision(data, val); > + if (rc) > return rc; > > - return scale_factors[ilog2(rc)]; > + *val = scale_factors[ilog2(*val)]; As above. > + > + return 0; > }