nouveau_connector.c 24 KB

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