Re: [PATCH v3 2/5] drm: bridge: dw-hdmi: Report connector status using callback

2019-07-16 Thread Cheng-yi Chiang
On Fri, Jul 12, 2019 at 6:48 PM Russell King - ARM Linux admin
 wrote:
>
> On Fri, Jul 12, 2019 at 06:04:40PM +0800, Cheng-Yi Chiang wrote:
> > Allow codec driver register callback function for plug event.
> >
> > The callback registration flow:
> > dw-hdmi <--- hw-hdmi-i2s-audio <--- hdmi-codec
> >
> > dw-hdmi-i2s-audio implements hook_plugged_cb op
> > so codec driver can register the callback.
> >
> > dw-hdmi implements set_plugged_cb op so platform device can register the
> > callback.
> >
> > When connector plug/unplug event happens, report this event using the
> > callback.
> >
> > Make sure that audio and drm are using the single source of truth for
> > connector status.
> >
> > Signed-off-by: Cheng-Yi Chiang 
> > ---
> >  .../gpu/drm/bridge/synopsys/dw-hdmi-audio.h   |  3 +
> >  .../drm/bridge/synopsys/dw-hdmi-i2s-audio.c   | 10 
> >  drivers/gpu/drm/bridge/synopsys/dw-hdmi.c | 55 ++-
> >  3 files changed, 67 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/gpu/drm/bridge/synopsys/dw-hdmi-audio.h 
> > b/drivers/gpu/drm/bridge/synopsys/dw-hdmi-audio.h
> > index 63b5756f463b..f523c590984e 100644
> > --- a/drivers/gpu/drm/bridge/synopsys/dw-hdmi-audio.h
> > +++ b/drivers/gpu/drm/bridge/synopsys/dw-hdmi-audio.h
> > @@ -2,6 +2,8 @@
> >  #ifndef DW_HDMI_AUDIO_H
> >  #define DW_HDMI_AUDIO_H
> >
> > +#include 
> > +
> >  struct dw_hdmi;
> >
> >  struct dw_hdmi_audio_data {
> > @@ -17,6 +19,7 @@ struct dw_hdmi_i2s_audio_data {
> >
> >   void (*write)(struct dw_hdmi *hdmi, u8 val, int offset);
> >   u8 (*read)(struct dw_hdmi *hdmi, int offset);
> > + int (*set_plugged_cb)(struct dw_hdmi *hdmi, hdmi_codec_plugged_cb fn);
> >  };
> >
> >  #endif
> > diff --git a/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c 
> > b/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
> > index 5cbb71a866d5..7b93cf05c985 100644
> > --- a/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
> > +++ b/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
> > @@ -104,10 +104,20 @@ static int dw_hdmi_i2s_get_dai_id(struct 
> > snd_soc_component *component,
> >   return -EINVAL;
> >  }
> >
> > +static int dw_hdmi_i2s_hook_plugged_cb(struct device *dev, void *data,
> > +hdmi_codec_plugged_cb fn)
> > +{
> > + struct dw_hdmi_i2s_audio_data *audio = data;
> > + struct dw_hdmi *hdmi = audio->hdmi;
> > +
> > + return audio->set_plugged_cb(hdmi, fn);
> > +}
> > +
> >  static struct hdmi_codec_ops dw_hdmi_i2s_ops = {
> >   .hw_params  = dw_hdmi_i2s_hw_params,
> >   .audio_shutdown = dw_hdmi_i2s_audio_shutdown,
> >   .get_dai_id = dw_hdmi_i2s_get_dai_id,
> > + .hook_plugged_cb = dw_hdmi_i2s_hook_plugged_cb,
> >  };
> >
> >  static int snd_dw_hdmi_probe(struct platform_device *pdev)
> > diff --git a/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c 
> > b/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
> > index 045b1b13fd0e..ce6646067472 100644
> > --- a/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
> > +++ b/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
> > @@ -26,6 +26,8 @@
> >  #include 
> >  #include 
> >
> > +#include 
> > +
> >  #include 
> >  #include 
> >
> > @@ -185,6 +187,9 @@ struct dw_hdmi {
> >   void (*disable_audio)(struct dw_hdmi *hdmi);
> >
> >   struct cec_notifier *cec_notifier;
> > +
> > + hdmi_codec_plugged_cb plugged_cb;
> > + enum drm_connector_status last_connector_result;
> >  };
> >
> >  #define HDMI_IH_PHY_STAT0_RX_SENSE \
> > @@ -209,6 +214,40 @@ static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int 
> > offset)
> >   return val;
> >  }
> >
> > +static void handle_plugged_change(struct dw_hdmi *hdmi, bool plugged)
> > +{
> > + struct platform_device *codec_pdev;
> > +
> > + if (!hdmi->audio || IS_ERR(hdmi->audio))
> > + return;
> > + codec_pdev = platform_get_drvdata(hdmi->audio);
> > + if (!codec_pdev || IS_ERR(codec_pdev))
> > + return;
>
> This looks fragile to me, poking about in another device's driver data
> from another driver is really not a good design decision.  I think this
> can be simplified if the registration function took the function
> pointer and the struct device pointer, and then you only need one test
> below:
>
Hi Russell, Thank you for the detailed review.
ACK to this suggestion.
I have updated the registration function following your suggestion in v4.
It looks much cleaner.

> > + if (!hdmi->plugged_cb)
> > + return;
> > +
> > + hdmi->plugged_cb(&codec_pdev->dev, plugged);
> > +}
> > +
> > +static int hdmi_set_plugged_cb(struct dw_hdmi *hdmi, hdmi_codec_plugged_cb 
> > fn)
> > +{
> > + bool plugged;
> > + struct platform_device *codec_pdev;
> > +
> > + if (!hdmi->audio || IS_ERR(hdmi->audio))
> > + return -EINVAL;
>
> Given the current code structure, how can this ever be true when the
> function is called?
>
ACK
Removed in v4.
> > + codec_pdev = platform_get_drvdata(hdmi->audio);
> > +

Re: [PATCH v3 2/5] drm: bridge: dw-hdmi: Report connector status using callback

2019-07-12 Thread Russell King - ARM Linux admin
On Fri, Jul 12, 2019 at 06:04:40PM +0800, Cheng-Yi Chiang wrote:
> Allow codec driver register callback function for plug event.
> 
> The callback registration flow:
> dw-hdmi <--- hw-hdmi-i2s-audio <--- hdmi-codec
> 
> dw-hdmi-i2s-audio implements hook_plugged_cb op
> so codec driver can register the callback.
> 
> dw-hdmi implements set_plugged_cb op so platform device can register the
> callback.
> 
> When connector plug/unplug event happens, report this event using the
> callback.
> 
> Make sure that audio and drm are using the single source of truth for
> connector status.
> 
> Signed-off-by: Cheng-Yi Chiang 
> ---
>  .../gpu/drm/bridge/synopsys/dw-hdmi-audio.h   |  3 +
>  .../drm/bridge/synopsys/dw-hdmi-i2s-audio.c   | 10 
>  drivers/gpu/drm/bridge/synopsys/dw-hdmi.c | 55 ++-
>  3 files changed, 67 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/bridge/synopsys/dw-hdmi-audio.h 
> b/drivers/gpu/drm/bridge/synopsys/dw-hdmi-audio.h
> index 63b5756f463b..f523c590984e 100644
> --- a/drivers/gpu/drm/bridge/synopsys/dw-hdmi-audio.h
> +++ b/drivers/gpu/drm/bridge/synopsys/dw-hdmi-audio.h
> @@ -2,6 +2,8 @@
>  #ifndef DW_HDMI_AUDIO_H
>  #define DW_HDMI_AUDIO_H
>  
> +#include 
> +
>  struct dw_hdmi;
>  
>  struct dw_hdmi_audio_data {
> @@ -17,6 +19,7 @@ struct dw_hdmi_i2s_audio_data {
>  
>   void (*write)(struct dw_hdmi *hdmi, u8 val, int offset);
>   u8 (*read)(struct dw_hdmi *hdmi, int offset);
> + int (*set_plugged_cb)(struct dw_hdmi *hdmi, hdmi_codec_plugged_cb fn);
>  };
>  
>  #endif
> diff --git a/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c 
> b/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
> index 5cbb71a866d5..7b93cf05c985 100644
> --- a/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
> +++ b/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
> @@ -104,10 +104,20 @@ static int dw_hdmi_i2s_get_dai_id(struct 
> snd_soc_component *component,
>   return -EINVAL;
>  }
>  
> +static int dw_hdmi_i2s_hook_plugged_cb(struct device *dev, void *data,
> +hdmi_codec_plugged_cb fn)
> +{
> + struct dw_hdmi_i2s_audio_data *audio = data;
> + struct dw_hdmi *hdmi = audio->hdmi;
> +
> + return audio->set_plugged_cb(hdmi, fn);
> +}
> +
>  static struct hdmi_codec_ops dw_hdmi_i2s_ops = {
>   .hw_params  = dw_hdmi_i2s_hw_params,
>   .audio_shutdown = dw_hdmi_i2s_audio_shutdown,
>   .get_dai_id = dw_hdmi_i2s_get_dai_id,
> + .hook_plugged_cb = dw_hdmi_i2s_hook_plugged_cb,
>  };
>  
>  static int snd_dw_hdmi_probe(struct platform_device *pdev)
> diff --git a/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c 
> b/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
> index 045b1b13fd0e..ce6646067472 100644
> --- a/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
> +++ b/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
> @@ -26,6 +26,8 @@
>  #include 
>  #include 
>  
> +#include 
> +
>  #include 
>  #include 
>  
> @@ -185,6 +187,9 @@ struct dw_hdmi {
>   void (*disable_audio)(struct dw_hdmi *hdmi);
>  
>   struct cec_notifier *cec_notifier;
> +
> + hdmi_codec_plugged_cb plugged_cb;
> + enum drm_connector_status last_connector_result;
>  };
>  
>  #define HDMI_IH_PHY_STAT0_RX_SENSE \
> @@ -209,6 +214,40 @@ static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int 
> offset)
>   return val;
>  }
>  
> +static void handle_plugged_change(struct dw_hdmi *hdmi, bool plugged)
> +{
> + struct platform_device *codec_pdev;
> +
> + if (!hdmi->audio || IS_ERR(hdmi->audio))
> + return;
> + codec_pdev = platform_get_drvdata(hdmi->audio);
> + if (!codec_pdev || IS_ERR(codec_pdev))
> + return;

This looks fragile to me, poking about in another device's driver data
from another driver is really not a good design decision.  I think this
can be simplified if the registration function took the function
pointer and the struct device pointer, and then you only need one test
below:

> + if (!hdmi->plugged_cb)
> + return;
> +
> + hdmi->plugged_cb(&codec_pdev->dev, plugged);
> +}
> +
> +static int hdmi_set_plugged_cb(struct dw_hdmi *hdmi, hdmi_codec_plugged_cb 
> fn)
> +{
> + bool plugged;
> + struct platform_device *codec_pdev;
> +
> + if (!hdmi->audio || IS_ERR(hdmi->audio))
> + return -EINVAL;

Given the current code structure, how can this ever be true when the
function is called?

> + codec_pdev = platform_get_drvdata(hdmi->audio);
> + if (!codec_pdev || IS_ERR(codec_pdev))
> + return -EINVAL;

This doesn't seem like a good idea as I've pointed out above.

> +
> + mutex_lock(&hdmi->mutex);
> + hdmi->plugged_cb = fn;
> + plugged = hdmi->last_connector_result == connector_status_connected;
> + handle_plugged_change(hdmi, plugged);
> + mutex_unlock(&hdmi->mutex);

Should be a blank line here for readability.

> + return 0;
> +}
> +
>  static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask,