From patchwork Mon Mar 8 10:02:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 12121873 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=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 F15F6C433E6 for ; Mon, 8 Mar 2021 10:03:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A616964E92 for ; Mon, 8 Mar 2021 10:03:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231464AbhCHKCa (ORCPT ); Mon, 8 Mar 2021 05:02:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231643AbhCHKCa (ORCPT ); Mon, 8 Mar 2021 05:02:30 -0500 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6022C06174A for ; Mon, 8 Mar 2021 02:02:29 -0800 (PST) Received: by mail-lf1-x12f.google.com with SMTP id r3so11903710lfc.13 for ; Mon, 08 Mar 2021 02:02:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ywGXmbO4ZKXPKOBwo/VXhEMMza2MItTJHpVMCLgWzHA=; b=uvse9CuNLuA4stytN0lktycADnkVRupsZWHElb/Fv1IUS7wn2l02KhqV4ziMKnyP/P icxN/oYf/DjHn1xYpzNJWtUrCiuvsl647yM7SwDIcDGBXHTaoZnfppvovckTaYQ71OBr eXjRW/juaeUAR74xKdINk6djQsykuyTwnI8qlWHDxnLHweuLLok/YrM103uqat1E0OPD hIQLEMGzQ1d4mr8TJjVi8uj0sS/TJmuI9aOb9mMpdSkhmNJxKoCUQ1H5a4bmAMOZdQOF Kntqmm2jyEUhqseFDuEEyPcaZLa4guU7yGY3BoxqhzbLDVpcm1ZnSS6PEQ9e+Maub6vc fzVQ== 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:mime-version :content-transfer-encoding; bh=ywGXmbO4ZKXPKOBwo/VXhEMMza2MItTJHpVMCLgWzHA=; b=QTuJs7GppxOynIEm/Woo8qcIfhIax49IZgQ+MeJN/f3hlMaY5FbkAiD5Of4KUfMJlK B0iK0+V3htqnXhN1oZu2nr0XBopF79Xig2PNbQWkxhTQXuUEE8/rR0bqo8qD9XP2t7j4 NzzdEh6O2T51RZfN/6IR2ZfyLmyYfGK8PPhL9cxqp9dmihZQUIGRb8BFTHx/tqEZekc9 hEVix/4F8QDn/8Wma2Z95mCC3QOTYm8BjpSpjQDaThy54QKNDaB+OF77jF+yqia6FJEP oR4eo2xn8USFy0QGwmisaeDmYlOkd2L03kho/Abn3K1FEDZmFIEoAp7z18koBNZ66ULO PrRg== X-Gm-Message-State: AOAM532bKBQC1QVRxdHN8vGaqkTmToq3mXkIX9p++l2vGZeJp6pmPm6U 29XnLwJL+Dbs9Sofd4L2N3s+JQ== X-Google-Smtp-Source: ABdhPJxpcQ1cJ+xMZWapyn3H6YQ4+LSsev0FuM4ni8mrS+/uh51GnhMg/z/YgRGsUdxxP9ub+hdDLg== X-Received: by 2002:a19:48ca:: with SMTP id v193mr14601914lfa.323.1615197748405; Mon, 08 Mar 2021 02:02:28 -0800 (PST) Received: from localhost.localdomain (c-d7cb225c.014-348-6c756e10.bbcust.telenor.se. [92.34.203.215]) by smtp.gmail.com with ESMTPSA id v2sm1425734ljg.89.2021.03.08.02.02.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Mar 2021 02:02:28 -0800 (PST) From: Linus Walleij To: Jean Delvare , Guenter Roeck , Jonathan Cameron Cc: linux-hwmon@vger.kernel.org, Linus Walleij , Peter Rosin , Chris Lesiak , linux-iio@vger.kernel.org Subject: [PATCH 1/2 v4] iio: Provide iio_read_channel_processed_scale() API Date: Mon, 8 Mar 2021 11:02:18 +0100 Message-Id: <20210308100219.2732156-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Since the old iio_read_channel_processed() would lose precision if we fall back to reading raw and scaling, we introduce a new API that will pass in a scale factor when reading a processed channel: iio_read_channel_processed_scale(). Refactor iio_read_channel_processed() as a special case with scale factor 1. Cc: Peter Rosin Cc: Chris Lesiak Cc: Jonathan Cameron Cc: linux-iio@vger.kernel.org Link: https://lore.kernel.org/linux-iio/20201224011607.1059534-1-linus.walleij@linaro.org/ Signed-off-by: Linus Walleij --- ChangeLog v3->v4: - Split off as separate API-introducing patch. - Fix a goto err when scaling. - My suggestion is to apply this patch with Jonathan's ACK to the hwmon tree. --- drivers/iio/inkern.c | 16 ++++++++++++++-- include/linux/iio/consumer.h | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index db77a2d4a56b..c61fc06f98b8 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -688,7 +688,8 @@ int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2) } EXPORT_SYMBOL_GPL(iio_read_channel_offset); -int iio_read_channel_processed(struct iio_channel *chan, int *val) +int iio_read_channel_processed_scale(struct iio_channel *chan, int *val, + unsigned int scale) { int ret; @@ -701,11 +702,15 @@ int iio_read_channel_processed(struct iio_channel *chan, int *val) if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_PROCESSED); + if (ret) + goto err_unlock; + *val *= scale; } else { ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); if (ret < 0) goto err_unlock; - ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1); + ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, + scale); } err_unlock: @@ -713,6 +718,13 @@ int iio_read_channel_processed(struct iio_channel *chan, int *val) return ret; } +EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale); + +int iio_read_channel_processed(struct iio_channel *chan, int *val) +{ + /* This is just a special case with scale factor 1 */ + return iio_read_channel_processed_scale(chan, val, 1); +} EXPORT_SYMBOL_GPL(iio_read_channel_processed); int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2) diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h index 0a90ba8fa1bb..5fa5957586cf 100644 --- a/include/linux/iio/consumer.h +++ b/include/linux/iio/consumer.h @@ -241,6 +241,21 @@ int iio_read_channel_average_raw(struct iio_channel *chan, int *val); */ int iio_read_channel_processed(struct iio_channel *chan, int *val); +/** + * iio_read_channel_processed_scale() - read and scale a processed value + * @chan: The channel being queried. + * @val: Value read back. + * @scale: Scale factor to apply during the conversion + * + * Returns an error code or 0. + * + * This function will read a processed value from a channel. This will work + * like @iio_read_channel_processed() but also scale with an additional + * scale factor while attempting to minimize any precision loss. + */ +int iio_read_channel_processed_scale(struct iio_channel *chan, int *val, + unsigned int scale); + /** * iio_write_channel_attribute() - Write values to the device attribute. * @chan: The channel being queried. From patchwork Mon Mar 8 10:02:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 12121875 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=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 41E1BC433E0 for ; Mon, 8 Mar 2021 10:03:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 22BA760190 for ; Mon, 8 Mar 2021 10:03:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231571AbhCHKDH (ORCPT ); Mon, 8 Mar 2021 05:03:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231561AbhCHKCc (ORCPT ); Mon, 8 Mar 2021 05:02:32 -0500 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5557C06174A for ; Mon, 8 Mar 2021 02:02:31 -0800 (PST) Received: by mail-lj1-x22d.google.com with SMTP id k12so15289433ljg.9 for ; Mon, 08 Mar 2021 02:02:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jQj8Y7bl6W/EwTgFM51ZkEcyxtVI5DMBeB3fwIzUQaU=; b=jQ2Yb87wSov9EOAYKKig7gkL5mtFZrgT7jtwVDLmVEVjG7niFZCnS6r9rsFf5VDt84 UGxEn2W8prRS96gwOj3n3E/nToKAZP4ByR64Hs7y0vvEkz0DhEir+6OijwZmrDu4kEYU NSsYBpHPMRU4LOkzqcNgfArlMDbUgyUP5wK8MmbLp5vGEWBUDFGLt1GSXOLeCvtb1TgS IqGWpDBhXtnFA0a5BgamnottiCcf9wYflitmY2G4GR0gi48TEasJk/hxLMp2Kgq6N1DW qJZflz/jcVjKtWyOgEWdmHnsvDnQT+OacUf039gRCyCdBDPENr9+oOmBLnQHpksnsLeb Sz2A== 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=jQj8Y7bl6W/EwTgFM51ZkEcyxtVI5DMBeB3fwIzUQaU=; b=WeQ9cYQ/+c8YPtaLerRp+50VsvZ+VW4Xl36MPeEox71PIvWAmRqSehjjU1FqjG6OnO QW4r8Xk0NZwVgOBehNBLsBNGTReGPmLl6UBjC92xumMt/CoaSgCA5SDXJVw8Iin4g+sw vmog1+k9cZOoLxQ47dSFyr7RM5l3KL2QowzMjwltfNUO1FJiDO3+/Vhm6hljt2T50vkL YpuKOJl1cP+prUUS7Ntw5RkfTsML8CvoQwUL7HBCYxoiTTNTH9w2D1BT8H6ipZxtfDIx E6GAqNtS8zNwEv0Wlq8Loi3A/eo9Vk2/tIR1R8JHqTsdR8QHrtKpwoXoALcrq1hkSGTf 3BrA== X-Gm-Message-State: AOAM530Nwj3r2PvqEWiD4Q5ToMGcVZ7+KYyS2VrgBZiY6QDMMEwMJ+Wk zSMh9piEufxf9YlqIKR1WqV+pA== X-Google-Smtp-Source: ABdhPJyrKjGR1WckL+EvevOldG/9RWm4Yin/HfubNd7pR+J+ntlEZo0nv3ly+Y9EjuIMR/uzyFFheQ== X-Received: by 2002:a2e:9e17:: with SMTP id e23mr13441080ljk.501.1615197750378; Mon, 08 Mar 2021 02:02:30 -0800 (PST) Received: from localhost.localdomain (c-d7cb225c.014-348-6c756e10.bbcust.telenor.se. [92.34.203.215]) by smtp.gmail.com with ESMTPSA id v2sm1425734ljg.89.2021.03.08.02.02.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Mar 2021 02:02:30 -0800 (PST) From: Linus Walleij To: Jean Delvare , Guenter Roeck , Jonathan Cameron Cc: linux-hwmon@vger.kernel.org, Linus Walleij , Peter Rosin , Chris Lesiak , linux-iio@vger.kernel.org Subject: [PATCH 2/2 v4] hwmon: (ntc_thermistor): try reading processed Date: Mon, 8 Mar 2021 11:02:19 +0100 Message-Id: <20210308100219.2732156-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210308100219.2732156-1-linus.walleij@linaro.org> References: <20210308100219.2732156-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Before trying the custom method of reading the sensor as raw and then converting, we want to use iio_read_channel_processed_scale() which first tries to see if the ADC can provide a processed value directly, else reads raw and applies scaling inside of IIO using the scale attributes of the ADC. We need to multiply the scaled value with 1000 to get to microvolts from millivolts which is what processed IIO channels returns. Keep the code that assumes 12bit ADC around as a fallback. This gives correct readings on the AB8500 thermistor inputs used in the Ux500 HREFP520 platform for reading battery and board temperature. Cc: Peter Rosin Cc: Chris Lesiak Cc: Jonathan Cameron Cc: linux-iio@vger.kernel.org Link: https://lore.kernel.org/linux-iio/20201224011607.1059534-1-linus.walleij@linaro.org/ Signed-off-by: Linus Walleij Tested-by: Chris Lesiak Acked-by: Guenter Roeck --- ChangeLog v3->v4: - Split out the new iio_read_channel_processed_scale() API addition to a separate patch. - My suggestion is to apply both patches to the hwmon tree. ChangeLog v2->v3: - After discussion about v2 we concludes that iio_read_channel_processed() could loose precision so we introduce a new API to read processed and scale. - Include a link to the v2 discussion for reference. - For ease of applying to the hwmon tree, keep it all in one patch. - This needs Jonathans ACK to be merged through hwmon. ChangeLog v1->v2: - Fix the patch to multiply the processed value by 1000 to get to microvolts from millivolts. - Fix up the confusion in the commit message. - Drop pointless comments about the code, we keep the original code path around if processed reads don't work, nothing bad with that. --- drivers/hwmon/ntc_thermistor.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c index 3aad62a0e661..8587189c7f15 100644 --- a/drivers/hwmon/ntc_thermistor.c +++ b/drivers/hwmon/ntc_thermistor.c @@ -326,18 +326,27 @@ struct ntc_data { static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata) { struct iio_channel *channel = pdata->chan; - int raw, uv, ret; + int uv, ret; - ret = iio_read_channel_raw(channel, &raw); + ret = iio_read_channel_processed_scale(channel, &uv, 1000); if (ret < 0) { - pr_err("read channel() error: %d\n", ret); - return ret; - } + int raw; - ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000); - if (ret < 0) { - /* Assume 12 bit ADC with vref at pullup_uv */ - uv = (pdata->pullup_uv * (s64)raw) >> 12; + /* + * This fallback uses a raw read and then + * assumes the ADC is 12 bits, scaling with + * a factor 1000 to get to microvolts. + */ + ret = iio_read_channel_raw(channel, &raw); + if (ret < 0) { + pr_err("read channel() error: %d\n", ret); + return ret; + } + ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000); + if (ret < 0) { + /* Assume 12 bit ADC with vref at pullup_uv */ + uv = (pdata->pullup_uv * (s64)raw) >> 12; + } } return uv;