nouveau_connector.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. 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->hpd,
  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(connector, OUTPUT_TMDS)) ||
  141. (nv_encoder = find_encoder(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 (dev_priv->card_type >= NV_50) {
  168. connector->interlace_allowed = true;
  169. connector->doublescan_allowed = true;
  170. } else
  171. if (nv_encoder->dcb->type == OUTPUT_LVDS ||
  172. nv_encoder->dcb->type == OUTPUT_TMDS) {
  173. connector->doublescan_allowed = false;
  174. connector->interlace_allowed = false;
  175. } else {
  176. connector->doublescan_allowed = true;
  177. if (dev_priv->card_type == NV_20 ||
  178. (dev_priv->card_type == NV_10 &&
  179. (dev->pci_device & 0x0ff0) != 0x0100 &&
  180. (dev->pci_device & 0x0ff0) != 0x0150))
  181. /* HW is broken */
  182. connector->interlace_allowed = false;
  183. else
  184. connector->interlace_allowed = true;
  185. }
  186. if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
  187. drm_connector_property_set_value(connector,
  188. dev->mode_config.dvi_i_subconnector_property,
  189. nv_encoder->dcb->type == OUTPUT_TMDS ?
  190. DRM_MODE_SUBCONNECTOR_DVID :
  191. DRM_MODE_SUBCONNECTOR_DVIA);
  192. }
  193. }
  194. static enum drm_connector_status
  195. nouveau_connector_detect(struct drm_connector *connector, bool force)
  196. {
  197. struct drm_device *dev = connector->dev;
  198. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  199. struct nouveau_encoder *nv_encoder = NULL;
  200. struct nouveau_encoder *nv_partner;
  201. struct nouveau_i2c_chan *i2c;
  202. int type;
  203. /* Cleanup the previous EDID block. */
  204. if (nv_connector->edid) {
  205. drm_mode_connector_update_edid_property(connector, NULL);
  206. kfree(nv_connector->edid);
  207. nv_connector->edid = NULL;
  208. }
  209. i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
  210. if (i2c) {
  211. nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
  212. drm_mode_connector_update_edid_property(connector,
  213. nv_connector->edid);
  214. if (!nv_connector->edid) {
  215. NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
  216. drm_get_connector_name(connector));
  217. goto detect_analog;
  218. }
  219. if (nv_encoder->dcb->type == OUTPUT_DP &&
  220. !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
  221. NV_ERROR(dev, "Detected %s, but failed init\n",
  222. drm_get_connector_name(connector));
  223. return connector_status_disconnected;
  224. }
  225. /* Override encoder type for DVI-I based on whether EDID
  226. * says the display is digital or analog, both use the
  227. * same i2c channel so the value returned from ddc_detect
  228. * isn't necessarily correct.
  229. */
  230. nv_partner = NULL;
  231. if (nv_encoder->dcb->type == OUTPUT_TMDS)
  232. nv_partner = find_encoder(connector, OUTPUT_ANALOG);
  233. if (nv_encoder->dcb->type == OUTPUT_ANALOG)
  234. nv_partner = find_encoder(connector, OUTPUT_TMDS);
  235. if (nv_partner && ((nv_encoder->dcb->type == OUTPUT_ANALOG &&
  236. nv_partner->dcb->type == OUTPUT_TMDS) ||
  237. (nv_encoder->dcb->type == OUTPUT_TMDS &&
  238. nv_partner->dcb->type == OUTPUT_ANALOG))) {
  239. if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
  240. type = OUTPUT_TMDS;
  241. else
  242. type = OUTPUT_ANALOG;
  243. nv_encoder = find_encoder(connector, type);
  244. }
  245. nouveau_connector_set_encoder(connector, nv_encoder);
  246. return connector_status_connected;
  247. }
  248. nv_encoder = nouveau_connector_of_detect(connector);
  249. if (nv_encoder) {
  250. nouveau_connector_set_encoder(connector, nv_encoder);
  251. return connector_status_connected;
  252. }
  253. detect_analog:
  254. nv_encoder = find_encoder(connector, OUTPUT_ANALOG);
  255. if (!nv_encoder && !nouveau_tv_disable)
  256. nv_encoder = find_encoder(connector, OUTPUT_TV);
  257. if (nv_encoder && force) {
  258. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  259. struct drm_encoder_helper_funcs *helper =
  260. encoder->helper_private;
  261. if (helper->detect(encoder, connector) ==
  262. connector_status_connected) {
  263. nouveau_connector_set_encoder(connector, nv_encoder);
  264. return connector_status_connected;
  265. }
  266. }
  267. return connector_status_disconnected;
  268. }
  269. static enum drm_connector_status
  270. nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
  271. {
  272. struct drm_device *dev = connector->dev;
  273. struct drm_nouveau_private *dev_priv = dev->dev_private;
  274. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  275. struct nouveau_encoder *nv_encoder = NULL;
  276. enum drm_connector_status status = connector_status_disconnected;
  277. /* Cleanup the previous EDID block. */
  278. if (nv_connector->edid) {
  279. drm_mode_connector_update_edid_property(connector, NULL);
  280. kfree(nv_connector->edid);
  281. nv_connector->edid = NULL;
  282. }
  283. nv_encoder = find_encoder(connector, OUTPUT_LVDS);
  284. if (!nv_encoder)
  285. return connector_status_disconnected;
  286. /* Try retrieving EDID via DDC */
  287. if (!dev_priv->vbios.fp_no_ddc) {
  288. status = nouveau_connector_detect(connector, force);
  289. if (status == connector_status_connected)
  290. goto out;
  291. }
  292. /* On some laptops (Sony, i'm looking at you) there appears to
  293. * be no direct way of accessing the panel's EDID. The only
  294. * option available to us appears to be to ask ACPI for help..
  295. *
  296. * It's important this check's before trying straps, one of the
  297. * said manufacturer's laptops are configured in such a way
  298. * the nouveau decides an entry in the VBIOS FP mode table is
  299. * valid - it's not (rh#613284)
  300. */
  301. if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
  302. if (!nouveau_acpi_edid(dev, connector)) {
  303. status = connector_status_connected;
  304. goto out;
  305. }
  306. }
  307. /* If no EDID found above, and the VBIOS indicates a hardcoded
  308. * modeline is avalilable for the panel, set it as the panel's
  309. * native mode and exit.
  310. */
  311. if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
  312. nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
  313. status = connector_status_connected;
  314. goto out;
  315. }
  316. /* Still nothing, some VBIOS images have a hardcoded EDID block
  317. * stored for the panel stored in them.
  318. */
  319. if (!dev_priv->vbios.fp_no_ddc) {
  320. struct edid *edid =
  321. (struct edid *)nouveau_bios_embedded_edid(dev);
  322. if (edid) {
  323. nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  324. *(nv_connector->edid) = *edid;
  325. status = connector_status_connected;
  326. }
  327. }
  328. out:
  329. #if defined(CONFIG_ACPI_BUTTON) || \
  330. (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
  331. if (status == connector_status_connected &&
  332. !nouveau_ignorelid && !acpi_lid_open())
  333. status = connector_status_unknown;
  334. #endif
  335. drm_mode_connector_update_edid_property(connector, nv_connector->edid);
  336. nouveau_connector_set_encoder(connector, nv_encoder);
  337. return status;
  338. }
  339. static void
  340. nouveau_connector_force(struct drm_connector *connector)
  341. {
  342. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  343. struct nouveau_encoder *nv_encoder;
  344. int type;
  345. if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
  346. if (connector->force == DRM_FORCE_ON_DIGITAL)
  347. type = OUTPUT_TMDS;
  348. else
  349. type = OUTPUT_ANALOG;
  350. } else
  351. type = OUTPUT_ANY;
  352. nv_encoder = find_encoder(connector, type);
  353. if (!nv_encoder) {
  354. NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
  355. drm_get_connector_name(connector));
  356. connector->status = connector_status_disconnected;
  357. return;
  358. }
  359. nouveau_connector_set_encoder(connector, nv_encoder);
  360. }
  361. static int
  362. nouveau_connector_set_property(struct drm_connector *connector,
  363. struct drm_property *property, uint64_t value)
  364. {
  365. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  366. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  367. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  368. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  369. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  370. struct drm_device *dev = connector->dev;
  371. struct nouveau_crtc *nv_crtc;
  372. int ret;
  373. nv_crtc = NULL;
  374. if (connector->encoder && connector->encoder->crtc)
  375. nv_crtc = nouveau_crtc(connector->encoder->crtc);
  376. /* Scaling mode */
  377. if (property == dev->mode_config.scaling_mode_property) {
  378. bool modeset = false;
  379. switch (value) {
  380. case DRM_MODE_SCALE_NONE:
  381. case DRM_MODE_SCALE_FULLSCREEN:
  382. case DRM_MODE_SCALE_CENTER:
  383. case DRM_MODE_SCALE_ASPECT:
  384. break;
  385. default:
  386. return -EINVAL;
  387. }
  388. /* LVDS always needs gpu scaling */
  389. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
  390. value == DRM_MODE_SCALE_NONE)
  391. return -EINVAL;
  392. /* Changing between GPU and panel scaling requires a full
  393. * modeset
  394. */
  395. if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
  396. (value == DRM_MODE_SCALE_NONE))
  397. modeset = true;
  398. nv_connector->scaling_mode = value;
  399. if (!nv_crtc)
  400. return 0;
  401. if (modeset || !nv_crtc->set_scale) {
  402. ret = drm_crtc_helper_set_mode(&nv_crtc->base,
  403. &nv_crtc->base.mode,
  404. nv_crtc->base.x,
  405. nv_crtc->base.y, NULL);
  406. if (!ret)
  407. return -EINVAL;
  408. } else {
  409. ret = nv_crtc->set_scale(nv_crtc, true);
  410. if (ret)
  411. return ret;
  412. }
  413. return 0;
  414. }
  415. /* Underscan */
  416. if (property == disp->underscan_property) {
  417. if (nv_connector->underscan != value) {
  418. nv_connector->underscan = value;
  419. if (!nv_crtc || !nv_crtc->set_scale)
  420. return 0;
  421. return nv_crtc->set_scale(nv_crtc, true);
  422. }
  423. return 0;
  424. }
  425. if (property == disp->underscan_hborder_property) {
  426. if (nv_connector->underscan_hborder != value) {
  427. nv_connector->underscan_hborder = value;
  428. if (!nv_crtc || !nv_crtc->set_scale)
  429. return 0;
  430. return nv_crtc->set_scale(nv_crtc, true);
  431. }
  432. return 0;
  433. }
  434. if (property == disp->underscan_vborder_property) {
  435. if (nv_connector->underscan_vborder != value) {
  436. nv_connector->underscan_vborder = value;
  437. if (!nv_crtc || !nv_crtc->set_scale)
  438. return 0;
  439. return nv_crtc->set_scale(nv_crtc, true);
  440. }
  441. return 0;
  442. }
  443. /* Dithering */
  444. if (property == disp->dithering_mode) {
  445. nv_connector->dithering_mode = value;
  446. if (!nv_crtc || !nv_crtc->set_dither)
  447. return 0;
  448. return nv_crtc->set_dither(nv_crtc, true);
  449. }
  450. if (property == disp->dithering_depth) {
  451. nv_connector->dithering_depth = value;
  452. if (!nv_crtc || !nv_crtc->set_dither)
  453. return 0;
  454. return nv_crtc->set_dither(nv_crtc, true);
  455. }
  456. if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
  457. return get_slave_funcs(encoder)->set_property(
  458. encoder, connector, property, value);
  459. return -EINVAL;
  460. }
  461. static struct drm_display_mode *
  462. nouveau_connector_native_mode(struct drm_connector *connector)
  463. {
  464. struct drm_connector_helper_funcs *helper = connector->helper_private;
  465. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  466. struct drm_device *dev = connector->dev;
  467. struct drm_display_mode *mode, *largest = NULL;
  468. int high_w = 0, high_h = 0, high_v = 0;
  469. list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
  470. mode->vrefresh = drm_mode_vrefresh(mode);
  471. if (helper->mode_valid(connector, mode) != MODE_OK ||
  472. (mode->flags & DRM_MODE_FLAG_INTERLACE))
  473. continue;
  474. /* Use preferred mode if there is one.. */
  475. if (mode->type & DRM_MODE_TYPE_PREFERRED) {
  476. NV_DEBUG_KMS(dev, "native mode from preferred\n");
  477. return drm_mode_duplicate(dev, mode);
  478. }
  479. /* Otherwise, take the resolution with the largest width, then
  480. * height, then vertical refresh
  481. */
  482. if (mode->hdisplay < high_w)
  483. continue;
  484. if (mode->hdisplay == high_w && mode->vdisplay < high_h)
  485. continue;
  486. if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
  487. mode->vrefresh < high_v)
  488. continue;
  489. high_w = mode->hdisplay;
  490. high_h = mode->vdisplay;
  491. high_v = mode->vrefresh;
  492. largest = mode;
  493. }
  494. NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
  495. high_w, high_h, high_v);
  496. return largest ? drm_mode_duplicate(dev, largest) : NULL;
  497. }
  498. struct moderec {
  499. int hdisplay;
  500. int vdisplay;
  501. };
  502. static struct moderec scaler_modes[] = {
  503. { 1920, 1200 },
  504. { 1920, 1080 },
  505. { 1680, 1050 },
  506. { 1600, 1200 },
  507. { 1400, 1050 },
  508. { 1280, 1024 },
  509. { 1280, 960 },
  510. { 1152, 864 },
  511. { 1024, 768 },
  512. { 800, 600 },
  513. { 720, 400 },
  514. { 640, 480 },
  515. { 640, 400 },
  516. { 640, 350 },
  517. {}
  518. };
  519. static int
  520. nouveau_connector_scaler_modes_add(struct drm_connector *connector)
  521. {
  522. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  523. struct drm_display_mode *native = nv_connector->native_mode, *m;
  524. struct drm_device *dev = connector->dev;
  525. struct moderec *mode = &scaler_modes[0];
  526. int modes = 0;
  527. if (!native)
  528. return 0;
  529. while (mode->hdisplay) {
  530. if (mode->hdisplay <= native->hdisplay &&
  531. mode->vdisplay <= native->vdisplay) {
  532. m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
  533. drm_mode_vrefresh(native), false,
  534. false, false);
  535. if (!m)
  536. continue;
  537. m->type |= DRM_MODE_TYPE_DRIVER;
  538. drm_mode_probed_add(connector, m);
  539. modes++;
  540. }
  541. mode++;
  542. }
  543. return modes;
  544. }
  545. static void
  546. nouveau_connector_detect_depth(struct drm_connector *connector)
  547. {
  548. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  549. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  550. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  551. struct nvbios *bios = &dev_priv->vbios;
  552. struct drm_display_mode *mode = nv_connector->native_mode;
  553. bool duallink;
  554. /* if the edid is feeling nice enough to provide this info, use it */
  555. if (nv_connector->edid && connector->display_info.bpc)
  556. return;
  557. /* if not, we're out of options unless we're LVDS, default to 6bpc */
  558. connector->display_info.bpc = 6;
  559. if (nv_encoder->dcb->type != OUTPUT_LVDS)
  560. return;
  561. /* LVDS: panel straps */
  562. if (bios->fp_no_ddc) {
  563. if (bios->fp.if_is_24bit)
  564. connector->display_info.bpc = 8;
  565. return;
  566. }
  567. /* LVDS: DDC panel, need to first determine the number of links to
  568. * know which if_is_24bit flag to check...
  569. */
  570. if (nv_connector->edid &&
  571. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
  572. duallink = ((u8 *)nv_connector->edid)[121] == 2;
  573. else
  574. duallink = mode->clock >= bios->fp.duallink_transition_clk;
  575. if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
  576. ( duallink && (bios->fp.strapless_is_24bit & 2)))
  577. connector->display_info.bpc = 8;
  578. }
  579. static int
  580. nouveau_connector_get_modes(struct drm_connector *connector)
  581. {
  582. struct drm_device *dev = connector->dev;
  583. struct drm_nouveau_private *dev_priv = dev->dev_private;
  584. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  585. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  586. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  587. int ret = 0;
  588. /* destroy the native mode, the attached monitor could have changed.
  589. */
  590. if (nv_connector->native_mode) {
  591. drm_mode_destroy(dev, nv_connector->native_mode);
  592. nv_connector->native_mode = NULL;
  593. }
  594. if (nv_connector->edid)
  595. ret = drm_add_edid_modes(connector, nv_connector->edid);
  596. else
  597. if (nv_encoder->dcb->type == OUTPUT_LVDS &&
  598. (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
  599. dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
  600. struct drm_display_mode mode;
  601. nouveau_bios_fp_mode(dev, &mode);
  602. nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
  603. }
  604. /* Find the native mode if this is a digital panel, if we didn't
  605. * find any modes through DDC previously add the native mode to
  606. * the list of modes.
  607. */
  608. if (!nv_connector->native_mode)
  609. nv_connector->native_mode =
  610. nouveau_connector_native_mode(connector);
  611. if (ret == 0 && nv_connector->native_mode) {
  612. struct drm_display_mode *mode;
  613. mode = drm_mode_duplicate(dev, nv_connector->native_mode);
  614. drm_mode_probed_add(connector, mode);
  615. ret = 1;
  616. }
  617. /* Attempt to determine display colour depth, this has to happen after
  618. * we've determined the "native" mode for LVDS, as the VBIOS tables
  619. * require us to compare against a pixel clock in some cases..
  620. */
  621. nouveau_connector_detect_depth(connector);
  622. if (nv_encoder->dcb->type == OUTPUT_TV)
  623. ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
  624. if (nv_connector->type == DCB_CONNECTOR_LVDS ||
  625. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
  626. nv_connector->type == DCB_CONNECTOR_eDP)
  627. ret += nouveau_connector_scaler_modes_add(connector);
  628. return ret;
  629. }
  630. static unsigned
  631. get_tmds_link_bandwidth(struct drm_connector *connector)
  632. {
  633. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  634. struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
  635. struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
  636. if (dcb->location != DCB_LOC_ON_CHIP ||
  637. dev_priv->chipset >= 0x46)
  638. return 165000;
  639. else if (dev_priv->chipset >= 0x40)
  640. return 155000;
  641. else if (dev_priv->chipset >= 0x18)
  642. return 135000;
  643. else
  644. return 112000;
  645. }
  646. static int
  647. nouveau_connector_mode_valid(struct drm_connector *connector,
  648. struct drm_display_mode *mode)
  649. {
  650. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  651. struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
  652. struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
  653. unsigned min_clock = 25000, max_clock = min_clock;
  654. unsigned clock = mode->clock;
  655. switch (nv_encoder->dcb->type) {
  656. case OUTPUT_LVDS:
  657. if (nv_connector->native_mode &&
  658. (mode->hdisplay > nv_connector->native_mode->hdisplay ||
  659. mode->vdisplay > nv_connector->native_mode->vdisplay))
  660. return MODE_PANEL;
  661. min_clock = 0;
  662. max_clock = 400000;
  663. break;
  664. case OUTPUT_TMDS:
  665. max_clock = get_tmds_link_bandwidth(connector);
  666. if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
  667. max_clock *= 2;
  668. break;
  669. case OUTPUT_ANALOG:
  670. max_clock = nv_encoder->dcb->crtconf.maxfreq;
  671. if (!max_clock)
  672. max_clock = 350000;
  673. break;
  674. case OUTPUT_TV:
  675. return get_slave_funcs(encoder)->mode_valid(encoder, mode);
  676. case OUTPUT_DP:
  677. max_clock = nv_encoder->dp.link_nr;
  678. max_clock *= nv_encoder->dp.link_bw;
  679. clock = clock * nouveau_connector_bpp(connector) / 10;
  680. break;
  681. default:
  682. BUG_ON(1);
  683. return MODE_BAD;
  684. }
  685. if (clock < min_clock)
  686. return MODE_CLOCK_LOW;
  687. if (clock > max_clock)
  688. return MODE_CLOCK_HIGH;
  689. return MODE_OK;
  690. }
  691. static struct drm_encoder *
  692. nouveau_connector_best_encoder(struct drm_connector *connector)
  693. {
  694. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  695. if (nv_connector->detected_encoder)
  696. return to_drm_encoder(nv_connector->detected_encoder);
  697. return NULL;
  698. }
  699. static const struct drm_connector_helper_funcs
  700. nouveau_connector_helper_funcs = {
  701. .get_modes = nouveau_connector_get_modes,
  702. .mode_valid = nouveau_connector_mode_valid,
  703. .best_encoder = nouveau_connector_best_encoder,
  704. };
  705. static const struct drm_connector_funcs
  706. nouveau_connector_funcs = {
  707. .dpms = drm_helper_connector_dpms,
  708. .save = NULL,
  709. .restore = NULL,
  710. .detect = nouveau_connector_detect,
  711. .destroy = nouveau_connector_destroy,
  712. .fill_modes = drm_helper_probe_single_connector_modes,
  713. .set_property = nouveau_connector_set_property,
  714. .force = nouveau_connector_force
  715. };
  716. static const struct drm_connector_funcs
  717. nouveau_connector_funcs_lvds = {
  718. .dpms = drm_helper_connector_dpms,
  719. .save = NULL,
  720. .restore = NULL,
  721. .detect = nouveau_connector_detect_lvds,
  722. .destroy = nouveau_connector_destroy,
  723. .fill_modes = drm_helper_probe_single_connector_modes,
  724. .set_property = nouveau_connector_set_property,
  725. .force = nouveau_connector_force
  726. };
  727. static int
  728. drm_conntype_from_dcb(enum dcb_connector_type dcb)
  729. {
  730. switch (dcb) {
  731. case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
  732. case DCB_CONNECTOR_TV_0 :
  733. case DCB_CONNECTOR_TV_1 :
  734. case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
  735. case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
  736. case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
  737. case DCB_CONNECTOR_LVDS :
  738. case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
  739. case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort;
  740. case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
  741. case DCB_CONNECTOR_HDMI_0 :
  742. case DCB_CONNECTOR_HDMI_1 : return DRM_MODE_CONNECTOR_HDMIA;
  743. default:
  744. break;
  745. }
  746. return DRM_MODE_CONNECTOR_Unknown;
  747. }
  748. struct drm_connector *
  749. nouveau_connector_create(struct drm_device *dev, int index)
  750. {
  751. const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
  752. struct drm_nouveau_private *dev_priv = dev->dev_private;
  753. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  754. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  755. struct nouveau_connector *nv_connector = NULL;
  756. struct drm_connector *connector;
  757. int type, ret = 0;
  758. bool dummy;
  759. NV_DEBUG_KMS(dev, "\n");
  760. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  761. nv_connector = nouveau_connector(connector);
  762. if (nv_connector->index == index)
  763. return connector;
  764. }
  765. nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
  766. if (!nv_connector)
  767. return ERR_PTR(-ENOMEM);
  768. connector = &nv_connector->base;
  769. nv_connector->index = index;
  770. /* attempt to parse vbios connector type and hotplug gpio */
  771. nv_connector->dcb = dcb_conn(dev, index);
  772. if (nv_connector->dcb) {
  773. static const u8 hpd[16] = {
  774. 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
  775. 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
  776. };
  777. u32 entry = ROM16(nv_connector->dcb[0]);
  778. if (dcb_conntab(dev)[3] >= 4)
  779. entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
  780. nv_connector->hpd = ffs((entry & 0x07033000) >> 12);
  781. nv_connector->hpd = hpd[nv_connector->hpd];
  782. nv_connector->type = nv_connector->dcb[0];
  783. if (drm_conntype_from_dcb(nv_connector->type) ==
  784. DRM_MODE_CONNECTOR_Unknown) {
  785. NV_WARN(dev, "unknown connector type %02x\n",
  786. nv_connector->type);
  787. nv_connector->type = DCB_CONNECTOR_NONE;
  788. }
  789. /* Gigabyte NX85T */
  790. if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
  791. if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
  792. nv_connector->type = DCB_CONNECTOR_DVI_I;
  793. }
  794. /* Gigabyte GV-NX86T512H */
  795. if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
  796. if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
  797. nv_connector->type = DCB_CONNECTOR_DVI_I;
  798. }
  799. } else {
  800. nv_connector->type = DCB_CONNECTOR_NONE;
  801. nv_connector->hpd = DCB_GPIO_UNUSED;
  802. }
  803. /* no vbios data, or an unknown dcb connector type - attempt to
  804. * figure out something suitable ourselves
  805. */
  806. if (nv_connector->type == DCB_CONNECTOR_NONE) {
  807. struct drm_nouveau_private *dev_priv = dev->dev_private;
  808. struct dcb_table *dcbt = &dev_priv->vbios.dcb;
  809. u32 encoders = 0;
  810. int i;
  811. for (i = 0; i < dcbt->entries; i++) {
  812. if (dcbt->entry[i].connector == nv_connector->index)
  813. encoders |= (1 << dcbt->entry[i].type);
  814. }
  815. if (encoders & (1 << OUTPUT_DP)) {
  816. if (encoders & (1 << OUTPUT_TMDS))
  817. nv_connector->type = DCB_CONNECTOR_DP;
  818. else
  819. nv_connector->type = DCB_CONNECTOR_eDP;
  820. } else
  821. if (encoders & (1 << OUTPUT_TMDS)) {
  822. if (encoders & (1 << OUTPUT_ANALOG))
  823. nv_connector->type = DCB_CONNECTOR_DVI_I;
  824. else
  825. nv_connector->type = DCB_CONNECTOR_DVI_D;
  826. } else
  827. if (encoders & (1 << OUTPUT_ANALOG)) {
  828. nv_connector->type = DCB_CONNECTOR_VGA;
  829. } else
  830. if (encoders & (1 << OUTPUT_LVDS)) {
  831. nv_connector->type = DCB_CONNECTOR_LVDS;
  832. } else
  833. if (encoders & (1 << OUTPUT_TV)) {
  834. nv_connector->type = DCB_CONNECTOR_TV_0;
  835. }
  836. }
  837. type = drm_conntype_from_dcb(nv_connector->type);
  838. if (type == DRM_MODE_CONNECTOR_LVDS) {
  839. ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
  840. if (ret) {
  841. NV_ERROR(dev, "Error parsing LVDS table, disabling\n");
  842. kfree(nv_connector);
  843. return ERR_PTR(ret);
  844. }
  845. funcs = &nouveau_connector_funcs_lvds;
  846. } else {
  847. funcs = &nouveau_connector_funcs;
  848. }
  849. /* defaults, will get overridden in detect() */
  850. connector->interlace_allowed = false;
  851. connector->doublescan_allowed = false;
  852. drm_connector_init(dev, connector, funcs, type);
  853. drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
  854. /* Init DVI-I specific properties */
  855. if (nv_connector->type == DCB_CONNECTOR_DVI_I)
  856. drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
  857. /* Add overscan compensation options to digital outputs */
  858. if (disp->underscan_property &&
  859. (nv_connector->type == DCB_CONNECTOR_DVI_D ||
  860. nv_connector->type == DCB_CONNECTOR_DVI_I ||
  861. nv_connector->type == DCB_CONNECTOR_HDMI_0 ||
  862. nv_connector->type == DCB_CONNECTOR_HDMI_1 ||
  863. nv_connector->type == DCB_CONNECTOR_DP)) {
  864. drm_connector_attach_property(connector,
  865. disp->underscan_property,
  866. UNDERSCAN_OFF);
  867. drm_connector_attach_property(connector,
  868. disp->underscan_hborder_property,
  869. 0);
  870. drm_connector_attach_property(connector,
  871. disp->underscan_vborder_property,
  872. 0);
  873. }
  874. switch (nv_connector->type) {
  875. case DCB_CONNECTOR_VGA:
  876. if (dev_priv->card_type >= NV_50) {
  877. drm_connector_attach_property(connector,
  878. dev->mode_config.scaling_mode_property,
  879. nv_connector->scaling_mode);
  880. }
  881. /* fall-through */
  882. case DCB_CONNECTOR_TV_0:
  883. case DCB_CONNECTOR_TV_1:
  884. case DCB_CONNECTOR_TV_3:
  885. nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
  886. break;
  887. default:
  888. nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
  889. drm_connector_attach_property(connector,
  890. dev->mode_config.scaling_mode_property,
  891. nv_connector->scaling_mode);
  892. if (disp->dithering_mode) {
  893. nv_connector->dithering_mode = DITHERING_MODE_AUTO;
  894. drm_connector_attach_property(connector,
  895. disp->dithering_mode,
  896. nv_connector->dithering_mode);
  897. }
  898. if (disp->dithering_depth) {
  899. nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
  900. drm_connector_attach_property(connector,
  901. disp->dithering_depth,
  902. nv_connector->dithering_depth);
  903. }
  904. break;
  905. }
  906. if (nv_connector->hpd != DCB_GPIO_UNUSED && pgpio->irq_register) {
  907. pgpio->irq_register(dev, nv_connector->hpd,
  908. nouveau_connector_hotplug, connector);
  909. connector->polled = DRM_CONNECTOR_POLL_HPD;
  910. } else {
  911. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  912. }
  913. drm_sysfs_connector_add(connector);
  914. return connector;
  915. }
  916. static void
  917. nouveau_connector_hotplug(void *data, int plugged)
  918. {
  919. struct drm_connector *connector = data;
  920. struct drm_device *dev = connector->dev;
  921. NV_DEBUG(dev, "%splugged %s\n", plugged ? "" : "un",
  922. drm_get_connector_name(connector));
  923. if (plugged)
  924. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  925. else
  926. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  927. drm_helper_hpd_irq_event(dev);
  928. }