diff mbox

[v4,4/5] ALSA: hda - hdmi dynamically bind PCM to pin when monitor hotplug

Message ID 1450244565-77041-5-git-send-email-libin.yang@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

libin.yang@linux.intel.com Dec. 16, 2015, 5:42 a.m. UTC
From: Libin Yang <libin.yang@linux.intel.com>

Dynamically bind/unbind the PCM to pin when HDMI/DP monitor hotplug.

When monitor is connected, find a proper PCM for the monitor.
When monitor is disconnected, unbind the PCM from the pin.

The binding policy (use Intel platform as example) is:
1. Try to use the legacy pin-pcm mapping for the device entry 0
   of the pin.
2. If step 1 fails, try to bind pin to the backup PCMs. For example,
   on Intel platform, if DP MST is enabled, 5 PCMs will be created.
   PCM 3, PCM 7, PCM 8 are supposed to be used by device entry 0 of
   pin 5, pin 6 and pin 7. PCM 9 and PCM 10 are the backup PCMs.
3. If step 2 fails, try to find any PCM to bind to the pin.

Signed-off-by: Libin Yang <libin.yang@linux.intel.com>
---
 sound/pci/hda/patch_hdmi.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

Comments

Takashi Iwai Dec. 16, 2015, 7:43 a.m. UTC | #1
On Wed, 16 Dec 2015 06:42:44 +0100,
libin.yang@linux.intel.com wrote:
> 
> From: Libin Yang <libin.yang@linux.intel.com>
> 
> Dynamically bind/unbind the PCM to pin when HDMI/DP monitor hotplug.
> 
> When monitor is connected, find a proper PCM for the monitor.
> When monitor is disconnected, unbind the PCM from the pin.
> 
> The binding policy (use Intel platform as example) is:
> 1. Try to use the legacy pin-pcm mapping for the device entry 0
>    of the pin.
> 2. If step 1 fails, try to bind pin to the backup PCMs. For example,
>    on Intel platform, if DP MST is enabled, 5 PCMs will be created.
>    PCM 3, PCM 7, PCM 8 are supposed to be used by device entry 0 of
>    pin 5, pin 6 and pin 7. PCM 9 and PCM 10 are the backup PCMs.
> 3. If step 2 fails, try to find any PCM to bind to the pin.
> 
> Signed-off-by: Libin Yang <libin.yang@linux.intel.com>

This patch adds the code in sync_eld_via_acomp(), i.e. it's handled
only with component.  As the dynamic HDA attach/detach itself is
agnostic to the component, I'd suggest to put this rather in
update_eld().

Also pcm_lock can be put in hdmi_present_sense() to wrap both
sync_eld_via_acomp() and hdmi_present_sense_via_verbs().


Takashi

