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