1 // SPDX-License-Identifier: GPL-2.0-only
3 * Hisilicon Hi6220 SoC ADE(Advanced Display Engine)'s crtc&plane driver
5 * Copyright (c) 2016 Linaro Limited.
6 * Copyright (c) 2014-2016 Hisilicon Limited.
9 * Xinliang Liu <z.liuxinliang@hisilicon.com>
10 * Xinliang Liu <xinliang.liu@linaro.org>
11 * Xinwei Kong <kong.kongxinwei@hisilicon.com>
14 #include <linux/bitops.h>
15 #include <linux/clk.h>
16 #include <video/display_timing.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/regmap.h>
19 #include <linux/reset.h>
22 #include <drm/drm_atomic.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_crtc.h>
25 #include <drm/drm_fb_cma_helper.h>
26 #include <drm/drm_gem_cma_helper.h>
27 #include <drm/drm_plane_helper.h>
28 #include <drm/drm_probe_helper.h>
30 #include "kirin_drm_drv.h"
31 #include "kirin_ade_reg.h"
33 #define PRIMARY_CH ADE_CH1 /* primary plane */
34 #define OUT_OVLY ADE_OVLY2 /* output overlay compositor */
37 #define to_ade_crtc(crtc) \
38 container_of(crtc, struct ade_crtc, base)
40 #define to_ade_plane(plane) \
41 container_of(plane, struct ade_plane, base)
45 struct regmap *noc_regmap;
46 struct clk *ade_core_clk;
47 struct clk *media_noc_clk;
48 struct clk *ade_pix_clk;
49 struct reset_control *reset;
56 struct ade_hw_ctx *ctx;
62 struct drm_plane base;
68 struct ade_crtc acrtc;
69 struct ade_plane aplane[ADE_CH_NUM];
70 struct ade_hw_ctx ctx;
73 /* ade-format info: */
76 enum ade_fb_format ade_format;
79 static const struct ade_format ade_formats[] = {
81 { DRM_FORMAT_RGB565, ADE_RGB_565 },
82 { DRM_FORMAT_BGR565, ADE_BGR_565 },
84 { DRM_FORMAT_RGB888, ADE_RGB_888 },
85 { DRM_FORMAT_BGR888, ADE_BGR_888 },
87 { DRM_FORMAT_XRGB8888, ADE_XRGB_8888 },
88 { DRM_FORMAT_XBGR8888, ADE_XBGR_8888 },
89 { DRM_FORMAT_RGBA8888, ADE_RGBA_8888 },
90 { DRM_FORMAT_BGRA8888, ADE_BGRA_8888 },
91 { DRM_FORMAT_ARGB8888, ADE_ARGB_8888 },
92 { DRM_FORMAT_ABGR8888, ADE_ABGR_8888 },
95 static const u32 channel_formats1[] = {
97 DRM_FORMAT_RGB565, DRM_FORMAT_BGR565, DRM_FORMAT_RGB888,
98 DRM_FORMAT_BGR888, DRM_FORMAT_XRGB8888, DRM_FORMAT_XBGR8888,
99 DRM_FORMAT_RGBA8888, DRM_FORMAT_BGRA8888, DRM_FORMAT_ARGB8888,
103 u32 ade_get_channel_formats(u8 ch, const u32 **formats)
107 *formats = channel_formats1;
108 return ARRAY_SIZE(channel_formats1);
110 DRM_ERROR("no this channel %d\n", ch);
116 /* convert from fourcc format to ade format */
117 static u32 ade_get_format(u32 pixel_format)
121 for (i = 0; i < ARRAY_SIZE(ade_formats); i++)
122 if (ade_formats[i].pixel_format == pixel_format)
123 return ade_formats[i].ade_format;
126 DRM_ERROR("Not found pixel format!!fourcc_format= %d\n",
128 return ADE_FORMAT_UNSUPPORT;
131 static void ade_update_reload_bit(void __iomem *base, u32 bit_num, u32 val)
133 u32 bit_ofst, reg_num;
135 bit_ofst = bit_num % 32;
136 reg_num = bit_num / 32;
138 ade_update_bits(base + ADE_RELOAD_DIS(reg_num), bit_ofst,
142 static u32 ade_read_reload_bit(void __iomem *base, u32 bit_num)
144 u32 tmp, bit_ofst, reg_num;
146 bit_ofst = bit_num % 32;
147 reg_num = bit_num / 32;
149 tmp = readl(base + ADE_RELOAD_DIS(reg_num));
150 return !!(BIT(bit_ofst) & tmp);
153 static void ade_init(struct ade_hw_ctx *ctx)
155 void __iomem *base = ctx->base;
157 /* enable clk gate */
158 ade_update_bits(base + ADE_CTRL1, AUTO_CLK_GATE_EN_OFST,
159 AUTO_CLK_GATE_EN, ADE_ENABLE);
161 writel(0, base + ADE_OVLY1_TRANS_CFG);
162 writel(0, base + ADE_OVLY_CTL);
163 writel(0, base + ADE_OVLYX_CTL(OUT_OVLY));
164 /* clear reset and reload regs */
165 writel(MASK(32), base + ADE_SOFT_RST_SEL(0));
166 writel(MASK(32), base + ADE_SOFT_RST_SEL(1));
167 writel(MASK(32), base + ADE_RELOAD_DIS(0));
168 writel(MASK(32), base + ADE_RELOAD_DIS(1));
170 * for video mode, all the ade registers should
171 * become effective at frame end.
173 ade_update_bits(base + ADE_CTRL, FRM_END_START_OFST,
174 FRM_END_START_MASK, REG_EFFECTIVE_IN_ADEEN_FRMEND);
177 static bool ade_crtc_mode_fixup(struct drm_crtc *crtc,
178 const struct drm_display_mode *mode,
179 struct drm_display_mode *adjusted_mode)
181 struct ade_crtc *acrtc = to_ade_crtc(crtc);
182 struct ade_hw_ctx *ctx = acrtc->ctx;
184 adjusted_mode->clock =
185 clk_round_rate(ctx->ade_pix_clk, mode->clock * 1000) / 1000;
190 static void ade_set_pix_clk(struct ade_hw_ctx *ctx,
191 struct drm_display_mode *mode,
192 struct drm_display_mode *adj_mode)
194 u32 clk_Hz = mode->clock * 1000;
198 * Success should be guaranteed in mode_valid call back,
199 * so failure shouldn't happen here
201 ret = clk_set_rate(ctx->ade_pix_clk, clk_Hz);
203 DRM_ERROR("failed to set pixel clk %dHz (%d)\n", clk_Hz, ret);
204 adj_mode->clock = clk_get_rate(ctx->ade_pix_clk) / 1000;
207 static void ade_ldi_set_mode(struct ade_crtc *acrtc,
208 struct drm_display_mode *mode,
209 struct drm_display_mode *adj_mode)
211 struct ade_hw_ctx *ctx = acrtc->ctx;
212 void __iomem *base = ctx->base;
213 u32 width = mode->hdisplay;
214 u32 height = mode->vdisplay;
215 u32 hfp, hbp, hsw, vfp, vbp, vsw;
218 plr_flags = (mode->flags & DRM_MODE_FLAG_NVSYNC) ? FLAG_NVSYNC : 0;
219 plr_flags |= (mode->flags & DRM_MODE_FLAG_NHSYNC) ? FLAG_NHSYNC : 0;
220 hfp = mode->hsync_start - mode->hdisplay;
221 hbp = mode->htotal - mode->hsync_end;
222 hsw = mode->hsync_end - mode->hsync_start;
223 vfp = mode->vsync_start - mode->vdisplay;
224 vbp = mode->vtotal - mode->vsync_end;
225 vsw = mode->vsync_end - mode->vsync_start;
227 DRM_DEBUG_DRIVER("vsw exceeded 15\n");
231 writel((hbp << HBP_OFST) | hfp, base + LDI_HRZ_CTRL0);
232 /* the configured value is actual value - 1 */
233 writel(hsw - 1, base + LDI_HRZ_CTRL1);
234 writel((vbp << VBP_OFST) | vfp, base + LDI_VRT_CTRL0);
235 /* the configured value is actual value - 1 */
236 writel(vsw - 1, base + LDI_VRT_CTRL1);
237 /* the configured value is actual value - 1 */
238 writel(((height - 1) << VSIZE_OFST) | (width - 1),
239 base + LDI_DSP_SIZE);
240 writel(plr_flags, base + LDI_PLR_CTRL);
242 /* set overlay compositor output size */
243 writel(((width - 1) << OUTPUT_XSIZE_OFST) | (height - 1),
244 base + ADE_OVLY_OUTPUT_SIZE(OUT_OVLY));
247 writel(CTRAN_BYPASS_ON, base + ADE_CTRAN_DIS(ADE_CTRAN6));
248 /* the configured value is actual value - 1 */
249 writel(width * height - 1, base + ADE_CTRAN_IMAGE_SIZE(ADE_CTRAN6));
250 ade_update_reload_bit(base, CTRAN_OFST + ADE_CTRAN6, 0);
252 ade_set_pix_clk(ctx, mode, adj_mode);
254 DRM_DEBUG_DRIVER("set mode: %dx%d\n", width, height);
257 static int ade_power_up(struct ade_hw_ctx *ctx)
261 ret = clk_prepare_enable(ctx->media_noc_clk);
263 DRM_ERROR("failed to enable media_noc_clk (%d)\n", ret);
267 ret = reset_control_deassert(ctx->reset);
269 DRM_ERROR("failed to deassert reset\n");
273 ret = clk_prepare_enable(ctx->ade_core_clk);
275 DRM_ERROR("failed to enable ade_core_clk (%d)\n", ret);
280 ctx->power_on = true;
284 static void ade_power_down(struct ade_hw_ctx *ctx)
286 void __iomem *base = ctx->base;
288 writel(ADE_DISABLE, base + LDI_CTRL);
290 writel(DSI_PCLK_OFF, base + LDI_HDMI_DSI_GT);
292 clk_disable_unprepare(ctx->ade_core_clk);
293 reset_control_assert(ctx->reset);
294 clk_disable_unprepare(ctx->media_noc_clk);
295 ctx->power_on = false;
298 static void ade_set_medianoc_qos(struct ade_crtc *acrtc)
300 struct ade_hw_ctx *ctx = acrtc->ctx;
301 struct regmap *map = ctx->noc_regmap;
303 regmap_update_bits(map, ADE0_QOSGENERATOR_MODE,
304 QOSGENERATOR_MODE_MASK, BYPASS_MODE);
305 regmap_update_bits(map, ADE0_QOSGENERATOR_EXTCONTROL,
306 SOCKET_QOS_EN, SOCKET_QOS_EN);
308 regmap_update_bits(map, ADE1_QOSGENERATOR_MODE,
309 QOSGENERATOR_MODE_MASK, BYPASS_MODE);
310 regmap_update_bits(map, ADE1_QOSGENERATOR_EXTCONTROL,
311 SOCKET_QOS_EN, SOCKET_QOS_EN);
314 static int ade_crtc_enable_vblank(struct drm_crtc *crtc)
316 struct ade_crtc *acrtc = to_ade_crtc(crtc);
317 struct ade_hw_ctx *ctx = acrtc->ctx;
318 void __iomem *base = ctx->base;
321 (void)ade_power_up(ctx);
323 ade_update_bits(base + LDI_INT_EN, FRAME_END_INT_EN_OFST,
329 static void ade_crtc_disable_vblank(struct drm_crtc *crtc)
331 struct ade_crtc *acrtc = to_ade_crtc(crtc);
332 struct ade_hw_ctx *ctx = acrtc->ctx;
333 void __iomem *base = ctx->base;
335 if (!ctx->power_on) {
336 DRM_ERROR("power is down! vblank disable fail\n");
340 ade_update_bits(base + LDI_INT_EN, FRAME_END_INT_EN_OFST,
344 static irqreturn_t ade_irq_handler(int irq, void *data)
346 struct ade_crtc *acrtc = data;
347 struct ade_hw_ctx *ctx = acrtc->ctx;
348 struct drm_crtc *crtc = &acrtc->base;
349 void __iomem *base = ctx->base;
352 status = readl(base + LDI_MSK_INT);
353 DRM_DEBUG_VBL("LDI IRQ: status=0x%X\n", status);
356 if (status & BIT(FRAME_END_INT_EN_OFST)) {
357 ade_update_bits(base + LDI_INT_CLR, FRAME_END_INT_EN_OFST,
359 drm_crtc_handle_vblank(crtc);
365 static void ade_display_enable(struct ade_crtc *acrtc)
367 struct ade_hw_ctx *ctx = acrtc->ctx;
368 void __iomem *base = ctx->base;
369 u32 out_fmt = acrtc->out_format;
371 /* enable output overlay compositor */
372 writel(ADE_ENABLE, base + ADE_OVLYX_CTL(OUT_OVLY));
373 ade_update_reload_bit(base, OVLY_OFST + OUT_OVLY, 0);
375 /* display source setting */
376 writel(DISP_SRC_OVLY2, base + ADE_DISP_SRC_CFG);
379 writel(ADE_ENABLE, base + ADE_EN);
381 writel(NORMAL_MODE, base + LDI_WORK_MODE);
382 writel((out_fmt << BPP_OFST) | DATA_GATE_EN | LDI_EN,
385 writel(DSI_PCLK_ON, base + LDI_HDMI_DSI_GT);
389 static void ade_rdma_dump_regs(void __iomem *base, u32 ch)
391 u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
394 reg_ctrl = RD_CH_CTRL(ch);
395 reg_addr = RD_CH_ADDR(ch);
396 reg_size = RD_CH_SIZE(ch);
397 reg_stride = RD_CH_STRIDE(ch);
398 reg_space = RD_CH_SPACE(ch);
399 reg_en = RD_CH_EN(ch);
401 val = ade_read_reload_bit(base, RDMA_OFST + ch);
402 DRM_DEBUG_DRIVER("[rdma%d]: reload(%d)\n", ch + 1, val);
403 val = readl(base + reg_ctrl);
404 DRM_DEBUG_DRIVER("[rdma%d]: reg_ctrl(0x%08x)\n", ch + 1, val);
405 val = readl(base + reg_addr);
406 DRM_DEBUG_DRIVER("[rdma%d]: reg_addr(0x%08x)\n", ch + 1, val);
407 val = readl(base + reg_size);
408 DRM_DEBUG_DRIVER("[rdma%d]: reg_size(0x%08x)\n", ch + 1, val);
409 val = readl(base + reg_stride);
410 DRM_DEBUG_DRIVER("[rdma%d]: reg_stride(0x%08x)\n", ch + 1, val);
411 val = readl(base + reg_space);
412 DRM_DEBUG_DRIVER("[rdma%d]: reg_space(0x%08x)\n", ch + 1, val);
413 val = readl(base + reg_en);
414 DRM_DEBUG_DRIVER("[rdma%d]: reg_en(0x%08x)\n", ch + 1, val);
417 static void ade_clip_dump_regs(void __iomem *base, u32 ch)
421 val = ade_read_reload_bit(base, CLIP_OFST + ch);
422 DRM_DEBUG_DRIVER("[clip%d]: reload(%d)\n", ch + 1, val);
423 val = readl(base + ADE_CLIP_DISABLE(ch));
424 DRM_DEBUG_DRIVER("[clip%d]: reg_clip_disable(0x%08x)\n", ch + 1, val);
425 val = readl(base + ADE_CLIP_SIZE0(ch));
426 DRM_DEBUG_DRIVER("[clip%d]: reg_clip_size0(0x%08x)\n", ch + 1, val);
427 val = readl(base + ADE_CLIP_SIZE1(ch));
428 DRM_DEBUG_DRIVER("[clip%d]: reg_clip_size1(0x%08x)\n", ch + 1, val);
431 static void ade_compositor_routing_dump_regs(void __iomem *base, u32 ch)
433 u8 ovly_ch = 0; /* TODO: Only primary plane now */
436 val = readl(base + ADE_OVLY_CH_XY0(ovly_ch));
437 DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_xy0(0x%08x)\n", ovly_ch, val);
438 val = readl(base + ADE_OVLY_CH_XY1(ovly_ch));
439 DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_xy1(0x%08x)\n", ovly_ch, val);
440 val = readl(base + ADE_OVLY_CH_CTL(ovly_ch));
441 DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_ctl(0x%08x)\n", ovly_ch, val);
444 static void ade_dump_overlay_compositor_regs(void __iomem *base, u32 comp)
448 val = ade_read_reload_bit(base, OVLY_OFST + comp);
449 DRM_DEBUG_DRIVER("[overlay%d]: reload(%d)\n", comp + 1, val);
450 writel(ADE_ENABLE, base + ADE_OVLYX_CTL(comp));
451 DRM_DEBUG_DRIVER("[overlay%d]: reg_ctl(0x%08x)\n", comp + 1, val);
452 val = readl(base + ADE_OVLY_CTL);
453 DRM_DEBUG_DRIVER("ovly_ctl(0x%08x)\n", val);
456 static void ade_dump_regs(void __iomem *base)
460 /* dump channel regs */
461 for (i = 0; i < ADE_CH_NUM; i++) {
463 ade_rdma_dump_regs(base, i);
466 ade_clip_dump_regs(base, i);
468 /* dump compositor routing regs */
469 ade_compositor_routing_dump_regs(base, i);
472 /* dump overlay compositor regs */
473 ade_dump_overlay_compositor_regs(base, OUT_OVLY);
476 static void ade_dump_regs(void __iomem *base) { }
479 static void ade_crtc_atomic_enable(struct drm_crtc *crtc,
480 struct drm_crtc_state *old_state)
482 struct ade_crtc *acrtc = to_ade_crtc(crtc);
483 struct ade_hw_ctx *ctx = acrtc->ctx;
489 if (!ctx->power_on) {
490 ret = ade_power_up(ctx);
495 ade_set_medianoc_qos(acrtc);
496 ade_display_enable(acrtc);
497 ade_dump_regs(ctx->base);
498 drm_crtc_vblank_on(crtc);
499 acrtc->enable = true;
502 static void ade_crtc_atomic_disable(struct drm_crtc *crtc,
503 struct drm_crtc_state *old_state)
505 struct ade_crtc *acrtc = to_ade_crtc(crtc);
506 struct ade_hw_ctx *ctx = acrtc->ctx;
511 drm_crtc_vblank_off(crtc);
513 acrtc->enable = false;
516 static void ade_crtc_mode_set_nofb(struct drm_crtc *crtc)
518 struct ade_crtc *acrtc = to_ade_crtc(crtc);
519 struct ade_hw_ctx *ctx = acrtc->ctx;
520 struct drm_display_mode *mode = &crtc->state->mode;
521 struct drm_display_mode *adj_mode = &crtc->state->adjusted_mode;
524 (void)ade_power_up(ctx);
525 ade_ldi_set_mode(acrtc, mode, adj_mode);
528 static void ade_crtc_atomic_begin(struct drm_crtc *crtc,
529 struct drm_crtc_state *old_state)
531 struct ade_crtc *acrtc = to_ade_crtc(crtc);
532 struct ade_hw_ctx *ctx = acrtc->ctx;
533 struct drm_display_mode *mode = &crtc->state->mode;
534 struct drm_display_mode *adj_mode = &crtc->state->adjusted_mode;
537 (void)ade_power_up(ctx);
538 ade_ldi_set_mode(acrtc, mode, adj_mode);
541 static void ade_crtc_atomic_flush(struct drm_crtc *crtc,
542 struct drm_crtc_state *old_state)
545 struct ade_crtc *acrtc = to_ade_crtc(crtc);
546 struct ade_hw_ctx *ctx = acrtc->ctx;
547 struct drm_pending_vblank_event *event = crtc->state->event;
548 void __iomem *base = ctx->base;
550 /* only crtc is enabled regs take effect */
553 /* flush ade registers */
554 writel(ADE_ENABLE, base + ADE_EN);
558 crtc->state->event = NULL;
560 spin_lock_irq(&crtc->dev->event_lock);
561 if (drm_crtc_vblank_get(crtc) == 0)
562 drm_crtc_arm_vblank_event(crtc, event);
564 drm_crtc_send_vblank_event(crtc, event);
565 spin_unlock_irq(&crtc->dev->event_lock);
569 static const struct drm_crtc_helper_funcs ade_crtc_helper_funcs = {
570 .mode_fixup = ade_crtc_mode_fixup,
571 .mode_set_nofb = ade_crtc_mode_set_nofb,
572 .atomic_begin = ade_crtc_atomic_begin,
573 .atomic_flush = ade_crtc_atomic_flush,
574 .atomic_enable = ade_crtc_atomic_enable,
575 .atomic_disable = ade_crtc_atomic_disable,
578 static const struct drm_crtc_funcs ade_crtc_funcs = {
579 .destroy = drm_crtc_cleanup,
580 .set_config = drm_atomic_helper_set_config,
581 .page_flip = drm_atomic_helper_page_flip,
582 .reset = drm_atomic_helper_crtc_reset,
583 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
584 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
585 .enable_vblank = ade_crtc_enable_vblank,
586 .disable_vblank = ade_crtc_disable_vblank,
589 static int ade_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
590 struct drm_plane *plane)
592 struct device_node *port;
595 /* set crtc port so that
596 * drm_of_find_possible_crtcs call works
598 port = of_get_child_by_name(dev->dev->of_node, "port");
600 DRM_ERROR("no port node found in %pOF\n", dev->dev->of_node);
606 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
607 &ade_crtc_funcs, NULL);
609 DRM_ERROR("failed to init crtc.\n");
613 drm_crtc_helper_add(crtc, &ade_crtc_helper_funcs);
618 static void ade_rdma_set(void __iomem *base, struct drm_framebuffer *fb,
619 u32 ch, u32 y, u32 in_h, u32 fmt)
621 struct drm_gem_cma_object *obj = drm_fb_cma_get_gem_obj(fb, 0);
622 struct drm_format_name_buf format_name;
623 u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
624 u32 stride = fb->pitches[0];
625 u32 addr = (u32)obj->paddr + y * stride;
627 DRM_DEBUG_DRIVER("rdma%d: (y=%d, height=%d), stride=%d, paddr=0x%x\n",
628 ch + 1, y, in_h, stride, (u32)obj->paddr);
629 DRM_DEBUG_DRIVER("addr=0x%x, fb:%dx%d, pixel_format=%d(%s)\n",
630 addr, fb->width, fb->height, fmt,
631 drm_get_format_name(fb->format->format, &format_name));
634 reg_ctrl = RD_CH_CTRL(ch);
635 reg_addr = RD_CH_ADDR(ch);
636 reg_size = RD_CH_SIZE(ch);
637 reg_stride = RD_CH_STRIDE(ch);
638 reg_space = RD_CH_SPACE(ch);
639 reg_en = RD_CH_EN(ch);
644 writel((fmt << 16) & 0x1f0000, base + reg_ctrl);
645 writel(addr, base + reg_addr);
646 writel((in_h << 16) | stride, base + reg_size);
647 writel(stride, base + reg_stride);
648 writel(in_h * stride, base + reg_space);
649 writel(ADE_ENABLE, base + reg_en);
650 ade_update_reload_bit(base, RDMA_OFST + ch, 0);
653 static void ade_rdma_disable(void __iomem *base, u32 ch)
658 reg_en = RD_CH_EN(ch);
659 writel(0, base + reg_en);
660 ade_update_reload_bit(base, RDMA_OFST + ch, 1);
663 static void ade_clip_set(void __iomem *base, u32 ch, u32 fb_w, u32 x,
671 * clip width, no need to clip height
673 if (fb_w == in_w) { /* bypass */
680 clip_right = fb_w - (x + in_w) - 1;
683 DRM_DEBUG_DRIVER("clip%d: clip_left=%d, clip_right=%d\n",
684 ch + 1, clip_left, clip_right);
686 writel(disable_val, base + ADE_CLIP_DISABLE(ch));
687 writel((fb_w - 1) << 16 | (in_h - 1), base + ADE_CLIP_SIZE0(ch));
688 writel(clip_left << 16 | clip_right, base + ADE_CLIP_SIZE1(ch));
689 ade_update_reload_bit(base, CLIP_OFST + ch, 0);
692 static void ade_clip_disable(void __iomem *base, u32 ch)
694 writel(1, base + ADE_CLIP_DISABLE(ch));
695 ade_update_reload_bit(base, CLIP_OFST + ch, 1);
698 static bool has_Alpha_channel(int format)
711 static void ade_get_blending_params(u32 fmt, u8 glb_alpha, u8 *alp_mode,
712 u8 *alp_sel, u8 *under_alp_sel)
714 bool has_alpha = has_Alpha_channel(fmt);
719 if (has_alpha && glb_alpha < 255)
720 *alp_mode = ADE_ALP_PIXEL_AND_GLB;
722 *alp_mode = ADE_ALP_PIXEL;
724 *alp_mode = ADE_ALP_GLOBAL;
729 *alp_sel = ADE_ALP_MUL_COEFF_3; /* 1 */
730 *under_alp_sel = ADE_ALP_MUL_COEFF_2; /* 0 */
733 static void ade_compositor_routing_set(void __iomem *base, u8 ch,
735 u32 in_w, u32 in_h, u32 fmt)
737 u8 ovly_ch = 0; /* TODO: This is the zpos, only one plane now */
739 u32 x1 = x0 + in_w - 1;
740 u32 y1 = y0 + in_h - 1;
746 ade_get_blending_params(fmt, glb_alpha, &alp_mode, &alp_sel,
749 /* overlay routing setting
751 writel(x0 << 16 | y0, base + ADE_OVLY_CH_XY0(ovly_ch));
752 writel(x1 << 16 | y1, base + ADE_OVLY_CH_XY1(ovly_ch));
753 val = (ch + 1) << CH_SEL_OFST | BIT(CH_EN_OFST) |
754 alp_sel << CH_ALP_SEL_OFST |
755 under_alp_sel << CH_UNDER_ALP_SEL_OFST |
756 glb_alpha << CH_ALP_GBL_OFST |
757 alp_mode << CH_ALP_MODE_OFST;
758 writel(val, base + ADE_OVLY_CH_CTL(ovly_ch));
759 /* connect this plane/channel to overlay2 compositor */
760 ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
761 CH_OVLY_SEL_MASK, CH_OVLY_SEL_VAL(OUT_OVLY));
764 static void ade_compositor_routing_disable(void __iomem *base, u32 ch)
766 u8 ovly_ch = 0; /* TODO: Only primary plane now */
768 /* disable this plane/channel */
769 ade_update_bits(base + ADE_OVLY_CH_CTL(ovly_ch), CH_EN_OFST,
771 /* dis-connect this plane/channel of overlay2 compositor */
772 ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
773 CH_OVLY_SEL_MASK, 0);
777 * Typicaly, a channel looks like: DMA-->clip-->scale-->ctrans-->compositor
779 static void ade_update_channel(struct ade_plane *aplane,
780 struct drm_framebuffer *fb, int crtc_x,
781 int crtc_y, unsigned int crtc_w,
782 unsigned int crtc_h, u32 src_x,
783 u32 src_y, u32 src_w, u32 src_h)
785 struct ade_hw_ctx *ctx = aplane->ctx;
786 void __iomem *base = ctx->base;
787 u32 fmt = ade_get_format(fb->format->format);
792 DRM_DEBUG_DRIVER("channel%d: src:(%d, %d)-%dx%d, crtc:(%d, %d)-%dx%d",
793 ch + 1, src_x, src_y, src_w, src_h,
794 crtc_x, crtc_y, crtc_w, crtc_h);
799 ade_rdma_set(base, fb, ch, src_y, in_h, fmt);
801 /* 2) clip setting */
802 ade_clip_set(base, ch, fb->width, src_x, in_w, in_h);
804 /* 3) TODO: scale setting for overlay planes */
806 /* 4) TODO: ctran/csc setting for overlay planes */
808 /* 5) compositor routing setting */
809 ade_compositor_routing_set(base, ch, crtc_x, crtc_y, in_w, in_h, fmt);
812 static void ade_disable_channel(struct ade_plane *aplane)
814 struct ade_hw_ctx *ctx = aplane->ctx;
815 void __iomem *base = ctx->base;
818 DRM_DEBUG_DRIVER("disable channel%d\n", ch + 1);
820 /* disable read DMA */
821 ade_rdma_disable(base, ch);
824 ade_clip_disable(base, ch);
826 /* disable compositor routing */
827 ade_compositor_routing_disable(base, ch);
830 static int ade_plane_atomic_check(struct drm_plane *plane,
831 struct drm_plane_state *state)
833 struct drm_framebuffer *fb = state->fb;
834 struct drm_crtc *crtc = state->crtc;
835 struct drm_crtc_state *crtc_state;
836 u32 src_x = state->src_x >> 16;
837 u32 src_y = state->src_y >> 16;
838 u32 src_w = state->src_w >> 16;
839 u32 src_h = state->src_h >> 16;
840 int crtc_x = state->crtc_x;
841 int crtc_y = state->crtc_y;
842 u32 crtc_w = state->crtc_w;
843 u32 crtc_h = state->crtc_h;
849 fmt = ade_get_format(fb->format->format);
850 if (fmt == ADE_FORMAT_UNSUPPORT)
853 crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
854 if (IS_ERR(crtc_state))
855 return PTR_ERR(crtc_state);
857 if (src_w != crtc_w || src_h != crtc_h) {
861 if (src_x + src_w > fb->width ||
862 src_y + src_h > fb->height)
865 if (crtc_x < 0 || crtc_y < 0)
868 if (crtc_x + crtc_w > crtc_state->adjusted_mode.hdisplay ||
869 crtc_y + crtc_h > crtc_state->adjusted_mode.vdisplay)
875 static void ade_plane_atomic_update(struct drm_plane *plane,
876 struct drm_plane_state *old_state)
878 struct drm_plane_state *state = plane->state;
879 struct ade_plane *aplane = to_ade_plane(plane);
881 ade_update_channel(aplane, state->fb, state->crtc_x, state->crtc_y,
882 state->crtc_w, state->crtc_h,
883 state->src_x >> 16, state->src_y >> 16,
884 state->src_w >> 16, state->src_h >> 16);
887 static void ade_plane_atomic_disable(struct drm_plane *plane,
888 struct drm_plane_state *old_state)
890 struct ade_plane *aplane = to_ade_plane(plane);
892 ade_disable_channel(aplane);
895 static const struct drm_plane_helper_funcs ade_plane_helper_funcs = {
896 .atomic_check = ade_plane_atomic_check,
897 .atomic_update = ade_plane_atomic_update,
898 .atomic_disable = ade_plane_atomic_disable,
901 static struct drm_plane_funcs ade_plane_funcs = {
902 .update_plane = drm_atomic_helper_update_plane,
903 .disable_plane = drm_atomic_helper_disable_plane,
904 .destroy = drm_plane_cleanup,
905 .reset = drm_atomic_helper_plane_reset,
906 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
907 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
910 static int ade_plane_init(struct drm_device *dev, struct ade_plane *aplane,
911 enum drm_plane_type type)
918 fmts_cnt = ade_get_channel_formats(aplane->ch, &fmts);
922 ret = drm_universal_plane_init(dev, &aplane->base, 1, &ade_plane_funcs,
923 fmts, fmts_cnt, NULL, type, NULL);
925 DRM_ERROR("fail to init plane, ch=%d\n", aplane->ch);
929 drm_plane_helper_add(&aplane->base, &ade_plane_helper_funcs);
934 static int ade_dts_parse(struct platform_device *pdev, struct ade_hw_ctx *ctx)
936 struct resource *res;
937 struct device *dev = &pdev->dev;
938 struct device_node *np = pdev->dev.of_node;
940 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
941 ctx->base = devm_ioremap_resource(dev, res);
942 if (IS_ERR(ctx->base)) {
943 DRM_ERROR("failed to remap ade io base\n");
944 return PTR_ERR(ctx->base);
947 ctx->reset = devm_reset_control_get(dev, NULL);
948 if (IS_ERR(ctx->reset))
949 return PTR_ERR(ctx->reset);
952 syscon_regmap_lookup_by_phandle(np, "hisilicon,noc-syscon");
953 if (IS_ERR(ctx->noc_regmap)) {
954 DRM_ERROR("failed to get noc regmap\n");
955 return PTR_ERR(ctx->noc_regmap);
958 ctx->irq = platform_get_irq(pdev, 0);
960 DRM_ERROR("failed to get irq\n");
964 ctx->ade_core_clk = devm_clk_get(dev, "clk_ade_core");
965 if (IS_ERR(ctx->ade_core_clk)) {
966 DRM_ERROR("failed to parse clk ADE_CORE\n");
967 return PTR_ERR(ctx->ade_core_clk);
970 ctx->media_noc_clk = devm_clk_get(dev, "clk_codec_jpeg");
971 if (IS_ERR(ctx->media_noc_clk)) {
972 DRM_ERROR("failed to parse clk CODEC_JPEG\n");
973 return PTR_ERR(ctx->media_noc_clk);
976 ctx->ade_pix_clk = devm_clk_get(dev, "clk_ade_pix");
977 if (IS_ERR(ctx->ade_pix_clk)) {
978 DRM_ERROR("failed to parse clk ADE_PIX\n");
979 return PTR_ERR(ctx->ade_pix_clk);
985 static int ade_drm_init(struct platform_device *pdev)
987 struct drm_device *dev = platform_get_drvdata(pdev);
988 struct ade_data *ade;
989 struct ade_hw_ctx *ctx;
990 struct ade_crtc *acrtc;
991 struct ade_plane *aplane;
992 enum drm_plane_type type;
996 ade = devm_kzalloc(dev->dev, sizeof(*ade), GFP_KERNEL);
998 DRM_ERROR("failed to alloc ade_data\n");
1001 platform_set_drvdata(pdev, ade);
1004 acrtc = &ade->acrtc;
1006 acrtc->out_format = LDI_OUT_RGB_888;
1008 ret = ade_dts_parse(pdev, ctx);
1014 * TODO: Now only support primary plane, overlay planes
1017 for (i = 0; i < ADE_CH_NUM; i++) {
1018 aplane = &ade->aplane[i];
1021 type = i == PRIMARY_CH ? DRM_PLANE_TYPE_PRIMARY :
1022 DRM_PLANE_TYPE_OVERLAY;
1024 ret = ade_plane_init(dev, aplane, type);
1030 ret = ade_crtc_init(dev, &acrtc->base, &ade->aplane[PRIMARY_CH].base);
1034 /* vblank irq init */
1035 ret = devm_request_irq(dev->dev, ctx->irq, ade_irq_handler,
1036 IRQF_SHARED, dev->driver->name, acrtc);
1043 static void ade_drm_cleanup(struct platform_device *pdev)
1047 const struct kirin_dc_ops ade_dc_ops = {
1048 .init = ade_drm_init,
1049 .cleanup = ade_drm_cleanup