Commit | Line | Data |
---|---|---|
6ee73861 BS |
1 | /* |
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 | ||
a1470890 BS |
27 | #include <acpi/button.h> |
28 | ||
6ee73861 BS |
29 | #include "drmP.h" |
30 | #include "drm_edid.h" | |
31 | #include "drm_crtc_helper.h" | |
a1470890 | 32 | |
6ee73861 BS |
33 | #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 | ||
40 | static inline struct drm_encoder_slave_funcs * | |
41 | get_slave_funcs(struct nouveau_encoder *enc) | |
42 | { | |
43 | return to_encoder_slave(to_drm_encoder(enc))->slave_funcs; | |
44 | } | |
45 | ||
46 | static struct nouveau_encoder * | |
47 | find_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 | ||
71 | struct nouveau_connector * | |
72 | nouveau_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 | ||
86 | static void | |
87 | nouveau_connector_destroy(struct drm_connector *drm_connector) | |
88 | { | |
c8ebe275 XC |
89 | struct nouveau_connector *nv_connector = |
90 | nouveau_connector(drm_connector); | |
dd19e44b | 91 | struct drm_device *dev; |
6ee73861 | 92 | |
c8ebe275 | 93 | if (!nv_connector) |
6ee73861 BS |
94 | return; |
95 | ||
dd19e44b MS |
96 | dev = nv_connector->base.dev; |
97 | NV_DEBUG_KMS(dev, "\n"); | |
98 | ||
c8ebe275 | 99 | kfree(nv_connector->edid); |
6ee73861 BS |
100 | drm_sysfs_connector_remove(drm_connector); |
101 | drm_connector_cleanup(drm_connector); | |
102 | kfree(drm_connector); | |
103 | } | |
104 | ||
105 | static void | |
106 | nouveau_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 | ||
123 | static void | |
124 | nouveau_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 | ||
137 | static struct nouveau_i2c_chan * | |
138 | nouveau_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 | ||
193 | static void | |
194 | nouveau_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 | ||
be079e97 | 221 | if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) { |
6ee73861 BS |
222 | 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 | ||
230 | static enum drm_connector_status | |
231 | nouveau_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 | ||
b8780e2a FJ |
239 | /* 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 | } | |
c8ebe275 | 245 | |
6ee73861 BS |
246 | 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)); | |
0ed3165e | 256 | goto detect_analog; |
6ee73861 BS |
257 | } |
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 | */ | |
be079e97 | 271 | if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) { |
6ee73861 BS |
272 | 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 | ||
0ed3165e | 290 | detect_analog: |
6ee73861 | 291 | nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG); |
f4053509 | 292 | if (!nv_encoder && !nouveau_tv_disable) |
6ee73861 BS |
293 | 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 | ||
d17f395c BS |
310 | static enum drm_connector_status |
311 | nouveau_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 | ||
a6ed76d7 | 330 | /* Try retrieving EDID via DDC */ |
d17f395c BS |
331 | if (!dev_priv->vbios.fp_no_ddc) { |
332 | status = nouveau_connector_detect(connector); | |
333 | if (status == connector_status_connected) | |
334 | goto out; | |
335 | } | |
336 | ||
a6ed76d7 BS |
337 | /* 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 | ||
d17f395c BS |
353 | /* 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 | ||
376 | out: | |
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); | |
3195c5f9 | 385 | nouveau_connector_set_encoder(connector, nv_encoder); |
d17f395c BS |
386 | return status; |
387 | } | |
388 | ||
6ee73861 BS |
389 | static void |
390 | nouveau_connector_force(struct drm_connector *connector) | |
391 | { | |
be079e97 | 392 | struct nouveau_connector *nv_connector = nouveau_connector(connector); |
6ee73861 BS |
393 | struct nouveau_encoder *nv_encoder; |
394 | int type; | |
395 | ||
be079e97 | 396 | if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) { |
6ee73861 BS |
397 | 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) { | |
be079e97 | 406 | NV_ERROR(connector->dev, "can't find encoder to force %s on!\n", |
6ee73861 BS |
407 | 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 | ||
415 | static int | |
416 | nouveau_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 */ | |
be079e97 | 440 | if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS && |
6ee73861 BS |
441 | 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 | ||
499 | static struct drm_display_mode * | |
26099a74 | 500 | nouveau_connector_native_mode(struct drm_connector *connector) |
6ee73861 | 501 | { |
26099a74 BS |
502 | 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; | |
6ee73861 BS |
505 | struct drm_display_mode *mode, *largest = NULL; |
506 | int high_w = 0, high_h = 0, high_v = 0; | |
507 | ||
26099a74 BS |
508 | 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.. */ | |
6ee73861 | 513 | if (mode->type & DRM_MODE_TYPE_PREFERRED) { |
ef2bb506 | 514 | NV_DEBUG_KMS(dev, "native mode from preferred\n"); |
6ee73861 BS |
515 | return drm_mode_duplicate(dev, mode); |
516 | } | |
6ee73861 | 517 | |
26099a74 BS |
518 | /* Otherwise, take the resolution with the largest width, then |
519 | * height, then vertical refresh | |
520 | */ | |
6ee73861 BS |
521 | 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 | ||
ef2bb506 | 537 | NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n", |
6ee73861 BS |
538 | high_w, high_h, high_v); |
539 | return largest ? drm_mode_duplicate(dev, largest) : NULL; | |
540 | } | |
541 | ||
542 | struct moderec { | |
543 | int hdisplay; | |
544 | int vdisplay; | |
545 | }; | |
546 | ||
547 | static 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 | ||
565 | static int | |
566 | nouveau_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 | ||
598 | static int | |
599 | nouveau_connector_get_modes(struct drm_connector *connector) | |
600 | { | |
601 | struct drm_device *dev = connector->dev; | |
d17f395c | 602 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
6ee73861 BS |
603 | struct nouveau_connector *nv_connector = nouveau_connector(connector); |
604 | struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder; | |
605 | int ret = 0; | |
606 | ||
d17f395c | 607 | /* destroy the native mode, the attached monitor could have changed. |
6ee73861 | 608 | */ |
d17f395c | 609 | if (nv_connector->native_mode) { |
6ee73861 BS |
610 | 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); | |
d17f395c BS |
616 | else |
617 | if (nv_encoder->dcb->type == OUTPUT_LVDS && | |
618 | (nv_encoder->dcb->lvdsconf.use_straps_for_mode || | |
6e86e041 FJ |
619 | 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); | |
d17f395c | 622 | } |
6ee73861 BS |
623 | |
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 = | |
26099a74 | 630 | nouveau_connector_native_mode(connector); |
6ee73861 BS |
631 | 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 | ||
646bef2d BS |
643 | if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS || |
644 | nv_connector->dcb->type == DCB_CONNECTOR_eDP) | |
6ee73861 BS |
645 | ret += nouveau_connector_scaler_modes_add(connector); |
646 | ||
647 | return ret; | |
648 | } | |
649 | ||
650 | static int | |
651 | nouveau_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: | |
26099a74 BS |
662 | if (nv_connector->native_mode && |
663 | (mode->hdisplay > nv_connector->native_mode->hdisplay || | |
664 | mode->vdisplay > nv_connector->native_mode->vdisplay)) | |
6ee73861 BS |
665 | 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) || | |
2c580775 | 672 | !nv_encoder->dcb->duallink_possible) |
6ee73861 BS |
673 | 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; | |
e7cc51c5 BS |
693 | default: |
694 | BUG_ON(1); | |
695 | return MODE_BAD; | |
6ee73861 BS |
696 | } |
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 | ||
707 | static struct drm_encoder * | |
708 | nouveau_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 | ||
2ed06b7d FJ |
718 | void |
719 | nouveau_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 | ||
6ee73861 BS |
756 | static const struct drm_connector_helper_funcs |
757 | nouveau_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 | ||
763 | static const struct drm_connector_funcs | |
764 | nouveau_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 | ||
d17f395c BS |
775 | static const struct drm_connector_funcs |
776 | nouveau_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 | }; | |
6ee73861 | 786 | |
8f1a6086 BS |
787 | struct drm_connector * |
788 | nouveau_connector_create(struct drm_device *dev, int index) | |
6ee73861 | 789 | { |
d17f395c | 790 | const struct drm_connector_funcs *funcs = &nouveau_connector_funcs; |
6ee73861 BS |
791 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
792 | struct nouveau_connector *nv_connector = NULL; | |
8f1a6086 | 793 | struct dcb_connector_table_entry *dcb = NULL; |
6ee73861 | 794 | struct drm_connector *connector; |
2fa67f12 | 795 | int type, ret = 0; |
6ee73861 | 796 | |
ef2bb506 | 797 | NV_DEBUG_KMS(dev, "\n"); |
6ee73861 | 798 | |
8f1a6086 BS |
799 | 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 | ||
7f612d87 | 806 | switch (dcb->type) { |
7f612d87 | 807 | case DCB_CONNECTOR_VGA: |
7f612d87 | 808 | type = DRM_MODE_CONNECTOR_VGA; |
6ee73861 | 809 | break; |
7f612d87 BS |
810 | case DCB_CONNECTOR_TV_0: |
811 | case DCB_CONNECTOR_TV_1: | |
812 | case DCB_CONNECTOR_TV_3: | |
7f612d87 | 813 | type = DRM_MODE_CONNECTOR_TV; |
6ee73861 | 814 | break; |
7f612d87 | 815 | case DCB_CONNECTOR_DVI_I: |
7f612d87 | 816 | type = DRM_MODE_CONNECTOR_DVII; |
6ee73861 | 817 | break; |
7f612d87 | 818 | case DCB_CONNECTOR_DVI_D: |
7f612d87 | 819 | type = DRM_MODE_CONNECTOR_DVID; |
6ee73861 | 820 | break; |
be079e97 BS |
821 | case DCB_CONNECTOR_HDMI_0: |
822 | case DCB_CONNECTOR_HDMI_1: | |
be079e97 BS |
823 | type = DRM_MODE_CONNECTOR_HDMIA; |
824 | break; | |
7f612d87 | 825 | case DCB_CONNECTOR_LVDS: |
7f612d87 | 826 | type = DRM_MODE_CONNECTOR_LVDS; |
d17f395c | 827 | funcs = &nouveau_connector_funcs_lvds; |
6ee73861 | 828 | break; |
7f612d87 | 829 | case DCB_CONNECTOR_DP: |
7f612d87 | 830 | type = DRM_MODE_CONNECTOR_DisplayPort; |
6ee73861 | 831 | break; |
be079e97 | 832 | case DCB_CONNECTOR_eDP: |
be079e97 BS |
833 | type = DRM_MODE_CONNECTOR_eDP; |
834 | break; | |
6ee73861 | 835 | default: |
7f612d87 | 836 | NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type); |
8f1a6086 | 837 | return ERR_PTR(-EINVAL); |
6ee73861 BS |
838 | } |
839 | ||
7f612d87 BS |
840 | nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL); |
841 | if (!nv_connector) | |
8f1a6086 | 842 | return ERR_PTR(-ENOMEM); |
7f612d87 BS |
843 | nv_connector->dcb = dcb; |
844 | connector = &nv_connector->base; | |
845 | ||
6ee73861 BS |
846 | /* defaults, will get overridden in detect() */ |
847 | connector->interlace_allowed = false; | |
848 | connector->doublescan_allowed = false; | |
849 | ||
d17f395c | 850 | drm_connector_init(dev, connector, funcs, type); |
6ee73861 BS |
851 | drm_connector_helper_add(connector, &nouveau_connector_helper_funcs); |
852 | ||
2fa67f12 FJ |
853 | /* 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; | |
7f612d87 BS |
865 | } |
866 | ||
6ee73861 | 867 | /* Init DVI-I specific properties */ |
be079e97 | 868 | if (dcb->type == DCB_CONNECTOR_DVI_I) { |
6ee73861 BS |
869 | 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 | ||
be079e97 BS |
874 | switch (dcb->type) { |
875 | case DCB_CONNECTOR_VGA: | |
876 | if (dev_priv->card_type >= NV_50) { | |
6ee73861 BS |
877 | drm_connector_attach_property(connector, |
878 | dev->mode_config.scaling_mode_property, | |
879 | nv_connector->scaling_mode); | |
880 | } | |
be079e97 BS |
881 | /* 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; | |
6ee73861 BS |
898 | } |
899 | ||
2ed06b7d FJ |
900 | nouveau_connector_set_polling(connector); |
901 | ||
6ee73861 | 902 | drm_sysfs_connector_add(connector); |
8f1a6086 BS |
903 | dcb->drm = connector; |
904 | return dcb->drm; | |
2fa67f12 FJ |
905 | |
906 | fail: | |
907 | drm_connector_cleanup(connector); | |
908 | kfree(connector); | |
8f1a6086 | 909 | return ERR_PTR(ret); |
2fa67f12 | 910 | |
6ee73861 | 911 | } |