1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2014
4 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
5 * Fabien Dessenne <fabien.dessenne@st.com>
6 * for STMicroelectronics.
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_device.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drm_print.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_vblank.h>
19 #include "sti_compositor.h"
25 static void sti_crtc_atomic_enable(struct drm_crtc
*crtc
,
26 struct drm_atomic_state
*state
)
28 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
30 DRM_DEBUG_DRIVER("\n");
32 mixer
->status
= STI_MIXER_READY
;
34 drm_crtc_vblank_on(crtc
);
37 static void sti_crtc_atomic_disable(struct drm_crtc
*crtc
,
38 struct drm_atomic_state
*state
)
40 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
42 DRM_DEBUG_DRIVER("\n");
44 mixer
->status
= STI_MIXER_DISABLING
;
46 drm_crtc_wait_one_vblank(crtc
);
50 sti_crtc_mode_set(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
)
52 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
53 struct device
*dev
= mixer
->dev
;
54 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
55 struct clk
*compo_clk
, *pix_clk
;
56 int rate
= mode
->clock
* 1000;
58 DRM_DEBUG_KMS("CRTC:%d (%s) mode: (%s)\n",
59 crtc
->base
.id
, sti_mixer_to_str(mixer
), mode
->name
);
61 DRM_DEBUG_KMS(DRM_MODE_FMT
"\n", DRM_MODE_ARG(mode
));
63 if (mixer
->id
== STI_MIXER_MAIN
) {
64 compo_clk
= compo
->clk_compo_main
;
65 pix_clk
= compo
->clk_pix_main
;
67 compo_clk
= compo
->clk_compo_aux
;
68 pix_clk
= compo
->clk_pix_aux
;
71 /* Prepare and enable the compo IP clock */
72 if (clk_prepare_enable(compo_clk
)) {
73 DRM_INFO("Failed to prepare/enable compositor clk\n");
77 /* Set rate and prepare/enable pixel clock */
78 if (clk_set_rate(pix_clk
, rate
) < 0) {
79 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate
);
82 if (clk_prepare_enable(pix_clk
)) {
83 DRM_ERROR("Failed to prepare/enable pix clk\n");
87 sti_vtg_set_config(compo
->vtg
[mixer
->id
], &crtc
->mode
);
89 if (sti_mixer_active_video_area(mixer
, &crtc
->mode
)) {
90 DRM_ERROR("Can't set active video area\n");
97 clk_disable_unprepare(pix_clk
);
99 clk_disable_unprepare(compo_clk
);
104 static void sti_crtc_disable(struct drm_crtc
*crtc
)
106 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
107 struct device
*dev
= mixer
->dev
;
108 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
110 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc
->base
.id
, sti_mixer_to_str(mixer
));
112 /* Disable Background */
113 sti_mixer_set_background_status(mixer
, false);
115 drm_crtc_vblank_off(crtc
);
117 /* Disable pixel clock and compo IP clocks */
118 if (mixer
->id
== STI_MIXER_MAIN
) {
119 clk_disable_unprepare(compo
->clk_pix_main
);
120 clk_disable_unprepare(compo
->clk_compo_main
);
122 clk_disable_unprepare(compo
->clk_pix_aux
);
123 clk_disable_unprepare(compo
->clk_compo_aux
);
126 mixer
->status
= STI_MIXER_DISABLED
;
130 sti_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
132 sti_crtc_mode_set(crtc
, &crtc
->state
->adjusted_mode
);
135 static void sti_crtc_atomic_flush(struct drm_crtc
*crtc
,
136 struct drm_atomic_state
*state
)
138 struct drm_device
*drm_dev
= crtc
->dev
;
139 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
140 struct sti_compositor
*compo
= dev_get_drvdata(mixer
->dev
);
142 struct drm_pending_vblank_event
*event
;
145 DRM_DEBUG_DRIVER("\n");
147 /* perform plane actions */
148 list_for_each_entry(p
, &drm_dev
->mode_config
.plane_list
, head
) {
149 struct sti_plane
*plane
= to_sti_plane(p
);
151 switch (plane
->status
) {
152 case STI_PLANE_UPDATED
:
153 /* ignore update for other CRTC */
154 if (p
->state
->crtc
!= crtc
)
157 /* update planes tag as updated */
158 DRM_DEBUG_DRIVER("update plane %s\n",
159 sti_plane_to_str(plane
));
161 if (sti_mixer_set_plane_depth(mixer
, plane
)) {
162 DRM_ERROR("Cannot set plane %s depth\n",
163 sti_plane_to_str(plane
));
167 if (sti_mixer_set_plane_status(mixer
, plane
, true)) {
168 DRM_ERROR("Cannot enable plane %s at mixer\n",
169 sti_plane_to_str(plane
));
173 /* if plane is HQVDP_0 then commit the vid[0] */
174 if (plane
->desc
== STI_HQVDP_0
)
175 sti_vid_commit(compo
->vid
[0], p
->state
);
177 plane
->status
= STI_PLANE_READY
;
180 case STI_PLANE_DISABLING
:
181 /* disabling sequence for planes tag as disabling */
182 DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
183 sti_plane_to_str(plane
));
185 if (sti_mixer_set_plane_status(mixer
, plane
, false)) {
186 DRM_ERROR("Cannot disable plane %s at mixer\n",
187 sti_plane_to_str(plane
));
191 if (plane
->desc
== STI_CURSOR
)
192 /* tag plane status for disabled */
193 plane
->status
= STI_PLANE_DISABLED
;
195 /* tag plane status for flushing */
196 plane
->status
= STI_PLANE_FLUSHING
;
198 /* if plane is HQVDP_0 then disable the vid[0] */
199 if (plane
->desc
== STI_HQVDP_0
)
200 sti_vid_disable(compo
->vid
[0]);
204 /* Other status case are not handled */
209 event
= crtc
->state
->event
;
211 crtc
->state
->event
= NULL
;
213 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
214 if (drm_crtc_vblank_get(crtc
) == 0)
215 drm_crtc_arm_vblank_event(crtc
, event
);
217 drm_crtc_send_vblank_event(crtc
, event
);
218 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
222 static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs
= {
223 .mode_set_nofb
= sti_crtc_mode_set_nofb
,
224 .atomic_flush
= sti_crtc_atomic_flush
,
225 .atomic_enable
= sti_crtc_atomic_enable
,
226 .atomic_disable
= sti_crtc_atomic_disable
,
229 static void sti_crtc_destroy(struct drm_crtc
*crtc
)
232 drm_crtc_cleanup(crtc
);
235 static int sti_crtc_set_property(struct drm_crtc
*crtc
,
236 struct drm_property
*property
,
243 int sti_crtc_vblank_cb(struct notifier_block
*nb
,
244 unsigned long event
, void *data
)
246 struct sti_compositor
*compo
;
247 struct drm_crtc
*crtc
= data
;
248 struct sti_mixer
*mixer
;
251 pipe
= drm_crtc_index(crtc
);
252 compo
= container_of(nb
, struct sti_compositor
, vtg_vblank_nb
[pipe
]);
253 mixer
= compo
->mixer
[pipe
];
255 if ((event
!= VTG_TOP_FIELD_EVENT
) &&
256 (event
!= VTG_BOTTOM_FIELD_EVENT
)) {
257 DRM_ERROR("unknown event: %lu\n", event
);
261 drm_crtc_handle_vblank(crtc
);
263 if (mixer
->status
== STI_MIXER_DISABLING
) {
266 /* Disable mixer only if all overlay planes (GDP and VDP)
268 list_for_each_entry(p
, &crtc
->dev
->mode_config
.plane_list
,
270 struct sti_plane
*plane
= to_sti_plane(p
);
272 if ((plane
->desc
& STI_PLANE_TYPE_MASK
) <= STI_VDP
)
273 if (plane
->status
!= STI_PLANE_DISABLED
)
276 sti_crtc_disable(crtc
);
282 static int sti_crtc_enable_vblank(struct drm_crtc
*crtc
)
284 struct drm_device
*dev
= crtc
->dev
;
285 unsigned int pipe
= crtc
->index
;
286 struct sti_private
*dev_priv
= dev
->dev_private
;
287 struct sti_compositor
*compo
= dev_priv
->compo
;
288 struct notifier_block
*vtg_vblank_nb
= &compo
->vtg_vblank_nb
[pipe
];
289 struct sti_vtg
*vtg
= compo
->vtg
[pipe
];
291 DRM_DEBUG_DRIVER("\n");
293 if (sti_vtg_register_client(vtg
, vtg_vblank_nb
, crtc
)) {
294 DRM_ERROR("Cannot register VTG notifier\n");
301 static void sti_crtc_disable_vblank(struct drm_crtc
*crtc
)
303 struct drm_device
*drm_dev
= crtc
->dev
;
304 unsigned int pipe
= crtc
->index
;
305 struct sti_private
*priv
= drm_dev
->dev_private
;
306 struct sti_compositor
*compo
= priv
->compo
;
307 struct notifier_block
*vtg_vblank_nb
= &compo
->vtg_vblank_nb
[pipe
];
308 struct sti_vtg
*vtg
= compo
->vtg
[pipe
];
310 DRM_DEBUG_DRIVER("\n");
312 if (sti_vtg_unregister_client(vtg
, vtg_vblank_nb
))
313 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
316 static int sti_crtc_late_register(struct drm_crtc
*crtc
)
318 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
319 struct sti_compositor
*compo
= dev_get_drvdata(mixer
->dev
);
321 if (drm_crtc_index(crtc
) == 0)
322 sti_compositor_debugfs_init(compo
, crtc
->dev
->primary
);
327 static const struct drm_crtc_funcs sti_crtc_funcs
= {
328 .set_config
= drm_atomic_helper_set_config
,
329 .page_flip
= drm_atomic_helper_page_flip
,
330 .destroy
= sti_crtc_destroy
,
331 .set_property
= sti_crtc_set_property
,
332 .reset
= drm_atomic_helper_crtc_reset
,
333 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
334 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
335 .late_register
= sti_crtc_late_register
,
336 .enable_vblank
= sti_crtc_enable_vblank
,
337 .disable_vblank
= sti_crtc_disable_vblank
,
340 bool sti_crtc_is_main(struct drm_crtc
*crtc
)
342 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
344 if (mixer
->id
== STI_MIXER_MAIN
)
350 int sti_crtc_init(struct drm_device
*drm_dev
, struct sti_mixer
*mixer
,
351 struct drm_plane
*primary
, struct drm_plane
*cursor
)
353 struct drm_crtc
*crtc
= &mixer
->drm_crtc
;
356 res
= drm_crtc_init_with_planes(drm_dev
, crtc
, primary
, cursor
,
357 &sti_crtc_funcs
, NULL
);
359 DRM_ERROR("Can't initialize CRTC\n");
363 drm_crtc_helper_add(crtc
, &sti_crtc_helper_funcs
);
365 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
366 crtc
->base
.id
, sti_mixer_to_str(mixer
));