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