cdv_intel_dp.c 55 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_print.h>
  36. #include <drm/drm_simple_kms_helper.h>
  37. #include "gma_display.h"
  38. #include "psb_drv.h"
  39. #include "psb_intel_drv.h"
  40. #include "psb_intel_reg.h"
  41. /**
  42. * struct i2c_algo_dp_aux_data - driver interface structure for i2c over dp
  43. * aux algorithm
  44. * @running: set by the algo indicating whether an i2c is ongoing or whether
  45. * the i2c bus is quiescent
  46. * @address: i2c target address for the currently ongoing transfer
  47. * @aux_ch: driver callback to transfer a single byte of the i2c payload
  48. */
  49. struct i2c_algo_dp_aux_data {
  50. bool running;
  51. u16 address;
  52. int (*aux_ch) (struct i2c_adapter *adapter,
  53. int mode, uint8_t write_byte,
  54. uint8_t *read_byte);
  55. };
  56. /* Run a single AUX_CH I2C transaction, writing/reading data as necessary */
  57. static int
  58. i2c_algo_dp_aux_transaction(struct i2c_adapter *adapter, int mode,
  59. uint8_t write_byte, uint8_t *read_byte)
  60. {
  61. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  62. int ret;
  63. ret = (*algo_data->aux_ch)(adapter, mode,
  64. write_byte, read_byte);
  65. return ret;
  66. }
  67. /*
  68. * I2C over AUX CH
  69. */
  70. /*
  71. * Send the address. If the I2C link is running, this 'restarts'
  72. * the connection with the new address, this is used for doing
  73. * a write followed by a read (as needed for DDC)
  74. */
  75. static int
  76. i2c_algo_dp_aux_address(struct i2c_adapter *adapter, u16 address, bool reading)
  77. {
  78. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  79. int mode = MODE_I2C_START;
  80. if (reading)
  81. mode |= MODE_I2C_READ;
  82. else
  83. mode |= MODE_I2C_WRITE;
  84. algo_data->address = address;
  85. algo_data->running = true;
  86. return i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
  87. }
  88. /*
  89. * Stop the I2C transaction. This closes out the link, sending
  90. * a bare address packet with the MOT bit turned off
  91. */
  92. static void
  93. i2c_algo_dp_aux_stop(struct i2c_adapter *adapter, bool reading)
  94. {
  95. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  96. int mode = MODE_I2C_STOP;
  97. if (reading)
  98. mode |= MODE_I2C_READ;
  99. else
  100. mode |= MODE_I2C_WRITE;
  101. if (algo_data->running) {
  102. (void) i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
  103. algo_data->running = false;
  104. }
  105. }
  106. /*
  107. * Write a single byte to the current I2C address, the
  108. * I2C link must be running or this returns -EIO
  109. */
  110. static int
  111. i2c_algo_dp_aux_put_byte(struct i2c_adapter *adapter, u8 byte)
  112. {
  113. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  114. if (!algo_data->running)
  115. return -EIO;
  116. return i2c_algo_dp_aux_transaction(adapter, MODE_I2C_WRITE, byte, NULL);
  117. }
  118. /*
  119. * Read a single byte from the current I2C address, the
  120. * I2C link must be running or this returns -EIO
  121. */
  122. static int
  123. i2c_algo_dp_aux_get_byte(struct i2c_adapter *adapter, u8 *byte_ret)
  124. {
  125. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  126. if (!algo_data->running)
  127. return -EIO;
  128. return i2c_algo_dp_aux_transaction(adapter, MODE_I2C_READ, 0, byte_ret);
  129. }
  130. static int
  131. i2c_algo_dp_aux_xfer(struct i2c_adapter *adapter,
  132. struct i2c_msg *msgs,
  133. int num)
  134. {
  135. int ret = 0;
  136. bool reading = false;
  137. int m;
  138. int b;
  139. for (m = 0; m < num; m++) {
  140. u16 len = msgs[m].len;
  141. u8 *buf = msgs[m].buf;
  142. reading = (msgs[m].flags & I2C_M_RD) != 0;
  143. ret = i2c_algo_dp_aux_address(adapter, msgs[m].addr, reading);
  144. if (ret < 0)
  145. break;
  146. if (reading) {
  147. for (b = 0; b < len; b++) {
  148. ret = i2c_algo_dp_aux_get_byte(adapter, &buf[b]);
  149. if (ret < 0)
  150. break;
  151. }
  152. } else {
  153. for (b = 0; b < len; b++) {
  154. ret = i2c_algo_dp_aux_put_byte(adapter, buf[b]);
  155. if (ret < 0)
  156. break;
  157. }
  158. }
  159. if (ret < 0)
  160. break;
  161. }
  162. if (ret >= 0)
  163. ret = num;
  164. i2c_algo_dp_aux_stop(adapter, reading);
  165. DRM_DEBUG_KMS("dp_aux_xfer return %d\n", ret);
  166. return ret;
  167. }
  168. static u32
  169. i2c_algo_dp_aux_functionality(struct i2c_adapter *adapter)
  170. {
  171. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  172. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  173. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  174. I2C_FUNC_10BIT_ADDR;
  175. }
  176. static const struct i2c_algorithm i2c_dp_aux_algo = {
  177. .master_xfer = i2c_algo_dp_aux_xfer,
  178. .functionality = i2c_algo_dp_aux_functionality,
  179. };
  180. static void
  181. i2c_dp_aux_reset_bus(struct i2c_adapter *adapter)
  182. {
  183. (void) i2c_algo_dp_aux_address(adapter, 0, false);
  184. (void) i2c_algo_dp_aux_stop(adapter, false);
  185. }
  186. static int
  187. i2c_dp_aux_prepare_bus(struct i2c_adapter *adapter)
  188. {
  189. adapter->algo = &i2c_dp_aux_algo;
  190. adapter->retries = 3;
  191. i2c_dp_aux_reset_bus(adapter);
  192. return 0;
  193. }
  194. /*
  195. * FIXME: This is the old dp aux helper, gma500 is the last driver that needs to
  196. * be ported over to the new helper code in drm_dp_helper.c like i915 or radeon.
  197. */
  198. static int
  199. i2c_dp_aux_add_bus(struct i2c_adapter *adapter)
  200. {
  201. int error;
  202. error = i2c_dp_aux_prepare_bus(adapter);
  203. if (error)
  204. return error;
  205. error = i2c_add_adapter(adapter);
  206. return error;
  207. }
  208. #define _wait_for(COND, MS, W) ({ \
  209. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS); \
  210. int ret__ = 0; \
  211. while (! (COND)) { \
  212. if (time_after(jiffies, timeout__)) { \
  213. ret__ = -ETIMEDOUT; \
  214. break; \
  215. } \
  216. if (W && !in_dbg_master()) msleep(W); \
  217. } \
  218. ret__; \
  219. })
  220. #define wait_for(COND, MS) _wait_for(COND, MS, 1)
  221. #define DP_LINK_CHECK_TIMEOUT (10 * 1000)
  222. #define DP_LINK_CONFIGURATION_SIZE 9
  223. #define CDV_FAST_LINK_TRAIN 1
  224. struct cdv_intel_dp {
  225. uint32_t output_reg;
  226. uint32_t DP;
  227. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
  228. bool has_audio;
  229. int force_audio;
  230. uint32_t color_range;
  231. uint8_t link_bw;
  232. uint8_t lane_count;
  233. uint8_t dpcd[4];
  234. struct gma_encoder *encoder;
  235. struct i2c_adapter adapter;
  236. struct i2c_algo_dp_aux_data algo;
  237. uint8_t train_set[4];
  238. uint8_t link_status[DP_LINK_STATUS_SIZE];
  239. int panel_power_up_delay;
  240. int panel_power_down_delay;
  241. int panel_power_cycle_delay;
  242. int backlight_on_delay;
  243. int backlight_off_delay;
  244. struct drm_display_mode *panel_fixed_mode; /* for eDP */
  245. bool panel_on;
  246. };
  247. struct ddi_regoff {
  248. uint32_t PreEmph1;
  249. uint32_t PreEmph2;
  250. uint32_t VSwing1;
  251. uint32_t VSwing2;
  252. uint32_t VSwing3;
  253. uint32_t VSwing4;
  254. uint32_t VSwing5;
  255. };
  256. static struct ddi_regoff ddi_DP_train_table[] = {
  257. {.PreEmph1 = 0x812c, .PreEmph2 = 0x8124, .VSwing1 = 0x8154,
  258. .VSwing2 = 0x8148, .VSwing3 = 0x814C, .VSwing4 = 0x8150,
  259. .VSwing5 = 0x8158,},
  260. {.PreEmph1 = 0x822c, .PreEmph2 = 0x8224, .VSwing1 = 0x8254,
  261. .VSwing2 = 0x8248, .VSwing3 = 0x824C, .VSwing4 = 0x8250,
  262. .VSwing5 = 0x8258,},
  263. };
  264. static uint32_t dp_vswing_premph_table[] = {
  265. 0x55338954, 0x4000,
  266. 0x554d8954, 0x2000,
  267. 0x55668954, 0,
  268. 0x559ac0d4, 0x6000,
  269. };
  270. /**
  271. * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
  272. * @encoder: GMA encoder struct
  273. *
  274. * If a CPU or PCH DP output is attached to an eDP panel, this function
  275. * will return true, and false otherwise.
  276. */
  277. static bool is_edp(struct gma_encoder *encoder)
  278. {
  279. return encoder->type == INTEL_OUTPUT_EDP;
  280. }
  281. static void cdv_intel_dp_start_link_train(struct gma_encoder *encoder);
  282. static void cdv_intel_dp_complete_link_train(struct gma_encoder *encoder);
  283. static void cdv_intel_dp_link_down(struct gma_encoder *encoder);
  284. static int
  285. cdv_intel_dp_max_lane_count(struct gma_encoder *encoder)
  286. {
  287. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  288. int max_lane_count = 4;
  289. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
  290. max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
  291. switch (max_lane_count) {
  292. case 1: case 2: case 4:
  293. break;
  294. default:
  295. max_lane_count = 4;
  296. }
  297. }
  298. return max_lane_count;
  299. }
  300. static int
  301. cdv_intel_dp_max_link_bw(struct gma_encoder *encoder)
  302. {
  303. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  304. int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
  305. switch (max_link_bw) {
  306. case DP_LINK_BW_1_62:
  307. case DP_LINK_BW_2_7:
  308. break;
  309. default:
  310. max_link_bw = DP_LINK_BW_1_62;
  311. break;
  312. }
  313. return max_link_bw;
  314. }
  315. static int
  316. cdv_intel_dp_link_clock(uint8_t link_bw)
  317. {
  318. if (link_bw == DP_LINK_BW_2_7)
  319. return 270000;
  320. else
  321. return 162000;
  322. }
  323. static int
  324. cdv_intel_dp_link_required(int pixel_clock, int bpp)
  325. {
  326. return (pixel_clock * bpp + 7) / 8;
  327. }
  328. static int
  329. cdv_intel_dp_max_data_rate(int max_link_clock, int max_lanes)
  330. {
  331. return (max_link_clock * max_lanes * 19) / 20;
  332. }
  333. static void cdv_intel_edp_panel_vdd_on(struct gma_encoder *intel_encoder)
  334. {
  335. struct drm_device *dev = intel_encoder->base.dev;
  336. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  337. u32 pp;
  338. if (intel_dp->panel_on) {
  339. DRM_DEBUG_KMS("Skip VDD on because of panel on\n");
  340. return;
  341. }
  342. DRM_DEBUG_KMS("\n");
  343. pp = REG_READ(PP_CONTROL);
  344. pp |= EDP_FORCE_VDD;
  345. REG_WRITE(PP_CONTROL, pp);
  346. REG_READ(PP_CONTROL);
  347. msleep(intel_dp->panel_power_up_delay);
  348. }
  349. static void cdv_intel_edp_panel_vdd_off(struct gma_encoder *intel_encoder)
  350. {
  351. struct drm_device *dev = intel_encoder->base.dev;
  352. u32 pp;
  353. DRM_DEBUG_KMS("\n");
  354. pp = REG_READ(PP_CONTROL);
  355. pp &= ~EDP_FORCE_VDD;
  356. REG_WRITE(PP_CONTROL, pp);
  357. REG_READ(PP_CONTROL);
  358. }
  359. /* Returns true if the panel was already on when called */
  360. static bool cdv_intel_edp_panel_on(struct gma_encoder *intel_encoder)
  361. {
  362. struct drm_device *dev = intel_encoder->base.dev;
  363. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  364. u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_NONE;
  365. if (intel_dp->panel_on)
  366. return true;
  367. DRM_DEBUG_KMS("\n");
  368. pp = REG_READ(PP_CONTROL);
  369. pp &= ~PANEL_UNLOCK_MASK;
  370. pp |= (PANEL_UNLOCK_REGS | POWER_TARGET_ON);
  371. REG_WRITE(PP_CONTROL, pp);
  372. REG_READ(PP_CONTROL);
  373. if (wait_for(((REG_READ(PP_STATUS) & idle_on_mask) == idle_on_mask), 1000)) {
  374. DRM_DEBUG_KMS("Error in Powering up eDP panel, status %x\n", REG_READ(PP_STATUS));
  375. intel_dp->panel_on = false;
  376. } else
  377. intel_dp->panel_on = true;
  378. msleep(intel_dp->panel_power_up_delay);
  379. return false;
  380. }
  381. static void cdv_intel_edp_panel_off (struct gma_encoder *intel_encoder)
  382. {
  383. struct drm_device *dev = intel_encoder->base.dev;
  384. u32 pp, idle_off_mask = PP_ON ;
  385. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  386. DRM_DEBUG_KMS("\n");
  387. pp = REG_READ(PP_CONTROL);
  388. if ((pp & POWER_TARGET_ON) == 0)
  389. return;
  390. intel_dp->panel_on = false;
  391. pp &= ~PANEL_UNLOCK_MASK;
  392. /* ILK workaround: disable reset around power sequence */
  393. pp &= ~POWER_TARGET_ON;
  394. pp &= ~EDP_FORCE_VDD;
  395. pp &= ~EDP_BLC_ENABLE;
  396. REG_WRITE(PP_CONTROL, pp);
  397. REG_READ(PP_CONTROL);
  398. DRM_DEBUG_KMS("PP_STATUS %x\n", REG_READ(PP_STATUS));
  399. if (wait_for((REG_READ(PP_STATUS) & idle_off_mask) == 0, 1000)) {
  400. DRM_DEBUG_KMS("Error in turning off Panel\n");
  401. }
  402. msleep(intel_dp->panel_power_cycle_delay);
  403. DRM_DEBUG_KMS("Over\n");
  404. }
  405. static void cdv_intel_edp_backlight_on (struct gma_encoder *intel_encoder)
  406. {
  407. struct drm_device *dev = intel_encoder->base.dev;
  408. u32 pp;
  409. DRM_DEBUG_KMS("\n");
  410. /*
  411. * If we enable the backlight right away following a panel power
  412. * on, we may see slight flicker as the panel syncs with the eDP
  413. * link. So delay a bit to make sure the image is solid before
  414. * allowing it to appear.
  415. */
  416. msleep(300);
  417. pp = REG_READ(PP_CONTROL);
  418. pp |= EDP_BLC_ENABLE;
  419. REG_WRITE(PP_CONTROL, pp);
  420. gma_backlight_enable(dev);
  421. }
  422. static void cdv_intel_edp_backlight_off (struct gma_encoder *intel_encoder)
  423. {
  424. struct drm_device *dev = intel_encoder->base.dev;
  425. struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
  426. u32 pp;
  427. DRM_DEBUG_KMS("\n");
  428. gma_backlight_disable(dev);
  429. msleep(10);
  430. pp = REG_READ(PP_CONTROL);
  431. pp &= ~EDP_BLC_ENABLE;
  432. REG_WRITE(PP_CONTROL, pp);
  433. msleep(intel_dp->backlight_off_delay);
  434. }
  435. static enum drm_mode_status
  436. cdv_intel_dp_mode_valid(struct drm_connector *connector,
  437. const struct drm_display_mode *mode)
  438. {
  439. struct gma_encoder *encoder = gma_attached_encoder(connector);
  440. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  441. int max_link_clock = cdv_intel_dp_link_clock(cdv_intel_dp_max_link_bw(encoder));
  442. int max_lanes = cdv_intel_dp_max_lane_count(encoder);
  443. struct drm_psb_private *dev_priv = to_drm_psb_private(connector->dev);
  444. if (is_edp(encoder) && intel_dp->panel_fixed_mode) {
  445. if (mode->hdisplay > intel_dp->panel_fixed_mode->hdisplay)
  446. return MODE_PANEL;
  447. if (mode->vdisplay > intel_dp->panel_fixed_mode->vdisplay)
  448. return MODE_PANEL;
  449. }
  450. /* only refuse the mode on non eDP since we have seen some weird eDP panels
  451. which are outside spec tolerances but somehow work by magic */
  452. if (!is_edp(encoder) &&
  453. (cdv_intel_dp_link_required(mode->clock, dev_priv->edp.bpp)
  454. > cdv_intel_dp_max_data_rate(max_link_clock, max_lanes)))
  455. return MODE_CLOCK_HIGH;
  456. if (is_edp(encoder)) {
  457. if (cdv_intel_dp_link_required(mode->clock, 24)
  458. > cdv_intel_dp_max_data_rate(max_link_clock, max_lanes))
  459. return MODE_CLOCK_HIGH;
  460. }
  461. if (mode->clock < 10000)
  462. return MODE_CLOCK_LOW;
  463. return MODE_OK;
  464. }
  465. static uint32_t
  466. pack_aux(uint8_t *src, int src_bytes)
  467. {
  468. int i;
  469. uint32_t v = 0;
  470. if (src_bytes > 4)
  471. src_bytes = 4;
  472. for (i = 0; i < src_bytes; i++)
  473. v |= ((uint32_t) src[i]) << ((3-i) * 8);
  474. return v;
  475. }
  476. static void
  477. unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
  478. {
  479. int i;
  480. if (dst_bytes > 4)
  481. dst_bytes = 4;
  482. for (i = 0; i < dst_bytes; i++)
  483. dst[i] = src >> ((3-i) * 8);
  484. }
  485. static int
  486. cdv_intel_dp_aux_ch(struct gma_encoder *encoder,
  487. uint8_t *send, int send_bytes,
  488. uint8_t *recv, int recv_size)
  489. {
  490. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  491. uint32_t output_reg = intel_dp->output_reg;
  492. struct drm_device *dev = encoder->base.dev;
  493. uint32_t ch_ctl = output_reg + 0x10;
  494. uint32_t ch_data = ch_ctl + 4;
  495. int i;
  496. int recv_bytes;
  497. uint32_t status;
  498. uint32_t aux_clock_divider;
  499. int try, precharge;
  500. /* The clock divider is based off the hrawclk,
  501. * and would like to run at 2MHz. So, take the
  502. * hrawclk value and divide by 2 and use that
  503. * On CDV platform it uses 200MHz as hrawclk.
  504. *
  505. */
  506. aux_clock_divider = 200 / 2;
  507. precharge = 4;
  508. if (is_edp(encoder))
  509. precharge = 10;
  510. if (REG_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
  511. DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
  512. REG_READ(ch_ctl));
  513. return -EBUSY;
  514. }
  515. /* Must try at least 3 times according to DP spec */
  516. for (try = 0; try < 5; try++) {
  517. /* Load the send data into the aux channel data registers */
  518. for (i = 0; i < send_bytes; i += 4)
  519. REG_WRITE(ch_data + i,
  520. pack_aux(send + i, send_bytes - i));
  521. /* Send the command and wait for it to complete */
  522. REG_WRITE(ch_ctl,
  523. DP_AUX_CH_CTL_SEND_BUSY |
  524. DP_AUX_CH_CTL_TIME_OUT_400us |
  525. (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  526. (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  527. (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
  528. DP_AUX_CH_CTL_DONE |
  529. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  530. DP_AUX_CH_CTL_RECEIVE_ERROR);
  531. for (;;) {
  532. status = REG_READ(ch_ctl);
  533. if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  534. break;
  535. udelay(100);
  536. }
  537. /* Clear done status and any errors */
  538. REG_WRITE(ch_ctl,
  539. status |
  540. DP_AUX_CH_CTL_DONE |
  541. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  542. DP_AUX_CH_CTL_RECEIVE_ERROR);
  543. if (status & DP_AUX_CH_CTL_DONE)
  544. break;
  545. }
  546. if ((status & DP_AUX_CH_CTL_DONE) == 0) {
  547. DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
  548. return -EBUSY;
  549. }
  550. /* Check for timeout or receive error.
  551. * Timeouts occur when the sink is not connected
  552. */
  553. if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  554. DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
  555. return -EIO;
  556. }
  557. /* Timeouts occur when the device isn't connected, so they're
  558. * "normal" -- don't fill the kernel log with these */
  559. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  560. DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
  561. return -ETIMEDOUT;
  562. }
  563. /* Unload any bytes sent back from the other side */
  564. recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  565. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  566. if (recv_bytes > recv_size)
  567. recv_bytes = recv_size;
  568. for (i = 0; i < recv_bytes; i += 4)
  569. unpack_aux(REG_READ(ch_data + i),
  570. recv + i, recv_bytes - i);
  571. return recv_bytes;
  572. }
  573. /* Write data to the aux channel in native mode */
  574. static int
  575. cdv_intel_dp_aux_native_write(struct gma_encoder *encoder,
  576. uint16_t address, uint8_t *send, int send_bytes)
  577. {
  578. int ret;
  579. uint8_t msg[20];
  580. int msg_bytes;
  581. uint8_t ack;
  582. if (send_bytes > 16)
  583. return -1;
  584. msg[0] = DP_AUX_NATIVE_WRITE << 4;
  585. msg[1] = address >> 8;
  586. msg[2] = address & 0xff;
  587. msg[3] = send_bytes - 1;
  588. memcpy(&msg[4], send, send_bytes);
  589. msg_bytes = send_bytes + 4;
  590. for (;;) {
  591. ret = cdv_intel_dp_aux_ch(encoder, msg, msg_bytes, &ack, 1);
  592. if (ret < 0)
  593. return ret;
  594. ack >>= 4;
  595. if ((ack & DP_AUX_NATIVE_REPLY_MASK) == DP_AUX_NATIVE_REPLY_ACK)
  596. break;
  597. else if ((ack & DP_AUX_NATIVE_REPLY_MASK) == DP_AUX_NATIVE_REPLY_DEFER)
  598. udelay(100);
  599. else
  600. return -EIO;
  601. }
  602. return send_bytes;
  603. }
  604. /* Write a single byte to the aux channel in native mode */
  605. static int
  606. cdv_intel_dp_aux_native_write_1(struct gma_encoder *encoder,
  607. uint16_t address, uint8_t byte)
  608. {
  609. return cdv_intel_dp_aux_native_write(encoder, address, &byte, 1);
  610. }
  611. /* read bytes from a native aux channel */
  612. static int
  613. cdv_intel_dp_aux_native_read(struct gma_encoder *encoder,
  614. uint16_t address, uint8_t *recv, int recv_bytes)
  615. {
  616. uint8_t msg[4];
  617. int msg_bytes;
  618. uint8_t reply[20];
  619. int reply_bytes;
  620. uint8_t ack;
  621. int ret;
  622. msg[0] = DP_AUX_NATIVE_READ << 4;
  623. msg[1] = address >> 8;
  624. msg[2] = address & 0xff;
  625. msg[3] = recv_bytes - 1;
  626. msg_bytes = 4;
  627. reply_bytes = recv_bytes + 1;
  628. for (;;) {
  629. ret = cdv_intel_dp_aux_ch(encoder, msg, msg_bytes,
  630. reply, reply_bytes);
  631. if (ret == 0)
  632. return -EPROTO;
  633. if (ret < 0)
  634. return ret;
  635. ack = reply[0] >> 4;
  636. if ((ack & DP_AUX_NATIVE_REPLY_MASK) == DP_AUX_NATIVE_REPLY_ACK) {
  637. memcpy(recv, reply + 1, ret - 1);
  638. return ret - 1;
  639. }
  640. else if ((ack & DP_AUX_NATIVE_REPLY_MASK) == DP_AUX_NATIVE_REPLY_DEFER)
  641. udelay(100);
  642. else
  643. return -EIO;
  644. }
  645. }
  646. static int
  647. cdv_intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  648. uint8_t write_byte, uint8_t *read_byte)
  649. {
  650. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  651. struct cdv_intel_dp *intel_dp = container_of(adapter,
  652. struct cdv_intel_dp,
  653. adapter);
  654. struct gma_encoder *encoder = intel_dp->encoder;
  655. uint16_t address = algo_data->address;
  656. uint8_t msg[5];
  657. uint8_t reply[2];
  658. unsigned retry;
  659. int msg_bytes;
  660. int reply_bytes;
  661. int ret;
  662. /* Set up the command byte */
  663. if (mode & MODE_I2C_READ)
  664. msg[0] = DP_AUX_I2C_READ << 4;
  665. else
  666. msg[0] = DP_AUX_I2C_WRITE << 4;
  667. if (!(mode & MODE_I2C_STOP))
  668. msg[0] |= DP_AUX_I2C_MOT << 4;
  669. msg[1] = address >> 8;
  670. msg[2] = address;
  671. switch (mode) {
  672. case MODE_I2C_WRITE:
  673. msg[3] = 0;
  674. msg[4] = write_byte;
  675. msg_bytes = 5;
  676. reply_bytes = 1;
  677. break;
  678. case MODE_I2C_READ:
  679. msg[3] = 0;
  680. msg_bytes = 4;
  681. reply_bytes = 2;
  682. break;
  683. default:
  684. msg_bytes = 3;
  685. reply_bytes = 1;
  686. break;
  687. }
  688. for (retry = 0; retry < 5; retry++) {
  689. ret = cdv_intel_dp_aux_ch(encoder,
  690. msg, msg_bytes,
  691. reply, reply_bytes);
  692. if (ret < 0) {
  693. DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
  694. return ret;
  695. }
  696. switch ((reply[0] >> 4) & DP_AUX_NATIVE_REPLY_MASK) {
  697. case DP_AUX_NATIVE_REPLY_ACK:
  698. /* I2C-over-AUX Reply field is only valid
  699. * when paired with AUX ACK.
  700. */
  701. break;
  702. case DP_AUX_NATIVE_REPLY_NACK:
  703. DRM_DEBUG_KMS("aux_ch native nack\n");
  704. return -EREMOTEIO;
  705. case DP_AUX_NATIVE_REPLY_DEFER:
  706. udelay(100);
  707. continue;
  708. default:
  709. DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
  710. reply[0]);
  711. return -EREMOTEIO;
  712. }
  713. switch ((reply[0] >> 4) & DP_AUX_I2C_REPLY_MASK) {
  714. case DP_AUX_I2C_REPLY_ACK:
  715. if (mode == MODE_I2C_READ) {
  716. *read_byte = reply[1];
  717. }
  718. return reply_bytes - 1;
  719. case DP_AUX_I2C_REPLY_NACK:
  720. DRM_DEBUG_KMS("aux_i2c nack\n");
  721. return -EREMOTEIO;
  722. case DP_AUX_I2C_REPLY_DEFER:
  723. DRM_DEBUG_KMS("aux_i2c defer\n");
  724. udelay(100);
  725. break;
  726. default:
  727. DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
  728. return -EREMOTEIO;
  729. }
  730. }
  731. DRM_ERROR("too many retries, giving up\n");
  732. return -EREMOTEIO;
  733. }
  734. static int
  735. cdv_intel_dp_i2c_init(struct gma_connector *connector,
  736. struct gma_encoder *encoder, const char *name)
  737. {
  738. struct cdv_intel_dp *intel_dp = encoder->dev_priv;
  739. int ret;
  740. DRM_DEBUG_KMS("i2c_init %s\n", name);
  741. intel_dp->algo.running = false;
  742. intel_dp->algo.address = 0;
  743. intel_dp->algo.aux_ch = cdv_intel_dp_i2c_aux_ch;
  744. memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
  745. intel_dp->adapter.owner = THIS_MODULE;
  746. strscpy(intel_dp->adapter.name, name);
  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_obj(struct gma_encoder);
  1667. if (!gma_encoder)
  1668. return;
  1669. gma_connector = kzalloc_obj(struct gma_connector);
  1670. if (!gma_connector)
  1671. goto err_connector;
  1672. intel_dp = kzalloc_obj(struct cdv_intel_dp);
  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. }