nouveau_connector.c 23 KB

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