dw_drm_dsi.c 20 KB

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