| From fe19f02dbfd020df9b028cf2c580417c4edc31b3 Mon Sep 17 00:00:00 2001 | 
 | From: Maxime Ripard <maxime@cerno.tech> | 
 | Date: Mon, 6 Jan 2020 18:45:46 +0100 | 
 | Subject: [PATCH] drm/vc4: hdmi: Add container_of macros for encoders | 
 |  and connectors | 
 |  | 
 | Whenever the code needs to access the vc4_hdmi structure from a DRM | 
 | connector or encoder, it first accesses the drm_device associated to the | 
 | connector, then retrieve the drm_dev private data which gives it a | 
 | pointer to our vc4_dev, and will finally follow the vc4_hdmi pointer in | 
 | that structure. | 
 |  | 
 | That will also give us some trouble when having multiple controllers, | 
 | but now that we have our encoder and connector structures that are part | 
 | of vc4_hdmi, we can simply call container_of on the DRM connector or | 
 | encoder and retrieve the vc4_hdmi structure directly. | 
 |  | 
 | Signed-off-by: Maxime Ripard <maxime@cerno.tech> | 
 | --- | 
 |  drivers/gpu/drm/vc4/vc4_hdmi.c | 41 ++++++++++------------------------ | 
 |  drivers/gpu/drm/vc4/vc4_hdmi.h | 16 +++++++++++++ | 
 |  2 files changed, 28 insertions(+), 29 deletions(-) | 
 |  | 
 | --- a/drivers/gpu/drm/vc4/vc4_hdmi.c | 
 | +++ b/drivers/gpu/drm/vc4/vc4_hdmi.c | 
 | @@ -120,9 +120,7 @@ static int vc4_hdmi_debugfs_regs(struct | 
 |  static enum drm_connector_status | 
 |  vc4_hdmi_connector_detect(struct drm_connector *connector, bool force) | 
 |  { | 
 | -	struct drm_device *dev = connector->dev; | 
 | -	struct vc4_dev *vc4 = to_vc4_dev(dev); | 
 | -	struct vc4_hdmi *vc4_hdmi = vc4->hdmi; | 
 | +	struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); | 
 |   | 
 |  	if (vc4_hdmi->hpd_gpio) { | 
 |  		if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^ | 
 | @@ -149,17 +147,13 @@ static void vc4_hdmi_connector_destroy(s | 
 |   | 
 |  static int vc4_hdmi_connector_get_modes(struct drm_connector *connector) | 
 |  { | 
 | -	struct vc4_hdmi_connector *vc4_connector = | 
 | -		to_vc4_hdmi_connector(connector); | 
 | -	struct drm_encoder *encoder = vc4_connector->encoder; | 
 | -	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); | 
 | -	struct drm_device *dev = connector->dev; | 
 | -	struct vc4_dev *vc4 = to_vc4_dev(dev); | 
 | +	struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); | 
 | +	struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder; | 
 |  	int ret = 0; | 
 |  	struct edid *edid; | 
 |   | 
 | -	edid = drm_get_edid(connector, vc4->hdmi->ddc); | 
 | -	cec_s_phys_addr_from_edid(vc4->hdmi->cec_adap, edid); | 
 | +	edid = drm_get_edid(connector, vc4_hdmi->ddc); | 
 | +	cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid); | 
 |  	if (!edid) | 
 |  		return -ENODEV; | 
 |   | 
 | @@ -235,9 +229,7 @@ static const struct drm_encoder_funcs vc | 
 |  static int vc4_hdmi_stop_packet(struct drm_encoder *encoder, | 
 |  				enum hdmi_infoframe_type type) | 
 |  { | 
 | -	struct drm_device *dev = encoder->dev; | 
 | -	struct vc4_dev *vc4 = to_vc4_dev(dev); | 
 | -	struct vc4_hdmi *vc4_hdmi = vc4->hdmi; | 
 | +	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); | 
 |  	u32 packet_id = type - 0x80; | 
 |   | 
 |  	HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, | 
 | @@ -250,9 +242,7 @@ static int vc4_hdmi_stop_packet(struct d | 
 |  static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder, | 
 |  				     union hdmi_infoframe *frame) | 
 |  { | 
 | -	struct drm_device *dev = encoder->dev; | 
 | -	struct vc4_dev *vc4 = to_vc4_dev(dev); | 
 | -	struct vc4_hdmi *vc4_hdmi = vc4->hdmi; | 
 | +	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); | 
 |  	u32 packet_id = frame->any.type - 0x80; | 
 |  	u32 packet_reg = VC4_HDMI_RAM_PACKET(packet_id); | 
 |  	uint8_t buffer[VC4_HDMI_PACKET_STRIDE]; | 
 | @@ -298,9 +288,8 @@ static void vc4_hdmi_write_infoframe(str | 
 |   | 
 |  static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder) | 
 |  { | 
 | +	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); | 
 |  	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); | 
 | -	struct vc4_dev *vc4 = encoder->dev->dev_private; | 
 | -	struct vc4_hdmi *vc4_hdmi = vc4->hdmi; | 
 |  	struct drm_connector *connector = &vc4_hdmi->connector.base; | 
 |  	struct drm_connector_state *cstate = connector->state; | 
 |  	struct drm_crtc *crtc = encoder->crtc; | 
 | @@ -347,9 +336,7 @@ static void vc4_hdmi_set_spd_infoframe(s | 
 |   | 
 |  static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder) | 
 |  { | 
 | -	struct drm_device *drm = encoder->dev; | 
 | -	struct vc4_dev *vc4 = drm->dev_private; | 
 | -	struct vc4_hdmi *vc4_hdmi = vc4->hdmi; | 
 | +	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); | 
 |  	union hdmi_infoframe frame; | 
 |  	int ret; | 
 |   | 
 | @@ -371,9 +358,7 @@ static void vc4_hdmi_set_infoframes(stru | 
 |   | 
 |  static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder) | 
 |  { | 
 | -	struct drm_device *dev = encoder->dev; | 
 | -	struct vc4_dev *vc4 = to_vc4_dev(dev); | 
 | -	struct vc4_hdmi *vc4_hdmi = vc4->hdmi; | 
 | +	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); | 
 |  	int ret; | 
 |   | 
 |  	HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0); | 
 | @@ -392,10 +377,8 @@ static void vc4_hdmi_encoder_disable(str | 
 |  static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder) | 
 |  { | 
 |  	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; | 
 | -	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); | 
 | -	struct drm_device *dev = encoder->dev; | 
 | -	struct vc4_dev *vc4 = to_vc4_dev(dev); | 
 | -	struct vc4_hdmi *vc4_hdmi = vc4->hdmi; | 
 | +	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); | 
 | +	struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder; | 
 |  	bool debug_dump_regs = false; | 
 |  	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC; | 
 |  	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC; | 
 | --- a/drivers/gpu/drm/vc4/vc4_hdmi.h | 
 | +++ b/drivers/gpu/drm/vc4/vc4_hdmi.h | 
 | @@ -78,6 +78,22 @@ struct vc4_hdmi { | 
 |  	struct debugfs_regset32 hd_regset; | 
 |  }; | 
 |   | 
 | +static inline struct vc4_hdmi * | 
 | +connector_to_vc4_hdmi(struct drm_connector *connector) | 
 | +{ | 
 | +	struct vc4_hdmi_connector *_connector = to_vc4_hdmi_connector(connector); | 
 | + | 
 | +	return container_of(_connector, struct vc4_hdmi, connector); | 
 | +} | 
 | + | 
 | +static inline struct vc4_hdmi * | 
 | +encoder_to_vc4_hdmi(struct drm_encoder *encoder) | 
 | +{ | 
 | +	struct vc4_hdmi_encoder *_encoder = to_vc4_hdmi_encoder(encoder); | 
 | + | 
 | +	return container_of(_encoder, struct vc4_hdmi, encoder); | 
 | +} | 
 | + | 
 |  #define HDMI_READ(offset) readl(vc4_hdmi->hdmicore_regs + offset) | 
 |  #define HDMI_WRITE(offset, val) writel(val, vc4_hdmi->hdmicore_regs + offset) | 
 |  #define HD_READ(offset) readl(vc4_hdmi->hd_regs + offset) |