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_print.h> 32 #include <drm/drm_probe_helper.h> 33 34 #include "intel_connector.h" 35 #include "intel_display_core.h" 36 #include "intel_display_debugfs.h" 37 #include "intel_display_types.h" 38 #include "intel_hdcp.h" 39 #include "intel_panel.h" 40 41 static void intel_connector_modeset_retry_work_fn(struct work_struct *work) 42 { 43 struct intel_connector *connector = container_of(work, typeof(*connector), 44 modeset_retry_work); 45 struct intel_display *display = to_intel_display(connector); 46 47 drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s]\n", connector->base.base.id, 48 connector->base.name); 49 50 /* Grab the locks before changing connector property*/ 51 mutex_lock(&display->drm->mode_config.mutex); 52 /* Set connector link status to BAD and send a Uevent to notify 53 * userspace to do a modeset. 54 */ 55 drm_connector_set_link_status_property(&connector->base, 56 DRM_MODE_LINK_STATUS_BAD); 57 mutex_unlock(&display->drm->mode_config.mutex); 58 /* Send Hotplug uevent so userspace can reprobe */ 59 drm_kms_helper_connector_hotplug_event(&connector->base); 60 61 drm_connector_put(&connector->base); 62 } 63 64 void intel_connector_queue_modeset_retry_work(struct intel_connector *connector) 65 { 66 struct intel_display *display = to_intel_display(connector); 67 68 drm_connector_get(&connector->base); 69 if (!queue_work(display->wq.unordered, &connector->modeset_retry_work)) 70 drm_connector_put(&connector->base); 71 } 72 73 void intel_connector_cancel_modeset_retry_work(struct intel_connector *connector) 74 { 75 if (cancel_work_sync(&connector->modeset_retry_work)) 76 drm_connector_put(&connector->base); 77 } 78 79 static int intel_connector_init(struct intel_connector *connector) 80 { 81 struct intel_digital_connector_state *conn_state; 82 83 /* 84 * Allocate enough memory to hold intel_digital_connector_state, 85 * This might be a few bytes too many, but for connectors that don't 86 * need it we'll free the state and allocate a smaller one on the first 87 * successful commit anyway. 88 */ 89 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL); 90 if (!conn_state) 91 return -ENOMEM; 92 93 __drm_atomic_helper_connector_reset(&connector->base, 94 &conn_state->base); 95 96 intel_panel_init_alloc(connector); 97 98 INIT_WORK(&connector->modeset_retry_work, 99 intel_connector_modeset_retry_work_fn); 100 101 return 0; 102 } 103 104 struct intel_connector *intel_connector_alloc(void) 105 { 106 struct intel_connector *connector; 107 108 connector = kzalloc(sizeof(*connector), GFP_KERNEL); 109 if (!connector) 110 return NULL; 111 112 if (intel_connector_init(connector) < 0) { 113 kfree(connector); 114 return NULL; 115 } 116 117 return connector; 118 } 119 120 /* 121 * Free the bits allocated by intel_connector_alloc. 122 * This should only be used after intel_connector_alloc has returned 123 * successfully, and before drm_connector_init returns successfully. 124 * Otherwise the destroy callbacks for the connector and the state should 125 * take care of proper cleanup/free (see intel_connector_destroy). 126 */ 127 void intel_connector_free(struct intel_connector *connector) 128 { 129 kfree(to_intel_digital_connector_state(connector->base.state)); 130 kfree(connector); 131 } 132 133 /* 134 * Connector type independent destroy hook for drm_connector_funcs. 135 */ 136 void intel_connector_destroy(struct drm_connector *connector) 137 { 138 struct intel_connector *intel_connector = to_intel_connector(connector); 139 140 drm_edid_free(intel_connector->detect_edid); 141 142 intel_hdcp_cleanup(intel_connector); 143 144 intel_panel_fini(intel_connector); 145 146 drm_connector_cleanup(connector); 147 148 if (intel_connector->mst.port) 149 drm_dp_mst_put_port_malloc(intel_connector->mst.port); 150 151 kfree(connector); 152 } 153 154 int intel_connector_register(struct drm_connector *_connector) 155 { 156 struct intel_connector *connector = to_intel_connector(_connector); 157 int ret; 158 159 ret = intel_panel_register(connector); 160 if (ret) 161 return ret; 162 163 intel_connector_debugfs_add(connector); 164 165 return 0; 166 } 167 ALLOW_ERROR_INJECTION(intel_connector_register, ERRNO); 168 169 void intel_connector_unregister(struct drm_connector *_connector) 170 { 171 struct intel_connector *connector = to_intel_connector(_connector); 172 173 intel_panel_unregister(connector); 174 } 175 176 void intel_connector_attach_encoder(struct intel_connector *connector, 177 struct intel_encoder *encoder) 178 { 179 connector->encoder = encoder; 180 drm_connector_attach_encoder(&connector->base, &encoder->base); 181 } 182 183 /* 184 * Simple connector->get_hw_state implementation for encoders that support only 185 * one connector and no cloning and hence the encoder state determines the state 186 * of the connector. 187 */ 188 bool intel_connector_get_hw_state(struct intel_connector *connector) 189 { 190 enum pipe pipe = 0; 191 struct intel_encoder *encoder = intel_attached_encoder(connector); 192 193 return encoder->get_hw_state(encoder, &pipe); 194 } 195 196 enum pipe intel_connector_get_pipe(struct intel_connector *connector) 197 { 198 struct intel_display *display = to_intel_display(connector); 199 200 drm_modeset_lock_assert_held(&display->drm->mode_config.connection_mutex); 201 202 if (!connector->base.state->crtc) 203 return INVALID_PIPE; 204 205 return to_intel_crtc(connector->base.state->crtc)->pipe; 206 } 207 208 /** 209 * intel_connector_update_modes - update connector from edid 210 * @connector: DRM connector device to use 211 * @drm_edid: previously read EDID information 212 */ 213 int intel_connector_update_modes(struct drm_connector *connector, 214 const struct drm_edid *drm_edid) 215 { 216 int ret; 217 218 drm_edid_connector_update(connector, drm_edid); 219 ret = drm_edid_connector_add_modes(connector); 220 221 return ret; 222 } 223 224 /** 225 * intel_ddc_get_modes - get modelist from monitor 226 * @connector: DRM connector device to use 227 * @ddc: DDC bus i2c adapter 228 * 229 * Fetch the EDID information from @connector using the DDC bus. 230 */ 231 int intel_ddc_get_modes(struct drm_connector *connector, 232 struct i2c_adapter *ddc) 233 { 234 const struct drm_edid *drm_edid; 235 int ret; 236 237 drm_edid = drm_edid_read_ddc(connector, ddc); 238 if (!drm_edid) 239 return 0; 240 241 ret = intel_connector_update_modes(connector, drm_edid); 242 drm_edid_free(drm_edid); 243 244 return ret; 245 } 246 247 static const struct drm_prop_enum_list force_audio_names[] = { 248 { HDMI_AUDIO_OFF_DVI, "force-dvi" }, 249 { HDMI_AUDIO_OFF, "off" }, 250 { HDMI_AUDIO_AUTO, "auto" }, 251 { HDMI_AUDIO_ON, "on" }, 252 }; 253 254 void 255 intel_attach_force_audio_property(struct drm_connector *connector) 256 { 257 struct intel_display *display = to_intel_display(connector->dev); 258 struct drm_property *prop; 259 260 prop = display->properties.force_audio; 261 if (prop == NULL) { 262 prop = drm_property_create_enum(display->drm, 0, 263 "audio", 264 force_audio_names, 265 ARRAY_SIZE(force_audio_names)); 266 if (prop == NULL) 267 return; 268 269 display->properties.force_audio = prop; 270 } 271 drm_object_attach_property(&connector->base, prop, 0); 272 } 273 274 static const struct drm_prop_enum_list broadcast_rgb_names[] = { 275 { INTEL_BROADCAST_RGB_AUTO, "Automatic" }, 276 { INTEL_BROADCAST_RGB_FULL, "Full" }, 277 { INTEL_BROADCAST_RGB_LIMITED, "Limited 16:235" }, 278 }; 279 280 void 281 intel_attach_broadcast_rgb_property(struct drm_connector *connector) 282 { 283 struct intel_display *display = to_intel_display(connector->dev); 284 struct drm_property *prop; 285 286 prop = display->properties.broadcast_rgb; 287 if (prop == NULL) { 288 prop = drm_property_create_enum(display->drm, DRM_MODE_PROP_ENUM, 289 "Broadcast RGB", 290 broadcast_rgb_names, 291 ARRAY_SIZE(broadcast_rgb_names)); 292 if (prop == NULL) 293 return; 294 295 display->properties.broadcast_rgb = prop; 296 } 297 298 drm_object_attach_property(&connector->base, prop, 0); 299 } 300 301 void 302 intel_attach_aspect_ratio_property(struct drm_connector *connector) 303 { 304 if (!drm_mode_create_aspect_ratio_property(connector->dev)) 305 drm_object_attach_property(&connector->base, 306 connector->dev->mode_config.aspect_ratio_property, 307 DRM_MODE_PICTURE_ASPECT_NONE); 308 } 309 310 void 311 intel_attach_hdmi_colorspace_property(struct drm_connector *connector) 312 { 313 if (!drm_mode_create_hdmi_colorspace_property(connector, 0)) 314 drm_connector_attach_colorspace_property(connector); 315 } 316 317 void 318 intel_attach_dp_colorspace_property(struct drm_connector *connector) 319 { 320 if (!drm_mode_create_dp_colorspace_property(connector, 0)) 321 drm_connector_attach_colorspace_property(connector); 322 } 323 324 void 325 intel_attach_scaling_mode_property(struct drm_connector *connector) 326 { 327 struct intel_display *display = to_intel_display(connector->dev); 328 u32 scaling_modes; 329 330 scaling_modes = BIT(DRM_MODE_SCALE_ASPECT) | 331 BIT(DRM_MODE_SCALE_FULLSCREEN); 332 333 /* On GMCH platforms borders are only possible on the LVDS port */ 334 if (!HAS_GMCH(display) || connector->connector_type == DRM_MODE_CONNECTOR_LVDS) 335 scaling_modes |= BIT(DRM_MODE_SCALE_CENTER); 336 337 drm_connector_attach_scaling_mode_property(connector, scaling_modes); 338 339 connector->state->scaling_mode = DRM_MODE_SCALE_ASPECT; 340 } 341