nouveau_connector.c 32 KB

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