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: 587213 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 8C9BFC433EF for ; Mon, 4 Jul 2022 17:19:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233220AbiGDRTN (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: devicetree@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: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: 587212 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 24DA9CCA479 for ; Mon, 4 Jul 2022 17:19:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234030AbiGDRTQ (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: devicetree@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: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: 587211 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 1D4A8CCA481 for ; Mon, 4 Jul 2022 17:19:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232174AbiGDRTU (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: devicetree@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: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: 587210 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 7CB8ACCA479 for ; Mon, 4 Jul 2022 17:19:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230311AbiGDRTV (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: devicetree@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: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: 587209 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 81685CCA47C for ; Mon, 4 Jul 2022 17:19:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234668AbiGDRTk (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: devicetree@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;