alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] leds: trigger: audio: Add audio jack plugging indicator led
@ 2020-09-14  8:06 Hui Wang
  2020-09-14  8:06 ` [PATCH 2/2] ALSA: hda/realtek - enable jack plugging indicator led on LENOVO P520 Hui Wang
  2020-09-15  0:40 ` [PATCH 1/2] leds: trigger: audio: Add audio jack plugging indicator led Hui Wang
  0 siblings, 2 replies; 3+ messages in thread
From: Hui Wang @ 2020-09-14  8:06 UTC (permalink / raw)
  To: alsa-devel, tiwai, pavel; +Cc: kai.heng.feng, linux-leds

On the Lenovo P520 front panel, there is a headset plugging indicator
led. According to LENOVO's requirement, this led should be on if the
front headset audio jack is plugged, otherwise the led should be off.

We tested with windows, if we plugged a headphone/headset in that
jack, the led was on, if we removed the headphone/headset from that
jack, the led was off.

Here we add JACKPLUG led in the ledtrig_audio, then we could call
ledtrig_audio_set() in the alsa driver to control that led.

Signed-off-by: Hui Wang <hui.wang@canonical.com>
---
 drivers/leds/trigger/Kconfig         | 4 ++--
 drivers/leds/trigger/ledtrig-audio.c | 7 +++++--
 include/linux/leds.h                 | 1 +
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
index ce9429ca6dde..a61893a6e3e4 100644
--- a/drivers/leds/trigger/Kconfig
+++ b/drivers/leds/trigger/Kconfig
@@ -138,10 +138,10 @@ config LEDS_TRIGGER_PATTERN
 	  If unsure, say N
 
 config LEDS_TRIGGER_AUDIO
-	tristate "Audio Mute LED Trigger"
+	tristate "Audio Mute and Jack Plugging LED Trigger"
 	help
 	  This allows LEDs to be controlled by audio drivers for following
-	  the audio mute and mic-mute changes.
+	  the audio mute, mic-mute and jack plugging changes.
 	  If unsure, say N
 
 endif # LEDS_TRIGGERS
diff --git a/drivers/leds/trigger/ledtrig-audio.c b/drivers/leds/trigger/ledtrig-audio.c
index f76621e88482..fa66bb982400 100644
--- a/drivers/leds/trigger/ledtrig-audio.c
+++ b/drivers/leds/trigger/ledtrig-audio.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 //
-// Audio Mute LED trigger
+// Audio Mute And Jack Plugging LED trigger
 //
 
 #include <linux/kernel.h>
@@ -29,6 +29,8 @@ static int __init ledtrig_audio_init(void)
 				    &ledtrig_audio[LED_AUDIO_MUTE]);
 	led_trigger_register_simple("audio-micmute",
 				    &ledtrig_audio[LED_AUDIO_MICMUTE]);
+	led_trigger_register_simple("audio-jackplug",
+				    &ledtrig_audio[LED_AUDIO_JACKPLUG]);
 	return 0;
 }
 module_init(ledtrig_audio_init);
@@ -37,8 +39,9 @@ static void __exit ledtrig_audio_exit(void)
 {
 	led_trigger_unregister_simple(ledtrig_audio[LED_AUDIO_MUTE]);
 	led_trigger_unregister_simple(ledtrig_audio[LED_AUDIO_MICMUTE]);
+	led_trigger_unregister_simple(ledtrig_audio[LED_AUDIO_JACKPLUG]);
 }
 module_exit(ledtrig_audio_exit);
 
-MODULE_DESCRIPTION("LED trigger for audio mute control");
+MODULE_DESCRIPTION("LED trigger for audio mute and jack plugging control");
 MODULE_LICENSE("GPL v2");
diff --git a/include/linux/leds.h b/include/linux/leds.h
index 6a8d6409c993..1858c2d30ecd 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -584,6 +584,7 @@ struct led_pattern {
 enum led_audio {
 	LED_AUDIO_MUTE,		/* master mute LED */
 	LED_AUDIO_MICMUTE,	/* mic mute LED */
+	LED_AUDIO_JACKPLUG,	/* audio jack plug indicator LED */
 	NUM_AUDIO_LEDS
 };
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] ALSA: hda/realtek - enable jack plugging indicator led on LENOVO P520
  2020-09-14  8:06 [PATCH 1/2] leds: trigger: audio: Add audio jack plugging indicator led Hui Wang
