intel_dp.c 71 KB

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