intel_dp.c 69 KB

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