sprd_dsi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Unisoc Inc.
  4. */
  5. #include <linux/component.h>
  6. #include <linux/module.h>
  7. #include <linux/of.h>
  8. #include <linux/platform_device.h>
  9. #include <video/mipi_display.h>
  10. #include <drm/drm_atomic_helper.h>
  11. #include <drm/drm_bridge.h>
  12. #include <drm/drm_of.h>
  13. #include <drm/drm_probe_helper.h>
  14. #include "sprd_drm.h"
  15. #include "sprd_dpu.h"
  16. #include "sprd_dsi.h"
  17. #define SOFT_RESET 0x04
  18. #define MASK_PROTOCOL_INT 0x0C
  19. #define MASK_INTERNAL_INT 0x14
  20. #define DSI_MODE_CFG 0x18
  21. #define VIRTUAL_CHANNEL_ID 0x1C
  22. #define GEN_RX_VCID GENMASK(1, 0)
  23. #define VIDEO_PKT_VCID GENMASK(3, 2)
  24. #define DPI_VIDEO_FORMAT 0x20
  25. #define DPI_VIDEO_MODE_FORMAT GENMASK(5, 0)
  26. #define LOOSELY18_EN BIT(6)
  27. #define VIDEO_PKT_CONFIG 0x24
  28. #define VIDEO_PKT_SIZE GENMASK(15, 0)
  29. #define VIDEO_LINE_CHUNK_NUM GENMASK(31, 16)
  30. #define VIDEO_LINE_HBLK_TIME 0x28
  31. #define VIDEO_LINE_HBP_TIME GENMASK(15, 0)
  32. #define VIDEO_LINE_HSA_TIME GENMASK(31, 16)
  33. #define VIDEO_LINE_TIME 0x2C
  34. #define VIDEO_VBLK_LINES 0x30
  35. #define VFP_LINES GENMASK(9, 0)
  36. #define VBP_LINES GENMASK(19, 10)
  37. #define VSA_LINES GENMASK(29, 20)
  38. #define VIDEO_VACTIVE_LINES 0x34
  39. #define VID_MODE_CFG 0x38
  40. #define VID_MODE_TYPE GENMASK(1, 0)
  41. #define LP_VSA_EN BIT(8)
  42. #define LP_VBP_EN BIT(9)
  43. #define LP_VFP_EN BIT(10)
  44. #define LP_VACT_EN BIT(11)
  45. #define LP_HBP_EN BIT(12)
  46. #define LP_HFP_EN BIT(13)
  47. #define FRAME_BTA_ACK_EN BIT(14)
  48. #define TIMEOUT_CNT_CLK_CONFIG 0x40
  49. #define HTX_TO_CONFIG 0x44
  50. #define LRX_H_TO_CONFIG 0x48
  51. #define TX_ESC_CLK_CONFIG 0x5C
  52. #define CMD_MODE_CFG 0x68
  53. #define TEAR_FX_EN BIT(0)
  54. #define GEN_HDR 0x6C
  55. #define GEN_DT GENMASK(5, 0)
  56. #define GEN_VC GENMASK(7, 6)
  57. #define GEN_PLD_DATA 0x70
  58. #define PHY_CLK_LANE_LP_CTRL 0x74
  59. #define PHY_CLKLANE_TX_REQ_HS BIT(0)
  60. #define AUTO_CLKLANE_CTRL_EN BIT(1)
  61. #define PHY_INTERFACE_CTRL 0x78
  62. #define RF_PHY_SHUTDOWN BIT(0)
  63. #define RF_PHY_RESET_N BIT(1)
  64. #define RF_PHY_CLK_EN BIT(2)
  65. #define CMD_MODE_STATUS 0x98
  66. #define GEN_CMD_RDATA_FIFO_EMPTY BIT(1)
  67. #define GEN_CMD_WDATA_FIFO_EMPTY BIT(3)
  68. #define GEN_CMD_CMD_FIFO_EMPTY BIT(5)
  69. #define GEN_CMD_RDCMD_DONE BIT(7)
  70. #define PHY_STATUS 0x9C
  71. #define PHY_LOCK BIT(1)
  72. #define PHY_MIN_STOP_TIME 0xA0
  73. #define PHY_LANE_NUM_CONFIG 0xA4
  74. #define PHY_CLKLANE_TIME_CONFIG 0xA8
  75. #define PHY_CLKLANE_LP_TO_HS_TIME GENMASK(15, 0)
  76. #define PHY_CLKLANE_HS_TO_LP_TIME GENMASK(31, 16)
  77. #define PHY_DATALANE_TIME_CONFIG 0xAC
  78. #define PHY_DATALANE_LP_TO_HS_TIME GENMASK(15, 0)
  79. #define PHY_DATALANE_HS_TO_LP_TIME GENMASK(31, 16)
  80. #define MAX_READ_TIME 0xB0
  81. #define RX_PKT_CHECK_CONFIG 0xB4
  82. #define RX_PKT_ECC_EN BIT(0)
  83. #define RX_PKT_CRC_EN BIT(1)
  84. #define TA_EN 0xB8
  85. #define EOTP_EN 0xBC
  86. #define TX_EOTP_EN BIT(0)
  87. #define RX_EOTP_EN BIT(1)
  88. #define VIDEO_NULLPKT_SIZE 0xC0
  89. #define DCS_WM_PKT_SIZE 0xC4
  90. #define VIDEO_SIG_DELAY_CONFIG 0xD0
  91. #define VIDEO_SIG_DELAY GENMASK(23, 0)
  92. #define PHY_TST_CTRL0 0xF0
  93. #define PHY_TESTCLR BIT(0)
  94. #define PHY_TESTCLK BIT(1)
  95. #define PHY_TST_CTRL1 0xF4
  96. #define PHY_TESTDIN GENMASK(7, 0)
  97. #define PHY_TESTDOUT GENMASK(15, 8)
  98. #define PHY_TESTEN BIT(16)
  99. #define host_to_dsi(host) \
  100. container_of(host, struct sprd_dsi, host)
  101. static inline u32
  102. dsi_reg_rd(struct dsi_context *ctx, u32 offset, u32 mask,
  103. u32 shift)
  104. {
  105. return (readl(ctx->base + offset) & mask) >> shift;
  106. }
  107. static inline void
  108. dsi_reg_wr(struct dsi_context *ctx, u32 offset, u32 mask,
  109. u32 shift, u32 val)
  110. {
  111. u32 ret;
  112. ret = readl(ctx->base + offset);
  113. ret &= ~mask;
  114. ret |= (val << shift) & mask;
  115. writel(ret, ctx->base + offset);
  116. }
  117. static inline void
  118. dsi_reg_up(struct dsi_context *ctx, u32 offset, u32 mask,
  119. u32 val)
  120. {
  121. u32 ret = readl(ctx->base + offset);
  122. writel((ret & ~mask) | (val & mask), ctx->base + offset);
  123. }
  124. static int regmap_tst_io_write(void *context, u32 reg, u32 val)
  125. {
  126. struct sprd_dsi *dsi = context;
  127. struct dsi_context *ctx = &dsi->ctx;
  128. if (val > 0xff || reg > 0xff)
  129. return -EINVAL;
  130. drm_dbg(dsi->drm, "reg = 0x%02x, val = 0x%02x\n", reg, val);
  131. dsi_reg_up(ctx, PHY_TST_CTRL1, PHY_TESTEN, PHY_TESTEN);
  132. dsi_reg_wr(ctx, PHY_TST_CTRL1, PHY_TESTDIN, 0, reg);
  133. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, PHY_TESTCLK);
  134. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, 0);
  135. dsi_reg_up(ctx, PHY_TST_CTRL1, PHY_TESTEN, 0);
  136. dsi_reg_wr(ctx, PHY_TST_CTRL1, PHY_TESTDIN, 0, val);
  137. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, PHY_TESTCLK);
  138. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, 0);
  139. return 0;
  140. }
  141. static int regmap_tst_io_read(void *context, u32 reg, u32 *val)
  142. {
  143. struct sprd_dsi *dsi = context;
  144. struct dsi_context *ctx = &dsi->ctx;
  145. int ret;
  146. if (reg > 0xff)
  147. return -EINVAL;
  148. dsi_reg_up(ctx, PHY_TST_CTRL1, PHY_TESTEN, PHY_TESTEN);
  149. dsi_reg_wr(ctx, PHY_TST_CTRL1, PHY_TESTDIN, 0, reg);
  150. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, PHY_TESTCLK);
  151. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, 0);
  152. dsi_reg_up(ctx, PHY_TST_CTRL1, PHY_TESTEN, 0);
  153. udelay(1);
  154. ret = dsi_reg_rd(ctx, PHY_TST_CTRL1, PHY_TESTDOUT, 8);
  155. if (ret < 0)
  156. return ret;
  157. *val = ret;
  158. drm_dbg(dsi->drm, "reg = 0x%02x, val = 0x%02x\n", reg, *val);
  159. return 0;
  160. }
  161. static struct regmap_bus regmap_tst_io = {
  162. .reg_write = regmap_tst_io_write,
  163. .reg_read = regmap_tst_io_read,
  164. };
  165. static const struct regmap_config byte_config = {
  166. .reg_bits = 8,
  167. .val_bits = 8,
  168. };
  169. static int dphy_wait_pll_locked(struct dsi_context *ctx)
  170. {
  171. struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
  172. int i;
  173. for (i = 0; i < 50000; i++) {
  174. if (dsi_reg_rd(ctx, PHY_STATUS, PHY_LOCK, 1))
  175. return 0;
  176. udelay(3);
  177. }
  178. drm_err(dsi->drm, "dphy pll can not be locked\n");
  179. return -ETIMEDOUT;
  180. }
  181. static int dsi_wait_tx_payload_fifo_empty(struct dsi_context *ctx)
  182. {
  183. int i;
  184. for (i = 0; i < 5000; i++) {
  185. if (dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_WDATA_FIFO_EMPTY, 3))
  186. return 0;
  187. udelay(1);
  188. }
  189. return -ETIMEDOUT;
  190. }
  191. static int dsi_wait_tx_cmd_fifo_empty(struct dsi_context *ctx)
  192. {
  193. int i;
  194. for (i = 0; i < 5000; i++) {
  195. if (dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_CMD_FIFO_EMPTY, 5))
  196. return 0;
  197. udelay(1);
  198. }
  199. return -ETIMEDOUT;
  200. }
  201. static int dsi_wait_rd_resp_completed(struct dsi_context *ctx)
  202. {
  203. int i;
  204. for (i = 0; i < 10000; i++) {
  205. if (dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_RDCMD_DONE, 7))
  206. return 0;
  207. udelay(10);
  208. }
  209. return -ETIMEDOUT;
  210. }
  211. static u16 calc_bytes_per_pixel_x100(int coding)
  212. {
  213. u16 bpp_x100;
  214. switch (coding) {
  215. case COLOR_CODE_16BIT_CONFIG1:
  216. case COLOR_CODE_16BIT_CONFIG2:
  217. case COLOR_CODE_16BIT_CONFIG3:
  218. bpp_x100 = 200;
  219. break;
  220. case COLOR_CODE_18BIT_CONFIG1:
  221. case COLOR_CODE_18BIT_CONFIG2:
  222. bpp_x100 = 225;
  223. break;
  224. case COLOR_CODE_24BIT:
  225. bpp_x100 = 300;
  226. break;
  227. case COLOR_CODE_COMPRESSTION:
  228. bpp_x100 = 100;
  229. break;
  230. case COLOR_CODE_20BIT_YCC422_LOOSELY:
  231. bpp_x100 = 250;
  232. break;
  233. case COLOR_CODE_24BIT_YCC422:
  234. bpp_x100 = 300;
  235. break;
  236. case COLOR_CODE_16BIT_YCC422:
  237. bpp_x100 = 200;
  238. break;
  239. case COLOR_CODE_30BIT:
  240. bpp_x100 = 375;
  241. break;
  242. case COLOR_CODE_36BIT:
  243. bpp_x100 = 450;
  244. break;
  245. case COLOR_CODE_12BIT_YCC420:
  246. bpp_x100 = 150;
  247. break;
  248. default:
  249. DRM_ERROR("invalid color coding");
  250. bpp_x100 = 0;
  251. break;
  252. }
  253. return bpp_x100;
  254. }
  255. static u8 calc_video_size_step(int coding)
  256. {
  257. u8 video_size_step;
  258. switch (coding) {
  259. case COLOR_CODE_16BIT_CONFIG1:
  260. case COLOR_CODE_16BIT_CONFIG2:
  261. case COLOR_CODE_16BIT_CONFIG3:
  262. case COLOR_CODE_18BIT_CONFIG1:
  263. case COLOR_CODE_18BIT_CONFIG2:
  264. case COLOR_CODE_24BIT:
  265. case COLOR_CODE_COMPRESSTION:
  266. return video_size_step = 1;
  267. case COLOR_CODE_20BIT_YCC422_LOOSELY:
  268. case COLOR_CODE_24BIT_YCC422:
  269. case COLOR_CODE_16BIT_YCC422:
  270. case COLOR_CODE_30BIT:
  271. case COLOR_CODE_36BIT:
  272. case COLOR_CODE_12BIT_YCC420:
  273. return video_size_step = 2;
  274. default:
  275. DRM_ERROR("invalid color coding");
  276. return 0;
  277. }
  278. }
  279. static u16 round_video_size(int coding, u16 video_size)
  280. {
  281. switch (coding) {
  282. case COLOR_CODE_16BIT_YCC422:
  283. case COLOR_CODE_24BIT_YCC422:
  284. case COLOR_CODE_20BIT_YCC422_LOOSELY:
  285. case COLOR_CODE_12BIT_YCC420:
  286. /* round up active H pixels to a multiple of 2 */
  287. if ((video_size % 2) != 0)
  288. video_size += 1;
  289. break;
  290. default:
  291. break;
  292. }
  293. return video_size;
  294. }
  295. #define SPRD_MIPI_DSI_FMT_DSC 0xff
  296. static u32 fmt_to_coding(u32 fmt)
  297. {
  298. switch (fmt) {
  299. case MIPI_DSI_FMT_RGB565:
  300. return COLOR_CODE_16BIT_CONFIG1;
  301. case MIPI_DSI_FMT_RGB666:
  302. case MIPI_DSI_FMT_RGB666_PACKED:
  303. return COLOR_CODE_18BIT_CONFIG1;
  304. case MIPI_DSI_FMT_RGB888:
  305. return COLOR_CODE_24BIT;
  306. case SPRD_MIPI_DSI_FMT_DSC:
  307. return COLOR_CODE_COMPRESSTION;
  308. default:
  309. DRM_ERROR("Unsupported format (%d)\n", fmt);
  310. return COLOR_CODE_24BIT;
  311. }
  312. }
  313. #define ns_to_cycle(ns, byte_clk) \
  314. DIV_ROUND_UP((ns) * (byte_clk), 1000000)
  315. static void sprd_dsi_init(struct dsi_context *ctx)
  316. {
  317. struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
  318. u32 byte_clk = dsi->slave->hs_rate / 8;
  319. u16 data_hs2lp, data_lp2hs, clk_hs2lp, clk_lp2hs;
  320. u16 max_rd_time;
  321. int div;
  322. writel(0, ctx->base + SOFT_RESET);
  323. writel(0xffffffff, ctx->base + MASK_PROTOCOL_INT);
  324. writel(0xffffffff, ctx->base + MASK_INTERNAL_INT);
  325. writel(1, ctx->base + DSI_MODE_CFG);
  326. dsi_reg_up(ctx, EOTP_EN, RX_EOTP_EN, 0);
  327. dsi_reg_up(ctx, EOTP_EN, TX_EOTP_EN, 0);
  328. dsi_reg_up(ctx, RX_PKT_CHECK_CONFIG, RX_PKT_ECC_EN, RX_PKT_ECC_EN);
  329. dsi_reg_up(ctx, RX_PKT_CHECK_CONFIG, RX_PKT_CRC_EN, RX_PKT_CRC_EN);
  330. writel(1, ctx->base + TA_EN);
  331. dsi_reg_up(ctx, VIRTUAL_CHANNEL_ID, VIDEO_PKT_VCID, 0);
  332. dsi_reg_up(ctx, VIRTUAL_CHANNEL_ID, GEN_RX_VCID, 0);
  333. div = DIV_ROUND_UP(byte_clk, dsi->slave->lp_rate);
  334. writel(div, ctx->base + TX_ESC_CLK_CONFIG);
  335. max_rd_time = ns_to_cycle(ctx->max_rd_time, byte_clk);
  336. writel(max_rd_time, ctx->base + MAX_READ_TIME);
  337. data_hs2lp = ns_to_cycle(ctx->data_hs2lp, byte_clk);
  338. data_lp2hs = ns_to_cycle(ctx->data_lp2hs, byte_clk);
  339. clk_hs2lp = ns_to_cycle(ctx->clk_hs2lp, byte_clk);
  340. clk_lp2hs = ns_to_cycle(ctx->clk_lp2hs, byte_clk);
  341. dsi_reg_wr(ctx, PHY_DATALANE_TIME_CONFIG,
  342. PHY_DATALANE_HS_TO_LP_TIME, 16, data_hs2lp);
  343. dsi_reg_wr(ctx, PHY_DATALANE_TIME_CONFIG,
  344. PHY_DATALANE_LP_TO_HS_TIME, 0, data_lp2hs);
  345. dsi_reg_wr(ctx, PHY_CLKLANE_TIME_CONFIG,
  346. PHY_CLKLANE_HS_TO_LP_TIME, 16, clk_hs2lp);
  347. dsi_reg_wr(ctx, PHY_CLKLANE_TIME_CONFIG,
  348. PHY_CLKLANE_LP_TO_HS_TIME, 0, clk_lp2hs);
  349. writel(1, ctx->base + SOFT_RESET);
  350. }
  351. /*
  352. * Free up resources and shutdown host controller and PHY
  353. */
  354. static void sprd_dsi_fini(struct dsi_context *ctx)
  355. {
  356. writel(0xffffffff, ctx->base + MASK_PROTOCOL_INT);
  357. writel(0xffffffff, ctx->base + MASK_INTERNAL_INT);
  358. writel(0, ctx->base + SOFT_RESET);
  359. }
  360. /*
  361. * If not in burst mode, it will compute the video and null packet sizes
  362. * according to necessity.
  363. * Configure timers for data lanes and/or clock lane to return to LP when
  364. * bandwidth is not filled by data.
  365. */
  366. static int sprd_dsi_dpi_video(struct dsi_context *ctx)
  367. {
  368. struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
  369. struct videomode *vm = &ctx->vm;
  370. u32 byte_clk = dsi->slave->hs_rate / 8;
  371. u16 bpp_x100;
  372. u16 video_size;
  373. u32 ratio_x1000;
  374. u16 null_pkt_size = 0;
  375. u8 video_size_step;
  376. u32 hs_to;
  377. u32 total_bytes;
  378. u32 bytes_per_chunk;
  379. u32 chunks = 0;
  380. u32 bytes_left = 0;
  381. u32 chunk_overhead;
  382. const u8 pkt_header = 6;
  383. u8 coding;
  384. int div;
  385. u16 hline;
  386. u16 byte_cycle;
  387. coding = fmt_to_coding(dsi->slave->format);
  388. video_size = round_video_size(coding, vm->hactive);
  389. bpp_x100 = calc_bytes_per_pixel_x100(coding);
  390. video_size_step = calc_video_size_step(coding);
  391. ratio_x1000 = byte_clk * 1000 / (vm->pixelclock / 1000);
  392. hline = vm->hactive + vm->hsync_len + vm->hfront_porch +
  393. vm->hback_porch;
  394. writel(0, ctx->base + SOFT_RESET);
  395. dsi_reg_wr(ctx, VID_MODE_CFG, FRAME_BTA_ACK_EN, 15, ctx->frame_ack_en);
  396. dsi_reg_wr(ctx, DPI_VIDEO_FORMAT, DPI_VIDEO_MODE_FORMAT, 0, coding);
  397. dsi_reg_wr(ctx, VID_MODE_CFG, VID_MODE_TYPE, 0, ctx->burst_mode);
  398. byte_cycle = 95 * hline * ratio_x1000 / 100000;
  399. dsi_reg_wr(ctx, VIDEO_SIG_DELAY_CONFIG, VIDEO_SIG_DELAY, 0, byte_cycle);
  400. byte_cycle = hline * ratio_x1000 / 1000;
  401. writel(byte_cycle, ctx->base + VIDEO_LINE_TIME);
  402. byte_cycle = vm->hsync_len * ratio_x1000 / 1000;
  403. dsi_reg_wr(ctx, VIDEO_LINE_HBLK_TIME, VIDEO_LINE_HSA_TIME, 16, byte_cycle);
  404. byte_cycle = vm->hback_porch * ratio_x1000 / 1000;
  405. dsi_reg_wr(ctx, VIDEO_LINE_HBLK_TIME, VIDEO_LINE_HBP_TIME, 0, byte_cycle);
  406. writel(vm->vactive, ctx->base + VIDEO_VACTIVE_LINES);
  407. dsi_reg_wr(ctx, VIDEO_VBLK_LINES, VFP_LINES, 0, vm->vfront_porch);
  408. dsi_reg_wr(ctx, VIDEO_VBLK_LINES, VBP_LINES, 10, vm->vback_porch);
  409. dsi_reg_wr(ctx, VIDEO_VBLK_LINES, VSA_LINES, 20, vm->vsync_len);
  410. dsi_reg_up(ctx, VID_MODE_CFG, LP_HBP_EN | LP_HFP_EN | LP_VACT_EN |
  411. LP_VFP_EN | LP_VBP_EN | LP_VSA_EN, LP_HBP_EN | LP_HFP_EN |
  412. LP_VACT_EN | LP_VFP_EN | LP_VBP_EN | LP_VSA_EN);
  413. hs_to = (hline * vm->vactive) + (2 * bpp_x100) / 100;
  414. for (div = 0x80; (div < hs_to) && (div > 2); div--) {
  415. if ((hs_to % div) == 0) {
  416. writel(div, ctx->base + TIMEOUT_CNT_CLK_CONFIG);
  417. writel(hs_to / div, ctx->base + LRX_H_TO_CONFIG);
  418. writel(hs_to / div, ctx->base + HTX_TO_CONFIG);
  419. break;
  420. }
  421. }
  422. if (ctx->burst_mode == VIDEO_BURST_WITH_SYNC_PULSES) {
  423. dsi_reg_wr(ctx, VIDEO_PKT_CONFIG, VIDEO_PKT_SIZE, 0, video_size);
  424. writel(0, ctx->base + VIDEO_NULLPKT_SIZE);
  425. dsi_reg_up(ctx, VIDEO_PKT_CONFIG, VIDEO_LINE_CHUNK_NUM, 0);
  426. } else {
  427. /* non burst transmission */
  428. null_pkt_size = 0;
  429. /* bytes to be sent - first as one chunk */
  430. bytes_per_chunk = vm->hactive * bpp_x100 / 100 + pkt_header;
  431. /* hline total bytes from the DPI interface */
  432. total_bytes = (vm->hactive + vm->hfront_porch) *
  433. ratio_x1000 / dsi->slave->lanes / 1000;
  434. /* check if the pixels actually fit on the DSI link */
  435. if (total_bytes < bytes_per_chunk) {
  436. drm_err(dsi->drm, "current resolution can not be set\n");
  437. return -EINVAL;
  438. }
  439. chunk_overhead = total_bytes - bytes_per_chunk;
  440. /* overhead higher than 1 -> enable multi packets */
  441. if (chunk_overhead > 1) {
  442. /* multi packets */
  443. for (video_size = video_size_step;
  444. video_size < vm->hactive;
  445. video_size += video_size_step) {
  446. if (vm->hactive * 1000 / video_size % 1000)
  447. continue;
  448. chunks = vm->hactive / video_size;
  449. bytes_per_chunk = bpp_x100 * video_size / 100
  450. + pkt_header;
  451. if (total_bytes >= (bytes_per_chunk * chunks)) {
  452. bytes_left = total_bytes -
  453. bytes_per_chunk * chunks;
  454. break;
  455. }
  456. }
  457. /* prevent overflow (unsigned - unsigned) */
  458. if (bytes_left > (pkt_header * chunks)) {
  459. null_pkt_size = (bytes_left -
  460. pkt_header * chunks) / chunks;
  461. /* avoid register overflow */
  462. if (null_pkt_size > 1023)
  463. null_pkt_size = 1023;
  464. }
  465. } else {
  466. /* single packet */
  467. chunks = 1;
  468. /* must be a multiple of 4 except 18 loosely */
  469. for (video_size = vm->hactive;
  470. (video_size % video_size_step) != 0;
  471. video_size++)
  472. ;
  473. }
  474. dsi_reg_wr(ctx, VIDEO_PKT_CONFIG, VIDEO_PKT_SIZE, 0, video_size);
  475. writel(null_pkt_size, ctx->base + VIDEO_NULLPKT_SIZE);
  476. dsi_reg_wr(ctx, VIDEO_PKT_CONFIG, VIDEO_LINE_CHUNK_NUM, 16, chunks);
  477. }
  478. writel(ctx->int0_mask, ctx->base + MASK_PROTOCOL_INT);
  479. writel(ctx->int1_mask, ctx->base + MASK_INTERNAL_INT);
  480. writel(1, ctx->base + SOFT_RESET);
  481. return 0;
  482. }
  483. static void sprd_dsi_edpi_video(struct dsi_context *ctx)
  484. {
  485. struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
  486. const u32 fifo_depth = 1096;
  487. const u32 word_length = 4;
  488. u32 hactive = ctx->vm.hactive;
  489. u32 bpp_x100;
  490. u32 max_fifo_len;
  491. u8 coding;
  492. coding = fmt_to_coding(dsi->slave->format);
  493. bpp_x100 = calc_bytes_per_pixel_x100(coding);
  494. max_fifo_len = word_length * fifo_depth * 100 / bpp_x100;
  495. writel(0, ctx->base + SOFT_RESET);
  496. dsi_reg_wr(ctx, DPI_VIDEO_FORMAT, DPI_VIDEO_MODE_FORMAT, 0, coding);
  497. dsi_reg_wr(ctx, CMD_MODE_CFG, TEAR_FX_EN, 0, ctx->te_ack_en);
  498. if (max_fifo_len > hactive)
  499. writel(hactive, ctx->base + DCS_WM_PKT_SIZE);
  500. else
  501. writel(max_fifo_len, ctx->base + DCS_WM_PKT_SIZE);
  502. writel(ctx->int0_mask, ctx->base + MASK_PROTOCOL_INT);
  503. writel(ctx->int1_mask, ctx->base + MASK_INTERNAL_INT);
  504. writel(1, ctx->base + SOFT_RESET);
  505. }
  506. /*
  507. * Send a packet on the generic interface,
  508. * this function has an active delay to wait for the buffer to clear.
  509. * The delay is limited to:
  510. * (param_length / 4) x DSIH_FIFO_ACTIVE_WAIT x register access time
  511. * the controller restricts the sending of.
  512. *
  513. * This function will not be able to send Null and Blanking packets due to
  514. * controller restriction
  515. */
  516. static int sprd_dsi_wr_pkt(struct dsi_context *ctx, u8 vc, u8 type,
  517. const u8 *param, u16 len)
  518. {
  519. struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
  520. u8 wc_lsbyte, wc_msbyte;
  521. u32 payload;
  522. int i, j, ret;
  523. if (vc > 3)
  524. return -EINVAL;
  525. /* 1st: for long packet, must config payload first */
  526. ret = dsi_wait_tx_payload_fifo_empty(ctx);
  527. if (ret) {
  528. drm_err(dsi->drm, "tx payload fifo is not empty\n");
  529. return ret;
  530. }
  531. if (len > 2) {
  532. for (i = 0, j = 0; i < len; i += j) {
  533. payload = 0;
  534. for (j = 0; (j < 4) && ((j + i) < (len)); j++)
  535. payload |= param[i + j] << (j * 8);
  536. writel(payload, ctx->base + GEN_PLD_DATA);
  537. }
  538. wc_lsbyte = len & 0xff;
  539. wc_msbyte = len >> 8;
  540. } else {
  541. wc_lsbyte = (len > 0) ? param[0] : 0;
  542. wc_msbyte = (len > 1) ? param[1] : 0;
  543. }
  544. /* 2nd: then set packet header */
  545. ret = dsi_wait_tx_cmd_fifo_empty(ctx);
  546. if (ret) {
  547. drm_err(dsi->drm, "tx cmd fifo is not empty\n");
  548. return ret;
  549. }
  550. writel(type | (vc << 6) | (wc_lsbyte << 8) | (wc_msbyte << 16),
  551. ctx->base + GEN_HDR);
  552. return 0;
  553. }
  554. /*
  555. * Send READ packet to peripheral using the generic interface,
  556. * this will force command mode and stop video mode (because of BTA).
  557. *
  558. * This function has an active delay to wait for the buffer to clear,
  559. * the delay is limited to 2 x DSIH_FIFO_ACTIVE_WAIT
  560. * (waiting for command buffer, and waiting for receiving)
  561. * @note this function will enable BTA
  562. */
  563. static int sprd_dsi_rd_pkt(struct dsi_context *ctx, u8 vc, u8 type,
  564. u8 msb_byte, u8 lsb_byte,
  565. u8 *buffer, u8 bytes_to_read)
  566. {
  567. struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
  568. int i, ret;
  569. int count = 0;
  570. u32 temp;
  571. if (vc > 3)
  572. return -EINVAL;
  573. /* 1st: send read command to peripheral */
  574. ret = dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_CMD_FIFO_EMPTY, 5);
  575. if (!ret)
  576. return -EIO;
  577. writel(type | (vc << 6) | (lsb_byte << 8) | (msb_byte << 16),
  578. ctx->base + GEN_HDR);
  579. /* 2nd: wait peripheral response completed */
  580. ret = dsi_wait_rd_resp_completed(ctx);
  581. if (ret) {
  582. drm_err(dsi->drm, "wait read response time out\n");
  583. return ret;
  584. }
  585. /* 3rd: get data from rx payload fifo */
  586. ret = dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_RDATA_FIFO_EMPTY, 1);
  587. if (ret) {
  588. drm_err(dsi->drm, "rx payload fifo empty\n");
  589. return -EIO;
  590. }
  591. for (i = 0; i < 100; i++) {
  592. temp = readl(ctx->base + GEN_PLD_DATA);
  593. if (count < bytes_to_read)
  594. buffer[count++] = temp & 0xff;
  595. if (count < bytes_to_read)
  596. buffer[count++] = (temp >> 8) & 0xff;
  597. if (count < bytes_to_read)
  598. buffer[count++] = (temp >> 16) & 0xff;
  599. if (count < bytes_to_read)
  600. buffer[count++] = (temp >> 24) & 0xff;
  601. ret = dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_RDATA_FIFO_EMPTY, 1);
  602. if (ret)
  603. return count;
  604. }
  605. return 0;
  606. }
  607. static void sprd_dsi_set_work_mode(struct dsi_context *ctx, u8 mode)
  608. {
  609. if (mode == DSI_MODE_CMD)
  610. writel(1, ctx->base + DSI_MODE_CFG);
  611. else
  612. writel(0, ctx->base + DSI_MODE_CFG);
  613. }
  614. static void sprd_dsi_state_reset(struct dsi_context *ctx)
  615. {
  616. writel(0, ctx->base + SOFT_RESET);
  617. udelay(100);
  618. writel(1, ctx->base + SOFT_RESET);
  619. }
  620. static int sprd_dphy_init(struct dsi_context *ctx)
  621. {
  622. struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
  623. int ret;
  624. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_RESET_N, 0);
  625. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_SHUTDOWN, 0);
  626. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_CLK_EN, 0);
  627. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLR, 0);
  628. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLR, PHY_TESTCLR);
  629. dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLR, 0);
  630. dphy_pll_config(ctx);
  631. dphy_timing_config(ctx);
  632. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_SHUTDOWN, RF_PHY_SHUTDOWN);
  633. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_RESET_N, RF_PHY_RESET_N);
  634. writel(0x1C, ctx->base + PHY_MIN_STOP_TIME);
  635. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_CLK_EN, RF_PHY_CLK_EN);
  636. writel(dsi->slave->lanes - 1, ctx->base + PHY_LANE_NUM_CONFIG);
  637. ret = dphy_wait_pll_locked(ctx);
  638. if (ret) {
  639. drm_err(dsi->drm, "dphy initial failed\n");
  640. return ret;
  641. }
  642. return 0;
  643. }
  644. static void sprd_dphy_fini(struct dsi_context *ctx)
  645. {
  646. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_RESET_N, 0);
  647. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_SHUTDOWN, 0);
  648. dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_RESET_N, RF_PHY_RESET_N);
  649. }
  650. static void sprd_dsi_encoder_mode_set(struct drm_encoder *encoder,
  651. struct drm_display_mode *mode,
  652. struct drm_display_mode *adj_mode)
  653. {
  654. struct sprd_dsi *dsi = encoder_to_dsi(encoder);
  655. drm_display_mode_to_videomode(adj_mode, &dsi->ctx.vm);
  656. }
  657. static void sprd_dsi_encoder_enable(struct drm_encoder *encoder)
  658. {
  659. struct sprd_dsi *dsi = encoder_to_dsi(encoder);
  660. struct sprd_dpu *dpu = to_sprd_crtc(encoder->crtc);
  661. struct dsi_context *ctx = &dsi->ctx;
  662. if (ctx->enabled) {
  663. drm_warn(dsi->drm, "dsi is initialized\n");
  664. return;
  665. }
  666. sprd_dsi_init(ctx);
  667. if (ctx->work_mode == DSI_MODE_VIDEO)
  668. sprd_dsi_dpi_video(ctx);
  669. else
  670. sprd_dsi_edpi_video(ctx);
  671. sprd_dphy_init(ctx);
  672. sprd_dsi_set_work_mode(ctx, ctx->work_mode);
  673. sprd_dsi_state_reset(ctx);
  674. if (dsi->slave->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
  675. dsi_reg_up(ctx, PHY_CLK_LANE_LP_CTRL, AUTO_CLKLANE_CTRL_EN,
  676. AUTO_CLKLANE_CTRL_EN);
  677. } else {
  678. dsi_reg_up(ctx, PHY_CLK_LANE_LP_CTRL, RF_PHY_CLK_EN, RF_PHY_CLK_EN);
  679. dsi_reg_up(ctx, PHY_CLK_LANE_LP_CTRL, PHY_CLKLANE_TX_REQ_HS,
  680. PHY_CLKLANE_TX_REQ_HS);
  681. dphy_wait_pll_locked(ctx);
  682. }
  683. sprd_dpu_run(dpu);
  684. ctx->enabled = true;
  685. }
  686. static void sprd_dsi_encoder_disable(struct drm_encoder *encoder)
  687. {
  688. struct sprd_dsi *dsi = encoder_to_dsi(encoder);
  689. struct sprd_dpu *dpu = to_sprd_crtc(encoder->crtc);
  690. struct dsi_context *ctx = &dsi->ctx;
  691. if (!ctx->enabled) {
  692. drm_warn(dsi->drm, "dsi isn't initialized\n");
  693. return;
  694. }
  695. sprd_dpu_stop(dpu);
  696. sprd_dphy_fini(ctx);
  697. sprd_dsi_fini(ctx);
  698. ctx->enabled = false;
  699. }
  700. static const struct drm_encoder_helper_funcs sprd_encoder_helper_funcs = {
  701. .mode_set = sprd_dsi_encoder_mode_set,
  702. .enable = sprd_dsi_encoder_enable,
  703. .disable = sprd_dsi_encoder_disable
  704. };
  705. static const struct drm_encoder_funcs sprd_encoder_funcs = {
  706. .destroy = drm_encoder_cleanup,
  707. };
  708. static int sprd_dsi_encoder_init(struct sprd_dsi *dsi,
  709. struct device *dev)
  710. {
  711. struct drm_encoder *encoder = &dsi->encoder;
  712. u32 crtc_mask;
  713. int ret;
  714. crtc_mask = drm_of_find_possible_crtcs(dsi->drm, dev->of_node);
  715. if (!crtc_mask) {
  716. drm_err(dsi->drm, "failed to find crtc mask\n");
  717. return -EINVAL;
  718. }
  719. drm_dbg(dsi->drm, "find possible crtcs: 0x%08x\n", crtc_mask);
  720. encoder->possible_crtcs = crtc_mask;
  721. ret = drm_encoder_init(dsi->drm, encoder, &sprd_encoder_funcs,
  722. DRM_MODE_ENCODER_DSI, NULL);
  723. if (ret) {
  724. drm_err(dsi->drm, "failed to init dsi encoder\n");
  725. return ret;
  726. }
  727. drm_encoder_helper_add(encoder, &sprd_encoder_helper_funcs);
  728. return 0;
  729. }
  730. static int sprd_dsi_bridge_init(struct sprd_dsi *dsi,
  731. struct device *dev)
  732. {
  733. int ret;
  734. dsi->panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 1, 0);
  735. if (IS_ERR(dsi->panel_bridge))
  736. return PTR_ERR(dsi->panel_bridge);
  737. ret = drm_bridge_attach(&dsi->encoder, dsi->panel_bridge, NULL, 0);
  738. if (ret)
  739. return ret;
  740. return 0;
  741. }
  742. static int sprd_dsi_context_init(struct sprd_dsi *dsi,
  743. struct device *dev)
  744. {
  745. struct platform_device *pdev = to_platform_device(dev);
  746. struct dsi_context *ctx = &dsi->ctx;
  747. struct resource *res;
  748. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  749. if (!res) {
  750. dev_err(dev, "failed to get I/O resource\n");
  751. return -EINVAL;
  752. }
  753. ctx->base = devm_ioremap(dev, res->start, resource_size(res));
  754. if (!ctx->base) {
  755. drm_err(dsi->drm, "failed to map dsi host registers\n");
  756. return -ENXIO;
  757. }
  758. ctx->regmap = devm_regmap_init(dev, &regmap_tst_io, dsi, &byte_config);
  759. if (IS_ERR(ctx->regmap)) {
  760. drm_err(dsi->drm, "dphy regmap init failed\n");
  761. return PTR_ERR(ctx->regmap);
  762. }
  763. ctx->data_hs2lp = 120;
  764. ctx->data_lp2hs = 500;
  765. ctx->clk_hs2lp = 4;
  766. ctx->clk_lp2hs = 15;
  767. ctx->max_rd_time = 6000;
  768. ctx->int0_mask = 0xffffffff;
  769. ctx->int1_mask = 0xffffffff;
  770. ctx->enabled = true;
  771. return 0;
  772. }
  773. static int sprd_dsi_bind(struct device *dev, struct device *master, void *data)
  774. {
  775. struct drm_device *drm = data;
  776. struct sprd_dsi *dsi = dev_get_drvdata(dev);
  777. int ret;
  778. dsi->drm = drm;
  779. ret = sprd_dsi_encoder_init(dsi, dev);
  780. if (ret)
  781. return ret;
  782. ret = sprd_dsi_bridge_init(dsi, dev);
  783. if (ret)
  784. return ret;
  785. ret = sprd_dsi_context_init(dsi, dev);
  786. if (ret)
  787. return ret;
  788. return 0;
  789. }
  790. static void sprd_dsi_unbind(struct device *dev,
  791. struct device *master, void *data)
  792. {
  793. struct sprd_dsi *dsi = dev_get_drvdata(dev);
  794. drm_of_panel_bridge_remove(dev->of_node, 1, 0);
  795. drm_encoder_cleanup(&dsi->encoder);
  796. }
  797. static const struct component_ops dsi_component_ops = {
  798. .bind = sprd_dsi_bind,
  799. .unbind = sprd_dsi_unbind,
  800. };
  801. static int sprd_dsi_host_attach(struct mipi_dsi_host *host,
  802. struct mipi_dsi_device *slave)
  803. {
  804. struct sprd_dsi *dsi = host_to_dsi(host);
  805. struct dsi_context *ctx = &dsi->ctx;
  806. dsi->slave = slave;
  807. if (slave->mode_flags & MIPI_DSI_MODE_VIDEO)
  808. ctx->work_mode = DSI_MODE_VIDEO;
  809. else
  810. ctx->work_mode = DSI_MODE_CMD;
  811. if (slave->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  812. ctx->burst_mode = VIDEO_BURST_WITH_SYNC_PULSES;
  813. else if (slave->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  814. ctx->burst_mode = VIDEO_NON_BURST_WITH_SYNC_PULSES;
  815. else
  816. ctx->burst_mode = VIDEO_NON_BURST_WITH_SYNC_EVENTS;
  817. return component_add(host->dev, &dsi_component_ops);
  818. }
  819. static int sprd_dsi_host_detach(struct mipi_dsi_host *host,
  820. struct mipi_dsi_device *slave)
  821. {
  822. component_del(host->dev, &dsi_component_ops);
  823. return 0;
  824. }
  825. static ssize_t sprd_dsi_host_transfer(struct mipi_dsi_host *host,
  826. const struct mipi_dsi_msg *msg)
  827. {
  828. struct sprd_dsi *dsi = host_to_dsi(host);
  829. const u8 *tx_buf = msg->tx_buf;
  830. if (msg->rx_buf && msg->rx_len) {
  831. u8 lsb = (msg->tx_len > 0) ? tx_buf[0] : 0;
  832. u8 msb = (msg->tx_len > 1) ? tx_buf[1] : 0;
  833. return sprd_dsi_rd_pkt(&dsi->ctx, msg->channel, msg->type,
  834. msb, lsb, msg->rx_buf, msg->rx_len);
  835. }
  836. if (msg->tx_buf && msg->tx_len)
  837. return sprd_dsi_wr_pkt(&dsi->ctx, msg->channel, msg->type,
  838. tx_buf, msg->tx_len);
  839. return 0;
  840. }
  841. static const struct mipi_dsi_host_ops sprd_dsi_host_ops = {
  842. .attach = sprd_dsi_host_attach,
  843. .detach = sprd_dsi_host_detach,
  844. .transfer = sprd_dsi_host_transfer,
  845. };
  846. static const struct of_device_id dsi_match_table[] = {
  847. { .compatible = "sprd,sharkl3-dsi-host" },
  848. { /* sentinel */ },
  849. };
  850. static int sprd_dsi_probe(struct platform_device *pdev)
  851. {
  852. struct device *dev = &pdev->dev;
  853. struct sprd_dsi *dsi;
  854. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  855. if (!dsi)
  856. return -ENOMEM;
  857. dev_set_drvdata(dev, dsi);
  858. dsi->host.ops = &sprd_dsi_host_ops;
  859. dsi->host.dev = dev;
  860. return mipi_dsi_host_register(&dsi->host);
  861. }
  862. static void sprd_dsi_remove(struct platform_device *pdev)
  863. {
  864. struct sprd_dsi *dsi = dev_get_drvdata(&pdev->dev);
  865. mipi_dsi_host_unregister(&dsi->host);
  866. }
  867. struct platform_driver sprd_dsi_driver = {
  868. .probe = sprd_dsi_probe,
  869. .remove_new = sprd_dsi_remove,
  870. .driver = {
  871. .name = "sprd-dsi-drv",
  872. .of_match_table = dsi_match_table,
  873. },
  874. };
  875. MODULE_AUTHOR("Leon He <leon.he@unisoc.com>");
  876. MODULE_AUTHOR("Kevin Tang <kevin.tang@unisoc.com>");
  877. MODULE_DESCRIPTION("Unisoc MIPI DSI HOST Controller Driver");
  878. MODULE_LICENSE("GPL v2");