dw-mipi-dsi.c 26 KB

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