blob: de11c7cfb02f4b1d8d65d52cca8676526e80a68b [file] [log] [blame]
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +02001/*
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
7 */
8
9#include <linux/clk.h>
10
11#include <drm/drmP.h>
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010012#include <drm/drm_atomic.h>
13#include <drm/drm_atomic_helper.h>
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020014#include <drm/drm_crtc_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010015#include <drm/drm_plane_helper.h>
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020016
17#include "sti_compositor.h"
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020018#include "sti_crtc.h"
19#include "sti_drv.h"
Vincent Abriou29d1dc62015-08-03 14:22:16 +020020#include "sti_vid.h"
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020021#include "sti_vtg.h"
22
Vincent Abriou29d1dc62015-08-03 14:22:16 +020023static void sti_crtc_enable(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020024{
25 struct sti_mixer *mixer = to_sti_mixer(crtc);
26 struct device *dev = mixer->dev;
27 struct sti_compositor *compo = dev_get_drvdata(dev);
28
Vincent Abriou29d1dc62015-08-03 14:22:16 +020029 DRM_DEBUG_DRIVER("\n");
30
31 mixer->status = STI_MIXER_READY;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020032
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");
37 } else {
38 if (clk_prepare_enable(compo->clk_compo_aux))
39 DRM_INFO("Failed to prepare/enable compo_aux clk\n");
40 }
Benjamin Gaignard2f7d0e82c2014-12-04 11:17:32 +010041
Vincent Abriou29d1dc62015-08-03 14:22:16 +020042 drm_crtc_vblank_on(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020043}
44
Vincent Abriou29d1dc62015-08-03 14:22:16 +020045static void sti_crtc_disabling(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020046{
47 struct sti_mixer *mixer = to_sti_mixer(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020048
Vincent Abriou29d1dc62015-08-03 14:22:16 +020049 DRM_DEBUG_DRIVER("\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020050
Vincent Abriou29d1dc62015-08-03 14:22:16 +020051 mixer->status = STI_MIXER_DISABLING;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020052}
53
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020054static bool sti_crtc_mode_fixup(struct drm_crtc *crtc,
55 const struct drm_display_mode *mode,
56 struct drm_display_mode *adjusted_mode)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020057{
58 /* accept the provided drm_display_mode, do not fix it up */
59 return true;
60}
61
62static int
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020063sti_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020064{
65 struct sti_mixer *mixer = to_sti_mixer(crtc);
66 struct device *dev = mixer->dev;
67 struct sti_compositor *compo = dev_get_drvdata(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020068 struct clk *clk;
69 int rate = mode->clock * 1000;
70 int res;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020071
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010072 DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020073 crtc->base.id, sti_mixer_to_str(mixer),
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010074 mode->base.id, mode->name);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020075
76 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
77 mode->vrefresh, mode->clock,
78 mode->hdisplay,
79 mode->hsync_start, mode->hsync_end,
80 mode->htotal,
81 mode->vdisplay,
82 mode->vsync_start, mode->vsync_end,
83 mode->vtotal, mode->type, mode->flags);
84
85 /* Set rate and prepare/enable pixel clock */
86 if (mixer->id == STI_MIXER_MAIN)
87 clk = compo->clk_pix_main;
88 else
89 clk = compo->clk_pix_aux;
90
91 res = clk_set_rate(clk, rate);
92 if (res < 0) {
93 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
94 return -EINVAL;
95 }
96 if (clk_prepare_enable(clk)) {
97 DRM_ERROR("Failed to prepare/enable pix clk\n");
98 return -EINVAL;
99 }
100
101 sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
102 compo->vtg_main : compo->vtg_aux, &crtc->mode);
103
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200104 res = sti_mixer_active_video_area(mixer, &crtc->mode);
105 if (res) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200106 DRM_ERROR("Can't set active video area\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200107 return -EINVAL;
108 }
109
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100110 return res;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200111}
112
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200113static void sti_crtc_disable(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200114{
115 struct sti_mixer *mixer = to_sti_mixer(crtc);
116 struct device *dev = mixer->dev;
117 struct sti_compositor *compo = dev_get_drvdata(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200118
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200119 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
120
121 /* Disable Background */
122 sti_mixer_set_background_status(mixer, false);
123
Benjamin Gaignardca614aa2014-12-04 11:27:45 +0100124 drm_crtc_vblank_off(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200125
126 /* Disable pixel clock and compo IP clocks */
127 if (mixer->id == STI_MIXER_MAIN) {
128 clk_disable_unprepare(compo->clk_pix_main);
129 clk_disable_unprepare(compo->clk_compo_main);
130 } else {
131 clk_disable_unprepare(compo->clk_pix_aux);
132 clk_disable_unprepare(compo->clk_compo_aux);
133 }
134
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200135 mixer->status = STI_MIXER_DISABLED;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200136}
137
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100138static void
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200139sti_crtc_mode_set_nofb(struct drm_crtc *crtc)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100140{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200141 sti_crtc_enable(crtc);
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200142 sti_crtc_mode_set(crtc, &crtc->state->adjusted_mode);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100143}
144
Dave Airliee1474e72015-08-14 10:14:23 +1000145static void sti_crtc_atomic_begin(struct drm_crtc *crtc,
146 struct drm_crtc_state *old_crtc_state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100147{
148 struct sti_mixer *mixer = to_sti_mixer(crtc);
149
150 if (crtc->state->event) {
151 crtc->state->event->pipe = drm_crtc_index(crtc);
152
153 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
154
155 mixer->pending_event = crtc->state->event;
156 crtc->state->event = NULL;
157 }
158}
159
Dave Airliee1474e72015-08-14 10:14:23 +1000160static void sti_crtc_atomic_flush(struct drm_crtc *crtc,
161 struct drm_crtc_state *old_crtc_state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100162{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200163 struct drm_device *drm_dev = crtc->dev;
164 struct sti_mixer *mixer = to_sti_mixer(crtc);
165 struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
166 struct drm_plane *p;
167
168 DRM_DEBUG_DRIVER("\n");
169
170 /* perform plane actions */
171 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
172 struct sti_plane *plane = to_sti_plane(p);
173
174 switch (plane->status) {
175 case STI_PLANE_UPDATED:
176 /* update planes tag as updated */
177 DRM_DEBUG_DRIVER("update plane %s\n",
178 sti_plane_to_str(plane));
179
180 if (sti_mixer_set_plane_depth(mixer, plane)) {
181 DRM_ERROR("Cannot set plane %s depth\n",
182 sti_plane_to_str(plane));
183 break;
184 }
185
186 if (sti_mixer_set_plane_status(mixer, plane, true)) {
187 DRM_ERROR("Cannot enable plane %s at mixer\n",
188 sti_plane_to_str(plane));
189 break;
190 }
191
192 /* if plane is HQVDP_0 then commit the vid[0] */
193 if (plane->desc == STI_HQVDP_0)
194 sti_vid_commit(compo->vid[0], p->state);
195
196 plane->status = STI_PLANE_READY;
197
198 break;
199 case STI_PLANE_DISABLING:
200 /* disabling sequence for planes tag as disabling */
201 DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
202 sti_plane_to_str(plane));
203
204 if (sti_mixer_set_plane_status(mixer, plane, false)) {
205 DRM_ERROR("Cannot disable plane %s at mixer\n",
206 sti_plane_to_str(plane));
207 continue;
208 }
209
210 if (plane->desc == STI_CURSOR)
211 /* tag plane status for disabled */
212 plane->status = STI_PLANE_DISABLED;
213 else
214 /* tag plane status for flushing */
215 plane->status = STI_PLANE_FLUSHING;
216
217 /* if plane is HQVDP_0 then disable the vid[0] */
218 if (plane->desc == STI_HQVDP_0)
219 sti_vid_disable(compo->vid[0]);
220
221 break;
222 default:
223 /* Other status case are not handled */
224 break;
225 }
226 }
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100227}
228
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300229static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200230 .enable = sti_crtc_enable,
231 .disable = sti_crtc_disabling,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200232 .mode_fixup = sti_crtc_mode_fixup,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100233 .mode_set = drm_helper_crtc_mode_set,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200234 .mode_set_nofb = sti_crtc_mode_set_nofb,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100235 .mode_set_base = drm_helper_crtc_mode_set_base,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200236 .atomic_begin = sti_crtc_atomic_begin,
237 .atomic_flush = sti_crtc_atomic_flush,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200238};
239
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200240static void sti_crtc_destroy(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200241{
242 DRM_DEBUG_KMS("\n");
243 drm_crtc_cleanup(crtc);
244}
245
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200246static int sti_crtc_set_property(struct drm_crtc *crtc,
247 struct drm_property *property,
248 uint64_t val)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200249{
250 DRM_DEBUG_KMS("\n");
251 return 0;
252}
253
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200254int sti_crtc_vblank_cb(struct notifier_block *nb,
255 unsigned long event, void *data)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200256{
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200257 struct sti_compositor *compo =
258 container_of(nb, struct sti_compositor, vtg_vblank_nb);
Thierry Reding23886932015-09-24 18:35:38 +0200259 struct drm_crtc *crtc = data;
260 struct sti_mixer *mixer;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200261 unsigned long flags;
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200262 struct sti_private *priv;
Thierry Reding23886932015-09-24 18:35:38 +0200263 unsigned int pipe;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200264
Thierry Reding23886932015-09-24 18:35:38 +0200265 priv = crtc->dev->dev_private;
266 pipe = drm_crtc_index(crtc);
267 mixer = compo->mixer[pipe];
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200268
269 if ((event != VTG_TOP_FIELD_EVENT) &&
270 (event != VTG_BOTTOM_FIELD_EVENT)) {
271 DRM_ERROR("unknown event: %lu\n", event);
272 return -EINVAL;
273 }
274
Thierry Reding23886932015-09-24 18:35:38 +0200275 drm_crtc_handle_vblank(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200276
Thierry Reding23886932015-09-24 18:35:38 +0200277 spin_lock_irqsave(&crtc->dev->event_lock, flags);
278 if (mixer->pending_event) {
279 drm_crtc_send_vblank_event(crtc, mixer->pending_event);
280 drm_crtc_vblank_put(crtc);
281 mixer->pending_event = NULL;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200282 }
Thierry Reding23886932015-09-24 18:35:38 +0200283 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200284
Thierry Reding23886932015-09-24 18:35:38 +0200285 if (mixer->status == STI_MIXER_DISABLING) {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200286 struct drm_plane *p;
287
288 /* Disable mixer only if all overlay planes (GDP and VDP)
289 * are disabled */
Thierry Reding23886932015-09-24 18:35:38 +0200290 list_for_each_entry(p, &crtc->dev->mode_config.plane_list,
291 head) {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200292 struct sti_plane *plane = to_sti_plane(p);
293
294 if ((plane->desc & STI_PLANE_TYPE_MASK) <= STI_VDP)
295 if (plane->status != STI_PLANE_DISABLED)
296 return 0;
297 }
Thierry Reding23886932015-09-24 18:35:38 +0200298 sti_crtc_disable(crtc);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200299 }
300
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200301 return 0;
302}
303
Thierry Reding88e72712015-09-24 18:35:31 +0200304int sti_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200305{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200306 struct sti_private *dev_priv = dev->dev_private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200307 struct sti_compositor *compo = dev_priv->compo;
308 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
Thierry Reding23886932015-09-24 18:35:38 +0200309 struct drm_crtc *crtc = &compo->mixer[pipe]->drm_crtc;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200310
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200311 DRM_DEBUG_DRIVER("\n");
312
Thierry Reding88e72712015-09-24 18:35:31 +0200313 if (sti_vtg_register_client(pipe == STI_MIXER_MAIN ?
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200314 compo->vtg_main : compo->vtg_aux,
Thierry Reding23886932015-09-24 18:35:38 +0200315 vtg_vblank_nb, crtc)) {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200316 DRM_ERROR("Cannot register VTG notifier\n");
317 return -EINVAL;
318 }
319
320 return 0;
321}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200322
Thierry Reding88e72712015-09-24 18:35:31 +0200323void sti_crtc_disable_vblank(struct drm_device *drm_dev, unsigned int pipe)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200324{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200325 struct sti_private *priv = drm_dev->dev_private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200326 struct sti_compositor *compo = priv->compo;
327 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
Thierry Reding23886932015-09-24 18:35:38 +0200328 struct drm_crtc *crtc = &compo->mixer[pipe]->drm_crtc;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200329
330 DRM_DEBUG_DRIVER("\n");
331
Thierry Reding88e72712015-09-24 18:35:31 +0200332 if (sti_vtg_unregister_client(pipe == STI_MIXER_MAIN ?
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200333 compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
334 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
335
336 /* free the resources of the pending requests */
Thierry Reding88e72712015-09-24 18:35:31 +0200337 if (compo->mixer[pipe]->pending_event) {
Thierry Reding23886932015-09-24 18:35:38 +0200338 drm_crtc_vblank_put(crtc);
Thierry Reding88e72712015-09-24 18:35:31 +0200339 compo->mixer[pipe]->pending_event = NULL;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200340 }
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200341}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200342
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300343static const struct drm_crtc_funcs sti_crtc_funcs = {
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100344 .set_config = drm_atomic_helper_set_config,
345 .page_flip = drm_atomic_helper_page_flip,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200346 .destroy = sti_crtc_destroy,
347 .set_property = sti_crtc_set_property,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100348 .reset = drm_atomic_helper_crtc_reset,
349 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
350 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200351};
352
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200353bool sti_crtc_is_main(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200354{
355 struct sti_mixer *mixer = to_sti_mixer(crtc);
356
357 if (mixer->id == STI_MIXER_MAIN)
358 return true;
359
360 return false;
361}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200362
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200363int sti_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
364 struct drm_plane *primary, struct drm_plane *cursor)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200365{
366 struct drm_crtc *crtc = &mixer->drm_crtc;
367 int res;
368
369 res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200370 &sti_crtc_funcs, NULL);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200371 if (res) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200372 DRM_ERROR("Can't initialze CRTC\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200373 return -EINVAL;
374 }
375
376 drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
377
378 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
379 crtc->base.id, sti_mixer_to_str(mixer));
380
381 return 0;
382}