intel_dp.c 72 KB

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