Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp6076461ybe; Tue, 10 Sep 2019 13:09:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqxtx0KXYruz6mv/VDW6/YnR6z3lzZbKE6hmRa3LpzfB83uMPee2TvPWxPmbrUiogqpRiNBv X-Received: by 2002:a50:f38b:: with SMTP id g11mr33325909edm.292.1568146161617; Tue, 10 Sep 2019 13:09:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568146161; cv=none; d=google.com; s=arc-20160816; b=upSJWb2YFW13rJe/FDiUl8T8GMv0y33IOf00/JuwWHC4ChV5xWoly19HCRMuhI/ZDc ZAnUetQUnsWfSerAPPSRqo44YeVd0g4p1Cnv8p+CRMy5YxiNUOYFnIk5HnbpXjiKU3tP S6iSYq6NT3z8h1UunZFwpfflUKXZ2HXBDfoeZolToyE2NBqr+IrzTxi8KdjYn3PAmlf/ DFFVSz0fBHQ31cheqpjuDmeLqHm7UNLqmdoSuq2flsA7SLgb0kkNxOwRAQGAnKUeqWO8 IhnIvqTkwK9k7zOuRgzx86zRQ6cBp9ZXiQgCfuYZIfkXm4LAPU1Sl/cy06ztMUC5i6Dj Kliw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date; bh=5FbxXvqcAuIBsZ9ADykXdszT9ZaTw/ALgOedkM6gSKk=; b=fiNOyqOoCqqvrE7AbvK7bhRgI2ek7qww4FkNZdASlelG7DARqr3tIbiM5baR6kB7jj 7ODRFDRjfC9Pv7aOfO1vvPbdolcrZcf0b29d73ARLDtha++yxB3mOZAMZptc/4wxyp9y 08aQn3zEXVonSp7+uAL0biV/H6GLNQ8pbX0G9HqLpkXmb2BFs8tr8oqkg1lyYraMAmLd GCWvPyL4OIbjfcTiPzNNnHWStqfb8JGakBg+xmFhpORY9/2mhOND+vZdAwVRAk/8T9R6 cyNJLJS/PAbzKwt9mBQpbhBD2m/wXoO8UumdAVh5zeBvpiiOz3fIcNYHk481y6ffQo0r QNpQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id qp23si10256470ejb.39.2019.09.10.13.08.56; Tue, 10 Sep 2019 13:09:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728154AbfIIMhs (ORCPT + 99 others); Mon, 9 Sep 2019 08:37:48 -0400 Received: from mout.kundenserver.de ([212.227.126.187]:43601 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727629AbfIIMhs (ORCPT ); Mon, 9 Sep 2019 08:37:48 -0400 Received: from localhost ([46.78.15.232]) by mrelayeu.kundenserver.de (mreue009 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MplPh-1iUryj3KQv-00q8v8; Mon, 09 Sep 2019 14:37:24 +0200 Date: Mon, 9 Sep 2019 14:37:21 +0200 From: Andreas Klinger To: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] iio: adc: hx711: fix bug in sampling of data Message-ID: <20190909123720.qdew2c4evxd6dpyk@arbad> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) X-Provags-ID: V03:K1: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nJPKnMtHoxewx+fK7cMk1RYIySV9zN9G+m miuUly2VPk9Mb6w2ctYRyerfZiWBjzNMTqgbTKj2MWmP8mPRkY= Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix bug in sampling function hx711_cycle() when interrupt occures while PD_SCK is high. If PD_SCK is high for at least 60 us power down mode of the sensor is entered which in turn leads to a wrong measurement. Switch off interrupts during a PD_SCK high period and move query of DOUT to the latest point of time which is at the end of PD_SCK low period. This bug exists in the driver since it's initial addition. The more interrupts on the system the higher is the probability that it happens. Fixes: c3b2fdd0ea7e ("iio: adc: hx711: Add IIO driver for AVIA HX711") Signed-off-by: Andreas Klinger --- drivers/iio/adc/hx711.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c index 88c7fe15003b..0678964dbd21 100644 --- a/drivers/iio/adc/hx711.c +++ b/drivers/iio/adc/hx711.c @@ -101,13 +101,14 @@ struct hx711_data { static int hx711_cycle(struct hx711_data *hx711_data) { int val; + unsigned long flags; /* * if preempted for more then 60us while PD_SCK is high: * hx711 is going in reset * ==> measuring is false */ - preempt_disable(); + local_irq_save(flags); gpiod_set_value(hx711_data->gpiod_pd_sck, 1); /* @@ -117,7 +118,6 @@ static int hx711_cycle(struct hx711_data *hx711_data) */ ndelay(hx711_data->data_ready_delay_ns); - val = gpiod_get_value(hx711_data->gpiod_dout); /* * here we are not waiting for 0.2 us as suggested by the datasheet, * because the oscilloscope showed in a test scenario @@ -125,7 +125,7 @@ static int hx711_cycle(struct hx711_data *hx711_data) * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz */ gpiod_set_value(hx711_data->gpiod_pd_sck, 0); - preempt_enable(); + local_irq_restore(flags); /* * make it a square wave for addressing cases with capacitance on @@ -133,7 +133,8 @@ static int hx711_cycle(struct hx711_data *hx711_data) */ ndelay(hx711_data->data_ready_delay_ns); - return val; + /* sample as late as possible */ + return gpiod_get_value(hx711_data->gpiod_dout); } static int hx711_read(struct hx711_data *hx711_data) -- 2.11.0