[3/7] drm/i915: refactoring audio component functions

Submitted by Takashi Iwai on Nov. 30, 2015, 1:37 p.m.

Details

Message ID 1448890671-2983-4-git-send-email-tiwai@suse.de
State New
Headers show
Series "Add get_eld audio component for i915/HD-audio" ( rev: 1 ) in Intel GFX

Commit Message

Takashi Iwai Nov. 30, 2015, 1:37 p.m.
We have a common loop of encoder to look for the given audio port in
two audio component functions.  Split out a local helper function to
do it for the code simplification.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 drivers/gpu/drm/i915/intel_audio.c | 61 ++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 29 deletions(-)

Patch hide | download patch | download mbox

diff --git a/drivers/gpu/drm/i915/intel_audio.c b/drivers/gpu/drm/i915/intel_audio.c
index 6b318a8d5dc9..024e88ae6305 100644
--- a/drivers/gpu/drm/i915/intel_audio.c
+++ b/drivers/gpu/drm/i915/intel_audio.c
@@ -630,17 +630,33 @@  static int i915_audio_component_get_cdclk_freq(struct device *dev)
 	return ret;
 }
 
+static struct intel_encoder *audio_port_to_encoder(struct drm_device *drm_dev,
+						   int port)
+{
+	struct intel_encoder *intel_encoder;
+	struct intel_digital_port *intel_dig_port;
+
+	for_each_intel_encoder(drm_dev, intel_encoder) {
+		if (intel_encoder->type != INTEL_OUTPUT_HDMI &&
+		    intel_encoder->type != INTEL_OUTPUT_DISPLAYPORT)
+			continue;
+		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
+		if (port == intel_dig_port->port)
+			return intel_encoder;
+	}
+	return NULL;
+}
+
 static int i915_audio_component_sync_audio_rate(struct device *dev,
 						int port, int rate)
 {
 	struct drm_i915_private *dev_priv = dev_to_i915(dev);
 	struct drm_device *drm_dev = dev_priv->dev;
 	struct intel_encoder *intel_encoder;
-	struct intel_digital_port *intel_dig_port;
 	struct intel_crtc *crtc;
 	struct drm_display_mode *mode;
 	struct i915_audio_component *acomp = dev_priv->audio_component;
-	enum pipe pipe = -1;
+	enum pipe pipe;
 	u32 tmp;
 	int n;
 
@@ -652,22 +668,14 @@  static int i915_audio_component_sync_audio_rate(struct device *dev,
 
 	mutex_lock(&dev_priv->av_mutex);
 	/* 1. get the pipe */
-	for_each_intel_encoder(drm_dev, intel_encoder) {
-		if (intel_encoder->type != INTEL_OUTPUT_HDMI)
-			continue;
-		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
-		if (port == intel_dig_port->port) {
-			crtc = to_intel_crtc(intel_encoder->base.crtc);
-			pipe = crtc->pipe;
-			break;
-		}
-	}
-
-	if (pipe == INVALID_PIPE) {
+	intel_encoder = audio_port_to_encoder(drm_dev, port);
+	if (!intel_encoder || intel_encoder->type != INTEL_OUTPUT_HDMI) {
 		DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));
 		mutex_unlock(&dev_priv->av_mutex);
 		return -ENODEV;
 	}
+	crtc = to_intel_crtc(intel_encoder->base.crtc);
+	pipe = crtc->pipe;
 	DRM_DEBUG_KMS("pipe %c connects port %c\n",
 				  pipe_name(pipe), port_name(port));
 	mode = &crtc->config->base.adjusted_mode;
@@ -717,23 +725,18 @@  static int i915_audio_component_get_eld(struct device *dev, int port,
 	int ret = -EINVAL;
 
 	mutex_lock(&dev_priv->av_mutex);
-	for_each_intel_encoder(drm_dev, intel_encoder) {
-		if (intel_encoder->type != INTEL_OUTPUT_DISPLAYPORT &&
-		    intel_encoder->type != INTEL_OUTPUT_HDMI)
-			continue;
+	intel_encoder = audio_port_to_encoder(drm_dev, port);
+	if (intel_encoder) {
+		ret = 0;
 		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
-		if (port == intel_dig_port->port) {
-			ret = 0;
-			*enabled = intel_dig_port->audio_enabled;
-			if (!*enabled)
-				break;
+		*enabled = intel_dig_port->audio_enabled;
+		if (*enabled) {
 			connector = drm_select_eld(&intel_encoder->base);
-			if (!connector)
-				break;
-			eld = connector->eld;
-			ret = min(max_bytes, drm_eld_size(eld));
-			memcpy(buf, eld, ret);
-			break;
+			if (connector) {
+				eld = connector->eld;
+				ret = min(max_bytes, drm_eld_size(eld));
+				memcpy(buf, eld, ret);
+			}
 		}
 	}
 

Comments

On Mon, Nov 30, 2015 at 02:37:47PM +0100, Takashi Iwai wrote:
> We have a common loop of encoder to look for the given audio port in
> two audio component functions.  Split out a local helper function to
> do it for the code simplification.
> 
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> ---
>  drivers/gpu/drm/i915/intel_audio.c | 61 ++++++++++++++++++++------------------
>  1 file changed, 32 insertions(+), 29 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/intel_audio.c b/drivers/gpu/drm/i915/intel_audio.c
> index 6b318a8d5dc9..024e88ae6305 100644
> --- a/drivers/gpu/drm/i915/intel_audio.c
> +++ b/drivers/gpu/drm/i915/intel_audio.c
> @@ -630,17 +630,33 @@ static int i915_audio_component_get_cdclk_freq(struct device *dev)
>  	return ret;
>  }
>  
> +static struct intel_encoder *audio_port_to_encoder(struct drm_device *drm_dev,
> +						   int port)
> +{
> +	struct intel_encoder *intel_encoder;
> +	struct intel_digital_port *intel_dig_port;
> +
> +	for_each_intel_encoder(drm_dev, intel_encoder) {
> +		if (intel_encoder->type != INTEL_OUTPUT_HDMI &&
> +		    intel_encoder->type != INTEL_OUTPUT_DISPLAYPORT)
> +			continue;
> +		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
> +		if (port == intel_dig_port->port)

If this is static, maybe just maintain a snd_pin_to_port mapping in
dev_priv? That's the approach we're usually taking, and it has the benefit
of making it clear(er) that the binding is static ... Or is it not?

Makes sense otherwise.
-Daniel

> +			return intel_encoder;
> +	}
> +	return NULL;
> +}
> +
>  static int i915_audio_component_sync_audio_rate(struct device *dev,
>  						int port, int rate)
>  {
>  	struct drm_i915_private *dev_priv = dev_to_i915(dev);
>  	struct drm_device *drm_dev = dev_priv->dev;
>  	struct intel_encoder *intel_encoder;
> -	struct intel_digital_port *intel_dig_port;
>  	struct intel_crtc *crtc;
>  	struct drm_display_mode *mode;
>  	struct i915_audio_component *acomp = dev_priv->audio_component;
> -	enum pipe pipe = -1;
> +	enum pipe pipe;
>  	u32 tmp;
>  	int n;
>  
> @@ -652,22 +668,14 @@ static int i915_audio_component_sync_audio_rate(struct device *dev,
>  
>  	mutex_lock(&dev_priv->av_mutex);
>  	/* 1. get the pipe */
> -	for_each_intel_encoder(drm_dev, intel_encoder) {
> -		if (intel_encoder->type != INTEL_OUTPUT_HDMI)
> -			continue;
> -		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
> -		if (port == intel_dig_port->port) {
> -			crtc = to_intel_crtc(intel_encoder->base.crtc);
> -			pipe = crtc->pipe;
> -			break;
> -		}
> -	}
> -
> -	if (pipe == INVALID_PIPE) {
> +	intel_encoder = audio_port_to_encoder(drm_dev, port);
> +	if (!intel_encoder || intel_encoder->type != INTEL_OUTPUT_HDMI) {
>  		DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));
>  		mutex_unlock(&dev_priv->av_mutex);
>  		return -ENODEV;
>  	}
> +	crtc = to_intel_crtc(intel_encoder->base.crtc);
> +	pipe = crtc->pipe;
>  	DRM_DEBUG_KMS("pipe %c connects port %c\n",
>  				  pipe_name(pipe), port_name(port));
>  	mode = &crtc->config->base.adjusted_mode;
> @@ -717,23 +725,18 @@ static int i915_audio_component_get_eld(struct device *dev, int port,
>  	int ret = -EINVAL;
>  
>  	mutex_lock(&dev_priv->av_mutex);
> -	for_each_intel_encoder(drm_dev, intel_encoder) {
> -		if (intel_encoder->type != INTEL_OUTPUT_DISPLAYPORT &&
> -		    intel_encoder->type != INTEL_OUTPUT_HDMI)
> -			continue;
> +	intel_encoder = audio_port_to_encoder(drm_dev, port);
> +	if (intel_encoder) {
> +		ret = 0;
>  		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
> -		if (port == intel_dig_port->port) {
> -			ret = 0;
> -			*enabled = intel_dig_port->audio_enabled;
> -			if (!*enabled)
> -				break;
> +		*enabled = intel_dig_port->audio_enabled;
> +		if (*enabled) {
>  			connector = drm_select_eld(&intel_encoder->base);
> -			if (!connector)
> -				break;
> -			eld = connector->eld;
> -			ret = min(max_bytes, drm_eld_size(eld));
> -			memcpy(buf, eld, ret);
> -			break;
> +			if (connector) {
> +				eld = connector->eld;
> +				ret = min(max_bytes, drm_eld_size(eld));
> +				memcpy(buf, eld, ret);
> +			}
>  		}
>  	}
>  
> -- 
> 2.6.3
> 
> _______________________________________________
> Intel-gfx mailing list
> Intel-gfx@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/intel-gfx
On Mon, 30 Nov 2015 15:14:03 +0100,
Daniel Vetter wrote:
> 
> On Mon, Nov 30, 2015 at 02:37:47PM +0100, Takashi Iwai wrote:
> > We have a common loop of encoder to look for the given audio port in
> > two audio component functions.  Split out a local helper function to
> > do it for the code simplification.
> > 
> > Signed-off-by: Takashi Iwai <tiwai@suse.de>
> > ---
> >  drivers/gpu/drm/i915/intel_audio.c | 61 ++++++++++++++++++++------------------
> >  1 file changed, 32 insertions(+), 29 deletions(-)
> > 
> > diff --git a/drivers/gpu/drm/i915/intel_audio.c b/drivers/gpu/drm/i915/intel_audio.c
> > index 6b318a8d5dc9..024e88ae6305 100644
> > --- a/drivers/gpu/drm/i915/intel_audio.c
> > +++ b/drivers/gpu/drm/i915/intel_audio.c
> > @@ -630,17 +630,33 @@ static int i915_audio_component_get_cdclk_freq(struct device *dev)
> >  	return ret;
> >  }
> >  
> > +static struct intel_encoder *audio_port_to_encoder(struct drm_device *drm_dev,
> > +						   int port)
> > +{
> > +	struct intel_encoder *intel_encoder;
> > +	struct intel_digital_port *intel_dig_port;
> > +
> > +	for_each_intel_encoder(drm_dev, intel_encoder) {
> > +		if (intel_encoder->type != INTEL_OUTPUT_HDMI &&
> > +		    intel_encoder->type != INTEL_OUTPUT_DISPLAYPORT)
> > +			continue;
> > +		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
> > +		if (port == intel_dig_port->port)
> 
> If this is static, maybe just maintain a snd_pin_to_port mapping in
> dev_priv? That's the approach we're usually taking, and it has the benefit
> of making it clear(er) that the binding is static ... Or is it not?

Yes, I *guess* this is static, but need a double-check.  The current
patchset just derives from the existing code.


thanks,

Takashi

> Makes sense otherwise.
> -Daniel
> 
> > +			return intel_encoder;
> > +	}
> > +	return NULL;
> > +}
> > +
> >  static int i915_audio_component_sync_audio_rate(struct device *dev,
> >  						int port, int rate)
> >  {
> >  	struct drm_i915_private *dev_priv = dev_to_i915(dev);
> >  	struct drm_device *drm_dev = dev_priv->dev;
> >  	struct intel_encoder *intel_encoder;
> > -	struct intel_digital_port *intel_dig_port;
> >  	struct intel_crtc *crtc;
> >  	struct drm_display_mode *mode;
> >  	struct i915_audio_component *acomp = dev_priv->audio_component;
> > -	enum pipe pipe = -1;
> > +	enum pipe pipe;
> >  	u32 tmp;
> >  	int n;
> >  
> > @@ -652,22 +668,14 @@ static int i915_audio_component_sync_audio_rate(struct device *dev,
> >  
> >  	mutex_lock(&dev_priv->av_mutex);
> >  	/* 1. get the pipe */
> > -	for_each_intel_encoder(drm_dev, intel_encoder) {
> > -		if (intel_encoder->type != INTEL_OUTPUT_HDMI)
> > -			continue;
> > -		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
> > -		if (port == intel_dig_port->port) {
> > -			crtc = to_intel_crtc(intel_encoder->base.crtc);
> > -			pipe = crtc->pipe;
> > -			break;
> > -		}
> > -	}
> > -
> > -	if (pipe == INVALID_PIPE) {
> > +	intel_encoder = audio_port_to_encoder(drm_dev, port);
> > +	if (!intel_encoder || intel_encoder->type != INTEL_OUTPUT_HDMI) {
> >  		DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));
> >  		mutex_unlock(&dev_priv->av_mutex);
> >  		return -ENODEV;
> >  	}
> > +	crtc = to_intel_crtc(intel_encoder->base.crtc);
> > +	pipe = crtc->pipe;
> >  	DRM_DEBUG_KMS("pipe %c connects port %c\n",
> >  				  pipe_name(pipe), port_name(port));
> >  	mode = &crtc->config->base.adjusted_mode;
> > @@ -717,23 +725,18 @@ static int i915_audio_component_get_eld(struct device *dev, int port,
> >  	int ret = -EINVAL;
> >  
> >  	mutex_lock(&dev_priv->av_mutex);
> > -	for_each_intel_encoder(drm_dev, intel_encoder) {
> > -		if (intel_encoder->type != INTEL_OUTPUT_DISPLAYPORT &&
> > -		    intel_encoder->type != INTEL_OUTPUT_HDMI)
> > -			continue;
> > +	intel_encoder = audio_port_to_encoder(drm_dev, port);
> > +	if (intel_encoder) {
> > +		ret = 0;
> >  		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
> > -		if (port == intel_dig_port->port) {
> > -			ret = 0;
> > -			*enabled = intel_dig_port->audio_enabled;
> > -			if (!*enabled)
> > -				break;
> > +		*enabled = intel_dig_port->audio_enabled;
> > +		if (*enabled) {
> >  			connector = drm_select_eld(&intel_encoder->base);
> > -			if (!connector)
> > -				break;
> > -			eld = connector->eld;
> > -			ret = min(max_bytes, drm_eld_size(eld));
> > -			memcpy(buf, eld, ret);
> > -			break;
> > +			if (connector) {
> > +				eld = connector->eld;
> > +				ret = min(max_bytes, drm_eld_size(eld));
> > +				memcpy(buf, eld, ret);
> > +			}
> >  		}
> >  	}
> >  
> > -- 
> > 2.6.3
> > 
> > _______________________________________________
> > Intel-gfx mailing list
> > Intel-gfx@lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/intel-gfx
> 
> -- 
> Daniel Vetter
> Software Engineer, Intel Corporation
> http://blog.ffwll.ch
>