blob: 5dbf2e45993d722edc4a1248b6b371abc4f8f956 [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"
Ben Skeggsc0077062012-07-26 08:51:21 +100039#include "nouveau_acpi.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100040
Ben Skeggse0996ae2012-07-10 12:20:17 +100041#include <subdev/bios/gpio.h>
42
Ben Skeggsfce2bad2010-11-11 16:14:56 +100043static void nouveau_connector_hotplug(void *, int);
44
Ben Skeggs10b461e2011-08-02 19:29:37 +100045struct nouveau_encoder *
Ben Skeggse19b20b2011-07-12 13:32:07 +100046find_encoder(struct drm_connector *connector, int type)
Ben Skeggs6ee73862009-12-11 19:24:15 +100047{
48 struct drm_device *dev = connector->dev;
49 struct nouveau_encoder *nv_encoder;
50 struct drm_mode_object *obj;
51 int i, id;
52
53 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
54 id = connector->encoder_ids[i];
55 if (!id)
56 break;
57
58 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
59 if (!obj)
60 continue;
61 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
62
Ben Skeggscb75d972012-07-11 10:44:20 +100063 if (type == DCB_OUTPUT_ANY || nv_encoder->dcb->type == type)
Ben Skeggs6ee73862009-12-11 19:24:15 +100064 return nv_encoder;
65 }
66
67 return NULL;
68}
69
70struct nouveau_connector *
71nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
72{
73 struct drm_device *dev = to_drm_encoder(encoder)->dev;
74 struct drm_connector *drm_connector;
75
76 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
77 if (drm_connector->encoder == to_drm_encoder(encoder))
78 return nouveau_connector(drm_connector);
79 }
80
81 return NULL;
82}
83
Ben Skeggs6ee73862009-12-11 19:24:15 +100084static void
Ben Skeggsfce2bad2010-11-11 16:14:56 +100085nouveau_connector_destroy(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +100086{
Ben Skeggsfce2bad2010-11-11 16:14:56 +100087 struct nouveau_connector *nv_connector = nouveau_connector(connector);
88 struct drm_nouveau_private *dev_priv;
Marcin Slusarzdd19e442010-01-30 15:41:00 +010089 struct drm_device *dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +100090
Xavier Chantryc8ebe272010-01-11 22:42:21 +010091 if (!nv_connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +100092 return;
93
Marcin Slusarzdd19e442010-01-30 15:41:00 +010094 dev = nv_connector->base.dev;
Ben Skeggsfce2bad2010-11-11 16:14:56 +100095 dev_priv = dev->dev_private;
Marcin Slusarzdd19e442010-01-30 15:41:00 +010096 NV_DEBUG_KMS(dev, "\n");
97
Ben Skeggsa0b25632011-11-21 16:41:48 +100098 if (nv_connector->hpd != DCB_GPIO_UNUSED) {
99 nouveau_gpio_isr_del(dev, 0, nv_connector->hpd, 0xff,
100 nouveau_connector_hotplug, connector);
Ben Skeggsfce2bad2010-11-11 16:14:56 +1000101 }
102
Xavier Chantryc8ebe272010-01-11 22:42:21 +0100103 kfree(nv_connector->edid);
Ben Skeggsfce2bad2010-11-11 16:14:56 +1000104 drm_sysfs_connector_remove(connector);
105 drm_connector_cleanup(connector);
106 kfree(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000107}
108
Ben Skeggs4196faa2012-07-10 14:36:38 +1000109static struct nouveau_i2c_port *
Ben Skeggs6ee73862009-12-11 19:24:15 +1000110nouveau_connector_ddc_detect(struct drm_connector *connector,
111 struct nouveau_encoder **pnv_encoder)
112{
113 struct drm_device *dev = connector->dev;
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200114 int i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000115
Ben Skeggs6ee73862009-12-11 19:24:15 +1000116 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
Ben Skeggs4196faa2012-07-10 14:36:38 +1000117 struct nouveau_i2c_port *i2c = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000118 struct nouveau_encoder *nv_encoder;
119 struct drm_mode_object *obj;
120 int id;
121
122 id = connector->encoder_ids[i];
123 if (!id)
124 break;
125
126 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
127 if (!obj)
128 continue;
129 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
Francisco Jerez4ca2b712010-08-08 21:35:57 +0200130
131 if (nv_encoder->dcb->i2c_index < 0xf)
132 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000133
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200134 if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000135 *pnv_encoder = nv_encoder;
136 return i2c;
137 }
138 }
139
140 return NULL;
141}
142
Francisco Jerezc16c5702010-09-09 14:33:17 +0200143static struct nouveau_encoder *
144nouveau_connector_of_detect(struct drm_connector *connector)
145{
146#ifdef __powerpc__
147 struct drm_device *dev = connector->dev;
148 struct nouveau_connector *nv_connector = nouveau_connector(connector);
149 struct nouveau_encoder *nv_encoder;
150 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
151
152 if (!dn ||
Ben Skeggscb75d972012-07-11 10:44:20 +1000153 !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
154 (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
Francisco Jerezc16c5702010-09-09 14:33:17 +0200155 return NULL;
156
157 for_each_child_of_node(dn, cn) {
158 const char *name = of_get_property(cn, "name", NULL);
159 const void *edid = of_get_property(cn, "EDID", NULL);
160 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
161
162 if (nv_encoder->dcb->i2c_index == idx && edid) {
163 nv_connector->edid =
164 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
165 of_node_put(cn);
166 return nv_encoder;
167 }
168 }
169#endif
170 return NULL;
171}
172
Ben Skeggs6ee73862009-12-11 19:24:15 +1000173static void
174nouveau_connector_set_encoder(struct drm_connector *connector,
175 struct nouveau_encoder *nv_encoder)
176{
177 struct nouveau_connector *nv_connector = nouveau_connector(connector);
178 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
179 struct drm_device *dev = connector->dev;
180
181 if (nv_connector->detected_encoder == nv_encoder)
182 return;
183 nv_connector->detected_encoder = nv_encoder;
184
Ben Skeggsc8334422011-10-19 13:06:48 +1000185 if (dev_priv->card_type >= NV_50) {
186 connector->interlace_allowed = true;
187 connector->doublescan_allowed = true;
188 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000189 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
190 nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000191 connector->doublescan_allowed = false;
192 connector->interlace_allowed = false;
193 } else {
194 connector->doublescan_allowed = true;
195 if (dev_priv->card_type == NV_20 ||
196 (dev_priv->card_type == NV_10 &&
197 (dev->pci_device & 0x0ff0) != 0x0100 &&
198 (dev->pci_device & 0x0ff0) != 0x0150))
199 /* HW is broken */
200 connector->interlace_allowed = false;
201 else
202 connector->interlace_allowed = true;
203 }
204
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000205 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000206 drm_connector_property_set_value(connector,
207 dev->mode_config.dvi_i_subconnector_property,
Ben Skeggscb75d972012-07-11 10:44:20 +1000208 nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209 DRM_MODE_SUBCONNECTOR_DVID :
210 DRM_MODE_SUBCONNECTOR_DVIA);
211 }
212}
213
214static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100215nouveau_connector_detect(struct drm_connector *connector, bool force)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000216{
217 struct drm_device *dev = connector->dev;
218 struct nouveau_connector *nv_connector = nouveau_connector(connector);
219 struct nouveau_encoder *nv_encoder = NULL;
Ben Skeggse19b20b2011-07-12 13:32:07 +1000220 struct nouveau_encoder *nv_partner;
Ben Skeggs4196faa2012-07-10 14:36:38 +1000221 struct nouveau_i2c_port *i2c;
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200222 int type;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223
Francisco Jerezb8780e22010-01-14 15:54:23 +0100224 /* Cleanup the previous EDID block. */
225 if (nv_connector->edid) {
226 drm_mode_connector_update_edid_property(connector, NULL);
227 kfree(nv_connector->edid);
228 nv_connector->edid = NULL;
229 }
Xavier Chantryc8ebe272010-01-11 22:42:21 +0100230
Ben Skeggs6ee73862009-12-11 19:24:15 +1000231 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
232 if (i2c) {
Ben Skeggs4196faa2012-07-10 14:36:38 +1000233 nv_connector->edid = drm_get_edid(connector, nouveau_i2c_adapter(i2c));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000234 drm_mode_connector_update_edid_property(connector,
235 nv_connector->edid);
236 if (!nv_connector->edid) {
237 NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
238 drm_get_connector_name(connector));
Francisco Jerez0ed31652010-01-14 15:47:03 +0100239 goto detect_analog;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000240 }
241
Ben Skeggscb75d972012-07-11 10:44:20 +1000242 if (nv_encoder->dcb->type == DCB_OUTPUT_DP &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000243 !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
244 NV_ERROR(dev, "Detected %s, but failed init\n",
245 drm_get_connector_name(connector));
246 return connector_status_disconnected;
247 }
248
249 /* Override encoder type for DVI-I based on whether EDID
250 * says the display is digital or analog, both use the
251 * same i2c channel so the value returned from ddc_detect
252 * isn't necessarily correct.
253 */
Ben Skeggse19b20b2011-07-12 13:32:07 +1000254 nv_partner = NULL;
Ben Skeggscb75d972012-07-11 10:44:20 +1000255 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
256 nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
257 if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
258 nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
Ben Skeggse19b20b2011-07-12 13:32:07 +1000259
Ben Skeggscb75d972012-07-11 10:44:20 +1000260 if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
261 nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
262 (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
263 nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000264 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
Ben Skeggscb75d972012-07-11 10:44:20 +1000265 type = DCB_OUTPUT_TMDS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000266 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000267 type = DCB_OUTPUT_ANALOG;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000268
Ben Skeggse19b20b2011-07-12 13:32:07 +1000269 nv_encoder = find_encoder(connector, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000270 }
271
272 nouveau_connector_set_encoder(connector, nv_encoder);
273 return connector_status_connected;
274 }
275
Francisco Jerezc16c5702010-09-09 14:33:17 +0200276 nv_encoder = nouveau_connector_of_detect(connector);
277 if (nv_encoder) {
278 nouveau_connector_set_encoder(connector, nv_encoder);
279 return connector_status_connected;
280 }
281
Francisco Jerez0ed31652010-01-14 15:47:03 +0100282detect_analog:
Ben Skeggscb75d972012-07-11 10:44:20 +1000283 nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
Ben Skeggsf4053502010-03-15 09:43:51 +1000284 if (!nv_encoder && !nouveau_tv_disable)
Ben Skeggscb75d972012-07-11 10:44:20 +1000285 nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
Francisco Jerez84b80812010-10-26 02:17:56 +0200286 if (nv_encoder && force) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000287 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
288 struct drm_encoder_helper_funcs *helper =
289 encoder->helper_private;
290
291 if (helper->detect(encoder, connector) ==
292 connector_status_connected) {
293 nouveau_connector_set_encoder(connector, nv_encoder);
294 return connector_status_connected;
295 }
296
297 }
298
299 return connector_status_disconnected;
300}
301
Ben Skeggsd17f3952010-06-01 13:32:42 +1000302static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100303nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
Ben Skeggsd17f3952010-06-01 13:32:42 +1000304{
305 struct drm_device *dev = connector->dev;
306 struct drm_nouveau_private *dev_priv = dev->dev_private;
307 struct nouveau_connector *nv_connector = nouveau_connector(connector);
308 struct nouveau_encoder *nv_encoder = NULL;
309 enum drm_connector_status status = connector_status_disconnected;
310
311 /* Cleanup the previous EDID block. */
312 if (nv_connector->edid) {
313 drm_mode_connector_update_edid_property(connector, NULL);
314 kfree(nv_connector->edid);
315 nv_connector->edid = NULL;
316 }
317
Ben Skeggscb75d972012-07-11 10:44:20 +1000318 nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000319 if (!nv_encoder)
320 return connector_status_disconnected;
321
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000322 /* Try retrieving EDID via DDC */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000323 if (!dev_priv->vbios.fp_no_ddc) {
Chris Wilson930a9e22010-09-14 11:07:23 +0100324 status = nouveau_connector_detect(connector, force);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000325 if (status == connector_status_connected)
326 goto out;
327 }
328
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000329 /* On some laptops (Sony, i'm looking at you) there appears to
330 * be no direct way of accessing the panel's EDID. The only
331 * option available to us appears to be to ask ACPI for help..
332 *
333 * It's important this check's before trying straps, one of the
334 * said manufacturer's laptops are configured in such a way
335 * the nouveau decides an entry in the VBIOS FP mode table is
336 * valid - it's not (rh#613284)
337 */
338 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
Ben Skeggsc0077062012-07-26 08:51:21 +1000339 if (!(nv_connector->edid = nouveau_acpi_edid(dev, connector))) {
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000340 status = connector_status_connected;
341 goto out;
342 }
343 }
344
Ben Skeggsd17f3952010-06-01 13:32:42 +1000345 /* If no EDID found above, and the VBIOS indicates a hardcoded
346 * modeline is avalilable for the panel, set it as the panel's
347 * native mode and exit.
348 */
349 if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
350 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
351 status = connector_status_connected;
352 goto out;
353 }
354
355 /* Still nothing, some VBIOS images have a hardcoded EDID block
356 * stored for the panel stored in them.
357 */
358 if (!dev_priv->vbios.fp_no_ddc) {
359 struct edid *edid =
360 (struct edid *)nouveau_bios_embedded_edid(dev);
361 if (edid) {
362 nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
363 *(nv_connector->edid) = *edid;
364 status = connector_status_connected;
365 }
366 }
367
368out:
369#if defined(CONFIG_ACPI_BUTTON) || \
370 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
371 if (status == connector_status_connected &&
372 !nouveau_ignorelid && !acpi_lid_open())
373 status = connector_status_unknown;
374#endif
375
376 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
Albert Damen3195c5f2010-06-20 16:57:57 +0200377 nouveau_connector_set_encoder(connector, nv_encoder);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000378 return status;
379}
380
Ben Skeggs6ee73862009-12-11 19:24:15 +1000381static void
382nouveau_connector_force(struct drm_connector *connector)
383{
Ben Skeggsbe079e92010-02-24 14:01:40 +1000384 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000385 struct nouveau_encoder *nv_encoder;
386 int type;
387
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000388 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000389 if (connector->force == DRM_FORCE_ON_DIGITAL)
Ben Skeggscb75d972012-07-11 10:44:20 +1000390 type = DCB_OUTPUT_TMDS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000391 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000392 type = DCB_OUTPUT_ANALOG;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000393 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000394 type = DCB_OUTPUT_ANY;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000395
Ben Skeggse19b20b2011-07-12 13:32:07 +1000396 nv_encoder = find_encoder(connector, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000397 if (!nv_encoder) {
Ben Skeggsbe079e92010-02-24 14:01:40 +1000398 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000399 drm_get_connector_name(connector));
400 connector->status = connector_status_disconnected;
401 return;
402 }
403
404 nouveau_connector_set_encoder(connector, nv_encoder);
405}
406
407static int
408nouveau_connector_set_property(struct drm_connector *connector,
409 struct drm_property *property, uint64_t value)
410{
Ben Skeggsb29caa52011-10-06 13:29:05 +1000411 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
412 struct nouveau_display_engine *disp = &dev_priv->engine.display;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000413 struct nouveau_connector *nv_connector = nouveau_connector(connector);
414 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200415 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000416 struct drm_device *dev = connector->dev;
Ben Skeggsb29caa52011-10-06 13:29:05 +1000417 struct nouveau_crtc *nv_crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000418 int ret;
419
Ben Skeggsb29caa52011-10-06 13:29:05 +1000420 nv_crtc = NULL;
421 if (connector->encoder && connector->encoder->crtc)
422 nv_crtc = nouveau_crtc(connector->encoder->crtc);
423
Ben Skeggs6ee73862009-12-11 19:24:15 +1000424 /* Scaling mode */
425 if (property == dev->mode_config.scaling_mode_property) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000426 bool modeset = false;
427
428 switch (value) {
429 case DRM_MODE_SCALE_NONE:
430 case DRM_MODE_SCALE_FULLSCREEN:
431 case DRM_MODE_SCALE_CENTER:
432 case DRM_MODE_SCALE_ASPECT:
433 break;
434 default:
435 return -EINVAL;
436 }
437
438 /* LVDS always needs gpu scaling */
Ben Skeggs8c3f6bb2011-04-18 09:57:48 +1000439 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000440 value == DRM_MODE_SCALE_NONE)
441 return -EINVAL;
442
443 /* Changing between GPU and panel scaling requires a full
444 * modeset
445 */
446 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
447 (value == DRM_MODE_SCALE_NONE))
448 modeset = true;
449 nv_connector->scaling_mode = value;
450
Ben Skeggs6ee73862009-12-11 19:24:15 +1000451 if (!nv_crtc)
452 return 0;
453
454 if (modeset || !nv_crtc->set_scale) {
455 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
456 &nv_crtc->base.mode,
457 nv_crtc->base.x,
458 nv_crtc->base.y, NULL);
459 if (!ret)
460 return -EINVAL;
461 } else {
Ben Skeggs488ff202011-10-17 10:38:10 +1000462 ret = nv_crtc->set_scale(nv_crtc, true);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000463 if (ret)
464 return ret;
465 }
466
467 return 0;
468 }
469
Ben Skeggsb29caa52011-10-06 13:29:05 +1000470 /* Underscan */
471 if (property == disp->underscan_property) {
472 if (nv_connector->underscan != value) {
473 nv_connector->underscan = value;
474 if (!nv_crtc || !nv_crtc->set_scale)
475 return 0;
476
Ben Skeggs488ff202011-10-17 10:38:10 +1000477 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000478 }
479
480 return 0;
481 }
482
483 if (property == disp->underscan_hborder_property) {
484 if (nv_connector->underscan_hborder != value) {
485 nv_connector->underscan_hborder = value;
486 if (!nv_crtc || !nv_crtc->set_scale)
487 return 0;
488
Ben Skeggs488ff202011-10-17 10:38:10 +1000489 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000490 }
491
492 return 0;
493 }
494
495 if (property == disp->underscan_vborder_property) {
496 if (nv_connector->underscan_vborder != value) {
497 nv_connector->underscan_vborder = value;
498 if (!nv_crtc || !nv_crtc->set_scale)
499 return 0;
500
Ben Skeggs488ff202011-10-17 10:38:10 +1000501 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000502 }
503
504 return 0;
505 }
506
Ben Skeggs6ee73862009-12-11 19:24:15 +1000507 /* Dithering */
Ben Skeggsde691852011-10-17 12:23:41 +1000508 if (property == disp->dithering_mode) {
509 nv_connector->dithering_mode = value;
510 if (!nv_crtc || !nv_crtc->set_dither)
511 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000512
Ben Skeggsde691852011-10-17 12:23:41 +1000513 return nv_crtc->set_dither(nv_crtc, true);
514 }
515
516 if (property == disp->dithering_depth) {
517 nv_connector->dithering_depth = value;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000518 if (!nv_crtc || !nv_crtc->set_dither)
519 return 0;
520
Ben Skeggs488ff202011-10-17 10:38:10 +1000521 return nv_crtc->set_dither(nv_crtc, true);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000522 }
523
Christoph Bumillerdf26bc92012-01-21 23:13:26 +0100524 if (nv_crtc && nv_crtc->set_color_vibrance) {
525 /* Hue */
526 if (property == disp->vibrant_hue_property) {
527 nv_crtc->vibrant_hue = value - 90;
528 return nv_crtc->set_color_vibrance(nv_crtc, true);
529 }
530 /* Saturation */
531 if (property == disp->color_vibrance_property) {
532 nv_crtc->color_vibrance = value - 100;
533 return nv_crtc->set_color_vibrance(nv_crtc, true);
534 }
535 }
536
Ben Skeggscb75d972012-07-11 10:44:20 +1000537 if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200538 return get_slave_funcs(encoder)->set_property(
539 encoder, connector, property, value);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000540
541 return -EINVAL;
542}
543
544static struct drm_display_mode *
Ben Skeggs26099a72010-05-28 10:13:16 +1000545nouveau_connector_native_mode(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000546{
Ben Skeggs26099a72010-05-28 10:13:16 +1000547 struct drm_connector_helper_funcs *helper = connector->helper_private;
548 struct nouveau_connector *nv_connector = nouveau_connector(connector);
549 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000550 struct drm_display_mode *mode, *largest = NULL;
551 int high_w = 0, high_h = 0, high_v = 0;
552
Ben Skeggs26099a72010-05-28 10:13:16 +1000553 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
Ben Skeggs0d9b6192011-02-07 08:41:18 +1000554 mode->vrefresh = drm_mode_vrefresh(mode);
Francisco Jereza5afb772010-07-18 16:19:16 +0200555 if (helper->mode_valid(connector, mode) != MODE_OK ||
556 (mode->flags & DRM_MODE_FLAG_INTERLACE))
Ben Skeggs26099a72010-05-28 10:13:16 +1000557 continue;
558
559 /* Use preferred mode if there is one.. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100561 NV_DEBUG_KMS(dev, "native mode from preferred\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000562 return drm_mode_duplicate(dev, mode);
563 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000564
Ben Skeggs26099a72010-05-28 10:13:16 +1000565 /* Otherwise, take the resolution with the largest width, then
566 * height, then vertical refresh
567 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000568 if (mode->hdisplay < high_w)
569 continue;
570
571 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
572 continue;
573
574 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
575 mode->vrefresh < high_v)
576 continue;
577
578 high_w = mode->hdisplay;
579 high_h = mode->vdisplay;
580 high_v = mode->vrefresh;
581 largest = mode;
582 }
583
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100584 NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000585 high_w, high_h, high_v);
586 return largest ? drm_mode_duplicate(dev, largest) : NULL;
587}
588
589struct moderec {
590 int hdisplay;
591 int vdisplay;
592};
593
594static struct moderec scaler_modes[] = {
595 { 1920, 1200 },
596 { 1920, 1080 },
597 { 1680, 1050 },
598 { 1600, 1200 },
599 { 1400, 1050 },
600 { 1280, 1024 },
601 { 1280, 960 },
602 { 1152, 864 },
603 { 1024, 768 },
604 { 800, 600 },
605 { 720, 400 },
606 { 640, 480 },
607 { 640, 400 },
608 { 640, 350 },
609 {}
610};
611
612static int
613nouveau_connector_scaler_modes_add(struct drm_connector *connector)
614{
615 struct nouveau_connector *nv_connector = nouveau_connector(connector);
616 struct drm_display_mode *native = nv_connector->native_mode, *m;
617 struct drm_device *dev = connector->dev;
618 struct moderec *mode = &scaler_modes[0];
619 int modes = 0;
620
621 if (!native)
622 return 0;
623
624 while (mode->hdisplay) {
625 if (mode->hdisplay <= native->hdisplay &&
626 mode->vdisplay <= native->vdisplay) {
627 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
628 drm_mode_vrefresh(native), false,
629 false, false);
630 if (!m)
631 continue;
632
633 m->type |= DRM_MODE_TYPE_DRIVER;
634
635 drm_mode_probed_add(connector, m);
636 modes++;
637 }
638
639 mode++;
640 }
641
642 return modes;
643}
644
Ben Skeggs63221752011-10-17 10:24:49 +1000645static void
646nouveau_connector_detect_depth(struct drm_connector *connector)
647{
648 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
649 struct nouveau_connector *nv_connector = nouveau_connector(connector);
650 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
651 struct nvbios *bios = &dev_priv->vbios;
652 struct drm_display_mode *mode = nv_connector->native_mode;
653 bool duallink;
654
655 /* if the edid is feeling nice enough to provide this info, use it */
656 if (nv_connector->edid && connector->display_info.bpc)
657 return;
658
Ben Skeggsa6a17852012-05-05 00:39:21 +1000659 /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
660 if (nv_connector->type == DCB_CONNECTOR_eDP) {
661 connector->display_info.bpc = 6;
662 return;
663 }
664
665 /* we're out of options unless we're LVDS, default to 8bpc */
Ben Skeggscb75d972012-07-11 10:44:20 +1000666 if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
Ben Skeggsc8435362012-03-24 16:26:13 +1000667 connector->display_info.bpc = 8;
Ben Skeggs63221752011-10-17 10:24:49 +1000668 return;
Ben Skeggsc8435362012-03-24 16:26:13 +1000669 }
670
671 connector->display_info.bpc = 6;
Ben Skeggs63221752011-10-17 10:24:49 +1000672
673 /* LVDS: panel straps */
674 if (bios->fp_no_ddc) {
675 if (bios->fp.if_is_24bit)
676 connector->display_info.bpc = 8;
677 return;
678 }
679
680 /* LVDS: DDC panel, need to first determine the number of links to
681 * know which if_is_24bit flag to check...
682 */
683 if (nv_connector->edid &&
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000684 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
Ben Skeggs63221752011-10-17 10:24:49 +1000685 duallink = ((u8 *)nv_connector->edid)[121] == 2;
686 else
687 duallink = mode->clock >= bios->fp.duallink_transition_clk;
688
689 if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
690 ( duallink && (bios->fp.strapless_is_24bit & 2)))
691 connector->display_info.bpc = 8;
692}
693
Ben Skeggs6ee73862009-12-11 19:24:15 +1000694static int
695nouveau_connector_get_modes(struct drm_connector *connector)
696{
697 struct drm_device *dev = connector->dev;
Ben Skeggsd17f3952010-06-01 13:32:42 +1000698 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000699 struct nouveau_connector *nv_connector = nouveau_connector(connector);
700 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200701 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000702 int ret = 0;
703
Ben Skeggsd17f3952010-06-01 13:32:42 +1000704 /* destroy the native mode, the attached monitor could have changed.
Ben Skeggs6ee73862009-12-11 19:24:15 +1000705 */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000706 if (nv_connector->native_mode) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000707 drm_mode_destroy(dev, nv_connector->native_mode);
708 nv_connector->native_mode = NULL;
709 }
710
711 if (nv_connector->edid)
712 ret = drm_add_edid_modes(connector, nv_connector->edid);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000713 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000714 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
Ben Skeggsd17f3952010-06-01 13:32:42 +1000715 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
Francisco Jerez6e86e042010-07-03 18:36:39 +0200716 dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
Ben Skeggs80dad862010-09-23 16:37:39 +1000717 struct drm_display_mode mode;
718
719 nouveau_bios_fp_mode(dev, &mode);
720 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000721 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000722
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000723 /* Determine display colour depth for everything except LVDS now,
724 * DP requires this before mode_valid() is called.
725 */
726 if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
727 nouveau_connector_detect_depth(connector);
728
Ben Skeggs6ee73862009-12-11 19:24:15 +1000729 /* Find the native mode if this is a digital panel, if we didn't
730 * find any modes through DDC previously add the native mode to
731 * the list of modes.
732 */
733 if (!nv_connector->native_mode)
734 nv_connector->native_mode =
Ben Skeggs26099a72010-05-28 10:13:16 +1000735 nouveau_connector_native_mode(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000736 if (ret == 0 && nv_connector->native_mode) {
737 struct drm_display_mode *mode;
738
739 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
740 drm_mode_probed_add(connector, mode);
741 ret = 1;
742 }
743
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000744 /* Determine LVDS colour depth, must happen after determining
745 * "native" mode as some VBIOS tables require us to use the
746 * pixel clock as part of the lookup...
Ben Skeggs63221752011-10-17 10:24:49 +1000747 */
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000748 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
749 nouveau_connector_detect_depth(connector);
Ben Skeggs63221752011-10-17 10:24:49 +1000750
Ben Skeggscb75d972012-07-11 10:44:20 +1000751 if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200752 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000753
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000754 if (nv_connector->type == DCB_CONNECTOR_LVDS ||
755 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
756 nv_connector->type == DCB_CONNECTOR_eDP)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000757 ret += nouveau_connector_scaler_modes_add(connector);
758
759 return ret;
760}
761
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200762static unsigned
763get_tmds_link_bandwidth(struct drm_connector *connector)
764{
765 struct nouveau_connector *nv_connector = nouveau_connector(connector);
766 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
Ben Skeggscb75d972012-07-11 10:44:20 +1000767 struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200768
769 if (dcb->location != DCB_LOC_ON_CHIP ||
770 dev_priv->chipset >= 0x46)
771 return 165000;
772 else if (dev_priv->chipset >= 0x40)
773 return 155000;
774 else if (dev_priv->chipset >= 0x18)
775 return 135000;
776 else
777 return 112000;
778}
779
Ben Skeggs6ee73862009-12-11 19:24:15 +1000780static int
781nouveau_connector_mode_valid(struct drm_connector *connector,
782 struct drm_display_mode *mode)
783{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000784 struct nouveau_connector *nv_connector = nouveau_connector(connector);
785 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200786 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000787 unsigned min_clock = 25000, max_clock = min_clock;
788 unsigned clock = mode->clock;
789
790 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000791 case DCB_OUTPUT_LVDS:
Ben Skeggs26099a72010-05-28 10:13:16 +1000792 if (nv_connector->native_mode &&
793 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
794 mode->vdisplay > nv_connector->native_mode->vdisplay))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000795 return MODE_PANEL;
796
797 min_clock = 0;
798 max_clock = 400000;
799 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000800 case DCB_OUTPUT_TMDS:
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200801 max_clock = get_tmds_link_bandwidth(connector);
802 if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
803 max_clock *= 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000804 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000805 case DCB_OUTPUT_ANALOG:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000806 max_clock = nv_encoder->dcb->crtconf.maxfreq;
807 if (!max_clock)
808 max_clock = 350000;
809 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000810 case DCB_OUTPUT_TV:
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200811 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
Ben Skeggscb75d972012-07-11 10:44:20 +1000812 case DCB_OUTPUT_DP:
Ben Skeggs75a1fcc2011-08-04 09:55:44 +1000813 max_clock = nv_encoder->dp.link_nr;
814 max_clock *= nv_encoder->dp.link_bw;
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000815 clock = clock * (connector->display_info.bpc * 3) / 10;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000816 break;
Ben Skeggse7cc51c2010-02-24 10:31:39 +1000817 default:
818 BUG_ON(1);
819 return MODE_BAD;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000820 }
821
822 if (clock < min_clock)
823 return MODE_CLOCK_LOW;
824
825 if (clock > max_clock)
826 return MODE_CLOCK_HIGH;
827
828 return MODE_OK;
829}
830
831static struct drm_encoder *
832nouveau_connector_best_encoder(struct drm_connector *connector)
833{
834 struct nouveau_connector *nv_connector = nouveau_connector(connector);
835
836 if (nv_connector->detected_encoder)
837 return to_drm_encoder(nv_connector->detected_encoder);
838
839 return NULL;
840}
841
842static const struct drm_connector_helper_funcs
843nouveau_connector_helper_funcs = {
844 .get_modes = nouveau_connector_get_modes,
845 .mode_valid = nouveau_connector_mode_valid,
846 .best_encoder = nouveau_connector_best_encoder,
847};
848
849static const struct drm_connector_funcs
850nouveau_connector_funcs = {
851 .dpms = drm_helper_connector_dpms,
852 .save = NULL,
853 .restore = NULL,
854 .detect = nouveau_connector_detect,
855 .destroy = nouveau_connector_destroy,
856 .fill_modes = drm_helper_probe_single_connector_modes,
857 .set_property = nouveau_connector_set_property,
858 .force = nouveau_connector_force
859};
860
Ben Skeggsd17f3952010-06-01 13:32:42 +1000861static const struct drm_connector_funcs
862nouveau_connector_funcs_lvds = {
863 .dpms = drm_helper_connector_dpms,
864 .save = NULL,
865 .restore = NULL,
866 .detect = nouveau_connector_detect_lvds,
867 .destroy = nouveau_connector_destroy,
868 .fill_modes = drm_helper_probe_single_connector_modes,
869 .set_property = nouveau_connector_set_property,
870 .force = nouveau_connector_force
871};
Ben Skeggs6ee73862009-12-11 19:24:15 +1000872
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000873static int
874drm_conntype_from_dcb(enum dcb_connector_type dcb)
875{
876 switch (dcb) {
877 case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
878 case DCB_CONNECTOR_TV_0 :
879 case DCB_CONNECTOR_TV_1 :
880 case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
Ben Skeggsfa2c1132012-03-22 11:27:15 +1000881 case DCB_CONNECTOR_DMS59_0 :
882 case DCB_CONNECTOR_DMS59_1 :
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000883 case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
884 case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
885 case DCB_CONNECTOR_LVDS :
886 case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
Ben Skeggs4abb4102012-01-12 16:17:16 +1000887 case DCB_CONNECTOR_DMS59_DP0:
888 case DCB_CONNECTOR_DMS59_DP1:
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000889 case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort;
890 case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
891 case DCB_CONNECTOR_HDMI_0 :
892 case DCB_CONNECTOR_HDMI_1 : return DRM_MODE_CONNECTOR_HDMIA;
893 default:
894 break;
895 }
896
897 return DRM_MODE_CONNECTOR_Unknown;
898}
899
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000900struct drm_connector *
901nouveau_connector_create(struct drm_device *dev, int index)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000902{
Ben Skeggsd17f3952010-06-01 13:32:42 +1000903 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000904 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsb29caa52011-10-06 13:29:05 +1000905 struct nouveau_display_engine *disp = &dev_priv->engine.display;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000906 struct nouveau_connector *nv_connector = NULL;
907 struct drm_connector *connector;
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200908 int type, ret = 0;
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000909 bool dummy;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000910
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100911 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000912
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000913 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
914 nv_connector = nouveau_connector(connector);
915 if (nv_connector->index == index)
916 return connector;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000917 }
918
Ben Skeggs6ee73862009-12-11 19:24:15 +1000919 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
920 if (!nv_connector)
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000921 return ERR_PTR(-ENOMEM);
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000922
Ben Skeggs6ee73862009-12-11 19:24:15 +1000923 connector = &nv_connector->base;
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000924 nv_connector->index = index;
925
926 /* attempt to parse vbios connector type and hotplug gpio */
Ben Skeggscb75d972012-07-11 10:44:20 +1000927 nv_connector->dcb = olddcb_conn(dev, index);
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000928 if (nv_connector->dcb) {
929 static const u8 hpd[16] = {
930 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
931 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
932 };
933
934 u32 entry = ROM16(nv_connector->dcb[0]);
Ben Skeggscb75d972012-07-11 10:44:20 +1000935 if (olddcb_conntab(dev)[3] >= 4)
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000936 entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
937
938 nv_connector->hpd = ffs((entry & 0x07033000) >> 12);
939 nv_connector->hpd = hpd[nv_connector->hpd];
940
941 nv_connector->type = nv_connector->dcb[0];
942 if (drm_conntype_from_dcb(nv_connector->type) ==
943 DRM_MODE_CONNECTOR_Unknown) {
944 NV_WARN(dev, "unknown connector type %02x\n",
945 nv_connector->type);
946 nv_connector->type = DCB_CONNECTOR_NONE;
947 }
948
949 /* Gigabyte NX85T */
950 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
951 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
952 nv_connector->type = DCB_CONNECTOR_DVI_I;
953 }
954
955 /* Gigabyte GV-NX86T512H */
956 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
957 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
958 nv_connector->type = DCB_CONNECTOR_DVI_I;
959 }
960 } else {
961 nv_connector->type = DCB_CONNECTOR_NONE;
962 nv_connector->hpd = DCB_GPIO_UNUSED;
963 }
964
965 /* no vbios data, or an unknown dcb connector type - attempt to
966 * figure out something suitable ourselves
967 */
968 if (nv_connector->type == DCB_CONNECTOR_NONE) {
969 struct drm_nouveau_private *dev_priv = dev->dev_private;
970 struct dcb_table *dcbt = &dev_priv->vbios.dcb;
971 u32 encoders = 0;
972 int i;
973
974 for (i = 0; i < dcbt->entries; i++) {
975 if (dcbt->entry[i].connector == nv_connector->index)
976 encoders |= (1 << dcbt->entry[i].type);
977 }
978
Ben Skeggscb75d972012-07-11 10:44:20 +1000979 if (encoders & (1 << DCB_OUTPUT_DP)) {
980 if (encoders & (1 << DCB_OUTPUT_TMDS))
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000981 nv_connector->type = DCB_CONNECTOR_DP;
982 else
983 nv_connector->type = DCB_CONNECTOR_eDP;
984 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000985 if (encoders & (1 << DCB_OUTPUT_TMDS)) {
986 if (encoders & (1 << DCB_OUTPUT_ANALOG))
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000987 nv_connector->type = DCB_CONNECTOR_DVI_I;
988 else
989 nv_connector->type = DCB_CONNECTOR_DVI_D;
990 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000991 if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000992 nv_connector->type = DCB_CONNECTOR_VGA;
993 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000994 if (encoders & (1 << DCB_OUTPUT_LVDS)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000995 nv_connector->type = DCB_CONNECTOR_LVDS;
996 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000997 if (encoders & (1 << DCB_OUTPUT_TV)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000998 nv_connector->type = DCB_CONNECTOR_TV_0;
999 }
1000 }
1001
1002 type = drm_conntype_from_dcb(nv_connector->type);
1003 if (type == DRM_MODE_CONNECTOR_LVDS) {
1004 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
1005 if (ret) {
1006 NV_ERROR(dev, "Error parsing LVDS table, disabling\n");
1007 kfree(nv_connector);
1008 return ERR_PTR(ret);
1009 }
1010
1011 funcs = &nouveau_connector_funcs_lvds;
1012 } else {
1013 funcs = &nouveau_connector_funcs;
1014 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001015
Ben Skeggs6ee73862009-12-11 19:24:15 +10001016 /* defaults, will get overridden in detect() */
1017 connector->interlace_allowed = false;
1018 connector->doublescan_allowed = false;
1019
Ben Skeggsd17f3952010-06-01 13:32:42 +10001020 drm_connector_init(dev, connector, funcs, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001021 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1022
1023 /* Init DVI-I specific properties */
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001024 if (nv_connector->type == DCB_CONNECTOR_DVI_I)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001025 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001026
Ben Skeggsb29caa52011-10-06 13:29:05 +10001027 /* Add overscan compensation options to digital outputs */
Ben Skeggsde691852011-10-17 12:23:41 +10001028 if (disp->underscan_property &&
Ben Skeggsfa2c1132012-03-22 11:27:15 +10001029 (type == DRM_MODE_CONNECTOR_DVID ||
1030 type == DRM_MODE_CONNECTOR_DVII ||
1031 type == DRM_MODE_CONNECTOR_HDMIA ||
1032 type == DRM_MODE_CONNECTOR_DisplayPort)) {
Ben Skeggsb29caa52011-10-06 13:29:05 +10001033 drm_connector_attach_property(connector,
1034 disp->underscan_property,
1035 UNDERSCAN_OFF);
1036 drm_connector_attach_property(connector,
1037 disp->underscan_hborder_property,
1038 0);
1039 drm_connector_attach_property(connector,
1040 disp->underscan_vborder_property,
1041 0);
1042 }
1043
Christoph Bumillerdf26bc92012-01-21 23:13:26 +01001044 /* Add hue and saturation options */
1045 if (disp->vibrant_hue_property)
1046 drm_connector_attach_property(connector,
1047 disp->vibrant_hue_property,
1048 90);
1049 if (disp->color_vibrance_property)
1050 drm_connector_attach_property(connector,
1051 disp->color_vibrance_property,
1052 150);
1053
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001054 switch (nv_connector->type) {
Ben Skeggsbe079e92010-02-24 14:01:40 +10001055 case DCB_CONNECTOR_VGA:
1056 if (dev_priv->card_type >= NV_50) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001057 drm_connector_attach_property(connector,
1058 dev->mode_config.scaling_mode_property,
1059 nv_connector->scaling_mode);
1060 }
Ben Skeggsbe079e92010-02-24 14:01:40 +10001061 /* fall-through */
1062 case DCB_CONNECTOR_TV_0:
1063 case DCB_CONNECTOR_TV_1:
1064 case DCB_CONNECTOR_TV_3:
1065 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1066 break;
1067 default:
1068 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1069
1070 drm_connector_attach_property(connector,
1071 dev->mode_config.scaling_mode_property,
1072 nv_connector->scaling_mode);
Ben Skeggsde691852011-10-17 12:23:41 +10001073 if (disp->dithering_mode) {
1074 nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1075 drm_connector_attach_property(connector,
1076 disp->dithering_mode,
1077 nv_connector->dithering_mode);
1078 }
1079 if (disp->dithering_depth) {
1080 nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
1081 drm_connector_attach_property(connector,
1082 disp->dithering_depth,
1083 nv_connector->dithering_depth);
1084 }
Ben Skeggsbe079e92010-02-24 14:01:40 +10001085 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001086 }
1087
Ben Skeggsa0b25632011-11-21 16:41:48 +10001088 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1089 if (nv_connector->hpd != DCB_GPIO_UNUSED) {
1090 ret = nouveau_gpio_isr_add(dev, 0, nv_connector->hpd, 0xff,
1091 nouveau_connector_hotplug,
1092 connector);
1093 if (ret == 0)
1094 connector->polled = DRM_CONNECTOR_POLL_HPD;
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001095 }
1096
Ben Skeggs6ee73862009-12-11 19:24:15 +10001097 drm_sysfs_connector_add(connector);
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001098 return connector;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001099}
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001100
1101static void
1102nouveau_connector_hotplug(void *data, int plugged)
1103{
1104 struct drm_connector *connector = data;
1105 struct drm_device *dev = connector->dev;
1106
Ben Skeggs68c99182011-08-02 13:57:10 +10001107 NV_DEBUG(dev, "%splugged %s\n", plugged ? "" : "un",
1108 drm_get_connector_name(connector));
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001109
Ben Skeggs68c99182011-08-02 13:57:10 +10001110 if (plugged)
1111 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1112 else
1113 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001114
1115 drm_helper_hpd_irq_event(dev);
1116}