@ 2020-09-14  8:06 ` Hui Wang
  2020-09-15  0:40 ` [PATCH 1/2] leds: trigger: audio: Add audio jack plugging indicator led Hui Wang
  1 sibling, 0 replies; 3+ messages in thread
From: Hui Wang @ 2020-09-14  8:06 UTC (permalink / raw)
  To: alsa-devel, tiwai, pavel; +Cc: kai.heng.feng, linux-leds

There is a headset jack plugging indicator led on the LENOVO P520
front panel, we expect this led to be on if we plug a headphone or
headset in that jack, and expect this led to be off if there is
nothing plugged in that jack.

After adding the JACKPLUG led in the ledtrig_audio.c, we could add
led control in the hda audio driver via ledtrig API.

On the LENOVO P520, the jack indicator led connects to the alc233
gpio2, we hook a callback in the unsol event handler to update this
led.

Signed-off-by: Hui Wang <hui.wang@canonical.com>
---
 sound/pci/hda/hda_generic.c   | 26 ++++++++++++++++++++++
 sound/pci/hda/hda_generic.h   |  3 +++
 sound/pci/hda/patch_realtek.c | 42 +++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)

diff --git a/sound/pci/hda/hda_generic.c b/sound/pci/hda/hda_generic.c
index bbb17481159e..6b4b06583e98 100644
--- a/sound/pci/hda/hda_generic.c
+++ b/sound/pci/hda/hda_generic.c
@@ -3888,6 +3888,32 @@ static int parse_mic_boost(struct hda_codec *codec)
 }
 
 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
+/**
+ * snd_dha_gen_add_jackplug_led_cdev - Create a LED classdev and enable as jack plugging LED
+ * @codec: the HDA codec
+ * @callback: the callback for LED classdev brightness_set_blocking
+ */
+int snd_hda_gen_add_jackplug_led_cdev(struct hda_codec *codec,
+				      int (*callback)(struct led_classdev *,
+						      enum led_brightness))
+{
+	struct led_classdev *cdev;
+
+	cdev = devm_kzalloc(&codec->core.dev, sizeof(*cdev), GFP_KERNEL);
+	if (!cdev)
+		return -ENOMEM;
+
+	cdev->name = "hda::jackplug";
+	cdev->max_brightness = 1;
+	cdev->default_trigger = "audio-jackplug";
+	cdev->brightness_set_blocking = callback;
+	cdev->brightness = ledtrig_audio_get(LED_AUDIO_JACKPLUG);
+	cdev->flags = LED_CORE_SUSPENDRESUME;
+
+	return devm_led_classdev_register(&codec->core.dev, cdev);
+}
+EXPORT_SYMBOL_GPL(snd_hda_gen_add_jackplug_led_cdev);
+
 /*
  * vmaster mute LED hook helpers
  */
diff --git a/sound/pci/hda/hda_generic.h b/sound/pci/hda/hda_generic.h
index a43f0bb77dae..8d363d14d0c3 100644
--- a/sound/pci/hda/hda_generic.h
+++ b/sound/pci/hda/hda_generic.h
@@ -360,5 +360,8 @@ int snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
 int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
 				     int (*callback)(struct led_classdev *,
 						     enum led_brightness));
+int snd_hda_gen_add_jackplug_led_cdev(struct hda_codec *codec,
+				      int (*callback)(struct led_classdev *,
+						      enum led_brightness));
 
 #endif /* __SOUND_HDA_GENERIC_H */
diff --git a/sound/pci/hda/patch_realtek.c b/sound/pci/hda/patch_realtek.c
index b6dc47da1d7b..8dbc7ef76206 100644
--- a/sound/pci/hda/patch_realtek.c
+++ b/sound/pci/hda/patch_realtek.c
@@ -90,11 +90,13 @@ struct alc_spec {
 	/* mute LED for HP laptops, see vref_mute_led_set() */
 	int mute_led_polarity;
 	int micmute_led_polarity;
+	int jackplug_led_polarity;
 	hda_nid_t mute_led_nid;
 	hda_nid_t cap_mute_led_nid;
 
 	unsigned int gpio_mute_led_mask;
 	unsigned int gpio_mic_led_mask;
+	unsigned int gpio_jackplug_led_mask;
 	struct alc_coef_led mute_led_coef;
 	struct alc_coef_led mic_led_coef;
 
@@ -5905,15 +5907,50 @@ static void alc285_fixup_thinkpad_x1_gen7(struct hda_codec *codec,
 	}
 }
 
