dw-mipi-dsi.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /*
  2. * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/component.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/math64.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/regmap.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_crtc.h>
  19. #include <drm/drm_crtc_helper.h>
  20. #include <drm/drm_mipi_dsi.h>
  21. #include <drm/drm_of.h>
  22. #include <drm/drm_panel.h>
  23. #include <drm/drmP.h>
  24. #include <video/mipi_display.h>
  25. #include "rockchip_drm_drv.h"
  26. #include "rockchip_drm_vop.h"
  27. #define DRIVER_NAME "dw-mipi-dsi"
  28. #define GRF_SOC_CON6 0x025c
  29. #define DSI0_SEL_VOP_LIT (1 << 6)
  30. #define DSI1_SEL_VOP_LIT (1 << 9)
  31. #define DSI_VERSION 0x00
  32. #define DSI_PWR_UP 0x04
  33. #define RESET 0
  34. #define POWERUP BIT(0)
  35. #define DSI_CLKMGR_CFG 0x08
  36. #define TO_CLK_DIVIDSION(div) (((div) & 0xff) << 8)
  37. #define TX_ESC_CLK_DIVIDSION(div) (((div) & 0xff) << 0)
  38. #define DSI_DPI_VCID 0x0c
  39. #define DPI_VID(vid) (((vid) & 0x3) << 0)
  40. #define DSI_DPI_COLOR_CODING 0x10
  41. #define EN18_LOOSELY BIT(8)
  42. #define DPI_COLOR_CODING_16BIT_1 0x0
  43. #define DPI_COLOR_CODING_16BIT_2 0x1
  44. #define DPI_COLOR_CODING_16BIT_3 0x2
  45. #define DPI_COLOR_CODING_18BIT_1 0x3
  46. #define DPI_COLOR_CODING_18BIT_2 0x4
  47. #define DPI_COLOR_CODING_24BIT 0x5
  48. #define DSI_DPI_CFG_POL 0x14
  49. #define COLORM_ACTIVE_LOW BIT(4)
  50. #define SHUTD_ACTIVE_LOW BIT(3)
  51. #define HSYNC_ACTIVE_LOW BIT(2)
  52. #define VSYNC_ACTIVE_LOW BIT(1)
  53. #define DATAEN_ACTIVE_LOW BIT(0)
  54. #define DSI_DPI_LP_CMD_TIM 0x18
  55. #define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
  56. #define INVACT_LPCMD_TIME(p) ((p) & 0xff)
  57. #define DSI_DBI_CFG 0x20
  58. #define DSI_DBI_CMDSIZE 0x28
  59. #define DSI_PCKHDL_CFG 0x2c
  60. #define EN_CRC_RX BIT(4)
  61. #define EN_ECC_RX BIT(3)
  62. #define EN_BTA BIT(2)
  63. #define EN_EOTP_RX BIT(1)
  64. #define EN_EOTP_TX BIT(0)
  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 FRAME_BTA_ACK BIT(14)
  70. #define ENABLE_LOW_POWER (0x3f << 8)
  71. #define ENABLE_LOW_POWER_MASK (0x3f << 8)
  72. #define VID_MODE_TYPE_BURST_SYNC_PULSES 0x2
  73. #define VID_MODE_TYPE_MASK 0x3
  74. #define DSI_VID_PKT_SIZE 0x3c
  75. #define VID_PKT_SIZE(p) (((p) & 0x3fff) << 0)
  76. #define VID_PKT_MAX_SIZE 0x3fff
  77. #define DSI_VID_HSA_TIME 0x48
  78. #define DSI_VID_HBP_TIME 0x4c
  79. #define DSI_VID_HLINE_TIME 0x50
  80. #define DSI_VID_VSA_LINES 0x54
  81. #define DSI_VID_VBP_LINES 0x58
  82. #define DSI_VID_VFP_LINES 0x5c
  83. #define DSI_VID_VACTIVE_LINES 0x60
  84. #define DSI_CMD_MODE_CFG 0x68
  85. #define MAX_RD_PKT_SIZE_LP BIT(24)
  86. #define DCS_LW_TX_LP BIT(19)
  87. #define DCS_SR_0P_TX_LP BIT(18)
  88. #define DCS_SW_1P_TX_LP BIT(17)
  89. #define DCS_SW_0P_TX_LP BIT(16)
  90. #define GEN_LW_TX_LP BIT(14)
  91. #define GEN_SR_2P_TX_LP BIT(13)
  92. #define GEN_SR_1P_TX_LP BIT(12)
  93. #define GEN_SR_0P_TX_LP BIT(11)
  94. #define GEN_SW_2P_TX_LP BIT(10)
  95. #define GEN_SW_1P_TX_LP BIT(9)
  96. #define GEN_SW_0P_TX_LP BIT(8)
  97. #define EN_ACK_RQST BIT(1)
  98. #define EN_TEAR_FX BIT(0)
  99. #define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
  100. DCS_LW_TX_LP | \
  101. DCS_SR_0P_TX_LP | \
  102. DCS_SW_1P_TX_LP | \
  103. DCS_SW_0P_TX_LP | \
  104. GEN_LW_TX_LP | \
  105. GEN_SR_2P_TX_LP | \
  106. GEN_SR_1P_TX_LP | \
  107. GEN_SR_0P_TX_LP | \
  108. GEN_SW_2P_TX_LP | \
  109. GEN_SW_1P_TX_LP | \
  110. GEN_SW_0P_TX_LP)
  111. #define DSI_GEN_HDR 0x6c
  112. #define GEN_HDATA(data) (((data) & 0xffff) << 8)
  113. #define GEN_HDATA_MASK (0xffff << 8)
  114. #define GEN_HTYPE(type) (((type) & 0xff) << 0)
  115. #define GEN_HTYPE_MASK 0xff
  116. #define DSI_GEN_PLD_DATA 0x70
  117. #define DSI_CMD_PKT_STATUS 0x74
  118. #define GEN_CMD_EMPTY BIT(0)
  119. #define GEN_CMD_FULL BIT(1)
  120. #define GEN_PLD_W_EMPTY BIT(2)
  121. #define GEN_PLD_W_FULL BIT(3)
  122. #define GEN_PLD_R_EMPTY BIT(4)
  123. #define GEN_PLD_R_FULL BIT(5)
  124. #define GEN_RD_CMD_BUSY BIT(6)
  125. #define DSI_TO_CNT_CFG 0x78
  126. #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
  127. #define LPRX_TO_CNT(p) ((p) & 0xffff)
  128. #define DSI_BTA_TO_CNT 0x8c
  129. #define DSI_LPCLK_CTRL 0x94
  130. #define AUTO_CLKLANE_CTRL BIT(1)
  131. #define PHY_TXREQUESTCLKHS BIT(0)
  132. #define DSI_PHY_TMR_LPCLK_CFG 0x98
  133. #define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
  134. #define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
  135. #define DSI_PHY_TMR_CFG 0x9c
  136. #define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
  137. #define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
  138. #define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
  139. #define DSI_PHY_RSTZ 0xa0
  140. #define PHY_DISFORCEPLL 0
  141. #define PHY_ENFORCEPLL BIT(3)
  142. #define PHY_DISABLECLK 0
  143. #define PHY_ENABLECLK BIT(2)
  144. #define PHY_RSTZ 0
  145. #define PHY_UNRSTZ BIT(1)
  146. #define PHY_SHUTDOWNZ 0
  147. #define PHY_UNSHUTDOWNZ BIT(0)
  148. #define DSI_PHY_IF_CFG 0xa4
  149. #define N_LANES(n) ((((n) - 1) & 0x3) << 0)
  150. #define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
  151. #define DSI_PHY_STATUS 0xb0
  152. #define LOCK BIT(0)
  153. #define STOP_STATE_CLK_LANE BIT(2)
  154. #define DSI_PHY_TST_CTRL0 0xb4
  155. #define PHY_TESTCLK BIT(1)
  156. #define PHY_UNTESTCLK 0
  157. #define PHY_TESTCLR BIT(0)
  158. #define PHY_UNTESTCLR 0
  159. #define DSI_PHY_TST_CTRL1 0xb8
  160. #define PHY_TESTEN BIT(16)
  161. #define PHY_UNTESTEN 0
  162. #define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
  163. #define PHY_TESTDIN(n) (((n) & 0xff) << 0)
  164. #define DSI_INT_ST0 0xbc
  165. #define DSI_INT_ST1 0xc0
  166. #define DSI_INT_MSK0 0xc4
  167. #define DSI_INT_MSK1 0xc8
  168. #define PHY_STATUS_TIMEOUT_US 10000
  169. #define CMD_PKT_STATUS_TIMEOUT_US 20000
  170. #define BYPASS_VCO_RANGE BIT(7)
  171. #define VCO_RANGE_CON_SEL(val) (((val) & 0x7) << 3)
  172. #define VCO_IN_CAP_CON_DEFAULT (0x0 << 1)
  173. #define VCO_IN_CAP_CON_LOW (0x1 << 1)
  174. #define VCO_IN_CAP_CON_HIGH (0x2 << 1)
  175. #define REF_BIAS_CUR_SEL BIT(0)
  176. #define CP_CURRENT_3MA BIT(3)
  177. #define CP_PROGRAM_EN BIT(7)
  178. #define LPF_PROGRAM_EN BIT(6)
  179. #define LPF_RESISTORS_20_KOHM 0
  180. #define HSFREQRANGE_SEL(val) (((val) & 0x3f) << 1)
  181. #define INPUT_DIVIDER(val) ((val - 1) & 0x7f)
  182. #define LOW_PROGRAM_EN 0
  183. #define HIGH_PROGRAM_EN BIT(7)
  184. #define LOOP_DIV_LOW_SEL(val) ((val - 1) & 0x1f)
  185. #define LOOP_DIV_HIGH_SEL(val) (((val - 1) >> 5) & 0x1f)
  186. #define PLL_LOOP_DIV_EN BIT(5)
  187. #define PLL_INPUT_DIV_EN BIT(4)
  188. #define POWER_CONTROL BIT(6)
  189. #define INTERNAL_REG_CURRENT BIT(3)
  190. #define BIAS_BLOCK_ON BIT(2)
  191. #define BANDGAP_ON BIT(0)
  192. #define TER_RESISTOR_HIGH BIT(7)
  193. #define TER_RESISTOR_LOW 0
  194. #define LEVEL_SHIFTERS_ON BIT(6)
  195. #define TER_CAL_DONE BIT(5)
  196. #define SETRD_MAX (0x7 << 2)
  197. #define POWER_MANAGE BIT(1)
  198. #define TER_RESISTORS_ON BIT(0)
  199. #define BIASEXTR_SEL(val) ((val) & 0x7)
  200. #define BANDGAP_SEL(val) ((val) & 0x7)
  201. #define TLP_PROGRAM_EN BIT(7)
  202. #define THS_PRE_PROGRAM_EN BIT(7)
  203. #define THS_ZERO_PROGRAM_EN BIT(6)
  204. enum {
  205. BANDGAP_97_07,
  206. BANDGAP_98_05,
  207. BANDGAP_99_02,
  208. BANDGAP_100_00,
  209. BANDGAP_93_17,
  210. BANDGAP_94_15,
  211. BANDGAP_95_12,
  212. BANDGAP_96_10,
  213. };
  214. enum {
  215. BIASEXTR_87_1,
  216. BIASEXTR_91_5,
  217. BIASEXTR_95_9,
  218. BIASEXTR_100,
  219. BIASEXTR_105_94,
  220. BIASEXTR_111_88,
  221. BIASEXTR_118_8,
  222. BIASEXTR_127_7,
  223. };
  224. struct dw_mipi_dsi_plat_data {
  225. unsigned int max_data_lanes;
  226. enum drm_mode_status (*mode_valid)(struct drm_connector *connector,
  227. struct drm_display_mode *mode);
  228. };
  229. struct dw_mipi_dsi {
  230. struct drm_encoder encoder;
  231. struct drm_connector connector;
  232. struct mipi_dsi_host dsi_host;
  233. struct drm_panel *panel;
  234. struct device *dev;
  235. struct regmap *grf_regmap;
  236. void __iomem *base;
  237. struct clk *pllref_clk;
  238. struct clk *pclk;
  239. unsigned int lane_mbps; /* per lane */
  240. u32 channel;
  241. u32 lanes;
  242. u32 format;
  243. u16 input_div;
  244. u16 feedback_div;
  245. struct drm_display_mode *mode;
  246. const struct dw_mipi_dsi_plat_data *pdata;
  247. };
  248. enum dw_mipi_dsi_mode {
  249. DW_MIPI_DSI_CMD_MODE,
  250. DW_MIPI_DSI_VID_MODE,
  251. };
  252. struct dphy_pll_testdin_map {
  253. unsigned int max_mbps;
  254. u8 testdin;
  255. };
  256. /* The table is based on 27MHz DPHY pll reference clock. */
  257. static const struct dphy_pll_testdin_map dptdin_map[] = {
  258. { 90, 0x00}, { 100, 0x10}, { 110, 0x20}, { 130, 0x01},
  259. { 140, 0x11}, { 150, 0x21}, { 170, 0x02}, { 180, 0x12},
  260. { 200, 0x22}, { 220, 0x03}, { 240, 0x13}, { 250, 0x23},
  261. { 270, 0x04}, { 300, 0x14}, { 330, 0x05}, { 360, 0x15},
  262. { 400, 0x25}, { 450, 0x06}, { 500, 0x16}, { 550, 0x07},
  263. { 600, 0x17}, { 650, 0x08}, { 700, 0x18}, { 750, 0x09},
  264. { 800, 0x19}, { 850, 0x29}, { 900, 0x39}, { 950, 0x0a},
  265. {1000, 0x1a}, {1050, 0x2a}, {1100, 0x3a}, {1150, 0x0b},
  266. {1200, 0x1b}, {1250, 0x2b}, {1300, 0x3b}, {1350, 0x0c},
  267. {1400, 0x1c}, {1450, 0x2c}, {1500, 0x3c}
  268. };
  269. static int max_mbps_to_testdin(unsigned int max_mbps)
  270. {
  271. int i;
  272. for (i = 0; i < ARRAY_SIZE(dptdin_map); i++)
  273. if (dptdin_map[i].max_mbps > max_mbps)
  274. return dptdin_map[i].testdin;
  275. return -EINVAL;
  276. }
  277. /*
  278. * The controller should generate 2 frames before
  279. * preparing the peripheral.
  280. */
  281. static void dw_mipi_dsi_wait_for_two_frames(struct dw_mipi_dsi *dsi)
  282. {
  283. int refresh, two_frames;
  284. refresh = drm_mode_vrefresh(dsi->mode);
  285. two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
  286. msleep(two_frames);
  287. }
  288. static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
  289. {
  290. return container_of(host, struct dw_mipi_dsi, dsi_host);
  291. }
  292. static inline struct dw_mipi_dsi *con_to_dsi(struct drm_connector *con)
  293. {
  294. return container_of(con, struct dw_mipi_dsi, connector);
  295. }
  296. static inline struct dw_mipi_dsi *encoder_to_dsi(struct drm_encoder *encoder)
  297. {
  298. return container_of(encoder, struct dw_mipi_dsi, encoder);
  299. }
  300. static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
  301. {
  302. writel(val, dsi->base + reg);
  303. }
  304. static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
  305. {
  306. return readl(dsi->base + reg);
  307. }
  308. static void dw_mipi_dsi_phy_write(struct dw_mipi_dsi *dsi, u8 test_code,
  309. u8 test_data)
  310. {
  311. /*
  312. * With the falling edge on TESTCLK, the TESTDIN[7:0] signal content
  313. * is latched internally as the current test code. Test data is
  314. * programmed internally by rising edge on TESTCLK.
  315. */
  316. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLK | PHY_UNTESTCLR);
  317. dsi_write(dsi, DSI_PHY_TST_CTRL1, PHY_TESTEN | PHY_TESTDOUT(0) |
  318. PHY_TESTDIN(test_code));
  319. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLK | PHY_UNTESTCLR);
  320. dsi_write(dsi, DSI_PHY_TST_CTRL1, PHY_UNTESTEN | PHY_TESTDOUT(0) |
  321. PHY_TESTDIN(test_data));
  322. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLK | PHY_UNTESTCLR);
  323. }
  324. static int dw_mipi_dsi_phy_init(struct dw_mipi_dsi *dsi)
  325. {
  326. int ret, testdin, vco, val;
  327. vco = (dsi->lane_mbps < 200) ? 0 : (dsi->lane_mbps + 100) / 200;
  328. testdin = max_mbps_to_testdin(dsi->lane_mbps);
  329. if (testdin < 0) {
  330. dev_err(dsi->dev,
  331. "failed to get testdin for %dmbps lane clock\n",
  332. dsi->lane_mbps);
  333. return testdin;
  334. }
  335. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  336. dw_mipi_dsi_phy_write(dsi, 0x10, BYPASS_VCO_RANGE |
  337. VCO_RANGE_CON_SEL(vco) |
  338. VCO_IN_CAP_CON_LOW |
  339. REF_BIAS_CUR_SEL);
  340. dw_mipi_dsi_phy_write(dsi, 0x11, CP_CURRENT_3MA);
  341. dw_mipi_dsi_phy_write(dsi, 0x12, CP_PROGRAM_EN | LPF_PROGRAM_EN |
  342. LPF_RESISTORS_20_KOHM);
  343. dw_mipi_dsi_phy_write(dsi, 0x44, HSFREQRANGE_SEL(testdin));
  344. dw_mipi_dsi_phy_write(dsi, 0x19, PLL_LOOP_DIV_EN | PLL_INPUT_DIV_EN);
  345. dw_mipi_dsi_phy_write(dsi, 0x17, INPUT_DIVIDER(dsi->input_div));
  346. dw_mipi_dsi_phy_write(dsi, 0x18, LOOP_DIV_LOW_SEL(dsi->feedback_div) |
  347. LOW_PROGRAM_EN);
  348. dw_mipi_dsi_phy_write(dsi, 0x18, LOOP_DIV_HIGH_SEL(dsi->feedback_div) |
  349. HIGH_PROGRAM_EN);
  350. dw_mipi_dsi_phy_write(dsi, 0x20, POWER_CONTROL | INTERNAL_REG_CURRENT |
  351. BIAS_BLOCK_ON | BANDGAP_ON);
  352. dw_mipi_dsi_phy_write(dsi, 0x21, TER_RESISTOR_LOW | TER_CAL_DONE |
  353. SETRD_MAX | TER_RESISTORS_ON);
  354. dw_mipi_dsi_phy_write(dsi, 0x21, TER_RESISTOR_HIGH | LEVEL_SHIFTERS_ON |
  355. SETRD_MAX | POWER_MANAGE |
  356. TER_RESISTORS_ON);
  357. dw_mipi_dsi_phy_write(dsi, 0x22, LOW_PROGRAM_EN |
  358. BIASEXTR_SEL(BIASEXTR_127_7));
  359. dw_mipi_dsi_phy_write(dsi, 0x22, HIGH_PROGRAM_EN |
  360. BANDGAP_SEL(BANDGAP_96_10));
  361. dw_mipi_dsi_phy_write(dsi, 0x70, TLP_PROGRAM_EN | 0xf);
  362. dw_mipi_dsi_phy_write(dsi, 0x71, THS_PRE_PROGRAM_EN | 0x55);
  363. dw_mipi_dsi_phy_write(dsi, 0x72, THS_ZERO_PROGRAM_EN | 0xa);
  364. dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
  365. PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
  366. ret = readx_poll_timeout(readl, dsi->base + DSI_PHY_STATUS,
  367. val, val & LOCK, 1000, PHY_STATUS_TIMEOUT_US);
  368. if (ret < 0) {
  369. dev_err(dsi->dev, "failed to wait for phy lock state\n");
  370. return ret;
  371. }
  372. ret = readx_poll_timeout(readl, dsi->base + DSI_PHY_STATUS,
  373. val, val & STOP_STATE_CLK_LANE, 1000,
  374. PHY_STATUS_TIMEOUT_US);
  375. if (ret < 0) {
  376. dev_err(dsi->dev,
  377. "failed to wait for phy clk lane stop state\n");
  378. return ret;
  379. }
  380. return ret;
  381. }
  382. static int dw_mipi_dsi_get_lane_bps(struct dw_mipi_dsi *dsi)
  383. {
  384. unsigned int i, pre;
  385. unsigned long mpclk, pllref, tmp;
  386. unsigned int m = 1, n = 1, target_mbps = 1000;
  387. unsigned int max_mbps = dptdin_map[ARRAY_SIZE(dptdin_map) - 1].max_mbps;
  388. int bpp;
  389. bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
  390. if (bpp < 0) {
  391. dev_err(dsi->dev, "failed to get bpp for pixel format %d\n",
  392. dsi->format);
  393. return bpp;
  394. }
  395. mpclk = DIV_ROUND_UP(dsi->mode->clock, MSEC_PER_SEC);
  396. if (mpclk) {
  397. /* take 1 / 0.9, since mbps must big than bandwidth of RGB */
  398. tmp = mpclk * (bpp / dsi->lanes) * 10 / 9;
  399. if (tmp < max_mbps)
  400. target_mbps = tmp;
  401. else
  402. dev_err(dsi->dev, "DPHY clock frequency is out of range\n");
  403. }
  404. pllref = DIV_ROUND_UP(clk_get_rate(dsi->pllref_clk), USEC_PER_SEC);
  405. tmp = pllref;
  406. for (i = 1; i < 6; i++) {
  407. pre = pllref / i;
  408. if ((tmp > (target_mbps % pre)) && (target_mbps / pre < 512)) {
  409. tmp = target_mbps % pre;
  410. n = i;
  411. m = target_mbps / pre;
  412. }
  413. if (tmp == 0)
  414. break;
  415. }
  416. dsi->lane_mbps = pllref / n * m;
  417. dsi->input_div = n;
  418. dsi->feedback_div = m;
  419. return 0;
  420. }
  421. static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
  422. struct mipi_dsi_device *device)
  423. {
  424. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  425. if (device->lanes > dsi->pdata->max_data_lanes) {
  426. dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
  427. device->lanes);
  428. return -EINVAL;
  429. }
  430. if (!(device->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) ||
  431. !(device->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)) {
  432. dev_err(dsi->dev, "device mode is unsupported\n");
  433. return -EINVAL;
  434. }
  435. dsi->lanes = device->lanes;
  436. dsi->channel = device->channel;
  437. dsi->format = device->format;
  438. dsi->panel = of_drm_find_panel(device->dev.of_node);
  439. if (dsi->panel)
  440. return drm_panel_attach(dsi->panel, &dsi->connector);
  441. return -EINVAL;
  442. }
  443. static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
  444. struct mipi_dsi_device *device)
  445. {
  446. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  447. drm_panel_detach(dsi->panel);
  448. return 0;
  449. }
  450. static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 val)
  451. {
  452. int ret;
  453. ret = readx_poll_timeout(readl, dsi->base + DSI_CMD_PKT_STATUS,
  454. val, !(val & GEN_CMD_FULL), 1000,
  455. CMD_PKT_STATUS_TIMEOUT_US);
  456. if (ret < 0) {
  457. dev_err(dsi->dev, "failed to get available command FIFO\n");
  458. return ret;
  459. }
  460. dsi_write(dsi, DSI_GEN_HDR, val);
  461. ret = readx_poll_timeout(readl, dsi->base + DSI_CMD_PKT_STATUS,
  462. val, val & (GEN_CMD_EMPTY | GEN_PLD_W_EMPTY),
  463. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  464. if (ret < 0) {
  465. dev_err(dsi->dev, "failed to write command FIFO\n");
  466. return ret;
  467. }
  468. return 0;
  469. }
  470. static int dw_mipi_dsi_dcs_short_write(struct dw_mipi_dsi *dsi,
  471. const struct mipi_dsi_msg *msg)
  472. {
  473. const u16 *tx_buf = msg->tx_buf;
  474. u32 val = GEN_HDATA(*tx_buf) | GEN_HTYPE(msg->type);
  475. if (msg->tx_len > 2) {
  476. dev_err(dsi->dev, "too long tx buf length %zu for short write\n",
  477. msg->tx_len);
  478. return -EINVAL;
  479. }
  480. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, val);
  481. }
  482. static int dw_mipi_dsi_dcs_long_write(struct dw_mipi_dsi *dsi,
  483. const struct mipi_dsi_msg *msg)
  484. {
  485. const u32 *tx_buf = msg->tx_buf;
  486. int len = msg->tx_len, pld_data_bytes = sizeof(*tx_buf), ret;
  487. u32 val = GEN_HDATA(msg->tx_len) | GEN_HTYPE(msg->type);
  488. u32 remainder = 0;
  489. if (msg->tx_len < 3) {
  490. dev_err(dsi->dev, "wrong tx buf length %zu for long write\n",
  491. msg->tx_len);
  492. return -EINVAL;
  493. }
  494. while (DIV_ROUND_UP(len, pld_data_bytes)) {
  495. if (len < pld_data_bytes) {
  496. memcpy(&remainder, tx_buf, len);
  497. dsi_write(dsi, DSI_GEN_PLD_DATA, remainder);
  498. len = 0;
  499. } else {
  500. dsi_write(dsi, DSI_GEN_PLD_DATA, *tx_buf);
  501. tx_buf++;
  502. len -= pld_data_bytes;
  503. }
  504. ret = readx_poll_timeout(readl, dsi->base + DSI_CMD_PKT_STATUS,
  505. val, !(val & GEN_PLD_W_FULL), 1000,
  506. CMD_PKT_STATUS_TIMEOUT_US);
  507. if (ret < 0) {
  508. dev_err(dsi->dev,
  509. "failed to get available write payload FIFO\n");
  510. return ret;
  511. }
  512. }
  513. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, val);
  514. }
  515. static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
  516. const struct mipi_dsi_msg *msg)
  517. {
  518. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  519. int ret;
  520. switch (msg->type) {
  521. case MIPI_DSI_DCS_SHORT_WRITE:
  522. case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
  523. case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
  524. ret = dw_mipi_dsi_dcs_short_write(dsi, msg);
  525. break;
  526. case MIPI_DSI_DCS_LONG_WRITE:
  527. ret = dw_mipi_dsi_dcs_long_write(dsi, msg);
  528. break;
  529. default:
  530. dev_err(dsi->dev, "unsupported message type\n");
  531. ret = -EINVAL;
  532. }
  533. return ret;
  534. }
  535. static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
  536. .attach = dw_mipi_dsi_host_attach,
  537. .detach = dw_mipi_dsi_host_detach,
  538. .transfer = dw_mipi_dsi_host_transfer,
  539. };
  540. static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
  541. {
  542. u32 val;
  543. val = VID_MODE_TYPE_BURST_SYNC_PULSES | ENABLE_LOW_POWER;
  544. dsi_write(dsi, DSI_VID_MODE_CFG, val);
  545. }
  546. static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
  547. enum dw_mipi_dsi_mode mode)
  548. {
  549. if (mode == DW_MIPI_DSI_CMD_MODE) {
  550. dsi_write(dsi, DSI_PWR_UP, RESET);
  551. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  552. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  553. } else {
  554. dsi_write(dsi, DSI_PWR_UP, RESET);
  555. dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
  556. dw_mipi_dsi_video_mode_config(dsi);
  557. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  558. }
  559. }
  560. static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
  561. {
  562. dsi_write(dsi, DSI_PWR_UP, RESET);
  563. dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
  564. }
  565. static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
  566. {
  567. dsi_write(dsi, DSI_PWR_UP, RESET);
  568. dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
  569. | PHY_RSTZ | PHY_SHUTDOWNZ);
  570. dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVIDSION(10) |
  571. TX_ESC_CLK_DIVIDSION(7));
  572. dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
  573. }
  574. static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
  575. struct drm_display_mode *mode)
  576. {
  577. u32 val = 0, color = 0;
  578. switch (dsi->format) {
  579. case MIPI_DSI_FMT_RGB888:
  580. color = DPI_COLOR_CODING_24BIT;
  581. break;
  582. case MIPI_DSI_FMT_RGB666:
  583. color = DPI_COLOR_CODING_18BIT_2 | EN18_LOOSELY;
  584. break;
  585. case MIPI_DSI_FMT_RGB666_PACKED:
  586. color = DPI_COLOR_CODING_18BIT_1;
  587. break;
  588. case MIPI_DSI_FMT_RGB565:
  589. color = DPI_COLOR_CODING_16BIT_1;
  590. break;
  591. }
  592. if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
  593. val |= VSYNC_ACTIVE_LOW;
  594. if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
  595. val |= HSYNC_ACTIVE_LOW;
  596. dsi_write(dsi, DSI_DPI_VCID, DPI_VID(dsi->channel));
  597. dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
  598. dsi_write(dsi, DSI_DPI_CFG_POL, val);
  599. dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
  600. | INVACT_LPCMD_TIME(4));
  601. }
  602. static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
  603. {
  604. dsi_write(dsi, DSI_PCKHDL_CFG, EN_CRC_RX | EN_ECC_RX | EN_BTA);
  605. }
  606. static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
  607. struct drm_display_mode *mode)
  608. {
  609. dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
  610. }
  611. static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
  612. {
  613. dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
  614. dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
  615. dsi_write(dsi, DSI_CMD_MODE_CFG, CMD_MODE_ALL_LP);
  616. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  617. }
  618. /* Get lane byte clock cycles. */
  619. static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
  620. u32 hcomponent)
  621. {
  622. u32 frac, lbcc;
  623. lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
  624. frac = lbcc % dsi->mode->clock;
  625. lbcc = lbcc / dsi->mode->clock;
  626. if (frac)
  627. lbcc++;
  628. return lbcc;
  629. }
  630. static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi)
  631. {
  632. u32 htotal, hsa, hbp, lbcc;
  633. struct drm_display_mode *mode = dsi->mode;
  634. htotal = mode->htotal;
  635. hsa = mode->hsync_end - mode->hsync_start;
  636. hbp = mode->htotal - mode->hsync_end;
  637. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, htotal);
  638. dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
  639. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, hsa);
  640. dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
  641. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, hbp);
  642. dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
  643. }
  644. static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi)
  645. {
  646. u32 vactive, vsa, vfp, vbp;
  647. struct drm_display_mode *mode = dsi->mode;
  648. vactive = mode->vdisplay;
  649. vsa = mode->vsync_end - mode->vsync_start;
  650. vfp = mode->vsync_start - mode->vdisplay;
  651. vbp = mode->vtotal - mode->vsync_end;
  652. dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
  653. dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
  654. dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
  655. dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
  656. }
  657. static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
  658. {
  659. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40)
  660. | PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
  661. dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
  662. | PHY_CLKLP2HS_TIME(0x40));
  663. }
  664. static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
  665. {
  666. dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
  667. N_LANES(dsi->lanes));
  668. }
  669. static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
  670. {
  671. dsi_read(dsi, DSI_INT_ST0);
  672. dsi_read(dsi, DSI_INT_ST1);
  673. dsi_write(dsi, DSI_INT_MSK0, 0);
  674. dsi_write(dsi, DSI_INT_MSK1, 0);
  675. }
  676. static void dw_mipi_dsi_encoder_mode_set(struct drm_encoder *encoder,
  677. struct drm_display_mode *mode,
  678. struct drm_display_mode *adjusted_mode)
  679. {
  680. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  681. int ret;
  682. dsi->mode = adjusted_mode;
  683. ret = dw_mipi_dsi_get_lane_bps(dsi);
  684. if (ret < 0)
  685. return;
  686. if (clk_prepare_enable(dsi->pclk)) {
  687. dev_err(dsi->dev, "%s: Failed to enable pclk\n", __func__);
  688. return;
  689. }
  690. dw_mipi_dsi_init(dsi);
  691. dw_mipi_dsi_dpi_config(dsi, mode);
  692. dw_mipi_dsi_packet_handler_config(dsi);
  693. dw_mipi_dsi_video_mode_config(dsi);
  694. dw_mipi_dsi_video_packet_config(dsi, mode);
  695. dw_mipi_dsi_command_mode_config(dsi);
  696. dw_mipi_dsi_line_timer_config(dsi);
  697. dw_mipi_dsi_vertical_timing_config(dsi);
  698. dw_mipi_dsi_dphy_timing_config(dsi);
  699. dw_mipi_dsi_dphy_interface_config(dsi);
  700. dw_mipi_dsi_clear_err(dsi);
  701. if (drm_panel_prepare(dsi->panel))
  702. dev_err(dsi->dev, "failed to prepare panel\n");
  703. clk_disable_unprepare(dsi->pclk);
  704. }
  705. static void dw_mipi_dsi_encoder_disable(struct drm_encoder *encoder)
  706. {
  707. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  708. drm_panel_disable(dsi->panel);
  709. if (clk_prepare_enable(dsi->pclk)) {
  710. dev_err(dsi->dev, "%s: Failed to enable pclk\n", __func__);
  711. return;
  712. }
  713. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_CMD_MODE);
  714. drm_panel_unprepare(dsi->panel);
  715. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_VID_MODE);
  716. /*
  717. * This is necessary to make sure the peripheral will be driven
  718. * normally when the display is enabled again later.
  719. */
  720. msleep(120);
  721. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_CMD_MODE);
  722. dw_mipi_dsi_disable(dsi);
  723. clk_disable_unprepare(dsi->pclk);
  724. }
  725. static void dw_mipi_dsi_encoder_commit(struct drm_encoder *encoder)
  726. {
  727. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  728. int mux = drm_of_encoder_active_endpoint_id(dsi->dev->of_node, encoder);
  729. u32 val;
  730. if (clk_prepare_enable(dsi->pclk)) {
  731. dev_err(dsi->dev, "%s: Failed to enable pclk\n", __func__);
  732. return;
  733. }
  734. dw_mipi_dsi_phy_init(dsi);
  735. dw_mipi_dsi_wait_for_two_frames(dsi);
  736. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_VID_MODE);
  737. drm_panel_enable(dsi->panel);
  738. clk_disable_unprepare(dsi->pclk);
  739. if (mux)
  740. val = DSI0_SEL_VOP_LIT | (DSI0_SEL_VOP_LIT << 16);
  741. else
  742. val = DSI0_SEL_VOP_LIT << 16;
  743. regmap_write(dsi->grf_regmap, GRF_SOC_CON6, val);
  744. dev_dbg(dsi->dev, "vop %s output to dsi0\n", (mux) ? "LIT" : "BIG");
  745. }
  746. static int
  747. dw_mipi_dsi_encoder_atomic_check(struct drm_encoder *encoder,
  748. struct drm_crtc_state *crtc_state,
  749. struct drm_connector_state *conn_state)
  750. {
  751. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  752. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  753. switch (dsi->format) {
  754. case MIPI_DSI_FMT_RGB888:
  755. s->output_mode = ROCKCHIP_OUT_MODE_P888;
  756. break;
  757. case MIPI_DSI_FMT_RGB666:
  758. s->output_mode = ROCKCHIP_OUT_MODE_P666;
  759. break;
  760. case MIPI_DSI_FMT_RGB565:
  761. s->output_mode = ROCKCHIP_OUT_MODE_P565;
  762. break;
  763. default:
  764. WARN_ON(1);
  765. return -EINVAL;
  766. }
  767. s->output_type = DRM_MODE_CONNECTOR_DSI;
  768. return 0;
  769. }
  770. static struct drm_encoder_helper_funcs
  771. dw_mipi_dsi_encoder_helper_funcs = {
  772. .commit = dw_mipi_dsi_encoder_commit,
  773. .mode_set = dw_mipi_dsi_encoder_mode_set,
  774. .disable = dw_mipi_dsi_encoder_disable,
  775. .atomic_check = dw_mipi_dsi_encoder_atomic_check,
  776. };
  777. static struct drm_encoder_funcs dw_mipi_dsi_encoder_funcs = {
  778. .destroy = drm_encoder_cleanup,
  779. };
  780. static int dw_mipi_dsi_connector_get_modes(struct drm_connector *connector)
  781. {
  782. struct dw_mipi_dsi *dsi = con_to_dsi(connector);
  783. return drm_panel_get_modes(dsi->panel);
  784. }
  785. static enum drm_mode_status dw_mipi_dsi_mode_valid(
  786. struct drm_connector *connector,
  787. struct drm_display_mode *mode)
  788. {
  789. struct dw_mipi_dsi *dsi = con_to_dsi(connector);
  790. enum drm_mode_status mode_status = MODE_OK;
  791. if (dsi->pdata->mode_valid)
  792. mode_status = dsi->pdata->mode_valid(connector, mode);
  793. return mode_status;
  794. }
  795. static struct drm_connector_helper_funcs dw_mipi_dsi_connector_helper_funcs = {
  796. .get_modes = dw_mipi_dsi_connector_get_modes,
  797. .mode_valid = dw_mipi_dsi_mode_valid,
  798. };
  799. static enum drm_connector_status
  800. dw_mipi_dsi_detect(struct drm_connector *connector, bool force)
  801. {
  802. return connector_status_connected;
  803. }
  804. static void dw_mipi_dsi_drm_connector_destroy(struct drm_connector *connector)
  805. {
  806. drm_connector_unregister(connector);
  807. drm_connector_cleanup(connector);
  808. }
  809. static struct drm_connector_funcs dw_mipi_dsi_atomic_connector_funcs = {
  810. .dpms = drm_atomic_helper_connector_dpms,
  811. .fill_modes = drm_helper_probe_single_connector_modes,
  812. .detect = dw_mipi_dsi_detect,
  813. .destroy = dw_mipi_dsi_drm_connector_destroy,
  814. .reset = drm_atomic_helper_connector_reset,
  815. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  816. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  817. };
  818. static int dw_mipi_dsi_register(struct drm_device *drm,
  819. struct dw_mipi_dsi *dsi)
  820. {
  821. struct drm_encoder *encoder = &dsi->encoder;
  822. struct drm_connector *connector = &dsi->connector;
  823. struct device *dev = dsi->dev;
  824. int ret;
  825. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm,
  826. dev->of_node);
  827. /*
  828. * If we failed to find the CRTC(s) which this encoder is
  829. * supposed to be connected to, it's because the CRTC has
  830. * not been registered yet. Defer probing, and hope that
  831. * the required CRTC is added later.
  832. */
  833. if (encoder->possible_crtcs == 0)
  834. return -EPROBE_DEFER;
  835. drm_encoder_helper_add(&dsi->encoder,
  836. &dw_mipi_dsi_encoder_helper_funcs);
  837. ret = drm_encoder_init(drm, &dsi->encoder, &dw_mipi_dsi_encoder_funcs,
  838. DRM_MODE_ENCODER_DSI, NULL);
  839. if (ret) {
  840. dev_err(dev, "Failed to initialize encoder with drm\n");
  841. return ret;
  842. }
  843. drm_connector_helper_add(connector,
  844. &dw_mipi_dsi_connector_helper_funcs);
  845. drm_connector_init(drm, &dsi->connector,
  846. &dw_mipi_dsi_atomic_connector_funcs,
  847. DRM_MODE_CONNECTOR_DSI);
  848. drm_mode_connector_attach_encoder(connector, encoder);
  849. return 0;
  850. }
  851. static int rockchip_mipi_parse_dt(struct dw_mipi_dsi *dsi)
  852. {
  853. struct device_node *np = dsi->dev->of_node;
  854. dsi->grf_regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  855. if (IS_ERR(dsi->grf_regmap)) {
  856. dev_err(dsi->dev, "Unable to get rockchip,grf\n");
  857. return PTR_ERR(dsi->grf_regmap);
  858. }
  859. return 0;
  860. }
  861. static enum drm_mode_status rk3288_mipi_dsi_mode_valid(
  862. struct drm_connector *connector,
  863. struct drm_display_mode *mode)
  864. {
  865. /*
  866. * The VID_PKT_SIZE field in the DSI_VID_PKT_CFG
  867. * register is 11-bit.
  868. */
  869. if (mode->hdisplay > 0x7ff)
  870. return MODE_BAD_HVALUE;
  871. /*
  872. * The V_ACTIVE_LINES field in the DSI_VTIMING_CFG
  873. * register is 11-bit.
  874. */
  875. if (mode->vdisplay > 0x7ff)
  876. return MODE_BAD_VVALUE;
  877. return MODE_OK;
  878. }
  879. static struct dw_mipi_dsi_plat_data rk3288_mipi_dsi_drv_data = {
  880. .max_data_lanes = 4,
  881. .mode_valid = rk3288_mipi_dsi_mode_valid,
  882. };
  883. static const struct of_device_id dw_mipi_dsi_dt_ids[] = {
  884. {
  885. .compatible = "rockchip,rk3288-mipi-dsi",
  886. .data = &rk3288_mipi_dsi_drv_data,
  887. },
  888. { /* sentinel */ }
  889. };
  890. MODULE_DEVICE_TABLE(of, dw_mipi_dsi_dt_ids);
  891. static int dw_mipi_dsi_bind(struct device *dev, struct device *master,
  892. void *data)
  893. {
  894. const struct of_device_id *of_id =
  895. of_match_device(dw_mipi_dsi_dt_ids, dev);
  896. const struct dw_mipi_dsi_plat_data *pdata = of_id->data;
  897. struct platform_device *pdev = to_platform_device(dev);
  898. struct drm_device *drm = data;
  899. struct dw_mipi_dsi *dsi;
  900. struct resource *res;
  901. int ret;
  902. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  903. if (!dsi)
  904. return -ENOMEM;
  905. dsi->dev = dev;
  906. dsi->pdata = pdata;
  907. ret = rockchip_mipi_parse_dt(dsi);
  908. if (ret)
  909. return ret;
  910. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  911. if (!res)
  912. return -ENODEV;
  913. dsi->base = devm_ioremap_resource(dev, res);
  914. if (IS_ERR(dsi->base))
  915. return PTR_ERR(dsi->base);
  916. dsi->pllref_clk = devm_clk_get(dev, "ref");
  917. if (IS_ERR(dsi->pllref_clk)) {
  918. ret = PTR_ERR(dsi->pllref_clk);
  919. dev_err(dev, "Unable to get pll reference clock: %d\n", ret);
  920. return ret;
  921. }
  922. dsi->pclk = devm_clk_get(dev, "pclk");
  923. if (IS_ERR(dsi->pclk)) {
  924. ret = PTR_ERR(dsi->pclk);
  925. dev_err(dev, "Unable to get pclk: %d\n", ret);
  926. return ret;
  927. }
  928. ret = clk_prepare_enable(dsi->pllref_clk);
  929. if (ret) {
  930. dev_err(dev, "%s: Failed to enable pllref_clk\n", __func__);
  931. return ret;
  932. }
  933. ret = dw_mipi_dsi_register(drm, dsi);
  934. if (ret) {
  935. dev_err(dev, "Failed to register mipi_dsi: %d\n", ret);
  936. goto err_pllref;
  937. }
  938. dev_set_drvdata(dev, dsi);
  939. dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
  940. dsi->dsi_host.dev = dev;
  941. return mipi_dsi_host_register(&dsi->dsi_host);
  942. err_pllref:
  943. clk_disable_unprepare(dsi->pllref_clk);
  944. return ret;
  945. }
  946. static void dw_mipi_dsi_unbind(struct device *dev, struct device *master,
  947. void *data)
  948. {
  949. struct dw_mipi_dsi *dsi = dev_get_drvdata(dev);
  950. mipi_dsi_host_unregister(&dsi->dsi_host);
  951. clk_disable_unprepare(dsi->pllref_clk);
  952. }
  953. static const struct component_ops dw_mipi_dsi_ops = {
  954. .bind = dw_mipi_dsi_bind,
  955. .unbind = dw_mipi_dsi_unbind,
  956. };
  957. static int dw_mipi_dsi_probe(struct platform_device *pdev)
  958. {
  959. return component_add(&pdev->dev, &dw_mipi_dsi_ops);
  960. }
  961. static int dw_mipi_dsi_remove(struct platform_device *pdev)
  962. {
  963. component_del(&pdev->dev, &dw_mipi_dsi_ops);
  964. return 0;
  965. }
  966. static struct platform_driver dw_mipi_dsi_driver = {
  967. .probe = dw_mipi_dsi_probe,
  968. .remove = dw_mipi_dsi_remove,
  969. .driver = {
  970. .of_match_table = dw_mipi_dsi_dt_ids,
  971. .name = DRIVER_NAME,
  972. },
  973. };
  974. module_platform_driver(dw_mipi_dsi_driver);
  975. MODULE_DESCRIPTION("ROCKCHIP MIPI DSI host controller driver");
  976. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  977. MODULE_LICENSE("GPL");
  978. MODULE_ALIAS("platform:" DRIVER_NAME);