RE: [PATCH] pci: pcie-rcar: add regulators support

From: Li, Meng
Date: Tue Oct 19 2021 - 07:00:09 EST




> -----Original Message-----
> From: Geert Uytterhoeven <geert@xxxxxxxxxxxxxx>
> Sent: Tuesday, October 19, 2021 6:20 PM
> To: Li, Meng <Meng.Li@xxxxxxxxxxxxx>
> Cc: Magnus Damm <magnus.damm@xxxxxxxxx>; Rob Herring
> <robh+dt@xxxxxxxxxx>; Marek Vasut <marek.vasut+renesas@xxxxxxxxx>;
> Yoshihiro Shimoda <yoshihiro.shimoda.uh@xxxxxxxxxxx>; Lorenzo Pieralisi
> <lorenzo.pieralisi@xxxxxxx>; Krzysztof Wilczyński <kw@xxxxxxxxx>; Bjorn
> Helgaas <bhelgaas@xxxxxxxxxx>; Liam Girdwood <lgirdwood@xxxxxxxxx>;
> Mark Brown <broonie@xxxxxxxxxx>; Linux-Renesas <linux-renesas-
> soc@xxxxxxxxxxxxxxx>; open list:OPEN FIRMWARE AND FLATTENED DEVICE
> TREE BINDINGS <devicetree@xxxxxxxxxxxxxxx>; Linux Kernel Mailing List
> <linux-kernel@xxxxxxxxxxxxxxx>; linux-pci <linux-pci@xxxxxxxxxxxxxxx>
> Subject: Re: [PATCH] pci: pcie-rcar: add regulators support
>
> [Please note: This e-mail is from an EXTERNAL e-mail address]
>
> Hi Meng,
>
> On Tue, Oct 19, 2021 at 11:59 AM Meng Li <Meng.Li@xxxxxxxxxxxxx> wrote:
> > From: Andrey Gusakov <andrey.gusakov@xxxxxxxxxxxxxxxxxx>
> >
> > Add PCIe regulators for KingFisher board.
> >
> > Signed-off-by: Meng Li <Meng.Li@xxxxxxxxxxxxx>
>
> Thanks for your patch!
>
> > arch/arm64/boot/dts/renesas/ulcb-kf.dtsi | 47 +++++++++++++++++
> > drivers/pci/controller/pcie-rcar-host.c | 64 ++++++++++++++++++++++++
>
> Please split patches touching both DT and driver sources.
>
> > --- a/arch/arm64/boot/dts/renesas/ulcb-kf.dtsi
> > +++ b/arch/arm64/boot/dts/renesas/ulcb-kf.dtsi
>
> > @@ -259,6 +303,9 @@
> >
> > &pciec1 {
> > status = "okay";
> > +
> > + pcie3v3-supply = <&mpcie_3v3>;
> > + pcie1v8-supply = <&mpcie_1v8>;
>
> This needs an update to the R-Car PCIe DT bindings first.
>
> > --- a/drivers/pci/controller/pcie-rcar-host.c
> > +++ b/drivers/pci/controller/pcie-rcar-host.c
>
> > @@ -893,6 +896,36 @@ static const struct of_device_id
> rcar_pcie_of_match[] = {
> > {},
> > };
> >
> > +static int rcar_pcie_set_vpcie(struct rcar_pcie_host *host) {
> > + struct device *dev = host->pcie.dev;
> > + int err;
> > +
> > + if (!IS_ERR(host->pcie3v3)) {
> > + err = regulator_enable(host->pcie3v3);
>
> This will crash if host->pcie3v3 is NULL (optional regulator not present).
> Probably you just want to check for non-NULL (see below).
>
> > + if (err) {
> > + dev_err(dev, "fail to enable vpcie3v3 regulator\n");
> > + goto err_out;
> > + }
> > + }
> > +
> > + if (!IS_ERR(host->pcie1v8)) {
> > + err = regulator_enable(host->pcie1v8);
>
> Likewise.
>
> > + if (err) {
> > + dev_err(dev, "fail to enable vpcie1v8 regulator\n");
> > + goto err_disable_3v3;
> > + }
> > + }
> > +
> > + return 0;
> > +
> > +err_disable_3v3:
> > + if (!IS_ERR(host->pcie3v3))
>
> Likewise.
>
> > + regulator_disable(host->pcie3v3);
> > +err_out:
> > + return err;
> > +}
> > +
> > static int rcar_pcie_probe(struct platform_device *pdev) {
> > struct device *dev = &pdev->dev; @@ -911,6 +944,31 @@ static
> > int rcar_pcie_probe(struct platform_device *pdev)
> > pcie->dev = dev;
> > platform_set_drvdata(pdev, host);
> >
> > + host->pcie3v3 = devm_regulator_get_optional(dev, "pcie3v3");
> > + if (IS_ERR(host->pcie3v3)) {
> > + if (PTR_ERR(host->pcie3v3) == -EPROBE_DEFER) {
> > + pci_free_host_bridge(bridge);
>
> Please drop this. As the bridge was allocated using
> devm_pci_alloc_host_bridge(), freeing it manually will cause a double free.
>
> > + return -EPROBE_DEFER;
> > + }
> > + dev_info(dev, "no pcie3v3 regulator found\n");
>
> devm_regulator_get_optional() returns NULL if the regulator was not found.
> Hence if IS_ERR() is true, this indicates a real error, which you should handle:
>
> if (IS_ERR(host->pcie3v3))
> return PTR_ERR(host->pcie3v3);
>
> > + }
> > +
> > + host->pcie1v8 = devm_regulator_get_optional(dev, "pcie1v8");
> > + if (IS_ERR(host->pcie1v8)) {
> > + if (PTR_ERR(host->pcie1v8) == -EPROBE_DEFER) {
> > + pci_free_host_bridge(bridge);
> > + return -EPROBE_DEFER;
> > + }
> > + dev_info(dev, "no pcie1v8 regulator found\n");
>
> Likewise.
>
> > + }
> > +
> > + err = rcar_pcie_set_vpcie(host);
> > + if (err) {
> > + dev_err(dev, "failed to set pcie regulators\n");
> > + pci_free_host_bridge(bridge);
>
> Please drop this to avoid double free.
>
> > + return err;
> > + }
> > +
> > pm_runtime_enable(pcie->dev);
> > err = pm_runtime_get_sync(pcie->dev);
> > if (err < 0) {
> > @@ -985,6 +1043,12 @@ static int rcar_pcie_probe(struct platform_device
> *pdev)
> > irq_dispose_mapping(host->msi.irq1);
> >
> > err_pm_put:
> > + if(!IS_ERR(host->pcie3v3))
>
> if (host->pcie3v3)
>
> > + if (regulator_is_enabled(host->pcie3v3))
>
> If you get here, the regulator should be enabled?
>
> > + regulator_disable(host->pcie3v3);
> > + if(!IS_ERR(host->pcie1v8))
>
> if (host->pcie1v8)
>
> > + if (regulator_is_enabled(host->pcie1v8))
> > + regulator_disable(host->pcie1v8);
>
> Please move this below the call to pm_runtime_disable(), to preserve
> symmetry.
>
> > pm_runtime_put(dev);
> > pm_runtime_disable(dev);
>
> Gr{oetje,eeting}s,
>

Thanks for your professional suggest.
I will improve patches and send v2 in later

Thanks,
Limeng

> Geert
>
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-
> m68k.org
>
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
> -- Linus Torvalds