nouveau_connector.c 24 KB

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