nouveau_connector.c 33 KB

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