intel_dsi_cmd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 <linux/export.h>
  26. #include <drm/drmP.h>
  27. #include <drm/drm_crtc.h>
  28. #include <video/mipi_display.h>
  29. #include "i915_drv.h"
  30. #include "intel_drv.h"
  31. #include "intel_dsi.h"
  32. #include "intel_dsi_cmd.h"
  33. /*
  34. * XXX: MIPI_DATA_ADDRESS, MIPI_DATA_LENGTH, MIPI_COMMAND_LENGTH, and
  35. * MIPI_COMMAND_ADDRESS registers.
  36. *
  37. * Apparently these registers provide a MIPI adapter level way to send (lots of)
  38. * commands and data to the receiver, without having to write the commands and
  39. * data to MIPI_{HS,LP}_GEN_{CTRL,DATA} registers word by word.
  40. *
  41. * Presumably for anything other than MIPI_DCS_WRITE_MEMORY_START and
  42. * MIPI_DCS_WRITE_MEMORY_CONTINUE (which are used to update the external
  43. * framebuffer in command mode displays) these are just an optimization that can
  44. * come later.
  45. *
  46. * For memory writes, these should probably be used for performance.
  47. */
  48. static void print_stat(struct intel_dsi *intel_dsi)
  49. {
  50. struct drm_encoder *encoder = &intel_dsi->base.base;
  51. struct drm_device *dev = encoder->dev;
  52. struct drm_i915_private *dev_priv = dev->dev_private;
  53. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  54. enum pipe pipe = intel_crtc->pipe;
  55. u32 val;
  56. val = I915_READ(MIPI_INTR_STAT(pipe));
  57. #define STAT_BIT(val, bit) (val) & (bit) ? " " #bit : ""
  58. DRM_DEBUG_KMS("MIPI_INTR_STAT(%d) = %08x"
  59. "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
  60. "\n", pipe, val,
  61. STAT_BIT(val, TEARING_EFFECT),
  62. STAT_BIT(val, SPL_PKT_SENT_INTERRUPT),
  63. STAT_BIT(val, GEN_READ_DATA_AVAIL),
  64. STAT_BIT(val, LP_GENERIC_WR_FIFO_FULL),
  65. STAT_BIT(val, HS_GENERIC_WR_FIFO_FULL),
  66. STAT_BIT(val, RX_PROT_VIOLATION),
  67. STAT_BIT(val, RX_INVALID_TX_LENGTH),
  68. STAT_BIT(val, ACK_WITH_NO_ERROR),
  69. STAT_BIT(val, TURN_AROUND_ACK_TIMEOUT),
  70. STAT_BIT(val, LP_RX_TIMEOUT),
  71. STAT_BIT(val, HS_TX_TIMEOUT),
  72. STAT_BIT(val, DPI_FIFO_UNDERRUN),
  73. STAT_BIT(val, LOW_CONTENTION),
  74. STAT_BIT(val, HIGH_CONTENTION),
  75. STAT_BIT(val, TXDSI_VC_ID_INVALID),
  76. STAT_BIT(val, TXDSI_DATA_TYPE_NOT_RECOGNISED),
  77. STAT_BIT(val, TXCHECKSUM_ERROR),
  78. STAT_BIT(val, TXECC_MULTIBIT_ERROR),
  79. STAT_BIT(val, TXECC_SINGLE_BIT_ERROR),
  80. STAT_BIT(val, TXFALSE_CONTROL_ERROR),
  81. STAT_BIT(val, RXDSI_VC_ID_INVALID),
  82. STAT_BIT(val, RXDSI_DATA_TYPE_NOT_REGOGNISED),
  83. STAT_BIT(val, RXCHECKSUM_ERROR),
  84. STAT_BIT(val, RXECC_MULTIBIT_ERROR),
  85. STAT_BIT(val, RXECC_SINGLE_BIT_ERROR),
  86. STAT_BIT(val, RXFALSE_CONTROL_ERROR),
  87. STAT_BIT(val, RXHS_RECEIVE_TIMEOUT_ERROR),
  88. STAT_BIT(val, RX_LP_TX_SYNC_ERROR),
  89. STAT_BIT(val, RXEXCAPE_MODE_ENTRY_ERROR),
  90. STAT_BIT(val, RXEOT_SYNC_ERROR),
  91. STAT_BIT(val, RXSOT_SYNC_ERROR),
  92. STAT_BIT(val, RXSOT_ERROR));
  93. #undef STAT_BIT
  94. }
  95. enum dsi_type {
  96. DSI_DCS,
  97. DSI_GENERIC,
  98. };
  99. /* enable or disable command mode hs transmissions */
  100. void dsi_hs_mode_enable(struct intel_dsi *intel_dsi, bool enable)
  101. {
  102. struct drm_encoder *encoder = &intel_dsi->base.base;
  103. struct drm_device *dev = encoder->dev;
  104. struct drm_i915_private *dev_priv = dev->dev_private;
  105. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  106. enum pipe pipe = intel_crtc->pipe;
  107. u32 temp;
  108. u32 mask = DBI_FIFO_EMPTY;
  109. if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(pipe)) & mask) == mask, 50))
  110. DRM_ERROR("Timeout waiting for DBI FIFO empty\n");
  111. temp = I915_READ(MIPI_HS_LP_DBI_ENABLE(pipe));
  112. temp &= DBI_HS_LP_MODE_MASK;
  113. I915_WRITE(MIPI_HS_LP_DBI_ENABLE(pipe), enable ? DBI_HS_MODE : DBI_LP_MODE);
  114. intel_dsi->hs = enable;
  115. }
  116. static int dsi_vc_send_short(struct intel_dsi *intel_dsi, int channel,
  117. u8 data_type, u16 data)
  118. {
  119. struct drm_encoder *encoder = &intel_dsi->base.base;
  120. struct drm_device *dev = encoder->dev;
  121. struct drm_i915_private *dev_priv = dev->dev_private;
  122. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  123. enum pipe pipe = intel_crtc->pipe;
  124. u32 ctrl_reg;
  125. u32 ctrl;
  126. u32 mask;
  127. DRM_DEBUG_KMS("channel %d, data_type %d, data %04x\n",
  128. channel, data_type, data);
  129. if (intel_dsi->hs) {
  130. ctrl_reg = MIPI_HS_GEN_CTRL(pipe);
  131. mask = HS_CTRL_FIFO_FULL;
  132. } else {
  133. ctrl_reg = MIPI_LP_GEN_CTRL(pipe);
  134. mask = LP_CTRL_FIFO_FULL;
  135. }
  136. if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(pipe)) & mask) == 0, 50)) {
  137. DRM_ERROR("Timeout waiting for HS/LP CTRL FIFO !full\n");
  138. print_stat(intel_dsi);
  139. }
  140. /*
  141. * Note: This function is also used for long packets, with length passed
  142. * as data, since SHORT_PACKET_PARAM_SHIFT ==
  143. * LONG_PACKET_WORD_COUNT_SHIFT.
  144. */
  145. ctrl = data << SHORT_PACKET_PARAM_SHIFT |
  146. channel << VIRTUAL_CHANNEL_SHIFT |
  147. data_type << DATA_TYPE_SHIFT;
  148. I915_WRITE(ctrl_reg, ctrl);
  149. return 0;
  150. }
  151. static int dsi_vc_send_long(struct intel_dsi *intel_dsi, int channel,
  152. u8 data_type, const u8 *data, int len)
  153. {
  154. struct drm_encoder *encoder = &intel_dsi->base.base;
  155. struct drm_device *dev = encoder->dev;
  156. struct drm_i915_private *dev_priv = dev->dev_private;
  157. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  158. enum pipe pipe = intel_crtc->pipe;
  159. u32 data_reg;
  160. int i, j, n;
  161. u32 mask;
  162. DRM_DEBUG_KMS("channel %d, data_type %d, len %04x\n",
  163. channel, data_type, len);
  164. if (intel_dsi->hs) {
  165. data_reg = MIPI_HS_GEN_DATA(pipe);
  166. mask = HS_DATA_FIFO_FULL;
  167. } else {
  168. data_reg = MIPI_LP_GEN_DATA(pipe);
  169. mask = LP_DATA_FIFO_FULL;
  170. }
  171. if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(pipe)) & mask) == 0, 50))
  172. DRM_ERROR("Timeout waiting for HS/LP DATA FIFO !full\n");
  173. for (i = 0; i < len; i += n) {
  174. u32 val = 0;
  175. n = min_t(int, len - i, 4);
  176. for (j = 0; j < n; j++)
  177. val |= *data++ << 8 * j;
  178. I915_WRITE(data_reg, val);
  179. /* XXX: check for data fifo full, once that is set, write 4
  180. * dwords, then wait for not set, then continue. */
  181. }
  182. return dsi_vc_send_short(intel_dsi, channel, data_type, len);
  183. }
  184. static int dsi_vc_write_common(struct intel_dsi *intel_dsi,
  185. int channel, const u8 *data, int len,
  186. enum dsi_type type)
  187. {
  188. int ret;
  189. if (len == 0) {
  190. BUG_ON(type == DSI_GENERIC);
  191. ret = dsi_vc_send_short(intel_dsi, channel,
  192. MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM,
  193. 0);
  194. } else if (len == 1) {
  195. ret = dsi_vc_send_short(intel_dsi, channel,
  196. type == DSI_GENERIC ?
  197. MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM :
  198. MIPI_DSI_DCS_SHORT_WRITE, data[0]);
  199. } else if (len == 2) {
  200. ret = dsi_vc_send_short(intel_dsi, channel,
  201. type == DSI_GENERIC ?
  202. MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM :
  203. MIPI_DSI_DCS_SHORT_WRITE_PARAM,
  204. (data[1] << 8) | data[0]);
  205. } else {
  206. ret = dsi_vc_send_long(intel_dsi, channel,
  207. type == DSI_GENERIC ?
  208. MIPI_DSI_GENERIC_LONG_WRITE :
  209. MIPI_DSI_DCS_LONG_WRITE, data, len);
  210. }
  211. return ret;
  212. }
  213. int dsi_vc_dcs_write(struct intel_dsi *intel_dsi, int channel,
  214. const u8 *data, int len)
  215. {
  216. return dsi_vc_write_common(intel_dsi, channel, data, len, DSI_DCS);
  217. }
  218. int dsi_vc_generic_write(struct intel_dsi *intel_dsi, int channel,
  219. const u8 *data, int len)
  220. {
  221. return dsi_vc_write_common(intel_dsi, channel, data, len, DSI_GENERIC);
  222. }
  223. static int dsi_vc_dcs_send_read_request(struct intel_dsi *intel_dsi,
  224. int channel, u8 dcs_cmd)
  225. {
  226. return dsi_vc_send_short(intel_dsi, channel, MIPI_DSI_DCS_READ,
  227. dcs_cmd);
  228. }
  229. static int dsi_vc_generic_send_read_request(struct intel_dsi *intel_dsi,
  230. int channel, u8 *reqdata,
  231. int reqlen)
  232. {
  233. u16 data;
  234. u8 data_type;
  235. switch (reqlen) {
  236. case 0:
  237. data_type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
  238. data = 0;
  239. break;
  240. case 1:
  241. data_type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
  242. data = reqdata[0];
  243. break;
  244. case 2:
  245. data_type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
  246. data = (reqdata[1] << 8) | reqdata[0];
  247. break;
  248. default:
  249. BUG();
  250. }
  251. return dsi_vc_send_short(intel_dsi, channel, data_type, data);
  252. }
  253. static int dsi_read_data_return(struct intel_dsi *intel_dsi,
  254. u8 *buf, int buflen)
  255. {
  256. struct drm_encoder *encoder = &intel_dsi->base.base;
  257. struct drm_device *dev = encoder->dev;
  258. struct drm_i915_private *dev_priv = dev->dev_private;
  259. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  260. enum pipe pipe = intel_crtc->pipe;
  261. int i, len = 0;
  262. u32 data_reg, val;
  263. if (intel_dsi->hs) {
  264. data_reg = MIPI_HS_GEN_DATA(pipe);
  265. } else {
  266. data_reg = MIPI_LP_GEN_DATA(pipe);
  267. }
  268. while (len < buflen) {
  269. val = I915_READ(data_reg);
  270. for (i = 0; i < 4 && len < buflen; i++, len++)
  271. buf[len] = val >> 8 * i;
  272. }
  273. return len;
  274. }
  275. int dsi_vc_dcs_read(struct intel_dsi *intel_dsi, int channel, u8 dcs_cmd,
  276. u8 *buf, int buflen)
  277. {
  278. struct drm_encoder *encoder = &intel_dsi->base.base;
  279. struct drm_device *dev = encoder->dev;
  280. struct drm_i915_private *dev_priv = dev->dev_private;
  281. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  282. enum pipe pipe = intel_crtc->pipe;
  283. u32 mask;
  284. int ret;
  285. /*
  286. * XXX: should issue multiple read requests and reads if request is
  287. * longer than MIPI_MAX_RETURN_PKT_SIZE
  288. */
  289. I915_WRITE(MIPI_INTR_STAT(pipe), GEN_READ_DATA_AVAIL);
  290. ret = dsi_vc_dcs_send_read_request(intel_dsi, channel, dcs_cmd);
  291. if (ret)
  292. return ret;
  293. mask = GEN_READ_DATA_AVAIL;
  294. if (wait_for((I915_READ(MIPI_INTR_STAT(pipe)) & mask) == mask, 50))
  295. DRM_ERROR("Timeout waiting for read data.\n");
  296. ret = dsi_read_data_return(intel_dsi, buf, buflen);
  297. if (ret < 0)
  298. return ret;
  299. if (ret != buflen)
  300. return -EIO;
  301. return 0;
  302. }
  303. int dsi_vc_generic_read(struct intel_dsi *intel_dsi, int channel,
  304. u8 *reqdata, int reqlen, u8 *buf, int buflen)
  305. {
  306. struct drm_encoder *encoder = &intel_dsi->base.base;
  307. struct drm_device *dev = encoder->dev;
  308. struct drm_i915_private *dev_priv = dev->dev_private;
  309. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  310. enum pipe pipe = intel_crtc->pipe;
  311. u32 mask;
  312. int ret;
  313. /*
  314. * XXX: should issue multiple read requests and reads if request is
  315. * longer than MIPI_MAX_RETURN_PKT_SIZE
  316. */
  317. I915_WRITE(MIPI_INTR_STAT(pipe), GEN_READ_DATA_AVAIL);
  318. ret = dsi_vc_generic_send_read_request(intel_dsi, channel, reqdata,
  319. reqlen);
  320. if (ret)
  321. return ret;
  322. mask = GEN_READ_DATA_AVAIL;
  323. if (wait_for((I915_READ(MIPI_INTR_STAT(pipe)) & mask) == mask, 50))
  324. DRM_ERROR("Timeout waiting for read data.\n");
  325. ret = dsi_read_data_return(intel_dsi, buf, buflen);
  326. if (ret < 0)
  327. return ret;
  328. if (ret != buflen)
  329. return -EIO;
  330. return 0;
  331. }
  332. /*
  333. * send a video mode command
  334. *
  335. * XXX: commands with data in MIPI_DPI_DATA?
  336. */
  337. int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd)
  338. {
  339. struct drm_encoder *encoder = &intel_dsi->base.base;
  340. struct drm_device *dev = encoder->dev;
  341. struct drm_i915_private *dev_priv = dev->dev_private;
  342. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  343. enum pipe pipe = intel_crtc->pipe;
  344. u32 mask;
  345. /* XXX: pipe, hs */
  346. if (intel_dsi->hs)
  347. cmd &= ~DPI_LP_MODE;
  348. else
  349. cmd |= DPI_LP_MODE;
  350. /* DPI virtual channel?! */
  351. mask = DPI_FIFO_EMPTY;
  352. if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(pipe)) & mask) == mask, 50))
  353. DRM_ERROR("Timeout waiting for DPI FIFO empty.\n");
  354. /* clear bit */
  355. I915_WRITE(MIPI_INTR_STAT(pipe), SPL_PKT_SENT_INTERRUPT);
  356. /* XXX: old code skips write if control unchanged */
  357. if (cmd == I915_READ(MIPI_DPI_CONTROL(pipe)))
  358. DRM_ERROR("Same special packet %02x twice in a row.\n", cmd);
  359. I915_WRITE(MIPI_DPI_CONTROL(pipe), cmd);
  360. mask = SPL_PKT_SENT_INTERRUPT;
  361. if (wait_for((I915_READ(MIPI_INTR_STAT(pipe)) & mask) == mask, 100))
  362. DRM_ERROR("Video mode command 0x%08x send failed.\n", cmd);
  363. return 0;
  364. }