Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3805534pxv; Mon, 19 Jul 2021 09:08:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx979N9PczGO8CWX57nY63Q0do+Se78unotqroL+8mBN7PKSK5EMCZSRrFJUEQJW8JrXlsm X-Received: by 2002:a17:906:c1d0:: with SMTP id bw16mr28296452ejb.146.1626710936705; Mon, 19 Jul 2021 09:08:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626710936; cv=none; d=google.com; s=arc-20160816; b=lptGPVd4a0rJRPjcAf/o7+5K16P2uusy3bqMx9BAuW9XCWMYceZf6SzhKCsBX4JimY 6YxXn1afsUlRHkW2rkFdDpJZJydxuHusZMW3Di5dAgw8MrZ4joOVXF0rIuRbEkfixYKc A9dQpPxPCN2vORX8tuFT89G2kOiLizJc5ugJCa2egCd7aGg8WpR3ByL/wvSy97CAD8Mg Q6nvAk942D7NWkjKG3Wuk79/khXb5BEyeLC/dWyzacqKYHQd9xbmQJaxNSnrsNnq+OVT 5DoiA+aAwOxAHKYohb+JhchzN0EWDnbEqbeCe65ttLAJbWrlMWP/xCMRbPnZJTWWhyVU 8FLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:references:to:from:subject:cc :message-id:date:content-transfer-encoding:mime-version :dkim-signature; bh=+qAeaJCZPqO/HVsIw3gOHNEnkqH/VbzFWNwRdJqpOrw=; b=Slondr94baVBaAk1SVQBhWsWIslaplblW1ZgxirEAfjeky7JOOmjc6FWQtw6XQJD36 YRREKa/vqUWg3gWrMulTalEI6BCglrTuM7NVTOJiiPC9m8Z/5CYA0SLEI0+sgzYJx5J6 B8HRxiZ4eW1zYlVDERimUc7xotLKZWsFa3Mxo2ApWg+SjbVVfuZtoXtQUVpjrUDvQESM lOVzehriZ21ATL1GSUYY2ZpMdTWbXLknhjRfsnwEMWgzvLdlczLLl2hQl9EkpLAdCS55 pkenEqlYZ/0weFZZXWlsAi7D4nmNLP+PFPIerYmuHvhda5jownaXQGGEdw9H8kzOQdwp Ovog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=chGmklXF; 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 cw15si22129760ejc.604.2021.07.19.09.08.29; Mon, 19 Jul 2021 09:08:56 -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=chGmklXF; 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 S1349184AbhGSPZu (ORCPT + 99 others); Mon, 19 Jul 2021 11:25:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239968AbhGSOuv (ORCPT ); Mon, 19 Jul 2021 10:50:51 -0400 Received: from mail-qt1-x82c.google.com (mail-qt1-x82c.google.com [IPv6:2607:f8b0:4864:20::82c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D63E6C0613B4; Mon, 19 Jul 2021 07:45:45 -0700 (PDT) Received: by mail-qt1-x82c.google.com with SMTP id q15so8234014qtp.0; Mon, 19 Jul 2021 08:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:content-transfer-encoding:date:message-id:cc:subject :from:to:references:in-reply-to; bh=+qAeaJCZPqO/HVsIw3gOHNEnkqH/VbzFWNwRdJqpOrw=; b=chGmklXFlHaJdMw7VL24uwTgk+Ncrr3GltNArjBTjhgv9aOKWl4unqCMqzXYS/GREU af9olIRWxmV7LoIlqmiFvjNAHTj/y8iJFAgq2yPOTiW0yh6DIaORMpOdFgsYlpoN1yon 7Wok7/91K91OB14MIFrE9TGGmEnx9YP4izp71QNW2VOT1LXJDzzZKWkEyODra50k0A0q gGsRJzvXzFCR8apLuXu08Iv99/uY39Ivjp5Mvw3bpr0Is9pidXWD+Ae2dXcRDIuE8gNz YY2UWAXbNSFRKwa7Kt2nv0uGYYL5QsAGBSue1FYqnjCSw5wp64tE7o1MmeTtV7/HkrW8 Qwxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:content-transfer-encoding:date :message-id:cc:subject:from:to:references:in-reply-to; bh=+qAeaJCZPqO/HVsIw3gOHNEnkqH/VbzFWNwRdJqpOrw=; b=le8lJIgpQtBsqu+XODtYHmev0b64EGpz8mIm30doPUigWlq5ExmeM5b5Qzfws+78Sx O8wnfE4ZqgRU8BIXbGbLa5ZDL8urMi70QYwEPhITDznOjb0tq9eOdkE7MovfbwwZ4KkJ WsD6e/fjjflu/Cp/vlGLh5wDgZUS7J95/rlQTkFV4uKBhUbHb5CJFxaiFVXo0k4xDn7c pftU0lFr76RbbwyDQxoeWaDmDF9RBrt9K5iGAxhwvRqbnsLHM9+9LgbO6FjhgRDTWgVf VB5++f3JhNoHmwIHcCz8Vr6LrYZ8FKbh62e74iBftnTjjFwC+YZumxtICpNo6FgAcxGe 8z9Q== X-Gm-Message-State: AOAM533Lk1XAvgJ0vui4zISe3maqfOKLtb01YirsbbTBil9jogQ0hJkc USyJPNfKwUfJeMdjBXcVbeIrADCAWoem7A== X-Received: by 2002:ac8:51cd:: with SMTP id d13mr22426827qtn.114.1626707731696; Mon, 19 Jul 2021 08:15:31 -0700 (PDT) Received: from localhost (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id c2sm8307508qkd.57.2021.07.19.08.15.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 Jul 2021 08:15:31 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 19 Jul 2021 11:15:29 -0400 Message-Id: Cc: , , , , , Subject: Re: [PATCH v5 05/10] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support From: "Liam Beguin" To: "Peter Rosin" , "Jonathan Cameron" References: <20210715031215.1534938-1-liambeguin@gmail.com> <20210715031215.1534938-6-liambeguin@gmail.com> <8417f698-eef2-3311-625a-1ceb17d3e5b2@axentia.se> <20210717175551.20265ac4@jic23-huawei> In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon Jul 19, 2021 at 4:31 AM EDT, Peter Rosin wrote: > > > On 2021-07-19 01:44, Liam Beguin wrote: > > On Sat Jul 17, 2021 at 12:55 PM EDT, Jonathan Cameron wrote: > >> On Fri, 16 Jul 2021 15:18:33 -0400 > >> "Liam Beguin" wrote: > >> > >>> On Thu Jul 15, 2021 at 5:48 AM EDT, Peter Rosin wrote: > >>>> > >>>> On 2021-07-15 05:12, Liam Beguin wrote: =20 > >>>>> 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 | 15 +++++++++++++++ > >>>>> 1 file changed, 15 insertions(+) > >>>>> > >>>>> diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-re= scale.c > >>>>> index 4c3cfd4d5181..a2b220b5ba86 100644 > >>>>> --- a/drivers/iio/afe/iio-rescale.c > >>>>> +++ b/drivers/iio/afe/iio-rescale.c > >>>>> @@ -92,7 +92,22 @@ static int rescale_read_raw(struct iio_dev *indi= o_dev, > >>>>> do_div(tmp, 1000000000LL); > >>>>> *val =3D tmp; > >>>>> return ret; > >>>>> + case IIO_VAL_INT_PLUS_NANO: > >>>>> + tmp =3D ((s64)*val * 1000000000LL + *val2) * rescale->numerator= ; > >>>>> + do_div(tmp, rescale->denominator); > >>>>> + > >>>>> + *val =3D div_s64(tmp, 1000000000LL); > >>>>> + *val2 =3D tmp - *val * 1000000000LL; > >>>>> + return ret; =20 > >>>> > >>>> This is too simplistic and prone to overflow. We need something like > >>>> this > >>>> (untested) > >>>> > >>>> tmp =3D (s64)*val * rescale->numerator; > >>>> rem =3D do_div(tmp, rescale->denominator); > >>>> *val =3D tmp; > >>>> tmp =3D ((s64)rem * 1000000000LL + (s64)*val2) * rescale->numerator; > >>>> do_div(tmp, rescale->denominator); > >>>> *val2 =3D tmp; > >>>> > >>>> Still not very safe with numerator and denominator both "large", but > >>>> much > >>>> better. And then we need normalizing the fraction part after the abo= ve, > >>>> of > >>>> course. > >>>> =20 > >>> > >>> Understood, I'll test that. > >>> > >>>> And, of course, I'm not sure what *val =3D=3D -1 and *val2 =3D=3D 50= 0000000 > >>>> really > >>>> means. Is that -1.5 or -0.5? The above may very well need adjusting = for > >>>> negative values... > >>>> =20 > >>> > >>> I would've assumed the correct answer is -1 + 500000000e-9 =3D -0.5 > >>> but adding a test case to iio-test-format.c seems to return -1.5... > >> > >=20 > > Hi Jonathan, > >=20 > >> No. -1.5 is as intended, though the IIO_VAL_PLUS_MICRO is rather > >> confusing > >> naming :( We should perhaps add more documentation for that. Signs wer= e > >> always a bit of a pain with this two integer scheme for fixed point. > >> > >> The intent is to have moderately readable look up tables with the > >> problem that > >> we don't have a signed 0 available. Meh, maybe this decision a long ti= me > >> back wasn't a the right one, but it may be a pain to change now as too > >> many > >> drivers to check! > >> > >> 1, 0000000 =3D=3D 1 > >> 0, 5000000 =3D=3D 0.5 > >> 0, 0000000 =3D=3D 0 > >> 0, -5000000 =3D=3D -0.5 > >> -1, 5000000 =3D=3D -1.5 > >> > >=20 > > Understood, thanks for clearing that out. Hi Peter, > > I just realized that do_div assumes unsigned operands... > > :-( I noticed the same thing after adding the kunit tests. I added patches for that. For IIO_VAL_PLUS_{MICRO,NANO} specifically, I have something working but I like your approach better so I'll work on it a little more. Thanks, Liam > > Cheers, > Peter