Commit 6ec417d2 authored by Fabrice Gasnier's avatar Fabrice Gasnier Committed by Jonathan Cameron

iio: adc: stm32-dfsdm: add PM support

Add PM and runtime PM support to STM32 DFSDM drivers:
- stm32-dfsdm-core: manage clocks.
- stm32-dfsdm-adc: restore channels configuration upon resume. Also stop
  restart everything in case of buffer mode.
Signed-off-by: default avatarFabrice Gasnier <fabrice.gasnier@st.com>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 9f57110d
...@@ -868,24 +868,18 @@ static int stm32_dfsdm_update_scan_mode(struct iio_dev *indio_dev, ...@@ -868,24 +868,18 @@ static int stm32_dfsdm_update_scan_mode(struct iio_dev *indio_dev,
return 0; return 0;
} }
static int stm32_dfsdm_postenable(struct iio_dev *indio_dev) static int __stm32_dfsdm_postenable(struct iio_dev *indio_dev)
{ {
struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
int ret; int ret;
if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
ret = iio_triggered_buffer_postenable(indio_dev);
if (ret < 0)
return ret;
}
/* Reset adc buffer index */ /* Reset adc buffer index */
adc->bufi = 0; adc->bufi = 0;
if (adc->hwc) { if (adc->hwc) {
ret = iio_hw_consumer_enable(adc->hwc); ret = iio_hw_consumer_enable(adc->hwc);
if (ret < 0) if (ret < 0)
goto err_predisable; return ret;
} }
ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
...@@ -913,6 +907,26 @@ static int stm32_dfsdm_postenable(struct iio_dev *indio_dev) ...@@ -913,6 +907,26 @@ static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
err_stop_hwc: err_stop_hwc:
if (adc->hwc) if (adc->hwc)
iio_hw_consumer_disable(adc->hwc); iio_hw_consumer_disable(adc->hwc);
return ret;
}
static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
{
int ret;
if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
ret = iio_triggered_buffer_postenable(indio_dev);
if (ret < 0)
return ret;
}
ret = __stm32_dfsdm_postenable(indio_dev);
if (ret < 0)
goto err_predisable;
return 0;
err_predisable: err_predisable:
if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
iio_triggered_buffer_predisable(indio_dev); iio_triggered_buffer_predisable(indio_dev);
...@@ -920,7 +934,7 @@ static int stm32_dfsdm_postenable(struct iio_dev *indio_dev) ...@@ -920,7 +934,7 @@ static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
return ret; return ret;
} }
static int stm32_dfsdm_predisable(struct iio_dev *indio_dev) static void __stm32_dfsdm_predisable(struct iio_dev *indio_dev)
{ {
struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
...@@ -932,6 +946,11 @@ static int stm32_dfsdm_predisable(struct iio_dev *indio_dev) ...@@ -932,6 +946,11 @@ static int stm32_dfsdm_predisable(struct iio_dev *indio_dev)
if (adc->hwc) if (adc->hwc)
iio_hw_consumer_disable(adc->hwc); iio_hw_consumer_disable(adc->hwc);
}
static int stm32_dfsdm_predisable(struct iio_dev *indio_dev)
{
__stm32_dfsdm_predisable(indio_dev);
if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
iio_triggered_buffer_predisable(indio_dev); iio_triggered_buffer_predisable(indio_dev);
...@@ -1496,10 +1515,48 @@ static int stm32_dfsdm_adc_remove(struct platform_device *pdev) ...@@ -1496,10 +1515,48 @@ static int stm32_dfsdm_adc_remove(struct platform_device *pdev)
return 0; return 0;
} }
static int __maybe_unused stm32_dfsdm_adc_suspend(struct device *dev)
{
struct stm32_dfsdm_adc *adc = dev_get_drvdata(dev);
struct iio_dev *indio_dev = iio_priv_to_dev(adc);
if (iio_buffer_enabled(indio_dev))
__stm32_dfsdm_predisable(indio_dev);
return 0;
}
static int __maybe_unused stm32_dfsdm_adc_resume(struct device *dev)
{
struct stm32_dfsdm_adc *adc = dev_get_drvdata(dev);
struct iio_dev *indio_dev = iio_priv_to_dev(adc);
const struct iio_chan_spec *chan;
struct stm32_dfsdm_channel *ch;
int i, ret;
/* restore channels configuration */
for (i = 0; i < indio_dev->num_channels; i++) {
chan = indio_dev->channels + i;
ch = &adc->dfsdm->ch_list[chan->channel];
ret = stm32_dfsdm_chan_configure(adc->dfsdm, ch);
if (ret)
return ret;
}
if (iio_buffer_enabled(indio_dev))
__stm32_dfsdm_postenable(indio_dev);
return 0;
}
static SIMPLE_DEV_PM_OPS(stm32_dfsdm_adc_pm_ops,
stm32_dfsdm_adc_suspend, stm32_dfsdm_adc_resume);
static struct platform_driver stm32_dfsdm_adc_driver = { static struct platform_driver stm32_dfsdm_adc_driver = {
.driver = { .driver = {
.name = "stm32-dfsdm-adc", .name = "stm32-dfsdm-adc",
.of_match_table = stm32_dfsdm_adc_match, .of_match_table = stm32_dfsdm_adc_match,
.pm = &stm32_dfsdm_adc_pm_ops,
}, },
.probe = stm32_dfsdm_adc_probe, .probe = stm32_dfsdm_adc_probe,
.remove = stm32_dfsdm_adc_remove, .remove = stm32_dfsdm_adc_remove,
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/of_device.h> #include <linux/of_device.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h> #include <linux/regmap.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -90,6 +92,36 @@ struct dfsdm_priv { ...@@ -90,6 +92,36 @@ struct dfsdm_priv {
struct clk *aclk; /* audio clock */ struct clk *aclk; /* audio clock */
}; };
static inline struct dfsdm_priv *to_stm32_dfsdm_priv(struct stm32_dfsdm *dfsdm)
{
return container_of(dfsdm, struct dfsdm_priv, dfsdm);
}
static int stm32_dfsdm_clk_prepare_enable(struct stm32_dfsdm *dfsdm)
{
struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
int ret;
ret = clk_prepare_enable(priv->clk);
if (ret || !priv->aclk)
return ret;
ret = clk_prepare_enable(priv->aclk);
if (ret)
clk_disable_unprepare(priv->clk);
return ret;
}
static void stm32_dfsdm_clk_disable_unprepare(struct stm32_dfsdm *dfsdm)
{
struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
if (priv->aclk)
clk_disable_unprepare(priv->aclk);
clk_disable_unprepare(priv->clk);
}
/** /**
* stm32_dfsdm_start_dfsdm - start global dfsdm interface. * stm32_dfsdm_start_dfsdm - start global dfsdm interface.
* *
...@@ -98,24 +130,17 @@ struct dfsdm_priv { ...@@ -98,24 +130,17 @@ struct dfsdm_priv {
*/ */
int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm) int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm)
{ {
struct dfsdm_priv *priv = container_of(dfsdm, struct dfsdm_priv, dfsdm); struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
struct device *dev = &priv->pdev->dev; struct device *dev = &priv->pdev->dev;
unsigned int clk_div = priv->spi_clk_out_div, clk_src; unsigned int clk_div = priv->spi_clk_out_div, clk_src;
int ret; int ret;
if (atomic_inc_return(&priv->n_active_ch) == 1) { if (atomic_inc_return(&priv->n_active_ch) == 1) {
ret = clk_prepare_enable(priv->clk); ret = pm_runtime_get_sync(dev);
if (ret < 0) { if (ret < 0) {
dev_err(dev, "Failed to start clock\n"); pm_runtime_put_noidle(dev);
goto error_ret; goto error_ret;
} }
if (priv->aclk) {
ret = clk_prepare_enable(priv->aclk);
if (ret < 0) {
dev_err(dev, "Failed to start audio clock\n");
goto disable_clk;
}
}
/* select clock source, e.g. 0 for "dfsdm" or 1 for "audio" */ /* select clock source, e.g. 0 for "dfsdm" or 1 for "audio" */
clk_src = priv->aclk ? 1 : 0; clk_src = priv->aclk ? 1 : 0;
...@@ -123,21 +148,21 @@ int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm) ...@@ -123,21 +148,21 @@ int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm)
DFSDM_CHCFGR1_CKOUTSRC_MASK, DFSDM_CHCFGR1_CKOUTSRC_MASK,
DFSDM_CHCFGR1_CKOUTSRC(clk_src)); DFSDM_CHCFGR1_CKOUTSRC(clk_src));
if (ret < 0) if (ret < 0)
goto disable_aclk; goto pm_put;
/* Output the SPI CLKOUT (if clk_div == 0 clock if OFF) */ /* Output the SPI CLKOUT (if clk_div == 0 clock if OFF) */
ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0), ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0),
DFSDM_CHCFGR1_CKOUTDIV_MASK, DFSDM_CHCFGR1_CKOUTDIV_MASK,
DFSDM_CHCFGR1_CKOUTDIV(clk_div)); DFSDM_CHCFGR1_CKOUTDIV(clk_div));
if (ret < 0) if (ret < 0)
goto disable_aclk; goto pm_put;
/* Global enable of DFSDM interface */ /* Global enable of DFSDM interface */
ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0), ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0),
DFSDM_CHCFGR1_DFSDMEN_MASK, DFSDM_CHCFGR1_DFSDMEN_MASK,
DFSDM_CHCFGR1_DFSDMEN(1)); DFSDM_CHCFGR1_DFSDMEN(1));
if (ret < 0) if (ret < 0)
goto disable_aclk; goto pm_put;
} }
dev_dbg(dev, "%s: n_active_ch %d\n", __func__, dev_dbg(dev, "%s: n_active_ch %d\n", __func__,
...@@ -145,11 +170,8 @@ int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm) ...@@ -145,11 +170,8 @@ int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm)
return 0; return 0;
disable_aclk: pm_put:
clk_disable_unprepare(priv->aclk); pm_runtime_put_sync(dev);
disable_clk:
clk_disable_unprepare(priv->clk);
error_ret: error_ret:
atomic_dec(&priv->n_active_ch); atomic_dec(&priv->n_active_ch);
...@@ -165,7 +187,7 @@ EXPORT_SYMBOL_GPL(stm32_dfsdm_start_dfsdm); ...@@ -165,7 +187,7 @@ EXPORT_SYMBOL_GPL(stm32_dfsdm_start_dfsdm);
*/ */
int stm32_dfsdm_stop_dfsdm(struct stm32_dfsdm *dfsdm) int stm32_dfsdm_stop_dfsdm(struct stm32_dfsdm *dfsdm)
{ {
struct dfsdm_priv *priv = container_of(dfsdm, struct dfsdm_priv, dfsdm); struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
int ret; int ret;
if (atomic_dec_and_test(&priv->n_active_ch)) { if (atomic_dec_and_test(&priv->n_active_ch)) {
...@@ -183,9 +205,7 @@ int stm32_dfsdm_stop_dfsdm(struct stm32_dfsdm *dfsdm) ...@@ -183,9 +205,7 @@ int stm32_dfsdm_stop_dfsdm(struct stm32_dfsdm *dfsdm)
if (ret < 0) if (ret < 0)
return ret; return ret;
clk_disable_unprepare(priv->clk); pm_runtime_put_sync(&priv->pdev->dev);
if (priv->aclk)
clk_disable_unprepare(priv->aclk);
} }
dev_dbg(&priv->pdev->dev, "%s: n_active_ch %d\n", __func__, dev_dbg(&priv->pdev->dev, "%s: n_active_ch %d\n", __func__,
atomic_read(&priv->n_active_ch)); atomic_read(&priv->n_active_ch));
...@@ -325,14 +345,111 @@ static int stm32_dfsdm_probe(struct platform_device *pdev) ...@@ -325,14 +345,111 @@ static int stm32_dfsdm_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, dfsdm); platform_set_drvdata(pdev, dfsdm);
return devm_of_platform_populate(&pdev->dev); ret = stm32_dfsdm_clk_prepare_enable(dfsdm);
if (ret) {
dev_err(&pdev->dev, "Failed to start clock\n");
return ret;
}
pm_runtime_get_noresume(&pdev->dev);
pm_runtime_set_active(&pdev->dev);
pm_runtime_enable(&pdev->dev);
ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
if (ret)
goto pm_put;
pm_runtime_put(&pdev->dev);
return 0;
pm_put:
pm_runtime_disable(&pdev->dev);
pm_runtime_set_suspended(&pdev->dev);
pm_runtime_put_noidle(&pdev->dev);
stm32_dfsdm_clk_disable_unprepare(dfsdm);
return ret;
}
static int stm32_dfsdm_core_remove(struct platform_device *pdev)
{
struct stm32_dfsdm *dfsdm = platform_get_drvdata(pdev);
pm_runtime_get_sync(&pdev->dev);
of_platform_depopulate(&pdev->dev);
pm_runtime_disable(&pdev->dev);
pm_runtime_set_suspended(&pdev->dev);
pm_runtime_put_noidle(&pdev->dev);
stm32_dfsdm_clk_disable_unprepare(dfsdm);
return 0;
}
static int __maybe_unused stm32_dfsdm_core_suspend(struct device *dev)
{
struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev);
struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
int ret;
ret = pm_runtime_force_suspend(dev);
if (ret)
return ret;
/* Balance devm_regmap_init_mmio_clk() clk_prepare() */
clk_unprepare(priv->clk);
return pinctrl_pm_select_sleep_state(dev);
}
static int __maybe_unused stm32_dfsdm_core_resume(struct device *dev)
{
struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev);
struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
int ret;
ret = pinctrl_pm_select_default_state(dev);
if (ret)
return ret;
ret = clk_prepare(priv->clk);
if (ret)
return ret;
return pm_runtime_force_resume(dev);
} }
static int __maybe_unused stm32_dfsdm_core_runtime_suspend(struct device *dev)
{
struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev);
stm32_dfsdm_clk_disable_unprepare(dfsdm);
return 0;
}
static int __maybe_unused stm32_dfsdm_core_runtime_resume(struct device *dev)
{
struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev);
return stm32_dfsdm_clk_prepare_enable(dfsdm);
}
static const struct dev_pm_ops stm32_dfsdm_core_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(stm32_dfsdm_core_suspend,
stm32_dfsdm_core_resume)
SET_RUNTIME_PM_OPS(stm32_dfsdm_core_runtime_suspend,
stm32_dfsdm_core_runtime_resume,
NULL)
};
static struct platform_driver stm32_dfsdm_driver = { static struct platform_driver stm32_dfsdm_driver = {
.probe = stm32_dfsdm_probe, .probe = stm32_dfsdm_probe,
.remove = stm32_dfsdm_core_remove,
.driver = { .driver = {
.name = "stm32-dfsdm", .name = "stm32-dfsdm",
.of_match_table = stm32_dfsdm_of_match, .of_match_table = stm32_dfsdm_of_match,
.pm = &stm32_dfsdm_core_pm_ops,
}, },
}; };
......
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