exynos_drm_dsi.c 50 KB

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