2 * Copyright (C) 2016 BayLibre, SAS
3 * Author: Neil Armstrong <narmstrong@baylibre.com>
4 * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
5 * Copyright (C) 2014 Endless Mobile
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 * Jasper St. Pierre <jstpierre@mecheye.net>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/platform_device.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_plane_helper.h>
32 #include <drm/drm_gem_cma_helper.h>
33 #include <drm/drm_fb_cma_helper.h>
34 #include <drm/drm_rect.h>
36 #include "meson_plane.h"
37 #include "meson_vpp.h"
38 #include "meson_viu.h"
39 #include "meson_canvas.h"
40 #include "meson_registers.h"
43 struct drm_plane base;
44 struct meson_drm *priv;
46 #define to_meson_plane(x) container_of(x, struct meson_plane, base)
48 static int meson_plane_atomic_check(struct drm_plane *plane,
49 struct drm_plane_state *state)
51 struct drm_crtc_state *crtc_state;
56 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
57 if (IS_ERR(crtc_state))
58 return PTR_ERR(crtc_state);
60 return drm_atomic_helper_check_plane_state(state, crtc_state,
61 DRM_PLANE_HELPER_NO_SCALING,
62 DRM_PLANE_HELPER_NO_SCALING,
66 /* Takes a fixed 16.16 number and converts it to integer. */
67 static inline int64_t fixed16_to_int(int64_t value)
72 static void meson_plane_atomic_update(struct drm_plane *plane,
73 struct drm_plane_state *old_state)
75 struct meson_plane *meson_plane = to_meson_plane(plane);
76 struct drm_plane_state *state = plane->state;
77 struct drm_framebuffer *fb = state->fb;
78 struct meson_drm *priv = meson_plane->priv;
79 struct drm_gem_cma_object *gem;
80 struct drm_rect src = {
83 .x2 = (state->src_x + state->src_w),
84 .y2 = (state->src_y + state->src_h),
86 struct drm_rect dest = {
89 .x2 = state->crtc_x + state->crtc_w,
90 .y2 = state->crtc_y + state->crtc_h,
100 spin_lock_irqsave(&priv->drm->event_lock, flags);
102 /* Enable OSD and BLK0, set max global alpha */
103 priv->viu.osd1_ctrl_stat = OSD_ENABLE |
104 (0x100 << OSD_GLOBAL_ALPHA_SHIFT) |
107 /* Set up BLK0 to point to the right canvas */
108 priv->viu.osd1_blk0_cfg[0] = ((MESON_CANVAS_ID_OSD1 << OSD_CANVAS_SEL) |
111 /* On GXBB, Use the old non-HDR RGB2YUV converter */
112 if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu"))
113 priv->viu.osd1_blk0_cfg[0] |= OSD_OUTPUT_COLOR_RGB;
115 switch (fb->format->format) {
116 case DRM_FORMAT_XRGB8888:
117 /* For XRGB, replace the pixel's alpha by 0xFF */
118 writel_bits_relaxed(OSD_REPLACE_EN, OSD_REPLACE_EN,
119 priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
120 priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
121 OSD_COLOR_MATRIX_32_ARGB;
123 case DRM_FORMAT_XBGR8888:
124 /* For XRGB, replace the pixel's alpha by 0xFF */
125 writel_bits_relaxed(OSD_REPLACE_EN, OSD_REPLACE_EN,
126 priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
127 priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
128 OSD_COLOR_MATRIX_32_ABGR;
130 case DRM_FORMAT_ARGB8888:
131 /* For ARGB, use the pixel's alpha */
132 writel_bits_relaxed(OSD_REPLACE_EN, 0,
133 priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
134 priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
135 OSD_COLOR_MATRIX_32_ARGB;
137 case DRM_FORMAT_ABGR8888:
138 /* For ARGB, use the pixel's alpha */
139 writel_bits_relaxed(OSD_REPLACE_EN, 0,
140 priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
141 priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
142 OSD_COLOR_MATRIX_32_ABGR;
144 case DRM_FORMAT_RGB888:
145 priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_24 |
146 OSD_COLOR_MATRIX_24_RGB;
148 case DRM_FORMAT_RGB565:
149 priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_16 |
150 OSD_COLOR_MATRIX_16_RGB565;
154 if (state->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) {
155 priv->viu.osd1_interlace = true;
160 priv->viu.osd1_interlace = false;
163 * The format of these registers is (x2 << 16 | x1),
164 * where x2 is exclusive.
165 * e.g. +30x1920 would be (1919 << 16) | 30
167 priv->viu.osd1_blk0_cfg[1] = ((fixed16_to_int(src.x2) - 1) << 16) |
168 fixed16_to_int(src.x1);
169 priv->viu.osd1_blk0_cfg[2] = ((fixed16_to_int(src.y2) - 1) << 16) |
170 fixed16_to_int(src.y1);
171 priv->viu.osd1_blk0_cfg[3] = ((dest.x2 - 1) << 16) | dest.x1;
172 priv->viu.osd1_blk0_cfg[4] = ((dest.y2 - 1) << 16) | dest.y1;
174 /* Update Canvas with buffer address */
175 gem = drm_fb_cma_get_gem_obj(fb, 0);
177 priv->viu.osd1_addr = gem->paddr;
178 priv->viu.osd1_stride = fb->pitches[0];
179 priv->viu.osd1_height = fb->height;
181 spin_unlock_irqrestore(&priv->drm->event_lock, flags);
184 static void meson_plane_atomic_disable(struct drm_plane *plane,
185 struct drm_plane_state *old_state)
187 struct meson_plane *meson_plane = to_meson_plane(plane);
188 struct meson_drm *priv = meson_plane->priv;
191 writel_bits_relaxed(VPP_OSD1_POSTBLEND, 0,
192 priv->io_base + _REG(VPP_MISC));
196 static const struct drm_plane_helper_funcs meson_plane_helper_funcs = {
197 .atomic_check = meson_plane_atomic_check,
198 .atomic_disable = meson_plane_atomic_disable,
199 .atomic_update = meson_plane_atomic_update,
202 static const struct drm_plane_funcs meson_plane_funcs = {
203 .update_plane = drm_atomic_helper_update_plane,
204 .disable_plane = drm_atomic_helper_disable_plane,
205 .destroy = drm_plane_cleanup,
206 .reset = drm_atomic_helper_plane_reset,
207 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
208 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
211 static const uint32_t supported_drm_formats[] = {
220 int meson_plane_create(struct meson_drm *priv)
222 struct meson_plane *meson_plane;
223 struct drm_plane *plane;
225 meson_plane = devm_kzalloc(priv->drm->dev, sizeof(*meson_plane),
230 meson_plane->priv = priv;
231 plane = &meson_plane->base;
233 drm_universal_plane_init(priv->drm, plane, 0xFF,
235 supported_drm_formats,
236 ARRAY_SIZE(supported_drm_formats),
238 DRM_PLANE_TYPE_PRIMARY, "meson_primary_plane");
240 drm_plane_helper_add(plane, &meson_plane_helper_funcs);
242 priv->primary_plane = plane;