2 * Copyright (C) 2014 Free Electrons
3 * Copyright (C) 2014 Atmel
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "atmel_hlcdc_dc.h"
23 * Atmel HLCDC Plane state structure.
25 * @base: DRM plane state
26 * @crtc_x: x position of the plane relative to the CRTC
27 * @crtc_y: y position of the plane relative to the CRTC
28 * @crtc_w: visible width of the plane
29 * @crtc_h: visible height of the plane
30 * @src_x: x buffer position
31 * @src_y: y buffer position
32 * @src_w: buffer width
33 * @src_h: buffer height
34 * @alpha: alpha blending of the plane
35 * @disc_x: x discard position
36 * @disc_y: y discard position
37 * @disc_w: discard width
38 * @disc_h: discard height
39 * @bpp: bytes per pixel deduced from pixel_format
40 * @offsets: offsets to apply to the GEM buffers
41 * @xstride: value to add to the pixel pointer between each line
42 * @pstride: value to add to the pixel pointer between each pixel
43 * @nplanes: number of planes (deduced from pixel_format)
44 * @dscrs: DMA descriptors
46 struct atmel_hlcdc_plane_state {
47 struct drm_plane_state base;
66 /* These fields are private and should not be touched */
67 int bpp[ATMEL_HLCDC_LAYER_MAX_PLANES];
68 unsigned int offsets[ATMEL_HLCDC_LAYER_MAX_PLANES];
69 int xstride[ATMEL_HLCDC_LAYER_MAX_PLANES];
70 int pstride[ATMEL_HLCDC_LAYER_MAX_PLANES];
73 /* DMA descriptors. */
74 struct atmel_hlcdc_dma_channel_dscr *dscrs[ATMEL_HLCDC_LAYER_MAX_PLANES];
77 static inline struct atmel_hlcdc_plane_state *
78 drm_plane_state_to_atmel_hlcdc_plane_state(struct drm_plane_state *s)
80 return container_of(s, struct atmel_hlcdc_plane_state, base);
83 #define SUBPIXEL_MASK 0xffff
85 static uint32_t rgb_formats[] = {
98 struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_formats = {
99 .formats = rgb_formats,
100 .nformats = ARRAY_SIZE(rgb_formats),
103 static uint32_t rgb_and_yuv_formats[] = {
125 struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_and_yuv_formats = {
126 .formats = rgb_and_yuv_formats,
127 .nformats = ARRAY_SIZE(rgb_and_yuv_formats),
130 static int atmel_hlcdc_format_to_plane_mode(u32 format, u32 *mode)
134 *mode = ATMEL_HLCDC_C8_MODE;
136 case DRM_FORMAT_XRGB4444:
137 *mode = ATMEL_HLCDC_XRGB4444_MODE;
139 case DRM_FORMAT_ARGB4444:
140 *mode = ATMEL_HLCDC_ARGB4444_MODE;
142 case DRM_FORMAT_RGBA4444:
143 *mode = ATMEL_HLCDC_RGBA4444_MODE;
145 case DRM_FORMAT_RGB565:
146 *mode = ATMEL_HLCDC_RGB565_MODE;
148 case DRM_FORMAT_RGB888:
149 *mode = ATMEL_HLCDC_RGB888_MODE;
151 case DRM_FORMAT_ARGB1555:
152 *mode = ATMEL_HLCDC_ARGB1555_MODE;
154 case DRM_FORMAT_XRGB8888:
155 *mode = ATMEL_HLCDC_XRGB8888_MODE;
157 case DRM_FORMAT_ARGB8888:
158 *mode = ATMEL_HLCDC_ARGB8888_MODE;
160 case DRM_FORMAT_RGBA8888:
161 *mode = ATMEL_HLCDC_RGBA8888_MODE;
163 case DRM_FORMAT_AYUV:
164 *mode = ATMEL_HLCDC_AYUV_MODE;
166 case DRM_FORMAT_YUYV:
167 *mode = ATMEL_HLCDC_YUYV_MODE;
169 case DRM_FORMAT_UYVY:
170 *mode = ATMEL_HLCDC_UYVY_MODE;
172 case DRM_FORMAT_YVYU:
173 *mode = ATMEL_HLCDC_YVYU_MODE;
175 case DRM_FORMAT_VYUY:
176 *mode = ATMEL_HLCDC_VYUY_MODE;
178 case DRM_FORMAT_NV21:
179 *mode = ATMEL_HLCDC_NV21_MODE;
181 case DRM_FORMAT_NV61:
182 *mode = ATMEL_HLCDC_NV61_MODE;
184 case DRM_FORMAT_YUV420:
185 *mode = ATMEL_HLCDC_YUV420_MODE;
187 case DRM_FORMAT_YUV422:
188 *mode = ATMEL_HLCDC_YUV422_MODE;
197 static u32 heo_downscaling_xcoef[] = {
216 static u32 heo_downscaling_ycoef[] = {
227 static u32 heo_upscaling_xcoef[] = {
246 static u32 heo_upscaling_ycoef[] = {
257 #define ATMEL_HLCDC_XPHIDEF 4
258 #define ATMEL_HLCDC_YPHIDEF 4
260 static u32 atmel_hlcdc_plane_phiscaler_get_factor(u32 srcsize,
264 u32 factor, max_memsize;
266 factor = (256 * ((8 * (srcsize - 1)) - phidef)) / (dstsize - 1);
267 max_memsize = ((factor * (dstsize - 1)) + (256 * phidef)) / 2048;
269 if (max_memsize > srcsize - 1)
276 atmel_hlcdc_plane_scaler_set_phicoeff(struct atmel_hlcdc_plane *plane,
277 const u32 *coeff_tab, int size,
278 unsigned int cfg_offs)
282 for (i = 0; i < size; i++)
283 atmel_hlcdc_layer_write_cfg(&plane->layer, cfg_offs + i,
287 void atmel_hlcdc_plane_setup_scaler(struct atmel_hlcdc_plane *plane,
288 struct atmel_hlcdc_plane_state *state)
290 const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
291 u32 xfactor, yfactor;
293 if (!desc->layout.scaler_config)
296 if (state->crtc_w == state->src_w && state->crtc_h == state->src_h) {
297 atmel_hlcdc_layer_write_cfg(&plane->layer,
298 desc->layout.scaler_config, 0);
302 if (desc->layout.phicoeffs.x) {
303 xfactor = atmel_hlcdc_plane_phiscaler_get_factor(state->src_w,
305 ATMEL_HLCDC_XPHIDEF);
307 yfactor = atmel_hlcdc_plane_phiscaler_get_factor(state->src_h,
309 ATMEL_HLCDC_YPHIDEF);
311 atmel_hlcdc_plane_scaler_set_phicoeff(plane,
312 state->crtc_w < state->src_w ?
313 heo_downscaling_xcoef :
315 ARRAY_SIZE(heo_upscaling_xcoef),
316 desc->layout.phicoeffs.x);
318 atmel_hlcdc_plane_scaler_set_phicoeff(plane,
319 state->crtc_h < state->src_h ?
320 heo_downscaling_ycoef :
322 ARRAY_SIZE(heo_upscaling_ycoef),
323 desc->layout.phicoeffs.y);
325 xfactor = (1024 * state->src_w) / state->crtc_w;
326 yfactor = (1024 * state->src_h) / state->crtc_h;
329 atmel_hlcdc_layer_write_cfg(&plane->layer, desc->layout.scaler_config,
330 ATMEL_HLCDC_LAYER_SCALER_ENABLE |
331 ATMEL_HLCDC_LAYER_SCALER_FACTORS(xfactor,
336 atmel_hlcdc_plane_update_pos_and_size(struct atmel_hlcdc_plane *plane,
337 struct atmel_hlcdc_plane_state *state)
339 const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
341 if (desc->layout.size)
342 atmel_hlcdc_layer_write_cfg(&plane->layer, desc->layout.size,
343 ATMEL_HLCDC_LAYER_SIZE(state->crtc_w,
346 if (desc->layout.memsize)
347 atmel_hlcdc_layer_write_cfg(&plane->layer,
348 desc->layout.memsize,
349 ATMEL_HLCDC_LAYER_SIZE(state->src_w,
352 if (desc->layout.pos)
353 atmel_hlcdc_layer_write_cfg(&plane->layer, desc->layout.pos,
354 ATMEL_HLCDC_LAYER_POS(state->crtc_x,
357 atmel_hlcdc_plane_setup_scaler(plane, state);
361 atmel_hlcdc_plane_update_general_settings(struct atmel_hlcdc_plane *plane,
362 struct atmel_hlcdc_plane_state *state)
364 unsigned int cfg = ATMEL_HLCDC_LAYER_DMA_BLEN_INCR16 | state->ahb_id;
365 const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
366 const struct drm_format_info *format = state->base.fb->format;
369 * Rotation optimization is not working on RGB888 (rotation is still
370 * working but without any optimization).
372 if (format->format == DRM_FORMAT_RGB888)
373 cfg |= ATMEL_HLCDC_LAYER_DMA_ROTDIS;
375 atmel_hlcdc_layer_write_cfg(&plane->layer, ATMEL_HLCDC_LAYER_DMA_CFG,
378 cfg = ATMEL_HLCDC_LAYER_DMA;
380 if (plane->base.type != DRM_PLANE_TYPE_PRIMARY) {
381 cfg |= ATMEL_HLCDC_LAYER_OVR | ATMEL_HLCDC_LAYER_ITER2BL |
382 ATMEL_HLCDC_LAYER_ITER;
384 if (format->has_alpha)
385 cfg |= ATMEL_HLCDC_LAYER_LAEN;
387 cfg |= ATMEL_HLCDC_LAYER_GAEN |
388 ATMEL_HLCDC_LAYER_GA(state->alpha);
391 if (state->disc_h && state->disc_w)
392 cfg |= ATMEL_HLCDC_LAYER_DISCEN;
394 atmel_hlcdc_layer_write_cfg(&plane->layer, desc->layout.general_config,
398 static void atmel_hlcdc_plane_update_format(struct atmel_hlcdc_plane *plane,
399 struct atmel_hlcdc_plane_state *state)
404 ret = atmel_hlcdc_format_to_plane_mode(state->base.fb->format->format,
409 if ((state->base.fb->format->format == DRM_FORMAT_YUV422 ||
410 state->base.fb->format->format == DRM_FORMAT_NV61) &&
411 drm_rotation_90_or_270(state->base.rotation))
412 cfg |= ATMEL_HLCDC_YUV422ROT;
414 atmel_hlcdc_layer_write_cfg(&plane->layer,
415 ATMEL_HLCDC_LAYER_FORMAT_CFG, cfg);
418 static void atmel_hlcdc_plane_update_clut(struct atmel_hlcdc_plane *plane)
420 struct drm_crtc *crtc = plane->base.crtc;
421 struct drm_color_lut *lut;
424 if (!crtc || !crtc->state)
427 if (!crtc->state->color_mgmt_changed || !crtc->state->gamma_lut)
430 lut = (struct drm_color_lut *)crtc->state->gamma_lut->data;
432 for (idx = 0; idx < ATMEL_HLCDC_CLUT_SIZE; idx++, lut++) {
433 u32 val = ((lut->red << 8) & 0xff0000) |
434 (lut->green & 0xff00) |
437 atmel_hlcdc_layer_write_clut(&plane->layer, idx, val);
441 static void atmel_hlcdc_plane_update_buffers(struct atmel_hlcdc_plane *plane,
442 struct atmel_hlcdc_plane_state *state)
444 const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
445 struct drm_framebuffer *fb = state->base.fb;
449 sr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHSR);
451 for (i = 0; i < state->nplanes; i++) {
452 struct drm_gem_cma_object *gem = drm_fb_cma_get_gem_obj(fb, i);
454 state->dscrs[i]->addr = gem->paddr + state->offsets[i];
456 atmel_hlcdc_layer_write_reg(&plane->layer,
457 ATMEL_HLCDC_LAYER_PLANE_HEAD(i),
458 state->dscrs[i]->self);
460 if (!(sr & ATMEL_HLCDC_LAYER_EN)) {
461 atmel_hlcdc_layer_write_reg(&plane->layer,
462 ATMEL_HLCDC_LAYER_PLANE_ADDR(i),
463 state->dscrs[i]->addr);
464 atmel_hlcdc_layer_write_reg(&plane->layer,
465 ATMEL_HLCDC_LAYER_PLANE_CTRL(i),
466 state->dscrs[i]->ctrl);
467 atmel_hlcdc_layer_write_reg(&plane->layer,
468 ATMEL_HLCDC_LAYER_PLANE_NEXT(i),
469 state->dscrs[i]->self);
472 if (desc->layout.xstride[i])
473 atmel_hlcdc_layer_write_cfg(&plane->layer,
474 desc->layout.xstride[i],
477 if (desc->layout.pstride[i])
478 atmel_hlcdc_layer_write_cfg(&plane->layer,
479 desc->layout.pstride[i],
484 int atmel_hlcdc_plane_prepare_ahb_routing(struct drm_crtc_state *c_state)
486 unsigned int ahb_load[2] = { };
487 struct drm_plane *plane;
489 drm_atomic_crtc_state_for_each_plane(plane, c_state) {
490 struct atmel_hlcdc_plane_state *plane_state;
491 struct drm_plane_state *plane_s;
492 unsigned int pixels, load = 0;
495 plane_s = drm_atomic_get_plane_state(c_state->state, plane);
497 return PTR_ERR(plane_s);
500 drm_plane_state_to_atmel_hlcdc_plane_state(plane_s);
502 pixels = (plane_state->src_w * plane_state->src_h) -
503 (plane_state->disc_w * plane_state->disc_h);
505 for (i = 0; i < plane_state->nplanes; i++)
506 load += pixels * plane_state->bpp[i];
508 if (ahb_load[0] <= ahb_load[1])
509 plane_state->ahb_id = 0;
511 plane_state->ahb_id = 1;
513 ahb_load[plane_state->ahb_id] += load;
520 atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state)
522 int disc_x = 0, disc_y = 0, disc_w = 0, disc_h = 0;
523 const struct atmel_hlcdc_layer_cfg_layout *layout;
524 struct atmel_hlcdc_plane_state *primary_state;
525 struct drm_plane_state *primary_s;
526 struct atmel_hlcdc_plane *primary;
527 struct drm_plane *ovl;
529 primary = drm_plane_to_atmel_hlcdc_plane(c_state->crtc->primary);
530 layout = &primary->layer.desc->layout;
531 if (!layout->disc_pos || !layout->disc_size)
534 primary_s = drm_atomic_get_plane_state(c_state->state,
536 if (IS_ERR(primary_s))
537 return PTR_ERR(primary_s);
539 primary_state = drm_plane_state_to_atmel_hlcdc_plane_state(primary_s);
541 drm_atomic_crtc_state_for_each_plane(ovl, c_state) {
542 struct atmel_hlcdc_plane_state *ovl_state;
543 struct drm_plane_state *ovl_s;
545 if (ovl == c_state->crtc->primary)
548 ovl_s = drm_atomic_get_plane_state(c_state->state, ovl);
550 return PTR_ERR(ovl_s);
552 ovl_state = drm_plane_state_to_atmel_hlcdc_plane_state(ovl_s);
555 ovl_s->fb->format->has_alpha ||
556 ovl_state->alpha != 255)
559 /* TODO: implement a smarter hidden area detection */
560 if (ovl_state->crtc_h * ovl_state->crtc_w < disc_h * disc_w)
563 disc_x = ovl_state->crtc_x;
564 disc_y = ovl_state->crtc_y;
565 disc_h = ovl_state->crtc_h;
566 disc_w = ovl_state->crtc_w;
569 primary_state->disc_x = disc_x;
570 primary_state->disc_y = disc_y;
571 primary_state->disc_w = disc_w;
572 primary_state->disc_h = disc_h;
578 atmel_hlcdc_plane_update_disc_area(struct atmel_hlcdc_plane *plane,
579 struct atmel_hlcdc_plane_state *state)
581 const struct atmel_hlcdc_layer_cfg_layout *layout;
583 layout = &plane->layer.desc->layout;
584 if (!layout->disc_pos || !layout->disc_size)
587 atmel_hlcdc_layer_write_cfg(&plane->layer, layout->disc_pos,
588 ATMEL_HLCDC_LAYER_DISC_POS(state->disc_x,
591 atmel_hlcdc_layer_write_cfg(&plane->layer, layout->disc_size,
592 ATMEL_HLCDC_LAYER_DISC_SIZE(state->disc_w,
596 static int atmel_hlcdc_plane_atomic_check(struct drm_plane *p,
597 struct drm_plane_state *s)
599 struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
600 struct atmel_hlcdc_plane_state *state =
601 drm_plane_state_to_atmel_hlcdc_plane_state(s);
602 const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
603 struct drm_framebuffer *fb = state->base.fb;
604 const struct drm_display_mode *mode;
605 struct drm_crtc_state *crtc_state;
606 unsigned int patched_crtc_w;
607 unsigned int patched_crtc_h;
608 unsigned int patched_src_w;
609 unsigned int patched_src_h;
617 if (!state->base.crtc || !fb)
620 crtc_state = drm_atomic_get_existing_crtc_state(s->state, s->crtc);
621 mode = &crtc_state->adjusted_mode;
623 state->src_x = s->src_x;
624 state->src_y = s->src_y;
625 state->src_h = s->src_h;
626 state->src_w = s->src_w;
627 state->crtc_x = s->crtc_x;
628 state->crtc_y = s->crtc_y;
629 state->crtc_h = s->crtc_h;
630 state->crtc_w = s->crtc_w;
631 if ((state->src_x | state->src_y | state->src_w | state->src_h) &
640 state->nplanes = fb->format->num_planes;
641 if (state->nplanes > ATMEL_HLCDC_LAYER_MAX_PLANES)
645 * Swap width and size in case of 90 or 270 degrees rotation
647 if (drm_rotation_90_or_270(state->base.rotation)) {
649 state->crtc_w = state->crtc_h;
652 state->src_w = state->src_h;
656 if (state->crtc_x + state->crtc_w > mode->hdisplay)
657 patched_crtc_w = mode->hdisplay - state->crtc_x;
659 patched_crtc_w = state->crtc_w;
661 if (state->crtc_x < 0) {
662 patched_crtc_w += state->crtc_x;
663 x_offset = -state->crtc_x;
667 if (state->crtc_y + state->crtc_h > mode->vdisplay)
668 patched_crtc_h = mode->vdisplay - state->crtc_y;
670 patched_crtc_h = state->crtc_h;
672 if (state->crtc_y < 0) {
673 patched_crtc_h += state->crtc_y;
674 y_offset = -state->crtc_y;
678 patched_src_w = DIV_ROUND_CLOSEST(patched_crtc_w * state->src_w,
680 patched_src_h = DIV_ROUND_CLOSEST(patched_crtc_h * state->src_h,
683 hsub = drm_format_horz_chroma_subsampling(fb->format->format);
684 vsub = drm_format_vert_chroma_subsampling(fb->format->format);
686 for (i = 0; i < state->nplanes; i++) {
687 unsigned int offset = 0;
688 int xdiv = i ? hsub : 1;
689 int ydiv = i ? vsub : 1;
691 state->bpp[i] = fb->format->cpp[i];
695 switch (state->base.rotation & DRM_MODE_ROTATE_MASK) {
696 case DRM_MODE_ROTATE_90:
697 offset = ((y_offset + state->src_y + patched_src_w - 1) /
698 ydiv) * fb->pitches[i];
699 offset += ((x_offset + state->src_x) / xdiv) *
701 state->xstride[i] = ((patched_src_w - 1) / ydiv) *
703 state->pstride[i] = -fb->pitches[i] - state->bpp[i];
705 case DRM_MODE_ROTATE_180:
706 offset = ((y_offset + state->src_y + patched_src_h - 1) /
707 ydiv) * fb->pitches[i];
708 offset += ((x_offset + state->src_x + patched_src_w - 1) /
709 xdiv) * state->bpp[i];
710 state->xstride[i] = ((((patched_src_w - 1) / xdiv) - 1) *
711 state->bpp[i]) - fb->pitches[i];
712 state->pstride[i] = -2 * state->bpp[i];
714 case DRM_MODE_ROTATE_270:
715 offset = ((y_offset + state->src_y) / ydiv) *
717 offset += ((x_offset + state->src_x + patched_src_h - 1) /
718 xdiv) * state->bpp[i];
719 state->xstride[i] = -(((patched_src_w - 1) / ydiv) *
722 state->pstride[i] = fb->pitches[i] - state->bpp[i];
724 case DRM_MODE_ROTATE_0:
726 offset = ((y_offset + state->src_y) / ydiv) *
728 offset += ((x_offset + state->src_x) / xdiv) *
730 state->xstride[i] = fb->pitches[i] -
731 ((patched_src_w / xdiv) *
733 state->pstride[i] = 0;
737 state->offsets[i] = offset + fb->offsets[i];
740 state->src_w = patched_src_w;
741 state->src_h = patched_src_h;
742 state->crtc_w = patched_crtc_w;
743 state->crtc_h = patched_crtc_h;
745 if (!desc->layout.size &&
746 (mode->hdisplay != state->crtc_w ||
747 mode->vdisplay != state->crtc_h))
750 if (desc->max_height && state->crtc_h > desc->max_height)
753 if (desc->max_width && state->crtc_w > desc->max_width)
756 if ((state->crtc_h != state->src_h || state->crtc_w != state->src_w) &&
757 (!desc->layout.memsize ||
758 state->base.fb->format->has_alpha))
761 if (state->crtc_x < 0 || state->crtc_y < 0)
764 if (state->crtc_w + state->crtc_x > mode->hdisplay ||
765 state->crtc_h + state->crtc_y > mode->vdisplay)
771 static void atmel_hlcdc_plane_atomic_update(struct drm_plane *p,
772 struct drm_plane_state *old_s)
774 struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
775 struct atmel_hlcdc_plane_state *state =
776 drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
779 if (!p->state->crtc || !p->state->fb)
782 atmel_hlcdc_plane_update_pos_and_size(plane, state);
783 atmel_hlcdc_plane_update_general_settings(plane, state);
784 atmel_hlcdc_plane_update_format(plane, state);
785 atmel_hlcdc_plane_update_clut(plane);
786 atmel_hlcdc_plane_update_buffers(plane, state);
787 atmel_hlcdc_plane_update_disc_area(plane, state);
789 /* Enable the overrun interrupts. */
790 atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_IER,
791 ATMEL_HLCDC_LAYER_OVR_IRQ(0) |
792 ATMEL_HLCDC_LAYER_OVR_IRQ(1) |
793 ATMEL_HLCDC_LAYER_OVR_IRQ(2));
795 /* Apply the new config at the next SOF event. */
796 sr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHSR);
797 atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHER,
798 ATMEL_HLCDC_LAYER_UPDATE |
799 (sr & ATMEL_HLCDC_LAYER_EN ?
800 ATMEL_HLCDC_LAYER_A2Q : ATMEL_HLCDC_LAYER_EN));
803 static void atmel_hlcdc_plane_atomic_disable(struct drm_plane *p,
804 struct drm_plane_state *old_state)
806 struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
808 /* Disable interrupts */
809 atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_IDR,
812 /* Disable the layer */
813 atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHDR,
814 ATMEL_HLCDC_LAYER_RST |
815 ATMEL_HLCDC_LAYER_A2Q |
816 ATMEL_HLCDC_LAYER_UPDATE);
818 /* Clear all pending interrupts */
819 atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_ISR);
822 static void atmel_hlcdc_plane_destroy(struct drm_plane *p)
824 struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
827 drm_framebuffer_put(plane->base.fb);
829 drm_plane_cleanup(p);
832 static int atmel_hlcdc_plane_atomic_set_property(struct drm_plane *p,
833 struct drm_plane_state *s,
834 struct drm_property *property,
837 struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
838 struct atmel_hlcdc_plane_properties *props = plane->properties;
839 struct atmel_hlcdc_plane_state *state =
840 drm_plane_state_to_atmel_hlcdc_plane_state(s);
842 if (property == props->alpha)
850 static int atmel_hlcdc_plane_atomic_get_property(struct drm_plane *p,
851 const struct drm_plane_state *s,
852 struct drm_property *property,
855 struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
856 struct atmel_hlcdc_plane_properties *props = plane->properties;
857 const struct atmel_hlcdc_plane_state *state =
858 container_of(s, const struct atmel_hlcdc_plane_state, base);
860 if (property == props->alpha)
868 static int atmel_hlcdc_plane_init_properties(struct atmel_hlcdc_plane *plane,
869 struct atmel_hlcdc_plane_properties *props)
871 const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
873 if (desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
874 desc->type == ATMEL_HLCDC_CURSOR_LAYER)
875 drm_object_attach_property(&plane->base.base,
878 if (desc->layout.xstride && desc->layout.pstride) {
881 ret = drm_plane_create_rotation_property(&plane->base,
885 DRM_MODE_ROTATE_180 |
886 DRM_MODE_ROTATE_270);
891 if (desc->layout.csc) {
893 * TODO: decare a "yuv-to-rgb-conv-factors" property to let
894 * userspace modify these factors (using a BLOB property ?).
896 atmel_hlcdc_layer_write_cfg(&plane->layer,
899 atmel_hlcdc_layer_write_cfg(&plane->layer,
900 desc->layout.csc + 1,
902 atmel_hlcdc_layer_write_cfg(&plane->layer,
903 desc->layout.csc + 2,
910 void atmel_hlcdc_plane_irq(struct atmel_hlcdc_plane *plane)
912 const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
915 isr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_ISR);
918 * There's not much we can do in case of overrun except informing
919 * the user. However, we are in interrupt context here, hence the
923 (ATMEL_HLCDC_LAYER_OVR_IRQ(0) | ATMEL_HLCDC_LAYER_OVR_IRQ(1) |
924 ATMEL_HLCDC_LAYER_OVR_IRQ(2)))
925 dev_dbg(plane->base.dev->dev, "overrun on plane %s\n",
929 static const struct drm_plane_helper_funcs atmel_hlcdc_layer_plane_helper_funcs = {
930 .atomic_check = atmel_hlcdc_plane_atomic_check,
931 .atomic_update = atmel_hlcdc_plane_atomic_update,
932 .atomic_disable = atmel_hlcdc_plane_atomic_disable,
935 static int atmel_hlcdc_plane_alloc_dscrs(struct drm_plane *p,
936 struct atmel_hlcdc_plane_state *state)
938 struct atmel_hlcdc_dc *dc = p->dev->dev_private;
941 for (i = 0; i < ARRAY_SIZE(state->dscrs); i++) {
942 struct atmel_hlcdc_dma_channel_dscr *dscr;
945 dscr = dma_pool_alloc(dc->dscrpool, GFP_KERNEL, &dscr_dma);
950 dscr->next = dscr_dma;
951 dscr->self = dscr_dma;
952 dscr->ctrl = ATMEL_HLCDC_LAYER_DFETCH;
954 state->dscrs[i] = dscr;
960 for (i--; i >= 0; i--) {
961 dma_pool_free(dc->dscrpool, state->dscrs[i],
962 state->dscrs[i]->self);
968 static void atmel_hlcdc_plane_reset(struct drm_plane *p)
970 struct atmel_hlcdc_plane_state *state;
973 state = drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
976 drm_framebuffer_put(state->base.fb);
982 state = kzalloc(sizeof(*state), GFP_KERNEL);
984 if (atmel_hlcdc_plane_alloc_dscrs(p, state)) {
987 "Failed to allocate initial plane state\n");
992 p->state = &state->base;
997 static struct drm_plane_state *
998 atmel_hlcdc_plane_atomic_duplicate_state(struct drm_plane *p)
1000 struct atmel_hlcdc_plane_state *state =
1001 drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
1002 struct atmel_hlcdc_plane_state *copy;
1004 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1008 if (atmel_hlcdc_plane_alloc_dscrs(p, copy)) {
1014 drm_framebuffer_get(copy->base.fb);
1019 static void atmel_hlcdc_plane_atomic_destroy_state(struct drm_plane *p,
1020 struct drm_plane_state *s)
1022 struct atmel_hlcdc_plane_state *state =
1023 drm_plane_state_to_atmel_hlcdc_plane_state(s);
1024 struct atmel_hlcdc_dc *dc = p->dev->dev_private;
1027 for (i = 0; i < ARRAY_SIZE(state->dscrs); i++) {
1028 dma_pool_free(dc->dscrpool, state->dscrs[i],
1029 state->dscrs[i]->self);
1033 drm_framebuffer_put(s->fb);
1038 static const struct drm_plane_funcs layer_plane_funcs = {
1039 .update_plane = drm_atomic_helper_update_plane,
1040 .disable_plane = drm_atomic_helper_disable_plane,
1041 .destroy = atmel_hlcdc_plane_destroy,
1042 .reset = atmel_hlcdc_plane_reset,
1043 .atomic_duplicate_state = atmel_hlcdc_plane_atomic_duplicate_state,
1044 .atomic_destroy_state = atmel_hlcdc_plane_atomic_destroy_state,
1045 .atomic_set_property = atmel_hlcdc_plane_atomic_set_property,
1046 .atomic_get_property = atmel_hlcdc_plane_atomic_get_property,
1049 static int atmel_hlcdc_plane_create(struct drm_device *dev,
1050 const struct atmel_hlcdc_layer_desc *desc,
1051 struct atmel_hlcdc_plane_properties *props)
1053 struct atmel_hlcdc_dc *dc = dev->dev_private;
1054 struct atmel_hlcdc_plane *plane;
1055 enum drm_plane_type type;
1058 plane = devm_kzalloc(dev->dev, sizeof(*plane), GFP_KERNEL);
1062 atmel_hlcdc_layer_init(&plane->layer, desc, dc->hlcdc->regmap);
1063 plane->properties = props;
1065 if (desc->type == ATMEL_HLCDC_BASE_LAYER)
1066 type = DRM_PLANE_TYPE_PRIMARY;
1067 else if (desc->type == ATMEL_HLCDC_CURSOR_LAYER)
1068 type = DRM_PLANE_TYPE_CURSOR;
1070 type = DRM_PLANE_TYPE_OVERLAY;
1072 ret = drm_universal_plane_init(dev, &plane->base, 0,
1074 desc->formats->formats,
1075 desc->formats->nformats,
1080 drm_plane_helper_add(&plane->base,
1081 &atmel_hlcdc_layer_plane_helper_funcs);
1083 /* Set default property values*/
1084 ret = atmel_hlcdc_plane_init_properties(plane, props);
1088 dc->layers[desc->id] = &plane->layer;
1093 static struct atmel_hlcdc_plane_properties *
1094 atmel_hlcdc_plane_create_properties(struct drm_device *dev)
1096 struct atmel_hlcdc_plane_properties *props;
1098 props = devm_kzalloc(dev->dev, sizeof(*props), GFP_KERNEL);
1100 return ERR_PTR(-ENOMEM);
1102 props->alpha = drm_property_create_range(dev, 0, "alpha", 0, 255);
1104 return ERR_PTR(-ENOMEM);
1109 int atmel_hlcdc_create_planes(struct drm_device *dev)
1111 struct atmel_hlcdc_dc *dc = dev->dev_private;
1112 struct atmel_hlcdc_plane_properties *props;
1113 const struct atmel_hlcdc_layer_desc *descs = dc->desc->layers;
1114 int nlayers = dc->desc->nlayers;
1117 props = atmel_hlcdc_plane_create_properties(dev);
1119 return PTR_ERR(props);
1121 dc->dscrpool = dmam_pool_create("atmel-hlcdc-dscr", dev->dev,
1122 sizeof(struct atmel_hlcdc_dma_channel_dscr),
1127 for (i = 0; i < nlayers; i++) {
1128 if (descs[i].type != ATMEL_HLCDC_BASE_LAYER &&
1129 descs[i].type != ATMEL_HLCDC_OVERLAY_LAYER &&
1130 descs[i].type != ATMEL_HLCDC_CURSOR_LAYER)
1133 ret = atmel_hlcdc_plane_create(dev, &descs[i], props);