nouveau_connector.c 32 KB

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