kirin_drm_ade.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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 void ade_set_pix_clk(struct ade_hw_ctx *ctx,
  156. struct drm_display_mode *mode,
  157. struct drm_display_mode *adj_mode)
  158. {
  159. u32 clk_Hz = mode->clock * 1000;
  160. int ret;
  161. /*
  162. * Success should be guaranteed in mode_valid call back,
  163. * so failure shouldn't happen here
  164. */
  165. ret = clk_set_rate(ctx->ade_pix_clk, clk_Hz);
  166. if (ret)
  167. DRM_ERROR("failed to set pixel clk %dHz (%d)\n", clk_Hz, ret);
  168. adj_mode->clock = clk_get_rate(ctx->ade_pix_clk) / 1000;
  169. }
  170. static void ade_ldi_set_mode(struct ade_crtc *acrtc,
  171. struct drm_display_mode *mode,
  172. struct drm_display_mode *adj_mode)
  173. {
  174. struct ade_hw_ctx *ctx = acrtc->ctx;
  175. void __iomem *base = ctx->base;
  176. u32 width = mode->hdisplay;
  177. u32 height = mode->vdisplay;
  178. u32 hfp, hbp, hsw, vfp, vbp, vsw;
  179. u32 plr_flags;
  180. plr_flags = (mode->flags & DRM_MODE_FLAG_NVSYNC) ? FLAG_NVSYNC : 0;
  181. plr_flags |= (mode->flags & DRM_MODE_FLAG_NHSYNC) ? FLAG_NHSYNC : 0;
  182. hfp = mode->hsync_start - mode->hdisplay;
  183. hbp = mode->htotal - mode->hsync_end;
  184. hsw = mode->hsync_end - mode->hsync_start;
  185. vfp = mode->vsync_start - mode->vdisplay;
  186. vbp = mode->vtotal - mode->vsync_end;
  187. vsw = mode->vsync_end - mode->vsync_start;
  188. if (vsw > 15) {
  189. DRM_DEBUG_DRIVER("vsw exceeded 15\n");
  190. vsw = 15;
  191. }
  192. writel((hbp << HBP_OFST) | hfp, base + LDI_HRZ_CTRL0);
  193. /* the configured value is actual value - 1 */
  194. writel(hsw - 1, base + LDI_HRZ_CTRL1);
  195. writel((vbp << VBP_OFST) | vfp, base + LDI_VRT_CTRL0);
  196. /* the configured value is actual value - 1 */
  197. writel(vsw - 1, base + LDI_VRT_CTRL1);
  198. /* the configured value is actual value - 1 */
  199. writel(((height - 1) << VSIZE_OFST) | (width - 1),
  200. base + LDI_DSP_SIZE);
  201. writel(plr_flags, base + LDI_PLR_CTRL);
  202. /* set overlay compositor output size */
  203. writel(((width - 1) << OUTPUT_XSIZE_OFST) | (height - 1),
  204. base + ADE_OVLY_OUTPUT_SIZE(OUT_OVLY));
  205. /* ctran6 setting */
  206. writel(CTRAN_BYPASS_ON, base + ADE_CTRAN_DIS(ADE_CTRAN6));
  207. /* the configured value is actual value - 1 */
  208. writel(width * height - 1, base + ADE_CTRAN_IMAGE_SIZE(ADE_CTRAN6));
  209. ade_update_reload_bit(base, CTRAN_OFST + ADE_CTRAN6, 0);
  210. ade_set_pix_clk(ctx, mode, adj_mode);
  211. DRM_DEBUG_DRIVER("set mode: %dx%d\n", width, height);
  212. }
  213. static int ade_power_up(struct ade_hw_ctx *ctx)
  214. {
  215. int ret;
  216. ret = clk_prepare_enable(ctx->media_noc_clk);
  217. if (ret) {
  218. DRM_ERROR("failed to enable media_noc_clk (%d)\n", ret);
  219. return ret;
  220. }
  221. ret = reset_control_deassert(ctx->reset);
  222. if (ret) {
  223. DRM_ERROR("failed to deassert reset\n");
  224. return ret;
  225. }
  226. ret = clk_prepare_enable(ctx->ade_core_clk);
  227. if (ret) {
  228. DRM_ERROR("failed to enable ade_core_clk (%d)\n", ret);
  229. return ret;
  230. }
  231. ade_init(ctx);
  232. ctx->power_on = true;
  233. return 0;
  234. }
  235. static void ade_power_down(struct ade_hw_ctx *ctx)
  236. {
  237. void __iomem *base = ctx->base;
  238. writel(ADE_DISABLE, base + LDI_CTRL);
  239. /* dsi pixel off */
  240. writel(DSI_PCLK_OFF, base + LDI_HDMI_DSI_GT);
  241. clk_disable_unprepare(ctx->ade_core_clk);
  242. reset_control_assert(ctx->reset);
  243. clk_disable_unprepare(ctx->media_noc_clk);
  244. ctx->power_on = false;
  245. }
  246. static void ade_set_medianoc_qos(struct ade_crtc *acrtc)
  247. {
  248. struct ade_hw_ctx *ctx = acrtc->ctx;
  249. struct regmap *map = ctx->noc_regmap;
  250. regmap_update_bits(map, ADE0_QOSGENERATOR_MODE,
  251. QOSGENERATOR_MODE_MASK, BYPASS_MODE);
  252. regmap_update_bits(map, ADE0_QOSGENERATOR_EXTCONTROL,
  253. SOCKET_QOS_EN, SOCKET_QOS_EN);
  254. regmap_update_bits(map, ADE1_QOSGENERATOR_MODE,
  255. QOSGENERATOR_MODE_MASK, BYPASS_MODE);
  256. regmap_update_bits(map, ADE1_QOSGENERATOR_EXTCONTROL,
  257. SOCKET_QOS_EN, SOCKET_QOS_EN);
  258. }
  259. static int ade_enable_vblank(struct drm_device *dev, unsigned int pipe)
  260. {
  261. struct kirin_drm_private *priv = dev->dev_private;
  262. struct ade_crtc *acrtc = to_ade_crtc(priv->crtc[pipe]);
  263. struct ade_hw_ctx *ctx = acrtc->ctx;
  264. void __iomem *base = ctx->base;
  265. if (!ctx->power_on)
  266. (void)ade_power_up(ctx);
  267. ade_update_bits(base + LDI_INT_EN, FRAME_END_INT_EN_OFST,
  268. MASK(1), 1);
  269. return 0;
  270. }
  271. static void ade_disable_vblank(struct drm_device *dev, unsigned int pipe)
  272. {
  273. struct kirin_drm_private *priv = dev->dev_private;
  274. struct ade_crtc *acrtc = to_ade_crtc(priv->crtc[pipe]);
  275. struct ade_hw_ctx *ctx = acrtc->ctx;
  276. void __iomem *base = ctx->base;
  277. if (!ctx->power_on) {
  278. DRM_ERROR("power is down! vblank disable fail\n");
  279. return;
  280. }
  281. ade_update_bits(base + LDI_INT_EN, FRAME_END_INT_EN_OFST,
  282. MASK(1), 0);
  283. }
  284. static irqreturn_t ade_irq_handler(int irq, void *data)
  285. {
  286. struct ade_crtc *acrtc = data;
  287. struct ade_hw_ctx *ctx = acrtc->ctx;
  288. struct drm_crtc *crtc = &acrtc->base;
  289. void __iomem *base = ctx->base;
  290. u32 status;
  291. status = readl(base + LDI_MSK_INT);
  292. DRM_DEBUG_VBL("LDI IRQ: status=0x%X\n", status);
  293. /* vblank irq */
  294. if (status & BIT(FRAME_END_INT_EN_OFST)) {
  295. ade_update_bits(base + LDI_INT_CLR, FRAME_END_INT_EN_OFST,
  296. MASK(1), 1);
  297. drm_crtc_handle_vblank(crtc);
  298. }
  299. return IRQ_HANDLED;
  300. }
  301. static void ade_display_enable(struct ade_crtc *acrtc)
  302. {
  303. struct ade_hw_ctx *ctx = acrtc->ctx;
  304. void __iomem *base = ctx->base;
  305. u32 out_fmt = acrtc->out_format;
  306. /* enable output overlay compositor */
  307. writel(ADE_ENABLE, base + ADE_OVLYX_CTL(OUT_OVLY));
  308. ade_update_reload_bit(base, OVLY_OFST + OUT_OVLY, 0);
  309. /* display source setting */
  310. writel(DISP_SRC_OVLY2, base + ADE_DISP_SRC_CFG);
  311. /* enable ade */
  312. writel(ADE_ENABLE, base + ADE_EN);
  313. /* enable ldi */
  314. writel(NORMAL_MODE, base + LDI_WORK_MODE);
  315. writel((out_fmt << BPP_OFST) | DATA_GATE_EN | LDI_EN,
  316. base + LDI_CTRL);
  317. /* dsi pixel on */
  318. writel(DSI_PCLK_ON, base + LDI_HDMI_DSI_GT);
  319. }
  320. #if ADE_DEBUG
  321. static void ade_rdma_dump_regs(void __iomem *base, u32 ch)
  322. {
  323. u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
  324. u32 val;
  325. reg_ctrl = RD_CH_CTRL(ch);
  326. reg_addr = RD_CH_ADDR(ch);
  327. reg_size = RD_CH_SIZE(ch);
  328. reg_stride = RD_CH_STRIDE(ch);
  329. reg_space = RD_CH_SPACE(ch);
  330. reg_en = RD_CH_EN(ch);
  331. val = ade_read_reload_bit(base, RDMA_OFST + ch);
  332. DRM_DEBUG_DRIVER("[rdma%d]: reload(%d)\n", ch + 1, val);
  333. val = readl(base + reg_ctrl);
  334. DRM_DEBUG_DRIVER("[rdma%d]: reg_ctrl(0x%08x)\n", ch + 1, val);
  335. val = readl(base + reg_addr);
  336. DRM_DEBUG_DRIVER("[rdma%d]: reg_addr(0x%08x)\n", ch + 1, val);
  337. val = readl(base + reg_size);
  338. DRM_DEBUG_DRIVER("[rdma%d]: reg_size(0x%08x)\n", ch + 1, val);
  339. val = readl(base + reg_stride);
  340. DRM_DEBUG_DRIVER("[rdma%d]: reg_stride(0x%08x)\n", ch + 1, val);
  341. val = readl(base + reg_space);
  342. DRM_DEBUG_DRIVER("[rdma%d]: reg_space(0x%08x)\n", ch + 1, val);
  343. val = readl(base + reg_en);
  344. DRM_DEBUG_DRIVER("[rdma%d]: reg_en(0x%08x)\n", ch + 1, val);
  345. }
  346. static void ade_clip_dump_regs(void __iomem *base, u32 ch)
  347. {
  348. u32 val;
  349. val = ade_read_reload_bit(base, CLIP_OFST + ch);
  350. DRM_DEBUG_DRIVER("[clip%d]: reload(%d)\n", ch + 1, val);
  351. val = readl(base + ADE_CLIP_DISABLE(ch));
  352. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_disable(0x%08x)\n", ch + 1, val);
  353. val = readl(base + ADE_CLIP_SIZE0(ch));
  354. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_size0(0x%08x)\n", ch + 1, val);
  355. val = readl(base + ADE_CLIP_SIZE1(ch));
  356. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_size1(0x%08x)\n", ch + 1, val);
  357. }
  358. static void ade_compositor_routing_dump_regs(void __iomem *base, u32 ch)
  359. {
  360. u8 ovly_ch = 0; /* TODO: Only primary plane now */
  361. u32 val;
  362. val = readl(base + ADE_OVLY_CH_XY0(ovly_ch));
  363. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_xy0(0x%08x)\n", ovly_ch, val);
  364. val = readl(base + ADE_OVLY_CH_XY1(ovly_ch));
  365. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_xy1(0x%08x)\n", ovly_ch, val);
  366. val = readl(base + ADE_OVLY_CH_CTL(ovly_ch));
  367. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_ctl(0x%08x)\n", ovly_ch, val);
  368. }
  369. static void ade_dump_overlay_compositor_regs(void __iomem *base, u32 comp)
  370. {
  371. u32 val;
  372. val = ade_read_reload_bit(base, OVLY_OFST + comp);
  373. DRM_DEBUG_DRIVER("[overlay%d]: reload(%d)\n", comp + 1, val);
  374. writel(ADE_ENABLE, base + ADE_OVLYX_CTL(comp));
  375. DRM_DEBUG_DRIVER("[overlay%d]: reg_ctl(0x%08x)\n", comp + 1, val);
  376. val = readl(base + ADE_OVLY_CTL);
  377. DRM_DEBUG_DRIVER("ovly_ctl(0x%08x)\n", val);
  378. }
  379. static void ade_dump_regs(void __iomem *base)
  380. {
  381. u32 i;
  382. /* dump channel regs */
  383. for (i = 0; i < ADE_CH_NUM; i++) {
  384. /* dump rdma regs */
  385. ade_rdma_dump_regs(base, i);
  386. /* dump clip regs */
  387. ade_clip_dump_regs(base, i);
  388. /* dump compositor routing regs */
  389. ade_compositor_routing_dump_regs(base, i);
  390. }
  391. /* dump overlay compositor regs */
  392. ade_dump_overlay_compositor_regs(base, OUT_OVLY);
  393. }
  394. #else
  395. static void ade_dump_regs(void __iomem *base) { }
  396. #endif
  397. static void ade_crtc_enable(struct drm_crtc *crtc)
  398. {
  399. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  400. struct ade_hw_ctx *ctx = acrtc->ctx;
  401. int ret;
  402. if (acrtc->enable)
  403. return;
  404. if (!ctx->power_on) {
  405. ret = ade_power_up(ctx);
  406. if (ret)
  407. return;
  408. }
  409. ade_set_medianoc_qos(acrtc);
  410. ade_display_enable(acrtc);
  411. ade_dump_regs(ctx->base);
  412. drm_crtc_vblank_on(crtc);
  413. acrtc->enable = true;
  414. }
  415. static void ade_crtc_disable(struct drm_crtc *crtc)
  416. {
  417. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  418. struct ade_hw_ctx *ctx = acrtc->ctx;
  419. if (!acrtc->enable)
  420. return;
  421. drm_crtc_vblank_off(crtc);
  422. ade_power_down(ctx);
  423. acrtc->enable = false;
  424. }
  425. static void ade_crtc_mode_set_nofb(struct drm_crtc *crtc)
  426. {
  427. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  428. struct ade_hw_ctx *ctx = acrtc->ctx;
  429. struct drm_display_mode *mode = &crtc->state->mode;
  430. struct drm_display_mode *adj_mode = &crtc->state->adjusted_mode;
  431. if (!ctx->power_on)
  432. (void)ade_power_up(ctx);
  433. ade_ldi_set_mode(acrtc, mode, adj_mode);
  434. }
  435. static void ade_crtc_atomic_begin(struct drm_crtc *crtc,
  436. struct drm_crtc_state *old_state)
  437. {
  438. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  439. struct ade_hw_ctx *ctx = acrtc->ctx;
  440. if (!ctx->power_on)
  441. (void)ade_power_up(ctx);
  442. }
  443. static void ade_crtc_atomic_flush(struct drm_crtc *crtc,
  444. struct drm_crtc_state *old_state)
  445. {
  446. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  447. struct ade_hw_ctx *ctx = acrtc->ctx;
  448. struct drm_pending_vblank_event *event = crtc->state->event;
  449. void __iomem *base = ctx->base;
  450. /* only crtc is enabled regs take effect */
  451. if (acrtc->enable) {
  452. ade_dump_regs(base);
  453. /* flush ade registers */
  454. writel(ADE_ENABLE, base + ADE_EN);
  455. }
  456. if (event) {
  457. crtc->state->event = NULL;
  458. spin_lock_irq(&crtc->dev->event_lock);
  459. if (drm_crtc_vblank_get(crtc) == 0)
  460. drm_crtc_arm_vblank_event(crtc, event);
  461. else
  462. drm_crtc_send_vblank_event(crtc, event);
  463. spin_unlock_irq(&crtc->dev->event_lock);
  464. }
  465. }
  466. static const struct drm_crtc_helper_funcs ade_crtc_helper_funcs = {
  467. .enable = ade_crtc_enable,
  468. .disable = ade_crtc_disable,
  469. .mode_set_nofb = ade_crtc_mode_set_nofb,
  470. .atomic_begin = ade_crtc_atomic_begin,
  471. .atomic_flush = ade_crtc_atomic_flush,
  472. };
  473. static const struct drm_crtc_funcs ade_crtc_funcs = {
  474. .destroy = drm_crtc_cleanup,
  475. .set_config = drm_atomic_helper_set_config,
  476. .page_flip = drm_atomic_helper_page_flip,
  477. .reset = drm_atomic_helper_crtc_reset,
  478. .set_property = drm_atomic_helper_crtc_set_property,
  479. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  480. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  481. };
  482. static int ade_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  483. struct drm_plane *plane)
  484. {
  485. struct kirin_drm_private *priv = dev->dev_private;
  486. struct device_node *port;
  487. int ret;
  488. /* set crtc port so that
  489. * drm_of_find_possible_crtcs call works
  490. */
  491. port = of_get_child_by_name(dev->dev->of_node, "port");
  492. if (!port) {
  493. DRM_ERROR("no port node found in %s\n",
  494. dev->dev->of_node->full_name);
  495. return -EINVAL;
  496. }
  497. of_node_put(port);
  498. crtc->port = port;
  499. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  500. &ade_crtc_funcs, NULL);
  501. if (ret) {
  502. DRM_ERROR("failed to init crtc.\n");
  503. return ret;
  504. }
  505. drm_crtc_helper_add(crtc, &ade_crtc_helper_funcs);
  506. priv->crtc[drm_crtc_index(crtc)] = crtc;
  507. return 0;
  508. }
  509. static void ade_rdma_set(void __iomem *base, struct drm_framebuffer *fb,
  510. u32 ch, u32 y, u32 in_h, u32 fmt)
  511. {
  512. struct drm_gem_cma_object *obj = drm_fb_cma_get_gem_obj(fb, 0);
  513. u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
  514. u32 stride = fb->pitches[0];
  515. u32 addr = (u32)obj->paddr + y * stride;
  516. DRM_DEBUG_DRIVER("rdma%d: (y=%d, height=%d), stride=%d, paddr=0x%x\n",
  517. ch + 1, y, in_h, stride, (u32)obj->paddr);
  518. DRM_DEBUG_DRIVER("addr=0x%x, fb:%dx%d, pixel_format=%d(%s)\n",
  519. addr, fb->width, fb->height, fmt,
  520. drm_get_format_name(fb->pixel_format));
  521. /* get reg offset */
  522. reg_ctrl = RD_CH_CTRL(ch);
  523. reg_addr = RD_CH_ADDR(ch);
  524. reg_size = RD_CH_SIZE(ch);
  525. reg_stride = RD_CH_STRIDE(ch);
  526. reg_space = RD_CH_SPACE(ch);
  527. reg_en = RD_CH_EN(ch);
  528. /*
  529. * TODO: set rotation
  530. */
  531. writel((fmt << 16) & 0x1f0000, base + reg_ctrl);
  532. writel(addr, base + reg_addr);
  533. writel((in_h << 16) | stride, base + reg_size);
  534. writel(stride, base + reg_stride);
  535. writel(in_h * stride, base + reg_space);
  536. writel(ADE_ENABLE, base + reg_en);
  537. ade_update_reload_bit(base, RDMA_OFST + ch, 0);
  538. }
  539. static void ade_rdma_disable(void __iomem *base, u32 ch)
  540. {
  541. u32 reg_en;
  542. /* get reg offset */
  543. reg_en = RD_CH_EN(ch);
  544. writel(0, base + reg_en);
  545. ade_update_reload_bit(base, RDMA_OFST + ch, 1);
  546. }
  547. static void ade_clip_set(void __iomem *base, u32 ch, u32 fb_w, u32 x,
  548. u32 in_w, u32 in_h)
  549. {
  550. u32 disable_val;
  551. u32 clip_left;
  552. u32 clip_right;
  553. /*
  554. * clip width, no need to clip height
  555. */
  556. if (fb_w == in_w) { /* bypass */
  557. disable_val = 1;
  558. clip_left = 0;
  559. clip_right = 0;
  560. } else {
  561. disable_val = 0;
  562. clip_left = x;
  563. clip_right = fb_w - (x + in_w) - 1;
  564. }
  565. DRM_DEBUG_DRIVER("clip%d: clip_left=%d, clip_right=%d\n",
  566. ch + 1, clip_left, clip_right);
  567. writel(disable_val, base + ADE_CLIP_DISABLE(ch));
  568. writel((fb_w - 1) << 16 | (in_h - 1), base + ADE_CLIP_SIZE0(ch));
  569. writel(clip_left << 16 | clip_right, base + ADE_CLIP_SIZE1(ch));
  570. ade_update_reload_bit(base, CLIP_OFST + ch, 0);
  571. }
  572. static void ade_clip_disable(void __iomem *base, u32 ch)
  573. {
  574. writel(1, base + ADE_CLIP_DISABLE(ch));
  575. ade_update_reload_bit(base, CLIP_OFST + ch, 1);
  576. }
  577. static bool has_Alpha_channel(int format)
  578. {
  579. switch (format) {
  580. case ADE_ARGB_8888:
  581. case ADE_ABGR_8888:
  582. case ADE_RGBA_8888:
  583. case ADE_BGRA_8888:
  584. return true;
  585. default:
  586. return false;
  587. }
  588. }
  589. static void ade_get_blending_params(u32 fmt, u8 glb_alpha, u8 *alp_mode,
  590. u8 *alp_sel, u8 *under_alp_sel)
  591. {
  592. bool has_alpha = has_Alpha_channel(fmt);
  593. /*
  594. * get alp_mode
  595. */
  596. if (has_alpha && glb_alpha < 255)
  597. *alp_mode = ADE_ALP_PIXEL_AND_GLB;
  598. else if (has_alpha)
  599. *alp_mode = ADE_ALP_PIXEL;
  600. else
  601. *alp_mode = ADE_ALP_GLOBAL;
  602. /*
  603. * get alp sel
  604. */
  605. *alp_sel = ADE_ALP_MUL_COEFF_3; /* 1 */
  606. *under_alp_sel = ADE_ALP_MUL_COEFF_2; /* 0 */
  607. }
  608. static void ade_compositor_routing_set(void __iomem *base, u8 ch,
  609. u32 x0, u32 y0,
  610. u32 in_w, u32 in_h, u32 fmt)
  611. {
  612. u8 ovly_ch = 0; /* TODO: This is the zpos, only one plane now */
  613. u8 glb_alpha = 255;
  614. u32 x1 = x0 + in_w - 1;
  615. u32 y1 = y0 + in_h - 1;
  616. u32 val;
  617. u8 alp_sel;
  618. u8 under_alp_sel;
  619. u8 alp_mode;
  620. ade_get_blending_params(fmt, glb_alpha, &alp_mode, &alp_sel,
  621. &under_alp_sel);
  622. /* overlay routing setting
  623. */
  624. writel(x0 << 16 | y0, base + ADE_OVLY_CH_XY0(ovly_ch));
  625. writel(x1 << 16 | y1, base + ADE_OVLY_CH_XY1(ovly_ch));
  626. val = (ch + 1) << CH_SEL_OFST | BIT(CH_EN_OFST) |
  627. alp_sel << CH_ALP_SEL_OFST |
  628. under_alp_sel << CH_UNDER_ALP_SEL_OFST |
  629. glb_alpha << CH_ALP_GBL_OFST |
  630. alp_mode << CH_ALP_MODE_OFST;
  631. writel(val, base + ADE_OVLY_CH_CTL(ovly_ch));
  632. /* connect this plane/channel to overlay2 compositor */
  633. ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
  634. CH_OVLY_SEL_MASK, CH_OVLY_SEL_VAL(OUT_OVLY));
  635. }
  636. static void ade_compositor_routing_disable(void __iomem *base, u32 ch)
  637. {
  638. u8 ovly_ch = 0; /* TODO: Only primary plane now */
  639. /* disable this plane/channel */
  640. ade_update_bits(base + ADE_OVLY_CH_CTL(ovly_ch), CH_EN_OFST,
  641. MASK(1), 0);
  642. /* dis-connect this plane/channel of overlay2 compositor */
  643. ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
  644. CH_OVLY_SEL_MASK, 0);
  645. }
  646. /*
  647. * Typicaly, a channel looks like: DMA-->clip-->scale-->ctrans-->compositor
  648. */
  649. static void ade_update_channel(struct ade_plane *aplane,
  650. struct drm_framebuffer *fb, int crtc_x,
  651. int crtc_y, unsigned int crtc_w,
  652. unsigned int crtc_h, u32 src_x,
  653. u32 src_y, u32 src_w, u32 src_h)
  654. {
  655. struct ade_hw_ctx *ctx = aplane->ctx;
  656. void __iomem *base = ctx->base;
  657. u32 fmt = ade_get_format(fb->pixel_format);
  658. u32 ch = aplane->ch;
  659. u32 in_w;
  660. u32 in_h;
  661. DRM_DEBUG_DRIVER("channel%d: src:(%d, %d)-%dx%d, crtc:(%d, %d)-%dx%d",
  662. ch + 1, src_x, src_y, src_w, src_h,
  663. crtc_x, crtc_y, crtc_w, crtc_h);
  664. /* 1) DMA setting */
  665. in_w = src_w;
  666. in_h = src_h;
  667. ade_rdma_set(base, fb, ch, src_y, in_h, fmt);
  668. /* 2) clip setting */
  669. ade_clip_set(base, ch, fb->width, src_x, in_w, in_h);
  670. /* 3) TODO: scale setting for overlay planes */
  671. /* 4) TODO: ctran/csc setting for overlay planes */
  672. /* 5) compositor routing setting */
  673. ade_compositor_routing_set(base, ch, crtc_x, crtc_y, in_w, in_h, fmt);
  674. }
  675. static void ade_disable_channel(struct ade_plane *aplane)
  676. {
  677. struct ade_hw_ctx *ctx = aplane->ctx;
  678. void __iomem *base = ctx->base;
  679. u32 ch = aplane->ch;
  680. DRM_DEBUG_DRIVER("disable channel%d\n", ch + 1);
  681. /* disable read DMA */
  682. ade_rdma_disable(base, ch);
  683. /* disable clip */
  684. ade_clip_disable(base, ch);
  685. /* disable compositor routing */
  686. ade_compositor_routing_disable(base, ch);
  687. }
  688. static int ade_plane_prepare_fb(struct drm_plane *plane,
  689. const struct drm_plane_state *new_state)
  690. {
  691. /* do nothing */
  692. return 0;
  693. }
  694. static void ade_plane_cleanup_fb(struct drm_plane *plane,
  695. const struct drm_plane_state *old_state)
  696. {
  697. /* do nothing */
  698. }
  699. static int ade_plane_atomic_check(struct drm_plane *plane,
  700. struct drm_plane_state *state)
  701. {
  702. struct drm_framebuffer *fb = state->fb;
  703. struct drm_crtc *crtc = state->crtc;
  704. struct drm_crtc_state *crtc_state;
  705. u32 src_x = state->src_x >> 16;
  706. u32 src_y = state->src_y >> 16;
  707. u32 src_w = state->src_w >> 16;
  708. u32 src_h = state->src_h >> 16;
  709. int crtc_x = state->crtc_x;
  710. int crtc_y = state->crtc_y;
  711. u32 crtc_w = state->crtc_w;
  712. u32 crtc_h = state->crtc_h;
  713. u32 fmt;
  714. if (!crtc || !fb)
  715. return 0;
  716. fmt = ade_get_format(fb->pixel_format);
  717. if (fmt == ADE_FORMAT_UNSUPPORT)
  718. return -EINVAL;
  719. crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
  720. if (IS_ERR(crtc_state))
  721. return PTR_ERR(crtc_state);
  722. if (src_w != crtc_w || src_h != crtc_h) {
  723. DRM_ERROR("Scale not support!!!\n");
  724. return -EINVAL;
  725. }
  726. if (src_x + src_w > fb->width ||
  727. src_y + src_h > fb->height)
  728. return -EINVAL;
  729. if (crtc_x < 0 || crtc_y < 0)
  730. return -EINVAL;
  731. if (crtc_x + crtc_w > crtc_state->adjusted_mode.hdisplay ||
  732. crtc_y + crtc_h > crtc_state->adjusted_mode.vdisplay)
  733. return -EINVAL;
  734. return 0;
  735. }
  736. static void ade_plane_atomic_update(struct drm_plane *plane,
  737. struct drm_plane_state *old_state)
  738. {
  739. struct drm_plane_state *state = plane->state;
  740. struct ade_plane *aplane = to_ade_plane(plane);
  741. ade_update_channel(aplane, state->fb, state->crtc_x, state->crtc_y,
  742. state->crtc_w, state->crtc_h,
  743. state->src_x >> 16, state->src_y >> 16,
  744. state->src_w >> 16, state->src_h >> 16);
  745. }
  746. static void ade_plane_atomic_disable(struct drm_plane *plane,
  747. struct drm_plane_state *old_state)
  748. {
  749. struct ade_plane *aplane = to_ade_plane(plane);
  750. ade_disable_channel(aplane);
  751. }
  752. static const struct drm_plane_helper_funcs ade_plane_helper_funcs = {
  753. .prepare_fb = ade_plane_prepare_fb,
  754. .cleanup_fb = ade_plane_cleanup_fb,
  755. .atomic_check = ade_plane_atomic_check,
  756. .atomic_update = ade_plane_atomic_update,
  757. .atomic_disable = ade_plane_atomic_disable,
  758. };
  759. static struct drm_plane_funcs ade_plane_funcs = {
  760. .update_plane = drm_atomic_helper_update_plane,
  761. .disable_plane = drm_atomic_helper_disable_plane,
  762. .set_property = drm_atomic_helper_plane_set_property,
  763. .destroy = drm_plane_cleanup,
  764. .reset = drm_atomic_helper_plane_reset,
  765. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  766. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  767. };
  768. static int ade_plane_init(struct drm_device *dev, struct ade_plane *aplane,
  769. enum drm_plane_type type)
  770. {
  771. const u32 *fmts;
  772. u32 fmts_cnt;
  773. int ret = 0;
  774. /* get properties */
  775. fmts_cnt = ade_get_channel_formats(aplane->ch, &fmts);
  776. if (ret)
  777. return ret;
  778. ret = drm_universal_plane_init(dev, &aplane->base, 1, &ade_plane_funcs,
  779. fmts, fmts_cnt, type, NULL);
  780. if (ret) {
  781. DRM_ERROR("fail to init plane, ch=%d\n", aplane->ch);
  782. return ret;
  783. }
  784. drm_plane_helper_add(&aplane->base, &ade_plane_helper_funcs);
  785. return 0;
  786. }
  787. static int ade_dts_parse(struct platform_device *pdev, struct ade_hw_ctx *ctx)
  788. {
  789. struct resource *res;
  790. struct device *dev = &pdev->dev;
  791. struct device_node *np = pdev->dev.of_node;
  792. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  793. ctx->base = devm_ioremap_resource(dev, res);
  794. if (IS_ERR(ctx->base)) {
  795. DRM_ERROR("failed to remap ade io base\n");
  796. return PTR_ERR(ctx->base);
  797. }
  798. ctx->reset = devm_reset_control_get(dev, NULL);
  799. if (IS_ERR(ctx->reset))
  800. return PTR_ERR(ctx->reset);
  801. ctx->noc_regmap =
  802. syscon_regmap_lookup_by_phandle(np, "hisilicon,noc-syscon");
  803. if (IS_ERR(ctx->noc_regmap)) {
  804. DRM_ERROR("failed to get noc regmap\n");
  805. return PTR_ERR(ctx->noc_regmap);
  806. }
  807. ctx->irq = platform_get_irq(pdev, 0);
  808. if (ctx->irq < 0) {
  809. DRM_ERROR("failed to get irq\n");
  810. return -ENODEV;
  811. }
  812. ctx->ade_core_clk = devm_clk_get(dev, "clk_ade_core");
  813. if (IS_ERR(ctx->ade_core_clk)) {
  814. DRM_ERROR("failed to parse clk ADE_CORE\n");
  815. return PTR_ERR(ctx->ade_core_clk);
  816. }
  817. ctx->media_noc_clk = devm_clk_get(dev, "clk_codec_jpeg");
  818. if (IS_ERR(ctx->media_noc_clk)) {
  819. DRM_ERROR("failed to parse clk CODEC_JPEG\n");
  820. return PTR_ERR(ctx->media_noc_clk);
  821. }
  822. ctx->ade_pix_clk = devm_clk_get(dev, "clk_ade_pix");
  823. if (IS_ERR(ctx->ade_pix_clk)) {
  824. DRM_ERROR("failed to parse clk ADE_PIX\n");
  825. return PTR_ERR(ctx->ade_pix_clk);
  826. }
  827. return 0;
  828. }
  829. static int ade_drm_init(struct drm_device *dev)
  830. {
  831. struct platform_device *pdev = dev->platformdev;
  832. struct ade_data *ade;
  833. struct ade_hw_ctx *ctx;
  834. struct ade_crtc *acrtc;
  835. struct ade_plane *aplane;
  836. enum drm_plane_type type;
  837. int ret;
  838. int i;
  839. ade = devm_kzalloc(dev->dev, sizeof(*ade), GFP_KERNEL);
  840. if (!ade) {
  841. DRM_ERROR("failed to alloc ade_data\n");
  842. return -ENOMEM;
  843. }
  844. platform_set_drvdata(pdev, ade);
  845. ctx = &ade->ctx;
  846. acrtc = &ade->acrtc;
  847. acrtc->ctx = ctx;
  848. acrtc->out_format = LDI_OUT_RGB_888;
  849. ret = ade_dts_parse(pdev, ctx);
  850. if (ret)
  851. return ret;
  852. /*
  853. * plane init
  854. * TODO: Now only support primary plane, overlay planes
  855. * need to do.
  856. */
  857. for (i = 0; i < ADE_CH_NUM; i++) {
  858. aplane = &ade->aplane[i];
  859. aplane->ch = i;
  860. aplane->ctx = ctx;
  861. type = i == PRIMARY_CH ? DRM_PLANE_TYPE_PRIMARY :
  862. DRM_PLANE_TYPE_OVERLAY;
  863. ret = ade_plane_init(dev, aplane, type);
  864. if (ret)
  865. return ret;
  866. }
  867. /* crtc init */
  868. ret = ade_crtc_init(dev, &acrtc->base, &ade->aplane[PRIMARY_CH].base);
  869. if (ret)
  870. return ret;
  871. /* vblank irq init */
  872. ret = devm_request_irq(dev->dev, ctx->irq, ade_irq_handler,
  873. IRQF_SHARED, dev->driver->name, acrtc);
  874. if (ret)
  875. return ret;
  876. dev->driver->get_vblank_counter = drm_vblank_no_hw_counter;
  877. dev->driver->enable_vblank = ade_enable_vblank;
  878. dev->driver->disable_vblank = ade_disable_vblank;
  879. return 0;
  880. }
  881. static void ade_drm_cleanup(struct drm_device *dev)
  882. {
  883. struct platform_device *pdev = dev->platformdev;
  884. struct ade_data *ade = platform_get_drvdata(pdev);
  885. struct drm_crtc *crtc = &ade->acrtc.base;
  886. drm_crtc_cleanup(crtc);
  887. }
  888. const struct kirin_dc_ops ade_dc_ops = {
  889. .init = ade_drm_init,
  890. .cleanup = ade_drm_cleanup
  891. };