2 * Copyright (C) 2016 Noralf Trønnes
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_crtc_helper.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drm_simple_kms_helper.h>
16 #include <linux/slab.h>
21 * This helper library provides helpers for drivers for simple display
24 * drm_simple_display_pipe_init() initializes a simple display pipeline
25 * which has only one full-screen scanout buffer feeding one output. The
26 * pipeline is represented by struct &drm_simple_display_pipe and binds
27 * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
28 * entity. Some flexibility for code reuse is provided through a separately
29 * allocated &drm_connector object and supporting optional &drm_bridge
33 static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs
= {
34 .destroy
= drm_encoder_cleanup
,
37 static void drm_simple_kms_crtc_enable(struct drm_crtc
*crtc
)
39 struct drm_simple_display_pipe
*pipe
;
41 pipe
= container_of(crtc
, struct drm_simple_display_pipe
, crtc
);
42 if (!pipe
->funcs
|| !pipe
->funcs
->enable
)
45 pipe
->funcs
->enable(pipe
, crtc
->state
);
48 static void drm_simple_kms_crtc_disable(struct drm_crtc
*crtc
)
50 struct drm_simple_display_pipe
*pipe
;
52 pipe
= container_of(crtc
, struct drm_simple_display_pipe
, crtc
);
53 if (!pipe
->funcs
|| !pipe
->funcs
->disable
)
56 pipe
->funcs
->disable(pipe
);
59 static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs
= {
60 .disable
= drm_simple_kms_crtc_disable
,
61 .enable
= drm_simple_kms_crtc_enable
,
64 static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs
= {
65 .reset
= drm_atomic_helper_crtc_reset
,
66 .destroy
= drm_crtc_cleanup
,
67 .set_config
= drm_atomic_helper_set_config
,
68 .page_flip
= drm_atomic_helper_page_flip
,
69 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
70 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
73 static int drm_simple_kms_plane_atomic_check(struct drm_plane
*plane
,
74 struct drm_plane_state
*plane_state
)
76 struct drm_rect src
= {
77 .x1
= plane_state
->src_x
,
78 .y1
= plane_state
->src_y
,
79 .x2
= plane_state
->src_x
+ plane_state
->src_w
,
80 .y2
= plane_state
->src_y
+ plane_state
->src_h
,
82 struct drm_rect dest
= {
83 .x1
= plane_state
->crtc_x
,
84 .y1
= plane_state
->crtc_y
,
85 .x2
= plane_state
->crtc_x
+ plane_state
->crtc_w
,
86 .y2
= plane_state
->crtc_y
+ plane_state
->crtc_h
,
88 struct drm_rect clip
= { 0 };
89 struct drm_simple_display_pipe
*pipe
;
90 struct drm_crtc_state
*crtc_state
;
94 pipe
= container_of(plane
, struct drm_simple_display_pipe
, plane
);
95 crtc_state
= drm_atomic_get_existing_crtc_state(plane_state
->state
,
97 if (crtc_state
->enable
!= !!plane_state
->crtc
)
98 return -EINVAL
; /* plane must match crtc enable state */
100 if (!crtc_state
->enable
)
101 return 0; /* nothing to check when disabling or disabled */
103 clip
.x2
= crtc_state
->adjusted_mode
.hdisplay
;
104 clip
.y2
= crtc_state
->adjusted_mode
.vdisplay
;
105 ret
= drm_plane_helper_check_update(plane
, &pipe
->crtc
,
108 plane_state
->rotation
,
109 DRM_PLANE_HELPER_NO_SCALING
,
110 DRM_PLANE_HELPER_NO_SCALING
,
111 false, true, &visible
);
118 if (!pipe
->funcs
|| !pipe
->funcs
->check
)
121 return pipe
->funcs
->check(pipe
, plane_state
, crtc_state
);
124 static void drm_simple_kms_plane_atomic_update(struct drm_plane
*plane
,
125 struct drm_plane_state
*pstate
)
127 struct drm_simple_display_pipe
*pipe
;
129 pipe
= container_of(plane
, struct drm_simple_display_pipe
, plane
);
130 if (!pipe
->funcs
|| !pipe
->funcs
->update
)
133 pipe
->funcs
->update(pipe
, pstate
);
136 static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs
= {
137 .atomic_check
= drm_simple_kms_plane_atomic_check
,
138 .atomic_update
= drm_simple_kms_plane_atomic_update
,
141 static const struct drm_plane_funcs drm_simple_kms_plane_funcs
= {
142 .update_plane
= drm_atomic_helper_update_plane
,
143 .disable_plane
= drm_atomic_helper_disable_plane
,
144 .destroy
= drm_plane_cleanup
,
145 .reset
= drm_atomic_helper_plane_reset
,
146 .atomic_duplicate_state
= drm_atomic_helper_plane_duplicate_state
,
147 .atomic_destroy_state
= drm_atomic_helper_plane_destroy_state
,
151 * drm_simple_display_pipe_init - Initialize a simple display pipeline
153 * @pipe: simple display pipe object to initialize
154 * @funcs: callbacks for the display pipe (optional)
155 * @formats: array of supported formats (%DRM_FORMAT_*)
156 * @format_count: number of elements in @formats
157 * @connector: connector to attach and register
159 * Sets up a display pipeline which consist of a really simple
160 * plane-crtc-encoder pipe coupled with the provided connector.
161 * Teardown of a simple display pipe is all handled automatically by the drm
162 * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
163 * release the memory for the structure themselves.
166 * Zero on success, negative error code on failure.
168 int drm_simple_display_pipe_init(struct drm_device
*dev
,
169 struct drm_simple_display_pipe
*pipe
,
170 const struct drm_simple_display_pipe_funcs
*funcs
,
171 const uint32_t *formats
, unsigned int format_count
,
172 struct drm_connector
*connector
)
174 struct drm_encoder
*encoder
= &pipe
->encoder
;
175 struct drm_plane
*plane
= &pipe
->plane
;
176 struct drm_crtc
*crtc
= &pipe
->crtc
;
179 pipe
->connector
= connector
;
182 drm_plane_helper_add(plane
, &drm_simple_kms_plane_helper_funcs
);
183 ret
= drm_universal_plane_init(dev
, plane
, 0,
184 &drm_simple_kms_plane_funcs
,
185 formats
, format_count
,
186 DRM_PLANE_TYPE_PRIMARY
, NULL
);
190 drm_crtc_helper_add(crtc
, &drm_simple_kms_crtc_helper_funcs
);
191 ret
= drm_crtc_init_with_planes(dev
, crtc
, plane
, NULL
,
192 &drm_simple_kms_crtc_funcs
, NULL
);
196 encoder
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
197 ret
= drm_encoder_init(dev
, encoder
, &drm_simple_kms_encoder_funcs
,
198 DRM_MODE_ENCODER_NONE
, NULL
);
202 return drm_mode_connector_attach_encoder(connector
, encoder
);
204 EXPORT_SYMBOL(drm_simple_display_pipe_init
);
206 MODULE_LICENSE("GPL");