diff mbox series

[9/9] ASoC: cs35l41: Use devm_pm_runtime_enable()

Message ID 20230902210621.1184693-10-cristian.ciocaltea@collabora.com
State New
Headers show
Series Improve CS35l41 ALSA SoC audio driver | expand

Commit Message

Cristian Ciocaltea Sept. 2, 2023, 9:06 p.m. UTC
Simplify runtime PM during probe by converting pm_runtime_enable() to
the managed version.

Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
---
 sound/soc/codecs/cs35l41.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Charles Keepax Sept. 5, 2023, 9:45 a.m. UTC | #1
On Sun, Sep 03, 2023 at 12:06:21AM +0300, Cristian Ciocaltea wrote:
> Simplify runtime PM during probe by converting pm_runtime_enable() to
> the managed version.
> 
> Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
> ---
> @@ -1376,7 +1379,6 @@ void cs35l41_remove(struct cs35l41_private *cs35l41)
>  	cancel_work_sync(&cs35l41->mdsync_up_work);
>  
>  	pm_runtime_get_sync(cs35l41->dev);
> -	pm_runtime_disable(cs35l41->dev);
>  
>  	regmap_write(cs35l41->regmap, CS35L41_IRQ1_MASK1, 0xFFFFFFFF);
>  	if (cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_PASS ||

Are we sure this is safe? The remove handler appears to be
written to disable pm_runtime at the start presumably to stop the
resume/suspend handler running during the remove callback.
Whereas after this change the pm_runtime isn't disabled until
after the remove callback has run. Does this open a window were
we could get an erroneous pm_runtime suspend after the
pm_runtime_put_noidle?

Thanks,
Charles
Cristian Ciocaltea Sept. 5, 2023, 7:15 p.m. UTC | #2
On 9/5/23 12:45, Charles Keepax wrote:
> On Sun, Sep 03, 2023 at 12:06:21AM +0300, Cristian Ciocaltea wrote:
>> Simplify runtime PM during probe by converting pm_runtime_enable() to
>> the managed version.
>>
>> Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
>> ---
>> @@ -1376,7 +1379,6 @@ void cs35l41_remove(struct cs35l41_private *cs35l41)
>>  	cancel_work_sync(&cs35l41->mdsync_up_work);
>>  
>>  	pm_runtime_get_sync(cs35l41->dev);
>> -	pm_runtime_disable(cs35l41->dev);
>>  
>>  	regmap_write(cs35l41->regmap, CS35L41_IRQ1_MASK1, 0xFFFFFFFF);
>>  	if (cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_PASS ||
> 
> Are we sure this is safe? The remove handler appears to be
> written to disable pm_runtime at the start presumably to stop the
> resume/suspend handler running during the remove callback.
> Whereas after this change the pm_runtime isn't disabled until
> after the remove callback has run. Does this open a window were
> we could get an erroneous pm_runtime suspend after the
> pm_runtime_put_noidle?

I've just made a test adding a 6s sleep before returning from the remove 
handler: 

[14444.894316] cs35l41 spi-VLV1776:00: Runtime resume
[14444.894469] cs35l41 spi-VLV1776:00: sleep 6s before return of cs35l41_remove()
[14448.338994] cs35l41 spi-VLV1776:00: Runtime suspend
[14451.079649] cs35l41 spi-VLV1776:00: return from cs35l41_remove()
[14451.080129] cs35l41 spi-VLV1776:00: Runtime resume
[14451.080165] cs35l41 spi-VLV1776:00: ASoC: Unregistered DAI 'cs35l41-pcm'
[14451.080181] cs35l41 spi-VLV1776:00: Runtime suspend
[14451.813639] acp5x_i2s_playcap acp5x_i2s_playcap.0: ASoC: Unregistered DAI 'acp5x_i2s_playcap.0'

As expected, suspend triggered, but a resume was issued later, before DAI
got unregistered.

I didn't notice any issues while repeating the test several times, hence 
I wonder what would be the reason to prevent getting suspend/resume events 
at this point?

Thanks,
Cristian
Charles Keepax Sept. 6, 2023, 4:37 p.m. UTC | #3
On Tue, Sep 05, 2023 at 10:15:46PM +0300, Cristian Ciocaltea wrote:
> On 9/5/23 12:45, Charles Keepax wrote:
> > On Sun, Sep 03, 2023 at 12:06:21AM +0300, Cristian Ciocaltea wrote:
> >> Simplify runtime PM during probe by converting pm_runtime_enable() to
> >> the managed version.
> >>
> >> Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
> >> ---
> >> @@ -1376,7 +1379,6 @@ void cs35l41_remove(struct cs35l41_private *cs35l41)
> >>  	cancel_work_sync(&cs35l41->mdsync_up_work);
> >>  
> >>  	pm_runtime_get_sync(cs35l41->dev);
> >> -	pm_runtime_disable(cs35l41->dev);
> >>  
> >>  	regmap_write(cs35l41->regmap, CS35L41_IRQ1_MASK1, 0xFFFFFFFF);
> >>  	if (cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_PASS ||
> > 
> > Are we sure this is safe? The remove handler appears to be
> > written to disable pm_runtime at the start presumably to stop the
> > resume/suspend handler running during the remove callback.
> > Whereas after this change the pm_runtime isn't disabled until
> > after the remove callback has run. Does this open a window were
> > we could get an erroneous pm_runtime suspend after the
> > pm_runtime_put_noidle?
> 
> I've just made a test adding a 6s sleep before returning from the remove 
> handler: 
> 
> [14444.894316] cs35l41 spi-VLV1776:00: Runtime resume
> [14444.894469] cs35l41 spi-VLV1776:00: sleep 6s before return of cs35l41_remove()
> [14448.338994] cs35l41 spi-VLV1776:00: Runtime suspend
> [14451.079649] cs35l41 spi-VLV1776:00: return from cs35l41_remove()
> [14451.080129] cs35l41 spi-VLV1776:00: Runtime resume
> [14451.080165] cs35l41 spi-VLV1776:00: ASoC: Unregistered DAI 'cs35l41-pcm'
> [14451.080181] cs35l41 spi-VLV1776:00: Runtime suspend
> [14451.813639] acp5x_i2s_playcap acp5x_i2s_playcap.0: ASoC: Unregistered DAI 'acp5x_i2s_playcap.0'
> 
> As expected, suspend triggered, but a resume was issued later, before DAI
> got unregistered.
> 
> I didn't notice any issues while repeating the test several times, hence 
> I wonder what would be the reason to prevent getting suspend/resume events 
> at this point?

The enter/exit hibernate code might run, which at the very
least might result in a bunch of unexpected and failing bus
traffic. Having a bit of a poke through the code, I guess the
most dangerous thing would if you actually got as far as an
extra runtime resume. This might cause cs35l41_init_boost
to run which would undo the work done by the call to
cs35l41_safe_reset in remove, which could leave the boost in a
dangerous state when we enable reset/power down the supplies,
which I think was not considered good. But its just likely
simpler/cleaner if we don't have to think about all the
possible implications of such things by just not allowing
it to happen.

Thanks,
Charles
Cristian Ciocaltea Sept. 6, 2023, 7:55 p.m. UTC | #4
On 9/6/23 19:37, Charles Keepax wrote:
> On Tue, Sep 05, 2023 at 10:15:46PM +0300, Cristian Ciocaltea wrote:
>> On 9/5/23 12:45, Charles Keepax wrote:
>>> On Sun, Sep 03, 2023 at 12:06:21AM +0300, Cristian Ciocaltea wrote:
>>>> Simplify runtime PM during probe by converting pm_runtime_enable() to
>>>> the managed version.
>>>>
>>>> Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
>>>> ---
>>>> @@ -1376,7 +1379,6 @@ void cs35l41_remove(struct cs35l41_private *cs35l41)
>>>>  	cancel_work_sync(&cs35l41->mdsync_up_work);
>>>>  
>>>>  	pm_runtime_get_sync(cs35l41->dev);
>>>> -	pm_runtime_disable(cs35l41->dev);
>>>>  
>>>>  	regmap_write(cs35l41->regmap, CS35L41_IRQ1_MASK1, 0xFFFFFFFF);
>>>>  	if (cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_PASS ||
>>>
>>> Are we sure this is safe? The remove handler appears to be
>>> written to disable pm_runtime at the start presumably to stop the
>>> resume/suspend handler running during the remove callback.
>>> Whereas after this change the pm_runtime isn't disabled until
>>> after the remove callback has run. Does this open a window were
>>> we could get an erroneous pm_runtime suspend after the
>>> pm_runtime_put_noidle?
>>
>> I've just made a test adding a 6s sleep before returning from the remove 
>> handler: 
>>
>> [14444.894316] cs35l41 spi-VLV1776:00: Runtime resume
>> [14444.894469] cs35l41 spi-VLV1776:00: sleep 6s before return of cs35l41_remove()
>> [14448.338994] cs35l41 spi-VLV1776:00: Runtime suspend
>> [14451.079649] cs35l41 spi-VLV1776:00: return from cs35l41_remove()
>> [14451.080129] cs35l41 spi-VLV1776:00: Runtime resume
>> [14451.080165] cs35l41 spi-VLV1776:00: ASoC: Unregistered DAI 'cs35l41-pcm'
>> [14451.080181] cs35l41 spi-VLV1776:00: Runtime suspend
>> [14451.813639] acp5x_i2s_playcap acp5x_i2s_playcap.0: ASoC: Unregistered DAI 'acp5x_i2s_playcap.0'
>>
>> As expected, suspend triggered, but a resume was issued later, before DAI
>> got unregistered.
>>
>> I didn't notice any issues while repeating the test several times, hence 
>> I wonder what would be the reason to prevent getting suspend/resume events 
>> at this point?
> 
> The enter/exit hibernate code might run, which at the very
> least might result in a bunch of unexpected and failing bus
> traffic. Having a bit of a poke through the code, I guess the
> most dangerous thing would if you actually got as far as an
> extra runtime resume. This might cause cs35l41_init_boost
> to run which would undo the work done by the call to
> cs35l41_safe_reset in remove, which could leave the boost in a
> dangerous state when we enable reset/power down the supplies,
> which I think was not considered good. But its just likely
> simpler/cleaner if we don't have to think about all the
> possible implications of such things by just not allowing
> it to happen.

Agree, let's keep it simple. I will revert the change and instead ensure
a proper cleanup of pm_runtime_use_autosuspend(), according to the
documentation:

"It's important to undo this with pm_runtime_dont_use_autosuspend() at
driver exit time unless your driver initially enabled pm_runtime with
devm_pm_runtime_enable() (which handles it for you)."

Thanks for the clarifications,
Cristian
Charles Keepax Sept. 7, 2023, 8:37 a.m. UTC | #5
On Wed, Sep 06, 2023 at 10:55:28PM +0300, Cristian Ciocaltea wrote:
> On 9/6/23 19:37, Charles Keepax wrote:
> > On Tue, Sep 05, 2023 at 10:15:46PM +0300, Cristian Ciocaltea wrote:
> >> On 9/5/23 12:45, Charles Keepax wrote:
> >>> On Sun, Sep 03, 2023 at 12:06:21AM +0300, Cristian Ciocaltea wrote:
> Agree, let's keep it simple. I will revert the change and instead ensure
> a proper cleanup of pm_runtime_use_autosuspend(), according to the
> documentation:
> 
> "It's important to undo this with pm_runtime_dont_use_autosuspend() at
> driver exit time unless your driver initially enabled pm_runtime with
> devm_pm_runtime_enable() (which handles it for you)."
> 

Good spot was not aware of that, thank you.

Thanks,
Charles
diff mbox series

Patch

diff --git a/sound/soc/codecs/cs35l41.c b/sound/soc/codecs/cs35l41.c
index 5655758063ae..2e5b4633e98d 100644
--- a/sound/soc/codecs/cs35l41.c
+++ b/sound/soc/codecs/cs35l41.c
@@ -1340,7 +1340,12 @@  int cs35l41_probe(struct cs35l41_private *cs35l41, const struct cs35l41_hw_cfg *
 	pm_runtime_mark_last_busy(cs35l41->dev);
 	pm_runtime_set_active(cs35l41->dev);
 	pm_runtime_get_noresume(cs35l41->dev);
-	pm_runtime_enable(cs35l41->dev);
+
+	ret = devm_pm_runtime_enable(cs35l41->dev);
+	if (ret < 0) {
+		dev_err_probe(cs35l41->dev, ret, "Failed to enable PM runtime\n");
+		goto err_pm;
+	}
 
 	ret = devm_snd_soc_register_component(cs35l41->dev,
 					      &soc_component_dev_cs35l41,
@@ -1358,9 +1363,7 @@  int cs35l41_probe(struct cs35l41_private *cs35l41, const struct cs35l41_hw_cfg *
 	return 0;
 
 err_pm:
-	pm_runtime_disable(cs35l41->dev);
 	pm_runtime_put_noidle(cs35l41->dev);
-
 	wm_adsp2_remove(&cs35l41->dsp);
 err:
 	cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
@@ -1376,7 +1379,6 @@  void cs35l41_remove(struct cs35l41_private *cs35l41)
 	cancel_work_sync(&cs35l41->mdsync_up_work);
 
 	pm_runtime_get_sync(cs35l41->dev);
-	pm_runtime_disable(cs35l41->dev);
 
 	regmap_write(cs35l41->regmap, CS35L41_IRQ1_MASK1, 0xFFFFFFFF);
 	if (cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_PASS ||