> ---
>  sound/pci/hda/patch_hdmi.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 73 insertions(+)
> 
> diff --git a/sound/pci/hda/patch_hdmi.c b/sound/pci/hda/patch_hdmi.c
> index 19bf782..10edab8 100644
> --- a/sound/pci/hda/patch_hdmi.c
> +++ b/sound/pci/hda/patch_hdmi.c
> @@ -73,6 +73,8 @@ struct hdmi_spec_per_cvt {
>  
>  struct hdmi_spec_per_pin {
>  	hda_nid_t pin_nid;
> +	/* pin idx, different device entries on the same pin use the same idx */
> +	int pin_nid_idx;
>  	int num_mux_nids;
>  	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
>  	int mux_idx;
> @@ -85,6 +87,7 @@ struct hdmi_spec_per_pin {
>  	struct snd_kcontrol *eld_ctl;
>  	struct snd_jack *acomp_jack; /* jack via audio component */
>  	struct hda_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
> +	int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
>  	int repoll_count;
>  	bool setup; /* the stream has been set up by prepare callback */
>  	int channels; /* current number of channels */
> @@ -138,6 +141,8 @@ struct hdmi_spec {
>  	struct snd_array pins; /* struct hdmi_spec_per_pin */
>  	struct hda_pcm *pcm_rec[16];
>  	struct mutex pcm_lock;
> +	/* pcm_bitmap means which pcms have been assigned to pins*/
> +	unsigned long pcm_bitmap;
>  	int pcm_used;	/* counter of pcm_rec[] */
>  	unsigned int channels_max; /* max over all cvts */
>  
> @@ -1786,6 +1791,60 @@ static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
>  	return ret;
>  }
>  
> +static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
> +				struct hdmi_spec_per_pin *per_pin)
> +{
> +	int i;
> +
> +	/* try the prefer PCM */
> +	if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
> +		return per_pin->pin_nid_idx;
> +
> +	/* have a second try; check the "reserved area" over num_pins */
> +	for (i = spec->num_pins; i < spec->pcm_used; i++) {
> +		if (!test_bit(i, &spec->pcm_bitmap))
> +			return i;
> +	}
> +
> +	/* the last try; check the empty slots in pins */
> +	for (i = 0; i < spec->num_pins; i++) {
> +		if (!test_bit(i, &spec->pcm_bitmap))
> +			return i;
> +	}
> +	return -EBUSY;
> +}
> +
> +static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
> +				struct hdmi_spec_per_pin *per_pin)
> +{
> +	int idx;
> +
> +	/* pcm already be attached to the pin */
> +	if (per_pin->pcm)
> +		return;
> +	idx = hdmi_find_pcm_slot(spec, per_pin);
> +	if (idx == -ENODEV)
> +		return;
> +	per_pin->pcm_idx = idx;
> +	per_pin->pcm = spec->pcm_rec[idx];
> +	set_bit(idx, &spec->pcm_bitmap);
> +}
> +
> +static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
> +				struct hdmi_spec_per_pin *per_pin)
> +{
> +	int idx;
> +
> +	/* pcm already be detached from the pin */
> +	if (!per_pin->pcm)
> +		return;
> +	idx = per_pin->pcm_idx;
> +	per_pin->pcm_idx = -1;
> +	per_pin->pcm = NULL;
> +	if (idx >= 0 && idx < spec->pcm_used)
> +		clear_bit(idx, &spec->pcm_bitmap);
> +}
> +
>  /* update ELD and jack state via audio component */
>  static void sync_eld_via_acomp(struct hda_codec *codec,
>  			       struct hdmi_spec_per_pin *per_pin)
> @@ -1794,6 +1853,7 @@ static void sync_eld_via_acomp(struct hda_codec *codec,
>  	struct hdmi_eld *eld = &spec->temp_eld;
>  	int size;
>  
> +	mutex_lock(&spec->pcm_lock);
>  	mutex_lock(&per_pin->lock);
>  	size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
>  				      &eld->monitor_present, eld->eld_buffer,
> @@ -1815,11 +1875,19 @@ static void sync_eld_via_acomp(struct hda_codec *codec,
>  		eld->eld_size = 0;
>  	}
>  
> +	if (spec->dyn_pcm_assign) {
> +		if (eld->eld_valid)
> +			hdmi_attach_hda_pcm(spec, per_pin);
> +		else
> +			hdmi_detach_hda_pcm(spec, per_pin);
> +	}
> +
>  	update_eld(codec, per_pin, eld);
>  	snd_jack_report(per_pin->acomp_jack,
>  			eld->monitor_present ? SND_JACK_AVOUT : 0);
>   unlock:
>  	mutex_unlock(&per_pin->lock);
> +	mutex_unlock(&spec->pcm_lock);
>  }
>  
>  static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
> @@ -1875,6 +1943,11 @@ static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
>  
>  	per_pin->pin_nid = pin_nid;
>  	per_pin->non_pcm = false;
> +	if (spec->dyn_pcm_assign)
> +		per_pin->pcm_idx = -1;
> +	else
> +		per_pin->pcm_idx = pin_idx;
> +	per_pin->pin_nid_idx = pin_idx;
>  
>  	err = hdmi_read_pin_conn(codec, pin_idx);
>  	if (err < 0)
> -- 
> 1.9.1
>
libin.yang@linux.intel.com Dec. 16, 2015, 7:47 a.m. UTC | #2
On 12/16/2015 03:43 PM, Takashi Iwai wrote:
> On Wed, 16 Dec 2015 06:42:44 +0100,
> libin.yang@linux.intel.com wrote:
>>
>> From: Libin Yang <libin.yang@linux.intel.com>
>>
>> Dynamically bind/unbind the PCM to pin when HDMI/DP monitor hotplug.
>>
>> When monitor is connected, find a proper PCM for the monitor.
>> When monitor is disconnected, unbind the PCM from the pin.
>>
>> The binding policy (use Intel platform as example) is:
>> 1. Try to use the legacy pin-pcm mapping for the device entry 0
>>     of the pin.
>> 2. If step 1 fails, try to bind pin to the backup PCMs. For example,
>>     on Intel platform, if DP MST is enabled, 5 PCMs will be created.
>>     PCM 3, PCM 7, PCM 8 are supposed to be used by device entry 0 of
>>     pin 5, pin 6 and pin 7. PCM 9 and PCM 10 are the backup PCMs.
>> 3. If step 2 fails, try to find any PCM to bind to the pin.
>>
>> Signed-off-by: Libin Yang <libin.yang@linux.intel.com>
>
> This patch adds the code in sync_eld_via_acomp(), i.e. it's handled
> only with component.  As the dynamic HDA attach/detach itself is
> agnostic to the component, I'd suggest to put this rather in
> update_eld().
>
> Also pcm_lock can be put in hdmi_present_sense() to wrap both
> sync_eld_via_acomp() and hdmi_present_sense_via_verbs().

