1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
7 #include <linux/delay.h>
8 #include <drm/drm_bridge_connector.h>
9 #include <drm/drm_edid.h>
14 void msm_hdmi_bridge_destroy(struct drm_bridge *bridge)
16 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
18 msm_hdmi_hpd_disable(hdmi_bridge);
19 drm_bridge_remove(bridge);
22 static void msm_hdmi_power_on(struct drm_bridge *bridge)
24 struct drm_device *dev = bridge->dev;
25 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
26 struct hdmi *hdmi = hdmi_bridge->hdmi;
27 const struct hdmi_platform_config *config = hdmi->config;
30 pm_runtime_get_sync(&hdmi->pdev->dev);
32 ret = regulator_bulk_enable(config->pwr_reg_cnt, hdmi->pwr_regs);
34 DRM_DEV_ERROR(dev->dev, "failed to enable pwr regulator: %d\n", ret);
36 if (config->pwr_clk_cnt > 0) {
37 DBG("pixclock: %lu", hdmi->pixclock);
38 ret = clk_set_rate(hdmi->pwr_clks[0], hdmi->pixclock);
40 DRM_DEV_ERROR(dev->dev, "failed to set pixel clk: %s (%d)\n",
41 config->pwr_clk_names[0], ret);
45 for (i = 0; i < config->pwr_clk_cnt; i++) {
46 ret = clk_prepare_enable(hdmi->pwr_clks[i]);
48 DRM_DEV_ERROR(dev->dev, "failed to enable pwr clk: %s (%d)\n",
49 config->pwr_clk_names[i], ret);
54 static void power_off(struct drm_bridge *bridge)
56 struct drm_device *dev = bridge->dev;
57 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
58 struct hdmi *hdmi = hdmi_bridge->hdmi;
59 const struct hdmi_platform_config *config = hdmi->config;
62 /* TODO do we need to wait for final vblank somewhere before
67 for (i = 0; i < config->pwr_clk_cnt; i++)
68 clk_disable_unprepare(hdmi->pwr_clks[i]);
70 ret = regulator_bulk_disable(config->pwr_reg_cnt, hdmi->pwr_regs);
72 DRM_DEV_ERROR(dev->dev, "failed to disable pwr regulator: %d\n", ret);
74 pm_runtime_put(&hdmi->pdev->dev);
77 #define AVI_IFRAME_LINE_NUMBER 1
79 static void msm_hdmi_config_avi_infoframe(struct hdmi *hdmi)
81 struct drm_crtc *crtc = hdmi->encoder->crtc;
82 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
83 union hdmi_infoframe frame;
84 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
88 drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
89 hdmi->connector, mode);
91 len = hdmi_infoframe_pack(&frame, buffer, sizeof(buffer));
93 DRM_DEV_ERROR(&hdmi->pdev->dev,
94 "failed to configure avi infoframe\n");
99 * the AVI_INFOx registers don't map exactly to how the AVI infoframes
100 * are packed according to the spec. The checksum from the header is
101 * written to the LSB byte of AVI_INFO0 and the version is written to
102 * the third byte from the LSB of AVI_INFO3
104 hdmi_write(hdmi, REG_HDMI_AVI_INFO(0),
110 hdmi_write(hdmi, REG_HDMI_AVI_INFO(1),
116 hdmi_write(hdmi, REG_HDMI_AVI_INFO(2),
122 hdmi_write(hdmi, REG_HDMI_AVI_INFO(3),
127 hdmi_write(hdmi, REG_HDMI_INFOFRAME_CTRL0,
128 HDMI_INFOFRAME_CTRL0_AVI_SEND |
129 HDMI_INFOFRAME_CTRL0_AVI_CONT);
131 val = hdmi_read(hdmi, REG_HDMI_INFOFRAME_CTRL1);
132 val &= ~HDMI_INFOFRAME_CTRL1_AVI_INFO_LINE__MASK;
133 val |= HDMI_INFOFRAME_CTRL1_AVI_INFO_LINE(AVI_IFRAME_LINE_NUMBER);
134 hdmi_write(hdmi, REG_HDMI_INFOFRAME_CTRL1, val);
137 static void msm_hdmi_bridge_pre_enable(struct drm_bridge *bridge)
139 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
140 struct hdmi *hdmi = hdmi_bridge->hdmi;
141 struct hdmi_phy *phy = hdmi->phy;
145 if (!hdmi->power_on) {
146 msm_hdmi_phy_resource_enable(phy);
147 msm_hdmi_power_on(bridge);
148 hdmi->power_on = true;
149 if (hdmi->hdmi_mode) {
150 msm_hdmi_config_avi_infoframe(hdmi);
151 msm_hdmi_audio_update(hdmi);
155 msm_hdmi_phy_powerup(phy, hdmi->pixclock);
157 msm_hdmi_set_mode(hdmi, true);
160 msm_hdmi_hdcp_on(hdmi->hdcp_ctrl);
163 static void msm_hdmi_bridge_post_disable(struct drm_bridge *bridge)
165 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
166 struct hdmi *hdmi = hdmi_bridge->hdmi;
167 struct hdmi_phy *phy = hdmi->phy;
170 msm_hdmi_hdcp_off(hdmi->hdcp_ctrl);
173 msm_hdmi_set_mode(hdmi, false);
175 msm_hdmi_phy_powerdown(phy);
177 if (hdmi->power_on) {
179 hdmi->power_on = false;
181 msm_hdmi_audio_update(hdmi);
182 msm_hdmi_phy_resource_disable(phy);
186 static void msm_hdmi_bridge_mode_set(struct drm_bridge *bridge,
187 const struct drm_display_mode *mode,
188 const struct drm_display_mode *adjusted_mode)
190 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
191 struct hdmi *hdmi = hdmi_bridge->hdmi;
192 int hstart, hend, vstart, vend;
195 mode = adjusted_mode;
197 hdmi->pixclock = mode->clock * 1000;
199 hstart = mode->htotal - mode->hsync_start;
200 hend = mode->htotal - mode->hsync_start + mode->hdisplay;
202 vstart = mode->vtotal - mode->vsync_start - 1;
203 vend = mode->vtotal - mode->vsync_start + mode->vdisplay - 1;
205 DBG("htotal=%d, vtotal=%d, hstart=%d, hend=%d, vstart=%d, vend=%d",
206 mode->htotal, mode->vtotal, hstart, hend, vstart, vend);
208 hdmi_write(hdmi, REG_HDMI_TOTAL,
209 HDMI_TOTAL_H_TOTAL(mode->htotal - 1) |
210 HDMI_TOTAL_V_TOTAL(mode->vtotal - 1));
212 hdmi_write(hdmi, REG_HDMI_ACTIVE_HSYNC,
213 HDMI_ACTIVE_HSYNC_START(hstart) |
214 HDMI_ACTIVE_HSYNC_END(hend));
215 hdmi_write(hdmi, REG_HDMI_ACTIVE_VSYNC,
216 HDMI_ACTIVE_VSYNC_START(vstart) |
217 HDMI_ACTIVE_VSYNC_END(vend));
219 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
220 hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2,
221 HDMI_VSYNC_TOTAL_F2_V_TOTAL(mode->vtotal));
222 hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2,
223 HDMI_VSYNC_ACTIVE_F2_START(vstart + 1) |
224 HDMI_VSYNC_ACTIVE_F2_END(vend + 1));
226 hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2,
227 HDMI_VSYNC_TOTAL_F2_V_TOTAL(0));
228 hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2,
229 HDMI_VSYNC_ACTIVE_F2_START(0) |
230 HDMI_VSYNC_ACTIVE_F2_END(0));
234 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
235 frame_ctrl |= HDMI_FRAME_CTRL_HSYNC_LOW;
236 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
237 frame_ctrl |= HDMI_FRAME_CTRL_VSYNC_LOW;
238 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
239 frame_ctrl |= HDMI_FRAME_CTRL_INTERLACED_EN;
240 DBG("frame_ctrl=%08x", frame_ctrl);
241 hdmi_write(hdmi, REG_HDMI_FRAME_CTRL, frame_ctrl);
244 msm_hdmi_audio_update(hdmi);
247 static struct edid *msm_hdmi_bridge_get_edid(struct drm_bridge *bridge,
248 struct drm_connector *connector)
250 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
251 struct hdmi *hdmi = hdmi_bridge->hdmi;
255 hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
256 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
258 edid = drm_get_edid(connector, hdmi->i2c);
260 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
262 hdmi->hdmi_mode = drm_detect_hdmi_monitor(edid);
267 static enum drm_mode_status msm_hdmi_bridge_mode_valid(struct drm_bridge *bridge,
268 const struct drm_display_info *info,
269 const struct drm_display_mode *mode)
271 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
272 struct hdmi *hdmi = hdmi_bridge->hdmi;
273 const struct hdmi_platform_config *config = hdmi->config;
274 struct msm_drm_private *priv = bridge->dev->dev_private;
275 struct msm_kms *kms = priv->kms;
276 long actual, requested;
278 requested = 1000 * mode->clock;
280 /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
281 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
284 if (kms->funcs->round_pixclk)
285 actual = kms->funcs->round_pixclk(kms,
286 requested, hdmi_bridge->hdmi->encoder);
287 else if (config->pwr_clk_cnt > 0)
288 actual = clk_round_rate(hdmi->pwr_clks[0], requested);
292 DBG("requested=%ld, actual=%ld", requested, actual);
294 if (actual != requested)
295 return MODE_CLOCK_RANGE;
300 static const struct drm_bridge_funcs msm_hdmi_bridge_funcs = {
301 .pre_enable = msm_hdmi_bridge_pre_enable,
302 .post_disable = msm_hdmi_bridge_post_disable,
303 .mode_set = msm_hdmi_bridge_mode_set,
304 .mode_valid = msm_hdmi_bridge_mode_valid,
305 .get_edid = msm_hdmi_bridge_get_edid,
306 .detect = msm_hdmi_bridge_detect,
310 msm_hdmi_hotplug_work(struct work_struct *work)
312 struct hdmi_bridge *hdmi_bridge =
313 container_of(work, struct hdmi_bridge, hpd_work);
314 struct drm_bridge *bridge = &hdmi_bridge->base;
316 drm_bridge_hpd_notify(bridge, drm_bridge_detect(bridge));
319 /* initialize bridge */
320 struct drm_bridge *msm_hdmi_bridge_init(struct hdmi *hdmi)
322 struct drm_bridge *bridge = NULL;
323 struct hdmi_bridge *hdmi_bridge;
326 hdmi_bridge = devm_kzalloc(hdmi->dev->dev,
327 sizeof(*hdmi_bridge), GFP_KERNEL);
333 hdmi_bridge->hdmi = hdmi;
334 INIT_WORK(&hdmi_bridge->hpd_work, msm_hdmi_hotplug_work);
336 bridge = &hdmi_bridge->base;
337 bridge->funcs = &msm_hdmi_bridge_funcs;
338 bridge->ddc = hdmi->i2c;
339 bridge->type = DRM_MODE_CONNECTOR_HDMIA;
340 bridge->ops = DRM_BRIDGE_OP_HPD |
341 DRM_BRIDGE_OP_DETECT |
344 drm_bridge_add(bridge);
346 ret = drm_bridge_attach(hdmi->encoder, bridge, NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR);
354 msm_hdmi_bridge_destroy(bridge);