intel_dp.c 68 KB

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