nouveau_connector.c 26 KB

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