nouveau_connector.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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 void nouveau_connector_hotplug(void *, int);
  37. static struct nouveau_encoder *
  38. find_encoder_by_type(struct drm_connector *connector, int type)
  39. {
  40. struct drm_device *dev = connector->dev;
  41. struct nouveau_encoder *nv_encoder;
  42. struct drm_mode_object *obj;
  43. int i, id;
  44. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  45. id = connector->encoder_ids[i];
  46. if (!id)
  47. break;
  48. obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
  49. if (!obj)
  50. continue;
  51. nv_encoder = nouveau_encoder(obj_to_encoder(obj));
  52. if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
  53. return nv_encoder;
  54. }
  55. return NULL;
  56. }
  57. struct nouveau_connector *
  58. nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
  59. {
  60. struct drm_device *dev = to_drm_encoder(encoder)->dev;
  61. struct drm_connector *drm_connector;
  62. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
  63. if (drm_connector->encoder == to_drm_encoder(encoder))
  64. return nouveau_connector(drm_connector);
  65. }
  66. return NULL;
  67. }
  68. /*TODO: This could use improvement, and learn to handle the fixed
  69. * BIOS tables etc. It's fine currently, for its only user.
  70. */
  71. int
  72. nouveau_connector_bpp(struct drm_connector *connector)
  73. {
  74. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  75. if (nv_connector->edid && nv_connector->edid->revision >= 4) {
  76. u8 bpc = ((nv_connector->edid->input & 0x70) >> 3) + 4;
  77. if (bpc > 4)
  78. return bpc;
  79. }
  80. return 18;
  81. }
  82. static void
  83. nouveau_connector_destroy(struct drm_connector *connector)
  84. {
  85. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  86. struct drm_nouveau_private *dev_priv;
  87. struct nouveau_gpio_engine *pgpio;
  88. struct drm_device *dev;
  89. if (!nv_connector)
  90. return;
  91. dev = nv_connector->base.dev;
  92. dev_priv = dev->dev_private;
  93. NV_DEBUG_KMS(dev, "\n");
  94. pgpio = &dev_priv->engine.gpio;
  95. if (pgpio->irq_unregister) {
  96. pgpio->irq_unregister(dev, nv_connector->dcb->gpio_tag,
  97. nouveau_connector_hotplug, connector);
  98. }
  99. kfree(nv_connector->edid);
  100. drm_sysfs_connector_remove(connector);
  101. drm_connector_cleanup(connector);
  102. kfree(connector);
  103. }
  104. static struct nouveau_i2c_chan *
  105. nouveau_connector_ddc_detect(struct drm_connector *connector,
  106. struct nouveau_encoder **pnv_encoder)
  107. {
  108. struct drm_device *dev = connector->dev;
  109. int i;
  110. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  111. struct nouveau_i2c_chan *i2c = NULL;
  112. struct nouveau_encoder *nv_encoder;
  113. struct drm_mode_object *obj;
  114. int id;
  115. id = connector->encoder_ids[i];
  116. if (!id)
  117. break;
  118. obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
  119. if (!obj)
  120. continue;
  121. nv_encoder = nouveau_encoder(obj_to_encoder(obj));
  122. if (nv_encoder->dcb->i2c_index < 0xf)
  123. i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  124. if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
  125. *pnv_encoder = nv_encoder;
  126. return i2c;
  127. }
  128. }
  129. return NULL;
  130. }
  131. static struct nouveau_encoder *
  132. nouveau_connector_of_detect(struct drm_connector *connector)
  133. {
  134. #ifdef __powerpc__
  135. struct drm_device *dev = connector->dev;
  136. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  137. struct nouveau_encoder *nv_encoder;
  138. struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
  139. if (!dn ||
  140. !((nv_encoder = find_encoder_by_type(connector, OUTPUT_TMDS)) ||
  141. (nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG))))
  142. return NULL;
  143. for_each_child_of_node(dn, cn) {
  144. const char *name = of_get_property(cn, "name", NULL);
  145. const void *edid = of_get_property(cn, "EDID", NULL);
  146. int idx = name ? name[strlen(name) - 1] - 'A' : 0;
  147. if (nv_encoder->dcb->i2c_index == idx && edid) {
  148. nv_connector->edid =
  149. kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  150. of_node_put(cn);
  151. return nv_encoder;
  152. }
  153. }
  154. #endif
  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 (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
  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, bool force)
  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;
  198. /* Cleanup the previous EDID block. */
  199. if (nv_connector->edid) {
  200. drm_mode_connector_update_edid_property(connector, NULL);
  201. kfree(nv_connector->edid);
  202. nv_connector->edid = NULL;
  203. }
  204. i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
  205. if (i2c) {
  206. nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
  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. goto detect_analog;
  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 (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
  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 = nouveau_connector_of_detect(connector);
  242. if (nv_encoder) {
  243. nouveau_connector_set_encoder(connector, nv_encoder);
  244. return connector_status_connected;
  245. }
  246. detect_analog:
  247. nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
  248. if (!nv_encoder && !nouveau_tv_disable)
  249. nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
  250. if (nv_encoder && force) {
  251. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  252. struct drm_encoder_helper_funcs *helper =
  253. encoder->helper_private;
  254. if (helper->detect(encoder, connector) ==
  255. connector_status_connected) {
  256. nouveau_connector_set_encoder(connector, nv_encoder);
  257. return connector_status_connected;
  258. }
  259. }
  260. return connector_status_disconnected;
  261. }
  262. static enum drm_connector_status
  263. nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
  264. {
  265. struct drm_device *dev = connector->dev;
  266. struct drm_nouveau_private *dev_priv = dev->dev_private;
  267. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  268. struct nouveau_encoder *nv_encoder = NULL;
  269. enum drm_connector_status status = connector_status_disconnected;
  270. /* Cleanup the previous EDID block. */
  271. if (nv_connector->edid) {
  272. drm_mode_connector_update_edid_property(connector, NULL);
  273. kfree(nv_connector->edid);
  274. nv_connector->edid = NULL;
  275. }
  276. nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
  277. if (!nv_encoder)
  278. return connector_status_disconnected;
  279. /* Try retrieving EDID via DDC */
  280. if (!dev_priv->vbios.fp_no_ddc) {
  281. status = nouveau_connector_detect(connector, force);
  282. if (status == connector_status_connected)
  283. goto out;
  284. }
  285. /* On some laptops (Sony, i'm looking at you) there appears to
  286. * be no direct way of accessing the panel's EDID. The only
  287. * option available to us appears to be to ask ACPI for help..
  288. *
  289. * It's important this check's before trying straps, one of the
  290. * said manufacturer's laptops are configured in such a way
  291. * the nouveau decides an entry in the VBIOS FP mode table is
  292. * valid - it's not (rh#613284)
  293. */
  294. if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
  295. if (!nouveau_acpi_edid(dev, connector)) {
  296. status = connector_status_connected;
  297. goto out;
  298. }
  299. }
  300. /* If no EDID found above, and the VBIOS indicates a hardcoded
  301. * modeline is avalilable for the panel, set it as the panel's
  302. * native mode and exit.
  303. */
  304. if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
  305. nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
  306. status = connector_status_connected;
  307. goto out;
  308. }
  309. /* Still nothing, some VBIOS images have a hardcoded EDID block
  310. * stored for the panel stored in them.
  311. */
  312. if (!dev_priv->vbios.fp_no_ddc) {
  313. struct edid *edid =
  314. (struct edid *)nouveau_bios_embedded_edid(dev);
  315. if (edid) {
  316. nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  317. *(nv_connector->edid) = *edid;
  318. status = connector_status_connected;
  319. }
  320. }
  321. out:
  322. #if defined(CONFIG_ACPI_BUTTON) || \
  323. (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
  324. if (status == connector_status_connected &&
  325. !nouveau_ignorelid && !acpi_lid_open())
  326. status = connector_status_unknown;
  327. #endif
  328. drm_mode_connector_update_edid_property(connector, nv_connector->edid);
  329. nouveau_connector_set_encoder(connector, nv_encoder);
  330. return status;
  331. }
  332. static void
  333. nouveau_connector_force(struct drm_connector *connector)
  334. {
  335. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  336. struct nouveau_encoder *nv_encoder;
  337. int type;
  338. if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
  339. if (connector->force == DRM_FORCE_ON_DIGITAL)
  340. type = OUTPUT_TMDS;
  341. else
  342. type = OUTPUT_ANALOG;
  343. } else
  344. type = OUTPUT_ANY;
  345. nv_encoder = find_encoder_by_type(connector, type);
  346. if (!nv_encoder) {
  347. NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
  348. drm_get_connector_name(connector));
  349. connector->status = connector_status_disconnected;
  350. return;
  351. }
  352. nouveau_connector_set_encoder(connector, nv_encoder);
  353. }
  354. static int
  355. nouveau_connector_set_property(struct drm_connector *connector,
  356. struct drm_property *property, uint64_t value)
  357. {
  358. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  359. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  360. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  361. struct drm_device *dev = connector->dev;
  362. int ret;
  363. /* Scaling mode */
  364. if (property == dev->mode_config.scaling_mode_property) {
  365. struct nouveau_crtc *nv_crtc = NULL;
  366. bool modeset = false;
  367. switch (value) {
  368. case DRM_MODE_SCALE_NONE:
  369. case DRM_MODE_SCALE_FULLSCREEN:
  370. case DRM_MODE_SCALE_CENTER:
  371. case DRM_MODE_SCALE_ASPECT:
  372. break;
  373. default:
  374. return -EINVAL;
  375. }
  376. /* LVDS always needs gpu scaling */
  377. if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
  378. value == DRM_MODE_SCALE_NONE)
  379. return -EINVAL;
  380. /* Changing between GPU and panel scaling requires a full
  381. * modeset
  382. */
  383. if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
  384. (value == DRM_MODE_SCALE_NONE))
  385. modeset = true;
  386. nv_connector->scaling_mode = value;
  387. if (connector->encoder && connector->encoder->crtc)
  388. nv_crtc = nouveau_crtc(connector->encoder->crtc);
  389. if (!nv_crtc)
  390. return 0;
  391. if (modeset || !nv_crtc->set_scale) {
  392. ret = drm_crtc_helper_set_mode(&nv_crtc->base,
  393. &nv_crtc->base.mode,
  394. nv_crtc->base.x,
  395. nv_crtc->base.y, NULL);
  396. if (!ret)
  397. return -EINVAL;
  398. } else {
  399. ret = nv_crtc->set_scale(nv_crtc, value, true);
  400. if (ret)
  401. return ret;
  402. }
  403. return 0;
  404. }
  405. /* Dithering */
  406. if (property == dev->mode_config.dithering_mode_property) {
  407. struct nouveau_crtc *nv_crtc = NULL;
  408. if (value == DRM_MODE_DITHERING_ON)
  409. nv_connector->use_dithering = true;
  410. else
  411. nv_connector->use_dithering = false;
  412. if (connector->encoder && connector->encoder->crtc)
  413. nv_crtc = nouveau_crtc(connector->encoder->crtc);
  414. if (!nv_crtc || !nv_crtc->set_dither)
  415. return 0;
  416. return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
  417. true);
  418. }
  419. if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
  420. return get_slave_funcs(encoder)->set_property(
  421. encoder, connector, property, value);
  422. return -EINVAL;
  423. }
  424. static struct drm_display_mode *
  425. nouveau_connector_native_mode(struct drm_connector *connector)
  426. {
  427. struct drm_connector_helper_funcs *helper = connector->helper_private;
  428. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  429. struct drm_device *dev = connector->dev;
  430. struct drm_display_mode *mode, *largest = NULL;
  431. int high_w = 0, high_h = 0, high_v = 0;
  432. list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
  433. if (helper->mode_valid(connector, mode) != MODE_OK ||
  434. (mode->flags & DRM_MODE_FLAG_INTERLACE))
  435. continue;
  436. /* Use preferred mode if there is one.. */
  437. if (mode->type & DRM_MODE_TYPE_PREFERRED) {
  438. NV_DEBUG_KMS(dev, "native mode from preferred\n");
  439. return drm_mode_duplicate(dev, mode);
  440. }
  441. /* Otherwise, take the resolution with the largest width, then
  442. * height, then vertical refresh
  443. */
  444. if (mode->hdisplay < high_w)
  445. continue;
  446. if (mode->hdisplay == high_w && mode->vdisplay < high_h)
  447. continue;
  448. if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
  449. mode->vrefresh < high_v)
  450. continue;
  451. high_w = mode->hdisplay;
  452. high_h = mode->vdisplay;
  453. high_v = mode->vrefresh;
  454. largest = mode;
  455. }
  456. NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
  457. high_w, high_h, high_v);
  458. return largest ? drm_mode_duplicate(dev, largest) : NULL;
  459. }
  460. struct moderec {
  461. int hdisplay;
  462. int vdisplay;
  463. };
  464. static struct moderec scaler_modes[] = {
  465. { 1920, 1200 },
  466. { 1920, 1080 },
  467. { 1680, 1050 },
  468. { 1600, 1200 },
  469. { 1400, 1050 },
  470. { 1280, 1024 },
  471. { 1280, 960 },
  472. { 1152, 864 },
  473. { 1024, 768 },
  474. { 800, 600 },
  475. { 720, 400 },
  476. { 640, 480 },
  477. { 640, 400 },
  478. { 640, 350 },
  479. {}
  480. };
  481. static int
  482. nouveau_connector_scaler_modes_add(struct drm_connector *connector)
  483. {
  484. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  485. struct drm_display_mode *native = nv_connector->native_mode, *m;
  486. struct drm_device *dev = connector->dev;
  487. struct moderec *mode = &scaler_modes[0];
  488. int modes = 0;
  489. if (!native)
  490. return 0;
  491. while (mode->hdisplay) {
  492. if (mode->hdisplay <= native->hdisplay &&
  493. mode->vdisplay <= native->vdisplay) {
  494. m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
  495. drm_mode_vrefresh(native), false,
  496. false, false);
  497. if (!m)
  498. continue;
  499. m->type |= DRM_MODE_TYPE_DRIVER;
  500. drm_mode_probed_add(connector, m);
  501. modes++;
  502. }
  503. mode++;
  504. }
  505. return modes;
  506. }
  507. static int
  508. nouveau_connector_get_modes(struct drm_connector *connector)
  509. {
  510. struct drm_device *dev = connector->dev;
  511. struct drm_nouveau_private *dev_priv = dev->dev_private;
  512. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  513. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  514. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  515. int ret = 0;
  516. /* destroy the native mode, the attached monitor could have changed.
  517. */
  518. if (nv_connector->native_mode) {
  519. drm_mode_destroy(dev, nv_connector->native_mode);
  520. nv_connector->native_mode = NULL;
  521. }
  522. if (nv_connector->edid)
  523. ret = drm_add_edid_modes(connector, nv_connector->edid);
  524. else
  525. if (nv_encoder->dcb->type == OUTPUT_LVDS &&
  526. (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
  527. dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
  528. struct drm_display_mode mode;
  529. nouveau_bios_fp_mode(dev, &mode);
  530. nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
  531. }
  532. /* Find the native mode if this is a digital panel, if we didn't
  533. * find any modes through DDC previously add the native mode to
  534. * the list of modes.
  535. */
  536. if (!nv_connector->native_mode)
  537. nv_connector->native_mode =
  538. nouveau_connector_native_mode(connector);
  539. if (ret == 0 && nv_connector->native_mode) {
  540. struct drm_display_mode *mode;
  541. mode = drm_mode_duplicate(dev, nv_connector->native_mode);
  542. drm_mode_probed_add(connector, mode);
  543. ret = 1;
  544. }
  545. if (nv_encoder->dcb->type == OUTPUT_TV)
  546. ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
  547. if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
  548. nv_connector->dcb->type == DCB_CONNECTOR_eDP)
  549. ret += nouveau_connector_scaler_modes_add(connector);
  550. return ret;
  551. }
  552. static unsigned
  553. get_tmds_link_bandwidth(struct drm_connector *connector)
  554. {
  555. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  556. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  557. struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
  558. if (dcb->location != DCB_LOC_ON_CHIP ||
  559. dev_priv->chipset >= 0x46)
  560. return 165000;
  561. else if (dev_priv->chipset >= 0x40)
  562. return 155000;
  563. else if (dev_priv->chipset >= 0x18)
  564. return 135000;
  565. else
  566. return 112000;
  567. }
  568. static int
  569. nouveau_connector_mode_valid(struct drm_connector *connector,
  570. struct drm_display_mode *mode)
  571. {
  572. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  573. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  574. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  575. unsigned min_clock = 25000, max_clock = min_clock;
  576. unsigned clock = mode->clock;
  577. switch (nv_encoder->dcb->type) {
  578. case OUTPUT_LVDS:
  579. if (nv_connector->native_mode &&
  580. (mode->hdisplay > nv_connector->native_mode->hdisplay ||
  581. mode->vdisplay > nv_connector->native_mode->vdisplay))
  582. return MODE_PANEL;
  583. min_clock = 0;
  584. max_clock = 400000;
  585. break;
  586. case OUTPUT_TMDS:
  587. max_clock = get_tmds_link_bandwidth(connector);
  588. if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
  589. max_clock *= 2;
  590. break;
  591. case OUTPUT_ANALOG:
  592. max_clock = nv_encoder->dcb->crtconf.maxfreq;
  593. if (!max_clock)
  594. max_clock = 350000;
  595. break;
  596. case OUTPUT_TV:
  597. return get_slave_funcs(encoder)->mode_valid(encoder, mode);
  598. case OUTPUT_DP:
  599. if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
  600. max_clock = nv_encoder->dp.link_nr * 270000;
  601. else
  602. max_clock = nv_encoder->dp.link_nr * 162000;
  603. clock = clock * nouveau_connector_bpp(connector) / 8;
  604. break;
  605. default:
  606. BUG_ON(1);
  607. return MODE_BAD;
  608. }
  609. if (clock < min_clock)
  610. return MODE_CLOCK_LOW;
  611. if (clock > max_clock)
  612. return MODE_CLOCK_HIGH;
  613. return MODE_OK;
  614. }
  615. static struct drm_encoder *
  616. nouveau_connector_best_encoder(struct drm_connector *connector)
  617. {
  618. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  619. if (nv_connector->detected_encoder)
  620. return to_drm_encoder(nv_connector->detected_encoder);
  621. return NULL;
  622. }
  623. static const struct drm_connector_helper_funcs
  624. nouveau_connector_helper_funcs = {
  625. .get_modes = nouveau_connector_get_modes,
  626. .mode_valid = nouveau_connector_mode_valid,
  627. .best_encoder = nouveau_connector_best_encoder,
  628. };
  629. static const struct drm_connector_funcs
  630. nouveau_connector_funcs = {
  631. .dpms = drm_helper_connector_dpms,
  632. .save = NULL,
  633. .restore = NULL,
  634. .detect = nouveau_connector_detect,
  635. .destroy = nouveau_connector_destroy,
  636. .fill_modes = drm_helper_probe_single_connector_modes,
  637. .set_property = nouveau_connector_set_property,
  638. .force = nouveau_connector_force
  639. };
  640. static const struct drm_connector_funcs
  641. nouveau_connector_funcs_lvds = {
  642. .dpms = drm_helper_connector_dpms,
  643. .save = NULL,
  644. .restore = NULL,
  645. .detect = nouveau_connector_detect_lvds,
  646. .destroy = nouveau_connector_destroy,
  647. .fill_modes = drm_helper_probe_single_connector_modes,
  648. .set_property = nouveau_connector_set_property,
  649. .force = nouveau_connector_force
  650. };
  651. struct drm_connector *
  652. nouveau_connector_create(struct drm_device *dev, int index)
  653. {
  654. const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
  655. struct drm_nouveau_private *dev_priv = dev->dev_private;
  656. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  657. struct nouveau_connector *nv_connector = NULL;
  658. struct dcb_connector_table_entry *dcb = NULL;
  659. struct drm_connector *connector;
  660. int type, ret = 0;
  661. NV_DEBUG_KMS(dev, "\n");
  662. if (index >= dev_priv->vbios.dcb.connector.entries)
  663. return ERR_PTR(-EINVAL);
  664. dcb = &dev_priv->vbios.dcb.connector.entry[index];
  665. if (dcb->drm)
  666. return dcb->drm;
  667. switch (dcb->type) {
  668. case DCB_CONNECTOR_VGA:
  669. type = DRM_MODE_CONNECTOR_VGA;
  670. break;
  671. case DCB_CONNECTOR_TV_0:
  672. case DCB_CONNECTOR_TV_1:
  673. case DCB_CONNECTOR_TV_3:
  674. type = DRM_MODE_CONNECTOR_TV;
  675. break;
  676. case DCB_CONNECTOR_DVI_I:
  677. type = DRM_MODE_CONNECTOR_DVII;
  678. break;
  679. case DCB_CONNECTOR_DVI_D:
  680. type = DRM_MODE_CONNECTOR_DVID;
  681. break;
  682. case DCB_CONNECTOR_HDMI_0:
  683. case DCB_CONNECTOR_HDMI_1:
  684. type = DRM_MODE_CONNECTOR_HDMIA;
  685. break;
  686. case DCB_CONNECTOR_LVDS:
  687. type = DRM_MODE_CONNECTOR_LVDS;
  688. funcs = &nouveau_connector_funcs_lvds;
  689. break;
  690. case DCB_CONNECTOR_DP:
  691. type = DRM_MODE_CONNECTOR_DisplayPort;
  692. break;
  693. case DCB_CONNECTOR_eDP:
  694. type = DRM_MODE_CONNECTOR_eDP;
  695. break;
  696. default:
  697. NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
  698. return ERR_PTR(-EINVAL);
  699. }
  700. nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
  701. if (!nv_connector)
  702. return ERR_PTR(-ENOMEM);
  703. nv_connector->dcb = dcb;
  704. connector = &nv_connector->base;
  705. /* defaults, will get overridden in detect() */
  706. connector->interlace_allowed = false;
  707. connector->doublescan_allowed = false;
  708. drm_connector_init(dev, connector, funcs, type);
  709. drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
  710. /* Check if we need dithering enabled */
  711. if (dcb->type == DCB_CONNECTOR_LVDS) {
  712. bool dummy, is_24bit = false;
  713. ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
  714. if (ret) {
  715. NV_ERROR(dev, "Error parsing LVDS table, disabling "
  716. "LVDS\n");
  717. goto fail;
  718. }
  719. nv_connector->use_dithering = !is_24bit;
  720. }
  721. /* Init DVI-I specific properties */
  722. if (dcb->type == DCB_CONNECTOR_DVI_I) {
  723. drm_mode_create_dvi_i_properties(dev);
  724. drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
  725. drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
  726. }
  727. switch (dcb->type) {
  728. case DCB_CONNECTOR_VGA:
  729. if (dev_priv->card_type >= NV_50) {
  730. drm_connector_attach_property(connector,
  731. dev->mode_config.scaling_mode_property,
  732. nv_connector->scaling_mode);
  733. }
  734. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  735. /* fall-through */
  736. case DCB_CONNECTOR_TV_0:
  737. case DCB_CONNECTOR_TV_1:
  738. case DCB_CONNECTOR_TV_3:
  739. nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
  740. break;
  741. default:
  742. nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
  743. drm_connector_attach_property(connector,
  744. dev->mode_config.scaling_mode_property,
  745. nv_connector->scaling_mode);
  746. drm_connector_attach_property(connector,
  747. dev->mode_config.dithering_mode_property,
  748. nv_connector->use_dithering ?
  749. DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
  750. if (dcb->type != DCB_CONNECTOR_LVDS) {
  751. if (dev_priv->card_type >= NV_50)
  752. connector->polled = DRM_CONNECTOR_POLL_HPD;
  753. else
  754. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  755. }
  756. break;
  757. }
  758. if (pgpio->irq_register) {
  759. pgpio->irq_register(dev, nv_connector->dcb->gpio_tag,
  760. nouveau_connector_hotplug, connector);
  761. }
  762. drm_sysfs_connector_add(connector);
  763. dcb->drm = connector;
  764. return dcb->drm;
  765. fail:
  766. drm_connector_cleanup(connector);
  767. kfree(connector);
  768. return ERR_PTR(ret);
  769. }
  770. static void
  771. nouveau_connector_hotplug(void *data, int plugged)
  772. {
  773. struct drm_connector *connector = data;
  774. struct drm_device *dev = connector->dev;
  775. NV_INFO(dev, "%splugged %s\n", plugged ? "" : "un",
  776. drm_get_connector_name(connector));
  777. if (connector->encoder && connector->encoder->crtc &&
  778. connector->encoder->crtc->enabled) {
  779. struct nouveau_encoder *nv_encoder = nouveau_encoder(connector->encoder);
  780. struct drm_encoder_helper_funcs *helper =
  781. connector->encoder->helper_private;
  782. if (nv_encoder->dcb->type == OUTPUT_DP) {
  783. if (plugged)
  784. helper->dpms(connector->encoder, DRM_MODE_DPMS_ON);
  785. else
  786. helper->dpms(connector->encoder, DRM_MODE_DPMS_OFF);
  787. }
  788. }
  789. drm_helper_hpd_irq_event(dev);
  790. }