Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp4586833pxb; Wed, 20 Apr 2022 06:25:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvDdzVeFAr6jUfw+RcAP5/XFYkKdGuxmIgXkUVu8aHyNW07U9SMoSM3OQK06lLcWtQDGEW X-Received: by 2002:a17:907:968f:b0:6db:a3c5:ae3e with SMTP id hd15-20020a170907968f00b006dba3c5ae3emr18792650ejc.770.1650461140190; Wed, 20 Apr 2022 06:25:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650461140; cv=none; d=google.com; s=arc-20160816; b=C6MbzCFt667Motm5SriqdOYkZACMuyQgAaCvl9WQkROAnT8G/ABlB4j4DlQyFQCqB4 eBeaNzsT4QHNZF3ID0DXnF7UmooSOLh7XxJ8seSz0q03vvyelS238XRhoRLXAVbxOkJm YSwA2ZQ02sGSHhXLA2YW3eoUnhX8DErefVPp0LTDhoaFgeaEkEx70hRAgM+UCVhr/O1/ CjNVraF7LKqm9VptIc/mEKhsXXKq/AgQ/4KJROKpWJa9P+b4ApOY63jjBATx4VLiP1qN 6dxJid/WAa18jsdrPYuxay0+7vzLTe5wMoU1o4i+5DFRAJAz+1gBXdzaie+2fjYetr+V EjyQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=GQwujobtmOH2MoeQGa/+AXxcPjKZMoM2fCFRDRPjdvU=; b=ULwZ2DFJ+jnYgKCTAw+54oLW2vuEaDhlgPi/6jr9oxoNM1TPeOCcEbFsZwrajX7eSS Ing/8rR6s/RmxbLPvdd+U94cDUS86olwkUrPVpM5mJxMLQErRcGtgXEsqtW1YJTnsUTQ qLEEV2jCKncVv1g8MCTidkvK3tpeYEe1iCHQBi1x2860VG/b+XSDoZG/TLRnmBfSZ+Mb ZrL0e/0WM/wk1qdmmS69MbWgaXUp4fOIQef6X9CpCpcngAsxKew/Sj8/7UOSJavuHhkS K798ZmPrL2q/9QOUtXMWoompC8YJhIOapLAMiHYsahSyO7bHG1N5s/XDcfn3vKCIxLjg rVWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CEgSArJL; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f22-20020a0564021e9600b0041d78a7223esi1586337edf.597.2022.04.20.06.25.13; Wed, 20 Apr 2022 06:25:40 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=CEgSArJL; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243574AbiDRNmq (ORCPT + 99 others); Mon, 18 Apr 2022 09:42:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241517AbiDRNIB (ORCPT ); Mon, 18 Apr 2022 09:08:01 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52B792AE31; Mon, 18 Apr 2022 05:47:42 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 0848DB80EC0; Mon, 18 Apr 2022 12:47:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CF3FC385A1; Mon, 18 Apr 2022 12:47:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286059; bh=lFCP5nzLeSmkOcqS5XILgFyL0/GQwVTVtYdNABHyz08=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CEgSArJLZclO4u5KpkB0AtYDZpoeS8QKqsNaggox764VLWu6zHyClPU9KYdIo94PU 6jL+nBaNz0GnZtEMO3Y9M+6uk+OJ6AmEnqZfcG2pikYprSer5W2mu6JYzQ292p6QsK KLEK4vqx1J+gzibU/Bsi+JG+VMqoUsMohe0rpxWY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Liam Beguin , Peter Rosin , Andy Shevchenko , Stable@vger.kernel.org, Jonathan Cameron Subject: [PATCH 4.14 017/284] iio: inkern: make a best effort on offset calculation Date: Mon, 18 Apr 2022 14:09:58 +0200 Message-Id: <20220418121211.187957179@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Liam Beguin commit ca85123354e1a65a22170286387b4791997fe864 upstream. 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 Reviewed-by: Peter Rosin Reviewed-by: Andy Shevchenko Link: https://lore.kernel.org/r/20220108205319.2046348-4-liambeguin@gmail.com Cc: Signed-off-by: Jonathan Cameron Signed-off-by: Greg Kroah-Hartman --- drivers/iio/inkern.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -591,13 +591,35 @@ EXPORT_SYMBOL_GPL(iio_read_channel_avera 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 >>= offset_val2; + break; + default: + return -EINVAL; + } + + raw64 += offset_val; + } scale_type = iio_channel_read(chan, &scale_val, &scale_val2, IIO_CHAN_INFO_SCALE);