nouveau_connector.c 24 KB

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