Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp755943rwb; Mon, 26 Sep 2022 05:35:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7xzDvYBY/a4L2zUubPaijZBy5q/xXbKB8enO4Ox2sWAmNZWqQJ5KfAo7y03x7xM7UQ3Pjf X-Received: by 2002:a17:907:805:b0:782:1a0d:337f with SMTP id wv5-20020a170907080500b007821a0d337fmr18198481ejb.475.1664195701124; Mon, 26 Sep 2022 05:35:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664195701; cv=none; d=google.com; s=arc-20160816; b=BDg/qQRPjrpXPpNokSXPx5bm9QfvAAYEg9fFMmB1mp/PLjwPRuJFJcaVVuHCs27n2K bf8MQkeKx22zJb5yY/O9XPxS1V3kUXuJ4LUJ7sMU+5b+kX8Sbw9RD3CriUaPH633b4A5 w21YcBnwm8Eqql/M2B15s+2Y2JOJZaE+g0mqlcgHwpUtwSvYmsoMdac5DlKiMFecjpy5 quZJ6gvo8R8XynM+75p3EffIVtxhGd913O2asAe88R+kwyGRNWKt65oEzL9jAe0ewxSx vDX0fnl9WD+B4/HTbP+pXQnkttJ/i/JM5FPEimtKNfEmCjhDsjbC9kSN8lKUmLqfZ/m7 FQKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id :dkim-signature; bh=CUyy93fVWZiZBeHDU8yZwsYToR2inVd+vCWQtJE+jak=; b=XiGwLZEcgA1p0x7nxSE0t9qyk6bFcKq55LtYxjHVhQhWbv+r1K+388Z4odNrl0pepg dX1doeEyaZ8JpfUm7Evjw4r282PyeVoafPiIS5oX750JhuVPTSqHHQDGEykltVMhqfGf OvZvGV9I3ujv0GPrDxtYjCLmDlsyV3VlPVHTGzLTTKGAcCzLRFmfegkDfFyAQ7CuO93q EXMBbnG1YWH9jEv1edlrExFKPfvnF8RSmuyxNsSuxIDaqLgGmtJZ+N2FCWso1s3kpUnV J9kN1HmgNeqZ7rF/UmFvS2ImR+jg+xoP1SoS0vdIKw5a2jRZQuJRK8Z2/iaOScZP/pWd vXuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ojr9bKEy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s7-20020a50ab07000000b00446dc8f5e44si15756048edc.243.2022.09.26.05.34.34; Mon, 26 Sep 2022 05:35:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ojr9bKEy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S237585AbiIZLMO (ORCPT + 99 others); Mon, 26 Sep 2022 07:12:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237487AbiIZLLQ (ORCPT ); Mon, 26 Sep 2022 07:11:16 -0400 Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8F264333E; Mon, 26 Sep 2022 03:35:24 -0700 (PDT) Received: by mail-lj1-f178.google.com with SMTP id a14so6879740ljj.8; Mon, 26 Sep 2022 03:35:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:subject:from:cc:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date; bh=CUyy93fVWZiZBeHDU8yZwsYToR2inVd+vCWQtJE+jak=; b=Ojr9bKEyXSZ73KrB5cZzFDeH/ffP8PKSqHhrgt/se5yA3QCCeURStYLcNLSRiap7Yb 96tFUYk28OgmUOR6qLy9UqlhpaSbiIreANSMfGPehSVRN0n7lew4eLBKpDV7McG8WpR2 bBzHCd1+/0MEvjoLtwpWji2VArs+RbJijFUthbA0CR7JXMIJTcjWGu5O9/Q746/YpXak 0g9goUG0yms1D7JTu75k1HAKLSM+gUW8awnOXJHXhHNlQzv6hSE5D4my5Yllwx+QhFZy Nph7JXrHdur+tpaj39lTV18CqOfgj6NfMx7pKG6zqzBPcEySvaXNfbydTLV42EclHQ3D Ac3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:subject:from:cc:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date; bh=CUyy93fVWZiZBeHDU8yZwsYToR2inVd+vCWQtJE+jak=; b=wc07T5KmD9l+qxiUNgFA9EBT5Sp7z5obl3pE4QJ3jrPCA1ee4my/orkJy+Mc5QXu2z veQSW1Ey9euuVGgURQamPQHl/R88IeZjYo48NAvq4de1BLpirXd7AdUcDG3cCvrXj0m5 23r+HC2iR//4WKkspqj3JIIKWVw4hpRC1+sdwmyC/jvOPl6gl/6rT+JZtprykBgbkH31 Qq12GWOSyu8Lx0mDUoLgQsAfQQlYLRr6yJ2JUS9sc3PnoglVwQ6EvpMq+Jze/6FW3s6v MnkvV3MUUj7rESUHFX+RVDFPOXG73Htuxj2fD1QvbzsaXTOQxRlx/APaSoCiAQf9I4XW DQCQ== X-Gm-Message-State: ACrzQf21FdXBo2lzO5P8ZsnhHtX11/Mvik1A+yf6U2qNEiiikvbNOuH6 MyT7gBEv+c+OQFL4oxtri1k= X-Received: by 2002:a2e:1458:0:b0:26c:3b83:e039 with SMTP id 24-20020a2e1458000000b0026c3b83e039mr7832436lju.484.1664188123948; Mon, 26 Sep 2022 03:28:43 -0700 (PDT) Received: from [172.16.196.5] ([213.255.186.46]) by smtp.gmail.com with ESMTPSA id z13-20020a05651c11cd00b0026a92616cd2sm2343887ljo.35.2022.09.26.03.28.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Sep 2022 03:28:43 -0700 (PDT) Message-ID: Date: Mon, 26 Sep 2022 13:28:41 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Content-Language: en-US To: Matti Vaittinen , "Vaittinen, Matti" Cc: Jonathan Cameron , Lars-Peter Clausen , Alexandru Ardelean , Mihail Chindris , Miaoqian Lin , Andy Shevchenko , Miquel Raynal , =?UTF-8?Q?Uwe_Kleine-K=c3=b6nig?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org From: Matti Vaittinen Subject: bmc150-accel-core: potential timestamp calculation issue. Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi dee Ho peeps! I selected you as a recipient purely based on get_maintainer.pl. Please drop me a note if the bmc150-accel-core.c does not feel like your playground and I'll drop you from the CC if I send further mails for this topic. Sorry in advance. I was ruthlessly copying timestamp logic from the bmc150-accel-core.c for my accelerometer driver. I noticed there may be a problem if FIFO is not read empty: The code computes time between two samples by ... * * To avoid this issue we compute the actual sample period ourselves * based on the timestamp delta between the last two flush operations. */ sample_period = (data->timestamp - data->old_timestamp); do_div(sample_period, count); tstamp = data->timestamp - (count - 1) * sample_period; Here the "count" is amount of samples bmc-150 reports there is in fifo. As far as I understand, this works only if the old_timestamp match the time when first sample in FIFO has been captured. This is true if the previous 'flush' (where the old_timestamp is stored) has emptied the FIFO - but as far as I understand the IIO does not guarantee the flush reading all samples there are in the FIFO. If the flush leaves - say 10 samples in FIFO, then at the next flush there old_timestamp will be time when FIFO was previously flushed, but this time does not match the first sample in FIFO, but the 6.th. Let's say the sensor has collected 10 new samples between the flushes. This would mean there is now 10 new and 5 old samples in FIFO, totaling 15 samples. Yet, the time difference computed by sample_period = (data->timestamp - data->old_timestamp); will reflect time between flushes - which means time of 10 samples. The division which should compute time between two samples: do_div(sample_period, count); will now use incorrect amount of samples (10 + 6) and the sample period will be too small. (Or is there something I don't quite understand). I added following piece of code in the driver I am developing: if (samples && count > samples) { /* * Here we leave some old samples to the buffer. We need to * adjust the timestamp to match the first sample in the buffer * or we will miscalculate the sample_period at next round. */ data->timestamp -= (count - samples) * sample_period; count = samples; } I can send this as a patch also to the bmc150-accel-core.c - but I'd prefer someone who is familiar with this IC to verify my finding and suggested fix :) Yours, -- Matti -- Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~