intel_dp.c 70 KB

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