All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V2] gpio: mxc: add power management support
@ 2018-07-16  6:06 Anson Huang
  2018-07-17 13:52 ` Fabio Estevam
  0 siblings, 1 reply; 3+ messages in thread
From: Anson Huang @ 2018-07-16  6:06 UTC (permalink / raw)
  To: linus.walleij, linux-gpio, linux-kernel; +Cc: Linux-imx

GPIO registers could lose context on i.MX7D,
when enter LPSR mode, the whole SoC will be
powered off except LPSR domain, GPIO banks
will lose context in this case, need to restore
the context after resume from LPSR mode.

This patch adds GPIO save/restore for those necessary
registers, and put the save/restore operations in noirq
suspend/resume phase, since GPIO is fundamental module
which could be used by other peripherals' resume phase.

Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
---
changes since V1:
	Add condition check to make it only work for i.MX7D which GPIO might lost power.
 drivers/gpio/gpio-mxc.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/drivers/gpio/gpio-mxc.c b/drivers/gpio/gpio-mxc.c
index 2f28299..17f2fe2 100644
--- a/drivers/gpio/gpio-mxc.c
+++ b/drivers/gpio/gpio-mxc.c
@@ -45,6 +45,15 @@ struct mxc_gpio_hwdata {
 	unsigned fall_edge;
 };
 
+struct mxc_gpio_reg_saved {
+	u32 icr1;
+	u32 icr2;
+	u32 imr;
+	u32 gdir;
+	u32 edge_sel;
+	u32 dr;
+};
+
 struct mxc_gpio_port {
 	struct list_head node;
 	void __iomem *base;
@@ -55,6 +64,8 @@ struct mxc_gpio_port {
 	struct gpio_chip gc;
 	struct device *dev;
 	u32 both_edges;
+	struct mxc_gpio_reg_saved gpio_saved_reg;
+	bool support_power_off;
 };
 
 static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
@@ -434,6 +445,9 @@ static int mxc_gpio_probe(struct platform_device *pdev)
 		return err;
 	}
 
+	if (of_machine_is_compatible("fsl,imx7d"))
+		port->support_power_off = true;
+
 	/* disable the interrupt and clear the status */
 	writel(0, port->base + GPIO_IMR);
 	writel(~0, port->base + GPIO_ISR);
@@ -497,6 +511,8 @@ static int mxc_gpio_probe(struct platform_device *pdev)
 
 	list_add_tail(&port->node, &mxc_gpio_ports);
 
+	platform_set_drvdata(pdev, port);
+
 	return 0;
 
 out_irqdomain_remove:
@@ -507,11 +523,67 @@ static int mxc_gpio_probe(struct platform_device *pdev)
 	return err;
 }
 
+static void mxc_gpio_save_regs(struct mxc_gpio_port *port)
+{
+	if (!port->support_power_off)
+		return;
+
+	port->gpio_saved_reg.icr1 = readl(port->base + GPIO_ICR1);
+	port->gpio_saved_reg.icr2 = readl(port->base + GPIO_ICR2);
+	port->gpio_saved_reg.imr = readl(port->base + GPIO_IMR);
+	port->gpio_saved_reg.gdir = readl(port->base + GPIO_GDIR);
+	port->gpio_saved_reg.edge_sel = readl(port->base + GPIO_EDGE_SEL);
+	port->gpio_saved_reg.dr = readl(port->base + GPIO_DR);
+}
+
+static void mxc_gpio_restore_regs(struct mxc_gpio_port *port)
+{
+	if (!port->support_power_off)
+		return;
+
+	writel(port->gpio_saved_reg.icr1, port->base + GPIO_ICR1);
+	writel(port->gpio_saved_reg.icr2, port->base + GPIO_ICR2);
+	writel(port->gpio_saved_reg.imr, port->base + GPIO_IMR);
+	writel(port->gpio_saved_reg.gdir, port->base + GPIO_GDIR);
+	writel(port->gpio_saved_reg.edge_sel, port->base + GPIO_EDGE_SEL);
+	writel(port->gpio_saved_reg.dr, port->base + GPIO_DR);
+}
+
+static int __maybe_unused mxc_gpio_noirq_suspend(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct mxc_gpio_port *port = platform_get_drvdata(pdev);
+
+	mxc_gpio_save_regs(port);
+	clk_disable_unprepare(port->clk);
+
+	return 0;
+}
+
+static int __maybe_unused mxc_gpio_noirq_resume(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct mxc_gpio_port *port = platform_get_drvdata(pdev);
+	int ret;
+
+	ret = clk_prepare_enable(port->clk);
+	if (ret)
+		return ret;
+	mxc_gpio_restore_regs(port);
+
+	return 0;
+}
+
+static const struct dev_pm_ops mxc_gpio_dev_pm_ops = {
+	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mxc_gpio_noirq_suspend, mxc_gpio_noirq_resume)
+};
+
 static struct platform_driver mxc_gpio_driver = {
 	.driver		= {
 		.name	= "gpio-mxc",
 		.of_match_table = mxc_gpio_dt_ids,
 		.suppress_bind_attrs = true,
+		.pm = &mxc_gpio_dev_pm_ops,
 	},
 	.probe		= mxc_gpio_probe,
 	.id_table	= mxc_gpio_devtype,
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH V2] gpio: mxc: add power management support
  2018-07-16  6:06 [PATCH V2] gpio: mxc: add power management support Anson Huang
@ 2018-07-17 13:52 ` Fabio Estevam
  2018-07-18  0:54   ` Anson Huang
  0 siblings, 1 reply; 3+ messages in thread
From: Fabio Estevam @ 2018-07-17 13:52 UTC (permalink / raw)
  To: Anson Huang
  Cc: Linus Walleij, open list:GPIO SUBSYSTEM, linux-kernel, NXP Linux Team

Hi Anson,

[Next time please put the i.mx maintainers on Cc]

On Mon, Jul 16, 2018 at 3:06 AM, Anson Huang <Anson.Huang@nxp.com> wrote:
> GPIO registers could lose context on i.MX7D,
> when enter LPSR mode, the whole SoC will be
> powered off except LPSR domain, GPIO banks
> will lose context in this case, need to restore
> the context after resume from LPSR mode.
>
> This patch adds GPIO save/restore for those necessary
> registers, and put the save/restore operations in noirq
> suspend/resume phase, since GPIO is fundamental module
> which could be used by other peripherals' resume phase.
>
> Signed-off-by: Anson Huang <Anson.Huang@nxp.com>

>  static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
> @@ -434,6 +445,9 @@ static int mxc_gpio_probe(struct platform_device *pdev)
>                 return err;
>         }
>
> +       if (of_machine_is_compatible("fsl,imx7d"))
> +               port->support_power_off = true;

This does not scale well as we need to support mx8 soon.

Like I replied in my last message the best solution here is to add a
"fsl,imx7d-gpio" compatible entry in this driver.

Then you can set a flag in the "fsl,imx7d-gpio" case to handle the
'power_off' feature.

With this method there will be no need to touch this driver again for
adding mx8 support.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH V2] gpio: mxc: add power management support
  2018-07-17 13:52 ` Fabio Estevam
