nouveau_connector.c 31 KB

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