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