nouveau_connector.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. kfree(nv_connector->edid);
  205. nv_connector->edid = NULL;
  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. return connector_status_disconnected;
  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 (connector->connector_type == DRM_MODE_CONNECTOR_DVII) {
  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. nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
  246. if (!nv_encoder)
  247. nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
  248. if (nv_encoder) {
  249. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  250. struct drm_encoder_helper_funcs *helper =
  251. encoder->helper_private;
  252. if (helper->detect(encoder, connector) ==
  253. connector_status_connected) {
  254. nouveau_connector_set_encoder(connector, nv_encoder);
  255. return connector_status_connected;
  256. }
  257. }
  258. return connector_status_disconnected;
  259. }
  260. static void
  261. nouveau_connector_force(struct drm_connector *connector)
  262. {
  263. struct drm_device *dev = connector->dev;
  264. struct nouveau_encoder *nv_encoder;
  265. int type;
  266. if (connector->connector_type == DRM_MODE_CONNECTOR_DVII) {
  267. if (connector->force == DRM_FORCE_ON_DIGITAL)
  268. type = OUTPUT_TMDS;
  269. else
  270. type = OUTPUT_ANALOG;
  271. } else
  272. type = OUTPUT_ANY;
  273. nv_encoder = find_encoder_by_type(connector, type);
  274. if (!nv_encoder) {
  275. NV_ERROR(dev, "can't find encoder to force %s on!\n",
  276. drm_get_connector_name(connector));
  277. connector->status = connector_status_disconnected;
  278. return;
  279. }
  280. nouveau_connector_set_encoder(connector, nv_encoder);
  281. }
  282. static int
  283. nouveau_connector_set_property(struct drm_connector *connector,
  284. struct drm_property *property, uint64_t value)
  285. {
  286. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  287. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  288. struct drm_device *dev = connector->dev;
  289. int ret;
  290. /* Scaling mode */
  291. if (property == dev->mode_config.scaling_mode_property) {
  292. struct nouveau_crtc *nv_crtc = NULL;
  293. bool modeset = false;
  294. switch (value) {
  295. case DRM_MODE_SCALE_NONE:
  296. case DRM_MODE_SCALE_FULLSCREEN:
  297. case DRM_MODE_SCALE_CENTER:
  298. case DRM_MODE_SCALE_ASPECT:
  299. break;
  300. default:
  301. return -EINVAL;
  302. }
  303. /* LVDS always needs gpu scaling */
  304. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
  305. value == DRM_MODE_SCALE_NONE)
  306. return -EINVAL;
  307. /* Changing between GPU and panel scaling requires a full
  308. * modeset
  309. */
  310. if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
  311. (value == DRM_MODE_SCALE_NONE))
  312. modeset = true;
  313. nv_connector->scaling_mode = value;
  314. if (connector->encoder && connector->encoder->crtc)
  315. nv_crtc = nouveau_crtc(connector->encoder->crtc);
  316. if (!nv_crtc)
  317. return 0;
  318. if (modeset || !nv_crtc->set_scale) {
  319. ret = drm_crtc_helper_set_mode(&nv_crtc->base,
  320. &nv_crtc->base.mode,
  321. nv_crtc->base.x,
  322. nv_crtc->base.y, NULL);
  323. if (!ret)
  324. return -EINVAL;
  325. } else {
  326. ret = nv_crtc->set_scale(nv_crtc, value, true);
  327. if (ret)
  328. return ret;
  329. }
  330. return 0;
  331. }
  332. /* Dithering */
  333. if (property == dev->mode_config.dithering_mode_property) {
  334. struct nouveau_crtc *nv_crtc = NULL;
  335. if (value == DRM_MODE_DITHERING_ON)
  336. nv_connector->use_dithering = true;
  337. else
  338. nv_connector->use_dithering = false;
  339. if (connector->encoder && connector->encoder->crtc)
  340. nv_crtc = nouveau_crtc(connector->encoder->crtc);
  341. if (!nv_crtc || !nv_crtc->set_dither)
  342. return 0;
  343. return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
  344. true);
  345. }
  346. if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
  347. return get_slave_funcs(nv_encoder)->
  348. set_property(to_drm_encoder(nv_encoder), connector, property, value);
  349. return -EINVAL;
  350. }
  351. static struct drm_display_mode *
  352. nouveau_connector_native_mode(struct nouveau_connector *connector)
  353. {
  354. struct drm_device *dev = connector->base.dev;
  355. struct drm_display_mode *mode, *largest = NULL;
  356. int high_w = 0, high_h = 0, high_v = 0;
  357. /* Use preferred mode if there is one.. */
  358. list_for_each_entry(mode, &connector->base.probed_modes, head) {
  359. if (mode->type & DRM_MODE_TYPE_PREFERRED) {
  360. NV_DEBUG_KMS(dev, "native mode from preferred\n");
  361. return drm_mode_duplicate(dev, mode);
  362. }
  363. }
  364. /* Otherwise, take the resolution with the largest width, then height,
  365. * then vertical refresh
  366. */
  367. list_for_each_entry(mode, &connector->base.probed_modes, head) {
  368. if (mode->hdisplay < high_w)
  369. continue;
  370. if (mode->hdisplay == high_w && mode->vdisplay < high_h)
  371. continue;
  372. if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
  373. mode->vrefresh < high_v)
  374. continue;
  375. high_w = mode->hdisplay;
  376. high_h = mode->vdisplay;
  377. high_v = mode->vrefresh;
  378. largest = mode;
  379. }
  380. NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
  381. high_w, high_h, high_v);
  382. return largest ? drm_mode_duplicate(dev, largest) : NULL;
  383. }
  384. struct moderec {
  385. int hdisplay;
  386. int vdisplay;
  387. };
  388. static struct moderec scaler_modes[] = {
  389. { 1920, 1200 },
  390. { 1920, 1080 },
  391. { 1680, 1050 },
  392. { 1600, 1200 },
  393. { 1400, 1050 },
  394. { 1280, 1024 },
  395. { 1280, 960 },
  396. { 1152, 864 },
  397. { 1024, 768 },
  398. { 800, 600 },
  399. { 720, 400 },
  400. { 640, 480 },
  401. { 640, 400 },
  402. { 640, 350 },
  403. {}
  404. };
  405. static int
  406. nouveau_connector_scaler_modes_add(struct drm_connector *connector)
  407. {
  408. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  409. struct drm_display_mode *native = nv_connector->native_mode, *m;
  410. struct drm_device *dev = connector->dev;
  411. struct moderec *mode = &scaler_modes[0];
  412. int modes = 0;
  413. if (!native)
  414. return 0;
  415. while (mode->hdisplay) {
  416. if (mode->hdisplay <= native->hdisplay &&
  417. mode->vdisplay <= native->vdisplay) {
  418. m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
  419. drm_mode_vrefresh(native), false,
  420. false, false);
  421. if (!m)
  422. continue;
  423. m->type |= DRM_MODE_TYPE_DRIVER;
  424. drm_mode_probed_add(connector, m);
  425. modes++;
  426. }
  427. mode++;
  428. }
  429. return modes;
  430. }
  431. static int
  432. nouveau_connector_get_modes(struct drm_connector *connector)
  433. {
  434. struct drm_device *dev = connector->dev;
  435. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  436. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  437. int ret = 0;
  438. /* If we're not LVDS, destroy the previous native mode, the attached
  439. * monitor could have changed.
  440. */
  441. if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
  442. nv_connector->native_mode) {
  443. drm_mode_destroy(dev, nv_connector->native_mode);
  444. nv_connector->native_mode = NULL;
  445. }
  446. if (nv_connector->edid)
  447. ret = drm_add_edid_modes(connector, nv_connector->edid);
  448. /* Find the native mode if this is a digital panel, if we didn't
  449. * find any modes through DDC previously add the native mode to
  450. * the list of modes.
  451. */
  452. if (!nv_connector->native_mode)
  453. nv_connector->native_mode =
  454. nouveau_connector_native_mode(nv_connector);
  455. if (ret == 0 && nv_connector->native_mode) {
  456. struct drm_display_mode *mode;
  457. mode = drm_mode_duplicate(dev, nv_connector->native_mode);
  458. drm_mode_probed_add(connector, mode);
  459. ret = 1;
  460. }
  461. if (nv_encoder->dcb->type == OUTPUT_TV)
  462. ret = get_slave_funcs(nv_encoder)->
  463. get_modes(to_drm_encoder(nv_encoder), connector);
  464. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
  465. ret += nouveau_connector_scaler_modes_add(connector);
  466. return ret;
  467. }
  468. static int
  469. nouveau_connector_mode_valid(struct drm_connector *connector,
  470. struct drm_display_mode *mode)
  471. {
  472. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  473. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  474. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  475. unsigned min_clock = 25000, max_clock = min_clock;
  476. unsigned clock = mode->clock;
  477. switch (nv_encoder->dcb->type) {
  478. case OUTPUT_LVDS:
  479. BUG_ON(!nv_connector->native_mode);
  480. if (mode->hdisplay > nv_connector->native_mode->hdisplay ||
  481. mode->vdisplay > nv_connector->native_mode->vdisplay)
  482. return MODE_PANEL;
  483. min_clock = 0;
  484. max_clock = 400000;
  485. break;
  486. case OUTPUT_TMDS:
  487. if ((dev_priv->card_type >= NV_50 && !nouveau_duallink) ||
  488. (dev_priv->card_type < NV_50 &&
  489. !nv_encoder->dcb->duallink_possible))
  490. max_clock = 165000;
  491. else
  492. max_clock = 330000;
  493. break;
  494. case OUTPUT_ANALOG:
  495. max_clock = nv_encoder->dcb->crtconf.maxfreq;
  496. if (!max_clock)
  497. max_clock = 350000;
  498. break;
  499. case OUTPUT_TV:
  500. return get_slave_funcs(nv_encoder)->
  501. mode_valid(to_drm_encoder(nv_encoder), mode);
  502. case OUTPUT_DP:
  503. if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
  504. max_clock = nv_encoder->dp.link_nr * 270000;
  505. else
  506. max_clock = nv_encoder->dp.link_nr * 162000;
  507. clock *= 3;
  508. break;
  509. }
  510. if (clock < min_clock)
  511. return MODE_CLOCK_LOW;
  512. if (clock > max_clock)
  513. return MODE_CLOCK_HIGH;
  514. return MODE_OK;
  515. }
  516. static struct drm_encoder *
  517. nouveau_connector_best_encoder(struct drm_connector *connector)
  518. {
  519. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  520. if (nv_connector->detected_encoder)
  521. return to_drm_encoder(nv_connector->detected_encoder);
  522. return NULL;
  523. }
  524. static const struct drm_connector_helper_funcs
  525. nouveau_connector_helper_funcs = {
  526. .get_modes = nouveau_connector_get_modes,
  527. .mode_valid = nouveau_connector_mode_valid,
  528. .best_encoder = nouveau_connector_best_encoder,
  529. };
  530. static const struct drm_connector_funcs
  531. nouveau_connector_funcs = {
  532. .dpms = drm_helper_connector_dpms,
  533. .save = NULL,
  534. .restore = NULL,
  535. .detect = nouveau_connector_detect,
  536. .destroy = nouveau_connector_destroy,
  537. .fill_modes = drm_helper_probe_single_connector_modes,
  538. .set_property = nouveau_connector_set_property,
  539. .force = nouveau_connector_force
  540. };
  541. static int
  542. nouveau_connector_create_lvds(struct drm_device *dev,
  543. struct drm_connector *connector)
  544. {
  545. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  546. struct drm_nouveau_private *dev_priv = dev->dev_private;
  547. struct nouveau_i2c_chan *i2c = NULL;
  548. struct nouveau_encoder *nv_encoder;
  549. struct drm_display_mode native, *mode, *temp;
  550. bool dummy, if_is_24bit = false;
  551. int ret, flags;
  552. nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
  553. if (!nv_encoder)
  554. return -ENODEV;
  555. ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &if_is_24bit);
  556. if (ret) {
  557. NV_ERROR(dev, "Error parsing LVDS table, disabling LVDS\n");
  558. return ret;
  559. }
  560. nv_connector->use_dithering = !if_is_24bit;
  561. /* Firstly try getting EDID over DDC, if allowed and I2C channel
  562. * is available.
  563. */
  564. if (!dev_priv->VBIOS.pub.fp_no_ddc && nv_encoder->dcb->i2c_index < 0xf)
  565. i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  566. if (i2c) {
  567. nouveau_connector_ddc_prepare(connector, &flags);
  568. nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
  569. nouveau_connector_ddc_finish(connector, flags);
  570. }
  571. /* If no EDID found above, and the VBIOS indicates a hardcoded
  572. * modeline is avalilable for the panel, set it as the panel's
  573. * native mode and exit.
  574. */
  575. if (!nv_connector->edid && nouveau_bios_fp_mode(dev, &native) &&
  576. (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
  577. dev_priv->VBIOS.pub.fp_no_ddc)) {
  578. nv_connector->native_mode = drm_mode_duplicate(dev, &native);
  579. goto out;
  580. }
  581. /* Still nothing, some VBIOS images have a hardcoded EDID block
  582. * stored for the panel stored in them.
  583. */
  584. if (!nv_connector->edid && !nv_connector->native_mode &&
  585. !dev_priv->VBIOS.pub.fp_no_ddc) {
  586. struct edid *edid =
  587. (struct edid *)nouveau_bios_embedded_edid(dev);
  588. if (edid) {
  589. nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  590. *(nv_connector->edid) = *edid;
  591. }
  592. }
  593. if (!nv_connector->edid)
  594. goto out;
  595. /* We didn't find/use a panel mode from the VBIOS, so parse the EDID
  596. * block and look for the preferred mode there.
  597. */
  598. ret = drm_add_edid_modes(connector, nv_connector->edid);
  599. if (ret == 0)
  600. goto out;
  601. nv_connector->detected_encoder = nv_encoder;
  602. nv_connector->native_mode = nouveau_connector_native_mode(nv_connector);
  603. list_for_each_entry_safe(mode, temp, &connector->probed_modes, head)
  604. drm_mode_remove(connector, mode);
  605. out:
  606. if (!nv_connector->native_mode) {
  607. NV_ERROR(dev, "LVDS present in DCB table, but couldn't "
  608. "determine its native mode. Disabling.\n");
  609. return -ENODEV;
  610. }
  611. drm_mode_connector_update_edid_property(connector, nv_connector->edid);
  612. return 0;
  613. }
  614. int
  615. nouveau_connector_create(struct drm_device *dev, int index, int type)
  616. {
  617. struct drm_nouveau_private *dev_priv = dev->dev_private;
  618. struct nouveau_connector *nv_connector = NULL;
  619. struct drm_connector *connector;
  620. struct drm_encoder *encoder;
  621. int ret;
  622. NV_DEBUG_KMS(dev, "\n");
  623. nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
  624. if (!nv_connector)
  625. return -ENOMEM;
  626. nv_connector->dcb = nouveau_bios_connector_entry(dev, index);
  627. connector = &nv_connector->base;
  628. switch (type) {
  629. case DRM_MODE_CONNECTOR_VGA:
  630. NV_INFO(dev, "Detected a VGA connector\n");
  631. break;
  632. case DRM_MODE_CONNECTOR_DVID:
  633. NV_INFO(dev, "Detected a DVI-D connector\n");
  634. break;
  635. case DRM_MODE_CONNECTOR_DVII:
  636. NV_INFO(dev, "Detected a DVI-I connector\n");
  637. break;
  638. case DRM_MODE_CONNECTOR_LVDS:
  639. NV_INFO(dev, "Detected a LVDS connector\n");
  640. break;
  641. case DRM_MODE_CONNECTOR_TV:
  642. NV_INFO(dev, "Detected a TV connector\n");
  643. break;
  644. case DRM_MODE_CONNECTOR_DisplayPort:
  645. NV_INFO(dev, "Detected a DisplayPort connector\n");
  646. break;
  647. default:
  648. NV_ERROR(dev, "Unknown connector, this is not good.\n");
  649. break;
  650. }
  651. /* defaults, will get overridden in detect() */
  652. connector->interlace_allowed = false;
  653. connector->doublescan_allowed = false;
  654. drm_connector_init(dev, connector, &nouveau_connector_funcs, type);
  655. drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
  656. /* Init DVI-I specific properties */
  657. if (type == DRM_MODE_CONNECTOR_DVII) {
  658. drm_mode_create_dvi_i_properties(dev);
  659. drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
  660. drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
  661. }
  662. if (type != DRM_MODE_CONNECTOR_LVDS)
  663. nv_connector->use_dithering = false;
  664. if (type == DRM_MODE_CONNECTOR_DVID ||
  665. type == DRM_MODE_CONNECTOR_DVII ||
  666. type == DRM_MODE_CONNECTOR_LVDS ||
  667. type == DRM_MODE_CONNECTOR_DisplayPort) {
  668. nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
  669. drm_connector_attach_property(connector, dev->mode_config.scaling_mode_property,
  670. nv_connector->scaling_mode);
  671. drm_connector_attach_property(connector, dev->mode_config.dithering_mode_property,
  672. nv_connector->use_dithering ? DRM_MODE_DITHERING_ON
  673. : DRM_MODE_DITHERING_OFF);
  674. } else {
  675. nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
  676. if (type == DRM_MODE_CONNECTOR_VGA &&
  677. dev_priv->card_type >= NV_50) {
  678. drm_connector_attach_property(connector,
  679. dev->mode_config.scaling_mode_property,
  680. nv_connector->scaling_mode);
  681. }
  682. }
  683. /* attach encoders */
  684. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  685. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  686. if (nv_encoder->dcb->connector != index)
  687. continue;
  688. if (get_slave_funcs(nv_encoder))
  689. get_slave_funcs(nv_encoder)->create_resources(encoder, connector);
  690. drm_mode_connector_attach_encoder(connector, encoder);
  691. }
  692. drm_sysfs_connector_add(connector);
  693. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
  694. ret = nouveau_connector_create_lvds(dev, connector);
  695. if (ret) {
  696. connector->funcs->destroy(connector);
  697. return ret;
  698. }
  699. }
  700. return 0;
  701. }