Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2579514pxb; Sat, 28 Aug 2021 20:05:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQ2owxTTpP12ewp5efxCIgSd0g7TCZSFNTkWMwI74BwEhgd1mqQshOI+L0AkL/KnnZXwkM X-Received: by 2002:a17:906:69d0:: with SMTP id g16mr17695080ejs.20.1630206337338; Sat, 28 Aug 2021 20:05:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630206337; cv=none; d=google.com; s=arc-20160816; b=bxGTewk+UAjVjoHjjKoPQAhQayXFlWxN2YyCQQvYPQy+/gaG7cuGzta0rfscAnUPyU 5x6uMZK0B5WduRyxULj7YHbsrTuNUlRWBx4f9gnVG9XLKTSIJjo7Fi5yo252q/V7SvJZ rO901EF4g3Jkmc+fj+HUhdkIVFLBu6jV7TehymQXmLOUm6S9H+36v9C8AXDhOPAUHC2h oFd8LnmsR4RUhYs9lm8R9FdVV1JbSAUhZCsUQ8pkqs+nXi1NR5yhrAgIEnB74pj5dyki giFKCOYWCP3s9scmsyYUu8W8Q78tuZ0fPA+iPAaKANYXJYBZYNWpcJL4u0NhfMmcxm1f LVaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=bSFSnBg/LwKqVLw8WSTQhnXu1cC1HTBzcNRsDCdhXyI=; b=tAdM56WshSLwYWmEa9JRkKX9gg1J3to9ZDqaxxKpaLElqxYu0m0ZW8fnPqn1M3DUb9 BG4ylz6sK0tR4wDzOg1k15VLcTyJLS/QeXqP5CQGLs9GgtltGkI0VNP2ud6U3X58akJX 3DlCaY0VaQ4syjjVJ7PkHH8IpUjBNb8gnCZqkYDv7GA4/GWz3IW7zzPd2EsdnqsAMyMB PCkslDiZCFZHtFkppjx5HJ7ZipC2MqMwybsi3rzclctpeS2ENYBtZgu5iJlVh0PmgzRO CXdF2lu+WfJHuHlT2+ahVyD45eg9l1IBQseUlSTUNP0i4TJDEebtapxB44W6HHLaNAqq Y1ZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=c3HwxsSo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g23si11090967edp.82.2021.08.28.20.04.44; Sat, 28 Aug 2021 20:05:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=c3HwxsSo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233061AbhH2Cvt (ORCPT + 99 others); Sat, 28 Aug 2021 22:51:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230334AbhH2Cvs (ORCPT ); Sat, 28 Aug 2021 22:51:48 -0400 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2683CC061756; Sat, 28 Aug 2021 19:50:57 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id 14so11799961qkc.4; Sat, 28 Aug 2021 19:50:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=bSFSnBg/LwKqVLw8WSTQhnXu1cC1HTBzcNRsDCdhXyI=; b=c3HwxsSoQzxR7C7XPdiJn7TnB3h6Rw+zH+o53xB6LeH+idmoMcQ5mjKM+7naWUdcz/ IwuqKtUp47+JXfhdSbpOCZiVCtl9wTgFJJXQ+YVb7IbF+6IGqan0OvqXmP+LphfvgFJI Uvo3nZvSz6uvOSh2NflY+aIAZMl/QU1KVNHd4pt4uwO7uSfA6YbcIs1vGEx6IzSBP/Op egsndoLfw+l15zJzmeHet8iqCf9cWlFnS6Uvo0VleAKXcMlqZsB/GnRORTwDHUkS7UbJ 8qGFsfNVQZXg2sRO9agEyWHr8SORcWGHPiVmlPOUExgqmGYDyGC/8CYhIPxhSv5L8gbU FqTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=bSFSnBg/LwKqVLw8WSTQhnXu1cC1HTBzcNRsDCdhXyI=; b=hufaiQB0d+BWTbhGIlyXfyxIo+SBnSpTPKrz4F3vEp4b2D2//+5VgZ6sUIi+/BDAg8 FbrBWpHpJBLw9Dl1ZMiECeNTAqUjdPoZcBNgz6mk/0xVtr1HsehOdIz7L4bSEUGKsJ21 XvLoMIp0GhJjJcwwaQaKLfcC/b5hA+1Laim+eb3xLhELRHjLqtCkMXTb0usAhlOMnY9R GaRSdHJN5t3BTlJlLdQHsN0+KNFYO2PfDQ7YiAgE46l2U/IZNYyqviUjMKPwFixMCu/n 9TprFFTIQAATpyLh8jjlYzO230spZLxAOIT8jctKrAuQdMhCLlgjhjSiygXlnu324BbU LAoA== X-Gm-Message-State: AOAM531tgbifdhyj/WGRL7CqJ/3QiX8GfGE+8yOhSZ+HfEiizOGnWDYc Wgp9CvpP2ie2P8kpP+TmI8A= X-Received: by 2002:a05:620a:675:: with SMTP id a21mr16184628qkh.421.1630205456157; Sat, 28 Aug 2021 19:50:56 -0700 (PDT) Received: from shaak (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id d78sm8180916qkg.92.2021.08.28.19.50.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Aug 2021 19:50:55 -0700 (PDT) Date: Sat, 28 Aug 2021 22:50:53 -0400 From: Liam Beguin To: Peter Rosin Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: Re: [PATCH v8 05/14] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Message-ID: References: <20210820191714.69898-1-liambeguin@gmail.com> <20210820191714.69898-6-liambeguin@gmail.com> <9a7aa55f-0dd0-3fc7-13a4-5cb9c5d0a252@axentia.se> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9a7aa55f-0dd0-3fc7-13a4-5cb9c5d0a252@axentia.se> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 26, 2021 at 10:11:18AM +0200, Peter Rosin wrote: > On 2021-08-20 21:17, Liam Beguin wrote: > > From: Liam Beguin > > > > Some ADCs use IIO_VAL_INT_PLUS_{NANO,MICRO} scale types. > > Add support for these to allow using the iio-rescaler with them. > > > > Signed-off-by: Liam Beguin > > --- > > drivers/iio/afe/iio-rescale.c | 35 +++++++++++++++++++++++++++++++++++ > > 1 file changed, 35 insertions(+) > > > > diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c > > index d0669fd8eac5..8488f1d83527 100644 > > --- a/drivers/iio/afe/iio-rescale.c > > +++ b/drivers/iio/afe/iio-rescale.c > > @@ -22,6 +22,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, > > int *val, int *val2) > > { > > unsigned long long tmp; > > + s32 rem; > > + u32 mult; > > + u32 neg; > > > > switch (scale_type) { > > case IIO_VAL_FRACTIONAL: > > @@ -40,6 +43,38 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, > > tmp *= rescale->numerator; > > do_div(tmp, 1000000000LL); > > *val = tmp; > > + return scale_type; > > + case IIO_VAL_INT_PLUS_NANO: > > + case IIO_VAL_INT_PLUS_MICRO: > > + if (scale_type == IIO_VAL_INT_PLUS_NANO) > > + mult = 1000000000LL; > > + else > > + mult = 1000000LL; > > + /* > > + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if *val OR > > + * *val2 is negative the schan scale is negative > > The last line doesn't parse for me. It doesn't end with a period either, so > it looks like you moved on before you finished it? I meant to warn that IIO_VAL_INT_PLUS_{MICRO,NANO} are a little odd, and that if either one *val or *val2 is negative, the result will be negative. i.e. *val = 1 and *val2 = -0.5 gives -1.5 and not 0.5. I'll give it another try and will add the period. Thanks, Liam > > Cheers, > Peter > > > + */ > > + neg = *val < 0 || *val2 < 0; > > + > > + tmp = (s64)abs(*val) * abs(rescale->numerator); > > + *val = div_s64_rem(tmp, abs(rescale->denominator), &rem); > > + > > + tmp = (s64)rem * mult + (s64)abs(*val2) * abs(rescale->numerator); > > + tmp = div_s64(tmp, abs(rescale->denominator)); > > + > > + *val += div_s64_rem(tmp, mult, val2); > > + > > + /* > > + * If only one of the rescaler elements or the schan scale is > > + * negative, the combined scale is negative. > > + */ > > + if (neg ^ ((rescale->numerator < 0) ^ (rescale->denominator < 0))) { > > + if (*val) > > + *val = -*val; > > + else > > + *val2 = -*val2; > > + } > > + > > return scale_type; > > default: > > return -EOPNOTSUPP; > >