intel_dp.c 65 KB

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