nouveau_connector.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. mode->vrefresh = drm_mode_vrefresh(mode);
  434. if (helper->mode_valid(connector, mode) != MODE_OK ||
  435. (mode->flags & DRM_MODE_FLAG_INTERLACE))
  436. continue;
  437. /* Use preferred mode if there is one.. */
  438. if (mode->type & DRM_MODE_TYPE_PREFERRED) {
  439. NV_DEBUG_KMS(dev, "native mode from preferred\n");
  440. return drm_mode_duplicate(dev, mode);
  441. }
  442. /* Otherwise, take the resolution with the largest width, then
  443. * height, then vertical refresh
  444. */
  445. if (mode->hdisplay < high_w)
  446. continue;
  447. if (mode->hdisplay == high_w && mode->vdisplay < high_h)
  448. continue;
  449. if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
  450. mode->vrefresh < high_v)
  451. continue;
  452. high_w = mode->hdisplay;
  453. high_h = mode->vdisplay;
  454. high_v = mode->vrefresh;
  455. largest = mode;
  456. }
  457. NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
  458. high_w, high_h, high_v);
  459. return largest ? drm_mode_duplicate(dev, largest) : NULL;
  460. }
  461. struct moderec {
  462. int hdisplay;
  463. int vdisplay;
  464. };
  465. static struct moderec scaler_modes[] = {
  466. { 1920, 1200 },
  467. { 1920, 1080 },
  468. { 1680, 1050 },
  469. { 1600, 1200 },
  470. { 1400, 1050 },
  471. { 1280, 1024 },
  472. { 1280, 960 },
  473. { 1152, 864 },
  474. { 1024, 768 },
  475. { 800, 600 },
  476. { 720, 400 },
  477. { 640, 480 },
  478. { 640, 400 },
  479. { 640, 350 },
  480. {}
  481. };
  482. static int
  483. nouveau_connector_scaler_modes_add(struct drm_connector *connector)
  484. {
  485. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  486. struct drm_display_mode *native = nv_connector->native_mode, *m;
  487. struct drm_device *dev = connector->dev;
  488. struct moderec *mode = &scaler_modes[0];
  489. int modes = 0;
  490. if (!native)
  491. return 0;
  492. while (mode->hdisplay) {
  493. if (mode->hdisplay <= native->hdisplay &&
  494. mode->vdisplay <= native->vdisplay) {
  495. m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
  496. drm_mode_vrefresh(native), false,
  497. false, false);
  498. if (!m)
  499. continue;
  500. m->type |= DRM_MODE_TYPE_DRIVER;
  501. drm_mode_probed_add(connector, m);
  502. modes++;
  503. }
  504. mode++;
  505. }
  506. return modes;
  507. }
  508. static int
  509. nouveau_connector_get_modes(struct drm_connector *connector)
  510. {
  511. struct drm_device *dev = connector->dev;
  512. struct drm_nouveau_private *dev_priv = dev->dev_private;
  513. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  514. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  515. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  516. int ret = 0;
  517. /* destroy the native mode, the attached monitor could have changed.
  518. */
  519. if (nv_connector->native_mode) {
  520. drm_mode_destroy(dev, nv_connector->native_mode);
  521. nv_connector->native_mode = NULL;
  522. }
  523. if (nv_connector->edid)
  524. ret = drm_add_edid_modes(connector, nv_connector->edid);
  525. else
  526. if (nv_encoder->dcb->type == OUTPUT_LVDS &&
  527. (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
  528. dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
  529. struct drm_display_mode mode;
  530. nouveau_bios_fp_mode(dev, &mode);
  531. nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
  532. }
  533. /* Find the native mode if this is a digital panel, if we didn't
  534. * find any modes through DDC previously add the native mode to
  535. * the list of modes.
  536. */
  537. if (!nv_connector->native_mode)
  538. nv_connector->native_mode =
  539. nouveau_connector_native_mode(connector);
  540. if (ret == 0 && nv_connector->native_mode) {
  541. struct drm_display_mode *mode;
  542. mode = drm_mode_duplicate(dev, nv_connector->native_mode);
  543. drm_mode_probed_add(connector, mode);
  544. ret = 1;
  545. }
  546. if (nv_encoder->dcb->type == OUTPUT_TV)
  547. ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
  548. if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
  549. nv_connector->dcb->type == DCB_CONNECTOR_eDP)
  550. ret += nouveau_connector_scaler_modes_add(connector);
  551. return ret;
  552. }
  553. static unsigned
  554. get_tmds_link_bandwidth(struct drm_connector *connector)
  555. {
  556. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  557. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  558. struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
  559. if (dcb->location != DCB_LOC_ON_CHIP ||
  560. dev_priv->chipset >= 0x46)
  561. return 165000;
  562. else if (dev_priv->chipset >= 0x40)
  563. return 155000;
  564. else if (dev_priv->chipset >= 0x18)
  565. return 135000;
  566. else
  567. return 112000;
  568. }
  569. static int
  570. nouveau_connector_mode_valid(struct drm_connector *connector,
  571. struct drm_display_mode *mode)
  572. {
  573. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  574. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  575. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  576. unsigned min_clock = 25000, max_clock = min_clock;
  577. unsigned clock = mode->clock;
  578. switch (nv_encoder->dcb->type) {
  579. case OUTPUT_LVDS:
  580. if (nv_connector->native_mode &&
  581. (mode->hdisplay > nv_connector->native_mode->hdisplay ||
  582. mode->vdisplay > nv_connector->native_mode->vdisplay))
  583. return MODE_PANEL;
  584. min_clock = 0;
  585. max_clock = 400000;
  586. break;
  587. case OUTPUT_TMDS:
  588. max_clock = get_tmds_link_bandwidth(connector);
  589. if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
  590. max_clock *= 2;
  591. break;
  592. case OUTPUT_ANALOG:
  593. max_clock = nv_encoder->dcb->crtconf.maxfreq;
  594. if (!max_clock)
  595. max_clock = 350000;
  596. break;
  597. case OUTPUT_TV:
  598. return get_slave_funcs(encoder)->mode_valid(encoder, mode);
  599. case OUTPUT_DP:
  600. if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
  601. max_clock = nv_encoder->dp.link_nr * 270000;
  602. else
  603. max_clock = nv_encoder->dp.link_nr * 162000;
  604. clock = clock * nouveau_connector_bpp(connector) / 8;
  605. break;
  606. default:
  607. BUG_ON(1);
  608. return MODE_BAD;
  609. }
  610. if (clock < min_clock)
  611. return MODE_CLOCK_LOW;
  612. if (clock > max_clock)
  613. return MODE_CLOCK_HIGH;
  614. return MODE_OK;
  615. }
  616. static struct drm_encoder *
  617. nouveau_connector_best_encoder(struct drm_connector *connector)
  618. {
  619. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  620. if (nv_connector->detected_encoder)
  621. return to_drm_encoder(nv_connector->detected_encoder);
  622. return NULL;
  623. }
  624. static const struct drm_connector_helper_funcs
  625. nouveau_connector_helper_funcs = {
  626. .get_modes = nouveau_connector_get_modes,
  627. .mode_valid = nouveau_connector_mode_valid,
  628. .best_encoder = nouveau_connector_best_encoder,
  629. };
  630. static const struct drm_connector_funcs
  631. nouveau_connector_funcs = {
  632. .dpms = drm_helper_connector_dpms,
  633. .save = NULL,
  634. .restore = NULL,
  635. .detect = nouveau_connector_detect,
  636. .destroy = nouveau_connector_destroy,
  637. .fill_modes = drm_helper_probe_single_connector_modes,
  638. .set_property = nouveau_connector_set_property,
  639. .force = nouveau_connector_force
  640. };
  641. static const struct drm_connector_funcs
  642. nouveau_connector_funcs_lvds = {
  643. .dpms = drm_helper_connector_dpms,
  644. .save = NULL,
  645. .restore = NULL,
  646. .detect = nouveau_connector_detect_lvds,
  647. .destroy = nouveau_connector_destroy,
  648. .fill_modes = drm_helper_probe_single_connector_modes,
  649. .set_property = nouveau_connector_set_property,
  650. .force = nouveau_connector_force
  651. };
  652. struct drm_connector *
  653. nouveau_connector_create(struct drm_device *dev, int index)
  654. {
  655. const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
  656. struct drm_nouveau_private *dev_priv = dev->dev_private;
  657. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  658. struct nouveau_connector *nv_connector = NULL;
  659. struct dcb_connector_table_entry *dcb = NULL;
  660. struct drm_connector *connector;
  661. int type, ret = 0;
  662. NV_DEBUG_KMS(dev, "\n");
  663. if (index >= dev_priv->vbios.dcb.connector.entries)
  664. return ERR_PTR(-EINVAL);
  665. dcb = &dev_priv->vbios.dcb.connector.entry[index];
  666. if (dcb->drm)
  667. return dcb->drm;
  668. switch (dcb->type) {
  669. case DCB_CONNECTOR_VGA:
  670. type = DRM_MODE_CONNECTOR_VGA;
  671. break;
  672. case DCB_CONNECTOR_TV_0:
  673. case DCB_CONNECTOR_TV_1:
  674. case DCB_CONNECTOR_TV_3:
  675. type = DRM_MODE_CONNECTOR_TV;
  676. break;
  677. case DCB_CONNECTOR_DVI_I:
  678. type = DRM_MODE_CONNECTOR_DVII;
  679. break;
  680. case DCB_CONNECTOR_DVI_D:
  681. type = DRM_MODE_CONNECTOR_DVID;
  682. break;
  683. case DCB_CONNECTOR_HDMI_0:
  684. case DCB_CONNECTOR_HDMI_1:
  685. type = DRM_MODE_CONNECTOR_HDMIA;
  686. break;
  687. case DCB_CONNECTOR_LVDS:
  688. type = DRM_MODE_CONNECTOR_LVDS;
  689. funcs = &nouveau_connector_funcs_lvds;
  690. break;
  691. case DCB_CONNECTOR_DP:
  692. type = DRM_MODE_CONNECTOR_DisplayPort;
  693. break;
  694. case DCB_CONNECTOR_eDP:
  695. type = DRM_MODE_CONNECTOR_eDP;
  696. break;
  697. default:
  698. NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
  699. return ERR_PTR(-EINVAL);
  700. }
  701. nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
  702. if (!nv_connector)
  703. return ERR_PTR(-ENOMEM);
  704. nv_connector->dcb = dcb;
  705. connector = &nv_connector->base;
  706. /* defaults, will get overridden in detect() */
  707. connector->interlace_allowed = false;
  708. connector->doublescan_allowed = false;
  709. drm_connector_init(dev, connector, funcs, type);
  710. drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
  711. /* Check if we need dithering enabled */
  712. if (dcb->type == DCB_CONNECTOR_LVDS) {
  713. bool dummy, is_24bit = false;
  714. ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
  715. if (ret) {
  716. NV_ERROR(dev, "Error parsing LVDS table, disabling "
  717. "LVDS\n");
  718. goto fail;
  719. }
  720. nv_connector->use_dithering = !is_24bit;
  721. }
  722. /* Init DVI-I specific properties */
  723. if (dcb->type == DCB_CONNECTOR_DVI_I) {
  724. drm_mode_create_dvi_i_properties(dev);
  725. drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
  726. drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
  727. }
  728. switch (dcb->type) {
  729. case DCB_CONNECTOR_VGA:
  730. if (dev_priv->card_type >= NV_50) {
  731. drm_connector_attach_property(connector,
  732. dev->mode_config.scaling_mode_property,
  733. nv_connector->scaling_mode);
  734. }
  735. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  736. /* fall-through */
  737. case DCB_CONNECTOR_TV_0:
  738. case DCB_CONNECTOR_TV_1:
  739. case DCB_CONNECTOR_TV_3:
  740. nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
  741. break;
  742. default:
  743. nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
  744. drm_connector_attach_property(connector,
  745. dev->mode_config.scaling_mode_property,
  746. nv_connector->scaling_mode);
  747. drm_connector_attach_property(connector,
  748. dev->mode_config.dithering_mode_property,
  749. nv_connector->use_dithering ?
  750. DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
  751. if (dcb->type != DCB_CONNECTOR_LVDS) {
  752. if (dev_priv->card_type >= NV_50)
  753. connector->polled = DRM_CONNECTOR_POLL_HPD;
  754. else
  755. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  756. }
  757. break;
  758. }
  759. if (pgpio->irq_register) {
  760. pgpio->irq_register(dev, nv_connector->dcb->gpio_tag,
  761. nouveau_connector_hotplug, connector);
  762. }
  763. drm_sysfs_connector_add(connector);
  764. dcb->drm = connector;
  765. return dcb->drm;
  766. fail:
  767. drm_connector_cleanup(connector);
  768. kfree(connector);
  769. return ERR_PTR(ret);
  770. }
  771. static void
  772. nouveau_connector_hotplug(void *data, int plugged)
  773. {
  774. struct drm_connector *connector = data;
  775. struct drm_device *dev = connector->dev;
  776. NV_INFO(dev, "%splugged %s\n", plugged ? "" : "un",
  777. drm_get_connector_name(connector));
  778. if (connector->encoder && connector->encoder->crtc &&
  779. connector->encoder->crtc->enabled) {
  780. struct nouveau_encoder *nv_encoder = nouveau_encoder(connector->encoder);
  781. struct drm_encoder_helper_funcs *helper =
  782. connector->encoder->helper_private;
  783. if (nv_encoder->dcb->type == OUTPUT_DP) {
  784. if (plugged)
  785. helper->dpms(connector->encoder, DRM_MODE_DPMS_ON);
  786. else
  787. helper->dpms(connector->encoder, DRM_MODE_DPMS_OFF);
  788. }
  789. }
  790. drm_helper_hpd_irq_event(dev);
  791. }