Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp112672pxv; Wed, 14 Jul 2021 20:16:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyG+6FjSFyI73+EwphQJEndMD3cNRtsmxhOrk89Q5A+4Hu5VwxAuaqmOjyBZQ7pEQ4uQ3nX X-Received: by 2002:a17:906:7012:: with SMTP id n18mr2211531ejj.236.1626318984234; Wed, 14 Jul 2021 20:16:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626318984; cv=none; d=google.com; s=arc-20160816; b=arAGSxHOD196gValXng+DlSsIO7vM5cBn0NVIxTnkrla8qh7Ii0m00PdqyT0o3KTgG YqG0ct0rLijz6XwdZJVR+8e1gh4F73Did+oRD+Yuh8hUz2Zt7upxJUdZr3rYMVBXzOob XFBLDKN5XDOLfTWpNbpj3y2/FIdjGewGIFPIMGAYAA45xfOSU0TOBBAySGsQRRt/jj4A g43qDLkhwk2Rvy5aWVOoSsW6q5Ts9ZCGF3mzRnum1jJMCmq1i8nWtOurHtRbIgPTYNpG KSodgvpkHq0xEzbeQYzxkW5JqpqpME4wWbzW9WH3cHiV4X5ttTY8ECI5Kb2aJe36s/B0 XNSA== 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=d7VqdoSjONZ0M1T6Blgsbc7cMPOEkLdKmb5GaEc5a8ngR3mtHUiHOyvXusbs0Z9T5f ZmulAmF3eAxvZXEua7WXrCGp554hNW2go1R8aDrZeEunOLniYvjQmo1sd5cIX60xppYk xK3pabIp/Y+4/tdcWQsZ7+EsvACq085m7G7wF1BwPrKnSAGOlWqEjSxKehVokMX4cmDJ +n1zjjf31ATu6eT6k937c/r6qXPOKu2IYK3L6/5Jsnh3ANjCUaVnr7wfkXm8R69ceVXa DoM1Ek50QLFlCjR8f7K5bfp3HXCOVt9ILn3vVj1swxNWflj/4ZTyGDaU1PlXQR2xj5S4 CylQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ktixEYDE; 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 l12si3909859edb.589.2021.07.14.20.16.01; Wed, 14 Jul 2021 20:16:24 -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=ktixEYDE; 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 S233380AbhGODPh (ORCPT + 99 others); Wed, 14 Jul 2021 23:15:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233203AbhGODPe (ORCPT ); Wed, 14 Jul 2021 23:15:34 -0400 Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 320B8C061762; Wed, 14 Jul 2021 20:12:40 -0700 (PDT) Received: by mail-qv1-xf35.google.com with SMTP id i4so2147996qvq.10; Wed, 14 Jul 2021 20:12:40 -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=ktixEYDEVNnbF74lVxjv9jgUdBZOLbrHEfbaIXe+S9yjG0+RMCXjpopBCzHmDxq+6R Dj4c+QK0cQtwkV6/ZV8sPSZILtTLZhP3I6SUxzjiRsGgGTz5kxfmQ98IlJbfAboD73OK owzBT9PM9wsTNlLdKyLSDa1/kqLWr1jVNNp1Y9K+6bGERaA/I9RaGF4ERnBmdM5l1Jca FrGwtJktm08v2gmdEJO9Q+bVY6emN4jU2B4igq+HUKHWqftSMZLq1vLUxHZCGkAbjYFx lU/6qWidSu+UTMtSz3pJwg95vXnZpAMCS43ZBjQQaY/pGzp9veUh9hSfcRUFsyMRm7F4 Cvqg== 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=ASmFYITtJ/CPlyzSFvO6llvfygHsXMPfLjj4g9136rBS59ahfkl+tZBGDwVETFBb0R t7IahSX/RCTJrFXiroZggYCq7ojS6vV76BvTZOUbjgNhIZuOmKuO+5EZrh/jsD3fVTwD hkaqjR95crbk71zXZEzj07JU8thSa6lKKRFZkrJKdXW+lKd5VPghuOdiF5PN9qeYt7Ze WXJ9WAnokCTg9o/NJng2bfSWCGX5GRKWmtns0Vv1TyZb7OQlSm+8v5wsRAk9/cYLg1TF pBy2P1z2WC58pIanHOiu3AE6/FmPR7S21JofzDWXr6CkjIiPu1YPtwU89Mrh2y/GoBWf iQFA== X-Gm-Message-State: AOAM531/EnRiIft81cH4Hpeu1b/SZGfnElOF8akZ3bYsJzqgLNOOuj+L STEd6Fhn/I7+IheO2QuWj6s= X-Received: by 2002:ad4:5ca6:: with SMTP id q6mr1604863qvh.23.1626318759467; Wed, 14 Jul 2021 20:12:39 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:39 -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 v5 03/10] iio: inkern: make a best effort on offset calculation Date: Wed, 14 Jul 2021 23:12:08 -0400 Message-Id: <20210715031215.1534938-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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