intel_dp.c 101 KB

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