cdv_intel_dp.c 56 KB

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