Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83CF6C6FA9E for ; Sat, 4 Mar 2023 17:26:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229646AbjCDR0b (ORCPT ); Sat, 4 Mar 2023 12:26:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229447AbjCDR01 (ORCPT ); Sat, 4 Mar 2023 12:26:27 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 130B811148; Sat, 4 Mar 2023 09:26:26 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id C628DB802C4; Sat, 4 Mar 2023 17:26:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A240C433EF; Sat, 4 Mar 2023 17:26:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677950783; bh=KPYwvzh5duXtSKstrTopOnZQQL4PKi7YUvW2PwfHDEI=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=VesKVDDG2Tt1VV0/GQYKX9RWeYtV+5JXy3PYJgH9l4fdo+JR1Z3egSBZhKQBqqrrW RDE4JDNzgBP4ht90X/Ls0dAMb/WAOm/MvOQxPcTKZYT2ihoOkCyxAKPncY09D+pH1T T4Ax37OttA/ZLVulGLojHolIIiC4cQCrUWFuiYsND0chRKmU8iuAoPwCwgcFD2+ck7 fz6KBKdHNVWi6p/Li6BmQthlvi+G0kTjJYXiO9H/nuibWwh2lfKr4OMudz4pS4vIy0 RMTSgpJ4aAlQxS3rIR3/+BzZPRiNQ22UZjQcOY58kQY6jvTePiPcGOgFvqr+3UhWMG ZmtuFkGGllNpw== Date: Sat, 4 Mar 2023 17:26:18 +0000 From: Jonathan Cameron To: Andy Shevchenko Cc: Mike Looijmans , devicetree@vger.kernel.org, linux-iio@vger.kernel.org, Caleb Connolly , ChiYuan Huang , ChiaEn Wu , Cosmin Tanislav , Ibrahim Tilki , Lars-Peter Clausen , Ramona Bolboaca , William Breathitt Gray , linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 2/2] iio: adc: Add TI ADS1100 and ADS1000 Message-ID: <20230304172618.37f448d0@jic23-huawei> In-Reply-To: References: <20230228063151.17598-1-mike.looijmans@topic.nl> <20230228063151.17598-2-mike.looijmans@topic.nl> <1b153bce-a66a-45ee-a5c6-963ea6fb1c82.949ef384-8293-46b8-903f-40a477c056ae.0685d97e-4a28-499e-a9e3-3bafec126832@emailsignatures365.codetwo.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.37; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2 Mar 2023 16:23:14 +0200 Andy Shevchenko wrote: > On Thu, Mar 02, 2023 at 08:49:22AM +0100, Mike Looijmans wrote: > > On 01-03-2023 16:30, Andy Shevchenko wrote: =20 > > > On Tue, Feb 28, 2023 at 07:31:51AM +0100, Mike Looijmans wrote: =20 >=20 > ... >=20 > > > > + /* Shift result to compensate for bit resolution vs. sample rate = */ > > > > + value <<=3D 16 - ads1100_data_bits(data); > > > > + *val =3D sign_extend32(value, 15); =20 > > > Why not simply > > >=20 > > > *val =3D sign_extend32(value, ads1100_data_bits(data) - 1); > > >=20 > > > ? =20 > >=20 > > As discussed with=C2=A0 Jonathan Cameron, the register is right-justifi= ed and the > > number of bits depend on the data rate. Rather than having the "scale" > > change when the sample rate changes, we chose to adjust the sample resu= lt so > > it's always left-justified. =20 >=20 > Hmm... OK, but it adds unneeded code I think. There isn't a way to do it in one go that I can think of. The first statement is multiplying the value by a power of 2, not just sign= extending it. You could sign extend first then shift to do the multiply, but ends up same= amount of code. It does look a bit like a weird open coded sign extension though so I can s= ee where the confusion came from! >=20 > ... >=20 > > > > + for (i =3D 0; i < 4; i++) { > > > > + if (BIT(i) =3D=3D gain) { =20 > > > ffs()/__ffs() (look at the documentation for the difference and use p= roper one). =20 > >=20 > > Thought of it, but I'd rather have it return EINVAL for attempting to s= et > > the analog gain to "7" (0nly 1,2,4,8 allowed). =20 >=20 > I'm not sure what you are implying. >=20 > You have open coded something that has already to be a function which on = some > architectures become a single assembly instruction. >=20 > That said, drop your for-loop if-cond and use one of the proposed directl= y. > Then you may compare the result to what ever you want to be a limit and r= eturn > whatever error code you want to Agreed, could do it with appropriate ffs() followed by if (BIT(i) !=3D gain= ) return -EINVAL;