intel_dsi.c 55 KB

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