Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1101902pxy; Sun, 1 Aug 2021 12:41:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxwaZx2WA/TKcbSuloscfV1xseAGRen3qlsfu7uEVQFlccxxioMOIPhemSR6vMlbp5ARQ+M X-Received: by 2002:a92:c24c:: with SMTP id k12mr6870904ilo.28.1627846887325; Sun, 01 Aug 2021 12:41:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627846887; cv=none; d=google.com; s=arc-20160816; b=ofn1D4rB/MGkZCDwfFuowfStpMXhkzTRJMK3lInVE0rv8dM0tqKQnJsA6SfNEBdOEk xRWwoG91VmcDWmAhZkBQrbefu8+9c89nrZgqgHivqrsSx8HPnoIVyIvALMx8zIqruaf2 Xqg9GTFg8BCfPL3sz72DpRS6aaJd9ObrFwvJwdZmF8a3kAH1vdAGrBvwBo9kRjrN9dIu B7eU+rIbZzQZJ30r7HHrByeUDDg9jIIHMnY273Vfc1YnmIX3TiFS13dkeOC8RCcWpJ20 HRBbNhgTXXPSkGgtpkOpeik4RC19qUDOybnDBGFBuHqsCEpBKA+BT0D1bfjSRfzQ48P6 hQ/Q== 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:date:subject:cc:to:from :dkim-signature; bh=hlz1mlRz0pIJ2/uHlMEkfOBEElQWI/RnQqjZdJLYyew=; b=LTrYWhKQB8gdTNgpJoq9zxwhsgKqgGeHgwRjYV5Bp6YpPo2oQepldWPUIaSrllE/bc 1NBvAecEP8zWEzTJtyXyxMNUB7+yHDBMlBmx22EcBb5ipKP8EcJW6MZHB5Slz2fyeRBT f3NF65bTSrvZrBVjTMvc45yeySGkEz90Rn28E/Jrrx8mHRbKNAgqhYBZlnfwLi0Qp3Nd xwz6hrdSjXa+AknSZ5ht/QD3mdeN6wr+ZMsI9ZiZv/8ydclN7Kn2Am1OL+aOXvWaYMGZ TyhLUPZuMPMCqr0/OYGhWbphi7U9gRAZTsrLTUeAVx0FBrzy06qdg0GNbaKV2bM2f7EG fy5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oISa7DuC; 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 l7si10713463jad.82.2021.08.01.12.41.16; Sun, 01 Aug 2021 12:41:27 -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=oISa7DuC; 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 S231803AbhHATkb (ORCPT + 99 others); Sun, 1 Aug 2021 15:40:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230516AbhHATkZ (ORCPT ); Sun, 1 Aug 2021 15:40:25 -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 6CA6DC0613D3; Sun, 1 Aug 2021 12:40:16 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id x3so14740172qkl.6; Sun, 01 Aug 2021 12:40:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hlz1mlRz0pIJ2/uHlMEkfOBEElQWI/RnQqjZdJLYyew=; b=oISa7DuCKwko9vd6uCu7LUGn+NDiJeJyTVxDcHYTHFiz2bFiXCG8mlP7JOtJnFbkoZ fVv4G/cn2DSFqOO9QsAOC+pzs6LPDMQv1ot1EaER49NYhyaTnjHg28VJZ9hf53RdhTPm K5zwb68ZcIsMA7QJAGXDvF+3jAC/j/dE6+wl/HFBsdBtAaEQ+DzIVKC8wJglrNqj3aDF Gqc9j1sYrU7VfNRR8VYqNCPJvzKR+PCdmK2mW8d7IwCPZD3X505bV/GDMgcfn9t8pQCE FmGXLcZ2m8uzgHwHFtdPSWLAwpOR+QRzlVyuIi49iv/9gdBSQZRMfD7rn3yn2f/3WBQ1 Z6wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hlz1mlRz0pIJ2/uHlMEkfOBEElQWI/RnQqjZdJLYyew=; b=a6llzC3eXzYKAwdSki4nm2eNNDG6iCjg24CuhLWIsGMDlr8n106EYyR2K/jJ8fT2mN EYC6fAyQc0MjXJbGyoDCfC/gx8zw4jLNmkodXkr93IXdCWsFdHc8FwCxfe10Hta807oA 1rDpzG2XGgJ0+jDiZ3gd4XOx1/hGoItwyLBvgBEp6oMZr05qmdRSQemn8BHMWkoqgj8t pZmrRWUXM9XtKonmxlXY7kaMrHaALYazH7uIokmlcukAYHLHPW98vGF4XxO/g+BnyX83 /F4xYJ01A/JqA7MIwzxEDejFE0RZQlJCIH6yaUjXhjFIttCi2z0J0lGRa1gTDJ5zxVxb Dshg== X-Gm-Message-State: AOAM530r5lFRAmzhJu+awLqti/H9I55zekhoF18FBp15IMWNzUih8siB I5Vx5gUQjFiUt27ToI5kdyM= X-Received: by 2002:a37:a004:: with SMTP id j4mr12193562qke.499.1627846815634; Sun, 01 Aug 2021 12:40:15 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:15 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 05/13] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Sun, 1 Aug 2021 15:39:52 -0400 Message-Id: <20210801194000.3646303-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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..615f5d9cbb8b 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; + u32 mult; + u32 rem; + 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 + */ + 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; -- 2.30.1.489.g328c10930387