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 4F9E6C61DA4 for ; Mon, 6 Mar 2023 12:21:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230272AbjCFMVI (ORCPT ); Mon, 6 Mar 2023 07:21:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229483AbjCFMVF (ORCPT ); Mon, 6 Mar 2023 07:21:05 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C5FC28D18; Mon, 6 Mar 2023 04:21:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678105261; x=1709641261; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=+muziHGQBV++mc4VwRxt7YSdosdi2ZYRIOhBBzVrxfQ=; b=ApiEmR7t0nael0ko8HGcUIR9BRB1YzhvMP7eYrb8FciiQVd9AfCrruqv FgiACabVT5rgRxI7XOT5nQcumJXox11tfdNOKV4gpt9pQ6Px2wm/dZyml IFKrVzJm3D2V3kjejZCQHUJrPpaIhDSRq7iAHRoBHYSRVzR2+fIS+mxf5 E7bXlmjltiJzeWSNcUGKFz0zSoAogFY4bebZH9ZbUzbpcsj2Mk+SOn2Bf /zlH8VEd2RYuZezVocBm/5pIJ1wK3XCf7yyqo6jz7Yl+Ju4LKuLstgp+S jGMCxBsbxTMlbKV15/UuLGjhTR/kcw54kNS24MeSPEI5MX0n9JIq5wJVC Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="400360986" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="400360986" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 04:21:01 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="626139989" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="626139989" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga003.jf.intel.com with ESMTP; 06 Mar 2023 04:20:57 -0800 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1pZ9pn-00GLLj-1O; Mon, 06 Mar 2023 14:20:55 +0200 Date: Mon, 6 Mar 2023 14:20:55 +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:16:24PM +0200, Andy Shevchenko wrote: > 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; Just a side note. I have been wanting to have is_in_range() for a long time. Perhaps we can add a such to the kernel (math.h) /* Check if in the range [start .. end] */ #define is_in_range(value, start, end) (value >= start && value <= end) With it, the above will probably better look if (!is_in_range(gain, BIT(0), BIT(3) || !is_power_of_2(gain)) > 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