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 80294C61DA4 for ; Mon, 6 Mar 2023 12:16:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230256AbjCFMQd (ORCPT ); Mon, 6 Mar 2023 07:16:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229977AbjCFMQa (ORCPT ); Mon, 6 Mar 2023 07:16:30 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23DF020579; Mon, 6 Mar 2023 04:16:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678104990; x=1709640990; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=PuPDornh1OEsP3yzAZOWoqW4lUda0/ttH3RpXrbjS8s=; b=PygDbpHWIPxWSyE8IvXWrpvUkiZ2qYqLNhVQalvcTExejMAl/hzX2nyo IGaV+pk7uurgm+DIdOtWtM2KiVnUEug8bU8aAdadtdj/nIx5uEetgMHEA l+pVdeqL/y90C/b7cMJJf4HSC7rTnA1DNxtnYjXeRchIL/M06SK+EJWt9 9VpsD8xGFxsSAKp7jsHbQDPmGR5JXXcagLujdJbjXSa0G0M2XD3rWnOxK 13RUexfez0b8CzYRVGf+oDrrgGaMXyU4wBTPAqTjwE7O7xt27eqZpMkuK o4rdI994UCUOIB/tp1oZsqgcr8Jcl9I99SONT2oc6fu5f2bnIrAfQpeOf w==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="335560376" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="335560376" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 04:16:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="708633313" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="708633313" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga001.jf.intel.com with ESMTP; 06 Mar 2023 04:16:26 -0800 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1pZ9lQ-00GLG9-1E; Mon, 06 Mar 2023 14:16:24 +0200 Date: Mon, 6 Mar 2023 14:16:24 +0200 From: Andy Shevchenko To: Jonathan Cameron 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: References: <20230228063151.17598-1-mike.looijmans@topic.nl> <20230228063151.17598-2-mike.looijmans@topic.nl> <20230304175751.2daae308@jic23-huawei> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 06, 2023 at 02:11:48PM +0200, Andy Shevchenko wrote: > On Sat, Mar 04, 2023 at 05:57:51PM +0000, Jonathan Cameron wrote: > > On Tue, 28 Feb 2023 07:31:51 +0100 > > Mike Looijmans wrote: ... > > > + for (i = 0; i < 4; i++) { > > > + if (BIT(i) == gain) { > > > + ads1100_set_config_bits(data, ADS1100_PGA_MASK, i); > > > + return 0; > > > + } > > > + } > > Andy's suggestion of something like.. > > if (!gain) > > return -EINVAL; > > i = ffs(gain); > > if (i >= 4 || BIT(i) != gain) > > return -EINVAL; > > > > ads... > > > > Is perhaps nicer than the loop. > > Even better: > > if (!gain || !is_power_of_2(gain)) > return -EINVAL; Or if you want to combine all checks: if (clamp_val(gain, BIT(0), BIT(3)) != gain || !is_power_of_2(gain)) return -EINVAL; ads1100_set_config_bits(data, ADS1100_PGA_MASK, ffs(gain)); return 0; (You can play with bloat-o-meter for the code generation and see which one is better from that aspect) -- With Best Regards, Andy Shevchenko