From patchwork Fri Jul 22 13:07:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926467 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BCE55CCA489 for ; Fri, 22 Jul 2022 13:04:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235213AbiGVNED (ORCPT ); Fri, 22 Jul 2022 09:04:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235198AbiGVNEC (ORCPT ); Fri, 22 Jul 2022 09:04:02 -0400 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F860AF87C; Fri, 22 Jul 2022 06:04:02 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id z22so7540410lfu.7; Fri, 22 Jul 2022 06:04:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mqpNQIvQ4UFsQ79e0nCaBDrArkXcARxy0hgxZyWDOKM=; b=QK5I9K0SkBTkC+qKkrxU9HL4SH5j2qXYf2qTNB0XJzYdsjeKH7XTHxHlXwj0OGTy54 Zp0T8PuSmzKrbmSlcNPKqS6UMMhwrvGl8Rtv6Hcp8y22eG1W7X9WLLVeeDOGjKte2U5L StEdDx1Kff+jR3zHtm9MPfIrNeHUNVx7DOzvYkOfr6TCkHhznNe31A+fHEzLdpzsyZCp +Z/azhxW10Z3iVIK+t93xYm+ERFoMmqVaYywp0IDoBgZlbqNflC6YuPcnjZaAs9/fKNO i4/xgTWRpb7ZBkN7xuYsFoc0h51c6qDj/8Tsc1dfIa1jRo+U8vEcZ1kpYBx4xRXo0Lw/ gtIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mqpNQIvQ4UFsQ79e0nCaBDrArkXcARxy0hgxZyWDOKM=; b=JShJi02c/M6v8EOrX/4ZoViwsqCjMkXudUu1HgIlXBNff1be9nGKybmpRb38uVw3+r qCidUYjEhHZEXnbvPngUEsNq4mShZOqzmHf1eyJYP0eQoGrnxVCf5q7SvCjzKPqldDtZ FxVvzhaibwfbQp/ZVCLbnLI5Zv0YYWKaNHpTxzcsPEWd0hk77Lo4O3tG1JFuChNdPD5F ZwowEC20FFSH3sjYz3T8IWQgHYdQYumEmY3WFp1pImhPiyJD86LBoiqK2qVr143+8wqi rlFVh7BRV0y1jDjuqoK4CtPEWWDwX0jqC/nUHfEOhwibJE2NRwtVQdGngY9LhJz+3MfM jSKQ== X-Gm-Message-State: AJIora8ZWES4C/TIcVE8koX8ayY5R7dwax5fhE3LqyBKPxtutImiQEM1 uI2/O/TlnQ5q99Be/+nx/3A= X-Google-Smtp-Source: AGRyM1tadi/g4Nz573WLxQA6eU9xz8pa1F6BWQQQkXtEK0Pye2b3OdmE6QcQN9a9rBtJvkfCzIpKfQ== X-Received: by 2002:a05:6512:22c2:b0:485:8c7a:530d with SMTP id g2-20020a05651222c200b004858c7a530dmr170384lfu.459.1658495040364; Fri, 22 Jul 2022 06:04:00 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.03.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:03:59 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/9] iio: adc: mcp3911: make use of the sign bit Date: Fri, 22 Jul 2022 15:07:18 +0200 Message-Id: <20220722130726.7627-2-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The device supports negative values as well. Fixes: 3a89b289df5d ("iio: adc: add support for mcp3911") Signed-off-by: Marcus Folkesson Reviewed-by: Andy Shevchenko --- drivers/iio/adc/mcp3911.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 1cb4590fe412..f581cefb6719 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -113,6 +113,8 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev, if (ret) goto out; + *val = sign_extend32(*val, 23); + ret = IIO_VAL_INT; break; From patchwork Fri Jul 22 13:07:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926468 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C12FCCA473 for ; Fri, 22 Jul 2022 13:04:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235265AbiGVNEF (ORCPT ); Fri, 22 Jul 2022 09:04:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235231AbiGVNEE (ORCPT ); Fri, 22 Jul 2022 09:04:04 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77BE0AF957; Fri, 22 Jul 2022 06:04:03 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id p11so2683531lfu.5; Fri, 22 Jul 2022 06:04:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=veEx5EC4oEDmljEEGVPcw1pQuw+XxKgBNvnHwBCodyk=; b=qdI1a0MUGooN6d+KUEfkNEo/m4cXU2Oz6fgknlIpummykQfj6JSx8sng1m0Iuy3VjD acwW8b+6RO3zLEtLXCOeP6QOt832OgIYNxSEIpyQGPVMuAh9wikPIGh4gvSJ4e3EFs5z xpx0m9tViKFeYFuRFlV67aGoEhKogokcpn5OERzwQT8ynV8oMSu5LvefsQs3ZLmjWSb4 yNuyltRnY4WhF40GuWAVpkXI679DrKJFjdqx4KzjW9+3NdeCjOgOKHQ9NqPvdIgyDlf2 HYpFQ2XXM9T75SOVt7M4LgqZuf5UGQueHOBbeAanYIa03om/yIdE4SCnFlQz2GjLI+nk W0bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=veEx5EC4oEDmljEEGVPcw1pQuw+XxKgBNvnHwBCodyk=; b=1lYqyWlLzOJnUzzNwJU7qQhYp2TGz3y3F5tPdo7+8d9Bk3xLLvN6xbs0xc9vkWNMUu LKVm8JfknDvj038qmQtREpcb6nZgUgH4B18VdlrQN1cfewpToaJHsBqI+VCDOxcWMwK5 8FconSj11IRQ7/gox95eBaBfT/2P8ePRnbDMib0rygd9ZLlPwOYICy4uZ2Hcll+tJ/Nk BZodhdyi7AFYEet4NNDFhMvQIaSSWkn6s83gZ6EZlbuS+4BLcA3Za/lS6ArgMKHYzPhA 8aDTOZhjar3C5ratbWKa8SJ9UiMjPN60uEr2sydd4b/bTIF+k74ELcf+M5yW9NzflFI6 FrvQ== X-Gm-Message-State: AJIora8lL5VHEPKJmNBdE1dDT7zZaR433Z3HbGYcRXmN+jQ/Jexvb8Cp OqMJFDT/bfugFNgN5u8gVhvYUPJZovbo8A== X-Google-Smtp-Source: AGRyM1uch/IbjVpyG6HaJnj5UaMVtRawNaoBvTFrlBjxuTFDO/lWGxznqiSqZ79bdUbjZ/n7llj0yQ== X-Received: by 2002:a05:6512:b09:b0:481:5b19:3ae4 with SMTP id w9-20020a0565120b0900b004815b193ae4mr177286lfu.293.1658495041706; Fri, 22 Jul 2022 06:04:01 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.04.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:04:00 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/9] iio: adc: mcp3911: correct "microchip,device-addr" property Date: Fri, 22 Jul 2022 15:07:19 +0200 Message-Id: <20220722130726.7627-3-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Go for the right property name that is documented in the bindings. Fixes: 3a89b289df5d ("iio: adc: add support for mcp3911") Signed-off-by: Marcus Folkesson Reviewed-by: Andy Shevchenko --- drivers/iio/adc/mcp3911.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index f581cefb6719..f8875076ae80 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -210,7 +210,14 @@ static int mcp3911_config(struct mcp3911 *adc) u32 configreg; int ret; - device_property_read_u32(dev, "device-addr", &adc->dev_addr); + ret = device_property_read_u32(dev, "microchip,device-addr", &adc->dev_addr); + + /* + * Fallback to "device-addr" due to historical mismatch between + * dt-bindings and implementation + */ + if (ret) + device_property_read_u32(dev, "device-addr", &adc->dev_addr); if (adc->dev_addr > 3) { dev_err(&adc->spi->dev, "invalid device address (%i). Must be in range 0-3.\n", From patchwork Fri Jul 22 13:07:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926469 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B58E7C433EF for ; Fri, 22 Jul 2022 13:04:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235297AbiGVNEG (ORCPT ); Fri, 22 Jul 2022 09:04:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235242AbiGVNEF (ORCPT ); Fri, 22 Jul 2022 09:04:05 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DD1BAF964; Fri, 22 Jul 2022 06:04:04 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id a23so5143641lfm.10; Fri, 22 Jul 2022 06:04:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LOBKkyxGhqnbdYbdfpuPA+uTBiXYlsIeG7X+Dc9rmpo=; b=OQHk61dP9obbTlyXmSO2X+FMtCLoE6dPTyRRLEcPhk40HEWUbjNeifF65Zcku+so7K jsg+z4/+KKCEpHEZpKJEWC+vQd4FKHHYeD78F2hS8zHPExtr37WFWGjlTZWaypLzBMWS GsIWsyepXS3KN3JWI/3BQglzvjWqUMq0JVPK1ms7zAz9qAmRdVfxdlgU3zddWIythcDa Si5f5dKXjebmrEVnCezJQAq9B+wwtRq/Fs8szalCrVnhhAdGc2ZrtRl3FgTwE3B9Ucxa zRG1iEzOyXxuUcDK0hORy7D0cK36PNl4NhRTIOB+3x+1UoEjy3GAYe/4GOs5NUWtdq/K jW4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LOBKkyxGhqnbdYbdfpuPA+uTBiXYlsIeG7X+Dc9rmpo=; b=H58xRhXxyjaS4+7BI4ftjD/svzceJZNxt3cbk5KfuRNBUfKop6qa24spRgQgH3jtyt KFvFXeKFol/6t/su0CPEOmKgbnlJFU1LwpgB8VvtTQhgE4l89/mIXVPk4Dt2SQzHH31U zl/P34i0eDeJ3dO6y/9r2mHaxV50e36rGdOiR5MFG9aR1hN+rvNBSooeirfyKR1ciAmL DIcHSG/9xgkCeccHTEq8Zn7Bgw+58b5WLuBMoaFN6F34vLsCU+CNkrEEo36lVfgp/LVL b7wQeDA7QwEowr/xdxI4OOhrbrA8yqWFOnDDVddjRVoHIffe351qdskcW/pNjnS4tcM1 UWNw== X-Gm-Message-State: AJIora8RgxzPoLAHIjZ0kBxGJme7/HBzHVNDqUiPCM9oWNqVeohfc7z6 RN8oF4e7TzYD4qgykseQqgo= X-Google-Smtp-Source: AGRyM1tjTFWg3W3r/SkCWGS4CibsSM9q5ByXOxNFED0J3QMvDAZuY4YLnzVyCuvO5PKITYkGzS001A== X-Received: by 2002:a05:6512:1109:b0:489:e7b4:7cc with SMTP id l9-20020a056512110900b00489e7b407ccmr157051lfg.617.1658495043622; Fri, 22 Jul 2022 06:04:03 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:04:03 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/9] iio: adc: mcp3911: use correct formula for AD conversion Date: Fri, 22 Jul 2022 15:07:20 +0200 Message-Id: <20220722130726.7627-4-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The ADC conversion is actually not rail-to-rail but with a factor 1.5. Make use of this factor when calculating actual voltage. Fixes: 3a89b289df5d ("iio: adc: add support for mcp3911") Signed-off-by: Marcus Folkesson Reviewed-by: Andy Shevchenko --- drivers/iio/adc/mcp3911.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index f8875076ae80..890af7dca62d 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -40,8 +40,8 @@ #define MCP3911_CHANNEL(x) (MCP3911_REG_CHANNEL0 + x * 3) #define MCP3911_OFFCAL(x) (MCP3911_REG_OFFCAL_CH0 + x * 6) -/* Internal voltage reference in uV */ -#define MCP3911_INT_VREF_UV 1200000 +/* Internal voltage reference in mV */ +#define MCP3911_INT_VREF_MV 1200 #define MCP3911_REG_READ(reg, id) ((((reg) << 1) | ((id) << 5) | (1 << 0)) & 0xff) #define MCP3911_REG_WRITE(reg, id) ((((reg) << 1) | ((id) << 5) | (0 << 0)) & 0xff) @@ -139,11 +139,18 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev, *val = ret / 1000; } else { - *val = MCP3911_INT_VREF_UV; + *val = MCP3911_INT_VREF_MV; } - *val2 = 24; - ret = IIO_VAL_FRACTIONAL_LOG2; + /* + * For 24bit Conversion + * Raw = ((Voltage)/(Vref) * 2^23 * Gain * 1.5 + * Voltage = Raw * (Vref)/(2^23 * Gain * 1.5) + */ + + /* val2 = (2^23 * 1.5) */ + *val2 = 12582912; + ret = IIO_VAL_FRACTIONAL; break; } From patchwork Fri Jul 22 13:07:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926471 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90432CCA48E for ; Fri, 22 Jul 2022 13:04:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235314AbiGVNET (ORCPT ); Fri, 22 Jul 2022 09:04:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235319AbiGVNEH (ORCPT ); Fri, 22 Jul 2022 09:04:07 -0400 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5243AF97D; Fri, 22 Jul 2022 06:04:05 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id z22so7540832lfu.7; Fri, 22 Jul 2022 06:04:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fzXEWH4hDhz8Ds7uR4+Q3KQN6VTJZX7sN1d8PJvppqU=; b=WVqFoCaiooJSy/gM2Zbhu4GH8dfBL8u7leYCY+EBNMk4j+NGKEDf2rRJdeVDC4nt+y rQER8X1dEDJp1ozHNEs9JxaQG7zjUooI2EF2eirDtq51OsPMNu+nMADYxLa10Mb/lZGM NYzlWzdd0ZiMXkuSMIWS77t0m5oF1q1xsqvbhlpEHv+dSWY4dc0aautDYU9Gh7sjFMbE UFtZ5IJcVc8HjYE8uv3IzroZrXRZfdQmrkKcDQ8B6kNn+cbp3Kp7maWLHvs88DGc5SQz go6YpXNW8EebUGTbE/u92zzmplgkPje9Q5a8dBoGsJQuIoSAQwVgkYhG+oGy6YCbLQia yOnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fzXEWH4hDhz8Ds7uR4+Q3KQN6VTJZX7sN1d8PJvppqU=; b=TWRwhZp4gl8S2IKIelflxRKAywxwx5YqBn00bDDDBIjHnR93k+pctjpGPZBaw3UKWH fgozw21mkHvb2ZEz8m2aO91Q0LTS2tb/SAAbmbAPamm1119plyAVA7pdvbpDdq7vdjPU 5voUieggB7xzZad+gM/bMVFfVq/45IMp0fKfvw8pBj7Z1HP5G6gRhTX7Dk7HsahD2WBz GfqFJ9MT/LjQin11Eix+Amg1exzNeE+CxFq1HQg1s4eC5aZhiOvgQ5q8otGOzMSh34F8 UgPV+gfKNIyQekj1tfQVUq2mwEbFydX/BlR7F1Fj6UpfHZYoC+HYTw8TfTAFJAE/wluo MgtA== X-Gm-Message-State: AJIora8C5OJOHA2IfLDHUsV7/RY+Ih4WPAEslEVH91V7TrlaavBkGY6D FVitJKECjIq0hRYe17HY+tM= X-Google-Smtp-Source: AGRyM1vP1xGFwXyFjlW1PXuB+nVZufJAhxUEPm6x+ex4iDF/jjgDMu0kZg3yU+2m2wUbuDNgnoH8SQ== X-Received: by 2002:a05:6512:376d:b0:48a:49fa:5574 with SMTP id z13-20020a056512376d00b0048a49fa5574mr195982lft.30.1658495045225; Fri, 22 Jul 2022 06:04:05 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.04.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:04:04 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/9] iio: adc: mcp3911: use resource-managed version of iio_device_register Date: Fri, 22 Jul 2022 15:07:21 +0200 Message-Id: <20220722130726.7627-5-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Keep using managed resources as much as possible. Signed-off-by: Marcus Folkesson Reviewed-by: Andy Shevchenko --- drivers/iio/adc/Kconfig | 2 ++ drivers/iio/adc/mcp3911.c | 51 ++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 7fe5930891e0..3b5db59f3931 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -718,6 +718,8 @@ config MCP3422 config MCP3911 tristate "Microchip Technology MCP3911 driver" depends on SPI + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER help Say yes here to build support for Microchip Technology's MCP3911 analog to digital converter. diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 890af7dca62d..00dadb1761dc 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -258,6 +258,18 @@ static int mcp3911_config(struct mcp3911 *adc) return mcp3911_write(adc, MCP3911_REG_CONFIG, configreg, 2); } +static void mcp3911_cleanup_clock(void *_adc) +{ + struct mcp3911 *adc = _adc; + clk_disable_unprepare(adc->clki); +} + +static void mcp3911_cleanup_regulator(void *_adc) +{ + struct mcp3911 *adc = _adc; + regulator_disable(adc->vref); +} + static int mcp3911_probe(struct spi_device *spi) { struct iio_dev *indio_dev; @@ -286,6 +298,11 @@ static int mcp3911_probe(struct spi_device *spi) ret = regulator_enable(adc->vref); if (ret) return ret; + + ret = devm_add_action_or_reset(&spi->dev, + mcp3911_cleanup_regulator, adc); + if (ret) + return ret; } adc->clki = devm_clk_get(&adc->spi->dev, NULL); @@ -296,21 +313,25 @@ static int mcp3911_probe(struct spi_device *spi) dev_err(&adc->spi->dev, "failed to get adc clk (%ld)\n", PTR_ERR(adc->clki)); - ret = PTR_ERR(adc->clki); - goto reg_disable; + return PTR_ERR(adc->clki); } } else { ret = clk_prepare_enable(adc->clki); if (ret < 0) { dev_err(&adc->spi->dev, "Failed to enable clki: %d\n", ret); - goto reg_disable; + return ret; } + + ret = devm_add_action_or_reset(&spi->dev, + mcp3911_cleanup_clock, adc); + if (ret) + return ret; } ret = mcp3911_config(adc); if (ret) - goto clk_disable; + return ret; indio_dev->name = spi_get_device_id(spi)->name; indio_dev->modes = INDIO_DIRECT_MODE; @@ -322,31 +343,11 @@ static int mcp3911_probe(struct spi_device *spi) mutex_init(&adc->lock); - ret = iio_device_register(indio_dev); - if (ret) - goto clk_disable; - - return ret; - -clk_disable: - clk_disable_unprepare(adc->clki); -reg_disable: - if (adc->vref) - regulator_disable(adc->vref); - - return ret; + return devm_iio_device_register(&adc->spi->dev, indio_dev); } static void mcp3911_remove(struct spi_device *spi) { - struct iio_dev *indio_dev = spi_get_drvdata(spi); - struct mcp3911 *adc = iio_priv(indio_dev); - - iio_device_unregister(indio_dev); - - clk_disable_unprepare(adc->clki); - if (adc->vref) - regulator_disable(adc->vref); } static const struct of_device_id mcp3911_dt_ids[] = { From patchwork Fri Jul 22 13:07:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926470 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BFB6C43334 for ; Fri, 22 Jul 2022 13:04:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235275AbiGVNER (ORCPT ); Fri, 22 Jul 2022 09:04:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235357AbiGVNEL (ORCPT ); Fri, 22 Jul 2022 09:04:11 -0400 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94247AFB45; Fri, 22 Jul 2022 06:04:08 -0700 (PDT) Received: by mail-lj1-x230.google.com with SMTP id j26so5387464lji.1; Fri, 22 Jul 2022 06:04:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=x/nyurVBH7ue7nYvu0dJP8wwIsDDtOcE5tkxfN9/wqE=; b=B64q7QIIbYL/qDMsOwTzd5v9HYBhBbi6BcoRhUZxe/eHlvCs/dZPokGubRqDLWGMFa dmeeRkTBcCSA5rHDZ8SsFo/wdBzJ797eaiyuXcdCabdeidSE3tP7a7qRvAgHCfbJNzTH oKB1uyzuEZWwCyYphBzo3v3/uvAm8Sh8pc/aFK3zdj0lhOQiyJnvbSezsqDszAUUpkNz /9C3ESH6S9p1vZdFktFWb9G2b0Ucrvcdo+1mNXOjD47r1zZDyGEo4QCNk5cQkrEySjRt 8Sx0ZGFx6z1a4uwWupv+RIDMeEoLO23J6Z6tWxii2zYpKM6sZ2+/k1p0XbVG7/8/aTp9 MCVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=x/nyurVBH7ue7nYvu0dJP8wwIsDDtOcE5tkxfN9/wqE=; b=qvtYf02G0YooF6/PLKfnsU74GkddG60B4YQ1rvh1wzZEw68+qTiFCtClq2qF9e9yBs McI/7qgcBtN2NwAkrUEXhI2J1e7BdW4nFwE96D1GxHbWgUlIq7uGTRITAPmwXmXKg3dp VUtq6WlmNPMAdM1+uBgASUhYdgG9kBDVIhjUjFcRLNSIPnWgbwrPpgpG9hZdPz+v9ZKs dlvnEOfrCTjEapw3PcI51x8PkDVUxljzl7QgHAqH9teJOiON7y66+lMQ4riIHOmMdcln ILijsSbinyf1flKzRDKjm9xp4I+buE2KnMe5Kb02ogv4Ahen9QEDHMbDpXEv2UkYOn/s 7nyQ== X-Gm-Message-State: AJIora/2LvT1lUaHEpfqKexAm1b2IxgcqMcGlhyuArRjmjT0WQs6Suzg OlYxjq2GL5QNMwC8YvzBZ6Y= X-Google-Smtp-Source: AGRyM1ucnvYY1/cFheV/5KueYIJLX3zfCMGKQb/vSJLEQXwFcgOBy73H3rSIardVq8rZukT5Y0LJ6Q== X-Received: by 2002:a2e:934f:0:b0:250:a7bc:2b8f with SMTP id m15-20020a2e934f000000b00250a7bc2b8fmr165404ljh.512.1658495046816; Fri, 22 Jul 2022 06:04:06 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.04.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:04:06 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 5/9] iio: adc: mcp3911: add support for buffers Date: Fri, 22 Jul 2022 15:07:22 +0200 Message-Id: <20220722130726.7627-6-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add support for buffers to make the driver fit for more usecases. Signed-off-by: Marcus Folkesson Reviewed-by: Andy Shevchenko --- drivers/iio/adc/mcp3911.c | 96 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 7 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 00dadb1761dc..96c0a2a50c7c 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -5,15 +5,22 @@ * Copyright (C) 2018 Marcus Folkesson * Copyright (C) 2018 Kent Gustavsson */ +#include +#include #include #include #include #include +#include +#include +#include +#include #include #include #include #include #include +#include #define MCP3911_REG_CHANNEL0 0x00 #define MCP3911_REG_CHANNEL1 0x03 @@ -22,6 +29,7 @@ #define MCP3911_REG_GAIN 0x09 #define MCP3911_REG_STATUSCOM 0x0a +#define MCP3911_STATUSCOM_READ GENMASK(7, 6) #define MCP3911_STATUSCOM_CH1_24WIDTH BIT(4) #define MCP3911_STATUSCOM_CH0_24WIDTH BIT(3) #define MCP3911_STATUSCOM_EN_OFFCAL BIT(2) @@ -54,6 +62,13 @@ struct mcp3911 { struct regulator *vref; struct clk *clki; u32 dev_addr; + struct { + u32 channels[MCP3911_NUM_CHANNELS]; + s64 ts __aligned(8); + } scan; + + u8 tx_buf[MCP3911_NUM_CHANNELS * 3] __aligned(IIO_DMA_MINALIGN); + u8 rx_buf[MCP3911_NUM_CHANNELS * 3]; }; static int mcp3911_read(struct mcp3911 *adc, u8 reg, u32 *val, u8 len) @@ -196,16 +211,63 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, .type = IIO_VOLTAGE, \ .indexed = 1, \ .channel = idx, \ + .scan_index = idx, \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ BIT(IIO_CHAN_INFO_OFFSET) | \ BIT(IIO_CHAN_INFO_SCALE), \ + .scan_type = { \ + .sign = 's', \ + .realbits = 24, \ + .storagebits = 32, \ + .endianness = IIO_BE, \ + }, \ } static const struct iio_chan_spec mcp3911_channels[] = { MCP3911_CHAN(0), MCP3911_CHAN(1), + IIO_CHAN_SOFT_TIMESTAMP(2), }; +static irqreturn_t mcp3911_trigger_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct mcp3911 *adc = iio_priv(indio_dev); + struct spi_transfer xfer = { + .tx_buf = adc->tx_buf, + .rx_buf = adc->rx_buf, + .len = sizeof(adc->rx_buf), + }; + int scan_index; + int i = 0; + int ret; + + mutex_lock(&adc->lock); + adc->tx_buf[0] = MCP3911_REG_READ(MCP3911_CHANNEL(0), adc->dev_addr); + ret = spi_sync_transfer(adc->spi, &xfer, 1); + if (ret < 0) { + dev_warn(&adc->spi->dev, + "failed to get conversion data\n"); + goto out; + } + + for_each_set_bit(scan_index, indio_dev->active_scan_mask, + indio_dev->masklength) { + const struct iio_chan_spec *scan_chan = &indio_dev->channels[scan_index]; + + adc->scan.channels[i] = get_unaligned_be24(&adc->rx_buf[scan_chan->channel * 3]); + i++; + } + iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan, + iio_get_time_ns(indio_dev)); +out: + mutex_unlock(&adc->lock); + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + static const struct iio_info mcp3911_info = { .read_raw = mcp3911_read_raw, .write_raw = mcp3911_write_raw, @@ -214,7 +276,7 @@ static const struct iio_info mcp3911_info = { static int mcp3911_config(struct mcp3911 *adc) { struct device *dev = &adc->spi->dev; - u32 configreg; + u32 regval; int ret; ret = device_property_read_u32(dev, "microchip,device-addr", &adc->dev_addr); @@ -233,29 +295,43 @@ static int mcp3911_config(struct mcp3911 *adc) } dev_dbg(&adc->spi->dev, "use device address %i\n", adc->dev_addr); - ret = mcp3911_read(adc, MCP3911_REG_CONFIG, &configreg, 2); + ret = mcp3911_read(adc, MCP3911_REG_CONFIG, ®val, 2); if (ret) return ret; + regval &= ~MCP3911_CONFIG_VREFEXT; if (adc->vref) { dev_dbg(&adc->spi->dev, "use external voltage reference\n"); - configreg |= MCP3911_CONFIG_VREFEXT; + regval |= FIELD_PREP(MCP3911_CONFIG_VREFEXT, 1); } else { dev_dbg(&adc->spi->dev, "use internal voltage reference (1.2V)\n"); - configreg &= ~MCP3911_CONFIG_VREFEXT; + regval |= FIELD_PREP(MCP3911_CONFIG_VREFEXT, 0); } + regval &= ~MCP3911_CONFIG_CLKEXT; if (adc->clki) { dev_dbg(&adc->spi->dev, "use external clock as clocksource\n"); - configreg |= MCP3911_CONFIG_CLKEXT; + regval |= FIELD_PREP(MCP3911_CONFIG_CLKEXT, 1); } else { dev_dbg(&adc->spi->dev, "use crystal oscillator as clocksource\n"); - configreg &= ~MCP3911_CONFIG_CLKEXT; + regval |= FIELD_PREP(MCP3911_CONFIG_CLKEXT, 0); } - return mcp3911_write(adc, MCP3911_REG_CONFIG, configreg, 2); + ret = mcp3911_write(adc, MCP3911_REG_CONFIG, regval, 2); + if (ret) + return ret; + + ret = mcp3911_read(adc, MCP3911_REG_STATUSCOM, ®val, 2); + if (ret) + return ret; + + /* Address counter incremented, cycle through register types */ + regval &= ~MCP3911_STATUSCOM_READ; + regval |= FIELD_PREP(MCP3911_STATUSCOM_READ, 0x02); + + return mcp3911_write(adc, MCP3911_REG_STATUSCOM, regval, 2); } static void mcp3911_cleanup_clock(void *_adc) @@ -343,6 +419,12 @@ static int mcp3911_probe(struct spi_device *spi) mutex_init(&adc->lock); + ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, + NULL, + mcp3911_trigger_handler, NULL); + if (ret) + return ret; + return devm_iio_device_register(&adc->spi->dev, indio_dev); } From patchwork Fri Jul 22 13:07:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926472 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02CD8CCA490 for ; Fri, 22 Jul 2022 13:04:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235362AbiGVNEU (ORCPT ); Fri, 22 Jul 2022 09:04:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235368AbiGVNEL (ORCPT ); Fri, 22 Jul 2022 09:04:11 -0400 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0A4CAF970; Fri, 22 Jul 2022 06:04:10 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id o7so7552776lfq.9; Fri, 22 Jul 2022 06:04:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lP/jBdxtlvbqvrCkJL3Z7fkeonR4hZLm7YdyaYMKfFs=; b=Qs9scKC2u5aLHQ9jg8i68FaDiKjVOEhPY4+aOzyx/Retd7GfkjEE1AZ14c+1Pao0n7 HyfYSpFS9TW7y1RY759Mvu/6jU9u5iwW3roYIt/C/5ucPX9Txnxfk6zVjuf/uizs5DBu sHA+5uujRRecAqqPiccsbQpg0GcfG2GP15JEJIJxXOvnj+ikE+hKfDKvxYrzodLOTtUS mbiLtzGwEnmS5ocuDDZ+zMXevrTCe36+giQQenWbcCPTfba+/SN736iwTWlgzkgFjmXB m2LcVaU92AjAICcQ0DJCgSQfJZHOx1gr2aR5GiKmbyDSd9k59rz/ce0QjNfEaiEOX3d3 ZgtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lP/jBdxtlvbqvrCkJL3Z7fkeonR4hZLm7YdyaYMKfFs=; b=S2FYC/5yxLWqBoCU88YdOCPcBX7saA/3lxbbsDjcoh+eV2nhUOM9pnRdaD9U5K4F3V ZJgU+amMB7gZsWYy22qjoTYI8RefjFV/EY+waBziBlz1x9wFHgJwSY3jUWw3nggyDvqw e0qJwHQw/zS5MjoDOVYqmYeBgGE18GuHD7XnHR1mShiso2xaZzXxpEkv6IaID2HzdCEz geBBHUAPC6qey5D70Z17zD8QYZTQHJ8tMs29lafiqE2Vgwgdc0rgJfqxCudSBtBJ32l5 vV1hGb3YhXsGWJlps6nIOjgjHiheKiP6L3oz/cY5G0HLRLY5Fp7PEMIEugdN5zqRapdC Hz0Q== X-Gm-Message-State: AJIora+q1hdblpdcNnoD0a8Vog9bYTa92ezAjPaqDdOqhRsArzA3NEgY 9i5OXTFE5ZgyMeI4TBn8+yo= X-Google-Smtp-Source: AGRyM1vEc8KNR3meV8qcOJz4YyltHvZlYFpJ/VKAVw1o6/x3ODjm67DX0h9yYu13xw0KVtwWWFUXFQ== X-Received: by 2002:a05:6512:114d:b0:48a:783c:c13f with SMTP id m13-20020a056512114d00b0048a783cc13fmr179833lfg.355.1658495048832; Fri, 22 Jul 2022 06:04:08 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.04.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:04:08 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 6/9] iio: adc: mcp3911: add support for interrupts Date: Fri, 22 Jul 2022 15:07:23 +0200 Message-Id: <20220722130726.7627-7-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Make it possible to read values upon interrupts. Configure Data Ready Signal Output Pin to either HiZ or push-pull and use it as interrupt source. Signed-off-by: Marcus Folkesson Reviewed-by: Andy Shevchenko --- drivers/iio/adc/mcp3911.c | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 96c0a2a50c7c..7db2c75da4ac 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -29,6 +29,7 @@ #define MCP3911_REG_GAIN 0x09 #define MCP3911_REG_STATUSCOM 0x0a +#define MCP3911_STATUSCOM_DRHIZ BIT(12) #define MCP3911_STATUSCOM_READ GENMASK(7, 6) #define MCP3911_STATUSCOM_CH1_24WIDTH BIT(4) #define MCP3911_STATUSCOM_CH0_24WIDTH BIT(3) @@ -62,6 +63,7 @@ struct mcp3911 { struct regulator *vref; struct clk *clki; u32 dev_addr; + struct iio_trigger *trig; struct { u32 channels[MCP3911_NUM_CHANNELS]; s64 ts __aligned(8); @@ -346,6 +348,23 @@ static void mcp3911_cleanup_regulator(void *_adc) regulator_disable(adc->vref); } +static int mcp3911_set_trigger_state(struct iio_trigger *trig, bool enable) +{ + struct mcp3911 *adc = iio_trigger_get_drvdata(trig); + + if (enable) + enable_irq(adc->spi->irq); + else + disable_irq(adc->spi->irq); + + return 0; +} + +static const struct iio_trigger_ops mcp3911_trigger_ops = { + .validate_device = iio_trigger_validate_own_device, + .set_trigger_state = mcp3911_set_trigger_state, +}; + static int mcp3911_probe(struct spi_device *spi) { struct iio_dev *indio_dev; @@ -409,6 +428,15 @@ static int mcp3911_probe(struct spi_device *spi) if (ret) return ret; + if (device_property_read_bool(&adc->spi->dev, "microchip,data-ready-hiz")) + ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ, + 0, 2); + else + ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ, + MCP3911_STATUSCOM_DRHIZ, 2); + if (ret) + return ret; + indio_dev->name = spi_get_device_id(spi)->name; indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->info = &mcp3911_info; @@ -419,6 +447,31 @@ static int mcp3911_probe(struct spi_device *spi) mutex_init(&adc->lock); + if (spi->irq > 0) { + adc->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d", + indio_dev->name, + iio_device_id(indio_dev)); + if (!adc->trig) + return PTR_ERR(adc->trig); + + adc->trig->ops = &mcp3911_trigger_ops; + iio_trigger_set_drvdata(adc->trig, adc); + ret = devm_iio_trigger_register(&spi->dev, adc->trig); + if (ret) + return ret; + + /* + * The device generates interrupts as long as it is powered up. + * Some platforms might not allow the option to power it down so + * don't enable the interrupt to avoid extra load on the system. + */ + ret = devm_request_irq(&spi->dev, spi->irq, + &iio_trigger_generic_data_rdy_poll, IRQF_NO_AUTOEN | IRQF_ONESHOT, + indio_dev->name, adc->trig); + if (ret) + return ret; + } + ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL, mcp3911_trigger_handler, NULL); From patchwork Fri Jul 22 13:07:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926473 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 160E4CCA492 for ; Fri, 22 Jul 2022 13:04:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235395AbiGVNEU (ORCPT ); Fri, 22 Jul 2022 09:04:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235405AbiGVNEO (ORCPT ); Fri, 22 Jul 2022 09:04:14 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E369AAF960; Fri, 22 Jul 2022 06:04:12 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id m12so6460804lfj.4; Fri, 22 Jul 2022 06:04:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=knFtpsDUHJLhvP+nNFn6yLtZbr5Vu25n0owL6mtc+zk=; b=f6lRXuyAwcbLJbExmbcb5sJghYbagvLVgGeF83P5SMaORdUIkKD3raS8+xn3Qo6p/K R+tbv3jZOIk1tPSdvi2k4j/OZJe9pWTECqMCXBeYJTfHxaS1LpKJyw1jb7Q3oXwlYhOI lpChgYzRxnINoWr6dgXMIODiXpUzUJmcIoWQmGZ9vJKlEtLHgOAn6qHcNmxxKltc4y2W ejya4dI4jrSKng+K2Ahcmqkm+6nu18c9WU2BNJ81jnqOc4BfrKz+haZDRvraBS3syccC g8OGUzeYGTHcjevRYl0g7UTmJyPwWy5yWTNq59bxucl74P+m1nwtpf3b0iSl1Cr5Xyrh PRDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=knFtpsDUHJLhvP+nNFn6yLtZbr5Vu25n0owL6mtc+zk=; b=FG6YoDetvFRnUOFwkI8rSgd6Espv+POwR16Br2RpQH4tdzQ/A5QVJdpvooG+YbNBPW 6cbwS+RsTszUVDo17U7q+OZwPCqM8vZNqMJ2ZbkyjrVETyGxWgSjjEFZ55YUhGStJgRs wZfcRqjW2xNvHOeNyrg18KkiOjRhBO9cOvO74v2icT9C2hes1Gs7Suz+tue2u9z6Tq8N y3EK6Yx7SA6C3xPgoYXb5WphCxliaCWXXwGGuA4pykt8qKgJFxT5q0+EQou0PRRldvyj kRRGqaVfR2lKNt9fD4CPact7P/1JHDJaC/pkN5BoZWiPdDzWd6LK9rOEbdZEKVjg3Nyr qHvA== X-Gm-Message-State: AJIora/RBfEeTMfHYMkVmbkLgTnOc6OjjWyZ/80YRKoR4rya7OsppZBr AeABi6KFXNYwm3NsL4oRXbQ= X-Google-Smtp-Source: AGRyM1sFcAFfYMUJaH4oxbzjmlCO0Bjv6SAAGINlAquwJJU9mqZcGgvbgV7etlXVUt4w8kVFapWWmg== X-Received: by 2002:a05:6512:12c8:b0:489:efbf:18d1 with SMTP id p8-20020a05651212c800b00489efbf18d1mr194117lfg.192.1658495051239; Fri, 22 Jul 2022 06:04:11 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.04.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:04:10 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Krzysztof Kozlowski Subject: [PATCH v4 7/9] dt-bindings: iio: adc: mcp3911: add microchip,data-ready-hiz entry Date: Fri, 22 Jul 2022 15:07:24 +0200 Message-Id: <20220722130726.7627-8-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The Data Ready Output Pin is either hard wired to work as high impedance or push-pull. Make it configurable. Signed-off-by: Marcus Folkesson Acked-by: Krzysztof Kozlowski --- .../devicetree/bindings/iio/adc/microchip,mcp3911.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml b/Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml index 95ab285f4eba..57a16380f864 100644 --- a/Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml +++ b/Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml @@ -36,6 +36,13 @@ properties: description: IRQ line of the ADC maxItems: 1 + microchip,data-ready-hiz: + description: + Data Ready Pin Inactive State Control + true = The DR pin state is high-impedance when data are NOT ready + false = The DR pin state is a logic high when data are NOT ready + type: boolean + microchip,device-addr: description: Device address when multiple MCP3911 chips are present on the same SPI bus. $ref: /schemas/types.yaml#/definitions/uint32 From patchwork Fri Jul 22 13:07:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926474 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA311CCA493 for ; Fri, 22 Jul 2022 13:04:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235418AbiGVNEV (ORCPT ); Fri, 22 Jul 2022 09:04:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235453AbiGVNEP (ORCPT ); Fri, 22 Jul 2022 09:04:15 -0400 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CED4DAF957; Fri, 22 Jul 2022 06:04:14 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id bf9so7479492lfb.13; Fri, 22 Jul 2022 06:04:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QchHYYaodKcbw6uTcWqsRbkjdQtlMxZfWSHqeYa1TyE=; b=FATcg7DMmBGvOSu0G+irWKJOqwynRWNlB6Dp1aUBqISDi6R3RM3+fUQIeVKcgFObvg VLnCu8jcfUdIxPWv2jvjRfQS1sX4aNIyDWGmqNeUVwQH3zr6JLnpwJq4E0t8GNQKHJOU n887vixckl98ga5hsDZCuvxTn+lULDLY21buWx3FDIaMD7B/wCIdGwIxM+Jup16oGkmx 6bsIZTdY5jM/tAffuFBgMaaKKmif7EFK599nLtTL4bqxMvAB3H9HlNcWJ5FFubm3e1ci gqlc/GzL1rkjSOy6JiJZv/eOq2O+oZdo3A0OSPHg84DVj6U1GZYB/s8yqaHNk1uPjUSQ pLvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QchHYYaodKcbw6uTcWqsRbkjdQtlMxZfWSHqeYa1TyE=; b=T5ZGAMYKb2ZJLsY4H2tCzJ3rmPZjxq/LD9MgzSrZ1FOWnpC6L992vHAPhsUppja9gY g1n1p2g/8YCfyr69Y9RbhCdR1+C75HfHRTvKp/YVSK+9k5lIPcTeF7aAjw1Mwbw1lJwe j4ZGrqBsG5aEbVztnzOY9tZfwRS47jR4G2IzqdSc+26MSZuo+umxty5uA9pHabVGqNwK yhbTOKne+Nbi0S9hAm54/8upR1eoKixEj8i6SsVA7bkQKf8un1GhqzCImW/QH0A9Pp1o IaevBAkvl4AbFG9KVPMGT8/eeZ6SM2XTuvXLJ7rWWa0oCgIZm47KLUYzddRAMrrOJMAh XNOw== X-Gm-Message-State: AJIora9floMqnfoiCqe6X5rAoSWr3YZHW0CTylx0vWp2ZNYsVGIIzo3w VttcVIkXWjI/2WaxjB1d/5E= X-Google-Smtp-Source: AGRyM1twYx53HVAyU8h+h3sd75qI/5edkEenReu4YksqwKTpjUShFYbKZsYzhnPapJeVtINr2ty6Fw== X-Received: by 2002:a05:6512:2613:b0:48a:6f26:5c5a with SMTP id bt19-20020a056512261300b0048a6f265c5amr203744lfb.116.1658495053202; Fri, 22 Jul 2022 06:04:13 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:04:12 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 8/9] iio: adc: mcp3911: add support for oversampling ratio Date: Fri, 22 Jul 2022 15:07:25 +0200 Message-Id: <20220722130726.7627-9-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The chip supports oversampling ratio, so expose it to userspace. Signed-off-by: Marcus Folkesson Reviewed-by: Andy Shevchenko --- drivers/iio/adc/mcp3911.c | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 7db2c75da4ac..30c91ccc5fb6 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -39,6 +39,7 @@ #define MCP3911_REG_CONFIG 0x0c #define MCP3911_CONFIG_CLKEXT BIT(1) #define MCP3911_CONFIG_VREFEXT BIT(2) +#define MCP3911_CONFIG_OSR GENMASK(13, 11) #define MCP3911_REG_OFFCAL_CH0 0x0e #define MCP3911_REG_GAINCAL_CH0 0x11 @@ -57,6 +58,8 @@ #define MCP3911_NUM_CHANNELS 2 +static const int mcp3911_osr_table[] = { 32, 64, 128, 256, 512, 1024, 2048, 4096 }; + struct mcp3911 { struct spi_device *spi; struct mutex lock; @@ -115,6 +118,36 @@ static int mcp3911_update(struct mcp3911 *adc, u8 reg, u32 mask, return mcp3911_write(adc, reg, val, len); } +static int mcp3911_write_raw_get_fmt(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + long mask) +{ + switch (mask) { + case IIO_CHAN_INFO_SCALE: + return IIO_VAL_INT_PLUS_NANO; + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + return IIO_VAL_INT; + default: + return IIO_VAL_INT_PLUS_NANO; + } +} + +static int mcp3911_read_avail(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int **vals, int *type, int *length, + long info) +{ + switch (info) { + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + *type = IIO_VAL_INT; + *vals = mcp3911_osr_table; + *length = ARRAY_SIZE(mcp3911_osr_table); + return IIO_AVAIL_LIST; + default: + return -EINVAL; + } +} + static int mcp3911_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *channel, int *val, int *val2, long mask) @@ -143,6 +176,15 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev, ret = IIO_VAL_INT; break; + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + ret = mcp3911_read(adc, MCP3911_REG_CONFIG, val, 2); + if (ret) + goto out; + + *val = FIELD_GET(MCP3911_CONFIG_OSR, *val); + *val = 32 << *val; + ret = IIO_VAL_INT; + break; case IIO_CHAN_INFO_SCALE: if (adc->vref) { @@ -202,6 +244,17 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, MCP3911_STATUSCOM_EN_OFFCAL, MCP3911_STATUSCOM_EN_OFFCAL, 2); break; + + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + for (int i = 0; i < sizeof(mcp3911_osr_table); i++) { + if (val == mcp3911_osr_table[i]) { + val = FIELD_PREP(MCP3911_CONFIG_OSR, i); + ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR, + val, 2); + break; + } + } + break; } out: @@ -214,9 +267,12 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, .indexed = 1, \ .channel = idx, \ .scan_index = idx, \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ BIT(IIO_CHAN_INFO_OFFSET) | \ BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_shared_by_type_available = \ + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ .scan_type = { \ .sign = 's', \ .realbits = 24, \ @@ -273,6 +329,8 @@ static irqreturn_t mcp3911_trigger_handler(int irq, void *p) static const struct iio_info mcp3911_info = { .read_raw = mcp3911_read_raw, .write_raw = mcp3911_write_raw, + .read_avail = mcp3911_read_avail, + .write_raw_get_fmt = mcp3911_write_raw_get_fmt, }; static int mcp3911_config(struct mcp3911 *adc) From patchwork Fri Jul 22 13:07:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12926475 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E282C43334 for ; Fri, 22 Jul 2022 13:04:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235231AbiGVNEW (ORCPT ); Fri, 22 Jul 2022 09:04:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235233AbiGVNES (ORCPT ); Fri, 22 Jul 2022 09:04:18 -0400 Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7E93AF960; Fri, 22 Jul 2022 06:04:16 -0700 (PDT) Received: by mail-lj1-x229.google.com with SMTP id r14so5365754ljp.2; Fri, 22 Jul 2022 06:04:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=F3i1nh0A1DHu/AJzxatV5Ku9zoBWXQ5Yp0VbvHDsCrw=; b=EDRNUioqRnq0DZaVEO2bvdnLyHv1qmw16uc9geh3IsTDCf/ZVX/zikyN0y+fZZEuQz ylWhEkP0OVB4oOTXIW0pG/woVg0EQ5j2kM2bH5s4HUgeRBA/VmvSclln9iInvje29Lgi yaA1qezL8iGOwfCHxwLukornUQtPkS88drkXfUTqVjB6gB1Rc71SjUvrmRV6pLw8wkOT 2umkQ5KuAqflHsPeCiNAtDkgChirRGTULbIzbt/+eXYA5USQyIus3lCIKpqnOgjRjYGN 7I6Ou9vmvrs4SGpafGbcrM5744Ns8hWhYThjb3t8EEotY2+aPPoni/8pueCo0FMpBQzz eKRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=F3i1nh0A1DHu/AJzxatV5Ku9zoBWXQ5Yp0VbvHDsCrw=; b=IykRaXMZxGJj7tgSm7izjBbzERWbFIRwXiiWTeU8fg49vML8aUb1LcqvVZHGDDD2RP 9ZZfeY716g0l31Q36qW09Izwj+HeixzVoMGhKP8dOPHiZSrU4+U9iOr1M9kRPU67yfyG bDNRWzlKYVN0GL9Wl7BaX9/dvEZqF25NT+zCfhGHrHpnBZe9hTb0Bsis2BRkfNIBq5eu r9IQvlbz0Mtqb5mdrGwNOyJq/mPS22HjNCgGeZdKROI9Fe0jRGp/0mcvxRp/ZeXhDLgi hkexB7YylaxxVFqomlLjVQdsv3H9b9hp+y6M7++c/UjqsJeD1CdGnbXAUQxcbqXmB7Hc fvhw== X-Gm-Message-State: AJIora/Gb1Q8/WHJ8A2MmeoBgEIeDW6h36Z/lEN7tL6m5oDG4tVNfdal D32ANUvYg48ZYrnJ8QBjXvM= X-Google-Smtp-Source: AGRyM1viDf923Qcug9VOj5X/cN4Oq4EE1XKhddh1BkKFwmk/LlkItvoZ3WcgJQEvh8GrC9oD5cYwww== X-Received: by 2002:a2e:93c8:0:b0:24d:b348:b070 with SMTP id p8-20020a2e93c8000000b0024db348b070mr167912ljh.434.1658495055190; Fri, 22 Jul 2022 06:04:15 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id s18-20020a056512203200b004786d37229csm1039673lfs.155.2022.07.22.06.04.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:04:14 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 9/9] iio: adc: mcp3911: add support to set PGA Date: Fri, 22 Jul 2022 15:07:26 +0200 Message-Id: <20220722130726.7627-10-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220722130726.7627-1-marcus.folkesson@gmail.com> References: <20220722130726.7627-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add support for setting the Programmable Gain Amplifiers by adjust the scale value. Signed-off-by: Marcus Folkesson Reviewed-by: Andy Shevchenko --- drivers/iio/adc/mcp3911.c | 107 +++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 24 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 30c91ccc5fb6..22a43d3fe402 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -27,6 +27,8 @@ #define MCP3911_REG_MOD 0x06 #define MCP3911_REG_PHASE 0x07 #define MCP3911_REG_GAIN 0x09 +#define MCP3911_GAIN_MASK(ch) (GENMASK(2, 0) << 3 * ch) +#define MCP3911_GAIN_VAL(ch, val) ((val << 3 * ch) & MCP3911_GAIN_MASK(ch)) #define MCP3911_REG_STATUSCOM 0x0a #define MCP3911_STATUSCOM_DRHIZ BIT(12) @@ -57,8 +59,10 @@ #define MCP3911_REG_WRITE(reg, id) ((((reg) << 1) | ((id) << 5) | (0 << 0)) & 0xff) #define MCP3911_NUM_CHANNELS 2 +#define MCP3911_NUM_SCALES 6 static const int mcp3911_osr_table[] = { 32, 64, 128, 256, 512, 1024, 2048, 4096 }; +static u32 mcp3911_scale_table[MCP3911_NUM_SCALES][2]; struct mcp3911 { struct spi_device *spi; @@ -67,6 +71,7 @@ struct mcp3911 { struct clk *clki; u32 dev_addr; struct iio_trigger *trig; + u32 gain[MCP3911_NUM_CHANNELS]; struct { u32 channels[MCP3911_NUM_CHANNELS]; s64 ts __aligned(8); @@ -143,6 +148,11 @@ static int mcp3911_read_avail(struct iio_dev *indio_dev, *vals = mcp3911_osr_table; *length = ARRAY_SIZE(mcp3911_osr_table); return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_SCALE: + *type = IIO_VAL_INT_PLUS_NANO; + *vals = (int *)mcp3911_scale_table; + *length = ARRAY_SIZE(mcp3911_scale_table) * 2; + return IIO_AVAIL_LIST; default: return -EINVAL; } @@ -187,29 +197,9 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev, break; case IIO_CHAN_INFO_SCALE: - if (adc->vref) { - ret = regulator_get_voltage(adc->vref); - if (ret < 0) { - dev_err(indio_dev->dev.parent, - "failed to get vref voltage: %d\n", - ret); - goto out; - } - - *val = ret / 1000; - } else { - *val = MCP3911_INT_VREF_MV; - } - - /* - * For 24bit Conversion - * Raw = ((Voltage)/(Vref) * 2^23 * Gain * 1.5 - * Voltage = Raw * (Vref)/(2^23 * Gain * 1.5) - */ - - /* val2 = (2^23 * 1.5) */ - *val2 = 12582912; - ret = IIO_VAL_FRACTIONAL; + *val = mcp3911_scale_table[ilog2(adc->gain[channel->channel])][0]; + *val2 = mcp3911_scale_table[ilog2(adc->gain[channel->channel])][1]; + ret = IIO_VAL_INT_PLUS_NANO; break; } @@ -227,6 +217,18 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, mutex_lock(&adc->lock); switch (mask) { + case IIO_CHAN_INFO_SCALE: + for (int i = 0; i < MCP3911_NUM_SCALES; i++) { + if (val == mcp3911_scale_table[i][0] && + val2 == mcp3911_scale_table[i][1]) { + + adc->gain[channel->channel] = BIT(i); + ret = mcp3911_update(adc, MCP3911_REG_GAIN, + MCP3911_GAIN_MASK(channel->channel), + MCP3911_GAIN_VAL(channel->channel, i), 1); + } + } + break; case IIO_CHAN_INFO_OFFSET: if (val2 != 0) { ret = -EINVAL; @@ -262,6 +264,47 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, return ret; } +static int mcp3911_calc_scale_table(struct mcp3911 *adc) +{ + u32 ref = MCP3911_INT_VREF_MV; + u32 div; + int ret; + int tmp0, tmp1; + s64 tmp2; + + if (adc->vref) { + ret = regulator_get_voltage(adc->vref); + if (ret < 0) { + dev_err(&adc->spi->dev, + "failed to get vref voltage: %d\n", + ret); + return ret; + } + + ref = ret / 1000; + } + + /* + * For 24-bit Conversion + * Raw = ((Voltage)/(Vref) * 2^23 * Gain * 1.5 + * Voltage = Raw * (Vref)/(2^23 * Gain * 1.5) + * + * ref = Reference voltage + * div = (2^23 * 1.5 * gain) = 12582912 * gain + */ + for (int i = 0; i < MCP3911_NUM_SCALES; i++) { + div = 12582912 * BIT(i); + tmp2 = div_s64((s64)ref * 1000000000LL, div); + tmp1 = div; + tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1); + + mcp3911_scale_table[i][0] = 0; + mcp3911_scale_table[i][1] = tmp1; + } + + return 0; +} + #define MCP3911_CHAN(idx) { \ .type = IIO_VOLTAGE, \ .indexed = 1, \ @@ -271,8 +314,10 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ BIT(IIO_CHAN_INFO_OFFSET) | \ BIT(IIO_CHAN_INFO_SCALE), \ - .info_mask_shared_by_type_available = \ + .info_mask_shared_by_type_available = \ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .info_mask_separate_available = \ + BIT(IIO_CHAN_INFO_SCALE), \ .scan_type = { \ .sign = 's', \ .realbits = 24, \ @@ -495,6 +540,20 @@ static int mcp3911_probe(struct spi_device *spi) if (ret) return ret; + ret = mcp3911_calc_scale_table(adc); + if (ret) + return ret; + + /* Set gain to 1 for all channels */ + for (int i = 0; i < MCP3911_NUM_CHANNELS; i++) { + adc->gain[i] = 1; + ret = mcp3911_update(adc, MCP3911_REG_GAIN, + MCP3911_GAIN_MASK(i), + MCP3911_GAIN_VAL(i, 0), 1); + if (ret) + return ret; + } + indio_dev->name = spi_get_device_id(spi)->name; indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->info = &mcp3911_info;