[PATCH 2/7] ASoC: hdmi-codec: use HDMI state notifier to add jack support

From: Cheng-Yi Chiang
Date: Mon Jun 03 2019 - 00:37:21 EST


From: Philipp Zabel <p.zabel@xxxxxxxxxxxxxx>

Use HDMI connection / disconnection notifications to update an ALSA
jack object. Also make a copy of the ELD block after every change.

This was posted by Philipp Zabel at

https://patchwork.kernel.org/patch/9430747/

Modified by Cheng-Yi Chiang:
- Fix the conflict of removed hdmi_codec_remove ops.
- Other minor fix for the conflict with latest hdmi-codec on ASoC
for-next tree.

Signed-off-by: Philipp Zabel <p.zabel@xxxxxxxxxxxxxx>
Signed-off-by: Cheng-Yi Chiang <cychiang@xxxxxxxxxxxx>
---
The original patch is at https://patchwork.kernel.org/patch/9430747/
I could not find the LKML link for the patch.

include/sound/hdmi-codec.h | 7 +++
sound/soc/codecs/Kconfig | 1 +
sound/soc/codecs/hdmi-codec.c | 104 +++++++++++++++++++++++++++++++++-
3 files changed, 110 insertions(+), 2 deletions(-)

diff --git a/include/sound/hdmi-codec.h b/include/sound/hdmi-codec.h
index 9483c55f871b..4fa39c93363f 100644
--- a/include/sound/hdmi-codec.h
+++ b/include/sound/hdmi-codec.h
@@ -107,6 +107,13 @@ struct hdmi_codec_pdata {
void *data;
};

+struct snd_soc_component;
+struct snd_soc_jack;
+
+int hdmi_codec_set_jack_detect(struct snd_soc_component *component,
+ struct snd_soc_jack *jack,
+ struct device *dev);
+
#define HDMI_CODEC_DRV_NAME "hdmi-audio-codec"

#endif /* __HDMI_CODEC_H__ */
diff --git a/sound/soc/codecs/Kconfig b/sound/soc/codecs/Kconfig
index 8f577258080b..f5f6dd04234c 100644
--- a/sound/soc/codecs/Kconfig
+++ b/sound/soc/codecs/Kconfig
@@ -639,6 +639,7 @@ config SND_SOC_HDMI_CODEC
select SND_PCM_ELD
select SND_PCM_IEC958
select HDMI
+ select HDMI_NOTIFIERS

config SND_SOC_ES7134
tristate "Everest Semi ES7134 CODEC"
diff --git a/sound/soc/codecs/hdmi-codec.c b/sound/soc/codecs/hdmi-codec.c
index 6a0cc8d7e141..fe796a7475a5 100644
--- a/sound/soc/codecs/hdmi-codec.c
+++ b/sound/soc/codecs/hdmi-codec.c
@@ -12,9 +12,12 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
+#include <linux/hdmi-notifier.h>
#include <linux/module.h>
+#include <linux/notifier.h>
#include <linux/string.h>
#include <sound/core.h>
+#include <sound/jack.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
@@ -282,6 +285,13 @@ struct hdmi_codec_priv {
struct snd_pcm_chmap *chmap_info;
unsigned int chmap_idx;
struct mutex lock;
+ struct snd_soc_jack *jack;
+ /* Lock to protect setting and getting eld. */
+ struct mutex eld_lock;
+ struct device *dev;
+ struct hdmi_notifier *notifier;
+ struct notifier_block nb;
+ unsigned int jack_status;
};

static const struct snd_soc_dapm_widget hdmi_widgets[] = {
@@ -308,7 +318,9 @@ static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);

+ mutex_lock(&hcp->eld_lock);
memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
+ mutex_unlock(&hcp->eld_lock);

