intel_dp.c 70 KB

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