Commit ca109896 authored by Aisheng Dong's avatar Aisheng Dong Committed by Marc Kleine-Budde

can: flexcan: implement can Runtime PM

Flexcan will be disabled during suspend if no wakeup function required and
enabled after resume accordingly. During this period, we could explicitly
disable clocks.
Since PM is optional, the clock is enabled at probe to guarante the
clock is running when PM is not enabled in the kernel.

Implement Runtime PM which will:
1) Without CONFIG_PM, clock is running whether Flexcan up or down.
2) With CONFIG_PM, clock enabled while Flexcan up and disabled when
   Flexcan down.
3) Disable clock when do system suspend and enable clock while system
   resume.
4) Make Power Domain framework be able to shutdown the corresponding
   power domain of this device.
Signed-off-by: default avatarAisheng Dong <aisheng.dong@nxp.com>
Signed-off-by: default avatarJoakim Zhang <qiangqing.zhang@nxp.com>
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parent 26bca9fe
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <linux/of.h> #include <linux/of.h>
#include <linux/of_device.h> #include <linux/of_device.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h> #include <linux/regulator/consumer.h>
#include <linux/regmap.h> #include <linux/regmap.h>
...@@ -266,6 +267,7 @@ struct flexcan_stop_mode { ...@@ -266,6 +267,7 @@ struct flexcan_stop_mode {
struct flexcan_priv { struct flexcan_priv {
struct can_priv can; struct can_priv can;
struct can_rx_offload offload; struct can_rx_offload offload;
struct device *dev;
struct flexcan_regs __iomem *regs; struct flexcan_regs __iomem *regs;
struct flexcan_mb __iomem *tx_mb; struct flexcan_mb __iomem *tx_mb;
...@@ -444,6 +446,27 @@ static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv) ...@@ -444,6 +446,27 @@ static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv)
priv->write(reg_ctrl, &regs->ctrl); priv->write(reg_ctrl, &regs->ctrl);
} }
static int flexcan_clks_enable(const struct flexcan_priv *priv)
{
int err;
err = clk_prepare_enable(priv->clk_ipg);
if (err)
return err;
err = clk_prepare_enable(priv->clk_per);
if (err)
clk_disable_unprepare(priv->clk_ipg);
return err;
}
static void flexcan_clks_disable(const struct flexcan_priv *priv)
{
clk_disable_unprepare(priv->clk_per);
clk_disable_unprepare(priv->clk_ipg);
}
static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv) static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
{ {
if (!priv->reg_xceiver) if (!priv->reg_xceiver)
...@@ -570,19 +593,13 @@ static int flexcan_get_berr_counter(const struct net_device *dev, ...@@ -570,19 +593,13 @@ static int flexcan_get_berr_counter(const struct net_device *dev,
const struct flexcan_priv *priv = netdev_priv(dev); const struct flexcan_priv *priv = netdev_priv(dev);
int err; int err;
err = clk_prepare_enable(priv->clk_ipg); err = pm_runtime_get_sync(priv->dev);
if (err) if (err < 0)
return err; return err;
err = clk_prepare_enable(priv->clk_per);
if (err)
goto out_disable_ipg;
err = __flexcan_get_berr_counter(dev, bec); err = __flexcan_get_berr_counter(dev, bec);
clk_disable_unprepare(priv->clk_per); pm_runtime_put(priv->dev);
out_disable_ipg:
clk_disable_unprepare(priv->clk_ipg);
return err; return err;
} }
...@@ -1215,17 +1232,13 @@ static int flexcan_open(struct net_device *dev) ...@@ -1215,17 +1232,13 @@ static int flexcan_open(struct net_device *dev)
struct flexcan_priv *priv = netdev_priv(dev); struct flexcan_priv *priv = netdev_priv(dev);
int err; int err;
err = clk_prepare_enable(priv->clk_ipg); err = pm_runtime_get_sync(priv->dev);
if (err) if (err < 0)
return err; return err;
err = clk_prepare_enable(priv->clk_per);
if (err)
goto out_disable_ipg;
err = open_candev(dev); err = open_candev(dev);
if (err) if (err)
goto out_disable_per; goto out_runtime_put;
err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev); err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
if (err) if (err)
...@@ -1288,10 +1301,8 @@ static int flexcan_open(struct net_device *dev) ...@@ -1288,10 +1301,8 @@ static int flexcan_open(struct net_device *dev)
free_irq(dev->irq, dev); free_irq(dev->irq, dev);
out_close: out_close:
close_candev(dev); close_candev(dev);
out_disable_per: out_runtime_put:
clk_disable_unprepare(priv->clk_per); pm_runtime_put(priv->dev);
out_disable_ipg:
clk_disable_unprepare(priv->clk_ipg);
return err; return err;
} }
...@@ -1306,10 +1317,9 @@ static int flexcan_close(struct net_device *dev) ...@@ -1306,10 +1317,9 @@ static int flexcan_close(struct net_device *dev)
can_rx_offload_del(&priv->offload); can_rx_offload_del(&priv->offload);
free_irq(dev->irq, dev); free_irq(dev->irq, dev);
clk_disable_unprepare(priv->clk_per);
clk_disable_unprepare(priv->clk_ipg);
close_candev(dev); close_candev(dev);
pm_runtime_put(priv->dev);
can_led_event(dev, CAN_LED_EVENT_STOP); can_led_event(dev, CAN_LED_EVENT_STOP);
...@@ -1349,18 +1359,15 @@ static int register_flexcandev(struct net_device *dev) ...@@ -1349,18 +1359,15 @@ static int register_flexcandev(struct net_device *dev)
struct flexcan_regs __iomem *regs = priv->regs; struct flexcan_regs __iomem *regs = priv->regs;
u32 reg, err; u32 reg, err;
err = clk_prepare_enable(priv->clk_ipg); err = flexcan_clks_enable(priv);
if (err) if (err)
return err; return err;
err = clk_prepare_enable(priv->clk_per);
if (err)
goto out_disable_ipg;
/* select "bus clock", chip must be disabled */ /* select "bus clock", chip must be disabled */
err = flexcan_chip_disable(priv); err = flexcan_chip_disable(priv);
if (err) if (err)
goto out_disable_per; goto out_clks_disable;
reg = priv->read(&regs->ctrl); reg = priv->read(&regs->ctrl);
reg |= FLEXCAN_CTRL_CLK_SRC; reg |= FLEXCAN_CTRL_CLK_SRC;
priv->write(reg, &regs->ctrl); priv->write(reg, &regs->ctrl);
...@@ -1388,15 +1395,21 @@ static int register_flexcandev(struct net_device *dev) ...@@ -1388,15 +1395,21 @@ static int register_flexcandev(struct net_device *dev)
} }
err = register_candev(dev); err = register_candev(dev);
if (err)
goto out_chip_disable;
/* disable core and turn off clocks */ /* Disable core and let pm_runtime_put() disable the clocks.
out_chip_disable: * If CONFIG_PM is not enabled, the clocks will stay powered.
*/
flexcan_chip_disable(priv); flexcan_chip_disable(priv);
out_disable_per: pm_runtime_put(priv->dev);
clk_disable_unprepare(priv->clk_per);
out_disable_ipg:
clk_disable_unprepare(priv->clk_ipg);
return 0;
out_chip_disable:
flexcan_chip_disable(priv);
out_clks_disable:
flexcan_clks_disable(priv);
return err; return err;
} }
...@@ -1556,6 +1569,7 @@ static int flexcan_probe(struct platform_device *pdev) ...@@ -1556,6 +1569,7 @@ static int flexcan_probe(struct platform_device *pdev)
priv->write = flexcan_write_le; priv->write = flexcan_write_le;
} }
priv->dev = &pdev->dev;
priv->can.clock.freq = clock_freq; priv->can.clock.freq = clock_freq;
priv->can.bittiming_const = &flexcan_bittiming_const; priv->can.bittiming_const = &flexcan_bittiming_const;
priv->can.do_set_mode = flexcan_set_mode; priv->can.do_set_mode = flexcan_set_mode;
...@@ -1569,6 +1583,10 @@ static int flexcan_probe(struct platform_device *pdev) ...@@ -1569,6 +1583,10 @@ static int flexcan_probe(struct platform_device *pdev)
priv->devtype_data = devtype_data; priv->devtype_data = devtype_data;
priv->reg_xceiver = reg_xceiver; priv->reg_xceiver = reg_xceiver;
pm_runtime_get_noresume(&pdev->dev);
pm_runtime_set_active(&pdev->dev);
pm_runtime_enable(&pdev->dev);
err = register_flexcandev(dev); err = register_flexcandev(dev);
if (err) { if (err) {
dev_err(&pdev->dev, "registering netdev failed\n"); dev_err(&pdev->dev, "registering netdev failed\n");
...@@ -1595,6 +1613,7 @@ static int flexcan_remove(struct platform_device *pdev) ...@@ -1595,6 +1613,7 @@ static int flexcan_remove(struct platform_device *pdev)
struct net_device *dev = platform_get_drvdata(pdev); struct net_device *dev = platform_get_drvdata(pdev);
unregister_flexcandev(dev); unregister_flexcandev(dev);
pm_runtime_disable(&pdev->dev);
free_candev(dev); free_candev(dev);
return 0; return 0;
...@@ -1604,7 +1623,7 @@ static int __maybe_unused flexcan_suspend(struct device *device) ...@@ -1604,7 +1623,7 @@ static int __maybe_unused flexcan_suspend(struct device *device)
{ {
struct net_device *dev = dev_get_drvdata(device); struct net_device *dev = dev_get_drvdata(device);
struct flexcan_priv *priv = netdev_priv(dev); struct flexcan_priv *priv = netdev_priv(dev);
int err; int err = 0;
if (netif_running(dev)) { if (netif_running(dev)) {
/* if wakeup is enabled, enter stop mode /* if wakeup is enabled, enter stop mode
...@@ -1617,20 +1636,22 @@ static int __maybe_unused flexcan_suspend(struct device *device) ...@@ -1617,20 +1636,22 @@ static int __maybe_unused flexcan_suspend(struct device *device)
err = flexcan_chip_disable(priv); err = flexcan_chip_disable(priv);
if (err) if (err)
return err; return err;
err = pm_runtime_force_suspend(device);
} }
netif_stop_queue(dev); netif_stop_queue(dev);
netif_device_detach(dev); netif_device_detach(dev);
} }
priv->can.state = CAN_STATE_SLEEPING; priv->can.state = CAN_STATE_SLEEPING;
return 0; return err;
} }
static int __maybe_unused flexcan_resume(struct device *device) static int __maybe_unused flexcan_resume(struct device *device)
{ {
struct net_device *dev = dev_get_drvdata(device); struct net_device *dev = dev_get_drvdata(device);
struct flexcan_priv *priv = netdev_priv(dev); struct flexcan_priv *priv = netdev_priv(dev);
int err; int err = 0;
priv->can.state = CAN_STATE_ERROR_ACTIVE; priv->can.state = CAN_STATE_ERROR_ACTIVE;
if (netif_running(dev)) { if (netif_running(dev)) {
...@@ -1639,14 +1660,35 @@ static int __maybe_unused flexcan_resume(struct device *device) ...@@ -1639,14 +1660,35 @@ static int __maybe_unused flexcan_resume(struct device *device)
if (device_may_wakeup(device)) { if (device_may_wakeup(device)) {
disable_irq_wake(dev->irq); disable_irq_wake(dev->irq);
} else { } else {
err = flexcan_chip_enable(priv); err = pm_runtime_force_resume(device);
if (err) if (err)
return err; return err;
err = flexcan_chip_enable(priv);
} }
} }
return err;
}
static int __maybe_unused flexcan_runtime_suspend(struct device *device)
{
struct net_device *dev = dev_get_drvdata(device);
struct flexcan_priv *priv = netdev_priv(dev);
flexcan_clks_disable(priv);
return 0; return 0;
} }
static int __maybe_unused flexcan_runtime_resume(struct device *device)
{
struct net_device *dev = dev_get_drvdata(device);
struct flexcan_priv *priv = netdev_priv(dev);
return flexcan_clks_enable(priv);
}
static int __maybe_unused flexcan_noirq_suspend(struct device *device) static int __maybe_unused flexcan_noirq_suspend(struct device *device)
{ {
struct net_device *dev = dev_get_drvdata(device); struct net_device *dev = dev_get_drvdata(device);
...@@ -1673,6 +1715,7 @@ static int __maybe_unused flexcan_noirq_resume(struct device *device) ...@@ -1673,6 +1715,7 @@ static int __maybe_unused flexcan_noirq_resume(struct device *device)
static const struct dev_pm_ops flexcan_pm_ops = { static const struct dev_pm_ops flexcan_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume) SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume)
SET_RUNTIME_PM_OPS(flexcan_runtime_suspend, flexcan_runtime_resume, NULL)
SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume) SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume)
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment