intel_dp.c 69 KB

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