intel_dsi.c 45 KB

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