nouveau_connector.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. * Copyright (C) 2008 Maarten Maathuis.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <acpi/button.h>
  27. #include "drmP.h"
  28. #include "drm_edid.h"
  29. #include "drm_crtc_helper.h"
  30. #include "nouveau_reg.h"
  31. #include "nouveau_drv.h"
  32. #include "nouveau_encoder.h"
  33. #include "nouveau_crtc.h"
  34. #include "nouveau_connector.h"
  35. #include "nouveau_gpio.h"
  36. #include "nouveau_hw.h"
  37. static void nouveau_connector_hotplug(void *, int);
  38. struct nouveau_encoder *
  39. find_encoder(struct drm_connector *connector, int type)
  40. {
  41. struct drm_device *dev = connector->dev;
  42. struct nouveau_encoder *nv_encoder;
  43. struct drm_mode_object *obj;
  44. int i, id;
  45. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  46. id = connector->encoder_ids[i];
  47. if (!id)
  48. break;
  49. obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
  50. if (!obj)
  51. continue;
  52. nv_encoder = nouveau_encoder(obj_to_encoder(obj));
  53. if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
  54. return nv_encoder;
  55. }
  56. return NULL;
  57. }
  58. struct nouveau_connector *
  59. nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
  60. {
  61. struct drm_device *dev = to_drm_encoder(encoder)->dev;
  62. struct drm_connector *drm_connector;
  63. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
  64. if (drm_connector->encoder == to_drm_encoder(encoder))
  65. return nouveau_connector(drm_connector);
  66. }
  67. return NULL;
  68. }
  69. static void
  70. nouveau_connector_destroy(struct drm_connector *connector)
  71. {
  72. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  73. struct drm_nouveau_private *dev_priv;
  74. struct drm_device *dev;
  75. if (!nv_connector)
  76. return;
  77. dev = nv_connector->base.dev;
  78. dev_priv = dev->dev_private;
  79. NV_DEBUG_KMS(dev, "\n");
  80. if (nv_connector->hpd != DCB_GPIO_UNUSED) {
  81. nouveau_gpio_isr_del(dev, 0, nv_connector->hpd, 0xff,
  82. nouveau_connector_hotplug, connector);
  83. }
  84. kfree(nv_connector->edid);
  85. drm_sysfs_connector_remove(connector);
  86. drm_connector_cleanup(connector);
  87. kfree(connector);
  88. }
  89. static struct nouveau_i2c_chan *
  90. nouveau_connector_ddc_detect(struct drm_connector *connector,
  91. struct nouveau_encoder **pnv_encoder)
  92. {
  93. struct drm_device *dev = connector->dev;
  94. int i;
  95. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  96. struct nouveau_i2c_chan *i2c = NULL;
  97. struct nouveau_encoder *nv_encoder;
  98. struct drm_mode_object *obj;
  99. int id;
  100. id = connector->encoder_ids[i];
  101. if (!id)
  102. break;
  103. obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
  104. if (!obj)
  105. continue;
  106. nv_encoder = nouveau_encoder(obj_to_encoder(obj));
  107. if (nv_encoder->dcb->i2c_index < 0xf)
  108. i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  109. if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
  110. *pnv_encoder = nv_encoder;
  111. return i2c;
  112. }
  113. }
  114. return NULL;
  115. }
  116. static struct nouveau_encoder *
  117. nouveau_connector_of_detect(struct drm_connector *connector)
  118. {
  119. #ifdef __powerpc__
  120. struct drm_device *dev = connector->dev;
  121. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  122. struct nouveau_encoder *nv_encoder;
  123. struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
  124. if (!dn ||
  125. !((nv_encoder = find_encoder(connector, OUTPUT_TMDS)) ||
  126. (nv_encoder = find_encoder(connector, OUTPUT_ANALOG))))
  127. return NULL;
  128. for_each_child_of_node(dn, cn) {
  129. const char *name = of_get_property(cn, "name", NULL);
  130. const void *edid = of_get_property(cn, "EDID", NULL);
  131. int idx = name ? name[strlen(name) - 1] - 'A' : 0;
  132. if (nv_encoder->dcb->i2c_index == idx && edid) {
  133. nv_connector->edid =
  134. kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  135. of_node_put(cn);
  136. return nv_encoder;
  137. }
  138. }
  139. #endif
  140. return NULL;
  141. }
  142. static void
  143. nouveau_connector_set_encoder(struct drm_connector *connector,
  144. struct nouveau_encoder *nv_encoder)
  145. {
  146. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  147. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  148. struct drm_device *dev = connector->dev;
  149. if (nv_connector->detected_encoder == nv_encoder)
  150. return;
  151. nv_connector->detected_encoder = nv_encoder;
  152. if (dev_priv->card_type >= NV_50) {
  153. connector->interlace_allowed = true;
  154. connector->doublescan_allowed = true;
  155. } else
  156. if (nv_encoder->dcb->type == OUTPUT_LVDS ||
  157. nv_encoder->dcb->type == OUTPUT_TMDS) {
  158. connector->doublescan_allowed = false;
  159. connector->interlace_allowed = false;
  160. } else {
  161. connector->doublescan_allowed = true;
  162. if (dev_priv->card_type == NV_20 ||
  163. (dev_priv->card_type == NV_10 &&
  164. (dev->pci_device & 0x0ff0) != 0x0100 &&
  165. (dev->pci_device & 0x0ff0) != 0x0150))
  166. /* HW is broken */
  167. connector->interlace_allowed = false;
  168. else
  169. connector->interlace_allowed = true;
  170. }
  171. if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
  172. drm_connector_property_set_value(connector,
  173. dev->mode_config.dvi_i_subconnector_property,
  174. nv_encoder->dcb->type == OUTPUT_TMDS ?
  175. DRM_MODE_SUBCONNECTOR_DVID :
  176. DRM_MODE_SUBCONNECTOR_DVIA);
  177. }
  178. }
  179. static enum drm_connector_status
  180. nouveau_connector_detect(struct drm_connector *connector, bool force)
  181. {
  182. struct drm_device *dev = connector->dev;
  183. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  184. struct nouveau_encoder *nv_encoder = NULL;
  185. struct nouveau_encoder *nv_partner;
  186. struct nouveau_i2c_chan *i2c;
  187. int type;
  188. /* Cleanup the previous EDID block. */
  189. if (nv_connector->edid) {
  190. drm_mode_connector_update_edid_property(connector, NULL);
  191. kfree(nv_connector->edid);
  192. nv_connector->edid = NULL;
  193. }
  194. i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
  195. if (i2c) {
  196. nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
  197. drm_mode_connector_update_edid_property(connector,
  198. nv_connector->edid);
  199. if (!nv_connector->edid) {
  200. NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
  201. drm_get_connector_name(connector));
  202. goto detect_analog;
  203. }
  204. if (nv_encoder->dcb->type == OUTPUT_DP &&
  205. !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
  206. NV_ERROR(dev, "Detected %s, but failed init\n",
  207. drm_get_connector_name(connector));
  208. return connector_status_disconnected;
  209. }
  210. /* Override encoder type for DVI-I based on whether EDID
  211. * says the display is digital or analog, both use the
  212. * same i2c channel so the value returned from ddc_detect
  213. * isn't necessarily correct.
  214. */
  215. nv_partner = NULL;
  216. if (nv_encoder->dcb->type == OUTPUT_TMDS)
  217. nv_partner = find_encoder(connector, OUTPUT_ANALOG);
  218. if (nv_encoder->dcb->type == OUTPUT_ANALOG)
  219. nv_partner = find_encoder(connector, OUTPUT_TMDS);
  220. if (nv_partner && ((nv_encoder->dcb->type == OUTPUT_ANALOG &&
  221. nv_partner->dcb->type == OUTPUT_TMDS) ||
  222. (nv_encoder->dcb->type == OUTPUT_TMDS &&
  223. nv_partner->dcb->type == OUTPUT_ANALOG))) {
  224. if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
  225. type = OUTPUT_TMDS;
  226. else
  227. type = OUTPUT_ANALOG;
  228. nv_encoder = find_encoder(connector, type);
  229. }
  230. nouveau_connector_set_encoder(connector, nv_encoder);
  231. return connector_status_connected;
  232. }
  233. nv_encoder = nouveau_connector_of_detect(connector);
  234. if (nv_encoder) {
  235. nouveau_connector_set_encoder(connector, nv_encoder);
  236. return connector_status_connected;
  237. }
  238. detect_analog:
  239. nv_encoder = find_encoder(connector, OUTPUT_ANALOG);
  240. if (!nv_encoder && !nouveau_tv_disable)
  241. nv_encoder = find_encoder(connector, OUTPUT_TV);
  242. if (nv_encoder && force) {
  243. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  244. struct drm_encoder_helper_funcs *helper =
  245. encoder->helper_private;
  246. if (helper->detect(encoder, connector) ==
  247. connector_status_connected) {
  248. nouveau_connector_set_encoder(connector, nv_encoder);
  249. return connector_status_connected;
  250. }
  251. }
  252. return connector_status_disconnected;
  253. }
  254. static enum drm_connector_status
  255. nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
  256. {
  257. struct drm_device *dev = connector->dev;
  258. struct drm_nouveau_private *dev_priv = dev->dev_private;
  259. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  260. struct nouveau_encoder *nv_encoder = NULL;
  261. enum drm_connector_status status = connector_status_disconnected;
  262. /* Cleanup the previous EDID block. */
  263. if (nv_connector->edid) {
  264. drm_mode_connector_update_edid_property(connector, NULL);
  265. kfree(nv_connector->edid);
  266. nv_connector->edid = NULL;
  267. }
  268. nv_encoder = find_encoder(connector, OUTPUT_LVDS);
  269. if (!nv_encoder)
  270. return connector_status_disconnected;
  271. /* Try retrieving EDID via DDC */
  272. if (!dev_priv->vbios.fp_no_ddc) {
  273. status = nouveau_connector_detect(connector, force);
  274. if (status == connector_status_connected)
  275. goto out;
  276. }
  277. /* On some laptops (Sony, i'm looking at you) there appears to
  278. * be no direct way of accessing the panel's EDID. The only
  279. * option available to us appears to be to ask ACPI for help..
  280. *
  281. * It's important this check's before trying straps, one of the
  282. * said manufacturer's laptops are configured in such a way
  283. * the nouveau decides an entry in the VBIOS FP mode table is
  284. * valid - it's not (rh#613284)
  285. */
  286. if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
  287. if (!nouveau_acpi_edid(dev, connector)) {
  288. status = connector_status_connected;
  289. goto out;
  290. }
  291. }
  292. /* If no EDID found above, and the VBIOS indicates a hardcoded
  293. * modeline is avalilable for the panel, set it as the panel's
  294. * native mode and exit.
  295. */
  296. if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
  297. nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
  298. status = connector_status_connected;
  299. goto out;
  300. }
  301. /* Still nothing, some VBIOS images have a hardcoded EDID block
  302. * stored for the panel stored in them.
  303. */
  304. if (!dev_priv->vbios.fp_no_ddc) {
  305. struct edid *edid =
  306. (struct edid *)nouveau_bios_embedded_edid(dev);
  307. if (edid) {
  308. nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  309. *(nv_connector->edid) = *edid;
  310. status = connector_status_connected;
  311. }
  312. }
  313. out:
  314. #if defined(CONFIG_ACPI_BUTTON) || \
  315. (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
  316. if (status == connector_status_connected &&
  317. !nouveau_ignorelid && !acpi_lid_open())
  318. status = connector_status_unknown;
  319. #endif
  320. drm_mode_connector_update_edid_property(connector, nv_connector->edid);
  321. nouveau_connector_set_encoder(connector, nv_encoder);
  322. return status;
  323. }
  324. static void
  325. nouveau_connector_force(struct drm_connector *connector)
  326. {
  327. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  328. struct nouveau_encoder *nv_encoder;
  329. int type;
  330. if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
  331. if (connector->force == DRM_FORCE_ON_DIGITAL)
  332. type = OUTPUT_TMDS;
  333. else
  334. type = OUTPUT_ANALOG;
  335. } else
  336. type = OUTPUT_ANY;
  337. nv_encoder = find_encoder(connector, type);
  338. if (!nv_encoder) {
  339. NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
  340. drm_get_connector_name(connector));
  341. connector->status = connector_status_disconnected;
  342. return;
  343. }
  344. nouveau_connector_set_encoder(connector, nv_encoder);
  345. }
  346. static int
  347. nouveau_connector_set_property(struct drm_connector *connector,
  348. struct drm_property *property, uint64_t value)
  349. {
  350. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  351. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  352. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  353. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  354. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  355. struct drm_device *dev = connector->dev;
  356. struct nouveau_crtc *nv_crtc;
  357. int ret;
  358. nv_crtc = NULL;
  359. if (connector->encoder && connector->encoder->crtc)
  360. nv_crtc = nouveau_crtc(connector->encoder->crtc);
  361. /* Scaling mode */
  362. if (property == dev->mode_config.scaling_mode_property) {
  363. bool modeset = false;
  364. switch (value) {
  365. case DRM_MODE_SCALE_NONE:
  366. case DRM_MODE_SCALE_FULLSCREEN:
  367. case DRM_MODE_SCALE_CENTER:
  368. case DRM_MODE_SCALE_ASPECT:
  369. break;
  370. default:
  371. return -EINVAL;
  372. }
  373. /* LVDS always needs gpu scaling */
  374. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
  375. value == DRM_MODE_SCALE_NONE)
  376. return -EINVAL;
  377. /* Changing between GPU and panel scaling requires a full
  378. * modeset
  379. */
  380. if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
  381. (value == DRM_MODE_SCALE_NONE))
  382. modeset = true;
  383. nv_connector->scaling_mode = value;
  384. if (!nv_crtc)
  385. return 0;
  386. if (modeset || !nv_crtc->set_scale) {
  387. ret = drm_crtc_helper_set_mode(&nv_crtc->base,
  388. &nv_crtc->base.mode,
  389. nv_crtc->base.x,
  390. nv_crtc->base.y, NULL);
  391. if (!ret)
  392. return -EINVAL;
  393. } else {
  394. ret = nv_crtc->set_scale(nv_crtc, true);
  395. if (ret)
  396. return ret;
  397. }
  398. return 0;
  399. }
  400. /* Underscan */
  401. if (property == disp->underscan_property) {
  402. if (nv_connector->underscan != value) {
  403. nv_connector->underscan = value;
  404. if (!nv_crtc || !nv_crtc->set_scale)
  405. return 0;
  406. return nv_crtc->set_scale(nv_crtc, true);
  407. }
  408. return 0;
  409. }
  410. if (property == disp->underscan_hborder_property) {
  411. if (nv_connector->underscan_hborder != value) {
  412. nv_connector->underscan_hborder = value;
  413. if (!nv_crtc || !nv_crtc->set_scale)
  414. return 0;
  415. return nv_crtc->set_scale(nv_crtc, true);
  416. }
  417. return 0;
  418. }
  419. if (property == disp->underscan_vborder_property) {
  420. if (nv_connector->underscan_vborder != value) {
  421. nv_connector->underscan_vborder = value;
  422. if (!nv_crtc || !nv_crtc->set_scale)
  423. return 0;
  424. return nv_crtc->set_scale(nv_crtc, true);
  425. }
  426. return 0;
  427. }
  428. /* Dithering */
  429. if (property == disp->dithering_mode) {
  430. nv_connector->dithering_mode = value;
  431. if (!nv_crtc || !nv_crtc->set_dither)
  432. return 0;
  433. return nv_crtc->set_dither(nv_crtc, true);
  434. }
  435. if (property == disp->dithering_depth) {
  436. nv_connector->dithering_depth = value;
  437. if (!nv_crtc || !nv_crtc->set_dither)
  438. return 0;
  439. return nv_crtc->set_dither(nv_crtc, true);
  440. }
  441. if (nv_crtc && nv_crtc->set_color_vibrance) {
  442. /* Hue */
  443. if (property == disp->vibrant_hue_property) {
  444. nv_crtc->vibrant_hue = value - 90;
  445. return nv_crtc->set_color_vibrance(nv_crtc, true);
  446. }
  447. /* Saturation */
  448. if (property == disp->color_vibrance_property) {
  449. nv_crtc->color_vibrance = value - 100;
  450. return nv_crtc->set_color_vibrance(nv_crtc, true);
  451. }
  452. }
  453. if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
  454. return get_slave_funcs(encoder)->set_property(
  455. encoder, connector, property, value);
  456. return -EINVAL;
  457. }
  458. static struct drm_display_mode *
  459. nouveau_connector_native_mode(struct drm_connector *connector)
  460. {
  461. struct drm_connector_helper_funcs *helper = connector->helper_private;
  462. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  463. struct drm_device *dev = connector->dev;
  464. struct drm_display_mode *mode, *largest = NULL;
  465. int high_w = 0, high_h = 0, high_v = 0;
  466. list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
  467. mode->vrefresh = drm_mode_vrefresh(mode);
  468. if (helper->mode_valid(connector, mode) != MODE_OK ||
  469. (mode->flags & DRM_MODE_FLAG_INTERLACE))
  470. continue;
  471. /* Use preferred mode if there is one.. */
  472. if (mode->type & DRM_MODE_TYPE_PREFERRED) {
  473. NV_DEBUG_KMS(dev, "native mode from preferred\n");
  474. return drm_mode_duplicate(dev, mode);
  475. }
  476. /* Otherwise, take the resolution with the largest width, then
  477. * height, then vertical refresh
  478. */
  479. if (mode->hdisplay < high_w)
  480. continue;
  481. if (mode->hdisplay == high_w && mode->vdisplay < high_h)
  482. continue;
  483. if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
  484. mode->vrefresh < high_v)
  485. continue;
  486. high_w = mode->hdisplay;
  487. high_h = mode->vdisplay;
  488. high_v = mode->vrefresh;
  489. largest = mode;
  490. }
  491. NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
  492. high_w, high_h, high_v);
  493. return largest ? drm_mode_duplicate(dev, largest) : NULL;
  494. }
  495. struct moderec {
  496. int hdisplay;
  497. int vdisplay;
  498. };
  499. static struct moderec scaler_modes[] = {
  500. { 1920, 1200 },
  501. { 1920, 1080 },
  502. { 1680, 1050 },
  503. { 1600, 1200 },
  504. { 1400, 1050 },
  505. { 1280, 1024 },
  506. { 1280, 960 },
  507. { 1152, 864 },
  508. { 1024, 768 },
  509. { 800, 600 },
  510. { 720, 400 },
  511. { 640, 480 },
  512. { 640, 400 },
  513. { 640, 350 },
  514. {}
  515. };
  516. static int
  517. nouveau_connector_scaler_modes_add(struct drm_connector *connector)
  518. {
  519. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  520. struct drm_display_mode *native = nv_connector->native_mode, *m;
  521. struct drm_device *dev = connector->dev;
  522. struct moderec *mode = &scaler_modes[0];
  523. int modes = 0;
  524. if (!native)
  525. return 0;
  526. while (mode->hdisplay) {
  527. if (mode->hdisplay <= native->hdisplay &&
  528. mode->vdisplay <= native->vdisplay) {
  529. m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
  530. drm_mode_vrefresh(native), false,
  531. false, false);
  532. if (!m)
  533. continue;
  534. m->type |= DRM_MODE_TYPE_DRIVER;
  535. drm_mode_probed_add(connector, m);
  536. modes++;
  537. }
  538. mode++;
  539. }
  540. return modes;
  541. }
  542. static void
  543. nouveau_connector_detect_depth(struct drm_connector *connector)
  544. {
  545. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  546. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  547. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  548. struct nvbios *bios = &dev_priv->vbios;
  549. struct drm_display_mode *mode = nv_connector->native_mode;
  550. bool duallink;
  551. /* if the edid is feeling nice enough to provide this info, use it */
  552. if (nv_connector->edid && connector->display_info.bpc)
  553. return;
  554. /* if not, we're out of options unless we're LVDS, default to 8bpc */
  555. if (nv_encoder->dcb->type != OUTPUT_LVDS) {
  556. connector->display_info.bpc = 8;
  557. return;
  558. }
  559. connector->display_info.bpc = 6;
  560. /* LVDS: panel straps */
  561. if (bios->fp_no_ddc) {
  562. if (bios->fp.if_is_24bit)
  563. connector->display_info.bpc = 8;
  564. return;
  565. }
  566. /* LVDS: DDC panel, need to first determine the number of links to
  567. * know which if_is_24bit flag to check...
  568. */
  569. if (nv_connector->edid &&
  570. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
  571. duallink = ((u8 *)nv_connector->edid)[121] == 2;
  572. else
  573. duallink = mode->clock >= bios->fp.duallink_transition_clk;
  574. if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
  575. ( duallink && (bios->fp.strapless_is_24bit & 2)))
  576. connector->display_info.bpc = 8;
  577. }
  578. static int
  579. nouveau_connector_get_modes(struct drm_connector *connector)
  580. {
  581. struct drm_device *dev = connector->dev;
  582. struct drm_nouveau_private *dev_priv = dev->dev_private;
  583. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  584. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  585. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  586. int ret = 0;
  587. /* destroy the native mode, the attached monitor could have changed.
  588. */
  589. if (nv_connector->native_mode) {
  590. drm_mode_destroy(dev, nv_connector->native_mode);
  591. nv_connector->native_mode = NULL;
  592. }
  593. if (nv_connector->edid)
  594. ret = drm_add_edid_modes(connector, nv_connector->edid);
  595. else
  596. if (nv_encoder->dcb->type == OUTPUT_LVDS &&
  597. (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
  598. dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
  599. struct drm_display_mode mode;
  600. nouveau_bios_fp_mode(dev, &mode);
  601. nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
  602. }
  603. /* Determine display colour depth for everything except LVDS now,
  604. * DP requires this before mode_valid() is called.
  605. */
  606. if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
  607. nouveau_connector_detect_depth(connector);
  608. /* Find the native mode if this is a digital panel, if we didn't
  609. * find any modes through DDC previously add the native mode to
  610. * the list of modes.
  611. */
  612. if (!nv_connector->native_mode)
  613. nv_connector->native_mode =
  614. nouveau_connector_native_mode(connector);
  615. if (ret == 0 && nv_connector->native_mode) {
  616. struct drm_display_mode *mode;
  617. mode = drm_mode_duplicate(dev, nv_connector->native_mode);
  618. drm_mode_probed_add(connector, mode);
  619. ret = 1;
  620. }
  621. /* Determine LVDS colour depth, must happen after determining
  622. * "native" mode as some VBIOS tables require us to use the
  623. * pixel clock as part of the lookup...
  624. */
  625. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
  626. nouveau_connector_detect_depth(connector);
  627. if (nv_encoder->dcb->type == OUTPUT_TV)
  628. ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
  629. if (nv_connector->type == DCB_CONNECTOR_LVDS ||
  630. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
  631. nv_connector->type == DCB_CONNECTOR_eDP)
  632. ret += nouveau_connector_scaler_modes_add(connector);
  633. return ret;
  634. }
  635. static unsigned
  636. get_tmds_link_bandwidth(struct drm_connector *connector)
  637. {
  638. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  639. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  640. struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
  641. if (dcb->location != DCB_LOC_ON_CHIP ||
  642. dev_priv->chipset >= 0x46)
  643. return 165000;
  644. else if (dev_priv->chipset >= 0x40)
  645. return 155000;
  646. else if (dev_priv->chipset >= 0x18)
  647. return 135000;
  648. else
  649. return 112000;
  650. }
  651. static int
  652. nouveau_connector_mode_valid(struct drm_connector *connector,
  653. struct drm_display_mode *mode)
  654. {
  655. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  656. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  657. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  658. unsigned min_clock = 25000, max_clock = min_clock;
  659. unsigned clock = mode->clock;
  660. switch (nv_encoder->dcb->type) {
  661. case OUTPUT_LVDS:
  662. if (nv_connector->native_mode &&
  663. (mode->hdisplay > nv_connector->native_mode->hdisplay ||
  664. mode->vdisplay > nv_connector->native_mode->vdisplay))
  665. return MODE_PANEL;
  666. min_clock = 0;
  667. max_clock = 400000;
  668. break;
  669. case OUTPUT_TMDS:
  670. max_clock = get_tmds_link_bandwidth(connector);
  671. if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
  672. max_clock *= 2;
  673. break;
  674. case OUTPUT_ANALOG:
  675. max_clock = nv_encoder->dcb->crtconf.maxfreq;
  676. if (!max_clock)
  677. max_clock = 350000;
  678. break;
  679. case OUTPUT_TV:
  680. return get_slave_funcs(encoder)->mode_valid(encoder, mode);
  681. case OUTPUT_DP:
  682. max_clock = nv_encoder->dp.link_nr;
  683. max_clock *= nv_encoder->dp.link_bw;
  684. clock = clock * (connector->display_info.bpc * 3) / 10;
  685. break;
  686. default:
  687. BUG_ON(1);
  688. return MODE_BAD;
  689. }
  690. if (clock < min_clock)
  691. return MODE_CLOCK_LOW;
  692. if (clock > max_clock)
  693. return MODE_CLOCK_HIGH;
  694. return MODE_OK;
  695. }
  696. static struct drm_encoder *
  697. nouveau_connector_best_encoder(struct drm_connector *connector)
  698. {
  699. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  700. if (nv_connector->detected_encoder)
  701. return to_drm_encoder(nv_connector->detected_encoder);
  702. return NULL;
  703. }
  704. static const struct drm_connector_helper_funcs
  705. nouveau_connector_helper_funcs = {
  706. .get_modes = nouveau_connector_get_modes,
  707. .mode_valid = nouveau_connector_mode_valid,
  708. .best_encoder = nouveau_connector_best_encoder,
  709. };
  710. static const struct drm_connector_funcs
  711. nouveau_connector_funcs = {
  712. .dpms = drm_helper_connector_dpms,
  713. .save = NULL,
  714. .restore = NULL,
  715. .detect = nouveau_connector_detect,
  716. .destroy = nouveau_connector_destroy,
  717. .fill_modes = drm_helper_probe_single_connector_modes,
  718. .set_property = nouveau_connector_set_property,
  719. .force = nouveau_connector_force
  720. };
  721. static const struct drm_connector_funcs
  722. nouveau_connector_funcs_lvds = {
  723. .dpms = drm_helper_connector_dpms,
  724. .save = NULL,
  725. .restore = NULL,
  726. .detect = nouveau_connector_detect_lvds,
  727. .destroy = nouveau_connector_destroy,
  728. .fill_modes = drm_helper_probe_single_connector_modes,
  729. .set_property = nouveau_connector_set_property,
  730. .force = nouveau_connector_force
  731. };
  732. static int
  733. drm_conntype_from_dcb(enum dcb_connector_type dcb)
  734. {
  735. switch (dcb) {
  736. case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
  737. case DCB_CONNECTOR_TV_0 :
  738. case DCB_CONNECTOR_TV_1 :
  739. case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
  740. case DCB_CONNECTOR_DMS59_0 :
  741. case DCB_CONNECTOR_DMS59_1 :
  742. case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
  743. case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
  744. case DCB_CONNECTOR_LVDS :
  745. case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
  746. case DCB_CONNECTOR_DMS59_DP0:
  747. case DCB_CONNECTOR_DMS59_DP1:
  748. case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort;
  749. case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
  750. case DCB_CONNECTOR_HDMI_0 :
  751. case DCB_CONNECTOR_HDMI_1 : return DRM_MODE_CONNECTOR_HDMIA;
  752. default:
  753. break;
  754. }
  755. return DRM_MODE_CONNECTOR_Unknown;
  756. }
  757. struct drm_connector *
  758. nouveau_connector_create(struct drm_device *dev, int index)
  759. {
  760. const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
  761. struct drm_nouveau_private *dev_priv = dev->dev_private;
  762. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  763. struct nouveau_connector *nv_connector = NULL;
  764. struct drm_connector *connector;
  765. int type, ret = 0;
  766. bool dummy;
  767. NV_DEBUG_KMS(dev, "\n");
  768. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  769. nv_connector = nouveau_connector(connector);
  770. if (nv_connector->index == index)
  771. return connector;
  772. }
  773. nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
  774. if (!nv_connector)
  775. return ERR_PTR(-ENOMEM);
  776. connector = &nv_connector->base;
  777. nv_connector->index = index;
  778. /* attempt to parse vbios connector type and hotplug gpio */
  779. nv_connector->dcb = dcb_conn(dev, index);
  780. if (nv_connector->dcb) {
  781. static const u8 hpd[16] = {
  782. 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
  783. 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
  784. };
  785. u32 entry = ROM16(nv_connector->dcb[0]);
  786. if (dcb_conntab(dev)[3] >= 4)
  787. entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
  788. nv_connector->hpd = ffs((entry & 0x07033000) >> 12);
  789. nv_connector->hpd = hpd[nv_connector->hpd];
  790. nv_connector->type = nv_connector->dcb[0];
  791. if (drm_conntype_from_dcb(nv_connector->type) ==
  792. DRM_MODE_CONNECTOR_Unknown) {
  793. NV_WARN(dev, "unknown connector type %02x\n",
  794. nv_connector->type);
  795. nv_connector->type = DCB_CONNECTOR_NONE;
  796. }
  797. /* Gigabyte NX85T */
  798. if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
  799. if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
  800. nv_connector->type = DCB_CONNECTOR_DVI_I;
  801. }
  802. /* Gigabyte GV-NX86T512H */
  803. if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
  804. if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
  805. nv_connector->type = DCB_CONNECTOR_DVI_I;
  806. }
  807. } else {
  808. nv_connector->type = DCB_CONNECTOR_NONE;
  809. nv_connector->hpd = DCB_GPIO_UNUSED;
  810. }
  811. /* no vbios data, or an unknown dcb connector type - attempt to
  812. * figure out something suitable ourselves
  813. */
  814. if (nv_connector->type == DCB_CONNECTOR_NONE) {
  815. struct drm_nouveau_private *dev_priv = dev->dev_private;
  816. struct dcb_table *dcbt = &dev_priv->vbios.dcb;
  817. u32 encoders = 0;
  818. int i;
  819. for (i = 0; i < dcbt->entries; i++) {
  820. if (dcbt->entry[i].connector == nv_connector->index)
  821. encoders |= (1 << dcbt->entry[i].type);
  822. }
  823. if (encoders & (1 << OUTPUT_DP)) {
  824. if (encoders & (1 << OUTPUT_TMDS))
  825. nv_connector->type = DCB_CONNECTOR_DP;
  826. else
  827. nv_connector->type = DCB_CONNECTOR_eDP;
  828. } else
  829. if (encoders & (1 << OUTPUT_TMDS)) {
  830. if (encoders & (1 << OUTPUT_ANALOG))
  831. nv_connector->type = DCB_CONNECTOR_DVI_I;
  832. else
  833. nv_connector->type = DCB_CONNECTOR_DVI_D;
  834. } else
  835. if (encoders & (1 << OUTPUT_ANALOG)) {
  836. nv_connector->type = DCB_CONNECTOR_VGA;
  837. } else
  838. if (encoders & (1 << OUTPUT_LVDS)) {
  839. nv_connector->type = DCB_CONNECTOR_LVDS;
  840. } else
  841. if (encoders & (1 << OUTPUT_TV)) {
  842. nv_connector->type = DCB_CONNECTOR_TV_0;
  843. }
  844. }
  845. type = drm_conntype_from_dcb(nv_connector->type);
  846. if (type == DRM_MODE_CONNECTOR_LVDS) {
  847. ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
  848. if (ret) {
  849. NV_ERROR(dev, "Error parsing LVDS table, disabling\n");
  850. kfree(nv_connector);
  851. return ERR_PTR(ret);
  852. }
  853. funcs = &nouveau_connector_funcs_lvds;
  854. } else {
  855. funcs = &nouveau_connector_funcs;
  856. }
  857. /* defaults, will get overridden in detect() */
  858. connector->interlace_allowed = false;
  859. connector->doublescan_allowed = false;
  860. drm_connector_init(dev, connector, funcs, type);
  861. drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
  862. /* Init DVI-I specific properties */
  863. if (nv_connector->type == DCB_CONNECTOR_DVI_I)
  864. drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
  865. /* Add overscan compensation options to digital outputs */
  866. if (disp->underscan_property &&
  867. (type == DRM_MODE_CONNECTOR_DVID ||
  868. type == DRM_MODE_CONNECTOR_DVII ||
  869. type == DRM_MODE_CONNECTOR_HDMIA ||
  870. type == DRM_MODE_CONNECTOR_DisplayPort)) {
  871. drm_connector_attach_property(connector,
  872. disp->underscan_property,
  873. UNDERSCAN_OFF);
  874. drm_connector_attach_property(connector,
  875. disp->underscan_hborder_property,
  876. 0);
  877. drm_connector_attach_property(connector,
  878. disp->underscan_vborder_property,
  879. 0);
  880. }
  881. /* Add hue and saturation options */
  882. if (disp->vibrant_hue_property)
  883. drm_connector_attach_property(connector,
  884. disp->vibrant_hue_property,
  885. 90);
  886. if (disp->color_vibrance_property)
  887. drm_connector_attach_property(connector,
  888. disp->color_vibrance_property,
  889. 150);
  890. switch (nv_connector->type) {
  891. case DCB_CONNECTOR_VGA:
  892. if (dev_priv->card_type >= NV_50) {
  893. drm_connector_attach_property(connector,
  894. dev->mode_config.scaling_mode_property,
  895. nv_connector->scaling_mode);
  896. }
  897. /* fall-through */
  898. case DCB_CONNECTOR_TV_0:
  899. case DCB_CONNECTOR_TV_1:
  900. case DCB_CONNECTOR_TV_3:
  901. nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
  902. break;
  903. default:
  904. nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
  905. drm_connector_attach_property(connector,
  906. dev->mode_config.scaling_mode_property,
  907. nv_connector->scaling_mode);
  908. if (disp->dithering_mode) {
  909. nv_connector->dithering_mode = DITHERING_MODE_AUTO;
  910. drm_connector_attach_property(connector,
  911. disp->dithering_mode,
  912. nv_connector->dithering_mode);
  913. }
  914. if (disp->dithering_depth) {
  915. nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
  916. drm_connector_attach_property(connector,
  917. disp->dithering_depth,
  918. nv_connector->dithering_depth);
  919. }
  920. break;
  921. }
  922. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  923. if (nv_connector->hpd != DCB_GPIO_UNUSED) {
  924. ret = nouveau_gpio_isr_add(dev, 0, nv_connector->hpd, 0xff,
  925. nouveau_connector_hotplug,
  926. connector);
  927. if (ret == 0)
  928. connector->polled = DRM_CONNECTOR_POLL_HPD;
  929. }
  930. drm_sysfs_connector_add(connector);
  931. return connector;
  932. }
  933. static void
  934. nouveau_connector_hotplug(void *data, int plugged)
  935. {
  936. struct drm_connector *connector = data;
  937. struct drm_device *dev = connector->dev;
  938. NV_DEBUG(dev, "%splugged %s\n", plugged ? "" : "un",
  939. drm_get_connector_name(connector));
  940. if (plugged)
  941. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  942. else
  943. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  944. drm_helper_hpd_irq_event(dev);
  945. }