sun6i_mipi_dsi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. u16 delay = mode->vtotal - (mode->vsync_end - mode->vdisplay) + 1;
  280. if (delay > mode->vtotal)
  281. delay = delay % mode->vtotal;
  282. return max_t(u16, delay, 1);
  283. }
  284. static void sun6i_dsi_setup_burst(struct sun6i_dsi *dsi,
  285. struct drm_display_mode *mode)
  286. {
  287. struct mipi_dsi_device *device = dsi->device;
  288. u32 val = 0;
  289. if ((mode->hsync_end - mode->hdisplay) > 20) {
  290. /* Maaaaaagic */
  291. u16 drq = (mode->hsync_end - mode->hdisplay) - 20;
  292. drq *= mipi_dsi_pixel_format_to_bpp(device->format);
  293. drq /= 32;
  294. val = (SUN6I_DSI_TCON_DRQ_ENABLE_MODE |
  295. SUN6I_DSI_TCON_DRQ_SET(drq));
  296. }
  297. regmap_write(dsi->regs, SUN6I_DSI_TCON_DRQ_REG, val);
  298. }
  299. static void sun6i_dsi_setup_inst_loop(struct sun6i_dsi *dsi,
  300. struct drm_display_mode *mode)
  301. {
  302. u16 delay = 50 - 1;
  303. regmap_write(dsi->regs, SUN6I_DSI_INST_LOOP_NUM_REG(0),
  304. SUN6I_DSI_INST_LOOP_NUM_N0(50 - 1) |
  305. SUN6I_DSI_INST_LOOP_NUM_N1(delay));
  306. regmap_write(dsi->regs, SUN6I_DSI_INST_LOOP_NUM_REG(1),
  307. SUN6I_DSI_INST_LOOP_NUM_N0(50 - 1) |
  308. SUN6I_DSI_INST_LOOP_NUM_N1(delay));
  309. }
  310. static void sun6i_dsi_setup_format(struct sun6i_dsi *dsi,
  311. struct drm_display_mode *mode)
  312. {
  313. struct mipi_dsi_device *device = dsi->device;
  314. u32 val = SUN6I_DSI_PIXEL_PH_VC(device->channel);
  315. u8 dt, fmt;
  316. u16 wc;
  317. /*
  318. * TODO: The format defines are only valid in video mode and
  319. * change in command mode.
  320. */
  321. switch (device->format) {
  322. case MIPI_DSI_FMT_RGB888:
  323. dt = MIPI_DSI_PACKED_PIXEL_STREAM_24;
  324. fmt = 8;
  325. break;
  326. case MIPI_DSI_FMT_RGB666:
  327. dt = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
  328. fmt = 9;
  329. break;
  330. case MIPI_DSI_FMT_RGB666_PACKED:
  331. dt = MIPI_DSI_PACKED_PIXEL_STREAM_18;
  332. fmt = 10;
  333. break;
  334. case MIPI_DSI_FMT_RGB565:
  335. dt = MIPI_DSI_PACKED_PIXEL_STREAM_16;
  336. fmt = 11;
  337. break;
  338. default:
  339. return;
  340. }
  341. val |= SUN6I_DSI_PIXEL_PH_DT(dt);
  342. wc = mode->hdisplay * mipi_dsi_pixel_format_to_bpp(device->format) / 8;
  343. val |= SUN6I_DSI_PIXEL_PH_WC(wc);
  344. val |= SUN6I_DSI_PIXEL_PH_ECC(sun6i_dsi_ecc_compute(val));
  345. regmap_write(dsi->regs, SUN6I_DSI_PIXEL_PH_REG, val);
  346. regmap_write(dsi->regs, SUN6I_DSI_PIXEL_PF0_REG,
  347. SUN6I_DSI_PIXEL_PF0_CRC_FORCE(0xffff));
  348. regmap_write(dsi->regs, SUN6I_DSI_PIXEL_PF1_REG,
  349. SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINE0(0xffff) |
  350. SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINEN(0xffff));
  351. regmap_write(dsi->regs, SUN6I_DSI_PIXEL_CTL0_REG,
  352. SUN6I_DSI_PIXEL_CTL0_PD_PLUG_DISABLE |
  353. SUN6I_DSI_PIXEL_CTL0_FORMAT(fmt));
  354. }
  355. static void sun6i_dsi_setup_timings(struct sun6i_dsi *dsi,
  356. struct drm_display_mode *mode)
  357. {
  358. struct mipi_dsi_device *device = dsi->device;
  359. unsigned int Bpp = mipi_dsi_pixel_format_to_bpp(device->format) / 8;
  360. u16 hbp, hfp, hsa, hblk, vblk;
  361. size_t bytes;
  362. u8 *buffer;
  363. /* Do all timing calculations up front to allocate buffer space */
  364. /*
  365. * A sync period is composed of a blanking packet (4 bytes +
  366. * payload + 2 bytes) and a sync event packet (4 bytes). Its
  367. * minimal size is therefore 10 bytes
  368. */
  369. #define HSA_PACKET_OVERHEAD 10
  370. hsa = max((unsigned int)HSA_PACKET_OVERHEAD,
  371. (mode->hsync_end - mode->hsync_start) * Bpp - HSA_PACKET_OVERHEAD);
  372. /*
  373. * The backporch is set using a blanking packet (4 bytes +
  374. * payload + 2 bytes). Its minimal size is therefore 6 bytes
  375. */
  376. #define HBP_PACKET_OVERHEAD 6
  377. hbp = max((unsigned int)HBP_PACKET_OVERHEAD,
  378. (mode->hsync_start - mode->hdisplay) * Bpp - HBP_PACKET_OVERHEAD);
  379. /*
  380. * The frontporch is set using a blanking packet (4 bytes +
  381. * payload + 2 bytes). Its minimal size is therefore 6 bytes
  382. */
  383. #define HFP_PACKET_OVERHEAD 6
  384. hfp = max((unsigned int)HFP_PACKET_OVERHEAD,
  385. (mode->htotal - mode->hsync_end) * Bpp - HFP_PACKET_OVERHEAD);
  386. /*
  387. * hblk seems to be the line + porches length.
  388. */
  389. hblk = mode->htotal * Bpp - hsa;
  390. /*
  391. * And I'm not entirely sure what vblk is about. The driver in
  392. * Allwinner BSP is using a rather convoluted calculation
  393. * there only for 4 lanes. However, using 0 (the !4 lanes
  394. * case) even with a 4 lanes screen seems to work...
  395. */
  396. vblk = 0;
  397. /* How many bytes do we need to send all payloads? */
  398. bytes = max_t(size_t, max(max(hfp, hblk), max(hsa, hbp)), vblk);
  399. buffer = kmalloc(bytes, GFP_KERNEL);
  400. if (WARN_ON(!buffer))
  401. return;
  402. regmap_write(dsi->regs, SUN6I_DSI_BASIC_CTL_REG, 0);
  403. regmap_write(dsi->regs, SUN6I_DSI_SYNC_HSS_REG,
  404. sun6i_dsi_build_sync_pkt(MIPI_DSI_H_SYNC_START,
  405. device->channel,
  406. 0, 0));
  407. regmap_write(dsi->regs, SUN6I_DSI_SYNC_HSE_REG,
  408. sun6i_dsi_build_sync_pkt(MIPI_DSI_H_SYNC_END,
  409. device->channel,
  410. 0, 0));
  411. regmap_write(dsi->regs, SUN6I_DSI_SYNC_VSS_REG,
  412. sun6i_dsi_build_sync_pkt(MIPI_DSI_V_SYNC_START,
  413. device->channel,
  414. 0, 0));
  415. regmap_write(dsi->regs, SUN6I_DSI_SYNC_VSE_REG,
  416. sun6i_dsi_build_sync_pkt(MIPI_DSI_V_SYNC_END,
  417. device->channel,
  418. 0, 0));
  419. regmap_write(dsi->regs, SUN6I_DSI_BASIC_SIZE0_REG,
  420. SUN6I_DSI_BASIC_SIZE0_VSA(mode->vsync_end -
  421. mode->vsync_start) |
  422. SUN6I_DSI_BASIC_SIZE0_VBP(mode->vsync_start -
  423. mode->vdisplay));
  424. regmap_write(dsi->regs, SUN6I_DSI_BASIC_SIZE1_REG,
  425. SUN6I_DSI_BASIC_SIZE1_VACT(mode->vdisplay) |
  426. SUN6I_DSI_BASIC_SIZE1_VT(mode->vtotal));
  427. /* sync */
  428. regmap_write(dsi->regs, SUN6I_DSI_BLK_HSA0_REG,
  429. sun6i_dsi_build_blk0_pkt(device->channel, hsa));
  430. regmap_write(dsi->regs, SUN6I_DSI_BLK_HSA1_REG,
  431. sun6i_dsi_build_blk1_pkt(0, buffer, hsa));
  432. /* backporch */
  433. regmap_write(dsi->regs, SUN6I_DSI_BLK_HBP0_REG,
  434. sun6i_dsi_build_blk0_pkt(device->channel, hbp));
  435. regmap_write(dsi->regs, SUN6I_DSI_BLK_HBP1_REG,
  436. sun6i_dsi_build_blk1_pkt(0, buffer, hbp));
  437. /* frontporch */
  438. regmap_write(dsi->regs, SUN6I_DSI_BLK_HFP0_REG,
  439. sun6i_dsi_build_blk0_pkt(device->channel, hfp));
  440. regmap_write(dsi->regs, SUN6I_DSI_BLK_HFP1_REG,
  441. sun6i_dsi_build_blk1_pkt(0, buffer, hfp));
  442. /* hblk */
  443. regmap_write(dsi->regs, SUN6I_DSI_BLK_HBLK0_REG,
  444. sun6i_dsi_build_blk0_pkt(device->channel, hblk));
  445. regmap_write(dsi->regs, SUN6I_DSI_BLK_HBLK1_REG,
  446. sun6i_dsi_build_blk1_pkt(0, buffer, hblk));
  447. /* vblk */
  448. regmap_write(dsi->regs, SUN6I_DSI_BLK_VBLK0_REG,
  449. sun6i_dsi_build_blk0_pkt(device->channel, vblk));
  450. regmap_write(dsi->regs, SUN6I_DSI_BLK_VBLK1_REG,
  451. sun6i_dsi_build_blk1_pkt(0, buffer, vblk));
  452. kfree(buffer);
  453. }
  454. static int sun6i_dsi_start(struct sun6i_dsi *dsi,
  455. enum sun6i_dsi_start_inst func)
  456. {
  457. switch (func) {
  458. case DSI_START_LPTX:
  459. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  460. DSI_INST_ID_LPDT << (4 * DSI_INST_ID_LP11) |
  461. DSI_INST_ID_END << (4 * DSI_INST_ID_LPDT));
  462. break;
  463. case DSI_START_LPRX:
  464. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  465. DSI_INST_ID_LPDT << (4 * DSI_INST_ID_LP11) |
  466. DSI_INST_ID_DLY << (4 * DSI_INST_ID_LPDT) |
  467. DSI_INST_ID_TBA << (4 * DSI_INST_ID_DLY) |
  468. DSI_INST_ID_END << (4 * DSI_INST_ID_TBA));
  469. break;
  470. case DSI_START_HSC:
  471. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  472. DSI_INST_ID_HSC << (4 * DSI_INST_ID_LP11) |
  473. DSI_INST_ID_END << (4 * DSI_INST_ID_HSC));
  474. break;
  475. case DSI_START_HSD:
  476. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  477. DSI_INST_ID_NOP << (4 * DSI_INST_ID_LP11) |
  478. DSI_INST_ID_HSD << (4 * DSI_INST_ID_NOP) |
  479. DSI_INST_ID_DLY << (4 * DSI_INST_ID_HSD) |
  480. DSI_INST_ID_NOP << (4 * DSI_INST_ID_DLY) |
  481. DSI_INST_ID_END << (4 * DSI_INST_ID_HSCEXIT));
  482. break;
  483. default:
  484. regmap_write(dsi->regs, SUN6I_DSI_INST_JUMP_SEL_REG,
  485. DSI_INST_ID_END << (4 * DSI_INST_ID_LP11));
  486. break;
  487. }
  488. sun6i_dsi_inst_abort(dsi);
  489. sun6i_dsi_inst_commit(dsi);
  490. if (func == DSI_START_HSC)
  491. regmap_write_bits(dsi->regs,
  492. SUN6I_DSI_INST_FUNC_REG(DSI_INST_ID_LP11),
  493. SUN6I_DSI_INST_FUNC_LANE_CEN, 0);
  494. return 0;
  495. }
  496. static void sun6i_dsi_encoder_enable(struct drm_encoder *encoder)
  497. {
  498. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  499. struct sun6i_dsi *dsi = encoder_to_sun6i_dsi(encoder);
  500. struct mipi_dsi_device *device = dsi->device;
  501. u16 delay;
  502. DRM_DEBUG_DRIVER("Enabling DSI output\n");
  503. pm_runtime_get_sync(dsi->dev);
  504. delay = sun6i_dsi_get_video_start_delay(dsi, mode);
  505. regmap_write(dsi->regs, SUN6I_DSI_BASIC_CTL1_REG,
  506. SUN6I_DSI_BASIC_CTL1_VIDEO_ST_DELAY(delay) |
  507. SUN6I_DSI_BASIC_CTL1_VIDEO_FILL |
  508. SUN6I_DSI_BASIC_CTL1_VIDEO_PRECISION |
  509. SUN6I_DSI_BASIC_CTL1_VIDEO_MODE);
  510. sun6i_dsi_setup_burst(dsi, mode);
  511. sun6i_dsi_setup_inst_loop(dsi, mode);
  512. sun6i_dsi_setup_format(dsi, mode);
  513. sun6i_dsi_setup_timings(dsi, mode);
  514. sun6i_dphy_init(dsi->dphy, device->lanes);
  515. sun6i_dphy_power_on(dsi->dphy, device->lanes);
  516. if (!IS_ERR(dsi->panel))
  517. drm_panel_prepare(dsi->panel);
  518. /*
  519. * FIXME: This should be moved after the switch to HS mode.
  520. *
  521. * Unfortunately, once in HS mode, it seems like we're not
  522. * able to send DCS commands anymore, which would prevent any
  523. * panel to send any DCS command as part as their enable
  524. * method, which is quite common.
  525. *
  526. * I haven't seen any artifact due to that sub-optimal
  527. * ordering on the panels I've tested it with, so I guess this
  528. * will do for now, until that IP is better understood.
  529. */
  530. if (!IS_ERR(dsi->panel))
  531. drm_panel_enable(dsi->panel);
  532. sun6i_dsi_start(dsi, DSI_START_HSC);
  533. udelay(1000);
  534. sun6i_dsi_start(dsi, DSI_START_HSD);
  535. }
  536. static void sun6i_dsi_encoder_disable(struct drm_encoder *encoder)
  537. {
  538. struct sun6i_dsi *dsi = encoder_to_sun6i_dsi(encoder);
  539. DRM_DEBUG_DRIVER("Disabling DSI output\n");
  540. if (!IS_ERR(dsi->panel)) {
  541. drm_panel_disable(dsi->panel);
  542. drm_panel_unprepare(dsi->panel);
  543. }
  544. sun6i_dphy_power_off(dsi->dphy);
  545. sun6i_dphy_exit(dsi->dphy);
  546. pm_runtime_put(dsi->dev);
  547. }
  548. static int sun6i_dsi_get_modes(struct drm_connector *connector)
  549. {
  550. struct sun6i_dsi *dsi = connector_to_sun6i_dsi(connector);
  551. return drm_panel_get_modes(dsi->panel);
  552. }
  553. static struct drm_connector_helper_funcs sun6i_dsi_connector_helper_funcs = {
  554. .get_modes = sun6i_dsi_get_modes,
  555. };
  556. static enum drm_connector_status
  557. sun6i_dsi_connector_detect(struct drm_connector *connector, bool force)
  558. {
  559. return connector_status_connected;
  560. }
  561. static const struct drm_connector_funcs sun6i_dsi_connector_funcs = {
  562. .detect = sun6i_dsi_connector_detect,
  563. .fill_modes = drm_helper_probe_single_connector_modes,
  564. .destroy = drm_connector_cleanup,
  565. .reset = drm_atomic_helper_connector_reset,
  566. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  567. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  568. };
  569. static const struct drm_encoder_helper_funcs sun6i_dsi_enc_helper_funcs = {
  570. .disable = sun6i_dsi_encoder_disable,
  571. .enable = sun6i_dsi_encoder_enable,
  572. };
  573. static const struct drm_encoder_funcs sun6i_dsi_enc_funcs = {
  574. .destroy = drm_encoder_cleanup,
  575. };
  576. static u32 sun6i_dsi_dcs_build_pkt_hdr(struct sun6i_dsi *dsi,
  577. const struct mipi_dsi_msg *msg)
  578. {
  579. u32 pkt = msg->type;
  580. if (msg->type == MIPI_DSI_DCS_LONG_WRITE) {
  581. pkt |= ((msg->tx_len + 1) & 0xffff) << 8;
  582. pkt |= (((msg->tx_len + 1) >> 8) & 0xffff) << 16;
  583. } else {
  584. pkt |= (((u8 *)msg->tx_buf)[0] << 8);
  585. if (msg->tx_len > 1)
  586. pkt |= (((u8 *)msg->tx_buf)[1] << 16);
  587. }
  588. pkt |= sun6i_dsi_ecc_compute(pkt) << 24;
  589. return pkt;
  590. }
  591. static int sun6i_dsi_dcs_write_short(struct sun6i_dsi *dsi,
  592. const struct mipi_dsi_msg *msg)
  593. {
  594. regmap_write(dsi->regs, SUN6I_DSI_CMD_TX_REG(0),
  595. sun6i_dsi_dcs_build_pkt_hdr(dsi, msg));
  596. regmap_write_bits(dsi->regs, SUN6I_DSI_CMD_CTL_REG,
  597. 0xff, (4 - 1));
  598. sun6i_dsi_start(dsi, DSI_START_LPTX);
  599. return msg->tx_len;
  600. }
  601. static int sun6i_dsi_dcs_write_long(struct sun6i_dsi *dsi,
  602. const struct mipi_dsi_msg *msg)
  603. {
  604. int ret, len = 0;
  605. u8 *bounce;
  606. u16 crc;
  607. regmap_write(dsi->regs, SUN6I_DSI_CMD_TX_REG(0),
  608. sun6i_dsi_dcs_build_pkt_hdr(dsi, msg));
  609. bounce = kzalloc(ALIGN(msg->tx_len + sizeof(crc), 4), GFP_KERNEL);
  610. if (!bounce)
  611. return -ENOMEM;
  612. memcpy(bounce, msg->tx_buf, msg->tx_len);
  613. len += msg->tx_len;
  614. crc = sun6i_dsi_crc_compute(bounce, msg->tx_len);
  615. memcpy((u8 *)bounce + msg->tx_len, &crc, sizeof(crc));
  616. len += sizeof(crc);
  617. regmap_bulk_write(dsi->regs, SUN6I_DSI_CMD_TX_REG(1), bounce, DIV_ROUND_UP(len, 4));
  618. regmap_write(dsi->regs, SUN6I_DSI_CMD_CTL_REG, len + 4 - 1);
  619. kfree(bounce);
  620. sun6i_dsi_start(dsi, DSI_START_LPTX);
  621. ret = sun6i_dsi_inst_wait_for_completion(dsi);
  622. if (ret < 0) {
  623. sun6i_dsi_inst_abort(dsi);
  624. return ret;
  625. }
  626. /*
  627. * TODO: There's some bits (reg 0x200, bits 8/9) that
  628. * apparently can be used to check whether the data have been
  629. * sent, but I couldn't get it to work reliably.
  630. */
  631. return msg->tx_len;
  632. }
  633. static int sun6i_dsi_dcs_read(struct sun6i_dsi *dsi,
  634. const struct mipi_dsi_msg *msg)
  635. {
  636. u32 val;
  637. int ret;
  638. u8 byte0;
  639. regmap_write(dsi->regs, SUN6I_DSI_CMD_TX_REG(0),
  640. sun6i_dsi_dcs_build_pkt_hdr(dsi, msg));
  641. regmap_write(dsi->regs, SUN6I_DSI_CMD_CTL_REG,
  642. (4 - 1));
  643. sun6i_dsi_start(dsi, DSI_START_LPRX);
  644. ret = sun6i_dsi_inst_wait_for_completion(dsi);
  645. if (ret < 0) {
  646. sun6i_dsi_inst_abort(dsi);
  647. return ret;
  648. }
  649. /*
  650. * TODO: There's some bits (reg 0x200, bits 24/25) that
  651. * apparently can be used to check whether the data have been
  652. * received, but I couldn't get it to work reliably.
  653. */
  654. regmap_read(dsi->regs, SUN6I_DSI_CMD_CTL_REG, &val);
  655. if (val & SUN6I_DSI_CMD_CTL_RX_OVERFLOW)
  656. return -EIO;
  657. regmap_read(dsi->regs, SUN6I_DSI_CMD_RX_REG(0), &val);
  658. byte0 = val & 0xff;
  659. if (byte0 == MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT)
  660. return -EIO;
  661. ((u8 *)msg->rx_buf)[0] = (val >> 8);
  662. return 1;
  663. }
  664. static int sun6i_dsi_attach(struct mipi_dsi_host *host,
  665. struct mipi_dsi_device *device)
  666. {
  667. struct sun6i_dsi *dsi = host_to_sun6i_dsi(host);
  668. dsi->device = device;
  669. dsi->panel = of_drm_find_panel(device->dev.of_node);
  670. if (IS_ERR(dsi->panel))
  671. return PTR_ERR(dsi->panel);
  672. dev_info(host->dev, "Attached device %s\n", device->name);
  673. return 0;
  674. }
  675. static int sun6i_dsi_detach(struct mipi_dsi_host *host,
  676. struct mipi_dsi_device *device)
  677. {
  678. struct sun6i_dsi *dsi = host_to_sun6i_dsi(host);
  679. dsi->panel = NULL;
  680. dsi->device = NULL;
  681. return 0;
  682. }
  683. static ssize_t sun6i_dsi_transfer(struct mipi_dsi_host *host,
  684. const struct mipi_dsi_msg *msg)
  685. {
  686. struct sun6i_dsi *dsi = host_to_sun6i_dsi(host);
  687. int ret;
  688. ret = sun6i_dsi_inst_wait_for_completion(dsi);
  689. if (ret < 0)
  690. sun6i_dsi_inst_abort(dsi);
  691. regmap_write(dsi->regs, SUN6I_DSI_CMD_CTL_REG,
  692. SUN6I_DSI_CMD_CTL_RX_OVERFLOW |
  693. SUN6I_DSI_CMD_CTL_RX_FLAG |
  694. SUN6I_DSI_CMD_CTL_TX_FLAG);
  695. switch (msg->type) {
  696. case MIPI_DSI_DCS_SHORT_WRITE:
  697. case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
  698. ret = sun6i_dsi_dcs_write_short(dsi, msg);
  699. break;
  700. case MIPI_DSI_DCS_LONG_WRITE:
  701. ret = sun6i_dsi_dcs_write_long(dsi, msg);
  702. break;
  703. case MIPI_DSI_DCS_READ:
  704. if (msg->rx_len == 1) {
  705. ret = sun6i_dsi_dcs_read(dsi, msg);
  706. break;
  707. }
  708. default:
  709. ret = -EINVAL;
  710. }
  711. return ret;
  712. }
  713. static const struct mipi_dsi_host_ops sun6i_dsi_host_ops = {
  714. .attach = sun6i_dsi_attach,
  715. .detach = sun6i_dsi_detach,
  716. .transfer = sun6i_dsi_transfer,
  717. };
  718. static const struct regmap_config sun6i_dsi_regmap_config = {
  719. .reg_bits = 32,
  720. .val_bits = 32,
  721. .reg_stride = 4,
  722. .max_register = SUN6I_DSI_CMD_TX_REG(255),
  723. .name = "mipi-dsi",
  724. };
  725. static int sun6i_dsi_bind(struct device *dev, struct device *master,
  726. void *data)
  727. {
  728. struct drm_device *drm = data;
  729. struct sun4i_drv *drv = drm->dev_private;
  730. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  731. int ret;
  732. if (!dsi->panel)
  733. return -EPROBE_DEFER;
  734. dsi->drv = drv;
  735. drm_encoder_helper_add(&dsi->encoder,
  736. &sun6i_dsi_enc_helper_funcs);
  737. ret = drm_encoder_init(drm,
  738. &dsi->encoder,
  739. &sun6i_dsi_enc_funcs,
  740. DRM_MODE_ENCODER_DSI,
  741. NULL);
  742. if (ret) {
  743. dev_err(dsi->dev, "Couldn't initialise the DSI encoder\n");
  744. return ret;
  745. }
  746. dsi->encoder.possible_crtcs = BIT(0);
  747. drm_connector_helper_add(&dsi->connector,
  748. &sun6i_dsi_connector_helper_funcs);
  749. ret = drm_connector_init(drm, &dsi->connector,
  750. &sun6i_dsi_connector_funcs,
  751. DRM_MODE_CONNECTOR_DSI);
  752. if (ret) {
  753. dev_err(dsi->dev,
  754. "Couldn't initialise the DSI connector\n");
  755. goto err_cleanup_connector;
  756. }
  757. drm_connector_attach_encoder(&dsi->connector, &dsi->encoder);
  758. drm_panel_attach(dsi->panel, &dsi->connector);
  759. return 0;
  760. err_cleanup_connector:
  761. drm_encoder_cleanup(&dsi->encoder);
  762. return ret;
  763. }
  764. static void sun6i_dsi_unbind(struct device *dev, struct device *master,
  765. void *data)
  766. {
  767. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  768. drm_panel_detach(dsi->panel);
  769. }
  770. static const struct component_ops sun6i_dsi_ops = {
  771. .bind = sun6i_dsi_bind,
  772. .unbind = sun6i_dsi_unbind,
  773. };
  774. static int sun6i_dsi_probe(struct platform_device *pdev)
  775. {
  776. struct device *dev = &pdev->dev;
  777. struct device_node *dphy_node;
  778. struct sun6i_dsi *dsi;
  779. struct resource *res;
  780. void __iomem *base;
  781. int ret;
  782. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  783. if (!dsi)
  784. return -ENOMEM;
  785. dev_set_drvdata(dev, dsi);
  786. dsi->dev = dev;
  787. dsi->host.ops = &sun6i_dsi_host_ops;
  788. dsi->host.dev = dev;
  789. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  790. base = devm_ioremap_resource(dev, res);
  791. if (IS_ERR(base)) {
  792. dev_err(dev, "Couldn't map the DSI encoder registers\n");
  793. return PTR_ERR(base);
  794. }
  795. dsi->regs = devm_regmap_init_mmio_clk(dev, "bus", base,
  796. &sun6i_dsi_regmap_config);
  797. if (IS_ERR(dsi->regs)) {
  798. dev_err(dev, "Couldn't create the DSI encoder regmap\n");
  799. return PTR_ERR(dsi->regs);
  800. }
  801. dsi->reset = devm_reset_control_get_shared(dev, NULL);
  802. if (IS_ERR(dsi->reset)) {
  803. dev_err(dev, "Couldn't get our reset line\n");
  804. return PTR_ERR(dsi->reset);
  805. }
  806. dsi->mod_clk = devm_clk_get(dev, "mod");
  807. if (IS_ERR(dsi->mod_clk)) {
  808. dev_err(dev, "Couldn't get the DSI mod clock\n");
  809. return PTR_ERR(dsi->mod_clk);
  810. }
  811. /*
  812. * In order to operate properly, that clock seems to be always
  813. * set to 297MHz.
  814. */
  815. clk_set_rate_exclusive(dsi->mod_clk, 297000000);
  816. dphy_node = of_parse_phandle(dev->of_node, "phys", 0);
  817. ret = sun6i_dphy_probe(dsi, dphy_node);
  818. of_node_put(dphy_node);
  819. if (ret) {
  820. dev_err(dev, "Couldn't get the MIPI D-PHY\n");
  821. goto err_unprotect_clk;
  822. }
  823. pm_runtime_enable(dev);
  824. ret = mipi_dsi_host_register(&dsi->host);
  825. if (ret) {
  826. dev_err(dev, "Couldn't register MIPI-DSI host\n");
  827. goto err_remove_phy;
  828. }
  829. ret = component_add(&pdev->dev, &sun6i_dsi_ops);
  830. if (ret) {
  831. dev_err(dev, "Couldn't register our component\n");
  832. goto err_remove_dsi_host;
  833. }
  834. return 0;
  835. err_remove_dsi_host:
  836. mipi_dsi_host_unregister(&dsi->host);
  837. err_remove_phy:
  838. pm_runtime_disable(dev);
  839. sun6i_dphy_remove(dsi);
  840. err_unprotect_clk:
  841. clk_rate_exclusive_put(dsi->mod_clk);
  842. return ret;
  843. }
  844. static int sun6i_dsi_remove(struct platform_device *pdev)
  845. {
  846. struct device *dev = &pdev->dev;
  847. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  848. component_del(&pdev->dev, &sun6i_dsi_ops);
  849. mipi_dsi_host_unregister(&dsi->host);
  850. pm_runtime_disable(dev);
  851. sun6i_dphy_remove(dsi);
  852. clk_rate_exclusive_put(dsi->mod_clk);
  853. return 0;
  854. }
  855. static int __maybe_unused sun6i_dsi_runtime_resume(struct device *dev)
  856. {
  857. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  858. reset_control_deassert(dsi->reset);
  859. clk_prepare_enable(dsi->mod_clk);
  860. /*
  861. * Enable the DSI block.
  862. *
  863. * Some part of it can only be done once we get a number of
  864. * lanes, see sun6i_dsi_inst_init
  865. */
  866. regmap_write(dsi->regs, SUN6I_DSI_CTL_REG, SUN6I_DSI_CTL_EN);
  867. regmap_write(dsi->regs, SUN6I_DSI_BASIC_CTL0_REG,
  868. SUN6I_DSI_BASIC_CTL0_ECC_EN | SUN6I_DSI_BASIC_CTL0_CRC_EN);
  869. regmap_write(dsi->regs, SUN6I_DSI_TRANS_START_REG, 10);
  870. regmap_write(dsi->regs, SUN6I_DSI_TRANS_ZERO_REG, 0);
  871. if (dsi->device)
  872. sun6i_dsi_inst_init(dsi, dsi->device);
  873. regmap_write(dsi->regs, SUN6I_DSI_DEBUG_DATA_REG, 0xff);
  874. return 0;
  875. }
  876. static int __maybe_unused sun6i_dsi_runtime_suspend(struct device *dev)
  877. {
  878. struct sun6i_dsi *dsi = dev_get_drvdata(dev);
  879. clk_disable_unprepare(dsi->mod_clk);
  880. reset_control_assert(dsi->reset);
  881. return 0;
  882. }
  883. static const struct dev_pm_ops sun6i_dsi_pm_ops = {
  884. SET_RUNTIME_PM_OPS(sun6i_dsi_runtime_suspend,
  885. sun6i_dsi_runtime_resume,
  886. NULL)
  887. };
  888. static const struct of_device_id sun6i_dsi_of_table[] = {
  889. { .compatible = "allwinner,sun6i-a31-mipi-dsi" },
  890. { }
  891. };
  892. MODULE_DEVICE_TABLE(of, sun6i_dsi_of_table);
  893. static struct platform_driver sun6i_dsi_platform_driver = {
  894. .probe = sun6i_dsi_probe,
  895. .remove = sun6i_dsi_remove,
  896. .driver = {
  897. .name = "sun6i-mipi-dsi",
  898. .of_match_table = sun6i_dsi_of_table,
  899. .pm = &sun6i_dsi_pm_ops,
  900. },
  901. };
  902. module_platform_driver(sun6i_dsi_platform_driver);
  903. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  904. MODULE_DESCRIPTION("Allwinner A31 DSI Driver");
  905. MODULE_LICENSE("GPL");