From patchwork Wed Dec 22 03:46:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12690951 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 A15C5C433EF for ; Wed, 22 Dec 2021 03:46:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242171AbhLVDq5 (ORCPT ); Tue, 21 Dec 2021 22:46:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242180AbhLVDq4 (ORCPT ); Tue, 21 Dec 2021 22:46:56 -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 E5825C061574; Tue, 21 Dec 2021 19:46:55 -0800 (PST) Received: by mail-qk1-x732.google.com with SMTP id t83so1088661qke.8; Tue, 21 Dec 2021 19:46:55 -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=OL7A5ukBxkoMWkSsnhIoSLHCTIoM15iUGM/H1c4aWqU=; b=jEao2wLQcyGxGuoTIL6ubPNypFcKGQlhlhG5JbD8bO30iob5ynRdrel6kpXfOWkyVg rL1Y1WVhJMXCfAhreGdSCP50NmzXOQ9hdi5w0RfLdDojGKggJYLCBW7T0n7KUVWfSD1C 3fafJcexIYADzC1qFd90wLoyTOgE3Jh8QEYJy5RW6Y/9Tye059QNBgBsze2wnbxTqZxv l66nJAHAdxQyxHtQIWMZBAXaSr8QIpqN4gStaXnp4Ma4KwtfbwEIhl9uMs+1uf2dRtFe Wa1fyUJUWsv/DH6SHt29qq9NpziwpldE1yhCME5uHJhaUO2TxGpbUo1jSU5wAWmHa7o2 eIJQ== 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=OL7A5ukBxkoMWkSsnhIoSLHCTIoM15iUGM/H1c4aWqU=; b=g9waPYB44Homdy8DjDI5nFC3U/fY9tevSsMAc15AOABxYYVH2EZeFEHdVmnG0/c8zX v4EUQLfoZWS8z9y1qeksX7k0FgObeTFbzgh+xb96WseyZtvEsre+rjJbqhCZVobro2be MOwyXnvVbF/pSzuyQLAlzwZo8UnU8vKwHwxYgjSI618C2yPaSBqkJ57EpkN9COJJqwXO PZN/2IZB2J750+i/pq7sLQRoqwpERkhH1O5yUkUaKuf0NYLNThM6ZcH75atz5TDk7auE S5k3bnsOsBNI9+5eS99Visg7Gipc6RN/7IDxHpnNMcE7ObCsFrGbJrqUoc6cmsh/ucc0 6vZA== X-Gm-Message-State: AOAM531G4plcTcyOq/6HQ4fnKSXkH4Ah9nMIfvbaURjOkFSgRb2Uwuek 4W1/wyQel2ISjcnr6rpIE2M= X-Google-Smtp-Source: ABdhPJwHOMoUEWhiTTlowbbnhQF4B5e9DBvNC+Iqt/biwtXFDBaEMvZn4PTU+edhTy91ziycWsRPhA== X-Received: by 2002:a05:620a:459f:: with SMTP id bp31mr962107qkb.108.1640144815114; Tue, 21 Dec 2021 19:46:55 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.46.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:46:54 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 01/15] iio: inkern: apply consumer scale on IIO_VAL_INT cases Date: Tue, 21 Dec 2021 22:46:32 -0500 Message-Id: <20211222034646.222189-2-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- 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 Wed Dec 22 03:46:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12690955 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 0F236C433EF for ; Wed, 22 Dec 2021 03:47:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242184AbhLVDq6 (ORCPT ); Tue, 21 Dec 2021 22:46:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242185AbhLVDq5 (ORCPT ); Tue, 21 Dec 2021 22:46:57 -0500 Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26B67C061574; Tue, 21 Dec 2021 19:46:57 -0800 (PST) Received: by mail-qv1-xf35.google.com with SMTP id q3so1175830qvc.7; Tue, 21 Dec 2021 19:46:57 -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=12xfYndy4GLO1P8S68dDnZdcfNV0/po3RgsVeOg3A6s=; b=h97Lke+QI1VFG0/C6kGFCVXWHvPV5tQAlTeFYGMKh6SobJMDZCItBZpPRG9+5T2DPr XCtHy555yney34+q7X9Bt7Ecm0wSNSIQ7THZYTEP83okE2E7VhnRczgESCEKmGgxItML nCsR9l/4rQptrmivmbyS6S/3+Rx+WRQoZT3HEW3sDmrIbiig8/c1rWE0T1fU894QekRi VbJ31vE3Zps5JpB4aGz3knnHFDeaTGgDuMEcWcd7KfnkEjyfWgQFdy9nVVlAq+H3kDxd /tRI2LlUzuwTZ5l2GPrBRPJ3aNDtGnML/amiYr2AL7ZLGsVYKGXH0ia6b/Z8VUv61dHf Knew== 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=12xfYndy4GLO1P8S68dDnZdcfNV0/po3RgsVeOg3A6s=; b=3mQIfQjcQ6c7cRBZu9wfGlszZuM9tq1XysZfDzTdSGU21T529D58F5Jmj8113El0Xb +Q2BRuxbV/jJbTcCAcZF5MtFPbH74cWH7ntIjbyVv7v8lf7CmNsXnYQ/8KRavBmFCugp LYCdtypnb2T+M2kRWMXd5NxOOQfvuHWSrp4op8p62x6KM8Th3oMpRsaDu+nHL+1NMA0X yNoWhdNcJ50LgUZXyMNzGGLjT5F0dBt22E6XEgXo3SET+mcb3kjuMRtxxYgdgwxOajSR tt8iZSJuPgZRg95M7BZ1jBJ4UWniqApQC7cXgoRuEqxaz2cB6KfZWbbpbrqGEhI3WWWy pX7A== X-Gm-Message-State: AOAM531DEImw4B/cVYRB65C47Rn+tHruqvGYB4+pn9D37tGeLwhlwlUe c1HA7aV0SSvRw72RT66TURg= X-Google-Smtp-Source: ABdhPJyZQUF72w9wgDGg1uldz1qZm8CfEHCZCv+PeBOzZTn0B3mXndc2VNvprZRLW552ULV1mzAlaQ== X-Received: by 2002:a05:6214:c42:: with SMTP id r2mr1116414qvj.70.1640144816317; Tue, 21 Dec 2021 19:46:56 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.46.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:46:55 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 02/15] iio: inkern: apply consumer scale when no channel scale is available Date: Tue, 21 Dec 2021 22:46:33 -0500 Message-Id: <20211222034646.222189-3-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- 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 Wed Dec 22 03:46:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12690957 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 80D3CC4321E for ; Wed, 22 Dec 2021 03:47:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242200AbhLVDrD (ORCPT ); Tue, 21 Dec 2021 22:47:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242193AbhLVDq6 (ORCPT ); Tue, 21 Dec 2021 22:46:58 -0500 Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85E50C061746; Tue, 21 Dec 2021 19:46:58 -0800 (PST) Received: by mail-qt1-x834.google.com with SMTP id n15so1113743qta.0; Tue, 21 Dec 2021 19:46:58 -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=6Gr+hfyXSmVuc3I7nGeWbuucj1tCjCUpPYNUNmCPI3E=; b=FlI4StCgijESKymNVPP9cPVKmDlfYQrBO6LxmYKeR/nA1DAJQzcuGa9p+qcuQD8UYT ZqQrKBGjTvfwG1zA/SvYrXWYbL/lq2AWkON65atByrodqcNpRpB0HdEs2l8UXo92Y8pw 0q+UJXrNjtWIEx0h+i8EOJlEfFlRjeQ+RndORmcp66vmatiLLlZdwCuIzzo2x9GlTCbp V4k3Ka1Wg/j1JRHkIJ5Jt0GHRB2ixsEVlwXaKzIp6emqy3AhWNgPfZj+SDg3nUjc5VNr aJ8M8GxiuKihuH6gWgsAPiKzK4EJtwYNU5aw3Y3JYDrQJZSXWeMNTAp1b2O0ier4CKOq 83hQ== 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=6Gr+hfyXSmVuc3I7nGeWbuucj1tCjCUpPYNUNmCPI3E=; b=TBOnZwMEPb17fuicE74yGTvt4cNKjegyDK7k72Q12f8t9BSNWUTFTnTmr1YAYbxzHb Zufk34NLB1QYdrS9YJcUlkBeodwU20N6Seqqo6G1RY6Sz0lYdKU2/7r5aX4WyUxg754D NJM2m/QkQrJJoFec0JuZ1/H1Yck2RlCPSZcX+wckQnFZ9Qx/eDN1nR/De9g0u5+TqoB0 3O5TPM8xtcK/VsOxtfUnv9xW9jZAm4SengzJ1I+lTZhEYH2QJImwxza4zN3NzEK7sllw ghrJd8PCxRuL/gDCddNgvcsIwAbCgPQq+tkbAqrEheMTATub7/BuxC45N8UTXqWpK2VR oI0A== X-Gm-Message-State: AOAM532c0Y5nqJCdItlElc3xSGsMDdHtWeDvyWZsFdGDTZVHYxcXy3pj 8votIFkAnwn1E4IwW+ZuJqPU4fCqDO7k8A== X-Google-Smtp-Source: ABdhPJyqw/v3WnEQcjraAy9L7d7S1oWNcLq+EZ97VHK1L3z+DM4iCdWoEqFIOAyY2/tvBhD9Mnk34Q== X-Received: by 2002:a05:622a:50a:: with SMTP id l10mr897703qtx.491.1640144817690; Tue, 21 Dec 2021 19:46:57 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.46.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:46:57 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 03/15] iio: inkern: make a best effort on offset calculation Date: Tue, 21 Dec 2021 22:46:34 -0500 Message-Id: <20211222034646.222189-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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..df74765d33dc 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 >>= offset_val2; + break; + default: + return -EINVAL; + } + + raw64 += offset_val; + } scale_type = iio_channel_read(chan, &scale_val, &scale_val2, IIO_CHAN_INFO_SCALE); From patchwork Wed Dec 22 03:46:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12690953 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 8C529C4332F for ; Wed, 22 Dec 2021 03:47:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242207AbhLVDrB (ORCPT ); Tue, 21 Dec 2021 22:47:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242197AbhLVDrA (ORCPT ); Tue, 21 Dec 2021 22:47:00 -0500 Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07FD9C061748; Tue, 21 Dec 2021 19:47:00 -0800 (PST) Received: by mail-qt1-x830.google.com with SMTP id z9so790013qtj.9; Tue, 21 Dec 2021 19:46:59 -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=hscPqaqXpcu6DXMBiBTFfYMJ6fCqMu/wY7BdxG1ViLQ=; b=hzfnCEqv7errqekAIlP7SAYOWW2zwRjaQcwXhXM6+yIAITajMYfpqDbnqrmHDG1Dy5 7+JGi+ugOqYOxya8w7WxIwqOtt3xmTmMEWlkY0fyZbRehFLBdVN9WBkGdnna6+mGUVly zbDeZjISXN16/KlWeqSVuG5r322PEwZCAtxg8m0rkN7sGFXNQ9Pw1rK2nJTnixlMFAO/ KtpApQu/hYPrB/ydCat9dx5JpxlZOUThnb02e3WOFCM4z4xZhN1Zv5Zy+1shvkhPmJM3 Y5faXUMLuwBrV9Scv7fnJpJPQropPh14nHRfvTzzuTpNsHHpJrLniQ9Y8eDqIlZA4pd2 d0eg== 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=hscPqaqXpcu6DXMBiBTFfYMJ6fCqMu/wY7BdxG1ViLQ=; b=Fc6JPgrTmvXCYsfdsK/y3zATpPBu5ZXTFrKZFiqNS023ZS4fvNwnZEV6RnXx99gQkc LfgdmVddyYGJ+Pe3FiQMbZzx0scrUe4aU1ClqDSEzcq2f7LhXmcgAIvCXHCYS/m/vBhb YJQHbr8f/upXP11hDczclA73jQfoPggFywzzafrD7i9i+WwlwLTAGp2nKycFOuVU+ouS zYiP7y/h4ZsURhE2hckF6QiJDwX7e2Spki5wveF+tatnLcCeVh8/ug7/mI+yarIKqEtB TGd5JW3IFqrLTtN3FVf/4ux6SIQRB6ruohZowGIvXqzKsHCbLvtNbu6kLSUikWvSC7oH EyPQ== X-Gm-Message-State: AOAM533zExAw30wuU5cFwDce4+kMoJWbaQcQAbxsHDnEt+4tED2aVGnt fT3sMO/n13w8eQri3dM+oQs= X-Google-Smtp-Source: ABdhPJwmn1KG+mWlZqSEw4btikmEOatPeQw/JyhnjbYr2V3A+9BxM5EURkJU+F/WyPJhublWa+eaUg== X-Received: by 2002:ac8:7f88:: with SMTP id z8mr902242qtj.365.1640144819208; Tue, 21 Dec 2021 19:46:59 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.46.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:46:58 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 04/15] iio: afe: rescale: expose scale processing function Date: Tue, 21 Dec 2021 22:46:35 -0500 Message-Id: <20211222034646.222189-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- drivers/iio/afe/iio-rescale.c | 65 ++++++++++++++------------------- include/linux/iio/afe/rescale.h | 30 +++++++++++++++ 2 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 include/linux/iio/afe/rescale.h diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 774eb3044edd..d0669fd8eac5 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -11,35 +11,46 @@ #include #include #include +#include #include #include #include #include #include -struct rescale; - -struct rescale_cfg { - enum iio_chan_type type; - int (*props)(struct device *dev, struct rescale *rescale); -}; +int rescale_process_scale(struct rescale *rescale, int scale_type, + int *val, int *val2) +{ + unsigned long long tmp; -struct rescale { - const struct rescale_cfg *cfg; - struct iio_channel *source; - struct iio_chan_spec chan; - struct iio_chan_spec_ext_info *ext_info; - bool chan_processed; - s32 numerator; - s32 denominator; -}; + switch (scale_type) { + case IIO_VAL_FRACTIONAL: + *val *= rescale->numerator; + *val2 *= rescale->denominator; + return scale_type; + case IIO_VAL_INT: + *val *= rescale->numerator; + if (rescale->denominator == 1) + return scale_type; + *val2 = rescale->denominator; + return IIO_VAL_FRACTIONAL; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = *val * 1000000000LL; + do_div(tmp, rescale->denominator); + tmp *= rescale->numerator; + do_div(tmp, 1000000000LL); + *val = tmp; + return scale_type; + default: + return -EOPNOTSUPP; + } +} static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); - unsigned long long tmp; int ret; switch (mask) { @@ -65,27 +76,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev, } else { ret = iio_read_channel_scale(rescale->source, val, val2); } - switch (ret) { - case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; - return ret; - case IIO_VAL_INT: - *val *= rescale->numerator; - if (rescale->denominator == 1) - return ret; - *val2 = rescale->denominator; - return IIO_VAL_FRACTIONAL; - case IIO_VAL_FRACTIONAL_LOG2: - tmp = *val * 1000000000LL; - do_div(tmp, rescale->denominator); - tmp *= rescale->numerator; - do_div(tmp, 1000000000LL); - *val = tmp; - return ret; - default: - return -EOPNOTSUPP; - } + return rescale_process_scale(rescale, ret, val, val2); default: return -EINVAL; } diff --git a/include/linux/iio/afe/rescale.h b/include/linux/iio/afe/rescale.h new file mode 100644 index 000000000000..14d4ee1227c6 --- /dev/null +++ b/include/linux/iio/afe/rescale.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2021 Liam Beguin + */ + +#ifndef __IIO_RESCALE_H__ +#define __IIO_RESCALE_H__ + +#include + +struct rescale; + +struct rescale_cfg { + enum iio_chan_type type; + int (*props)(struct device *dev, struct rescale *rescale); +}; + +struct rescale { + const struct rescale_cfg *cfg; + struct iio_channel *source; + struct iio_chan_spec chan; + struct iio_chan_spec_ext_info *ext_info; + bool chan_processed; + s32 numerator; + s32 denominator; +}; + +int rescale_process_scale(struct rescale *rescale, int scale_type, + int *val, int *val2); +#endif /* __IIO_RESCALE_H__ */ From patchwork Wed Dec 22 03:46:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12690965 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 60832C4321E for ; Wed, 22 Dec 2021 03:47:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242296AbhLVDrO (ORCPT ); Tue, 21 Dec 2021 22:47:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242208AbhLVDrB (ORCPT ); Tue, 21 Dec 2021 22:47:01 -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 6FEEDC06173F; Tue, 21 Dec 2021 19:47:01 -0800 (PST) Received: by mail-qt1-x831.google.com with SMTP id p19so774117qtw.12; Tue, 21 Dec 2021 19:47: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=BDkFtZR4GcVfPI2FAnt2gKNxJlGgIx5025z/PArQTSg=; b=hmhACLBVidwWeIBUvFwJTuDfbcxgEIfQ8CLsjIpb0uF8rLeVfPTFuDMLVeM2DWZjk2 Lg+iiOzUIYbM9pyjgCsghW7kSomnYChIKZQyhKYdhQlhYALaRGLNaD5YzaquzRqOLKXN WV7D+4zRyjJA1YhwemjuoIAacqpjhi3QYbuyH5/mCjLBKnA0Igczn9f1TyQNxXRiQl9h i836I5u3EacczEk+/HQn0EMcLAsIx4+oGZfNZxm2L2sD+jQKSX5J3DbJ47Ju1Fz3gHpu rv2NdLPp6VdJ8prdnGArhOMgLjXmUwnsmf84nj9UgA9afvw6dFarnQGo9i1Z/zVwMAxZ bqoA== 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=BDkFtZR4GcVfPI2FAnt2gKNxJlGgIx5025z/PArQTSg=; b=lYQTeM8Bpc5tmRZOHxvJ2ZUVR7Y8AdZjB3f5EybPCUQXoK1dr9cIVrrxF+siRdCYkP 4jYOuU2uNGeWBJL3SMh+wKutgXQme/0+OkGL2I+sEs/qgkmgRp49sdzcCPtslo6mrzWU 1SzRzXQk8pptRAWJInHcVyxr/xUUgZi/tqxwkjnlIlXtMPEgU+viKnI/g0w4lEqCXZmL xMQUFwLqbYsDidN2C1soX2adI0rpjes2j16ziaH8rvA9VirKkX6o9jxFpuW8A3Ynkcwq lqr+spBDsiyK7EcndKYb+oq9rXoFbdtFDZsGlK5/R6Bel9cCQFjeG7mA3ktvP9RJJmjX rUdQ== X-Gm-Message-State: AOAM530VtQwdycR1cOgUwt2DSyNdf5d335L4Y5gFqXTLnxEkuhZQfqGA OzV9dkYw/4bWir3tP3Kv24A= X-Google-Smtp-Source: ABdhPJzRqVp6q+2KRVioVVU+SUQ2i7jXZSiSONTaFZuIku/5yrQeFFIRowOwJt6WaIhyEBKkYGifyA== X-Received: by 2002:ac8:5a4a:: with SMTP id o10mr916786qta.91.1640144820596; Tue, 21 Dec 2021 19:47:00 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.46.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:00 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 05/15] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Tue, 21 Dec 2021 22:46:36 -0500 Message-Id: <20211222034646.222189-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- 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 Wed Dec 22 03:46:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12690959 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 57220C433EF for ; Wed, 22 Dec 2021 03:47:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242224AbhLVDrG (ORCPT ); Tue, 21 Dec 2021 22:47:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242198AbhLVDrD (ORCPT ); Tue, 21 Dec 2021 22:47:03 -0500 Received: from mail-qv1-xf2e.google.com (mail-qv1-xf2e.google.com [IPv6:2607:f8b0:4864:20::f2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8133C061401; Tue, 21 Dec 2021 19:47:02 -0800 (PST) Received: by mail-qv1-xf2e.google.com with SMTP id q4so1170130qvh.9; Tue, 21 Dec 2021 19:47:02 -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=4ttl57VYYmerlQaL0ZjPy6rHZFJKzri6btWZcm5ATyE=; b=nic9ZXu2Wrk3AVNbLu19LcF+/hVUHwSgOsi5NDruCwmY/A3DIZwFBnkV/bliZpm1RU PW9nG/wAy7poTHhZh9ahg5xOVnwDsIz/yRk6c7bE3QJygjirejrpl90q91vdoYZ3bjmk /37KNQIyXZdn+eGADNvvEY8b9yg2cxO47zlySY7sgXFNkuosehlvr66BFjqIB7Fro5wl jvw8zNfTnX4uFF0qkrppZXNd/fcxY+AkX/fAeq9TYm/0xsdjr5tmj5aUJ8zF3kjnBUb5 TMzrcxWTWa1zsqcdXJaKIAkFGcm6W9gM9aXlneOuoBWi+dfmvuThttyAjIsZvikBJLEw aDAQ== 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=4ttl57VYYmerlQaL0ZjPy6rHZFJKzri6btWZcm5ATyE=; b=RbHRlSMG4u71VMVJi4R2Y4Cf2+ONKWLsGp4BFb8njqt1akLNrVnwc0exipWOfCN0YT 4upPeYXAaUUWrU/exH9ZZBUayToMKzkrTcvPhePeZ1IfWqrzKP1/roRrSB564OxkWVQP 0HWGBK3VXADag10f9rX28Xo+9WxSU+JQXJ1on4voe5CD6pG9WSkW8zhwZbVXv6txVOV9 zCPmCbakdmAxzPEhrStTH2o6H+E9NSQibCCozXz1tmqmx/uqzsOdQXXZVqL+sS6UdXIJ Pr5F4ugamWLm4gmHnYGX/tQwxGBkrk7apHRsJWCVOGHbUeDMyY4Cxo9u8nBEfm6Jqdeu Jvbw== X-Gm-Message-State: AOAM530UGfYFzOJyXuAGBh7+WV4wfstkRUM7mbUobRCId7+QYgAXimoV +VR0xLDNS/gAHM5WCz5jS90= X-Google-Smtp-Source: ABdhPJwDvr0QW/7pr0kNwBSdPRUUBjocWst9Z9oTA0uC3I4/TFB5JUQpswnJDc4OmHvG3QxT092Z8w== X-Received: by 2002:a0c:f8cc:: with SMTP id h12mr1120934qvo.6.1640144822011; Tue, 21 Dec 2021 19:47:02 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:01 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 06/15] iio: afe: rescale: add offset support Date: Tue, 21 Dec 2021 22:46:37 -0500 Message-Id: <20211222034646.222189-7-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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..6a2d4ae80652 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 * NANO; + tmp2 = ((s64)scale * NANO) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * MICRO; + tmp2 = ((s64)scale * MICRO) + 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 Wed Dec 22 03:46:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12690967 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 357BBC433F5 for ; Wed, 22 Dec 2021 03:47:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242285AbhLVDrN (ORCPT ); Tue, 21 Dec 2021 22:47:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242225AbhLVDrE (ORCPT ); Tue, 21 Dec 2021 22:47:04 -0500 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1C57C061747; Tue, 21 Dec 2021 19:47:03 -0800 (PST) Received: by mail-qv1-xf2d.google.com with SMTP id kk22so1247146qvb.0; Tue, 21 Dec 2021 19:47: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=/IzWlRLf5sgpJcd8IRlM3A1f2sn7deh22dzHI1NbenQ=; b=hcvGWMnZF+lSbiJEzlVt4JnRteJW7J4+jclBnpPJRrIJDINVBFcgfcnw9hjOuKCtXa KwGZsZL+iLog/pCvMQNN3/+AMWb1Z8T/LCEc8KCQMNdQE1raxjHLKM1ATcXa10YvEVMC ZEz2XKSxVbmVzlm8aCYQI0jCil/D8Jnul1RfKJvOxvtxZgvRK+m+vMSoFxAPg2Em4GFG V+kVNNdAkgxOVR6gd/pFrHia39vAInljcCbhh3InjSjukMnrEb8U1bfqQiNbk3wXSLDM qTZKaY0uTeMeizycJ88k9ZcyKyVN95QxosNbi5ww3JYSLsYQNnkyWzX3VR7mx0gEYrcV bDYg== 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=/IzWlRLf5sgpJcd8IRlM3A1f2sn7deh22dzHI1NbenQ=; b=KAQWIfn0+F84QITM5LRJz3a7qo1AjJaZ5sDGozirNojXqRAvUyFNmrNGYwJPuAeB2G iwynPUeHV39r3BOSNgcJoCcorE65sDUbHD24fw6fQDzb/2wb9ESGsy2qvPjTYS98w5F1 18ugGvPB0tlXMtu1GlrUH2QszGmptISv3wsAxN/XcjKlc48xrJaTASTOBNvm86dPxugy 9Y0knurtm8W9uhDUMFxwFVa/Puv7ENbU8pcTfQcs+VsjshySQqDkRDmtNcyL/qkMSfoP sS4RVO/CJER9DZRPgGLkBuoh22k05s1k3y81a2hu0XvlNMqo/lyoOnuh1L7g9ndqh2tn NkMg== X-Gm-Message-State: AOAM530Ul8Ja7BED2ptZZd0y66xlaNfvmIQCP7cl1/mJKl70kcuPHS07 qMMM7TJ1d6XtXzA1QIDpSDM= X-Google-Smtp-Source: ABdhPJwG2klhNV7gpWn9e/NX6RvPfJbGPLEFYjzvXaWfv9ZDpCKWjKN9gUaceihEhRr32/OqfnrY6w== X-Received: by 2002:a05:6214:1c06:: with SMTP id u6mr1104768qvc.35.1640144823223; Tue, 21 Dec 2021 19:47:03 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:02 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 07/15] iio: afe: rescale: use s64 for temporary scale calculations Date: Tue, 21 Dec 2021 22:46:38 -0500 Message-Id: <20211222034646.222189-8-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- 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 6a2d4ae80652..a7297b4ba17e 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 Wed Dec 22 03:46:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12690961 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 B6588C433EF for ; Wed, 22 Dec 2021 03:47:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242265AbhLVDrJ (ORCPT ); Tue, 21 Dec 2021 22:47:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242241AbhLVDrF (ORCPT ); Tue, 21 Dec 2021 22:47:05 -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 7B184C061574; Tue, 21 Dec 2021 19:47:05 -0800 (PST) Received: by mail-qv1-xf32.google.com with SMTP id q4so1170217qvh.9; Tue, 21 Dec 2021 19:47: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=eYPd8gcfe49CwczxLhcMyngfR3LuZKuWpZ+bj0iJ0Z4=; b=JBJA6EPRPuttGWy6OcVQXcRnHtGWiQUBh3ZMBhKuMlYPB1wzDsAsOqSmo3Wa7x2SzK CJoGqvfGIpGoJES3M3EpbA7kX4WHHDBrMCgioJ3MJ1FYbn4DHF/mLGdtpN8bo2w7/lh8 HroG7TmWlHG1StO9EbkGxTJZ+ypeHungXBDZJP8y/XJaslf8CADKBcJVHcTvLvhrf+Vz uPpwIsi4fj2vgYKSm1gNSW0gdONFEIUYHXAgQmdQtMb0S8qjNQMutN//eiSDqI4SsUEM Rv5YQFIzHwP/k3OZcd7FF0OwzyzX80Lfa8Si2iriHSdjuyTbLyypxbem5508pwZ8V9ST gvig== 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=eYPd8gcfe49CwczxLhcMyngfR3LuZKuWpZ+bj0iJ0Z4=; b=cQtwl+fIWH+MafRk+jdO6gztCalNWhoF1S7pO+x0Hnd4vJqMNk4B0MPS1UexrhNY4Q fklp7chVbFVPWHYmeFCoqls8ZzlnQ5NDdettsb+bHwdwChFjlirDruTrhvQF9sEktVd2 xqOtK2XdACt7Qy/zW25Xwux0oVfSC5wHRE4j3ugUMhBNOFGHnMLvyWO4fkw7N4GPJc6x /pZ9lh+3+OhsU2ZoGNF6f8owd540jOsvJkAkyICR6NXiRS95x2ugleKG+YjNMN1Vwj4r P4PedzIsT4VIvxZ0LJeom+ApKO7osKIRdKVEtY6sev8PpE4klJ2oVHtecLOVejIhiKEH qlDg== X-Gm-Message-State: AOAM530diobwLS5GngYnvOnQ/TIH94urA9K+75cGv97HvsktHvkuUWIi u1rBolMrCrrjiiFH+2gBpAU= X-Google-Smtp-Source: ABdhPJzyD9LFui3R0kZllDI2FdpWo1O6TKl/HAeMKJ2jdiTbc4TfXPB43gBlDh710DYr96gAZOuwAQ== X-Received: by 2002:a05:6214:1c85:: with SMTP id ib5mr1119587qvb.72.1640144824747; Tue, 21 Dec 2021 19:47:04 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:04 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 08/15] iio: afe: rescale: fix accuracy for small fractional scales Date: Tue, 21 Dec 2021 22:46:39 -0500 Message-Id: <20211222034646.222189-9-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- 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 a7297b4ba17e..ca8fd69bfe46 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 Wed Dec 22 03:46: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: 12690963 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 88827C433F5 for ; Wed, 22 Dec 2021 03:47:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242273AbhLVDrM (ORCPT ); Tue, 21 Dec 2021 22:47:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242233AbhLVDrH (ORCPT ); Tue, 21 Dec 2021 22:47:07 -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 E8FD8C061747; Tue, 21 Dec 2021 19:47:06 -0800 (PST) Received: by mail-qv1-xf2c.google.com with SMTP id o10so1191128qvc.5; Tue, 21 Dec 2021 19:47: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=EgxBJt4KJLwPCnRlFoogBKl1bDW0aQ7la8JVMlwDvkQ=; b=BJjb0KftF5Av3FGcq0xlbS4fS4JVaETnGFPkCt59NEH7FEVv4CeW07jNlhhSOQ1sh4 R4cbIZUb0hoxzNX8Zrk5/xIt298yq8uugef5AfEU3cdgIFvs+938VMFljcAsHYj44tLd feJp+XUE+1fMn1E95QL2H1bE0JMJB7nXgAKgiaD1aIofWfAGoEMlip/hPlpbVa5MgGCG cO0OXgKmAjCcg1yB9aG7ndFEhFLaod4hSktRYU74Lb+A/RsM85QeZQem5Pk4stmHeU4M qvK2qdteaL4G/U67mAZTu4Cd2O4InkVUxIgQa7Xq7soUmtICUX6tJCtA+2B4orjNZpoO zc2w== 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=EgxBJt4KJLwPCnRlFoogBKl1bDW0aQ7la8JVMlwDvkQ=; b=eC43cbDJDG/9JLSZN8pByEKZ0Kjmwddo0NEe03o4wn7yGx4woIb4omRIUQuBr9nkcw QMijxCgXGwXaNrY9CXgxflFRhPP21Nd2tL0E++ZDcE0SjJYlU7NA21v3YoThGiyjfWxG s7rfuUq6odGQ+rGp4e7x1mOY8rT26KNpkKp1VCzABZgrCgiKL1JIc/9GZxDhki1er82Z yln7H6OF4dtHmMs1T+2ExavkqOTrI8sU+IhoOFSjsROWV97n8HQA4W9wLFNOZKreGYjM J1uY1p1Ft6/kmA7l5N76dp6o4iEvyAlHgwWB/heAp7SJUI1N5Syo5dXAl63pz2EUZmlQ ClAg== X-Gm-Message-State: AOAM5323wTY+xvjZdrvoizBmd4/kWimvbRCPnlmPYxTKNN6A43VtwwYG wQKXa7j+YRMV1JjYCrW597nWm6oU5DmJeg== X-Google-Smtp-Source: ABdhPJyUbWbKGjAvS2c+7+yZt3kdHDdAxfjLac3bLpyUgFaWxdb8uK11uhmHUVQC5oeW/L2pTJwC2A== X-Received: by 2002:a05:6214:5096:: with SMTP id kk22mr1076664qvb.117.1640144826109; Tue, 21 Dec 2021 19:47:06 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:05 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 09/15] iio: afe: rescale: reduce risk of integer overflow Date: Tue, 21 Dec 2021 22:46:40 -0500 Message-Id: <20211222034646.222189-10-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- 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 ca8fd69bfe46..0000a58bab9d 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 Wed Dec 22 03:46: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: 12690969 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 AAF2DC433FE for ; Wed, 22 Dec 2021 03:47:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242363AbhLVDrT (ORCPT ); Tue, 21 Dec 2021 22:47:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242231AbhLVDrI (ORCPT ); Tue, 21 Dec 2021 22:47:08 -0500 Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 662CAC061756; Tue, 21 Dec 2021 19:47:08 -0800 (PST) Received: by mail-qt1-x82b.google.com with SMTP id f9so813736qtk.4; Tue, 21 Dec 2021 19:47:08 -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=UwYzFW3ObM8HmwfflXwu0huIgB7vI5nc3iAdMmTmNmU=; b=gkGt1VmVj87XpqmfwtcUnP+dNXQGARdd4RirKrYz9F1ElEwmr4a/i/BS36b2pAA5cC TFBAf5cYiLyJ2g7fb2F7qWEzonhcjTL1i5tWeTWXocAu1PqLI3inGjDYtULGN/wgj3Xl u74hoGNUWsmklNJI24TozFaW33E2vIayODsWj1Lz7oL62iTSLPWq4Qq8BJhlUpDg9eg8 HWIcHqC+/6PIICStaYmiNd7M2kaB1OFWt498cA3iQpObNAhY4UGKC7A+awgMMygO/eaQ rLNpSsX50TgBR6FiktY3wz+L/sze8sfgnwAnNVNnBd0MHyFQV+4N9NigfKIq9lMPKgyA 7TNw== 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=UwYzFW3ObM8HmwfflXwu0huIgB7vI5nc3iAdMmTmNmU=; b=J6Za3WFGfFNuUSy9NM/DemDCNOtRoyGgRemBpxf+1Gnr9H6OIQ+tJOfjrOzNn/8jxp ug6aFxVuK+stKwFWRhQRG3nX0wivU4zVTlKMrG8OXIPq5EU/bx8TlmaReVu//xZM5x+6 ttIHTYOEk8dSlVoIZ2h4m8I7Y2uya92DF38ZN+L/5JPKBk+r3B/J9yCjJWa+VKSLUzjt ZWObth0DlXNYz0Hh7W0grLWztr4+zFeJD7L3sZTdgGeKnWKzdL9Ej7xLE6vdOCUbNTn1 Wzs8W+t2Zd7/PbZH0By9sXLtWeoid27DGuOS86KEhWsZFuL1g/pnm97IMzELFl1fLCS4 n3tg== X-Gm-Message-State: AOAM531pyx3d1KxXwy1y1u2BrNB9fQV2h7YCJ3gYx3ea0v3lfvtlLSGu O5n1r6wP1K9MocQWgtbMfpVZRsHbXq+8XQ== X-Google-Smtp-Source: ABdhPJxodpMPcKxoG5CyMw2GNazR17uc6pIl4Xl2Z0qt4LRJZKKtjvEbUio5uk2Z+qjeAktxgBKkiw== X-Received: by 2002:a05:622a:256:: with SMTP id c22mr937110qtx.284.1640144827607; Tue, 21 Dec 2021 19:47:07 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:07 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 10/15] iio: afe: rescale: make use of units.h Date: Tue, 21 Dec 2021 22:46:41 -0500 Message-Id: <20211222034646.222189-11-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin Make use of well-defined SI metric prefixes to improve code readability. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 0000a58bab9d..27f99ce67b94 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -49,11 +49,11 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, } fallthrough; case IIO_VAL_FRACTIONAL_LOG2: - tmp = (s64)*val * 1000000000LL; + tmp = (s64)*val * NANO; tmp = div_s64(tmp, rescale->denominator); tmp *= rescale->numerator; - tmp = div_s64_rem(tmp, 1000000000LL, &rem); + tmp = div_s64_rem(tmp, NANO, &rem); *val = tmp; if (!rem) @@ -69,7 +69,7 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, *val2 = rem / (int)tmp; if (rem2) - *val2 += div_s64((s64)rem2 * 1000000000LL, tmp); + *val2 += div_s64((s64)rem2 * NANO, tmp); return IIO_VAL_INT_PLUS_NANO; case IIO_VAL_INT_PLUS_NANO: @@ -330,8 +330,8 @@ static int rescale_current_sense_amplifier_props(struct device *dev, * gain_div / (gain_mult * sense), while trying to keep the * numerator/denominator from overflowing. */ - factor = gcd(sense, 1000000); - rescale->numerator = 1000000 / factor; + factor = gcd(sense, MICRO); + rescale->numerator = MICRO / factor; rescale->denominator = sense / factor; factor = gcd(rescale->numerator, gain_mult); @@ -359,8 +359,8 @@ static int rescale_current_sense_shunt_props(struct device *dev, return ret; } - factor = gcd(shunt, 1000000); - rescale->numerator = 1000000 / factor; + factor = gcd(shunt, MICRO); + rescale->numerator = MICRO / factor; rescale->denominator = shunt / factor; return 0; From patchwork Wed Dec 22 03:46: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: 12690971 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 75025C433EF for ; Wed, 22 Dec 2021 03:47:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242388AbhLVDr3 (ORCPT ); Tue, 21 Dec 2021 22:47:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242324AbhLVDrQ (ORCPT ); Tue, 21 Dec 2021 22:47:16 -0500 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC35FC061746; Tue, 21 Dec 2021 19:47:09 -0800 (PST) Received: by mail-qk1-x72b.google.com with SMTP id i130so95675qke.3; Tue, 21 Dec 2021 19:47: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=7gxNmJO9F4s3LhNovsfkYcqyzl2DfyGX4Xl7RgShIu0=; b=Pu+/8DbGSPhX+kWFwf8ruSXWQv7IwhewERvyJqWSQD+E9qrdMwt9DS6UrFlZsdXtT8 nZ3ibbQI5r3oC+g7Le0dSoCPkCf0IDL9k9xpfKYwWqw7MzneGyloB9IWzsQCSup9/I2S CJcFYFJxgy067pcUZvYSBsWHiYJnohg7e9revS32VaDSeIPrZ04+LDGQIjzU3nZ+kwgB L0Kx1DEeTbpCzWDkDHQ5lOeYYoGSXeg0oWrjvZ4fvGVeG9pZj0hLfAZiQXq2lZae0GE1 MCrCgeGlK1KVOZIwCnuI3vTGkE8SXGQqmkEmBYBvz0KSrNGoeaNQt0Go7hPNalaUFbDu apqw== 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=7gxNmJO9F4s3LhNovsfkYcqyzl2DfyGX4Xl7RgShIu0=; b=kV+RTSPtHJbonHKFwgwYty5YQo1XGF5JiEFXxk+NuukTziE9eckkkH+alyezd29+PI 0TfR396jNx3LNsEB5K32Fq6zuTttPWPcirj2UmIXa+6LfS5sGau45tfn0QU8jINa/8sU F5RLLWLTIpJ8Z3hdOO1tYYeqajPEn3y/mksT1zfYb6lMl70VbXI2wj3481R8U/s6pCEE xwbXMi+hOePbG0zDVRC11Y4vwOMIvKBnLQSC/O1hhqpytSsM3bbXtjucL4BhH3J5zEN0 s7jORxfZUG0WrHrdSqBI9EAHL04ugMLQpjUw31lm8/2aHyiXicn51IzAB+g6KcOXr7RN K+RA== X-Gm-Message-State: AOAM532M8WYFbs37QJE1tP+frbpge1HO8IDuI2U1mXmCSOwPh6j8ZmMz v134HxxugtSn1QWS6vb6U/k= X-Google-Smtp-Source: ABdhPJzIrGsdXtK6Mr87CPE9UqKbozt9nG7cgFF/+c8CBV582P+/idmxH0kJSL1Bx3vUQEq1lbvytQ== X-Received: by 2002:a05:620a:4624:: with SMTP id br36mr893056qkb.531.1640144828891; Tue, 21 Dec 2021 19:47:08 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:08 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 11/15] iio: test: add basic tests for the iio-rescale driver Date: Tue, 21 Dec 2021 22:46:42 -0500 Message-Id: <20211222034646.222189-12-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- 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 Wed Dec 22 03:46: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: 12690975 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 43E5DC433FE for ; Wed, 22 Dec 2021 03:47:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242330AbhLVDra (ORCPT ); Tue, 21 Dec 2021 22:47:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242328AbhLVDrQ (ORCPT ); Tue, 21 Dec 2021 22:47:16 -0500 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C6B9C06175C; Tue, 21 Dec 2021 19:47:11 -0800 (PST) Received: by mail-qk1-x734.google.com with SMTP id f138so1081050qke.10; Tue, 21 Dec 2021 19:47: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=pigMIgkunI5i2snmGmunXuqLDS2CrPPd48Fho6Szmdo=; b=gCkQe2rNPWyPBzT6lTaClKtwIXUaW8wACcJ8CST5tXlkGP40sb+k5lCECG361NNDPn FyhMM15XPfsLNZ9SCWRBbyvZtAA+U7DwTXxyCTgV1s3n3Gm9MMCApJ6w+rpQWvRLiP0V /ZPWPLvMheYG4FWxoVCSSZpmCipVjAZ8QNkPgoCReCGXYAYtFizbG8EE1bdn2RJcRKVJ /FgLMgqijFSSzrakeVynWxs083knAgmIeriHCDS6wjLNxjbgWEzm68AXBU7uehyYxYUu zmXb0QnP9tdjx0D0ofWMklfgLWvjzSQ1EExKmGZQ9piQ64I0viyAz6U2qX3wb2OHvyHP G4nA== 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=pigMIgkunI5i2snmGmunXuqLDS2CrPPd48Fho6Szmdo=; b=2pixuhuYTi8wjXYfbioiNQj1RIM25ZTyvFSvrNDsTTQPMS88ID6Er8/QpXpKBVUPY7 Y9u6TYdUXVe7j2cAWTOpFx+MV1BOK88eXsnUDQQFyOFMyksx0SYlS6jS4JqEbTXPGaNX QzyPYy3FtB2OLIQkTEW9gyLTUNG6UxsVjhyASIbUzML9RdQx/cfZHz2b3D+x3cWvMKPV C2HjEJE5lntVLhSbcOxX/UAmf7c9rlnTID3Zspt21KFmmJOE88RPPdIfyLjfosmXos5u rb1un1SBDJv3KNWG28YoCG3gEFS1TNMhCPnI7aBGmVpF9qyQb4orxQE0D3yVHsu0KcO1 N4+A== X-Gm-Message-State: AOAM531bH5kXwY6DTZDxWNYEP3DKtl+N7akTf7/7yj1SOQ0UtUMviGBp ERRNP9ARcac1oWKbib9WUpk= X-Google-Smtp-Source: ABdhPJz+XW2lkuDF8x6Bu712pi2trqBF9eej86h9oW7mytFiDe4jChXT2DKDOg8mJJv2xtucVR+N5A== X-Received: by 2002:a05:620a:1f8:: with SMTP id x24mr927854qkn.773.1640144830317; Tue, 21 Dec 2021 19:47:10 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:09 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 12/15] iio: afe: rescale: add RTD temperature sensor support Date: Tue, 21 Dec 2021 22:46:43 -0500 Message-Id: <20211222034646.222189-13-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 27f99ce67b94..8cf392500f18 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 / MICRO; + factor = gcd(tmp, MICRO); + rescale->numerator = MICRO / factor; + rescale->denominator = tmp / factor; + + rescale->offset = -1 * ((r0 * iexc) / MICRO * MILLI); + + 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 Wed Dec 22 03:46: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: 12690973 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 EB922C43219 for ; Wed, 22 Dec 2021 03:47:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242409AbhLVDrb (ORCPT ); Tue, 21 Dec 2021 22:47:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242329AbhLVDrQ (ORCPT ); Tue, 21 Dec 2021 22:47:16 -0500 Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AE17C06175D; Tue, 21 Dec 2021 19:47:12 -0800 (PST) Received: by mail-qt1-x82b.google.com with SMTP id z9so790297qtj.9; Tue, 21 Dec 2021 19:47: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=TH9SGoqCGqjpRTVMP6t0tW87Hqe51JYySghz/DelDTQ=; b=RxdgT4vZ5qQmwj+NgpYDZEIwyzNBxMS1WnsS8/wldFZSyZ1G2xGGrajP2G+7w37eyn A8/pXb+VJQL/9jfUTgRXyJqJCK4K6lbZCwanr9HDKn5vUJbG+RkpL9FWff+rcZ8iM4j2 l4VhEcstKvKgR3gpia8+q9/Xjr4mFcp00mY2yTXAIBpfp02vPdWDlQhtQurX0Nk5Xjfl 8ruJuShfjhuvUGPYok3Ta16IZoP2QWh1lYJ4aWeYN1mNmgbjDqjALd4zlgeSb+T50OcW kD5+wHNFNfttwVdSw27dj5N6LMZsjIGMOOZeGdwOBKobTPvp13+lcMbE4SM945t97yUB Yr/A== 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=TH9SGoqCGqjpRTVMP6t0tW87Hqe51JYySghz/DelDTQ=; b=dx8Ov/sJrDLyplojzLckLihj31aXKYsEBsASYq+s9ydhPp+486fSdPmcNw5AHJOA+o YLxkIaISMT7plq6QdUFrBN+mHgNjJHDtPyfk+UIkRKQVlPimOG7DBcYHhm888mhHaW6m /hhEKeyWoN5GflCU56Jh7akKSyTmM5/nGvNtl0hXXxky6uX9YXGW4Bg2PqP+94jRqmrL kvizNncIECvzbH7+w22LEW3Y65eMGEqyZG5YvN5Kjr+xbANAB9itUNpoYczq6T90/JV2 iR0Pkng1iFAtKuhDxkjbcDLhmDKVimvy5pqh7m4BO7mg2s5jTz336BlirBj+tH2EP3oX pPuw== X-Gm-Message-State: AOAM531SaOgpSSbCY6Mrh8HUzOLyoWH7ATLiGtuKryy7XA2pmwiaqjLn KxaOj3tmUEBBL6Iks2hdV+Q= X-Google-Smtp-Source: ABdhPJx0NMxMbjtMJ2A9Q7kKQgNOJFPhWslsyWreeFEiGFRD4Tw8XW7Q0zsoIU8w6hC2syKw+MmCbA== X-Received: by 2002:a05:622a:8d:: with SMTP id o13mr916731qtw.574.1640144831650; Tue, 21 Dec 2021 19:47:11 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:11 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 13/15] iio: afe: rescale: add temperature transducers Date: Tue, 21 Dec 2021 22:46:44 -0500 Message-Id: <20211222034646.222189-14-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 8cf392500f18..a8e5771d4e79 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 = MICRO; + 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 Wed Dec 22 03:46: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: 12690977 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 66781C433F5 for ; Wed, 22 Dec 2021 03:47:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242418AbhLVDrb (ORCPT ); Tue, 21 Dec 2021 22:47:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242332AbhLVDrQ (ORCPT ); Tue, 21 Dec 2021 22:47:16 -0500 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01532C061761; Tue, 21 Dec 2021 19:47:14 -0800 (PST) Received: by mail-qv1-xf29.google.com with SMTP id a9so1157425qvd.12; Tue, 21 Dec 2021 19:47: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=+lYGAnRZm8/Ly28B67BQjOPX/ihzee1D2awrbW2Murk=; b=QlxjUXwazuTNo6ZNdVKrHMgBO4xSYrzTp7yIREWdBGNfHZOAolWlvXNHsqM+L4Qkzl pXhpwO8VEZc6bO+jmbaiQLGS0GXWQSIMUim3bxZ7JeuXR1m0m6QvIIpxRIWakczmMjqR 3GoxqXfPyUGWR+X97X3TZp7mzO0xjm0yPVTAL5Hw5EnFwhzXwPp3rZY1b4WU8yzZ4q6b 2/KwtYCuNkZaH8NyABOhK1yUGfcSDhLAXvMpeA648iIhlDWFbHb1E+2oIo1W0VkddvQ+ e5VmQHl9QRaaBa4KtmX1J6e8FiJcwaj9r+MbXRxChVvIFoD+ilr5+gaCsgkWnAEWUfGh i3WQ== 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=+lYGAnRZm8/Ly28B67BQjOPX/ihzee1D2awrbW2Murk=; b=efePV716CErjnr9N+/K43Y10fjtzHz+Bq33DtYX/LHjxu3jGpzElOfsGs6UC+dCob6 WY++zLuXOkiaDOi/J4UITluWqd8Wqkx6DMUzWL+SOH/GEK04enWHpTcQ9ArvZOO6qygN ooYWiYA55pQKGOW9zkPT7/EGILN1UUb/LHPVu25PRJjPVv/D5kP0UYYKPCUB2duLovre rOuUoquyX5plvmMAHJ8mG3Gr/ly7hBPuxVcbbUfmGclZMRdlooYbgjFeDrLBf8iuxwdu Hbmtw9Ls35XrIBVBIgxXNcFWUsBToXXMj6Y2wSa8SDbMNkbL4kEhv96BruAGsuSYCjag E3eQ== X-Gm-Message-State: AOAM532p/CuiF9Y8QamHtrD3ITxYtSGw93Lo1txjVLF1ndYmZfoNGwop MKymyA1u0Tb2GRfC8URN9onF6W9SdhDBUA== X-Google-Smtp-Source: ABdhPJxB+sKGCjZ1ai+MRBQ4t89RhGW6HFFSstobvaxQbHJX1pRqg/VNmYqavbF7vCHmxUt8ij7LVA== X-Received: by 2002:ad4:4ee6:: with SMTP id dv6mr1125044qvb.50.1640144833157; Tue, 21 Dec 2021 19:47:13 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:12 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 14/15] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Date: Tue, 21 Dec 2021 22:46:45 -0500 Message-Id: <20211222034646.222189-15-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- .../iio/afe/temperature-sense-rtd.yaml | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml diff --git a/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml b/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml new file mode 100644 index 000000000000..336ce96371db --- /dev/null +++ b/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml @@ -0,0 +1,101 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/afe/temperature-sense-rtd.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Temperature Sense RTD + +maintainers: + - Liam Beguin + +description: | + RTDs (Resistance Temperature Detectors) are a kind of temperature sensors + used to get a linear voltage to temperature reading within a give range + (usually 0 to 100 degrees Celsius). + + When an io-channel measures the output voltage across an RTD such as a + PT1000, the interesting measurement is almost always the corresponding + temperature, not the voltage output. This binding describes such a circuit. + + The general transfer function here is (using SI units) + + V = R(T) * iexc + R(T) = r0 * (1 + alpha * T) + T = 1 / (alpha * r0 * iexc) * (V - r0 * iexc) + + The following circuit matches what's in the examples section. + + 5V0 + ----- + | + +---+----+ + | R 5k | + +---+----+ + | + V 1mA + | + +---- Vout + | + +---+----+ + | PT1000 | + +---+----+ + | + ----- + GND + +properties: + compatible: + const: temperature-sense-rtd + + io-channels: + maxItems: 1 + description: | + Channel node of a voltage io-channel. + + '#io-channel-cells': + const: 0 + + excitation-current-microamp: + description: The current fed through the RTD sensor. + + alpha-ppm-per-celsius: + description: | + alpha can also be expressed in micro-ohms per ohm Celsius. It's a linear + approximation of the resistance versus temperature relationship + between 0 and 100 degrees Celsius. + + alpha = (R_100 - R_0) / (100 * R_0) + + Where, R_100 is the resistance of the sensor at 100 degrees Celsius, and + R_0 (or r-naught-ohms) is the resistance of the sensor at 0 degrees + Celsius. + + Pure platinum has an alpha of 3925. Industry standards such as IEC60751 + and ASTM E-1137 specify an alpha of 3850. + + r-naught-ohms: + description: | + Resistance of the sensor at 0 degrees Celsius. + Common values are 100 for PT100, 500 for PT500, and 1000 for PT1000 + +additionalProperties: false +required: + - compatible + - io-channels + - excitation-current-microamp + - alpha-ppm-per-celsius + - r-naught-ohms + +examples: + - | + pt1000_1: temperature-sensor0 { + compatible = "temperature-sense-rtd"; + #io-channel-cells = <0>; + io-channels = <&temp_adc1 0>; + + excitation-current-microamp = <1000>; /* i = U/R = 5 / 5000 */ + alpha-ppm-per-celsius = <3908>; + r-naught-ohms = <1000>; + }; +... From patchwork Wed Dec 22 03:46: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: 12690979 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 BB4D4C433FE for ; Wed, 22 Dec 2021 03:47:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242342AbhLVDrl (ORCPT ); Tue, 21 Dec 2021 22:47:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242256AbhLVDrR (ORCPT ); Tue, 21 Dec 2021 22:47:17 -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 70066C06179B; Tue, 21 Dec 2021 19:47:15 -0800 (PST) Received: by mail-qt1-x82f.google.com with SMTP id q14so781759qtx.10; Tue, 21 Dec 2021 19:47: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=ARAnJI6Kh01POg+Ick5UifmHWpptElTSf8hWc/xW4rk=; b=qrKQYNM01OmYGNwiEumMzJI2RKwmWGl9S7s0/lQWjTKdH5UL5x6SLz/rp8eDMS8iA+ tv/sVFL7nyglLeL4FGH1o0fgx8FHnhWW3tJzS3lwkOGrgApZf4VqY6/uMMmuPIVLIN9+ wJfp59I0mwbH/4vxINubgHFyp6KQ/FpR30mVALeF4WNCgoEwb8LxKjzbW28/ljTiLFpz 2FNaUQHvmPxXKC26hbjaxcQhcqgxT89A18LyI3yBJJPbQAKBY0e8cgdVdJvo+VRQ4nHz fdDq6/AhL7/J43MDAldfK1tZiDhUJ3FfJ7a+FUTtmEDhksMP3g5l1hRPbgwlsiAt60ig YGEg== 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=ARAnJI6Kh01POg+Ick5UifmHWpptElTSf8hWc/xW4rk=; b=14n+ya3AoB+K0TMU8XH0hhuKI7SyGgdGMeggeo5A+3MiQYc4c7WU3dHC7Q7qg1ZMJq 9m7x3XaRDxIbUkp+UdIvAL/FNPzkOak/LGuhU1wSU4f8MRSdmiRJAxx8FHXQiiA7xPio Wn8oI0ulbwPhJFLx5UQYGQvrlmQyw+07qZ4LmpGyX0pO4F2vHTdVh52ppYzr6Wpn7xCb lfpSKp0Tobp2d1R36vC4wdV29G5Il4QHCcGOmPEpTZ0g/5EjTOgZyzuZGKdcTwbUwulJ Wg9gTrH5dYsW+BPsnEIW2hT0PTDBfRZEX2gC6axoCO/iLl3F32ulLU20xijOOyeq9SxS P1Ew== X-Gm-Message-State: AOAM531Z+NvcSC5FUytpWaYZRu8aBpCSz/jvUsCzUhhlN4n3sib7QvS+ pctI9wMS0T5ARtzjA95agAU= X-Google-Smtp-Source: ABdhPJxFsmX3sr4HFJa9Sw7ZNXfOdpEMfIQ/Jauo/9P56Y+mJ7W2h4yGPbeARlaej/Ut/JVBNiGK5Q== X-Received: by 2002:a05:622a:346:: with SMTP id r6mr928005qtw.78.1640144834592; Tue, 21 Dec 2021 19:47:14 -0800 (PST) Received: from shaak.xiphos.ca (69-165-204-82.cable.teksavvy.com. [69.165.204.82]) by smtp.gmail.com with ESMTPSA id f18sm918944qko.34.2021.12.21.19.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 19:47:14 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v11 15/15] dt-bindings: iio: afe: add bindings for temperature transducers Date: Tue, 21 Dec 2021 22:46:46 -0500 Message-Id: <20211222034646.222189-16-liambeguin@gmail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211222034646.222189-1-liambeguin@gmail.com> References: <20211222034646.222189-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 Reviewed-by: Peter Rosin --- .../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 */ + }; +...