blob: 50704287a8c20b74f80529b2f1426b8972c7d3fe [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
40static inline struct drm_encoder_slave_funcs *
41get_slave_funcs(struct nouveau_encoder *enc)
42{
43 return to_encoder_slave(to_drm_encoder(enc))->slave_funcs;
44}
45
46static struct nouveau_encoder *
47find_encoder_by_type(struct drm_connector *connector, int type)
48{
49 struct drm_device *dev = connector->dev;
50 struct nouveau_encoder *nv_encoder;
51 struct drm_mode_object *obj;
52 int i, id;
53
54 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
55 id = connector->encoder_ids[i];
56 if (!id)
57 break;
58
59 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
60 if (!obj)
61 continue;
62 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
63
64 if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
65 return nv_encoder;
66 }
67
68 return NULL;
69}
70
71struct nouveau_connector *
72nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
73{
74 struct drm_device *dev = to_drm_encoder(encoder)->dev;
75 struct drm_connector *drm_connector;
76
77 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
78 if (drm_connector->encoder == to_drm_encoder(encoder))
79 return nouveau_connector(drm_connector);
80 }
81
82 return NULL;
83}
84
85
86static void
87nouveau_connector_destroy(struct drm_connector *drm_connector)
88{
Xavier Chantryc8ebe272010-01-11 22:42:21 +010089 struct nouveau_connector *nv_connector =
90 nouveau_connector(drm_connector);
Marcin Slusarzdd19e442010-01-30 15:41:00 +010091 struct drm_device *dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +100092
Xavier Chantryc8ebe272010-01-11 22:42:21 +010093 if (!nv_connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +100094 return;
95
Marcin Slusarzdd19e442010-01-30 15:41:00 +010096 dev = nv_connector->base.dev;
97 NV_DEBUG_KMS(dev, "\n");
98
Xavier Chantryc8ebe272010-01-11 22:42:21 +010099 kfree(nv_connector->edid);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000100 drm_sysfs_connector_remove(drm_connector);
101 drm_connector_cleanup(drm_connector);
102 kfree(drm_connector);
103}
104
105static void
106nouveau_connector_ddc_prepare(struct drm_connector *connector, int *flags)
107{
108 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
109
110 if (dev_priv->card_type >= NV_50)
111 return;
112
113 *flags = 0;
114 if (NVLockVgaCrtcs(dev_priv->dev, false))
115 *flags |= 1;
116 if (nv_heads_tied(dev_priv->dev))
117 *flags |= 2;
118
119 if (*flags & 2)
120 NVSetOwner(dev_priv->dev, 0); /* necessary? */
121}
122
123static void
124nouveau_connector_ddc_finish(struct drm_connector *connector, int flags)
125{
126 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
127
128 if (dev_priv->card_type >= NV_50)
129 return;
130
131 if (flags & 2)
132 NVSetOwner(dev_priv->dev, 4);
133 if (flags & 1)
134 NVLockVgaCrtcs(dev_priv->dev, true);
135}
136
137static struct nouveau_i2c_chan *
138nouveau_connector_ddc_detect(struct drm_connector *connector,
139 struct nouveau_encoder **pnv_encoder)
140{
141 struct drm_device *dev = connector->dev;
142 uint8_t out_buf[] = { 0x0, 0x0}, buf[2];
143 int ret, flags, i;
144
145 struct i2c_msg msgs[] = {
146 {
147 .addr = 0x50,
148 .flags = 0,
149 .len = 1,
150 .buf = out_buf,
151 },
152 {
153 .addr = 0x50,
154 .flags = I2C_M_RD,
155 .len = 1,
156 .buf = buf,
157 }
158 };
159
160 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
161 struct nouveau_i2c_chan *i2c = NULL;
162 struct nouveau_encoder *nv_encoder;
163 struct drm_mode_object *obj;
164 int id;
165
166 id = connector->encoder_ids[i];
167 if (!id)
168 break;
169
170 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
171 if (!obj)
172 continue;
173 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
174
175 if (nv_encoder->dcb->i2c_index < 0xf)
176 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
177 if (!i2c)
178 continue;
179
180 nouveau_connector_ddc_prepare(connector, &flags);
181 ret = i2c_transfer(&i2c->adapter, msgs, 2);
182 nouveau_connector_ddc_finish(connector, flags);
183
184 if (ret == 2) {
185 *pnv_encoder = nv_encoder;
186 return i2c;
187 }
188 }
189
190 return NULL;
191}
192
193static void
194nouveau_connector_set_encoder(struct drm_connector *connector,
195 struct nouveau_encoder *nv_encoder)
196{
197 struct nouveau_connector *nv_connector = nouveau_connector(connector);
198 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
199 struct drm_device *dev = connector->dev;
200
201 if (nv_connector->detected_encoder == nv_encoder)
202 return;
203 nv_connector->detected_encoder = nv_encoder;
204
205 if (nv_encoder->dcb->type == OUTPUT_LVDS ||
206 nv_encoder->dcb->type == OUTPUT_TMDS) {
207 connector->doublescan_allowed = false;
208 connector->interlace_allowed = false;
209 } else {
210 connector->doublescan_allowed = true;
211 if (dev_priv->card_type == NV_20 ||
212 (dev_priv->card_type == NV_10 &&
213 (dev->pci_device & 0x0ff0) != 0x0100 &&
214 (dev->pci_device & 0x0ff0) != 0x0150))
215 /* HW is broken */
216 connector->interlace_allowed = false;
217 else
218 connector->interlace_allowed = true;
219 }
220
Ben Skeggsbe079e92010-02-24 14:01:40 +1000221 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222 drm_connector_property_set_value(connector,
223 dev->mode_config.dvi_i_subconnector_property,
224 nv_encoder->dcb->type == OUTPUT_TMDS ?
225 DRM_MODE_SUBCONNECTOR_DVID :
226 DRM_MODE_SUBCONNECTOR_DVIA);
227 }
228}
229
230static enum drm_connector_status
231nouveau_connector_detect(struct drm_connector *connector)
232{
233 struct drm_device *dev = connector->dev;
234 struct nouveau_connector *nv_connector = nouveau_connector(connector);
235 struct nouveau_encoder *nv_encoder = NULL;
236 struct nouveau_i2c_chan *i2c;
237 int type, flags;
238
Francisco Jerezb8780e22010-01-14 15:54:23 +0100239 /* Cleanup the previous EDID block. */
240 if (nv_connector->edid) {
241 drm_mode_connector_update_edid_property(connector, NULL);
242 kfree(nv_connector->edid);
243 nv_connector->edid = NULL;
244 }
Xavier Chantryc8ebe272010-01-11 22:42:21 +0100245
Ben Skeggs6ee73862009-12-11 19:24:15 +1000246 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
247 if (i2c) {
248 nouveau_connector_ddc_prepare(connector, &flags);
249 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
250 nouveau_connector_ddc_finish(connector, flags);
251 drm_mode_connector_update_edid_property(connector,
252 nv_connector->edid);
253 if (!nv_connector->edid) {
254 NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
255 drm_get_connector_name(connector));
Francisco Jerez0ed31652010-01-14 15:47:03 +0100256 goto detect_analog;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000257 }
258
259 if (nv_encoder->dcb->type == OUTPUT_DP &&
260 !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
261 NV_ERROR(dev, "Detected %s, but failed init\n",
262 drm_get_connector_name(connector));
263 return connector_status_disconnected;
264 }
265
266 /* Override encoder type for DVI-I based on whether EDID
267 * says the display is digital or analog, both use the
268 * same i2c channel so the value returned from ddc_detect
269 * isn't necessarily correct.
270 */
Ben Skeggsbe079e92010-02-24 14:01:40 +1000271 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000272 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
273 type = OUTPUT_TMDS;
274 else
275 type = OUTPUT_ANALOG;
276
277 nv_encoder = find_encoder_by_type(connector, type);
278 if (!nv_encoder) {
279 NV_ERROR(dev, "Detected %d encoder on %s, "
280 "but no object!\n", type,
281 drm_get_connector_name(connector));
282 return connector_status_disconnected;
283 }
284 }
285
286 nouveau_connector_set_encoder(connector, nv_encoder);
287 return connector_status_connected;
288 }
289
Francisco Jerez0ed31652010-01-14 15:47:03 +0100290detect_analog:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000291 nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
Ben Skeggsf4053502010-03-15 09:43:51 +1000292 if (!nv_encoder && !nouveau_tv_disable)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000293 nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
294 if (nv_encoder) {
295 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
296 struct drm_encoder_helper_funcs *helper =
297 encoder->helper_private;
298
299 if (helper->detect(encoder, connector) ==
300 connector_status_connected) {
301 nouveau_connector_set_encoder(connector, nv_encoder);
302 return connector_status_connected;
303 }
304
305 }
306
307 return connector_status_disconnected;
308}
309
Ben Skeggsd17f3952010-06-01 13:32:42 +1000310static enum drm_connector_status
311nouveau_connector_detect_lvds(struct drm_connector *connector)
312{
313 struct drm_device *dev = connector->dev;
314 struct drm_nouveau_private *dev_priv = dev->dev_private;
315 struct nouveau_connector *nv_connector = nouveau_connector(connector);
316 struct nouveau_encoder *nv_encoder = NULL;
317 enum drm_connector_status status = connector_status_disconnected;
318
319 /* Cleanup the previous EDID block. */
320 if (nv_connector->edid) {
321 drm_mode_connector_update_edid_property(connector, NULL);
322 kfree(nv_connector->edid);
323 nv_connector->edid = NULL;
324 }
325
326 nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
327 if (!nv_encoder)
328 return connector_status_disconnected;
329
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000330 /* Try retrieving EDID via DDC */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000331 if (!dev_priv->vbios.fp_no_ddc) {
332 status = nouveau_connector_detect(connector);
333 if (status == connector_status_connected)
334 goto out;
335 }
336
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000337 /* On some laptops (Sony, i'm looking at you) there appears to
338 * be no direct way of accessing the panel's EDID. The only
339 * option available to us appears to be to ask ACPI for help..
340 *
341 * It's important this check's before trying straps, one of the
342 * said manufacturer's laptops are configured in such a way
343 * the nouveau decides an entry in the VBIOS FP mode table is
344 * valid - it's not (rh#613284)
345 */
346 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
347 if (!nouveau_acpi_edid(dev, connector)) {
348 status = connector_status_connected;
349 goto out;
350 }
351 }
352
Ben Skeggsd17f3952010-06-01 13:32:42 +1000353 /* If no EDID found above, and the VBIOS indicates a hardcoded
354 * modeline is avalilable for the panel, set it as the panel's
355 * native mode and exit.
356 */
357 if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
358 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
359 status = connector_status_connected;
360 goto out;
361 }
362
363 /* Still nothing, some VBIOS images have a hardcoded EDID block
364 * stored for the panel stored in them.
365 */
366 if (!dev_priv->vbios.fp_no_ddc) {
367 struct edid *edid =
368 (struct edid *)nouveau_bios_embedded_edid(dev);
369 if (edid) {
370 nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
371 *(nv_connector->edid) = *edid;
372 status = connector_status_connected;
373 }
374 }
375
376out:
377#if defined(CONFIG_ACPI_BUTTON) || \
378 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
379 if (status == connector_status_connected &&
380 !nouveau_ignorelid && !acpi_lid_open())
381 status = connector_status_unknown;
382#endif
383
384 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
Albert Damen3195c5f2010-06-20 16:57:57 +0200385 nouveau_connector_set_encoder(connector, nv_encoder);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000386 return status;
387}
388
Ben Skeggs6ee73862009-12-11 19:24:15 +1000389static void
390nouveau_connector_force(struct drm_connector *connector)
391{
Ben Skeggsbe079e92010-02-24 14:01:40 +1000392 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000393 struct nouveau_encoder *nv_encoder;
394 int type;
395
Ben Skeggsbe079e92010-02-24 14:01:40 +1000396 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000397 if (connector->force == DRM_FORCE_ON_DIGITAL)
398 type = OUTPUT_TMDS;
399 else
400 type = OUTPUT_ANALOG;
401 } else
402 type = OUTPUT_ANY;
403
404 nv_encoder = find_encoder_by_type(connector, type);
405 if (!nv_encoder) {
Ben Skeggsbe079e92010-02-24 14:01:40 +1000406 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000407 drm_get_connector_name(connector));
408 connector->status = connector_status_disconnected;
409 return;
410 }
411
412 nouveau_connector_set_encoder(connector, nv_encoder);
413}
414
415static int
416nouveau_connector_set_property(struct drm_connector *connector,
417 struct drm_property *property, uint64_t value)
418{
419 struct nouveau_connector *nv_connector = nouveau_connector(connector);
420 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
421 struct drm_device *dev = connector->dev;
422 int ret;
423
424 /* Scaling mode */
425 if (property == dev->mode_config.scaling_mode_property) {
426 struct nouveau_crtc *nv_crtc = NULL;
427 bool modeset = false;
428
429 switch (value) {
430 case DRM_MODE_SCALE_NONE:
431 case DRM_MODE_SCALE_FULLSCREEN:
432 case DRM_MODE_SCALE_CENTER:
433 case DRM_MODE_SCALE_ASPECT:
434 break;
435 default:
436 return -EINVAL;
437 }
438
439 /* LVDS always needs gpu scaling */
Ben Skeggsbe079e92010-02-24 14:01:40 +1000440 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000441 value == DRM_MODE_SCALE_NONE)
442 return -EINVAL;
443
444 /* Changing between GPU and panel scaling requires a full
445 * modeset
446 */
447 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
448 (value == DRM_MODE_SCALE_NONE))
449 modeset = true;
450 nv_connector->scaling_mode = value;
451
452 if (connector->encoder && connector->encoder->crtc)
453 nv_crtc = nouveau_crtc(connector->encoder->crtc);
454 if (!nv_crtc)
455 return 0;
456
457 if (modeset || !nv_crtc->set_scale) {
458 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
459 &nv_crtc->base.mode,
460 nv_crtc->base.x,
461 nv_crtc->base.y, NULL);
462 if (!ret)
463 return -EINVAL;
464 } else {
465 ret = nv_crtc->set_scale(nv_crtc, value, true);
466 if (ret)
467 return ret;
468 }
469
470 return 0;
471 }
472
473 /* Dithering */
474 if (property == dev->mode_config.dithering_mode_property) {
475 struct nouveau_crtc *nv_crtc = NULL;
476
477 if (value == DRM_MODE_DITHERING_ON)
478 nv_connector->use_dithering = true;
479 else
480 nv_connector->use_dithering = false;
481
482 if (connector->encoder && connector->encoder->crtc)
483 nv_crtc = nouveau_crtc(connector->encoder->crtc);
484
485 if (!nv_crtc || !nv_crtc->set_dither)
486 return 0;
487
488 return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
489 true);
490 }
491
492 if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
493 return get_slave_funcs(nv_encoder)->
494 set_property(to_drm_encoder(nv_encoder), connector, property, value);
495
496 return -EINVAL;
497}
498
499static struct drm_display_mode *
Ben Skeggs26099a72010-05-28 10:13:16 +1000500nouveau_connector_native_mode(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000501{
Ben Skeggs26099a72010-05-28 10:13:16 +1000502 struct drm_connector_helper_funcs *helper = connector->helper_private;
503 struct nouveau_connector *nv_connector = nouveau_connector(connector);
504 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000505 struct drm_display_mode *mode, *largest = NULL;
506 int high_w = 0, high_h = 0, high_v = 0;
507
Ben Skeggs26099a72010-05-28 10:13:16 +1000508 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
509 if (helper->mode_valid(connector, mode) != MODE_OK)
510 continue;
511
512 /* Use preferred mode if there is one.. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000513 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100514 NV_DEBUG_KMS(dev, "native mode from preferred\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000515 return drm_mode_duplicate(dev, mode);
516 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000517
Ben Skeggs26099a72010-05-28 10:13:16 +1000518 /* Otherwise, take the resolution with the largest width, then
519 * height, then vertical refresh
520 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000521 if (mode->hdisplay < high_w)
522 continue;
523
524 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
525 continue;
526
527 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
528 mode->vrefresh < high_v)
529 continue;
530
531 high_w = mode->hdisplay;
532 high_h = mode->vdisplay;
533 high_v = mode->vrefresh;
534 largest = mode;
535 }
536
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100537 NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000538 high_w, high_h, high_v);
539 return largest ? drm_mode_duplicate(dev, largest) : NULL;
540}
541
542struct moderec {
543 int hdisplay;
544 int vdisplay;
545};
546
547static struct moderec scaler_modes[] = {
548 { 1920, 1200 },
549 { 1920, 1080 },
550 { 1680, 1050 },
551 { 1600, 1200 },
552 { 1400, 1050 },
553 { 1280, 1024 },
554 { 1280, 960 },
555 { 1152, 864 },
556 { 1024, 768 },
557 { 800, 600 },
558 { 720, 400 },
559 { 640, 480 },
560 { 640, 400 },
561 { 640, 350 },
562 {}
563};
564
565static int
566nouveau_connector_scaler_modes_add(struct drm_connector *connector)
567{
568 struct nouveau_connector *nv_connector = nouveau_connector(connector);
569 struct drm_display_mode *native = nv_connector->native_mode, *m;
570 struct drm_device *dev = connector->dev;
571 struct moderec *mode = &scaler_modes[0];
572 int modes = 0;
573
574 if (!native)
575 return 0;
576
577 while (mode->hdisplay) {
578 if (mode->hdisplay <= native->hdisplay &&
579 mode->vdisplay <= native->vdisplay) {
580 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
581 drm_mode_vrefresh(native), false,
582 false, false);
583 if (!m)
584 continue;
585
586 m->type |= DRM_MODE_TYPE_DRIVER;
587
588 drm_mode_probed_add(connector, m);
589 modes++;
590 }
591
592 mode++;
593 }
594
595 return modes;
596}
597
598static int
599nouveau_connector_get_modes(struct drm_connector *connector)
600{
601 struct drm_device *dev = connector->dev;
Ben Skeggsd17f3952010-06-01 13:32:42 +1000602 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000603 struct nouveau_connector *nv_connector = nouveau_connector(connector);
604 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
605 int ret = 0;
606
Ben Skeggsd17f3952010-06-01 13:32:42 +1000607 /* destroy the native mode, the attached monitor could have changed.
Ben Skeggs6ee73862009-12-11 19:24:15 +1000608 */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000609 if (nv_connector->native_mode) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610 drm_mode_destroy(dev, nv_connector->native_mode);
611 nv_connector->native_mode = NULL;
612 }
613
614 if (nv_connector->edid)
615 ret = drm_add_edid_modes(connector, nv_connector->edid);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000616 else
617 if (nv_encoder->dcb->type == OUTPUT_LVDS &&
618 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
Francisco Jerez6e86e042010-07-03 18:36:39 +0200619 dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
620 nv_connector->native_mode = drm_mode_create(dev);
621 nouveau_bios_fp_mode(dev, nv_connector->native_mode);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000622 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000623
624 /* Find the native mode if this is a digital panel, if we didn't
625 * find any modes through DDC previously add the native mode to
626 * the list of modes.
627 */
628 if (!nv_connector->native_mode)
629 nv_connector->native_mode =
Ben Skeggs26099a72010-05-28 10:13:16 +1000630 nouveau_connector_native_mode(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000631 if (ret == 0 && nv_connector->native_mode) {
632 struct drm_display_mode *mode;
633
634 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
635 drm_mode_probed_add(connector, mode);
636 ret = 1;
637 }
638
639 if (nv_encoder->dcb->type == OUTPUT_TV)
640 ret = get_slave_funcs(nv_encoder)->
641 get_modes(to_drm_encoder(nv_encoder), connector);
642
Ben Skeggs646bef22010-07-06 11:27:52 +1000643 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
644 nv_connector->dcb->type == DCB_CONNECTOR_eDP)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000645 ret += nouveau_connector_scaler_modes_add(connector);
646
647 return ret;
648}
649
650static int
651nouveau_connector_mode_valid(struct drm_connector *connector,
652 struct drm_display_mode *mode)
653{
654 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
655 struct nouveau_connector *nv_connector = nouveau_connector(connector);
656 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
657 unsigned min_clock = 25000, max_clock = min_clock;
658 unsigned clock = mode->clock;
659
660 switch (nv_encoder->dcb->type) {
661 case OUTPUT_LVDS:
Ben Skeggs26099a72010-05-28 10:13:16 +1000662 if (nv_connector->native_mode &&
663 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
664 mode->vdisplay > nv_connector->native_mode->vdisplay))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000665 return MODE_PANEL;
666
667 min_clock = 0;
668 max_clock = 400000;
669 break;
670 case OUTPUT_TMDS:
671 if ((dev_priv->card_type >= NV_50 && !nouveau_duallink) ||
Ben Skeggs2c580772010-05-28 10:04:05 +1000672 !nv_encoder->dcb->duallink_possible)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000673 max_clock = 165000;
674 else
675 max_clock = 330000;
676 break;
677 case OUTPUT_ANALOG:
678 max_clock = nv_encoder->dcb->crtconf.maxfreq;
679 if (!max_clock)
680 max_clock = 350000;
681 break;
682 case OUTPUT_TV:
683 return get_slave_funcs(nv_encoder)->
684 mode_valid(to_drm_encoder(nv_encoder), mode);
685 case OUTPUT_DP:
686 if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
687 max_clock = nv_encoder->dp.link_nr * 270000;
688 else
689 max_clock = nv_encoder->dp.link_nr * 162000;
690
691 clock *= 3;
692 break;
Ben Skeggse7cc51c2010-02-24 10:31:39 +1000693 default:
694 BUG_ON(1);
695 return MODE_BAD;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000696 }
697
698 if (clock < min_clock)
699 return MODE_CLOCK_LOW;
700
701 if (clock > max_clock)
702 return MODE_CLOCK_HIGH;
703
704 return MODE_OK;
705}
706
707static struct drm_encoder *
708nouveau_connector_best_encoder(struct drm_connector *connector)
709{
710 struct nouveau_connector *nv_connector = nouveau_connector(connector);
711
712 if (nv_connector->detected_encoder)
713 return to_drm_encoder(nv_connector->detected_encoder);
714
715 return NULL;
716}
717
Francisco Jerez2ed06b72010-07-03 15:52:03 +0200718void
719nouveau_connector_set_polling(struct drm_connector *connector)
720{
721 struct drm_device *dev = connector->dev;
722 struct drm_nouveau_private *dev_priv = dev->dev_private;
723 struct drm_crtc *crtc;
724 bool spare_crtc = false;
725
726 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
727 spare_crtc |= !crtc->enabled;
728
729 connector->polled = 0;
730
731 switch (connector->connector_type) {
732 case DRM_MODE_CONNECTOR_VGA:
733 case DRM_MODE_CONNECTOR_TV:
734 if (dev_priv->card_type >= NV_50 ||
735 (nv_gf4_disp_arch(dev) && spare_crtc))
736 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
737 break;
738
739 case DRM_MODE_CONNECTOR_DVII:
740 case DRM_MODE_CONNECTOR_DVID:
741 case DRM_MODE_CONNECTOR_HDMIA:
742 case DRM_MODE_CONNECTOR_DisplayPort:
743 case DRM_MODE_CONNECTOR_eDP:
744 if (dev_priv->card_type >= NV_50)
745 connector->polled = DRM_CONNECTOR_POLL_HPD;
746 else if (connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
747 spare_crtc)
748 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
749 break;
750
751 default:
752 break;
753 }
754}
755
Ben Skeggs6ee73862009-12-11 19:24:15 +1000756static const struct drm_connector_helper_funcs
757nouveau_connector_helper_funcs = {
758 .get_modes = nouveau_connector_get_modes,
759 .mode_valid = nouveau_connector_mode_valid,
760 .best_encoder = nouveau_connector_best_encoder,
761};
762
763static const struct drm_connector_funcs
764nouveau_connector_funcs = {
765 .dpms = drm_helper_connector_dpms,
766 .save = NULL,
767 .restore = NULL,
768 .detect = nouveau_connector_detect,
769 .destroy = nouveau_connector_destroy,
770 .fill_modes = drm_helper_probe_single_connector_modes,
771 .set_property = nouveau_connector_set_property,
772 .force = nouveau_connector_force
773};
774
Ben Skeggsd17f3952010-06-01 13:32:42 +1000775static const struct drm_connector_funcs
776nouveau_connector_funcs_lvds = {
777 .dpms = drm_helper_connector_dpms,
778 .save = NULL,
779 .restore = NULL,
780 .detect = nouveau_connector_detect_lvds,
781 .destroy = nouveau_connector_destroy,
782 .fill_modes = drm_helper_probe_single_connector_modes,
783 .set_property = nouveau_connector_set_property,
784 .force = nouveau_connector_force
785};
Ben Skeggs6ee73862009-12-11 19:24:15 +1000786
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000787struct drm_connector *
788nouveau_connector_create(struct drm_device *dev, int index)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000789{
Ben Skeggsd17f3952010-06-01 13:32:42 +1000790 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000791 struct drm_nouveau_private *dev_priv = dev->dev_private;
792 struct nouveau_connector *nv_connector = NULL;
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000793 struct dcb_connector_table_entry *dcb = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000794 struct drm_connector *connector;
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200795 int type, ret = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000796
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100797 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000798
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000799 if (index >= dev_priv->vbios.dcb.connector.entries)
800 return ERR_PTR(-EINVAL);
801
802 dcb = &dev_priv->vbios.dcb.connector.entry[index];
803 if (dcb->drm)
804 return dcb->drm;
805
Ben Skeggs7f612d82010-02-24 12:07:31 +1000806 switch (dcb->type) {
Ben Skeggs7f612d82010-02-24 12:07:31 +1000807 case DCB_CONNECTOR_VGA:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000808 type = DRM_MODE_CONNECTOR_VGA;
809 break;
810 case DCB_CONNECTOR_TV_0:
811 case DCB_CONNECTOR_TV_1:
812 case DCB_CONNECTOR_TV_3:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000813 type = DRM_MODE_CONNECTOR_TV;
814 break;
815 case DCB_CONNECTOR_DVI_I:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000816 type = DRM_MODE_CONNECTOR_DVII;
817 break;
818 case DCB_CONNECTOR_DVI_D:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000819 type = DRM_MODE_CONNECTOR_DVID;
820 break;
Ben Skeggsbe079e92010-02-24 14:01:40 +1000821 case DCB_CONNECTOR_HDMI_0:
822 case DCB_CONNECTOR_HDMI_1:
Ben Skeggsbe079e92010-02-24 14:01:40 +1000823 type = DRM_MODE_CONNECTOR_HDMIA;
824 break;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000825 case DCB_CONNECTOR_LVDS:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000826 type = DRM_MODE_CONNECTOR_LVDS;
Ben Skeggsd17f3952010-06-01 13:32:42 +1000827 funcs = &nouveau_connector_funcs_lvds;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000828 break;
829 case DCB_CONNECTOR_DP:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000830 type = DRM_MODE_CONNECTOR_DisplayPort;
831 break;
Ben Skeggsbe079e92010-02-24 14:01:40 +1000832 case DCB_CONNECTOR_eDP:
Ben Skeggsbe079e92010-02-24 14:01:40 +1000833 type = DRM_MODE_CONNECTOR_eDP;
834 break;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000835 default:
836 NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000837 return ERR_PTR(-EINVAL);
Ben Skeggs7f612d82010-02-24 12:07:31 +1000838 }
839
Ben Skeggs6ee73862009-12-11 19:24:15 +1000840 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
841 if (!nv_connector)
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000842 return ERR_PTR(-ENOMEM);
Ben Skeggs7f612d82010-02-24 12:07:31 +1000843 nv_connector->dcb = dcb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000844 connector = &nv_connector->base;
845
Ben Skeggs6ee73862009-12-11 19:24:15 +1000846 /* defaults, will get overridden in detect() */
847 connector->interlace_allowed = false;
848 connector->doublescan_allowed = false;
849
Ben Skeggsd17f3952010-06-01 13:32:42 +1000850 drm_connector_init(dev, connector, funcs, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000851 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
852
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200853 /* Check if we need dithering enabled */
854 if (dcb->type == DCB_CONNECTOR_LVDS) {
855 bool dummy, is_24bit = false;
856
857 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
858 if (ret) {
859 NV_ERROR(dev, "Error parsing LVDS table, disabling "
860 "LVDS\n");
861 goto fail;
862 }
863
864 nv_connector->use_dithering = !is_24bit;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000865 }
866
Ben Skeggs6ee73862009-12-11 19:24:15 +1000867 /* Init DVI-I specific properties */
Ben Skeggsbe079e92010-02-24 14:01:40 +1000868 if (dcb->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000869 drm_mode_create_dvi_i_properties(dev);
870 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
871 drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
872 }
873
Ben Skeggsbe079e92010-02-24 14:01:40 +1000874 switch (dcb->type) {
875 case DCB_CONNECTOR_VGA:
876 if (dev_priv->card_type >= NV_50) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000877 drm_connector_attach_property(connector,
878 dev->mode_config.scaling_mode_property,
879 nv_connector->scaling_mode);
880 }
Ben Skeggsbe079e92010-02-24 14:01:40 +1000881 /* fall-through */
882 case DCB_CONNECTOR_TV_0:
883 case DCB_CONNECTOR_TV_1:
884 case DCB_CONNECTOR_TV_3:
885 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
886 break;
887 default:
888 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
889
890 drm_connector_attach_property(connector,
891 dev->mode_config.scaling_mode_property,
892 nv_connector->scaling_mode);
893 drm_connector_attach_property(connector,
894 dev->mode_config.dithering_mode_property,
895 nv_connector->use_dithering ?
896 DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
897 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000898 }
899
Francisco Jerez2ed06b72010-07-03 15:52:03 +0200900 nouveau_connector_set_polling(connector);
901
Ben Skeggs6ee73862009-12-11 19:24:15 +1000902 drm_sysfs_connector_add(connector);
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000903 dcb->drm = connector;
904 return dcb->drm;
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200905
906fail:
907 drm_connector_cleanup(connector);
908 kfree(connector);
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000909 return ERR_PTR(ret);
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200910
Ben Skeggs6ee73862009-12-11 19:24:15 +1000911}