nouveau_connector.c 33 KB

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