intel_dp.c 68 KB

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