From patchwork Mon Sep 9 12:37:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Klinger X-Patchwork-Id: 11137869 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 049EB14DB for ; Mon, 9 Sep 2019 12:37:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DF39D218DE for ; Mon, 9 Sep 2019 12:37:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727830AbfIIMhs (ORCPT ); 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-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) X-Provags-ID: V03:K1:Eojq1HDVaeAwkrHQiQU/HLlmOLn2Cs9VBHIwNAYCN2QsjHgH1hA vaZtKSjN3Nc2z6bUFFl+3Mg8tYqy7dq7fqIkugeNIOL2adp3Sf8QHG9KbLsyd7A0KKGQpHu EKdfW+xCKtdZEkmYiZjvQ1yg0mXNlYI7vdhI7odgWSFQmKjTsaLmgdRwarkPYwEqrpz7xbU MldZ4TA1ZRrELqRX87+IA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:oi5vBwwBnbs=:ehiJoCvILHNBRnyYrxWn0t TmzNlSURrTNYRVdWzt95GAL8n0Psr1mXBgS+o9fq5ZCAbJsX026gP3woIVN7RzjO55b4TsKNv 1o5gSufRt6/RLaxbzREATLOJTLvP/diwEAAkixxlbUlGOgRewg5lfpRGkoZAackDBudeCbzpT Zt5ebXC/a9fk/SSrJmczZxkUBakNSlWvXJ0i8/bt9/vFfFBG2ZNb9AIz9EJrdnRQvb8bH9hE4 pqUCweKAtM7KVjATxaOlnWbB/2P6S+YquKLwWU4MLh+vGgaADrSlAlohuohyIFtClRs3JF+rA uflXakY4qHXa8JzlQDJTiMxGQNzENk0x+e1wu+GFFibYlT4ogOt3UbjNC1JG9VgOADt58x4jX oANsAADz38oiQK+Is/WTsI0BI69RyWRbKPftS7ay+i6QI3HLCE2ToyBoiDBqcY4UAEUwqQ0ox Rbx0Q0WgoxMzLVMRfVX0PkXntiqwVgQ6MqSvraW0LZa6HcjlVJrp4d6cSSjVdRTw3opnwHSqv TJuCG55HOvwM4KQABBJtMI8Z2CAVfsCrr5IjQgi+HSwYPMgI/SiWYPhmB+2rBxoHdBl4clIko 0Ne8kLcpGN8cvddLjPnSZ6fx9UNkPYYG6xy8+tyEs+IWIbwP7/0ObLGHdsDmfv2MPSqhQ15f7 8xgoIp/f+vREjMnUkR1DlA0gMV8fsEBZ+QEocV7EsaERjENKWzNKrrJjo1BB2dLbbZztlNSs3 lS66yQlTXwh9upSYbC+ltm0iAnOEtLNWQ+r6fOJnJPKnMtHoxewx+fK7cMk1RYIySV9zN9G+m miuUly2VPk9Mb6w2ctYRyerfZiWBjzNMTqgbTKj2MWmP8mPRkY= Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@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) From patchwork Mon Sep 9 12:37:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Klinger X-Patchwork-Id: 11137871 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BBE1D16B1 for ; Mon, 9 Sep 2019 12:38:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A2ACD218DE for ; Mon, 9 Sep 2019 12:38:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728180AbfIIMiI (ORCPT ); Mon, 9 Sep 2019 08:38:08 -0400 Received: from mout.kundenserver.de ([212.227.126.131]:35179 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727810AbfIIMiH (ORCPT ); Mon, 9 Sep 2019 08:38:07 -0400 Received: from localhost ([46.78.15.232]) by mrelayeu.kundenserver.de (mreue010 [212.227.15.167]) with ESMTPSA (Nemesis) id 1M27Bp-1i9mpb45bV-002a2d; Mon, 09 Sep 2019 14:37:51 +0200 Date: Mon, 9 Sep 2019 14:37:48 +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 2/3] iio: adc: hx711: optimize performance in read cycle Message-ID: <20190909123746.lvd2q3dwgaksktuy@arbad> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) X-Provags-ID: V03:K1:2r6WQaikpwqG7k81U+l+sZ8AUOML4uXyn3ufomrEmSckU9Rlh61 kmF07rREvzVOdRFAjr/o0h4Vh6yFmhKHJkCYKf3ZNzkWAyK5QcQCpeevDbsQH4MON5fJnAE 44B9V2N9R1hrSf7hbtfxNfYFi1JV0j4tP9mfUJaGYJ298sLcgWSHSBpm4zzOTKA0ZxvZBsW x4ukdQYbYl+qmlALvdQiA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:Vr+21FCK1aA=:ikqRx4OngkgZ0qB7j8VmzP FFPQiCoA6Z4iTI27Pd9EZsI55lNmMD4afUPRns5ubL9z1dD/yeL0OglGNDmrJdPpuC7ZX1gfM NdDmxzigmOtBKIIUrqQiXetWWCC0xz4invZD1jS1HXl/LONXqv4JAC5xw+BYjQMlotjIGCsWY tunsDD2iM01S/ovrE3S2eZkuqzK9EJSvL5zv9GEBjNZyF7ebq0DyvPbXi3ViJp/cb/eAlnHbw ucrE5ISiv2R0SPczCn91UVzhdG7KENYc5jhJ3fISEBgFgYRA7GQHuY9JE+d7sB7wRI8XG+Xef TrfPgRSKri4H9Uncy6NLt4FfmqHQf/IzjxzEwmb2Y1FPINasV2kf0IcpwiooEjFq8/gbOgk/d Pi0299ORDn+vJUP1ry73EtjqsiCVUDzEK80zVhxyDsSy7mxjqYQF53iD8I3SDMjhy45x+9ue5 3IA1J1jtj1OHtnw8uJwSMNlX0LaX4WQhYtqMc2wHEaHMDFIeobhDa/I4TlayK7AaB1yXNt1yA b1GBjVwvF8TDmGTt8rCEFBnYDe6v/dZWmKNocbnOh7KnrzsPTL5Yc/7wHfTRe2Zq00vAmAYF2 KbcDa91BEg+Q2uXt8Z8rIVjsl/oAYBMBK+19gU4RBwS/C9IpQxNvu+TiDCUtBYsORloZgLu8r 06u0yLL7AwVU4dG8Ex6LmAS0YN5QrvK+OLUhs9+F6no+DW/yCY5Y+KAvxauMvoCng+XF/cjsk 2RCf5bsfKrbGuZ+8YmH/e0sGsqYnzc40xBdluU34840xROLEStNDS/zBMHM+Ad3poYaJx309N TgtfAwacLsqyABlk2TL0Ht5qYbmgKY2FBDjWm5hj/Qr3z5lgzY= Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Set gain in hx711_reset() to its default value after a reset cycle. This omits one precautionary read cycle, because the read is performed in hx711_set_gain_for_channel() anyway if gain has changed. Check for DOUT low and if its high wait some time if it goes down instead of doing a blind reset cycle when DOUT is not down. This is a performance optimization which allows to query the sensor with a higher frequency. Signed-off-by: Andreas Klinger --- drivers/iio/adc/hx711.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c index 0678964dbd21..c8686558429b 100644 --- a/drivers/iio/adc/hx711.c +++ b/drivers/iio/adc/hx711.c @@ -23,6 +23,7 @@ /* gain to pulse and scale conversion */ #define HX711_GAIN_MAX 3 +#define HX711_RESET_GAIN 128 struct hx711_gain_to_scale { int gain; @@ -100,7 +101,6 @@ struct hx711_data { static int hx711_cycle(struct hx711_data *hx711_data) { - int val; unsigned long flags; /* @@ -186,8 +186,7 @@ static int hx711_wait_for_ready(struct hx711_data *hx711_data) static int hx711_reset(struct hx711_data *hx711_data) { - int ret; - int val = gpiod_get_value(hx711_data->gpiod_dout); + int val = hx711_wait_for_ready(hx711_data); if (val) { /* @@ -203,22 +202,10 @@ static int hx711_reset(struct hx711_data *hx711_data) msleep(10); gpiod_set_value(hx711_data->gpiod_pd_sck, 0); - ret = hx711_wait_for_ready(hx711_data); - if (ret) - return ret; - /* - * after a reset the gain is 128 so we do a dummy read - * to set the gain for the next read - */ - ret = hx711_read(hx711_data); - if (ret < 0) - return ret; - - /* - * after a dummy read we need to wait vor readiness - * for not mixing gain pulses with the clock - */ val = hx711_wait_for_ready(hx711_data); + + /* after a reset the gain is 128 */ + hx711_data->gain_set = HX711_RESET_GAIN; } return val; From patchwork Mon Sep 9 12:38:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Klinger X-Patchwork-Id: 11137873 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8F04516B1 for ; Mon, 9 Sep 2019 12:38:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 75E92218DE for ; Mon, 9 Sep 2019 12:38:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727925AbfIIMic (ORCPT ); Mon, 9 Sep 2019 08:38:32 -0400 Received: from mout.kundenserver.de ([212.227.126.134]:45625 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727967AbfIIMib (ORCPT ); Mon, 9 Sep 2019 08:38:31 -0400 Received: from localhost ([46.78.15.232]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MsqMq-1iRBEX2R1k-00tA70; Mon, 09 Sep 2019 14:38:09 +0200 Date: Mon, 9 Sep 2019 14:38:08 +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 3/3] iio: adc: hx711: remove unnecessary returns Message-ID: <20190909123806.ewrxc7y5gmh5plmk@arbad> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) X-Provags-ID: V03:K1:sYDxRPyTH6C963S9/gAb7OfPBbNXhNeJ1VYf7nec78X5l8tEuYz 89GJ3GsRA3UGY+lHLXgvDVrblbvhsuMpk3tdhjGV9YYmscCDr8C/3RS7cLhaMzcg0CrVjU3 1oHubHyyDAvunKH+hjjY1KGERUiW3P7AOfzdjn7CpafTqTJMUl4h82npocnVxPkqQ/cNkQm v8N1sbOksdpuhhrvbnr0A== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:D1A/xcjwmhE=:2hJiZpX/Ug+t4LH3e2ULTW QW6BrjPYoO7hKtZuenuHvoBcsy6l9Bpwf4CW4ZlBjTUr+JVP+jcCHzQZjEmEPFtASvjAq8o7r pvR3Z5Ts93bNTsbC0sfIE3KQs2tN5w1xTyOSG19Tj0D3/VBEXTcBXYt190/gzElSbNxrVP7dE C/Xf5o5HviXcVwknJWXeZHKuDaIihd+BO4E/1vx6Sc5Y0lN6X5gG+3/6vfvJPmOB6+h01rcnN 1bH3MZIUM/RJKMGS/+4YQ3RtepBvxIxc4yhYNRJqigcH5d9ZvvOPNAPzGog+6HDAJyCe4+LKo vZJgs5MHfC219paPDGIdsto6xdx/KB3UtknvevYtSw0khpt/mZtHNSr635sOAYIVqNp8B8xBi sGWzdDO2eCGczM8U2i62u2olujLErvkTf9/A+LVEfYfsLHNKdnrKc9hxpQMUumHVvQ1Z9PpNb 7hXdmk9SrHu9wfOcYWHvO2+jR/u7Z2K7/LFgUI3nT8ev74Sh0oAXaD9M7FS4PU+V48f5i1I98 HVukkycewsEv07Reh34/BBZ/F//TLPzFzX3oP2p2CQ91Nc4eNvZtz3YndLwuJVhOkrxhX2Iif IAgYg6ktZRYEEcoJVMMa6xqUt6QbnBjgCKpHLN0obphipe1eDX6R8xOyg4gHAHHVjuUpO0zZH seH3ZSUkw/eNtamEzfH1x0FvObNIuoxRTiDau171iIE+8UOHKQQYCayTfWM1OvDjEdFVX/EXM b/u71vAG3p48x7mEfIlzH99dF39Iv8fJ3Pztck/mtc2RpGiKoieeFX9gcEzujGG/FJLhgO+d8 4RI6bWnX0LYYSWPrTosoAxaS4yxLQ== Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Optimize use of return in hx711_set_gain_for_channel(). Signed-off-by: Andreas Klinger --- drivers/iio/adc/hx711.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c index c8686558429b..20c249f502c0 100644 --- a/drivers/iio/adc/hx711.c +++ b/drivers/iio/adc/hx711.c @@ -213,7 +213,7 @@ static int hx711_reset(struct hx711_data *hx711_data) static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan) { - int ret; + int ret = 0; if (chan == 0) { if (hx711_data->gain_set == 32) { @@ -224,8 +224,6 @@ static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan) return ret; ret = hx711_wait_for_ready(hx711_data); - if (ret) - return ret; } } else { if (hx711_data->gain_set != 32) { @@ -236,12 +234,10 @@ static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan) return ret; ret = hx711_wait_for_ready(hx711_data); - if (ret) - return ret; } } - return 0; + return ret; } static int hx711_reset_read(struct hx711_data *hx711_data, int chan)