nouveau_connector.c 23 KB

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