2 * Copyright (C) STMicroelectronics SA 2014
3 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
4 * Fabien Dessenne <fabien.dessenne@st.com>
5 * for STMicroelectronics.
6 * License terms: GNU General Public License (GPL), version 2
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_crtc_helper.h>
15 #include <drm/drm_plane_helper.h>
17 #include "sti_compositor.h"
23 static void sti_crtc_enable(struct drm_crtc
*crtc
)
25 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
26 struct device
*dev
= mixer
->dev
;
27 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
29 DRM_DEBUG_DRIVER("\n");
31 mixer
->status
= STI_MIXER_READY
;
33 /* Prepare and enable the compo IP clock */
34 if (mixer
->id
== STI_MIXER_MAIN
) {
35 if (clk_prepare_enable(compo
->clk_compo_main
))
36 DRM_INFO("Failed to prepare/enable compo_main clk\n");
38 if (clk_prepare_enable(compo
->clk_compo_aux
))
39 DRM_INFO("Failed to prepare/enable compo_aux clk\n");
42 drm_crtc_vblank_on(crtc
);
45 static void sti_crtc_disabling(struct drm_crtc
*crtc
)
47 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
49 DRM_DEBUG_DRIVER("\n");
51 mixer
->status
= STI_MIXER_DISABLING
;
55 sti_crtc_mode_set(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
)
57 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
58 struct device
*dev
= mixer
->dev
;
59 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
61 int rate
= mode
->clock
* 1000;
64 DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
65 crtc
->base
.id
, sti_mixer_to_str(mixer
),
66 mode
->base
.id
, mode
->name
);
68 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
69 mode
->vrefresh
, mode
->clock
,
71 mode
->hsync_start
, mode
->hsync_end
,
74 mode
->vsync_start
, mode
->vsync_end
,
75 mode
->vtotal
, mode
->type
, mode
->flags
);
77 /* Set rate and prepare/enable pixel clock */
78 if (mixer
->id
== STI_MIXER_MAIN
)
79 clk
= compo
->clk_pix_main
;
81 clk
= compo
->clk_pix_aux
;
83 res
= clk_set_rate(clk
, rate
);
85 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate
);
88 if (clk_prepare_enable(clk
)) {
89 DRM_ERROR("Failed to prepare/enable pix clk\n");
93 sti_vtg_set_config(mixer
->id
== STI_MIXER_MAIN
?
94 compo
->vtg_main
: compo
->vtg_aux
, &crtc
->mode
);
96 res
= sti_mixer_active_video_area(mixer
, &crtc
->mode
);
98 DRM_ERROR("Can't set active video area\n");
105 static void sti_crtc_disable(struct drm_crtc
*crtc
)
107 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
108 struct device
*dev
= mixer
->dev
;
109 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
111 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc
->base
.id
, sti_mixer_to_str(mixer
));
113 /* Disable Background */
114 sti_mixer_set_background_status(mixer
, false);
116 drm_crtc_vblank_off(crtc
);
118 /* Disable pixel clock and compo IP clocks */
119 if (mixer
->id
== STI_MIXER_MAIN
) {
120 clk_disable_unprepare(compo
->clk_pix_main
);
121 clk_disable_unprepare(compo
->clk_compo_main
);
123 clk_disable_unprepare(compo
->clk_pix_aux
);
124 clk_disable_unprepare(compo
->clk_compo_aux
);
127 mixer
->status
= STI_MIXER_DISABLED
;
131 sti_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
133 sti_crtc_enable(crtc
);
134 sti_crtc_mode_set(crtc
, &crtc
->state
->adjusted_mode
);
137 static void sti_crtc_atomic_begin(struct drm_crtc
*crtc
,
138 struct drm_crtc_state
*old_crtc_state
)
140 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
142 if (crtc
->state
->event
) {
143 crtc
->state
->event
->pipe
= drm_crtc_index(crtc
);
145 WARN_ON(drm_crtc_vblank_get(crtc
) != 0);
147 mixer
->pending_event
= crtc
->state
->event
;
148 crtc
->state
->event
= NULL
;
152 static void sti_crtc_atomic_flush(struct drm_crtc
*crtc
,
153 struct drm_crtc_state
*old_crtc_state
)
155 struct drm_device
*drm_dev
= crtc
->dev
;
156 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
157 struct sti_compositor
*compo
= dev_get_drvdata(mixer
->dev
);
160 DRM_DEBUG_DRIVER("\n");
162 /* perform plane actions */
163 list_for_each_entry(p
, &drm_dev
->mode_config
.plane_list
, head
) {
164 struct sti_plane
*plane
= to_sti_plane(p
);
166 switch (plane
->status
) {
167 case STI_PLANE_UPDATED
:
168 /* update planes tag as updated */
169 DRM_DEBUG_DRIVER("update plane %s\n",
170 sti_plane_to_str(plane
));
172 if (sti_mixer_set_plane_depth(mixer
, plane
)) {
173 DRM_ERROR("Cannot set plane %s depth\n",
174 sti_plane_to_str(plane
));
178 if (sti_mixer_set_plane_status(mixer
, plane
, true)) {
179 DRM_ERROR("Cannot enable plane %s at mixer\n",
180 sti_plane_to_str(plane
));
184 /* if plane is HQVDP_0 then commit the vid[0] */
185 if (plane
->desc
== STI_HQVDP_0
)
186 sti_vid_commit(compo
->vid
[0], p
->state
);
188 plane
->status
= STI_PLANE_READY
;
191 case STI_PLANE_DISABLING
:
192 /* disabling sequence for planes tag as disabling */
193 DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
194 sti_plane_to_str(plane
));
196 if (sti_mixer_set_plane_status(mixer
, plane
, false)) {
197 DRM_ERROR("Cannot disable plane %s at mixer\n",
198 sti_plane_to_str(plane
));
202 if (plane
->desc
== STI_CURSOR
)
203 /* tag plane status for disabled */
204 plane
->status
= STI_PLANE_DISABLED
;
206 /* tag plane status for flushing */
207 plane
->status
= STI_PLANE_FLUSHING
;
209 /* if plane is HQVDP_0 then disable the vid[0] */
210 if (plane
->desc
== STI_HQVDP_0
)
211 sti_vid_disable(compo
->vid
[0]);
215 /* Other status case are not handled */
221 static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs
= {
222 .enable
= sti_crtc_enable
,
223 .disable
= sti_crtc_disabling
,
224 .mode_set
= drm_helper_crtc_mode_set
,
225 .mode_set_nofb
= sti_crtc_mode_set_nofb
,
226 .mode_set_base
= drm_helper_crtc_mode_set_base
,
227 .atomic_begin
= sti_crtc_atomic_begin
,
228 .atomic_flush
= sti_crtc_atomic_flush
,
231 static void sti_crtc_destroy(struct drm_crtc
*crtc
)
234 drm_crtc_cleanup(crtc
);
237 static int sti_crtc_set_property(struct drm_crtc
*crtc
,
238 struct drm_property
*property
,
245 int sti_crtc_vblank_cb(struct notifier_block
*nb
,
246 unsigned long event
, void *data
)
248 struct sti_compositor
*compo
=
249 container_of(nb
, struct sti_compositor
, vtg_vblank_nb
);
250 struct drm_crtc
*crtc
= data
;
251 struct sti_mixer
*mixer
;
253 struct sti_private
*priv
;
256 priv
= crtc
->dev
->dev_private
;
257 pipe
= drm_crtc_index(crtc
);
258 mixer
= compo
->mixer
[pipe
];
260 if ((event
!= VTG_TOP_FIELD_EVENT
) &&
261 (event
!= VTG_BOTTOM_FIELD_EVENT
)) {
262 DRM_ERROR("unknown event: %lu\n", event
);
266 drm_crtc_handle_vblank(crtc
);
268 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
269 if (mixer
->pending_event
) {
270 drm_crtc_send_vblank_event(crtc
, mixer
->pending_event
);
271 drm_crtc_vblank_put(crtc
);
272 mixer
->pending_event
= NULL
;
274 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
276 if (mixer
->status
== STI_MIXER_DISABLING
) {
279 /* Disable mixer only if all overlay planes (GDP and VDP)
281 list_for_each_entry(p
, &crtc
->dev
->mode_config
.plane_list
,
283 struct sti_plane
*plane
= to_sti_plane(p
);
285 if ((plane
->desc
& STI_PLANE_TYPE_MASK
) <= STI_VDP
)
286 if (plane
->status
!= STI_PLANE_DISABLED
)
289 sti_crtc_disable(crtc
);
295 int sti_crtc_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
297 struct sti_private
*dev_priv
= dev
->dev_private
;
298 struct sti_compositor
*compo
= dev_priv
->compo
;
299 struct notifier_block
*vtg_vblank_nb
= &compo
->vtg_vblank_nb
;
300 struct drm_crtc
*crtc
= &compo
->mixer
[pipe
]->drm_crtc
;
302 DRM_DEBUG_DRIVER("\n");
304 if (sti_vtg_register_client(pipe
== STI_MIXER_MAIN
?
305 compo
->vtg_main
: compo
->vtg_aux
,
306 vtg_vblank_nb
, crtc
)) {
307 DRM_ERROR("Cannot register VTG notifier\n");
314 void sti_crtc_disable_vblank(struct drm_device
*drm_dev
, unsigned int pipe
)
316 struct sti_private
*priv
= drm_dev
->dev_private
;
317 struct sti_compositor
*compo
= priv
->compo
;
318 struct notifier_block
*vtg_vblank_nb
= &compo
->vtg_vblank_nb
;
319 struct drm_crtc
*crtc
= &compo
->mixer
[pipe
]->drm_crtc
;
321 DRM_DEBUG_DRIVER("\n");
323 if (sti_vtg_unregister_client(pipe
== STI_MIXER_MAIN
?
324 compo
->vtg_main
: compo
->vtg_aux
, vtg_vblank_nb
))
325 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
327 /* free the resources of the pending requests */
328 if (compo
->mixer
[pipe
]->pending_event
) {
329 drm_crtc_vblank_put(crtc
);
330 compo
->mixer
[pipe
]->pending_event
= NULL
;
334 static const struct drm_crtc_funcs sti_crtc_funcs
= {
335 .set_config
= drm_atomic_helper_set_config
,
336 .page_flip
= drm_atomic_helper_page_flip
,
337 .destroy
= sti_crtc_destroy
,
338 .set_property
= sti_crtc_set_property
,
339 .reset
= drm_atomic_helper_crtc_reset
,
340 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
341 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
344 bool sti_crtc_is_main(struct drm_crtc
*crtc
)
346 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
348 if (mixer
->id
== STI_MIXER_MAIN
)
354 int sti_crtc_init(struct drm_device
*drm_dev
, struct sti_mixer
*mixer
,
355 struct drm_plane
*primary
, struct drm_plane
*cursor
)
357 struct drm_crtc
*crtc
= &mixer
->drm_crtc
;
360 res
= drm_crtc_init_with_planes(drm_dev
, crtc
, primary
, cursor
,
361 &sti_crtc_funcs
, NULL
);
363 DRM_ERROR("Can't initialze CRTC\n");
367 drm_crtc_helper_add(crtc
, &sti_crtc_helper_funcs
);
369 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
370 crtc
->base
.id
, sti_mixer_to_str(mixer
));