From patchwork Sat Jun 25 10:38: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: 12895301 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 2B3A7C43334 for ; Sat, 25 Jun 2022 10:37:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232297AbiFYKhV (ORCPT ); Sat, 25 Jun 2022 06:37:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231287AbiFYKhU (ORCPT ); Sat, 25 Jun 2022 06:37:20 -0400 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 287AA2A255; Sat, 25 Jun 2022 03:37:19 -0700 (PDT) Received: by mail-lj1-x22d.google.com with SMTP id r9so193514ljp.9; Sat, 25 Jun 2022 03:37: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:mime-version :content-transfer-encoding; bh=p74EiAOrPkTvHsU80Gm6F1ejn2Q2dF5pBSqgM8W6O8g=; b=Kvo0fglBTj0/ELNAGvVtCWnzgWVkDUGRwavsjI+Z1C2J8G1iiPUBQrUDtjDFfnS6DI WXhW3+jl5RGEzCsWPistAArfqXHjaBztUMA75lJoTJS2DThF7aoJltWZGG2swjlG9zUU b6Jx7BjRgfH9BetMqutckV+TLzqQ5qgp30iCctoGnnoYJ3PInwXmG89faMBRCFDkKTqb d+vvXNVUpHMnWPjhae43agN6WjUjMaRt6aRT7Xk5FuK3aH/cNeLJYRPKaSNS6y6GR6Og rlCN26rO4hHoJRMnfM52TCKADZv2xVPWzut6jOwkUKGVv3gebpTrxE9ReABorpyXtzSP RQgQ== 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:mime-version :content-transfer-encoding; bh=p74EiAOrPkTvHsU80Gm6F1ejn2Q2dF5pBSqgM8W6O8g=; b=YJdXVv4swG4Rbm3Ty4D8lLTfKhnir2ksbtlSb1mchWlmaLTlI/SOWNkn3ySf5Wt+pa XLvwiXPFOtGqWbZS6vZNsq4n1vaX8LQUE0CkJ3nayzT8eCQPpgPcberEZzN702pjr3xE rMfJGNjZntRaJ4J+eEsvQjOQXE8YF7V2W1jzDRbEEh1ym43/fC1wQlYoAWKFvnZCczcC JJiCM/1rNjEhlpcr76kcwt2L20j+8FdD8TuAxArqFI+1uXqKHTvEgH8rFqo/BMRU6kMQ EjVd0ioGFQnNtOMWC8qZH+DBEUaCAlTOyCihlWuauAFNTlAKN91uubbKzHhk5RgYmugD xDzQ== X-Gm-Message-State: AJIora9y5bedwOZaS4ul+ym1El57bDpXP1iYfb1hl2BydLoQpbJW2FoR NbSCH14m4yod6WWEqEczluYiv9gvACZC5QTU X-Google-Smtp-Source: AGRyM1uXChu1I/cl858gf+d6K/L0j/uYWpeDmUCmU93MP0j1pfBCh4E4Y9vvCAk368dg1dxykRCRQg== X-Received: by 2002:a05:651c:1202:b0:25a:6096:bd7e with SMTP id i2-20020a05651c120200b0025a6096bd7emr1617796lja.304.1656153437330; Sat, 25 Jun 2022 03:37: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 z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:16 -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 v2 01/10] iio: adc: mcp3911: correct "microchip,device-addr" property Date: Sat, 25 Jun 2022 12:38:44 +0200 Message-Id: <20220625103853.2470346-1-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 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. Signed-off-by: Marcus Folkesson --- Notes: v2: - Fallback to "device-addr" due to compatibility (Andy Shevchenko) drivers/iio/adc/mcp3911.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 1cb4590fe412..c5a0f19d7834 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -208,7 +208,13 @@ 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 Sat Jun 25 10:38: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: 12895302 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 9AE3FCCA47F for ; Sat, 25 Jun 2022 10:37:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232388AbiFYKhV (ORCPT ); Sat, 25 Jun 2022 06:37:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232118AbiFYKhV (ORCPT ); Sat, 25 Jun 2022 06:37:21 -0400 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ED812A266; Sat, 25 Jun 2022 03:37:20 -0700 (PDT) Received: by mail-lf1-x12d.google.com with SMTP id w20so8523900lfa.11; Sat, 25 Jun 2022 03:37: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=FhFEJJvWGyZnS2sVrWZb7NGdcDQyK2DU7A6T6gb2WJA=; b=RzwHycqWheYC+noCEWZ3pWrvopMwyQEoy5HU95C3GvBesC2Yw8xgEz/2D5REAw/Mxl XHwCZXJwQ8qrtgs11qKmIquWIlRzd4JYXRkNveyBXK7ecGjvtnzPsKg2QTQHJCd7UaRW 7DQFN80XFv17tmYLYVnE+DXR4wOjY2mAgEDoGei3w8ipXMTPFaQccWPYVK/d0pGUh3wV DcbVy8zdU6HgXiCUMGRm/9tcIwgZR8FlwvZr1dWg0p/YWDrHNn+k6XgXaatqJpoE4A7k 3nrd0ws5gfU41oTToUvROlrqWMkhR83dAbIImO2NdshEWtKUB8aWbhku+kT1z1ul1/4G e26Q== 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=FhFEJJvWGyZnS2sVrWZb7NGdcDQyK2DU7A6T6gb2WJA=; b=SMkEMtOQmgTg32kctfi8qwjivyvwA1Cu109+fKBik1HqY8UXJwv2OIMdzXOmR8l1VC bPH98e18tGmOQu9QrFx3HLP9nm6yVBStUwOuEtLuSp0Gy6D3T39k5+EQZdU0te05EqKz 3mgF1uWPNP3MD+zVVanjDicVHz2pNNqSg/sLiMdMzNHJASbcOQ3cSs7TJ4utJ7i3w1uy 29Hm7sPNb9iFvAMUlxpWh5teUwvSOs7cwFBQMyS3sZtaFgIWG3GEFLv4pSfj194qWKhQ GI0ulhqHD5TM45i/ctaok7o/GTLB2tkvShZdub1GBe6989SanZ8yWHBcGOH/Qys2cEvO KNhg== X-Gm-Message-State: AJIora/FC4YQfuNwe28ww1+QKfKeyBRmyucKSDQShCmmlczX5OSSpKGn PJ7reASm5qD91vqkUC+Ptl4= X-Google-Smtp-Source: AGRyM1skwcUgga5EQlzbu4D1tihnETa2VYbRZh4HYXXTOIZ23nt4jdOxqkv3egu2H1ieohMK7dY4WQ== X-Received: by 2002:a05:6512:4c3:b0:47f:6f6e:a7e7 with SMTP id w3-20020a05651204c300b0047f6f6ea7e7mr2209711lfq.674.1656153438771; Sat, 25 Jun 2022 03:37: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 z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37: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 v2 02/10] iio: adc: mcp3911: use resource-managed version of iio_device_register Date: Sat, 25 Jun 2022 12:38:45 +0200 Message-Id: <20220625103853.2470346-2-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-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 --- Notes: v2: - No changes drivers/iio/adc/mcp3911.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index c5a0f19d7834..25a235cce56c 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -312,7 +312,7 @@ static int mcp3911_probe(struct spi_device *spi) mutex_init(&adc->lock); - ret = iio_device_register(indio_dev); + ret = devm_iio_device_register(&adc->spi->dev, indio_dev); if (ret) goto clk_disable; @@ -332,8 +332,6 @@ 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); From patchwork Sat Jun 25 10:38: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: 12895303 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 096C4C43334 for ; Sat, 25 Jun 2022 10:37:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232449AbiFYKhZ (ORCPT ); Sat, 25 Jun 2022 06:37:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232277AbiFYKhX (ORCPT ); Sat, 25 Jun 2022 06:37:23 -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 DFDB42A266; Sat, 25 Jun 2022 03:37:22 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id x3so8601745lfd.2; Sat, 25 Jun 2022 03:37:22 -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=DtWFlPGPjVSnMZ4erXWfPBR06ujhnTCZkKaRKoERWf8=; b=ESfRLf2NxNJFMdlALFjxdqvR7bhoTehmwJUioSfvKlzNvcfPuBDB0NzKCDQPMTFXsI r7NnOspg0cJuUqiHK4Eq4ujdBf36irns9HbK283iiaMR8mWZ/9Y2U0BFTvBhp4kgB7hG oHoGEZLse/dRvEeOm9RFLY6UeXhdUyDNeX0iyEMHBjnheaFK+a6oLe/79ABGOyWZ9eqT fobidA5ee7nXT3QPIxvgjUVpsDDDhWQWLS7g6BvfRCEFHPWbnG2BQ0RNBueR5mOBf72Y xe2Mn33qjwB6cMeZVBUxHrHAcY5dr/HH8iFuzTuNi/LR3BBxKuS06BQO3kxDVz0O6VLq AxGA== 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=DtWFlPGPjVSnMZ4erXWfPBR06ujhnTCZkKaRKoERWf8=; b=GfohkBpBe9seqcoFrbOgxN10SVRlfar5yteVDyzhn+mJEj8rrpZ6dQsxSn4sshjxvY D4c/LZ4gxZFaLYGPDUNbyU1dgukfgPmt05PvygXE3wcNe7jfb5hgseeGruy3bsxHvROo LsyI2nrEcIeUL+XqCK/bfNmToj4T2NrSmsNUiOU4oLQJbIs89MsSpZIOXFfHbwKKGnQF qprmtUDEvzlBdJUPHjfuJFtcuYNFrIHrnsENhPFrXYVoEap5+SPhfRW/NAwhhWmEO3AB lJTbDU35JoYDtZeBurjzlWNKauBflCdJCP0+lLlxvr25CG4yF3uSwok/4C8HYJZDSh+S GrFQ== X-Gm-Message-State: AJIora9yYWmzXX+9NRHXsfmNV+9Dq4LD96uw3GFjAtssDcrM/aCJLpin EBtPbYfkyPAr1oOUXiK/4x8= X-Google-Smtp-Source: AGRyM1taTAUzfY2cDEMMbKMHGCLHGiW+1rnUMl9L6SKviO8I/PYa0Jfa/5qCwqpggp3clA+i+MFLuQ== X-Received: by 2002:a05:6512:6d0:b0:47c:6082:9ca3 with SMTP id u16-20020a05651206d000b0047c60829ca3mr2225636lff.353.1656153440266; Sat, 25 Jun 2022 03:37:20 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:19 -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 v2 03/10] iio: adc: mcp3911: add support for buffers Date: Sat, 25 Jun 2022 12:38:46 +0200 Message-Id: <20220625103853.2470346-3-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-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 --- Notes: v2: - No changes drivers/iio/adc/mcp3911.c | 58 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 25a235cce56c..2a4bf374f140 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -9,6 +9,10 @@ #include #include #include +#include +#include +#include +#include #include #include #include @@ -54,6 +58,10 @@ struct mcp3911 { struct regulator *vref; struct clk *clki; u32 dev_addr; + struct { + u32 channels[2]; + s64 ts __aligned(8); + } scan; }; static int mcp3911_read(struct mcp3911 *adc, u8 reg, u32 *val, u8 len) @@ -187,16 +195,58 @@ 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); + int scan_index; + int i = 0; + u32 val; + + mutex_lock(&adc->lock); + 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]; + int ret = mcp3911_read(adc, + MCP3911_CHANNEL(scan_chan->channel), &val, 3); + + if (ret < 0) { + dev_warn(&adc->spi->dev, + "failed to get conversion data\n"); + goto out; + } + + adc->scan.channels[i] = val; + 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, @@ -303,7 +353,7 @@ static int mcp3911_probe(struct spi_device *spi) goto clk_disable; indio_dev->name = spi_get_device_id(spi)->name; - indio_dev->modes = INDIO_DIRECT_MODE; + indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_TRIGGERED; indio_dev->info = &mcp3911_info; spi_set_drvdata(spi, indio_dev); @@ -312,6 +362,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) + goto clk_disable; + ret = devm_iio_device_register(&adc->spi->dev, indio_dev); if (ret) goto clk_disable; From patchwork Sat Jun 25 10:38: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: 12895304 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 E0583CCA481 for ; Sat, 25 Jun 2022 10:37:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232501AbiFYKh3 (ORCPT ); Sat, 25 Jun 2022 06:37:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232450AbiFYKhZ (ORCPT ); Sat, 25 Jun 2022 06:37:25 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CC4C2A255; Sat, 25 Jun 2022 03:37:24 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id b7so5502910ljr.6; Sat, 25 Jun 2022 03:37:24 -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=dAG25Y0XDP+Vyl6v8IvvJ8qg+yJ22TMKtj4bJENIGag=; b=qyyd1hNVs9W6aOFx1mXF6ugcyEG5nGiKV2mRDOcgdantgIipD4tT0inbu5TU4lg3ga dLRkQVjcDHVMzDhLDqr3wnf0C9TP6swb8tfyXBthJ+wqb57pUbb5qB8kiB8lfdBJAtVi KZXFsgrT0bLFsbGyCn0C1ZQPr4CiaAZqnVuyLzMEbb9eQ/Efn/3a353wEHcd+0ptTmjC KCqAxXJSzSm5sWW8v0tSuT59JcHwaB+wSBNSXUXvndmuSYjAfdol5d0onqn38AKLPzne CqtnkOY8rDzgU5xhepo8XLaGhbqgKP7yViPWpGvnWuLn8+z3mHkb2RSVcXIdVy+mtsRc WYVw== 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=dAG25Y0XDP+Vyl6v8IvvJ8qg+yJ22TMKtj4bJENIGag=; b=YlId+eyteZRIknu6/pxa8htuxxjNQ2BebwEpJHSOg6Q6tBhqb8GA+J76UMGWXEiDsV 3A0r1zq3ZZdjvZfGH8fcIcc2e13xIgRfxKiGAcnotel9yz80N6tNgCoIjSchxZmycXHI JctqpUTUmJb1qnjYpr+mF29Rj1t3TFpUZatFVal7QW4zs4eYPIhztXfZ1SUH18/1880k 914R8yVz70JWqWQo9olojpQz7zXt54XrxHZ9L1K+XTBnHwWNmfMSR2ecMrft2kGG6vRf 13XhfETU/poNr9h4nSojHjvgoClaXUKbKOGIfQLBsa+dWrZZMdftwaHIYra2JSIPTK/n aJZQ== X-Gm-Message-State: AJIora9cCVcqOOEVJqryUQ80XqCdaIigLgKoIWYpZvxUiMO7Mc5ZOlr4 MXAOqB5hSh/+wu+YADDZZ/0= X-Google-Smtp-Source: AGRyM1uSv2ouCH1ZnIH4pAJZVYt/5kxrZQTwJzqiXE/kpWP7FATFYQIi3QsAfAt558H/pMn/PuiVrw== X-Received: by 2002:a05:651c:1695:b0:25a:6e09:cf37 with SMTP id bd21-20020a05651c169500b0025a6e09cf37mr1614809ljb.19.1656153442886; Sat, 25 Jun 2022 03:37:22 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:21 -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 v2 04/10] iio: adc: mcp3911: add support for interrupts Date: Sat, 25 Jun 2022 12:38:47 +0200 Message-Id: <20220625103853.2470346-4-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-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 --- Notes: v2: - Removed blank lines (Andy Shevchenko) - Removed dr_hiz variable (Andy Shevchenko) drivers/iio/adc/mcp3911.c | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 2a4bf374f140..f4ee0c27c2ab 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -26,6 +26,7 @@ #define MCP3911_REG_GAIN 0x09 #define MCP3911_REG_STATUSCOM 0x0a +#define MCP3911_STATUSCOM_DRHIZ BIT(12) #define MCP3911_STATUSCOM_CH1_24WIDTH BIT(4) #define MCP3911_STATUSCOM_CH0_24WIDTH BIT(3) #define MCP3911_STATUSCOM_EN_OFFCAL BIT(2) @@ -58,6 +59,7 @@ struct mcp3911 { struct regulator *vref; struct clk *clki; u32 dev_addr; + struct iio_trigger *trig; struct { u32 channels[2]; s64 ts __aligned(8); @@ -252,6 +254,17 @@ static const struct iio_info mcp3911_info = { .write_raw = mcp3911_write_raw, }; +static irqreturn_t mcp3911_interrupt(int irq, void *dev_id) +{ + struct iio_dev *indio_dev = dev_id; + struct mcp3911 *adc = iio_priv(indio_dev); + + if (iio_buffer_enabled(indio_dev)) + iio_trigger_poll(adc->trig); + + return IRQ_HANDLED; +}; + static int mcp3911_config(struct mcp3911 *adc) { struct device *dev = &adc->spi->dev; @@ -298,6 +311,23 @@ static int mcp3911_config(struct mcp3911 *adc) return mcp3911_write(adc, MCP3911_REG_CONFIG, configreg, 2); } +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; @@ -352,6 +382,15 @@ static int mcp3911_probe(struct spi_device *spi) if (ret) goto clk_disable; + 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 < 0) + goto clk_disable; + indio_dev->name = spi_get_device_id(spi)->name; indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_TRIGGERED; indio_dev->info = &mcp3911_info; @@ -362,6 +401,32 @@ 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) + goto clk_disable; + + adc->trig->ops = &mcp3911_trigger_ops; + iio_trigger_set_drvdata(adc->trig, adc); + ret = devm_iio_trigger_register(&spi->dev, adc->trig); + if (ret) + goto clk_disable; + + /* + * 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, + &mcp3911_interrupt, + IRQF_TRIGGER_FALLING | IRQF_NO_AUTOEN | IRQF_ONESHOT, + indio_dev->name, indio_dev); + if (ret) + goto clk_disable; + } + ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL, mcp3911_trigger_handler, NULL); From patchwork Sat Jun 25 10:38: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: 12895307 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 CF3B8CCA486 for ; Sat, 25 Jun 2022 10:37:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232530AbiFYKhb (ORCPT ); Sat, 25 Jun 2022 06:37:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232456AbiFYKh0 (ORCPT ); Sat, 25 Jun 2022 06:37:26 -0400 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B74772A266; Sat, 25 Jun 2022 03:37:25 -0700 (PDT) Received: by mail-lf1-x136.google.com with SMTP id z21so8502110lfb.12; Sat, 25 Jun 2022 03:37:25 -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=RhpY9q307vkYuavF4O5uT48LhWT/p2dvSpT/WVpY8QA=; b=D7ghu+f6ICIMbeM3+3iundy78ehQ3Xsf8inMsSq+QGLlKLnzF9fGMMNGT/rIOKLGHy tYcJUiS4Y0cljBTHeNbpVdQrOJ9P4hUknvVWE6NgDeYuWsYOP2DKan+7QAG6AGfn7wjg lwUY5VcyWc+Jg4GVJ+R4r/wPFp1+J04dnJNZKLNTjwEjJr75UBf2e770q045ukSvtNWh pZMEnvl+jkrtg0tlNLw2sGspdITFAWI4yDPRHKNOM9HXMExmpwiy6K3O396kCxyrn4DF QrU4WvGgJZKg4cYmDTHFvMtXK38x9DGDRDa1tX4iJsILSFZ6bTeChxFAgHXVochIFOPQ eS0A== 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=RhpY9q307vkYuavF4O5uT48LhWT/p2dvSpT/WVpY8QA=; b=AA+8x/4HWhjvobEtshRxnKVmI0nov9cfDu9xY3bAlZE5DPpUIqoyJ5JbfTJxfE14pB pYDU30Ba/+0G4JKT3TskA9lVGJ0GwqLBpTrxmVhOFfJZAclW/Qz3dy79pkingjNoIDp3 L/TOPfJ8AeFsGGQ2SPS60TkHYSqAvAkJaW1zpTMw6XBZXS3vl/GFPTGSdJ4egx+3fxyO 8sH2onR2dhqV2UQ3kNdtRtmG3FrAr9otu/xHyQEzlTBWRdKiAPcBuM7Bnk+bz/O5jdI7 A9ygXizpudVdsp0GrWqsC3ajFRPc31mrPDmPg4xKXYmlgIOr5p/nKQvbnEwjutuChPPU DsRw== X-Gm-Message-State: AJIora+ItLpV/nXauqiEKI9I0ip8k5lT6BFA/ddsPQC/8N9lwrQrMpKa WAVh5imYblDMyGR4BbB+B3A= X-Google-Smtp-Source: AGRyM1u7On5+J34DXqynsZaKmqm81Ck2L1jsz6Q42brQDBSolDljmXm3Jd7TnO/ffxppz9+G64kFng== X-Received: by 2002:a05:6512:3d0a:b0:47f:73c2:bf7e with SMTP id d10-20020a0565123d0a00b0047f73c2bf7emr2141042lfv.192.1656153444082; Sat, 25 Jun 2022 03:37:24 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:23 -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 v2 05/10] dt-bindings: iio: adc: mcp3911: add microchip,data-ready-hiz entry Date: Sat, 25 Jun 2022 12:38:48 +0200 Message-Id: <20220625103853.2470346-5-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-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 --- Notes: v2: - Removed default value and changed description (Krzysztof Kozlo) .../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 Sat Jun 25 10:38:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12895305 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 BD23CC43334 for ; Sat, 25 Jun 2022 10:37:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232517AbiFYKha (ORCPT ); Sat, 25 Jun 2022 06:37:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232484AbiFYKh2 (ORCPT ); Sat, 25 Jun 2022 06:37:28 -0400 Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18C272A40D; Sat, 25 Jun 2022 03:37:27 -0700 (PDT) Received: by mail-lf1-x12c.google.com with SMTP id j21so8601634lfe.1; Sat, 25 Jun 2022 03:37:27 -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=jUsXE3yzxLd+uhE7uMJm8hyFKvelHgMO2H58Uby0WNA=; b=XYpFIAucCcRTIXXYfQ1iJkSLhg89fcAARUZAZ5Zhj4lqBBx91FPOScqks0zNCrnTKN rXO032Qii1VZGH3A6HnqGjTXIalG4JGZNJrGUWeE5rwAuQ9dCB9FGieX6evN/+8Q6HvM PM7ELnVDQTDh0tkaaZ0ZyGzdrBmCnNznBVHImge97IK+775sgeQd61dsMUTkKm80HhfL 9t3BIXitPQZmN0GBuiu7kjzIwCEnFj96OCY3NZfHJJsP4PwwfIIZyRgkwK+b1gC5J7+X FqLI21OHnd5YscMfZvhadr/VJVrHzvtrvu5mSm5osIRSJAWgc6ofzabGygKGnXhLG8Ra /7dQ== 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=jUsXE3yzxLd+uhE7uMJm8hyFKvelHgMO2H58Uby0WNA=; b=Gq3zBcSiiEBTCJ0MuNn5iyWol2J89FE9yRzHxWeEbwKdRl8N4yqWZ8DGumePijLeto Th3Qvq/a3kddah1nwPNedv4Pja0/JF8uGKSYrKQfSozxhAc6EyRkVhBjLDpYOy749S5g anDiA5nYD4ubqlqefDB0LDYgqlo+sqmziV3hvUhUPL+VpXcxWHaXCTX523X1U5h+kDHs evxQRPTvmuGqf0oNyPMtBlB5vdKVl8DufTYT5EODp9I1iP6Wkg5aXuCnZCxUSjCT9bwl c3mNC0AU63iyKQ9itFXiaaNPIXFBkWrT2zLzHqJ7fhEbj8OP14jh3tZ7og2WceKc4pKv t/7g== X-Gm-Message-State: AJIora+IZktliEWWLZS6TCKUSHLAqqeircfB5T3hyYJTdFxlJD5eCdHb 7ECFnF5CZKiBW7xD41zMLOU0JlGvSaydDfEj X-Google-Smtp-Source: AGRyM1vhJNdg6eDWkw+7SlhudVQwj5pj0hzkpbWCKCxHk5XVw7WBTDymvtLTw1QeUoHFsNYVrQhCYw== X-Received: by 2002:a19:e05e:0:b0:47f:7821:444d with SMTP id g30-20020a19e05e000000b0047f7821444dmr2198898lfj.515.1656153445436; Sat, 25 Jun 2022 03:37:25 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:24 -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 v2 06/10] iio: adc: mcp3911: add support for oversampling ratio Date: Sat, 25 Jun 2022 12:38:49 +0200 Message-Id: <20220625103853.2470346-6-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-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 --- Notes: v2: - Make use of osr table - Change formatting and typos drivers/iio/adc/mcp3911.c | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index f4ee0c27c2ab..1469c12ebbb2 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -5,6 +5,8 @@ * Copyright (C) 2018 Marcus Folkesson * Copyright (C) 2018 Kent Gustavsson */ +#include +#include #include #include #include @@ -35,6 +37,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 @@ -53,6 +56,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; @@ -108,6 +113,22 @@ static int mcp3911_update(struct mcp3911 *adc, u8 reg, u32 mask, return mcp3911_write(adc, reg, val, len); } +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) @@ -134,6 +155,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) { @@ -186,6 +217,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: @@ -198,9 +240,13 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, .indexed = 1, \ .channel = idx, \ .scan_index = 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, \ @@ -252,6 +298,7 @@ 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, }; static irqreturn_t mcp3911_interrupt(int irq, void *dev_id) From patchwork Sat Jun 25 10:38:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12895306 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 1C587CCA47F for ; Sat, 25 Jun 2022 10:37:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232508AbiFYKha (ORCPT ); Sat, 25 Jun 2022 06:37:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232480AbiFYKh1 (ORCPT ); Sat, 25 Jun 2022 06:37:27 -0400 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 011902A269; Sat, 25 Jun 2022 03:37:27 -0700 (PDT) Received: by mail-lf1-x136.google.com with SMTP id z21so8502110lfb.12; Sat, 25 Jun 2022 03:37:26 -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=0dm9KJtzaR5ke5V2d8sXQIXCLMAgrorSfKoR4+KGAsY=; b=kQoaDdeFQrxAQACWktYKJF3CXh7lJZIds2a8QPiNHMEtgVbiLkhWJd6XSri4bx/w1P TixXGmZGu9+Sx/CY6FwUNj3T9e+tuOBOWggkHLyL861KPZ/1C7cqVr3AlA4TU0+mxJ00 gKvsezxEuEEAWJVipkOLWm0Gs1qdZr1wrPX4fwVmSV36Teje0pynFWKMBews9Pa4NvRm LMU6QIK48obfRSRmbQt/n8XF5S4XHIJr/9Sj756XAhls+UQc3ptq/k9vORZ1Y+tVsXLg QwD30EvIgUDOAyOE61LvwoCGEAmT6lfeCWE/IRQNS16zLwiDH5eOCjQfambb/1aWpm4M syBw== 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=0dm9KJtzaR5ke5V2d8sXQIXCLMAgrorSfKoR4+KGAsY=; b=1w8GFJqJ+qQWuOCOV64a3+uQJmx8RSnTZp9IIg58hNssPmjmF9xVvP5PFZyJE0zEtd GB7gTJteDuF73WRpLkbR5L6cF3eSzwElwX22A9BcveW2tvfWmalkAUF7k5sWJeC0wfjh 0Z+pbZVYrQ0wXTitG/yYQD1YM5nVISJw0QTCzIEdqSP9pSaptxsvZi4phR9m5RYDIFgA zijMK8TEFA4ovNnqwimz72ytLaSPTpbBrk934BGaChr33mo7j9Tz5amlLs/18KU30Krp /sXeiXpq3NfTL3ZZchdSDS4J4H5XBRC1XtwPw6PXtJA/rQGdiBZg/WZO+q0kAz+Y4F4C RSwA== X-Gm-Message-State: AJIora9GtD5KZ72hAbewsiwo6a4qMJAqnJENbtUQwzXgCBglZRDZiIQt Zmj+Qlk6eYTP2yqNBf0WKso= X-Google-Smtp-Source: AGRyM1uelFPjZXnt8LC1ISb8/24MEX9UYHofUjaqpmhRvZ7bmg1TjcNp4E6Tv8uKTilDNE7BeGt2Yw== X-Received: by 2002:a05:6512:39c6:b0:47f:6ad5:7184 with SMTP id k6-20020a05651239c600b0047f6ad57184mr2100458lfu.114.1656153446559; Sat, 25 Jun 2022 03:37:26 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:25 -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 v2 07/10] iio: adc: mcp3911: use correct formula for AD conversion Date: Sat, 25 Jun 2022 12:38:50 +0200 Message-Id: <20220625103853.2470346-7-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-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. Signed-off-by: Marcus Folkesson --- Notes: v2: - No changes 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 1469c12ebbb2..ede1ad97ed4d 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -48,8 +48,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) @@ -178,11 +178,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 Sat Jun 25 10:38:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12895308 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 65A23C43334 for ; Sat, 25 Jun 2022 10:37:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232542AbiFYKhd (ORCPT ); Sat, 25 Jun 2022 06:37:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232505AbiFYKha (ORCPT ); Sat, 25 Jun 2022 06:37:30 -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 4D8302A266; Sat, 25 Jun 2022 03:37:29 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id by38so5483173ljb.10; Sat, 25 Jun 2022 03:37: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=o7jSMSvtnflA0oSrQ3YALnecp8wzRM0jcii6vCYQa94=; b=iCrGLxIK0aGalTYNVdNbi4eUvF+oR9pDW1Tj8AdKwlmnj4mEdKz1BiG1unGRh0K9cy wkfrDJt2tTd883OT5fFoTWomrZJmQaewjO39heqLdfMtgK9pe9L/EN/+vOwoA8I+ICYh wfJu0ze9rp99IuG1e1ekxeplN3KLNjHMBTsP4lgPGit3iw+Wop1uh2KdkNUk0gSY/Pca BTjytWRyD70oKrx/Pm8oxsp6gIE4T4KO4OJ0n9+n6JgOMg8AuKAiS/dx5+m/bq2d9vDJ POWPp8PwAoYANuNZsD7pp9O9ZTIo0uvqNkmF+5UrbI6ZvXQJJyRBSDAL+9BroriRqFjq niSQ== 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=o7jSMSvtnflA0oSrQ3YALnecp8wzRM0jcii6vCYQa94=; b=UtgmGIUXZK4GL5eqsDkX7qZvNOkZNpJ+NCgtY8jEdJaesVjZWDKEt5pLFM2x+LLKz2 MzfrXz4mUX7wAi9yYHJt0bqVwm+UDxX/WqIy7mCDQbXxBjcbjHa/UcGitQFkPQgCGkEI qaPSYDnJ/8TBgv6pQ8jOB+VE7UekfY8rORifsiEpyl0FWLEcCLyz2+h0oskw3k+u1JPj RFOIgZD8Susk2ZmAsxUxGZ40UvuhXaAh4ZpyU5iQG14DtLiVkcCixSk5lMYF0tWh2FGi htwcI/7k4uJZyJDQIerdWkHe4zQhTZtqx8mtTCM+Hz8g42fzr4PBTe5xrO67EU01Msez cxoQ== X-Gm-Message-State: AJIora9IcxU2PeLich2x0H/+CVg0hly/M7IAAKEbjifuMvhdkqI1cLAD vGpyel7pi1VZsfXLKW2vNzQ= X-Google-Smtp-Source: AGRyM1vhPlo9ZjFu9GQ28dm1ziLUX20dHpsyXGo6Ay2u5xNx9fWZOan+y0vHUlDoIkOuL1f2QTFIEg== X-Received: by 2002:a2e:880b:0:b0:25b:8aaf:d873 with SMTP id x11-20020a2e880b000000b0025b8aafd873mr1712154ljh.332.1656153447681; Sat, 25 Jun 2022 03:37: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 z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:27 -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 v2 08/10] iio: adc: mcp3911: add support for phase Date: Sat, 25 Jun 2022 12:38:51 +0200 Message-Id: <20220625103853.2470346-8-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The MCP3911 incorporates a phase delay generator, which ensures that the two ADCs are converting the inputs with a fixed delay between them. Expose it to userspace. Signed-off-by: Marcus Folkesson --- Notes: v2: - Fix formatting (Andy Schevchenko) drivers/iio/adc/mcp3911.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index ede1ad97ed4d..a0609d7663e1 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -155,6 +155,17 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev, ret = IIO_VAL_INT; break; + + case IIO_CHAN_INFO_PHASE: + ret = mcp3911_read(adc, + MCP3911_REG_PHASE, val, 2); + if (ret) + goto out; + + *val = sign_extend32(*val, 12); + ret = IIO_VAL_INT; + break; + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: ret = mcp3911_read(adc, MCP3911_REG_CONFIG, val, 2); @@ -225,6 +236,15 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, MCP3911_STATUSCOM_EN_OFFCAL, 2); break; + case IIO_CHAN_INFO_PHASE: + if (val2 != 0 || val > 0xfff) { + ret = -EINVAL; + goto out; + } + + /* Write phase */ + ret = mcp3911_write(adc, MCP3911_REG_PHASE, val, 2); + break; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: for (int i = 0; i < sizeof(mcp3911_osr_table); i++) { if (val == mcp3911_osr_table[i]) { @@ -248,7 +268,9 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev, .channel = idx, \ .scan_index = idx, \ .scan_index = idx, \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .info_mask_shared_by_type = \ + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)| \ + BIT(IIO_CHAN_INFO_PHASE), \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ BIT(IIO_CHAN_INFO_OFFSET) | \ BIT(IIO_CHAN_INFO_SCALE), \ From patchwork Sat Jun 25 10:38:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12895309 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 F26C1CCA481 for ; Sat, 25 Jun 2022 10:37:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232549AbiFYKhe (ORCPT ); Sat, 25 Jun 2022 06:37:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232277AbiFYKhb (ORCPT ); Sat, 25 Jun 2022 06:37:31 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2F152A42A; Sat, 25 Jun 2022 03:37:30 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id q9so5517097ljp.4; Sat, 25 Jun 2022 03:37: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=EFo8yVv76+QKgYsUZG2r1Y+bUYCEAcBjASCnIiNxJ0k=; b=B+TiX6soRjyv8tcAgYV3WYUUA99102wxcm9Ti4aoyD19TXEHjTRB9kD03qx4866ah9 g6sfU4PuCcVjS/vkC3uzTdKGFXDXBPGszEogcdDw0hs9eHbGa6ZGI/xm4FJ1dMGJ39kG X3zrAS3UHQwN+jvGXA4Ub1F7nUnouyDC29HEFpBQujAcAA92l/vYZHGvW1F9tDCXO4t/ /jkm4al0iq58HhPFmzU2G3LNlzQWdm01bhW516NrhkCpGeBroUz28Fz1reIO2gzhq09z BI8R27pzsoX6i1gWiCjaCoq+LEODTvrTPxNoX3uaz1rZSiw4DphXU2a6iP3FgzNWJXMQ NZKA== 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=EFo8yVv76+QKgYsUZG2r1Y+bUYCEAcBjASCnIiNxJ0k=; b=O498qiUvpsUta0SkJoG09RoAV7zbgmH7faJfNADREswhtTkpXCrfj9UtFYpOsnA3+W l4zZWmxdUbRSeletI047qu/W+Dcl+WU0X9QSkhVWtAy+GifinU6IcMfrZKMhiyLRAcxm 2id03kDUhmXa90qdhVwdD96o6un5ERfljqM96Lr2Xy/N4RycIbabYfb8Az8akGfp/bxk jsFAgwLbFGnzpz8YRvFLsGTUDY4X81TMikQKFZRPo48hCA2k4J0gxPdZFnp9uKWWgDvF flwJZteFjEi0hK5PhGMFdzkitOngwWdaKEI9Umv6+XcDkEr0D0eq5QwiYkXnEtixEGez FeVQ== X-Gm-Message-State: AJIora+V1pSVpm3eelEUA3J4gppF3FW1e+o3Gdb8eLsVO/ayIMelmOhV dkWpnQ5ib8nkZErzEOOQhlM= X-Google-Smtp-Source: AGRyM1sfuiPIr4jIryx93VmN+kG+LFlZdA5qReU+zcSg+rqjOlYVYnKTxRyr33pSebfEAFM1Ir/jkw== X-Received: by 2002:a05:651c:1189:b0:25a:7f29:6015 with SMTP id w9-20020a05651c118900b0025a7f296015mr1666197ljo.179.1656153449130; Sat, 25 Jun 2022 03:37: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 z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:28 -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 v2 09/10] iio: adc: mcp3911: make use of the sign bit Date: Sat, 25 Jun 2022 12:38:52 +0200 Message-Id: <20220625103853.2470346-9-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-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. Signed-off-by: Marcus Folkesson --- Notes: v2: - No changes 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 a0609d7663e1..a019264e73e3 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -144,6 +144,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 Sat Jun 25 10:38:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12895310 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 6941DCCA47F for ; Sat, 25 Jun 2022 10:37:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232556AbiFYKhf (ORCPT ); Sat, 25 Jun 2022 06:37:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232537AbiFYKhd (ORCPT ); Sat, 25 Jun 2022 06:37:33 -0400 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C2512A709; Sat, 25 Jun 2022 03:37:32 -0700 (PDT) Received: by mail-lf1-x132.google.com with SMTP id i18so8545361lfu.8; Sat, 25 Jun 2022 03:37: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=5oKTuqkNAMcSE9m/aA7zeta4JWfDKTgCumXtoFm2uzk=; b=KJDBq6yW8PCQhMfGSov0ZPGR4Wk5yPhnBbktUIrgYaKu1/IQRsi4di63b160zNeYxB ERCpAa2yrI4L65wYikYrlVIdWf+vee8h0pBEU+FZQMdHEatJiRokxgXuDuldQeh/wccE F25KECVpEUf4HqUpodGsn79cntAqM7ynAPGKfccBC5w1kg93nvqR40CUnzocfoSmBVo0 5Cfv2EN7JA2NdQ1XRLVEj/57m6JO/bIYden2QlhI+30hj7sXJJEI9eOUI8D38g6fHAGg 3p5TXn+qmqV9Yh6pZLa1CX/1Glm8n4zFN6Jz0/ee6U0pQQp7SXVW67H+CDl8x5K7hcZF hltA== 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=5oKTuqkNAMcSE9m/aA7zeta4JWfDKTgCumXtoFm2uzk=; b=r/mj/xTNqe/WRGUcM1SC48LAWBrPKE6Zwg0owC4KdyoHAu8IoAdSTmBFFr7xy+OuTg UEWT2VeYthzD62MKnsb0M1WHW/Ll4NtWhN22bcSVFXmG30pwtp7uyJlpcww3lj9lt63Y WvZCyrPw2Aq+vUbMClXQobK0dsZ3TRvN99rsuvRiYrPOQ+EWg6VT4kKQKpmS4zbn1aBA NjqQjY2zrogGMOG2+4lEoBF74j8dpgEEXFjjcB/kFAy/DqdPqboH3emLwQNTwvvh94dm USD2lhLUoFlV7yHzK6aWlC155lM4Z8BjbLAA4gmq4yq7yBYsSz84Yow3ORtGLTvPJZHh UgRA== X-Gm-Message-State: AJIora/LAClhj7XFWmGgGp9hd9v81TdhTIBJuZkZluOWhFvSj23/8WaL f4OoLOqVzB1H2sVCebv7fI8= X-Google-Smtp-Source: AGRyM1tjDyzTbaCUZguZZ7tYArQsytO/cRDImRTNJminAWr3VyrM1JyNrUAnnvnMUEkQnxNfIgVQaw== X-Received: by 2002:a05:6512:234e:b0:47f:8c9c:dfd with SMTP id p14-20020a056512234e00b0047f8c9c0dfdmr2105854lfu.438.1656153450337; Sat, 25 Jun 2022 03:37: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 z8-20020ac25de8000000b0047ac01fc644sm809115lfq.44.2022.06.25.03.37.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jun 2022 03:37:29 -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 v2 10/10] iio: adc: mcp3911: add support to set PGA Date: Sat, 25 Jun 2022 12:38:53 +0200 Message-Id: <20220625103853.2470346-10-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220625103853.2470346-1-marcus.folkesson@gmail.com> References: <20220625103853.2470346-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 Reported-by: kernel test robot --- Notes: v2: - add missing comma - Address comments from Andy Shevchenko drivers/iio/adc/mcp3911.c | 134 +++++++++++++++++++++++++++++++------- 1 file changed, 110 insertions(+), 24 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index a019264e73e3..f0179385485f 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) @@ -55,8 +57,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; @@ -65,6 +69,7 @@ struct mcp3911 { struct clk *clki; u32 dev_addr; struct iio_trigger *trig; + u32 gain[MCP3911_NUM_CHANNELS]; struct { u32 channels[2]; s64 ts __aligned(8); @@ -113,6 +118,37 @@ static int mcp3911_update(struct mcp3911 *adc, u8 reg, u32 mask, return mcp3911_write(adc, reg, val, len); } +static int mcp3911_get_gain(struct mcp3911 *adc, u8 channel, u32 *val) +{ + int ret; + + ret = mcp3911_read(adc, MCP3911_REG_GAIN, val, 1); + if (ret) + return ret; + + *val >>= channel * 3; + *val &= GENMASK(2, 0); + *val = BIT(*val); + + return 0; +} + +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; + case IIO_CHAN_INFO_PHASE: + 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, @@ -124,6 +160,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; } @@ -180,29 +221,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; } @@ -220,6 +241,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; @@ -264,6 +297,48 @@ 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, \ @@ -276,8 +351,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, \ @@ -330,6 +407,7 @@ 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 irqreturn_t mcp3911_interrupt(int irq, void *dev_id) @@ -460,6 +538,14 @@ static int mcp3911_probe(struct spi_device *spi) if (ret) goto clk_disable; + ret = mcp3911_calc_scale_table(adc); + if (ret) + goto clk_disable; + + /* Store gain values to better calculate scale values */ + mcp3911_get_gain(adc, 0, &adc->gain[0]); + mcp3911_get_gain(adc, 1, &adc->gain[1]); + if (device_property_read_bool(&adc->spi->dev, "microchip,data-ready-hiz")) ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ, 0, 2);