intel_dp.c 70 KB

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