intel_dp.c 70 KB

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