dw-mipi-dsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
  3. * Copyright (C) STMicroelectronics SA 2017
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Modified by Philippe Cornu <philippe.cornu@st.com>
  11. * This generic Synopsys DesignWare MIPI DSI host driver is based on the
  12. * Rockchip version from rockchip/dw-mipi-dsi.c with phy & bridge APIs.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/component.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/reset.h>
  21. #include <drm/drmP.h>
  22. #include <drm/drm_atomic_helper.h>
  23. #include <drm/drm_bridge.h>
  24. #include <drm/drm_crtc.h>
  25. #include <drm/drm_crtc_helper.h>
  26. #include <drm/drm_mipi_dsi.h>
  27. #include <drm/drm_of.h>
  28. #include <drm/bridge/dw_mipi_dsi.h>
  29. #include <video/mipi_display.h>
  30. #define HWVER_131 0x31333100 /* IP version 1.31 */
  31. #define DSI_VERSION 0x00
  32. #define VERSION GENMASK(31, 8)
  33. #define DSI_PWR_UP 0x04
  34. #define RESET 0
  35. #define POWERUP BIT(0)
  36. #define DSI_CLKMGR_CFG 0x08
  37. #define TO_CLK_DIVISION(div) (((div) & 0xff) << 8)
  38. #define TX_ESC_CLK_DIVISION(div) ((div) & 0xff)
  39. #define DSI_DPI_VCID 0x0c
  40. #define DPI_VCID(vcid) ((vcid) & 0x3)
  41. #define DSI_DPI_COLOR_CODING 0x10
  42. #define LOOSELY18_EN BIT(8)
  43. #define DPI_COLOR_CODING_16BIT_1 0x0
  44. #define DPI_COLOR_CODING_16BIT_2 0x1
  45. #define DPI_COLOR_CODING_16BIT_3 0x2
  46. #define DPI_COLOR_CODING_18BIT_1 0x3
  47. #define DPI_COLOR_CODING_18BIT_2 0x4
  48. #define DPI_COLOR_CODING_24BIT 0x5
  49. #define DSI_DPI_CFG_POL 0x14
  50. #define COLORM_ACTIVE_LOW BIT(4)
  51. #define SHUTD_ACTIVE_LOW BIT(3)
  52. #define HSYNC_ACTIVE_LOW BIT(2)
  53. #define VSYNC_ACTIVE_LOW BIT(1)
  54. #define DATAEN_ACTIVE_LOW BIT(0)
  55. #define DSI_DPI_LP_CMD_TIM 0x18
  56. #define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
  57. #define INVACT_LPCMD_TIME(p) ((p) & 0xff)
  58. #define DSI_DBI_VCID 0x1c
  59. #define DSI_DBI_CFG 0x20
  60. #define DSI_DBI_PARTITIONING_EN 0x24
  61. #define DSI_DBI_CMDSIZE 0x28
  62. #define DSI_PCKHDL_CFG 0x2c
  63. #define CRC_RX_EN BIT(4)
  64. #define ECC_RX_EN BIT(3)
  65. #define BTA_EN BIT(2)
  66. #define EOTP_RX_EN BIT(1)
  67. #define EOTP_TX_EN BIT(0)
  68. #define DSI_GEN_VCID 0x30
  69. #define DSI_MODE_CFG 0x34
  70. #define ENABLE_VIDEO_MODE 0
  71. #define ENABLE_CMD_MODE BIT(0)
  72. #define DSI_VID_MODE_CFG 0x38
  73. #define ENABLE_LOW_POWER (0x3f << 8)
  74. #define ENABLE_LOW_POWER_MASK (0x3f << 8)
  75. #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
  76. #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
  77. #define VID_MODE_TYPE_BURST 0x2
  78. #define VID_MODE_TYPE_MASK 0x3
  79. #define DSI_VID_PKT_SIZE 0x3c
  80. #define VID_PKT_SIZE(p) ((p) & 0x3fff)
  81. #define DSI_VID_NUM_CHUNKS 0x40
  82. #define VID_NUM_CHUNKS(c) ((c) & 0x1fff)
  83. #define DSI_VID_NULL_SIZE 0x44
  84. #define VID_NULL_SIZE(b) ((b) & 0x1fff)
  85. #define DSI_VID_HSA_TIME 0x48
  86. #define DSI_VID_HBP_TIME 0x4c
  87. #define DSI_VID_HLINE_TIME 0x50
  88. #define DSI_VID_VSA_LINES 0x54
  89. #define DSI_VID_VBP_LINES 0x58
  90. #define DSI_VID_VFP_LINES 0x5c
  91. #define DSI_VID_VACTIVE_LINES 0x60
  92. #define DSI_EDPI_CMD_SIZE 0x64
  93. #define DSI_CMD_MODE_CFG 0x68
  94. #define MAX_RD_PKT_SIZE_LP BIT(24)
  95. #define DCS_LW_TX_LP BIT(19)
  96. #define DCS_SR_0P_TX_LP BIT(18)
  97. #define DCS_SW_1P_TX_LP BIT(17)
  98. #define DCS_SW_0P_TX_LP BIT(16)
  99. #define GEN_LW_TX_LP BIT(14)
  100. #define GEN_SR_2P_TX_LP BIT(13)
  101. #define GEN_SR_1P_TX_LP BIT(12)
  102. #define GEN_SR_0P_TX_LP BIT(11)
  103. #define GEN_SW_2P_TX_LP BIT(10)
  104. #define GEN_SW_1P_TX_LP BIT(9)
  105. #define GEN_SW_0P_TX_LP BIT(8)
  106. #define ACK_RQST_EN BIT(1)
  107. #define TEAR_FX_EN BIT(0)
  108. #define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
  109. DCS_LW_TX_LP | \
  110. DCS_SR_0P_TX_LP | \
  111. DCS_SW_1P_TX_LP | \
  112. DCS_SW_0P_TX_LP | \
  113. GEN_LW_TX_LP | \
  114. GEN_SR_2P_TX_LP | \
  115. GEN_SR_1P_TX_LP | \
  116. GEN_SR_0P_TX_LP | \
  117. GEN_SW_2P_TX_LP | \
  118. GEN_SW_1P_TX_LP | \
  119. GEN_SW_0P_TX_LP)
  120. #define DSI_GEN_HDR 0x6c
  121. #define DSI_GEN_PLD_DATA 0x70
  122. #define DSI_CMD_PKT_STATUS 0x74
  123. #define GEN_RD_CMD_BUSY BIT(6)
  124. #define GEN_PLD_R_FULL BIT(5)
  125. #define GEN_PLD_R_EMPTY BIT(4)
  126. #define GEN_PLD_W_FULL BIT(3)
  127. #define GEN_PLD_W_EMPTY BIT(2)
  128. #define GEN_CMD_FULL BIT(1)
  129. #define GEN_CMD_EMPTY BIT(0)
  130. #define DSI_TO_CNT_CFG 0x78
  131. #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
  132. #define LPRX_TO_CNT(p) ((p) & 0xffff)
  133. #define DSI_HS_RD_TO_CNT 0x7c
  134. #define DSI_LP_RD_TO_CNT 0x80
  135. #define DSI_HS_WR_TO_CNT 0x84
  136. #define DSI_LP_WR_TO_CNT 0x88
  137. #define DSI_BTA_TO_CNT 0x8c
  138. #define DSI_LPCLK_CTRL 0x94
  139. #define AUTO_CLKLANE_CTRL BIT(1)
  140. #define PHY_TXREQUESTCLKHS BIT(0)
  141. #define DSI_PHY_TMR_LPCLK_CFG 0x98
  142. #define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
  143. #define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
  144. #define DSI_PHY_TMR_CFG 0x9c
  145. #define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
  146. #define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
  147. #define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
  148. #define PHY_HS2LP_TIME_V131(lbcc) (((lbcc) & 0x3ff) << 16)
  149. #define PHY_LP2HS_TIME_V131(lbcc) ((lbcc) & 0x3ff)
  150. #define DSI_PHY_RSTZ 0xa0
  151. #define PHY_DISFORCEPLL 0
  152. #define PHY_ENFORCEPLL BIT(3)
  153. #define PHY_DISABLECLK 0
  154. #define PHY_ENABLECLK BIT(2)
  155. #define PHY_RSTZ 0
  156. #define PHY_UNRSTZ BIT(1)
  157. #define PHY_SHUTDOWNZ 0
  158. #define PHY_UNSHUTDOWNZ BIT(0)
  159. #define DSI_PHY_IF_CFG 0xa4
  160. #define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
  161. #define N_LANES(n) (((n) - 1) & 0x3)
  162. #define DSI_PHY_ULPS_CTRL 0xa8
  163. #define DSI_PHY_TX_TRIGGERS 0xac
  164. #define DSI_PHY_STATUS 0xb0
  165. #define PHY_STOP_STATE_CLK_LANE BIT(2)
  166. #define PHY_LOCK BIT(0)
  167. #define DSI_PHY_TST_CTRL0 0xb4
  168. #define PHY_TESTCLK BIT(1)
  169. #define PHY_UNTESTCLK 0
  170. #define PHY_TESTCLR BIT(0)
  171. #define PHY_UNTESTCLR 0
  172. #define DSI_PHY_TST_CTRL1 0xb8
  173. #define PHY_TESTEN BIT(16)
  174. #define PHY_UNTESTEN 0
  175. #define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
  176. #define PHY_TESTDIN(n) ((n) & 0xff)
  177. #define DSI_INT_ST0 0xbc
  178. #define DSI_INT_ST1 0xc0
  179. #define DSI_INT_MSK0 0xc4
  180. #define DSI_INT_MSK1 0xc8
  181. #define DSI_PHY_TMR_RD_CFG 0xf4
  182. #define MAX_RD_TIME_V131(lbcc) ((lbcc) & 0x7fff)
  183. #define PHY_STATUS_TIMEOUT_US 10000
  184. #define CMD_PKT_STATUS_TIMEOUT_US 20000
  185. struct dw_mipi_dsi {
  186. struct drm_bridge bridge;
  187. struct mipi_dsi_host dsi_host;
  188. struct drm_bridge *panel_bridge;
  189. struct device *dev;
  190. void __iomem *base;
  191. struct clk *pclk;
  192. unsigned int lane_mbps; /* per lane */
  193. u32 channel;
  194. u32 lanes;
  195. u32 format;
  196. unsigned long mode_flags;
  197. const struct dw_mipi_dsi_plat_data *plat_data;
  198. };
  199. /*
  200. * The controller should generate 2 frames before
  201. * preparing the peripheral.
  202. */
  203. static void dw_mipi_dsi_wait_for_two_frames(struct drm_display_mode *mode)
  204. {
  205. int refresh, two_frames;
  206. refresh = drm_mode_vrefresh(mode);
  207. two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
  208. msleep(two_frames);
  209. }
  210. static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
  211. {
  212. return container_of(host, struct dw_mipi_dsi, dsi_host);
  213. }
  214. static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
  215. {
  216. return container_of(bridge, struct dw_mipi_dsi, bridge);
  217. }
  218. static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
  219. {
  220. writel(val, dsi->base + reg);
  221. }
  222. static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
  223. {
  224. return readl(dsi->base + reg);
  225. }
  226. static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
  227. struct mipi_dsi_device *device)
  228. {
  229. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  230. struct drm_bridge *bridge;
  231. struct drm_panel *panel;
  232. int ret;
  233. if (device->lanes > dsi->plat_data->max_data_lanes) {
  234. dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
  235. device->lanes);
  236. return -EINVAL;
  237. }
  238. dsi->lanes = device->lanes;
  239. dsi->channel = device->channel;
  240. dsi->format = device->format;
  241. dsi->mode_flags = device->mode_flags;
  242. ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, 0,
  243. &panel, &bridge);
  244. if (ret)
  245. return ret;
  246. if (panel) {
  247. bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DSI);
  248. if (IS_ERR(bridge))
  249. return PTR_ERR(bridge);
  250. }
  251. dsi->panel_bridge = bridge;
  252. drm_bridge_add(&dsi->bridge);
  253. return 0;
  254. }
  255. static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
  256. struct mipi_dsi_device *device)
  257. {
  258. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  259. drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
  260. drm_bridge_remove(&dsi->bridge);
  261. return 0;
  262. }
  263. static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
  264. const struct mipi_dsi_msg *msg)
  265. {
  266. bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
  267. u32 val = 0;
  268. if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
  269. val |= ACK_RQST_EN;
  270. if (lpm)
  271. val |= CMD_MODE_ALL_LP;
  272. dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
  273. dsi_write(dsi, DSI_CMD_MODE_CFG, val);
  274. }
  275. static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
  276. {
  277. int ret;
  278. u32 val, mask;
  279. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  280. val, !(val & GEN_CMD_FULL), 1000,
  281. CMD_PKT_STATUS_TIMEOUT_US);
  282. if (ret) {
  283. dev_err(dsi->dev, "failed to get available command FIFO\n");
  284. return ret;
  285. }
  286. dsi_write(dsi, DSI_GEN_HDR, hdr_val);
  287. mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
  288. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  289. val, (val & mask) == mask,
  290. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  291. if (ret) {
  292. dev_err(dsi->dev, "failed to write command FIFO\n");
  293. return ret;
  294. }
  295. return 0;
  296. }
  297. static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
  298. const struct mipi_dsi_packet *packet)
  299. {
  300. const u8 *tx_buf = packet->payload;
  301. int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
  302. __le32 word;
  303. u32 val;
  304. while (len) {
  305. if (len < pld_data_bytes) {
  306. word = 0;
  307. memcpy(&word, tx_buf, len);
  308. dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
  309. len = 0;
  310. } else {
  311. memcpy(&word, tx_buf, pld_data_bytes);
  312. dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
  313. tx_buf += pld_data_bytes;
  314. len -= pld_data_bytes;
  315. }
  316. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  317. val, !(val & GEN_PLD_W_FULL), 1000,
  318. CMD_PKT_STATUS_TIMEOUT_US);
  319. if (ret) {
  320. dev_err(dsi->dev,
  321. "failed to get available write payload FIFO\n");
  322. return ret;
  323. }
  324. }
  325. word = 0;
  326. memcpy(&word, packet->header, sizeof(packet->header));
  327. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
  328. }
  329. static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
  330. const struct mipi_dsi_msg *msg)
  331. {
  332. int i, j, ret, len = msg->rx_len;
  333. u8 *buf = msg->rx_buf;
  334. u32 val;
  335. /* Wait end of the read operation */
  336. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  337. val, !(val & GEN_RD_CMD_BUSY),
  338. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  339. if (ret) {
  340. dev_err(dsi->dev, "Timeout during read operation\n");
  341. return ret;
  342. }
  343. for (i = 0; i < len; i += 4) {
  344. /* Read fifo must not be empty before all bytes are read */
  345. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  346. val, !(val & GEN_PLD_R_EMPTY),
  347. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  348. if (ret) {
  349. dev_err(dsi->dev, "Read payload FIFO is empty\n");
  350. return ret;
  351. }
  352. val = dsi_read(dsi, DSI_GEN_PLD_DATA);
  353. for (j = 0; j < 4 && j + i < len; j++)
  354. buf[i + j] = val >> (8 * j);
  355. }
  356. return ret;
  357. }
  358. static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
  359. const struct mipi_dsi_msg *msg)
  360. {
  361. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  362. struct mipi_dsi_packet packet;
  363. int ret, nb_bytes;
  364. ret = mipi_dsi_create_packet(&packet, msg);
  365. if (ret) {
  366. dev_err(dsi->dev, "failed to create packet: %d\n", ret);
  367. return ret;
  368. }
  369. dw_mipi_message_config(dsi, msg);
  370. ret = dw_mipi_dsi_write(dsi, &packet);
  371. if (ret)
  372. return ret;
  373. if (msg->rx_buf && msg->rx_len) {
  374. ret = dw_mipi_dsi_read(dsi, msg);
  375. if (ret)
  376. return ret;
  377. nb_bytes = msg->rx_len;
  378. } else {
  379. nb_bytes = packet.size;
  380. }
  381. return nb_bytes;
  382. }
  383. static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
  384. .attach = dw_mipi_dsi_host_attach,
  385. .detach = dw_mipi_dsi_host_detach,
  386. .transfer = dw_mipi_dsi_host_transfer,
  387. };
  388. static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
  389. {
  390. u32 val;
  391. /*
  392. * TODO dw drv improvements
  393. * enabling low power is panel-dependent, we should use the
  394. * panel configuration here...
  395. */
  396. val = ENABLE_LOW_POWER;
  397. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  398. val |= VID_MODE_TYPE_BURST;
  399. else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  400. val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
  401. else
  402. val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
  403. dsi_write(dsi, DSI_VID_MODE_CFG, val);
  404. }
  405. static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
  406. unsigned long mode_flags)
  407. {
  408. dsi_write(dsi, DSI_PWR_UP, RESET);
  409. if (mode_flags & MIPI_DSI_MODE_VIDEO) {
  410. dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
  411. dw_mipi_dsi_video_mode_config(dsi);
  412. dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
  413. } else {
  414. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  415. }
  416. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  417. }
  418. static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
  419. {
  420. dsi_write(dsi, DSI_PWR_UP, RESET);
  421. dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
  422. }
  423. static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
  424. {
  425. /*
  426. * The maximum permitted escape clock is 20MHz and it is derived from
  427. * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
  428. *
  429. * (lane_mbps >> 3) / esc_clk_division < 20
  430. * which is:
  431. * (lane_mbps >> 3) / 20 > esc_clk_division
  432. */
  433. u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
  434. dsi_write(dsi, DSI_PWR_UP, RESET);
  435. /*
  436. * TODO dw drv improvements
  437. * timeout clock division should be computed with the
  438. * high speed transmission counter timeout and byte lane...
  439. */
  440. dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
  441. TX_ESC_CLK_DIVISION(esc_clk_division));
  442. }
  443. static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
  444. struct drm_display_mode *mode)
  445. {
  446. u32 val = 0, color = 0;
  447. switch (dsi->format) {
  448. case MIPI_DSI_FMT_RGB888:
  449. color = DPI_COLOR_CODING_24BIT;
  450. break;
  451. case MIPI_DSI_FMT_RGB666:
  452. color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
  453. break;
  454. case MIPI_DSI_FMT_RGB666_PACKED:
  455. color = DPI_COLOR_CODING_18BIT_1;
  456. break;
  457. case MIPI_DSI_FMT_RGB565:
  458. color = DPI_COLOR_CODING_16BIT_1;
  459. break;
  460. }
  461. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  462. val |= VSYNC_ACTIVE_LOW;
  463. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  464. val |= HSYNC_ACTIVE_LOW;
  465. dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
  466. dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
  467. dsi_write(dsi, DSI_DPI_CFG_POL, val);
  468. /*
  469. * TODO dw drv improvements
  470. * largest packet sizes during hfp or during vsa/vpb/vfp
  471. * should be computed according to byte lane, lane number and only
  472. * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
  473. */
  474. dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
  475. | INVACT_LPCMD_TIME(4));
  476. }
  477. static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
  478. {
  479. dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
  480. }
  481. static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
  482. struct drm_display_mode *mode)
  483. {
  484. /*
  485. * TODO dw drv improvements
  486. * only burst mode is supported here. For non-burst video modes,
  487. * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
  488. * DSI_VNPCR.NPSIZE... especially because this driver supports
  489. * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
  490. */
  491. dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
  492. }
  493. static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
  494. {
  495. /*
  496. * TODO dw drv improvements
  497. * compute high speed transmission counter timeout according
  498. * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
  499. */
  500. dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
  501. /*
  502. * TODO dw drv improvements
  503. * the Bus-Turn-Around Timeout Counter should be computed
  504. * according to byte lane...
  505. */
  506. dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
  507. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  508. }
  509. /* Get lane byte clock cycles. */
  510. static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
  511. struct drm_display_mode *mode,
  512. u32 hcomponent)
  513. {
  514. u32 frac, lbcc;
  515. lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
  516. frac = lbcc % mode->clock;
  517. lbcc = lbcc / mode->clock;
  518. if (frac)
  519. lbcc++;
  520. return lbcc;
  521. }
  522. static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
  523. struct drm_display_mode *mode)
  524. {
  525. u32 htotal, hsa, hbp, lbcc;
  526. htotal = mode->htotal;
  527. hsa = mode->hsync_end - mode->hsync_start;
  528. hbp = mode->htotal - mode->hsync_end;
  529. /*
  530. * TODO dw drv improvements
  531. * computations below may be improved...
  532. */
  533. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
  534. dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
  535. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
  536. dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
  537. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
  538. dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
  539. }
  540. static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
  541. struct drm_display_mode *mode)
  542. {
  543. u32 vactive, vsa, vfp, vbp;
  544. vactive = mode->vdisplay;
  545. vsa = mode->vsync_end - mode->vsync_start;
  546. vfp = mode->vsync_start - mode->vdisplay;
  547. vbp = mode->vtotal - mode->vsync_end;
  548. dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
  549. dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
  550. dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
  551. dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
  552. }
  553. static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
  554. {
  555. u32 hw_version;
  556. /*
  557. * TODO dw drv improvements
  558. * data & clock lane timers should be computed according to panel
  559. * blankings and to the automatic clock lane control mode...
  560. * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
  561. * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
  562. */
  563. hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
  564. if (hw_version >= HWVER_131) {
  565. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME_V131(0x40) |
  566. PHY_LP2HS_TIME_V131(0x40));
  567. dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
  568. } else {
  569. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40) |
  570. PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
  571. }
  572. dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
  573. | PHY_CLKLP2HS_TIME(0x40));
  574. }
  575. static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
  576. {
  577. /*
  578. * TODO dw drv improvements
  579. * stop wait time should be the maximum between host dsi
  580. * and panel stop wait times
  581. */
  582. dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
  583. N_LANES(dsi->lanes));
  584. }
  585. static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
  586. {
  587. /* Clear PHY state */
  588. dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
  589. | PHY_RSTZ | PHY_SHUTDOWNZ);
  590. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
  591. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
  592. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
  593. }
  594. static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
  595. {
  596. u32 val;
  597. int ret;
  598. dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
  599. PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
  600. ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
  601. val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
  602. if (ret)
  603. DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
  604. ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
  605. val, val & PHY_STOP_STATE_CLK_LANE, 1000,
  606. PHY_STATUS_TIMEOUT_US);
  607. if (ret)
  608. DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
  609. }
  610. static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
  611. {
  612. dsi_read(dsi, DSI_INT_ST0);
  613. dsi_read(dsi, DSI_INT_ST1);
  614. dsi_write(dsi, DSI_INT_MSK0, 0);
  615. dsi_write(dsi, DSI_INT_MSK1, 0);
  616. }
  617. static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge)
  618. {
  619. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  620. /*
  621. * Switch to command mode before panel-bridge post_disable &
  622. * panel unprepare.
  623. * Note: panel-bridge disable & panel disable has been called
  624. * before by the drm framework.
  625. */
  626. dw_mipi_dsi_set_mode(dsi, 0);
  627. /*
  628. * TODO Only way found to call panel-bridge post_disable &
  629. * panel unprepare before the dsi "final" disable...
  630. * This needs to be fixed in the drm_bridge framework and the API
  631. * needs to be updated to manage our own call chains...
  632. */
  633. dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
  634. dw_mipi_dsi_disable(dsi);
  635. clk_disable_unprepare(dsi->pclk);
  636. pm_runtime_put(dsi->dev);
  637. }
  638. static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
  639. struct drm_display_mode *mode,
  640. struct drm_display_mode *adjusted_mode)
  641. {
  642. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  643. const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
  644. void *priv_data = dsi->plat_data->priv_data;
  645. int ret;
  646. clk_prepare_enable(dsi->pclk);
  647. ret = phy_ops->get_lane_mbps(priv_data, mode, dsi->mode_flags,
  648. dsi->lanes, dsi->format, &dsi->lane_mbps);
  649. if (ret)
  650. DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
  651. pm_runtime_get_sync(dsi->dev);
  652. dw_mipi_dsi_init(dsi);
  653. dw_mipi_dsi_dpi_config(dsi, mode);
  654. dw_mipi_dsi_packet_handler_config(dsi);
  655. dw_mipi_dsi_video_mode_config(dsi);
  656. dw_mipi_dsi_video_packet_config(dsi, mode);
  657. dw_mipi_dsi_command_mode_config(dsi);
  658. dw_mipi_dsi_line_timer_config(dsi, mode);
  659. dw_mipi_dsi_vertical_timing_config(dsi, mode);
  660. dw_mipi_dsi_dphy_init(dsi);
  661. dw_mipi_dsi_dphy_timing_config(dsi);
  662. dw_mipi_dsi_dphy_interface_config(dsi);
  663. dw_mipi_dsi_clear_err(dsi);
  664. ret = phy_ops->init(priv_data);
  665. if (ret)
  666. DRM_DEBUG_DRIVER("Phy init() failed\n");
  667. dw_mipi_dsi_dphy_enable(dsi);
  668. dw_mipi_dsi_wait_for_two_frames(mode);
  669. /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
  670. dw_mipi_dsi_set_mode(dsi, 0);
  671. }
  672. static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge)
  673. {
  674. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  675. /* Switch to video mode for panel-bridge enable & panel enable */
  676. dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
  677. }
  678. static enum drm_mode_status
  679. dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
  680. const struct drm_display_mode *mode)
  681. {
  682. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  683. const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
  684. enum drm_mode_status mode_status = MODE_OK;
  685. if (pdata->mode_valid)
  686. mode_status = pdata->mode_valid(pdata->priv_data, mode);
  687. return mode_status;
  688. }
  689. static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge)
  690. {
  691. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  692. if (!bridge->encoder) {
  693. DRM_ERROR("Parent encoder object not found\n");
  694. return -ENODEV;
  695. }
  696. /* Set the encoder type as caller does not know it */
  697. bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
  698. /* Attach the panel-bridge to the dsi bridge */
  699. return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge);
  700. }
  701. static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
  702. .mode_set = dw_mipi_dsi_bridge_mode_set,
  703. .enable = dw_mipi_dsi_bridge_enable,
  704. .post_disable = dw_mipi_dsi_bridge_post_disable,
  705. .mode_valid = dw_mipi_dsi_bridge_mode_valid,
  706. .attach = dw_mipi_dsi_bridge_attach,
  707. };
  708. static struct dw_mipi_dsi *
  709. __dw_mipi_dsi_probe(struct platform_device *pdev,
  710. const struct dw_mipi_dsi_plat_data *plat_data)
  711. {
  712. struct device *dev = &pdev->dev;
  713. struct reset_control *apb_rst;
  714. struct dw_mipi_dsi *dsi;
  715. struct resource *res;
  716. int ret;
  717. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  718. if (!dsi)
  719. return ERR_PTR(-ENOMEM);
  720. dsi->dev = dev;
  721. dsi->plat_data = plat_data;
  722. if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps) {
  723. DRM_ERROR("Phy not properly configured\n");
  724. return ERR_PTR(-ENODEV);
  725. }
  726. if (!plat_data->base) {
  727. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  728. if (!res)
  729. return ERR_PTR(-ENODEV);
  730. dsi->base = devm_ioremap_resource(dev, res);
  731. if (IS_ERR(dsi->base))
  732. return ERR_PTR(-ENODEV);
  733. } else {
  734. dsi->base = plat_data->base;
  735. }
  736. dsi->pclk = devm_clk_get(dev, "pclk");
  737. if (IS_ERR(dsi->pclk)) {
  738. ret = PTR_ERR(dsi->pclk);
  739. dev_err(dev, "Unable to get pclk: %d\n", ret);
  740. return ERR_PTR(ret);
  741. }
  742. /*
  743. * Note that the reset was not defined in the initial device tree, so
  744. * we have to be prepared for it not being found.
  745. */
  746. apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
  747. if (IS_ERR(apb_rst)) {
  748. ret = PTR_ERR(apb_rst);
  749. if (ret != -EPROBE_DEFER)
  750. dev_err(dev, "Unable to get reset control: %d\n", ret);
  751. return ERR_PTR(ret);
  752. }
  753. if (apb_rst) {
  754. ret = clk_prepare_enable(dsi->pclk);
  755. if (ret) {
  756. dev_err(dev, "%s: Failed to enable pclk\n", __func__);
  757. return ERR_PTR(ret);
  758. }
  759. reset_control_assert(apb_rst);
  760. usleep_range(10, 20);
  761. reset_control_deassert(apb_rst);
  762. clk_disable_unprepare(dsi->pclk);
  763. }
  764. pm_runtime_enable(dev);
  765. dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
  766. dsi->dsi_host.dev = dev;
  767. ret = mipi_dsi_host_register(&dsi->dsi_host);
  768. if (ret) {
  769. dev_err(dev, "Failed to register MIPI host: %d\n", ret);
  770. return ERR_PTR(ret);
  771. }
  772. dsi->bridge.driver_private = dsi;
  773. dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
  774. #ifdef CONFIG_OF
  775. dsi->bridge.of_node = pdev->dev.of_node;
  776. #endif
  777. return dsi;
  778. }
  779. static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
  780. {
  781. pm_runtime_disable(dsi->dev);
  782. }
  783. /*
  784. * Probe/remove API, used from platforms based on the DRM bridge API.
  785. */
  786. struct dw_mipi_dsi *
  787. dw_mipi_dsi_probe(struct platform_device *pdev,
  788. const struct dw_mipi_dsi_plat_data *plat_data)
  789. {
  790. return __dw_mipi_dsi_probe(pdev, plat_data);
  791. }
  792. EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
  793. void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
  794. {
  795. mipi_dsi_host_unregister(&dsi->dsi_host);
  796. __dw_mipi_dsi_remove(dsi);
  797. }
  798. EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
  799. /*
  800. * Bind/unbind API, used from platforms based on the component framework.
  801. */
  802. struct dw_mipi_dsi *
  803. dw_mipi_dsi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
  804. const struct dw_mipi_dsi_plat_data *plat_data)
  805. {
  806. struct dw_mipi_dsi *dsi;
  807. int ret;
  808. dsi = __dw_mipi_dsi_probe(pdev, plat_data);
  809. if (IS_ERR(dsi))
  810. return dsi;
  811. ret = drm_bridge_attach(encoder, &dsi->bridge, NULL);
  812. if (ret) {
  813. dw_mipi_dsi_remove(dsi);
  814. DRM_ERROR("Failed to initialize bridge with drm\n");
  815. return ERR_PTR(ret);
  816. }
  817. return dsi;
  818. }
  819. EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
  820. void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
  821. {
  822. __dw_mipi_dsi_remove(dsi);
  823. }
  824. EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
  825. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  826. MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
  827. MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
  828. MODULE_LICENSE("GPL");
  829. MODULE_ALIAS("platform:dw-mipi-dsi");