blob: 4da776f344d70d205297df5e2d0fb351898e6d73 [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
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/drm_edid.h>
31#include <drm/drm_crtc_helper.h>
Ben Skeggsa1470892010-01-18 11:42:37 +100032
Ben Skeggs6ee73862009-12-11 19:24:15 +100033#include "nouveau_reg.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100034#include "nouveau_drm.h"
Ben Skeggs1a646342013-03-21 15:45:11 +100035#include "dispnv04/hw.h"
Ben Skeggsc0077062012-07-26 08:51:21 +100036#include "nouveau_acpi.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100037
Ben Skeggs77145f12012-07-31 16:16:21 +100038#include "nouveau_display.h"
39#include "nouveau_connector.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100040#include "nouveau_encoder.h"
41#include "nouveau_crtc.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100042
43#include <subdev/i2c.h>
44#include <subdev/gpio.h>
45
46MODULE_PARM_DESC(tv_disable, "Disable TV-out detection");
47static int nouveau_tv_disable = 0;
48module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
49
50MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
51static int nouveau_ignorelid = 0;
52module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
53
54MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (default: enabled)");
55static int nouveau_duallink = 1;
56module_param_named(duallink, nouveau_duallink, int, 0400);
Ben Skeggs6ee73862009-12-11 19:24:15 +100057
Ben Skeggs10b461e2011-08-02 19:29:37 +100058struct nouveau_encoder *
Ben Skeggse19b20b2011-07-12 13:32:07 +100059find_encoder(struct drm_connector *connector, int type)
Ben Skeggs6ee73862009-12-11 19:24:15 +100060{
61 struct drm_device *dev = connector->dev;
62 struct nouveau_encoder *nv_encoder;
63 struct drm_mode_object *obj;
64 int i, id;
65
66 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
67 id = connector->encoder_ids[i];
68 if (!id)
69 break;
70
71 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
72 if (!obj)
73 continue;
74 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
75
Ben Skeggscb75d972012-07-11 10:44:20 +100076 if (type == DCB_OUTPUT_ANY || nv_encoder->dcb->type == type)
Ben Skeggs6ee73862009-12-11 19:24:15 +100077 return nv_encoder;
78 }
79
80 return NULL;
81}
82
83struct nouveau_connector *
84nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
85{
86 struct drm_device *dev = to_drm_encoder(encoder)->dev;
87 struct drm_connector *drm_connector;
88
89 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
90 if (drm_connector->encoder == to_drm_encoder(encoder))
91 return nouveau_connector(drm_connector);
92 }
93
94 return NULL;
95}
96
Ben Skeggs6ee73862009-12-11 19:24:15 +100097static void
Ben Skeggsfce2bad2010-11-11 16:14:56 +100098nouveau_connector_destroy(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +100099{
Ben Skeggsfce2bad2010-11-11 16:14:56 +1000100 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Xavier Chantryc8ebe272010-01-11 22:42:21 +0100101 kfree(nv_connector->edid);
Ben Skeggsfce2bad2010-11-11 16:14:56 +1000102 drm_sysfs_connector_remove(connector);
103 drm_connector_cleanup(connector);
104 kfree(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000105}
106
Ben Skeggs4196faa2012-07-10 14:36:38 +1000107static struct nouveau_i2c_port *
Ben Skeggs6ee73862009-12-11 19:24:15 +1000108nouveau_connector_ddc_detect(struct drm_connector *connector,
109 struct nouveau_encoder **pnv_encoder)
110{
111 struct drm_device *dev = connector->dev;
Ben Skeggs1a1841d2012-12-10 18:53:43 +1000112 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs77145f12012-07-31 16:16:21 +1000113 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs1a1841d2012-12-10 18:53:43 +1000114 struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
Ben Skeggs1a1841d2012-12-10 18:53:43 +1000115 struct nouveau_i2c_port *port = NULL;
116 int i, panel = -ENODEV;
117
118 /* eDP panels need powering on by us (if the VBIOS doesn't default it
119 * to on) before doing any AUX channel transactions. LVDS panel power
120 * is handled by the SOR itself, and not required for LVDS DDC.
121 */
122 if (nv_connector->type == DCB_CONNECTOR_eDP) {
123 panel = gpio->get(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff);
124 if (panel == 0) {
125 gpio->set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 1);
126 msleep(300);
127 }
128 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000129
Ben Skeggs6ee73862009-12-11 19:24:15 +1000130 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000131 struct nouveau_encoder *nv_encoder;
132 struct drm_mode_object *obj;
133 int id;
134
135 id = connector->encoder_ids[i];
136 if (!id)
137 break;
138
139 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
140 if (!obj)
141 continue;
142 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
Francisco Jerez4ca2b712010-08-08 21:35:57 +0200143
Ben Skeggs5ed50202013-02-11 20:15:03 +1000144 port = nv_encoder->i2c;
Ben Skeggs77145f12012-07-31 16:16:21 +1000145 if (port && nv_probe_i2c(port, 0x50)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000146 *pnv_encoder = nv_encoder;
Ben Skeggs1a1841d2012-12-10 18:53:43 +1000147 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000148 }
Ben Skeggs1a1841d2012-12-10 18:53:43 +1000149
150 port = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000151 }
152
Ben Skeggs1a1841d2012-12-10 18:53:43 +1000153 /* eDP panel not detected, restore panel power GPIO to previous
154 * state to avoid confusing the SOR for other output types.
155 */
156 if (!port && panel == 0)
157 gpio->set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, panel);
158
159 return port;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000160}
161
Francisco Jerezc16c5702010-09-09 14:33:17 +0200162static struct nouveau_encoder *
163nouveau_connector_of_detect(struct drm_connector *connector)
164{
165#ifdef __powerpc__
166 struct drm_device *dev = connector->dev;
167 struct nouveau_connector *nv_connector = nouveau_connector(connector);
168 struct nouveau_encoder *nv_encoder;
169 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
170
171 if (!dn ||
Ben Skeggscb75d972012-07-11 10:44:20 +1000172 !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
173 (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
Francisco Jerezc16c5702010-09-09 14:33:17 +0200174 return NULL;
175
176 for_each_child_of_node(dn, cn) {
177 const char *name = of_get_property(cn, "name", NULL);
178 const void *edid = of_get_property(cn, "EDID", NULL);
179 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
180
181 if (nv_encoder->dcb->i2c_index == idx && edid) {
182 nv_connector->edid =
183 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
184 of_node_put(cn);
185 return nv_encoder;
186 }
187 }
188#endif
189 return NULL;
190}
191
Ben Skeggs6ee73862009-12-11 19:24:15 +1000192static void
193nouveau_connector_set_encoder(struct drm_connector *connector,
194 struct nouveau_encoder *nv_encoder)
195{
196 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs77145f12012-07-31 16:16:21 +1000197 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000198 struct drm_device *dev = connector->dev;
199
200 if (nv_connector->detected_encoder == nv_encoder)
201 return;
202 nv_connector->detected_encoder = nv_encoder;
203
Ben Skeggs77145f12012-07-31 16:16:21 +1000204 if (nv_device(drm->device)->card_type >= NV_50) {
Ben Skeggsc8334422011-10-19 13:06:48 +1000205 connector->interlace_allowed = true;
206 connector->doublescan_allowed = true;
207 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000208 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
209 nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000210 connector->doublescan_allowed = false;
211 connector->interlace_allowed = false;
212 } else {
213 connector->doublescan_allowed = true;
Ben Skeggs77145f12012-07-31 16:16:21 +1000214 if (nv_device(drm->device)->card_type == NV_20 ||
215 (nv_device(drm->device)->card_type == NV_10 &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000216 (dev->pci_device & 0x0ff0) != 0x0100 &&
217 (dev->pci_device & 0x0ff0) != 0x0150))
218 /* HW is broken */
219 connector->interlace_allowed = false;
220 else
221 connector->interlace_allowed = true;
222 }
223
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000224 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
Rob Clark2db83822012-10-11 20:42:04 -0500225 drm_object_property_set_value(&connector->base,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000226 dev->mode_config.dvi_i_subconnector_property,
Ben Skeggscb75d972012-07-11 10:44:20 +1000227 nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
Ben Skeggs6ee73862009-12-11 19:24:15 +1000228 DRM_MODE_SUBCONNECTOR_DVID :
229 DRM_MODE_SUBCONNECTOR_DVIA);
230 }
231}
232
233static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100234nouveau_connector_detect(struct drm_connector *connector, bool force)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235{
236 struct drm_device *dev = connector->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000237 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000238 struct nouveau_connector *nv_connector = nouveau_connector(connector);
239 struct nouveau_encoder *nv_encoder = NULL;
Ben Skeggse19b20b2011-07-12 13:32:07 +1000240 struct nouveau_encoder *nv_partner;
Ben Skeggs4196faa2012-07-10 14:36:38 +1000241 struct nouveau_i2c_port *i2c;
Francisco Jerez03cd06c2010-07-20 03:08:25 +0200242 int type;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000243
Francisco Jerezb8780e22010-01-14 15:54:23 +0100244 /* Cleanup the previous EDID block. */
245 if (nv_connector->edid) {
246 drm_mode_connector_update_edid_property(connector, NULL);
247 kfree(nv_connector->edid);
248 nv_connector->edid = NULL;
249 }
Xavier Chantryc8ebe272010-01-11 22:42:21 +0100250
Ben Skeggs6ee73862009-12-11 19:24:15 +1000251 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
252 if (i2c) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000253 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254 drm_mode_connector_update_edid_property(connector,
255 nv_connector->edid);
256 if (!nv_connector->edid) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000257 NV_ERROR(drm, "DDC responded, but no EDID for %s\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000258 drm_get_connector_name(connector));
Francisco Jerez0ed31652010-01-14 15:47:03 +0100259 goto detect_analog;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000260 }
261
Ben Skeggscb75d972012-07-11 10:44:20 +1000262 if (nv_encoder->dcb->type == DCB_OUTPUT_DP &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000263 !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000264 NV_ERROR(drm, "Detected %s, but failed init\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000265 drm_get_connector_name(connector));
266 return connector_status_disconnected;
267 }
268
269 /* Override encoder type for DVI-I based on whether EDID
270 * says the display is digital or analog, both use the
271 * same i2c channel so the value returned from ddc_detect
272 * isn't necessarily correct.
273 */
Ben Skeggse19b20b2011-07-12 13:32:07 +1000274 nv_partner = NULL;
Ben Skeggscb75d972012-07-11 10:44:20 +1000275 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
276 nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
277 if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
278 nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
Ben Skeggse19b20b2011-07-12 13:32:07 +1000279
Ben Skeggscb75d972012-07-11 10:44:20 +1000280 if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
281 nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
282 (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
283 nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000284 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
Ben Skeggscb75d972012-07-11 10:44:20 +1000285 type = DCB_OUTPUT_TMDS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000286 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000287 type = DCB_OUTPUT_ANALOG;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000288
Ben Skeggse19b20b2011-07-12 13:32:07 +1000289 nv_encoder = find_encoder(connector, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000290 }
291
292 nouveau_connector_set_encoder(connector, nv_encoder);
293 return connector_status_connected;
294 }
295
Francisco Jerezc16c5702010-09-09 14:33:17 +0200296 nv_encoder = nouveau_connector_of_detect(connector);
297 if (nv_encoder) {
298 nouveau_connector_set_encoder(connector, nv_encoder);
299 return connector_status_connected;
300 }
301
Francisco Jerez0ed31652010-01-14 15:47:03 +0100302detect_analog:
Ben Skeggscb75d972012-07-11 10:44:20 +1000303 nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
Ben Skeggsf4053502010-03-15 09:43:51 +1000304 if (!nv_encoder && !nouveau_tv_disable)
Ben Skeggscb75d972012-07-11 10:44:20 +1000305 nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
Francisco Jerez84b80812010-10-26 02:17:56 +0200306 if (nv_encoder && force) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000307 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
308 struct drm_encoder_helper_funcs *helper =
309 encoder->helper_private;
310
311 if (helper->detect(encoder, connector) ==
312 connector_status_connected) {
313 nouveau_connector_set_encoder(connector, nv_encoder);
314 return connector_status_connected;
315 }
316
317 }
318
319 return connector_status_disconnected;
320}
321
Ben Skeggsd17f3952010-06-01 13:32:42 +1000322static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100323nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
Ben Skeggsd17f3952010-06-01 13:32:42 +1000324{
325 struct drm_device *dev = connector->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000326 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000327 struct nouveau_connector *nv_connector = nouveau_connector(connector);
328 struct nouveau_encoder *nv_encoder = NULL;
329 enum drm_connector_status status = connector_status_disconnected;
330
331 /* Cleanup the previous EDID block. */
332 if (nv_connector->edid) {
333 drm_mode_connector_update_edid_property(connector, NULL);
334 kfree(nv_connector->edid);
335 nv_connector->edid = NULL;
336 }
337
Ben Skeggscb75d972012-07-11 10:44:20 +1000338 nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000339 if (!nv_encoder)
340 return connector_status_disconnected;
341
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000342 /* Try retrieving EDID via DDC */
Ben Skeggs77145f12012-07-31 16:16:21 +1000343 if (!drm->vbios.fp_no_ddc) {
Chris Wilson930a9e22010-09-14 11:07:23 +0100344 status = nouveau_connector_detect(connector, force);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000345 if (status == connector_status_connected)
346 goto out;
347 }
348
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000349 /* On some laptops (Sony, i'm looking at you) there appears to
350 * be no direct way of accessing the panel's EDID. The only
351 * option available to us appears to be to ask ACPI for help..
352 *
353 * It's important this check's before trying straps, one of the
354 * said manufacturer's laptops are configured in such a way
355 * the nouveau decides an entry in the VBIOS FP mode table is
356 * valid - it's not (rh#613284)
357 */
358 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
Maarten Lankhorstdf285502012-11-08 17:19:38 +0100359 if ((nv_connector->edid = nouveau_acpi_edid(dev, connector))) {
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000360 status = connector_status_connected;
361 goto out;
362 }
363 }
364
Ben Skeggsd17f3952010-06-01 13:32:42 +1000365 /* If no EDID found above, and the VBIOS indicates a hardcoded
366 * modeline is avalilable for the panel, set it as the panel's
367 * native mode and exit.
368 */
Ben Skeggs77145f12012-07-31 16:16:21 +1000369 if (nouveau_bios_fp_mode(dev, NULL) && (drm->vbios.fp_no_ddc ||
Ben Skeggsd17f3952010-06-01 13:32:42 +1000370 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
371 status = connector_status_connected;
372 goto out;
373 }
374
375 /* Still nothing, some VBIOS images have a hardcoded EDID block
376 * stored for the panel stored in them.
377 */
Ben Skeggs77145f12012-07-31 16:16:21 +1000378 if (!drm->vbios.fp_no_ddc) {
Ben Skeggsd17f3952010-06-01 13:32:42 +1000379 struct edid *edid =
380 (struct edid *)nouveau_bios_embedded_edid(dev);
381 if (edid) {
Marcin Slusarza441dbb2013-01-27 17:04:48 +0100382 nv_connector->edid =
383 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
384 if (nv_connector->edid)
385 status = connector_status_connected;
Ben Skeggsd17f3952010-06-01 13:32:42 +1000386 }
387 }
388
389out:
390#if defined(CONFIG_ACPI_BUTTON) || \
391 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
392 if (status == connector_status_connected &&
393 !nouveau_ignorelid && !acpi_lid_open())
394 status = connector_status_unknown;
395#endif
396
397 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
Albert Damen3195c5f2010-06-20 16:57:57 +0200398 nouveau_connector_set_encoder(connector, nv_encoder);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000399 return status;
400}
401
Ben Skeggs6ee73862009-12-11 19:24:15 +1000402static void
403nouveau_connector_force(struct drm_connector *connector)
404{
Ben Skeggs77145f12012-07-31 16:16:21 +1000405 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe079e92010-02-24 14:01:40 +1000406 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000407 struct nouveau_encoder *nv_encoder;
408 int type;
409
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000410 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000411 if (connector->force == DRM_FORCE_ON_DIGITAL)
Ben Skeggscb75d972012-07-11 10:44:20 +1000412 type = DCB_OUTPUT_TMDS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000413 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000414 type = DCB_OUTPUT_ANALOG;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000415 } else
Ben Skeggscb75d972012-07-11 10:44:20 +1000416 type = DCB_OUTPUT_ANY;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000417
Ben Skeggse19b20b2011-07-12 13:32:07 +1000418 nv_encoder = find_encoder(connector, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000419 if (!nv_encoder) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000420 NV_ERROR(drm, "can't find encoder to force %s on!\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000421 drm_get_connector_name(connector));
422 connector->status = connector_status_disconnected;
423 return;
424 }
425
426 nouveau_connector_set_encoder(connector, nv_encoder);
427}
428
429static int
430nouveau_connector_set_property(struct drm_connector *connector,
431 struct drm_property *property, uint64_t value)
432{
Ben Skeggs77145f12012-07-31 16:16:21 +1000433 struct nouveau_display *disp = nouveau_display(connector->dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000434 struct nouveau_connector *nv_connector = nouveau_connector(connector);
435 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200436 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000437 struct drm_device *dev = connector->dev;
Ben Skeggsb29caa52011-10-06 13:29:05 +1000438 struct nouveau_crtc *nv_crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000439 int ret;
440
Ben Skeggsb29caa52011-10-06 13:29:05 +1000441 nv_crtc = NULL;
442 if (connector->encoder && connector->encoder->crtc)
443 nv_crtc = nouveau_crtc(connector->encoder->crtc);
444
Ben Skeggs6ee73862009-12-11 19:24:15 +1000445 /* Scaling mode */
446 if (property == dev->mode_config.scaling_mode_property) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000447 bool modeset = false;
448
449 switch (value) {
450 case DRM_MODE_SCALE_NONE:
451 case DRM_MODE_SCALE_FULLSCREEN:
452 case DRM_MODE_SCALE_CENTER:
453 case DRM_MODE_SCALE_ASPECT:
454 break;
455 default:
456 return -EINVAL;
457 }
458
459 /* LVDS always needs gpu scaling */
Ben Skeggs8c3f6bb2011-04-18 09:57:48 +1000460 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000461 value == DRM_MODE_SCALE_NONE)
462 return -EINVAL;
463
464 /* Changing between GPU and panel scaling requires a full
465 * modeset
466 */
467 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
468 (value == DRM_MODE_SCALE_NONE))
469 modeset = true;
470 nv_connector->scaling_mode = value;
471
Ben Skeggs6ee73862009-12-11 19:24:15 +1000472 if (!nv_crtc)
473 return 0;
474
475 if (modeset || !nv_crtc->set_scale) {
476 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
477 &nv_crtc->base.mode,
478 nv_crtc->base.x,
479 nv_crtc->base.y, NULL);
480 if (!ret)
481 return -EINVAL;
482 } else {
Ben Skeggs488ff202011-10-17 10:38:10 +1000483 ret = nv_crtc->set_scale(nv_crtc, true);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000484 if (ret)
485 return ret;
486 }
487
488 return 0;
489 }
490
Ben Skeggsb29caa52011-10-06 13:29:05 +1000491 /* Underscan */
492 if (property == disp->underscan_property) {
493 if (nv_connector->underscan != value) {
494 nv_connector->underscan = value;
495 if (!nv_crtc || !nv_crtc->set_scale)
496 return 0;
497
Ben Skeggs488ff202011-10-17 10:38:10 +1000498 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000499 }
500
501 return 0;
502 }
503
504 if (property == disp->underscan_hborder_property) {
505 if (nv_connector->underscan_hborder != value) {
506 nv_connector->underscan_hborder = value;
507 if (!nv_crtc || !nv_crtc->set_scale)
508 return 0;
509
Ben Skeggs488ff202011-10-17 10:38:10 +1000510 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000511 }
512
513 return 0;
514 }
515
516 if (property == disp->underscan_vborder_property) {
517 if (nv_connector->underscan_vborder != value) {
518 nv_connector->underscan_vborder = value;
519 if (!nv_crtc || !nv_crtc->set_scale)
520 return 0;
521
Ben Skeggs488ff202011-10-17 10:38:10 +1000522 return nv_crtc->set_scale(nv_crtc, true);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000523 }
524
525 return 0;
526 }
527
Ben Skeggs6ee73862009-12-11 19:24:15 +1000528 /* Dithering */
Ben Skeggsde691852011-10-17 12:23:41 +1000529 if (property == disp->dithering_mode) {
530 nv_connector->dithering_mode = value;
531 if (!nv_crtc || !nv_crtc->set_dither)
532 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000533
Ben Skeggsde691852011-10-17 12:23:41 +1000534 return nv_crtc->set_dither(nv_crtc, true);
535 }
536
537 if (property == disp->dithering_depth) {
538 nv_connector->dithering_depth = value;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000539 if (!nv_crtc || !nv_crtc->set_dither)
540 return 0;
541
Ben Skeggs488ff202011-10-17 10:38:10 +1000542 return nv_crtc->set_dither(nv_crtc, true);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000543 }
544
Christoph Bumillerdf26bc92012-01-21 23:13:26 +0100545 if (nv_crtc && nv_crtc->set_color_vibrance) {
546 /* Hue */
547 if (property == disp->vibrant_hue_property) {
548 nv_crtc->vibrant_hue = value - 90;
549 return nv_crtc->set_color_vibrance(nv_crtc, true);
550 }
551 /* Saturation */
552 if (property == disp->color_vibrance_property) {
553 nv_crtc->color_vibrance = value - 100;
554 return nv_crtc->set_color_vibrance(nv_crtc, true);
555 }
556 }
557
Ben Skeggscb75d972012-07-11 10:44:20 +1000558 if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200559 return get_slave_funcs(encoder)->set_property(
560 encoder, connector, property, value);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000561
562 return -EINVAL;
563}
564
565static struct drm_display_mode *
Ben Skeggs26099a72010-05-28 10:13:16 +1000566nouveau_connector_native_mode(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000567{
Ben Skeggs26099a72010-05-28 10:13:16 +1000568 struct drm_connector_helper_funcs *helper = connector->helper_private;
Ben Skeggs77145f12012-07-31 16:16:21 +1000569 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs26099a72010-05-28 10:13:16 +1000570 struct nouveau_connector *nv_connector = nouveau_connector(connector);
571 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000572 struct drm_display_mode *mode, *largest = NULL;
573 int high_w = 0, high_h = 0, high_v = 0;
574
Ben Skeggs26099a72010-05-28 10:13:16 +1000575 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
Ben Skeggs0d9b6192011-02-07 08:41:18 +1000576 mode->vrefresh = drm_mode_vrefresh(mode);
Francisco Jereza5afb772010-07-18 16:19:16 +0200577 if (helper->mode_valid(connector, mode) != MODE_OK ||
578 (mode->flags & DRM_MODE_FLAG_INTERLACE))
Ben Skeggs26099a72010-05-28 10:13:16 +1000579 continue;
580
581 /* Use preferred mode if there is one.. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000582 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000583 NV_DEBUG(drm, "native mode from preferred\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000584 return drm_mode_duplicate(dev, mode);
585 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000586
Ben Skeggs26099a72010-05-28 10:13:16 +1000587 /* Otherwise, take the resolution with the largest width, then
588 * height, then vertical refresh
589 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000590 if (mode->hdisplay < high_w)
591 continue;
592
593 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
594 continue;
595
596 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
597 mode->vrefresh < high_v)
598 continue;
599
600 high_w = mode->hdisplay;
601 high_h = mode->vdisplay;
602 high_v = mode->vrefresh;
603 largest = mode;
604 }
605
Ben Skeggs77145f12012-07-31 16:16:21 +1000606 NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000607 high_w, high_h, high_v);
608 return largest ? drm_mode_duplicate(dev, largest) : NULL;
609}
610
611struct moderec {
612 int hdisplay;
613 int vdisplay;
614};
615
616static struct moderec scaler_modes[] = {
617 { 1920, 1200 },
618 { 1920, 1080 },
619 { 1680, 1050 },
620 { 1600, 1200 },
621 { 1400, 1050 },
622 { 1280, 1024 },
623 { 1280, 960 },
624 { 1152, 864 },
625 { 1024, 768 },
626 { 800, 600 },
627 { 720, 400 },
628 { 640, 480 },
629 { 640, 400 },
630 { 640, 350 },
631 {}
632};
633
634static int
635nouveau_connector_scaler_modes_add(struct drm_connector *connector)
636{
637 struct nouveau_connector *nv_connector = nouveau_connector(connector);
638 struct drm_display_mode *native = nv_connector->native_mode, *m;
639 struct drm_device *dev = connector->dev;
640 struct moderec *mode = &scaler_modes[0];
641 int modes = 0;
642
643 if (!native)
644 return 0;
645
646 while (mode->hdisplay) {
647 if (mode->hdisplay <= native->hdisplay &&
648 mode->vdisplay <= native->vdisplay) {
649 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
650 drm_mode_vrefresh(native), false,
651 false, false);
652 if (!m)
653 continue;
654
655 m->type |= DRM_MODE_TYPE_DRIVER;
656
657 drm_mode_probed_add(connector, m);
658 modes++;
659 }
660
661 mode++;
662 }
663
664 return modes;
665}
666
Ben Skeggs63221752011-10-17 10:24:49 +1000667static void
668nouveau_connector_detect_depth(struct drm_connector *connector)
669{
Ben Skeggs77145f12012-07-31 16:16:21 +1000670 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs63221752011-10-17 10:24:49 +1000671 struct nouveau_connector *nv_connector = nouveau_connector(connector);
672 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Ben Skeggs77145f12012-07-31 16:16:21 +1000673 struct nvbios *bios = &drm->vbios;
Ben Skeggs63221752011-10-17 10:24:49 +1000674 struct drm_display_mode *mode = nv_connector->native_mode;
675 bool duallink;
676
677 /* if the edid is feeling nice enough to provide this info, use it */
678 if (nv_connector->edid && connector->display_info.bpc)
679 return;
680
Ben Skeggsa6a17852012-05-05 00:39:21 +1000681 /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
682 if (nv_connector->type == DCB_CONNECTOR_eDP) {
683 connector->display_info.bpc = 6;
684 return;
685 }
686
687 /* we're out of options unless we're LVDS, default to 8bpc */
Ben Skeggscb75d972012-07-11 10:44:20 +1000688 if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
Ben Skeggsc8435362012-03-24 16:26:13 +1000689 connector->display_info.bpc = 8;
Ben Skeggs63221752011-10-17 10:24:49 +1000690 return;
Ben Skeggsc8435362012-03-24 16:26:13 +1000691 }
692
693 connector->display_info.bpc = 6;
Ben Skeggs63221752011-10-17 10:24:49 +1000694
695 /* LVDS: panel straps */
696 if (bios->fp_no_ddc) {
697 if (bios->fp.if_is_24bit)
698 connector->display_info.bpc = 8;
699 return;
700 }
701
702 /* LVDS: DDC panel, need to first determine the number of links to
703 * know which if_is_24bit flag to check...
704 */
705 if (nv_connector->edid &&
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000706 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
Ben Skeggs63221752011-10-17 10:24:49 +1000707 duallink = ((u8 *)nv_connector->edid)[121] == 2;
708 else
709 duallink = mode->clock >= bios->fp.duallink_transition_clk;
710
711 if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
712 ( duallink && (bios->fp.strapless_is_24bit & 2)))
713 connector->display_info.bpc = 8;
714}
715
Ben Skeggs6ee73862009-12-11 19:24:15 +1000716static int
717nouveau_connector_get_modes(struct drm_connector *connector)
718{
719 struct drm_device *dev = connector->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000720 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000721 struct nouveau_connector *nv_connector = nouveau_connector(connector);
722 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200723 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000724 int ret = 0;
725
Ben Skeggsd17f3952010-06-01 13:32:42 +1000726 /* destroy the native mode, the attached monitor could have changed.
Ben Skeggs6ee73862009-12-11 19:24:15 +1000727 */
Ben Skeggsd17f3952010-06-01 13:32:42 +1000728 if (nv_connector->native_mode) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000729 drm_mode_destroy(dev, nv_connector->native_mode);
730 nv_connector->native_mode = NULL;
731 }
732
733 if (nv_connector->edid)
734 ret = drm_add_edid_modes(connector, nv_connector->edid);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000735 else
Ben Skeggscb75d972012-07-11 10:44:20 +1000736 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
Ben Skeggsd17f3952010-06-01 13:32:42 +1000737 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
Ben Skeggs77145f12012-07-31 16:16:21 +1000738 drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
Ben Skeggs80dad862010-09-23 16:37:39 +1000739 struct drm_display_mode mode;
740
741 nouveau_bios_fp_mode(dev, &mode);
742 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
Ben Skeggsd17f3952010-06-01 13:32:42 +1000743 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000744
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000745 /* Determine display colour depth for everything except LVDS now,
746 * DP requires this before mode_valid() is called.
747 */
748 if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
749 nouveau_connector_detect_depth(connector);
750
Ben Skeggs6ee73862009-12-11 19:24:15 +1000751 /* Find the native mode if this is a digital panel, if we didn't
752 * find any modes through DDC previously add the native mode to
753 * the list of modes.
754 */
755 if (!nv_connector->native_mode)
756 nv_connector->native_mode =
Ben Skeggs26099a72010-05-28 10:13:16 +1000757 nouveau_connector_native_mode(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000758 if (ret == 0 && nv_connector->native_mode) {
759 struct drm_display_mode *mode;
760
761 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
762 drm_mode_probed_add(connector, mode);
763 ret = 1;
764 }
765
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000766 /* Determine LVDS colour depth, must happen after determining
767 * "native" mode as some VBIOS tables require us to use the
768 * pixel clock as part of the lookup...
Ben Skeggs63221752011-10-17 10:24:49 +1000769 */
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000770 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
771 nouveau_connector_detect_depth(connector);
Ben Skeggs63221752011-10-17 10:24:49 +1000772
Ben Skeggscb75d972012-07-11 10:44:20 +1000773 if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200774 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000775
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000776 if (nv_connector->type == DCB_CONNECTOR_LVDS ||
777 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
778 nv_connector->type == DCB_CONNECTOR_eDP)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000779 ret += nouveau_connector_scaler_modes_add(connector);
780
781 return ret;
782}
783
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200784static unsigned
785get_tmds_link_bandwidth(struct drm_connector *connector)
786{
787 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs77145f12012-07-31 16:16:21 +1000788 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggscb75d972012-07-11 10:44:20 +1000789 struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200790
791 if (dcb->location != DCB_LOC_ON_CHIP ||
Ben Skeggs77145f12012-07-31 16:16:21 +1000792 nv_device(drm->device)->chipset >= 0x46)
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200793 return 165000;
Ben Skeggs77145f12012-07-31 16:16:21 +1000794 else if (nv_device(drm->device)->chipset >= 0x40)
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200795 return 155000;
Ben Skeggs77145f12012-07-31 16:16:21 +1000796 else if (nv_device(drm->device)->chipset >= 0x18)
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200797 return 135000;
798 else
799 return 112000;
800}
801
Ben Skeggs6ee73862009-12-11 19:24:15 +1000802static int
803nouveau_connector_mode_valid(struct drm_connector *connector,
804 struct drm_display_mode *mode)
805{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000806 struct nouveau_connector *nv_connector = nouveau_connector(connector);
807 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200808 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000809 unsigned min_clock = 25000, max_clock = min_clock;
810 unsigned clock = mode->clock;
811
812 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000813 case DCB_OUTPUT_LVDS:
Ben Skeggs26099a72010-05-28 10:13:16 +1000814 if (nv_connector->native_mode &&
815 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
816 mode->vdisplay > nv_connector->native_mode->vdisplay))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000817 return MODE_PANEL;
818
819 min_clock = 0;
820 max_clock = 400000;
821 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000822 case DCB_OUTPUT_TMDS:
Francisco Jerez1f5bd442010-10-02 16:00:35 +0200823 max_clock = get_tmds_link_bandwidth(connector);
824 if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
825 max_clock *= 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000826 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000827 case DCB_OUTPUT_ANALOG:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000828 max_clock = nv_encoder->dcb->crtconf.maxfreq;
829 if (!max_clock)
830 max_clock = 350000;
831 break;
Ben Skeggscb75d972012-07-11 10:44:20 +1000832 case DCB_OUTPUT_TV:
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200833 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
Ben Skeggscb75d972012-07-11 10:44:20 +1000834 case DCB_OUTPUT_DP:
Ben Skeggs75a1fcc2011-08-04 09:55:44 +1000835 max_clock = nv_encoder->dp.link_nr;
836 max_clock *= nv_encoder->dp.link_bw;
Ben Skeggsd4c2c992011-11-15 14:31:13 +1000837 clock = clock * (connector->display_info.bpc * 3) / 10;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000838 break;
Ben Skeggse7cc51c2010-02-24 10:31:39 +1000839 default:
840 BUG_ON(1);
841 return MODE_BAD;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000842 }
843
844 if (clock < min_clock)
845 return MODE_CLOCK_LOW;
846
847 if (clock > max_clock)
848 return MODE_CLOCK_HIGH;
849
850 return MODE_OK;
851}
852
853static struct drm_encoder *
854nouveau_connector_best_encoder(struct drm_connector *connector)
855{
856 struct nouveau_connector *nv_connector = nouveau_connector(connector);
857
858 if (nv_connector->detected_encoder)
859 return to_drm_encoder(nv_connector->detected_encoder);
860
861 return NULL;
862}
863
864static const struct drm_connector_helper_funcs
865nouveau_connector_helper_funcs = {
866 .get_modes = nouveau_connector_get_modes,
867 .mode_valid = nouveau_connector_mode_valid,
868 .best_encoder = nouveau_connector_best_encoder,
869};
870
871static const struct drm_connector_funcs
872nouveau_connector_funcs = {
873 .dpms = drm_helper_connector_dpms,
874 .save = NULL,
875 .restore = NULL,
876 .detect = nouveau_connector_detect,
877 .destroy = nouveau_connector_destroy,
878 .fill_modes = drm_helper_probe_single_connector_modes,
879 .set_property = nouveau_connector_set_property,
880 .force = nouveau_connector_force
881};
882
Ben Skeggsd17f3952010-06-01 13:32:42 +1000883static const struct drm_connector_funcs
884nouveau_connector_funcs_lvds = {
885 .dpms = drm_helper_connector_dpms,
886 .save = NULL,
887 .restore = NULL,
888 .detect = nouveau_connector_detect_lvds,
889 .destroy = nouveau_connector_destroy,
890 .fill_modes = drm_helper_probe_single_connector_modes,
891 .set_property = nouveau_connector_set_property,
892 .force = nouveau_connector_force
893};
Ben Skeggs6ee73862009-12-11 19:24:15 +1000894
Ben Skeggs4f476432013-02-03 12:56:16 +1000895static void
896nouveau_connector_hotplug_work(struct work_struct *work)
897{
898 struct nouveau_connector *nv_connector =
899 container_of(work, struct nouveau_connector, hpd_work);
900 struct drm_connector *connector = &nv_connector->base;
901 struct drm_device *dev = connector->dev;
902 struct nouveau_drm *drm = nouveau_drm(dev);
903 struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
904 bool plugged = gpio->get(gpio, 0, nv_connector->hpd.func, 0xff);
905
906 NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un",
907 drm_get_connector_name(connector));
908
909 if (plugged)
910 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
911 else
912 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
913
914 drm_helper_hpd_irq_event(dev);
915}
916
917static int
918nouveau_connector_hotplug(struct nouveau_eventh *event, int index)
919{
920 struct nouveau_connector *nv_connector =
921 container_of(event, struct nouveau_connector, hpd_func);
922 schedule_work(&nv_connector->hpd_work);
923 return NVKM_EVENT_KEEP;
924}
925
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000926static int
927drm_conntype_from_dcb(enum dcb_connector_type dcb)
928{
929 switch (dcb) {
930 case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
931 case DCB_CONNECTOR_TV_0 :
932 case DCB_CONNECTOR_TV_1 :
933 case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
Ben Skeggsfa2c1132012-03-22 11:27:15 +1000934 case DCB_CONNECTOR_DMS59_0 :
935 case DCB_CONNECTOR_DMS59_1 :
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000936 case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
937 case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
938 case DCB_CONNECTOR_LVDS :
939 case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
Ben Skeggs4abb4102012-01-12 16:17:16 +1000940 case DCB_CONNECTOR_DMS59_DP0:
941 case DCB_CONNECTOR_DMS59_DP1:
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000942 case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort;
943 case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
944 case DCB_CONNECTOR_HDMI_0 :
945 case DCB_CONNECTOR_HDMI_1 : return DRM_MODE_CONNECTOR_HDMIA;
946 default:
947 break;
948 }
949
950 return DRM_MODE_CONNECTOR_Unknown;
951}
952
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000953struct drm_connector *
954nouveau_connector_create(struct drm_device *dev, int index)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000955{
Ben Skeggsd17f3952010-06-01 13:32:42 +1000956 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
Ben Skeggs77145f12012-07-31 16:16:21 +1000957 struct nouveau_drm *drm = nouveau_drm(dev);
958 struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
959 struct nouveau_display *disp = nouveau_display(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000960 struct nouveau_connector *nv_connector = NULL;
961 struct drm_connector *connector;
Francisco Jerez2fa67f12010-06-16 15:52:44 +0200962 int type, ret = 0;
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000963 bool dummy;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000964
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000965 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
966 nv_connector = nouveau_connector(connector);
967 if (nv_connector->index == index)
968 return connector;
Ben Skeggs7f612d82010-02-24 12:07:31 +1000969 }
970
Ben Skeggs6ee73862009-12-11 19:24:15 +1000971 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
972 if (!nv_connector)
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000973 return ERR_PTR(-ENOMEM);
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000974
Ben Skeggs6ee73862009-12-11 19:24:15 +1000975 connector = &nv_connector->base;
Ben Skeggs4f476432013-02-03 12:56:16 +1000976 INIT_WORK(&nv_connector->hpd_work, nouveau_connector_hotplug_work);
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000977 nv_connector->index = index;
978
979 /* attempt to parse vbios connector type and hotplug gpio */
Ben Skeggscb75d972012-07-11 10:44:20 +1000980 nv_connector->dcb = olddcb_conn(dev, index);
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000981 if (nv_connector->dcb) {
982 static const u8 hpd[16] = {
983 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
984 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
985 };
986
987 u32 entry = ROM16(nv_connector->dcb[0]);
Ben Skeggscb75d972012-07-11 10:44:20 +1000988 if (olddcb_conntab(dev)[3] >= 4)
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000989 entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
990
Ben Skeggs23fc09e2013-02-03 20:29:53 +1000991 ret = gpio->find(gpio, 0, hpd[ffs((entry & 0x07033000) >> 12)],
992 DCB_GPIO_UNUSED, &nv_connector->hpd);
Ben Skeggs4f476432013-02-03 12:56:16 +1000993 nv_connector->hpd_func.func = nouveau_connector_hotplug;
Ben Skeggs23fc09e2013-02-03 20:29:53 +1000994 if (ret)
995 nv_connector->hpd.func = DCB_GPIO_UNUSED;
Ben Skeggsbefb51e2011-11-18 10:23:59 +1000996
997 nv_connector->type = nv_connector->dcb[0];
998 if (drm_conntype_from_dcb(nv_connector->type) ==
999 DRM_MODE_CONNECTOR_Unknown) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001000 NV_WARN(drm, "unknown connector type %02x\n",
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001001 nv_connector->type);
1002 nv_connector->type = DCB_CONNECTOR_NONE;
1003 }
1004
1005 /* Gigabyte NX85T */
1006 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
1007 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1008 nv_connector->type = DCB_CONNECTOR_DVI_I;
1009 }
1010
1011 /* Gigabyte GV-NX86T512H */
1012 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
1013 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1014 nv_connector->type = DCB_CONNECTOR_DVI_I;
1015 }
1016 } else {
1017 nv_connector->type = DCB_CONNECTOR_NONE;
Ben Skeggs23fc09e2013-02-03 20:29:53 +10001018 nv_connector->hpd.func = DCB_GPIO_UNUSED;
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001019 }
1020
1021 /* no vbios data, or an unknown dcb connector type - attempt to
1022 * figure out something suitable ourselves
1023 */
1024 if (nv_connector->type == DCB_CONNECTOR_NONE) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001025 struct nouveau_drm *drm = nouveau_drm(dev);
1026 struct dcb_table *dcbt = &drm->vbios.dcb;
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001027 u32 encoders = 0;
1028 int i;
1029
1030 for (i = 0; i < dcbt->entries; i++) {
1031 if (dcbt->entry[i].connector == nv_connector->index)
1032 encoders |= (1 << dcbt->entry[i].type);
1033 }
1034
Ben Skeggscb75d972012-07-11 10:44:20 +10001035 if (encoders & (1 << DCB_OUTPUT_DP)) {
1036 if (encoders & (1 << DCB_OUTPUT_TMDS))
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001037 nv_connector->type = DCB_CONNECTOR_DP;
1038 else
1039 nv_connector->type = DCB_CONNECTOR_eDP;
1040 } else
Ben Skeggscb75d972012-07-11 10:44:20 +10001041 if (encoders & (1 << DCB_OUTPUT_TMDS)) {
1042 if (encoders & (1 << DCB_OUTPUT_ANALOG))
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001043 nv_connector->type = DCB_CONNECTOR_DVI_I;
1044 else
1045 nv_connector->type = DCB_CONNECTOR_DVI_D;
1046 } else
Ben Skeggscb75d972012-07-11 10:44:20 +10001047 if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001048 nv_connector->type = DCB_CONNECTOR_VGA;
1049 } else
Ben Skeggscb75d972012-07-11 10:44:20 +10001050 if (encoders & (1 << DCB_OUTPUT_LVDS)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001051 nv_connector->type = DCB_CONNECTOR_LVDS;
1052 } else
Ben Skeggscb75d972012-07-11 10:44:20 +10001053 if (encoders & (1 << DCB_OUTPUT_TV)) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001054 nv_connector->type = DCB_CONNECTOR_TV_0;
1055 }
1056 }
1057
1058 type = drm_conntype_from_dcb(nv_connector->type);
1059 if (type == DRM_MODE_CONNECTOR_LVDS) {
1060 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
1061 if (ret) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001062 NV_ERROR(drm, "Error parsing LVDS table, disabling\n");
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001063 kfree(nv_connector);
1064 return ERR_PTR(ret);
1065 }
1066
1067 funcs = &nouveau_connector_funcs_lvds;
1068 } else {
1069 funcs = &nouveau_connector_funcs;
1070 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001071
Ben Skeggs6ee73862009-12-11 19:24:15 +10001072 /* defaults, will get overridden in detect() */
1073 connector->interlace_allowed = false;
1074 connector->doublescan_allowed = false;
1075
Ben Skeggsd17f3952010-06-01 13:32:42 +10001076 drm_connector_init(dev, connector, funcs, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001077 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1078
1079 /* Init DVI-I specific properties */
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001080 if (nv_connector->type == DCB_CONNECTOR_DVI_I)
Rob Clark2db83822012-10-11 20:42:04 -05001081 drm_object_attach_property(&connector->base, dev->mode_config.dvi_i_subconnector_property, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001082
Ben Skeggsb29caa52011-10-06 13:29:05 +10001083 /* Add overscan compensation options to digital outputs */
Ben Skeggsde691852011-10-17 12:23:41 +10001084 if (disp->underscan_property &&
Ben Skeggsfa2c1132012-03-22 11:27:15 +10001085 (type == DRM_MODE_CONNECTOR_DVID ||
1086 type == DRM_MODE_CONNECTOR_DVII ||
1087 type == DRM_MODE_CONNECTOR_HDMIA ||
1088 type == DRM_MODE_CONNECTOR_DisplayPort)) {
Rob Clark2db83822012-10-11 20:42:04 -05001089 drm_object_attach_property(&connector->base,
Ben Skeggsb29caa52011-10-06 13:29:05 +10001090 disp->underscan_property,
1091 UNDERSCAN_OFF);
Rob Clark2db83822012-10-11 20:42:04 -05001092 drm_object_attach_property(&connector->base,
Ben Skeggsb29caa52011-10-06 13:29:05 +10001093 disp->underscan_hborder_property,
1094 0);
Rob Clark2db83822012-10-11 20:42:04 -05001095 drm_object_attach_property(&connector->base,
Ben Skeggsb29caa52011-10-06 13:29:05 +10001096 disp->underscan_vborder_property,
1097 0);
1098 }
1099
Christoph Bumillerdf26bc92012-01-21 23:13:26 +01001100 /* Add hue and saturation options */
1101 if (disp->vibrant_hue_property)
Rob Clark2db83822012-10-11 20:42:04 -05001102 drm_object_attach_property(&connector->base,
Christoph Bumillerdf26bc92012-01-21 23:13:26 +01001103 disp->vibrant_hue_property,
1104 90);
1105 if (disp->color_vibrance_property)
Rob Clark2db83822012-10-11 20:42:04 -05001106 drm_object_attach_property(&connector->base,
Christoph Bumillerdf26bc92012-01-21 23:13:26 +01001107 disp->color_vibrance_property,
1108 150);
1109
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001110 switch (nv_connector->type) {
Ben Skeggsbe079e92010-02-24 14:01:40 +10001111 case DCB_CONNECTOR_VGA:
Ben Skeggs77145f12012-07-31 16:16:21 +10001112 if (nv_device(drm->device)->card_type >= NV_50) {
Rob Clark2db83822012-10-11 20:42:04 -05001113 drm_object_attach_property(&connector->base,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001114 dev->mode_config.scaling_mode_property,
1115 nv_connector->scaling_mode);
1116 }
Ben Skeggsbe079e92010-02-24 14:01:40 +10001117 /* fall-through */
1118 case DCB_CONNECTOR_TV_0:
1119 case DCB_CONNECTOR_TV_1:
1120 case DCB_CONNECTOR_TV_3:
1121 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1122 break;
1123 default:
1124 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1125
Rob Clark2db83822012-10-11 20:42:04 -05001126 drm_object_attach_property(&connector->base,
Ben Skeggsbe079e92010-02-24 14:01:40 +10001127 dev->mode_config.scaling_mode_property,
1128 nv_connector->scaling_mode);
Ben Skeggsde691852011-10-17 12:23:41 +10001129 if (disp->dithering_mode) {
1130 nv_connector->dithering_mode = DITHERING_MODE_AUTO;
Rob Clark2db83822012-10-11 20:42:04 -05001131 drm_object_attach_property(&connector->base,
Ben Skeggsde691852011-10-17 12:23:41 +10001132 disp->dithering_mode,
1133 nv_connector->dithering_mode);
1134 }
1135 if (disp->dithering_depth) {
1136 nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
Rob Clark2db83822012-10-11 20:42:04 -05001137 drm_object_attach_property(&connector->base,
Ben Skeggsde691852011-10-17 12:23:41 +10001138 disp->dithering_depth,
1139 nv_connector->dithering_depth);
1140 }
Ben Skeggsbe079e92010-02-24 14:01:40 +10001141 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001142 }
1143
Ben Skeggsa0b25632011-11-21 16:41:48 +10001144 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
Ben Skeggs4f476432013-02-03 12:56:16 +10001145 if (nv_connector->hpd.func != DCB_GPIO_UNUSED)
1146 connector->polled = DRM_CONNECTOR_POLL_HPD;
Ben Skeggsfce2bad2010-11-11 16:14:56 +10001147
Ben Skeggs6ee73862009-12-11 19:24:15 +10001148 drm_sysfs_connector_add(connector);
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001149 return connector;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001150}