dw_mipi_dsi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016, Fuzhou Rockchip Electronics Co., Ltd
  4. * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
  5. * Author(s): Philippe Cornu <philippe.cornu@st.com> for STMicroelectronics.
  6. * Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
  7. *
  8. * This generic Synopsys DesignWare MIPI DSI host driver is inspired from
  9. * the Linux Kernel driver drivers/gpu/drm/bridge/synopsys/dw-mipi-dsi.c.
  10. */
  11. #include <common.h>
  12. #include <clk.h>
  13. #include <dsi_host.h>
  14. #include <dm.h>
  15. #include <errno.h>
  16. #include <panel.h>
  17. #include <video.h>
  18. #include <asm/io.h>
  19. #include <asm/arch/gpio.h>
  20. #include <dm/device-internal.h>
  21. #include <dm/device_compat.h>
  22. #include <linux/bitops.h>
  23. #include <linux/delay.h>
  24. #include <linux/iopoll.h>
  25. #include <video_bridge.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. #define MSEC_PER_SEC 1000
  182. struct dw_mipi_dsi {
  183. struct mipi_dsi_host dsi_host;
  184. struct mipi_dsi_device *device;
  185. void __iomem *base;
  186. unsigned int lane_mbps; /* per lane */
  187. u32 channel;
  188. unsigned int max_data_lanes;
  189. const struct mipi_dsi_phy_ops *phy_ops;
  190. };
  191. static int dsi_mode_vrefresh(struct display_timing *timings)
  192. {
  193. int refresh = 0;
  194. unsigned int calc_val;
  195. u32 htotal = timings->hactive.typ + timings->hfront_porch.typ +
  196. timings->hback_porch.typ + timings->hsync_len.typ;
  197. u32 vtotal = timings->vactive.typ + timings->vfront_porch.typ +
  198. timings->vback_porch.typ + timings->vsync_len.typ;
  199. if (htotal > 0 && vtotal > 0) {
  200. calc_val = timings->pixelclock.typ;
  201. calc_val /= htotal;
  202. refresh = (calc_val + vtotal / 2) / vtotal;
  203. }
  204. return refresh;
  205. }
  206. /*
  207. * The controller should generate 2 frames before
  208. * preparing the peripheral.
  209. */
  210. static void dw_mipi_dsi_wait_for_two_frames(struct display_timing *timings)
  211. {
  212. int refresh, two_frames;
  213. refresh = dsi_mode_vrefresh(timings);
  214. two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
  215. mdelay(two_frames);
  216. }
  217. static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
  218. {
  219. return container_of(host, struct dw_mipi_dsi, dsi_host);
  220. }
  221. static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
  222. {
  223. writel(val, dsi->base + reg);
  224. }
  225. static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
  226. {
  227. return readl(dsi->base + reg);
  228. }
  229. static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
  230. struct mipi_dsi_device *device)
  231. {
  232. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  233. if (device->lanes > dsi->max_data_lanes) {
  234. dev_err(device->dev,
  235. "the number of data lanes(%u) is too many\n",
  236. device->lanes);
  237. return -EINVAL;
  238. }
  239. dsi->channel = device->channel;
  240. return 0;
  241. }
  242. static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
  243. const struct mipi_dsi_msg *msg)
  244. {
  245. bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
  246. u32 val = 0;
  247. if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
  248. val |= ACK_RQST_EN;
  249. if (lpm)
  250. val |= CMD_MODE_ALL_LP;
  251. dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
  252. dsi_write(dsi, DSI_CMD_MODE_CFG, val);
  253. }
  254. static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
  255. {
  256. int ret;
  257. u32 val, mask;
  258. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  259. val, !(val & GEN_CMD_FULL),
  260. CMD_PKT_STATUS_TIMEOUT_US);
  261. if (ret) {
  262. dev_err(dsi->dsi_host.dev,
  263. "failed to get available command FIFO\n");
  264. return ret;
  265. }
  266. dsi_write(dsi, DSI_GEN_HDR, hdr_val);
  267. mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
  268. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  269. val, (val & mask) == mask,
  270. CMD_PKT_STATUS_TIMEOUT_US);
  271. if (ret) {
  272. dev_err(dsi->dsi_host.dev, "failed to write command FIFO\n");
  273. return ret;
  274. }
  275. return 0;
  276. }
  277. static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
  278. const struct mipi_dsi_packet *packet)
  279. {
  280. const u8 *tx_buf = packet->payload;
  281. int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
  282. __le32 word;
  283. u32 val;
  284. while (len) {
  285. if (len < pld_data_bytes) {
  286. word = 0;
  287. memcpy(&word, tx_buf, len);
  288. dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
  289. len = 0;
  290. } else {
  291. memcpy(&word, tx_buf, pld_data_bytes);
  292. dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
  293. tx_buf += pld_data_bytes;
  294. len -= pld_data_bytes;
  295. }
  296. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  297. val, !(val & GEN_PLD_W_FULL),
  298. CMD_PKT_STATUS_TIMEOUT_US);
  299. if (ret) {
  300. dev_err(dsi->dsi_host.dev,
  301. "failed to get available write payload FIFO\n");
  302. return ret;
  303. }
  304. }
  305. word = 0;
  306. memcpy(&word, packet->header, sizeof(packet->header));
  307. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
  308. }
  309. static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
  310. const struct mipi_dsi_msg *msg)
  311. {
  312. int i, j, ret, len = msg->rx_len;
  313. u8 *buf = msg->rx_buf;
  314. u32 val;
  315. /* Wait end of the read operation */
  316. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  317. val, !(val & GEN_RD_CMD_BUSY),
  318. CMD_PKT_STATUS_TIMEOUT_US);
  319. if (ret) {
  320. dev_err(dsi->dsi_host.dev, "Timeout during read operation\n");
  321. return ret;
  322. }
  323. for (i = 0; i < len; i += 4) {
  324. /* Read fifo must not be empty before all bytes are read */
  325. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  326. val, !(val & GEN_PLD_R_EMPTY),
  327. CMD_PKT_STATUS_TIMEOUT_US);
  328. if (ret) {
  329. dev_err(dsi->dsi_host.dev,
  330. "Read payload FIFO is empty\n");
  331. return ret;
  332. }
  333. val = dsi_read(dsi, DSI_GEN_PLD_DATA);
  334. for (j = 0; j < 4 && j + i < len; j++)
  335. buf[i + j] = val >> (8 * j);
  336. }
  337. return ret;
  338. }
  339. static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
  340. const struct mipi_dsi_msg *msg)
  341. {
  342. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  343. struct mipi_dsi_packet packet;
  344. int ret, nb_bytes;
  345. ret = mipi_dsi_create_packet(&packet, msg);
  346. if (ret) {
  347. dev_err(host->dev, "failed to create packet: %d\n", ret);
  348. return ret;
  349. }
  350. dw_mipi_message_config(dsi, msg);
  351. ret = dw_mipi_dsi_write(dsi, &packet);
  352. if (ret)
  353. return ret;
  354. if (msg->rx_buf && msg->rx_len) {
  355. ret = dw_mipi_dsi_read(dsi, msg);
  356. if (ret)
  357. return ret;
  358. nb_bytes = msg->rx_len;
  359. } else {
  360. nb_bytes = packet.size;
  361. }
  362. return nb_bytes;
  363. }
  364. static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
  365. .attach = dw_mipi_dsi_host_attach,
  366. .transfer = dw_mipi_dsi_host_transfer,
  367. };
  368. static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
  369. {
  370. struct mipi_dsi_device *device = dsi->device;
  371. u32 val;
  372. /*
  373. * TODO dw drv improvements
  374. * enabling low power is panel-dependent, we should use the
  375. * panel configuration here...
  376. */
  377. val = ENABLE_LOW_POWER;
  378. if (device->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  379. val |= VID_MODE_TYPE_BURST;
  380. else if (device->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  381. val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
  382. else
  383. val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
  384. dsi_write(dsi, DSI_VID_MODE_CFG, val);
  385. }
  386. static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
  387. unsigned long mode_flags)
  388. {
  389. const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
  390. dsi_write(dsi, DSI_PWR_UP, RESET);
  391. if (mode_flags & MIPI_DSI_MODE_VIDEO) {
  392. dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
  393. dw_mipi_dsi_video_mode_config(dsi);
  394. dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
  395. } else {
  396. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  397. }
  398. if (phy_ops->post_set_mode)
  399. phy_ops->post_set_mode(dsi->device, mode_flags);
  400. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  401. }
  402. static void dw_mipi_dsi_init_pll(struct dw_mipi_dsi *dsi)
  403. {
  404. const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
  405. unsigned int esc_rate;
  406. u32 esc_clk_division;
  407. /*
  408. * The maximum permitted escape clock is 20MHz and it is derived from
  409. * lanebyteclk, which is running at "lane_mbps / 8".
  410. */
  411. if (phy_ops->get_esc_clk_rate)
  412. phy_ops->get_esc_clk_rate(dsi->device, &esc_rate);
  413. else
  414. esc_rate = 20; /* Default to 20MHz */
  415. /*
  416. * We want:
  417. *
  418. * (lane_mbps >> 3) / esc_clk_division < X
  419. * which is:
  420. * (lane_mbps >> 3) / X > esc_clk_division
  421. */
  422. esc_clk_division = (dsi->lane_mbps >> 3) / esc_rate + 1;
  423. dsi_write(dsi, DSI_PWR_UP, RESET);
  424. /*
  425. * TODO dw drv improvements
  426. * timeout clock division should be computed with the
  427. * high speed transmission counter timeout and byte lane...
  428. */
  429. dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
  430. TX_ESC_CLK_DIVISION(esc_clk_division));
  431. }
  432. static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
  433. struct display_timing *timings)
  434. {
  435. struct mipi_dsi_device *device = dsi->device;
  436. u32 val = 0, color = 0;
  437. switch (device->format) {
  438. case MIPI_DSI_FMT_RGB888:
  439. color = DPI_COLOR_CODING_24BIT;
  440. break;
  441. case MIPI_DSI_FMT_RGB666:
  442. color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
  443. break;
  444. case MIPI_DSI_FMT_RGB666_PACKED:
  445. color = DPI_COLOR_CODING_18BIT_1;
  446. break;
  447. case MIPI_DSI_FMT_RGB565:
  448. color = DPI_COLOR_CODING_16BIT_1;
  449. break;
  450. }
  451. if (device->mode_flags & DISPLAY_FLAGS_VSYNC_HIGH)
  452. val |= VSYNC_ACTIVE_LOW;
  453. if (device->mode_flags & DISPLAY_FLAGS_HSYNC_HIGH)
  454. val |= HSYNC_ACTIVE_LOW;
  455. dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
  456. dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
  457. dsi_write(dsi, DSI_DPI_CFG_POL, val);
  458. /*
  459. * TODO dw drv improvements
  460. * largest packet sizes during hfp or during vsa/vpb/vfp
  461. * should be computed according to byte lane, lane number and only
  462. * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
  463. */
  464. dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
  465. | INVACT_LPCMD_TIME(4));
  466. }
  467. static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
  468. {
  469. dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
  470. }
  471. static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
  472. struct display_timing *timings)
  473. {
  474. /*
  475. * TODO dw drv improvements
  476. * only burst mode is supported here. For non-burst video modes,
  477. * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
  478. * DSI_VNPCR.NPSIZE... especially because this driver supports
  479. * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
  480. */
  481. dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(timings->hactive.typ));
  482. }
  483. static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
  484. {
  485. const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
  486. /*
  487. * TODO dw drv improvements
  488. * compute high speed transmission counter timeout according
  489. * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
  490. */
  491. dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
  492. /*
  493. * TODO dw drv improvements
  494. * the Bus-Turn-Around Timeout Counter should be computed
  495. * according to byte lane...
  496. */
  497. dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
  498. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  499. if (phy_ops->post_set_mode)
  500. phy_ops->post_set_mode(dsi->device, 0);
  501. }
  502. /* Get lane byte clock cycles. */
  503. static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
  504. struct display_timing *timings,
  505. u32 hcomponent)
  506. {
  507. u32 frac, lbcc;
  508. lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
  509. frac = lbcc % (timings->pixelclock.typ / 1000);
  510. lbcc = lbcc / (timings->pixelclock.typ / 1000);
  511. if (frac)
  512. lbcc++;
  513. return lbcc;
  514. }
  515. static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
  516. struct display_timing *timings)
  517. {
  518. u32 htotal, hsa, hbp, lbcc;
  519. htotal = timings->hactive.typ + timings->hfront_porch.typ +
  520. timings->hback_porch.typ + timings->hsync_len.typ;
  521. hsa = timings->hback_porch.typ;
  522. hbp = timings->hsync_len.typ;
  523. /*
  524. * TODO dw drv improvements
  525. * computations below may be improved...
  526. */
  527. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, timings, htotal);
  528. dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
  529. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, timings, hsa);
  530. dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
  531. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, timings, hbp);
  532. dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
  533. }
  534. static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
  535. struct display_timing *timings)
  536. {
  537. u32 vactive, vsa, vfp, vbp;
  538. vactive = timings->vactive.typ;
  539. vsa = timings->vback_porch.typ;
  540. vfp = timings->vfront_porch.typ;
  541. vbp = timings->vsync_len.typ;
  542. dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
  543. dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
  544. dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
  545. dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
  546. }
  547. static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
  548. {
  549. const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
  550. struct mipi_dsi_phy_timing timing = {0x40, 0x40, 0x40, 0x40};
  551. u32 hw_version;
  552. if (phy_ops->get_timing)
  553. phy_ops->get_timing(dsi->device, dsi->lane_mbps, &timing);
  554. /*
  555. * TODO dw drv improvements
  556. * data & clock lane timers should be computed according to panel
  557. * blankings and to the automatic clock lane control mode...
  558. * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
  559. * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
  560. */
  561. hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
  562. if (hw_version >= HWVER_131) {
  563. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME_V131(timing.data_hs2lp) |
  564. PHY_LP2HS_TIME_V131(timing.data_lp2hs));
  565. dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
  566. } else {
  567. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(timing.data_hs2lp) |
  568. PHY_LP2HS_TIME(timing.data_lp2hs) | MAX_RD_TIME(10000));
  569. }
  570. dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(timing.clk_hs2lp)
  571. | PHY_CLKLP2HS_TIME(timing.clk_lp2hs));
  572. }
  573. static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
  574. {
  575. struct mipi_dsi_device *device = dsi->device;
  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(device->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, PHY_STATUS_TIMEOUT_US);
  601. if (ret)
  602. dev_dbg(dsi->dsi_host.dev,
  603. "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,
  606. PHY_STATUS_TIMEOUT_US);
  607. if (ret)
  608. dev_dbg(dsi->dsi_host.dev,
  609. "failed to wait phy clk lane stop state\n");
  610. }
  611. static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
  612. {
  613. dsi_read(dsi, DSI_INT_ST0);
  614. dsi_read(dsi, DSI_INT_ST1);
  615. dsi_write(dsi, DSI_INT_MSK0, 0);
  616. dsi_write(dsi, DSI_INT_MSK1, 0);
  617. }
  618. static void dw_mipi_dsi_bridge_set(struct dw_mipi_dsi *dsi,
  619. struct display_timing *timings)
  620. {
  621. const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
  622. struct mipi_dsi_device *device = dsi->device;
  623. int ret;
  624. ret = phy_ops->get_lane_mbps(dsi->device, timings, device->lanes,
  625. device->format, &dsi->lane_mbps);
  626. if (ret)
  627. dev_warn(dsi->dsi_host.dev, "Phy get_lane_mbps() failed\n");
  628. dw_mipi_dsi_init_pll(dsi);
  629. dw_mipi_dsi_dpi_config(dsi, timings);
  630. dw_mipi_dsi_packet_handler_config(dsi);
  631. dw_mipi_dsi_video_mode_config(dsi);
  632. dw_mipi_dsi_video_packet_config(dsi, timings);
  633. dw_mipi_dsi_command_mode_config(dsi);
  634. dw_mipi_dsi_line_timer_config(dsi, timings);
  635. dw_mipi_dsi_vertical_timing_config(dsi, timings);
  636. dw_mipi_dsi_dphy_init(dsi);
  637. dw_mipi_dsi_dphy_timing_config(dsi);
  638. dw_mipi_dsi_dphy_interface_config(dsi);
  639. dw_mipi_dsi_clear_err(dsi);
  640. ret = phy_ops->init(dsi->device);
  641. if (ret)
  642. dev_warn(dsi->dsi_host.dev, "Phy init() failed\n");
  643. dw_mipi_dsi_dphy_enable(dsi);
  644. dw_mipi_dsi_wait_for_two_frames(timings);
  645. /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
  646. dw_mipi_dsi_set_mode(dsi, 0);
  647. }
  648. static int dw_mipi_dsi_init(struct udevice *dev,
  649. struct mipi_dsi_device *device,
  650. struct display_timing *timings,
  651. unsigned int max_data_lanes,
  652. const struct mipi_dsi_phy_ops *phy_ops)
  653. {
  654. struct dw_mipi_dsi *dsi = dev_get_priv(dev);
  655. struct clk clk;
  656. int ret;
  657. if (!phy_ops->init || !phy_ops->get_lane_mbps) {
  658. dev_err(device->dev, "Phy not properly configured\n");
  659. return -ENODEV;
  660. }
  661. dsi->phy_ops = phy_ops;
  662. dsi->max_data_lanes = max_data_lanes;
  663. dsi->device = device;
  664. dsi->dsi_host.dev = (struct device *)dev;
  665. dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
  666. device->host = &dsi->dsi_host;
  667. dsi->base = (void *)dev_read_addr(device->dev);
  668. if ((fdt_addr_t)dsi->base == FDT_ADDR_T_NONE) {
  669. dev_err(device->dev, "dsi dt register address error\n");
  670. return -EINVAL;
  671. }
  672. ret = clk_get_by_name(device->dev, "px_clk", &clk);
  673. if (ret) {
  674. dev_err(device->dev, "peripheral clock get error %d\n", ret);
  675. return ret;
  676. }
  677. /* get the pixel clock set by the clock framework */
  678. timings->pixelclock.typ = clk_get_rate(&clk);
  679. dw_mipi_dsi_bridge_set(dsi, timings);
  680. return 0;
  681. }
  682. static int dw_mipi_dsi_enable(struct udevice *dev)
  683. {
  684. struct dw_mipi_dsi *dsi = dev_get_priv(dev);
  685. /* Switch to video mode for panel-bridge enable & panel enable */
  686. dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
  687. return 0;
  688. }
  689. struct dsi_host_ops dw_mipi_dsi_ops = {
  690. .init = dw_mipi_dsi_init,
  691. .enable = dw_mipi_dsi_enable,
  692. };
  693. static int dw_mipi_dsi_probe(struct udevice *dev)
  694. {
  695. return 0;
  696. }
  697. U_BOOT_DRIVER(dw_mipi_dsi) = {
  698. .name = "dw_mipi_dsi",
  699. .id = UCLASS_DSI_HOST,
  700. .probe = dw_mipi_dsi_probe,
  701. .ops = &dw_mipi_dsi_ops,
  702. .priv_auto = sizeof(struct dw_mipi_dsi),
  703. };
  704. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  705. MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
  706. MODULE_AUTHOR("Yannick Fertré <yannick.fertre@st.com>");
  707. MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
  708. MODULE_LICENSE("GPL");
  709. MODULE_ALIAS("platform:dw-mipi-dsi");