intel_dp.c 83 KB

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