From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 04B73413D99 for ; Tue, 16 Jun 2026 10:02:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781604143; cv=none; b=KWpZPeFjZe+GNz28u2uhSXGreBBzQqWWQgVq7HO8mNXsrtAbnmJ0zXd+h2nH/MbelQA8JYAqWrUQZMi8IqxAlZHSj4bzTpwkbpxMccrH7HlVo+9tnHb1BOvLp9bnldaSTmYZl0Qd410xjO1kpagw7xKnr1zLZYCH2tvmnMSKSxI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781604143; c=relaxed/simple; bh=i6bosY+B4cjxEpF+3mF54+/6GhbEdLbvsBZzhRr77h4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=E6dSJPnkd+dKmKr2EWno74U8882O9TCzXuYQS5uEIra7h5UZO6fO+pAbU/Bn9VuQfDJUuDNqimxBNfyOsnzjJ6yC7Zv7jxtDXpOxKhCHlGSnIiXaP90C41gTupoxbv3XvTmTmzHvLhCj5Ht/mly0AQLkXmwIrws3zclW2ScbO7M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MzZ2qnph; arc=none smtp.client-ip=209.85.221.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MzZ2qnph" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-45ee5cdbd28so3220909f8f.1 for ; Tue, 16 Jun 2026 03:02:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781604140; x=1782208940; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=FBVbbNx8x7Z7B6UeBxppHM5awP8f4QZk9Bz01k7j7Ck=; b=MzZ2qnphnZqNYYwXMoNMy6nzQZ1lvgR+j3BQ0zONFNYBdNFEg7wdx5htUMPlyG5bjl lqnpXhdVjWkbn10wfaT7k4Gl+G6jCF+98JwcNbHzPKn/3Iqgj/+kUuI3ykHoFMcdFlhL ERUJA4onEBJZBqA0BQTsnI9t66WBl3F7xs9q5cVICOfOzWdYoL5rYYDYYlN/qi3gaDUF VsTDlsK8QccCEyEnYyr7g/Q6RB5vPmezAcm+mYmApjCWzrBYdDpCjrEJfQqGAMqO4i/S 8ze10wVYdHNDiTg+ld7LuBEpTaQW48L803OS3ImRQnxCk0bLJXn6RZIGgO71aohf+5ZY yT5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781604140; x=1782208940; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FBVbbNx8x7Z7B6UeBxppHM5awP8f4QZk9Bz01k7j7Ck=; b=hca2fC5O4v+flViXf6q3jLX2tIJ965db5GyNVehdiE1QKAJJ4Srqpu4Qcm1jd7Mbcm 0SffnjwqaDlV4AEo3QzCICCKVjWpvqe9cLFJXON0LLohSN5hMsYu6UHu0NF+sDrkpg5R +2at0OnBlEwYmo0srV3CSmNKW4yM39G4imakTrMc1EIRBKtTzv5Q5zD3j0ZSKZBknbn2 tedWPR2+x+c6Z6nE7RIPhtAgZYSnChf8Ps8a1k8MuUzykO/LUR8xJqigfLmjybGUTGkq cHJkCm6+M/xAqxGDW0fmioyGJfCZjC5BOrNUnKZuD8eG/5joyGuwkMZyd0VjMB8dV7uF QTYg== X-Forwarded-Encrypted: i=1; AFNElJ9dyii2PFo7LhelWeyz0vQclPyz/gGT4miACFw7A3ifAutBaiqZpkoLMKNlnPlKs7rflOw2YZOxXWahx5M=@vger.kernel.org X-Gm-Message-State: AOJu0YzxyFSc1S5EzWn3hL3AYfv/sx7nBHqiSiwtYZj/VE1MBhs5UhYR jA0AO5UHxE+aTF3OBZIMuouOdA3xce0X9BAx3i4kIrVxx2EJEThqPMrP X-Gm-Gg: Acq92OG6GHpyoOte1qksQSZ6/ZmQ0Lb42FYEjA2f6UAMo9qc/i0QyGUjHF2lAvLE48r ChYATDk9UUyu81Y7uDaP7grsNVqjPrA250OsLqgU6s140zdBLrqrbm001s29VDf+fAkmb/f9xqk u61XTFQlc4Alr9ti/ZWXN5CRbvTfm4FfX5tQkG72CxkXoTixvIMXRsPativZDGYxx3z0XOammI2 Uv2GYs4GJQquBah2PM4O4E6ax+aN5331923TD8+Cx+0aeFR/3dLbQcrcnkHYEskmfpKLaeAabZi gcKOxlj4AIuGw/burQxhOnsLnCRphaM+N8yctVKBb9rzQqOhG+K7qPjVuhK5iRQoXqhPNjiWcHI ekyktl4s31/T7PScZz7ZBdRSqxCik9VRzVmdnyvJqNpCxV7B60148A4ewnJBlbE17XJZELtSfGF hpAZeD1spYSwCekeU= X-Received: by 2002:a5d:688d:0:b0:460:1c93:6eb6 with SMTP id ffacd0b85a97d-4619f3133c6mr3730064f8f.20.1781604139935; Tue, 16 Jun 2026 03:02:19 -0700 (PDT) Received: from nsa ([148.63.225.166]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4606f2d933esm42184667f8f.33.2026.06.16.03.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jun 2026 03:02:19 -0700 (PDT) Date: Tue, 16 Jun 2026 11:03:20 +0100 From: Nuno =?utf-8?B?U8Oh?= To: Andy Shevchenko Cc: "David Lechner (TI)" , Jonathan Cameron , Nuno =?utf-8?B?U8Oh?= , Andy Shevchenko , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Kurt Borja , Nguyen Minh Tien , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/4] iio: adc: ti-ads112c14: implement gain on internal short SYS_MON channel Message-ID: References: <20260615-iio-adc-ti-ads122c14-v1-0-e6bdadf7cb2b@baylibre.com> <20260615-iio-adc-ti-ads122c14-v1-3-e6bdadf7cb2b@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Tue, Jun 16, 2026 at 10:58:46AM +0300, Andy Shevchenko wrote: > On Mon, Jun 15, 2026 at 05:00:01PM -0500, David Lechner (TI) wrote: > > Implement support for the programmable gain amplifier on the internal > > short SYS_MON channel. This channel is used for calibration, so it is > > useful to be able to set the PGA to the same gain as the external > > channels. The gain setting is implemented via the `_scale` attribute. > > > > In the future, we may want to support different reference voltages for > > this channel, so the scale_available table is populated during probe > > rather than being a static table. > ... > > + > > + for (i = 0; i < ARRAY_SIZE(ads112c14_pga_gains_x10); i++) { > > + int *scale_avail = &data->sys_mon_chan_short_scale_available[i][0]; > > + u32 gain_x10 = ads112c14_pga_gains_x10[i]; > > + > > + /* NB: slightly odd arrangement to avoid overflow. */ > > + scale_avail[0] = div_u64_rem(div_u64((u64)NANO * 10 / > > + (MICRO / MILLI) * vref_uV / > > + gain_x10, > > + BIT(fsr_bits)), > > + NANO, &scale_avail[1]); > > Oh, what about temporary variable for the inner division? Also note one trick > to avoid casting (and making it shorter). > > u64 foo; > > foo = div_u64(10ULL * NANO / (MICRO / MILLI) * vref_uV / gain_x10, BIT(fsr_bits)); > > /* NB: slightly odd arrangement to avoid overflow. */ > scale_avail[0] = div_u64_rem(foo, NANO, &scale_avail[1]); > > Now, with much more readability, it's visible that the first division is just a right shift. > > u64 foo; > > /* ...a comment to explain voodoo calculations... */ > foo = (10ULL * NANO / (MICRO / MILLI) * vref_uV / gain_x10) >> fsr_bits; Did not looked enough time but also looks like maybe mul_u64_u32_shr() could be used? And given that it handles things nicely (even uses 128bit when possible), might open the door for more simplifications. Anyways, agree with Andy that in the current patch it's a bit of a pain to the eyes :). - Nuno Sá > > scale_avail[0] = div_u64_rem(foo, NANO, &scale_avail[1]); > > > + } > > +} > > -- > With Best Regards, > Andy Shevchenko > >