cdv_intel_dp.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. /*
  2. * Copyright © 2012 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/module.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm_crtc.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include "psb_drv.h"
  34. #include "psb_intel_drv.h"
  35. #include "psb_intel_reg.h"
  36. #include "gma_display.h"
  37. #include <drm/drm_dp_helper.h>
  38. #define _wait_for(COND, MS, W) ({ \
  39. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS); \
  40. int ret__ = 0; \
  41. while (! (COND)) { \
  42. if (time_after(jiffies, timeout__)) { \
  43. ret__ = -ETIMEDOUT; \
  44. break; \
  45. } \
  46. if (W && !in_dbg_master()) msleep(W); \
  47. } \
  48. ret__; \
  49. })
  50. #define wait_for(COND, MS) _wait_for(COND, MS, 1)
  51. #define DP_LINK_STATUS_SIZE 6
  52. #define DP_LINK_CHECK_TIMEOUT (10 * 1000)
  53. #define DP_LINK_CONFIGURATION_SIZE 9
  54. #define CDV_FAST_LINK_TRAIN 1
  55. struct cdv_intel_dp {
  56. uint32_t output_reg;
  57. uint32_t DP;
  58. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
  59. bool has_audio;
  60. int force_audio;
  61. uint32_t color_range;
  62. uint8_t link_bw;
  63. uint8_t lane_count;
  64. uint8_t dpcd[4];
  65. struct gma_encoder *encoder;
  66. struct i2c_adapter adapter;
  67. struct i2c_algo_dp_aux_data algo;
  68. uint8_t train_set[4];
  69. uint8_t link_status[DP_LINK_STATUS_SIZE];
  70. int panel_power_up_delay;
  71. int panel_power_down_delay;
  72. int panel_power_cycle_delay;
  73. int backlight_on_delay;
  74. int backlight_off_delay;
  75. struct drm_display_mode *panel_fixed_mode; /* for eDP */
  76. bool panel_on;
  77. };
  78. struct ddi_regoff {
  79. uint32_t PreEmph1;
  80. uint32_t PreEmph2;
  81. uint32_t VSwing1;
  82. uint32_t VSwing2;
  83. uint32_t VSwing3;
  84. uint32_t VSwing4;
  85. uint32_t VSwing5;
  86. };
  87. static struct ddi_regoff ddi_DP_train_table[] = {
  88. {.PreEmph1 = 0x812c, .PreEmph2 = 0x8124, .VSwing1 = 0x8154,
  89. .VSwing2 = 0x8148, .VSwing3 = 0x814C, .VSwing4 = 0x8150,
  90. .VSwing5 = 0x8158,},
  91. {.PreEmph1 = 0x822c, .PreEmph2 = 0x8224, .VSwing1 = 0x8254,
  92. .VSwing2 = 0x8248, .VSwing3 = 0x824C, .VSwing4 = 0x8250,
  93. .VSwing5 = 0x8258,},
  94. };
  95. static uint32_t dp_vswing_premph_table[] = {
  96. 0x55338954, 0x4000,
  97. 0x554d8954, 0x2000,
  98. 0x55668954, 0,
  99. 0x559ac0d4, 0x6000,
  100. };
  101. /**
  102. * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
  103. * @intel_dp: DP struct
  104. *
  105. * If a CPU or PCH DP output is attached to an eDP panel, this function
  106. * will return true, and false otherwise.
  107. */
  108. static bool is_edp(struct gma_encoder *encoder)
  109. {
  110. return encoder->type == INTEL_OUTPUT_EDP;
  111. }
  112. static void cdv_intel_dp_start_link_train(struct gma_encoder *encoder);
  113. static void cdv_intel_dp_complete_link_train(struct gma_encoder *encoder);
  114. static void cdv_intel_dp_link_down(struct gma_encoder *encoder);
  115. static int
  116. cdv_intel_dp_max_lane_count(struct gma_encoder *encoder)
  117. {
  118. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  119. int max_lane_count = 4;
  120. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
  121. max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
  122. switch (max_lane_count) {
  123. case 1: case 2: case 4:
  124. break;
  125. default:
  126. max_lane_count = 4;
  127. }
  128. }
  129. return max_lane_count;
  130. }
  131. static int
  132. cdv_intel_dp_max_link_bw(struct gma_encoder *encoder)
  133. {
  134. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  135. int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
  136. switch (max_link_bw) {
  137. case DP_LINK_BW_1_62:
  138. case DP_LINK_BW_2_7:
  139. break;
  140. default:
  141. max_link_bw = DP_LINK_BW_1_62;
  142. break;
  143. }
  144. return max_link_bw;
  145. }
  146. static int
  147. cdv_intel_dp_link_clock(uint8_t link_bw)
  148. {
  149. if (link_bw == DP_LINK_BW_2_7)
  150. return 270000;
  151. else
  152. return 162000;
  153. }
  154. static int
  155. cdv_intel_dp_link_required(int pixel_clock, int bpp)
  156. {
  157. return (pixel_clock * bpp + 7) / 8;
  158. }
  159. static int
  160. cdv_intel_dp_max_data_rate(int max_link_clock, int max_lanes)
  161. {
  162. return (max_link_clock * max_lanes * 19) / 20;
  163. }
  164. static void cdv_intel_edp_panel_vdd_on(struct gma_encoder *intel_encoder)
  165. {
  166. struct drm_device *dev = intel_encoder->base.dev;
  167. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  168. u32 pp;
  169. if (intel_dp->panel_on) {
  170. DRM_DEBUG_KMS("Skip VDD on because of panel on\n");
  171. return;
  172. }
  173. DRM_DEBUG_KMS("\n");
  174. pp = REG_READ(PP_CONTROL);
  175. pp |= EDP_FORCE_VDD;
  176. REG_WRITE(PP_CONTROL, pp);
  177. REG_READ(PP_CONTROL);
  178. msleep(intel_dp->panel_power_up_delay);
  179. }
  180. static void cdv_intel_edp_panel_vdd_off(struct gma_encoder *intel_encoder)
  181. {
  182. struct drm_device *dev = intel_encoder->base.dev;
  183. u32 pp;
  184. DRM_DEBUG_KMS("\n");
  185. pp = REG_READ(PP_CONTROL);
  186. pp &= ~EDP_FORCE_VDD;
  187. REG_WRITE(PP_CONTROL, pp);
  188. REG_READ(PP_CONTROL);
  189. }
  190. /* Returns true if the panel was already on when called */
  191. static bool cdv_intel_edp_panel_on(struct gma_encoder *intel_encoder)
  192. {
  193. struct drm_device *dev = intel_encoder->base.dev;
  194. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  195. u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_NONE;
  196. if (intel_dp->panel_on)
  197. return true;
  198. DRM_DEBUG_KMS("\n");
  199. pp = REG_READ(PP_CONTROL);
  200. pp &= ~PANEL_UNLOCK_MASK;
  201. pp |= (PANEL_UNLOCK_REGS | POWER_TARGET_ON);
  202. REG_WRITE(PP_CONTROL, pp);
  203. REG_READ(PP_CONTROL);
  204. if (wait_for(((REG_READ(PP_STATUS) & idle_on_mask) == idle_on_mask), 1000)) {
  205. DRM_DEBUG_KMS("Error in Powering up eDP panel, status %x\n", REG_READ(PP_STATUS));
  206. intel_dp->panel_on = false;
  207. } else
  208. intel_dp->panel_on = true;
  209. msleep(intel_dp->panel_power_up_delay);
  210. return false;
  211. }
  212. static void cdv_intel_edp_panel_off (struct gma_encoder *intel_encoder)
  213. {
  214. struct drm_device *dev = intel_encoder->base.dev;
  215. u32 pp, idle_off_mask = PP_ON ;
  216. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  217. DRM_DEBUG_KMS("\n");
  218. pp = REG_READ(PP_CONTROL);
  219. if ((pp & POWER_TARGET_ON) == 0)
  220. return;
  221. intel_dp->panel_on = false;
  222. pp &= ~PANEL_UNLOCK_MASK;
  223. /* ILK workaround: disable reset around power sequence */
  224. pp &= ~POWER_TARGET_ON;
  225. pp &= ~EDP_FORCE_VDD;
  226. pp &= ~EDP_BLC_ENABLE;
  227. REG_WRITE(PP_CONTROL, pp);
  228. REG_READ(PP_CONTROL);
  229. DRM_DEBUG_KMS("PP_STATUS %x\n", REG_READ(PP_STATUS));
  230. if (wait_for((REG_READ(PP_STATUS) & idle_off_mask) == 0, 1000)) {
  231. DRM_DEBUG_KMS("Error in turning off Panel\n");
  232. }
  233. msleep(intel_dp->panel_power_cycle_delay);
  234. DRM_DEBUG_KMS("Over\n");
  235. }
  236. static void cdv_intel_edp_backlight_on (struct gma_encoder *intel_encoder)
  237. {
  238. struct drm_device *dev = intel_encoder->base.dev;
  239. u32 pp;
  240. DRM_DEBUG_KMS("\n");
  241. /*
  242. * If we enable the backlight right away following a panel power
  243. * on, we may see slight flicker as the panel syncs with the eDP
  244. * link. So delay a bit to make sure the image is solid before
  245. * allowing it to appear.
  246. */
  247. msleep(300);
  248. pp = REG_READ(PP_CONTROL);
  249. pp |= EDP_BLC_ENABLE;
  250. REG_WRITE(PP_CONTROL, pp);
  251. gma_backlight_enable(dev);
  252. }
  253. static void cdv_intel_edp_backlight_off (struct gma_encoder *intel_encoder)
  254. {
  255. struct drm_device *dev = intel_encoder->base.dev;
  256. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  257. u32 pp;
  258. DRM_DEBUG_KMS("\n");
  259. gma_backlight_disable(dev);
  260. msleep(10);
  261. pp = REG_READ(PP_CONTROL);
  262. pp &= ~EDP_BLC_ENABLE;
  263. REG_WRITE(PP_CONTROL, pp);
  264. msleep(intel_dp->backlight_off_delay);
  265. }
  266. static int
  267. cdv_intel_dp_mode_valid(struct drm_connector *connector,
  268. struct drm_display_mode *mode)
  269. {
  270. struct gma_encoder *encoder = gma_attached_encoder(connector);
  271. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  272. int max_link_clock = cdv_intel_dp_link_clock(cdv_intel_dp_max_link_bw(encoder));
  273. int max_lanes = cdv_intel_dp_max_lane_count(encoder);
  274. struct drm_psb_private *dev_priv = connector->dev->dev_private;
  275. if (is_edp(encoder) && intel_dp->panel_fixed_mode) {
  276. if (mode->hdisplay > intel_dp->panel_fixed_mode->hdisplay)
  277. return MODE_PANEL;
  278. if (mode->vdisplay > intel_dp->panel_fixed_mode->vdisplay)
  279. return MODE_PANEL;
  280. }
  281. /* only refuse the mode on non eDP since we have seen some weird eDP panels
  282. which are outside spec tolerances but somehow work by magic */
  283. if (!is_edp(encoder) &&
  284. (cdv_intel_dp_link_required(mode->clock, dev_priv->edp.bpp)
  285. > cdv_intel_dp_max_data_rate(max_link_clock, max_lanes)))
  286. return MODE_CLOCK_HIGH;
  287. if (is_edp(encoder)) {
  288. if (cdv_intel_dp_link_required(mode->clock, 24)
  289. > cdv_intel_dp_max_data_rate(max_link_clock, max_lanes))
  290. return MODE_CLOCK_HIGH;
  291. }
  292. if (mode->clock < 10000)
  293. return MODE_CLOCK_LOW;
  294. return MODE_OK;
  295. }
  296. static uint32_t
  297. pack_aux(uint8_t *src, int src_bytes)
  298. {
  299. int i;
  300. uint32_t v = 0;
  301. if (src_bytes > 4)
  302. src_bytes = 4;
  303. for (i = 0; i < src_bytes; i++)
  304. v |= ((uint32_t) src[i]) << ((3-i) * 8);
  305. return v;
  306. }
  307. static void
  308. unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
  309. {
  310. int i;
  311. if (dst_bytes > 4)
  312. dst_bytes = 4;
  313. for (i = 0; i < dst_bytes; i++)
  314. dst[i] = src >> ((3-i) * 8);
  315. }
  316. static int
  317. cdv_intel_dp_aux_ch(struct gma_encoder *encoder,
  318. uint8_t *send, int send_bytes,
  319. uint8_t *recv, int recv_size)
  320. {
  321. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  322. uint32_t output_reg = intel_dp->output_reg;
  323. struct drm_device *dev = encoder->base.dev;
  324. uint32_t ch_ctl = output_reg + 0x10;
  325. uint32_t ch_data = ch_ctl + 4;
  326. int i;
  327. int recv_bytes;
  328. uint32_t status;
  329. uint32_t aux_clock_divider;
  330. int try, precharge;
  331. /* The clock divider is based off the hrawclk,
  332. * and would like to run at 2MHz. So, take the
  333. * hrawclk value and divide by 2 and use that
  334. * On CDV platform it uses 200MHz as hrawclk.
  335. *
  336. */
  337. aux_clock_divider = 200 / 2;
  338. precharge = 4;
  339. if (is_edp(encoder))
  340. precharge = 10;
  341. if (REG_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
  342. DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
  343. REG_READ(ch_ctl));
  344. return -EBUSY;
  345. }
  346. /* Must try at least 3 times according to DP spec */
  347. for (try = 0; try < 5; try++) {
  348. /* Load the send data into the aux channel data registers */
  349. for (i = 0; i < send_bytes; i += 4)
  350. REG_WRITE(ch_data + i,
  351. pack_aux(send + i, send_bytes - i));
  352. /* Send the command and wait for it to complete */
  353. REG_WRITE(ch_ctl,
  354. DP_AUX_CH_CTL_SEND_BUSY |
  355. DP_AUX_CH_CTL_TIME_OUT_400us |
  356. (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  357. (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  358. (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
  359. DP_AUX_CH_CTL_DONE |
  360. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  361. DP_AUX_CH_CTL_RECEIVE_ERROR);
  362. for (;;) {
  363. status = REG_READ(ch_ctl);
  364. if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  365. break;
  366. udelay(100);
  367. }
  368. /* Clear done status and any errors */
  369. REG_WRITE(ch_ctl,
  370. status |
  371. DP_AUX_CH_CTL_DONE |
  372. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  373. DP_AUX_CH_CTL_RECEIVE_ERROR);
  374. if (status & DP_AUX_CH_CTL_DONE)
  375. break;
  376. }
  377. if ((status & DP_AUX_CH_CTL_DONE) == 0) {
  378. DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
  379. return -EBUSY;
  380. }
  381. /* Check for timeout or receive error.
  382. * Timeouts occur when the sink is not connected
  383. */
  384. if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  385. DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
  386. return -EIO;
  387. }
  388. /* Timeouts occur when the device isn't connected, so they're
  389. * "normal" -- don't fill the kernel log with these */
  390. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  391. DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
  392. return -ETIMEDOUT;
  393. }
  394. /* Unload any bytes sent back from the other side */
  395. recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  396. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  397. if (recv_bytes > recv_size)
  398. recv_bytes = recv_size;
  399. for (i = 0; i < recv_bytes; i += 4)
  400. unpack_aux(REG_READ(ch_data + i),
  401. recv + i, recv_bytes - i);
  402. return recv_bytes;
  403. }
  404. /* Write data to the aux channel in native mode */
  405. static int
  406. cdv_intel_dp_aux_native_write(struct gma_encoder *encoder,
  407. uint16_t address, uint8_t *send, int send_bytes)
  408. {
  409. int ret;
  410. uint8_t msg[20];
  411. int msg_bytes;
  412. uint8_t ack;
  413. if (send_bytes > 16)
  414. return -1;
  415. msg[0] = AUX_NATIVE_WRITE << 4;
  416. msg[1] = address >> 8;
  417. msg[2] = address & 0xff;
  418. msg[3] = send_bytes - 1;
  419. memcpy(&msg[4], send, send_bytes);
  420. msg_bytes = send_bytes + 4;
  421. for (;;) {
  422. ret = cdv_intel_dp_aux_ch(encoder, msg, msg_bytes, &ack, 1);
  423. if (ret < 0)
  424. return ret;
  425. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
  426. break;
  427. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  428. udelay(100);
  429. else
  430. return -EIO;
  431. }
  432. return send_bytes;
  433. }
  434. /* Write a single byte to the aux channel in native mode */
  435. static int
  436. cdv_intel_dp_aux_native_write_1(struct gma_encoder *encoder,
  437. uint16_t address, uint8_t byte)
  438. {
  439. return cdv_intel_dp_aux_native_write(encoder, address, &byte, 1);
  440. }
  441. /* read bytes from a native aux channel */
  442. static int
  443. cdv_intel_dp_aux_native_read(struct gma_encoder *encoder,
  444. uint16_t address, uint8_t *recv, int recv_bytes)
  445. {
  446. uint8_t msg[4];
  447. int msg_bytes;
  448. uint8_t reply[20];
  449. int reply_bytes;
  450. uint8_t ack;
  451. int ret;
  452. msg[0] = AUX_NATIVE_READ << 4;
  453. msg[1] = address >> 8;
  454. msg[2] = address & 0xff;
  455. msg[3] = recv_bytes - 1;
  456. msg_bytes = 4;
  457. reply_bytes = recv_bytes + 1;
  458. for (;;) {
  459. ret = cdv_intel_dp_aux_ch(encoder, msg, msg_bytes,
  460. reply, reply_bytes);
  461. if (ret == 0)
  462. return -EPROTO;
  463. if (ret < 0)
  464. return ret;
  465. ack = reply[0];
  466. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
  467. memcpy(recv, reply + 1, ret - 1);
  468. return ret - 1;
  469. }
  470. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  471. udelay(100);
  472. else
  473. return -EIO;
  474. }
  475. }
  476. static int
  477. cdv_intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  478. uint8_t write_byte, uint8_t *read_byte)
  479. {
  480. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  481. struct cdv_intel_dp *intel_dp = container_of(adapter,
  482. struct cdv_intel_dp,
  483. adapter);
  484. struct gma_encoder *encoder = intel_dp->encoder;
  485. uint16_t address = algo_data->address;
  486. uint8_t msg[5];
  487. uint8_t reply[2];
  488. unsigned retry;
  489. int msg_bytes;
  490. int reply_bytes;
  491. int ret;
  492. /* Set up the command byte */
  493. if (mode & MODE_I2C_READ)
  494. msg[0] = AUX_I2C_READ << 4;
  495. else
  496. msg[0] = AUX_I2C_WRITE << 4;
  497. if (!(mode & MODE_I2C_STOP))
  498. msg[0] |= AUX_I2C_MOT << 4;
  499. msg[1] = address >> 8;
  500. msg[2] = address;
  501. switch (mode) {
  502. case MODE_I2C_WRITE:
  503. msg[3] = 0;
  504. msg[4] = write_byte;
  505. msg_bytes = 5;
  506. reply_bytes = 1;
  507. break;
  508. case MODE_I2C_READ:
  509. msg[3] = 0;
  510. msg_bytes = 4;
  511. reply_bytes = 2;
  512. break;
  513. default:
  514. msg_bytes = 3;
  515. reply_bytes = 1;
  516. break;
  517. }
  518. for (retry = 0; retry < 5; retry++) {
  519. ret = cdv_intel_dp_aux_ch(encoder,
  520. msg, msg_bytes,
  521. reply, reply_bytes);
  522. if (ret < 0) {
  523. DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
  524. return ret;
  525. }
  526. switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
  527. case AUX_NATIVE_REPLY_ACK:
  528. /* I2C-over-AUX Reply field is only valid
  529. * when paired with AUX ACK.
  530. */
  531. break;
  532. case AUX_NATIVE_REPLY_NACK:
  533. DRM_DEBUG_KMS("aux_ch native nack\n");
  534. return -EREMOTEIO;
  535. case AUX_NATIVE_REPLY_DEFER:
  536. udelay(100);
  537. continue;
  538. default:
  539. DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
  540. reply[0]);
  541. return -EREMOTEIO;
  542. }
  543. switch (reply[0] & AUX_I2C_REPLY_MASK) {
  544. case AUX_I2C_REPLY_ACK:
  545. if (mode == MODE_I2C_READ) {
  546. *read_byte = reply[1];
  547. }
  548. return reply_bytes - 1;
  549. case AUX_I2C_REPLY_NACK:
  550. DRM_DEBUG_KMS("aux_i2c nack\n");
  551. return -EREMOTEIO;
  552. case AUX_I2C_REPLY_DEFER:
  553. DRM_DEBUG_KMS("aux_i2c defer\n");
  554. udelay(100);
  555. break;
  556. default:
  557. DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
  558. return -EREMOTEIO;
  559. }
  560. }
  561. DRM_ERROR("too many retries, giving up\n");
  562. return -EREMOTEIO;
  563. }
  564. static int
  565. cdv_intel_dp_i2c_init(struct gma_connector *connector,
  566. struct gma_encoder *encoder, const char *name)
  567. {
  568. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  569. int ret;
  570. DRM_DEBUG_KMS("i2c_init %s\n", name);
  571. intel_dp->algo.running = false;
  572. intel_dp->algo.address = 0;
  573. intel_dp->algo.aux_ch = cdv_intel_dp_i2c_aux_ch;
  574. memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
  575. intel_dp->adapter.owner = THIS_MODULE;
  576. intel_dp->adapter.class = I2C_CLASS_DDC;
  577. strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
  578. intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
  579. intel_dp->adapter.algo_data = &intel_dp->algo;
  580. intel_dp->adapter.dev.parent = &connector->base.kdev;
  581. if (is_edp(encoder))
  582. cdv_intel_edp_panel_vdd_on(encoder);
  583. ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
  584. if (is_edp(encoder))
  585. cdv_intel_edp_panel_vdd_off(encoder);
  586. return ret;
  587. }
  588. void cdv_intel_fixed_panel_mode(struct drm_display_mode *fixed_mode,
  589. struct drm_display_mode *adjusted_mode)
  590. {
  591. adjusted_mode->hdisplay = fixed_mode->hdisplay;
  592. adjusted_mode->hsync_start = fixed_mode->hsync_start;
  593. adjusted_mode->hsync_end = fixed_mode->hsync_end;
  594. adjusted_mode->htotal = fixed_mode->htotal;
  595. adjusted_mode->vdisplay = fixed_mode->vdisplay;
  596. adjusted_mode->vsync_start = fixed_mode->vsync_start;
  597. adjusted_mode->vsync_end = fixed_mode->vsync_end;
  598. adjusted_mode->vtotal = fixed_mode->vtotal;
  599. adjusted_mode->clock = fixed_mode->clock;
  600. drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
  601. }
  602. static bool
  603. cdv_intel_dp_mode_fixup(struct drm_encoder *encoder, const struct drm_display_mode *mode,
  604. struct drm_display_mode *adjusted_mode)
  605. {
  606. struct drm_psb_private *dev_priv = encoder->dev->dev_private;
  607. struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
  608. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  609. int lane_count, clock;
  610. int max_lane_count = cdv_intel_dp_max_lane_count(intel_encoder);
  611. int max_clock = cdv_intel_dp_max_link_bw(intel_encoder) == DP_LINK_BW_2_7 ? 1 : 0;
  612. static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
  613. int refclock = mode->clock;
  614. int bpp = 24;
  615. if (is_edp(intel_encoder) && intel_dp->panel_fixed_mode) {
  616. cdv_intel_fixed_panel_mode(intel_dp->panel_fixed_mode, adjusted_mode);
  617. refclock = intel_dp->panel_fixed_mode->clock;
  618. bpp = dev_priv->edp.bpp;
  619. }
  620. for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
  621. for (clock = max_clock; clock >= 0; clock--) {
  622. int link_avail = cdv_intel_dp_max_data_rate(cdv_intel_dp_link_clock(bws[clock]), lane_count);
  623. if (cdv_intel_dp_link_required(refclock, bpp) <= link_avail) {
  624. intel_dp->link_bw = bws[clock];
  625. intel_dp->lane_count = lane_count;
  626. adjusted_mode->clock = cdv_intel_dp_link_clock(intel_dp->link_bw);
  627. DRM_DEBUG_KMS("Display port link bw %02x lane "
  628. "count %d clock %d\n",
  629. intel_dp->link_bw, intel_dp->lane_count,
  630. adjusted_mode->clock);
  631. return true;
  632. }
  633. }
  634. }
  635. if (is_edp(intel_encoder)) {
  636. /* okay we failed just pick the highest */
  637. intel_dp->lane_count = max_lane_count;
  638. intel_dp->link_bw = bws[max_clock];
  639. adjusted_mode->clock = cdv_intel_dp_link_clock(intel_dp->link_bw);
  640. DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
  641. "count %d clock %d\n",
  642. intel_dp->link_bw, intel_dp->lane_count,
  643. adjusted_mode->clock);
  644. return true;
  645. }
  646. return false;
  647. }
  648. struct cdv_intel_dp_m_n {
  649. uint32_t tu;
  650. uint32_t gmch_m;
  651. uint32_t gmch_n;
  652. uint32_t link_m;
  653. uint32_t link_n;
  654. };
  655. static void
  656. cdv_intel_reduce_ratio(uint32_t *num, uint32_t *den)
  657. {
  658. /*
  659. while (*num > 0xffffff || *den > 0xffffff) {
  660. *num >>= 1;
  661. *den >>= 1;
  662. }*/
  663. uint64_t value, m;
  664. m = *num;
  665. value = m * (0x800000);
  666. m = do_div(value, *den);
  667. *num = value;
  668. *den = 0x800000;
  669. }
  670. static void
  671. cdv_intel_dp_compute_m_n(int bpp,
  672. int nlanes,
  673. int pixel_clock,
  674. int link_clock,
  675. struct cdv_intel_dp_m_n *m_n)
  676. {
  677. m_n->tu = 64;
  678. m_n->gmch_m = (pixel_clock * bpp + 7) >> 3;
  679. m_n->gmch_n = link_clock * nlanes;
  680. cdv_intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  681. m_n->link_m = pixel_clock;
  682. m_n->link_n = link_clock;
  683. cdv_intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  684. }
  685. void
  686. cdv_intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
  687. struct drm_display_mode *adjusted_mode)
  688. {
  689. struct drm_device *dev = crtc->dev;
  690. struct drm_psb_private *dev_priv = dev->dev_private;
  691. struct drm_mode_config *mode_config = &dev->mode_config;
  692. struct drm_encoder *encoder;
  693. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  694. int lane_count = 4, bpp = 24;
  695. struct cdv_intel_dp_m_n m_n;
  696. int pipe = gma_crtc->pipe;
  697. /*
  698. * Find the lane count in the intel_encoder private
  699. */
  700. list_for_each_entry(encoder, &mode_config->encoder_list, head) {
  701. struct gma_encoder *intel_encoder;
  702. struct cdv_intel_dp *intel_dp;
  703. if (encoder->crtc != crtc)
  704. continue;
  705. intel_encoder = to_gma_encoder(encoder);
  706. intel_dp = intel_encoder->dev_priv;
  707. if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT) {
  708. lane_count = intel_dp->lane_count;
  709. break;
  710. } else if (is_edp(intel_encoder)) {
  711. lane_count = intel_dp->lane_count;
  712. bpp = dev_priv->edp.bpp;
  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. cdv_intel_dp_compute_m_n(bpp, lane_count,
  722. mode->clock, adjusted_mode->clock, &m_n);
  723. {
  724. REG_WRITE(PIPE_GMCH_DATA_M(pipe),
  725. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  726. m_n.gmch_m);
  727. REG_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
  728. REG_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
  729. REG_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
  730. }
  731. }
  732. static void
  733. cdv_intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  734. struct drm_display_mode *adjusted_mode)
  735. {
  736. struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
  737. struct drm_crtc *crtc = encoder->crtc;
  738. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  739. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  740. struct drm_device *dev = encoder->dev;
  741. intel_dp->DP = DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
  742. intel_dp->DP |= intel_dp->color_range;
  743. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  744. intel_dp->DP |= DP_SYNC_HS_HIGH;
  745. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  746. intel_dp->DP |= DP_SYNC_VS_HIGH;
  747. intel_dp->DP |= DP_LINK_TRAIN_OFF;
  748. switch (intel_dp->lane_count) {
  749. case 1:
  750. intel_dp->DP |= DP_PORT_WIDTH_1;
  751. break;
  752. case 2:
  753. intel_dp->DP |= DP_PORT_WIDTH_2;
  754. break;
  755. case 4:
  756. intel_dp->DP |= DP_PORT_WIDTH_4;
  757. break;
  758. }
  759. if (intel_dp->has_audio)
  760. intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
  761. memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  762. intel_dp->link_configuration[0] = intel_dp->link_bw;
  763. intel_dp->link_configuration[1] = intel_dp->lane_count;
  764. /*
  765. * Check for DPCD version > 1.1 and enhanced framing support
  766. */
  767. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  768. (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
  769. intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  770. intel_dp->DP |= DP_ENHANCED_FRAMING;
  771. }
  772. /* CPT DP's pipe select is decided in TRANS_DP_CTL */
  773. if (gma_crtc->pipe == 1)
  774. intel_dp->DP |= DP_PIPEB_SELECT;
  775. REG_WRITE(intel_dp->output_reg, (intel_dp->DP | DP_PORT_EN));
  776. DRM_DEBUG_KMS("DP expected reg is %x\n", intel_dp->DP);
  777. if (is_edp(intel_encoder)) {
  778. uint32_t pfit_control;
  779. cdv_intel_edp_panel_on(intel_encoder);
  780. if (mode->hdisplay != adjusted_mode->hdisplay ||
  781. mode->vdisplay != adjusted_mode->vdisplay)
  782. pfit_control = PFIT_ENABLE;
  783. else
  784. pfit_control = 0;
  785. pfit_control |= gma_crtc->pipe << PFIT_PIPE_SHIFT;
  786. REG_WRITE(PFIT_CONTROL, pfit_control);
  787. }
  788. }
  789. /* If the sink supports it, try to set the power state appropriately */
  790. static void cdv_intel_dp_sink_dpms(struct gma_encoder *encoder, int mode)
  791. {
  792. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  793. int ret, i;
  794. /* Should have a valid DPCD by this point */
  795. if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
  796. return;
  797. if (mode != DRM_MODE_DPMS_ON) {
  798. ret = cdv_intel_dp_aux_native_write_1(encoder, DP_SET_POWER,
  799. DP_SET_POWER_D3);
  800. if (ret != 1)
  801. DRM_DEBUG_DRIVER("failed to write sink power state\n");
  802. } else {
  803. /*
  804. * When turning on, we need to retry for 1ms to give the sink
  805. * time to wake up.
  806. */
  807. for (i = 0; i < 3; i++) {
  808. ret = cdv_intel_dp_aux_native_write_1(encoder,
  809. DP_SET_POWER,
  810. DP_SET_POWER_D0);
  811. if (ret == 1)
  812. break;
  813. udelay(1000);
  814. }
  815. }
  816. }
  817. static void cdv_intel_dp_prepare(struct drm_encoder *encoder)
  818. {
  819. struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
  820. int edp = is_edp(intel_encoder);
  821. if (edp) {
  822. cdv_intel_edp_backlight_off(intel_encoder);
  823. cdv_intel_edp_panel_off(intel_encoder);
  824. cdv_intel_edp_panel_vdd_on(intel_encoder);
  825. }
  826. /* Wake up the sink first */
  827. cdv_intel_dp_sink_dpms(intel_encoder, DRM_MODE_DPMS_ON);
  828. cdv_intel_dp_link_down(intel_encoder);
  829. if (edp)
  830. cdv_intel_edp_panel_vdd_off(intel_encoder);
  831. }
  832. static void cdv_intel_dp_commit(struct drm_encoder *encoder)
  833. {
  834. struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
  835. int edp = is_edp(intel_encoder);
  836. if (edp)
  837. cdv_intel_edp_panel_on(intel_encoder);
  838. cdv_intel_dp_start_link_train(intel_encoder);
  839. cdv_intel_dp_complete_link_train(intel_encoder);
  840. if (edp)
  841. cdv_intel_edp_backlight_on(intel_encoder);
  842. }
  843. static void
  844. cdv_intel_dp_dpms(struct drm_encoder *encoder, int mode)
  845. {
  846. struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
  847. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  848. struct drm_device *dev = encoder->dev;
  849. uint32_t dp_reg = REG_READ(intel_dp->output_reg);
  850. int edp = is_edp(intel_encoder);
  851. if (mode != DRM_MODE_DPMS_ON) {
  852. if (edp) {
  853. cdv_intel_edp_backlight_off(intel_encoder);
  854. cdv_intel_edp_panel_vdd_on(intel_encoder);
  855. }
  856. cdv_intel_dp_sink_dpms(intel_encoder, mode);
  857. cdv_intel_dp_link_down(intel_encoder);
  858. if (edp) {
  859. cdv_intel_edp_panel_vdd_off(intel_encoder);
  860. cdv_intel_edp_panel_off(intel_encoder);
  861. }
  862. } else {
  863. if (edp)
  864. cdv_intel_edp_panel_on(intel_encoder);
  865. cdv_intel_dp_sink_dpms(intel_encoder, mode);
  866. if (!(dp_reg & DP_PORT_EN)) {
  867. cdv_intel_dp_start_link_train(intel_encoder);
  868. cdv_intel_dp_complete_link_train(intel_encoder);
  869. }
  870. if (edp)
  871. cdv_intel_edp_backlight_on(intel_encoder);
  872. }
  873. }
  874. /*
  875. * Native read with retry for link status and receiver capability reads for
  876. * cases where the sink may still be asleep.
  877. */
  878. static bool
  879. cdv_intel_dp_aux_native_read_retry(struct gma_encoder *encoder, uint16_t address,
  880. uint8_t *recv, int recv_bytes)
  881. {
  882. int ret, i;
  883. /*
  884. * Sinks are *supposed* to come up within 1ms from an off state,
  885. * but we're also supposed to retry 3 times per the spec.
  886. */
  887. for (i = 0; i < 3; i++) {
  888. ret = cdv_intel_dp_aux_native_read(encoder, address, recv,
  889. recv_bytes);
  890. if (ret == recv_bytes)
  891. return true;
  892. udelay(1000);
  893. }
  894. return false;
  895. }
  896. /*
  897. * Fetch AUX CH registers 0x202 - 0x207 which contain
  898. * link status information
  899. */
  900. static bool
  901. cdv_intel_dp_get_link_status(struct gma_encoder *encoder)
  902. {
  903. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  904. return cdv_intel_dp_aux_native_read_retry(encoder,
  905. DP_LANE0_1_STATUS,
  906. intel_dp->link_status,
  907. DP_LINK_STATUS_SIZE);
  908. }
  909. static uint8_t
  910. cdv_intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  911. int r)
  912. {
  913. return link_status[r - DP_LANE0_1_STATUS];
  914. }
  915. static uint8_t
  916. cdv_intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
  917. int lane)
  918. {
  919. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  920. int s = ((lane & 1) ?
  921. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  922. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  923. uint8_t l = cdv_intel_dp_link_status(link_status, i);
  924. return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  925. }
  926. static uint8_t
  927. cdv_intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
  928. int lane)
  929. {
  930. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  931. int s = ((lane & 1) ?
  932. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  933. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  934. uint8_t l = cdv_intel_dp_link_status(link_status, i);
  935. return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  936. }
  937. #if 0
  938. static char *voltage_names[] = {
  939. "0.4V", "0.6V", "0.8V", "1.2V"
  940. };
  941. static char *pre_emph_names[] = {
  942. "0dB", "3.5dB", "6dB", "9.5dB"
  943. };
  944. static char *link_train_names[] = {
  945. "pattern 1", "pattern 2", "idle", "off"
  946. };
  947. #endif
  948. #define CDV_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_1200
  949. /*
  950. static uint8_t
  951. cdv_intel_dp_pre_emphasis_max(uint8_t voltage_swing)
  952. {
  953. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  954. case DP_TRAIN_VOLTAGE_SWING_400:
  955. return DP_TRAIN_PRE_EMPHASIS_6;
  956. case DP_TRAIN_VOLTAGE_SWING_600:
  957. return DP_TRAIN_PRE_EMPHASIS_6;
  958. case DP_TRAIN_VOLTAGE_SWING_800:
  959. return DP_TRAIN_PRE_EMPHASIS_3_5;
  960. case DP_TRAIN_VOLTAGE_SWING_1200:
  961. default:
  962. return DP_TRAIN_PRE_EMPHASIS_0;
  963. }
  964. }
  965. */
  966. static void
  967. cdv_intel_get_adjust_train(struct gma_encoder *encoder)
  968. {
  969. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  970. uint8_t v = 0;
  971. uint8_t p = 0;
  972. int lane;
  973. for (lane = 0; lane < intel_dp->lane_count; lane++) {
  974. uint8_t this_v = cdv_intel_get_adjust_request_voltage(intel_dp->link_status, lane);
  975. uint8_t this_p = cdv_intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
  976. if (this_v > v)
  977. v = this_v;
  978. if (this_p > p)
  979. p = this_p;
  980. }
  981. if (v >= CDV_DP_VOLTAGE_MAX)
  982. v = CDV_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
  983. if (p == DP_TRAIN_PRE_EMPHASIS_MASK)
  984. p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  985. for (lane = 0; lane < 4; lane++)
  986. intel_dp->train_set[lane] = v | p;
  987. }
  988. static uint8_t
  989. cdv_intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  990. int lane)
  991. {
  992. int i = DP_LANE0_1_STATUS + (lane >> 1);
  993. int s = (lane & 1) * 4;
  994. uint8_t l = cdv_intel_dp_link_status(link_status, i);
  995. return (l >> s) & 0xf;
  996. }
  997. /* Check for clock recovery is done on all channels */
  998. static bool
  999. cdv_intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  1000. {
  1001. int lane;
  1002. uint8_t lane_status;
  1003. for (lane = 0; lane < lane_count; lane++) {
  1004. lane_status = cdv_intel_get_lane_status(link_status, lane);
  1005. if ((lane_status & DP_LANE_CR_DONE) == 0)
  1006. return false;
  1007. }
  1008. return true;
  1009. }
  1010. /* Check to see if channel eq is done on all channels */
  1011. #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
  1012. DP_LANE_CHANNEL_EQ_DONE|\
  1013. DP_LANE_SYMBOL_LOCKED)
  1014. static bool
  1015. cdv_intel_channel_eq_ok(struct gma_encoder *encoder)
  1016. {
  1017. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1018. uint8_t lane_align;
  1019. uint8_t lane_status;
  1020. int lane;
  1021. lane_align = cdv_intel_dp_link_status(intel_dp->link_status,
  1022. DP_LANE_ALIGN_STATUS_UPDATED);
  1023. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  1024. return false;
  1025. for (lane = 0; lane < intel_dp->lane_count; lane++) {
  1026. lane_status = cdv_intel_get_lane_status(intel_dp->link_status, lane);
  1027. if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
  1028. return false;
  1029. }
  1030. return true;
  1031. }
  1032. static bool
  1033. cdv_intel_dp_set_link_train(struct gma_encoder *encoder,
  1034. uint32_t dp_reg_value,
  1035. uint8_t dp_train_pat)
  1036. {
  1037. struct drm_device *dev = encoder->base.dev;
  1038. int ret;
  1039. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1040. REG_WRITE(intel_dp->output_reg, dp_reg_value);
  1041. REG_READ(intel_dp->output_reg);
  1042. ret = cdv_intel_dp_aux_native_write_1(encoder,
  1043. DP_TRAINING_PATTERN_SET,
  1044. dp_train_pat);
  1045. if (ret != 1) {
  1046. DRM_DEBUG_KMS("Failure in setting link pattern %x\n",
  1047. dp_train_pat);
  1048. return false;
  1049. }
  1050. return true;
  1051. }
  1052. static bool
  1053. cdv_intel_dplink_set_level(struct gma_encoder *encoder,
  1054. uint8_t dp_train_pat)
  1055. {
  1056. int ret;
  1057. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1058. ret = cdv_intel_dp_aux_native_write(encoder,
  1059. DP_TRAINING_LANE0_SET,
  1060. intel_dp->train_set,
  1061. intel_dp->lane_count);
  1062. if (ret != intel_dp->lane_count) {
  1063. DRM_DEBUG_KMS("Failure in setting level %d, lane_cnt= %d\n",
  1064. intel_dp->train_set[0], intel_dp->lane_count);
  1065. return false;
  1066. }
  1067. return true;
  1068. }
  1069. static void
  1070. cdv_intel_dp_set_vswing_premph(struct gma_encoder *encoder, uint8_t signal_level)
  1071. {
  1072. struct drm_device *dev = encoder->base.dev;
  1073. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1074. struct ddi_regoff *ddi_reg;
  1075. int vswing, premph, index;
  1076. if (intel_dp->output_reg == DP_B)
  1077. ddi_reg = &ddi_DP_train_table[0];
  1078. else
  1079. ddi_reg = &ddi_DP_train_table[1];
  1080. vswing = (signal_level & DP_TRAIN_VOLTAGE_SWING_MASK);
  1081. premph = ((signal_level & DP_TRAIN_PRE_EMPHASIS_MASK)) >>
  1082. DP_TRAIN_PRE_EMPHASIS_SHIFT;
  1083. if (vswing + premph > 3)
  1084. return;
  1085. #ifdef CDV_FAST_LINK_TRAIN
  1086. return;
  1087. #endif
  1088. DRM_DEBUG_KMS("Test2\n");
  1089. //return ;
  1090. cdv_sb_reset(dev);
  1091. /* ;Swing voltage programming
  1092. ;gfx_dpio_set_reg(0xc058, 0x0505313A) */
  1093. cdv_sb_write(dev, ddi_reg->VSwing5, 0x0505313A);
  1094. /* ;gfx_dpio_set_reg(0x8154, 0x43406055) */
  1095. cdv_sb_write(dev, ddi_reg->VSwing1, 0x43406055);
  1096. /* ;gfx_dpio_set_reg(0x8148, 0x55338954)
  1097. * The VSwing_PreEmph table is also considered based on the vswing/premp
  1098. */
  1099. index = (vswing + premph) * 2;
  1100. if (premph == 1 && vswing == 1) {
  1101. cdv_sb_write(dev, ddi_reg->VSwing2, 0x055738954);
  1102. } else
  1103. cdv_sb_write(dev, ddi_reg->VSwing2, dp_vswing_premph_table[index]);
  1104. /* ;gfx_dpio_set_reg(0x814c, 0x40802040) */
  1105. if ((vswing + premph) == DP_TRAIN_VOLTAGE_SWING_1200)
  1106. cdv_sb_write(dev, ddi_reg->VSwing3, 0x70802040);
  1107. else
  1108. cdv_sb_write(dev, ddi_reg->VSwing3, 0x40802040);
  1109. /* ;gfx_dpio_set_reg(0x8150, 0x2b405555) */
  1110. /* cdv_sb_write(dev, ddi_reg->VSwing4, 0x2b405555); */
  1111. /* ;gfx_dpio_set_reg(0x8154, 0xc3406055) */
  1112. cdv_sb_write(dev, ddi_reg->VSwing1, 0xc3406055);
  1113. /* ;Pre emphasis programming
  1114. * ;gfx_dpio_set_reg(0xc02c, 0x1f030040)
  1115. */
  1116. cdv_sb_write(dev, ddi_reg->PreEmph1, 0x1f030040);
  1117. /* ;gfx_dpio_set_reg(0x8124, 0x00004000) */
  1118. index = 2 * premph + 1;
  1119. cdv_sb_write(dev, ddi_reg->PreEmph2, dp_vswing_premph_table[index]);
  1120. return;
  1121. }
  1122. /* Enable corresponding port and start training pattern 1 */
  1123. static void
  1124. cdv_intel_dp_start_link_train(struct gma_encoder *encoder)
  1125. {
  1126. struct drm_device *dev = encoder->base.dev;
  1127. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1128. int i;
  1129. uint8_t voltage;
  1130. bool clock_recovery = false;
  1131. int tries;
  1132. u32 reg;
  1133. uint32_t DP = intel_dp->DP;
  1134. DP |= DP_PORT_EN;
  1135. DP &= ~DP_LINK_TRAIN_MASK;
  1136. reg = DP;
  1137. reg |= DP_LINK_TRAIN_PAT_1;
  1138. /* Enable output, wait for it to become active */
  1139. REG_WRITE(intel_dp->output_reg, reg);
  1140. REG_READ(intel_dp->output_reg);
  1141. gma_wait_for_vblank(dev);
  1142. DRM_DEBUG_KMS("Link config\n");
  1143. /* Write the link configuration data */
  1144. cdv_intel_dp_aux_native_write(encoder, DP_LINK_BW_SET,
  1145. intel_dp->link_configuration,
  1146. 2);
  1147. memset(intel_dp->train_set, 0, 4);
  1148. voltage = 0;
  1149. tries = 0;
  1150. clock_recovery = false;
  1151. DRM_DEBUG_KMS("Start train\n");
  1152. reg = DP | DP_LINK_TRAIN_PAT_1;
  1153. for (;;) {
  1154. /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
  1155. DRM_DEBUG_KMS("DP Link Train Set %x, Link_config %x, %x\n",
  1156. intel_dp->train_set[0],
  1157. intel_dp->link_configuration[0],
  1158. intel_dp->link_configuration[1]);
  1159. if (!cdv_intel_dp_set_link_train(encoder, reg, DP_TRAINING_PATTERN_1)) {
  1160. DRM_DEBUG_KMS("Failure in aux-transfer setting pattern 1\n");
  1161. }
  1162. cdv_intel_dp_set_vswing_premph(encoder, intel_dp->train_set[0]);
  1163. /* Set training pattern 1 */
  1164. cdv_intel_dplink_set_level(encoder, DP_TRAINING_PATTERN_1);
  1165. udelay(200);
  1166. if (!cdv_intel_dp_get_link_status(encoder))
  1167. break;
  1168. DRM_DEBUG_KMS("DP Link status %x, %x, %x, %x, %x, %x\n",
  1169. intel_dp->link_status[0], intel_dp->link_status[1], intel_dp->link_status[2],
  1170. intel_dp->link_status[3], intel_dp->link_status[4], intel_dp->link_status[5]);
  1171. if (cdv_intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
  1172. DRM_DEBUG_KMS("PT1 train is done\n");
  1173. clock_recovery = true;
  1174. break;
  1175. }
  1176. /* Check to see if we've tried the max voltage */
  1177. for (i = 0; i < intel_dp->lane_count; i++)
  1178. if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  1179. break;
  1180. if (i == intel_dp->lane_count)
  1181. break;
  1182. /* Check to see if we've tried the same voltage 5 times */
  1183. if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  1184. ++tries;
  1185. if (tries == 5)
  1186. break;
  1187. } else
  1188. tries = 0;
  1189. voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  1190. /* Compute new intel_dp->train_set as requested by target */
  1191. cdv_intel_get_adjust_train(encoder);
  1192. }
  1193. if (!clock_recovery) {
  1194. DRM_DEBUG_KMS("failure in DP patter 1 training, train set %x\n", intel_dp->train_set[0]);
  1195. }
  1196. intel_dp->DP = DP;
  1197. }
  1198. static void
  1199. cdv_intel_dp_complete_link_train(struct gma_encoder *encoder)
  1200. {
  1201. struct drm_device *dev = encoder->base.dev;
  1202. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1203. bool channel_eq = false;
  1204. int tries, cr_tries;
  1205. u32 reg;
  1206. uint32_t DP = intel_dp->DP;
  1207. /* channel equalization */
  1208. tries = 0;
  1209. cr_tries = 0;
  1210. channel_eq = false;
  1211. DRM_DEBUG_KMS("\n");
  1212. reg = DP | DP_LINK_TRAIN_PAT_2;
  1213. for (;;) {
  1214. DRM_DEBUG_KMS("DP Link Train Set %x, Link_config %x, %x\n",
  1215. intel_dp->train_set[0],
  1216. intel_dp->link_configuration[0],
  1217. intel_dp->link_configuration[1]);
  1218. /* channel eq pattern */
  1219. if (!cdv_intel_dp_set_link_train(encoder, reg,
  1220. DP_TRAINING_PATTERN_2)) {
  1221. DRM_DEBUG_KMS("Failure in aux-transfer setting pattern 2\n");
  1222. }
  1223. /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
  1224. if (cr_tries > 5) {
  1225. DRM_ERROR("failed to train DP, aborting\n");
  1226. cdv_intel_dp_link_down(encoder);
  1227. break;
  1228. }
  1229. cdv_intel_dp_set_vswing_premph(encoder, intel_dp->train_set[0]);
  1230. cdv_intel_dplink_set_level(encoder, DP_TRAINING_PATTERN_2);
  1231. udelay(1000);
  1232. if (!cdv_intel_dp_get_link_status(encoder))
  1233. break;
  1234. DRM_DEBUG_KMS("DP Link status %x, %x, %x, %x, %x, %x\n",
  1235. intel_dp->link_status[0], intel_dp->link_status[1], intel_dp->link_status[2],
  1236. intel_dp->link_status[3], intel_dp->link_status[4], intel_dp->link_status[5]);
  1237. /* Make sure clock is still ok */
  1238. if (!cdv_intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
  1239. cdv_intel_dp_start_link_train(encoder);
  1240. cr_tries++;
  1241. continue;
  1242. }
  1243. if (cdv_intel_channel_eq_ok(encoder)) {
  1244. DRM_DEBUG_KMS("PT2 train is done\n");
  1245. channel_eq = true;
  1246. break;
  1247. }
  1248. /* Try 5 times, then try clock recovery if that fails */
  1249. if (tries > 5) {
  1250. cdv_intel_dp_link_down(encoder);
  1251. cdv_intel_dp_start_link_train(encoder);
  1252. tries = 0;
  1253. cr_tries++;
  1254. continue;
  1255. }
  1256. /* Compute new intel_dp->train_set as requested by target */
  1257. cdv_intel_get_adjust_train(encoder);
  1258. ++tries;
  1259. }
  1260. reg = DP | DP_LINK_TRAIN_OFF;
  1261. REG_WRITE(intel_dp->output_reg, reg);
  1262. REG_READ(intel_dp->output_reg);
  1263. cdv_intel_dp_aux_native_write_1(encoder,
  1264. DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
  1265. }
  1266. static void
  1267. cdv_intel_dp_link_down(struct gma_encoder *encoder)
  1268. {
  1269. struct drm_device *dev = encoder->base.dev;
  1270. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1271. uint32_t DP = intel_dp->DP;
  1272. if ((REG_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
  1273. return;
  1274. DRM_DEBUG_KMS("\n");
  1275. {
  1276. DP &= ~DP_LINK_TRAIN_MASK;
  1277. REG_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
  1278. }
  1279. REG_READ(intel_dp->output_reg);
  1280. msleep(17);
  1281. REG_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
  1282. REG_READ(intel_dp->output_reg);
  1283. }
  1284. static enum drm_connector_status cdv_dp_detect(struct gma_encoder *encoder)
  1285. {
  1286. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1287. enum drm_connector_status status;
  1288. status = connector_status_disconnected;
  1289. if (cdv_intel_dp_aux_native_read(encoder, 0x000, intel_dp->dpcd,
  1290. sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
  1291. {
  1292. if (intel_dp->dpcd[DP_DPCD_REV] != 0)
  1293. status = connector_status_connected;
  1294. }
  1295. if (status == connector_status_connected)
  1296. DRM_DEBUG_KMS("DPCD: Rev=%x LN_Rate=%x LN_CNT=%x LN_DOWNSP=%x\n",
  1297. intel_dp->dpcd[0], intel_dp->dpcd[1],
  1298. intel_dp->dpcd[2], intel_dp->dpcd[3]);
  1299. return status;
  1300. }
  1301. /**
  1302. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
  1303. *
  1304. * \return true if DP port is connected.
  1305. * \return false if DP port is disconnected.
  1306. */
  1307. static enum drm_connector_status
  1308. cdv_intel_dp_detect(struct drm_connector *connector, bool force)
  1309. {
  1310. struct gma_encoder *encoder = gma_attached_encoder(connector);
  1311. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1312. enum drm_connector_status status;
  1313. struct edid *edid = NULL;
  1314. int edp = is_edp(encoder);
  1315. intel_dp->has_audio = false;
  1316. if (edp)
  1317. cdv_intel_edp_panel_vdd_on(encoder);
  1318. status = cdv_dp_detect(encoder);
  1319. if (status != connector_status_connected) {
  1320. if (edp)
  1321. cdv_intel_edp_panel_vdd_off(encoder);
  1322. return status;
  1323. }
  1324. if (intel_dp->force_audio) {
  1325. intel_dp->has_audio = intel_dp->force_audio > 0;
  1326. } else {
  1327. edid = drm_get_edid(connector, &intel_dp->adapter);
  1328. if (edid) {
  1329. intel_dp->has_audio = drm_detect_monitor_audio(edid);
  1330. kfree(edid);
  1331. }
  1332. }
  1333. if (edp)
  1334. cdv_intel_edp_panel_vdd_off(encoder);
  1335. return connector_status_connected;
  1336. }
  1337. static int cdv_intel_dp_get_modes(struct drm_connector *connector)
  1338. {
  1339. struct gma_encoder *intel_encoder = gma_attached_encoder(connector);
  1340. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  1341. struct edid *edid = NULL;
  1342. int ret = 0;
  1343. int edp = is_edp(intel_encoder);
  1344. edid = drm_get_edid(connector, &intel_dp->adapter);
  1345. if (edid) {
  1346. drm_mode_connector_update_edid_property(connector, edid);
  1347. ret = drm_add_edid_modes(connector, edid);
  1348. kfree(edid);
  1349. }
  1350. if (is_edp(intel_encoder)) {
  1351. struct drm_device *dev = connector->dev;
  1352. struct drm_psb_private *dev_priv = dev->dev_private;
  1353. cdv_intel_edp_panel_vdd_off(intel_encoder);
  1354. if (ret) {
  1355. if (edp && !intel_dp->panel_fixed_mode) {
  1356. struct drm_display_mode *newmode;
  1357. list_for_each_entry(newmode, &connector->probed_modes,
  1358. head) {
  1359. if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
  1360. intel_dp->panel_fixed_mode =
  1361. drm_mode_duplicate(dev, newmode);
  1362. break;
  1363. }
  1364. }
  1365. }
  1366. return ret;
  1367. }
  1368. if (!intel_dp->panel_fixed_mode && dev_priv->lfp_lvds_vbt_mode) {
  1369. intel_dp->panel_fixed_mode =
  1370. drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
  1371. if (intel_dp->panel_fixed_mode) {
  1372. intel_dp->panel_fixed_mode->type |=
  1373. DRM_MODE_TYPE_PREFERRED;
  1374. }
  1375. }
  1376. if (intel_dp->panel_fixed_mode != NULL) {
  1377. struct drm_display_mode *mode;
  1378. mode = drm_mode_duplicate(dev, intel_dp->panel_fixed_mode);
  1379. drm_mode_probed_add(connector, mode);
  1380. return 1;
  1381. }
  1382. }
  1383. return ret;
  1384. }
  1385. static bool
  1386. cdv_intel_dp_detect_audio(struct drm_connector *connector)
  1387. {
  1388. struct gma_encoder *encoder = gma_attached_encoder(connector);
  1389. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1390. struct edid *edid;
  1391. bool has_audio = false;
  1392. int edp = is_edp(encoder);
  1393. if (edp)
  1394. cdv_intel_edp_panel_vdd_on(encoder);
  1395. edid = drm_get_edid(connector, &intel_dp->adapter);
  1396. if (edid) {
  1397. has_audio = drm_detect_monitor_audio(edid);
  1398. kfree(edid);
  1399. }
  1400. if (edp)
  1401. cdv_intel_edp_panel_vdd_off(encoder);
  1402. return has_audio;
  1403. }
  1404. static int
  1405. cdv_intel_dp_set_property(struct drm_connector *connector,
  1406. struct drm_property *property,
  1407. uint64_t val)
  1408. {
  1409. struct drm_psb_private *dev_priv = connector->dev->dev_private;
  1410. struct gma_encoder *encoder = gma_attached_encoder(connector);
  1411. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  1412. int ret;
  1413. ret = drm_object_property_set_value(&connector->base, property, val);
  1414. if (ret)
  1415. return ret;
  1416. if (property == dev_priv->force_audio_property) {
  1417. int i = val;
  1418. bool has_audio;
  1419. if (i == intel_dp->force_audio)
  1420. return 0;
  1421. intel_dp->force_audio = i;
  1422. if (i == 0)
  1423. has_audio = cdv_intel_dp_detect_audio(connector);
  1424. else
  1425. has_audio = i > 0;
  1426. if (has_audio == intel_dp->has_audio)
  1427. return 0;
  1428. intel_dp->has_audio = has_audio;
  1429. goto done;
  1430. }
  1431. if (property == dev_priv->broadcast_rgb_property) {
  1432. if (val == !!intel_dp->color_range)
  1433. return 0;
  1434. intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
  1435. goto done;
  1436. }
  1437. return -EINVAL;
  1438. done:
  1439. if (encoder->base.crtc) {
  1440. struct drm_crtc *crtc = encoder->base.crtc;
  1441. drm_crtc_helper_set_mode(crtc, &crtc->mode,
  1442. crtc->x, crtc->y,
  1443. crtc->fb);
  1444. }
  1445. return 0;
  1446. }
  1447. static void
  1448. cdv_intel_dp_destroy(struct drm_connector *connector)
  1449. {
  1450. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  1451. struct cdv_intel_dp *intel_dp = gma_encoder->dev_priv;
  1452. if (is_edp(gma_encoder)) {
  1453. /* cdv_intel_panel_destroy_backlight(connector->dev); */
  1454. if (intel_dp->panel_fixed_mode) {
  1455. kfree(intel_dp->panel_fixed_mode);
  1456. intel_dp->panel_fixed_mode = NULL;
  1457. }
  1458. }
  1459. i2c_del_adapter(&intel_dp->adapter);
  1460. drm_sysfs_connector_remove(connector);
  1461. drm_connector_cleanup(connector);
  1462. kfree(connector);
  1463. }
  1464. static void cdv_intel_dp_encoder_destroy(struct drm_encoder *encoder)
  1465. {
  1466. drm_encoder_cleanup(encoder);
  1467. }
  1468. static const struct drm_encoder_helper_funcs cdv_intel_dp_helper_funcs = {
  1469. .dpms = cdv_intel_dp_dpms,
  1470. .mode_fixup = cdv_intel_dp_mode_fixup,
  1471. .prepare = cdv_intel_dp_prepare,
  1472. .mode_set = cdv_intel_dp_mode_set,
  1473. .commit = cdv_intel_dp_commit,
  1474. };
  1475. static const struct drm_connector_funcs cdv_intel_dp_connector_funcs = {
  1476. .dpms = drm_helper_connector_dpms,
  1477. .detect = cdv_intel_dp_detect,
  1478. .fill_modes = drm_helper_probe_single_connector_modes,
  1479. .set_property = cdv_intel_dp_set_property,
  1480. .destroy = cdv_intel_dp_destroy,
  1481. };
  1482. static const struct drm_connector_helper_funcs cdv_intel_dp_connector_helper_funcs = {
  1483. .get_modes = cdv_intel_dp_get_modes,
  1484. .mode_valid = cdv_intel_dp_mode_valid,
  1485. .best_encoder = gma_best_encoder,
  1486. };
  1487. static const struct drm_encoder_funcs cdv_intel_dp_enc_funcs = {
  1488. .destroy = cdv_intel_dp_encoder_destroy,
  1489. };
  1490. static void cdv_intel_dp_add_properties(struct drm_connector *connector)
  1491. {
  1492. cdv_intel_attach_force_audio_property(connector);
  1493. cdv_intel_attach_broadcast_rgb_property(connector);
  1494. }
  1495. /* check the VBT to see whether the eDP is on DP-D port */
  1496. static bool cdv_intel_dpc_is_edp(struct drm_device *dev)
  1497. {
  1498. struct drm_psb_private *dev_priv = dev->dev_private;
  1499. struct child_device_config *p_child;
  1500. int i;
  1501. if (!dev_priv->child_dev_num)
  1502. return false;
  1503. for (i = 0; i < dev_priv->child_dev_num; i++) {
  1504. p_child = dev_priv->child_dev + i;
  1505. if (p_child->dvo_port == PORT_IDPC &&
  1506. p_child->device_type == DEVICE_TYPE_eDP)
  1507. return true;
  1508. }
  1509. return false;
  1510. }
  1511. /* Cedarview display clock gating
  1512. We need this disable dot get correct behaviour while enabling
  1513. DP/eDP. TODO - investigate if we can turn it back to normality
  1514. after enabling */
  1515. static void cdv_disable_intel_clock_gating(struct drm_device *dev)
  1516. {
  1517. u32 reg_value;
  1518. reg_value = REG_READ(DSPCLK_GATE_D);
  1519. reg_value |= (DPUNIT_PIPEB_GATE_DISABLE |
  1520. DPUNIT_PIPEA_GATE_DISABLE |
  1521. DPCUNIT_CLOCK_GATE_DISABLE |
  1522. DPLSUNIT_CLOCK_GATE_DISABLE |
  1523. DPOUNIT_CLOCK_GATE_DISABLE |
  1524. DPIOUNIT_CLOCK_GATE_DISABLE);
  1525. REG_WRITE(DSPCLK_GATE_D, reg_value);
  1526. udelay(500);
  1527. }
  1528. void
  1529. cdv_intel_dp_init(struct drm_device *dev, struct psb_intel_mode_device *mode_dev, int output_reg)
  1530. {
  1531. struct gma_encoder *gma_encoder;
  1532. struct gma_connector *gma_connector;
  1533. struct drm_connector *connector;
  1534. struct drm_encoder *encoder;
  1535. struct cdv_intel_dp *intel_dp;
  1536. const char *name = NULL;
  1537. int type = DRM_MODE_CONNECTOR_DisplayPort;
  1538. gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
  1539. if (!gma_encoder)
  1540. return;
  1541. gma_connector = kzalloc(sizeof(struct gma_connector), GFP_KERNEL);
  1542. if (!gma_connector)
  1543. goto err_connector;
  1544. intel_dp = kzalloc(sizeof(struct cdv_intel_dp), GFP_KERNEL);
  1545. if (!intel_dp)
  1546. goto err_priv;
  1547. if ((output_reg == DP_C) && cdv_intel_dpc_is_edp(dev))
  1548. type = DRM_MODE_CONNECTOR_eDP;
  1549. connector = &gma_connector->base;
  1550. encoder = &gma_encoder->base;
  1551. drm_connector_init(dev, connector, &cdv_intel_dp_connector_funcs, type);
  1552. drm_encoder_init(dev, encoder, &cdv_intel_dp_enc_funcs, DRM_MODE_ENCODER_TMDS);
  1553. gma_connector_attach_encoder(gma_connector, gma_encoder);
  1554. if (type == DRM_MODE_CONNECTOR_DisplayPort)
  1555. gma_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  1556. else
  1557. gma_encoder->type = INTEL_OUTPUT_EDP;
  1558. gma_encoder->dev_priv=intel_dp;
  1559. intel_dp->encoder = gma_encoder;
  1560. intel_dp->output_reg = output_reg;
  1561. drm_encoder_helper_add(encoder, &cdv_intel_dp_helper_funcs);
  1562. drm_connector_helper_add(connector, &cdv_intel_dp_connector_helper_funcs);
  1563. connector->polled = DRM_CONNECTOR_POLL_HPD;
  1564. connector->interlace_allowed = false;
  1565. connector->doublescan_allowed = false;
  1566. drm_sysfs_connector_add(connector);
  1567. /* Set up the DDC bus. */
  1568. switch (output_reg) {
  1569. case DP_B:
  1570. name = "DPDDC-B";
  1571. gma_encoder->ddi_select = (DP_MASK | DDI0_SELECT);
  1572. break;
  1573. case DP_C:
  1574. name = "DPDDC-C";
  1575. gma_encoder->ddi_select = (DP_MASK | DDI1_SELECT);
  1576. break;
  1577. }
  1578. cdv_disable_intel_clock_gating(dev);
  1579. cdv_intel_dp_i2c_init(gma_connector, gma_encoder, name);
  1580. /* FIXME:fail check */
  1581. cdv_intel_dp_add_properties(connector);
  1582. if (is_edp(gma_encoder)) {
  1583. int ret;
  1584. struct edp_power_seq cur;
  1585. u32 pp_on, pp_off, pp_div;
  1586. u32 pwm_ctrl;
  1587. pp_on = REG_READ(PP_CONTROL);
  1588. pp_on &= ~PANEL_UNLOCK_MASK;
  1589. pp_on |= PANEL_UNLOCK_REGS;
  1590. REG_WRITE(PP_CONTROL, pp_on);
  1591. pwm_ctrl = REG_READ(BLC_PWM_CTL2);
  1592. pwm_ctrl |= PWM_PIPE_B;
  1593. REG_WRITE(BLC_PWM_CTL2, pwm_ctrl);
  1594. pp_on = REG_READ(PP_ON_DELAYS);
  1595. pp_off = REG_READ(PP_OFF_DELAYS);
  1596. pp_div = REG_READ(PP_DIVISOR);
  1597. /* Pull timing values out of registers */
  1598. cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
  1599. PANEL_POWER_UP_DELAY_SHIFT;
  1600. cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
  1601. PANEL_LIGHT_ON_DELAY_SHIFT;
  1602. cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
  1603. PANEL_LIGHT_OFF_DELAY_SHIFT;
  1604. cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
  1605. PANEL_POWER_DOWN_DELAY_SHIFT;
  1606. cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
  1607. PANEL_POWER_CYCLE_DELAY_SHIFT);
  1608. DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
  1609. cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
  1610. intel_dp->panel_power_up_delay = cur.t1_t3 / 10;
  1611. intel_dp->backlight_on_delay = cur.t8 / 10;
  1612. intel_dp->backlight_off_delay = cur.t9 / 10;
  1613. intel_dp->panel_power_down_delay = cur.t10 / 10;
  1614. intel_dp->panel_power_cycle_delay = (cur.t11_t12 - 1) * 100;
  1615. DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
  1616. intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
  1617. intel_dp->panel_power_cycle_delay);
  1618. DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
  1619. intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
  1620. cdv_intel_edp_panel_vdd_on(gma_encoder);
  1621. ret = cdv_intel_dp_aux_native_read(gma_encoder, DP_DPCD_REV,
  1622. intel_dp->dpcd,
  1623. sizeof(intel_dp->dpcd));
  1624. cdv_intel_edp_panel_vdd_off(gma_encoder);
  1625. if (ret == 0) {
  1626. /* if this fails, presume the device is a ghost */
  1627. DRM_INFO("failed to retrieve link info, disabling eDP\n");
  1628. cdv_intel_dp_encoder_destroy(encoder);
  1629. cdv_intel_dp_destroy(connector);
  1630. goto err_priv;
  1631. } else {
  1632. DRM_DEBUG_KMS("DPCD: Rev=%x LN_Rate=%x LN_CNT=%x LN_DOWNSP=%x\n",
  1633. intel_dp->dpcd[0], intel_dp->dpcd[1],
  1634. intel_dp->dpcd[2], intel_dp->dpcd[3]);
  1635. }
  1636. /* The CDV reference driver moves pnale backlight setup into the displays that
  1637. have a backlight: this is a good idea and one we should probably adopt, however
  1638. we need to migrate all the drivers before we can do that */
  1639. /*cdv_intel_panel_setup_backlight(dev); */
  1640. }
  1641. return;
  1642. err_priv:
  1643. kfree(gma_connector);
  1644. err_connector:
  1645. kfree(gma_encoder);
  1646. }