From patchwork Sun May 21 22:59:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artur Rojek X-Patchwork-Id: 96942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1093511vqo; Sun, 21 May 2023 16:07:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Uk1GnlGJ3NVEWE5vAt9C9ld/0LK+QTIGWCNie2b8oMioknNYxD8cq4XUjx0mrCUNz/5rw X-Received: by 2002:a05:6a20:a120:b0:103:7b79:1506 with SMTP id q32-20020a056a20a12000b001037b791506mr9769008pzk.24.1684710443538; Sun, 21 May 2023 16:07:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684710443; cv=none; d=google.com; s=arc-20160816; b=UgK/KpzPD6tzaDErDhigfxhzi+Q1SztP+gHiazdR7SpeIbl4Y9phuocjD9UJdr9bAl 74PT0aJ4mHKhtm7OlK7x6Ao8LBe4uNcxjVODFzC2oXe9ZRPB3ekCqAmcmInwb5EaYtbJ 1mVj0wB7Lve8DVY7Wttyf6DrEvWhRynMNjUDIexABogTKLH5cfvv4CJ9aTTpBzdEC1Sm paPkU2Ync1QibYGBbpGPlbDpeKZBWm9Uw843BgodH9ZudZxESv68NHe0u/GqrdFDPUZY 5x1zK1doqoQI7xOFxzKdmtt99RyUF9L4XGeBMxJcmz1uqK0lU8Rymtr5gsucNMp5SjpM DrOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=LrB+fvPvvZ3r2mY8RpmQNKLa3SATQwbfcD3rDBy5psc=; b=dKxEjmqgYWiVD+DCEBbIpbHKLwirURLRoCOucZg4VHMg7p4acr8nax+Ec6JMVBQ+4m IfCYqFICwhIvATNCj0z9JFEiXP6Ix05ICiYW4WRrjYg241JbFRRAFFMRt/RFTcmbzKUX xLC3ptxyrsqYXzA63S1z2lMnPdTRyz9LogM6/kXBO8VwwQjD2kM85VnNTC3UflPSpOkb OwJj5SwOnfiCl1WvYUKoInCCG/StgXM9M/5oh++1tDo9xLltBgZYR6sRuduwlhiBTFV9 yXS5e+YUNJEX48hZ8Pj7IYOU4Enof1mMGAHtt6aFZTfvq/beSyI0KXEZk1pirBk8Dnkd JlAQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o21-20020a637315000000b00527b90936b8si3769279pgc.38.2023.05.21.16.07.11; Sun, 21 May 2023 16:07:23 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231528AbjEUW74 (ORCPT + 99 others); Sun, 21 May 2023 18:59:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231329AbjEUW7u (ORCPT ); Sun, 21 May 2023 18:59:50 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B36CBBE; Sun, 21 May 2023 15:59:48 -0700 (PDT) Received: (Authenticated sender: contact@artur-rojek.eu) by mail.gandi.net (Postfix) with ESMTPSA id 6424B1C0003; Sun, 21 May 2023 22:59:45 +0000 (UTC) From: Artur Rojek To: Paul Cercueil , Jonathan Cameron , Dmitry Torokhov , Chris Morgan , Andy Shevchenko Cc: linux-mips@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Artur Rojek Subject: [PATCH v2 1/2] iio/adc: ingenic: Fix channel offsets in buffer Date: Mon, 22 May 2023 00:59:00 +0200 Message-Id: <20230521225901.388455-2-contact@artur-rojek.eu> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230521225901.388455-1-contact@artur-rojek.eu> References: <20230521225901.388455-1-contact@artur-rojek.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766546938365455894?= X-GMAIL-MSGID: =?utf-8?q?1766546938365455894?= Consumers expect the buffer to only contain enabled channels. While preparing the buffer, the driver makes two mistakes: 1) It inserts empty data for disabled channels. 2) Each ADC readout contains samples for two 16-bit channels. If either of them is active, the whole 32-bit sample is pushed into the buffer as-is. Both of those issues cause the active channels to appear at the wrong offsets in the buffer. Fix the above by demuxing samples for active channels only. This has remained unnoticed, as all the consumers so far were only using channels 0 and 1, leaving them unaffected by changes introduced in this commit. Signed-off-by: Artur Rojek Tested-by: Paul Cercueil --- v2: - demux active channels from ADC readouts - clarify in the commit description that this patch doesn't impact existing consumers of this driver drivers/iio/adc/ingenic-adc.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/drivers/iio/adc/ingenic-adc.c b/drivers/iio/adc/ingenic-adc.c index a7325dbbb99a..093710a7ad4c 100644 --- a/drivers/iio/adc/ingenic-adc.c +++ b/drivers/iio/adc/ingenic-adc.c @@ -802,13 +802,19 @@ static irqreturn_t ingenic_adc_irq(int irq, void *data) struct ingenic_adc *adc = iio_priv(iio_dev); unsigned long mask = iio_dev->active_scan_mask[0]; unsigned int i; - u32 tdat[3]; - - for (i = 0; i < ARRAY_SIZE(tdat); mask >>= 2, i++) { - if (mask & 0x3) - tdat[i] = readl(adc->base + JZ_ADC_REG_ADTCH); - else - tdat[i] = 0; + u16 tdat[6]; + u32 val; + + memset(tdat, 0, ARRAY_SIZE(tdat)); + for (i = 0; mask && i < ARRAY_SIZE(tdat); mask >>= 2) { + if (mask & 0x3) { + val = readl(adc->base + JZ_ADC_REG_ADTCH); + /* Two channels per sample. Demux active. */ + if (mask & BIT(0)) + tdat[i++] = val & 0xffff; + if (mask & BIT(1)) + tdat[i++] = val >> 16; + } } iio_push_to_buffers(iio_dev, tdat);