nouveau_connector.c 23 KB

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