nouveau_connector.c 24 KB

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