From patchwork Fri Aug 20 19:17:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450309 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBB5EC43216 for ; Fri, 20 Aug 2021 19:17:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BEA0F6112E for ; Fri, 20 Aug 2021 19:17:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236938AbhHTTSd (ORCPT ); Fri, 20 Aug 2021 15:18:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229927AbhHTTSc (ORCPT ); Fri, 20 Aug 2021 15:18:32 -0400 Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39370C061756; Fri, 20 Aug 2021 12:17:54 -0700 (PDT) Received: by mail-qv1-xf35.google.com with SMTP id c14so6057201qvs.9; Fri, 20 Aug 2021 12:17:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zjI68m1FTfjUdZFdXRsH4SrtzTIRTWjQ+J9nVQl3/gM=; b=RMxWScVpPE3neU8juyICJt1HGTKsL72aDk8XFEEMY+WIyOOz27regfjg6Q9MpRk7hV +sgdqSjhlG0/xR1bg6/aOIfQvgNLJqUVIJVv3UzcUsHL98JSkys0qyppd2hgNIaXJnYN SaxmH0owmTFWA+fKz+44QkS1gq/f3bIGwMaDRH464HZ89b92MGOU5OWs0N/XFs2nky+D zj5nNXCLICCsSyowm0rKzAUt++ObXwzS3MAXp+6KJBADki5RrSGIjjjGrHW4kMM/rYG2 SDGm566kzOvBInNpyTQ3B7J5ITtw8q+2+E44W79hWdSUBy/UyvxJDCsJ4fUiXoq34OyO F/Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zjI68m1FTfjUdZFdXRsH4SrtzTIRTWjQ+J9nVQl3/gM=; b=UAZ8ZW8nTQCKCMwmf2Z/sYkyklxpcBdwYYzJ3AxT3IrXosdnNabMverHerjWiE3EB3 rxe/FQLqwSbjs+W6yKCXNF63fMNGzv/fupGAcsW8S4haUXTReT6npA6d6hwmyq/yy8Fw y4f5lFvh3ei2N38pwx8XYJwhHDshWmNTAGcoMfmA1fbWFhWsPwX5443kbfnz3cXryl1D 2ngKjqcMOM+jcbQjwoEtCo68tKdIf94o855yAJuDoYCNi7Ha6y3t+JFprImxVNM+OFAx savDzafO9+sVyZHvym9ZFaETeZNiTVrSNpXTyJkkuLuZhThBKd4IP/S+rQA9roOXuKoV 5g7w== X-Gm-Message-State: AOAM5336nIe3qz1ILUpTp5imQPlwH9S+3VGOSjrGyLhCEFmjrGqmcrJn 4xq+Nmg88Tn5tRh7VOxozJ4= X-Google-Smtp-Source: ABdhPJwwrtH02moIkCR7p+xQpPoIbZMqCGezsi/+cK4G+KMVWtOQd55Mxe7BNTYVOtEVnb3VF6uYZg== X-Received: by 2002:a05:6214:d65:: with SMTP id 5mr21856713qvs.11.1629487073450; Fri, 20 Aug 2021 12:17:53 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.17.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:17:53 -0700 (PDT) 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 v8 01/14] iio: inkern: apply consumer scale on IIO_VAL_INT cases Date: Fri, 20 Aug 2021 15:17:01 -0400 Message-Id: <20210820191714.69898-2-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin When a consumer calls iio_read_channel_processed() and the channel has an integer scale, the scale channel scale is applied and the processed value is returned as expected. On the other hand, if the consumer calls iio_convert_raw_to_processed() the scaling factor requested by the consumer is not applied. This for example causes the consumer to process mV when expecting uV. Make sure to always apply the scaling factor requested by the consumer. Fixes: 48e44ce0f881 ("iio:inkern: Add function to read the processed value") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 391a3380a1d1..b752fe5818e7 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -599,7 +599,7 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, switch (scale_type) { case IIO_VAL_INT: - *processed = raw64 * scale_val; + *processed = raw64 * scale_val * scale; break; case IIO_VAL_INT_PLUS_MICRO: if (scale_val2 < 0) From patchwork Fri Aug 20 19:17:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450307 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABB63C4338F for ; Fri, 20 Aug 2021 19:17:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 94BDB61004 for ; Fri, 20 Aug 2021 19:17:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236949AbhHTTSe (ORCPT ); Fri, 20 Aug 2021 15:18:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236654AbhHTTSd (ORCPT ); Fri, 20 Aug 2021 15:18:33 -0400 Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41113C061575; Fri, 20 Aug 2021 12:17:55 -0700 (PDT) Received: by mail-qv1-xf2a.google.com with SMTP id jv8so6083103qvb.3; Fri, 20 Aug 2021 12:17:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yb6bnKhOc9+gxx5x6SczajCd0sry7xBboXJ9PwvGMsI=; b=sR8bZqMrBNE+TiH5qENySn5XAG7Uaam+WvV2837eME4b5k9UjUOE9Bi/etvlpn1dKW e5EonlIKrFYGv1DS/aeq8TXG4r+ISBswKLsnO+7TRM+Yi1hwuhBTtbjH3rvxZOIj6M8V wVC7ljRx4TzFr3v1DtqZ0GZXnDELCIy3iuShIzdpm/s8aKVd1fO8XCoLFEUejR4ZbU7Z sP3ecnFl94GXozoO7tb3KLl9mK2yHbIYrhqfntx8hCxfrP6Y6Wwow1ls6OcxL+7BVJzg +mbagkmtNVMbjrDtFoDwOwFpGfHvuGtBB4h/dKag4kNLHRsztHTFHNqzlToTOObbfOo3 FtYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yb6bnKhOc9+gxx5x6SczajCd0sry7xBboXJ9PwvGMsI=; b=YQAxF0o7FAhx/eyRcpI8sAyDJxsOVsp57Lyt4LZ1gmHmo6pJC6Crijt71vpvdounE5 fd73VHUOR5UVUG6j/pFMtzabowtRYtZ+zQljtjPWzUC7fdBAmYQvV5yynEySlGDgjL5b KCJHBEkxrPtvnTGjuysHx1Z9wBxDs56Q6EvBvv04KyO71uba7p6qYkNjF+y4YR3Rk+jZ 7t6eUvcaM8qH6YL6A8sawV2++Jbn8HfFE+HpU1dcPt2bbfy6KQMzLJCs+pulLmWTrTby oAe/CJJzo5WdSpP4Us9tj8wx+a9PESnccn02tA60YAkFs6OgptSRn6HwBb9u13S9SApl MwMQ== X-Gm-Message-State: AOAM5338b4U4jV46DqqfwpWrV12uVp6onMDJcR7W2v5/gNTelOBnT5eC +qHydkZAM2pObmPc3z9dYeI= X-Google-Smtp-Source: ABdhPJy5izsCHrKgZpcSGSbSDKOGOu4fv+ERS7q5UOqJtvBoUDkAcXpOa7VSXIlB9U0waFZBcVzo1A== X-Received: by 2002:ad4:438e:: with SMTP id s14mr21716393qvr.26.1629487074532; Fri, 20 Aug 2021 12:17:54 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.17.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:17:53 -0700 (PDT) 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 v8 02/14] iio: inkern: apply consumer scale when no channel scale is available Date: Fri, 20 Aug 2021 15:17:02 -0400 Message-Id: <20210820191714.69898-3-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin When a consumer calls iio_read_channel_processed() and no channel scale is available, it's assumed that the scale is one and the raw value is returned as expected. On the other hand, if the consumer calls iio_convert_raw_to_processed() the scaling factor requested by the consumer is not applied. This for example causes the consumer to process mV when expecting uV. Make sure to always apply the scaling factor requested by the consumer. Fixes: adc8ec5ff183 ("iio: inkern: pass through raw values if no scaling") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index b752fe5818e7..b69027690ed5 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -590,10 +590,10 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, IIO_CHAN_INFO_SCALE); if (scale_type < 0) { /* - * Just pass raw values as processed if no scaling is - * available. + * If no channel scaling is available apply consumer scale to + * raw value and return. */ - *processed = raw; + *processed = raw * scale; return 0; } From patchwork Fri Aug 20 19:17:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450311 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66A8CC4338F for ; Fri, 20 Aug 2021 19:18:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4F31161004 for ; Fri, 20 Aug 2021 19:18:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237503AbhHTTSg (ORCPT ); Fri, 20 Aug 2021 15:18:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229923AbhHTTSe (ORCPT ); Fri, 20 Aug 2021 15:18:34 -0400 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D36EC061575; Fri, 20 Aug 2021 12:17:56 -0700 (PDT) Received: by mail-qv1-xf29.google.com with SMTP id q6so6050735qvs.12; Fri, 20 Aug 2021 12:17:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4cgnObxx9glG9jRFcAKtSHXIEK1QCoxWRUeDTwcnOA8=; b=he3Ft2wQmkNC5qd1X9fkCZBI+yG+NzN6amogJDB3OCtIIErxPq4ENQaoDlQ4S9oZVI Jpf0uVSnewAPBG+BmgJkg48H4UOFa1EiuvHaQWN8OPP7JNh6ENPxkszv+EVAwysqucJG 9qRvVQKO6qO6b9QHM4PgT/+NbX9Hs1jNBjt/a3RYt6FUBeBWgKjyh4AwgAs3AQB3y3+i IkTG56OfIW0aAClU9t3P+WgAp+kJmiWcsi+jF5Anyqbg7orJMwLRXlg8cGQcau2MXS1z /ZZ2Kux1YmvGya9L5qHIYowOGzqCKG6T5RdXd56OK/WKT3CJvPHs+aqO8I8t7jInlNCu s1Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4cgnObxx9glG9jRFcAKtSHXIEK1QCoxWRUeDTwcnOA8=; b=h4FpurwdnOd+OeZLbm+Y5yld+DWWBFcI+VjD11SxRKQ7QoI/q933frvtwu6gfxl9Ih OPXeqJtO9pWAEHmNrp2J0D2OK3S2Q46HUUNPoojzj4pkWjhSEBqDmX9WGltxsfOYh/9k TIi5sS6jErLY+KXrFMCwWgyUc59X8kBMGbTcJESYH7fYRGKudqWFRbjqA6MuywEWNTwb YGRu4sKTP9k4q+8/AywpGUQOfJZDumlCkHi6+WZIVXCh3ESeqvgLH+mEva5xKqgm07ub dx5w+l66pggGGVywoo7+Hvpk32k0XxgGjOxdpM6y2iMCSz5rT3HjrfO1+aAqKrYDKH7e tp8g== X-Gm-Message-State: AOAM531iWwxWPL+8b7YjSMIaRILBpgtISzeDfYDJimY04kqqwxI8qAR1 UBYLuejc8/d3NVXCmnvqoAw= X-Google-Smtp-Source: ABdhPJxBROzBsMLFRtYzUxSOHXMlYCosdKvgqH5yzHxYsm2XB0wkq5TCVzdoYwAWikXd2y+Ct5G2MQ== X-Received: by 2002:a0c:85a1:: with SMTP id o30mr21932403qva.57.1629487075437; Fri, 20 Aug 2021 12:17:55 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.17.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:17:55 -0700 (PDT) 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 v8 03/14] iio: inkern: make a best effort on offset calculation Date: Fri, 20 Aug 2021 15:17:03 -0400 Message-Id: <20210820191714.69898-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin iio_convert_raw_to_processed_unlocked() assumes the offset is an integer. Make a best effort to get a valid offset value for fractional cases without breaking implicit truncations. Fixes: 48e44ce0f881 ("iio:inkern: Add function to read the processed value") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index b69027690ed5..5e74d8983874 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -578,13 +578,35 @@ EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, int raw, int *processed, unsigned int scale) { - int scale_type, scale_val, scale_val2, offset; + int scale_type, scale_val, scale_val2; + int offset_type, offset_val, offset_val2; s64 raw64 = raw; - int ret; - ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); - if (ret >= 0) - raw64 += offset; + offset_type = iio_channel_read(chan, &offset_val, &offset_val2, + IIO_CHAN_INFO_OFFSET); + if (offset_type >= 0) { + switch (offset_type) { + case IIO_VAL_INT: + break; + case IIO_VAL_INT_PLUS_MICRO: + case IIO_VAL_INT_PLUS_NANO: + /* + * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO + * implicitely truncate the offset to it's integer form. + */ + break; + case IIO_VAL_FRACTIONAL: + offset_val /= offset_val2; + break; + case IIO_VAL_FRACTIONAL_LOG2: + offset_val /= (1 << offset_val2); + break; + default: + return -EINVAL; + } + + raw64 += offset_val; + } scale_type = iio_channel_read(chan, &scale_val, &scale_val2, IIO_CHAN_INFO_SCALE); From patchwork Fri Aug 20 19:17:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450313 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3442C432BE for ; Fri, 20 Aug 2021 19:18:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CFD2F61130 for ; Fri, 20 Aug 2021 19:18:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238550AbhHTTSm (ORCPT ); Fri, 20 Aug 2021 15:18:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237182AbhHTTSf (ORCPT ); Fri, 20 Aug 2021 15:18:35 -0400 Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18C5CC061575; Fri, 20 Aug 2021 12:17:57 -0700 (PDT) Received: by mail-qv1-xf30.google.com with SMTP id f7so6064054qvt.8; Fri, 20 Aug 2021 12:17:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UbEAz7/Cn9UDWRCP1DwPP6+dnbZoJceTj6XSahw11p4=; b=XYv927Zim0gEqw3UikaJc9a4bkZATd1rwl5sOil5pmbXur0ohiUO7flmDCt0I8NIh2 KApqTzpkEvqTdLaK09oZVAeY/950B1qOh4w0tx4yM98Gj62jXN3YYxZJsgfYFErOifRZ gXt6+5v0VpLbwnrmzSn7doLX8LVs52ekTL2/r9Kqui5WG5d9iBEaen25CvIRES8ojqMG VbmfsTGRmKe+6iE4t01vyaVUtAitEhPQIUvwrwfYX2bfk98P8CXNM5NOMN5YVwc1GRC7 +VzlGr6yzM9tpe0xB56Ii17uYKCbyGuMxFd/pYLqLn79+pvQHJ9nRGbYc8egOHirak7O K+3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UbEAz7/Cn9UDWRCP1DwPP6+dnbZoJceTj6XSahw11p4=; b=riWmyaPTieOMyWIUbEldtN3qJZbvlQ/XiNDEK8KEQn1Rug2DaOUXSp9hNlX9hZ+zVP alHwtqcKntbD/Ld1NyB4OCT4EMmfd+agOeOIXJgpMR2Y4+inEo8UauV+yJkZppGv20XC hFAF8kKibn9+eZG1wJ5/Xy5Gnn0uIrY+L4yRj3gg/vk/wdBhza9yyb+7X5MRBewdTrLt /O2TpwfVrMH/fxixZLleTiyJ8SoJ5BKaawFPbxH/7ewEbLMPZds/M9A7yAqKNESRmJIi 6+sp0S4FQsUamgB1Zfd2H2uLDPP5VeV0Tlg9UpqIzeuvAO143bxcBnzf40vBRHbYcKsz otMA== X-Gm-Message-State: AOAM530W1N/iKGBgrUK+QCP7Bi56YGR/8RMo/YST0YJKM1A+jJgob8as UBTYvpPtiMFjeRNwf0QS2kY= X-Google-Smtp-Source: ABdhPJzqizUHKUEKZ915iXKdoKInFkk/CNjGZ+KsaApHlkAcLvGANBZrGnLj8Rk7CT5r3Jv/pDhM+Q== X-Received: by 2002:a05:6214:e4e:: with SMTP id o14mr21604222qvc.46.1629487076351; Fri, 20 Aug 2021 12:17:56 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.17.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:17:55 -0700 (PDT) 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 v8 04/14] iio: afe: rescale: expose scale processing function Date: Fri, 20 Aug 2021 15:17:04 -0400 Message-Id: <20210820191714.69898-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-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 Fri Aug 20 19:17:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450315 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79C84C432BE for ; Fri, 20 Aug 2021 19:18:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 64FC46112E for ; Fri, 20 Aug 2021 19:18:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238808AbhHTTSq (ORCPT ); Fri, 20 Aug 2021 15:18:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238277AbhHTTSl (ORCPT ); Fri, 20 Aug 2021 15:18:41 -0400 Received: from mail-qt1-x831.google.com (mail-qt1-x831.google.com [IPv6:2607:f8b0:4864:20::831]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 076A1C0613CF; Fri, 20 Aug 2021 12:17:58 -0700 (PDT) Received: by mail-qt1-x831.google.com with SMTP id u21so5324112qtw.8; Fri, 20 Aug 2021 12:17:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XMRICZfY4ADl2DVliKOzvM085ZlUKV9RB8+CiwFV7h4=; b=t+5nTxWjFzI2lu2FdMy+BNYLCV4KSo75259r877f9nIyabDDD61UzR1MdQ4A3X/nQ+ 3QiCoMyb7SS+0Eqt3ersa4D98AnIh9ECz+NP6jL76WmSBDuKK3oDnSKkPW3qdrOVIiaX XWsMnSEjCsCiACreR4LeIjLAOPe6RTUxiu4tTddywTj1Hbx0/zRAOAaheGspv9KjM3nu B2gocB6sA5sc8KJScH3/EHAPb4bUEUsZHdOpfl1UE+EiT6gSd58CATvoQGJEqBlXoKpc bT2sKgB5CNzw2e4izqKCURJM5Te8vOzVym2p8usmtYuQmdiSDdFopHJIw4ahlCAZ6TnP hQVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XMRICZfY4ADl2DVliKOzvM085ZlUKV9RB8+CiwFV7h4=; b=hw4D+4Hoqw0H62ZKmjo8RyeF4YPkQiQrI2Lz1GyF+tTS5pHjsZEVjiWNH2B7ozJFkR aj/w7udWmWoxsHeRjUUoOkk7WzXZW4iH/5OU6qGx6mf4KDOcr/2brkEIRB5lW2Qdbp3S BVSamVOce95w9tjrDUAeEawHmpNwd+KJWvepnWnzNcPf8YzPrKHj2CJiF8TGz7t5nBvL qe1ERML73VvYzfqkmfh61+D7dtPeD2dKEbxdR+txhceDEMaUgr6d86zq7pda/Ktfe3T1 gGhCPJ1wZk6GfJ8w9s7UvjuPxxSf1FyCr6/6utkZ99BRShndcIeNp0pysN/2EdxhBHTY JgbQ== X-Gm-Message-State: AOAM530lx0qEV1IY7cxd5+LYZfeMj5ePLvnLpodh0Mld3Sl6PVGus46Q JeaeZEF3rLZJHZz//3r6gEg= X-Google-Smtp-Source: ABdhPJxY2csI8jtU7G//w3xPsyByEj3Xe705LcgNd/G71DQdjCLq3w/J9xHNeOK+PGCa+mXPGlOGxw== X-Received: by 2002:ac8:7612:: with SMTP id t18mr19270274qtq.324.1629487077251; Fri, 20 Aug 2021 12:17:57 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.17.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:17:56 -0700 (PDT) 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 v8 05/14] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Fri, 20 Aug 2021 15:17:05 -0400 Message-Id: <20210820191714.69898-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin Some ADCs use IIO_VAL_INT_PLUS_{NANO,MICRO} scale types. Add support for these to allow using the iio-rescaler with them. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index d0669fd8eac5..8488f1d83527 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,38 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, tmp *= rescale->numerator; do_div(tmp, 1000000000LL); *val = tmp; + return scale_type; + case IIO_VAL_INT_PLUS_NANO: + case IIO_VAL_INT_PLUS_MICRO: + if (scale_type == IIO_VAL_INT_PLUS_NANO) + mult = 1000000000LL; + else + mult = 1000000LL; + /* + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if *val OR + * *val2 is negative the schan scale is negative + */ + neg = *val < 0 || *val2 < 0; + + tmp = (s64)abs(*val) * abs(rescale->numerator); + *val = div_s64_rem(tmp, abs(rescale->denominator), &rem); + + tmp = (s64)rem * mult + (s64)abs(*val2) * abs(rescale->numerator); + tmp = div_s64(tmp, abs(rescale->denominator)); + + *val += div_s64_rem(tmp, mult, val2); + + /* + * If only one of the rescaler elements or the schan scale is + * negative, the combined scale is negative. + */ + if (neg ^ ((rescale->numerator < 0) ^ (rescale->denominator < 0))) { + if (*val) + *val = -*val; + else + *val2 = -*val2; + } + return scale_type; default: return -EOPNOTSUPP; From patchwork Fri Aug 20 19:17:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450319 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81942C4320E for ; Fri, 20 Aug 2021 19:18:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 65C6B6112E for ; Fri, 20 Aug 2021 19:18:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238878AbhHTTSq (ORCPT ); Fri, 20 Aug 2021 15:18:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238377AbhHTTSm (ORCPT ); Fri, 20 Aug 2021 15:18:42 -0400 Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1183C0617A8; Fri, 20 Aug 2021 12:17:58 -0700 (PDT) Received: by mail-qt1-x829.google.com with SMTP id d2so8309244qto.6; Fri, 20 Aug 2021 12:17:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ioB7MyFdDko6W35L3sW/WaIL34HomRofFmeSWyl1lGI=; b=UL9y8qsS7dBsQupCho7pQ6w4Iz6lyOytA5TpZjyBEbTw/ndk2cxOVnTPgXuHe4jtul M0X5c8fCRwSOVI8OrByCwKPuv4LYlPFeupU6J4wbio1IdNBHi2YIxaRdk1qdt7PsdWC1 ZF4Z4XvmrMCg72UngFfvGZxvraU93EAJ3UDeGKDqg/I0x7rAhpmNwO86ZJI0uXZjlNO/ RyxlZeIzGvcK3NVhpE8sKTrtsQ8WDkUHYGeOcAcDAAVTO59BvoMn5il8xWmqjqB3aDi9 XUj9rzckHIcLsBmWaZqFLhz2pfQWveekltAEQIn/fMWUB1IPLxsmSWG4c+jb/MdbbUEs qb3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ioB7MyFdDko6W35L3sW/WaIL34HomRofFmeSWyl1lGI=; b=DOToeY3l9D43Qmef9bQX9Mc67OqsnVuqD1pwioAX+JYklpyxlzBMoEPUqE+4uYqab+ Lrr53QhAjKifQkLGkHZBOsWi/YhVLpYAUue0u82kkMyNoDfdn1tdJ+RtZpyPKrYaczcc 5jspEXMbVNIKG60p4KArsV+CjA/dAWTj53cXQ3IJ5Chha5BpnmnDMtnZfMhRBG4HIjHI yncnrGKX0JiIQq+/7F/p1G00fofEaEXkEn84KQT7s6nLJpk0bIsEleAXSmBzuZhoa8kH J54EHCkZb/dTNpBFASwD6FjzLU7KpZxR4fQGkndw1Qr+F46Yrv8qyj0Cr0EROGCHQjfK DvDQ== X-Gm-Message-State: AOAM532F5GsMbAZDZpUbQhxgvLiLrgW2A2ue6xjVPKOvb5W/kaMICyg9 HWJ26xnPl7x3tMYiUsTobsU= X-Google-Smtp-Source: ABdhPJyQjGERmpW8lhJ6SWYktwHp3k/IiN8cu3bTgu8SstvymjPGIxfpQwoJBZqgC38+VCCW924YQg== X-Received: by 2002:ac8:7491:: with SMTP id v17mr4184111qtq.291.1629487078198; Fri, 20 Aug 2021 12:17:58 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.17.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:17:57 -0700 (PDT) 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 v8 06/14] iio: afe: rescale: add offset support Date: Fri, 20 Aug 2021 15:17:06 -0400 Message-Id: <20210820191714.69898-7-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-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 8488f1d83527..48a8096f68ca 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -81,11 +81,46 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, } } +int rescale_process_offset(struct rescale *rescale, int scale_type, + int scale, int scale2, int schan_off, + int *val, int *val2) +{ + s64 tmp, tmp2; + + switch (scale_type) { + case IIO_VAL_FRACTIONAL: + tmp = (s64)rescale->offset * scale2; + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT: + *val = div_s64(rescale->offset, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = (s64)rescale->offset * (1 << scale2); + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_NANO: + tmp = (s64)rescale->offset * 1000000000; + tmp2 = ((s64)scale * 1000000000L) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * 1000000L; + tmp2 = ((s64)scale * 1000000L) + scale2; + *val = div64_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + default: + return -EOPNOTSUPP; + } +} + static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); + int scale, scale2; + int schan_off = 0; int ret; switch (mask) { @@ -112,6 +147,47 @@ static int rescale_read_raw(struct iio_dev *indio_dev, ret = iio_read_channel_scale(rescale->source, val, val2); } return rescale_process_scale(rescale, ret, val, val2); + case IIO_CHAN_INFO_OFFSET: + /* + * Processed channels are scaled 1-to-1 and source offset is + * already taken into account. + * + * In other cases, real world measurement are expressed as: + * + * schan_scale * (raw + schan_offset) + * + * Given that the rescaler parameters are applied recursively: + * + * rescaler_scale * (schan_scale * (raw + schan_offset) + + * rescaler_offset) + * + * Or, + * + * (rescaler_scale * schan_scale) * (raw + + * (schan_offset + rescaler_offset / schan_scale) + * + * Thus, reusing the original expression the parameters exposed + * to userspace are: + * + * scale = schan_scale * rescaler_scale + * offset = schan_offset + rescaler_offset / schan_scale + */ + if (rescale->chan_processed) { + *val = rescale->offset; + return IIO_VAL_INT; + } + + if (iio_channel_has_info(rescale->source->channel, + IIO_CHAN_INFO_OFFSET)) { + ret = iio_read_channel_offset(rescale->source, + &schan_off, NULL); + if (ret != IIO_VAL_INT) + return ret < 0 ? ret : -EOPNOTSUPP; + } + + ret = iio_read_channel_scale(rescale->source, &scale, &scale2); + return rescale_process_offset(rescale, ret, scale, scale2, + schan_off, val, val2); default: return -EINVAL; } @@ -188,6 +264,9 @@ static int rescale_configure_channel(struct device *dev, chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE); + if (rescale->offset) + chan->info_mask_separate |= BIT(IIO_CHAN_INFO_OFFSET); + /* * Using .read_avail() is fringe to begin with and makes no sense * whatsoever for processed channels, so we make sure that this cannot @@ -352,6 +431,7 @@ static int rescale_probe(struct platform_device *pdev) rescale->cfg = of_device_get_match_data(dev); rescale->numerator = 1; rescale->denominator = 1; + rescale->offset = 0; ret = rescale->cfg->props(dev, rescale); if (ret) diff --git a/include/linux/iio/afe/rescale.h b/include/linux/iio/afe/rescale.h index 14d4ee1227c6..b152ac487403 100644 --- a/include/linux/iio/afe/rescale.h +++ b/include/linux/iio/afe/rescale.h @@ -23,8 +23,12 @@ struct rescale { bool chan_processed; s32 numerator; s32 denominator; + s32 offset; }; int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2); +int rescale_process_offset(struct rescale *rescale, int scale_type, + int scale, int scale2, int schan_off, + int *val, int *val2); #endif /* __IIO_RESCALE_H__ */ From patchwork Fri Aug 20 19:17:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450317 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C114C4338F for ; Fri, 20 Aug 2021 19:18:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 04CA461004 for ; Fri, 20 Aug 2021 19:18:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236654AbhHTTSr (ORCPT ); Fri, 20 Aug 2021 15:18:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238427AbhHTTSm (ORCPT ); Fri, 20 Aug 2021 15:18:42 -0400 Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03E8CC0617AD; Fri, 20 Aug 2021 12:18:00 -0700 (PDT) Received: by mail-qk1-x732.google.com with SMTP id t190so11935526qke.7; Fri, 20 Aug 2021 12:17:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=R+zgKUXTnho1DuYnz2N4IQ26TfKFFIYe0e2gWK71jNo=; b=SVDHAG5esKb79+7OfCI/tq1W+FWERAgwfbL8+y1Rig9RZBLXFyO51oAqUpTaQ0iDuH oHnZioX1bwoVAPA5ZxnHt57dsncxgvevEcl/OXDkyXU3iKkqJxf2LCqq+vup/k+5B4XJ BhZdhlDXEu2I3nmBmi9r5nzXPr6+4uBf+F8jmAcoHaqs5ykiAu4kaF85HjNuEukz0T/1 TTCUsWHKzZ0fG0strInWQu3nvTTyiSaYp4HcfleUMlE8nK/L280FAfHTzYA0ny6U3QQD WE6pIzb4SCMzT27CopxB2gRi8XwGJvw2xh7rZeQ6Nnx9t3Dpogx+afAS1GW30e8Ifssd bMgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=R+zgKUXTnho1DuYnz2N4IQ26TfKFFIYe0e2gWK71jNo=; b=hpzbDTZT9M7A2kRj6xjdG+A/adMMUtTBpyupBMenxFrR+04F+fT9ZubpP7eDfPT9Uu EezD4WpJjLq5s1+uATBpB6SlYLyOTsrLFYt9mmkO3BQojeL1knJqx3Rll3tFdCNePlJM fiRtDDR0Ax0SC9nuWdAGNawg5W/TuWqZXJoN2slnHQVfn9J34QTayrFwzAze3TSclE46 RcYtNYc1HcsSre6lz1oCUFJ3WDnPCT0EXWeZsZRjXO3zBI1xoL/PUT3F+YBdLqOnn7Rb EMEKzC6SlBmteVo8gOcNTlNAeVIZJC5EUUvmBckYpgbjKrGEmmENZ59FxwQCfnKGrlcQ rsNw== X-Gm-Message-State: AOAM532+gdz4iFsaHApOtWjDNxFcmozpZ9NKVHte1zFPkOTL7ehRq4jB a+m2x7ofP/u3vZN6yLoK0RI= X-Google-Smtp-Source: ABdhPJzlx/k+lLv0AhZ1tNdWtJNj/r3JQZ7JvAzfQXSFD2IOMvryS3kpiUHd1ZgBsXVNeu+QWSAECQ== X-Received: by 2002:a37:6103:: with SMTP id v3mr10319950qkb.12.1629487079253; Fri, 20 Aug 2021 12:17:59 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.17.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:17:58 -0700 (PDT) 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 v8 07/14] iio: afe: rescale: use s64 for temporary scale calculations Date: Fri, 20 Aug 2021 15:17:07 -0400 Message-Id: <20210820191714.69898-8-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-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 48a8096f68ca..809e966f7058 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 Fri Aug 20 19:17:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450321 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6AA2BC432BE for ; Fri, 20 Aug 2021 19:18:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 581EE61004 for ; Fri, 20 Aug 2021 19:18:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237996AbhHTTSt (ORCPT ); Fri, 20 Aug 2021 15:18:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238661AbhHTTSp (ORCPT ); Fri, 20 Aug 2021 15:18:45 -0400 Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BADAC0613A4; Fri, 20 Aug 2021 12:18:01 -0700 (PDT) Received: by mail-qt1-x836.google.com with SMTP id r21so8267295qtw.11; Fri, 20 Aug 2021 12:18:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Y+d7i6VNQL7/AzXrJYxjwN+0uTqvj42FO5FoBzckvOQ=; b=j00J1cFAFoK82HRUM/wQ9AoggSBeZcRA4T08k3Lgh8Semk3jC/u8WC1MZ9Ebi6jlMv HNBL+wvqMvL3NzZlsCclw5PJ2NCSjbhzjSI948CIVT9cLciSmptZCxgrt3BLC2XS/Vfg uzYEquss1lX462b0s6UFW7Rq73+hjnLIwlxXoHwGt2zCj1S98vtDSFO7zp0JBZdzq5Ui 1EmaU1b+Ky93AzUy2CUOZvpmOEx562sW/FMsp2WxAm8ex1eCIz91MQnMFLiXjSgaDUJX cWkRJEf+NG88USM1OYA1gdoyiHKxNcX93PQtwbOXdg7HtL6iIJnWQ0mJy8ERwvyrabp9 VeBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Y+d7i6VNQL7/AzXrJYxjwN+0uTqvj42FO5FoBzckvOQ=; b=E6D3ws8AFwZ8F1m7REe7ZnzxeCTJkrN7DXoSPuJWd/VNI3I1dx/NhL6YTTKqwprARi xEqdTQGGMqz/0aTy0JQduflHi3nPcjmsTXHRrzDPa3ViRJ+yWKsyhMJrv+/V2IukkdW2 pLf/R1LIeIfcbKRtuA/Fo7Rq3WHJn0KQCiVoN+8tQYojnjMINjrFl+FclAU3zBRb4gYX h/iIccErrv7O/Sz20z6d9H1qLo+zSdsx4zCHSUAvFhtVg6oxKV/TtQTv9R69HXrjnn2U Xz65NJpeiZVfPO106p5MSc9uvMOnFhpcLXSLKFmq2jroJ0luCT0Yxx3qkKSAGEdoYTGX hgXg== X-Gm-Message-State: AOAM532bw4Ziz2rnZ5W7lBnGE4/db1vWaDXgaDRuPrwj+OMPsQs+Ms8l TOs6UUdszADHl1uWOaAVfHU= X-Google-Smtp-Source: ABdhPJxaiOEUnfdcHhv/uvEPJUETGmctk4qghzh+OU5Tx5VYBhAPtYedxKkNdE5zgoTiHjHBmHXyuQ== X-Received: by 2002:ac8:13c8:: with SMTP id i8mr19147787qtj.219.1629487080350; Fri, 20 Aug 2021 12:18:00 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.17.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:17:59 -0700 (PDT) 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 v8 08/14] iio: afe: rescale: reduce risk of integer overflow Date: Fri, 20 Aug 2021 15:17:08 -0400 Message-Id: <20210820191714.69898-9-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-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 809e966f7058..c408c4057c08 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 Fri Aug 20 19:17:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450323 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B170C432BE for ; Fri, 20 Aug 2021 19:18:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6007161004 for ; Fri, 20 Aug 2021 19:18:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239217AbhHTTSx (ORCPT ); Fri, 20 Aug 2021 15:18:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238706AbhHTTSp (ORCPT ); Fri, 20 Aug 2021 15:18:45 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DDBAC06129E; Fri, 20 Aug 2021 12:18:02 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id y144so11942987qkb.6; Fri, 20 Aug 2021 12:18:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YnnFSbZWVuY/lp3GGbUfkpd0TSOBOPS1Fj7ATDd62Jc=; b=I0nxilWJsRfK6KtVkgqY1F+/052IFgYsFi0t+OjNXb5/PwEZFX3DA9Ur4ERAnX8GJo cOO+Quz3JR/+GxzwAf6Nf2lvnLyz7uT5SPVSJEmJuqq0DFXkStJr+VpVOr8XFqTiufiz c601xsY2Bo/RcQYPfL+SnDKZimjgoMTaGE21QnAmHkvR0q/RmP6TU25g9d8twzbawXrz KYz4pYtEDVXu1z4Pgh9xm4npobIhayS9s5gYz/IHdvEe+ya/xKwr64DFB2Mt91/LJtpJ OORaIMUZLZba89uJKWWdvE8qAudL6BvC631/F1LMsqlxpGrt+worD9u/6i5PEfqKVWzf Ypgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YnnFSbZWVuY/lp3GGbUfkpd0TSOBOPS1Fj7ATDd62Jc=; b=Le4kUv0Emvy/yxDkNjdFEWIgDGYMHV14SPn31EtkOX74TrBWjY9kVSHIsUDdo1iLWo iosMnyNgfHCY7LvzItbY46IxiaYnXupp/6iFdLUpnAxZIaa0vKiWqt7s6+REghFU7ZTw jPMLJhb4urQhCZFvL4QjrIJtCqaiRWfz4x1rKCpwKmykYG7k4dhEwCnO9r4lNFvDdygk JCKHkiMZd7Wu/8mjSWnog9/5ZDeX6A3gmDM7ah2ZAUHGdPzgAFAh3S2Ft4T/qlmskMzK AMl4z5N/S1z2OGiKM5kfGSudRbOpPSQCI21WCwM9zDjK0hLEDdb6eGXcU7A4I1fFfCYQ wOhw== X-Gm-Message-State: AOAM532qrbQVaTxCpeSBFswGH74yrjIkF9Jtv8bECZYRbxmw+m2x3hTq pzHYdh2RA35kRyYXLT9fByE= X-Google-Smtp-Source: ABdhPJwxWS8FwzBKx8rfEPSBwNSO4R9/W4j+cQ4I+2f0wsrarpZxDoUQUQ0evZ1PRY5OlXj0LNaNHA== X-Received: by 2002:a05:620a:2045:: with SMTP id d5mr3628969qka.281.1629487081316; Fri, 20 Aug 2021 12:18:01 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:18:00 -0700 (PDT) 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 v8 09/14] iio: afe: rescale: fix accuracy for small fractional scales Date: Fri, 20 Aug 2021 15:17:09 -0400 Message-Id: <20210820191714.69898-10-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-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 Reported-by: kernel test robot Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/iio/afe/iio-rescale.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index c408c4057c08..7304306c9806 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,8 +38,31 @@ 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; + + /* + * For small values, the approximation can be costly, + * change scale type to maintain accuracy. + * + * 100 vs. 10000000 NANO caps the error to about 100 ppm. + */ + if (scale_type == IIO_VAL_FRACTIONAL) + tmp = *val2; + else + tmp = 1 << *val2; + + if (abs(rem) > 10000000 && abs(*val / tmp) < 100) { + *val = div_s64_rem(*val, tmp, &rem2); + + *val2 = div_s64(rem, tmp); + if (rem2) + *val2 += div_s64(rem2 * 1000000000LL, tmp); + + return IIO_VAL_INT_PLUS_NANO; + } + return scale_type; case IIO_VAL_INT_PLUS_NANO: case IIO_VAL_INT_PLUS_MICRO: From patchwork Fri Aug 20 19:17:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450327 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4EF22C4338F for ; Fri, 20 Aug 2021 19:18:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3632861004 for ; Fri, 20 Aug 2021 19:18:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239281AbhHTTSy (ORCPT ); Fri, 20 Aug 2021 15:18:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238717AbhHTTSp (ORCPT ); Fri, 20 Aug 2021 15:18:45 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 390ACC0612A6; Fri, 20 Aug 2021 12:18:03 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id 14so11953629qkc.4; Fri, 20 Aug 2021 12:18:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=85qmLmywqR8GVU2QxUwAruJ7MLlwFKnSqsZHl/rEAeg=; b=NP664eK7JLXwAZlNOP7Y6Oxl7scy8pbR2T0uvzkY7wWuhtsvBHEZlHeVGLrgsd1+fU TL5n9HLXri7T3A01tUVfjf41g50fIrN46Nnxx7uwFDv2fgcc8QOkJIOzbcKhU2yNql1x cebYczSdKwu5SBKzNzKDPO9s81G6YBcJ1dnKOh5oIoqwo3qAQg6wVyYifwyPakrIvHvN ep96GxHcxeLf3K5wVc0YL9erURGkcbZTN+opRuhCMBCvHilQONHtjDsar1rE6pzCAANz 7dNaODsOUDJzjbOvMc8oaJHPSFjpIx0M0JSM7U8zvTcit8bHEmvV4xfbylVXSniZ0ASc MXwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=85qmLmywqR8GVU2QxUwAruJ7MLlwFKnSqsZHl/rEAeg=; b=LvDERDCarzRvRpmxa+sy7eBKcTWMw60Yefi8LFVcCnXfugQu9Sl5IeAeJfP3Cy94rh 8GTdzxRFur3/VVLZLoRbPQe5dpvoLOke27ONou/DXgwwYMM2Vl7GmtF35VATvQ4Cxz8t OR4vAk7SChH8jNuCaEKKQuDtgc4kHb0LNQevC8S6fqDY7L8T80d7jTn3dC6JcfNH8FlT JebPXx9hRwSMxKXo20wvaMHgYyiuc6Qzf7oUAnuGeJ08KkNWaTNDb5yYFyhWRZYt1BDQ pXCKnsmSxmt2IDDTq9178c2PBdd66ZkYT6YVg8Ydak2Cfp81lCmwmM7shqNZCgTuei0h QeZA== X-Gm-Message-State: AOAM531cjkV7ZoHp9JLDaOcEfbr4D51ktyni3n8uscXaMpgtJ5QCeBw/ 41CApYnHRT5p1qxhVKOII5Y= X-Google-Smtp-Source: ABdhPJxZ9XTIZkEKtIQtkhX2WrDyoykd9HFH/Y+eipRMXzZAtkc27CdTZAAGWIlFjC7gVUuBlmxEnA== X-Received: by 2002:a37:c04:: with SMTP id 4mr5610517qkm.283.1629487082365; Fri, 20 Aug 2021 12:18:02 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.18.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:18:01 -0700 (PDT) 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 v8 10/14] iio: test: add basic tests for the iio-rescale driver Date: Fri, 20 Aug 2021 15:17:10 -0400 Message-Id: <20210820191714.69898-11-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin The iio-rescale driver supports various combinations of scale types and offsets. These can often result in large integer multiplications. Make sure these calculations are done right by adding a set of kunit test cases that build on top of iio-test-format. To run these tests, add the following to .kunitconfig $ cat .kunitconfig CONFIG_IIO=y CONFIG_IIO_RESCALE_KUNIT_TEST=y CONFIG_KUNIT=y Then run: $ ./tools/testing/kunit/kunit.py run --kunitconfig .kunitconfig Signed-off-by: Liam Beguin --- drivers/iio/test/Kconfig | 10 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-rescale.c | 637 ++++++++++++++++++++++++++++ 3 files changed, 648 insertions(+) create mode 100644 drivers/iio/test/iio-test-rescale.c diff --git a/drivers/iio/test/Kconfig b/drivers/iio/test/Kconfig index 679a7794af20..872ed4ed140a 100644 --- a/drivers/iio/test/Kconfig +++ b/drivers/iio/test/Kconfig @@ -4,6 +4,16 @@ # # Keep in alphabetical order +config IIO_RESCALE_KUNIT_TEST + bool "Test IIO rescale conversion functions" + depends on KUNIT && !IIO_RESCALE + default KUNIT_ALL_TESTS + help + If you want to run tests on the iio-rescale code say Y here. + + This takes advantage of ARCH=um to run tests and should be used by + developers to tests their changes to the rescaling logic. + config IIO_TEST_FORMAT bool "Test IIO formatting functions" depends on KUNIT=y diff --git a/drivers/iio/test/Makefile b/drivers/iio/test/Makefile index f1099b495301..908963ca0b2f 100644 --- a/drivers/iio/test/Makefile +++ b/drivers/iio/test/Makefile @@ -4,4 +4,5 @@ # # Keep in alphabetical order +obj-$(CONFIG_IIO_RESCALE_KUNIT_TEST) += iio-test-rescale.o ../afe/iio-rescale.o obj-$(CONFIG_IIO_TEST_FORMAT) += iio-test-format.o diff --git a/drivers/iio/test/iio-test-rescale.c b/drivers/iio/test/iio-test-rescale.c new file mode 100644 index 000000000000..68ab5a40e397 --- /dev/null +++ b/drivers/iio/test/iio-test-rescale.c @@ -0,0 +1,637 @@ +// 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, 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, 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_micro() - Parse a fixed-point string to get an + * IIO_VAL_INT_PLUS_MICRO value + * @str: The string to parse + * @micro: 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_micro(const char *str, s64 *micro) +{ + int fract_mult = 100000LL; + 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; + + *micro = (s64)tmp * 10 * fract_mult + tmp2; + + return ret; +} + +/** + * iio_test_relative_error_ppm() - Compute relative error (in ppm) 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 ppm. + */ +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_micro(real_str, &real); + if (ret < 0) + return ret; + + ret = iio_str_to_micro(exp_str, &exp); + if (ret < 0) + return ret; + + 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_LT_MSG(test, rel_ppm, 500, + "\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 Fri Aug 20 19:17:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450325 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E52B9C43214 for ; Fri, 20 Aug 2021 19:18:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D177661004 for ; Fri, 20 Aug 2021 19:18:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239353AbhHTTSy (ORCPT ); Fri, 20 Aug 2021 15:18:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238723AbhHTTSp (ORCPT ); Fri, 20 Aug 2021 15:18:45 -0400 Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F4E5C061575; Fri, 20 Aug 2021 12:18:04 -0700 (PDT) Received: by mail-qk1-x732.google.com with SMTP id 14so11953696qkc.4; Fri, 20 Aug 2021 12:18:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+eLtC7JNuGxTnORcSm7E5kaYd0kc5jriuJPklML6vkM=; b=io06IjKIAKAV/u574Zq+DnR6x/4Cxf/C7zpEFPJEjFo6o0RhuVKBYaY+rTML5h9HfY UcoyrN2K5aIGIBIBcC8/c8Wrtl4Z4ggZPhtHMmz0fjmatLRAWC91FavgDLRFz4eqI8wc Jju53yYDf7y1NDgysa3HrnVG4KWZrriWRz+JbV44F6Zxob8/90y8quazYsSUbyW0WZ10 meNwopX1kXJygTJHNDplPWUSprBXTeGgFOOGPtvNcOu8JsyWMoz/IbQbtYDfkMeX2ngG /I7qoWd6yIOAnQyF2bozwhwdNOHx7SB7MQv4uvGUQB/NUYzlZUPntyHTyTmq4TLNn7Yg VU/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+eLtC7JNuGxTnORcSm7E5kaYd0kc5jriuJPklML6vkM=; b=q25iRXGJ4jQ4vRCdVU/+u7cigAMOGjR4I695uHL5bvSUxs/0JaCH6o24K8iItrHbg0 qM/wvc38CQTlgaKx2ZRiyPxllXZohf5fZ2J4Fqsg/KppPxDPccn0d6hw6wW6PNWlk/RG qHKPYE/FmRfr5RyLBoaCKbGjixQq09zSSepGwKXNEtPPC0CclGWYeFXDt8nt9FuuEUnw qAYEfXbL/kAYZdB4PjSIUa5CfpZxZjsrKGR4RAoBdywwPKKU6rBagTYPb42/VCxrdJ6Y 6UkDtMhV+Zt9XaifWpt62AOAujlmsdOfo9/KIjtFaWGNlIfDZ7lbFdEtBpTeaG69jwDE sPCQ== X-Gm-Message-State: AOAM53306KeJm9rRbqHGrxuOTZZfGyE6ZjhSDkvJyr+nfdO80YSx/gOC y91plRWoitskIHbiPYtOBCXdgWL36dY= X-Google-Smtp-Source: ABdhPJw6y7hFM/TMxDwmnM3oDGB/RhwsbLyUPbfcGi6FXH+fgA70GJaE31sPOnvAT2mgVt64KgE31g== X-Received: by 2002:a37:a60f:: with SMTP id p15mr10120471qke.377.1629487083584; Fri, 20 Aug 2021 12:18:03 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.18.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:18:03 -0700 (PDT) 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 v8 11/14] iio: afe: rescale: add RTD temperature sensor support Date: Fri, 20 Aug 2021 15:17:11 -0400 Message-Id: <20210820191714.69898-12-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-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 7304306c9806..8cdcb6ffb563 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -386,10 +386,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[] = { @@ -405,6 +447,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[] = { @@ -414,6 +460,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 Fri Aug 20 19:17:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450329 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5A46C3F6A3 for ; Fri, 20 Aug 2021 19:18:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CC34061175 for ; Fri, 20 Aug 2021 19:18:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239766AbhHTTS5 (ORCPT ); Fri, 20 Aug 2021 15:18:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238769AbhHTTSq (ORCPT ); Fri, 20 Aug 2021 15:18:46 -0400 Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49CAFC06175F; Fri, 20 Aug 2021 12:18:05 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id d5so8293936qtd.3; Fri, 20 Aug 2021 12:18:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=H+RerQlwryfsOV+KVw2IwUfRQlhiGdN9D5M7AJ5qR8M=; b=BDXiPenjgrU0w3oUGhluoGsjqc743snb0ZgjeOCN3Nrop6MTgDvXorU9104eTpGsjj 1vMGbZD8kIxOFg4V1VlnDoEhoKQuJ0jY+45ftbdcV1fQ4ag/TW9j3XLkXqCQYfQ+f8n+ zj/W4cj7gNTR7djDKjoU/PuVVpdDkQrRRNoUrG1Tbph4fu9B0WxsoGL/C/79d0t55RYb RdrC/q//rJ9HlshycOi67XMjNI841KzPL7uaOnsNFPaJliCmvM6xsc4t1WiqhObLZZ6b e+ISmQXQ4n6YSoKwXnNuBF6/TvjAbpleO8wxbuEWS9MFX4IT/58SaxmNTJ9rlCRxI6Oi XfHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=H+RerQlwryfsOV+KVw2IwUfRQlhiGdN9D5M7AJ5qR8M=; b=j+vbbupMqXfW+vdTaUFxLDg9Gtm7OL8QEOx6Uk/hp0D6mUhI6ZKVn5YyAnCHmBQ46o 8g5zLUEDo8YQ39v0WO+QGnH32QkUugig2Ro1V/8mmEs5iAeGhAcJqRFgJ9L/5T0+MUX4 26bG49iZBRrt1gclVGu4m1AOvueLOUKVsq1qZq3N4J2sIGo/CR4guh+K94reYXiGrn38 O8jArDYOMRbtBi6bwGDbgocmVphNc36qQ8FwrejdbABR5aas3M0NffMs8L6KVBX2o1vL 2H/fZXMJIwB65BcuuFDBY3akh4WX29xKM4lfd/StbilvgPLjFgyfW8q4L34i6kTNm5DF 6b9g== X-Gm-Message-State: AOAM5338huqG3MY3vQp57iJtUMM/V1Whr1KNqgQxj0YqPT3MvpTUz03D DPVy6RCoQJqtrA4dLy5fzkEt9vziEAI= X-Google-Smtp-Source: ABdhPJz5A0/3nZ4wMYzEUrwby4V/lUSgl1ol+y1wIJGlr9i6Sr3e7rDWWy/DqcmS5exOb5Io/bvwfQ== X-Received: by 2002:ac8:5696:: with SMTP id h22mr9269476qta.236.1629487084510; Fri, 20 Aug 2021 12:18:04 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:18:04 -0700 (PDT) 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 v8 12/14] iio: afe: rescale: add temperature transducers Date: Fri, 20 Aug 2021 15:17:12 -0400 Message-Id: <20210820191714.69898-13-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin A temperature transducer is a device that converts a thermal quantity into any other physical quantity. This patch add support for temperature to voltage (like the LTC2997) and temperature to current (like the AD590) linear transducers. In both cases these are assumed to be connected to a voltage ADC. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 8cdcb6ffb563..12de44058bea 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -427,11 +427,38 @@ static int rescale_temp_sense_rtd_props(struct device *dev, return 0; } +static int rescale_temp_transducer_props(struct device *dev, + struct rescale *rescale) +{ + s32 offset = 0; + s32 sense = 1; + s32 alpha; + s64 tmp; + int ret; + + device_property_read_u32(dev, "sense-offset-millicelsius", &offset); + device_property_read_u32(dev, "sense-resistor-ohms", &sense); + ret = device_property_read_u32(dev, "alpha-ppm-per-celsius", &alpha); + if (ret) { + dev_err(dev, "failed to read alpha-ppm-per-celsius: %d\n", ret); + return ret; + } + + rescale->numerator = 1000000; + rescale->denominator = alpha * sense; + + tmp = (s64)offset * (s64)alpha * (s64)sense; + rescale->offset = div_s64(tmp, (s32)1000000); + + return 0; +} + enum rescale_variant { CURRENT_SENSE_AMPLIFIER, CURRENT_SENSE_SHUNT, VOLTAGE_DIVIDER, TEMP_SENSE_RTD, + TEMP_TRANSDUCER, }; static const struct rescale_cfg rescale_cfg[] = { @@ -451,6 +478,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[] = { @@ -462,6 +493,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 Fri Aug 20 19:17:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450331 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59E6FC19F33 for ; Fri, 20 Aug 2021 19:18:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3D63C61004 for ; Fri, 20 Aug 2021 19:18:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239826AbhHTTS5 (ORCPT ); Fri, 20 Aug 2021 15:18:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238779AbhHTTSq (ORCPT ); Fri, 20 Aug 2021 15:18:46 -0400 Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4156BC0612A7; Fri, 20 Aug 2021 12:18:06 -0700 (PDT) Received: by mail-qt1-x835.google.com with SMTP id g11so8296670qtk.5; Fri, 20 Aug 2021 12:18:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ilb3VJUZk+DYaeMxgADmJ12g/nNeM9ZFYvcv8BX2YiE=; b=Apa8StN2iontzQU7Cuy+bB9QQq9qPdJ9eUUs17DPN2YyY1/d68WgBvL36c2mF2Bx5/ xSYhN4/S7y3jOU6VjFXTa4F37NmZ92/U8cRNSMiWzgpkGFllBpMnW+D/C9FoL5appbmV HS7SugYgy/em9RqrVhVGMPC1/c4Ie2Jse74kPslKoWjyuseduaKd/l5a4AvAbbGzyYtS W1cb/fk2PD4gTe603Jm5+WzgsiRqTzAgOK1SmDLt74mRq0zmtVJrEITNLd2K5t5vF+XS /VmsM0xUvcpW0FbMPGzC9cKGkJyCOMwlL9FKhEZx0cmEIPlTDwJPlEmjd+FYcgGgLLqJ u6eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ilb3VJUZk+DYaeMxgADmJ12g/nNeM9ZFYvcv8BX2YiE=; b=d0mouuWIWT8U46UBwdkfkaaSNywtyBDn/E479aQOK0slJn83NPMaN3SfeeWwHAI+HD A+SpClagM9Jxh9gUnxCI91p6WwwBLsoqgzfM0KmtXu0rcUfAMNAjsfwiJb0UtWUZSmkP 8M5Nma3P33QUDYkKcHLpGLKHiv2kqy9y4Fj8Sk1Ko2p5ikr4mxoB1mOo/DPYyytEmCRF CnqATLfIZxIbrPSve/ad5hnzcpKGiCm19scKDhbVJePZQ2YkjzUHkoJTufpGdDhV3exs QYUazGvSF61ImSyUzBlWMNinrTRnjNUqmG+C4iWNBflbMd+4EMy6xWG1o91zg/4jxffZ FaOw== X-Gm-Message-State: AOAM533n2syblPEUZxf2qyxWajnPlZPwz+ampZNqePXDBHSDuPDt99vA /z3UZQjg+k3Oo6OXwSMj6Wo= X-Google-Smtp-Source: ABdhPJxF7WRTH91+grSYbdLGDVqIewqkNzr24oHgWEldJHMXBWjDkmeDVbLXQegbzmWgzb0cLotDMw== X-Received: by 2002:ac8:5194:: with SMTP id c20mr10526537qtn.68.1629487085489; Fri, 20 Aug 2021 12:18:05 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.18.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:18:05 -0700 (PDT) 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 v8 13/14] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Date: Fri, 20 Aug 2021 15:17:13 -0400 Message-Id: <20210820191714.69898-14-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-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 Fri Aug 20 19:17:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12450333 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66887C19F37 for ; Fri, 20 Aug 2021 19:18:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4B8866112E for ; Fri, 20 Aug 2021 19:18:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238784AbhHTTS4 (ORCPT ); Fri, 20 Aug 2021 15:18:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238834AbhHTTSq (ORCPT ); Fri, 20 Aug 2021 15:18:46 -0400 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 42652C061764; Fri, 20 Aug 2021 12:18:07 -0700 (PDT) Received: by mail-qk1-x729.google.com with SMTP id y144so11943241qkb.6; Fri, 20 Aug 2021 12:18:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ULLabupwpKZ9q0uzoJvIF5vhkAHSbV+6NawrmKl86jU=; b=vOxq6guuOESasXLTFsciWuL/8IsC33E6WqMuvMmKpi8j/CaQyQnWvBpiqi73GSmsv7 VLWh8TZ+ZsW79yNPESMEP7aPe6SeLHI5E1g8IDHflKuzxTySRWU80BK+FPWKHgoyaYj2 khtzh29vBCSxG6cWvXdVoGx1ms0Rp2z+WbkejEjXlh8zu1C839hQUy0OdBa8FZi4n/Mc YVIERs0X5xARMytVbUb+qwK2VzRL12ao3MiU3uKitobZeTE/8dDRQL2Tr5A+K4zecNKb 3UEMjbe2JcfI0QBoGPnX+hGIJK9/Mot7kmwzmUd8VGtS60iSt/DyfS0sZxI9bwYtLQ61 ngmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ULLabupwpKZ9q0uzoJvIF5vhkAHSbV+6NawrmKl86jU=; b=mpooq+lOQm6ypyRj41+JyfLHlwZq/ktdkXsgqurhdr/ZSqcWDcsKXb4g/mYzQTF8OV bcUdGOxjulDIxYw7+YFua4iRWAlnW5SidgjE0sgBxE/OO6nbn/oV8Znfqx/mVlVPq6Qq Bx06bhROMClWk6U4WdbozbBPsZaPbYNIvihM5W5FqG9n75t6FvE8I2tfzms4LqHQv43l mWc6FQd46Q0EnnZHDxz3VLR3qQlXgbQWho5Pbn6LmdDDDl4iR4XfinXX5sudwfTtTKcZ EpfkW6PrI9qQgXPnlsSQB7diZyE0DfzEP5GKAkcU+p3aRyL1TEdCnSl7ZiJUbeVVmVxc LsEg== X-Gm-Message-State: AOAM533OkemHeagR6JyjUK4xzqtgvPObsx+WQZeU2rDtf0hflfacxOLg rXOG6Xzy60+cKI40FYbxwlI= X-Google-Smtp-Source: ABdhPJznSPycGbedrlQKmPvJfkRYfeDeksvk/8oXo4aQfgJe/E0SVUmAp5ijmgjXWe9ArQuDXFQ0cg== X-Received: by 2002:a37:6c43:: with SMTP id h64mr10502944qkc.362.1629487086476; Fri, 20 Aug 2021 12:18:06 -0700 (PDT) Received: from shaak.xiphos.ca (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id o6sm3603869qkp.111.2021.08.20.12.18.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 12:18:06 -0700 (PDT) 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 v8 14/14] dt-bindings: iio: afe: add bindings for temperature transducers Date: Fri, 20 Aug 2021 15:17:14 -0400 Message-Id: <20210820191714.69898-15-liambeguin@gmail.com> X-Mailer: git-send-email 2.32.0.452.g940fe202adcb In-Reply-To: <20210820191714.69898-1-liambeguin@gmail.com> References: <20210820191714.69898-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 */ + }; +...