From patchwork Mon Nov 15 03:43:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618635 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3A04C4332F for ; Mon, 15 Nov 2021 03:43:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 93CFC60551 for ; Mon, 15 Nov 2021 03:43:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236991AbhKODqt (ORCPT ); Sun, 14 Nov 2021 22:46:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236975AbhKODqi (ORCPT ); Sun, 14 Nov 2021 22:46:38 -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 4EB1FC061767; Sun, 14 Nov 2021 19:43:43 -0800 (PST) Received: by mail-qt1-x830.google.com with SMTP id f20so14081216qtb.4; Sun, 14 Nov 2021 19:43:43 -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=Petjn8wOVRgbGhHpVIXk1V8uScdJMFucnSi/yCBhRaU=; b=nZL2M21XBEkgJywhAn6qtQMz/KyxzuoC/ryZVNj5xJJq96qOGJ6aZmpduntslF+/OW S3DZnV+TyOYZEdRxRgnBrizJI4N4BB516u4XEsBCamSkX3SdohNn7oaWu8FhjAHGTx0R Zy2NAmTYqTgY4K+PPQCCDYMyFuaaket4UThSSkx7okMp2h+bAqBBmTcDphjQ2IMp8raa H2TlhaTg3QfbS9jZsbpBkPYDoKI+rWG6XiRY1O3yXrR8fMUzN/38BcH8WBmSTkkLf9yu yH8pOeZatv9ZL3VM2IqOsTM+Soem1QTZrTqtV4nM74nKTZ0Qi7IgMqjZfak9NSV2BkHR whew== 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=Petjn8wOVRgbGhHpVIXk1V8uScdJMFucnSi/yCBhRaU=; b=GqGGiGFc7CtaGDWWXGWgMpJ8iK6y0/0lb95hZIQMe4PKcrgCwPce47lYmp3mYZf7Vr RvlPAWjMeUNYi9iLYMqVIIRG6T9yh8sRTg4s9vcLHrhqyM//7uTrPBSVYFpAtMCind3C +JHkxRQ76LnPviFFM4pSW9Pd5J6Fsx+XTdoemRVxoTzMCOybYobXZoHZVY5qaIwcXNtw AngefXzjZ2CjGB8B/n12TSDosXBsKRzImsDsK1WqqF3kA0xmLdVmscI1jEPKLDTih/6t ekVCZdVV4Ls9KTo+IjmOLjZhRLsfFWEiPIqCtaNW5XycZu875MyALxZKrVWdddI418KZ 9IxA== X-Gm-Message-State: AOAM531f9NN9H7Amb2MHPb+6OKB6nscY5hXQ6OKUhSuyL19Diuw5dz5P CLvvi2WD8O6iPueTW7H7QtU= X-Google-Smtp-Source: ABdhPJzqAdDOiO6S2FiCYtpNmkh1L85teqKeCkxqksjwNVsppYpYQYinWIuZ90tifePoaGvT2vNydQ== X-Received: by 2002:a05:622a:170d:: with SMTP id h13mr32524468qtk.99.1636947822458; Sun, 14 Nov 2021 19:43:42 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:41 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 01/14] iio: inkern: apply consumer scale on IIO_VAL_INT cases Date: Sun, 14 Nov 2021 22:43:21 -0500 Message-Id: <20211115034334.1713050-2-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin When a consumer calls iio_read_channel_processed() and the channel has an integer scale, the scale channel scale is applied and the processed value is returned as expected. On the other hand, if the consumer calls iio_convert_raw_to_processed() the scaling factor requested by the consumer is not applied. This for example causes the consumer to process mV when expecting uV. Make sure to always apply the scaling factor requested by the consumer. Fixes: 48e44ce0f881 ("iio:inkern: Add function to read the processed value") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 0222885b334c..021e1397ffc5 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -616,7 +616,7 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, switch (scale_type) { case IIO_VAL_INT: - *processed = raw64 * scale_val; + *processed = raw64 * scale_val * scale; break; case IIO_VAL_INT_PLUS_MICRO: if (scale_val2 < 0) From patchwork Mon Nov 15 03:43:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618633 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3CF7C43217 for ; Mon, 15 Nov 2021 03:43:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AC0A161501 for ; Mon, 15 Nov 2021 03:43:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236998AbhKODqr (ORCPT ); Sun, 14 Nov 2021 22:46:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236980AbhKODqj (ORCPT ); Sun, 14 Nov 2021 22:46:39 -0500 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23806C0613B9; Sun, 14 Nov 2021 19:43:44 -0800 (PST) Received: by mail-qt1-x833.google.com with SMTP id q14so14362189qtx.10; Sun, 14 Nov 2021 19:43:44 -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=VMY/JioF3zIz+Odh4I/DMfpPOO5BAQwBFBQHl1YqOXg=; b=XCzh1vVCyZqeQus0OO+L0Fq/KxcL9FHjGn3HgY4xzR+SFl7FH3xZ1F7zapXOLVi0Wm Cb4tZ4Tin4Kk1/mwxVNR1w6ihE2R2397a4hjNJdOJr+Sa8dLU7lGG/0+4xkbaC05xxIB 93zeARHa7zcTDjR4fbQr5n8k8gAvkCjmaUDjxDEl0WKNWEjePljFGM8W40/aI3ygFO1x NJJpCFHDcP+TPIAw7ycor47Q1TFADXZzTbtpm5U96njlmJkIuWmEyJF4jko7SpbkX863 nbc/PrC49Uoa5fcgk8R/prVAUhFq5L1bfutwGhG3gLZ9Q5ASIZfBzH4m+FYVkEciZ1Lm bXxA== 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=VMY/JioF3zIz+Odh4I/DMfpPOO5BAQwBFBQHl1YqOXg=; b=4h+9OmV0Y4amhgcSM/GkEhuASkc3WUNGLAuePnF7boOwdihXynjg5absLb1BVrUQfS grINRSzQrZL21Z3XRpXygQNDXRFXSFCXfBA4haPeRGWUPo50aKlEkNrGpS2Gbjsy+Hey SFcs6ovbSTlfZ/xHjjbTylLvQw06xHP76ZMsRvzorancey24Cm7MMAJTbPV+pDs+V14i 3qXg4s5iqll7MZb4FCuf4NOnTvlmodCcFzQQ/ULqejsxA4cP+niznu4VXki8u/LdqVKe qQ5Ht1djj3Fx7/7yX8Dhiz+g8NDzgXdxe4W+b76tRREQj73D/eEJoRJab74U0S/4ek65 w+gQ== X-Gm-Message-State: AOAM5309f7/e/rbQzxLSJTP0BK6ZXvJw9ekngDCWVSt1soj2alTHNKef odxgj8QU7Dddea6Ud4s19Fw= X-Google-Smtp-Source: ABdhPJwCrPSlOg81o6BVcDvxJvuSkeNrb/3pXRDLCJNzhPWKtFnGibkV7lDiTGLMKmaOqROS2JDmoA== X-Received: by 2002:ac8:5c16:: with SMTP id i22mr12526397qti.313.1636947823306; Sun, 14 Nov 2021 19:43:43 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:42 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 02/14] iio: inkern: apply consumer scale when no channel scale is available Date: Sun, 14 Nov 2021 22:43:22 -0500 Message-Id: <20211115034334.1713050-3-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin When a consumer calls iio_read_channel_processed() and no channel scale is available, it's assumed that the scale is one and the raw value is returned as expected. On the other hand, if the consumer calls iio_convert_raw_to_processed() the scaling factor requested by the consumer is not applied. This for example causes the consumer to process mV when expecting uV. Make sure to always apply the scaling factor requested by the consumer. Fixes: adc8ec5ff183 ("iio: inkern: pass through raw values if no scaling") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 021e1397ffc5..dbe13fad3cbb 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -607,10 +607,10 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, IIO_CHAN_INFO_SCALE); if (scale_type < 0) { /* - * Just pass raw values as processed if no scaling is - * available. + * If no channel scaling is available apply consumer scale to + * raw value and return. */ - *processed = raw; + *processed = raw * scale; return 0; } From patchwork Mon Nov 15 03:43:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618655 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB029C433EF for ; Mon, 15 Nov 2021 03:44:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CEE3261B4E for ; Mon, 15 Nov 2021 03:44:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237074AbhKODrP (ORCPT ); Sun, 14 Nov 2021 22:47:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236904AbhKODqq (ORCPT ); Sun, 14 Nov 2021 22:46:46 -0500 Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F014FC061200; Sun, 14 Nov 2021 19:43:44 -0800 (PST) Received: by mail-qt1-x82e.google.com with SMTP id 8so4756047qtx.5; Sun, 14 Nov 2021 19:43:44 -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=l0Xi6ZFTcGrFltb6tqOU/b1e/rqtTlkrNbyKOULVJ8Q=; b=oQag/Q37DaqugGCMp7S6eHvewC94q2DDuB47+Ldkx7Sskw1NnyQ4Z2t2FKL1i42AG2 DkNlFyV0oz/7xOOM0Oe3j54IZNGZGC96gPYz46eMR7qAq3SO/9OPKLT7iKlMyqIwRr4X PRTeFZ+8bDnT/8MdtWypStLdWOMgwkI2HqEd7i+/5fdYYlx+hbxH4dbpx0CTlxd5vU+A le2MPorPYCFEUtyxaJ8vIwqfgPQfJcwhqSch+2VA9au6A0oz7JNb/qwusb2Dxh0pe18U 9Ie0+18QM3kkhMMHh+9xFG1Tq4RnAXLl17wssh8obNQ6wm4DUsCmNivmBefvNyXX1u/+ BFMg== 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=l0Xi6ZFTcGrFltb6tqOU/b1e/rqtTlkrNbyKOULVJ8Q=; b=Xaeo7ZNYuscv6U+EbOvTAz5q0gHckDfkuZrRdSuSIDBui950DoFFJ9fuuz0+NXBlNW VH5X+Na+68AiBqUws9vbo8HZtTQv0GSqw2OAoJfEOQTpqvOpY+dKXEmMDTXIGuJWhLk0 nYA7WloLYrFbY6if0GC9qFNOOESXVQZrL1wwy8+VRfoZ3pmFIpxyo1zRF4lfpYUM15I9 NvWfCucJur/kfZRTEUFKJufxwWv0sJwRcPqLBk5w0ENiuOAj1xyFeo97RUxXJ5nWEL3w zXHL5Y0UYf53w4alRwQPovmB1WzOLIUzRPfRxp9iZqKnwWCjBbnFlmDuLLZtXEqm3NyW AgoQ== X-Gm-Message-State: AOAM532V8ozzrEnz1Jb1UrfbTrmVrSEEjuKI+3+Heq1WNO6lEqh/s3j7 d9edSG/Q0AYhQinHbSB3PiinTMLYLgA= X-Google-Smtp-Source: ABdhPJw/NfKZ1RKmNhtiw/zJakdR2nQvGutBolbVL/GcsTHwahcHsSLlSPcMXM8lqRvjTmwvBzVbIw== X-Received: by 2002:a05:622a:1484:: with SMTP id t4mr36756230qtx.57.1636947824160; Sun, 14 Nov 2021 19:43:44 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:43 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 03/14] iio: inkern: make a best effort on offset calculation Date: Sun, 14 Nov 2021 22:43:23 -0500 Message-Id: <20211115034334.1713050-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin iio_convert_raw_to_processed_unlocked() assumes the offset is an integer. Make a best effort to get a valid offset value for fractional cases without breaking implicit truncations. Fixes: 48e44ce0f881 ("iio:inkern: Add function to read the processed value") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index dbe13fad3cbb..aff6d2d3447c 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -595,13 +595,35 @@ EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, int raw, int *processed, unsigned int scale) { - int scale_type, scale_val, scale_val2, offset; + int scale_type, scale_val, scale_val2; + int offset_type, offset_val, offset_val2; s64 raw64 = raw; - int ret; - ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); - if (ret >= 0) - raw64 += offset; + offset_type = iio_channel_read(chan, &offset_val, &offset_val2, + IIO_CHAN_INFO_OFFSET); + if (offset_type >= 0) { + switch (offset_type) { + case IIO_VAL_INT: + break; + case IIO_VAL_INT_PLUS_MICRO: + case IIO_VAL_INT_PLUS_NANO: + /* + * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO + * implicitely truncate the offset to it's integer form. + */ + break; + case IIO_VAL_FRACTIONAL: + offset_val /= offset_val2; + break; + case IIO_VAL_FRACTIONAL_LOG2: + offset_val /= (1 << offset_val2); + break; + default: + return -EINVAL; + } + + raw64 += offset_val; + } scale_type = iio_channel_read(chan, &scale_val, &scale_val2, IIO_CHAN_INFO_SCALE); From patchwork Mon Nov 15 03:43:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618659 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99B9DC433F5 for ; Mon, 15 Nov 2021 03:45:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8419461B6F for ; Mon, 15 Nov 2021 03:45:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237148AbhKODsj (ORCPT ); Sun, 14 Nov 2021 22:48:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236590AbhKODqp (ORCPT ); Sun, 14 Nov 2021 22:46:45 -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 D95E3C061202; Sun, 14 Nov 2021 19:43:45 -0800 (PST) Received: by mail-qt1-x82b.google.com with SMTP id j17so14414469qtx.2; Sun, 14 Nov 2021 19:43:45 -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=UbEAz7/Cn9UDWRCP1DwPP6+dnbZoJceTj6XSahw11p4=; b=Wj36hlxjHpuwzVT2J1hIS/p17YlFwiUxPEp15JjQlKMXVev/Wb6+hMoremFgCfwxK6 7I0IdBDXEf0Egq28R9cU9ioR55Bsmi6hboLvUjEKdbhio94hCg2jPIZsQmodPM42DOXk QoaWPags2vS2PL53xlCIiTQKejegaAe0zO9cd9YGMAtnBP8EkbTuzcyZpnyqOq2fsU2t H7apqpD0ASxeP6oFuwUMq73iD6DLa/JSS6l6pRh+Qk/P3VvDQ4EIHgnVeVClZUON65mQ K36cF1yP1IVneplDxeYzJOB/JoqvpY8czU2yKmlGk5Y5UR5qp9pvXhLlBrRLEJ108v7u lsDg== 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=UbEAz7/Cn9UDWRCP1DwPP6+dnbZoJceTj6XSahw11p4=; b=AWK8QNifGOvugDyQzzf2S/nu5NA4JZmcmoE4GvN8NHk3l6V3eeRCXkiKu+sOnh//du g+6cuuc8NyPYNsAFROZh+03vkXMDKkwnuxn+7bMTIRCiBbil80ZvlGVxzewejHbyVZRY i5uw2kE6h8Vzn3FIMBweZnVMxIXnRjiOYWkbnn5zap2wNaWs6fyFH8hYgOxzyfuoeXFp OqMrqoRmaqTx8adxRsEEGkSJPSXCl+DOsMtVMJJ8n8S8MfVkHzQm7YsuCd7v+AnccDZK K5ZlWZ0aOeZzG9ZD+amq2abSrzycgGKuZnjfYI6kuxS/4XrOsOBvOwkA26opA8MEg++I D2+A== X-Gm-Message-State: AOAM531z5prdl7rq58kx153zQLfGxe7kvUyaMC5aW6MDpRql6MwkEdgP GW+H2FbFpX414/UZ3NhW0zk= X-Google-Smtp-Source: ABdhPJx/jXLyhqvoOfbg73y9n1K6KME5CuynIBK5hiEandg4/Yl0vmFlNqO9LGwtuT4eD3c00ND1AA== X-Received: by 2002:ac8:5f84:: with SMTP id j4mr38091240qta.271.1636947825071; Sun, 14 Nov 2021 19:43:45 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:44 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 04/14] iio: afe: rescale: expose scale processing function Date: Sun, 14 Nov 2021 22:43:24 -0500 Message-Id: <20211115034334.1713050-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin In preparation for the addition of kunit tests, expose the logic responsible for combining channel scales. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 65 ++++++++++++++------------------- include/linux/iio/afe/rescale.h | 30 +++++++++++++++ 2 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 include/linux/iio/afe/rescale.h diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 774eb3044edd..d0669fd8eac5 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -11,35 +11,46 @@ #include #include #include +#include #include #include #include #include #include -struct rescale; - -struct rescale_cfg { - enum iio_chan_type type; - int (*props)(struct device *dev, struct rescale *rescale); -}; +int rescale_process_scale(struct rescale *rescale, int scale_type, + int *val, int *val2) +{ + unsigned long long tmp; -struct rescale { - const struct rescale_cfg *cfg; - struct iio_channel *source; - struct iio_chan_spec chan; - struct iio_chan_spec_ext_info *ext_info; - bool chan_processed; - s32 numerator; - s32 denominator; -}; + switch (scale_type) { + case IIO_VAL_FRACTIONAL: + *val *= rescale->numerator; + *val2 *= rescale->denominator; + return scale_type; + case IIO_VAL_INT: + *val *= rescale->numerator; + if (rescale->denominator == 1) + return scale_type; + *val2 = rescale->denominator; + return IIO_VAL_FRACTIONAL; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = *val * 1000000000LL; + do_div(tmp, rescale->denominator); + tmp *= rescale->numerator; + do_div(tmp, 1000000000LL); + *val = tmp; + return scale_type; + default: + return -EOPNOTSUPP; + } +} static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); - unsigned long long tmp; int ret; switch (mask) { @@ -65,27 +76,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev, } else { ret = iio_read_channel_scale(rescale->source, val, val2); } - switch (ret) { - case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; - return ret; - case IIO_VAL_INT: - *val *= rescale->numerator; - if (rescale->denominator == 1) - return ret; - *val2 = rescale->denominator; - return IIO_VAL_FRACTIONAL; - case IIO_VAL_FRACTIONAL_LOG2: - tmp = *val * 1000000000LL; - do_div(tmp, rescale->denominator); - tmp *= rescale->numerator; - do_div(tmp, 1000000000LL); - *val = tmp; - return ret; - default: - return -EOPNOTSUPP; - } + return rescale_process_scale(rescale, ret, val, val2); default: return -EINVAL; } diff --git a/include/linux/iio/afe/rescale.h b/include/linux/iio/afe/rescale.h new file mode 100644 index 000000000000..14d4ee1227c6 --- /dev/null +++ b/include/linux/iio/afe/rescale.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2021 Liam Beguin + */ + +#ifndef __IIO_RESCALE_H__ +#define __IIO_RESCALE_H__ + +#include + +struct rescale; + +struct rescale_cfg { + enum iio_chan_type type; + int (*props)(struct device *dev, struct rescale *rescale); +}; + +struct rescale { + const struct rescale_cfg *cfg; + struct iio_channel *source; + struct iio_chan_spec chan; + struct iio_chan_spec_ext_info *ext_info; + bool chan_processed; + s32 numerator; + s32 denominator; +}; + +int rescale_process_scale(struct rescale *rescale, int scale_type, + int *val, int *val2); +#endif /* __IIO_RESCALE_H__ */ From patchwork Mon Nov 15 03:43:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618637 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECC27C4332F for ; Mon, 15 Nov 2021 03:43:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D1B3061B51 for ; Mon, 15 Nov 2021 03:43:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237003AbhKODqu (ORCPT ); Sun, 14 Nov 2021 22:46:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236983AbhKODqq (ORCPT ); Sun, 14 Nov 2021 22:46:46 -0500 Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE428C061746; Sun, 14 Nov 2021 19:43:46 -0800 (PST) Received: by mail-qk1-x729.google.com with SMTP id de30so15425422qkb.0; Sun, 14 Nov 2021 19:43:46 -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=I7D4nEFouFzhdepZfuhLsdMRJne14BoWmzVnF0SIgeg=; b=ab7lWP2hi3/tmDgXMkXDpXKnReAlfU0aGPgFLrE4wXiRZjludYe2nzHOCqZ+8YvFbW ISIjUj2+/3p4l98kPnEkxGU7US4zylEFV5R/FxUf8uNWx1cB3fz5c83paGcdSor2skDe XKMuYF4slMF3rVYeueLGaP/4po4DXbGARtE/G5yHnbOO4mqSrzk+uE4wm/mH7dYh/sJu vkqMNNtfoQ6feAjJETpnq2wGW8x0sgVgvtfkGReHZoQEbI0jxiQBq/OZ8EM5Lu2y/wu9 3IV3bAExA/MNe2XTsbjtObU8Y6a6QvhskA3TZ7/vBFtMbBqiLqJ7B5Kvh7DO5V+L2457 ddWQ== 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=I7D4nEFouFzhdepZfuhLsdMRJne14BoWmzVnF0SIgeg=; b=4Dj6Ei3ojQsbOId5cHNjCWvPLPHQYomSI6XFH7YNg8mctuaOCrE1FupFd2yEW1fQj8 Jw1+el0FS0wyUUE0BZP8BYHMxDtfNTJDuhPLJxQ1cj3XcK3SIK5O0OvhMOXxQuBrnr5f lAgzn6LpHzjGVDjzaW3lNevJK6V/65yOZ5S3LUas+79BkZn3k80U9lq7KOrVNYtfaqv+ IbAbiuJNELu7A8OcWOM2FzKYxIxSCb035V4eZV+KFwcbL8/IfaoRoRYzU5PZ72UDwdeL z/+VJ6BUxjhuCvRDZxqmkq9CaUSVgjaKPEpAuo4TVlLyEq//A1L+Plm/TS2At98u7vw7 6wnA== X-Gm-Message-State: AOAM532dA6zzFlbvWwzWc7Wfd2VtTyJ/iyxJSqhTlOdQq8lRy/Uqcjyx 2b1lqHdZ5u0u1uhOT1/x26E= X-Google-Smtp-Source: ABdhPJzBIXabjp14B6f4yQsRpcDDVHg49EgufMADMME53jbWIEAAkOZd+o2SFTNfG4X+KpPiz89HBw== X-Received: by 2002:a37:b781:: with SMTP id h123mr26806238qkf.491.1636947826032; Sun, 14 Nov 2021 19:43:46 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:45 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 05/14] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Sun, 14 Nov 2021 22:43:25 -0500 Message-Id: <20211115034334.1713050-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin Some ADCs use IIO_VAL_INT_PLUS_{NANO,MICRO} scale types. Add support for these to allow using the iio-rescaler with them. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index d0669fd8eac5..2c25a6375f99 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -22,6 +22,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { unsigned long long tmp; + s32 rem; + u32 mult; + u32 neg; switch (scale_type) { case IIO_VAL_FRACTIONAL: @@ -40,6 +43,39 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, tmp *= rescale->numerator; do_div(tmp, 1000000000LL); *val = tmp; + return scale_type; + case IIO_VAL_INT_PLUS_NANO: + case IIO_VAL_INT_PLUS_MICRO: + if (scale_type == IIO_VAL_INT_PLUS_NANO) + mult = 1000000000LL; + else + mult = 1000000LL; + /* + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if 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 Mon Nov 15 03:43:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618639 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DD17C433EF for ; Mon, 15 Nov 2021 03:44:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0755B60551 for ; Mon, 15 Nov 2021 03:44:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237001AbhKODqy (ORCPT ); Sun, 14 Nov 2021 22:46:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236985AbhKODqq (ORCPT ); Sun, 14 Nov 2021 22:46:46 -0500 Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8482C061766; Sun, 14 Nov 2021 19:43:47 -0800 (PST) Received: by mail-qk1-x736.google.com with SMTP id q64so4623580qkd.5; Sun, 14 Nov 2021 19:43:47 -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=IX/ZGPvAy7LXbAYeuzpvSfBa2Ha80uZ+Fc7J14Ewl2A=; b=jrGKLAfK7+7U8EPejEWbjPGlrtdtYpdEK3bRGPlD9uyPBsTAFK/4cOEG7rFzQzmaxX 3taf0IKzZ+sbhnGVvpqFrg/Gw/jp5czJnKQY3ybaraQoSv2JGSvi8kJY86oGnHG9ZrUZ +MezK8rgq6UxHrcgV3M+NNXjAtL60XX1tvp3pe49OJk71b6mfm4PQbHImILwvS0OV6S8 H1CFxIPP5+sETlSETpENuMcfG/2UWEUjvifevGTA2ea/KIWxIvEenkkVZ1gZeQW+EHfU QPlr/Zkcmy6ea4AMusJSr9LyA5j5xi4OO6pgDGOePbz1we4IHuYuEU917flPsHTMDcae BfCw== 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=IX/ZGPvAy7LXbAYeuzpvSfBa2Ha80uZ+Fc7J14Ewl2A=; b=JOfqpZWYTLofzu48ROKMMR9AQtVWEUqobHqn5MuC+JvPbaXkgGYSxUYjqpmQRjib2g QYdEU3hyGTUP1UDYypU7sET0Kh48yKFx9guMsosnl94v9lfyY6wAaUS8Jv449DRZQneG J4g86N9OrUxbKIQE/Ra3hDuOnpmuhUKBKkdf2RF6sPp1FDATe+qzyOzY41p4U89jvrUZ 4E+EfLPTMbVpacT7C+t7tixZ7WeAf0R/4POb5jAh2QCES3jPHB56IiSpAxSZnmFMoiAX xpc+CFvzaGu0Ga/m7GHFSclkNGELM92OyrPZ+ecjUPE6O5quc7Fg6T+rdTrKvfKO4aHi PuKQ== X-Gm-Message-State: AOAM530VcnVnjw7NW7LRNA8uYi8qMKkeO3MrAyEl/naHMBqLwyKXgW5i fmWs4YQbTziLN67TpWBFIEE= X-Google-Smtp-Source: ABdhPJzMgfQGV1Mz/AuAUdqqCHsDCK4EMgKfbUrE9xZ+uE8m2zj65AOqpCeNE2Hu0b8pqKm2JEBqFA== X-Received: by 2002:a05:620a:2f9:: with SMTP id a25mr28474283qko.327.1636947827061; Sun, 14 Nov 2021 19:43:47 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:46 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 06/14] iio: afe: rescale: add offset support Date: Sun, 14 Nov 2021 22:43:26 -0500 Message-Id: <20211115034334.1713050-7-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-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 2c25a6375f99..da462e0910ff 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -82,11 +82,46 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, } } +int rescale_process_offset(struct rescale *rescale, int scale_type, + int scale, int scale2, int schan_off, + int *val, int *val2) +{ + s64 tmp, tmp2; + + switch (scale_type) { + case IIO_VAL_FRACTIONAL: + tmp = (s64)rescale->offset * scale2; + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT: + *val = div_s64(rescale->offset, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = (s64)rescale->offset * (1 << scale2); + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_NANO: + tmp = (s64)rescale->offset * 1000000000; + tmp2 = ((s64)scale * 1000000000L) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * 1000000L; + tmp2 = ((s64)scale * 1000000L) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + default: + return -EOPNOTSUPP; + } +} + static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); + int scale, scale2; + int schan_off = 0; int ret; switch (mask) { @@ -113,6 +148,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; } @@ -189,6 +265,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 @@ -353,6 +432,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 Mon Nov 15 03:43:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618645 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6AA4C433F5 for ; Mon, 15 Nov 2021 03:44:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8DFC361501 for ; Mon, 15 Nov 2021 03:44:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234984AbhKODrB (ORCPT ); Sun, 14 Nov 2021 22:47:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236994AbhKODqs (ORCPT ); Sun, 14 Nov 2021 22:46:48 -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 C59E8C061207; Sun, 14 Nov 2021 19:43:48 -0800 (PST) Received: by mail-qk1-x72b.google.com with SMTP id d2so10428209qki.12; Sun, 14 Nov 2021 19:43:48 -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=vzT5IZ5YlHRugGQyo3L+z7jU4ovJcZXhCyDbEZIDlM8=; b=EfkVu6bn+xzaOQ3sCe+bRdeu8GBPVHZNQHu/zuCGa3p5xr42GMYI2X8DjnHhJm30sL v3tuf2uVr8jwmxPVhujkOSMQyoJG6lYPwO/stOhraDTVYGNlqFmc1uwZXQl2GVKrBqVO 5n/51NtEq/4lazGeOCCmh+yqJ4SyEC0mATgJt9+qZ2zL+uxHEmeL9zB8AES9Coy6bOea +pYwignEAZn0/YfRIRxpWCThnos+wWxN2N8R1PQTgUVG3U5IcR6AzJNS77n2g/B9oolW x/h9AxwIioWB5riWDXeAemGD2WFnQ107/l0pp1dXTj2AjNIBeptUzplH3aZKlNK7rAjX ohFw== 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=vzT5IZ5YlHRugGQyo3L+z7jU4ovJcZXhCyDbEZIDlM8=; b=KfHX4JRtP4c5whRP7fMjNBA9HHf053KxgEhGBeCx7Ok7YA6U7OrakW6IYrhQK8Yln9 Raui/E+BHr9jujYw34iJME6lLlYAoqBoAgpeCWE2xXICdYPiHXtLnYnQv8L3dk/5f035 s89fFKqp05OSeTZQcYJnpvfD+BSw+Bd0G8REjcEAbHpbfFBdgZvqkxXGJ0gyePwB7+MM o/fcBJXsTJuWnTciG9fmO568Kq4tVyv58pZsE2r5X/QGWMBSmDmDK64YaBwGvpVXYdoG 0SEhgu1pCT/WrDhnAzjwfjwZO0pf2Uy22P2m0CoOTxQWYiUmIlo2x8Txeg2B9JPUk35c NWaw== X-Gm-Message-State: AOAM530iqqGOC8j1IejaIvoe0QzgAuG/GAftdc0Vi+VuUXH1by2u/NFw E0VhET/0QIUHazBKENAt6S8= X-Google-Smtp-Source: ABdhPJyyTQGLHYrrcmZQ1LlRvRTAIqqcRJRsRKdZjFeCvA2CQQnpgYAY9uJN2Hsl/n4apD0Ak7231A== X-Received: by 2002:a05:620a:16b9:: with SMTP id s25mr15457218qkj.409.1636947827967; Sun, 14 Nov 2021 19:43:47 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:47 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 07/14] iio: afe: rescale: use s64 for temporary scale calculations Date: Sun, 14 Nov 2021 22:43:27 -0500 Message-Id: <20211115034334.1713050-8-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin All four scaling coefficients can take signed values. Make tmp a signed 64-bit integer and switch to div_s64() to preserve signs during 64-bit divisions. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index da462e0910ff..394f8b16b29c 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -21,7 +21,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; @@ -38,10 +38,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 Mon Nov 15 03:43:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618643 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D97FAC433FE for ; Mon, 15 Nov 2021 03:44:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BFDC460555 for ; Mon, 15 Nov 2021 03:44:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236971AbhKODq7 (ORCPT ); Sun, 14 Nov 2021 22:46:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236995AbhKODqs (ORCPT ); Sun, 14 Nov 2021 22:46:48 -0500 Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD510C061208; Sun, 14 Nov 2021 19:43:49 -0800 (PST) Received: by mail-qk1-x729.google.com with SMTP id q64so4623664qkd.5; Sun, 14 Nov 2021 19:43:49 -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=Jw54IKpjaf2lkn1AccMjzP5THrJX1wdOQq5FTCzCg5c=; b=DjLRDmDVbl1ZyXfxqXLk1sdMNlIAw/tQVyXtqgCGXuAKMPNc4nEPFvThwMufa8lLo8 qvw4v2b66A4cDzPnMM5L8jKgwt8JixhJGrd2K3pNktNZo4axDrN88DodndrSpeSsPpUa 9YDH6Th83X0FdrwVZdQa/bRD1wxHPXZcXbuoBme1Mp73SfDoDWZCGw4fELV0GSLlPjVM +ypz+eb5fPoVEjZNh3I/TnmdAdu8NFfzSiqAK/nZIqdLs5ZlN/eUcq7sBndG3H0usBr+ Rx5EDYhyIxZIZWayZocsEphIKMEU+cQbSDHIVg5F3C08K/KJj/lzN8zhEt1THYiNpr71 S6HA== 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=Jw54IKpjaf2lkn1AccMjzP5THrJX1wdOQq5FTCzCg5c=; b=6b/cFSupnqZF+0A4NDrwbbLTztH3DIb/vyFgg+kNGDEl1+qe4h7fV4lbwHUUGGXaBk Q9MNjV/laMZs8P5L8lWdKlrRs44bGMjtxz3w8ZCf4/Xsl56++CrOfFOp3LaNVwfmP8tQ gs2RkU2Nf067PzQVc77bGx5yPISwNcExdXJWlkXzeuPcToWumfd6R4tf9B2fzMaM5xfK x5YN4xsAzK4jPbAOuOfDe0GSiSjpm6AzWGNK05Vg3vx8eQPOQ5bmLKTmjcksiZLGpBMj ir0w91qCoensHTNcPUUmPhE8B4x6cAomglFCE6cIku9E/dATy8MFpI1JAQHEjP+wOIPF Ttqw== X-Gm-Message-State: AOAM533c+8PRlNceqLOwjAsCreAmz3AQu2nylwXWtT3k/0gIUstIa51k KjC/jOlOwyKJH+BsH3hyOH8= X-Google-Smtp-Source: ABdhPJxL/aQ5f6EoQ5PThbTF+n8FB/+yxi8RKinApwa1x4nntEkDDH6FvPHU7hQOwMcq2q8zlFgHWQ== X-Received: by 2002:a05:620a:3191:: with SMTP id bi17mr22879088qkb.266.1636947828961; Sun, 14 Nov 2021 19:43:48 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:48 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 08/14] iio: afe: rescale: reduce risk of integer overflow Date: Sun, 14 Nov 2021 22:43:28 -0500 Message-Id: <20211115034334.1713050-9-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin Reduce the risk of integer overflow by doing the scale calculation on a 64-bit integer. Since the rescaling is only performed on *val, reuse the IIO_VAL_FRACTIONAL_LOG2 case. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 394f8b16b29c..17036130d364 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -27,16 +27,13 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, 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: case IIO_VAL_FRACTIONAL_LOG2: tmp = (s64)*val * 1000000000LL; tmp = div_s64(tmp, rescale->denominator); From patchwork Mon Nov 15 03:43:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618657 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B5E4C433EF for ; Mon, 15 Nov 2021 03:44:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2DE7B60551 for ; Mon, 15 Nov 2021 03:44:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237015AbhKODq7 (ORCPT ); Sun, 14 Nov 2021 22:46:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236996AbhKODqs (ORCPT ); Sun, 14 Nov 2021 22:46:48 -0500 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C61A7C06120A; Sun, 14 Nov 2021 19:43:50 -0800 (PST) Received: by mail-qk1-x731.google.com with SMTP id p4so6964673qkm.7; Sun, 14 Nov 2021 19:43:50 -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=jbkvNiZPeqKZDFobOFBdHT7ASm74e41udPizXrb8loc=; b=gqQu1qRYyYkbkTxRfa2gJq13cZESiPU6TwYXyhhtszE8OP2w0N/JT842Bqu0t8la7u 9dTURCd3Q8xz+cqeusNg8cgUPy9NrTsdINrJUjf0RwKSg9u7WYr5G/DHaUz7VZTW2LtF qdCsWFfpOdu9KTRoov6p4KQzRDR10ChVZPotvFX6yrGz7jrW9/6sn6WUid5PN6N+sq9S bQ8Ahys8rltIeg5lyX36PwJMXRNysE8+yY/e1pmHniL//RFm81MkettaJHYQLnoJuq9L M8qMXkqJQz08lyQGYr+ajgSnzxV2aqxPi0SeA1Ip7q/rFRlzyCPYUOm6ffgfZhViygoo jlCw== 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=jbkvNiZPeqKZDFobOFBdHT7ASm74e41udPizXrb8loc=; b=e6cksk26ka196tMSJXeZDp2MwvgD5+osKtRZ+/Bz5NpyjdCPxP+j9I+5grFShahegb rb5rwuO9gI8vc/h/mHXJz3Dwf52TFUpGwXYRLBwfQkeT9Zz0lNffDiRt9Yn8pIiy93GW XRjgEiGkCJ+ozPjWUz+wHcmW0f9VS9kqQuOy/MNCuICo+71IfHU/AefVMf4U3dLY3ED5 +C8tnAY7Fy+1XOBCA95y3FNphnEWP2aOZjQ+quyET+LcT3gcbSXI5jBQycwBispKSg5b /UvOVjQy1DpM+eHLa/+9E8jyqIErhuDQoecYI4ifwfnCb0g0ODcVGvdY+95ycg3z67hE dMQw== X-Gm-Message-State: AOAM532ysMEQoLeIwS9UcOM6l6Yne2rAj6ica+v28h0jvkInd9Qu3E5C NiQFDKspDn+ZuwbE4B3g6swi1m+dJRY= X-Google-Smtp-Source: ABdhPJwXbbMyQTknT3hcPa31IUIno454eah6cdfqQiinZd09ZQjFYtKMbMa3fLmY/o2bEBfNR0awtA== X-Received: by 2002:a05:620a:4248:: with SMTP id w8mr27476306qko.228.1636947829962; Sun, 14 Nov 2021 19:43:49 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:49 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 09/14] iio: afe: rescale: fix accuracy for small fractional scales Date: Sun, 14 Nov 2021 22:43:29 -0500 Message-Id: <20211115034334.1713050-10-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin The approximation caused by integer divisions can be costly on smaller scale values since the decimal part is significant compared to the integer part. Switch to an IIO_VAL_INT_PLUS_NANO scale type in such cases to maintain accuracy. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 17036130d364..8a2f1c0ca5a3 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) { s64 tmp; - s32 rem; + s32 rem, rem2; u32 mult; u32 neg; @@ -38,9 +38,26 @@ 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; + + if (scale_type == IIO_VAL_FRACTIONAL) + tmp = *val2; + else + 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: if (scale_type == IIO_VAL_INT_PLUS_NANO) From patchwork Mon Nov 15 03:43:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618641 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 098DFC4332F for ; Mon, 15 Nov 2021 03:44:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E318A60555 for ; Mon, 15 Nov 2021 03:44:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235121AbhKODq6 (ORCPT ); Sun, 14 Nov 2021 22:46:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236979AbhKODqs (ORCPT ); Sun, 14 Nov 2021 22:46:48 -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 C2A05C06120B; Sun, 14 Nov 2021 19:43:51 -0800 (PST) Received: by mail-qk1-x72b.google.com with SMTP id 193so15330755qkh.10; Sun, 14 Nov 2021 19:43:51 -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=sTKRSB4Qc+yPSLTqN9oTRz1FeyqDvG7ZVYmsxWkyjsI=; b=OOqmxtRwLvaiEqqcHzTWgPR4qIeNkXuh2vaY06oYmGjSKdEOZ5TnLGya+UthulWij4 e9qXp82+OX27csggSBd13ZrRKvLzzMOeEUg7kUa8pa7eIdB0UQlHzCbegWQKGAqZE3af X2XHcRG0WQghQvc6dYachvSLi4DxnPcYFHPpWkSvckImGus+GdNCRQfFicbTi2IWaXhr 7xcN4iRzPcLQJo7/KcfuV47JRkB69qKa9laserRJk7u/FveyJhjoW/RX03RDi+Ym+H9h PLxpVle44QMM/amvHIyzqZ6voqe2oOU/THEMzeLlgMPy5OuPAeTz0JG3VXrUCy4hkV9i Bq3g== 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=sTKRSB4Qc+yPSLTqN9oTRz1FeyqDvG7ZVYmsxWkyjsI=; b=Ckr00hAp2yfR4P8YF/GZUcjtyfybZxSiSzWCqphx2XsvxneQ/EMinzOb3m+1CHhKKf 8ZSydNTeZjlNpR93VMQBC8GzCODDa75Ww3yuq8etD1mkt6V5PjeIizq7DdSZq0iPDFep OGZOHMu9sJ8W4mc47rLkafnEQ88IO10eWXqLgcBJps/x++92MBwswiCnpFx5cZKvOeRo tD4N17NltVRkGM9jUApg+RfUFRjEGNwbpa0GqOQmXfxjf7IQ+E+TNxKRW84OmOyZhf0p TG3wdumrN+vbGGQ56TUuMjHUf68O6gMWW0GOkelSFo+NwfScS1ehvfLC6uSzdWPohPl6 Wjtw== X-Gm-Message-State: AOAM533SS5Y0Wp6B9VsfasqoBsEk0NCVqwyj+ChVYgJO0Z4OGiYgXV22 LxcmGyuqCM6bl3P38RC2iyk= X-Google-Smtp-Source: ABdhPJwFsCKrBidDcz/o5clSKe3Xu4Oo7aA/1mkT2wzQVxiSByQg2QRLzYiwpJKt5QtqWgal7B9uXg== X-Received: by 2002:a05:620a:424b:: with SMTP id w11mr27439359qko.318.1636947830868; Sun, 14 Nov 2021 19:43:50 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:50 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 10/14] iio: test: add basic tests for the iio-rescale driver Date: Sun, 14 Nov 2021 22:43:30 -0500 Message-Id: <20211115034334.1713050-11-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin The iio-rescale driver supports various combinations of scale types and offsets. These can often result in large integer multiplications. Make sure these calculations are done right by adding a set of kunit test cases that build on top of iio-test-format. To run these tests, add the following to .kunitconfig $ cat .kunitconfig CONFIG_IIO=y CONFIG_IIO_RESCALE_KUNIT_TEST=y CONFIG_KUNIT=y Then run: $ ./tools/testing/kunit/kunit.py run --kunitconfig .kunitconfig Signed-off-by: Liam Beguin Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/iio/test/Kconfig | 10 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-rescale.c | 705 ++++++++++++++++++++++++++++ 3 files changed, 716 insertions(+) create mode 100644 drivers/iio/test/iio-test-rescale.c diff --git a/drivers/iio/test/Kconfig b/drivers/iio/test/Kconfig index 679a7794af20..872ed4ed140a 100644 --- a/drivers/iio/test/Kconfig +++ b/drivers/iio/test/Kconfig @@ -4,6 +4,16 @@ # # Keep in alphabetical order +config IIO_RESCALE_KUNIT_TEST + bool "Test IIO rescale conversion functions" + depends on KUNIT && !IIO_RESCALE + default KUNIT_ALL_TESTS + help + If you want to run tests on the iio-rescale code say Y here. + + This takes advantage of ARCH=um to run tests and should be used by + developers to tests their changes to the rescaling logic. + config IIO_TEST_FORMAT bool "Test IIO formatting functions" depends on KUNIT=y diff --git a/drivers/iio/test/Makefile b/drivers/iio/test/Makefile index 467519a2027e..f15ae0a6394f 100644 --- a/drivers/iio/test/Makefile +++ b/drivers/iio/test/Makefile @@ -4,5 +4,6 @@ # # Keep in alphabetical order +obj-$(CONFIG_IIO_RESCALE_KUNIT_TEST) += iio-test-rescale.o ../afe/iio-rescale.o obj-$(CONFIG_IIO_TEST_FORMAT) += iio-test-format.o CFLAGS_iio-test-format.o += $(DISABLE_STRUCTLEAK_PLUGIN) diff --git a/drivers/iio/test/iio-test-rescale.c b/drivers/iio/test/iio-test-rescale.c new file mode 100644 index 000000000000..526f87fa3514 --- /dev/null +++ b/drivers/iio/test/iio-test-rescale.c @@ -0,0 +1,705 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Kunit tests for IIO rescale conversions + * + * Copyright (c) 2021 Liam Beguin + */ + +#include +#include +#include +#include +#include + +struct rescale_tc_data { + const char *name; + + const s32 numerator; + const s32 denominator; + const s32 offset; + + const int schan_val; + const int schan_val2; + const int schan_off; + const int schan_scale_type; + + const char *expected; + const char *expected_off; +}; + +const struct rescale_tc_data scale_cases[] = { + /* + * Typical use cases + */ + { + .name = "typical IIO_VAL_INT, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 42, + .expected = "5210.918114143", + }, + { + .name = "typical IIO_VAL_INT, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 42, + .expected = "-5210.918114143", + }, + { + .name = "typical IIO_VAL_FRACTIONAL, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 42, + .schan_val2 = 20, + .expected = "260.545905707", + }, + { + .name = "typical IIO_VAL_FRACTIONAL, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 42, + .schan_val2 = 20, + .expected = "-260.545905707", + }, + { + .name = "typical IIO_VAL_FRACTIONAL_LOG2, positive", + .numerator = 42, + .denominator = 53, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 4096, + .schan_val2 = 16, + .expected = "0.049528301", + }, + { + .name = "typical IIO_VAL_FRACTIONAL_LOG2, negative", + .numerator = -42, + .denominator = 53, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 4096, + .schan_val2 = 16, + .expected = "-0.049528301", + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456, + .expected = "1240.710106203", + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456, + .expected = "-1240.710106203", + }, + { + .name = "typical IIO_VAL_INT_PLUS_MICRO, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 1234, + .expected = "1240.84789", + }, + { + .name = "typical IIO_VAL_INT_PLUS_MICRO, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 1234, + .expected = "-1240.84789", + }, + /* + * Use cases with small scales involving divisions + */ + { + .name = "small IIO_VAL_FRACTIONAL, 261/509 scaled by 90/1373754273", + .numerator = 261, + .denominator = 509, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 90, + .schan_val2 = 1373754273, + .expected = "0.000000033594", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 90/1373754273 scaled by 261/509", + .numerator = 90, + .denominator = 1373754273, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 261, + .schan_val2 = 509, + .expected = "0.000000033594", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 760/1373754273 scaled by 427/2727", + .numerator = 760, + .denominator = 1373754273, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 427, + .schan_val2 = 2727, + .expected = "0.000000086626", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 761/1373754273 scaled by 427/2727", + .numerator = 761, + .denominator = 1373754273, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 427, + .schan_val2 = 2727, + .expected = "0.000000086740", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 5/32768 scaled by 3/10000", + .numerator = 5, + .denominator = 32768, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 3, + .schan_val2 = 10000, + .expected = "0.0000000457763671875", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 0 < scale < 1", + .numerator = 6, + .denominator = 6, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 1, + .schan_val2 = 3, + .expected = "0.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL, -1 < scale < 0", + .numerator = -6, + .denominator = 6, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 1, + .schan_val2 = 3, + .expected = "-0.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL, 0 < scale < 2", + .numerator = 8, + .denominator = 2, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 1, + .schan_val2 = 3, + .expected = "1.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL, -2 < scale < 0", + .numerator = -8, + .denominator = 2, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 1, + .schan_val2 = 3, + .expected = "-1.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, 760/32768 scaled by 15/22", + .numerator = 760, + .denominator = 32768, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 15, + .schan_val2 = 22, + .expected = "0.000000082946", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, 761/32768 scaled by 15/22", + .numerator = 761, + .denominator = 32768, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 15, + .schan_val2 = 22, + .expected = "0.000000083055", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, 0 < scale < 1", + .numerator = 16, + .denominator = 3, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 1, + .schan_val2 = 4, + .expected = "0.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, -1 < scale < 0", + .numerator = -16, + .denominator = 3, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 1, + .schan_val2 = 4, + .expected = "-0.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, 0 < scale < 2", + .numerator = 8, + .denominator = 3, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 1, + .schan_val2 = 1, + .expected = "1.3333333333333333", + }, + { + .name = "small IIO_VAL_FRACTIONAL_LOG2, -2 < scale < 0", + .numerator = -8, + .denominator = 3, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 1, + .schan_val2 = 1, + .expected = "-1.3333333333333333", + }, + { + .name = "small IIO_VAL_INT_PLUS_MICRO, positive", + .numerator = 1, + .denominator = 2, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 5, + .schan_val2 = 1234, + .expected = "2.500617", + }, + { + .name = "small IIO_VAL_INT_PLUS_MICRO, negative", + .numerator = -1, + .denominator = 2, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 5, + .schan_val2 = 1234, + .expected = "-2.500617", + }, + /* + * INT_PLUS_{MICRO,NANO} positive/negative corner cases + */ + { + .name = "negative IIO_VAL_INT_PLUS_NANO, negative schan", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = 123456, + .expected = "-1240.710106203", + }, + { + .name = "negative IIO_VAL_INT_PLUS_NANO, both negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = 123456, + .expected = "1240.710106203", + }, + { + .name = "negative IIO_VAL_INT_PLUS_NANO, 3 negative", + .numerator = -1000000, + .denominator = -8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = 123456, + .expected = "-1240.710106203", + }, + { + .name = "negative IIO_VAL_INT_PLUS_NANO, 4 negative", + .numerator = -1000000, + .denominator = -8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = -123456, + .expected = "-1240.710106203", + }, + { + .name = "negative IIO_VAL_INT_PLUS_NANO, negative, *val = 0", + .numerator = 1, + .denominator = -10, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 0, + .schan_val2 = 123456789, + .expected = "-0.012345678", + }, + /* + * INT_PLUS_{MICRO,NANO} decimal part overflow + */ + { + .name = "decimal overflow IIO_VAL_INT_PLUS_NANO, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456789, + .expected = "1256.01200856", + }, + { + .name = "decimal overflow IIO_VAL_INT_PLUS_NANO, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456789, + .expected = "-1256.01200856", + }, + { + .name = "decimal overflow IIO_VAL_INT_PLUS_NANO, negative schan", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = 123456789, + .expected = "-1256.01200856", + }, + { + .name = "decimal overflow IIO_VAL_INT_PLUS_MICRO, positive", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 123456789, + .expected = "16557.914267", + }, + { + .name = "decimal overflow IIO_VAL_INT_PLUS_MICRO, negative", + .numerator = -1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 123456789, + .expected = "-16557.914267", + }, + { + .name = "decimal overflow IIO_VAL_INT_PLUS_MICRO, negative schan", + .numerator = 1000000, + .denominator = 8060, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = -10, + .schan_val2 = 123456789, + .expected = "-16557.914267", + }, + /* + * 32-bit overflow conditions + */ + { + .name = "overflow IIO_VAL_FRACTIONAL, positive", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = S32_MAX, + .schan_val2 = 1, + .expected = "214748364.7", + }, + { + .name = "overflow IIO_VAL_FRACTIONAL, negative", + .numerator = -2, + .denominator = 20, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = S32_MAX, + .schan_val2 = 1, + .expected = "-214748364.7", + }, + { + .name = "overflow IIO_VAL_FRACTIONAL_LOG2, positive", + .numerator = S32_MAX, + .denominator = 4096, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 4096, + .schan_val2 = 16, + .expected = "32767.99998474121", + }, + { + .name = "overflow IIO_VAL_FRACTIONAL_LOG2, negative", + .numerator = S32_MAX, + .denominator = 4096, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = -4096, + .schan_val2 = 16, + .expected = "-32767.99998474121", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_NANO, positive", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = S32_MAX, + .expected = "1.214748364", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_NANO, negative", + .numerator = -2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = S32_MAX, + .expected = "-1.214748364", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_NANO, negative schan", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = -10, + .schan_val2 = S32_MAX, + .expected = "-1.214748364", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_MICRO, positive", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = S32_MAX, + .expected = "215.748364", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_MICRO, negative", + .numerator = -2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = S32_MAX, + .expected = "-215.748364", + }, + { + .name = "overflow IIO_VAL_INT_PLUS_MICRO, negative schan", + .numerator = 2, + .denominator = 20, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = -10, + .schan_val2 = S32_MAX, + .expected = "-215.748364", + }, +}; + +const struct rescale_tc_data offset_cases[] = { + /* + * Typical use cases + */ + { + .name = "typical IIO_VAL_INT, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 123, + .schan_val2 = 0, + .schan_off = 14, + .expected_off = "24", /* 23.872 */ + }, + { + .name = "typical IIO_VAL_INT, negative", + .offset = -1234, + .schan_scale_type = IIO_VAL_INT, + .schan_val = 12, + .schan_val2 = 0, + .schan_off = 14, + .expected_off = "-88", /* -88.83333333333333 */ + }, + { + .name = "typical IIO_VAL_FRACTIONAL, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 12, + .schan_val2 = 34, + .schan_off = 14, + .expected_off = "3510", /* 3510.333333333333 */ + }, + { + .name = "typical IIO_VAL_FRACTIONAL, negative", + .offset = -1234, + .schan_scale_type = IIO_VAL_FRACTIONAL, + .schan_val = 12, + .schan_val2 = 34, + .schan_off = 14, + .expected_off = "-3482", /* -3482.333333333333 */ + }, + { + .name = "typical IIO_VAL_FRACTIONAL_LOG2, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 12, + .schan_val2 = 16, + .schan_off = 14, + .expected_off = "6739299", /* 6739299.333333333 */ + }, + { + .name = "typical IIO_VAL_FRACTIONAL_LOG2, negative", + .offset = -1234, + .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2, + .schan_val = 12, + .schan_val2 = 16, + .schan_off = 14, + .expected_off = "-6739271", /* -6739271.333333333 */ + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456789, + .schan_off = 14, + .expected_off = "135", /* 135.8951219647469 */ + }, + { + .name = "typical IIO_VAL_INT_PLUS_NANO, negative", + .offset = -1234, + .schan_scale_type = IIO_VAL_INT_PLUS_NANO, + .schan_val = 10, + .schan_val2 = 123456789, + .schan_off = 14, + .expected_off = "-107", /* -107.89512196474689 */ + }, + { + .name = "typical IIO_VAL_INT_PLUS_MICRO, positive", + .offset = 1234, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 123456789, + .schan_off = 14, + .expected_off = "23", /* 23.246438560723952 */ + }, + { + .name = "typical IIO_VAL_INT_PLUS_MICRO, negative", + .offset = -12345, + .schan_scale_type = IIO_VAL_INT_PLUS_MICRO, + .schan_val = 10, + .schan_val2 = 123456789, + .schan_off = 14, + .expected_off = "-78", /* -78.50185091745313 */ + }, +}; + +static void case_to_desc(const struct rescale_tc_data *t, char *desc) +{ + strcpy(desc, t->name); +} + +KUNIT_ARRAY_PARAM(iio_rescale_scale, scale_cases, case_to_desc); +KUNIT_ARRAY_PARAM(iio_rescale_offset, offset_cases, case_to_desc); + +/** + * iio_str_to_nano() - Parse a fixed-point string to get an + * IIO_VAL_INT_PLUS_NANO value + * @str: The string to parse + * @nano: The number as an integer + * + * Returns 0 on success, or a negative error code if the string cound not be + * parsed. + */ +static int iio_str_to_nano(const char *str, s64 *nano) +{ + int fract_mult = 100000000LL; + int tmp, tmp2; + int ret = 0; + + ret = iio_str_to_fixpoint(str, fract_mult, &tmp, &tmp2); + if (ret < 0) + return ret; + + if (tmp < 0) + tmp2 *= -1; + + *nano = (s64)tmp * 10 * fract_mult + tmp2; + + return ret; +} + +/** + * iio_test_relative_error_ppm() - Compute relative error (in parts-per-million) + * between two fixed-point strings + * @real_str: The real value as a string + * @exp_str: The expected value as a string + * + * Returns a negative error code if the strings cound not be parsed, or the + * relative error in parts-per-million. + */ +static int iio_test_relative_error_ppm(const char *real_str, const char *exp_str) +{ + s64 real, exp, err; + int ret; + + ret = iio_str_to_nano(real_str, &real); + if (ret < 0) + return ret; + + ret = iio_str_to_nano(exp_str, &exp); + if (ret < 0) + return ret; + + if (!exp) { + pr_err("Expected value is null, relative error is undefined\n"); + return -EINVAL; + } + + err = 1000000 * abs(exp - real); + err = div64_u64(err, abs(exp)); + return (int)err; +} + +static void iio_rescale_test_scale(struct kunit *test) +{ + struct rescale_tc_data *t = (struct rescale_tc_data *)test->param_value; + char *buff = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); + struct rescale rescale; + int values[2]; + int rel_ppm; + int ret; + + rescale.numerator = t->numerator; + rescale.denominator = t->denominator; + rescale.offset = t->offset; + values[0] = t->schan_val; + values[1] = t->schan_val2; + + ret = rescale_process_scale(&rescale, t->schan_scale_type, + &values[0], &values[1]); + + ret = iio_format_value(buff, ret, 2, values); + KUNIT_EXPECT_EQ(test, (int)strlen(buff), ret); + + rel_ppm = iio_test_relative_error_ppm(buff, t->expected); + KUNIT_EXPECT_GE_MSG(test, rel_ppm, 0, "failed to compute ppm\n"); + + KUNIT_EXPECT_EQ_MSG(test, rel_ppm, 0, + "\t real=%s" + "\texpected=%s\n", + buff, t->expected); +} + +static void iio_rescale_test_offset(struct kunit *test) +{ + struct rescale_tc_data *t = (struct rescale_tc_data *)test->param_value; + char *buff_off = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); + struct rescale rescale; + int values[2]; + int ret; + + rescale.numerator = t->numerator; + rescale.denominator = t->denominator; + rescale.offset = t->offset; + values[0] = t->schan_val; + values[1] = t->schan_val2; + + ret = rescale_process_offset(&rescale, t->schan_scale_type, + t->schan_val, t->schan_val2, t->schan_off, + &values[0], &values[1]); + + ret = iio_format_value(buff_off, ret, 2, values); + KUNIT_EXPECT_EQ(test, (int)strlen(buff_off), ret); + + KUNIT_EXPECT_STREQ(test, strim(buff_off), t->expected_off); +} + +static struct kunit_case iio_rescale_test_cases[] = { + KUNIT_CASE_PARAM(iio_rescale_test_scale, iio_rescale_scale_gen_params), + KUNIT_CASE_PARAM(iio_rescale_test_offset, iio_rescale_offset_gen_params), + {} +}; + +static struct kunit_suite iio_rescale_test_suite = { + .name = "iio-rescale", + .test_cases = iio_rescale_test_cases, +}; +kunit_test_suite(iio_rescale_test_suite); From patchwork Mon Nov 15 03:43:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12618647 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 372A3C4332F for ; Mon, 15 Nov 2021 03:44:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E43661B4C for ; Mon, 15 Nov 2021 03:44:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237044AbhKODrD (ORCPT ); Sun, 14 Nov 2021 22:47:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236977AbhKODqs (ORCPT ); Sun, 14 Nov 2021 22:46:48 -0500 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4696C06120C; Sun, 14 Nov 2021 19:43:52 -0800 (PST) Received: by mail-qv1-xf2f.google.com with SMTP id j9so10442456qvm.10; Sun, 14 Nov 2021 19:43:52 -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=fRYkqMHoPJ/CsqAgxSDIdXX+yarePPpakk34LYl1XTw=; b=UkUzXfzzrtAmRGzcnb0AmXtGmFe6eWA7PLFgZuz6ZE4tkDvhUirUvr4BynfrUHL8D7 eqDst16WxtG3kbgdLX1AFtg6Mt/QoRe1cI4XB7avg2QshLMJb5aKKO+HtmOjF4kcX2cs +obhNnzd2iSj2AMNn8D7lvRRiZ2TMT0ZD7RG6ZG/0fmqDGro0cBxXzIp4dn9F93OcZ2h 4CaI6o96Zx9tDkTogZVtezznIyKHHlzeSgXrppsR8QWL/753ru2I/VGGE6x6dXkkYm0F djpapwUXm9cmkz+r0VChFAQYi+dXcNe8aDO8/hebCsLJtD4EIysxbCLQZC93ls+kanUo DZqw== 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=fRYkqMHoPJ/CsqAgxSDIdXX+yarePPpakk34LYl1XTw=; b=Hv3mLbHdiQ3NzqOShWobrokM2UGqnDJBZEJfTOc7zOVnEcY++MY2ra+9D7XoUrcdYO hgImnUxQ0WSRNbOezMcVqIm3rLiZYTMIhF9Hlz6DfZnVbyRytjemMrHYKK9IXtd/9TRB yqiEVoWwFrnjRyZN3zbKGc9IvhHIT3b37+9NrO6BBy/o0IMvmsiOL34jkOqIq6XGtrOm aQIzjZMGUiPlHkYCZTGmtiJWoiCHaW8gLVicchOep5W059Iq5M+K9sIgZwJLXs4AiDm5 23bXeZImPs8F05LclMBevM1XBsBl/jYV7zfGJPM+becjCMeZ10ZkSxzUcnAAWRbJH2YZ 5jOA== X-Gm-Message-State: AOAM533JF/4F1XNXKfhiGe/To//qToS5DNV2LToqGdiBG2LxEK1T5wd/ t8yb/A/JYUM6JVknRNYRZ3WK9+MbaQE= X-Google-Smtp-Source: ABdhPJy+1F4q1ev5aybd+xTKfud8FVC6wzco1Gd5I4JIXw5zlzGAM1Th+o7u97ar3Tdf25TgM+MgzQ== X-Received: by 2002:a05:6214:f61:: with SMTP id iy1mr33797274qvb.0.1636947831903; Sun, 14 Nov 2021 19:43:51 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:51 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 11/14] iio: afe: rescale: add RTD temperature sensor support Date: Sun, 14 Nov 2021 22:43:31 -0500 Message-Id: <20211115034334.1713050-12-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-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 8a2f1c0ca5a3..ea162ba2a674 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -381,10 +381,52 @@ static int rescale_voltage_divider_props(struct device *dev, return 0; } +static int rescale_temp_sense_rtd_props(struct device *dev, + struct rescale *rescale) +{ + u32 factor; + u32 alpha; + u32 iexc; + u32 tmp; + int ret; + u32 r0; + + ret = device_property_read_u32(dev, "excitation-current-microamp", + &iexc); + if (ret) { + dev_err(dev, "failed to read excitation-current-microamp: %d\n", + ret); + return ret; + } + + ret = device_property_read_u32(dev, "alpha-ppm-per-celsius", &alpha); + if (ret) { + dev_err(dev, "failed to read alpha-ppm-per-celsius: %d\n", + ret); + return ret; + } + + ret = device_property_read_u32(dev, "r-naught-ohms", &r0); + if (ret) { + dev_err(dev, "failed to read r-naught-ohms: %d\n", ret); + return ret; + } + + tmp = r0 * iexc * alpha / 1000000; + factor = gcd(tmp, 1000000); + rescale->numerator = 1000000 / factor; + rescale->denominator = tmp / factor; + + rescale->offset = -1 * ((r0 * iexc) / 1000); + + return 0; +} + enum rescale_variant { CURRENT_SENSE_AMPLIFIER, CURRENT_SENSE_SHUNT, VOLTAGE_DIVIDER, + TEMP_SENSE_RTD, }; static const struct rescale_cfg rescale_cfg[] = { @@ -400,6 +442,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[] = { @@ -409,6 +455,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 Mon Nov 15 03:43: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: 12618649 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4834C433F5 for ; Mon, 15 Nov 2021 03:44:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A31CE60551 for ; Mon, 15 Nov 2021 03:44:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237048AbhKODrG (ORCPT ); Sun, 14 Nov 2021 22:47:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236986AbhKODqt (ORCPT ); Sun, 14 Nov 2021 22:46:49 -0500 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E2C5C061746; Sun, 14 Nov 2021 19:43:53 -0800 (PST) Received: by mail-qv1-xf31.google.com with SMTP id kl8so5962601qvb.3; Sun, 14 Nov 2021 19:43:53 -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=eNlwuNoia1G1oP9SNKRZNXK98CkNYszLFmmmscXJdmM=; b=OITNPXYsDcuBUX8IuIJ+b7KIR5xGSajaUnyA1Rg263I0hw+7GCuVoe++nyhj7x3zIW cHsrsWZi4aMzqfHpGE3XUyhJw8xyrZLfGOi/NC2aZ6qdfQlO4rtCGFnEEIxGV2+Dna3X xELqclOzgVJQ0qjZjskg3IuWoPT1aoYApki2/TiP/ccPplej77D6L2vuuIjbxGB3LXty Ocw0nKwuCSQIS5vzPK20dNDpTEJ0ojxBY2iVDAvVgzIqGULrlZxlehWZfMbGMdBCrNCW Si4YUBwxB4Cl9M4bAisozRrnhqLCK2+TyktS1GstjVcVl5cPup0SEfVBnfyRFM2OyTwy ucfg== 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=eNlwuNoia1G1oP9SNKRZNXK98CkNYszLFmmmscXJdmM=; b=RnD2YLtsV8Gui3puBv3H9EYhRmpjXf6R+yVY77FtTPzvaA96i7lE3XjRYDJOPNpmk/ LJBmiTEKvkJXuGwHbnO30fNFe9HnYp0D4vCSxzewd/7k8ZVR4BRIcnFv1F6g9PbyeUP3 REJ8PFStK5fSYOl4T4TnHbBXskhxanX4dtbWhOD2i+79MRC70U8ttJ2pvt77VGr3nbUX b5x2cJOA+ipVVWdjy4k+e5r4S/tb4etxN7mRN13YHTEpKda5NP6ssrlS5B7XFA3lD6TN gReMccXM0EoUhmTXjjS1CC+4w5gqflkwqz/aXyONPo+bPXtEjMHr+XQk/pXUfwM6QChq 1vrg== X-Gm-Message-State: AOAM532oGeoXVuLKGftOYd8z0tBCSfZzXgOALgRGWYkAvTJgz3D4HI3N /xEZyBKmj/q0K2b+gqMutfo= X-Google-Smtp-Source: ABdhPJyyXigWsaQYbTaSTnsyvO0Xs8zpZ6fiVscxRenbxvf+uBOaFFCq0zsbLXKdmVHeDtuuXnNJXQ== X-Received: by 2002:a05:6214:ace:: with SMTP id g14mr33746763qvi.12.1636947832809; Sun, 14 Nov 2021 19:43:52 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:52 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 12/14] iio: afe: rescale: add temperature transducers Date: Sun, 14 Nov 2021 22:43:32 -0500 Message-Id: <20211115034334.1713050-13-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-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 ea162ba2a674..6bdcf6e139c7 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -422,11 +422,37 @@ static int rescale_temp_sense_rtd_props(struct device *dev, return 0; } +static int rescale_temp_transducer_props(struct device *dev, + struct rescale *rescale) +{ + s32 offset = 0; + s32 sense = 1; + s32 alpha; + int ret; + + device_property_read_u32(dev, "sense-offset-millicelsius", &offset); + device_property_read_u32(dev, "sense-resistor-ohms", &sense); + ret = device_property_read_u32(dev, "alpha-ppm-per-celsius", &alpha); + if (ret) { + dev_err(dev, "failed to read alpha-ppm-per-celsius: %d\n", ret); + return ret; + } + + rescale->numerator = 1000000; + rescale->denominator = alpha * sense; + + rescale->offset = div_s64((s64)offset * rescale->denominator, + rescale->numerator); + + return 0; +} + enum rescale_variant { CURRENT_SENSE_AMPLIFIER, CURRENT_SENSE_SHUNT, VOLTAGE_DIVIDER, TEMP_SENSE_RTD, + TEMP_TRANSDUCER, }; static const struct rescale_cfg rescale_cfg[] = { @@ -446,6 +472,10 @@ static const struct rescale_cfg rescale_cfg[] = { .type = IIO_TEMP, .props = rescale_temp_sense_rtd_props, }, + [TEMP_TRANSDUCER] = { + .type = IIO_TEMP, + .props = rescale_temp_transducer_props, + }, }; static const struct of_device_id rescale_match[] = { @@ -457,6 +487,8 @@ static const struct of_device_id rescale_match[] = { .data = &rescale_cfg[VOLTAGE_DIVIDER], }, { .compatible = "temperature-sense-rtd", .data = &rescale_cfg[TEMP_SENSE_RTD], }, + { .compatible = "temperature-transducer", + .data = &rescale_cfg[TEMP_TRANSDUCER], }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, rescale_match); From patchwork Mon Nov 15 03:43: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: 12618651 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34A45C433F5 for ; Mon, 15 Nov 2021 03:44:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F98E60698 for ; Mon, 15 Nov 2021 03:44:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237036AbhKODrH (ORCPT ); Sun, 14 Nov 2021 22:47:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236993AbhKODqt (ORCPT ); Sun, 14 Nov 2021 22:46:49 -0500 Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A4A6C061766; Sun, 14 Nov 2021 19:43:54 -0800 (PST) Received: by mail-qv1-xf30.google.com with SMTP id b17so10444977qvl.9; Sun, 14 Nov 2021 19:43:54 -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=Ilb3VJUZk+DYaeMxgADmJ12g/nNeM9ZFYvcv8BX2YiE=; b=XPoH5yPj/+OXJskEZMtBuZgywuiLvuvvFxe42EjATQ51LOG4pdmg41D1I5UL6oIDJm KQfwA3lHlRqIyMIOdmUPxVDiUbU++FOAXKg3Pa3j8WgZRLI0nPa7wsm0IZpK6NHL6MY6 G0lOXWceexiuKr51On3wIclYC8xY3ZXsTPyoA4QXKrec+Iz4QrELTTGs/moPlYZ7QzOA 3ibekYzvvdslEwxRuKKhqfbKYpoebY3o208AtAK7IGgoUE+zXEgYE+08K5kbOqMD/TtN PIMZwbpk6vmEw7lfLzJJQH86WfTfI/I7gUdrvptp+V4T0dB6kJ6TxZhe8xv18E0OlYMR upeA== 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=Ilb3VJUZk+DYaeMxgADmJ12g/nNeM9ZFYvcv8BX2YiE=; b=wUePYvolq4TRTCxkpK3JQXKKkAJt8gxAJtGpkWLjVGC3f0cnQIlVEwl6i19eGVVxQW +DfNqqPI3sCj9KO6AdIJw+BCk0f/pIa50yR5PW8Jn/7TsTvWJJNQo7JqR0WTheG/d7A0 AI6DOItDqwdq/AnrwTTFZ2tOOlsCQK/qBS0tvpWmsLd2a4ZuzZ5vUeXBz4wd7EvuP1Cw zhUfMPdoOp/j8PU+WFkM0EeJ7XqiUVDD0edWAB0ha/ERhW0h3IYIX2DsKOEyW+6dWvYD 3fL9ZSYUThRted9U3K2nRFEv7WHpUlcc5XP/ogQdU8JEooPwYsZWruQADWsfML4j2oBn mXyg== X-Gm-Message-State: AOAM5333Zu0Jd1UsH/lR3zlPVkhTxHEFuzB+1wwrutZgqLjFlN2EMfQI PW0dSApw9ibq2L+S0/Drz5g= X-Google-Smtp-Source: ABdhPJyZHiWxxH8H6Y6hr2mnx8x0905N6yB8ZIQQEcBPFvfiZesYtWF/x/1PnV3Rar9X6la1BHNo6w== X-Received: by 2002:a05:6214:627:: with SMTP id a7mr24094436qvx.59.1636947833755; Sun, 14 Nov 2021 19:43:53 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:53 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 13/14] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Date: Sun, 14 Nov 2021 22:43:33 -0500 Message-Id: <20211115034334.1713050-14-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin An ADC is often used to measure other quantities indirectly. This binding describe one case, the measurement of a temperature through the voltage across an RTD resistor such as a PT1000. Signed-off-by: Liam Beguin Reviewed-by: Rob Herring --- .../iio/afe/temperature-sense-rtd.yaml | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml diff --git a/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml b/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml new file mode 100644 index 000000000000..336ce96371db --- /dev/null +++ b/Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml @@ -0,0 +1,101 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/afe/temperature-sense-rtd.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Temperature Sense RTD + +maintainers: + - Liam Beguin + +description: | + RTDs (Resistance Temperature Detectors) are a kind of temperature sensors + used to get a linear voltage to temperature reading within a give range + (usually 0 to 100 degrees Celsius). + + When an io-channel measures the output voltage across an RTD such as a + PT1000, the interesting measurement is almost always the corresponding + temperature, not the voltage output. This binding describes such a circuit. + + The general transfer function here is (using SI units) + + V = R(T) * iexc + R(T) = r0 * (1 + alpha * T) + T = 1 / (alpha * r0 * iexc) * (V - r0 * iexc) + + The following circuit matches what's in the examples section. + + 5V0 + ----- + | + +---+----+ + | R 5k | + +---+----+ + | + V 1mA + | + +---- Vout + | + +---+----+ + | PT1000 | + +---+----+ + | + ----- + GND + +properties: + compatible: + const: temperature-sense-rtd + + io-channels: + maxItems: 1 + description: | + Channel node of a voltage io-channel. + + '#io-channel-cells': + const: 0 + + excitation-current-microamp: + description: The current fed through the RTD sensor. + + alpha-ppm-per-celsius: + description: | + alpha can also be expressed in micro-ohms per ohm Celsius. It's a linear + approximation of the resistance versus temperature relationship + between 0 and 100 degrees Celsius. + + alpha = (R_100 - R_0) / (100 * R_0) + + Where, R_100 is the resistance of the sensor at 100 degrees Celsius, and + R_0 (or r-naught-ohms) is the resistance of the sensor at 0 degrees + Celsius. + + Pure platinum has an alpha of 3925. Industry standards such as IEC60751 + and ASTM E-1137 specify an alpha of 3850. + + r-naught-ohms: + description: | + Resistance of the sensor at 0 degrees Celsius. + Common values are 100 for PT100, 500 for PT500, and 1000 for PT1000 + +additionalProperties: false +required: + - compatible + - io-channels + - excitation-current-microamp + - alpha-ppm-per-celsius + - r-naught-ohms + +examples: + - | + pt1000_1: temperature-sensor0 { + compatible = "temperature-sense-rtd"; + #io-channel-cells = <0>; + io-channels = <&temp_adc1 0>; + + excitation-current-microamp = <1000>; /* i = U/R = 5 / 5000 */ + alpha-ppm-per-celsius = <3908>; + r-naught-ohms = <1000>; + }; +... From patchwork Mon Nov 15 03:43: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: 12618653 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8B66C433EF for ; Mon, 15 Nov 2021 03:44:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9072A61B4D for ; Mon, 15 Nov 2021 03:44:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237063AbhKODrL (ORCPT ); Sun, 14 Nov 2021 22:47:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237005AbhKODqu (ORCPT ); Sun, 14 Nov 2021 22:46:50 -0500 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7491FC061746; Sun, 14 Nov 2021 19:43:55 -0800 (PST) Received: by mail-qv1-xf2b.google.com with SMTP id s9so10445955qvk.12; Sun, 14 Nov 2021 19:43: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=ULLabupwpKZ9q0uzoJvIF5vhkAHSbV+6NawrmKl86jU=; b=YPsPndXsufgMAjwX+0mAHSmIjffN/anUwJ2PTqrKhaKIqnIVPEysGRIwqPGhrxZgrK ikU90sS+216cYhdvp6X5YOIIBqnXbvnLDoJE602hIpoBmyJxXm0RBzJ1wn+vVZQlbrcZ egK/bcvJl2sVwM+cKludIBhTugpcTmhk7hq7KiQt9c5p4obN2k33TDbm70ZhOH7Xwk5n AQ4LZ+DmmrapBPd7sjsn9gnEbFaKPWEj8dWvAPFZi1PA9WMv4wLg8lOhChNehZe3aY8s kB911cNLhE8aNOpO3bXON852akTbEzWGHKWo4l/OcnvBtcuMn2HaIrCbE4AiHq6rj7PA OA3g== 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=ULLabupwpKZ9q0uzoJvIF5vhkAHSbV+6NawrmKl86jU=; b=7vIAHuc+AVxnltVnTSQWDtygYSmAmghWMGK1WA+CR2v35T4pnMP6kEI2ZHMfznJijb Upf9pbe/xzUf/1kJ8AGjoNzjNLsx4Ctp6STYp4cvPgsOVLGVfF2n56sZnuO/SaTYx4Dq P+rSzfF0vDYFU2KSNutWnRp98GTqJbHSxMgVqSBv/xcjYAvtl5lcbphayqNO+76Q/AdG E2vWfqmWeJVwutP7pgl9RuQgdexGh3xnlkTnUZkUyZqb1LXCrsd90qt7rte7E5hBBakO 2jX77krgos+faWxcP9ZaA9J7zUSbpv0B2CuE5aGvSb7kpWqkAhvX9BwIWvlRwPx9gmb4 W0DQ== X-Gm-Message-State: AOAM531PuCXBw//vpVRFiv9bZds/AjzdfwCZnEiZ0xoOgs+pz8cn6tfm W7EVveJyXYxEipWZg2TPhdSZF8FS6vw= X-Google-Smtp-Source: ABdhPJz3CT2phySirIRZCTcHO911TUPPF4oXibyT0E6xf2aW91lBsp6wLHXGreA/GGVzLdk4id+skQ== X-Received: by 2002:a0c:fa07:: with SMTP id q7mr34051205qvn.18.1636947834600; Sun, 14 Nov 2021 19:43:54 -0800 (PST) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id h5sm206519qkn.62.2021.11.14.19.43.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 19:43:54 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v9 14/14] dt-bindings: iio: afe: add bindings for temperature transducers Date: Sun, 14 Nov 2021 22:43:34 -0500 Message-Id: <20211115034334.1713050-15-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20211115034334.1713050-1-liambeguin@gmail.com> References: <20211115034334.1713050-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin An ADC is often used to measure other quantities indirectly. This binding describe one case, the measurement of a temperature through a temperature transducer (either voltage or current). Signed-off-by: Liam Beguin Reviewed-by: Rob Herring --- .../iio/afe/temperature-transducer.yaml | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml diff --git a/Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml b/Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml new file mode 100644 index 000000000000..cfbf5350db27 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml @@ -0,0 +1,114 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/afe/temperature-transducer.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Temperature Transducer + +maintainers: + - Liam Beguin + +description: | + A temperature transducer is a device that converts a thermal quantity + into any other physical quantity. This binding applies to temperature to + voltage (like the LTC2997), and temperature to current (like the AD590) + linear transducers. + In both cases these are assumed to be connected to a voltage ADC. + + When an io-channel measures the output voltage of a temperature analog front + end such as a temperature transducer, the interesting measurement is almost + always the corresponding temperature, not the voltage output. This binding + describes such a circuit. + + The general transfer function here is (using SI units) + V(T) = Rsense * Isense(T) + T = (Isense(T) / alpha) + offset + T = 1 / (Rsense * alpha) * (V + offset * Rsense * alpha) + + When using a temperature to voltage transducer, Rsense is set to 1. + + The following circuits show a temperature to current and a temperature to + voltage transducer that can be used with this binding. + + VCC + ----- + | + +---+---+ + | AD590 | VCC + +---+---+ ----- + | | + V proportional to T +----+----+ + | D+ --+ | + +---- Vout | LTC2997 +--- Vout + | D- --+ | + +---+----+ +---------+ + | Rsense | | + +---+----+ ----- + | GND + ----- + GND + +properties: + compatible: + const: temperature-transducer + + io-channels: + maxItems: 1 + description: | + Channel node of a voltage io-channel. + + '#io-channel-cells': + const: 0 + + sense-offset-millicelsius: + description: | + Temperature offset. + This offset is commonly used to convert from Kelvins to degrees Celsius. + In that case, sense-offset-millicelsius would be set to <(-273150)>. + default: 0 + + sense-resistor-ohms: + description: | + The sense resistor. + By default sense-resistor-ohms cancels out the resistor making the + circuit behave like a temperature transducer. + default: 1 + + alpha-ppm-per-celsius: + description: | + Sometimes referred to as output gain, slope, or temperature coefficient. + + alpha is expressed in parts per million which can be micro-amps per + degrees Celsius or micro-volts per degrees Celsius. The is the main + characteristic of a temperature transducer and should be stated in the + datasheet. + +additionalProperties: false + +required: + - compatible + - io-channels + - alpha-ppm-per-celsius + +examples: + - | + ad950: temperature-sensor-0 { + compatible = "temperature-transducer"; + #io-channel-cells = <0>; + io-channels = <&temp_adc 3>; + + sense-offset-millicelsius = <(-273150)>; /* Kelvin to degrees Celsius */ + sense-resistor-ohms = <8060>; + alpha-ppm-per-celsius = <1>; /* 1 uA/K */ + }; + - | + znq_tmp: temperature-sensor-1 { + compatible = "temperature-transducer"; + #io-channel-cells = <0>; + io-channels = <&temp_adc 2>; + + sense-offset-millicelsius = <(-273150)>; /* Kelvin to degrees Celsius */ + alpha-ppm-per-celsius = <4000>; /* 4 mV/K */ + }; +...