Improve cs42l43 suspend/IRQ interactions
Subsystems (4)
cover Improve cs42l43 suspend/IRQ interactions
cs42l43 uses pm_runtime_force_suspend() during system suspend, however this means care must be taken that IRQ handler code isn't running when entering system suspend as force suspend will ignore that the handler is holding a pm reference. Typically the result of this is just a few error messages, but better to improve the handling and ensure that all IRQ processing is synchronised in before system suspend.
Note the ASoC and MFD bits of this series have no build dependencies, so could go through their seperate trees, however, it would be nice if all the patches could land in the same kernel version to avoid the IRQ handling being in a transitional state.
Thanks,
Charles
Charles Keepax (6):
ASoC: cs42l43: Rename system suspend callback and fix debug print
ASoC: cs42l43: Store IRQ domain in codec private data
ASoC: cs42l43: Disable IRQs in system suspend
ASoC: cs42l43: Shutdown jack detection on suspend
mfd: cs42l43: Move IRQ enable/disable to encompass force suspend
mfd: cs42l43: Remove IRQ masking in suspend
drivers/mfd/cs42l43.c | 32 +--------- sound/soc/codecs/cs42l43-jack.c | 8 ++- sound/soc/codecs/cs42l43.c | 109 +++++++++++++++++++++++--------- sound/soc/codecs/cs42l43.h | 3 + 4 files changed, 89 insertions(+), 63 deletions(-)
Signature · 1 line
2.47.2
PATCH 1/6 ASoC: cs42l43: Rename system suspend callback and fix debug print
There is some confusion around cs42l43_codec_runtime_force_suspend(). This function is the system suspend callback, however the name and the debug print both use the words runtime. Rename the function to the simpler cs42l43_codec_suspend() and correct the debug print.
--- sound/soc/codecs/cs42l43.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
sound/soc/codecs/cs42l43.c +3 -3
@@ -2386,11 +2386,11 @@ static int cs42l43_codec_runtime_resume(struct device *dev)return 0 ; } -static int cs42l43_codec_runtime_force_suspend (struct device *dev) +static int cs42l43_codec_suspend (struct device *dev) {struct cs42l43_codec *priv =dev_get_drvdata (dev); -dev_dbg (priv->dev,"Runtime suspend\n" ); +dev_dbg (priv->dev,"System suspend\n" ); priv->suspend_jack_debounce = true;
@@ -2401,7 +2401,7 @@ static int cs42l43_codec_runtime_force_suspend(struct device *dev)static const struct dev_pm_ops cs42l43_codec_pm_ops = { RUNTIME_PM_OPS(NULL, cs42l43_codec_runtime_resume, NULL) -SYSTEM_SLEEP_PM_OPS (cs42l43_codec_runtime_force_suspend, pm_runtime_force_resume) +SYSTEM_SLEEP_PM_OPS (cs42l43_codec_suspend, pm_runtime_force_resume) };static const struct platform_device_id cs42l43_codec_id_table[] = {
Signature · 1 line
2.47.2
PATCH 2/6 ASoC: cs42l43: Store IRQ domain in codec private data
In reply to Charles KeepaxTo support future refactoring store a pointer to the IRQ domain in the codec private data allowing easier access to it outside of probe.
--- sound/soc/codecs/cs42l43.c | 29 +++++++++++++---------------- sound/soc/codecs/cs42l43.h | 1 + 2 files changed, 14 insertions(+), 16 deletions(-)
sound/soc/codecs/cs42l43.c +13 -16
@@ -2210,13 +2210,12 @@ static const struct cs42l43_irq cs42l43_irqs[] = { };static int cs42l43_request_irq (struct cs42l43_codec *priv, -struct irq_domain *dom,const char *const name, -unsigned int irq,irq_handler_t handler, -unsigned long flags) +const char *const name,unsigned int irq, +irq_handler_t handler,unsigned long flags) {int ret; - ret =irq_create_mapping (dom, irq); + ret =irq_create_mapping (priv->dom, irq);if (ret <0 )return dev_err_probe (priv->dev, ret,"Failed to map IRQ %s\n" , name);
@@ -2230,8 +2229,7 @@ static int cs42l43_request_irq(struct cs42l43_codec *priv,return 0 ; } -static int cs42l43_shutter_irq (struct cs42l43_codec *priv, -struct irq_domain *dom,unsigned int shutter, +static int cs42l43_shutter_irq (struct cs42l43_codec *priv,unsigned int shutter,const char *const open_name,const char *const close_name,irq_handler_t handler)
@@ -2259,25 +2257,20 @@ static int cs42l43_shutter_irq(struct cs42l43_codec *priv,return 0 ; } - ret =cs42l43_request_irq (priv, dom, close_name, close_irq, handler,IRQF_SHARED ); + ret =cs42l43_request_irq (priv, close_name, close_irq, handler,IRQF_SHARED );if (ret)return ret; -return cs42l43_request_irq (priv, dom, open_name, open_irq, handler,IRQF_SHARED ); +return cs42l43_request_irq (priv, open_name, open_irq, handler,IRQF_SHARED ); }static int cs42l43_codec_probe (struct platform_device *pdev) {struct cs42l43 *cs42l43 =dev_get_drvdata (pdev->dev.parent);struct cs42l43_codec *priv; -struct irq_domain *dom;unsigned int val;int i, ret; - dom =irq_find_matching_fwnode (dev_fwnode (cs42l43->dev),DOMAIN_BUS_ANY ); -if (!dom) -return -EPROBE_DEFER ; - priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL );if (!priv)return -ENOMEM ;
@@ -2285,6 +2278,10 @@ static int cs42l43_codec_probe(struct platform_device *pdev) priv->dev = &pdev->dev; priv->core = cs42l43; + priv->dom =irq_find_matching_fwnode (dev_fwnode (cs42l43->dev),DOMAIN_BUS_ANY ); +if (!priv->dom) +return -EPROBE_DEFER ; +platform_set_drvdata (pdev, priv);mutex_init (&priv->jack_lock);
@@ -2314,7 +2311,7 @@ static int cs42l43_codec_probe(struct platform_device *pdev) goto err_pm;for (i =0 ; i <ARRAY_SIZE (cs42l43_irqs); i++) { - ret =cs42l43_request_irq (priv, dom, cs42l43_irqs[i].name, + ret =cs42l43_request_irq (priv, cs42l43_irqs[i].name, cs42l43_irqs[i].irq, cs42l43_irqs[i].handler,0 );if (ret)
@@ -2327,13 +2324,13 @@ static int cs42l43_codec_probe(struct platform_device *pdev) goto err_pm; } - ret =cs42l43_shutter_irq (priv, dom, val &CS42L43_MIC_SHUTTER_CFG_MASK , + ret =cs42l43_shutter_irq (priv, val &CS42L43_MIC_SHUTTER_CFG_MASK ,"mic shutter open" ,"mic shutter close" , cs42l43_mic_shutter);if (ret) goto err_pm; - ret =cs42l43_shutter_irq (priv, dom, (val &CS42L43_SPK_SHUTTER_CFG_MASK ) >> + ret =cs42l43_shutter_irq (priv, (val &CS42L43_SPK_SHUTTER_CFG_MASK ) >>CS42L43_SPK_SHUTTER_CFG_SHIFT ,"spk shutter open" ,"spk shutter close" , cs42l43_spk_shutter);
sound/soc/codecs/cs42l43.h +1 -0
@@ -44,6 +44,7 @@ struct cs42l43_codec {struct device *dev;struct cs42l43 *core;struct snd_soc_component *component; +struct irq_domain *dom;struct clk *mclk;
Signature · 1 line
2.47.2
PATCH 3/6 ASoC: cs42l43: Disable IRQs in system suspend
In reply to Charles KeepaxCurrently the MFD driver disables all the IRQs upon entering system suspend, however there are some issues with this approach. As this device uses runtime force suspend.
The regmap IRQ handler can run, claim a PM runtime reference and get scheduled, the MFD can then force suspend. When the IRQ thread gets rescheduled it will try to access volatile registers on the suspended device. Furthermore, this race also applies to work queue items scheduled by the IRQ handlers.
As the MFD code doesn't know about the individual work queue items, the end drivers must mask their own IRQs and sync in any work queues as part of entering system suspend. Update the code here to do so.
--- sound/soc/codecs/cs42l43.c | 78 ++++++++++++++++++++++++++++++-------- sound/soc/codecs/cs42l43.h | 1 + 2 files changed, 64 insertions(+), 15 deletions(-)
sound/soc/codecs/cs42l43.c +63 -15
@@ -2229,12 +2229,29 @@ static int cs42l43_request_irq(struct cs42l43_codec *priv,return 0 ; } +static void cs42l43_disable_irq (struct cs42l43_codec *priv,unsigned int irq) +{ +int ret; + + ret =irq_find_mapping (priv->dom, irq); +if (ret >0 ) +disable_irq (ret); +} + +static void cs42l43_enable_irq (struct cs42l43_codec *priv,unsigned int irq) +{ +int ret; + + ret =irq_find_mapping (priv->dom, irq); +if (ret >0 ) +enable_irq (ret); +} +static int cs42l43_shutter_irq (struct cs42l43_codec *priv,unsigned int shutter, -const char *const open_name, -const char *const close_name, +const char *const open_name,unsigned int *open_irq, +const char *const close_name,unsigned int *close_irq,irq_handler_t handler) { -unsigned int open_irq, close_irq;int ret;switch (shutter) {
@@ -2242,26 +2259,26 @@ static int cs42l43_shutter_irq(struct cs42l43_codec *priv, unsigned int shutter,dev_warn (priv->dev,"Manual shutters, notifications not available\n" );return 0 ;case 0x2 : - open_irq =CS42L43_GPIO1_RISE ; - close_irq =CS42L43_GPIO1_FALL ; + *open_irq =CS42L43_GPIO1_RISE ; + *close_irq =CS42L43_GPIO1_FALL ;break ;case 0x4 : - open_irq =CS42L43_GPIO2_RISE ; - close_irq =CS42L43_GPIO2_FALL ; + *open_irq =CS42L43_GPIO2_RISE ; + *close_irq =CS42L43_GPIO2_FALL ;break ;case 0x8 : - open_irq =CS42L43_GPIO3_RISE ; - close_irq =CS42L43_GPIO3_FALL ; + *open_irq =CS42L43_GPIO3_RISE ; + *close_irq =CS42L43_GPIO3_FALL ;break ;default :return 0 ; } - ret =cs42l43_request_irq (priv, close_name, close_irq, handler,IRQF_SHARED ); + ret =cs42l43_request_irq (priv, close_name, *close_irq, handler,IRQF_SHARED );if (ret)return ret; -return cs42l43_request_irq (priv, open_name, open_irq, handler,IRQF_SHARED ); +return cs42l43_request_irq (priv, open_name, *open_irq, handler,IRQF_SHARED ); }static int cs42l43_codec_probe (struct platform_device *pdev)
@@ -2325,14 +2342,16 @@ static int cs42l43_codec_probe(struct platform_device *pdev) } ret =cs42l43_shutter_irq (priv, val &CS42L43_MIC_SHUTTER_CFG_MASK , -"mic shutter open" ,"mic shutter close" , +"mic shutter open" , &priv->shutter_irqs[0 ], +"mic shutter close" , &priv->shutter_irqs[1 ], cs42l43_mic_shutter);if (ret) goto err_pm; ret =cs42l43_shutter_irq (priv, (val &CS42L43_SPK_SHUTTER_CFG_MASK ) >>CS42L43_SPK_SHUTTER_CFG_SHIFT , -"spk shutter open" ,"spk shutter close" , +"spk shutter open" , &priv->shutter_irqs[2 ], +"spk shutter close" , &priv->shutter_irqs[3 ], cs42l43_spk_shutter);if (ret) goto err_pm;
@@ -2386,19 +2405,48 @@ static int cs42l43_codec_runtime_resume(struct device *dev)static int cs42l43_codec_suspend (struct device *dev) {struct cs42l43_codec *priv =dev_get_drvdata (dev); +int i;dev_dbg (priv->dev,"System suspend\n" ); priv->suspend_jack_debounce = true; -pm_runtime_force_suspend (dev); +for (i =0 ; i <ARRAY_SIZE (cs42l43_irqs); i++) +cs42l43_disable_irq (priv, cs42l43_irqs[i].irq); + +for (i =0 ; i <ARRAY_SIZE (priv->shutter_irqs); i++) +if (priv->shutter_irqs[i]) +cs42l43_disable_irq (priv, priv->shutter_irqs[i]); + +cancel_delayed_work_sync (&priv->bias_sense_timeout); +cancel_delayed_work_sync (&priv->tip_sense_work); +cancel_delayed_work_sync (&priv->hp_ilimit_clear_work); + +return pm_runtime_force_suspend (dev); +} + +static int cs42l43_codec_resume (struct device *dev) +{ +struct cs42l43_codec *priv =dev_get_drvdata (dev); +int ret, i; + + ret =pm_runtime_force_resume (dev); +if (ret) +return ret; + +for (i =0 ; i <ARRAY_SIZE (cs42l43_irqs); i++) +cs42l43_enable_irq (priv, cs42l43_irqs[i].irq); + +for (i =0 ; i <ARRAY_SIZE (priv->shutter_irqs); i++) +if (priv->shutter_irqs[i]) +cs42l43_enable_irq (priv, priv->shutter_irqs[i]);return 0 ; }static const struct dev_pm_ops cs42l43_codec_pm_ops = { RUNTIME_PM_OPS(NULL, cs42l43_codec_runtime_resume, NULL) -SYSTEM_SLEEP_PM_OPS (cs42l43_codec_suspend, pm_runtime_force_resume) +SYSTEM_SLEEP_PM_OPS (cs42l43_codec_suspend, cs42l43_codec_resume) };static const struct platform_device_id cs42l43_codec_id_table[] = {
sound/soc/codecs/cs42l43.h +1 -0
@@ -45,6 +45,7 @@ struct cs42l43_codec {struct cs42l43 *core;struct snd_soc_component *component;struct irq_domain *dom; +unsigned int shutter_irqs[4 ];struct clk *mclk;
Signature · 1 line
2.47.2
PATCH 4/6 ASoC: cs42l43: Shutdown jack detection on suspend
In reply to Charles KeepaxFully power down the jack detection on system suspend since the device will not be powered up during.
--- sound/soc/codecs/cs42l43-jack.c | 8 +++++--- sound/soc/codecs/cs42l43.c | 2 ++ sound/soc/codecs/cs42l43.h | 1 + 3 files changed, 8 insertions(+), 3 deletions(-)
sound/soc/codecs/cs42l43-jack.c +5 -3
@@ -684,7 +684,7 @@ static int cs42l43_run_type_detect(struct cs42l43_codec *priv) } } -static void cs42l43_clear_jack (struct cs42l43_codec *priv) +void cs42l43_clear_jack (struct cs42l43_codec *priv) {struct cs42l43 *cs42l43 = priv->core;
@@ -703,8 +703,6 @@ static void cs42l43_clear_jack(struct cs42l43_codec *priv)regmap_update_bits (cs42l43->regmap,CS42L43_HS2 ,CS42L43_HSDET_MODE_MASK |CS42L43_HSDET_MANUAL_MODE_MASK ,0x2 <<CS42L43_HSDET_MODE_SHIFT ); - -snd_soc_jack_report (priv->jack_hp,0 ,0xFFFF ); }void cs42l43_tip_sense_work (struct work_struct *work)
@@ -753,6 +751,8 @@ void cs42l43_tip_sense_work(struct work_struct *work)cs42l43_clear_jack (priv); +snd_soc_jack_report (priv->jack_hp,0 ,0xFFFF ); +if (cs42l43->sdw && priv->jack_present) {pm_runtime_put (priv->dev); priv->jack_present = false;
@@ -903,6 +903,8 @@ int cs42l43_jack_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucs42l43_clear_jack (priv); +snd_soc_jack_report (priv->jack_hp,0 ,0xFFFF ); +if (!override) {queue_delayed_work (system_long_wq, &priv->tip_sense_work,0 ); }else {
sound/soc/codecs/cs42l43.c +2 -0
@@ -2422,6 +2422,8 @@ static int cs42l43_codec_suspend(struct device *dev)cancel_delayed_work_sync (&priv->tip_sense_work);cancel_delayed_work_sync (&priv->hp_ilimit_clear_work); +cs42l43_clear_jack (priv); +return pm_runtime_force_suspend (dev); }
sound/soc/codecs/cs42l43.h +1 -0
@@ -132,6 +132,7 @@ static inline int cs42l43_sdw_add_peripheral(struct snd_pcm_substream *substreamint cs42l43_set_jack (struct snd_soc_component *component,struct snd_soc_jack *jack,void *d);void cs42l43_bias_sense_timeout (struct work_struct *work); +void cs42l43_clear_jack (struct cs42l43_codec *priv);void cs42l43_tip_sense_work (struct work_struct *work);irqreturn_t cs42l43_bias_detect_clamp (int irq,void *data);irqreturn_t cs42l43_button_press (int irq,void *data);
Signature · 1 line
2.47.2
PATCH 5/6 mfd: cs42l43: Move IRQ enable/disable to encompass force suspend
In reply to Charles KeepaxAs pm_runtime_force_suspend() will force the device state to suspend, the driver needs to ensure no IRQ handlers are currently running. If not those handlers may find they are now running on suspended hardware despite holding a PM runtime reference. disable_irq() will sync any currently running handlers, so move the IRQ disabling to cover the whole of the forced suspend state to avoid such race conditions.
--- drivers/mfd/cs42l43.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/mfd/cs42l43.c +4 -4
@@ -1151,6 +1151,8 @@ static int cs42l43_suspend(struct device *dev)return ret; } +disable_irq (cs42l43->irq); + ret =pm_runtime_force_suspend (dev);if (ret) {dev_err (cs42l43->dev,"Failed to force suspend: %d\n" , ret);
@@ -1164,8 +1166,6 @@ static int cs42l43_suspend(struct device *dev)if (ret)return ret; -disable_irq (cs42l43->irq); -return 0 ; }
@@ -1196,14 +1196,14 @@ static int cs42l43_resume(struct device *dev)if (ret)return ret; -enable_irq (cs42l43->irq); - ret =pm_runtime_force_resume (dev);if (ret) {dev_err (cs42l43->dev,"Failed to force resume: %d\n" , ret);return ret; } +enable_irq (cs42l43->irq); +return 0 ; }
Signature · 1 line
2.47.2
PATCH 6/6 mfd: cs42l43: Remove IRQ masking in suspend
In reply to Charles KeepaxNow the individual child drivers mask their own IRQs there is no need for the MFD code to do so anymore.
--- drivers/mfd/cs42l43.c | 26 -------------------------- 1 file changed, 26 deletions(-)
drivers/mfd/cs42l43.c +0 -26
@@ -1117,24 +1117,6 @@ EXPORT_SYMBOL_NS_GPL(cs42l43_dev_probe, "MFD_CS42L43");static int cs42l43_suspend (struct device *dev) {struct cs42l43 *cs42l43 =dev_get_drvdata (dev); -static const struct reg_sequence mask_all[] = { - {CS42L43_DECIM_MASK ,0xFFFFFFFF , }, - {CS42L43_EQ_MIX_MASK ,0xFFFFFFFF , }, - {CS42L43_ASP_MASK ,0xFFFFFFFF , }, - {CS42L43_PLL_MASK ,0xFFFFFFFF , }, - {CS42L43_SOFT_MASK ,0xFFFFFFFF , }, - {CS42L43_SWIRE_MASK ,0xFFFFFFFF , }, - {CS42L43_MSM_MASK ,0xFFFFFFFF , }, - {CS42L43_ACC_DET_MASK ,0xFFFFFFFF , }, - {CS42L43_I2C_TGT_MASK ,0xFFFFFFFF , }, - {CS42L43_SPI_MSTR_MASK ,0xFFFFFFFF , }, - {CS42L43_SW_TO_SPI_BRIDGE_MASK ,0xFFFFFFFF , }, - {CS42L43_OTP_MASK ,0xFFFFFFFF , }, - {CS42L43_CLASS_D_AMP_MASK ,0xFFFFFFFF , }, - {CS42L43_GPIO_INT_MASK ,0xFFFFFFFF , }, - {CS42L43_ASRC_MASK ,0xFFFFFFFF , }, - {CS42L43_HPOUT_MASK ,0xFFFFFFFF , }, - };int ret; ret =pm_runtime_resume_and_get (dev);
@@ -1143,14 +1125,6 @@ static int cs42l43_suspend(struct device *dev)return ret; } -/* The IRQs will be re-enabled on resume by the cache sync */ - ret =regmap_multi_reg_write_bypassed (cs42l43->regmap, - mask_all,ARRAY_SIZE (mask_all)); -if (ret) { -dev_err (cs42l43->dev,"Failed to mask IRQs: %d\n" , ret); -return ret; - } -disable_irq (cs42l43->irq); ret =pm_runtime_force_suspend (dev);
Signature · 1 line
2.47.2
Mark Brown
maintainerRe: (subset) [PATCH 0/6] Improve cs42l43 suspend/IRQ interactions
In reply to Charles KeepaxQuoted 8 lines · Charles Keepax
cs42l43 uses
pm_runtime_force_suspend()during system suspend, however this means care must be taken that IRQ handler code isn't running when entering system suspend as force suspend will ignore that the handler is holding a pm reference. Typically the result of this is just a few error messages, but better to improve the handling and ensure that all IRQ processing is synchronised in before system suspend.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next
Thanks!
[1/6] ASoC: cs42l43: Rename system suspend callback and fix debug print
commit: 638ca7601f41a3f8368811f3185b06e2547b7a0f
[2/6] ASoC: cs42l43: Store IRQ domain in codec private data
commit: 149dda5f42a8fa6dacf2cff1d16952de28622d30
[3/6] ASoC: cs42l43: Disable IRQs in system suspend
commit: a69b4ba19a07896e7e4246446bad002f5fc0dae1
[4/6] ASoC: cs42l43: Shutdown jack detection on suspend
commit: dd7ae5b8b3c291c0206f127a564ae1e316705ca0All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark
Re: (subset) [PATCH 0/6] Improve cs42l43 suspend/IRQ interactions
In reply to Charles KeepaxQuoted 8 lines · Charles Keepax
cs42l43 uses
pm_runtime_force_suspend()during system suspend, however this means care must be taken that IRQ handler code isn't running when entering system suspend as force suspend will ignore that the handler is holding a pm reference. Typically the result of this is just a few error messages, but better to improve the handling and ensure that all IRQ processing is synchronised in before system suspend.[...]
Applied, thanks!
[5/6] mfd: cs42l43: Move IRQ enable/disable to encompass force suspend
commit: b3f893e969b6e8bb80bb2307c509ef1619e1c3fa
[6/6] mfd: cs42l43: Remove IRQ masking in suspend
commit: d614c669a205f39ca8be545d5d829a6b9f3cf61c--
Lee Jones [李琼斯]