From patchwork Fri Dec 8 17:41:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Stefan_Br=C3=BCns?= X-Patchwork-Id: 10103091 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E786360329 for ; Fri, 8 Dec 2017 17:44:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CDE0B28E42 for ; Fri, 8 Dec 2017 17:44:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C2EC728E50; Fri, 8 Dec 2017 17:44:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 77A8928E42 for ; Fri, 8 Dec 2017 17:44:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753247AbdLHRoK (ORCPT ); Fri, 8 Dec 2017 12:44:10 -0500 Received: from mail-out-2.itc.rwth-aachen.de ([134.130.5.47]:62483 "EHLO mail-out-2.itc.rwth-aachen.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752963AbdLHRmN (ORCPT ); Fri, 8 Dec 2017 12:42:13 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A2CLBQDbzSpa/54agoZcHAEBAQQBAQoBA?= =?us-ascii?q?YM+ggEHg3uZIYFXmUcKhTsChF9DFAEBAQEBAQEBAWsohSMGIwQLAUYQJQImAgJ?= =?us-ascii?q?XBg4FiigEqC6BbTqISYIaAQEBAQEFAQEBAQEjCQGBBYJMgguDaAuIYoJJgmMFi?= =?us-ascii?q?kWYQ4EQoBqHV5YvAgICAgkCGoE7NiKBT3CCd4JSHIFod4khAYEUAQEB?= X-IPAS-Result: =?us-ascii?q?A2CLBQDbzSpa/54agoZcHAEBAQQBAQoBAYM+ggEHg3uZIYF?= =?us-ascii?q?XmUcKhTsChF9DFAEBAQEBAQEBAWsohSMGIwQLAUYQJQImAgJXBg4FiigEqC6Bb?= =?us-ascii?q?TqISYIaAQEBAQEFAQEBAQEjCQGBBYJMgguDaAuIYoJJgmMFikWYQ4EQoBqHV5Y?= =?us-ascii?q?vAgICAgkCGoE7NiKBT3CCd4JSHIFod4khAYEUAQEB?= X-IronPort-AV: E=Sophos;i="5.45,378,1508796000"; d="scan'208";a="28277017" Received: from rwthex-w2-a.rwth-ad.de ([134.130.26.158]) by mail-in-2.itc.rwth-aachen.de with ESMTP; 08 Dec 2017 18:42:06 +0100 Received: from pebbles.fritz.box (78.48.176.14) by rwthex-w2-a.rwth-ad.de (2002:8682:1a9e::8682:1a9e) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1034.26; Fri, 8 Dec 2017 18:42:03 +0100 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= To: CC: Peter Meerwald-Stadler , =?UTF-8?q?Stefan=20Br=C3=BCns?= , Maciej Purski , , "Andrew F . Davis" , Lars-Peter Clausen , Jonathan Cameron , Hartmut Knaack , "Javier Martinez Canillas" Subject: [PATCH v1 5/7] iio: adc: ina2xx: Use a monotonic clock for delay calculation Date: Fri, 8 Dec 2017 18:41:50 +0100 X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171208174152.30341-1-stefan.bruens@rwth-aachen.de> References: <20171208174152.30341-1-stefan.bruens@rwth-aachen.de> MIME-Version: 1.0 X-Originating-IP: [78.48.176.14] X-ClientProxiedBy: rwthex-w3-a.rwth-ad.de (2002:8682:1aa2::8682:1aa2) To rwthex-w2-a.rwth-ad.de (2002:8682:1a9e::8682:1a9e) Message-ID: <7330c7df-330f-449f-8706-a2407e92ebcf@rwthex-w2-a.rwth-ad.de> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The iio timestamp clock is user selectable and may be non-monotonic. Also, only part of the acquisition time is measured, thus the delay was longer than intended. Signed-off-by: Stefan BrĂ¼ns --- drivers/iio/adc/ina2xx-adc.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c index 2621a34ee5c6..65bd9e69faf2 100644 --- a/drivers/iio/adc/ina2xx-adc.c +++ b/drivers/iio/adc/ina2xx-adc.c @@ -703,10 +703,10 @@ static int ina2xx_work_buffer(struct iio_dev *indio_dev) /* data buffer needs space for channel data and timestap */ unsigned short data[4 + sizeof(s64)/sizeof(short)]; int bit, ret, i = 0; - s64 time_a, time_b; + s64 time; unsigned int alert; - time_a = iio_get_time_ns(indio_dev); + time = iio_get_time_ns(indio_dev); /* * Because the timer thread and the chip conversion clock @@ -752,11 +752,9 @@ static int ina2xx_work_buffer(struct iio_dev *indio_dev) data[i++] = val; } - time_b = iio_get_time_ns(indio_dev); + iio_push_to_buffers_with_timestamp(indio_dev, data, time); - iio_push_to_buffers_with_timestamp(indio_dev, data, time_a); - - return (unsigned long)(time_b - time_a) / 1000; + return 0; }; static int ina2xx_capture_thread(void *data) @@ -764,7 +762,9 @@ static int ina2xx_capture_thread(void *data) struct iio_dev *indio_dev = data; struct ina2xx_chip_info *chip = iio_priv(indio_dev); int sampling_us = SAMPLING_PERIOD(chip); - int buffer_us, delay_us; + int ret; + struct timespec64 next, now, delta; + s64 delay_us; /* * Poll a bit faster than the chip internal Fs, in case @@ -773,15 +773,22 @@ static int ina2xx_capture_thread(void *data) if (!chip->allow_async_readout) sampling_us -= 200; + ktime_get_ts64(&next); + do { - buffer_us = ina2xx_work_buffer(indio_dev); - if (buffer_us < 0) - return buffer_us; + ret = ina2xx_work_buffer(indio_dev); + if (ret < 0) + return ret; - if (sampling_us > buffer_us) { - delay_us = sampling_us - buffer_us; - usleep_range(delay_us, (delay_us * 3) >> 1); - } + ktime_get_ts64(&now); + + do { + timespec64_add_ns(&next, 1000 * sampling_us); + delta = timespec64_sub(next, now); + delay_us = timespec64_to_ns(&delta) / 1000; + } while (delay_us <= 0); + + usleep_range(delay_us, (delay_us * 3) >> 1); } while (!kthread_should_stop());