1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2013 Red Hat 4 * Author: Rob Clark <robdclark@gmail.com> 5 */ 6 7 #include <linux/delay.h> 8 #include <drm/drm_bridge_connector.h> 9 #include <drm/drm_edid.h> 10 11 #include "msm_kms.h" 12 #include "hdmi.h" 13 14 void msm_hdmi_bridge_destroy(struct drm_bridge *bridge) 15 { 16 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge); 17 18 msm_hdmi_hpd_disable(hdmi_bridge); 19 drm_bridge_remove(bridge); 20 } 21 22 static void msm_hdmi_power_on(struct drm_bridge *bridge) 23 { 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; 28 int i, ret; 29 30 pm_runtime_get_sync(&hdmi->pdev->dev); 31 32 ret = regulator_bulk_enable(config->pwr_reg_cnt, hdmi->pwr_regs); 33 if (ret) 34 DRM_DEV_ERROR(dev->dev, "failed to enable pwr regulator: %d\n", ret); 35 36 if (config->pwr_clk_cnt > 0) { 37 DBG("pixclock: %lu", hdmi->pixclock); 38 ret = clk_set_rate(hdmi->pwr_clks[0], hdmi->pixclock); 39 if (ret) { 40 DRM_DEV_ERROR(dev->dev, "failed to set pixel clk: %s (%d)\n", 41 config->pwr_clk_names[0], ret); 42 } 43 } 44 45 for (i = 0; i < config->pwr_clk_cnt; i++) { 46 ret = clk_prepare_enable(hdmi->pwr_clks[i]); 47 if (ret) { 48 DRM_DEV_ERROR(dev->dev, "failed to enable pwr clk: %s (%d)\n", 49 config->pwr_clk_names[i], ret); 50 } 51 } 52 } 53 54 static void power_off(struct drm_bridge *bridge) 55 { 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; 60 int i, ret; 61 62 /* TODO do we need to wait for final vblank somewhere before 63 * cutting the clocks? 64 */ 65 mdelay(16 + 4); 66 67 for (i = 0; i < config->pwr_clk_cnt; i++) 68 clk_disable_unprepare(hdmi->pwr_clks[i]); 69 70 ret = regulator_bulk_disable(config->pwr_reg_cnt, hdmi->pwr_regs); 71 if (ret) 72 DRM_DEV_ERROR(dev->dev, "failed to disable pwr regulator: %d\n", ret); 73 74 pm_runtime_put(&hdmi->pdev->dev); 75 } 76 77 #define AVI_IFRAME_LINE_NUMBER 1 78 79 static void msm_hdmi_config_avi_infoframe(struct hdmi *hdmi) 80 { 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)]; 85 u32 val; 86 int len; 87 88 drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, 89 hdmi->connector, mode); 90 91 len = hdmi_infoframe_pack(&frame, buffer, sizeof(buffer)); 92 if (len < 0) { 93 DRM_DEV_ERROR(&hdmi->pdev->dev, 94 "failed to configure avi infoframe\n"); 95 return; 96 } 97 98 /* 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 103 */ 104 hdmi_write(hdmi, REG_HDMI_AVI_INFO(0), 105 buffer[3] | 106 buffer[4] << 8 | 107 buffer[5] << 16 | 108 buffer[6] << 24); 109 110 hdmi_write(hdmi, REG_HDMI_AVI_INFO(1), 111 buffer[7] | 112 buffer[8] << 8 | 113 buffer[9] << 16 | 114 buffer[10] << 24); 115 116 hdmi_write(hdmi, REG_HDMI_AVI_INFO(2), 117 buffer[11] | 118 buffer[12] << 8 | 119 buffer[13] << 16 | 120 buffer[14] << 24); 121 122 hdmi_write(hdmi, REG_HDMI_AVI_INFO(3), 123 buffer[15] | 124 buffer[16] << 8 | 125 buffer[1] << 24); 126 127 hdmi_write(hdmi, REG_HDMI_INFOFRAME_CTRL0, 128 HDMI_INFOFRAME_CTRL0_AVI_SEND | 129 HDMI_INFOFRAME_CTRL0_AVI_CONT); 130 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); 135 } 136 137 static void msm_hdmi_bridge_pre_enable(struct drm_bridge *bridge) 138 { 139 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge); 140 struct hdmi *hdmi = hdmi_bridge->hdmi; 141 struct hdmi_phy *phy = hdmi->phy; 142 143 DBG("power up"); 144 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); 152 } 153 } 154 155 msm_hdmi_phy_powerup(phy, hdmi->pixclock); 156 157 msm_hdmi_set_mode(hdmi, true); 158 159 if (hdmi->hdcp_ctrl) 160 msm_hdmi_hdcp_on(hdmi->hdcp_ctrl); 161 } 162 163 static void msm_hdmi_bridge_enable(struct drm_bridge *bridge) 164 { 165 } 166 167 static void msm_hdmi_bridge_disable(struct drm_bridge *bridge) 168 { 169 } 170 171 static void msm_hdmi_bridge_post_disable(struct drm_bridge *bridge) 172 { 173 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge); 174 struct hdmi *hdmi = hdmi_bridge->hdmi; 175 struct hdmi_phy *phy = hdmi->phy; 176 177 if (hdmi->hdcp_ctrl) 178 msm_hdmi_hdcp_off(hdmi->hdcp_ctrl); 179 180 DBG("power down"); 181 msm_hdmi_set_mode(hdmi, false); 182 183 msm_hdmi_phy_powerdown(phy); 184 185 if (hdmi->power_on) { 186 power_off(bridge); 187 hdmi->power_on = false; 188 if (hdmi->hdmi_mode) 189 msm_hdmi_audio_update(hdmi); 190 msm_hdmi_phy_resource_disable(phy); 191 } 192 } 193 194 static void msm_hdmi_bridge_mode_set(struct drm_bridge *bridge, 195 const struct drm_display_mode *mode, 196 const struct drm_display_mode *adjusted_mode) 197 { 198 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge); 199 struct hdmi *hdmi = hdmi_bridge->hdmi; 200 int hstart, hend, vstart, vend; 201 uint32_t frame_ctrl; 202 203 mode = adjusted_mode; 204 205 hdmi->pixclock = mode->clock * 1000; 206 207 hstart = mode->htotal - mode->hsync_start; 208 hend = mode->htotal - mode->hsync_start + mode->hdisplay; 209 210 vstart = mode->vtotal - mode->vsync_start - 1; 211 vend = mode->vtotal - mode->vsync_start + mode->vdisplay - 1; 212 213 DBG("htotal=%d, vtotal=%d, hstart=%d, hend=%d, vstart=%d, vend=%d", 214 mode->htotal, mode->vtotal, hstart, hend, vstart, vend); 215 216 hdmi_write(hdmi, REG_HDMI_TOTAL, 217 HDMI_TOTAL_H_TOTAL(mode->htotal - 1) | 218 HDMI_TOTAL_V_TOTAL(mode->vtotal - 1)); 219 220 hdmi_write(hdmi, REG_HDMI_ACTIVE_HSYNC, 221 HDMI_ACTIVE_HSYNC_START(hstart) | 222 HDMI_ACTIVE_HSYNC_END(hend)); 223 hdmi_write(hdmi, REG_HDMI_ACTIVE_VSYNC, 224 HDMI_ACTIVE_VSYNC_START(vstart) | 225 HDMI_ACTIVE_VSYNC_END(vend)); 226 227 if (mode->flags & DRM_MODE_FLAG_INTERLACE) { 228 hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2, 229 HDMI_VSYNC_TOTAL_F2_V_TOTAL(mode->vtotal)); 230 hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2, 231 HDMI_VSYNC_ACTIVE_F2_START(vstart + 1) | 232 HDMI_VSYNC_ACTIVE_F2_END(vend + 1)); 233 } else { 234 hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2, 235 HDMI_VSYNC_TOTAL_F2_V_TOTAL(0)); 236 hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2, 237 HDMI_VSYNC_ACTIVE_F2_START(0) | 238 HDMI_VSYNC_ACTIVE_F2_END(0)); 239 } 240 241 frame_ctrl = 0; 242 if (mode->flags & DRM_MODE_FLAG_NHSYNC) 243 frame_ctrl |= HDMI_FRAME_CTRL_HSYNC_LOW; 244 if (mode->flags & DRM_MODE_FLAG_NVSYNC) 245 frame_ctrl |= HDMI_FRAME_CTRL_VSYNC_LOW; 246 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 247 frame_ctrl |= HDMI_FRAME_CTRL_INTERLACED_EN; 248 DBG("frame_ctrl=%08x", frame_ctrl); 249 hdmi_write(hdmi, REG_HDMI_FRAME_CTRL, frame_ctrl); 250 251 if (hdmi->hdmi_mode) 252 msm_hdmi_audio_update(hdmi); 253 } 254 255 static struct edid *msm_hdmi_bridge_get_edid(struct drm_bridge *bridge, 256 struct drm_connector *connector) 257 { 258 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge); 259 struct hdmi *hdmi = hdmi_bridge->hdmi; 260 struct edid *edid; 261 uint32_t hdmi_ctrl; 262 263 hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL); 264 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE); 265 266 edid = drm_get_edid(connector, hdmi->i2c); 267 268 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl); 269 270 hdmi->hdmi_mode = drm_detect_hdmi_monitor(edid); 271 272 return edid; 273 } 274 275 static enum drm_mode_status msm_hdmi_bridge_mode_valid(struct drm_bridge *bridge, 276 const struct drm_display_info *info, 277 const struct drm_display_mode *mode) 278 { 279 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge); 280 struct hdmi *hdmi = hdmi_bridge->hdmi; 281 const struct hdmi_platform_config *config = hdmi->config; 282 struct msm_drm_private *priv = bridge->dev->dev_private; 283 struct msm_kms *kms = priv->kms; 284 long actual, requested; 285 286 requested = 1000 * mode->clock; 287 288 /* for mdp5/apq8074, we manage our own pixel clk (as opposed to 289 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder 290 * instead): 291 */ 292 if (kms->funcs->round_pixclk) 293 actual = kms->funcs->round_pixclk(kms, 294 requested, hdmi_bridge->hdmi->encoder); 295 else if (config->pwr_clk_cnt > 0) 296 actual = clk_round_rate(hdmi->pwr_clks[0], requested); 297 else 298 actual = requested; 299 300 DBG("requested=%ld, actual=%ld", requested, actual); 301 302 if (actual != requested) 303 return MODE_CLOCK_RANGE; 304 305 return 0; 306 } 307 308 static const struct drm_bridge_funcs msm_hdmi_bridge_funcs = { 309 .pre_enable = msm_hdmi_bridge_pre_enable, 310 .enable = msm_hdmi_bridge_enable, 311 .disable = msm_hdmi_bridge_disable, 312 .post_disable = msm_hdmi_bridge_post_disable, 313 .mode_set = msm_hdmi_bridge_mode_set, 314 .mode_valid = msm_hdmi_bridge_mode_valid, 315 .get_edid = msm_hdmi_bridge_get_edid, 316 .detect = msm_hdmi_bridge_detect, 317 }; 318 319 static void 320 msm_hdmi_hotplug_work(struct work_struct *work) 321 { 322 struct hdmi_bridge *hdmi_bridge = 323 container_of(work, struct hdmi_bridge, hpd_work); 324 struct drm_bridge *bridge = &hdmi_bridge->base; 325 326 drm_bridge_hpd_notify(bridge, drm_bridge_detect(bridge)); 327 } 328 329 /* initialize bridge */ 330 struct drm_bridge *msm_hdmi_bridge_init(struct hdmi *hdmi) 331 { 332 struct drm_bridge *bridge = NULL; 333 struct hdmi_bridge *hdmi_bridge; 334 int ret; 335 336 hdmi_bridge = devm_kzalloc(hdmi->dev->dev, 337 sizeof(*hdmi_bridge), GFP_KERNEL); 338 if (!hdmi_bridge) { 339 ret = -ENOMEM; 340 goto fail; 341 } 342 343 hdmi_bridge->hdmi = hdmi; 344 INIT_WORK(&hdmi_bridge->hpd_work, msm_hdmi_hotplug_work); 345 346 bridge = &hdmi_bridge->base; 347 bridge->funcs = &msm_hdmi_bridge_funcs; 348 bridge->ddc = hdmi->i2c; 349 bridge->type = DRM_MODE_CONNECTOR_HDMIA; 350 bridge->ops = DRM_BRIDGE_OP_HPD | 351 DRM_BRIDGE_OP_DETECT | 352 DRM_BRIDGE_OP_EDID; 353 354 drm_bridge_add(bridge); 355 356 ret = drm_bridge_attach(hdmi->encoder, bridge, NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR); 357 if (ret) 358 goto fail; 359 360 return bridge; 361 362 fail: 363 if (bridge) 364 msm_hdmi_bridge_destroy(bridge); 365 366 return ERR_PTR(ret); 367 } 368