From patchwork Wed Jul 21 03:06:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389957 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D780C07E9B for ; Wed, 21 Jul 2021 03:07:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 156B261029 for ; Wed, 21 Jul 2021 03:07:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231144AbhGUC01 (ORCPT ); Tue, 20 Jul 2021 22:26:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231324AbhGUC0Y (ORCPT ); Tue, 20 Jul 2021 22:26:24 -0400 Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A6B7C061762; Tue, 20 Jul 2021 20:07:01 -0700 (PDT) Received: by mail-qv1-xf2c.google.com with SMTP id o31so306795qvo.1; Tue, 20 Jul 2021 20:07:01 -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=aLIjEv3Y3XGxT5C/sRBa0wIugXh2Hwubx4IA7LHkqcs=; b=LQE4Y14dktjeNg5+WJ5Yv8IXF2bJwzDDhOIJtWOvBucLM0aeie/s+V5AxnrmAS0nsd S+x/I2YCVMSMQhR6/FEhNZAuP1lkxZbLuW4OEjoLDZAKWmR6i9Rg9C/gjQTcBeRvha5J yCV+l2EBsZMiKa6h2dL/zoXcDDRtHE1GSoWNtjAAC/HSovnjtRlNNEmqh73PIeNkDRJ3 3IVOQp4TexIsOIfp3xvnC1P6NeF3MsLyMB0ZytxqhV3wws+7jqewbpPV/lAM1/9N8Gwq liP0U//BZZOYHbzgn2AvVNv3aQ9F80fbdF53D/7onL0KEfe0CRzmA8j0QykM6R6rIzUl pQBQ== 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=aLIjEv3Y3XGxT5C/sRBa0wIugXh2Hwubx4IA7LHkqcs=; b=lBc54INnC1ACarARTTpTk6xZ+QBYGEUQ0kzZZjLsIr+U2YowlS9Ol9CEY3aQaoXbb7 /yam4pABvTFICLW2AqfRvMS64uW10vRakk4cpFtiom2kmMGvgbCy6/NogSZQIhmiozhW Bs+jfQOWjJkoQMp74kPd3UMlXLBb/mIJkMOezCudu+NxPoyOWhDEmq5SrmEAEgIaqBNp ODN3MIPi/1CPLlfcRsDBWvQIkUj0mF4ZS0AC6AUNs0jtTX//6y3SHZqiOUibGfrrIIGx sgmMoZtXxTdN2DjRpwpybOE7H3DSejGsUtLPvzR1mFQYsm4Y2M9Eg0gsqEUBaxJ0JF7s kBpA== X-Gm-Message-State: AOAM532IMsBqSBQWfLB7JosFw0xzQZ6V5XkcjCkno9Cu6FcfHGWGkn8x soh3FFT+lqzVrSZLcRkF9GI= X-Google-Smtp-Source: ABdhPJwOZm6u8WfQCUC4PgRTb6IT2YUmJJE3CvbfIeizAjG+rFD8Bb+d0k9CelvrYXwwdBf1WxilJQ== X-Received: by 2002:a05:6214:1941:: with SMTP id q1mr33142166qvk.35.1626836820699; Tue, 20 Jul 2021 20:07:00 -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.06.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:00 -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 01/13] iio: inkern: apply consumer scale on IIO_VAL_INT cases Date: Tue, 20 Jul 2021 23:06:01 -0400 Message-Id: <20210721030613.3105327-2-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin When a consumer calls iio_read_channel_processed() and the channel has an integer scale, the scale channel scale is applied and the processed value is returned as expected. On the other hand, if the consumer calls iio_convert_raw_to_processed() the scaling factor requested by the consumer is not applied. This for example causes the consumer to process mV when expecting uV. Make sure to always apply the scaling factor requested by the consumer. Fixes: 48e44ce0f881 ("iio:inkern: Add function to read the processed value") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 391a3380a1d1..b752fe5818e7 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -599,7 +599,7 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, switch (scale_type) { case IIO_VAL_INT: - *processed = raw64 * scale_val; + *processed = raw64 * scale_val * scale; break; case IIO_VAL_INT_PLUS_MICRO: if (scale_val2 < 0) From patchwork Wed Jul 21 03:06:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389961 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6AC9C636CE for ; Wed, 21 Jul 2021 03:07:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D3DA5611C1 for ; Wed, 21 Jul 2021 03:07:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231666AbhGUC0g (ORCPT ); Tue, 20 Jul 2021 22:26:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231359AbhGUC0Z (ORCPT ); Tue, 20 Jul 2021 22:26:25 -0400 Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 743DEC061574; Tue, 20 Jul 2021 20:07:02 -0700 (PDT) Received: by mail-qt1-x829.google.com with SMTP id q15so1065099qtp.0; Tue, 20 Jul 2021 20:07:02 -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=OKwbA7CkZ/dV0H/nAmt5zTOn4ixRO23QkLcRozVJn9c=; b=tpT0Bo0saWDiswje5emv5emtlmtpDKJqhEw32Dm+/AqpGcCMns0YWXgNkSEDaFyJ6s gCyTU23DQt4+eAz8NiDnjC4qQ5wecJYrpnyj7oc1jUsGlNPYA+AtRw4Nmb4ZSq2l3EnR nXCDjzNAajq1GziHbTokT4iHxAmF0hzDFJT2CkpXnojc0anOxsQqTaofChO+mfEGZQPs d0fZBuACGXNQhEsE2tCh5hHgfAAXliCY3iqAvxzvuR4tSLjFTWKUp7jAao1KIkUNY8Jy /wckLOYCx3bu+GSUPKdZpWfyIDTWlPkZHeFh+/yevuANDyndr3e8Ml5Ctab+AnLSprn/ FJtA== 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=OKwbA7CkZ/dV0H/nAmt5zTOn4ixRO23QkLcRozVJn9c=; b=OTmKuD7IM7aKX4JxA0C2GJUiQ4q0EgJmYObKA/iMrb581FOHsdzLGNF9d87Y65lZ7j p3dEDY1EcAYGQL4CcxcyOUXiw8hN1On+6Mlz9ZafYdxIZHXHDj8OZbNpjPYfV4ppS0S+ 6LHipblJQJCHiD8Z1GdSoRFyFr+EkQS4LBIjKOSvVHEkEgQrQ1zDhgNnJ2KFbZaaZRdg Jlvv2GAK3N8lDika5NzcPDtaxzL0HI3n24a/MdrEqXnLNl1xEyMnFvBHS8TpSu+zb1h+ AT7230CUCuSBja2LD0g4D8hYKENVpPF8gDy4mtyWIVXpQL6O8TIksE9q9QD4Xlj38vs3 Uuxw== X-Gm-Message-State: AOAM530hHEM0yIS3Zhk4d6CIilLohIMro82gWxhRmpcSbRN+5a1lTgUa 7+NRWPhNtkYNvHU8RhurOSE= X-Google-Smtp-Source: ABdhPJxP9OgxwC3BWp8HPuEJCPPTiTkrLCVMGTSbFULt6l66+Emzl1EerVGjeAa+ySSty5NRjDpVGQ== X-Received: by 2002:a05:622a:d5:: with SMTP id p21mr3817934qtw.75.1626836821656; Tue, 20 Jul 2021 20:07:01 -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.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:01 -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 02/13] iio: inkern: apply consumer scale when no channel scale is available Date: Tue, 20 Jul 2021 23:06:02 -0400 Message-Id: <20210721030613.3105327-3-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin When a consumer calls iio_read_channel_processed() and no channel scale is available, it's assumed that the scale is one and the raw value is returned as expected. On the other hand, if the consumer calls iio_convert_raw_to_processed() the scaling factor requested by the consumer is not applied. This for example causes the consumer to process mV when expecting uV. Make sure to always apply the scaling factor requested by the consumer. Fixes: adc8ec5ff183 ("iio: inkern: pass through raw values if no scaling") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index b752fe5818e7..b69027690ed5 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -590,10 +590,10 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, IIO_CHAN_INFO_SCALE); if (scale_type < 0) { /* - * Just pass raw values as processed if no scaling is - * available. + * If no channel scaling is available apply consumer scale to + * raw value and return. */ - *processed = raw; + *processed = raw * scale; return 0; } From patchwork Wed Jul 21 03:06:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389959 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47089C636C8 for ; Wed, 21 Jul 2021 03:07:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2F28761178 for ; Wed, 21 Jul 2021 03:07:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231580AbhGUC0d (ORCPT ); Tue, 20 Jul 2021 22:26:33 -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-Google-Smtp-Source: ABdhPJwk5XWCwKz2yJJDCS5aofZDVi1r5JysgMI4wNphzDdo1ML5lupw1P/Ec2aCdaK0gmB+GbP27w== 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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@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); From patchwork Wed Jul 21 03:06:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389991 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8E82C636C9 for ; Wed, 21 Jul 2021 03:09:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9688D611C1 for ; Wed, 21 Jul 2021 03:09:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231724AbhGUC0j (ORCPT ); Tue, 20 Jul 2021 22:26:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231411AbhGUC00 (ORCPT ); Tue, 20 Jul 2021 22:26:26 -0400 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50B4EC061762; Tue, 20 Jul 2021 20:07:04 -0700 (PDT) Received: by mail-qv1-xf2b.google.com with SMTP id h9so322377qvs.0; Tue, 20 Jul 2021 20:07:04 -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=cUoCjxVUE34s1+eP3pk70mRzAMWsM0v+FDRsaXLy0ew=; b=O+4oPdsTBvXGRKuCjChd/jvK/2guiWMgnD0KnXzOxTTNHJ0a0g96YeOnzvaXVB2zTS 3NVJXeXOcyXtbIczvSmDhW1IVpOYh65QFek85KLh+OIlWbIOm5BOmjyM/I0271dELFZY 6Uqyx9p9RRF/1mp5oKa2wPKjZDe/f8omgihf9JGZ+gHry/zLdJSnWV4vovMwj65YnRsr YBrHwffVZw1/PlGxEHGVo5hyvqoormKeDjxePVbODkQ67/8j1Gv+BeCeDZ5mUTJCydRY CmxlI/MkNKuw0BcjWgDcWIoLkO8mjgI4OjKLIK0eq+PcyulXKnDwQx7kSEnkTri0W5Gf tMpQ== 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=cUoCjxVUE34s1+eP3pk70mRzAMWsM0v+FDRsaXLy0ew=; b=WdQmArVV5tqfpKwY+Z+dLaeA8GLZ1E/77yep0s/Mu4Ra4gQi4w+RRzXOXwmdtNbxcF qlOxMoZ/PtsL6WHGcvWPhCzedrrF3xQv0hwC5TtdzzZtYALB+h5jQruZapG4mD4MTLZP 1AmTex0lwX2Mqvg3LHFa/FxkPYUfRRNsWBGyepsQ7aFZG0Yd6PQn4LjSt1RuK/u2zTQJ RNiddk4Iw/zY+GMMM2UbASg+b2eUe5murd8sxaZ2/Oc+DaQgMiKoV7w4jbZlNvdXbOms Wu4ets85OOBdJ9rHAw0YlLIFFnLep09ntkZYfhg7mcKvxvp2p3I22dchHgaLrIsUceBf 6BCg== X-Gm-Message-State: AOAM533v3+nDEzuOMzZzLBVizFQjD3UlBmHWf4iP4nIqrHwstlgTuLsf cO9NkpgdOaZWxBUXLT+GB60= X-Google-Smtp-Source: ABdhPJxFvDK5NKxP4rXDqntI8iCputgTr+LprXy/hDbz61H6MJhqhY/teQ0KkpDziI0tjStQS09obQ== X-Received: by 2002:ad4:5b86:: with SMTP id 6mr33667089qvp.17.1626836823490; Tue, 20 Jul 2021 20:07:03 -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.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:03 -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 04/13] iio: afe: rescale: expose scale processing function Date: Tue, 20 Jul 2021 23:06:04 -0400 Message-Id: <20210721030613.3105327-5-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin In preparation for the addition of kunit tests, expose the logic responsible for combining channel scales. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 65 ++++++++++++++------------------- include/linux/iio/afe/rescale.h | 30 +++++++++++++++ 2 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 include/linux/iio/afe/rescale.h diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 774eb3044edd..d0669fd8eac5 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -11,35 +11,46 @@ #include #include #include +#include #include #include #include #include #include -struct rescale; - -struct rescale_cfg { - enum iio_chan_type type; - int (*props)(struct device *dev, struct rescale *rescale); -}; +int rescale_process_scale(struct rescale *rescale, int scale_type, + int *val, int *val2) +{ + unsigned long long tmp; -struct rescale { - const struct rescale_cfg *cfg; - struct iio_channel *source; - struct iio_chan_spec chan; - struct iio_chan_spec_ext_info *ext_info; - bool chan_processed; - s32 numerator; - s32 denominator; -}; + switch (scale_type) { + case IIO_VAL_FRACTIONAL: + *val *= rescale->numerator; + *val2 *= rescale->denominator; + return scale_type; + case IIO_VAL_INT: + *val *= rescale->numerator; + if (rescale->denominator == 1) + return scale_type; + *val2 = rescale->denominator; + return IIO_VAL_FRACTIONAL; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = *val * 1000000000LL; + do_div(tmp, rescale->denominator); + tmp *= rescale->numerator; + do_div(tmp, 1000000000LL); + *val = tmp; + return scale_type; + default: + return -EOPNOTSUPP; + } +} static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); - unsigned long long tmp; int ret; switch (mask) { @@ -65,27 +76,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev, } else { ret = iio_read_channel_scale(rescale->source, val, val2); } - switch (ret) { - case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; - return ret; - case IIO_VAL_INT: - *val *= rescale->numerator; - if (rescale->denominator == 1) - return ret; - *val2 = rescale->denominator; - return IIO_VAL_FRACTIONAL; - case IIO_VAL_FRACTIONAL_LOG2: - tmp = *val * 1000000000LL; - do_div(tmp, rescale->denominator); - tmp *= rescale->numerator; - do_div(tmp, 1000000000LL); - *val = tmp; - return ret; - default: - return -EOPNOTSUPP; - } + return rescale_process_scale(rescale, ret, val, val2); default: return -EINVAL; } diff --git a/include/linux/iio/afe/rescale.h b/include/linux/iio/afe/rescale.h new file mode 100644 index 000000000000..14d4ee1227c6 --- /dev/null +++ b/include/linux/iio/afe/rescale.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2021 Liam Beguin + */ + +#ifndef __IIO_RESCALE_H__ +#define __IIO_RESCALE_H__ + +#include + +struct rescale; + +struct rescale_cfg { + enum iio_chan_type type; + int (*props)(struct device *dev, struct rescale *rescale); +}; + +struct rescale { + const struct rescale_cfg *cfg; + struct iio_channel *source; + struct iio_chan_spec chan; + struct iio_chan_spec_ext_info *ext_info; + bool chan_processed; + s32 numerator; + s32 denominator; +}; + +int rescale_process_scale(struct rescale *rescale, int scale_type, + int *val, int *val2); +#endif /* __IIO_RESCALE_H__ */ From patchwork Wed Jul 21 03:06:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389963 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9C3DC636C8 for ; Wed, 21 Jul 2021 03:07:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B0D0E610A0 for ; Wed, 21 Jul 2021 03:07:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231856AbhGUC0p (ORCPT ); Tue, 20 Jul 2021 22:26:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231432AbhGUC01 (ORCPT ); Tue, 20 Jul 2021 22:26:27 -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 3BF8FC061574; Tue, 20 Jul 2021 20:07:05 -0700 (PDT) Received: by mail-qv1-xf35.google.com with SMTP id ay16so282612qvb.12; Tue, 20 Jul 2021 20:07:05 -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=KxRWxmUWooh8K8wECdDWjv2qMB1v7LjacHyh+1GZlIs=; b=jG7T/o6af+b1FGPAiPh53sHSINRFk6kcIBKwTZ6RDWSgRqEclxkI42G6t3wsSgKDBX /BQpGNps3TOb34oUWrmOVluFYccEAxAwXY6jFypNvMmYtnOQJI1i1I59xoOWGLWHAaF6 31NnBl60aMIw382WEozMGYQLpqvaNmJOGTDgKZQzlSlAnhpP1e5lC0e9/kQSxVJgFlcg +wGtNMGRX6jXdV7sveuDZ1a8Nm//i6RqnPnLO/Cvcdm0DMuCGYXlxbAzCu0t2ye3tELi BS6IRR6d3+BxwBODc8fQCciRaQ2g8yvP3UxSYO/rbZTVXTcvt7yVTjn6J/rcivk2X/Ro 8uVw== 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=KxRWxmUWooh8K8wECdDWjv2qMB1v7LjacHyh+1GZlIs=; b=WPRw3TD1zxpdGNCW/RFHZnjM/AWFOYB0LyQsjBNQ6H61IE8pra64eVZTWmbRjHoeiO aPC375ZHB7ekCOg03oQY/OnNjCGI7qSmFql9cu1k6jV5Xx46dwqU7MDs3jUL7RclWh1E 1OjakvIB7QKSmz60v2saN7pjBb+xXhJvGwzppnMMAV6meZtIVWeB+xd48jeUSSiM9Wut peYezumePPVwHrXf8ElXYm3TlVuYS+AKzxJT1ysReZGqSwvZyulzRCDpq57LmJSl7fmF otnYSco75bRBoWtttBWDfLrKKX5I9w39qhS5AHjP2lQXarn43rHQi1lsqjryzabCum31 4APA== X-Gm-Message-State: AOAM533he1zcpCO9P0sp8ga9p2z4a1JneYTKOqeU7G9lf4CPKGtHDpxz lU/tVc5aySlmf0XmwjDDMY4= X-Google-Smtp-Source: ABdhPJyxBk5XcHK1Px5RdTIVQ8bYgzv5M+hgxwW6B8rp4MVoE9vKbmq/+LEk/8m6gMUfVdjU4MG2OA== X-Received: by 2002:a05:6214:242b:: with SMTP id gy11mr33839017qvb.9.1626836824487; Tue, 20 Jul 2021 20:07:04 -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.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:04 -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 05/13] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Tue, 20 Jul 2021 23:06:05 -0400 Message-Id: <20210721030613.3105327-6-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin Some ADCs use IIO_VAL_INT_PLUS_{NANO,MICRO} scale types. Add support for these to allow using the iio-rescaler with them. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index d0669fd8eac5..2b73047365cc 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -41,6 +41,20 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, do_div(tmp, 1000000000LL); *val = tmp; return scale_type; + case IIO_VAL_INT_PLUS_NANO: + tmp = ((s64)*val * 1000000000LL + *val2) * rescale->numerator; + tmp = div_s64(tmp, rescale->denominator); + + *val = div_s64(tmp, 1000000000LL); + *val2 = tmp - *val * 1000000000LL; + return scale_type; + case IIO_VAL_INT_PLUS_MICRO: + tmp = ((s64)*val * 1000000LL + *val2) * rescale->numerator; + tmp = div_s64(tmp, rescale->denominator); + + *val = div_s64(tmp, 1000000); + *val2 = tmp - *val * 1000000; + return scale_type; default: return -EOPNOTSUPP; } From patchwork Wed Jul 21 03:06:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389989 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99701C636C9 for ; Wed, 21 Jul 2021 03:09:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7D71661029 for ; Wed, 21 Jul 2021 03:09:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231786AbhGUC0l (ORCPT ); Tue, 20 Jul 2021 22:26:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231447AbhGUC02 (ORCPT ); Tue, 20 Jul 2021 22:26:28 -0400 Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D40BC061762; Tue, 20 Jul 2021 20:07:06 -0700 (PDT) Received: by mail-qt1-x82e.google.com with SMTP id z25so997092qto.12; Tue, 20 Jul 2021 20:07:06 -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=qYrh4kaa4Okjc79DXSnbAcrxZZJ0CDhm5w5KA6D2OhM=; b=Sx9OkTxPKPPDLdIBE6qOVMDwShaNhmBM6IZbVRkWVJIgJtYCJP86doprveTqXzSkXa gym2uSap8ZaKIMjgBT849hP5HK5czDTcc3z62RC22axHH8jW/iI7IyiUqRw9MdUm7nY5 KbP16XrM1bfk7gvua/Nmnzsl/z3tNUmGeqCuD2R9R5bZPr6mTsX3xbTkrt256iM+Ln0O pS+FmVSDpoxQDiOqBSZmASehcOIR5THBAcypNjnVjgZrrWw+Mer9sGzjdAuwnwlkQY3Q 4v2THuxUzFFnjcMmKrfqz3qSWOT0fvMBpy7c1laa/4ZJOq26aE7PZIMVh3HKpusJz8E9 f20Q== 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=qYrh4kaa4Okjc79DXSnbAcrxZZJ0CDhm5w5KA6D2OhM=; b=r13vGe+O3StLoAvW/Sb7AseYHgn1GZcN4TCQDBhmHEoh+NyMTYuj1wfMANu7Nw3KJA uhoEKCIVY6NyvUB3sVg0fsrKpkeKyc/2HqH0u6UZMwlhYZ7s2v2J2Hs3KUchrZoSwMZh j3Asl+Q76BCfgZOs7cqX276iozTSiEATQbC1sw5L5Wn/O5yBV2UoxdBYgYpZDa6Glxtl r9VNo5rpwH/28aIrqJoSGLO1wG54jiT8ep76XuU59HSXBkkG10CpdMOYY2BgV3dd/mQm KA3W1L5bclFVxvEooNc2xdoO5Vn0xqevWcq5ek+4MiojnUhtrZgpBLF+P+lH0WfAyj79 N2XA== X-Gm-Message-State: AOAM533HadcfP0WVQc84w0a4Ixg86gQjgF4iPo81Y8ibB4+bMaVvi7xl Iva+LmiUignmTjGjgPxCqY0= X-Google-Smtp-Source: ABdhPJytsST9J8qOzA8XJUYzfR7Lku2lI9p5Wqnumlj7ulsKCqRvnsOEXFXuPWUJPxaqfIhh5g2hGw== X-Received: by 2002:ac8:76da:: with SMTP id q26mr16774969qtr.183.1626836825368; Tue, 20 Jul 2021 20:07:05 -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.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:05 -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 06/13] iio: afe: rescale: add offset support Date: Tue, 20 Jul 2021 23:06:06 -0400 Message-Id: <20210721030613.3105327-7-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin This is a preparatory change required for the addition of temperature sensing front ends. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 80 +++++++++++++++++++++++++++++++++ include/linux/iio/afe/rescale.h | 4 ++ 2 files changed, 84 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 2b73047365cc..6f6a711ae3ae 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -60,11 +60,46 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, } } +int rescale_process_offset(struct rescale *rescale, int scale_type, + int scale, int scale2, int schan_off, + int *val, int *val2) +{ + s64 tmp, tmp2; + + switch (scale_type) { + case IIO_VAL_FRACTIONAL: + tmp = (s64)rescale->offset * scale2; + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT: + *val = div_s64(rescale->offset, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = (s64)rescale->offset * (1 << scale2); + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_NANO: + tmp = (s64)rescale->offset * 1000000000; + tmp2 = ((s64)scale * 1000000000L) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * 1000000L; + tmp2 = ((s64)scale * 1000000L) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + default: + return -EOPNOTSUPP; + } +} + static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); + int scale, scale2; + int schan_off = 0; int ret; switch (mask) { @@ -91,6 +126,47 @@ static int rescale_read_raw(struct iio_dev *indio_dev, ret = iio_read_channel_scale(rescale->source, val, val2); } return rescale_process_scale(rescale, ret, val, val2); + case IIO_CHAN_INFO_OFFSET: + /* + * Processed channels are scaled 1-to-1 and source offset is + * already taken into account. + * + * In other cases, real world measurement are expressed as: + * + * schan_scale * (raw + schan_offset) + * + * Given that the rescaler parameters are applied recursively: + * + * rescaler_scale * (schan_scale * (raw + schan_offset) + + * rescaler_offset) + * + * Or, + * + * (rescaler_scale * schan_scale) * (raw + + * (schan_offset + rescaler_offset / schan_scale) + * + * Thus, reusing the original expression the parameters exposed + * to userspace are: + * + * scale = schan_scale * rescaler_scale + * offset = schan_offset + rescaler_offset / schan_scale + */ + if (rescale->chan_processed) { + *val = rescale->offset; + return IIO_VAL_INT; + } + + if (iio_channel_has_info(rescale->source->channel, + IIO_CHAN_INFO_OFFSET)) { + ret = iio_read_channel_offset(rescale->source, + &schan_off, NULL); + if (ret != IIO_VAL_INT) + return ret < 0 ? ret : -EOPNOTSUPP; + } + + ret = iio_read_channel_scale(rescale->source, &scale, &scale2); + return rescale_process_offset(rescale, ret, scale, scale2, + schan_off, val, val2); default: return -EINVAL; } @@ -167,6 +243,9 @@ static int rescale_configure_channel(struct device *dev, chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE); + if (rescale->offset) + chan->info_mask_separate |= BIT(IIO_CHAN_INFO_OFFSET); + /* * Using .read_avail() is fringe to begin with and makes no sense * whatsoever for processed channels, so we make sure that this cannot @@ -331,6 +410,7 @@ static int rescale_probe(struct platform_device *pdev) rescale->cfg = of_device_get_match_data(dev); rescale->numerator = 1; rescale->denominator = 1; + rescale->offset = 0; ret = rescale->cfg->props(dev, rescale); if (ret) diff --git a/include/linux/iio/afe/rescale.h b/include/linux/iio/afe/rescale.h index 14d4ee1227c6..b152ac487403 100644 --- a/include/linux/iio/afe/rescale.h +++ b/include/linux/iio/afe/rescale.h @@ -23,8 +23,12 @@ struct rescale { bool chan_processed; s32 numerator; s32 denominator; + s32 offset; }; int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2); +int rescale_process_offset(struct rescale *rescale, int scale_type, + int scale, int scale2, int schan_off, + int *val, int *val2); #endif /* __IIO_RESCALE_H__ */ From patchwork Wed Jul 21 03:06:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389965 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6582C636C8 for ; Wed, 21 Jul 2021 03:07:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9F822610A0 for ; Wed, 21 Jul 2021 03:07:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231181AbhGUC0s (ORCPT ); Tue, 20 Jul 2021 22:26:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231489AbhGUC03 (ORCPT ); Tue, 20 Jul 2021 22:26:29 -0400 Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53171C061574; Tue, 20 Jul 2021 20:07:07 -0700 (PDT) Received: by mail-qt1-x82d.google.com with SMTP id g8so1039177qtj.1; Tue, 20 Jul 2021 20:07:07 -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=liDg9SkfmALL91hvXiez7VH6xJpgmfwbB3K9vi7H1kk=; b=bL3X7S86966naf1GN4VDdwDX6BRu20eKfjbTAsNcFwWnifmm7fnzAwq5HmoC2nAYAx mR8mwKvMrOh0IrzBhgOh3SZbLDyTlEwpVWjDCCgtKIoSLeQvWIDb8BasrqYfxCuyRqmj QpmNPMFJW4Mf3GbrwE2AnNUDyfrJCHDq15eH4m+IwO+7TFL95Fr0+dak9iwlHCsYF3gr 9cZbNvdBZFBachLy0AogadvG31BHmhNVd4hNRe4x1EKGoHh98K8QEmb682kjYx0lptyR XRq4LGrhZ9349M0H1N66eTsy/58VdqO7hZ7R0U4mpcKVFvCSOvtaL7odXaor8NOaSLwd sNRQ== 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=liDg9SkfmALL91hvXiez7VH6xJpgmfwbB3K9vi7H1kk=; b=KjXkXLz7V7FMo2ul4p1N/mZpx5ze1BlYhAGXXkgO/X0tr5nmS5sBdGeYDx4CQLeV0H OBLe3zHuLqcpNq7i/0ysW6JZeKE0IBmCsog4+IGIdDpZaJUKBF1TXgDlUGz4G1hc63tW KMKN5QCirg7EAKKGsXTXnKfdJJ1f/um9lg59ifLiHFcSKlX3Jxt6FmJXlY7wTCzEaIMP 76dFytaqmp5kHWvv3Y4hS17qdvmwrBr5Bu/IJni7FipxPqndnaVLN/g7UdqzrfRG1BEQ cCrBvH1JTFAnszfr9IY577ljvdJLDkJIcvbD944QTxqYjwNclj2Pf44ClDzTlqNTyKlb DXtg== X-Gm-Message-State: AOAM531MLj1V0NkRCF9rscvO4B9Tec4VK89RaKGUx8etGSGqSQJ/S2lB adpM7Xvj9ta0RyBiKD9IVNA= X-Google-Smtp-Source: ABdhPJwkN3IM1maxb2DtiDocYGF9tirthSlzSllfdnuaPeQSKQWnk7ygxa8ABDw+ad9BkRHoETD2ww== X-Received: by 2002:ac8:59c5:: with SMTP id f5mr28753959qtf.50.1626836826464; Tue, 20 Jul 2021 20:07:06 -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.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:05 -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 07/13] iio: test: add basic tests for the iio-rescale driver Date: Tue, 20 Jul 2021 23:06:07 -0400 Message-Id: <20210721030613.3105327-8-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin The iio-rescale driver supports various combinations of scale types and offsets. These can often result in large integer multiplications. Make sure these calculations are done right by adding a set of kunit test cases that build on top of iio-test-format. To run these tests, add the following to .kunitconfig $ cat .kunitconfig CONFIG_IIO=y CONFIG_IIO_RESCALE_KUNIT_TEST=y CONFIG_KUNIT=y Then run: $ ./tools/testing/kunit/kunit.py run --kunitconfig .kunitconfig Signed-off-by: Liam Beguin Reported-by: kernel test robot --- drivers/iio/test/Kconfig | 10 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-rescale.c | 342 ++++++++++++++++++++++++++++ 3 files changed, 353 insertions(+) create mode 100644 drivers/iio/test/iio-test-rescale.c diff --git a/drivers/iio/test/Kconfig b/drivers/iio/test/Kconfig index 679a7794af20..4db74a4d7146 100644 --- a/drivers/iio/test/Kconfig +++ b/drivers/iio/test/Kconfig @@ -4,6 +4,16 @@ # # Keep in alphabetical order +config IIO_RESCALE_KUNIT_TEST + bool "Test IIO rescale conversion functions" + depends on KUNIT=y + default KUNIT_ALL_TESTS + help + If you want to run tests on the iio-rescale code say Y here. + + This takes advantage of ARCH=um to run tests and should be used by + developers to tests their changes to the rescaling logic. + config IIO_TEST_FORMAT bool "Test IIO formatting functions" depends on KUNIT=y diff --git a/drivers/iio/test/Makefile b/drivers/iio/test/Makefile index f1099b495301..908963ca0b2f 100644 --- a/drivers/iio/test/Makefile +++ b/drivers/iio/test/Makefile @@ -4,4 +4,5 @@ # # Keep in alphabetical order +obj-$(CONFIG_IIO_RESCALE_KUNIT_TEST) += iio-test-rescale.o ../afe/iio-rescale.o obj-$(CONFIG_IIO_TEST_FORMAT) += iio-test-format.o diff --git a/drivers/iio/test/iio-test-rescale.c b/drivers/iio/test/iio-test-rescale.c new file mode 100644 index 000000000000..ac1aee310ccd --- /dev/null +++ b/drivers/iio/test/iio-test-rescale.c @@ -0,0 +1,342 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Kunit tests for IIO rescale conversions + * + * Copyright (c) 2021 Liam Beguin + */ + +#include +#include +#include +#include +#include + +struct rescale_tc_data { + const char *name; + + const s32 numerator; + const s32 denominator; + const s32 offset; + + const int schan_val; + const int schan_val2; + const int schan_off; + const int schan_scale_type; + + const char *expected; + const char *expected_off; +}; + +const struct rescale_tc_data scale_cases[] = { + /* + * Typical use cases + */ + { + .name = "typical IIO_VAL_INT, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 42, + .expected = "5210.918114143\n", + }, + { + .name = "typical IIO_VAL_INT, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 42, + .expected = "-5210.918114143\n", + }, + { + .name = "typical IIO_VAL_FRACTIONAL, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 42, + .schan_val2 = 20, + .expected = "260.545905707\n", + }, + { + .name = "typical IIO_VAL_FRACTIONAL, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 42, + .schan_val2 = 20, + .expected = "-260.545905707\n", + }, + { + .name = "typical IIO_VAL_FRACTIONAL_LOG2, positive", + .numerator = 42, + .denominator = 53, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 4096, + .schan_val2 = 16, + .expected = "0.049528301\n", + }, + { + .name = "typical IIO_VAL_FRACTIONAL_LOG2, negative", + .numerator = -42, + .denominator = 53, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 4096, + .schan_val2 = 16, + .expected = "-0.049528301\n", + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456789, + .expected = "1256.012008560\n", + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456789, + .expected = "-1256.012008560\n", + }, + { + .name = "typical IIO_VAL_INT_PLUS_MICRO, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 123456789, + .expected = "16557.914267\n", + }, + { + .name = "typical IIO_VAL_INT_PLUS_MICRO, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 123456789, + .expected = "-16557.914267\n", + }, + /* + * 32-bit overflow conditions + */ + { + .name = "overflow IIO_VAL_FRACTIONAL, positive", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 0x7FFFFFFF, + .schan_val2 = 1, + .expected = "214748364.700000000\n", + }, + { + .name = "overflow IIO_VAL_FRACTIONAL, negative", + .numerator = -2, + .denominator = 20, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 0x7FFFFFFF, + .schan_val2 = 1, + .expected = "-214748364.700000000\n", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_NANO, positive", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 0x7fffffff, + .expected = "1.214748364\n", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_NANO, negative", + .numerator = -2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 0x7fffffff, + .expected = "-1.214748364\n", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_MICRO, positive", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 0x7fffffff, + .expected = "215.748364\n", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_MICRO, negative", + .numerator = -2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 0x7fffffff, + .expected = "-215.748364\n", + }, +}; + +const struct rescale_tc_data offset_cases[] = { + /* + * Typical use cases + */ + { + .name = "typical IIO_VAL_INT, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 123, + .schan_val2 = 0, + .schan_off = 14, + .expected_off = "24\n", /* 23.872 */ + }, + { + .name = "typical IIO_VAL_INT, negative", + .offset = -1234, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 12, + .schan_val2 = 0, + .schan_off = 14, + .expected_off = "-88\n", /* -88.83333333333333 */ + }, + { + .name = "typical IIO_VAL_FRACTIONAL, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 12, + .schan_val2 = 34, + .schan_off = 14, + .expected_off = "3510\n", /* 3510.333333333333 */ + }, + { + .name = "typical IIO_VAL_FRACTIONAL, negative", + .offset = -1234, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 12, + .schan_val2 = 34, + .schan_off = 14, + .expected_off = "-3482\n", /* -3482.333333333333 */ + }, + { + .name = "typical IIO_VAL_FRACTIONAL_LOG2, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 12, + .schan_val2 = 16, + .schan_off = 14, + .expected_off = "6739299\n", /* 6739299.333333333 */ + }, + { + .name = "typical IIO_VAL_FRACTIONAL_LOG2, negative", + .offset = -1234, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 12, + .schan_val2 = 16, + .schan_off = 14, + .expected_off = "-6739271\n", /* -6739271.333333333 */ + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456789, + .schan_off = 14, + .expected_off = "135\n", /* 135.8951219647469 */ + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, negative", + .offset = -1234, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456789, + .schan_off = 14, + .expected_off = "-107\n", /* -107.89512196474689 */ + }, + { + .name = "typical IIO_VAL_INT_PLUS_MICRO, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 123456789, + .schan_off = 14, + .expected_off = "23\n", /* 23.246438560723952 */ + }, + { + .name = "typical IIO_VAL_INT_PLUS_MICRO, negative", + .offset = -12345, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 123456789, + .schan_off = 14, + .expected_off = "-78\n", /* -78.50185091745313 */ + }, +}; + +static void case_to_desc(const struct rescale_tc_data *t, char *desc) +{ + strcpy(desc, t->name); +} + +KUNIT_ARRAY_PARAM(iio_rescale_scale, scale_cases, case_to_desc); +KUNIT_ARRAY_PARAM(iio_rescale_offset, offset_cases, case_to_desc); + +static void iio_rescale_test_scale(struct kunit *test) +{ + struct rescale_tc_data *t = (struct rescale_tc_data *)test->param_value; + char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); + struct rescale rescale; + int values[2]; + int ret; + + rescale.numerator = t->numerator; + rescale.denominator = t->denominator; + rescale.offset = t->offset; + values[0] = t->schan_val; + values[1] = t->schan_val2; + + ret = rescale_process_scale(&rescale, t->schan_scale_type, + &values[0], &values[1]); + + ret = iio_format_value(buf, ret, 2, values); + + KUNIT_EXPECT_EQ(test, (int)strlen(buf), ret); + KUNIT_EXPECT_STREQ(test, buf, t->expected); +} + +static void iio_rescale_test_offset(struct kunit *test) +{ + struct rescale_tc_data *t = (struct rescale_tc_data *)test->param_value; + char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); + struct rescale rescale; + int values[2]; + int ret; + + rescale.numerator = t->numerator; + rescale.denominator = t->denominator; + rescale.offset = t->offset; + values[0] = t->schan_val; + values[1] = t->schan_val2; + + ret = rescale_process_offset(&rescale, t->schan_scale_type, + t->schan_val, t->schan_val2, t->schan_off, + &values[0], &values[1]); + + ret = iio_format_value(buf, ret, 2, values); + + KUNIT_EXPECT_EQ(test, (int)strlen(buf), ret); + KUNIT_EXPECT_STREQ(test, buf, t->expected_off); +} + +static struct kunit_case iio_rescale_test_cases[] = { + KUNIT_CASE_PARAM(iio_rescale_test_scale, iio_rescale_scale_gen_params), + KUNIT_CASE_PARAM(iio_rescale_test_offset, iio_rescale_offset_gen_params), + {} +}; + +static struct kunit_suite iio_rescale_test_suite = { + .name = "iio-rescale", + .test_cases = iio_rescale_test_cases, +}; +kunit_test_suite(iio_rescale_test_suite); From patchwork Wed Jul 21 03:06:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389967 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF771C636C9 for ; Wed, 21 Jul 2021 03:07:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D395761029 for ; Wed, 21 Jul 2021 03:07:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231849AbhGUC0z (ORCPT ); Tue, 20 Jul 2021 22:26:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231493AbhGUC0a (ORCPT ); Tue, 20 Jul 2021 22:26:30 -0400 Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FB25C061762; Tue, 20 Jul 2021 20:07:08 -0700 (PDT) Received: by mail-qk1-x736.google.com with SMTP id k4so819851qkj.13; Tue, 20 Jul 2021 20:07:08 -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=a17Z7u+gpgZyFiipJt9rouvOOgmIz72gFFa6sOhMfEc=; b=WJUVn+yc178Asm5EjJ+CtVPNu8/yZSjqwfnZnRAyPb5HhYs+kCKbvLkynLlCYQ/ra6 +oK5wAdZaEyrynEf/rVLhk3r4Hj1HHL2Cx+cHE8EbxZucE7nfBOQ3c0DJoljZ+sh3BJC eRxdzumZ1kl0LJ+oaomquB4tX0fo5uIm0AyUElHnPfPnJB68vC6d7Z6E4CcJXtRaCSq0 zSKSDD8Wex4x7ep40nAKT2og/XPReNcTaCssO6Jb7Kjql8lmLSOTyqeCYI+eTKQf+P1T BmVgAOsEyBeLkJKp7uggy/dpO/nJv5VqaA4Hh2FgOeHfrraTKhkSXJen7M13p5MhEYwA xkdw== 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=a17Z7u+gpgZyFiipJt9rouvOOgmIz72gFFa6sOhMfEc=; b=XhtmBpleCIAft1wTgUXe25ercaa/bG4DsUl7DtYjtNRg6ZjALvp3e8GBR1Z3z2Cyee FvFfMYTvhRICwiFG0rUS8s7zY2KMa2MG83ITOsU80AgoY4wT7lb9gCTZHIOXQoRP9LDo T5IYCxJ2DgzXsEbO5mWBKJE0QXrK3oeAtgoRJw1LCx19EHdLGY3p8F8SW7Iw1faGbAmJ BazVy1KfijSknreWG+L21LAlZnrVwDM3TlZYsOoUkUzdHj60/YlmTsbzw0rMGxpPwAxi qc800GPDZ3bh3Rm8MJlJL2HlRWHDe3Yg24VHcicrWUY+zBmgjqj7xWhkeAUZffix+F8p w6Lw== X-Gm-Message-State: AOAM532AvFjhtOh1c9LKOn75WTo6+kR0TprZB7UVKDM15BOV+x3GatSK WLxyTJuFRWmR0ZwC102CJxc= X-Google-Smtp-Source: ABdhPJyaTlCSmUrE29R9noJQBWuuRatVzqxH0tSmpAcGXCJY7so9ArADNagZ2+TbQd2en1Ycaoitsw== X-Received: by 2002:a05:620a:56b:: with SMTP id p11mr31440424qkp.66.1626836827441; Tue, 20 Jul 2021 20:07:07 -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.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:07 -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 08/13] iio: afe: rescale: reduce risk of integer overflow Date: Tue, 20 Jul 2021 23:06:08 -0400 Message-Id: <20210721030613.3105327-9-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin Reduce the risk of integer overflow by doing the scale calculation with 64bit integers and looking for a Greatest Common Divider for both parts of the fractional value when required. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 6f6a711ae3ae..35fa3b4e53e0 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -21,12 +21,21 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { - unsigned long long tmp; + s64 tmp, tmp2; + u32 factor; switch (scale_type) { case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; + if (check_mul_overflow(*val, rescale->numerator, (s32 *)&tmp) || + check_mul_overflow(*val2, rescale->denominator, (s32 *)&tmp2)) { + tmp = (s64)*val * rescale->numerator; + tmp2 = (s64)*val2 * rescale->denominator; + factor = gcd(tmp, tmp2); + tmp = div_s64(tmp, factor); + tmp2 = div_s64(tmp2, factor); + } + *val = tmp; + *val2 = tmp2; return scale_type; case IIO_VAL_INT: *val *= rescale->numerator; From patchwork Wed Jul 21 03:06:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389983 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C61BC07E9B for ; Wed, 21 Jul 2021 03:08:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2C07D61029 for ; Wed, 21 Jul 2021 03:08:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231587AbhGUC1C (ORCPT ); Tue, 20 Jul 2021 22:27:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231499AbhGUC0b (ORCPT ); Tue, 20 Jul 2021 22:26:31 -0400 Received: from mail-qt1-x832.google.com (mail-qt1-x832.google.com [IPv6:2607:f8b0:4864:20::832]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F26AC061766; Tue, 20 Jul 2021 20:07:09 -0700 (PDT) Received: by mail-qt1-x832.google.com with SMTP id q15so1065247qtp.0; Tue, 20 Jul 2021 20:07:09 -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=yvFuB5OUIFHmYXYGJ6m3xV8M488XLtaqGhWB8Fo0+4g=; b=K2LHgLhqnaU3AEwamjtgb5YkW/HAdmkf+0JMgoBUvBaQ01NVbmMbWZhyZJ8hfbYSKW 1yYh/VAQQq9ts4iE3nCiRM69TfKBHmslL+7iLTBf7cmCaSSl/bxKhqLVH8WrPWGzrWqW nR8geF44ZQnquh9P32l8uVzC1i+xK3C/AXcWKwwFM3kb4SgDgYS6AA+n0DyfElwHiT/P 8e+s5ADxz6PndKEb2LuIltbyRdoIYpuex4yC/4J/c1Bejoq0QxR/l/nxgD7WPEpmRr2A kLHYMk1YTZ0nDo5YXtgd4jzaduTM0LeXWGBEVxjC31VPkozBjkarsdxJLMGX/IrpZ7/p s/QQ== 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=yvFuB5OUIFHmYXYGJ6m3xV8M488XLtaqGhWB8Fo0+4g=; b=NrIl9s5WhSWxW++6/JcvPR1BVxoOT06DonIO/o0NnRer1/dN/S0tJl8xwDIsYdoN04 PQUCiv2XM/KepfeaAVMaLAsww231IsPQB1qbPLkYnLw62LwB3PMIHI1FMFGgsdmvV2CS SVYcCrwJD5eQWi6eWbFUC3fwwoeIbMVWt8qeIyKRZcqOh/z0Lya6TanWyoE+0cKafFOm 39CXkM2vDUYMm8wEoiYKgrepTsH/2yCBzTCGDE7N+LTlZ0usIHMtFCGUI1s3aPFdP8ZL mBFIauFJ+FhSva6aksuPAZUbCRmTlo6ZWqDCam6wUM9Bad5ECwj2Kqg95E/mRGPbVTd+ h7dg== X-Gm-Message-State: AOAM5310qg0SL5PNhVjxbghvBL/1lujBoFFkNkyFid/EHCiArsT56QSX 6P7xUKynaeG9R6nW1N7KQZ4= X-Google-Smtp-Source: ABdhPJzwlAXvpv5XALNTDrC7fIGOCR+/RPCmHXkAv/Ucf/N19DiYub+YgIVxZck2DzNLT312xysdgQ== X-Received: by 2002:ac8:7e89:: with SMTP id w9mr29865082qtj.8.1626836828418; Tue, 20 Jul 2021 20:07:08 -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.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:08 -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 09/13] iio: afe: rescale: fix precision on fractional log scale Date: Tue, 20 Jul 2021 23:06:09 -0400 Message-Id: <20210721030613.3105327-10-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin The IIO_VAL_FRACTIONAL_LOG2 scale type doesn't return the expected scale. Update the case so that the rescaler returns a fractional type and a more precise scale. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 35fa3b4e53e0..47cd4a6d9aca 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -44,12 +44,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, *val2 = rescale->denominator; return IIO_VAL_FRACTIONAL; case IIO_VAL_FRACTIONAL_LOG2: - tmp = *val * 1000000000LL; - do_div(tmp, rescale->denominator); - tmp *= rescale->numerator; - do_div(tmp, 1000000000LL); - *val = tmp; - return scale_type; + *val = rescale->numerator * *val; + *val2 = rescale->denominator * (1 << *val2); + return IIO_VAL_FRACTIONAL; case IIO_VAL_INT_PLUS_NANO: tmp = ((s64)*val * 1000000000LL + *val2) * rescale->numerator; tmp = div_s64(tmp, rescale->denominator); From patchwork Wed Jul 21 03:06:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389993 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4FA21C07E9B for ; Wed, 21 Jul 2021 03:09:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 33788611C1 for ; Wed, 21 Jul 2021 03:09:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231659AbhGUC2n (ORCPT ); Tue, 20 Jul 2021 22:28:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231512AbhGUC0c (ORCPT ); Tue, 20 Jul 2021 22:26:32 -0400 Received: from mail-qv1-xf34.google.com (mail-qv1-xf34.google.com [IPv6:2607:f8b0:4864:20::f34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47676C061767; Tue, 20 Jul 2021 20:07:10 -0700 (PDT) Received: by mail-qv1-xf34.google.com with SMTP id f3so305453qvm.2; Tue, 20 Jul 2021 20:07:10 -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=/AVhj16iNURSZKn9I66HSovjlv+JCMA7WuCaKBrMzQo=; b=XvTAw5ovqxPJHuW5WxMc8fBTLguBwnYcYyB+yMDcx6htF3XAPE13YP0S2oRwSg+fV9 9EY5UBS2cPPT3tdIFtAkrqZDHYQ764ihap5pWR0FGc0UWdr04zWLR2xfjRu18Rxq86uC t/2E4MhI6qhlwNzYu7nGs1rF1YuDcMiytTvs+cZYC+QhP3iP7jkNRDYejTmAX8Vc+B6d ega2cmmVz/kYGw/caneQQQ1R38jc5AzkXYllWTUGSg1ZR/pofsh88dFNd6hjzfYgn6oY 2iQrknq3brHM1h2boFhy9flNAX2FQgtirzHrg/7q5PKQZKs2DuGUD8CYU/VzN1z3S+Fo E/Dw== 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=/AVhj16iNURSZKn9I66HSovjlv+JCMA7WuCaKBrMzQo=; b=HRPgL74eeinutKTF7AqP6UUGw1DXgjbjABZcrxTP8i0U5Y9m3svkE/faGByTfBbBuy 7JBA5MHOV8PmU1xeGynIafVBF5wZCAdCNUvmvT/RI5MHvREP8Hvg2SfjbCmkR1kYk0wb RbOZbAu+Dcv9Id10lhwHZVOBstt9JF3Bs3Y9dkeeCmyexZTKtG1M3XOpVxl0oo3xoEWD mSvr11bFGQMDW5oOnE6ol8SAmicr0FEMAB5CmSQukcxmwA24pCdrcjprkkka9ReCv64Q PEOoCbh1CI+nNqQwxIeVJ0T42yZHe4fV57XW8mP5Laiai7LXtECciLJ3YqY3tXxbg7b/ a64g== X-Gm-Message-State: AOAM533W6hlkWagdgOv3lxa1fPo5/LDTeMDcaIizKKWH4bx/yhr2Lwpj sf6wZKM8x5I/CiZjH456Iyw= X-Google-Smtp-Source: ABdhPJyGDTvcAx/C80x4YYcfqVaS8jpHUQmDuqFLg0jgdDFj2bzpO/3zdMRaYmLli4mEW/e8x1jwCw== X-Received: by 2002:a05:6214:aa5:: with SMTP id ew5mr29083131qvb.18.1626836829482; Tue, 20 Jul 2021 20:07:09 -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.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:09 -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 10/13] iio: afe: rescale: add RTD temperature sensor support Date: Tue, 20 Jul 2021 23:06:10 -0400 Message-Id: <20210721030613.3105327-11-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin An RTD (Resistance Temperature Detector) is a kind of temperature sensor used to get a linear voltage to temperature reading within a give range (usually 0 to 100 degrees Celsius). Common types of RTDs include PT100, PT500, and PT1000. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 47cd4a6d9aca..6a1ecaad2da4 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -351,10 +351,52 @@ static int rescale_voltage_divider_props(struct device *dev, return 0; } +static int rescale_temp_sense_rtd_props(struct device *dev, + struct rescale *rescale) +{ + u32 factor; + u32 alpha; + u32 iexc; + u32 tmp; + int ret; + u32 r0; + + ret = device_property_read_u32(dev, "excitation-current-microamp", + &iexc); + if (ret) { + dev_err(dev, "failed to read excitation-current-microamp: %d\n", + ret); + return ret; + } + + ret = device_property_read_u32(dev, "alpha-ppm-per-celsius", &alpha); + if (ret) { + dev_err(dev, "failed to read alpha-ppm-per-celsius: %d\n", + ret); + return ret; + } + + ret = device_property_read_u32(dev, "r-naught-ohms", &r0); + if (ret) { + dev_err(dev, "failed to read r-naught-ohms: %d\n", ret); + return ret; + } + + tmp = r0 * iexc * alpha / 1000000; + factor = gcd(tmp, 1000000); + rescale->numerator = 1000000 / factor; + rescale->denominator = tmp / factor; + + rescale->offset = -1 * ((r0 * iexc) / 1000); + + return 0; +} + enum rescale_variant { CURRENT_SENSE_AMPLIFIER, CURRENT_SENSE_SHUNT, VOLTAGE_DIVIDER, + TEMP_SENSE_RTD, }; static const struct rescale_cfg rescale_cfg[] = { @@ -370,6 +412,10 @@ static const struct rescale_cfg rescale_cfg[] = { .type = IIO_VOLTAGE, .props = rescale_voltage_divider_props, }, + [TEMP_SENSE_RTD] = { + .type = IIO_TEMP, + .props = rescale_temp_sense_rtd_props, + }, }; static const struct of_device_id rescale_match[] = { @@ -379,6 +425,8 @@ static const struct of_device_id rescale_match[] = { .data = &rescale_cfg[CURRENT_SENSE_SHUNT], }, { .compatible = "voltage-divider", .data = &rescale_cfg[VOLTAGE_DIVIDER], }, + { .compatible = "temperature-sense-rtd", + .data = &rescale_cfg[TEMP_SENSE_RTD], }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, rescale_match); From patchwork Wed Jul 21 03:06:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389995 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6260C636CA for ; Wed, 21 Jul 2021 03:09:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C8E0360FDB for ; Wed, 21 Jul 2021 03:09:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231936AbhGUC2q (ORCPT ); Tue, 20 Jul 2021 22:28:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231583AbhGUC0d (ORCPT ); Tue, 20 Jul 2021 22:26:33 -0400 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22749C061768; Tue, 20 Jul 2021 20:07:11 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id bm6so889258qkb.1; Tue, 20 Jul 2021 20:07:11 -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=RO8WlOdaYdCVzds6lcbAmbmkaYOfrcMWqvsyZPbXX9A=; b=FwMuZGrUXefxl9YCPS70WjvXbNa4PJvH6na09u+nuND9ZahCFmiv7/UR86hpVaDHnM pT8BYGWthlIJEtZ994oFDqcfv+TF2Nn/P+CgKzFkjlNylXn0FjolCm9LaS6I+4KCnJa+ mgq1b9nXW93c4tdK1JWVScMLtPs5EQxPLkvIEBnChbJi7kwyk+b8EMLBBX8VrhPK9a2p j5SaaJtsd40bDPgzWqE9N0i8LTfGkgCfNSJ4UwobAOTGoHEuWuZynn7qixU/9PkClLZE 3F11TXBSZtqZuCY7CdIdMqDhRVgRMk1qx73XKoFdR5iughPg2kh7mA2KTXTGjIG5RWdH E2Kg== 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=RO8WlOdaYdCVzds6lcbAmbmkaYOfrcMWqvsyZPbXX9A=; b=N29BdkytLqWatvkk7UP4x4b3sGmNZ01dnJJ7lvopz3+YutghB5vsDWYUCkEJYUe4oC n0MFBuiA0i+HFWrvAN5LMXhoVpl/u2MuLvoH1dR/9RbCQRZV5Lmf8q7XSy4E0TGvXgqW 1Jf5SDGCKgvX2NMTO5ZzM+BpFdHPNqbAsG/94eXlOTmvfsMXp42AFOJD1hM7EgDbgvw5 5h+lO7fLzStKD4Fj+vph0Fa4HBTvR87HX5+bSaR/EyW5Aaig4J+hVdE5JvRw4bFaUmj3 rx4PcwcsQoL9X2RYRJQpGx6KEhUD2Zxo+tQgigYmxXZE1bYcswvRT1VubxaAQL2nDIgM 99GQ== X-Gm-Message-State: AOAM531kxFDshBKlZfsQ0WpXMiFSzpjosRGywaFt64rrXL05nZkW75hr QsxTbXs/GTwauvuqXVqo9eU= X-Google-Smtp-Source: ABdhPJyiTvWiy2m4uJfdqSjcCJFvbbDhsq6qd4UrxUKrlhuk3MNqiyYQ+JSXqNaFIk0IyRtFr5PLeA== X-Received: by 2002:a37:8581:: with SMTP id h123mr32637571qkd.316.1626836830362; Tue, 20 Jul 2021 20:07:10 -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.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:10 -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 11/13] iio: afe: rescale: add temperature transducers Date: Tue, 20 Jul 2021 23:06:11 -0400 Message-Id: <20210721030613.3105327-12-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin A temperature transducer is a device that converts a thermal quantity into any other physical quantity. This patch add support for temperature to voltage (like the LTC2997) and temperature to current (like the AD590) linear transducers. In both cases these are assumed to be connected to a voltage ADC. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 6a1ecaad2da4..3abeaded6169 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -392,11 +392,38 @@ static int rescale_temp_sense_rtd_props(struct device *dev, return 0; } +static int rescale_temp_transducer_props(struct device *dev, + struct rescale *rescale) +{ + s32 offset = 0; + s32 sense = 1; + s32 alpha; + s64 tmp; + int ret; + + device_property_read_u32(dev, "sense-offset-millicelsius", &offset); + device_property_read_u32(dev, "sense-resistor-ohms", &sense); + ret = device_property_read_u32(dev, "alpha-ppm-per-celsius", &alpha); + if (ret) { + dev_err(dev, "failed to read alpha-ppm-per-celsius: %d\n", ret); + return ret; + } + + rescale->numerator = 1000000; + rescale->denominator = alpha * sense; + + tmp = (s64)offset * (s64)alpha * (s64)sense; + rescale->offset = div_s64(tmp, (s32)1000000); + + return 0; +} + enum rescale_variant { CURRENT_SENSE_AMPLIFIER, CURRENT_SENSE_SHUNT, VOLTAGE_DIVIDER, TEMP_SENSE_RTD, + TEMP_TRANSDUCER, }; static const struct rescale_cfg rescale_cfg[] = { @@ -416,6 +443,10 @@ static const struct rescale_cfg rescale_cfg[] = { .type = IIO_TEMP, .props = rescale_temp_sense_rtd_props, }, + [TEMP_TRANSDUCER] = { + .type = IIO_TEMP, + .props = rescale_temp_transducer_props, + }, }; static const struct of_device_id rescale_match[] = { @@ -427,6 +458,8 @@ static const struct of_device_id rescale_match[] = { .data = &rescale_cfg[VOLTAGE_DIVIDER], }, { .compatible = "temperature-sense-rtd", .data = &rescale_cfg[TEMP_SENSE_RTD], }, + { .compatible = "temperature-transducer", + .data = &rescale_cfg[TEMP_TRANSDUCER], }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, rescale_match); From patchwork Wed Jul 21 03:06:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389985 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0931AC07E9B for ; Wed, 21 Jul 2021 03:08:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DE736611C1 for ; Wed, 21 Jul 2021 03:08:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231970AbhGUC1Q (ORCPT ); Tue, 20 Jul 2021 22:27:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231607AbhGUC0f (ORCPT ); Tue, 20 Jul 2021 22:26:35 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CCC2C0613DB; Tue, 20 Jul 2021 20:07:12 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id m3so838333qkm.10; Tue, 20 Jul 2021 20:07:12 -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=R5EJ5Gnotkd7ffF8IJqFhFOyVW3BEMA9J80b/3joIJo=; b=E9dvB3pf/ikUebET2VoanuLsWWmhGzXGFwouYFkDPR76b/Ub8evqYxPAbY3ymTdQ0E nXlAsEenQic5ODsrO604NYkAQl0mhoMGcG4xDtWCeevw3R58uIJ3svHICrbHzkWb2fZZ prmwC4HqR8mHkekOUU4NobL0YIqwZqbGlkB63551W2GuCVgM96TXBL7KVjVOydauzrNl U/tTnovBtLtXO7p5T6/6o+gvgg2bNYIKV/Lxq1U9as6GzXTliBV/+mNtMqeLNVfiGp6t ngclELuh2jGUJHYwMNLx74OMOo8eXJ4Hi1W+kQ7NwyzkyASuTH94HmQ//n/CnY4/RSA/ LQpQ== 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=R5EJ5Gnotkd7ffF8IJqFhFOyVW3BEMA9J80b/3joIJo=; b=mkOxKmGo+owfHWRt3aHyqSK16JeQdUXDzu+HnUizOYOpOl3A1ioMzJLZ62evRxZvPC /hTe6aRvJ+Vw26MSe7wIn0M7I1Kimnhl7LxNOxK3jQrpI0ZzqYVXF2XEhhynmZforj6V 4hbq38xzpeNGnpPIVylzsX4R1eigApbL+ZNqSyoW/moQNe2cgrPKAnFjKoSGoDl7CVGV P8Fz/mQ8hRkn6si2umyCsbGUy1XcHhBGqMtVlWrnuK6bgM9IDvFKetAB48LySAgy8CG9 uElr8wdJc2nidi0G54/YvmZGAGHNGFlXFYBleZ5k2s6ISy5whjostWRu4tWtlb6oKVIu o8WQ== X-Gm-Message-State: AOAM5301CTUwGsmcr0LKIPzd+pBsQlR5SJIYCk59KiFlraqXpVTK6kWm B0iqfgLpft8XcVds32RQEB4= X-Google-Smtp-Source: ABdhPJwRK5nTlIBEJG187YxlE/VGLVAsfa4yb/5n5fbWjbAx3OWmyzETMjmd05SS8PpkawHGVW8lcA== X-Received: by 2002:ae9:ec03:: with SMTP id h3mr18188589qkg.116.1626836831331; Tue, 20 Jul 2021 20:07:11 -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.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:10 -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 12/13] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Date: Tue, 20 Jul 2021 23:06:12 -0400 Message-Id: <20210721030613.3105327-13-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin An ADC is often used to measure other quantities indirectly. This binding describe one case, the measurement of a temperature through the voltage across an RTD resistor such as a PT1000. Signed-off-by: Liam Beguin Reviewed-by: Rob Herring --- .../iio/afe/temperature-sense-rtd.yaml | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml diff --git a/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml b/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml new file mode 100644 index 000000000000..336ce96371db --- /dev/null +++ b/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml @@ -0,0 +1,101 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/afe/temperature-sense-rtd.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Temperature Sense RTD + +maintainers: + - Liam Beguin + +description: | + RTDs (Resistance Temperature Detectors) are a kind of temperature sensors + used to get a linear voltage to temperature reading within a give range + (usually 0 to 100 degrees Celsius). + + When an io-channel measures the output voltage across an RTD such as a + PT1000, the interesting measurement is almost always the corresponding + temperature, not the voltage output. This binding describes such a circuit. + + The general transfer function here is (using SI units) + + V = R(T) * iexc + R(T) = r0 * (1 + alpha * T) + T = 1 / (alpha * r0 * iexc) * (V - r0 * iexc) + + The following circuit matches what's in the examples section. + + 5V0 + ----- + | + +---+----+ + | R 5k | + +---+----+ + | + V 1mA + | + +---- Vout + | + +---+----+ + | PT1000 | + +---+----+ + | + ----- + GND + +properties: + compatible: + const: temperature-sense-rtd + + io-channels: + maxItems: 1 + description: | + Channel node of a voltage io-channel. + + '#io-channel-cells': + const: 0 + + excitation-current-microamp: + description: The current fed through the RTD sensor. + + alpha-ppm-per-celsius: + description: | + alpha can also be expressed in micro-ohms per ohm Celsius. It's a linear + approximation of the resistance versus temperature relationship + between 0 and 100 degrees Celsius. + + alpha = (R_100 - R_0) / (100 * R_0) + + Where, R_100 is the resistance of the sensor at 100 degrees Celsius, and + R_0 (or r-naught-ohms) is the resistance of the sensor at 0 degrees + Celsius. + + Pure platinum has an alpha of 3925. Industry standards such as IEC60751 + and ASTM E-1137 specify an alpha of 3850. + + r-naught-ohms: + description: | + Resistance of the sensor at 0 degrees Celsius. + Common values are 100 for PT100, 500 for PT500, and 1000 for PT1000 + +additionalProperties: false +required: + - compatible + - io-channels + - excitation-current-microamp + - alpha-ppm-per-celsius + - r-naught-ohms + +examples: + - | + pt1000_1: temperature-sensor0 { + compatible = "temperature-sense-rtd"; + #io-channel-cells = <0>; + io-channels = <&temp_adc1 0>; + + excitation-current-microamp = <1000>; /* i = U/R = 5 / 5000 */ + alpha-ppm-per-celsius = <3908>; + r-naught-ohms = <1000>; + }; +... From patchwork Wed Jul 21 03:06:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12389987 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36B92C07E9B for ; Wed, 21 Jul 2021 03:08:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0EC1C611C1 for ; Wed, 21 Jul 2021 03:08:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231765AbhGUC1i (ORCPT ); Tue, 20 Jul 2021 22:27:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231756AbhGUC0k (ORCPT ); Tue, 20 Jul 2021 22:26:40 -0400 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0807AC0613E0; Tue, 20 Jul 2021 20:07:13 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id s193so872332qke.4; Tue, 20 Jul 2021 20:07:12 -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=GBxLtWhEZsYL5lfe8/wLjcrrsUgudlTtucyNHZMYMQk=; b=fOaY69lbuts10kKaqZ0eocvUPYPzq+XmHmkus9YUsrdpL+EC9m3xmrTkEPmpGkc1Wt 327EKfhyimv2Mv/Ie05WzHvPeL2jZ70DADMv8rgCApFcZWh72DjDpYUiFcYJ/94HzT+R xOYV0E//WvXXNgnCvQ8sScO39cfhYlmXSfAwylpHZ6bAKdnPgRW+79xO4EgkDHlTFfJW QHufIZn/ghYZ9tCMTgarIwe3uK5cxMLihsN6r/SD8ivfddQTAQe4LQIilYH1uWmEMuOG yfxW7T12u349fRI9haOgFHe2NMn4s+pqvII17RG1L4UsqGkT6OISiTOplsvCB6mhvBRV f5XQ== 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=GBxLtWhEZsYL5lfe8/wLjcrrsUgudlTtucyNHZMYMQk=; b=p6PVBD53T+MMgLSxW4F3PfaNQ+4FGKJ6hQ0O6busVn8r6N5qY0wkd/chRVO7sAxGPR q77U81WiE/a223kK8SZsEhn8e9On43ZD+7s0QpDKUIRInpzgPSn6KQipYbC/wPyFpQte rGRxalE/8qHTOnx6ivM+YiKXY68uDqcT8xtDkRVhpU+ihO3mxUE6I34JoCokk/B52Ap3 pW0cZLY1h67vZRpbnKZkVLBaVb4mgo7hn4hatsjhbrJ9Qm/aDkF6BxErqCRLirayqddo of7BL3gNDEL8hXu8YrXC9LKiFGKYGPpSebzRDr+YzrOzDoIM/2kFTU4FVuVPOZ8KSto2 ZUow== X-Gm-Message-State: AOAM5332SeaC76QSN9Z5Z4w3DDvkT/h0EiySmw6sQw4zHt3jfxIKdr5S ob8riuTVIB0jMndTD4uFfZs= X-Google-Smtp-Source: ABdhPJyMTVTQBqf6Xy0foGGb4wr4dcTmPTf+Bk5XNZNWU5dESbc8BZgKDeaY2rYaUKzEKSVkf8HWxw== X-Received: by 2002:ae9:f805:: with SMTP id x5mr32483942qkh.373.1626836832254; Tue, 20 Jul 2021 20:07:12 -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.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jul 2021 20:07:11 -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 13/13] dt-bindings: iio: afe: add bindings for temperature transducers Date: Tue, 20 Jul 2021 23:06:13 -0400 Message-Id: <20210721030613.3105327-14-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 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin An ADC is often used to measure other quantities indirectly. This binding describe one case, the measurement of a temperature through a temperature transducer (either voltage or current). Signed-off-by: Liam Beguin Reviewed-by: Rob Herring --- .../iio/afe/temperature-transducer.yaml | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml diff --git a/Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml b/Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml new file mode 100644 index 000000000000..cfbf5350db27 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml @@ -0,0 +1,114 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/afe/temperature-transducer.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Temperature Transducer + +maintainers: + - Liam Beguin + +description: | + A temperature transducer is a device that converts a thermal quantity + into any other physical quantity. This binding applies to temperature to + voltage (like the LTC2997), and temperature to current (like the AD590) + linear transducers. + In both cases these are assumed to be connected to a voltage ADC. + + When an io-channel measures the output voltage of a temperature analog front + end such as a temperature transducer, the interesting measurement is almost + always the corresponding temperature, not the voltage output. This binding + describes such a circuit. + + The general transfer function here is (using SI units) + V(T) = Rsense * Isense(T) + T = (Isense(T) / alpha) + offset + T = 1 / (Rsense * alpha) * (V + offset * Rsense * alpha) + + When using a temperature to voltage transducer, Rsense is set to 1. + + The following circuits show a temperature to current and a temperature to + voltage transducer that can be used with this binding. + + VCC + ----- + | + +---+---+ + | AD590 | VCC + +---+---+ ----- + | | + V proportional to T +----+----+ + | D+ --+ | + +---- Vout | LTC2997 +--- Vout + | D- --+ | + +---+----+ +---------+ + | Rsense | | + +---+----+ ----- + | GND + ----- + GND + +properties: + compatible: + const: temperature-transducer + + io-channels: + maxItems: 1 + description: | + Channel node of a voltage io-channel. + + '#io-channel-cells': + const: 0 + + sense-offset-millicelsius: + description: | + Temperature offset. + This offset is commonly used to convert from Kelvins to degrees Celsius. + In that case, sense-offset-millicelsius would be set to <(-273150)>. + default: 0 + + sense-resistor-ohms: + description: | + The sense resistor. + By default sense-resistor-ohms cancels out the resistor making the + circuit behave like a temperature transducer. + default: 1 + + alpha-ppm-per-celsius: + description: | + Sometimes referred to as output gain, slope, or temperature coefficient. + + alpha is expressed in parts per million which can be micro-amps per + degrees Celsius or micro-volts per degrees Celsius. The is the main + characteristic of a temperature transducer and should be stated in the + datasheet. + +additionalProperties: false + +required: + - compatible + - io-channels + - alpha-ppm-per-celsius + +examples: + - | + ad950: temperature-sensor-0 { + compatible = "temperature-transducer"; + #io-channel-cells = <0>; + io-channels = <&temp_adc 3>; + + sense-offset-millicelsius = <(-273150)>; /* Kelvin to degrees Celsius */ + sense-resistor-ohms = <8060>; + alpha-ppm-per-celsius = <1>; /* 1 uA/K */ + }; + - | + znq_tmp: temperature-sensor-1 { + compatible = "temperature-transducer"; + #io-channel-cells = <0>; + io-channels = <&temp_adc 2>; + + sense-offset-millicelsius = <(-273150)>; /* Kelvin to degrees Celsius */ + alpha-ppm-per-celsius = <4000>; /* 4 mV/K */ + }; +...