nouveau_connector.c 23 KB

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