From patchwork Sun Aug 1 19:39:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413073 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 9439DC4320E for ; Sun, 1 Aug 2021 19:40:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 75F6B610A1 for ; Sun, 1 Aug 2021 19:40:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230130AbhHATkX (ORCPT ); Sun, 1 Aug 2021 15:40:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229955AbhHATkW (ORCPT ); Sun, 1 Aug 2021 15:40:22 -0400 Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C4DEC0613D3; Sun, 1 Aug 2021 12:40:13 -0700 (PDT) Received: by mail-qt1-x830.google.com with SMTP id l24so10334887qtj.4; Sun, 01 Aug 2021 12:40:13 -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=dzG+MAqz/Sb4ya3ePvJyRkD31JJY5HYLT4GhK/3639OAm/Xjau+2W9xk4zufy0aR5d RTbkIZ3QaQwrkVJl6McyArrDE8BL1XUiwK1WanxCrmBVp3ntp5b3MKplgHRho6R5i825 UBDjWewYc28JVpvBFOMV77v/SSjuWZC/vV/TeJZe4MOf5SLfvNA+199O1Xl/0AXsg1PK Y4orGBsUODq7LwHrxTPIAlnKUmHa4F9UU0uE83SypnUzO8bAEny4LaMIaECdnwc4xz05 OOJFYMzPjtIMqG4sOy0LFWOyn94vcr78FOHVVDa17UBN1DLY6Bnv5TAhOCXSBOWlQ37z MUWQ== 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=gE9s8PySc9ULKv/26JX6DQs2gGuNEuDnN4DSJVKZaPCcI5o6KJg8EIqb0QA1XGBNkT 3nMLwNnqM4urWqjVG25g9fQK6NIhredxiifUeyq6rni2YCdP7qWfXxQmPyjFroRkUdok fFg0nlk6Clw+iIwPPu+SfMNg60wD3UIZs/rFMZdBfBT94mrpTwbOupo5xDupESGy3eF9 0GeicPbA2wpiEBZdLuEOWbf3g1kHlsYVT7bMDQ5GL/zgjUISsCU4+HapM8Ut5mGv2SDX 9oo7Fo65AYNNBPhwBM/St+6rAAgQnGmeXUDKoBummLTck29A+6NA650puqR0QQgyRl4N b8vw== X-Gm-Message-State: AOAM533K7omN161G2G+BFtatBaYbPmL6AEBhMQgvKalwy3GzYO5um8ij MF6WNzem1dgCEfm49FHx7l4= X-Google-Smtp-Source: ABdhPJx0v0bhE0Ov2CnPwdwv1McBjkgelOuMproMqFlOMrmvZ5PanoporcwO+bI1pg1UcfQpUw7paQ== X-Received: by 2002:ac8:665a:: with SMTP id j26mr11530747qtp.254.1627846811657; Sun, 01 Aug 2021 12:40:11 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40: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 v7 01/13] iio: inkern: apply consumer scale on IIO_VAL_INT cases Date: Sun, 1 Aug 2021 15:39:48 -0400 Message-Id: <20210801194000.3646303-2-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 Sun Aug 1 19:39:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413075 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 C80D1C43216 for ; Sun, 1 Aug 2021 19:40:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A3207610F7 for ; Sun, 1 Aug 2021 19:40:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230230AbhHATkX (ORCPT ); Sun, 1 Aug 2021 15:40:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230072AbhHATkW (ORCPT ); Sun, 1 Aug 2021 15:40:22 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6160CC0613D5; Sun, 1 Aug 2021 12:40:13 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id f22so14745158qke.10; Sun, 01 Aug 2021 12:40:13 -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=T/kLWJHDY0jlV2/QSYqw8Gnjdp52QUgg3dkG85+ktLA4udP18yHXZybVUi5bGh6fmI ldB2NZyLo4NHENkwPrjk72jxSRRHecdEIzjFraS7a5mxNhKBUR3oDLL8RzeE+Mg1oBvb Bx4XIt3AcQP6jKuGBdP7b3eaZ5OQ6adPHOMVP0+RaA1E2zhTGSle7c1GnPrfdtGVc5uM U4i2ND0A68JYTPy/PZG1GoThJ6p6UbJ8FSHLfpENONNno/uBhL5DEoc0dIKhJd16Hru5 2kfAs5QrsxT1grKDh+4wJFCEweUFtkjHwhO9aAuICJw0bEkcGwSyMYPS3jmbF+HCkXZR kW/g== 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=gE363wJnVzOhvme2Ngcjsl0o8xosvHHTabj0qv6Oca/eAYcC3/sa19UK7pzDKQPthd aW2NsnZe8znSecsikIsL5ZCVdVV7hmIZavAPwjzc8BaayuvOZ+xuP9IFlcJplMxE5OYn utp3vHGUGYog11+MI/dq6sT2OGVZ7XsSM4nBV8fvi+gUTvli20V3Fi5s+lMTxUHg4riP tzywyhPLRwPi7VcQ32QQBfDbmk3gcIQkFq6gGbEevw6D2C1DIRuJ56y/h0ZW7rmtH2mG l9jTXLA+wa96q27OSfm2aSdPux3Vtskiq53L68j5YqmjIfMjmzUFH7QwM2fJFd5ReRnD aWNw== X-Gm-Message-State: AOAM533l2Pm/hXhxgpZNUoT5GJCm5rduYWOY824pXpRePFnacWW2cOh2 l1p5O/yE+AkUDTYEbOwjvu8= X-Google-Smtp-Source: ABdhPJxgcecg3IPXVMwIZfDGLFQYkke32fidqSRIig816CldvD095OsSVj8q7xyfZSPzAIOiVlsL8w== X-Received: by 2002:a05:620a:1001:: with SMTP id z1mr12329350qkj.204.1627846812598; Sun, 01 Aug 2021 12:40:12 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:12 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 02/13] iio: inkern: apply consumer scale when no channel scale is available Date: Sun, 1 Aug 2021 15:39:49 -0400 Message-Id: <20210801194000.3646303-3-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 Sun Aug 1 19:39:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413083 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 4E96DC43216 for ; Sun, 1 Aug 2021 19:40:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 38DBE61059 for ; Sun, 1 Aug 2021 19:40:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230347AbhHATka (ORCPT ); Sun, 1 Aug 2021 15:40:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230225AbhHATkX (ORCPT ); Sun, 1 Aug 2021 15:40:23 -0400 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60989C06175F; Sun, 1 Aug 2021 12:40:14 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id p38so7911721qvp.11; Sun, 01 Aug 2021 12:40:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zhQeMelYWNMHpFAMGtlyl1sUbg34IFFWmo4FOYtEzaY=; b=l5Kb87vnrHyjiimiX1rx2Duk6q8wFU9ih6mRQNbq/BMzM0Z5Ulig0sQUeyQ82GzMHQ 8icw6LDh8AC4K0U1EC6/J16qwctiwWkJP9uwH8RflV/KRjKZa2Cv9vJfDrjQ6lOATUTG nJwEGZvl1rbmvaKLzlOU8flUnfRurpF24v8KCScEIbvfwACLQLh+2Bgs52tCtnqWoydy 6fAWMtX7CqpaaGX8Gz3QT+lCMOGTQKTrQAqqkHeCTcmEYTMlTcz06cH71e1AolUYKKUO pgOTtSSfvlwhmZxW+IlzSon07Voa3pQXFSLkCc6Z0UK0gtfElKhTkDdj3DYjzhVdvBwj rHhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zhQeMelYWNMHpFAMGtlyl1sUbg34IFFWmo4FOYtEzaY=; b=Py/F+ZV6TbSOt53a3lKjtF84QO/S78C0mVTQEk/ysbMBePKZbKbOqsF+Hvph0rn56l rpltrrq1hwNc7AfM5hPSPXWnLWsg5aDMj1ECD3MS3p/azYKG+vUzor3alMpYyeY0L7bi Tm9Zed0NS25bB3J+gc1ktMQiZhDbpgK1ZQD9s6yf9NfzYNRfjbLh/UnNmhTptucayZyb YajnZq6q18K2EdHH1IaJ1mRVJLWmMcIWFCcPrFMCQheCBqql7u9O57DLHt4gwZUyUsZy LTWOepKsxOCZBj/35NzY0u0pVyECCWL8Bw2Fs07GQ4H2ZKasG7ElAtNA9Bi/Yl9pwNBJ FFZQ== X-Gm-Message-State: AOAM532iQL04uyOILf/QOftQAKwHhR4AOeR0O7pBieLqAhSRkQcITXdv qvC/hOF6gZqypkOLN8XsySCp9BZVSbaLHw== X-Google-Smtp-Source: ABdhPJwmVNiCFKDo7HlknpSdx1gyZIC9FQU9o3L/BdlZZN7gZF3gXesp4V/pXPBgKvGEBRX/irqwTQ== X-Received: by 2002:ad4:5ccc:: with SMTP id iu12mr12331806qvb.47.1627846813573; Sun, 01 Aug 2021 12:40:13 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:13 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 03/13] iio: inkern: make a best effort on offset calculation Date: Sun, 1 Aug 2021 15:39:50 -0400 Message-Id: <20210801194000.3646303-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 Sun Aug 1 19:39:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413077 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 0DED3C432BE for ; Sun, 1 Aug 2021 19:40:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E7AD861059 for ; Sun, 1 Aug 2021 19:40:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230348AbhHATkY (ORCPT ); Sun, 1 Aug 2021 15:40:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230186AbhHATkX (ORCPT ); Sun, 1 Aug 2021 15:40:23 -0400 Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EB5AC0613D3; Sun, 1 Aug 2021 12:40:15 -0700 (PDT) Received: by mail-qk1-x72d.google.com with SMTP id t66so14809365qkb.0; Sun, 01 Aug 2021 12:40:15 -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=QDDOb/9OIR5QbnjGLnasWpaABwJPXE0AHCUYUfFL+4gH6ZAkzkWqYQeZlasfXUww2T 1V88mA9k579I96q7Py7KxCHOqbkSf11hYsJAic42zRSqFyDVxkeFVkQQbKuP928/JzJE mH9E21tp61MfIrK4Tv+EAo0eB6rcXT+TWEf4cADcqEZTb1C5VqG0gBzMSIiTSc3W3mVq nhD0r31jSeXx6A+tz/aFaRU0g8gZV8Un8dN/rUscuWoK62ntH0VE1dKLP3Hfh7O+9tlt b14hB/aq0tSdszzphrqBExqAtQ3nJRu62nyeI/EPN0+ku/C6W/7WN6x+xzOzXanonRcK pkyA== 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=eOkWoJ/6xJoLAxn8O4XNfmKNiRywJ6ZQ+xbuxA3jTGlkdmZaHUT4hfAwajv0xhIcr6 QrmsZDw51+eVZUKyeTk1dhBitdFCq3QLxRIozRSLJrJgCXLeR5VzV2nA5nP/gIam/mRB OR8TBFPuwSrugQIvOdV2+IQ+/vMzq5p0jnX1f+inVl11CwtYeIIBRb3/LmqgtpUYvlWB mu/f8ZyzBpspGoSGVR7N+7qdKLhbt0EmJsXhAFPaUCrxKmzHFKL6YTAVtWLY6CkiLO3r MFgvi3EkB0i8lEwNFm6GN90o/1N5q23j5mZdcps+AGJv2Nj5fNCsZhRQWTzH989VJK+Z GnLA== X-Gm-Message-State: AOAM533ZoakV3uu+RrzKXSkcJjl/ro9spJzaU/j5NheyxEQNHlUEJsp7 lKWEP8OmsKnkkM2P5PXLPJk= X-Google-Smtp-Source: ABdhPJx9jtb7xZ3DQDx0IG0OCowA0LBaHciXzQzT6REPvulK/Hk+2niVhsK9ROrB8Y8GhDEXMfPJNg== X-Received: by 2002:a37:90d:: with SMTP id 13mr12315643qkj.386.1627846814549; Sun, 01 Aug 2021 12:40:14 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:14 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 04/13] iio: afe: rescale: expose scale processing function Date: Sun, 1 Aug 2021 15:39:51 -0400 Message-Id: <20210801194000.3646303-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 Sun Aug 1 19:39:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413079 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 CB535C432BE for ; Sun, 1 Aug 2021 19:40:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B3D11610D1 for ; Sun, 1 Aug 2021 19:40:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230072AbhHATk2 (ORCPT ); Sun, 1 Aug 2021 15:40:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230516AbhHATkZ (ORCPT ); Sun, 1 Aug 2021 15:40:25 -0400 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CA6DC0613D3; Sun, 1 Aug 2021 12:40:16 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id x3so14740172qkl.6; Sun, 01 Aug 2021 12:40:16 -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=hlz1mlRz0pIJ2/uHlMEkfOBEElQWI/RnQqjZdJLYyew=; b=oISa7DuCKwko9vd6uCu7LUGn+NDiJeJyTVxDcHYTHFiz2bFiXCG8mlP7JOtJnFbkoZ fVv4G/cn2DSFqOO9QsAOC+pzs6LPDMQv1ot1EaER49NYhyaTnjHg28VJZ9hf53RdhTPm K5zwb68ZcIsMA7QJAGXDvF+3jAC/j/dE6+wl/HFBsdBtAaEQ+DzIVKC8wJglrNqj3aDF Gqc9j1sYrU7VfNRR8VYqNCPJvzKR+PCdmK2mW8d7IwCPZD3X505bV/GDMgcfn9t8pQCE FmGXLcZ2m8uzgHwHFtdPSWLAwpOR+QRzlVyuIi49iv/9gdBSQZRMfD7rn3yn2f/3WBQ1 Z6wA== 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=hlz1mlRz0pIJ2/uHlMEkfOBEElQWI/RnQqjZdJLYyew=; b=a6llzC3eXzYKAwdSki4nm2eNNDG6iCjg24CuhLWIsGMDlr8n106EYyR2K/jJ8fT2mN EYC6fAyQc0MjXJbGyoDCfC/gx8zw4jLNmkodXkr93IXdCWsFdHc8FwCxfe10Hta807oA 1rDpzG2XGgJ0+jDiZ3gd4XOx1/hGoItwyLBvgBEp6oMZr05qmdRSQemn8BHMWkoqgj8t pZmrRWUXM9XtKonmxlXY7kaMrHaALYazH7uIokmlcukAYHLHPW98vGF4XxO/g+BnyX83 /F4xYJ01A/JqA7MIwzxEDejFE0RZQlJCIH6yaUjXhjFIttCi2z0J0lGRa1gTDJ5zxVxb Dshg== X-Gm-Message-State: AOAM530r5lFRAmzhJu+awLqti/H9I55zekhoF18FBp15IMWNzUih8siB I5Vx5gUQjFiUt27ToI5kdyM= X-Google-Smtp-Source: ABdhPJyQv2g8CrhE2Dufi5BzY3omQa53Ek9urWQnc9Lc1jkk3nini1/rKqwSUmvQAw+tBjoneUWEhw== X-Received: by 2002:a37:a004:: with SMTP id j4mr12193562qke.499.1627846815634; Sun, 01 Aug 2021 12:40:15 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:15 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 05/13] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Sun, 1 Aug 2021 15:39:52 -0400 Message-Id: <20210801194000.3646303-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index d0669fd8eac5..615f5d9cbb8b 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -22,6 +22,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { unsigned long long tmp; + u32 mult; + u32 rem; + u32 neg; switch (scale_type) { case IIO_VAL_FRACTIONAL: @@ -40,6 +43,38 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, tmp *= rescale->numerator; do_div(tmp, 1000000000LL); *val = tmp; + return scale_type; + case IIO_VAL_INT_PLUS_NANO: + case IIO_VAL_INT_PLUS_MICRO: + if (scale_type == IIO_VAL_INT_PLUS_NANO) + mult = 1000000000LL; + else + mult = 1000000LL; + /* + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if *val OR + * *val2 is negative the schan scale is negative + */ + neg = *val < 0 || *val2 < 0; + + tmp = (s64)abs(*val) * abs(rescale->numerator); + *val = div_s64_rem(tmp, abs(rescale->denominator), &rem); + + tmp = (s64)rem * mult + (s64)abs(*val2) * abs(rescale->numerator); + tmp = div_s64(tmp, abs(rescale->denominator)); + + *val += div_s64_rem(tmp, mult, val2); + + /* + * If only one of the rescaler elements or the schan scale is + * negative, the combined scale is negative. + */ + if (neg ^ ((rescale->numerator < 0) ^ (rescale->denominator < 0))) { + if (*val) + *val = -*val; + else + *val2 = -*val2; + } + return scale_type; default: return -EOPNOTSUPP; From patchwork Sun Aug 1 19:39:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413081 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 7E540C43214 for ; Sun, 1 Aug 2021 19:40:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C01B61078 for ; Sun, 1 Aug 2021 19:40:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231466AbhHATk3 (ORCPT ); Sun, 1 Aug 2021 15:40:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231150AbhHATkZ (ORCPT ); Sun, 1 Aug 2021 15:40:25 -0400 Received: from mail-qt1-x82c.google.com (mail-qt1-x82c.google.com [IPv6:2607:f8b0:4864:20::82c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66852C06179B; Sun, 1 Aug 2021 12:40:17 -0700 (PDT) Received: by mail-qt1-x82c.google.com with SMTP id h10so10331230qth.5; Sun, 01 Aug 2021 12:40:17 -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=u1EgV+qDKh1MLXoqKHgC3S7qm/xOWGqYPVM+PTaJ7PI=; b=OkBL/TDm7lmhfh9GZTA4v0xQJL9vFYSvcsV50bwr0UI570YfQSxVc20ra7Y1AlJofs GTdHjy+i0aDA/SfHb8F+q0xVOOWD9idQpB5WdCJJm7x/erQA/lX1QE5g2tRXNndoVXQv qdPfRuOXuCg8M71jBCrlBVIna5bOtj38iVZmv0n9686lq4GJE6lEaEhOckCYdt/P6Zbo yqbY7Gnt9O53VEwDxL/+RjNYjLCKzPwfh+eqkDFIYLm4LZdsI4lyQ5t8pskvJE78W003 wieHXyeohFxUv4K454SLpiNiIWX62sxzFBYZIsEDYhapvBUlVt/wA+q8qTzI+EQ3OJa7 pOvQ== 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=u1EgV+qDKh1MLXoqKHgC3S7qm/xOWGqYPVM+PTaJ7PI=; b=l3HzIYU2wzy0UbEmUjcS32PFK3/CFZlpxZttIeZ1A+pPKbUBlFx3J5uQFLLD7GMBwL 2FG77+OliEzHH5KTXU9bRbxo1GxmgRCHDEG8tKiH5BmIDYFX/5yeRinuDkETv23LiSUC 8FJYaqq4Y6+Kbjp6y4qay2UqLaEVjzQ1wv9aWGiNH6CNPuHDJCBTXkWN+km9dnrIMDQK RTsE+OBr01SQWCMGV7hXalYNv/b46H+54RE+j6WVn1EuOR7QDHeasDY93hrCPQ7wG4wE +TGzNCE1SvwdY8qz6Nqgs9m6q6CT9l+3xy3G6vk8GxRBP+6chzMwDkuCnFKXdYYcleea sEmQ== X-Gm-Message-State: AOAM533zJsht8XR8I/Ky8inISwlQo7q1SmpMN5GurtytfR9YI/S18w0q lnd+R27w5OVtb2LrZP4o4YU= X-Google-Smtp-Source: ABdhPJy+RaA9+D7viTtFYPT3NN272SlzS8VXIjCWN/4cKC2uI2NEPpiiIIUoH+/2x+7lpKa2s4Gbug== X-Received: by 2002:a05:622a:142:: with SMTP id v2mr11421423qtw.264.1627846816592; Sun, 01 Aug 2021 12:40:16 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:16 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 06/13] iio: afe: rescale: add offset support Date: Sun, 1 Aug 2021 15:39:53 -0400 Message-Id: <20210801194000.3646303-7-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 615f5d9cbb8b..623744da269a 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -81,11 +81,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) { @@ -112,6 +147,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; } @@ -188,6 +264,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 @@ -352,6 +431,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 Sun Aug 1 19:39:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413087 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 A501BC19F35 for ; Sun, 1 Aug 2021 19:40:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8AE0861050 for ; Sun, 1 Aug 2021 19:40:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231787AbhHATkb (ORCPT ); Sun, 1 Aug 2021 15:40:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231279AbhHATk1 (ORCPT ); Sun, 1 Aug 2021 15:40:27 -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 6369FC0613D5; Sun, 1 Aug 2021 12:40:18 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id m11so10321912qtx.7; Sun, 01 Aug 2021 12:40:18 -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=3wCGWQrzyuyd2FP596gwvPjVMRpxLtI0wwZ+j1tmNrA=; b=S9lp6eWG7lBaoA98OImEGiEoKIIDOTtu+o9P1tKGLIK62LBeWOfT1BioOtb+mA+q5F WOm1cRz3rsBoB/kceGolZzn/SfX0rv7QJnck+HDNTYwEAFddqF8fMyqntvZJCK2bVyJa x5w+E/iijjZwGHhvdaIvgEqHipjvbgmQHl8uygsCprcHzD51IX5h/iXwjo7u35KWtz8H HDdL54eKI/eD8LFTMuIhpmF0fQGBVlmcnymPXOkUOjKd0EIYGhzD9YtDxa4hFqtGogEk 3nxegSYimyzjyq1U9FVnEDOoYtfCipo9899AvRPfFEDqAEnZenwAuZImYrLdBSyhL5Qh OWsg== 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=3wCGWQrzyuyd2FP596gwvPjVMRpxLtI0wwZ+j1tmNrA=; b=qS7ijG0mPdXOQWBRTBeykZApc9VCIOMgppBuRKoGLkY+LHs0Vg1MoscurpA0EtD1G3 CkSM60BQdvoKvKMvrAA325Vk9qlSvDK4jVbJIiGpHO90g/EUM3yiTZUuRbXz6Sg3n0j3 G4sp74VWmY3phf9dqBnvXrgCoTQWVX8qqGD/tMbObcKCe1WLDa+qg7uF4Fha9wRy1bV/ c+vCAbqovnT93N+8l4f/lS6D0nj7zUuTg1mxDsdBPRdELsqxUJEhO32bo9ayRaJlR6b/ vZSK+sr75TxpYOCnoeGLR/D5UMV5NgTkiF6JQ7ALfqeBKZz9MIcbYBiIB8ad0ErP3YJX 0fnw== X-Gm-Message-State: AOAM533Nv+yIRci/hwWtgTLZo9FRkOWjX0+0hEYM5/+rNP/k5oaJPGbZ HiQKgMLWHj+yHPryzpgIYFg= X-Google-Smtp-Source: ABdhPJz++KDa/exeB4OJsZoSUuZ3V7OvRvdNF9/t3RzTkLFN0Q3JUzTclHN1Et6fvW3CcUozEvOdbg== X-Received: by 2002:a05:622a:104a:: with SMTP id f10mr85170qte.340.1627846817584; Sun, 01 Aug 2021 12:40:17 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:17 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 07/13] iio: afe: rescale: reduce risk of integer overflow Date: Sun, 1 Aug 2021 15:39:54 -0400 Message-Id: <20210801194000.3646303-8-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 623744da269a..abd7ad73d1ce 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -21,15 +21,24 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { - unsigned long long tmp; + s64 tmp, tmp2; + u32 factor; u32 mult; u32 rem; u32 neg; 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(abs(tmp), abs(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 Sun Aug 1 19:39:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413085 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 08FFDC4338F for ; Sun, 1 Aug 2021 19:40:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9DE960E09 for ; Sun, 1 Aug 2021 19:40:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231728AbhHATkb (ORCPT ); Sun, 1 Aug 2021 15:40:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231250AbhHATk1 (ORCPT ); Sun, 1 Aug 2021 15:40:27 -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 50F87C0613D3; Sun, 1 Aug 2021 12:40:19 -0700 (PDT) Received: by mail-qv1-xf2c.google.com with SMTP id d3so7920605qvq.6; Sun, 01 Aug 2021 12:40:19 -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=r9noamx1Lea6uM4ckRlZuCQifseImk8SgpqIftuzmWw=; b=rU7aL0sXno4InxJ3qI7n0CaL6/QT223xMb5SMKSH1a/08QZWB6INzjeZq4RfQhJygg Agqkmj7nUPOL1+qwyrNvFPIWqfMhuyqhFs957ufw9ZzSYIhLWRXvHPYVCbSQaPsu3Csp Mg+kowc75abuhjnQI1PHaKcJg5XCwF0jIUCohL1gY/8N7jW7A6jzHc11ShTSoZo2DnNo 7GHDVmh8JaSO9AMpyNa3LLZyYHNdHUfsS4X4L0mSSQSMfvBnQqoEXHSVTVdUMllz1ijQ W7sYMzifcT9iM5kBzgZgMh7rGQ6BK82/Id2YrTXZcw8meXaTiidGqyThDHZDSk4amg4I htYg== 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=r9noamx1Lea6uM4ckRlZuCQifseImk8SgpqIftuzmWw=; b=IoHGGryTgmWmvWZ8bjSc4F3KRSGWKZhWaiBSopiUe+Es29O0v/v08RhnKWAT0IBSgA f71v/dgKIKCdxLz0nkeJ/Krr/Iw4CuT1kWjOQmLsvAu9TnSfy/qvcz4rbKODdebhjycn 1IMAy7EhSQQ8WGsTbbxuI4UeWdFDPnhpMWeeJsQ3ysyO5m4t1azl+Ap+4+C4/dMbGNHX nSEK+lUS7xyI4Yy4ucxfWNLQWjG4ZoRyH3bbVteHj/1OoZpoPAfohCvQ+btX8dVQsQtk yoDoObBSGf5rLtT+L63gt+5Fs5UwUpQtxluahluLG2VxaKHC51yaMnJxDYQQ+G5MtIEL /NJg== X-Gm-Message-State: AOAM533f2r8Io+yxUHG/MamLk6XTx9NplBAIp1hLaQW4fvdKud5x0T/x Ka35h2OxY9woofWrbPSMNNQ= X-Google-Smtp-Source: ABdhPJzxuNUFugRAkv2VJtND5tNBzLF/XUF76s37o8ZAT/Wo4GTzUv5JdIQ3XZhKKjtlrpqpJ2pwEg== X-Received: by 2002:a05:6214:3001:: with SMTP id ke1mr13377381qvb.52.1627846818542; Sun, 01 Aug 2021 12:40:18 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:18 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 08/13] iio: afe: rescale: fix precision on fractional log scale Date: Sun, 1 Aug 2021 15:39:55 -0400 Message-Id: <20210801194000.3646303-9-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index abd7ad73d1ce..e37a9766080c 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -47,12 +47,17 @@ 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); + if (check_mul_overflow(*val, rescale->numerator, (s32 *)&tmp) || + check_mul_overflow(rescale->denominator, (1 << *val2), (s32 *)&tmp2)) { + tmp = (s64)*val * rescale->numerator; + tmp2 = (s64)rescale->denominator * (1 << *val2); + factor = gcd(abs(tmp), abs(tmp2)); + tmp = div_s64(tmp, factor); + tmp2 = div_s64(tmp2, factor); + } *val = tmp; - return scale_type; + *val2 = tmp2; + return IIO_VAL_FRACTIONAL; case IIO_VAL_INT_PLUS_NANO: case IIO_VAL_INT_PLUS_MICRO: if (scale_type == IIO_VAL_INT_PLUS_NANO) From patchwork Sun Aug 1 19:39:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413091 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 B3B56C432BE for ; Sun, 1 Aug 2021 19:40:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9D98360E09 for ; Sun, 1 Aug 2021 19:40:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230516AbhHATkc (ORCPT ); Sun, 1 Aug 2021 15:40:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231534AbhHATk3 (ORCPT ); Sun, 1 Aug 2021 15:40:29 -0400 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F0EAC06175F; Sun, 1 Aug 2021 12:40:20 -0700 (PDT) Received: by mail-qv1-xf2d.google.com with SMTP id cg4so2893866qvb.5; Sun, 01 Aug 2021 12:40:20 -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=9Yrod4zWvzjowEkPDcqfhN1HMp0hOcbRDJShGJNFTJM=; b=JSPwF6UllFYFfQ0ij9zBWHs7B/rphrIrvyxz4HwxK0hTnMOXFoH2D4IiQ1TXaRMtzZ Gq7q4wDMuwWJ5SGDjc0uLdduS7Z9uBKqf9KZ/DRJN63hvfbRNJH96N+xldjGbw1YOdFT 7yDZmRG+pNeNfqf5AtLopFj2oGZdNJiWGolXjtDxWzMgXYNg0JxNzYkxaLcCtDCCinmf bgu9p6iM1/rffIAEF9NlZwsTU8eMgTk7t0c/PknWEuP3h6h0H8cDsqrdoiTfJEgQlswD bnFQMM3eQK2t89UOkTT67DfMWt+t8VzWa7aomPBVNxJY5B/WtGSXM3cWWfG65hsskzgw puAw== 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=9Yrod4zWvzjowEkPDcqfhN1HMp0hOcbRDJShGJNFTJM=; b=j6p2Qfs4tiE3JFqUrNpsyU0Lf0Heo5SErDitEqdz86EftyGZDyOBCj+3r30Hl2VdVi O8Q8tKGd0IZz+Kjl+buMh4Jla+yDocwt4i75znbTvEE2a8JmECZ37Hpk+Y1/Lwd0S7XR IKAUy0mR/IAUoE6yrZ5ZT6WDy/Cf2VsMlWqu4SthcTfL/72Cz6JV2SQL3WVwLfDOgrGK D2L43Utx8kOXXQLvNgGv+CQwo8pkcHHpuFt9uJb0V8HsHXn9aLWTXH1mPx8SAc9iSe3n evUmsJopxWJLB2A7EZ8N9iFaJaow1W2mg87+fAAi0st5LlqA7zbrqIGOd9GKwxPcaNaa GnIw== X-Gm-Message-State: AOAM530YQzrhPN3LSjA/w4X2pTOThKcA+TrAF0+agcg2OLTOxhxD1TJF 17v4NQM0kVT8A5upbv7GCE00tndqjNT1GA== X-Google-Smtp-Source: ABdhPJzQOs03a/4j6sFYEQDQAUOkV9xm1Qel6LglZd4uxxGCBOPRiok51Q6xC4rsNn9JG4L4CwUXIw== X-Received: by 2002:ad4:536a:: with SMTP id e10mr13257288qvv.9.1627846819528; Sun, 01 Aug 2021 12:40:19 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:19 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 09/13] iio: test: add basic tests for the iio-rescale driver Date: Sun, 1 Aug 2021 15:39:56 -0400 Message-Id: <20210801194000.3646303-10-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 --- drivers/iio/test/Kconfig | 10 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-rescale.c | 483 ++++++++++++++++++++++++++++ 3 files changed, 494 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..872ed4ed140a 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 && !IIO_RESCALE + 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..9008fb223d9d --- /dev/null +++ b/drivers/iio/test/iio-test-rescale.c @@ -0,0 +1,483 @@ +// 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 = 123456, + .expected = "1240.710106203\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 = 123456, + .expected = "-1240.710106203\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 = 1234, + .expected = "1240.847890\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 = 1234, + .expected = "-1240.847890\n", + }, + /* + * INT_PLUS_{MICRO,NANO} positive/negative corner cases + */ + { + .name = "typical IIO_VAL_INT_PLUS_NANO, negative schan", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = 123456, + .expected = "-1240.710106203\n", + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, both negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = 123456, + .expected = "1240.710106203\n", + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, 3 negative", + .numerator = -1000000, + .denominator = -8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = 123456, + .expected = "-1240.710106203\n", + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, 4 negative", + .numerator = -1000000, + .denominator = -8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = -123456, + .expected = "-1240.710106203\n", + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, negative, *val = 0", + .numerator = 1, + .denominator = -10, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 0, + .schan_val2 = 123456789, + .expected = "-0.012345678\n", + }, + /* + * INT_PLUS_{MICRO,NANO} decimal part overflow + */ + { + .name = "decimal overflow 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 = "decimal overflow 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 = "decimal overflow IIO_VAL_INT_PLUS_NANO, negative schan", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = 123456789, + .expected = "-1256.012008560\n", + }, + { + .name = "decimal overflow 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 = "decimal overflow 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", + }, + { + .name = "decimal overflow IIO_VAL_INT_PLUS_MICRO, negative schan", + .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_FRACTIONAL_LOG2, positive", + .numerator = 0x7FFFFFFF, + .denominator = 53, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 4096, + .schan_val2 = 16, + .expected = "2532409.961084905\n", + }, + { + .name = "overflow IIO_VAL_FRACTIONAL_LOG2, negative", + .numerator = 0x7FFFFFFF, + .denominator = 53, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = -4096, + .schan_val2 = 16, + .expected = "-2532409.961084905\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_NANO, negative schan", + .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", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_MICRO, negative schan", + .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 Sun Aug 1 19:39:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413089 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 A2D92C4320A for ; Sun, 1 Aug 2021 19:40:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92AC060E09 for ; Sun, 1 Aug 2021 19:40:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231956AbhHATkd (ORCPT ); Sun, 1 Aug 2021 15:40:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231426AbhHATk3 (ORCPT ); Sun, 1 Aug 2021 15:40:29 -0400 Received: from mail-qv1-xf36.google.com (mail-qv1-xf36.google.com [IPv6:2607:f8b0:4864:20::f36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36BAFC0613D5; Sun, 1 Aug 2021 12:40:21 -0700 (PDT) Received: by mail-qv1-xf36.google.com with SMTP id g6so7915401qvj.8; Sun, 01 Aug 2021 12:40:21 -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=bdTcpTrw9RjzJaHUr2c/qhrWaB2G6iEuRUkeRd5GNB0=; b=Hz0G6hFteC/nQgsf1a58+ec1X8oI4x9TS60CPjUwbdpVcXgvOyen3zwju4YqInzzvp DpKuPh5y3JOE2+DQuHmYMGcdKImZZzhe2t9s5tgIGqZKGtrj2pnHajKVy9J8Z5GrCKD1 GDvA9tJnjJUdvnOvzNsR+33rwM/uCH4yspV8MLBDsvQLvHApRpquTQbtesRnT6AL9NMq ZpPvs+HoEKRWZ32CIEDnrjcJrbsbIAfBMqHbRppnu+y7mzcrSnmC8pfCw3pC/xHFLXcq iAAEnIfoZh4jGX+I068mjngnfMpSolUbYJRC+G2nqSyBxXPCe2Ps1XK7RZGEuvFz2g0m zYFg== 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=bdTcpTrw9RjzJaHUr2c/qhrWaB2G6iEuRUkeRd5GNB0=; b=rUccITH7Nbgz+Tw7wJ4VtxlW6oalIrWGzFCARNXnsoLZ/3gtVAPVrrNCrGAG3A6Lj3 QebL1BqPr4dTt7K3s812szFF2j0ge0tyNIj4X56IiQBtGvF0uXU8NUfOOChrTK/3m8// a9HEGolwwB4CVhqiEbqjWT7GdyDIwroyMKlPRRulgUghyAorcmqqaMx/zI2rBKeSR1Ks NNXlBsaKROgSRFBWcT6DebVThaG+zUs/bmikb86wKMFcmpCLb0+lfc4rK+Vtgbw3J3uv V4jsdispBTGcrhLvKXqMMbqLvso1AlMQnSna671vcmIIcDawHj2bYYsCA1wM40Cwv4XJ P7bQ== X-Gm-Message-State: AOAM531NrzLL6JkkTB1StLXmGRu46gN7WeXJETRnwfct3BHqsAdpe9gB Fu0EA5XP61RzJTPDkvPorhSW/xwgoAlmuw== X-Google-Smtp-Source: ABdhPJwVTqo8GXUsUwbXMD6p1SUEwcAkGbHKjQSF3+qr7aE+CB9Xdgv/RRmq14KiqmgLDsXJR4t5rg== X-Received: by 2002:a0c:e6a4:: with SMTP id j4mr13069190qvn.16.1627846820470; Sun, 01 Aug 2021 12:40:20 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:20 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 10/13] iio: afe: rescale: add RTD temperature sensor support Date: Sun, 1 Aug 2021 15:39:57 -0400 Message-Id: <20210801194000.3646303-11-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 e37a9766080c..9683380aee5e 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -380,10 +380,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[] = { @@ -399,6 +441,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[] = { @@ -408,6 +454,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 Sun Aug 1 19:39:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413093 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 0D575C4338F for ; Sun, 1 Aug 2021 19:40:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EA6E860E09 for ; Sun, 1 Aug 2021 19:40:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231981AbhHATkd (ORCPT ); Sun, 1 Aug 2021 15:40:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231248AbhHATka (ORCPT ); Sun, 1 Aug 2021 15:40:30 -0400 Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 333CEC06175F; Sun, 1 Aug 2021 12:40:22 -0700 (PDT) Received: by mail-qv1-xf30.google.com with SMTP id cg4so2893886qvb.5; Sun, 01 Aug 2021 12:40:22 -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=JW0rtRe7VfEERJdhqxGKq5/nOxmZDzKEBQ4TjMWfoV4=; b=e4vK7gm5neVP1Yud4uDkOZwkAWT1hqIck7JLX9LoQB7marbuFzxCN3XkD5Vm0hWGEs obxB7YQgVHZ2CZ7V3UmdVxCy1iZvupUTBPae4n2+i2z9GpiZWtYSmZZ3rB6DNX6STYLF ZT+1Gsw79az1SQaOCxN9EHto58Y7BbAYr9ddeN0zUakKJ5M1Ls9R8ixupN5Z++flABtG Pf6NVOlSI8mU96fYCAll710QlX7BMi8e5WoLn7cZioXB7L6DQ31kkgGN63TOesMCtekV 7SBUXXE9S4iYUuAxQMqm/5SrVRu4tiD0hnuGHp721oA4KYKHSV44/xRQ6mzkIZL1uWUG I6hg== 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=JW0rtRe7VfEERJdhqxGKq5/nOxmZDzKEBQ4TjMWfoV4=; b=TrJnZP6FXfXWHsnIt8B9XOX/Ut6zaduxVr6zh+XpiA9shbm5I0i+1rALV+pFew4WP7 Ty9V9Om6hqUcrtMqMRtQ4MreMCp3kbdrM92L3TmmPGei8IfbeNrvcnfInwvumWF6bcdF Fqn0yGJb6HLM7n03rgq8eTkGvwS95ltWPz+7XhH1xI+9opqRol8ojVqbVGQquVPQzFG1 GALZLGfDIj6yzaHBUtv7fBkUGR9YeKGguQVQSTb73rJP4n2sisw8QeHJmi0pP5Q+3Zea Bu6I3RKbT7OEoeVZ5+zTPy48sJMqf7rlIjS2iFnizvahErmRc1GWi7itRwn2+nf1bL9P 4KIQ== X-Gm-Message-State: AOAM5308Gp0J/fH6ZkpRJWiZrl3YjYkO3PXKr58/xF+0Hv45OGcMN+he Xr8feaphchl6xch7E8MKsEc= X-Google-Smtp-Source: ABdhPJxqyODdZ/5VFdRJ6S7oYqaJMWg4dbsT7hsUfmfQ8zdIevSB1SqOC6sDWA57FHPWUH2poA+CNw== X-Received: by 2002:a0c:c490:: with SMTP id u16mr12815117qvi.26.1627846821406; Sun, 01 Aug 2021 12:40:21 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:21 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 11/13] iio: afe: rescale: add temperature transducers Date: Sun, 1 Aug 2021 15:39:58 -0400 Message-Id: <20210801194000.3646303-12-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 9683380aee5e..dc638402782d 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -421,11 +421,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[] = { @@ -445,6 +472,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[] = { @@ -456,6 +487,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 Sun Aug 1 19:39:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413097 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 34826C04FE3 for ; Sun, 1 Aug 2021 19:40:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E19561050 for ; Sun, 1 Aug 2021 19:40:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231925AbhHATkk (ORCPT ); Sun, 1 Aug 2021 15:40:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231785AbhHATkb (ORCPT ); Sun, 1 Aug 2021 15:40:31 -0400 Received: from mail-qt1-x831.google.com (mail-qt1-x831.google.com [IPv6:2607:f8b0:4864:20::831]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28FF0C061799; Sun, 1 Aug 2021 12:40:23 -0700 (PDT) Received: by mail-qt1-x831.google.com with SMTP id m11so10321970qtx.7; Sun, 01 Aug 2021 12:40:23 -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=khCOP/CAZUFPUggKGpoFL2EBdz3UXIPUwp7FrRee7+IvmWLmGshoYcRazovwO/0bIR HoVVLLjZqaZnBdkoaOuQ76mRFC4KxexnGYfaSw+oC52SbTccU1/FKYpYlO8Fgbnj+C++ JuJE9dB2OeIjVtyRm9X9MpCXDp7oChAmCTfgAGz5ce1jFEukaxLmYu2glBrLDygCGKXG LJglGdScgDzRqSzMs1GLNfL56kchOsYVvKqoH1/PcYqNwVNd74LPAZEhpcDtpeOXsIT7 2xtkGIl8KSpMdVzsdI9O/G2QckA7KHiIYQadHVuZOv9XJ5X6NKrYC9B089B4kqMORwq6 C3xA== 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=AvEbb67VQ0wT9putbLrdlPjcZ98jUtQMsJIrN12cnwYZyY5uHMRu9shufWq8oNdVNa 8fjD5KOUU83ptCi5AIaID6t+X/1rm8dp2X5t0HYbW5eart3qO2s6oJMEReE558tA+Fcd hOeyJWZM3KQ8QTNucR6qqr3ApdJSX9sByHJR+5Ot1B4SAHldJPVOFVEP01GMz91SvLHw Du/D68YFjDZtA4WJUYMoZXtj53+LRxuAah5/AY9UGxV85lfoK+H1fyx1aut/fU9z6z99 Kb1nwlYZmW3Z2HjTBCYpoPAhwX2V8H1SH4WZT8+SKPbzEqT6GW4+v8Hj6vjf13DL3G3N MG5A== X-Gm-Message-State: AOAM533DeHDrOjeci8UAnVx2tK64iup48EB2aB1qq2zWG+Rsk+392hQa PEhvpZkgMc2Kin4WhkGIH+0= X-Google-Smtp-Source: ABdhPJyc3xX/ss1pVYBdk0wigqmT67Ms891lDBijJPr/6O0UQ+cBpw7QnOhPurWBb3LXEkHKvRXALQ== X-Received: by 2002:ac8:7545:: with SMTP id b5mr11272072qtr.297.1627846822332; Sun, 01 Aug 2021 12:40:22 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:21 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 12/13] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Date: Sun, 1 Aug 2021 15:39:59 -0400 Message-Id: <20210801194000.3646303-13-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 Sun Aug 1 19:40:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12413095 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 A0EA0C4320E for ; Sun, 1 Aug 2021 19:40:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8A4C961050 for ; Sun, 1 Aug 2021 19:40:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232051AbhHATke (ORCPT ); Sun, 1 Aug 2021 15:40:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231836AbhHATkc (ORCPT ); Sun, 1 Aug 2021 15:40:32 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18207C0613D3; Sun, 1 Aug 2021 12:40:24 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id k7so14726341qki.11; Sun, 01 Aug 2021 12:40:24 -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=ia7RMXcuDJun2fDwzVlIawa2qDQplWBzZcO/n2ULYNU=; b=XtG8c331aXAFHWjU41FzyHNp9YISwpzSfKqlNYD2aqjgdsPL2xntY/MmEFnIa4k5lh zXKl/KCPUKbzLJzqR1M05Rce+iQ0tTLH0Nnp6PO9qfIvIALQnhHngOmNSruDGQys5y6U 5ZO3n6zx6FajqGj4gY5pOSH2TK6lqotwmbmRFAgVRYVy3sM0lPk7fcbLba5w5bOYQSPb gfqdxCKxP0AAptr8biDgCOs7nr9DS6lgR0hkXQ3nVIGfr3OD/FTGrxrm4aWYDBmV3AI7 38AoB4jyW0CE9zWSFh0pZHWF+mDaxP8pu70oAfLeznmp5zte35Tg5BFYYvGvYqXlhuNx gSqA== 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=ia7RMXcuDJun2fDwzVlIawa2qDQplWBzZcO/n2ULYNU=; b=sZz/Ej3yXs01NWL3gQ0lKhhxSBjFMd1L2cAgqyEAPFSKmCGiWAOfY6c/+p9jWhQB8h He481p/vgvXRL+FaLFyeNEJaNTEwZPJ+q3i6vzAiitXpKrXaiJfdd0BftMSIcgiStENN nczbFX1fSUOVU5a9Dr01BNlT1T7PysKTAnZLJ5s3Mt+8tIRZ41gYAltZNNr7cRqvMtT5 Q4lbEH345r6BSCp2/pmg+Zoh17myEGY9wkEwj45vljbS1IwN/+JhwsDWN5xY163Zv7ax OTu32iILj1Cz/w2KnodsJomtvofr+zqplZH9zVtUsHSq8qF/dC1yAjAOj7ykc9xDPmN3 O7oA== X-Gm-Message-State: AOAM531s/ZH7icaN2WJ2jDAKW7HFgx10Gh6A2gEcypPXVwY2WVAM4OhX 8xUJd0faMbGAO9lzN3ocJas= X-Google-Smtp-Source: ABdhPJwMBD/Z/a/+l7yLRWBNjfN7HM5cTgL9pAOUnYTkEFZqfTpDnPzG+QaPKEIZxQjnnWFMSM40Ig== X-Received: by 2002:a05:620a:6ac:: with SMTP id i12mr12763298qkh.198.1627846823318; Sun, 01 Aug 2021 12:40:23 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:22 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 13/13] dt-bindings: iio: afe: add bindings for temperature transducers Date: Sun, 1 Aug 2021 15:40:00 -0400 Message-Id: <20210801194000.3646303-14-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 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 */ + }; +...