intel_dp.c 70 KB

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