From patchwork Sun Dec 19 22:39:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12686897 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0152DC43217 for ; Sun, 19 Dec 2021 22:40:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236843AbhLSWkD (ORCPT ); Sun, 19 Dec 2021 17:40:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236846AbhLSWkC (ORCPT ); Sun, 19 Dec 2021 17:40:02 -0500 Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDB9CC061574; Sun, 19 Dec 2021 14:40:01 -0800 (PST) Received: by mail-qk1-x732.google.com with SMTP id b67so7782085qkg.6; Sun, 19 Dec 2021 14:40:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=175owKn8svKTjKHpJ6j/PlL+nNrbx2r36iN22vV80Jc=; b=gpKMkqCcjORx7YraGeFlnVGrcsWa8TUewN5J3OAz4bLRhsblU3zgqj6qATpDaB4yi2 69WOpt1e+XL5l0a6aTayp5Dsa89J9TIRYW/ThU5Iz5hGvRaJe0hmWKUGISheAtU6NWkE mIYQMERykEB8OYL7nP0bngLPmm9JDVenJLJNaPaYGSieF3oqmbpFY3cR5U8Ynh811nq8 QNJ4Qz7oNQJ5whLFBpKSqezkb78q1izVCXALIahIEzok8TbJhE8gWwMU1MQkiUXHt3ot bStHaWNIlZujZD8H/31sm8sKKT6eXFm2QspNX6oT7NHVXoKBhp6/w+p31Ieno+nK9rx9 rvLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=175owKn8svKTjKHpJ6j/PlL+nNrbx2r36iN22vV80Jc=; b=yKGURKtNMHyVq3qAxj6do3AFZbyBts0uCKH5HyGG3V7fHvUYWwEHjNzya3DPiqPmPs 7Z/bx+J/a4aJRfqBApYZfcDb1vf8B3nSUvZlrHWDnLP6XNJFAuklJS2JlOhFQ7vSKKnX mQGrcwWshQma7tgwotWVU0JtslEc20F2hzH3uzB5bXiIM+oQnM2H0tjHNFqRMg4fEbPo zfJbhH3vdqJ1pG58aE57V+Pd5AHrQk7l1gsh6sFx57nogvtB9vFxOHdGI5X1uDfOKHGJ m9Ir2xI0IFJaz1WCn0QdzqaJEk7JzrvMbLDNCHL8O/ciZK/ABx/P81MkBln1QXujhRYx dTpw== X-Gm-Message-State: AOAM5337gUbuHYofOevOMfoE1qiIsIzgaNDyndlW1sU3oaDKNvpukX5K 6R2F5intWMvz7SMJ6z3HwzY= X-Google-Smtp-Source: ABdhPJxjeIkqFpkfmsgLB2ZZzfMZG5ck51J/rj08gDQ52chE2sN9Uzmtwx8cZ9CjN8/zn5CUnDq4gA== X-Received: by 2002:a05:620a:4495:: with SMTP id x21mr8039187qkp.604.1639953600977; Sun, 19 Dec 2021 14:40:00 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.39.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:00 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 01/14] iio: inkern: apply consumer scale on IIO_VAL_INT cases Date: Sun, 19 Dec 2021 17:39:40 -0500 Message-Id: <20211219223953.16074-2-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 0222885b334c..021e1397ffc5 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -616,7 +616,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 Dec 19 22:39:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12686899 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E888C433FE for ; Sun, 19 Dec 2021 22:40:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236846AbhLSWkF (ORCPT ); Sun, 19 Dec 2021 17:40:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236849AbhLSWkD (ORCPT ); Sun, 19 Dec 2021 17:40:03 -0500 Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31F6FC061574; Sun, 19 Dec 2021 14:40:03 -0800 (PST) Received: by mail-qk1-x732.google.com with SMTP id a11so7737917qkh.13; Sun, 19 Dec 2021 14:40:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+BykN4wh/Gl1WUeG25C/oGubDqM9wUUXvBJW6U6ZVso=; b=SiLtrLZmowMXwBfoa+gpwff3VtOC+/7+rbQrxZo1FvGab1YrAlysG8AsbHRZiTR5dz yl8FykVA6WwfTdv0W0dkAioZ+ARPdd9Tl2aLYYRyRJpr51qOPGrdgDkV947R/DFJGGgG fToefhQu5KIj34Ir5a85e8A951M2YSpH9q1gs+Lyp4ar4Fh4fbQjQCa5hz6zhm1XpNVI OeuL7mGLVzWqKWpO7TAZcykfrJyyqTurREtqibkCfcdF6TITZZ+B0AOShduIHv8NrWKl L7N5baVPsPx4Zy4CHLdJsypuygH19xJw//Oqq9MFLtyxWC1Z66LxvXRv04uhMBk9FFY0 IAMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+BykN4wh/Gl1WUeG25C/oGubDqM9wUUXvBJW6U6ZVso=; b=3RDQcSx1Ibit36+tKnWzfAMgmcFghh9zADXhng2O5w96+CajBLig2FeQsNz6LWkLIT 3JiaJCgrOQgZnc3BdDH+4dGPTWn+FEChIkS1QdR2OLEtDkn1IQxUYLGTBxoF/YvsOgvg ZLUERklei/g4GP+8HRl0BHe/t+PSz9oe+1pwgYMqoDdsiYpM/c+AJrP7c5VeU1VnDeFU vxc6QlW+sOrDjf6qtAGE99v5txa+BWE7NvvXaq9LUj5y28/z1rUHw1oz+r+Expzwzamt WylC/1vrGJIXXEe4BDvmJyMtUl5dG+hIJiMrcOCeYJdRbGZQFnGLPxRuihuXt6NSPJhp FXRg== X-Gm-Message-State: AOAM532l9CiyySRAcbA3M0y3V04BJ7n8iApqRIK/pIdxoRrKRg6DF+J0 C0tFMVGdPQAqa9kaqVn5Pwo= X-Google-Smtp-Source: ABdhPJyIKoVbgkXxgGPjjeM30V2vRP1j5uuwLIMySE63XmIbrYrbQaXw4CbwS3U42V8JpsdKSmt0Tg== X-Received: by 2002:a05:620a:1720:: with SMTP id az32mr5018676qkb.259.1639953602404; Sun, 19 Dec 2021 14:40:02 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:02 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 02/14] iio: inkern: apply consumer scale when no channel scale is available Date: Sun, 19 Dec 2021 17:39:41 -0500 Message-Id: <20211219223953.16074-3-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 021e1397ffc5..dbe13fad3cbb 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -607,10 +607,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 Dec 19 22:39:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12686901 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 440C9C433F5 for ; Sun, 19 Dec 2021 22:40:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236864AbhLSWkG (ORCPT ); Sun, 19 Dec 2021 17:40:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236857AbhLSWkE (ORCPT ); Sun, 19 Dec 2021 17:40:04 -0500 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 A2081C061574; Sun, 19 Dec 2021 14:40:04 -0800 (PST) Received: by mail-qk1-x735.google.com with SMTP id d21so7796495qkl.3; Sun, 19 Dec 2021 14:40:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=r/J/yS3v9lRITtz+ahTNaRdQPYHDQc6+wiw6txSGUmE=; b=bWAzs/SKJabcDIXij35O9Lg2G/aS95sDffBgMMhZ5vCefkyW7SOkkb/qVCekOu/SfA m/rXblPkk9An4CeGi1B7+sSvube4/sfjEPINQPaKhGKmgJeAbHu4Pmg0wyfXIa+oLmgP MBfbmnQxggFkYFScTzDMnsA5TXRkb6LyfVxPQYYO9O4E1pm5x2U8XoBMTwApuewERXof ifUfIXchz+65qgt3Qx1VIz8ZNk4ppSIagFRpPOmwKJSWGLhH6OmjTmN5gznFrISF/MiM txGfXjKBgrSXYgjf34bT+jxRC19TPg/2yvGBmV85HBEb2OJwsxJ1fx4bMwMi8imszgBJ HOFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=r/J/yS3v9lRITtz+ahTNaRdQPYHDQc6+wiw6txSGUmE=; b=B7cdodDyStz+hNFVFjm9UFYs0+A49yASVwbbe9eGHmI43qwgnnNGBye1D/38/jnv+f a/kG/NNJRgRUn8Zgs2Qxu1h0gDa2emMz0K8sjdoTISzWUGhuhCC9ULRHf035jUI4M6EC GT0BvO4WTyuvjG+ec4uyEWyMINYF4y/kWjfvZLPBfAZoAwemgVFgapp8YSkZ+gVZWUDF l4twZ6uLaT5vKmmoSA7zhLf+x96Xh98rqUP5juO38ni+udie3VPPCfiTq7k3KnWiwPFL UMVV5cjpkIDGk9pt+uRJAaqPc7ZswHbDQDed9VwB64yyKcgGCIKIrI6OEVCeLwDJm6/v XBLQ== X-Gm-Message-State: AOAM5303qIhpMAcAqf9Z3KcWxlqtdsy6ebDzWJb+zjSyntpZvV+qAUEL c2Ykikuel9DjZjEjIRl7AQY= X-Google-Smtp-Source: ABdhPJz98I6ArtQMYLDn872AHmsZ/iL4WQnaFugt8FOgSMHgpxg4cwMgO406eaFafJ/4NtF0hbzZ+w== X-Received: by 2002:a05:620a:a09:: with SMTP id i9mr7959882qka.768.1639953603850; Sun, 19 Dec 2021 14:40:03 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:03 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 03/14] iio: inkern: make a best effort on offset calculation Date: Sun, 19 Dec 2021 17:39:42 -0500 Message-Id: <20211219223953.16074-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 dbe13fad3cbb..aff6d2d3447c 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -595,13 +595,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 Dec 19 22:39:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12686903 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 822CCC43217 for ; Sun, 19 Dec 2021 22:40:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236874AbhLSWkH (ORCPT ); Sun, 19 Dec 2021 17:40:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236858AbhLSWkG (ORCPT ); Sun, 19 Dec 2021 17:40:06 -0500 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 DC0E3C061574; Sun, 19 Dec 2021 14:40:05 -0800 (PST) Received: by mail-qk1-x735.google.com with SMTP id l25so7775675qkl.5; Sun, 19 Dec 2021 14:40:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=G47PgUXxClqnLBALjPrDawXxxJ3JotIIVYOm6gyc1rw=; b=KyvzdgLLGkfq7k+L6ro/24JCb3lgAX5BInSxo8GYpZcv8WkPWHNpcG2AQlcJCAchQ9 rGUHubGP+EqroO3o5oGGE+UJ5lML/k1fA9qWWjZv73KuPs2hGTLZp/QKDljvnWEdTDl3 Xbb6v+zOmzf0pHFUUWAXNWh9BnA/p8w/k+G6OCtmGAI0IHm9GKXA6iRf12rAXM1ttVPt aq0LtJwt32vn+VBDkqjis+xgYxmsAyfEPB4auzoTzx/usZmRPTprldVttMSu3t9af3qX T9hteDteNowxOF65DjfeeuyteQ541eK3Brk/1+2kE3obQCijZTI+3QFZNtQldzS6nntw Xl/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=G47PgUXxClqnLBALjPrDawXxxJ3JotIIVYOm6gyc1rw=; b=59ai1UCccZvU3260jtf5dP4PsTd/WFrf6qT+ClJ+4PEqV0Efs4QhDNsliPzKtjKeVv YUU9yGBGX0Khlf7x+C4RlQTLtIQxsEVy9k23f/rpEkv2cbe0tl+Q088c2kz2CQ27zs9L AfkhqaA/qqzXBXdHLVd9Yl+7lsrEFZx963Oxdqs2PFFrzafL1HTAIwZFP3TgHzQ/CsCy D9w6dOPWKINd/NGplb7S7rfT+fgOlqJdyW7YeKgs6N+Ytp42jEDDNj48gn6OozERbAbb 6xwfGKeZg7wy+9HSvophb89F6u3jyL97GMbZokpkLvbzDp0ss7XxAmROseJXZp8YUlMo VQJA== X-Gm-Message-State: AOAM530/Vby11mod9YC8vFX6yD2S39Uw2zPmBcTqjjCxITOIPPcbaful Gs2KFdL7bNdBMay2ZxMqPgg= X-Google-Smtp-Source: ABdhPJyrIjZ7CoqtM2oRbwBLvyf9twPC+0fQvRe+sHWjk3thbkNnN7FjNGR0wfMr5u983bHYgvalPQ== X-Received: by 2002:a05:620a:4710:: with SMTP id bs16mr8192178qkb.551.1639953605116; Sun, 19 Dec 2021 14:40:05 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:04 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 04/14] iio: afe: rescale: expose scale processing function Date: Sun, 19 Dec 2021 17:39:43 -0500 Message-Id: <20211219223953.16074-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 Dec 19 22:39:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12686905 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91A3EC433F5 for ; Sun, 19 Dec 2021 22:40:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236891AbhLSWkJ (ORCPT ); Sun, 19 Dec 2021 17:40:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231651AbhLSWkH (ORCPT ); Sun, 19 Dec 2021 17:40:07 -0500 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 D0688C061574; Sun, 19 Dec 2021 14:40:06 -0800 (PST) Received: by mail-qk1-x72e.google.com with SMTP id 132so7760142qkj.11; Sun, 19 Dec 2021 14:40:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9e8irlmRSA2t7JqsVEUlUCAVCFnFIJe4Q4DTVAL8PV4=; b=dBz3DQ5iRgcc92fF9auJ0PJ/X7AdURt/qcz6J/BtzTxXufZ8nsyiatjSutdnYI/d6N Ol8YdmnMXjdXFiBXT7Pd2HlWOHFBtDOGnW4ajljKLStuOpSqTLseFWFMsfo8xc+WPMhb IaVdXE/2gCrCt168LgpM/5zf4EpT+O/XANIkPaq9dKRQPPik8qo5ECGiVbti7h7eBRyp iEek//AQUhT/ir4LOJmfvC8IY0D7VWBj6a9w7l8/Olpv5wdrHU7wD3UcoCajZz+VuUPn 0aPUcxvqp2cmYTbMQPWkS2JGWj+VAdc8PyGnnGCbNPZfv6uASQUGZyP8dLngeSHFdHnl hhnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9e8irlmRSA2t7JqsVEUlUCAVCFnFIJe4Q4DTVAL8PV4=; b=flRfwdez40ApRGMrx6/UkB9Aq0cCoMbjt8ScKP4SspR/qiBHHTgS2Ngz34KWoydQUp PIKp2mwrs95+O5hDatkib+J5Fm8tmQgkNK9bRx2sQ7NBaBTkxxwBOhRTOpy4HWecqNJn 8ha0HwJmUNRLa/HlxWLCk45pyH33j1SkSNoFVQ8CYh/EqYtGICjmX9uqqY5IByfGr421 cKYPILNLnaVADD2W5RP2KlMfd6LYMdyY5cfJyqGmj0FAB6zNlqfZ7d/2eGpf77dMdPZj cZ/63fxBValbUrv90trdvezndirgBQ/6HF+14R0x286YmvE3ZLxq6jkHSuitaylIr0+T HKCw== X-Gm-Message-State: AOAM532gx8cV6tk9a2w2d6hE2cfkQ1V06qovP1dwsu3DIob+HbSYG7xT ng7GlNwbGHYY5ozYfgKbhLg= X-Google-Smtp-Source: ABdhPJzs4RxzbsB2sXi0g3Kh+GVYzE8a0jnXaHikNE4EG6DMqKdbpht2TQHZP0LnfJtiSsOlXIiKow== X-Received: by 2002:a37:f60a:: with SMTP id y10mr7712828qkj.518.1639953606008; Sun, 19 Dec 2021 14:40:06 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:05 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 05/14] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Sun, 19 Dec 2021 17:39:44 -0500 Message-Id: <20211219223953.16074-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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..190a83e08008 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -17,11 +17,15 @@ #include #include #include +#include int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { unsigned long long tmp; + s32 rem; + u32 mult; + u32 neg; switch (scale_type) { case IIO_VAL_FRACTIONAL: @@ -40,6 +44,37 @@ 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: + mult = scale_type == IIO_VAL_INT_PLUS_NANO ? NANO : MICRO; + + /* + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if either *val + * OR *val2 is negative the schan scale is negative, i.e. + * *val = 1 and *val2 = -0.5 yields -1.5 not -0.5. + */ + 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 Dec 19 22:39:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12686909 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A69ACC4332F for ; Sun, 19 Dec 2021 22:40:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236903AbhLSWkK (ORCPT ); Sun, 19 Dec 2021 17:40:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236879AbhLSWkI (ORCPT ); Sun, 19 Dec 2021 17:40:08 -0500 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 C2839C061574; Sun, 19 Dec 2021 14:40:07 -0800 (PST) Received: by mail-qt1-x831.google.com with SMTP id j17so8223911qtx.2; Sun, 19 Dec 2021 14:40:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CYuLpiNgjZAk6En3eDhsfM00/87L3Gj1iZzBEk2fSKU=; b=QAukYmgmQKJnXeyTsxsyME2fEGAnHJxeFYugmxGhZ2PqFnt7koDN6j0rASqoPnKQ9S HLjddiei8ZjpOmcHl1HfNwIYNzOhXynLzr+GS5zs3hm9Xk5WXiIfkWtw/BJzhIKlEceF odgV0PLZIfaXxMlecC/bPXGhAc436FsTBgaj0HamxI5ZHEbHFyLJJHIFNsvQ/ll+8NZT CxvuLfNzNOgxz6KUa1wzV/n3IEVpidaSXbB7JMGik+1VbOk8d7E2vaEDyzysK/pgQ9PR ED1vXm+EhWEjWuE9pymYilv09A7E1h32TloB175PZE5zX2tht0lkzIQDTvsA1Ii5CgrB OPig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CYuLpiNgjZAk6En3eDhsfM00/87L3Gj1iZzBEk2fSKU=; b=d+O7doYXctIgP2YeaTD14F/JEU0HkEk2GhLgkvvytui8PwNWOLe+rbM9Bz89SRAyiX RnEfHp+Cpe4N9Q5uX/T9C1r1a32jg3D+zmJK7NLDdI2aYpVXwD1r/uFmxz2bpMbxC/6l lNdvdLXgFguqUQySPQUn3umEu6Voh5L2FPWUme1spy71kVuK8d7rrTBnAoHN4t8KSAz5 fYnjq7rPsygvUSlTqpAUAnXX+Llak7QJeCfKAKGdt0lhjZ//39pK90Hv2466uddNukUu aAz1VDDpBm1LwsOipAJZL6kzSfq9SZG01H8BBrzaolvgh4RaHhKRZZf+wdriLupfjRcr fr5g== X-Gm-Message-State: AOAM532KmRoo3A8x2f+BH80dayz30U3KaiMyHSAdS+hO56PUOHk2S5Dw 8oN/46yB4mPqDdpe1F7EwjA= X-Google-Smtp-Source: ABdhPJzzdfZcKPuvKa6PfgJpiehnqpzoZx0Gwyew7+DLeGFXSqFV95J7408tKmW9PTrjt4gyGaruXw== X-Received: by 2002:a05:622a:1004:: with SMTP id d4mr10554851qte.95.1639953606949; Sun, 19 Dec 2021 14:40:06 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:06 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 06/14] iio: afe: rescale: add offset support Date: Sun, 19 Dec 2021 17:39:45 -0500 Message-Id: <20211219223953.16074-7-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 190a83e08008..e88b63747a46 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 Dec 19 22:39:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12686913 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CFC0C4167B for ; Sun, 19 Dec 2021 22:40:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236876AbhLSWkM (ORCPT ); Sun, 19 Dec 2021 17:40:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236890AbhLSWkJ (ORCPT ); Sun, 19 Dec 2021 17:40:09 -0500 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3291CC06173F; Sun, 19 Dec 2021 14:40:09 -0800 (PST) Received: by mail-qk1-x730.google.com with SMTP id de30so7842122qkb.0; Sun, 19 Dec 2021 14:40:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0SUJD5RhKo4LXIGfIsq/LUpj7WT5zhErdlUyg5eL4QI=; b=NCnvE877ligyNhEqqS+D9zSGL8LySaHOkBzljXDiaB+19fnPYkNbjRL9AXvMvLoY+l H7ATmcg9+ymsSgMQkiFDNSuqa1A2YYCRk1eUlckTqLpz/pMQcGLAtA9iwRtAjeJ5uNYG w7oHU50Ym1bft0Ezw+m1T78GByoFlyyiftE27xztEJvwfERXboQaXqD0I/vWQyQAfm+q HLQdZt/6XqrvMltqcL4JoI/fpeqQOz87HZzFK+0waKRZEhSdXLIhPpJ0b1xyE2pFlG1A LP7z/MQ75O/pwjp5wPi0StCAonBJpRCmebT1Ihu7Jy3HoGJJiHvo8TO0TmWr6PF7Do9t KPKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0SUJD5RhKo4LXIGfIsq/LUpj7WT5zhErdlUyg5eL4QI=; b=Rwmn495Zm1VUhqzgVv+2bfzEPY8tQS59xBnZUuJmdPLRXMc+JWIm0MpiQoPLOaJUqS 8y6SMVZz7vXoFObd8H2nlOOk15y3S12Ys0byFQdUPFaoBigWaleBUu2WExfCu4T7Xkqu 2w0me69JpDdg+FEiRV1mBhej3jEXJZUHgYpmnlMZPnWJ8Eowhh4WbVpON9RnXdKvoIkE ni0Ltx8Z6VTvAYS7k747mddkAZaIqXFnsSTaG4zaWYVEn6YMB+EF+ZRcBqYLRO2lZ2RZ j0tzUqb63lWiE3Hc6zQ/M4ffkXDNrkM3FJKmDoG5FpFdkVkMWwawmAS3mQ795iyIDjG+ hqJw== X-Gm-Message-State: AOAM531+JkO7HTkwBII/FfwPgfpkj0w+TjEQ490k0GdBWR/Y7ZwKAKbM Q99KGEeqgjk+P0N7XrTHtKkEJeqV9oM= X-Google-Smtp-Source: ABdhPJxwtOG+30ANKaFnC7JjwoCkElnwkxwZ1SKPdnKPghF6VyTWvS1nNiOUpeEbiEamiVgDvrbAcQ== X-Received: by 2002:a37:a693:: with SMTP id p141mr8051888qke.269.1639953608376; Sun, 19 Dec 2021 14:40:08 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:08 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 07/14] iio: afe: rescale: use s64 for temporary scale calculations Date: Sun, 19 Dec 2021 17:39:46 -0500 Message-Id: <20211219223953.16074-8-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin All four scaling coefficients can take signed values. Make tmp a signed 64-bit integer and switch to div_s64() to preserve signs during 64-bit divisions. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index e88b63747a46..e3680998565e 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -22,7 +22,7 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { - unsigned long long tmp; + s64 tmp; s32 rem; u32 mult; u32 neg; @@ -39,10 +39,10 @@ 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 = (s64)*val * 1000000000LL; + tmp = div_s64(tmp, rescale->denominator); tmp *= rescale->numerator; - do_div(tmp, 1000000000LL); + tmp = div_s64(tmp, 1000000000LL); *val = tmp; return scale_type; case IIO_VAL_INT_PLUS_NANO: From patchwork Sun Dec 19 22:39:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12686923 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45206C43217 for ; Sun, 19 Dec 2021 22:40:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236993AbhLSWkW (ORCPT ); Sun, 19 Dec 2021 17:40:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236902AbhLSWkK (ORCPT ); Sun, 19 Dec 2021 17:40:10 -0500 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 45F63C06173E; Sun, 19 Dec 2021 14:40:10 -0800 (PST) Received: by mail-qt1-x82f.google.com with SMTP id t34so8202507qtc.7; Sun, 19 Dec 2021 14:40:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7leHdpaQg3PwahA55nSHAM68OeE/r5eeWqToNrZ4TPs=; b=Eb+NaZuiEKmlScNeOI50w2VXZ4Rying3DIBotVg1EMD04TLjdcHgu5SvC7NKi1K9k+ +NN27fwo4GvG3PWQB/smToRnqRljsWaXijDT+7zB6xGDpD82KF7X0nEPwRjYioGoQxii eUsxXTw1FFEIU5UCx7hxzF9Of6JBu4xlz3okVqciUKJ5plBptHeeotJxIJtnQc0213Ur E+R8QucKrHbGkFwpqwmZf35jbgqZ2LuqwphWBNH+nIz32ZCalwcF87U2PBwFk8MIQpzw Y/6ULIF7ZhVfVyKAYgJGpkxnio7OEuu+i2U5bJUVsqOwOoAxcpOQwmyn4mjX0ER33DUW Ph0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7leHdpaQg3PwahA55nSHAM68OeE/r5eeWqToNrZ4TPs=; b=0MY85STkBps/rp2Tpw4DVdPwO8OvbAK4SjyuH8Ani+1FNtEEuZRIT+hLRfL5T+6VCq v0EMBJ+4rOtoLUIFptEhRREIjjbRuWp5QB8w7lNl8b+KClb47yGQ4B79munjtnDCqcbD 0VAbws+9tHYWOh1WGoQNMhzZ4+dbkZZIYj9p/zBs88NFLlEqToLdwuUNB77G5oO0NXH6 h/55QOOBDYo6TsPLQm4uKRLdxIXN/gKoanXJEsZeqVSoA5Vgl3MDTmyjXKXCxqtl3MHc 4jZt9rHAMg8GUu4Gruq8vQHCTrdtKsJ4ijmvpqNJf5lrrGMUZGMbd91HxI5qEjCfuHxV j+hw== X-Gm-Message-State: AOAM5305RoGK/UsMavXf57xbwW2/NKI+9TjsKjG7pFx7vFlJ6Y9wk/So /R6kut5ANjZ9Fg1zcY3VJc8= X-Google-Smtp-Source: ABdhPJxwnuJY99XWoJOV8ybu+RhV9q4J6//X6PmdbCJ2zuIBibiMgqvK/oxuy3gA1ia7eSCyyMOTng== X-Received: by 2002:ac8:7c4b:: with SMTP id o11mr10487070qtv.358.1639953609467; Sun, 19 Dec 2021 14:40:09 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:09 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 08/14] iio: afe: rescale: fix accuracy for small fractional scales Date: Sun, 19 Dec 2021 17:39:47 -0500 Message-Id: <20211219223953.16074-9-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin The approximation caused by integer divisions can be costly on smaller scale values since the decimal part is significant compared to the integer part. Switch to an IIO_VAL_INT_PLUS_NANO scale type in such cases to maintain accuracy. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index e3680998565e..66306477aeab 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -23,7 +23,7 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { s64 tmp; - s32 rem; + s32 rem, rem2; u32 mult; u32 neg; @@ -42,9 +42,23 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, tmp = (s64)*val * 1000000000LL; tmp = div_s64(tmp, rescale->denominator); tmp *= rescale->numerator; - tmp = div_s64(tmp, 1000000000LL); + + tmp = div_s64_rem(tmp, 1000000000LL, &rem); *val = tmp; - return scale_type; + + if (!rem) + return scale_type; + + tmp = 1 << *val2; + + rem2 = *val % (int)tmp; + *val = *val / (int)tmp; + + *val2 = rem / (int)tmp; + if (rem2) + *val2 += div_s64((s64)rem2 * 1000000000LL, tmp); + + return IIO_VAL_INT_PLUS_NANO; case IIO_VAL_INT_PLUS_NANO: case IIO_VAL_INT_PLUS_MICRO: mult = scale_type == IIO_VAL_INT_PLUS_NANO ? NANO : MICRO; From patchwork Sun Dec 19 22: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: 12686907 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBBC0C43217 for ; Sun, 19 Dec 2021 22:40:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236895AbhLSWkN (ORCPT ); Sun, 19 Dec 2021 17:40:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236919AbhLSWkL (ORCPT ); Sun, 19 Dec 2021 17:40:11 -0500 Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D15EC06173F; Sun, 19 Dec 2021 14:40:11 -0800 (PST) Received: by mail-qv1-xf32.google.com with SMTP id kk22so7850382qvb.0; Sun, 19 Dec 2021 14:40:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+ORHem+cjpQo4DN5Jr/pC6A3WhQVJUKYdKPjLXZMj8I=; b=qF3wg8tziqev4/NjipGLGSXln0YyYqLQ/uDjYRIFQnKy0VDpfeJczaA+Z3Sa/3rjBh sqrRwe3vbqZBuHYoP7S6hMH5FSud1HRo4/zTrImTX61XAxwTUGU/0fuNlS6dun6aZGRi 8YEFlwpu8R+CYdH+ypeIPE+9vRRsRykwDEcBGi7b95yyhLzcXI7OKMqZtkECmO7S7jPP 6fGAsTTFsPRUNMTP2gmaYzgktSBTPIPGYJq4apM1GMB0RJxnw8nXSa7PRkLrDzUEhIEu q/bD5hDa9p9coXeO2TqT2cY//JShElAA3jORdEUoX0pQFxj51lLXClLqzeAIj2Qait12 WLFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+ORHem+cjpQo4DN5Jr/pC6A3WhQVJUKYdKPjLXZMj8I=; b=KcQkgvB3hLqDeuX1bzAQRhFfZPCq99UOQeqyvFlud++7uc3nKdh1zhHcAd4dnP5mOx yHxKPUQ9ICIfsqE0pAlOuWqqLSA8AXm8ZnGSH/PL7k0lvE+ZyZlQUbwIOtqxl/9Zlh45 3Die3i+TyxyxsUmq0W+99KnyACpFUrEko3Itqf32mkAVib3Uyt9W/BUnru/YOBxgJJTz 7leyvMj1fJNatTsKpxjEZXlFwe9swreb175cHFfTx81BaeNkY4nn8YfaNPfMuqyL0p6r u6PC254zLgpGZ4MiWeCFte7KYE0oC7vOZW6EnSqOMLtNu1j8iEJOoM6jWfgX3kJamM2f 29OQ== X-Gm-Message-State: AOAM531S3uxjkDE7M/BcUK8F/YXUeDGOaLvs+a6n7Ec5HvZffmOCTDTD 1i8dZABabwtNHcchBmd+ksU= X-Google-Smtp-Source: ABdhPJywyWBMyutWRO7OA9+3fRkvOZByUHJNFpsbrBmRrQJAxq7Sq6AChlKHq3713v9rWujHGMjLQw== X-Received: by 2002:a05:6214:4019:: with SMTP id kd25mr10656766qvb.59.1639953610651; Sun, 19 Dec 2021 14:40:10 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:10 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 09/14] iio: afe: rescale: reduce risk of integer overflow Date: Sun, 19 Dec 2021 17:39:48 -0500 Message-Id: <20211219223953.16074-10-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 on a 64-bit integer. Since the rescaling is only performed on *val, reuse the IIO_VAL_FRACTIONAL_LOG2 case. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 66306477aeab..4ab19a730a57 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -23,21 +23,31 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { s64 tmp; + int _val, _val2; s32 rem, rem2; u32 mult; u32 neg; 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: + /* + * When the product of both scales doesn't overflow, avoid + * potential accuracy loss (for in kernel consumers) by + * keeping a fractional representation. + */ + if (!check_mul_overflow(*val, rescale->numerator, &_val) && + !check_mul_overflow(*val2, rescale->denominator, &_val2)) { + *val = _val; + *val2 = _val2; + return IIO_VAL_FRACTIONAL; + } + fallthrough; case IIO_VAL_FRACTIONAL_LOG2: tmp = (s64)*val * 1000000000LL; tmp = div_s64(tmp, rescale->denominator); @@ -49,7 +59,10 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, if (!rem) return scale_type; - tmp = 1 << *val2; + if (scale_type == IIO_VAL_FRACTIONAL) + tmp = *val2; + else + tmp = 1 << *val2; rem2 = *val % (int)tmp; *val = *val / (int)tmp; From patchwork Sun Dec 19 22: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: 12686911 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A735C433EF for ; Sun, 19 Dec 2021 22:40:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236858AbhLSWkP (ORCPT ); Sun, 19 Dec 2021 17:40:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236885AbhLSWkN (ORCPT ); Sun, 19 Dec 2021 17:40:13 -0500 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 B1D58C061574; Sun, 19 Dec 2021 14:40:12 -0800 (PST) Received: by mail-qv1-xf2c.google.com with SMTP id h5so6058398qvh.8; Sun, 19 Dec 2021 14:40:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=a6b6BMbMLc+vLCOYjW1Hx5E9nRh99Jbbn43byrXwHm4=; b=cXs3YzODsfZBmr6o8MglcLyc12twDqvoNjZ0wW/X7PLKzBkRopHGIB5oiVbHyyfYc8 s0YrUGYV5Ei0i9vh33YxKZismhcAC8aaRDJD3+5G1S2zM1xEyBIYMUjNMJzWTs9g5UdP KRlQLjLlKmzbKXrJuxfemSA6c2s/10mrdWBjYubnePwaIuIDf8ZEsUwy4O426Dc1j20v bogZU+fYvYpgVeAxH1xs6dWmPKTSZnflUXxUoYVvsllDceY7UPD4kFVlM4P9kaNe4Wj9 53wOHx3I7518FKYfAFeQWMk9z12DLs3XFbJF3pw1uZ4Ia0UZ2UW9Y1ykwZA6vtrLQrRd yb3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a6b6BMbMLc+vLCOYjW1Hx5E9nRh99Jbbn43byrXwHm4=; b=7L865JMd8abqZrwmZkX0kMc1lOGGb5DvEazVZNAWTLxe+ntYAIYHEfuZYfJN/OJV6u FPJ+/V8hvNv8e++flMCRkTmN4qfFiLIGN3ZJzOEieef//wcG2vqbpLtbQ7GjhgHV+3y+ paG7phla3sxhMSGoqdrsetFgRPC5i7w9SOjP9aq4/TdyDMvA3C+vC08EmcxgqBMT3dJA GkksyKl3fPRtFxqY5id/EQeWLusEk3Ymo8p8F7nJcvJ/zXlUn9J63HiF9WGw7f4l2VQv JXBb/lFFNdfcX0/Fyb4Pw4IBfRObJ3uTCIzm7EC6p70Fze3PfuXtoAdIsO0OCVqx7Hjf 3MtQ== X-Gm-Message-State: AOAM5323opvJQ51JySJEQbx+i4z4R/Wto8cp+kJ6R4Qbet/JrbH5flgX Ypyk1ToISyaOGT4lwfl6m+MS5hEc+uo= X-Google-Smtp-Source: ABdhPJxZ5suOZ+6DnmWAlLsliiEZiMJ4D5/h0mpjyYCB57B6AXJiNBDbX3tLchfgvdm1PscAORXN+w== X-Received: by 2002:ad4:5b8c:: with SMTP id 12mr10462273qvp.83.1639953611740; Sun, 19 Dec 2021 14:40:11 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:11 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 10/14] iio: test: add basic tests for the iio-rescale driver Date: Sun, 19 Dec 2021 17:39:49 -0500 Message-Id: <20211219223953.16074-11-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin The iio-rescale driver supports various combinations of scale types and offsets. These can often result in large integer multiplications. Make sure these calculations are done right by adding a set of kunit test cases that build on top of iio-test-format. To run these tests, add the following to .kunitconfig $ cat .kunitconfig CONFIG_IIO=y CONFIG_IIO_RESCALE_KUNIT_TEST=y CONFIG_KUNIT=y Then run: $ ./tools/testing/kunit/kunit.py run --kunitconfig .kunitconfig Signed-off-by: Liam Beguin Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/iio/test/Kconfig | 10 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-rescale.c | 705 ++++++++++++++++++++++++++++ 3 files changed, 716 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 467519a2027e..f15ae0a6394f 100644 --- a/drivers/iio/test/Makefile +++ b/drivers/iio/test/Makefile @@ -4,5 +4,6 @@ # # 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 CFLAGS_iio-test-format.o += $(DISABLE_STRUCTLEAK_PLUGIN) diff --git a/drivers/iio/test/iio-test-rescale.c b/drivers/iio/test/iio-test-rescale.c new file mode 100644 index 000000000000..526f87fa3514 --- /dev/null +++ b/drivers/iio/test/iio-test-rescale.c @@ -0,0 +1,705 @@ +// 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", + }, + { + .name = "typical IIO_VAL_INT, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 42, + .expected = "-5210.918114143", + }, + { + .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", + }, + { + .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", + }, + { + .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", + }, + { + .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", + }, + { + .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", + }, + { + .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", + }, + { + .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.84789", + }, + { + .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.84789", + }, + /* + * Use cases with small scales involving divisions + */ + { + .name = "small IIO_VAL_FRACTIONAL, 261/509 scaled by 90/1373754273", + .numerator = 261, + .denominator = 509, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 90, + .schan_val2 = 1373754273, + .expected = "0.000000033594", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 90/1373754273 scaled by 261/509", + .numerator = 90, + .denominator = 1373754273, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 261, + .schan_val2 = 509, + .expected = "0.000000033594", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 760/1373754273 scaled by 427/2727", + .numerator = 760, + .denominator = 1373754273, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 427, + .schan_val2 = 2727, + .expected = "0.000000086626", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 761/1373754273 scaled by 427/2727", + .numerator = 761, + .denominator = 1373754273, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 427, + .schan_val2 = 2727, + .expected = "0.000000086740", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 5/32768 scaled by 3/10000", + .numerator = 5, + .denominator = 32768, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 3, + .schan_val2 = 10000, + .expected = "0.0000000457763671875", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 0 < scale < 1", + .numerator = 6, + .denominator = 6, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 1, + .schan_val2 = 3, + .expected = "0.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL, -1 < scale < 0", + .numerator = -6, + .denominator = 6, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 1, + .schan_val2 = 3, + .expected = "-0.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 0 < scale < 2", + .numerator = 8, + .denominator = 2, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 1, + .schan_val2 = 3, + .expected = "1.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL, -2 < scale < 0", + .numerator = -8, + .denominator = 2, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 1, + .schan_val2 = 3, + .expected = "-1.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, 760/32768 scaled by 15/22", + .numerator = 760, + .denominator = 32768, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 15, + .schan_val2 = 22, + .expected = "0.000000082946", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, 761/32768 scaled by 15/22", + .numerator = 761, + .denominator = 32768, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 15, + .schan_val2 = 22, + .expected = "0.000000083055", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, 0 < scale < 1", + .numerator = 16, + .denominator = 3, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 1, + .schan_val2 = 4, + .expected = "0.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, -1 < scale < 0", + .numerator = -16, + .denominator = 3, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 1, + .schan_val2 = 4, + .expected = "-0.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, 0 < scale < 2", + .numerator = 8, + .denominator = 3, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 1, + .schan_val2 = 1, + .expected = "1.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, -2 < scale < 0", + .numerator = -8, + .denominator = 3, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 1, + .schan_val2 = 1, + .expected = "-1.3333333333333333", + }, + { + .name = "small IIO_VAL_INT_PLUS_MICRO, positive", + .numerator = 1, + .denominator = 2, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 5, + .schan_val2 = 1234, + .expected = "2.500617", + }, + { + .name = "small IIO_VAL_INT_PLUS_MICRO, negative", + .numerator = -1, + .denominator = 2, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 5, + .schan_val2 = 1234, + .expected = "-2.500617", + }, + /* + * INT_PLUS_{MICRO,NANO} positive/negative corner cases + */ + { + .name = "negative 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", + }, + { + .name = "negative 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", + }, + { + .name = "negative 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", + }, + { + .name = "negative 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", + }, + { + .name = "negative 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", + }, + /* + * 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.01200856", + }, + { + .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.01200856", + }, + { + .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.01200856", + }, + { + .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", + }, + { + .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", + }, + { + .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", + }, + /* + * 32-bit overflow conditions + */ + { + .name = "overflow IIO_VAL_FRACTIONAL, positive", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = S32_MAX, + .schan_val2 = 1, + .expected = "214748364.7", + }, + { + .name = "overflow IIO_VAL_FRACTIONAL, negative", + .numerator = -2, + .denominator = 20, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = S32_MAX, + .schan_val2 = 1, + .expected = "-214748364.7", + }, + { + .name = "overflow IIO_VAL_FRACTIONAL_LOG2, positive", + .numerator = S32_MAX, + .denominator = 4096, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 4096, + .schan_val2 = 16, + .expected = "32767.99998474121", + }, + { + .name = "overflow IIO_VAL_FRACTIONAL_LOG2, negative", + .numerator = S32_MAX, + .denominator = 4096, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = -4096, + .schan_val2 = 16, + .expected = "-32767.99998474121", + }, + { + .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 = S32_MAX, + .expected = "1.214748364", + }, + { + .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 = S32_MAX, + .expected = "-1.214748364", + }, + { + .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 = S32_MAX, + .expected = "-1.214748364", + }, + { + .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 = S32_MAX, + .expected = "215.748364", + }, + { + .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 = S32_MAX, + .expected = "-215.748364", + }, + { + .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 = S32_MAX, + .expected = "-215.748364", + }, +}; + +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", /* 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", /* -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", /* 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", /* -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", /* 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", /* -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", /* 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", /* -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", /* 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", /* -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); + +/** + * iio_str_to_nano() - Parse a fixed-point string to get an + * IIO_VAL_INT_PLUS_NANO value + * @str: The string to parse + * @nano: The number as an integer + * + * Returns 0 on success, or a negative error code if the string cound not be + * parsed. + */ +static int iio_str_to_nano(const char *str, s64 *nano) +{ + int fract_mult = 100000000LL; + int tmp, tmp2; + int ret = 0; + + ret = iio_str_to_fixpoint(str, fract_mult, &tmp, &tmp2); + if (ret < 0) + return ret; + + if (tmp < 0) + tmp2 *= -1; + + *nano = (s64)tmp * 10 * fract_mult + tmp2; + + return ret; +} + +/** + * iio_test_relative_error_ppm() - Compute relative error (in parts-per-million) + * between two fixed-point strings + * @real_str: The real value as a string + * @exp_str: The expected value as a string + * + * Returns a negative error code if the strings cound not be parsed, or the + * relative error in parts-per-million. + */ +static int iio_test_relative_error_ppm(const char *real_str, const char *exp_str) +{ + s64 real, exp, err; + int ret; + + ret = iio_str_to_nano(real_str, &real); + if (ret < 0) + return ret; + + ret = iio_str_to_nano(exp_str, &exp); + if (ret < 0) + return ret; + + if (!exp) { + pr_err("Expected value is null, relative error is undefined\n"); + return -EINVAL; + } + + err = 1000000 * abs(exp - real); + err = div64_u64(err, abs(exp)); + return (int)err; +} + +static void iio_rescale_test_scale(struct kunit *test) +{ + struct rescale_tc_data *t = (struct rescale_tc_data *)test->param_value; + char *buff = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); + struct rescale rescale; + int values[2]; + int rel_ppm; + 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(buff, ret, 2, values); + KUNIT_EXPECT_EQ(test, (int)strlen(buff), ret); + + rel_ppm = iio_test_relative_error_ppm(buff, t->expected); + KUNIT_EXPECT_GE_MSG(test, rel_ppm, 0, "failed to compute ppm\n"); + + KUNIT_EXPECT_EQ_MSG(test, rel_ppm, 0, + "\t real=%s" + "\texpected=%s\n", + buff, t->expected); +} + +static void iio_rescale_test_offset(struct kunit *test) +{ + struct rescale_tc_data *t = (struct rescale_tc_data *)test->param_value; + char *buff_off = 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(buff_off, ret, 2, values); + KUNIT_EXPECT_EQ(test, (int)strlen(buff_off), ret); + + KUNIT_EXPECT_STREQ(test, strim(buff_off), 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 Dec 19 22: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: 12686915 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 306B2C43217 for ; Sun, 19 Dec 2021 22:40:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236897AbhLSWkQ (ORCPT ); Sun, 19 Dec 2021 17:40:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236925AbhLSWkN (ORCPT ); Sun, 19 Dec 2021 17:40:13 -0500 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 83EE8C061574; Sun, 19 Dec 2021 14:40:13 -0800 (PST) Received: by mail-qt1-x831.google.com with SMTP id n15so8270911qta.0; Sun, 19 Dec 2021 14:40:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Y2vJRPt5O3209sbPrv3mGFkOdn7JojNelTUXnuF8fWk=; b=ekk8GcZQzoerTVy8HXZubcJjMReC8RCOQ7LdIo6ZAcPK6dGWOdJb2UjLQWjfiZVR4D ogxV+P9uBYpvMqJAvfVeLNu0HCyQSzVv3A83glSH7tpcGaOGjKIS5cMl4sUKKCpkwP+c Ghric0rBDTZlyRHpmDR41az0mtkPpUUPHzFIn2/62saEbOE+19fx0BzZiXtAA4dsBM8o gv0tmLot9dM90AmSYO61dWsUr9hZf4mxrlTxNDrGkxC176gzAKXvfGCIKQzgmyUN2C7T suY8z/ETAt7Bm0XgqiH+ZUS6zeUJgaSjUtNpdRrQvOvp4R5BSbMbAqp1Wm8Rms8Q2qIE dVbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Y2vJRPt5O3209sbPrv3mGFkOdn7JojNelTUXnuF8fWk=; b=sPMazTuaW+aUHNZht/XUnrm+ZvxFo10+VBqoHgNSw8rUX3ugJTGCQ/63pOGsiobGFE gsFJ01ft7OQcvkFzT+5cWzw1voIU1gr4JdTv+T9oOai0iH3B53nvr5OBrTXU5mzP5NOQ UUHCPVgDV6cbuhOG6MjjApVgDdbu1MVJSZtbP7bl4yqv77HEJ/mcP6gEapXZiMp1f61L YL2Yf/TO4J9ZtMzbUITyWpEwYbHkV4uxccYX5DvQJGWVfFPeSiBkvkryxep4wsIB6Jkp j0Ty+cCx4GOS2Ep62j+645x+HnTJufBC6VjaMw/BPEnNm7sxYn8oYS1gVvE1u42wJ4Df VP8Q== X-Gm-Message-State: AOAM530MsiTVZM6aVh3dEtwkmWjRuoo0PWB59xGvxLSZqM947BsTXcY+ 8uj65FGkx5hAiEWpF8WKJms= X-Google-Smtp-Source: ABdhPJyGKAPf54DiPC7KLM2ycmOw23nJhvrwAcI4TrSm03tsQChBSk4sYKb8O1JdGj8EZSCSdHbwfA== X-Received: by 2002:ac8:5a13:: with SMTP id n19mr2851442qta.259.1639953612753; Sun, 19 Dec 2021 14:40:12 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:12 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 11/14] iio: afe: rescale: add RTD temperature sensor support Date: Sun, 19 Dec 2021 17:39:50 -0500 Message-Id: <20211219223953.16074-12-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 4ab19a730a57..cde39432196a 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -393,10 +393,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[] = { @@ -412,6 +454,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[] = { @@ -421,6 +467,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 Dec 19 22: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: 12686919 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EF77C4167D for ; Sun, 19 Dec 2021 22:40:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236879AbhLSWkT (ORCPT ); Sun, 19 Dec 2021 17:40:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236938AbhLSWkO (ORCPT ); Sun, 19 Dec 2021 17:40:14 -0500 Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8129BC061574; Sun, 19 Dec 2021 14:40:14 -0800 (PST) Received: by mail-qk1-x729.google.com with SMTP id t6so7809497qkg.1; Sun, 19 Dec 2021 14:40:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oiSB69/lnlt0JvHfLERHkoqhXycA88i7DNNoftdnvwg=; b=Z7mP2oLeF8ek5f+BbDG/IrsKnYNJ/hp28DFi9aZhJfUJYZx4Dk72L6MYoo1cNizDo0 gTMAmRi9Uwf1WPQh6g9CkMxFp4gri9YdmNAHeNnhmPQn7fA2zUhI3XjGSYWt6m/nNswk RkIzUNRQK3AP4t6OV6OIgBKXgR7OZEtE4K7slUNIX3JALBLjx9wXpqqedxjCRiE4Q7v6 jzTIqYVNgZ0joVY108yNYi8eGZItHKUGLq20k0K4Oie/toj9W8tyy/JPvs/4wApZCzqO gp5QacM5Ar1mJrlrJHmHmhQQTJqi5hZ9mld2LyQdTEbzgB3gX7U7QhAjXohkmOCnI60+ XMcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oiSB69/lnlt0JvHfLERHkoqhXycA88i7DNNoftdnvwg=; b=jnimHfBFSc6MK9LiieNZXLDW8GwHx0hxgPerQtE2y35L098jWIl2A20vTrylMKATQn RSTVqwzPd8ValX9gmr21B5h6XfUGvCIFf37UYXMZgsGj9kX/tEXo/BYxIoQoYioiOwQu /+nIWb2nTmQDpiWssVPCdAD8q8qQMoBCAUuyD7ih1cnvWHKKQj4J+qhIHFb4pzPME4WD KuQt2898jZiEeZtrLucushA67yu6qojuqqqHTyaincZwtGzOvjlnsYGFCu5F+jyeByiB HkzgJZo/a69mSiO5NKzVwXk14yiUPzMBVSnVCsjXFnyoB/MsHmLW9kqVxgcE8trOVnKt zYJQ== X-Gm-Message-State: AOAM532/t+gEFtJTovKqNomci/ESnwrFWJc5mJ+TqTnGmy7BsKALVbXn 8cbmicQgg6z2LyKFLNr9iWA= X-Google-Smtp-Source: ABdhPJyvH3zFDbg8t+lRnP+/ft/4rzrL61dFUPAL9N8HATrPHyr+dqhADbDVCoBMdE1jSRTX5vxSzg== X-Received: by 2002:a05:620a:1226:: with SMTP id v6mr7975613qkj.240.1639953613730; Sun, 19 Dec 2021 14:40:13 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:13 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 12/14] iio: afe: rescale: add temperature transducers Date: Sun, 19 Dec 2021 17:39:51 -0500 Message-Id: <20211219223953.16074-13-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index cde39432196a..486eb80f89d7 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -434,11 +434,37 @@ 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; + 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; + + rescale->offset = div_s64((s64)offset * rescale->denominator, + rescale->numerator); + + 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[] = { @@ -458,6 +484,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[] = { @@ -469,6 +499,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 Dec 19 22: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: 12686917 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00CE1C4167E for ; Sun, 19 Dec 2021 22:40:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236975AbhLSWkU (ORCPT ); Sun, 19 Dec 2021 17:40:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236886AbhLSWkP (ORCPT ); Sun, 19 Dec 2021 17:40:15 -0500 Received: from mail-qv1-xf34.google.com (mail-qv1-xf34.google.com [IPv6:2607:f8b0:4864:20::f34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EDEAC06173F; Sun, 19 Dec 2021 14:40:15 -0800 (PST) Received: by mail-qv1-xf34.google.com with SMTP id kc16so7820621qvb.3; Sun, 19 Dec 2021 14:40:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2qe70Unlntn4QgtFhfss5TgMBkLN99m0wOrqhV7w1NQ=; b=Jba5AZdWIVeUJc8KDXFcQkYsSxehZ7+bl+T66ReLMWlPymThZKZ2t/VTcpaU/yn7VS ex5Us3bQI23fUuBPXGwsp/c1shXoyat75kN/XoV4ZEN2+HXapGzqo9KQCp+tM+wh59d2 yAPQLhnyUEamLYlZ7gePTnhcT77OtaXOH8EeDUT5BSxcL5qjhNgdnoRb2ULmOaBc/ubY ebKGs3UtO7NQITOdxFvS8B8lyJ5cmBDSgtl6CEH3/aytJX+gPDNKWPUapBA+bRLM1pq1 983FNXCt4Peak3s8DV7dn+ddQaXce1yFHcx/fCOZVlt3udw6+a4fI1g1KEvEzERnmoG7 iv8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2qe70Unlntn4QgtFhfss5TgMBkLN99m0wOrqhV7w1NQ=; b=3NZ2m5J1Cr78iYAU6XwpYXel4hatpA4WLKLsDYAVwhnSb6Hx+0nv95HARiABafosGX KHYMjmbHsYmU7rp3nXZH6GKnMSTYjkRb2O9mxm4+jtl+sRXvtwO8H3t5m0iGp4pWusKz o8nbHU6MeIwi+5xzuhBAPR/m4kUOJdat+d6aBUd2dj7dSnBxhlt7N6fOGe1l/2gNprFQ zkVhE2Hsld8i3Dn369Gww+a686oyyZlUE5NckTV2ulPcWKP/jUAvWS4V/2P0SnDZuHtu MD5MsjzviOY5BFohStAX5LG8YZ75WSXdm9u01pNezua/RLaYFwsHhXvpJwbEh9M2YYz6 paNg== X-Gm-Message-State: AOAM530cw6mO70Ez9qdHKvtQJJJgj5zIbCZfN6dGyss1O5cSvHfENYA1 Cjx09U4GfhFtjXIn1bBDpW8= X-Google-Smtp-Source: ABdhPJzOJ68az9Fq8UaYANaGVQC6fRmgBJQo7THMeCyEMN1HlgOvD0UtS4jK8S6GdIddf2IRFa3NGw== X-Received: by 2002:a05:6214:ca1:: with SMTP id s1mr9171346qvs.57.1639953614677; Sun, 19 Dec 2021 14:40:14 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:14 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 13/14] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Date: Sun, 19 Dec 2021 17:39:52 -0500 Message-Id: <20211219223953.16074-14-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 Dec 19 22: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: 12686921 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65FF0C41535 for ; Sun, 19 Dec 2021 22:40:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236983AbhLSWkV (ORCPT ); Sun, 19 Dec 2021 17:40:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236951AbhLSWkS (ORCPT ); Sun, 19 Dec 2021 17:40:18 -0500 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 4152EC061748; Sun, 19 Dec 2021 14:40:17 -0800 (PST) Received: by mail-qk1-x72f.google.com with SMTP id d2so7736158qki.12; Sun, 19 Dec 2021 14:40:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Khru1gNSAZVb2dMG+TO7z4gJTe9dnuvVrFQcYhc9czQ=; b=WsEqPUjGfo//u6VT36ZrYmJPvGEm4hx9/IFexQrithneVoUGf8VxWCAx/XIo1r0SPu 4fi4wp08yrz44eSCzhgW28U0IWMSVrShEO2ncIqIoC13zmkZ5LFRy/HSsXEhgiT8xUMT i3OkdnkCODvpNsE85EjQtFn0+g6EynTPO52hQvzA6Uh+3oIOvwv/2OYIALCMH5qZAgkI qUQBncALtgk8XOwIcC9UVHHDiPkYRoxARxSfbVM1ukNPPkZkyvTVGHf/fMQuvTMTDS2c ON2E3Z8pFdz7ePbnK21+bnQT5AFDJeEQwJKd5AQW7w0ZWTIoC8X1UD+Ub1Peur9+shQt G63w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Khru1gNSAZVb2dMG+TO7z4gJTe9dnuvVrFQcYhc9czQ=; b=j18q+8GuNFHQim81q1O96qy3RyD/KEjo+pDmwFnzwPtJzQLg6UJtPJU0Jym8TobarI xg7Fvo6dTNZurUfb3jq4lZZ2N4gPBacXLQQ5bVFxNZ/F/YFuXvzFGgv9mHPSmqS2b7Lg hZ8+PQW7Y7bNHoobbh4V9h/mAJOZ7d3ormbocsoCXmO8x1MrlOWBLR/weLa7F2CLrVX8 4y5v2qc8q83lIa6KTTnRrQJsljDjNFJQI19g7KWcOcpzra3pxw01hUYB4azyVhSnyJw9 r1jtQW8DBIAvIcQMkOoymRBuVYhhxHfuQ2lI2FTyazUdgh7HxtsmxpC1LME4lNArA6I3 QU1Q== X-Gm-Message-State: AOAM532zb1bhnYD4uqjbJZhRqdbP5Wfp77ApIpFiTP8zhc8Wb4zGYA26 RKE6TXT2vjf9hQQUDraLSc5I8YkFaZM= X-Google-Smtp-Source: ABdhPJwQgfjCoKy9BYKWYqV/ba6iVihTNFU+djy2tpXeyPWZcw/o9OQXlNQd6/3/jTvMlxi7NqGRRQ== X-Received: by 2002:a05:620a:22d7:: with SMTP id o23mr7996385qki.222.1639953615632; Sun, 19 Dec 2021 14:40:15 -0800 (PST) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id q7sm4591880qkl.72.2021.12.19.14.40.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 14:40:15 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v10 14/14] dt-bindings: iio: afe: add bindings for temperature transducers Date: Sun, 19 Dec 2021 17:39:53 -0500 Message-Id: <20211219223953.16074-15-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211219223953.16074-1-liambeguin@gmail.com> References: <20211219223953.16074-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 */ + }; +...