intel_dp.c 69 KB

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