Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3207929pxv; Sun, 4 Jul 2021 11:02:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPLaA/DRuGukl8JFRpsxgxma2wPisExtaobaGRARK3nnGMsIaHxgF2TQrezQXa3zNNJXAZ X-Received: by 2002:a92:d10c:: with SMTP id a12mr1180787ilb.100.1625421772804; Sun, 04 Jul 2021 11:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625421772; cv=none; d=google.com; s=arc-20160816; b=MGzgZLGUP866SrzTLQpClMavZBaQY7YrdvgIm68Cg1RBmoPb56R2MZDV7VTw8syi/k 8cJHabzETcw3oGIQq56MlGnsjeaeipDy+LEbpH1DD031c9f546G3HaYhf6csYJGJT+Ll IS97O7wN8fEn2LQiJhPk/6789wculmvzRXoOAmGW9s/VDW8EU6OAoYlnOVHeHz55ok6Q s9LbukVpwCWNgnVy+3a8XaluV0ybaiAA3INzz9V9SD/dEu55SnSbi0bnN35yuYvB9PeZ uYpXqqxECL1VyK5HnftBoO5lrQ/BGrNLYDFUTnAvIurpTEXVk1Bi1yfIWI1uqgFujEA9 siVQ== 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:subject:cc:to:from:date; bh=du+1oRM0CqBEO3tdYGa7Igr9r2jujSSAO5N2UIMsVtg=; b=kT73a1NT3q824EM2F2uuQbOfVgwMWQauXIp5mr6Z83wVO9Mm10JG1O6LQJouU9Um8e V+SL1u71L5LEhGf0c6qQUhy4YAnNnuftwO0SEQG+zgoyt1pvfbBGAbbvIm1O/gM73ICE WiioroaoDYE4byy66D13XtTsZWchhDAE3C4C0I86knpbIFg7yeI+w4uaL7NfhRCpdsCL dF+o4urwvwnEW2S7kHO7VD4vEk7Ca3QNU7sX4gsCNbXwmJ4lQSJJ2ZSTcEynY3BDD+H8 pQVmJl9sH+TeJLN/5y6G+dzxKQ0OxDybaqpt0Uj+bwWiF+3Gy+6jp4oHD4au7zgOWmh+ ppNg== 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t6si13540637ilu.96.2021.07.04.11.02.41; Sun, 04 Jul 2021 11:02:52 -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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229724AbhGDSCN (ORCPT + 99 others); Sun, 4 Jul 2021 14:02:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:54758 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229636AbhGDSCM (ORCPT ); Sun, 4 Jul 2021 14:02:12 -0400 Received: from jic23-huawei (cpc108967-cmbg20-2-0-cust86.5-4.cable.virginm.net [81.101.6.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 772146128D; Sun, 4 Jul 2021 17:59:35 +0000 (UTC) Date: Sun, 4 Jul 2021 19:01:58 +0100 From: Jonathan Cameron To: Alexandru Ardelean Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, shawnguo@kernel.org, s.hauer@pengutronix.de Subject: Re: [PATCH] iio: adc: fsl-imx25-gcq: initialize regulators as needed Message-ID: <20210704190158.6676ab99@jic23-huawei> In-Reply-To: <20210625074325.9237-1-aardelean@deviqon.com> References: <20210625074325.9237-1-aardelean@deviqon.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 25 Jun 2021 10:43:25 +0300 Alexandru Ardelean wrote: > The driver tries to initialize all possible regulators from the DT, then > match the external regulators with each channel and then release all unused > regulators. > > We can change the logic a bit to initialize regulators only when at least > one channel needs them. > > This change creates a mx25_gcq_ext_regulator_setup() function that is > called only for the external regulators. If there's already a reference to > an external regulator, the function will just exit early with no error. > > This way, the driver doesn't need to keep any track of these regulators > during init. > > Signed-off-by: Alexandru Ardelean whilst I agree this is a bit cleaner, I definitely want to see review from those more familiar with the device before I take it! Thanks, Jonathan > --- > drivers/iio/adc/fsl-imx25-gcq.c | 57 ++++++++++++++++----------------- > 1 file changed, 28 insertions(+), 29 deletions(-) > > diff --git a/drivers/iio/adc/fsl-imx25-gcq.c b/drivers/iio/adc/fsl-imx25-gcq.c > index ab5139e911c3..31776f80f847 100644 > --- a/drivers/iio/adc/fsl-imx25-gcq.c > +++ b/drivers/iio/adc/fsl-imx25-gcq.c > @@ -172,13 +172,37 @@ static const struct regmap_config mx25_gcq_regconfig = { > .reg_stride = 4, > }; > > +static int mx25_gcq_ext_regulator_setup(struct device *dev, > + struct mx25_gcq_priv *priv, u32 refp) > +{ > + char reg_name[12]; > + int ret; > + > + if (priv->vref[refp]) > + return 0; > + > + ret = snprintf(reg_name, sizeof(reg_name), "vref-%s", > + mx25_gcq_refp_names[refp]); > + if (ret < 0) > + return ret; > + > + priv->vref[refp] = devm_regulator_get_optional(dev, reg_name); > + if (IS_ERR(priv->vref[refp])) { > + dev_err(dev, > + "Error, trying to use external voltage reference without a %s regulator.", > + reg_name); > + return PTR_ERR(priv->vref[refp]); > + } > + > + return 0; > +} > + > static int mx25_gcq_setup_cfgs(struct platform_device *pdev, > struct mx25_gcq_priv *priv) > { > struct device_node *np = pdev->dev.of_node; > struct device_node *child; > struct device *dev = &pdev->dev; > - unsigned int refp_used[4] = {}; > int ret, i; > > /* > @@ -194,19 +218,6 @@ static int mx25_gcq_setup_cfgs(struct platform_device *pdev, > MX25_ADCQ_CFG_IN(i) | > MX25_ADCQ_CFG_REFN_NGND2); > > - /* > - * First get all regulators to store them in channel_vref_mv if > - * necessary. Later we use that information for proper IIO scale > - * information. > - */ > - priv->vref[MX25_ADC_REFP_INT] = NULL; > - priv->vref[MX25_ADC_REFP_EXT] = > - devm_regulator_get_optional(&pdev->dev, "vref-ext"); > - priv->vref[MX25_ADC_REFP_XP] = > - devm_regulator_get_optional(&pdev->dev, "vref-xp"); > - priv->vref[MX25_ADC_REFP_YP] = > - devm_regulator_get_optional(&pdev->dev, "vref-yp"); > - > for_each_child_of_node(np, child) { > u32 reg; > u32 refp = MX25_ADCQ_CFG_REFP_INT; > @@ -233,11 +244,10 @@ static int mx25_gcq_setup_cfgs(struct platform_device *pdev, > case MX25_ADC_REFP_EXT: > case MX25_ADC_REFP_XP: > case MX25_ADC_REFP_YP: > - if (IS_ERR(priv->vref[refp])) { > - dev_err(dev, "Error, trying to use external voltage reference without a vref-%s regulator.", > - mx25_gcq_refp_names[refp]); > + ret = mx25_gcq_ext_regulator_setup(&pdev->dev, priv, refp); > + if (ret) { > of_node_put(child); > - return PTR_ERR(priv->vref[refp]); > + return ret; > } > priv->channel_vref_mv[reg] = > regulator_get_voltage(priv->vref[refp]); > @@ -253,8 +263,6 @@ static int mx25_gcq_setup_cfgs(struct platform_device *pdev, > return -EINVAL; > } > > - ++refp_used[refp]; > - > /* > * Shift the read values to the correct positions within the > * register. > @@ -285,15 +293,6 @@ static int mx25_gcq_setup_cfgs(struct platform_device *pdev, > regmap_write(priv->regs, MX25_ADCQ_CR, > MX25_ADCQ_CR_PDMSK | MX25_ADCQ_CR_QSM_FQS); > > - /* Remove unused regulators */ > - for (i = 0; i != 4; ++i) { > - if (!refp_used[i]) { > - if (!IS_ERR_OR_NULL(priv->vref[i])) > - devm_regulator_put(priv->vref[i]); > - priv->vref[i] = NULL; > - } > - } > - > return 0; > } >