nouveau_connector.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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.func != DCB_GPIO_UNUSED) {
  94. gpio->isr_del(gpio, 0, nv_connector->hpd.func, 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 =
  344. kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  345. if (nv_connector->edid)
  346. status = connector_status_connected;
  347. }
  348. }
  349. out:
  350. #if defined(CONFIG_ACPI_BUTTON) || \
  351. (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
  352. if (status == connector_status_connected &&
  353. !nouveau_ignorelid && !acpi_lid_open())
  354. status = connector_status_unknown;
  355. #endif
  356. drm_mode_connector_update_edid_property(connector, nv_connector->edid);
  357. nouveau_connector_set_encoder(connector, nv_encoder);
  358. return status;
  359. }
  360. static void
  361. nouveau_connector_force(struct drm_connector *connector)
  362. {
  363. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  364. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  365. struct nouveau_encoder *nv_encoder;
  366. int type;
  367. if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
  368. if (connector->force == DRM_FORCE_ON_DIGITAL)
  369. type = DCB_OUTPUT_TMDS;
  370. else
  371. type = DCB_OUTPUT_ANALOG;
  372. } else
  373. type = DCB_OUTPUT_ANY;
  374. nv_encoder = find_encoder(connector, type);
  375. if (!nv_encoder) {
  376. NV_ERROR(drm, "can't find encoder to force %s on!\n",
  377. drm_get_connector_name(connector));
  378. connector->status = connector_status_disconnected;
  379. return;
  380. }
  381. nouveau_connector_set_encoder(connector, nv_encoder);
  382. }
  383. static int
  384. nouveau_connector_set_property(struct drm_connector *connector,
  385. struct drm_property *property, uint64_t value)
  386. {
  387. struct nouveau_display *disp = nouveau_display(connector->dev);
  388. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  389. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  390. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  391. struct drm_device *dev = connector->dev;
  392. struct nouveau_crtc *nv_crtc;
  393. int ret;
  394. nv_crtc = NULL;
  395. if (connector->encoder && connector->encoder->crtc)
  396. nv_crtc = nouveau_crtc(connector->encoder->crtc);
  397. /* Scaling mode */
  398. if (property == dev->mode_config.scaling_mode_property) {
  399. bool modeset = false;
  400. switch (value) {
  401. case DRM_MODE_SCALE_NONE:
  402. case DRM_MODE_SCALE_FULLSCREEN:
  403. case DRM_MODE_SCALE_CENTER:
  404. case DRM_MODE_SCALE_ASPECT:
  405. break;
  406. default:
  407. return -EINVAL;
  408. }
  409. /* LVDS always needs gpu scaling */
  410. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
  411. value == DRM_MODE_SCALE_NONE)
  412. return -EINVAL;
  413. /* Changing between GPU and panel scaling requires a full
  414. * modeset
  415. */
  416. if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
  417. (value == DRM_MODE_SCALE_NONE))
  418. modeset = true;
  419. nv_connector->scaling_mode = value;
  420. if (!nv_crtc)
  421. return 0;
  422. if (modeset || !nv_crtc->set_scale) {
  423. ret = drm_crtc_helper_set_mode(&nv_crtc->base,
  424. &nv_crtc->base.mode,
  425. nv_crtc->base.x,
  426. nv_crtc->base.y, NULL);
  427. if (!ret)
  428. return -EINVAL;
  429. } else {
  430. ret = nv_crtc->set_scale(nv_crtc, true);
  431. if (ret)
  432. return ret;
  433. }
  434. return 0;
  435. }
  436. /* Underscan */
  437. if (property == disp->underscan_property) {
  438. if (nv_connector->underscan != value) {
  439. nv_connector->underscan = value;
  440. if (!nv_crtc || !nv_crtc->set_scale)
  441. return 0;
  442. return nv_crtc->set_scale(nv_crtc, true);
  443. }
  444. return 0;
  445. }
  446. if (property == disp->underscan_hborder_property) {
  447. if (nv_connector->underscan_hborder != value) {
  448. nv_connector->underscan_hborder = value;
  449. if (!nv_crtc || !nv_crtc->set_scale)
  450. return 0;
  451. return nv_crtc->set_scale(nv_crtc, true);
  452. }
  453. return 0;
  454. }
  455. if (property == disp->underscan_vborder_property) {
  456. if (nv_connector->underscan_vborder != value) {
  457. nv_connector->underscan_vborder = value;
  458. if (!nv_crtc || !nv_crtc->set_scale)
  459. return 0;
  460. return nv_crtc->set_scale(nv_crtc, true);
  461. }
  462. return 0;
  463. }
  464. /* Dithering */
  465. if (property == disp->dithering_mode) {
  466. nv_connector->dithering_mode = value;
  467. if (!nv_crtc || !nv_crtc->set_dither)
  468. return 0;
  469. return nv_crtc->set_dither(nv_crtc, true);
  470. }
  471. if (property == disp->dithering_depth) {
  472. nv_connector->dithering_depth = value;
  473. if (!nv_crtc || !nv_crtc->set_dither)
  474. return 0;
  475. return nv_crtc->set_dither(nv_crtc, true);
  476. }
  477. if (nv_crtc && nv_crtc->set_color_vibrance) {
  478. /* Hue */
  479. if (property == disp->vibrant_hue_property) {
  480. nv_crtc->vibrant_hue = value - 90;
  481. return nv_crtc->set_color_vibrance(nv_crtc, true);
  482. }
  483. /* Saturation */
  484. if (property == disp->color_vibrance_property) {
  485. nv_crtc->color_vibrance = value - 100;
  486. return nv_crtc->set_color_vibrance(nv_crtc, true);
  487. }
  488. }
  489. if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
  490. return get_slave_funcs(encoder)->set_property(
  491. encoder, connector, property, value);
  492. return -EINVAL;
  493. }
  494. static struct drm_display_mode *
  495. nouveau_connector_native_mode(struct drm_connector *connector)
  496. {
  497. struct drm_connector_helper_funcs *helper = connector->helper_private;
  498. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  499. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  500. struct drm_device *dev = connector->dev;
  501. struct drm_display_mode *mode, *largest = NULL;
  502. int high_w = 0, high_h = 0, high_v = 0;
  503. list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
  504. mode->vrefresh = drm_mode_vrefresh(mode);
  505. if (helper->mode_valid(connector, mode) != MODE_OK ||
  506. (mode->flags & DRM_MODE_FLAG_INTERLACE))
  507. continue;
  508. /* Use preferred mode if there is one.. */
  509. if (mode->type & DRM_MODE_TYPE_PREFERRED) {
  510. NV_DEBUG(drm, "native mode from preferred\n");
  511. return drm_mode_duplicate(dev, mode);
  512. }
  513. /* Otherwise, take the resolution with the largest width, then
  514. * height, then vertical refresh
  515. */
  516. if (mode->hdisplay < high_w)
  517. continue;
  518. if (mode->hdisplay == high_w && mode->vdisplay < high_h)
  519. continue;
  520. if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
  521. mode->vrefresh < high_v)
  522. continue;
  523. high_w = mode->hdisplay;
  524. high_h = mode->vdisplay;
  525. high_v = mode->vrefresh;
  526. largest = mode;
  527. }
  528. NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n",
  529. high_w, high_h, high_v);
  530. return largest ? drm_mode_duplicate(dev, largest) : NULL;
  531. }
  532. struct moderec {
  533. int hdisplay;
  534. int vdisplay;
  535. };
  536. static struct moderec scaler_modes[] = {
  537. { 1920, 1200 },
  538. { 1920, 1080 },
  539. { 1680, 1050 },
  540. { 1600, 1200 },
  541. { 1400, 1050 },
  542. { 1280, 1024 },
  543. { 1280, 960 },
  544. { 1152, 864 },
  545. { 1024, 768 },
  546. { 800, 600 },
  547. { 720, 400 },
  548. { 640, 480 },
  549. { 640, 400 },
  550. { 640, 350 },
  551. {}
  552. };
  553. static int
  554. nouveau_connector_scaler_modes_add(struct drm_connector *connector)
  555. {
  556. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  557. struct drm_display_mode *native = nv_connector->native_mode, *m;
  558. struct drm_device *dev = connector->dev;
  559. struct moderec *mode = &scaler_modes[0];
  560. int modes = 0;
  561. if (!native)
  562. return 0;
  563. while (mode->hdisplay) {
  564. if (mode->hdisplay <= native->hdisplay &&
  565. mode->vdisplay <= native->vdisplay) {
  566. m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
  567. drm_mode_vrefresh(native), false,
  568. false, false);
  569. if (!m)
  570. continue;
  571. m->type |= DRM_MODE_TYPE_DRIVER;
  572. drm_mode_probed_add(connector, m);
  573. modes++;
  574. }
  575. mode++;
  576. }
  577. return modes;
  578. }
  579. static void
  580. nouveau_connector_detect_depth(struct drm_connector *connector)
  581. {
  582. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  583. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  584. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  585. struct nvbios *bios = &drm->vbios;
  586. struct drm_display_mode *mode = nv_connector->native_mode;
  587. bool duallink;
  588. /* if the edid is feeling nice enough to provide this info, use it */
  589. if (nv_connector->edid && connector->display_info.bpc)
  590. return;
  591. /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
  592. if (nv_connector->type == DCB_CONNECTOR_eDP) {
  593. connector->display_info.bpc = 6;
  594. return;
  595. }
  596. /* we're out of options unless we're LVDS, default to 8bpc */
  597. if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
  598. connector->display_info.bpc = 8;
  599. return;
  600. }
  601. connector->display_info.bpc = 6;
  602. /* LVDS: panel straps */
  603. if (bios->fp_no_ddc) {
  604. if (bios->fp.if_is_24bit)
  605. connector->display_info.bpc = 8;
  606. return;
  607. }
  608. /* LVDS: DDC panel, need to first determine the number of links to
  609. * know which if_is_24bit flag to check...
  610. */
  611. if (nv_connector->edid &&
  612. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
  613. duallink = ((u8 *)nv_connector->edid)[121] == 2;
  614. else
  615. duallink = mode->clock >= bios->fp.duallink_transition_clk;
  616. if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
  617. ( duallink && (bios->fp.strapless_is_24bit & 2)))
  618. connector->display_info.bpc = 8;
  619. }
  620. static int
  621. nouveau_connector_get_modes(struct drm_connector *connector)
  622. {
  623. struct drm_device *dev = connector->dev;
  624. struct nouveau_drm *drm = nouveau_drm(dev);
  625. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  626. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  627. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  628. int ret = 0;
  629. /* destroy the native mode, the attached monitor could have changed.
  630. */
  631. if (nv_connector->native_mode) {
  632. drm_mode_destroy(dev, nv_connector->native_mode);
  633. nv_connector->native_mode = NULL;
  634. }
  635. if (nv_connector->edid)
  636. ret = drm_add_edid_modes(connector, nv_connector->edid);
  637. else
  638. if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
  639. (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
  640. drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
  641. struct drm_display_mode mode;
  642. nouveau_bios_fp_mode(dev, &mode);
  643. nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
  644. }
  645. /* Determine display colour depth for everything except LVDS now,
  646. * DP requires this before mode_valid() is called.
  647. */
  648. if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
  649. nouveau_connector_detect_depth(connector);
  650. /* Find the native mode if this is a digital panel, if we didn't
  651. * find any modes through DDC previously add the native mode to
  652. * the list of modes.
  653. */
  654. if (!nv_connector->native_mode)
  655. nv_connector->native_mode =
  656. nouveau_connector_native_mode(connector);
  657. if (ret == 0 && nv_connector->native_mode) {
  658. struct drm_display_mode *mode;
  659. mode = drm_mode_duplicate(dev, nv_connector->native_mode);
  660. drm_mode_probed_add(connector, mode);
  661. ret = 1;
  662. }
  663. /* Determine LVDS colour depth, must happen after determining
  664. * "native" mode as some VBIOS tables require us to use the
  665. * pixel clock as part of the lookup...
  666. */
  667. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
  668. nouveau_connector_detect_depth(connector);
  669. if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
  670. ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
  671. if (nv_connector->type == DCB_CONNECTOR_LVDS ||
  672. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
  673. nv_connector->type == DCB_CONNECTOR_eDP)
  674. ret += nouveau_connector_scaler_modes_add(connector);
  675. return ret;
  676. }
  677. static unsigned
  678. get_tmds_link_bandwidth(struct drm_connector *connector)
  679. {
  680. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  681. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  682. struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
  683. if (dcb->location != DCB_LOC_ON_CHIP ||
  684. nv_device(drm->device)->chipset >= 0x46)
  685. return 165000;
  686. else if (nv_device(drm->device)->chipset >= 0x40)
  687. return 155000;
  688. else if (nv_device(drm->device)->chipset >= 0x18)
  689. return 135000;
  690. else
  691. return 112000;
  692. }
  693. static int
  694. nouveau_connector_mode_valid(struct drm_connector *connector,
  695. struct drm_display_mode *mode)
  696. {
  697. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  698. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  699. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  700. unsigned min_clock = 25000, max_clock = min_clock;
  701. unsigned clock = mode->clock;
  702. switch (nv_encoder->dcb->type) {
  703. case DCB_OUTPUT_LVDS:
  704. if (nv_connector->native_mode &&
  705. (mode->hdisplay > nv_connector->native_mode->hdisplay ||
  706. mode->vdisplay > nv_connector->native_mode->vdisplay))
  707. return MODE_PANEL;
  708. min_clock = 0;
  709. max_clock = 400000;
  710. break;
  711. case DCB_OUTPUT_TMDS:
  712. max_clock = get_tmds_link_bandwidth(connector);
  713. if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
  714. max_clock *= 2;
  715. break;
  716. case DCB_OUTPUT_ANALOG:
  717. max_clock = nv_encoder->dcb->crtconf.maxfreq;
  718. if (!max_clock)
  719. max_clock = 350000;
  720. break;
  721. case DCB_OUTPUT_TV:
  722. return get_slave_funcs(encoder)->mode_valid(encoder, mode);
  723. case DCB_OUTPUT_DP:
  724. max_clock = nv_encoder->dp.link_nr;
  725. max_clock *= nv_encoder->dp.link_bw;
  726. clock = clock * (connector->display_info.bpc * 3) / 10;
  727. break;
  728. default:
  729. BUG_ON(1);
  730. return MODE_BAD;
  731. }
  732. if (clock < min_clock)
  733. return MODE_CLOCK_LOW;
  734. if (clock > max_clock)
  735. return MODE_CLOCK_HIGH;
  736. return MODE_OK;
  737. }
  738. static struct drm_encoder *
  739. nouveau_connector_best_encoder(struct drm_connector *connector)
  740. {
  741. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  742. if (nv_connector->detected_encoder)
  743. return to_drm_encoder(nv_connector->detected_encoder);
  744. return NULL;
  745. }
  746. static const struct drm_connector_helper_funcs
  747. nouveau_connector_helper_funcs = {
  748. .get_modes = nouveau_connector_get_modes,
  749. .mode_valid = nouveau_connector_mode_valid,
  750. .best_encoder = nouveau_connector_best_encoder,
  751. };
  752. static const struct drm_connector_funcs
  753. nouveau_connector_funcs = {
  754. .dpms = drm_helper_connector_dpms,
  755. .save = NULL,
  756. .restore = NULL,
  757. .detect = nouveau_connector_detect,
  758. .destroy = nouveau_connector_destroy,
  759. .fill_modes = drm_helper_probe_single_connector_modes,
  760. .set_property = nouveau_connector_set_property,
  761. .force = nouveau_connector_force
  762. };
  763. static const struct drm_connector_funcs
  764. nouveau_connector_funcs_lvds = {
  765. .dpms = drm_helper_connector_dpms,
  766. .save = NULL,
  767. .restore = NULL,
  768. .detect = nouveau_connector_detect_lvds,
  769. .destroy = nouveau_connector_destroy,
  770. .fill_modes = drm_helper_probe_single_connector_modes,
  771. .set_property = nouveau_connector_set_property,
  772. .force = nouveau_connector_force
  773. };
  774. static int
  775. drm_conntype_from_dcb(enum dcb_connector_type dcb)
  776. {
  777. switch (dcb) {
  778. case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
  779. case DCB_CONNECTOR_TV_0 :
  780. case DCB_CONNECTOR_TV_1 :
  781. case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
  782. case DCB_CONNECTOR_DMS59_0 :
  783. case DCB_CONNECTOR_DMS59_1 :
  784. case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
  785. case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
  786. case DCB_CONNECTOR_LVDS :
  787. case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
  788. case DCB_CONNECTOR_DMS59_DP0:
  789. case DCB_CONNECTOR_DMS59_DP1:
  790. case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort;
  791. case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
  792. case DCB_CONNECTOR_HDMI_0 :
  793. case DCB_CONNECTOR_HDMI_1 : return DRM_MODE_CONNECTOR_HDMIA;
  794. default:
  795. break;
  796. }
  797. return DRM_MODE_CONNECTOR_Unknown;
  798. }
  799. struct drm_connector *
  800. nouveau_connector_create(struct drm_device *dev, int index)
  801. {
  802. const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
  803. struct nouveau_drm *drm = nouveau_drm(dev);
  804. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  805. struct nouveau_display *disp = nouveau_display(dev);
  806. struct nouveau_connector *nv_connector = NULL;
  807. struct drm_connector *connector;
  808. int type, ret = 0;
  809. bool dummy;
  810. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  811. nv_connector = nouveau_connector(connector);
  812. if (nv_connector->index == index)
  813. return connector;
  814. }
  815. nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
  816. if (!nv_connector)
  817. return ERR_PTR(-ENOMEM);
  818. connector = &nv_connector->base;
  819. nv_connector->index = index;
  820. /* attempt to parse vbios connector type and hotplug gpio */
  821. nv_connector->dcb = olddcb_conn(dev, index);
  822. if (nv_connector->dcb) {
  823. static const u8 hpd[16] = {
  824. 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
  825. 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
  826. };
  827. u32 entry = ROM16(nv_connector->dcb[0]);
  828. if (olddcb_conntab(dev)[3] >= 4)
  829. entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
  830. ret = gpio->find(gpio, 0, hpd[ffs((entry & 0x07033000) >> 12)],
  831. DCB_GPIO_UNUSED, &nv_connector->hpd);
  832. if (ret)
  833. nv_connector->hpd.func = DCB_GPIO_UNUSED;
  834. nv_connector->type = nv_connector->dcb[0];
  835. if (drm_conntype_from_dcb(nv_connector->type) ==
  836. DRM_MODE_CONNECTOR_Unknown) {
  837. NV_WARN(drm, "unknown connector type %02x\n",
  838. nv_connector->type);
  839. nv_connector->type = DCB_CONNECTOR_NONE;
  840. }
  841. /* Gigabyte NX85T */
  842. if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
  843. if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
  844. nv_connector->type = DCB_CONNECTOR_DVI_I;
  845. }
  846. /* Gigabyte GV-NX86T512H */
  847. if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
  848. if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
  849. nv_connector->type = DCB_CONNECTOR_DVI_I;
  850. }
  851. } else {
  852. nv_connector->type = DCB_CONNECTOR_NONE;
  853. nv_connector->hpd.func = DCB_GPIO_UNUSED;
  854. }
  855. /* no vbios data, or an unknown dcb connector type - attempt to
  856. * figure out something suitable ourselves
  857. */
  858. if (nv_connector->type == DCB_CONNECTOR_NONE) {
  859. struct nouveau_drm *drm = nouveau_drm(dev);
  860. struct dcb_table *dcbt = &drm->vbios.dcb;
  861. u32 encoders = 0;
  862. int i;
  863. for (i = 0; i < dcbt->entries; i++) {
  864. if (dcbt->entry[i].connector == nv_connector->index)
  865. encoders |= (1 << dcbt->entry[i].type);
  866. }
  867. if (encoders & (1 << DCB_OUTPUT_DP)) {
  868. if (encoders & (1 << DCB_OUTPUT_TMDS))
  869. nv_connector->type = DCB_CONNECTOR_DP;
  870. else
  871. nv_connector->type = DCB_CONNECTOR_eDP;
  872. } else
  873. if (encoders & (1 << DCB_OUTPUT_TMDS)) {
  874. if (encoders & (1 << DCB_OUTPUT_ANALOG))
  875. nv_connector->type = DCB_CONNECTOR_DVI_I;
  876. else
  877. nv_connector->type = DCB_CONNECTOR_DVI_D;
  878. } else
  879. if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
  880. nv_connector->type = DCB_CONNECTOR_VGA;
  881. } else
  882. if (encoders & (1 << DCB_OUTPUT_LVDS)) {
  883. nv_connector->type = DCB_CONNECTOR_LVDS;
  884. } else
  885. if (encoders & (1 << DCB_OUTPUT_TV)) {
  886. nv_connector->type = DCB_CONNECTOR_TV_0;
  887. }
  888. }
  889. type = drm_conntype_from_dcb(nv_connector->type);
  890. if (type == DRM_MODE_CONNECTOR_LVDS) {
  891. ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
  892. if (ret) {
  893. NV_ERROR(drm, "Error parsing LVDS table, disabling\n");
  894. kfree(nv_connector);
  895. return ERR_PTR(ret);
  896. }
  897. funcs = &nouveau_connector_funcs_lvds;
  898. } else {
  899. funcs = &nouveau_connector_funcs;
  900. }
  901. /* defaults, will get overridden in detect() */
  902. connector->interlace_allowed = false;
  903. connector->doublescan_allowed = false;
  904. drm_connector_init(dev, connector, funcs, type);
  905. drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
  906. /* Init DVI-I specific properties */
  907. if (nv_connector->type == DCB_CONNECTOR_DVI_I)
  908. drm_object_attach_property(&connector->base, dev->mode_config.dvi_i_subconnector_property, 0);
  909. /* Add overscan compensation options to digital outputs */
  910. if (disp->underscan_property &&
  911. (type == DRM_MODE_CONNECTOR_DVID ||
  912. type == DRM_MODE_CONNECTOR_DVII ||
  913. type == DRM_MODE_CONNECTOR_HDMIA ||
  914. type == DRM_MODE_CONNECTOR_DisplayPort)) {
  915. drm_object_attach_property(&connector->base,
  916. disp->underscan_property,
  917. UNDERSCAN_OFF);
  918. drm_object_attach_property(&connector->base,
  919. disp->underscan_hborder_property,
  920. 0);
  921. drm_object_attach_property(&connector->base,
  922. disp->underscan_vborder_property,
  923. 0);
  924. }
  925. /* Add hue and saturation options */
  926. if (disp->vibrant_hue_property)
  927. drm_object_attach_property(&connector->base,
  928. disp->vibrant_hue_property,
  929. 90);
  930. if (disp->color_vibrance_property)
  931. drm_object_attach_property(&connector->base,
  932. disp->color_vibrance_property,
  933. 150);
  934. switch (nv_connector->type) {
  935. case DCB_CONNECTOR_VGA:
  936. if (nv_device(drm->device)->card_type >= NV_50) {
  937. drm_object_attach_property(&connector->base,
  938. dev->mode_config.scaling_mode_property,
  939. nv_connector->scaling_mode);
  940. }
  941. /* fall-through */
  942. case DCB_CONNECTOR_TV_0:
  943. case DCB_CONNECTOR_TV_1:
  944. case DCB_CONNECTOR_TV_3:
  945. nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
  946. break;
  947. default:
  948. nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
  949. drm_object_attach_property(&connector->base,
  950. dev->mode_config.scaling_mode_property,
  951. nv_connector->scaling_mode);
  952. if (disp->dithering_mode) {
  953. nv_connector->dithering_mode = DITHERING_MODE_AUTO;
  954. drm_object_attach_property(&connector->base,
  955. disp->dithering_mode,
  956. nv_connector->dithering_mode);
  957. }
  958. if (disp->dithering_depth) {
  959. nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
  960. drm_object_attach_property(&connector->base,
  961. disp->dithering_depth,
  962. nv_connector->dithering_depth);
  963. }
  964. break;
  965. }
  966. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  967. if (gpio && nv_connector->hpd.func != DCB_GPIO_UNUSED) {
  968. ret = gpio->isr_add(gpio, 0, nv_connector->hpd.func, 0xff,
  969. nouveau_connector_hotplug, connector);
  970. if (ret == 0)
  971. connector->polled = DRM_CONNECTOR_POLL_HPD;
  972. }
  973. drm_sysfs_connector_add(connector);
  974. return connector;
  975. }
  976. static void
  977. nouveau_connector_hotplug(void *data, int plugged)
  978. {
  979. struct drm_connector *connector = data;
  980. struct drm_device *dev = connector->dev;
  981. struct nouveau_drm *drm = nouveau_drm(dev);
  982. NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un",
  983. drm_get_connector_name(connector));
  984. if (plugged)
  985. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  986. else
  987. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  988. drm_helper_hpd_irq_event(dev);
  989. }