intel_dp.c 99 KB

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