Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp1490319iol; Fri, 10 Jun 2022 08:31:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweTscb8lVyiYea695rlS5egZmrDEK+R00uODAhVjqgBAFOAAN+PP1nG+2PjDMtGcMY1r1Z X-Received: by 2002:a05:6402:5409:b0:42a:a643:4eb8 with SMTP id ev9-20020a056402540900b0042aa6434eb8mr52636719edb.71.1654875074177; Fri, 10 Jun 2022 08:31:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654875074; cv=none; d=google.com; s=arc-20160816; b=zzx8S967MnN3fYxra3WIgC3L9zN7y5+wbTqwwdO6rdG5/eB+ZJakjf3O0/gUKbx+dJ xRpy2jzWFBiVT9hSyV0ED5mpUtamyboJSwUvRKKHrG+LEUM9bwfje4mmmBgGlqeVqCTU pUwD3JU7EPVP7yMGGoj0tSog/sQMy60Cxth15XvPqE/MXIMb59QdW2GIH9pJXVCWoJ7k WJH1pbAgTMm9XWERPircFQzFuGe/z0F4YR2qV0BXntP3/Ii7+Avl1StXvZlFOs0IEe1k jbjAhIY3TtsctP/NsxK/cJMNtq1LbxVSTPhT9u+7nsWvaos/lK1geQ2sEfHbDk4wEKD7 8B5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=jyCd7hBSL9GiEfMM7xgKz0XL3YQAl24XQwd7lRWBYUQ=; b=U8XlOis0Ozc/521vSDQOfl6KMjD1fGgsrnsH9WptowkNL/hP2K0h/Dclfv+I08H/k3 htEePvXYODlCFSJd9OSplWe80g24apLFN+1SuLstBUiKFpX+10bGX7eftMkc99ogYAR0 8nlzaaf24lBzp8B/NngoMVP5Dxh5snZBcGkj5ZXCs9Ts5TrsQ1WzadEhxmskOYZ2DU0K QF08I4M97qb6jcr9llSN9luiLAW988uV4yTlH+MIUiQj5dadcpkYKPwRW4b8udO711Sf MCyD8f7R4SqA/DzFN8CKnMCSP8rnsv24cpvXqRZ5wvFBWnWnU0hf5ctJGwLMfjumJ11g zAsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RZiqXB8w; 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 hs6-20020a1709073e8600b006fe8e64d480si10100085ejc.701.2022.06.10.08.30.35; Fri, 10 Jun 2022 08:31:14 -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=RZiqXB8w; 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 S1349428AbiFJOhr (ORCPT + 99 others); Fri, 10 Jun 2022 10:37:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234139AbiFJOhp (ORCPT ); Fri, 10 Jun 2022 10:37:45 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B04FF1FCE7; Fri, 10 Jun 2022 07:37:43 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id v1so43176803ejg.13; Fri, 10 Jun 2022 07:37:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jyCd7hBSL9GiEfMM7xgKz0XL3YQAl24XQwd7lRWBYUQ=; b=RZiqXB8w47FlzcjDL0OseLBYQPSrZJVy1WC/I0f1PTxVTjduz2Udq4JpxOs0DDYTxk DVe3lcRUPcioBmos3p0VOFkXpUvoLUYMZrrpeqbqNecxsRtTmgMUmXilGCGDt7de5OXJ U71OHgZUn0R+uNHNpapEDo12/8HN9pZqoKm4oLzE5JbIjatgglCxN92hFq4eQZ7De4X1 TT3/GBsVIIQbCZzw3dk4o+uOXXRbafFAnqrOnfkNQt5A+xXGBtWiGl7pMpPQYgxzPIuu +1wwR3Afp/VVb3NnFbQzxvMGNKRnHwRLaUqM9+P/gmx1MqU3cRAZ6L4yn35bvuXa8pS8 t+8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jyCd7hBSL9GiEfMM7xgKz0XL3YQAl24XQwd7lRWBYUQ=; b=A78/hX9M5f3WqNKPJdQFLhqM1x+fne8FLY3k+iWSr8wJEzznpcFAC6n1amR+G2cPHZ /3f+k67ztjS3stVQmXvSWfToPnSK5mGbgBETwjnsVq6vleM7qxz8Q75pWC7pMh0Opppg aOSsghpeRr0c87yrIaeI73JyUvr1i0TYcrvFYIp4fNeulhw21y72WgkJkyK1m/Plfsdk GBSAOCbmdNXtH71IQsXFLcBJYBNeBXHIYgM9g/YwCcz1wzZbRZlhUZ5swHHh77OElnll jF/y3cH5rejYfePzlUxP7oxAchIKkIxdIJzUpfM9Y2hg0i/Y3cR6EcdgfI5yrMf+tA1R JjAw== X-Gm-Message-State: AOAM531MtNMYhIVIZDYzIhOcIUAR1BQjvBa2ISrMNwkteNkhSDAziYKa gyPGs1ICj96OexKAIqn08dytD4yM7dHui7aDqSM= X-Received: by 2002:a17:906:d550:b0:704:7ba6:9854 with SMTP id cr16-20020a170906d55000b007047ba69854mr40247496ejc.579.1654871862288; Fri, 10 Jun 2022 07:37:42 -0700 (PDT) MIME-Version: 1.0 References: <20220610053012.27279-1-xiaohuizhang@ruc.edu.cn> In-Reply-To: <20220610053012.27279-1-xiaohuizhang@ruc.edu.cn> From: Andy Shevchenko Date: Fri, 10 Jun 2022 16:37:05 +0200 Message-ID: Subject: Re: [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write To: Xiaohui Zhang Cc: Jonathan Cameron , Lars-Peter Clausen , Gwendal Grignou , Stephen Boyd , Jongpil Jung , linux-iio , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" 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,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 On Fri, Jun 10, 2022 at 7:53 AM Xiaohui Zhang wrote: > > Similar to the handling of read/write in commit 108e4d4de2b5 > ("iio:proximity:sx9324: Fix hardware gain read/write"), we thought > a patch might be needed here as well. > > There are four possible gain values according to 'sx9360_gain_vals[]': > > 1, 2, 4, and 8 > > The values are off by one when writing and reading the register. The > bits should be set according to this equation: > > ilog2() + 1 > > so that a gain of 8 is 0x4 in the register field and a gain of 4 is 0x3 > in the register field, etc. Note that a gain of 0 is reserved per the > datasheet. The default gain (SX9360_REG_PROX_CTRL0_GAIN_1) is also > wrong. It should be 0x1 << 3, i.e. 0x8, not 0x80 which is setting the > reserved bit 7. > > Fix this all up to properly handle the hardware gain and return errors > for invalid settings. ... > + regval = FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval); > + if (regval) > + regval--; > + else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD || > + regval > SX9360_REG_PROX_CTRL0_GAIN_8) else?! Isn't it a dead code? How has it been tested? > + return -EINVAL; > + *val = 1 << regval; Even in the original code this is wrong in accordance with C standard. It might have potentially UB. BIT(), for example, solves this issue. You may do what it does under the hood. -- With Best Regards, Andy Shevchenko