kirin_drm_ade.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. struct drm_display_mode *mode = &crtc->state->mode;
  451. struct drm_display_mode *adj_mode = &crtc->state->adjusted_mode;
  452. if (!ctx->power_on)
  453. (void)ade_power_up(ctx);
  454. ade_ldi_set_mode(acrtc, mode, adj_mode);
  455. }
  456. static void ade_crtc_atomic_flush(struct drm_crtc *crtc,
  457. struct drm_crtc_state *old_state)
  458. {
  459. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  460. struct ade_hw_ctx *ctx = acrtc->ctx;
  461. struct drm_pending_vblank_event *event = crtc->state->event;
  462. void __iomem *base = ctx->base;
  463. /* only crtc is enabled regs take effect */
  464. if (acrtc->enable) {
  465. ade_dump_regs(base);
  466. /* flush ade registers */
  467. writel(ADE_ENABLE, base + ADE_EN);
  468. }
  469. if (event) {
  470. crtc->state->event = NULL;
  471. spin_lock_irq(&crtc->dev->event_lock);
  472. if (drm_crtc_vblank_get(crtc) == 0)
  473. drm_crtc_arm_vblank_event(crtc, event);
  474. else
  475. drm_crtc_send_vblank_event(crtc, event);
  476. spin_unlock_irq(&crtc->dev->event_lock);
  477. }
  478. }
  479. static const struct drm_crtc_helper_funcs ade_crtc_helper_funcs = {
  480. .mode_fixup = ade_crtc_mode_fixup,
  481. .mode_set_nofb = ade_crtc_mode_set_nofb,
  482. .atomic_begin = ade_crtc_atomic_begin,
  483. .atomic_flush = ade_crtc_atomic_flush,
  484. .atomic_enable = ade_crtc_atomic_enable,
  485. .atomic_disable = ade_crtc_atomic_disable,
  486. };
  487. static const struct drm_crtc_funcs ade_crtc_funcs = {
  488. .destroy = drm_crtc_cleanup,
  489. .set_config = drm_atomic_helper_set_config,
  490. .page_flip = drm_atomic_helper_page_flip,
  491. .reset = drm_atomic_helper_crtc_reset,
  492. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  493. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  494. .enable_vblank = ade_crtc_enable_vblank,
  495. .disable_vblank = ade_crtc_disable_vblank,
  496. };
  497. static int ade_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  498. struct drm_plane *plane)
  499. {
  500. struct device_node *port;
  501. int ret;
  502. /* set crtc port so that
  503. * drm_of_find_possible_crtcs call works
  504. */
  505. port = of_get_child_by_name(dev->dev->of_node, "port");
  506. if (!port) {
  507. DRM_ERROR("no port node found in %pOF\n", dev->dev->of_node);
  508. return -EINVAL;
  509. }
  510. of_node_put(port);
  511. crtc->port = port;
  512. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  513. &ade_crtc_funcs, NULL);
  514. if (ret) {
  515. DRM_ERROR("failed to init crtc.\n");
  516. return ret;
  517. }
  518. drm_crtc_helper_add(crtc, &ade_crtc_helper_funcs);
  519. return 0;
  520. }
  521. static void ade_rdma_set(void __iomem *base, struct drm_framebuffer *fb,
  522. u32 ch, u32 y, u32 in_h, u32 fmt)
  523. {
  524. struct drm_gem_cma_object *obj = drm_fb_cma_get_gem_obj(fb, 0);
  525. struct drm_format_name_buf format_name;
  526. u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
  527. u32 stride = fb->pitches[0];
  528. u32 addr = (u32)obj->paddr + y * stride;
  529. DRM_DEBUG_DRIVER("rdma%d: (y=%d, height=%d), stride=%d, paddr=0x%x\n",
  530. ch + 1, y, in_h, stride, (u32)obj->paddr);
  531. DRM_DEBUG_DRIVER("addr=0x%x, fb:%dx%d, pixel_format=%d(%s)\n",
  532. addr, fb->width, fb->height, fmt,
  533. drm_get_format_name(fb->format->format, &format_name));
  534. /* get reg offset */
  535. reg_ctrl = RD_CH_CTRL(ch);
  536. reg_addr = RD_CH_ADDR(ch);
  537. reg_size = RD_CH_SIZE(ch);
  538. reg_stride = RD_CH_STRIDE(ch);
  539. reg_space = RD_CH_SPACE(ch);
  540. reg_en = RD_CH_EN(ch);
  541. /*
  542. * TODO: set rotation
  543. */
  544. writel((fmt << 16) & 0x1f0000, base + reg_ctrl);
  545. writel(addr, base + reg_addr);
  546. writel((in_h << 16) | stride, base + reg_size);
  547. writel(stride, base + reg_stride);
  548. writel(in_h * stride, base + reg_space);
  549. writel(ADE_ENABLE, base + reg_en);
  550. ade_update_reload_bit(base, RDMA_OFST + ch, 0);
  551. }
  552. static void ade_rdma_disable(void __iomem *base, u32 ch)
  553. {
  554. u32 reg_en;
  555. /* get reg offset */
  556. reg_en = RD_CH_EN(ch);
  557. writel(0, base + reg_en);
  558. ade_update_reload_bit(base, RDMA_OFST + ch, 1);
  559. }
  560. static void ade_clip_set(void __iomem *base, u32 ch, u32 fb_w, u32 x,
  561. u32 in_w, u32 in_h)
  562. {
  563. u32 disable_val;
  564. u32 clip_left;
  565. u32 clip_right;
  566. /*
  567. * clip width, no need to clip height
  568. */
  569. if (fb_w == in_w) { /* bypass */
  570. disable_val = 1;
  571. clip_left = 0;
  572. clip_right = 0;
  573. } else {
  574. disable_val = 0;
  575. clip_left = x;
  576. clip_right = fb_w - (x + in_w) - 1;
  577. }
  578. DRM_DEBUG_DRIVER("clip%d: clip_left=%d, clip_right=%d\n",
  579. ch + 1, clip_left, clip_right);
  580. writel(disable_val, base + ADE_CLIP_DISABLE(ch));
  581. writel((fb_w - 1) << 16 | (in_h - 1), base + ADE_CLIP_SIZE0(ch));
  582. writel(clip_left << 16 | clip_right, base + ADE_CLIP_SIZE1(ch));
  583. ade_update_reload_bit(base, CLIP_OFST + ch, 0);
  584. }
  585. static void ade_clip_disable(void __iomem *base, u32 ch)
  586. {
  587. writel(1, base + ADE_CLIP_DISABLE(ch));
  588. ade_update_reload_bit(base, CLIP_OFST + ch, 1);
  589. }
  590. static bool has_Alpha_channel(int format)
  591. {
  592. switch (format) {
  593. case ADE_ARGB_8888:
  594. case ADE_ABGR_8888:
  595. case ADE_RGBA_8888:
  596. case ADE_BGRA_8888:
  597. return true;
  598. default:
  599. return false;
  600. }
  601. }
  602. static void ade_get_blending_params(u32 fmt, u8 glb_alpha, u8 *alp_mode,
  603. u8 *alp_sel, u8 *under_alp_sel)
  604. {
  605. bool has_alpha = has_Alpha_channel(fmt);
  606. /*
  607. * get alp_mode
  608. */
  609. if (has_alpha && glb_alpha < 255)
  610. *alp_mode = ADE_ALP_PIXEL_AND_GLB;
  611. else if (has_alpha)
  612. *alp_mode = ADE_ALP_PIXEL;
  613. else
  614. *alp_mode = ADE_ALP_GLOBAL;
  615. /*
  616. * get alp sel
  617. */
  618. *alp_sel = ADE_ALP_MUL_COEFF_3; /* 1 */
  619. *under_alp_sel = ADE_ALP_MUL_COEFF_2; /* 0 */
  620. }
  621. static void ade_compositor_routing_set(void __iomem *base, u8 ch,
  622. u32 x0, u32 y0,
  623. u32 in_w, u32 in_h, u32 fmt)
  624. {
  625. u8 ovly_ch = 0; /* TODO: This is the zpos, only one plane now */
  626. u8 glb_alpha = 255;
  627. u32 x1 = x0 + in_w - 1;
  628. u32 y1 = y0 + in_h - 1;
  629. u32 val;
  630. u8 alp_sel;
  631. u8 under_alp_sel;
  632. u8 alp_mode;
  633. ade_get_blending_params(fmt, glb_alpha, &alp_mode, &alp_sel,
  634. &under_alp_sel);
  635. /* overlay routing setting
  636. */
  637. writel(x0 << 16 | y0, base + ADE_OVLY_CH_XY0(ovly_ch));
  638. writel(x1 << 16 | y1, base + ADE_OVLY_CH_XY1(ovly_ch));
  639. val = (ch + 1) << CH_SEL_OFST | BIT(CH_EN_OFST) |
  640. alp_sel << CH_ALP_SEL_OFST |
  641. under_alp_sel << CH_UNDER_ALP_SEL_OFST |
  642. glb_alpha << CH_ALP_GBL_OFST |
  643. alp_mode << CH_ALP_MODE_OFST;
  644. writel(val, base + ADE_OVLY_CH_CTL(ovly_ch));
  645. /* connect this plane/channel to overlay2 compositor */
  646. ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
  647. CH_OVLY_SEL_MASK, CH_OVLY_SEL_VAL(OUT_OVLY));
  648. }
  649. static void ade_compositor_routing_disable(void __iomem *base, u32 ch)
  650. {
  651. u8 ovly_ch = 0; /* TODO: Only primary plane now */
  652. /* disable this plane/channel */
  653. ade_update_bits(base + ADE_OVLY_CH_CTL(ovly_ch), CH_EN_OFST,
  654. MASK(1), 0);
  655. /* dis-connect this plane/channel of overlay2 compositor */
  656. ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
  657. CH_OVLY_SEL_MASK, 0);
  658. }
  659. /*
  660. * Typicaly, a channel looks like: DMA-->clip-->scale-->ctrans-->compositor
  661. */
  662. static void ade_update_channel(struct ade_plane *aplane,
  663. struct drm_framebuffer *fb, int crtc_x,
  664. int crtc_y, unsigned int crtc_w,
  665. unsigned int crtc_h, u32 src_x,
  666. u32 src_y, u32 src_w, u32 src_h)
  667. {
  668. struct ade_hw_ctx *ctx = aplane->ctx;
  669. void __iomem *base = ctx->base;
  670. u32 fmt = ade_get_format(fb->format->format);
  671. u32 ch = aplane->ch;
  672. u32 in_w;
  673. u32 in_h;
  674. DRM_DEBUG_DRIVER("channel%d: src:(%d, %d)-%dx%d, crtc:(%d, %d)-%dx%d",
  675. ch + 1, src_x, src_y, src_w, src_h,
  676. crtc_x, crtc_y, crtc_w, crtc_h);
  677. /* 1) DMA setting */
  678. in_w = src_w;
  679. in_h = src_h;
  680. ade_rdma_set(base, fb, ch, src_y, in_h, fmt);
  681. /* 2) clip setting */
  682. ade_clip_set(base, ch, fb->width, src_x, in_w, in_h);
  683. /* 3) TODO: scale setting for overlay planes */
  684. /* 4) TODO: ctran/csc setting for overlay planes */
  685. /* 5) compositor routing setting */
  686. ade_compositor_routing_set(base, ch, crtc_x, crtc_y, in_w, in_h, fmt);
  687. }
  688. static void ade_disable_channel(struct ade_plane *aplane)
  689. {
  690. struct ade_hw_ctx *ctx = aplane->ctx;
  691. void __iomem *base = ctx->base;
  692. u32 ch = aplane->ch;
  693. DRM_DEBUG_DRIVER("disable channel%d\n", ch + 1);
  694. /* disable read DMA */
  695. ade_rdma_disable(base, ch);
  696. /* disable clip */
  697. ade_clip_disable(base, ch);
  698. /* disable compositor routing */
  699. ade_compositor_routing_disable(base, ch);
  700. }
  701. static int ade_plane_atomic_check(struct drm_plane *plane,
  702. struct drm_plane_state *state)
  703. {
  704. struct drm_framebuffer *fb = state->fb;
  705. struct drm_crtc *crtc = state->crtc;
  706. struct drm_crtc_state *crtc_state;
  707. u32 src_x = state->src_x >> 16;
  708. u32 src_y = state->src_y >> 16;
  709. u32 src_w = state->src_w >> 16;
  710. u32 src_h = state->src_h >> 16;
  711. int crtc_x = state->crtc_x;
  712. int crtc_y = state->crtc_y;
  713. u32 crtc_w = state->crtc_w;
  714. u32 crtc_h = state->crtc_h;
  715. u32 fmt;
  716. if (!crtc || !fb)
  717. return 0;
  718. fmt = ade_get_format(fb->format->format);
  719. if (fmt == ADE_FORMAT_UNSUPPORT)
  720. return -EINVAL;
  721. crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
  722. if (IS_ERR(crtc_state))
  723. return PTR_ERR(crtc_state);
  724. if (src_w != crtc_w || src_h != crtc_h) {
  725. return -EINVAL;
  726. }
  727. if (src_x + src_w > fb->width ||
  728. src_y + src_h > fb->height)
  729. return -EINVAL;
  730. if (crtc_x < 0 || crtc_y < 0)
  731. return -EINVAL;
  732. if (crtc_x + crtc_w > crtc_state->adjusted_mode.hdisplay ||
  733. crtc_y + crtc_h > crtc_state->adjusted_mode.vdisplay)
  734. return -EINVAL;
  735. return 0;
  736. }
  737. static void ade_plane_atomic_update(struct drm_plane *plane,
  738. struct drm_plane_state *old_state)
  739. {
  740. struct drm_plane_state *state = plane->state;
  741. struct ade_plane *aplane = to_ade_plane(plane);
  742. ade_update_channel(aplane, state->fb, state->crtc_x, state->crtc_y,
  743. state->crtc_w, state->crtc_h,
  744. state->src_x >> 16, state->src_y >> 16,
  745. state->src_w >> 16, state->src_h >> 16);
  746. }
  747. static void ade_plane_atomic_disable(struct drm_plane *plane,
  748. struct drm_plane_state *old_state)
  749. {
  750. struct ade_plane *aplane = to_ade_plane(plane);
  751. ade_disable_channel(aplane);
  752. }
  753. static const struct drm_plane_helper_funcs ade_plane_helper_funcs = {
  754. .atomic_check = ade_plane_atomic_check,
  755. .atomic_update = ade_plane_atomic_update,
  756. .atomic_disable = ade_plane_atomic_disable,
  757. };
  758. static struct drm_plane_funcs ade_plane_funcs = {
  759. .update_plane = drm_atomic_helper_update_plane,
  760. .disable_plane = drm_atomic_helper_disable_plane,
  761. .destroy = drm_plane_cleanup,
  762. .reset = drm_atomic_helper_plane_reset,
  763. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  764. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  765. };
  766. static int ade_plane_init(struct drm_device *dev, struct ade_plane *aplane,
  767. enum drm_plane_type type)
  768. {
  769. const u32 *fmts;
  770. u32 fmts_cnt;
  771. int ret = 0;
  772. /* get properties */
  773. fmts_cnt = ade_get_channel_formats(aplane->ch, &fmts);
  774. if (ret)
  775. return ret;
  776. ret = drm_universal_plane_init(dev, &aplane->base, 1, &ade_plane_funcs,
  777. fmts, fmts_cnt, NULL, type, NULL);
  778. if (ret) {
  779. DRM_ERROR("fail to init plane, ch=%d\n", aplane->ch);
  780. return ret;
  781. }
  782. drm_plane_helper_add(&aplane->base, &ade_plane_helper_funcs);
  783. return 0;
  784. }
  785. static int ade_dts_parse(struct platform_device *pdev, struct ade_hw_ctx *ctx)
  786. {
  787. struct resource *res;
  788. struct device *dev = &pdev->dev;
  789. struct device_node *np = pdev->dev.of_node;
  790. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  791. ctx->base = devm_ioremap_resource(dev, res);
  792. if (IS_ERR(ctx->base)) {
  793. DRM_ERROR("failed to remap ade io base\n");
  794. return PTR_ERR(ctx->base);
  795. }
  796. ctx->reset = devm_reset_control_get(dev, NULL);
  797. if (IS_ERR(ctx->reset))
  798. return PTR_ERR(ctx->reset);
  799. ctx->noc_regmap =
  800. syscon_regmap_lookup_by_phandle(np, "hisilicon,noc-syscon");
  801. if (IS_ERR(ctx->noc_regmap)) {
  802. DRM_ERROR("failed to get noc regmap\n");
  803. return PTR_ERR(ctx->noc_regmap);
  804. }
  805. ctx->irq = platform_get_irq(pdev, 0);
  806. if (ctx->irq < 0) {
  807. DRM_ERROR("failed to get irq\n");
  808. return -ENODEV;
  809. }
  810. ctx->ade_core_clk = devm_clk_get(dev, "clk_ade_core");
  811. if (IS_ERR(ctx->ade_core_clk)) {
  812. DRM_ERROR("failed to parse clk ADE_CORE\n");
  813. return PTR_ERR(ctx->ade_core_clk);
  814. }
  815. ctx->media_noc_clk = devm_clk_get(dev, "clk_codec_jpeg");
  816. if (IS_ERR(ctx->media_noc_clk)) {
  817. DRM_ERROR("failed to parse clk CODEC_JPEG\n");
  818. return PTR_ERR(ctx->media_noc_clk);
  819. }
  820. ctx->ade_pix_clk = devm_clk_get(dev, "clk_ade_pix");
  821. if (IS_ERR(ctx->ade_pix_clk)) {
  822. DRM_ERROR("failed to parse clk ADE_PIX\n");
  823. return PTR_ERR(ctx->ade_pix_clk);
  824. }
  825. return 0;
  826. }
  827. static int ade_drm_init(struct platform_device *pdev)
  828. {
  829. struct drm_device *dev = platform_get_drvdata(pdev);
  830. struct ade_data *ade;
  831. struct ade_hw_ctx *ctx;
  832. struct ade_crtc *acrtc;
  833. struct ade_plane *aplane;
  834. enum drm_plane_type type;
  835. int ret;
  836. int i;
  837. ade = devm_kzalloc(dev->dev, sizeof(*ade), GFP_KERNEL);
  838. if (!ade) {
  839. DRM_ERROR("failed to alloc ade_data\n");
  840. return -ENOMEM;
  841. }
  842. platform_set_drvdata(pdev, ade);
  843. ctx = &ade->ctx;
  844. acrtc = &ade->acrtc;
  845. acrtc->ctx = ctx;
  846. acrtc->out_format = LDI_OUT_RGB_888;
  847. ret = ade_dts_parse(pdev, ctx);
  848. if (ret)
  849. return ret;
  850. /*
  851. * plane init
  852. * TODO: Now only support primary plane, overlay planes
  853. * need to do.
  854. */
  855. for (i = 0; i < ADE_CH_NUM; i++) {
  856. aplane = &ade->aplane[i];
  857. aplane->ch = i;
  858. aplane->ctx = ctx;
  859. type = i == PRIMARY_CH ? DRM_PLANE_TYPE_PRIMARY :
  860. DRM_PLANE_TYPE_OVERLAY;
  861. ret = ade_plane_init(dev, aplane, type);
  862. if (ret)
  863. return ret;
  864. }
  865. /* crtc init */
  866. ret = ade_crtc_init(dev, &acrtc->base, &ade->aplane[PRIMARY_CH].base);
  867. if (ret)
  868. return ret;
  869. /* vblank irq init */
  870. ret = devm_request_irq(dev->dev, ctx->irq, ade_irq_handler,
  871. IRQF_SHARED, dev->driver->name, acrtc);
  872. if (ret)
  873. return ret;
  874. return 0;
  875. }
  876. static void ade_drm_cleanup(struct platform_device *pdev)
  877. {
  878. }
  879. const struct kirin_dc_ops ade_dc_ops = {
  880. .init = ade_drm_init,
  881. .cleanup = ade_drm_cleanup
  882. };