intel_dsi.c 55 KB

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