nouveau_connector.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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 "drmP.h"
  28. #include "drm_edid.h"
  29. #include "drm_crtc_helper.h"
  30. #include "nouveau_reg.h"
  31. #include "nouveau_drv.h"
  32. #include "nouveau_encoder.h"
  33. #include "nouveau_crtc.h"
  34. #include "nouveau_connector.h"
  35. #include "nouveau_hw.h"
  36. static void nouveau_connector_hotplug(void *, int);
  37. struct nouveau_encoder *
  38. find_encoder(struct drm_connector *connector, int type)
  39. {
  40. struct drm_device *dev = connector->dev;
  41. struct nouveau_encoder *nv_encoder;
  42. struct drm_mode_object *obj;
  43. int i, id;
  44. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  45. id = connector->encoder_ids[i];
  46. if (!id)
  47. break;
  48. obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
  49. if (!obj)
  50. continue;
  51. nv_encoder = nouveau_encoder(obj_to_encoder(obj));
  52. if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
  53. return nv_encoder;
  54. }
  55. return NULL;
  56. }
  57. struct nouveau_connector *
  58. nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
  59. {
  60. struct drm_device *dev = to_drm_encoder(encoder)->dev;
  61. struct drm_connector *drm_connector;
  62. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
  63. if (drm_connector->encoder == to_drm_encoder(encoder))
  64. return nouveau_connector(drm_connector);
  65. }
  66. return NULL;
  67. }
  68. /*TODO: This could use improvement, and learn to handle the fixed
  69. * BIOS tables etc. It's fine currently, for its only user.
  70. */
  71. int
  72. nouveau_connector_bpp(struct drm_connector *connector)
  73. {
  74. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  75. if (nv_connector->edid && nv_connector->edid->revision >= 4) {
  76. u8 bpc = ((nv_connector->edid->input & 0x70) >> 3) + 4;
  77. if (bpc > 4)
  78. return bpc;
  79. }
  80. return 18;
  81. }
  82. static void
  83. nouveau_connector_destroy(struct drm_connector *connector)
  84. {
  85. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  86. struct drm_nouveau_private *dev_priv;
  87. struct nouveau_gpio_engine *pgpio;
  88. struct drm_device *dev;
  89. if (!nv_connector)
  90. return;
  91. dev = nv_connector->base.dev;
  92. dev_priv = dev->dev_private;
  93. NV_DEBUG_KMS(dev, "\n");
  94. pgpio = &dev_priv->engine.gpio;
  95. if (pgpio->irq_unregister) {
  96. pgpio->irq_unregister(dev, nv_connector->dcb->gpio_tag,
  97. nouveau_connector_hotplug, connector);
  98. }
  99. kfree(nv_connector->edid);
  100. drm_sysfs_connector_remove(connector);
  101. drm_connector_cleanup(connector);
  102. kfree(connector);
  103. }
  104. static struct nouveau_i2c_chan *
  105. nouveau_connector_ddc_detect(struct drm_connector *connector,
  106. struct nouveau_encoder **pnv_encoder)
  107. {
  108. struct drm_device *dev = connector->dev;
  109. int i;
  110. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  111. struct nouveau_i2c_chan *i2c = 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. i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  124. if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
  125. *pnv_encoder = nv_encoder;
  126. return i2c;
  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, OUTPUT_TMDS)) ||
  141. (nv_encoder = find_encoder(connector, 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 drm_nouveau_private *dev_priv = connector->dev->dev_private;
  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_encoder->dcb->type == OUTPUT_LVDS ||
  168. nv_encoder->dcb->type == OUTPUT_TMDS) {
  169. connector->doublescan_allowed = false;
  170. connector->interlace_allowed = false;
  171. } else {
  172. connector->doublescan_allowed = true;
  173. if (dev_priv->card_type == NV_20 ||
  174. (dev_priv->card_type == NV_10 &&
  175. (dev->pci_device & 0x0ff0) != 0x0100 &&
  176. (dev->pci_device & 0x0ff0) != 0x0150))
  177. /* HW is broken */
  178. connector->interlace_allowed = false;
  179. else
  180. connector->interlace_allowed = true;
  181. }
  182. if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
  183. drm_connector_property_set_value(connector,
  184. dev->mode_config.dvi_i_subconnector_property,
  185. nv_encoder->dcb->type == OUTPUT_TMDS ?
  186. DRM_MODE_SUBCONNECTOR_DVID :
  187. DRM_MODE_SUBCONNECTOR_DVIA);
  188. }
  189. }
  190. static enum drm_connector_status
  191. nouveau_connector_detect(struct drm_connector *connector, bool force)
  192. {
  193. struct drm_device *dev = connector->dev;
  194. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  195. struct nouveau_encoder *nv_encoder = NULL;
  196. struct nouveau_encoder *nv_partner;
  197. struct nouveau_i2c_chan *i2c;
  198. int type;
  199. /* Cleanup the previous EDID block. */
  200. if (nv_connector->edid) {
  201. drm_mode_connector_update_edid_property(connector, NULL);
  202. kfree(nv_connector->edid);
  203. nv_connector->edid = NULL;
  204. }
  205. i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
  206. if (i2c) {
  207. nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
  208. drm_mode_connector_update_edid_property(connector,
  209. nv_connector->edid);
  210. if (!nv_connector->edid) {
  211. NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
  212. drm_get_connector_name(connector));
  213. goto detect_analog;
  214. }
  215. if (nv_encoder->dcb->type == OUTPUT_DP &&
  216. !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
  217. NV_ERROR(dev, "Detected %s, but failed init\n",
  218. drm_get_connector_name(connector));
  219. return connector_status_disconnected;
  220. }
  221. /* Override encoder type for DVI-I based on whether EDID
  222. * says the display is digital or analog, both use the
  223. * same i2c channel so the value returned from ddc_detect
  224. * isn't necessarily correct.
  225. */
  226. nv_partner = NULL;
  227. if (nv_encoder->dcb->type == OUTPUT_TMDS)
  228. nv_partner = find_encoder(connector, OUTPUT_ANALOG);
  229. if (nv_encoder->dcb->type == OUTPUT_ANALOG)
  230. nv_partner = find_encoder(connector, OUTPUT_TMDS);
  231. if (nv_partner && ((nv_encoder->dcb->type == OUTPUT_ANALOG &&
  232. nv_partner->dcb->type == OUTPUT_TMDS) ||
  233. (nv_encoder->dcb->type == OUTPUT_TMDS &&
  234. nv_partner->dcb->type == OUTPUT_ANALOG))) {
  235. if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
  236. type = OUTPUT_TMDS;
  237. else
  238. type = OUTPUT_ANALOG;
  239. nv_encoder = find_encoder(connector, type);
  240. }
  241. nouveau_connector_set_encoder(connector, nv_encoder);
  242. return connector_status_connected;
  243. }
  244. nv_encoder = nouveau_connector_of_detect(connector);
  245. if (nv_encoder) {
  246. nouveau_connector_set_encoder(connector, nv_encoder);
  247. return connector_status_connected;
  248. }
  249. detect_analog:
  250. nv_encoder = find_encoder(connector, OUTPUT_ANALOG);
  251. if (!nv_encoder && !nouveau_tv_disable)
  252. nv_encoder = find_encoder(connector, OUTPUT_TV);
  253. if (nv_encoder && force) {
  254. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  255. struct drm_encoder_helper_funcs *helper =
  256. encoder->helper_private;
  257. if (helper->detect(encoder, connector) ==
  258. connector_status_connected) {
  259. nouveau_connector_set_encoder(connector, nv_encoder);
  260. return connector_status_connected;
  261. }
  262. }
  263. return connector_status_disconnected;
  264. }
  265. static enum drm_connector_status
  266. nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
  267. {
  268. struct drm_device *dev = connector->dev;
  269. struct drm_nouveau_private *dev_priv = dev->dev_private;
  270. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  271. struct nouveau_encoder *nv_encoder = NULL;
  272. enum drm_connector_status status = connector_status_disconnected;
  273. /* Cleanup the previous EDID block. */
  274. if (nv_connector->edid) {
  275. drm_mode_connector_update_edid_property(connector, NULL);
  276. kfree(nv_connector->edid);
  277. nv_connector->edid = NULL;
  278. }
  279. nv_encoder = find_encoder(connector, OUTPUT_LVDS);
  280. if (!nv_encoder)
  281. return connector_status_disconnected;
  282. /* Try retrieving EDID via DDC */
  283. if (!dev_priv->vbios.fp_no_ddc) {
  284. status = nouveau_connector_detect(connector, force);
  285. if (status == connector_status_connected)
  286. goto out;
  287. }
  288. /* On some laptops (Sony, i'm looking at you) there appears to
  289. * be no direct way of accessing the panel's EDID. The only
  290. * option available to us appears to be to ask ACPI for help..
  291. *
  292. * It's important this check's before trying straps, one of the
  293. * said manufacturer's laptops are configured in such a way
  294. * the nouveau decides an entry in the VBIOS FP mode table is
  295. * valid - it's not (rh#613284)
  296. */
  297. if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
  298. if (!nouveau_acpi_edid(dev, connector)) {
  299. status = connector_status_connected;
  300. goto out;
  301. }
  302. }
  303. /* If no EDID found above, and the VBIOS indicates a hardcoded
  304. * modeline is avalilable for the panel, set it as the panel's
  305. * native mode and exit.
  306. */
  307. if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
  308. nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
  309. status = connector_status_connected;
  310. goto out;
  311. }
  312. /* Still nothing, some VBIOS images have a hardcoded EDID block
  313. * stored for the panel stored in them.
  314. */
  315. if (!dev_priv->vbios.fp_no_ddc) {
  316. struct edid *edid =
  317. (struct edid *)nouveau_bios_embedded_edid(dev);
  318. if (edid) {
  319. nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  320. *(nv_connector->edid) = *edid;
  321. status = connector_status_connected;
  322. }
  323. }
  324. out:
  325. #if defined(CONFIG_ACPI_BUTTON) || \
  326. (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
  327. if (status == connector_status_connected &&
  328. !nouveau_ignorelid && !acpi_lid_open())
  329. status = connector_status_unknown;
  330. #endif
  331. drm_mode_connector_update_edid_property(connector, nv_connector->edid);
  332. nouveau_connector_set_encoder(connector, nv_encoder);
  333. return status;
  334. }
  335. static void
  336. nouveau_connector_force(struct drm_connector *connector)
  337. {
  338. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  339. struct nouveau_encoder *nv_encoder;
  340. int type;
  341. if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
  342. if (connector->force == DRM_FORCE_ON_DIGITAL)
  343. type = OUTPUT_TMDS;
  344. else
  345. type = OUTPUT_ANALOG;
  346. } else
  347. type = OUTPUT_ANY;
  348. nv_encoder = find_encoder(connector, type);
  349. if (!nv_encoder) {
  350. NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
  351. drm_get_connector_name(connector));
  352. connector->status = connector_status_disconnected;
  353. return;
  354. }
  355. nouveau_connector_set_encoder(connector, nv_encoder);
  356. }
  357. static int
  358. nouveau_connector_set_property(struct drm_connector *connector,
  359. struct drm_property *property, uint64_t value)
  360. {
  361. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  362. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  363. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  364. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  365. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  366. struct drm_device *dev = connector->dev;
  367. struct nouveau_crtc *nv_crtc;
  368. int ret;
  369. nv_crtc = NULL;
  370. if (connector->encoder && connector->encoder->crtc)
  371. nv_crtc = nouveau_crtc(connector->encoder->crtc);
  372. /* Scaling mode */
  373. if (property == dev->mode_config.scaling_mode_property) {
  374. bool modeset = false;
  375. switch (value) {
  376. case DRM_MODE_SCALE_NONE:
  377. case DRM_MODE_SCALE_FULLSCREEN:
  378. case DRM_MODE_SCALE_CENTER:
  379. case DRM_MODE_SCALE_ASPECT:
  380. break;
  381. default:
  382. return -EINVAL;
  383. }
  384. /* LVDS always needs gpu scaling */
  385. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
  386. value == DRM_MODE_SCALE_NONE)
  387. return -EINVAL;
  388. /* Changing between GPU and panel scaling requires a full
  389. * modeset
  390. */
  391. if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
  392. (value == DRM_MODE_SCALE_NONE))
  393. modeset = true;
  394. nv_connector->scaling_mode = value;
  395. if (!nv_crtc)
  396. return 0;
  397. if (modeset || !nv_crtc->set_scale) {
  398. ret = drm_crtc_helper_set_mode(&nv_crtc->base,
  399. &nv_crtc->base.mode,
  400. nv_crtc->base.x,
  401. nv_crtc->base.y, NULL);
  402. if (!ret)
  403. return -EINVAL;
  404. } else {
  405. ret = nv_crtc->set_scale(nv_crtc, value, true);
  406. if (ret)
  407. return ret;
  408. }
  409. return 0;
  410. }
  411. /* Underscan */
  412. if (property == disp->underscan_property) {
  413. if (nv_connector->underscan != value) {
  414. nv_connector->underscan = value;
  415. if (!nv_crtc || !nv_crtc->set_scale)
  416. return 0;
  417. return nv_crtc->set_scale(nv_crtc,
  418. nv_connector->scaling_mode,
  419. true);
  420. }
  421. return 0;
  422. }
  423. if (property == disp->underscan_hborder_property) {
  424. if (nv_connector->underscan_hborder != value) {
  425. nv_connector->underscan_hborder = value;
  426. if (!nv_crtc || !nv_crtc->set_scale)
  427. return 0;
  428. return nv_crtc->set_scale(nv_crtc,
  429. nv_connector->scaling_mode,
  430. true);
  431. }
  432. return 0;
  433. }
  434. if (property == disp->underscan_vborder_property) {
  435. if (nv_connector->underscan_vborder != value) {
  436. nv_connector->underscan_vborder = value;
  437. if (!nv_crtc || !nv_crtc->set_scale)
  438. return 0;
  439. return nv_crtc->set_scale(nv_crtc,
  440. nv_connector->scaling_mode,
  441. true);
  442. }
  443. return 0;
  444. }
  445. /* Dithering */
  446. if (property == dev->mode_config.dithering_mode_property) {
  447. if (value == DRM_MODE_DITHERING_ON)
  448. nv_connector->use_dithering = true;
  449. else
  450. nv_connector->use_dithering = false;
  451. if (!nv_crtc || !nv_crtc->set_dither)
  452. return 0;
  453. return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
  454. true);
  455. }
  456. if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
  457. return get_slave_funcs(encoder)->set_property(
  458. encoder, connector, property, value);
  459. return -EINVAL;
  460. }
  461. static struct drm_display_mode *
  462. nouveau_connector_native_mode(struct drm_connector *connector)
  463. {
  464. struct drm_connector_helper_funcs *helper = connector->helper_private;
  465. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  466. struct drm_device *dev = connector->dev;
  467. struct drm_display_mode *mode, *largest = NULL;
  468. int high_w = 0, high_h = 0, high_v = 0;
  469. list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
  470. mode->vrefresh = drm_mode_vrefresh(mode);
  471. if (helper->mode_valid(connector, mode) != MODE_OK ||
  472. (mode->flags & DRM_MODE_FLAG_INTERLACE))
  473. continue;
  474. /* Use preferred mode if there is one.. */
  475. if (mode->type & DRM_MODE_TYPE_PREFERRED) {
  476. NV_DEBUG_KMS(dev, "native mode from preferred\n");
  477. return drm_mode_duplicate(dev, mode);
  478. }
  479. /* Otherwise, take the resolution with the largest width, then
  480. * height, then vertical refresh
  481. */
  482. if (mode->hdisplay < high_w)
  483. continue;
  484. if (mode->hdisplay == high_w && mode->vdisplay < high_h)
  485. continue;
  486. if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
  487. mode->vrefresh < high_v)
  488. continue;
  489. high_w = mode->hdisplay;
  490. high_h = mode->vdisplay;
  491. high_v = mode->vrefresh;
  492. largest = mode;
  493. }
  494. NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
  495. high_w, high_h, high_v);
  496. return largest ? drm_mode_duplicate(dev, largest) : NULL;
  497. }
  498. struct moderec {
  499. int hdisplay;
  500. int vdisplay;
  501. };
  502. static struct moderec scaler_modes[] = {
  503. { 1920, 1200 },
  504. { 1920, 1080 },
  505. { 1680, 1050 },
  506. { 1600, 1200 },
  507. { 1400, 1050 },
  508. { 1280, 1024 },
  509. { 1280, 960 },
  510. { 1152, 864 },
  511. { 1024, 768 },
  512. { 800, 600 },
  513. { 720, 400 },
  514. { 640, 480 },
  515. { 640, 400 },
  516. { 640, 350 },
  517. {}
  518. };
  519. static int
  520. nouveau_connector_scaler_modes_add(struct drm_connector *connector)
  521. {
  522. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  523. struct drm_display_mode *native = nv_connector->native_mode, *m;
  524. struct drm_device *dev = connector->dev;
  525. struct moderec *mode = &scaler_modes[0];
  526. int modes = 0;
  527. if (!native)
  528. return 0;
  529. while (mode->hdisplay) {
  530. if (mode->hdisplay <= native->hdisplay &&
  531. mode->vdisplay <= native->vdisplay) {
  532. m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
  533. drm_mode_vrefresh(native), false,
  534. false, false);
  535. if (!m)
  536. continue;
  537. m->type |= DRM_MODE_TYPE_DRIVER;
  538. drm_mode_probed_add(connector, m);
  539. modes++;
  540. }
  541. mode++;
  542. }
  543. return modes;
  544. }
  545. static int
  546. nouveau_connector_get_modes(struct drm_connector *connector)
  547. {
  548. struct drm_device *dev = connector->dev;
  549. struct drm_nouveau_private *dev_priv = dev->dev_private;
  550. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  551. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  552. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  553. int ret = 0;
  554. /* destroy the native mode, the attached monitor could have changed.
  555. */
  556. if (nv_connector->native_mode) {
  557. drm_mode_destroy(dev, nv_connector->native_mode);
  558. nv_connector->native_mode = NULL;
  559. }
  560. if (nv_connector->edid)
  561. ret = drm_add_edid_modes(connector, nv_connector->edid);
  562. else
  563. if (nv_encoder->dcb->type == OUTPUT_LVDS &&
  564. (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
  565. dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
  566. struct drm_display_mode mode;
  567. nouveau_bios_fp_mode(dev, &mode);
  568. nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
  569. }
  570. /* Find the native mode if this is a digital panel, if we didn't
  571. * find any modes through DDC previously add the native mode to
  572. * the list of modes.
  573. */
  574. if (!nv_connector->native_mode)
  575. nv_connector->native_mode =
  576. nouveau_connector_native_mode(connector);
  577. if (ret == 0 && nv_connector->native_mode) {
  578. struct drm_display_mode *mode;
  579. mode = drm_mode_duplicate(dev, nv_connector->native_mode);
  580. drm_mode_probed_add(connector, mode);
  581. ret = 1;
  582. }
  583. if (nv_encoder->dcb->type == OUTPUT_TV)
  584. ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
  585. if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
  586. nv_connector->dcb->type == DCB_CONNECTOR_LVDS_SPWG ||
  587. nv_connector->dcb->type == DCB_CONNECTOR_eDP)
  588. ret += nouveau_connector_scaler_modes_add(connector);
  589. return ret;
  590. }
  591. static unsigned
  592. get_tmds_link_bandwidth(struct drm_connector *connector)
  593. {
  594. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  595. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  596. struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
  597. if (dcb->location != DCB_LOC_ON_CHIP ||
  598. dev_priv->chipset >= 0x46)
  599. return 165000;
  600. else if (dev_priv->chipset >= 0x40)
  601. return 155000;
  602. else if (dev_priv->chipset >= 0x18)
  603. return 135000;
  604. else
  605. return 112000;
  606. }
  607. static int
  608. nouveau_connector_mode_valid(struct drm_connector *connector,
  609. struct drm_display_mode *mode)
  610. {
  611. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  612. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  613. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  614. unsigned min_clock = 25000, max_clock = min_clock;
  615. unsigned clock = mode->clock;
  616. switch (nv_encoder->dcb->type) {
  617. case OUTPUT_LVDS:
  618. if (nv_connector->native_mode &&
  619. (mode->hdisplay > nv_connector->native_mode->hdisplay ||
  620. mode->vdisplay > nv_connector->native_mode->vdisplay))
  621. return MODE_PANEL;
  622. min_clock = 0;
  623. max_clock = 400000;
  624. break;
  625. case OUTPUT_TMDS:
  626. max_clock = get_tmds_link_bandwidth(connector);
  627. if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
  628. max_clock *= 2;
  629. break;
  630. case OUTPUT_ANALOG:
  631. max_clock = nv_encoder->dcb->crtconf.maxfreq;
  632. if (!max_clock)
  633. max_clock = 350000;
  634. break;
  635. case OUTPUT_TV:
  636. return get_slave_funcs(encoder)->mode_valid(encoder, mode);
  637. case OUTPUT_DP:
  638. max_clock = nv_encoder->dp.link_nr;
  639. max_clock *= nv_encoder->dp.link_bw;
  640. clock = clock * nouveau_connector_bpp(connector) / 10;
  641. break;
  642. default:
  643. BUG_ON(1);
  644. return MODE_BAD;
  645. }
  646. if (clock < min_clock)
  647. return MODE_CLOCK_LOW;
  648. if (clock > max_clock)
  649. return MODE_CLOCK_HIGH;
  650. return MODE_OK;
  651. }
  652. static struct drm_encoder *
  653. nouveau_connector_best_encoder(struct drm_connector *connector)
  654. {
  655. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  656. if (nv_connector->detected_encoder)
  657. return to_drm_encoder(nv_connector->detected_encoder);
  658. return NULL;
  659. }
  660. static const struct drm_connector_helper_funcs
  661. nouveau_connector_helper_funcs = {
  662. .get_modes = nouveau_connector_get_modes,
  663. .mode_valid = nouveau_connector_mode_valid,
  664. .best_encoder = nouveau_connector_best_encoder,
  665. };
  666. static const struct drm_connector_funcs
  667. nouveau_connector_funcs = {
  668. .dpms = drm_helper_connector_dpms,
  669. .save = NULL,
  670. .restore = NULL,
  671. .detect = nouveau_connector_detect,
  672. .destroy = nouveau_connector_destroy,
  673. .fill_modes = drm_helper_probe_single_connector_modes,
  674. .set_property = nouveau_connector_set_property,
  675. .force = nouveau_connector_force
  676. };
  677. static const struct drm_connector_funcs
  678. nouveau_connector_funcs_lvds = {
  679. .dpms = drm_helper_connector_dpms,
  680. .save = NULL,
  681. .restore = NULL,
  682. .detect = nouveau_connector_detect_lvds,
  683. .destroy = nouveau_connector_destroy,
  684. .fill_modes = drm_helper_probe_single_connector_modes,
  685. .set_property = nouveau_connector_set_property,
  686. .force = nouveau_connector_force
  687. };
  688. struct drm_connector *
  689. nouveau_connector_create(struct drm_device *dev, int index)
  690. {
  691. const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
  692. struct drm_nouveau_private *dev_priv = dev->dev_private;
  693. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  694. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  695. struct nouveau_connector *nv_connector = NULL;
  696. struct dcb_connector_table_entry *dcb = NULL;
  697. struct drm_connector *connector;
  698. int type, ret = 0;
  699. NV_DEBUG_KMS(dev, "\n");
  700. if (index >= dev_priv->vbios.dcb.connector.entries)
  701. return ERR_PTR(-EINVAL);
  702. dcb = &dev_priv->vbios.dcb.connector.entry[index];
  703. if (dcb->drm)
  704. return dcb->drm;
  705. switch (dcb->type) {
  706. case DCB_CONNECTOR_VGA:
  707. type = DRM_MODE_CONNECTOR_VGA;
  708. break;
  709. case DCB_CONNECTOR_TV_0:
  710. case DCB_CONNECTOR_TV_1:
  711. case DCB_CONNECTOR_TV_3:
  712. type = DRM_MODE_CONNECTOR_TV;
  713. break;
  714. case DCB_CONNECTOR_DVI_I:
  715. type = DRM_MODE_CONNECTOR_DVII;
  716. break;
  717. case DCB_CONNECTOR_DVI_D:
  718. type = DRM_MODE_CONNECTOR_DVID;
  719. break;
  720. case DCB_CONNECTOR_HDMI_0:
  721. case DCB_CONNECTOR_HDMI_1:
  722. type = DRM_MODE_CONNECTOR_HDMIA;
  723. break;
  724. case DCB_CONNECTOR_LVDS:
  725. case DCB_CONNECTOR_LVDS_SPWG:
  726. type = DRM_MODE_CONNECTOR_LVDS;
  727. funcs = &nouveau_connector_funcs_lvds;
  728. break;
  729. case DCB_CONNECTOR_DP:
  730. type = DRM_MODE_CONNECTOR_DisplayPort;
  731. break;
  732. case DCB_CONNECTOR_eDP:
  733. type = DRM_MODE_CONNECTOR_eDP;
  734. break;
  735. default:
  736. NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
  737. return ERR_PTR(-EINVAL);
  738. }
  739. nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
  740. if (!nv_connector)
  741. return ERR_PTR(-ENOMEM);
  742. nv_connector->dcb = dcb;
  743. connector = &nv_connector->base;
  744. /* defaults, will get overridden in detect() */
  745. connector->interlace_allowed = false;
  746. connector->doublescan_allowed = false;
  747. drm_connector_init(dev, connector, funcs, type);
  748. drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
  749. /* Check if we need dithering enabled */
  750. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
  751. bool dummy, is_24bit = false;
  752. ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
  753. if (ret) {
  754. NV_ERROR(dev, "Error parsing LVDS table, disabling "
  755. "LVDS\n");
  756. goto fail;
  757. }
  758. nv_connector->use_dithering = !is_24bit;
  759. }
  760. /* Init DVI-I specific properties */
  761. if (dcb->type == DCB_CONNECTOR_DVI_I) {
  762. drm_mode_create_dvi_i_properties(dev);
  763. drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
  764. drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
  765. }
  766. /* Add overscan compensation options to digital outputs */
  767. if ((dev_priv->card_type == NV_50 ||
  768. dev_priv->card_type == NV_C0) &&
  769. (dcb->type == DCB_CONNECTOR_DVI_D ||
  770. dcb->type == DCB_CONNECTOR_DVI_I ||
  771. dcb->type == DCB_CONNECTOR_HDMI_0 ||
  772. dcb->type == DCB_CONNECTOR_HDMI_1 ||
  773. dcb->type == DCB_CONNECTOR_DP)) {
  774. drm_connector_attach_property(connector,
  775. disp->underscan_property,
  776. UNDERSCAN_OFF);
  777. drm_connector_attach_property(connector,
  778. disp->underscan_hborder_property,
  779. 0);
  780. drm_connector_attach_property(connector,
  781. disp->underscan_vborder_property,
  782. 0);
  783. }
  784. switch (dcb->type) {
  785. case DCB_CONNECTOR_VGA:
  786. if (dev_priv->card_type >= NV_50) {
  787. drm_connector_attach_property(connector,
  788. dev->mode_config.scaling_mode_property,
  789. nv_connector->scaling_mode);
  790. }
  791. /* fall-through */
  792. case DCB_CONNECTOR_TV_0:
  793. case DCB_CONNECTOR_TV_1:
  794. case DCB_CONNECTOR_TV_3:
  795. nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
  796. break;
  797. default:
  798. nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
  799. drm_connector_attach_property(connector,
  800. dev->mode_config.scaling_mode_property,
  801. nv_connector->scaling_mode);
  802. drm_connector_attach_property(connector,
  803. dev->mode_config.dithering_mode_property,
  804. nv_connector->use_dithering ?
  805. DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
  806. break;
  807. }
  808. if (nv_connector->dcb->gpio_tag != 0xff && pgpio->irq_register) {
  809. pgpio->irq_register(dev, nv_connector->dcb->gpio_tag,
  810. nouveau_connector_hotplug, connector);
  811. connector->polled = DRM_CONNECTOR_POLL_HPD;
  812. } else {
  813. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  814. }
  815. drm_sysfs_connector_add(connector);
  816. dcb->drm = connector;
  817. return dcb->drm;
  818. fail:
  819. drm_connector_cleanup(connector);
  820. kfree(connector);
  821. return ERR_PTR(ret);
  822. }
  823. static void
  824. nouveau_connector_hotplug(void *data, int plugged)
  825. {
  826. struct drm_connector *connector = data;
  827. struct drm_device *dev = connector->dev;
  828. NV_DEBUG(dev, "%splugged %s\n", plugged ? "" : "un",
  829. drm_get_connector_name(connector));
  830. if (plugged)
  831. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  832. else
  833. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  834. drm_helper_hpd_irq_event(dev);
  835. }