OK, I will put it in update_eld().

Best Regards,
Libin

>
>
> Takashi
>
>> ---
>>   sound/pci/hda/patch_hdmi.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 73 insertions(+)
>>
>> diff --git a/sound/pci/hda/patch_hdmi.c b/sound/pci/hda/patch_hdmi.c
>> index 19bf782..10edab8 100644
>> --- a/sound/pci/hda/patch_hdmi.c
>> +++ b/sound/pci/hda/patch_hdmi.c
>> @@ -73,6 +73,8 @@ struct hdmi_spec_per_cvt {
>>
>>   struct hdmi_spec_per_pin {
>>   	hda_nid_t pin_nid;
>> +	/* pin idx, different device entries on the same pin use the same idx */
>> +	int pin_nid_idx;
>>   	int num_mux_nids;
>>   	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
>>   	int mux_idx;
>> @@ -85,6 +87,7 @@ struct hdmi_spec_per_pin {
>>   	struct snd_kcontrol *eld_ctl;
>>   	struct snd_jack *acomp_jack; /* jack via audio component */
>>   	struct hda_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
>> +	int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
>>   	int repoll_count;
>>   	bool setup; /* the stream has been set up by prepare callback */
>>   	int channels; /* current number of channels */
>> @@ -138,6 +141,8 @@ struct hdmi_spec {
>>   	struct snd_array pins; /* struct hdmi_spec_per_pin */
>>   	struct hda_pcm *pcm_rec[16];
>>   	struct mutex pcm_lock;
>> +	/* pcm_bitmap means which pcms have been assigned to pins*/
>> +	unsigned long pcm_bitmap;
>>   	int pcm_used;	/* counter of pcm_rec[] */
>>   	unsigned int channels_max; /* max over all cvts */
>>
>> @@ -1786,6 +1791,60 @@ static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
>>   	return ret;
>>   }
>>
>> +static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
>> +				struct hdmi_spec_per_pin *per_pin)
>> +{
>> +	int i;
>> +
>> +	/* try the prefer PCM */
>> +	if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
>> +		return per_pin->pin_nid_idx;
>> +
>> +	/* have a second try; check the "reserved area" over num_pins */
>> +	for (i = spec->num_pins; i < spec->pcm_used; i++) {
>> +		if (!test_bit(i, &spec->pcm_bitmap))
>> +			return i;
>> +	}
>> +
>> +	/* the last try; check the empty slots in pins */
>> +	for (i = 0; i < spec->num_pins; i++) {
>> +		if (!test_bit(i, &spec->pcm_bitmap))
>> +			return i;
>> +	}
>> +	return -EBUSY;
>> +}
>> +
>> +static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
>> +				struct hdmi_spec_per_pin *per_pin)
>> +{
>> +	int idx;
>> +
>> +	/* pcm already be attached to the pin */
>> +	if (per_pin->pcm)
>> +		return;
>> +	idx = hdmi_find_pcm_slot(spec, per_pin);
>> +	if (idx == -ENODEV)
>> +		return;
>> +	per_pin->pcm_idx = idx;
>> +	per_pin->pcm = spec->pcm_rec[idx];
>> +	set_bit(idx, &spec->pcm_bitmap);
>> +}
>> +
>> +static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
>> +				struct hdmi_spec_per_pin *per_pin)
>> +{
>> +	int idx;
>> +
>> +	/* pcm already be detached from the pin */
>> +	if (!per_pin->pcm)
>> +		return;
>> +	idx = per_pin->pcm_idx;
>> +	per_pin->pcm_idx = -1;
>> +	per_pin->pcm = NULL;
>> +	if (idx >= 0 && idx < spec->pcm_used)
>> +		clear_bit(idx, &spec->pcm_bitmap);
>> +}
>> +
>>   /* update ELD and jack state via audio component */
>>   static void sync_eld_via_acomp(struct hda_codec *codec,
>>   			       struct hdmi_spec_per_pin *per_pin)
>> @@ -1794,6 +1853,7 @@ static void sync_eld_via_acomp(struct hda_codec *codec,
>>   	struct hdmi_eld *eld = &spec->temp_eld;
>>   	int size;
>>
>> +	mutex_lock(&spec->pcm_lock);
>>   	mutex_lock(&per_pin->lock);
>>   	size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
>>   				      &eld->monitor_present, eld->eld_buffer,
>> @@ -1815,11 +1875,19 @@ static void sync_eld_via_acomp(struct hda_codec *codec,
>>   		eld->eld_size = 0;
>>   	}
>>
>> +	if (spec->dyn_pcm_assign) {
>> +		if (eld->eld_valid)
>> +			hdmi_attach_hda_pcm(spec, per_pin);
>> +		else
>> +			hdmi_detach_hda_pcm(spec, per_pin);
>> +	}
>> +
>>   	update_eld(codec, per_pin, eld);
>>   	snd_jack_report(per_pin->acomp_jack,
>>   			eld->monitor_present ? SND_JACK_AVOUT : 0);
>>    unlock:
>>   	mutex_unlock(&per_pin->lock);
>> +	mutex_unlock(&spec->pcm_lock);
>>   }
>>
>>   static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
>> @@ -1875,6 +1943,11 @@ static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
>>
>>   	per_pin->pin_nid = pin_nid;
>>   	per_pin->non_pcm = false;
>> +	if (spec->dyn_pcm_assign)
>> +		per_pin->pcm_idx = -1;
>> +	else
>> +		per_pin->pcm_idx = pin_idx;
>> +	per_pin->pin_nid_idx = pin_idx;
>>
>>   	err = hdmi_read_pin_conn(codec, pin_idx);
>>   	if (err < 0)
>> --
>> 1.9.1
>>
diff mbox

