Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp2311168pxv; Sat, 17 Jul 2021 09:54:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcQWWMycv3lZaWB9cC6f+fKm+k9BJBFGaBfP53A8VQjNBFSNXV9OuRUe+y9SbQ75nquKKs X-Received: by 2002:a92:d80c:: with SMTP id y12mr10645830ilm.30.1626540878236; Sat, 17 Jul 2021 09:54:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626540878; cv=none; d=google.com; s=arc-20160816; b=ZVbBLqQaP9LtvoPAmXSnPUKAINKSn6zmyXUqMa2DN8Z7HQIAy4SwKr4yJHnpislRI9 y2Ql70S3K1q+pPp1/lv0aE65nPOSR5cx7s9nasbIqueKbG5mqWgum25mOWfNZwoFZLXt 0asq9Vf+VOF5HOivvimKMmlkOKyGO1NApSQJ76i95caZeQvyo5061nVFBPCVSBei58K3 OA22+MaXnxngFhZkybiq/0ncfKr2wlMbFXC/nz1S7zWqre2Dl3TxG42zD3nC15pLQTqb fyFXQcB5pfgnCoB1Bon8uhpkqaGJ8U88TUrVcqRaGQU3Su4PQk3rxrf+Xg2FIXRJy2qo aupg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=fKT0BMzSXiMLdk02dWzCyBzgF7nJyTfnLN/rTKoOHNk=; b=BV6Cszlz99flVPV70egmm1DdzafMmUhu+AFv1I9zdJJ0RaqbM3a9ehlLyw+4tCPKQs /mwbbkJ2Z8tzUqSKNW7bozJEpjxtg+Lck8+d/heDcxyO8Z8H7JCOgmI8T1rPQYPSU8ih 0hbCGEnEKOWBwVC7m2jxnBZe0EZZFImxejwZTVOV+2MDDM7PF6LXmKTCkj8h8s+sdZlj KeUckO7cWcqCFmOc42zTO39l1NgG1o+M7+0xKgA1CgV7WCq0xOYFQjLkFWQrNydgRpSl 2sjBae+pR+msvmt1h2kEwo3qO4K/bZfI7Nz7pmbTJECJpRrJlmHw6Ld5cig7hZzhmcT4 i0AA== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t15si13051060ilg.47.2021.07.17.09.54.25; Sat, 17 Jul 2021 09:54:38 -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; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232312AbhGQQ42 (ORCPT + 99 others); Sat, 17 Jul 2021 12:56:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:38126 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231346AbhGQQ42 (ORCPT ); Sat, 17 Jul 2021 12:56:28 -0400 Received: from jic23-huawei (cpc108967-cmbg20-2-0-cust86.5-4.cable.virginm.net [81.101.6.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E1FA26109E; Sat, 17 Jul 2021 16:53:28 +0000 (UTC) Date: Sat, 17 Jul 2021 17:55:51 +0100 From: Jonathan Cameron To: "Liam Beguin" Cc: "Peter Rosin" , , , , , , Subject: Re: [PATCH v5 05/10] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Message-ID: <20210717175551.20265ac4@jic23-huawei> In-Reply-To: References: <20210715031215.1534938-1-liambeguin@gmail.com> <20210715031215.1534938-6-liambeguin@gmail.com> <8417f698-eef2-3311-625a-1ceb17d3e5b2@axentia.se> X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: > > > 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-rescale.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 *indio_dev, > > > do_div(tmp, 1000000000LL); > > > *val = tmp; > > > return ret; > > > + case IIO_VAL_INT_PLUS_NANO: > > > + tmp = ((s64)*val * 1000000000LL + *val2) * rescale->numerator; > > > + do_div(tmp, rescale->denominator); > > > + > > > + *val = div_s64(tmp, 1000000000LL); > > > + *val2 = tmp - *val * 1000000000LL; > > > + return ret; > > > > This is too simplistic and prone to overflow. We need something like > > this > > (untested) > > > > tmp = (s64)*val * rescale->numerator; > > rem = do_div(tmp, rescale->denominator); > > *val = tmp; > > tmp = ((s64)rem * 1000000000LL + (s64)*val2) * rescale->numerator; > > do_div(tmp, rescale->denominator); > > *val2 = tmp; > > > > Still not very safe with numerator and denominator both "large", but > > much > > better. And then we need normalizing the fraction part after the above, > > of > > course. > > > > Understood, I'll test that. > > > And, of course, I'm not sure what *val == -1 and *val2 == 500000000 > > really > > means. Is that -1.5 or -0.5? The above may very well need adjusting for > > negative values... > > > > I would've assumed the correct answer is -1 + 500000000e-9 = -0.5 > but adding a test case to iio-test-format.c seems to return -1.5... 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 were 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 time back wasn't a the right one, but it may be a pain to change now as too many drivers to check! 1, 0000000 == 1 0, 5000000 == 0.5 0, 0000000 == 0 0, -5000000 == -0.5 -1, 5000000 == -1.5 > > I believe that's a bug but we can work around if for now by moving the > integer part of *val2 to *val. Yup. Fiddly corner cases.. Jonathan > > Liam > > > Cheers, > > Peter > > > > > + case IIO_VAL_INT_PLUS_MICRO: > > > + tmp = ((s64)*val * 1000000LL + *val2) * rescale->numerator; > > > + do_div(tmp, rescale->denominator); > > > + > > > + *val = div_s64(tmp, 1000000LL); > > > + *val2 = tmp - *val * 1000000LL; > > > + return ret; > > > default: > > > + dev_err(&indio_dev->dev, "unsupported type %d\n", ret); > > > return -EOPNOTSUPP; > > > } > > > default: > > > >