blob: bd0985a4ff647d18f34ea9e10dfb5115d2b9a91f [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2008 Maarten Maathuis.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
Ben Skeggsa1470892010-01-18 11:42:37 +100027#include <acpi/button.h>
28
Ben Skeggs6ee73862009-12-11 19:24:15 +100029#include "drmP.h"
30#include "drm_edid.h"
31#include "drm_crtc_helper.h"
Ben Skeggsa1470892010-01-18 11:42:37 +100032
Ben Skeggs6ee73862009-12-11 19:24:15 +100033#include "nouveau_reg.h"
34#include "nouveau_drv.h"
35#include "nouveau_encoder.h"
36#include "nouveau_crtc.h"
37#include "nouveau_connector.h"
38#include "nouveau_hw.h"
39
Ben Skeggse0996ae2012-07-10 12:20:17 +100040#include <subdev/bios/gpio.h>
41
Ben Skeggsfce2bad2010-11-11 16:14:56 +100042static void nouveau_connector_hotplug(void *, int);
43
Ben Skeggs10b461e2011-08-02 19:29:37 +100044struct nouveau_encoder *
Ben Skeggse19b20b2011-07-12 13:32:07 +100045find_encoder(struct drm_connector *connector, int type)
Ben Skeggs6ee73862009-12-11 19:24:15 +100046{
47 struct drm_device *dev = connector->dev;
48 struct nouveau_encoder *nv_encoder;
49 struct drm_mode_object *obj;
50 int i, id;
51
52 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
53 id = connector->encoder_ids[i];
54 if (!id)
55 break;
56
57 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
58 if (!obj)
59 continue;
60 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
61
Ben Skeggscb75d972012-07-11 10:44:20 +100062 if (type == DCB_OUTPUT_ANY || nv_encoder->dcb->type == type)
Ben Skeggs6ee73862009-12-11 19:24:15 +100063 return nv_encoder;
64 }
65
66 return NULL;
67}
68
69struct nouveau_connector *
70nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
71{
72 struct drm_device *dev = to_drm_encoder(encoder)->dev;
73 struct drm_connector *drm_connector;
74
75 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
76 if (drm_connector->encoder == to_drm_encoder(encoder))
77 return nouveau_connector(drm_connector);
78 }
79
80 return NULL;
81}
82
Ben Skeggs6ee73862009-12-11 19:24:15 +100083static void
Ben Skeggsfce2bad2010-11-11 16:14:56 +100084nouveau_connector_destroy(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +100085{
Ben Skeggsfce2bad2010-11-11 16:14:56 +100086 struct nouveau_connector *nv_connector = nouveau_connector(connector);
87 struct drm_nouveau_private *dev_priv;
Marcin Slusarzdd19e442010-01-30 15:41:00 +010088 struct drm_device *dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +100089
Xavier Chantryc8ebe272010-01-11 22:42:21 +010090 if (!nv_connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +100091 return;
92
Marcin Slusarzdd19e442010-01-30 15:41:00 +010093 dev = nv_connector->base.dev;
Ben Skeggsfce2bad2010-11-11 16:14:56 +100094 dev_priv = dev->dev_private;
Marcin Slusarzdd19e442010-01-30 15:41:00 +010095 NV_DEBUG_KMS(dev, "\n");
96
Ben Skeggsa0b25632011-11-21 16:41:48 +100097 if (nv_connector->hpd != DCB_GPIO_UNUSED) {
98 nouveau_gpio_isr_del(dev, 0, nv_connector->hpd, 0xff,
99 nouveau_connector_hotplug, connector);
Ben Skeggsfce2bad2010-11-11 16:14:56 +1000100 }
101
Xavier Chantryc8ebe272010-01-11 22:42:21 +0100102 kfree(nv_connector->edid);
Ben Skeggsfce2bad2010-11-11 16:14:56 +1000103 drm_sysfs_connector_remove(connector);
104 drm_connector_cleanup(connector);
105 kfree(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000106}
107
Ben Skeggs4196faa2012-07-10 14:36:38 +1000108static struct nouveau_i2c_port *
Ben Skeggs6ee73862009-12-11 19:24:15 +1000109nouveau_connector_ddc_detect(struct drm_connector *connector,
110 struct nouveau_encoder **pnv_encoder)
111{
112 struct drm_device *dev = connector->dev;
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200113 int i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000114
Ben Skeggs6ee73862009-12-11 19:24:15 +1000115 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
Ben Skeggs4196faa2012-07-10 14:36:38 +1000116 struct nouveau_i2c_port *i2c = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000117 struct nouveau_encoder *nv_encoder;
118 struct drm_mode_object *obj;
119 int id;
120
121 id = connector->encoder_ids[i];
122 if (!id)
123 break;
124
125 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
126 if (!obj)
127 continue;
128 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
Francisco Jerez4ca2b712010-08-08 21:35:57 +0200129
130 if (nv_encoder->dcb->i2c_index < 0xf)
131 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000132
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200133 if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000134 *pnv_encoder = nv_encoder;
135 return i2c;
136 }
137 }
138
139 return NULL;
140}
141
Francisco Jerezc16c5702010-09-09 14:33:17 +0200142static struct nouveau_encoder *
143nouveau_connector_of_detect(struct drm_connector *connector)
144{
145#ifdef __powerpc__
146 struct drm_device *dev = connector->dev;
147 struct nouveau_connector *nv_connector = nouveau_connector(connector);
148 struct nouveau_encoder *nv_encoder;
149 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
150
151 if (!dn ||
Ben Skeggscb75d972012-07-11 10:44:20 +1000152 !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
153 (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
Francisco Jerezc16c5702010-09-09 14:33:17 +0200154 return NULL;
155
156 for_each_child_of_node(dn, cn) {
157 const char *name = of_get_property(cn, "name", NULL);
158 const void *edid = of_get_property(cn, "EDID", NULL);
159 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
160
161 if (nv_encoder->dcb->i2c_index == idx && edid) {
162 nv_connector->edid =
163 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
164 of_node_put(cn);
165 return nv_encoder;
166 }
167 }
168#endif
169 return NULL;
170}
171
Ben Skeggs6ee73862009-12-11 19:24:15 +1000172static void
173nouveau_connector_set_encoder(struct drm_connector *connector,
174 struct nouveau_encoder *nv_encoder)
175{
176 struct nouveau_connector *nv_connector = nouveau_connector(connector);
177 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
178 struct drm_device *dev = connector->dev;
179
180 if (nv_connector->detected_encoder == nv_encoder)
181 return;
182 nv_connector->detected_encoder = nv_encoder;
183
Ben Skeggsc8334422011-10-19 13:06:48 +1000184 if (dev_priv->card_type >= NV_50) {
185 connector->interlace_allowed = true;
186 connector->doublescan_allowed = true;
187 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000188 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
189 nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000190 connector->doublescan_allowed = false;
191 connector->interlace_allowed = false;
192 } else {
193 connector->doublescan_allowed = true;
194 if (dev_priv->card_type == NV_20 ||
195 (dev_priv->card_type == NV_10 &&
196 (dev->pci_device & 0x0ff0) != 0x0100 &&
197 (dev->pci_device & 0x0ff0) != 0x0150))
198 /* HW is broken */
199 connector->interlace_allowed = false;
200 else
201 connector->interlace_allowed = true;
202 }
203
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000204 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000205 drm_connector_property_set_value(connector,
206 dev->mode_config.dvi_i_subconnector_property,
Ben Skeggscb75d972012-07-11 10:44:20 +1000207 nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
Ben Skeggs6ee73862009-12-11 19:24:15 +1000208 DRM_MODE_SUBCONNECTOR_DVID :
209 DRM_MODE_SUBCONNECTOR_DVIA);
210 }
211}
212
213static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100214nouveau_connector_detect(struct drm_connector *connector, bool force)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000215{
216 struct drm_device *dev = connector->dev;
217 struct nouveau_connector *nv_connector = nouveau_connector(connector);
218 struct nouveau_encoder *nv_encoder = NULL;
Ben Skeggse19b20b2011-07-12 13:32:07 +1000219 struct nouveau_encoder *nv_partner;
Ben Skeggs4196faa2012-07-10 14:36:38 +1000220 struct nouveau_i2c_port *i2c;
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200221 int type;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222
Francisco Jerezb8780e22010-01-14 15:54:23 +0100223 /* Cleanup the previous EDID block. */
224 if (nv_connector->edid) {
225 drm_mode_connector_update_edid_property(connector, NULL);
226 kfree(nv_connector->edid);
227 nv_connector->edid = NULL;
228 }
Xavier Chantryc8ebe272010-01-11 22:42:21 +0100229
Ben Skeggs6ee73862009-12-11 19:24:15 +1000230 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
231 if (i2c) {
Ben Skeggs4196faa2012-07-10 14:36:38 +1000232 nv_connector->edid = drm_get_edid(connector, nouveau_i2c_adapter(i2c));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000233 drm_mode_connector_update_edid_property(connector,
234 nv_connector->edid);
235 if (!nv_connector->edid) {
236 NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
237 drm_get_connector_name(connector));
Francisco Jerez0ed31652010-01-14 15:47:03 +0100238 goto detect_analog;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000239 }
240
Ben Skeggscb75d972012-07-11 10:44:20 +1000241 if (nv_encoder->dcb->type == DCB_OUTPUT_DP &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000242 !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
243 NV_ERROR(dev, "Detected %s, but failed init\n",
244 drm_get_connector_name(connector));
245 return connector_status_disconnected;
246 }
247
248 /* Override encoder type for DVI-I based on whether EDID
249 * says the display is digital or analog, both use the
250 * same i2c channel so the value returned from ddc_detect
251 * isn't necessarily correct.
252 */
Ben Skeggse19b20b2011-07-12 13:32:07 +1000253 nv_partner = NULL;
Ben Skeggscb75d972012-07-11 10:44:20 +1000254 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
255 nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
256 if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
257 nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
Ben Skeggse19b20b2011-07-12 13:32:07 +1000258
Ben Skeggscb75d972012-07-11 10:44:20 +1000259 if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
260 nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
261 (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
262 nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000263 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
Ben Skeggscb75d972012-07-11 10:44:20 +1000264 type = DCB_OUTPUT_TMDS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000265 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000266 type = DCB_OUTPUT_ANALOG;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000267
Ben Skeggse19b20b2011-07-12 13:32:07 +1000268 nv_encoder = find_encoder(connector, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000269 }
270
271 nouveau_connector_set_encoder(connector, nv_encoder);
272 return connector_status_connected;
273 }
274
Francisco Jerezc16c5702010-09-09 14:33:17 +0200275 nv_encoder = nouveau_connector_of_detect(connector);
276 if (nv_encoder) {
277 nouveau_connector_set_encoder(connector, nv_encoder);
278 return connector_status_connected;
279 }
280
Francisco Jerez0ed31652010-01-14 15:47:03 +0100281detect_analog:
Ben Skeggscb75d972012-07-11 10:44:20 +1000282 nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
Ben Skeggsf4053502010-03-15 09:43:51 +1000283 if (!nv_encoder && !nouveau_tv_disable)
Ben Skeggscb75d972012-07-11 10:44:20 +1000284 nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
Francisco Jerez84b80812010-10-26 02:17:56 +0200285 if (nv_encoder && force) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000286 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
287 struct drm_encoder_helper_funcs *helper =
288 encoder->helper_private;
289
290 if (helper->detect(encoder, connector) ==
291 connector_status_connected) {
292 nouveau_connector_set_encoder(connector, nv_encoder);
293 return connector_status_connected;
294 }
295
296 }
297
298 return connector_status_disconnected;
299}
300
Ben Skeggsd17f3952010-06-01 13:32:42 +1000301static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100302nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
Ben Skeggsd17f3952010-06-01 13:32:42 +1000303{
304 struct drm_device *dev = connector->dev;
305 struct drm_nouveau_private *dev_priv = dev->dev_private;
306 struct nouveau_connector *nv_connector = nouveau_connector(connector);
307 struct nouveau_encoder *nv_encoder = NULL;
308 enum drm_connector_status status = connector_status_disconnected;
309
310 /* Cleanup the previous EDID block. */
311 if (nv_connector->edid) {
312 drm_mode_connector_update_edid_property(connector, NULL);
313 kfree(nv_connector->edid);
314 nv_connector->edid = NULL;
315 }
316
Ben Skeggscb75d972012-07-11 10:44:20 +1000317 nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000318 if (!nv_encoder)
319 return connector_status_disconnected;
320
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000321 /* Try retrieving EDID via DDC */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000322 if (!dev_priv->vbios.fp_no_ddc) {
Chris Wilson930a9e22010-09-14 11:07:23 +0100323 status = nouveau_connector_detect(connector, force);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000324 if (status == connector_status_connected)
325 goto out;
326 }
327
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000328 /* On some laptops (Sony, i'm looking at you) there appears to
329 * be no direct way of accessing the panel's EDID. The only
330 * option available to us appears to be to ask ACPI for help..
331 *
332 * It's important this check's before trying straps, one of the
333 * said manufacturer's laptops are configured in such a way
334 * the nouveau decides an entry in the VBIOS FP mode table is
335 * valid - it's not (rh#613284)
336 */
337 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
338 if (!nouveau_acpi_edid(dev, connector)) {
339 status = connector_status_connected;
340 goto out;
341 }
342 }
343
Ben Skeggsd17f3952010-06-01 13:32:42 +1000344 /* If no EDID found above, and the VBIOS indicates a hardcoded
345 * modeline is avalilable for the panel, set it as the panel's
346 * native mode and exit.
347 */
348 if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
349 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
350 status = connector_status_connected;
351 goto out;
352 }
353
354 /* Still nothing, some VBIOS images have a hardcoded EDID block
355 * stored for the panel stored in them.
356 */
357 if (!dev_priv->vbios.fp_no_ddc) {
358 struct edid *edid =
359 (struct edid *)nouveau_bios_embedded_edid(dev);
360 if (edid) {
361 nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
362 *(nv_connector->edid) = *edid;
363 status = connector_status_connected;
364 }
365 }
366
367out:
368#if defined(CONFIG_ACPI_BUTTON) || \
369 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
370 if (status == connector_status_connected &&
371 !nouveau_ignorelid && !acpi_lid_open())
372 status = connector_status_unknown;
373#endif
374
375 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
Albert Damen3195c5f2010-06-20 16:57:57 +0200376 nouveau_connector_set_encoder(connector, nv_encoder);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000377 return status;
378}
379
Ben Skeggs6ee73862009-12-11 19:24:15 +1000380static void
381nouveau_connector_force(struct drm_connector *connector)
382{
Ben Skeggsbe079e92010-02-24 14:01:40 +1000383 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000384 struct nouveau_encoder *nv_encoder;
385 int type;
386
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000387 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000388 if (connector->force == DRM_FORCE_ON_DIGITAL)
Ben Skeggscb75d972012-07-11 10:44:20 +1000389 type = DCB_OUTPUT_TMDS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000390 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000391 type = DCB_OUTPUT_ANALOG;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000392 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000393 type = DCB_OUTPUT_ANY;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000394
Ben Skeggse19b20b2011-07-12 13:32:07 +1000395 nv_encoder = find_encoder(connector, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000396 if (!nv_encoder) {
Ben Skeggsbe079e92010-02-24 14:01:40 +1000397 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000398 drm_get_connector_name(connector));
399 connector->status = connector_status_disconnected;
400 return;
401 }
402
403 nouveau_connector_set_encoder(connector, nv_encoder);
404}
405
406static int
407nouveau_connector_set_property(struct drm_connector *connector,
408 struct drm_property *property, uint64_t value)
409{
Ben Skeggsb29caa52011-10-06 13:29:05 +1000410 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
411 struct nouveau_display_engine *disp = &dev_priv->engine.display;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000412 struct nouveau_connector *nv_connector = nouveau_connector(connector);
413 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200414 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000415 struct drm_device *dev = connector->dev;
Ben Skeggsb29caa52011-10-06 13:29:05 +1000416 struct nouveau_crtc *nv_crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000417 int ret;
418
Ben Skeggsb29caa52011-10-06 13:29:05 +1000419 nv_crtc = NULL;
420 if (connector->encoder && connector->encoder->crtc)
421 nv_crtc = nouveau_crtc(connector->encoder->crtc);
422
Ben Skeggs6ee73862009-12-11 19:24:15 +1000423 /* Scaling mode */
424 if (property == dev->mode_config.scaling_mode_property) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000425 bool modeset = false;
426
427 switch (value) {
428 case DRM_MODE_SCALE_NONE:
429 case DRM_MODE_SCALE_FULLSCREEN:
430 case DRM_MODE_SCALE_CENTER:
431 case DRM_MODE_SCALE_ASPECT:
432 break;
433 default:
434 return -EINVAL;
435 }
436
437 /* LVDS always needs gpu scaling */
Ben Skeggs8c3f6bb2011-04-18 09:57:48 +1000438 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000439 value == DRM_MODE_SCALE_NONE)
440 return -EINVAL;
441
442 /* Changing between GPU and panel scaling requires a full
443 * modeset
444 */
445 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
446 (value == DRM_MODE_SCALE_NONE))
447 modeset = true;
448 nv_connector->scaling_mode = value;
449
Ben Skeggs6ee73862009-12-11 19:24:15 +1000450 if (!nv_crtc)
451 return 0;
452
453 if (modeset || !nv_crtc->set_scale) {
454 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
455 &nv_crtc->base.mode,
456 nv_crtc->base.x,
457 nv_crtc->base.y, NULL);
458 if (!ret)
459 return -EINVAL;
460 } else {
Ben Skeggs488ff202011-10-17 10:38:10 +1000461 ret = nv_crtc->set_scale(nv_crtc, true);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000462 if (ret)
463 return ret;
464 }
465
466 return 0;
467 }
468
Ben Skeggsb29caa52011-10-06 13:29:05 +1000469 /* Underscan */
470 if (property == disp->underscan_property) {
471 if (nv_connector->underscan != value) {
472 nv_connector->underscan = value;
473 if (!nv_crtc || !nv_crtc->set_scale)
474 return 0;
475
Ben Skeggs488ff202011-10-17 10:38:10 +1000476 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000477 }
478
479 return 0;
480 }
481
482 if (property == disp->underscan_hborder_property) {
483 if (nv_connector->underscan_hborder != value) {
484 nv_connector->underscan_hborder = value;
485 if (!nv_crtc || !nv_crtc->set_scale)
486 return 0;
487
Ben Skeggs488ff202011-10-17 10:38:10 +1000488 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000489 }
490
491 return 0;
492 }
493
494 if (property == disp->underscan_vborder_property) {
495 if (nv_connector->underscan_vborder != value) {
496 nv_connector->underscan_vborder = value;
497 if (!nv_crtc || !nv_crtc->set_scale)
498 return 0;
499
Ben Skeggs488ff202011-10-17 10:38:10 +1000500 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000501 }
502
503 return 0;
504 }
505
Ben Skeggs6ee73862009-12-11 19:24:15 +1000506 /* Dithering */
Ben Skeggsde691852011-10-17 12:23:41 +1000507 if (property == disp->dithering_mode) {
508 nv_connector->dithering_mode = value;
509 if (!nv_crtc || !nv_crtc->set_dither)
510 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000511
Ben Skeggsde691852011-10-17 12:23:41 +1000512 return nv_crtc->set_dither(nv_crtc, true);
513 }
514
515 if (property == disp->dithering_depth) {
516 nv_connector->dithering_depth = value;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000517 if (!nv_crtc || !nv_crtc->set_dither)
518 return 0;
519
Ben Skeggs488ff202011-10-17 10:38:10 +1000520 return nv_crtc->set_dither(nv_crtc, true);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000521 }
522
Christoph Bumillerdf26bc92012-01-21 23:13:26 +0100523 if (nv_crtc && nv_crtc->set_color_vibrance) {
524 /* Hue */
525 if (property == disp->vibrant_hue_property) {
526 nv_crtc->vibrant_hue = value - 90;
527 return nv_crtc->set_color_vibrance(nv_crtc, true);
528 }
529 /* Saturation */
530 if (property == disp->color_vibrance_property) {
531 nv_crtc->color_vibrance = value - 100;
532 return nv_crtc->set_color_vibrance(nv_crtc, true);
533 }
534 }
535
Ben Skeggscb75d972012-07-11 10:44:20 +1000536 if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200537 return get_slave_funcs(encoder)->set_property(
538 encoder, connector, property, value);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000539
540 return -EINVAL;
541}
542
543static struct drm_display_mode *
Ben Skeggs26099a72010-05-28 10:13:16 +1000544nouveau_connector_native_mode(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000545{
Ben Skeggs26099a72010-05-28 10:13:16 +1000546 struct drm_connector_helper_funcs *helper = connector->helper_private;
547 struct nouveau_connector *nv_connector = nouveau_connector(connector);
548 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000549 struct drm_display_mode *mode, *largest = NULL;
550 int high_w = 0, high_h = 0, high_v = 0;
551
Ben Skeggs26099a72010-05-28 10:13:16 +1000552 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
Ben Skeggs0d9b6192011-02-07 08:41:18 +1000553 mode->vrefresh = drm_mode_vrefresh(mode);
Francisco Jereza5afb772010-07-18 16:19:16 +0200554 if (helper->mode_valid(connector, mode) != MODE_OK ||
555 (mode->flags & DRM_MODE_FLAG_INTERLACE))
Ben Skeggs26099a72010-05-28 10:13:16 +1000556 continue;
557
558 /* Use preferred mode if there is one.. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000559 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100560 NV_DEBUG_KMS(dev, "native mode from preferred\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000561 return drm_mode_duplicate(dev, mode);
562 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000563
Ben Skeggs26099a72010-05-28 10:13:16 +1000564 /* Otherwise, take the resolution with the largest width, then
565 * height, then vertical refresh
566 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000567 if (mode->hdisplay < high_w)
568 continue;
569
570 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
571 continue;
572
573 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
574 mode->vrefresh < high_v)
575 continue;
576
577 high_w = mode->hdisplay;
578 high_h = mode->vdisplay;
579 high_v = mode->vrefresh;
580 largest = mode;
581 }
582
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100583 NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000584 high_w, high_h, high_v);
585 return largest ? drm_mode_duplicate(dev, largest) : NULL;
586}
587
588struct moderec {
589 int hdisplay;
590 int vdisplay;
591};
592
593static struct moderec scaler_modes[] = {
594 { 1920, 1200 },
595 { 1920, 1080 },
596 { 1680, 1050 },
597 { 1600, 1200 },
598 { 1400, 1050 },
599 { 1280, 1024 },
600 { 1280, 960 },
601 { 1152, 864 },
602 { 1024, 768 },
603 { 800, 600 },
604 { 720, 400 },
605 { 640, 480 },
606 { 640, 400 },
607 { 640, 350 },
608 {}
609};
610
611static int
612nouveau_connector_scaler_modes_add(struct drm_connector *connector)
613{
614 struct nouveau_connector *nv_connector = nouveau_connector(connector);
615 struct drm_display_mode *native = nv_connector->native_mode, *m;
616 struct drm_device *dev = connector->dev;
617 struct moderec *mode = &scaler_modes[0];
618 int modes = 0;
619
620 if (!native)
621 return 0;
622
623 while (mode->hdisplay) {
624 if (mode->hdisplay <= native->hdisplay &&
625 mode->vdisplay <= native->vdisplay) {
626 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
627 drm_mode_vrefresh(native), false,
628 false, false);
629 if (!m)
630 continue;
631
632 m->type |= DRM_MODE_TYPE_DRIVER;
633
634 drm_mode_probed_add(connector, m);
635 modes++;
636 }
637
638 mode++;
639 }
640
641 return modes;
642}
643
Ben Skeggs63221752011-10-17 10:24:49 +1000644static void
645nouveau_connector_detect_depth(struct drm_connector *connector)
646{
647 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
648 struct nouveau_connector *nv_connector = nouveau_connector(connector);
649 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
650 struct nvbios *bios = &dev_priv->vbios;
651 struct drm_display_mode *mode = nv_connector->native_mode;
652 bool duallink;
653
654 /* if the edid is feeling nice enough to provide this info, use it */
655 if (nv_connector->edid && connector->display_info.bpc)
656 return;
657
Ben Skeggsa6a17852012-05-05 00:39:21 +1000658 /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
659 if (nv_connector->type == DCB_CONNECTOR_eDP) {
660 connector->display_info.bpc = 6;
661 return;
662 }
663
664 /* we're out of options unless we're LVDS, default to 8bpc */
Ben Skeggscb75d972012-07-11 10:44:20 +1000665 if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
Ben Skeggsc8435362012-03-24 16:26:13 +1000666 connector->display_info.bpc = 8;
Ben Skeggs63221752011-10-17 10:24:49 +1000667 return;
Ben Skeggsc8435362012-03-24 16:26:13 +1000668 }
669
670 connector->display_info.bpc = 6;
Ben Skeggs63221752011-10-17 10:24:49 +1000671
672 /* LVDS: panel straps */
673 if (bios->fp_no_ddc) {
674 if (bios->fp.if_is_24bit)
675 connector->display_info.bpc = 8;
676 return;
677 }
678
679 /* LVDS: DDC panel, need to first determine the number of links to
680 * know which if_is_24bit flag to check...
681 */
682 if (nv_connector->edid &&
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000683 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
Ben Skeggs63221752011-10-17 10:24:49 +1000684 duallink = ((u8 *)nv_connector->edid)[121] == 2;
685 else
686 duallink = mode->clock >= bios->fp.duallink_transition_clk;
687
688 if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
689 ( duallink && (bios->fp.strapless_is_24bit & 2)))
690 connector->display_info.bpc = 8;
691}
692
Ben Skeggs6ee73862009-12-11 19:24:15 +1000693static int
694nouveau_connector_get_modes(struct drm_connector *connector)
695{
696 struct drm_device *dev = connector->dev;
Ben Skeggsd17f3952010-06-01 13:32:42 +1000697 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000698 struct nouveau_connector *nv_connector = nouveau_connector(connector);
699 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200700 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000701 int ret = 0;
702
Ben Skeggsd17f3952010-06-01 13:32:42 +1000703 /* destroy the native mode, the attached monitor could have changed.
Ben Skeggs6ee73862009-12-11 19:24:15 +1000704 */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000705 if (nv_connector->native_mode) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000706 drm_mode_destroy(dev, nv_connector->native_mode);
707 nv_connector->native_mode = NULL;
708 }
709
710 if (nv_connector->edid)
711 ret = drm_add_edid_modes(connector, nv_connector->edid);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000712 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000713 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
Ben Skeggsd17f3952010-06-01 13:32:42 +1000714 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
Francisco Jerez6e86e042010-07-03 18:36:39 +0200715 dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
Ben Skeggs80dad862010-09-23 16:37:39 +1000716 struct drm_display_mode mode;
717
718 nouveau_bios_fp_mode(dev, &mode);
719 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000720 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000721
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000722 /* Determine display colour depth for everything except LVDS now,
723 * DP requires this before mode_valid() is called.
724 */
725 if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
726 nouveau_connector_detect_depth(connector);
727
Ben Skeggs6ee73862009-12-11 19:24:15 +1000728 /* Find the native mode if this is a digital panel, if we didn't
729 * find any modes through DDC previously add the native mode to
730 * the list of modes.
731 */
732 if (!nv_connector->native_mode)
733 nv_connector->native_mode =
Ben Skeggs26099a72010-05-28 10:13:16 +1000734 nouveau_connector_native_mode(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000735 if (ret == 0 && nv_connector->native_mode) {
736 struct drm_display_mode *mode;
737
738 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
739 drm_mode_probed_add(connector, mode);
740 ret = 1;
741 }
742
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000743 /* Determine LVDS colour depth, must happen after determining
744 * "native" mode as some VBIOS tables require us to use the
745 * pixel clock as part of the lookup...
Ben Skeggs63221752011-10-17 10:24:49 +1000746 */
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000747 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
748 nouveau_connector_detect_depth(connector);
Ben Skeggs63221752011-10-17 10:24:49 +1000749
Ben Skeggscb75d972012-07-11 10:44:20 +1000750 if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200751 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000752
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000753 if (nv_connector->type == DCB_CONNECTOR_LVDS ||
754 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
755 nv_connector->type == DCB_CONNECTOR_eDP)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000756 ret += nouveau_connector_scaler_modes_add(connector);
757
758 return ret;
759}
760
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200761static unsigned
762get_tmds_link_bandwidth(struct drm_connector *connector)
763{
764 struct nouveau_connector *nv_connector = nouveau_connector(connector);
765 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
Ben Skeggscb75d972012-07-11 10:44:20 +1000766 struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200767
768 if (dcb->location != DCB_LOC_ON_CHIP ||
769 dev_priv->chipset >= 0x46)
770 return 165000;
771 else if (dev_priv->chipset >= 0x40)
772 return 155000;
773 else if (dev_priv->chipset >= 0x18)
774 return 135000;
775 else
776 return 112000;
777}
778
Ben Skeggs6ee73862009-12-11 19:24:15 +1000779static int
780nouveau_connector_mode_valid(struct drm_connector *connector,
781 struct drm_display_mode *mode)
782{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000783 struct nouveau_connector *nv_connector = nouveau_connector(connector);
784 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200785 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000786 unsigned min_clock = 25000, max_clock = min_clock;
787 unsigned clock = mode->clock;
788
789 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000790 case DCB_OUTPUT_LVDS:
Ben Skeggs26099a72010-05-28 10:13:16 +1000791 if (nv_connector->native_mode &&
792 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
793 mode->vdisplay > nv_connector->native_mode->vdisplay))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000794 return MODE_PANEL;
795
796 min_clock = 0;
797 max_clock = 400000;
798 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000799 case DCB_OUTPUT_TMDS:
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200800 max_clock = get_tmds_link_bandwidth(connector);
801 if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
802 max_clock *= 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000803 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000804 case DCB_OUTPUT_ANALOG:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000805 max_clock = nv_encoder->dcb->crtconf.maxfreq;
806 if (!max_clock)
807 max_clock = 350000;
808 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000809 case DCB_OUTPUT_TV:
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200810 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
Ben Skeggscb75d972012-07-11 10:44:20 +1000811 case DCB_OUTPUT_DP:
Ben Skeggs75a1fcc2011-08-04 09:55:44 +1000812 max_clock = nv_encoder->dp.link_nr;
813 max_clock *= nv_encoder->dp.link_bw;
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000814 clock = clock * (connector->display_info.bpc * 3) / 10;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000815 break;
Ben Skeggse7cc51c2010-02-24 10:31:39 +1000816 default:
817 BUG_ON(1);
818 return MODE_BAD;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000819 }
820
821 if (clock < min_clock)
822 return MODE_CLOCK_LOW;
823
824 if (clock > max_clock)
825 return MODE_CLOCK_HIGH;
826
827 return MODE_OK;
828}
829
830static struct drm_encoder *
831nouveau_connector_best_encoder(struct drm_connector *connector)
832{
833 struct nouveau_connector *nv_connector = nouveau_connector(connector);
834
835 if (nv_connector->detected_encoder)
836 return to_drm_encoder(nv_connector->detected_encoder);
837
838 return NULL;
839}
840
841static const struct drm_connector_helper_funcs
842nouveau_connector_helper_funcs = {
843 .get_modes = nouveau_connector_get_modes,
844 .mode_valid = nouveau_connector_mode_valid,
845 .best_encoder = nouveau_connector_best_encoder,
846};
847
848static const struct drm_connector_funcs
849nouveau_connector_funcs = {
850 .dpms = drm_helper_connector_dpms,
851 .save = NULL,
852 .restore = NULL,
853 .detect = nouveau_connector_detect,
854 .destroy = nouveau_connector_destroy,
855 .fill_modes = drm_helper_probe_single_connector_modes,
856 .set_property = nouveau_connector_set_property,
857 .force = nouveau_connector_force
858};
859
Ben Skeggsd17f3952010-06-01 13:32:42 +1000860static const struct drm_connector_funcs
861nouveau_connector_funcs_lvds = {
862 .dpms = drm_helper_connector_dpms,
863 .save = NULL,
864 .restore = NULL,
865 .detect = nouveau_connector_detect_lvds,
866 .destroy = nouveau_connector_destroy,
867 .fill_modes = drm_helper_probe_single_connector_modes,
868 .set_property = nouveau_connector_set_property,
869 .force = nouveau_connector_force
870};
Ben Skeggs6ee73862009-12-11 19:24:15 +1000871
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000872static int
873drm_conntype_from_dcb(enum dcb_connector_type dcb)
874{
875 switch (dcb) {
876 case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
877 case DCB_CONNECTOR_TV_0 :
878 case DCB_CONNECTOR_TV_1 :
879 case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
Ben Skeggsfa2c1132012-03-22 11:27:15 +1000880 case DCB_CONNECTOR_DMS59_0 :
881 case DCB_CONNECTOR_DMS59_1 :
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000882 case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
883 case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
884 case DCB_CONNECTOR_LVDS :
885 case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
Ben Skeggs4abb4102012-01-12 16:17:16 +1000886 case DCB_CONNECTOR_DMS59_DP0:
887 case DCB_CONNECTOR_DMS59_DP1:
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000888 case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort;
889 case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
890 case DCB_CONNECTOR_HDMI_0 :
891 case DCB_CONNECTOR_HDMI_1 : return DRM_MODE_CONNECTOR_HDMIA;
892 default:
893 break;
894 }
895
896 return DRM_MODE_CONNECTOR_Unknown;
897}
898
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000899struct drm_connector *
900nouveau_connector_create(struct drm_device *dev, int index)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000901{
Ben Skeggsd17f3952010-06-01 13:32:42 +1000902 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000903 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsb29caa52011-10-06 13:29:05 +1000904 struct nouveau_display_engine *disp = &dev_priv->engine.display;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000905 struct nouveau_connector *nv_connector = NULL;
906 struct drm_connector *connector;
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200907 int type, ret = 0;
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000908 bool dummy;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000909
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100910 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000911
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000912 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
913 nv_connector = nouveau_connector(connector);
914 if (nv_connector->index == index)
915 return connector;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000916 }
917
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
919 if (!nv_connector)
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000920 return ERR_PTR(-ENOMEM);
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000921
Ben Skeggs6ee73862009-12-11 19:24:15 +1000922 connector = &nv_connector->base;
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000923 nv_connector->index = index;
924
925 /* attempt to parse vbios connector type and hotplug gpio */
Ben Skeggscb75d972012-07-11 10:44:20 +1000926 nv_connector->dcb = olddcb_conn(dev, index);
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000927 if (nv_connector->dcb) {
928 static const u8 hpd[16] = {
929 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
930 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
931 };
932
933 u32 entry = ROM16(nv_connector->dcb[0]);
Ben Skeggscb75d972012-07-11 10:44:20 +1000934 if (olddcb_conntab(dev)[3] >= 4)
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000935 entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
936
937 nv_connector->hpd = ffs((entry & 0x07033000) >> 12);
938 nv_connector->hpd = hpd[nv_connector->hpd];
939
940 nv_connector->type = nv_connector->dcb[0];
941 if (drm_conntype_from_dcb(nv_connector->type) ==
942 DRM_MODE_CONNECTOR_Unknown) {
943 NV_WARN(dev, "unknown connector type %02x\n",
944 nv_connector->type);
945 nv_connector->type = DCB_CONNECTOR_NONE;
946 }
947
948 /* Gigabyte NX85T */
949 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
950 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
951 nv_connector->type = DCB_CONNECTOR_DVI_I;
952 }
953
954 /* Gigabyte GV-NX86T512H */
955 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
956 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
957 nv_connector->type = DCB_CONNECTOR_DVI_I;
958 }
959 } else {
960 nv_connector->type = DCB_CONNECTOR_NONE;
961 nv_connector->hpd = DCB_GPIO_UNUSED;
962 }
963
964 /* no vbios data, or an unknown dcb connector type - attempt to
965 * figure out something suitable ourselves
966 */
967 if (nv_connector->type == DCB_CONNECTOR_NONE) {
968 struct drm_nouveau_private *dev_priv = dev->dev_private;
969 struct dcb_table *dcbt = &dev_priv->vbios.dcb;
970 u32 encoders = 0;
971 int i;
972
973 for (i = 0; i < dcbt->entries; i++) {
974 if (dcbt->entry[i].connector == nv_connector->index)
975 encoders |= (1 << dcbt->entry[i].type);
976 }
977
Ben Skeggscb75d972012-07-11 10:44:20 +1000978 if (encoders & (1 << DCB_OUTPUT_DP)) {
979 if (encoders & (1 << DCB_OUTPUT_TMDS))
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000980 nv_connector->type = DCB_CONNECTOR_DP;
981 else
982 nv_connector->type = DCB_CONNECTOR_eDP;
983 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000984 if (encoders & (1 << DCB_OUTPUT_TMDS)) {
985 if (encoders & (1 << DCB_OUTPUT_ANALOG))
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000986 nv_connector->type = DCB_CONNECTOR_DVI_I;
987 else
988 nv_connector->type = DCB_CONNECTOR_DVI_D;
989 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000990 if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000991 nv_connector->type = DCB_CONNECTOR_VGA;
992 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000993 if (encoders & (1 << DCB_OUTPUT_LVDS)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000994 nv_connector->type = DCB_CONNECTOR_LVDS;
995 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000996 if (encoders & (1 << DCB_OUTPUT_TV)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000997 nv_connector->type = DCB_CONNECTOR_TV_0;
998 }
999 }
1000
1001 type = drm_conntype_from_dcb(nv_connector->type);
1002 if (type == DRM_MODE_CONNECTOR_LVDS) {
1003 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
1004 if (ret) {
1005 NV_ERROR(dev, "Error parsing LVDS table, disabling\n");
1006 kfree(nv_connector);
1007 return ERR_PTR(ret);
1008 }
1009
1010 funcs = &nouveau_connector_funcs_lvds;
1011 } else {
1012 funcs = &nouveau_connector_funcs;
1013 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001014
Ben Skeggs6ee73862009-12-11 19:24:15 +10001015 /* defaults, will get overridden in detect() */
1016 connector->interlace_allowed = false;
1017 connector->doublescan_allowed = false;
1018
Ben Skeggsd17f3952010-06-01 13:32:42 +10001019 drm_connector_init(dev, connector, funcs, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001020 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1021
1022 /* Init DVI-I specific properties */
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001023 if (nv_connector->type == DCB_CONNECTOR_DVI_I)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001024 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001025
Ben Skeggsb29caa52011-10-06 13:29:05 +10001026 /* Add overscan compensation options to digital outputs */
Ben Skeggsde691852011-10-17 12:23:41 +10001027 if (disp->underscan_property &&
Ben Skeggsfa2c1132012-03-22 11:27:15 +10001028 (type == DRM_MODE_CONNECTOR_DVID ||
1029 type == DRM_MODE_CONNECTOR_DVII ||
1030 type == DRM_MODE_CONNECTOR_HDMIA ||
1031 type == DRM_MODE_CONNECTOR_DisplayPort)) {
Ben Skeggsb29caa52011-10-06 13:29:05 +10001032 drm_connector_attach_property(connector,
1033 disp->underscan_property,
1034 UNDERSCAN_OFF);
1035 drm_connector_attach_property(connector,
1036 disp->underscan_hborder_property,
1037 0);
1038 drm_connector_attach_property(connector,
1039 disp->underscan_vborder_property,
1040 0);
1041 }
1042
Christoph Bumillerdf26bc92012-01-21 23:13:26 +01001043 /* Add hue and saturation options */
1044 if (disp->vibrant_hue_property)
1045 drm_connector_attach_property(connector,
1046 disp->vibrant_hue_property,
1047 90);
1048 if (disp->color_vibrance_property)
1049 drm_connector_attach_property(connector,
1050 disp->color_vibrance_property,
1051 150);
1052
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001053 switch (nv_connector->type) {
Ben Skeggsbe079e92010-02-24 14:01:40 +10001054 case DCB_CONNECTOR_VGA:
1055 if (dev_priv->card_type >= NV_50) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001056 drm_connector_attach_property(connector,
1057 dev->mode_config.scaling_mode_property,
1058 nv_connector->scaling_mode);
1059 }
Ben Skeggsbe079e92010-02-24 14:01:40 +10001060 /* fall-through */
1061 case DCB_CONNECTOR_TV_0:
1062 case DCB_CONNECTOR_TV_1:
1063 case DCB_CONNECTOR_TV_3:
1064 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1065 break;
1066 default:
1067 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1068
1069 drm_connector_attach_property(connector,
1070 dev->mode_config.scaling_mode_property,
1071 nv_connector->scaling_mode);
Ben Skeggsde691852011-10-17 12:23:41 +10001072 if (disp->dithering_mode) {
1073 nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1074 drm_connector_attach_property(connector,
1075 disp->dithering_mode,
1076 nv_connector->dithering_mode);
1077 }
1078 if (disp->dithering_depth) {
1079 nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
1080 drm_connector_attach_property(connector,
1081 disp->dithering_depth,
1082 nv_connector->dithering_depth);
1083 }
Ben Skeggsbe079e92010-02-24 14:01:40 +10001084 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001085 }
1086
Ben Skeggsa0b25632011-11-21 16:41:48 +10001087 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1088 if (nv_connector->hpd != DCB_GPIO_UNUSED) {
1089 ret = nouveau_gpio_isr_add(dev, 0, nv_connector->hpd, 0xff,
1090 nouveau_connector_hotplug,
1091 connector);
1092 if (ret == 0)
1093 connector->polled = DRM_CONNECTOR_POLL_HPD;
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001094 }
1095
Ben Skeggs6ee73862009-12-11 19:24:15 +10001096 drm_sysfs_connector_add(connector);
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001097 return connector;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001098}
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001099
1100static void
1101nouveau_connector_hotplug(void *data, int plugged)
1102{
1103 struct drm_connector *connector = data;
1104 struct drm_device *dev = connector->dev;
1105
Ben Skeggs68c99182011-08-02 13:57:10 +10001106 NV_DEBUG(dev, "%splugged %s\n", plugged ? "" : "un",
1107 drm_get_connector_name(connector));
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001108
Ben Skeggs68c99182011-08-02 13:57:10 +10001109 if (plugged)
1110 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1111 else
1112 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001113
1114 drm_helper_hpd_irq_event(dev);
1115}