1 /* 2 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 3 * Copyright (c) 2007, 2010 Intel Corporation 4 * Jesse Barnes <jesse.barnes@intel.com> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 */ 25 26 #include <linux/i2c.h> 27 #include <linux/slab.h> 28 29 #include <drm/drm_atomic_helper.h> 30 #include <drm/drm_edid.h> 31 #include <drm/drm_probe_helper.h> 32 33 #include "i915_drv.h" 34 #include "i915_utils.h" 35 #include "intel_backlight.h" 36 #include "intel_connector.h" 37 #include "intel_display_core.h" 38 #include "intel_display_debugfs.h" 39 #include "intel_display_types.h" 40 #include "intel_hdcp.h" 41 #include "intel_panel.h" 42 43 static void intel_connector_modeset_retry_work_fn(struct work_struct *work) 44 { 45 struct intel_connector *connector = container_of(work, typeof(*connector), 46 modeset_retry_work); 47 struct intel_display *display = to_intel_display(connector); 48 49 drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s]\n", connector->base.base.id, 50 connector->base.name); 51 52 /* Grab the locks before changing connector property*/ 53 mutex_lock(&display->drm->mode_config.mutex); 54 /* Set connector link status to BAD and send a Uevent to notify 55 * userspace to do a modeset. 56 */ 57 drm_connector_set_link_status_property(&connector->base, 58 DRM_MODE_LINK_STATUS_BAD); 59 mutex_unlock(&display->drm->mode_config.mutex); 60 /* Send Hotplug uevent so userspace can reprobe */ 61 drm_kms_helper_connector_hotplug_event(&connector->base); 62 63 drm_connector_put(&connector->base); 64 } 65 66 void intel_connector_queue_modeset_retry_work(struct intel_connector *connector) 67 { 68 struct drm_i915_private *i915 = to_i915(connector->base.dev); 69 70 drm_connector_get(&connector->base); 71 if (!queue_work(i915->unordered_wq, &connector->modeset_retry_work)) 72 drm_connector_put(&connector->base); 73 } 74 75 void intel_connector_cancel_modeset_retry_work(struct intel_connector *connector) 76 { 77 if (cancel_work_sync(&connector->modeset_retry_work)) 78 drm_connector_put(&connector->base); 79 } 80 81 int intel_connector_init(struct intel_connector *connector) 82 { 83 struct intel_digital_connector_state *conn_state; 84 85 /* 86 * Allocate enough memory to hold intel_digital_connector_state, 87 * This might be a few bytes too many, but for connectors that don't 88 * need it we'll free the state and allocate a smaller one on the first 89 * successful commit anyway. 90 */ 91 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL); 92 if (!conn_state) 93 return -ENOMEM; 94 95 __drm_atomic_helper_connector_reset(&connector->base, 96 &conn_state->base); 97 98 intel_panel_init_alloc(connector); 99 100 INIT_WORK(&connector->modeset_retry_work, 101 intel_connector_modeset_retry_work_fn); 102 103 return 0; 104 } 105 106 struct intel_connector *intel_connector_alloc(void) 107 { 108 struct intel_connector *connector; 109 110 connector = kzalloc(sizeof(*connector), GFP_KERNEL); 111 if (!connector) 112 return NULL; 113 114 if (intel_connector_init(connector) < 0) { 115 kfree(connector); 116 return NULL; 117 } 118 119 return connector; 120 } 121 122 /* 123 * Free the bits allocated by intel_connector_alloc. 124 * This should only be used after intel_connector_alloc has returned 125 * successfully, and before drm_connector_init returns successfully. 126 * Otherwise the destroy callbacks for the connector and the state should 127 * take care of proper cleanup/free (see intel_connector_destroy). 128 */ 129 void intel_connector_free(struct intel_connector *connector) 130 { 131 kfree(to_intel_digital_connector_state(connector->base.state)); 132 kfree(connector); 133 } 134 135 /* 136 * Connector type independent destroy hook for drm_connector_funcs. 137 */ 138 void intel_connector_destroy(struct drm_connector *connector) 139 { 140 struct intel_connector *intel_connector = to_intel_connector(connector); 141 142 drm_edid_free(intel_connector->detect_edid); 143 144 intel_hdcp_cleanup(intel_connector); 145 146 intel_panel_fini(intel_connector); 147 148 drm_connector_cleanup(connector); 149 150 if (intel_connector->mst.port) 151 drm_dp_mst_put_port_malloc(intel_connector->mst.port); 152 153 kfree(connector); 154 } 155 156 int intel_connector_register(struct drm_connector *connector) 157 { 158 struct intel_connector *intel_connector = to_intel_connector(connector); 159 struct drm_i915_private *i915 = to_i915(connector->dev); 160 int ret; 161 162 ret = intel_backlight_device_register(intel_connector); 163 if (ret) 164 goto err; 165 166 if (i915_inject_probe_failure(i915)) { 167 ret = -EFAULT; 168 goto err_backlight; 169 } 170 171 intel_connector_debugfs_add(intel_connector); 172 173 return 0; 174 175 err_backlight: 176 intel_backlight_device_unregister(intel_connector); 177 err: 178 return ret; 179 } 180 181 void intel_connector_unregister(struct drm_connector *connector) 182 { 183 struct intel_connector *intel_connector = to_intel_connector(connector); 184 185 intel_backlight_device_unregister(intel_connector); 186 } 187 188 void intel_connector_attach_encoder(struct intel_connector *connector, 189 struct intel_encoder *encoder) 190 { 191 connector->encoder = encoder; 192 drm_connector_attach_encoder(&connector->base, &encoder->base); 193 } 194 195 /* 196 * Simple connector->get_hw_state implementation for encoders that support only 197 * one connector and no cloning and hence the encoder state determines the state 198 * of the connector. 199 */ 200 bool intel_connector_get_hw_state(struct intel_connector *connector) 201 { 202 enum pipe pipe = 0; 203 struct intel_encoder *encoder = intel_attached_encoder(connector); 204 205 return encoder->get_hw_state(encoder, &pipe); 206 } 207 208 enum pipe intel_connector_get_pipe(struct intel_connector *connector) 209 { 210 struct intel_display *display = to_intel_display(connector); 211 212 drm_WARN_ON(display->drm, 213 !drm_modeset_is_locked(&display->drm->mode_config.connection_mutex)); 214 215 if (!connector->base.state->crtc) 216 return INVALID_PIPE; 217 218 return to_intel_crtc(connector->base.state->crtc)->pipe; 219 } 220 221 /** 222 * intel_connector_update_modes - update connector from edid 223 * @connector: DRM connector device to use 224 * @drm_edid: previously read EDID information 225 */ 226 int intel_connector_update_modes(struct drm_connector *connector, 227 const struct drm_edid *drm_edid) 228 { 229 int ret; 230 231 drm_edid_connector_update(connector, drm_edid); 232 ret = drm_edid_connector_add_modes(connector); 233 234 return ret; 235 } 236 237 /** 238 * intel_ddc_get_modes - get modelist from monitor 239 * @connector: DRM connector device to use 240 * @ddc: DDC bus i2c adapter 241 * 242 * Fetch the EDID information from @connector using the DDC bus. 243 */ 244 int intel_ddc_get_modes(struct drm_connector *connector, 245 struct i2c_adapter *ddc) 246 { 247 const struct drm_edid *drm_edid; 248 int ret; 249 250 drm_edid = drm_edid_read_ddc(connector, ddc); 251 if (!drm_edid) 252 return 0; 253 254 ret = intel_connector_update_modes(connector, drm_edid); 255 drm_edid_free(drm_edid); 256 257 return ret; 258 } 259 260 static const struct drm_prop_enum_list force_audio_names[] = { 261 { HDMI_AUDIO_OFF_DVI, "force-dvi" }, 262 { HDMI_AUDIO_OFF, "off" }, 263 { HDMI_AUDIO_AUTO, "auto" }, 264 { HDMI_AUDIO_ON, "on" }, 265 }; 266 267 void 268 intel_attach_force_audio_property(struct drm_connector *connector) 269 { 270 struct intel_display *display = to_intel_display(connector->dev); 271 struct drm_property *prop; 272 273 prop = display->properties.force_audio; 274 if (prop == NULL) { 275 prop = drm_property_create_enum(display->drm, 0, 276 "audio", 277 force_audio_names, 278 ARRAY_SIZE(force_audio_names)); 279 if (prop == NULL) 280 return; 281 282 display->properties.force_audio = prop; 283 } 284 drm_object_attach_property(&connector->base, prop, 0); 285 } 286 287 static const struct drm_prop_enum_list broadcast_rgb_names[] = { 288 { INTEL_BROADCAST_RGB_AUTO, "Automatic" }, 289 { INTEL_BROADCAST_RGB_FULL, "Full" }, 290 { INTEL_BROADCAST_RGB_LIMITED, "Limited 16:235" }, 291 }; 292 293 void 294 intel_attach_broadcast_rgb_property(struct drm_connector *connector) 295 { 296 struct intel_display *display = to_intel_display(connector->dev); 297 struct drm_property *prop; 298 299 prop = display->properties.broadcast_rgb; 300 if (prop == NULL) { 301 prop = drm_property_create_enum(display->drm, DRM_MODE_PROP_ENUM, 302 "Broadcast RGB", 303 broadcast_rgb_names, 304 ARRAY_SIZE(broadcast_rgb_names)); 305 if (prop == NULL) 306 return; 307 308 display->properties.broadcast_rgb = prop; 309 } 310 311 drm_object_attach_property(&connector->base, prop, 0); 312 } 313 314 void 315 intel_attach_aspect_ratio_property(struct drm_connector *connector) 316 { 317 if (!drm_mode_create_aspect_ratio_property(connector->dev)) 318 drm_object_attach_property(&connector->base, 319 connector->dev->mode_config.aspect_ratio_property, 320 DRM_MODE_PICTURE_ASPECT_NONE); 321 } 322 323 void 324 intel_attach_hdmi_colorspace_property(struct drm_connector *connector) 325 { 326 if (!drm_mode_create_hdmi_colorspace_property(connector, 0)) 327 drm_connector_attach_colorspace_property(connector); 328 } 329 330 void 331 intel_attach_dp_colorspace_property(struct drm_connector *connector) 332 { 333 if (!drm_mode_create_dp_colorspace_property(connector, 0)) 334 drm_connector_attach_colorspace_property(connector); 335 } 336 337 void 338 intel_attach_scaling_mode_property(struct drm_connector *connector) 339 { 340 struct intel_display *display = to_intel_display(connector->dev); 341 u32 scaling_modes; 342 343 scaling_modes = BIT(DRM_MODE_SCALE_ASPECT) | 344 BIT(DRM_MODE_SCALE_FULLSCREEN); 345 346 /* On GMCH platforms borders are only possible on the LVDS port */ 347 if (!HAS_GMCH(display) || connector->connector_type == DRM_MODE_CONNECTOR_LVDS) 348 scaling_modes |= BIT(DRM_MODE_SCALE_CENTER); 349 350 drm_connector_attach_scaling_mode_property(connector, scaling_modes); 351 352 connector->state->scaling_mode = DRM_MODE_SCALE_ASPECT; 353 } 354