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