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