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.
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drm_probe_helper.h>
17 #include "sti_compositor.h"
23 static void sti_crtc_atomic_enable(struct drm_crtc
*crtc
,
24 struct drm_crtc_state
*old_state
)
26 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
28 DRM_DEBUG_DRIVER("\n");
30 mixer
->status
= STI_MIXER_READY
;
32 drm_crtc_vblank_on(crtc
);
35 static void sti_crtc_atomic_disable(struct drm_crtc
*crtc
,
36 struct drm_crtc_state
*old_state
)
38 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
40 DRM_DEBUG_DRIVER("\n");
42 mixer
->status
= STI_MIXER_DISABLING
;
44 drm_crtc_wait_one_vblank(crtc
);
48 sti_crtc_mode_set(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
)
50 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
51 struct device
*dev
= mixer
->dev
;
52 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
53 struct clk
*compo_clk
, *pix_clk
;
54 int rate
= mode
->clock
* 1000;
56 DRM_DEBUG_KMS("CRTC:%d (%s) mode: (%s)\n",
57 crtc
->base
.id
, sti_mixer_to_str(mixer
), mode
->name
);
59 DRM_DEBUG_KMS(DRM_MODE_FMT
"\n", DRM_MODE_ARG(mode
));
61 if (mixer
->id
== STI_MIXER_MAIN
) {
62 compo_clk
= compo
->clk_compo_main
;
63 pix_clk
= compo
->clk_pix_main
;
65 compo_clk
= compo
->clk_compo_aux
;
66 pix_clk
= compo
->clk_pix_aux
;
69 /* Prepare and enable the compo IP clock */
70 if (clk_prepare_enable(compo_clk
)) {
71 DRM_INFO("Failed to prepare/enable compositor clk\n");
75 /* Set rate and prepare/enable pixel clock */
76 if (clk_set_rate(pix_clk
, rate
) < 0) {
77 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate
);
80 if (clk_prepare_enable(pix_clk
)) {
81 DRM_ERROR("Failed to prepare/enable pix clk\n");
85 sti_vtg_set_config(compo
->vtg
[mixer
->id
], &crtc
->mode
);
87 if (sti_mixer_active_video_area(mixer
, &crtc
->mode
)) {
88 DRM_ERROR("Can't set active video area\n");
95 clk_disable_unprepare(pix_clk
);
97 clk_disable_unprepare(compo_clk
);
102 static void sti_crtc_disable(struct drm_crtc
*crtc
)
104 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
105 struct device
*dev
= mixer
->dev
;
106 struct sti_compositor
*compo
= dev_get_drvdata(dev
);
108 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc
->base
.id
, sti_mixer_to_str(mixer
));
110 /* Disable Background */
111 sti_mixer_set_background_status(mixer
, false);
113 drm_crtc_vblank_off(crtc
);
115 /* Disable pixel clock and compo IP clocks */
116 if (mixer
->id
== STI_MIXER_MAIN
) {
117 clk_disable_unprepare(compo
->clk_pix_main
);
118 clk_disable_unprepare(compo
->clk_compo_main
);
120 clk_disable_unprepare(compo
->clk_pix_aux
);
121 clk_disable_unprepare(compo
->clk_compo_aux
);
124 mixer
->status
= STI_MIXER_DISABLED
;
128 sti_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
130 sti_crtc_mode_set(crtc
, &crtc
->state
->adjusted_mode
);
133 static void sti_crtc_atomic_flush(struct drm_crtc
*crtc
,
134 struct drm_crtc_state
*old_crtc_state
)
136 struct drm_device
*drm_dev
= crtc
->dev
;
137 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
138 struct sti_compositor
*compo
= dev_get_drvdata(mixer
->dev
);
140 struct drm_pending_vblank_event
*event
;
143 DRM_DEBUG_DRIVER("\n");
145 /* perform plane actions */
146 list_for_each_entry(p
, &drm_dev
->mode_config
.plane_list
, head
) {
147 struct sti_plane
*plane
= to_sti_plane(p
);
149 switch (plane
->status
) {
150 case STI_PLANE_UPDATED
:
151 /* ignore update for other CRTC */
152 if (p
->state
->crtc
!= crtc
)
155 /* update planes tag as updated */
156 DRM_DEBUG_DRIVER("update plane %s\n",
157 sti_plane_to_str(plane
));
159 if (sti_mixer_set_plane_depth(mixer
, plane
)) {
160 DRM_ERROR("Cannot set plane %s depth\n",
161 sti_plane_to_str(plane
));
165 if (sti_mixer_set_plane_status(mixer
, plane
, true)) {
166 DRM_ERROR("Cannot enable plane %s at mixer\n",
167 sti_plane_to_str(plane
));
171 /* if plane is HQVDP_0 then commit the vid[0] */
172 if (plane
->desc
== STI_HQVDP_0
)
173 sti_vid_commit(compo
->vid
[0], p
->state
);
175 plane
->status
= STI_PLANE_READY
;
178 case STI_PLANE_DISABLING
:
179 /* disabling sequence for planes tag as disabling */
180 DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
181 sti_plane_to_str(plane
));
183 if (sti_mixer_set_plane_status(mixer
, plane
, false)) {
184 DRM_ERROR("Cannot disable plane %s at mixer\n",
185 sti_plane_to_str(plane
));
189 if (plane
->desc
== STI_CURSOR
)
190 /* tag plane status for disabled */
191 plane
->status
= STI_PLANE_DISABLED
;
193 /* tag plane status for flushing */
194 plane
->status
= STI_PLANE_FLUSHING
;
196 /* if plane is HQVDP_0 then disable the vid[0] */
197 if (plane
->desc
== STI_HQVDP_0
)
198 sti_vid_disable(compo
->vid
[0]);
202 /* Other status case are not handled */
207 event
= crtc
->state
->event
;
209 crtc
->state
->event
= NULL
;
211 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
212 if (drm_crtc_vblank_get(crtc
) == 0)
213 drm_crtc_arm_vblank_event(crtc
, event
);
215 drm_crtc_send_vblank_event(crtc
, event
);
216 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
220 static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs
= {
221 .mode_set_nofb
= sti_crtc_mode_set_nofb
,
222 .atomic_flush
= sti_crtc_atomic_flush
,
223 .atomic_enable
= sti_crtc_atomic_enable
,
224 .atomic_disable
= sti_crtc_atomic_disable
,
227 static void sti_crtc_destroy(struct drm_crtc
*crtc
)
230 drm_crtc_cleanup(crtc
);
233 static int sti_crtc_set_property(struct drm_crtc
*crtc
,
234 struct drm_property
*property
,
241 int sti_crtc_vblank_cb(struct notifier_block
*nb
,
242 unsigned long event
, void *data
)
244 struct sti_compositor
*compo
;
245 struct drm_crtc
*crtc
= data
;
246 struct sti_mixer
*mixer
;
249 pipe
= drm_crtc_index(crtc
);
250 compo
= container_of(nb
, struct sti_compositor
, vtg_vblank_nb
[pipe
]);
251 mixer
= compo
->mixer
[pipe
];
253 if ((event
!= VTG_TOP_FIELD_EVENT
) &&
254 (event
!= VTG_BOTTOM_FIELD_EVENT
)) {
255 DRM_ERROR("unknown event: %lu\n", event
);
259 drm_crtc_handle_vblank(crtc
);
261 if (mixer
->status
== STI_MIXER_DISABLING
) {
264 /* Disable mixer only if all overlay planes (GDP and VDP)
266 list_for_each_entry(p
, &crtc
->dev
->mode_config
.plane_list
,
268 struct sti_plane
*plane
= to_sti_plane(p
);
270 if ((plane
->desc
& STI_PLANE_TYPE_MASK
) <= STI_VDP
)
271 if (plane
->status
!= STI_PLANE_DISABLED
)
274 sti_crtc_disable(crtc
);
280 int sti_crtc_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
282 struct sti_private
*dev_priv
= dev
->dev_private
;
283 struct sti_compositor
*compo
= dev_priv
->compo
;
284 struct notifier_block
*vtg_vblank_nb
= &compo
->vtg_vblank_nb
[pipe
];
285 struct drm_crtc
*crtc
= &compo
->mixer
[pipe
]->drm_crtc
;
286 struct sti_vtg
*vtg
= compo
->vtg
[pipe
];
288 DRM_DEBUG_DRIVER("\n");
290 if (sti_vtg_register_client(vtg
, vtg_vblank_nb
, crtc
)) {
291 DRM_ERROR("Cannot register VTG notifier\n");
298 void sti_crtc_disable_vblank(struct drm_device
*drm_dev
, unsigned int pipe
)
300 struct sti_private
*priv
= drm_dev
->dev_private
;
301 struct sti_compositor
*compo
= priv
->compo
;
302 struct notifier_block
*vtg_vblank_nb
= &compo
->vtg_vblank_nb
[pipe
];
303 struct sti_vtg
*vtg
= compo
->vtg
[pipe
];
305 DRM_DEBUG_DRIVER("\n");
307 if (sti_vtg_unregister_client(vtg
, vtg_vblank_nb
))
308 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
311 static int sti_crtc_late_register(struct drm_crtc
*crtc
)
313 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
314 struct sti_compositor
*compo
= dev_get_drvdata(mixer
->dev
);
316 if (drm_crtc_index(crtc
) == 0)
317 return sti_compositor_debugfs_init(compo
, crtc
->dev
->primary
);
322 static const struct drm_crtc_funcs sti_crtc_funcs
= {
323 .set_config
= drm_atomic_helper_set_config
,
324 .page_flip
= drm_atomic_helper_page_flip
,
325 .destroy
= sti_crtc_destroy
,
326 .set_property
= sti_crtc_set_property
,
327 .reset
= drm_atomic_helper_crtc_reset
,
328 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
329 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
330 .late_register
= sti_crtc_late_register
,
333 bool sti_crtc_is_main(struct drm_crtc
*crtc
)
335 struct sti_mixer
*mixer
= to_sti_mixer(crtc
);
337 if (mixer
->id
== STI_MIXER_MAIN
)
343 int sti_crtc_init(struct drm_device
*drm_dev
, struct sti_mixer
*mixer
,
344 struct drm_plane
*primary
, struct drm_plane
*cursor
)
346 struct drm_crtc
*crtc
= &mixer
->drm_crtc
;
349 res
= drm_crtc_init_with_planes(drm_dev
, crtc
, primary
, cursor
,
350 &sti_crtc_funcs
, NULL
);
352 DRM_ERROR("Can't initialize CRTC\n");
356 drm_crtc_helper_add(crtc
, &sti_crtc_helper_funcs
);
358 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
359 crtc
->base
.id
, sti_mixer_to_str(mixer
));