+/* turn on/off jackplugging indicator LED via GPIO per unsol event callback */
+static int gpio_jackplug_led_set(struct led_classdev *led_cdev,
+				 enum led_brightness brightness)
+{
+	struct hda_codec *codec = dev_to_hda_codec(led_cdev->dev->parent);
+	struct alc_spec *spec = codec->spec;
+
+	alc_update_gpio_led(codec, spec->gpio_jackplug_led_mask,
+			    spec->jackplug_led_polarity, !brightness);
+	return 0;
+}
+
+static void alc_update_hp_jack_stat_led(struct hda_codec *codec,
+					struct hda_jack_callback *cb)
+{
+	u32 pin_sense;
+
+	pin_sense = snd_hda_jack_pin_sense(codec, 0x21, 0);
+	ledtrig_audio_set(LED_AUDIO_JACKPLUG,
+			  (pin_sense & AC_PINSENSE_PRESENCE) ? LED_ON : LED_OFF);
+}
+
 static void alc233_alc662_fixup_lenovo_dual_codecs(struct hda_codec *codec,
 					 const struct hda_fixup *fix,
 					 int action)
 {
+	struct alc_spec *spec = codec->spec;
 	alc_fixup_dual_codecs(codec, fix, action);
 	switch (action) {
 	case HDA_FIXUP_ACT_PRE_PROBE:
 		/* override card longname to provide a unique UCM profile */
 		strcpy(codec->card->longname, "HDAudio-Lenovo-DualCodecs");
+
+		/* the jack plug indicator led connects to gpio2 of alc233 */
+		if (codec->core.vendor_id == 0x10ec0662)
+			break;
+		/* enable gpio2 and set it to output */
+		spec->gpio_mask |= 0x2;
+		spec->gpio_dir |= 0x2;
+		alc_write_gpio(codec);
+		spec->gpio_jackplug_led_mask = 0x2;
+		spec->jackplug_led_polarity = 0x0;
+		snd_hda_gen_add_jackplug_led_cdev(codec, gpio_jackplug_led_set);
+
 		break;
 	case HDA_FIXUP_ACT_BUILD:
 		/* rename Capture controls depending on the codec */
@@ -5925,6 +5962,11 @@ static void alc233_alc662_fixup_lenovo_dual_codecs(struct hda_codec *codec,
 			   codec->addr == 0 ?
 			   "Rear-Panel Capture Switch" :
 			   "Front-Panel Capture Switch");
+
+		if (codec->core.vendor_id == 0x10ec0662)
+			break;
+		alc_update_hp_jack_stat_led(codec, NULL);
+		snd_hda_jack_detect_enable_callback(codec, 0x21, alc_update_hp_jack_stat_led);
 		break;
 	}
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] leds: trigger: audio: Add audio jack plugging indicator led
  2020-09-14  8:06 [PATCH 1/2] leds: trigger: audio: Add audio jack plugging indicator led Hui Wang
  2020-09-14  8:06 ` [PATCH 2/2] ALSA: hda/realtek - enable jack plugging indicator led on LENOVO P520 Hui Wang
@ 2020-09-15  0:40 ` Hui Wang
  1 sibling, 0 replies; 3+ messages in thread
From: Hui Wang @ 2020-09-15  0:40 UTC (permalink / raw)
  To: alsa-devel, tiwai, pavel; +Cc: kai.heng.feng, linux-leds

Please ignore this patchset,  our QA retested this led under Windows, 
the led was always on after booting no matter users plug in or plug out.

If the led could change under Windows in future, I will resend the patchset.

Thanks,

Hui.


