sun6i_mipi_dsi.c 30 KB

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