Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1101947pxy; Sun, 1 Aug 2021 12:41:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxl0HtzTExJQsKexTkDIF+X7RyUqNMUMU75nivqFFDnSgBS5jimKLOsfcPrcR79eOTLDYRt X-Received: by 2002:a05:6638:114:: with SMTP id x20mr11172150jao.118.1627846894189; Sun, 01 Aug 2021 12:41:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627846894; cv=none; d=google.com; s=arc-20160816; b=MpCd2jGk/AU5TsNBNZJuQEfUDI34oxFeEcQlQrctxvJlP72WU6BFigCXoTf9pDIbb3 zyOuHbXWRO2mHJzXusyrG3T1vXFECOX5148k40Rt4DVAB44gBG6/Ljsucyhj2W4m2/5C 8MjmJoGGo9k7faCb2h4Zm+78344sQ4WpzEmVUmkBZFRC0mOoe5gXrwZgZYODaX9aiXxO fWJ1fllb1rBXNh2ctv4f1+VtSTTQH9gH8CRgI+2+DIbkfQSORy+t5p0VZ4KIu+5WWRhM y8/NfjYzfX9GTfpIoe4sZKzBtuKAH+5TFMjKgKzFP40N1YaLXlXgfr3FOCoJZHxtBr1o 6Dwg== 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=u1EgV+qDKh1MLXoqKHgC3S7qm/xOWGqYPVM+PTaJ7PI=; b=K/1HtmgrOBHD8wUczXm7Y9Yiq9rKGVPepeYaiy4FKq4RsFqoUjQMWgAO0i6PfpIqGi uoJh2F4IS3RtFx8RlZY+ayQIEVfiZ9GU9FQqWSydpzwEG/m9jn/cFWNQeKNZTpFaoRge kvbwHf5xbSWd9kU7K1JVye3usxnws893gG1e+bnLo+ENk9BgOozj7nfQWQHfxmhwLaHV zscLbcM+ECNnUrlLYJZGDG+DrgTKGUGe2osIDig9ijEiHHaIIFGdW6tJXpLunllyOeBx UbQElr1eJ8IgEQWmXGHUJ6hskoK8hZic6YRw71v5XQduHIWjn+DjuAscI5E5iud36uyv g5qg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="OkBL/TDm"; 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 a6si12510758ilv.81.2021.08.01.12.41.23; Sun, 01 Aug 2021 12:41:34 -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="OkBL/TDm"; 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 S231877AbhHATkd (ORCPT + 99 others); Sun, 1 Aug 2021 15:40:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231150AbhHATkZ (ORCPT ); Sun, 1 Aug 2021 15:40:25 -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 66852C06179B; Sun, 1 Aug 2021 12:40:17 -0700 (PDT) Received: by mail-qt1-x82c.google.com with SMTP id h10so10331230qth.5; Sun, 01 Aug 2021 12:40:17 -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=u1EgV+qDKh1MLXoqKHgC3S7qm/xOWGqYPVM+PTaJ7PI=; b=OkBL/TDm7lmhfh9GZTA4v0xQJL9vFYSvcsV50bwr0UI570YfQSxVc20ra7Y1AlJofs GTdHjy+i0aDA/SfHb8F+q0xVOOWD9idQpB5WdCJJm7x/erQA/lX1QE5g2tRXNndoVXQv qdPfRuOXuCg8M71jBCrlBVIna5bOtj38iVZmv0n9686lq4GJE6lEaEhOckCYdt/P6Zbo yqbY7Gnt9O53VEwDxL/+RjNYjLCKzPwfh+eqkDFIYLm4LZdsI4lyQ5t8pskvJE78W003 wieHXyeohFxUv4K454SLpiNiIWX62sxzFBYZIsEDYhapvBUlVt/wA+q8qTzI+EQ3OJa7 pOvQ== 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=u1EgV+qDKh1MLXoqKHgC3S7qm/xOWGqYPVM+PTaJ7PI=; b=l3HzIYU2wzy0UbEmUjcS32PFK3/CFZlpxZttIeZ1A+pPKbUBlFx3J5uQFLLD7GMBwL 2FG77+OliEzHH5KTXU9bRbxo1GxmgRCHDEG8tKiH5BmIDYFX/5yeRinuDkETv23LiSUC 8FJYaqq4Y6+Kbjp6y4qay2UqLaEVjzQ1wv9aWGiNH6CNPuHDJCBTXkWN+km9dnrIMDQK RTsE+OBr01SQWCMGV7hXalYNv/b46H+54RE+j6WVn1EuOR7QDHeasDY93hrCPQ7wG4wE +TGzNCE1SvwdY8qz6Nqgs9m6q6CT9l+3xy3G6vk8GxRBP+6chzMwDkuCnFKXdYYcleea sEmQ== X-Gm-Message-State: AOAM533zJsht8XR8I/Ky8inISwlQo7q1SmpMN5GurtytfR9YI/S18w0q lnd+R27w5OVtb2LrZP4o4YU= X-Received: by 2002:a05:622a:142:: with SMTP id v2mr11421423qtw.264.1627846816592; Sun, 01 Aug 2021 12:40:16 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:16 -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 06/13] iio: afe: rescale: add offset support Date: Sun, 1 Aug 2021 15:39:53 -0400 Message-Id: <20210801194000.3646303-7-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 This is a preparatory change required for the addition of temperature sensing front ends. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 80 +++++++++++++++++++++++++++++++++ include/linux/iio/afe/rescale.h | 4 ++ 2 files changed, 84 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 615f5d9cbb8b..623744da269a 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -81,11 +81,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 * 1000000000; + tmp2 = ((s64)scale * 1000000000L) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * 1000000L; + tmp2 = ((s64)scale * 1000000L) + 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) { @@ -112,6 +147,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; } @@ -188,6 +264,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 @@ -352,6 +431,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 14d4ee1227c6..b152ac487403 100644 --- a/include/linux/iio/afe/rescale.h +++ b/include/linux/iio/afe/rescale.h @@ -23,8 +23,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.30.1.489.g328c10930387