From patchwork Tue Aug 9 07:36:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939391 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 EF5B9C25B06 for ; Tue, 9 Aug 2022 07:32:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239002AbiHIHcb (ORCPT ); Tue, 9 Aug 2022 03:32:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235150AbiHIHcb (ORCPT ); Tue, 9 Aug 2022 03:32:31 -0400 Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7216A1EED3; Tue, 9 Aug 2022 00:32:29 -0700 (PDT) Received: by mail-lj1-x236.google.com with SMTP id l10so7553929lje.7; Tue, 09 Aug 2022 00:32:29 -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=FbGrIGAHhJp/faYmz82qs7Q5FS2VF0GR2GJ1cl1M5UM=; b=A4/NB6hQs7YAzVnSIW1tzll4PpJ/8TqKsheiN14pBB7krXkCKiYlMn9lQwHcoN7d4b 2CFaPZ3rR45zCrrl1/bWeOaIEIXvWSziVPt6Kag7ON6FNIrcBSyfhfRnPiobZACgrXwe +XPzHPmzoUNAHiTQhVkoBj4UsMsVEiAKdRuUfVH+TX7J7l1z0BXc7cc5p3unVO1khBex uywKZfu5Dbptdaq0cikCK8y4yW9IRwr/TJpaK/VK/5iK3I5NC0eHh992b3puR9vCYSbH GBaWQY2NDxZN9hPs7AtI+U6yCGh8ByVLE8Zf5ND2043XnTGFaOqbbk8A+WgWLEGwTHHQ BtOA== 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=FbGrIGAHhJp/faYmz82qs7Q5FS2VF0GR2GJ1cl1M5UM=; b=mP9LltqcotfxsCShgPxjTFybebjTmpuyyTcPfqYYVABQAICB08kaNY8MAlfk8sDj8+ 1BNgTi5WaFZD1+Wda/iMQglMi5TgdV9l692vv31Gp3Pa9lFERZ5jMqJTTQkspe5uzk1I MZWFi6Ms/j4SmJWLl6wEZ/qnjcws6ktl+wHv3VIwxCKqFV6RiLo2ggMsp1Pu1rRgmCrQ XZBwn08KEyQiNHLRokVYLmB27kx1ogiwLBmK10iUOHuKjRuDN37W9qc9Vis/d2lCjf27 aYzl1JWZtE5LizU4wUtQkANAzIlNv4y+EFzUcTGdpyOzgyiCprO4Cg+Vt7lFMx/GkOKL fK2A== X-Gm-Message-State: ACgBeo1Jy19U+ehHintOoPov4vRWY1FdHJSPvHLFnPV4BXoMhspC9UEr 9D/2uPOHlxKMnCrHLepMwMbsQDKXcTjjtQ== X-Google-Smtp-Source: AA6agR5Rcj6qS7KuUrq6Dlec0sG22Pv/33VKcqgKumBGGXotc5CXBxqQy1XwO7THvfX9fsRo3dS6ig== X-Received: by 2002:a2e:92c8:0:b0:25d:6ddf:e71d with SMTP id k8-20020a2e92c8000000b0025d6ddfe71dmr6734711ljh.170.1660030347821; Tue, 09 Aug 2022 00:32:27 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:27 -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 v5 1/9] iio: adc: mcp3911: make use of the sign bit Date: Tue, 9 Aug 2022 09:36:40 +0200 Message-Id: <20220809073648.167821-2-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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 Tue Aug 9 07:36:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939392 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 B2B1AC19F2D for ; Tue, 9 Aug 2022 07:32:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239068AbiHIHcd (ORCPT ); Tue, 9 Aug 2022 03:32:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239000AbiHIHcb (ORCPT ); Tue, 9 Aug 2022 03:32:31 -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 9F4821EED4; Tue, 9 Aug 2022 00:32:30 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id f20so15815737lfc.10; Tue, 09 Aug 2022 00:32:30 -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=KE7Doo8+PSeMnrmOEri8h44PeWIbSZ8XCkY/na8+SzA=; b=RKoQ4gneA2KRJSaw1sRwwm17vkbyM2oLjdPe6pleykMyfMwjEjR6EoW/h13hUAtEay WZQZDDsAMjsqzm5+2MaLeWjpGYbl4hBzWtaO0cUGHkwxFH7ZBdAcbwQf7AwIuKhOx2RD i0H5qDDqx0yrSo4Q4pSXCjExDBDhYsvnQ/5ikOTSAtnnKHuSypGcF6psAmJFHrGJgeUN ZKvmX066e8XT3Mmg78XRoC5NzQZhWu2XcWIhA8evoLEsW0jkEWEREEW0SShfJAFDv6ai V+uIx7938FP4NzxKISUE1vDnO8mmZxf66mU3C5HMX+yTS39a6VIybrDu14PuUfBhhG8E LOmA== 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=KE7Doo8+PSeMnrmOEri8h44PeWIbSZ8XCkY/na8+SzA=; b=BTVlA8in/EqWdjhjx6RIeFt4aejyO3OSvsARtdMIcKc1I7f16iyY+xNM7kPOynqkg2 2wnn4yRY+xkoQEAudI0r2phftjTIMqnHvgxxLN+tFBU5I2X+4xLeEDsNuOtZmMhgnzKL 0oLfdG0mqVOJoLO4oF5Jqd4woTnN6aLB4CQJpm5dy85VB+5bJ5EJUBVKcpq20c0dEH5H /Ma8vMVmCdECC2X1mwyJAmUtCZlHG/nm//FNuvHQluWf1xIQVfOK5VhRlILMwp+hDSWA I4ALU9VEp+MFjY0Jv40mPejWOqq3n9319g78aeW+tixQPxBYRQxU3c39SFu1F9qYUhFd +MXA== X-Gm-Message-State: ACgBeo3qWhfmJlBCFt26gQLjt5E3t9QyDsKjkyU3MCXhaXnPrWqF53gz KQG/8GAlXwmyIX2c42rOoSRol+hO9SR/4w== X-Google-Smtp-Source: AA6agR46o9npt9wZ0dLDI2CWbEDJcbyQu26SxvCraiUyCtbozsaYXGLpxLMXs5Yf3N6kCOJcso+M1Q== X-Received: by 2002:a05:6512:e82:b0:48b:23f5:d153 with SMTP id bi2-20020a0565120e8200b0048b23f5d153mr8553992lfb.291.1660030348828; Tue, 09 Aug 2022 00:32:28 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:28 -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 v5 2/9] iio: adc: mcp3911: correct "microchip,device-addr" property Date: Tue, 9 Aug 2022 09:36:41 +0200 Message-Id: <20220809073648.167821-3-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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 Tue Aug 9 07:36:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939393 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 8A947C3F6B0 for ; Tue, 9 Aug 2022 07:32:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239078AbiHIHce (ORCPT ); Tue, 9 Aug 2022 03:32:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239056AbiHIHcc (ORCPT ); Tue, 9 Aug 2022 03:32:32 -0400 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 773BBE003; Tue, 9 Aug 2022 00:32:31 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id bx38so12116655ljb.10; Tue, 09 Aug 2022 00:32:31 -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=ZnlLRiHQsCpOZ8GUpmYe1fQICBNpJYF4yJ5P9XeXI20=; b=MONaGphdMLbgiLFkSjeBJTZ2fYuavDc1hO5Ln/eQm5EpJgrYrOX4hJcOabbierBVk7 Y9/IoeVEx8HwjAB7Qfp0IEOQur9m5P24A3FgBORb2SpwzsA/kZBZsUU7Fp8LVF+rn8ad tZBhzF9oNs9ymftwNjHuQFsThCeNOYETqYg5x9wdZ0cyWZIfIjBUTFDy5T760lA5ttfG YasrH3JhSpnUmTHNgh7U18I+y4mmjoOy6I6vUClmBqvjdtmrF7/QZG1kqdhAL/19B/G/ 2OcA9M5Flw2GFjigyBiAP0ikXIQmKoF70lXfmjXFB15gfmVmyFakxSInmaAjMWb/BVGs LEng== 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=ZnlLRiHQsCpOZ8GUpmYe1fQICBNpJYF4yJ5P9XeXI20=; b=wrvCE3zKH3r7fnKYj8Dy8tM+isDpOIa6MCvfXSrggLBkv32dk5RZEvvP2jvE9BmIZ5 zAI9cVFeRITyIOYVqZ+gouoQlBUJWWPeb1v5rJLFNvVFKGg8y4U2Vr1eHNf8rKFyslJ8 RgUiORCuO7BFyWeN+E2t00UNEdenuX2r0xDtdQTNr5J0NoeQNltVqR1oEGFSKTI70i1l krtDx9uraL6EUnzNEqoAnVeacXv7rq6VX6ns1K1OhhjRhWvuib0djuQ8vva1ZUkb8Dtv 55V0E116DepBUTqttATYjJiREtu/Q/OCP1Wr/6WCc3VC9DIjstcoId2btji4RjqrFSML 6YQA== X-Gm-Message-State: ACgBeo0BJBsrgfd4Nn/Ptx2V15m1wZHK3myeQo/0chaVSrw+Y677a/Pn 5xGTLcy5lAEsVj+VkWGPZjM= X-Google-Smtp-Source: AA6agR4nCzarSzkMC9IdG5foQgQhVyDMR1qMxsMaEbCzbXZ/i70TBWI/uD/bWyM2Jga2/l++5zu8Iw== X-Received: by 2002:a05:651c:543:b0:25f:dbbb:9cd4 with SMTP id q3-20020a05651c054300b0025fdbbb9cd4mr2825936ljp.495.1660030349846; Tue, 09 Aug 2022 00:32:29 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:29 -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 v5 3/9] iio: adc: mcp3911: use correct formula for AD conversion Date: Tue, 9 Aug 2022 09:36:42 +0200 Message-Id: <20220809073648.167821-4-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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 Tue Aug 9 07:36:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939394 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 C97F9C19F2D for ; Tue, 9 Aug 2022 07:32:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239090AbiHIHcf (ORCPT ); Tue, 9 Aug 2022 03:32:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239069AbiHIHce (ORCPT ); Tue, 9 Aug 2022 03:32:34 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F6311F63F; Tue, 9 Aug 2022 00:32:32 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id u6so6789895ljk.8; Tue, 09 Aug 2022 00:32:32 -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=MmAFyPEWTYXBpGJTGxeKOerbkQVNbVS4fi1FukS6r28=; b=a9mz3qIq8uByvysJppH43ZSQyAN3lHYP5KwLeUAfKSW/KGOIIfT0oYm2RrAFvXUGRE AKuElskMVLxy1Q023Ev3N9GnG7xJ9h1Xxxpcju0gQX0afO0RntR7qBe2btdyw9Oia5ul RB03GdNnJRgnnEypv+GTe6Fr4vvXmIdEZcYNjxXIjve4zq6f8ffV1/JVz9KUzsEwQuM1 1YEx0j19ezYdxP+p/LAUSbpcs6pE8ca2t2SyMwBrZjuDx1/YAQCn29AjqkidHJnNFJJK SMVb+FAlwFNx96/EjgOb+O0xphvVxIb4ucSJdmpLd/fuC4rVbpG0rmsDRGXp9Ns0EM2B wZFg== 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=MmAFyPEWTYXBpGJTGxeKOerbkQVNbVS4fi1FukS6r28=; b=c+AT17pmYE2r7mLMFGnmcneINMlBIqMibWCjpxQIGWk+PUzqSyC1VRnydKsVkQtK8b d7BFVg0KXZZZYCyUTKHN8plwbapquWfSWP/sAM/porF+MmPa0/4iAbDRdNiWqalZjSCn BSfZu5QFfXDqsPMfcEPnvra90h1mXoo4+bvqXDefdklGSLcG9KvFufqY7vqq2IaEBzHG Rx0UJVX6oagpp7drPGcK2wUWzcZBqMyDgAjwN//91lfQUkwX2yXR+19hMRmTwZSrEbTp lIW9rVSGaSY3HM49ZLi0z/nrlS6OQ8LI6cqgLrH4M3vhwkPSY2LqbRVsz6E73/OptVWc wPDA== X-Gm-Message-State: ACgBeo3KpxhHvMN96Mp7oVUFPziHu3BigBvK/733OLKe5k3qAeM+jdEr p+aF/l9prRoHOtamlryelG8= X-Google-Smtp-Source: AA6agR77YR8aVVE5TvMuJydOWPQWOYZEWzygkDLcLS7VZAL/396FriRzqDNHbeGghdP9uDxh3M0nCQ== X-Received: by 2002:a2e:be88:0:b0:25f:e9a8:44b8 with SMTP id a8-20020a2ebe88000000b0025fe9a844b8mr1762235ljr.92.1660030350921; Tue, 09 Aug 2022 00:32:30 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:30 -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 v5 4/9] iio: adc: mcp3911: use resource-managed version of iio_device_register Date: Tue, 9 Aug 2022 09:36:43 +0200 Message-Id: <20220809073648.167821-5-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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/mcp3911.c | 56 ++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 890af7dca62d..29d73b847f5f 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,7 @@ 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; -} - -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); + return devm_iio_device_register(&adc->spi->dev, indio_dev); } static const struct of_device_id mcp3911_dt_ids[] = { @@ -367,7 +364,6 @@ static struct spi_driver mcp3911_driver = { .of_match_table = mcp3911_dt_ids, }, .probe = mcp3911_probe, - .remove = mcp3911_remove, .id_table = mcp3911_id, }; module_spi_driver(mcp3911_driver); From patchwork Tue Aug 9 07:36:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939396 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 853A2C19F2D for ; Tue, 9 Aug 2022 07:32:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239226AbiHIHcj (ORCPT ); Tue, 9 Aug 2022 03:32:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239104AbiHIHcf (ORCPT ); Tue, 9 Aug 2022 03:32:35 -0400 Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD4A620F60; Tue, 9 Aug 2022 00:32:33 -0700 (PDT) Received: by mail-lj1-x22b.google.com with SMTP id bx38so12116743ljb.10; Tue, 09 Aug 2022 00:32:33 -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=KyES8n4TEV7WaT3XyazRiNBK6BLwoglNQHGkECI369k=; b=Z4d7a0bXbSyFBBVpiYpQXgdxIzpjOu0OZfzJDYeM7/oTuhlVeM91hL+ZphL0qfEWd9 l6o5chbVfz2pOVaiR772hV9SeT2dVi/P8Rdvl9+qwC9Rf2N6ejWtLJxtQx6nES+Xp5HU j9aGtl61u+/jsWNHVkg0hQNM/Or+7UGD4xbdxoe+7xmZfUJvPfaxRaxz9Ltou279XNry B8l//7iP5eu7S//pkquR3uTPowPybg1ZCZ3krT9TxxekmPBvNn1lEQnYLamVy6L+FYV+ tTDqNS3I7+tUpwOwnI5w45IBqEbUbnWHhjNCOEoaU6W3TTQg8Zqd64XBlyoBH0hbFWkh rDHA== 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=KyES8n4TEV7WaT3XyazRiNBK6BLwoglNQHGkECI369k=; b=qCNkGNaAlQNBfXfhFXkpg+ROa8XPFfF73VNnrVZ+Wo9XMLErXK4ikOJuR7dCNQwMU8 L8t3wg+exxeq1KN8yG4v2PT49p11ZLaGtbFfyAl2Fed6cEBxiAYvD+r82S7J2xxPNVcd hsDVLAJdwcy8plElw4jx5zZosaq8ensaT3j3xNgb3rWk7E4vvsh3BA6ELrrRmgBN6mQf 7BJDmj3u6WKWgTkEwNVFToLOD8PBe9R27t1FIqbgiHTzzvefwu9sD+xveVCr/GQgKJyE ub//uy1EFwq4MYLHwOsLvWaZZ17kIjxwa4j7hM9IZ//tUTF47yrsmIhhF+U12w/i0Sje zXFg== X-Gm-Message-State: ACgBeo0ujfYfFTOC06wMQ8yeB6d3pRWNqXDSdNlH5RJqOc7MFzlKtoQY 5aVdVx2IKWaoy5CqEE3uP18= X-Google-Smtp-Source: AA6agR7B6uvn/8TLmjNdDwNij0xrgqMZfhxegmHtL+MsSEk4KDHnfLhsNYLldMmL6h826apPvilGDQ== X-Received: by 2002:a2e:9b47:0:b0:25e:4887:648b with SMTP id o7-20020a2e9b47000000b0025e4887648bmr6614128ljj.414.1660030352128; Tue, 09 Aug 2022 00:32:32 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:31 -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 v5 5/9] iio: adc: mcp3911: add support for buffers Date: Tue, 9 Aug 2022 09:36:44 +0200 Message-Id: <20220809073648.167821-6-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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/Kconfig | 2 + drivers/iio/adc/mcp3911.c | 102 +++++++++++++++++++++++++++++++++++--- 2 files changed, 97 insertions(+), 7 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 29d73b847f5f..87ab7e24ab08 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -5,16 +5,24 @@ * 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 #define MCP3911_REG_MOD 0x06 @@ -22,6 +30,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 +63,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 __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 +212,68 @@ 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, + .len = 1, + }, { + .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 = MCP3911_REG_READ(MCP3911_CHANNEL(0), adc->dev_addr); + ret = spi_sync_transfer(adc->spi, xfer, ARRAY_SIZE(xfer)); + 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 +282,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 +301,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 +425,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 Tue Aug 9 07:36:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939395 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 5CF79C25B06 for ; Tue, 9 Aug 2022 07:32:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239140AbiHIHch (ORCPT ); Tue, 9 Aug 2022 03:32:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233545AbiHIHce (ORCPT ); Tue, 9 Aug 2022 03:32:34 -0400 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85F72201A8; Tue, 9 Aug 2022 00:32:33 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id bx38so12116782ljb.10; Tue, 09 Aug 2022 00:32:33 -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=V+SAkjMA9USkunV6u8e85+vmzHskDjPwmtcGKKR/aSo=; b=mmHxuYFsb+En24QdjK9F2J9TD1SkCS6Gz9hjqVGIumoGsrFhrdC5HvQJYk9KqMgnq3 YK4H0Tj3fZusyL9Sn8ZCWqU+1P/Be3gGNe/wtFTEbJFxetoAUxtsU26lftmTKZ5u6+bS Jxhg3MtQTVkqSdcl6sV0sWYE1Z6Vd9rnbeYTdRyyuvxQhps9Us/WQgVUxEObuUnDwXsR uVwR+M2+kDArqYEa4hioOpP+EkWzgbOtoP7xBWK5+xqlbtAXLj9RJdpvRrIzBeRmTWVI hwxQJUlzQsLRgPNrd3LQPqpiH4E3LllL8YPXHqMsZmFPAA6l2Vh6XvaPOEz8qowDFy4z R0SA== 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=V+SAkjMA9USkunV6u8e85+vmzHskDjPwmtcGKKR/aSo=; b=tNDQ9nJxsX29qUNea5GT40IA9LzqXddiwk7vbNnJ5pPdZtR9Tt76Fs+KBSzaW3ddFN t+VjJbG6/FMa/5u9c/AXysuSRhEdFvTs5x884VsdA4OrNmNX3XF5fwkAdRvPB0jVS6po a9992q8Mye95sc0duubGUs2lGTgurn8VgFAs8Dx5ctuaP+hRkniysV1g3UDPOIFaZhb0 KU9DIk9JXOSTxgzcJytMaXS1FF9F+s+oq9cudzrsMZNl5wn/N8x8nv+F2G6uX6qHUCbZ LcUnfjsdyuSJMycy51u3J4Zrw+wtsL/rkvxgvQRdiZ9GTwL4nuVgoW6xVzA07qyj4VNz aE3A== X-Gm-Message-State: ACgBeo1bf2eggz8dAKwcrzvjPVLV5py6YDtjVkcZLOpajl7I7fKXrbzX 59/z+aDmPoX7io5+d1d7z/A= X-Google-Smtp-Source: AA6agR4pFmAGiKXTOeaKFuDcGc9ML9KnnfLVnJWTM87Msbs4Sfz7Oku96RJ7ODDpJDCpN5zI8KBxBg== X-Received: by 2002:a2e:a814:0:b0:25d:915d:35be with SMTP id l20-20020a2ea814000000b0025d915d35bemr6906832ljq.256.1660030353109; Tue, 09 Aug 2022 00:32:33 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:32 -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 v5 6/9] iio: adc: mcp3911: add support for interrupts Date: Tue, 9 Aug 2022 09:36:45 +0200 Message-Id: <20220809073648.167821-7-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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 87ab7e24ab08..ac614030e400 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -30,6 +30,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) @@ -63,6 +64,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); @@ -352,6 +354,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; @@ -415,6 +434,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; @@ -425,6 +453,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 Tue Aug 9 07:36:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939397 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 CA914C25B07 for ; Tue, 9 Aug 2022 07:32:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239425AbiHIHcq (ORCPT ); Tue, 9 Aug 2022 03:32:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239184AbiHIHcj (ORCPT ); Tue, 9 Aug 2022 03:32:39 -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 E6CC421258; Tue, 9 Aug 2022 00:32:36 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id z25so15893783lfr.2; Tue, 09 Aug 2022 00:32:36 -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=DUg/UtKI3lhbBag3pyXoXDGizX7U/1Bs4mZYx2tp8Kc=; b=aR+bvgQhUTsfetzQeHWlaii1W2UeggcAx2yrHxMGTdEAWRGi4iTR7I4ouSyx/2RRhZ rtOMdVxg90ee2PUdGHJ0mOFMDXI5q2fdwjhfAH2EgU6yuvHzVvyPo4vXb4k3wtpyRgq1 kI4bC7pkLeppLMJcqn6jCUCyatHnBdba2iQ1tHOPlq0Lk7zbE8ndyed989sDGAqlDR0l 7W74b5y1bI4nbnqUfL/GT1kfbaT/3Uo7+O+Lv3dzow5Rf1zYFjdPnL+9IDxZfEpj9qYS kuAjUMmmFH8y+aM0lqp77vEGuXlKZqNqU717CXkhq3G2+qcbfB8TTyS5+CG7SGKtXLuW AygQ== 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=DUg/UtKI3lhbBag3pyXoXDGizX7U/1Bs4mZYx2tp8Kc=; b=K0f+OT7fQ/tNsMyzfRISAOkVzzK7koR1N5BB5AoVcvqfHqrbxAlfH3mV1wd6Sox/gb uqZLcOzBZYJNcHDVrOHlKfANTud7BuP02FRH7MfszSeNfyDAH+QWMEbYT0OlJY6Uz0EI iHcL/aX8jz1kOQdfw1Z5dNul/9fA5qQ7k2yTMZZFAHDUNVSfhDQne9/zcJNmyuPSZrq0 h9aO2UYDZFfj/XtLjwUoHS6dXdHbNtFv2JGeSqg20iFXdhxvWnGI7M7BdsyWp5DBKhIu jhlDw1LUlP8XGdvtHCvI4diwfTA5JtgK9FPFchofyGVeZHwStO44wa3BiutAKmrMwIcj +Dlw== X-Gm-Message-State: ACgBeo1C+wNVOaqes6KMQHLpF020wzTw94iZ8jvoloTu2v/5bnbUbmgF K/Zx0OaECGqSdHGrd16wc4o= X-Google-Smtp-Source: AA6agR7ypL5xCbfspKg3LaqVdwfp1e+V1Qn58xCO6kI3Xrvl7ga9kDMJLa5SswtuSuhqs8JOPkaHww== X-Received: by 2002:ac2:46c4:0:b0:48a:ea0e:1bdc with SMTP id p4-20020ac246c4000000b0048aea0e1bdcmr8193730lfo.613.1660030354150; Tue, 09 Aug 2022 00:32:34 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:33 -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 v5 7/9] dt-bindings: iio: adc: mcp3911: add microchip,data-ready-hiz entry Date: Tue, 9 Aug 2022 09:36:46 +0200 Message-Id: <20220809073648.167821-8-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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 Tue Aug 9 07:36:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939398 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 F14E0C25B0E for ; Tue, 9 Aug 2022 07:32:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234661AbiHIHcr (ORCPT ); Tue, 9 Aug 2022 03:32:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239095AbiHIHcj (ORCPT ); Tue, 9 Aug 2022 03:32:39 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28E7C21259; Tue, 9 Aug 2022 00:32:37 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id v10so8652671ljh.9; Tue, 09 Aug 2022 00:32:36 -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=QIOq4IMQabHqc6y9r29v4FayDraSHRsNoNlUdi/9yx0=; b=oY9fyepLV7ROHYvBYRaKwyrBTSBjUR5Dv/LyQelCPK4bhGEXTwQKUGIDSF3viMUkLF CcT1uHokBkaok4gyt/6hBdcLiXrjsEjvM6GqBNiYNQhdq5ZHF1ySTo9jDrB8IC5A8F/F SaBOc/EZKtkjmPrEciqvWPxBc2zbBlmwHq444bHTapror16idZcUactvi5Q9hpyixHYg o3aybmpGprOa3OPhjza/6juSh8nb7oGyEQHMITVxwWWt4wkodyWw47pEW/hnZpfZd+Yr Xlm/2qvgWQq1eSPxaQFroTG8i72Cd7mOqSQSauXc5H8+gtCXv9IO93E2PWKLHVkQLGWF /1IA== 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=QIOq4IMQabHqc6y9r29v4FayDraSHRsNoNlUdi/9yx0=; b=xsSt+t98iUbPmpB/YidblR8NKaMbPOs7vyKgOwFX4M0l5oPdwIGDD1Gm/rHUqWo4C9 xmna+veffH3ETAucNeru0ir7Q1Iht2ZmQfIP6i4enYPtXhbDPMmZFPgeNuSnoc6coiOi bsnmekyugwVmiuFJvztOIsd/OmO7VjkQdn4we/i0Pw4fQUQdPKTAj4jGDd79bafJ7Bfr ckg7+7qNS8APwYhEXU7uuOWtrBGXy8LI0Bp5WZPdbHCIfPeaGVJG00blGN5fvcwjJDhO MCX8i4CcJdnHYMQYZyUpa7hn0u1pI6eJkYmtvm6KJGosPXGN3+bcRhKPmNbaWrb5sBpJ Y0NQ== X-Gm-Message-State: ACgBeo3wVen6NRb38Ci2CsU1DyzzKfa7rCBrhzsEWpk53uTQEWTD6jtc yGaOqt5WRLOfJt+lNB7Yjrfk/dP/mbyckQ== X-Google-Smtp-Source: AA6agR4ctdZFMCLrOswEOPeJSNm/VQu1YwvQqJPpKP9Gk6+NdiayIR2qETaiFAH4+AP/GbYIaud+eQ== X-Received: by 2002:a05:651c:446:b0:25e:5629:213b with SMTP id g6-20020a05651c044600b0025e5629213bmr6628019ljg.53.1660030355132; Tue, 09 Aug 2022 00:32:35 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:34 -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 v5 8/9] iio: adc: mcp3911: add support for oversampling ratio Date: Tue, 9 Aug 2022 09:36:47 +0200 Message-Id: <20220809073648.167821-9-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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 ac614030e400..61b516573207 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -40,6 +40,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 @@ -58,6 +59,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; @@ -116,6 +119,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) @@ -144,6 +177,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) { @@ -203,6 +245,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: @@ -215,9 +268,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, \ @@ -279,6 +335,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 Tue Aug 9 07:36:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12939399 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 6ECD7C25B07 for ; Tue, 9 Aug 2022 07:32:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237530AbiHIHc5 (ORCPT ); Tue, 9 Aug 2022 03:32:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239308AbiHIHcn (ORCPT ); Tue, 9 Aug 2022 03:32:43 -0400 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA20021267; Tue, 9 Aug 2022 00:32:37 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id y23so12107030ljh.12; Tue, 09 Aug 2022 00:32:37 -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=xJYIUaLcJnfA7jR+QMucT/pwh4GYvtALN5i/Jagf/fA=; b=khy3TteSikCWgpJ3E/7/42mmpA3Qc3Kg7mMaxSLA8WK/vcBaxFHdCLfOLTI2PGhB0s yoc03/xmi/KpuhzzD01XKERBCl9MPB7hGo4s2L2+ujRq0teQn9JY7cdm8CVXowHuI92r jQ32coHHwzHYOdT+9HaIIwQ0980BWFaDHuRdkwcYA6+bFbOk4XhULY2/kzlnYaswQdlY TGJBXxv92Cg+cMaVQlkLzjwZgxZt4hQFXdqIT83r3Dx3un5b7gaw+w//6BECsFKni/p4 KyOgRSUSw+qX+j0H66RV7XfH2VntuVNR5PN9TxSHpz9D3oHioYoe7MNdrLqDOhYlTkYo pTXg== 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=xJYIUaLcJnfA7jR+QMucT/pwh4GYvtALN5i/Jagf/fA=; b=OODdcmtHsTPeYk1su0USoYb02b7cFn0e4N0B2uy7DhbHylaBUNa/RpovrTTigAm8Am /AefBdAeLYcu/8Kr/RNrsb2Tj5L9yzMzdfNi41UB1SHEz1ZVpaxfv+loBW1S2MQtui3L iP8r+7fXTnFfZfD3iMT3c5/RrojEMpO/AnVWarP43DHXI4yuOmfViO0xL6XaN9NHCCbH KR6rH/HWnoQEvxXOVgghejxgim7pYIItsqCRCXCb50rByFqXONZ34+TQj9IWGuBDqT+L jtpPcLYd71AXx8B4pKOX7BtafEuIb52TqtRe0Lehg41GNa0qtl5Uqj4obiqyCSf6xLgk oVkg== X-Gm-Message-State: ACgBeo2FVqskNpptK03N+SCFYmSmOH9/FfAGg7xcedTr1GHceNt+/EHX y/xNBrN5RwElyFOX3QCOGDs= X-Google-Smtp-Source: AA6agR4NF0sVbqATqatNT+TK8TtTA+zwj9ggS7BCjr+zRYFl1NxOnhzSKHjx8ioqLhQkym0WlubNzA== X-Received: by 2002:a2e:597:0:b0:25e:4a64:1628 with SMTP id 145-20020a2e0597000000b0025e4a641628mr6791595ljf.378.1660030356126; Tue, 09 Aug 2022 00:32:36 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id k15-20020a05651c10af00b0025d620892cdsm1528911ljn.107.2022.08.09.00.32.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 00:32:35 -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 v5 9/9] iio: adc: mcp3911: add support to set PGA Date: Tue, 9 Aug 2022 09:36:48 +0200 Message-Id: <20220809073648.167821-10-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220809073648.167821-1-marcus.folkesson@gmail.com> References: <20220809073648.167821-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 61b516573207..ed8d0bb11fd6 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -28,6 +28,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) @@ -58,8 +60,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; @@ -68,6 +72,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); @@ -144,6 +149,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; } @@ -188,29 +198,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; } @@ -228,6 +218,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; @@ -263,6 +265,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, \ @@ -272,8 +315,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, \ @@ -501,6 +546,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;