diff mbox series

[v9,5/8] PCI: imx6: Refine the regulator usage

Message ID 1651801629-30223-6-git-send-email-hongxing.zhu@nxp.com
State New
Headers show
Series PCI: imx6: refine codes and add compliance tests mode support | expand

Commit Message

Hongxing Zhu May 6, 2022, 1:47 a.m. UTC
The driver should undo any enables it did itself. The regulator disable
shouldn't be basing decisions on regulator_is_enabled().

To keep the balance of the regulator usage counter, disable the regulator
just behind of imx6_pcie_assert_core_reset() in resume and shutdown.

Signed-off-by: Richard Zhu <hongxing.zhu@nxp.com>
---
 drivers/pci/controller/dwc/pci-imx6.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

Comments

Lucas Stach June 8, 2022, 7:26 a.m. UTC | #1
Am Freitag, dem 06.05.2022 um 09:47 +0800 schrieb Richard Zhu:
> The driver should undo any enables it did itself. The regulator disable
> shouldn't be basing decisions on regulator_is_enabled().
> 
> To keep the balance of the regulator usage counter, disable the regulator
> just behind of imx6_pcie_assert_core_reset() in resume and shutdown.
> 
> Signed-off-by: Richard Zhu <hongxing.zhu@nxp.com>
> ---
>  drivers/pci/controller/dwc/pci-imx6.c | 19 +++++++------------
>  1 file changed, 7 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c
> index 7005a7910003..3ce3993d5797 100644
> --- a/drivers/pci/controller/dwc/pci-imx6.c
> +++ b/drivers/pci/controller/dwc/pci-imx6.c
> @@ -369,8 +369,6 @@ static int imx6_pcie_attach_pd(struct device *dev)
>  
>  static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
>  {
> -	struct device *dev = imx6_pcie->pci->dev;
> -
>  	switch (imx6_pcie->drvdata->variant) {
>  	case IMX7D:
>  	case IMX8MQ:
> @@ -400,14 +398,6 @@ static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
>  				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
>  		break;
>  	}
> -
> -	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
> -		int ret = regulator_disable(imx6_pcie->vpcie);
> -
> -		if (ret)
> -			dev_err(dev, "failed to disable vpcie regulator: %d\n",
> -				ret);
> -	}
>  }
>  
>  static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
> @@ -580,7 +570,7 @@ static int imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
>  	struct device *dev = pci->dev;
>  	int ret, err;
>  
> -	if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
> +	if (imx6_pcie->vpcie) {
>  		ret = regulator_enable(imx6_pcie->vpcie);
>  		if (ret) {
>  			dev_err(dev, "failed to enable vpcie regulator: %d\n",
> @@ -653,7 +643,7 @@ static int imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
>  	return 0;
>  
>  err_clks:
> -	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
> +	if (imx6_pcie->vpcie) {
>  		ret = regulator_disable(imx6_pcie->vpcie);
>  		if (ret)
>  			dev_err(dev, "failed to disable vpcie regulator: %d\n",
> @@ -1026,6 +1016,9 @@ static int imx6_pcie_resume_noirq(struct device *dev)
>  		return 0;
>  
>  	imx6_pcie_assert_core_reset(imx6_pcie);
> +	if (imx6_pcie->vpcie)
> +		regulator_disable(imx6_pcie->vpcie);
> +
This one looks misplaced. Surely you want the regulator to be on when
resuming the PCIe subsystem. Isn't this just papering over a wrong
usage count here, because there is no regulator_disable in
imx6_pcie_suspend_noirq, where I would expect this to happen?

Regards,
Lucas

>  	imx6_pcie_init_phy(imx6_pcie);
>  	imx6_pcie_deassert_core_reset(imx6_pcie);
>  	dw_pcie_setup_rc(pp);
> @@ -1259,6 +1252,8 @@ static void imx6_pcie_shutdown(struct platform_device *pdev)
>  
>  	/* bring down link, so bootloader gets clean state in case of reboot */
>  	imx6_pcie_assert_core_reset(imx6_pcie);
> +	if (imx6_pcie->vpcie)
> +		regulator_disable(imx6_pcie->vpcie);
>  }
>  
>  static const struct imx6_pcie_drvdata drvdata[] = {
Bjorn Helgaas June 8, 2022, 6:54 p.m. UTC | #2
On Fri, May 06, 2022 at 09:47:06AM +0800, Richard Zhu wrote:
> The driver should undo any enables it did itself. The regulator disable
> shouldn't be basing decisions on regulator_is_enabled().
> 
> To keep the balance of the regulator usage counter, disable the regulator
> just behind of imx6_pcie_assert_core_reset() in resume and shutdown.

In subject, "Refine" doesn't tell me anything about what's happening
here.
Hongxing Zhu June 9, 2022, 6:17 a.m. UTC | #3
> -----Original Message-----
> From: Lucas Stach <l.stach@pengutronix.de>
> Sent: 2022年6月8日 15:27
> To: Hongxing Zhu <hongxing.zhu@nxp.com>; bhelgaas@google.com;
> robh+dt@kernel.org; broonie@kernel.org; lorenzo.pieralisi@arm.com;
> jingoohan1@gmail.com; festevam@gmail.com;
> francesco.dolcini@toradex.com
> Cc: linux-pci@vger.kernel.org; linux-arm-kernel@lists.infradead.org;
> linux-kernel@vger.kernel.org; kernel@pengutronix.de; dl-linux-imx
> <linux-imx@nxp.com>
> Subject: Re: [PATCH v9 5/8] PCI: imx6: Refine the regulator usage
> 
> Am Freitag, dem 06.05.2022 um 09:47 +0800 schrieb Richard Zhu:
> > The driver should undo any enables it did itself. The regulator
> > disable shouldn't be basing decisions on regulator_is_enabled().
> >
> > To keep the balance of the regulator usage counter, disable the
> > regulator just behind of imx6_pcie_assert_core_reset() in resume and
> shutdown.
> >
> > Signed-off-by: Richard Zhu <hongxing.zhu@nxp.com>
> > ---
> >  drivers/pci/controller/dwc/pci-imx6.c | 19 +++++++------------
> >  1 file changed, 7 insertions(+), 12 deletions(-)
> >
> > diff --git a/drivers/pci/controller/dwc/pci-imx6.c
> > b/drivers/pci/controller/dwc/pci-imx6.c
> > index 7005a7910003..3ce3993d5797 100644
> > --- a/drivers/pci/controller/dwc/pci-imx6.c
> > +++ b/drivers/pci/controller/dwc/pci-imx6.c
> > @@ -369,8 +369,6 @@ static int imx6_pcie_attach_pd(struct device *dev)
> >
> >  static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
> > {
> > -	struct device *dev = imx6_pcie->pci->dev;
> > -
> >  	switch (imx6_pcie->drvdata->variant) {
> >  	case IMX7D:
> >  	case IMX8MQ:
> > @@ -400,14 +398,6 @@ static void imx6_pcie_assert_core_reset(struct
> imx6_pcie *imx6_pcie)
> >  				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
> >  		break;
> >  	}
> > -
> > -	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
> > -		int ret = regulator_disable(imx6_pcie->vpcie);
> > -
> > -		if (ret)
> > -			dev_err(dev, "failed to disable vpcie regulator: %d\n",
> > -				ret);
> > -	}
> >  }
> >
> >  static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie
> > *imx6_pcie) @@ -580,7 +570,7 @@ static int
> imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
> >  	struct device *dev = pci->dev;
> >  	int ret, err;
> >
> > -	if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
> > +	if (imx6_pcie->vpcie) {
> >  		ret = regulator_enable(imx6_pcie->vpcie);
> >  		if (ret) {
> >  			dev_err(dev, "failed to enable vpcie regulator: %d\n", @@
> -653,7
> > +643,7 @@ static int imx6_pcie_deassert_core_reset(struct imx6_pcie
> *imx6_pcie)
> >  	return 0;
> >
> >  err_clks:
> > -	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
> > +	if (imx6_pcie->vpcie) {
> >  		ret = regulator_disable(imx6_pcie->vpcie);
> >  		if (ret)
> >  			dev_err(dev, "failed to disable vpcie regulator: %d\n", @@
> -1026,6
> > +1016,9 @@ static int imx6_pcie_resume_noirq(struct device *dev)
> >  		return 0;
> >
> >  	imx6_pcie_assert_core_reset(imx6_pcie);
> > +	if (imx6_pcie->vpcie)
> > +		regulator_disable(imx6_pcie->vpcie);
> > +
> This one looks misplaced. Surely you want the regulator to be on when
> resuming the PCIe subsystem. Isn't this just papering over a wrong usage count
> here, because there is no regulator_disable in imx6_pcie_suspend_noirq,
> where I would expect this to happen?
> 
Hi Lucas:
Thanks for your comments.
There was one regulator_disable() operation at the end of the
 imx6_pcie_assert_core_reset() function before.
When create the 5/8 patch, I follow the same behavior to disable the
regulator just behind the imx6_pcie_assert_core_reset() function.

Yes, it is. Imx6_pcie_suspend_noirq doesn't have the regulator_disable.
The regulaor_enable is contained in imx6_pcie_deassert_core_reset().
Both of the regulator_disable and regulator_enabe are invoked once in
 imx6_pcie_resume_noirq.
So, the regulator is on and has a balanced usage count after resume.
Best Regards
Richard Zhu

> Regards,
> Lucas
> 
> >  	imx6_pcie_init_phy(imx6_pcie);
> >  	imx6_pcie_deassert_core_reset(imx6_pcie);
> >  	dw_pcie_setup_rc(pp);
> > @@ -1259,6 +1252,8 @@ static void imx6_pcie_shutdown(struct
> > platform_device *pdev)
> >
> >  	/* bring down link, so bootloader gets clean state in case of reboot */
> >  	imx6_pcie_assert_core_reset(imx6_pcie);
> > +	if (imx6_pcie->vpcie)
> > +		regulator_disable(imx6_pcie->vpcie);
> >  }
> >
> >  static const struct imx6_pcie_drvdata drvdata[] = {
>
Hongxing Zhu June 9, 2022, 6:19 a.m. UTC | #4
> -----Original Message-----
> From: Bjorn Helgaas <helgaas@kernel.org>
> Sent: 2022年6月9日 2:55
> To: Hongxing Zhu <hongxing.zhu@nxp.com>
> Cc: l.stach@pengutronix.de; bhelgaas@google.com; robh+dt@kernel.org;
> broonie@kernel.org; lorenzo.pieralisi@arm.com; jingoohan1@gmail.com;
> festevam@gmail.com; francesco.dolcini@toradex.com;
> linux-pci@vger.kernel.org; linux-arm-kernel@lists.infradead.org;
> linux-kernel@vger.kernel.org; kernel@pengutronix.de; dl-linux-imx
> <linux-imx@nxp.com>
> Subject: Re: [PATCH v9 5/8] PCI: imx6: Refine the regulator usage
> 
> On Fri, May 06, 2022 at 09:47:06AM +0800, Richard Zhu wrote:
> > The driver should undo any enables it did itself. The regulator
> > disable shouldn't be basing decisions on regulator_is_enabled().
> >
> > To keep the balance of the regulator usage counter, disable the
> > regulator just behind of imx6_pcie_assert_core_reset() in resume and
> shutdown.
> 
> In subject, "Refine" doesn't tell me anything about what's happening here.
Thanks for your comments.
How about the following one?
PCI: imx6: Do regulator disable without the regulator_is_enabled check

Best Regards
Richard Zhu
Lucas Stach June 9, 2022, 7:47 a.m. UTC | #5
Am Donnerstag, dem 09.06.2022 um 06:17 +0000 schrieb Hongxing Zhu:
> > -----Original Message-----
> > From: Lucas Stach <l.stach@pengutronix.de>
> > Sent: 2022年6月8日 15:27
> > To: Hongxing Zhu <hongxing.zhu@nxp.com>; bhelgaas@google.com;
> > robh+dt@kernel.org; broonie@kernel.org; lorenzo.pieralisi@arm.com;
> > jingoohan1@gmail.com; festevam@gmail.com;
> > francesco.dolcini@toradex.com
> > Cc: linux-pci@vger.kernel.org;
> > linux-arm-kernel@lists.infradead.org;
> > linux-kernel@vger.kernel.org; kernel@pengutronix.de; dl-linux-imx
> > <linux-imx@nxp.com>
> > Subject: Re: [PATCH v9 5/8] PCI: imx6: Refine the regulator usage
> > 
> > Am Freitag, dem 06.05.2022 um 09:47 +0800 schrieb Richard Zhu:
> > > The driver should undo any enables it did itself. The regulator
> > > disable shouldn't be basing decisions on regulator_is_enabled().
> > > 
> > > To keep the balance of the regulator usage counter, disable the
> > > regulator just behind of imx6_pcie_assert_core_reset() in resume
> > > and
> > shutdown.
> > > 
> > > Signed-off-by: Richard Zhu <hongxing.zhu@nxp.com>
> > > ---
> > >  drivers/pci/controller/dwc/pci-imx6.c | 19 +++++++------------
> > >  1 file changed, 7 insertions(+), 12 deletions(-)
> > > 
> > > diff --git a/drivers/pci/controller/dwc/pci-imx6.c
> > > b/drivers/pci/controller/dwc/pci-imx6.c
> > > index 7005a7910003..3ce3993d5797 100644
> > > --- a/drivers/pci/controller/dwc/pci-imx6.c
> > > +++ b/drivers/pci/controller/dwc/pci-imx6.c
> > > @@ -369,8 +369,6 @@ static int imx6_pcie_attach_pd(struct device
> > > *dev)
> > > 
> > >  static void imx6_pcie_assert_core_reset(struct imx6_pcie
> > > *imx6_pcie)
> > > {
> > > -	struct device *dev = imx6_pcie->pci->dev;
> > > -
> > >  	switch (imx6_pcie->drvdata->variant) {
> > >  	case IMX7D:
> > >  	case IMX8MQ:
> > > @@ -400,14 +398,6 @@ static void
> > > imx6_pcie_assert_core_reset(struct
> > imx6_pcie *imx6_pcie)
> > >  				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 0
> > > << 16);
> > >  		break;
> > >  	}
> > > -
> > > -	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie-
> > > >vpcie) > 0) {
> > > -		int ret = regulator_disable(imx6_pcie->vpcie);
> > > -
> > > -		if (ret)
> > > -			dev_err(dev, "failed to disable vpcie
> > > regulator: %d\n",
> > > -				ret);
> > > -	}
> > >  }
> > > 
> > >  static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie
> > > *imx6_pcie) @@ -580,7 +570,7 @@ static int
> > imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
> > >  	struct device *dev = pci->dev;
> > >  	int ret, err;
> > > 
> > > -	if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie-
> > > >vpcie)) {
> > > +	if (imx6_pcie->vpcie) {
> > >  		ret = regulator_enable(imx6_pcie->vpcie);
> > >  		if (ret) {
> > >  			dev_err(dev, "failed to enable vpcie
> > > regulator: %d\n", @@
> > -653,7
> > > +643,7 @@ static int imx6_pcie_deassert_core_reset(struct
> > > imx6_pcie
> > *imx6_pcie)
> > >  	return 0;
> > > 
> > >  err_clks:
> > > -	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie-
> > > >vpcie) > 0) {
> > > +	if (imx6_pcie->vpcie) {
> > >  		ret = regulator_disable(imx6_pcie->vpcie);
> > >  		if (ret)
> > >  			dev_err(dev, "failed to disable vpcie
> > > regulator: %d\n", @@
> > -1026,6
> > > +1016,9 @@ static int imx6_pcie_resume_noirq(struct device *dev)
> > >  		return 0;
> > > 
> > >  	imx6_pcie_assert_core_reset(imx6_pcie);
> > > +	if (imx6_pcie->vpcie)
> > > +		regulator_disable(imx6_pcie->vpcie);
> > > +
> > This one looks misplaced. Surely you want the regulator to be on
> > when
> > resuming the PCIe subsystem. Isn't this just papering over a wrong
> > usage count
> > here, because there is no regulator_disable in
> > imx6_pcie_suspend_noirq,
> > where I would expect this to happen?
> > 
> Hi Lucas:
> Thanks for your comments.
> There was one regulator_disable() operation at the end of the
>  imx6_pcie_assert_core_reset() function before.
> When create the 5/8 patch, I follow the same behavior to disable the
> regulator just behind the imx6_pcie_assert_core_reset() function.
> 
> Yes, it is. Imx6_pcie_suspend_noirq doesn't have the
> regulator_disable.
> The regulaor_enable is contained in imx6_pcie_deassert_core_reset().
> Both of the regulator_disable and regulator_enabe are invoked once in
>  imx6_pcie_resume_noirq.
> So, the regulator is on and has a balanced usage count after resume.
> 

Yea, my argument is that when we are moving around the regulator
handling anyways, we should move the regulator_disable into the suspend
function. It's the right thing to do: we don't want the bus to be
powered when the system is in suspend and while the use-count is
correct, it's confusing to read the resume function otherwise.

Regards,
Lucas
Hongxing Zhu June 9, 2022, 7:54 a.m. UTC | #6
> -----Original Message-----
> From: Lucas Stach <l.stach@pengutronix.de>
> Sent: 2022年6月9日 15:47
> To: Hongxing Zhu <hongxing.zhu@nxp.com>; bhelgaas@google.com;
> robh+dt@kernel.org; broonie@kernel.org; lorenzo.pieralisi@arm.com;
> jingoohan1@gmail.com; festevam@gmail.com;
> francesco.dolcini@toradex.com
> Cc: linux-pci@vger.kernel.org; linux-arm-kernel@lists.infradead.org;
> linux-kernel@vger.kernel.org; kernel@pengutronix.de; dl-linux-imx
> <linux-imx@nxp.com>
> Subject: Re: [PATCH v9 5/8] PCI: imx6: Refine the regulator usage
> 
> Am Donnerstag, dem 09.06.2022 um 06:17 +0000 schrieb Hongxing Zhu:
> > > -----Original Message-----
> > > From: Lucas Stach <l.stach@pengutronix.de>
> > > Sent: 2022年6月8日 15:27
> > > To: Hongxing Zhu <hongxing.zhu@nxp.com>; bhelgaas@google.com;
> > > robh+dt@kernel.org; broonie@kernel.org; lorenzo.pieralisi@arm.com;
> > > jingoohan1@gmail.com; festevam@gmail.com;
> > > francesco.dolcini@toradex.com
> > > Cc: linux-pci@vger.kernel.org;
> > > linux-arm-kernel@lists.infradead.org;
> > > linux-kernel@vger.kernel.org; kernel@pengutronix.de; dl-linux-imx
> > > <linux-imx@nxp.com>
> > > Subject: Re: [PATCH v9 5/8] PCI: imx6: Refine the regulator usage
> > >
> > > Am Freitag, dem 06.05.2022 um 09:47 +0800 schrieb Richard Zhu:
> > > > The driver should undo any enables it did itself. The regulator
> > > > disable shouldn't be basing decisions on regulator_is_enabled().
> > > >
> > > > To keep the balance of the regulator usage counter, disable the
> > > > regulator just behind of imx6_pcie_assert_core_reset() in resume
> > > > and
> > > shutdown.
> > > >
> > > > Signed-off-by: Richard Zhu <hongxing.zhu@nxp.com>
> > > > ---
> > > >  drivers/pci/controller/dwc/pci-imx6.c | 19 +++++++------------
> > > >  1 file changed, 7 insertions(+), 12 deletions(-)
> > > >
> > > > diff --git a/drivers/pci/controller/dwc/pci-imx6.c
> > > > b/drivers/pci/controller/dwc/pci-imx6.c
> > > > index 7005a7910003..3ce3993d5797 100644
> > > > --- a/drivers/pci/controller/dwc/pci-imx6.c
> > > > +++ b/drivers/pci/controller/dwc/pci-imx6.c
> > > > @@ -369,8 +369,6 @@ static int imx6_pcie_attach_pd(struct device
> > > > *dev)
> > > >
> > > >  static void imx6_pcie_assert_core_reset(struct imx6_pcie
> > > > *imx6_pcie)
> > > > {
> > > > -	struct device *dev = imx6_pcie->pci->dev;
> > > > -
> > > >  	switch (imx6_pcie->drvdata->variant) {
> > > >  	case IMX7D:
> > > >  	case IMX8MQ:
> > > > @@ -400,14 +398,6 @@ static void
> > > > imx6_pcie_assert_core_reset(struct
> > > imx6_pcie *imx6_pcie)
> > > >  				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
> > > >  		break;
> > > >  	}
> > > > -
> > > > -	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie-
> > > > >vpcie) > 0) {
> > > > -		int ret = regulator_disable(imx6_pcie->vpcie);
> > > > -
> > > > -		if (ret)
> > > > -			dev_err(dev, "failed to disable vpcie
> > > > regulator: %d\n",
> > > > -				ret);
> > > > -	}
> > > >  }
> > > >
> > > >  static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie
> > > > *imx6_pcie) @@ -580,7 +570,7 @@ static int
> > > imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
> > > >  	struct device *dev = pci->dev;
> > > >  	int ret, err;
> > > >
> > > > -	if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie-
> > > > >vpcie)) {
> > > > +	if (imx6_pcie->vpcie) {
> > > >  		ret = regulator_enable(imx6_pcie->vpcie);
> > > >  		if (ret) {
> > > >  			dev_err(dev, "failed to enable vpcie
> > > > regulator: %d\n", @@
> > > -653,7
> > > > +643,7 @@ static int imx6_pcie_deassert_core_reset(struct
> > > > imx6_pcie
> > > *imx6_pcie)
> > > >  	return 0;
> > > >
> > > >  err_clks:
> > > > -	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie-
> > > > >vpcie) > 0) {
> > > > +	if (imx6_pcie->vpcie) {
> > > >  		ret = regulator_disable(imx6_pcie->vpcie);
> > > >  		if (ret)
> > > >  			dev_err(dev, "failed to disable vpcie
> > > > regulator: %d\n", @@
> > > -1026,6
> > > > +1016,9 @@ static int imx6_pcie_resume_noirq(struct device *dev)
> > > >  		return 0;
> > > >
> > > >  	imx6_pcie_assert_core_reset(imx6_pcie);
> > > > +	if (imx6_pcie->vpcie)
> > > > +		regulator_disable(imx6_pcie->vpcie);
> > > > +
> > > This one looks misplaced. Surely you want the regulator to be on
> > > when resuming the PCIe subsystem. Isn't this just papering over a
> > > wrong usage count here, because there is no regulator_disable in
> > > imx6_pcie_suspend_noirq, where I would expect this to happen?
> > >
> > Hi Lucas:
> > Thanks for your comments.
> > There was one regulator_disable() operation at the end of the
> >  imx6_pcie_assert_core_reset() function before.
> > When create the 5/8 patch, I follow the same behavior to disable the
> > regulator just behind the imx6_pcie_assert_core_reset() function.
> >
> > Yes, it is. Imx6_pcie_suspend_noirq doesn't have the
> > regulator_disable.
> > The regulaor_enable is contained in imx6_pcie_deassert_core_reset().
> > Both of the regulator_disable and regulator_enabe are invoked once in
> >  imx6_pcie_resume_noirq.
> > So, the regulator is on and has a balanced usage count after resume.
> >
> 
> Yea, my argument is that when we are moving around the regulator handling
> anyways, we should move the regulator_disable into the suspend function. It's
> the right thing to do: we don't want the bus to be powered when the system is
> in suspend and while the use-count is correct, it's confusing to read the resume
> function otherwise.
> 
Thanks for your quick reply.
Understand. I would move the regulator_disable at the end of the suspend
 function if you're fine with it.

Best Regards
Richard Zhu

> Regards,
> Lucas
Bjorn Helgaas June 9, 2022, 5:20 p.m. UTC | #7
On Thu, Jun 09, 2022 at 06:19:47AM +0000, Hongxing Zhu wrote:
> > -----Original Message-----
> > From: Bjorn Helgaas <helgaas@kernel.org>
> > Sent: 2022年6月9日 2:55
> > To: Hongxing Zhu <hongxing.zhu@nxp.com>
> > Cc: l.stach@pengutronix.de; bhelgaas@google.com; robh+dt@kernel.org;
> > broonie@kernel.org; lorenzo.pieralisi@arm.com; jingoohan1@gmail.com;
> > festevam@gmail.com; francesco.dolcini@toradex.com;
> > linux-pci@vger.kernel.org; linux-arm-kernel@lists.infradead.org;
> > linux-kernel@vger.kernel.org; kernel@pengutronix.de; dl-linux-imx
> > <linux-imx@nxp.com>
> > Subject: Re: [PATCH v9 5/8] PCI: imx6: Refine the regulator usage
> > 
> > On Fri, May 06, 2022 at 09:47:06AM +0800, Richard Zhu wrote:
> > > The driver should undo any enables it did itself. The regulator
> > > disable shouldn't be basing decisions on regulator_is_enabled().

The driver should disable things if an error occurs after it has
enabled something, or if it enabled something during probe and we're
now detaching the driver.  That doesn't look like the case here.

> > > To keep the balance of the regulator usage counter, disable the
> > > regulator just behind of imx6_pcie_assert_core_reset() in resume and
> > > shutdown.
> > 
> > In subject, "Refine" doesn't tell me anything about what's happening here.
>
> Thanks for your comments.
> How about the following one?
> PCI: imx6: Do regulator disable without the regulator_is_enabled check

That's too low-level, like describing the C code line by line.
I'm hoping for something about the purpose for the patch so
"git log --oneline" can tell a coherent story.

Apparently this is about disabling the power regulator when the slot
isn't being used, so maybe it could say something about that.

  $ git grep -Ep "regulator_(en|dis)able" drivers/pci/controller/

shows that in other drivers, this being done in
probe/remove/suspend/resume-type paths.  imx6 should be similar.

Bjorn
Hongxing Zhu June 10, 2022, 7:09 a.m. UTC | #8
> -----Original Message-----
> From: Bjorn Helgaas <helgaas@kernel.org>
> Sent: 2022年6月10日 1:20
> To: Hongxing Zhu <hongxing.zhu@nxp.com>
> Cc: l.stach@pengutronix.de; bhelgaas@google.com; robh+dt@kernel.org;
> broonie@kernel.org; lorenzo.pieralisi@arm.com; jingoohan1@gmail.com;
> festevam@gmail.com; francesco.dolcini@toradex.com;
> linux-pci@vger.kernel.org; linux-arm-kernel@lists.infradead.org;
> linux-kernel@vger.kernel.org; kernel@pengutronix.de; dl-linux-imx
> <linux-imx@nxp.com>
> Subject: Re: [PATCH v9 5/8] PCI: imx6: Refine the regulator usage
> 
> On Thu, Jun 09, 2022 at 06:19:47AM +0000, Hongxing Zhu wrote:
> > > -----Original Message-----
> > > From: Bjorn Helgaas <helgaas@kernel.org>
> > > Sent: 2022年6月9日 2:55
> > > To: Hongxing Zhu <hongxing.zhu@nxp.com>
> > > Cc: l.stach@pengutronix.de; bhelgaas@google.com; robh+dt@kernel.org;
> > > broonie@kernel.org; lorenzo.pieralisi@arm.com; jingoohan1@gmail.com;
> > > festevam@gmail.com; francesco.dolcini@toradex.com;
> > > linux-pci@vger.kernel.org; linux-arm-kernel@lists.infradead.org;
> > > linux-kernel@vger.kernel.org; kernel@pengutronix.de; dl-linux-imx
> > > <linux-imx@nxp.com>
> > > Subject: Re: [PATCH v9 5/8] PCI: imx6: Refine the regulator usage
> > >
> > > On Fri, May 06, 2022 at 09:47:06AM +0800, Richard Zhu wrote:
> > > > The driver should undo any enables it did itself. The regulator
> > > > disable shouldn't be basing decisions on regulator_is_enabled().
> 
> The driver should disable things if an error occurs after it has enabled
> something, or if it enabled something during probe and we're now detaching
> the driver.  That doesn't look like the case here.
> 
> > > > To keep the balance of the regulator usage counter, disable the
> > > > regulator just behind of imx6_pcie_assert_core_reset() in resume
> > > > and shutdown.
> > >
> > > In subject, "Refine" doesn't tell me anything about what's happening here.
> >
> > Thanks for your comments.
> > How about the following one?
> > PCI: imx6: Do regulator disable without the regulator_is_enabled check
> 
> That's too low-level, like describing the C code line by line.
> I'm hoping for something about the purpose for the patch so "git log --oneline"
> can tell a coherent story.
> 
> Apparently this is about disabling the power regulator when the slot isn't being
> used, so maybe it could say something about that.
> 
>   $ git grep -Ep "regulator_(en|dis)able" drivers/pci/controller/
> 
> shows that in other drivers, this being done in
> probe/remove/suspend/resume-type paths.  imx6 should be similar.
> 
Got that, thanks a lot.

Best Regards
Richard Zhu

> Bjorn
diff mbox series

Patch

diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c
index 7005a7910003..3ce3993d5797 100644
--- a/drivers/pci/controller/dwc/pci-imx6.c
+++ b/drivers/pci/controller/dwc/pci-imx6.c
@@ -369,8 +369,6 @@  static int imx6_pcie_attach_pd(struct device *dev)
 
 static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
 {
-	struct device *dev = imx6_pcie->pci->dev;
-
 	switch (imx6_pcie->drvdata->variant) {
 	case IMX7D:
 	case IMX8MQ:
@@ -400,14 +398,6 @@  static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
 				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
 		break;
 	}
-
-	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
-		int ret = regulator_disable(imx6_pcie->vpcie);
-
-		if (ret)
-			dev_err(dev, "failed to disable vpcie regulator: %d\n",
-				ret);
-	}
 }
 
 static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
@@ -580,7 +570,7 @@  static int imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
 	struct device *dev = pci->dev;
 	int ret, err;
 
-	if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
+	if (imx6_pcie->vpcie) {
 		ret = regulator_enable(imx6_pcie->vpcie);
 		if (ret) {
 			dev_err(dev, "failed to enable vpcie regulator: %d\n",
@@ -653,7 +643,7 @@  static int imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
 	return 0;
 
 err_clks:
-	if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
+	if (imx6_pcie->vpcie) {
 		ret = regulator_disable(imx6_pcie->vpcie);
 		if (ret)
 			dev_err(dev, "failed to disable vpcie regulator: %d\n",
@@ -1026,6 +1016,9 @@  static int imx6_pcie_resume_noirq(struct device *dev)
 		return 0;
 
 	imx6_pcie_assert_core_reset(imx6_pcie);
+	if (imx6_pcie->vpcie)
+		regulator_disable(imx6_pcie->vpcie);
+
 	imx6_pcie_init_phy(imx6_pcie);
 	imx6_pcie_deassert_core_reset(imx6_pcie);
 	dw_pcie_setup_rc(pp);
@@ -1259,6 +1252,8 @@  static void imx6_pcie_shutdown(struct platform_device *pdev)
 
 	/* bring down link, so bootloader gets clean state in case of reboot */
 	imx6_pcie_assert_core_reset(imx6_pcie);
+	if (imx6_pcie->vpcie)
+		regulator_disable(imx6_pcie->vpcie);
 }
 
 static const struct imx6_pcie_drvdata drvdata[] = {