kirin_drm_ade.c 27 KB

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