nouveau_connector.c 26 KB

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