intel_dp.c 68 KB

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