return 0;
}
@@ -393,7 +405,7 @@ static int hdmi_codec_startup(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
- int ret = 0;
+ int ret;

ret = mutex_trylock(&hcp->lock);
if (!ret) {
@@ -408,9 +420,9 @@ static int hdmi_codec_startup(struct snd_pcm_substream *substream,
}

if (hcp->hcd.ops->get_eld) {
+ mutex_lock(&hcp->eld_lock);
ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
hcp->eld, sizeof(hcp->eld));
-
if (!ret) {
ret = snd_pcm_hw_constraint_eld(substream->runtime,
hcp->eld);
@@ -419,6 +431,7 @@ static int hdmi_codec_startup(struct snd_pcm_substream *substream,
}
/* Select chmap supported */
hdmi_codec_eld_chmap(hcp);
+ mutex_unlock(&hcp->eld_lock);
}
return 0;

@@ -747,6 +760,77 @@ static const struct snd_soc_component_driver hdmi_driver = {
.non_legacy_dai_naming = 1,
};

+static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
+ unsigned int jack_status)
+{
+ if (!hcp->jack)
+ return;
+
+ if (jack_status != hcp->jack_status) {
+ snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_LINEOUT);
+ hcp->jack_status = jack_status;
+ }
+}
+
+static int hdmi_codec_notify(struct notifier_block *nb, unsigned long event,
+ void *data)
+{
+ struct hdmi_codec_priv *hcp = container_of(nb, struct hdmi_codec_priv,
+ nb);
+ struct hdmi_notifier *n = data;
+
+ if (!hcp->jack)
+ return NOTIFY_OK;
+
+ switch (event) {
+ case HDMI_NEW_ELD:
+ mutex_lock(&hcp->eld_lock);
+ memcpy(hcp->eld, n->eld, sizeof(hcp->eld));
+ mutex_unlock(&hcp->eld_lock);
+ /* fall through */
+ case HDMI_CONNECTED:
+ hdmi_codec_jack_report(hcp, SND_JACK_LINEOUT);
+ break;
+ case HDMI_DISCONNECTED:
+ hdmi_codec_jack_report(hcp, 0);
+ break;
+ }
+
+ return NOTIFY_OK;
+}
+
+/**
+ * hdmi_codec_set_jack_detect - register HDMI state notifier callback
+ * @component: the hdmi-codec instance
+ * @jack: ASoC jack to report (dis)connection events on
+ * @dev: hdmi_notifier device, usually HDMI_TX or CEC device
+ */
+int hdmi_codec_set_jack_detect(struct snd_soc_component *component,
+ struct snd_soc_jack *jack,
+ struct device *dev)
+{
+ struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
+ int ret;
+
+ hcp->notifier = hdmi_notifier_get(dev);
+ if (!hcp->notifier)
+ return -ENOMEM;
+
+ hcp->jack = jack;
+ hcp->nb.notifier_call = hdmi_codec_notify;
+ ret = hdmi_notifier_register(hcp->notifier, &hcp->nb);
+ if (ret)
+ goto err_notifier_put;
+
+ return 0;
+
+err_notifier_put:
+ hdmi_notifier_put(hcp->notifier);
+ hcp->notifier = NULL;
+ return ret;
+}
+EXPORT_SYMBOL_GPL(hdmi_codec_set_jack_detect);
+
static int hdmi_codec_probe(struct platform_device *pdev)
{
struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
@@ -774,6 +858,7 @@ static int hdmi_codec_probe(struct platform_device *pdev)

hcp->hcd = *hcd;
mutex_init(&hcp->lock);
+ mutex_init(&hcp->eld_lock);

daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
if (!daidrv)
@@ -797,6 +882,20 @@ static int hdmi_codec_probe(struct platform_device *pdev)
__func__, ret);
return ret;
}
+
+ hcp->dev = dev;
+
+ return 0;
+}
+
+static int hdmi_codec_remove(struct platform_device *pdev)
+{
+ struct hdmi_codec_priv *hcp = platform_get_drvdata(pdev);
+
+ if (hcp->notifier) {
+ hdmi_notifier_unregister(hcp->notifier, &hcp->nb);
+ hdmi_notifier_put(hcp->notifier);
+ }
return 0;
}

@@ -805,6 +904,7 @@ static struct platform_driver hdmi_codec_driver = {
.name = HDMI_CODEC_DRV_NAME,
},
.probe = hdmi_codec_probe,
+ .remove = hdmi_codec_remove,
};

module_platform_driver(hdmi_codec_driver);
--
2.22.0.rc1.257.g3120a18244-goog