Commit 525c9e5a authored by Clark Wang's avatar Clark Wang Committed by Mark Brown

spi: imx: enable runtime pm support

Enable runtime pm support for spi-imx driver.
Signed-off-by: default avatarClark Wang <xiaoning.wang@nxp.com>
Link: https://lore.kernel.org/r/20200727063354.17031-1-xiaoning.wang@nxp.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 30962fe3
...@@ -13,7 +13,9 @@ ...@@ -13,7 +13,9 @@
#include <linux/irq.h> #include <linux/irq.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/pinctrl/consumer.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/spi/spi.h> #include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h> #include <linux/spi/spi_bitbang.h>
...@@ -30,6 +32,8 @@ static bool use_dma = true; ...@@ -30,6 +32,8 @@ static bool use_dma = true;
module_param(use_dma, bool, 0644); module_param(use_dma, bool, 0644);
MODULE_PARM_DESC(use_dma, "Enable usage of DMA when available (default)"); MODULE_PARM_DESC(use_dma, "Enable usage of DMA when available (default)");
#define MXC_RPM_TIMEOUT 2000 /* 2000ms */
#define MXC_CSPIRXDATA 0x00 #define MXC_CSPIRXDATA 0x00
#define MXC_CSPITXDATA 0x04 #define MXC_CSPITXDATA 0x04
#define MXC_CSPICTRL 0x08 #define MXC_CSPICTRL 0x08
...@@ -1530,20 +1534,16 @@ spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg) ...@@ -1530,20 +1534,16 @@ spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
struct spi_imx_data *spi_imx = spi_master_get_devdata(master); struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
int ret; int ret;
ret = clk_enable(spi_imx->clk_per); ret = pm_runtime_get_sync(spi_imx->dev);
if (ret) if (ret < 0) {
return ret; dev_err(spi_imx->dev, "failed to enable clock\n");
ret = clk_enable(spi_imx->clk_ipg);
if (ret) {
clk_disable(spi_imx->clk_per);
return ret; return ret;
} }
ret = spi_imx->devtype_data->prepare_message(spi_imx, msg); ret = spi_imx->devtype_data->prepare_message(spi_imx, msg);
if (ret) { if (ret) {
clk_disable(spi_imx->clk_ipg); pm_runtime_mark_last_busy(spi_imx->dev);
clk_disable(spi_imx->clk_per); pm_runtime_put_autosuspend(spi_imx->dev);
} }
return ret; return ret;
...@@ -1554,8 +1554,8 @@ spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg) ...@@ -1554,8 +1554,8 @@ spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
{ {
struct spi_imx_data *spi_imx = spi_master_get_devdata(master); struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
clk_disable(spi_imx->clk_ipg); pm_runtime_mark_last_busy(spi_imx->dev);
clk_disable(spi_imx->clk_per); pm_runtime_put_autosuspend(spi_imx->dev);
return 0; return 0;
} }
...@@ -1674,13 +1674,15 @@ static int spi_imx_probe(struct platform_device *pdev) ...@@ -1674,13 +1674,15 @@ static int spi_imx_probe(struct platform_device *pdev)
goto out_master_put; goto out_master_put;
} }
ret = clk_prepare_enable(spi_imx->clk_per); pm_runtime_enable(spi_imx->dev);
if (ret) pm_runtime_set_autosuspend_delay(spi_imx->dev, MXC_RPM_TIMEOUT);
goto out_master_put; pm_runtime_use_autosuspend(spi_imx->dev);
ret = clk_prepare_enable(spi_imx->clk_ipg); ret = pm_runtime_get_sync(spi_imx->dev);
if (ret) if (ret < 0) {
goto out_put_per; dev_err(spi_imx->dev, "failed to enable clock\n");
goto out_runtime_pm_put;
}
spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per); spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
/* /*
...@@ -1690,7 +1692,7 @@ static int spi_imx_probe(struct platform_device *pdev) ...@@ -1690,7 +1692,7 @@ static int spi_imx_probe(struct platform_device *pdev)
if (spi_imx->devtype_data->has_dmamode) { if (spi_imx->devtype_data->has_dmamode) {
ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master); ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
if (ret == -EPROBE_DEFER) if (ret == -EPROBE_DEFER)
goto out_clk_put; goto out_runtime_pm_put;
if (ret < 0) if (ret < 0)
dev_err(&pdev->dev, "dma setup error %d, use pio\n", dev_err(&pdev->dev, "dma setup error %d, use pio\n",
...@@ -1705,19 +1707,20 @@ static int spi_imx_probe(struct platform_device *pdev) ...@@ -1705,19 +1707,20 @@ static int spi_imx_probe(struct platform_device *pdev)
ret = spi_bitbang_start(&spi_imx->bitbang); ret = spi_bitbang_start(&spi_imx->bitbang);
if (ret) { if (ret) {
dev_err(&pdev->dev, "bitbang start failed with %d\n", ret); dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
goto out_clk_put; goto out_runtime_pm_put;
} }
dev_info(&pdev->dev, "probed\n"); dev_info(&pdev->dev, "probed\n");
clk_disable(spi_imx->clk_ipg); pm_runtime_mark_last_busy(spi_imx->dev);
clk_disable(spi_imx->clk_per); pm_runtime_put_autosuspend(spi_imx->dev);
return ret; return ret;
out_clk_put: out_runtime_pm_put:
clk_disable_unprepare(spi_imx->clk_ipg); pm_runtime_dont_use_autosuspend(spi_imx->dev);
out_put_per: pm_runtime_put_sync(spi_imx->dev);
clk_disable_unprepare(spi_imx->clk_per); pm_runtime_disable(spi_imx->dev);
out_master_put: out_master_put:
spi_master_put(master); spi_master_put(master);
...@@ -1732,30 +1735,82 @@ static int spi_imx_remove(struct platform_device *pdev) ...@@ -1732,30 +1735,82 @@ static int spi_imx_remove(struct platform_device *pdev)
spi_bitbang_stop(&spi_imx->bitbang); spi_bitbang_stop(&spi_imx->bitbang);
ret = clk_enable(spi_imx->clk_per); ret = pm_runtime_get_sync(spi_imx->dev);
if (ret < 0) {
dev_err(spi_imx->dev, "failed to enable clock\n");
return ret;
}
writel(0, spi_imx->base + MXC_CSPICTRL);
pm_runtime_dont_use_autosuspend(spi_imx->dev);
pm_runtime_put_sync(spi_imx->dev);
pm_runtime_disable(spi_imx->dev);
spi_imx_sdma_exit(spi_imx);
spi_master_put(master);
return 0;
}
static int __maybe_unused spi_imx_runtime_resume(struct device *dev)
{
struct spi_master *master = dev_get_drvdata(dev);
struct spi_imx_data *spi_imx;
int ret;
spi_imx = spi_master_get_devdata(master);
ret = clk_prepare_enable(spi_imx->clk_per);
if (ret) if (ret)
return ret; return ret;
ret = clk_enable(spi_imx->clk_ipg); ret = clk_prepare_enable(spi_imx->clk_ipg);
if (ret) { if (ret) {
clk_disable(spi_imx->clk_per); clk_disable_unprepare(spi_imx->clk_per);
return ret; return ret;
} }
writel(0, spi_imx->base + MXC_CSPICTRL); return 0;
clk_disable_unprepare(spi_imx->clk_ipg); }
static int __maybe_unused spi_imx_runtime_suspend(struct device *dev)
{
struct spi_master *master = dev_get_drvdata(dev);
struct spi_imx_data *spi_imx;
spi_imx = spi_master_get_devdata(master);
clk_disable_unprepare(spi_imx->clk_per); clk_disable_unprepare(spi_imx->clk_per);
spi_imx_sdma_exit(spi_imx); clk_disable_unprepare(spi_imx->clk_ipg);
spi_master_put(master);
return 0;
}
static int __maybe_unused spi_imx_suspend(struct device *dev)
{
pinctrl_pm_select_sleep_state(dev);
return 0; return 0;
} }
static int __maybe_unused spi_imx_resume(struct device *dev)
{
pinctrl_pm_select_default_state(dev);
return 0;
}
static const struct dev_pm_ops imx_spi_pm = {
SET_RUNTIME_PM_OPS(spi_imx_runtime_suspend,
spi_imx_runtime_resume, NULL)
SET_SYSTEM_SLEEP_PM_OPS(spi_imx_suspend, spi_imx_resume)
};
static struct platform_driver spi_imx_driver = { static struct platform_driver spi_imx_driver = {
.driver = { .driver = {
.name = DRIVER_NAME, .name = DRIVER_NAME,
.of_match_table = spi_imx_dt_ids, .of_match_table = spi_imx_dt_ids,
}, .pm = &imx_spi_pm,
},
.id_table = spi_imx_devtype, .id_table = spi_imx_devtype,
.probe = spi_imx_probe, .probe = spi_imx_probe,
.remove = spi_imx_remove, .remove = spi_imx_remove,
......
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