Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp1649758lqo; Sun, 12 May 2024 11:43:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXQmcqKm7b8NQJ9JZvDz1W98+uv2pMGy+sJD0Kg6GpGb/SLEIY7QsfM7Oknj4fHOWDbswojSZGzBoTEb0rQ5uMyOunex79Yt3u7KH58vQ== X-Google-Smtp-Source: AGHT+IEtSdEPsSuWGGc9BiUnWgu2AHMNF1yJcNb6IvdwKhkrTzAbmHQKXFGSeACKSdxPKgCnVdKW X-Received: by 2002:a17:902:b681:b0:1e4:c75e:aad8 with SMTP id d9443c01a7336-1ef43d2ec80mr71208715ad.26.1715539385134; Sun, 12 May 2024 11:43:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715539385; cv=pass; d=google.com; s=arc-20160816; b=PJALKrc8jG6suGiiKs60cotbXWqItVR+ZUt1982dRNcmbqGeEVvIKXgiB0c2R68M0Y YMKqvajjviIeN+B3jCe5PK5URYuQ/37YeMkGYepg34Z9kiFOqa8rJoQataQXyMvbuK84 nic6CGAjXXMSm8evZjIeyqfhe6eXclH+eiNzobCovjLAZJ3UUYjN3W7KIqPhstpAtlSl e14gFEjQ8hMKAyWMuOc0v7+9Ir81nnvWCE/rIVzB8eOXm5zgsFsJ/T/FDG/1vFTSnGhp XYpYfU6xe1Xe1DdgtUl1cakDKfKuvv30zZQcsXGWcYfc71vYL7f+VYF0opYvGi8ACwfw /1Qw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:date:from:dkim-signature; bh=8iSdY4IAt1rjl81SkB6DZ1/llfE2KLF2W//U478tz9U=; fh=Zw3EkkkbMe2+ijTr1c/Cymvaq4RWWzMkMge3/udkyAo=; b=yOadXWBlspJ3eP+ToGa7p4IBik4+79D+b+ZUDyHj3xLUZfdXIIPzSnyWnvX8zFHeEd L5ovGQb9xlNBAHnRR9sMfgE0IlQ4YvMIu90TMcJqaDhnrXZXfgpsHpTW+8+fBEivyeWM 4986pdDjFYwMTL/5VEGluynEZfMzPEQwdq6NM3Pevi9bbWJeE3Y0ljA0+vNZYAiGT3jr BrsqKsQ1pauW2U60pr0SwUWn0zWSBBfZU0Gy8HyAKtmcopV6BZPafGLhgKBWCrdQgcsI XDkpVFFX2Jn1TS25QUTIeuOFDJw/qDOMLtOmKKF5gRIUCFZL7z1/3IW2XLmbsiFrCaba 8muA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gMb9F+pY; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-176976-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176976-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0c14c77esi78445165ad.476.2024.05.12.11.43.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 May 2024 11:43:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-176976-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gMb9F+pY; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-176976-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176976-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B89D22823C7 for ; Sun, 12 May 2024 18:43:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A4E334F205; Sun, 12 May 2024 18:42:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gMb9F+pY" Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CACBF134AC; Sun, 12 May 2024 18:42:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715539376; cv=none; b=FKZgX1riJH7hGoorIxH0h8cr5yJuPJdXYRZ+clCmunwusnvvgtQ+uVqkOEFJB3HEUULRJYE+8ZgqANwvuof6w63OSSDn78f9m6DfT1ee52Cgg2lN6jSn4U9XH8+xq+6NErNuPntmvk6Br9LWbnW4xksAxqvP9NKl0AF3IfVBiKQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715539376; c=relaxed/simple; bh=2Aowy+0Dh1NCr451QxQds3RyzTAecA3csen5IePe5t8=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=daaz9mVz/cQrBYyeIt7bl6ek0EXrd0wyXxC10rMnrYVDtceWa6QYXhYIeMuy9ueJgRbCeeady2nbZg75Epqo9weg7NQ/IOTe75HMEAww3/vGexPm7mW8kx8heSW8uJWAb60kXoTKeP2V9e7QCkB4TOu41w8QRObCTAK1uHVkVRU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gMb9F+pY; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-41ffad242c8so14455165e9.3; Sun, 12 May 2024 11:42:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715539373; x=1716144173; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=8iSdY4IAt1rjl81SkB6DZ1/llfE2KLF2W//U478tz9U=; b=gMb9F+pY1lp7+n54DMEn6Ke1QHdFwHAeQLA74gjtolDdz3GrBbIzHMBu7749ooc1Dt vda+z+D9To+rrsDYIvyFY4NEPe7CRY24dNJafGta9qby6iY7M9NcdTHOXOP80563+Jke 6e7Mg0AJjOAVYfHfEH+TSU/4A/0dxQCK2UP3zb8klyLfzJwy4SKVHMk64xIJ0L7dL1Qb q3iFCQoP4CTt+YkNNX1IiBayPLjBJKP2EsUXUXCRQFOvWvQG0jZ5lGtcVcXg5Td12lfe VG/1zDMeQTYDeeRa5L0iGiAbxFIASgQtkUOloKOGDv/bIDlusdk/qORd97Ey0Q0dgF2Q amxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715539373; x=1716144173; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=8iSdY4IAt1rjl81SkB6DZ1/llfE2KLF2W//U478tz9U=; b=WKm4nvZtAdenfsQ/6NusnCUXZjpCO0DLYvoMLzPQttO/Eq5dMnTHvp6AMtFU4fYuqI ZSa1Ux0/g8I+zE6LXbJChU/YnN9ZDoYi9DEeCAG1S5SaZjtk/XbU+ZcXteABWtFFMvoD mRHxpaIbeSwF8iU41Cc1Jt0yQT0gxKZ7NI9bb1+pe0VCsRk0TzhQrzEw0bDhqKwVm7Ej 021zgZFqceNX7TzdclWzvvWvtAeI0e3NM85j+7NYJ1g0z6Rq6rOORMoN/F8ChdtVPmgF eppLZQhSL8xG1a6DejUOLxifftLXPTjJB0ixcmRm6Mt16joLhc3FZKniFZbwR60WL8pB wdEw== X-Forwarded-Encrypted: i=1; AJvYcCUlRT6NPpTRYOTtxjdLHah2W/yByOAkkbvQSoqHB6Ijjo/WndkL75pLP65zz3YiosZuWeRjd7pPd4SytG+I0yiEQHR8AG8whIw8uqUfPDWsvX8bN7GcO7i8bs1BSBjUJAACO/FZZdX5 X-Gm-Message-State: AOJu0YxfFDvykOqv632Gtz93s3Q2GBvhMJX6pYq0gBhEKozeVoQwUUx0 RaPcki6i1OmlWCJuYteMqWTywY97fZjB2ovtcbW/zTRWZTNJhxJynNYRQW52 X-Received: by 2002:a05:600c:444c:b0:41b:b39c:603 with SMTP id 5b1f17b1804b1-41fea53833cmr83349525e9.0.1715539372810; Sun, 12 May 2024 11:42:52 -0700 (PDT) Received: from vamoiridPC ([2a04:ee41:82:7577:ce14:864c:436e:5c6d]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3502bbbbf20sm9328904f8f.93.2024.05.12.11.42.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 May 2024 11:42:52 -0700 (PDT) From: Vasileios Amoiridis X-Google-Original-From: Vasileios Amoiridis Date: Sun, 12 May 2024 20:42:50 +0200 To: Jonathan Cameron Cc: Vasileios Amoiridis , lars@metafoo.de, andriy.shevchenko@linux.intel.com, ang.iglesiasg@gmail.com, mazziesaccount@gmail.com, ak@it-klinger.de, petre.rodan@subdimension.ro, phil@raspberrypi.com, 579lpy@gmail.com, linus.walleij@linaro.org, semen.protsenko@linaro.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron Subject: Re: [PATCH v6 5/9] iio: pressure: bmp280: Refactorize reading functions Message-ID: <20240512184250.GA24095@vamoiridPC> References: <20240508165207.145554-1-vassilisamir@gmail.com> <20240508165207.145554-6-vassilisamir@gmail.com> <20240512132412.35d8fe36@jic23-huawei> 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-Disposition: inline In-Reply-To: <20240512132412.35d8fe36@jic23-huawei> On Sun, May 12, 2024 at 01:24:12PM +0100, Jonathan Cameron wrote: > On Wed, 8 May 2024 18:52:03 +0200 > Vasileios Amoiridis wrote: > > > For BMP18x, BMP28x, BME280, BMP38x the reading of the pressure > > value requires an update of the t_fine variable which happens > > through reading the temperature value. > > > > So all the bmpxxx_read_press() functions of the above sensors > > are internally calling the equivalent bmpxxx_read_temp() function > > in order to update the t_fine value. By just looking at the code > > this functionality is a bit hidden and is not easy to understand > > why those channels are not independent. > > > > This commit tries to clear these things a bit by splitting the > > bmpxxx_{read/compensate}_{temp/press/humid}() to the following: > > > > i. bmpxxx_read_{temp/press/humid}_adc(): read the raw value from > > the sensor. > > > > ii. bmpxx_calc_t_fine(): calculate the t_fine variable. > > > > iii. bmpxxx_get_t_fine(): get the t_fine variable. > > > > iv. bmpxxx_compensate_{temp/press/humid}(): compensate the adc > > values and return the calculated value. > > > > v. bmpxxx_read_{temp/press/humid}(): combine calls of the > > aforementioned functions to return the requested value. > > > > Suggested-by: Jonathan Cameron > > Signed-off-by: Vasileios Amoiridis > A few minor things inline. Given I've picked up the 1st 4 patches, > please base your v7 on top of those. > > Thanks, > > Jonathan > > > --- > > drivers/iio/pressure/bmp280-core.c | 361 ++++++++++++++++++----------- > > drivers/iio/pressure/bmp280.h | 6 - > > 2 files changed, 232 insertions(+), 135 deletions(-) > > > > diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c > > index dd5c526dacbd..a864f8db8e24 100644 > > --- a/drivers/iio/pressure/bmp280-core.c > > +++ b/drivers/iio/pressure/bmp280-core.c > > @@ -288,13 +288,35 @@ static int bme280_read_calib(struct bmp280_data *data) > > * > > * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula". > > */ > > Seems this comment should probably follow the maths which has moved. > > > +static int bme280_read_humid_adc(struct bmp280_data *data, u16 *adc_humidity) > > +{ > > + u16 value_humidity; > > + int ret; > > + > > + ret = regmap_bulk_read(data->regmap, BME280_REG_HUMIDITY_MSB, > > + &data->be16, sizeof(data->be16)); > > + if (ret) { > > + dev_err(data->dev, "failed to read humidity\n"); > > + return ret; > > + } > > + > > + value_humidity = be16_to_cpu(data->be16); > > + if (value_humidity == BMP280_HUMIDITY_SKIPPED) { > > + dev_err(data->dev, "reading humidity skipped\n"); > > + return -EIO; > > + } > > + *adc_humidity = value_humidity; > > + > > + return 0; > > +} > > ... > > @@ -313,8 +335,29 @@ static u32 bme280_compensate_humidity(struct bmp280_data *data, > > * > > * Taken from datasheet, Section 3.11.3, "Compensation formula". > > */ > Is this comment still relevant here? Seems it should probably move to follow > that maths. > > > -static s32 bmp280_compensate_temp(struct bmp280_data *data, > > - u32 adc_temp) > > +static int bmp280_read_temp_adc(struct bmp280_data *data, u32 *adc_temp) > > +{ > > > > > static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2) > > { > > - u32 adc_press, comp_press; > > + u32 adc_press, comp_press, t_fine; > > int ret; > > > > - /* Read and compensate for temperature so we get a reading of t_fine */ > > - ret = bmp380_read_temp(data, NULL, NULL); > > + ret = bmp380_get_t_fine(data, &t_fine); > > if (ret) > > return ret; > > > > - ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, > > - data->buf, sizeof(data->buf)); > > - if (ret) { > > - dev_err(data->dev, "failed to read pressure\n"); > > + ret = bmp380_read_press_adc(data, &adc_press); > > + if (ret) > > return ret; > > - } > > > > - adc_press = get_unaligned_le24(data->buf); > > - if (adc_press == BMP380_PRESS_SKIPPED) { > > - dev_err(data->dev, "reading pressure skipped\n"); > > - return -EIO; > > - } > > - comp_press = bmp380_compensate_press(data, adc_press); > > + comp_press = bmp380_compensate_press(data, adc_press, t_fine); > > > > + /* IIO units are in kPa */ > > Probably worth keeping the reference to what the unit of the > datasheet maths is around. > > > *val = comp_press; > > - /* Compensated pressure is in cPa (centipascals) */ > > *val2 = 100000; > > > > return IIO_VAL_FRACTIONAL; > > @@ -1825,7 +1916,7 @@ static int bmp180_wait_for_eoc(struct bmp280_data *data, u8 ctrl_meas) > > return 0; > > } > > > > -static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press) > > +static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press, > > + s32 t_fine) > > { > > struct bmp180_calib *calib = &data->calib.bmp180; > > s32 oss = data->oversampling_press; > > @@ -1965,7 +2068,7 @@ static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press) > > s32 b3, b6; > > u32 b4, b7; > > > > - b6 = data->t_fine - 4000; > > + b6 = t_fine - 4000; > > x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11; > > x2 = calib->AC2 * b6 >> 11; > > x3 = x1 + x2; > > @@ -1974,7 +2077,7 @@ static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press) > > x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16; > > x3 = (x1 + x2 + 2) >> 2; > > b4 = calib->AC4 * (u32)(x3 + 32768) >> 15; > > - b7 = (adc_press - b3) * (50000 >> oss); > > + b7 = (((u32)adc_press) - b3) * (50000 >> oss); > > Casting from u32 to u32? > > > if (b7 < 0x80000000) > > p = (b7 * 2) / b4; > > else > > @@ -1990,19 +2093,19 @@ static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press) > > static int bmp180_read_press(struct bmp280_data *data, int *val, int *val2) > > > + /* IIO units are in kPa */ > > I think this is an unrelated improvement as original code doesn't have such a comment. > So shouldn't really be in this patch. If you want to keep it here rather than pushing it > into an additional patch, mention it in the commit message. "additional comments on base > units added for consistency" or something like that. > > *val = comp_press; > > *val2 = 1000; Hi Jonathan! Thank you very much once again for the amazing feedback! As it looks like, I changed the code but I forgot to move the comments accordingly. Thank you very much for pointing this out. The extra comments are indeed not necessary, they don't add some specific value so I can drop them. Cheers, Vasilis