Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp5621701pxb; Mon, 14 Feb 2022 03:43:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJxkcFQIZem08EgTi+pmLRZIwckeatICA3a1SzbfA+sIb4Zt1JERqMQNaA1tjpRf2jE+0RWA X-Received: by 2002:a17:906:43c9:: with SMTP id j9mr10909603ejn.652.1644839016802; Mon, 14 Feb 2022 03:43:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644839016; cv=none; d=google.com; s=arc-20160816; b=fqEdD5LYl+nNKNYIXwd2OtSkuMG5QjjNiKyLVYk8eP2uoigPtS/lXe6DZhBSSJUe79 TuKXOMYoXHcXAxKnEJM1LAVdV7lCSCBowldkAP1WgoqPGO3YEjgicsDEphM9t2yz9CuG O/UxbUQBUbTLUsZ9DosOKmmVSBgTLskCUIgv+tDjU9YHGZlkShwo5KZS6Y2ApF5QaFNO ZyanL+j/9+WZ9JY5mAAvEVlovyz4Bu/XoyP/IixS27crBwYlcWeEDYWVXPJT07Q7FoFM XU7iddrtQZYMJFbeJp9Sx5AfQpDup1DuuKJEEvO+n/nH83V8TAIyTe8ACMYXy3UkEoyT pYEw== 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=IsBxqaV4NE+StTwNs6VHaWSVWDLX6eURZgOwgnglY+0=; b=beMmWQLvPS+bIcvFX7eNtU2/Yx41RIBzWkQrv0h4welfvm0KSECg69sZ1c5iuEsAkI QNbXbNiFJvQfi2eKIn6f3n4xsrGdWZtWJxtDhbnquHfP2ziio5igMeeZFk/Bcqxj4KBU LoeChVqRvlnRoU/b17Jbai12R9PRgn67FHGM3jofVR77srUBdYJ8iya33/vq1Hw0/x5Y tJB8F4SgDfoEMb1EOjUxEBPZFiqgN+TjOpUXihIfmmAJ9IzKObNoeChQcxKBhJuIGdcX KCIKI6tcUSFjofD3WLKcm2GUE0TvTjUzDyuaT9aLUUlubO/otNvplzI2gWKtovdV7g1R W/yA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=WpZqj92q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ec30si3695191edb.80.2022.02.14.03.43.13; Mon, 14 Feb 2022 03:43:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=WpZqj92q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S233130AbiBMC6D (ORCPT + 99 others); Sat, 12 Feb 2022 21:58:03 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:46426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233043AbiBMC5x (ORCPT ); Sat, 12 Feb 2022 21:57:53 -0500 Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C34C60074; Sat, 12 Feb 2022 18:57:48 -0800 (PST) Received: by mail-qv1-xf2a.google.com with SMTP id n6so11962266qvk.13; Sat, 12 Feb 2022 18:57:48 -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=IsBxqaV4NE+StTwNs6VHaWSVWDLX6eURZgOwgnglY+0=; b=WpZqj92qxemLSk2Rw+xIyFlpa9FmqoInwquohPACuj4UHfLd4mB2es/AMBhrW1uAAg fy+CshOgHQNH/5hSn8cn6KEolTP8EhZf+0pLNWTToGSqummy2cgIKRQFhUPhpMdUoJUC Olr9c9d7DNnbtDVN1HS2GITtOZAzIIwx9E4QrcD0q3P5zFiZ4YBSqihxlVrPPCqJXzNn iinT0HVBeng8qOKl8BLsQ97yadw1ZFVR4PkbI17CzhOD9XSWcuiZ/fm/2D8FWAHCBd1+ 2/pppehO8+VXWzz9uKloL82/98wtffQcMjFMlgZEZA6tXVD1C3UHp0l6sDBOpIYH+cWt pkPA== 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=IsBxqaV4NE+StTwNs6VHaWSVWDLX6eURZgOwgnglY+0=; b=RPCekHK8YND0D576V17+Syz0j9+PZBuFa3bK5mWPxno4I8peJNmAXjrhWEPRZLzAUs LRHTKaNpjR+fwgNE7NOGkMqlhuewv8BjjhLJPT91So4E9GiW+xQ8YQwwJm2r/C/AVkyj rywk6aOv7GDgbXoah3NcuH6/BzCd7NrrbN8nD+Iqq+mytu939H5JBlUtBmCTvqtyfENk IaN6I6ah+hSq15M4aTgtzAgFN/0or4UES0myFkNNP3//9FfRLUVnz5QWPC4z7u8ZzIOc TIBXUkO3ImB6jBkrgBXwWLHKSMCDoejiLdvrM9jWINwTVap4xEB4QySADUZMoXOsUqzS FYjA== X-Gm-Message-State: AOAM532BkGourHzNBRFlAZrolCcY/Ipbq9AYT0xaClNbRbH5IA8DqunB ktBGhKP3C3RX1fpFfMuxbbYGW4lRTHs= X-Received: by 2002:a05:6214:f09:: with SMTP id gw9mr5813066qvb.84.1644721067515; Sat, 12 Feb 2022 18:57:47 -0800 (PST) Received: from shaak.. (modemcable055.92-163-184.mc.videotron.ca. [184.163.92.55]) by smtp.gmail.com with ESMTPSA id y15sm14551106qkp.22.2022.02.12.18.57.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Feb 2022 18:57:47 -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 v15 03/10] iio: afe: rescale: add offset support Date: Sat, 12 Feb 2022 21:57:32 -0500 Message-Id: <20220213025739.2561834-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.35.1.4.g5d01301f2b86 In-Reply-To: <20220213025739.2561834-1-liambeguin@gmail.com> References: <20220213025739.2561834-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 e67d9a9e6135..8eaf766e28cc 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 */ @@ -81,11 +82,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 * 1000000000LL; + tmp2 = ((s64)scale * 1000000000LL) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * 1000000LL; + tmp2 = ((s64)scale * 1000000LL) + 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 +148,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 +265,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 +432,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