Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp423310pxb; Tue, 1 Feb 2022 03:00:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJwlSJKE9hfkf/qWau9DPDdsnkV/S3yKzODPc4/TOLMlfMx8HYlcV97ojl69gu3DfRjef4Pc X-Received: by 2002:a17:906:5d12:: with SMTP id g18mr19900951ejt.745.1643713215244; Tue, 01 Feb 2022 03:00:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643713215; cv=none; d=google.com; s=arc-20160816; b=CsbyO1pHkwj9nA1CIOd2PnOwIeyEf+Gi0q1IEfVOemmmLJ7OasW04maTiAdgIlG1JB 2CilUdl+ZUbe5+sG83O53EAEINT0sE0GUUz0YyNUjdOB/yDLeF0wRjyh8Vf1vo8Hdtiy 5kM9yIFHWBncO9MeEXggf7yQb/zQ0SSN7/DJIr2K/H5v8AVtJk+s2z4rFz10vE9yGsRk q2Xb2FczwG/sfswBn4fMU5sQl1jDVz2UUn9kSEoYgzwNHtxtcAUqDX9FriVrY4h5GSWg SNQ5LNRPgPN6z1+1Aw1gK0tx4XdOtsKLqvfQpPAecoQcxlXymK6ajyKdBvHqfP6MCW+J VFaw== 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=N4vn2QrbP+FP78/S6RhE3MtkGHax4gBMpmADGrGNkNY=; b=HuxyNCd/UdrRq1XvK3rAU5mbvevzmr27mNC8plQ6w3/fcg86DdJu5kO/FpQPC2CLV4 EMcZLAZLfHqLfAupL9MVHITSQ3RuYbQWOLmJRB+AxzJXF7QDLLX49vuL4chugxAqE+hV tdNZCXjWD8BF+Nn3PnhUcML13o+9XaWkIhzwK7CbA81WC8lFhDt5s0F4kDTC1JeBqcU4 8zuH0zS5rNasMB4Kl1mFijhpEYFIieiMud5fiongW18+Ds6paKgP7195/0eezJf/pzVY Cw/ioDZPywwjrAlU1N0Uw3T/gFoyGJ/eimJz9r+ci4nQRSYNc0m0dhEdQ5vyPyEqFsC1 f70A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="oI/oNeeF"; 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 c17si10988284ede.572.2022.02.01.02.59.48; Tue, 01 Feb 2022 03:00:15 -0800 (PST) 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=20210112 header.b="oI/oNeeF"; 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 S1355519AbiA3QL2 (ORCPT + 99 others); Sun, 30 Jan 2022 11:11:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355484AbiA3QLV (ORCPT ); Sun, 30 Jan 2022 11:11:21 -0500 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2049BC061714; Sun, 30 Jan 2022 08:11:21 -0800 (PST) Received: by mail-qk1-x730.google.com with SMTP id c189so9909821qkg.11; Sun, 30 Jan 2022 08:11:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N4vn2QrbP+FP78/S6RhE3MtkGHax4gBMpmADGrGNkNY=; b=oI/oNeeF2vpxAxtWOrLeHa/Xoz2fIwaW5cB3sqZ3lic8YQPaVP4CP7GhWK6NfUbTfy 5FrZA8lyD7jkFxIUnJVH8KbQ5XQa6PwwEffUgyqVQK0Ve4FCUIImB6IFLzJ1vRSAxU5J RktqmLINIlxyJYz9gfUIzz81jNBx6M1r0O+bpoEnBAC5/4XW9nsXSdOXR7b7j2HUtx7Q 1rfNBbT63NFEiImaelNyfRQuiTcxBHKJnQ0W4kfjBt3yfwrxaa5xmNE+c/u3elg7blCf pjHCwYnl4wb1nDSwkIbH6+F2Ajbh1AKUm9w4nDlMEBYVu1NrpFrk8s7kDmD0KlC0Qx7L jcNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N4vn2QrbP+FP78/S6RhE3MtkGHax4gBMpmADGrGNkNY=; b=j+hQ5HIe0Lh3YF+2oEHcxAzYonqNd9aG1N6bgPZCORXPdBnmjxLttvDHlKgajQH3NV 5G8uudXrJF7pMQXDfpuLp9cp8/tHcIGWipNnD+Nns/AhmL/xOFqBxuxlOR6Imeb2EZ7Z MXd+Ja1p/o9iYqOp262hA9BtI+RhWnAdtj6X2RJaDoZZK8ank071/sidZaw3LjbWLaex 6Ie5UQ+MGnTU7ntVbkT0y8prjYpVKhtnQiQz+wupZxnPNhYf5tlU2pW7o8Nh1x5KnOkG yj6bYwcauoOHkSevIA7v43nRXjW3IILrW4fLl852d2c5KFmX2hDBMLspszG33BKV7Vz8 3lBA== X-Gm-Message-State: AOAM530Lxkdgt/giGhnR30j5Ln76jMb/h60Sv9fdgTCii47IXVwh6Rqa T6WXhr5S4h7arS3SNWOOokrrVtKI81M= X-Received: by 2002:a37:a711:: with SMTP id q17mr10992176qke.193.1643559080200; Sun, 30 Jan 2022 08:11:20 -0800 (PST) Received: from shaak.. (modemcable055.92-163-184.mc.videotron.ca. [184.163.92.55]) by smtp.gmail.com with ESMTPSA id v73sm6906148qkb.51.2022.01.30.08.11.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 08:11:19 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v13 03/11] iio: afe: rescale: add offset support Date: Sun, 30 Jan 2022 11:10:53 -0500 Message-Id: <20220130161101.1067691-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.35.1.4.g5d01301f2b86 In-Reply-To: <20220130161101.1067691-1-liambeguin@gmail.com> References: <20220130161101.1067691-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a preparatory change required for the addition of temperature sensing front ends. Signed-off-by: Liam Beguin Reviewed-by: Peter Rosin --- drivers/iio/afe/iio-rescale.c | 81 +++++++++++++++++++++++++++++++++ include/linux/iio/afe/rescale.h | 4 ++ 2 files changed, 85 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index f833eb38f8bb..63035b4bce5e 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -3,6 +3,7 @@ * IIO rescale driver * * Copyright (C) 2018 Axentia Technologies AB + * Copyright (C) 2022 Liam Beguin * * Author: Peter Rosin */ @@ -82,11 +83,46 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, } } +int rescale_process_offset(struct rescale *rescale, int scale_type, + int scale, int scale2, int schan_off, + int *val, int *val2) +{ + s64 tmp, tmp2; + + switch (scale_type) { + case IIO_VAL_FRACTIONAL: + tmp = (s64)rescale->offset * scale2; + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT: + *val = div_s64(rescale->offset, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = (s64)rescale->offset * (1 << scale2); + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_NANO: + tmp = (s64)rescale->offset * GIGA; + tmp2 = ((s64)scale * GIGA) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * MEGA; + tmp2 = ((s64)scale * MEGA) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + default: + return -EOPNOTSUPP; + } +} + static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); + int scale, scale2; + int schan_off = 0; int ret; switch (mask) { @@ -113,6 +149,47 @@ static int rescale_read_raw(struct iio_dev *indio_dev, ret = iio_read_channel_scale(rescale->source, val, val2); } return rescale_process_scale(rescale, ret, val, val2); + case IIO_CHAN_INFO_OFFSET: + /* + * Processed channels are scaled 1-to-1 and source offset is + * already taken into account. + * + * In other cases, real world measurement are expressed as: + * + * schan_scale * (raw + schan_offset) + * + * Given that the rescaler parameters are applied recursively: + * + * rescaler_scale * (schan_scale * (raw + schan_offset) + + * rescaler_offset) + * + * Or, + * + * (rescaler_scale * schan_scale) * (raw + + * (schan_offset + rescaler_offset / schan_scale) + * + * Thus, reusing the original expression the parameters exposed + * to userspace are: + * + * scale = schan_scale * rescaler_scale + * offset = schan_offset + rescaler_offset / schan_scale + */ + if (rescale->chan_processed) { + *val = rescale->offset; + return IIO_VAL_INT; + } + + if (iio_channel_has_info(rescale->source->channel, + IIO_CHAN_INFO_OFFSET)) { + ret = iio_read_channel_offset(rescale->source, + &schan_off, NULL); + if (ret != IIO_VAL_INT) + return ret < 0 ? ret : -EOPNOTSUPP; + } + + ret = iio_read_channel_scale(rescale->source, &scale, &scale2); + return rescale_process_offset(rescale, ret, scale, scale2, + schan_off, val, val2); default: return -EINVAL; } @@ -189,6 +266,9 @@ static int rescale_configure_channel(struct device *dev, chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE); + if (rescale->offset) + chan->info_mask_separate |= BIT(IIO_CHAN_INFO_OFFSET); + /* * Using .read_avail() is fringe to begin with and makes no sense * whatsoever for processed channels, so we make sure that this cannot @@ -353,6 +433,7 @@ static int rescale_probe(struct platform_device *pdev) rescale->cfg = of_device_get_match_data(dev); rescale->numerator = 1; rescale->denominator = 1; + rescale->offset = 0; ret = rescale->cfg->props(dev, rescale); if (ret) diff --git a/include/linux/iio/afe/rescale.h b/include/linux/iio/afe/rescale.h index 8a2eb34af327..6eecb435488f 100644 --- a/include/linux/iio/afe/rescale.h +++ b/include/linux/iio/afe/rescale.h @@ -25,8 +25,12 @@ struct rescale { bool chan_processed; s32 numerator; s32 denominator; + s32 offset; }; int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2); +int rescale_process_offset(struct rescale *rescale, int scale_type, + int scale, int scale2, int schan_off, + int *val, int *val2); #endif /* __IIO_RESCALE_H__ */ -- 2.35.1.4.g5d01301f2b86