Patch

diff --git a/sound/pci/hda/patch_hdmi.c b/sound/pci/hda/patch_hdmi.c
index 19bf782..10edab8 100644
--- a/sound/pci/hda/patch_hdmi.c
+++ b/sound/pci/hda/patch_hdmi.c
@@ -73,6 +73,8 @@  struct hdmi_spec_per_cvt {
 
 struct hdmi_spec_per_pin {
 	hda_nid_t pin_nid;
+	/* pin idx, different device entries on the same pin use the same idx */
+	int pin_nid_idx;
 	int num_mux_nids;
 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
 	int mux_idx;
@@ -85,6 +87,7 @@  struct hdmi_spec_per_pin {
 	struct snd_kcontrol *eld_ctl;
 	struct snd_jack *acomp_jack; /* jack via audio component */
 	struct hda_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
+	int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
 	int repoll_count;
 	bool setup; /* the stream has been set up by prepare callback */
 	int channels; /* current number of channels */
@@ -138,6 +141,8 @@  struct hdmi_spec {
 	struct snd_array pins; /* struct hdmi_spec_per_pin */
 	struct hda_pcm *pcm_rec[16];
 	struct mutex pcm_lock;
+	/* pcm_bitmap means which pcms have been assigned to pins*/
+	unsigned long pcm_bitmap;
 	int pcm_used;	/* counter of pcm_rec[] */
 	unsigned int channels_max; /* max over all cvts */
 
@@ -1786,6 +1791,60 @@  static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
 	return ret;
 }
 
+static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
+				struct hdmi_spec_per_pin *per_pin)
+{
+	int i;
+
+	/* try the prefer PCM */
+	if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
+		return per_pin->pin_nid_idx;
+
+	/* have a second try; check the "reserved area" over num_pins */
+	for (i = spec->num_pins; i < spec->pcm_used; i++) {
+		if (!test_bit(i, &spec->pcm_bitmap))
+			return i;
+	}
+
+	/* the last try; check the empty slots in pins */
+	for (i = 0; i < spec->num_pins; i++) {
+		if (!test_bit(i, &spec->pcm_bitmap))
+			return i;
+	}
+	return -EBUSY;
+}
+
+static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
+				struct hdmi_spec_per_pin *per_pin)
+{
+	int idx;
+
+	/* pcm already be attached to the pin */
+	if (per_pin->pcm)
+		return;
+	idx = hdmi_find_pcm_slot(spec, per_pin);
+	if (idx == -ENODEV)
+		return;
+	per_pin->pcm_idx = idx;
+	per_pin->pcm = spec->pcm_rec[idx];
+	set_bit(idx, &spec->pcm_bitmap);
+}
+
+static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
+				struct hdmi_spec_per_pin *per_pin)
+{
+	int idx;
+
+	/* pcm already be detached from the pin */
+	if (!per_pin->pcm)
+		return;
+	idx = per_pin->pcm_idx;
+	per_pin->pcm_idx = -1;
+	per_pin->pcm = NULL;
+	if (idx >= 0 && idx < spec->pcm_used)
+		clear_bit(idx, &spec->pcm_bitmap);
+}
+
 /* update ELD and jack state via audio component */
 static void sync_eld_via_acomp(struct hda_codec *codec,
 			       struct hdmi_spec_per_pin *per_pin)
