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 <linux/dma-fence.h>
9 #include <drm/drm_atomic.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_bridge.h>
12 #include <drm/drm_crtc_helper.h>
13 #include <drm/drm_fb_cma_helper.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_of.h>
17 #include <drm/drm_panel.h>
18 #include <drm/drm_vblank.h>
20 #include "tidss_crtc.h"
21 #include "tidss_dispc.h"
22 #include "tidss_drv.h"
23 #include "tidss_encoder.h"
24 #include "tidss_kms.h"
25 #include "tidss_plane.h"
27 static void tidss_atomic_commit_tail(struct drm_atomic_state *old_state)
29 struct drm_device *ddev = old_state->dev;
30 struct tidss_device *tidss = to_tidss(ddev);
31 bool fence_cookie = dma_fence_begin_signalling();
33 dev_dbg(ddev->dev, "%s\n", __func__);
35 tidss_runtime_get(tidss);
37 drm_atomic_helper_commit_modeset_disables(ddev, old_state);
38 drm_atomic_helper_commit_planes(ddev, old_state, 0);
39 drm_atomic_helper_commit_modeset_enables(ddev, old_state);
41 drm_atomic_helper_commit_hw_done(old_state);
42 dma_fence_end_signalling(fence_cookie);
43 drm_atomic_helper_wait_for_flip_done(ddev, old_state);
45 drm_atomic_helper_cleanup_planes(ddev, old_state);
47 tidss_runtime_put(tidss);
50 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
51 .atomic_commit_tail = tidss_atomic_commit_tail,
54 static int tidss_atomic_check(struct drm_device *ddev,
55 struct drm_atomic_state *state)
57 struct drm_plane_state *opstate;
58 struct drm_plane_state *npstate;
59 struct drm_plane *plane;
60 struct drm_crtc_state *cstate;
61 struct drm_crtc *crtc;
64 ret = drm_atomic_helper_check(ddev, state);
69 * Add all active planes on a CRTC to the atomic state, if
70 * x/y/z position or activity of any plane on that CRTC
71 * changes. This is needed for updating the plane positions in
72 * tidss_crtc_position_planes() which is called from
73 * crtc_atomic_enable() and crtc_atomic_flush(). We have an
74 * extra flag to to mark x,y-position changes and together
75 * with zpos_changed the condition recognizes all the above
78 for_each_oldnew_plane_in_state(state, plane, opstate, npstate, i) {
79 if (!npstate->crtc || !npstate->visible)
82 if (!opstate->crtc || opstate->crtc_x != npstate->crtc_x ||
83 opstate->crtc_y != npstate->crtc_y) {
84 cstate = drm_atomic_get_crtc_state(state,
87 return PTR_ERR(cstate);
88 to_tidss_crtc_state(cstate)->plane_pos_changed = true;
92 for_each_new_crtc_in_state(state, crtc, cstate, i) {
93 if (to_tidss_crtc_state(cstate)->plane_pos_changed ||
94 cstate->zpos_changed) {
95 ret = drm_atomic_add_affected_planes(state, crtc);
104 static const struct drm_mode_config_funcs mode_config_funcs = {
105 .fb_create = drm_gem_fb_create,
106 .atomic_check = tidss_atomic_check,
107 .atomic_commit = drm_atomic_helper_commit,
110 static int tidss_dispc_modeset_init(struct tidss_device *tidss)
112 struct device *dev = tidss->dev;
113 unsigned int fourccs_len;
114 const u32 *fourccs = dispc_plane_formats(tidss->dispc, &fourccs_len);
119 struct drm_bridge *bridge;
123 const struct dispc_features *feat = tidss->feat;
124 u32 max_vps = feat->num_vps;
125 u32 max_planes = feat->num_planes;
127 struct pipe pipes[TIDSS_MAX_PORTS];
131 /* first find all the connected panels & bridges */
133 for (i = 0; i < max_vps; i++) {
134 struct drm_panel *panel;
135 struct drm_bridge *bridge;
136 u32 enc_type = DRM_MODE_ENCODER_NONE;
139 ret = drm_of_find_panel_or_bridge(dev->of_node, i, 0,
141 if (ret == -ENODEV) {
142 dev_dbg(dev, "no panel/bridge for port %d\n", i);
145 dev_dbg(dev, "port %d probe returned %d\n", i, ret);
152 dev_dbg(dev, "Setting up panel for port %d\n", i);
154 switch (feat->vp_bus_type[i]) {
156 enc_type = DRM_MODE_ENCODER_LVDS;
157 conn_type = DRM_MODE_CONNECTOR_LVDS;
160 enc_type = DRM_MODE_ENCODER_DPI;
161 conn_type = DRM_MODE_CONNECTOR_DPI;
168 if (panel->connector_type != conn_type) {
170 "%s: Panel %s has incompatible connector type for vp%d (%d != %d)\n",
171 __func__, dev_name(panel->dev), i,
172 panel->connector_type, conn_type);
176 bridge = devm_drm_panel_bridge_add(dev, panel);
177 if (IS_ERR(bridge)) {
179 "failed to set up panel bridge for port %d\n",
181 return PTR_ERR(bridge);
185 pipes[num_pipes].hw_videoport = i;
186 pipes[num_pipes].bridge = bridge;
187 pipes[num_pipes].enc_type = enc_type;
191 /* all planes can be on any crtc */
192 crtc_mask = (1 << num_pipes) - 1;
194 /* then create a plane, a crtc and an encoder for each panel/bridge */
196 for (i = 0; i < num_pipes; ++i) {
197 struct tidss_plane *tplane;
198 struct tidss_crtc *tcrtc;
199 struct drm_encoder *enc;
200 u32 hw_plane_id = feat->vid_order[tidss->num_planes];
203 tplane = tidss_plane_create(tidss, hw_plane_id,
204 DRM_PLANE_TYPE_PRIMARY, crtc_mask,
205 fourccs, fourccs_len);
206 if (IS_ERR(tplane)) {
207 dev_err(tidss->dev, "plane create failed\n");
208 return PTR_ERR(tplane);
211 tidss->planes[tidss->num_planes++] = &tplane->plane;
213 tcrtc = tidss_crtc_create(tidss, pipes[i].hw_videoport,
216 dev_err(tidss->dev, "crtc create failed\n");
217 return PTR_ERR(tcrtc);
220 tidss->crtcs[tidss->num_crtcs++] = &tcrtc->crtc;
222 enc = tidss_encoder_create(tidss, pipes[i].enc_type,
223 1 << tcrtc->crtc.index);
225 dev_err(tidss->dev, "encoder create failed\n");
229 ret = drm_bridge_attach(enc, pipes[i].bridge, NULL, 0);
234 /* create overlay planes of the leftover planes */
236 while (tidss->num_planes < max_planes) {
237 struct tidss_plane *tplane;
238 u32 hw_plane_id = feat->vid_order[tidss->num_planes];
240 tplane = tidss_plane_create(tidss, hw_plane_id,
241 DRM_PLANE_TYPE_OVERLAY, crtc_mask,
242 fourccs, fourccs_len);
244 if (IS_ERR(tplane)) {
245 dev_err(tidss->dev, "plane create failed\n");
246 return PTR_ERR(tplane);
249 tidss->planes[tidss->num_planes++] = &tplane->plane;
255 int tidss_modeset_init(struct tidss_device *tidss)
257 struct drm_device *ddev = &tidss->ddev;
260 dev_dbg(tidss->dev, "%s\n", __func__);
262 ret = drmm_mode_config_init(ddev);
266 ddev->mode_config.min_width = 8;
267 ddev->mode_config.min_height = 8;
268 ddev->mode_config.max_width = 8096;
269 ddev->mode_config.max_height = 8096;
270 ddev->mode_config.normalize_zpos = true;
271 ddev->mode_config.funcs = &mode_config_funcs;
272 ddev->mode_config.helper_private = &mode_config_helper_funcs;
274 ret = tidss_dispc_modeset_init(tidss);
278 ret = drm_vblank_init(ddev, tidss->num_crtcs);
282 drm_mode_config_reset(ddev);
284 dev_dbg(tidss->dev, "%s done\n", __func__);