blob: 4b286a8c30c8f1621989e7a79e0249492606525c [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 Skeggs6ee73862009-12-11 19:24:15 +100040static struct nouveau_encoder *
41find_encoder_by_type(struct drm_connector *connector, int type)
42{
43 struct drm_device *dev = connector->dev;
44 struct nouveau_encoder *nv_encoder;
45 struct drm_mode_object *obj;
46 int i, id;
47
48 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
49 id = connector->encoder_ids[i];
50 if (!id)
51 break;
52
53 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
54 if (!obj)
55 continue;
56 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
57
58 if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
59 return nv_encoder;
60 }
61
62 return NULL;
63}
64
65struct nouveau_connector *
66nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
67{
68 struct drm_device *dev = to_drm_encoder(encoder)->dev;
69 struct drm_connector *drm_connector;
70
71 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
72 if (drm_connector->encoder == to_drm_encoder(encoder))
73 return nouveau_connector(drm_connector);
74 }
75
76 return NULL;
77}
78
79
80static void
81nouveau_connector_destroy(struct drm_connector *drm_connector)
82{
Xavier Chantryc8ebe272010-01-11 22:42:21 +010083 struct nouveau_connector *nv_connector =
84 nouveau_connector(drm_connector);
Marcin Slusarzdd19e442010-01-30 15:41:00 +010085 struct drm_device *dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +100086
Xavier Chantryc8ebe272010-01-11 22:42:21 +010087 if (!nv_connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +100088 return;
89
Marcin Slusarzdd19e442010-01-30 15:41:00 +010090 dev = nv_connector->base.dev;
91 NV_DEBUG_KMS(dev, "\n");
92
Xavier Chantryc8ebe272010-01-11 22:42:21 +010093 kfree(nv_connector->edid);
Ben Skeggs6ee73862009-12-11 19:24:15 +100094 drm_sysfs_connector_remove(drm_connector);
95 drm_connector_cleanup(drm_connector);
96 kfree(drm_connector);
97}
98
Ben Skeggs6ee73862009-12-11 19:24:15 +100099static struct nouveau_i2c_chan *
100nouveau_connector_ddc_detect(struct drm_connector *connector,
101 struct nouveau_encoder **pnv_encoder)
102{
103 struct drm_device *dev = connector->dev;
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200104 int i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000105
Ben Skeggs6ee73862009-12-11 19:24:15 +1000106 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
Francisco Jerez4ca2b712010-08-08 21:35:57 +0200107 struct nouveau_i2c_chan *i2c = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000108 struct nouveau_encoder *nv_encoder;
109 struct drm_mode_object *obj;
110 int id;
111
112 id = connector->encoder_ids[i];
113 if (!id)
114 break;
115
116 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
117 if (!obj)
118 continue;
119 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
Francisco Jerez4ca2b712010-08-08 21:35:57 +0200120
121 if (nv_encoder->dcb->i2c_index < 0xf)
122 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000123
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200124 if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000125 *pnv_encoder = nv_encoder;
126 return i2c;
127 }
128 }
129
130 return NULL;
131}
132
Francisco Jerezc16c5702010-09-09 14:33:17 +0200133static struct nouveau_encoder *
134nouveau_connector_of_detect(struct drm_connector *connector)
135{
136#ifdef __powerpc__
137 struct drm_device *dev = connector->dev;
138 struct nouveau_connector *nv_connector = nouveau_connector(connector);
139 struct nouveau_encoder *nv_encoder;
140 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
141
142 if (!dn ||
143 !((nv_encoder = find_encoder_by_type(connector, OUTPUT_TMDS)) ||
144 (nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG))))
145 return NULL;
146
147 for_each_child_of_node(dn, cn) {
148 const char *name = of_get_property(cn, "name", NULL);
149 const void *edid = of_get_property(cn, "EDID", NULL);
150 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
151
152 if (nv_encoder->dcb->i2c_index == idx && edid) {
153 nv_connector->edid =
154 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
155 of_node_put(cn);
156 return nv_encoder;
157 }
158 }
159#endif
160 return NULL;
161}
162
Ben Skeggs6ee73862009-12-11 19:24:15 +1000163static void
164nouveau_connector_set_encoder(struct drm_connector *connector,
165 struct nouveau_encoder *nv_encoder)
166{
167 struct nouveau_connector *nv_connector = nouveau_connector(connector);
168 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
169 struct drm_device *dev = connector->dev;
170
171 if (nv_connector->detected_encoder == nv_encoder)
172 return;
173 nv_connector->detected_encoder = nv_encoder;
174
175 if (nv_encoder->dcb->type == OUTPUT_LVDS ||
176 nv_encoder->dcb->type == OUTPUT_TMDS) {
177 connector->doublescan_allowed = false;
178 connector->interlace_allowed = false;
179 } else {
180 connector->doublescan_allowed = true;
181 if (dev_priv->card_type == NV_20 ||
182 (dev_priv->card_type == NV_10 &&
183 (dev->pci_device & 0x0ff0) != 0x0100 &&
184 (dev->pci_device & 0x0ff0) != 0x0150))
185 /* HW is broken */
186 connector->interlace_allowed = false;
187 else
188 connector->interlace_allowed = true;
189 }
190
Ben Skeggsbe079e92010-02-24 14:01:40 +1000191 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000192 drm_connector_property_set_value(connector,
193 dev->mode_config.dvi_i_subconnector_property,
194 nv_encoder->dcb->type == OUTPUT_TMDS ?
195 DRM_MODE_SUBCONNECTOR_DVID :
196 DRM_MODE_SUBCONNECTOR_DVIA);
197 }
198}
199
200static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100201nouveau_connector_detect(struct drm_connector *connector, bool force)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000202{
203 struct drm_device *dev = connector->dev;
204 struct nouveau_connector *nv_connector = nouveau_connector(connector);
205 struct nouveau_encoder *nv_encoder = NULL;
206 struct nouveau_i2c_chan *i2c;
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200207 int type;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000208
Francisco Jerezb8780e22010-01-14 15:54:23 +0100209 /* Cleanup the previous EDID block. */
210 if (nv_connector->edid) {
211 drm_mode_connector_update_edid_property(connector, NULL);
212 kfree(nv_connector->edid);
213 nv_connector->edid = NULL;
214 }
Xavier Chantryc8ebe272010-01-11 22:42:21 +0100215
Ben Skeggs6ee73862009-12-11 19:24:15 +1000216 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
217 if (i2c) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000218 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000219 drm_mode_connector_update_edid_property(connector,
220 nv_connector->edid);
221 if (!nv_connector->edid) {
222 NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
223 drm_get_connector_name(connector));
Francisco Jerez0ed31652010-01-14 15:47:03 +0100224 goto detect_analog;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000225 }
226
227 if (nv_encoder->dcb->type == OUTPUT_DP &&
228 !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
229 NV_ERROR(dev, "Detected %s, but failed init\n",
230 drm_get_connector_name(connector));
231 return connector_status_disconnected;
232 }
233
234 /* Override encoder type for DVI-I based on whether EDID
235 * says the display is digital or analog, both use the
236 * same i2c channel so the value returned from ddc_detect
237 * isn't necessarily correct.
238 */
Ben Skeggsbe079e92010-02-24 14:01:40 +1000239 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000240 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
241 type = OUTPUT_TMDS;
242 else
243 type = OUTPUT_ANALOG;
244
245 nv_encoder = find_encoder_by_type(connector, type);
246 if (!nv_encoder) {
247 NV_ERROR(dev, "Detected %d encoder on %s, "
248 "but no object!\n", type,
249 drm_get_connector_name(connector));
250 return connector_status_disconnected;
251 }
252 }
253
254 nouveau_connector_set_encoder(connector, nv_encoder);
255 return connector_status_connected;
256 }
257
Francisco Jerezc16c5702010-09-09 14:33:17 +0200258 nv_encoder = nouveau_connector_of_detect(connector);
259 if (nv_encoder) {
260 nouveau_connector_set_encoder(connector, nv_encoder);
261 return connector_status_connected;
262 }
263
Francisco Jerez0ed31652010-01-14 15:47:03 +0100264detect_analog:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000265 nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
Ben Skeggsf4053502010-03-15 09:43:51 +1000266 if (!nv_encoder && !nouveau_tv_disable)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000267 nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
268 if (nv_encoder) {
269 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
270 struct drm_encoder_helper_funcs *helper =
271 encoder->helper_private;
272
273 if (helper->detect(encoder, connector) ==
274 connector_status_connected) {
275 nouveau_connector_set_encoder(connector, nv_encoder);
276 return connector_status_connected;
277 }
278
279 }
280
281 return connector_status_disconnected;
282}
283
Ben Skeggsd17f3952010-06-01 13:32:42 +1000284static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100285nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
Ben Skeggsd17f3952010-06-01 13:32:42 +1000286{
287 struct drm_device *dev = connector->dev;
288 struct drm_nouveau_private *dev_priv = dev->dev_private;
289 struct nouveau_connector *nv_connector = nouveau_connector(connector);
290 struct nouveau_encoder *nv_encoder = NULL;
291 enum drm_connector_status status = connector_status_disconnected;
292
293 /* Cleanup the previous EDID block. */
294 if (nv_connector->edid) {
295 drm_mode_connector_update_edid_property(connector, NULL);
296 kfree(nv_connector->edid);
297 nv_connector->edid = NULL;
298 }
299
300 nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
301 if (!nv_encoder)
302 return connector_status_disconnected;
303
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000304 /* Try retrieving EDID via DDC */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000305 if (!dev_priv->vbios.fp_no_ddc) {
Chris Wilson930a9e22010-09-14 11:07:23 +0100306 status = nouveau_connector_detect(connector, force);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000307 if (status == connector_status_connected)
308 goto out;
309 }
310
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000311 /* On some laptops (Sony, i'm looking at you) there appears to
312 * be no direct way of accessing the panel's EDID. The only
313 * option available to us appears to be to ask ACPI for help..
314 *
315 * It's important this check's before trying straps, one of the
316 * said manufacturer's laptops are configured in such a way
317 * the nouveau decides an entry in the VBIOS FP mode table is
318 * valid - it's not (rh#613284)
319 */
320 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
321 if (!nouveau_acpi_edid(dev, connector)) {
322 status = connector_status_connected;
323 goto out;
324 }
325 }
326
Ben Skeggsd17f3952010-06-01 13:32:42 +1000327 /* If no EDID found above, and the VBIOS indicates a hardcoded
328 * modeline is avalilable for the panel, set it as the panel's
329 * native mode and exit.
330 */
331 if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
332 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
333 status = connector_status_connected;
334 goto out;
335 }
336
337 /* Still nothing, some VBIOS images have a hardcoded EDID block
338 * stored for the panel stored in them.
339 */
340 if (!dev_priv->vbios.fp_no_ddc) {
341 struct edid *edid =
342 (struct edid *)nouveau_bios_embedded_edid(dev);
343 if (edid) {
344 nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
345 *(nv_connector->edid) = *edid;
346 status = connector_status_connected;
347 }
348 }
349
350out:
351#if defined(CONFIG_ACPI_BUTTON) || \
352 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
353 if (status == connector_status_connected &&
354 !nouveau_ignorelid && !acpi_lid_open())
355 status = connector_status_unknown;
356#endif
357
358 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
Albert Damen3195c5f2010-06-20 16:57:57 +0200359 nouveau_connector_set_encoder(connector, nv_encoder);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000360 return status;
361}
362
Ben Skeggs6ee73862009-12-11 19:24:15 +1000363static void
364nouveau_connector_force(struct drm_connector *connector)
365{
Ben Skeggsbe079e92010-02-24 14:01:40 +1000366 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000367 struct nouveau_encoder *nv_encoder;
368 int type;
369
Ben Skeggsbe079e92010-02-24 14:01:40 +1000370 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000371 if (connector->force == DRM_FORCE_ON_DIGITAL)
372 type = OUTPUT_TMDS;
373 else
374 type = OUTPUT_ANALOG;
375 } else
376 type = OUTPUT_ANY;
377
378 nv_encoder = find_encoder_by_type(connector, type);
379 if (!nv_encoder) {
Ben Skeggsbe079e92010-02-24 14:01:40 +1000380 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000381 drm_get_connector_name(connector));
382 connector->status = connector_status_disconnected;
383 return;
384 }
385
386 nouveau_connector_set_encoder(connector, nv_encoder);
387}
388
389static int
390nouveau_connector_set_property(struct drm_connector *connector,
391 struct drm_property *property, uint64_t value)
392{
393 struct nouveau_connector *nv_connector = nouveau_connector(connector);
394 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200395 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000396 struct drm_device *dev = connector->dev;
397 int ret;
398
399 /* Scaling mode */
400 if (property == dev->mode_config.scaling_mode_property) {
401 struct nouveau_crtc *nv_crtc = NULL;
402 bool modeset = false;
403
404 switch (value) {
405 case DRM_MODE_SCALE_NONE:
406 case DRM_MODE_SCALE_FULLSCREEN:
407 case DRM_MODE_SCALE_CENTER:
408 case DRM_MODE_SCALE_ASPECT:
409 break;
410 default:
411 return -EINVAL;
412 }
413
414 /* LVDS always needs gpu scaling */
Ben Skeggsbe079e92010-02-24 14:01:40 +1000415 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000416 value == DRM_MODE_SCALE_NONE)
417 return -EINVAL;
418
419 /* Changing between GPU and panel scaling requires a full
420 * modeset
421 */
422 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
423 (value == DRM_MODE_SCALE_NONE))
424 modeset = true;
425 nv_connector->scaling_mode = value;
426
427 if (connector->encoder && connector->encoder->crtc)
428 nv_crtc = nouveau_crtc(connector->encoder->crtc);
429 if (!nv_crtc)
430 return 0;
431
432 if (modeset || !nv_crtc->set_scale) {
433 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
434 &nv_crtc->base.mode,
435 nv_crtc->base.x,
436 nv_crtc->base.y, NULL);
437 if (!ret)
438 return -EINVAL;
439 } else {
440 ret = nv_crtc->set_scale(nv_crtc, value, true);
441 if (ret)
442 return ret;
443 }
444
445 return 0;
446 }
447
448 /* Dithering */
449 if (property == dev->mode_config.dithering_mode_property) {
450 struct nouveau_crtc *nv_crtc = NULL;
451
452 if (value == DRM_MODE_DITHERING_ON)
453 nv_connector->use_dithering = true;
454 else
455 nv_connector->use_dithering = false;
456
457 if (connector->encoder && connector->encoder->crtc)
458 nv_crtc = nouveau_crtc(connector->encoder->crtc);
459
460 if (!nv_crtc || !nv_crtc->set_dither)
461 return 0;
462
463 return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
464 true);
465 }
466
467 if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200468 return get_slave_funcs(encoder)->set_property(
469 encoder, connector, property, value);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000470
471 return -EINVAL;
472}
473
474static struct drm_display_mode *
Ben Skeggs26099a72010-05-28 10:13:16 +1000475nouveau_connector_native_mode(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000476{
Ben Skeggs26099a72010-05-28 10:13:16 +1000477 struct drm_connector_helper_funcs *helper = connector->helper_private;
478 struct nouveau_connector *nv_connector = nouveau_connector(connector);
479 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000480 struct drm_display_mode *mode, *largest = NULL;
481 int high_w = 0, high_h = 0, high_v = 0;
482
Ben Skeggs26099a72010-05-28 10:13:16 +1000483 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
Francisco Jereza5afb772010-07-18 16:19:16 +0200484 if (helper->mode_valid(connector, mode) != MODE_OK ||
485 (mode->flags & DRM_MODE_FLAG_INTERLACE))
Ben Skeggs26099a72010-05-28 10:13:16 +1000486 continue;
487
488 /* Use preferred mode if there is one.. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000489 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100490 NV_DEBUG_KMS(dev, "native mode from preferred\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000491 return drm_mode_duplicate(dev, mode);
492 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000493
Ben Skeggs26099a72010-05-28 10:13:16 +1000494 /* Otherwise, take the resolution with the largest width, then
495 * height, then vertical refresh
496 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000497 if (mode->hdisplay < high_w)
498 continue;
499
500 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
501 continue;
502
503 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
504 mode->vrefresh < high_v)
505 continue;
506
507 high_w = mode->hdisplay;
508 high_h = mode->vdisplay;
509 high_v = mode->vrefresh;
510 largest = mode;
511 }
512
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100513 NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000514 high_w, high_h, high_v);
515 return largest ? drm_mode_duplicate(dev, largest) : NULL;
516}
517
518struct moderec {
519 int hdisplay;
520 int vdisplay;
521};
522
523static struct moderec scaler_modes[] = {
524 { 1920, 1200 },
525 { 1920, 1080 },
526 { 1680, 1050 },
527 { 1600, 1200 },
528 { 1400, 1050 },
529 { 1280, 1024 },
530 { 1280, 960 },
531 { 1152, 864 },
532 { 1024, 768 },
533 { 800, 600 },
534 { 720, 400 },
535 { 640, 480 },
536 { 640, 400 },
537 { 640, 350 },
538 {}
539};
540
541static int
542nouveau_connector_scaler_modes_add(struct drm_connector *connector)
543{
544 struct nouveau_connector *nv_connector = nouveau_connector(connector);
545 struct drm_display_mode *native = nv_connector->native_mode, *m;
546 struct drm_device *dev = connector->dev;
547 struct moderec *mode = &scaler_modes[0];
548 int modes = 0;
549
550 if (!native)
551 return 0;
552
553 while (mode->hdisplay) {
554 if (mode->hdisplay <= native->hdisplay &&
555 mode->vdisplay <= native->vdisplay) {
556 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
557 drm_mode_vrefresh(native), false,
558 false, false);
559 if (!m)
560 continue;
561
562 m->type |= DRM_MODE_TYPE_DRIVER;
563
564 drm_mode_probed_add(connector, m);
565 modes++;
566 }
567
568 mode++;
569 }
570
571 return modes;
572}
573
574static int
575nouveau_connector_get_modes(struct drm_connector *connector)
576{
577 struct drm_device *dev = connector->dev;
Ben Skeggsd17f3952010-06-01 13:32:42 +1000578 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000579 struct nouveau_connector *nv_connector = nouveau_connector(connector);
580 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200581 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000582 int ret = 0;
583
Ben Skeggsd17f3952010-06-01 13:32:42 +1000584 /* destroy the native mode, the attached monitor could have changed.
Ben Skeggs6ee73862009-12-11 19:24:15 +1000585 */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000586 if (nv_connector->native_mode) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000587 drm_mode_destroy(dev, nv_connector->native_mode);
588 nv_connector->native_mode = NULL;
589 }
590
591 if (nv_connector->edid)
592 ret = drm_add_edid_modes(connector, nv_connector->edid);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000593 else
594 if (nv_encoder->dcb->type == OUTPUT_LVDS &&
595 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
Francisco Jerez6e86e042010-07-03 18:36:39 +0200596 dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
Ben Skeggs80dad862010-09-23 16:37:39 +1000597 struct drm_display_mode mode;
598
599 nouveau_bios_fp_mode(dev, &mode);
600 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000601 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000602
603 /* Find the native mode if this is a digital panel, if we didn't
604 * find any modes through DDC previously add the native mode to
605 * the list of modes.
606 */
607 if (!nv_connector->native_mode)
608 nv_connector->native_mode =
Ben Skeggs26099a72010-05-28 10:13:16 +1000609 nouveau_connector_native_mode(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610 if (ret == 0 && nv_connector->native_mode) {
611 struct drm_display_mode *mode;
612
613 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
614 drm_mode_probed_add(connector, mode);
615 ret = 1;
616 }
617
618 if (nv_encoder->dcb->type == OUTPUT_TV)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200619 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000620
Ben Skeggs646bef22010-07-06 11:27:52 +1000621 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
622 nv_connector->dcb->type == DCB_CONNECTOR_eDP)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000623 ret += nouveau_connector_scaler_modes_add(connector);
624
625 return ret;
626}
627
628static int
629nouveau_connector_mode_valid(struct drm_connector *connector,
630 struct drm_display_mode *mode)
631{
632 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
633 struct nouveau_connector *nv_connector = nouveau_connector(connector);
634 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200635 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000636 unsigned min_clock = 25000, max_clock = min_clock;
637 unsigned clock = mode->clock;
638
639 switch (nv_encoder->dcb->type) {
640 case OUTPUT_LVDS:
Ben Skeggs26099a72010-05-28 10:13:16 +1000641 if (nv_connector->native_mode &&
642 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
643 mode->vdisplay > nv_connector->native_mode->vdisplay))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000644 return MODE_PANEL;
645
646 min_clock = 0;
647 max_clock = 400000;
648 break;
649 case OUTPUT_TMDS:
650 if ((dev_priv->card_type >= NV_50 && !nouveau_duallink) ||
Ben Skeggs2c580772010-05-28 10:04:05 +1000651 !nv_encoder->dcb->duallink_possible)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000652 max_clock = 165000;
653 else
654 max_clock = 330000;
655 break;
656 case OUTPUT_ANALOG:
657 max_clock = nv_encoder->dcb->crtconf.maxfreq;
658 if (!max_clock)
659 max_clock = 350000;
660 break;
661 case OUTPUT_TV:
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200662 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000663 case OUTPUT_DP:
664 if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
665 max_clock = nv_encoder->dp.link_nr * 270000;
666 else
667 max_clock = nv_encoder->dp.link_nr * 162000;
668
669 clock *= 3;
670 break;
Ben Skeggse7cc51c2010-02-24 10:31:39 +1000671 default:
672 BUG_ON(1);
673 return MODE_BAD;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000674 }
675
676 if (clock < min_clock)
677 return MODE_CLOCK_LOW;
678
679 if (clock > max_clock)
680 return MODE_CLOCK_HIGH;
681
682 return MODE_OK;
683}
684
685static struct drm_encoder *
686nouveau_connector_best_encoder(struct drm_connector *connector)
687{
688 struct nouveau_connector *nv_connector = nouveau_connector(connector);
689
690 if (nv_connector->detected_encoder)
691 return to_drm_encoder(nv_connector->detected_encoder);
692
693 return NULL;
694}
695
Francisco Jerez2ed06b72010-07-03 15:52:03 +0200696void
697nouveau_connector_set_polling(struct drm_connector *connector)
698{
699 struct drm_device *dev = connector->dev;
700 struct drm_nouveau_private *dev_priv = dev->dev_private;
701 struct drm_crtc *crtc;
702 bool spare_crtc = false;
703
704 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
705 spare_crtc |= !crtc->enabled;
706
707 connector->polled = 0;
708
709 switch (connector->connector_type) {
710 case DRM_MODE_CONNECTOR_VGA:
711 case DRM_MODE_CONNECTOR_TV:
712 if (dev_priv->card_type >= NV_50 ||
713 (nv_gf4_disp_arch(dev) && spare_crtc))
714 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
715 break;
716
717 case DRM_MODE_CONNECTOR_DVII:
718 case DRM_MODE_CONNECTOR_DVID:
719 case DRM_MODE_CONNECTOR_HDMIA:
720 case DRM_MODE_CONNECTOR_DisplayPort:
721 case DRM_MODE_CONNECTOR_eDP:
722 if (dev_priv->card_type >= NV_50)
723 connector->polled = DRM_CONNECTOR_POLL_HPD;
724 else if (connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
725 spare_crtc)
726 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
727 break;
728
729 default:
730 break;
731 }
732}
733
Ben Skeggs6ee73862009-12-11 19:24:15 +1000734static const struct drm_connector_helper_funcs
735nouveau_connector_helper_funcs = {
736 .get_modes = nouveau_connector_get_modes,
737 .mode_valid = nouveau_connector_mode_valid,
738 .best_encoder = nouveau_connector_best_encoder,
739};
740
741static const struct drm_connector_funcs
742nouveau_connector_funcs = {
743 .dpms = drm_helper_connector_dpms,
744 .save = NULL,
745 .restore = NULL,
746 .detect = nouveau_connector_detect,
747 .destroy = nouveau_connector_destroy,
748 .fill_modes = drm_helper_probe_single_connector_modes,
749 .set_property = nouveau_connector_set_property,
750 .force = nouveau_connector_force
751};
752
Ben Skeggsd17f3952010-06-01 13:32:42 +1000753static const struct drm_connector_funcs
754nouveau_connector_funcs_lvds = {
755 .dpms = drm_helper_connector_dpms,
756 .save = NULL,
757 .restore = NULL,
758 .detect = nouveau_connector_detect_lvds,
759 .destroy = nouveau_connector_destroy,
760 .fill_modes = drm_helper_probe_single_connector_modes,
761 .set_property = nouveau_connector_set_property,
762 .force = nouveau_connector_force
763};
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000765struct drm_connector *
766nouveau_connector_create(struct drm_device *dev, int index)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000767{
Ben Skeggsd17f3952010-06-01 13:32:42 +1000768 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000769 struct drm_nouveau_private *dev_priv = dev->dev_private;
770 struct nouveau_connector *nv_connector = NULL;
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000771 struct dcb_connector_table_entry *dcb = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000772 struct drm_connector *connector;
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200773 int type, ret = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000774
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100775 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000777 if (index >= dev_priv->vbios.dcb.connector.entries)
778 return ERR_PTR(-EINVAL);
779
780 dcb = &dev_priv->vbios.dcb.connector.entry[index];
781 if (dcb->drm)
782 return dcb->drm;
783
Ben Skeggs7f612d82010-02-24 12:07:31 +1000784 switch (dcb->type) {
Ben Skeggs7f612d82010-02-24 12:07:31 +1000785 case DCB_CONNECTOR_VGA:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000786 type = DRM_MODE_CONNECTOR_VGA;
787 break;
788 case DCB_CONNECTOR_TV_0:
789 case DCB_CONNECTOR_TV_1:
790 case DCB_CONNECTOR_TV_3:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000791 type = DRM_MODE_CONNECTOR_TV;
792 break;
793 case DCB_CONNECTOR_DVI_I:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000794 type = DRM_MODE_CONNECTOR_DVII;
795 break;
796 case DCB_CONNECTOR_DVI_D:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000797 type = DRM_MODE_CONNECTOR_DVID;
798 break;
Ben Skeggsbe079e92010-02-24 14:01:40 +1000799 case DCB_CONNECTOR_HDMI_0:
800 case DCB_CONNECTOR_HDMI_1:
Ben Skeggsbe079e92010-02-24 14:01:40 +1000801 type = DRM_MODE_CONNECTOR_HDMIA;
802 break;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000803 case DCB_CONNECTOR_LVDS:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000804 type = DRM_MODE_CONNECTOR_LVDS;
Ben Skeggsd17f3952010-06-01 13:32:42 +1000805 funcs = &nouveau_connector_funcs_lvds;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000806 break;
807 case DCB_CONNECTOR_DP:
Ben Skeggs7f612d82010-02-24 12:07:31 +1000808 type = DRM_MODE_CONNECTOR_DisplayPort;
809 break;
Ben Skeggsbe079e92010-02-24 14:01:40 +1000810 case DCB_CONNECTOR_eDP:
Ben Skeggsbe079e92010-02-24 14:01:40 +1000811 type = DRM_MODE_CONNECTOR_eDP;
812 break;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000813 default:
814 NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000815 return ERR_PTR(-EINVAL);
Ben Skeggs7f612d82010-02-24 12:07:31 +1000816 }
817
Ben Skeggs6ee73862009-12-11 19:24:15 +1000818 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
819 if (!nv_connector)
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000820 return ERR_PTR(-ENOMEM);
Ben Skeggs7f612d82010-02-24 12:07:31 +1000821 nv_connector->dcb = dcb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000822 connector = &nv_connector->base;
823
Ben Skeggs6ee73862009-12-11 19:24:15 +1000824 /* defaults, will get overridden in detect() */
825 connector->interlace_allowed = false;
826 connector->doublescan_allowed = false;
827
Ben Skeggsd17f3952010-06-01 13:32:42 +1000828 drm_connector_init(dev, connector, funcs, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000829 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
830
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200831 /* Check if we need dithering enabled */
832 if (dcb->type == DCB_CONNECTOR_LVDS) {
833 bool dummy, is_24bit = false;
834
835 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
836 if (ret) {
837 NV_ERROR(dev, "Error parsing LVDS table, disabling "
838 "LVDS\n");
839 goto fail;
840 }
841
842 nv_connector->use_dithering = !is_24bit;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000843 }
844
Ben Skeggs6ee73862009-12-11 19:24:15 +1000845 /* Init DVI-I specific properties */
Ben Skeggsbe079e92010-02-24 14:01:40 +1000846 if (dcb->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000847 drm_mode_create_dvi_i_properties(dev);
848 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
849 drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
850 }
851
Ben Skeggsbe079e92010-02-24 14:01:40 +1000852 switch (dcb->type) {
853 case DCB_CONNECTOR_VGA:
854 if (dev_priv->card_type >= NV_50) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000855 drm_connector_attach_property(connector,
856 dev->mode_config.scaling_mode_property,
857 nv_connector->scaling_mode);
858 }
Ben Skeggsbe079e92010-02-24 14:01:40 +1000859 /* fall-through */
860 case DCB_CONNECTOR_TV_0:
861 case DCB_CONNECTOR_TV_1:
862 case DCB_CONNECTOR_TV_3:
863 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
864 break;
865 default:
866 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
867
868 drm_connector_attach_property(connector,
869 dev->mode_config.scaling_mode_property,
870 nv_connector->scaling_mode);
871 drm_connector_attach_property(connector,
872 dev->mode_config.dithering_mode_property,
873 nv_connector->use_dithering ?
874 DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
875 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000876 }
877
Francisco Jerez2ed06b72010-07-03 15:52:03 +0200878 nouveau_connector_set_polling(connector);
879
Ben Skeggs6ee73862009-12-11 19:24:15 +1000880 drm_sysfs_connector_add(connector);
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000881 dcb->drm = connector;
882 return dcb->drm;
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200883
884fail:
885 drm_connector_cleanup(connector);
886 kfree(connector);
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000887 return ERR_PTR(ret);
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200888
Ben Skeggs6ee73862009-12-11 19:24:15 +1000889}