blob: 534cd2c810ec3c6ce5c1f39df0ad614da01e5d24 [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>
12#include <drm/drm_crtc_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010013#include <drm/drm_plane_helper.h>
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020014
15#include "sti_compositor.h"
16#include "sti_drm_drv.h"
17#include "sti_drm_crtc.h"
18#include "sti_vtg.h"
19
20static void sti_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
21{
22 DRM_DEBUG_KMS("\n");
23}
24
25static void sti_drm_crtc_prepare(struct drm_crtc *crtc)
26{
27 struct sti_mixer *mixer = to_sti_mixer(crtc);
28 struct device *dev = mixer->dev;
29 struct sti_compositor *compo = dev_get_drvdata(dev);
30
31 compo->enable = true;
32
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
42 sti_mixer_clear_all_layers(mixer);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020043}
44
45static void sti_drm_crtc_commit(struct drm_crtc *crtc)
46{
47 struct sti_mixer *mixer = to_sti_mixer(crtc);
48 struct device *dev = mixer->dev;
49 struct sti_compositor *compo = dev_get_drvdata(dev);
50 struct sti_layer *layer;
51
52 if ((!mixer || !compo)) {
53 DRM_ERROR("Can not find mixer or compositor)\n");
54 return;
55 }
56
57 /* get GDP which is reserved to the CRTC FB */
58 layer = to_sti_layer(crtc->primary);
59 if (layer)
60 sti_layer_commit(layer);
61 else
62 DRM_ERROR("Can not find CRTC dedicated plane (GDP0)\n");
63
64 /* Enable layer on mixer */
65 if (sti_mixer_set_layer_status(mixer, layer, true))
66 DRM_ERROR("Can not enable layer at mixer\n");
Benjamin Gaignardca614aa2014-12-04 11:27:45 +010067
68 drm_crtc_vblank_on(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020069}
70
71static bool sti_drm_crtc_mode_fixup(struct drm_crtc *crtc,
72 const struct drm_display_mode *mode,
73 struct drm_display_mode *adjusted_mode)
74{
75 /* accept the provided drm_display_mode, do not fix it up */
76 return true;
77}
78
79static int
80sti_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
81 struct drm_display_mode *adjusted_mode, int x, int y,
82 struct drm_framebuffer *old_fb)
83{
84 struct sti_mixer *mixer = to_sti_mixer(crtc);
85 struct device *dev = mixer->dev;
86 struct sti_compositor *compo = dev_get_drvdata(dev);
87 struct sti_layer *layer;
88 struct clk *clk;
89 int rate = mode->clock * 1000;
90 int res;
91 unsigned int w, h;
92
93 DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d mode:%d (%s)\n",
94 crtc->base.id, sti_mixer_to_str(mixer),
95 crtc->primary->fb->base.id, mode->base.id, mode->name);
96
97 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
98 mode->vrefresh, mode->clock,
99 mode->hdisplay,
100 mode->hsync_start, mode->hsync_end,
101 mode->htotal,
102 mode->vdisplay,
103 mode->vsync_start, mode->vsync_end,
104 mode->vtotal, mode->type, mode->flags);
105
106 /* Set rate and prepare/enable pixel clock */
107 if (mixer->id == STI_MIXER_MAIN)
108 clk = compo->clk_pix_main;
109 else
110 clk = compo->clk_pix_aux;
111
112 res = clk_set_rate(clk, rate);
113 if (res < 0) {
114 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
115 return -EINVAL;
116 }
117 if (clk_prepare_enable(clk)) {
118 DRM_ERROR("Failed to prepare/enable pix clk\n");
119 return -EINVAL;
120 }
121
122 sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
123 compo->vtg_main : compo->vtg_aux, &crtc->mode);
124
125 /* a GDP is reserved to the CRTC FB */
126 layer = to_sti_layer(crtc->primary);
127 if (!layer) {
128 DRM_ERROR("Can not find GDP0)\n");
129 return -EINVAL;
130 }
131
132 /* copy the mode data adjusted by mode_fixup() into crtc->mode
133 * so that hardware can be set to proper mode
134 */
135 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
136
137 res = sti_mixer_set_layer_depth(mixer, layer);
138 if (res) {
139 DRM_ERROR("Can not set layer depth\n");
140 return -EINVAL;
141 }
142 res = sti_mixer_active_video_area(mixer, &crtc->mode);
143 if (res) {
144 DRM_ERROR("Can not set active video area\n");
145 return -EINVAL;
146 }
147
148 w = crtc->primary->fb->width - x;
149 h = crtc->primary->fb->height - y;
150
151 return sti_layer_prepare(layer, crtc->primary->fb, &crtc->mode,
152 mixer->id, 0, 0, w, h, x, y, w, h);
153}
154
155static int sti_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
156 struct drm_framebuffer *old_fb)
157{
158 struct sti_mixer *mixer = to_sti_mixer(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200159 struct sti_layer *layer;
160 unsigned int w, h;
161 int ret;
162
163 DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d (%d,%d)\n",
164 crtc->base.id, sti_mixer_to_str(mixer),
165 crtc->primary->fb->base.id, x, y);
166
167 /* GDP is reserved to the CRTC FB */
168 layer = to_sti_layer(crtc->primary);
169 if (!layer) {
170 DRM_ERROR("Can not find GDP0)\n");
171 ret = -EINVAL;
172 goto out;
173 }
174
175 w = crtc->primary->fb->width - crtc->x;
176 h = crtc->primary->fb->height - crtc->y;
177
178 ret = sti_layer_prepare(layer, crtc->primary->fb, &crtc->mode,
179 mixer->id, 0, 0, w, h,
180 crtc->x, crtc->y, w, h);
181 if (ret) {
182 DRM_ERROR("Can not prepare layer\n");
183 goto out;
184 }
185
186 sti_drm_crtc_commit(crtc);
187out:
188 return ret;
189}
190
191static void sti_drm_crtc_load_lut(struct drm_crtc *crtc)
192{
193 /* do nothing */
194}
195
196static void sti_drm_crtc_disable(struct drm_crtc *crtc)
197{
198 struct sti_mixer *mixer = to_sti_mixer(crtc);
199 struct device *dev = mixer->dev;
200 struct sti_compositor *compo = dev_get_drvdata(dev);
201 struct sti_layer *layer;
202
203 if (!compo->enable)
204 return;
205
206 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
207
208 /* Disable Background */
209 sti_mixer_set_background_status(mixer, false);
210
211 /* Disable GDP */
212 layer = to_sti_layer(crtc->primary);
213 if (!layer) {
214 DRM_ERROR("Cannot find GDP0\n");
215 return;
216 }
217
218 /* Disable layer at mixer level */
219 if (sti_mixer_set_layer_status(mixer, layer, false))
220 DRM_ERROR("Can not disable %s layer at mixer\n",
221 sti_layer_to_str(layer));
222
223 /* Wait a while to be sure that a Vsync event is received */
224 msleep(WAIT_NEXT_VSYNC_MS);
225
226 /* Then disable layer itself */
227 sti_layer_disable(layer);
228
Benjamin Gaignardca614aa2014-12-04 11:27:45 +0100229 drm_crtc_vblank_off(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200230
231 /* Disable pixel clock and compo IP clocks */
232 if (mixer->id == STI_MIXER_MAIN) {
233 clk_disable_unprepare(compo->clk_pix_main);
234 clk_disable_unprepare(compo->clk_compo_main);
235 } else {
236 clk_disable_unprepare(compo->clk_pix_aux);
237 clk_disable_unprepare(compo->clk_compo_aux);
238 }
239
240 compo->enable = false;
241}
242
243static struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
244 .dpms = sti_drm_crtc_dpms,
245 .prepare = sti_drm_crtc_prepare,
246 .commit = sti_drm_crtc_commit,
247 .mode_fixup = sti_drm_crtc_mode_fixup,
248 .mode_set = sti_drm_crtc_mode_set,
249 .mode_set_base = sti_drm_crtc_mode_set_base,
250 .load_lut = sti_drm_crtc_load_lut,
251 .disable = sti_drm_crtc_disable,
252};
253
254static int sti_drm_crtc_page_flip(struct drm_crtc *crtc,
255 struct drm_framebuffer *fb,
256 struct drm_pending_vblank_event *event,
257 uint32_t page_flip_flags)
258{
259 struct drm_device *drm_dev = crtc->dev;
260 struct drm_framebuffer *old_fb;
261 struct sti_mixer *mixer = to_sti_mixer(crtc);
262 unsigned long flags;
263 int ret;
264
265 DRM_DEBUG_KMS("fb %d --> fb %d\n",
266 crtc->primary->fb->base.id, fb->base.id);
267
268 mutex_lock(&drm_dev->struct_mutex);
269
270 old_fb = crtc->primary->fb;
271 crtc->primary->fb = fb;
272 ret = sti_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y, old_fb);
273 if (ret) {
274 DRM_ERROR("failed\n");
275 crtc->primary->fb = old_fb;
276 goto out;
277 }
278
279 if (event) {
280 event->pipe = mixer->id;
281
282 ret = drm_vblank_get(drm_dev, event->pipe);
283 if (ret) {
284 DRM_ERROR("Cannot get vblank\n");
285 goto out;
286 }
287
288 spin_lock_irqsave(&drm_dev->event_lock, flags);
289 if (mixer->pending_event) {
290 drm_vblank_put(drm_dev, event->pipe);
291 ret = -EBUSY;
292 } else {
293 mixer->pending_event = event;
294 }
295 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
296 }
297out:
298 mutex_unlock(&drm_dev->struct_mutex);
299 return ret;
300}
301
302static void sti_drm_crtc_destroy(struct drm_crtc *crtc)
303{
304 DRM_DEBUG_KMS("\n");
305 drm_crtc_cleanup(crtc);
306}
307
308static int sti_drm_crtc_set_property(struct drm_crtc *crtc,
309 struct drm_property *property,
310 uint64_t val)
311{
312 DRM_DEBUG_KMS("\n");
313 return 0;
314}
315
316int sti_drm_crtc_vblank_cb(struct notifier_block *nb,
317 unsigned long event, void *data)
318{
319 struct drm_device *drm_dev;
320 struct sti_compositor *compo =
321 container_of(nb, struct sti_compositor, vtg_vblank_nb);
322 int *crtc = data;
323 unsigned long flags;
324 struct sti_drm_private *priv;
325
326 drm_dev = compo->mixer[*crtc]->drm_crtc.dev;
327 priv = drm_dev->dev_private;
328
329 if ((event != VTG_TOP_FIELD_EVENT) &&
330 (event != VTG_BOTTOM_FIELD_EVENT)) {
331 DRM_ERROR("unknown event: %lu\n", event);
332 return -EINVAL;
333 }
334
335 drm_handle_vblank(drm_dev, *crtc);
336
337 spin_lock_irqsave(&drm_dev->event_lock, flags);
338 if (compo->mixer[*crtc]->pending_event) {
339 drm_send_vblank_event(drm_dev, -1,
340 compo->mixer[*crtc]->pending_event);
341 drm_vblank_put(drm_dev, *crtc);
342 compo->mixer[*crtc]->pending_event = NULL;
343 }
344 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
345
346 return 0;
347}
348
349int sti_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
350{
351 struct sti_drm_private *dev_priv = dev->dev_private;
352 struct sti_compositor *compo = dev_priv->compo;
353 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
354
355 if (sti_vtg_register_client(crtc == STI_MIXER_MAIN ?
356 compo->vtg_main : compo->vtg_aux,
357 vtg_vblank_nb, crtc)) {
358 DRM_ERROR("Cannot register VTG notifier\n");
359 return -EINVAL;
360 }
361
362 return 0;
363}
364EXPORT_SYMBOL(sti_drm_crtc_enable_vblank);
365
366void sti_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
367{
368 struct sti_drm_private *priv = dev->dev_private;
369 struct sti_compositor *compo = priv->compo;
370 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200371
372 DRM_DEBUG_DRIVER("\n");
373
374 if (sti_vtg_unregister_client(crtc == STI_MIXER_MAIN ?
375 compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
376 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
377
378 /* free the resources of the pending requests */
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200379 if (compo->mixer[crtc]->pending_event) {
380 drm_vblank_put(dev, crtc);
381 compo->mixer[crtc]->pending_event = NULL;
382 }
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200383}
384EXPORT_SYMBOL(sti_drm_crtc_disable_vblank);
385
386static struct drm_crtc_funcs sti_crtc_funcs = {
387 .set_config = drm_crtc_helper_set_config,
388 .page_flip = sti_drm_crtc_page_flip,
389 .destroy = sti_drm_crtc_destroy,
390 .set_property = sti_drm_crtc_set_property,
391};
392
393bool sti_drm_crtc_is_main(struct drm_crtc *crtc)
394{
395 struct sti_mixer *mixer = to_sti_mixer(crtc);
396
397 if (mixer->id == STI_MIXER_MAIN)
398 return true;
399
400 return false;
401}
402
403int sti_drm_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
404 struct drm_plane *primary, struct drm_plane *cursor)
405{
406 struct drm_crtc *crtc = &mixer->drm_crtc;
407 int res;
408
409 res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
410 &sti_crtc_funcs);
411 if (res) {
412 DRM_ERROR("Can not initialze CRTC\n");
413 return -EINVAL;
414 }
415
416 drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
417
418 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
419 crtc->base.id, sti_mixer_to_str(mixer));
420
421 return 0;
422}