dw_drm_dsi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * DesignWare MIPI DSI Host Controller v1.02 driver
  3. *
  4. * Copyright (c) 2016 Linaro Limited.
  5. * Copyright (c) 2014-2016 Hisilicon Limited.
  6. *
  7. * Author:
  8. * Xinliang Liu <z.liuxinliang@hisilicon.com>
  9. * Xinliang Liu <xinliang.liu@linaro.org>
  10. * Xinwei Kong <kong.kongxinwei@hisilicon.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/component.h>
  19. #include <drm/drm_of.h>
  20. #include <drm/drm_crtc_helper.h>
  21. #include <drm/drm_mipi_dsi.h>
  22. #include <drm/drm_encoder_slave.h>
  23. #include <drm/drm_atomic_helper.h>
  24. #include "dw_dsi_reg.h"
  25. #define MAX_TX_ESC_CLK 10
  26. #define ROUND(x, y) ((x) / (y) + \
  27. ((x) % (y) * 10 / (y) >= 5 ? 1 : 0))
  28. #define PHY_REF_CLK_RATE 19200000
  29. #define PHY_REF_CLK_PERIOD_PS (1000000000 / (PHY_REF_CLK_RATE / 1000))
  30. #define encoder_to_dsi(encoder) \
  31. container_of(encoder, struct dw_dsi, encoder)
  32. #define host_to_dsi(host) \
  33. container_of(host, struct dw_dsi, host)
  34. struct mipi_phy_params {
  35. u32 clk_t_lpx;
  36. u32 clk_t_hs_prepare;
  37. u32 clk_t_hs_zero;
  38. u32 clk_t_hs_trial;
  39. u32 clk_t_wakeup;
  40. u32 data_t_lpx;
  41. u32 data_t_hs_prepare;
  42. u32 data_t_hs_zero;
  43. u32 data_t_hs_trial;
  44. u32 data_t_ta_go;
  45. u32 data_t_ta_get;
  46. u32 data_t_wakeup;
  47. u32 hstx_ckg_sel;
  48. u32 pll_fbd_div5f;
  49. u32 pll_fbd_div1f;
  50. u32 pll_fbd_2p;
  51. u32 pll_enbwt;
  52. u32 pll_fbd_p;
  53. u32 pll_fbd_s;
  54. u32 pll_pre_div1p;
  55. u32 pll_pre_p;
  56. u32 pll_vco_750M;
  57. u32 pll_lpf_rs;
  58. u32 pll_lpf_cs;
  59. u32 clklp2hs_time;
  60. u32 clkhs2lp_time;
  61. u32 lp2hs_time;
  62. u32 hs2lp_time;
  63. u32 clk_to_data_delay;
  64. u32 data_to_clk_delay;
  65. u32 lane_byte_clk_kHz;
  66. u32 clk_division;
  67. };
  68. struct dsi_hw_ctx {
  69. void __iomem *base;
  70. struct clk *pclk;
  71. };
  72. struct dw_dsi {
  73. struct drm_encoder encoder;
  74. struct drm_bridge *bridge;
  75. struct mipi_dsi_host host;
  76. struct drm_display_mode cur_mode;
  77. struct dsi_hw_ctx *ctx;
  78. struct mipi_phy_params phy;
  79. u32 lanes;
  80. enum mipi_dsi_pixel_format format;
  81. unsigned long mode_flags;
  82. bool enable;
  83. };
  84. struct dsi_data {
  85. struct dw_dsi dsi;
  86. struct dsi_hw_ctx ctx;
  87. };
  88. struct dsi_phy_range {
  89. u32 min_range_kHz;
  90. u32 max_range_kHz;
  91. u32 pll_vco_750M;
  92. u32 hstx_ckg_sel;
  93. };
  94. static const struct dsi_phy_range dphy_range_info[] = {
  95. { 46875, 62500, 1, 7 },
  96. { 62500, 93750, 0, 7 },
  97. { 93750, 125000, 1, 6 },
  98. { 125000, 187500, 0, 6 },
  99. { 187500, 250000, 1, 5 },
  100. { 250000, 375000, 0, 5 },
  101. { 375000, 500000, 1, 4 },
  102. { 500000, 750000, 0, 4 },
  103. { 750000, 1000000, 1, 0 },
  104. { 1000000, 1500000, 0, 0 }
  105. };
  106. static u32 dsi_calc_phy_rate(u32 req_kHz, struct mipi_phy_params *phy)
  107. {
  108. u32 ref_clk_ps = PHY_REF_CLK_PERIOD_PS;
  109. u32 tmp_kHz = req_kHz;
  110. u32 i = 0;
  111. u32 q_pll = 1;
  112. u32 m_pll = 0;
  113. u32 n_pll = 0;
  114. u32 r_pll = 1;
  115. u32 m_n = 0;
  116. u32 m_n_int = 0;
  117. u32 f_kHz = 0;
  118. u64 temp;
  119. /*
  120. * Find a rate >= req_kHz.
  121. */
  122. do {
  123. f_kHz = tmp_kHz;
  124. for (i = 0; i < ARRAY_SIZE(dphy_range_info); i++)
  125. if (f_kHz >= dphy_range_info[i].min_range_kHz &&
  126. f_kHz <= dphy_range_info[i].max_range_kHz)
  127. break;
  128. if (i == ARRAY_SIZE(dphy_range_info)) {
  129. DRM_ERROR("%dkHz out of range\n", f_kHz);
  130. return 0;
  131. }
  132. phy->pll_vco_750M = dphy_range_info[i].pll_vco_750M;
  133. phy->hstx_ckg_sel = dphy_range_info[i].hstx_ckg_sel;
  134. if (phy->hstx_ckg_sel <= 7 &&
  135. phy->hstx_ckg_sel >= 4)
  136. q_pll = 0x10 >> (7 - phy->hstx_ckg_sel);
  137. temp = f_kHz * (u64)q_pll * (u64)ref_clk_ps;
  138. m_n_int = temp / (u64)1000000000;
  139. m_n = (temp % (u64)1000000000) / (u64)100000000;
  140. if (m_n_int % 2 == 0) {
  141. if (m_n * 6 >= 50) {
  142. n_pll = 2;
  143. m_pll = (m_n_int + 1) * n_pll;
  144. } else if (m_n * 6 >= 30) {
  145. n_pll = 3;
  146. m_pll = m_n_int * n_pll + 2;
  147. } else {
  148. n_pll = 1;
  149. m_pll = m_n_int * n_pll;
  150. }
  151. } else {
  152. if (m_n * 6 >= 50) {
  153. n_pll = 1;
  154. m_pll = (m_n_int + 1) * n_pll;
  155. } else if (m_n * 6 >= 30) {
  156. n_pll = 1;
  157. m_pll = (m_n_int + 1) * n_pll;
  158. } else if (m_n * 6 >= 10) {
  159. n_pll = 3;
  160. m_pll = m_n_int * n_pll + 1;
  161. } else {
  162. n_pll = 2;
  163. m_pll = m_n_int * n_pll;
  164. }
  165. }
  166. if (n_pll == 1) {
  167. phy->pll_fbd_p = 0;
  168. phy->pll_pre_div1p = 1;
  169. } else {
  170. phy->pll_fbd_p = n_pll;
  171. phy->pll_pre_div1p = 0;
  172. }
  173. if (phy->pll_fbd_2p <= 7 && phy->pll_fbd_2p >= 4)
  174. r_pll = 0x10 >> (7 - phy->pll_fbd_2p);
  175. if (m_pll == 2) {
  176. phy->pll_pre_p = 0;
  177. phy->pll_fbd_s = 0;
  178. phy->pll_fbd_div1f = 0;
  179. phy->pll_fbd_div5f = 1;
  180. } else if (m_pll >= 2 * 2 * r_pll && m_pll <= 2 * 4 * r_pll) {
  181. phy->pll_pre_p = m_pll / (2 * r_pll);
  182. phy->pll_fbd_s = 0;
  183. phy->pll_fbd_div1f = 1;
  184. phy->pll_fbd_div5f = 0;
  185. } else if (m_pll >= 2 * 5 * r_pll && m_pll <= 2 * 150 * r_pll) {
  186. if (((m_pll / (2 * r_pll)) % 2) == 0) {
  187. phy->pll_pre_p =
  188. (m_pll / (2 * r_pll)) / 2 - 1;
  189. phy->pll_fbd_s =
  190. (m_pll / (2 * r_pll)) % 2 + 2;
  191. } else {
  192. phy->pll_pre_p =
  193. (m_pll / (2 * r_pll)) / 2;
  194. phy->pll_fbd_s =
  195. (m_pll / (2 * r_pll)) % 2;
  196. }
  197. phy->pll_fbd_div1f = 0;
  198. phy->pll_fbd_div5f = 0;
  199. } else {
  200. phy->pll_pre_p = 0;
  201. phy->pll_fbd_s = 0;
  202. phy->pll_fbd_div1f = 0;
  203. phy->pll_fbd_div5f = 1;
  204. }
  205. f_kHz = (u64)1000000000 * (u64)m_pll /
  206. ((u64)ref_clk_ps * (u64)n_pll * (u64)q_pll);
  207. if (f_kHz >= req_kHz)
  208. break;
  209. tmp_kHz += 10;
  210. } while (true);
  211. return f_kHz;
  212. }
  213. static void dsi_get_phy_params(u32 phy_req_kHz,
  214. struct mipi_phy_params *phy)
  215. {
  216. u32 ref_clk_ps = PHY_REF_CLK_PERIOD_PS;
  217. u32 phy_rate_kHz;
  218. u32 ui;
  219. memset(phy, 0, sizeof(*phy));
  220. phy_rate_kHz = dsi_calc_phy_rate(phy_req_kHz, phy);
  221. if (!phy_rate_kHz)
  222. return;
  223. ui = 1000000 / phy_rate_kHz;
  224. phy->clk_t_lpx = ROUND(50, 8 * ui);
  225. phy->clk_t_hs_prepare = ROUND(133, 16 * ui) - 1;
  226. phy->clk_t_hs_zero = ROUND(262, 8 * ui);
  227. phy->clk_t_hs_trial = 2 * (ROUND(60, 8 * ui) - 1);
  228. phy->clk_t_wakeup = ROUND(1000000, (ref_clk_ps / 1000) - 1);
  229. if (phy->clk_t_wakeup > 0xff)
  230. phy->clk_t_wakeup = 0xff;
  231. phy->data_t_wakeup = phy->clk_t_wakeup;
  232. phy->data_t_lpx = phy->clk_t_lpx;
  233. phy->data_t_hs_prepare = ROUND(125 + 10 * ui, 16 * ui) - 1;
  234. phy->data_t_hs_zero = ROUND(105 + 6 * ui, 8 * ui);
  235. phy->data_t_hs_trial = 2 * (ROUND(60 + 4 * ui, 8 * ui) - 1);
  236. phy->data_t_ta_go = 3;
  237. phy->data_t_ta_get = 4;
  238. phy->pll_enbwt = 1;
  239. phy->clklp2hs_time = ROUND(407, 8 * ui) + 12;
  240. phy->clkhs2lp_time = ROUND(105 + 12 * ui, 8 * ui);
  241. phy->lp2hs_time = ROUND(240 + 12 * ui, 8 * ui) + 1;
  242. phy->hs2lp_time = phy->clkhs2lp_time;
  243. phy->clk_to_data_delay = 1 + phy->clklp2hs_time;
  244. phy->data_to_clk_delay = ROUND(60 + 52 * ui, 8 * ui) +
  245. phy->clkhs2lp_time;
  246. phy->lane_byte_clk_kHz = phy_rate_kHz / 8;
  247. phy->clk_division =
  248. DIV_ROUND_UP(phy->lane_byte_clk_kHz, MAX_TX_ESC_CLK);
  249. }
  250. static u32 dsi_get_dpi_color_coding(enum mipi_dsi_pixel_format format)
  251. {
  252. u32 val;
  253. /*
  254. * TODO: only support RGB888 now, to support more
  255. */
  256. switch (format) {
  257. case MIPI_DSI_FMT_RGB888:
  258. val = DSI_24BITS_1;
  259. break;
  260. default:
  261. val = DSI_24BITS_1;
  262. break;
  263. }
  264. return val;
  265. }
  266. /*
  267. * dsi phy reg write function
  268. */
  269. static void dsi_phy_tst_set(void __iomem *base, u32 reg, u32 val)
  270. {
  271. u32 reg_write = 0x10000 + reg;
  272. /*
  273. * latch reg first
  274. */
  275. writel(reg_write, base + PHY_TST_CTRL1);
  276. writel(0x02, base + PHY_TST_CTRL0);
  277. writel(0x00, base + PHY_TST_CTRL0);
  278. /*
  279. * then latch value
  280. */
  281. writel(val, base + PHY_TST_CTRL1);
  282. writel(0x02, base + PHY_TST_CTRL0);
  283. writel(0x00, base + PHY_TST_CTRL0);
  284. }
  285. static void dsi_set_phy_timer(void __iomem *base,
  286. struct mipi_phy_params *phy,
  287. u32 lanes)
  288. {
  289. u32 val;
  290. /*
  291. * Set lane value and phy stop wait time.
  292. */
  293. val = (lanes - 1) | (PHY_STOP_WAIT_TIME << 8);
  294. writel(val, base + PHY_IF_CFG);
  295. /*
  296. * Set phy clk division.
  297. */
  298. val = readl(base + CLKMGR_CFG) | phy->clk_division;
  299. writel(val, base + CLKMGR_CFG);
  300. /*
  301. * Set lp and hs switching params.
  302. */
  303. dw_update_bits(base + PHY_TMR_CFG, 24, MASK(8), phy->hs2lp_time);
  304. dw_update_bits(base + PHY_TMR_CFG, 16, MASK(8), phy->lp2hs_time);
  305. dw_update_bits(base + PHY_TMR_LPCLK_CFG, 16, MASK(10),
  306. phy->clkhs2lp_time);
  307. dw_update_bits(base + PHY_TMR_LPCLK_CFG, 0, MASK(10),
  308. phy->clklp2hs_time);
  309. dw_update_bits(base + CLK_DATA_TMR_CFG, 8, MASK(8),
  310. phy->data_to_clk_delay);
  311. dw_update_bits(base + CLK_DATA_TMR_CFG, 0, MASK(8),
  312. phy->clk_to_data_delay);
  313. }
  314. static void dsi_set_mipi_phy(void __iomem *base,
  315. struct mipi_phy_params *phy,
  316. u32 lanes)
  317. {
  318. u32 delay_count;
  319. u32 val;
  320. u32 i;
  321. /* phy timer setting */
  322. dsi_set_phy_timer(base, phy, lanes);
  323. /*
  324. * Reset to clean up phy tst params.
  325. */
  326. writel(0, base + PHY_RSTZ);
  327. writel(0, base + PHY_TST_CTRL0);
  328. writel(1, base + PHY_TST_CTRL0);
  329. writel(0, base + PHY_TST_CTRL0);
  330. /*
  331. * Clock lane timing control setting: TLPX, THS-PREPARE,
  332. * THS-ZERO, THS-TRAIL, TWAKEUP.
  333. */
  334. dsi_phy_tst_set(base, CLK_TLPX, phy->clk_t_lpx);
  335. dsi_phy_tst_set(base, CLK_THS_PREPARE, phy->clk_t_hs_prepare);
  336. dsi_phy_tst_set(base, CLK_THS_ZERO, phy->clk_t_hs_zero);
  337. dsi_phy_tst_set(base, CLK_THS_TRAIL, phy->clk_t_hs_trial);
  338. dsi_phy_tst_set(base, CLK_TWAKEUP, phy->clk_t_wakeup);
  339. /*
  340. * Data lane timing control setting: TLPX, THS-PREPARE,
  341. * THS-ZERO, THS-TRAIL, TTA-GO, TTA-GET, TWAKEUP.
  342. */
  343. for (i = 0; i < lanes; i++) {
  344. dsi_phy_tst_set(base, DATA_TLPX(i), phy->data_t_lpx);
  345. dsi_phy_tst_set(base, DATA_THS_PREPARE(i),
  346. phy->data_t_hs_prepare);
  347. dsi_phy_tst_set(base, DATA_THS_ZERO(i), phy->data_t_hs_zero);
  348. dsi_phy_tst_set(base, DATA_THS_TRAIL(i), phy->data_t_hs_trial);
  349. dsi_phy_tst_set(base, DATA_TTA_GO(i), phy->data_t_ta_go);
  350. dsi_phy_tst_set(base, DATA_TTA_GET(i), phy->data_t_ta_get);
  351. dsi_phy_tst_set(base, DATA_TWAKEUP(i), phy->data_t_wakeup);
  352. }
  353. /*
  354. * physical configuration: I, pll I, pll II, pll III,
  355. * pll IV, pll V.
  356. */
  357. dsi_phy_tst_set(base, PHY_CFG_I, phy->hstx_ckg_sel);
  358. val = (phy->pll_fbd_div5f << 5) + (phy->pll_fbd_div1f << 4) +
  359. (phy->pll_fbd_2p << 1) + phy->pll_enbwt;
  360. dsi_phy_tst_set(base, PHY_CFG_PLL_I, val);
  361. dsi_phy_tst_set(base, PHY_CFG_PLL_II, phy->pll_fbd_p);
  362. dsi_phy_tst_set(base, PHY_CFG_PLL_III, phy->pll_fbd_s);
  363. val = (phy->pll_pre_div1p << 7) + phy->pll_pre_p;
  364. dsi_phy_tst_set(base, PHY_CFG_PLL_IV, val);
  365. val = (5 << 5) + (phy->pll_vco_750M << 4) + (phy->pll_lpf_rs << 2) +
  366. phy->pll_lpf_cs;
  367. dsi_phy_tst_set(base, PHY_CFG_PLL_V, val);
  368. writel(PHY_ENABLECLK, base + PHY_RSTZ);
  369. udelay(1);
  370. writel(PHY_ENABLECLK | PHY_UNSHUTDOWNZ, base + PHY_RSTZ);
  371. udelay(1);
  372. writel(PHY_ENABLECLK | PHY_UNRSTZ | PHY_UNSHUTDOWNZ, base + PHY_RSTZ);
  373. usleep_range(1000, 1500);
  374. /*
  375. * wait for phy's clock ready
  376. */
  377. delay_count = 100;
  378. while (delay_count) {
  379. val = readl(base + PHY_STATUS);
  380. if ((BIT(0) | BIT(2)) & val)
  381. break;
  382. udelay(1);
  383. delay_count--;
  384. }
  385. if (!delay_count)
  386. DRM_INFO("phylock and phystopstateclklane is not ready.\n");
  387. }
  388. static void dsi_set_mode_timing(void __iomem *base,
  389. u32 lane_byte_clk_kHz,
  390. struct drm_display_mode *mode,
  391. enum mipi_dsi_pixel_format format)
  392. {
  393. u32 hfp, hbp, hsw, vfp, vbp, vsw;
  394. u32 hline_time;
  395. u32 hsa_time;
  396. u32 hbp_time;
  397. u32 pixel_clk_kHz;
  398. int htot, vtot;
  399. u32 val;
  400. u64 tmp;
  401. val = dsi_get_dpi_color_coding(format);
  402. writel(val, base + DPI_COLOR_CODING);
  403. val = (mode->flags & DRM_MODE_FLAG_NHSYNC ? 1 : 0) << 2;
  404. val |= (mode->flags & DRM_MODE_FLAG_NVSYNC ? 1 : 0) << 1;
  405. writel(val, base + DPI_CFG_POL);
  406. /*
  407. * The DSI IP accepts vertical timing using lines as normal,
  408. * but horizontal timing is a mixture of pixel-clocks for the
  409. * active region and byte-lane clocks for the blanking-related
  410. * timings. hfp is specified as the total hline_time in byte-
  411. * lane clocks minus hsa, hbp and active.
  412. */
  413. pixel_clk_kHz = mode->clock;
  414. htot = mode->htotal;
  415. vtot = mode->vtotal;
  416. hfp = mode->hsync_start - mode->hdisplay;
  417. hbp = mode->htotal - mode->hsync_end;
  418. hsw = mode->hsync_end - mode->hsync_start;
  419. vfp = mode->vsync_start - mode->vdisplay;
  420. vbp = mode->vtotal - mode->vsync_end;
  421. vsw = mode->vsync_end - mode->vsync_start;
  422. if (vsw > 15) {
  423. DRM_DEBUG_DRIVER("vsw exceeded 15\n");
  424. vsw = 15;
  425. }
  426. hsa_time = (hsw * lane_byte_clk_kHz) / pixel_clk_kHz;
  427. hbp_time = (hbp * lane_byte_clk_kHz) / pixel_clk_kHz;
  428. tmp = (u64)htot * (u64)lane_byte_clk_kHz;
  429. hline_time = DIV_ROUND_UP(tmp, pixel_clk_kHz);
  430. /* all specified in byte-lane clocks */
  431. writel(hsa_time, base + VID_HSA_TIME);
  432. writel(hbp_time, base + VID_HBP_TIME);
  433. writel(hline_time, base + VID_HLINE_TIME);
  434. writel(vsw, base + VID_VSA_LINES);
  435. writel(vbp, base + VID_VBP_LINES);
  436. writel(vfp, base + VID_VFP_LINES);
  437. writel(mode->vdisplay, base + VID_VACTIVE_LINES);
  438. writel(mode->hdisplay, base + VID_PKT_SIZE);
  439. DRM_DEBUG_DRIVER("htot=%d, hfp=%d, hbp=%d, hsw=%d\n",
  440. htot, hfp, hbp, hsw);
  441. DRM_DEBUG_DRIVER("vtol=%d, vfp=%d, vbp=%d, vsw=%d\n",
  442. vtot, vfp, vbp, vsw);
  443. DRM_DEBUG_DRIVER("hsa_time=%d, hbp_time=%d, hline_time=%d\n",
  444. hsa_time, hbp_time, hline_time);
  445. }
  446. static void dsi_set_video_mode(void __iomem *base, unsigned long flags)
  447. {
  448. u32 val;
  449. u32 mode_mask = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
  450. MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
  451. u32 non_burst_sync_pulse = MIPI_DSI_MODE_VIDEO |
  452. MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
  453. u32 non_burst_sync_event = MIPI_DSI_MODE_VIDEO;
  454. /*
  455. * choose video mode type
  456. */
  457. if ((flags & mode_mask) == non_burst_sync_pulse)
  458. val = DSI_NON_BURST_SYNC_PULSES;
  459. else if ((flags & mode_mask) == non_burst_sync_event)
  460. val = DSI_NON_BURST_SYNC_EVENTS;
  461. else
  462. val = DSI_BURST_SYNC_PULSES_1;
  463. writel(val, base + VID_MODE_CFG);
  464. writel(PHY_TXREQUESTCLKHS, base + LPCLK_CTRL);
  465. writel(DSI_VIDEO_MODE, base + MODE_CFG);
  466. }
  467. static void dsi_mipi_init(struct dw_dsi *dsi)
  468. {
  469. struct dsi_hw_ctx *ctx = dsi->ctx;
  470. struct mipi_phy_params *phy = &dsi->phy;
  471. struct drm_display_mode *mode = &dsi->cur_mode;
  472. u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
  473. void __iomem *base = ctx->base;
  474. u32 dphy_req_kHz;
  475. /*
  476. * count phy params
  477. */
  478. dphy_req_kHz = mode->clock * bpp / dsi->lanes;
  479. dsi_get_phy_params(dphy_req_kHz, phy);
  480. /* reset Core */
  481. writel(RESET, base + PWR_UP);
  482. /* set dsi phy params */
  483. dsi_set_mipi_phy(base, phy, dsi->lanes);
  484. /* set dsi mode timing */
  485. dsi_set_mode_timing(base, phy->lane_byte_clk_kHz, mode, dsi->format);
  486. /* set dsi video mode */
  487. dsi_set_video_mode(base, dsi->mode_flags);
  488. /* dsi wake up */
  489. writel(POWERUP, base + PWR_UP);
  490. DRM_DEBUG_DRIVER("lanes=%d, pixel_clk=%d kHz, bytes_freq=%d kHz\n",
  491. dsi->lanes, mode->clock, phy->lane_byte_clk_kHz);
  492. }
  493. static void dsi_encoder_disable(struct drm_encoder *encoder)
  494. {
  495. struct dw_dsi *dsi = encoder_to_dsi(encoder);
  496. struct dsi_hw_ctx *ctx = dsi->ctx;
  497. void __iomem *base = ctx->base;
  498. if (!dsi->enable)
  499. return;
  500. writel(0, base + PWR_UP);
  501. writel(0, base + LPCLK_CTRL);
  502. writel(0, base + PHY_RSTZ);
  503. clk_disable_unprepare(ctx->pclk);
  504. dsi->enable = false;
  505. }
  506. static void dsi_encoder_enable(struct drm_encoder *encoder)
  507. {
  508. struct dw_dsi *dsi = encoder_to_dsi(encoder);
  509. struct dsi_hw_ctx *ctx = dsi->ctx;
  510. int ret;
  511. if (dsi->enable)
  512. return;
  513. ret = clk_prepare_enable(ctx->pclk);
  514. if (ret) {
  515. DRM_ERROR("fail to enable pclk: %d\n", ret);
  516. return;
  517. }
  518. dsi_mipi_init(dsi);
  519. dsi->enable = true;
  520. }
  521. static void dsi_encoder_mode_set(struct drm_encoder *encoder,
  522. struct drm_display_mode *mode,
  523. struct drm_display_mode *adj_mode)
  524. {
  525. struct dw_dsi *dsi = encoder_to_dsi(encoder);
  526. drm_mode_copy(&dsi->cur_mode, adj_mode);
  527. }
  528. static int dsi_encoder_atomic_check(struct drm_encoder *encoder,
  529. struct drm_crtc_state *crtc_state,
  530. struct drm_connector_state *conn_state)
  531. {
  532. /* do nothing */
  533. return 0;
  534. }
  535. static const struct drm_encoder_helper_funcs dw_encoder_helper_funcs = {
  536. .atomic_check = dsi_encoder_atomic_check,
  537. .mode_set = dsi_encoder_mode_set,
  538. .enable = dsi_encoder_enable,
  539. .disable = dsi_encoder_disable
  540. };
  541. static const struct drm_encoder_funcs dw_encoder_funcs = {
  542. .destroy = drm_encoder_cleanup,
  543. };
  544. static int dw_drm_encoder_init(struct device *dev,
  545. struct drm_device *drm_dev,
  546. struct drm_encoder *encoder)
  547. {
  548. int ret;
  549. u32 crtc_mask = drm_of_find_possible_crtcs(drm_dev, dev->of_node);
  550. if (!crtc_mask) {
  551. DRM_ERROR("failed to find crtc mask\n");
  552. return -EINVAL;
  553. }
  554. encoder->possible_crtcs = crtc_mask;
  555. ret = drm_encoder_init(drm_dev, encoder, &dw_encoder_funcs,
  556. DRM_MODE_ENCODER_DSI, NULL);
  557. if (ret) {
  558. DRM_ERROR("failed to init dsi encoder\n");
  559. return ret;
  560. }
  561. drm_encoder_helper_add(encoder, &dw_encoder_helper_funcs);
  562. return 0;
  563. }
  564. static int dsi_host_attach(struct mipi_dsi_host *host,
  565. struct mipi_dsi_device *mdsi)
  566. {
  567. struct dw_dsi *dsi = host_to_dsi(host);
  568. if (mdsi->lanes < 1 || mdsi->lanes > 4) {
  569. DRM_ERROR("dsi device params invalid\n");
  570. return -EINVAL;
  571. }
  572. dsi->lanes = mdsi->lanes;
  573. dsi->format = mdsi->format;
  574. dsi->mode_flags = mdsi->mode_flags;
  575. return 0;
  576. }
  577. static int dsi_host_detach(struct mipi_dsi_host *host,
  578. struct mipi_dsi_device *mdsi)
  579. {
  580. /* do nothing */
  581. return 0;
  582. }
  583. static const struct mipi_dsi_host_ops dsi_host_ops = {
  584. .attach = dsi_host_attach,
  585. .detach = dsi_host_detach,
  586. };
  587. static int dsi_host_init(struct device *dev, struct dw_dsi *dsi)
  588. {
  589. struct mipi_dsi_host *host = &dsi->host;
  590. int ret;
  591. host->dev = dev;
  592. host->ops = &dsi_host_ops;
  593. ret = mipi_dsi_host_register(host);
  594. if (ret) {
  595. DRM_ERROR("failed to register dsi host\n");
  596. return ret;
  597. }
  598. return 0;
  599. }
  600. static int dsi_bridge_init(struct drm_device *dev, struct dw_dsi *dsi)
  601. {
  602. struct drm_encoder *encoder = &dsi->encoder;
  603. struct drm_bridge *bridge = dsi->bridge;
  604. int ret;
  605. /* associate the bridge to dsi encoder */
  606. ret = drm_bridge_attach(encoder, bridge, NULL);
  607. if (ret) {
  608. DRM_ERROR("failed to attach external bridge\n");
  609. return ret;
  610. }
  611. return 0;
  612. }
  613. static int dsi_bind(struct device *dev, struct device *master, void *data)
  614. {
  615. struct dsi_data *ddata = dev_get_drvdata(dev);
  616. struct dw_dsi *dsi = &ddata->dsi;
  617. struct drm_device *drm_dev = data;
  618. int ret;
  619. ret = dw_drm_encoder_init(dev, drm_dev, &dsi->encoder);
  620. if (ret)
  621. return ret;
  622. ret = dsi_host_init(dev, dsi);
  623. if (ret)
  624. return ret;
  625. ret = dsi_bridge_init(drm_dev, dsi);
  626. if (ret)
  627. return ret;
  628. return 0;
  629. }
  630. static void dsi_unbind(struct device *dev, struct device *master, void *data)
  631. {
  632. /* do nothing */
  633. }
  634. static const struct component_ops dsi_ops = {
  635. .bind = dsi_bind,
  636. .unbind = dsi_unbind,
  637. };
  638. static int dsi_parse_dt(struct platform_device *pdev, struct dw_dsi *dsi)
  639. {
  640. struct dsi_hw_ctx *ctx = dsi->ctx;
  641. struct device_node *np = pdev->dev.of_node;
  642. struct resource *res;
  643. int ret;
  644. /*
  645. * Get the endpoint node. In our case, dsi has one output port1
  646. * to which the external HDMI bridge is connected.
  647. */
  648. ret = drm_of_find_panel_or_bridge(np, 0, 0, NULL, &dsi->bridge);
  649. if (ret)
  650. return ret;
  651. ctx->pclk = devm_clk_get(&pdev->dev, "pclk");
  652. if (IS_ERR(ctx->pclk)) {
  653. DRM_ERROR("failed to get pclk clock\n");
  654. return PTR_ERR(ctx->pclk);
  655. }
  656. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  657. ctx->base = devm_ioremap_resource(&pdev->dev, res);
  658. if (IS_ERR(ctx->base)) {
  659. DRM_ERROR("failed to remap dsi io region\n");
  660. return PTR_ERR(ctx->base);
  661. }
  662. return 0;
  663. }
  664. static int dsi_probe(struct platform_device *pdev)
  665. {
  666. struct dsi_data *data;
  667. struct dw_dsi *dsi;
  668. struct dsi_hw_ctx *ctx;
  669. int ret;
  670. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  671. if (!data) {
  672. DRM_ERROR("failed to allocate dsi data.\n");
  673. return -ENOMEM;
  674. }
  675. dsi = &data->dsi;
  676. ctx = &data->ctx;
  677. dsi->ctx = ctx;
  678. ret = dsi_parse_dt(pdev, dsi);
  679. if (ret)
  680. return ret;
  681. platform_set_drvdata(pdev, data);
  682. return component_add(&pdev->dev, &dsi_ops);
  683. }
  684. static int dsi_remove(struct platform_device *pdev)
  685. {
  686. component_del(&pdev->dev, &dsi_ops);
  687. return 0;
  688. }
  689. static const struct of_device_id dsi_of_match[] = {
  690. {.compatible = "hisilicon,hi6220-dsi"},
  691. { }
  692. };
  693. MODULE_DEVICE_TABLE(of, dsi_of_match);
  694. static struct platform_driver dsi_driver = {
  695. .probe = dsi_probe,
  696. .remove = dsi_remove,
  697. .driver = {
  698. .name = "dw-dsi",
  699. .of_match_table = dsi_of_match,
  700. },
  701. };
  702. module_platform_driver(dsi_driver);
  703. MODULE_AUTHOR("Xinliang Liu <xinliang.liu@linaro.org>");
  704. MODULE_AUTHOR("Xinliang Liu <z.liuxinliang@hisilicon.com>");
  705. MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
  706. MODULE_DESCRIPTION("DesignWare MIPI DSI Host Controller v1.02 driver");
  707. MODULE_LICENSE("GPL v2");