intel_dsi.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. /*
  2. * Copyright © 2013 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
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Author: Jani Nikula <jani.nikula@intel.com>
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/drm_atomic_helper.h>
  27. #include <drm/drm_crtc.h>
  28. #include <drm/drm_edid.h>
  29. #include <drm/i915_drm.h>
  30. #include <drm/drm_mipi_dsi.h>
  31. #include <linux/slab.h>
  32. #include <linux/gpio/consumer.h>
  33. #include "i915_drv.h"
  34. #include "intel_drv.h"
  35. #include "intel_dsi.h"
  36. /* return pixels in terms of txbyteclkhs */
  37. static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
  38. u16 burst_mode_ratio)
  39. {
  40. return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
  41. 8 * 100), lane_count);
  42. }
  43. /* return pixels equvalent to txbyteclkhs */
  44. static u16 pixels_from_txbyteclkhs(u16 clk_hs, int bpp, int lane_count,
  45. u16 burst_mode_ratio)
  46. {
  47. return DIV_ROUND_UP((clk_hs * lane_count * 8 * 100),
  48. (bpp * burst_mode_ratio));
  49. }
  50. enum mipi_dsi_pixel_format pixel_format_from_register_bits(u32 fmt)
  51. {
  52. /* It just so happens the VBT matches register contents. */
  53. switch (fmt) {
  54. case VID_MODE_FORMAT_RGB888:
  55. return MIPI_DSI_FMT_RGB888;
  56. case VID_MODE_FORMAT_RGB666:
  57. return MIPI_DSI_FMT_RGB666;
  58. case VID_MODE_FORMAT_RGB666_PACKED:
  59. return MIPI_DSI_FMT_RGB666_PACKED;
  60. case VID_MODE_FORMAT_RGB565:
  61. return MIPI_DSI_FMT_RGB565;
  62. default:
  63. MISSING_CASE(fmt);
  64. return MIPI_DSI_FMT_RGB666;
  65. }
  66. }
  67. void wait_for_dsi_fifo_empty(struct intel_dsi *intel_dsi, enum port port)
  68. {
  69. struct drm_encoder *encoder = &intel_dsi->base.base;
  70. struct drm_device *dev = encoder->dev;
  71. struct drm_i915_private *dev_priv = to_i915(dev);
  72. u32 mask;
  73. mask = LP_CTRL_FIFO_EMPTY | HS_CTRL_FIFO_EMPTY |
  74. LP_DATA_FIFO_EMPTY | HS_DATA_FIFO_EMPTY;
  75. if (intel_wait_for_register(dev_priv,
  76. MIPI_GEN_FIFO_STAT(port), mask, mask,
  77. 100))
  78. DRM_ERROR("DPI FIFOs are not empty\n");
  79. }
  80. static void write_data(struct drm_i915_private *dev_priv,
  81. i915_reg_t reg,
  82. const u8 *data, u32 len)
  83. {
  84. u32 i, j;
  85. for (i = 0; i < len; i += 4) {
  86. u32 val = 0;
  87. for (j = 0; j < min_t(u32, len - i, 4); j++)
  88. val |= *data++ << 8 * j;
  89. I915_WRITE(reg, val);
  90. }
  91. }
  92. static void read_data(struct drm_i915_private *dev_priv,
  93. i915_reg_t reg,
  94. u8 *data, u32 len)
  95. {
  96. u32 i, j;
  97. for (i = 0; i < len; i += 4) {
  98. u32 val = I915_READ(reg);
  99. for (j = 0; j < min_t(u32, len - i, 4); j++)
  100. *data++ = val >> 8 * j;
  101. }
  102. }
  103. static ssize_t intel_dsi_host_transfer(struct mipi_dsi_host *host,
  104. const struct mipi_dsi_msg *msg)
  105. {
  106. struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
  107. struct drm_device *dev = intel_dsi_host->intel_dsi->base.base.dev;
  108. struct drm_i915_private *dev_priv = to_i915(dev);
  109. enum port port = intel_dsi_host->port;
  110. struct mipi_dsi_packet packet;
  111. ssize_t ret;
  112. const u8 *header, *data;
  113. i915_reg_t data_reg, ctrl_reg;
  114. u32 data_mask, ctrl_mask;
  115. ret = mipi_dsi_create_packet(&packet, msg);
  116. if (ret < 0)
  117. return ret;
  118. header = packet.header;
  119. data = packet.payload;
  120. if (msg->flags & MIPI_DSI_MSG_USE_LPM) {
  121. data_reg = MIPI_LP_GEN_DATA(port);
  122. data_mask = LP_DATA_FIFO_FULL;
  123. ctrl_reg = MIPI_LP_GEN_CTRL(port);
  124. ctrl_mask = LP_CTRL_FIFO_FULL;
  125. } else {
  126. data_reg = MIPI_HS_GEN_DATA(port);
  127. data_mask = HS_DATA_FIFO_FULL;
  128. ctrl_reg = MIPI_HS_GEN_CTRL(port);
  129. ctrl_mask = HS_CTRL_FIFO_FULL;
  130. }
  131. /* note: this is never true for reads */
  132. if (packet.payload_length) {
  133. if (intel_wait_for_register(dev_priv,
  134. MIPI_GEN_FIFO_STAT(port),
  135. data_mask, 0,
  136. 50))
  137. DRM_ERROR("Timeout waiting for HS/LP DATA FIFO !full\n");
  138. write_data(dev_priv, data_reg, packet.payload,
  139. packet.payload_length);
  140. }
  141. if (msg->rx_len) {
  142. I915_WRITE(MIPI_INTR_STAT(port), GEN_READ_DATA_AVAIL);
  143. }
  144. if (intel_wait_for_register(dev_priv,
  145. MIPI_GEN_FIFO_STAT(port),
  146. ctrl_mask, 0,
  147. 50)) {
  148. DRM_ERROR("Timeout waiting for HS/LP CTRL FIFO !full\n");
  149. }
  150. I915_WRITE(ctrl_reg, header[2] << 16 | header[1] << 8 | header[0]);
  151. /* ->rx_len is set only for reads */
  152. if (msg->rx_len) {
  153. data_mask = GEN_READ_DATA_AVAIL;
  154. if (intel_wait_for_register(dev_priv,
  155. MIPI_INTR_STAT(port),
  156. data_mask, data_mask,
  157. 50))
  158. DRM_ERROR("Timeout waiting for read data.\n");
  159. read_data(dev_priv, data_reg, msg->rx_buf, msg->rx_len);
  160. }
  161. /* XXX: fix for reads and writes */
  162. return 4 + packet.payload_length;
  163. }
  164. static int intel_dsi_host_attach(struct mipi_dsi_host *host,
  165. struct mipi_dsi_device *dsi)
  166. {
  167. return 0;
  168. }
  169. static int intel_dsi_host_detach(struct mipi_dsi_host *host,
  170. struct mipi_dsi_device *dsi)
  171. {
  172. return 0;
  173. }
  174. static const struct mipi_dsi_host_ops intel_dsi_host_ops = {
  175. .attach = intel_dsi_host_attach,
  176. .detach = intel_dsi_host_detach,
  177. .transfer = intel_dsi_host_transfer,
  178. };
  179. static struct intel_dsi_host *intel_dsi_host_init(struct intel_dsi *intel_dsi,
  180. enum port port)
  181. {
  182. struct intel_dsi_host *host;
  183. struct mipi_dsi_device *device;
  184. host = kzalloc(sizeof(*host), GFP_KERNEL);
  185. if (!host)
  186. return NULL;
  187. host->base.ops = &intel_dsi_host_ops;
  188. host->intel_dsi = intel_dsi;
  189. host->port = port;
  190. /*
  191. * We should call mipi_dsi_host_register(&host->base) here, but we don't
  192. * have a host->dev, and we don't have OF stuff either. So just use the
  193. * dsi framework as a library and hope for the best. Create the dsi
  194. * devices by ourselves here too. Need to be careful though, because we
  195. * don't initialize any of the driver model devices here.
  196. */
  197. device = kzalloc(sizeof(*device), GFP_KERNEL);
  198. if (!device) {
  199. kfree(host);
  200. return NULL;
  201. }
  202. device->host = &host->base;
  203. host->device = device;
  204. return host;
  205. }
  206. /*
  207. * send a video mode command
  208. *
  209. * XXX: commands with data in MIPI_DPI_DATA?
  210. */
  211. static int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd, bool hs,
  212. enum port port)
  213. {
  214. struct drm_encoder *encoder = &intel_dsi->base.base;
  215. struct drm_device *dev = encoder->dev;
  216. struct drm_i915_private *dev_priv = to_i915(dev);
  217. u32 mask;
  218. /* XXX: pipe, hs */
  219. if (hs)
  220. cmd &= ~DPI_LP_MODE;
  221. else
  222. cmd |= DPI_LP_MODE;
  223. /* clear bit */
  224. I915_WRITE(MIPI_INTR_STAT(port), SPL_PKT_SENT_INTERRUPT);
  225. /* XXX: old code skips write if control unchanged */
  226. if (cmd == I915_READ(MIPI_DPI_CONTROL(port)))
  227. DRM_DEBUG_KMS("Same special packet %02x twice in a row.\n", cmd);
  228. I915_WRITE(MIPI_DPI_CONTROL(port), cmd);
  229. mask = SPL_PKT_SENT_INTERRUPT;
  230. if (intel_wait_for_register(dev_priv,
  231. MIPI_INTR_STAT(port), mask, mask,
  232. 100))
  233. DRM_ERROR("Video mode command 0x%08x send failed.\n", cmd);
  234. return 0;
  235. }
  236. static void band_gap_reset(struct drm_i915_private *dev_priv)
  237. {
  238. mutex_lock(&dev_priv->sb_lock);
  239. vlv_flisdsi_write(dev_priv, 0x08, 0x0001);
  240. vlv_flisdsi_write(dev_priv, 0x0F, 0x0005);
  241. vlv_flisdsi_write(dev_priv, 0x0F, 0x0025);
  242. udelay(150);
  243. vlv_flisdsi_write(dev_priv, 0x0F, 0x0000);
  244. vlv_flisdsi_write(dev_priv, 0x08, 0x0000);
  245. mutex_unlock(&dev_priv->sb_lock);
  246. }
  247. static inline bool is_vid_mode(struct intel_dsi *intel_dsi)
  248. {
  249. return intel_dsi->operation_mode == INTEL_DSI_VIDEO_MODE;
  250. }
  251. static inline bool is_cmd_mode(struct intel_dsi *intel_dsi)
  252. {
  253. return intel_dsi->operation_mode == INTEL_DSI_COMMAND_MODE;
  254. }
  255. static bool intel_dsi_compute_config(struct intel_encoder *encoder,
  256. struct intel_crtc_state *pipe_config,
  257. struct drm_connector_state *conn_state)
  258. {
  259. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  260. struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
  261. base);
  262. struct intel_connector *intel_connector = intel_dsi->attached_connector;
  263. struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
  264. const struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
  265. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  266. int ret;
  267. DRM_DEBUG_KMS("\n");
  268. if (fixed_mode) {
  269. intel_fixed_panel_mode(fixed_mode, adjusted_mode);
  270. if (HAS_GMCH_DISPLAY(dev_priv))
  271. intel_gmch_panel_fitting(crtc, pipe_config,
  272. conn_state->scaling_mode);
  273. else
  274. intel_pch_panel_fitting(crtc, pipe_config,
  275. conn_state->scaling_mode);
  276. }
  277. if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
  278. return false;
  279. /* DSI uses short packets for sync events, so clear mode flags for DSI */
  280. adjusted_mode->flags = 0;
  281. if (IS_GEN9_LP(dev_priv)) {
  282. /* Enable Frame time stamp based scanline reporting */
  283. adjusted_mode->private_flags |=
  284. I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
  285. /* Dual link goes to DSI transcoder A. */
  286. if (intel_dsi->ports == BIT(PORT_C))
  287. pipe_config->cpu_transcoder = TRANSCODER_DSI_C;
  288. else
  289. pipe_config->cpu_transcoder = TRANSCODER_DSI_A;
  290. }
  291. ret = intel_compute_dsi_pll(encoder, pipe_config);
  292. if (ret)
  293. return false;
  294. pipe_config->clock_set = true;
  295. return true;
  296. }
  297. static bool glk_dsi_enable_io(struct intel_encoder *encoder)
  298. {
  299. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  300. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  301. enum port port;
  302. u32 tmp;
  303. bool cold_boot = false;
  304. /* Set the MIPI mode
  305. * If MIPI_Mode is off, then writing to LP_Wake bit is not reflecting.
  306. * Power ON MIPI IO first and then write into IO reset and LP wake bits
  307. */
  308. for_each_dsi_port(port, intel_dsi->ports) {
  309. tmp = I915_READ(MIPI_CTRL(port));
  310. I915_WRITE(MIPI_CTRL(port), tmp | GLK_MIPIIO_ENABLE);
  311. }
  312. /* Put the IO into reset */
  313. tmp = I915_READ(MIPI_CTRL(PORT_A));
  314. tmp &= ~GLK_MIPIIO_RESET_RELEASED;
  315. I915_WRITE(MIPI_CTRL(PORT_A), tmp);
  316. /* Program LP Wake */
  317. for_each_dsi_port(port, intel_dsi->ports) {
  318. tmp = I915_READ(MIPI_CTRL(port));
  319. if (!(I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY))
  320. tmp &= ~GLK_LP_WAKE;
  321. else
  322. tmp |= GLK_LP_WAKE;
  323. I915_WRITE(MIPI_CTRL(port), tmp);
  324. }
  325. /* Wait for Pwr ACK */
  326. for_each_dsi_port(port, intel_dsi->ports) {
  327. if (intel_wait_for_register(dev_priv,
  328. MIPI_CTRL(port), GLK_MIPIIO_PORT_POWERED,
  329. GLK_MIPIIO_PORT_POWERED, 20))
  330. DRM_ERROR("MIPIO port is powergated\n");
  331. }
  332. /* Check for cold boot scenario */
  333. for_each_dsi_port(port, intel_dsi->ports) {
  334. cold_boot |= !(I915_READ(MIPI_DEVICE_READY(port)) &
  335. DEVICE_READY);
  336. }
  337. return cold_boot;
  338. }
  339. static void glk_dsi_device_ready(struct intel_encoder *encoder)
  340. {
  341. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  342. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  343. enum port port;
  344. u32 val;
  345. /* Wait for MIPI PHY status bit to set */
  346. for_each_dsi_port(port, intel_dsi->ports) {
  347. if (intel_wait_for_register(dev_priv,
  348. MIPI_CTRL(port), GLK_PHY_STATUS_PORT_READY,
  349. GLK_PHY_STATUS_PORT_READY, 20))
  350. DRM_ERROR("PHY is not ON\n");
  351. }
  352. /* Get IO out of reset */
  353. val = I915_READ(MIPI_CTRL(PORT_A));
  354. I915_WRITE(MIPI_CTRL(PORT_A), val | GLK_MIPIIO_RESET_RELEASED);
  355. /* Get IO out of Low power state*/
  356. for_each_dsi_port(port, intel_dsi->ports) {
  357. if (!(I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY)) {
  358. val = I915_READ(MIPI_DEVICE_READY(port));
  359. val &= ~ULPS_STATE_MASK;
  360. val |= DEVICE_READY;
  361. I915_WRITE(MIPI_DEVICE_READY(port), val);
  362. usleep_range(10, 15);
  363. } else {
  364. /* Enter ULPS */
  365. val = I915_READ(MIPI_DEVICE_READY(port));
  366. val &= ~ULPS_STATE_MASK;
  367. val |= (ULPS_STATE_ENTER | DEVICE_READY);
  368. I915_WRITE(MIPI_DEVICE_READY(port), val);
  369. /* Wait for ULPS active */
  370. if (intel_wait_for_register(dev_priv,
  371. MIPI_CTRL(port), GLK_ULPS_NOT_ACTIVE, 0, 20))
  372. DRM_ERROR("ULPS not active\n");
  373. /* Exit ULPS */
  374. val = I915_READ(MIPI_DEVICE_READY(port));
  375. val &= ~ULPS_STATE_MASK;
  376. val |= (ULPS_STATE_EXIT | DEVICE_READY);
  377. I915_WRITE(MIPI_DEVICE_READY(port), val);
  378. /* Enter Normal Mode */
  379. val = I915_READ(MIPI_DEVICE_READY(port));
  380. val &= ~ULPS_STATE_MASK;
  381. val |= (ULPS_STATE_NORMAL_OPERATION | DEVICE_READY);
  382. I915_WRITE(MIPI_DEVICE_READY(port), val);
  383. val = I915_READ(MIPI_CTRL(port));
  384. val &= ~GLK_LP_WAKE;
  385. I915_WRITE(MIPI_CTRL(port), val);
  386. }
  387. }
  388. /* Wait for Stop state */
  389. for_each_dsi_port(port, intel_dsi->ports) {
  390. if (intel_wait_for_register(dev_priv,
  391. MIPI_CTRL(port), GLK_DATA_LANE_STOP_STATE,
  392. GLK_DATA_LANE_STOP_STATE, 20))
  393. DRM_ERROR("Date lane not in STOP state\n");
  394. }
  395. /* Wait for AFE LATCH */
  396. for_each_dsi_port(port, intel_dsi->ports) {
  397. if (intel_wait_for_register(dev_priv,
  398. BXT_MIPI_PORT_CTRL(port), AFE_LATCHOUT,
  399. AFE_LATCHOUT, 20))
  400. DRM_ERROR("D-PHY not entering LP-11 state\n");
  401. }
  402. }
  403. static void bxt_dsi_device_ready(struct intel_encoder *encoder)
  404. {
  405. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  406. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  407. enum port port;
  408. u32 val;
  409. DRM_DEBUG_KMS("\n");
  410. /* Enable MIPI PHY transparent latch */
  411. for_each_dsi_port(port, intel_dsi->ports) {
  412. val = I915_READ(BXT_MIPI_PORT_CTRL(port));
  413. I915_WRITE(BXT_MIPI_PORT_CTRL(port), val | LP_OUTPUT_HOLD);
  414. usleep_range(2000, 2500);
  415. }
  416. /* Clear ULPS and set device ready */
  417. for_each_dsi_port(port, intel_dsi->ports) {
  418. val = I915_READ(MIPI_DEVICE_READY(port));
  419. val &= ~ULPS_STATE_MASK;
  420. I915_WRITE(MIPI_DEVICE_READY(port), val);
  421. usleep_range(2000, 2500);
  422. val |= DEVICE_READY;
  423. I915_WRITE(MIPI_DEVICE_READY(port), val);
  424. }
  425. }
  426. static void vlv_dsi_device_ready(struct intel_encoder *encoder)
  427. {
  428. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  429. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  430. enum port port;
  431. u32 val;
  432. DRM_DEBUG_KMS("\n");
  433. mutex_lock(&dev_priv->sb_lock);
  434. /* program rcomp for compliance, reduce from 50 ohms to 45 ohms
  435. * needed everytime after power gate */
  436. vlv_flisdsi_write(dev_priv, 0x04, 0x0004);
  437. mutex_unlock(&dev_priv->sb_lock);
  438. /* bandgap reset is needed after everytime we do power gate */
  439. band_gap_reset(dev_priv);
  440. for_each_dsi_port(port, intel_dsi->ports) {
  441. I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_ENTER);
  442. usleep_range(2500, 3000);
  443. /* Enable MIPI PHY transparent latch
  444. * Common bit for both MIPI Port A & MIPI Port C
  445. * No similar bit in MIPI Port C reg
  446. */
  447. val = I915_READ(MIPI_PORT_CTRL(PORT_A));
  448. I915_WRITE(MIPI_PORT_CTRL(PORT_A), val | LP_OUTPUT_HOLD);
  449. usleep_range(1000, 1500);
  450. I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_EXIT);
  451. usleep_range(2500, 3000);
  452. I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY);
  453. usleep_range(2500, 3000);
  454. }
  455. }
  456. static void intel_dsi_device_ready(struct intel_encoder *encoder)
  457. {
  458. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  459. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  460. vlv_dsi_device_ready(encoder);
  461. else if (IS_BROXTON(dev_priv))
  462. bxt_dsi_device_ready(encoder);
  463. else if (IS_GEMINILAKE(dev_priv))
  464. glk_dsi_device_ready(encoder);
  465. }
  466. static void glk_dsi_enter_low_power_mode(struct intel_encoder *encoder)
  467. {
  468. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  469. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  470. enum port port;
  471. u32 val;
  472. /* Enter ULPS */
  473. for_each_dsi_port(port, intel_dsi->ports) {
  474. val = I915_READ(MIPI_DEVICE_READY(port));
  475. val &= ~ULPS_STATE_MASK;
  476. val |= (ULPS_STATE_ENTER | DEVICE_READY);
  477. I915_WRITE(MIPI_DEVICE_READY(port), val);
  478. }
  479. /* Wait for MIPI PHY status bit to unset */
  480. for_each_dsi_port(port, intel_dsi->ports) {
  481. if (intel_wait_for_register(dev_priv,
  482. MIPI_CTRL(port),
  483. GLK_PHY_STATUS_PORT_READY, 0, 20))
  484. DRM_ERROR("PHY is not turning OFF\n");
  485. }
  486. /* Wait for Pwr ACK bit to unset */
  487. for_each_dsi_port(port, intel_dsi->ports) {
  488. if (intel_wait_for_register(dev_priv,
  489. MIPI_CTRL(port),
  490. GLK_MIPIIO_PORT_POWERED, 0, 20))
  491. DRM_ERROR("MIPI IO Port is not powergated\n");
  492. }
  493. }
  494. static void glk_dsi_disable_mipi_io(struct intel_encoder *encoder)
  495. {
  496. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  497. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  498. enum port port;
  499. u32 tmp;
  500. /* Put the IO into reset */
  501. tmp = I915_READ(MIPI_CTRL(PORT_A));
  502. tmp &= ~GLK_MIPIIO_RESET_RELEASED;
  503. I915_WRITE(MIPI_CTRL(PORT_A), tmp);
  504. /* Wait for MIPI PHY status bit to unset */
  505. for_each_dsi_port(port, intel_dsi->ports) {
  506. if (intel_wait_for_register(dev_priv,
  507. MIPI_CTRL(port),
  508. GLK_PHY_STATUS_PORT_READY, 0, 20))
  509. DRM_ERROR("PHY is not turning OFF\n");
  510. }
  511. /* Clear MIPI mode */
  512. for_each_dsi_port(port, intel_dsi->ports) {
  513. tmp = I915_READ(MIPI_CTRL(port));
  514. tmp &= ~GLK_MIPIIO_ENABLE;
  515. I915_WRITE(MIPI_CTRL(port), tmp);
  516. }
  517. }
  518. static void glk_dsi_clear_device_ready(struct intel_encoder *encoder)
  519. {
  520. glk_dsi_enter_low_power_mode(encoder);
  521. glk_dsi_disable_mipi_io(encoder);
  522. }
  523. static void vlv_dsi_clear_device_ready(struct intel_encoder *encoder)
  524. {
  525. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  526. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  527. enum port port;
  528. DRM_DEBUG_KMS("\n");
  529. for_each_dsi_port(port, intel_dsi->ports) {
  530. /* Common bit for both MIPI Port A & MIPI Port C on VLV/CHV */
  531. i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
  532. BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(PORT_A);
  533. u32 val;
  534. I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
  535. ULPS_STATE_ENTER);
  536. usleep_range(2000, 2500);
  537. I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
  538. ULPS_STATE_EXIT);
  539. usleep_range(2000, 2500);
  540. I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
  541. ULPS_STATE_ENTER);
  542. usleep_range(2000, 2500);
  543. /*
  544. * On VLV/CHV, wait till Clock lanes are in LP-00 state for MIPI
  545. * Port A only. MIPI Port C has no similar bit for checking.
  546. */
  547. if ((IS_GEN9_LP(dev_priv) || port == PORT_A) &&
  548. intel_wait_for_register(dev_priv,
  549. port_ctrl, AFE_LATCHOUT, 0,
  550. 30))
  551. DRM_ERROR("DSI LP not going Low\n");
  552. /* Disable MIPI PHY transparent latch */
  553. val = I915_READ(port_ctrl);
  554. I915_WRITE(port_ctrl, val & ~LP_OUTPUT_HOLD);
  555. usleep_range(1000, 1500);
  556. I915_WRITE(MIPI_DEVICE_READY(port), 0x00);
  557. usleep_range(2000, 2500);
  558. }
  559. }
  560. static void intel_dsi_port_enable(struct intel_encoder *encoder,
  561. const struct intel_crtc_state *crtc_state)
  562. {
  563. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  564. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  565. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  566. enum port port;
  567. if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
  568. u32 temp;
  569. if (IS_GEN9_LP(dev_priv)) {
  570. for_each_dsi_port(port, intel_dsi->ports) {
  571. temp = I915_READ(MIPI_CTRL(port));
  572. temp &= ~BXT_PIXEL_OVERLAP_CNT_MASK |
  573. intel_dsi->pixel_overlap <<
  574. BXT_PIXEL_OVERLAP_CNT_SHIFT;
  575. I915_WRITE(MIPI_CTRL(port), temp);
  576. }
  577. } else {
  578. temp = I915_READ(VLV_CHICKEN_3);
  579. temp &= ~PIXEL_OVERLAP_CNT_MASK |
  580. intel_dsi->pixel_overlap <<
  581. PIXEL_OVERLAP_CNT_SHIFT;
  582. I915_WRITE(VLV_CHICKEN_3, temp);
  583. }
  584. }
  585. for_each_dsi_port(port, intel_dsi->ports) {
  586. i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
  587. BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
  588. u32 temp;
  589. temp = I915_READ(port_ctrl);
  590. temp &= ~LANE_CONFIGURATION_MASK;
  591. temp &= ~DUAL_LINK_MODE_MASK;
  592. if (intel_dsi->ports == (BIT(PORT_A) | BIT(PORT_C))) {
  593. temp |= (intel_dsi->dual_link - 1)
  594. << DUAL_LINK_MODE_SHIFT;
  595. if (IS_BROXTON(dev_priv))
  596. temp |= LANE_CONFIGURATION_DUAL_LINK_A;
  597. else
  598. temp |= crtc->pipe ?
  599. LANE_CONFIGURATION_DUAL_LINK_B :
  600. LANE_CONFIGURATION_DUAL_LINK_A;
  601. }
  602. /* assert ip_tg_enable signal */
  603. I915_WRITE(port_ctrl, temp | DPI_ENABLE);
  604. POSTING_READ(port_ctrl);
  605. }
  606. }
  607. static void intel_dsi_port_disable(struct intel_encoder *encoder)
  608. {
  609. struct drm_device *dev = encoder->base.dev;
  610. struct drm_i915_private *dev_priv = to_i915(dev);
  611. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  612. enum port port;
  613. for_each_dsi_port(port, intel_dsi->ports) {
  614. i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
  615. BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
  616. u32 temp;
  617. /* de-assert ip_tg_enable signal */
  618. temp = I915_READ(port_ctrl);
  619. I915_WRITE(port_ctrl, temp & ~DPI_ENABLE);
  620. POSTING_READ(port_ctrl);
  621. }
  622. }
  623. static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
  624. const struct intel_crtc_state *pipe_config);
  625. static void intel_dsi_unprepare(struct intel_encoder *encoder);
  626. static void intel_dsi_msleep(struct intel_dsi *intel_dsi, int msec)
  627. {
  628. struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
  629. /* For v3 VBTs in vid-mode the delays are part of the VBT sequences */
  630. if (is_vid_mode(intel_dsi) && dev_priv->vbt.dsi.seq_version >= 3)
  631. return;
  632. msleep(msec);
  633. }
  634. /*
  635. * Panel enable/disable sequences from the VBT spec.
  636. *
  637. * Note the spec has AssertReset / DeassertReset swapped from their
  638. * usual naming. We use the normal names to avoid confusion (so below
  639. * they are swapped compared to the spec).
  640. *
  641. * Steps starting with MIPI refer to VBT sequences, note that for v2
  642. * VBTs several steps which have a VBT in v2 are expected to be handled
  643. * directly by the driver, by directly driving gpios for example.
  644. *
  645. * v2 video mode seq v3 video mode seq command mode seq
  646. * - power on - MIPIPanelPowerOn - power on
  647. * - wait t1+t2 - wait t1+t2
  648. * - MIPIDeassertResetPin - MIPIDeassertResetPin - MIPIDeassertResetPin
  649. * - io lines to lp-11 - io lines to lp-11 - io lines to lp-11
  650. * - MIPISendInitialDcsCmds - MIPISendInitialDcsCmds - MIPISendInitialDcsCmds
  651. * - MIPITearOn
  652. * - MIPIDisplayOn
  653. * - turn on DPI - turn on DPI - set pipe to dsr mode
  654. * - MIPIDisplayOn - MIPIDisplayOn
  655. * - wait t5 - wait t5
  656. * - backlight on - MIPIBacklightOn - backlight on
  657. * ... ... ... issue mem cmds ...
  658. * - backlight off - MIPIBacklightOff - backlight off
  659. * - wait t6 - wait t6
  660. * - MIPIDisplayOff
  661. * - turn off DPI - turn off DPI - disable pipe dsr mode
  662. * - MIPITearOff
  663. * - MIPIDisplayOff - MIPIDisplayOff
  664. * - io lines to lp-00 - io lines to lp-00 - io lines to lp-00
  665. * - MIPIAssertResetPin - MIPIAssertResetPin - MIPIAssertResetPin
  666. * - wait t3 - wait t3
  667. * - power off - MIPIPanelPowerOff - power off
  668. * - wait t4 - wait t4
  669. */
  670. static void intel_dsi_pre_enable(struct intel_encoder *encoder,
  671. const struct intel_crtc_state *pipe_config,
  672. const struct drm_connector_state *conn_state)
  673. {
  674. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  675. struct drm_crtc *crtc = pipe_config->base.crtc;
  676. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  677. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  678. int pipe = intel_crtc->pipe;
  679. enum port port;
  680. u32 val;
  681. bool glk_cold_boot = false;
  682. DRM_DEBUG_KMS("\n");
  683. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  684. /*
  685. * The BIOS may leave the PLL in a wonky state where it doesn't
  686. * lock. It needs to be fully powered down to fix it.
  687. */
  688. intel_disable_dsi_pll(encoder);
  689. intel_enable_dsi_pll(encoder, pipe_config);
  690. if (IS_BROXTON(dev_priv)) {
  691. /* Add MIPI IO reset programming for modeset */
  692. val = I915_READ(BXT_P_CR_GT_DISP_PWRON);
  693. I915_WRITE(BXT_P_CR_GT_DISP_PWRON,
  694. val | MIPIO_RST_CTRL);
  695. /* Power up DSI regulator */
  696. I915_WRITE(BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
  697. I915_WRITE(BXT_P_DSI_REGULATOR_TX_CTRL, 0);
  698. }
  699. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  700. u32 val;
  701. /* Disable DPOunit clock gating, can stall pipe */
  702. val = I915_READ(DSPCLK_GATE_D);
  703. val |= DPOUNIT_CLOCK_GATE_DISABLE;
  704. I915_WRITE(DSPCLK_GATE_D, val);
  705. }
  706. if (!IS_GEMINILAKE(dev_priv))
  707. intel_dsi_prepare(encoder, pipe_config);
  708. /* Power on, try both CRC pmic gpio and VBT */
  709. if (intel_dsi->gpio_panel)
  710. gpiod_set_value_cansleep(intel_dsi->gpio_panel, 1);
  711. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_ON);
  712. intel_dsi_msleep(intel_dsi, intel_dsi->panel_on_delay);
  713. /* Deassert reset */
  714. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DEASSERT_RESET);
  715. if (IS_GEMINILAKE(dev_priv)) {
  716. glk_cold_boot = glk_dsi_enable_io(encoder);
  717. /* Prepare port in cold boot(s3/s4) scenario */
  718. if (glk_cold_boot)
  719. intel_dsi_prepare(encoder, pipe_config);
  720. }
  721. /* Put device in ready state (LP-11) */
  722. intel_dsi_device_ready(encoder);
  723. /* Prepare port in normal boot scenario */
  724. if (IS_GEMINILAKE(dev_priv) && !glk_cold_boot)
  725. intel_dsi_prepare(encoder, pipe_config);
  726. /* Send initialization commands in LP mode */
  727. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_INIT_OTP);
  728. /* Enable port in pre-enable phase itself because as per hw team
  729. * recommendation, port should be enabled befor plane & pipe */
  730. if (is_cmd_mode(intel_dsi)) {
  731. for_each_dsi_port(port, intel_dsi->ports)
  732. I915_WRITE(MIPI_MAX_RETURN_PKT_SIZE(port), 8 * 4);
  733. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_ON);
  734. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
  735. } else {
  736. msleep(20); /* XXX */
  737. for_each_dsi_port(port, intel_dsi->ports)
  738. dpi_send_cmd(intel_dsi, TURN_ON, false, port);
  739. intel_dsi_msleep(intel_dsi, 100);
  740. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
  741. intel_dsi_port_enable(encoder, pipe_config);
  742. }
  743. intel_panel_enable_backlight(pipe_config, conn_state);
  744. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_ON);
  745. }
  746. /*
  747. * DSI port enable has to be done before pipe and plane enable, so we do it in
  748. * the pre_enable hook.
  749. */
  750. static void intel_dsi_enable_nop(struct intel_encoder *encoder,
  751. const struct intel_crtc_state *pipe_config,
  752. const struct drm_connector_state *conn_state)
  753. {
  754. DRM_DEBUG_KMS("\n");
  755. }
  756. /*
  757. * DSI port disable has to be done after pipe and plane disable, so we do it in
  758. * the post_disable hook.
  759. */
  760. static void intel_dsi_disable(struct intel_encoder *encoder,
  761. const struct intel_crtc_state *old_crtc_state,
  762. const struct drm_connector_state *old_conn_state)
  763. {
  764. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  765. enum port port;
  766. DRM_DEBUG_KMS("\n");
  767. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_OFF);
  768. intel_panel_disable_backlight(old_conn_state);
  769. /*
  770. * According to the spec we should send SHUTDOWN before
  771. * MIPI_SEQ_DISPLAY_OFF only for v3+ VBTs, but field testing
  772. * has shown that the v3 sequence works for v2 VBTs too
  773. */
  774. if (is_vid_mode(intel_dsi)) {
  775. /* Send Shutdown command to the panel in LP mode */
  776. for_each_dsi_port(port, intel_dsi->ports)
  777. dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
  778. msleep(10);
  779. }
  780. }
  781. static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
  782. {
  783. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  784. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv) ||
  785. IS_BROXTON(dev_priv))
  786. vlv_dsi_clear_device_ready(encoder);
  787. else if (IS_GEMINILAKE(dev_priv))
  788. glk_dsi_clear_device_ready(encoder);
  789. }
  790. static void intel_dsi_post_disable(struct intel_encoder *encoder,
  791. const struct intel_crtc_state *pipe_config,
  792. const struct drm_connector_state *conn_state)
  793. {
  794. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  795. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  796. enum port port;
  797. u32 val;
  798. DRM_DEBUG_KMS("\n");
  799. if (is_vid_mode(intel_dsi)) {
  800. for_each_dsi_port(port, intel_dsi->ports)
  801. wait_for_dsi_fifo_empty(intel_dsi, port);
  802. intel_dsi_port_disable(encoder);
  803. usleep_range(2000, 5000);
  804. }
  805. intel_dsi_unprepare(encoder);
  806. /*
  807. * if disable packets are sent before sending shutdown packet then in
  808. * some next enable sequence send turn on packet error is observed
  809. */
  810. if (is_cmd_mode(intel_dsi))
  811. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_OFF);
  812. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_OFF);
  813. /* Transition to LP-00 */
  814. intel_dsi_clear_device_ready(encoder);
  815. if (IS_BROXTON(dev_priv)) {
  816. /* Power down DSI regulator to save power */
  817. I915_WRITE(BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
  818. I915_WRITE(BXT_P_DSI_REGULATOR_TX_CTRL, HS_IO_CTRL_SELECT);
  819. /* Add MIPI IO reset programming for modeset */
  820. val = I915_READ(BXT_P_CR_GT_DISP_PWRON);
  821. I915_WRITE(BXT_P_CR_GT_DISP_PWRON,
  822. val & ~MIPIO_RST_CTRL);
  823. }
  824. intel_disable_dsi_pll(encoder);
  825. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  826. u32 val;
  827. val = I915_READ(DSPCLK_GATE_D);
  828. val &= ~DPOUNIT_CLOCK_GATE_DISABLE;
  829. I915_WRITE(DSPCLK_GATE_D, val);
  830. }
  831. /* Assert reset */
  832. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_ASSERT_RESET);
  833. /* Power off, try both CRC pmic gpio and VBT */
  834. intel_dsi_msleep(intel_dsi, intel_dsi->panel_off_delay);
  835. intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_OFF);
  836. if (intel_dsi->gpio_panel)
  837. gpiod_set_value_cansleep(intel_dsi->gpio_panel, 0);
  838. /*
  839. * FIXME As we do with eDP, just make a note of the time here
  840. * and perform the wait before the next panel power on.
  841. */
  842. intel_dsi_msleep(intel_dsi, intel_dsi->panel_pwr_cycle_delay);
  843. }
  844. static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
  845. enum pipe *pipe)
  846. {
  847. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  848. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  849. enum port port;
  850. bool active = false;
  851. DRM_DEBUG_KMS("\n");
  852. if (!intel_display_power_get_if_enabled(dev_priv,
  853. encoder->power_domain))
  854. return false;
  855. /*
  856. * On Broxton the PLL needs to be enabled with a valid divider
  857. * configuration, otherwise accessing DSI registers will hang the
  858. * machine. See BSpec North Display Engine registers/MIPI[BXT].
  859. */
  860. if (IS_GEN9_LP(dev_priv) && !intel_dsi_pll_is_enabled(dev_priv))
  861. goto out_put_power;
  862. /* XXX: this only works for one DSI output */
  863. for_each_dsi_port(port, intel_dsi->ports) {
  864. i915_reg_t ctrl_reg = IS_GEN9_LP(dev_priv) ?
  865. BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
  866. bool enabled = I915_READ(ctrl_reg) & DPI_ENABLE;
  867. /*
  868. * Due to some hardware limitations on VLV/CHV, the DPI enable
  869. * bit in port C control register does not get set. As a
  870. * workaround, check pipe B conf instead.
  871. */
  872. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  873. port == PORT_C)
  874. enabled = I915_READ(PIPECONF(PIPE_B)) & PIPECONF_ENABLE;
  875. /* Try command mode if video mode not enabled */
  876. if (!enabled) {
  877. u32 tmp = I915_READ(MIPI_DSI_FUNC_PRG(port));
  878. enabled = tmp & CMD_MODE_DATA_WIDTH_MASK;
  879. }
  880. if (!enabled)
  881. continue;
  882. if (!(I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY))
  883. continue;
  884. if (IS_GEN9_LP(dev_priv)) {
  885. u32 tmp = I915_READ(MIPI_CTRL(port));
  886. tmp &= BXT_PIPE_SELECT_MASK;
  887. tmp >>= BXT_PIPE_SELECT_SHIFT;
  888. if (WARN_ON(tmp > PIPE_C))
  889. continue;
  890. *pipe = tmp;
  891. } else {
  892. *pipe = port == PORT_A ? PIPE_A : PIPE_B;
  893. }
  894. active = true;
  895. break;
  896. }
  897. out_put_power:
  898. intel_display_power_put(dev_priv, encoder->power_domain);
  899. return active;
  900. }
  901. static void bxt_dsi_get_pipe_config(struct intel_encoder *encoder,
  902. struct intel_crtc_state *pipe_config)
  903. {
  904. struct drm_device *dev = encoder->base.dev;
  905. struct drm_i915_private *dev_priv = to_i915(dev);
  906. struct drm_display_mode *adjusted_mode =
  907. &pipe_config->base.adjusted_mode;
  908. struct drm_display_mode *adjusted_mode_sw;
  909. struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
  910. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  911. unsigned int lane_count = intel_dsi->lane_count;
  912. unsigned int bpp, fmt;
  913. enum port port;
  914. u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
  915. u16 hfp_sw, hsync_sw, hbp_sw;
  916. u16 crtc_htotal_sw, crtc_hsync_start_sw, crtc_hsync_end_sw,
  917. crtc_hblank_start_sw, crtc_hblank_end_sw;
  918. /* FIXME: hw readout should not depend on SW state */
  919. adjusted_mode_sw = &crtc->config->base.adjusted_mode;
  920. /*
  921. * Atleast one port is active as encoder->get_config called only if
  922. * encoder->get_hw_state() returns true.
  923. */
  924. for_each_dsi_port(port, intel_dsi->ports) {
  925. if (I915_READ(BXT_MIPI_PORT_CTRL(port)) & DPI_ENABLE)
  926. break;
  927. }
  928. fmt = I915_READ(MIPI_DSI_FUNC_PRG(port)) & VID_MODE_FORMAT_MASK;
  929. pipe_config->pipe_bpp =
  930. mipi_dsi_pixel_format_to_bpp(
  931. pixel_format_from_register_bits(fmt));
  932. bpp = pipe_config->pipe_bpp;
  933. /* Enable Frame time stamo based scanline reporting */
  934. adjusted_mode->private_flags |=
  935. I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
  936. /* In terms of pixels */
  937. adjusted_mode->crtc_hdisplay =
  938. I915_READ(BXT_MIPI_TRANS_HACTIVE(port));
  939. adjusted_mode->crtc_vdisplay =
  940. I915_READ(BXT_MIPI_TRANS_VACTIVE(port));
  941. adjusted_mode->crtc_vtotal =
  942. I915_READ(BXT_MIPI_TRANS_VTOTAL(port));
  943. hactive = adjusted_mode->crtc_hdisplay;
  944. hfp = I915_READ(MIPI_HFP_COUNT(port));
  945. /*
  946. * Meaningful for video mode non-burst sync pulse mode only,
  947. * can be zero for non-burst sync events and burst modes
  948. */
  949. hsync = I915_READ(MIPI_HSYNC_PADDING_COUNT(port));
  950. hbp = I915_READ(MIPI_HBP_COUNT(port));
  951. /* harizontal values are in terms of high speed byte clock */
  952. hfp = pixels_from_txbyteclkhs(hfp, bpp, lane_count,
  953. intel_dsi->burst_mode_ratio);
  954. hsync = pixels_from_txbyteclkhs(hsync, bpp, lane_count,
  955. intel_dsi->burst_mode_ratio);
  956. hbp = pixels_from_txbyteclkhs(hbp, bpp, lane_count,
  957. intel_dsi->burst_mode_ratio);
  958. if (intel_dsi->dual_link) {
  959. hfp *= 2;
  960. hsync *= 2;
  961. hbp *= 2;
  962. }
  963. /* vertical values are in terms of lines */
  964. vfp = I915_READ(MIPI_VFP_COUNT(port));
  965. vsync = I915_READ(MIPI_VSYNC_PADDING_COUNT(port));
  966. vbp = I915_READ(MIPI_VBP_COUNT(port));
  967. adjusted_mode->crtc_htotal = hactive + hfp + hsync + hbp;
  968. adjusted_mode->crtc_hsync_start = hfp + adjusted_mode->crtc_hdisplay;
  969. adjusted_mode->crtc_hsync_end = hsync + adjusted_mode->crtc_hsync_start;
  970. adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hdisplay;
  971. adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_htotal;
  972. adjusted_mode->crtc_vsync_start = vfp + adjusted_mode->crtc_vdisplay;
  973. adjusted_mode->crtc_vsync_end = vsync + adjusted_mode->crtc_vsync_start;
  974. adjusted_mode->crtc_vblank_start = adjusted_mode->crtc_vdisplay;
  975. adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vtotal;
  976. /*
  977. * In BXT DSI there is no regs programmed with few horizontal timings
  978. * in Pixels but txbyteclkhs.. So retrieval process adds some
  979. * ROUND_UP ERRORS in the process of PIXELS<==>txbyteclkhs.
  980. * Actually here for the given adjusted_mode, we are calculating the
  981. * value programmed to the port and then back to the horizontal timing
  982. * param in pixels. This is the expected value, including roundup errors
  983. * And if that is same as retrieved value from port, then
  984. * (HW state) adjusted_mode's horizontal timings are corrected to
  985. * match with SW state to nullify the errors.
  986. */
  987. /* Calculating the value programmed to the Port register */
  988. hfp_sw = adjusted_mode_sw->crtc_hsync_start -
  989. adjusted_mode_sw->crtc_hdisplay;
  990. hsync_sw = adjusted_mode_sw->crtc_hsync_end -
  991. adjusted_mode_sw->crtc_hsync_start;
  992. hbp_sw = adjusted_mode_sw->crtc_htotal -
  993. adjusted_mode_sw->crtc_hsync_end;
  994. if (intel_dsi->dual_link) {
  995. hfp_sw /= 2;
  996. hsync_sw /= 2;
  997. hbp_sw /= 2;
  998. }
  999. hfp_sw = txbyteclkhs(hfp_sw, bpp, lane_count,
  1000. intel_dsi->burst_mode_ratio);
  1001. hsync_sw = txbyteclkhs(hsync_sw, bpp, lane_count,
  1002. intel_dsi->burst_mode_ratio);
  1003. hbp_sw = txbyteclkhs(hbp_sw, bpp, lane_count,
  1004. intel_dsi->burst_mode_ratio);
  1005. /* Reverse calculating the adjusted mode parameters from port reg vals*/
  1006. hfp_sw = pixels_from_txbyteclkhs(hfp_sw, bpp, lane_count,
  1007. intel_dsi->burst_mode_ratio);
  1008. hsync_sw = pixels_from_txbyteclkhs(hsync_sw, bpp, lane_count,
  1009. intel_dsi->burst_mode_ratio);
  1010. hbp_sw = pixels_from_txbyteclkhs(hbp_sw, bpp, lane_count,
  1011. intel_dsi->burst_mode_ratio);
  1012. if (intel_dsi->dual_link) {
  1013. hfp_sw *= 2;
  1014. hsync_sw *= 2;
  1015. hbp_sw *= 2;
  1016. }
  1017. crtc_htotal_sw = adjusted_mode_sw->crtc_hdisplay + hfp_sw +
  1018. hsync_sw + hbp_sw;
  1019. crtc_hsync_start_sw = hfp_sw + adjusted_mode_sw->crtc_hdisplay;
  1020. crtc_hsync_end_sw = hsync_sw + crtc_hsync_start_sw;
  1021. crtc_hblank_start_sw = adjusted_mode_sw->crtc_hdisplay;
  1022. crtc_hblank_end_sw = crtc_htotal_sw;
  1023. if (adjusted_mode->crtc_htotal == crtc_htotal_sw)
  1024. adjusted_mode->crtc_htotal = adjusted_mode_sw->crtc_htotal;
  1025. if (adjusted_mode->crtc_hsync_start == crtc_hsync_start_sw)
  1026. adjusted_mode->crtc_hsync_start =
  1027. adjusted_mode_sw->crtc_hsync_start;
  1028. if (adjusted_mode->crtc_hsync_end == crtc_hsync_end_sw)
  1029. adjusted_mode->crtc_hsync_end =
  1030. adjusted_mode_sw->crtc_hsync_end;
  1031. if (adjusted_mode->crtc_hblank_start == crtc_hblank_start_sw)
  1032. adjusted_mode->crtc_hblank_start =
  1033. adjusted_mode_sw->crtc_hblank_start;
  1034. if (adjusted_mode->crtc_hblank_end == crtc_hblank_end_sw)
  1035. adjusted_mode->crtc_hblank_end =
  1036. adjusted_mode_sw->crtc_hblank_end;
  1037. }
  1038. static void intel_dsi_get_config(struct intel_encoder *encoder,
  1039. struct intel_crtc_state *pipe_config)
  1040. {
  1041. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  1042. u32 pclk;
  1043. DRM_DEBUG_KMS("\n");
  1044. pipe_config->output_types |= BIT(INTEL_OUTPUT_DSI);
  1045. if (IS_GEN9_LP(dev_priv))
  1046. bxt_dsi_get_pipe_config(encoder, pipe_config);
  1047. pclk = intel_dsi_get_pclk(encoder, pipe_config->pipe_bpp,
  1048. pipe_config);
  1049. if (!pclk)
  1050. return;
  1051. pipe_config->base.adjusted_mode.crtc_clock = pclk;
  1052. pipe_config->port_clock = pclk;
  1053. }
  1054. static enum drm_mode_status
  1055. intel_dsi_mode_valid(struct drm_connector *connector,
  1056. struct drm_display_mode *mode)
  1057. {
  1058. struct intel_connector *intel_connector = to_intel_connector(connector);
  1059. const struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
  1060. int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
  1061. DRM_DEBUG_KMS("\n");
  1062. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  1063. return MODE_NO_DBLESCAN;
  1064. if (fixed_mode) {
  1065. if (mode->hdisplay > fixed_mode->hdisplay)
  1066. return MODE_PANEL;
  1067. if (mode->vdisplay > fixed_mode->vdisplay)
  1068. return MODE_PANEL;
  1069. if (fixed_mode->clock > max_dotclk)
  1070. return MODE_CLOCK_HIGH;
  1071. }
  1072. return MODE_OK;
  1073. }
  1074. /* return txclkesc cycles in terms of divider and duration in us */
  1075. static u16 txclkesc(u32 divider, unsigned int us)
  1076. {
  1077. switch (divider) {
  1078. case ESCAPE_CLOCK_DIVIDER_1:
  1079. default:
  1080. return 20 * us;
  1081. case ESCAPE_CLOCK_DIVIDER_2:
  1082. return 10 * us;
  1083. case ESCAPE_CLOCK_DIVIDER_4:
  1084. return 5 * us;
  1085. }
  1086. }
  1087. static void set_dsi_timings(struct drm_encoder *encoder,
  1088. const struct drm_display_mode *adjusted_mode)
  1089. {
  1090. struct drm_device *dev = encoder->dev;
  1091. struct drm_i915_private *dev_priv = to_i915(dev);
  1092. struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
  1093. enum port port;
  1094. unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
  1095. unsigned int lane_count = intel_dsi->lane_count;
  1096. u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
  1097. hactive = adjusted_mode->crtc_hdisplay;
  1098. hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
  1099. hsync = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
  1100. hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
  1101. if (intel_dsi->dual_link) {
  1102. hactive /= 2;
  1103. if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
  1104. hactive += intel_dsi->pixel_overlap;
  1105. hfp /= 2;
  1106. hsync /= 2;
  1107. hbp /= 2;
  1108. }
  1109. vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
  1110. vsync = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
  1111. vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
  1112. /* horizontal values are in terms of high speed byte clock */
  1113. hactive = txbyteclkhs(hactive, bpp, lane_count,
  1114. intel_dsi->burst_mode_ratio);
  1115. hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
  1116. hsync = txbyteclkhs(hsync, bpp, lane_count,
  1117. intel_dsi->burst_mode_ratio);
  1118. hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
  1119. for_each_dsi_port(port, intel_dsi->ports) {
  1120. if (IS_GEN9_LP(dev_priv)) {
  1121. /*
  1122. * Program hdisplay and vdisplay on MIPI transcoder.
  1123. * This is different from calculated hactive and
  1124. * vactive, as they are calculated per channel basis,
  1125. * whereas these values should be based on resolution.
  1126. */
  1127. I915_WRITE(BXT_MIPI_TRANS_HACTIVE(port),
  1128. adjusted_mode->crtc_hdisplay);
  1129. I915_WRITE(BXT_MIPI_TRANS_VACTIVE(port),
  1130. adjusted_mode->crtc_vdisplay);
  1131. I915_WRITE(BXT_MIPI_TRANS_VTOTAL(port),
  1132. adjusted_mode->crtc_vtotal);
  1133. }
  1134. I915_WRITE(MIPI_HACTIVE_AREA_COUNT(port), hactive);
  1135. I915_WRITE(MIPI_HFP_COUNT(port), hfp);
  1136. /* meaningful for video mode non-burst sync pulse mode only,
  1137. * can be zero for non-burst sync events and burst modes */
  1138. I915_WRITE(MIPI_HSYNC_PADDING_COUNT(port), hsync);
  1139. I915_WRITE(MIPI_HBP_COUNT(port), hbp);
  1140. /* vertical values are in terms of lines */
  1141. I915_WRITE(MIPI_VFP_COUNT(port), vfp);
  1142. I915_WRITE(MIPI_VSYNC_PADDING_COUNT(port), vsync);
  1143. I915_WRITE(MIPI_VBP_COUNT(port), vbp);
  1144. }
  1145. }
  1146. static u32 pixel_format_to_reg(enum mipi_dsi_pixel_format fmt)
  1147. {
  1148. switch (fmt) {
  1149. case MIPI_DSI_FMT_RGB888:
  1150. return VID_MODE_FORMAT_RGB888;
  1151. case MIPI_DSI_FMT_RGB666:
  1152. return VID_MODE_FORMAT_RGB666;
  1153. case MIPI_DSI_FMT_RGB666_PACKED:
  1154. return VID_MODE_FORMAT_RGB666_PACKED;
  1155. case MIPI_DSI_FMT_RGB565:
  1156. return VID_MODE_FORMAT_RGB565;
  1157. default:
  1158. MISSING_CASE(fmt);
  1159. return VID_MODE_FORMAT_RGB666;
  1160. }
  1161. }
  1162. static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
  1163. const struct intel_crtc_state *pipe_config)
  1164. {
  1165. struct drm_encoder *encoder = &intel_encoder->base;
  1166. struct drm_device *dev = encoder->dev;
  1167. struct drm_i915_private *dev_priv = to_i915(dev);
  1168. struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
  1169. struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
  1170. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  1171. enum port port;
  1172. unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
  1173. u32 val, tmp;
  1174. u16 mode_hdisplay;
  1175. DRM_DEBUG_KMS("pipe %c\n", pipe_name(intel_crtc->pipe));
  1176. mode_hdisplay = adjusted_mode->crtc_hdisplay;
  1177. if (intel_dsi->dual_link) {
  1178. mode_hdisplay /= 2;
  1179. if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
  1180. mode_hdisplay += intel_dsi->pixel_overlap;
  1181. }
  1182. for_each_dsi_port(port, intel_dsi->ports) {
  1183. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1184. /*
  1185. * escape clock divider, 20MHz, shared for A and C.
  1186. * device ready must be off when doing this! txclkesc?
  1187. */
  1188. tmp = I915_READ(MIPI_CTRL(PORT_A));
  1189. tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
  1190. I915_WRITE(MIPI_CTRL(PORT_A), tmp |
  1191. ESCAPE_CLOCK_DIVIDER_1);
  1192. /* read request priority is per pipe */
  1193. tmp = I915_READ(MIPI_CTRL(port));
  1194. tmp &= ~READ_REQUEST_PRIORITY_MASK;
  1195. I915_WRITE(MIPI_CTRL(port), tmp |
  1196. READ_REQUEST_PRIORITY_HIGH);
  1197. } else if (IS_GEN9_LP(dev_priv)) {
  1198. enum pipe pipe = intel_crtc->pipe;
  1199. tmp = I915_READ(MIPI_CTRL(port));
  1200. tmp &= ~BXT_PIPE_SELECT_MASK;
  1201. tmp |= BXT_PIPE_SELECT(pipe);
  1202. I915_WRITE(MIPI_CTRL(port), tmp);
  1203. }
  1204. /* XXX: why here, why like this? handling in irq handler?! */
  1205. I915_WRITE(MIPI_INTR_STAT(port), 0xffffffff);
  1206. I915_WRITE(MIPI_INTR_EN(port), 0xffffffff);
  1207. I915_WRITE(MIPI_DPHY_PARAM(port), intel_dsi->dphy_reg);
  1208. I915_WRITE(MIPI_DPI_RESOLUTION(port),
  1209. adjusted_mode->crtc_vdisplay << VERTICAL_ADDRESS_SHIFT |
  1210. mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
  1211. }
  1212. set_dsi_timings(encoder, adjusted_mode);
  1213. val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
  1214. if (is_cmd_mode(intel_dsi)) {
  1215. val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
  1216. val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
  1217. } else {
  1218. val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
  1219. val |= pixel_format_to_reg(intel_dsi->pixel_format);
  1220. }
  1221. tmp = 0;
  1222. if (intel_dsi->eotp_pkt == 0)
  1223. tmp |= EOT_DISABLE;
  1224. if (intel_dsi->clock_stop)
  1225. tmp |= CLOCKSTOP;
  1226. if (IS_GEN9_LP(dev_priv)) {
  1227. tmp |= BXT_DPHY_DEFEATURE_EN;
  1228. if (!is_cmd_mode(intel_dsi))
  1229. tmp |= BXT_DEFEATURE_DPI_FIFO_CTR;
  1230. }
  1231. for_each_dsi_port(port, intel_dsi->ports) {
  1232. I915_WRITE(MIPI_DSI_FUNC_PRG(port), val);
  1233. /* timeouts for recovery. one frame IIUC. if counter expires,
  1234. * EOT and stop state. */
  1235. /*
  1236. * In burst mode, value greater than one DPI line Time in byte
  1237. * clock (txbyteclkhs) To timeout this timer 1+ of the above
  1238. * said value is recommended.
  1239. *
  1240. * In non-burst mode, Value greater than one DPI frame time in
  1241. * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
  1242. * said value is recommended.
  1243. *
  1244. * In DBI only mode, value greater than one DBI frame time in
  1245. * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
  1246. * said value is recommended.
  1247. */
  1248. if (is_vid_mode(intel_dsi) &&
  1249. intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
  1250. I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
  1251. txbyteclkhs(adjusted_mode->crtc_htotal, bpp,
  1252. intel_dsi->lane_count,
  1253. intel_dsi->burst_mode_ratio) + 1);
  1254. } else {
  1255. I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
  1256. txbyteclkhs(adjusted_mode->crtc_vtotal *
  1257. adjusted_mode->crtc_htotal,
  1258. bpp, intel_dsi->lane_count,
  1259. intel_dsi->burst_mode_ratio) + 1);
  1260. }
  1261. I915_WRITE(MIPI_LP_RX_TIMEOUT(port), intel_dsi->lp_rx_timeout);
  1262. I915_WRITE(MIPI_TURN_AROUND_TIMEOUT(port),
  1263. intel_dsi->turn_arnd_val);
  1264. I915_WRITE(MIPI_DEVICE_RESET_TIMER(port),
  1265. intel_dsi->rst_timer_val);
  1266. /* dphy stuff */
  1267. /* in terms of low power clock */
  1268. I915_WRITE(MIPI_INIT_COUNT(port),
  1269. txclkesc(intel_dsi->escape_clk_div, 100));
  1270. if (IS_GEN9_LP(dev_priv) && (!intel_dsi->dual_link)) {
  1271. /*
  1272. * BXT spec says write MIPI_INIT_COUNT for
  1273. * both the ports, even if only one is
  1274. * getting used. So write the other port
  1275. * if not in dual link mode.
  1276. */
  1277. I915_WRITE(MIPI_INIT_COUNT(port ==
  1278. PORT_A ? PORT_C : PORT_A),
  1279. intel_dsi->init_count);
  1280. }
  1281. /* recovery disables */
  1282. I915_WRITE(MIPI_EOT_DISABLE(port), tmp);
  1283. /* in terms of low power clock */
  1284. I915_WRITE(MIPI_INIT_COUNT(port), intel_dsi->init_count);
  1285. /* in terms of txbyteclkhs. actual high to low switch +
  1286. * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
  1287. *
  1288. * XXX: write MIPI_STOP_STATE_STALL?
  1289. */
  1290. I915_WRITE(MIPI_HIGH_LOW_SWITCH_COUNT(port),
  1291. intel_dsi->hs_to_lp_count);
  1292. /* XXX: low power clock equivalence in terms of byte clock.
  1293. * the number of byte clocks occupied in one low power clock.
  1294. * based on txbyteclkhs and txclkesc.
  1295. * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
  1296. * ) / 105.???
  1297. */
  1298. I915_WRITE(MIPI_LP_BYTECLK(port), intel_dsi->lp_byte_clk);
  1299. if (IS_GEMINILAKE(dev_priv)) {
  1300. I915_WRITE(MIPI_TLPX_TIME_COUNT(port),
  1301. intel_dsi->lp_byte_clk);
  1302. /* Shadow of DPHY reg */
  1303. I915_WRITE(MIPI_CLK_LANE_TIMING(port),
  1304. intel_dsi->dphy_reg);
  1305. }
  1306. /* the bw essential for transmitting 16 long packets containing
  1307. * 252 bytes meant for dcs write memory command is programmed in
  1308. * this register in terms of byte clocks. based on dsi transfer
  1309. * rate and the number of lanes configured the time taken to
  1310. * transmit 16 long packets in a dsi stream varies. */
  1311. I915_WRITE(MIPI_DBI_BW_CTRL(port), intel_dsi->bw_timer);
  1312. I915_WRITE(MIPI_CLK_LANE_SWITCH_TIME_CNT(port),
  1313. intel_dsi->clk_lp_to_hs_count << LP_HS_SSW_CNT_SHIFT |
  1314. intel_dsi->clk_hs_to_lp_count << HS_LP_PWR_SW_CNT_SHIFT);
  1315. if (is_vid_mode(intel_dsi))
  1316. /* Some panels might have resolution which is not a
  1317. * multiple of 64 like 1366 x 768. Enable RANDOM
  1318. * resolution support for such panels by default */
  1319. I915_WRITE(MIPI_VIDEO_MODE_FORMAT(port),
  1320. intel_dsi->video_frmt_cfg_bits |
  1321. intel_dsi->video_mode_format |
  1322. IP_TG_CONFIG |
  1323. RANDOM_DPI_DISPLAY_RESOLUTION);
  1324. }
  1325. }
  1326. static void intel_dsi_unprepare(struct intel_encoder *encoder)
  1327. {
  1328. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  1329. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  1330. enum port port;
  1331. u32 val;
  1332. if (!IS_GEMINILAKE(dev_priv)) {
  1333. for_each_dsi_port(port, intel_dsi->ports) {
  1334. /* Panel commands can be sent when clock is in LP11 */
  1335. I915_WRITE(MIPI_DEVICE_READY(port), 0x0);
  1336. intel_dsi_reset_clocks(encoder, port);
  1337. I915_WRITE(MIPI_EOT_DISABLE(port), CLOCKSTOP);
  1338. val = I915_READ(MIPI_DSI_FUNC_PRG(port));
  1339. val &= ~VID_MODE_FORMAT_MASK;
  1340. I915_WRITE(MIPI_DSI_FUNC_PRG(port), val);
  1341. I915_WRITE(MIPI_DEVICE_READY(port), 0x1);
  1342. }
  1343. }
  1344. }
  1345. static int intel_dsi_get_modes(struct drm_connector *connector)
  1346. {
  1347. struct intel_connector *intel_connector = to_intel_connector(connector);
  1348. struct drm_display_mode *mode;
  1349. DRM_DEBUG_KMS("\n");
  1350. if (!intel_connector->panel.fixed_mode) {
  1351. DRM_DEBUG_KMS("no fixed mode\n");
  1352. return 0;
  1353. }
  1354. mode = drm_mode_duplicate(connector->dev,
  1355. intel_connector->panel.fixed_mode);
  1356. if (!mode) {
  1357. DRM_DEBUG_KMS("drm_mode_duplicate failed\n");
  1358. return 0;
  1359. }
  1360. drm_mode_probed_add(connector, mode);
  1361. return 1;
  1362. }
  1363. static void intel_dsi_connector_destroy(struct drm_connector *connector)
  1364. {
  1365. struct intel_connector *intel_connector = to_intel_connector(connector);
  1366. DRM_DEBUG_KMS("\n");
  1367. intel_panel_fini(&intel_connector->panel);
  1368. drm_connector_cleanup(connector);
  1369. kfree(connector);
  1370. }
  1371. static void intel_dsi_encoder_destroy(struct drm_encoder *encoder)
  1372. {
  1373. struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
  1374. /* dispose of the gpios */
  1375. if (intel_dsi->gpio_panel)
  1376. gpiod_put(intel_dsi->gpio_panel);
  1377. intel_encoder_destroy(encoder);
  1378. }
  1379. static const struct drm_encoder_funcs intel_dsi_funcs = {
  1380. .destroy = intel_dsi_encoder_destroy,
  1381. };
  1382. static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
  1383. .get_modes = intel_dsi_get_modes,
  1384. .mode_valid = intel_dsi_mode_valid,
  1385. .atomic_check = intel_digital_connector_atomic_check,
  1386. };
  1387. static const struct drm_connector_funcs intel_dsi_connector_funcs = {
  1388. .late_register = intel_connector_register,
  1389. .early_unregister = intel_connector_unregister,
  1390. .destroy = intel_dsi_connector_destroy,
  1391. .fill_modes = drm_helper_probe_single_connector_modes,
  1392. .atomic_get_property = intel_digital_connector_atomic_get_property,
  1393. .atomic_set_property = intel_digital_connector_atomic_set_property,
  1394. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1395. .atomic_duplicate_state = intel_digital_connector_duplicate_state,
  1396. };
  1397. static int intel_dsi_get_panel_orientation(struct intel_connector *connector)
  1398. {
  1399. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1400. int orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
  1401. enum i9xx_plane_id i9xx_plane;
  1402. u32 val;
  1403. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1404. if (connector->encoder->crtc_mask == BIT(PIPE_B))
  1405. i9xx_plane = PLANE_B;
  1406. else
  1407. i9xx_plane = PLANE_A;
  1408. val = I915_READ(DSPCNTR(i9xx_plane));
  1409. if (val & DISPPLANE_ROTATE_180)
  1410. orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
  1411. }
  1412. return orientation;
  1413. }
  1414. static void intel_dsi_add_properties(struct intel_connector *connector)
  1415. {
  1416. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1417. if (connector->panel.fixed_mode) {
  1418. u32 allowed_scalers;
  1419. allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) | BIT(DRM_MODE_SCALE_FULLSCREEN);
  1420. if (!HAS_GMCH_DISPLAY(dev_priv))
  1421. allowed_scalers |= BIT(DRM_MODE_SCALE_CENTER);
  1422. drm_connector_attach_scaling_mode_property(&connector->base,
  1423. allowed_scalers);
  1424. connector->base.state->scaling_mode = DRM_MODE_SCALE_ASPECT;
  1425. connector->base.display_info.panel_orientation =
  1426. intel_dsi_get_panel_orientation(connector);
  1427. drm_connector_init_panel_orientation_property(
  1428. &connector->base,
  1429. connector->panel.fixed_mode->hdisplay,
  1430. connector->panel.fixed_mode->vdisplay);
  1431. }
  1432. }
  1433. void intel_dsi_init(struct drm_i915_private *dev_priv)
  1434. {
  1435. struct drm_device *dev = &dev_priv->drm;
  1436. struct intel_dsi *intel_dsi;
  1437. struct intel_encoder *intel_encoder;
  1438. struct drm_encoder *encoder;
  1439. struct intel_connector *intel_connector;
  1440. struct drm_connector *connector;
  1441. struct drm_display_mode *scan, *fixed_mode = NULL;
  1442. enum port port;
  1443. DRM_DEBUG_KMS("\n");
  1444. /* There is no detection method for MIPI so rely on VBT */
  1445. if (!intel_bios_is_dsi_present(dev_priv, &port))
  1446. return;
  1447. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1448. dev_priv->mipi_mmio_base = VLV_MIPI_BASE;
  1449. } else if (IS_GEN9_LP(dev_priv)) {
  1450. dev_priv->mipi_mmio_base = BXT_MIPI_BASE;
  1451. } else {
  1452. DRM_ERROR("Unsupported Mipi device to reg base");
  1453. return;
  1454. }
  1455. intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
  1456. if (!intel_dsi)
  1457. return;
  1458. intel_connector = intel_connector_alloc();
  1459. if (!intel_connector) {
  1460. kfree(intel_dsi);
  1461. return;
  1462. }
  1463. intel_encoder = &intel_dsi->base;
  1464. encoder = &intel_encoder->base;
  1465. intel_dsi->attached_connector = intel_connector;
  1466. connector = &intel_connector->base;
  1467. drm_encoder_init(dev, encoder, &intel_dsi_funcs, DRM_MODE_ENCODER_DSI,
  1468. "DSI %c", port_name(port));
  1469. intel_encoder->compute_config = intel_dsi_compute_config;
  1470. intel_encoder->pre_enable = intel_dsi_pre_enable;
  1471. intel_encoder->enable = intel_dsi_enable_nop;
  1472. intel_encoder->disable = intel_dsi_disable;
  1473. intel_encoder->post_disable = intel_dsi_post_disable;
  1474. intel_encoder->get_hw_state = intel_dsi_get_hw_state;
  1475. intel_encoder->get_config = intel_dsi_get_config;
  1476. intel_connector->get_hw_state = intel_connector_get_hw_state;
  1477. intel_encoder->port = port;
  1478. /*
  1479. * On BYT/CHV, pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI
  1480. * port C. BXT isn't limited like this.
  1481. */
  1482. if (IS_GEN9_LP(dev_priv))
  1483. intel_encoder->crtc_mask = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C);
  1484. else if (port == PORT_A)
  1485. intel_encoder->crtc_mask = BIT(PIPE_A);
  1486. else
  1487. intel_encoder->crtc_mask = BIT(PIPE_B);
  1488. if (dev_priv->vbt.dsi.config->dual_link)
  1489. intel_dsi->ports = BIT(PORT_A) | BIT(PORT_C);
  1490. else
  1491. intel_dsi->ports = BIT(port);
  1492. intel_dsi->dcs_backlight_ports = dev_priv->vbt.dsi.bl_ports;
  1493. intel_dsi->dcs_cabc_ports = dev_priv->vbt.dsi.cabc_ports;
  1494. /* Create a DSI host (and a device) for each port. */
  1495. for_each_dsi_port(port, intel_dsi->ports) {
  1496. struct intel_dsi_host *host;
  1497. host = intel_dsi_host_init(intel_dsi, port);
  1498. if (!host)
  1499. goto err;
  1500. intel_dsi->dsi_hosts[port] = host;
  1501. }
  1502. if (!intel_dsi_vbt_init(intel_dsi, MIPI_DSI_GENERIC_PANEL_ID)) {
  1503. DRM_DEBUG_KMS("no device found\n");
  1504. goto err;
  1505. }
  1506. /*
  1507. * In case of BYT with CRC PMIC, we need to use GPIO for
  1508. * Panel control.
  1509. */
  1510. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  1511. (dev_priv->vbt.dsi.config->pwm_blc == PPS_BLC_PMIC)) {
  1512. intel_dsi->gpio_panel =
  1513. gpiod_get(dev->dev, "panel", GPIOD_OUT_HIGH);
  1514. if (IS_ERR(intel_dsi->gpio_panel)) {
  1515. DRM_ERROR("Failed to own gpio for panel control\n");
  1516. intel_dsi->gpio_panel = NULL;
  1517. }
  1518. }
  1519. intel_encoder->type = INTEL_OUTPUT_DSI;
  1520. intel_encoder->power_domain = POWER_DOMAIN_PORT_DSI;
  1521. intel_encoder->cloneable = 0;
  1522. drm_connector_init(dev, connector, &intel_dsi_connector_funcs,
  1523. DRM_MODE_CONNECTOR_DSI);
  1524. drm_connector_helper_add(connector, &intel_dsi_connector_helper_funcs);
  1525. connector->display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
  1526. connector->interlace_allowed = false;
  1527. connector->doublescan_allowed = false;
  1528. intel_connector_attach_encoder(intel_connector, intel_encoder);
  1529. mutex_lock(&dev->mode_config.mutex);
  1530. intel_dsi_vbt_get_modes(intel_dsi);
  1531. list_for_each_entry(scan, &connector->probed_modes, head) {
  1532. if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
  1533. fixed_mode = drm_mode_duplicate(dev, scan);
  1534. break;
  1535. }
  1536. }
  1537. mutex_unlock(&dev->mode_config.mutex);
  1538. if (!fixed_mode) {
  1539. DRM_DEBUG_KMS("no fixed mode\n");
  1540. goto err;
  1541. }
  1542. connector->display_info.width_mm = fixed_mode->width_mm;
  1543. connector->display_info.height_mm = fixed_mode->height_mm;
  1544. intel_panel_init(&intel_connector->panel, fixed_mode, NULL);
  1545. intel_panel_setup_backlight(connector, INVALID_PIPE);
  1546. intel_dsi_add_properties(intel_connector);
  1547. return;
  1548. err:
  1549. drm_encoder_cleanup(&intel_encoder->base);
  1550. kfree(intel_dsi);
  1551. kfree(intel_connector);
  1552. }