intel_dp.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Keith Packard <keithp@keithp.com>
  25. *
  26. */
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include <linux/export.h>
  30. #include "drmP.h"
  31. #include "drm.h"
  32. #include "drm_crtc.h"
  33. #include "drm_crtc_helper.h"
  34. #include "intel_drv.h"
  35. #include "i915_drm.h"
  36. #include "i915_drv.h"
  37. #include "drm_dp_helper.h"
  38. #define DP_RECEIVER_CAP_SIZE 0xf
  39. #define DP_LINK_STATUS_SIZE 6
  40. #define DP_LINK_CHECK_TIMEOUT (10 * 1000)
  41. #define DP_LINK_CONFIGURATION_SIZE 9
  42. struct intel_dp {
  43. struct intel_encoder base;
  44. uint32_t output_reg;
  45. uint32_t DP;
  46. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
  47. bool has_audio;
  48. int force_audio;
  49. uint32_t color_range;
  50. int dpms_mode;
  51. uint8_t link_bw;
  52. uint8_t lane_count;
  53. uint8_t dpcd[DP_RECEIVER_CAP_SIZE];
  54. struct i2c_adapter adapter;
  55. struct i2c_algo_dp_aux_data algo;
  56. bool is_pch_edp;
  57. uint8_t train_set[4];
  58. int panel_power_up_delay;
  59. int panel_power_down_delay;
  60. int panel_power_cycle_delay;
  61. int backlight_on_delay;
  62. int backlight_off_delay;
  63. struct drm_display_mode *panel_fixed_mode; /* for eDP */
  64. struct delayed_work panel_vdd_work;
  65. bool want_panel_vdd;
  66. };
  67. /**
  68. * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
  69. * @intel_dp: DP struct
  70. *
  71. * If a CPU or PCH DP output is attached to an eDP panel, this function
  72. * will return true, and false otherwise.
  73. */
  74. static bool is_edp(struct intel_dp *intel_dp)
  75. {
  76. return intel_dp->base.type == INTEL_OUTPUT_EDP;
  77. }
  78. /**
  79. * is_pch_edp - is the port on the PCH and attached to an eDP panel?
  80. * @intel_dp: DP struct
  81. *
  82. * Returns true if the given DP struct corresponds to a PCH DP port attached
  83. * to an eDP panel, false otherwise. Helpful for determining whether we
  84. * may need FDI resources for a given DP output or not.
  85. */
  86. static bool is_pch_edp(struct intel_dp *intel_dp)
  87. {
  88. return intel_dp->is_pch_edp;
  89. }
  90. /**
  91. * is_cpu_edp - is the port on the CPU and attached to an eDP panel?
  92. * @intel_dp: DP struct
  93. *
  94. * Returns true if the given DP struct corresponds to a CPU eDP port.
  95. */
  96. static bool is_cpu_edp(struct intel_dp *intel_dp)
  97. {
  98. return is_edp(intel_dp) && !is_pch_edp(intel_dp);
  99. }
  100. static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
  101. {
  102. return container_of(encoder, struct intel_dp, base.base);
  103. }
  104. static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
  105. {
  106. return container_of(intel_attached_encoder(connector),
  107. struct intel_dp, base);
  108. }
  109. /**
  110. * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
  111. * @encoder: DRM encoder
  112. *
  113. * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
  114. * by intel_display.c.
  115. */
  116. bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
  117. {
  118. struct intel_dp *intel_dp;
  119. if (!encoder)
  120. return false;
  121. intel_dp = enc_to_intel_dp(encoder);
  122. return is_pch_edp(intel_dp);
  123. }
  124. static void intel_dp_start_link_train(struct intel_dp *intel_dp);
  125. static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
  126. static void intel_dp_link_down(struct intel_dp *intel_dp);
  127. void
  128. intel_edp_link_config(struct intel_encoder *intel_encoder,
  129. int *lane_num, int *link_bw)
  130. {
  131. struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
  132. *lane_num = intel_dp->lane_count;
  133. if (intel_dp->link_bw == DP_LINK_BW_1_62)
  134. *link_bw = 162000;
  135. else if (intel_dp->link_bw == DP_LINK_BW_2_7)
  136. *link_bw = 270000;
  137. }
  138. static int
  139. intel_dp_max_lane_count(struct intel_dp *intel_dp)
  140. {
  141. int max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
  142. switch (max_lane_count) {
  143. case 1: case 2: case 4:
  144. break;
  145. default:
  146. max_lane_count = 4;
  147. }
  148. return max_lane_count;
  149. }
  150. static int
  151. intel_dp_max_link_bw(struct intel_dp *intel_dp)
  152. {
  153. int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
  154. switch (max_link_bw) {
  155. case DP_LINK_BW_1_62:
  156. case DP_LINK_BW_2_7:
  157. break;
  158. default:
  159. max_link_bw = DP_LINK_BW_1_62;
  160. break;
  161. }
  162. return max_link_bw;
  163. }
  164. static int
  165. intel_dp_link_clock(uint8_t link_bw)
  166. {
  167. if (link_bw == DP_LINK_BW_2_7)
  168. return 270000;
  169. else
  170. return 162000;
  171. }
  172. /*
  173. * The units on the numbers in the next two are... bizarre. Examples will
  174. * make it clearer; this one parallels an example in the eDP spec.
  175. *
  176. * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
  177. *
  178. * 270000 * 1 * 8 / 10 == 216000
  179. *
  180. * The actual data capacity of that configuration is 2.16Gbit/s, so the
  181. * units are decakilobits. ->clock in a drm_display_mode is in kilohertz -
  182. * or equivalently, kilopixels per second - so for 1680x1050R it'd be
  183. * 119000. At 18bpp that's 2142000 kilobits per second.
  184. *
  185. * Thus the strange-looking division by 10 in intel_dp_link_required, to
  186. * get the result in decakilobits instead of kilobits.
  187. */
  188. static int
  189. intel_dp_link_required(struct intel_dp *intel_dp, int pixel_clock, int check_bpp)
  190. {
  191. struct drm_crtc *crtc = intel_dp->base.base.crtc;
  192. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  193. int bpp = 24;
  194. if (check_bpp)
  195. bpp = check_bpp;
  196. else if (intel_crtc)
  197. bpp = intel_crtc->bpp;
  198. return (pixel_clock * bpp + 9) / 10;
  199. }
  200. static int
  201. intel_dp_max_data_rate(int max_link_clock, int max_lanes)
  202. {
  203. return (max_link_clock * max_lanes * 8) / 10;
  204. }
  205. static int
  206. intel_dp_mode_valid(struct drm_connector *connector,
  207. struct drm_display_mode *mode)
  208. {
  209. struct intel_dp *intel_dp = intel_attached_dp(connector);
  210. int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
  211. int max_lanes = intel_dp_max_lane_count(intel_dp);
  212. int max_rate, mode_rate;
  213. if (is_edp(intel_dp) && intel_dp->panel_fixed_mode) {
  214. if (mode->hdisplay > intel_dp->panel_fixed_mode->hdisplay)
  215. return MODE_PANEL;
  216. if (mode->vdisplay > intel_dp->panel_fixed_mode->vdisplay)
  217. return MODE_PANEL;
  218. }
  219. mode_rate = intel_dp_link_required(intel_dp, mode->clock, 0);
  220. max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
  221. if (mode_rate > max_rate) {
  222. mode_rate = intel_dp_link_required(intel_dp,
  223. mode->clock, 18);
  224. if (mode_rate > max_rate)
  225. return MODE_CLOCK_HIGH;
  226. else
  227. mode->private_flags |= INTEL_MODE_DP_FORCE_6BPC;
  228. }
  229. if (mode->clock < 10000)
  230. return MODE_CLOCK_LOW;
  231. return MODE_OK;
  232. }
  233. static uint32_t
  234. pack_aux(uint8_t *src, int src_bytes)
  235. {
  236. int i;
  237. uint32_t v = 0;
  238. if (src_bytes > 4)
  239. src_bytes = 4;
  240. for (i = 0; i < src_bytes; i++)
  241. v |= ((uint32_t) src[i]) << ((3-i) * 8);
  242. return v;
  243. }
  244. static void
  245. unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
  246. {
  247. int i;
  248. if (dst_bytes > 4)
  249. dst_bytes = 4;
  250. for (i = 0; i < dst_bytes; i++)
  251. dst[i] = src >> ((3-i) * 8);
  252. }
  253. /* hrawclock is 1/4 the FSB frequency */
  254. static int
  255. intel_hrawclk(struct drm_device *dev)
  256. {
  257. struct drm_i915_private *dev_priv = dev->dev_private;
  258. uint32_t clkcfg;
  259. clkcfg = I915_READ(CLKCFG);
  260. switch (clkcfg & CLKCFG_FSB_MASK) {
  261. case CLKCFG_FSB_400:
  262. return 100;
  263. case CLKCFG_FSB_533:
  264. return 133;
  265. case CLKCFG_FSB_667:
  266. return 166;
  267. case CLKCFG_FSB_800:
  268. return 200;
  269. case CLKCFG_FSB_1067:
  270. return 266;
  271. case CLKCFG_FSB_1333:
  272. return 333;
  273. /* these two are just a guess; one of them might be right */
  274. case CLKCFG_FSB_1600:
  275. case CLKCFG_FSB_1600_ALT:
  276. return 400;
  277. default:
  278. return 133;
  279. }
  280. }
  281. static bool ironlake_edp_have_panel_power(struct intel_dp *intel_dp)
  282. {
  283. struct drm_device *dev = intel_dp->base.base.dev;
  284. struct drm_i915_private *dev_priv = dev->dev_private;
  285. return (I915_READ(PCH_PP_STATUS) & PP_ON) != 0;
  286. }
  287. static bool ironlake_edp_have_panel_vdd(struct intel_dp *intel_dp)
  288. {
  289. struct drm_device *dev = intel_dp->base.base.dev;
  290. struct drm_i915_private *dev_priv = dev->dev_private;
  291. return (I915_READ(PCH_PP_CONTROL) & EDP_FORCE_VDD) != 0;
  292. }
  293. static void
  294. intel_dp_check_edp(struct intel_dp *intel_dp)
  295. {
  296. struct drm_device *dev = intel_dp->base.base.dev;
  297. struct drm_i915_private *dev_priv = dev->dev_private;
  298. if (!is_edp(intel_dp))
  299. return;
  300. if (!ironlake_edp_have_panel_power(intel_dp) && !ironlake_edp_have_panel_vdd(intel_dp)) {
  301. WARN(1, "eDP powered off while attempting aux channel communication.\n");
  302. DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
  303. I915_READ(PCH_PP_STATUS),
  304. I915_READ(PCH_PP_CONTROL));
  305. }
  306. }
  307. static int
  308. intel_dp_aux_ch(struct intel_dp *intel_dp,
  309. uint8_t *send, int send_bytes,
  310. uint8_t *recv, int recv_size)
  311. {
  312. uint32_t output_reg = intel_dp->output_reg;
  313. struct drm_device *dev = intel_dp->base.base.dev;
  314. struct drm_i915_private *dev_priv = dev->dev_private;
  315. uint32_t ch_ctl = output_reg + 0x10;
  316. uint32_t ch_data = ch_ctl + 4;
  317. int i;
  318. int recv_bytes;
  319. uint32_t status;
  320. uint32_t aux_clock_divider;
  321. int try, precharge;
  322. intel_dp_check_edp(intel_dp);
  323. /* The clock divider is based off the hrawclk,
  324. * and would like to run at 2MHz. So, take the
  325. * hrawclk value and divide by 2 and use that
  326. *
  327. * Note that PCH attached eDP panels should use a 125MHz input
  328. * clock divider.
  329. */
  330. if (is_cpu_edp(intel_dp)) {
  331. if (IS_GEN6(dev) || IS_GEN7(dev))
  332. aux_clock_divider = 200; /* SNB & IVB eDP input clock at 400Mhz */
  333. else
  334. aux_clock_divider = 225; /* eDP input clock at 450Mhz */
  335. } else if (HAS_PCH_SPLIT(dev))
  336. aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
  337. else
  338. aux_clock_divider = intel_hrawclk(dev) / 2;
  339. if (IS_GEN6(dev))
  340. precharge = 3;
  341. else
  342. precharge = 5;
  343. /* Try to wait for any previous AUX channel activity */
  344. for (try = 0; try < 3; try++) {
  345. status = I915_READ(ch_ctl);
  346. if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  347. break;
  348. msleep(1);
  349. }
  350. if (try == 3) {
  351. WARN(1, "dp_aux_ch not started status 0x%08x\n",
  352. I915_READ(ch_ctl));
  353. return -EBUSY;
  354. }
  355. /* Must try at least 3 times according to DP spec */
  356. for (try = 0; try < 5; try++) {
  357. /* Load the send data into the aux channel data registers */
  358. for (i = 0; i < send_bytes; i += 4)
  359. I915_WRITE(ch_data + i,
  360. pack_aux(send + i, send_bytes - i));
  361. /* Send the command and wait for it to complete */
  362. I915_WRITE(ch_ctl,
  363. DP_AUX_CH_CTL_SEND_BUSY |
  364. DP_AUX_CH_CTL_TIME_OUT_400us |
  365. (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  366. (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  367. (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
  368. DP_AUX_CH_CTL_DONE |
  369. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  370. DP_AUX_CH_CTL_RECEIVE_ERROR);
  371. for (;;) {
  372. status = I915_READ(ch_ctl);
  373. if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  374. break;
  375. udelay(100);
  376. }
  377. /* Clear done status and any errors */
  378. I915_WRITE(ch_ctl,
  379. status |
  380. DP_AUX_CH_CTL_DONE |
  381. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  382. DP_AUX_CH_CTL_RECEIVE_ERROR);
  383. if (status & DP_AUX_CH_CTL_DONE)
  384. break;
  385. }
  386. if ((status & DP_AUX_CH_CTL_DONE) == 0) {
  387. DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
  388. return -EBUSY;
  389. }
  390. /* Check for timeout or receive error.
  391. * Timeouts occur when the sink is not connected
  392. */
  393. if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  394. DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
  395. return -EIO;
  396. }
  397. /* Timeouts occur when the device isn't connected, so they're
  398. * "normal" -- don't fill the kernel log with these */
  399. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  400. DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
  401. return -ETIMEDOUT;
  402. }
  403. /* Unload any bytes sent back from the other side */
  404. recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  405. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  406. if (recv_bytes > recv_size)
  407. recv_bytes = recv_size;
  408. for (i = 0; i < recv_bytes; i += 4)
  409. unpack_aux(I915_READ(ch_data + i),
  410. recv + i, recv_bytes - i);
  411. return recv_bytes;
  412. }
  413. /* Write data to the aux channel in native mode */
  414. static int
  415. intel_dp_aux_native_write(struct intel_dp *intel_dp,
  416. uint16_t address, uint8_t *send, int send_bytes)
  417. {
  418. int ret;
  419. uint8_t msg[20];
  420. int msg_bytes;
  421. uint8_t ack;
  422. intel_dp_check_edp(intel_dp);
  423. if (send_bytes > 16)
  424. return -1;
  425. msg[0] = AUX_NATIVE_WRITE << 4;
  426. msg[1] = address >> 8;
  427. msg[2] = address & 0xff;
  428. msg[3] = send_bytes - 1;
  429. memcpy(&msg[4], send, send_bytes);
  430. msg_bytes = send_bytes + 4;
  431. for (;;) {
  432. ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
  433. if (ret < 0)
  434. return ret;
  435. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
  436. break;
  437. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  438. udelay(100);
  439. else
  440. return -EIO;
  441. }
  442. return send_bytes;
  443. }
  444. /* Write a single byte to the aux channel in native mode */
  445. static int
  446. intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
  447. uint16_t address, uint8_t byte)
  448. {
  449. return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
  450. }
  451. /* read bytes from a native aux channel */
  452. static int
  453. intel_dp_aux_native_read(struct intel_dp *intel_dp,
  454. uint16_t address, uint8_t *recv, int recv_bytes)
  455. {
  456. uint8_t msg[4];
  457. int msg_bytes;
  458. uint8_t reply[20];
  459. int reply_bytes;
  460. uint8_t ack;
  461. int ret;
  462. intel_dp_check_edp(intel_dp);
  463. msg[0] = AUX_NATIVE_READ << 4;
  464. msg[1] = address >> 8;
  465. msg[2] = address & 0xff;
  466. msg[3] = recv_bytes - 1;
  467. msg_bytes = 4;
  468. reply_bytes = recv_bytes + 1;
  469. for (;;) {
  470. ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
  471. reply, reply_bytes);
  472. if (ret == 0)
  473. return -EPROTO;
  474. if (ret < 0)
  475. return ret;
  476. ack = reply[0];
  477. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
  478. memcpy(recv, reply + 1, ret - 1);
  479. return ret - 1;
  480. }
  481. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  482. udelay(100);
  483. else
  484. return -EIO;
  485. }
  486. }
  487. static int
  488. intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  489. uint8_t write_byte, uint8_t *read_byte)
  490. {
  491. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  492. struct intel_dp *intel_dp = container_of(adapter,
  493. struct intel_dp,
  494. adapter);
  495. uint16_t address = algo_data->address;
  496. uint8_t msg[5];
  497. uint8_t reply[2];
  498. unsigned retry;
  499. int msg_bytes;
  500. int reply_bytes;
  501. int ret;
  502. intel_dp_check_edp(intel_dp);
  503. /* Set up the command byte */
  504. if (mode & MODE_I2C_READ)
  505. msg[0] = AUX_I2C_READ << 4;
  506. else
  507. msg[0] = AUX_I2C_WRITE << 4;
  508. if (!(mode & MODE_I2C_STOP))
  509. msg[0] |= AUX_I2C_MOT << 4;
  510. msg[1] = address >> 8;
  511. msg[2] = address;
  512. switch (mode) {
  513. case MODE_I2C_WRITE:
  514. msg[3] = 0;
  515. msg[4] = write_byte;
  516. msg_bytes = 5;
  517. reply_bytes = 1;
  518. break;
  519. case MODE_I2C_READ:
  520. msg[3] = 0;
  521. msg_bytes = 4;
  522. reply_bytes = 2;
  523. break;
  524. default:
  525. msg_bytes = 3;
  526. reply_bytes = 1;
  527. break;
  528. }
  529. for (retry = 0; retry < 5; retry++) {
  530. ret = intel_dp_aux_ch(intel_dp,
  531. msg, msg_bytes,
  532. reply, reply_bytes);
  533. if (ret < 0) {
  534. DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
  535. return ret;
  536. }
  537. switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
  538. case AUX_NATIVE_REPLY_ACK:
  539. /* I2C-over-AUX Reply field is only valid
  540. * when paired with AUX ACK.
  541. */
  542. break;
  543. case AUX_NATIVE_REPLY_NACK:
  544. DRM_DEBUG_KMS("aux_ch native nack\n");
  545. return -EREMOTEIO;
  546. case AUX_NATIVE_REPLY_DEFER:
  547. udelay(100);
  548. continue;
  549. default:
  550. DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
  551. reply[0]);
  552. return -EREMOTEIO;
  553. }
  554. switch (reply[0] & AUX_I2C_REPLY_MASK) {
  555. case AUX_I2C_REPLY_ACK:
  556. if (mode == MODE_I2C_READ) {
  557. *read_byte = reply[1];
  558. }
  559. return reply_bytes - 1;
  560. case AUX_I2C_REPLY_NACK:
  561. DRM_DEBUG_KMS("aux_i2c nack\n");
  562. return -EREMOTEIO;
  563. case AUX_I2C_REPLY_DEFER:
  564. DRM_DEBUG_KMS("aux_i2c defer\n");
  565. udelay(100);
  566. break;
  567. default:
  568. DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
  569. return -EREMOTEIO;
  570. }
  571. }
  572. DRM_ERROR("too many retries, giving up\n");
  573. return -EREMOTEIO;
  574. }
  575. static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp);
  576. static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
  577. static int
  578. intel_dp_i2c_init(struct intel_dp *intel_dp,
  579. struct intel_connector *intel_connector, const char *name)
  580. {
  581. int ret;
  582. DRM_DEBUG_KMS("i2c_init %s\n", name);
  583. intel_dp->algo.running = false;
  584. intel_dp->algo.address = 0;
  585. intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
  586. memset(&intel_dp->adapter, '\0', sizeof(intel_dp->adapter));
  587. intel_dp->adapter.owner = THIS_MODULE;
  588. intel_dp->adapter.class = I2C_CLASS_DDC;
  589. strncpy(intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
  590. intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
  591. intel_dp->adapter.algo_data = &intel_dp->algo;
  592. intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
  593. ironlake_edp_panel_vdd_on(intel_dp);
  594. ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
  595. ironlake_edp_panel_vdd_off(intel_dp, false);
  596. return ret;
  597. }
  598. static bool
  599. intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  600. struct drm_display_mode *adjusted_mode)
  601. {
  602. struct drm_device *dev = encoder->dev;
  603. struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
  604. int lane_count, clock;
  605. int max_lane_count = intel_dp_max_lane_count(intel_dp);
  606. int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
  607. int bpp = mode->private_flags & INTEL_MODE_DP_FORCE_6BPC ? 18 : 0;
  608. static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
  609. if (is_edp(intel_dp) && intel_dp->panel_fixed_mode) {
  610. intel_fixed_panel_mode(intel_dp->panel_fixed_mode, adjusted_mode);
  611. intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
  612. mode, adjusted_mode);
  613. /*
  614. * the mode->clock is used to calculate the Data&Link M/N
  615. * of the pipe. For the eDP the fixed clock should be used.
  616. */
  617. mode->clock = intel_dp->panel_fixed_mode->clock;
  618. }
  619. for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
  620. for (clock = 0; clock <= max_clock; clock++) {
  621. int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
  622. if (intel_dp_link_required(intel_dp, mode->clock, bpp)
  623. <= link_avail) {
  624. intel_dp->link_bw = bws[clock];
  625. intel_dp->lane_count = lane_count;
  626. adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
  627. DRM_DEBUG_KMS("Display port link bw %02x lane "
  628. "count %d clock %d\n",
  629. intel_dp->link_bw, intel_dp->lane_count,
  630. adjusted_mode->clock);
  631. return true;
  632. }
  633. }
  634. }
  635. return false;
  636. }
  637. struct intel_dp_m_n {
  638. uint32_t tu;
  639. uint32_t gmch_m;
  640. uint32_t gmch_n;
  641. uint32_t link_m;
  642. uint32_t link_n;
  643. };
  644. static void
  645. intel_reduce_ratio(uint32_t *num, uint32_t *den)
  646. {
  647. while (*num > 0xffffff || *den > 0xffffff) {
  648. *num >>= 1;
  649. *den >>= 1;
  650. }
  651. }
  652. static void
  653. intel_dp_compute_m_n(int bpp,
  654. int nlanes,
  655. int pixel_clock,
  656. int link_clock,
  657. struct intel_dp_m_n *m_n)
  658. {
  659. m_n->tu = 64;
  660. m_n->gmch_m = (pixel_clock * bpp) >> 3;
  661. m_n->gmch_n = link_clock * nlanes;
  662. intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  663. m_n->link_m = pixel_clock;
  664. m_n->link_n = link_clock;
  665. intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  666. }
  667. void
  668. intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
  669. struct drm_display_mode *adjusted_mode)
  670. {
  671. struct drm_device *dev = crtc->dev;
  672. struct drm_mode_config *mode_config = &dev->mode_config;
  673. struct drm_encoder *encoder;
  674. struct drm_i915_private *dev_priv = dev->dev_private;
  675. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  676. int lane_count = 4;
  677. struct intel_dp_m_n m_n;
  678. int pipe = intel_crtc->pipe;
  679. /*
  680. * Find the lane count in the intel_encoder private
  681. */
  682. list_for_each_entry(encoder, &mode_config->encoder_list, head) {
  683. struct intel_dp *intel_dp;
  684. if (encoder->crtc != crtc)
  685. continue;
  686. intel_dp = enc_to_intel_dp(encoder);
  687. if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT ||
  688. intel_dp->base.type == INTEL_OUTPUT_EDP)
  689. {
  690. lane_count = intel_dp->lane_count;
  691. break;
  692. }
  693. }
  694. /*
  695. * Compute the GMCH and Link ratios. The '3' here is
  696. * the number of bytes_per_pixel post-LUT, which we always
  697. * set up for 8-bits of R/G/B, or 3 bytes total.
  698. */
  699. intel_dp_compute_m_n(intel_crtc->bpp, lane_count,
  700. mode->clock, adjusted_mode->clock, &m_n);
  701. if (HAS_PCH_SPLIT(dev)) {
  702. I915_WRITE(TRANSDATA_M1(pipe),
  703. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  704. m_n.gmch_m);
  705. I915_WRITE(TRANSDATA_N1(pipe), m_n.gmch_n);
  706. I915_WRITE(TRANSDPLINK_M1(pipe), m_n.link_m);
  707. I915_WRITE(TRANSDPLINK_N1(pipe), m_n.link_n);
  708. } else {
  709. I915_WRITE(PIPE_GMCH_DATA_M(pipe),
  710. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  711. m_n.gmch_m);
  712. I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
  713. I915_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
  714. I915_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
  715. }
  716. }
  717. static void ironlake_edp_pll_on(struct drm_encoder *encoder);
  718. static void ironlake_edp_pll_off(struct drm_encoder *encoder);
  719. static void
  720. intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  721. struct drm_display_mode *adjusted_mode)
  722. {
  723. struct drm_device *dev = encoder->dev;
  724. struct drm_i915_private *dev_priv = dev->dev_private;
  725. struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
  726. struct drm_crtc *crtc = intel_dp->base.base.crtc;
  727. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  728. /* Turn on the eDP PLL if needed */
  729. if (is_edp(intel_dp)) {
  730. if (!is_pch_edp(intel_dp))
  731. ironlake_edp_pll_on(encoder);
  732. else
  733. ironlake_edp_pll_off(encoder);
  734. }
  735. /*
  736. * There are four kinds of DP registers:
  737. *
  738. * IBX PCH
  739. * SNB CPU
  740. * IVB CPU
  741. * CPT PCH
  742. *
  743. * IBX PCH and CPU are the same for almost everything,
  744. * except that the CPU DP PLL is configured in this
  745. * register
  746. *
  747. * CPT PCH is quite different, having many bits moved
  748. * to the TRANS_DP_CTL register instead. That
  749. * configuration happens (oddly) in ironlake_pch_enable
  750. */
  751. /* Preserve the BIOS-computed detected bit. This is
  752. * supposed to be read-only.
  753. */
  754. intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
  755. intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
  756. /* Handle DP bits in common between all three register formats */
  757. intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
  758. switch (intel_dp->lane_count) {
  759. case 1:
  760. intel_dp->DP |= DP_PORT_WIDTH_1;
  761. break;
  762. case 2:
  763. intel_dp->DP |= DP_PORT_WIDTH_2;
  764. break;
  765. case 4:
  766. intel_dp->DP |= DP_PORT_WIDTH_4;
  767. break;
  768. }
  769. if (intel_dp->has_audio) {
  770. DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
  771. pipe_name(intel_crtc->pipe));
  772. intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
  773. intel_write_eld(encoder, adjusted_mode);
  774. }
  775. memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  776. intel_dp->link_configuration[0] = intel_dp->link_bw;
  777. intel_dp->link_configuration[1] = intel_dp->lane_count;
  778. intel_dp->link_configuration[8] = DP_SET_ANSI_8B10B;
  779. /*
  780. * Check for DPCD version > 1.1 and enhanced framing support
  781. */
  782. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  783. (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
  784. intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  785. }
  786. /* Split out the IBX/CPU vs CPT settings */
  787. if (is_cpu_edp(intel_dp) && IS_GEN7(dev)) {
  788. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  789. intel_dp->DP |= DP_SYNC_HS_HIGH;
  790. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  791. intel_dp->DP |= DP_SYNC_VS_HIGH;
  792. intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
  793. if (intel_dp->link_configuration[1] & DP_LANE_COUNT_ENHANCED_FRAME_EN)
  794. intel_dp->DP |= DP_ENHANCED_FRAMING;
  795. intel_dp->DP |= intel_crtc->pipe << 29;
  796. /* don't miss out required setting for eDP */
  797. intel_dp->DP |= DP_PLL_ENABLE;
  798. if (adjusted_mode->clock < 200000)
  799. intel_dp->DP |= DP_PLL_FREQ_160MHZ;
  800. else
  801. intel_dp->DP |= DP_PLL_FREQ_270MHZ;
  802. } else if (!HAS_PCH_CPT(dev) || is_cpu_edp(intel_dp)) {
  803. intel_dp->DP |= intel_dp->color_range;
  804. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  805. intel_dp->DP |= DP_SYNC_HS_HIGH;
  806. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  807. intel_dp->DP |= DP_SYNC_VS_HIGH;
  808. intel_dp->DP |= DP_LINK_TRAIN_OFF;
  809. if (intel_dp->link_configuration[1] & DP_LANE_COUNT_ENHANCED_FRAME_EN)
  810. intel_dp->DP |= DP_ENHANCED_FRAMING;
  811. if (intel_crtc->pipe == 1)
  812. intel_dp->DP |= DP_PIPEB_SELECT;
  813. if (is_cpu_edp(intel_dp)) {
  814. /* don't miss out required setting for eDP */
  815. intel_dp->DP |= DP_PLL_ENABLE;
  816. if (adjusted_mode->clock < 200000)
  817. intel_dp->DP |= DP_PLL_FREQ_160MHZ;
  818. else
  819. intel_dp->DP |= DP_PLL_FREQ_270MHZ;
  820. }
  821. } else {
  822. intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
  823. }
  824. }
  825. #define IDLE_ON_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
  826. #define IDLE_ON_VALUE (PP_ON | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
  827. #define IDLE_OFF_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
  828. #define IDLE_OFF_VALUE (0 | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
  829. #define IDLE_CYCLE_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
  830. #define IDLE_CYCLE_VALUE (0 | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
  831. static void ironlake_wait_panel_status(struct intel_dp *intel_dp,
  832. u32 mask,
  833. u32 value)
  834. {
  835. struct drm_device *dev = intel_dp->base.base.dev;
  836. struct drm_i915_private *dev_priv = dev->dev_private;
  837. DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
  838. mask, value,
  839. I915_READ(PCH_PP_STATUS),
  840. I915_READ(PCH_PP_CONTROL));
  841. if (_wait_for((I915_READ(PCH_PP_STATUS) & mask) == value, 5000, 10)) {
  842. DRM_ERROR("Panel status timeout: status %08x control %08x\n",
  843. I915_READ(PCH_PP_STATUS),
  844. I915_READ(PCH_PP_CONTROL));
  845. }
  846. }
  847. static void ironlake_wait_panel_on(struct intel_dp *intel_dp)
  848. {
  849. DRM_DEBUG_KMS("Wait for panel power on\n");
  850. ironlake_wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
  851. }
  852. static void ironlake_wait_panel_off(struct intel_dp *intel_dp)
  853. {
  854. DRM_DEBUG_KMS("Wait for panel power off time\n");
  855. ironlake_wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
  856. }
  857. static void ironlake_wait_panel_power_cycle(struct intel_dp *intel_dp)
  858. {
  859. DRM_DEBUG_KMS("Wait for panel power cycle\n");
  860. ironlake_wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
  861. }
  862. /* Read the current pp_control value, unlocking the register if it
  863. * is locked
  864. */
  865. static u32 ironlake_get_pp_control(struct drm_i915_private *dev_priv)
  866. {
  867. u32 control = I915_READ(PCH_PP_CONTROL);
  868. control &= ~PANEL_UNLOCK_MASK;
  869. control |= PANEL_UNLOCK_REGS;
  870. return control;
  871. }
  872. static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
  873. {
  874. struct drm_device *dev = intel_dp->base.base.dev;
  875. struct drm_i915_private *dev_priv = dev->dev_private;
  876. u32 pp;
  877. if (!is_edp(intel_dp))
  878. return;
  879. DRM_DEBUG_KMS("Turn eDP VDD on\n");
  880. WARN(intel_dp->want_panel_vdd,
  881. "eDP VDD already requested on\n");
  882. intel_dp->want_panel_vdd = true;
  883. if (ironlake_edp_have_panel_vdd(intel_dp)) {
  884. DRM_DEBUG_KMS("eDP VDD already on\n");
  885. return;
  886. }
  887. if (!ironlake_edp_have_panel_power(intel_dp))
  888. ironlake_wait_panel_power_cycle(intel_dp);
  889. pp = ironlake_get_pp_control(dev_priv);
  890. pp |= EDP_FORCE_VDD;
  891. I915_WRITE(PCH_PP_CONTROL, pp);
  892. POSTING_READ(PCH_PP_CONTROL);
  893. DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
  894. I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
  895. /*
  896. * If the panel wasn't on, delay before accessing aux channel
  897. */
  898. if (!ironlake_edp_have_panel_power(intel_dp)) {
  899. DRM_DEBUG_KMS("eDP was not running\n");
  900. msleep(intel_dp->panel_power_up_delay);
  901. }
  902. }
  903. static void ironlake_panel_vdd_off_sync(struct intel_dp *intel_dp)
  904. {
  905. struct drm_device *dev = intel_dp->base.base.dev;
  906. struct drm_i915_private *dev_priv = dev->dev_private;
  907. u32 pp;
  908. if (!intel_dp->want_panel_vdd && ironlake_edp_have_panel_vdd(intel_dp)) {
  909. pp = ironlake_get_pp_control(dev_priv);
  910. pp &= ~EDP_FORCE_VDD;
  911. I915_WRITE(PCH_PP_CONTROL, pp);
  912. POSTING_READ(PCH_PP_CONTROL);
  913. /* Make sure sequencer is idle before allowing subsequent activity */
  914. DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
  915. I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
  916. msleep(intel_dp->panel_power_down_delay);
  917. }
  918. }
  919. static void ironlake_panel_vdd_work(struct work_struct *__work)
  920. {
  921. struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
  922. struct intel_dp, panel_vdd_work);
  923. struct drm_device *dev = intel_dp->base.base.dev;
  924. mutex_lock(&dev->mode_config.mutex);
  925. ironlake_panel_vdd_off_sync(intel_dp);
  926. mutex_unlock(&dev->mode_config.mutex);
  927. }
  928. static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
  929. {
  930. if (!is_edp(intel_dp))
  931. return;
  932. DRM_DEBUG_KMS("Turn eDP VDD off %d\n", intel_dp->want_panel_vdd);
  933. WARN(!intel_dp->want_panel_vdd, "eDP VDD not forced on");
  934. intel_dp->want_panel_vdd = false;
  935. if (sync) {
  936. ironlake_panel_vdd_off_sync(intel_dp);
  937. } else {
  938. /*
  939. * Queue the timer to fire a long
  940. * time from now (relative to the power down delay)
  941. * to keep the panel power up across a sequence of operations
  942. */
  943. schedule_delayed_work(&intel_dp->panel_vdd_work,
  944. msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5));
  945. }
  946. }
  947. static void ironlake_edp_panel_on(struct intel_dp *intel_dp)
  948. {
  949. struct drm_device *dev = intel_dp->base.base.dev;
  950. struct drm_i915_private *dev_priv = dev->dev_private;
  951. u32 pp;
  952. if (!is_edp(intel_dp))
  953. return;
  954. DRM_DEBUG_KMS("Turn eDP power on\n");
  955. if (ironlake_edp_have_panel_power(intel_dp)) {
  956. DRM_DEBUG_KMS("eDP power already on\n");
  957. return;
  958. }
  959. ironlake_wait_panel_power_cycle(intel_dp);
  960. pp = ironlake_get_pp_control(dev_priv);
  961. if (IS_GEN5(dev)) {
  962. /* ILK workaround: disable reset around power sequence */
  963. pp &= ~PANEL_POWER_RESET;
  964. I915_WRITE(PCH_PP_CONTROL, pp);
  965. POSTING_READ(PCH_PP_CONTROL);
  966. }
  967. pp |= POWER_TARGET_ON;
  968. if (!IS_GEN5(dev))
  969. pp |= PANEL_POWER_RESET;
  970. I915_WRITE(PCH_PP_CONTROL, pp);
  971. POSTING_READ(PCH_PP_CONTROL);
  972. ironlake_wait_panel_on(intel_dp);
  973. if (IS_GEN5(dev)) {
  974. pp |= PANEL_POWER_RESET; /* restore panel reset bit */
  975. I915_WRITE(PCH_PP_CONTROL, pp);
  976. POSTING_READ(PCH_PP_CONTROL);
  977. }
  978. }
  979. static void ironlake_edp_panel_off(struct intel_dp *intel_dp)
  980. {
  981. struct drm_device *dev = intel_dp->base.base.dev;
  982. struct drm_i915_private *dev_priv = dev->dev_private;
  983. u32 pp;
  984. if (!is_edp(intel_dp))
  985. return;
  986. DRM_DEBUG_KMS("Turn eDP power off\n");
  987. WARN(intel_dp->want_panel_vdd, "Cannot turn power off while VDD is on\n");
  988. pp = ironlake_get_pp_control(dev_priv);
  989. pp &= ~(POWER_TARGET_ON | EDP_FORCE_VDD | PANEL_POWER_RESET | EDP_BLC_ENABLE);
  990. I915_WRITE(PCH_PP_CONTROL, pp);
  991. POSTING_READ(PCH_PP_CONTROL);
  992. ironlake_wait_panel_off(intel_dp);
  993. }
  994. static void ironlake_edp_backlight_on(struct intel_dp *intel_dp)
  995. {
  996. struct drm_device *dev = intel_dp->base.base.dev;
  997. struct drm_i915_private *dev_priv = dev->dev_private;
  998. u32 pp;
  999. if (!is_edp(intel_dp))
  1000. return;
  1001. DRM_DEBUG_KMS("\n");
  1002. /*
  1003. * If we enable the backlight right away following a panel power
  1004. * on, we may see slight flicker as the panel syncs with the eDP
  1005. * link. So delay a bit to make sure the image is solid before
  1006. * allowing it to appear.
  1007. */
  1008. msleep(intel_dp->backlight_on_delay);
  1009. pp = ironlake_get_pp_control(dev_priv);
  1010. pp |= EDP_BLC_ENABLE;
  1011. I915_WRITE(PCH_PP_CONTROL, pp);
  1012. POSTING_READ(PCH_PP_CONTROL);
  1013. }
  1014. static void ironlake_edp_backlight_off(struct intel_dp *intel_dp)
  1015. {
  1016. struct drm_device *dev = intel_dp->base.base.dev;
  1017. struct drm_i915_private *dev_priv = dev->dev_private;
  1018. u32 pp;
  1019. if (!is_edp(intel_dp))
  1020. return;
  1021. DRM_DEBUG_KMS("\n");
  1022. pp = ironlake_get_pp_control(dev_priv);
  1023. pp &= ~EDP_BLC_ENABLE;
  1024. I915_WRITE(PCH_PP_CONTROL, pp);
  1025. POSTING_READ(PCH_PP_CONTROL);
  1026. msleep(intel_dp->backlight_off_delay);
  1027. }
  1028. static void ironlake_edp_pll_on(struct drm_encoder *encoder)
  1029. {
  1030. struct drm_device *dev = encoder->dev;
  1031. struct drm_i915_private *dev_priv = dev->dev_private;
  1032. u32 dpa_ctl;
  1033. DRM_DEBUG_KMS("\n");
  1034. dpa_ctl = I915_READ(DP_A);
  1035. dpa_ctl |= DP_PLL_ENABLE;
  1036. I915_WRITE(DP_A, dpa_ctl);
  1037. POSTING_READ(DP_A);
  1038. udelay(200);
  1039. }
  1040. static void ironlake_edp_pll_off(struct drm_encoder *encoder)
  1041. {
  1042. struct drm_device *dev = encoder->dev;
  1043. struct drm_i915_private *dev_priv = dev->dev_private;
  1044. u32 dpa_ctl;
  1045. dpa_ctl = I915_READ(DP_A);
  1046. dpa_ctl &= ~DP_PLL_ENABLE;
  1047. I915_WRITE(DP_A, dpa_ctl);
  1048. POSTING_READ(DP_A);
  1049. udelay(200);
  1050. }
  1051. /* If the sink supports it, try to set the power state appropriately */
  1052. static void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
  1053. {
  1054. int ret, i;
  1055. /* Should have a valid DPCD by this point */
  1056. if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
  1057. return;
  1058. if (mode != DRM_MODE_DPMS_ON) {
  1059. ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
  1060. DP_SET_POWER_D3);
  1061. if (ret != 1)
  1062. DRM_DEBUG_DRIVER("failed to write sink power state\n");
  1063. } else {
  1064. /*
  1065. * When turning on, we need to retry for 1ms to give the sink
  1066. * time to wake up.
  1067. */
  1068. for (i = 0; i < 3; i++) {
  1069. ret = intel_dp_aux_native_write_1(intel_dp,
  1070. DP_SET_POWER,
  1071. DP_SET_POWER_D0);
  1072. if (ret == 1)
  1073. break;
  1074. msleep(1);
  1075. }
  1076. }
  1077. }
  1078. static void intel_dp_prepare(struct drm_encoder *encoder)
  1079. {
  1080. struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
  1081. ironlake_edp_backlight_off(intel_dp);
  1082. ironlake_edp_panel_off(intel_dp);
  1083. /* Wake up the sink first */
  1084. ironlake_edp_panel_vdd_on(intel_dp);
  1085. intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
  1086. intel_dp_link_down(intel_dp);
  1087. ironlake_edp_panel_vdd_off(intel_dp, false);
  1088. /* Make sure the panel is off before trying to
  1089. * change the mode
  1090. */
  1091. }
  1092. static void intel_dp_commit(struct drm_encoder *encoder)
  1093. {
  1094. struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
  1095. struct drm_device *dev = encoder->dev;
  1096. struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
  1097. ironlake_edp_panel_vdd_on(intel_dp);
  1098. intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
  1099. intel_dp_start_link_train(intel_dp);
  1100. ironlake_edp_panel_on(intel_dp);
  1101. ironlake_edp_panel_vdd_off(intel_dp, true);
  1102. intel_dp_complete_link_train(intel_dp);
  1103. ironlake_edp_backlight_on(intel_dp);
  1104. intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
  1105. if (HAS_PCH_CPT(dev))
  1106. intel_cpt_verify_modeset(dev, intel_crtc->pipe);
  1107. }
  1108. static void
  1109. intel_dp_dpms(struct drm_encoder *encoder, int mode)
  1110. {
  1111. struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
  1112. struct drm_device *dev = encoder->dev;
  1113. struct drm_i915_private *dev_priv = dev->dev_private;
  1114. uint32_t dp_reg = I915_READ(intel_dp->output_reg);
  1115. if (mode != DRM_MODE_DPMS_ON) {
  1116. ironlake_edp_backlight_off(intel_dp);
  1117. ironlake_edp_panel_off(intel_dp);
  1118. ironlake_edp_panel_vdd_on(intel_dp);
  1119. intel_dp_sink_dpms(intel_dp, mode);
  1120. intel_dp_link_down(intel_dp);
  1121. ironlake_edp_panel_vdd_off(intel_dp, false);
  1122. if (is_cpu_edp(intel_dp))
  1123. ironlake_edp_pll_off(encoder);
  1124. } else {
  1125. if (is_cpu_edp(intel_dp))
  1126. ironlake_edp_pll_on(encoder);
  1127. ironlake_edp_panel_vdd_on(intel_dp);
  1128. intel_dp_sink_dpms(intel_dp, mode);
  1129. if (!(dp_reg & DP_PORT_EN)) {
  1130. intel_dp_start_link_train(intel_dp);
  1131. ironlake_edp_panel_on(intel_dp);
  1132. ironlake_edp_panel_vdd_off(intel_dp, true);
  1133. intel_dp_complete_link_train(intel_dp);
  1134. } else
  1135. ironlake_edp_panel_vdd_off(intel_dp, false);
  1136. ironlake_edp_backlight_on(intel_dp);
  1137. }
  1138. intel_dp->dpms_mode = mode;
  1139. }
  1140. /*
  1141. * Native read with retry for link status and receiver capability reads for
  1142. * cases where the sink may still be asleep.
  1143. */
  1144. static bool
  1145. intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
  1146. uint8_t *recv, int recv_bytes)
  1147. {
  1148. int ret, i;
  1149. /*
  1150. * Sinks are *supposed* to come up within 1ms from an off state,
  1151. * but we're also supposed to retry 3 times per the spec.
  1152. */
  1153. for (i = 0; i < 3; i++) {
  1154. ret = intel_dp_aux_native_read(intel_dp, address, recv,
  1155. recv_bytes);
  1156. if (ret == recv_bytes)
  1157. return true;
  1158. msleep(1);
  1159. }
  1160. return false;
  1161. }
  1162. /*
  1163. * Fetch AUX CH registers 0x202 - 0x207 which contain
  1164. * link status information
  1165. */
  1166. static bool
  1167. intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
  1168. {
  1169. return intel_dp_aux_native_read_retry(intel_dp,
  1170. DP_LANE0_1_STATUS,
  1171. link_status,
  1172. DP_LINK_STATUS_SIZE);
  1173. }
  1174. static uint8_t
  1175. intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  1176. int r)
  1177. {
  1178. return link_status[r - DP_LANE0_1_STATUS];
  1179. }
  1180. static uint8_t
  1181. intel_get_adjust_request_voltage(uint8_t adjust_request[2],
  1182. int lane)
  1183. {
  1184. int s = ((lane & 1) ?
  1185. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  1186. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  1187. uint8_t l = adjust_request[lane>>1];
  1188. return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  1189. }
  1190. static uint8_t
  1191. intel_get_adjust_request_pre_emphasis(uint8_t adjust_request[2],
  1192. int lane)
  1193. {
  1194. int s = ((lane & 1) ?
  1195. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  1196. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  1197. uint8_t l = adjust_request[lane>>1];
  1198. return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  1199. }
  1200. #if 0
  1201. static char *voltage_names[] = {
  1202. "0.4V", "0.6V", "0.8V", "1.2V"
  1203. };
  1204. static char *pre_emph_names[] = {
  1205. "0dB", "3.5dB", "6dB", "9.5dB"
  1206. };
  1207. static char *link_train_names[] = {
  1208. "pattern 1", "pattern 2", "idle", "off"
  1209. };
  1210. #endif
  1211. /*
  1212. * These are source-specific values; current Intel hardware supports
  1213. * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
  1214. */
  1215. static uint8_t
  1216. intel_dp_voltage_max(struct intel_dp *intel_dp)
  1217. {
  1218. struct drm_device *dev = intel_dp->base.base.dev;
  1219. if (IS_GEN7(dev) && is_cpu_edp(intel_dp))
  1220. return DP_TRAIN_VOLTAGE_SWING_800;
  1221. else if (HAS_PCH_CPT(dev) && !is_cpu_edp(intel_dp))
  1222. return DP_TRAIN_VOLTAGE_SWING_1200;
  1223. else
  1224. return DP_TRAIN_VOLTAGE_SWING_800;
  1225. }
  1226. static uint8_t
  1227. intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
  1228. {
  1229. struct drm_device *dev = intel_dp->base.base.dev;
  1230. if (IS_GEN7(dev) && is_cpu_edp(intel_dp)) {
  1231. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  1232. case DP_TRAIN_VOLTAGE_SWING_400:
  1233. return DP_TRAIN_PRE_EMPHASIS_6;
  1234. case DP_TRAIN_VOLTAGE_SWING_600:
  1235. case DP_TRAIN_VOLTAGE_SWING_800:
  1236. return DP_TRAIN_PRE_EMPHASIS_3_5;
  1237. default:
  1238. return DP_TRAIN_PRE_EMPHASIS_0;
  1239. }
  1240. } else {
  1241. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  1242. case DP_TRAIN_VOLTAGE_SWING_400:
  1243. return DP_TRAIN_PRE_EMPHASIS_6;
  1244. case DP_TRAIN_VOLTAGE_SWING_600:
  1245. return DP_TRAIN_PRE_EMPHASIS_6;
  1246. case DP_TRAIN_VOLTAGE_SWING_800:
  1247. return DP_TRAIN_PRE_EMPHASIS_3_5;
  1248. case DP_TRAIN_VOLTAGE_SWING_1200:
  1249. default:
  1250. return DP_TRAIN_PRE_EMPHASIS_0;
  1251. }
  1252. }
  1253. }
  1254. static void
  1255. intel_get_adjust_train(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
  1256. {
  1257. uint8_t v = 0;
  1258. uint8_t p = 0;
  1259. int lane;
  1260. uint8_t *adjust_request = link_status + (DP_ADJUST_REQUEST_LANE0_1 - DP_LANE0_1_STATUS);
  1261. uint8_t voltage_max;
  1262. uint8_t preemph_max;
  1263. for (lane = 0; lane < intel_dp->lane_count; lane++) {
  1264. uint8_t this_v = intel_get_adjust_request_voltage(adjust_request, lane);
  1265. uint8_t this_p = intel_get_adjust_request_pre_emphasis(adjust_request, lane);
  1266. if (this_v > v)
  1267. v = this_v;
  1268. if (this_p > p)
  1269. p = this_p;
  1270. }
  1271. voltage_max = intel_dp_voltage_max(intel_dp);
  1272. if (v >= voltage_max)
  1273. v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
  1274. preemph_max = intel_dp_pre_emphasis_max(intel_dp, v);
  1275. if (p >= preemph_max)
  1276. p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  1277. for (lane = 0; lane < 4; lane++)
  1278. intel_dp->train_set[lane] = v | p;
  1279. }
  1280. static uint32_t
  1281. intel_dp_signal_levels(uint8_t train_set)
  1282. {
  1283. uint32_t signal_levels = 0;
  1284. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  1285. case DP_TRAIN_VOLTAGE_SWING_400:
  1286. default:
  1287. signal_levels |= DP_VOLTAGE_0_4;
  1288. break;
  1289. case DP_TRAIN_VOLTAGE_SWING_600:
  1290. signal_levels |= DP_VOLTAGE_0_6;
  1291. break;
  1292. case DP_TRAIN_VOLTAGE_SWING_800:
  1293. signal_levels |= DP_VOLTAGE_0_8;
  1294. break;
  1295. case DP_TRAIN_VOLTAGE_SWING_1200:
  1296. signal_levels |= DP_VOLTAGE_1_2;
  1297. break;
  1298. }
  1299. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  1300. case DP_TRAIN_PRE_EMPHASIS_0:
  1301. default:
  1302. signal_levels |= DP_PRE_EMPHASIS_0;
  1303. break;
  1304. case DP_TRAIN_PRE_EMPHASIS_3_5:
  1305. signal_levels |= DP_PRE_EMPHASIS_3_5;
  1306. break;
  1307. case DP_TRAIN_PRE_EMPHASIS_6:
  1308. signal_levels |= DP_PRE_EMPHASIS_6;
  1309. break;
  1310. case DP_TRAIN_PRE_EMPHASIS_9_5:
  1311. signal_levels |= DP_PRE_EMPHASIS_9_5;
  1312. break;
  1313. }
  1314. return signal_levels;
  1315. }
  1316. /* Gen6's DP voltage swing and pre-emphasis control */
  1317. static uint32_t
  1318. intel_gen6_edp_signal_levels(uint8_t train_set)
  1319. {
  1320. int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
  1321. DP_TRAIN_PRE_EMPHASIS_MASK);
  1322. switch (signal_levels) {
  1323. case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
  1324. case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
  1325. return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
  1326. case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
  1327. return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
  1328. case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
  1329. case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
  1330. return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
  1331. case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
  1332. case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
  1333. return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
  1334. case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
  1335. case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
  1336. return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
  1337. default:
  1338. DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
  1339. "0x%x\n", signal_levels);
  1340. return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
  1341. }
  1342. }
  1343. /* Gen7's DP voltage swing and pre-emphasis control */
  1344. static uint32_t
  1345. intel_gen7_edp_signal_levels(uint8_t train_set)
  1346. {
  1347. int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
  1348. DP_TRAIN_PRE_EMPHASIS_MASK);
  1349. switch (signal_levels) {
  1350. case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
  1351. return EDP_LINK_TRAIN_400MV_0DB_IVB;
  1352. case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
  1353. return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
  1354. case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
  1355. return EDP_LINK_TRAIN_400MV_6DB_IVB;
  1356. case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
  1357. return EDP_LINK_TRAIN_600MV_0DB_IVB;
  1358. case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
  1359. return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
  1360. case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
  1361. return EDP_LINK_TRAIN_800MV_0DB_IVB;
  1362. case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
  1363. return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
  1364. default:
  1365. DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
  1366. "0x%x\n", signal_levels);
  1367. return EDP_LINK_TRAIN_500MV_0DB_IVB;
  1368. }
  1369. }
  1370. static uint8_t
  1371. intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  1372. int lane)
  1373. {
  1374. int s = (lane & 1) * 4;
  1375. uint8_t l = link_status[lane>>1];
  1376. return (l >> s) & 0xf;
  1377. }
  1378. /* Check for clock recovery is done on all channels */
  1379. static bool
  1380. intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  1381. {
  1382. int lane;
  1383. uint8_t lane_status;
  1384. for (lane = 0; lane < lane_count; lane++) {
  1385. lane_status = intel_get_lane_status(link_status, lane);
  1386. if ((lane_status & DP_LANE_CR_DONE) == 0)
  1387. return false;
  1388. }
  1389. return true;
  1390. }
  1391. /* Check to see if channel eq is done on all channels */
  1392. #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
  1393. DP_LANE_CHANNEL_EQ_DONE|\
  1394. DP_LANE_SYMBOL_LOCKED)
  1395. static bool
  1396. intel_channel_eq_ok(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
  1397. {
  1398. uint8_t lane_align;
  1399. uint8_t lane_status;
  1400. int lane;
  1401. lane_align = intel_dp_link_status(link_status,
  1402. DP_LANE_ALIGN_STATUS_UPDATED);
  1403. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  1404. return false;
  1405. for (lane = 0; lane < intel_dp->lane_count; lane++) {
  1406. lane_status = intel_get_lane_status(link_status, lane);
  1407. if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
  1408. return false;
  1409. }
  1410. return true;
  1411. }
  1412. static bool
  1413. intel_dp_set_link_train(struct intel_dp *intel_dp,
  1414. uint32_t dp_reg_value,
  1415. uint8_t dp_train_pat)
  1416. {
  1417. struct drm_device *dev = intel_dp->base.base.dev;
  1418. struct drm_i915_private *dev_priv = dev->dev_private;
  1419. int ret;
  1420. I915_WRITE(intel_dp->output_reg, dp_reg_value);
  1421. POSTING_READ(intel_dp->output_reg);
  1422. intel_dp_aux_native_write_1(intel_dp,
  1423. DP_TRAINING_PATTERN_SET,
  1424. dp_train_pat);
  1425. ret = intel_dp_aux_native_write(intel_dp,
  1426. DP_TRAINING_LANE0_SET,
  1427. intel_dp->train_set,
  1428. intel_dp->lane_count);
  1429. if (ret != intel_dp->lane_count)
  1430. return false;
  1431. return true;
  1432. }
  1433. /* Enable corresponding port and start training pattern 1 */
  1434. static void
  1435. intel_dp_start_link_train(struct intel_dp *intel_dp)
  1436. {
  1437. struct drm_device *dev = intel_dp->base.base.dev;
  1438. struct drm_i915_private *dev_priv = dev->dev_private;
  1439. struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
  1440. int i;
  1441. uint8_t voltage;
  1442. bool clock_recovery = false;
  1443. int voltage_tries, loop_tries;
  1444. u32 reg;
  1445. uint32_t DP = intel_dp->DP;
  1446. /*
  1447. * On CPT we have to enable the port in training pattern 1, which
  1448. * will happen below in intel_dp_set_link_train. Otherwise, enable
  1449. * the port and wait for it to become active.
  1450. */
  1451. if (!HAS_PCH_CPT(dev)) {
  1452. I915_WRITE(intel_dp->output_reg, intel_dp->DP);
  1453. POSTING_READ(intel_dp->output_reg);
  1454. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1455. }
  1456. /* Write the link configuration data */
  1457. intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
  1458. intel_dp->link_configuration,
  1459. DP_LINK_CONFIGURATION_SIZE);
  1460. DP |= DP_PORT_EN;
  1461. if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
  1462. DP &= ~DP_LINK_TRAIN_MASK_CPT;
  1463. else
  1464. DP &= ~DP_LINK_TRAIN_MASK;
  1465. memset(intel_dp->train_set, 0, 4);
  1466. voltage = 0xff;
  1467. voltage_tries = 0;
  1468. loop_tries = 0;
  1469. clock_recovery = false;
  1470. for (;;) {
  1471. /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
  1472. uint8_t link_status[DP_LINK_STATUS_SIZE];
  1473. uint32_t signal_levels;
  1474. if (IS_GEN7(dev) && is_cpu_edp(intel_dp)) {
  1475. signal_levels = intel_gen7_edp_signal_levels(intel_dp->train_set[0]);
  1476. DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_IVB) | signal_levels;
  1477. } else if (IS_GEN6(dev) && is_cpu_edp(intel_dp)) {
  1478. signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
  1479. DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
  1480. } else {
  1481. signal_levels = intel_dp_signal_levels(intel_dp->train_set[0]);
  1482. DRM_DEBUG_KMS("training pattern 1 signal levels %08x\n", signal_levels);
  1483. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  1484. }
  1485. if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
  1486. reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
  1487. else
  1488. reg = DP | DP_LINK_TRAIN_PAT_1;
  1489. if (!intel_dp_set_link_train(intel_dp, reg,
  1490. DP_TRAINING_PATTERN_1 |
  1491. DP_LINK_SCRAMBLING_DISABLE))
  1492. break;
  1493. /* Set training pattern 1 */
  1494. udelay(100);
  1495. if (!intel_dp_get_link_status(intel_dp, link_status)) {
  1496. DRM_ERROR("failed to get link status\n");
  1497. break;
  1498. }
  1499. if (intel_clock_recovery_ok(link_status, intel_dp->lane_count)) {
  1500. DRM_DEBUG_KMS("clock recovery OK\n");
  1501. clock_recovery = true;
  1502. break;
  1503. }
  1504. /* Check to see if we've tried the max voltage */
  1505. for (i = 0; i < intel_dp->lane_count; i++)
  1506. if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  1507. break;
  1508. if (i == intel_dp->lane_count) {
  1509. ++loop_tries;
  1510. if (loop_tries == 5) {
  1511. DRM_DEBUG_KMS("too many full retries, give up\n");
  1512. break;
  1513. }
  1514. memset(intel_dp->train_set, 0, 4);
  1515. voltage_tries = 0;
  1516. continue;
  1517. }
  1518. /* Check to see if we've tried the same voltage 5 times */
  1519. if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  1520. ++voltage_tries;
  1521. if (voltage_tries == 5) {
  1522. DRM_DEBUG_KMS("too many voltage retries, give up\n");
  1523. break;
  1524. }
  1525. } else
  1526. voltage_tries = 0;
  1527. voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  1528. /* Compute new intel_dp->train_set as requested by target */
  1529. intel_get_adjust_train(intel_dp, link_status);
  1530. }
  1531. intel_dp->DP = DP;
  1532. }
  1533. static void
  1534. intel_dp_complete_link_train(struct intel_dp *intel_dp)
  1535. {
  1536. struct drm_device *dev = intel_dp->base.base.dev;
  1537. struct drm_i915_private *dev_priv = dev->dev_private;
  1538. bool channel_eq = false;
  1539. int tries, cr_tries;
  1540. u32 reg;
  1541. uint32_t DP = intel_dp->DP;
  1542. /* channel equalization */
  1543. tries = 0;
  1544. cr_tries = 0;
  1545. channel_eq = false;
  1546. for (;;) {
  1547. /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
  1548. uint32_t signal_levels;
  1549. uint8_t link_status[DP_LINK_STATUS_SIZE];
  1550. if (cr_tries > 5) {
  1551. DRM_ERROR("failed to train DP, aborting\n");
  1552. intel_dp_link_down(intel_dp);
  1553. break;
  1554. }
  1555. if (IS_GEN7(dev) && is_cpu_edp(intel_dp)) {
  1556. signal_levels = intel_gen7_edp_signal_levels(intel_dp->train_set[0]);
  1557. DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_IVB) | signal_levels;
  1558. } else if (IS_GEN6(dev) && is_cpu_edp(intel_dp)) {
  1559. signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
  1560. DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
  1561. } else {
  1562. signal_levels = intel_dp_signal_levels(intel_dp->train_set[0]);
  1563. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  1564. }
  1565. if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
  1566. reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
  1567. else
  1568. reg = DP | DP_LINK_TRAIN_PAT_2;
  1569. /* channel eq pattern */
  1570. if (!intel_dp_set_link_train(intel_dp, reg,
  1571. DP_TRAINING_PATTERN_2 |
  1572. DP_LINK_SCRAMBLING_DISABLE))
  1573. break;
  1574. udelay(400);
  1575. if (!intel_dp_get_link_status(intel_dp, link_status))
  1576. break;
  1577. /* Make sure clock is still ok */
  1578. if (!intel_clock_recovery_ok(link_status, intel_dp->lane_count)) {
  1579. intel_dp_start_link_train(intel_dp);
  1580. cr_tries++;
  1581. continue;
  1582. }
  1583. if (intel_channel_eq_ok(intel_dp, link_status)) {
  1584. channel_eq = true;
  1585. break;
  1586. }
  1587. /* Try 5 times, then try clock recovery if that fails */
  1588. if (tries > 5) {
  1589. intel_dp_link_down(intel_dp);
  1590. intel_dp_start_link_train(intel_dp);
  1591. tries = 0;
  1592. cr_tries++;
  1593. continue;
  1594. }
  1595. /* Compute new intel_dp->train_set as requested by target */
  1596. intel_get_adjust_train(intel_dp, link_status);
  1597. ++tries;
  1598. }
  1599. if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
  1600. reg = DP | DP_LINK_TRAIN_OFF_CPT;
  1601. else
  1602. reg = DP | DP_LINK_TRAIN_OFF;
  1603. I915_WRITE(intel_dp->output_reg, reg);
  1604. POSTING_READ(intel_dp->output_reg);
  1605. intel_dp_aux_native_write_1(intel_dp,
  1606. DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
  1607. }
  1608. static void
  1609. intel_dp_link_down(struct intel_dp *intel_dp)
  1610. {
  1611. struct drm_device *dev = intel_dp->base.base.dev;
  1612. struct drm_i915_private *dev_priv = dev->dev_private;
  1613. uint32_t DP = intel_dp->DP;
  1614. if ((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
  1615. return;
  1616. DRM_DEBUG_KMS("\n");
  1617. if (is_edp(intel_dp)) {
  1618. DP &= ~DP_PLL_ENABLE;
  1619. I915_WRITE(intel_dp->output_reg, DP);
  1620. POSTING_READ(intel_dp->output_reg);
  1621. udelay(100);
  1622. }
  1623. if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp))) {
  1624. DP &= ~DP_LINK_TRAIN_MASK_CPT;
  1625. I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
  1626. } else {
  1627. DP &= ~DP_LINK_TRAIN_MASK;
  1628. I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
  1629. }
  1630. POSTING_READ(intel_dp->output_reg);
  1631. msleep(17);
  1632. if (is_edp(intel_dp)) {
  1633. if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
  1634. DP |= DP_LINK_TRAIN_OFF_CPT;
  1635. else
  1636. DP |= DP_LINK_TRAIN_OFF;
  1637. }
  1638. if (!HAS_PCH_CPT(dev) &&
  1639. I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
  1640. struct drm_crtc *crtc = intel_dp->base.base.crtc;
  1641. /* Hardware workaround: leaving our transcoder select
  1642. * set to transcoder B while it's off will prevent the
  1643. * corresponding HDMI output on transcoder A.
  1644. *
  1645. * Combine this with another hardware workaround:
  1646. * transcoder select bit can only be cleared while the
  1647. * port is enabled.
  1648. */
  1649. DP &= ~DP_PIPEB_SELECT;
  1650. I915_WRITE(intel_dp->output_reg, DP);
  1651. /* Changes to enable or select take place the vblank
  1652. * after being written.
  1653. */
  1654. if (crtc == NULL) {
  1655. /* We can arrive here never having been attached
  1656. * to a CRTC, for instance, due to inheriting
  1657. * random state from the BIOS.
  1658. *
  1659. * If the pipe is not running, play safe and
  1660. * wait for the clocks to stabilise before
  1661. * continuing.
  1662. */
  1663. POSTING_READ(intel_dp->output_reg);
  1664. msleep(50);
  1665. } else
  1666. intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
  1667. }
  1668. DP &= ~DP_AUDIO_OUTPUT_ENABLE;
  1669. I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
  1670. POSTING_READ(intel_dp->output_reg);
  1671. msleep(intel_dp->panel_power_down_delay);
  1672. }
  1673. static bool
  1674. intel_dp_get_dpcd(struct intel_dp *intel_dp)
  1675. {
  1676. if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
  1677. sizeof(intel_dp->dpcd)) &&
  1678. (intel_dp->dpcd[DP_DPCD_REV] != 0)) {
  1679. return true;
  1680. }
  1681. return false;
  1682. }
  1683. static bool
  1684. intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
  1685. {
  1686. int ret;
  1687. ret = intel_dp_aux_native_read_retry(intel_dp,
  1688. DP_DEVICE_SERVICE_IRQ_VECTOR,
  1689. sink_irq_vector, 1);
  1690. if (!ret)
  1691. return false;
  1692. return true;
  1693. }
  1694. static void
  1695. intel_dp_handle_test_request(struct intel_dp *intel_dp)
  1696. {
  1697. /* NAK by default */
  1698. intel_dp_aux_native_write_1(intel_dp, DP_TEST_RESPONSE, DP_TEST_ACK);
  1699. }
  1700. /*
  1701. * According to DP spec
  1702. * 5.1.2:
  1703. * 1. Read DPCD
  1704. * 2. Configure link according to Receiver Capabilities
  1705. * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
  1706. * 4. Check link status on receipt of hot-plug interrupt
  1707. */
  1708. static void
  1709. intel_dp_check_link_status(struct intel_dp *intel_dp)
  1710. {
  1711. u8 sink_irq_vector;
  1712. u8 link_status[DP_LINK_STATUS_SIZE];
  1713. if (intel_dp->dpms_mode != DRM_MODE_DPMS_ON)
  1714. return;
  1715. if (!intel_dp->base.base.crtc)
  1716. return;
  1717. /* Try to read receiver status if the link appears to be up */
  1718. if (!intel_dp_get_link_status(intel_dp, link_status)) {
  1719. intel_dp_link_down(intel_dp);
  1720. return;
  1721. }
  1722. /* Now read the DPCD to see if it's actually running */
  1723. if (!intel_dp_get_dpcd(intel_dp)) {
  1724. intel_dp_link_down(intel_dp);
  1725. return;
  1726. }
  1727. /* Try to read the source of the interrupt */
  1728. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  1729. intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
  1730. /* Clear interrupt source */
  1731. intel_dp_aux_native_write_1(intel_dp,
  1732. DP_DEVICE_SERVICE_IRQ_VECTOR,
  1733. sink_irq_vector);
  1734. if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
  1735. intel_dp_handle_test_request(intel_dp);
  1736. if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
  1737. DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
  1738. }
  1739. if (!intel_channel_eq_ok(intel_dp, link_status)) {
  1740. DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
  1741. drm_get_encoder_name(&intel_dp->base.base));
  1742. intel_dp_start_link_train(intel_dp);
  1743. intel_dp_complete_link_train(intel_dp);
  1744. }
  1745. }
  1746. static enum drm_connector_status
  1747. intel_dp_detect_dpcd(struct intel_dp *intel_dp)
  1748. {
  1749. if (intel_dp_get_dpcd(intel_dp))
  1750. return connector_status_connected;
  1751. return connector_status_disconnected;
  1752. }
  1753. static enum drm_connector_status
  1754. ironlake_dp_detect(struct intel_dp *intel_dp)
  1755. {
  1756. enum drm_connector_status status;
  1757. /* Can't disconnect eDP, but you can close the lid... */
  1758. if (is_edp(intel_dp)) {
  1759. status = intel_panel_detect(intel_dp->base.base.dev);
  1760. if (status == connector_status_unknown)
  1761. status = connector_status_connected;
  1762. return status;
  1763. }
  1764. return intel_dp_detect_dpcd(intel_dp);
  1765. }
  1766. static enum drm_connector_status
  1767. g4x_dp_detect(struct intel_dp *intel_dp)
  1768. {
  1769. struct drm_device *dev = intel_dp->base.base.dev;
  1770. struct drm_i915_private *dev_priv = dev->dev_private;
  1771. uint32_t temp, bit;
  1772. switch (intel_dp->output_reg) {
  1773. case DP_B:
  1774. bit = DPB_HOTPLUG_INT_STATUS;
  1775. break;
  1776. case DP_C:
  1777. bit = DPC_HOTPLUG_INT_STATUS;
  1778. break;
  1779. case DP_D:
  1780. bit = DPD_HOTPLUG_INT_STATUS;
  1781. break;
  1782. default:
  1783. return connector_status_unknown;
  1784. }
  1785. temp = I915_READ(PORT_HOTPLUG_STAT);
  1786. if ((temp & bit) == 0)
  1787. return connector_status_disconnected;
  1788. return intel_dp_detect_dpcd(intel_dp);
  1789. }
  1790. static struct edid *
  1791. intel_dp_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
  1792. {
  1793. struct intel_dp *intel_dp = intel_attached_dp(connector);
  1794. struct edid *edid;
  1795. ironlake_edp_panel_vdd_on(intel_dp);
  1796. edid = drm_get_edid(connector, adapter);
  1797. ironlake_edp_panel_vdd_off(intel_dp, false);
  1798. return edid;
  1799. }
  1800. static int
  1801. intel_dp_get_edid_modes(struct drm_connector *connector, struct i2c_adapter *adapter)
  1802. {
  1803. struct intel_dp *intel_dp = intel_attached_dp(connector);
  1804. int ret;
  1805. ironlake_edp_panel_vdd_on(intel_dp);
  1806. ret = intel_ddc_get_modes(connector, adapter);
  1807. ironlake_edp_panel_vdd_off(intel_dp, false);
  1808. return ret;
  1809. }
  1810. /**
  1811. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
  1812. *
  1813. * \return true if DP port is connected.
  1814. * \return false if DP port is disconnected.
  1815. */
  1816. static enum drm_connector_status
  1817. intel_dp_detect(struct drm_connector *connector, bool force)
  1818. {
  1819. struct intel_dp *intel_dp = intel_attached_dp(connector);
  1820. struct drm_device *dev = intel_dp->base.base.dev;
  1821. enum drm_connector_status status;
  1822. struct edid *edid = NULL;
  1823. intel_dp->has_audio = false;
  1824. if (HAS_PCH_SPLIT(dev))
  1825. status = ironlake_dp_detect(intel_dp);
  1826. else
  1827. status = g4x_dp_detect(intel_dp);
  1828. DRM_DEBUG_KMS("DPCD: %02hx%02hx%02hx%02hx%02hx%02hx%02hx%02hx\n",
  1829. intel_dp->dpcd[0], intel_dp->dpcd[1], intel_dp->dpcd[2],
  1830. intel_dp->dpcd[3], intel_dp->dpcd[4], intel_dp->dpcd[5],
  1831. intel_dp->dpcd[6], intel_dp->dpcd[7]);
  1832. if (status != connector_status_connected)
  1833. return status;
  1834. if (intel_dp->force_audio) {
  1835. intel_dp->has_audio = intel_dp->force_audio > 0;
  1836. } else {
  1837. edid = intel_dp_get_edid(connector, &intel_dp->adapter);
  1838. if (edid) {
  1839. intel_dp->has_audio = drm_detect_monitor_audio(edid);
  1840. connector->display_info.raw_edid = NULL;
  1841. kfree(edid);
  1842. }
  1843. }
  1844. return connector_status_connected;
  1845. }
  1846. static int intel_dp_get_modes(struct drm_connector *connector)
  1847. {
  1848. struct intel_dp *intel_dp = intel_attached_dp(connector);
  1849. struct drm_device *dev = intel_dp->base.base.dev;
  1850. struct drm_i915_private *dev_priv = dev->dev_private;
  1851. int ret;
  1852. /* We should parse the EDID data and find out if it has an audio sink
  1853. */
  1854. ret = intel_dp_get_edid_modes(connector, &intel_dp->adapter);
  1855. if (ret) {
  1856. if (is_edp(intel_dp) && !intel_dp->panel_fixed_mode) {
  1857. struct drm_display_mode *newmode;
  1858. list_for_each_entry(newmode, &connector->probed_modes,
  1859. head) {
  1860. if ((newmode->type & DRM_MODE_TYPE_PREFERRED)) {
  1861. intel_dp->panel_fixed_mode =
  1862. drm_mode_duplicate(dev, newmode);
  1863. break;
  1864. }
  1865. }
  1866. }
  1867. return ret;
  1868. }
  1869. /* if eDP has no EDID, try to use fixed panel mode from VBT */
  1870. if (is_edp(intel_dp)) {
  1871. /* initialize panel mode from VBT if available for eDP */
  1872. if (intel_dp->panel_fixed_mode == NULL && dev_priv->lfp_lvds_vbt_mode != NULL) {
  1873. intel_dp->panel_fixed_mode =
  1874. drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
  1875. if (intel_dp->panel_fixed_mode) {
  1876. intel_dp->panel_fixed_mode->type |=
  1877. DRM_MODE_TYPE_PREFERRED;
  1878. }
  1879. }
  1880. if (intel_dp->panel_fixed_mode) {
  1881. struct drm_display_mode *mode;
  1882. mode = drm_mode_duplicate(dev, intel_dp->panel_fixed_mode);
  1883. drm_mode_probed_add(connector, mode);
  1884. return 1;
  1885. }
  1886. }
  1887. return 0;
  1888. }
  1889. static bool
  1890. intel_dp_detect_audio(struct drm_connector *connector)
  1891. {
  1892. struct intel_dp *intel_dp = intel_attached_dp(connector);
  1893. struct edid *edid;
  1894. bool has_audio = false;
  1895. edid = intel_dp_get_edid(connector, &intel_dp->adapter);
  1896. if (edid) {
  1897. has_audio = drm_detect_monitor_audio(edid);
  1898. connector->display_info.raw_edid = NULL;
  1899. kfree(edid);
  1900. }
  1901. return has_audio;
  1902. }
  1903. static int
  1904. intel_dp_set_property(struct drm_connector *connector,
  1905. struct drm_property *property,
  1906. uint64_t val)
  1907. {
  1908. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1909. struct intel_dp *intel_dp = intel_attached_dp(connector);
  1910. int ret;
  1911. ret = drm_connector_property_set_value(connector, property, val);
  1912. if (ret)
  1913. return ret;
  1914. if (property == dev_priv->force_audio_property) {
  1915. int i = val;
  1916. bool has_audio;
  1917. if (i == intel_dp->force_audio)
  1918. return 0;
  1919. intel_dp->force_audio = i;
  1920. if (i == 0)
  1921. has_audio = intel_dp_detect_audio(connector);
  1922. else
  1923. has_audio = i > 0;
  1924. if (has_audio == intel_dp->has_audio)
  1925. return 0;
  1926. intel_dp->has_audio = has_audio;
  1927. goto done;
  1928. }
  1929. if (property == dev_priv->broadcast_rgb_property) {
  1930. if (val == !!intel_dp->color_range)
  1931. return 0;
  1932. intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
  1933. goto done;
  1934. }
  1935. return -EINVAL;
  1936. done:
  1937. if (intel_dp->base.base.crtc) {
  1938. struct drm_crtc *crtc = intel_dp->base.base.crtc;
  1939. drm_crtc_helper_set_mode(crtc, &crtc->mode,
  1940. crtc->x, crtc->y,
  1941. crtc->fb);
  1942. }
  1943. return 0;
  1944. }
  1945. static void
  1946. intel_dp_destroy(struct drm_connector *connector)
  1947. {
  1948. struct drm_device *dev = connector->dev;
  1949. if (intel_dpd_is_edp(dev))
  1950. intel_panel_destroy_backlight(dev);
  1951. drm_sysfs_connector_remove(connector);
  1952. drm_connector_cleanup(connector);
  1953. kfree(connector);
  1954. }
  1955. static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
  1956. {
  1957. struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
  1958. i2c_del_adapter(&intel_dp->adapter);
  1959. drm_encoder_cleanup(encoder);
  1960. if (is_edp(intel_dp)) {
  1961. cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
  1962. ironlake_panel_vdd_off_sync(intel_dp);
  1963. }
  1964. kfree(intel_dp);
  1965. }
  1966. static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
  1967. .dpms = intel_dp_dpms,
  1968. .mode_fixup = intel_dp_mode_fixup,
  1969. .prepare = intel_dp_prepare,
  1970. .mode_set = intel_dp_mode_set,
  1971. .commit = intel_dp_commit,
  1972. };
  1973. static const struct drm_connector_funcs intel_dp_connector_funcs = {
  1974. .dpms = drm_helper_connector_dpms,
  1975. .detect = intel_dp_detect,
  1976. .fill_modes = drm_helper_probe_single_connector_modes,
  1977. .set_property = intel_dp_set_property,
  1978. .destroy = intel_dp_destroy,
  1979. };
  1980. static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
  1981. .get_modes = intel_dp_get_modes,
  1982. .mode_valid = intel_dp_mode_valid,
  1983. .best_encoder = intel_best_encoder,
  1984. };
  1985. static const struct drm_encoder_funcs intel_dp_enc_funcs = {
  1986. .destroy = intel_dp_encoder_destroy,
  1987. };
  1988. static void
  1989. intel_dp_hot_plug(struct intel_encoder *intel_encoder)
  1990. {
  1991. struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
  1992. intel_dp_check_link_status(intel_dp);
  1993. }
  1994. /* Return which DP Port should be selected for Transcoder DP control */
  1995. int
  1996. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  1997. {
  1998. struct drm_device *dev = crtc->dev;
  1999. struct drm_mode_config *mode_config = &dev->mode_config;
  2000. struct drm_encoder *encoder;
  2001. list_for_each_entry(encoder, &mode_config->encoder_list, head) {
  2002. struct intel_dp *intel_dp;
  2003. if (encoder->crtc != crtc)
  2004. continue;
  2005. intel_dp = enc_to_intel_dp(encoder);
  2006. if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT ||
  2007. intel_dp->base.type == INTEL_OUTPUT_EDP)
  2008. return intel_dp->output_reg;
  2009. }
  2010. return -1;
  2011. }
  2012. /* check the VBT to see whether the eDP is on DP-D port */
  2013. bool intel_dpd_is_edp(struct drm_device *dev)
  2014. {
  2015. struct drm_i915_private *dev_priv = dev->dev_private;
  2016. struct child_device_config *p_child;
  2017. int i;
  2018. if (!dev_priv->child_dev_num)
  2019. return false;
  2020. for (i = 0; i < dev_priv->child_dev_num; i++) {
  2021. p_child = dev_priv->child_dev + i;
  2022. if (p_child->dvo_port == PORT_IDPD &&
  2023. p_child->device_type == DEVICE_TYPE_eDP)
  2024. return true;
  2025. }
  2026. return false;
  2027. }
  2028. static void
  2029. intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
  2030. {
  2031. intel_attach_force_audio_property(connector);
  2032. intel_attach_broadcast_rgb_property(connector);
  2033. }
  2034. void
  2035. intel_dp_init(struct drm_device *dev, int output_reg)
  2036. {
  2037. struct drm_i915_private *dev_priv = dev->dev_private;
  2038. struct drm_connector *connector;
  2039. struct intel_dp *intel_dp;
  2040. struct intel_encoder *intel_encoder;
  2041. struct intel_connector *intel_connector;
  2042. const char *name = NULL;
  2043. int type;
  2044. intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
  2045. if (!intel_dp)
  2046. return;
  2047. intel_dp->output_reg = output_reg;
  2048. intel_dp->dpms_mode = -1;
  2049. intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
  2050. if (!intel_connector) {
  2051. kfree(intel_dp);
  2052. return;
  2053. }
  2054. intel_encoder = &intel_dp->base;
  2055. if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
  2056. if (intel_dpd_is_edp(dev))
  2057. intel_dp->is_pch_edp = true;
  2058. if (output_reg == DP_A || is_pch_edp(intel_dp)) {
  2059. type = DRM_MODE_CONNECTOR_eDP;
  2060. intel_encoder->type = INTEL_OUTPUT_EDP;
  2061. } else {
  2062. type = DRM_MODE_CONNECTOR_DisplayPort;
  2063. intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  2064. }
  2065. connector = &intel_connector->base;
  2066. drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
  2067. drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
  2068. connector->polled = DRM_CONNECTOR_POLL_HPD;
  2069. if (output_reg == DP_B || output_reg == PCH_DP_B)
  2070. intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
  2071. else if (output_reg == DP_C || output_reg == PCH_DP_C)
  2072. intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
  2073. else if (output_reg == DP_D || output_reg == PCH_DP_D)
  2074. intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
  2075. if (is_edp(intel_dp)) {
  2076. intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
  2077. INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
  2078. ironlake_panel_vdd_work);
  2079. }
  2080. intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  2081. connector->interlace_allowed = true;
  2082. connector->doublescan_allowed = 0;
  2083. drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
  2084. DRM_MODE_ENCODER_TMDS);
  2085. drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
  2086. intel_connector_attach_encoder(intel_connector, intel_encoder);
  2087. drm_sysfs_connector_add(connector);
  2088. /* Set up the DDC bus. */
  2089. switch (output_reg) {
  2090. case DP_A:
  2091. name = "DPDDC-A";
  2092. break;
  2093. case DP_B:
  2094. case PCH_DP_B:
  2095. dev_priv->hotplug_supported_mask |=
  2096. HDMIB_HOTPLUG_INT_STATUS;
  2097. name = "DPDDC-B";
  2098. break;
  2099. case DP_C:
  2100. case PCH_DP_C:
  2101. dev_priv->hotplug_supported_mask |=
  2102. HDMIC_HOTPLUG_INT_STATUS;
  2103. name = "DPDDC-C";
  2104. break;
  2105. case DP_D:
  2106. case PCH_DP_D:
  2107. dev_priv->hotplug_supported_mask |=
  2108. HDMID_HOTPLUG_INT_STATUS;
  2109. name = "DPDDC-D";
  2110. break;
  2111. }
  2112. /* Cache some DPCD data in the eDP case */
  2113. if (is_edp(intel_dp)) {
  2114. bool ret;
  2115. struct edp_power_seq cur, vbt;
  2116. u32 pp_on, pp_off, pp_div;
  2117. pp_on = I915_READ(PCH_PP_ON_DELAYS);
  2118. pp_off = I915_READ(PCH_PP_OFF_DELAYS);
  2119. pp_div = I915_READ(PCH_PP_DIVISOR);
  2120. /* Pull timing values out of registers */
  2121. cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
  2122. PANEL_POWER_UP_DELAY_SHIFT;
  2123. cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
  2124. PANEL_LIGHT_ON_DELAY_SHIFT;
  2125. cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
  2126. PANEL_LIGHT_OFF_DELAY_SHIFT;
  2127. cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
  2128. PANEL_POWER_DOWN_DELAY_SHIFT;
  2129. cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
  2130. PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
  2131. DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
  2132. cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
  2133. vbt = dev_priv->edp.pps;
  2134. DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
  2135. vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
  2136. #define get_delay(field) ((max(cur.field, vbt.field) + 9) / 10)
  2137. intel_dp->panel_power_up_delay = get_delay(t1_t3);
  2138. intel_dp->backlight_on_delay = get_delay(t8);
  2139. intel_dp->backlight_off_delay = get_delay(t9);
  2140. intel_dp->panel_power_down_delay = get_delay(t10);
  2141. intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
  2142. DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
  2143. intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
  2144. intel_dp->panel_power_cycle_delay);
  2145. DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
  2146. intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
  2147. ironlake_edp_panel_vdd_on(intel_dp);
  2148. ret = intel_dp_get_dpcd(intel_dp);
  2149. ironlake_edp_panel_vdd_off(intel_dp, false);
  2150. if (ret) {
  2151. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
  2152. dev_priv->no_aux_handshake =
  2153. intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
  2154. DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
  2155. } else {
  2156. /* if this fails, presume the device is a ghost */
  2157. DRM_INFO("failed to retrieve link info, disabling eDP\n");
  2158. intel_dp_encoder_destroy(&intel_dp->base.base);
  2159. intel_dp_destroy(&intel_connector->base);
  2160. return;
  2161. }
  2162. }
  2163. intel_dp_i2c_init(intel_dp, intel_connector, name);
  2164. intel_encoder->hot_plug = intel_dp_hot_plug;
  2165. if (is_edp(intel_dp)) {
  2166. dev_priv->int_edp_connector = connector;
  2167. intel_panel_setup_backlight(dev);
  2168. }
  2169. intel_dp_add_properties(intel_dp, connector);
  2170. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  2171. * 0xd. Failure to do so will result in spurious interrupts being
  2172. * generated on the port when a cable is not attached.
  2173. */
  2174. if (IS_G4X(dev) && !IS_GM45(dev)) {
  2175. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  2176. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  2177. }
  2178. }