Skip to contentSkip to conversation

ASoC: img-i2s-in: Add power management

PATCH In review Ed Blake 2 participants 8 messages Oct 2, 2017 – Oct 5, 2017

Subsystems (2)
lore
Share & tools
lore

Ed Blake

author
#0 lore raw

cover ASoC: img-i2s-in: Add power management

This patch set adds power managment to the img-i2s-in driver. The first patch adds system PM suspend / resume handling and the second patch adds runtime PM.

Ed Blake (2):
ASoC: img-i2s-in: Add suspend / resume handling
ASoC: img-i2s-in: Add runtime PM

 sound/soc/img/img-i2s-in.c | 126 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 114 insertions(+), 12 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/2 ASoC: img-i2s-in: Add suspend / resume handling

Implement ASoC suspend and resume callbacks to save and restore register state, to support platforms where the power is disabled during suspend.

---
 sound/soc/img/img-i2s-in.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
sound/soc/img/img-i2s-in.c +47 -0
index 567f976..ebed04d 100644
@@ -60,6 +60,8 @@ struct img_i2s_in {
 	void __iomem *channel_base;
 	unsigned int active_channels;
 	struct snd_soc_dai_driver dai_driver;
+	u32 suspend_ctl;
+	u32 *suspend_ch_ctl;
 };
 
 static inline void img_i2s_in_writel(struct img_i2s_in *i2s, u32 val, u32 reg)
@@ -360,6 +362,42 @@ static int img_i2s_in_dai_probe(struct snd_soc_dai *dai)
 	.name = "img-i2s-in"
 };
 
+static int img_i2s_in_dai_suspend(struct snd_soc_dai *dai)
+{
+	struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
+	int i;
+	u32 reg;
+
+	for (i = 0; i < i2s->max_i2s_chan; i++) {
+		reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
+		i2s->suspend_ch_ctl[i] = reg;
+	}
+
+	i2s->suspend_ctl = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
+
+	clk_disable_unprepare(i2s->clk_sys);
+
+	return 0;
+}
+
+static int img_i2s_in_dai_resume(struct snd_soc_dai *dai)
+{
+	struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
+	int i;
+	u32 reg;
+
+	clk_prepare_enable(i2s->clk_sys);
+
+	for (i = 0; i < i2s->max_i2s_chan; i++) {
+		reg = i2s->suspend_ch_ctl[i];
+		img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
+	}
+
+	img_i2s_in_writel(i2s, i2s->suspend_ctl, IMG_I2S_IN_CTL);
+
+	return 0;
+}
+
 static int img_i2s_in_dma_prepare_slave_config(struct snd_pcm_substream *st,
 	struct snd_pcm_hw_params *params, struct dma_slave_config *sc)
 {
@@ -436,6 +474,8 @@ static int img_i2s_in_probe(struct platform_device *pdev)
 	i2s->dma_data.addr_width = 4;
 
 	i2s->dai_driver.probe = img_i2s_in_dai_probe;
+	i2s->dai_driver.suspend = img_i2s_in_dai_suspend;
+	i2s->dai_driver.resume = img_i2s_in_dai_resume;
 	i2s->dai_driver.capture.channels_min = 2;
 	i2s->dai_driver.capture.channels_max = i2s->max_i2s_chan * 2;
 	i2s->dai_driver.capture.rates = SNDRV_PCM_RATE_8000_192000;
@@ -469,6 +509,13 @@ static int img_i2s_in_probe(struct platform_device *pdev)
 			IMG_I2S_IN_CH_CTL_JUST_MASK |
 			IMG_I2S_IN_CH_CTL_FW_MASK, IMG_I2S_IN_CH_CTL);
 
+	i2s->suspend_ch_ctl = devm_kzalloc(dev,
+		sizeof(*i2s->suspend_ch_ctl) * i2s->max_i2s_chan, GFP_KERNEL);
+	if (!i2s->suspend_ch_ctl) {
+		ret = -ENOMEM;
+		goto err_clk_disable;
+	}
+
 	ret = devm_snd_soc_register_component(dev, &img_i2s_in_component,
 						&i2s->dai_driver, 1);
 	if (ret)
