From patchwork Mon Jul 4 17:21:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905713 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 4DF1FCCA479 for ; Mon, 4 Jul 2022 17:19:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233329AbiGDRTN (ORCPT ); Mon, 4 Jul 2022 13:19:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230368AbiGDRTM (ORCPT ); Mon, 4 Jul 2022 13:19:12 -0400 Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53E10C3A; Mon, 4 Jul 2022 10:19:11 -0700 (PDT) Received: by mail-lf1-x130.google.com with SMTP id i18so16753460lfu.8; Mon, 04 Jul 2022 10:19:11 -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=uz1r1MYBHCUxjx9kX5YfhmuLjAg4kDpzXkE88JpxOxc=; b=H1+BqzxpxrdAsvw7O/ibopA4RXDHhXoyTrxCKl8gIG+d8vspZmWQVD5Z5ga41UU4Os 5CaC/o5EaVwY9PSGHYeQC6s47492vdlS1FyvjDl/D2AITQ8FxRi8WhxbPi3GvU25Vd5d U9T026pUZ5CxXhF/S//F8jJYOwTmsuyoPRJfnjVkzAzqVar/t7PtXHgzRidA04X9cH52 lMRrJWlat7j1UTtnkO2P1gmVrcbH4lxvdFzjdig8zO8Qk6SeVJpIC28AHK6PqLVe97/6 211D7QrCE9maJTV3Ohhd68agEWIsMcXTeRpz7gIt7r4FVew585pRGCp7Hv8lyi1fNyIL UycQ== 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=uz1r1MYBHCUxjx9kX5YfhmuLjAg4kDpzXkE88JpxOxc=; b=4WKyT/ar1Bgd3XypieTbj6gHLEu4I2YLiuRiMLGUHiBjf8O1Tlr4pnXOBnOiwWdRE2 rkkKjUHjluqNeDL4stSb/e5kBeqOQLSuPYPjtxaydAXnQidX2zeCYvmebB+iQMWNRn9d VTZc6NqZDrXgc/eVgXzsIaWmBaNtthRPp+6FdFnADbV46MZAws1uzpBHgEwxEuVeGVK7 79O9iEDE36IM3PILurdSyfQe0xVn3ylVDaWpXC9pAIcBQ1jJ8rPVk2ju70bEtQxMpzOE tVVeu/UYM7I+h9y9+q7s10USKjfTgU3FV92ma+w+vF9OwD/U7Lx6+IKUX8AV8ihMonEP 3OqA== X-Gm-Message-State: AJIora9znac+bHimnZWXDWXCsCySl2gtBiby5chOIEaj6p/G88p4WLF7 Jk8a7XcnKPdhxGccPTCnnU0= X-Google-Smtp-Source: AGRyM1vMndnS/8hs28jgccJsv8T0iI4Zh0odFQZqziQpNHxSwflURHrGChgGiQ+grNUMVMw87Rp4LA== X-Received: by 2002:ac2:4896:0:b0:47f:773e:242c with SMTP id x22-20020ac24896000000b0047f773e242cmr20697024lfc.79.1656955149560; Mon, 04 Jul 2022 10:19:09 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:08 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/9] iio: adc: mcp3911: make use of the sign bit Date: Mon, 4 Jul 2022 19:21:08 +0200 Message-Id: <20220704172116.195841-2-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 --- 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 Mon Jul 4 17:21:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905714 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 3A5EBCCA479 for ; Mon, 4 Jul 2022 17:19:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233345AbiGDRTO (ORCPT ); Mon, 4 Jul 2022 13:19:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231582AbiGDRTN (ORCPT ); Mon, 4 Jul 2022 13:19:13 -0400 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68314C10; Mon, 4 Jul 2022 10:19:12 -0700 (PDT) Received: by mail-lf1-x135.google.com with SMTP id j21so16827591lfe.1; Mon, 04 Jul 2022 10:19: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=/dGiAySADJtdpNfaNk5czDU+zGxujA+xtyshoy0uOj4=; b=YGaNspCkh42qYBUAe1smIUHSD5+/5+V/CSRBievEv11ejq8y/Kx+FIK8a8ioxJysOt byADy2L4kmCo8hFjfouqMXZfueVzrzo8AxTHDz/ozcgI8kxotLiKjbHXpEyH0uxP+trS SMWeEcjz0QmRjgai9TYZNxxzo0ouCn/JglFqrGmgsRecoKXD2fWRgHEp88bsdKl1eyye 8DY5NjBQmUSUd5p8J+TFDa2619NewSPJJc5wGIhRsotp9IW9t1AYg3PCWaFZAyY8chVo yqmtBjWVk5K7Pip/ncgEygdbdkysrymC4buuqlGKMKGr8QqjFotGJLXf1I5L2joStcjX OrSw== 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=/dGiAySADJtdpNfaNk5czDU+zGxujA+xtyshoy0uOj4=; b=3UDQp2FNfHZW5Iy6TWIemuPb/4hsplbDOsG83xCvUJ8W5e/1j0ogG6+EZsiZ3YP2KF zGuuOjbk+0/024Agu7IHLbK34jBxiEpATl03dbZ7at19/NtjSFwyCIHeyLqtYdY484om AJLV8QOhUNZyy1KAg+vD4gJb9UJsRyvzfXpfYymF1634iRx638GhLjNiJGYLEJSDY9tM EJ0KkfY5+JefyAmOhyI6bEAQn8R60hKy0AR2CbtF8iPKqu+wrsKmZt7+6IBpQJfFs3gb 4g3awul9LIfNOdw9vfKqNQG8LWRE1jGDwim9BVWZVUeQgAVopD7cE6+H04xHr+sxims8 Z6vw== X-Gm-Message-State: AJIora/sXSL+cFr4aW6JHVdSLxA6gtXXyk2cJhbiR7NzPJHUxx/MqC57 aCelaALMqNcrvVLUlTX17GA= X-Google-Smtp-Source: AGRyM1v/3+cc+tWPlvkc/x+ppkLi0CNDASgDkMsPTDG2HwoizZolD8U5wCtOVBcQStB8fSJRJn7ZMQ== X-Received: by 2002:a05:6512:22c3:b0:47f:9396:861a with SMTP id g3-20020a05651222c300b0047f9396861amr20574581lfu.230.1656955150819; Mon, 04 Jul 2022 10:19:10 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:10 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/9] iio: adc: mcp3911: correct "microchip,device-addr" property Date: Mon, 4 Jul 2022 19:21:09 +0200 Message-Id: <20220704172116.195841-3-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 --- 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 Mon Jul 4 17:21:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905715 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 0F953C433EF for ; Mon, 4 Jul 2022 17:19:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234022AbiGDRTQ (ORCPT ); Mon, 4 Jul 2022 13:19:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229812AbiGDRTO (ORCPT ); Mon, 4 Jul 2022 13:19:14 -0400 Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83305C40; Mon, 4 Jul 2022 10:19:13 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id t25so16767021lfg.7; Mon, 04 Jul 2022 10:19:13 -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=P3iLMWXKaK4OETrzqj817kmzJiQmAFyABnf3Lx/BATM=; b=o36lKSXE5dV6YeHDcu6+aT70KGbnlzBB/WyHUS2O4iHqc1a0gGIpnUXB02QlB8LGlo bRplTe02AZCH7c1+QGGxqtHkDsAt9NCeAFavbxIr/an504ksNxkSZGz9Bej9iTwdxzdE LawqEpapxbb096jnn342x8GXP8aTr3tG2EX06AfgZaaRiMnSjIhPFQR+afuRQTFmEwhQ 1sO2LjikVhXJSxSuDFElWXl2YWXLp71uwZBCn7fFwL/fc9hB2s4djxQepMxOUbSkIpzv g6bLiyvgZNVy1B5BNdWdzFQt8yldgAV5NfyNoF9Waag7p+MQ0JMgSnb8yNj+cX2+H57L 7C3w== 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=P3iLMWXKaK4OETrzqj817kmzJiQmAFyABnf3Lx/BATM=; b=YhCOv+G7GZ1psFpwluLYscEHHYgwOpsLGv0nUo30SFOFPgGqIuxPe99eOIa52aq6Hg oQ7YSPtYKmuaq7ODEv8gjjGCvnq8vdfSNY2PKzV2O1x5mNezQsen2wpsaghzdKsbGB/9 8dIeRpjDEMtcncsvSdWIZnemyd+p9fRfQbAusKjBXY5sWZDLP1Blp9M7jl749gy3rXl2 ZNKSOUcjPAwUIBPgMvz1YiTm7wG3pyUQ0MVsd5xNLEszkUWWFEmQKbjVeHxXAWZ4c71o tHf6HJQOav8sfxp9ys4/213Fzogs2wsiTcJLYzCyts91OC+FweF4cH5v7yoIXIdo4Rz+ 0r6g== X-Gm-Message-State: AJIora8AuWLsBlcNps2MHZpi6sc7k7gajskJ1xbXF5JvEV0hB48dxSF2 K9esmXYfvOGmow1krOuxFdk= X-Google-Smtp-Source: AGRyM1vTlowbru3uEqOCa5DQTi/XPqqbzfMUgzGK/exiKz/jVMmvW0C8wOA/Od145tYXRgYZNIzM+Q== X-Received: by 2002:ac2:4d82:0:b0:47f:aff4:3684 with SMTP id g2-20020ac24d82000000b0047faff43684mr20092398lfe.557.1656955151922; Mon, 04 Jul 2022 10:19: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 v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:11 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/9] iio: adc: mcp3911: use correct formula for AD conversion Date: Mon, 4 Jul 2022 19:21:10 +0200 Message-Id: <20220704172116.195841-4-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 Acked-by: Jonathan Cameron --- 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 Mon Jul 4 17:21:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905716 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 816DCC43334 for ; Mon, 4 Jul 2022 17:19:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234154AbiGDRTT (ORCPT ); Mon, 4 Jul 2022 13:19:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233830AbiGDRTQ (ORCPT ); Mon, 4 Jul 2022 13:19:16 -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 A1B02F3A; Mon, 4 Jul 2022 10:19:14 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id i17so7012517ljj.12; Mon, 04 Jul 2022 10:19: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=9IMnOPFsjcxK/PLm5RmjMuxbEUfs1z1kxBu9661T4Q8=; b=agQrv+IFskuEeNO6CZahMRE8TjZedWOEN+E616LSBJkd9Ndgrx5W2Q6Q7LTcZ0bf2T GDntNiPMvG7HrRILqcb2VNF3gq0Nsb9IdbE+bCVZB/mAwnRAXmTZnWMnqzrajdOcZchj 42a/wn+cenpTWVCHt+0tSMKqiHH8RhDuM1EOWFndJD65hSajF+exD074KoowPHvo0g18 kbQYHLbzYsiaOMlu6FDYC5LHlzU/Ex59ThL7yQDxldndYYjTcl38fNFffWHRFL5aYhho SiCzV6QDcUdidjuJmQLUeXYKdB2iS2U3QejmhV8LlBlaR0U5e97dWxwXhz+jeAvI2Ou1 qOfQ== 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=9IMnOPFsjcxK/PLm5RmjMuxbEUfs1z1kxBu9661T4Q8=; b=aK9BDn4cDzlfxAHz2pRMJSP4O70uURvjdc6g+avq0PHQJ8OmkgiQPeU4hUHdjWF1jP KCkdFVHIvQOBS7CpYaG3KE5CXizKn/QUkwQeWcmMA1ikiN8kl2WOoTpgq5PWmlbZXjV7 8zFb+7LXOY/0/yox94hOBt/YVF25n4E6hO8Vb06QkbA+JXdt+nLmqLMPz0tHd3zvqb7r JfdacsbwFVcUn0HTpBpFKbRmw7L0sAshf7ZOA4BkBQPChSITS33o45+7q1HUIcM2L6sP sfq00FX+ss4YuGUMCySluBYoePTohO8G18l/KJhexN1pyYkW+7QgNgD0nkaQNFULeR4V MQXQ== X-Gm-Message-State: AJIora8w+e+7E4pWSkUT6vHAD4Jbv/Q2Vg6N3Yaz5qVKweVTexDWIddO /mW9335CANLT3O97p14y9MI/SB36lqE= X-Google-Smtp-Source: AGRyM1sXf/svu0IaP74xq8Lk17RSxFDMlJad09RVQdWT5jEzJrs0U98VG6hhRgmz9ubcjazIyKHeTA== X-Received: by 2002:a05:651c:211e:b0:25a:88ca:d766 with SMTP id a30-20020a05651c211e00b0025a88cad766mr18105554ljq.230.1656955153044; Mon, 04 Jul 2022 10:19: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 v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:12 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 4/9] iio: adc: mcp3911: use resource-managed version of iio_device_register Date: Mon, 4 Jul 2022 19:21:11 +0200 Message-Id: <20220704172116.195841-5-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 --- drivers/iio/adc/mcp3911.c | 43 ++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 890af7dca62d..f5fd9da6ab55 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -258,6 +258,16 @@ static int mcp3911_config(struct mcp3911 *adc) return mcp3911_write(adc, MCP3911_REG_CONFIG, configreg, 2); } +static void mcp3911_cleanup(void *_adc) +{ + struct mcp3911 *adc = _adc; + + if (adc->clki) + clk_disable_unprepare(adc->clki); + if (adc->vref) + regulator_disable(adc->vref); +} + static int mcp3911_probe(struct spi_device *spi) { struct iio_dev *indio_dev; @@ -271,6 +281,10 @@ static int mcp3911_probe(struct spi_device *spi) adc = iio_priv(indio_dev); adc->spi = spi; + ret = devm_add_action_or_reset(&spi->dev, mcp3911_cleanup, adc); + if (ret) + return ret; + adc->vref = devm_regulator_get_optional(&adc->spi->dev, "vref"); if (IS_ERR(adc->vref)) { if (PTR_ERR(adc->vref) == -ENODEV) { @@ -296,21 +310,20 @@ 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 = 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 +335,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 Mon Jul 4 17:21:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905717 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 0C0D8CCA479 for ; Mon, 4 Jul 2022 17:19:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234072AbiGDRTU (ORCPT ); Mon, 4 Jul 2022 13:19:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234090AbiGDRTR (ORCPT ); Mon, 4 Jul 2022 13:19:17 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1916E26E3; Mon, 4 Jul 2022 10:19:15 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id z13so16713792lfj.13; Mon, 04 Jul 2022 10:19:15 -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=zNIj3LpkA8AS8doXHlRfvjaWK2oNi27ElwWZcfgxMbE=; b=H+QWJDolEciQaI2ggUP+L8jsOsuXR8sBjQLxjeaVJw5OF8LoiQqILWmMgZQCICNmJV eHomv/K8MUDI8HG+dSuCgGpOO2UB8lsY/WmALHg5FxOWoecC+WxNTTKE8h5D1PsFJCF6 E67xeqN0JKs3WK+5pZYqbkCSmr/po1Kyh4kp2zwfv0QCd50xnJkPaGoDSMiPuak3YJ0R NRniJXG8hPsPrYW/7vS+FGAOEFsZHomFLWvBRpYzCn/uNS8sR+K8+ogM/aS3Kgo3xaOt HFNKDQsg2RvUQo5A81hjszqCo6V9/0OZxEhfJn7aZAuqPkPI6260/2/LX7fiWHoj8G/I qRaw== 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=zNIj3LpkA8AS8doXHlRfvjaWK2oNi27ElwWZcfgxMbE=; b=1RJLciqUP6ma5kk7gF0JWqJ/5ZKhUAMQZh02JCeDZZU1zgQjTgvgRryaTwrqYyyUFe civJl/AFi4d4GlgiKOsepeOcCPQpssmeerkAFBaPUM7OxpFd9kqtK2VVJus+qwXt1xMY FJigNSwUyUdnTiGGTKonWNybjZGjZpQaDGz4jnh72QaJpSgVsjxCOEoeuRDy6OPw2HDQ suDwXknNGspGBWQ52Qv9yFqSn1vZ8ARfsVD/8UKD5svN/Bsh2qX2uFPvGXdjJcoW72rt 3b6ayBvvw66ckhtNAYCNlb4afsFVHGt8EuxAeQDlwLfvqdUPMtaPO6Fih3sG2pJbdy3F SQ3w== X-Gm-Message-State: AJIora8hE3b5keHnSFw2d0jRXM+h7HPGZSnWcbISbjkrPOLdBydAaDPp yXGyriyhOIXaBM3VTC3ucc4= X-Google-Smtp-Source: AGRyM1spgvgl60EAao6HETeL4DgG3bNDQcbxap9wbMLzZomffILM1CJB32L3LQ8wyZm3W2/tgoZxsQ== X-Received: by 2002:a05:6512:3b08:b0:481:1b90:bf55 with SMTP id f8-20020a0565123b0800b004811b90bf55mr20108627lfv.153.1656955154249; Mon, 04 Jul 2022 10:19:14 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:13 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 5/9] iio: adc: mcp3911: add support for buffers Date: Mon, 4 Jul 2022 19:21:12 +0200 Message-Id: <20220704172116.195841-6-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 --- drivers/iio/adc/mcp3911.c | 99 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 7 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index f5fd9da6ab55..cf4dacc7de06 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -5,10 +5,16 @@ * Copyright (C) 2018 Marcus Folkesson * Copyright (C) 2018 Kent Gustavsson */ +#include +#include #include #include #include #include +#include +#include +#include +#include #include #include #include @@ -22,6 +28,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 +61,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 +210,66 @@ 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, \ + }, \ } 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] = adc->rx_buf[scan_chan->channel * 3 + 0] << 16 | + adc->rx_buf[scan_chan->channel * 3 + 1] << 8 | + adc->rx_buf[scan_chan->channel * 3 + 2] << 0; + + 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 +278,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 +297,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(void *_adc) @@ -325,6 +403,7 @@ static int mcp3911_probe(struct spi_device *spi) if (ret) return ret; + indio_dev->name = spi_get_device_id(spi)->name; indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->info = &mcp3911_info; @@ -335,6 +414,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 Mon Jul 4 17:21:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905718 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 93228C433EF for ; Mon, 4 Jul 2022 17:19:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234183AbiGDRTU (ORCPT ); Mon, 4 Jul 2022 13:19:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234129AbiGDRTS (ORCPT ); Mon, 4 Jul 2022 13:19:18 -0400 Received: from mail-lj1-x235.google.com (mail-lj1-x235.google.com [IPv6:2a00:1450:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 165285F4E; Mon, 4 Jul 2022 10:19:17 -0700 (PDT) Received: by mail-lj1-x235.google.com with SMTP id c15so11849801ljr.0; Mon, 04 Jul 2022 10:19: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=KGaeZMwPFEZ/IA3EvMTSeCiC4FG4w1jQa0acswAkwmE=; b=ZwKDawghrksnfeMhJaebVAg7GK9lGYBL/Y02gDNIHl6Ts3AXrNB9qZK7knPbUQ6Kd6 SvAKSP3vzKZD87UFmJURJ/MzRn4gEuYJaiAYKguZXwKuEclaeSnUuqJ+47rmZ+8ZEF9q T+IsctVRzQS57zhBL31+4vjoGpge+D4rdyCzFatJlKLXjz8usn7JELeZ6TM/ROHn2smK anv2vlTytIMYpFbrzPZTqiITg+24X24t/79urHqo5LSRUquOj8rUCiaQlQfyfD3hOXRQ mITi3jTZadiz4xYYYLQ3KTqTftKx1pavB308XmSJLR5wwg8JwldVaAbCx2dIq9riAzmi 4nhg== 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=KGaeZMwPFEZ/IA3EvMTSeCiC4FG4w1jQa0acswAkwmE=; b=npskJwbq79WVATHdRFcO5xFuJkOSL8Df1MWraZxpp+xkU8FzjmhUgo+cw+7d1/3+u7 kClWyi+YzD1uhWCRvh10C9Af1Un2EhBFAErkFo7ysp+dAq7LRFfbb33QRn3nNpLqXmGj /2Z6bzP97OBOAIdL6RchWDF4F47osLhMEcdgLwmGAf4zxy6F3BsWFGZN54zMat65EaKa fROmcOBDCm2+ZAuGJ6gJtfV0KWzxi/51auQbNRKq1T8gtgWF1RpHAqbPpG9xuUNSMHa+ G1LXjBCuxSy+A7qznH/Ud1j16EcIR8hWD0YgjaEPMcKo60t795O4pNlpvOl3+zwJMtn5 5wMQ== X-Gm-Message-State: AJIora8Bz0ZZyGnGs5YwkfjRiyZY5j0LRsfmPMqlW/ETqtn4gLakkQO+ lNoyxDSnPzZD918/ySOBvkg= X-Google-Smtp-Source: AGRyM1tA2Fc5VoSjZLRdAqFLHt2TJixeZOb+1OQjjPrrcsLX2gEJv4DCVAaIbfq6SJmFGwcUKEppKg== X-Received: by 2002:a2e:9004:0:b0:25a:7c03:eb73 with SMTP id h4-20020a2e9004000000b0025a7c03eb73mr16960444ljg.470.1656955155394; Mon, 04 Jul 2022 10:19: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 v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:14 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 6/9] iio: adc: mcp3911: add support for interrupts Date: Mon, 4 Jul 2022 19:21:13 +0200 Message-Id: <20220704172116.195841-7-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 --- drivers/iio/adc/mcp3911.c | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index cf4dacc7de06..9c6f456bdbdf 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -28,6 +28,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) @@ -61,6 +62,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(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; @@ -403,6 +422,14 @@ 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; @@ -414,6 +441,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 Mon Jul 4 17:21:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905719 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 28B76CCA47C for ; Mon, 4 Jul 2022 17:19:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231582AbiGDRTV (ORCPT ); Mon, 4 Jul 2022 13:19:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233920AbiGDRTT (ORCPT ); Mon, 4 Jul 2022 13:19:19 -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 4874126E2; Mon, 4 Jul 2022 10:19:18 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id a13so16749149lfr.10; Mon, 04 Jul 2022 10:19:18 -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=pvGal0LOB0yFzBlXFuxaiIecuEofk4mw2GbDEFA2PXk0x5lAB6OcvGwLpRVHV+zIdl qBikG+v7m3I9suYueNHdMoJPJlbsA8Xs29FskxFqHrxmwDYi+CuLWitoaG6xeD5WxMdI MwewFaV7iTLnWv41OfAm865T722mRxBOWbOfFhcEtlcSpFsRTTD9Jt0iaXVutMZjupyw EYEW/Eao2jwMm/HjQ3z9UNd5/h1EkEaahZyJchzU1dG9nRUUjEhspp+08bz2KlMbO82r /C9pPsod/MstNUq8Pv+zSUjhgp3k80+6AWyTfVUax8YoI5rsKJwVnhWv2CjwBu0jICRZ QnXQ== 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=rXPVugCHuc+KsqyjFwyzd2l0PT1irikX+OmhDHkta6x/rAXunBXTQ4ahDoDkm2kxPm w6JL4uF80tXYy0uWINwGaKZaTtKnHjGwCDdztIWDjR8OBRKx7BF0zGa5qL+LRfymQWMr FCJSuwI80RI5UAbXxYT2rH1RKlJ/0rnAAqC/drmhJBx7CHC2RL9Gv8rGKE2JdQWyDs/h bQxIzA9LsbJKcR3rWi02RgDeIS4WLaJQY8vlAt1y3EB9ONq9hrRsDTWhpewnQ0CY+86C ve9gyjHZSfdPxqxdNA3N23Ad/U/tFGjEr48e278rRFyhc3UwnsDrg3zAran49oudxL8K D4/A== X-Gm-Message-State: AJIora/4qR5s2xKdsKYBdSwh6eBHnEi8lryHvy+ZqPNiCkl9VY2aDBuy sCqOhXMr31F/NjnvntKLnA4= X-Google-Smtp-Source: AGRyM1vKCFfMSZh0twn9oJ/TAoHFCQ2yOMpSU9QlGuqHVsWr/cIRujNV4AHSk70itMi7TadPBYbexw== X-Received: by 2002:a05:6512:c09:b0:47f:bd92:4181 with SMTP id z9-20020a0565120c0900b0047fbd924181mr20050191lfu.470.1656955156496; Mon, 04 Jul 2022 10:19:16 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:15 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Krzysztof Kozlowski Subject: [PATCH v3 7/9] dt-bindings: iio: adc: mcp3911: add microchip,data-ready-hiz entry Date: Mon, 4 Jul 2022 19:21:14 +0200 Message-Id: <20220704172116.195841-8-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 Mon Jul 4 17:21:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905720 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 7C5FAC43334 for ; Mon, 4 Jul 2022 17:19:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234604AbiGDRTi (ORCPT ); Mon, 4 Jul 2022 13:19:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234191AbiGDRTV (ORCPT ); Mon, 4 Jul 2022 13:19:21 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1B3BF3A; Mon, 4 Jul 2022 10:19:19 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id t19so16129398lfl.5; Mon, 04 Jul 2022 10:19:19 -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=A1ZkiED2+naYPTbg6tDbNhI0CZl87H54p76rxmGjur0=; b=Ha1Y2dyxxOT4DBC8XbeZFnWfynbiBmOAVZU+RTnbfh9ajcXuJHmxs8LnJ+puEHBFRg or7pgKmU6X2p+ReEpk+1Gaqdu6VBRBfNsTHxX99fJfD3fpDuftjOk+VMyE63uYytBfTi pB9qnn7fJGO5sm5Ta3twSZWyP4UsoVZNtJyOzNGt47B8pNz//dDct3elyVJ+Y+w2SgB0 FvIOGF9L36dQ/SiNkLg+F7Jp6ZO5owQxb1ZNnxiCilNjzRC+w0pZFNB88zzzPi10Io5E 0NTVgy9nSn7hdwMCsqjLxFyXDA3CnnVYgdhqTzZz6LA20z+T9b+P/Y5z72xSpbZ/wwEO zMnw== 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=A1ZkiED2+naYPTbg6tDbNhI0CZl87H54p76rxmGjur0=; b=oWj53RiNleAcyu8nSdn6kBhMHxOfsw9QX1ccQgpzfx93R2gXes/aFPJJK53cpaymjA OX5vhyjUfZkilCow4nOCj1sexY5W0Vqygse/bO233/B6Csx3tx6VtgxR9Oe9qCD962q4 FO+Sfe4VGAE3oikwtHNlQitjW7f49S0bhAI5j8hsr0GmbwrOzxtLpZKaRKqXvmLlN38u qR0VTQELYGt3LJGMTZh8OKUbAm5y0lpOtRlXT2qxMnEk/ZbKQqkQohftb0P9B+n+hUXU cqfzY0AD74ZfsC7e1m7Z3WAF5RvZNN610o7Q6prRMh49LWDU4T2vNpmbqUZveYZi3j1q 2Hlg== X-Gm-Message-State: AJIora/N/FsthxZk8TwwcQqDHxbPs8GEG2T7el7AB3Mr8VfmZ5gL0YGa P+IqffluKYnswO+ijQw13C0= X-Google-Smtp-Source: AGRyM1ur0m9Hod7r2mN8CTdCWJGjINIgT+1pmkFhPKv1ZFFK5n44oYzfKXTS1sUYTwGFKrS36CIsQw== X-Received: by 2002:a05:6512:61:b0:47f:7f37:fad3 with SMTP id i1-20020a056512006100b0047f7f37fad3mr20109181lfo.318.1656955157735; Mon, 04 Jul 2022 10:19:17 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:17 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 8/9] iio: adc: mcp3911: add support for oversampling ratio Date: Mon, 4 Jul 2022 19:21:15 +0200 Message-Id: <20220704172116.195841-9-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 --- drivers/iio/adc/mcp3911.c | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 9c6f456bdbdf..c6e30471bb81 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -38,6 +38,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 @@ -56,6 +57,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; @@ -114,6 +117,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) @@ -142,6 +175,16 @@ 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) { @@ -201,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: @@ -213,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, \ @@ -275,6 +332,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 Mon Jul 4 17:21:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12905721 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 D062BC433EF for ; Mon, 4 Jul 2022 17:19:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234661AbiGDRTk (ORCPT ); Mon, 4 Jul 2022 13:19:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234512AbiGDRTf (ORCPT ); Mon, 4 Jul 2022 13:19:35 -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 9CEE45F4E; Mon, 4 Jul 2022 10:19:20 -0700 (PDT) Received: by mail-lj1-x230.google.com with SMTP id bx13so11806066ljb.1; Mon, 04 Jul 2022 10:19:20 -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=c0RRFr1/0VSC8RisYdALgOp+0KHQIKOc91K1b7cS78g=; b=N16SsQyUfckM3WZLSBGwPGuxRWBhO6mS2DVXLofseYBcqHLhmroRKdvSMPxWasNmo6 SlSLtP5AOBcTb7JwDcHy0cPJq9NRVWkLwPdsVbmQdT7jaZDLs/ZuRcgGXJXom0XwihO/ qnIFCSRImrMh6HEdwicUPCz9Y2QKy+aM51SavU5aRUDWRqdMwI4uWT9N/JNREW5VtfMW yBAHrno703z/6f54xdvmtQhu4bauaSeUpVA6CHXKjcEmApKAvx4dNQxUzx6DhP++Duro kTmSD+RGtOumUkVYwP7rEz/+uzI8q5VUH35S92DJxT2QkmT0ULCXfpYqKm6hb+Z7CfHu Wgfg== 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=c0RRFr1/0VSC8RisYdALgOp+0KHQIKOc91K1b7cS78g=; b=VOk3hMwHPWTjAKLxzkDtp6Ff0bRcDeUxkEfT2fBdzcjm6JYKwaufRjX3VIfnsmJG4V 6Jat5urFEbA+u2CiAbsBbEUbbauFzD14ugAfb7Mym1vlhRjxvjOg2fMS3B1LETURnhYo k3/CoZz8f5bG9z1kpU86U28BAUMuzOdgzdZu2FsXkk4RtOZjff7IQWs026f0lMgICTn9 6kuK0cfplN5p3lBrhSLMWQhc3+eDi4Ye4LIq6RAzJYeCQqMCgZ2iT2XURv+Ah9paGQcl bRJKMLZwAcLo2o1gAtOULAdgk1LFx8w8wuiAU1Jr6aULvf3hu2TkAhIAzX653CYwbYvC VBcw== X-Gm-Message-State: AJIora+VojNrqtOemS2bKq0SDzkWD/biJ0n++YroKy2dIeH/QCbK/p+I o+O+vSMrDWOp9j2FBTKVK7s= X-Google-Smtp-Source: AGRyM1vUWq3+5TuOrMqtYE1BEhYGOdDbn5YOTKAHx4N1qf8RUwNOeD2jbUSrEVh0tFdTxNxa/mK90w== X-Received: by 2002:a05:651c:1699:b0:25b:c598:3803 with SMTP id bd25-20020a05651c169900b0025bc5983803mr17569254ljb.0.1656955158926; Mon, 04 Jul 2022 10:19:18 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id v9-20020a2ea609000000b0025bf6099cdbsm2772720ljp.78.2022.07.04.10.19.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 10:19:18 -0700 (PDT) From: Marcus Folkesson To: Marcus Folkesson , Kent Gustavsson , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 9/9] iio: adc: mcp3911: add support to set PGA Date: Mon, 4 Jul 2022 19:21:16 +0200 Message-Id: <20220704172116.195841-10-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220704172116.195841-1-marcus.folkesson@gmail.com> References: <20220704172116.195841-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 --- 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 c6e30471bb81..7fc53f0057d6 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -26,6 +26,8 @@ #define MCP3911_REG_MOD 0x06 #define MCP3911_REG_PHASE 0x07 #define MCP3911_REG_GAIN 0x09 +#define MCP3911_GAIN_MASK(ch) (0x7 << 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) @@ -56,8 +58,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; @@ -66,6 +70,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); @@ -142,6 +147,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 24bit 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, \ @@ -490,6 +535,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 a known value (1) */ + 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;