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.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_fb_cma_helper.h>
17 #include <drm/drm_gem_cma_helper.h>
18 #include <drm/drm_plane_helper.h>
20 #include "malidp_hw.h"
21 #include "malidp_drv.h"
23 /* Layer specific register offsets */
24 #define MALIDP_LAYER_FORMAT 0x000
25 #define MALIDP_LAYER_CONTROL 0x004
26 #define LAYER_ENABLE (1 << 0)
27 #define LAYER_ROT_OFFSET 8
28 #define LAYER_H_FLIP (1 << 10)
29 #define LAYER_V_FLIP (1 << 11)
30 #define LAYER_ROT_MASK (0xf << 8)
31 #define LAYER_COMP_MASK (0x3 << 12)
32 #define LAYER_COMP_PIXEL (0x3 << 12)
33 #define LAYER_COMP_PLANE (0x2 << 12)
34 #define MALIDP_LAYER_COMPOSE 0x008
35 #define MALIDP_LAYER_SIZE 0x00c
36 #define LAYER_H_VAL(x) (((x) & 0x1fff) << 0)
37 #define LAYER_V_VAL(x) (((x) & 0x1fff) << 16)
38 #define MALIDP_LAYER_COMP_SIZE 0x010
39 #define MALIDP_LAYER_OFFSET 0x014
42 * This 4-entry look-up-table is used to determine the full 8-bit alpha value
43 * for formats with 1- or 2-bit alpha channels.
44 * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
45 * opacity for 2-bit formats.
47 #define MALIDP_ALPHA_LUT 0xffaa5500
49 static void malidp_de_plane_destroy(struct drm_plane
*plane
)
51 struct malidp_plane
*mp
= to_malidp_plane(plane
);
54 drm_framebuffer_unreference(mp
->base
.fb
);
56 drm_plane_helper_disable(plane
);
57 drm_plane_cleanup(plane
);
58 devm_kfree(plane
->dev
->dev
, mp
);
62 drm_plane_state
*malidp_duplicate_plane_state(struct drm_plane
*plane
)
64 struct malidp_plane_state
*state
, *m_state
;
69 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
73 m_state
= to_malidp_plane_state(plane
->state
);
74 __drm_atomic_helper_plane_duplicate_state(plane
, &state
->base
);
75 state
->rotmem_size
= m_state
->rotmem_size
;
76 state
->format
= m_state
->format
;
77 state
->n_planes
= m_state
->n_planes
;
82 static void malidp_destroy_plane_state(struct drm_plane
*plane
,
83 struct drm_plane_state
*state
)
85 struct malidp_plane_state
*m_state
= to_malidp_plane_state(state
);
87 __drm_atomic_helper_plane_destroy_state(state
);
91 static const struct drm_plane_funcs malidp_de_plane_funcs
= {
92 .update_plane
= drm_atomic_helper_update_plane
,
93 .disable_plane
= drm_atomic_helper_disable_plane
,
94 .set_property
= drm_atomic_helper_plane_set_property
,
95 .destroy
= malidp_de_plane_destroy
,
96 .reset
= drm_atomic_helper_plane_reset
,
97 .atomic_duplicate_state
= malidp_duplicate_plane_state
,
98 .atomic_destroy_state
= malidp_destroy_plane_state
,
101 static int malidp_de_plane_check(struct drm_plane
*plane
,
102 struct drm_plane_state
*state
)
104 struct malidp_plane
*mp
= to_malidp_plane(plane
);
105 struct malidp_plane_state
*ms
= to_malidp_plane_state(state
);
106 struct drm_crtc_state
*crtc_state
;
107 struct drm_framebuffer
*fb
;
108 struct drm_rect clip
= { 0 };
112 if (!state
->crtc
|| !state
->fb
)
117 ms
->format
= malidp_hw_get_format_id(&mp
->hwdev
->map
, mp
->layer
->id
,
119 if (ms
->format
== MALIDP_INVALID_FORMAT_ID
)
122 ms
->n_planes
= fb
->format
->num_planes
;
123 for (i
= 0; i
< ms
->n_planes
; i
++) {
124 if (!malidp_hw_pitch_valid(mp
->hwdev
, fb
->pitches
[i
])) {
125 DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
131 src_w
= state
->src_w
>> 16;
132 src_h
= state
->src_h
>> 16;
134 if ((state
->crtc_w
> mp
->hwdev
->max_line_size
) ||
135 (state
->crtc_h
> mp
->hwdev
->max_line_size
) ||
136 (state
->crtc_w
< mp
->hwdev
->min_line_size
) ||
137 (state
->crtc_h
< mp
->hwdev
->min_line_size
))
141 * DP550/650 video layers can accept 3 plane formats only if
142 * fb->pitches[1] == fb->pitches[2] since they don't have a
143 * third plane stride register.
145 if (ms
->n_planes
== 3 &&
146 !(mp
->hwdev
->features
& MALIDP_DEVICE_LV_HAS_3_STRIDES
) &&
147 (state
->fb
->pitches
[1] != state
->fb
->pitches
[2]))
150 /* packed RGB888 / BGR888 can't be rotated or flipped */
151 if (state
->rotation
!= DRM_ROTATE_0
&&
152 (fb
->format
->format
== DRM_FORMAT_RGB888
||
153 fb
->format
->format
== DRM_FORMAT_BGR888
))
156 crtc_state
= drm_atomic_get_existing_crtc_state(state
->state
, state
->crtc
);
157 clip
.x2
= crtc_state
->adjusted_mode
.hdisplay
;
158 clip
.y2
= crtc_state
->adjusted_mode
.vdisplay
;
159 ret
= drm_plane_helper_check_state(state
, &clip
,
160 DRM_PLANE_HELPER_NO_SCALING
,
161 DRM_PLANE_HELPER_NO_SCALING
,
167 if (state
->rotation
& MALIDP_ROTATED_MASK
) {
170 val
= mp
->hwdev
->rotmem_required(mp
->hwdev
, state
->crtc_h
,
176 ms
->rotmem_size
= val
;
182 static void malidp_de_set_plane_pitches(struct malidp_plane
*mp
,
183 int num_planes
, unsigned int pitches
[3])
186 int num_strides
= num_planes
;
188 if (!mp
->layer
->stride_offset
)
192 num_strides
= (mp
->hwdev
->features
&
193 MALIDP_DEVICE_LV_HAS_3_STRIDES
) ? 3 : 2;
195 for (i
= 0; i
< num_strides
; ++i
)
196 malidp_hw_write(mp
->hwdev
, pitches
[i
],
198 mp
->layer
->stride_offset
+ i
* 4);
201 static void malidp_de_plane_update(struct drm_plane
*plane
,
202 struct drm_plane_state
*old_state
)
204 struct drm_gem_cma_object
*obj
;
205 struct malidp_plane
*mp
;
206 const struct malidp_hw_regmap
*map
;
207 struct malidp_plane_state
*ms
= to_malidp_plane_state(plane
->state
);
209 u32 src_w
, src_h
, dest_w
, dest_h
, val
;
212 mp
= to_malidp_plane(plane
);
213 map
= &mp
->hwdev
->map
;
215 /* convert src values from Q16 fixed point to integer */
216 src_w
= plane
->state
->src_w
>> 16;
217 src_h
= plane
->state
->src_h
>> 16;
218 dest_w
= plane
->state
->crtc_w
;
219 dest_h
= plane
->state
->crtc_h
;
221 malidp_hw_write(mp
->hwdev
, ms
->format
, mp
->layer
->base
);
223 for (i
= 0; i
< ms
->n_planes
; i
++) {
224 /* calculate the offset for the layer's plane registers */
225 ptr
= mp
->layer
->ptr
+ (i
<< 4);
227 obj
= drm_fb_cma_get_gem_obj(plane
->state
->fb
, i
);
228 obj
->paddr
+= plane
->state
->fb
->offsets
[i
];
229 malidp_hw_write(mp
->hwdev
, lower_32_bits(obj
->paddr
), ptr
);
230 malidp_hw_write(mp
->hwdev
, upper_32_bits(obj
->paddr
), ptr
+ 4);
232 malidp_de_set_plane_pitches(mp
, ms
->n_planes
,
233 plane
->state
->fb
->pitches
);
235 malidp_hw_write(mp
->hwdev
, LAYER_H_VAL(src_w
) | LAYER_V_VAL(src_h
),
236 mp
->layer
->base
+ MALIDP_LAYER_SIZE
);
238 malidp_hw_write(mp
->hwdev
, LAYER_H_VAL(dest_w
) | LAYER_V_VAL(dest_h
),
239 mp
->layer
->base
+ MALIDP_LAYER_COMP_SIZE
);
241 malidp_hw_write(mp
->hwdev
, LAYER_H_VAL(plane
->state
->crtc_x
) |
242 LAYER_V_VAL(plane
->state
->crtc_y
),
243 mp
->layer
->base
+ MALIDP_LAYER_OFFSET
);
245 /* first clear the rotation bits */
246 val
= malidp_hw_read(mp
->hwdev
, mp
->layer
->base
+ MALIDP_LAYER_CONTROL
);
247 val
&= ~LAYER_ROT_MASK
;
249 /* setup the rotation and axis flip bits */
250 if (plane
->state
->rotation
& DRM_ROTATE_MASK
)
251 val
|= ilog2(plane
->state
->rotation
& DRM_ROTATE_MASK
) <<
253 if (plane
->state
->rotation
& DRM_REFLECT_X
)
255 if (plane
->state
->rotation
& DRM_REFLECT_Y
)
259 * always enable pixel alpha blending until we have a way to change
262 val
&= ~LAYER_COMP_MASK
;
263 val
|= LAYER_COMP_PIXEL
;
265 /* set the 'enable layer' bit */
268 malidp_hw_write(mp
->hwdev
, val
,
269 mp
->layer
->base
+ MALIDP_LAYER_CONTROL
);
272 static void malidp_de_plane_disable(struct drm_plane
*plane
,
273 struct drm_plane_state
*state
)
275 struct malidp_plane
*mp
= to_malidp_plane(plane
);
277 malidp_hw_clearbits(mp
->hwdev
, LAYER_ENABLE
,
278 mp
->layer
->base
+ MALIDP_LAYER_CONTROL
);
281 static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs
= {
282 .atomic_check
= malidp_de_plane_check
,
283 .atomic_update
= malidp_de_plane_update
,
284 .atomic_disable
= malidp_de_plane_disable
,
287 int malidp_de_planes_init(struct drm_device
*drm
)
289 struct malidp_drm
*malidp
= drm
->dev_private
;
290 const struct malidp_hw_regmap
*map
= &malidp
->dev
->map
;
291 struct malidp_plane
*plane
= NULL
;
292 enum drm_plane_type plane_type
;
293 unsigned long crtcs
= 1 << drm
->mode_config
.num_crtc
;
294 unsigned long flags
= DRM_ROTATE_0
| DRM_ROTATE_90
| DRM_ROTATE_180
|
295 DRM_ROTATE_270
| DRM_REFLECT_X
| DRM_REFLECT_Y
;
299 formats
= kcalloc(map
->n_pixel_formats
, sizeof(*formats
), GFP_KERNEL
);
305 for (i
= 0; i
< map
->n_layers
; i
++) {
306 u8 id
= map
->layers
[i
].id
;
308 plane
= kzalloc(sizeof(*plane
), GFP_KERNEL
);
314 /* build the list of DRM supported formats based on the map */
315 for (n
= 0, j
= 0; j
< map
->n_pixel_formats
; j
++) {
316 if ((map
->pixel_formats
[j
].layer
& id
) == id
)
317 formats
[n
++] = map
->pixel_formats
[j
].format
;
320 plane_type
= (i
== 0) ? DRM_PLANE_TYPE_PRIMARY
:
321 DRM_PLANE_TYPE_OVERLAY
;
322 ret
= drm_universal_plane_init(drm
, &plane
->base
, crtcs
,
323 &malidp_de_plane_funcs
, formats
,
324 n
, plane_type
, NULL
);
328 drm_plane_helper_add(&plane
->base
,
329 &malidp_de_plane_helper_funcs
);
330 plane
->hwdev
= malidp
->dev
;
331 plane
->layer
= &map
->layers
[i
];
333 /* Skip the features which the SMART layer doesn't have */
337 drm_plane_create_rotation_property(&plane
->base
, DRM_ROTATE_0
, flags
);
338 malidp_hw_write(malidp
->dev
, MALIDP_ALPHA_LUT
,
339 plane
->layer
->base
+ MALIDP_LAYER_COMPOSE
);
347 malidp_de_planes_destroy(drm
);
353 void malidp_de_planes_destroy(struct drm_device
*drm
)
355 struct drm_plane
*p
, *pt
;
357 list_for_each_entry_safe(p
, pt
, &drm
->mode_config
.plane_list
, head
) {
358 drm_plane_cleanup(p
);