@@ -1794,6 +1853,7 @@  static void sync_eld_via_acomp(struct hda_codec *codec,
 	struct hdmi_eld *eld = &spec->temp_eld;
 	int size;
 
+	mutex_lock(&spec->pcm_lock);
 	mutex_lock(&per_pin->lock);
 	size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
 				      &eld->monitor_present, eld->eld_buffer,
@@ -1815,11 +1875,19 @@  static void sync_eld_via_acomp(struct hda_codec *codec,
 		eld->eld_size = 0;
 	}
 
+	if (spec->dyn_pcm_assign) {
+		if (eld->eld_valid)
+			hdmi_attach_hda_pcm(spec, per_pin);
+		else
+			hdmi_detach_hda_pcm(spec, per_pin);
+	}
+
 	update_eld(codec, per_pin, eld);
 	snd_jack_report(per_pin->acomp_jack,
 			eld->monitor_present ? SND_JACK_AVOUT : 0);
  unlock:
 	mutex_unlock(&per_pin->lock);
+	mutex_unlock(&spec->pcm_lock);
 }
 
 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
@@ -1875,6 +1943,11 @@  static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
 
 	per_pin->pin_nid = pin_nid;
 	per_pin->non_pcm = false;
+	if (spec->dyn_pcm_assign)
+		per_pin->pcm_idx = -1;
+	else
+		per_pin->pcm_idx = pin_idx;
+	per_pin->pin_nid_idx = pin_idx;
 
 	err = hdmi_read_pin_conn(codec, pin_idx);
 	if (err < 0)