Signature · 1 line
1.9.1
maintainer
#2 lore raw

Re: [PATCH 1/2] ASoC: img-i2s-in: Add suspend / resume handling

sound/soc/img/img-i2s-in.c static int img_i2s_in_probe(struct platform_device *pdev) · line 478

Quoted 3 lines · Ed Blake
sound/soc/img/img-i2s-in.c +2 -0
 	i2s->dai_driver.probe = img_i2s_in_dai_probe;
+	i2s->dai_driver.suspend = img_i2s_in_dai_suspend;
+	i2s->dai_driver.resume = img_i2s_in_dai_resume;

Why use the ASoC specific power management callbacks and not the system ones? It does also look a bit like there's a case for converting the driver to regmap and using the regmap cache code.

author
#3 lore raw

Re: [PATCH 1/2] ASoC: img-i2s-in: Add suspend / resume handling

Quoted 8 lines · Mark Brown

sound/soc/img/img-i2s-in.c static int img_i2s_in_probe(struct platform_device *pdev) · line 478

Quoted 3 lines · Ed Blake
sound/soc/img/img-i2s-in.c +2 -0
 	i2s->dai_driver.probe = img_i2s_in_dai_probe;
+	i2s->dai_driver.suspend = img_i2s_in_dai_suspend;
+	i2s->dai_driver.resume = img_i2s_in_dai_resume;

Why use the ASoC specific power management callbacks and not the system ones? It does also look a bit like there's a case for converting the driver to regmap and using the regmap cache code.

Looking at other drivers, most seemed to use the ASoC specific callbacks.  Are the system ones preferred?  If so I'll change it for v2.

Quoted 2 lines

It does also look a bit like there's a case for converting the driver to regmap and using the regmap cache code.

Is it ok to do this in a future patch?

Ed.

maintainer
#4 lore raw

Re: [PATCH 1/2] ASoC: img-i2s-in: Add suspend / resume handling

Quoted 8 lines · Ed Blake
Quoted 3 lines · Mark Brown

Why use the ASoC specific power management callbacks and not the system ones? It does also look a bit like there's a case for converting the driver to regmap and using the regmap cache code.

Looking at other drivers, most seemed to use the ASoC specific callbacks.  Are the system ones preferred?  If so I'll change it for v2.

Yes, look at newer drivers more here. Unless you've a need to do some ASoC specific stuff but that's not the case for this.

Quoted 4 lines
Quoted 2 lines

It does also look a bit like there's a case for converting the driver to regmap and using the regmap cache code.

Is it ok to do this in a future patch?

Well, the comment was there because you were open coding the regmap cache sync code but it's not the end of the world to do it later.

author
#5 lore raw

PATCH 2/2 ASoC: img-i2s-in: Add runtime PM

In reply to Ed Blake

Add runtime PM to disable the clock when the device is not in use. The ASoC core takes care of the pm_runtime_get_sync / put calls so we just need to provide runtime_suspend / runtime_resume functions and enable runtime PM.

---
 sound/soc/img/img-i2s-in.c | 89 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 72 insertions(+), 17 deletions(-)
sound/soc/img/img-i2s-in.c +72 -17
index ebed04d..45e7b1a 100644
@@ -16,6 +16,7 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
 #include <linux/reset.h>
 
 #include <sound/core.h>
@@ -64,6 +65,29 @@ struct img_i2s_in {
 	u32 *suspend_ch_ctl;
 };
 
