intel_dp.c 82 KB

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