dw-mipi-dsi.c 25 KB

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