From patchwork Thu Jul 1 01:00:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353013 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CCA82C11F66 for ; Thu, 1 Jul 2021 01:00:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AF35661425 for ; Thu, 1 Jul 2021 01:00:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238252AbhGABDT (ORCPT ); Wed, 30 Jun 2021 21:03:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232066AbhGABDS (ORCPT ); Wed, 30 Jun 2021 21:03:18 -0400 Received: from mail-qv1-xf33.google.com (mail-qv1-xf33.google.com [IPv6:2607:f8b0:4864:20::f33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19268C061756; Wed, 30 Jun 2021 18:00:48 -0700 (PDT) Received: by mail-qv1-xf33.google.com with SMTP id dj3so2127771qvb.11; Wed, 30 Jun 2021 18:00:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aLIjEv3Y3XGxT5C/sRBa0wIugXh2Hwubx4IA7LHkqcs=; b=abeBQ7HDze70AzrZWLTyi/x0HPTwfGhKphNcgOT8r7VAkeLghAo2c5uxwnmmWMLZAY AGd2TI8mW6gja4AjQeqnzq5pDS8fVVUvHXfLvGtTB1MvlNpSKrfLyWdB2FLAbcVQNJ5f ZWyJhcRBaqOWrU6DxLVPOc58Tud1NCer9EjcWTzwnxFGZPrVi4GJhNZSIsYJ2ZQwtVdd 3RDXjKz7oD0nd2iMjWrjuhpxdm7Jhtcab2a40eSzUF1DtqRVVoL1yEI3Xilb4VxfLZ7f uAXvN5RNVJxQ4T3XxQRrjwc6AKY02TmQQ0eojkVwEr0hpmoUgRk9H4q5kIHkvE6/FzRD XyaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aLIjEv3Y3XGxT5C/sRBa0wIugXh2Hwubx4IA7LHkqcs=; b=eumO9YpWvc0qGdP2Vv20ukSKPbYb1zViaecDIEnZItwqdTvDIOB/kVhfkg+nfzgYrF jbisWcr8b+KE3ls7CRMKLqMTCK8ASgQPl63mEn6QeuM5UtCKMBgdNZM2MUqXNaeSHULZ 5bJaiyv/Lh+ga6wdjwfc4Iq+RLpjlclkt0ew/tfzuxbXy2S5tEzAQHZyPCFHEsu6+2wm ugWxGnWSQHXhIM1uvYuo0DhvSbLW7e9od2DWkAaZkcllbYxQFP1v//f2knD/1yhNxXuN 5gwROEKdgDdLVSc2Tw++4pQYe5SETtFtv9kQQ1w6Z/gjnRceithbtLBKij9SC7Zj2Q2s KBQw== X-Gm-Message-State: AOAM533yLVVKqrCK2C7l3SL/ZwlGGoiItW6dpC8qnphDnHpvopGa787D +zje0kdg5Qq1LYzJeieSerk= X-Google-Smtp-Source: ABdhPJwSNrLpc8fuZVizpnNA/xDQxi4rl4wrIH4nvKcCWOhUShh+Y868e9ZGVwNzR8Rad3/CQtQ7yw== X-Received: by 2002:a0c:9e49:: with SMTP id z9mr39578031qve.52.1625101247172; Wed, 30 Jun 2021 18:00:47 -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 l127sm14087782qkc.64.2021.06.30.18.00.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:46 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 01/10] iio: inkern: apply consumer scale on IIO_VAL_INT cases Date: Wed, 30 Jun 2021 21:00:25 -0400 Message-Id: <20210701010034.303088-2-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin When a consumer calls iio_read_channel_processed() and the channel has an integer scale, the scale channel scale is applied and the processed value is returned as expected. On the other hand, if the consumer calls iio_convert_raw_to_processed() the scaling factor requested by the consumer is not applied. This for example causes the consumer to process mV when expecting uV. Make sure to always apply the scaling factor requested by the consumer. Fixes: 48e44ce0f881 ("iio:inkern: Add function to read the processed value") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 391a3380a1d1..b752fe5818e7 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -599,7 +599,7 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, switch (scale_type) { case IIO_VAL_INT: - *processed = raw64 * scale_val; + *processed = raw64 * scale_val * scale; break; case IIO_VAL_INT_PLUS_MICRO: if (scale_val2 < 0) From patchwork Thu Jul 1 01:00:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353017 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA3F9C11F66 for ; Thu, 1 Jul 2021 01:01:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8D648610A1 for ; Thu, 1 Jul 2021 01:01:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238290AbhGABDX (ORCPT ); Wed, 30 Jun 2021 21:03:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238269AbhGABDT (ORCPT ); Wed, 30 Jun 2021 21:03:19 -0400 Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 042A4C0617AD; Wed, 30 Jun 2021 18:00:48 -0700 (PDT) Received: by mail-qt1-x834.google.com with SMTP id g3so2968333qth.11; Wed, 30 Jun 2021 18:00:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OKwbA7CkZ/dV0H/nAmt5zTOn4ixRO23QkLcRozVJn9c=; b=LfLnuFlRknbp8AER1AHlz2WXSFgMGDBWsNUdwGO5iHyddwn2w77t9UZ0zzNDoosZzb yNBgKYpgIIzp53v4Gk4O+1s0EjKgTJUPW12ERtjNX+ATmw7P+pLL3wSYVJPguY516EEK 2vXwMbFHrrVNtcPMX+V9pqarXNTp0q97M9s0OE36uEoJEOG7ciGX5WV+13yMPz7dSKxa uJ116dbZFCqSDdz42HgaMB1n06714E+XpEPkiLf5OqYhbfEQVSPacVEUNvJvoUX4mzZB MJajY874ghSHYXqdxGXwbOV6O3uQv8XnIOMRjV2CHAnFtHQ2BMMQd0QjY+8IB2baTAdI 2axA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OKwbA7CkZ/dV0H/nAmt5zTOn4ixRO23QkLcRozVJn9c=; b=CG+763yq2TIVlS4ED/7RvUp4ZDRH3M0e+vXDL3k7BQSkih9gKUEzfXQoWNMNxJTIWk zj1jUibrluqRkF5YhW9DZLq/xsKxQCRqi5LXgGeK2ieEFdA4NX3kccxFEz68Sp5h2tTD Sxbtk/nORq+ZI52QzkCNeX0WkgZKr45Lsgx/Xb8Z4+P4jV4LjGwEieTB463GWMGVtlV/ uxGI6Uv/PLnJQBf8efb+gOH6h10PNTa94hKk4MZDdltjt8L5gPEgWRp+Vlu9A8XKmiGa qoQtQdIdUCKrD8TjCkdUT3cVYHdx94Ns42px/VdfYFq1eAnSyHoNJmtp+4uBxpEH+yIC hHQg== X-Gm-Message-State: AOAM530WabrzhTSPhUgfFSXCl1VQPpAeQ+f1u39TeYoknT752ZNp6R0c SxsGhGVF9KCnQX850Q3zPNQ= X-Google-Smtp-Source: ABdhPJyiEsuyDAoBTgh8Og8TaRBMtG+qEi+QfEvwQYUza6+sQMgvlbjzxQ5aTMiZF54vDk/6psMn8A== X-Received: by 2002:ac8:4f50:: with SMTP id i16mr13093682qtw.150.1625101248144; Wed, 30 Jun 2021 18:00:48 -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 l127sm14087782qkc.64.2021.06.30.18.00.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:47 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 02/10] iio: inkern: apply consumer scale when no channel scale is available Date: Wed, 30 Jun 2021 21:00:26 -0400 Message-Id: <20210701010034.303088-3-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin When a consumer calls iio_read_channel_processed() and no channel scale is available, it's assumed that the scale is one and the raw value is returned as expected. On the other hand, if the consumer calls iio_convert_raw_to_processed() the scaling factor requested by the consumer is not applied. This for example causes the consumer to process mV when expecting uV. Make sure to always apply the scaling factor requested by the consumer. Fixes: adc8ec5ff183 ("iio: inkern: pass through raw values if no scaling") Signed-off-by: Liam Beguin --- drivers/iio/inkern.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index b752fe5818e7..b69027690ed5 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -590,10 +590,10 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, IIO_CHAN_INFO_SCALE); if (scale_type < 0) { /* - * Just pass raw values as processed if no scaling is - * available. + * If no channel scaling is available apply consumer scale to + * raw value and return. */ - *processed = raw; + *processed = raw * scale; return 0; } From patchwork Thu Jul 1 01:00:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353015 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12E5EC11F68 for ; Thu, 1 Jul 2021 01:00:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E835B610A1 for ; Thu, 1 Jul 2021 01:00:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238279AbhGABDU (ORCPT ); Wed, 30 Jun 2021 21:03:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238270AbhGABDT (ORCPT ); Wed, 30 Jun 2021 21:03:19 -0400 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFF71C061756; Wed, 30 Jun 2021 18:00:49 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id j184so4456504qkd.6; Wed, 30 Jun 2021 18:00:49 -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=8M+IZsg6mC9vNYvrvfewfcCtryWP0CDkqJ0WF7tXO24=; b=DT3O6vY/81RoS2jUjaLvhy7Oc++xRCCbKX/ji56XSR2Yt6Zwo4y3qiR6uWcBY5gCT/ ONCN0LzfnFqNY4pCmsKmSW6CFCivW240jJHwg2sGjxTtVqeqV+xTiPPla8TcHQ3xrvYp jw91zyh5Sd7xY+fjxm+piPmVXmed8SWuEdW25FrCZyHCsplz/3Aymj2to1QJ45rcO1cn weDOMBY4VytU2YZmsE4NJ/IdnEo9cpt097sgC93RFZeNpHT2XwqSEyfdQM5mSXhErwaF YP7mFYeTpdTAvqD2DSjtlv+FTE//ZF5ElilpOTeMsscUk5Qs/p8Hex/Xhqwono5WDh0w 0wEQ== 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=8M+IZsg6mC9vNYvrvfewfcCtryWP0CDkqJ0WF7tXO24=; b=DKk0zhLIECMBvAdb5uxwyCmVff8FbkD/nTLmv/ALsK9pjhd7tx31vwMrh1aFWvtkJG KRXqU5OOiNb+zMx9rO8LPIsIjgayvWgV5JdgREIZDdp1k8cuIR0qSdhh9MorbI7WhtLs J1vSOe5HW1LjmrHO0EbPlhQFkO9pHPy1gjqCJVP9WwuNU0o27rWDxPLrVNzbWEhvKv4I 8AJy/K4AoPmfBdsT95/xDijJylR2uqrwuhZNp+rabVurpbwiQY5XsApzDSz0YLQMxrJS xh0yKuBfiX5OmF2C0CJTRy5r+Y/mFghVcMOT9zWIDEs15BL8NryTcMIPSwIW2FNDLYOU IJSg== X-Gm-Message-State: AOAM532+7hTg/R5kWpnf8yH4eI8yZxtWue+aQZQj4TCB8fxppfnwsC8E VWKVbaNfTXu6V24VEpH+v0A= X-Google-Smtp-Source: ABdhPJzkn6SzdttbtN/lu85HAXmsCUHLOya8f5vC7lHOn3zOa0Z089KC3PG3mHvM/Tz3yY+gyETsJQ== X-Received: by 2002:a05:620a:cd2:: with SMTP id b18mr39228411qkj.221.1625101249082; Wed, 30 Jun 2021 18:00:49 -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 l127sm14087782qkc.64.2021.06.30.18.00.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:48 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 03/10] iio: inkern: make a best effort on offset calculation Date: Wed, 30 Jun 2021 21:00:27 -0400 Message-Id: <20210701010034.303088-4-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-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 | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index b69027690ed5..e1712c1099c5 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -578,13 +578,39 @@ 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; + int tmp; - 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: + fallthrough; + 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: + tmp = offset_val / offset_val2; + offset_val = tmp; + break; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = offset_val / (1 << offset_val2); + offset_val = tmp; + break; + default: + return -EINVAL; + } + + raw64 += offset_val; + } scale_type = iio_channel_read(chan, &scale_val, &scale_val2, IIO_CHAN_INFO_SCALE); From patchwork Thu Jul 1 01:00:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353023 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35D9FC11F6D for ; Thu, 1 Jul 2021 01:01:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1BE366146D for ; Thu, 1 Jul 2021 01:01:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238304AbhGABDZ (ORCPT ); Wed, 30 Jun 2021 21:03:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238283AbhGABDV (ORCPT ); Wed, 30 Jun 2021 21:03:21 -0400 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0DF8C061756; Wed, 30 Jun 2021 18:00:50 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id fi7so1602852qvb.0; Wed, 30 Jun 2021 18:00:50 -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=cqVmsQrj5gPERV46gGlf3in6+hs6JrqcUorTsVE2iLk=; b=GibRhms39S7d1sRjv0TgmrXaJVjb00Q7CiEk+VM0/Zd/YA/esNuRpDgOJpjy+PzhRq Lhkgzt2bnTFa/TPued0PN6p1yQsdK87s1Vq5kNZj1OTGa7lufSqo04vxAraWL0I/zawQ cBRndEMus/9cobFICHcNJhIoOvgNMWATAOWsdHQFqKWTetYJNbkqaI6LJ77lU4EPFeWu vqoDRVc9gFidHx2qGdjwkCE/Tu99fA8BbGdtzTbO7Uyr1c6XiiGSA+nbpylXAnuzYts8 dG+ogQ4hsEUu9rp5CSbLlr0A/HLR4HmbQ1MdND6eT9JLPevilO9h94kvHGHGueRc6+cE 5XaQ== 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=cqVmsQrj5gPERV46gGlf3in6+hs6JrqcUorTsVE2iLk=; b=dLugPW3O2EpEgse9cnauNkvMlkCzEhJYndez/ovsICgD/GzGrBCRmzXYC+GZzLCIEm DrseMtY8wIDcbgpdOcTR3W497wxQ61pkVF7YNKz6YpA+W4MIvka++wFRx9apOlZngdUU OSnKxcQn6xq5FCwiWQ+dCHFMuYX8qLVUtjpZvthGB9M+1ruDNItKyHCrS778w6i/X64E 1yqsBvw+lBkiJTMDZfIoU2pzdfY624EEIfTUhWP495z92y9JqAO4gpNQNlIsYiACRu5W qBeRb9z719OEpsz89MlAcRwtTmRZTL/axWRVb4VahEOit7cgFw1/hTFqlAeCT48S32FK vg0w== X-Gm-Message-State: AOAM532aurHsXmq21IOHvcD+0cJvlY4L8WUTvmBJSGmWY5kqerzMqnOJ ypb6GyOnajiFGQC1LG0dfW0= X-Google-Smtp-Source: ABdhPJyxM9sBJltI9LfkzndGR6gJjfuoc2F+HF7lPS4O651mBthuFMjz7mObZJd9X89lFfdmsY6BGw== X-Received: by 2002:a05:6214:1d24:: with SMTP id f4mr19821789qvd.56.1625101250082; Wed, 30 Jun 2021 18:00:50 -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 l127sm14087782qkc.64.2021.06.30.18.00.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:49 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 04/10] iio: afe: rescale: reduce risk of integer overflow Date: Wed, 30 Jun 2021 21:00:28 -0400 Message-Id: <20210701010034.303088-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin Reduce the risk of integer overflow by doing the scale calculation with 64bit integers and looking for a Greatest Common Divider for both parts of the fractional value. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 774eb3044edd..98bcb5d418d6 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -39,7 +39,8 @@ static int rescale_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); - unsigned long long tmp; + s64 tmp, tmp2; + u32 factor; int ret; switch (mask) { @@ -67,8 +68,11 @@ static int rescale_read_raw(struct iio_dev *indio_dev, } switch (ret) { case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; + tmp = (s64)*val * rescale->numerator; + tmp2 = (s64)*val2 * rescale->denominator; + factor = gcd(tmp, tmp2); + *val = tmp / factor; + *val2 = tmp2 / factor; return ret; case IIO_VAL_INT: *val *= rescale->numerator; From patchwork Thu Jul 1 01:00:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDEE7C47E48 for ; Thu, 1 Jul 2021 01:01:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CFB1F610A1 for ; Thu, 1 Jul 2021 01:01:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238298AbhGABDY (ORCPT ); Wed, 30 Jun 2021 21:03:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238285AbhGABDV (ORCPT ); Wed, 30 Jun 2021 21:03:21 -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 D23C5C0617A8; Wed, 30 Jun 2021 18:00:51 -0700 (PDT) Received: by mail-qv1-xf35.google.com with SMTP id h18so2159020qve.1; Wed, 30 Jun 2021 18:00:51 -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=lmky5l0N9TXo0NTQ1YrbAaaRvC+qKWRHhjy6j7fXuAs=; b=ntCMWnr+mb8qqz6COldcm7/FWBgC1yCjxPE+nUWSbipTWh7Or88qx72riHlmwmnYnP q8JKAhCPnGAr+x+1Xh+/ApFRzS0jWq6O8EusTI59s2XJHNAbPfM+WT0wNwTwlvsVQWiI lPer1AZjPb63rVxMMvqChIm94rNhdKWCqYsRg8lNdMLdeSJv3Tkr2xcgNW2wWZqsN5Ik YktpAsTgq+Hw7GLWrkQFXTowubqzSP+pDWy30siSP5xn7W6VcgwRKZrC7YyjQBgxUEZg wMtbEAaMlyHsA/NMYDNCes1EdcWgQG3Su3Gcvas8Vp2xRhLyf6D9BTuO87nj3qs1zGnM 1wOw== 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=lmky5l0N9TXo0NTQ1YrbAaaRvC+qKWRHhjy6j7fXuAs=; b=DOqSV3yuUWvYRgLc1pP/yh0vIIoA3UVAvH9T7yCGTwJvlj//sBJS4S+Y9IHUPqfc3R kxG/Qv7CNrs1YJqNl0lhfdVbjpINjYTWBTwICqO+ddodYFOnjwrhVfDPgk0TEOn0z66N qKLovv1stZNsZgz7h/ODY4D+36EEjva8a4nT1YvwcdgCjj4F9qehP+SHQKblAR8VU/KM OypVCSnyPPmKzZGu4GEKGiAkm0DO2wmKW3vLnAUAAqY0Zb6bVnRRloiq6+z97Nw5IOuV gcW8EXKS6DIOWerxIxjbj/csZMgbwuiLN4O5lIEEAhv80h51wheP/lVumaFbOOpicZHC 9/WA== X-Gm-Message-State: AOAM533p7XAZm8psaeSXafVeWshMqJoqx3IHSIAAvVs987HKXnVhDUIC 3Tgm127rzE+d14vMI6rbGCE= X-Google-Smtp-Source: ABdhPJxsz7LB5Lxi8wCWyoP5YMPMlkBGDlS+IKSfR2I6DzSP4o+3RcKmYE4c2pyN15Ah43m+ndzscw== X-Received: by 2002:ad4:58d1:: with SMTP id dh17mr35648261qvb.16.1625101251014; Wed, 30 Jun 2021 18:00:51 -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 l127sm14087782qkc.64.2021.06.30.18.00.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:50 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 05/10] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Date: Wed, 30 Jun 2021 21:00:29 -0400 Message-Id: <20210701010034.303088-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin Add IIO_VAL_INT_PLUS_{NANO,MICRO} scaling support. Scale the integer part and the decimal parts individually and keep the original scaling type. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 98bcb5d418d6..8f79c582519c 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -87,7 +87,16 @@ static int rescale_read_raw(struct iio_dev *indio_dev, do_div(tmp, 1000000000LL); *val = tmp; return ret; + case IIO_VAL_INT_PLUS_NANO: + fallthrough; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)*val * rescale->numerator; + *val = div_s64(tmp, rescale->denominator); + tmp = (s64)*val2 * rescale->numerator; + *val2 = div_s64(tmp, rescale->denominator); + return ret; default: + dev_err(&indio_dev->dev, "unsupported type %d\n", ret); return -EOPNOTSUPP; } default: From patchwork Thu Jul 1 01:00:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EA22C11F69 for ; Thu, 1 Jul 2021 01:01:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0051E61424 for ; Thu, 1 Jul 2021 01:01:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238270AbhGABDf (ORCPT ); Wed, 30 Jun 2021 21:03:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232066AbhGABDX (ORCPT ); Wed, 30 Jun 2021 21:03:23 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9CFCC061756; Wed, 30 Jun 2021 18:00:52 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id q16so4438454qke.10; Wed, 30 Jun 2021 18:00:52 -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=816EyhVOKf76HumLoBV1Mqb1SVwEpqWr1yvDp2TFTUA=; b=sfzQfscnADhErtx2+cWwR9oMC4WjegDil8fd2qjxXOQc1s3Lsx9MW0CjZ7x+1sgEev cfummlR85/udX/Hdbat3Plua/nhmv/qMPs8PLNgjCV1b9efWYUEGg6b7mtBE430HVsHV lPcifbfiUgvKqEXI+TqvDBzE8mD0+DpE/zPCugSumDtjT/GQXJNABZeKf2rZCbxDbUsB x666Q1zzs3bLThwEq2SOwGRx+nJoupnbG87Cc4bvV2muRNL8S8o7Dr2MU95mfJGZM89A hhXm7nmKmNbh6rq65IV5msf24AWkhMzAShrg/wWT8CGOaftUiJAtZsj57bOubaywg2Nb RQ5Q== 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=816EyhVOKf76HumLoBV1Mqb1SVwEpqWr1yvDp2TFTUA=; b=IZ4ZOs9yk7iIxzPk1cb8LMKZhp0WdoJ9oIjskbsfPjwPf6I/Ic37zWxosSZTHSLyY+ Go8lJus2LXTJaWuxo/59HX9KedKcBpuQloqSVOl0CrVk5sNgR4M9dkD5iOK23udoC9VD sdl+zx9EAXWBdmiVWP8W/fuxyx+9YUoOoA98RQmZWUvBJr69IOJuqpuRKdJKpAiyAMZw pu7k3nx4+VNwmMo3dP+coo77l8SjDoRYA+C3yu1y831ISEIMnxFHYhmWqJwmbQRdVPTO n6oGZZab6Y3lmW6tY7H8kNeD2S+1cp34QEtqVdisVcF3ops019AUfSg6RDPjC2C+tFwx wM3A== X-Gm-Message-State: AOAM530oqhTDE+FrLN5Q+AdW8GGuj693ufGPb/rE+kHMYvRPI1QiQXfy F8m55tanBB/w/RkqxnR0I3M= X-Google-Smtp-Source: ABdhPJxuCjqJRtqYqYf+Qteteb3wkAPIeVePbauryi4RpigmPWLKoGu/Pv+tvJWVW8ULSaviJENp6g== X-Received: by 2002:a37:44cc:: with SMTP id r195mr39106574qka.478.1625101251990; Wed, 30 Jun 2021 18:00:51 -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 l127sm14087782qkc.64.2021.06.30.18.00.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:51 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 06/10] iio: afe: rescale: add offset support Date: Wed, 30 Jun 2021 21:00:30 -0400 Message-Id: <20210701010034.303088-7-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-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 | 63 +++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 8f79c582519c..c8750286c308 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -32,6 +32,7 @@ struct rescale { bool chan_processed; s32 numerator; s32 denominator; + s32 offset; }; static int rescale_read_raw(struct iio_dev *indio_dev, @@ -39,6 +40,8 @@ static int rescale_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); + int scale, scale2; + int schan_off = 0; s64 tmp, tmp2; u32 factor; int ret; @@ -99,6 +102,62 @@ static int rescale_read_raw(struct iio_dev *indio_dev, dev_err(&indio_dev->dev, "unsupported type %d\n", ret); return -EOPNOTSUPP; } + case IIO_CHAN_INFO_OFFSET: + /* + * Processed channels are scaled 1-to-1 and source offset is + * already taken into account. + * + * In other cases, the final offset value is defined by: + * 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 < 0) + return ret; + else if (ret != IIO_VAL_INT) + return -EOPNOTSUPP; + } + + ret = iio_read_channel_scale(rescale->source, &scale, &scale2); + switch (ret) { + case IIO_VAL_FRACTIONAL: + tmp = (s64)rescale->offset * scale2; + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT: + *val = div_s64(rescale->offset, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = (s64)rescale->offset * (1 << scale2); + *val = div_s64(tmp, scale) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_NANO: + tmp = (s64)rescale->offset * 1000000000UL; + tmp2 = ((s64)scale * 1000000000UL) + scale2; + factor = gcd(tmp, tmp2); + tmp /= factor; + tmp2 /= factor; + *val = div_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + case IIO_VAL_INT_PLUS_MICRO: + tmp = (s64)rescale->offset * 1000000UL; + tmp2 = ((s64)scale * 1000000UL) + scale2; + factor = gcd(tmp, tmp2); + tmp /= factor; + tmp2 /= factor; + *val = div_s64(tmp, tmp2) + schan_off; + return IIO_VAL_INT; + default: + dev_err(&indio_dev->dev, "unsupported type %d\n", ret); + return -EOPNOTSUPP; + } default: return -EINVAL; } @@ -175,6 +234,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 @@ -339,6 +401,7 @@ static int rescale_probe(struct platform_device *pdev) rescale->cfg = of_device_get_match_data(dev); rescale->numerator = 1; rescale->denominator = 1; + rescale->offset = 0; ret = rescale->cfg->props(dev, rescale); if (ret) From patchwork Thu Jul 1 01:00:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0803FC11F76 for ; Thu, 1 Jul 2021 01:01:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E1D6361424 for ; Thu, 1 Jul 2021 01:01:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238334AbhGABDg (ORCPT ); Wed, 30 Jun 2021 21:03:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238294AbhGABDY (ORCPT ); Wed, 30 Jun 2021 21:03:24 -0400 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B57F5C0617A8; Wed, 30 Jun 2021 18:00:53 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id q190so4485118qkd.2; Wed, 30 Jun 2021 18:00:53 -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=ibNMgUV/9imqXNXJIDWyn8fd0s+6qTMjhrOOTvVWpVk=; b=lTPnhrCbhIELR5bNOTMcGyvDB1v2VcGN29BaYKPXIvrY62PdTSGoPlIHijImmBTBQT o1gl7svVS0UBJTHRq42zJIuYNJlZyRzJA15sUFA3+1ZXYb5ZnJ6hNUB+NYonmm3IuE1K xBsZtQOdH4wkf8ClHVzZqL3kMdq/RpCy1+PixiabSAf6UfZL0rDSGlEax+2bB8fBB60i l2qyKj52BHJ1o690lzyxjgtlcY5QYVlJfz864JEGUm7jbHv5QUFkP8XYpPrdx1SvHLi0 hI8i4AyJAs974wEm/kLLaZS91eaj8pddkprW27/cIwz4VjZJvUFYUQ5sIk+wqDD76mPd pSgw== 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=ibNMgUV/9imqXNXJIDWyn8fd0s+6qTMjhrOOTvVWpVk=; b=aiCsbnmlMKaHlPuGe5uw0aOMjCqZfzTfPsVqGpYy7Nby2ZoL1g3klVbLbEsQdGib5O Alupjpro0ROQd6AxgvA9elBcD4SH4mBjiLcBNGaRWR9zq19+goip19gP+4SyaztfjJaf BMvjoKqzMePN6KNpykZojR69M2/htwwyzNsNidMoDG88a4YJY4uSZuTkyyUFmvlzCPdP 7khumLhFKTDKWZb+PKTkt8na9wXsg9LtpzK6zifL5g5ZBmxSjFJlxO6hflF6iavoNAvU gHBVDpbkTBhYQxsUpe0c6vzM4VIYIpZoiL7gnGDIn0BtPrSeW+rVwvD9/5rA2AvaQUOz Tc8A== X-Gm-Message-State: AOAM532JCdGwGeDBA59hx58bo8dkZxZlZqZRuM9SIYaSkuY/D8rCGJjJ RJuKU9Yxis0nymNOGGwjyB4= X-Google-Smtp-Source: ABdhPJza8ZdGVml+mzk6p8ejH3yd2Ovevt0IpUaG5PWgUX3oodI2ehKKS5xj//qHoiuS4obKSqwsBw== X-Received: by 2002:a37:438e:: with SMTP id q136mr39492390qka.382.1625101252908; Wed, 30 Jun 2021 18:00:52 -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 l127sm14087782qkc.64.2021.06.30.18.00.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:52 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 07/10] iio: afe: rescale: add RTD temperature sensor support Date: Wed, 30 Jun 2021 21:00:31 -0400 Message-Id: <20210701010034.303088-8-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-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 c8750286c308..5d7fe8695b68 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -336,10 +336,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[] = { @@ -355,6 +397,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[] = { @@ -364,6 +410,8 @@ static const struct of_device_id rescale_match[] = { .data = &rescale_cfg[CURRENT_SENSE_SHUNT], }, { .compatible = "voltage-divider", .data = &rescale_cfg[VOLTAGE_DIVIDER], }, + { .compatible = "temperature-sense-rtd", + .data = &rescale_cfg[TEMP_SENSE_RTD], }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, rescale_match); From patchwork Thu Jul 1 01:00:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353027 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2FCC3C11F75 for ; Thu, 1 Jul 2021 01:01:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1B0A76146B for ; Thu, 1 Jul 2021 01:01:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238300AbhGABDf (ORCPT ); Wed, 30 Jun 2021 21:03:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238296AbhGABDY (ORCPT ); Wed, 30 Jun 2021 21:03:24 -0400 Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4F40C061756; Wed, 30 Jun 2021 18:00:54 -0700 (PDT) Received: by mail-qt1-x830.google.com with SMTP id y9so2967717qtx.9; Wed, 30 Jun 2021 18:00: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=Coq9W6UR2KbpR7gTQ+r9if/M5xvmwZcH5jkSQhQdKvw=; b=BlETA7j4OINrnTw7yCwkrqh4AWUihvJ71rhID+ROps8XbNfSLxfJ4LyTGgK69UlbmU JwohJgh4FgMdrxeRmSYhJ+eHHVAB6+uIjm24O6JVftxSulDQeqy4iqE4u8YsYHlZZ+Ey cBQsC/41ZberKPN1kxxqIFo9GZe0FJDxJc8/F4HIvVXskCogCyezL8+AuPdCnpFHfBR2 Su1hLxlz1Rc2CgtR7XGkvCiqsUlPfjrU6nmLUoGC5J8JSK5CHH4X3FRO+P5ffGBrhCb8 sBGWyz7n/e2XSadYsgCKjHQCc2tirpQD7AkdFn5v5/qOBbuxcKqZAMfHJj094XoivU21 zZbw== 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=Coq9W6UR2KbpR7gTQ+r9if/M5xvmwZcH5jkSQhQdKvw=; b=T1NhQCZdn4CC5E209ezvdKKiNsguKcJY7AuIB+gMJ8yS8cX+674n6128TB1aB1ZOIw pC8Bj5swLJwK82iWdG5Rkh/xHcppqzUeqBtBWCccGavUi5zBzL9bqJ1MCwgnKcxRF7nj 9/tC7H5K0jYh8e1/Wy5b8WfjQW47xTkxraykKAZeMukoDG11hnOYoXkc4GeeNSWunkXx GpbH0/he3kKBr5pDg6a7X0KTH5qnbgQNTZTN6vjgIxkPQ4Dnnp1GlbHWm+b15p0pwuMT idWzIWpRd6ZJwcKf5ninz/KFLUC6m6pj7Ff4LqZqEfNyZaxP6DROWpeiwnpofAfK3IVf 9Oyw== X-Gm-Message-State: AOAM531aAiiW52QvmI99EN4pqDaJ675UAFbbQ9kJLm1yv7QmSGNCMhAr ZVuTurdRQrhcGzLhYsG4idk= X-Google-Smtp-Source: ABdhPJxRC7JQW3cJoaRlxtkG2ljUJYhhPo3CYhBnGjizObq5MSZzOPl44GAOcZpnpNxHQ8CgEGuY/w== X-Received: by 2002:ac8:549:: with SMTP id c9mr34582099qth.80.1625101254118; Wed, 30 Jun 2021 18:00: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 l127sm14087782qkc.64.2021.06.30.18.00.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:53 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 08/10] iio: afe: rescale: add temperature transducers Date: Wed, 30 Jun 2021 21:00:32 -0400 Message-Id: <20210701010034.303088-9-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-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 5d7fe8695b68..e820e9f37a7d 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -377,11 +377,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[] = { @@ -401,6 +428,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[] = { @@ -412,6 +443,8 @@ static const struct of_device_id rescale_match[] = { .data = &rescale_cfg[VOLTAGE_DIVIDER], }, { .compatible = "temperature-sense-rtd", .data = &rescale_cfg[TEMP_SENSE_RTD], }, + { .compatible = "temperature-transducer", + .data = &rescale_cfg[TEMP_TRANSDUCER], }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, rescale_match); From patchwork Thu Jul 1 01:00:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353031 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E46F1C11F68 for ; Thu, 1 Jul 2021 01:01:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C85F161425 for ; Thu, 1 Jul 2021 01:01:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238362AbhGABDh (ORCPT ); Wed, 30 Jun 2021 21:03:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238317AbhGABD0 (ORCPT ); Wed, 30 Jun 2021 21:03:26 -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 D242DC0617A8; Wed, 30 Jun 2021 18:00:55 -0700 (PDT) Received: by mail-qt1-x836.google.com with SMTP id f13so2985169qtb.6; Wed, 30 Jun 2021 18:00: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=UuHr7m+N9MiuddW4ua5pFfmrMFxnaeCwG/Hr0n7vYAs=; b=Ht3rlYP4hKjOtlhb1rDlxaiWGVQJN5YExnDSZfiLiwYW2U9XBoolmpJOPXb9m3P3l6 3u0He6Gc6U4yJVuPNIXTGg4z6DIJr+WmVFXypk1zxFCNPNCgbWhtMidZuSxqndiOVhAY MOH8xnoumgjVuh3RNo/tNWQUXKb8MUmQyXn4/5loEgmnp2BDivGZ9Mm5ByGa9qYZaXh7 7JA84cM9RUVJAP4vAFm4+IbpatiDsKlpe3vsB7yTNEdwYZ0jwHHU7aAPnqIIZUWzAEwz IzWy0dxfSzF372rlx9NdBD8StgGJbQyiO/RoSmY4bnABKqGIKD9ZsqNTupXzgmJJB2ou SChw== 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=UuHr7m+N9MiuddW4ua5pFfmrMFxnaeCwG/Hr0n7vYAs=; b=aNz8cEqKl0lozhuL0j9FgRxLaUfEouUnSIbnu0xk+XeRh7Ac3ly1MutRfptYRUQmaR 3XxwV23U2hG93wxbWAw0c8/ZPf6I1qICCyej3J1dyvnxGvyEf0ZzJvE+YcLHtuhd/7OG 4idCWlvBY7ksMbQyoLlQStaPU55NqAtcUiYvNmbJGy4kPzkIEjbU2Ya8OZ8Z0TWyQDEJ n7YXCHkXH4VMbbBS9FHcTcvlWBUu+TCDnoZ54ivx3QBZzreIScYY84kuIf55qtlD2ofL /4UeTF7piPcn4+Xv1ETQQ2aZ6sm4msC9K2d2xiouPl85ckakaNde2ZlA2dcy4fT4PmS/ PtEA== X-Gm-Message-State: AOAM533WCr8xvWOk6nospaCGVlkGqDPvGV1rrsDM24mPRtL8M6eFdLVd pT8TR5wAEn2ZxNwrwCs8gnE= X-Google-Smtp-Source: ABdhPJxoEsfZ6DQeMJhoyQf/hrbMEKvzWPFjQ3Od+PbcvBVZEze+pyUpIAhBLfaSpxuICie0Tq0p8w== X-Received: by 2002:ac8:51c3:: with SMTP id d3mr19923467qtn.320.1625101255036; Wed, 30 Jun 2021 18:00: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 l127sm14087782qkc.64.2021.06.30.18.00.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:54 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 09/10] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Date: Wed, 30 Jun 2021 21:00:33 -0400 Message-Id: <20210701010034.303088-10-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-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 --- .../iio/afe/temperature-sense-rtd.yaml | 101 ++++++++++++++++++ MAINTAINERS | 7 ++ 2 files changed, 108 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..e23e74e63ec5 --- /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: 1 + + 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 = <1>; + io-channels = <&temp_adc1 0>; + + excitation-current-microamp = <1000>; /* i = U/R = 5 / 5000 */ + alpha-ppm-per-celsius = <3908>; + r-naught-ohms = <1000>; + }; +... diff --git a/MAINTAINERS b/MAINTAINERS index 9bf553e53f0f..ed80e671a16a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8897,6 +8897,13 @@ F: Documentation/devicetree/bindings/iio/afe/current-sense-shunt.yaml F: Documentation/devicetree/bindings/iio/afe/voltage-divider.yaml F: drivers/iio/afe/iio-rescale.c +IIO UNIT CONVERTER (TEMPERATURE) +M: Liam Beguin +R: Peter Rosin +L: linux-iio@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml + IKANOS/ADI EAGLE ADSL USB DRIVER M: Matthieu Castet M: Stanislaw Gruszka From patchwork Thu Jul 1 01:00:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Beguin X-Patchwork-Id: 12353033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA82BC11F7B for ; Thu, 1 Jul 2021 01:01:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AC80F61493 for ; Thu, 1 Jul 2021 01:01:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238367AbhGABDi (ORCPT ); Wed, 30 Jun 2021 21:03:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238314AbhGABD0 (ORCPT ); Wed, 30 Jun 2021 21:03:26 -0400 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6DB7C0617AF; Wed, 30 Jun 2021 18:00:56 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id 19so4418336qky.13; Wed, 30 Jun 2021 18:00: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=uu5hiFy+Aqn229khLLdQXnhG+QuibP+/oNp4fvlu80s=; b=Agrx/33QED8t5YMdAFlBRvEKSuezKgi1c4h3+T7tSuOS7GeQic+51sqTznYG40Nqfz XpVVxEFDmsQPgMyiYhvQflBxjuG2im4DzOl7ivvJA7yKznAJ80wy5rvVAzdaHf9wHWLA dB0Oi0PnHyoQYTmBfQssKeg1ubJ6nHjiiBGdMD7hKBNc1dgBvjOag9ymR5cZ27zLNnFx n1HDfwNvo8dzO51pSbGs7xDRFb11M4fvqn4YMM72jDZxxf3rHEX8Zhc7BKRZ1BKO0k71 W6ZIe6XpUDIGj6z7EmgjxaAxoyUiP7tXhx+zDCPC2yDJFI0PCYToSkjx0/+4pm0ab+m3 ncFg== 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=uu5hiFy+Aqn229khLLdQXnhG+QuibP+/oNp4fvlu80s=; b=aOKkl07pxZMA7Fdrqv2mtMStISBquLa35sTATAy5ZP3Yq6HYkZM+DfYm3aQ8EgQIby htLWlLKegpko1BUYWMxc+3shkyzAkO/5Y2pJkaadobUt7PSRq3n0ZYyOnWNNU0/17ugo GAcY08mg3EjSKHmKEQGxZE0RsO7qzUXh+q7o8OQKaCjAAM61ps+OkIZUUeVCh9YX5qrG j8uN5dbsnEfNpeXqiiR1VE8TzCTVXGs3bMdTZlb/ueie9MBde+DENR0kxLSP92enk4yi WsZw+bRH1Ohe0yadhpNKBKcZaitOZA9b7BQFdx7joxKNZj8a5MhPNUM6us5eOBjW7Jm3 kEmw== X-Gm-Message-State: AOAM530nQzcpc75Zg/xeOmhGf8mWvhXWdwZGulgpjMwgeiZrsYQ6QwtO YpZMbTvA9mdwGAoO1UZFXAU= X-Google-Smtp-Source: ABdhPJxD7NrbMr1RerXra44zha5hJ8pA4lYyChEgFHQ+tx49AU6pHMD1C1XuPHP5rn30HDqGjObwrQ== X-Received: by 2002:ae9:f305:: with SMTP id p5mr22002272qkg.68.1625101256014; Wed, 30 Jun 2021 18:00: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 l127sm14087782qkc.64.2021.06.30.18.00.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 18:00:55 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v3 10/10] dt-bindings: iio: afe: add bindings for temperature transducers Date: Wed, 30 Jun 2021 21:00:34 -0400 Message-Id: <20210701010034.303088-11-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210701010034.303088-1-liambeguin@gmail.com> References: <20210701010034.303088-1-liambeguin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Liam Beguin An ADC is often used to measure other quantities indirectly. This binding describe one case, the measurement of a temperature through a temperature transducer (either voltage or current). Signed-off-by: Liam Beguin --- .../iio/afe/temperature-transducer.yaml | 111 ++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 112 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..d16c0ade2271 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml @@ -0,0 +1,111 @@ +# 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: 1 + + sense-offset-millicelsius: + description: | + Temperature offset. The default is <0>. + This offset is commonly used to convert from Kelvins to degrees Celsius. + In that case, sense-offset-millicelsius would be set to <(-273150)>. + + sense-resistor-ohms: + description: | + The sense resistor. Defaults to <1>. + Set sense-resistor-ohms to <1> when using a temperature to voltage + transducer. + + 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 = <1>; + 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 = <1>; + io-channels = <&temp_adc 2>; + + sense-offset-millicelsius = <(-273150)>; /* Kelvin to degrees Celsius */ + alpha-ppm-per-celsius = <4000>; /* 4 mV/K */ + }; +... diff --git a/MAINTAINERS b/MAINTAINERS index ed80e671a16a..e9e11c3ea4e3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8903,6 +8903,7 @@ R: Peter Rosin L: linux-iio@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml +F: Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml IKANOS/ADI EAGLE ADSL USB DRIVER M: Matthieu Castet