Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp684993pxb; Tue, 5 Apr 2022 18:43:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJznhab8ToC9Kcce1aLlpFQadz7eypYoMGCVX5Ni3BXFQUKxKJgF6wrmCBR5itOUn9FMOEg8 X-Received: by 2002:a17:906:948:b0:6d6:e479:1fe4 with SMTP id j8-20020a170906094800b006d6e4791fe4mr6126530ejd.240.1649209382409; Tue, 05 Apr 2022 18:43:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649209382; cv=none; d=google.com; s=arc-20160816; b=VtZE0Pzk3aDieScStbyeA1pLHfl5a24mNkxfPZ2RTsXngDYwqWIB/Nylm2I4ByCX1F pj90KYRt/DD5TJDyhIWfqc1haWmq8jshCgnueOnPwwCrkKRP5l1hVjzwkVg7V9Tyd/BS Oia/IntRSGC9szceBDnCbOj1t97UxwOSdOsbtXa3Kmi2oq0plB0BpsE6dlGU3MQmV12z rZBNFGn9sk24inqPWZ+sLiemyPzkQ31fvI8AzdCXGz7r0RhUF3yE1bJT/Z6Rgg/simNC ZE7jqU5H0VIYYj3vTbCNOWvQwePRFn6kjCb9F0JxOwHp6ElpuOUcXv6Pl6kaexwiMkf/ X/4g== 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=Uz6avuhOKBQsu8s+JnIPAEN7jc8AJ+ybzQVbIF4F5tQ=; b=Yr07S54ybLhYDUhZ9ypWOMrAP2X5pI1j/a/anjO07zmJaiXXSXGLmhmWvyqlA+e7i+ 2RhFp2dQv6z1WYyBU6X1WIQasXMRKBU+Wm6MMeV/ip7Rc5/KHA4y9wbXDCMbSFRW9Ozo p5svwbZMaMfgR80RPWltVpxX2Wo4587GDlyX7BTIs9/rT/hdVNqBrl8Rh92tRkn1ScDF EIIYOnb6a66l12l0khwl0M0qqeGBTg41Dow6jG+CtPdSpevqqr65dPidABGCeJtH1HcF pKdQoXPZyaTb80dFmcBYolxb8n8e/Is0YbJSNS6qJGfYJwwef5y/M3qUFi38J5bs2cah eFJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kZPh9gPn; 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 b10-20020a056402278a00b00418eca4233csi11432342ede.470.2022.04.05.18.42.32; Tue, 05 Apr 2022 18:43:02 -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=kZPh9gPn; 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 S232075AbiDEHpq (ORCPT + 99 others); Tue, 5 Apr 2022 03:45:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232033AbiDEHnO (ORCPT ); Tue, 5 Apr 2022 03:43:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 578A5931B9; Tue, 5 Apr 2022 00:40:23 -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 F25CBB81B92; Tue, 5 Apr 2022 07:40:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42A26C340EE; Tue, 5 Apr 2022 07:40:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649144420; bh=h46s61eTOw3K/so5Fb4otBH2wA/GpX9mBE1XqOcPSco=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kZPh9gPnI1LD9qzXOk1C2AYbfRtrE0MtlWqR0FJN0U7x76IqOZUpp97ptESzccuT7 zsQbr8Qji3nKlcrTIdyL1jSoCOl1qKvfs6P1yGlRYoPZjSjDUgKoUE3qBygR84vPZW 2GbF9JOgYSL4+TkOjz5pawfNeKfgqWg9ttv63mrY= 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 5.17 0038/1126] iio: inkern: make a best effort on offset calculation Date: Tue, 5 Apr 2022 09:13:05 +0200 Message-Id: <20220405070408.675586397@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070407.513532867@linuxfoundation.org> References: <20220405070407.513532867@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.1 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 @@ -595,13 +595,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);