intel_dp.c 68 KB

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