From patchwork Wed Aug 18 11:11:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444003 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5965DC432BE for ; Wed, 18 Aug 2021 11:11:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 344196109F for ; Wed, 18 Aug 2021 11:11:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235092AbhHRLMR (ORCPT ); Wed, 18 Aug 2021 07:12:17 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:45679 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235207AbhHRLMR (ORCPT ); Wed, 18 Aug 2021 07:12:17 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 6DBF66000A; Wed, 18 Aug 2021 11:11:40 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal , stable@vger.kernel.org Subject: [PATCH 01/16] iio: adc: max1027: Fix wrong shift with 12-bit devices Date: Wed, 18 Aug 2021 13:11:24 +0200 Message-Id: <20210818111139.330636-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org 10-bit devices must shift the value twice. This is not needed anymore on 12-bit devices. Fixes: ae47d009b508 ("iio: adc: max1027: Introduce 12-bit devices support") Cc: stable@vger.kernel.org Signed-off-by: Miquel Raynal Reviewed-by: Nuno Sá --- drivers/iio/adc/max1027.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 655ab02d03d8..4a42d140a4b0 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -103,7 +103,7 @@ MODULE_DEVICE_TABLE(of, max1027_adc_dt_ids); .sign = 'u', \ .realbits = depth, \ .storagebits = 16, \ - .shift = 2, \ + .shift = (depth == 10) ? 2 : 0, \ .endianness = IIO_BE, \ }, \ } From patchwork Wed Aug 18 11:11:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444005 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3DF6C4320E for ; Wed, 18 Aug 2021 11:11:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A73466108F for ; Wed, 18 Aug 2021 11:11:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235379AbhHRLMS (ORCPT ); Wed, 18 Aug 2021 07:12:18 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:37771 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235253AbhHRLMR (ORCPT ); Wed, 18 Aug 2021 07:12:17 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 2E47260009; Wed, 18 Aug 2021 11:11:41 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal , stable@vger.kernel.org Subject: [PATCH 02/16] iio: adc: max1027: Fix the number of max1X31 channels Date: Wed, 18 Aug 2021 13:11:25 +0200 Message-Id: <20210818111139.330636-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The macro MAX1X29_CHANNELS() already calls MAX1X27_CHANNELS(). Calling MAX1X27_CHANNELS() before MAX1X29_CHANNELS() in the definition of MAX1X31_CHANNELS() declares the first 8 channels twice. So drop this extra call from the MAX1X31 channels list definition. Fixes: 7af5257d8427 ("iio: adc: max1027: Prepare the introduction of different resolutions") Cc: stable@vger.kernel.org Signed-off-by: Miquel Raynal Reviewed-by: Nuno Sá --- drivers/iio/adc/max1027.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 4a42d140a4b0..b753658bb41e 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -142,7 +142,6 @@ MODULE_DEVICE_TABLE(of, max1027_adc_dt_ids); MAX1027_V_CHAN(11, depth) #define MAX1X31_CHANNELS(depth) \ - MAX1X27_CHANNELS(depth), \ MAX1X29_CHANNELS(depth), \ MAX1027_V_CHAN(12, depth), \ MAX1027_V_CHAN(13, depth), \ From patchwork Wed Aug 18 11:11:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444007 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52C93C43214 for ; Wed, 18 Aug 2021 11:11:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3D5CE610E6 for ; Wed, 18 Aug 2021 11:11:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235459AbhHRLMT (ORCPT ); Wed, 18 Aug 2021 07:12:19 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:51527 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233798AbhHRLMR (ORCPT ); Wed, 18 Aug 2021 07:12:17 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id EEA666000E; Wed, 18 Aug 2021 11:11:41 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 03/16] iio: adc: max1027: Push only the requested samples Date: Wed, 18 Aug 2021 13:11:26 +0200 Message-Id: <20210818111139.330636-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org When a triggered scan occurs, the identity of the desired channels is known in indio_dev->active_scan_mask. Instead of reading and pushing to the IIO buffers all channels each time, scan the minimum amount of channels (0 to maximum requested chan, to be exact) and only provide the samples requested by the user. For example, if the user wants channels 1, 4 and 5, all channels from 0 to 5 will be scanned but only the desired channels will be pushed to the IIO buffers. Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index b753658bb41e..8ab660f596b5 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -360,6 +360,9 @@ static int max1027_set_trigger_state(struct iio_trigger *trig, bool state) struct max1027_state *st = iio_priv(indio_dev); int ret; + if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) + return -EINVAL; + if (state) { /* Start acquisition on cnvst */ st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 | @@ -368,9 +371,12 @@ static int max1027_set_trigger_state(struct iio_trigger *trig, bool state) if (ret < 0) return ret; - /* Scan from 0 to max */ - st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) | - MAX1027_SCAN_N_M | MAX1027_TEMP; + /* + * Scan from 0 to the highest requested channel. The temperature + * could be avoided but it simplifies a bit the logic. + */ + st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N | MAX1027_TEMP; + st->reg |= MAX1027_CHAN(fls(*indio_dev->active_scan_mask) - 2); ret = spi_write(st->spi, &st->reg, 1); if (ret < 0) return ret; @@ -391,11 +397,22 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private) struct iio_poll_func *pf = private; struct iio_dev *indio_dev = pf->indio_dev; struct max1027_state *st = iio_priv(indio_dev); + unsigned int scanned_chans = fls(*indio_dev->active_scan_mask); + u16 *buf = st->buffer; + unsigned int bit; pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private); /* fill buffer with all channel */ - spi_read(st->spi, st->buffer, indio_dev->masklength * 2); + spi_read(st->spi, st->buffer, scanned_chans * 2); + + /* Only keep the channels selected by the user */ + for_each_set_bit(bit, indio_dev->active_scan_mask, + indio_dev->masklength) { + if (buf[0] != st->buffer[bit]) + buf[0] = st->buffer[bit]; + buf++; + } iio_push_to_buffers(indio_dev, st->buffer); From patchwork Wed Aug 18 11:11:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444013 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA2C3C432BE for ; Wed, 18 Aug 2021 11:11:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C22BF610A1 for ; Wed, 18 Aug 2021 11:11:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235483AbhHRLMV (ORCPT ); Wed, 18 Aug 2021 07:12:21 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:43365 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235143AbhHRLMS (ORCPT ); Wed, 18 Aug 2021 07:12:18 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id A0CA560006; Wed, 18 Aug 2021 11:11:42 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 04/16] iio: adc: max1027: Lower conversion time Date: Wed, 18 Aug 2021 13:11:27 +0200 Message-Id: <20210818111139.330636-5-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org When only a few channels are requested, the core will request all of them as long as ->available_scan_masks is present, thus reducing the impact of any driver side optimization to avoid converting the unneeded channels. Do not share this bitmap with the core, which is optional anyway. Signed-off-by: Miquel Raynal Reviewed-by: Nuno Sá --- drivers/iio/adc/max1027.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 8ab660f596b5..d79dabf20567 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -457,7 +457,6 @@ static int max1027_probe(struct spi_device *spi) indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->channels = st->info->channels; indio_dev->num_channels = st->info->num_channels; - indio_dev->available_scan_masks = st->info->available_scan_masks; st->buffer = devm_kmalloc_array(&indio_dev->dev, indio_dev->num_channels, 2, From patchwork Wed Aug 18 11:11:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444009 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BCA20C4338F for ; Wed, 18 Aug 2021 11:11:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A79656108F for ; Wed, 18 Aug 2021 11:11:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235508AbhHRLMV (ORCPT ); Wed, 18 Aug 2021 07:12:21 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:36135 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235405AbhHRLMT (ORCPT ); Wed, 18 Aug 2021 07:12:19 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 583D260003; Wed, 18 Aug 2021 11:11:43 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 05/16] iio: adc: max1027: Drop extra warning message Date: Wed, 18 Aug 2021 13:11:28 +0200 Message-Id: <20210818111139.330636-6-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Memory allocation errors automatically trigger the right logs, no need to have our own. Signed-off-by: Miquel Raynal Reviewed-by: Nuno Sá --- drivers/iio/adc/max1027.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index d79dabf20567..ac603b4ca787 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -461,10 +461,8 @@ static int max1027_probe(struct spi_device *spi) st->buffer = devm_kmalloc_array(&indio_dev->dev, indio_dev->num_channels, 2, GFP_KERNEL); - if (st->buffer == NULL) { - dev_err(&indio_dev->dev, "Can't allocate buffer\n"); + if (!st->buffer) return -ENOMEM; - } if (spi->irq) { ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, From patchwork Wed Aug 18 11:11:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444015 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF9A3C43214 for ; Wed, 18 Aug 2021 11:11:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C5B9C61058 for ; Wed, 18 Aug 2021 11:11:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235627AbhHRLMZ (ORCPT ); Wed, 18 Aug 2021 07:12:25 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:50709 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235210AbhHRLMT (ORCPT ); Wed, 18 Aug 2021 07:12:19 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 0091660005; Wed, 18 Aug 2021 11:11:43 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 06/16] iio: adc: max1027: Rename a helper Date: Wed, 18 Aug 2021 13:11:29 +0200 Message-Id: <20210818111139.330636-7-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Make it clear that the *_set_trigger_state() hook is responsible for cnvst based conversions by renaming the helper. This may avoid confusions with software trigger support that is going to be introduced. Signed-off-by: Miquel Raynal Reviewed-by: Nuno Sá --- drivers/iio/adc/max1027.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index ac603b4ca787..a6ebc951c09a 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -354,7 +354,7 @@ static int max1027_validate_trigger(struct iio_dev *indio_dev, return 0; } -static int max1027_set_trigger_state(struct iio_trigger *trig, bool state) +static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state) { struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); struct max1027_state *st = iio_priv(indio_dev); @@ -423,7 +423,7 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private) static const struct iio_trigger_ops max1027_trigger_ops = { .validate_device = &iio_trigger_validate_own_device, - .set_trigger_state = &max1027_set_trigger_state, + .set_trigger_state = &max1027_set_cnvst_trigger_state, }; static const struct iio_info max1027_info = { From patchwork Wed Aug 18 11:11:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444011 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15DA3C4320A for ; Wed, 18 Aug 2021 11:11:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F2CBA61058 for ; Wed, 18 Aug 2021 11:11:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235528AbhHRLMW (ORCPT ); Wed, 18 Aug 2021 07:12:22 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:43365 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235166AbhHRLMU (ORCPT ); Wed, 18 Aug 2021 07:12:20 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id AC2E860011; Wed, 18 Aug 2021 11:11:44 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 07/16] iio: adc: max1027: Create a helper to configure the trigger Date: Wed, 18 Aug 2021 13:11:30 +0200 Message-Id: <20210818111139.330636-8-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org There are two ways to physically trigger a conversion: - A falling edge on the cnvst pin - A write operation on the conversion register Let's create a helper for this. Signed-off-by: Miquel Raynal Reviewed-by: Nuno Sá --- drivers/iio/adc/max1027.c | 52 ++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index a6ebc951c09a..59914fcfd320 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -232,10 +232,37 @@ struct max1027_state { struct iio_trigger *trig; __be16 *buffer; struct mutex lock; - + bool cnvst_trigger; u8 reg ____cacheline_aligned; }; +static int max1027_configure_trigger(struct iio_dev *indio_dev) +{ + struct max1027_state *st = iio_priv(indio_dev); + int ret; + + st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2; + + /* + * Start acquisition on: + * MODE0: external hardware trigger wired to the cnvst input pin + * MODE2: conversion register write + */ + if (st->cnvst_trigger) + st->reg |= MAX1027_CKS_MODE0; + else + st->reg |= MAX1027_CKS_MODE2; + + ret = spi_write(st->spi, &st->reg, 1); + if (ret < 0) { + dev_err(&indio_dev->dev, + "Failed to configure register (%d)\n", ret); + return ret; + } + + return 0; +} + static int max1027_read_single_value(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val) @@ -248,14 +275,9 @@ static int max1027_read_single_value(struct iio_dev *indio_dev, return -EBUSY; } - /* Start acquisition on conversion register write */ - st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2; - ret = spi_write(st->spi, &st->reg, 1); - if (ret < 0) { - dev_err(&indio_dev->dev, - "Failed to configure setup register\n"); + ret = max1027_configure_trigger(indio_dev); + if (ret) return ret; - } /* Configure conversion register with the requested chan */ st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) | @@ -363,12 +385,10 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state) if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) return -EINVAL; + st->cnvst_trigger = state; if (state) { - /* Start acquisition on cnvst */ - st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 | - MAX1027_REF_MODE2; - ret = spi_write(st->spi, &st->reg, 1); - if (ret < 0) + ret = max1027_configure_trigger(indio_dev); + if (ret) return ret; /* @@ -382,10 +402,8 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state) return ret; } else { /* Start acquisition on conversion register write */ - st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2 | - MAX1027_REF_MODE2; - ret = spi_write(st->spi, &st->reg, 1); - if (ret < 0) + ret = max1027_configure_trigger(indio_dev); + if (ret) return ret; } From patchwork Wed Aug 18 11:11:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444017 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5C26C4338F for ; Wed, 18 Aug 2021 11:11:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B37B66108F for ; Wed, 18 Aug 2021 11:11:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235658AbhHRLM0 (ORCPT ); Wed, 18 Aug 2021 07:12:26 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:36135 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235464AbhHRLMU (ORCPT ); Wed, 18 Aug 2021 07:12:20 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 5C9D760010; Wed, 18 Aug 2021 11:11:45 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 08/16] iio: adc: max1027: Explain better how the trigger state gets changed Date: Wed, 18 Aug 2021 13:11:31 +0200 Message-Id: <20210818111139.330636-9-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org It may appear strange to configure the trigger to start upon register write while we want to disable the trigger. Explain why we do this in the comment. Signed-off-by: Miquel Raynal Reviewed-by: Nuno Sá --- drivers/iio/adc/max1027.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 59914fcfd320..4a78c9cbc339 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -401,7 +401,12 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state) if (ret < 0) return ret; } else { - /* Start acquisition on conversion register write */ + /* + * Start acquisition on conversion register write, which + * basically disables triggering conversions upon cnvst changes + * and thus has the effect of disabling the external hardware + * trigger. + */ ret = max1027_configure_trigger(indio_dev); if (ret) return ret; From patchwork Wed Aug 18 11:11:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1183C432BE for ; Wed, 18 Aug 2021 11:11:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BA25D61058 for ; Wed, 18 Aug 2021 11:11:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235718AbhHRLM2 (ORCPT ); Wed, 18 Aug 2021 07:12:28 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:50709 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235328AbhHRLMV (ORCPT ); Wed, 18 Aug 2021 07:12:21 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 0A6256000F; Wed, 18 Aug 2021 11:11:45 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 09/16] iio: adc: max1027: Create a helper to configure the channels to scan Date: Wed, 18 Aug 2021 13:11:32 +0200 Message-Id: <20210818111139.330636-10-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org These bits are meant to be reused for triggered buffers setup. Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 4a78c9cbc339..223c9e4abd86 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -236,6 +236,22 @@ struct max1027_state { u8 reg ____cacheline_aligned; }; +/* Scan from 0 to the highest requested channel */ +static int max1027_configure_chans_to_scan(struct iio_dev *indio_dev) +{ + struct max1027_state *st = iio_priv(indio_dev); + int ret; + + /* The temperature could be avoided but it simplifies a bit the logic */ + st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N | MAX1027_TEMP; + st->reg |= MAX1027_CHAN(fls(*indio_dev->active_scan_mask) - 2); + ret = spi_write(st->spi, &st->reg, 1); + if (ret < 0) + return ret; + + return 0; +} + static int max1027_configure_trigger(struct iio_dev *indio_dev) { struct max1027_state *st = iio_priv(indio_dev); @@ -391,14 +407,8 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state) if (ret) return ret; - /* - * Scan from 0 to the highest requested channel. The temperature - * could be avoided but it simplifies a bit the logic. - */ - st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N | MAX1027_TEMP; - st->reg |= MAX1027_CHAN(fls(*indio_dev->active_scan_mask) - 2); - ret = spi_write(st->spi, &st->reg, 1); - if (ret < 0) + ret = max1027_configure_chans_to_scan(indio_dev); + if (ret) return ret; } else { /* From patchwork Wed Aug 18 11:11:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444031 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66A33C432BE for ; Wed, 18 Aug 2021 11:12:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5005C61058 for ; Wed, 18 Aug 2021 11:12:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235981AbhHRLMv (ORCPT ); Wed, 18 Aug 2021 07:12:51 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:43365 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235207AbhHRLMW (ORCPT ); Wed, 18 Aug 2021 07:12:22 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id B29D76000C; Wed, 18 Aug 2021 11:11:46 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads Date: Wed, 18 Aug 2021 13:11:33 +0200 Message-Id: <20210818111139.330636-11-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org When hardware buffers are enabled (the cnvst pin being the trigger), one should not mess with the device state by requesting a single channel read. Prevent it with a iio_buffer_enabled() check. Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 223c9e4abd86..83526f3d7d3a 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -335,6 +335,8 @@ static int max1027_read_raw(struct iio_dev *indio_dev, switch (mask) { case IIO_CHAN_INFO_RAW: + if (iio_buffer_enabled(indio_dev)) + return -EBUSY; ret = max1027_read_single_value(indio_dev, chan, val); break; case IIO_CHAN_INFO_SCALE: From patchwork Wed Aug 18 11:11:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 162D8C4338F for ; Wed, 18 Aug 2021 11:12:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0220361058 for ; Wed, 18 Aug 2021 11:12:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235499AbhHRLM7 (ORCPT ); Wed, 18 Aug 2021 07:12:59 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:36135 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235539AbhHRLMW (ORCPT ); Wed, 18 Aug 2021 07:12:22 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 5E12B6000B; Wed, 18 Aug 2021 11:11:47 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 11/16] iio: adc: max1027: Separate the IRQ handler from the read logic Date: Wed, 18 Aug 2021 13:11:34 +0200 Message-Id: <20210818111139.330636-12-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Create a max1027_read_scan() helper which will make clearer the future IRQ handler updates (no functional change). Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 83526f3d7d3a..afc3ce69f7ea 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -427,19 +427,18 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state) return 0; } -static irqreturn_t max1027_trigger_handler(int irq, void *private) +static int max1027_read_scan(struct iio_dev *indio_dev) { - struct iio_poll_func *pf = private; - struct iio_dev *indio_dev = pf->indio_dev; struct max1027_state *st = iio_priv(indio_dev); unsigned int scanned_chans = fls(*indio_dev->active_scan_mask); u16 *buf = st->buffer; unsigned int bit; - - pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private); + int ret; /* fill buffer with all channel */ - spi_read(st->spi, st->buffer, scanned_chans * 2); + ret = spi_read(st->spi, st->buffer, scanned_chans * 2); + if (ret < 0) + return ret; /* Only keep the channels selected by the user */ for_each_set_bit(bit, indio_dev->active_scan_mask, @@ -451,6 +450,22 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private) iio_push_to_buffers(indio_dev, st->buffer); + return 0; +} + +static irqreturn_t max1027_trigger_handler(int irq, void *private) +{ + struct iio_poll_func *pf = private; + struct iio_dev *indio_dev = pf->indio_dev; + int ret; + + pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private); + + ret = max1027_read_scan(indio_dev); + if (ret) + dev_err(&indio_dev->dev, + "Cannot read scanned values (%d)\n", ret); + iio_trigger_notify_done(indio_dev->trig); return IRQ_HANDLED; From patchwork Wed Aug 18 11:11:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444027 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E6A2C4338F for ; Wed, 18 Aug 2021 11:12:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 562B9606A5 for ; Wed, 18 Aug 2021 11:12:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235568AbhHRLMp (ORCPT ); Wed, 18 Aug 2021 07:12:45 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:60351 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235551AbhHRLMX (ORCPT ); Wed, 18 Aug 2021 07:12:23 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 0CE8260012; Wed, 18 Aug 2021 11:11:47 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 12/16] iio: adc: max1027: Introduce an end of conversion helper Date: Wed, 18 Aug 2021 13:11:35 +0200 Message-Id: <20210818111139.330636-13-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org For now this helper only waits for the maximum duration of a conversion, but it will soon be improved to properly handle the end of conversion interrupt. Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index afc3ce69f7ea..2d6485591761 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -60,6 +60,9 @@ #define MAX1027_NAVG_32 (0x03 << 2) #define MAX1027_AVG_EN BIT(4) +/* Device can achieve 300ksps so we assume a 3.33us conversion delay */ +#define MAX1027_CONVERSION_UDELAY 4 + enum max1027_id { max1027, max1029, @@ -236,6 +239,20 @@ struct max1027_state { u8 reg ____cacheline_aligned; }; +static DECLARE_WAIT_QUEUE_HEAD(max1027_queue); + +static int max1027_wait_eoc(struct iio_dev *indio_dev) +{ + unsigned int conversion_time = MAX1027_CONVERSION_UDELAY; + + if (indio_dev->active_scan_mask) + conversion_time *= hweight32(*indio_dev->active_scan_mask); + + usleep_range(conversion_time, conversion_time * 2); + + return 0; +} + /* Scan from 0 to the highest requested channel */ static int max1027_configure_chans_to_scan(struct iio_dev *indio_dev) { @@ -310,9 +327,11 @@ static int max1027_read_single_value(struct iio_dev *indio_dev, /* * For an unknown reason, when we use the mode "10" (write * conversion register), the interrupt doesn't occur every time. - * So we just wait 1 ms. + * So we just wait the maximum conversion time and deliver the value. */ - mdelay(1); + ret = max1027_wait_eoc(indio_dev); + if (ret) + return ret; /* Read result */ ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2); From patchwork Wed Aug 18 11:11:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C9E1C4338F for ; Wed, 18 Aug 2021 11:11:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 07A4F61058 for ; Wed, 18 Aug 2021 11:11:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235588AbhHRLM3 (ORCPT ); Wed, 18 Aug 2021 07:12:29 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:50709 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235572AbhHRLMY (ORCPT ); Wed, 18 Aug 2021 07:12:24 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id AE75960013; Wed, 18 Aug 2021 11:11:48 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 13/16] iio: adc: max1027: Prepare re-using the EOC interrupt Date: Wed, 18 Aug 2021 13:11:36 +0200 Message-Id: <20210818111139.330636-14-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Right now the driver only has hardware trigger support, which makes use of the End Of Conversion (EOC) interrupt by: - Enabling the external trigger - At completion of the conversion, run a generic handler - Push the data to the IIO subsystem by using the triggered buffer infrastructure, which may not only serve this purpose. In fact, the interrupt will fire for more reasons than just a hardware trigger condition, so make a dedicated interrupt handler which will be enhanced by the upcoming changes to handle more situations. Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 2d6485591761..8d86e77fb5db 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -472,6 +472,24 @@ static int max1027_read_scan(struct iio_dev *indio_dev) return 0; } +static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) +{ + struct iio_dev *indio_dev = private; + struct max1027_state *st = iio_priv(indio_dev); + int ret = 0; + + if (st->cnvst_trigger) { + ret = max1027_read_scan(indio_dev); + iio_trigger_notify_done(indio_dev->trig); + } + + if (ret) + dev_err(&indio_dev->dev, + "Cannot read scanned values (%d)\n", ret); + + return IRQ_HANDLED; +} + static irqreturn_t max1027_trigger_handler(int irq, void *private) { struct iio_poll_func *pf = private; @@ -563,11 +581,11 @@ static int max1027_probe(struct spi_device *spi) } ret = devm_request_threaded_irq(&spi->dev, spi->irq, - iio_trigger_generic_data_rdy_poll, + max1027_eoc_irq_handler, NULL, IRQF_TRIGGER_FALLING, spi->dev.driver->name, - st->trig); + indio_dev); if (ret < 0) { dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n"); return ret; From patchwork Wed Aug 18 11:11:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16855C4338F for ; Wed, 18 Aug 2021 11:12:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F2B53606A5 for ; Wed, 18 Aug 2021 11:12:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235965AbhHRLMr (ORCPT ); Wed, 18 Aug 2021 07:12:47 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:57119 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235612AbhHRLMZ (ORCPT ); Wed, 18 Aug 2021 07:12:25 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 5D7C160014; Wed, 18 Aug 2021 11:11:49 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper Date: Wed, 18 Aug 2021 13:11:37 +0200 Message-Id: <20210818111139.330636-15-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Now that we have a dedicated handler for End Of Conversion interrupts, let's create a second path: - Situation 1: we are using the external hardware trigger, a conversion has been triggered and the ADC pushed the data to its FIFO, we need to retrieve the data and push it to the IIO buffers. - Situation 2: we are not using the external hardware trigger, hence we are likely waiting in a blocked thread waiting for this interrupt to happen: in this case we just wake up the waiting thread. Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 8d86e77fb5db..8c5995ae59f2 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -235,6 +235,7 @@ struct max1027_state { struct iio_trigger *trig; __be16 *buffer; struct mutex lock; + bool data_rdy; bool cnvst_trigger; u8 reg ____cacheline_aligned; }; @@ -243,12 +244,22 @@ static DECLARE_WAIT_QUEUE_HEAD(max1027_queue); static int max1027_wait_eoc(struct iio_dev *indio_dev) { + struct max1027_state *st = iio_priv(indio_dev); unsigned int conversion_time = MAX1027_CONVERSION_UDELAY; + int ret; - if (indio_dev->active_scan_mask) - conversion_time *= hweight32(*indio_dev->active_scan_mask); + if (st->spi->irq) { + ret = wait_event_interruptible_timeout(max1027_queue, + st->data_rdy, HZ / 1000); + st->data_rdy = false; + if (ret == -ERESTARTSYS) + return ret; + } else { + if (indio_dev->active_scan_mask) + conversion_time *= hweight32(*indio_dev->active_scan_mask); - usleep_range(conversion_time, conversion_time * 2); + usleep_range(conversion_time, conversion_time * 2); + } return 0; } @@ -481,6 +492,9 @@ static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) if (st->cnvst_trigger) { ret = max1027_read_scan(indio_dev); iio_trigger_notify_done(indio_dev->trig); + } else { + st->data_rdy = true; + wake_up(&max1027_queue); } if (ret) From patchwork Wed Aug 18 11:11:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00540C4338F for ; Wed, 18 Aug 2021 11:12:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E17A461058 for ; Wed, 18 Aug 2021 11:12:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235813AbhHRLMl (ORCPT ); Wed, 18 Aug 2021 07:12:41 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:39539 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235629AbhHRLMZ (ORCPT ); Wed, 18 Aug 2021 07:12:25 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 037786000A; Wed, 18 Aug 2021 11:11:49 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 15/16] iio: adc: max1027: Support software triggers Date: Wed, 18 Aug 2021 13:11:38 +0200 Message-Id: <20210818111139.330636-16-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Now that max1027_trigger_handler() has been freed from handling hardware triggers EOC situations, we can use it for what it has been designed in the first place: trigger software originated conversions. In other words, when userspace initiates a conversion with a sysfs trigger or a hrtimer trigger, we must do all configuration steps, ie: 1- Configuring the trigger 2- Configuring the channels to scan 3- Starting the conversion (actually done automatically by step 2 in this case) 4- Waiting for the conversion to end 5- Retrieving the data from the ADC 6- Push the data to the IIO core and notify it Add the missing steps to this helper and drop the trigger verification hook otherwise software triggers would simply not be accepted at all. Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index 8c5995ae59f2..bb437e43adaf 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -413,17 +413,6 @@ static int max1027_debugfs_reg_access(struct iio_dev *indio_dev, return spi_write(st->spi, val, 1); } -static int max1027_validate_trigger(struct iio_dev *indio_dev, - struct iio_trigger *trig) -{ - struct max1027_state *st = iio_priv(indio_dev); - - if (st->trig != trig) - return -EINVAL; - - return 0; -} - static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state) { struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); @@ -512,7 +501,21 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private) pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private); + ret = max1027_configure_trigger(indio_dev); + if (ret) + goto out; + + ret = max1027_configure_chans_to_scan(indio_dev); + if (ret) + goto out; + + ret = max1027_wait_eoc(indio_dev); + if (ret) + goto out; + ret = max1027_read_scan(indio_dev); + +out: if (ret) dev_err(&indio_dev->dev, "Cannot read scanned values (%d)\n", ret); @@ -529,7 +532,6 @@ static const struct iio_trigger_ops max1027_trigger_ops = { static const struct iio_info max1027_info = { .read_raw = &max1027_read_raw, - .validate_trigger = &max1027_validate_trigger, .debugfs_reg_access = &max1027_debugfs_reg_access, }; From patchwork Wed Aug 18 11:11:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 12444023 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B270DC432BE for ; Wed, 18 Aug 2021 11:11:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 96B0D6109E for ; Wed, 18 Aug 2021 11:11:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235655AbhHRLMa (ORCPT ); Wed, 18 Aug 2021 07:12:30 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:57015 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235649AbhHRLM0 (ORCPT ); Wed, 18 Aug 2021 07:12:26 -0400 Received: (Authenticated sender: miquel.raynal@bootlin.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id AD88B60016; Wed, 18 Aug 2021 11:11:50 +0000 (UTC) From: Miquel Raynal To: Jonathan Cameron , Lars-Peter Clausen Cc: Thomas Petazzoni , linux-iio@vger.kernel.org, , Miquel Raynal Subject: [PATCH 16/16] iio: adc: max1027: Enable software triggers to be used without IRQ Date: Wed, 18 Aug 2021 13:11:39 +0200 Message-Id: <20210818111139.330636-17-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210818111139.330636-1-miquel.raynal@bootlin.com> References: <20210818111139.330636-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Software triggers do not need a device IRQ to work. As opposed to hardware triggers which need it to yield the data to the IIO core, software triggers run a dedicated thread which does all the tasks on their behalf. Then, the end of conversion status may either come from an interrupt or from a sufficient enough extra delay. IRQs are not mandatory so move the triggered buffer setup out of the IRQ condition. Signed-off-by: Miquel Raynal --- drivers/iio/adc/max1027.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c index bb437e43adaf..e767437a578e 100644 --- a/drivers/iio/adc/max1027.c +++ b/drivers/iio/adc/max1027.c @@ -567,16 +567,18 @@ static int max1027_probe(struct spi_device *spi) if (!st->buffer) return -ENOMEM; + /* Enable triggered buffers */ + ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, + &iio_pollfunc_store_time, + &max1027_trigger_handler, + NULL); + if (ret < 0) { + dev_err(&indio_dev->dev, "Failed to setup buffer\n"); + return ret; + } + + /* If there is an EOC interrupt, enable the hardware trigger (cnvst) */ if (spi->irq) { - ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, - &iio_pollfunc_store_time, - &max1027_trigger_handler, - NULL); - if (ret < 0) { - dev_err(&indio_dev->dev, "Failed to setup buffer\n"); - return ret; - } - st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-trigger", indio_dev->name); if (st->trig == NULL) {