sun6i_mipi_dsi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2016 Allwinnertech Co., Ltd.
  4. * Copyright (C) 2017-2018 Bootlin
  5. *
  6. * Maxime Ripard <maxime.ripard@bootlin.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/component.h>
  10. #include <linux/crc-ccitt.h>
  11. #include <linux/of_address.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/regmap.h>
  14. #include <linux/reset.h>
  15. #include <linux/phy/phy.h>
  16. #include <drm/drmP.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_mipi_dsi.h>
  20. #include <drm/drm_panel.h>
  21. #include "sun4i_drv.h"
  22. #include "sun6i_mipi_dsi.h"
  23. #include <video/mipi_display.h>
  24. #define SUN6I_DSI_CTL_REG 0x000
  25. #define SUN6I_DSI_CTL_EN BIT(0)
  26. #define SUN6I_DSI_BASIC_CTL_REG 0x00c
  27. #define SUN6I_DSI_BASIC_CTL_HBP_DIS BIT(2)
  28. #define SUN6I_DSI_BASIC_CTL_HSA_HSE_DIS BIT(1)
  29. #define SUN6I_DSI_BASIC_CTL_VIDEO_BURST BIT(0)
  30. #define SUN6I_DSI_BASIC_CTL0_REG 0x010
  31. #define SUN6I_DSI_BASIC_CTL0_HS_EOTP_EN BIT(18)
  32. #define SUN6I_DSI_BASIC_CTL0_CRC_EN BIT(17)
  33. #define SUN6I_DSI_BASIC_CTL0_ECC_EN BIT(16)
  34. #define SUN6I_DSI_BASIC_CTL0_INST_ST BIT(0)
  35. #define SUN6I_DSI_BASIC_CTL1_REG 0x014
  36. #define SUN6I_DSI_BASIC_CTL1_VIDEO_ST_DELAY(n) (((n) & 0x1fff) << 4)
  37. #define SUN6I_DSI_BASIC_CTL1_VIDEO_FILL BIT(2)
  38. #define SUN6I_DSI_BASIC_CTL1_VIDEO_PRECISION BIT(1)
  39. #define SUN6I_DSI_BASIC_CTL1_VIDEO_MODE BIT(0)
  40. #define SUN6I_DSI_BASIC_SIZE0_REG 0x018
  41. #define SUN6I_DSI_BASIC_SIZE0_VBP(n) (((n) & 0xfff) << 16)
  42. #define SUN6I_DSI_BASIC_SIZE0_VSA(n) ((n) & 0xfff)
  43. #define SUN6I_DSI_BASIC_SIZE1_REG 0x01c
  44. #define SUN6I_DSI_BASIC_SIZE1_VT(n) (((n) & 0xfff) << 16)
  45. #define SUN6I_DSI_BASIC_SIZE1_VACT(n) ((n) & 0xfff)
  46. #define SUN6I_DSI_INST_FUNC_REG(n) (0x020 + (n) * 0x04)
  47. #define SUN6I_DSI_INST_FUNC_INST_MODE(n) (((n) & 0xf) << 28)
  48. #define SUN6I_DSI_INST_FUNC_ESCAPE_ENTRY(n) (((n) & 0xf) << 24)
  49. #define SUN6I_DSI_INST_FUNC_TRANS_PACKET(n) (((n) & 0xf) << 20)
  50. #define SUN6I_DSI_INST_FUNC_LANE_CEN BIT(4)
  51. #define SUN6I_DSI_INST_FUNC_LANE_DEN(n) ((n) & 0xf)
  52. #define SUN6I_DSI_INST_LOOP_SEL_REG 0x040
  53. #define SUN6I_DSI_INST_LOOP_NUM_REG(n) (0x044 + (n) * 0x10)
  54. #define SUN6I_DSI_INST_LOOP_NUM_N1(n) (((n) & 0xfff) << 16)
  55. #define SUN6I_DSI_INST_LOOP_NUM_N0(n) ((n) & 0xfff)
  56. #define SUN6I_DSI_INST_JUMP_SEL_REG 0x048
  57. #define SUN6I_DSI_INST_JUMP_CFG_REG(n) (0x04c + (n) * 0x04)
  58. #define SUN6I_DSI_INST_JUMP_CFG_TO(n) (((n) & 0xf) << 20)
  59. #define SUN6I_DSI_INST_JUMP_CFG_POINT(n) (((n) & 0xf) << 16)
  60. #define SUN6I_DSI_INST_JUMP_CFG_NUM(n) ((n) & 0xffff)
  61. #define SUN6I_DSI_TRANS_START_REG 0x060
  62. #define SUN6I_DSI_TRANS_ZERO_REG 0x078
  63. #define SUN6I_DSI_TCON_DRQ_REG 0x07c
  64. #define SUN6I_DSI_TCON_DRQ_ENABLE_MODE BIT(28)
  65. #define SUN6I_DSI_TCON_DRQ_SET(n) ((n) & 0x3ff)
  66. #define SUN6I_DSI_PIXEL_CTL0_REG 0x080
  67. #define SUN6I_DSI_PIXEL_CTL0_PD_PLUG_DISABLE BIT(16)
  68. #define SUN6I_DSI_PIXEL_CTL0_FORMAT(n) ((n) & 0xf)
  69. #define SUN6I_DSI_PIXEL_CTL1_REG 0x084
  70. #define SUN6I_DSI_PIXEL_PH_REG 0x090
  71. #define SUN6I_DSI_PIXEL_PH_ECC(n) (((n) & 0xff) << 24)
  72. #define SUN6I_DSI_PIXEL_PH_WC(n) (((n) & 0xffff) << 8)
  73. #define SUN6I_DSI_PIXEL_PH_VC(n) (((n) & 3) << 6)
  74. #define SUN6I_DSI_PIXEL_PH_DT(n) ((n) & 0x3f)
  75. #define SUN6I_DSI_PIXEL_PF0_REG 0x098
  76. #define SUN6I_DSI_PIXEL_PF0_CRC_FORCE(n) ((n) & 0xffff)
  77. #define SUN6I_DSI_PIXEL_PF1_REG 0x09c
  78. #define SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINEN(n) (((n) & 0xffff) << 16)
  79. #define SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINE0(n) ((n) & 0xffff)
  80. #define SUN6I_DSI_SYNC_HSS_REG 0x0b0
  81. #define SUN6I_DSI_SYNC_HSE_REG 0x0b4
  82. #define SUN6I_DSI_SYNC_VSS_REG 0x0b8
  83. #define SUN6I_DSI_SYNC_VSE_REG 0x0bc
  84. #define SUN6I_DSI_BLK_HSA0_REG 0x0c0
  85. #define SUN6I_DSI_BLK_HSA1_REG 0x0c4
  86. #define SUN6I_DSI_BLK_PF(n) (((n) & 0xffff) << 16)
  87. #define SUN6I_DSI_BLK_PD(n) ((n) & 0xff)
  88. #define SUN6I_DSI_BLK_HBP0_REG 0x0c8
  89. #define SUN6I_DSI_BLK_HBP1_REG 0x0cc
  90. #define SUN6I_DSI_BLK_HFP0_REG 0x0d0
  91. #define SUN6I_DSI_BLK_HFP1_REG 0x0d4
  92. #define SUN6I_DSI_BLK_HBLK0_REG 0x0e0
  93. #define SUN6I_DSI_BLK_HBLK1_REG 0x0e4
  94. #define SUN6I_DSI_BLK_VBLK0_REG 0x0e8
  95. #define SUN6I_DSI_BLK_VBLK1_REG 0x0ec
  96. #define SUN6I_DSI_BURST_LINE_REG 0x0f0
  97. #define SUN6I_DSI_BURST_LINE_SYNC_POINT(n) (((n) & 0xffff) << 16)
  98. #define SUN6I_DSI_BURST_LINE_NUM(n) ((n) & 0xffff)
  99. #define SUN6I_DSI_BURST_DRQ_REG 0x0f4
  100. #define SUN6I_DSI_BURST_DRQ_EDGE1(n) (((n) & 0xffff) << 16)
  101. #define SUN6I_DSI_BURST_DRQ_EDGE0(n) ((n) & 0xffff)
  102. #define SUN6I_DSI_CMD_CTL_REG 0x200
  103. #define SUN6I_DSI_CMD_CTL_RX_OVERFLOW BIT(26)
  104. #define SUN6I_DSI_CMD_CTL_RX_FLAG BIT(25)
  105. #define SUN6I_DSI_CMD_CTL_TX_FLAG BIT(9)
  106. #define SUN6I_DSI_CMD_RX_REG(n) (0x240 + (n) * 0x04)
  107. #define SUN6I_DSI_DEBUG_DATA_REG 0x2f8
  108. #define SUN6I_DSI_CMD_TX_REG(n) (0x300 + (n) * 0x04)
  109. enum sun6i_dsi_start_inst {
  110. DSI_START_LPRX,
  111. DSI_START_LPTX,
  112. DSI_START_HSC,
  113. DSI_START_HSD,
  114. };
  115. enum sun6i_dsi_inst_id {
  116. DSI_INST_ID_LP11 = 0,
  117. DSI_INST_ID_TBA,
  118. DSI_INST_ID_HSC,
  119. DSI_INST_ID_HSD,
  120. DSI_INST_ID_LPDT,
  121. DSI_INST_ID_HSCEXIT,
  122. DSI_INST_ID_NOP,
  123. DSI_INST_ID_DLY,
  124. DSI_INST_ID_END = 15,
  125. };
  126. enum sun6i_dsi_inst_mode {
  127. DSI_INST_MODE_STOP = 0,
  128. DSI_INST_MODE_TBA,
  129. DSI_INST_MODE_HS,
  130. DSI_INST_MODE_ESCAPE,
  131. DSI_INST_MODE_HSCEXIT,
  132. DSI_INST_MODE_NOP,
  133. };
  134. enum sun6i_dsi_inst_escape {
  135. DSI_INST_ESCA_LPDT = 0,
  136. DSI_INST_ESCA_ULPS,
  137. DSI_INST_ESCA_UN1,
  138. DSI_INST_ESCA_UN2,
  139. DSI_INST_ESCA_RESET,
  140. DSI_INST_ESCA_UN3,
  141. DSI_INST_ESCA_UN4,
  142. DSI_INST_ESCA_UN5,
  143. };
  144. enum sun6i_dsi_inst_packet {
  145. DSI_INST_PACK_PIXEL = 0,
  146. DSI_INST_PACK_COMMAND,
  147. };
  148. static const u32 sun6i_dsi_ecc_array[] = {
  149. [0] = (BIT(0) | BIT(1) | BIT(2) | BIT(4) | BIT(5) | BIT(7) | BIT(10) |
  150. BIT(11) | BIT(13) | BIT(16) | BIT(20) | BIT(21) | BIT(22) |
  151. BIT(23)),
  152. [1] = (BIT(0) | BIT(1) | BIT(3) | BIT(4) | BIT(6) | BIT(8) | BIT(10) |
  153. BIT(12) | BIT(14) | BIT(17) | BIT(20) | BIT(21) | BIT(22) |
  154. BIT(23)),
  155. [2] = (BIT(0) | BIT(2) | BIT(3) | BIT(5) | BIT(6) | BIT(9) | BIT(11) |
  156. BIT(12) | BIT(15) | BIT(18) | BIT(20) | BIT(21) | BIT(22)),
  157. [3] = (BIT(1) | BIT(2) | BIT(3) | BIT(7) | BIT(8) | BIT(9) | BIT(13) |
  158. BIT(14) | BIT(15) | BIT(19) | BIT(20) | BIT(21) | BIT(23)),
  159. [4] = (BIT(4) | BIT(5) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | BIT(16) |
  160. BIT(17) | BIT(18) | BIT(19) | BIT(20) | BIT(22) | BIT(23)),
  161. [5] = (BIT(10) | BIT(11) | BIT(12) | BIT(13) | BIT(14) | BIT(15) |
  162. BIT(16) | BIT(17) | BIT(18) | BIT(19) | BIT(21) | BIT(22) |
  163. BIT(23)),
  164. };
  165. static u32 sun6i_dsi_ecc_compute(unsigned int data)
  166. {
  167. int i;
  168. u8 ecc = 0;
  169. for (i = 0; i < ARRAY_SIZE(sun6i_dsi_ecc_array); i++) {
  170. u32 field = sun6i_dsi_ecc_array[i];
  171. bool init = false;
  172. u8 val = 0;
  173. int j;
  174. for (j = 0; j < 24; j++) {
  175. if (!(BIT(j) & field))
  176. continue;
  177. if (!init) {
  178. val = (BIT(j) & data) ? 1 : 0;
  179. init = true;
  180. } else {
  181. val ^= (BIT(j) & data) ? 1 : 0;
  182. }
  183. }
  184. ecc |= val << i;
  185. }
  186. return ecc;
  187. }
  188. static u16 sun6i_dsi_crc_compute(u8 const *buffer, size_t len)
  189. {
  190. return crc_ccitt(0xffff, buffer, len);
  191. }
  192. static u16 sun6i_dsi_crc_repeat_compute(u8 pd, size_t len)
  193. {
  194. u8 buffer[len];
  195. memset(buffer, pd, len);
  196. return sun6i_dsi_crc_compute(buffer, len);
  197. }
  198. static u32 sun6i_dsi_build_sync_pkt(u8 dt, u8 vc, u8 d0, u8 d1)
  199. {
  200. u32 val = dt & 0x3f;
  201. val |= (vc & 3) << 6;
  202. val |= (d0 & 0xff) << 8;
  203. val |= (d1 & 0xff) << 16;
  204. val |= sun6i_dsi_ecc_compute(val) << 24;
  205. return val;
  206. }
  207. static u32 sun6i_dsi_build_blk0_pkt(u8 vc, u16 wc)
  208. {
  209. return sun6i_dsi_build_sync_pkt(MIPI_DSI_BLANKING_PACKET, vc,
  210. wc & 0xff, wc >> 8);
  211. }
  212. static u32 sun6i_dsi_build_blk1_pkt(u16 pd, size_t len)
  213. {
  214. u32 val = SUN6I_DSI_BLK_PD(pd);
  215. return val | SUN6I_DSI_BLK_PF(sun6i_dsi_crc_repeat_compute(pd, len));
  216. }
  217. static void sun6i_dsi_inst_abort(struct sun6i_dsi *dsi)
  218. {
  219. regmap_update_bits(dsi->regs, SUN6I_DSI_BASIC_CTL0_REG,
  220. SUN6I_DSI_BASIC_CTL0_INST_ST, 0);
  221. }
  222. static void sun6i_dsi_inst_commit(struct sun6i_dsi *dsi)
  223. {
  224. regmap_update_bits(dsi->regs, SUN6I_DSI_BASIC_CTL0_REG,
  225. SUN6I_DSI_BASIC_CTL0_INST_ST,
  226. SUN6I_DSI_BASIC_CTL0_INST_ST);
  227. }
  228. static int sun6i_dsi_inst_wait_for_completion(struct sun6i_dsi *dsi)
  229. {
  230. u32 val;
  231. return regmap_read_poll_timeout(dsi->regs, SUN6I_DSI_BASIC_CTL0_REG,
  232. val,
  233. !(val & SUN6I_DSI_BASIC_CTL0_INST_ST),
  234. 100, 5000);
  235. }
  236. static void sun6i_dsi_inst_setup(struct sun6i_dsi *dsi,
  237. enum sun6i_dsi_inst_id id,
  238. enum sun6i_dsi_inst_mode mode,
  239. bool clock, u8 data,
  240. enum sun6i_dsi_inst_packet packet,
  241. enum sun6i_dsi_inst_escape escape)
  242. {
  243. regmap_write(dsi->regs, SUN6I_DSI_INST_FUNC_REG(id),
  244. SUN6I_DSI_INST_FUNC_INST_MODE(mode) |
  245. SUN6I_DSI_INST_FUNC_ESCAPE_ENTRY(escape) |
  246. SUN6I_DSI_INST_FUNC_TRANS_PACKET(packet) |
  247. (clock ? SUN6I_DSI_INST_FUNC_LANE_CEN : 0) |
  248. SUN6I_DSI_INST_FUNC_LANE_DEN(data));
  249. }
  250. static void sun6i_dsi_inst_init(struct sun6i_dsi *dsi,
  251. struct mipi_dsi_device *device)
  252. {
  253. u8 lanes_mask = GENMASK(device->lanes - 1, 0);
  254. sun6i_dsi_inst_setup(dsi, DSI_INST_ID_LP11, DSI_INST_MODE_STOP,
  255. true, lanes_mask, 0, 0);
  256. sun6i_dsi_inst_setup(dsi, DSI_INST_ID_TBA, DSI_INST_MODE_TBA,
  257. false, 1, 0, 0);
  258. sun6i_dsi_inst_setup(dsi, DSI_INST_ID_HSC, DSI_INST_MODE_HS,
  259. true, 0, DSI_INST_PACK_PIXEL, 0);
  260. sun6i_dsi_inst_setup(dsi, DSI_INST_ID_HSD, DSI_INST_MODE_HS,
  261. false, lanes_mask, DSI_INST_PACK_PIXEL, 0);
  262. sun6i_dsi_inst_setup(dsi, DSI_INST_ID_LPDT, DSI_INST_MODE_ESCAPE,
  263. false, 1, DSI_INST_PACK_COMMAND,
  264. DSI_INST_ESCA_LPDT);
  265. sun6i_dsi_inst_setup(dsi, DSI_INST_ID_HSCEXIT, DSI_INST_MODE_HSCEXIT,
  266. true, 0, 0, 0);
  267. sun6i_dsi_inst_setup(dsi, DSI_INST_ID_NOP, DSI_INST_MODE_STOP,
  268. false, lanes_mask, 0, 0);
  269. sun6i_dsi_inst_setup(dsi, DSI_INST_ID_DLY, DSI_INST_MODE_NOP,
  270. true, lanes_mask, 0, 0);
  271. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_CFG_REG(0),
  272. SUN6I_DSI_INST_JUMP_CFG_POINT(DSI_INST_ID_NOP) |
  273. SUN6I_DSI_INST_JUMP_CFG_TO(DSI_INST_ID_HSCEXIT) |
  274. SUN6I_DSI_INST_JUMP_CFG_NUM(1));
  275. };
  276. static u16 sun6i_dsi_get_video_start_delay(struct sun6i_dsi *dsi,
  277. struct drm_display_mode *mode)
  278. {
  279. return mode->vtotal - (mode->vsync_end - mode->vdisplay) + 1;
  280. }
  281. static void sun6i_dsi_setup_burst(struct sun6i_dsi *dsi,
  282. struct drm_display_mode *mode)
  283. {
  284. struct mipi_dsi_device *device = dsi->device;
  285. u32 val = 0;
  286. if ((mode->hsync_end - mode->hdisplay) > 20) {
  287. /* Maaaaaagic */
  288. u16 drq = (mode->hsync_end - mode->hdisplay) - 20;
  289. drq *= mipi_dsi_pixel_format_to_bpp(device->format);
  290. drq /= 32;
  291. val = (SUN6I_DSI_TCON_DRQ_ENABLE_MODE |
  292. SUN6I_DSI_TCON_DRQ_SET(drq));
  293. }
  294. regmap_write(dsi->regs, SUN6I_DSI_TCON_DRQ_REG, val);
  295. }
  296. static void sun6i_dsi_setup_inst_loop(struct sun6i_dsi *dsi,
  297. struct drm_display_mode *mode)
  298. {
  299. u16 delay = 50 - 1;
  300. regmap_write(dsi->regs, SUN6I_DSI_INST_LOOP_NUM_REG(0),
  301. SUN6I_DSI_INST_LOOP_NUM_N0(50 - 1) |
  302. SUN6I_DSI_INST_LOOP_NUM_N1(delay));
  303. regmap_write(dsi->regs, SUN6I_DSI_INST_LOOP_NUM_REG(1),
  304. SUN6I_DSI_INST_LOOP_NUM_N0(50 - 1) |
  305. SUN6I_DSI_INST_LOOP_NUM_N1(delay));
  306. }
  307. static void sun6i_dsi_setup_format(struct sun6i_dsi *dsi,
  308. struct drm_display_mode *mode)
  309. {
  310. struct mipi_dsi_device *device = dsi->device;
  311. u32 val = SUN6I_DSI_PIXEL_PH_VC(device->channel);
  312. u8 dt, fmt;
  313. u16 wc;
  314. /*
  315. * TODO: The format defines are only valid in video mode and
  316. * change in command mode.
  317. */
  318. switch (device->format) {
  319. case MIPI_DSI_FMT_RGB888:
  320. dt = MIPI_DSI_PACKED_PIXEL_STREAM_24;
  321. fmt = 8;
  322. break;
  323. case MIPI_DSI_FMT_RGB666:
  324. dt = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
  325. fmt = 9;
  326. break;
  327. case MIPI_DSI_FMT_RGB666_PACKED:
  328. dt = MIPI_DSI_PACKED_PIXEL_STREAM_18;
  329. fmt = 10;
  330. break;
  331. case MIPI_DSI_FMT_RGB565:
  332. dt = MIPI_DSI_PACKED_PIXEL_STREAM_16;
  333. fmt = 11;
  334. break;
  335. default:
  336. return;
  337. }
  338. val |= SUN6I_DSI_PIXEL_PH_DT(dt);
  339. wc = mode->hdisplay * mipi_dsi_pixel_format_to_bpp(device->format) / 8;
  340. val |= SUN6I_DSI_PIXEL_PH_WC(wc);
  341. val |= SUN6I_DSI_PIXEL_PH_ECC(sun6i_dsi_ecc_compute(val));
  342. regmap_write(dsi->regs, SUN6I_DSI_PIXEL_PH_REG, val);
  343. regmap_write(dsi->regs, SUN6I_DSI_PIXEL_PF0_REG,
  344. SUN6I_DSI_PIXEL_PF0_CRC_FORCE(0xffff));
  345. regmap_write(dsi->regs, SUN6I_DSI_PIXEL_PF1_REG,
  346. SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINE0(0xffff) |
  347. SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINEN(0xffff));
  348. regmap_write(dsi->regs, SUN6I_DSI_PIXEL_CTL0_REG,
  349. SUN6I_DSI_PIXEL_CTL0_PD_PLUG_DISABLE |
  350. SUN6I_DSI_PIXEL_CTL0_FORMAT(fmt));
  351. }
  352. static void sun6i_dsi_setup_timings(struct sun6i_dsi *dsi,
  353. struct drm_display_mode *mode)
  354. {
  355. struct mipi_dsi_device *device = dsi->device;
  356. unsigned int Bpp = mipi_dsi_pixel_format_to_bpp(device->format) / 8;
  357. u16 hbp, hfp, hsa, hblk, vblk;
  358. regmap_write(dsi->regs, SUN6I_DSI_BASIC_CTL_REG, 0);
  359. regmap_write(dsi->regs, SUN6I_DSI_SYNC_HSS_REG,
  360. sun6i_dsi_build_sync_pkt(MIPI_DSI_H_SYNC_START,
  361. device->channel,
  362. 0, 0));
  363. regmap_write(dsi->regs, SUN6I_DSI_SYNC_HSE_REG,
  364. sun6i_dsi_build_sync_pkt(MIPI_DSI_H_SYNC_END,
  365. device->channel,
  366. 0, 0));
  367. regmap_write(dsi->regs, SUN6I_DSI_SYNC_VSS_REG,
  368. sun6i_dsi_build_sync_pkt(MIPI_DSI_V_SYNC_START,
  369. device->channel,
  370. 0, 0));
  371. regmap_write(dsi->regs, SUN6I_DSI_SYNC_VSE_REG,
  372. sun6i_dsi_build_sync_pkt(MIPI_DSI_V_SYNC_END,
  373. device->channel,
  374. 0, 0));
  375. regmap_write(dsi->regs, SUN6I_DSI_BASIC_SIZE0_REG,
  376. SUN6I_DSI_BASIC_SIZE0_VSA(mode->vsync_end -
  377. mode->vsync_start) |
  378. SUN6I_DSI_BASIC_SIZE0_VBP(mode->vsync_start -
  379. mode->vdisplay));
  380. regmap_write(dsi->regs, SUN6I_DSI_BASIC_SIZE1_REG,
  381. SUN6I_DSI_BASIC_SIZE1_VACT(mode->vdisplay) |
  382. SUN6I_DSI_BASIC_SIZE1_VT(mode->vtotal));
  383. /*
  384. * A sync period is composed of a blanking packet (4 bytes +
  385. * payload + 2 bytes) and a sync event packet (4 bytes). Its
  386. * minimal size is therefore 10 bytes
  387. */
  388. #define HSA_PACKET_OVERHEAD 10
  389. hsa = max((unsigned int)HSA_PACKET_OVERHEAD,
  390. (mode->hsync_end - mode->hsync_start) * Bpp - HSA_PACKET_OVERHEAD);
  391. regmap_write(dsi->regs, SUN6I_DSI_BLK_HSA0_REG,
  392. sun6i_dsi_build_blk0_pkt(device->channel, hsa));
  393. regmap_write(dsi->regs, SUN6I_DSI_BLK_HSA1_REG,
  394. sun6i_dsi_build_blk1_pkt(0, hsa));
  395. /*
  396. * The backporch is set using a blanking packet (4 bytes +
  397. * payload + 2 bytes). Its minimal size is therefore 6 bytes
  398. */
  399. #define HBP_PACKET_OVERHEAD 6
  400. hbp = max((unsigned int)HBP_PACKET_OVERHEAD,
  401. (mode->hsync_start - mode->hdisplay) * Bpp - HBP_PACKET_OVERHEAD);
  402. regmap_write(dsi->regs, SUN6I_DSI_BLK_HBP0_REG,
  403. sun6i_dsi_build_blk0_pkt(device->channel, hbp));
  404. regmap_write(dsi->regs, SUN6I_DSI_BLK_HBP1_REG,
  405. sun6i_dsi_build_blk1_pkt(0, hbp));
  406. /*
  407. * The frontporch is set using a blanking packet (4 bytes +
  408. * payload + 2 bytes). Its minimal size is therefore 6 bytes
  409. */
  410. #define HFP_PACKET_OVERHEAD 6
  411. hfp = max((unsigned int)HFP_PACKET_OVERHEAD,
  412. (mode->htotal - mode->hsync_end) * Bpp - HFP_PACKET_OVERHEAD);
  413. regmap_write(dsi->regs, SUN6I_DSI_BLK_HFP0_REG,
  414. sun6i_dsi_build_blk0_pkt(device->channel, hfp));
  415. regmap_write(dsi->regs, SUN6I_DSI_BLK_HFP1_REG,
  416. sun6i_dsi_build_blk1_pkt(0, hfp));
  417. /*
  418. * hblk seems to be the line + porches length.
  419. */
  420. hblk = mode->htotal * Bpp - hsa;
  421. regmap_write(dsi->regs, SUN6I_DSI_BLK_HBLK0_REG,
  422. sun6i_dsi_build_blk0_pkt(device->channel, hblk));
  423. regmap_write(dsi->regs, SUN6I_DSI_BLK_HBLK1_REG,
  424. sun6i_dsi_build_blk1_pkt(0, hblk));
  425. /*
  426. * And I'm not entirely sure what vblk is about. The driver in
  427. * Allwinner BSP is using a rather convoluted calculation
  428. * there only for 4 lanes. However, using 0 (the !4 lanes
  429. * case) even with a 4 lanes screen seems to work...
  430. */
  431. vblk = 0;
  432. regmap_write(dsi->regs, SUN6I_DSI_BLK_VBLK0_REG,
  433. sun6i_dsi_build_blk0_pkt(device->channel, vblk));
  434. regmap_write(dsi->regs, SUN6I_DSI_BLK_VBLK1_REG,
  435. sun6i_dsi_build_blk1_pkt(0, vblk));
  436. }
  437. static int sun6i_dsi_start(struct sun6i_dsi *dsi,
  438. enum sun6i_dsi_start_inst func)
  439. {
  440. switch (func) {
  441. case DSI_START_LPTX:
  442. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  443. DSI_INST_ID_LPDT << (4 * DSI_INST_ID_LP11) |
  444. DSI_INST_ID_END << (4 * DSI_INST_ID_LPDT));
  445. break;
  446. case DSI_START_LPRX:
  447. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  448. DSI_INST_ID_LPDT << (4 * DSI_INST_ID_LP11) |
  449. DSI_INST_ID_DLY << (4 * DSI_INST_ID_LPDT) |
  450. DSI_INST_ID_TBA << (4 * DSI_INST_ID_DLY) |
  451. DSI_INST_ID_END << (4 * DSI_INST_ID_TBA));
  452. break;
  453. case DSI_START_HSC:
  454. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  455. DSI_INST_ID_HSC << (4 * DSI_INST_ID_LP11) |
  456. DSI_INST_ID_END << (4 * DSI_INST_ID_HSC));
  457. break;
  458. case DSI_START_HSD:
  459. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  460. DSI_INST_ID_NOP << (4 * DSI_INST_ID_LP11) |
  461. DSI_INST_ID_HSD << (4 * DSI_INST_ID_NOP) |
  462. DSI_INST_ID_DLY << (4 * DSI_INST_ID_HSD) |
  463. DSI_INST_ID_NOP << (4 * DSI_INST_ID_DLY) |
  464. DSI_INST_ID_END << (4 * DSI_INST_ID_HSCEXIT));
  465. break;
  466. default:
  467. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  468. DSI_INST_ID_END << (4 * DSI_INST_ID_LP11));
  469. break;
  470. }
  471. sun6i_dsi_inst_abort(dsi);
  472. sun6i_dsi_inst_commit(dsi);
  473. if (func == DSI_START_HSC)
  474. regmap_write_bits(dsi->regs,
  475. SUN6I_DSI_INST_FUNC_REG(DSI_INST_ID_LP11),
  476. SUN6I_DSI_INST_FUNC_LANE_CEN, 0);
  477. return 0;
  478. }
  479. static void sun6i_dsi_encoder_enable(struct drm_encoder *encoder)
  480. {
  481. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  482. struct sun6i_dsi *dsi = encoder_to_sun6i_dsi(encoder);
  483. struct mipi_dsi_device *device = dsi->device;
  484. u16 delay;
  485. DRM_DEBUG_DRIVER("Enabling DSI output\n");
  486. pm_runtime_get_sync(dsi->dev);
  487. delay = sun6i_dsi_get_video_start_delay(dsi, mode);
  488. regmap_write(dsi->regs, SUN6I_DSI_BASIC_CTL1_REG,
  489. SUN6I_DSI_BASIC_CTL1_VIDEO_ST_DELAY(delay) |
  490. SUN6I_DSI_BASIC_CTL1_VIDEO_FILL |
  491. SUN6I_DSI_BASIC_CTL1_VIDEO_PRECISION |
  492. SUN6I_DSI_BASIC_CTL1_VIDEO_MODE);
  493. sun6i_dsi_setup_burst(dsi, mode);
  494. sun6i_dsi_setup_inst_loop(dsi, mode);
  495. sun6i_dsi_setup_format(dsi, mode);
  496. sun6i_dsi_setup_timings(dsi, mode);
  497. sun6i_dphy_init(dsi->dphy, device->lanes);
  498. sun6i_dphy_power_on(dsi->dphy, device->lanes);
  499. if (!IS_ERR(dsi->panel))
  500. drm_panel_prepare(dsi->panel);
  501. /*
  502. * FIXME: This should be moved after the switch to HS mode.
  503. *
  504. * Unfortunately, once in HS mode, it seems like we're not
  505. * able to send DCS commands anymore, which would prevent any
  506. * panel to send any DCS command as part as their enable
  507. * method, which is quite common.
  508. *
  509. * I haven't seen any artifact due to that sub-optimal
  510. * ordering on the panels I've tested it with, so I guess this
  511. * will do for now, until that IP is better understood.
  512. */
  513. if (!IS_ERR(dsi->panel))
  514. drm_panel_enable(dsi->panel);
  515. sun6i_dsi_start(dsi, DSI_START_HSC);
  516. udelay(1000);
  517. sun6i_dsi_start(dsi, DSI_START_HSD);
  518. }
  519. static void sun6i_dsi_encoder_disable(struct drm_encoder *encoder)
  520. {
  521. struct sun6i_dsi *dsi = encoder_to_sun6i_dsi(encoder);
  522. DRM_DEBUG_DRIVER("Disabling DSI output\n");
  523. if (!IS_ERR(dsi->panel)) {
  524. drm_panel_disable(dsi->panel);
  525. drm_panel_unprepare(dsi->panel);
  526. }
  527. sun6i_dphy_power_off(dsi->dphy);
  528. sun6i_dphy_exit(dsi->dphy);
  529. pm_runtime_put(dsi->dev);
  530. }
  531. static int sun6i_dsi_get_modes(struct drm_connector *connector)
  532. {
  533. struct sun6i_dsi *dsi = connector_to_sun6i_dsi(connector);
  534. return drm_panel_get_modes(dsi->panel);
  535. }
  536. static struct drm_connector_helper_funcs sun6i_dsi_connector_helper_funcs = {
  537. .get_modes = sun6i_dsi_get_modes,
  538. };
  539. static enum drm_connector_status
  540. sun6i_dsi_connector_detect(struct drm_connector *connector, bool force)
  541. {
  542. return connector_status_connected;
  543. }
  544. static const struct drm_connector_funcs sun6i_dsi_connector_funcs = {
  545. .detect = sun6i_dsi_connector_detect,
  546. .fill_modes = drm_helper_probe_single_connector_modes,
  547. .destroy = drm_connector_cleanup,
  548. .reset = drm_atomic_helper_connector_reset,
  549. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  550. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  551. };
  552. static const struct drm_encoder_helper_funcs sun6i_dsi_enc_helper_funcs = {
  553. .disable = sun6i_dsi_encoder_disable,
  554. .enable = sun6i_dsi_encoder_enable,
  555. };
  556. static const struct drm_encoder_funcs sun6i_dsi_enc_funcs = {
  557. .destroy = drm_encoder_cleanup,
  558. };
  559. static u32 sun6i_dsi_dcs_build_pkt_hdr(struct sun6i_dsi *dsi,
  560. const struct mipi_dsi_msg *msg)
  561. {
  562. u32 pkt = msg->type;
  563. if (msg->type == MIPI_DSI_DCS_LONG_WRITE) {
  564. pkt |= ((msg->tx_len + 1) & 0xffff) << 8;
  565. pkt |= (((msg->tx_len + 1) >> 8) & 0xffff) << 16;
  566. } else {
  567. pkt |= (((u8 *)msg->tx_buf)[0] << 8);
  568. if (msg->tx_len > 1)
  569. pkt |= (((u8 *)msg->tx_buf)[1] << 16);
  570. }
  571. pkt |= sun6i_dsi_ecc_compute(pkt) << 24;
  572. return pkt;
  573. }
  574. static int sun6i_dsi_dcs_write_short(struct sun6i_dsi *dsi,
  575. const struct mipi_dsi_msg *msg)
  576. {
  577. regmap_write(dsi->regs, SUN6I_DSI_CMD_TX_REG(0),
  578. sun6i_dsi_dcs_build_pkt_hdr(dsi, msg));
  579. regmap_write_bits(dsi->regs, SUN6I_DSI_CMD_CTL_REG,
  580. 0xff, (4 - 1));
  581. sun6i_dsi_start(dsi, DSI_START_LPTX);
  582. return msg->tx_len;
  583. }
  584. static int sun6i_dsi_dcs_write_long(struct sun6i_dsi *dsi,
  585. const struct mipi_dsi_msg *msg)
  586. {
  587. int ret, len = 0;
  588. u8 *bounce;
  589. u16 crc;
  590. regmap_write(dsi->regs, SUN6I_DSI_CMD_TX_REG(0),
  591. sun6i_dsi_dcs_build_pkt_hdr(dsi, msg));
  592. bounce = kzalloc(msg->tx_len + sizeof(crc), GFP_KERNEL);
  593. if (!bounce)
  594. return -ENOMEM;
  595. memcpy(bounce, msg->tx_buf, msg->tx_len);
  596. len += msg->tx_len;
  597. crc = sun6i_dsi_crc_compute(bounce, msg->tx_len);
  598. memcpy((u8 *)bounce + msg->tx_len, &crc, sizeof(crc));
  599. len += sizeof(crc);
  600. regmap_bulk_write(dsi->regs, SUN6I_DSI_CMD_TX_REG(1), bounce, len);
  601. regmap_write(dsi->regs, SUN6I_DSI_CMD_CTL_REG, len + 4 - 1);
  602. kfree(bounce);
  603. sun6i_dsi_start(dsi, DSI_START_LPTX);
  604. ret = sun6i_dsi_inst_wait_for_completion(dsi);
  605. if (ret < 0) {
  606. sun6i_dsi_inst_abort(dsi);
  607. return ret;
  608. }
  609. /*
  610. * TODO: There's some bits (reg 0x200, bits 8/9) that
  611. * apparently can be used to check whether the data have been
  612. * sent, but I couldn't get it to work reliably.
  613. */
  614. return msg->tx_len;
  615. }
  616. static int sun6i_dsi_dcs_read(struct sun6i_dsi *dsi,
  617. const struct mipi_dsi_msg *msg)
  618. {
  619. u32 val;
  620. int ret;
  621. u8 byte0;
  622. regmap_write(dsi->regs, SUN6I_DSI_CMD_TX_REG(0),
  623. sun6i_dsi_dcs_build_pkt_hdr(dsi, msg));
  624. regmap_write(dsi->regs, SUN6I_DSI_CMD_CTL_REG,
  625. (4 - 1));
  626. sun6i_dsi_start(dsi, DSI_START_LPRX);
  627. ret = sun6i_dsi_inst_wait_for_completion(dsi);
  628. if (ret < 0) {
  629. sun6i_dsi_inst_abort(dsi);
  630. return ret;
  631. }
  632. /*
  633. * TODO: There's some bits (reg 0x200, bits 24/25) that
  634. * apparently can be used to check whether the data have been
  635. * received, but I couldn't get it to work reliably.
  636. */
  637. regmap_read(dsi->regs, SUN6I_DSI_CMD_CTL_REG, &val);
  638. if (val & SUN6I_DSI_CMD_CTL_RX_OVERFLOW)
  639. return -EIO;
  640. regmap_read(dsi->regs, SUN6I_DSI_CMD_RX_REG(0), &val);
  641. byte0 = val & 0xff;
  642. if (byte0 == MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT)
  643. return -EIO;
  644. ((u8 *)msg->rx_buf)[0] = (val >> 8);
  645. return 1;
  646. }
  647. static int sun6i_dsi_attach(struct mipi_dsi_host *host,
  648. struct mipi_dsi_device *device)
  649. {
  650. struct sun6i_dsi *dsi = host_to_sun6i_dsi(host);
  651. dsi->device = device;
  652. dsi->panel = of_drm_find_panel(device->dev.of_node);
  653. if (!dsi->panel)
  654. return -EINVAL;
  655. dev_info(host->dev, "Attached device %s\n", device->name);
  656. return 0;
  657. }
  658. static int sun6i_dsi_detach(struct mipi_dsi_host *host,
  659. struct mipi_dsi_device *device)
  660. {
  661. struct sun6i_dsi *dsi = host_to_sun6i_dsi(host);
  662. dsi->panel = NULL;
  663. dsi->device = NULL;
  664. return 0;
  665. }
  666. static ssize_t sun6i_dsi_transfer(struct mipi_dsi_host *host,
  667. const struct mipi_dsi_msg *msg)
  668. {
  669. struct sun6i_dsi *dsi = host_to_sun6i_dsi(host);
  670. int ret;
  671. ret = sun6i_dsi_inst_wait_for_completion(dsi);
  672. if (ret < 0)
  673. sun6i_dsi_inst_abort(dsi);
  674. regmap_write(dsi->regs, SUN6I_DSI_CMD_CTL_REG,
  675. SUN6I_DSI_CMD_CTL_RX_OVERFLOW |
  676. SUN6I_DSI_CMD_CTL_RX_FLAG |
  677. SUN6I_DSI_CMD_CTL_TX_FLAG);
  678. switch (msg->type) {
  679. case MIPI_DSI_DCS_SHORT_WRITE:
  680. case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
  681. ret = sun6i_dsi_dcs_write_short(dsi, msg);
  682. break;
  683. case MIPI_DSI_DCS_LONG_WRITE:
  684. ret = sun6i_dsi_dcs_write_long(dsi, msg);
  685. break;
  686. case MIPI_DSI_DCS_READ:
  687. if (msg->rx_len == 1) {
  688. ret = sun6i_dsi_dcs_read(dsi, msg);
  689. break;
  690. }
  691. default:
  692. ret = -EINVAL;
  693. }
  694. return ret;
  695. }
  696. static const struct mipi_dsi_host_ops sun6i_dsi_host_ops = {
  697. .attach = sun6i_dsi_attach,
  698. .detach = sun6i_dsi_detach,
  699. .transfer = sun6i_dsi_transfer,
  700. };
  701. static const struct regmap_config sun6i_dsi_regmap_config = {
  702. .reg_bits = 32,
  703. .val_bits = 32,
  704. .reg_stride = 4,
  705. .max_register = SUN6I_DSI_CMD_TX_REG(255),
  706. .name = "mipi-dsi",
  707. };
  708. static int sun6i_dsi_bind(struct device *dev, struct device *master,
  709. void *data)
  710. {
  711. struct drm_device *drm = data;
  712. struct sun4i_drv *drv = drm->dev_private;
  713. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  714. int ret;
  715. if (!dsi->panel)
  716. return -EPROBE_DEFER;
  717. dsi->drv = drv;
  718. drm_encoder_helper_add(&dsi->encoder,
  719. &sun6i_dsi_enc_helper_funcs);
  720. ret = drm_encoder_init(drm,
  721. &dsi->encoder,
  722. &sun6i_dsi_enc_funcs,
  723. DRM_MODE_ENCODER_DSI,
  724. NULL);
  725. if (ret) {
  726. dev_err(dsi->dev, "Couldn't initialise the DSI encoder\n");
  727. return ret;
  728. }
  729. dsi->encoder.possible_crtcs = BIT(0);
  730. drm_connector_helper_add(&dsi->connector,
  731. &sun6i_dsi_connector_helper_funcs);
  732. ret = drm_connector_init(drm, &dsi->connector,
  733. &sun6i_dsi_connector_funcs,
  734. DRM_MODE_CONNECTOR_DSI);
  735. if (ret) {
  736. dev_err(dsi->dev,
  737. "Couldn't initialise the DSI connector\n");
  738. goto err_cleanup_connector;
  739. }
  740. drm_mode_connector_attach_encoder(&dsi->connector, &dsi->encoder);
  741. drm_panel_attach(dsi->panel, &dsi->connector);
  742. return 0;
  743. err_cleanup_connector:
  744. drm_encoder_cleanup(&dsi->encoder);
  745. return ret;
  746. }
  747. static void sun6i_dsi_unbind(struct device *dev, struct device *master,
  748. void *data)
  749. {
  750. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  751. drm_panel_detach(dsi->panel);
  752. }
  753. static const struct component_ops sun6i_dsi_ops = {
  754. .bind = sun6i_dsi_bind,
  755. .unbind = sun6i_dsi_unbind,
  756. };
  757. static int sun6i_dsi_probe(struct platform_device *pdev)
  758. {
  759. struct device *dev = &pdev->dev;
  760. struct device_node *dphy_node;
  761. struct sun6i_dsi *dsi;
  762. struct resource *res;
  763. void __iomem *base;
  764. int ret;
  765. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  766. if (!dsi)
  767. return -ENOMEM;
  768. dev_set_drvdata(dev, dsi);
  769. dsi->dev = dev;
  770. dsi->host.ops = &sun6i_dsi_host_ops;
  771. dsi->host.dev = dev;
  772. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  773. base = devm_ioremap_resource(dev, res);
  774. if (IS_ERR(base)) {
  775. dev_err(dev, "Couldn't map the DSI encoder registers\n");
  776. return PTR_ERR(base);
  777. }
  778. dsi->regs = devm_regmap_init_mmio_clk(dev, "bus", base,
  779. &sun6i_dsi_regmap_config);
  780. if (IS_ERR(dsi->regs)) {
  781. dev_err(dev, "Couldn't create the DSI encoder regmap\n");
  782. return PTR_ERR(dsi->regs);
  783. }
  784. dsi->reset = devm_reset_control_get_shared(dev, NULL);
  785. if (IS_ERR(dsi->reset)) {
  786. dev_err(dev, "Couldn't get our reset line\n");
  787. return PTR_ERR(dsi->reset);
  788. }
  789. dsi->mod_clk = devm_clk_get(dev, "mod");
  790. if (IS_ERR(dsi->mod_clk)) {
  791. dev_err(dev, "Couldn't get the DSI mod clock\n");
  792. return PTR_ERR(dsi->mod_clk);
  793. }
  794. /*
  795. * In order to operate properly, that clock seems to be always
  796. * set to 297MHz.
  797. */
  798. clk_set_rate_exclusive(dsi->mod_clk, 297000000);
  799. dphy_node = of_parse_phandle(dev->of_node, "phys", 0);
  800. ret = sun6i_dphy_probe(dsi, dphy_node);
  801. of_node_put(dphy_node);
  802. if (ret) {
  803. dev_err(dev, "Couldn't get the MIPI D-PHY\n");
  804. goto err_unprotect_clk;
  805. }
  806. pm_runtime_enable(dev);
  807. ret = mipi_dsi_host_register(&dsi->host);
  808. if (ret) {
  809. dev_err(dev, "Couldn't register MIPI-DSI host\n");
  810. goto err_remove_phy;
  811. }
  812. ret = component_add(&pdev->dev, &sun6i_dsi_ops);
  813. if (ret) {
  814. dev_err(dev, "Couldn't register our component\n");
  815. goto err_remove_dsi_host;
  816. }
  817. return 0;
  818. err_remove_dsi_host:
  819. mipi_dsi_host_unregister(&dsi->host);
  820. err_remove_phy:
  821. pm_runtime_disable(dev);
  822. sun6i_dphy_remove(dsi);
  823. err_unprotect_clk:
  824. clk_rate_exclusive_put(dsi->mod_clk);
  825. return ret;
  826. }
  827. static int sun6i_dsi_remove(struct platform_device *pdev)
  828. {
  829. struct device *dev = &pdev->dev;
  830. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  831. component_del(&pdev->dev, &sun6i_dsi_ops);
  832. mipi_dsi_host_unregister(&dsi->host);
  833. pm_runtime_disable(dev);
  834. sun6i_dphy_remove(dsi);
  835. clk_rate_exclusive_put(dsi->mod_clk);
  836. return 0;
  837. }
  838. static int sun6i_dsi_runtime_resume(struct device *dev)
  839. {
  840. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  841. reset_control_deassert(dsi->reset);
  842. clk_prepare_enable(dsi->mod_clk);
  843. /*
  844. * Enable the DSI block.
  845. *
  846. * Some part of it can only be done once we get a number of
  847. * lanes, see sun6i_dsi_inst_init
  848. */
  849. regmap_write(dsi->regs, SUN6I_DSI_CTL_REG, SUN6I_DSI_CTL_EN);
  850. regmap_write(dsi->regs, SUN6I_DSI_BASIC_CTL0_REG,
  851. SUN6I_DSI_BASIC_CTL0_ECC_EN | SUN6I_DSI_BASIC_CTL0_CRC_EN);
  852. regmap_write(dsi->regs, SUN6I_DSI_TRANS_START_REG, 10);
  853. regmap_write(dsi->regs, SUN6I_DSI_TRANS_ZERO_REG, 0);
  854. if (dsi->device)
  855. sun6i_dsi_inst_init(dsi, dsi->device);
  856. regmap_write(dsi->regs, SUN6I_DSI_DEBUG_DATA_REG, 0xff);
  857. return 0;
  858. }
  859. static int sun6i_dsi_runtime_suspend(struct device *dev)
  860. {
  861. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  862. clk_disable_unprepare(dsi->mod_clk);
  863. reset_control_assert(dsi->reset);
  864. return 0;
  865. }
  866. static const struct dev_pm_ops sun6i_dsi_pm_ops = {
  867. SET_RUNTIME_PM_OPS(sun6i_dsi_runtime_suspend,
  868. sun6i_dsi_runtime_resume,
  869. NULL)
  870. };
  871. static const struct of_device_id sun6i_dsi_of_table[] = {
  872. { .compatible = "allwinner,sun6i-a31-mipi-dsi" },
  873. { }
  874. };
  875. MODULE_DEVICE_TABLE(of, sun6i_dsi_of_table);
  876. static struct platform_driver sun6i_dsi_platform_driver = {
  877. .probe = sun6i_dsi_probe,
  878. .remove = sun6i_dsi_remove,
  879. .driver = {
  880. .name = "sun6i-mipi-dsi",
  881. .of_match_table = sun6i_dsi_of_table,
  882. .pm = &sun6i_dsi_pm_ops,
  883. },
  884. };
  885. module_platform_driver(sun6i_dsi_platform_driver);
  886. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  887. MODULE_DESCRIPTION("Allwinner A31 DSI Driver");
  888. MODULE_LICENSE("GPL");