Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp359993pxb; Wed, 18 Aug 2021 04:14:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJytVKFBUjLYXrNaf/dYUi+FW92Mlzlc3Dni1zeIGFhndYaxZO3llVQf1to0cgYOOINDc9HR X-Received: by 2002:a17:906:d1d6:: with SMTP id bs22mr8988929ejb.554.1629285290516; Wed, 18 Aug 2021 04:14:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629285290; cv=none; d=google.com; s=arc-20160816; b=ZglaY3DWd5J4CDLEkrtv5B0mG9wOsQNEspYKd/9ixDVnEGw19v8Iy8gAufFe3Dtdbp q0r4+6i/aGotIdJn7x5P/szpdpJ/eJdnNwOzU/u2ZZborrjfgtyMoUf5GVZjNrCyWOOP yK/MLZhhH2oYZwp0hwvUYqtGQbRTYrE8WcnLNPS2Wa9x3qia09/hUqc+laS8zDLUzpPU rA2cmBrxD0Gp6jr9sijJ8wQGzK7uRIv1B8bI+dWs4Xx1LQwuY8y0ZdKRuHfYIzfM9NJO EvL8Tq69zSORib122ZuhvPONdwbDbDvNvO+cad8veSgFzotjCMEXJdfWqJIXLp48QYAQ tZ4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=MU192yEjIp/NvdyFHwHm1kwlpByP4TAtu7ENUR8XMSQ=; b=x1Cynv45IUg96x1J0t+gdBww/zYYIT+EVXtAIMxMp5zKxdJhMOuM9rPkSv2IlQms+Q WGDf7TOkt1ymWrH93IVccvzx9uwF0s6A5G4aI8dQCRvmE277zAAPCiFm/A8wJCv8XJ79 I2kfFVxCsVVyFaQdVCNRCubyL1if0EdvCBKK7Ts5W1TNORBly15FgOAGuK3GBz+7xuoq 5y7UROMbGWWvgUPGX0uNkm54XwZa9gAmxY1+q5NwX0W2hd39cG7WkxDJIMYAL9OFePqW aS1S1sl3uySrUd//tO0CVRY5RV2xIn8u8Xu7nCoKV5fnY6PYNPh4WzOvr4toV7RcxrXs puaw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dy18si5293531edb.594.2021.08.18.04.14.27; Wed, 18 Aug 2021 04:14:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235606AbhHRLNG (ORCPT + 99 others); Wed, 18 Aug 2021 07:13:06 -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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 --- 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; } -- 2.27.0