From patchwork Thu Jul 15 03:12:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378351 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 58797C12002 for ; Thu, 15 Jul 2021 03:12:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3CE29613B4 for ; Thu, 15 Jul 2021 03:12:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233030AbhGODPc (ORCPT ); Wed, 14 Jul 2021 23:15:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232770AbhGODPb (ORCPT ); Wed, 14 Jul 2021 23:15:31 -0400 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56624C06175F; Wed, 14 Jul 2021 20:12:38 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id 201so3809957qkj.13; Wed, 14 Jul 2021 20:12:38 -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=SXLG1AME+jZDiQqShuO4lpIRMS9uk2FqeRX8KwMynZa6qHxraMumJPtoE+KOcoEP27 NRfaF7avW9GRpi8uoPHBWsBXJSWkVnbzh38md12tnZkYiFFZ0iL39lb4BJMmqds7ZGXi ZMK9MKhO9oLyO4TCmN2+gqGPfzG/8OGOUhm/6RFPwtUpvdANj1ybGpU63OttTGhWq1B4 bHBXcTL3u9YoHJxYCV+21PEq8pYW4vEhCeFpCT2GYFp86f5s4MhCK5iuXBlTkPyhsGq3 zliP96Y36O0OL+3YOfSkt3eCoWvnwo+++ctA9DHqbMrWkBRSByCFdYqmfDyB1OFzm3+h eZWw== 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=hFZXwBxRWmsXJQkXmnuvusTVp6sSdQOoInID5hteDy5LfCLJ4cTEdTbquD5IntXiUk PRFpPEBD5duzRYtHfGB0ULQjAAsG5r01YhJTE0fQ6wLyzSXh+jtskDS1tfWNFHIHaKKd Xj42vGh6RUmJRAsIQrrXzJlG+vOTlEZYgCkwdrASeyYhGmDgJ4fpSPLBCzkXvWI4DQfR 5HyFtcJqsJOge2j0mnxhkHk23wUcyEJGzCMAFO4MyPXs117MABnC82CeNgchP4QVCD5e wVvu7Agh8FMETMRaP4PtKCc86OJLuc+gZK9MXFk/zm+Nx/c1KyLbtQOmKN3EMGgLohwx ZXqw== X-Gm-Message-State: AOAM530TU6BN4qoIrLKhJSnPSGw1TUv0LOGtSUeR0zr2FqekH3BiOvk9 9LCtL/ore6fnKpsZ783tzTE= X-Google-Smtp-Source: ABdhPJw4L1G9mU41qAAtvtnmbgrKFIwht/EEvVq66G/8c7BnBFneE+CvHdRnYhVkpVQRk03F0KcXqw== X-Received: by 2002:a37:b983:: with SMTP id j125mr1435238qkf.482.1626318757409; Wed, 14 Jul 2021 20:12:37 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:37 -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 v5 01/10] iio: inkern: apply consumer scale on IIO_VAL_INT cases Date: Wed, 14 Jul 2021 23:12:06 -0400 Message-Id: <20210715031215.1534938-2-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 Thu Jul 15 03:12:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378353 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 40955C47E48 for ; Thu, 15 Jul 2021 03:12:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29CE1613A9 for ; Thu, 15 Jul 2021 03:12:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233173AbhGODPd (ORCPT ); Wed, 14 Jul 2021 23:15:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233057AbhGODPc (ORCPT ); Wed, 14 Jul 2021 23:15:32 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44183C061760; Wed, 14 Jul 2021 20:12:39 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id 23so3871139qke.0; Wed, 14 Jul 2021 20:12:39 -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=h5DRdorl/Cv+oBCaUXejlaBxd8o0gxonA1RPHhSv+DJ4AKfOKupLmfbs6sYjhtUUT3 61jvAcuMFZb4vv+n8Hf8E+hmc6GgmPkg7MpzYVYwfNb8zUIXsCTNS7GZdUmMF+nLViLu OLotoUm+H/N5hMyiNJfEZtpukayfpqCWqU9CO6gbWK8vERYzIq3vDoaHNRdjAoCGnX8A HvIMwphtM0ku6CqgdhDesgMw+VoTYISpdGgbOZZXC6W/fkZQBa9sR2DL+ZuXcGx3TDnd zVClW7wKEg0b2QvVqNO5routjGZamDaRPol60JwRRosXsp01NMbsfTAf+yOndTsWcqzD InXw== 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=XGL+0FyiIzju5mlVLmbfwaxHkEA/KCbhMMv6lGM0yi1cshpKcVKgR0tQFw/ZuSwDil D0pKkRCiTk5+EFuImrANqzdDUQaW6oOcvuiwTnkHCD73b/I6ymqFiKa2FSXOmileX/OV h7GleKy3sg20LGM7sNNAC8jgd3tOMhjVQO8o6TaVoIReZQYpY9UUM2OvipDpJ5wwKJX/ yCfKhwNoeAurcsg1wmJrcJLspDHzbbAEhTojoG11GHL1ik3FcJiGNgW23JSwDS0UHyQK v9yGnybO3HL0JzHoy6l89xFX+Rov4tGomYEYKszV36/urKJDFMu3BhI3UK1yF2G5XyuE TvNA== X-Gm-Message-State: AOAM533DYTKe1hY6R+rvCc9zLCuxi275xmeUZG1k3R3NnmivueXx0xfX Xkipzo4KDa+NOxf1+7bhWQc= X-Google-Smtp-Source: ABdhPJybJtHNP8AsTErA+fNnk3JfPtodx7D32j58h2ivpHuFUOJIZ1jYFVRvoa/3Nqku4ZC7aaaZ1A== X-Received: by 2002:a37:43c9:: with SMTP id q192mr1472077qka.470.1626318758486; Wed, 14 Jul 2021 20:12:38 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:38 -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 v5 02/10] iio: inkern: apply consumer scale when no channel scale is available Date: Wed, 14 Jul 2021 23:12:07 -0400 Message-Id: <20210715031215.1534938-3-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 Thu Jul 15 03:12:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378355 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 04922C1B08C for ; Thu, 15 Jul 2021 03:12:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DFF5D613A9 for ; Thu, 15 Jul 2021 03:12:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233210AbhGODPe (ORCPT ); Wed, 14 Jul 2021 23:15:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233203AbhGODPe (ORCPT ); Wed, 14 Jul 2021 23:15:34 -0400 Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 320B8C061762; Wed, 14 Jul 2021 20:12:40 -0700 (PDT) Received: by mail-qv1-xf35.google.com with SMTP id i4so2147996qvq.10; Wed, 14 Jul 2021 20:12:40 -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=ktixEYDEVNnbF74lVxjv9jgUdBZOLbrHEfbaIXe+S9yjG0+RMCXjpopBCzHmDxq+6R Dj4c+QK0cQtwkV6/ZV8sPSZILtTLZhP3I6SUxzjiRsGgGTz5kxfmQ98IlJbfAboD73OK owzBT9PM9wsTNlLdKyLSDa1/kqLWr1jVNNp1Y9K+6bGERaA/I9RaGF4ERnBmdM5l1Jca FrGwtJktm08v2gmdEJO9Q+bVY6emN4jU2B4igq+HUKHWqftSMZLq1vLUxHZCGkAbjYFx lU/6qWidSu+UTMtSz3pJwg95vXnZpAMCS43ZBjQQaY/pGzp9veUh9hSfcRUFsyMRm7F4 Cvqg== 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=ASmFYITtJ/CPlyzSFvO6llvfygHsXMPfLjj4g9136rBS59ahfkl+tZBGDwVETFBb0R t7IahSX/RCTJrFXiroZggYCq7ojS6vV76BvTZOUbjgNhIZuOmKuO+5EZrh/jsD3fVTwD hkaqjR95crbk71zXZEzj07JU8thSa6lKKRFZkrJKdXW+lKd5VPghuOdiF5PN9qeYt7Ze WXJ9WAnokCTg9o/NJng2bfSWCGX5GRKWmtns0Vv1TyZb7OQlSm+8v5wsRAk9/cYLg1TF pBy2P1z2WC58pIanHOiu3AE6/FmPR7S21JofzDWXr6CkjIiPu1YPtwU89Mrh2y/GoBWf iQFA== X-Gm-Message-State: AOAM531/EnRiIft81cH4Hpeu1b/SZGfnElOF8akZ3bYsJzqgLNOOuj+L STEd6Fhn/I7+IheO2QuWj6s= X-Google-Smtp-Source: ABdhPJzIMh0zeoSBwJFrjf+LAo5y5X5Bp0wecd1qRUmjTxiQne/KjylzYcrTCoO/vvCTPM0RQZZfiQ== X-Received: by 2002:ad4:5ca6:: with SMTP id q6mr1604863qvh.23.1626318759467; Wed, 14 Jul 2021 20:12:39 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:39 -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 v5 03/10] iio: inkern: make a best effort on offset calculation Date: Wed, 14 Jul 2021 23:12:08 -0400 Message-Id: <20210715031215.1534938-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 Thu Jul 15 03:12:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378357 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 824F1C47E4D for ; Thu, 15 Jul 2021 03:12:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 67EC7613BA for ; Thu, 15 Jul 2021 03:12:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233260AbhGODPg (ORCPT ); Wed, 14 Jul 2021 23:15:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233174AbhGODPe (ORCPT ); Wed, 14 Jul 2021 23:15:34 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D804C06175F; Wed, 14 Jul 2021 20:12:41 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id m68so3825510qke.7; Wed, 14 Jul 2021 20:12:41 -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=dYEOBrpAYyNQfBDQxW1uLYBvGUr+L0xKm/KEyDQXKhE=; b=cJjdur8tABy/VLR+w+f77wVdYMc0GMgvMMbxiUPPZgddOmhQeRIimS4PQLmYn5+GBE EFNZFFfsXwIq6ourajjOChBOWpFk+sGTCJuvdGWSxVJmbH2QUmTnSK4LJrkc/YSZJ1tD 7wspTwCfbgMsEW94A14KZ0+MvuEEJnrHP6qYPdYW03IoKc1GyR9Vz2tyGcU0iHMa7yph 7Bx26OeapTcIrPBwV2vKAcO5bF6qmlIdaSzKbIIx9SZA1fBtSyFyR4frdXzQ2cwNmDU9 iDzEj9blbNQTuiPwQ/1EMOTGadA+s1QjHX3B/mGBhGjZTg2ZV1C3w9ppHEYr+Xu+jtqA 8Gpg== 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=dYEOBrpAYyNQfBDQxW1uLYBvGUr+L0xKm/KEyDQXKhE=; b=YqxvFQf3y/byeQUkq/ipN9roPHq3BReoaOEN0GxxdSQvc5dMjKql28BcntQd6YtcxK kVG9ud9/i0n28kUxRqwBgEaBL22HGZTc71CvxgVbNs4lFyP0c80H08i7hf9nOIly/aVc S2HTuNoREgYF+43EBu7YuvJ6AzImjHuPKPPU7F7Qbzsi5/dwlqTPxOuJwu+ps155j2+j N773sLm7216F+r6sbYN2M8DI2wYP0f6ExQSdpi8nRelG/+Ph+b5ouN6mclKvbyDlIcCu diw+NfHvZ8JA+lcVF9tQNAoFNuo30RNkppXPRYsHCXW5LO1WmDw7V892CVg9rY6vH28N NmnQ== X-Gm-Message-State: AOAM532RIgn7mJ+QQDTJ63H/vs53kmbOkRX4LoLMdh2hjVlCZydxMvZQ VLkIdWlxAfJi/OsemTux1og= X-Google-Smtp-Source: ABdhPJyBerwnEKytxdD5Zqze+Qfla90OkskFeJoZ6Cw33TCtWZSRVgn017zGl7MW8dIyy1Yh7lB0FQ== X-Received: by 2002:a37:e20f:: with SMTP id g15mr1491236qki.332.1626318760527; Wed, 14 Jul 2021 20:12:40 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:40 -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 v5 04/10] iio: afe: rescale: reduce risk of integer overflow Date: Wed, 14 Jul 2021 23:12:09 -0400 Message-Id: <20210715031215.1534938-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 774eb3044edd..4c3cfd4d5181 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -39,7 +39,8 @@ static int rescale_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); - unsigned long long tmp; + s64 tmp, tmp2; + u32 factor; int ret; switch (mask) { @@ -67,8 +68,16 @@ static int rescale_read_raw(struct iio_dev *indio_dev, } switch (ret) { case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; + tmp = (s64)*val * rescale->numerator; + tmp2 = (s64)*val2 * rescale->denominator; + if (check_mul_overflow(*val, rescale->numerator, (s32 *)&tmp) || + check_mul_overflow(*val2, rescale->denominator, (s32 *)&tmp2)) { + factor = gcd(tmp, tmp2); + do_div(tmp, factor); + do_div(tmp2, factor); + } + *val = tmp; + *val2 = tmp2; return ret; case IIO_VAL_INT: *val *= rescale->numerator; From patchwork Thu Jul 15 03:12:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378359 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 0D70DC47E4F for ; Thu, 15 Jul 2021 03:12:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EA1B2613BA for ; Thu, 15 Jul 2021 03:12:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233358AbhGODPh (ORCPT ); Wed, 14 Jul 2021 23:15:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233228AbhGODPf (ORCPT ); Wed, 14 Jul 2021 23:15:35 -0400 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52F53C061760; Wed, 14 Jul 2021 20:12:42 -0700 (PDT) Received: by mail-qt1-x833.google.com with SMTP id g8so3556201qtj.1; Wed, 14 Jul 2021 20:12:42 -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=WLWy0WK3+ERUVTzvLsLdoDiohksSvGaMH/lm8OdurSg=; b=fJiKfBdFQonXOMJ2QOZC9NrQf65f+pBqU/sjEOoUx2qPRgqTzs7TAtA6gB5p5yyi8m MVhrGXTJhaedinyYqEI6XP9wKi/+xIjvP58GNDLBSoFcf7wo2fcsBrUJgc77uVaVY4Tu V/YgvhdfWD9mOqmpptgxPVkMK0ZIPFxRpXRX/F5fpQzflvCVc/siRrjVFHJGwsiDRTZK d0Azlgpdnz9dOLwjwplRYM3Pg+8sLmCnOMLuj3/PM21l4S4rlaOFpF/ojCs+5XbsUAZz ORpjArA1kaqgYps88mr3k5k1i9vImAR5EMNeeMDcv67WJvXEpRNpHD4kg2smsRLsSUNE gc6g== 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=WLWy0WK3+ERUVTzvLsLdoDiohksSvGaMH/lm8OdurSg=; b=oJaoukPxFyOptxBWK7QnNmYyGsXGfc74V14d9oJEneux9i/xdZE73ljjICqiEddArb 9MqKCg6CpEM5TPUG2cbU86+76ckBCgYG68Q7rLAmn+FVOHEFAT6lD05ZRA7UFLEFAFcT 2/fewjBo1MjixLzr7924gt65ubxh8YBnL5f1abq6SpUeD5grr4AkkAmxC9oglKVqIFrm bvBA6X3dQ27F3X8VvFBgl2df1Ljh31kAZnnYBKDIK/YnTk4jZ8ZsdnCrv9snPgL679Jd 78p4HjSXi4ExG5vUf2Mijkmd2k351qG+pe+Fe5kJFgr/SlfgRftIYo7XfRKC0ZUEoJ6f Cxcw== X-Gm-Message-State: AOAM532dIJYnHStEbobGj+VH4Uge2DqYCqFxt4YDlYX1TzD+4lrPmnLW aHvCzuSIUbdfg8NDg5uPEUw= X-Google-Smtp-Source: ABdhPJx+5UrwYVMjlXUdPd3/akPUbcWCZSBNSi02lW/icOsZlzzssGlJ+2+tVPp/4tkmU8/u4NxlOA== X-Received: by 2002:ac8:7fd6:: with SMTP id b22mr788094qtk.235.1626318761533; Wed, 14 Jul 2021 20:12:41 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:41 -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 v5 05/10] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Wed, 14 Jul 2021 23:12:10 -0400 Message-Id: <20210715031215.1534938-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 4c3cfd4d5181..a2b220b5ba86 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -92,7 +92,22 @@ static int rescale_read_raw(struct iio_dev *indio_dev, do_div(tmp, 1000000000LL); *val = tmp; return ret; + case IIO_VAL_INT_PLUS_NANO: + tmp = ((s64)*val * 1000000000LL + *val2) * rescale->numerator; + do_div(tmp, rescale->denominator); + + *val = div_s64(tmp, 1000000000LL); + *val2 = tmp - *val * 1000000000LL; + return ret; + case IIO_VAL_INT_PLUS_MICRO: + tmp = ((s64)*val * 1000000LL + *val2) * rescale->numerator; + do_div(tmp, rescale->denominator); + + *val = div_s64(tmp, 1000000LL); + *val2 = tmp - *val * 1000000LL; + return ret; default: + dev_err(&indio_dev->dev, "unsupported type %d\n", ret); return -EOPNOTSUPP; } default: From patchwork Thu Jul 15 03:12:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378361 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 66F1BC12002 for ; Thu, 15 Jul 2021 03:12:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5174C613A9 for ; Thu, 15 Jul 2021 03:12:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233337AbhGODPh (ORCPT ); Wed, 14 Jul 2021 23:15:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233300AbhGODPg (ORCPT ); Wed, 14 Jul 2021 23:15:36 -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 69166C06175F; Wed, 14 Jul 2021 20:12:43 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id w26so3513932qto.9; Wed, 14 Jul 2021 20:12:43 -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=8XNdoHouTbgL9ckwPx58TUs8FmE0xoETBwUslKZabOY=; b=VStE3oWYpvsH48U/xml+kuTlGevL0rwqq7bhNpygxeyrTjODYNjxTlYNCmZTLhifHh tG9ty2oKrLkyxpTpsDln9HEBFrPYebP36iooBhqJhcGI8OS6urG8ff7gIdY4EPcvbDr3 EdJnS9Dr12fG9x8C03ZM9iq6L7hrj0B5GvrPYZ6PXvs3qYUd05XZnEeH172vtE/OQguO CMlA9QuEiOUqftYz07rKHBx+SDW1naMTfVxh3Sowc8rP7nK4nShcM3h5lTkBdtLd2k+S NY55jMtKPYg74cdLvq/4v5WI+DzX5tTCtpxSfnOH3rUrVfufrJsWA6rB0OPQue1rULF3 aWEw== 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=8XNdoHouTbgL9ckwPx58TUs8FmE0xoETBwUslKZabOY=; b=VRPiNM+pDzVfCup6ufsiqFDwlC/LEpbMn4xK65d91aklnZGxNqplYr/MXI2GO/GGJC zmNrOc7tgUB6O9S7NfLBEN7IpdOouODSWjEQCrN8LSkum2VkiKeDDQwP9wU1ttIHOgOP g4wq7HCWMH+0/pJOTiwHSb9r0kJ/CcH2R9tTaDLTIxQ/ygGwZf+OaWO4utQpjwvF5nfp wPVCW77njOR7YEZxILzT/GiIeF/FPalXLYZaW8iXgsmORTZD0QXskHPH3cgMYvBF/18Y m5i8mjla61UQeZIc+/KYfo5lsWnLiz4vmQOoD57tZ1LNmdwI2bnC3G3s4emAWmoC0rdZ BISg== X-Gm-Message-State: AOAM530ycBh/VKSAMUDM6bWi+gb08fuO2qM33PDc8dLnfa9MioqYpkOE lgKvB/Y4reqJmOHLYhk6mn9fgsHj5FYJlg== X-Google-Smtp-Source: ABdhPJyqyiyFs5WaONEmd+FpE1TsEqkfWFQlSj9+5F0kdICDGgtVho8Oa97ge2sgqHR7056WVj/+kg== X-Received: by 2002:ac8:7388:: with SMTP id t8mr1619255qtp.96.1626318762640; Wed, 14 Jul 2021 20:12:42 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:42 -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 v5 06/10] iio: afe: rescale: add offset support Date: Wed, 14 Jul 2021 23:12:11 -0400 Message-Id: <20210715031215.1534938-7-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index a2b220b5ba86..4d3b44884a89 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -32,6 +32,7 @@ struct rescale { bool chan_processed; s32 numerator; s32 denominator; + s32 offset; }; static int rescale_read_raw(struct iio_dev *indio_dev, @@ -39,6 +40,8 @@ static int rescale_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); + int scale, scale2; + int schan_off = 0; s64 tmp, tmp2; u32 factor; int ret; @@ -110,6 +113,71 @@ static int rescale_read_raw(struct iio_dev *indio_dev, dev_err(&indio_dev->dev, "unsupported type %d\n", ret); return -EOPNOTSUPP; } + 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); + switch (ret) { + 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 * 1000000000UL; + tmp2 = ((s64)scale * 1000000000UL) + scale2; + *val = div_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * 1000000UL; + tmp2 = ((s64)scale * 1000000UL) + scale2; + *val = div_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + default: + dev_err(&indio_dev->dev, "unsupported type %d\n", ret); + return -EOPNOTSUPP; + } default: return -EINVAL; } @@ -186,6 +254,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 @@ -350,6 +421,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) From patchwork Thu Jul 15 03:12:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378365 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 262A7C47E4E for ; Thu, 15 Jul 2021 03:12:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 08FDB61396 for ; Thu, 15 Jul 2021 03:12:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233495AbhGODPj (ORCPT ); Wed, 14 Jul 2021 23:15:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233404AbhGODPi (ORCPT ); Wed, 14 Jul 2021 23:15:38 -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 5F588C061766; Wed, 14 Jul 2021 20:12:44 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id x24so3510271qts.11; Wed, 14 Jul 2021 20:12:44 -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=wPn15m+gaHTBKq9r6+UPKjgnBs2mEAEQzXBARIvlfXI=; b=M3UAKvrqKAp3wOiTCFchrnnWv39BrKd4eiE//+bVyXcJ1xlHzpxGjpk2/3XW1DOWzV CLaUJ7SVmy7WUC5HOcMrZ22ilO/HvA9IDURGP5Kj6Zo6eY4Xq6q/sUcMTSGemzlFVvLW zpgSbLS5/RxsbYZUh+wGBYWTnwjgT/MJPRZtDy2l2MudvqBNEmLHc4sFNAFifS8qsh6A Lykh95DkHctR3WeB3HorXYEEhhdxyH6Q6gJbpXn3tC+Vr3RKD61xkQkT4dH+wabd+thz +8fJkWbkkMdbfvdM/PMZd4rIocz0aCLfmlnRkYRTHyatrQXN6V3OFYaO3MkG9RGYFUGO gHmg== 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=wPn15m+gaHTBKq9r6+UPKjgnBs2mEAEQzXBARIvlfXI=; b=Lqmd4GnF5ai5F5DfA9CBoEAVde/D0KQaD4FBy2MTFIKHEMWxUMHYVmjGSGEbovG8wo uVBLF14i5qL7OXrOZFl30bAIGCvrtQ5a3Vp/SYbxF+Ebluv//tQWBim8BPzL4eWBbCYz REGDEtuoblBHMtuwdVmjSTzxHIbsc/SxsSHjFPar4ViMc7W/0ZOkkdoHa2xjL4u/3plO nZDVjiHsbdpRMHgSy7nhrkDwP7ghU8u4IXTWFIYB9v+QEYo7tzIXwwIF5ZqVhVgQsSXE D4qNV/DJ4RAGzdoHl0RDLLkH78+c13h1YoTllaT/h1HACgCa0dzvoWUm2lj/LKJHj96/ dLpg== X-Gm-Message-State: AOAM533Q4VAQwogA5m+S5nU4S4Lwjy5WUsfBLlPacHhvugYX2v16xZP2 nnFmzSt2GH/9dfZ1n6EEE3c= X-Google-Smtp-Source: ABdhPJxgPS6mhe1oEVEwtZioipQcyrpvWhbl0O7Eqi6jPcZVBcumhlIiEzRaKzsyanSQoXvqOfdICQ== X-Received: by 2002:a05:622a:1190:: with SMTP id m16mr1620469qtk.92.1626318763635; Wed, 14 Jul 2021 20:12:43 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:43 -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 v5 07/10] iio: afe: rescale: add RTD temperature sensor support Date: Wed, 14 Jul 2021 23:12:12 -0400 Message-Id: <20210715031215.1534938-8-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 4d3b44884a89..055f6b7c9869 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -356,10 +356,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[] = { @@ -375,6 +417,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[] = { @@ -384,6 +430,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 Thu Jul 15 03:12:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378363 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 01046C47E48 for ; Thu, 15 Jul 2021 03:12:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DFF5661396 for ; Thu, 15 Jul 2021 03:12:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233343AbhGODPj (ORCPT ); Wed, 14 Jul 2021 23:15:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233416AbhGODPi (ORCPT ); Wed, 14 Jul 2021 23:15:38 -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 7D4C6C061767; Wed, 14 Jul 2021 20:12:45 -0700 (PDT) Received: by mail-qt1-x830.google.com with SMTP id x24so3510293qts.11; Wed, 14 Jul 2021 20:12:45 -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=iG9doMSkOjGbuRC6SLDooD1hmJaLMt+b8S+uRbwt3Gs=; b=pLISMormbjm2qrxlnj+b6/aOoaQsHPIRQ94TSugwqMuhT11+1SJXlbnPzv7WC/vAQA K90IBO6+hBJuy2TVxq16p9lYBpyVX7lEQrTiwYOuLpa0Vmh3TrBA6KSDnwADq+cPtSFv SIPjpiuOG13TgDAi/9CXV/sL0DC+V+FH+dVx6cfpsjK1cLxOYp/PXaMsuVoeeNlTAo4K /LMKRfc7auXtDoXyV3RGBxkW99eLBhyL/C7cWn4oLonU4ON3rENly7Vyp/DZnTJYi3p3 qBWmf//pSTETXekyln8t0ArqREmkV7T1wB2bcG6DF+gGDod+yGeDxdO+5xF9OaULL8m3 82kg== 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=iG9doMSkOjGbuRC6SLDooD1hmJaLMt+b8S+uRbwt3Gs=; b=JdfkhNbsqD2fjWDUx0aUN6+HBEvAU94L3aSB3OPWimrNH8UhZBritH4Cc3qAAi+Wzp zYw0Zb2Wgq4UH1co7KQR4gv7rlF2gWez4Ub1VIHFCOgyjlIyixjYsV1681uPAvIKOn5P sDIfGqlJvMakj7rR69ekSJkUUV4+6iO0p5yo0zuVcOZ2CjxQ3WMuOjfC6k1N/R+/1tKD g5qbq78YULIrM1sIgaqxVDKEgJJ/aidfozbjnrN2CNzwk8YApJOq+YtmGgF1aVKSNQcL SKwGdD6quy39gZnXbxIpSWuZMf8SDxfWVo0wrequ7/KyDhsU+9/PR8PKZrf+xW7qiYrE 4IyQ== X-Gm-Message-State: AOAM532t1RrmhR8iQ0UK+JkDP4qx2kEndTJhnLHOAnUKnPu24m4W7j4y 7w2euEbXrssAeT/xPpxv1xg= X-Google-Smtp-Source: ABdhPJwon8CRGlIixWaw59adLEzkn7nOwC2lXLvSvQ3CsmICZFdFnnQyfEn+W0j9K3sZX839jzfQyA== X-Received: by 2002:ac8:dc9:: with SMTP id t9mr1572776qti.293.1626318764633; Wed, 14 Jul 2021 20:12:44 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:44 -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 v5 08/10] iio: afe: rescale: add temperature transducers Date: Wed, 14 Jul 2021 23:12:13 -0400 Message-Id: <20210715031215.1534938-9-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 055f6b7c9869..4b3b5e9bbe40 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -397,11 +397,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[] = { @@ -421,6 +448,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[] = { @@ -432,6 +463,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 Thu Jul 15 03:12:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378367 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 085C0C1B08C for ; Thu, 15 Jul 2021 03:12:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EA6E2613A9 for ; Thu, 15 Jul 2021 03:12:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233487AbhGODPn (ORCPT ); Wed, 14 Jul 2021 23:15:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233529AbhGODPk (ORCPT ); Wed, 14 Jul 2021 23:15:40 -0400 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86524C06175F; Wed, 14 Jul 2021 20:12:46 -0700 (PDT) Received: by mail-qt1-x833.google.com with SMTP id k3so3527361qtq.7; Wed, 14 Jul 2021 20:12:46 -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=F9vLD4GPyVlyvHJVO/zwQSRJTRKTzhxUyD5QpwjJ88Q=; b=BcHcrgBA9jlXJxHDrhGi3xf1uNZUgp218E0NjIAHAA8H2fYiuwQpoyOcUwqGG0wYfx yhbwXAF6coyVu4Zhn948cg0Xjqn44eiAgVx41xoSxYlvUFcD1bqcbdNgSlJvK1pbRXWz vtls9BdFE5NkIF6Eg5BjYzR0g5Jc+HGzZp+9ZBTEzCbwvJsegiLZco3YdIhMvhxFzNpb xmB/rcJYr+rbsVnUlcCqSxi1bTV47DkccPkq0HoHHYVL/tucuxy2+QM39tnn1blv/LZO NI+OYdV2LwTJyNmplC5GaBmuR1MHpMZcCjORw90i5o/BzMu5FHv5MpXoWcGGH6CpGJCo P3ZQ== 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=F9vLD4GPyVlyvHJVO/zwQSRJTRKTzhxUyD5QpwjJ88Q=; b=FH1ssBLB65+KouMr0HzT6Jx1F7m2WqaNu5jl0PDFcFKzG7BcMrSHkWP+Dctjz7tb0r C3hlOVkLWAoeONtW59pw8M673rqbYPsy4J0SprKy4mszRvlbT1ckSGVXBUD2jSXerhQB uNDF2AW9TwglkP/cf67QOlfphlVIzMhfHONwV3A4iXISNS3tA52lB7xDdbwtfKiGYbkb q2/KSGZ/tuBodKgEL3jEeGe7kUu9UQCgnJlJUd2vlu77uTJvvc3ToTJORj24BzJV4vGh jmjosr7aiAXBkNGjbx38Keb4qZHK1EuKIoHb3r3rNpjT+0AWC82rb3zHNkSt1S7tmLDF 5NwQ== X-Gm-Message-State: AOAM5334Wn7l/ABA4dqhr6/G0Yzbktz6C32x38jW+QoOwK0bA0uDb+gQ FAmL9ZX+sA2Qw9WAUPPzopo= X-Google-Smtp-Source: ABdhPJxR2eb1fELMTVHNRvaUMDi0VjVTMTXZRpslXndzVJN5q+Yl8JPniWg1s92/dUuxCzkFeZxZfQ== X-Received: by 2002:ac8:5704:: with SMTP id 4mr1688880qtw.306.1626318765765; Wed, 14 Jul 2021 20:12:45 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:45 -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 v5 09/10] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Date: Wed, 14 Jul 2021 23:12:14 -0400 Message-Id: <20210715031215.1534938-10-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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..11a3d4bd3065 --- /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 Thu Jul 15 03:12:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12378369 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 28008C1B08C for ; Thu, 15 Jul 2021 03:12:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 125B0613B4 for ; Thu, 15 Jul 2021 03:12:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233467AbhGODPu (ORCPT ); Wed, 14 Jul 2021 23:15:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233574AbhGODPl (ORCPT ); Wed, 14 Jul 2021 23:15:41 -0400 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96461C061764; Wed, 14 Jul 2021 20:12:47 -0700 (PDT) Received: by mail-qv1-xf2b.google.com with SMTP id i4so2148081qvq.10; Wed, 14 Jul 2021 20:12:47 -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=aE1V8QRfi8n6sF30lMvTx95RKdAsIa59F0QLZHJ2zOc=; b=m0qFAw1m0kNwdNacHFNTF36k2HELiH8XJ81F8hZMnBvE0tR7vpIbe4pkRLhtVUuc4C QsuMbYCpXTCt20+yduDxtsb6nCKF1O5e+xu9OBgPR5WIGtzFsGKJ5td9I9T2YeD74suq F4g6zU0+1n2NdSEV4xJh+3PL0liOs3mSSkh2BCloqtCoQ0Ks9aXKYv9xf0YFoiIX5PE2 i5qkbIK1zOjet3p1e/1GbqchhsnHV/JLSGODIUu72dKpWZLjwJGTWvnJVzpnVaw9d+fm /Wmc9niXw+M5SX9ypa4rt/dQyB5RQzl92bZt/nuLFWIyt//0AROZA6sOWo/ILP792Yqt ILng== 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=aE1V8QRfi8n6sF30lMvTx95RKdAsIa59F0QLZHJ2zOc=; b=KQsCDcMEggbE9gPQjgmZ2Ds+i5H61J0jMjcXmyu5zIYcpO4WswPQjWTK8t55dc8VWs M/aimTo6aY4fwQQE5XwnbKsk8um/O3Z+2k4cvJjJiS2i+HN+W1tupyX7R+kyvVR3EZox HcqLMWmC7I7kyj89hYQ3PJu7sz0V5pBsekInhPom3cq99yfjGjMT/fQNlH/lf30hY46I e+PZ8YcvDO/EOJcOA0OHmOa+8VJz6f0b7ILM3Rwi9GDBfYj1W8/4aYTYHxTYZBYCCG7b yYzJ4A/t0g9TSXJku7c+q/pGvPG7OG5d4P1m7aKcKFXGpaEEce1UfkGKv6iG9l97JZ9g UZNg== X-Gm-Message-State: AOAM533v33B1EL0xMaHk62w3dVS3SX86CW4napim4IbqCMcTcUY8FDjl mCRgCmUFV3rtx3R7f6qPRd4= X-Google-Smtp-Source: ABdhPJzqjXQ0AVrEApOwNga/whINVfmKLmbLmX3gNSLkmbXvS9XTMyqE7+miZ/AQ7v5MOeF2A3+Lbw== X-Received: by 2002:a0c:ea31:: with SMTP id t17mr1753088qvp.17.1626318766735; Wed, 14 Jul 2021 20:12:46 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id t125sm1932847qkf.41.2021.07.14.20.12.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 20:12:46 -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 v5 10/10] dt-bindings: iio: afe: add bindings for temperature transducers Date: Wed, 14 Jul 2021 23:12:15 -0400 Message-Id: <20210715031215.1534938-11-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210715031215.1534938-1-liambeguin@gmail.com> References: <20210715031215.1534938-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 --- .../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..41411514aee0 --- /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 */ + }; +...