Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1101953pxy; Sun, 1 Aug 2021 12:41:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXPzkHNteNA6ISFsgvcG03+9r9eSnd46uCTLP6kRRYLJggfBv4omBtJZO3QHV7Dcvg+tWt X-Received: by 2002:a6b:4f17:: with SMTP id d23mr8693850iob.18.1627846895705; Sun, 01 Aug 2021 12:41:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627846895; cv=none; d=google.com; s=arc-20160816; b=u3A4FGidR9LUygdOPDdcbtwPIfjvXZzsQFC2ehThdpg+o9+xiUIxlVzGZlYIMkKQcW 7LiVFppx5jK5b0/XvxSDSc77WpwuWQkeYFvVYOkzHZPixYloh5xyg28nhjzBu7AdliYe PMQKotFPDFKwPFkDpQ0ZT6/ugFIdOQQvPjLX9MNrfNzfPVZOdgsGNM1tcCLgiqQnHOf6 jxm6WtVXhhmb5ASoOrnL0/BwqQiORqIlFB8ijyqfZC4051Qpnx9m3wGiCXrUQo8s/Um7 bghzZdzTJuw3jzFz+axChP6k93bWxEdvLVfArBbGTW3/2kFg6R/OKUOT+nJdPhkYGUd4 gqYw== 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=VaYrL33jnxUhwrfSflrkn5pSgjeNEynQajK5AMWhMiEEnjm5pW1IDqkrlQPYwHKq0v GvIA+PJr4koIMbgDLsJGJHgYvxXP8ZcB2NqHZFYf9rr5Q44XmGRKRbcia2r3AAQWounD 03KVBk/zNj1oFFmB7uFwTFDkEHy6wUPVzbjwJSc0SqhSYywOAUvzhV4XBKbFFWH7OS/j nGbKKqCym2dte2g6BR3oZLO/PFqqEPMsgQ2I5dEhaP6jfpkISYUf3JcQs6/dam0UXulX 1H7lN3RKdO54z4MvewyNoUhunTsuDafzp1ij40bMlSGy2LjVwGqNX/lxIrIYKHw5zw1j UlYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=l5Kb87vn; 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 m1si10904485ilu.13.2021.08.01.12.41.24; Sun, 01 Aug 2021 12:41:35 -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=l5Kb87vn; 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 S231640AbhHATkk (ORCPT + 99 others); Sun, 1 Aug 2021 15:40:40 -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 S230225AbhHATkX (ORCPT ); Sun, 1 Aug 2021 15:40:23 -0400 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60989C06175F; Sun, 1 Aug 2021 12:40:14 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id p38so7911721qvp.11; Sun, 01 Aug 2021 12:40:14 -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=l5Kb87vnrHyjiimiX1rx2Duk6q8wFU9ih6mRQNbq/BMzM0Z5Ulig0sQUeyQ82GzMHQ 8icw6LDh8AC4K0U1EC6/J16qwctiwWkJP9uwH8RflV/KRjKZa2Cv9vJfDrjQ6lOATUTG nJwEGZvl1rbmvaKLzlOU8flUnfRurpF24v8KCScEIbvfwACLQLh+2Bgs52tCtnqWoydy 6fAWMtX7CqpaaGX8Gz3QT+lCMOGTQKTrQAqqkHeCTcmEYTMlTcz06cH71e1AolUYKKUO pgOTtSSfvlwhmZxW+IlzSon07Voa3pQXFSLkCc6Z0UK0gtfElKhTkDdj3DYjzhVdvBwj rHhg== 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=Py/F+ZV6TbSOt53a3lKjtF84QO/S78C0mVTQEk/ysbMBePKZbKbOqsF+Hvph0rn56l rpltrrq1hwNc7AfM5hPSPXWnLWsg5aDMj1ECD3MS3p/azYKG+vUzor3alMpYyeY0L7bi Tm9Zed0NS25bB3J+gc1ktMQiZhDbpgK1ZQD9s6yf9NfzYNRfjbLh/UnNmhTptucayZyb YajnZq6q18K2EdHH1IaJ1mRVJLWmMcIWFCcPrFMCQheCBqql7u9O57DLHt4gwZUyUsZy LTWOepKsxOCZBj/35NzY0u0pVyECCWL8Bw2Fs07GQ4H2ZKasG7ElAtNA9Bi/Yl9pwNBJ FFZQ== X-Gm-Message-State: AOAM532iQL04uyOILf/QOftQAKwHhR4AOeR0O7pBieLqAhSRkQcITXdv qvC/hOF6gZqypkOLN8XsySCp9BZVSbaLHw== X-Received: by 2002:ad4:5ccc:: with SMTP id iu12mr12331806qvb.47.1627846813573; Sun, 01 Aug 2021 12:40:13 -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.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:13 -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 03/13] iio: inkern: make a best effort on offset calculation Date: Sun, 1 Aug 2021 15:39:50 -0400 Message-Id: <20210801194000.3646303-4-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 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