+static int img_i2s_in_runtime_suspend(struct device *dev)
+{
+	struct img_i2s_in *i2s = dev_get_drvdata(dev);
+
+	clk_disable_unprepare(i2s->clk_sys);
+
+	return 0;
+}
+
+static int img_i2s_in_runtime_resume(struct device *dev)
+{
+	struct img_i2s_in *i2s = dev_get_drvdata(dev);
+	int ret;
+
+	ret = clk_prepare_enable(i2s->clk_sys);
+	if (ret) {
+		dev_err(dev, "Unable to enable sys clock\n");
+		return ret;
+	}
+
+	return 0;
+}
+
 static inline void img_i2s_in_writel(struct img_i2s_in *i2s, u32 val, u32 reg)
 {
 	writel(val, i2s->base + reg);
@@ -321,6 +345,7 @@ static int img_i2s_in_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
 
 	chan_control_mask = IMG_I2S_IN_CH_CTL_CLK_TRANS_MASK;
 
+	pm_runtime_get_sync(i2s->dev);
 	for (i = 0; i < i2s->active_channels; i++)
 		img_i2s_in_ch_disable(i2s, i);
 
@@ -340,6 +365,8 @@ static int img_i2s_in_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
 	for (i = 0; i < i2s->active_channels; i++)
 		img_i2s_in_ch_enable(i2s, i);
 
+	pm_runtime_put(i2s->dev);
+
 	return 0;
 }
 
@@ -365,9 +392,15 @@ static int img_i2s_in_dai_probe(struct snd_soc_dai *dai)
 static int img_i2s_in_dai_suspend(struct snd_soc_dai *dai)
 {
 	struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
-	int i;
+	int i, ret;
 	u32 reg;
 
+	if (pm_runtime_status_suspended(i2s->dev)) {
+		ret = img_i2s_in_runtime_resume(i2s->dev);
+		if (ret)
+			return ret;
+	}
+
 	for (i = 0; i < i2s->max_i2s_chan; i++) {
 		reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
 		i2s->suspend_ch_ctl[i] = reg;
@@ -375,7 +408,7 @@ static int img_i2s_in_dai_suspend(struct snd_soc_dai *dai)
 
 	i2s->suspend_ctl = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
 
-	clk_disable_unprepare(i2s->clk_sys);
+	img_i2s_in_runtime_suspend(i2s->dev);
 
 	return 0;
 }
@@ -383,10 +416,12 @@ static int img_i2s_in_dai_suspend(struct snd_soc_dai *dai)
 static int img_i2s_in_dai_resume(struct snd_soc_dai *dai)
 {
 	struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
-	int i;
+	int i, ret;
 	u32 reg;
 
-	clk_prepare_enable(i2s->clk_sys);
+	ret = img_i2s_in_runtime_resume(i2s->dev);
+	if (ret)
+		return ret;
 
 	for (i = 0; i < i2s->max_i2s_chan; i++) {
 		reg = i2s->suspend_ch_ctl[i];
@@ -395,6 +430,9 @@ static int img_i2s_in_dai_resume(struct snd_soc_dai *dai)
 
 	img_i2s_in_writel(i2s, i2s->suspend_ctl, IMG_I2S_IN_CTL);
 
+	if (pm_runtime_status_suspended(i2s->dev))
+		img_i2s_in_runtime_suspend(i2s->dev);
+
 	return 0;
 }
 
@@ -465,9 +503,15 @@ static int img_i2s_in_probe(struct platform_device *pdev)
 		return PTR_ERR(i2s->clk_sys);
 	}
 
-	ret = clk_prepare_enable(i2s->clk_sys);
-	if (ret)
-		return ret;
+	pm_runtime_enable(&pdev->dev);
+	if (!pm_runtime_enabled(&pdev->dev)) {
+		ret = img_i2s_in_runtime_resume(&pdev->dev);
+		if (ret)
+			goto err_pm_disable;
+	}
+	ret = pm_runtime_get_sync(&pdev->dev);
+	if (ret < 0)
+		goto err_suspend;
 
 	i2s->active_channels = 1;
 	i2s->dma_data.addr = res->start + IMG_I2S_IN_RX_FIFO;
@@ -487,7 +531,7 @@ static int img_i2s_in_probe(struct platform_device *pdev)
 	if (IS_ERR(rst)) {
 		if (PTR_ERR(rst) == -EPROBE_DEFER) {
 			ret = -EPROBE_DEFER;
-			goto err_clk_disable;
+			goto err_suspend;
 		}
 
 		dev_dbg(dev, "No top level reset found\n");
@@ -509,35 +553,40 @@ static int img_i2s_in_probe(struct platform_device *pdev)
 			IMG_I2S_IN_CH_CTL_JUST_MASK |
 			IMG_I2S_IN_CH_CTL_FW_MASK, IMG_I2S_IN_CH_CTL);
 
+	pm_runtime_put(&pdev->dev);
+
 	i2s->suspend_ch_ctl = devm_kzalloc(dev,
 		sizeof(*i2s->suspend_ch_ctl) * i2s->max_i2s_chan, GFP_KERNEL);
 	if (!i2s->suspend_ch_ctl) {
 		ret = -ENOMEM;
-		goto err_clk_disable;
+		goto err_suspend;
 	}
 
 	ret = devm_snd_soc_register_component(dev, &img_i2s_in_component,
 						&i2s->dai_driver, 1);
 	if (ret)
-		goto err_clk_disable;
+		goto err_suspend;
 
 	ret = devm_snd_dmaengine_pcm_register(dev, &img_i2s_in_dma_config, 0);
 	if (ret)
-		goto err_clk_disable;
+		goto err_suspend;
 
 	return 0;
 
-err_clk_disable:
-	clk_disable_unprepare(i2s->clk_sys);
+err_suspend:
+	if (!pm_runtime_enabled(&pdev->dev))
+		img_i2s_in_runtime_suspend(&pdev->dev);
+err_pm_disable:
+	pm_runtime_disable(&pdev->dev);
 
 	return ret;
 }
 
 static int img_i2s_in_dev_remove(struct platform_device *pdev)
 {
-	struct img_i2s_in *i2s = platform_get_drvdata(pdev);
-
-	clk_disable_unprepare(i2s->clk_sys);
+	pm_runtime_disable(&pdev->dev);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		img_i2s_in_runtime_suspend(&pdev->dev);
 
 	return 0;
 }
