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
;
54 static bool sti_crtc_mode_fixup(struct drm_crtc
*crtc
,
55 const struct drm_display_mode
*mode
,
56 struct drm_display_mode
*adjusted_mode
)
58 /* accept the provided drm_display_mode, do not fix it up */
59 drm_mode_set_crtcinfo(adjusted_mode
, CRTC_INTERLACE_HALVE_V
);
64 sti_crtc_mode_set(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
)
66 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
67 struct device
*dev
= mixer
->dev
;
68 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
70 int rate
= mode
->clock
* 1000;
73 DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
74 crtc
->base
.id
, sti_mixer_to_str(mixer
),
75 mode
->base
.id
, mode
->name
);
77 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
78 mode
->vrefresh
, mode
->clock
,
80 mode
->hsync_start
, mode
->hsync_end
,
83 mode
->vsync_start
, mode
->vsync_end
,
84 mode
->vtotal
, mode
->type
, mode
->flags
);
86 /* Set rate and prepare/enable pixel clock */
87 if (mixer
->id
== STI_MIXER_MAIN
)
88 clk
= compo
->clk_pix_main
;
90 clk
= compo
->clk_pix_aux
;
92 res
= clk_set_rate(clk
, rate
);
94 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate
);
97 if (clk_prepare_enable(clk
)) {
98 DRM_ERROR("Failed to prepare/enable pix clk\n");
102 sti_vtg_set_config(mixer
->id
== STI_MIXER_MAIN
?
103 compo
->vtg_main
: compo
->vtg_aux
, &crtc
->mode
);
105 res
= sti_mixer_active_video_area(mixer
, &crtc
->mode
);
107 DRM_ERROR("Can't set active video area\n");
114 static void sti_crtc_disable(struct drm_crtc
*crtc
)
116 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
117 struct device
*dev
= mixer
->dev
;
118 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
120 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc
->base
.id
, sti_mixer_to_str(mixer
));
122 /* Disable Background */
123 sti_mixer_set_background_status(mixer
, false);
125 drm_crtc_vblank_off(crtc
);
127 /* Disable pixel clock and compo IP clocks */
128 if (mixer
->id
== STI_MIXER_MAIN
) {
129 clk_disable_unprepare(compo
->clk_pix_main
);
130 clk_disable_unprepare(compo
->clk_compo_main
);
132 clk_disable_unprepare(compo
->clk_pix_aux
);
133 clk_disable_unprepare(compo
->clk_compo_aux
);
136 mixer
->status
= STI_MIXER_DISABLED
;
140 sti_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
142 sti_crtc_enable(crtc
);
143 sti_crtc_mode_set(crtc
, &crtc
->state
->adjusted_mode
);
146 static void sti_crtc_atomic_begin(struct drm_crtc
*crtc
,
147 struct drm_crtc_state
*old_crtc_state
)
149 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
151 if (crtc
->state
->event
) {
152 crtc
->state
->event
->pipe
= drm_crtc_index(crtc
);
154 WARN_ON(drm_crtc_vblank_get(crtc
) != 0);
156 mixer
->pending_event
= crtc
->state
->event
;
157 crtc
->state
->event
= NULL
;
161 static void sti_crtc_atomic_flush(struct drm_crtc
*crtc
,
162 struct drm_crtc_state
*old_crtc_state
)
164 struct drm_device
*drm_dev
= crtc
->dev
;
165 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
166 struct sti_compositor
*compo
= dev_get_drvdata(mixer
->dev
);
169 DRM_DEBUG_DRIVER("\n");
171 /* perform plane actions */
172 list_for_each_entry(p
, &drm_dev
->mode_config
.plane_list
, head
) {
173 struct sti_plane
*plane
= to_sti_plane(p
);
175 switch (plane
->status
) {
176 case STI_PLANE_UPDATED
:
177 /* update planes tag as updated */
178 DRM_DEBUG_DRIVER("update plane %s\n",
179 sti_plane_to_str(plane
));
181 if (sti_mixer_set_plane_depth(mixer
, plane
)) {
182 DRM_ERROR("Cannot set plane %s depth\n",
183 sti_plane_to_str(plane
));
187 if (sti_mixer_set_plane_status(mixer
, plane
, true)) {
188 DRM_ERROR("Cannot enable plane %s at mixer\n",
189 sti_plane_to_str(plane
));
193 /* if plane is HQVDP_0 then commit the vid[0] */
194 if (plane
->desc
== STI_HQVDP_0
)
195 sti_vid_commit(compo
->vid
[0], p
->state
);
197 plane
->status
= STI_PLANE_READY
;
200 case STI_PLANE_DISABLING
:
201 /* disabling sequence for planes tag as disabling */
202 DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
203 sti_plane_to_str(plane
));
205 if (sti_mixer_set_plane_status(mixer
, plane
, false)) {
206 DRM_ERROR("Cannot disable plane %s at mixer\n",
207 sti_plane_to_str(plane
));
211 if (plane
->desc
== STI_CURSOR
)
212 /* tag plane status for disabled */
213 plane
->status
= STI_PLANE_DISABLED
;
215 /* tag plane status for flushing */
216 plane
->status
= STI_PLANE_FLUSHING
;
218 /* if plane is HQVDP_0 then disable the vid[0] */
219 if (plane
->desc
== STI_HQVDP_0
)
220 sti_vid_disable(compo
->vid
[0]);
224 /* Other status case are not handled */
230 static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs
= {
231 .enable
= sti_crtc_enable
,
232 .disable
= sti_crtc_disabling
,
233 .mode_fixup
= sti_crtc_mode_fixup
,
234 .mode_set
= drm_helper_crtc_mode_set
,
235 .mode_set_nofb
= sti_crtc_mode_set_nofb
,
236 .mode_set_base
= drm_helper_crtc_mode_set_base
,
237 .atomic_begin
= sti_crtc_atomic_begin
,
238 .atomic_flush
= sti_crtc_atomic_flush
,
241 static void sti_crtc_destroy(struct drm_crtc
*crtc
)
244 drm_crtc_cleanup(crtc
);
247 static int sti_crtc_set_property(struct drm_crtc
*crtc
,
248 struct drm_property
*property
,
255 int sti_crtc_vblank_cb(struct notifier_block
*nb
,
256 unsigned long event
, void *data
)
258 struct sti_compositor
*compo
=
259 container_of(nb
, struct sti_compositor
, vtg_vblank_nb
);
260 struct drm_crtc
*crtc
= data
;
261 struct sti_mixer
*mixer
;
263 struct sti_private
*priv
;
266 priv
= crtc
->dev
->dev_private
;
267 pipe
= drm_crtc_index(crtc
);
268 mixer
= compo
->mixer
[pipe
];
270 if ((event
!= VTG_TOP_FIELD_EVENT
) &&
271 (event
!= VTG_BOTTOM_FIELD_EVENT
)) {
272 DRM_ERROR("unknown event: %lu\n", event
);
276 drm_crtc_handle_vblank(crtc
);
278 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
279 if (mixer
->pending_event
) {
280 drm_crtc_send_vblank_event(crtc
, mixer
->pending_event
);
281 drm_crtc_vblank_put(crtc
);
282 mixer
->pending_event
= NULL
;
284 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
286 if (mixer
->status
== STI_MIXER_DISABLING
) {
289 /* Disable mixer only if all overlay planes (GDP and VDP)
291 list_for_each_entry(p
, &crtc
->dev
->mode_config
.plane_list
,
293 struct sti_plane
*plane
= to_sti_plane(p
);
295 if ((plane
->desc
& STI_PLANE_TYPE_MASK
) <= STI_VDP
)
296 if (plane
->status
!= STI_PLANE_DISABLED
)
299 sti_crtc_disable(crtc
);
305 int sti_crtc_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
307 struct sti_private
*dev_priv
= dev
->dev_private
;
308 struct sti_compositor
*compo
= dev_priv
->compo
;
309 struct notifier_block
*vtg_vblank_nb
= &compo
->vtg_vblank_nb
;
310 struct drm_crtc
*crtc
= &compo
->mixer
[pipe
]->drm_crtc
;
312 DRM_DEBUG_DRIVER("\n");
314 if (sti_vtg_register_client(pipe
== STI_MIXER_MAIN
?
315 compo
->vtg_main
: compo
->vtg_aux
,
316 vtg_vblank_nb
, crtc
)) {
317 DRM_ERROR("Cannot register VTG notifier\n");
324 void sti_crtc_disable_vblank(struct drm_device
*drm_dev
, unsigned int pipe
)
326 struct sti_private
*priv
= drm_dev
->dev_private
;
327 struct sti_compositor
*compo
= priv
->compo
;
328 struct notifier_block
*vtg_vblank_nb
= &compo
->vtg_vblank_nb
;
329 struct drm_crtc
*crtc
= &compo
->mixer
[pipe
]->drm_crtc
;
331 DRM_DEBUG_DRIVER("\n");
333 if (sti_vtg_unregister_client(pipe
== STI_MIXER_MAIN
?
334 compo
->vtg_main
: compo
->vtg_aux
, vtg_vblank_nb
))
335 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
337 /* free the resources of the pending requests */
338 if (compo
->mixer
[pipe
]->pending_event
) {
339 drm_crtc_vblank_put(crtc
);
340 compo
->mixer
[pipe
]->pending_event
= NULL
;
344 static const struct drm_crtc_funcs sti_crtc_funcs
= {
345 .set_config
= drm_atomic_helper_set_config
,
346 .page_flip
= drm_atomic_helper_page_flip
,
347 .destroy
= sti_crtc_destroy
,
348 .set_property
= sti_crtc_set_property
,
349 .reset
= drm_atomic_helper_crtc_reset
,
350 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
351 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
354 bool sti_crtc_is_main(struct drm_crtc
*crtc
)
356 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
358 if (mixer
->id
== STI_MIXER_MAIN
)
364 int sti_crtc_init(struct drm_device
*drm_dev
, struct sti_mixer
*mixer
,
365 struct drm_plane
*primary
, struct drm_plane
*cursor
)
367 struct drm_crtc
*crtc
= &mixer
->drm_crtc
;
370 res
= drm_crtc_init_with_planes(drm_dev
, crtc
, primary
, cursor
,
371 &sti_crtc_funcs
, NULL
);
373 DRM_ERROR("Can't initialze CRTC\n");
377 drm_crtc_helper_add(crtc
, &sti_crtc_helper_funcs
);
379 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
380 crtc
->base
.id
, sti_mixer_to_str(mixer
));