1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
4 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
7 #include <drm/drm_atomic.h>
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_bridge.h>
10 #include <drm/drm_crtc_helper.h>
11 #include <drm/drm_fb_cma_helper.h>
12 #include <drm/drm_fb_helper.h>
13 #include <drm/drm_gem_framebuffer_helper.h>
14 #include <drm/drm_of.h>
15 #include <drm/drm_panel.h>
16 #include <drm/drm_vblank.h>
18 #include "tidss_crtc.h"
19 #include "tidss_dispc.h"
20 #include "tidss_drv.h"
21 #include "tidss_encoder.h"
22 #include "tidss_kms.h"
23 #include "tidss_plane.h"
25 static void tidss_atomic_commit_tail(struct drm_atomic_state
*old_state
)
27 struct drm_device
*ddev
= old_state
->dev
;
28 struct tidss_device
*tidss
= to_tidss(ddev
);
30 dev_dbg(ddev
->dev
, "%s\n", __func__
);
32 tidss_runtime_get(tidss
);
34 drm_atomic_helper_commit_modeset_disables(ddev
, old_state
);
35 drm_atomic_helper_commit_planes(ddev
, old_state
, 0);
36 drm_atomic_helper_commit_modeset_enables(ddev
, old_state
);
38 drm_atomic_helper_commit_hw_done(old_state
);
39 drm_atomic_helper_wait_for_flip_done(ddev
, old_state
);
41 drm_atomic_helper_cleanup_planes(ddev
, old_state
);
43 tidss_runtime_put(tidss
);
46 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs
= {
47 .atomic_commit_tail
= tidss_atomic_commit_tail
,
50 static int tidss_atomic_check(struct drm_device
*ddev
,
51 struct drm_atomic_state
*state
)
53 struct drm_plane_state
*opstate
;
54 struct drm_plane_state
*npstate
;
55 struct drm_plane
*plane
;
56 struct drm_crtc_state
*cstate
;
57 struct drm_crtc
*crtc
;
60 ret
= drm_atomic_helper_check(ddev
, state
);
65 * Add all active planes on a CRTC to the atomic state, if
66 * x/y/z position or activity of any plane on that CRTC
67 * changes. This is needed for updating the plane positions in
68 * tidss_crtc_position_planes() which is called from
69 * crtc_atomic_enable() and crtc_atomic_flush(). We have an
70 * extra flag to to mark x,y-position changes and together
71 * with zpos_changed the condition recognizes all the above
74 for_each_oldnew_plane_in_state(state
, plane
, opstate
, npstate
, i
) {
75 if (!npstate
->crtc
|| !npstate
->visible
)
78 if (!opstate
->crtc
|| opstate
->crtc_x
!= npstate
->crtc_x
||
79 opstate
->crtc_y
!= npstate
->crtc_y
) {
80 cstate
= drm_atomic_get_crtc_state(state
,
83 return PTR_ERR(cstate
);
84 to_tidss_crtc_state(cstate
)->plane_pos_changed
= true;
88 for_each_new_crtc_in_state(state
, crtc
, cstate
, i
) {
89 if (to_tidss_crtc_state(cstate
)->plane_pos_changed
||
90 cstate
->zpos_changed
) {
91 ret
= drm_atomic_add_affected_planes(state
, crtc
);
100 static const struct drm_mode_config_funcs mode_config_funcs
= {
101 .fb_create
= drm_gem_fb_create
,
102 .atomic_check
= tidss_atomic_check
,
103 .atomic_commit
= drm_atomic_helper_commit
,
106 static int tidss_dispc_modeset_init(struct tidss_device
*tidss
)
108 struct device
*dev
= tidss
->dev
;
109 unsigned int fourccs_len
;
110 const u32
*fourccs
= dispc_plane_formats(tidss
->dispc
, &fourccs_len
);
115 struct drm_bridge
*bridge
;
119 const struct dispc_features
*feat
= tidss
->feat
;
120 u32 max_vps
= feat
->num_vps
;
121 u32 max_planes
= feat
->num_planes
;
123 struct pipe pipes
[TIDSS_MAX_PORTS
];
127 /* first find all the connected panels & bridges */
129 for (i
= 0; i
< max_vps
; i
++) {
130 struct drm_panel
*panel
;
131 struct drm_bridge
*bridge
;
132 u32 enc_type
= DRM_MODE_ENCODER_NONE
;
135 ret
= drm_of_find_panel_or_bridge(dev
->of_node
, i
, 0,
137 if (ret
== -ENODEV
) {
138 dev_dbg(dev
, "no panel/bridge for port %d\n", i
);
141 dev_dbg(dev
, "port %d probe returned %d\n", i
, ret
);
148 dev_dbg(dev
, "Setting up panel for port %d\n", i
);
150 switch (feat
->vp_bus_type
[i
]) {
152 enc_type
= DRM_MODE_ENCODER_LVDS
;
153 conn_type
= DRM_MODE_CONNECTOR_LVDS
;
156 enc_type
= DRM_MODE_ENCODER_DPI
;
157 conn_type
= DRM_MODE_CONNECTOR_LVDS
;
164 if (panel
->connector_type
!= conn_type
) {
166 "%s: Panel %s has incompatible connector type for vp%d (%d != %d)\n",
167 __func__
, dev_name(panel
->dev
), i
,
168 panel
->connector_type
, conn_type
);
172 bridge
= devm_drm_panel_bridge_add(dev
, panel
);
173 if (IS_ERR(bridge
)) {
175 "failed to set up panel bridge for port %d\n",
177 return PTR_ERR(bridge
);
181 pipes
[num_pipes
].hw_videoport
= i
;
182 pipes
[num_pipes
].bridge
= bridge
;
183 pipes
[num_pipes
].enc_type
= enc_type
;
187 /* all planes can be on any crtc */
188 crtc_mask
= (1 << num_pipes
) - 1;
190 /* then create a plane, a crtc and an encoder for each panel/bridge */
192 for (i
= 0; i
< num_pipes
; ++i
) {
193 struct tidss_plane
*tplane
;
194 struct tidss_crtc
*tcrtc
;
195 struct drm_encoder
*enc
;
196 u32 hw_plane_id
= feat
->vid_order
[tidss
->num_planes
];
199 tplane
= tidss_plane_create(tidss
, hw_plane_id
,
200 DRM_PLANE_TYPE_PRIMARY
, crtc_mask
,
201 fourccs
, fourccs_len
);
202 if (IS_ERR(tplane
)) {
203 dev_err(tidss
->dev
, "plane create failed\n");
204 return PTR_ERR(tplane
);
207 tidss
->planes
[tidss
->num_planes
++] = &tplane
->plane
;
209 tcrtc
= tidss_crtc_create(tidss
, pipes
[i
].hw_videoport
,
212 dev_err(tidss
->dev
, "crtc create failed\n");
213 return PTR_ERR(tcrtc
);
216 tidss
->crtcs
[tidss
->num_crtcs
++] = &tcrtc
->crtc
;
218 enc
= tidss_encoder_create(tidss
, pipes
[i
].enc_type
,
219 1 << tcrtc
->crtc
.index
);
221 dev_err(tidss
->dev
, "encoder create failed\n");
225 ret
= drm_bridge_attach(enc
, pipes
[i
].bridge
, NULL
, 0);
227 dev_err(tidss
->dev
, "bridge attach failed: %d\n", ret
);
232 /* create overlay planes of the leftover planes */
234 while (tidss
->num_planes
< max_planes
) {
235 struct tidss_plane
*tplane
;
236 u32 hw_plane_id
= feat
->vid_order
[tidss
->num_planes
];
238 tplane
= tidss_plane_create(tidss
, hw_plane_id
,
239 DRM_PLANE_TYPE_OVERLAY
, crtc_mask
,
240 fourccs
, fourccs_len
);
242 if (IS_ERR(tplane
)) {
243 dev_err(tidss
->dev
, "plane create failed\n");
244 return PTR_ERR(tplane
);
247 tidss
->planes
[tidss
->num_planes
++] = &tplane
->plane
;
253 int tidss_modeset_init(struct tidss_device
*tidss
)
255 struct drm_device
*ddev
= &tidss
->ddev
;
258 dev_dbg(tidss
->dev
, "%s\n", __func__
);
260 ret
= drmm_mode_config_init(ddev
);
264 ddev
->mode_config
.min_width
= 8;
265 ddev
->mode_config
.min_height
= 8;
266 ddev
->mode_config
.max_width
= 8096;
267 ddev
->mode_config
.max_height
= 8096;
268 ddev
->mode_config
.normalize_zpos
= true;
269 ddev
->mode_config
.funcs
= &mode_config_funcs
;
270 ddev
->mode_config
.helper_private
= &mode_config_helper_funcs
;
272 ret
= tidss_dispc_modeset_init(tidss
);
276 ret
= drm_vblank_init(ddev
, tidss
->num_crtcs
);
280 drm_mode_config_reset(ddev
);
282 dev_dbg(tidss
->dev
, "%s done\n", __func__
);