Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5116207pxv; Tue, 20 Jul 2021 20:13:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8VC7vRMrq2/AjSmPkMpobjYVspX7lRntO0yn7Gaeo08dOKpYFwB4s4T9lej5BFV3GMZSq X-Received: by 2002:a17:907:207b:: with SMTP id qp27mr36560700ejb.258.1626837190191; Tue, 20 Jul 2021 20:13:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626837190; cv=none; d=google.com; s=arc-20160816; b=hf9xJfl2n0K8KBrurQytmtaKbp1cdT7jUsXM7aAI/gv37XqNNoPEg3Twp1l7/SS1yb 4zKJnyKBc0oWrNEWN6fJHBZnJzgU2HT3dXhLPHm1MwHYms2O2h57h3G9MKF0LJ5DXr5u 5edHvFO4EoMQ9Htnl+3sp/3fzAMUQR0cOYkGdIVT3JGDVzXdTjdAfSm25cgD3512MWrJ h6ajlgbqohu6ibtwFbqdY2i2HYllpazFo9VjUm3kB6E9KrGBIutn/BIghHzXN2TeIhAf HKjfMwE+mHbs/EG1Kfs0/dPvvRZjdfnNOqRBCDK1D1icFOd/xR3QVwlhHJB51/88hqFM NusA== 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=Uel1CacJlG8Tv9L8WREBcW71pi4QI4Gna7JlbotRt0jfNp4OK/ePuai89RTacnjVN4 paT9wzWs8fG8zv53wgrV49UgbFmwUir4rPS3Nhlj5MOu/RJW3d0q5NyIRWuqXTkaBfYb EGkM3GCqHEkkXFNW3F+gCa+OPks8txQ3D9I3Ic8qtTk7Vy350/ZUIjXGgJ3QCi7UhGM8 9PMyt86nEStRzDJjBSopRYYa2q71qkGM/CmROm2y3UvgHT2qfsATvN+BtOAqAzfv236J jfEi25I2Dn/WA6j2I8XtajGvEcrms5HngjL15uivzjh/XtA/2Acs93aIOOtAQqPt9Dmk gZag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=t4HfGx2m; 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 dy10si26349070edb.218.2021.07.20.20.12.46; Tue, 20 Jul 2021 20:13:10 -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=t4HfGx2m; 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 S231733AbhGUC2m (ORCPT + 99 others); Tue, 20 Jul 2021 22:28:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231357AbhGUC0Z (ORCPT ); Tue, 20 Jul 2021 22:26:25 -0400 Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5770DC061766; Tue, 20 Jul 2021 20:07:03 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id l2so997565qtp.11; Tue, 20 Jul 2021 20:07:03 -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=t4HfGx2mRk4Enty4f/qmaF+qBssAiTHaDR89eTSAuc9Y2UZX2oaM3M6RiSx3aMpxoi qEI/G18uDyuwIkfUYYjjsd75LNBjxa7eIueTXTcOEWfgjdPfBnRDF9G+sAjcvqxpgkK1 t8e+wXwT/41xnf50dNkiSoNpt0jOd8H0yQC/lldijsZHfEGhHlpKxK0URqy6EPf4Zbnn dZD5L1YrZ5IPMLOHjWkE2TvKl6DFA6uvaDhdEf0lbQCNeGmg4/rEL0GbypH1BcOjW4SM h7Tf0Zu3T4JysRNiQfW07G/0eS6dR1OD9dKZf4jOTiv8WiPHB2dqkj21pxpTR1LuZ5z1 snXw== 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=P9u5Lpl8u9h5IZEGU4qTo3qCDgJe2SPBlZ3jHUViHy2oJ7PLu9hO0XCNMAXU1zU0Ae Qv4hyUD9p6Qzf75CGXSAjtEdqwRhNFLbV7omDDsaw3Mw/puKZR5ZI4+F78HbM4DLgXnl M7EyDrrJKaknkp51FfBmj40k/yCjp8blNmkR4rnuFylmfF8Et5DQGWP0Q257KG0JjDUr G5h/Vh9Tl4K1sNICrt239RDqNAPW/SJsoX2EO6tNpoEccH97gBHpN9x3fm95mVjKWDzg iNvA6sH+MoVX4i/hzq3aYcepsvsvZfy6AXujwxSs6EN1XWl1+cyxE46QnGqttIQagB1U 2FaA== X-Gm-Message-State: AOAM532AgiuuzyrSS5Et654wnW3mSLROMPOWFJSoOeGfJjsHzPJa7uFD e1NNhtOAnUlNYMEyQWnEdcM= X-Received: by 2002:ac8:7ee6:: with SMTP id r6mr29478049qtc.199.1626836822598; Tue, 20 Jul 2021 20:07:02 -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 g76sm10561024qke.127.2021.07.20.20.07.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:02 -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 v6 03/13] iio: inkern: make a best effort on offset calculation Date: Tue, 20 Jul 2021 23:06:03 -0400 Message-Id: <20210721030613.3105327-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210721030613.3105327-1-liambeguin@gmail.com> References: <20210721030613.3105327-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