@@ -548,10 +597,16 @@ static int img_i2s_in_dev_remove(struct platform_device *pdev)
 };
 MODULE_DEVICE_TABLE(of, img_i2s_in_of_match);
 
+static const struct dev_pm_ops img_i2s_in_pm_ops = {
+	SET_RUNTIME_PM_OPS(img_i2s_in_runtime_suspend,
+			   img_i2s_in_runtime_resume, NULL)
+};
+
 static struct platform_driver img_i2s_in_driver = {
 	.driver = {
 		.name = "img-i2s-in",
-		.of_match_table = img_i2s_in_of_match
+		.of_match_table = img_i2s_in_of_match,
+		.pm = &img_i2s_in_pm_ops
 	},
 	.probe = img_i2s_in_probe,
 	.remove = img_i2s_in_dev_remove
Signature · 1 line
1.9.1
maintainer
#6 lore raw

Re: [PATCH 2/2] ASoC: img-i2s-in: Add runtime PM

sound/soc/img/img-i2s-in.c static int img_i2s_in_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) · line 348

Quoted 1 line · Ed Blake
sound/soc/img/img-i2s-in.c +1 -0
+	pm_runtime_get_sync(i2s->dev);

There is no error checking here, you shouldn't just ignore the error.

author
#7 lore raw

Re: [PATCH 2/2] ASoC: img-i2s-in: Add runtime PM

Quoted 4 lines · Mark Brown

sound/soc/img/img-i2s-in.c static int img_i2s_in_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) · line 348

Quoted 1 line · Ed Blake
sound/soc/img/img-i2s-in.c +1 -0
+	pm_runtime_get_sync(i2s->dev);

There is no error checking here, you shouldn't just ignore the error.

I'll fix this in v2.

Ed.