From patchwork Thu Jun 23 17:08:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893004 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 DF97CCCA482 for ; Thu, 23 Jun 2022 17:40:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234403AbiFWRj6 (ORCPT ); Thu, 23 Jun 2022 13:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234517AbiFWRhm (ORCPT ); Thu, 23 Jun 2022 13:37:42 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81EBB848B5; Thu, 23 Jun 2022 10:07:05 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id bn8so6719077ljb.2; Thu, 23 Jun 2022 10:07:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=prMZe0pej9PoxwbNkQMGNemqFNqwjkROTTXt4DxreRU=; b=K5O5IQezMguE8NJFjGVU9xeaJvkVQaswm76vVr7PLmDajGglB50+z7e1wwApf1GiHN fXuOBDtSm2RL9FdWgDDGLQ6JbI2iON+xAzYgNM/t2JvndfAoUxRIivl41R674AEhxwGL oXZ4lSuYJF//2xqbdyraVak9KK9NBkYJCJDxZFtRuovXINRKS5eYhn/yhd8uocQRNGSf dKIM7/aVfDZ9kpmsHDlUQupDtiPLtvLot5GBClvhUz+egfLvsJgvZK6QTfHhcOnfCz7O w2MIv+RGMwmTY4GMrHUciw4yZuqTUUfPFEDcNm9geZbElIIOGGE2TZlIT7UvhlhY0VB0 dHqw== 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=prMZe0pej9PoxwbNkQMGNemqFNqwjkROTTXt4DxreRU=; b=qs58SoHuGRVjSg8Y49hCJ/utWNFObHf0IY7k2fCE6oNLb/ftIj0SdDr+782bInKNo0 1pkJ4nbYZ9PgAb1k6/GINh8Y7GwykGS+aYrXOuIDsw/k/acAVdvbBedS7Plzno36gRLm nzIb3NnDbpj0z9XC6XpZicAfSB8AsNyRDYwj80JFRBe01YO4N5llkBsntKqd1cRHzFq1 YPxeBKP5eEvsadM4kRZVGChGLIpZp/09Sq+GZ+i8+skuUL13JcALW2qOoC4tjInV84LZ eDNO3udA74sUvxCw1s8w6ymgRpuRXstKks2GSVrpyoFy/4w/5l3S/StC8unz45AJbLwl EH/A== X-Gm-Message-State: AJIora8UDz1yChUDIhaZ4RJ1eEi6Z/rX9PSKSlmXCyIYQvW1M/yHX8kY pnMyS6haAtK6JowRvlEv2Wo= X-Google-Smtp-Source: AGRyM1v2WkGrqBVIfjrP/9HqHkETaVQ9w8qZ0MMdQDVuC8NHd/2ka1BBq8Qc7NlkJlkV9BZY4cE0OQ== X-Received: by 2002:a05:651c:1310:b0:255:81f3:e627 with SMTP id u16-20020a05651c131000b0025581f3e627mr5323395lja.397.1656004023694; Thu, 23 Jun 2022 10:07:03 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07:02 -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 01/10] iio: adc: mcp3911: correct "microchip,device-addr" property Date: Thu, 23 Jun 2022 19:08:35 +0200 Message-Id: <20220623170844.2189814-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 --- drivers/iio/adc/mcp3911.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 1cb4590fe412..4338552cd0ca 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -208,7 +208,7 @@ static int mcp3911_config(struct mcp3911 *adc) u32 configreg; int ret; - device_property_read_u32(dev, "device-addr", &adc->dev_addr); + device_property_read_u32(dev, "microchip,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 Thu Jun 23 17:08:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893007 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 91E6FC43334 for ; Thu, 23 Jun 2022 17:40:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234448AbiFWRkB (ORCPT ); Thu, 23 Jun 2022 13:40:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234529AbiFWRhr (ORCPT ); Thu, 23 Jun 2022 13:37:47 -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 4DA8492151; Thu, 23 Jun 2022 10:07:07 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id o23so16929561ljg.13; Thu, 23 Jun 2022 10:07:07 -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=IGSdRSXlkf0EIyduZq/whIQ2rDkrEQQxbaXXIkw+vOc=; b=ZFHGjYUJvqZZhg78fpoliU7qdjoto7KiFJyYriKWwGL0L/3RDJ0Sw/wu0prrvThurs CfMrLatOrnBaIH445OM2GhWnP1bL4MelDWESLWvDdoAeybUiu7l9QkxT5uKQGnR6phJO 1ULSiORHbAjQapGBYmBTipq16k1Nn6T9pBqgdWk8CJCUuUDeoOZrRlafyCZzANaqZGPX E6zOtPIJa71dIQPeOPAB2MyP1/fHYwP0H8PaK0XLO9Ol79cx4qnKD5FZjerp3QHIG7XP 9IQNl+GkyCPSHO4wIB3gTRJLN/xbTMJrVdGsrJZ4t7aPuAqE/Ozy0N2TeEPPB27EbPN2 Z4ew== 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=IGSdRSXlkf0EIyduZq/whIQ2rDkrEQQxbaXXIkw+vOc=; b=7rS7A4RFfHwN+0v0GwD+zJRCnOXqpSszrRbK2Ea1pclgkfowEvbIsUr1BSW8l/v6aN ij9ziGGKYyhxuwbu/b6r/3/kRfeT9KnFL2f+qKsLJZa6fDd1hIJoha9YfkVQNEXfSvTs i7SXsxTrPiJeyyLXbG13uP6yrBljdkV1SUMjg4UidxPlLYgt2HiCLZW6/qDd1WaZ7gb0 l2/3G69dtNm79m6iBSsx6tNx1tgmo9If5Y8FdvglnA6PszjQVhMXU1nYE/gmIGPY4F9x 2hbf6v9xI+26bqrhKcGRMgL/o6hRCNgVszPFK112ewkGwgvdOCwLpplx575XSRMdr/qa /D4w== X-Gm-Message-State: AJIora8bfRIVOj7ibqEqkPY9jaDOy9lZf6UlmnzKsW9BPVY2mqmpJU3k xFiK0xyNhGIAAmtAA5IjDR0= X-Google-Smtp-Source: AGRyM1vT6R/FeopIjUsc0j4VCnlgppCQy/5MJHK1pAwuLfvpE7UwIUaowXDXmGf8cnNZG+EHMTF3kA== X-Received: by 2002:a05:651c:233:b0:255:58b9:1f53 with SMTP id z19-20020a05651c023300b0025558b91f53mr5230297ljn.143.1656004025373; Thu, 23 Jun 2022 10:07:05 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07:04 -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 02/10] iio: adc: mcp3911: use resource-managed version of iio_device_register Date: Thu, 23 Jun 2022 19:08:36 +0200 Message-Id: <20220623170844.2189814-2-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-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 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 4338552cd0ca..3d9e8ed10874 100644 --- a/drivers/iio/adc/mcp3911.c +++ b/drivers/iio/adc/mcp3911.c @@ -306,7 +306,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; @@ -326,8 +326,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 Thu Jun 23 17:08:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893011 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 CE91ECCA481 for ; Thu, 23 Jun 2022 17:40:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234458AbiFWRkC (ORCPT ); Thu, 23 Jun 2022 13:40:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234547AbiFWRht (ORCPT ); Thu, 23 Jun 2022 13:37:49 -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 D14C292161; Thu, 23 Jun 2022 10:07:08 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id a2so56351lfg.5; Thu, 23 Jun 2022 10:07:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QQHq1R8+z5ISpQ71LBkGKEl1o+IhCGseMKIHQK9OFR4=; b=EI9rk/s6el+woaWK1fR4rjizrETaD7hvCzSsV6Hecp7G9Bmav3LwvqrFA8KKgFs4t+ nhqYiwIQ5iAN6MWCnR3mrxqxOSz/ekgoETCj5rv9+8TJRD+jeeq/1yloNSW/8kPCuQwS umm2cdav02G4E1/nW0AcYN22Uogk4PqfzsCbCQ2SemOl6nuW4Lu+NY82EEtWTJ7cs6U9 PQKmMvb9eZD5O1jW6p/htkrO+XIaiDpYB9q+3/3rOboIqH9rr/uE5nqU4OSdRMjxwHpz g3pBdab/UsbOPEtb1SVn+maV5cq8D+72rFs4WhTCW5Xb0wFtD/RYL0QUCiFI/00sPerl FuZA== 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=QQHq1R8+z5ISpQ71LBkGKEl1o+IhCGseMKIHQK9OFR4=; b=stC7BQAEl/EUhzgTKjywBDd8p23PqAlBvl5Iy5aJvmuCIbUkUYdeWZSfa5yo1hV3Rt islobTw3itTh4HIJA9RXhf8RYgV96eIa0H2cOO6+IR0aUiStrZ3xVFL5RPG2NGkRxr2A EMuvPL4WKExE3nwAyxW7WrxVejLUcc0NCcaz4eVKq/ju04/Xy0oCsexACVLAcq07ITrz P+ZZB3PhX+6Ym1SWA4J3i8nSGDcmrt5mWKarKDOUto85XxXNNNBTtjfQj5kaOlb4Y2N9 GL5gHO07pnhWctUkjdksw8ejpyNW+bAJDu/0ry5OyBoXO6zWMJaF346IE5rBz6xuui6M Gu5g== X-Gm-Message-State: AJIora8AxYBCdZ3fg8tzj0+oaxD3vW85ElRDqa9amPUWZKS4tgNiC1t7 Hl4bqjmEvF+Nx0UASzrD/5GbAovCyAvcZw== X-Google-Smtp-Source: AGRyM1txW/yYaILtToFPJXqzZ5Do/Pm+4fxfuBgG9r+hZXk+wXMxTHD/6koYJCDRnSe/pKH5kVOS4A== X-Received: by 2002:a05:6512:3151:b0:47f:9d42:8e11 with SMTP id s17-20020a056512315100b0047f9d428e11mr5046731lfi.274.1656004026843; Thu, 23 Jun 2022 10:07:06 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07:05 -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 03/10] iio: adc: mcp3911: add support for buffers Date: Thu, 23 Jun 2022 19:08:37 +0200 Message-Id: <20220623170844.2189814-3-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-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 | 58 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 3d9e8ed10874..768cb0203f52 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, @@ -297,7 +347,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); @@ -306,6 +356,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 Thu Jun 23 17:08:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893006 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 6E036C433EF for ; Thu, 23 Jun 2022 17:40:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234435AbiFWRkA (ORCPT ); Thu, 23 Jun 2022 13:40:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234559AbiFWRhu (ORCPT ); Thu, 23 Jun 2022 13:37:50 -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 B791892172; Thu, 23 Jun 2022 10:07:10 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id z13so3958104lfj.13; Thu, 23 Jun 2022 10:07:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vjncueEDEOXI1ZZ67zcML01m1fkxgPmoKbICq2roG2s=; b=Nbfpp64GCIC3Ssb2kIy30hSg2CvRnj7k5371N7oDJcy9yRrzXfWPlihKCsqH7PcIE5 V/ELtiN4T5onBwd8yDHruPhe3sX3Eq4thg1QJPmXD/i0/2y72IabKcnZGQQkg7uwNEdW 3xHKi9EFhWI7Pxs/aCSKK6I5xH3hLbiyIO7Jx9p+B2hVPglBA389cXxNok+pouJXyMff exqPyt/wkdciq0nqxpAVakavthyUR1FqPoZ6I352Jxlg2nhFIom+fz0/xOFU6ALT5Fn2 yjzTqT9kipZ49qQLAl4obHCmZlBjTkjiy9ULvFgOK2m0pujrSo9vG8Sq7IDu7jRbKWSc /chw== 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=vjncueEDEOXI1ZZ67zcML01m1fkxgPmoKbICq2roG2s=; b=d3ZrhNcmIDjfBj5qmAWOYgZ00uxZHY1sk81SeoAJHYUicMZxnhLTaANBaBJf77D8d/ FEi8g2DTPgsxbUA3ypDjXNDhZMNFfQC04UX75Yx7bWj9qaXc+9+09xrefjyFrGJmln7+ jej2HfPVE9Pd5/IFCXZkLTP+JZCGL7LPwjQdPKgDh8UtwNTWKtq11aqWhouS2qsGxfHW QfPhLyMyQtpMqtjoXdNbTonOtERyLBszvWZBS+j27moTqgS/hplFiDPWuissNLtd49w6 B8dALZL1FAEVSaXMoLAb7aMDnO/jxRH9vBcvzACVh/X5CigBUEXwttf8yMHi7UTXpiFt OYMA== X-Gm-Message-State: AJIora9J2k2TJ5SXXwlHe7s3kORJnsjBa7XC9PLebNYUnEfvuw8ch9YR 8smQOLOsXU2YI0tehqygZ68= X-Google-Smtp-Source: AGRyM1uz5elafW+BeYQYajDTPcNuwwsjDMnR5Qto7IrVP4vWW/8kdHK+rUwI23w6aY4oIrYIGwWBNg== X-Received: by 2002:a05:6512:31d5:b0:47f:718a:3459 with SMTP id j21-20020a05651231d500b0047f718a3459mr6103570lfe.480.1656004028467; Thu, 23 Jun 2022 10:07:08 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07:07 -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 04/10] iio: adc: mcp3911: add support for interrupts Date: Thu, 23 Jun 2022 19:08:38 +0200 Message-Id: <20220623170844.2189814-4-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-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 | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 768cb0203f52..e761feed5303 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; @@ -292,11 +305,30 @@ 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; struct mcp3911 *adc; int ret; + bool dr_hiz; indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); if (!indio_dev) @@ -346,6 +378,17 @@ static int mcp3911_probe(struct spi_device *spi) if (ret) goto clk_disable; + dr_hiz = device_property_read_bool(&adc->spi->dev, "microchip,data-ready-hiz"); + if (dr_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; @@ -356,6 +399,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 Thu Jun 23 17:08:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893005 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 154AECCA486 for ; Thu, 23 Jun 2022 17:40:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234427AbiFWRj7 (ORCPT ); Thu, 23 Jun 2022 13:39:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234580AbiFWRhy (ORCPT ); Thu, 23 Jun 2022 13:37:54 -0400 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74B039217C; Thu, 23 Jun 2022 10:07:12 -0700 (PDT) Received: by mail-lf1-x12f.google.com with SMTP id j21so91565lfe.1; Thu, 23 Jun 2022 10:07: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=S9hirshugQrBiBu7BVIXgDl5HJGPuOlxL8cHUey9ErY=; b=eyDXyaakL+t64wKHttYScosWHhadSVwSvD2Hj49UITiJlLprGrD0QnAJBtqYH/MVd3 orUCuxoiFSKdV32jUlu6lWSLKeeidlKw54kSIDAv7BDG0fzfgz+Go9U2wiWp62tViAJK mc7U26X+DIYOM064sg55Atb56iqpVHySeaqv/2bUXNBv19mTYUxz/6ukAML7GoTjllZ6 lV51lG8E/RTLexabib8SyY2kzbkmD9qmNyHe9x61OxSfyE8VQU6priXO2AfdEl/xUexs BCFdSheYtVEqwKpSHDC0A4AsNXzfEoZmCRZnqXWKL5pUMU4Lng8LtyUjTtjuPm58UcLr 5ruQ== 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=S9hirshugQrBiBu7BVIXgDl5HJGPuOlxL8cHUey9ErY=; b=i5m+VEr0Ra/vvB9CC6B4oc880MDS4dcIDmoPlgAXNn5gGOWkYyQwiW22suHYhPDCUv k5S5i2Py+/8ikUf/BdUhhkp+IzQpRHxVKHYcXCYMj+hupivuBM5K1gaN65qBhdLRXfbf NHn27CzDQxCNlVxH0smEhQFVI+usROSFFRyAtx5o3Y6kBnkH/ESyX4pXnjd5UUTLzNQ1 wpDy2++LoQIDbSWXV+hk+HlULCifYmWX+WZ2xiGbfbqTKjKVfxtrGEVEW3N6lG3IPo/f kkam7NJiAiqeItzIFGaaJXv/yrwCorUfIv8z1BFh9QiEEFvlB0YiJqM8z6LUb8c62Rsy 1GAg== X-Gm-Message-State: AJIora+jZ+2106ck2rENADs6yef2tPSCw5hdpLwVlr3Bfh4i1y0S/yKg qLDuuqI/7D8BoYcyqUgn2dc= X-Google-Smtp-Source: AGRyM1tVqxCUPNrOx6l18BO/MTilQeecDmIMWAET+X9ac9JTv5yu65+bqfVy2FhWxIKzOlZC41k4Zw== X-Received: by 2002:a05:6512:c20:b0:47f:6ebe:4de5 with SMTP id z32-20020a0565120c2000b0047f6ebe4de5mr6282273lfu.322.1656004030388; Thu, 23 Jun 2022 10:07: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 w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07:09 -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 05/10] dt-bindings: iio: adc: mcp3911: add microchip,data-ready-hiz entry Date: Thu, 23 Jun 2022 19:08:39 +0200 Message-Id: <20220623170844.2189814-5-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-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 --- .../devicetree/bindings/iio/adc/microchip,mcp3911.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml b/Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml index 95ab285f4eba..74b333e44bfd 100644 --- a/Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml +++ b/Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml @@ -36,6 +36,14 @@ properties: description: IRQ line of the ADC maxItems: 1 + microchip,data-ready-hiz: + description: + Data Ready Pin Inactive State Control bit + 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 + default: false + 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 Thu Jun 23 17:08:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893002 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 4FC42C43334 for ; Thu, 23 Jun 2022 17:39:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234370AbiFWRj4 (ORCPT ); Thu, 23 Jun 2022 13:39:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234582AbiFWRhy (ORCPT ); Thu, 23 Jun 2022 13:37:54 -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 C85A49217E; Thu, 23 Jun 2022 10:07:12 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id a2so56351lfg.5; Thu, 23 Jun 2022 10:07: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=lCOeJ/RL4OsQEpw+dtahUcCad0vIrYxWUL3JF0e8HLc=; b=mXw9FDQnxA5G0jf68xxK8Nvy+nDuvAqUO86Uk3FvI8bDhn8sPVP1p1K+yNfuO9zB7O 9Xb0wBw6U/wqwWgrk4q8JhC/T+IayTaOpieieJMwCjSPilOtXA0Nectk3syWHhSODjBR CBxEBvRFobg+l7irICuTNHniQPwnwKR5eXZFZewFs/rahujwfCaUXHcCScQOHdSON6ZT 3INnbxJAM4Ro/7rvGTnSyfUq6KKsQL1yIY5VRjQdvLiU73QsedibWVPDe8e5IAcjx8pq 1q2T2WI1mmpJl8j/OaCVfNS3KfAGZ+g3jlz955RlCX/EtF/KBo7xhMAKChDiW4fvIsUH 7bKg== 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=lCOeJ/RL4OsQEpw+dtahUcCad0vIrYxWUL3JF0e8HLc=; b=iYNlP3P/hX3Hs725q1SYqnHicTKTSf+dCxkUEYegb3ukHOWIwl8okSWsqR/zJ5YV2W CjRvGNKQJZUXlVZ0ccdYZE+xmLVU4MypzgzppFXv1vfdkEUW+i1BQAxcYYmpJkKq7flW 3ONB/D3iBwEoKEAcgc7seW01FKgih1DFlF7CXgnhnSUStZIVbioYYH6+FIVm2oBjORW1 xIRWXdFs1CTTbcORII5OMcl59nGO9rXCoc4lqEF/ICkZ9rGfGJ2VdRUqwm0OKsKv5GZ2 nh4mxrZ/+Yv2NH2lYsH+9kST+U897RGff+x8O4jMuMfFu+NmWW7fKxFVa9JqoaTWSJs/ YMNA== X-Gm-Message-State: AJIora9RQyTn9hPZlfGAhCxcd8efFHbuaEy4CcgdaG1bRKykB/JyROrN e7xyWXumY2HvGfzUqh+aCagkUdtva+X4tQ== X-Google-Smtp-Source: AGRyM1vojOZilE7iWyjmaHwrX0NcTKcapCRrhLVtOYKRuxOtPiiFijPvlHEKwdLdysC+QWy6IeallQ== X-Received: by 2002:ac2:4e08:0:b0:47f:7b73:c9b9 with SMTP id e8-20020ac24e08000000b0047f7b73c9b9mr6202076lfr.5.1656004032201; Thu, 23 Jun 2022 10:07:12 -0700 (PDT) Received: from localhost.localdomain (82-209-154-112.cust.bredband2.com. [82.209.154.112]) by smtp.gmail.com with ESMTPSA id w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07: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 06/10] iio: adc: mcp3911: add support for oversampling ratio Date: Thu, 23 Jun 2022 19:08:40 +0200 Message-Id: <20220623170844.2189814-6-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-1-marcus.folkesson@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The chip support oversampling ratio so expose it to userspace. Signed-off-by: Marcus Folkesson --- drivers/iio/adc/mcp3911.c | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index e761feed5303..65831bef12f6 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,42 @@ 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: + switch (val) { + case 4096: + val = FIELD_PREP(MCP3911_CONFIG_OSR, 0x07); + break; + case 2048: + val = FIELD_PREP(MCP3911_CONFIG_OSR, 0x06); + break; + case 1024: + val = FIELD_PREP(MCP3911_CONFIG_OSR, 0x05); + break; + case 512: + val = FIELD_PREP(MCP3911_CONFIG_OSR, 0x04); + break; + case 256: + val = FIELD_PREP(MCP3911_CONFIG_OSR, 0x03); + break; + case 128: + val = FIELD_PREP(MCP3911_CONFIG_OSR, 0x02); + break; + case 64: + val = FIELD_PREP(MCP3911_CONFIG_OSR, 0x01); + break; + case 32: + val = FIELD_PREP(MCP3911_CONFIG_OSR, 0x00); + break; + default: + ret = -EINVAL; + goto out; + } + + ret = mcp3911_update(adc, MCP3911_REG_CONFIG, + MCP3911_CONFIG_OSR, + val, 2); + break; } out: @@ -198,9 +265,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 +323,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 Thu Jun 23 17:08:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893003 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 D17B1CCA482 for ; Thu, 23 Jun 2022 17:39:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234390AbiFWRj5 (ORCPT ); Thu, 23 Jun 2022 13:39:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234594AbiFWRhy (ORCPT ); Thu, 23 Jun 2022 13:37:54 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A92692B4F; Thu, 23 Jun 2022 10:07:15 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id j21so91785lfe.1; Thu, 23 Jun 2022 10:07: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=0E4art4WHyIDsKy+IOK+bSE3VnO/zAwRAVWiCR6wQsM=; b=XK1yu15Q26rYs9zDD16HkxWE+plO8GeZ+dNujYTCKpe0TBb9yxE8UjsSaLbdNB5qgy wdewUnj3qqqzHpiTGOtPOCQfXBhSf8Aa7T0CijynNDEP+9Sb+fuQu6kBrqECzbe7hJDW aBFumHP43UwBjt5ZxPjA2w60ETmwUhIzpJCpCt54fTlFh40UC3ez72g0dMICG0is8RDD PjLS/EGzGYJnRl41uj1poo5Iq7znQ2n+xYLNlVbYePC2Z/qqucryweylIgCBzuCVy0Oc 4wZqFJmB22a2V+tIDMI9B9+h4B63K2rBEqcgg6JO4RkT9H8KhnartdiDsbFlE2yCgd/m RCXw== 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=0E4art4WHyIDsKy+IOK+bSE3VnO/zAwRAVWiCR6wQsM=; b=AfTF06MC0ga6m29Gv6x3CeoHzW8gYCb1o2pPll/MMp10KSXdaAz8HMH5kNiS2MQieq 9WAi7ApMBEvIwMMqCuKbzThZonqkv7xFsbCiXa7IuFAeIoyr4DzP6JdbajSCHh1LzBZl 5MKZZ1/YbGZVe9IZj0PATq5G6MUaXSCgweGasmYLsRylg0mIa7Y9xiU7wYiUjiuceyOx VhQ3BmqzQFxWj1YsyYKqeYt42VsmRiCWJFw/EAVWi3c8gGPPFkb2i946Q8pf34BuIhiS J6Bf62KBOvSxr8fwbxUt//i0j9rN/VrQ8lAC4ceguJKGY48+RzuHbsn1zmj2v7vI60TL dZQQ== X-Gm-Message-State: AJIora8wOzGvVg4Ea6PGmQ5TFjCI8/VRV+6yDmiVeb0Du15ppuWzV2vi 7avgRM3gYfnQOQVQ5yE6QFo= X-Google-Smtp-Source: AGRyM1vX8jlCl+4BPopA0BVNn2QNiJNNOFUmG++cyTTTiZGKTZyDG8lzDMD3GuWJRGn17RqeB6OEkA== X-Received: by 2002:a05:6512:22c1:b0:479:54b6:8281 with SMTP id g1-20020a05651222c100b0047954b68281mr6210521lfu.291.1656004033642; Thu, 23 Jun 2022 10:07: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 w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07: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 07/10] iio: adc: mcp3911: use correct formula for AD conversion Date: Thu, 23 Jun 2022 19:08:41 +0200 Message-Id: <20220623170844.2189814-7-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-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 --- 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 65831bef12f6..9e365947d285 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 Thu Jun 23 17:08:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893008 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 425CACCA48C for ; Thu, 23 Jun 2022 17:40:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234470AbiFWRkD (ORCPT ); Thu, 23 Jun 2022 13:40:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234605AbiFWRhz (ORCPT ); Thu, 23 Jun 2022 13:37:55 -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 2463B92173; Thu, 23 Jun 2022 10:07:17 -0700 (PDT) Received: by mail-lf1-x12c.google.com with SMTP id a13so12442lfr.10; Thu, 23 Jun 2022 10:07:17 -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=ept+S9MCFJKpRtcRZmZou1ZYAVo4ChcwRbtVyRYJMdc=; b=JWqBhXxwp1hbh1XQ70jQ65D9d304cBsZX/9wj8GGvKcBrs6ki9VAPE7Nm+O9iF5oUO iu+wpFi5JPnrwJLXEhGS5PsbWfZwBb2kv1pkcVw3c66s/bKry1R4elqnKN+0GXDolGlz +F3SB3K1sjwY8a0W3ddUq/TnT6pfYqbSUP3ichMGIviqxp2e/kUeFOMz7iOPTIeudii8 du2plq5Sb+UrvMtNCsWB4zvuf14r0tyWP0o5j3vVT4oUupRL5T1F+FbjbfLSB5NgcqDL 4pi4XLpK2zg1f0DFK1yJWLN9hGrIvzsRbSE/d+PgnSUX+jV+FDaIJpeMkNqMSKSHja9R zgLw== 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=ept+S9MCFJKpRtcRZmZou1ZYAVo4ChcwRbtVyRYJMdc=; b=BAI0FuwhlxFcyVMBAIwB+6BMbRlOqLDZxKrsyyo255Iq5ocZADEIYXt5EeKOrGNWkj wDCkoBPZ6X4feeBvv7aUDpnaGoxV2gSlvzbujRWH/S6K07PZMpo76OJ5VuHitJ9iGqzu 39ec/ypoVNMoTiV6SV3WtHFMSoZeQmjicVLTVvndo5LZXOtc9i9HU/e5EF4xVfPot/DO 0nAICfLmQZZvNYDHBcc5/r5wU1gTenwKC1hcF6weNLOLgT+u4oYCCcN8StaGPQBgx3El KOTJWT29XAVwY1x5LclEdYrCiVpIAHicqIDMucNC2zOnat5TP24ccnHrzCPAX8N+Dwt2 XWkQ== X-Gm-Message-State: AJIora9kClyAzBkpPSm8VS/exAUAPPz11xco31h4itSlpKh5D6Q2zocd PRyXigMOjS7t6jxJR02N3Eg= X-Google-Smtp-Source: AGRyM1tpzsmJMfxjHHIP30Az2JIeDqD5QQ4o8hSl1yuZbdZwLwg48AaQlyAKLjBfl3h19xzJx4k1gw== X-Received: by 2002:a05:6512:36ce:b0:479:4e8:c208 with SMTP id e14-20020a05651236ce00b0047904e8c208mr6219729lfs.496.1656004035383; Thu, 23 Jun 2022 10:07: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 w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07: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 08/10] iio: adc: mcp3911: add support for phase Date: Thu, 23 Jun 2022 19:08:42 +0200 Message-Id: <20220623170844.2189814-8-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-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 --- drivers/iio/adc/mcp3911.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 9e365947d285..aefc5eac874c 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,16 @@ 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: switch (val) { case 4096: @@ -273,7 +294,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 Thu Jun 23 17:08:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 12893009 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 BF551C433EF for ; Thu, 23 Jun 2022 17:40:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234484AbiFWRkE (ORCPT ); Thu, 23 Jun 2022 13:40:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234625AbiFWRh5 (ORCPT ); Thu, 23 Jun 2022 13:37:57 -0400 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [IPv6:2a00:1450:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D1396596; Thu, 23 Jun 2022 10:07:19 -0700 (PDT) Received: by mail-lj1-x231.google.com with SMTP id q9so6043090ljp.4; Thu, 23 Jun 2022 10:07: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=WrAwCaa8SFiIkRGl0IlNr/Pb/vSSatSRIxkJ5qLkgXc=; b=i++raDe8RRGy6Sw3E46ns65wlY7v8Dg1dmpy4K3YEj/MWulBKVFbQsIoEikswDPTF7 c4yIwH3nf++NRTAvPMFurf4Xv6XRJ8zMJ+SvMc49Oho2N6Fc//GqjZUNPItywGJLGGo3 y6IvNSO/moXJOa2dp6y315l4E0NRDSZTJyL00ak1GWaJShCYWFrPG1J3aFvksh3CBUQZ 8MUkOI/QDLa+Y7fbtqSQ+Ms0MRcfQIISs47YLmToWcQSv4FMXy5ll/Xamz6m847Lagng 1QqV4alCpp+iyGrQDKjiGnlAO0FaXwKI/kzYtIjeF11quQLME8BkyFUmzAYqG9J/wBcM AxAg== 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=WrAwCaa8SFiIkRGl0IlNr/Pb/vSSatSRIxkJ5qLkgXc=; b=fY1S5zpxhWfLl1sVUZlevEDFmzyq+J4m9wLudYefIx83Ipn2Zum7tNML4sfF5fWBQK XvcjH3yFZqWAM8kVJEU/pensoVtmSMh6r9mcjAX29P0Fd+zKL3Kyyfv1eSFWvhxpzRB+ Snnoq96NpnP4l4KVI5f9PmRlcu0mcDaWQYutuuPmVWbbdYRV+vMzldbzg7RXjK07pQeh fRkVasuKGhopcP9oVgfN0daFSg2A9sp92qdqCGjxD67nkRh+MQSLwOXGlS4HRvNCqflm NetcDnKkeYfWvCN4YvKQ6RGSDiTJjCkymfN77GYEVNl0Sp/ljAN0ExhlU/etPU5nItdu dbRQ== X-Gm-Message-State: AJIora8zGcDI99gXtn5aSBXpVsx6rjXr/CbcVKYcW1UdK5pBUufdu5m9 XFXfz7LoUZrnbU/p7Hbsupw= X-Google-Smtp-Source: AGRyM1sdZMmftr/KkG0O9vbX2TeMeEgbKE13w7eLi0Z4jVWOmOqnnMVR0aKuAFmyIwLzn68++I6Nzw== X-Received: by 2002:a2e:6f03:0:b0:25a:74b7:3a59 with SMTP id k3-20020a2e6f03000000b0025a74b73a59mr5288610ljc.390.1656004036857; Thu, 23 Jun 2022 10:07: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 w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07: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 09/10] iio: adc: mcp3911: make use of the sign bit Date: Thu, 23 Jun 2022 19:08:43 +0200 Message-Id: <20220623170844.2189814-9-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-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 --- 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 aefc5eac874c..084f6d1aa6d1 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 Thu Jun 23 17:08: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: 12893010 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 88AD9CCA486 for ; Thu, 23 Jun 2022 17:40:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234488AbiFWRkF (ORCPT ); Thu, 23 Jun 2022 13:40:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234638AbiFWRiB (ORCPT ); Thu, 23 Jun 2022 13:38:01 -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 2A4BE517F4; Thu, 23 Jun 2022 10:07:20 -0700 (PDT) Received: by mail-lj1-x230.google.com with SMTP id s14so17321366ljs.3; Thu, 23 Jun 2022 10:07: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=Buq93uC223YN7gDu+OZOG27+jvtgYZSgy8F1DGxsPvY=; b=h+XVLeEjU4ZDDxHet9bxgDp2Rg3mEyuWA4cLjmZ6bS+2YNZvOrAfSoWnBR3DEtIGdu OR7GmIZqqmzhdSXWOHcPOJC6c67LkBLz6qnbOPBxkDHLKc8EUBXhFybAyxYh2ATiq3vL /tpBNSUCkZJ9nbM3/MEshdvXhKv//ubkd7GEJbcJon0vyUsdUIRJjJksDYoitqrdLBe3 OzPPddVOnKwj9QzQhLKmU4v4oujPiS54YhElaGgMmTmTI+Izfe/jYSSvSTAULYWGR9zO 6FOMOxF+ZCvlTqyBbRc/KKcLbf6cmL6MydSgsK5LedMxYiT8433y+ExZ69PRzB7dFsat gz4A== 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=Buq93uC223YN7gDu+OZOG27+jvtgYZSgy8F1DGxsPvY=; b=woa8n6nAvqCQq+obNp+hkVgQXC+vhHVIODOwSiB4Suvj4PFmJA9HwWVzIT9vLnRAO1 TJZqNbBxw8KKRWehWCanv79PgUSmIxyhA2O5CgfIEfq6WsEunoVfpTWHWTn15RnC2onu X7uG/2Qsrs4fGzPiqiIdwkdRj7YHqHiDmPCbPd+fV09SJ2ApSzdMpKfkJpAph9dmmyWW ivnXMQLof4HB9i7M+LkVRanEDZS9G2RhllaimOX7nAqgJe8tbZVfsfeMaI6bBKJf2tm9 O08k+/JNXhrq9nKkHW+kqxgJpQXXwtoAMaY5qz4qVp+xlhpiIrTuj4efx+EaTySVTy+m GbRQ== X-Gm-Message-State: AJIora+NZUt2/jdHlV8vcFkd4cbedrM8wI6uRzOgsARwyc9IuaJl4zX6 cC4umE3EwZ++LDvzPtZf2++BeE5jEZ+NQA== X-Google-Smtp-Source: AGRyM1uz4+ft3o2Wx7KjcSnvtK4U/kgA0UmYvAX8dk8vkCbHkmGTkJTjjy9DHcgDlpQQ4Xxx00I39g== X-Received: by 2002:a2e:a7c7:0:b0:255:8ecd:14b0 with SMTP id x7-20020a2ea7c7000000b002558ecd14b0mr5442305ljp.472.1656004038484; Thu, 23 Jun 2022 10:07: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 w27-20020a19491b000000b0047a0bf9540asm3060405lfa.213.2022.06.23.10.07.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 10:07: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 10/10] iio: adc: mcp3911: add support to set PGA Date: Thu, 23 Jun 2022 19:08:44 +0200 Message-Id: <20220623170844.2189814-10-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623170844.2189814-1-marcus.folkesson@gmail.com> References: <20220623170844.2189814-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 | 135 +++++++++++++++++++++++++++++++------- 1 file changed, 110 insertions(+), 25 deletions(-) diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c index 084f6d1aa6d1..631015ead8e7 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,7 +118,35 @@ 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 = mcp3911_read(adc, MCP3911_REG_GAIN, val, 1); + + if (ret) + return ret; + *val >>= channel * 3; + *val &= 0x07; + *val = (1 << *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, @@ -125,6 +158,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 +218,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 +238,19 @@ 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; @@ -290,6 +321,49 @@ 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 = 0; + 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); + goto out; + } + + 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; + } + +out: + return ret; +} + #define MCP3911_CHAN(idx) { \ .type = IIO_VOLTAGE, \ .indexed = 1, \ @@ -302,8 +376,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 = \ - BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .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, \ @@ -356,6 +432,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) @@ -482,6 +559,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]); + dr_hiz = device_property_read_bool(&adc->spi->dev, "microchip,data-ready-hiz"); if (dr_hiz) ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ,