kirin_drm_ade.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * Hisilicon Hi6220 SoC ADE(Advanced Display Engine)'s crtc&plane driver
  3. *
  4. * Copyright (c) 2016 Linaro Limited.
  5. * Copyright (c) 2014-2016 Hisilicon Limited.
  6. *
  7. * Author:
  8. * Xinliang Liu <z.liuxinliang@hisilicon.com>
  9. * Xinliang Liu <xinliang.liu@linaro.org>
  10. * Xinwei Kong <kong.kongxinwei@hisilicon.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/clk.h>
  19. #include <video/display_timing.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/regmap.h>
  22. #include <linux/reset.h>
  23. #include <drm/drmP.h>
  24. #include <drm/drm_crtc.h>
  25. #include <drm/drm_crtc_helper.h>
  26. #include <drm/drm_atomic.h>
  27. #include <drm/drm_atomic_helper.h>
  28. #include <drm/drm_plane_helper.h>
  29. #include <drm/drm_gem_cma_helper.h>
  30. #include <drm/drm_fb_cma_helper.h>
  31. #include "kirin_drm_drv.h"
  32. #include "kirin_ade_reg.h"
  33. #define PRIMARY_CH ADE_CH1 /* primary plane */
  34. #define OUT_OVLY ADE_OVLY2 /* output overlay compositor */
  35. #define ADE_DEBUG 1
  36. #define to_ade_crtc(crtc) \
  37. container_of(crtc, struct ade_crtc, base)
  38. #define to_ade_plane(plane) \
  39. container_of(plane, struct ade_plane, base)
  40. struct ade_hw_ctx {
  41. void __iomem *base;
  42. struct regmap *noc_regmap;
  43. struct clk *ade_core_clk;
  44. struct clk *media_noc_clk;
  45. struct clk *ade_pix_clk;
  46. struct reset_control *reset;
  47. bool power_on;
  48. int irq;
  49. };
  50. struct ade_crtc {
  51. struct drm_crtc base;
  52. struct ade_hw_ctx *ctx;
  53. bool enable;
  54. u32 out_format;
  55. };
  56. struct ade_plane {
  57. struct drm_plane base;
  58. void *ctx;
  59. u8 ch; /* channel */
  60. };
  61. struct ade_data {
  62. struct ade_crtc acrtc;
  63. struct ade_plane aplane[ADE_CH_NUM];
  64. struct ade_hw_ctx ctx;
  65. };
  66. /* ade-format info: */
  67. struct ade_format {
  68. u32 pixel_format;
  69. enum ade_fb_format ade_format;
  70. };
  71. static const struct ade_format ade_formats[] = {
  72. /* 16bpp RGB: */
  73. { DRM_FORMAT_RGB565, ADE_RGB_565 },
  74. { DRM_FORMAT_BGR565, ADE_BGR_565 },
  75. /* 24bpp RGB: */
  76. { DRM_FORMAT_RGB888, ADE_RGB_888 },
  77. { DRM_FORMAT_BGR888, ADE_BGR_888 },
  78. /* 32bpp [A]RGB: */
  79. { DRM_FORMAT_XRGB8888, ADE_XRGB_8888 },
  80. { DRM_FORMAT_XBGR8888, ADE_XBGR_8888 },
  81. { DRM_FORMAT_RGBA8888, ADE_RGBA_8888 },
  82. { DRM_FORMAT_BGRA8888, ADE_BGRA_8888 },
  83. { DRM_FORMAT_ARGB8888, ADE_ARGB_8888 },
  84. { DRM_FORMAT_ABGR8888, ADE_ABGR_8888 },
  85. };
  86. static const u32 channel_formats1[] = {
  87. /* channel 1,2,3,4 */
  88. DRM_FORMAT_RGB565, DRM_FORMAT_BGR565, DRM_FORMAT_RGB888,
  89. DRM_FORMAT_BGR888, DRM_FORMAT_XRGB8888, DRM_FORMAT_XBGR8888,
  90. DRM_FORMAT_RGBA8888, DRM_FORMAT_BGRA8888, DRM_FORMAT_ARGB8888,
  91. DRM_FORMAT_ABGR8888
  92. };
  93. u32 ade_get_channel_formats(u8 ch, const u32 **formats)
  94. {
  95. switch (ch) {
  96. case ADE_CH1:
  97. *formats = channel_formats1;
  98. return ARRAY_SIZE(channel_formats1);
  99. default:
  100. DRM_ERROR("no this channel %d\n", ch);
  101. *formats = NULL;
  102. return 0;
  103. }
  104. }
  105. /* convert from fourcc format to ade format */
  106. static u32 ade_get_format(u32 pixel_format)
  107. {
  108. int i;
  109. for (i = 0; i < ARRAY_SIZE(ade_formats); i++)
  110. if (ade_formats[i].pixel_format == pixel_format)
  111. return ade_formats[i].ade_format;
  112. /* not found */
  113. DRM_ERROR("Not found pixel format!!fourcc_format= %d\n",
  114. pixel_format);
  115. return ADE_FORMAT_UNSUPPORT;
  116. }
  117. static void ade_update_reload_bit(void __iomem *base, u32 bit_num, u32 val)
  118. {
  119. u32 bit_ofst, reg_num;
  120. bit_ofst = bit_num % 32;
  121. reg_num = bit_num / 32;
  122. ade_update_bits(base + ADE_RELOAD_DIS(reg_num), bit_ofst,
  123. MASK(1), !!val);
  124. }
  125. static u32 ade_read_reload_bit(void __iomem *base, u32 bit_num)
  126. {
  127. u32 tmp, bit_ofst, reg_num;
  128. bit_ofst = bit_num % 32;
  129. reg_num = bit_num / 32;
  130. tmp = readl(base + ADE_RELOAD_DIS(reg_num));
  131. return !!(BIT(bit_ofst) & tmp);
  132. }
  133. static void ade_init(struct ade_hw_ctx *ctx)
  134. {
  135. void __iomem *base = ctx->base;
  136. /* enable clk gate */
  137. ade_update_bits(base + ADE_CTRL1, AUTO_CLK_GATE_EN_OFST,
  138. AUTO_CLK_GATE_EN, ADE_ENABLE);
  139. /* clear overlay */
  140. writel(0, base + ADE_OVLY1_TRANS_CFG);
  141. writel(0, base + ADE_OVLY_CTL);
  142. writel(0, base + ADE_OVLYX_CTL(OUT_OVLY));
  143. /* clear reset and reload regs */
  144. writel(MASK(32), base + ADE_SOFT_RST_SEL(0));
  145. writel(MASK(32), base + ADE_SOFT_RST_SEL(1));
  146. writel(MASK(32), base + ADE_RELOAD_DIS(0));
  147. writel(MASK(32), base + ADE_RELOAD_DIS(1));
  148. /*
  149. * for video mode, all the ade registers should
  150. * become effective at frame end.
  151. */
  152. ade_update_bits(base + ADE_CTRL, FRM_END_START_OFST,
  153. FRM_END_START_MASK, REG_EFFECTIVE_IN_ADEEN_FRMEND);
  154. }
  155. static bool ade_crtc_mode_fixup(struct drm_crtc *crtc,
  156. const struct drm_display_mode *mode,
  157. struct drm_display_mode *adjusted_mode)
  158. {
  159. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  160. struct ade_hw_ctx *ctx = acrtc->ctx;
  161. adjusted_mode->clock =
  162. clk_round_rate(ctx->ade_pix_clk, mode->clock * 1000) / 1000;
  163. return true;
  164. }
  165. static void ade_set_pix_clk(struct ade_hw_ctx *ctx,
  166. struct drm_display_mode *mode,
  167. struct drm_display_mode *adj_mode)
  168. {
  169. u32 clk_Hz = mode->clock * 1000;
  170. int ret;
  171. /*
  172. * Success should be guaranteed in mode_valid call back,
  173. * so failure shouldn't happen here
  174. */
  175. ret = clk_set_rate(ctx->ade_pix_clk, clk_Hz);
  176. if (ret)
  177. DRM_ERROR("failed to set pixel clk %dHz (%d)\n", clk_Hz, ret);
  178. adj_mode->clock = clk_get_rate(ctx->ade_pix_clk) / 1000;
  179. }
  180. static void ade_ldi_set_mode(struct ade_crtc *acrtc,
  181. struct drm_display_mode *mode,
  182. struct drm_display_mode *adj_mode)
  183. {
  184. struct ade_hw_ctx *ctx = acrtc->ctx;
  185. void __iomem *base = ctx->base;
  186. u32 width = mode->hdisplay;
  187. u32 height = mode->vdisplay;
  188. u32 hfp, hbp, hsw, vfp, vbp, vsw;
  189. u32 plr_flags;
  190. plr_flags = (mode->flags & DRM_MODE_FLAG_NVSYNC) ? FLAG_NVSYNC : 0;
  191. plr_flags |= (mode->flags & DRM_MODE_FLAG_NHSYNC) ? FLAG_NHSYNC : 0;
  192. hfp = mode->hsync_start - mode->hdisplay;
  193. hbp = mode->htotal - mode->hsync_end;
  194. hsw = mode->hsync_end - mode->hsync_start;
  195. vfp = mode->vsync_start - mode->vdisplay;
  196. vbp = mode->vtotal - mode->vsync_end;
  197. vsw = mode->vsync_end - mode->vsync_start;
  198. if (vsw > 15) {
  199. DRM_DEBUG_DRIVER("vsw exceeded 15\n");
  200. vsw = 15;
  201. }
  202. writel((hbp << HBP_OFST) | hfp, base + LDI_HRZ_CTRL0);
  203. /* the configured value is actual value - 1 */
  204. writel(hsw - 1, base + LDI_HRZ_CTRL1);
  205. writel((vbp << VBP_OFST) | vfp, base + LDI_VRT_CTRL0);
  206. /* the configured value is actual value - 1 */
  207. writel(vsw - 1, base + LDI_VRT_CTRL1);
  208. /* the configured value is actual value - 1 */
  209. writel(((height - 1) << VSIZE_OFST) | (width - 1),
  210. base + LDI_DSP_SIZE);
  211. writel(plr_flags, base + LDI_PLR_CTRL);
  212. /* set overlay compositor output size */
  213. writel(((width - 1) << OUTPUT_XSIZE_OFST) | (height - 1),
  214. base + ADE_OVLY_OUTPUT_SIZE(OUT_OVLY));
  215. /* ctran6 setting */
  216. writel(CTRAN_BYPASS_ON, base + ADE_CTRAN_DIS(ADE_CTRAN6));
  217. /* the configured value is actual value - 1 */
  218. writel(width * height - 1, base + ADE_CTRAN_IMAGE_SIZE(ADE_CTRAN6));
  219. ade_update_reload_bit(base, CTRAN_OFST + ADE_CTRAN6, 0);
  220. ade_set_pix_clk(ctx, mode, adj_mode);
  221. DRM_DEBUG_DRIVER("set mode: %dx%d\n", width, height);
  222. }
  223. static int ade_power_up(struct ade_hw_ctx *ctx)
  224. {
  225. int ret;
  226. ret = clk_prepare_enable(ctx->media_noc_clk);
  227. if (ret) {
  228. DRM_ERROR("failed to enable media_noc_clk (%d)\n", ret);
  229. return ret;
  230. }
  231. ret = reset_control_deassert(ctx->reset);
  232. if (ret) {
  233. DRM_ERROR("failed to deassert reset\n");
  234. return ret;
  235. }
  236. ret = clk_prepare_enable(ctx->ade_core_clk);
  237. if (ret) {
  238. DRM_ERROR("failed to enable ade_core_clk (%d)\n", ret);
  239. return ret;
  240. }
  241. ade_init(ctx);
  242. ctx->power_on = true;
  243. return 0;
  244. }
  245. static void ade_power_down(struct ade_hw_ctx *ctx)
  246. {
  247. void __iomem *base = ctx->base;
  248. writel(ADE_DISABLE, base + LDI_CTRL);
  249. /* dsi pixel off */
  250. writel(DSI_PCLK_OFF, base + LDI_HDMI_DSI_GT);
  251. clk_disable_unprepare(ctx->ade_core_clk);
  252. reset_control_assert(ctx->reset);
  253. clk_disable_unprepare(ctx->media_noc_clk);
  254. ctx->power_on = false;
  255. }
  256. static void ade_set_medianoc_qos(struct ade_crtc *acrtc)
  257. {
  258. struct ade_hw_ctx *ctx = acrtc->ctx;
  259. struct regmap *map = ctx->noc_regmap;
  260. regmap_update_bits(map, ADE0_QOSGENERATOR_MODE,
  261. QOSGENERATOR_MODE_MASK, BYPASS_MODE);
  262. regmap_update_bits(map, ADE0_QOSGENERATOR_EXTCONTROL,
  263. SOCKET_QOS_EN, SOCKET_QOS_EN);
  264. regmap_update_bits(map, ADE1_QOSGENERATOR_MODE,
  265. QOSGENERATOR_MODE_MASK, BYPASS_MODE);
  266. regmap_update_bits(map, ADE1_QOSGENERATOR_EXTCONTROL,
  267. SOCKET_QOS_EN, SOCKET_QOS_EN);
  268. }
  269. static int ade_crtc_enable_vblank(struct drm_crtc *crtc)
  270. {
  271. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  272. struct ade_hw_ctx *ctx = acrtc->ctx;
  273. void __iomem *base = ctx->base;
  274. if (!ctx->power_on)
  275. (void)ade_power_up(ctx);
  276. ade_update_bits(base + LDI_INT_EN, FRAME_END_INT_EN_OFST,
  277. MASK(1), 1);
  278. return 0;
  279. }
  280. static void ade_crtc_disable_vblank(struct drm_crtc *crtc)
  281. {
  282. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  283. struct ade_hw_ctx *ctx = acrtc->ctx;
  284. void __iomem *base = ctx->base;
  285. if (!ctx->power_on) {
  286. DRM_ERROR("power is down! vblank disable fail\n");
  287. return;
  288. }
  289. ade_update_bits(base + LDI_INT_EN, FRAME_END_INT_EN_OFST,
  290. MASK(1), 0);
  291. }
  292. static irqreturn_t ade_irq_handler(int irq, void *data)
  293. {
  294. struct ade_crtc *acrtc = data;
  295. struct ade_hw_ctx *ctx = acrtc->ctx;
  296. struct drm_crtc *crtc = &acrtc->base;
  297. void __iomem *base = ctx->base;
  298. u32 status;
  299. status = readl(base + LDI_MSK_INT);
  300. DRM_DEBUG_VBL("LDI IRQ: status=0x%X\n", status);
  301. /* vblank irq */
  302. if (status & BIT(FRAME_END_INT_EN_OFST)) {
  303. ade_update_bits(base + LDI_INT_CLR, FRAME_END_INT_EN_OFST,
  304. MASK(1), 1);
  305. drm_crtc_handle_vblank(crtc);
  306. }
  307. return IRQ_HANDLED;
  308. }
  309. static void ade_display_enable(struct ade_crtc *acrtc)
  310. {
  311. struct ade_hw_ctx *ctx = acrtc->ctx;
  312. void __iomem *base = ctx->base;
  313. u32 out_fmt = acrtc->out_format;
  314. /* enable output overlay compositor */
  315. writel(ADE_ENABLE, base + ADE_OVLYX_CTL(OUT_OVLY));
  316. ade_update_reload_bit(base, OVLY_OFST + OUT_OVLY, 0);
  317. /* display source setting */
  318. writel(DISP_SRC_OVLY2, base + ADE_DISP_SRC_CFG);
  319. /* enable ade */
  320. writel(ADE_ENABLE, base + ADE_EN);
  321. /* enable ldi */
  322. writel(NORMAL_MODE, base + LDI_WORK_MODE);
  323. writel((out_fmt << BPP_OFST) | DATA_GATE_EN | LDI_EN,
  324. base + LDI_CTRL);
  325. /* dsi pixel on */
  326. writel(DSI_PCLK_ON, base + LDI_HDMI_DSI_GT);
  327. }
  328. #if ADE_DEBUG
  329. static void ade_rdma_dump_regs(void __iomem *base, u32 ch)
  330. {
  331. u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
  332. u32 val;
  333. reg_ctrl = RD_CH_CTRL(ch);
  334. reg_addr = RD_CH_ADDR(ch);
  335. reg_size = RD_CH_SIZE(ch);
  336. reg_stride = RD_CH_STRIDE(ch);
  337. reg_space = RD_CH_SPACE(ch);
  338. reg_en = RD_CH_EN(ch);
  339. val = ade_read_reload_bit(base, RDMA_OFST + ch);
  340. DRM_DEBUG_DRIVER("[rdma%d]: reload(%d)\n", ch + 1, val);
  341. val = readl(base + reg_ctrl);
  342. DRM_DEBUG_DRIVER("[rdma%d]: reg_ctrl(0x%08x)\n", ch + 1, val);
  343. val = readl(base + reg_addr);
  344. DRM_DEBUG_DRIVER("[rdma%d]: reg_addr(0x%08x)\n", ch + 1, val);
  345. val = readl(base + reg_size);
  346. DRM_DEBUG_DRIVER("[rdma%d]: reg_size(0x%08x)\n", ch + 1, val);
  347. val = readl(base + reg_stride);
  348. DRM_DEBUG_DRIVER("[rdma%d]: reg_stride(0x%08x)\n", ch + 1, val);
  349. val = readl(base + reg_space);
  350. DRM_DEBUG_DRIVER("[rdma%d]: reg_space(0x%08x)\n", ch + 1, val);
  351. val = readl(base + reg_en);
  352. DRM_DEBUG_DRIVER("[rdma%d]: reg_en(0x%08x)\n", ch + 1, val);
  353. }
  354. static void ade_clip_dump_regs(void __iomem *base, u32 ch)
  355. {
  356. u32 val;
  357. val = ade_read_reload_bit(base, CLIP_OFST + ch);
  358. DRM_DEBUG_DRIVER("[clip%d]: reload(%d)\n", ch + 1, val);
  359. val = readl(base + ADE_CLIP_DISABLE(ch));
  360. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_disable(0x%08x)\n", ch + 1, val);
  361. val = readl(base + ADE_CLIP_SIZE0(ch));
  362. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_size0(0x%08x)\n", ch + 1, val);
  363. val = readl(base + ADE_CLIP_SIZE1(ch));
  364. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_size1(0x%08x)\n", ch + 1, val);
  365. }
  366. static void ade_compositor_routing_dump_regs(void __iomem *base, u32 ch)
  367. {
  368. u8 ovly_ch = 0; /* TODO: Only primary plane now */
  369. u32 val;
  370. val = readl(base + ADE_OVLY_CH_XY0(ovly_ch));
  371. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_xy0(0x%08x)\n", ovly_ch, val);
  372. val = readl(base + ADE_OVLY_CH_XY1(ovly_ch));
  373. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_xy1(0x%08x)\n", ovly_ch, val);
  374. val = readl(base + ADE_OVLY_CH_CTL(ovly_ch));
  375. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_ctl(0x%08x)\n", ovly_ch, val);
  376. }
  377. static void ade_dump_overlay_compositor_regs(void __iomem *base, u32 comp)
  378. {
  379. u32 val;
  380. val = ade_read_reload_bit(base, OVLY_OFST + comp);
  381. DRM_DEBUG_DRIVER("[overlay%d]: reload(%d)\n", comp + 1, val);
  382. writel(ADE_ENABLE, base + ADE_OVLYX_CTL(comp));
  383. DRM_DEBUG_DRIVER("[overlay%d]: reg_ctl(0x%08x)\n", comp + 1, val);
  384. val = readl(base + ADE_OVLY_CTL);
  385. DRM_DEBUG_DRIVER("ovly_ctl(0x%08x)\n", val);
  386. }
  387. static void ade_dump_regs(void __iomem *base)
  388. {
  389. u32 i;
  390. /* dump channel regs */
  391. for (i = 0; i < ADE_CH_NUM; i++) {
  392. /* dump rdma regs */
  393. ade_rdma_dump_regs(base, i);
  394. /* dump clip regs */
  395. ade_clip_dump_regs(base, i);
  396. /* dump compositor routing regs */
  397. ade_compositor_routing_dump_regs(base, i);
  398. }
  399. /* dump overlay compositor regs */
  400. ade_dump_overlay_compositor_regs(base, OUT_OVLY);
  401. }
  402. #else
  403. static void ade_dump_regs(void __iomem *base) { }
  404. #endif
  405. static void ade_crtc_atomic_enable(struct drm_crtc *crtc,
  406. struct drm_crtc_state *old_state)
  407. {
  408. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  409. struct ade_hw_ctx *ctx = acrtc->ctx;
  410. int ret;
  411. if (acrtc->enable)
  412. return;
  413. if (!ctx->power_on) {
  414. ret = ade_power_up(ctx);
  415. if (ret)
  416. return;
  417. }
  418. ade_set_medianoc_qos(acrtc);
  419. ade_display_enable(acrtc);
  420. ade_dump_regs(ctx->base);
  421. drm_crtc_vblank_on(crtc);
  422. acrtc->enable = true;
  423. }
  424. static void ade_crtc_atomic_disable(struct drm_crtc *crtc,
  425. struct drm_crtc_state *old_state)
  426. {
  427. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  428. struct ade_hw_ctx *ctx = acrtc->ctx;
  429. if (!acrtc->enable)
  430. return;
  431. drm_crtc_vblank_off(crtc);
  432. ade_power_down(ctx);
  433. acrtc->enable = false;
  434. }
  435. static void ade_crtc_mode_set_nofb(struct drm_crtc *crtc)
  436. {
  437. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  438. struct ade_hw_ctx *ctx = acrtc->ctx;
  439. struct drm_display_mode *mode = &crtc->state->mode;
  440. struct drm_display_mode *adj_mode = &crtc->state->adjusted_mode;
  441. if (!ctx->power_on)
  442. (void)ade_power_up(ctx);
  443. ade_ldi_set_mode(acrtc, mode, adj_mode);
  444. }
  445. static void ade_crtc_atomic_begin(struct drm_crtc *crtc,
  446. struct drm_crtc_state *old_state)
  447. {
  448. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  449. struct ade_hw_ctx *ctx = acrtc->ctx;
  450. if (!ctx->power_on)
  451. (void)ade_power_up(ctx);
  452. }
  453. static void ade_crtc_atomic_flush(struct drm_crtc *crtc,
  454. struct drm_crtc_state *old_state)
  455. {
  456. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  457. struct ade_hw_ctx *ctx = acrtc->ctx;
  458. struct drm_pending_vblank_event *event = crtc->state->event;
  459. void __iomem *base = ctx->base;
  460. /* only crtc is enabled regs take effect */
  461. if (acrtc->enable) {
  462. ade_dump_regs(base);
  463. /* flush ade registers */
  464. writel(ADE_ENABLE, base + ADE_EN);
  465. }
  466. if (event) {
  467. crtc->state->event = NULL;
  468. spin_lock_irq(&crtc->dev->event_lock);
  469. if (drm_crtc_vblank_get(crtc) == 0)
  470. drm_crtc_arm_vblank_event(crtc, event);
  471. else
  472. drm_crtc_send_vblank_event(crtc, event);
  473. spin_unlock_irq(&crtc->dev->event_lock);
  474. }
  475. }
  476. static const struct drm_crtc_helper_funcs ade_crtc_helper_funcs = {
  477. .mode_fixup = ade_crtc_mode_fixup,
  478. .mode_set_nofb = ade_crtc_mode_set_nofb,
  479. .atomic_begin = ade_crtc_atomic_begin,
  480. .atomic_flush = ade_crtc_atomic_flush,
  481. .atomic_enable = ade_crtc_atomic_enable,
  482. .atomic_disable = ade_crtc_atomic_disable,
  483. };
  484. static const struct drm_crtc_funcs ade_crtc_funcs = {
  485. .destroy = drm_crtc_cleanup,
  486. .set_config = drm_atomic_helper_set_config,
  487. .page_flip = drm_atomic_helper_page_flip,
  488. .reset = drm_atomic_helper_crtc_reset,
  489. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  490. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  491. .enable_vblank = ade_crtc_enable_vblank,
  492. .disable_vblank = ade_crtc_disable_vblank,
  493. };
  494. static int ade_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  495. struct drm_plane *plane)
  496. {
  497. struct device_node *port;
  498. int ret;
  499. /* set crtc port so that
  500. * drm_of_find_possible_crtcs call works
  501. */
  502. port = of_get_child_by_name(dev->dev->of_node, "port");
  503. if (!port) {
  504. DRM_ERROR("no port node found in %pOF\n", dev->dev->of_node);
  505. return -EINVAL;
  506. }
  507. of_node_put(port);
  508. crtc->port = port;
  509. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  510. &ade_crtc_funcs, NULL);
  511. if (ret) {
  512. DRM_ERROR("failed to init crtc.\n");
  513. return ret;
  514. }
  515. drm_crtc_helper_add(crtc, &ade_crtc_helper_funcs);
  516. return 0;
  517. }
  518. static void ade_rdma_set(void __iomem *base, struct drm_framebuffer *fb,
  519. u32 ch, u32 y, u32 in_h, u32 fmt)
  520. {
  521. struct drm_gem_cma_object *obj = drm_fb_cma_get_gem_obj(fb, 0);
  522. struct drm_format_name_buf format_name;
  523. u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
  524. u32 stride = fb->pitches[0];
  525. u32 addr = (u32)obj->paddr + y * stride;
  526. DRM_DEBUG_DRIVER("rdma%d: (y=%d, height=%d), stride=%d, paddr=0x%x\n",
  527. ch + 1, y, in_h, stride, (u32)obj->paddr);
  528. DRM_DEBUG_DRIVER("addr=0x%x, fb:%dx%d, pixel_format=%d(%s)\n",
  529. addr, fb->width, fb->height, fmt,
  530. drm_get_format_name(fb->format->format, &format_name));
  531. /* get reg offset */
  532. reg_ctrl = RD_CH_CTRL(ch);
  533. reg_addr = RD_CH_ADDR(ch);
  534. reg_size = RD_CH_SIZE(ch);
  535. reg_stride = RD_CH_STRIDE(ch);
  536. reg_space = RD_CH_SPACE(ch);
  537. reg_en = RD_CH_EN(ch);
  538. /*
  539. * TODO: set rotation
  540. */
  541. writel((fmt << 16) & 0x1f0000, base + reg_ctrl);
  542. writel(addr, base + reg_addr);
  543. writel((in_h << 16) | stride, base + reg_size);
  544. writel(stride, base + reg_stride);
  545. writel(in_h * stride, base + reg_space);
  546. writel(ADE_ENABLE, base + reg_en);
  547. ade_update_reload_bit(base, RDMA_OFST + ch, 0);
  548. }
  549. static void ade_rdma_disable(void __iomem *base, u32 ch)
  550. {
  551. u32 reg_en;
  552. /* get reg offset */
  553. reg_en = RD_CH_EN(ch);
  554. writel(0, base + reg_en);
  555. ade_update_reload_bit(base, RDMA_OFST + ch, 1);
  556. }
  557. static void ade_clip_set(void __iomem *base, u32 ch, u32 fb_w, u32 x,
  558. u32 in_w, u32 in_h)
  559. {
  560. u32 disable_val;
  561. u32 clip_left;
  562. u32 clip_right;
  563. /*
  564. * clip width, no need to clip height
  565. */
  566. if (fb_w == in_w) { /* bypass */
  567. disable_val = 1;
  568. clip_left = 0;
  569. clip_right = 0;
  570. } else {
  571. disable_val = 0;
  572. clip_left = x;
  573. clip_right = fb_w - (x + in_w) - 1;
  574. }
  575. DRM_DEBUG_DRIVER("clip%d: clip_left=%d, clip_right=%d\n",
  576. ch + 1, clip_left, clip_right);
  577. writel(disable_val, base + ADE_CLIP_DISABLE(ch));
  578. writel((fb_w - 1) << 16 | (in_h - 1), base + ADE_CLIP_SIZE0(ch));
  579. writel(clip_left << 16 | clip_right, base + ADE_CLIP_SIZE1(ch));
  580. ade_update_reload_bit(base, CLIP_OFST + ch, 0);
  581. }
  582. static void ade_clip_disable(void __iomem *base, u32 ch)
  583. {
  584. writel(1, base + ADE_CLIP_DISABLE(ch));
  585. ade_update_reload_bit(base, CLIP_OFST + ch, 1);
  586. }
  587. static bool has_Alpha_channel(int format)
  588. {
  589. switch (format) {
  590. case ADE_ARGB_8888:
  591. case ADE_ABGR_8888:
  592. case ADE_RGBA_8888:
  593. case ADE_BGRA_8888:
  594. return true;
  595. default:
  596. return false;
  597. }
  598. }
  599. static void ade_get_blending_params(u32 fmt, u8 glb_alpha, u8 *alp_mode,
  600. u8 *alp_sel, u8 *under_alp_sel)
  601. {
  602. bool has_alpha = has_Alpha_channel(fmt);
  603. /*
  604. * get alp_mode
  605. */
  606. if (has_alpha && glb_alpha < 255)
  607. *alp_mode = ADE_ALP_PIXEL_AND_GLB;
  608. else if (has_alpha)
  609. *alp_mode = ADE_ALP_PIXEL;
  610. else
  611. *alp_mode = ADE_ALP_GLOBAL;
  612. /*
  613. * get alp sel
  614. */
  615. *alp_sel = ADE_ALP_MUL_COEFF_3; /* 1 */
  616. *under_alp_sel = ADE_ALP_MUL_COEFF_2; /* 0 */
  617. }
  618. static void ade_compositor_routing_set(void __iomem *base, u8 ch,
  619. u32 x0, u32 y0,
  620. u32 in_w, u32 in_h, u32 fmt)
  621. {
  622. u8 ovly_ch = 0; /* TODO: This is the zpos, only one plane now */
  623. u8 glb_alpha = 255;
  624. u32 x1 = x0 + in_w - 1;
  625. u32 y1 = y0 + in_h - 1;
  626. u32 val;
  627. u8 alp_sel;
  628. u8 under_alp_sel;
  629. u8 alp_mode;
  630. ade_get_blending_params(fmt, glb_alpha, &alp_mode, &alp_sel,
  631. &under_alp_sel);
  632. /* overlay routing setting
  633. */
  634. writel(x0 << 16 | y0, base + ADE_OVLY_CH_XY0(ovly_ch));
  635. writel(x1 << 16 | y1, base + ADE_OVLY_CH_XY1(ovly_ch));
  636. val = (ch + 1) << CH_SEL_OFST | BIT(CH_EN_OFST) |
  637. alp_sel << CH_ALP_SEL_OFST |
  638. under_alp_sel << CH_UNDER_ALP_SEL_OFST |
  639. glb_alpha << CH_ALP_GBL_OFST |
  640. alp_mode << CH_ALP_MODE_OFST;
  641. writel(val, base + ADE_OVLY_CH_CTL(ovly_ch));
  642. /* connect this plane/channel to overlay2 compositor */
  643. ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
  644. CH_OVLY_SEL_MASK, CH_OVLY_SEL_VAL(OUT_OVLY));
  645. }
  646. static void ade_compositor_routing_disable(void __iomem *base, u32 ch)
  647. {
  648. u8 ovly_ch = 0; /* TODO: Only primary plane now */
  649. /* disable this plane/channel */
  650. ade_update_bits(base + ADE_OVLY_CH_CTL(ovly_ch), CH_EN_OFST,
  651. MASK(1), 0);
  652. /* dis-connect this plane/channel of overlay2 compositor */
  653. ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
  654. CH_OVLY_SEL_MASK, 0);
  655. }
  656. /*
  657. * Typicaly, a channel looks like: DMA-->clip-->scale-->ctrans-->compositor
  658. */
  659. static void ade_update_channel(struct ade_plane *aplane,
  660. struct drm_framebuffer *fb, int crtc_x,
  661. int crtc_y, unsigned int crtc_w,
  662. unsigned int crtc_h, u32 src_x,
  663. u32 src_y, u32 src_w, u32 src_h)
  664. {
  665. struct ade_hw_ctx *ctx = aplane->ctx;
  666. void __iomem *base = ctx->base;
  667. u32 fmt = ade_get_format(fb->format->format);
  668. u32 ch = aplane->ch;
  669. u32 in_w;
  670. u32 in_h;
  671. DRM_DEBUG_DRIVER("channel%d: src:(%d, %d)-%dx%d, crtc:(%d, %d)-%dx%d",
  672. ch + 1, src_x, src_y, src_w, src_h,
  673. crtc_x, crtc_y, crtc_w, crtc_h);
  674. /* 1) DMA setting */
  675. in_w = src_w;
  676. in_h = src_h;
  677. ade_rdma_set(base, fb, ch, src_y, in_h, fmt);
  678. /* 2) clip setting */
  679. ade_clip_set(base, ch, fb->width, src_x, in_w, in_h);
  680. /* 3) TODO: scale setting for overlay planes */
  681. /* 4) TODO: ctran/csc setting for overlay planes */
  682. /* 5) compositor routing setting */
  683. ade_compositor_routing_set(base, ch, crtc_x, crtc_y, in_w, in_h, fmt);
  684. }
  685. static void ade_disable_channel(struct ade_plane *aplane)
  686. {
  687. struct ade_hw_ctx *ctx = aplane->ctx;
  688. void __iomem *base = ctx->base;
  689. u32 ch = aplane->ch;
  690. DRM_DEBUG_DRIVER("disable channel%d\n", ch + 1);
  691. /* disable read DMA */
  692. ade_rdma_disable(base, ch);
  693. /* disable clip */
  694. ade_clip_disable(base, ch);
  695. /* disable compositor routing */
  696. ade_compositor_routing_disable(base, ch);
  697. }
  698. static int ade_plane_atomic_check(struct drm_plane *plane,
  699. struct drm_plane_state *state)
  700. {
  701. struct drm_framebuffer *fb = state->fb;
  702. struct drm_crtc *crtc = state->crtc;
  703. struct drm_crtc_state *crtc_state;
  704. u32 src_x = state->src_x >> 16;
  705. u32 src_y = state->src_y >> 16;
  706. u32 src_w = state->src_w >> 16;
  707. u32 src_h = state->src_h >> 16;
  708. int crtc_x = state->crtc_x;
  709. int crtc_y = state->crtc_y;
  710. u32 crtc_w = state->crtc_w;
  711. u32 crtc_h = state->crtc_h;
  712. u32 fmt;
  713. if (!crtc || !fb)
  714. return 0;
  715. fmt = ade_get_format(fb->format->format);
  716. if (fmt == ADE_FORMAT_UNSUPPORT)
  717. return -EINVAL;
  718. crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
  719. if (IS_ERR(crtc_state))
  720. return PTR_ERR(crtc_state);
  721. if (src_w != crtc_w || src_h != crtc_h) {
  722. DRM_ERROR("Scale not support!!!\n");
  723. return -EINVAL;
  724. }
  725. if (src_x + src_w > fb->width ||
  726. src_y + src_h > fb->height)
  727. return -EINVAL;
  728. if (crtc_x < 0 || crtc_y < 0)
  729. return -EINVAL;
  730. if (crtc_x + crtc_w > crtc_state->adjusted_mode.hdisplay ||
  731. crtc_y + crtc_h > crtc_state->adjusted_mode.vdisplay)
  732. return -EINVAL;
  733. return 0;
  734. }
  735. static void ade_plane_atomic_update(struct drm_plane *plane,
  736. struct drm_plane_state *old_state)
  737. {
  738. struct drm_plane_state *state = plane->state;
  739. struct ade_plane *aplane = to_ade_plane(plane);
  740. ade_update_channel(aplane, state->fb, state->crtc_x, state->crtc_y,
  741. state->crtc_w, state->crtc_h,
  742. state->src_x >> 16, state->src_y >> 16,
  743. state->src_w >> 16, state->src_h >> 16);
  744. }
  745. static void ade_plane_atomic_disable(struct drm_plane *plane,
  746. struct drm_plane_state *old_state)
  747. {
  748. struct ade_plane *aplane = to_ade_plane(plane);
  749. ade_disable_channel(aplane);
  750. }
  751. static const struct drm_plane_helper_funcs ade_plane_helper_funcs = {
  752. .atomic_check = ade_plane_atomic_check,
  753. .atomic_update = ade_plane_atomic_update,
  754. .atomic_disable = ade_plane_atomic_disable,
  755. };
  756. static struct drm_plane_funcs ade_plane_funcs = {
  757. .update_plane = drm_atomic_helper_update_plane,
  758. .disable_plane = drm_atomic_helper_disable_plane,
  759. .destroy = drm_plane_cleanup,
  760. .reset = drm_atomic_helper_plane_reset,
  761. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  762. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  763. };
  764. static int ade_plane_init(struct drm_device *dev, struct ade_plane *aplane,
  765. enum drm_plane_type type)
  766. {
  767. const u32 *fmts;
  768. u32 fmts_cnt;
  769. int ret = 0;
  770. /* get properties */
  771. fmts_cnt = ade_get_channel_formats(aplane->ch, &fmts);
  772. if (ret)
  773. return ret;
  774. ret = drm_universal_plane_init(dev, &aplane->base, 1, &ade_plane_funcs,
  775. fmts, fmts_cnt, NULL, type, NULL);
  776. if (ret) {
  777. DRM_ERROR("fail to init plane, ch=%d\n", aplane->ch);
  778. return ret;
  779. }
  780. drm_plane_helper_add(&aplane->base, &ade_plane_helper_funcs);
  781. return 0;
  782. }
  783. static int ade_dts_parse(struct platform_device *pdev, struct ade_hw_ctx *ctx)
  784. {
  785. struct resource *res;
  786. struct device *dev = &pdev->dev;
  787. struct device_node *np = pdev->dev.of_node;
  788. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  789. ctx->base = devm_ioremap_resource(dev, res);
  790. if (IS_ERR(ctx->base)) {
  791. DRM_ERROR("failed to remap ade io base\n");
  792. return PTR_ERR(ctx->base);
  793. }
  794. ctx->reset = devm_reset_control_get(dev, NULL);
  795. if (IS_ERR(ctx->reset))
  796. return PTR_ERR(ctx->reset);
  797. ctx->noc_regmap =
  798. syscon_regmap_lookup_by_phandle(np, "hisilicon,noc-syscon");
  799. if (IS_ERR(ctx->noc_regmap)) {
  800. DRM_ERROR("failed to get noc regmap\n");
  801. return PTR_ERR(ctx->noc_regmap);
  802. }
  803. ctx->irq = platform_get_irq(pdev, 0);
  804. if (ctx->irq < 0) {
  805. DRM_ERROR("failed to get irq\n");
  806. return -ENODEV;
  807. }
  808. ctx->ade_core_clk = devm_clk_get(dev, "clk_ade_core");
  809. if (IS_ERR(ctx->ade_core_clk)) {
  810. DRM_ERROR("failed to parse clk ADE_CORE\n");
  811. return PTR_ERR(ctx->ade_core_clk);
  812. }
  813. ctx->media_noc_clk = devm_clk_get(dev, "clk_codec_jpeg");
  814. if (IS_ERR(ctx->media_noc_clk)) {
  815. DRM_ERROR("failed to parse clk CODEC_JPEG\n");
  816. return PTR_ERR(ctx->media_noc_clk);
  817. }
  818. ctx->ade_pix_clk = devm_clk_get(dev, "clk_ade_pix");
  819. if (IS_ERR(ctx->ade_pix_clk)) {
  820. DRM_ERROR("failed to parse clk ADE_PIX\n");
  821. return PTR_ERR(ctx->ade_pix_clk);
  822. }
  823. return 0;
  824. }
  825. static int ade_drm_init(struct platform_device *pdev)
  826. {
  827. struct drm_device *dev = platform_get_drvdata(pdev);
  828. struct ade_data *ade;
  829. struct ade_hw_ctx *ctx;
  830. struct ade_crtc *acrtc;
  831. struct ade_plane *aplane;
  832. enum drm_plane_type type;
  833. int ret;
  834. int i;
  835. ade = devm_kzalloc(dev->dev, sizeof(*ade), GFP_KERNEL);
  836. if (!ade) {
  837. DRM_ERROR("failed to alloc ade_data\n");
  838. return -ENOMEM;
  839. }
  840. platform_set_drvdata(pdev, ade);
  841. ctx = &ade->ctx;
  842. acrtc = &ade->acrtc;
  843. acrtc->ctx = ctx;
  844. acrtc->out_format = LDI_OUT_RGB_888;
  845. ret = ade_dts_parse(pdev, ctx);
  846. if (ret)
  847. return ret;
  848. /*
  849. * plane init
  850. * TODO: Now only support primary plane, overlay planes
  851. * need to do.
  852. */
  853. for (i = 0; i < ADE_CH_NUM; i++) {
  854. aplane = &ade->aplane[i];
  855. aplane->ch = i;
  856. aplane->ctx = ctx;
  857. type = i == PRIMARY_CH ? DRM_PLANE_TYPE_PRIMARY :
  858. DRM_PLANE_TYPE_OVERLAY;
  859. ret = ade_plane_init(dev, aplane, type);
  860. if (ret)
  861. return ret;
  862. }
  863. /* crtc init */
  864. ret = ade_crtc_init(dev, &acrtc->base, &ade->aplane[PRIMARY_CH].base);
  865. if (ret)
  866. return ret;
  867. /* vblank irq init */
  868. ret = devm_request_irq(dev->dev, ctx->irq, ade_irq_handler,
  869. IRQF_SHARED, dev->driver->name, acrtc);
  870. if (ret)
  871. return ret;
  872. return 0;
  873. }
  874. static void ade_drm_cleanup(struct platform_device *pdev)
  875. {
  876. }
  877. const struct kirin_dc_ops ade_dc_ops = {
  878. .init = ade_drm_init,
  879. .cleanup = ade_drm_cleanup
  880. };