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