@ 2018-07-18  0:54   ` Anson Huang
  0 siblings, 0 replies; 3+ messages in thread
From: Anson Huang @ 2018-07-18  0:54 UTC (permalink / raw)
  To: Fabio Estevam
  Cc: Linus Walleij, open list:GPIO SUBSYSTEM, linux-kernel, dl-linux-imx

Hi, Fabio

Anson Huang
Best Regards!


> -----Original Message-----
> From: Fabio Estevam [mailto:festevam@gmail.com]
> Sent: Tuesday, July 17, 2018 9:52 PM
> To: Anson Huang <anson.huang@nxp.com>
> Cc: Linus Walleij <linus.walleij@linaro.org>; open list:GPIO SUBSYSTEM
> <linux-gpio@vger.kernel.org>; linux-kernel <linux-kernel@vger.kernel.org>;
> dl-linux-imx <linux-imx@nxp.com>
> Subject: Re: [PATCH V2] gpio: mxc: add power management support
> 
> Hi Anson,
> 
> [Next time please put the i.mx maintainers on Cc]
> 
> On Mon, Jul 16, 2018 at 3:06 AM, Anson Huang <Anson.Huang@nxp.com>
> wrote:
> > GPIO registers could lose context on i.MX7D, when enter LPSR mode, the
> > whole SoC will be powered off except LPSR domain, GPIO banks will lose
> > context in this case, need to restore the context after resume from
> > LPSR mode.
> >
> > This patch adds GPIO save/restore for those necessary registers, and
> > put the save/restore operations in noirq suspend/resume phase, since
> > GPIO is fundamental module which could be used by other peripherals'
> > resume phase.
> >
> > Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
> 
> >  static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = { @@ -434,6
> > +445,9 @@ static int mxc_gpio_probe(struct platform_device *pdev)
> >                 return err;
> >         }
> >
> > +       if (of_machine_is_compatible("fsl,imx7d"))
> > +               port->support_power_off = true;
> 
> This does not scale well as we need to support mx8 soon.
> 
> Like I replied in my last message the best solution here is to add a
> "fsl,imx7d-gpio" compatible entry in this driver.
> 
> Then you can set a flag in the "fsl,imx7d-gpio" case to handle the 'power_off'
> feature.
> 
> With this method there will be no need to touch this driver again for adding
> mx8 support.

Thanks for detail suggestion, I will sent a V3 patch accordingly.

Anson.
 




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-07-18  0:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-16  6:06 [PATCH V2] gpio: mxc: add power management support Anson Huang
2018-07-17 13:52 ` Fabio Estevam
2018-07-18  0:54   ` Anson Huang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.