nouveau_connector.c 23 KB

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