On 2020/9/14 下午4:06, Hui Wang wrote:
> On the Lenovo P520 front panel, there is a headset plugging indicator
> led. According to LENOVO's requirement, this led should be on if the
> front headset audio jack is plugged, otherwise the led should be off.
>
> We tested with windows, if we plugged a headphone/headset in that
> jack, the led was on, if we removed the headphone/headset from that
> jack, the led was off.
>
> Here we add JACKPLUG led in the ledtrig_audio, then we could call
> ledtrig_audio_set() in the alsa driver to control that led.
>
> Signed-off-by: Hui Wang <hui.wang@canonical.com>
> ---
>   drivers/leds/trigger/Kconfig         | 4 ++--
>   drivers/leds/trigger/ledtrig-audio.c | 7 +++++--
>   include/linux/leds.h                 | 1 +
>   3 files changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
> index ce9429ca6dde..a61893a6e3e4 100644
> --- a/drivers/leds/trigger/Kconfig
> +++ b/drivers/leds/trigger/Kconfig
> @@ -138,10 +138,10 @@ config LEDS_TRIGGER_PATTERN
>   	  If unsure, say N
>   
>   config LEDS_TRIGGER_AUDIO
> -	tristate "Audio Mute LED Trigger"
> +	tristate "Audio Mute and Jack Plugging LED Trigger"
>   	help
>   	  This allows LEDs to be controlled by audio drivers for following
> -	  the audio mute and mic-mute changes.
> +	  the audio mute, mic-mute and jack plugging changes.
>   	  If unsure, say N
>   
>   endif # LEDS_TRIGGERS
> diff --git a/drivers/leds/trigger/ledtrig-audio.c b/drivers/leds/trigger/ledtrig-audio.c
> index f76621e88482..fa66bb982400 100644
> --- a/drivers/leds/trigger/ledtrig-audio.c
> +++ b/drivers/leds/trigger/ledtrig-audio.c
> @@ -1,6 +1,6 @@
>   // SPDX-License-Identifier: GPL-2.0
>   //
> -// Audio Mute LED trigger
> +// Audio Mute And Jack Plugging LED trigger
>   //
>   
>   #include <linux/kernel.h>
> @@ -29,6 +29,8 @@ static int __init ledtrig_audio_init(void)
>   				    &ledtrig_audio[LED_AUDIO_MUTE]);
>   	led_trigger_register_simple("audio-micmute",
>   				    &ledtrig_audio[LED_AUDIO_MICMUTE]);
> +	led_trigger_register_simple("audio-jackplug",
> +				    &ledtrig_audio[LED_AUDIO_JACKPLUG]);
>   	return 0;
>   }
>   module_init(ledtrig_audio_init);
> @@ -37,8 +39,9 @@ static void __exit ledtrig_audio_exit(void)
>   {
>   	led_trigger_unregister_simple(ledtrig_audio[LED_AUDIO_MUTE]);
>   	led_trigger_unregister_simple(ledtrig_audio[LED_AUDIO_MICMUTE]);
> +	led_trigger_unregister_simple(ledtrig_audio[LED_AUDIO_JACKPLUG]);
>   }
>   module_exit(ledtrig_audio_exit);
>   
> -MODULE_DESCRIPTION("LED trigger for audio mute control");
> +MODULE_DESCRIPTION("LED trigger for audio mute and jack plugging control");
>   MODULE_LICENSE("GPL v2");
> diff --git a/include/linux/leds.h b/include/linux/leds.h
> index 6a8d6409c993..1858c2d30ecd 100644
> --- a/include/linux/leds.h
> +++ b/include/linux/leds.h
> @@ -584,6 +584,7 @@ struct led_pattern {
>   enum led_audio {
>   	LED_AUDIO_MUTE,		/* master mute LED */
>   	LED_AUDIO_MICMUTE,	/* mic mute LED */
> +	LED_AUDIO_JACKPLUG,	/* audio jack plug indicator LED */
>   	NUM_AUDIO_LEDS
>   };
>   

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-15  0:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-14  8:06 [PATCH 1/2] leds: trigger: audio: Add audio jack plugging indicator led Hui Wang
2020-09-14  8:06 ` [PATCH 2/2] ALSA: hda/realtek - enable jack plugging indicator led on LENOVO P520 Hui Wang
2020-09-15  0:40 ` [PATCH 1/2] leds: trigger: audio: Add audio jack plugging indicator led Hui Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).