cdns-dsi.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright: 2017 Cadence Design Systems, Inc.
  4. *
  5. * Author: Boris Brezillon <boris.brezillon@bootlin.com>
  6. */
  7. #include <drm/drm_atomic_helper.h>
  8. #include <drm/drm_bridge.h>
  9. #include <drm/drm_crtc_helper.h>
  10. #include <drm/drm_mipi_dsi.h>
  11. #include <drm/drm_panel.h>
  12. #include <video/mipi_display.h>
  13. #include <linux/clk.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/module.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_graph.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/reset.h>
  21. #define IP_CONF 0x0
  22. #define SP_HS_FIFO_DEPTH(x) (((x) & GENMASK(30, 26)) >> 26)
  23. #define SP_LP_FIFO_DEPTH(x) (((x) & GENMASK(25, 21)) >> 21)
  24. #define VRS_FIFO_DEPTH(x) (((x) & GENMASK(20, 16)) >> 16)
  25. #define DIRCMD_FIFO_DEPTH(x) (((x) & GENMASK(15, 13)) >> 13)
  26. #define SDI_IFACE_32 BIT(12)
  27. #define INTERNAL_DATAPATH_32 (0 << 10)
  28. #define INTERNAL_DATAPATH_16 (1 << 10)
  29. #define INTERNAL_DATAPATH_8 (3 << 10)
  30. #define INTERNAL_DATAPATH_SIZE ((x) & GENMASK(11, 10))
  31. #define NUM_IFACE(x) ((((x) & GENMASK(9, 8)) >> 8) + 1)
  32. #define MAX_LANE_NB(x) (((x) & GENMASK(7, 6)) >> 6)
  33. #define RX_FIFO_DEPTH(x) ((x) & GENMASK(5, 0))
  34. #define MCTL_MAIN_DATA_CTL 0x4
  35. #define TE_MIPI_POLLING_EN BIT(25)
  36. #define TE_HW_POLLING_EN BIT(24)
  37. #define DISP_EOT_GEN BIT(18)
  38. #define HOST_EOT_GEN BIT(17)
  39. #define DISP_GEN_CHECKSUM BIT(16)
  40. #define DISP_GEN_ECC BIT(15)
  41. #define BTA_EN BIT(14)
  42. #define READ_EN BIT(13)
  43. #define REG_TE_EN BIT(12)
  44. #define IF_TE_EN(x) BIT(8 + (x))
  45. #define TVG_SEL BIT(6)
  46. #define VID_EN BIT(5)
  47. #define IF_VID_SELECT(x) ((x) << 2)
  48. #define IF_VID_SELECT_MASK GENMASK(3, 2)
  49. #define IF_VID_MODE BIT(1)
  50. #define LINK_EN BIT(0)
  51. #define MCTL_MAIN_PHY_CTL 0x8
  52. #define HS_INVERT_DAT(x) BIT(19 + ((x) * 2))
  53. #define SWAP_PINS_DAT(x) BIT(18 + ((x) * 2))
  54. #define HS_INVERT_CLK BIT(17)
  55. #define SWAP_PINS_CLK BIT(16)
  56. #define HS_SKEWCAL_EN BIT(15)
  57. #define WAIT_BURST_TIME(x) ((x) << 10)
  58. #define DATA_ULPM_EN(x) BIT(6 + (x))
  59. #define CLK_ULPM_EN BIT(5)
  60. #define CLK_CONTINUOUS BIT(4)
  61. #define DATA_LANE_EN(x) BIT((x) - 1)
  62. #define MCTL_MAIN_EN 0xc
  63. #define DATA_FORCE_STOP BIT(17)
  64. #define CLK_FORCE_STOP BIT(16)
  65. #define IF_EN(x) BIT(13 + (x))
  66. #define DATA_LANE_ULPM_REQ(l) BIT(9 + (l))
  67. #define CLK_LANE_ULPM_REQ BIT(8)
  68. #define DATA_LANE_START(x) BIT(4 + (x))
  69. #define CLK_LANE_EN BIT(3)
  70. #define PLL_START BIT(0)
  71. #define MCTL_DPHY_CFG0 0x10
  72. #define DPHY_C_RSTB BIT(20)
  73. #define DPHY_D_RSTB(x) GENMASK(15 + (x), 16)
  74. #define DPHY_PLL_PDN BIT(10)
  75. #define DPHY_CMN_PDN BIT(9)
  76. #define DPHY_C_PDN BIT(8)
  77. #define DPHY_D_PDN(x) GENMASK(3 + (x), 4)
  78. #define DPHY_ALL_D_PDN GENMASK(7, 4)
  79. #define DPHY_PLL_PSO BIT(1)
  80. #define DPHY_CMN_PSO BIT(0)
  81. #define MCTL_DPHY_TIMEOUT1 0x14
  82. #define HSTX_TIMEOUT(x) ((x) << 4)
  83. #define HSTX_TIMEOUT_MAX GENMASK(17, 0)
  84. #define CLK_DIV(x) (x)
  85. #define CLK_DIV_MAX GENMASK(3, 0)
  86. #define MCTL_DPHY_TIMEOUT2 0x18
  87. #define LPRX_TIMEOUT(x) (x)
  88. #define MCTL_ULPOUT_TIME 0x1c
  89. #define DATA_LANE_ULPOUT_TIME(x) ((x) << 9)
  90. #define CLK_LANE_ULPOUT_TIME(x) (x)
  91. #define MCTL_3DVIDEO_CTL 0x20
  92. #define VID_VSYNC_3D_EN BIT(7)
  93. #define VID_VSYNC_3D_LR BIT(5)
  94. #define VID_VSYNC_3D_SECOND_EN BIT(4)
  95. #define VID_VSYNC_3DFORMAT_LINE (0 << 2)
  96. #define VID_VSYNC_3DFORMAT_FRAME (1 << 2)
  97. #define VID_VSYNC_3DFORMAT_PIXEL (2 << 2)
  98. #define VID_VSYNC_3DMODE_OFF 0
  99. #define VID_VSYNC_3DMODE_PORTRAIT 1
  100. #define VID_VSYNC_3DMODE_LANDSCAPE 2
  101. #define MCTL_MAIN_STS 0x24
  102. #define MCTL_MAIN_STS_CTL 0x130
  103. #define MCTL_MAIN_STS_CLR 0x150
  104. #define MCTL_MAIN_STS_FLAG 0x170
  105. #define HS_SKEWCAL_DONE BIT(11)
  106. #define IF_UNTERM_PKT_ERR(x) BIT(8 + (x))
  107. #define LPRX_TIMEOUT_ERR BIT(7)
  108. #define HSTX_TIMEOUT_ERR BIT(6)
  109. #define DATA_LANE_RDY(l) BIT(2 + (l))
  110. #define CLK_LANE_RDY BIT(1)
  111. #define PLL_LOCKED BIT(0)
  112. #define MCTL_DPHY_ERR 0x28
  113. #define MCTL_DPHY_ERR_CTL1 0x148
  114. #define MCTL_DPHY_ERR_CLR 0x168
  115. #define MCTL_DPHY_ERR_FLAG 0x188
  116. #define ERR_CONT_LP(x, l) BIT(18 + ((x) * 4) + (l))
  117. #define ERR_CONTROL(l) BIT(14 + (l))
  118. #define ERR_SYNESC(l) BIT(10 + (l))
  119. #define ERR_ESC(l) BIT(6 + (l))
  120. #define MCTL_DPHY_ERR_CTL2 0x14c
  121. #define ERR_CONT_LP_EDGE(x, l) BIT(12 + ((x) * 4) + (l))
  122. #define ERR_CONTROL_EDGE(l) BIT(8 + (l))
  123. #define ERR_SYN_ESC_EDGE(l) BIT(4 + (l))
  124. #define ERR_ESC_EDGE(l) BIT(0 + (l))
  125. #define MCTL_LANE_STS 0x2c
  126. #define PPI_C_TX_READY_HS BIT(18)
  127. #define DPHY_PLL_LOCK BIT(17)
  128. #define PPI_D_RX_ULPS_ESC(x) (((x) & GENMASK(15, 12)) >> 12)
  129. #define LANE_STATE_START 0
  130. #define LANE_STATE_IDLE 1
  131. #define LANE_STATE_WRITE 2
  132. #define LANE_STATE_ULPM 3
  133. #define LANE_STATE_READ 4
  134. #define DATA_LANE_STATE(l, val) \
  135. (((val) >> (2 + 2 * (l) + ((l) ? 1 : 0))) & GENMASK((l) ? 1 : 2, 0))
  136. #define CLK_LANE_STATE_HS 2
  137. #define CLK_LANE_STATE(val) ((val) & GENMASK(1, 0))
  138. #define DSC_MODE_CTL 0x30
  139. #define DSC_MODE_EN BIT(0)
  140. #define DSC_CMD_SEND 0x34
  141. #define DSC_SEND_PPS BIT(0)
  142. #define DSC_EXECUTE_QUEUE BIT(1)
  143. #define DSC_PPS_WRDAT 0x38
  144. #define DSC_MODE_STS 0x3c
  145. #define DSC_PPS_DONE BIT(1)
  146. #define DSC_EXEC_DONE BIT(2)
  147. #define CMD_MODE_CTL 0x70
  148. #define IF_LP_EN(x) BIT(9 + (x))
  149. #define IF_VCHAN_ID(x, c) ((c) << ((x) * 2))
  150. #define CMD_MODE_CTL2 0x74
  151. #define TE_TIMEOUT(x) ((x) << 11)
  152. #define FILL_VALUE(x) ((x) << 3)
  153. #define ARB_IF_WITH_HIGHEST_PRIORITY(x) ((x) << 1)
  154. #define ARB_ROUND_ROBIN_MODE BIT(0)
  155. #define CMD_MODE_STS 0x78
  156. #define CMD_MODE_STS_CTL 0x134
  157. #define CMD_MODE_STS_CLR 0x154
  158. #define CMD_MODE_STS_FLAG 0x174
  159. #define ERR_IF_UNDERRUN(x) BIT(4 + (x))
  160. #define ERR_UNWANTED_READ BIT(3)
  161. #define ERR_TE_MISS BIT(2)
  162. #define ERR_NO_TE BIT(1)
  163. #define CSM_RUNNING BIT(0)
  164. #define DIRECT_CMD_SEND 0x80
  165. #define DIRECT_CMD_MAIN_SETTINGS 0x84
  166. #define TRIGGER_VAL(x) ((x) << 25)
  167. #define CMD_LP_EN BIT(24)
  168. #define CMD_SIZE(x) ((x) << 16)
  169. #define CMD_VCHAN_ID(x) ((x) << 14)
  170. #define CMD_DATATYPE(x) ((x) << 8)
  171. #define CMD_LONG BIT(3)
  172. #define WRITE_CMD 0
  173. #define READ_CMD 1
  174. #define TE_REQ 4
  175. #define TRIGGER_REQ 5
  176. #define BTA_REQ 6
  177. #define DIRECT_CMD_STS 0x88
  178. #define DIRECT_CMD_STS_CTL 0x138
  179. #define DIRECT_CMD_STS_CLR 0x158
  180. #define DIRECT_CMD_STS_FLAG 0x178
  181. #define RCVD_ACK_VAL(val) ((val) >> 16)
  182. #define RCVD_TRIGGER_VAL(val) (((val) & GENMASK(14, 11)) >> 11)
  183. #define READ_COMPLETED_WITH_ERR BIT(10)
  184. #define BTA_FINISHED BIT(9)
  185. #define BTA_COMPLETED BIT(8)
  186. #define TE_RCVD BIT(7)
  187. #define TRIGGER_RCVD BIT(6)
  188. #define ACK_WITH_ERR_RCVD BIT(5)
  189. #define ACK_RCVD BIT(4)
  190. #define READ_COMPLETED BIT(3)
  191. #define TRIGGER_COMPLETED BIT(2)
  192. #define WRITE_COMPLETED BIT(1)
  193. #define SENDING_CMD BIT(0)
  194. #define DIRECT_CMD_STOP_READ 0x8c
  195. #define DIRECT_CMD_WRDATA 0x90
  196. #define DIRECT_CMD_FIFO_RST 0x94
  197. #define DIRECT_CMD_RDDATA 0xa0
  198. #define DIRECT_CMD_RD_PROPS 0xa4
  199. #define RD_DCS BIT(18)
  200. #define RD_VCHAN_ID(val) (((val) >> 16) & GENMASK(1, 0))
  201. #define RD_SIZE(val) ((val) & GENMASK(15, 0))
  202. #define DIRECT_CMD_RD_STS 0xa8
  203. #define DIRECT_CMD_RD_STS_CTL 0x13c
  204. #define DIRECT_CMD_RD_STS_CLR 0x15c
  205. #define DIRECT_CMD_RD_STS_FLAG 0x17c
  206. #define ERR_EOT_WITH_ERR BIT(8)
  207. #define ERR_MISSING_EOT BIT(7)
  208. #define ERR_WRONG_LENGTH BIT(6)
  209. #define ERR_OVERSIZE BIT(5)
  210. #define ERR_RECEIVE BIT(4)
  211. #define ERR_UNDECODABLE BIT(3)
  212. #define ERR_CHECKSUM BIT(2)
  213. #define ERR_UNCORRECTABLE BIT(1)
  214. #define ERR_FIXED BIT(0)
  215. #define VID_MAIN_CTL 0xb0
  216. #define VID_IGNORE_MISS_VSYNC BIT(31)
  217. #define VID_FIELD_SW BIT(28)
  218. #define VID_INTERLACED_EN BIT(27)
  219. #define RECOVERY_MODE(x) ((x) << 25)
  220. #define RECOVERY_MODE_NEXT_HSYNC 0
  221. #define RECOVERY_MODE_NEXT_STOP_POINT 2
  222. #define RECOVERY_MODE_NEXT_VSYNC 3
  223. #define REG_BLKEOL_MODE(x) ((x) << 23)
  224. #define REG_BLKLINE_MODE(x) ((x) << 21)
  225. #define REG_BLK_MODE_NULL_PKT 0
  226. #define REG_BLK_MODE_BLANKING_PKT 1
  227. #define REG_BLK_MODE_LP 2
  228. #define SYNC_PULSE_HORIZONTAL BIT(20)
  229. #define SYNC_PULSE_ACTIVE BIT(19)
  230. #define BURST_MODE BIT(18)
  231. #define VID_PIXEL_MODE_MASK GENMASK(17, 14)
  232. #define VID_PIXEL_MODE_RGB565 (0 << 14)
  233. #define VID_PIXEL_MODE_RGB666_PACKED (1 << 14)
  234. #define VID_PIXEL_MODE_RGB666 (2 << 14)
  235. #define VID_PIXEL_MODE_RGB888 (3 << 14)
  236. #define VID_PIXEL_MODE_RGB101010 (4 << 14)
  237. #define VID_PIXEL_MODE_RGB121212 (5 << 14)
  238. #define VID_PIXEL_MODE_YUV420 (8 << 14)
  239. #define VID_PIXEL_MODE_YUV422_PACKED (9 << 14)
  240. #define VID_PIXEL_MODE_YUV422 (10 << 14)
  241. #define VID_PIXEL_MODE_YUV422_24B (11 << 14)
  242. #define VID_PIXEL_MODE_DSC_COMP (12 << 14)
  243. #define VID_DATATYPE(x) ((x) << 8)
  244. #define VID_VIRTCHAN_ID(iface, x) ((x) << (4 + (iface) * 2))
  245. #define STOP_MODE(x) ((x) << 2)
  246. #define START_MODE(x) (x)
  247. #define VID_VSIZE1 0xb4
  248. #define VFP_LEN(x) ((x) << 12)
  249. #define VBP_LEN(x) ((x) << 6)
  250. #define VSA_LEN(x) (x)
  251. #define VID_VSIZE2 0xb8
  252. #define VACT_LEN(x) (x)
  253. #define VID_HSIZE1 0xc0
  254. #define HBP_LEN(x) ((x) << 16)
  255. #define HSA_LEN(x) (x)
  256. #define VID_HSIZE2 0xc4
  257. #define HFP_LEN(x) ((x) << 16)
  258. #define HACT_LEN(x) (x)
  259. #define VID_BLKSIZE1 0xcc
  260. #define BLK_EOL_PKT_LEN(x) ((x) << 15)
  261. #define BLK_LINE_EVENT_PKT_LEN(x) (x)
  262. #define VID_BLKSIZE2 0xd0
  263. #define BLK_LINE_PULSE_PKT_LEN(x) (x)
  264. #define VID_PKT_TIME 0xd8
  265. #define BLK_EOL_DURATION(x) (x)
  266. #define VID_DPHY_TIME 0xdc
  267. #define REG_WAKEUP_TIME(x) ((x) << 17)
  268. #define REG_LINE_DURATION(x) (x)
  269. #define VID_ERR_COLOR1 0xe0
  270. #define COL_GREEN(x) ((x) << 12)
  271. #define COL_RED(x) (x)
  272. #define VID_ERR_COLOR2 0xe4
  273. #define PAD_VAL(x) ((x) << 12)
  274. #define COL_BLUE(x) (x)
  275. #define VID_VPOS 0xe8
  276. #define LINE_VAL(val) (((val) & GENMASK(14, 2)) >> 2)
  277. #define LINE_POS(val) ((val) & GENMASK(1, 0))
  278. #define VID_HPOS 0xec
  279. #define HORIZ_VAL(val) (((val) & GENMASK(17, 3)) >> 3)
  280. #define HORIZ_POS(val) ((val) & GENMASK(2, 0))
  281. #define VID_MODE_STS 0xf0
  282. #define VID_MODE_STS_CTL 0x140
  283. #define VID_MODE_STS_CLR 0x160
  284. #define VID_MODE_STS_FLAG 0x180
  285. #define VSG_RECOVERY BIT(10)
  286. #define ERR_VRS_WRONG_LEN BIT(9)
  287. #define ERR_LONG_READ BIT(8)
  288. #define ERR_LINE_WRITE BIT(7)
  289. #define ERR_BURST_WRITE BIT(6)
  290. #define ERR_SMALL_HEIGHT BIT(5)
  291. #define ERR_SMALL_LEN BIT(4)
  292. #define ERR_MISSING_VSYNC BIT(3)
  293. #define ERR_MISSING_HSYNC BIT(2)
  294. #define ERR_MISSING_DATA BIT(1)
  295. #define VSG_RUNNING BIT(0)
  296. #define VID_VCA_SETTING1 0xf4
  297. #define BURST_LP BIT(16)
  298. #define MAX_BURST_LIMIT(x) (x)
  299. #define VID_VCA_SETTING2 0xf8
  300. #define MAX_LINE_LIMIT(x) ((x) << 16)
  301. #define EXACT_BURST_LIMIT(x) (x)
  302. #define TVG_CTL 0xfc
  303. #define TVG_STRIPE_SIZE(x) ((x) << 5)
  304. #define TVG_MODE_MASK GENMASK(4, 3)
  305. #define TVG_MODE_SINGLE_COLOR (0 << 3)
  306. #define TVG_MODE_VSTRIPES (2 << 3)
  307. #define TVG_MODE_HSTRIPES (3 << 3)
  308. #define TVG_STOPMODE_MASK GENMASK(2, 1)
  309. #define TVG_STOPMODE_EOF (0 << 1)
  310. #define TVG_STOPMODE_EOL (1 << 1)
  311. #define TVG_STOPMODE_NOW (2 << 1)
  312. #define TVG_RUN BIT(0)
  313. #define TVG_IMG_SIZE 0x100
  314. #define TVG_NBLINES(x) ((x) << 16)
  315. #define TVG_LINE_SIZE(x) (x)
  316. #define TVG_COLOR1 0x104
  317. #define TVG_COL1_GREEN(x) ((x) << 12)
  318. #define TVG_COL1_RED(x) (x)
  319. #define TVG_COLOR1_BIS 0x108
  320. #define TVG_COL1_BLUE(x) (x)
  321. #define TVG_COLOR2 0x10c
  322. #define TVG_COL2_GREEN(x) ((x) << 12)
  323. #define TVG_COL2_RED(x) (x)
  324. #define TVG_COLOR2_BIS 0x110
  325. #define TVG_COL2_BLUE(x) (x)
  326. #define TVG_STS 0x114
  327. #define TVG_STS_CTL 0x144
  328. #define TVG_STS_CLR 0x164
  329. #define TVG_STS_FLAG 0x184
  330. #define TVG_STS_RUNNING BIT(0)
  331. #define STS_CTL_EDGE(e) ((e) << 16)
  332. #define DPHY_LANES_MAP 0x198
  333. #define DAT_REMAP_CFG(b, l) ((l) << ((b) * 8))
  334. #define DPI_IRQ_EN 0x1a0
  335. #define DPI_IRQ_CLR 0x1a4
  336. #define DPI_IRQ_STS 0x1a8
  337. #define PIXEL_BUF_OVERFLOW BIT(0)
  338. #define DPI_CFG 0x1ac
  339. #define DPI_CFG_FIFO_DEPTH(x) ((x) >> 16)
  340. #define DPI_CFG_FIFO_LEVEL(x) ((x) & GENMASK(15, 0))
  341. #define TEST_GENERIC 0x1f0
  342. #define TEST_STATUS(x) ((x) >> 16)
  343. #define TEST_CTRL(x) (x)
  344. #define ID_REG 0x1fc
  345. #define REV_VENDOR_ID(x) (((x) & GENMASK(31, 20)) >> 20)
  346. #define REV_PRODUCT_ID(x) (((x) & GENMASK(19, 12)) >> 12)
  347. #define REV_HW(x) (((x) & GENMASK(11, 8)) >> 8)
  348. #define REV_MAJOR(x) (((x) & GENMASK(7, 4)) >> 4)
  349. #define REV_MINOR(x) ((x) & GENMASK(3, 0))
  350. #define DSI_OUTPUT_PORT 0
  351. #define DSI_INPUT_PORT(inputid) (1 + (inputid))
  352. #define DSI_HBP_FRAME_OVERHEAD 12
  353. #define DSI_HSA_FRAME_OVERHEAD 14
  354. #define DSI_HFP_FRAME_OVERHEAD 6
  355. #define DSI_HSS_VSS_VSE_FRAME_OVERHEAD 4
  356. #define DSI_BLANKING_FRAME_OVERHEAD 6
  357. #define DSI_NULL_FRAME_OVERHEAD 6
  358. #define DSI_EOT_PKT_SIZE 4
  359. #define REG_WAKEUP_TIME_NS 800
  360. #define DPHY_PLL_RATE_HZ 108000000
  361. /* DPHY registers */
  362. #define DPHY_PMA_CMN(reg) (reg)
  363. #define DPHY_PMA_LCLK(reg) (0x100 + (reg))
  364. #define DPHY_PMA_LDATA(lane, reg) (0x200 + ((lane) * 0x100) + (reg))
  365. #define DPHY_PMA_RCLK(reg) (0x600 + (reg))
  366. #define DPHY_PMA_RDATA(lane, reg) (0x700 + ((lane) * 0x100) + (reg))
  367. #define DPHY_PCS(reg) (0xb00 + (reg))
  368. #define DPHY_CMN_SSM DPHY_PMA_CMN(0x20)
  369. #define DPHY_CMN_SSM_EN BIT(0)
  370. #define DPHY_CMN_TX_MODE_EN BIT(9)
  371. #define DPHY_CMN_PWM DPHY_PMA_CMN(0x40)
  372. #define DPHY_CMN_PWM_DIV(x) ((x) << 20)
  373. #define DPHY_CMN_PWM_LOW(x) ((x) << 10)
  374. #define DPHY_CMN_PWM_HIGH(x) (x)
  375. #define DPHY_CMN_FBDIV DPHY_PMA_CMN(0x4c)
  376. #define DPHY_CMN_FBDIV_VAL(low, high) (((high) << 11) | ((low) << 22))
  377. #define DPHY_CMN_FBDIV_FROM_REG (BIT(10) | BIT(21))
  378. #define DPHY_CMN_OPIPDIV DPHY_PMA_CMN(0x50)
  379. #define DPHY_CMN_IPDIV_FROM_REG BIT(0)
  380. #define DPHY_CMN_IPDIV(x) ((x) << 1)
  381. #define DPHY_CMN_OPDIV_FROM_REG BIT(6)
  382. #define DPHY_CMN_OPDIV(x) ((x) << 7)
  383. #define DPHY_PSM_CFG DPHY_PCS(0x4)
  384. #define DPHY_PSM_CFG_FROM_REG BIT(0)
  385. #define DPHY_PSM_CLK_DIV(x) ((x) << 1)
  386. struct cdns_dsi_output {
  387. struct mipi_dsi_device *dev;
  388. struct drm_panel *panel;
  389. struct drm_bridge *bridge;
  390. };
  391. enum cdns_dsi_input_id {
  392. CDNS_SDI_INPUT,
  393. CDNS_DPI_INPUT,
  394. CDNS_DSC_INPUT,
  395. };
  396. struct cdns_dphy_cfg {
  397. u8 pll_ipdiv;
  398. u8 pll_opdiv;
  399. u16 pll_fbdiv;
  400. unsigned long lane_bps;
  401. unsigned int nlanes;
  402. };
  403. struct cdns_dsi_cfg {
  404. unsigned int hfp;
  405. unsigned int hsa;
  406. unsigned int hbp;
  407. unsigned int hact;
  408. unsigned int htotal;
  409. };
  410. struct cdns_dphy;
  411. enum cdns_dphy_clk_lane_cfg {
  412. DPHY_CLK_CFG_LEFT_DRIVES_ALL = 0,
  413. DPHY_CLK_CFG_LEFT_DRIVES_RIGHT = 1,
  414. DPHY_CLK_CFG_LEFT_DRIVES_LEFT = 2,
  415. DPHY_CLK_CFG_RIGHT_DRIVES_ALL = 3,
  416. };
  417. struct cdns_dphy_ops {
  418. int (*probe)(struct cdns_dphy *dphy);
  419. void (*remove)(struct cdns_dphy *dphy);
  420. void (*set_psm_div)(struct cdns_dphy *dphy, u8 div);
  421. void (*set_clk_lane_cfg)(struct cdns_dphy *dphy,
  422. enum cdns_dphy_clk_lane_cfg cfg);
  423. void (*set_pll_cfg)(struct cdns_dphy *dphy,
  424. const struct cdns_dphy_cfg *cfg);
  425. unsigned long (*get_wakeup_time_ns)(struct cdns_dphy *dphy);
  426. };
  427. struct cdns_dphy {
  428. struct cdns_dphy_cfg cfg;
  429. void __iomem *regs;
  430. struct clk *psm_clk;
  431. struct clk *pll_ref_clk;
  432. const struct cdns_dphy_ops *ops;
  433. };
  434. struct cdns_dsi_input {
  435. enum cdns_dsi_input_id id;
  436. struct drm_bridge bridge;
  437. };
  438. struct cdns_dsi {
  439. struct mipi_dsi_host base;
  440. void __iomem *regs;
  441. struct cdns_dsi_input input;
  442. struct cdns_dsi_output output;
  443. unsigned int direct_cmd_fifo_depth;
  444. unsigned int rx_fifo_depth;
  445. struct completion direct_cmd_comp;
  446. struct clk *dsi_p_clk;
  447. struct reset_control *dsi_p_rst;
  448. struct clk *dsi_sys_clk;
  449. bool link_initialized;
  450. struct cdns_dphy *dphy;
  451. };
  452. static inline struct cdns_dsi *input_to_dsi(struct cdns_dsi_input *input)
  453. {
  454. return container_of(input, struct cdns_dsi, input);
  455. }
  456. static inline struct cdns_dsi *to_cdns_dsi(struct mipi_dsi_host *host)
  457. {
  458. return container_of(host, struct cdns_dsi, base);
  459. }
  460. static inline struct cdns_dsi_input *
  461. bridge_to_cdns_dsi_input(struct drm_bridge *bridge)
  462. {
  463. return container_of(bridge, struct cdns_dsi_input, bridge);
  464. }
  465. static int cdns_dsi_get_dphy_pll_cfg(struct cdns_dphy *dphy,
  466. struct cdns_dphy_cfg *cfg,
  467. unsigned int dpi_htotal,
  468. unsigned int dpi_bpp,
  469. unsigned int dpi_hz,
  470. unsigned int dsi_htotal,
  471. unsigned int dsi_nlanes,
  472. unsigned int *dsi_hfp_ext)
  473. {
  474. u64 dlane_bps, dlane_bps_max, fbdiv, fbdiv_max, adj_dsi_htotal;
  475. unsigned long pll_ref_hz = clk_get_rate(dphy->pll_ref_clk);
  476. memset(cfg, 0, sizeof(*cfg));
  477. cfg->nlanes = dsi_nlanes;
  478. if (pll_ref_hz < 9600000 || pll_ref_hz >= 150000000)
  479. return -EINVAL;
  480. else if (pll_ref_hz < 19200000)
  481. cfg->pll_ipdiv = 1;
  482. else if (pll_ref_hz < 38400000)
  483. cfg->pll_ipdiv = 2;
  484. else if (pll_ref_hz < 76800000)
  485. cfg->pll_ipdiv = 4;
  486. else
  487. cfg->pll_ipdiv = 8;
  488. /*
  489. * Make sure DSI htotal is aligned on a lane boundary when calculating
  490. * the expected data rate. This is done by extending HFP in case of
  491. * misalignment.
  492. */
  493. adj_dsi_htotal = dsi_htotal;
  494. if (dsi_htotal % dsi_nlanes)
  495. adj_dsi_htotal += dsi_nlanes - (dsi_htotal % dsi_nlanes);
  496. dlane_bps = (u64)dpi_hz * adj_dsi_htotal;
  497. /* data rate in bytes/sec is not an integer, refuse the mode. */
  498. if (do_div(dlane_bps, dsi_nlanes * dpi_htotal))
  499. return -EINVAL;
  500. /* data rate was in bytes/sec, convert to bits/sec. */
  501. dlane_bps *= 8;
  502. if (dlane_bps > 2500000000UL || dlane_bps < 160000000UL)
  503. return -EINVAL;
  504. else if (dlane_bps >= 1250000000)
  505. cfg->pll_opdiv = 1;
  506. else if (dlane_bps >= 630000000)
  507. cfg->pll_opdiv = 2;
  508. else if (dlane_bps >= 320000000)
  509. cfg->pll_opdiv = 4;
  510. else if (dlane_bps >= 160000000)
  511. cfg->pll_opdiv = 8;
  512. /*
  513. * Allow a deviation of 0.2% on the per-lane data rate to try to
  514. * recover a potential mismatch between DPI and PPI clks.
  515. */
  516. dlane_bps_max = dlane_bps + DIV_ROUND_DOWN_ULL(dlane_bps, 500);
  517. fbdiv_max = DIV_ROUND_DOWN_ULL(dlane_bps_max * 2 *
  518. cfg->pll_opdiv * cfg->pll_ipdiv,
  519. pll_ref_hz);
  520. fbdiv = DIV_ROUND_UP_ULL(dlane_bps * 2 * cfg->pll_opdiv *
  521. cfg->pll_ipdiv,
  522. pll_ref_hz);
  523. /*
  524. * Iterate over all acceptable fbdiv and try to find an adjusted DSI
  525. * htotal length providing an exact match.
  526. *
  527. * Note that we could do something even trickier by relying on the fact
  528. * that a new line is not necessarily aligned on a lane boundary, so,
  529. * by making adj_dsi_htotal non aligned on a dsi_lanes we can improve a
  530. * bit the precision. With this, the step would be
  531. *
  532. * pll_ref_hz / (2 * opdiv * ipdiv * nlanes)
  533. *
  534. * instead of
  535. *
  536. * pll_ref_hz / (2 * opdiv * ipdiv)
  537. *
  538. * The drawback of this approach is that we would need to make sure the
  539. * number or lines is a multiple of the realignment periodicity which is
  540. * a function of the number of lanes and the original misalignment. For
  541. * example, for NLANES = 4 and HTOTAL % NLANES = 3, it takes 4 lines
  542. * to realign on a lane:
  543. * LINE 0: expected number of bytes, starts emitting first byte of
  544. * LINE 1 on LANE 3
  545. * LINE 1: expected number of bytes, starts emitting first 2 bytes of
  546. * LINE 2 on LANES 2 and 3
  547. * LINE 2: expected number of bytes, starts emitting first 3 bytes of
  548. * of LINE 3 on LANES 1, 2 and 3
  549. * LINE 3: one byte less, now things are realigned on LANE 0 for LINE 4
  550. *
  551. * I figured this extra complexity was not worth the benefit, but if
  552. * someone really has unfixable mismatch, that would be something to
  553. * investigate.
  554. */
  555. for (; fbdiv <= fbdiv_max; fbdiv++) {
  556. u32 rem;
  557. adj_dsi_htotal = (u64)fbdiv * pll_ref_hz * dsi_nlanes *
  558. dpi_htotal;
  559. /*
  560. * Do the division in 2 steps to avoid an overflow on the
  561. * divider.
  562. */
  563. rem = do_div(adj_dsi_htotal, dpi_hz);
  564. if (rem)
  565. continue;
  566. rem = do_div(adj_dsi_htotal,
  567. cfg->pll_opdiv * cfg->pll_ipdiv * 2 * 8);
  568. if (rem)
  569. continue;
  570. cfg->pll_fbdiv = fbdiv;
  571. *dsi_hfp_ext = adj_dsi_htotal - dsi_htotal;
  572. break;
  573. }
  574. /* No match, let's just reject the display mode. */
  575. if (!cfg->pll_fbdiv)
  576. return -EINVAL;
  577. dlane_bps = DIV_ROUND_DOWN_ULL((u64)dpi_hz * adj_dsi_htotal * 8,
  578. dsi_nlanes * dpi_htotal);
  579. cfg->lane_bps = dlane_bps;
  580. return 0;
  581. }
  582. static int cdns_dphy_setup_psm(struct cdns_dphy *dphy)
  583. {
  584. unsigned long psm_clk_hz = clk_get_rate(dphy->psm_clk);
  585. unsigned long psm_div;
  586. if (!psm_clk_hz || psm_clk_hz > 100000000)
  587. return -EINVAL;
  588. psm_div = DIV_ROUND_CLOSEST(psm_clk_hz, 1000000);
  589. if (dphy->ops->set_psm_div)
  590. dphy->ops->set_psm_div(dphy, psm_div);
  591. return 0;
  592. }
  593. static void cdns_dphy_set_clk_lane_cfg(struct cdns_dphy *dphy,
  594. enum cdns_dphy_clk_lane_cfg cfg)
  595. {
  596. if (dphy->ops->set_clk_lane_cfg)
  597. dphy->ops->set_clk_lane_cfg(dphy, cfg);
  598. }
  599. static void cdns_dphy_set_pll_cfg(struct cdns_dphy *dphy,
  600. const struct cdns_dphy_cfg *cfg)
  601. {
  602. if (dphy->ops->set_pll_cfg)
  603. dphy->ops->set_pll_cfg(dphy, cfg);
  604. }
  605. static unsigned long cdns_dphy_get_wakeup_time_ns(struct cdns_dphy *dphy)
  606. {
  607. return dphy->ops->get_wakeup_time_ns(dphy);
  608. }
  609. static unsigned int dpi_to_dsi_timing(unsigned int dpi_timing,
  610. unsigned int dpi_bpp,
  611. unsigned int dsi_pkt_overhead)
  612. {
  613. unsigned int dsi_timing = DIV_ROUND_UP(dpi_timing * dpi_bpp, 8);
  614. if (dsi_timing < dsi_pkt_overhead)
  615. dsi_timing = 0;
  616. else
  617. dsi_timing -= dsi_pkt_overhead;
  618. return dsi_timing;
  619. }
  620. static int cdns_dsi_mode2cfg(struct cdns_dsi *dsi,
  621. const struct drm_display_mode *mode,
  622. struct cdns_dsi_cfg *dsi_cfg,
  623. struct cdns_dphy_cfg *dphy_cfg,
  624. bool mode_valid_check)
  625. {
  626. unsigned long dsi_htotal = 0, dsi_hss_hsa_hse_hbp = 0;
  627. struct cdns_dsi_output *output = &dsi->output;
  628. unsigned int dsi_hfp_ext = 0, dpi_hfp, tmp;
  629. bool sync_pulse = false;
  630. int bpp, nlanes, ret;
  631. memset(dsi_cfg, 0, sizeof(*dsi_cfg));
  632. if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  633. sync_pulse = true;
  634. bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format);
  635. nlanes = output->dev->lanes;
  636. if (mode_valid_check)
  637. tmp = mode->htotal -
  638. (sync_pulse ? mode->hsync_end : mode->hsync_start);
  639. else
  640. tmp = mode->crtc_htotal -
  641. (sync_pulse ?
  642. mode->crtc_hsync_end : mode->crtc_hsync_start);
  643. dsi_cfg->hbp = dpi_to_dsi_timing(tmp, bpp, DSI_HBP_FRAME_OVERHEAD);
  644. dsi_htotal += dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD;
  645. dsi_hss_hsa_hse_hbp += dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD;
  646. if (sync_pulse) {
  647. if (mode_valid_check)
  648. tmp = mode->hsync_end - mode->hsync_start;
  649. else
  650. tmp = mode->crtc_hsync_end - mode->crtc_hsync_start;
  651. dsi_cfg->hsa = dpi_to_dsi_timing(tmp, bpp,
  652. DSI_HSA_FRAME_OVERHEAD);
  653. dsi_htotal += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD;
  654. dsi_hss_hsa_hse_hbp += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD;
  655. }
  656. dsi_cfg->hact = dpi_to_dsi_timing(mode_valid_check ?
  657. mode->hdisplay : mode->crtc_hdisplay,
  658. bpp, 0);
  659. dsi_htotal += dsi_cfg->hact;
  660. if (mode_valid_check)
  661. dpi_hfp = mode->hsync_start - mode->hdisplay;
  662. else
  663. dpi_hfp = mode->crtc_hsync_start - mode->crtc_hdisplay;
  664. dsi_cfg->hfp = dpi_to_dsi_timing(dpi_hfp, bpp, DSI_HFP_FRAME_OVERHEAD);
  665. dsi_htotal += dsi_cfg->hfp + DSI_HFP_FRAME_OVERHEAD;
  666. if (mode_valid_check)
  667. ret = cdns_dsi_get_dphy_pll_cfg(dsi->dphy, dphy_cfg,
  668. mode->htotal, bpp,
  669. mode->clock * 1000,
  670. dsi_htotal, nlanes,
  671. &dsi_hfp_ext);
  672. else
  673. ret = cdns_dsi_get_dphy_pll_cfg(dsi->dphy, dphy_cfg,
  674. mode->crtc_htotal, bpp,
  675. mode->crtc_clock * 1000,
  676. dsi_htotal, nlanes,
  677. &dsi_hfp_ext);
  678. if (ret)
  679. return ret;
  680. dsi_cfg->hfp += dsi_hfp_ext;
  681. dsi_htotal += dsi_hfp_ext;
  682. dsi_cfg->htotal = dsi_htotal;
  683. /*
  684. * Make sure DPI(HFP) > DSI(HSS+HSA+HSE+HBP) to guarantee that the FIFO
  685. * is empty before we start a receiving a new line on the DPI
  686. * interface.
  687. */
  688. if ((u64)dphy_cfg->lane_bps * dpi_hfp * nlanes <
  689. (u64)dsi_hss_hsa_hse_hbp *
  690. (mode_valid_check ? mode->clock : mode->crtc_clock) * 1000)
  691. return -EINVAL;
  692. return 0;
  693. }
  694. static int cdns_dsi_bridge_attach(struct drm_bridge *bridge)
  695. {
  696. struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
  697. struct cdns_dsi *dsi = input_to_dsi(input);
  698. struct cdns_dsi_output *output = &dsi->output;
  699. if (!drm_core_check_feature(bridge->dev, DRIVER_ATOMIC)) {
  700. dev_err(dsi->base.dev,
  701. "cdns-dsi driver is only compatible with DRM devices supporting atomic updates");
  702. return -ENOTSUPP;
  703. }
  704. return drm_bridge_attach(bridge->encoder, output->bridge, bridge);
  705. }
  706. static enum drm_mode_status
  707. cdns_dsi_bridge_mode_valid(struct drm_bridge *bridge,
  708. const struct drm_display_mode *mode)
  709. {
  710. struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
  711. struct cdns_dsi *dsi = input_to_dsi(input);
  712. struct cdns_dsi_output *output = &dsi->output;
  713. struct cdns_dphy_cfg dphy_cfg;
  714. struct cdns_dsi_cfg dsi_cfg;
  715. int bpp, nlanes, ret;
  716. /*
  717. * VFP_DSI should be less than VFP_DPI and VFP_DSI should be at
  718. * least 1.
  719. */
  720. if (mode->vtotal - mode->vsync_end < 2)
  721. return MODE_V_ILLEGAL;
  722. /* VSA_DSI = VSA_DPI and must be at least 2. */
  723. if (mode->vsync_end - mode->vsync_start < 2)
  724. return MODE_V_ILLEGAL;
  725. /* HACT must be 32-bits aligned. */
  726. bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format);
  727. if ((mode->hdisplay * bpp) % 32)
  728. return MODE_H_ILLEGAL;
  729. nlanes = output->dev->lanes;
  730. ret = cdns_dsi_mode2cfg(dsi, mode, &dsi_cfg, &dphy_cfg, true);
  731. if (ret)
  732. return MODE_CLOCK_RANGE;
  733. return MODE_OK;
  734. }
  735. static void cdns_dsi_bridge_disable(struct drm_bridge *bridge)
  736. {
  737. struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
  738. struct cdns_dsi *dsi = input_to_dsi(input);
  739. u32 val;
  740. val = readl(dsi->regs + MCTL_MAIN_DATA_CTL);
  741. val &= ~(IF_VID_SELECT_MASK | IF_VID_MODE | VID_EN | HOST_EOT_GEN |
  742. DISP_EOT_GEN);
  743. writel(val, dsi->regs + MCTL_MAIN_DATA_CTL);
  744. val = readl(dsi->regs + MCTL_MAIN_EN) & ~IF_EN(input->id);
  745. writel(val, dsi->regs + MCTL_MAIN_EN);
  746. pm_runtime_put(dsi->base.dev);
  747. }
  748. static void cdns_dsi_hs_init(struct cdns_dsi *dsi,
  749. const struct cdns_dphy_cfg *dphy_cfg)
  750. {
  751. u32 status;
  752. /*
  753. * Power all internal DPHY blocks down and maintain their reset line
  754. * asserted before changing the DPHY config.
  755. */
  756. writel(DPHY_CMN_PSO | DPHY_PLL_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN |
  757. DPHY_CMN_PDN | DPHY_PLL_PDN,
  758. dsi->regs + MCTL_DPHY_CFG0);
  759. /*
  760. * Configure the internal PSM clk divider so that the DPHY has a
  761. * 1MHz clk (or something close).
  762. */
  763. WARN_ON_ONCE(cdns_dphy_setup_psm(dsi->dphy));
  764. /*
  765. * Configure attach clk lanes to data lanes: the DPHY has 2 clk lanes
  766. * and 8 data lanes, each clk lane can be attache different set of
  767. * data lanes. The 2 groups are named 'left' and 'right', so here we
  768. * just say that we want the 'left' clk lane to drive the 'left' data
  769. * lanes.
  770. */
  771. cdns_dphy_set_clk_lane_cfg(dsi->dphy, DPHY_CLK_CFG_LEFT_DRIVES_LEFT);
  772. /*
  773. * Configure the DPHY PLL that will be used to generate the TX byte
  774. * clk.
  775. */
  776. cdns_dphy_set_pll_cfg(dsi->dphy, dphy_cfg);
  777. /* Start TX state machine. */
  778. writel(DPHY_CMN_SSM_EN | DPHY_CMN_TX_MODE_EN,
  779. dsi->dphy->regs + DPHY_CMN_SSM);
  780. /* Activate the PLL and wait until it's locked. */
  781. writel(PLL_LOCKED, dsi->regs + MCTL_MAIN_STS_CLR);
  782. writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN,
  783. dsi->regs + MCTL_DPHY_CFG0);
  784. WARN_ON_ONCE(readl_poll_timeout(dsi->regs + MCTL_MAIN_STS, status,
  785. status & PLL_LOCKED, 100, 100));
  786. /* De-assert data and clock reset lines. */
  787. writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN |
  788. DPHY_D_RSTB(dphy_cfg->nlanes) | DPHY_C_RSTB,
  789. dsi->regs + MCTL_DPHY_CFG0);
  790. }
  791. static void cdns_dsi_init_link(struct cdns_dsi *dsi)
  792. {
  793. struct cdns_dsi_output *output = &dsi->output;
  794. unsigned long sysclk_period, ulpout;
  795. u32 val;
  796. int i;
  797. if (dsi->link_initialized)
  798. return;
  799. val = 0;
  800. for (i = 1; i < output->dev->lanes; i++)
  801. val |= DATA_LANE_EN(i);
  802. if (!(output->dev->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
  803. val |= CLK_CONTINUOUS;
  804. writel(val, dsi->regs + MCTL_MAIN_PHY_CTL);
  805. /* ULPOUT should be set to 1ms and is expressed in sysclk cycles. */
  806. sysclk_period = NSEC_PER_SEC / clk_get_rate(dsi->dsi_sys_clk);
  807. ulpout = DIV_ROUND_UP(NSEC_PER_MSEC, sysclk_period);
  808. writel(CLK_LANE_ULPOUT_TIME(ulpout) | DATA_LANE_ULPOUT_TIME(ulpout),
  809. dsi->regs + MCTL_ULPOUT_TIME);
  810. writel(LINK_EN, dsi->regs + MCTL_MAIN_DATA_CTL);
  811. val = CLK_LANE_EN | PLL_START;
  812. for (i = 0; i < output->dev->lanes; i++)
  813. val |= DATA_LANE_START(i);
  814. writel(val, dsi->regs + MCTL_MAIN_EN);
  815. dsi->link_initialized = true;
  816. }
  817. static void cdns_dsi_bridge_enable(struct drm_bridge *bridge)
  818. {
  819. struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
  820. struct cdns_dsi *dsi = input_to_dsi(input);
  821. struct cdns_dsi_output *output = &dsi->output;
  822. struct drm_display_mode *mode;
  823. struct cdns_dphy_cfg dphy_cfg;
  824. unsigned long tx_byte_period;
  825. struct cdns_dsi_cfg dsi_cfg;
  826. u32 tmp, reg_wakeup, div;
  827. int bpp, nlanes;
  828. if (WARN_ON(pm_runtime_get_sync(dsi->base.dev) < 0))
  829. return;
  830. mode = &bridge->encoder->crtc->state->adjusted_mode;
  831. bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format);
  832. nlanes = output->dev->lanes;
  833. WARN_ON_ONCE(cdns_dsi_mode2cfg(dsi, mode, &dsi_cfg, &dphy_cfg, false));
  834. cdns_dsi_hs_init(dsi, &dphy_cfg);
  835. cdns_dsi_init_link(dsi);
  836. writel(HBP_LEN(dsi_cfg.hbp) | HSA_LEN(dsi_cfg.hsa),
  837. dsi->regs + VID_HSIZE1);
  838. writel(HFP_LEN(dsi_cfg.hfp) | HACT_LEN(dsi_cfg.hact),
  839. dsi->regs + VID_HSIZE2);
  840. writel(VBP_LEN(mode->crtc_vtotal - mode->crtc_vsync_end - 1) |
  841. VFP_LEN(mode->crtc_vsync_start - mode->crtc_vdisplay) |
  842. VSA_LEN(mode->crtc_vsync_end - mode->crtc_vsync_start + 1),
  843. dsi->regs + VID_VSIZE1);
  844. writel(mode->crtc_vdisplay, dsi->regs + VID_VSIZE2);
  845. tmp = dsi_cfg.htotal -
  846. (dsi_cfg.hsa + DSI_BLANKING_FRAME_OVERHEAD +
  847. DSI_HSA_FRAME_OVERHEAD);
  848. writel(BLK_LINE_PULSE_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE2);
  849. if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  850. writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD),
  851. dsi->regs + VID_VCA_SETTING2);
  852. tmp = dsi_cfg.htotal -
  853. (DSI_HSS_VSS_VSE_FRAME_OVERHEAD + DSI_BLANKING_FRAME_OVERHEAD);
  854. writel(BLK_LINE_EVENT_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE1);
  855. if (!(output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE))
  856. writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD),
  857. dsi->regs + VID_VCA_SETTING2);
  858. tmp = DIV_ROUND_UP(dsi_cfg.htotal, nlanes) -
  859. DIV_ROUND_UP(dsi_cfg.hsa, nlanes);
  860. if (!(output->dev->mode_flags & MIPI_DSI_MODE_EOT_PACKET))
  861. tmp -= DIV_ROUND_UP(DSI_EOT_PKT_SIZE, nlanes);
  862. tx_byte_period = DIV_ROUND_DOWN_ULL((u64)NSEC_PER_SEC * 8,
  863. dphy_cfg.lane_bps);
  864. reg_wakeup = cdns_dphy_get_wakeup_time_ns(dsi->dphy) /
  865. tx_byte_period;
  866. writel(REG_WAKEUP_TIME(reg_wakeup) | REG_LINE_DURATION(tmp),
  867. dsi->regs + VID_DPHY_TIME);
  868. /*
  869. * HSTX and LPRX timeouts are both expressed in TX byte clk cycles and
  870. * both should be set to at least the time it takes to transmit a
  871. * frame.
  872. */
  873. tmp = NSEC_PER_SEC / drm_mode_vrefresh(mode);
  874. tmp /= tx_byte_period;
  875. for (div = 0; div <= CLK_DIV_MAX; div++) {
  876. if (tmp <= HSTX_TIMEOUT_MAX)
  877. break;
  878. tmp >>= 1;
  879. }
  880. if (tmp > HSTX_TIMEOUT_MAX)
  881. tmp = HSTX_TIMEOUT_MAX;
  882. writel(CLK_DIV(div) | HSTX_TIMEOUT(tmp),
  883. dsi->regs + MCTL_DPHY_TIMEOUT1);
  884. writel(LPRX_TIMEOUT(tmp), dsi->regs + MCTL_DPHY_TIMEOUT2);
  885. if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO) {
  886. switch (output->dev->format) {
  887. case MIPI_DSI_FMT_RGB888:
  888. tmp = VID_PIXEL_MODE_RGB888 |
  889. VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_24);
  890. break;
  891. case MIPI_DSI_FMT_RGB666:
  892. tmp = VID_PIXEL_MODE_RGB666 |
  893. VID_DATATYPE(MIPI_DSI_PIXEL_STREAM_3BYTE_18);
  894. break;
  895. case MIPI_DSI_FMT_RGB666_PACKED:
  896. tmp = VID_PIXEL_MODE_RGB666_PACKED |
  897. VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_18);
  898. break;
  899. case MIPI_DSI_FMT_RGB565:
  900. tmp = VID_PIXEL_MODE_RGB565 |
  901. VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_16);
  902. break;
  903. default:
  904. dev_err(dsi->base.dev, "Unsupported DSI format\n");
  905. return;
  906. }
  907. if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  908. tmp |= SYNC_PULSE_ACTIVE | SYNC_PULSE_HORIZONTAL;
  909. tmp |= REG_BLKLINE_MODE(REG_BLK_MODE_BLANKING_PKT) |
  910. REG_BLKEOL_MODE(REG_BLK_MODE_BLANKING_PKT) |
  911. RECOVERY_MODE(RECOVERY_MODE_NEXT_HSYNC) |
  912. VID_IGNORE_MISS_VSYNC;
  913. writel(tmp, dsi->regs + VID_MAIN_CTL);
  914. }
  915. tmp = readl(dsi->regs + MCTL_MAIN_DATA_CTL);
  916. tmp &= ~(IF_VID_SELECT_MASK | HOST_EOT_GEN | IF_VID_MODE);
  917. if (!(output->dev->mode_flags & MIPI_DSI_MODE_EOT_PACKET))
  918. tmp |= HOST_EOT_GEN;
  919. if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO)
  920. tmp |= IF_VID_MODE | IF_VID_SELECT(input->id) | VID_EN;
  921. writel(tmp, dsi->regs + MCTL_MAIN_DATA_CTL);
  922. tmp = readl(dsi->regs + MCTL_MAIN_EN) | IF_EN(input->id);
  923. writel(tmp, dsi->regs + MCTL_MAIN_EN);
  924. }
  925. static const struct drm_bridge_funcs cdns_dsi_bridge_funcs = {
  926. .attach = cdns_dsi_bridge_attach,
  927. .mode_valid = cdns_dsi_bridge_mode_valid,
  928. .disable = cdns_dsi_bridge_disable,
  929. .enable = cdns_dsi_bridge_enable,
  930. };
  931. static int cdns_dsi_attach(struct mipi_dsi_host *host,
  932. struct mipi_dsi_device *dev)
  933. {
  934. struct cdns_dsi *dsi = to_cdns_dsi(host);
  935. struct cdns_dsi_output *output = &dsi->output;
  936. struct cdns_dsi_input *input = &dsi->input;
  937. struct drm_bridge *bridge;
  938. struct drm_panel *panel;
  939. struct device_node *np;
  940. int ret;
  941. /*
  942. * We currently do not support connecting several DSI devices to the
  943. * same host. In order to support that we'd need the DRM bridge
  944. * framework to allow dynamic reconfiguration of the bridge chain.
  945. */
  946. if (output->dev)
  947. return -EBUSY;
  948. /* We do not support burst mode yet. */
  949. if (dev->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  950. return -ENOTSUPP;
  951. /*
  952. * The host <-> device link might be described using an OF-graph
  953. * representation, in this case we extract the device of_node from
  954. * this representation, otherwise we use dsidev->dev.of_node which
  955. * should have been filled by the core.
  956. */
  957. np = of_graph_get_remote_node(dsi->base.dev->of_node, DSI_OUTPUT_PORT,
  958. dev->channel);
  959. if (!np)
  960. np = of_node_get(dev->dev.of_node);
  961. panel = of_drm_find_panel(np);
  962. if (!IS_ERR(panel)) {
  963. bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DSI);
  964. } else {
  965. bridge = of_drm_find_bridge(dev->dev.of_node);
  966. if (!bridge)
  967. bridge = ERR_PTR(-EINVAL);
  968. }
  969. of_node_put(np);
  970. if (IS_ERR(bridge)) {
  971. ret = PTR_ERR(bridge);
  972. dev_err(host->dev, "failed to add DSI device %s (err = %d)",
  973. dev->name, ret);
  974. return ret;
  975. }
  976. output->dev = dev;
  977. output->bridge = bridge;
  978. output->panel = panel;
  979. /*
  980. * The DSI output has been properly configured, we can now safely
  981. * register the input to the bridge framework so that it can take place
  982. * in a display pipeline.
  983. */
  984. drm_bridge_add(&input->bridge);
  985. return 0;
  986. }
  987. static int cdns_dsi_detach(struct mipi_dsi_host *host,
  988. struct mipi_dsi_device *dev)
  989. {
  990. struct cdns_dsi *dsi = to_cdns_dsi(host);
  991. struct cdns_dsi_output *output = &dsi->output;
  992. struct cdns_dsi_input *input = &dsi->input;
  993. drm_bridge_remove(&input->bridge);
  994. if (output->panel)
  995. drm_panel_bridge_remove(output->bridge);
  996. return 0;
  997. }
  998. static irqreturn_t cdns_dsi_interrupt(int irq, void *data)
  999. {
  1000. struct cdns_dsi *dsi = data;
  1001. irqreturn_t ret = IRQ_NONE;
  1002. u32 flag, ctl;
  1003. flag = readl(dsi->regs + DIRECT_CMD_STS_FLAG);
  1004. if (flag) {
  1005. ctl = readl(dsi->regs + DIRECT_CMD_STS_CTL);
  1006. ctl &= ~flag;
  1007. writel(ctl, dsi->regs + DIRECT_CMD_STS_CTL);
  1008. complete(&dsi->direct_cmd_comp);
  1009. ret = IRQ_HANDLED;
  1010. }
  1011. return ret;
  1012. }
  1013. static ssize_t cdns_dsi_transfer(struct mipi_dsi_host *host,
  1014. const struct mipi_dsi_msg *msg)
  1015. {
  1016. struct cdns_dsi *dsi = to_cdns_dsi(host);
  1017. u32 cmd, sts, val, wait = WRITE_COMPLETED, ctl = 0;
  1018. struct mipi_dsi_packet packet;
  1019. int ret, i, tx_len, rx_len;
  1020. ret = pm_runtime_get_sync(host->dev);
  1021. if (ret < 0)
  1022. return ret;
  1023. cdns_dsi_init_link(dsi);
  1024. ret = mipi_dsi_create_packet(&packet, msg);
  1025. if (ret)
  1026. goto out;
  1027. tx_len = msg->tx_buf ? msg->tx_len : 0;
  1028. rx_len = msg->rx_buf ? msg->rx_len : 0;
  1029. /* For read operations, the maximum TX len is 2. */
  1030. if (rx_len && tx_len > 2) {
  1031. ret = -ENOTSUPP;
  1032. goto out;
  1033. }
  1034. /* TX len is limited by the CMD FIFO depth. */
  1035. if (tx_len > dsi->direct_cmd_fifo_depth) {
  1036. ret = -ENOTSUPP;
  1037. goto out;
  1038. }
  1039. /* RX len is limited by the RX FIFO depth. */
  1040. if (rx_len > dsi->rx_fifo_depth) {
  1041. ret = -ENOTSUPP;
  1042. goto out;
  1043. }
  1044. cmd = CMD_SIZE(tx_len) | CMD_VCHAN_ID(msg->channel) |
  1045. CMD_DATATYPE(msg->type);
  1046. if (msg->flags & MIPI_DSI_MSG_USE_LPM)
  1047. cmd |= CMD_LP_EN;
  1048. if (mipi_dsi_packet_format_is_long(msg->type))
  1049. cmd |= CMD_LONG;
  1050. if (rx_len) {
  1051. cmd |= READ_CMD;
  1052. wait = READ_COMPLETED_WITH_ERR | READ_COMPLETED;
  1053. ctl = READ_EN | BTA_EN;
  1054. } else if (msg->flags & MIPI_DSI_MSG_REQ_ACK) {
  1055. cmd |= BTA_REQ;
  1056. wait = ACK_WITH_ERR_RCVD | ACK_RCVD;
  1057. ctl = BTA_EN;
  1058. }
  1059. writel(readl(dsi->regs + MCTL_MAIN_DATA_CTL) | ctl,
  1060. dsi->regs + MCTL_MAIN_DATA_CTL);
  1061. writel(cmd, dsi->regs + DIRECT_CMD_MAIN_SETTINGS);
  1062. for (i = 0; i < tx_len; i += 4) {
  1063. const u8 *buf = msg->tx_buf;
  1064. int j;
  1065. val = 0;
  1066. for (j = 0; j < 4 && j + i < tx_len; j++)
  1067. val |= (u32)buf[i + j] << (8 * j);
  1068. writel(val, dsi->regs + DIRECT_CMD_WRDATA);
  1069. }
  1070. /* Clear status flags before sending the command. */
  1071. writel(wait, dsi->regs + DIRECT_CMD_STS_CLR);
  1072. writel(wait, dsi->regs + DIRECT_CMD_STS_CTL);
  1073. reinit_completion(&dsi->direct_cmd_comp);
  1074. writel(0, dsi->regs + DIRECT_CMD_SEND);
  1075. wait_for_completion_timeout(&dsi->direct_cmd_comp,
  1076. msecs_to_jiffies(1000));
  1077. sts = readl(dsi->regs + DIRECT_CMD_STS);
  1078. writel(wait, dsi->regs + DIRECT_CMD_STS_CLR);
  1079. writel(0, dsi->regs + DIRECT_CMD_STS_CTL);
  1080. writel(readl(dsi->regs + MCTL_MAIN_DATA_CTL) & ~ctl,
  1081. dsi->regs + MCTL_MAIN_DATA_CTL);
  1082. /* We did not receive the events we were waiting for. */
  1083. if (!(sts & wait)) {
  1084. ret = -ETIMEDOUT;
  1085. goto out;
  1086. }
  1087. /* 'READ' or 'WRITE with ACK' failed. */
  1088. if (sts & (READ_COMPLETED_WITH_ERR | ACK_WITH_ERR_RCVD)) {
  1089. ret = -EIO;
  1090. goto out;
  1091. }
  1092. for (i = 0; i < rx_len; i += 4) {
  1093. u8 *buf = msg->rx_buf;
  1094. int j;
  1095. val = readl(dsi->regs + DIRECT_CMD_RDDATA);
  1096. for (j = 0; j < 4 && j + i < rx_len; j++)
  1097. buf[i + j] = val >> (8 * j);
  1098. }
  1099. out:
  1100. pm_runtime_put(host->dev);
  1101. return ret;
  1102. }
  1103. static const struct mipi_dsi_host_ops cdns_dsi_ops = {
  1104. .attach = cdns_dsi_attach,
  1105. .detach = cdns_dsi_detach,
  1106. .transfer = cdns_dsi_transfer,
  1107. };
  1108. static int __maybe_unused cdns_dsi_resume(struct device *dev)
  1109. {
  1110. struct cdns_dsi *dsi = dev_get_drvdata(dev);
  1111. reset_control_deassert(dsi->dsi_p_rst);
  1112. clk_prepare_enable(dsi->dsi_p_clk);
  1113. clk_prepare_enable(dsi->dsi_sys_clk);
  1114. clk_prepare_enable(dsi->dphy->psm_clk);
  1115. clk_prepare_enable(dsi->dphy->pll_ref_clk);
  1116. return 0;
  1117. }
  1118. static int __maybe_unused cdns_dsi_suspend(struct device *dev)
  1119. {
  1120. struct cdns_dsi *dsi = dev_get_drvdata(dev);
  1121. clk_disable_unprepare(dsi->dphy->pll_ref_clk);
  1122. clk_disable_unprepare(dsi->dphy->psm_clk);
  1123. clk_disable_unprepare(dsi->dsi_sys_clk);
  1124. clk_disable_unprepare(dsi->dsi_p_clk);
  1125. reset_control_assert(dsi->dsi_p_rst);
  1126. dsi->link_initialized = false;
  1127. return 0;
  1128. }
  1129. static UNIVERSAL_DEV_PM_OPS(cdns_dsi_pm_ops, cdns_dsi_suspend, cdns_dsi_resume,
  1130. NULL);
  1131. static unsigned long cdns_dphy_ref_get_wakeup_time_ns(struct cdns_dphy *dphy)
  1132. {
  1133. /* Default wakeup time is 800 ns (in a simulated environment). */
  1134. return 800;
  1135. }
  1136. static void cdns_dphy_ref_set_pll_cfg(struct cdns_dphy *dphy,
  1137. const struct cdns_dphy_cfg *cfg)
  1138. {
  1139. u32 fbdiv_low, fbdiv_high;
  1140. fbdiv_low = (cfg->pll_fbdiv / 4) - 2;
  1141. fbdiv_high = cfg->pll_fbdiv - fbdiv_low - 2;
  1142. writel(DPHY_CMN_IPDIV_FROM_REG | DPHY_CMN_OPDIV_FROM_REG |
  1143. DPHY_CMN_IPDIV(cfg->pll_ipdiv) |
  1144. DPHY_CMN_OPDIV(cfg->pll_opdiv),
  1145. dphy->regs + DPHY_CMN_OPIPDIV);
  1146. writel(DPHY_CMN_FBDIV_FROM_REG |
  1147. DPHY_CMN_FBDIV_VAL(fbdiv_low, fbdiv_high),
  1148. dphy->regs + DPHY_CMN_FBDIV);
  1149. writel(DPHY_CMN_PWM_HIGH(6) | DPHY_CMN_PWM_LOW(0x101) |
  1150. DPHY_CMN_PWM_DIV(0x8),
  1151. dphy->regs + DPHY_CMN_PWM);
  1152. }
  1153. static void cdns_dphy_ref_set_psm_div(struct cdns_dphy *dphy, u8 div)
  1154. {
  1155. writel(DPHY_PSM_CFG_FROM_REG | DPHY_PSM_CLK_DIV(div),
  1156. dphy->regs + DPHY_PSM_CFG);
  1157. }
  1158. /*
  1159. * This is the reference implementation of DPHY hooks. Specific integration of
  1160. * this IP may have to re-implement some of them depending on how they decided
  1161. * to wire things in the SoC.
  1162. */
  1163. static const struct cdns_dphy_ops ref_dphy_ops = {
  1164. .get_wakeup_time_ns = cdns_dphy_ref_get_wakeup_time_ns,
  1165. .set_pll_cfg = cdns_dphy_ref_set_pll_cfg,
  1166. .set_psm_div = cdns_dphy_ref_set_psm_div,
  1167. };
  1168. static const struct of_device_id cdns_dphy_of_match[] = {
  1169. { .compatible = "cdns,dphy", .data = &ref_dphy_ops },
  1170. { /* sentinel */ },
  1171. };
  1172. static struct cdns_dphy *cdns_dphy_probe(struct platform_device *pdev)
  1173. {
  1174. const struct of_device_id *match;
  1175. struct cdns_dphy *dphy;
  1176. struct of_phandle_args args;
  1177. struct resource res;
  1178. int ret;
  1179. ret = of_parse_phandle_with_args(pdev->dev.of_node, "phys",
  1180. "#phy-cells", 0, &args);
  1181. if (ret)
  1182. return ERR_PTR(-ENOENT);
  1183. match = of_match_node(cdns_dphy_of_match, args.np);
  1184. if (!match || !match->data)
  1185. return ERR_PTR(-EINVAL);
  1186. dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL);
  1187. if (!dphy)
  1188. return ERR_PTR(-ENOMEM);
  1189. dphy->ops = match->data;
  1190. ret = of_address_to_resource(args.np, 0, &res);
  1191. if (ret)
  1192. return ERR_PTR(ret);
  1193. dphy->regs = devm_ioremap_resource(&pdev->dev, &res);
  1194. if (IS_ERR(dphy->regs))
  1195. return ERR_CAST(dphy->regs);
  1196. dphy->psm_clk = of_clk_get_by_name(args.np, "psm");
  1197. if (IS_ERR(dphy->psm_clk))
  1198. return ERR_CAST(dphy->psm_clk);
  1199. dphy->pll_ref_clk = of_clk_get_by_name(args.np, "pll_ref");
  1200. if (IS_ERR(dphy->pll_ref_clk)) {
  1201. ret = PTR_ERR(dphy->pll_ref_clk);
  1202. goto err_put_psm_clk;
  1203. }
  1204. if (dphy->ops->probe) {
  1205. ret = dphy->ops->probe(dphy);
  1206. if (ret)
  1207. goto err_put_pll_ref_clk;
  1208. }
  1209. return dphy;
  1210. err_put_pll_ref_clk:
  1211. clk_put(dphy->pll_ref_clk);
  1212. err_put_psm_clk:
  1213. clk_put(dphy->psm_clk);
  1214. return ERR_PTR(ret);
  1215. }
  1216. static void cdns_dphy_remove(struct cdns_dphy *dphy)
  1217. {
  1218. if (dphy->ops->remove)
  1219. dphy->ops->remove(dphy);
  1220. clk_put(dphy->pll_ref_clk);
  1221. clk_put(dphy->psm_clk);
  1222. }
  1223. static int cdns_dsi_drm_probe(struct platform_device *pdev)
  1224. {
  1225. struct cdns_dsi *dsi;
  1226. struct cdns_dsi_input *input;
  1227. struct resource *res;
  1228. int ret, irq;
  1229. u32 val;
  1230. dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
  1231. if (!dsi)
  1232. return -ENOMEM;
  1233. platform_set_drvdata(pdev, dsi);
  1234. input = &dsi->input;
  1235. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1236. dsi->regs = devm_ioremap_resource(&pdev->dev, res);
  1237. if (IS_ERR(dsi->regs))
  1238. return PTR_ERR(dsi->regs);
  1239. dsi->dsi_p_clk = devm_clk_get(&pdev->dev, "dsi_p_clk");
  1240. if (IS_ERR(dsi->dsi_p_clk))
  1241. return PTR_ERR(dsi->dsi_p_clk);
  1242. dsi->dsi_p_rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
  1243. "dsi_p_rst");
  1244. if (IS_ERR(dsi->dsi_p_rst))
  1245. return PTR_ERR(dsi->dsi_p_rst);
  1246. dsi->dsi_sys_clk = devm_clk_get(&pdev->dev, "dsi_sys_clk");
  1247. if (IS_ERR(dsi->dsi_sys_clk))
  1248. return PTR_ERR(dsi->dsi_sys_clk);
  1249. irq = platform_get_irq(pdev, 0);
  1250. if (irq < 0)
  1251. return irq;
  1252. dsi->dphy = cdns_dphy_probe(pdev);
  1253. if (IS_ERR(dsi->dphy))
  1254. return PTR_ERR(dsi->dphy);
  1255. ret = clk_prepare_enable(dsi->dsi_p_clk);
  1256. if (ret)
  1257. goto err_remove_dphy;
  1258. val = readl(dsi->regs + ID_REG);
  1259. if (REV_VENDOR_ID(val) != 0xcad) {
  1260. dev_err(&pdev->dev, "invalid vendor id\n");
  1261. ret = -EINVAL;
  1262. goto err_disable_pclk;
  1263. }
  1264. val = readl(dsi->regs + IP_CONF);
  1265. dsi->direct_cmd_fifo_depth = 1 << (DIRCMD_FIFO_DEPTH(val) + 2);
  1266. dsi->rx_fifo_depth = RX_FIFO_DEPTH(val);
  1267. init_completion(&dsi->direct_cmd_comp);
  1268. writel(0, dsi->regs + MCTL_MAIN_DATA_CTL);
  1269. writel(0, dsi->regs + MCTL_MAIN_EN);
  1270. writel(0, dsi->regs + MCTL_MAIN_PHY_CTL);
  1271. /*
  1272. * We only support the DPI input, so force input->id to
  1273. * CDNS_DPI_INPUT.
  1274. */
  1275. input->id = CDNS_DPI_INPUT;
  1276. input->bridge.funcs = &cdns_dsi_bridge_funcs;
  1277. input->bridge.of_node = pdev->dev.of_node;
  1278. /* Mask all interrupts before registering the IRQ handler. */
  1279. writel(0, dsi->regs + MCTL_MAIN_STS_CTL);
  1280. writel(0, dsi->regs + MCTL_DPHY_ERR_CTL1);
  1281. writel(0, dsi->regs + CMD_MODE_STS_CTL);
  1282. writel(0, dsi->regs + DIRECT_CMD_STS_CTL);
  1283. writel(0, dsi->regs + DIRECT_CMD_RD_STS_CTL);
  1284. writel(0, dsi->regs + VID_MODE_STS_CTL);
  1285. writel(0, dsi->regs + TVG_STS_CTL);
  1286. writel(0, dsi->regs + DPI_IRQ_EN);
  1287. ret = devm_request_irq(&pdev->dev, irq, cdns_dsi_interrupt, 0,
  1288. dev_name(&pdev->dev), dsi);
  1289. if (ret)
  1290. goto err_disable_pclk;
  1291. pm_runtime_enable(&pdev->dev);
  1292. dsi->base.dev = &pdev->dev;
  1293. dsi->base.ops = &cdns_dsi_ops;
  1294. ret = mipi_dsi_host_register(&dsi->base);
  1295. if (ret)
  1296. goto err_disable_runtime_pm;
  1297. clk_disable_unprepare(dsi->dsi_p_clk);
  1298. return 0;
  1299. err_disable_runtime_pm:
  1300. pm_runtime_disable(&pdev->dev);
  1301. err_disable_pclk:
  1302. clk_disable_unprepare(dsi->dsi_p_clk);
  1303. err_remove_dphy:
  1304. cdns_dphy_remove(dsi->dphy);
  1305. return ret;
  1306. }
  1307. static int cdns_dsi_drm_remove(struct platform_device *pdev)
  1308. {
  1309. struct cdns_dsi *dsi = platform_get_drvdata(pdev);
  1310. mipi_dsi_host_unregister(&dsi->base);
  1311. pm_runtime_disable(&pdev->dev);
  1312. cdns_dphy_remove(dsi->dphy);
  1313. return 0;
  1314. }
  1315. static const struct of_device_id cdns_dsi_of_match[] = {
  1316. { .compatible = "cdns,dsi" },
  1317. { },
  1318. };
  1319. static struct platform_driver cdns_dsi_platform_driver = {
  1320. .probe = cdns_dsi_drm_probe,
  1321. .remove = cdns_dsi_drm_remove,
  1322. .driver = {
  1323. .name = "cdns-dsi",
  1324. .of_match_table = cdns_dsi_of_match,
  1325. .pm = &cdns_dsi_pm_ops,
  1326. },
  1327. };
  1328. module_platform_driver(cdns_dsi_platform_driver);
  1329. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
  1330. MODULE_DESCRIPTION("Cadence DSI driver");
  1331. MODULE_LICENSE("GPL");
  1332. MODULE_ALIAS("platform:cdns-dsi");