Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp4791789pxv; Tue, 6 Jul 2021 09:13:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhXCydrUOMpqw2UbkuQ0QRjKxZr5sC6ed9dC9ptVpfMfsmfM3FrvD5oxnSoaXWkudiGsCZ X-Received: by 2002:a05:6402:458:: with SMTP id p24mr23846138edw.142.1625588017545; Tue, 06 Jul 2021 09:13:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625588017; cv=none; d=google.com; s=arc-20160816; b=nCFKBgP0fnv7q8BhiMn6Iijpa7TaYc5bUB0nSrlLfYEccRi0whr89u96BKKmRG+Xkc vV1XvyBfp8emzAjrSXGBlLXHHK9ZBMsqGq2uCvSEiEaUTGZwbd3YhkLuJy4FUKhw12S/ M+DHr3PK/7aye2pPD9ZMYrhL9989OT+3TdnV8PLJz7Bm5eGIFmktvFTPFMYi21WRMURb HoqReJ+/u+zE5kC2oSp8VTWMzjfX/5PMiwqSYUZ+3VmNrhwoXMe/nGMzPI1FcrXehH/s eIvvk10GtaWOzPeQhbbskQupmivbRSW6ly+wN41m9kux01LU2HX0yn7rbmrf3PlxwWgD AYtA== 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=zhQeMelYWNMHpFAMGtlyl1sUbg34IFFWmo4FOYtEzaY=; b=nHBGkVtAGFAeqmojNmZEpOunT9HDq6MuhrlDGbSsfUH8vm07XRCNOofMhAV6+35dB7 PK1/KZeIsyT/hleQXEWEJKpfEzEl9qMMPT5/Xo5HUqfjwUrFfWLxyuqyc/COPHHwRABX FAbgw1bM/mMSlazjwJQegEcVGgXT2nKk+dsZcohdQqRBSEj3K7x15Src9HSaRwVQD+6k JVGAlJs7Ps1QDHUdIHDlVyo5yXbGxF8VD3zBnp2XnBniht5+Jk98yIsSbh2YIfgl5VJz I+rTVKejXEGxxNtFT5dWNN2mMJ7lRZ5l37tbkaWZKLZzI92P9F3U4VuiT46cXqZLzOq8 f29A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=e0hCouqs; 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 ho15si9019537ejc.49.2021.07.06.09.13.13; Tue, 06 Jul 2021 09:13:37 -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=e0hCouqs; 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 S230209AbhGFQMp (ORCPT + 99 others); Tue, 6 Jul 2021 12:12:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230111AbhGFQMc (ORCPT ); Tue, 6 Jul 2021 12:12:32 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92478C061762; Tue, 6 Jul 2021 09:09:52 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id s4so1998867qkm.13; Tue, 06 Jul 2021 09:09:52 -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=zhQeMelYWNMHpFAMGtlyl1sUbg34IFFWmo4FOYtEzaY=; b=e0hCouqsW64PHJjeDsZcvlMX0fF+yG2B4GOys0Gnr2fBJpupvojuz8dY/Q9A8679FY S8MHosmBnXJ7UrYak1JL3q/DuhWU2au4hTL9YhQUv5PtyVnu+MLTFfhrAiKcCO9Vxthi u4rmSR8D5nakvZ2g1Wah9RqVN+Y0Uppn5wAxKYrEwBHTU3hDEOjmsdkFzUiQ9yI2qnK5 3ZemMJbto7Bu23LdOwROmHXKF9pUXluFI35j5Y8h4yEI8gjVHGB5k1DsVN+El1QWsLFs uEc2nEkgluTOYBL/S4jRmPM0sMrWVoAChqyAntF/quXozIGHLoTHCq4UUloX6SksXTQ0 hlew== 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=zhQeMelYWNMHpFAMGtlyl1sUbg34IFFWmo4FOYtEzaY=; b=KTUEh6CZuAT3nXrgYfYgsRCOiIBnwE6vvD5WcqBK77lGjIW0lpBZT2ywj16PBaoud2 YISDNRkjj5OrXvHGX3iieDaFazmyTzf8xMeAPTxr3iOumq53Me2ciAmxfFyesRcYMYbt ssA9c2nUvQTgGkyeclsBht4mZUD78uq2OkC+yGYGMgYeJ+5245vA0Q9RZ4PzRl+mBnF0 0pgd3LxKZtXH1R3BJVyJ3j8E4JKdSv0RRgDLnDwgwm+hcV7ilahmq0RHBjcnwmCHESG3 gSBeKwPIaSgtC8z9891GlwIBw5CF5w5K16c2rMxHnKbYPfS1rRd6I1DttqdoneT7rmvJ ibuQ== X-Gm-Message-State: AOAM531Ty97NOorWBCfK0MXeYIkSQDPlex4A6fCNBA12pjovYrVZbHK5 FLlac9/bpbCoKiHu1HwHcX4= X-Received: by 2002:a37:a688:: with SMTP id p130mr4641361qke.257.1625587791818; Tue, 06 Jul 2021 09:09:51 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id n64sm6995371qkd.79.2021.07.06.09.09.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jul 2021 09:09:51 -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 v4 03/10] iio: inkern: make a best effort on offset calculation Date: Tue, 6 Jul 2021 12:09:35 -0400 Message-Id: <20210706160942.3181474-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210706160942.3181474-1-liambeguin@gmail.com> References: <20210706160942.3181474-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 iio_convert_raw_to_processed_unlocked() assumes the offset is an integer. Make a best effort to get a valid offset value for fractional cases without breaking implicit truncations. Fixes: 48e44ce0f881 ("iio:inkern: Add function to read the processed value") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index b69027690ed5..5e74d8983874 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -578,13 +578,35 @@ EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, int raw, int *processed, unsigned int scale) { - int scale_type, scale_val, scale_val2, offset; + int scale_type, scale_val, scale_val2; + int offset_type, offset_val, offset_val2; s64 raw64 = raw; - int ret; - ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); - if (ret >= 0) - raw64 += offset; + offset_type = iio_channel_read(chan, &offset_val, &offset_val2, + IIO_CHAN_INFO_OFFSET); + if (offset_type >= 0) { + switch (offset_type) { + case IIO_VAL_INT: + break; + case IIO_VAL_INT_PLUS_MICRO: + case IIO_VAL_INT_PLUS_NANO: + /* + * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO + * implicitely truncate the offset to it's integer form. + */ + break; + case IIO_VAL_FRACTIONAL: + offset_val /= offset_val2; + break; + case IIO_VAL_FRACTIONAL_LOG2: + offset_val /= (1 << offset_val2); + break; + default: + return -EINVAL; + } + + raw64 += offset_val; + } scale_type = iio_channel_read(chan, &scale_val, &scale_val2, IIO_CHAN_INFO_SCALE); -- 2.30.1.489.g328c10930387