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 if (plane
->state
->rotation
& MALIDP_ROTATED_MASK
) {
154 dest_w
= plane
->state
->crtc_h
;
155 dest_h
= plane
->state
->crtc_w
;
157 dest_w
= plane
->state
->crtc_w
;
158 dest_h
= plane
->state
->crtc_h
;
161 malidp_hw_write(mp
->hwdev
, format_id
, mp
->layer
->base
);
163 for (i
= 0; i
< num_planes
; i
++) {
164 /* calculate the offset for the layer's plane registers */
165 ptr
= mp
->layer
->ptr
+ (i
<< 4);
167 obj
= drm_fb_cma_get_gem_obj(plane
->state
->fb
, i
);
168 malidp_hw_write(mp
->hwdev
, lower_32_bits(obj
->paddr
), ptr
);
169 malidp_hw_write(mp
->hwdev
, upper_32_bits(obj
->paddr
), ptr
+ 4);
170 malidp_hw_write(mp
->hwdev
, plane
->state
->fb
->pitches
[i
],
171 mp
->layer
->base
+ MALIDP_LAYER_STRIDE
);
174 malidp_hw_write(mp
->hwdev
, LAYER_H_VAL(src_w
) | LAYER_V_VAL(src_h
),
175 mp
->layer
->base
+ MALIDP_LAYER_SIZE
);
177 malidp_hw_write(mp
->hwdev
, LAYER_H_VAL(dest_w
) | LAYER_V_VAL(dest_h
),
178 mp
->layer
->base
+ MALIDP_LAYER_COMP_SIZE
);
180 malidp_hw_write(mp
->hwdev
, LAYER_H_VAL(plane
->state
->crtc_x
) |
181 LAYER_V_VAL(plane
->state
->crtc_y
),
182 mp
->layer
->base
+ MALIDP_LAYER_OFFSET
);
184 /* first clear the rotation bits in the register */
185 malidp_hw_clearbits(mp
->hwdev
, LAYER_ROT_MASK
,
186 mp
->layer
->base
+ MALIDP_LAYER_CONTROL
);
188 /* setup the rotation and axis flip bits */
189 if (plane
->state
->rotation
& DRM_ROTATE_MASK
)
190 val
= ilog2(plane
->state
->rotation
& DRM_ROTATE_MASK
) << LAYER_ROT_OFFSET
;
191 if (plane
->state
->rotation
& DRM_REFLECT_X
)
193 if (plane
->state
->rotation
& DRM_REFLECT_Y
)
196 /* set the 'enable layer' bit */
199 malidp_hw_setbits(mp
->hwdev
, val
,
200 mp
->layer
->base
+ MALIDP_LAYER_CONTROL
);
203 static void malidp_de_plane_disable(struct drm_plane
*plane
,
204 struct drm_plane_state
*state
)
206 struct malidp_plane
*mp
= to_malidp_plane(plane
);
208 malidp_hw_clearbits(mp
->hwdev
, LAYER_ENABLE
,
209 mp
->layer
->base
+ MALIDP_LAYER_CONTROL
);
212 static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs
= {
213 .atomic_check
= malidp_de_plane_check
,
214 .atomic_update
= malidp_de_plane_update
,
215 .atomic_disable
= malidp_de_plane_disable
,
218 int malidp_de_planes_init(struct drm_device
*drm
)
220 struct malidp_drm
*malidp
= drm
->dev_private
;
221 const struct malidp_hw_regmap
*map
= &malidp
->dev
->map
;
222 struct malidp_plane
*plane
= NULL
;
223 enum drm_plane_type plane_type
;
224 unsigned long crtcs
= 1 << drm
->mode_config
.num_crtc
;
228 formats
= kcalloc(map
->n_input_formats
, sizeof(*formats
), GFP_KERNEL
);
234 for (i
= 0; i
< map
->n_layers
; i
++) {
235 u8 id
= map
->layers
[i
].id
;
237 plane
= kzalloc(sizeof(*plane
), GFP_KERNEL
);
243 /* build the list of DRM supported formats based on the map */
244 for (n
= 0, j
= 0; j
< map
->n_input_formats
; j
++) {
245 if ((map
->input_formats
[j
].layer
& id
) == id
)
246 formats
[n
++] = map
->input_formats
[j
].format
;
249 plane_type
= (i
== 0) ? DRM_PLANE_TYPE_PRIMARY
:
250 DRM_PLANE_TYPE_OVERLAY
;
251 ret
= drm_universal_plane_init(drm
, &plane
->base
, crtcs
,
252 &malidp_de_plane_funcs
, formats
,
253 n
, plane_type
, NULL
);
257 if (!drm
->mode_config
.rotation_property
) {
258 unsigned long flags
= DRM_ROTATE_0
|
264 drm
->mode_config
.rotation_property
=
265 drm_mode_create_rotation_property(drm
, flags
);
267 /* SMART layer can't be rotated */
268 if (drm
->mode_config
.rotation_property
&& (id
!= DE_SMART
))
269 drm_object_attach_property(&plane
->base
.base
,
270 drm
->mode_config
.rotation_property
,
273 drm_plane_helper_add(&plane
->base
,
274 &malidp_de_plane_helper_funcs
);
275 plane
->hwdev
= malidp
->dev
;
276 plane
->layer
= &map
->layers
[i
];
284 malidp_de_planes_destroy(drm
);
290 void malidp_de_planes_destroy(struct drm_device
*drm
)
292 struct drm_plane
*p
, *pt
;
294 list_for_each_entry_safe(p
, pt
, &drm
->mode_config
.plane_list
, head
) {
295 drm_plane_cleanup(p
);