nouveau_connector.c 22 KB

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