2 * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
3 * Author: Liviu Dudau <Liviu.Dudau@arm.com>
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
10 * ARM Mali DP plane manipulation routines.
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_fb_cma_helper.h>
16 #include <drm/drm_gem_cma_helper.h>
17 #include <drm/drm_plane_helper.h>
19 #include "malidp_hw.h"
20 #include "malidp_drv.h"
22 /* Layer specific register offsets */
23 #define MALIDP_LAYER_FORMAT 0x000
24 #define MALIDP_LAYER_CONTROL 0x004
25 #define LAYER_ENABLE (1 << 0)
26 #define LAYER_ROT_OFFSET 8
27 #define LAYER_H_FLIP (1 << 10)
28 #define LAYER_V_FLIP (1 << 11)
29 #define LAYER_ROT_MASK (0xf << 8)
30 #define MALIDP_LAYER_SIZE 0x00c
31 #define LAYER_H_VAL(x) (((x) & 0x1fff) << 0)
32 #define LAYER_V_VAL(x) (((x) & 0x1fff) << 16)
33 #define MALIDP_LAYER_COMP_SIZE 0x010
34 #define MALIDP_LAYER_OFFSET 0x014
35 #define MALIDP_LAYER_STRIDE 0x018
37 static void malidp_de_plane_destroy(struct drm_plane *plane)
39 struct malidp_plane *mp = to_malidp_plane(plane);
42 drm_framebuffer_unreference(mp->base.fb);
44 drm_plane_helper_disable(plane);
45 drm_plane_cleanup(plane);
46 devm_kfree(plane->dev->dev, mp);
49 struct drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
51 struct malidp_plane_state *state, *m_state;
56 state = kmalloc(sizeof(*state), GFP_KERNEL);
58 m_state = to_malidp_plane_state(plane->state);
59 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
60 state->rotmem_size = m_state->rotmem_size;
66 void malidp_destroy_plane_state(struct drm_plane *plane,
67 struct drm_plane_state *state)
69 struct malidp_plane_state *m_state = to_malidp_plane_state(state);
71 __drm_atomic_helper_plane_destroy_state(state);
75 static const struct drm_plane_funcs malidp_de_plane_funcs = {
76 .update_plane = drm_atomic_helper_update_plane,
77 .disable_plane = drm_atomic_helper_disable_plane,
78 .destroy = malidp_de_plane_destroy,
79 .reset = drm_atomic_helper_plane_reset,
80 .atomic_duplicate_state = malidp_duplicate_plane_state,
81 .atomic_destroy_state = malidp_destroy_plane_state,
84 static int malidp_de_plane_check(struct drm_plane *plane,
85 struct drm_plane_state *state)
87 struct malidp_plane *mp = to_malidp_plane(plane);
88 struct malidp_plane_state *ms = to_malidp_plane_state(state);
92 if (!state->crtc || !state->fb)
95 format_id = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id,
96 state->fb->pixel_format);
97 if (format_id == MALIDP_INVALID_FORMAT_ID)
100 src_w = state->src_w >> 16;
101 src_h = state->src_h >> 16;
103 if ((state->crtc_w > mp->hwdev->max_line_size) ||
104 (state->crtc_h > mp->hwdev->max_line_size) ||
105 (state->crtc_w < mp->hwdev->min_line_size) ||
106 (state->crtc_h < mp->hwdev->min_line_size) ||
107 (state->crtc_w != src_w) || (state->crtc_h != src_h))
110 /* packed RGB888 / BGR888 can't be rotated or flipped */
111 if (state->rotation != DRM_ROTATE_0 &&
112 (state->fb->pixel_format == DRM_FORMAT_RGB888 ||
113 state->fb->pixel_format == DRM_FORMAT_BGR888))
117 if (state->rotation & MALIDP_ROTATED_MASK) {
120 val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
122 state->fb->pixel_format);
126 ms->rotmem_size = val;
132 static void malidp_de_plane_update(struct drm_plane *plane,
133 struct drm_plane_state *old_state)
135 struct drm_gem_cma_object *obj;
136 struct malidp_plane *mp;
137 const struct malidp_hw_regmap *map;
140 u32 format, src_w, src_h, dest_w, dest_h, val = 0;
143 mp = to_malidp_plane(plane);
145 map = &mp->hwdev->map;
146 format = plane->state->fb->pixel_format;
147 format_id = malidp_hw_get_format_id(map, mp->layer->id, format);
148 num_planes = drm_format_num_planes(format);
150 /* convert src values from Q16 fixed point to integer */
151 src_w = plane->state->src_w >> 16;
152 src_h = plane->state->src_h >> 16;
153 dest_w = plane->state->crtc_w;
154 dest_h = plane->state->crtc_h;
156 malidp_hw_write(mp->hwdev, format_id, mp->layer->base);
158 for (i = 0; i < num_planes; i++) {
159 /* calculate the offset for the layer's plane registers */
160 ptr = mp->layer->ptr + (i << 4);
162 obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
163 malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
164 malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
165 malidp_hw_write(mp->hwdev, plane->state->fb->pitches[i],
166 mp->layer->base + MALIDP_LAYER_STRIDE);
169 malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
170 mp->layer->base + MALIDP_LAYER_SIZE);
172 malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
173 mp->layer->base + MALIDP_LAYER_COMP_SIZE);
175 malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
176 LAYER_V_VAL(plane->state->crtc_y),
177 mp->layer->base + MALIDP_LAYER_OFFSET);
179 /* first clear the rotation bits in the register */
180 malidp_hw_clearbits(mp->hwdev, LAYER_ROT_MASK,
181 mp->layer->base + MALIDP_LAYER_CONTROL);
183 /* setup the rotation and axis flip bits */
184 if (plane->state->rotation & DRM_ROTATE_MASK)
185 val |= ilog2(plane->state->rotation & DRM_ROTATE_MASK) <<
187 if (plane->state->rotation & DRM_REFLECT_X)
189 if (plane->state->rotation & DRM_REFLECT_Y)
192 /* set the 'enable layer' bit */
195 malidp_hw_setbits(mp->hwdev, val,
196 mp->layer->base + MALIDP_LAYER_CONTROL);
199 static void malidp_de_plane_disable(struct drm_plane *plane,
200 struct drm_plane_state *state)
202 struct malidp_plane *mp = to_malidp_plane(plane);
204 malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE,
205 mp->layer->base + MALIDP_LAYER_CONTROL);
208 static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
209 .atomic_check = malidp_de_plane_check,
210 .atomic_update = malidp_de_plane_update,
211 .atomic_disable = malidp_de_plane_disable,
214 int malidp_de_planes_init(struct drm_device *drm)
216 struct malidp_drm *malidp = drm->dev_private;
217 const struct malidp_hw_regmap *map = &malidp->dev->map;
218 struct malidp_plane *plane = NULL;
219 enum drm_plane_type plane_type;
220 unsigned long crtcs = 1 << drm->mode_config.num_crtc;
224 formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL);
230 for (i = 0; i < map->n_layers; i++) {
231 u8 id = map->layers[i].id;
233 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
239 /* build the list of DRM supported formats based on the map */
240 for (n = 0, j = 0; j < map->n_input_formats; j++) {
241 if ((map->input_formats[j].layer & id) == id)
242 formats[n++] = map->input_formats[j].format;
245 plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
246 DRM_PLANE_TYPE_OVERLAY;
247 ret = drm_universal_plane_init(drm, &plane->base, crtcs,
248 &malidp_de_plane_funcs, formats,
249 n, plane_type, NULL);
253 if (!drm->mode_config.rotation_property) {
254 unsigned long flags = DRM_ROTATE_0 |
260 drm->mode_config.rotation_property =
261 drm_mode_create_rotation_property(drm, flags);
263 /* SMART layer can't be rotated */
264 if (drm->mode_config.rotation_property && (id != DE_SMART))
265 drm_object_attach_property(&plane->base.base,
266 drm->mode_config.rotation_property,
269 drm_plane_helper_add(&plane->base,
270 &malidp_de_plane_helper_funcs);
271 plane->hwdev = malidp->dev;
272 plane->layer = &map->layers[i];
280 malidp_de_planes_destroy(drm);
286 void malidp_de_planes_destroy(struct drm_device *drm)
288 struct drm_plane *p, *pt;
290 list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
291 drm_plane_cleanup(p);