exynos_drm_dsi.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. /*
  2. * Samsung SoC MIPI DSI Master driver.
  3. *
  4. * Copyright (c) 2014 Samsung Electronics Co., Ltd
  5. *
  6. * Contacts: Tomasz Figa <t.figa@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <asm/unaligned.h>
  13. #include <drm/drmP.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include <drm/drm_mipi_dsi.h>
  16. #include <drm/drm_panel.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <linux/clk.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/irq.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/of_graph.h>
  24. #include <linux/phy/phy.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/component.h>
  27. #include <video/mipi_display.h>
  28. #include <video/videomode.h>
  29. #include "exynos_drm_crtc.h"
  30. #include "exynos_drm_drv.h"
  31. /* returns true iff both arguments logically differs */
  32. #define NEQV(a, b) (!(a) ^ !(b))
  33. /* DSIM_STATUS */
  34. #define DSIM_STOP_STATE_DAT(x) (((x) & 0xf) << 0)
  35. #define DSIM_STOP_STATE_CLK (1 << 8)
  36. #define DSIM_TX_READY_HS_CLK (1 << 10)
  37. #define DSIM_PLL_STABLE (1 << 31)
  38. /* DSIM_SWRST */
  39. #define DSIM_FUNCRST (1 << 16)
  40. #define DSIM_SWRST (1 << 0)
  41. /* DSIM_TIMEOUT */
  42. #define DSIM_LPDR_TIMEOUT(x) ((x) << 0)
  43. #define DSIM_BTA_TIMEOUT(x) ((x) << 16)
  44. /* DSIM_CLKCTRL */
  45. #define DSIM_ESC_PRESCALER(x) (((x) & 0xffff) << 0)
  46. #define DSIM_ESC_PRESCALER_MASK (0xffff << 0)
  47. #define DSIM_LANE_ESC_CLK_EN_CLK (1 << 19)
  48. #define DSIM_LANE_ESC_CLK_EN_DATA(x) (((x) & 0xf) << 20)
  49. #define DSIM_LANE_ESC_CLK_EN_DATA_MASK (0xf << 20)
  50. #define DSIM_BYTE_CLKEN (1 << 24)
  51. #define DSIM_BYTE_CLK_SRC(x) (((x) & 0x3) << 25)
  52. #define DSIM_BYTE_CLK_SRC_MASK (0x3 << 25)
  53. #define DSIM_PLL_BYPASS (1 << 27)
  54. #define DSIM_ESC_CLKEN (1 << 28)
  55. #define DSIM_TX_REQUEST_HSCLK (1 << 31)
  56. /* DSIM_CONFIG */
  57. #define DSIM_LANE_EN_CLK (1 << 0)
  58. #define DSIM_LANE_EN(x) (((x) & 0xf) << 1)
  59. #define DSIM_NUM_OF_DATA_LANE(x) (((x) & 0x3) << 5)
  60. #define DSIM_SUB_PIX_FORMAT(x) (((x) & 0x7) << 8)
  61. #define DSIM_MAIN_PIX_FORMAT_MASK (0x7 << 12)
  62. #define DSIM_MAIN_PIX_FORMAT_RGB888 (0x7 << 12)
  63. #define DSIM_MAIN_PIX_FORMAT_RGB666 (0x6 << 12)
  64. #define DSIM_MAIN_PIX_FORMAT_RGB666_P (0x5 << 12)
  65. #define DSIM_MAIN_PIX_FORMAT_RGB565 (0x4 << 12)
  66. #define DSIM_SUB_VC (((x) & 0x3) << 16)
  67. #define DSIM_MAIN_VC (((x) & 0x3) << 18)
  68. #define DSIM_HSA_MODE (1 << 20)
  69. #define DSIM_HBP_MODE (1 << 21)
  70. #define DSIM_HFP_MODE (1 << 22)
  71. #define DSIM_HSE_MODE (1 << 23)
  72. #define DSIM_AUTO_MODE (1 << 24)
  73. #define DSIM_VIDEO_MODE (1 << 25)
  74. #define DSIM_BURST_MODE (1 << 26)
  75. #define DSIM_SYNC_INFORM (1 << 27)
  76. #define DSIM_EOT_DISABLE (1 << 28)
  77. #define DSIM_MFLUSH_VS (1 << 29)
  78. /* This flag is valid only for exynos3250/3472/5260/5430 */
  79. #define DSIM_CLKLANE_STOP (1 << 30)
  80. /* DSIM_ESCMODE */
  81. #define DSIM_TX_TRIGGER_RST (1 << 4)
  82. #define DSIM_TX_LPDT_LP (1 << 6)
  83. #define DSIM_CMD_LPDT_LP (1 << 7)
  84. #define DSIM_FORCE_BTA (1 << 16)
  85. #define DSIM_FORCE_STOP_STATE (1 << 20)
  86. #define DSIM_STOP_STATE_CNT(x) (((x) & 0x7ff) << 21)
  87. #define DSIM_STOP_STATE_CNT_MASK (0x7ff << 21)
  88. /* DSIM_MDRESOL */
  89. #define DSIM_MAIN_STAND_BY (1 << 31)
  90. #define DSIM_MAIN_VRESOL(x, num_bits) (((x) & ((1 << (num_bits)) - 1)) << 16)
  91. #define DSIM_MAIN_HRESOL(x, num_bits) (((x) & ((1 << (num_bits)) - 1)) << 0)
  92. /* DSIM_MVPORCH */
  93. #define DSIM_CMD_ALLOW(x) ((x) << 28)
  94. #define DSIM_STABLE_VFP(x) ((x) << 16)
  95. #define DSIM_MAIN_VBP(x) ((x) << 0)
  96. #define DSIM_CMD_ALLOW_MASK (0xf << 28)
  97. #define DSIM_STABLE_VFP_MASK (0x7ff << 16)
  98. #define DSIM_MAIN_VBP_MASK (0x7ff << 0)
  99. /* DSIM_MHPORCH */
  100. #define DSIM_MAIN_HFP(x) ((x) << 16)
  101. #define DSIM_MAIN_HBP(x) ((x) << 0)
  102. #define DSIM_MAIN_HFP_MASK ((0xffff) << 16)
  103. #define DSIM_MAIN_HBP_MASK ((0xffff) << 0)
  104. /* DSIM_MSYNC */
  105. #define DSIM_MAIN_VSA(x) ((x) << 22)
  106. #define DSIM_MAIN_HSA(x) ((x) << 0)
  107. #define DSIM_MAIN_VSA_MASK ((0x3ff) << 22)
  108. #define DSIM_MAIN_HSA_MASK ((0xffff) << 0)
  109. /* DSIM_SDRESOL */
  110. #define DSIM_SUB_STANDY(x) ((x) << 31)
  111. #define DSIM_SUB_VRESOL(x) ((x) << 16)
  112. #define DSIM_SUB_HRESOL(x) ((x) << 0)
  113. #define DSIM_SUB_STANDY_MASK ((0x1) << 31)
  114. #define DSIM_SUB_VRESOL_MASK ((0x7ff) << 16)
  115. #define DSIM_SUB_HRESOL_MASK ((0x7ff) << 0)
  116. /* DSIM_INTSRC */
  117. #define DSIM_INT_PLL_STABLE (1 << 31)
  118. #define DSIM_INT_SW_RST_RELEASE (1 << 30)
  119. #define DSIM_INT_SFR_FIFO_EMPTY (1 << 29)
  120. #define DSIM_INT_SFR_HDR_FIFO_EMPTY (1 << 28)
  121. #define DSIM_INT_BTA (1 << 25)
  122. #define DSIM_INT_FRAME_DONE (1 << 24)
  123. #define DSIM_INT_RX_TIMEOUT (1 << 21)
  124. #define DSIM_INT_BTA_TIMEOUT (1 << 20)
  125. #define DSIM_INT_RX_DONE (1 << 18)
  126. #define DSIM_INT_RX_TE (1 << 17)
  127. #define DSIM_INT_RX_ACK (1 << 16)
  128. #define DSIM_INT_RX_ECC_ERR (1 << 15)
  129. #define DSIM_INT_RX_CRC_ERR (1 << 14)
  130. /* DSIM_FIFOCTRL */
  131. #define DSIM_RX_DATA_FULL (1 << 25)
  132. #define DSIM_RX_DATA_EMPTY (1 << 24)
  133. #define DSIM_SFR_HEADER_FULL (1 << 23)
  134. #define DSIM_SFR_HEADER_EMPTY (1 << 22)
  135. #define DSIM_SFR_PAYLOAD_FULL (1 << 21)
  136. #define DSIM_SFR_PAYLOAD_EMPTY (1 << 20)
  137. #define DSIM_I80_HEADER_FULL (1 << 19)
  138. #define DSIM_I80_HEADER_EMPTY (1 << 18)
  139. #define DSIM_I80_PAYLOAD_FULL (1 << 17)
  140. #define DSIM_I80_PAYLOAD_EMPTY (1 << 16)
  141. #define DSIM_SD_HEADER_FULL (1 << 15)
  142. #define DSIM_SD_HEADER_EMPTY (1 << 14)
  143. #define DSIM_SD_PAYLOAD_FULL (1 << 13)
  144. #define DSIM_SD_PAYLOAD_EMPTY (1 << 12)
  145. #define DSIM_MD_HEADER_FULL (1 << 11)
  146. #define DSIM_MD_HEADER_EMPTY (1 << 10)
  147. #define DSIM_MD_PAYLOAD_FULL (1 << 9)
  148. #define DSIM_MD_PAYLOAD_EMPTY (1 << 8)
  149. #define DSIM_RX_FIFO (1 << 4)
  150. #define DSIM_SFR_FIFO (1 << 3)
  151. #define DSIM_I80_FIFO (1 << 2)
  152. #define DSIM_SD_FIFO (1 << 1)
  153. #define DSIM_MD_FIFO (1 << 0)
  154. /* DSIM_PHYACCHR */
  155. #define DSIM_AFC_EN (1 << 14)
  156. #define DSIM_AFC_CTL(x) (((x) & 0x7) << 5)
  157. /* DSIM_PLLCTRL */
  158. #define DSIM_FREQ_BAND(x) ((x) << 24)
  159. #define DSIM_PLL_EN (1 << 23)
  160. #define DSIM_PLL_P(x) ((x) << 13)
  161. #define DSIM_PLL_M(x) ((x) << 4)
  162. #define DSIM_PLL_S(x) ((x) << 1)
  163. /* DSIM_PHYCTRL */
  164. #define DSIM_PHYCTRL_ULPS_EXIT(x) (((x) & 0x1ff) << 0)
  165. #define DSIM_PHYCTRL_B_DPHYCTL_VREG_LP (1 << 30)
  166. #define DSIM_PHYCTRL_B_DPHYCTL_SLEW_UP (1 << 14)
  167. /* DSIM_PHYTIMING */
  168. #define DSIM_PHYTIMING_LPX(x) ((x) << 8)
  169. #define DSIM_PHYTIMING_HS_EXIT(x) ((x) << 0)
  170. /* DSIM_PHYTIMING1 */
  171. #define DSIM_PHYTIMING1_CLK_PREPARE(x) ((x) << 24)
  172. #define DSIM_PHYTIMING1_CLK_ZERO(x) ((x) << 16)
  173. #define DSIM_PHYTIMING1_CLK_POST(x) ((x) << 8)
  174. #define DSIM_PHYTIMING1_CLK_TRAIL(x) ((x) << 0)
  175. /* DSIM_PHYTIMING2 */
  176. #define DSIM_PHYTIMING2_HS_PREPARE(x) ((x) << 16)
  177. #define DSIM_PHYTIMING2_HS_ZERO(x) ((x) << 8)
  178. #define DSIM_PHYTIMING2_HS_TRAIL(x) ((x) << 0)
  179. #define DSI_MAX_BUS_WIDTH 4
  180. #define DSI_NUM_VIRTUAL_CHANNELS 4
  181. #define DSI_TX_FIFO_SIZE 2048
  182. #define DSI_RX_FIFO_SIZE 256
  183. #define DSI_XFER_TIMEOUT_MS 100
  184. #define DSI_RX_FIFO_EMPTY 0x30800002
  185. #define OLD_SCLK_MIPI_CLK_NAME "pll_clk"
  186. static char *clk_names[5] = { "bus_clk", "sclk_mipi",
  187. "phyclk_mipidphy0_bitclkdiv8", "phyclk_mipidphy0_rxclkesc0",
  188. "sclk_rgb_vclk_to_dsim0" };
  189. enum exynos_dsi_transfer_type {
  190. EXYNOS_DSI_TX,
  191. EXYNOS_DSI_RX,
  192. };
  193. struct exynos_dsi_transfer {
  194. struct list_head list;
  195. struct completion completed;
  196. int result;
  197. struct mipi_dsi_packet packet;
  198. u16 flags;
  199. u16 tx_done;
  200. u8 *rx_payload;
  201. u16 rx_len;
  202. u16 rx_done;
  203. };
  204. #define DSIM_STATE_ENABLED BIT(0)
  205. #define DSIM_STATE_INITIALIZED BIT(1)
  206. #define DSIM_STATE_CMD_LPM BIT(2)
  207. #define DSIM_STATE_VIDOUT_AVAILABLE BIT(3)
  208. struct exynos_dsi_driver_data {
  209. const unsigned int *reg_ofs;
  210. unsigned int plltmr_reg;
  211. unsigned int has_freqband:1;
  212. unsigned int has_clklane_stop:1;
  213. unsigned int num_clks;
  214. unsigned int max_freq;
  215. unsigned int wait_for_reset;
  216. unsigned int num_bits_resol;
  217. const unsigned int *reg_values;
  218. };
  219. struct exynos_dsi {
  220. struct drm_encoder encoder;
  221. struct mipi_dsi_host dsi_host;
  222. struct drm_connector connector;
  223. struct drm_panel *panel;
  224. struct device *dev;
  225. void __iomem *reg_base;
  226. struct phy *phy;
  227. struct clk **clks;
  228. struct regulator_bulk_data supplies[2];
  229. int irq;
  230. int te_gpio;
  231. u32 pll_clk_rate;
  232. u32 burst_clk_rate;
  233. u32 esc_clk_rate;
  234. u32 lanes;
  235. u32 mode_flags;
  236. u32 format;
  237. struct videomode vm;
  238. int state;
  239. struct drm_property *brightness;
  240. struct completion completed;
  241. spinlock_t transfer_lock; /* protects transfer_list */
  242. struct list_head transfer_list;
  243. const struct exynos_dsi_driver_data *driver_data;
  244. struct device_node *bridge_node;
  245. };
  246. #define host_to_dsi(host) container_of(host, struct exynos_dsi, dsi_host)
  247. #define connector_to_dsi(c) container_of(c, struct exynos_dsi, connector)
  248. static inline struct exynos_dsi *encoder_to_dsi(struct drm_encoder *e)
  249. {
  250. return container_of(e, struct exynos_dsi, encoder);
  251. }
  252. enum reg_idx {
  253. DSIM_STATUS_REG, /* Status register */
  254. DSIM_SWRST_REG, /* Software reset register */
  255. DSIM_CLKCTRL_REG, /* Clock control register */
  256. DSIM_TIMEOUT_REG, /* Time out register */
  257. DSIM_CONFIG_REG, /* Configuration register */
  258. DSIM_ESCMODE_REG, /* Escape mode register */
  259. DSIM_MDRESOL_REG,
  260. DSIM_MVPORCH_REG, /* Main display Vporch register */
  261. DSIM_MHPORCH_REG, /* Main display Hporch register */
  262. DSIM_MSYNC_REG, /* Main display sync area register */
  263. DSIM_INTSRC_REG, /* Interrupt source register */
  264. DSIM_INTMSK_REG, /* Interrupt mask register */
  265. DSIM_PKTHDR_REG, /* Packet Header FIFO register */
  266. DSIM_PAYLOAD_REG, /* Payload FIFO register */
  267. DSIM_RXFIFO_REG, /* Read FIFO register */
  268. DSIM_FIFOCTRL_REG, /* FIFO status and control register */
  269. DSIM_PLLCTRL_REG, /* PLL control register */
  270. DSIM_PHYCTRL_REG,
  271. DSIM_PHYTIMING_REG,
  272. DSIM_PHYTIMING1_REG,
  273. DSIM_PHYTIMING2_REG,
  274. NUM_REGS
  275. };
  276. static inline void exynos_dsi_write(struct exynos_dsi *dsi, enum reg_idx idx,
  277. u32 val)
  278. {
  279. writel(val, dsi->reg_base + dsi->driver_data->reg_ofs[idx]);
  280. }
  281. static inline u32 exynos_dsi_read(struct exynos_dsi *dsi, enum reg_idx idx)
  282. {
  283. return readl(dsi->reg_base + dsi->driver_data->reg_ofs[idx]);
  284. }
  285. static const unsigned int exynos_reg_ofs[] = {
  286. [DSIM_STATUS_REG] = 0x00,
  287. [DSIM_SWRST_REG] = 0x04,
  288. [DSIM_CLKCTRL_REG] = 0x08,
  289. [DSIM_TIMEOUT_REG] = 0x0c,
  290. [DSIM_CONFIG_REG] = 0x10,
  291. [DSIM_ESCMODE_REG] = 0x14,
  292. [DSIM_MDRESOL_REG] = 0x18,
  293. [DSIM_MVPORCH_REG] = 0x1c,
  294. [DSIM_MHPORCH_REG] = 0x20,
  295. [DSIM_MSYNC_REG] = 0x24,
  296. [DSIM_INTSRC_REG] = 0x2c,
  297. [DSIM_INTMSK_REG] = 0x30,
  298. [DSIM_PKTHDR_REG] = 0x34,
  299. [DSIM_PAYLOAD_REG] = 0x38,
  300. [DSIM_RXFIFO_REG] = 0x3c,
  301. [DSIM_FIFOCTRL_REG] = 0x44,
  302. [DSIM_PLLCTRL_REG] = 0x4c,
  303. [DSIM_PHYCTRL_REG] = 0x5c,
  304. [DSIM_PHYTIMING_REG] = 0x64,
  305. [DSIM_PHYTIMING1_REG] = 0x68,
  306. [DSIM_PHYTIMING2_REG] = 0x6c,
  307. };
  308. static const unsigned int exynos5433_reg_ofs[] = {
  309. [DSIM_STATUS_REG] = 0x04,
  310. [DSIM_SWRST_REG] = 0x0C,
  311. [DSIM_CLKCTRL_REG] = 0x10,
  312. [DSIM_TIMEOUT_REG] = 0x14,
  313. [DSIM_CONFIG_REG] = 0x18,
  314. [DSIM_ESCMODE_REG] = 0x1C,
  315. [DSIM_MDRESOL_REG] = 0x20,
  316. [DSIM_MVPORCH_REG] = 0x24,
  317. [DSIM_MHPORCH_REG] = 0x28,
  318. [DSIM_MSYNC_REG] = 0x2C,
  319. [DSIM_INTSRC_REG] = 0x34,
  320. [DSIM_INTMSK_REG] = 0x38,
  321. [DSIM_PKTHDR_REG] = 0x3C,
  322. [DSIM_PAYLOAD_REG] = 0x40,
  323. [DSIM_RXFIFO_REG] = 0x44,
  324. [DSIM_FIFOCTRL_REG] = 0x4C,
  325. [DSIM_PLLCTRL_REG] = 0x94,
  326. [DSIM_PHYCTRL_REG] = 0xA4,
  327. [DSIM_PHYTIMING_REG] = 0xB4,
  328. [DSIM_PHYTIMING1_REG] = 0xB8,
  329. [DSIM_PHYTIMING2_REG] = 0xBC,
  330. };
  331. enum reg_value_idx {
  332. RESET_TYPE,
  333. PLL_TIMER,
  334. STOP_STATE_CNT,
  335. PHYCTRL_ULPS_EXIT,
  336. PHYCTRL_VREG_LP,
  337. PHYCTRL_SLEW_UP,
  338. PHYTIMING_LPX,
  339. PHYTIMING_HS_EXIT,
  340. PHYTIMING_CLK_PREPARE,
  341. PHYTIMING_CLK_ZERO,
  342. PHYTIMING_CLK_POST,
  343. PHYTIMING_CLK_TRAIL,
  344. PHYTIMING_HS_PREPARE,
  345. PHYTIMING_HS_ZERO,
  346. PHYTIMING_HS_TRAIL
  347. };
  348. static const unsigned int reg_values[] = {
  349. [RESET_TYPE] = DSIM_SWRST,
  350. [PLL_TIMER] = 500,
  351. [STOP_STATE_CNT] = 0xf,
  352. [PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0x0af),
  353. [PHYCTRL_VREG_LP] = 0,
  354. [PHYCTRL_SLEW_UP] = 0,
  355. [PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x06),
  356. [PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0b),
  357. [PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x07),
  358. [PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x27),
  359. [PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0d),
  360. [PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x08),
  361. [PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x09),
  362. [PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x0d),
  363. [PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0b),
  364. };
  365. static const unsigned int exynos5422_reg_values[] = {
  366. [RESET_TYPE] = DSIM_SWRST,
  367. [PLL_TIMER] = 500,
  368. [STOP_STATE_CNT] = 0xf,
  369. [PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0xaf),
  370. [PHYCTRL_VREG_LP] = 0,
  371. [PHYCTRL_SLEW_UP] = 0,
  372. [PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x08),
  373. [PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0d),
  374. [PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x09),
  375. [PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x30),
  376. [PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0e),
  377. [PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x0a),
  378. [PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x0c),
  379. [PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x11),
  380. [PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0d),
  381. };
  382. static const unsigned int exynos5433_reg_values[] = {
  383. [RESET_TYPE] = DSIM_FUNCRST,
  384. [PLL_TIMER] = 22200,
  385. [STOP_STATE_CNT] = 0xa,
  386. [PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0x190),
  387. [PHYCTRL_VREG_LP] = DSIM_PHYCTRL_B_DPHYCTL_VREG_LP,
  388. [PHYCTRL_SLEW_UP] = DSIM_PHYCTRL_B_DPHYCTL_SLEW_UP,
  389. [PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x07),
  390. [PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0c),
  391. [PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x09),
  392. [PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x2d),
  393. [PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0e),
  394. [PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x09),
  395. [PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x0b),
  396. [PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x10),
  397. [PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0c),
  398. };
  399. static const struct exynos_dsi_driver_data exynos3_dsi_driver_data = {
  400. .reg_ofs = exynos_reg_ofs,
  401. .plltmr_reg = 0x50,
  402. .has_freqband = 1,
  403. .has_clklane_stop = 1,
  404. .num_clks = 2,
  405. .max_freq = 1000,
  406. .wait_for_reset = 1,
  407. .num_bits_resol = 11,
  408. .reg_values = reg_values,
  409. };
  410. static const struct exynos_dsi_driver_data exynos4_dsi_driver_data = {
  411. .reg_ofs = exynos_reg_ofs,
  412. .plltmr_reg = 0x50,
  413. .has_freqband = 1,
  414. .has_clklane_stop = 1,
  415. .num_clks = 2,
  416. .max_freq = 1000,
  417. .wait_for_reset = 1,
  418. .num_bits_resol = 11,
  419. .reg_values = reg_values,
  420. };
  421. static const struct exynos_dsi_driver_data exynos5_dsi_driver_data = {
  422. .reg_ofs = exynos_reg_ofs,
  423. .plltmr_reg = 0x58,
  424. .num_clks = 2,
  425. .max_freq = 1000,
  426. .wait_for_reset = 1,
  427. .num_bits_resol = 11,
  428. .reg_values = reg_values,
  429. };
  430. static const struct exynos_dsi_driver_data exynos5433_dsi_driver_data = {
  431. .reg_ofs = exynos5433_reg_ofs,
  432. .plltmr_reg = 0xa0,
  433. .has_clklane_stop = 1,
  434. .num_clks = 5,
  435. .max_freq = 1500,
  436. .wait_for_reset = 0,
  437. .num_bits_resol = 12,
  438. .reg_values = exynos5433_reg_values,
  439. };
  440. static const struct exynos_dsi_driver_data exynos5422_dsi_driver_data = {
  441. .reg_ofs = exynos5433_reg_ofs,
  442. .plltmr_reg = 0xa0,
  443. .has_clklane_stop = 1,
  444. .num_clks = 2,
  445. .max_freq = 1500,
  446. .wait_for_reset = 1,
  447. .num_bits_resol = 12,
  448. .reg_values = exynos5422_reg_values,
  449. };
  450. static const struct of_device_id exynos_dsi_of_match[] = {
  451. { .compatible = "samsung,exynos3250-mipi-dsi",
  452. .data = &exynos3_dsi_driver_data },
  453. { .compatible = "samsung,exynos4210-mipi-dsi",
  454. .data = &exynos4_dsi_driver_data },
  455. { .compatible = "samsung,exynos5410-mipi-dsi",
  456. .data = &exynos5_dsi_driver_data },
  457. { .compatible = "samsung,exynos5422-mipi-dsi",
  458. .data = &exynos5422_dsi_driver_data },
  459. { .compatible = "samsung,exynos5433-mipi-dsi",
  460. .data = &exynos5433_dsi_driver_data },
  461. { }
  462. };
  463. static void exynos_dsi_wait_for_reset(struct exynos_dsi *dsi)
  464. {
  465. if (wait_for_completion_timeout(&dsi->completed, msecs_to_jiffies(300)))
  466. return;
  467. dev_err(dsi->dev, "timeout waiting for reset\n");
  468. }
  469. static void exynos_dsi_reset(struct exynos_dsi *dsi)
  470. {
  471. u32 reset_val = dsi->driver_data->reg_values[RESET_TYPE];
  472. reinit_completion(&dsi->completed);
  473. exynos_dsi_write(dsi, DSIM_SWRST_REG, reset_val);
  474. }
  475. #ifndef MHZ
  476. #define MHZ (1000*1000)
  477. #endif
  478. static unsigned long exynos_dsi_pll_find_pms(struct exynos_dsi *dsi,
  479. unsigned long fin, unsigned long fout, u8 *p, u16 *m, u8 *s)
  480. {
  481. const struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
  482. unsigned long best_freq = 0;
  483. u32 min_delta = 0xffffffff;
  484. u8 p_min, p_max;
  485. u8 _p, uninitialized_var(best_p);
  486. u16 _m, uninitialized_var(best_m);
  487. u8 _s, uninitialized_var(best_s);
  488. p_min = DIV_ROUND_UP(fin, (12 * MHZ));
  489. p_max = fin / (6 * MHZ);
  490. for (_p = p_min; _p <= p_max; ++_p) {
  491. for (_s = 0; _s <= 5; ++_s) {
  492. u64 tmp;
  493. u32 delta;
  494. tmp = (u64)fout * (_p << _s);
  495. do_div(tmp, fin);
  496. _m = tmp;
  497. if (_m < 41 || _m > 125)
  498. continue;
  499. tmp = (u64)_m * fin;
  500. do_div(tmp, _p);
  501. if (tmp < 500 * MHZ ||
  502. tmp > driver_data->max_freq * MHZ)
  503. continue;
  504. tmp = (u64)_m * fin;
  505. do_div(tmp, _p << _s);
  506. delta = abs(fout - tmp);
  507. if (delta < min_delta) {
  508. best_p = _p;
  509. best_m = _m;
  510. best_s = _s;
  511. min_delta = delta;
  512. best_freq = tmp;
  513. }
  514. }
  515. }
  516. if (best_freq) {
  517. *p = best_p;
  518. *m = best_m;
  519. *s = best_s;
  520. }
  521. return best_freq;
  522. }
  523. static unsigned long exynos_dsi_set_pll(struct exynos_dsi *dsi,
  524. unsigned long freq)
  525. {
  526. const struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
  527. unsigned long fin, fout;
  528. int timeout;
  529. u8 p, s;
  530. u16 m;
  531. u32 reg;
  532. fin = dsi->pll_clk_rate;
  533. fout = exynos_dsi_pll_find_pms(dsi, fin, freq, &p, &m, &s);
  534. if (!fout) {
  535. dev_err(dsi->dev,
  536. "failed to find PLL PMS for requested frequency\n");
  537. return 0;
  538. }
  539. dev_dbg(dsi->dev, "PLL freq %lu, (p %d, m %d, s %d)\n", fout, p, m, s);
  540. writel(driver_data->reg_values[PLL_TIMER],
  541. dsi->reg_base + driver_data->plltmr_reg);
  542. reg = DSIM_PLL_EN | DSIM_PLL_P(p) | DSIM_PLL_M(m) | DSIM_PLL_S(s);
  543. if (driver_data->has_freqband) {
  544. static const unsigned long freq_bands[] = {
  545. 100 * MHZ, 120 * MHZ, 160 * MHZ, 200 * MHZ,
  546. 270 * MHZ, 320 * MHZ, 390 * MHZ, 450 * MHZ,
  547. 510 * MHZ, 560 * MHZ, 640 * MHZ, 690 * MHZ,
  548. 770 * MHZ, 870 * MHZ, 950 * MHZ,
  549. };
  550. int band;
  551. for (band = 0; band < ARRAY_SIZE(freq_bands); ++band)
  552. if (fout < freq_bands[band])
  553. break;
  554. dev_dbg(dsi->dev, "band %d\n", band);
  555. reg |= DSIM_FREQ_BAND(band);
  556. }
  557. exynos_dsi_write(dsi, DSIM_PLLCTRL_REG, reg);
  558. timeout = 1000;
  559. do {
  560. if (timeout-- == 0) {
  561. dev_err(dsi->dev, "PLL failed to stabilize\n");
  562. return 0;
  563. }
  564. reg = exynos_dsi_read(dsi, DSIM_STATUS_REG);
  565. } while ((reg & DSIM_PLL_STABLE) == 0);
  566. return fout;
  567. }
  568. static int exynos_dsi_enable_clock(struct exynos_dsi *dsi)
  569. {
  570. unsigned long hs_clk, byte_clk, esc_clk;
  571. unsigned long esc_div;
  572. u32 reg;
  573. hs_clk = exynos_dsi_set_pll(dsi, dsi->burst_clk_rate);
  574. if (!hs_clk) {
  575. dev_err(dsi->dev, "failed to configure DSI PLL\n");
  576. return -EFAULT;
  577. }
  578. byte_clk = hs_clk / 8;
  579. esc_div = DIV_ROUND_UP(byte_clk, dsi->esc_clk_rate);
  580. esc_clk = byte_clk / esc_div;
  581. if (esc_clk > 20 * MHZ) {
  582. ++esc_div;
  583. esc_clk = byte_clk / esc_div;
  584. }
  585. dev_dbg(dsi->dev, "hs_clk = %lu, byte_clk = %lu, esc_clk = %lu\n",
  586. hs_clk, byte_clk, esc_clk);
  587. reg = exynos_dsi_read(dsi, DSIM_CLKCTRL_REG);
  588. reg &= ~(DSIM_ESC_PRESCALER_MASK | DSIM_LANE_ESC_CLK_EN_CLK
  589. | DSIM_LANE_ESC_CLK_EN_DATA_MASK | DSIM_PLL_BYPASS
  590. | DSIM_BYTE_CLK_SRC_MASK);
  591. reg |= DSIM_ESC_CLKEN | DSIM_BYTE_CLKEN
  592. | DSIM_ESC_PRESCALER(esc_div)
  593. | DSIM_LANE_ESC_CLK_EN_CLK
  594. | DSIM_LANE_ESC_CLK_EN_DATA(BIT(dsi->lanes) - 1)
  595. | DSIM_BYTE_CLK_SRC(0)
  596. | DSIM_TX_REQUEST_HSCLK;
  597. exynos_dsi_write(dsi, DSIM_CLKCTRL_REG, reg);
  598. return 0;
  599. }
  600. static void exynos_dsi_set_phy_ctrl(struct exynos_dsi *dsi)
  601. {
  602. const struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
  603. const unsigned int *reg_values = driver_data->reg_values;
  604. u32 reg;
  605. if (driver_data->has_freqband)
  606. return;
  607. /* B D-PHY: D-PHY Master & Slave Analog Block control */
  608. reg = reg_values[PHYCTRL_ULPS_EXIT] | reg_values[PHYCTRL_VREG_LP] |
  609. reg_values[PHYCTRL_SLEW_UP];
  610. exynos_dsi_write(dsi, DSIM_PHYCTRL_REG, reg);
  611. /*
  612. * T LPX: Transmitted length of any Low-Power state period
  613. * T HS-EXIT: Time that the transmitter drives LP-11 following a HS
  614. * burst
  615. */
  616. reg = reg_values[PHYTIMING_LPX] | reg_values[PHYTIMING_HS_EXIT];
  617. exynos_dsi_write(dsi, DSIM_PHYTIMING_REG, reg);
  618. /*
  619. * T CLK-PREPARE: Time that the transmitter drives the Clock Lane LP-00
  620. * Line state immediately before the HS-0 Line state starting the
  621. * HS transmission
  622. * T CLK-ZERO: Time that the transmitter drives the HS-0 state prior to
  623. * transmitting the Clock.
  624. * T CLK_POST: Time that the transmitter continues to send HS clock
  625. * after the last associated Data Lane has transitioned to LP Mode
  626. * Interval is defined as the period from the end of T HS-TRAIL to
  627. * the beginning of T CLK-TRAIL
  628. * T CLK-TRAIL: Time that the transmitter drives the HS-0 state after
  629. * the last payload clock bit of a HS transmission burst
  630. */
  631. reg = reg_values[PHYTIMING_CLK_PREPARE] |
  632. reg_values[PHYTIMING_CLK_ZERO] |
  633. reg_values[PHYTIMING_CLK_POST] |
  634. reg_values[PHYTIMING_CLK_TRAIL];
  635. exynos_dsi_write(dsi, DSIM_PHYTIMING1_REG, reg);
  636. /*
  637. * T HS-PREPARE: Time that the transmitter drives the Data Lane LP-00
  638. * Line state immediately before the HS-0 Line state starting the
  639. * HS transmission
  640. * T HS-ZERO: Time that the transmitter drives the HS-0 state prior to
  641. * transmitting the Sync sequence.
  642. * T HS-TRAIL: Time that the transmitter drives the flipped differential
  643. * state after last payload data bit of a HS transmission burst
  644. */
  645. reg = reg_values[PHYTIMING_HS_PREPARE] | reg_values[PHYTIMING_HS_ZERO] |
  646. reg_values[PHYTIMING_HS_TRAIL];
  647. exynos_dsi_write(dsi, DSIM_PHYTIMING2_REG, reg);
  648. }
  649. static void exynos_dsi_disable_clock(struct exynos_dsi *dsi)
  650. {
  651. u32 reg;
  652. reg = exynos_dsi_read(dsi, DSIM_CLKCTRL_REG);
  653. reg &= ~(DSIM_LANE_ESC_CLK_EN_CLK | DSIM_LANE_ESC_CLK_EN_DATA_MASK
  654. | DSIM_ESC_CLKEN | DSIM_BYTE_CLKEN);
  655. exynos_dsi_write(dsi, DSIM_CLKCTRL_REG, reg);
  656. reg = exynos_dsi_read(dsi, DSIM_PLLCTRL_REG);
  657. reg &= ~DSIM_PLL_EN;
  658. exynos_dsi_write(dsi, DSIM_PLLCTRL_REG, reg);
  659. }
  660. static void exynos_dsi_enable_lane(struct exynos_dsi *dsi, u32 lane)
  661. {
  662. u32 reg = exynos_dsi_read(dsi, DSIM_CONFIG_REG);
  663. reg |= (DSIM_NUM_OF_DATA_LANE(dsi->lanes - 1) | DSIM_LANE_EN_CLK |
  664. DSIM_LANE_EN(lane));
  665. exynos_dsi_write(dsi, DSIM_CONFIG_REG, reg);
  666. }
  667. static int exynos_dsi_init_link(struct exynos_dsi *dsi)
  668. {
  669. const struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
  670. int timeout;
  671. u32 reg;
  672. u32 lanes_mask;
  673. /* Initialize FIFO pointers */
  674. reg = exynos_dsi_read(dsi, DSIM_FIFOCTRL_REG);
  675. reg &= ~0x1f;
  676. exynos_dsi_write(dsi, DSIM_FIFOCTRL_REG, reg);
  677. usleep_range(9000, 11000);
  678. reg |= 0x1f;
  679. exynos_dsi_write(dsi, DSIM_FIFOCTRL_REG, reg);
  680. usleep_range(9000, 11000);
  681. /* DSI configuration */
  682. reg = 0;
  683. /*
  684. * The first bit of mode_flags specifies display configuration.
  685. * If this bit is set[= MIPI_DSI_MODE_VIDEO], dsi will support video
  686. * mode, otherwise it will support command mode.
  687. */
  688. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  689. reg |= DSIM_VIDEO_MODE;
  690. /*
  691. * The user manual describes that following bits are ignored in
  692. * command mode.
  693. */
  694. if (!(dsi->mode_flags & MIPI_DSI_MODE_VSYNC_FLUSH))
  695. reg |= DSIM_MFLUSH_VS;
  696. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  697. reg |= DSIM_SYNC_INFORM;
  698. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  699. reg |= DSIM_BURST_MODE;
  700. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_AUTO_VERT)
  701. reg |= DSIM_AUTO_MODE;
  702. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HSE)
  703. reg |= DSIM_HSE_MODE;
  704. if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HFP))
  705. reg |= DSIM_HFP_MODE;
  706. if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HBP))
  707. reg |= DSIM_HBP_MODE;
  708. if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HSA))
  709. reg |= DSIM_HSA_MODE;
  710. }
  711. if (!(dsi->mode_flags & MIPI_DSI_MODE_EOT_PACKET))
  712. reg |= DSIM_EOT_DISABLE;
  713. switch (dsi->format) {
  714. case MIPI_DSI_FMT_RGB888:
  715. reg |= DSIM_MAIN_PIX_FORMAT_RGB888;
  716. break;
  717. case MIPI_DSI_FMT_RGB666:
  718. reg |= DSIM_MAIN_PIX_FORMAT_RGB666;
  719. break;
  720. case MIPI_DSI_FMT_RGB666_PACKED:
  721. reg |= DSIM_MAIN_PIX_FORMAT_RGB666_P;
  722. break;
  723. case MIPI_DSI_FMT_RGB565:
  724. reg |= DSIM_MAIN_PIX_FORMAT_RGB565;
  725. break;
  726. default:
  727. dev_err(dsi->dev, "invalid pixel format\n");
  728. return -EINVAL;
  729. }
  730. /*
  731. * Use non-continuous clock mode if the periparal wants and
  732. * host controller supports
  733. *
  734. * In non-continous clock mode, host controller will turn off
  735. * the HS clock between high-speed transmissions to reduce
  736. * power consumption.
  737. */
  738. if (driver_data->has_clklane_stop &&
  739. dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
  740. reg |= DSIM_CLKLANE_STOP;
  741. }
  742. exynos_dsi_write(dsi, DSIM_CONFIG_REG, reg);
  743. lanes_mask = BIT(dsi->lanes) - 1;
  744. exynos_dsi_enable_lane(dsi, lanes_mask);
  745. /* Check clock and data lane state are stop state */
  746. timeout = 100;
  747. do {
  748. if (timeout-- == 0) {
  749. dev_err(dsi->dev, "waiting for bus lanes timed out\n");
  750. return -EFAULT;
  751. }
  752. reg = exynos_dsi_read(dsi, DSIM_STATUS_REG);
  753. if ((reg & DSIM_STOP_STATE_DAT(lanes_mask))
  754. != DSIM_STOP_STATE_DAT(lanes_mask))
  755. continue;
  756. } while (!(reg & (DSIM_STOP_STATE_CLK | DSIM_TX_READY_HS_CLK)));
  757. reg = exynos_dsi_read(dsi, DSIM_ESCMODE_REG);
  758. reg &= ~DSIM_STOP_STATE_CNT_MASK;
  759. reg |= DSIM_STOP_STATE_CNT(driver_data->reg_values[STOP_STATE_CNT]);
  760. exynos_dsi_write(dsi, DSIM_ESCMODE_REG, reg);
  761. reg = DSIM_BTA_TIMEOUT(0xff) | DSIM_LPDR_TIMEOUT(0xffff);
  762. exynos_dsi_write(dsi, DSIM_TIMEOUT_REG, reg);
  763. return 0;
  764. }
  765. static void exynos_dsi_set_display_mode(struct exynos_dsi *dsi)
  766. {
  767. struct videomode *vm = &dsi->vm;
  768. unsigned int num_bits_resol = dsi->driver_data->num_bits_resol;
  769. u32 reg;
  770. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  771. reg = DSIM_CMD_ALLOW(0xf)
  772. | DSIM_STABLE_VFP(vm->vfront_porch)
  773. | DSIM_MAIN_VBP(vm->vback_porch);
  774. exynos_dsi_write(dsi, DSIM_MVPORCH_REG, reg);
  775. reg = DSIM_MAIN_HFP(vm->hfront_porch)
  776. | DSIM_MAIN_HBP(vm->hback_porch);
  777. exynos_dsi_write(dsi, DSIM_MHPORCH_REG, reg);
  778. reg = DSIM_MAIN_VSA(vm->vsync_len)
  779. | DSIM_MAIN_HSA(vm->hsync_len);
  780. exynos_dsi_write(dsi, DSIM_MSYNC_REG, reg);
  781. }
  782. reg = DSIM_MAIN_HRESOL(vm->hactive, num_bits_resol) |
  783. DSIM_MAIN_VRESOL(vm->vactive, num_bits_resol);
  784. exynos_dsi_write(dsi, DSIM_MDRESOL_REG, reg);
  785. dev_dbg(dsi->dev, "LCD size = %dx%d\n", vm->hactive, vm->vactive);
  786. }
  787. static void exynos_dsi_set_display_enable(struct exynos_dsi *dsi, bool enable)
  788. {
  789. u32 reg;
  790. reg = exynos_dsi_read(dsi, DSIM_MDRESOL_REG);
  791. if (enable)
  792. reg |= DSIM_MAIN_STAND_BY;
  793. else
  794. reg &= ~DSIM_MAIN_STAND_BY;
  795. exynos_dsi_write(dsi, DSIM_MDRESOL_REG, reg);
  796. }
  797. static int exynos_dsi_wait_for_hdr_fifo(struct exynos_dsi *dsi)
  798. {
  799. int timeout = 2000;
  800. do {
  801. u32 reg = exynos_dsi_read(dsi, DSIM_FIFOCTRL_REG);
  802. if (!(reg & DSIM_SFR_HEADER_FULL))
  803. return 0;
  804. if (!cond_resched())
  805. usleep_range(950, 1050);
  806. } while (--timeout);
  807. return -ETIMEDOUT;
  808. }
  809. static void exynos_dsi_set_cmd_lpm(struct exynos_dsi *dsi, bool lpm)
  810. {
  811. u32 v = exynos_dsi_read(dsi, DSIM_ESCMODE_REG);
  812. if (lpm)
  813. v |= DSIM_CMD_LPDT_LP;
  814. else
  815. v &= ~DSIM_CMD_LPDT_LP;
  816. exynos_dsi_write(dsi, DSIM_ESCMODE_REG, v);
  817. }
  818. static void exynos_dsi_force_bta(struct exynos_dsi *dsi)
  819. {
  820. u32 v = exynos_dsi_read(dsi, DSIM_ESCMODE_REG);
  821. v |= DSIM_FORCE_BTA;
  822. exynos_dsi_write(dsi, DSIM_ESCMODE_REG, v);
  823. }
  824. static void exynos_dsi_send_to_fifo(struct exynos_dsi *dsi,
  825. struct exynos_dsi_transfer *xfer)
  826. {
  827. struct device *dev = dsi->dev;
  828. struct mipi_dsi_packet *pkt = &xfer->packet;
  829. const u8 *payload = pkt->payload + xfer->tx_done;
  830. u16 length = pkt->payload_length - xfer->tx_done;
  831. bool first = !xfer->tx_done;
  832. u32 reg;
  833. dev_dbg(dev, "< xfer %pK: tx len %u, done %u, rx len %u, done %u\n",
  834. xfer, length, xfer->tx_done, xfer->rx_len, xfer->rx_done);
  835. if (length > DSI_TX_FIFO_SIZE)
  836. length = DSI_TX_FIFO_SIZE;
  837. xfer->tx_done += length;
  838. /* Send payload */
  839. while (length >= 4) {
  840. reg = get_unaligned_le32(payload);
  841. exynos_dsi_write(dsi, DSIM_PAYLOAD_REG, reg);
  842. payload += 4;
  843. length -= 4;
  844. }
  845. reg = 0;
  846. switch (length) {
  847. case 3:
  848. reg |= payload[2] << 16;
  849. /* Fall through */
  850. case 2:
  851. reg |= payload[1] << 8;
  852. /* Fall through */
  853. case 1:
  854. reg |= payload[0];
  855. exynos_dsi_write(dsi, DSIM_PAYLOAD_REG, reg);
  856. break;
  857. }
  858. /* Send packet header */
  859. if (!first)
  860. return;
  861. reg = get_unaligned_le32(pkt->header);
  862. if (exynos_dsi_wait_for_hdr_fifo(dsi)) {
  863. dev_err(dev, "waiting for header FIFO timed out\n");
  864. return;
  865. }
  866. if (NEQV(xfer->flags & MIPI_DSI_MSG_USE_LPM,
  867. dsi->state & DSIM_STATE_CMD_LPM)) {
  868. exynos_dsi_set_cmd_lpm(dsi, xfer->flags & MIPI_DSI_MSG_USE_LPM);
  869. dsi->state ^= DSIM_STATE_CMD_LPM;
  870. }
  871. exynos_dsi_write(dsi, DSIM_PKTHDR_REG, reg);
  872. if (xfer->flags & MIPI_DSI_MSG_REQ_ACK)
  873. exynos_dsi_force_bta(dsi);
  874. }
  875. static void exynos_dsi_read_from_fifo(struct exynos_dsi *dsi,
  876. struct exynos_dsi_transfer *xfer)
  877. {
  878. u8 *payload = xfer->rx_payload + xfer->rx_done;
  879. bool first = !xfer->rx_done;
  880. struct device *dev = dsi->dev;
  881. u16 length;
  882. u32 reg;
  883. if (first) {
  884. reg = exynos_dsi_read(dsi, DSIM_RXFIFO_REG);
  885. switch (reg & 0x3f) {
  886. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
  887. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  888. if (xfer->rx_len >= 2) {
  889. payload[1] = reg >> 16;
  890. ++xfer->rx_done;
  891. }
  892. /* Fall through */
  893. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
  894. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  895. payload[0] = reg >> 8;
  896. ++xfer->rx_done;
  897. xfer->rx_len = xfer->rx_done;
  898. xfer->result = 0;
  899. goto clear_fifo;
  900. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  901. dev_err(dev, "DSI Error Report: 0x%04x\n",
  902. (reg >> 8) & 0xffff);
  903. xfer->result = 0;
  904. goto clear_fifo;
  905. }
  906. length = (reg >> 8) & 0xffff;
  907. if (length > xfer->rx_len) {
  908. dev_err(dev,
  909. "response too long (%u > %u bytes), stripping\n",
  910. xfer->rx_len, length);
  911. length = xfer->rx_len;
  912. } else if (length < xfer->rx_len)
  913. xfer->rx_len = length;
  914. }
  915. length = xfer->rx_len - xfer->rx_done;
  916. xfer->rx_done += length;
  917. /* Receive payload */
  918. while (length >= 4) {
  919. reg = exynos_dsi_read(dsi, DSIM_RXFIFO_REG);
  920. payload[0] = (reg >> 0) & 0xff;
  921. payload[1] = (reg >> 8) & 0xff;
  922. payload[2] = (reg >> 16) & 0xff;
  923. payload[3] = (reg >> 24) & 0xff;
  924. payload += 4;
  925. length -= 4;
  926. }
  927. if (length) {
  928. reg = exynos_dsi_read(dsi, DSIM_RXFIFO_REG);
  929. switch (length) {
  930. case 3:
  931. payload[2] = (reg >> 16) & 0xff;
  932. /* Fall through */
  933. case 2:
  934. payload[1] = (reg >> 8) & 0xff;
  935. /* Fall through */
  936. case 1:
  937. payload[0] = reg & 0xff;
  938. }
  939. }
  940. if (xfer->rx_done == xfer->rx_len)
  941. xfer->result = 0;
  942. clear_fifo:
  943. length = DSI_RX_FIFO_SIZE / 4;
  944. do {
  945. reg = exynos_dsi_read(dsi, DSIM_RXFIFO_REG);
  946. if (reg == DSI_RX_FIFO_EMPTY)
  947. break;
  948. } while (--length);
  949. }
  950. static void exynos_dsi_transfer_start(struct exynos_dsi *dsi)
  951. {
  952. unsigned long flags;
  953. struct exynos_dsi_transfer *xfer;
  954. bool start = false;
  955. again:
  956. spin_lock_irqsave(&dsi->transfer_lock, flags);
  957. if (list_empty(&dsi->transfer_list)) {
  958. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  959. return;
  960. }
  961. xfer = list_first_entry(&dsi->transfer_list,
  962. struct exynos_dsi_transfer, list);
  963. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  964. if (xfer->packet.payload_length &&
  965. xfer->tx_done == xfer->packet.payload_length)
  966. /* waiting for RX */
  967. return;
  968. exynos_dsi_send_to_fifo(dsi, xfer);
  969. if (xfer->packet.payload_length || xfer->rx_len)
  970. return;
  971. xfer->result = 0;
  972. complete(&xfer->completed);
  973. spin_lock_irqsave(&dsi->transfer_lock, flags);
  974. list_del_init(&xfer->list);
  975. start = !list_empty(&dsi->transfer_list);
  976. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  977. if (start)
  978. goto again;
  979. }
  980. static bool exynos_dsi_transfer_finish(struct exynos_dsi *dsi)
  981. {
  982. struct exynos_dsi_transfer *xfer;
  983. unsigned long flags;
  984. bool start = true;
  985. spin_lock_irqsave(&dsi->transfer_lock, flags);
  986. if (list_empty(&dsi->transfer_list)) {
  987. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  988. return false;
  989. }
  990. xfer = list_first_entry(&dsi->transfer_list,
  991. struct exynos_dsi_transfer, list);
  992. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  993. dev_dbg(dsi->dev,
  994. "> xfer %pK, tx_len %zu, tx_done %u, rx_len %u, rx_done %u\n",
  995. xfer, xfer->packet.payload_length, xfer->tx_done, xfer->rx_len,
  996. xfer->rx_done);
  997. if (xfer->tx_done != xfer->packet.payload_length)
  998. return true;
  999. if (xfer->rx_done != xfer->rx_len)
  1000. exynos_dsi_read_from_fifo(dsi, xfer);
  1001. if (xfer->rx_done != xfer->rx_len)
  1002. return true;
  1003. spin_lock_irqsave(&dsi->transfer_lock, flags);
  1004. list_del_init(&xfer->list);
  1005. start = !list_empty(&dsi->transfer_list);
  1006. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  1007. if (!xfer->rx_len)
  1008. xfer->result = 0;
  1009. complete(&xfer->completed);
  1010. return start;
  1011. }
  1012. static void exynos_dsi_remove_transfer(struct exynos_dsi *dsi,
  1013. struct exynos_dsi_transfer *xfer)
  1014. {
  1015. unsigned long flags;
  1016. bool start;
  1017. spin_lock_irqsave(&dsi->transfer_lock, flags);
  1018. if (!list_empty(&dsi->transfer_list) &&
  1019. xfer == list_first_entry(&dsi->transfer_list,
  1020. struct exynos_dsi_transfer, list)) {
  1021. list_del_init(&xfer->list);
  1022. start = !list_empty(&dsi->transfer_list);
  1023. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  1024. if (start)
  1025. exynos_dsi_transfer_start(dsi);
  1026. return;
  1027. }
  1028. list_del_init(&xfer->list);
  1029. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  1030. }
  1031. static int exynos_dsi_transfer(struct exynos_dsi *dsi,
  1032. struct exynos_dsi_transfer *xfer)
  1033. {
  1034. unsigned long flags;
  1035. bool stopped;
  1036. xfer->tx_done = 0;
  1037. xfer->rx_done = 0;
  1038. xfer->result = -ETIMEDOUT;
  1039. init_completion(&xfer->completed);
  1040. spin_lock_irqsave(&dsi->transfer_lock, flags);
  1041. stopped = list_empty(&dsi->transfer_list);
  1042. list_add_tail(&xfer->list, &dsi->transfer_list);
  1043. spin_unlock_irqrestore(&dsi->transfer_lock, flags);
  1044. if (stopped)
  1045. exynos_dsi_transfer_start(dsi);
  1046. wait_for_completion_timeout(&xfer->completed,
  1047. msecs_to_jiffies(DSI_XFER_TIMEOUT_MS));
  1048. if (xfer->result == -ETIMEDOUT) {
  1049. struct mipi_dsi_packet *pkt = &xfer->packet;
  1050. exynos_dsi_remove_transfer(dsi, xfer);
  1051. dev_err(dsi->dev, "xfer timed out: %*ph %*ph\n", 4, pkt->header,
  1052. (int)pkt->payload_length, pkt->payload);
  1053. return -ETIMEDOUT;
  1054. }
  1055. /* Also covers hardware timeout condition */
  1056. return xfer->result;
  1057. }
  1058. static irqreturn_t exynos_dsi_irq(int irq, void *dev_id)
  1059. {
  1060. struct exynos_dsi *dsi = dev_id;
  1061. u32 status;
  1062. status = exynos_dsi_read(dsi, DSIM_INTSRC_REG);
  1063. if (!status) {
  1064. static unsigned long int j;
  1065. if (printk_timed_ratelimit(&j, 500))
  1066. dev_warn(dsi->dev, "spurious interrupt\n");
  1067. return IRQ_HANDLED;
  1068. }
  1069. exynos_dsi_write(dsi, DSIM_INTSRC_REG, status);
  1070. if (status & DSIM_INT_SW_RST_RELEASE) {
  1071. u32 mask = ~(DSIM_INT_RX_DONE | DSIM_INT_SFR_FIFO_EMPTY |
  1072. DSIM_INT_SFR_HDR_FIFO_EMPTY | DSIM_INT_FRAME_DONE |
  1073. DSIM_INT_RX_ECC_ERR | DSIM_INT_SW_RST_RELEASE);
  1074. exynos_dsi_write(dsi, DSIM_INTMSK_REG, mask);
  1075. complete(&dsi->completed);
  1076. return IRQ_HANDLED;
  1077. }
  1078. if (!(status & (DSIM_INT_RX_DONE | DSIM_INT_SFR_FIFO_EMPTY |
  1079. DSIM_INT_FRAME_DONE | DSIM_INT_PLL_STABLE)))
  1080. return IRQ_HANDLED;
  1081. if (exynos_dsi_transfer_finish(dsi))
  1082. exynos_dsi_transfer_start(dsi);
  1083. return IRQ_HANDLED;
  1084. }
  1085. static irqreturn_t exynos_dsi_te_irq_handler(int irq, void *dev_id)
  1086. {
  1087. struct exynos_dsi *dsi = (struct exynos_dsi *)dev_id;
  1088. struct drm_encoder *encoder = &dsi->encoder;
  1089. if (dsi->state & DSIM_STATE_VIDOUT_AVAILABLE)
  1090. exynos_drm_crtc_te_handler(encoder->crtc);
  1091. return IRQ_HANDLED;
  1092. }
  1093. static void exynos_dsi_enable_irq(struct exynos_dsi *dsi)
  1094. {
  1095. enable_irq(dsi->irq);
  1096. if (gpio_is_valid(dsi->te_gpio))
  1097. enable_irq(gpio_to_irq(dsi->te_gpio));
  1098. }
  1099. static void exynos_dsi_disable_irq(struct exynos_dsi *dsi)
  1100. {
  1101. if (gpio_is_valid(dsi->te_gpio))
  1102. disable_irq(gpio_to_irq(dsi->te_gpio));
  1103. disable_irq(dsi->irq);
  1104. }
  1105. static int exynos_dsi_init(struct exynos_dsi *dsi)
  1106. {
  1107. const struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
  1108. exynos_dsi_reset(dsi);
  1109. exynos_dsi_enable_irq(dsi);
  1110. if (driver_data->reg_values[RESET_TYPE] == DSIM_FUNCRST)
  1111. exynos_dsi_enable_lane(dsi, BIT(dsi->lanes) - 1);
  1112. exynos_dsi_enable_clock(dsi);
  1113. if (driver_data->wait_for_reset)
  1114. exynos_dsi_wait_for_reset(dsi);
  1115. exynos_dsi_set_phy_ctrl(dsi);
  1116. exynos_dsi_init_link(dsi);
  1117. return 0;
  1118. }
  1119. static int exynos_dsi_register_te_irq(struct exynos_dsi *dsi,
  1120. struct device *panel)
  1121. {
  1122. int ret;
  1123. int te_gpio_irq;
  1124. dsi->te_gpio = of_get_named_gpio(panel->of_node, "te-gpios", 0);
  1125. if (dsi->te_gpio == -ENOENT)
  1126. return 0;
  1127. if (!gpio_is_valid(dsi->te_gpio)) {
  1128. ret = dsi->te_gpio;
  1129. dev_err(dsi->dev, "cannot get te-gpios, %d\n", ret);
  1130. goto out;
  1131. }
  1132. ret = gpio_request(dsi->te_gpio, "te_gpio");
  1133. if (ret) {
  1134. dev_err(dsi->dev, "gpio request failed with %d\n", ret);
  1135. goto out;
  1136. }
  1137. te_gpio_irq = gpio_to_irq(dsi->te_gpio);
  1138. irq_set_status_flags(te_gpio_irq, IRQ_NOAUTOEN);
  1139. ret = request_threaded_irq(te_gpio_irq, exynos_dsi_te_irq_handler, NULL,
  1140. IRQF_TRIGGER_RISING, "TE", dsi);
  1141. if (ret) {
  1142. dev_err(dsi->dev, "request interrupt failed with %d\n", ret);
  1143. gpio_free(dsi->te_gpio);
  1144. goto out;
  1145. }
  1146. out:
  1147. return ret;
  1148. }
  1149. static void exynos_dsi_unregister_te_irq(struct exynos_dsi *dsi)
  1150. {
  1151. if (gpio_is_valid(dsi->te_gpio)) {
  1152. free_irq(gpio_to_irq(dsi->te_gpio), dsi);
  1153. gpio_free(dsi->te_gpio);
  1154. dsi->te_gpio = -ENOENT;
  1155. }
  1156. }
  1157. static void exynos_dsi_enable(struct drm_encoder *encoder)
  1158. {
  1159. struct exynos_dsi *dsi = encoder_to_dsi(encoder);
  1160. int ret;
  1161. if (dsi->state & DSIM_STATE_ENABLED)
  1162. return;
  1163. pm_runtime_get_sync(dsi->dev);
  1164. dsi->state |= DSIM_STATE_ENABLED;
  1165. ret = drm_panel_prepare(dsi->panel);
  1166. if (ret < 0) {
  1167. dsi->state &= ~DSIM_STATE_ENABLED;
  1168. pm_runtime_put_sync(dsi->dev);
  1169. return;
  1170. }
  1171. exynos_dsi_set_display_mode(dsi);
  1172. exynos_dsi_set_display_enable(dsi, true);
  1173. ret = drm_panel_enable(dsi->panel);
  1174. if (ret < 0) {
  1175. dsi->state &= ~DSIM_STATE_ENABLED;
  1176. exynos_dsi_set_display_enable(dsi, false);
  1177. drm_panel_unprepare(dsi->panel);
  1178. pm_runtime_put_sync(dsi->dev);
  1179. return;
  1180. }
  1181. dsi->state |= DSIM_STATE_VIDOUT_AVAILABLE;
  1182. }
  1183. static void exynos_dsi_disable(struct drm_encoder *encoder)
  1184. {
  1185. struct exynos_dsi *dsi = encoder_to_dsi(encoder);
  1186. if (!(dsi->state & DSIM_STATE_ENABLED))
  1187. return;
  1188. dsi->state &= ~DSIM_STATE_VIDOUT_AVAILABLE;
  1189. drm_panel_disable(dsi->panel);
  1190. exynos_dsi_set_display_enable(dsi, false);
  1191. drm_panel_unprepare(dsi->panel);
  1192. dsi->state &= ~DSIM_STATE_ENABLED;
  1193. pm_runtime_put_sync(dsi->dev);
  1194. }
  1195. static enum drm_connector_status
  1196. exynos_dsi_detect(struct drm_connector *connector, bool force)
  1197. {
  1198. return connector->status;
  1199. }
  1200. static void exynos_dsi_connector_destroy(struct drm_connector *connector)
  1201. {
  1202. drm_connector_unregister(connector);
  1203. drm_connector_cleanup(connector);
  1204. connector->dev = NULL;
  1205. }
  1206. static const struct drm_connector_funcs exynos_dsi_connector_funcs = {
  1207. .detect = exynos_dsi_detect,
  1208. .fill_modes = drm_helper_probe_single_connector_modes,
  1209. .destroy = exynos_dsi_connector_destroy,
  1210. .reset = drm_atomic_helper_connector_reset,
  1211. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1212. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1213. };
  1214. static int exynos_dsi_get_modes(struct drm_connector *connector)
  1215. {
  1216. struct exynos_dsi *dsi = connector_to_dsi(connector);
  1217. if (dsi->panel)
  1218. return dsi->panel->funcs->get_modes(dsi->panel);
  1219. return 0;
  1220. }
  1221. static const struct drm_connector_helper_funcs exynos_dsi_connector_helper_funcs = {
  1222. .get_modes = exynos_dsi_get_modes,
  1223. };
  1224. static int exynos_dsi_create_connector(struct drm_encoder *encoder)
  1225. {
  1226. struct exynos_dsi *dsi = encoder_to_dsi(encoder);
  1227. struct drm_connector *connector = &dsi->connector;
  1228. int ret;
  1229. connector->polled = DRM_CONNECTOR_POLL_HPD;
  1230. ret = drm_connector_init(encoder->dev, connector,
  1231. &exynos_dsi_connector_funcs,
  1232. DRM_MODE_CONNECTOR_DSI);
  1233. if (ret) {
  1234. DRM_ERROR("Failed to initialize connector with drm\n");
  1235. return ret;
  1236. }
  1237. connector->status = connector_status_disconnected;
  1238. drm_connector_helper_add(connector, &exynos_dsi_connector_helper_funcs);
  1239. drm_mode_connector_attach_encoder(connector, encoder);
  1240. return 0;
  1241. }
  1242. static void exynos_dsi_mode_set(struct drm_encoder *encoder,
  1243. struct drm_display_mode *mode,
  1244. struct drm_display_mode *adjusted_mode)
  1245. {
  1246. struct exynos_dsi *dsi = encoder_to_dsi(encoder);
  1247. struct videomode *vm = &dsi->vm;
  1248. struct drm_display_mode *m = adjusted_mode;
  1249. vm->hactive = m->hdisplay;
  1250. vm->vactive = m->vdisplay;
  1251. vm->vfront_porch = m->vsync_start - m->vdisplay;
  1252. vm->vback_porch = m->vtotal - m->vsync_end;
  1253. vm->vsync_len = m->vsync_end - m->vsync_start;
  1254. vm->hfront_porch = m->hsync_start - m->hdisplay;
  1255. vm->hback_porch = m->htotal - m->hsync_end;
  1256. vm->hsync_len = m->hsync_end - m->hsync_start;
  1257. }
  1258. static const struct drm_encoder_helper_funcs exynos_dsi_encoder_helper_funcs = {
  1259. .mode_set = exynos_dsi_mode_set,
  1260. .enable = exynos_dsi_enable,
  1261. .disable = exynos_dsi_disable,
  1262. };
  1263. static const struct drm_encoder_funcs exynos_dsi_encoder_funcs = {
  1264. .destroy = drm_encoder_cleanup,
  1265. };
  1266. MODULE_DEVICE_TABLE(of, exynos_dsi_of_match);
  1267. static int exynos_dsi_host_attach(struct mipi_dsi_host *host,
  1268. struct mipi_dsi_device *device)
  1269. {
  1270. struct exynos_dsi *dsi = host_to_dsi(host);
  1271. struct drm_device *drm = dsi->connector.dev;
  1272. /*
  1273. * This is a temporary solution and should be made by more generic way.
  1274. *
  1275. * If attached panel device is for command mode one, dsi should register
  1276. * TE interrupt handler.
  1277. */
  1278. if (!(device->mode_flags & MIPI_DSI_MODE_VIDEO)) {
  1279. int ret = exynos_dsi_register_te_irq(dsi, &device->dev);
  1280. if (ret)
  1281. return ret;
  1282. }
  1283. mutex_lock(&drm->mode_config.mutex);
  1284. dsi->lanes = device->lanes;
  1285. dsi->format = device->format;
  1286. dsi->mode_flags = device->mode_flags;
  1287. dsi->panel = of_drm_find_panel(device->dev.of_node);
  1288. if (dsi->panel) {
  1289. drm_panel_attach(dsi->panel, &dsi->connector);
  1290. dsi->connector.status = connector_status_connected;
  1291. }
  1292. exynos_drm_crtc_get_by_type(drm, EXYNOS_DISPLAY_TYPE_LCD)->i80_mode =
  1293. !(dsi->mode_flags & MIPI_DSI_MODE_VIDEO);
  1294. mutex_unlock(&drm->mode_config.mutex);
  1295. if (drm->mode_config.poll_enabled)
  1296. drm_kms_helper_hotplug_event(drm);
  1297. return 0;
  1298. }
  1299. static int exynos_dsi_host_detach(struct mipi_dsi_host *host,
  1300. struct mipi_dsi_device *device)
  1301. {
  1302. struct exynos_dsi *dsi = host_to_dsi(host);
  1303. struct drm_device *drm = dsi->connector.dev;
  1304. mutex_lock(&drm->mode_config.mutex);
  1305. if (dsi->panel) {
  1306. exynos_dsi_disable(&dsi->encoder);
  1307. drm_panel_detach(dsi->panel);
  1308. dsi->panel = NULL;
  1309. dsi->connector.status = connector_status_disconnected;
  1310. }
  1311. mutex_unlock(&drm->mode_config.mutex);
  1312. if (drm->mode_config.poll_enabled)
  1313. drm_kms_helper_hotplug_event(drm);
  1314. exynos_dsi_unregister_te_irq(dsi);
  1315. return 0;
  1316. }
  1317. static ssize_t exynos_dsi_host_transfer(struct mipi_dsi_host *host,
  1318. const struct mipi_dsi_msg *msg)
  1319. {
  1320. struct exynos_dsi *dsi = host_to_dsi(host);
  1321. struct exynos_dsi_transfer xfer;
  1322. int ret;
  1323. if (!(dsi->state & DSIM_STATE_ENABLED))
  1324. return -EINVAL;
  1325. if (!(dsi->state & DSIM_STATE_INITIALIZED)) {
  1326. ret = exynos_dsi_init(dsi);
  1327. if (ret)
  1328. return ret;
  1329. dsi->state |= DSIM_STATE_INITIALIZED;
  1330. }
  1331. ret = mipi_dsi_create_packet(&xfer.packet, msg);
  1332. if (ret < 0)
  1333. return ret;
  1334. xfer.rx_len = msg->rx_len;
  1335. xfer.rx_payload = msg->rx_buf;
  1336. xfer.flags = msg->flags;
  1337. ret = exynos_dsi_transfer(dsi, &xfer);
  1338. return (ret < 0) ? ret : xfer.rx_done;
  1339. }
  1340. static const struct mipi_dsi_host_ops exynos_dsi_ops = {
  1341. .attach = exynos_dsi_host_attach,
  1342. .detach = exynos_dsi_host_detach,
  1343. .transfer = exynos_dsi_host_transfer,
  1344. };
  1345. static int exynos_dsi_of_read_u32(const struct device_node *np,
  1346. const char *propname, u32 *out_value)
  1347. {
  1348. int ret = of_property_read_u32(np, propname, out_value);
  1349. if (ret < 0)
  1350. pr_err("%pOF: failed to get '%s' property\n", np, propname);
  1351. return ret;
  1352. }
  1353. enum {
  1354. DSI_PORT_IN,
  1355. DSI_PORT_OUT
  1356. };
  1357. static int exynos_dsi_parse_dt(struct exynos_dsi *dsi)
  1358. {
  1359. struct device *dev = dsi->dev;
  1360. struct device_node *node = dev->of_node;
  1361. int ret;
  1362. ret = exynos_dsi_of_read_u32(node, "samsung,pll-clock-frequency",
  1363. &dsi->pll_clk_rate);
  1364. if (ret < 0)
  1365. return ret;
  1366. ret = exynos_dsi_of_read_u32(node, "samsung,burst-clock-frequency",
  1367. &dsi->burst_clk_rate);
  1368. if (ret < 0)
  1369. return ret;
  1370. ret = exynos_dsi_of_read_u32(node, "samsung,esc-clock-frequency",
  1371. &dsi->esc_clk_rate);
  1372. if (ret < 0)
  1373. return ret;
  1374. dsi->bridge_node = of_graph_get_remote_node(node, DSI_PORT_IN, 0);
  1375. return 0;
  1376. }
  1377. static int exynos_dsi_bind(struct device *dev, struct device *master,
  1378. void *data)
  1379. {
  1380. struct drm_encoder *encoder = dev_get_drvdata(dev);
  1381. struct exynos_dsi *dsi = encoder_to_dsi(encoder);
  1382. struct drm_device *drm_dev = data;
  1383. struct drm_bridge *bridge;
  1384. int ret;
  1385. drm_encoder_init(drm_dev, encoder, &exynos_dsi_encoder_funcs,
  1386. DRM_MODE_ENCODER_TMDS, NULL);
  1387. drm_encoder_helper_add(encoder, &exynos_dsi_encoder_helper_funcs);
  1388. ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
  1389. if (ret < 0)
  1390. return ret;
  1391. ret = exynos_dsi_create_connector(encoder);
  1392. if (ret) {
  1393. DRM_ERROR("failed to create connector ret = %d\n", ret);
  1394. drm_encoder_cleanup(encoder);
  1395. return ret;
  1396. }
  1397. if (dsi->bridge_node) {
  1398. bridge = of_drm_find_bridge(dsi->bridge_node);
  1399. if (bridge)
  1400. drm_bridge_attach(encoder, bridge, NULL);
  1401. }
  1402. return mipi_dsi_host_register(&dsi->dsi_host);
  1403. }
  1404. static void exynos_dsi_unbind(struct device *dev, struct device *master,
  1405. void *data)
  1406. {
  1407. struct drm_encoder *encoder = dev_get_drvdata(dev);
  1408. struct exynos_dsi *dsi = encoder_to_dsi(encoder);
  1409. exynos_dsi_disable(encoder);
  1410. mipi_dsi_host_unregister(&dsi->dsi_host);
  1411. }
  1412. static const struct component_ops exynos_dsi_component_ops = {
  1413. .bind = exynos_dsi_bind,
  1414. .unbind = exynos_dsi_unbind,
  1415. };
  1416. static int exynos_dsi_probe(struct platform_device *pdev)
  1417. {
  1418. struct device *dev = &pdev->dev;
  1419. struct resource *res;
  1420. struct exynos_dsi *dsi;
  1421. int ret, i;
  1422. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  1423. if (!dsi)
  1424. return -ENOMEM;
  1425. /* To be checked as invalid one */
  1426. dsi->te_gpio = -ENOENT;
  1427. init_completion(&dsi->completed);
  1428. spin_lock_init(&dsi->transfer_lock);
  1429. INIT_LIST_HEAD(&dsi->transfer_list);
  1430. dsi->dsi_host.ops = &exynos_dsi_ops;
  1431. dsi->dsi_host.dev = dev;
  1432. dsi->dev = dev;
  1433. dsi->driver_data = of_device_get_match_data(dev);
  1434. ret = exynos_dsi_parse_dt(dsi);
  1435. if (ret)
  1436. return ret;
  1437. dsi->supplies[0].supply = "vddcore";
  1438. dsi->supplies[1].supply = "vddio";
  1439. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(dsi->supplies),
  1440. dsi->supplies);
  1441. if (ret) {
  1442. if (ret != -EPROBE_DEFER)
  1443. dev_info(dev, "failed to get regulators: %d\n", ret);
  1444. return ret;
  1445. }
  1446. dsi->clks = devm_kzalloc(dev,
  1447. sizeof(*dsi->clks) * dsi->driver_data->num_clks,
  1448. GFP_KERNEL);
  1449. if (!dsi->clks)
  1450. return -ENOMEM;
  1451. for (i = 0; i < dsi->driver_data->num_clks; i++) {
  1452. dsi->clks[i] = devm_clk_get(dev, clk_names[i]);
  1453. if (IS_ERR(dsi->clks[i])) {
  1454. if (strcmp(clk_names[i], "sclk_mipi") == 0) {
  1455. dsi->clks[i] = devm_clk_get(dev,
  1456. OLD_SCLK_MIPI_CLK_NAME);
  1457. if (!IS_ERR(dsi->clks[i]))
  1458. continue;
  1459. }
  1460. dev_info(dev, "failed to get the clock: %s\n",
  1461. clk_names[i]);
  1462. return PTR_ERR(dsi->clks[i]);
  1463. }
  1464. }
  1465. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1466. dsi->reg_base = devm_ioremap_resource(dev, res);
  1467. if (IS_ERR(dsi->reg_base)) {
  1468. dev_err(dev, "failed to remap io region\n");
  1469. return PTR_ERR(dsi->reg_base);
  1470. }
  1471. dsi->phy = devm_phy_get(dev, "dsim");
  1472. if (IS_ERR(dsi->phy)) {
  1473. dev_info(dev, "failed to get dsim phy\n");
  1474. return PTR_ERR(dsi->phy);
  1475. }
  1476. dsi->irq = platform_get_irq(pdev, 0);
  1477. if (dsi->irq < 0) {
  1478. dev_err(dev, "failed to request dsi irq resource\n");
  1479. return dsi->irq;
  1480. }
  1481. irq_set_status_flags(dsi->irq, IRQ_NOAUTOEN);
  1482. ret = devm_request_threaded_irq(dev, dsi->irq, NULL,
  1483. exynos_dsi_irq, IRQF_ONESHOT,
  1484. dev_name(dev), dsi);
  1485. if (ret) {
  1486. dev_err(dev, "failed to request dsi irq\n");
  1487. return ret;
  1488. }
  1489. platform_set_drvdata(pdev, &dsi->encoder);
  1490. pm_runtime_enable(dev);
  1491. return component_add(dev, &exynos_dsi_component_ops);
  1492. }
  1493. static int exynos_dsi_remove(struct platform_device *pdev)
  1494. {
  1495. struct exynos_dsi *dsi = platform_get_drvdata(pdev);
  1496. of_node_put(dsi->bridge_node);
  1497. pm_runtime_disable(&pdev->dev);
  1498. component_del(&pdev->dev, &exynos_dsi_component_ops);
  1499. return 0;
  1500. }
  1501. static int __maybe_unused exynos_dsi_suspend(struct device *dev)
  1502. {
  1503. struct drm_encoder *encoder = dev_get_drvdata(dev);
  1504. struct exynos_dsi *dsi = encoder_to_dsi(encoder);
  1505. const struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
  1506. int ret, i;
  1507. usleep_range(10000, 20000);
  1508. if (dsi->state & DSIM_STATE_INITIALIZED) {
  1509. dsi->state &= ~DSIM_STATE_INITIALIZED;
  1510. exynos_dsi_disable_clock(dsi);
  1511. exynos_dsi_disable_irq(dsi);
  1512. }
  1513. dsi->state &= ~DSIM_STATE_CMD_LPM;
  1514. phy_power_off(dsi->phy);
  1515. for (i = driver_data->num_clks - 1; i > -1; i--)
  1516. clk_disable_unprepare(dsi->clks[i]);
  1517. ret = regulator_bulk_disable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
  1518. if (ret < 0)
  1519. dev_err(dsi->dev, "cannot disable regulators %d\n", ret);
  1520. return 0;
  1521. }
  1522. static int __maybe_unused exynos_dsi_resume(struct device *dev)
  1523. {
  1524. struct drm_encoder *encoder = dev_get_drvdata(dev);
  1525. struct exynos_dsi *dsi = encoder_to_dsi(encoder);
  1526. const struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
  1527. int ret, i;
  1528. ret = regulator_bulk_enable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
  1529. if (ret < 0) {
  1530. dev_err(dsi->dev, "cannot enable regulators %d\n", ret);
  1531. return ret;
  1532. }
  1533. for (i = 0; i < driver_data->num_clks; i++) {
  1534. ret = clk_prepare_enable(dsi->clks[i]);
  1535. if (ret < 0)
  1536. goto err_clk;
  1537. }
  1538. ret = phy_power_on(dsi->phy);
  1539. if (ret < 0) {
  1540. dev_err(dsi->dev, "cannot enable phy %d\n", ret);
  1541. goto err_clk;
  1542. }
  1543. return 0;
  1544. err_clk:
  1545. while (--i > -1)
  1546. clk_disable_unprepare(dsi->clks[i]);
  1547. regulator_bulk_disable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
  1548. return ret;
  1549. }
  1550. static const struct dev_pm_ops exynos_dsi_pm_ops = {
  1551. SET_RUNTIME_PM_OPS(exynos_dsi_suspend, exynos_dsi_resume, NULL)
  1552. };
  1553. struct platform_driver dsi_driver = {
  1554. .probe = exynos_dsi_probe,
  1555. .remove = exynos_dsi_remove,
  1556. .driver = {
  1557. .name = "exynos-dsi",
  1558. .owner = THIS_MODULE,
  1559. .pm = &exynos_dsi_pm_ops,
  1560. .of_match_table = exynos_dsi_of_match,
  1561. },
  1562. };
  1563. MODULE_AUTHOR("Tomasz Figa <t.figa@samsung.com>");
  1564. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  1565. MODULE_DESCRIPTION("Samsung SoC MIPI DSI Master");
  1566. MODULE_LICENSE("GPL v2");